e1d50ed07f9dcc2a6cd490d9f4b951e0dc894f08
[elisp/gnus.git-] / lisp / mm-decode.el
1 ;;; mm-decode.el --- Function for decoding MIME things
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is not yet part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'base64)
27 (require 'qp)
28 (require 'nnheader)
29
30 (defvar mm-charset-regexp (concat "[^" "][\000-\040()<>@,\;:\\\"/?.=" "]+"))
31
32 (defvar mm-encoded-word-regexp
33   (concat "=\\?\\(" mm-charset-regexp "\\)\\?\\(B\\|Q\\)\\?"
34           "\\([!->@-~]+\\)\\?="))
35
36 (defun mm-decode-words-region (start end)
37   "Decode MIME-encoded words in region between START and END."
38   (interactive "r")
39   (save-excursion
40     (save-restriction
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 "\\)"
46                       "\\(\n?[ \t]\\)+"
47                       "\\(" mm-encoded-word-regexp "\\)")
48               nil t)
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
54                  (prog1
55                      (match-string 0)
56                    (delete-region (match-beginning 0) (match-end 0)))))))))
57
58 (defun mm-decode-words-string (string)
59  "Decode the quoted-printable-encoded STRING and return the results."
60  (with-temp-buffer
61    (insert string)
62    (inline
63      (mm-decode-words-region (point-min) (point-max)))
64    (buffer-string)))
65
66 (defun mm-decode-word (word)
67   "Decode WORD and return it if it is an encoded word.
68 Return WORD if not."
69   (if (not (string-match mm-encoded-word-regexp word))
70       word
71     (or
72      (condition-case nil
73          (mm-decode-text
74           (match-string 1 word)
75           (upcase (match-string 2 word))
76           (match-string 3 word))
77        (error word))
78      word)))
79
80 (eval-and-compile
81   (if (fboundp 'decode-coding-string)
82       (fset 'mm-decode-coding-string 'decode-coding-string)
83     (fset 'mm-decode-coding-string (lambda (s a) s))))
84
85 (defun mm-decode-text (charset encoding string)
86   "Decode STRING as an encoded text.
87 Valid ENCODINGs are \"B\" and \"Q\".
88 If your Emacs implementation can't decode CHARSET, it returns nil."
89   (let ((cs (mm-charset-to-coding-system charset)))
90     (when cs
91       (mm-decode-coding-string
92        (cond
93         ((equal "B" encoding)
94          (base64-decode string))
95         ((equal "Q" encoding)
96          (quoted-printable-decode-string
97           (nnheader-replace-chars-in-string string ?_ ? )))
98         (t (error "Invalid encoding: %s" encoding)))
99        cs))))
100
101 (defvar mm-charset-coding-system-alist
102   (let ((rest
103          '((us-ascii . iso-8859-1)
104            (gb2312 . cn-gb-2312)
105            (iso-2022-jp-2 . iso-2022-7bit-ss2)
106            (x-ctext . ctext)))
107         (systems (coding-system-list))
108         dest)
109     (while rest
110       (let ((pair (car rest)))
111         (unless (memq (car pair) systems)
112           (setq dest (cons pair dest))))
113       (setq rest (cdr rest)))
114     dest)
115   "Charset/coding system alist.")
116
117 (defun mm-charset-to-coding-system (charset &optional lbt)
118   "Return coding-system corresponding to CHARSET.
119 CHARSET is a symbol naming a MIME charset.
120 If optional argument LBT (`unix', `dos' or `mac') is specified, it is
121 used as the line break code type of the coding system."
122   (when (stringp charset)
123     (setq charset (intern (downcase charset))))
124   (setq charset
125         (or (cdr (assq charset mm-charset-coding-system-alist))
126             charset))
127   (when lbt
128     (setq charset (intern (format "%s-%s" charset lbt))))
129   (when (memq charset (coding-system-list))
130     charset))
131
132 (provide 'mm-decode)
133
134 ;; qp.el ends here