*** empty log message ***
[gnus] / 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 (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)))
85     (when cs
86       (decode-coding-string
87        (cond
88         ((equal "B" encoding)
89          (base64-decode string))
90         ((equal "Q" encoding)
91          (quoted-printable-decode-string
92           (nnheader-replace-chars-in-string string ?_ ? )))
93         (t (error "Invalid encoding: %s" encoding)))
94        cs))))
95
96 (defvar mm-charset-coding-system-alist
97   (let ((rest
98          '((us-ascii . iso-8859-1)
99            (gb2312 . cn-gb-2312)
100            (iso-2022-jp-2 . iso-2022-7bit-ss2)
101            (x-ctext . ctext)))
102         dest)
103     (while rest
104       (let ((pair (car rest)))
105         (unless (coding-system-p (car pair))
106           (setq dest (cons pair dest))))
107       (setq rest (cdr rest)))
108     dest)
109   "Charset/coding system alist.")
110
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))))
118   (setq charset
119         (or (cdr (assq charset mm-charset-coding-system-alist))
120             charset))
121   (when lbt
122     (setq charset (intern (format "%s-%s" charset lbt))))
123   (when (memq charset (coding-system-list))
124     charset))
125
126 (provide 'mm-decode)
127
128 ;; qp.el ends here