1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 ;; DRUMS is an IETF Working Group that works (or worked) on the
25 ;; successor to RFC822, "Standard For The Format Of Arpa Internet Text
26 ;; Messages". This library is based on
27 ;; draft-ietf-drums-msg-fmt-05.txt, released on 1998-08-05.
34 (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
35 "US-ASCII control characters excluding CR, LF and white space.")
36 (defvar ietf-drums-text-token "\001-\011\013\014\016-\177"
37 "US-ASCII characters exlcuding CR and LF.")
38 (defvar ietf-drums-specials-token "()<>[]:;@\\,.\""
39 "Special characters.")
40 (defvar ietf-drums-quote-token "\\"
42 (defvar ietf-drums-wsp-token " \t"
44 (defvar ietf-drums-fws-regexp
45 (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+")
46 "Folding white space.")
47 (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
49 (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
50 "Textual token including full stop.")
51 (defvar ietf-drums-qtext-token
52 (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177")
53 "Non-white-space control characaters, plus the rest of ASCII excluding backslash and doublequote.")
54 (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?="
57 (defvar ietf-drums-syntax-table
58 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
59 (modify-syntax-entry ?\\ "/" table)
60 (modify-syntax-entry ?< "(" table)
61 (modify-syntax-entry ?> ")" table)
62 (modify-syntax-entry ?@ "w" table)
63 (modify-syntax-entry ?/ "w" table)
64 (modify-syntax-entry ?= " " table)
65 (modify-syntax-entry ?* " " table)
66 (modify-syntax-entry ?\; " " table)
67 (modify-syntax-entry ?\' " " table)
70 (defun ietf-drums-token-to-list (token)
71 "Translate TOKEN into a list of characters."
74 (while (< i (length token))
75 (setq c (mm-char-int (aref token i)))
78 ((eq c (mm-char-int ?-))
84 (push (mm-make-char 'ascii b) out)
88 (push (mm-make-char 'ascii c) out))
91 (push (mm-make-char 'ascii b) out))
95 (defsubst ietf-drums-init (string)
96 (set-syntax-table ietf-drums-syntax-table)
98 (ietf-drums-unfold-fws)
99 (goto-char (point-min)))
101 (defun ietf-drums-remove-comments (string)
102 "Remove comments from STRING."
105 (ietf-drums-init string)
107 (setq c (char-after))
112 (delete-region (point) (progn (forward-sexp 1) (point))))
117 (defun ietf-drums-remove-whitespace (string)
118 "Remove whitespace from STRING."
120 (ietf-drums-init string)
123 (setq c (char-after))
129 ((memq c '(? ?\t ?\n))
135 (defun ietf-drums-get-comment (string)
136 "Return the first comment in STRING."
138 (ietf-drums-init string)
141 (setq c (char-after))
149 (progn (forward-sexp 1) (1- (point))))))
154 (defun ietf-drums-strip (string)
155 "Remove comments and whitespace from STRING."
156 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
158 (defun ietf-drums-parse-address (string)
159 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
161 (let (display-name mailbox c display-string)
162 (ietf-drums-init string)
164 (setq c (char-after))
172 (push (buffer-substring
173 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
175 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
176 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
180 (ietf-drums-remove-whitespace
181 (ietf-drums-remove-comments
184 (progn (forward-sexp 1) (1- (point))))))))
185 (t (error "Unknown symbol: %c" c))))
186 ;; If we found no display-name, then we look for comments.
189 (mapconcat 'identity (reverse display-name) " "))
190 (setq display-string (ietf-drums-get-comment string)))
192 (when (string-match "@" display-string)
194 (mapconcat 'identity (nreverse display-name) "")
195 (ietf-drums-get-comment string)))
196 (cons mailbox display-string)))))
198 (defun ietf-drums-parse-addresses (string)
199 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
201 (ietf-drums-init string)
205 (setq c (char-after))
207 ((memq c '(?\" ?< ?\())
210 (push (ietf-drums-parse-address (buffer-substring beg (point)))
216 (push (ietf-drums-parse-address (buffer-substring beg (point)))
220 (defun ietf-drums-unfold-fws ()
221 "Unfold folding white space in the current buffer."
222 (goto-char (point-min))
223 (while (re-search-forward ietf-drums-fws-regexp nil t)
224 (replace-match " " t t))
225 (goto-char (point-min)))
227 (defun ietf-drums-parse-date (string)
228 "Return an Emacs time spec from STRING."
229 (apply 'encode-time (parse-time-string string)))
231 (defun ietf-drums-narrow-to-header ()
232 "Narrow to the header section in the current buffer."
234 (goto-char (point-min))
235 (if (re-search-forward "^\r?$" nil 1)
238 (goto-char (point-min)))
240 (defun ietf-drums-quote-string (string)
241 "Quote string if it needs quoting to be displayed in a header."
242 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
243 (concat "\"" string "\"")
246 (provide 'ietf-drums)
248 ;;; ietf-drums.el ends here