home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / utils / mail-utils.el < prev    next >
Encoding:
Text File  |  1995-08-11  |  7.7 KB  |  218 lines

  1. ;;; mail-utils.el --- Utility functions used both by rmail and rnews
  2.  
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: mail, news, internal
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Code:
  24.  
  25. ;; should be in loaddefs
  26. ;;;###autoload
  27. (defvar mail-use-rfc822 nil
  28.   "*If non-nil, use a full, hairy RFC822 parser on mail addresses.
  29. Otherwise, (the default) use a smaller, somewhat faster and
  30. often-correct parser.")
  31.  
  32. (defun mail-string-delete (string start end)
  33.   "Returns a string containing all of STRING except the part
  34. from START (inclusive) to END (exclusive)."
  35.   (if (null end) (substring string 0 start)
  36.     (concat (substring string 0 start)
  37.         (substring string end nil))))
  38.  
  39. (defun mail-strip-quoted-names (address)
  40.   "Delete comments and quoted strings in an address list ADDRESS.
  41. Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
  42. Return a modified address list."
  43.   (if mail-use-rfc822
  44.       (progn (require 'rfc822)
  45.          (mapconcat 'identity (rfc822-addresses address) ", "))
  46.     (let (pos)
  47.      (string-match "\\`[ \t\n]*" address)
  48.      ;; strip surrounding whitespace
  49.      (setq address (substring address
  50.                   (match-end 0)
  51.                   (string-match "[ \t\n]*\\'" address
  52.                         (match-end 0))))
  53.  
  54.      ;; Detect nested comments.
  55.      (if (string-match "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*(" address)
  56.      ;; Strip nested comments.
  57.      (save-excursion
  58.        (set-buffer (get-buffer-create " *temp*"))
  59.        (erase-buffer)
  60.        (insert address)
  61.        (set-syntax-table lisp-mode-syntax-table)
  62.        (goto-char 1)
  63.        (while (search-forward "(" nil t)
  64.          (forward-char -1)
  65.          (skip-chars-backward " \t")
  66.          (delete-region (point)
  67.                 (save-excursion (forward-sexp 1) (point))))
  68.        (setq address (buffer-string))
  69.        (erase-buffer))
  70.        ;; Strip non-nested comments an easier way.
  71.        (while (setq pos (string-match 
  72.               ;; This doesn't hack rfc822 nested comments
  73.               ;;  `(xyzzy (foo) whinge)' properly.  Big deal.
  74.               "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*)"
  75.               address))
  76.      (setq address
  77.            (mail-string-delete address
  78.                    pos (match-end 0)))))
  79.  
  80.      ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
  81.      (setq pos 0)
  82.      (while (setq pos (string-match
  83.             "[ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*"
  84.             address pos))
  85.        ;; If the next thing is "@", we have "foo bar"@host.  Leave it.
  86.        (if (and (> (length address) (match-end 0))
  87.         (= (aref address (match-end 0)) ?@))
  88.        (setq pos (match-end 0))
  89.      (setq address
  90.            (mail-string-delete address
  91.                    pos (match-end 0)))))
  92.      ;; Retain only part of address in <> delims, if there is such a thing.
  93.      (while (setq pos (string-match "\\(,\\|\\`\\)[^,]*<\\([^>,]*>\\)"
  94.                     address))
  95.        (let ((junk-beg (match-end 1))
  96.          (junk-end (match-beginning 2))
  97.          (close (match-end 0)))
  98.      (setq address (mail-string-delete address (1- close) close))
  99.      (setq address (mail-string-delete address junk-beg junk-end))))
  100.      address)))
  101.   
  102. (or (and (boundp 'rmail-default-dont-reply-to-names)
  103.      (not (null rmail-default-dont-reply-to-names)))
  104.     (setq rmail-default-dont-reply-to-names "info-"))
  105.  
  106. ; rmail-dont-reply-to-names is defined in loaddefs
  107. (defun rmail-dont-reply-to (userids)
  108.   "Returns string of mail addresses USERIDS sans any recipients
  109. that start with matches for  rmail-dont-reply-to-names.
  110. Usenet paths ending in an element that matches are removed also."
  111.   (if (null rmail-dont-reply-to-names)
  112.       (setq rmail-dont-reply-to-names
  113.         (concat (if rmail-default-dont-reply-to-names
  114.             (concat rmail-default-dont-reply-to-names "\\|")
  115.                 "")
  116.             (concat (regexp-quote (user-original-login-name))
  117.                 "\\>"))))
  118.   (let ((match (concat "\\(^\\|,\\)[ \t\n]*\\([^,\n]*!\\|\\)\\("
  119.                rmail-dont-reply-to-names
  120.                "\\)"))
  121.     (case-fold-search t)
  122.     pos epos)
  123.     (while (setq pos (string-match match userids))
  124.       (if (> pos 0) (setq pos (1+ pos)))
  125.       (setq epos
  126.         (if (string-match "[ \t\n,]+" userids (match-end 0))
  127.         (match-end 0)
  128.           (length userids)))
  129.       (setq userids
  130.         (mail-string-delete
  131.           userids pos epos)))
  132.     ;; get rid of any trailing commas
  133.     (if (setq pos (string-match "[ ,\t\n]*\\'" userids))
  134.     (setq userids (substring userids 0 pos)))
  135.     ;; remove leading spaces. they bother me.
  136.     (if (string-match "\\s *" userids)
  137.     (substring userids (match-end 0))
  138.       userids)))
  139.  
  140. (defun mail-fetch-field (field-name &optional last all)
  141.   "Return the value of the header field FIELD.
  142. The buffer is expected to be narrowed to just the headers of the message.
  143. If 2nd arg LAST is non-nil, use the last such field if there are several.
  144. If 3rd arg ALL is non-nil, concatenate all such fields, with commas between."
  145.   (save-excursion
  146.     (goto-char (point-min))
  147.     (let ((case-fold-search t)
  148.       (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
  149.       (goto-char (point-min))
  150.       (if all
  151.       (let ((value ""))
  152.         (while (re-search-forward name nil t)
  153.           (let ((opoint (point)))
  154.         (while (progn (forward-line 1)
  155.                   (looking-at "[ \t]")))
  156.         (setq value (concat value
  157.                     (if (string= value "") "" ", ")
  158.                     (buffer-substring opoint (1- (point)))))))
  159.         (and (not (string= value "")) value))
  160.     (if (re-search-forward name nil t)
  161.         (progn
  162.           (if last (while (re-search-forward name nil t)))
  163.           (let ((opoint (point)))
  164.         (while (progn (forward-line 1)
  165.                   (looking-at "[ \t]")))
  166.         (buffer-substring opoint (1- (point))))))))))
  167.  
  168. ;; Parse a list of tokens separated by commas.
  169. ;; It runs from point to the end of the visible part of the buffer.
  170. ;; Whitespace before or after tokens is ignored,
  171. ;; but whitespace within tokens is kept.
  172. (defun mail-parse-comma-list ()
  173.   (let (accumulated
  174.     beg)
  175.     (skip-chars-forward " ")
  176.     (while (not (eobp))
  177.       (setq beg (point))
  178.       (skip-chars-forward "^,")
  179.       (skip-chars-backward " ")
  180.       (setq accumulated
  181.         (cons (buffer-substring beg (point))
  182.           accumulated))
  183.       (skip-chars-forward "^,")
  184.       (skip-chars-forward ", "))
  185.     accumulated))
  186.  
  187. (defun mail-comma-list-regexp (labels)
  188.   (let (pos)
  189.     (setq pos (or (string-match "[^ \t]" labels) 0))
  190.     ;; Remove leading and trailing whitespace.
  191.     (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
  192.     ;; Change each comma to \|, and flush surrounding whitespace.
  193.     (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
  194.       (setq labels
  195.         (concat (substring labels 0 pos)
  196.             "\\|"
  197.             (substring labels (match-end 0))))))
  198.   labels)
  199.  
  200. (defun mail-rfc822-time-zone (time)
  201.   (let* ((sec (or (car (current-time-zone time)) 0))
  202.      (absmin (/ (abs sec) 60)))
  203.     (format "%c%02d%02d" (if (< sec 0) ?- ?+) (/ absmin 60) (% absmin 60))))
  204.  
  205. (defun mail-rfc822-date ()
  206.   (let* ((time (current-time))
  207.      (s (current-time-string time)))
  208.     (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s)
  209.     (concat (substring s (match-beginning 2) (match-end 2)) " "
  210.         (substring s (match-beginning 1) (match-end 1)) " "
  211.         (substring s (match-beginning 4) (match-end 4)) " "
  212.         (substring s (match-beginning 3) (match-end 3)) " "
  213.         (mail-rfc822-time-zone time))))
  214.  
  215. (provide 'mail-utils)
  216.  
  217. ;;; mail-utils.el ends here
  218.