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.24 $
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.24 1997-06-26 09:12:48 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)
51 "*Alist to specify field encoding method.
52 Its key is field-name, value is encoding method.
54 If method is `mime', this field will be encoded into MIME format.
56 If method is a MIME-charset, this field will be encoded as the charset
57 when it must be convert into network-code.
59 If method is `default-mime-charset', this field will be encoded as
60 variable `default-mime-charset' when it must be convert into
63 If method is nil, this field will not be encoded.")
65 (defvar eword-generate-X-Nsubject nil
66 "*If it is not nil, X-Nsubject field is generated
67 when Subject field is encoded by `eword-encode-header'.")
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))
130 (while (and (< i len)
131 (setq chr (sref string i))
132 (eq charset (eword-encode-char-type chr))
134 (setq i (+ i (char-bytes chr)))
136 (setq dest (cons (cons charset (substring string 0 i)) dest)
137 string (substring string i)
147 (defun eword-encode-charset-words-to-words (charset-words)
150 (let* ((charset-word (car charset-words))
151 (charset (car charset-word))
154 (let ((charsets (list charset))
155 (str (cdr charset-word))
158 (while (setq charset-words (cdr charset-words))
159 (setq charset-word (car charset-words)
160 charset (car charset-word))
164 (or (memq charset charsets)
165 (setq charsets (cons charset charsets))
167 (setq str (concat str (cdr charset-word)))
169 (setq dest (cons (cons charsets str) dest))
171 (setq dest (cons charset-word dest)
172 charset-words (cdr charset-words)
181 (defmacro tm-eword::make-rword (text charset encoding type)
182 (` (list (, text)(, charset)(, encoding)(, type))))
183 (defmacro tm-eword::rword-text (rword)
185 (defmacro tm-eword::rword-charset (rword)
186 (` (car (cdr (, rword)))))
187 (defmacro tm-eword::rword-encoding (rword)
188 (` (car (cdr (cdr (, rword))))))
189 (defmacro tm-eword::rword-type (rword)
190 (` (car (cdr (cdr (cdr (, rword)))))))
192 (defun tm-eword::find-charset-rule (charsets)
194 (let* ((charset (charsets-to-mime-charset charsets))
195 (encoding (cdr (assq charset eword-charset-encoding-alist)))
197 (list charset encoding)
200 (defun tm-eword::words-to-ruled-words (wl &optional mode)
203 (let ((ret (tm-eword::find-charset-rule (car word))))
204 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
208 (defun tm-eword::space-process (seq)
209 (let (prev a ac b c cc)
214 (setq cc (tm-eword::rword-charset c))
215 (if (null (tm-eword::rword-charset b))
218 (setq ac (tm-eword::rword-charset a))
219 (if (and (tm-eword::rword-encoding a)
220 (tm-eword::rword-encoding c))
223 (cons (concat (car a)(car b)(car c))
231 (cons (concat (car a)(car b))
236 (setq prev (cons b prev))
238 (setq prev (cons b prev))
243 (defun tm-eword::split-string (str &optional mode)
244 (tm-eword::space-process
245 (tm-eword::words-to-ruled-words
246 (eword-encode-charset-words-to-words
247 (eword-encode-divide-into-charset-words str))
254 (defun tm-eword::encoded-word-length (rword)
255 (let ((string (tm-eword::rword-text rword))
256 (charset (tm-eword::rword-charset rword))
257 (encoding (tm-eword::rword-encoding rword))
260 (cond ((string-equal encoding "B")
261 (setq string (encode-mime-charset-string string charset))
262 (base64-encoded-length string)
264 ((string-equal encoding "Q")
265 (setq string (encode-mime-charset-string string charset))
266 (q-encoding-encoded-length string
267 (tm-eword::rword-type rword))
270 (cons (+ 7 (length (symbol-name charset)) ret) string)
277 (defun tm-eword::encode-string-1 (column rwl)
278 (let* ((rword (car rwl))
279 (ret (tm-eword::encoded-word-length rword))
282 (cond ((and (setq string (car rword))
283 (or (<= (setq len (+ (length string) column)) 76)
292 (cond ((and (setq len (car ret))
293 (<= (+ column len) 76)
297 (tm-eword::rword-charset rword)
298 (tm-eword::rword-encoding rword)
300 (tm-eword::rword-type rword)
302 (setq len (+ (length string) column))
306 (setq string (car rword))
309 (while (and (< p len)
311 (setq np (+ p (char-bytes (sref string p))))
312 (setq nstr (substring string 0 np))
313 (setq ret (tm-eword::encoded-word-length
314 (cons nstr (cdr rword))
316 (setq nstr (cdr ret))
317 (setq len (+ (car ret) column))
322 (if (string-equal str "")
325 (setq rwl (cons (cons (substring string p) (cdr rword))
329 (tm-eword::rword-charset rword)
330 (tm-eword::rword-encoding rword)
332 (tm-eword::rword-type rword)))
333 (setq len (+ (length string) column))
337 (list string len rwl)
340 (defun tm-eword::encode-rwl (column rwl)
341 (let (ret dest ps special str ew-f pew-f)
343 (setq ew-f (nth 2 (car rwl)))
345 (setq rwl (cons '(" ") rwl)
349 (setq ret (tm-eword::encode-string-1 column rwl))
351 (if (eq (elt str 0) ?\n)
354 (setq dest (concat dest "\n ("))
355 (setq ret (tm-eword::encode-string-1 2 rwl))
358 (cond ((eq special ? )
359 (if (string= str "(")
361 (setq dest (concat dest " "))
367 (setq dest (concat dest " ("))
370 (setq dest (concat dest "("))
373 (cond ((string= str " ")
381 (setq dest (concat dest str))
383 (setq column (nth 1 ret)
389 (defun tm-eword::encode-string (column str &optional mode)
390 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
397 (defun tm-eword::phrase-to-rwl (phrase)
398 (let (token type dest str)
400 (setq token (car phrase))
401 (setq type (car token))
402 (cond ((eq type 'quoted-string)
403 (setq str (concat "\"" (cdr token) "\""))
407 (let ((ret (tm-eword::find-charset-rule
408 (find-non-ascii-charset-string str))))
409 (tm-eword::make-rword
410 str (car ret)(nth 1 ret) 'phrase)
418 (tm-eword::words-to-ruled-words
419 (eword-encode-charset-words-to-words
420 (eword-encode-divide-into-charset-words
429 (tm-eword::words-to-ruled-words
430 (eword-encode-charset-words-to-words
431 (eword-encode-divide-into-charset-words
435 (setq phrase (cdr phrase))
437 (tm-eword::space-process dest)
440 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
441 (if (eq (car phrase-route-addr) 'phrase-route-addr)
442 (let ((phrase (nth 1 phrase-route-addr))
443 (route (nth 2 phrase-route-addr))
445 (if (eq (car (car phrase)) 'spaces)
446 (setq phrase (cdr phrase))
448 (setq dest (tm-eword::phrase-to-rwl phrase))
450 (setq dest (append dest '((" " nil nil))))
454 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
457 (defun tm-eword::addr-spec-to-rwl (addr-spec)
458 (if (eq (car addr-spec) 'addr-spec)
459 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
462 (defun tm-eword::mailbox-to-rwl (mbox)
463 (let ((addr (nth 1 mbox))
464 (comment (nth 2 mbox))
466 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
467 (tm-eword::addr-spec-to-rwl addr)
474 (tm-eword::split-string comment 'comment)
479 (defun tm-eword::addresses-to-rwl (addresses)
480 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
482 (while (setq addresses (cdr addresses))
483 (setq dest (append dest
486 (tm-eword::mailbox-to-rwl (car addresses))
491 (defun tm-eword::encode-address-list (column str)
492 (tm-eword::encode-rwl
494 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
498 ;;; @ application interfaces
501 (defun eword-encode-field (string)
502 "Encode header field STRING, and return the result.
503 A lexical token includes non-ASCII character is encoded as MIME
504 encoded-word. ASCII token is not encoded."
505 (setq string (std11-unfold-string string))
506 (let ((ret (string-match std11-field-head-regexp string)))
508 (let ((field-name (substring string 0 (1- (match-end 0))))
509 (field-body (eliminate-top-spaces
510 (substring string (match-end 0))))
513 (cond ((string-equal field-body "") "")
514 ((memq (intern (downcase field-name))
517 resent-reply-to resent-from
518 resent-sender to resent-to
522 (car (tm-eword::encode-address-list
523 (+ (length field-name) 2) field-body))
526 (car (tm-eword::encode-string
527 (1+ (length field-name))
531 (concat field-name ": " ret)
533 (car (tm-eword::encode-string 0 string))
536 (defun eword-in-subject-p ()
537 (let ((str (std11-field-body "Subject")))
538 (if (and str (string-match eword-encoded-word-regexp str))
541 (defun eword-encode-header (&optional code-conversion)
542 "Encode header fields to network representation, such as MIME encoded-word.
544 It refer variable `eword-field-encoding-method-alist'."
548 (std11-narrow-to-header mail-header-separator)
549 (goto-char (point-min))
550 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
552 (while (re-search-forward std11-field-head-regexp nil t)
553 (setq beg (match-beginning 0))
554 (setq field-name (buffer-substring beg (1- (match-end 0))))
555 (setq end (std11-field-end))
556 (and (find-non-ascii-charset-region beg end)
557 (let ((ret (or (let ((fname (downcase field-name)))
562 (string= fname (downcase str))
564 eword-field-encoding-method-alist))
565 (assq t eword-field-encoding-method-alist)
568 (let ((method (cdr ret)))
569 (cond ((eq method 'mime)
571 (buffer-substring-no-properties beg end)
573 (delete-region beg end)
574 (insert (eword-encode-field field))
578 (or (mime-charset-to-coding-system
581 (encode-coding-region beg end cs)
586 (and eword-generate-X-Nsubject
587 (or (std11-field-body "X-Nsubject")
588 (let ((str (eword-in-subject-p)))
593 (std11-unfold-string str)))
596 (encode-mime-charset-string
602 (string= "x-nsubject"
605 eword-field-encoding-method-alist))
608 (insert (concat "\nX-Nsubject: " str))
612 (defun eword-encode-string (str &optional column mode)
613 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
620 (provide 'eword-encode)
622 ;;; eword-encode.el ends here