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