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 SEMI (Spadework for Emacs MIME Interfaces).
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 (defvar eword-field-encoding-method-alist
38 '(("X-Nsubject" . iso-2022-jp-2)
43 "*Alist to specify field encoding method.
44 Its key is field-name, value is encoding method.
46 If method is `mime', this field will be encoded into MIME format.
48 If method is a MIME-charset, this field will be encoded as the charset
49 when it must be convert into network-code.
51 If method is `default-mime-charset', this field will be encoded as
52 variable `default-mime-charset' when it must be convert into
55 If method is nil, this field will not be encoded.")
57 (defvar eword-charset-encoding-alist
75 (iso-2022-int-1 . "B")
79 ;;; @ encoded-text encoder
82 (defun eword-encode-text (charset encoding string &optional mode)
83 "Encode STRING as an encoded-word, and return the result.
84 CHARSET is a symbol to indicate MIME charset of the encoded-word.
85 ENCODING allows \"B\" or \"Q\".
86 MODE is allows `text', `comment', `phrase' or nil. Default value is
89 (cond ((string= encoding "B")
90 (base64-encode-string string))
91 ((string= encoding "Q")
92 (q-encoding-encode-string string mode))
96 (concat "=?" (upcase (symbol-name charset)) "?"
97 encoding "?" text "?=")
104 (defsubst eword-encode-char-type (character)
105 (if (or (eq character ? )(eq character ?\t))
107 (char-charset character)
110 (defun eword-encode-divide-into-charset-words (string)
111 (let ((len (length string))
114 (let* ((chr (sref string 0))
115 (charset (eword-encode-char-type chr))
116 (i (char-length chr)))
117 (while (and (< i len)
118 (setq chr (sref string i))
119 (eq charset (eword-encode-char-type chr))
121 (setq i (char-next-index chr i))
123 (setq dest (cons (cons charset (substring string 0 i)) dest)
124 string (substring string i)
134 (defun eword-encode-charset-words-to-words (charset-words)
137 (let* ((charset-word (car charset-words))
138 (charset (car charset-word))
141 (let ((charsets (list charset))
142 (str (cdr charset-word))
145 (while (setq charset-words (cdr charset-words))
146 (setq charset-word (car charset-words)
147 charset (car charset-word))
151 (or (memq charset charsets)
152 (setq charsets (cons charset charsets))
154 (setq str (concat str (cdr charset-word)))
156 (setq dest (cons (cons charsets str) dest))
158 (setq dest (cons charset-word dest)
159 charset-words (cdr charset-words)
168 (defmacro tm-eword::make-rword (text charset encoding type)
169 (` (list (, text)(, charset)(, encoding)(, type))))
170 (defmacro tm-eword::rword-text (rword)
172 (defmacro tm-eword::rword-charset (rword)
173 (` (car (cdr (, rword)))))
174 (defmacro tm-eword::rword-encoding (rword)
175 (` (car (cdr (cdr (, rword))))))
176 (defmacro tm-eword::rword-type (rword)
177 (` (car (cdr (cdr (cdr (, rword)))))))
179 (defun tm-eword::find-charset-rule (charsets)
181 (let* ((charset (charsets-to-mime-charset charsets))
182 (encoding (cdr (assq charset eword-charset-encoding-alist)))
184 (list charset encoding)
187 (defun tm-eword::words-to-ruled-words (wl &optional mode)
190 (let ((ret (tm-eword::find-charset-rule (car word))))
191 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
195 (defun tm-eword::space-process (seq)
196 (let (prev a ac b c cc)
201 (setq cc (tm-eword::rword-charset c))
202 (if (null (tm-eword::rword-charset b))
205 (setq ac (tm-eword::rword-charset a))
206 (if (and (tm-eword::rword-encoding a)
207 (tm-eword::rword-encoding c))
210 (cons (concat (car a)(car b)(car c))
218 (cons (concat (car a)(car b))
223 (setq prev (cons b prev))
225 (setq prev (cons b prev))
230 (defun tm-eword::split-string (str &optional mode)
231 (tm-eword::space-process
232 (tm-eword::words-to-ruled-words
233 (eword-encode-charset-words-to-words
234 (eword-encode-divide-into-charset-words str))
241 (defun tm-eword::encoded-word-length (rword)
242 (let ((string (tm-eword::rword-text rword))
243 (charset (tm-eword::rword-charset rword))
244 (encoding (tm-eword::rword-encoding rword))
247 (cond ((string-equal encoding "B")
248 (setq string (encode-mime-charset-string string charset))
249 (base64-encoded-length string)
251 ((string-equal encoding "Q")
252 (setq string (encode-mime-charset-string string charset))
253 (q-encoding-encoded-length string
254 (tm-eword::rword-type rword))
257 (cons (+ 7 (length (symbol-name charset)) ret) string)
264 (defun tm-eword::encode-string-1 (column rwl)
265 (let* ((rword (car rwl))
266 (ret (tm-eword::encoded-word-length rword))
269 (cond ((and (setq string (car rword))
270 (or (<= (setq len (+ (length string) column)) 76)
279 (cond ((and (setq len (car ret))
280 (<= (+ column len) 76)
284 (tm-eword::rword-charset rword)
285 (tm-eword::rword-encoding rword)
287 (tm-eword::rword-type rword)
289 (setq len (+ (length string) column))
293 (setq string (car rword))
296 (while (and (< p len)
298 (setq np (char-next-index (sref string p) p))
299 (setq nstr (substring string 0 np))
300 (setq ret (tm-eword::encoded-word-length
301 (cons nstr (cdr rword))
303 (setq nstr (cdr ret))
304 (setq len (+ (car ret) column))
309 (if (string-equal str "")
312 (setq rwl (cons (cons (substring string p) (cdr rword))
316 (tm-eword::rword-charset rword)
317 (tm-eword::rword-encoding rword)
319 (tm-eword::rword-type rword)))
320 (setq len (+ (length string) column))
324 (list string len rwl)
327 (defun tm-eword::encode-rwl (column rwl)
328 (let (ret dest ps special str ew-f pew-f)
330 (setq ew-f (nth 2 (car rwl)))
332 (setq rwl (cons '(" ") rwl)
336 (setq ret (tm-eword::encode-string-1 column rwl))
338 (if (eq (elt str 0) ?\n)
341 (setq dest (concat dest "\n ("))
342 (setq ret (tm-eword::encode-string-1 2 rwl))
345 (cond ((eq special ? )
346 (if (string= str "(")
348 (setq dest (concat dest " "))
354 (setq dest (concat dest " ("))
357 (setq dest (concat dest "("))
360 (cond ((string= str " ")
368 (setq dest (concat dest str))
370 (setq column (nth 1 ret)
376 (defun tm-eword::encode-string (column str &optional mode)
377 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
384 (defun tm-eword::phrase-to-rwl (phrase)
385 (let (token type dest str)
387 (setq token (car phrase))
388 (setq type (car token))
389 (cond ((eq type 'quoted-string)
390 (setq str (concat "\"" (cdr token) "\""))
394 (let ((ret (tm-eword::find-charset-rule
395 (find-non-ascii-charset-string str))))
396 (tm-eword::make-rword
397 str (car ret)(nth 1 ret) 'phrase)
405 (tm-eword::words-to-ruled-words
406 (eword-encode-charset-words-to-words
407 (eword-encode-divide-into-charset-words
416 (tm-eword::words-to-ruled-words
417 (eword-encode-charset-words-to-words
418 (eword-encode-divide-into-charset-words
422 (setq phrase (cdr phrase))
424 (tm-eword::space-process dest)
427 (defun eword-addr-seq-to-rwl (seq)
430 (let* ((token (car seq))
433 (cond ((eq name 'spaces)
434 (setq dest (nconc dest (list (list (cdr token) nil nil))))
440 (list (list "(" nil nil))
441 (tm-eword::split-string (cdr token) 'comment)
442 (list (list ")" nil nil))
445 ((eq name 'quoted-string)
450 (list (concat "\"" (cdr token) "\"") nil nil)
455 (if (or (eq pname 'spaces)
457 (nconc dest (list (list (cdr token) nil nil)))
458 (nconc (butlast dest)
460 (list (concat (car (car (last dest)))
469 (defun eword-phrase-route-addr-to-rwl (phrase-route-addr)
470 (if (eq (car phrase-route-addr) 'phrase-route-addr)
471 (let ((phrase (nth 1 phrase-route-addr))
472 (route (nth 2 phrase-route-addr))
474 (if (eq (car (car phrase)) 'spaces)
475 (setq phrase (cdr phrase))
477 (setq dest (tm-eword::phrase-to-rwl phrase))
479 (setq dest (append dest '((" " nil nil))))
483 (eword-addr-seq-to-rwl
484 (append '((specials . "<"))
486 '((specials . ">"))))
489 (defun eword-addr-spec-to-rwl (addr-spec)
490 (if (eq (car addr-spec) 'addr-spec)
491 (eword-addr-seq-to-rwl (cdr addr-spec))
494 (defun tm-eword::mailbox-to-rwl (mbox)
495 (let ((addr (nth 1 mbox))
496 (comment (nth 2 mbox))
498 (setq dest (or (eword-phrase-route-addr-to-rwl addr)
499 (eword-addr-spec-to-rwl addr)
506 (tm-eword::split-string comment 'comment)
511 (defun tm-eword::addresses-to-rwl (addresses)
512 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
514 (while (setq addresses (cdr addresses))
515 (setq dest (append dest
518 (tm-eword::mailbox-to-rwl (car addresses))
523 (defun tm-eword::encode-address-list (column str)
524 (tm-eword::encode-rwl
526 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
530 ;;; @ application interfaces
533 (defun eword-encode-field (string)
534 "Encode header field STRING, and return the result.
535 A lexical token includes non-ASCII character is encoded as MIME
536 encoded-word. ASCII token is not encoded."
537 (setq string (std11-unfold-string string))
538 (let ((ret (string-match std11-field-head-regexp string)))
540 (let ((field-name (substring string 0 (1- (match-end 0))))
541 (field-body (eliminate-top-spaces
542 (substring string (match-end 0))))
545 (cond ((string-equal field-body "") "")
546 ((memq (intern (downcase field-name))
549 resent-reply-to resent-from
550 resent-sender to resent-to
555 (car (tm-eword::encode-address-list
556 (+ (length field-name) 2) field-body))
559 (car (tm-eword::encode-string
560 (1+ (length field-name))
564 (concat field-name ": " ret)
566 (car (tm-eword::encode-string 0 string))
569 (defun eword-in-subject-p ()
570 (let ((str (std11-field-body "Subject")))
571 (if (and str (string-match eword-encoded-word-regexp str))
574 (defsubst eword-find-field-encoding-method (field-name)
575 (setq field-name (downcase field-name))
576 (let ((alist eword-field-encoding-method-alist))
579 (let* ((pair (car alist))
581 (if (and (stringp str)
582 (string= field-name (downcase str)))
583 (throw 'found (cdr pair))
585 (setq alist (cdr alist)))
586 (cdr (assq t eword-field-encoding-method-alist))
589 (defun eword-encode-header (&optional code-conversion)
590 "Encode header fields to network representation, such as MIME encoded-word.
592 It refer variable `eword-field-encoding-method-alist'."
596 (std11-narrow-to-header mail-header-separator)
597 (goto-char (point-min))
598 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
600 (while (re-search-forward std11-field-head-regexp nil t)
601 (setq beg (match-beginning 0))
602 (setq field-name (buffer-substring beg (1- (match-end 0))))
603 (setq end (std11-field-end))
604 (and (find-non-ascii-charset-region beg end)
605 (let ((method (eword-find-field-encoding-method
606 (downcase field-name))))
607 (cond ((eq method 'mime)
609 (buffer-substring-no-properties beg end)
611 (delete-region beg end)
612 (insert (eword-encode-field field))
616 (or (mime-charset-to-coding-system
619 (encode-coding-region beg end cs)
625 (defun eword-encode-string (str &optional column mode)
626 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
633 (provide 'eword-encode)
635 ;;; eword-encode.el ends here