1 ;;; eword-encode.el --- RFC 2047 based encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 ;; User options are defined in mime-def.el.
38 (defvar mime-header-charset-encoding-alist
60 (iso-2022-int-1 . "B")
64 (defvar mime-header-default-charset-encoding "Q")
66 (defvar mime-header-encode-method-alist
67 '((eword-encode-address-list
70 Resent-Reply-To Resent-From
71 Resent-Sender To Resent-To
72 Cc Resent-Cc Bcc Resent-Bcc
74 (eword-encode-in-reply-to . (In-Reply-To))
75 (eword-encode-structured-field-body . (Mime-Version User-Agent))
76 (eword-encode-unstructured-field-body)))
78 ;;; @ encoded-text encoder
81 (defun eword-encode-text (charset encoding string &optional mode)
82 "Encode STRING as an encoded-word, and return the result.
83 CHARSET is a symbol to indicate MIME charset of the encoded-word.
84 ENCODING allows \"B\" or \"Q\".
85 MODE is allows `text', `comment', `phrase' or nil. Default value is
87 (let ((text (encoded-text-encode-string string encoding mode)))
89 (concat "=?" (upcase (symbol-name charset)) "?"
90 encoding "?" text "?=")
97 (defsubst eword-encode-char-type (character)
98 (if (memq character '(? ?\t ?\n))
100 (char-charset character)
103 (defun eword-encode-divide-into-charset-words (string)
104 (let ((len (length string))
107 (let* ((chr (aref string 0))
108 ;; (chr (sref string 0))
109 (charset (eword-encode-char-type chr))
111 ;; (i (char-length chr))
113 (while (and (< i len)
114 (setq chr (aref string i))
115 ;; (setq chr (sref string i))
116 (eq charset (eword-encode-char-type chr)))
118 ;; (setq i (char-next-index chr i))
120 (setq dest (cons (cons charset (substring string 0 i)) dest)
121 string (substring string i)
129 (defun eword-encode-charset-words-to-words (charset-words)
132 (let* ((charset-word (car charset-words))
133 (charset (car charset-word))
136 (let ((charsets (list charset))
137 (str (cdr charset-word))
140 (while (setq charset-words (cdr charset-words))
141 (setq charset-word (car charset-words)
142 charset (car charset-word))
146 (or (memq charset charsets)
147 (setq charsets (cons charset charsets))
149 (setq str (concat str (cdr charset-word)))
151 (setq dest (cons (cons charsets str) dest))
153 (setq dest (cons charset-word dest)
154 charset-words (cdr charset-words)
163 (defmacro make-ew-rword (text charset encoding type)
164 (` (list (, text)(, charset)(, encoding)(, type))))
165 (defmacro ew-rword-text (rword)
167 (defmacro ew-rword-charset (rword)
168 (` (car (cdr (, rword)))))
169 (defmacro ew-rword-encoding (rword)
170 (` (car (cdr (cdr (, rword))))))
171 (defmacro ew-rword-type (rword)
172 (` (car (cdr (cdr (cdr (, rword)))))))
174 (defun ew-find-charset-rule (charsets)
176 (let* ((charset (find-mime-charset-by-charsets charsets))
178 (cdr (or (assq charset mime-header-charset-encoding-alist)
179 (cons charset mime-header-default-charset-encoding)))))
180 (list charset encoding))))
182 ;; [tomo:2002-11-05] The following code is a quick-fix for emacsen
183 ;; which is not depended on the Mule model. We should redesign
184 ;; `eword-encode-split-string' to avoid to depend on the Mule model.
185 (if (featurep 'utf-2000)
186 ;; for CHISE Architecture
187 (defun tm-eword::words-to-ruled-words (wl &optional mode)
191 (setq mcs (detect-mime-charset-string (cdr word)))
195 (cdr (or (assq mcs mime-header-charset-encoding-alist)
196 (cons mcs mime-header-default-charset-encoding)))
202 (defun tm-eword::words-to-ruled-words (wl &optional mode)
205 (let ((ret (ew-find-charset-rule (car word))))
206 (make-ew-rword (cdr word) (car ret)(nth 1 ret) mode)
211 (defun ew-space-process (seq)
212 (let (prev a ac b c cc)
217 (setq cc (ew-rword-charset c))
218 (if (and (null (ew-rword-charset b))
219 (not (eq (ew-rword-type b) 'special)))
222 (setq ac (ew-rword-charset a))
223 (if (and (ew-rword-encoding a)
224 (ew-rword-encoding c))
227 (cons (concat (car a)(car b)(car c))
235 (cons (concat (car a)(car b))
240 (setq prev (cons b prev))
242 (setq prev (cons b prev))
247 (defun eword-encode-split-string (str &optional mode)
249 (tm-eword::words-to-ruled-words
250 (eword-encode-charset-words-to-words
251 (eword-encode-divide-into-charset-words str))
258 (defun tm-eword::encoded-word-length (rword)
259 (let ((string (ew-rword-text rword))
260 (charset (ew-rword-charset rword))
261 (encoding (ew-rword-encoding rword))
264 (cond ((string-equal encoding "B")
265 (setq string (encode-mime-charset-string string charset))
266 (base64-encoded-length string)
268 ((string-equal encoding "Q")
269 (setq string (encode-mime-charset-string string charset))
270 (Q-encoded-text-length string (ew-rword-type rword))
273 (cons (+ 7 (length (symbol-name charset)) ret) string)
280 (defun ew-encode-rword-1 (column rwl &optional must-output)
281 (catch 'can-not-output
282 (let* ((rword (car rwl))
283 (ret (tm-eword::encoded-word-length rword))
286 (cond ((and (setq string (car rword))
287 (or (<= (setq len (+ (length string) column)) 76)
292 ((memq (aref string 0) '(? ?\t))
293 (setq string (concat "\n" string)
302 (throw 'can-not-output nil)
304 (cond ((and (setq len (car ret))
305 (<= (+ column len) 76)
309 (ew-rword-charset rword)
310 (ew-rword-encoding rword)
312 (ew-rword-type rword)
314 (setq len (+ (length string) column))
318 (setq string (car rword))
321 (while (and (< p len)
324 ;;(setq np (char-next-index (sref string p) p))
325 (setq nstr (substring string 0 np))
326 (setq ret (tm-eword::encoded-word-length
327 (cons nstr (cdr rword))
329 (setq nstr (cdr ret))
330 (setq len (+ (car ret) column))
335 (if (string-equal str "")
339 (throw 'can-not-output nil))
340 (setq rwl (cons (cons (substring string p) (cdr rword))
344 (ew-rword-charset rword)
345 (ew-rword-encoding rword)
347 (ew-rword-type rword)))
348 (setq len (+ (length string) column))
352 (list string len rwl)
355 (defun eword-encode-rword-list (column rwl)
356 (let (ret dest str ew-f pew-f folded-points)
358 (setq ew-f (nth 2 (car rwl)))
360 (setq rwl (cons '(" ") rwl)
364 (if (null (setq ret (ew-encode-rword-1 column rwl)))
365 (let ((i (1- (length dest)))
370 (cond ((memq (setq c (aref dest i)) '(? ?\t))
371 (if (memq i folded-points)
373 (setq folded-points (cons i folded-points))
380 (setq s (substring dest i)
382 r-dest (concat (substring dest 0 i) "\n" s))
383 (when (setq ret (ew-encode-rword-1 r-column rwl))
388 (setq ret (ew-encode-rword-1 column rwl 'must-output))
391 (setq dest (concat dest str))
392 (setq column (nth 1 ret)
402 (defun eword-encode-phrase-to-rword-list (phrase)
403 (let (token type dest str)
405 (setq token (car phrase))
406 (setq type (car token))
407 (cond ((eq type 'quoted-string)
408 (setq str (concat "\"" (cdr token) "\""))
412 (let ((ret (ew-find-charset-rule
413 (find-charset-string str))))
415 str (car ret)(nth 1 ret) 'phrase)
422 '(("(" nil nil special))
423 (tm-eword::words-to-ruled-words
424 (eword-encode-charset-words-to-words
425 (eword-encode-divide-into-charset-words
428 '((")" nil nil special))
434 (tm-eword::words-to-ruled-words
435 (eword-encode-charset-words-to-words
436 (eword-encode-divide-into-charset-words
440 (setq phrase (cdr phrase))
442 (ew-space-process dest)
445 (defun eword-encode-addr-seq-to-rword-list (seq)
448 (let* ((token (car seq))
451 (cond ((eq name 'spaces)
452 (setq dest (nconc dest (list (list (cdr token) nil nil))))
458 (list (list "(" nil nil))
459 (eword-encode-split-string (cdr token) 'comment)
460 (list (list ")" nil nil))
463 ((eq name 'quoted-string)
468 (list (concat "\"" (cdr token) "\"") nil nil)
473 (if (or (eq pname 'spaces)
475 (nconc dest (list (list (cdr token) nil nil)))
476 (nconc (nreverse (cdr (reverse dest)))
479 (list (concat (car (car (last dest)))
488 (defun eword-encode-phrase-route-addr-to-rword-list (phrase-route-addr)
489 (if (eq (car phrase-route-addr) 'phrase-route-addr)
490 (let ((phrase (nth 1 phrase-route-addr))
491 (route (nth 2 phrase-route-addr))
493 ;; (if (eq (car (car phrase)) 'spaces)
494 ;; (setq phrase (cdr phrase))
496 (setq dest (eword-encode-phrase-to-rword-list phrase))
498 (setq dest (append dest '((" " nil nil))))
502 (eword-encode-addr-seq-to-rword-list
503 (append '((specials . "<"))
505 '((specials . ">"))))
508 (defun eword-encode-addr-spec-to-rword-list (addr-spec)
509 (if (eq (car addr-spec) 'addr-spec)
510 (eword-encode-addr-seq-to-rword-list (cdr addr-spec))
513 (defun eword-encode-mailbox-to-rword-list (mbox)
514 (let ((addr (nth 1 mbox))
515 (comment (nth 2 mbox))
517 (setq dest (or (eword-encode-phrase-route-addr-to-rword-list addr)
518 (eword-encode-addr-spec-to-rword-list addr)
525 (eword-encode-split-string comment 'comment)
526 (list '(")" nil nil))
530 (defsubst eword-encode-mailboxes-to-rword-list (mboxes)
531 (let ((dest (eword-encode-mailbox-to-rword-list (car mboxes))))
533 (while (setq mboxes (cdr mboxes))
536 (list '("," nil nil))
537 (eword-encode-mailbox-to-rword-list
541 (defsubst eword-encode-address-to-rword-list (address)
543 ((eq (car address) 'mailbox)
544 (eword-encode-mailbox-to-rword-list address))
545 ((eq (car address) 'group)
547 (eword-encode-phrase-to-rword-list (nth 1 address))
548 (list (list ":" nil nil))
549 (eword-encode-mailboxes-to-rword-list (nth 2 address))
550 (list (list ";" nil nil))))))
552 (defsubst eword-encode-addresses-to-rword-list (addresses)
553 (let ((dest (eword-encode-address-to-rword-list (car addresses))))
555 (while (setq addresses (cdr addresses))
558 (list '("," nil nil))
559 ;; (list '(" " nil nil))
560 (eword-encode-address-to-rword-list (car addresses))))))
563 (defsubst eword-encode-msg-id-to-rword-list (msg-id)
567 (caar (eword-encode-addr-seq-to-rword-list (cdr msg-id)))
571 (defsubst eword-encode-in-reply-to-to-rword-list (in-reply-to)
576 (let ((elt (car in-reply-to)))
577 (if (eq (car elt) 'phrase)
578 (eword-encode-phrase-to-rword-list (cdr elt))
579 (eword-encode-msg-id-to-rword-list elt)
581 (setq in-reply-to (cdr in-reply-to)))
585 ;;; @ application interfaces
588 (defvar eword-encode-default-start-column 10
589 "Default start column if it is omitted.")
591 (defun eword-encode-string (string &optional column mode)
592 "Encode STRING as encoded-words, and return the result.
593 Optional argument COLUMN is start-position of the field.
594 Optional argument MODE allows `text', `comment', `phrase' or nil.
595 Default value is `phrase'."
596 (car (eword-encode-rword-list
597 (or column eword-encode-default-start-column)
598 (eword-encode-split-string string mode))))
600 (defun eword-encode-address-list (string &optional column)
601 "Encode header field STRING as list of address, and return the result.
602 Optional argument COLUMN is start-position of the field."
603 (car (eword-encode-rword-list
604 (or column eword-encode-default-start-column)
605 (eword-encode-addresses-to-rword-list
606 (std11-parse-addresses-string string))
609 (defun eword-encode-in-reply-to (string &optional column)
610 "Encode header field STRING as In-Reply-To field, and return the result.
611 Optional argument COLUMN is start-position of the field."
612 (car (eword-encode-rword-list
614 (eword-encode-in-reply-to-to-rword-list
615 (std11-parse-msg-ids-string string)))))
617 (defun eword-encode-structured-field-body (string &optional column)
618 "Encode header field STRING as structured 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-addr-seq-to-rword-list (std11-lexical-analyze string))
625 (defun eword-encode-unstructured-field-body (string &optional column)
626 "Encode header field STRING as unstructured field, and return the result.
627 Optional argument COLUMN is start-position of the field."
628 (car (eword-encode-rword-list
629 (or column eword-encode-default-start-column)
630 (eword-encode-split-string string 'text))))
633 (defun mime-encode-field-body (field-body field-name)
634 "Encode FIELD-BODY as FIELD-NAME, and return the result.
635 A lexical token includes non-ASCII character is encoded as MIME
636 encoded-word. ASCII token is not encoded."
637 (setq field-body (std11-unfold-string field-body))
638 (if (string= field-body "")
640 (let ((method-alist mime-header-encode-method-alist)
642 (if (symbolp field-name)
643 (setq start (1+ (length (symbol-name field-name))))
644 (setq start (1+ (length field-name))
645 field-name (intern (capitalize field-name))))
646 (while (car method-alist)
647 (if (or (not (cdr (car method-alist)))
649 (cdr (car method-alist))))
652 (apply (caar method-alist) (list field-body start)))
653 (setq method-alist nil)))
654 (setq method-alist (cdr method-alist)))
656 (defalias 'eword-encode-field-body 'mime-encode-field-body)
657 (make-obsolete 'eword-encode-field-body 'mime-encode-field-body)
659 (defun eword-in-subject-p ()
660 (let ((str (std11-field-body "Subject")))
661 (if (and str (string-match eword-encoded-word-regexp str))
663 (make-obsolete 'eword-in-subject-p "Don't use it.")
665 (defsubst eword-find-field-encoding-method (field-name)
666 (setq field-name (downcase field-name))
667 (let ((alist mime-field-encoding-method-alist))
670 (let* ((pair (car alist))
672 (if (and (stringp str)
673 (string= field-name (downcase str)))
674 (throw 'found (cdr pair))
676 (setq alist (cdr alist)))
677 (cdr (assq t mime-field-encoding-method-alist))
681 (defun mime-encode-header-in-buffer (&optional code-conversion)
682 "Encode header fields to network representation, such as MIME encoded-word.
684 It refer variable `mime-field-encoding-method-alist'."
688 (std11-narrow-to-header mail-header-separator)
689 (goto-char (point-min))
690 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
692 (while (re-search-forward
693 (concat "\\(" std11-field-head-regexp "\\)" " ?")
695 (setq bbeg (match-end 0)
696 field-name (buffer-substring (match-beginning 0) (1- (match-end 1)))
697 end (std11-field-end))
698 (and (delq 'ascii (find-charset-region bbeg end))
699 (let ((method (eword-find-field-encoding-method
700 (downcase field-name))))
701 (cond ((eq method 'mime)
703 (buffer-substring-no-properties bbeg end))
705 (mime-encode-field-body
706 field-body field-name)))
707 (if (not encoded-body)
708 (error "Cannot encode %s:%s"
709 field-name field-body)
710 (delete-region bbeg end)
711 (insert encoded-body))))
714 (or (mime-charset-to-coding-system
717 (encode-coding-region bbeg end cs)
722 (defalias 'eword-encode-header 'mime-encode-header-in-buffer)
723 (make-obsolete 'eword-encode-header 'mime-encode-header-in-buffer)
729 (provide 'eword-encode)
731 ;;; eword-encode.el ends here