f500554a422271c7f7e87d456bd71f7add4a1036
[elisp/tm.git] / tm-ew-e.el
1 ;;;
2 ;;; tm-ew-e.el --- RFC 1522 based multilingual MIME message header
3 ;;;                encoder for GNU Emacs
4 ;;;
5 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
6 ;;; Copyright (C) 1993 .. 1996 MORIOKA Tomohiko
7 ;;;
8 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Version: $Revision: 7.18 $
10 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
11 ;;;
12 ;;; This file is part of tm (Tools for MIME).
13 ;;;
14 ;;; This program is free software; you can redistribute it and/or
15 ;;; modify it under the terms of the GNU General Public License as
16 ;;; published by the Free Software Foundation; either version 2, or
17 ;;; (at your option) any later version.
18 ;;;
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 ;;; General Public License for more details.
23 ;;;
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with This program.  If not, write to the Free Software
26 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;;;
28 ;;; Code:
29
30 (require 'mel)
31 (require 'tl-822)
32 (require 'tm-def)
33
34
35 ;;; @ version
36 ;;;
37
38 (defconst tm-ew-e/RCS-ID
39   "$Id: tm-ew-e.el,v 7.18 1996/05/09 18:08:47 morioka Exp $")
40 (defconst mime-eword/encoder-version (get-version-string tm-ew-e/RCS-ID))
41
42
43 ;;; @ variables
44 ;;;
45
46 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
47
48 (defvar mime/use-X-Nsubject nil)
49
50 (defvar mime-eword/charset-encoding-alist
51   '(("US-ASCII"       . nil)
52     ("ISO-8859-1"     . "Q")
53     ("ISO-8859-2"     . "Q")
54     ("ISO-8859-3"     . "Q")
55     ("ISO-8859-4"     . "Q")
56 ;;; ("ISO-8859-5"     . "Q")
57     ("KOI8-R"         . "Q")
58     ("ISO-8859-7"     . "Q")
59     ("ISO-8859-8"     . "Q")
60     ("ISO-8859-9"     . "Q")
61     ("ISO-2022-JP"    . "B")
62     ("ISO-2022-KR"    . "B")
63     ("EUC-KR"         . "B")
64     ("ISO-2022-JP-2"  . "B")
65     ("ISO-2022-INT-1" . "B")
66     ))
67
68
69 ;;; @ encoded-text encoder
70 ;;;
71
72 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
73   (let ((text
74          (cond ((string= encoding "B")
75                 (base64-encode-string string))
76                ((string= encoding "Q")
77                 (q-encoding-encode-string string mode))
78                )
79          ))
80     (if text
81         (concat "=?" charset "?" encoding "?" text "?=")
82       )))
83
84
85 ;;; @ leading char
86 ;;;
87
88 (defun tm-eword::char-type (chr)
89   (if (or (= chr 32)(= chr ?\t))
90       nil
91     (char-leading-char chr)
92     ))
93
94 (defun tm-eword::parse-lc-word (str)
95   (let* ((chr (sref str 0))
96          (lc (tm-eword::char-type chr))
97          (i (char-bytes chr))
98          (len (length str))
99          )
100     (while (and (< i len)
101                 (setq chr (sref str i))
102                 (eq lc (tm-eword::char-type chr))
103                 )
104       (setq i (+ i (char-bytes chr)))
105       )
106     (cons (cons lc (substring str 0 i)) (substring str i))
107     ))
108
109 (defun tm-eword::split-to-lc-words (str)
110   (let (ret dest)
111     (while (and (not (string= str ""))
112                 (setq ret (tm-eword::parse-lc-word str))
113                 )
114       (setq dest (cons (car ret) dest))
115       (setq str (cdr ret))
116       )
117     (reverse dest)
118     ))
119
120
121 ;;; @ word
122 ;;;
123
124 (defun tm-eword::parse-word (lcwl)
125   (let* ((lcw (car lcwl))
126          (lc (car lcw))
127          )
128     (if (null lc)
129         lcwl
130       (let ((lcl (list lc))
131             (str (cdr lcw))
132             )
133         (catch 'tag
134           (while (setq lcwl (cdr lcwl))
135             (setq lcw (car lcwl))
136             (setq lc (car lcw))
137             (if (null lc)
138                 (throw 'tag nil)
139               )
140             (if (not (memq lc lcl))
141                 (setq lcl (cons lc lcl))
142               )
143             (setq str (concat str (cdr lcw)))
144             ))
145         (cons (cons lcl str) lcwl)
146         ))))
147
148 (defun tm-eword::lc-words-to-words (lcwl)
149   (let (ret dest)
150     (while (setq ret (tm-eword::parse-word lcwl))
151       (setq dest (cons (car ret) dest))
152       (setq lcwl (cdr ret))
153       )
154     (reverse dest)
155     ))
156
157
158 ;;; @ rule
159 ;;;
160
161 (defun tm-eword::find-charset-rule (lcl)
162   (if lcl
163       (let* ((charset (mime/find-charset lcl))
164              (encoding
165               (cdr (assoc charset mime-eword/charset-encoding-alist))
166               ))
167         (list charset encoding)
168         )))
169
170 (defun tm-eword::words-to-ruled-words (wl)
171   (mapcar (function
172            (lambda (word)
173              (cons (cdr word) (tm-eword::find-charset-rule (car word)))
174              ))
175           wl))
176
177 (defun tm-eword::space-process (seq)
178   (let (prev a ac b c cc)
179     (while seq
180       (setq b (car seq))
181       (setq seq (cdr seq))
182       (setq c (car seq))
183       (setq cc (nth 1 c))
184       (if (null (nth 1 b))
185           (progn
186             (setq a (car prev))
187             (setq ac (nth 1 a))
188             (if (and (nth 2 a)(nth 2 c))
189                 (cond ((equal ac cc)
190                        (setq prev (cons
191                                    (cons (concat (car a)(car b)(car c))
192                                          (cdr a))
193                                    (cdr prev)
194                                    ))
195                        (setq seq (cdr seq))
196                        )
197                       (t
198                        (setq prev (cons
199                                    (cons (concat (car a)(car b))
200                                          (cdr a))
201                                    (cdr prev)
202                                    ))
203                        ))
204               (setq prev (cons b prev))
205               ))
206         (setq prev (cons b prev))
207         ))
208     (reverse prev)
209     ))
210
211 (defun tm-eword::split-string (str)
212   (tm-eword::space-process
213    (tm-eword::words-to-ruled-words
214     (tm-eword::lc-words-to-words
215      (tm-eword::split-to-lc-words str)
216      ))))
217
218
219 ;;; @ length
220 ;;;
221
222 (defun base64-length (string)
223   (let ((l (length string)))
224     (* (+ (/ l 3)
225           (if (= (mod l 3) 0) 0 1)
226           ) 4)
227     ))
228
229 (defun q-encoding-length (string)
230   (let ((l 0)(i 0)(len (length string)) chr)
231     (while (< i len)
232       (setq chr (elt string i))
233       (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
234           (setq l (+ l 1))
235         (setq l (+ l 3))
236         )
237       (setq i (+ i 1)) )
238     l))
239
240 (defun tm-eword::encoded-word-length (rword)
241   (let ((charset  (nth 1 rword))
242         (encoding (nth 2 rword))
243         (string   (car rword))
244         ret)
245     (setq ret
246           (cond ((equal encoding "B")
247                  (setq string (mime-charset-encode-string string charset))
248                  (base64-length string)
249                  )
250                 ((equal encoding "Q")
251                  (setq string (mime-charset-encode-string string charset))
252                  (q-encoding-length string)
253                  )))
254     (if ret
255         (cons (+ 7 (length charset) ret) string)
256       )))
257
258
259 ;;; @ encode-string
260 ;;;
261
262 (defun tm-eword::encode-string-1 (column rwl &optional mode)
263   (let* ((rword (car rwl))
264          (ret (tm-eword::encoded-word-length rword))
265          string len)
266     (if (null ret)
267         (cond ((and (setq string (car rword))
268                     (<= (setq len (+ (length string) column)) 76)
269                     )
270                (setq rwl (cdr rwl))
271                )
272               (t
273                (setq string "\n ")
274                (setq len 1)
275                ))
276       (cond ((and (setq len (car ret))
277                   (<= (+ column len) 76)
278                   )
279              (setq string
280                    (tm-eword::encode-encoded-text
281                     (nth 1 rword) (nth 2 rword) (cdr ret)
282                     ))
283              (setq len (+ (length string) column))
284              (setq rwl (cdr rwl))
285              )
286             (t
287              (setq string (car rword))
288              (let* ((sl (length string))
289                     (p 0) np
290                     (str "") nstr)
291                (while (and (< p len)
292                            (progn
293                              (setq np (+ p (char-bytes (sref string p))))
294                              (setq nstr (substring string 0 np))
295                              (setq ret (tm-eword::encoded-word-length
296                                         (cons nstr (cdr rword))
297                                         ))
298                              (setq nstr (cdr ret))
299                              (setq len (+ (car ret) column))
300                              (<= len 76)
301                              ))
302                  (setq str nstr
303                        p np))
304                (if (string-equal str "")
305                    (setq string "\n "
306                          len 1)
307                  (setq rwl (cons (cons (substring string p) (cdr rword))
308                                  (cdr rwl)))
309                  (setq string
310                        (tm-eword::encode-encoded-text
311                         (nth 1 rword) (nth 2 rword) str))
312                  (setq len (+ (length string) column))
313                  )
314                )))
315       )
316     (list string len rwl)
317     ))
318
319 (defun tm-eword::encode-rwl (column rwl &optional mode)
320   (let (ret dest ps special str)
321     (while rwl
322       (setq ret (tm-eword::encode-string-1 column rwl mode))
323       (setq str (car ret))
324       (if (eq (elt str 0) ?\n)
325           (if (eq special ?\()
326               (progn
327                 (setq dest (concat dest "\n ("))
328                 (setq ret (tm-eword::encode-string-1 2 rwl mode))
329                 (setq str (car ret))
330                 ))
331         (cond ((eq special 32)
332                (if (string= str "(")
333                    (setq ps t)
334                  (setq dest (concat dest " "))
335                  (setq ps nil)
336                  ))
337               ((eq special ?\()
338                (if ps
339                    (progn
340                      (setq dest (concat dest " ("))
341                      (setq ps nil)
342                      )
343                  (setq dest (concat dest "("))
344                  )
345                )))
346       (cond ((string= str " ")
347              (setq special 32)
348              )
349             ((string= str "(")
350              (setq special ?\()
351              )
352             (t
353              (setq special nil)
354              (setq dest (concat dest str))
355              ))
356       (setq column (nth 1 ret)
357             rwl (nth 2 ret))
358       )
359     (list dest column)
360     ))
361
362 (defun tm-eword::encode-string (column str &optional mode)
363   (tm-eword::encode-rwl column (tm-eword::split-string str) mode)
364   )
365
366
367 ;;; @ converter
368 ;;;
369
370 (defun tm-eword::phrase-to-rwl (phrase)
371   (let (token type dest str)
372     (while phrase
373       (setq token (car phrase))
374       (setq type (car token))
375       (cond ((eq type 'quoted-string)
376              (setq str (concat "\"" (cdr token) "\""))
377              (setq dest
378                    (append dest
379                            (list
380                             (cons str (tm-eword::find-charset-rule
381                                        (find-charset-string str)))
382                             )))
383              )
384             ((eq type 'comment)
385              (setq dest
386                    (append dest
387                            '(("(" nil nil))
388                            (tm-eword::words-to-ruled-words
389                             (tm-eword::lc-words-to-words
390                              (tm-eword::split-to-lc-words (cdr token))))
391                            '((")" nil nil))
392                            ))
393              )
394             (t
395              (setq dest (append dest
396                                 (tm-eword::words-to-ruled-words
397                                  (tm-eword::lc-words-to-words
398                                   (tm-eword::split-to-lc-words (cdr token))
399                                   ))))
400              ))
401       (setq phrase (cdr phrase))
402       )
403     (tm-eword::space-process dest)
404     ))
405
406 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
407   (if (eq (car phrase-route-addr) 'phrase-route-addr)
408       (let ((phrase (nth 1 phrase-route-addr))
409             (route (nth 2 phrase-route-addr))
410             dest)
411         (if (eq (car (car phrase)) 'spaces)
412             (setq phrase (cdr phrase))
413           )
414         (setq dest (tm-eword::phrase-to-rwl phrase))
415         (if dest
416             (setq dest (append dest '((" " nil nil))))
417           )
418         (append
419          dest
420          (list (list (concat "<" (rfc822/addr-to-string route) ">") nil nil))
421          ))))
422
423 (defun tm-eword::addr-spec-to-rwl (addr-spec)
424   (if (eq (car addr-spec) 'addr-spec)
425       (list (list (rfc822/addr-to-string (cdr addr-spec)) nil nil))
426     ))
427
428 (defun tm-eword::mailbox-to-rwl (mbox)
429   (let ((addr (nth 1 mbox))
430         (comment (nth 2 mbox))
431         dest)
432     (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
433                    (tm-eword::addr-spec-to-rwl addr)
434                    ))
435     (if comment
436         (setq dest
437               (append dest
438                       '((" " nil nil)
439                         ("(" nil nil))
440                       (tm-eword::split-string comment)
441                       '((")" nil nil))
442                       )))
443     dest))
444
445 (defun tm-eword::addresses-to-rwl (addresses)
446   (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
447     (if dest
448         (while (setq addresses (cdr addresses))
449           (setq dest (append dest
450                              '(("," nil nil))
451                              '((" " nil nil))
452                              (tm-eword::mailbox-to-rwl (car addresses))
453                              ))
454           ))
455     dest))
456
457 (defun tm-eword::encode-address-list (column str)
458   (tm-eword::encode-rwl
459    column
460    (tm-eword::addresses-to-rwl
461     (rfc822/parse-addresses
462      (rfc822/lexical-analyze str)))))
463
464
465 ;;; @ application interfaces
466 ;;;
467
468 (defun mime/encode-field (str)
469   (setq str (rfc822/unfolding-string str))
470   (let ((ret (string-match rfc822/field-top-regexp str)))
471     (or (if ret
472             (let ((field-name (substring str 0 (match-end 1)))
473                   (field-body (eliminate-top-spaces
474                                (substring str (match-end 0))))
475                   fname)
476               (if (setq ret
477                         (cond ((string= field-body "") "")
478                               ((member (setq fname (downcase field-name))
479                                        '("reply-to" "from" "sender"
480                                          "resent-reply-to" "resent-from"
481                                          "resent-sender" "to" "resent-to"
482                                          "cc" "resent-cc"
483                                          "bcc" "resent-bcc" "dcc")
484                                        )
485                                (car (tm-eword::encode-address-list
486                                      (+ (length field-name) 1) field-body))
487                                )
488                               (t
489                                (catch 'tag
490                                  (let ((r mime/no-encoding-header-fields)
491                                        fn)
492                                    (while r
493                                      (setq fn (car r))
494                                      (if (string= (downcase fn) fname)
495                                          (throw 'tag field-body)
496                                        )
497                                      (setq r (cdr r))
498                                      ))
499                                  (car (tm-eword::encode-string
500                                        (+ (length field-name) 1) field-body))
501                                  ))
502                               ))
503                   (concat field-name ": " ret)
504                 )))
505         (tm-eword::encode-string 0 str)
506         )))
507
508 (defun mime/exist-encoded-word-in-subject ()
509   (let ((str (rfc822/get-field-body "Subject")))
510     (if (and str (string-match mime/encoded-word-regexp str))
511         str)))
512
513 (defun mime/encode-message-header ()
514   (interactive "*")
515   (save-excursion
516     (save-restriction
517       (narrow-to-region (goto-char (point-min))
518                         (if (re-search-forward
519                              (concat
520                               "^" (regexp-quote mail-header-separator) "$")
521                              nil t)
522                             (match-beginning 0)
523                           (point-max)))
524       (goto-char (point-min))
525       (let (beg end field)
526         (while (re-search-forward rfc822/field-top-regexp nil t)
527           (setq beg (match-beginning 0))
528           (setq end (rfc822/field-end))
529           (if (and (find-charset-region beg end)
530                    (setq field
531                          (mime/encode-field
532                           (buffer-substring-no-properties beg end)
533                           ))
534                    )
535               (progn
536                 (delete-region beg end)
537                 (insert field)
538                 ))
539           ))
540       (if mime/use-X-Nsubject
541           (let ((str (mime/exist-encoded-word-in-subject)))
542             (if str
543                 (insert
544                  (concat
545                   "\nX-Nsubject: "
546                   (mime-eword/decode-string (rfc822/unfolding-string str))
547                   )))))
548       )))
549
550 (defun mime-eword/encode-string (str &optional column mode)
551   (car (tm-eword::encode-rwl (or column 0)
552                              (tm-eword::split-string str) mode))
553   )
554
555
556 ;;; @ end
557 ;;;
558
559 (provide 'tm-ew-e)
560
561 ;;; tm-ew-e.el ends here