1 ;;; drums.el --- Functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998 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 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 drums-text-token "\001-\011\013\014\016-\177"
37 "US-ASCII characters exlcuding CR and LF.")
38 (defvar drums-specials-token "()<>[]:;@\\,.\""
39 "Special characters.")
40 (defvar drums-quote-token "\\"
42 (defvar drums-wsp-token " \t"
44 (defvar drums-fws-regexp
45 (concat "[" drums-wsp-token "]*\n[" drums-wsp-token "]+")
46 "Folding white space.")
47 (defvar drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
49 (defvar drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
50 "Textual token including full stop.")
51 (defvar drums-qtext-token
52 (concat 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 drums-tspecials "][()<>@,;:\\\"/?="
57 (defvar 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)
68 (defun drums-token-to-list (token)
69 "Translate TOKEN into a list of characters."
72 (while (< i (length token))
73 (setq c (mm-char-int (aref token i)))
76 ((eq c (mm-char-int ?-))
82 (push (mm-make-char 'ascii b) out)
86 (push (mm-make-char 'ascii c) out))
91 (defsubst drums-init (string)
92 (set-syntax-table drums-syntax-table)
95 (goto-char (point-min)))
97 (defun drums-remove-comments (string)
98 "Remove comments from STRING."
103 (setq c (following-char))
108 (delete-region (point) (progn (forward-sexp 1) (point))))
113 (defun drums-remove-whitespace (string)
114 "Remove comments from STRING."
119 (setq c (following-char))
123 ((memq c '(? ?\t ?\n))
129 (defun drums-get-comment (string)
130 "Return the first comment in STRING."
135 (setq c (following-char))
143 (progn (forward-sexp 1) (1- (point))))))
148 (defun drums-parse-address (string)
149 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
151 (let (display-name mailbox c display-string)
154 (setq c (following-char))
162 (push (buffer-substring
163 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
165 ((looking-at (concat "[" drums-atext-token "@" "]"))
166 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
170 (drums-remove-whitespace
171 (drums-remove-comments
174 (progn (forward-sexp 1) (1- (point))))))))
175 (t (error "Unknown symbol: %c" c))))
176 ;; If we found no display-name, then we look for comments.
179 (mapconcat 'identity (reverse display-name) " "))
180 (setq display-string (drums-get-comment string)))
182 (when (string-match "@" display-string)
184 (mapconcat 'identity (nreverse display-name) "")
185 (drums-get-comment string)))
186 (cons mailbox display-name)))))
188 (defun drums-parse-addresses (string)
189 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
195 (setq c (following-char))
197 ((memq c '(?\" ?< ?\())
200 (push (drums-parse-address (buffer-substring beg (1- (point))))
207 (defun drums-unfold-fws ()
208 "Unfold folding white space in the current buffer."
209 (goto-char (point-min))
210 (while (re-search-forward drums-fws-regexp nil t)
211 (replace-match " " t t))
212 (goto-char (point-min)))
214 (defun drums-parse-date (string)
215 "Return an Emacs time spec from STRING."
216 (apply 'encode-time (parse-time-string string)))
218 (defun drums-content-type-get (ct attribute)
219 "Return the value of ATTRIBUTE from CT."
220 (cdr (assq attribute (cdr ct))))
222 (defun drums-parse-content-type (string)
223 "Parse STRING and return a list."
225 (let ((ttoken (drums-token-to-list drums-text-token))
226 (stoken (drums-token-to-list drums-tspecials))
227 display-name mailbox c display-string parameters
228 attribute value type subtype)
229 (drums-init (drums-remove-whitespace (drums-remove-comments string)))
230 (setq c (following-char))
231 (when (and (memq c ttoken)
232 (not (memq c stoken)))
233 (setq type (downcase (buffer-substring
234 (point) (progn (forward-sexp 1) (point)))))
237 (setq c (following-char))
239 (error "Invalid header: %s" string))
241 (setq c (following-char))
242 (if (and (memq c ttoken)
243 (not (memq c stoken)))
248 (point) (progn (forward-sexp 1) (point))))))
249 (error "Invalid header: %s" string))
250 (setq c (following-char))
252 (error "Invalid header: %s" string))
254 (setq c (following-char))
258 (buffer-substring (1+ (point))
259 (progn (forward-sexp 1) (1- (point))))))
260 ((and (memq c ttoken)
261 (not (memq c stoken)))
262 (setq value (buffer-substring
263 (point) (progn (forward-sexp 1) (point)))))
265 (error "Invalid header: %s" string)))
266 (push (cons attribute value) parameters))
267 `(,type ,@(nreverse parameters))))))
269 (defun drums-narrow-to-header ()
270 "Narrow to the header of the current buffer."
272 (goto-char (point-min))
273 (if (search-forward "\n\n" nil 1)
276 (goto-char (point-min)))
280 ;;; drums.el ends here