56927
|
1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
|
|
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
|
31717
|
3 ;; Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
|
6 ;; This file is part of GNU Emacs.
|
|
7
|
|
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 2, or (at your option)
|
|
11 ;; any later version.
|
|
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
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 ;; GNU General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 ;; Boston, MA 02111-1307, USA.
|
|
22
|
|
23 ;;; Commentary:
|
|
24
|
|
25 ;; DRUMS is an IETF Working Group that works (or worked) on the
|
|
26 ;; successor to RFC822, "Standard For The Format Of Arpa Internet Text
|
|
27 ;; Messages". This library is based on
|
|
28 ;; draft-ietf-drums-msg-fmt-05.txt, released on 1998-08-05.
|
|
29
|
56927
|
30 ;; Pending a real regression self test suite, Simon Josefsson added
|
|
31 ;; various self test expressions snipped from bug reports, and their
|
|
32 ;; expected value, below. I you believe it could be useful, please
|
|
33 ;; add your own test cases, or write a real self test suite, or just
|
|
34 ;; remove this.
|
|
35
|
|
36 ;; <m3oekvfd50.fsf@whitebox.m5r.de>
|
|
37 ;; (ietf-drums-parse-address "'foo' <foo@example.com>")
|
|
38 ;; => ("foo@example.com" . "'foo'")
|
|
39
|
31717
|
40 ;;; Code:
|
|
41
|
32514
|
42 (eval-when-compile (require 'cl))
|
31717
|
43 (require 'time-date)
|
|
44 (require 'mm-util)
|
|
45
|
|
46 (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
|
|
47 "US-ASCII control characters excluding CR, LF and white space.")
|
|
48 (defvar ietf-drums-text-token "\001-\011\013\014\016-\177"
|
48588
|
49 "US-ASCII characters excluding CR and LF.")
|
31717
|
50 (defvar ietf-drums-specials-token "()<>[]:;@\\,.\""
|
|
51 "Special characters.")
|
|
52 (defvar ietf-drums-quote-token "\\"
|
|
53 "Quote character.")
|
|
54 (defvar ietf-drums-wsp-token " \t"
|
|
55 "White space.")
|
|
56 (defvar ietf-drums-fws-regexp
|
|
57 (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+")
|
|
58 "Folding white space.")
|
|
59 (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
|
|
60 "Textual token.")
|
|
61 (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
|
|
62 "Textual token including full stop.")
|
|
63 (defvar ietf-drums-qtext-token
|
|
64 (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177")
|
49598
|
65 "Non-white-space control characters, plus the rest of ASCII excluding
|
48588
|
66 backslash and doublequote.")
|
31717
|
67 (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?="
|
|
68 "Tspecials.")
|
|
69
|
|
70 (defvar ietf-drums-syntax-table
|
|
71 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
|
|
72 (modify-syntax-entry ?\\ "/" table)
|
|
73 (modify-syntax-entry ?< "(" table)
|
|
74 (modify-syntax-entry ?> ")" table)
|
|
75 (modify-syntax-entry ?@ "w" table)
|
|
76 (modify-syntax-entry ?/ "w" table)
|
56927
|
77 (modify-syntax-entry ?* "_" table)
|
|
78 (modify-syntax-entry ?\; "_" table)
|
|
79 (modify-syntax-entry ?\' "_" table)
|
|
80 (if (featurep 'xemacs)
|
|
81 (let ((i 128))
|
|
82 (while (< i 256)
|
|
83 (modify-syntax-entry i "w" table)
|
|
84 (setq i (1+ i)))))
|
31717
|
85 table))
|
|
86
|
|
87 (defun ietf-drums-token-to-list (token)
|
|
88 "Translate TOKEN into a list of characters."
|
|
89 (let ((i 0)
|
|
90 b e c out range)
|
|
91 (while (< i (length token))
|
|
92 (setq c (mm-char-int (aref token i)))
|
|
93 (incf i)
|
|
94 (cond
|
|
95 ((eq c (mm-char-int ?-))
|
|
96 (if b
|
|
97 (setq range t)
|
|
98 (push c out)))
|
|
99 (range
|
|
100 (while (<= b c)
|
|
101 (push (mm-make-char 'ascii b) out)
|
|
102 (incf b))
|
|
103 (setq range nil))
|
|
104 ((= i (length token))
|
|
105 (push (mm-make-char 'ascii c) out))
|
|
106 (t
|
|
107 (when b
|
|
108 (push (mm-make-char 'ascii b) out))
|
|
109 (setq b c))))
|
|
110 (nreverse out)))
|
|
111
|
|
112 (defsubst ietf-drums-init (string)
|
|
113 (set-syntax-table ietf-drums-syntax-table)
|
|
114 (insert string)
|
|
115 (ietf-drums-unfold-fws)
|
|
116 (goto-char (point-min)))
|
|
117
|
|
118 (defun ietf-drums-remove-comments (string)
|
|
119 "Remove comments from STRING."
|
|
120 (with-temp-buffer
|
|
121 (let (c)
|
|
122 (ietf-drums-init string)
|
|
123 (while (not (eobp))
|
|
124 (setq c (char-after))
|
|
125 (cond
|
|
126 ((eq c ?\")
|
|
127 (forward-sexp 1))
|
|
128 ((eq c ?\()
|
|
129 (delete-region (point) (progn (forward-sexp 1) (point))))
|
|
130 (t
|
|
131 (forward-char 1))))
|
|
132 (buffer-string))))
|
|
133
|
|
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))
|
49844
|
146 ((memq c '(?\ ?\t ?\n))
|
31717
|
147 (delete-char 1))
|
|
148 (t
|
|
149 (forward-char 1))))
|
|
150 (buffer-string))))
|
|
151
|
|
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))))))
|
|
167 (t
|
|
168 (forward-char 1))))
|
|
169 result)))
|
|
170
|
|
171 (defun ietf-drums-strip (string)
|
|
172 "Remove comments and whitespace from STRING."
|
|
173 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
|
|
174
|
|
175 (defun ietf-drums-parse-address (string)
|
|
176 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
|
|
177 (with-temp-buffer
|
|
178 (let (display-name mailbox c display-string)
|
|
179 (ietf-drums-init string)
|
|
180 (while (not (eobp))
|
|
181 (setq c (char-after))
|
|
182 (cond
|
|
183 ((or (eq c ? )
|
|
184 (eq c ?\t))
|
|
185 (forward-char 1))
|
|
186 ((eq c ?\()
|
|
187 (forward-sexp 1))
|
|
188 ((eq c ?\")
|
|
189 (push (buffer-substring
|
|
190 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
|
|
191 display-name))
|
|
192 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
|
|
193 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
|
|
194 display-name))
|
|
195 ((eq c ?<)
|
|
196 (setq mailbox
|
|
197 (ietf-drums-remove-whitespace
|
|
198 (ietf-drums-remove-comments
|
|
199 (buffer-substring
|
|
200 (1+ (point))
|
|
201 (progn (forward-sexp 1) (1- (point))))))))
|
|
202 (t (error "Unknown symbol: %c" c))))
|
|
203 ;; If we found no display-name, then we look for comments.
|
|
204 (if display-name
|
|
205 (setq display-string
|
|
206 (mapconcat 'identity (reverse display-name) " "))
|
|
207 (setq display-string (ietf-drums-get-comment string)))
|
|
208 (if (not mailbox)
|
|
209 (when (string-match "@" display-string)
|
|
210 (cons
|
|
211 (mapconcat 'identity (nreverse display-name) "")
|
|
212 (ietf-drums-get-comment string)))
|
|
213 (cons mailbox display-string)))))
|
|
214
|
|
215 (defun ietf-drums-parse-addresses (string)
|
|
216 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
|
56927
|
217 (if (null string)
|
|
218 nil
|
|
219 (with-temp-buffer
|
|
220 (ietf-drums-init string)
|
|
221 (let ((beg (point))
|
|
222 pairs c address)
|
|
223 (while (not (eobp))
|
|
224 (setq c (char-after))
|
|
225 (cond
|
|
226 ((memq c '(?\" ?< ?\())
|
|
227 (condition-case nil
|
|
228 (forward-sexp 1)
|
|
229 (error
|
|
230 (skip-chars-forward "^,"))))
|
|
231 ((eq c ?,)
|
|
232 (setq address
|
|
233 (condition-case nil
|
|
234 (ietf-drums-parse-address
|
|
235 (buffer-substring beg (point)))
|
|
236 (error nil)))
|
|
237 (if address (push address pairs))
|
|
238 (forward-char 1)
|
|
239 (setq beg (point)))
|
|
240 (t
|
|
241 (forward-char 1))))
|
|
242 (setq address
|
|
243 (condition-case nil
|
|
244 (ietf-drums-parse-address
|
|
245 (buffer-substring beg (point)))
|
|
246 (error nil)))
|
|
247 (if address (push address pairs))
|
|
248 (nreverse pairs)))))
|
31717
|
249
|
|
250 (defun ietf-drums-unfold-fws ()
|
|
251 "Unfold folding white space in the current buffer."
|
|
252 (goto-char (point-min))
|
|
253 (while (re-search-forward ietf-drums-fws-regexp nil t)
|
|
254 (replace-match " " t t))
|
|
255 (goto-char (point-min)))
|
|
256
|
|
257 (defun ietf-drums-parse-date (string)
|
|
258 "Return an Emacs time spec from STRING."
|
|
259 (apply 'encode-time (parse-time-string string)))
|
|
260
|
|
261 (defun ietf-drums-narrow-to-header ()
|
|
262 "Narrow to the header section in the current buffer."
|
|
263 (narrow-to-region
|
|
264 (goto-char (point-min))
|
|
265 (if (re-search-forward "^\r?$" nil 1)
|
|
266 (match-beginning 0)
|
|
267 (point-max)))
|
|
268 (goto-char (point-min)))
|
|
269
|
|
270 (defun ietf-drums-quote-string (string)
|
|
271 "Quote string if it needs quoting to be displayed in a header."
|
|
272 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
|
|
273 (concat "\"" string "\"")
|
|
274 string))
|
|
275
|
|
276 (provide 'ietf-drums)
|
|
277
|
52401
|
278 ;;; arch-tag: 379a0191-dbae-4ca6-a0f5-d4202c209ef9
|
31717
|
279 ;;; ietf-drums.el ends here
|