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.10 $
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.
33 (defsubst find-non-ascii-charset-string (string)
34 "Return a list of charsets in the STRING except ascii."
35 (delq 'ascii (find-charset-string string))
42 (defconst eword-encode-RCS-ID
43 "$Id: eword-encode.el,v 0.10 1997-02-27 08:42:17 tmorioka Exp $")
44 (defconst eword-encode-version (get-version-string eword-encode-RCS-ID))
50 (defvar eword-field-encoding-method-alist
51 '(("X-Nsubject" . iso-2022-jp-2)
55 "*Alist to specify field encoding method.
56 Its key is field-name, value is encoding method.
58 If method is `mime', this field will be encoded into MIME format.
60 If method is a MIME-charset, this field will be encoded as the charset
61 when it must be convert into network-code.
63 If method is `default-mime-charset', this field will be encoded as
64 variable `default-mime-charset' when it must be convert into
67 If method is nil, this field will not be encoded.")
69 (defvar eword-generate-X-Nsubject nil
70 "*If it is not nil, X-Nsubject field is generated
71 when Subject field is encoded by `eword-encode-header'.")
73 (defvar eword-charset-encoding-alist
91 (iso-2022-int-1 . "B")
95 ;;; @ encoded-text encoder
98 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
100 (cond ((string= encoding "B")
101 (base64-encode-string string))
102 ((string= encoding "Q")
103 (q-encoding-encode-string string mode))
107 (concat "=?" (upcase (symbol-name charset)) "?"
108 encoding "?" text "?=")
115 (defun tm-eword::char-type (chr)
116 (if (or (= chr 32)(= chr ?\t))
121 (defun tm-eword::parse-lc-word (str)
122 (let* ((chr (sref str 0))
123 (lc (tm-eword::char-type chr))
124 (i (char-length chr))
127 (while (and (< i len)
128 (setq chr (sref str i))
129 (eq lc (tm-eword::char-type chr))
131 (setq i (+ i (char-length chr)))
133 (cons (cons lc (substring str 0 i)) (substring str i))
136 (defun tm-eword::split-to-lc-words (str)
138 (while (and (not (string= str ""))
139 (setq ret (tm-eword::parse-lc-word str))
141 (setq dest (cons (car ret) dest))
151 (defun tm-eword::parse-word (lcwl)
152 (let* ((lcw (car lcwl))
157 (let ((lcl (list lc))
161 (while (setq lcwl (cdr lcwl))
162 (setq lcw (car lcwl))
167 (if (not (memq lc lcl))
168 (setq lcl (cons lc lcl))
170 (setq str (concat str (cdr lcw)))
172 (cons (cons lcl str) lcwl)
175 (defun tm-eword::lc-words-to-words (lcwl)
177 (while (setq ret (tm-eword::parse-word lcwl))
178 (setq dest (cons (car ret) dest))
179 (setq lcwl (cdr ret))
188 (defmacro tm-eword::make-rword (text charset encoding type)
189 (` (list (, text)(, charset)(, encoding)(, type))))
190 (defmacro tm-eword::rword-text (rword)
192 (defmacro tm-eword::rword-charset (rword)
193 (` (car (cdr (, rword)))))
194 (defmacro tm-eword::rword-encoding (rword)
195 (` (car (cdr (cdr (, rword))))))
196 (defmacro tm-eword::rword-type (rword)
197 (` (car (cdr (cdr (cdr (, rword)))))))
199 (defun tm-eword::find-charset-rule (charsets)
201 (let* ((charset (charsets-to-mime-charset charsets))
202 (encoding (cdr (assq charset eword-charset-encoding-alist)))
204 (list charset encoding)
207 (defun tm-eword::words-to-ruled-words (wl &optional mode)
210 (let ((ret (tm-eword::find-charset-rule (car word))))
211 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
215 (defun tm-eword::space-process (seq)
216 (let (prev a ac b c cc)
221 (setq cc (tm-eword::rword-charset c))
222 (if (null (tm-eword::rword-charset b))
225 (setq ac (tm-eword::rword-charset a))
226 (if (and (tm-eword::rword-encoding a)
227 (tm-eword::rword-encoding c))
230 (cons (concat (car a)(car b)(car c))
238 (cons (concat (car a)(car b))
243 (setq prev (cons b prev))
245 (setq prev (cons b prev))
250 (defun tm-eword::split-string (str &optional mode)
251 (tm-eword::space-process
252 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
253 (tm-eword::split-to-lc-words str))
260 (defun tm-eword::encoded-word-length (rword)
261 (let ((string (tm-eword::rword-text rword))
262 (charset (tm-eword::rword-charset rword))
263 (encoding (tm-eword::rword-encoding rword))
266 (cond ((string-equal encoding "B")
267 (setq string (encode-mime-charset-string string charset))
268 (base64-encoded-length string)
270 ((string-equal encoding "Q")
271 (setq string (encode-mime-charset-string string charset))
272 (q-encoding-encoded-length string
273 (tm-eword::rword-type rword))
276 (cons (+ 7 (length (symbol-name charset)) ret) string)
283 (defun tm-eword::encode-string-1 (column rwl)
284 (let* ((rword (car rwl))
285 (ret (tm-eword::encoded-word-length rword))
288 (cond ((and (setq string (car rword))
289 (<= (setq len (+ (length string) column)) 76)
297 (cond ((and (setq len (car ret))
298 (<= (+ column len) 76)
301 (tm-eword::encode-encoded-text
302 (tm-eword::rword-charset rword)
303 (tm-eword::rword-encoding rword)
305 (tm-eword::rword-type rword)
307 (setq len (+ (length string) column))
311 (setq string (car rword))
314 (while (and (< p len)
316 (setq np (+ p (char-length (sref string p))))
317 (setq nstr (substring string 0 np))
318 (setq ret (tm-eword::encoded-word-length
319 (cons nstr (cdr rword))
321 (setq nstr (cdr ret))
322 (setq len (+ (car ret) column))
327 (if (string-equal str "")
330 (setq rwl (cons (cons (substring string p) (cdr rword))
333 (tm-eword::encode-encoded-text
334 (tm-eword::rword-charset rword)
335 (tm-eword::rword-encoding rword)
337 (tm-eword::rword-type rword)))
338 (setq len (+ (length string) column))
342 (list string len rwl)
345 (defun tm-eword::encode-rwl (column rwl)
346 (let (ret dest ps special str ew-f pew-f)
348 (setq ew-f (nth 2 (car rwl)))
350 (setq rwl (cons '(" ") rwl)
354 (setq ret (tm-eword::encode-string-1 column rwl))
356 (if (eq (elt str 0) ?\n)
359 (setq dest (concat dest "\n ("))
360 (setq ret (tm-eword::encode-string-1 2 rwl))
363 (cond ((eq special 32)
364 (if (string= str "(")
366 (setq dest (concat dest " "))
372 (setq dest (concat dest " ("))
375 (setq dest (concat dest "("))
378 (cond ((string= str " ")
386 (setq dest (concat dest str))
388 (setq column (nth 1 ret)
394 (defun tm-eword::encode-string (column str &optional mode)
395 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
402 (defun tm-eword::phrase-to-rwl (phrase)
403 (let (token type dest str)
405 (setq token (car phrase))
406 (setq type (car token))
407 (cond ((eq type 'quoted-string)
408 (setq str (concat "\"" (cdr token) "\""))
412 (let ((ret (tm-eword::find-charset-rule
413 (find-non-ascii-charset-string str))))
414 (tm-eword::make-rword
415 str (car ret)(nth 1 ret) 'phrase)
423 (tm-eword::words-to-ruled-words
424 (tm-eword::lc-words-to-words
425 (tm-eword::split-to-lc-words (cdr token)))
431 (setq dest (append dest
432 (tm-eword::words-to-ruled-words
433 (tm-eword::lc-words-to-words
434 (tm-eword::split-to-lc-words (cdr token))
437 (setq phrase (cdr phrase))
439 (tm-eword::space-process dest)
442 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
443 (if (eq (car phrase-route-addr) 'phrase-route-addr)
444 (let ((phrase (nth 1 phrase-route-addr))
445 (route (nth 2 phrase-route-addr))
447 (if (eq (car (car phrase)) 'spaces)
448 (setq phrase (cdr phrase))
450 (setq dest (tm-eword::phrase-to-rwl phrase))
452 (setq dest (append dest '((" " nil nil))))
456 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
459 (defun tm-eword::addr-spec-to-rwl (addr-spec)
460 (if (eq (car addr-spec) 'addr-spec)
461 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
464 (defun tm-eword::mailbox-to-rwl (mbox)
465 (let ((addr (nth 1 mbox))
466 (comment (nth 2 mbox))
468 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
469 (tm-eword::addr-spec-to-rwl addr)
476 (tm-eword::split-string comment 'comment)
481 (defun tm-eword::addresses-to-rwl (addresses)
482 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
484 (while (setq addresses (cdr addresses))
485 (setq dest (append dest
488 (tm-eword::mailbox-to-rwl (car addresses))
493 (defun tm-eword::encode-address-list (column str)
494 (tm-eword::encode-rwl
496 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
500 ;;; @ application interfaces
503 (defun eword-encode-field (str)
504 (setq str (std11-unfold-string str))
505 (let ((ret (string-match std11-field-head-regexp str)))
507 (let ((field-name (substring str 0 (1- (match-end 0))))
508 (field-body (eliminate-top-spaces
509 (substring str (match-end 0))))
512 (cond ((string-equal field-body "") "")
513 ((member (setq fname (downcase field-name))
514 '("reply-to" "from" "sender"
515 "resent-reply-to" "resent-from"
516 "resent-sender" "to" "resent-to"
518 "bcc" "resent-bcc" "dcc")
520 (car (tm-eword::encode-address-list
521 (+ (length field-name) 2) field-body))
524 (car (tm-eword::encode-string
525 (+ (length field-name) 1)
529 (concat field-name ": " ret)
531 (car (tm-eword::encode-string 0 str))
534 (defun eword-in-subject-p ()
535 (let ((str (std11-field-body "Subject")))
536 (if (and str (string-match eword-encoded-word-regexp str))
539 (defun eword-encode-header (&optional code-conversion)
540 "Encode header fields to network representation, such as MIME encoded-word.
542 It refer variable `eword-field-encoding-method-alist'."
546 (std11-narrow-to-header mail-header-separator)
547 (goto-char (point-min))
548 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
550 (while (re-search-forward std11-field-head-regexp nil t)
551 (setq beg (match-beginning 0))
552 (setq field-name (buffer-substring beg (1- (match-end 0))))
553 (setq end (std11-field-end))
554 (and (find-non-ascii-charset-region beg end)
555 (let ((ret (or (let ((fname (downcase field-name)))
560 (string= fname (downcase str))
562 eword-field-encoding-method-alist))
563 (assq t eword-field-encoding-method-alist)
566 (let ((method (cdr ret)))
567 (cond ((eq method 'mime)
569 (buffer-substring-no-properties beg end)
571 (delete-region beg end)
572 (insert (eword-encode-field field))
576 (or (mime-charset-to-coding-system
579 (encode-coding-region beg end cs)
584 (and eword-generate-X-Nsubject
585 (or (std11-field-body "X-Nsubject")
586 (let ((str (eword-in-subject-p)))
591 (std11-unfold-string str)))
594 (encode-mime-charset-string
600 (string= "x-nsubject"
603 eword-field-encoding-method-alist))
606 (insert (concat "\nX-Nsubject: " str))
610 (defun eword-encode-string (str &optional column mode)
611 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
618 (provide 'eword-encode)
620 ;;; eword-encode.el ends here