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 / rfc822.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  9.8 KB  |  315 lines

  1. ;;; rfc822.el --- hairy rfc822 parser for mail and news and suchlike
  2. ;; Keywords: mail
  3.  
  4. ;; Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
  5. ;; Author Richard Mlynarik.
  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.  
  24. ;; This code should probably be replaced with mail-extr.el once it's a
  25. ;; little more stable.
  26.  
  27.  
  28. (provide 'rfc822)
  29.  
  30. ;; uses address-start free, throws to address
  31. (defun rfc822-bad-address (reason)
  32.   (save-restriction
  33.     (insert "_^_")
  34.     (narrow-to-region address-start
  35.               (if (re-search-forward "[,;]" nil t)
  36.               (max (point-min) (1- (point)))
  37.             (point-max)))
  38.     ;; make the error string be suitable for inclusion in (...)
  39.     (let ((losers '("\\" "(" ")" "\n")))
  40.       (while losers
  41.     (goto-char (point-min))
  42.     (while (search-forward (car losers) nil t)
  43.       (backward-char 1)
  44.       (insert ?\\)
  45.       (forward-char 1))
  46.     (setq losers (cdr losers))))
  47.     (goto-char (point-min)) (insert "(Unparsable address -- "
  48.                     reason
  49.                     ":\n\t  \"")
  50.     (goto-char (point-max)) (insert "\")"))
  51.   (rfc822-nuke-whitespace)
  52.   (throw 'address (buffer-substring address-start (point))))
  53.  
  54. (defun rfc822-nuke-whitespace (&optional leave-space)
  55.   (let (ch)
  56.     (while (cond ((eobp)
  57.           nil)
  58.          ((= (setq ch (following-char)) ?\()
  59.           (forward-char 1)
  60.           (while (if (eobp)
  61.                  (rfc822-bad-address "Unbalanced comment (...)")
  62.                (/= (setq ch (following-char)) ?\)))
  63.             (cond ((looking-at "[^()\\]+")
  64.                (replace-match ""))
  65.               ((= ch ?\()
  66.                (rfc822-nuke-whitespace))
  67.               ((< (point) (1- (point-max)))
  68.                (delete-char 2))
  69.               (t
  70.                (rfc822-bad-address "orphaned backslash"))))
  71.           ;; delete remaining "()"
  72.           (forward-char -1)
  73.           (delete-char 2)
  74.           t)
  75.          ((memq ch '(?\ ?\t ?\n))
  76.           (delete-region (point)
  77.                  (progn (skip-chars-forward " \t\n") (point)))
  78.           t)
  79.          (t
  80.           nil)))
  81.     (or (not leave-space)
  82.     (eobp)
  83.     (bobp)
  84.     (= (preceding-char) ?\ )
  85.     (insert ?\ ))))
  86.  
  87. (defun rfc822-looking-at (regex &optional leave-space)
  88.   (if (cond ((stringp regex)
  89.          (if (looking-at regex)
  90.          (progn (goto-char (match-end 0))
  91.             t)))
  92.         (t
  93.          (if (and (not (eobp))
  94.               (= (following-char) regex))
  95.          (progn (forward-char 1)
  96.             t))))
  97.       (let ((tem (match-data)))
  98.     (rfc822-nuke-whitespace leave-space)
  99.     (store-match-data tem)
  100.     t)))
  101.  
  102. (defun rfc822-snarf-word ()
  103.   ;; word is atom | quoted-string
  104.   (cond ((= (following-char) ?\")
  105.      ;; quoted-string
  106.      (or (rfc822-looking-at "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"")
  107.          (rfc822-bad-address "Unterminated quoted string")))
  108.     ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+")
  109.      ;; atom
  110.      )
  111.     (t
  112.      (rfc822-bad-address "Rubbish in address"))))
  113.  
  114. (defun rfc822-snarf-words ()
  115.   (rfc822-snarf-word)
  116.   (while (rfc822-looking-at ?.)
  117.     (rfc822-snarf-word)))
  118.  
  119. (defun rfc822-snarf-subdomain ()
  120.   ;; sub-domain is domain-ref | domain-literal
  121.   (cond ((= (following-char) ?\[)
  122.      ;; domain-ref
  123.      (or (rfc822-looking-at "\\[\\([^][\\\n]\\|\\\\.\\|\\\\\n\\)*\\]")
  124.          (rfc822-bad-address "Unterminated domain literal [...]")))
  125.     ((rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\".]+")
  126.      ;; domain-literal = atom
  127.      )
  128.     (t
  129.      (rfc822-bad-address "Rubbish in host/domain specification"))))
  130.  
  131. (defun rfc822-snarf-domain ()
  132.   (rfc822-snarf-subdomain)
  133.   (while (rfc822-looking-at ?.)
  134.     (rfc822-snarf-subdomain)))
  135.  
  136. (defun rfc822-snarf-frob-list (name separator terminator snarfer
  137.                     &optional return)
  138.   (let ((first t)
  139.     (list ())
  140.     tem)
  141.     (while (cond ((eobp)
  142.           (rfc822-bad-address
  143.             (format "End of addresses in middle of %s" name)))
  144.          ((rfc822-looking-at terminator)
  145.           nil)
  146.          ((rfc822-looking-at separator)
  147.           ;; multiple separators are allowed and do nothing.
  148.           (while (rfc822-looking-at separator))
  149.           t)
  150.          (first
  151.           t)
  152.          (t
  153.           (rfc822-bad-address
  154.             (format "Gubbish in middle of %s" name))))
  155.       (setq tem (funcall snarfer)
  156.         first nil)
  157.       (and return tem
  158.        (setq list (if (listp tem)
  159.               (nconc (reverse tem) list)
  160.               (cons tem list)))))
  161.     (nreverse list)))
  162.  
  163. ;; return either an address (a string) or a list of addresses
  164. (defun rfc822-addresses-1 (&optional allow-groups)
  165.   ;; Looking for an rfc822 `address'
  166.   ;; Either a group (1*word ":" [#mailbox] ";")
  167.   ;; or a mailbox (addr-spec | 1*word route-addr)
  168.   ;;  addr-spec is (local-part "@" domain)
  169.   ;;  route-addr is ("<" [1#("@" domain) ":"] addr-spec ">")
  170.   ;;  local-part is (word *("." word))
  171.   ;;  word is (atom | quoted-string)
  172.   ;;  quoted-string is ("\([^\"\\n]\|\\.\|\\\n\)")
  173.   ;;  atom is [^\000-\037\177 ()<>@,;:\".[]]+
  174.   ;;  domain is sub-domain *("." sub-domain)
  175.   ;;  sub-domain is domain-ref | domain-literal
  176.   ;;  domain-literal is  "[" *(dtext | quoted-pair) "]"
  177.   ;;  dtext is "[^][\\n"
  178.   ;;  domain-ref is atom
  179.   (let ((address-start (point))
  180.     (n 0))
  181.     (catch 'address
  182.       ;; optimize common cases:
  183.       ;;  foo
  184.       ;;  foo.bar@bar.zap
  185.       ;; followed by "\\'\\|,\\|([^()\\]*)\\'"
  186.       ;; other common cases are:
  187.       ;;  foo bar <foo.bar@baz.zap>
  188.       ;;  "foo bar" <foo.bar@baz.zap>
  189.       ;;  those aren't hacked yet.
  190.       (if (and (rfc822-looking-at "[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\(\\|@[^][\000-\037\177-\377 ()<>@,;:\\\"]+\\)" t)
  191.            (progn (or (eobp)
  192.               (rfc822-looking-at ?,))))
  193.       (progn
  194.         ;; rfc822-looking-at may have inserted a space
  195.         (or (bobp) (/= (preceding-char) ?\ ) (delete-char -1))
  196.         ;; relying on the fact that rfc822-looking-at <char>
  197.         ;;  doesn't mung match-data
  198.         (throw 'address (buffer-substring address-start (match-end 0)))))
  199.       (goto-char address-start)
  200.       (while t
  201.     (cond ((and (= n 1) (rfc822-looking-at ?@))
  202.            ;; local-part@domain
  203.            (rfc822-snarf-domain)
  204.            (throw 'address
  205.          (buffer-substring address-start (point))))
  206.           ((rfc822-looking-at ?:)
  207.            (cond ((not allow-groups)
  208.               (rfc822-bad-address "A group name may not appear here"))
  209.              ((= n 0)
  210.               (rfc822-bad-address "No name for :...; group")))
  211.            ;; group
  212.            (throw 'address
  213.          ;; return a list of addresses
  214.          (rfc822-snarf-frob-list ":...; group" ?\, ?\;
  215.                      'rfc822-addresses-1 t)))
  216.           ((rfc822-looking-at ?<)
  217.            (let ((start (point))
  218.              (strip t))
  219.          (cond ((rfc822-looking-at ?>)
  220.             ;; empty path
  221.             ())
  222.                ((and (not (eobp)) (= (following-char) ?\@))
  223.             ;; <@foo.bar,@baz:quux@abcd.efg>
  224.             (rfc822-snarf-frob-list "<...> address" ?\, ?\:
  225.               (function (lambda ()
  226.                       (if (rfc822-looking-at ?\@)
  227.                       (rfc822-snarf-domain)
  228.                     (rfc822-bad-address
  229.                       "Gubbish in route-addr")))))
  230.             (rfc822-snarf-words)
  231.             (or (rfc822-looking-at ?@)
  232.                 (rfc822-bad-address "Malformed <..@..> address"))
  233.             (rfc822-snarf-domain)
  234.             (setq strip nil))
  235.                ((progn (rfc822-snarf-words) (rfc822-looking-at ?@))
  236.             ; allow <foo> (losing unix seems to do this)
  237.             (rfc822-snarf-domain)))
  238.          (let ((end (point)))
  239.            (if (rfc822-looking-at ?\>)
  240.                (throw 'address
  241.              (buffer-substring (if strip start (1- start))
  242.                        (if strip end (1+ end))))
  243.              (rfc822-bad-address "Unterminated <...> address")))))
  244.           ((looking-at "[^][\000-\037\177-\377 ()<>@,;:\\.]")
  245.            ;; this allows "." to be part of the words preceding
  246.            ;; an addr-spec, since many broken mailers output
  247.            ;; "Hern K. Herklemeyer III
  248.            ;;   <yank@megadeath.dod.gods-own-country>"
  249.            (or (= n 0)
  250.            (= (preceding-char) ?\ )
  251.            (insert ?\ ))
  252.            (rfc822-snarf-words)
  253.            (setq n (1+ n)))
  254.           ((= n 0)
  255.            (throw 'address nil))
  256.           ((= n 1) ; allow "foo" (losing unix seems to do this)
  257.            (throw 'address
  258.          (buffer-substring address-start (point))))
  259.           ((or (eobp) (looking-at ","))
  260.            (rfc822-bad-address "Missing comma or route-spec"))
  261.           (t
  262.            (rfc822-bad-address "Strange character or missing comma")))))))
  263.  
  264.                
  265. (defun rfc822-addresses (header-text)
  266.   (if (string-match "\\`[ \t]*\\([^][\000-\037\177-\377 ()<>@,;:\\\".]+\\)[ \t]*\\'"
  267.                     header-text)
  268.       ;; Make very simple case moderately fast.
  269.       (list (substring header-text (match-beginning 1) (match-end 1)))
  270.     (let ((buf (generate-new-buffer " rfc822")))
  271.       (unwind-protect
  272.     (save-excursion
  273.       (set-buffer buf)
  274.       (make-local-variable 'case-fold-search)
  275.       (setq case-fold-search nil)    ;For speed(?)
  276.       (insert header-text)
  277.       ;; unfold continuation lines
  278.       (goto-char (point-min))
  279.  
  280.       (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t)
  281.         (replace-match "\\1 " t))
  282.  
  283.       (goto-char (point-min))
  284.       (rfc822-nuke-whitespace)
  285.       (let ((list ())
  286.         tem
  287.         (p -1)
  288.         address-start); this is for rfc822-bad-address
  289.         (while (not (eobp))
  290.           (setq address-start (point))
  291.           (setq tem
  292.             (catch 'address ; this is for rfc822-bad-address
  293.               (cond ((rfc822-looking-at ?\,)
  294.                  nil)
  295.                 ((looking-at "[][\000-\037\177-\377@;:\\.>]")
  296.                  (forward-char)
  297.                  (rfc822-bad-address
  298.                    (format "Strange character \\%c found"
  299.                        (preceding-char))))
  300.                 (t
  301.                  (rfc822-addresses-1 t)))))
  302.           (cond ((null tem))
  303.             ((stringp tem)
  304.              (setq list (cons tem list)))
  305.             (t
  306.              (setq list (nconc (nreverse tem) list))))
  307.           (if (= (point) p)
  308.           ;; Punt on losing, misformatted address / infinite loop.
  309.           ;; For example: "lcm36651@uxa.cso.uiuc.edu (Hi!  ;) )"
  310.           (goto-char (point-max)))
  311.           (setq p (point))
  312.           )
  313.         (nreverse list)))
  314.       (and buf (kill-buffer buf))))))
  315.