1 ;;; tm-ew-e.el --- RFC 1522 based MIME 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.47 $
7 ;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
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 this program; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
36 (defconst tm-ew-e/RCS-ID
37 "$Id: tm-ew-e.el,v 7.47 1996/08/30 04:26:46 morioka Exp $")
38 (defconst mime-eword/encoder-version (get-version-string tm-ew-e/RCS-ID))
44 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
46 (defvar mime/use-X-Nsubject nil)
48 (defvar mime-eword/charset-encoding-alist
63 (iso-2022-int-1 . "B")
66 ;;; @ encoded-text encoder
69 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
71 (cond ((string= encoding "B")
72 (base64-encode-string string))
73 ((string= encoding "Q")
74 (q-encoding-encode-string string mode))
78 (concat "=?" (upcase (symbol-name charset)) "?"
79 encoding "?" text "?=")
86 (defun tm-eword::char-type (chr)
87 (if (or (= chr 32)(= chr ?\t))
89 (char-leading-char chr)
92 (defun tm-eword::parse-lc-word (str)
93 (let* ((chr (sref str 0))
94 (lc (tm-eword::char-type chr))
99 (setq chr (sref str i))
100 (eq lc (tm-eword::char-type chr))
102 (setq i (+ i (char-length chr)))
104 (cons (cons lc (substring str 0 i)) (substring str i))
107 (defun tm-eword::split-to-lc-words (str)
109 (while (and (not (string= str ""))
110 (setq ret (tm-eword::parse-lc-word str))
112 (setq dest (cons (car ret) dest))
122 (defun tm-eword::parse-word (lcwl)
123 (let* ((lcw (car lcwl))
128 (let ((lcl (list lc))
132 (while (setq lcwl (cdr lcwl))
133 (setq lcw (car lcwl))
138 (if (not (memq lc lcl))
139 (setq lcl (cons lc lcl))
141 (setq str (concat str (cdr lcw)))
143 (cons (cons lcl str) lcwl)
146 (defun tm-eword::lc-words-to-words (lcwl)
148 (while (setq ret (tm-eword::parse-word lcwl))
149 (setq dest (cons (car ret) dest))
150 (setq lcwl (cdr ret))
159 (defmacro tm-eword::make-rword (text charset encoding type)
160 (` (list (, text)(, charset)(, encoding)(, type))))
161 (defmacro tm-eword::rword-text (rword)
163 (defmacro tm-eword::rword-charset (rword)
164 (` (car (cdr (, rword)))))
165 (defmacro tm-eword::rword-encoding (rword)
166 (` (car (cdr (cdr (, rword))))))
167 (defmacro tm-eword::rword-type (rword)
168 (` (car (cdr (cdr (cdr (, rword)))))))
170 (defun tm-eword::find-charset-rule (charsets)
172 (let* ((charset (charsets-to-mime-charset charsets))
173 (encoding (cdr (assq charset mime-eword/charset-encoding-alist)))
175 (list charset encoding)
178 (defun tm-eword::words-to-ruled-words (wl &optional mode)
181 (let ((ret (tm-eword::find-charset-rule (car word))))
182 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
186 (defun tm-eword::space-process (seq)
187 (let (prev a ac b c cc)
192 (setq cc (tm-eword::rword-charset c))
193 (if (null (tm-eword::rword-charset b))
196 (setq ac (tm-eword::rword-charset a))
197 (if (and (tm-eword::rword-encoding a)
198 (tm-eword::rword-encoding c))
201 (cons (concat (car a)(car b)(car c))
209 (cons (concat (car a)(car b))
214 (setq prev (cons b prev))
216 (setq prev (cons b prev))
221 (defun tm-eword::split-string (str &optional mode)
222 (tm-eword::space-process
223 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
224 (tm-eword::split-to-lc-words str))
231 (defun tm-eword::encoded-word-length (rword)
232 (let ((string (tm-eword::rword-text rword))
233 (charset (tm-eword::rword-charset rword))
234 (encoding (tm-eword::rword-encoding rword))
237 (cond ((string-equal encoding "B")
238 (setq string (encode-mime-charset-string string charset))
239 (base64-encoded-length string)
241 ((string-equal encoding "Q")
242 (setq string (encode-mime-charset-string string charset))
243 (q-encoding-encoded-length string
244 (tm-eword::rword-type rword))
247 (cons (+ 7 (length (symbol-name charset)) ret) string)
254 (defun tm-eword::encode-string-1 (column rwl)
255 (let* ((rword (car rwl))
256 (ret (tm-eword::encoded-word-length rword))
259 (cond ((and (setq string (car rword))
260 (<= (setq len (+ (length string) column)) 76)
268 (cond ((and (setq len (car ret))
269 (<= (+ column len) 76)
272 (tm-eword::encode-encoded-text
273 (tm-eword::rword-charset rword)
274 (tm-eword::rword-encoding rword)
276 (tm-eword::rword-type rword)
278 (setq len (+ (length string) column))
282 (setq string (car rword))
283 (let* ((sl (length string))
286 (while (and (< p len)
288 (setq np (+ p (char-length (sref string p))))
289 (setq nstr (substring string 0 np))
290 (setq ret (tm-eword::encoded-word-length
291 (cons nstr (cdr rword))
293 (setq nstr (cdr ret))
294 (setq len (+ (car ret) column))
299 (if (string-equal str "")
302 (setq rwl (cons (cons (substring string p) (cdr rword))
305 (tm-eword::encode-encoded-text
306 (tm-eword::rword-charset rword)
307 (tm-eword::rword-encoding rword)
309 (tm-eword::rword-type rword)))
310 (setq len (+ (length string) column))
314 (list string len rwl)
317 (defun tm-eword::encode-rwl (column rwl)
318 (let (ret dest ps special str ew-f pew-f)
320 (setq ew-f (nth 2 (car rwl)))
322 (setq rwl (cons '(" ") rwl)
326 (setq ret (tm-eword::encode-string-1 column rwl))
328 (if (eq (elt str 0) ?\n)
331 (setq dest (concat dest "\n ("))
332 (setq ret (tm-eword::encode-string-1 2 rwl))
335 (cond ((eq special 32)
336 (if (string= str "(")
338 (setq dest (concat dest " "))
344 (setq dest (concat dest " ("))
347 (setq dest (concat dest "("))
350 (cond ((string= str " ")
358 (setq dest (concat dest str))
360 (setq column (nth 1 ret)
366 (defun tm-eword::encode-string (column str &optional mode)
367 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
374 (defun tm-eword::phrase-to-rwl (phrase)
375 (let (token type dest str)
377 (setq token (car phrase))
378 (setq type (car token))
379 (cond ((eq type 'quoted-string)
380 (setq str (concat "\"" (cdr token) "\""))
384 (let ((ret (tm-eword::find-charset-rule
385 (find-charset-string str))))
386 (tm-eword::make-rword
387 str (car ret)(nth 1 ret) 'phrase)
395 (tm-eword::words-to-ruled-words
396 (tm-eword::lc-words-to-words
397 (tm-eword::split-to-lc-words (cdr token)))
403 (setq dest (append dest
404 (tm-eword::words-to-ruled-words
405 (tm-eword::lc-words-to-words
406 (tm-eword::split-to-lc-words (cdr token))
409 (setq phrase (cdr phrase))
411 (tm-eword::space-process dest)
414 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
415 (if (eq (car phrase-route-addr) 'phrase-route-addr)
416 (let ((phrase (nth 1 phrase-route-addr))
417 (route (nth 2 phrase-route-addr))
419 (if (eq (car (car phrase)) 'spaces)
420 (setq phrase (cdr phrase))
422 (setq dest (tm-eword::phrase-to-rwl phrase))
424 (setq dest (append dest '((" " nil nil))))
428 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
431 (defun tm-eword::addr-spec-to-rwl (addr-spec)
432 (if (eq (car addr-spec) 'addr-spec)
433 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
436 (defun tm-eword::mailbox-to-rwl (mbox)
437 (let ((addr (nth 1 mbox))
438 (comment (nth 2 mbox))
440 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
441 (tm-eword::addr-spec-to-rwl addr)
448 (tm-eword::split-string comment 'comment)
453 (defun tm-eword::addresses-to-rwl (addresses)
454 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
456 (while (setq addresses (cdr addresses))
457 (setq dest (append dest
460 (tm-eword::mailbox-to-rwl (car addresses))
465 (defun tm-eword::encode-address-list (column str)
466 (tm-eword::encode-rwl
468 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
472 ;;; @ application interfaces
475 (defun mime/encode-field (str)
476 (setq str (std11-unfold-string str))
477 (let ((ret (string-match std11-field-head-regexp str)))
479 (let ((field-name (substring str 0 (1- (match-end 0))))
480 (field-body (eliminate-top-spaces
481 (substring str (match-end 0))))
484 (cond ((string-equal field-body "") "")
485 ((member (setq fname (downcase field-name))
486 '("reply-to" "from" "sender"
487 "resent-reply-to" "resent-from"
488 "resent-sender" "to" "resent-to"
490 "bcc" "resent-bcc" "dcc")
492 (car (tm-eword::encode-address-list
493 (+ (length field-name) 2) field-body))
497 (let ((r mime/no-encoding-header-fields)
501 (if (string-equal (downcase fn) fname)
502 (throw 'tag field-body)
506 (car (tm-eword::encode-string
507 (+ (length field-name) 1)
511 (concat field-name ": " ret)
513 (car (tm-eword::encode-string 0 str))
516 (defun mime/exist-encoded-word-in-subject ()
517 (let ((str (std11-field-body "Subject")))
518 (if (and str (string-match mime/encoded-word-regexp str))
521 (defun mime/encode-message-header ()
525 (std11-narrow-to-header mail-header-separator)
526 (goto-char (point-min))
528 (while (re-search-forward std11-field-head-regexp nil t)
529 (setq beg (match-beginning 0))
530 (setq end (std11-field-end))
531 (if (and (find-charset-region beg end)
534 (buffer-substring-no-properties beg end)
538 (delete-region beg end)
542 (if mime/use-X-Nsubject
543 (let ((str (mime/exist-encoded-word-in-subject)))
548 (mime-eword/decode-string (std11-unfold-string str))
552 (defun mime-eword/encode-string (str &optional column mode)
553 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
562 ;;; tm-ew-e.el ends here