1f45a89412391b241315489da5b96d86c7f40ab6
[elisp/gnus.git-] / lisp / drums.el
1 ;;; drums.el --- Functions for parsing RFC822bis headers
2 ;; Copyright (C) 1998 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
33 (defvar drums-no-ws-ctl-token "\001-\010\013\014\016-\037\177"
34   "US-ASCII control characters excluding CR, LF and white space.")
35 (defvar drums-text-token "\001-\011\013\014\016-\177"
36   "US-ASCII characters exlcuding CR and LF.")
37 (defvar drums-specials-token "()<>[]:;@\\,.\""
38   "Special characters.")
39 (defvar drums-quote-token "\\"
40   "Quote character.")
41 (defvar drums-wsp-token " \t"
42   "White space.")
43 (defvar drums-fws-regexp
44   (concat "[" drums-wsp-token "]*\n[" drums-wsp-token "]+")
45   "Folding white space.")
46 (defvar drums-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~"
47   "Textual token.")
48 (defvar drums-dot-atext-token "-^a-zA-Z0-9!#$%&'*+/=?_`{|}~."
49   "Textual token including full stop.")
50 (defvar drums-qtext-token
51   (concat drums-no-ws-ctl-token "\041\043-\133\135-\177")
52   "Non-white-space control characaters, plus the rest of ASCII excluding backslash and doublequote.")
53   
54 (defvar drums-syntax-table
55   (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
56     (modify-syntax-entry ?\\ "/" table)
57     (modify-syntax-entry ?< "(" table)
58     (modify-syntax-entry ?> ")" table)
59     table))
60
61 (defsubst drums-init (string)
62   (set-syntax-table drums-syntax-table)
63   (insert string)
64   (drums-unfold-fws)
65   (goto-char (point-min)))
66
67 (defun drums-remove-comments (string)
68   "Remove comments from STRING."
69   (with-temp-buffer
70     (let (c)
71       (drums-init string)
72       (while (not (eobp))
73         (setq c (following-char))
74         (cond
75          ((eq c ?\")
76           (forward-sexp 1))
77          ((eq c ?\()
78           (delete-region (point) (progn (forward-sexp 1) (point))))
79          (t
80           (forward-char 1))))
81       (buffer-string))))
82
83 (defun drums-remove-whitespace (string)
84   "Remove comments from STRING."
85   (with-temp-buffer
86     (drums-init string)
87     (let (c)
88       (while (not (eobp))
89         (setq c (following-char))
90         (cond
91          ((eq c ?\")
92           (forward-sexp 1))
93          ((memq c '(? ?\t))
94           (delete-char 1))
95          (t
96           (forward-char 1))))
97       (buffer-string))))
98
99 (defun drums-get-comment (string)
100   "Return the first comment in STRING."
101   (with-temp-buffer
102     (drums-init string)
103     (let (result c)
104       (while (not (eobp))
105         (setq c (following-char))
106         (cond
107          ((eq c ?\")
108           (forward-sexp 1))
109          ((eq c ?\()
110           (setq result
111                 (buffer-substring
112                  (1+ (point))
113                  (progn (forward-sexp 1) (1- (point)))))
114           (goto-char (point-max)))
115          (t
116           (forward-char 1))))
117       result)))
118
119 (defun drums-parse-address (string)
120   "Parse STRING and return a MAILBOX / DISPLAY-NAME pair."
121   (with-temp-buffer
122     (let (display-name mailbox c)
123       (drums-init string)
124       (while (not (eobp))
125         (setq c (following-char))
126         (cond
127          ((or (eq c ? )
128               (eq c ?\t))
129           (forward-char 1))
130          ((eq c ?\()
131           (forward-sexp 1))
132          ((eq c ?\")
133           (push (buffer-substring
134                  (1+ (point)) (progn (forward-sexp 1) (1- (point))))
135                 display-name))
136          ((looking-at (concat "[" drums-atext-token "]"))
137           (push (buffer-substring (point) (progn (forward-word 1) (point)))
138                 display-name))
139          ((eq c ?<)
140           (setq mailbox
141                 (drums-remove-whitespace
142                  (drums-remove-comments
143                   (buffer-substring
144                    (1+ (point))
145                    (progn (forward-sexp 1) (1- (point))))))))
146          (t (error "Unknown symbol: %c" c))))
147       ;; If we found no display-name, then we look for comments.
148       (if display-name
149           (setq display-name (mapconcat 'identity (nreverse display-name) " "))
150         (setq display-name (drums-get-comment string)))
151       (when mailbox
152         (cons mailbox display-name)))))
153
154 (defun drums-parse-addresses (string)
155   "Parse STRING and return a list of MAILBOX / DISPLAY-NAME pairs."
156   (with-temp-buffer
157     (drums-init string)
158     (let ((beg (point))
159           pairs c)
160       (while (not (eobp))
161         (setq c (following-char))
162         (cond
163          ((memq c '(?\" ?< ?\())
164           (forward-sexp 1))
165          ((eq c ?,)
166           (push (drums-parse-address (buffer-substring beg (1- (point))))
167                 pairs)
168           (setq beg (point)))
169          (t
170           (forward-char 1))))
171       (nreverse pairs))))
172
173 (defun drums-unfold-fws ()
174   "Unfold folding white space in the current buffer."
175   (goto-char (point-min))
176   (while (re-search-forward drums-fws-regexp nil t)
177     (replace-match " " t t))
178   (goto-char (point-min)))
179
180 (defun drums-parse-date (string)
181   "Return an Emacs time spec from STRING."
182   (encode-time (parse-time-string string)))
183     
184 (provide 'drums)
185
186 ;;; drums.el ends here