1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; DRUMS is an IETF Working Group that works (or worked) on the
25 ;; successor to RFC822, "Standard For The Format Of Arpa Internet Text
26 ;; Messages". This library is based on
27 ;; draft-ietf-drums-msg-fmt-05.txt, released on 1998-08-05.
29 ;; Pending a real regression self test suite, Simon Josefsson added
30 ;; various self test expressions snipped from bug reports, and their
31 ;; expected value, below. I you believe it could be useful, please
32 ;; add your own test cases, or write a real self test suite, or just
35 ;; <m3oekvfd50.fsf@whitebox.m5r.de>
36 ;; (ietf-drums-parse-address "'foo' <foo@example.com>")
37 ;; => ("foo@example.com" . "'foo'")
41 (eval-when-compile (require 'cl))
44 (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
45 "US-ASCII control characters excluding CR, LF and white space.")
46 (defvar ietf-drums-text-token "\001-\011\013\014\016-\177"
47 "US-ASCII characters excluding CR and LF.")
48 (defvar ietf-drums-specials-token "()<>[]:;@\\,.\""
49 "Special characters.")
50 (defvar ietf-drums-quote-token "\\"
52 (defvar ietf-drums-wsp-token " \t"
54 (defvar ietf-drums-fws-regexp
55 (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+")
56 "Folding white space.")
57 (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
59 (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
60 "Textual token including full stop.")
61 (defvar ietf-drums-qtext-token
62 (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177")
63 "Non-white-space control characters, plus the rest of ASCII excluding
64 backslash and doublequote.")
65 (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?="
68 (defvar ietf-drums-syntax-table
69 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
70 (modify-syntax-entry ?\\ "/" table)
71 (modify-syntax-entry ?< "(" table)
72 (modify-syntax-entry ?> ")" table)
73 (modify-syntax-entry ?@ "w" table)
74 (modify-syntax-entry ?/ "w" table)
75 (modify-syntax-entry ?* "_" table)
76 (modify-syntax-entry ?\; "_" table)
77 (modify-syntax-entry ?\' "_" table)
78 (if (featurep 'xemacs)
81 (modify-syntax-entry i "w" table)
85 (defun ietf-drums-token-to-list (token)
86 "Translate TOKEN into a list of characters."
89 (while (< i (length token))
90 (setq c (mm-char-int (aref token i)))
93 ((eq c (mm-char-int ?-))
99 (push (make-char 'ascii b) out)
102 ((= i (length token))
103 (push (make-char 'ascii c) out))
106 (push (make-char 'ascii b) out))
110 (defsubst ietf-drums-init (string)
111 (set-syntax-table ietf-drums-syntax-table)
113 (ietf-drums-unfold-fws)
114 (goto-char (point-min)))
116 (defun ietf-drums-remove-comments (string)
117 "Remove comments from STRING."
120 (ietf-drums-init string)
122 (setq c (char-after))
127 (error (goto-char (point-max)))))
132 (with-syntax-table (copy-syntax-table ietf-drums-syntax-table)
133 (modify-syntax-entry ?\" "w")
136 (error (point-max)))))
141 (defun ietf-drums-remove-whitespace (string)
142 "Remove whitespace from STRING."
144 (ietf-drums-init string)
147 (setq c (char-after))
153 ((memq c '(?\ ?\t ?\n))
159 (defun ietf-drums-get-comment (string)
160 "Return the first comment in STRING."
162 (ietf-drums-init string)
165 (setq c (char-after))
173 (progn (forward-sexp 1) (1- (point))))))
178 (defun ietf-drums-strip (string)
179 "Remove comments and whitespace from STRING."
180 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
182 (defun ietf-drums-parse-address (string)
183 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
185 (let (display-name mailbox c display-string)
186 (ietf-drums-init string)
188 (setq c (char-after))
196 (push (buffer-substring
197 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
199 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
200 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
204 (ietf-drums-remove-whitespace
205 (ietf-drums-remove-comments
208 (progn (forward-sexp 1) (1- (point))))))))
210 (message "Unknown symbol: %c" c)
212 ;; If we found no display-name, then we look for comments.
215 (mapconcat 'identity (reverse display-name) " "))
216 (setq display-string (ietf-drums-get-comment string)))
218 (when (string-match "@" display-string)
220 (mapconcat 'identity (nreverse display-name) "")
221 (ietf-drums-get-comment string)))
222 (cons mailbox display-string)))))
224 (defun ietf-drums-parse-addresses (string &optional rawp)
225 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs.
226 If RAWP, don't actually parse the addresses, but instead return
227 a list of address strings."
231 (ietf-drums-init string)
235 (setq c (char-after))
237 ((memq c '(?\" ?< ?\())
241 (skip-chars-forward "^,"))))
245 (buffer-substring beg (point))
247 (ietf-drums-parse-address
248 (buffer-substring beg (point)))
250 (if address (push address pairs))
257 (buffer-substring beg (point))
259 (ietf-drums-parse-address
260 (buffer-substring beg (point)))
262 (if address (push address pairs))
265 (defun ietf-drums-unfold-fws ()
266 "Unfold folding white space in the current buffer."
267 (goto-char (point-min))
268 (while (re-search-forward ietf-drums-fws-regexp nil t)
269 (replace-match " " t t))
270 (goto-char (point-min)))
272 (defun ietf-drums-parse-date (string)
273 "Return an Emacs time spec from STRING."
274 (apply 'encode-time (parse-time-string string)))
276 (defun ietf-drums-narrow-to-header ()
277 "Narrow to the header section in the current buffer."
279 (goto-char (point-min))
280 (if (re-search-forward "^\r?$" nil 1)
283 (goto-char (point-min)))
285 (defun ietf-drums-quote-string (string)
286 "Quote string if it needs quoting to be displayed in a header."
287 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
288 (concat "\"" string "\"")
291 (defun ietf-drums-make-address (name address)
293 (concat (ietf-drums-quote-string name) " <" address ">")
296 (provide 'ietf-drums)
298 ;; arch-tag: 379a0191-dbae-4ca6-a0f5-d4202c209ef9
299 ;;; ietf-drums.el ends here