1 ;;; eword-encode.el --- RFC 2047 based encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (require 'eword-decode)
36 (defgroup eword-encode nil
37 "Encoded-word encoding"
40 (defcustom eword-field-encoding-method-alist
41 '(("X-Nsubject" . iso-2022-jp-2)
46 "*Alist to specify field encoding method.
47 Its key is field-name, value is encoding method.
49 If method is `mime', this field will be encoded into MIME format.
51 If method is a MIME-charset, this field will be encoded as the charset
52 when it must be convert into network-code.
54 If method is `default-mime-charset', this field will be encoded as
55 variable `default-mime-charset' when it must be convert into
58 If method is nil, this field will not be encoded."
60 :type '(repeat (cons (choice :tag "Field"
62 (const :tag "Default" t))
64 (const :tag "MIME conversion" mime)
65 (symbol :tag "non-MIME conversion")
66 (const :tag "no-conversion" nil)))))
68 (defvar eword-charset-encoding-alist
87 (iso-2022-int-1 . "B")
92 ;;; @ encoded-text encoder
95 (defun eword-encode-text (charset encoding string &optional mode)
96 "Encode STRING as an encoded-word, and return the result.
97 CHARSET is a symbol to indicate MIME charset of the encoded-word.
98 ENCODING allows \"B\" or \"Q\".
99 MODE is allows `text', `comment', `phrase' or nil. Default value is
101 (let ((text (encoded-text-encode-string string encoding)))
103 (concat "=?" (upcase (symbol-name charset)) "?"
104 encoding "?" text "?=")
111 (defsubst eword-encode-char-type (character)
112 (if (memq character '(? ?\t ?\n))
114 (char-charset character)
117 (defun eword-encode-divide-into-charset-words (string)
118 (let ((len (length string))
121 (let* ((chr (sref string 0))
122 (charset (eword-encode-char-type chr))
123 (i (char-length chr)))
124 (while (and (< i len)
125 (setq chr (sref string i))
126 (eq charset (eword-encode-char-type chr))
128 (setq i (char-next-index chr i))
130 (setq dest (cons (cons charset (substring string 0 i)) dest)
131 string (substring string i)
141 (defun eword-encode-charset-words-to-words (charset-words)
144 (let* ((charset-word (car charset-words))
145 (charset (car charset-word))
148 (let ((charsets (list charset))
149 (str (cdr charset-word))
152 (while (setq charset-words (cdr charset-words))
153 (setq charset-word (car charset-words)
154 charset (car charset-word))
158 (or (memq charset charsets)
159 (setq charsets (cons charset charsets))
161 (setq str (concat str (cdr charset-word)))
163 (setq dest (cons (cons charsets str) dest))
165 (setq dest (cons charset-word dest)
166 charset-words (cdr charset-words)
175 (defmacro make-ew-rword (text charset encoding type)
176 (` (list (, text)(, charset)(, encoding)(, type))))
177 (defmacro ew-rword-text (rword)
179 (defmacro ew-rword-charset (rword)
180 (` (car (cdr (, rword)))))
181 (defmacro ew-rword-encoding (rword)
182 (` (car (cdr (cdr (, rword))))))
183 (defmacro ew-rword-type (rword)
184 (` (car (cdr (cdr (cdr (, rword)))))))
186 (defun ew-find-charset-rule (charsets)
188 (let* ((charset (find-mime-charset-by-charsets charsets))
189 (encoding (cdr (or (assq charset eword-charset-encoding-alist)
191 (list charset encoding)
194 (defun tm-eword::words-to-ruled-words (wl &optional mode)
197 (let ((ret (ew-find-charset-rule (car word))))
198 (make-ew-rword (cdr word) (car ret)(nth 1 ret) mode)
202 (defun ew-space-process (seq)
203 (let (prev a ac b c cc)
208 (setq cc (ew-rword-charset c))
209 (if (and (null (ew-rword-charset b))
210 (not (eq (ew-rword-type b) 'special)))
213 (setq ac (ew-rword-charset a))
214 (if (and (ew-rword-encoding a)
215 (ew-rword-encoding c))
218 (cons (concat (car a)(car b)(car c))
226 (cons (concat (car a)(car b))
231 (setq prev (cons b prev))
233 (setq prev (cons b prev))
238 (defun eword-encode-split-string (str &optional mode)
240 (tm-eword::words-to-ruled-words
241 (eword-encode-charset-words-to-words
242 (eword-encode-divide-into-charset-words str))
249 (defun tm-eword::encoded-word-length (rword)
250 (let ((string (ew-rword-text rword))
251 (charset (ew-rword-charset rword))
252 (encoding (ew-rword-encoding rword))
255 (cond ((string-equal encoding "B")
256 (setq string (encode-mime-charset-string string charset))
257 (base64-encoded-length string)
259 ((string-equal encoding "Q")
260 (setq string (encode-mime-charset-string string charset))
261 (Q-encoded-text-length string (ew-rword-type rword))
264 (cons (+ 7 (length (symbol-name charset)) ret) string)
271 (defun ew-encode-rword-1 (column rwl &optional must-output)
272 (catch 'can-not-output
273 (let* ((rword (car rwl))
274 (ret (tm-eword::encoded-word-length rword))
277 (cond ((and (setq string (car rword))
278 (or (<= (setq len (+ (length string) column)) 76)
283 ((memq (aref string 0) '(? ?\t))
284 (setq string (concat "\n" string)
293 (throw 'can-not-output nil)
295 (cond ((and (setq len (car ret))
296 (<= (+ column len) 76)
300 (ew-rword-charset rword)
301 (ew-rword-encoding rword)
303 (ew-rword-type rword)
305 (setq len (+ (length string) column))
309 (setq string (car rword))
312 (while (and (< p len)
314 (setq np (char-next-index (sref string p) p))
315 (setq nstr (substring string 0 np))
316 (setq ret (tm-eword::encoded-word-length
317 (cons nstr (cdr rword))
319 (setq nstr (cdr ret))
320 (setq len (+ (car ret) column))
325 (if (string-equal str "")
329 (throw 'can-not-output nil))
330 (setq rwl (cons (cons (substring string p) (cdr rword))
334 (ew-rword-charset rword)
335 (ew-rword-encoding rword)
337 (ew-rword-type rword)))
338 (setq len (+ (length string) column))
342 (list string len rwl)
345 (defun eword-encode-rword-list (column rwl)
346 (let (ret dest str ew-f pew-f folded-points)
348 (setq ew-f (nth 2 (car rwl)))
350 (setq rwl (cons '(" ") rwl)
354 (if (null (setq ret (ew-encode-rword-1 column rwl)))
355 (let ((i (1- (length dest)))
360 (cond ((memq (setq c (aref dest i)) '(? ?\t))
361 (if (memq i folded-points)
363 (setq folded-points (cons i folded-points))
370 (setq s (substring dest i)
372 r-dest (concat (substring dest 0 i) "\n" s))
373 (when (setq ret (ew-encode-rword-1 r-column rwl))
378 (setq ret (ew-encode-rword-1 column rwl 'must-output))
381 (setq dest (concat dest str))
382 (setq column (nth 1 ret)
392 (defun eword-encode-phrase-to-rword-list (phrase)
393 (let (token type dest str)
395 (setq token (car phrase))
396 (setq type (car token))
397 (cond ((eq type 'quoted-string)
398 (setq str (concat "\"" (cdr token) "\""))
402 (let ((ret (ew-find-charset-rule
403 (find-non-ascii-charset-string str))))
405 str (car ret)(nth 1 ret) 'phrase)
412 '(("(" nil nil special))
413 (tm-eword::words-to-ruled-words
414 (eword-encode-charset-words-to-words
415 (eword-encode-divide-into-charset-words
418 '((")" nil nil special))
424 (tm-eword::words-to-ruled-words
425 (eword-encode-charset-words-to-words
426 (eword-encode-divide-into-charset-words
430 (setq phrase (cdr phrase))
432 (ew-space-process dest)
435 (defun eword-encode-addr-seq-to-rword-list (seq)
438 (let* ((token (car seq))
441 (cond ((eq name 'spaces)
442 (setq dest (nconc dest (list (list (cdr token) nil nil))))
448 (list (list "(" nil nil))
449 (eword-encode-split-string (cdr token) 'comment)
450 (list (list ")" nil nil))
453 ((eq name 'quoted-string)
458 (list (concat "\"" (cdr token) "\"") nil nil)
463 (if (or (eq pname 'spaces)
465 (nconc dest (list (list (cdr token) nil nil)))
466 (nconc (butlast dest)
468 (list (concat (car (car (last dest)))
477 (defun eword-encode-phrase-route-addr-to-rword-list (phrase-route-addr)
478 (if (eq (car phrase-route-addr) 'phrase-route-addr)
479 (let ((phrase (nth 1 phrase-route-addr))
480 (route (nth 2 phrase-route-addr))
482 ;; (if (eq (car (car phrase)) 'spaces)
483 ;; (setq phrase (cdr phrase))
485 (setq dest (eword-encode-phrase-to-rword-list phrase))
487 (setq dest (append dest '((" " nil nil))))
491 (eword-encode-addr-seq-to-rword-list
492 (append '((specials . "<"))
494 '((specials . ">"))))
497 (defun eword-encode-addr-spec-to-rword-list (addr-spec)
498 (if (eq (car addr-spec) 'addr-spec)
499 (eword-encode-addr-seq-to-rword-list (cdr addr-spec))
502 (defun eword-encode-mailbox-to-rword-list (mbox)
503 (let ((addr (nth 1 mbox))
504 (comment (nth 2 mbox))
506 (setq dest (or (eword-encode-phrase-route-addr-to-rword-list addr)
507 (eword-encode-addr-spec-to-rword-list addr)
514 (eword-encode-split-string comment 'comment)
515 (list '(")" nil nil))
519 (defsubst eword-encode-addresses-to-rword-list (addresses)
520 (let ((dest (eword-encode-mailbox-to-rword-list (car addresses))))
522 (while (setq addresses (cdr addresses))
525 (list '("," nil nil))
526 ;; (list '(" " nil nil))
527 (eword-encode-mailbox-to-rword-list (car addresses))
532 (defsubst eword-encode-msg-id-to-rword-list (msg-id)
536 (caar (eword-encode-addr-seq-to-rword-list (cdr msg-id)))
540 (defsubst eword-encode-in-reply-to-to-rword-list (in-reply-to)
545 (let ((elt (car in-reply-to)))
546 (if (eq (car elt) 'phrase)
547 (eword-encode-phrase-to-rword-list (cdr elt))
548 (eword-encode-msg-id-to-rword-list elt)
550 (setq in-reply-to (cdr in-reply-to)))
554 ;;; @ application interfaces
557 (defcustom eword-encode-default-start-column 10
558 "Default start column if it is omitted."
562 (defun eword-encode-string (string &optional column mode)
563 "Encode STRING as encoded-words, and return the result.
564 Optional argument COLUMN is start-position of the field.
565 Optional argument MODE allows `text', `comment', `phrase' or nil.
566 Default value is `phrase'."
567 (car (eword-encode-rword-list
568 (or column eword-encode-default-start-column)
569 (eword-encode-split-string string mode))))
571 (defun eword-encode-address-list (string &optional column)
572 "Encode header field STRING as list of address, and return the result.
573 Optional argument COLUMN is start-position of the field."
574 (car (eword-encode-rword-list
575 (or column eword-encode-default-start-column)
576 (eword-encode-addresses-to-rword-list
577 (std11-parse-addresses-string string))
580 (defun eword-encode-in-reply-to (string &optional column)
581 "Encode header field STRING as In-Reply-To field, and return the result.
582 Optional argument COLUMN is start-position of the field."
583 (car (eword-encode-rword-list
585 (eword-encode-in-reply-to-to-rword-list
586 (std11-parse-msg-ids-string string)))))
588 (defun eword-encode-structured-field-body (string &optional column)
589 "Encode header field STRING as structured field, and return the result.
590 Optional argument COLUMN is start-position of the field."
591 (car (eword-encode-rword-list
592 (or column eword-encode-default-start-column)
593 (eword-encode-addr-seq-to-rword-list (std11-lexical-analyze string))
596 (defun eword-encode-unstructured-field-body (string &optional column)
597 "Encode header field STRING as unstructured field, and return the result.
598 Optional argument COLUMN is start-position of the field."
599 (car (eword-encode-rword-list
600 (or column eword-encode-default-start-column)
601 (eword-encode-split-string string 'text))))
603 (defun eword-encode-field-body (field-body field-name)
604 "Encode FIELD-BODY as FIELD-NAME, and return the result.
605 A lexical token includes non-ASCII character is encoded as MIME
606 encoded-word. ASCII token is not encoded."
607 (setq field-body (std11-unfold-string field-body))
608 (if (string= field-body "")
611 (if (symbolp field-name)
612 (setq start (1+ (length (symbol-name field-name))))
613 (setq start (1+ (length field-name))
614 field-name (intern (capitalize field-name))))
615 (cond ((memq field-name
618 Resent-Reply-To Resent-From
619 Resent-Sender To Resent-To
620 Cc Resent-Cc Bcc Resent-Bcc
622 (eword-encode-address-list field-body start)
624 ((eq field-name 'In-Reply-To)
625 (eword-encode-in-reply-to field-body start)
627 ((memq field-name '(Mime-Version User-Agent))
628 (eword-encode-structured-field-body field-body start)
631 (eword-encode-unstructured-field-body field-body start)
635 (defun eword-in-subject-p ()
636 (let ((str (std11-field-body "Subject")))
637 (if (and str (string-match eword-encoded-word-regexp str))
640 (defsubst eword-find-field-encoding-method (field-name)
641 (setq field-name (downcase field-name))
642 (let ((alist eword-field-encoding-method-alist))
645 (let* ((pair (car alist))
647 (if (and (stringp str)
648 (string= field-name (downcase str)))
649 (throw 'found (cdr pair))
651 (setq alist (cdr alist)))
652 (cdr (assq t eword-field-encoding-method-alist))
655 (defun eword-encode-header (&optional code-conversion)
656 "Encode header fields to network representation, such as MIME encoded-word.
658 It refer variable `eword-field-encoding-method-alist'."
662 (std11-narrow-to-header mail-header-separator)
663 (goto-char (point-min))
664 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
666 (while (re-search-forward std11-field-head-regexp nil t)
667 (setq bbeg (match-end 0)
668 field-name (buffer-substring (match-beginning 0) (1- bbeg))
669 end (std11-field-end))
670 (and (find-non-ascii-charset-region bbeg end)
671 (let ((method (eword-find-field-encoding-method
672 (downcase field-name))))
673 (cond ((eq method 'mime)
675 (buffer-substring-no-properties bbeg end)
677 (delete-region bbeg end)
678 (insert (eword-encode-field-body field-body
683 (or (mime-charset-to-coding-system
686 (encode-coding-region bbeg end cs)
696 (provide 'eword-encode)
698 ;;; eword-encode.el ends here