1 ;;; eword-decode.el --- RFC 2047 based encoded-word decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <tomo@m17n.org>
7 ;; TANAKA Akira <akr@m17n.org>
9 ;; Original: 1992/07/20 ENAMI Tsugutomo's `mime.el'.
10 ;; Renamed: 1993/06/03 to tiny-mime.el by MORIOKA Tomohiko
11 ;; Renamed: 1995/10/03 to tm-ew-d.el (split off encoder)
12 ;; by MORIOKA Tomohiko
13 ;; Renamed: 1997/02/22 from tm-ew-d.el by MORIOKA Tomohiko
14 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
16 ;; This file is part of FLIM (Faithful Library about Internet Message).
18 ;; This program is free software; you can redistribute it and/or
19 ;; modify it under the terms of the GNU General Public License as
20 ;; published by the Free Software Foundation; either version 2, or (at
21 ;; your option) any later version.
23 ;; This program is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
39 (eval-when-compile (require 'cl)) ; list*, pop
45 ;; User options are defined in mime-def.el.
48 ;;; @ MIME encoded-word definition
52 (defconst eword-encoded-text-regexp "[!->@-~]+")
54 (defconst eword-encoded-word-regexp
56 (concat (regexp-quote "=?")
64 eword-encoded-text-regexp
66 (regexp-quote "?="))))
73 (defun eword-decode-string (string &optional must-unfold)
74 "Decode MIME encoded-words in STRING.
76 STRING is unfolded before decoding.
78 If an encoded-word is broken or your emacs implementation can not
79 decode the charset included in it, it is not decoded.
81 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
82 if there are in decoded encoded-words (generated by bad manner MUA
83 such as a version of Net$cape)."
84 (setq string (std11-unfold-string string))
85 (let ((dest "")(ew nil)
87 (while (and (string-match eword-encoded-word-regexp string)
88 (setq beg (match-beginning 0)
94 (string-match "^[ \t]+$" (substring string 0 beg))
96 (setq dest (concat dest (substring string 0 beg)))
101 (eword-decode-encoded-word
102 (substring string beg end) must-unfold)
104 (setq string (substring string end))
110 (defun eword-decode-structured-field-body (string
111 &optional start-column max-column
113 (let ((tokens (eword-lexical-analyze string start 'must-unfold))
117 (setq token (car tokens))
118 (setq result (concat result (eword-decode-token token)))
119 (setq tokens (cdr tokens)))
122 (defun eword-decode-and-unfold-structured-field-body (string
127 "Decode and unfold STRING as structured field body.
128 It decodes non us-ascii characters in FULL-NAME encoded as
129 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
130 characters are regarded as variable `default-mime-charset'.
132 If an encoded-word is broken or your emacs implementation can not
133 decode the charset included in it, it is not decoded."
134 (let ((tokens (eword-lexical-analyze string start 'must-unfold))
137 (let* ((token (car tokens))
139 (setq tokens (cdr tokens))
141 (if (eq type 'spaces)
143 (concat result (eword-decode-token token))
147 (defun eword-decode-and-fold-structured-field-body (string
151 (if (and mime-field-decoding-max-size
152 (> (length string) mime-field-decoding-max-size))
155 (setq max-column fill-column))
156 (let ((c start-column)
157 (tokens (eword-lexical-analyze string start 'must-unfold))
160 (while (and (setq token (car tokens))
161 (setq tokens (cdr tokens)))
162 (let* ((type (car token)))
163 (if (eq type 'spaces)
164 (let* ((next-token (car tokens))
165 (next-str (eword-decode-token next-token))
166 (next-len (string-width next-str))
167 (next-c (+ c next-len 1)))
168 (if (< next-c max-column)
169 (setq result (concat result " " next-str)
171 (setq result (concat result "\n " next-str)
173 (setq tokens (cdr tokens))
175 (let* ((str (eword-decode-token token)))
176 (setq result (concat result str)
177 c (+ c (string-width str)))
180 (concat result (eword-decode-token token))
183 ;; unlimited patch by simm-emacs@fan.gr.jp
184 ;; Tue, 01 Feb 2000 13:43:09 +0900
185 (defun eword-decode-unstructured-field-body (string &optional start-column
188 (decode-mime-charset-string-unlimited
190 (or default-mime-charset-unlimited default-mime-charset))))
192 (defun eword-decode-and-unfold-unstructured-field-body (string
193 &optional start-column
196 (decode-mime-charset-string-unlimited
197 (std11-unfold-string string)
198 (or default-mime-charset-unlimited default-mime-charset))
201 (defun eword-decode-unfolded-unstructured-field-body (string
202 &optional start-column
205 (decode-mime-charset-string-unlimited
207 (or default-mime-charset-unlimited default-mime-charset))
214 (defun eword-decode-region (start end &optional unfolding must-unfold)
215 "Decode MIME encoded-words in region between START and END.
217 If UNFOLDING is not nil, it unfolds before decoding.
219 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
220 if there are in decoded encoded-words (generated by bad manner MUA
221 such as a version of Net$cape)."
225 (narrow-to-region start end)
227 (eword-decode-unfold)
229 (goto-char (point-min))
230 (while (re-search-forward (concat "\\(" eword-encoded-word-regexp "\\)"
232 "\\(" eword-encoded-word-regexp "\\)")
234 (replace-match "\\1\\6")
235 (goto-char (point-min))
237 (while (re-search-forward eword-encoded-word-regexp nil t)
238 (insert (eword-decode-encoded-word
240 (buffer-substring (match-beginning 0) (match-end 0))
241 (delete-region (match-beginning 0) (match-end 0))
246 (defun eword-decode-unfold ()
247 (goto-char (point-min))
249 (while (re-search-forward std11-field-head-regexp nil t)
250 (setq beg (match-beginning 0)
251 end (std11-field-end))
252 (setq field (buffer-substring beg end))
253 (if (string-match eword-encoded-word-regexp field)
255 (narrow-to-region (goto-char beg) end)
256 (while (re-search-forward "\n\\([ \t]\\)" nil t)
257 (replace-match (match-string 1))
259 (goto-char (point-max))
264 ;;; @ for message header
267 (defvar mime-field-decoder-alist nil)
269 (defvar mime-field-decoder-cache nil)
271 (defvar mime-update-field-decoder-cache 'mime-update-field-decoder-cache
272 "*Field decoder cache update function.")
275 (defun mime-set-field-decoder (field &rest specs)
276 "Set decoder of FIELD.
277 SPECS must be like `MODE1 DECODER1 MODE2 DECODER2 ...'.
278 Each mode must be `nil', `plain', `wide', `summary' or `nov'.
279 If mode is `nil', corresponding decoder is set up for every modes."
281 (let ((mode (pop specs))
282 (function (pop specs)))
285 (let ((cell (assq mode mime-field-decoder-alist)))
287 (setcdr cell (put-alist field function (cdr cell)))
288 (setq mime-field-decoder-alist
289 (cons (cons mode (list (cons field function)))
290 mime-field-decoder-alist))
292 (apply (function mime-set-field-decoder) field specs)
294 (mime-set-field-decoder field
302 (defmacro mime-find-field-presentation-method (name)
303 "Return field-presentation-method from NAME.
304 NAME must be `plain', `wide', `summary' or `nov'."
306 `(or (assq 'summary mime-field-decoder-cache)
312 (symbolp (car (cdr name)))
313 (null (cdr (cdr name))))
314 `(or (assq ,name mime-field-decoder-cache)
318 `(or (assq (or ,name 'summary) mime-field-decoder-cache)
319 (cons (or ,name 'summary) nil))
322 (defun mime-find-field-decoder-internal (field &optional mode)
323 "Return function to decode field-body of FIELD in MODE.
324 Optional argument MODE must be object of field-presentation-method."
325 (cdr (or (assq field (cdr mode))
327 (funcall mime-update-field-decoder-cache
330 (cdr (assq (car mode) mime-field-decoder-cache)))
334 (defun mime-find-field-decoder (field &optional mode)
335 "Return function to decode field-body of FIELD in MODE.
336 Optional argument MODE must be object or name of
337 field-presentation-method. Name of field-presentation-method must be
338 `plain', `wide', `summary' or `nov'.
339 Default value of MODE is `summary'."
341 (let ((p (cdr (mime-find-field-presentation-method mode))))
342 (if (and p (setq p (assq field p)))
344 (cdr (funcall mime-update-field-decoder-cache
345 field (or mode 'summary)))))
346 (inline (mime-find-field-decoder-internal field mode))
350 (defun mime-update-field-decoder-cache (field mode &optional function)
351 "Update field decoder cache `mime-field-decoder-cache'."
352 (cond ((eq function 'identity)
357 (cdr (assq (or mode 'summary) mime-field-decoder-alist))))
358 (setq function (cdr (or (assq field decoder-alist)
359 (assq t decoder-alist)))))
361 (let ((cell (assq mode mime-field-decoder-cache))
364 (if (setq ret (assq field (cdr cell)))
365 (setcdr ret function)
366 (setcdr cell (cons (setq ret (cons field function)) (cdr cell))))
367 (setq mime-field-decoder-cache
368 (cons (cons mode (list (setq ret (cons field function))))
369 mime-field-decoder-cache)))
373 (mime-set-field-decoder 'Archive nil nil)
374 (mime-set-field-decoder 'Content-Md5 nil nil)
375 (mime-set-field-decoder 'Control nil nil)
376 (mime-set-field-decoder 'Date nil nil)
377 (mime-set-field-decoder 'Distribution nil nil)
378 (mime-set-field-decoder 'Followup-Host nil nil)
379 (mime-set-field-decoder 'Followup-To nil nil)
380 (mime-set-field-decoder 'Lines nil nil)
381 (mime-set-field-decoder 'Message-Id nil nil)
382 (mime-set-field-decoder 'Newsgroups nil nil)
383 (mime-set-field-decoder 'Nntp-Posting-Host nil nil)
384 (mime-set-field-decoder 'Path nil nil)
385 (mime-set-field-decoder 'Posted-And-Mailed nil nil)
386 (mime-set-field-decoder 'Received nil nil)
387 (mime-set-field-decoder 'Status nil nil)
388 (mime-set-field-decoder 'X-Face nil nil)
389 (mime-set-field-decoder 'X-Face-Version nil nil)
390 (mime-set-field-decoder 'X-Info nil nil)
391 (mime-set-field-decoder 'X-Pgp-Key-Info nil nil)
392 (mime-set-field-decoder 'X-Pgp-Sig nil nil)
393 (mime-set-field-decoder 'X-Pgp-Sig-Version nil nil)
394 (mime-set-field-decoder 'Xref nil nil)
398 '(Reply-To Resent-Reply-To From Resent-From Sender Resent-Sender
399 To Resent-To Cc Resent-Cc Bcc Resent-Bcc Dcc
401 Mime-Version Content-Type Content-Transfer-Encoding
402 Content-Disposition User-Agent))
405 (setq field (pop fields))
406 (mime-set-field-decoder
408 'plain #'eword-decode-structured-field-body
409 'wide #'eword-decode-and-fold-structured-field-body
410 'summary #'eword-decode-and-unfold-structured-field-body
411 'nov #'eword-decode-and-unfold-structured-field-body)
414 ;; unstructured fields (default)
415 (mime-set-field-decoder
417 'plain #'eword-decode-unstructured-field-body
418 'wide #'eword-decode-unstructured-field-body
419 'summary #'eword-decode-and-unfold-unstructured-field-body
420 'nov #'eword-decode-unfolded-unstructured-field-body)
423 (defun mime-decode-field-body (field-body field-name
424 &optional mode max-column)
425 "Decode FIELD-BODY as FIELD-NAME in MODE, and return the result.
426 Optional argument MODE must be `plain', `wide', `summary' or `nov'.
427 Default mode is `summary'.
429 If MODE is `wide' and MAX-COLUMN is non-nil, the result is folded with
432 Non MIME encoded-word part in FILED-BODY is decoded with
433 `default-mime-charset'."
434 (let (field-name-symbol len decoder)
435 (if (symbolp field-name)
436 (setq field-name-symbol field-name
437 len (1+ (string-width (symbol-name field-name))))
438 (setq field-name-symbol (intern (capitalize field-name))
439 len (1+ (string-width field-name))))
440 (setq decoder (mime-find-field-decoder field-name-symbol mode))
442 (funcall decoder field-body len max-column)
444 (if (eq mode 'summary)
445 (std11-unfold-string field-body)
450 (defun mime-decode-header-in-region (start end
451 &optional code-conversion)
452 "Decode MIME encoded-words in region between START and END.
453 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
454 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
455 Otherwise it decodes non-ASCII bit patterns as the
456 default-mime-charset."
460 (narrow-to-region start end)
461 (let ((default-charset
463 (if (mime-charset-to-coding-system code-conversion)
465 default-mime-charset))))
467 (let ((mode-obj (mime-find-field-presentation-method 'wide))
468 beg p end field-name len field-decoder)
469 (goto-char (point-min))
470 (while (re-search-forward std11-field-head-regexp nil t)
471 (setq beg (match-beginning 0)
473 field-name (buffer-substring beg (1- p))
474 len (string-width field-name)
475 field-name (intern (capitalize field-name))
476 field-decoder (inline
477 (mime-find-field-decoder-internal
478 field-name mode-obj)))
480 (setq end (std11-field-end))
481 (let ((body (buffer-substring p end))
482 (default-mime-charset default-charset))
483 (delete-region p end)
484 (insert (funcall field-decoder body (1+ len)))
487 (eword-decode-region (point-min) (point-max) t)
491 (defun mime-decode-header-in-buffer (&optional code-conversion separator)
492 "Decode MIME encoded-words in header fields.
493 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
494 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
495 Otherwise it decodes non-ASCII bit patterns as the
496 default-mime-charset.
497 If SEPARATOR is not nil, it is used as header separator."
499 (mime-decode-header-in-region
502 (goto-char (point-min))
503 (if (re-search-forward
504 (concat "^\\(" (regexp-quote (or separator "")) "\\)?$")
511 (defalias 'eword-decode-header 'mime-decode-header-in-buffer)
512 (make-obsolete 'eword-decode-header 'mime-decode-header-in-buffer)
515 ;;; @ encoded-word decoder
518 (defvar eword-decode-encoded-word-error-handler
519 'eword-decode-encoded-word-default-error-handler)
521 (defvar eword-warning-face nil
522 "Face used for invalid encoded-word.")
524 (defun eword-decode-encoded-word-default-error-handler (word signal)
525 (and (add-text-properties 0 (length word)
526 (and eword-warning-face
527 (list 'face eword-warning-face))
531 (defun eword-decode-encoded-word (word &optional must-unfold)
532 "Decode WORD if it is an encoded-word.
534 If your emacs implementation can not decode the charset of WORD, it
535 returns WORD. Similarly the encoded-word is broken, it returns WORD.
537 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
538 if there are in decoded encoded-word (generated by bad manner MUA such
539 as a version of Net$cape)."
540 (or (if (string-match eword-encoded-word-regexp word)
542 (substring word (match-beginning 1) (match-end 1))
546 (substring word (match-beginning 2) (match-end 2))
549 (substring word (match-beginning 3) (match-end 3))
552 (eword-decode-encoded-text charset encoding text must-unfold)
554 (funcall eword-decode-encoded-word-error-handler word err)
560 ;;; @ encoded-text decoder
563 (defun eword-decode-encoded-text (charset encoding string
564 &optional must-unfold)
565 "Decode STRING as an encoded-text.
567 If your emacs implementation can not decode CHARSET, it returns nil.
569 If ENCODING is not \"B\" or \"Q\", it occurs error.
570 So you should write error-handling code if you don't want break by errors.
572 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
573 if there are in decoded encoded-text (generated by bad manner MUA such
574 as a version of Net$cape)."
575 (let ((cs (mime-charset-to-coding-system charset)))
577 (let ((dest (encoded-text-decode-string string encoding)))
579 (setq dest (decode-mime-charset-string dest charset))
583 (cond ((eq chr ?\n) "")
585 (t (char-to-string chr)))
587 (std11-unfold-string dest)
592 ;;; @ lexical analyze
595 (defvar eword-lexical-analyze-cache nil)
596 (defvar eword-lexical-analyze-cache-max 299
597 "*Max position of eword-lexical-analyze-cache.
598 It is max size of eword-lexical-analyze-cache - 1.")
600 (defvar mime-header-lexical-analyzer
601 '(eword-analyze-quoted-string
602 eword-analyze-domain-literal
603 eword-analyze-comment
605 eword-analyze-special
606 eword-analyze-encoded-word
608 "*List of functions to return result of lexical analyze.
609 Each function must have three arguments: STRING, START and MUST-UNFOLD.
610 STRING is the target string to be analyzed.
611 START is start position of STRING to analyze.
612 If MUST-UNFOLD is not nil, each function must unfold and eliminate
613 bare-CR and bare-LF from the result even if they are included in
614 content of the encoded-word.
615 Each function must return nil if it can not analyze STRING as its
618 Previous function is preferred to next function. If a function
619 returns nil, next function is used. Otherwise the return value will
622 (defun eword-analyze-quoted-string (string start &optional must-unfold)
623 (let ((p (std11-check-enclosure string ?\" ?\" nil start)))
625 (cons (cons 'quoted-string
626 (decode-mime-charset-string
627 (std11-strip-quoted-pair
628 (substring string (1+ start) (1- p)))
629 default-mime-charset))
630 ;;(substring string p))
634 (defun eword-analyze-domain-literal (string start &optional must-unfold)
635 (std11-analyze-domain-literal string start))
637 (defun eword-analyze-comment (string from &optional must-unfold)
638 (let ((len (length string))
643 (eq (aref string i) ?\())
648 (setq chr (aref string i))
654 (setq last-str (concat last-str
655 (substring string from (1- i))
656 (char-to-string (aref string i)))
661 (setq ret (concat last-str
662 (substring string from i)))
670 (decode-mime-charset-string
671 ret default-mime-charset)
678 (if (setq ret (eword-analyze-comment string i must-unfold))
681 (substring string from i))
683 (if (string= last-str "")
684 (cons (car ret) dest)
687 (decode-mime-charset-string
688 last-str default-mime-charset)
702 (defun eword-analyze-spaces (string start &optional must-unfold)
703 (std11-analyze-spaces string start))
705 (defun eword-analyze-special (string start &optional must-unfold)
706 (std11-analyze-special string start))
708 (defun eword-analyze-encoded-word (string start &optional must-unfold)
709 (if (and (string-match eword-encoded-word-regexp string start)
710 (= (match-beginning 0) start))
711 (let ((end (match-end 0))
712 (dest (eword-decode-encoded-word (match-string 0 string)
715 ;;(setq string (substring string end))
717 (while (and (string-match (eval-when-compile
718 (concat "[ \t\n]*\\("
719 eword-encoded-word-regexp
722 (= (match-beginning 0) start))
723 (setq end (match-end 0))
726 (eword-decode-encoded-word (match-string 1 string)
728 ;;string (substring string end))
731 (cons (cons 'atom dest) ;;string)
735 (defun eword-analyze-atom (string start &optional must-unfold)
736 (if (and (string-match std11-atom-regexp string start)
737 (= (match-beginning 0) start))
738 (let ((end (match-end 0)))
739 (cons (cons 'atom (decode-mime-charset-string
740 (substring string start end)
741 default-mime-charset))
742 ;;(substring string end)
746 (defun eword-lexical-analyze-internal (string start must-unfold)
747 (let ((len (length string))
751 (let ((rest mime-header-lexical-analyzer)
753 ;; unlimited patch by simm-emacs@fan.gr.jp,
754 ;; Mon, 10 Jan 2000 12:52:39 +0900
755 (while (and (setq func (car rest))
756 (or (and mime-decode-unlimited
757 (eq func 'eword-analyze-quoted-string))
759 (setq r (funcall func string start must-unfold)))))
760 (setq rest (cdr rest)))
762 (cons (cons 'error (substring string start)) (1+ len)))
764 (setq dest (cons (car ret) dest)
770 (defun eword-lexical-analyze (string &optional start must-unfold)
771 "Return lexical analyzed list corresponding STRING.
772 It is like std11-lexical-analyze, but it decodes non us-ascii
773 characters encoded as encoded-words or invalid \"raw\" format.
774 \"Raw\" non us-ascii characters are regarded as variable
775 `default-mime-charset'."
776 (let ((key (substring string (or start 0)))
778 (set-text-properties 0 (length key) nil key)
779 (if (setq ret (assoc key eword-lexical-analyze-cache))
781 (setq ret (eword-lexical-analyze-internal key 0 must-unfold))
782 (setq eword-lexical-analyze-cache
784 eword-lexical-analyze-cache))
785 (if (cdr (setq cell (nthcdr eword-lexical-analyze-cache-max
786 eword-lexical-analyze-cache)))
790 (defun eword-decode-token (token)
791 (let ((type (car token))
793 (cond ((eq type 'quoted-string)
794 (std11-wrap-as-quoted-string value))
798 (setq dest (concat dest
799 (if (stringp (car value))
800 ;; unlimited patch by simm-emacs@fan.gr.jp
801 ;; Mon, 10 Jan 2000 12:53:46 +0900
802 (if mime-decode-unlimited
804 (std11-wrap-as-quoted-pairs
805 (car value) '(?( ?))))
806 (std11-wrap-as-quoted-pairs
807 (car value) '(?( ?))))
808 (eword-decode-token (car value))
812 (concat "(" dest ")")
816 (defun eword-extract-address-components (string &optional start)
817 "Extract full name and canonical address from STRING.
818 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
819 If no name can be extracted, FULL-NAME will be nil.
820 It decodes non us-ascii characters in FULL-NAME encoded as
821 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
822 characters are regarded as variable `default-mime-charset'."
823 (let* ((structure (car (std11-parse-address
824 (eword-lexical-analyze
825 (std11-unfold-string string) start
827 (phrase (std11-full-name-string structure))
828 (address (std11-address-string structure))
830 (list phrase address)
837 (provide 'eword-decode)
839 ;;; eword-decode.el ends here