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 (defconst eword-encode-version "1.2")
43 (defvar eword-field-encoding-method-alist
44 '(("X-Nsubject" . iso-2022-jp-2)
49 "*Alist to specify field encoding method.
50 Its key is field-name, value is encoding method.
52 If method is `mime', this field will be encoded into MIME format.
54 If method is a MIME-charset, this field will be encoded as the charset
55 when it must be convert into network-code.
57 If method is `default-mime-charset', this field will be encoded as
58 variable `default-mime-charset' when it must be convert into
61 If method is nil, this field will not be encoded.")
63 (defvar eword-charset-encoding-alist
81 (iso-2022-int-1 . "B")
85 ;;; @ encoded-text encoder
88 (defun eword-encode-text (charset encoding string &optional mode)
89 "Encode STRING as an encoded-word, and return the result.
90 CHARSET is a symbol to indicate MIME charset of the encoded-word.
91 ENCODING allows \"B\" or \"Q\".
92 MODE is allows `text', `comment', `phrase' or nil. Default value is
95 (cond ((string= encoding "B")
96 (base64-encode-string string))
97 ((string= encoding "Q")
98 (q-encoding-encode-string string mode))
102 (concat "=?" (upcase (symbol-name charset)) "?"
103 encoding "?" text "?=")
110 (defsubst eword-encode-char-type (character)
111 (if (or (eq character ? )(eq character ?\t))
113 (char-charset character)
116 (defun eword-encode-divide-into-charset-words (string)
117 (let ((len (length string))
120 (let* ((chr (sref string 0))
121 (charset (eword-encode-char-type chr))
124 (while (and (< i len)
125 (setq chr (sref string i))
126 (eq charset (eword-encode-char-type chr))
128 (setq i (+ i (char-bytes chr)))
130 (setq dest (cons (cons charset (substring string 0 i)) dest)
131 string (substring string i)
141 (defun eword-encode-charset-words-to-words (charset-words)
144 (let* ((charset-word (car charset-words))
145 (charset (car charset-word))
148 (let ((charsets (list charset))
149 (str (cdr charset-word))
152 (while (setq charset-words (cdr charset-words))
153 (setq charset-word (car charset-words)
154 charset (car charset-word))
158 (or (memq charset charsets)
159 (setq charsets (cons charset charsets))
161 (setq str (concat str (cdr charset-word)))
163 (setq dest (cons (cons charsets str) dest))
165 (setq dest (cons charset-word dest)
166 charset-words (cdr charset-words)
175 (defmacro tm-eword::make-rword (text charset encoding type)
176 (` (list (, text)(, charset)(, encoding)(, type))))
177 (defmacro tm-eword::rword-text (rword)
179 (defmacro tm-eword::rword-charset (rword)
180 (` (car (cdr (, rword)))))
181 (defmacro tm-eword::rword-encoding (rword)
182 (` (car (cdr (cdr (, rword))))))
183 (defmacro tm-eword::rword-type (rword)
184 (` (car (cdr (cdr (cdr (, rword)))))))
186 (defun tm-eword::find-charset-rule (charsets)
188 (let* ((charset (charsets-to-mime-charset charsets))
189 (encoding (cdr (assq charset eword-charset-encoding-alist)))
191 (list charset encoding)
194 (defun tm-eword::words-to-ruled-words (wl &optional mode)
197 (let ((ret (tm-eword::find-charset-rule (car word))))
198 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
202 (defun tm-eword::space-process (seq)
203 (let (prev a ac b c cc)
208 (setq cc (tm-eword::rword-charset c))
209 (if (null (tm-eword::rword-charset b))
212 (setq ac (tm-eword::rword-charset a))
213 (if (and (tm-eword::rword-encoding a)
214 (tm-eword::rword-encoding c))
217 (cons (concat (car a)(car b)(car c))
225 (cons (concat (car a)(car b))
230 (setq prev (cons b prev))
232 (setq prev (cons b prev))
237 (defun tm-eword::split-string (str &optional mode)
238 (tm-eword::space-process
239 (tm-eword::words-to-ruled-words
240 (eword-encode-charset-words-to-words
241 (eword-encode-divide-into-charset-words str))
248 (defun tm-eword::encoded-word-length (rword)
249 (let ((string (tm-eword::rword-text rword))
250 (charset (tm-eword::rword-charset rword))
251 (encoding (tm-eword::rword-encoding rword))
254 (cond ((string-equal encoding "B")
255 (setq string (encode-mime-charset-string string charset))
256 (base64-encoded-length string)
258 ((string-equal encoding "Q")
259 (setq string (encode-mime-charset-string string charset))
260 (q-encoding-encoded-length string
261 (tm-eword::rword-type rword))
264 (cons (+ 7 (length (symbol-name charset)) ret) string)
271 (defun tm-eword::encode-string-1 (column rwl)
272 (let* ((rword (car rwl))
273 (ret (tm-eword::encoded-word-length rword))
276 (cond ((and (setq string (car rword))
277 (or (<= (setq len (+ (length string) column)) 76)
286 (cond ((and (setq len (car ret))
287 (<= (+ column len) 76)
291 (tm-eword::rword-charset rword)
292 (tm-eword::rword-encoding rword)
294 (tm-eword::rword-type rword)
296 (setq len (+ (length string) column))
300 (setq string (car rword))
303 (while (and (< p len)
305 (setq np (+ p (char-bytes (sref string p))))
306 (setq nstr (substring string 0 np))
307 (setq ret (tm-eword::encoded-word-length
308 (cons nstr (cdr rword))
310 (setq nstr (cdr ret))
311 (setq len (+ (car ret) column))
316 (if (string-equal str "")
319 (setq rwl (cons (cons (substring string p) (cdr rword))
323 (tm-eword::rword-charset rword)
324 (tm-eword::rword-encoding rword)
326 (tm-eword::rword-type rword)))
327 (setq len (+ (length string) column))
331 (list string len rwl)
334 (defun tm-eword::encode-rwl (column rwl)
335 (let (ret dest ps special str ew-f pew-f)
337 (setq ew-f (nth 2 (car rwl)))
339 (setq rwl (cons '(" ") rwl)
343 (setq ret (tm-eword::encode-string-1 column rwl))
345 (if (eq (elt str 0) ?\n)
348 (setq dest (concat dest "\n ("))
349 (setq ret (tm-eword::encode-string-1 2 rwl))
352 (cond ((eq special ? )
353 (if (string= str "(")
355 (setq dest (concat dest " "))
361 (setq dest (concat dest " ("))
364 (setq dest (concat dest "("))
367 (cond ((string= str " ")
375 (setq dest (concat dest str))
377 (setq column (nth 1 ret)
383 (defun tm-eword::encode-string (column str &optional mode)
384 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
391 (defun tm-eword::phrase-to-rwl (phrase)
392 (let (token type dest str)
394 (setq token (car phrase))
395 (setq type (car token))
396 (cond ((eq type 'quoted-string)
397 (setq str (concat "\"" (cdr token) "\""))
401 (let ((ret (tm-eword::find-charset-rule
402 (find-non-ascii-charset-string str))))
403 (tm-eword::make-rword
404 str (car ret)(nth 1 ret) 'phrase)
412 (tm-eword::words-to-ruled-words
413 (eword-encode-charset-words-to-words
414 (eword-encode-divide-into-charset-words
423 (tm-eword::words-to-ruled-words
424 (eword-encode-charset-words-to-words
425 (eword-encode-divide-into-charset-words
429 (setq phrase (cdr phrase))
431 (tm-eword::space-process dest)
434 (defun eword-addr-seq-to-rwl (seq)
437 (let* ((token (car seq))
440 (cond ((eq name 'spaces)
441 (setq dest (nconc dest (list (list (cdr token) nil nil))))
447 (list (list "(" nil nil))
448 (tm-eword::split-string (cdr token) 'comment)
449 (list (list ")" nil nil))
452 ((eq name 'quoted-string)
457 (list (concat "\"" (cdr token) "\"") nil nil)
462 (if (or (eq pname 'spaces)
464 (nconc dest (list (list (cdr token) nil nil)))
465 (nconc (butlast dest)
467 (list (concat (car (car (last dest)))
476 (defun eword-phrase-route-addr-to-rwl (phrase-route-addr)
477 (if (eq (car phrase-route-addr) 'phrase-route-addr)
478 (let ((phrase (nth 1 phrase-route-addr))
479 (route (nth 2 phrase-route-addr))
481 (if (eq (car (car phrase)) 'spaces)
482 (setq phrase (cdr phrase))
484 (setq dest (tm-eword::phrase-to-rwl phrase))
486 (setq dest (append dest '((" " nil nil))))
490 (eword-addr-seq-to-rwl
491 (append '((specials . "<"))
493 '((specials . ">"))))
496 (defun eword-addr-spec-to-rwl (addr-spec)
497 (if (eq (car addr-spec) 'addr-spec)
498 (eword-addr-seq-to-rwl (cdr addr-spec))
501 (defun tm-eword::mailbox-to-rwl (mbox)
502 (let ((addr (nth 1 mbox))
503 (comment (nth 2 mbox))
505 (setq dest (or (eword-phrase-route-addr-to-rwl addr)
506 (eword-addr-spec-to-rwl addr)
513 (tm-eword::split-string comment 'comment)
518 (defun tm-eword::addresses-to-rwl (addresses)
519 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
521 (while (setq addresses (cdr addresses))
522 (setq dest (append dest
525 (tm-eword::mailbox-to-rwl (car addresses))
530 (defun tm-eword::encode-address-list (column str)
531 (tm-eword::encode-rwl
533 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
537 ;;; @ application interfaces
540 (defun eword-encode-field (string)
541 "Encode header field STRING, and return the result.
542 A lexical token includes non-ASCII character is encoded as MIME
543 encoded-word. ASCII token is not encoded."
544 (setq string (std11-unfold-string string))
545 (let ((ret (string-match std11-field-head-regexp string)))
547 (let ((field-name (substring string 0 (1- (match-end 0))))
548 (field-body (eliminate-top-spaces
549 (substring string (match-end 0))))
552 (cond ((string-equal field-body "") "")
553 ((memq (intern (downcase field-name))
556 resent-reply-to resent-from
557 resent-sender to resent-to
562 (car (tm-eword::encode-address-list
563 (+ (length field-name) 2) field-body))
566 (car (tm-eword::encode-string
567 (1+ (length field-name))
571 (concat field-name ": " ret)
573 (car (tm-eword::encode-string 0 string))
576 (defun eword-in-subject-p ()
577 (let ((str (std11-field-body "Subject")))
578 (if (and str (string-match eword-encoded-word-regexp str))
581 (defsubst eword-find-field-encoding-method (field-name)
582 (setq field-name (downcase field-name))
583 (let ((alist eword-field-encoding-method-alist))
586 (let* ((pair (car alist))
588 (if (and (stringp str)
589 (string= field-name (downcase str)))
590 (throw 'found (cdr pair))
592 (setq alist (cdr alist)))
593 (cdr (assq t eword-field-encoding-method-alist))
596 (defun eword-encode-header (&optional code-conversion)
597 "Encode header fields to network representation, such as MIME encoded-word.
599 It refer variable `eword-field-encoding-method-alist'."
603 (std11-narrow-to-header mail-header-separator)
604 (goto-char (point-min))
605 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
607 (while (re-search-forward std11-field-head-regexp nil t)
608 (setq beg (match-beginning 0))
609 (setq field-name (buffer-substring beg (1- (match-end 0))))
610 (setq end (std11-field-end))
611 (and (find-non-ascii-charset-region beg end)
612 (let ((method (eword-find-field-encoding-method
613 (downcase field-name))))
614 (cond ((eq method 'mime)
616 (buffer-substring-no-properties beg end)
618 (delete-region beg end)
619 (insert (eword-encode-field field))
623 (or (mime-charset-to-coding-system
626 (encode-coding-region beg end cs)
632 (defun eword-encode-string (str &optional column mode)
633 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
640 (provide 'eword-encode)
642 ;;; eword-encode.el ends here