1 ;;; eword-decode.el --- RFC 2047 based encoded-word decoder for GNU Emacs
3 ;; Copyright (C) 1995,96,97,98,99,2000,01,03 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 "=?")
58 mime-charset-regexp ; 1
62 mime-language-regexp ; 2
66 mime-encoding-regexp ; 3
70 eword-encoded-text-regexp ; 4
72 (regexp-quote "?="))))
79 (defun eword-decode-string (string &optional must-unfold)
80 "Decode MIME encoded-words in STRING.
82 STRING is unfolded before decoding.
84 If an encoded-word is broken or your emacs implementation can not
85 decode the charset included in it, it is not decoded.
87 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
88 if there are in decoded encoded-words (generated by bad manner MUA
89 such as a version of Net$cape)."
90 (setq string (std11-unfold-string string))
91 (let ((dest "")(ew nil)
93 (while (and (string-match eword-encoded-word-regexp string)
94 (setq beg (match-beginning 0)
100 (string-match "^[ \t]+$" (substring string 0 beg))
102 (setq dest (concat dest (substring string 0 beg)))
107 (eword-decode-encoded-word
108 (substring string beg end) must-unfold)
110 (setq string (substring string end))
116 (defun eword-decode-structured-field-body (string
117 &optional start-column max-column
119 (let ((tokens (eword-lexical-analyze string start 'must-unfold))
123 (setq token (car tokens))
124 (setq result (concat result (eword-decode-token token)))
125 (setq tokens (cdr tokens)))
128 (defun eword-decode-and-unfold-structured-field-body (string
133 "Decode and unfold STRING as structured field body.
134 It decodes non us-ascii characters in FULL-NAME encoded as
135 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
136 characters are regarded as variable `default-mime-charset'.
138 If an encoded-word is broken or your emacs implementation can not
139 decode the charset included in it, it is not decoded."
140 (let ((tokens (eword-lexical-analyze string start 'must-unfold))
143 (let* ((token (car tokens))
145 (setq tokens (cdr tokens))
147 (if (eq type 'spaces)
149 (concat result (eword-decode-token token))
153 (defun eword-decode-and-fold-structured-field-body (string
157 (if (and mime-field-decoding-max-size
158 (> (length string) mime-field-decoding-max-size))
161 (setq max-column fill-column))
162 (let ((c start-column)
163 (tokens (eword-lexical-analyze string start 'must-unfold))
166 (while (and (setq token (car tokens))
167 (setq tokens (cdr tokens)))
168 (let* ((type (car token)))
169 (if (eq type 'spaces)
170 (let* ((next-token (car tokens))
171 (next-str (eword-decode-token next-token))
172 (next-len (string-width next-str))
173 (next-c (+ c next-len 1)))
174 (if (< next-c max-column)
175 (setq result (concat result " " next-str)
177 (setq result (concat result "\n " next-str)
179 (setq tokens (cdr tokens))
181 (let* ((str (eword-decode-token token)))
182 (setq result (concat result str)
183 c (+ c (string-width str)))
186 (concat result (eword-decode-token token))
189 (defun eword-decode-unstructured-field-body (string &optional start-column
192 (decode-mime-charset-string string default-mime-charset)))
194 (defun eword-decode-and-unfold-unstructured-field-body (string
195 &optional start-column
198 (decode-mime-charset-string (std11-unfold-string string)
199 default-mime-charset)
202 (defun eword-decode-unfolded-unstructured-field-body (string
203 &optional start-column
206 (decode-mime-charset-string string default-mime-charset)
213 (defun eword-decode-region (start end &optional unfolding must-unfold)
214 "Decode MIME encoded-words in region between START and END.
216 If UNFOLDING is not nil, it unfolds before decoding.
218 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
219 if there are in decoded encoded-words (generated by bad manner MUA
220 such as a version of Net$cape)."
224 (narrow-to-region start end)
226 (eword-decode-unfold)
228 (goto-char (point-min))
229 (while (re-search-forward (concat "\\(" eword-encoded-word-regexp "\\)"
231 "\\(" eword-encoded-word-regexp "\\)")
233 (replace-match "\\1\\7")
234 (goto-char (point-min))
236 (while (re-search-forward eword-encoded-word-regexp nil t)
237 (insert (eword-decode-encoded-word
239 (buffer-substring (match-beginning 0) (match-end 0))
240 (delete-region (match-beginning 0) (match-end 0))
245 (defun eword-decode-unfold ()
246 (goto-char (point-min))
248 (while (re-search-forward std11-field-head-regexp nil t)
249 (setq beg (match-beginning 0)
250 end (std11-field-end))
251 (setq field (buffer-substring beg end))
252 (if (string-match eword-encoded-word-regexp field)
254 (narrow-to-region (goto-char beg) end)
255 (while (re-search-forward "\n\\([ \t]\\)" nil t)
256 (replace-match (match-string 1))
258 (goto-char (point-max))
263 ;;; @ for message header
266 (defvar mime-field-decoder-alist nil)
268 (defvar mime-field-decoder-cache nil)
270 (defvar mime-update-field-decoder-cache 'mime-update-field-decoder-cache
271 "*Field decoder cache update function.")
274 (defun mime-set-field-decoder (field &rest specs)
275 "Set decoder of FIELD.
276 SPECS must be like `MODE1 DECODER1 MODE2 DECODER2 ...'.
277 Each mode must be `nil', `plain', `wide', `summary' or `nov'.
278 If mode is `nil', corresponding decoder is set up for every modes."
280 (let ((mode (pop specs))
281 (function (pop specs)))
284 (let ((cell (assq mode mime-field-decoder-alist)))
286 (setcdr cell (put-alist field function (cdr cell)))
287 (setq mime-field-decoder-alist
288 (cons (cons mode (list (cons field function)))
289 mime-field-decoder-alist))
291 (apply (function mime-set-field-decoder) field specs)
293 (mime-set-field-decoder field
301 (defmacro mime-find-field-presentation-method (name)
302 "Return field-presentation-method from NAME.
303 NAME must be `plain', `wide', `summary' or `nov'."
305 `(or (assq 'summary mime-field-decoder-cache)
311 (symbolp (car (cdr name)))
312 (null (cdr (cdr name))))
313 `(or (assq ,name mime-field-decoder-cache)
317 `(or (assq (or ,name 'summary) mime-field-decoder-cache)
318 (cons (or ,name 'summary) nil))
321 (defun mime-find-field-decoder-internal (field &optional mode)
322 "Return function to decode field-body of FIELD in MODE.
323 Optional argument MODE must be object of field-presentation-method."
324 (cdr (or (assq field (cdr mode))
326 (funcall mime-update-field-decoder-cache
329 (cdr (assq (car mode) mime-field-decoder-cache)))
333 (defun mime-find-field-decoder (field &optional mode)
334 "Return function to decode field-body of FIELD in MODE.
335 Optional argument MODE must be object or name of
336 field-presentation-method. Name of field-presentation-method must be
337 `plain', `wide', `summary' or `nov'.
338 Default value of MODE is `summary'."
340 (let ((p (cdr (mime-find-field-presentation-method mode))))
341 (if (and p (setq p (assq field p)))
343 (cdr (funcall mime-update-field-decoder-cache
344 field (or mode 'summary)))))
345 (inline (mime-find-field-decoder-internal field mode))
349 (defun mime-update-field-decoder-cache (field mode &optional function)
350 "Update field decoder cache `mime-field-decoder-cache'."
351 (cond ((eq function 'identity)
356 (cdr (assq (or mode 'summary) mime-field-decoder-alist))))
357 (setq function (cdr (or (assq field decoder-alist)
358 (assq t decoder-alist)))))
360 (let ((cell (assq mode mime-field-decoder-cache))
363 (if (setq ret (assq field (cdr cell)))
364 (setcdr ret function)
365 (setcdr cell (cons (setq ret (cons field function)) (cdr cell))))
366 (setq mime-field-decoder-cache
367 (cons (cons mode (list (setq ret (cons field function))))
368 mime-field-decoder-cache)))
372 (mime-set-field-decoder 'Archive nil nil)
373 (mime-set-field-decoder 'Content-Md5 nil nil)
374 (mime-set-field-decoder 'Control nil nil)
375 (mime-set-field-decoder 'Date nil nil)
376 (mime-set-field-decoder 'Distribution nil nil)
377 (mime-set-field-decoder 'Followup-Host nil nil)
378 (mime-set-field-decoder 'Followup-To nil nil)
379 (mime-set-field-decoder 'Lines nil nil)
380 (mime-set-field-decoder 'Message-Id nil nil)
381 (mime-set-field-decoder 'Newsgroups nil nil)
382 (mime-set-field-decoder 'Nntp-Posting-Host nil nil)
383 (mime-set-field-decoder 'Path nil nil)
384 (mime-set-field-decoder 'Posted-And-Mailed nil nil)
385 (mime-set-field-decoder 'Received nil nil)
386 (mime-set-field-decoder 'Status nil nil)
387 (mime-set-field-decoder 'X-Face nil nil)
388 (mime-set-field-decoder 'X-Face-Version nil nil)
389 (mime-set-field-decoder 'X-Info nil nil)
390 (mime-set-field-decoder 'X-Pgp-Key-Info nil nil)
391 (mime-set-field-decoder 'X-Pgp-Sig nil nil)
392 (mime-set-field-decoder 'X-Pgp-Sig-Version nil nil)
393 (mime-set-field-decoder 'Xref nil nil)
397 '(Reply-To Resent-Reply-To From Resent-From Sender Resent-Sender
398 To Resent-To Cc Resent-Cc Bcc Resent-Bcc Dcc
400 Mime-Version Content-Type Content-Transfer-Encoding
401 Content-Disposition User-Agent))
404 (setq field (pop fields))
405 (mime-set-field-decoder
407 'plain #'eword-decode-structured-field-body
408 'wide #'eword-decode-and-fold-structured-field-body
409 'summary #'eword-decode-and-unfold-structured-field-body
410 'nov #'eword-decode-and-unfold-structured-field-body)
413 ;; unstructured fields (default)
414 (mime-set-field-decoder
416 'plain #'eword-decode-unstructured-field-body
417 'wide #'eword-decode-unstructured-field-body
418 'summary #'eword-decode-and-unfold-unstructured-field-body
419 'nov #'eword-decode-unfolded-unstructured-field-body)
422 (defun mime-decode-field-body (field-body field-name
423 &optional mode max-column)
424 "Decode FIELD-BODY as FIELD-NAME in MODE, and return the result.
425 Optional argument MODE must be `plain', `wide', `summary' or `nov'.
426 Default mode is `summary'.
428 If MODE is `wide' and MAX-COLUMN is non-nil, the result is folded with
431 Non MIME encoded-word part in FILED-BODY is decoded with
432 `default-mime-charset'."
433 (let (field-name-symbol len decoder)
434 (if (symbolp field-name)
435 (setq field-name-symbol field-name
436 len (1+ (string-width (symbol-name field-name))))
437 (setq field-name-symbol (intern (capitalize field-name))
438 len (1+ (string-width field-name))))
439 (setq decoder (mime-find-field-decoder field-name-symbol mode))
441 (funcall decoder field-body len max-column)
443 (if (eq mode 'summary)
444 (std11-unfold-string field-body)
449 (defun mime-decode-header-in-region (start end
450 &optional code-conversion)
451 "Decode MIME encoded-words in region between START and END.
452 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
453 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
454 Otherwise it decodes non-ASCII bit patterns as the
455 default-mime-charset."
459 (narrow-to-region start end)
460 (let ((default-charset
462 (if (mime-charset-to-coding-system code-conversion)
464 default-mime-charset))))
466 (let ((mode-obj (mime-find-field-presentation-method 'wide))
467 beg p end field-name len field-decoder)
468 (goto-char (point-min))
469 (while (re-search-forward std11-field-head-regexp nil t)
470 (setq beg (match-beginning 0)
472 field-name (buffer-substring beg (1- p))
473 len (string-width field-name)
474 field-name (intern (capitalize field-name))
475 field-decoder (inline
476 (mime-find-field-decoder-internal
477 field-name mode-obj)))
479 (setq end (std11-field-end))
480 (let ((body (buffer-substring p end))
481 (default-mime-charset default-charset))
482 (delete-region p end)
483 (insert (funcall field-decoder body (1+ len)))
486 (eword-decode-region (point-min) (point-max) t)
490 (defun mime-decode-header-in-buffer (&optional code-conversion separator)
491 "Decode MIME encoded-words in header fields.
492 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
493 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
494 Otherwise it decodes non-ASCII bit patterns as the
495 default-mime-charset.
496 If SEPARATOR is not nil, it is used as header separator."
498 (mime-decode-header-in-region
501 (goto-char (point-min))
502 (if (re-search-forward
503 (concat "^\\(" (regexp-quote (or separator "")) "\\)?$")
510 (defalias 'eword-decode-header 'mime-decode-header-in-buffer)
511 (make-obsolete 'eword-decode-header 'mime-decode-header-in-buffer)
514 ;;; @ encoded-word decoder
517 (defvar eword-decode-encoded-word-error-handler
518 'eword-decode-encoded-word-default-error-handler)
520 (defvar eword-warning-face nil
521 "Face used for invalid encoded-word.")
523 (defun eword-decode-encoded-word-default-error-handler (word signal)
524 (and (add-text-properties 0 (length word)
525 (and eword-warning-face
526 (list 'face eword-warning-face))
530 (defun eword-decode-encoded-word (word &optional must-unfold)
531 "Decode WORD as an encoded-word.
533 If charset is unknown or unsupported, return WORD.
534 If encoding is unknown, or some error occurs while decoding,
535 `eword-decode-encoded-word-error-handler' is called with WORD and an
538 If MUST-UNFOLD is non-nil, unfold decoded WORD."
539 (or (and (string-match eword-encoded-word-regexp word)
541 (eword-decode-encoded-text
543 (substring word (match-beginning 1)(match-end 1))
545 (when (match-beginning 2)
548 (substring word (1+ (match-beginning 2))(match-end 2)))))
551 (substring word (match-beginning 3)(match-end 3)))
553 (substring word (match-beginning 4)(match-end 4))
556 (funcall eword-decode-encoded-word-error-handler word err))))
560 ;;; @ encoded-text decoder
563 (defun eword-decode-encoded-text (charset language 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 LANGUAGE is non-nil, it is put to `mime-language' text-property.
570 If ENCODING is not \"B\" or \"Q\", it occurs error.
571 So you should write error-handling code if you don't want break by errors.
573 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
574 if there are in decoded encoded-text (generated by bad manner MUA such
575 as a version of Net$cape)."
576 (when (mime-charset-to-coding-system charset)
577 (let ((dest (encoded-text-decode-string string encoding)))
579 (setq dest (decode-mime-charset-string dest charset))
584 (cond ((eq chr ?\n) "")
586 (t (char-to-string chr)))))
587 (std11-unfold-string dest) ""))
589 (put-text-property 0 (length dest) 'mime-language language dest))
593 ;;; @ lexical analyze
596 (defvar eword-lexical-analyze-cache nil)
597 (defvar eword-lexical-analyze-cache-max 299
598 "*Max position of eword-lexical-analyze-cache.
599 It is max size of eword-lexical-analyze-cache - 1.")
601 (defvar mime-header-lexical-analyzer
602 '(eword-analyze-quoted-string
603 eword-analyze-domain-literal
604 eword-analyze-comment
606 eword-analyze-special
607 eword-analyze-encoded-word
609 "*List of functions to return result of lexical analyze.
610 Each function must have three arguments: STRING, START and MUST-UNFOLD.
611 STRING is the target string to be analyzed.
612 START is start position of STRING to analyze.
613 If MUST-UNFOLD is not nil, each function must unfold and eliminate
614 bare-CR and bare-LF from the result even if they are included in
615 content of the encoded-word.
616 Each function must return nil if it can not analyze STRING as its
619 Previous function is preferred to next function. If a function
620 returns nil, next function is used. Otherwise the return value will
623 (defun eword-analyze-quoted-string (string start &optional must-unfold)
624 (let ((p (std11-check-enclosure string ?\" ?\" nil start))
627 (setq ret (decode-mime-charset-string
628 (std11-strip-quoted-pair
629 (substring string (1+ start) (1- p)))
630 default-mime-charset))
631 (if mime-header-accept-quoted-encoded-words
632 (setq ret (eword-decode-string ret)))
633 (cons (cons 'quoted-string ret)
636 (defun eword-analyze-domain-literal (string start &optional must-unfold)
637 (std11-analyze-domain-literal string start))
639 (defun eword-analyze-comment (string from &optional must-unfold)
640 (let ((len (length string))
645 (eq (aref string i) ?\())
650 (setq chr (aref string i))
656 (setq last-str (concat last-str
657 (substring string from (1- i))
658 (char-to-string (aref string i)))
663 (setq ret (concat last-str
664 (substring string from i)))
672 (decode-mime-charset-string
673 ret default-mime-charset)
680 (if (setq ret (eword-analyze-comment string i must-unfold))
683 (substring string from i))
685 (if (string= last-str "")
686 (cons (car ret) dest)
689 (decode-mime-charset-string
690 last-str default-mime-charset)
704 (defun eword-analyze-spaces (string start &optional must-unfold)
705 (std11-analyze-spaces string start))
707 (defun eword-analyze-special (string start &optional must-unfold)
708 (std11-analyze-special string start))
710 (defun eword-analyze-encoded-word (string start &optional must-unfold)
711 (if (and (string-match eword-encoded-word-regexp string start)
712 (= (match-beginning 0) start))
713 (let ((end (match-end 0))
714 (dest (eword-decode-encoded-word (match-string 0 string)
717 ;;(setq string (substring string end))
719 (while (and (string-match (eval-when-compile
720 (concat "[ \t\n]*\\("
721 eword-encoded-word-regexp
724 (= (match-beginning 0) start))
725 (setq end (match-end 0))
728 (eword-decode-encoded-word (match-string 1 string)
730 ;;string (substring string end))
733 (cons (cons 'atom dest) ;;string)
737 (defun eword-analyze-atom (string start &optional must-unfold)
738 (if (and (string-match std11-atom-regexp string start)
739 (= (match-beginning 0) start))
740 (let ((end (match-end 0)))
741 (cons (cons 'atom (decode-mime-charset-string
742 (substring string start end)
743 default-mime-charset))
744 ;;(substring string end)
748 (defun eword-lexical-analyze-internal (string start must-unfold)
749 (let ((len (length string))
753 (let ((rest mime-header-lexical-analyzer)
755 (while (and (setq func (car rest))
757 (setq r (funcall func string start must-unfold)))
759 (setq rest (cdr rest)))
761 (cons (cons 'error (substring string start)) (1+ len)))
763 (setq dest (cons (car ret) dest)
769 (defun eword-lexical-analyze (string &optional start must-unfold)
770 "Return lexical analyzed list corresponding STRING.
771 It is like std11-lexical-analyze, but it decodes non us-ascii
772 characters encoded as encoded-words or invalid \"raw\" format.
773 \"Raw\" non us-ascii characters are regarded as variable
774 `default-mime-charset'."
775 (let ((key (substring string (or start 0)))
777 (set-text-properties 0 (length key) nil key)
778 (if (setq ret (assoc key eword-lexical-analyze-cache))
780 (setq ret (eword-lexical-analyze-internal key 0 must-unfold))
781 (setq eword-lexical-analyze-cache
783 eword-lexical-analyze-cache))
784 (if (cdr (setq cell (nthcdr eword-lexical-analyze-cache-max
785 eword-lexical-analyze-cache)))
789 (defun eword-decode-token (token)
790 (let ((type (car token))
792 (cond ((eq type 'quoted-string)
793 (std11-wrap-as-quoted-string value))
797 (setq dest (concat dest
798 (if (stringp (car value))
799 (std11-wrap-as-quoted-pairs
800 (car value) '(?( ?)))
801 (eword-decode-token (car value))
805 (concat "(" dest ")")
809 (defun eword-extract-address-components (string &optional start)
810 "Extract full name and canonical address from STRING.
811 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
812 If no name can be extracted, FULL-NAME will be nil.
813 It decodes non us-ascii characters in FULL-NAME encoded as
814 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
815 characters are regarded as variable `default-mime-charset'."
816 (let* ((structure (car (std11-parse-address
817 (eword-lexical-analyze
818 (std11-unfold-string string) start
820 (phrase (std11-full-name-string structure))
821 (address (std11-address-string structure))
823 (list phrase address)
830 (provide 'eword-decode)
832 ;;; eword-decode.el ends here