1 ;;; mm-decode.el --- Function for decoding MIME things
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is not yet part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
30 (defvar mm-charset-regexp (concat "[^" "][\000-\040()<>@,\;:\\\"/?.=" "]+"))
32 (defvar mm-encoded-word-regexp
33 (concat "=\\?\\(" mm-charset-regexp "\\)\\?\\(B\\|Q\\)\\?"
34 "\\([!->@-~]+\\)\\?="))
36 (defun mm-decode-words-region (start end)
37 "Decode MIME-encoded words in region between START and END."
41 (narrow-to-region start end)
42 (goto-char (point-min))
43 ;; Remove whitespace between encoded words.
44 (while (re-search-forward
45 (concat "\\(" mm-encoded-word-regexp "\\)"
47 "\\(" mm-encoded-word-regexp "\\)")
49 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
50 ;; Decode the encoded words.
51 (goto-char (point-min))
52 (while (re-search-forward mm-encoded-word-regexp nil t)
53 (insert (mm-decode-word
56 (delete-region (match-beginning 0) (match-end 0)))))))))
58 (defun mm-decode-words-string (string)
59 "Decode the quoted-printable-encoded STRING and return the results."
63 (mm-decode-words-region (point-min) (point-max)))
66 (defun mm-decode-word (word)
67 "Decode WORD and return it if it is an encoded word.
69 (if (not (string-match mm-encoded-word-regexp word))
75 (upcase (match-string 2 word))
76 (match-string 3 word))
80 (defun mm-decode-text (charset encoding string)
81 "Decode STRING as an encoded text.
82 Valid ENCODINGs are \"B\" and \"Q\".
83 If your Emacs implementation can't decode CHARSET, it returns nil."
84 (let ((cs (mm-charset-to-coding-system charset)))
89 (base64-decode string))
91 (quoted-printable-decode-string
92 (nnheader-replace-chars-in-string string ?_ ? )))
93 (t (error "Invalid encoding: %s" encoding)))
96 (defvar mm-charset-coding-system-alist
98 '((us-ascii . iso-8859-1)
100 (iso-2022-jp-2 . iso-2022-7bit-ss2)
104 (let ((pair (car rest)))
105 (unless (coding-system-p (car pair))
106 (setq dest (cons pair dest))))
107 (setq rest (cdr rest)))
109 "Charset/coding system alist.")
111 (defun mm-charset-to-coding-system (charset &optional lbt)
112 "Return coding-system corresponding to CHARSET.
113 CHARSET is a symbol naming a MIME charset.
114 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
115 used as the line break code type of the coding system."
116 (when (stringp charset)
117 (setq charset (intern (downcase charset))))
119 (or (cdr (assq charset mm-charset-coding-system-alist))
122 (setq charset (intern (format "%s-%s" charset lbt))))
123 (when (memq charset (coding-system-list))