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