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.18 $
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.18 1997-06-21 09:00:09 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 (defun tm-eword::char-type (chr)
117 (if (or (= chr ? )(= chr ?\t))
122 (defun tm-eword::parse-lc-word (str)
123 (let* ((chr (sref str 0))
124 (lc (tm-eword::char-type chr))
128 (while (and (< i len)
129 (setq chr (sref str i))
130 (eq lc (tm-eword::char-type chr))
132 (setq i (+ i (char-bytes chr)))
134 (cons (cons lc (substring str 0 i)) (substring str i))
137 (defun tm-eword::split-to-lc-words (str)
139 (while (and (not (string= str ""))
140 (setq ret (tm-eword::parse-lc-word str))
142 (setq dest (cons (car ret) dest))
152 (defun tm-eword::parse-word (lcwl)
153 (let* ((lcw (car lcwl))
158 (let ((lcl (list lc))
162 (while (setq lcwl (cdr lcwl))
163 (setq lcw (car lcwl))
168 (if (not (memq lc lcl))
169 (setq lcl (cons lc lcl))
171 (setq str (concat str (cdr lcw)))
173 (cons (cons lcl str) lcwl)
176 (defun tm-eword::lc-words-to-words (lcwl)
178 (while (setq ret (tm-eword::parse-word lcwl))
179 (setq dest (cons (car ret) dest))
180 (setq lcwl (cdr ret))
189 (defmacro tm-eword::make-rword (text charset encoding type)
190 (` (list (, text)(, charset)(, encoding)(, type))))
191 (defmacro tm-eword::rword-text (rword)
193 (defmacro tm-eword::rword-charset (rword)
194 (` (car (cdr (, rword)))))
195 (defmacro tm-eword::rword-encoding (rword)
196 (` (car (cdr (cdr (, rword))))))
197 (defmacro tm-eword::rword-type (rword)
198 (` (car (cdr (cdr (cdr (, rword)))))))
200 (defun tm-eword::find-charset-rule (charsets)
202 (let* ((charset (charsets-to-mime-charset charsets))
203 (encoding (cdr (assq charset eword-charset-encoding-alist)))
205 (list charset encoding)
208 (defun tm-eword::words-to-ruled-words (wl &optional mode)
211 (let ((ret (tm-eword::find-charset-rule (car word))))
212 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
216 (defun tm-eword::space-process (seq)
217 (let (prev a ac b c cc)
222 (setq cc (tm-eword::rword-charset c))
223 (if (null (tm-eword::rword-charset b))
226 (setq ac (tm-eword::rword-charset a))
227 (if (and (tm-eword::rword-encoding a)
228 (tm-eword::rword-encoding c))
231 (cons (concat (car a)(car b)(car c))
239 (cons (concat (car a)(car b))
244 (setq prev (cons b prev))
246 (setq prev (cons b prev))
251 (defun tm-eword::split-string (str &optional mode)
252 (tm-eword::space-process
253 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
254 (tm-eword::split-to-lc-words str))
261 (defun tm-eword::encoded-word-length (rword)
262 (let ((string (tm-eword::rword-text rword))
263 (charset (tm-eword::rword-charset rword))
264 (encoding (tm-eword::rword-encoding rword))
267 (cond ((string-equal encoding "B")
268 (setq string (encode-mime-charset-string string charset))
269 (base64-encoded-length string)
271 ((string-equal encoding "Q")
272 (setq string (encode-mime-charset-string string charset))
273 (q-encoding-encoded-length string
274 (tm-eword::rword-type rword))
277 (cons (+ 7 (length (symbol-name charset)) ret) string)
284 (defun tm-eword::encode-string-1 (column rwl)
285 (let* ((rword (car rwl))
286 (ret (tm-eword::encoded-word-length rword))
289 (cond ((and (setq string (car rword))
290 (<= (setq len (+ (length string) column)) 76)
298 (cond ((and (setq len (car ret))
299 (<= (+ column len) 76)
303 (tm-eword::rword-charset rword)
304 (tm-eword::rword-encoding rword)
306 (tm-eword::rword-type rword)
308 (setq len (+ (length string) column))
312 (setq string (car rword))
315 (while (and (< p len)
317 (setq np (+ p (char-bytes (sref string p))))
318 (setq nstr (substring string 0 np))
319 (setq ret (tm-eword::encoded-word-length
320 (cons nstr (cdr rword))
322 (setq nstr (cdr ret))
323 (setq len (+ (car ret) column))
328 (if (string-equal str "")
331 (setq rwl (cons (cons (substring string p) (cdr rword))
335 (tm-eword::rword-charset rword)
336 (tm-eword::rword-encoding rword)
338 (tm-eword::rword-type rword)))
339 (setq len (+ (length string) column))
343 (list string len rwl)
346 (defun tm-eword::encode-rwl (column rwl)
347 (let (ret dest ps special str ew-f pew-f)
349 (setq ew-f (nth 2 (car rwl)))
351 (setq rwl (cons '(" ") rwl)
355 (setq ret (tm-eword::encode-string-1 column rwl))
357 (if (eq (elt str 0) ?\n)
360 (setq dest (concat dest "\n ("))
361 (setq ret (tm-eword::encode-string-1 2 rwl))
364 (cond ((eq special ? )
365 (if (string= str "(")
367 (setq dest (concat dest " "))
373 (setq dest (concat dest " ("))
376 (setq dest (concat dest "("))
379 (cond ((string= str " ")
387 (setq dest (concat dest str))
389 (setq column (nth 1 ret)
395 (defun tm-eword::encode-string (column str &optional mode)
396 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
403 (defun tm-eword::phrase-to-rwl (phrase)
404 (let (token type dest str)
406 (setq token (car phrase))
407 (setq type (car token))
408 (cond ((eq type 'quoted-string)
409 (setq str (concat "\"" (cdr token) "\""))
413 (let ((ret (tm-eword::find-charset-rule
414 (find-non-ascii-charset-string str))))
415 (tm-eword::make-rword
416 str (car ret)(nth 1 ret) 'phrase)
424 (tm-eword::words-to-ruled-words
425 (tm-eword::lc-words-to-words
426 (tm-eword::split-to-lc-words (cdr token)))
432 (setq dest (append dest
433 (tm-eword::words-to-ruled-words
434 (tm-eword::lc-words-to-words
435 (tm-eword::split-to-lc-words (cdr token))
438 (setq phrase (cdr phrase))
440 (tm-eword::space-process dest)
443 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
444 (if (eq (car phrase-route-addr) 'phrase-route-addr)
445 (let ((phrase (nth 1 phrase-route-addr))
446 (route (nth 2 phrase-route-addr))
448 (if (eq (car (car phrase)) 'spaces)
449 (setq phrase (cdr phrase))
451 (setq dest (tm-eword::phrase-to-rwl phrase))
453 (setq dest (append dest '((" " nil nil))))
457 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
460 (defun tm-eword::addr-spec-to-rwl (addr-spec)
461 (if (eq (car addr-spec) 'addr-spec)
462 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
465 (defun tm-eword::mailbox-to-rwl (mbox)
466 (let ((addr (nth 1 mbox))
467 (comment (nth 2 mbox))
469 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
470 (tm-eword::addr-spec-to-rwl addr)
477 (tm-eword::split-string comment 'comment)
482 (defun tm-eword::addresses-to-rwl (addresses)
483 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
485 (while (setq addresses (cdr addresses))
486 (setq dest (append dest
489 (tm-eword::mailbox-to-rwl (car addresses))
494 (defun tm-eword::encode-address-list (column str)
495 (tm-eword::encode-rwl
497 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
501 ;;; @ application interfaces
504 (defun eword-encode-field (string)
505 "Encode header field STRING, and return the result.
506 A lexical token includes non-ASCII character is encoded as MIME
507 encoded-word. ASCII token is not encoded."
508 (setq string (std11-unfold-string string))
509 (let ((ret (string-match std11-field-head-regexp string)))
511 (let ((field-name (substring string 0 (1- (match-end 0))))
512 (field-body (eliminate-top-spaces
513 (substring string (match-end 0))))
516 (cond ((string-equal field-body "") "")
517 ((memq (intern (downcase field-name))
520 resent-reply-to resent-from
521 resent-sender to resent-to
525 (car (tm-eword::encode-address-list
526 (+ (length field-name) 2) field-body))
529 (car (tm-eword::encode-string
530 (1+ (length field-name))
534 (concat field-name ": " ret)
536 (car (tm-eword::encode-string 0 string))
539 (defun eword-in-subject-p ()
540 (let ((str (std11-field-body "Subject")))
541 (if (and str (string-match eword-encoded-word-regexp str))
544 (defun eword-encode-header (&optional code-conversion)
545 "Encode header fields to network representation, such as MIME encoded-word.
547 It refer variable `eword-field-encoding-method-alist'."
551 (std11-narrow-to-header mail-header-separator)
552 (goto-char (point-min))
553 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
555 (while (re-search-forward std11-field-head-regexp nil t)
556 (setq beg (match-beginning 0))
557 (setq field-name (buffer-substring beg (1- (match-end 0))))
558 (setq end (std11-field-end))
559 (and (find-non-ascii-charset-region beg end)
560 (let ((ret (or (let ((fname (downcase field-name)))
565 (string= fname (downcase str))
567 eword-field-encoding-method-alist))
568 (assq t eword-field-encoding-method-alist)
571 (let ((method (cdr ret)))
572 (cond ((eq method 'mime)
574 (buffer-substring-no-properties beg end)
576 (delete-region beg end)
577 (insert (eword-encode-field field))
581 (or (mime-charset-to-coding-system
584 (encode-coding-region beg end cs)
589 (and eword-generate-X-Nsubject
590 (or (std11-field-body "X-Nsubject")
591 (let ((str (eword-in-subject-p)))
596 (std11-unfold-string str)))
599 (encode-mime-charset-string
605 (string= "x-nsubject"
608 eword-field-encoding-method-alist))
611 (insert (concat "\nX-Nsubject: " str))
615 (defun eword-encode-string (str &optional column mode)
616 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
623 (provide 'eword-encode)
625 ;;; eword-encode.el ends here