2 ;;; tm-ew-d.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,1994,1995 MORIOKA Tomohiko
8 ;;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
20 (defconst tm-ew-e/RCS-ID
21 "$Id: tm-ew-e.el,v 7.5 1995/10/24 00:18:39 morioka Exp $")
22 (defconst mime/eword-encoder-version (get-version-string tm-ew-e/RCS-ID))
28 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
30 (defvar mime/use-X-Nsubject nil)
33 ;;; @ encoded-text encoder
36 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
38 (cond ((string= encoding "B")
39 (base64-encode-string string))
40 ((string= encoding "Q")
41 (q-encoding-encode-string string mode))
45 (concat "=?" charset "?" encoding "?" text "?=")
52 (defun tm-eword::char-type (chr)
53 (if (or (= chr 32)(= chr ?\t))
55 (char-leading-char chr)
58 (defun tm-eword::parse-lc-word (str)
59 (let* ((rest (string-to-char-list str))
61 (lc (tm-eword::char-type chr))
65 (while (setq rest (cdr rest))
67 (if (not (eq lc (tm-eword::char-type chr)))
70 (setq p (+ p (char-bytes chr)))
72 (cons (cons lc (substring str 0 p)) (substring str p))
75 (defun tm-eword::split-to-lc-words (str)
77 (while (and (not (string= str ""))
78 (setq ret (tm-eword::parse-lc-word str))
80 (setq dest (cons (car ret) dest))
90 (defun tm-eword::parse-word (lcwl)
91 (let* ((lcw (car lcwl))
100 (while (setq lcwl (cdr lcwl))
101 (setq lcw (car lcwl))
106 (if (not (memq lc lcl))
107 (setq lcl (cons lc lcl))
109 (setq str (concat str (cdr lcw)))
111 (cons (cons lcl str) lcwl)
114 (defun tm-eword::lc-words-to-words (lcwl)
116 (while (setq ret (tm-eword::parse-word lcwl))
117 (setq dest (cons (car ret) dest))
118 (setq lcwl (cdr ret))
127 (defun mime/find-charset-rule (lcl)
129 (let ((ret (some-element
132 (subsetp lcl (car elt))
134 mime/lc-charset-rule-list)
138 mime/unknown-charset-rule)
143 (defun tm-eword::words-to-ruled-words (wl)
146 (cons (cdr word) (mime/find-charset-rule (car word)))
150 (defun tm-eword::space-process (seq)
151 (let (prev a ac b c cc)
161 (if (and (nth 2 a)(nth 2 c))
164 (cons (concat (car a)(car b)(car c))
172 (cons (concat (car a)(car b))
177 (setq prev (cons b prev))
179 (setq prev (cons b prev))
184 (defun tm-eword::split-string (str)
185 (tm-eword::space-process
186 (tm-eword::words-to-ruled-words
187 (tm-eword::lc-words-to-words
188 (tm-eword::split-to-lc-words str)
195 (defun base64-length (string)
196 (let ((l (length string)))
198 (if (= (mod l 3) 0) 0 1)
202 (defun q-encoding-length (string)
203 (let ((l 0)(i 0)(len (length string)) chr)
205 (setq chr (elt string i))
206 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
213 (defun tm-eword::encoded-word-length (rword)
214 (let ((charset (nth 1 rword))
215 (encoding (nth 2 rword))
219 (cond ((equal encoding "B")
221 (mime/convert-string-from-emacs string charset))
222 (base64-length string)
224 ((equal encoding "Q")
226 (mime/convert-string-from-emacs string charset))
227 (q-encoding-length string)
230 (cons (+ 7 (length charset) ret) string)
237 (defun tm-eword::encode-string-1 (column rwl &optional mode)
238 (let* ((rword (car rwl))
239 (ret (tm-eword::encoded-word-length rword))
242 (cond ((and (setq string (car rword))
243 (<= (setq len (+ (length string) column)) 76)
251 (cond ((and (setq len (car ret))
252 (<= (+ column len) 76)
255 (tm-eword::encode-encoded-text
256 (nth 1 rword) (nth 2 rword) (cdr ret)
258 (setq len (+ (length string) column))
262 (setq string (car rword))
263 (let* ((ls (reverse (string-to-char-list string)))
268 (setq p (- p (char-bytes (car ls))))
269 (setq str (substring string 0 p))
270 (setq ret (tm-eword::encoded-word-length
271 (cons str (cdr rword))
274 (setq len (+ (car ret) column))
281 (setq rwl (cons (cons (substring string p) (cdr rword))
284 (tm-eword::encode-encoded-text
285 (nth 1 rword) (nth 2 rword) str))
286 (setq len (+ (length string) column))
293 (list string len rwl)
296 (defun tm-eword::encode-rwl (column rwl &optional mode)
299 (setq ret (tm-eword::encode-string-1 column rwl mode))
300 (setq dest (concat dest (car ret))
307 (defun tm-eword::encode-string (column str &optional mode)
308 (tm-eword::encode-rwl column (tm-eword::split-string str) mode)
315 (defun tm-eword::phrase-to-rwl (phrase)
316 (let (token type dest str)
318 (setq token (car phrase))
319 (setq type (car token))
320 (cond ((eq type 'quoted-string)
321 (setq str (concat "\"" (cdr token) "\""))
325 (cons str (mime/find-charset-rule
326 (find-charset-string str)))
333 (tm-eword::words-to-ruled-words
334 (tm-eword::lc-words-to-words
335 (tm-eword::split-to-lc-words (cdr token))))
340 (setq dest (append dest
341 (tm-eword::words-to-ruled-words
342 (tm-eword::lc-words-to-words
343 (tm-eword::split-to-lc-words (cdr token))
346 (setq phrase (cdr phrase))
348 (tm-eword::space-process dest)
351 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
352 (if (eq (car phrase-route-addr) 'phrase-route-addr)
353 (let ((phrase (nth 1 phrase-route-addr))
354 (route (nth 2 phrase-route-addr))
356 (if (eq (car (car phrase)) 'spaces)
357 (setq phrase (cdr phrase))
359 (setq dest (tm-eword::phrase-to-rwl phrase))
361 (setq dest (append dest '((" " nil nil))))
365 (list (list (concat "<" (rfc822/addr-to-string route) ">") nil nil))
368 (defun tm-eword::addr-spec-to-rwl (addr-spec)
369 (if (eq (car addr-spec) 'addr-spec)
370 (list (list (rfc822/addr-to-string (cdr addr-spec)) nil nil))
373 (defun tm-eword::mailbox-to-rwl (mbox)
374 (let ((addr (nth 1 mbox))
375 (comment (nth 2 mbox))
377 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
378 (tm-eword::addr-spec-to-rwl addr)
385 (tm-eword::split-string comment)
390 (defun tm-eword::addresses-to-rwl (addresses)
391 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
393 (while (setq addresses (cdr addresses))
394 (setq dest (append dest
397 (tm-eword::mailbox-to-rwl (car addresses))
402 (defun tm-eword::encode-address-list (column str)
403 (tm-eword::encode-rwl
405 (tm-eword::addresses-to-rwl
406 (rfc822/parse-addresses
407 (rfc822/lexical-analyze str)))))
410 ;;; @ application interfaces
413 (defun mime/encode-field (str)
414 (setq str (rfc822/unfolding-string str))
415 (let ((ret (string-match rfc822/field-top-regexp str)))
417 (let ((field-name (substring str 0 (match-end 1)))
418 (field-body (eliminate-top-spaces
419 (substring str (match-end 0))))
421 (concat field-name ": "
422 (cond ((string= field-body "") "")
423 ((member (setq fname (downcase field-name))
424 '("reply-to" "from" "sender"
425 "resent-reply-to" "resent-from"
426 "resent-sender" "to" "resent-to"
428 "bcc" "resent-bcc" "dcc")
430 (car (tm-eword::encode-address-list
431 (+ (length field-name) 1) field-body))
435 (let ((r mime/no-encoding-header-fields) fn)
438 (if (string= (downcase fn) fname)
439 (throw 'tag field-body)
443 (car (tm-eword::encode-string
444 (+ (length field-name) 1) field-body))
450 (defun mime/exist-encoded-word-in-subject ()
451 (let ((str (rfc822/get-field-body "Subject")))
452 (if (and str (string-match mime/encoded-word-regexp str))
455 (defun mime/encode-message-header ()
459 (narrow-to-region (goto-char (point-min))
463 "^" (regexp-quote mail-header-separator) "$")
467 (goto-char (point-min))
469 (while (re-search-forward rfc822/field-top-regexp nil t)
470 (setq beg (match-beginning 0))
471 (setq end (rfc822/field-end))
472 (if (and (find-charset-region beg end)
475 (buffer-substring-no-properties beg end)
479 (delete-region beg end)
483 (if mime/use-X-Nsubject
484 (let ((str (mime/exist-encoded-word-in-subject)))
489 (mime-eword/decode-string (rfc822/unfolding-string str))
493 (defun mime-eword/encode-string (str &optional column mode)
494 (car (tm-eword::encode-rwl (or column 0)
495 (tm-eword::split-string str) mode))