rfc822.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. ;;; rfc822.el --- hairy rfc822 parser for mail and news and suchlike
  2. ;; Copyright (C) 1986-1987, 1990, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
  4. ;; Maintainer: FSF
  5. ;; Keywords: mail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Support functions for parsing RFC-822 headers, used by mail and news
  19. ;; modes.
  20. ;;; Code:
  21. (defvar rfc822-address-start)
  22. ;; uses rfc822-address-start free, throws to address
  23. (defun rfc822-bad-address (reason)
  24. (save-restriction
  25. (insert "_^_")
  26. (narrow-to-region rfc822-address-start
  27. (if (re-search-forward "[,;]" nil t)
  28. (max (point-min) (1- (point)))
  29. (point-max)))
  30. ;; make the error string be suitable for inclusion in (...)
  31. (let ((losers '("\\" "(" ")" "\n")))
  32. (while losers
  33. (goto-char (point-min))
  34. (while (search-forward (car losers) nil t)
  35. (backward-char 1)
  36. (insert ?\\)
  37. (forward-char 1))
  38. (setq losers (cdr losers))))
  39. (goto-char (point-min)) (insert "(Unparsable address -- "
  40. reason
  41. ": \"")
  42. (goto-char (point-max)) (insert "\")"))
  43. (rfc822-nuke-whitespace)
  44. (throw 'address (buffer-substring rfc822-address-start (point))))
  45. (defun rfc822-nuke-whitespace (&optional leave-space)
  46. (let (ch)
  47. (while (cond ((eobp)
  48. nil)
  49. ((= (setq ch (following-char)) ?\()
  50. (forward-char 1)
  51. (while (if (eobp)
  52. (rfc822-bad-address "Unbalanced comment (...)")
  53. (/= (setq ch (following-char)) ?\)))
  54. (cond ((looking-at "[^()\\]+")
  55. (replace-match ""))
  56. ((= ch ?\()
  57. (rfc822-nuke-whitespace))
  58. ((< (point) (1- (point-max)))
  59. (delete-char 2))
  60. (t
  61. (rfc822-bad-address "orphaned backslash"))))
  62. ;; delete remaining "()"
  63. (forward-char -1)
  64. (delete-char 2)
  65. t)
  66. ((memq ch '(?\ ?\t ?\n))
  67. (delete-region (point)
  68. (progn (skip-chars-forward " \t\n") (point)))
  69. t)
  70. (t
  71. nil)))
  72. (or (not leave-space)
  73. (eobp)
  74. (bobp)
  75. (= (preceding-char) ?\ )
  76. (insert ?\ ))))
  77. (defun rfc822-looking-at (regex &optional leave-space)
  78. (if (cond ((stringp regex)
  79. (if (looking-at regex)
  80. (progn (goto-char (match-end 0))
  81. t)))
  82. (t
  83. (if (and (not (eobp))
  84. (= (following-char) regex))
  85. (progn (forward-char 1)
  86. t))))
  87. (let ((tem (match-data)))
  88. (rfc822-nuke-whitespace leave-space)
  89. (set-match-data tem)
  90. t)))
  91. (defun rfc822-snarf-word ()
  92. ;; word is atom | quoted-string
  93. (cond ((= (following-char) ?\")
  94. ;; quoted-string
  95. (or (rfc822-looking-at "\"\\([^\"\\\n]\\|\\\\.\\|\\\\\n\\)*\"")
  96. (rfc822-bad-address "Unterminated quoted string")))
  97. ((rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\".]+")
  98. ;; atom
  99. )
  100. (t
  101. (rfc822-bad-address "Rubbish in address"))))
  102. (defun rfc822-snarf-words ()
  103. (rfc822-snarf-word)
  104. (while (rfc822-looking-at ?.)
  105. (rfc822-snarf-word)))
  106. (defun rfc822-snarf-subdomain ()
  107. ;; sub-domain is domain-ref | domain-literal
  108. (cond ((= (following-char) ?\[)
  109. ;; domain-ref
  110. (or (rfc822-looking-at "\\[\\([^][\\\n]\\|\\\\.\\|\\\\\n\\)*\\]")
  111. (rfc822-bad-address "Unterminated domain literal [...]")))
  112. ((rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\".]+")
  113. ;; domain-literal = atom
  114. )
  115. (t
  116. (rfc822-bad-address "Rubbish in host/domain specification"))))
  117. (defun rfc822-snarf-domain ()
  118. (rfc822-snarf-subdomain)
  119. (while (rfc822-looking-at ?.)
  120. (rfc822-snarf-subdomain)))
  121. (defun rfc822-snarf-frob-list (name separator terminator snarfer
  122. &optional return)
  123. (let ((first t)
  124. (list ())
  125. tem)
  126. (while (cond ((eobp)
  127. (rfc822-bad-address
  128. (format "End of addresses in middle of %s" name)))
  129. ((rfc822-looking-at terminator)
  130. nil)
  131. ((rfc822-looking-at separator)
  132. ;; multiple separators are allowed and do nothing.
  133. (while (rfc822-looking-at separator))
  134. t)
  135. (first
  136. t)
  137. (t
  138. (rfc822-bad-address
  139. (format "Gubbish in middle of %s" name))))
  140. (setq tem (funcall snarfer)
  141. first nil)
  142. (and return tem
  143. (setq list (if (listp tem)
  144. (nconc (reverse tem) list)
  145. (cons tem list)))))
  146. (nreverse list)))
  147. ;; return either an address (a string) or a list of addresses
  148. (defun rfc822-addresses-1 (&optional allow-groups)
  149. ;; Looking for an rfc822 `address'
  150. ;; Either a group (1*word ":" [#mailbox] ";")
  151. ;; or a mailbox (addr-spec | 1*word route-addr)
  152. ;; addr-spec is (local-part "@" domain)
  153. ;; route-addr is ("<" [1#("@" domain) ":"] addr-spec ">")
  154. ;; local-part is (word *("." word))
  155. ;; word is (atom | quoted-string)
  156. ;; quoted-string is ("\([^\"\\n]\|\\.\|\\\n\)")
  157. ;; atom is [^\000-\037\177 ()<>@,;:\".[]]+
  158. ;; domain is sub-domain *("." sub-domain)
  159. ;; sub-domain is domain-ref | domain-literal
  160. ;; domain-literal is "[" *(dtext | quoted-pair) "]"
  161. ;; dtext is "[^][\\n"
  162. ;; domain-ref is atom
  163. (let ((rfc822-address-start (point))
  164. (n 0))
  165. (catch 'address
  166. ;; optimize common cases:
  167. ;; foo
  168. ;; foo.bar@bar.zap
  169. ;; followed by "\\'\\|,\\|([^()\\]*)\\'"
  170. ;; other common cases are:
  171. ;; foo bar <foo.bar@baz.zap>
  172. ;; "foo bar" <foo.bar@baz.zap>
  173. ;; those aren't hacked yet.
  174. (if (and (rfc822-looking-at "[^][\000-\037 ()<>@,;:\\\"]+\\(\\|@[^][\000-\037 ()<>@,;:\\\"]+\\)" t)
  175. (progn (or (eobp)
  176. (rfc822-looking-at ?,))))
  177. (progn
  178. ;; rfc822-looking-at may have inserted a space
  179. (or (bobp) (/= (preceding-char) ?\ ) (delete-char -1))
  180. ;; relying on the fact that rfc822-looking-at <char>
  181. ;; doesn't mung match-data
  182. (throw 'address (buffer-substring rfc822-address-start (match-end 0)))))
  183. (goto-char rfc822-address-start)
  184. (while t
  185. (cond ((and (= n 1) (rfc822-looking-at ?@))
  186. ;; local-part@domain
  187. (rfc822-snarf-domain)
  188. (throw 'address
  189. (buffer-substring rfc822-address-start (point))))
  190. ((rfc822-looking-at ?:)
  191. (cond ((not allow-groups)
  192. (rfc822-bad-address "A group name may not appear here"))
  193. ((= n 0)
  194. (rfc822-bad-address "No name for :...; group")))
  195. ;; group
  196. (throw 'address
  197. ;; return a list of addresses
  198. (rfc822-snarf-frob-list ":...; group" ?\, ?\;
  199. 'rfc822-addresses-1 t)))
  200. ((rfc822-looking-at ?<)
  201. (let ((start (point))
  202. (strip t))
  203. (cond ((rfc822-looking-at ?>)
  204. ;; empty path
  205. ())
  206. ((and (not (eobp)) (= (following-char) ?\@))
  207. ;; <@foo.bar,@baz:quux@abcd.efg>
  208. (rfc822-snarf-frob-list "<...> address" ?\, ?\:
  209. (function (lambda ()
  210. (if (rfc822-looking-at ?\@)
  211. (rfc822-snarf-domain)
  212. (rfc822-bad-address
  213. "Gubbish in route-addr")))))
  214. (rfc822-snarf-words)
  215. (or (rfc822-looking-at ?@)
  216. (rfc822-bad-address "Malformed <..@..> address"))
  217. (rfc822-snarf-domain)
  218. (setq strip nil))
  219. ((progn (rfc822-snarf-words) (rfc822-looking-at ?@))
  220. ; allow <foo> (losing unix seems to do this)
  221. (rfc822-snarf-domain)))
  222. (let ((end (point)))
  223. (if (rfc822-looking-at ?\>)
  224. (throw 'address
  225. (buffer-substring (if strip start (1- start))
  226. (if strip end (1+ end))))
  227. (rfc822-bad-address "Unterminated <...> address")))))
  228. ((looking-at "[^][\000-\037 ()<>@,;:\\.]")
  229. ;; this allows "." to be part of the words preceding
  230. ;; an addr-spec, since many broken mailers output
  231. ;; "Hern K. Herklemeyer III
  232. ;; <yank@megadeath.dod.gods-own-country>"
  233. (let ((again t))
  234. (while again
  235. (or (= n 0) (bobp) (= (preceding-char) ?\ )
  236. (insert ?\ ))
  237. (rfc822-snarf-words)
  238. (setq n (1+ n))
  239. (setq again (or (rfc822-looking-at ?.)
  240. (looking-at "[^][\000-\037 ()<>@,;:\\.]"))))))
  241. ((= n 0)
  242. (throw 'address nil))
  243. ((= n 1) ; allow "foo" (losing unix seems to do this)
  244. (throw 'address
  245. (buffer-substring rfc822-address-start (point))))
  246. ((> n 1)
  247. (rfc822-bad-address "Missing comma between addresses or badly-formatted address"))
  248. ((or (eobp) (= (following-char) ?,))
  249. (rfc822-bad-address "Missing comma or route-spec"))
  250. (t
  251. (rfc822-bad-address "Strange character or missing comma")))))))
  252. (defun rfc822-addresses (header-text)
  253. (if (string-match "\\`[ \t]*\\([^][\000-\037 ()<>@,;:\\\".]+\\)[ \t]*\\'"
  254. header-text)
  255. ;; Make very simple case moderately fast.
  256. (list (substring header-text (match-beginning 1) (match-end 1)))
  257. (let ((buf (generate-new-buffer " rfc822")))
  258. (unwind-protect
  259. (with-current-buffer buf
  260. (make-local-variable 'case-fold-search)
  261. (setq case-fold-search nil) ;For speed(?)
  262. (insert header-text)
  263. ;; unfold continuation lines
  264. (goto-char (point-min))
  265. (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]"
  266. nil t)
  267. (replace-match "\\1 " t))
  268. (goto-char (point-min))
  269. ;; Give `rfc822-address-start' a non-nil initial value to
  270. ;; prevent `rfc822-bad-address' from raising a
  271. ;; `wrong-type-argument' error.
  272. (let* ((rfc822-address-start (point))
  273. list tem
  274. (err
  275. (catch 'address
  276. ;; Note that `rfc822-nuke-whitespace' and
  277. ;; `rfc822-looking-at' can throw.
  278. (rfc822-nuke-whitespace)
  279. (while (not (eobp))
  280. (setq rfc822-address-start (point))
  281. (setq tem
  282. (cond ((rfc822-looking-at ?\,)
  283. nil)
  284. ((looking-at "[][\000-\037@;:\\.>)]")
  285. (forward-char)
  286. (catch 'address ; For rfc822-bad-address
  287. (rfc822-bad-address
  288. (format "Strange character \\%c found"
  289. (preceding-char)))))
  290. (t
  291. (rfc822-addresses-1 t))))
  292. (cond ((null tem))
  293. ((stringp tem)
  294. (setq list (cons tem list)))
  295. (t
  296. (setq list (nconc (nreverse tem) list)))))
  297. nil)))
  298. (nreverse (append (if err (list err)) list))))
  299. (and buf (kill-buffer buf))))))
  300. (provide 'rfc822)
  301. ;;; rfc822.el ends here