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.9 $
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.
37 (defconst eword-encode-RCS-ID
38 "$Id: eword-encode.el,v 0.9 1997-02-24 10:07:33 tmorioka Exp $")
39 (defconst eword-encode-version (get-version-string eword-encode-RCS-ID))
45 (defvar eword-field-encoding-method-alist
46 '(("X-Nsubject" . iso-2022-jp-2)
50 "*Alist to specify field encoding method.
51 Its key is field-name, value is encoding method.
53 If method is `mime', this field will be encoded into MIME format.
55 If method is a MIME-charset, this field will be encoded as the charset
56 when it must be convert into network-code.
58 If method is `default-mime-charset', this field will be encoded as
59 variable `default-mime-charset' when it must be convert into
62 If method is nil, this field will not be encoded.")
64 (defvar eword-generate-X-Nsubject nil
65 "*If it is not nil, X-Nsubject field is generated
66 when Subject field is encoded by `eword-encode-header'.")
68 (defvar eword-charset-encoding-alist
86 (iso-2022-int-1 . "B")
90 ;;; @ encoded-text encoder
93 (defun tm-eword::encode-encoded-text (charset encoding string &optional mode)
95 (cond ((string= encoding "B")
96 (base64-encode-string string))
97 ((string= encoding "Q")
98 (q-encoding-encode-string string mode))
102 (concat "=?" (upcase (symbol-name charset)) "?"
103 encoding "?" text "?=")
110 (defun tm-eword::char-type (chr)
111 (if (or (= chr 32)(= chr ?\t))
116 (defun tm-eword::parse-lc-word (str)
117 (let* ((chr (sref str 0))
118 (lc (tm-eword::char-type chr))
119 (i (char-length chr))
122 (while (and (< i len)
123 (setq chr (sref str i))
124 (eq lc (tm-eword::char-type chr))
126 (setq i (+ i (char-length chr)))
128 (cons (cons lc (substring str 0 i)) (substring str i))
131 (defun tm-eword::split-to-lc-words (str)
133 (while (and (not (string= str ""))
134 (setq ret (tm-eword::parse-lc-word str))
136 (setq dest (cons (car ret) dest))
146 (defun tm-eword::parse-word (lcwl)
147 (let* ((lcw (car lcwl))
152 (let ((lcl (list lc))
156 (while (setq lcwl (cdr lcwl))
157 (setq lcw (car lcwl))
162 (if (not (memq lc lcl))
163 (setq lcl (cons lc lcl))
165 (setq str (concat str (cdr lcw)))
167 (cons (cons lcl str) lcwl)
170 (defun tm-eword::lc-words-to-words (lcwl)
172 (while (setq ret (tm-eword::parse-word lcwl))
173 (setq dest (cons (car ret) dest))
174 (setq lcwl (cdr ret))
183 (defmacro tm-eword::make-rword (text charset encoding type)
184 (` (list (, text)(, charset)(, encoding)(, type))))
185 (defmacro tm-eword::rword-text (rword)
187 (defmacro tm-eword::rword-charset (rword)
188 (` (car (cdr (, rword)))))
189 (defmacro tm-eword::rword-encoding (rword)
190 (` (car (cdr (cdr (, rword))))))
191 (defmacro tm-eword::rword-type (rword)
192 (` (car (cdr (cdr (cdr (, rword)))))))
194 (defun tm-eword::find-charset-rule (charsets)
196 (let* ((charset (charsets-to-mime-charset charsets))
197 (encoding (cdr (assq charset eword-charset-encoding-alist)))
199 (list charset encoding)
202 (defun tm-eword::words-to-ruled-words (wl &optional mode)
205 (let ((ret (tm-eword::find-charset-rule (car word))))
206 (tm-eword::make-rword (cdr word) (car ret)(nth 1 ret) mode)
210 (defun tm-eword::space-process (seq)
211 (let (prev a ac b c cc)
216 (setq cc (tm-eword::rword-charset c))
217 (if (null (tm-eword::rword-charset b))
220 (setq ac (tm-eword::rword-charset a))
221 (if (and (tm-eword::rword-encoding a)
222 (tm-eword::rword-encoding c))
225 (cons (concat (car a)(car b)(car c))
233 (cons (concat (car a)(car b))
238 (setq prev (cons b prev))
240 (setq prev (cons b prev))
245 (defun tm-eword::split-string (str &optional mode)
246 (tm-eword::space-process
247 (tm-eword::words-to-ruled-words (tm-eword::lc-words-to-words
248 (tm-eword::split-to-lc-words str))
255 (defun tm-eword::encoded-word-length (rword)
256 (let ((string (tm-eword::rword-text rword))
257 (charset (tm-eword::rword-charset rword))
258 (encoding (tm-eword::rword-encoding rword))
261 (cond ((string-equal encoding "B")
262 (setq string (encode-mime-charset-string string charset))
263 (base64-encoded-length string)
265 ((string-equal encoding "Q")
266 (setq string (encode-mime-charset-string string charset))
267 (q-encoding-encoded-length string
268 (tm-eword::rword-type rword))
271 (cons (+ 7 (length (symbol-name charset)) ret) string)
278 (defun tm-eword::encode-string-1 (column rwl)
279 (let* ((rword (car rwl))
280 (ret (tm-eword::encoded-word-length rword))
283 (cond ((and (setq string (car rword))
284 (<= (setq len (+ (length string) column)) 76)
292 (cond ((and (setq len (car ret))
293 (<= (+ column len) 76)
296 (tm-eword::encode-encoded-text
297 (tm-eword::rword-charset rword)
298 (tm-eword::rword-encoding rword)
300 (tm-eword::rword-type rword)
302 (setq len (+ (length string) column))
306 (setq string (car rword))
309 (while (and (< p len)
311 (setq np (+ p (char-length (sref string p))))
312 (setq nstr (substring string 0 np))
313 (setq ret (tm-eword::encoded-word-length
314 (cons nstr (cdr rword))
316 (setq nstr (cdr ret))
317 (setq len (+ (car ret) column))
322 (if (string-equal str "")
325 (setq rwl (cons (cons (substring string p) (cdr rword))
328 (tm-eword::encode-encoded-text
329 (tm-eword::rword-charset rword)
330 (tm-eword::rword-encoding rword)
332 (tm-eword::rword-type rword)))
333 (setq len (+ (length string) column))
337 (list string len rwl)
340 (defun tm-eword::encode-rwl (column rwl)
341 (let (ret dest ps special str ew-f pew-f)
343 (setq ew-f (nth 2 (car rwl)))
345 (setq rwl (cons '(" ") rwl)
349 (setq ret (tm-eword::encode-string-1 column rwl))
351 (if (eq (elt str 0) ?\n)
354 (setq dest (concat dest "\n ("))
355 (setq ret (tm-eword::encode-string-1 2 rwl))
358 (cond ((eq special 32)
359 (if (string= str "(")
361 (setq dest (concat dest " "))
367 (setq dest (concat dest " ("))
370 (setq dest (concat dest "("))
373 (cond ((string= str " ")
381 (setq dest (concat dest str))
383 (setq column (nth 1 ret)
389 (defun tm-eword::encode-string (column str &optional mode)
390 (tm-eword::encode-rwl column (tm-eword::split-string str mode))
397 (defun tm-eword::phrase-to-rwl (phrase)
398 (let (token type dest str)
400 (setq token (car phrase))
401 (setq type (car token))
402 (cond ((eq type 'quoted-string)
403 (setq str (concat "\"" (cdr token) "\""))
407 (let ((ret (tm-eword::find-charset-rule
408 (find-non-ascii-charset-string str))))
409 (tm-eword::make-rword
410 str (car ret)(nth 1 ret) 'phrase)
418 (tm-eword::words-to-ruled-words
419 (tm-eword::lc-words-to-words
420 (tm-eword::split-to-lc-words (cdr token)))
426 (setq dest (append dest
427 (tm-eword::words-to-ruled-words
428 (tm-eword::lc-words-to-words
429 (tm-eword::split-to-lc-words (cdr token))
432 (setq phrase (cdr phrase))
434 (tm-eword::space-process dest)
437 (defun tm-eword::phrase-route-addr-to-rwl (phrase-route-addr)
438 (if (eq (car phrase-route-addr) 'phrase-route-addr)
439 (let ((phrase (nth 1 phrase-route-addr))
440 (route (nth 2 phrase-route-addr))
442 (if (eq (car (car phrase)) 'spaces)
443 (setq phrase (cdr phrase))
445 (setq dest (tm-eword::phrase-to-rwl phrase))
447 (setq dest (append dest '((" " nil nil))))
451 (list (list (concat "<" (std11-addr-to-string route) ">") nil nil))
454 (defun tm-eword::addr-spec-to-rwl (addr-spec)
455 (if (eq (car addr-spec) 'addr-spec)
456 (list (list (std11-addr-to-string (cdr addr-spec)) nil nil))
459 (defun tm-eword::mailbox-to-rwl (mbox)
460 (let ((addr (nth 1 mbox))
461 (comment (nth 2 mbox))
463 (setq dest (or (tm-eword::phrase-route-addr-to-rwl addr)
464 (tm-eword::addr-spec-to-rwl addr)
471 (tm-eword::split-string comment 'comment)
476 (defun tm-eword::addresses-to-rwl (addresses)
477 (let ((dest (tm-eword::mailbox-to-rwl (car addresses))))
479 (while (setq addresses (cdr addresses))
480 (setq dest (append dest
483 (tm-eword::mailbox-to-rwl (car addresses))
488 (defun tm-eword::encode-address-list (column str)
489 (tm-eword::encode-rwl
491 (tm-eword::addresses-to-rwl (std11-parse-addresses-string str))
495 ;;; @ application interfaces
498 (defun eword-encode-field (str)
499 (setq str (std11-unfold-string str))
500 (let ((ret (string-match std11-field-head-regexp str)))
502 (let ((field-name (substring str 0 (1- (match-end 0))))
503 (field-body (eliminate-top-spaces
504 (substring str (match-end 0))))
507 (cond ((string-equal field-body "") "")
508 ((member (setq fname (downcase field-name))
509 '("reply-to" "from" "sender"
510 "resent-reply-to" "resent-from"
511 "resent-sender" "to" "resent-to"
513 "bcc" "resent-bcc" "dcc")
515 (car (tm-eword::encode-address-list
516 (+ (length field-name) 2) field-body))
519 (car (tm-eword::encode-string
520 (+ (length field-name) 1)
524 (concat field-name ": " ret)
526 (car (tm-eword::encode-string 0 str))
529 (defun eword-in-subject-p ()
530 (let ((str (std11-field-body "Subject")))
531 (if (and str (string-match eword-encoded-word-regexp str))
534 (defun eword-encode-header (&optional code-conversion)
535 "Encode header fields to network representation, such as MIME encoded-word.
537 It refer variable `eword-field-encoding-method-alist'."
541 (std11-narrow-to-header mail-header-separator)
542 (goto-char (point-min))
543 (let ((default-cs (mime-charset-to-coding-system default-mime-charset))
545 (while (re-search-forward std11-field-head-regexp nil t)
546 (setq beg (match-beginning 0))
547 (setq field-name (buffer-substring beg (1- (match-end 0))))
548 (setq end (std11-field-end))
549 (and (find-non-ascii-charset-region beg end)
550 (let ((ret (or (let ((fname (downcase field-name)))
555 (string= fname (downcase str))
557 eword-field-encoding-method-alist))
558 (assq t eword-field-encoding-method-alist)
561 (let ((method (cdr ret)))
562 (cond ((eq method 'mime)
564 (buffer-substring-no-properties beg end)
566 (delete-region beg end)
567 (insert (eword-encode-field field))
571 (or (mime-charset-to-coding-system
574 (encode-coding-region beg end cs)
579 (and eword-generate-X-Nsubject
580 (or (std11-field-body "X-Nsubject")
581 (let ((str (eword-in-subject-p)))
586 (std11-unfold-string str)))
589 (encode-mime-charset-string
595 (string= "x-nsubject"
598 eword-field-encoding-method-alist))
601 (insert (concat "\nX-Nsubject: " str))
605 (defun eword-encode-string (str &optional column mode)
606 (car (tm-eword::encode-rwl (or column 0) (tm-eword::split-string str mode)))
613 (provide 'eword-encode)
615 ;;; eword-encode.el ends here