*** empty log message ***
[emacs.git] / lisp / gnus / ietf-drums.el
blob1dc3cc7691f7ce18296067bf2a83df10d5e1ab4a
1 ;;; ietf-drums.el --- functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998, 1999, 2000, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
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.
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.
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.
23 ;;; Commentary:
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.
30 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (require 'time-date)
34 (require 'mm-util)
36 (defvar ietf-drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
37 "US-ASCII control characters excluding CR, LF and white space.")
38 (defvar ietf-drums-text-token "\001-\011\013\014\016-\177"
39 "US-ASCII characters excluding CR and LF.")
40 (defvar ietf-drums-specials-token "()<>[]:;@\\,.\""
41 "Special characters.")
42 (defvar ietf-drums-quote-token "\\"
43 "Quote character.")
44 (defvar ietf-drums-wsp-token " \t"
45 "White space.")
46 (defvar ietf-drums-fws-regexp
47 (concat "[" ietf-drums-wsp-token "]*\n[" ietf-drums-wsp-token "]+")
48 "Folding white space.")
49 (defvar ietf-drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
50 "Textual token.")
51 (defvar ietf-drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
52 "Textual token including full stop.")
53 (defvar ietf-drums-qtext-token
54 (concat ietf-drums-no-ws-ctl-token "\041\043-\133\135-\177")
55 "Non-white-space control characters, plus the rest of ASCII excluding
56 backslash and doublequote.")
57 (defvar ietf-drums-tspecials "][()<>@,;:\\\"/?="
58 "Tspecials.")
60 (defvar ietf-drums-syntax-table
61 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
62 (modify-syntax-entry ?\\ "/" table)
63 (modify-syntax-entry ?< "(" table)
64 (modify-syntax-entry ?> ")" table)
65 (modify-syntax-entry ?@ "w" table)
66 (modify-syntax-entry ?/ "w" table)
67 (modify-syntax-entry ?= " " table)
68 (modify-syntax-entry ?* " " table)
69 (modify-syntax-entry ?\; " " table)
70 (modify-syntax-entry ?\' " " table)
71 table))
73 (defun ietf-drums-token-to-list (token)
74 "Translate TOKEN into a list of characters."
75 (let ((i 0)
76 b e c out range)
77 (while (< i (length token))
78 (setq c (mm-char-int (aref token i)))
79 (incf i)
80 (cond
81 ((eq c (mm-char-int ?-))
82 (if b
83 (setq range t)
84 (push c out)))
85 (range
86 (while (<= b c)
87 (push (mm-make-char 'ascii b) out)
88 (incf b))
89 (setq range nil))
90 ((= i (length token))
91 (push (mm-make-char 'ascii c) out))
93 (when b
94 (push (mm-make-char 'ascii b) out))
95 (setq b c))))
96 (nreverse out)))
98 (defsubst ietf-drums-init (string)
99 (set-syntax-table ietf-drums-syntax-table)
100 (insert string)
101 (ietf-drums-unfold-fws)
102 (goto-char (point-min)))
104 (defun ietf-drums-remove-comments (string)
105 "Remove comments from STRING."
106 (with-temp-buffer
107 (let (c)
108 (ietf-drums-init string)
109 (while (not (eobp))
110 (setq c (char-after))
111 (cond
112 ((eq c ?\")
113 (forward-sexp 1))
114 ((eq c ?\()
115 (delete-region (point) (progn (forward-sexp 1) (point))))
117 (forward-char 1))))
118 (buffer-string))))
120 (defun ietf-drums-remove-whitespace (string)
121 "Remove whitespace from STRING."
122 (with-temp-buffer
123 (ietf-drums-init string)
124 (let (c)
125 (while (not (eobp))
126 (setq c (char-after))
127 (cond
128 ((eq c ?\")
129 (forward-sexp 1))
130 ((eq c ?\()
131 (forward-sexp 1))
132 ((memq c '(?\ ?\t ?\n))
133 (delete-char 1))
135 (forward-char 1))))
136 (buffer-string))))
138 (defun ietf-drums-get-comment (string)
139 "Return the first comment in STRING."
140 (with-temp-buffer
141 (ietf-drums-init string)
142 (let (result c)
143 (while (not (eobp))
144 (setq c (char-after))
145 (cond
146 ((eq c ?\")
147 (forward-sexp 1))
148 ((eq c ?\()
149 (setq result
150 (buffer-substring
151 (1+ (point))
152 (progn (forward-sexp 1) (1- (point))))))
154 (forward-char 1))))
155 result)))
157 (defun ietf-drums-strip (string)
158 "Remove comments and whitespace from STRING."
159 (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
161 (defun ietf-drums-parse-address (string)
162 "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
163 (with-temp-buffer
164 (let (display-name mailbox c display-string)
165 (ietf-drums-init string)
166 (while (not (eobp))
167 (setq c (char-after))
168 (cond
169 ((or (eq c ? )
170 (eq c ?\t))
171 (forward-char 1))
172 ((eq c ?\()
173 (forward-sexp 1))
174 ((eq c ?\")
175 (push (buffer-substring
176 (1+ (point)) (progn (forward-sexp 1) (1- (point))))
177 display-name))
178 ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
179 (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
180 display-name))
181 ((eq c ?<)
182 (setq mailbox
183 (ietf-drums-remove-whitespace
184 (ietf-drums-remove-comments
185 (buffer-substring
186 (1+ (point))
187 (progn (forward-sexp 1) (1- (point))))))))
188 (t (error "Unknown symbol: %c" c))))
189 ;; If we found no display-name, then we look for comments.
190 (if display-name
191 (setq display-string
192 (mapconcat 'identity (reverse display-name) " "))
193 (setq display-string (ietf-drums-get-comment string)))
194 (if (not mailbox)
195 (when (string-match "@" display-string)
196 (cons
197 (mapconcat 'identity (nreverse display-name) "")
198 (ietf-drums-get-comment string)))
199 (cons mailbox display-string)))))
201 (defun ietf-drums-parse-addresses (string)
202 "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
203 (with-temp-buffer
204 (ietf-drums-init string)
205 (let ((beg (point))
206 pairs c)
207 (while (not (eobp))
208 (setq c (char-after))
209 (cond
210 ((memq c '(?\" ?< ?\())
211 (forward-sexp 1))
212 ((eq c ?,)
213 (push (ietf-drums-parse-address (buffer-substring beg (point)))
214 pairs)
215 (forward-char 1)
216 (setq beg (point)))
218 (forward-char 1))))
219 (push (ietf-drums-parse-address (buffer-substring beg (point)))
220 pairs)
221 (nreverse pairs))))
223 (defun ietf-drums-unfold-fws ()
224 "Unfold folding white space in the current buffer."
225 (goto-char (point-min))
226 (while (re-search-forward ietf-drums-fws-regexp nil t)
227 (replace-match " " t t))
228 (goto-char (point-min)))
230 (defun ietf-drums-parse-date (string)
231 "Return an Emacs time spec from STRING."
232 (apply 'encode-time (parse-time-string string)))
234 (defun ietf-drums-narrow-to-header ()
235 "Narrow to the header section in the current buffer."
236 (narrow-to-region
237 (goto-char (point-min))
238 (if (re-search-forward "^\r?$" nil 1)
239 (match-beginning 0)
240 (point-max)))
241 (goto-char (point-min)))
243 (defun ietf-drums-quote-string (string)
244 "Quote string if it needs quoting to be displayed in a header."
245 (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
246 (concat "\"" string "\"")
247 string))
249 (provide 'ietf-drums)
251 ;;; ietf-drums.el ends here