(rfc822/parse-ascii-token): applied Shuhei KOBAYASHI
[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.5 1996-03-25 10:17:34 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 (cdr (car lal))
352                 (if (find-charset-string (cdr (setq token (car lal))))
353                     (setq token nil)
354                   (rfc822/ignored-token-p token)
355                   ))
356       (setq lal (cdr lal))
357       (setq itl (cons token itl))
358       )
359     (if (and token
360              (setq parsed (reverse (cons token itl)))
361              )
362         (cons parsed (cdr lal))
363       )))
364
365 (defun rfc822/parse-token-or-comment (lal)
366   (let (token itl)
367     (while (and lal
368                 (progn
369                   (setq token (car lal))
370                   (eq (car token) 'spaces)
371                   ))
372       (setq lal (cdr lal))
373       (setq itl (cons token itl))
374       )
375     (cons (reverse (cons token itl))
376           (cdr lal))
377     ))
378
379 (defun rfc822/parse-word (lal)
380   (let ((ret (rfc822/parse-ascii-token lal)))
381     (if ret
382         (let ((elt (car ret))
383               (rest (cdr ret))
384               )
385           (if (or (assq 'atom elt)
386                   (assq 'quoted-string elt))
387               (cons (cons 'word elt) rest)
388             )))))
389
390 (defun rfc822/parse-word-or-comment (lal)
391   (let ((ret (rfc822/parse-token-or-comment lal)))
392     (if ret
393         (let ((elt (car ret))
394               (rest (cdr ret))
395               )
396           (cond ((or (assq 'atom elt)
397                      (assq 'quoted-string elt))
398                  (cons (cons 'word elt) rest)
399                  )
400                 ((assq 'comment elt)
401                  (cons (cons 'comment-word elt) rest)
402                  ))
403           ))))
404
405 (defun rfc822/parse-phrase (lal)
406   (let (ret phrase)
407     (while (setq ret (rfc822/parse-word-or-comment lal))
408       (setq phrase (append phrase (cdr (car ret))))
409       (setq lal (cdr ret))
410       )
411     (if phrase
412         (cons (cons 'phrase phrase) lal)
413       )))
414
415 (defun rfc822/parse-local-part (lal)
416   (let ((ret (rfc822/parse-word lal)))
417     (if ret
418         (let ((local-part (cdr (car ret))) dot)
419           (setq lal (cdr ret))
420           (while (and (setq ret (rfc822/parse-ascii-token lal))
421                       (setq dot (car ret))
422                       (string-equal (cdr (assq 'specials dot)) ".")
423                       (setq ret (rfc822/parse-word (cdr ret)))
424                       (setq local-part
425                             (append local-part dot (cdr (car ret)))
426                             )
427                       (setq lal (cdr ret))
428                       ))
429           (cons (cons 'local-part local-part) lal)
430           ))))
431
432 (defun rfc822/parse-sub-domain (lal)
433   (let ((ret (rfc822/parse-ascii-token lal)))
434     (if ret
435         (let ((sub-domain (car ret)))
436           (if (or (assq 'atom sub-domain)
437                   (assq 'domain-literal sub-domain)
438                   )
439               (cons (cons 'sub-domain sub-domain)
440                     (cdr ret)
441                     )
442             )))))
443
444 (defun rfc822/parse-domain (lal)
445   (let ((ret (rfc822/parse-sub-domain lal)))
446     (if ret
447         (let ((domain (cdr (car ret))) dot)
448           (setq lal (cdr ret))
449           (while (and (setq ret (rfc822/parse-ascii-token lal))
450                       (setq dot (car ret))
451                       (string-equal (cdr (assq 'specials dot)) ".")
452                       (setq ret (rfc822/parse-sub-domain (cdr ret)))
453                       (setq domain
454                             (append domain dot (cdr (car ret)))
455                             )
456                       (setq lal (cdr ret))
457                       ))
458           (cons (cons 'domain domain) lal)
459           ))))
460
461 (defun rfc822/parse-at-domain (lal)
462   (let ((ret (rfc822/parse-ascii-token lal)) at-sign)
463     (if (and ret
464              (setq at-sign (car ret))
465              (string-equal (cdr (assq 'specials at-sign)) "@")
466              (setq ret (rfc822/parse-domain (cdr ret)))
467              )
468         (cons (cons 'at-domain (append at-sign (cdr (car ret))))
469               (cdr ret))
470       )))
471
472 (defun rfc822/parse-addr-spec (lal)
473   (let ((ret (rfc822/parse-local-part lal))
474         addr at-sign)
475     (if (and ret
476              (prog1
477                  (setq addr (cdr (car ret)))
478                (setq lal (cdr ret))
479                (and (setq ret (rfc822/parse-at-domain lal))
480                     (setq addr (append addr (cdr (car ret))))
481                     (setq lal (cdr ret))
482                     )))
483         (cons (cons 'addr-spec addr) lal)
484       )))
485
486 (defun rfc822/parse-route (lal)
487   (let ((ret (rfc822/parse-at-domain lal))
488         route comma colon)
489     (if (and ret
490              (progn
491                (setq route (cdr (car ret)))
492                (setq lal (cdr ret))
493                (while (and (setq ret (rfc822/parse-ascii-token lal))
494                            (setq comma (car ret))
495                            (string-equal (cdr (assq 'specials comma)) ",")
496                            (setq ret (rfc822/parse-at-domain (cdr ret)))
497                            )
498                  (setq route (append route comma (cdr (car ret))))
499                  (setq lal (cdr ret))
500                  )
501                (and (setq ret (rfc822/parse-ascii-token lal))
502                     (setq colon (car ret))
503                     (string-equal (cdr (assq 'specials colon)) ":")
504                     (setq route (append route colon))
505                     )
506                ))
507         (cons (cons 'route route)
508               (cdr ret)
509               )
510       )))
511
512 (defun rfc822/parse-route-addr (lal)
513   (let ((ret (rfc822/parse-ascii-token lal))
514         < route addr-spec >)
515     (if (and ret
516              (setq < (car ret))
517              (string-equal (cdr (assq 'specials <)) "<")
518              (setq lal (cdr ret))
519              (progn (and (setq ret (rfc822/parse-route lal))
520                          (setq route (cdr (car ret)))
521                          (setq lal (cdr ret))
522                          )
523                     (setq ret (rfc822/parse-addr-spec lal))
524                     )
525              (setq addr-spec (cdr (car ret)))
526              (setq lal (cdr ret))
527              (setq ret (rfc822/parse-ascii-token lal))
528              (setq > (car ret))
529              (string-equal (cdr (assq 'specials >)) ">")
530              )
531         (cons (cons 'route-addr (append route addr-spec))
532               (cdr ret)
533               )
534       )))
535
536 (defun rfc822/parse-phrase-route-addr (lal)
537   (let ((ret (rfc822/parse-phrase lal)) phrase)
538     (if ret
539         (progn
540           (setq phrase (cdr (car ret)))
541           (setq lal (cdr ret))
542           ))
543     (if (setq ret (rfc822/parse-route-addr lal))
544         (cons (list 'phrase-route-addr
545                     phrase
546                     (cdr (car ret)))
547               (cdr ret))
548       )))
549
550 (defun rfc822/parse-mailbox (lal)
551   (let ((ret (or (rfc822/parse-phrase-route-addr lal)
552                  (rfc822/parse-addr-spec lal)))
553         mbox comment)
554     (if (and ret
555              (prog1
556                  (setq mbox (car ret))
557                (setq lal (cdr ret))
558                (if (and (setq ret (rfc822/parse-token-or-comment lal))
559                         (setq comment (cdr (assq 'comment (car ret))))
560                         )
561                    (setq lal (cdr ret))
562                  )))
563         (cons (list 'mailbox mbox comment)
564               lal)
565       )))
566
567 (defun rfc822/parse-group (lal)
568   (let ((ret (rfc822/parse-phrase lal))
569         phrase colon comma mbox semicolon)
570     (if (and ret
571              (setq phrase (cdr (car ret)))
572              (setq lal (cdr ret))
573              (setq ret (rfc822/parse-ascii-token lal))
574              (setq colon (car ret))
575              (string-equal (cdr (assq 'specials colon)) ":")
576              (setq lal (cdr ret))
577              (progn
578                (and (setq ret (rfc822/parse-mailbox lal))
579                     (setq mbox (list (car ret)))
580                     (setq lal (cdr ret))
581                     (progn
582                       (while (and (setq ret (rfc822/parse-ascii-token lal))
583                                   (setq comma (car ret))
584                                   (string-equal
585                                    (cdr (assq 'specials comma)) ",")
586                                   (setq lal (cdr ret))
587                                   (setq ret (rfc822/parse-mailbox lal))
588                                   (setq mbox (cons (car ret) mbox))
589                                   (setq lal (cdr ret))
590                                   )
591                         )))
592                (and (setq ret (rfc822/parse-ascii-token lal))
593                     (setq semicolon (car ret))
594                     (string-equal (cdr (assq 'specials semicolon)) ";")
595                     )))
596         (cons (list 'group phrase (reverse mbox))
597               (cdr ret)
598               )
599       )))
600
601 (defun rfc822/parse-address (lal)
602   (or (rfc822/parse-group lal)
603       (rfc822/parse-mailbox lal)
604       ))
605
606 (defun rfc822/parse-addresses (lal)
607   (let ((ret (rfc822/parse-address lal)))
608     (if ret
609         (let ((dest (list (car ret))))
610           (setq lal (cdr ret))
611           (while (and (setq ret (rfc822/parse-ascii-token lal))
612                       (string-equal (cdr (assq 'specials (car ret))) ",")
613                       (setq ret (rfc822/parse-address (cdr ret)))
614                       )
615             (setq dest (cons (car ret) dest))
616             (setq lal (cdr ret))
617             )
618           (reverse dest)
619           ))))
620
621 (defun rfc822/addr-to-string (seq)
622   (mapconcat (function
623               (lambda (token)
624                 (if (eq (car token) 'spaces)
625                     ""
626                   (cdr token)
627                   )))
628              seq "")
629   )
630
631 (defun rfc822/address-string (address)
632   (if (eq (car address) 'mailbox)
633       (let ((addr (nth 1 address))
634             addr-spec)
635         (rfc822/addr-to-string
636          (if (eq (car addr) 'phrase-route-addr)
637              (nth 2 addr)
638            (cdr addr)
639            )
640          ))))
641
642 (defun rfc822/full-name-string (address)
643   (if (eq (car address) 'mailbox)
644       (let ((addr (nth 1 address))
645             (comment (nth 2 address))
646             phrase)
647         (if (eq (car addr) 'phrase-route-addr)
648             (setq phrase (mapconcat (function
649                                      (lambda (token)
650                                        (cdr token)
651                                        ))
652                                     (nth 1 addr) ""))
653           )
654         (or phrase comment)
655         )))
656
657 (defun rfc822/extract-address-components (str)
658   "Extract full name and canonical address from STR.
659 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
660 If no name can be extracted, FULL-NAME will be nil. [tl-822.el]"
661   (let* ((structure (car
662                      (rfc822/parse-address
663                       (rfc822/lexical-analyze str)
664                       )))
665          (phrase  (rfc822/full-name-string structure))
666          (address (rfc822/address-string structure))
667          )
668     (list phrase address)
669     ))
670
671
672 ;;; @ end
673 ;;;
674
675 (provide 'tl-822)
676
677 ;;; tl-822.el ends here