Port thread.c to OpenBSD ARM
[emacs.git] / lisp / mail / ietf-drums.el
blob86496beb0fddae114d68779eaff3694fb17fd34b
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 <https://www.gnu.org/licenses/>.
21 ;;; Commentary:
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
32 ;; remove this.
34 ;; <m3oekvfd50.fsf@whitebox.m5r.de>
35 ;; (ietf-drums-parse-address "'foo' <foo@example.com>")
36 ;; => ("foo@example.com" . "'foo'")
38 ;;; Code:
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 "\\"
49 "Quote character.")
50 (defvar ietf-drums-wsp-token " \t"
51 "White space.")
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!#$%&'*+/=?_`{|}~"
56 "Textual token.")
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 "][()<>@,;:\\\"/?="
64 "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)
76 table))
78 (defun ietf-drums-token-to-list (token)
79 "Translate TOKEN into a list of characters."
80 (let ((i 0)
81 b e c out range)
82 (while (< i (length token))
83 (setq c (aref token i))
84 (incf i)
85 (cond
86 ((eq c ?-)
87 (if b
88 (setq range t)
89 (push c out)))
90 (range
91 (while (<= b c)
92 (push (make-char 'ascii b) out)
93 (incf b))
94 (setq range nil))
95 ((= i (length token))
96 (push (make-char 'ascii c) out))
98 (when b
99 (push (make-char 'ascii b) out))
100 (setq b c))))
101 (nreverse out)))
103 (defsubst ietf-drums-init (string)
104 (set-syntax-table ietf-drums-syntax-table)
105 (insert string)
106 (ietf-drums-unfold-fws)
107 (goto-char (point-min)))
109 (defun ietf-drums-remove-comments (string)
110 "Remove comments from STRING."
111 (with-temp-buffer
112 (let (c)
113 (ietf-drums-init string)
114 (while (not (eobp))
115 (setq c (char-after))
116 (cond
117 ((eq c ?\")
118 (condition-case err
119 (forward-sexp 1)
120 (error (goto-char (point-max)))))
121 ((eq c ?\()
122 (delete-region
123 (point)
124 (condition-case nil
125 (with-syntax-table (copy-syntax-table ietf-drums-syntax-table)
126 (modify-syntax-entry ?\" "w")
127 (forward-sexp 1)
128 (point))
129 (error (point-max)))))
131 (forward-char 1))))
132 (buffer-string))))
134 (defun ietf-drums-remove-whitespace (string)
135 "Remove whitespace from STRING."
136 (with-temp-buffer
137 (ietf-drums-init string)
138 (let (c)
139 (while (not (eobp))
140 (setq c (char-after))
141 (cond
142 ((eq c ?\")
143 (forward-sexp 1))
144 ((eq c ?\()
145 (forward-sexp 1))
146 ((memq c '(?\ ?\t ?\n ?\r))
147 (delete-char 1))
149 (forward-char 1))))
150 (buffer-string))))
152 (defun ietf-drums-get-comment (string)
153 "Return the first comment in STRING."
154 (with-temp-buffer
155 (ietf-drums-init string)
156 (let (result c)
157 (while (not (eobp))
158 (setq c (char-after))
159 (cond
160 ((eq c ?\")
161 (forward-sexp 1))
162 ((eq c ?\()
163 (setq result
164 (buffer-substring
165 (1+ (point))
166 (progn (forward-sexp 1) (1- (point))))))
168 (forward-char 1))))
169 result)))
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-remove-garbage (string)
176 "Remove some garbage from STRING."
177 (while (string-match "[][()<>@,;:\\\"/?=]+" string)
178 (setq string (concat (substring string 0 (match-beginning 0))
179 (substring string (match-end 0)))))
180 string)
182 (defun ietf-drums-strip-cte (string)
183 "Remove comments, whitespace and garbage from STRING.
184 STRING is assumed to be a string that is extracted from
185 the Content-Transfer-Encoding header of a mail."
186 (ietf-drums-remove-garbage (inline (ietf-drums-strip string))))
188 (defun ietf-drums-parse-address (string)
189 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
190 (with-temp-buffer
191 (let (display-name mailbox c display-string)
192 (ietf-drums-init string)
193 (while (not (eobp))
194 (setq c (char-after))
195 ;; If we have an uneven number of quote characters,
196 ;; `forward-sexp' will fail. In these cases, just delete the
197 ;; final of these quote characters.
198 (when (and (eq c ?\")
199 (not
200 (save-excursion
201 (ignore-errors
202 (forward-sexp 1)
203 t))))
204 (delete-char 1)
205 (setq c (char-after)))
206 (cond
207 ((or (eq c ? )
208 (eq c ?\t))
209 (forward-char 1))
210 ((eq c ?\()
211 (forward-sexp 1))
212 ((eq c ?\")
213 (push (buffer-substring
214 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
215 display-name))
216 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
217 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
218 display-name))
219 ((eq c ?<)
220 (setq mailbox
221 (ietf-drums-remove-whitespace
222 (ietf-drums-remove-comments
223 (buffer-substring
224 (1+ (point))
225 (progn (forward-sexp 1) (1- (point))))))))
227 (forward-char 1))))
228 ;; If we found no display-name, then we look for comments.
229 (if display-name
230 (setq display-string
231 (mapconcat 'identity (reverse display-name) " "))
232 (setq display-string (ietf-drums-get-comment string)))
233 (if (not mailbox)
234 (when (and display-string
235 (string-match "@" display-string))
236 (cons
237 (mapconcat 'identity (nreverse display-name) "")
238 (ietf-drums-get-comment string)))
239 (cons mailbox display-string)))))
241 (defun ietf-drums-parse-addresses (string &optional rawp)
242 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs.
243 If RAWP, don't actually parse the addresses, but instead return
244 a list of address strings."
245 (if (null string)
247 (with-temp-buffer
248 (ietf-drums-init string)
249 (let ((beg (point))
250 pairs c address)
251 (while (not (eobp))
252 (setq c (char-after))
253 (cond
254 ((memq c '(?\" ?< ?\())
255 (condition-case nil
256 (forward-sexp 1)
257 (error
258 (skip-chars-forward "^,"))))
259 ((eq c ?,)
260 (setq address
261 (if rawp
262 (buffer-substring beg (point))
263 (condition-case nil
264 (ietf-drums-parse-address
265 (buffer-substring beg (point)))
266 (error nil))))
267 (if address (push address pairs))
268 (forward-char 1)
269 (setq beg (point)))
271 (forward-char 1))))
272 (setq address
273 (if rawp
274 (buffer-substring beg (point))
275 (condition-case nil
276 (ietf-drums-parse-address
277 (buffer-substring beg (point)))
278 (error nil))))
279 (if address (push address pairs))
280 (nreverse pairs)))))
282 (defun ietf-drums-unfold-fws ()
283 "Unfold folding white space in the current buffer."
284 (goto-char (point-min))
285 (while (re-search-forward ietf-drums-fws-regexp nil t)
286 (replace-match " " t t))
287 (goto-char (point-min)))
289 (defun ietf-drums-parse-date (string)
290 "Return an Emacs time spec from STRING."
291 (apply 'encode-time (parse-time-string string)))
293 (defun ietf-drums-narrow-to-header ()
294 "Narrow to the header section in the current buffer."
295 (narrow-to-region
296 (goto-char (point-min))
297 (if (re-search-forward "^\r?$" nil 1)
298 (match-beginning 0)
299 (point-max)))
300 (goto-char (point-min)))
302 (defun ietf-drums-quote-string (string)
303 "Quote string if it needs quoting to be displayed in a header."
304 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
305 (concat "\"" string "\"")
306 string))
308 (defun ietf-drums-make-address (name address)
309 (if name
310 (concat (ietf-drums-quote-string name) " <" address ">")
311 address))
313 (provide 'ietf-drums)
315 ;;; ietf-drums.el ends here