b41d5de676e9b5c36403f8583dbb7c2704658308
[gnus] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (or (fboundp  'base64-decode-region)
29       (require 'base64))
30   (autoload 'binhex-decode-region "binhex"))
31
32 (require 'mm-util)
33 (require 'rfc2047)
34 (require 'qp)
35 (require 'uudecode)
36
37 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
38 ;; BS, vertical TAB, form feed, and ^_
39 (defvar mm-8bit-char-regexp "[^\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f]")
40
41 (defun mm-encode-body ()
42   "Encode a body.
43 Should be called narrowed to the body that is to be encoded.
44 If there is more than one non-ASCII MULE charset, then list of found
45 MULE charsets are returned.
46 If successful, the MIME charset is returned.
47 If no encoding was done, nil is returned."
48   (if (not (featurep 'mule))
49       ;; In the non-Mule case, we search for non-ASCII chars and
50       ;; return the value of `mm-default-charset' if any are found.
51       (save-excursion
52         (goto-char (point-min))
53         (if (re-search-forward "[^\x0-\x7f]" nil t)
54             (mm-read-charset "Charset used in the article: ")
55           ;; The logic in `mml-generate-mime-1' confirms that it's OK
56           ;; to return nil here.
57           nil))
58     (save-excursion
59       (goto-char (point-min))
60       (let ((charsets
61              (delq 'ascii (mm-find-charset-region (point-min) (point-max))))
62             charset)
63         (cond
64          ;; No encoding.
65          ((null charsets)
66           nil)
67          ;; Too many charsets.
68          ((> (length charsets) 1)
69           charsets)
70          ;; We encode.
71          (t
72           (let ((mime-charset (mm-mime-charset (car charsets)))
73                 start)
74             (when (or t
75                       ;; We always decode.
76                       (not (mm-coding-system-equal
77                             mime-charset buffer-file-coding-system)))
78               (while (not (eobp))
79                 (if (eq (char-charset (char-after)) 'ascii)
80                     (when start
81                       (save-restriction
82                         (narrow-to-region start (point))
83                         (mm-encode-coding-region start (point) mime-charset)
84                         (goto-char (point-max)))
85                       (setq start nil))
86                   (unless start
87                     (setq start (point))))
88                 (forward-char 1))
89               (when start
90                 (mm-encode-coding-region start (point) mime-charset)
91                 (setq start nil)))
92             mime-charset)))))))
93
94 (defun mm-body-encoding ()
95   "Return the encoding of the current buffer."
96   (cond
97    ((not (featurep 'mule))
98     (if (save-excursion
99           (goto-char (point-min))
100           (re-search-forward mm-8bit-char-regexp nil t))
101         '8bit
102       '7bit))
103    (t
104     ;; Mule version
105     (if (and (null (delq 'ascii
106                          (mm-find-charset-region (point-min) (point-max))))
107              ;;!!!The following is necessary because the function
108              ;;!!!above seems to return the wrong result under
109              ;;!!!Emacs 20.3.  Sometimes.
110              (save-excursion
111                (goto-char (point-min))
112                (skip-chars-forward "\0-\177")
113                (eobp)))
114         '7bit
115       '8bit))))
116
117 ;;;
118 ;;; Functions for decoding
119 ;;;
120
121 (defun mm-decode-content-transfer-encoding (encoding &optional type)
122   (prog1
123       (condition-case error
124           (cond
125            ((eq encoding 'quoted-printable)
126             (quoted-printable-decode-region (point-min) (point-max)))
127            ((eq encoding 'base64)
128             (base64-decode-region (point-min) (point-max)))
129            ((memq encoding '(7bit 8bit binary))
130             )
131            ((null encoding)
132             )
133            ((eq encoding 'x-uuencode)
134             (funcall mm-uu-decode-function (point-min) (point-max)))
135            ((eq encoding 'x-binhex)
136             (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
137            ((functionp encoding)
138             (funcall encoding (point-min) (point-max)))
139            (t
140             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
141         (error
142          (message "Error while decoding: %s" error)
143          nil))
144     (when (and
145            (memq encoding '(base64 x-uuencode x-binhex))
146            (equal type "text/plain"))
147       (goto-char (point-min))
148       (while (search-forward "\r\n" nil t)
149         (replace-match "\n" t t)))))
150
151 (defun mm-decode-body (charset &optional encoding type)
152   "Decode the current article that has been encoded with ENCODING.
153 The characters in CHARSET should then be decoded."
154   (setq charset (or charset mail-parse-charset))
155   (save-excursion
156     (when encoding
157       (mm-decode-content-transfer-encoding encoding type))
158     (when (featurep 'mule)
159       (let (mule-charset)
160         (when (and charset
161                    (setq mule-charset (mm-charset-to-coding-system charset))
162                    ;; buffer-file-coding-system
163                                         ;Article buffer is nil coding system
164                                         ;in XEmacs
165                    enable-multibyte-characters
166                    (or (not (eq mule-charset 'ascii))
167                        (setq mule-charset mail-parse-charset)))
168           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
169
170 (defun mm-decode-string (string charset)
171   "Decode STRING with CHARSET."
172   (setq charset (or charset mail-parse-charset))
173   (or
174    (when (featurep 'mule)
175      (let (mule-charset)
176        (when (and charset
177                   (setq mule-charset (mm-charset-to-coding-system charset))
178                   enable-multibyte-characters
179                   (or (not (eq mule-charset 'ascii))
180                       (setq mule-charset mail-parse-charset)))
181          (mm-decode-coding-string string mule-charset))))
182    string))
183
184 (provide 'mm-bodies)
185
186 ;; mm-bodies.el ends here