1 ;;; tm-ew-e.el --- RFC 1522 based MIME encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Revision: 7.53 $
7 ;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
9 ;; This file is part of tm (Tools for MIME).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
37 (defconst tm-ew-e/RCS-ID
38 "$Id: tm-ew-e.el,v 7.53 1996/09/04 22:02:02 morioka Exp $")
39 (defconst mime-eword/encoder-version (get-version-string tm-ew-e/RCS-ID))
45 (defvar mime/field-encoding-method-alist
46 (if (boundp 'mime/no-encoding-header-fields)
50 (cons field-name 'default-mime-charset)
52 mime/no-encoding-header-fields)
55 '(("X-Nsubject" . iso-2022-jp-2)
59 "*Alist to specify field encoding method.
60 Its key is field-name, value is encoding method.
62 If method is `mime', this field will be encoded into MIME format.
64 If method is a MIME-charset, this field will be encoded as the charset
65 when it must be convert into network-code.
67 If method is `default-mime-charset', this field will be encoded as
68 variable `default-mime-charset' when it must be convert into
71 If method is nil, this field will not be encoded. [tm-ew-e.el]")
73 (defvar mime/generate-X-Nsubject
74 (and (boundp 'mime/use-X-Nsubject)
76 "*If it is not nil, X-Nsubject field is generated
77 when Subject field is encoded by `mime/encode-message-header'.
80 (defvar mime-eword/charset-encoding-alist
95 (iso-2022-int-1 . "B")
98 ;;; @ encoded-text encoder
101 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
103 (cond ((string= encoding "B")
104 (base64-encode-string string))
105 ((string= encoding "Q")
106 (q-encoding-encode-string string mode))
110 (concat "=?" (upcase (symbol-name charset)) "?"
111 encoding "?" text "?=")
118 (defun tm-eword::char-type (chr)
119 (if (or (= chr 32)(= chr ?\t))
121 (char-leading-char chr)
124 (defun tm-eword::parse-lc-word (str)
125 (let* ((chr (sref str 0))
126 (lc (tm-eword::char-type chr))
127 (i (char-length chr))
130 (while (and (< i len)
131 (setq chr (sref str i))
132 (eq lc (tm-eword::char-type chr))
134 (setq i (+ i (char-length chr)))
136 (cons (cons lc (substring str 0 i)) (substring str i))
139 (defun tm-eword::split-to-lc-words (str)
141 (while (and (not (string= str ""))
142 (setq ret (tm-eword::parse-lc-word str))
144 (setq dest (cons (car ret) dest))
154 (defun tm-eword::parse-word (lcwl)
155 (let* ((lcw (car lcwl))
160 (let ((lcl (list lc))
164 (while (setq lcwl (cdr lcwl))
165 (setq lcw (car lcwl))
170 (if (not (memq lc lcl))
171 (setq lcl (cons lc lcl))
173 (setq str (concat str (cdr lcw)))
175 (cons (cons lcl str) lcwl)
178 (defun tm-eword::lc-words-to-words (lcwl)
180 (while (setq ret (tm-eword::parse-word lcwl))
181 (setq dest (cons (car ret) dest))
182 (setq lcwl (cdr ret))
191 (defmacro tm-eword::make-rword (text charset encoding type)
192 (` (list (, text)(, charset)(, encoding)(, type))))
193 (defmacro tm-eword::rword-text (rword)
195 (defmacro tm-eword::rword-charset (rword)
196 (` (car (cdr (, rword)))))
197 (defmacro tm-eword::rword-encoding (rword)
198 (` (car (cdr (cdr (, rword))))))
199 (defmacro tm-eword::rword-type (rword)
200 (` (car (cdr (cdr (cdr (, rword)))))))
202 (defun tm-eword::find-charset-rule (charsets)
204 (let* ((charset (charsets-to-mime-charset charsets))
205 (encoding (cdr (assq charset mime-eword/charset-encoding-alist)))
207 (list charset encoding)
210 (defun tm-eword::words-to-ruled-words (wl &optional mode)
213 (let ((ret (tm-eword::find-charset-rule (car word))))
214 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
218 (defun tm-eword::space-process (seq)
219 (let (prev a ac b c cc)
224 (setq cc (tm-eword::rword-charset c))
225 (if (null (tm-eword::rword-charset b))
228 (setq ac (tm-eword::rword-charset a))
229 (if (and (tm-eword::rword-encoding a)
230 (tm-eword::rword-encoding c))
233 (cons (concat (car a)(car b)(car c))
241 (cons (concat (car a)(car b))
246 (setq prev (cons b prev))
248 (setq prev (cons b prev))
253 (defun tm-eword::split-string (str &optional mode)
254 (tm-eword::space-process
255 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
256 (tm-eword::split-to-lc-words str))
263 (defun tm-eword::encoded-word-length (rword)
264 (let ((string (tm-eword::rword-text rword))
265 (charset (tm-eword::rword-charset rword))
266 (encoding (tm-eword::rword-encoding rword))
269 (cond ((string-equal encoding "B")
270 (setq string (encode-mime-charset-string string charset))
271 (base64-encoded-length string)
273 ((string-equal encoding "Q")
274 (setq string (encode-mime-charset-string string charset))
275 (q-encoding-encoded-length string
276 (tm-eword::rword-type rword))
279 (cons (+ 7 (length (symbol-name charset)) ret) string)
286 (defun tm-eword::encode-string-1 (column rwl)
287 (let* ((rword (car rwl))
288 (ret (tm-eword::encoded-word-length rword))
291 (cond ((and (setq string (car rword))
292 (<= (setq len (+ (length string) column)) 76)
300 (cond ((and (setq len (car ret))
301 (<= (+ column len) 76)
304 (tm-eword::encode-encoded-text
305 (tm-eword::rword-charset rword)
306 (tm-eword::rword-encoding rword)
308 (tm-eword::rword-type rword)
310 (setq len (+ (length string) column))
314 (setq string (car rword))
315 (let* ((sl (length string))
318 (while (and (< p len)
320 (setq np (+ p (char-length (sref string p))))
321 (setq nstr (substring string 0 np))
322 (setq ret (tm-eword::encoded-word-length
323 (cons nstr (cdr rword))
325 (setq nstr (cdr ret))
326 (setq len (+ (car ret) column))
331 (if (string-equal str "")
334 (setq rwl (cons (cons (substring string p) (cdr rword))
337 (tm-eword::encode-encoded-text
338 (tm-eword::rword-charset rword)
339 (tm-eword::rword-encoding rword)
341 (tm-eword::rword-type rword)))
342 (setq len (+ (length string) column))
346 (list string len rwl)
349 (defun tm-eword::encode-rwl (column rwl)
350 (let (ret dest ps special str ew-f pew-f)
352 (setq ew-f (nth 2 (car rwl)))
354 (setq rwl (cons '(" ") rwl)
358 (setq ret (tm-eword::encode-string-1 column rwl))
360 (if (eq (elt str 0) ?\n)
363 (setq dest (concat dest "\n ("))
364 (setq ret (tm-eword::encode-string-1 2 rwl))
367 (cond ((eq special 32)
368 (if (string= str "(")
370 (setq dest (concat dest " "))
376 (setq dest (concat dest " ("))
379 (setq dest (concat dest "("))
382 (cond ((string= str " ")
390 (setq dest (concat dest str))
392 (setq column (nth 1 ret)
398 (defun tm-eword::encode-string (column str &optional mode)
399 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
406 (defun tm-eword::phrase-to-rwl (phrase)
407 (let (token type dest str)
409 (setq token (car phrase))
410 (setq type (car token))
411 (cond ((eq type 'quoted-string)
412 (setq str (concat "\"" (cdr token) "\""))
416 (let ((ret (tm-eword::find-charset-rule
417 (find-charset-string str))))
418 (tm-eword::make-rword
419 str (car ret)(nth 1 ret) 'phrase)
427 (tm-eword::words-to-ruled-words
428 (tm-eword::lc-words-to-words
429 (tm-eword::split-to-lc-words (cdr token)))
435 (setq dest (append dest
436 (tm-eword::words-to-ruled-words
437 (tm-eword::lc-words-to-words
438 (tm-eword::split-to-lc-words (cdr token))
441 (setq phrase (cdr phrase))
443 (tm-eword::space-process dest)
446 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
447 (if (eq (car phrase-route-addr) 'phrase-route-addr)
448 (let ((phrase (nth 1 phrase-route-addr))
449 (route (nth 2 phrase-route-addr))
451 (if (eq (car (car phrase)) 'spaces)
452 (setq phrase (cdr phrase))
454 (setq dest (tm-eword::phrase-to-rwl phrase))
456 (setq dest (append dest '((" " nil nil))))
460 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
463 (defun tm-eword::addr-spec-to-rwl (addr-spec)
464 (if (eq (car addr-spec) 'addr-spec)
465 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
468 (defun tm-eword::mailbox-to-rwl (mbox)
469 (let ((addr (nth 1 mbox))
470 (comment (nth 2 mbox))
472 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
473 (tm-eword::addr-spec-to-rwl addr)
480 (tm-eword::split-string comment 'comment)
485 (defun tm-eword::addresses-to-rwl (addresses)
486 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
488 (while (setq addresses (cdr addresses))
489 (setq dest (append dest
492 (tm-eword::mailbox-to-rwl (car addresses))
497 (defun tm-eword::encode-address-list (column str)
498 (tm-eword::encode-rwl
500 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
504 ;;; @ application interfaces
507 (defun mime/encode-field (str)
508 (setq str (std11-unfold-string str))
509 (let ((ret (string-match std11-field-head-regexp str)))
511 (let ((field-name (substring str 0 (1- (match-end 0))))
512 (field-body (eliminate-top-spaces
513 (substring str (match-end 0))))
516 (cond ((string-equal field-body "") "")
517 ((member (setq fname (downcase field-name))
518 '("reply-to" "from" "sender"
519 "resent-reply-to" "resent-from"
520 "resent-sender" "to" "resent-to"
522 "bcc" "resent-bcc" "dcc")
524 (car (tm-eword::encode-address-list
525 (+ (length field-name) 2) field-body))
528 (car (tm-eword::encode-string
529 (+ (length field-name) 1)
533 (concat field-name ": " ret)
535 (car (tm-eword::encode-string 0 str))
538 (defun mime/exist-encoded-word-in-subject ()
539 (let ((str (std11-field-body "Subject")))
540 (if (and str (string-match mime/encoded-word-regexp str))
543 (defun mime/encode-message-header (&optional code-conversion)
547 (std11-narrow-to-header mail-header-separator)
548 (goto-char (point-min))
549 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
551 (while (re-search-forward std11-field-head-regexp nil t)
552 (setq beg (match-beginning 0))
553 (setq field-name (buffer-substring beg (1- (match-end 0))))
554 (setq end (std11-field-end))
555 (and (find-charset-region beg end)
556 (let ((ret (or (ASSOC (downcase field-name)
557 mime/field-encoding-method-alist
564 (assq t mime/field-encoding-method-alist)
567 (let ((method (cdr ret)))
568 (cond ((eq method 'mime)
570 (buffer-substring-no-properties beg end)
572 (delete-region beg end)
573 (insert (mime/encode-field field))
577 (or (mime-charset-to-coding-system
580 (encode-coding-region beg end cs)
585 (and mime/generate-X-Nsubject
586 (or (std11-field-body "X-Nsubject")
587 (let ((str (mime/exist-encoded-word-in-subject)))
591 (mime-eword/decode-string
592 (std11-unfold-string str)))
595 (encode-mime-charset-string
599 mime/field-encoding-method-alist
609 (insert (concat "\nX-Nsubject: " str))
613 (defun mime-eword/encode-string (str &optional column mode)
614 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
623 ;;; tm-ew-e.el ends here