Update copyright for several files.
[gnus] / lisp / ietf-drums.el
1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
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.
12
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.
17
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.
22
23 ;;; Commentary:
24
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.
29
30 ;;; Code:
31
32 (eval-when-compile (require 'cl))
33 (require 'time-date)
34 (require 'mm-util)
35
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.")
59
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     (if (featurep 'xemacs)
72         (let ((i 128))
73           (while (< i 256)
74             (modify-syntax-entry i "w" table)
75             (setq i (1+ i)))))
76     table))
77
78 (defun ietf-drums-token-to-list (token)
79   "Translate TOKEN into a list of characters."
80   (let ((i 0)
81         b e c out range)
82     (while (< i (length token))
83       (setq c (mm-char-int (aref token i)))
84       (incf i)
85       (cond
86        ((eq c (mm-char-int ?-))
87         (if b
88             (setq range t)
89           (push c out)))
90        (range
91         (while (<= b c)
92           (push (mm-make-char 'ascii b) out)
93           (incf b))
94         (setq range nil))
95        ((= i (length token))
96         (push (mm-make-char 'ascii c) out))
97        (t
98         (when b
99           (push (mm-make-char 'ascii b) out))
100         (setq b c))))
101     (nreverse out)))
102
103 (defsubst ietf-drums-init (string)
104   (set-syntax-table ietf-drums-syntax-table)
105   (insert string)
106   (ietf-drums-unfold-fws)
107   (goto-char (point-min)))
108
109 (defun ietf-drums-remove-comments (string)
110   "Remove comments from STRING."
111   (with-temp-buffer
112     (let (c)
113       (ietf-drums-init string)
114       (while (not (eobp))
115         (setq c (char-after))
116         (cond
117          ((eq c ?\")
118           (forward-sexp 1))
119          ((eq c ?\()
120           (delete-region (point) (progn (forward-sexp 1) (point))))
121          (t
122           (forward-char 1))))
123       (buffer-string))))
124
125 (defun ietf-drums-remove-whitespace (string)
126   "Remove whitespace from STRING."
127   (with-temp-buffer
128     (ietf-drums-init string)
129     (let (c)
130       (while (not (eobp))
131         (setq c (char-after))
132         (cond
133          ((eq c ?\")
134           (forward-sexp 1))
135          ((eq c ?\()
136           (forward-sexp 1))
137          ((memq c '(? ?\t ?\n))
138           (delete-char 1))
139          (t
140           (forward-char 1))))
141       (buffer-string))))
142
143 (defun ietf-drums-get-comment (string)
144   "Return the first comment in STRING."
145   (with-temp-buffer
146     (ietf-drums-init string)
147     (let (result c)
148       (while (not (eobp))
149         (setq c (char-after))
150         (cond
151          ((eq c ?\")
152           (forward-sexp 1))
153          ((eq c ?\()
154           (setq result
155                 (buffer-substring
156                  (1+ (point))
157                  (progn (forward-sexp 1) (1- (point))))))
158          (t
159           (forward-char 1))))
160       result)))
161
162 (defun ietf-drums-strip (string)
163   "Remove comments and whitespace from STRING."
164   (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
165
166 (defun ietf-drums-parse-address (string)
167   "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
168   (with-temp-buffer
169     (let (display-name mailbox c display-string)
170       (ietf-drums-init string)
171       (while (not (eobp))
172         (setq c (char-after))
173         (cond
174          ((or (eq c ? )
175               (eq c ?\t))
176           (forward-char 1))
177          ((eq c ?\()
178           (forward-sexp 1))
179          ((eq c ?\")
180           (push (buffer-substring
181                  (1+ (point)) (progn (forward-sexp 1) (1- (point))))
182                 display-name))
183          ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
184           (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
185                 display-name))
186          ((eq c ?<)
187           (setq mailbox
188                 (ietf-drums-remove-whitespace
189                  (ietf-drums-remove-comments
190                   (buffer-substring
191                    (1+ (point))
192                    (progn (forward-sexp 1) (1- (point))))))))
193          (t (error "Unknown symbol: %c" c))))
194       ;; If we found no display-name, then we look for comments.
195       (if display-name
196           (setq display-string
197                 (mapconcat 'identity (reverse display-name) " "))
198         (setq display-string (ietf-drums-get-comment string)))
199       (if (not mailbox)
200           (when (string-match "@" display-string)
201             (cons
202              (mapconcat 'identity (nreverse display-name) "")
203              (ietf-drums-get-comment string)))
204         (cons mailbox display-string)))))
205
206 (defun ietf-drums-parse-addresses (string)
207   "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
208   (if (null string)
209       nil
210     (with-temp-buffer
211       (ietf-drums-init string)
212       (let ((beg (point))
213             pairs c address)
214         (while (not (eobp))
215           (setq c (char-after))
216           (cond
217            ((memq c '(?\" ?< ?\())
218             (condition-case nil
219                 (forward-sexp 1)
220               (error
221                (skip-chars-forward "^,"))))
222            ((eq c ?,)
223             (setq address
224                   (condition-case nil
225                       (ietf-drums-parse-address
226                        (buffer-substring beg (point)))
227                     (error nil)))
228             (if address (push address pairs))
229             (forward-char 1)
230             (setq beg (point)))
231            (t
232             (forward-char 1))))
233         (setq address
234               (condition-case nil
235                   (ietf-drums-parse-address
236                    (buffer-substring beg (point)))
237                 (error nil)))
238         (if address (push address pairs))
239         (nreverse pairs)))))
240
241 (defun ietf-drums-unfold-fws ()
242   "Unfold folding white space in the current buffer."
243   (goto-char (point-min))
244   (while (re-search-forward ietf-drums-fws-regexp nil t)
245     (replace-match " " t t))
246   (goto-char (point-min)))
247
248 (defun ietf-drums-parse-date (string)
249   "Return an Emacs time spec from STRING."
250   (apply 'encode-time (parse-time-string string)))
251
252 (defun ietf-drums-narrow-to-header ()
253   "Narrow to the header section in the current buffer."
254   (narrow-to-region
255    (goto-char (point-min))
256    (if (re-search-forward "^\r?$" nil 1)
257        (match-beginning 0)
258      (point-max)))
259   (goto-char (point-min)))
260
261 (defun ietf-drums-quote-string (string)
262   "Quote string if it needs quoting to be displayed in a header."
263   (if (string-match (concat "[^" ietf-drums-atext-token "]") string)
264       (concat "\"" string "\"")
265     string))
266
267 (provide 'ietf-drums)
268
269 ;;; ietf-drums.el ends here