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 comments 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-parse-address (string)
155 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
157 (let (display-name mailbox c display-string)
158 (ietf-drums-init string)
160 (setq c (char-after))
168 (push (buffer-substring
169 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
171 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
172 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
176 (ietf-drums-remove-whitespace
177 (ietf-drums-remove-comments
180 (progn (forward-sexp 1) (1- (point))))))))
181 (t (error "Unknown symbol: %c" c))))
182 ;; If we found no display-name, then we look for comments.
185 (mapconcat 'identity (reverse display-name) " "))
186 (setq display-string (ietf-drums-get-comment string)))
188 (when (string-match "@" display-string)
190 (mapconcat 'identity (nreverse display-name) "")
191 (ietf-drums-get-comment string)))
192 (cons mailbox display-string)))))
194 (defun ietf-drums-parse-addresses (string)
195 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
197 (ietf-drums-init string)
201 (setq c (char-after))
203 ((memq c '(?\" ?< ?\())
206 (push (ietf-drums-parse-address (buffer-substring beg (point)))
212 (push (ietf-drums-parse-address (buffer-substring beg (point)))
216 (defun ietf-drums-unfold-fws ()
217 "Unfold folding white space in the current buffer."
218 (goto-char (point-min))
219 (while (re-search-forward ietf-drums-fws-regexp nil t)
220 (replace-match " " t t))
221 (goto-char (point-min)))
223 (defun ietf-drums-parse-date (string)
224 "Return an Emacs time spec from STRING."
225 (apply 'encode-time (parse-time-string string)))
227 (defun ietf-drums-narrow-to-header ()
228 "Narrow to the header section in the current buffer."
230 (goto-char (point-min))
231 (if (re-search-forward "^\r?$" nil 1)
234 (goto-char (point-min)))
236 (defun ietf-drums-quote-string (string)
237 "Quote string if it needs quoting to be displayed in a header."
238 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
239 (concat "\"" string "\"")
242 (provide 'ietf-drums)
244 ;;; ietf-drums.el ends here