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
88 (iso-2022-int-1 . "B")
93 ;;; @ encoded-text encoder
96 (defun eword-encode-text (charset encoding string &optional mode)
97 "Encode STRING as an encoded-word, and return the result.
98 CHARSET is a symbol to indicate MIME charset of the encoded-word.
99 ENCODING allows \"B\" or \"Q\".
100 MODE is allows `text', `comment', `phrase' or nil. Default value is
102 (let ((text (encoded-text-encode-string string encoding)))
104 (concat "=?" (upcase (symbol-name charset)) "?"
105 encoding "?" text "?=")
112 (defsubst eword-encode-char-type (character)
113 (if (memq character '(? ?\t ?\n))
115 (char-charset character)
118 (defun eword-encode-divide-into-charset-words (string)
119 (let ((len (length string))
122 (let* ((chr (sref string 0))
123 (charset (eword-encode-char-type chr))
124 (i (char-length chr)))
125 (while (and (< i len)
126 (setq chr (sref string i))
127 (eq charset (eword-encode-char-type chr))
129 (setq i (char-next-index chr i))
131 (setq dest (cons (cons charset (substring string 0 i)) dest)
132 string (substring string i)
142 (defun eword-encode-charset-words-to-words (charset-words)
145 (let* ((charset-word (car charset-words))
146 (charset (car charset-word))
149 (let ((charsets (list charset))
150 (str (cdr charset-word))
153 (while (setq charset-words (cdr charset-words))
154 (setq charset-word (car charset-words)
155 charset (car charset-word))
159 (or (memq charset charsets)
160 (setq charsets (cons charset charsets))
162 (setq str (concat str (cdr charset-word)))
164 (setq dest (cons (cons charsets str) dest))
166 (setq dest (cons charset-word dest)
167 charset-words (cdr charset-words)
176 (defmacro make-ew-rword (text charset encoding type)
177 (` (list (, text)(, charset)(, encoding)(, type))))
178 (defmacro ew-rword-text (rword)
180 (defmacro ew-rword-charset (rword)
181 (` (car (cdr (, rword)))))
182 (defmacro ew-rword-encoding (rword)
183 (` (car (cdr (cdr (, rword))))))
184 (defmacro ew-rword-type (rword)
185 (` (car (cdr (cdr (cdr (, rword)))))))
187 (defun ew-find-charset-rule (charsets)
189 (let* ((charset (find-mime-charset-by-charsets charsets))
190 (encoding (cdr (or (assq charset eword-charset-encoding-alist)
192 (list charset encoding)
195 (defun tm-eword::words-to-ruled-words (wl &optional mode)
198 (let ((ret (ew-find-charset-rule (car word))))
199 (make-ew-rword (cdr word) (car ret)(nth 1 ret) mode)
203 (defun ew-space-process (seq)
204 (let (prev a ac b c cc)
209 (setq cc (ew-rword-charset c))
210 (if (and (null (ew-rword-charset b))
211 (not (eq (ew-rword-type b) 'special)))
214 (setq ac (ew-rword-charset a))
215 (if (and (ew-rword-encoding a)
216 (ew-rword-encoding c))
219 (cons (concat (car a)(car b)(car c))
227 (cons (concat (car a)(car b))
232 (setq prev (cons b prev))
234 (setq prev (cons b prev))
239 (defun eword-encode-split-string (str &optional mode)
241 (tm-eword::words-to-ruled-words
242 (eword-encode-charset-words-to-words
243 (eword-encode-divide-into-charset-words str))
250 (defun tm-eword::encoded-word-length (rword)
251 (let ((string (ew-rword-text rword))
252 (charset (ew-rword-charset rword))
253 (encoding (ew-rword-encoding rword))
256 (cond ((string-equal encoding "B")
257 (setq string (encode-mime-charset-string string charset))
258 (base64-encoded-length string)
260 ((string-equal encoding "Q")
261 (setq string (encode-mime-charset-string string charset))
262 (Q-encoded-text-length string (ew-rword-type rword))
265 (cons (+ 7 (length (symbol-name charset)) ret) string)
272 (defun ew-encode-rword-1 (column rwl &optional must-output)
273 (catch 'can-not-output
274 (let* ((rword (car rwl))
275 (ret (tm-eword::encoded-word-length rword))
278 (cond ((and (setq string (car rword))
279 (or (<= (setq len (+ (length string) column)) 76)
284 ((memq (aref string 0) '(? ?\t))
285 (setq string (concat "\n" string)
294 (throw 'can-not-output nil)
296 (cond ((and (setq len (car ret))
297 (<= (+ column len) 76)
301 (ew-rword-charset rword)
302 (ew-rword-encoding rword)
304 (ew-rword-type rword)
306 (setq len (+ (length string) column))
310 (setq string (car rword))
313 (while (and (< p len)
315 (setq np (char-next-index (sref string p) p))
316 (setq nstr (substring string 0 np))
317 (setq ret (tm-eword::encoded-word-length
318 (cons nstr (cdr rword))
320 (setq nstr (cdr ret))
321 (setq len (+ (car ret) column))
326 (if (string-equal str "")
330 (throw 'can-not-output nil))
331 (setq rwl (cons (cons (substring string p) (cdr rword))
335 (ew-rword-charset rword)
336 (ew-rword-encoding rword)
338 (ew-rword-type rword)))
339 (setq len (+ (length string) column))
343 (list string len rwl)
346 (defun eword-encode-rword-list (column rwl)
347 (let (ret dest str ew-f pew-f folded-points)
349 (setq ew-f (nth 2 (car rwl)))
351 (setq rwl (cons '(" ") rwl)
355 (if (null (setq ret (ew-encode-rword-1 column rwl)))
356 (let ((i (1- (length dest)))
361 (cond ((memq (setq c (aref dest i)) '(? ?\t))
362 (if (memq i folded-points)
364 (setq folded-points (cons i folded-points))
371 (setq s (substring dest i)
373 r-dest (concat (substring dest 0 i) "\n" s))
374 (when (setq ret (ew-encode-rword-1 r-column rwl))
379 (setq ret (ew-encode-rword-1 column rwl 'must-output))
382 (setq dest (concat dest str))
383 (setq column (nth 1 ret)
393 (defun eword-encode-phrase-to-rword-list (phrase)
394 (let (token type dest str)
396 (setq token (car phrase))
397 (setq type (car token))
398 (cond ((eq type 'quoted-string)
399 (setq str (concat "\"" (cdr token) "\""))
403 (let ((ret (ew-find-charset-rule
404 (find-non-ascii-charset-string str))))
406 str (car ret)(nth 1 ret) 'phrase)
413 '(("(" nil nil special))
414 (tm-eword::words-to-ruled-words
415 (eword-encode-charset-words-to-words
416 (eword-encode-divide-into-charset-words
419 '((")" nil nil special))
425 (tm-eword::words-to-ruled-words
426 (eword-encode-charset-words-to-words
427 (eword-encode-divide-into-charset-words
431 (setq phrase (cdr phrase))
433 (ew-space-process dest)
436 (defun eword-encode-addr-seq-to-rword-list (seq)
439 (let* ((token (car seq))
442 (cond ((eq name 'spaces)
443 (setq dest (nconc dest (list (list (cdr token) nil nil))))
449 (list (list "(" nil nil))
450 (eword-encode-split-string (cdr token) 'comment)
451 (list (list ")" nil nil))
454 ((eq name 'quoted-string)
459 (list (concat "\"" (cdr token) "\"") nil nil)
464 (if (or (eq pname 'spaces)
466 (nconc dest (list (list (cdr token) nil nil)))
467 (nconc (butlast dest)
469 (list (concat (car (car (last dest)))
478 (defun eword-encode-phrase-route-addr-to-rword-list (phrase-route-addr)
479 (if (eq (car phrase-route-addr) 'phrase-route-addr)
480 (let ((phrase (nth 1 phrase-route-addr))
481 (route (nth 2 phrase-route-addr))
483 ;; (if (eq (car (car phrase)) 'spaces)
484 ;; (setq phrase (cdr phrase))
486 (setq dest (eword-encode-phrase-to-rword-list phrase))
488 (setq dest (append dest '((" " nil nil))))
492 (eword-encode-addr-seq-to-rword-list
493 (append '((specials . "<"))
495 '((specials . ">"))))
498 (defun eword-encode-addr-spec-to-rword-list (addr-spec)
499 (if (eq (car addr-spec) 'addr-spec)
500 (eword-encode-addr-seq-to-rword-list (cdr addr-spec))
503 (defun eword-encode-mailbox-to-rword-list (mbox)
504 (let ((addr (nth 1 mbox))
505 (comment (nth 2 mbox))
507 (setq dest (or (eword-encode-phrase-route-addr-to-rword-list addr)
508 (eword-encode-addr-spec-to-rword-list addr)
515 (eword-encode-split-string comment 'comment)
516 (list '(")" nil nil))
520 (defsubst eword-encode-mailboxes-to-rword-list (mboxes)
521 (let ((dest (eword-encode-mailbox-to-rword-list (car mboxes))))
523 (while (setq mboxes (cdr mboxes))
526 (list '("," nil nil))
527 (eword-encode-mailbox-to-rword-list
531 (defsubst eword-encode-address-to-rword-list (address)
533 ((eq (car address) 'mailbox)
534 (eword-encode-mailbox-to-rword-list address))
535 ((eq (car address) 'group)
537 (eword-encode-phrase-to-rword-list (nth 1 address))
538 (list (list ":" nil nil))
539 (eword-encode-mailboxes-to-rword-list (nth 2 address))
540 (list (list ";" nil nil))))))
542 (defsubst eword-encode-addresses-to-rword-list (addresses)
543 (let ((dest (eword-encode-address-to-rword-list (car addresses))))
545 (while (setq addresses (cdr addresses))
548 (list '("," nil nil))
549 ;; (list '(" " nil nil))
550 (eword-encode-address-to-rword-list (car addresses))))))
553 (defsubst eword-encode-msg-id-to-rword-list (msg-id)
557 (caar (eword-encode-addr-seq-to-rword-list (cdr msg-id)))
561 (defsubst eword-encode-in-reply-to-to-rword-list (in-reply-to)
566 (let ((elt (car in-reply-to)))
567 (if (eq (car elt) 'phrase)
568 (eword-encode-phrase-to-rword-list (cdr elt))
569 (eword-encode-msg-id-to-rword-list elt)
571 (setq in-reply-to (cdr in-reply-to)))
575 ;;; @ application interfaces
578 (defcustom eword-encode-default-start-column 10
579 "Default start column if it is omitted."
583 (defun eword-encode-string (string &optional column mode)
584 "Encode STRING as encoded-words, and return the result.
585 Optional argument COLUMN is start-position of the field.
586 Optional argument MODE allows `text', `comment', `phrase' or nil.
587 Default value is `phrase'."
588 (car (eword-encode-rword-list
589 (or column eword-encode-default-start-column)
590 (eword-encode-split-string string mode))))
592 (defun eword-encode-address-list (string &optional column)
593 "Encode header field STRING as list of address, and return the result.
594 Optional argument COLUMN is start-position of the field."
595 (car (eword-encode-rword-list
596 (or column eword-encode-default-start-column)
597 (eword-encode-addresses-to-rword-list
598 (std11-parse-addresses-string string))
601 (defun eword-encode-in-reply-to (string &optional column)
602 "Encode header field STRING as In-Reply-To field, and return the result.
603 Optional argument COLUMN is start-position of the field."
604 (car (eword-encode-rword-list
606 (eword-encode-in-reply-to-to-rword-list
607 (std11-parse-msg-ids-string string)))))
609 (defun eword-encode-structured-field-body (string &optional column)
610 "Encode header field STRING as structured field, and return the result.
611 Optional argument COLUMN is start-position of the field."
612 (car (eword-encode-rword-list
613 (or column eword-encode-default-start-column)
614 (eword-encode-addr-seq-to-rword-list (std11-lexical-analyze string))
617 (defun eword-encode-unstructured-field-body (string &optional column)
618 "Encode header field STRING as unstructured field, and return the result.
619 Optional argument COLUMN is start-position of the field."
620 (car (eword-encode-rword-list
621 (or column eword-encode-default-start-column)
622 (eword-encode-split-string string 'text))))
624 (defun eword-encode-field-body (field-body field-name)
625 "Encode FIELD-BODY as FIELD-NAME, and return the result.
626 A lexical token includes non-ASCII character is encoded as MIME
627 encoded-word. ASCII token is not encoded."
628 (setq field-body (std11-unfold-string field-body))
629 (if (string= field-body "")
632 (if (symbolp field-name)
633 (setq start (1+ (length (symbol-name field-name))))
634 (setq start (1+ (length field-name))
635 field-name (intern (capitalize field-name))))
636 (cond ((memq field-name
639 Resent-Reply-To Resent-From
640 Resent-Sender To Resent-To
641 Cc Resent-Cc Bcc Resent-Bcc
643 (eword-encode-address-list field-body start)
645 ((eq field-name 'In-Reply-To)
646 (eword-encode-in-reply-to field-body start)
648 ((memq field-name '(Mime-Version User-Agent))
649 (eword-encode-structured-field-body field-body start)
652 (eword-encode-unstructured-field-body field-body start)
656 (defun eword-in-subject-p ()
657 (let ((str (std11-field-body "Subject")))
658 (if (and str (string-match eword-encoded-word-regexp str))
661 (defsubst eword-find-field-encoding-method (field-name)
662 (setq field-name (downcase field-name))
663 (let ((alist eword-field-encoding-method-alist))
666 (let* ((pair (car alist))
668 (if (and (stringp str)
669 (string= field-name (downcase str)))
670 (throw 'found (cdr pair))
672 (setq alist (cdr alist)))
673 (cdr (assq t eword-field-encoding-method-alist))
676 (defun eword-encode-header (&optional code-conversion)
677 "Encode header fields to network representation, such as MIME encoded-word.
679 It refer variable `eword-field-encoding-method-alist'."
683 (std11-narrow-to-header mail-header-separator)
684 (goto-char (point-min))
685 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
687 (while (re-search-forward std11-field-head-regexp nil t)
688 (setq bbeg (match-end 0)
689 field-name (buffer-substring (match-beginning 0) (1- bbeg))
690 end (std11-field-end))
691 (and (find-non-ascii-charset-region bbeg end)
692 (let ((method (eword-find-field-encoding-method
693 (downcase field-name))))
694 (cond ((eq method 'mime)
696 (buffer-substring-no-properties bbeg end)
698 (delete-region bbeg end)
699 (insert (eword-encode-field-body field-body
704 (or (mime-charset-to-coding-system
707 (encode-coding-region bbeg end cs)
717 (provide 'eword-encode)
719 ;;; eword-encode.el ends here