tm 7.67.
[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.21 $
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.21 1996/06/06 15:03:38 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 ew-f pew-f)
321     (while rwl
322       (setq ew-f (nth 2 (car rwl)))
323       (if (and pew-f ew-f)
324           (setq rwl (cons '(" ") rwl)
325                 pew-f nil)
326         (setq pew-f ew-f)
327         )
328       (setq ret (tm-eword::encode-string-1 column rwl mode))
329       (setq str (car ret))
330       (if (eq (elt str 0) ?\n)
331           (if (eq special ?\()
332               (progn
333                 (setq dest (concat dest "\n ("))
334                 (setq ret (tm-eword::encode-string-1 2 rwl mode))
335                 (setq str (car ret))
336                 ))
337         (cond ((eq special 32)
338                (if (string= str "(")
339                    (setq ps t)
340                  (setq dest (concat dest " "))
341                  (setq ps nil)
342                  ))
343               ((eq special ?\()
344                (if ps
345                    (progn
346                      (setq dest (concat dest " ("))
347                      (setq ps nil)
348                      )
349                  (setq dest (concat dest "("))
350                  )
351                )))
352       (cond ((string= str " ")
353              (setq special 32)
354              )
355             ((string= str "(")
356              (setq special ?\()
357              )
358             (t
359              (setq special nil)
360              (setq dest (concat dest str))
361              ))
362       (setq column (nth 1 ret)
363             rwl (nth 2 ret))
364       )
365     (list dest column)
366     ))
367
368 (defun tm-eword::encode-string (column str &optional mode)
369   (tm-eword::encode-rwl column (tm-eword::split-string str) mode)
370   )
371
372
373 ;;; @ converter
374 ;;;
375
376 (defun tm-eword::phrase-to-rwl (phrase)
377   (let (token type dest str)
378     (while phrase
379       (setq token (car phrase))
380       (setq type (car token))
381       (cond ((eq type 'quoted-string)
382              (setq str (concat "\"" (cdr token) "\""))
383              (setq dest
384                    (append dest
385                            (list
386                             (cons str (tm-eword::find-charset-rule
387                                        (find-charset-string str)))
388                             )))
389              )
390             ((eq type 'comment)
391              (setq dest
392                    (append dest
393                            '(("(" nil nil))
394                            (tm-eword::words-to-ruled-words
395                             (tm-eword::lc-words-to-words
396                              (tm-eword::split-to-lc-words (cdr token))))
397                            '((")" nil nil))
398                            ))
399              )
400             (t
401              (setq dest (append dest
402                                 (tm-eword::words-to-ruled-words
403                                  (tm-eword::lc-words-to-words
404                                   (tm-eword::split-to-lc-words (cdr token))
405                                   ))))
406              ))
407       (setq phrase (cdr phrase))
408       )
409     (tm-eword::space-process dest)
410     ))
411
412 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
413   (if (eq (car phrase-route-addr) 'phrase-route-addr)
414       (let ((phrase (nth 1 phrase-route-addr))
415             (route (nth 2 phrase-route-addr))
416             dest)
417         (if (eq (car (car phrase)) 'spaces)
418             (setq phrase (cdr phrase))
419           )
420         (setq dest (tm-eword::phrase-to-rwl phrase))
421         (if dest
422             (setq dest (append dest '((" " nil nil))))
423           )
424         (append
425          dest
426          (list (list (concat "<" (rfc822/addr-to-string route) ">") nil nil))
427          ))))
428
429 (defun tm-eword::addr-spec-to-rwl (addr-spec)
430   (if (eq (car addr-spec) 'addr-spec)
431       (list (list (rfc822/addr-to-string (cdr addr-spec)) nil nil))
432     ))
433
434 (defun tm-eword::mailbox-to-rwl (mbox)
435   (let ((addr (nth 1 mbox))
436         (comment (nth 2 mbox))
437         dest)
438     (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
439                    (tm-eword::addr-spec-to-rwl addr)
440                    ))
441     (if comment
442         (setq dest
443               (append dest
444                       '((" " nil nil)
445                         ("(" nil nil))
446                       (tm-eword::split-string comment)
447                       '((")" nil nil))
448                       )))
449     dest))
450
451 (defun tm-eword::addresses-to-rwl (addresses)
452   (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
453     (if dest
454         (while (setq addresses (cdr addresses))
455           (setq dest (append dest
456                              '(("," nil nil))
457                              '((" " nil nil))
458                              (tm-eword::mailbox-to-rwl (car addresses))
459                              ))
460           ))
461     dest))
462
463 (defun tm-eword::encode-address-list (column str)
464   (tm-eword::encode-rwl
465    column
466    (tm-eword::addresses-to-rwl
467     (rfc822/parse-addresses
468      (rfc822/lexical-analyze str)))))
469
470
471 ;;; @ application interfaces
472 ;;;
473
474 (defun mime/encode-field (str)
475   (setq str (rfc822/unfolding-string str))
476   (let ((ret (string-match rfc822/field-top-regexp str)))
477     (or (if ret
478             (let ((field-name (substring str 0 (match-end 1)))
479                   (field-body (eliminate-top-spaces
480                                (substring str (match-end 0))))
481                   fname)
482               (if (setq ret
483                         (cond ((string= field-body "") "")
484                               ((member (setq fname (downcase field-name))
485                                        '("reply-to" "from" "sender"
486                                          "resent-reply-to" "resent-from"
487                                          "resent-sender" "to" "resent-to"
488                                          "cc" "resent-cc"
489                                          "bcc" "resent-bcc" "dcc")
490                                        )
491                                (car (tm-eword::encode-address-list
492                                      (+ (length field-name) 2) field-body))
493                                )
494                               (t
495                                (catch 'tag
496                                  (let ((r mime/no-encoding-header-fields)
497                                        fn)
498                                    (while r
499                                      (setq fn (car r))
500                                      (if (string= (downcase fn) fname)
501                                          (throw 'tag field-body)
502                                        )
503                                      (setq r (cdr r))
504                                      ))
505                                  (car (tm-eword::encode-string
506                                        (+ (length field-name) 1) field-body))
507                                  ))
508                               ))
509                   (concat field-name ": " ret)
510                 )))
511         (car (tm-eword::encode-string 0 str))
512         )))
513
514 (defun mime/exist-encoded-word-in-subject ()
515   (let ((str (rfc822/get-field-body "Subject")))
516     (if (and str (string-match mime/encoded-word-regexp str))
517         str)))
518
519 (defun mime/encode-message-header ()
520   (interactive "*")
521   (save-excursion
522     (save-restriction
523       (narrow-to-region (goto-char (point-min))
524                         (if (re-search-forward
525                              (concat
526                               "^" (regexp-quote mail-header-separator) "$")
527                              nil t)
528                             (match-beginning 0)
529                           (point-max)))
530       (goto-char (point-min))
531       (let (beg end field)
532         (while (re-search-forward rfc822/field-top-regexp nil t)
533           (setq beg (match-beginning 0))
534           (setq end (rfc822/field-end))
535           (if (and (find-charset-region beg end)
536                    (setq field
537                          (mime/encode-field
538                           (buffer-substring-no-properties beg end)
539                           ))
540                    )
541               (progn
542                 (delete-region beg end)
543                 (insert field)
544                 ))
545           ))
546       (if mime/use-X-Nsubject
547           (let ((str (mime/exist-encoded-word-in-subject)))
548             (if str
549                 (insert
550                  (concat
551                   "\nX-Nsubject: "
552                   (mime-eword/decode-string (rfc822/unfolding-string str))
553                   )))))
554       )))
555
556 (defun mime-eword/encode-string (str &optional column mode)
557   (car (tm-eword::encode-rwl (or column 0)
558                              (tm-eword::split-string str) mode))
559   )
560
561
562 ;;; @ end
563 ;;;
564
565 (provide 'tm-ew-e)
566
567 ;;; tm-ew-e.el ends here