2 ;;; tm-ew-e.el --- RFC 1522 based multilingual MIME message header
3 ;;; encoder for GNU Emacs
5 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
6 ;;; Copyright (C) 1993 .. 1996 MORIOKA Tomohiko
8 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Version: $Revision: 7.37 $
10 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
12 ;;; This file is part of tm (Tools for MIME).
14 ;;; This program is free software; you can redistribute it and/or
15 ;;; modify it under the terms of the GNU General Public License as
16 ;;; published by the Free Software Foundation; either version 2, or
17 ;;; (at your option) any later version.
19 ;;; This program is distributed in the hope that it will be useful,
20 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;;; General Public License for more details.
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with This program. If not, write to the Free Software
26 ;;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
38 (defconst tm-ew-e/RCS-ID
39 "$Id: tm-ew-e.el,v 7.37 1996/07/10 12:52:46 morioka Exp $")
40 (defconst mime-eword/encoder-version (get-version-string tm-ew-e/RCS-ID))
46 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
48 (defvar mime/use-X-Nsubject nil)
50 (defvar mime-eword/charset-encoding-alist
65 (iso-2022-int-1 . "B")
68 ;;; @ encoded-text encoder
71 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
73 (cond ((string= encoding "B")
74 (base64-encode-string string))
75 ((string= encoding "Q")
76 (q-encoding-encode-string string mode))
80 (concat "=?" (upcase (symbol-name charset)) "?"
81 encoding "?" text "?=")
88 (defun tm-eword::char-type (chr)
89 (if (or (= chr 32)(= chr ?\t))
91 (char-leading-char chr)
94 (defun tm-eword::parse-lc-word (str)
95 (let* ((chr (sref str 0))
96 (lc (tm-eword::char-type chr))
100 (while (and (< i len)
101 (setq chr (sref str i))
102 (eq lc (tm-eword::char-type chr))
104 (setq i (+ i (char-length chr)))
106 (cons (cons lc (substring str 0 i)) (substring str i))
109 (defun tm-eword::split-to-lc-words (str)
111 (while (and (not (string= str ""))
112 (setq ret (tm-eword::parse-lc-word str))
114 (setq dest (cons (car ret) dest))
124 (defun tm-eword::parse-word (lcwl)
125 (let* ((lcw (car lcwl))
130 (let ((lcl (list lc))
134 (while (setq lcwl (cdr lcwl))
135 (setq lcw (car lcwl))
140 (if (not (memq lc lcl))
141 (setq lcl (cons lc lcl))
143 (setq str (concat str (cdr lcw)))
145 (cons (cons lcl str) lcwl)
148 (defun tm-eword::lc-words-to-words (lcwl)
150 (while (setq ret (tm-eword::parse-word lcwl))
151 (setq dest (cons (car ret) dest))
152 (setq lcwl (cdr ret))
161 (defmacro tm-eword::make-rword (text charset encoding type)
162 (` (list (, text)(, charset)(, encoding)(, type))))
163 (defmacro tm-eword::rword-text (rword)
165 (defmacro tm-eword::rword-charset (rword)
166 (` (car (cdr (, rword)))))
167 (defmacro tm-eword::rword-encoding (rword)
168 (` (car (cdr (cdr (, rword))))))
169 (defmacro tm-eword::rword-type (rword)
170 (` (car (cdr (cdr (cdr (, rword)))))))
172 (defun tm-eword::find-charset-rule (charsets)
174 (let* ((charset (charsets-to-mime-charset charsets))
175 (encoding (cdr (assq charset mime-eword/charset-encoding-alist)))
177 (list charset encoding)
180 (defun tm-eword::words-to-ruled-words (wl &optional mode)
183 (let ((ret (tm-eword::find-charset-rule (car word))))
184 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
188 (defun tm-eword::space-process (seq)
189 (let (prev a ac b c cc)
194 (setq cc (tm-eword::rword-charset c))
195 (if (null (tm-eword::rword-charset b))
198 (setq ac (tm-eword::rword-charset a))
199 (if (and (tm-eword::rword-encoding a)
200 (tm-eword::rword-encoding c))
203 (cons (concat (car a)(car b)(car c))
211 (cons (concat (car a)(car b))
216 (setq prev (cons b prev))
218 (setq prev (cons b prev))
223 (defun tm-eword::split-string (str &optional mode)
224 (tm-eword::space-process
225 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
226 (tm-eword::split-to-lc-words str))
233 (defun tm-eword::encoded-word-length (rword)
234 (let ((string (tm-eword::rword-text rword))
235 (charset (tm-eword::rword-charset rword))
236 (encoding (tm-eword::rword-encoding rword))
239 (cond ((string-equal encoding "B")
240 (setq string (encode-mime-charset-string string charset))
241 (base64-encoded-length string)
243 ((string-equal encoding "Q")
244 (setq string (encode-mime-charset-string string charset))
245 (q-encoding-encoded-length string
246 (tm-eword::rword-type rword))
249 (cons (+ 7 (length (symbol-name charset)) ret) string)
256 (defun tm-eword::encode-string-1 (column rwl)
257 (let* ((rword (car rwl))
258 (ret (tm-eword::encoded-word-length rword))
261 (cond ((and (setq string (car rword))
262 (<= (setq len (+ (length string) column)) 76)
270 (cond ((and (setq len (car ret))
271 (<= (+ column len) 76)
274 (tm-eword::encode-encoded-text
275 (tm-eword::rword-charset rword)
276 (tm-eword::rword-encoding rword)
278 (tm-eword::rword-type rword)
280 (setq len (+ (length string) column))
284 (setq string (car rword))
285 (let* ((sl (length string))
288 (while (and (< p len)
290 (setq np (+ p (char-length (sref string p))))
291 (setq nstr (substring string 0 np))
292 (setq ret (tm-eword::encoded-word-length
293 (cons nstr (cdr rword))
295 (setq nstr (cdr ret))
296 (setq len (+ (car ret) column))
301 (if (string-equal str "")
304 (setq rwl (cons (cons (substring string p) (cdr rword))
307 (tm-eword::encode-encoded-text
308 (tm-eword::rword-charset rword)
309 (tm-eword::rword-encoding rword)
311 (tm-eword::rword-type rword)))
312 (setq len (+ (length string) column))
316 (list string len rwl)
319 (defun tm-eword::encode-rwl (column rwl)
320 (let (ret dest ps special str ew-f pew-f)
322 (setq ew-f (nth 2 (car rwl)))
324 (setq rwl (cons '(" ") rwl)
328 (setq ret (tm-eword::encode-string-1 column rwl))
330 (if (eq (elt str 0) ?\n)
333 (setq dest (concat dest "\n ("))
334 (setq ret (tm-eword::encode-string-1 2 rwl))
337 (cond ((eq special 32)
338 (if (string= str "(")
340 (setq dest (concat dest " "))
346 (setq dest (concat dest " ("))
349 (setq dest (concat dest "("))
352 (cond ((string= str " ")
360 (setq dest (concat dest str))
362 (setq column (nth 1 ret)
368 (defun tm-eword::encode-string (column str &optional mode)
369 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
376 (defun tm-eword::phrase-to-rwl (phrase)
377 (let (token type dest str)
379 (setq token (car phrase))
380 (setq type (car token))
381 (cond ((eq type 'quoted-string)
382 (setq str (concat "\"" (cdr token) "\""))
386 (let ((ret (tm-eword::find-charset-rule
387 (find-charset-string str))))
388 (tm-eword::make-rword
389 str (car ret)(nth 1 ret) 'phrase)
397 (tm-eword::words-to-ruled-words
398 (tm-eword::lc-words-to-words
399 (tm-eword::split-to-lc-words (cdr token)))
405 (setq dest (append dest
406 (tm-eword::words-to-ruled-words
407 (tm-eword::lc-words-to-words
408 (tm-eword::split-to-lc-words (cdr token))
411 (setq phrase (cdr phrase))
413 (tm-eword::space-process dest)
416 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
417 (if (eq (car phrase-route-addr) 'phrase-route-addr)
418 (let ((phrase (nth 1 phrase-route-addr))
419 (route (nth 2 phrase-route-addr))
421 (if (eq (car (car phrase)) 'spaces)
422 (setq phrase (cdr phrase))
424 (setq dest (tm-eword::phrase-to-rwl phrase))
426 (setq dest (append dest '((" " nil nil))))
430 (list (list (concat "<" (rfc822/addr-to-string route) ">") nil nil))
433 (defun tm-eword::addr-spec-to-rwl (addr-spec)
434 (if (eq (car addr-spec) 'addr-spec)
435 (list (list (rfc822/addr-to-string (cdr addr-spec)) nil nil))
438 (defun tm-eword::mailbox-to-rwl (mbox)
439 (let ((addr (nth 1 mbox))
440 (comment (nth 2 mbox))
442 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
443 (tm-eword::addr-spec-to-rwl addr)
450 (tm-eword::split-string comment 'comment)
455 (defun tm-eword::addresses-to-rwl (addresses)
456 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
458 (while (setq addresses (cdr addresses))
459 (setq dest (append dest
462 (tm-eword::mailbox-to-rwl (car addresses))
467 (defun tm-eword::encode-address-list (column str)
468 (tm-eword::encode-rwl
470 (tm-eword::addresses-to-rwl
471 (rfc822/parse-addresses
472 (rfc822/lexical-analyze str)))))
475 ;;; @ application interfaces
478 (defun mime/encode-field (str)
479 (setq str (rfc822/unfolding-string str))
480 (let ((ret (string-match rfc822/field-top-regexp str)))
482 (let ((field-name (substring str 0 (match-end 1)))
483 (field-body (eliminate-top-spaces
484 (substring str (match-end 0))))
487 (cond ((string= field-body "") "")
488 ((member (setq fname (downcase field-name))
489 '("reply-to" "from" "sender"
490 "resent-reply-to" "resent-from"
491 "resent-sender" "to" "resent-to"
493 "bcc" "resent-bcc" "dcc")
495 (car (tm-eword::encode-address-list
496 (+ (length field-name) 2) field-body))
500 (let ((r mime/no-encoding-header-fields)
504 (if (string= (downcase fn) fname)
505 (throw 'tag field-body)
509 (car (tm-eword::encode-string
510 (+ (length field-name) 1)
514 (concat field-name ": " ret)
516 (car (tm-eword::encode-string 0 str))
519 (defun mime/exist-encoded-word-in-subject ()
520 (let ((str (rfc822/get-field-body "Subject")))
521 (if (and str (string-match mime/encoded-word-regexp str))
524 (defun mime/encode-message-header ()
528 (narrow-to-region (goto-char (point-min))
529 (if (re-search-forward
531 "^" (regexp-quote mail-header-separator) "$")
535 (goto-char (point-min))
537 (while (re-search-forward rfc822/field-top-regexp nil t)
538 (setq beg (match-beginning 0))
539 (setq end (rfc822/field-end))
540 (if (and (find-charset-region beg end)
543 (buffer-substring-no-properties beg end)
547 (delete-region beg end)
551 (if mime/use-X-Nsubject
552 (let ((str (mime/exist-encoded-word-in-subject)))
557 (mime-eword/decode-string (rfc822/unfolding-string str))
561 (defun mime-eword/encode-string (str &optional column mode)
562 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
571 ;;; tm-ew-e.el ends here