mm-bodies.el 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ;;; mm-bodies.el --- Functions for decoding MIME things
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; For Emacs <22.2 and XEmacs.
  19. (eval-and-compile
  20. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  21. (require 'mm-util)
  22. (require 'rfc2047)
  23. (require 'mm-encode)
  24. (defvar mm-uu-yenc-decode-function)
  25. (defvar mm-uu-decode-function)
  26. (defvar mm-uu-binhex-decode-function)
  27. ;; 8bit treatment gets any char except: 0x32 - 0x7f, LF, TAB, BEL,
  28. ;; BS, vertical TAB, form feed, and ^_
  29. ;;
  30. ;; Note that CR is *not* included, as that would allow a non-paired CR
  31. ;; in the body contrary to RFC 2822:
  32. ;;
  33. ;; - CR and LF MUST only occur together as CRLF; they MUST NOT
  34. ;; appear independently in the body.
  35. (defvar mm-7bit-chars "\x20-\x7f\n\t\x7\x8\xb\xc\x1f")
  36. (defcustom mm-body-charset-encoding-alist
  37. '((iso-2022-jp . 7bit)
  38. (iso-2022-jp-2 . 7bit)
  39. ;; We MUST encode UTF-16 because it can contain \0's which is
  40. ;; known to break servers.
  41. ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
  42. ;; so this can't happen :-/.
  43. ;; PPS: Yes, it can happen if the user specifies UTF-16 in the MML
  44. ;; markup. - jh.
  45. (utf-16 . base64)
  46. (utf-16be . base64)
  47. (utf-16le . base64))
  48. "Alist of MIME charsets to encodings.
  49. Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
  50. :type '(repeat (cons (symbol :tag "charset")
  51. (choice :tag "encoding"
  52. (const 7bit)
  53. (const 8bit)
  54. (const quoted-printable)
  55. (const base64))))
  56. :group 'mime)
  57. (autoload 'message-options-get "message")
  58. (declare-function message-options-set "message" (symbol value))
  59. (defun mm-encode-body (&optional charset)
  60. "Encode a body.
  61. Should be called narrowed to the body that is to be encoded.
  62. If there is more than one non-ASCII MULE charset in the body, then the
  63. list of MULE charsets found is returned.
  64. If CHARSET is non-nil, it is used as the MIME charset to encode the body.
  65. If successful, the MIME charset is returned.
  66. If no encoding was done, nil is returned."
  67. (if (not (mm-multibyte-p))
  68. ;; In the non-Mule case, we search for non-ASCII chars and
  69. ;; return the value of `mail-parse-charset' if any are found.
  70. (or charset
  71. (save-excursion
  72. (goto-char (point-min))
  73. (if (re-search-forward "[^\x0-\x7f]" nil t)
  74. (or mail-parse-charset
  75. (message-options-get 'mm-body-charset-encoding-alist)
  76. (message-options-set
  77. 'mm-body-charset-encoding-alist
  78. (mm-read-coding-system "Charset used in the article: ")))
  79. ;; The logic in `mml-generate-mime-1' confirms that it's OK
  80. ;; to return nil here.
  81. nil)))
  82. (save-excursion
  83. (if charset
  84. (progn
  85. (mm-encode-coding-region (point-min) (point-max)
  86. (mm-charset-to-coding-system charset))
  87. charset)
  88. (goto-char (point-min))
  89. (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
  90. mm-hack-charsets)))
  91. (cond
  92. ;; No encoding.
  93. ((null charsets)
  94. nil)
  95. ;; Too many charsets.
  96. ((> (length charsets) 1)
  97. charsets)
  98. ;; We encode.
  99. (t
  100. (prog1
  101. (setq charset (car charsets))
  102. (mm-encode-coding-region (point-min) (point-max)
  103. (mm-charset-to-coding-system charset))))
  104. ))))))
  105. (defun mm-long-lines-p (length)
  106. "Say whether any of the lines in the buffer is longer than LENGTH."
  107. (save-excursion
  108. (goto-char (point-min))
  109. (end-of-line)
  110. (while (and (not (eobp))
  111. (not (> (current-column) length)))
  112. (forward-line 1)
  113. (end-of-line))
  114. (and (> (current-column) length)
  115. (current-column))))
  116. (defvar message-posting-charset)
  117. (defun mm-body-encoding (charset &optional encoding)
  118. "Do Content-Transfer-Encoding and return the encoding of the current buffer."
  119. (when (stringp encoding)
  120. (setq encoding (intern (downcase encoding))))
  121. (let ((bits (mm-body-7-or-8))
  122. (longp (mm-long-lines-p 1000)))
  123. (require 'message)
  124. (cond
  125. ((and (not longp)
  126. (not (and mm-use-ultra-safe-encoding
  127. (or (save-excursion (re-search-forward " $" nil t))
  128. (save-excursion (re-search-forward "^From " nil t)))))
  129. (eq bits '7bit))
  130. bits)
  131. ((and (not mm-use-ultra-safe-encoding)
  132. (not longp)
  133. (not (cdr (assq charset mm-body-charset-encoding-alist)))
  134. (or (eq t (cdr message-posting-charset))
  135. (memq charset (cdr message-posting-charset))
  136. (eq charset mail-parse-charset)))
  137. bits)
  138. (t
  139. (let ((encoding (or encoding
  140. (cdr (assq charset mm-body-charset-encoding-alist))
  141. (mm-qp-or-base64))))
  142. (when mm-use-ultra-safe-encoding
  143. (setq encoding (mm-safer-encoding encoding)))
  144. (mm-encode-content-transfer-encoding encoding "text/plain")
  145. encoding)))))
  146. (defun mm-body-7-or-8 ()
  147. "Say whether the body is 7bit or 8bit."
  148. (if (save-excursion
  149. (goto-char (point-min))
  150. (skip-chars-forward mm-7bit-chars)
  151. (eobp))
  152. '7bit
  153. '8bit))
  154. ;;;
  155. ;;; Functions for decoding
  156. ;;;
  157. (defun mm-decode-content-transfer-encoding (encoding &optional type)
  158. "Decodes buffer encoded with ENCODING, returning success status.
  159. If TYPE is `text/plain' CRLF->LF translation may occur."
  160. (prog1
  161. (condition-case error
  162. (cond
  163. ((eq encoding 'quoted-printable)
  164. (quoted-printable-decode-region (point-min) (point-max))
  165. t)
  166. ((eq encoding 'base64)
  167. (base64-decode-region
  168. (point-min)
  169. ;; Some mailers insert whitespace
  170. ;; junk at the end which
  171. ;; base64-decode-region dislikes.
  172. ;; Also remove possible junk which could
  173. ;; have been added by mailing list software.
  174. (save-excursion
  175. (goto-char (point-min))
  176. (while (re-search-forward "^[\t ]*\r?\n" nil t)
  177. (delete-region (match-beginning 0) (match-end 0)))
  178. (goto-char (point-max))
  179. (when (re-search-backward "^[\t ]*[A-Za-z0-9+/]+=*[\t ]*$"
  180. nil t)
  181. (forward-line))
  182. (point))))
  183. ((memq encoding '(nil 7bit 8bit binary))
  184. ;; Do nothing.
  185. t)
  186. ((memq encoding '(x-uuencode x-uue))
  187. (require 'mm-uu)
  188. (funcall mm-uu-decode-function (point-min) (point-max))
  189. t)
  190. ((eq encoding 'x-binhex)
  191. (require 'mm-uu)
  192. (funcall mm-uu-binhex-decode-function (point-min) (point-max))
  193. t)
  194. ((eq encoding 'x-yenc)
  195. (require 'mm-uu)
  196. (funcall mm-uu-yenc-decode-function (point-min) (point-max))
  197. )
  198. ((functionp encoding)
  199. (funcall encoding (point-min) (point-max))
  200. t)
  201. (t
  202. (message "Unknown encoding %s; defaulting to 8bit" encoding)))
  203. (error
  204. (message "Error while decoding: %s" error)
  205. nil))
  206. (when (and
  207. type
  208. (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
  209. (string-match "\\`text/" type))
  210. (goto-char (point-min))
  211. (while (search-forward "\r\n" nil t)
  212. (replace-match "\n" t t)))))
  213. (defun mm-decode-body (charset &optional encoding type)
  214. "Decode the current article that has been encoded with ENCODING to CHARSET.
  215. ENCODING is a MIME content transfer encoding.
  216. CHARSET is the MIME charset with which to decode the data after transfer
  217. decoding. If it is nil, default to `mail-parse-charset'."
  218. (when (stringp charset)
  219. (setq charset (intern (downcase charset))))
  220. (when (or (not charset)
  221. (eq 'gnus-all mail-parse-ignored-charsets)
  222. (memq 'gnus-all mail-parse-ignored-charsets)
  223. (memq charset mail-parse-ignored-charsets))
  224. (setq charset mail-parse-charset))
  225. (save-excursion
  226. (when encoding
  227. (mm-decode-content-transfer-encoding encoding type))
  228. (when (and (featurep 'mule) ;; Fixme: Wrong test for unibyte session.
  229. (not (eq charset 'gnus-decoded)))
  230. (let ((coding-system (mm-charset-to-coding-system
  231. ;; Allow overwrite using
  232. ;; `mm-charset-override-alist'.
  233. charset nil t)))
  234. (if (and (not coding-system)
  235. (listp mail-parse-ignored-charsets)
  236. (memq 'gnus-unknown mail-parse-ignored-charsets))
  237. (setq coding-system
  238. (mm-charset-to-coding-system mail-parse-charset)))
  239. (when (and charset coding-system
  240. ;; buffer-file-coding-system
  241. ;;Article buffer is nil coding system
  242. ;;in XEmacs
  243. (mm-multibyte-p)
  244. (or (not (eq coding-system 'ascii))
  245. (setq coding-system mail-parse-charset)))
  246. (mm-decode-coding-region (point-min) (point-max)
  247. coding-system))
  248. (setq buffer-file-coding-system
  249. (if (boundp 'last-coding-system-used)
  250. (symbol-value 'last-coding-system-used)
  251. coding-system))))))
  252. (defun mm-decode-string (string charset)
  253. "Decode STRING with CHARSET."
  254. (when (stringp charset)
  255. (setq charset (intern (downcase charset))))
  256. (when (or (not charset)
  257. (eq 'gnus-all mail-parse-ignored-charsets)
  258. (memq 'gnus-all mail-parse-ignored-charsets)
  259. (memq charset mail-parse-ignored-charsets))
  260. (setq charset mail-parse-charset))
  261. (or
  262. (when (featurep 'mule)
  263. (let ((coding-system (mm-charset-to-coding-system
  264. charset
  265. ;; Allow overwrite using
  266. ;; `mm-charset-override-alist'.
  267. nil t)))
  268. (if (and (not coding-system)
  269. (listp mail-parse-ignored-charsets)
  270. (memq 'gnus-unknown mail-parse-ignored-charsets))
  271. (setq coding-system
  272. (mm-charset-to-coding-system mail-parse-charset)))
  273. (when (and charset coding-system
  274. (mm-multibyte-p)
  275. (or (not (eq coding-system 'ascii))
  276. (setq coding-system mail-parse-charset)))
  277. (mm-decode-coding-string string coding-system))))
  278. string))
  279. (provide 'mm-bodies)
  280. ;;; mm-bodies.el ends here