c4b6254d8845c2dda1d120ac402a6d559ca23772
[gnus] / lisp / ietf-drums.el
1 ;;; ietf-drums.el --- Functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998-2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
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.
28
29 ;;; Code:
30
31 (require 'time-date)
32 (require 'mm-util)
33
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 "\\"
41   "Quote character.")
42 (defvar ietf-drums-wsp-token " \t"
43   "White space.")
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!#$%&'*+/=?_`{|}~"
48   "Textual token.")
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 "][()<>@,;:\\\"/?="
55   "Tspecials.")
56
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)
68     table))
69
70 (defun ietf-drums-token-to-list (token)
71   "Translate TOKEN into a list of characters."
72   (let ((i 0)
73         b e c out range)
74     (while (< i (length token))
75       (setq c (mm-char-int (aref token i)))
76       (incf i)
77       (cond
78        ((eq c (mm-char-int ?-))
79         (if b
80             (setq range t)
81           (push c out)))
82        (range
83         (while (<= b c)
84           (push (mm-make-char 'ascii b) out)
85           (incf b))
86         (setq range nil))
87        ((= i (length token))
88         (push (mm-make-char 'ascii c) out))
89        (t
90         (when b
91           (push (mm-make-char 'ascii b) out))
92         (setq b c))))
93     (nreverse out)))
94
95 (defsubst ietf-drums-init (string)
96   (set-syntax-table ietf-drums-syntax-table)
97   (insert string)
98   (ietf-drums-unfold-fws)
99   (goto-char (point-min)))
100
101 (defun ietf-drums-remove-comments (string)
102   "Remove comments from STRING."
103   (with-temp-buffer
104     (let (c)
105       (ietf-drums-init string)
106       (while (not (eobp))
107         (setq c (char-after))
108         (cond
109          ((eq c ?\")
110           (forward-sexp 1))
111          ((eq c ?\()
112           (delete-region (point) (progn (forward-sexp 1) (point))))
113          (t
114           (forward-char 1))))
115       (buffer-string))))
116
117 (defun ietf-drums-remove-whitespace (string)
118   "Remove whitespace from STRING."
119   (with-temp-buffer
120     (ietf-drums-init string)
121     (let (c)
122       (while (not (eobp))
123         (setq c (char-after))
124         (cond
125          ((eq c ?\")
126           (forward-sexp 1))
127          ((eq c ?\()
128           (forward-sexp 1))
129          ((memq c '(? ?\t ?\n))
130           (delete-char 1))
131          (t
132           (forward-char 1))))
133       (buffer-string))))
134
135 (defun ietf-drums-get-comment (string)
136   "Return the first comment in STRING."
137   (with-temp-buffer
138     (ietf-drums-init string)
139     (let (result c)
140       (while (not (eobp))
141         (setq c (char-after))
142         (cond
143          ((eq c ?\")
144           (forward-sexp 1))
145          ((eq c ?\()
146           (setq result
147                 (buffer-substring
148                  (1+ (point))
149                  (progn (forward-sexp 1) (1- (point))))))
150          (t
151           (forward-char 1))))
152       result)))
153
154 (defun ietf-drums-strip (string)
155   "Remove comments and whitespace from STRING."
156   (ietf-drums-remove-whitespace (ietf-drums-remove-comments string)))
157
158 (defun ietf-drums-parse-address (string)
159   "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
160   (with-temp-buffer
161     (let (display-name mailbox c display-string)
162       (ietf-drums-init string)
163       (while (not (eobp))
164         (setq c (char-after))
165         (cond
166          ((or (eq c ? )
167               (eq c ?\t))
168           (forward-char 1))
169          ((eq c ?\()
170           (forward-sexp 1))
171          ((eq c ?\")
172           (push (buffer-substring
173                  (1+ (point)) (progn (forward-sexp 1) (1- (point))))
174                 display-name))
175          ((looking-at (concat "[" ietf-drums-atext-token "@" "]"))
176           (push (buffer-substring (point) (progn (forward-sexp 1) (point)))
177                 display-name))
178          ((eq c ?<)
179           (setq mailbox
180                 (ietf-drums-remove-whitespace
181                  (ietf-drums-remove-comments
182                   (buffer-substring
183                    (1+ (point))
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.
187       (if display-name
188           (setq display-string
189                 (mapconcat 'identity (reverse display-name) " "))
190         (setq display-string (ietf-drums-get-comment string)))
191       (if (not mailbox)
192           (when (string-match "@" display-string)
193             (cons
194              (mapconcat 'identity (nreverse display-name) "")
195              (ietf-drums-get-comment string)))
196         (cons mailbox display-string)))))
197
198 (defun ietf-drums-parse-addresses (string)
199   "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
200   (with-temp-buffer
201     (ietf-drums-init string)
202     (let ((beg (point))
203           pairs c)
204       (while (not (eobp))
205         (setq c (char-after))
206         (cond
207          ((memq c '(?\" ?< ?\())
208           (forward-sexp 1))
209          ((eq c ?,)
210           (push (ietf-drums-parse-address (buffer-substring beg (point)))
211                 pairs)
212           (forward-char 1)
213           (setq beg (point)))
214          (t
215           (forward-char 1))))
216       (push (ietf-drums-parse-address (buffer-substring beg (point)))
217             pairs)
218       (nreverse pairs))))
219
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)))
226
227 (defun ietf-drums-parse-date (string)
228   "Return an Emacs time spec from STRING."
229   (apply 'encode-time (parse-time-string string)))
230
231 (defun ietf-drums-narrow-to-header ()
232   "Narrow to the header section in the current buffer."
233   (narrow-to-region
234    (goto-char (point-min))
235    (if (re-search-forward "^\r?$" nil 1)
236        (match-beginning 0)
237      (point-max)))
238   (goto-char (point-min)))
239
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 "\"")
244     string))
245
246 (provide 'ietf-drums)
247
248 ;;; ietf-drums.el ends here