(rfc822/analyze-quoted-string): New alias for
[elisp/mu-cite.git] / tl-822.el
1 ;;; tl-822.el --- RFC 822 parser for GNU Emacs
2
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
4
5 ;; Author:   MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, RFC 822
7
8 ;; This file is part of tl (Tiny Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with This program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (require 'tl-seq)
28 (require 'tl-str)
29 (require 'std11)
30
31
32 (defconst rfc822/RCS-ID
33   "$Id: tl-822.el,v 7.51 1996-08-28 17:25:39 morioka Exp $")
34 (defconst rfc822/version (get-version-string rfc822/RCS-ID))
35
36
37 ;;; @ header
38 ;;;
39
40 (defalias 'rfc822/narrow-to-header      'std11-narrow-to-header)
41 (defalias 'rfc822/get-header-string     'std11-header-string)
42 (defalias 'rfc822/get-header-string-except 'std11-header-string-except)
43 (defalias 'rfc822/get-field-names       'std11-collect-field-names)
44
45
46 ;;; @ field
47 ;;;
48
49 (defalias `rfc822/field-end             'std11-field-end)
50 (defalias 'rfc822/get-field-body        'std11-find-field-body)
51 (defalias 'rfc822/get-field-bodies      'std11-find-field-bodies)
52
53
54 ;;; @ quoting
55 ;;;
56
57 (defconst rfc822/linear-white-space-regexp "\\(\n?[ \t]\\)+")
58 (defconst rfc822/quoted-pair-regexp "\\\\.")
59 (defconst rfc822/non-qtext-char-list '(?\" ?\\ ?\r ?\n))
60 (defconst rfc822/qtext-regexp
61   (concat "[^" (char-list-to-string rfc822/non-qtext-char-list) "]"))
62 (defconst rfc822/quoted-string-regexp
63   (concat "\""
64           (regexp-*
65            (regexp-or rfc822/qtext-regexp rfc822/quoted-pair-regexp)
66            )
67           "\""))
68
69 (defun rfc822/wrap-as-quoted-string (str)
70   "Wrap string STR as RFC 822 quoted-string. [tl-822.el]"
71   (concat "\""
72           (mapconcat (function
73                       (lambda (chr)
74                         (if (memq chr rfc822/non-qtext-char-list)
75                             (concat "\\" (char-to-string chr))
76                           (char-to-string chr)
77                           )
78                         )) str "")
79           "\""))
80
81 (defun rfc822/strip-quoted-pair (str)
82   (let ((dest "")
83         (i 0)
84         (len (length str))
85         chr flag)
86     (while (< i len)
87       (setq chr (elt str i))
88       (if (or flag (not (eq chr ?\\)))
89           (progn
90             (setq dest (concat dest (char-to-string chr)))
91             (setq flag nil)
92             )
93         (setq flag t)
94         )
95       (setq i (+ i 1))
96       )
97     dest))
98
99 (defun rfc822/strip-quoted-string (str)
100   (rfc822/strip-quoted-pair
101    (let ((max (- (length str) 1))
102          )
103      (if (and (eq (elt str 0) ?\")
104               (eq (elt str max) ?\")
105               )
106          (substring str 1 max)
107        str)
108      )))
109
110
111 ;;; @ unfolding
112 ;;;
113
114 (defalias 'rfc822/unfolding-string 'std11-unfold-string)
115
116
117 ;;; @ lexical analyze
118 ;;;
119
120 (defconst rfc822/non-dtext-chars "][")
121 (defconst rfc822/non-ctext-chars "()")
122
123 (defalias 'rfc822/analyze-spaces        'std11-analyze-spaces)
124 (defalias 'rfc822/analyze-special       'std11-analyze-special)
125 (defalias 'rfc822/analyze-atom          'std11-analyze-atom)
126 (defalias 'rfc822/analyze-quoted-string 'std11-analyze-quoted-string)
127
128 (defun rfc822/analyze-domain-literal (str)
129   (if (and (> (length str) 0)
130            (eq (aref str 0) ?\[)
131            )
132       (let* ((i (string-match (concat "[" rfc822/non-dtext-chars "]") str 1))
133              (rest (and i (substring str i)))
134              )
135         (if (and i
136                  (> (length rest) 0)
137                  (eq (aref rest 0) ?\])
138                  )
139             (cons (cons 'domain-literal (substring str 1 i))
140                   (substring rest 1)
141                   )
142           ))))
143
144 (defun rfc822/analyze-comment (str)
145   (if (and (> (length str) 0)
146            (eq (elt str 0) ?\()
147            )
148       (let ((dest "")
149             p ret)
150         (setq str (substring str 1))
151         (catch 'tag
152           (while (not (string-equal str ""))
153             (setq p (string-match (concat "[" rfc822/non-ctext-chars "]") str))
154             (cond ((> p 0)
155                    (setq dest (concat dest (substring str 0 p)))
156                    (setq str (substring str p))
157                    )
158                   ((setq ret (rfc822/analyze-comment str))
159                    (setq dest (concat dest "(" (cdr (car ret)) ")"))
160                    (setq str (cdr ret))
161                    )
162                   (t (throw 'tag nil))
163                   )
164             ))
165         (if (and (> (length str) 0)
166                  (eq (elt str 0) ?\))
167                  )
168             (cons (cons 'comment dest)
169                   (substring str 1)
170                   )
171           ))))
172
173 (defun rfc822/lexical-analyze (str)
174   (let (dest ret)
175     (while (not (string-equal str ""))
176       (setq ret
177             (or (rfc822/analyze-quoted-string str)
178                 (rfc822/analyze-domain-literal str)
179                 (rfc822/analyze-comment str)
180                 (rfc822/analyze-spaces str)
181                 (rfc822/analyze-special str)
182                 (rfc822/analyze-atom str)
183                 '((error) . "")
184                 ))
185       (setq dest (cons (car ret) dest))
186       (setq str (cdr ret))
187       )
188     (nreverse dest)
189     ))
190
191
192 ;;; @ parser
193 ;;;
194
195 (defun rfc822/ignored-token-p (token)
196   (let ((type (car token)))
197     (or (eq type 'spaces)(eq type 'comment))
198     ))
199
200 (defun rfc822/parse-token (lal)
201   (let (token itl)
202     (while (and lal
203                 (progn
204                   (setq token (car lal))
205                   (rfc822/ignored-token-p token)
206                   ))
207       (setq lal (cdr lal))
208       (setq itl (cons token itl))
209       )
210     (cons (nreverse (cons token itl))
211           (cdr lal))
212     ))
213
214 (defun rfc822/parse-ascii-token (lal)
215   (let (token itl parsed token-value)
216     (while (and lal
217                 (setq token (car lal))
218                 (if (and (setq token-value (cdr token))
219                          (find-charset-string token-value)
220                          )
221                     (setq token nil)
222                   (rfc822/ignored-token-p token)
223                   ))
224       (setq lal (cdr lal))
225       (setq itl (cons token itl))
226       )
227     (if (and token
228              (setq parsed (nreverse (cons token itl)))
229              )
230         (cons parsed (cdr lal))
231       )))
232
233 (defun rfc822/parse-token-or-comment (lal)
234   (let (token itl)
235     (while (and lal
236                 (progn
237                   (setq token (car lal))
238                   (eq (car token) 'spaces)
239                   ))
240       (setq lal (cdr lal))
241       (setq itl (cons token itl))
242       )
243     (cons (nreverse (cons token itl))
244           (cdr lal))
245     ))
246
247 (defun rfc822/parse-word (lal)
248   (let ((ret (rfc822/parse-ascii-token lal)))
249     (if ret
250         (let ((elt (car ret))
251               (rest (cdr ret))
252               )
253           (if (or (assq 'atom elt)
254                   (assq 'quoted-string elt))
255               (cons (cons 'word elt) rest)
256             )))))
257
258 (defun rfc822/parse-word-or-comment (lal)
259   (let ((ret (rfc822/parse-token-or-comment lal)))
260     (if ret
261         (let ((elt (car ret))
262               (rest (cdr ret))
263               )
264           (cond ((or (assq 'atom elt)
265                      (assq 'quoted-string elt))
266                  (cons (cons 'word elt) rest)
267                  )
268                 ((assq 'comment elt)
269                  (cons (cons 'comment-word elt) rest)
270                  ))
271           ))))
272
273 (defun rfc822/parse-phrase (lal)
274   (let (ret phrase)
275     (while (setq ret (rfc822/parse-word-or-comment lal))
276       (setq phrase (append phrase (cdr (car ret))))
277       (setq lal (cdr ret))
278       )
279     (if phrase
280         (cons (cons 'phrase phrase) lal)
281       )))
282
283 (defun rfc822/parse-local-part (lal)
284   (let ((ret (rfc822/parse-word lal)))
285     (if ret
286         (let ((local-part (cdr (car ret))) dot)
287           (setq lal (cdr ret))
288           (while (and (setq ret (rfc822/parse-ascii-token lal))
289                       (setq dot (car ret))
290                       (string-equal (cdr (assq 'specials dot)) ".")
291                       (setq ret (rfc822/parse-word (cdr ret)))
292                       (setq local-part
293                             (append local-part dot (cdr (car ret)))
294                             )
295                       (setq lal (cdr ret))
296                       ))
297           (cons (cons 'local-part local-part) lal)
298           ))))
299
300 (defun rfc822/parse-sub-domain (lal)
301   (let ((ret (rfc822/parse-ascii-token lal)))
302     (if ret
303         (let ((sub-domain (car ret)))
304           (if (or (assq 'atom sub-domain)
305                   (assq 'domain-literal sub-domain)
306                   )
307               (cons (cons 'sub-domain sub-domain)
308                     (cdr ret)
309                     )
310             )))))
311
312 (defun rfc822/parse-domain (lal)
313   (let ((ret (rfc822/parse-sub-domain lal)))
314     (if ret
315         (let ((domain (cdr (car ret))) dot)
316           (setq lal (cdr ret))
317           (while (and (setq ret (rfc822/parse-ascii-token lal))
318                       (setq dot (car ret))
319                       (string-equal (cdr (assq 'specials dot)) ".")
320                       (setq ret (rfc822/parse-sub-domain (cdr ret)))
321                       (setq domain
322                             (append domain dot (cdr (car ret)))
323                             )
324                       (setq lal (cdr ret))
325                       ))
326           (cons (cons 'domain domain) lal)
327           ))))
328
329 (defun rfc822/parse-at-domain (lal)
330   (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
331     (if (and ret
332              (setq at-sign (car ret))
333              (string-equal (cdr (assq 'specials at-sign)) "@")
334              (setq ret (rfc822/parse-domain (cdr ret)))
335              )
336         (cons (cons 'at-domain (append at-sign (cdr (car ret))))
337               (cdr ret))
338       )))
339
340 (defun rfc822/parse-addr-spec (lal)
341   (let ((ret (rfc822/parse-local-part lal))
342         addr)
343     (if (and ret
344              (prog1
345                  (setq addr (cdr (car ret)))
346                (setq lal (cdr ret))
347                (and (setq ret (rfc822/parse-at-domain lal))
348                     (setq addr (append addr (cdr (car ret))))
349                     (setq lal (cdr ret))
350                     )))
351         (cons (cons 'addr-spec addr) lal)
352       )))
353
354 (defun rfc822/parse-route (lal)
355   (let ((ret (rfc822/parse-at-domain lal))
356         route comma colon)
357     (if (and ret
358              (progn
359                (setq route (cdr (car ret)))
360                (setq lal (cdr ret))
361                (while (and (setq ret (rfc822/parse-ascii-token lal))
362                            (setq comma (car ret))
363                            (string-equal (cdr (assq 'specials comma)) ",")
364                            (setq ret (rfc822/parse-at-domain (cdr ret)))
365                            )
366                  (setq route (append route comma (cdr (car ret))))
367                  (setq lal (cdr ret))
368                  )
369                (and (setq ret (rfc822/parse-ascii-token lal))
370                     (setq colon (car ret))
371                     (string-equal (cdr (assq 'specials colon)) ":")
372                     (setq route (append route colon))
373                     )
374                ))
375         (cons (cons 'route route)
376               (cdr ret)
377               )
378       )))
379
380 (defun rfc822/parse-route-addr (lal)
381   (let ((ret (rfc822/parse-ascii-token lal))
382         < route addr-spec >)
383     (if (and ret
384              (setq < (car ret))
385              (string-equal (cdr (assq 'specials <)) "<")
386              (setq lal (cdr ret))
387              (progn (and (setq ret (rfc822/parse-route lal))
388                          (setq route (cdr (car ret)))
389                          (setq lal (cdr ret))
390                          )
391                     (setq ret (rfc822/parse-addr-spec lal))
392                     )
393              (setq addr-spec (cdr (car ret)))
394              (setq lal (cdr ret))
395              (setq ret (rfc822/parse-ascii-token lal))
396              (setq > (car ret))
397              (string-equal (cdr (assq 'specials >)) ">")
398              )
399         (cons (cons 'route-addr (append route addr-spec))
400               (cdr ret)
401               )
402       )))
403
404 (defun rfc822/parse-phrase-route-addr (lal)
405   (let ((ret (rfc822/parse-phrase lal)) phrase)
406     (if ret
407         (progn
408           (setq phrase (cdr (car ret)))
409           (setq lal (cdr ret))
410           ))
411     (if (setq ret (rfc822/parse-route-addr lal))
412         (cons (list 'phrase-route-addr
413                     phrase
414                     (cdr (car ret)))
415               (cdr ret))
416       )))
417
418 (defun rfc822/parse-mailbox (lal)
419   (let ((ret (or (rfc822/parse-phrase-route-addr lal)
420                  (rfc822/parse-addr-spec lal)))
421         mbox comment)
422     (if (and ret
423              (prog1
424                  (setq mbox (car ret))
425                (setq lal (cdr ret))
426                (if (and (setq ret (rfc822/parse-token-or-comment lal))
427                         (setq comment (cdr (assq 'comment (car ret))))
428                         )
429                    (setq lal (cdr ret))
430                  )))
431         (cons (list 'mailbox mbox comment)
432               lal)
433       )))
434
435 (defun rfc822/parse-group (lal)
436   (let ((ret (rfc822/parse-phrase lal))
437         phrase colon comma mbox semicolon)
438     (if (and ret
439              (setq phrase (cdr (car ret)))
440              (setq lal (cdr ret))
441              (setq ret (rfc822/parse-ascii-token lal))
442              (setq colon (car ret))
443              (string-equal (cdr (assq 'specials colon)) ":")
444              (setq lal (cdr ret))
445              (progn
446                (and (setq ret (rfc822/parse-mailbox lal))
447                     (setq mbox (list (car ret)))
448                     (setq lal (cdr ret))
449                     (progn
450                       (while (and (setq ret (rfc822/parse-ascii-token lal))
451                                   (setq comma (car ret))
452                                   (string-equal
453                                    (cdr (assq 'specials comma)) ",")
454                                   (setq lal (cdr ret))
455                                   (setq ret (rfc822/parse-mailbox lal))
456                                   (setq mbox (cons (car ret) mbox))
457                                   (setq lal (cdr ret))
458                                   )
459                         )))
460                (and (setq ret (rfc822/parse-ascii-token lal))
461                     (setq semicolon (car ret))
462                     (string-equal (cdr (assq 'specials semicolon)) ";")
463                     )))
464         (cons (list 'group phrase (nreverse mbox))
465               (cdr ret)
466               )
467       )))
468
469 (defun rfc822/parse-address (lal)
470   (or (rfc822/parse-group lal)
471       (rfc822/parse-mailbox lal)
472       ))
473
474 (defun rfc822/parse-addresses (lal)
475   (let ((ret (rfc822/parse-address lal)))
476     (if ret
477         (let ((dest (list (car ret))))
478           (setq lal (cdr ret))
479           (while (and (setq ret (rfc822/parse-ascii-token lal))
480                       (string-equal (cdr (assq 'specials (car ret))) ",")
481                       (setq ret (rfc822/parse-address (cdr ret)))
482                       )
483             (setq dest (cons (car ret) dest))
484             (setq lal (cdr ret))
485             )
486           (nreverse dest)
487           ))))
488
489 (defun rfc822/addr-to-string (seq)
490   (mapconcat (function
491               (lambda (token)
492                 (if (eq (car token) 'spaces)
493                     ""
494                   (cdr token)
495                   )))
496              seq "")
497   )
498
499 (defun rfc822/address-string (address)
500   (cond ((eq (car address) 'group)
501          (mapconcat (function rfc822/address-string)
502                     (nth 2 address)
503                     ", ")
504          )
505         ((eq (car address) 'mailbox)
506          (let ((addr (nth 1 address)))
507            (rfc822/addr-to-string
508             (if (eq (car addr) 'phrase-route-addr)
509                 (nth 2 addr)
510               (cdr addr)
511               )
512             )))))
513
514 (defun rfc822/full-name-string (address)
515   (cond ((eq (car address) 'group)
516          (mapconcat (function
517                      (lambda (token)
518                        (cdr token)
519                        ))
520                     (nth 1 address) "")
521          )
522         ((eq (car address) 'mailbox)
523          (let ((addr (nth 1 address))
524                (comment (nth 2 address))
525                phrase)
526            (if (eq (car addr) 'phrase-route-addr)
527                (setq phrase (mapconcat (function
528                                         (lambda (token)
529                                           (cdr token)
530                                           ))
531                                        (nth 1 addr) ""))
532              )
533            (or phrase comment)
534            ))))
535
536 (defun rfc822/extract-address-components (str)
537   "Extract full name and canonical address from STR.
538 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
539 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
540   (let* ((structure (car
541                      (rfc822/parse-address
542                       (rfc822/lexical-analyze str)
543                       )))
544          (phrase  (rfc822/full-name-string structure))
545          (address (rfc822/address-string structure))
546          )
547     (list phrase address)
548     ))
549
550
551 ;;; @ end
552 ;;;
553
554 (provide 'tl-822)
555
556 ;;; tl-822.el ends here