Commit | Line | Data |
---|---|---|
23f87bed | 1 | ;;; ietf-drums.el --- Functions for parsing RFC822bis headers |
e84b4b86 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1998-2014 Free Software Foundation, Inc. |
c113de23 GM |
4 | |
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
6 | ;; This file is part of GNU Emacs. | |
7 | ||
5e809f55 | 8 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c113de23 | 9 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
10 | ;; the Free Software Foundation, either version 3 of the License, or |
11 | ;; (at your option) any later version. | |
c113de23 GM |
12 | |
13 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c113de23 GM |
16 | ;; GNU General Public License for more details. |
17 | ||
18 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 19 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c113de23 GM |
20 | |
21 | ;;; Commentary: | |
22 | ||
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. | |
27 | ||
23f87bed MB |
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 | |
32 | ;; remove this. | |
33 | ||
34 | ;; <m3oekvfd50.fsf@whitebox.m5r.de> | |
35 | ;; (ietf-drums-parse-address "'foo' <foo@example.com>") | |
36 | ;; => ("foo@example.com" . "'foo'") | |
37 | ||
c113de23 GM |
38 | ;;; Code: |
39 | ||
3b9d5a55 | 40 | (eval-when-compile (require 'cl)) |
c113de23 GM |
41 | (require 'mm-util) |
42 | ||
43 | (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177" | |
44 | "US-ASCII control characters excluding CR, LF and white space.") | |
45 | (defvar ietf-drums-text-token "\001-\011\013\014\016-\177" | |
8f688cb0 | 46 | "US-ASCII characters excluding CR and LF.") |
c113de23 GM |
47 | (defvar ietf-drums-specials-token "()<>[]:;@\\,.\"" |
48 | "Special characters.") | |
49 | (defvar ietf-drums-quote-token "\\" | |
50 | "Quote character.") | |
51 | (defvar ietf-drums-wsp-token " \t" | |
52 | "White space.") | |
53 | (defvar ietf-drums-fws-regexp | |
54 | (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+") | |
55 | "Folding white space.") | |
56 | (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~" | |
57 | "Textual token.") | |
58 | (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~." | |
59 | "Textual token including full stop.") | |
60 | (defvar ietf-drums-qtext-token | |
61 | (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177") | |
a1506d29 | 62 | "Non-white-space control characters, plus the rest of ASCII excluding |
8f688cb0 | 63 | backslash and doublequote.") |
c113de23 GM |
64 | (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?=" |
65 | "Tspecials.") | |
66 | ||
67 | (defvar ietf-drums-syntax-table | |
68 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
69 | (modify-syntax-entry ?\\ "/" table) | |
70 | (modify-syntax-entry ?< "(" table) | |
71 | (modify-syntax-entry ?> ")" table) | |
72 | (modify-syntax-entry ?@ "w" table) | |
73 | (modify-syntax-entry ?/ "w" table) | |
23f87bed MB |
74 | (modify-syntax-entry ?* "_" table) |
75 | (modify-syntax-entry ?\; "_" table) | |
76 | (modify-syntax-entry ?\' "_" table) | |
77 | (if (featurep 'xemacs) | |
78 | (let ((i 128)) | |
79 | (while (< i 256) | |
80 | (modify-syntax-entry i "w" table) | |
81 | (setq i (1+ i))))) | |
c113de23 GM |
82 | table)) |
83 | ||
84 | (defun ietf-drums-token-to-list (token) | |
85 | "Translate TOKEN into a list of characters." | |
86 | (let ((i 0) | |
87 | b e c out range) | |
88 | (while (< i (length token)) | |
89 | (setq c (mm-char-int (aref token i))) | |
90 | (incf i) | |
91 | (cond | |
92 | ((eq c (mm-char-int ?-)) | |
93 | (if b | |
94 | (setq range t) | |
95 | (push c out))) | |
96 | (range | |
97 | (while (<= b c) | |
01c52d31 | 98 | (push (make-char 'ascii b) out) |
c113de23 GM |
99 | (incf b)) |
100 | (setq range nil)) | |
101 | ((= i (length token)) | |
01c52d31 | 102 | (push (make-char 'ascii c) out)) |
c113de23 GM |
103 | (t |
104 | (when b | |
01c52d31 | 105 | (push (make-char 'ascii b) out)) |
c113de23 GM |
106 | (setq b c)))) |
107 | (nreverse out))) | |
108 | ||
109 | (defsubst ietf-drums-init (string) | |
110 | (set-syntax-table ietf-drums-syntax-table) | |
111 | (insert string) | |
112 | (ietf-drums-unfold-fws) | |
113 | (goto-char (point-min))) | |
114 | ||
115 | (defun ietf-drums-remove-comments (string) | |
116 | "Remove comments from STRING." | |
117 | (with-temp-buffer | |
118 | (let (c) | |
119 | (ietf-drums-init string) | |
120 | (while (not (eobp)) | |
121 | (setq c (char-after)) | |
122 | (cond | |
123 | ((eq c ?\") | |
8e38a1a9 MB |
124 | (condition-case err |
125 | (forward-sexp 1) | |
126 | (error (goto-char (point-max))))) | |
c113de23 | 127 | ((eq c ?\() |
8e38a1a9 MB |
128 | (delete-region |
129 | (point) | |
130 | (condition-case nil | |
131 | (with-syntax-table (copy-syntax-table ietf-drums-syntax-table) | |
132 | (modify-syntax-entry ?\" "w") | |
133 | (forward-sexp 1) | |
134 | (point)) | |
135 | (error (point-max))))) | |
c113de23 GM |
136 | (t |
137 | (forward-char 1)))) | |
138 | (buffer-string)))) | |
139 | ||
140 | (defun ietf-drums-remove-whitespace (string) | |
141 | "Remove whitespace from STRING." | |
142 | (with-temp-buffer | |
143 | (ietf-drums-init string) | |
144 | (let (c) | |
145 | (while (not (eobp)) | |
146 | (setq c (char-after)) | |
147 | (cond | |
148 | ((eq c ?\") | |
149 | (forward-sexp 1)) | |
150 | ((eq c ?\() | |
151 | (forward-sexp 1)) | |
8afb8b29 | 152 | ((memq c '(?\ ?\t ?\n)) |
c113de23 GM |
153 | (delete-char 1)) |
154 | (t | |
155 | (forward-char 1)))) | |
156 | (buffer-string)))) | |
157 | ||
158 | (defun ietf-drums-get-comment (string) | |
159 | "Return the first comment in STRING." | |
160 | (with-temp-buffer | |
161 | (ietf-drums-init string) | |
162 | (let (result c) | |
163 | (while (not (eobp)) | |
164 | (setq c (char-after)) | |
165 | (cond | |
166 | ((eq c ?\") | |
167 | (forward-sexp 1)) | |
168 | ((eq c ?\() | |
169 | (setq result | |
170 | (buffer-substring | |
171 | (1+ (point)) | |
172 | (progn (forward-sexp 1) (1- (point)))))) | |
173 | (t | |
174 | (forward-char 1)))) | |
175 | result))) | |
176 | ||
177 | (defun ietf-drums-strip (string) | |
178 | "Remove comments and whitespace from STRING." | |
179 | (ietf-drums-remove-whitespace (ietf-drums-remove-comments string))) | |
180 | ||
181 | (defun ietf-drums-parse-address (string) | |
182 | "Parse STRING and return a MAILBOX / DISPLAY-NAME pair." | |
183 | (with-temp-buffer | |
184 | (let (display-name mailbox c display-string) | |
185 | (ietf-drums-init string) | |
186 | (while (not (eobp)) | |
187 | (setq c (char-after)) | |
188 | (cond | |
189 | ((or (eq c ? ) | |
190 | (eq c ?\t)) | |
191 | (forward-char 1)) | |
192 | ((eq c ?\() | |
193 | (forward-sexp 1)) | |
194 | ((eq c ?\") | |
195 | (push (buffer-substring | |
196 | (1+ (point)) (progn (forward-sexp 1) (1- (point)))) | |
197 | display-name)) | |
198 | ((looking-at (concat "[" ietf-drums-atext-token "@" "]")) | |
199 | (push (buffer-substring (point) (progn (forward-sexp 1) (point))) | |
200 | display-name)) | |
201 | ((eq c ?<) | |
202 | (setq mailbox | |
203 | (ietf-drums-remove-whitespace | |
204 | (ietf-drums-remove-comments | |
205 | (buffer-substring | |
206 | (1+ (point)) | |
207 | (progn (forward-sexp 1) (1- (point)))))))) | |
01c52d31 MB |
208 | (t |
209 | (message "Unknown symbol: %c" c) | |
210 | (forward-char 1)))) | |
c113de23 GM |
211 | ;; If we found no display-name, then we look for comments. |
212 | (if display-name | |
213 | (setq display-string | |
214 | (mapconcat 'identity (reverse display-name) " ")) | |
215 | (setq display-string (ietf-drums-get-comment string))) | |
216 | (if (not mailbox) | |
5c7e8442 LI |
217 | (when (and display-string |
218 | (string-match "@" display-string)) | |
c113de23 GM |
219 | (cons |
220 | (mapconcat 'identity (nreverse display-name) "") | |
221 | (ietf-drums-get-comment string))) | |
222 | (cons mailbox display-string))))) | |
223 | ||
01c52d31 MB |
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." | |
23f87bed MB |
228 | (if (null string) |
229 | nil | |
230 | (with-temp-buffer | |
231 | (ietf-drums-init string) | |
232 | (let ((beg (point)) | |
233 | pairs c address) | |
234 | (while (not (eobp)) | |
235 | (setq c (char-after)) | |
236 | (cond | |
237 | ((memq c '(?\" ?< ?\()) | |
238 | (condition-case nil | |
239 | (forward-sexp 1) | |
240 | (error | |
241 | (skip-chars-forward "^,")))) | |
242 | ((eq c ?,) | |
243 | (setq address | |
01c52d31 MB |
244 | (if rawp |
245 | (buffer-substring beg (point)) | |
246 | (condition-case nil | |
247 | (ietf-drums-parse-address | |
248 | (buffer-substring beg (point))) | |
249 | (error nil)))) | |
23f87bed MB |
250 | (if address (push address pairs)) |
251 | (forward-char 1) | |
252 | (setq beg (point))) | |
253 | (t | |
254 | (forward-char 1)))) | |
255 | (setq address | |
01c52d31 MB |
256 | (if rawp |
257 | (buffer-substring beg (point)) | |
258 | (condition-case nil | |
259 | (ietf-drums-parse-address | |
260 | (buffer-substring beg (point))) | |
261 | (error nil)))) | |
23f87bed MB |
262 | (if address (push address pairs)) |
263 | (nreverse pairs))))) | |
c113de23 GM |
264 | |
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))) | |
271 | ||
272 | (defun ietf-drums-parse-date (string) | |
273 | "Return an Emacs time spec from STRING." | |
274 | (apply 'encode-time (parse-time-string string))) | |
275 | ||
276 | (defun ietf-drums-narrow-to-header () | |
277 | "Narrow to the header section in the current buffer." | |
278 | (narrow-to-region | |
279 | (goto-char (point-min)) | |
280 | (if (re-search-forward "^\r?$" nil 1) | |
281 | (match-beginning 0) | |
282 | (point-max))) | |
283 | (goto-char (point-min))) | |
284 | ||
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 "\"") | |
289 | string)) | |
290 | ||
01c52d31 MB |
291 | (defun ietf-drums-make-address (name address) |
292 | (if name | |
293 | (concat (ietf-drums-quote-string name) " <" address ">") | |
294 | address)) | |
295 | ||
c113de23 GM |
296 | (provide 'ietf-drums) |
297 | ||
298 | ;;; ietf-drums.el ends here |