home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / utils / mail-utils.el.z / mail-utils.el
Encoding:
Text File  |  1998-05-21  |  9.0 KB  |  261 lines

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