1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 ;; DRUMS is an IETF Working Group that works (or worked) on the
27 ;; successor to RFC822, "Standard For The Format Of Arpa Internet Text
28 ;; Messages". This library is based on
29 ;; draft-ietf-drums-msg-fmt-05.txt, released on 1998-08-05.
31 ;; Pending a real regression self test suite, Simon Josefsson added
32 ;; various self test expressions snipped from bug reports, and their
33 ;; expected value, below. I you believe it could be useful, please
34 ;; add your own test cases, or write a real self test suite, or just
37 ;; <m3oekvfd50.fsf@whitebox.m5r.de>
38 ;; (ietf-drums-parse-address "'foo' <foo@example.com>")
39 ;; => ("foo@example.com" . "'foo'")
43 (eval-when-compile (require 'cl))
47 (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
48 "US-ASCII control characters excluding CR, LF and white space.")
49 (defvar ietf-drums-text-token "\001-\011\013\014\016-\177"
50 "US-ASCII characters excluding CR and LF.")
51 (defvar ietf-drums-specials-token "()<>[]:;@\\,.\""
52 "Special characters.")
53 (defvar ietf-drums-quote-token "\\"
55 (defvar ietf-drums-wsp-token " \t"
57 (defvar ietf-drums-fws-regexp
58 (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+")
59 "Folding white space.")
60 (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
62 (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
63 "Textual token including full stop.")
64 (defvar ietf-drums-qtext-token
65 (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177")
66 "Non-white-space control characters, plus the rest of ASCII excluding
67 backslash and doublequote.")
68 (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?="
71 (defvar ietf-drums-syntax-table
72 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
73 (modify-syntax-entry ?\\ "/" table)
74 (modify-syntax-entry ?< "(" table)
75 (modify-syntax-entry ?> ")" table)
76 (modify-syntax-entry ?@ "w" table)
77 (modify-syntax-entry ?/ "w" table)
78 (modify-syntax-entry ?* "_" table)
79 (modify-syntax-entry ?\; "_" table)
80 (modify-syntax-entry ?\' "_" table)
81 (if (featurep 'xemacs)
84 (modify-syntax-entry i "w" table)
88 (defun ietf-drums-token-to-list (token)
89 "Translate TOKEN into a list of characters."
92 (while (< i (length token))
93 (setq c (mm-char-int (aref token i)))
96 ((eq c (mm-char-int ?-))
102 (push (make-char 'ascii b) out)
105 ((= i (length token))
106 (push (make-char 'ascii c) out))
109 (push (make-char 'ascii b) out))
113 (defsubst ietf-drums-init (string)
114 (set-syntax-table ietf-drums-syntax-table)
116 (ietf-drums-unfold-fws)
117 (goto-char (point-min)))
119 (defun ietf-drums-remove-comments (string)
120 "Remove comments from STRING."
123 (ietf-drums-init string)
125 (setq c (char-after))
130 (delete-region (point) (progn (forward-sexp 1) (point))))
135 (defun ietf-drums-remove-whitespace (string)
136 "Remove whitespace from STRING."
138 (ietf-drums-init string)
141 (setq c (char-after))
147 ((memq c '(?\ ?\t ?\n))
153 (defun ietf-drums-get-comment (string)
154 "Return the first comment in STRING."
156 (ietf-drums-init string)
159 (setq c (char-after))
167 (progn (forward-sexp 1) (1- (point))))))
172 (defun ietf-drums-strip (string)
173 "Remove comments and whitespace from STRING."
174 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
176 (defun ietf-drums-parse-address (string)
177 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
179 (let (display-name mailbox c display-string)
180 (ietf-drums-init string)
182 (setq c (char-after))
190 (push (buffer-substring
191 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
193 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
194 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
198 (ietf-drums-remove-whitespace
199 (ietf-drums-remove-comments
202 (progn (forward-sexp 1) (1- (point))))))))
203 (t (error "Unknown symbol: %c" c))))
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 (string-match "@" display-string)
212 (mapconcat 'identity (nreverse display-name) "")
213 (ietf-drums-get-comment string)))
214 (cons mailbox display-string)))))
216 (defun ietf-drums-parse-addresses (string)
217 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
221 (ietf-drums-init string)
225 (setq c (char-after))
227 ((memq c '(?\" ?< ?\())
231 (skip-chars-forward "^,"))))
235 (ietf-drums-parse-address
236 (buffer-substring beg (point)))
238 (if address (push address pairs))
245 (ietf-drums-parse-address
246 (buffer-substring beg (point)))
248 (if address (push address pairs))
251 (defun ietf-drums-unfold-fws ()
252 "Unfold folding white space in the current buffer."
253 (goto-char (point-min))
254 (while (re-search-forward ietf-drums-fws-regexp nil t)
255 (replace-match " " t t))
256 (goto-char (point-min)))
258 (defun ietf-drums-parse-date (string)
259 "Return an Emacs time spec from STRING."
260 (apply 'encode-time (parse-time-string string)))
262 (defun ietf-drums-narrow-to-header ()
263 "Narrow to the header section in the current buffer."
265 (goto-char (point-min))
266 (if (re-search-forward "^\r?$" nil 1)
269 (goto-char (point-min)))
271 (defun ietf-drums-quote-string (string)
272 "Quote string if it needs quoting to be displayed in a header."
273 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
274 (concat "\"" string "\"")
277 (defun ietf-drums-make-address (name address)
279 (concat (ietf-drums-quote-string name) " <" address ">")
282 (provide 'ietf-drums)
284 ;;; ietf-drums.el ends here