1 ;;; eword-encode.el --- RFC 2047 based encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998 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.
31 (require 'eword-decode)
37 (defgroup eword-encode nil
38 "Encoded-word encoding"
41 (defcustom eword-field-encoding-method-alist
42 '(("X-Nsubject" . iso-2022-jp-2)
47 "*Alist to specify field encoding method.
48 Its key is field-name, value is encoding method.
50 If method is `mime', this field will be encoded into MIME format.
52 If method is a MIME-charset, this field will be encoded as the charset
53 when it must be convert into network-code.
55 If method is `default-mime-charset', this field will be encoded as
56 variable `default-mime-charset' when it must be convert into
59 If method is nil, this field will not be encoded."
61 :type '(repeat (cons (choice :tag "Field"
63 (const :tag "Default" t))
65 (const :tag "MIME conversion" mime)
66 (symbol :tag "non-MIME conversion")
67 (const :tag "no-conversion" nil)))))
69 (defvar eword-charset-encoding-alist
87 (iso-2022-int-1 . "B")
91 ;;; @ encoded-text encoder
94 (defun eword-encode-text (charset encoding string &optional mode)
95 "Encode STRING as an encoded-word, and return the result.
96 CHARSET is a symbol to indicate MIME charset of the encoded-word.
97 ENCODING allows \"B\" or \"Q\".
98 MODE is allows `text', `comment', `phrase' or nil. Default value is
101 (cond ((string= encoding "B")
102 (base64-encode-string string))
103 ((string= encoding "Q")
104 (q-encoding-encode-string string mode))
108 (concat "=?" (upcase (symbol-name charset)) "?"
109 encoding "?" text "?=")
116 (defsubst eword-encode-char-type (character)
117 (if (or (eq character ? )(eq character ?\t))
119 (char-charset character)
122 (defun eword-encode-divide-into-charset-words (string)
123 (let ((len (length string))
126 (let* ((chr (sref string 0))
127 (charset (eword-encode-char-type chr))
128 (i (char-length chr)))
129 (while (and (< i len)
130 (setq chr (sref string i))
131 (eq charset (eword-encode-char-type chr))
133 (setq i (char-next-index chr i))
135 (setq dest (cons (cons charset (substring string 0 i)) dest)
136 string (substring string i)
146 (defun eword-encode-charset-words-to-words (charset-words)
149 (let* ((charset-word (car charset-words))
150 (charset (car charset-word))
153 (let ((charsets (list charset))
154 (str (cdr charset-word))
157 (while (setq charset-words (cdr charset-words))
158 (setq charset-word (car charset-words)
159 charset (car charset-word))
163 (or (memq charset charsets)
164 (setq charsets (cons charset charsets))
166 (setq str (concat str (cdr charset-word)))
168 (setq dest (cons (cons charsets str) dest))
170 (setq dest (cons charset-word dest)
171 charset-words (cdr charset-words)
180 (defmacro make-ew-rword (text charset encoding type)
181 (` (list (, text)(, charset)(, encoding)(, type))))
182 (defmacro ew-rword-text (rword)
184 (defmacro ew-rword-charset (rword)
185 (` (car (cdr (, rword)))))
186 (defmacro ew-rword-encoding (rword)
187 (` (car (cdr (cdr (, rword))))))
188 (defmacro ew-rword-type (rword)
189 (` (car (cdr (cdr (cdr (, rword)))))))
191 (defun tm-eword::find-charset-rule (charsets)
193 (let* ((charset (charsets-to-mime-charset charsets))
194 (encoding (cdr (assq charset eword-charset-encoding-alist)))
196 (list charset encoding)
199 (defun tm-eword::words-to-ruled-words (wl &optional mode)
202 (let ((ret (tm-eword::find-charset-rule (car word))))
203 (make-ew-rword (cdr word) (car ret)(nth 1 ret) mode)
207 (defun tm-eword::space-process (seq)
208 (let (prev a ac b c cc)
213 (setq cc (ew-rword-charset c))
214 (if (null (ew-rword-charset b))
217 (setq ac (ew-rword-charset a))
218 (if (and (ew-rword-encoding a)
219 (ew-rword-encoding c))
222 (cons (concat (car a)(car b)(car c))
230 (cons (concat (car a)(car b))
235 (setq prev (cons b prev))
237 (setq prev (cons b prev))
242 (defun eword-encode-split-string (str &optional mode)
243 (tm-eword::space-process
244 (tm-eword::words-to-ruled-words
245 (eword-encode-charset-words-to-words
246 (eword-encode-divide-into-charset-words str))
253 (defun tm-eword::encoded-word-length (rword)
254 (let ((string (ew-rword-text rword))
255 (charset (ew-rword-charset rword))
256 (encoding (ew-rword-encoding rword))
259 (cond ((string-equal encoding "B")
260 (setq string (encode-mime-charset-string string charset))
261 (base64-encoded-length string)
263 ((string-equal encoding "Q")
264 (setq string (encode-mime-charset-string string charset))
265 (q-encoding-encoded-length string
266 (ew-rword-type rword))
269 (cons (+ 7 (length (symbol-name charset)) ret) string)
276 (defun tm-eword::encode-string-1 (column rwl)
277 (let* ((rword (car rwl))
278 (ret (tm-eword::encoded-word-length rword))
281 (cond ((and (setq string (car rword))
282 (or (<= (setq len (+ (length string) column)) 76)
291 (cond ((and (setq len (car ret))
292 (<= (+ column len) 76)
296 (ew-rword-charset rword)
297 (ew-rword-encoding rword)
299 (ew-rword-type rword)
301 (setq len (+ (length string) column))
305 (setq string (car rword))
308 (while (and (< p len)
310 (setq np (char-next-index (sref string p) p))
311 (setq nstr (substring string 0 np))
312 (setq ret (tm-eword::encoded-word-length
313 (cons nstr (cdr rword))
315 (setq nstr (cdr ret))
316 (setq len (+ (car ret) column))
321 (if (string-equal str "")
324 (setq rwl (cons (cons (substring string p) (cdr rword))
328 (ew-rword-charset rword)
329 (ew-rword-encoding rword)
331 (ew-rword-type rword)))
332 (setq len (+ (length string) column))
336 (list string len rwl)
339 (defun eword-encode-rword-list (column rwl)
340 (let (ret dest ps special str ew-f pew-f)
342 (setq ew-f (nth 2 (car rwl)))
344 (setq rwl (cons '(" ") rwl)
348 (setq ret (tm-eword::encode-string-1 column rwl))
350 (if (eq (elt str 0) ?\n)
353 (setq dest (concat dest "\n ("))
354 (setq ret (tm-eword::encode-string-1 2 rwl))
357 (cond ((eq special ? )
358 (if (string= str "(")
360 (setq dest (concat dest " "))
366 (setq dest (concat dest " ("))
369 (setq dest (concat dest "("))
372 (cond ((string= str " ")
380 (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 (tm-eword::find-charset-rule
403 (find-non-ascii-charset-string str))))
405 str (car ret)(nth 1 ret) 'phrase)
413 (tm-eword::words-to-ruled-words
414 (eword-encode-charset-words-to-words
415 (eword-encode-divide-into-charset-words
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 (tm-eword::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)
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))
527 (eword-encode-mailbox-to-rword-list (car addresses))
533 ;;; @ application interfaces
536 (defcustom eword-encode-default-start-column 10
537 "Default start column if it is omitted."
541 (defun eword-encode-string (string &optional column mode)
542 "Encode STRING as encoded-words, and return the result.
543 Optional argument COLUMN is start-position of the field.
544 Optional argument MODE allows `text', `comment', `phrase' or nil.
545 Default value is `phrase'."
546 (car (eword-encode-rword-list
547 (or column eword-encode-default-start-column)
548 (eword-encode-split-string string mode))))
550 (defun eword-encode-address-list (string &optional column)
551 "Encode header field STRING as list of address, and return the result.
552 Optional argument COLUMN is start-position of the field."
553 (car (eword-encode-rword-list
554 (or column eword-encode-default-start-column)
555 (eword-encode-addresses-to-rword-list
556 (std11-parse-addresses-string string))
559 (defun eword-encode-structured-field-body (string &optional column)
560 "Encode header field STRING as structured field, and return the result.
561 Optional argument COLUMN is start-position of the field."
562 (car (eword-encode-rword-list
563 (or column eword-encode-default-start-column)
564 (eword-encode-addr-seq-to-rword-list (std11-lexical-analyze string))
567 (defun eword-encode-unstructured-field-body (string &optional column)
568 "Encode header field STRING as unstructured field, and return the result.
569 Optional argument COLUMN is start-position of the field."
570 (car (eword-encode-rword-list
571 (or column eword-encode-default-start-column)
572 (eword-encode-split-string string 'text))))
574 (defun eword-encode-field (string)
575 "Encode header field STRING, and return the result.
576 A lexical token includes non-ASCII character is encoded as MIME
577 encoded-word. ASCII token is not encoded."
578 (setq string (std11-unfold-string string))
579 (let ((ret (string-match std11-field-head-regexp string)))
581 (let ((field-name (substring string 0 (1- (match-end 0))))
582 (field-body (eliminate-top-spaces
583 (substring string (match-end 0))))
586 (cond ((string= field-body "") "")
587 ((memq (setq field-name-symbol
588 (intern (capitalize field-name)))
591 Resent-Reply-To Resent-From
592 Resent-Sender To Resent-To
593 Cc Resent-Cc Bcc Resent-Bcc
595 (eword-encode-address-list
596 field-body (+ (length field-name) 2))
598 ((memq field-name-symbol
600 Mime-Version User-Agent))
601 (eword-encode-structured-field-body
602 field-body (+ (length field-name) 2))
605 (eword-encode-unstructured-field-body
606 field-body (1+ (length field-name)))
609 (concat field-name ": " ret)
611 (eword-encode-string string 0)
614 (defun eword-in-subject-p ()
615 (let ((str (std11-field-body "Subject")))
616 (if (and str (string-match eword-encoded-word-regexp str))
619 (defsubst eword-find-field-encoding-method (field-name)
620 (setq field-name (downcase field-name))
621 (let ((alist eword-field-encoding-method-alist))
624 (let* ((pair (car alist))
626 (if (and (stringp str)
627 (string= field-name (downcase str)))
628 (throw 'found (cdr pair))
630 (setq alist (cdr alist)))
631 (cdr (assq t eword-field-encoding-method-alist))
634 (defun eword-encode-header (&optional code-conversion)
635 "Encode header fields to network representation, such as MIME encoded-word.
637 It refer variable `eword-field-encoding-method-alist'."
641 (std11-narrow-to-header mail-header-separator)
642 (goto-char (point-min))
643 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
645 (while (re-search-forward std11-field-head-regexp nil t)
646 (setq beg (match-beginning 0))
647 (setq field-name (buffer-substring beg (1- (match-end 0))))
648 (setq end (std11-field-end))
649 (and (find-non-ascii-charset-region beg end)
650 (let ((method (eword-find-field-encoding-method
651 (downcase field-name))))
652 (cond ((eq method 'mime)
654 (buffer-substring-no-properties beg end)
656 (delete-region beg end)
657 (insert (eword-encode-field field))
661 (or (mime-charset-to-coding-system
664 (encode-coding-region beg end cs)
674 (provide 'eword-encode)
676 ;;; eword-encode.el ends here