1 ;;; eword-encode.el --- RFC 2047 based encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Revision: 0.33 $
7 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
9 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
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 GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (require 'eword-decode)
38 (defconst eword-encode-RCS-ID
39 "$Id: eword-encode.el,v 0.33 1997-11-04 17:29:57 morioka Exp $")
40 (defconst eword-encode-version (get-version-string eword-encode-RCS-ID))
46 (defvar eword-field-encoding-method-alist
47 '(("X-Nsubject" . iso-2022-jp-2)
52 "*Alist to specify field encoding method.
53 Its key is field-name, value is encoding method.
55 If method is `mime', this field will be encoded into MIME format.
57 If method is a MIME-charset, this field will be encoded as the charset
58 when it must be convert into network-code.
60 If method is `default-mime-charset', this field will be encoded as
61 variable `default-mime-charset' when it must be convert into
64 If method is nil, this field will not be encoded.")
66 (defvar eword-charset-encoding-alist
84 (iso-2022-int-1 . "B")
88 ;;; @ encoded-text encoder
91 (defun eword-encode-text (charset encoding string &optional mode)
92 "Encode STRING as an encoded-word, and return the result.
93 CHARSET is a symbol to indicate MIME charset of the encoded-word.
94 ENCODING allows \"B\" or \"Q\".
95 MODE is allows `text', `comment', `phrase' or nil. Default value is
98 (cond ((string= encoding "B")
99 (base64-encode-string string))
100 ((string= encoding "Q")
101 (q-encoding-encode-string string mode))
105 (concat "=?" (upcase (symbol-name charset)) "?"
106 encoding "?" text "?=")
113 (defsubst eword-encode-char-type (character)
114 (if (or (eq character ? )(eq character ?\t))
116 (char-charset character)
119 (defun eword-encode-divide-into-charset-words (string)
120 (let ((len (length string))
123 (let* ((chr (sref string 0))
124 (charset (eword-encode-char-type chr))
127 (while (and (< i len)
128 (setq chr (sref string i))
129 (eq charset (eword-encode-char-type chr))
131 (setq i (+ i (char-bytes chr)))
133 (setq dest (cons (cons charset (substring string 0 i)) dest)
134 string (substring string i)
144 (defun eword-encode-charset-words-to-words (charset-words)
147 (let* ((charset-word (car charset-words))
148 (charset (car charset-word))
151 (let ((charsets (list charset))
152 (str (cdr charset-word))
155 (while (setq charset-words (cdr charset-words))
156 (setq charset-word (car charset-words)
157 charset (car charset-word))
161 (or (memq charset charsets)
162 (setq charsets (cons charset charsets))
164 (setq str (concat str (cdr charset-word)))
166 (setq dest (cons (cons charsets str) dest))
168 (setq dest (cons charset-word dest)
169 charset-words (cdr charset-words)
178 (defmacro tm-eword::make-rword (text charset encoding type)
179 (` (list (, text)(, charset)(, encoding)(, type))))
180 (defmacro tm-eword::rword-text (rword)
182 (defmacro tm-eword::rword-charset (rword)
183 (` (car (cdr (, rword)))))
184 (defmacro tm-eword::rword-encoding (rword)
185 (` (car (cdr (cdr (, rword))))))
186 (defmacro tm-eword::rword-type (rword)
187 (` (car (cdr (cdr (cdr (, rword)))))))
189 (defun tm-eword::find-charset-rule (charsets)
191 (let* ((charset (charsets-to-mime-charset charsets))
192 (encoding (cdr (assq charset eword-charset-encoding-alist)))
194 (list charset encoding)
197 (defun tm-eword::words-to-ruled-words (wl &optional mode)
200 (let ((ret (tm-eword::find-charset-rule (car word))))
201 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
205 (defun tm-eword::space-process (seq)
206 (let (prev a ac b c cc)
211 (setq cc (tm-eword::rword-charset c))
212 (if (null (tm-eword::rword-charset b))
215 (setq ac (tm-eword::rword-charset a))
216 (if (and (tm-eword::rword-encoding a)
217 (tm-eword::rword-encoding c))
220 (cons (concat (car a)(car b)(car c))
228 (cons (concat (car a)(car b))
233 (setq prev (cons b prev))
235 (setq prev (cons b prev))
240 (defun tm-eword::split-string (str &optional mode)
241 (tm-eword::space-process
242 (tm-eword::words-to-ruled-words
243 (eword-encode-charset-words-to-words
244 (eword-encode-divide-into-charset-words str))
251 (defun tm-eword::encoded-word-length (rword)
252 (let ((string (tm-eword::rword-text rword))
253 (charset (tm-eword::rword-charset rword))
254 (encoding (tm-eword::rword-encoding rword))
257 (cond ((string-equal encoding "B")
258 (setq string (encode-mime-charset-string string charset))
259 (base64-encoded-length string)
261 ((string-equal encoding "Q")
262 (setq string (encode-mime-charset-string string charset))
263 (q-encoding-encoded-length string
264 (tm-eword::rword-type rword))
267 (cons (+ 7 (length (symbol-name charset)) ret) string)
274 (defun tm-eword::encode-string-1 (column rwl)
275 (let* ((rword (car rwl))
276 (ret (tm-eword::encoded-word-length rword))
279 (cond ((and (setq string (car rword))
280 (or (<= (setq len (+ (length string) column)) 76)
289 (cond ((and (setq len (car ret))
290 (<= (+ column len) 76)
294 (tm-eword::rword-charset rword)
295 (tm-eword::rword-encoding rword)
297 (tm-eword::rword-type rword)
299 (setq len (+ (length string) column))
303 (setq string (car rword))
306 (while (and (< p len)
308 (setq np (+ p (char-bytes (sref string p))))
309 (setq nstr (substring string 0 np))
310 (setq ret (tm-eword::encoded-word-length
311 (cons nstr (cdr rword))
313 (setq nstr (cdr ret))
314 (setq len (+ (car ret) column))
319 (if (string-equal str "")
322 (setq rwl (cons (cons (substring string p) (cdr rword))
326 (tm-eword::rword-charset rword)
327 (tm-eword::rword-encoding rword)
329 (tm-eword::rword-type rword)))
330 (setq len (+ (length string) column))
334 (list string len rwl)
337 (defun tm-eword::encode-rwl (column rwl)
338 (let (ret dest ps special str ew-f pew-f)
340 (setq ew-f (nth 2 (car rwl)))
342 (setq rwl (cons '(" ") rwl)
346 (setq ret (tm-eword::encode-string-1 column rwl))
348 (if (eq (elt str 0) ?\n)
351 (setq dest (concat dest "\n ("))
352 (setq ret (tm-eword::encode-string-1 2 rwl))
355 (cond ((eq special ? )
356 (if (string= str "(")
358 (setq dest (concat dest " "))
364 (setq dest (concat dest " ("))
367 (setq dest (concat dest "("))
370 (cond ((string= str " ")
378 (setq dest (concat dest str))
380 (setq column (nth 1 ret)
386 (defun tm-eword::encode-string (column str &optional mode)
387 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
394 (defun tm-eword::phrase-to-rwl (phrase)
395 (let (token type dest str)
397 (setq token (car phrase))
398 (setq type (car token))
399 (cond ((eq type 'quoted-string)
400 (setq str (concat "\"" (cdr token) "\""))
404 (let ((ret (tm-eword::find-charset-rule
405 (find-non-ascii-charset-string str))))
406 (tm-eword::make-rword
407 str (car ret)(nth 1 ret) 'phrase)
415 (tm-eword::words-to-ruled-words
416 (eword-encode-charset-words-to-words
417 (eword-encode-divide-into-charset-words
426 (tm-eword::words-to-ruled-words
427 (eword-encode-charset-words-to-words
428 (eword-encode-divide-into-charset-words
432 (setq phrase (cdr phrase))
434 (tm-eword::space-process dest)
437 (defun eword-addr-seq-to-rwl (seq)
440 (let* ((token (car seq))
443 (cond ((eq name 'spaces)
444 (setq dest (nconc dest (list (list (cdr token) nil nil))))
450 (list (list "(" nil nil))
451 (tm-eword::split-string (cdr token) 'comment)
452 (list (list ")" nil nil))
455 ((eq name 'quoted-string)
460 (list (concat "\"" (cdr token) "\"") nil nil)
465 (if (or (eq pname 'spaces)
467 (nconc dest (list (list (cdr token) nil nil)))
468 (nconc (butlast dest)
470 (list (concat (car (car (last dest)))
479 (defun eword-phrase-route-addr-to-rwl (phrase-route-addr)
480 (if (eq (car phrase-route-addr) 'phrase-route-addr)
481 (let ((phrase (nth 1 phrase-route-addr))
482 (route (nth 2 phrase-route-addr))
484 (if (eq (car (car phrase)) 'spaces)
485 (setq phrase (cdr phrase))
487 (setq dest (tm-eword::phrase-to-rwl phrase))
489 (setq dest (append dest '((" " nil nil))))
493 (eword-addr-seq-to-rwl
494 (append '((specials . "<"))
496 '((specials . ">"))))
499 (defun eword-addr-spec-to-rwl (addr-spec)
500 (if (eq (car addr-spec) 'addr-spec)
501 (eword-addr-seq-to-rwl (cdr addr-spec))
504 (defun tm-eword::mailbox-to-rwl (mbox)
505 (let ((addr (nth 1 mbox))
506 (comment (nth 2 mbox))
508 (setq dest (or (eword-phrase-route-addr-to-rwl addr)
509 (eword-addr-spec-to-rwl addr)
516 (tm-eword::split-string comment 'comment)
521 (defun tm-eword::addresses-to-rwl (addresses)
522 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
524 (while (setq addresses (cdr addresses))
525 (setq dest (append dest
528 (tm-eword::mailbox-to-rwl (car addresses))
533 (defun tm-eword::encode-address-list (column str)
534 (tm-eword::encode-rwl
536 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
540 ;;; @ application interfaces
543 (defun eword-encode-field (string)
544 "Encode header field STRING, and return the result.
545 A lexical token includes non-ASCII character is encoded as MIME
546 encoded-word. ASCII token is not encoded."
547 (setq string (std11-unfold-string string))
548 (let ((ret (string-match std11-field-head-regexp string)))
550 (let ((field-name (substring string 0 (1- (match-end 0))))
551 (field-body (eliminate-top-spaces
552 (substring string (match-end 0))))
555 (cond ((string-equal field-body "") "")
556 ((memq (intern (downcase field-name))
559 resent-reply-to resent-from
560 resent-sender to resent-to
565 (car (tm-eword::encode-address-list
566 (+ (length field-name) 2) field-body))
569 (car (tm-eword::encode-string
570 (1+ (length field-name))
574 (concat field-name ": " ret)
576 (car (tm-eword::encode-string 0 string))
579 (defun eword-in-subject-p ()
580 (let ((str (std11-field-body "Subject")))
581 (if (and str (string-match eword-encoded-word-regexp str))
584 (defsubst eword-find-field-encoding-method (field-name)
585 (setq field-name (downcase field-name))
586 (let ((alist eword-field-encoding-method-alist))
589 (let* ((pair (car alist))
591 (if (and (stringp str)
592 (string= field-name (downcase str)))
593 (throw 'found (cdr pair))
595 (setq alist (cdr alist)))
596 (cdr (assq t eword-field-encoding-method-alist))
599 (defun eword-encode-header (&optional code-conversion)
600 "Encode header fields to network representation, such as MIME encoded-word.
602 It refer variable `eword-field-encoding-method-alist'."
606 (std11-narrow-to-header mail-header-separator)
607 (goto-char (point-min))
608 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
610 (while (re-search-forward std11-field-head-regexp nil t)
611 (setq beg (match-beginning 0))
612 (setq field-name (buffer-substring beg (1- (match-end 0))))
613 (setq end (std11-field-end))
614 (and (find-non-ascii-charset-region beg end)
615 (let ((method (eword-find-field-encoding-method
616 (downcase field-name))))
617 (cond ((eq method 'mime)
619 (buffer-substring-no-properties beg end)
621 (delete-region beg end)
622 (insert (eword-encode-field field))
626 (or (mime-charset-to-coding-system
629 (encode-coding-region beg end cs)
635 (defun eword-encode-string (str &optional column mode)
636 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
643 (provide 'eword-encode)
645 ;;; eword-encode.el ends here