1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
3 ;; Copyright (C) 1998-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; DRUMS is an IETF Working Group that works (or worked) on the
24 ;; successor to RFC822, "Standard For The Format Of Arpa Internet Text
25 ;; Messages". This library is based on
26 ;; draft-ietf-drums-msg-fmt-05.txt, released on 1998-08-05.
28 ;; Pending a real regression self test suite, Simon Josefsson added
29 ;; various self test expressions snipped from bug reports, and their
30 ;; expected value, below. I you believe it could be useful, please
31 ;; add your own test cases, or write a real self test suite, or just
34 ;; <m3oekvfd50.fsf@whitebox.m5r.de>
35 ;; (ietf-drums-parse-address "'foo' <foo@example.com>")
36 ;; => ("foo@example.com" . "'foo'")
40 (eval-when-compile (require 'cl
))
42 (defvar ietf-drums-no-ws-ctl-token
"\001-\010\013\014\016-\037\177"
43 "US-ASCII control characters excluding CR, LF and white space.")
44 (defvar ietf-drums-text-token
"\001-\011\013\014\016-\177"
45 "US-ASCII characters excluding CR and LF.")
46 (defvar ietf-drums-specials-token
"()<>[]:;@\\,.\""
47 "Special characters.")
48 (defvar ietf-drums-quote-token
"\\"
50 (defvar ietf-drums-wsp-token
" \t"
52 (defvar ietf-drums-fws-regexp
53 (concat "[" ietf-drums-wsp-token
"]*\n[" ietf-drums-wsp-token
"]+")
54 "Folding white space.")
55 (defvar ietf-drums-atext-token
"-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
57 (defvar ietf-drums-dot-atext-token
"-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
58 "Textual token including full stop.")
59 (defvar ietf-drums-qtext-token
60 (concat ietf-drums-no-ws-ctl-token
"\041\043-\133\135-\177")
61 "Non-white-space control characters, plus the rest of ASCII excluding
62 backslash and doublequote.")
63 (defvar ietf-drums-tspecials
"][()<>@,;:\\\"/?="
66 (defvar ietf-drums-syntax-table
67 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
68 (modify-syntax-entry ?
\\ "/" table
)
69 (modify-syntax-entry ?
< "(" table
)
70 (modify-syntax-entry ?
> ")" table
)
71 (modify-syntax-entry ?
@ "w" table
)
72 (modify-syntax-entry ?
/ "w" table
)
73 (modify-syntax-entry ?
* "_" table
)
74 (modify-syntax-entry ?\
; "_" table)
75 (modify-syntax-entry ?
\' "_" table
)
78 (defun ietf-drums-token-to-list (token)
79 "Translate TOKEN into a list of characters."
82 (while (< i
(length token
))
83 (setq c
(aref token i
))
92 (push (make-char 'ascii b
) out
)
96 (push (make-char 'ascii c
) out
))
99 (push (make-char 'ascii b
) out
))
103 (defsubst ietf-drums-init
(string)
104 (set-syntax-table ietf-drums-syntax-table
)
106 (ietf-drums-unfold-fws)
107 (goto-char (point-min)))
109 (defun ietf-drums-remove-comments (string)
110 "Remove comments from STRING."
113 (ietf-drums-init string
)
115 (setq c
(char-after))
120 (error (goto-char (point-max)))))
125 (with-syntax-table (copy-syntax-table ietf-drums-syntax-table
)
126 (modify-syntax-entry ?
\" "w")
129 (error (point-max)))))
134 (defun ietf-drums-remove-whitespace (string)
135 "Remove whitespace from STRING."
137 (ietf-drums-init string
)
140 (setq c
(char-after))
146 ((memq c
'(?\ ?
\t ?
\n))
152 (defun ietf-drums-get-comment (string)
153 "Return the first comment in STRING."
155 (ietf-drums-init string
)
158 (setq c
(char-after))
166 (progn (forward-sexp 1) (1- (point))))))
171 (defun ietf-drums-strip (string)
172 "Remove comments and whitespace from STRING."
173 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string
)))
175 (defun ietf-drums-parse-address (string)
176 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
178 (let (display-name mailbox c display-string
)
179 (ietf-drums-init string
)
181 (setq c
(char-after))
189 (push (buffer-substring
190 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
192 ((looking-at (concat "[" ietf-drums-atext-token
"@" "]"))
193 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
197 (ietf-drums-remove-whitespace
198 (ietf-drums-remove-comments
201 (progn (forward-sexp 1) (1- (point))))))))
204 ;; If we found no display-name, then we look for comments.
207 (mapconcat 'identity
(reverse display-name
) " "))
208 (setq display-string
(ietf-drums-get-comment string
)))
210 (when (and display-string
211 (string-match "@" display-string
))
213 (mapconcat 'identity
(nreverse display-name
) "")
214 (ietf-drums-get-comment string
)))
215 (cons mailbox display-string
)))))
217 (defun ietf-drums-parse-addresses (string &optional rawp
)
218 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs.
219 If RAWP, don't actually parse the addresses, but instead return
220 a list of address strings."
224 (ietf-drums-init string
)
228 (setq c
(char-after))
230 ((memq c
'(?
\" ?
< ?\
())
234 (skip-chars-forward "^,"))))
238 (buffer-substring beg
(point))
240 (ietf-drums-parse-address
241 (buffer-substring beg
(point)))
243 (if address
(push address pairs
))
250 (buffer-substring beg
(point))
252 (ietf-drums-parse-address
253 (buffer-substring beg
(point)))
255 (if address
(push address pairs
))
258 (defun ietf-drums-unfold-fws ()
259 "Unfold folding white space in the current buffer."
260 (goto-char (point-min))
261 (while (re-search-forward ietf-drums-fws-regexp nil t
)
262 (replace-match " " t t
))
263 (goto-char (point-min)))
265 (defun ietf-drums-parse-date (string)
266 "Return an Emacs time spec from STRING."
267 (apply 'encode-time
(parse-time-string string
)))
269 (defun ietf-drums-narrow-to-header ()
270 "Narrow to the header section in the current buffer."
272 (goto-char (point-min))
273 (if (re-search-forward "^\r?$" nil
1)
276 (goto-char (point-min)))
278 (defun ietf-drums-quote-string (string)
279 "Quote string if it needs quoting to be displayed in a header."
280 (if (string-match (concat "[^" ietf-drums-atext-token
"]") string
)
281 (concat "\"" string
"\"")
284 (defun ietf-drums-make-address (name address
)
286 (concat (ietf-drums-quote-string name
) " <" address
">")
289 (provide 'ietf-drums
)
291 ;;; ietf-drums.el ends here