1 ;;; tm-ew-e.el --- RFC 2047 based encoded-word encoder for GNU Emacs
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Version: $Revision: 7.57 $
7 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
9 ;; This file is part of tm (Tools for MIME).
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.
37 (defconst tm-ew-e/RCS-ID
38 "$Id: tm-ew-e.el,v 7.57 1996/12/12 02:30:04 morioka Exp $")
39 (defconst mime-eword/encoder-version (get-version-string tm-ew-e/RCS-ID))
45 (defvar mime/field-encoding-method-alist
46 (if (boundp 'mime/no-encoding-header-fields)
50 (cons field-name 'default-mime-charset)
52 mime/no-encoding-header-fields)
55 '(("X-Nsubject" . iso-2022-jp-2)
59 "*Alist to specify field encoding method.
60 Its key is field-name, value is encoding method.
62 If method is `mime', this field will be encoded into MIME format.
64 If method is a MIME-charset, this field will be encoded as the charset
65 when it must be convert into network-code.
67 If method is `default-mime-charset', this field will be encoded as
68 variable `default-mime-charset' when it must be convert into
71 If method is nil, this field will not be encoded. [tm-ew-e.el]")
73 (defvar mime/generate-X-Nsubject
74 (and (boundp 'mime/use-X-Nsubject)
76 "*If it is not nil, X-Nsubject field is generated
77 when Subject field is encoded by `mime/encode-message-header'.
80 (defvar mime-eword/charset-encoding-alist
98 (iso-2022-int-1 . "B")
102 ;;; @ encoded-text encoder
105 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
107 (cond ((string= encoding "B")
108 (base64-encode-string string))
109 ((string= encoding "Q")
110 (q-encoding-encode-string string mode))
114 (concat "=?" (upcase (symbol-name charset)) "?"
115 encoding "?" text "?=")
122 (defun tm-eword::char-type (chr)
123 (if (or (= chr 32)(= chr ?\t))
128 (defun tm-eword::parse-lc-word (str)
129 (let* ((chr (sref str 0))
130 (lc (tm-eword::char-type chr))
131 (i (char-length chr))
134 (while (and (< i len)
135 (setq chr (sref str i))
136 (eq lc (tm-eword::char-type chr))
138 (setq i (+ i (char-length chr)))
140 (cons (cons lc (substring str 0 i)) (substring str i))
143 (defun tm-eword::split-to-lc-words (str)
145 (while (and (not (string= str ""))
146 (setq ret (tm-eword::parse-lc-word str))
148 (setq dest (cons (car ret) dest))
158 (defun tm-eword::parse-word (lcwl)
159 (let* ((lcw (car lcwl))
164 (let ((lcl (list lc))
168 (while (setq lcwl (cdr lcwl))
169 (setq lcw (car lcwl))
174 (if (not (memq lc lcl))
175 (setq lcl (cons lc lcl))
177 (setq str (concat str (cdr lcw)))
179 (cons (cons lcl str) lcwl)
182 (defun tm-eword::lc-words-to-words (lcwl)
184 (while (setq ret (tm-eword::parse-word lcwl))
185 (setq dest (cons (car ret) dest))
186 (setq lcwl (cdr ret))
195 (defmacro tm-eword::make-rword (text charset encoding type)
196 (` (list (, text)(, charset)(, encoding)(, type))))
197 (defmacro tm-eword::rword-text (rword)
199 (defmacro tm-eword::rword-charset (rword)
200 (` (car (cdr (, rword)))))
201 (defmacro tm-eword::rword-encoding (rword)
202 (` (car (cdr (cdr (, rword))))))
203 (defmacro tm-eword::rword-type (rword)
204 (` (car (cdr (cdr (cdr (, rword)))))))
206 (defun tm-eword::find-charset-rule (charsets)
208 (let* ((charset (charsets-to-mime-charset charsets))
209 (encoding (cdr (assq charset mime-eword/charset-encoding-alist)))
211 (list charset encoding)
214 (defun tm-eword::words-to-ruled-words (wl &optional mode)
217 (let ((ret (tm-eword::find-charset-rule (car word))))
218 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
222 (defun tm-eword::space-process (seq)
223 (let (prev a ac b c cc)
228 (setq cc (tm-eword::rword-charset c))
229 (if (null (tm-eword::rword-charset b))
232 (setq ac (tm-eword::rword-charset a))
233 (if (and (tm-eword::rword-encoding a)
234 (tm-eword::rword-encoding c))
237 (cons (concat (car a)(car b)(car c))
245 (cons (concat (car a)(car b))
250 (setq prev (cons b prev))
252 (setq prev (cons b prev))
257 (defun tm-eword::split-string (str &optional mode)
258 (tm-eword::space-process
259 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
260 (tm-eword::split-to-lc-words str))
267 (defun tm-eword::encoded-word-length (rword)
268 (let ((string (tm-eword::rword-text rword))
269 (charset (tm-eword::rword-charset rword))
270 (encoding (tm-eword::rword-encoding rword))
273 (cond ((string-equal encoding "B")
274 (setq string (encode-mime-charset-string string charset))
275 (base64-encoded-length string)
277 ((string-equal encoding "Q")
278 (setq string (encode-mime-charset-string string charset))
279 (q-encoding-encoded-length string
280 (tm-eword::rword-type rword))
283 (cons (+ 7 (length (symbol-name charset)) ret) string)
290 (defun tm-eword::encode-string-1 (column rwl)
291 (let* ((rword (car rwl))
292 (ret (tm-eword::encoded-word-length rword))
295 (cond ((and (setq string (car rword))
296 (<= (setq len (+ (length string) column)) 76)
304 (cond ((and (setq len (car ret))
305 (<= (+ column len) 76)
308 (tm-eword::encode-encoded-text
309 (tm-eword::rword-charset rword)
310 (tm-eword::rword-encoding rword)
312 (tm-eword::rword-type rword)
314 (setq len (+ (length string) column))
318 (setq string (car rword))
319 (let* ((sl (length string))
322 (while (and (< p len)
324 (setq np (+ p (char-length (sref string p))))
325 (setq nstr (substring string 0 np))
326 (setq ret (tm-eword::encoded-word-length
327 (cons nstr (cdr rword))
329 (setq nstr (cdr ret))
330 (setq len (+ (car ret) column))
335 (if (string-equal str "")
338 (setq rwl (cons (cons (substring string p) (cdr rword))
341 (tm-eword::encode-encoded-text
342 (tm-eword::rword-charset rword)
343 (tm-eword::rword-encoding rword)
345 (tm-eword::rword-type rword)))
346 (setq len (+ (length string) column))
350 (list string len rwl)
353 (defun tm-eword::encode-rwl (column rwl)
354 (let (ret dest ps special str ew-f pew-f)
356 (setq ew-f (nth 2 (car rwl)))
358 (setq rwl (cons '(" ") rwl)
362 (setq ret (tm-eword::encode-string-1 column rwl))
364 (if (eq (elt str 0) ?\n)
367 (setq dest (concat dest "\n ("))
368 (setq ret (tm-eword::encode-string-1 2 rwl))
371 (cond ((eq special 32)
372 (if (string= str "(")
374 (setq dest (concat dest " "))
380 (setq dest (concat dest " ("))
383 (setq dest (concat dest "("))
386 (cond ((string= str " ")
394 (setq dest (concat dest str))
396 (setq column (nth 1 ret)
402 (defun tm-eword::encode-string (column str &optional mode)
403 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
410 (defun tm-eword::phrase-to-rwl (phrase)
411 (let (token type dest str)
413 (setq token (car phrase))
414 (setq type (car token))
415 (cond ((eq type 'quoted-string)
416 (setq str (concat "\"" (cdr token) "\""))
420 (let ((ret (tm-eword::find-charset-rule
421 (find-non-ascii-charset-string str))))
422 (tm-eword::make-rword
423 str (car ret)(nth 1 ret) 'phrase)
431 (tm-eword::words-to-ruled-words
432 (tm-eword::lc-words-to-words
433 (tm-eword::split-to-lc-words (cdr token)))
439 (setq dest (append dest
440 (tm-eword::words-to-ruled-words
441 (tm-eword::lc-words-to-words
442 (tm-eword::split-to-lc-words (cdr token))
445 (setq phrase (cdr phrase))
447 (tm-eword::space-process dest)
450 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
451 (if (eq (car phrase-route-addr) 'phrase-route-addr)
452 (let ((phrase (nth 1 phrase-route-addr))
453 (route (nth 2 phrase-route-addr))
455 (if (eq (car (car phrase)) 'spaces)
456 (setq phrase (cdr phrase))
458 (setq dest (tm-eword::phrase-to-rwl phrase))
460 (setq dest (append dest '((" " nil nil))))
464 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
467 (defun tm-eword::addr-spec-to-rwl (addr-spec)
468 (if (eq (car addr-spec) 'addr-spec)
469 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
472 (defun tm-eword::mailbox-to-rwl (mbox)
473 (let ((addr (nth 1 mbox))
474 (comment (nth 2 mbox))
476 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
477 (tm-eword::addr-spec-to-rwl addr)
484 (tm-eword::split-string comment 'comment)
489 (defun tm-eword::addresses-to-rwl (addresses)
490 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
492 (while (setq addresses (cdr addresses))
493 (setq dest (append dest
496 (tm-eword::mailbox-to-rwl (car addresses))
501 (defun tm-eword::encode-address-list (column str)
502 (tm-eword::encode-rwl
504 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
508 ;;; @ application interfaces
511 (defun mime/encode-field (str)
512 (setq str (std11-unfold-string str))
513 (let ((ret (string-match std11-field-head-regexp str)))
515 (let ((field-name (substring str 0 (1- (match-end 0))))
516 (field-body (eliminate-top-spaces
517 (substring str (match-end 0))))
520 (cond ((string-equal field-body "") "")
521 ((member (setq fname (downcase field-name))
522 '("reply-to" "from" "sender"
523 "resent-reply-to" "resent-from"
524 "resent-sender" "to" "resent-to"
526 "bcc" "resent-bcc" "dcc")
528 (car (tm-eword::encode-address-list
529 (+ (length field-name) 2) field-body))
532 (car (tm-eword::encode-string
533 (+ (length field-name) 1)
537 (concat field-name ": " ret)
539 (car (tm-eword::encode-string 0 str))
542 (defun mime/exist-encoded-word-in-subject ()
543 (let ((str (std11-field-body "Subject")))
544 (if (and str (string-match mime/encoded-word-regexp str))
547 (defun mime/encode-message-header (&optional code-conversion)
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 (ASSOC (downcase field-name)
561 mime/field-encoding-method-alist
568 (assq t mime/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 (mime/encode-field field))
581 (or (mime-charset-to-coding-system
584 (encode-coding-region beg end cs)
589 (and mime/generate-X-Nsubject
590 (or (std11-field-body "X-Nsubject")
591 (let ((str (mime/exist-encoded-word-in-subject)))
595 (mime-eword/decode-string
596 (std11-unfold-string str)))
599 (encode-mime-charset-string
603 mime/field-encoding-method-alist
613 (insert (concat "\nX-Nsubject: " str))
617 (defun mime-eword/encode-string (str &optional column mode)
618 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
627 ;;; tm-ew-e.el ends here