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.14 $
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.
38 (defconst eword-encode-RCS-ID
39 "$Id: eword-encode.el,v 0.14 1997-03-06 21:23:52 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 tm-eword::encode-encoded-text (charset encoding string &optional mode)
96 (cond ((string= encoding "B")
97 (base64-encode-string string))
98 ((string= encoding "Q")
99 (q-encoding-encode-string string mode))
103 (concat "=?" (upcase (symbol-name charset)) "?"
104 encoding "?" text "?=")
111 (defun tm-eword::char-type (chr)
112 (if (or (= chr 32)(= chr ?\t))
117 (defun tm-eword::parse-lc-word (str)
118 (let* ((chr (sref str 0))
119 (lc (tm-eword::char-type chr))
123 (while (and (< i len)
124 (setq chr (sref str i))
125 (eq lc (tm-eword::char-type chr))
127 (setq i (+ i (char-bytes chr)))
129 (cons (cons lc (substring str 0 i)) (substring str i))
132 (defun tm-eword::split-to-lc-words (str)
134 (while (and (not (string= str ""))
135 (setq ret (tm-eword::parse-lc-word str))
137 (setq dest (cons (car ret) dest))
147 (defun tm-eword::parse-word (lcwl)
148 (let* ((lcw (car lcwl))
153 (let ((lcl (list lc))
157 (while (setq lcwl (cdr lcwl))
158 (setq lcw (car lcwl))
163 (if (not (memq lc lcl))
164 (setq lcl (cons lc lcl))
166 (setq str (concat str (cdr lcw)))
168 (cons (cons lcl str) lcwl)
171 (defun tm-eword::lc-words-to-words (lcwl)
173 (while (setq ret (tm-eword::parse-word lcwl))
174 (setq dest (cons (car ret) dest))
175 (setq lcwl (cdr ret))
184 (defmacro tm-eword::make-rword (text charset encoding type)
185 (` (list (, text)(, charset)(, encoding)(, type))))
186 (defmacro tm-eword::rword-text (rword)
188 (defmacro tm-eword::rword-charset (rword)
189 (` (car (cdr (, rword)))))
190 (defmacro tm-eword::rword-encoding (rword)
191 (` (car (cdr (cdr (, rword))))))
192 (defmacro tm-eword::rword-type (rword)
193 (` (car (cdr (cdr (cdr (, rword)))))))
195 (defun tm-eword::find-charset-rule (charsets)
197 (let* ((charset (charsets-to-mime-charset charsets))
198 (encoding (cdr (assq charset eword-charset-encoding-alist)))
200 (list charset encoding)
203 (defun tm-eword::words-to-ruled-words (wl &optional mode)
206 (let ((ret (tm-eword::find-charset-rule (car word))))
207 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
211 (defun tm-eword::space-process (seq)
212 (let (prev a ac b c cc)
217 (setq cc (tm-eword::rword-charset c))
218 (if (null (tm-eword::rword-charset b))
221 (setq ac (tm-eword::rword-charset a))
222 (if (and (tm-eword::rword-encoding a)
223 (tm-eword::rword-encoding c))
226 (cons (concat (car a)(car b)(car c))
234 (cons (concat (car a)(car b))
239 (setq prev (cons b prev))
241 (setq prev (cons b prev))
246 (defun tm-eword::split-string (str &optional mode)
247 (tm-eword::space-process
248 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
249 (tm-eword::split-to-lc-words str))
256 (defun tm-eword::encoded-word-length (rword)
257 (let ((string (tm-eword::rword-text rword))
258 (charset (tm-eword::rword-charset rword))
259 (encoding (tm-eword::rword-encoding rword))
262 (cond ((string-equal encoding "B")
263 (setq string (encode-mime-charset-string string charset))
264 (base64-encoded-length string)
266 ((string-equal encoding "Q")
267 (setq string (encode-mime-charset-string string charset))
268 (q-encoding-encoded-length string
269 (tm-eword::rword-type rword))
272 (cons (+ 7 (length (symbol-name charset)) ret) string)
279 (defun tm-eword::encode-string-1 (column rwl)
280 (let* ((rword (car rwl))
281 (ret (tm-eword::encoded-word-length rword))
284 (cond ((and (setq string (car rword))
285 (<= (setq len (+ (length string) column)) 76)
293 (cond ((and (setq len (car ret))
294 (<= (+ column len) 76)
297 (tm-eword::encode-encoded-text
298 (tm-eword::rword-charset rword)
299 (tm-eword::rword-encoding rword)
301 (tm-eword::rword-type rword)
303 (setq len (+ (length string) column))
307 (setq string (car rword))
310 (while (and (< p len)
312 (setq np (+ p (char-bytes (sref string p))))
313 (setq nstr (substring string 0 np))
314 (setq ret (tm-eword::encoded-word-length
315 (cons nstr (cdr rword))
317 (setq nstr (cdr ret))
318 (setq len (+ (car ret) column))
323 (if (string-equal str "")
326 (setq rwl (cons (cons (substring string p) (cdr rword))
329 (tm-eword::encode-encoded-text
330 (tm-eword::rword-charset rword)
331 (tm-eword::rword-encoding rword)
333 (tm-eword::rword-type rword)))
334 (setq len (+ (length string) column))
338 (list string len rwl)
341 (defun tm-eword::encode-rwl (column rwl)
342 (let (ret dest ps special str ew-f pew-f)
344 (setq ew-f (nth 2 (car rwl)))
346 (setq rwl (cons '(" ") rwl)
350 (setq ret (tm-eword::encode-string-1 column rwl))
352 (if (eq (elt str 0) ?\n)
355 (setq dest (concat dest "\n ("))
356 (setq ret (tm-eword::encode-string-1 2 rwl))
359 (cond ((eq special 32)
360 (if (string= str "(")
362 (setq dest (concat dest " "))
368 (setq dest (concat dest " ("))
371 (setq dest (concat dest "("))
374 (cond ((string= str " ")
382 (setq dest (concat dest str))
384 (setq column (nth 1 ret)
390 (defun tm-eword::encode-string (column str &optional mode)
391 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
398 (defun tm-eword::phrase-to-rwl (phrase)
399 (let (token type dest str)
401 (setq token (car phrase))
402 (setq type (car token))
403 (cond ((eq type 'quoted-string)
404 (setq str (concat "\"" (cdr token) "\""))
408 (let ((ret (tm-eword::find-charset-rule
409 (find-non-ascii-charset-string str))))
410 (tm-eword::make-rword
411 str (car ret)(nth 1 ret) 'phrase)
419 (tm-eword::words-to-ruled-words
420 (tm-eword::lc-words-to-words
421 (tm-eword::split-to-lc-words (cdr token)))
427 (setq dest (append dest
428 (tm-eword::words-to-ruled-words
429 (tm-eword::lc-words-to-words
430 (tm-eword::split-to-lc-words (cdr token))
433 (setq phrase (cdr phrase))
435 (tm-eword::space-process dest)
438 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
439 (if (eq (car phrase-route-addr) 'phrase-route-addr)
440 (let ((phrase (nth 1 phrase-route-addr))
441 (route (nth 2 phrase-route-addr))
443 (if (eq (car (car phrase)) 'spaces)
444 (setq phrase (cdr phrase))
446 (setq dest (tm-eword::phrase-to-rwl phrase))
448 (setq dest (append dest '((" " nil nil))))
452 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
455 (defun tm-eword::addr-spec-to-rwl (addr-spec)
456 (if (eq (car addr-spec) 'addr-spec)
457 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
460 (defun tm-eword::mailbox-to-rwl (mbox)
461 (let ((addr (nth 1 mbox))
462 (comment (nth 2 mbox))
464 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
465 (tm-eword::addr-spec-to-rwl addr)
472 (tm-eword::split-string comment 'comment)
477 (defun tm-eword::addresses-to-rwl (addresses)
478 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
480 (while (setq addresses (cdr addresses))
481 (setq dest (append dest
484 (tm-eword::mailbox-to-rwl (car addresses))
489 (defun tm-eword::encode-address-list (column str)
490 (tm-eword::encode-rwl
492 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
496 ;;; @ application interfaces
499 (defun eword-encode-field (str)
500 (setq str (std11-unfold-string str))
501 (let ((ret (string-match std11-field-head-regexp str)))
503 (let ((field-name (substring str 0 (1- (match-end 0))))
504 (field-body (eliminate-top-spaces
505 (substring str (match-end 0))))
508 (cond ((string-equal field-body "") "")
509 ((member (setq fname (downcase field-name))
510 '("reply-to" "from" "sender"
511 "resent-reply-to" "resent-from"
512 "resent-sender" "to" "resent-to"
514 "bcc" "resent-bcc" "dcc")
516 (car (tm-eword::encode-address-list
517 (+ (length field-name) 2) field-body))
520 (car (tm-eword::encode-string
521 (+ (length field-name) 1)
525 (concat field-name ": " ret)
527 (car (tm-eword::encode-string 0 str))
530 (defun eword-in-subject-p ()
531 (let ((str (std11-field-body "Subject")))
532 (if (and str (string-match eword-encoded-word-regexp str))
535 (defun eword-encode-header (&optional code-conversion)
536 "Encode header fields to network representation, such as MIME encoded-word.
538 It refer variable `eword-field-encoding-method-alist'."
542 (std11-narrow-to-header mail-header-separator)
543 (goto-char (point-min))
544 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
546 (while (re-search-forward std11-field-head-regexp nil t)
547 (setq beg (match-beginning 0))
548 (setq field-name (buffer-substring beg (1- (match-end 0))))
549 (setq end (std11-field-end))
550 (and (find-non-ascii-charset-region beg end)
551 (let ((ret (or (let ((fname (downcase field-name)))
556 (string= fname (downcase str))
558 eword-field-encoding-method-alist))
559 (assq t eword-field-encoding-method-alist)
562 (let ((method (cdr ret)))
563 (cond ((eq method 'mime)
565 (buffer-substring-no-properties beg end)
567 (delete-region beg end)
568 (insert (eword-encode-field field))
572 (or (mime-charset-to-coding-system
575 (encode-coding-region beg end cs)
580 (and eword-generate-X-Nsubject
581 (or (std11-field-body "X-Nsubject")
582 (let ((str (eword-in-subject-p)))
587 (std11-unfold-string str)))
590 (encode-mime-charset-string
596 (string= "x-nsubject"
599 eword-field-encoding-method-alist))
602 (insert (concat "\nX-Nsubject: " str))
606 (defun eword-encode-string (str &optional column mode)
607 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
614 (provide 'eword-encode)
616 ;;; eword-encode.el ends here