rmailmm.el 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. ;;; rmailmm.el --- MIME decoding and display stuff for RMAIL
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Alexander Pohoyda
  4. ;; Alex Schroeder
  5. ;; Maintainer: FSF
  6. ;; Keywords: mail
  7. ;; Package: rmail
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Essentially based on the design of Alexander Pohoyda's MIME
  21. ;; extensions (mime-display.el and mime.el).
  22. ;; This file provides two operation modes for viewing a MIME message.
  23. ;; (1) When rmail-enable-mime is non-nil (now it is the default), the
  24. ;; function `rmail-show-mime' is automatically called. That function
  25. ;; shows a MIME message directly in RMAIL's view buffer.
  26. ;; (2) When rmail-enable-mime is nil, the command 'v' (or M-x
  27. ;; rmail-mime) shows a MIME message in a new buffer "*RMAIL*".
  28. ;; Both operations share the intermediate functions rmail-mime-process
  29. ;; and rmail-mime-process-multipart as below.
  30. ;; rmail-show-mime
  31. ;; +- rmail-mime-parse
  32. ;; | +- rmail-mime-process <--+------------+
  33. ;; | | +---------+ |
  34. ;; | + rmail-mime-process-multipart --+
  35. ;; |
  36. ;; + rmail-mime-insert <----------------+
  37. ;; +- rmail-mime-insert-text |
  38. ;; +- rmail-mime-insert-bulk |
  39. ;; +- rmail-mime-insert-multipart --+
  40. ;;
  41. ;; rmail-mime
  42. ;; +- rmail-mime-show <----------------------------------+
  43. ;; +- rmail-mime-process |
  44. ;; +- rmail-mime-handle |
  45. ;; +- rmail-mime-text-handler |
  46. ;; +- rmail-mime-bulk-handler |
  47. ;; | + rmail-mime-insert-bulk
  48. ;; +- rmail-mime-multipart-handler |
  49. ;; +- rmail-mime-process-multipart --+
  50. ;; In addition, for the case of rmail-enable-mime being non-nil, this
  51. ;; file provides two functions rmail-insert-mime-forwarded-message and
  52. ;; rmail-insert-mime-resent-message for composing forwarded and resent
  53. ;; messages respectively.
  54. ;; Todo:
  55. ;; Make rmail-mime-media-type-handlers-alist usable in the first
  56. ;; operation mode.
  57. ;; Handle multipart/alternative in the second operation mode.
  58. ;; Offer the option to call external/internal viewers (doc-view, xpdf, etc).
  59. ;;; Code:
  60. (require 'rmail)
  61. (require 'mail-parse)
  62. (require 'message)
  63. ;;; User options.
  64. (defgroup rmail-mime nil
  65. "Rmail MIME handling options."
  66. :prefix "rmail-mime-"
  67. :group 'rmail)
  68. (defcustom rmail-mime-media-type-handlers-alist
  69. '(("multipart/.*" rmail-mime-multipart-handler)
  70. ("text/.*" rmail-mime-text-handler)
  71. ("text/\\(x-\\)?patch" rmail-mime-bulk-handler)
  72. ("\\(image\\|audio\\|video\\|application\\)/.*" rmail-mime-bulk-handler))
  73. "Functions to handle various content types.
  74. This is an alist with elements of the form (REGEXP FUNCTION ...).
  75. The first item is a regular expression matching a content-type.
  76. The remaining elements are handler functions to run, in order of
  77. decreasing preference. These are called until one returns non-nil.
  78. Note that this only applies to items with an inline Content-Disposition,
  79. all others are handled by `rmail-mime-bulk-handler'.
  80. Note also that this alist is ignored when the variable
  81. `rmail-enable-mime' is non-nil."
  82. :type '(alist :key-type regexp :value-type (repeat function))
  83. :version "23.1"
  84. :group 'rmail-mime)
  85. (defcustom rmail-mime-attachment-dirs-alist
  86. `(("text/.*" "~/Documents")
  87. ("image/.*" "~/Pictures")
  88. (".*" "~/Desktop" "~" ,temporary-file-directory))
  89. "Default directories to save attachments of various types into.
  90. This is an alist with elements of the form (REGEXP DIR ...).
  91. The first item is a regular expression matching a content-type.
  92. The remaining elements are directories, in order of decreasing preference.
  93. The first directory that exists is used."
  94. :type '(alist :key-type regexp :value-type (repeat directory))
  95. :version "23.1"
  96. :group 'rmail-mime)
  97. (defcustom rmail-mime-show-images 'button
  98. "What to do with image attachments that Emacs is capable of displaying.
  99. If nil, do nothing special. If `button', add an extra button
  100. that when pushed displays the image in the buffer. If a number,
  101. automatically show images if they are smaller than that size (in
  102. bytes), otherwise add a display button. Anything else means to
  103. automatically display the image in the buffer."
  104. :type '(choice (const :tag "Add button to view image" button)
  105. (const :tag "No special treatment" nil)
  106. (number :tag "Show if smaller than certain size")
  107. (other :tag "Always show" show))
  108. :version "23.2"
  109. :group 'rmail-mime)
  110. ;;; End of user options.
  111. ;;; Global variables that always have let-binding when referred.
  112. (defvar rmail-mime-mbox-buffer nil
  113. "Buffer containing the mbox data.
  114. The value is usually nil, and bound to a proper value while
  115. processing MIME.")
  116. (defvar rmail-mime-view-buffer nil
  117. "Buffer showing a message.
  118. The value is usually nil, and bound to a proper value while
  119. processing MIME.")
  120. (defvar rmail-mime-coding-system nil
  121. "The first coding-system used for decoding a MIME entity.
  122. The value is usually nil, and bound to non-nil while inserting
  123. MIME entities.")
  124. ;;; MIME-entity object
  125. (defun rmail-mime-entity (type disposition transfer-encoding
  126. display header tagline body children handler
  127. &optional truncated)
  128. "Return a newly created MIME-entity object from arguments.
  129. A MIME-entity is a vector of 10 elements:
  130. [TYPE DISPOSITION TRANSFER-ENCODING DISPLAY HEADER TAGLINE BODY
  131. CHILDREN HANDLER TRUNCATED]
  132. TYPE and DISPOSITION correspond to MIME headers Content-Type and
  133. Content-Disposition respectively, and have this format:
  134. \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
  135. Each VALUE is a string and each ATTRIBUTE is a string.
  136. Consider the following header, for example:
  137. Content-Type: multipart/mixed;
  138. boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
  139. The corresponding TYPE argument must be:
  140. \(\"multipart/mixed\"
  141. \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))
  142. TRANSFER-ENCODING corresponds to MIME header
  143. Content-Transfer-Encoding, and is a lower-case string.
  144. DISPLAY is a vector [CURRENT NEW], where CURRENT indicates how
  145. the header, tag line, and body of the entity are displayed now,
  146. and NEW indicates how their display should be updated.
  147. Both elements are vectors [HEADER-DISPLAY TAGLINE-DISPLAY BODY-DISPLAY],
  148. where each constituent element is a symbol for the corresponding
  149. item with these values:
  150. nil: not displayed
  151. t: displayed by the decoded presentation form
  152. raw: displayed by the raw MIME data (for the header and body only)
  153. HEADER and BODY are vectors [BEG END DISPLAY-FLAG], where BEG and
  154. END are markers that specify the region of the header or body lines
  155. in RMAIL's data (mbox) buffer, and DISPLAY-FLAG non-nil means that the
  156. header or body is, by default, displayed by the decoded
  157. presentation form.
  158. TAGLINE is a vector [TAG BULK-DATA DISPLAY-FLAG], where TAG is a
  159. string indicating the depth and index number of the entity,
  160. BULK-DATA is a cons (SIZE . TYPE) indicating the size and type of
  161. an attached data, DISPLAY-FLAG non-nil means that the tag line is
  162. displayed by default.
  163. CHILDREN is a list of child MIME-entities. A \"multipart/*\"
  164. entity has one or more children. A \"message/rfc822\" entity
  165. has just one child. Any other entity has no child.
  166. HANDLER is a function to insert the entity according to DISPLAY.
  167. It is called with one argument ENTITY.
  168. TRUNCATED is non-nil if the text of this entity was truncated."
  169. (vector type disposition transfer-encoding
  170. display header tagline body children handler truncated))
  171. ;; Accessors for a MIME-entity object.
  172. (defsubst rmail-mime-entity-type (entity) (aref entity 0))
  173. (defsubst rmail-mime-entity-disposition (entity) (aref entity 1))
  174. (defsubst rmail-mime-entity-transfer-encoding (entity) (aref entity 2))
  175. (defsubst rmail-mime-entity-display (entity) (aref entity 3))
  176. (defsubst rmail-mime-entity-header (entity) (aref entity 4))
  177. (defsubst rmail-mime-entity-tagline (entity) (aref entity 5))
  178. (defsubst rmail-mime-entity-body (entity) (aref entity 6))
  179. (defsubst rmail-mime-entity-children (entity) (aref entity 7))
  180. (defsubst rmail-mime-entity-handler (entity) (aref entity 8))
  181. (defsubst rmail-mime-entity-truncated (entity) (aref entity 9))
  182. (defsubst rmail-mime-entity-set-truncated (entity truncated)
  183. (aset entity 9 truncated))
  184. (defsubst rmail-mime-message-p ()
  185. "Non-nil if and only if the current message is a MIME."
  186. (or (get-text-property (point) 'rmail-mime-entity)
  187. (get-text-property (point-min) 'rmail-mime-entity)))
  188. ;;; Buttons
  189. (defun rmail-mime-save (button)
  190. "Save the attachment using info in the BUTTON."
  191. (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
  192. (filename (button-get button 'filename))
  193. (directory (button-get button 'directory))
  194. (data (button-get button 'data))
  195. (ofilename filename))
  196. (if (and (not (stringp data))
  197. (rmail-mime-entity-truncated data))
  198. (unless (y-or-n-p "This entity is truncated; save anyway? ")
  199. (error "Aborted")))
  200. (setq filename (expand-file-name
  201. (read-file-name (format "Save as (default: %s): " filename)
  202. directory
  203. (expand-file-name filename directory))
  204. directory))
  205. ;; If arg is just a directory, use the default file name, but in
  206. ;; that directory (copied from write-file).
  207. (if (file-directory-p filename)
  208. (setq filename (expand-file-name
  209. (file-name-nondirectory ofilename)
  210. (file-name-as-directory filename))))
  211. (with-temp-buffer
  212. (set-buffer-file-coding-system 'no-conversion)
  213. ;; Needed e.g. by jka-compr, so if the attachment is a compressed
  214. ;; file, the magic signature compares equal with the unibyte
  215. ;; signature string recorded in jka-compr-compression-info-list.
  216. (set-buffer-multibyte nil)
  217. (setq buffer-undo-list t)
  218. (if (stringp data)
  219. (insert data)
  220. ;; DATA is a MIME-entity object.
  221. (let ((transfer-encoding (rmail-mime-entity-transfer-encoding data))
  222. (body (rmail-mime-entity-body data)))
  223. (insert-buffer-substring rmail-mime-mbox-buffer
  224. (aref body 0) (aref body 1))
  225. (cond ((string= transfer-encoding "base64")
  226. (ignore-errors (base64-decode-region (point-min) (point-max))))
  227. ((string= transfer-encoding "quoted-printable")
  228. (quoted-printable-decode-region (point-min) (point-max))))))
  229. (write-region nil nil filename nil nil nil t))))
  230. (define-button-type 'rmail-mime-save 'action 'rmail-mime-save)
  231. ;; Display options returned by rmail-mime-entity-display.
  232. ;; Value is on of nil, t, raw.
  233. (defsubst rmail-mime-display-header (disp) (aref disp 0))
  234. (defsubst rmail-mime-display-tagline (disp) (aref disp 1))
  235. (defsubst rmail-mime-display-body (disp) (aref disp 2))
  236. (defun rmail-mime-entity-segment (pos &optional entity)
  237. "Return a vector describing the displayed region of a MIME-entity at POS.
  238. Optional 2nd argument ENTITY is the MIME-entity at POS.
  239. The value is a vector [INDEX HEADER TAGLINE BODY END], where
  240. INDEX: index into the returned vector indicating where POS is (1..3)
  241. HEADER: the position of the beginning of a header
  242. TAGLINE: the position of the beginning of a tag line, including
  243. the newline that precedes it
  244. BODY: the position of the beginning of a body
  245. END: the position of the end of the entity."
  246. (save-excursion
  247. (or entity
  248. (setq entity (get-text-property pos 'rmail-mime-entity)))
  249. (if (not entity)
  250. (vector 1 (point) (point) (point) (point))
  251. (let ((current (aref (rmail-mime-entity-display entity) 0))
  252. (beg (if (and (> pos (point-min))
  253. (eq (get-text-property (1- pos) 'rmail-mime-entity)
  254. entity))
  255. (previous-single-property-change pos 'rmail-mime-entity
  256. nil (point-min))
  257. pos))
  258. (index 1)
  259. tagline-beg body-beg end)
  260. (goto-char beg)
  261. ;; If the header is displayed, get past it to the tagline.
  262. (if (rmail-mime-display-header current)
  263. (search-forward "\n\n" nil t))
  264. (setq tagline-beg (point))
  265. (if (>= pos tagline-beg)
  266. (setq index 2))
  267. ;; If the tagline is displayed, get past it to the body.
  268. (if (rmail-mime-display-tagline current)
  269. ;; The next forward-line call must be in sync with how
  270. ;; `rmail-mime-insert-tagline' formats the tagline. The
  271. ;; body begins after the empty line that ends the tagline.
  272. (forward-line 3))
  273. (setq body-beg (point))
  274. (if (>= pos body-beg)
  275. (setq index 3))
  276. ;; If the body is displayed, find its end.
  277. (if (rmail-mime-display-body current)
  278. (let ((tag (aref (rmail-mime-entity-tagline entity) 0))
  279. tag2)
  280. (setq end (next-single-property-change beg 'rmail-mime-entity
  281. nil (point-max)))
  282. ;; `tag' is either an empty string or "/n" where n is
  283. ;; the number of the part of the multipart MIME message.
  284. ;; The loop below finds the next location whose
  285. ;; `rmail-mime-entity' property specifies a tag of a
  286. ;; different value.
  287. (while (and (< end (point-max))
  288. (setq entity (get-text-property end 'rmail-mime-entity)
  289. tag2 (aref (rmail-mime-entity-tagline entity) 0))
  290. (and (> (length tag2) 0)
  291. (eq (string-match tag tag2) 0)))
  292. (setq end (next-single-property-change end 'rmail-mime-entity
  293. nil (point-max)))))
  294. (setq end body-beg))
  295. (vector index beg tagline-beg body-beg end)))))
  296. (defun rmail-mime-shown-mode (entity)
  297. "Make MIME-entity ENTITY display in the default way."
  298. (let ((new (aref (rmail-mime-entity-display entity) 1)))
  299. (aset new 0 (aref (rmail-mime-entity-header entity) 2))
  300. (aset new 1 (aref (rmail-mime-entity-tagline entity) 2))
  301. (aset new 2 (aref (rmail-mime-entity-body entity) 2)))
  302. (dolist (child (rmail-mime-entity-children entity))
  303. (rmail-mime-shown-mode child)))
  304. (defun rmail-mime-hidden-mode (entity)
  305. "Make MIME-entity ENTITY display in hidden mode."
  306. (let ((new (aref (rmail-mime-entity-display entity) 1)))
  307. (aset new 0 nil)
  308. (aset new 1 t)
  309. (aset new 2 nil))
  310. (dolist (child (rmail-mime-entity-children entity))
  311. (rmail-mime-hidden-mode child)))
  312. (defun rmail-mime-raw-mode (entity)
  313. "Make MIME-entity ENTITY display in raw mode."
  314. (let ((new (aref (rmail-mime-entity-display entity) 1)))
  315. (aset new 0 'raw)
  316. (aset new 1 nil)
  317. (aset new 2 'raw))
  318. (dolist (child (rmail-mime-entity-children entity))
  319. (rmail-mime-raw-mode child)))
  320. (defun rmail-mime-toggle-raw (&optional state)
  321. "Toggle on and off the raw display mode of MIME-entity at point.
  322. With optional argument STATE, force the specified display mode.
  323. Use `raw' for raw mode, and any other non-nil value for decoded mode."
  324. (let* ((pos (if (eobp) (1- (point-max)) (point)))
  325. (entity (get-text-property pos 'rmail-mime-entity))
  326. (current (aref (rmail-mime-entity-display entity) 0))
  327. (segment (rmail-mime-entity-segment pos entity)))
  328. (if (or (eq state 'raw)
  329. (and (not state)
  330. (not (eq (rmail-mime-display-header current) 'raw))))
  331. ;; Enter the raw mode.
  332. (rmail-mime-raw-mode entity)
  333. ;; Enter the shown mode.
  334. (rmail-mime-shown-mode entity))
  335. (let ((inhibit-read-only t)
  336. (modified (buffer-modified-p)))
  337. (save-excursion
  338. (goto-char (aref segment 1))
  339. (rmail-mime-insert entity)
  340. (restore-buffer-modified-p modified)))))
  341. (defun rmail-mime-toggle-hidden ()
  342. "Hide or show the body of the MIME-entity at point."
  343. (interactive)
  344. (when (rmail-mime-message-p)
  345. (let* ((rmail-mime-mbox-buffer rmail-view-buffer)
  346. (rmail-mime-view-buffer (current-buffer))
  347. (pos (if (eobp) (1- (point-max)) (point)))
  348. (entity (get-text-property pos 'rmail-mime-entity))
  349. (current (aref (rmail-mime-entity-display entity) 0))
  350. (segment (rmail-mime-entity-segment pos entity)))
  351. (if (rmail-mime-display-body current)
  352. ;; Enter the hidden mode.
  353. (progn
  354. ;; If point is in the body part, move it to the tagline
  355. ;; (or the header if tagline is not displayed).
  356. (if (= (aref segment 0) 3)
  357. (goto-char (aref segment 2)))
  358. (rmail-mime-hidden-mode entity)
  359. ;; If the current entity is the topmost one, display the
  360. ;; header.
  361. (if (and rmail-mime-mbox-buffer (= (aref segment 1) (point-min)))
  362. (let ((new (aref (rmail-mime-entity-display entity) 1)))
  363. (aset new 0 t))))
  364. ;; Query as a warning before showing if truncated.
  365. (if (and (not (stringp entity))
  366. (rmail-mime-entity-truncated entity))
  367. (unless (y-or-n-p "This entity is truncated; show anyway? ")
  368. (error "Aborted")))
  369. ;; Enter the shown mode.
  370. (rmail-mime-shown-mode entity)
  371. ;; Force this body shown.
  372. (aset (aref (rmail-mime-entity-display entity) 1) 2 t))
  373. (let ((inhibit-read-only t)
  374. (modified (buffer-modified-p))
  375. (rmail-mime-mbox-buffer rmail-view-buffer)
  376. (rmail-mime-view-buffer rmail-buffer))
  377. (save-excursion
  378. (goto-char (aref segment 1))
  379. (rmail-mime-insert entity)
  380. (restore-buffer-modified-p modified))))))
  381. (define-key rmail-mode-map "\t" 'forward-button)
  382. (define-key rmail-mode-map [backtab] 'backward-button)
  383. (define-key rmail-mode-map "\r" 'rmail-mime-toggle-hidden)
  384. ;;; Handlers
  385. (defun rmail-mime-insert-tagline (entity &rest item-list)
  386. "Insert a tag line for MIME-entity ENTITY.
  387. ITEM-LIST is a list of strings or button-elements (list) to add
  388. to the tag line."
  389. ;; Precede the tagline by an empty line to make it a separate
  390. ;; paragraph, so that it is aligned to the left margin of the window
  391. ;; even if preceded by a right-to-left paragraph.
  392. (insert "\n[")
  393. (let ((tag (aref (rmail-mime-entity-tagline entity) 0)))
  394. (if (> (length tag) 0) (insert (substring tag 1) ":")))
  395. (insert (car (rmail-mime-entity-type entity)) " ")
  396. (insert-button (let ((new (aref (rmail-mime-entity-display entity) 1)))
  397. (if (rmail-mime-display-body new) "Hide" "Show"))
  398. :type 'rmail-mime-toggle
  399. 'help-echo "mouse-2, RET: Toggle show/hide")
  400. (dolist (item item-list)
  401. (when item
  402. (if (stringp item)
  403. (insert item)
  404. (apply 'insert-button item))))
  405. ;; Follow the tagline by an empty line to make it a separate
  406. ;; paragraph, so that the paragraph direction of the following text
  407. ;; is determined based on that text.
  408. (insert "]\n\n"))
  409. (defun rmail-mime-update-tagline (entity)
  410. "Update the current tag line for MIME-entity ENTITY."
  411. (let ((inhibit-read-only t)
  412. (modified (buffer-modified-p))
  413. ;; If we are going to show the body, the new button label is
  414. ;; "Hide". Otherwise, it's "Show".
  415. (label (if (aref (aref (rmail-mime-entity-display entity) 1) 2) "Hide"
  416. "Show"))
  417. (button (next-button (point))))
  418. ;; Go to the second character of the button "Show" or "Hide".
  419. (goto-char (1+ (button-start button)))
  420. (setq button (button-at (point)))
  421. (save-excursion
  422. (insert label)
  423. (delete-region (point) (button-end button)))
  424. (delete-region (button-start button) (point))
  425. (put-text-property (point) (button-end button) 'rmail-mime-entity entity)
  426. (restore-buffer-modified-p modified)
  427. ;; The following call to forward-line must be in sync with how
  428. ;; rmail-mime-insert-tagline formats the tagline.
  429. (forward-line 2)))
  430. (defun rmail-mime-insert-header (header)
  431. "Decode and insert a MIME-entity header HEADER in the current buffer.
  432. HEADER is a vector [BEG END DEFAULT-STATUS].
  433. See `rmail-mime-entity' for details."
  434. (let ((pos (point))
  435. (last-coding-system-used nil))
  436. (save-restriction
  437. (narrow-to-region pos pos)
  438. (with-current-buffer rmail-mime-mbox-buffer
  439. (let ((rmail-buffer rmail-mime-mbox-buffer)
  440. (rmail-view-buffer rmail-mime-view-buffer))
  441. (save-excursion
  442. (goto-char (aref header 0))
  443. (rmail-copy-headers (point) (aref header 1)))))
  444. (rfc2047-decode-region pos (point))
  445. (if (and last-coding-system-used (not rmail-mime-coding-system))
  446. (setq rmail-mime-coding-system (cons last-coding-system-used nil)))
  447. (goto-char (point-min))
  448. (rmail-highlight-headers)
  449. (goto-char (point-max))
  450. (insert "\n"))))
  451. (defun rmail-mime-find-header-encoding (header)
  452. "Return the last coding system used to decode HEADER.
  453. HEADER is a header component of a MIME-entity object (see
  454. `rmail-mime-entity')."
  455. (with-temp-buffer
  456. (let ((buf (current-buffer)))
  457. (with-current-buffer rmail-mime-mbox-buffer
  458. (let ((last-coding-system-used nil)
  459. (rmail-buffer rmail-mime-mbox-buffer)
  460. (rmail-view-buffer buf))
  461. (save-excursion
  462. (goto-char (aref header 0))
  463. (rmail-copy-headers (point) (aref header 1)))))
  464. (rfc2047-decode-region (point-min) (point-max))
  465. last-coding-system-used)))
  466. (defun rmail-mime-text-handler (content-type
  467. content-disposition
  468. content-transfer-encoding)
  469. "Handle the current buffer as a plain text MIME part."
  470. (rmail-mime-insert-text
  471. (rmail-mime-entity content-type content-disposition
  472. content-transfer-encoding
  473. (vector (vector nil nil nil) (vector nil nil t))
  474. (vector nil nil nil) (vector "" (cons nil nil) t)
  475. (vector nil nil nil) nil 'rmail-mime-insert-text))
  476. t)
  477. (defun rmail-mime-insert-decoded-text (entity)
  478. "Decode and insert the text body of MIME-entity ENTITY."
  479. (let* ((content-type (rmail-mime-entity-type entity))
  480. (charset (cdr (assq 'charset (cdr content-type))))
  481. (coding-system (if charset
  482. (coding-system-from-name charset)))
  483. (body (rmail-mime-entity-body entity))
  484. (pos (point)))
  485. (or (and coding-system (coding-system-p coding-system))
  486. (setq coding-system 'undecided))
  487. (if (stringp (aref body 0))
  488. (insert (aref body 0))
  489. (let ((transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
  490. (insert-buffer-substring rmail-mime-mbox-buffer
  491. (aref body 0) (aref body 1))
  492. (cond ((string= transfer-encoding "base64")
  493. (ignore-errors (base64-decode-region pos (point))))
  494. ((string= transfer-encoding "quoted-printable")
  495. (quoted-printable-decode-region pos (point))))))
  496. (decode-coding-region pos (point) coding-system)
  497. (if (and
  498. (or (not rmail-mime-coding-system) (consp rmail-mime-coding-system))
  499. (not (eq (coding-system-base coding-system) 'us-ascii)))
  500. (setq rmail-mime-coding-system coding-system))
  501. (or (bolp) (insert "\n"))))
  502. (defun rmail-mime-insert-text (entity)
  503. "Presentation handler for a plain text MIME entity."
  504. (let ((current (aref (rmail-mime-entity-display entity) 0))
  505. (new (aref (rmail-mime-entity-display entity) 1))
  506. (header (rmail-mime-entity-header entity))
  507. (tagline (rmail-mime-entity-tagline entity))
  508. (body (rmail-mime-entity-body entity))
  509. (beg (point))
  510. (segment (rmail-mime-entity-segment (point) entity)))
  511. (or (integerp (aref body 0)) (markerp (aref body 0))
  512. (let ((data (buffer-string)))
  513. (aset body 0 data)
  514. (delete-region (point-min) (point-max))))
  515. ;; header
  516. (if (eq (rmail-mime-display-header current)
  517. (rmail-mime-display-header new))
  518. (goto-char (aref segment 2))
  519. (if (rmail-mime-display-header current)
  520. (delete-char (- (aref segment 2) (aref segment 1))))
  521. (if (rmail-mime-display-header new)
  522. (rmail-mime-insert-header header)))
  523. ;; tagline
  524. (if (eq (rmail-mime-display-tagline current)
  525. (rmail-mime-display-tagline new))
  526. (if (or (not (rmail-mime-display-tagline current))
  527. (eq (rmail-mime-display-body current)
  528. (rmail-mime-display-body new)))
  529. (forward-char (- (aref segment 3) (aref segment 2)))
  530. (rmail-mime-update-tagline entity))
  531. (if (rmail-mime-display-tagline current)
  532. (delete-char (- (aref segment 3) (aref segment 2))))
  533. (if (rmail-mime-display-tagline new)
  534. (rmail-mime-insert-tagline entity)))
  535. ;; body
  536. (if (eq (rmail-mime-display-body current)
  537. (rmail-mime-display-body new))
  538. (forward-char (- (aref segment 4) (aref segment 3)))
  539. (if (rmail-mime-display-body current)
  540. (delete-char (- (aref segment 4) (aref segment 3))))
  541. (if (rmail-mime-display-body new)
  542. (rmail-mime-insert-decoded-text entity)))
  543. (put-text-property beg (point) 'rmail-mime-entity entity)))
  544. (defun rmail-mime-insert-image (entity)
  545. "Decode and insert the image body of MIME-entity ENTITY."
  546. (let* ((content-type (car (rmail-mime-entity-type entity)))
  547. (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
  548. (body (rmail-mime-entity-body entity))
  549. data)
  550. (if (stringp (aref body 0))
  551. (setq data (aref body 0))
  552. (let ((rmail-mime-mbox-buffer rmail-view-buffer)
  553. (transfer-encoding (rmail-mime-entity-transfer-encoding entity)))
  554. (with-temp-buffer
  555. (set-buffer-multibyte nil)
  556. (setq buffer-undo-list t)
  557. (insert-buffer-substring rmail-mime-mbox-buffer
  558. (aref body 0) (aref body 1))
  559. (cond ((string= transfer-encoding "base64")
  560. (ignore-errors (base64-decode-region (point-min) (point-max))))
  561. ((string= transfer-encoding "quoted-printable")
  562. (quoted-printable-decode-region (point-min) (point-max))))
  563. (setq data
  564. (buffer-substring-no-properties (point-min) (point-max))))))
  565. (insert-image (create-image data (cdr bulk-data) t))
  566. (insert "\n")))
  567. (defun rmail-mime-toggle-button (button)
  568. "Hide or show the body of the MIME-entity associated with BUTTON."
  569. (save-excursion
  570. (goto-char (button-start button))
  571. (rmail-mime-toggle-hidden)))
  572. (define-button-type 'rmail-mime-toggle 'action 'rmail-mime-toggle-button)
  573. (defun rmail-mime-bulk-handler (content-type
  574. content-disposition
  575. content-transfer-encoding)
  576. "Handle the current buffer as an attachment to download.
  577. For images that Emacs is capable of displaying, the behavior
  578. depends upon the value of `rmail-mime-show-images'."
  579. (rmail-mime-insert-bulk
  580. (rmail-mime-entity content-type content-disposition content-transfer-encoding
  581. (vector (vector nil nil nil) (vector nil t nil))
  582. (vector nil nil nil) (vector "" (cons nil nil) t)
  583. (vector nil nil nil) nil 'rmail-mime-insert-bulk)))
  584. (defun rmail-mime-set-bulk-data (entity)
  585. "Setup the information about the attachment object for MIME-entity ENTITY.
  586. The value is non-nil if and only if the attachment object should be shown
  587. directly."
  588. (let ((content-type (car (rmail-mime-entity-type entity)))
  589. (size (cdr (assq 'size (cdr (rmail-mime-entity-disposition entity)))))
  590. (bulk-data (aref (rmail-mime-entity-tagline entity) 1))
  591. (body (rmail-mime-entity-body entity))
  592. type to-show)
  593. (cond (size
  594. (setq size (string-to-number size)))
  595. ((stringp (aref body 0))
  596. (setq size (length (aref body 0))))
  597. (t
  598. ;; Rough estimation of the size.
  599. (let ((encoding (rmail-mime-entity-transfer-encoding entity)))
  600. (setq size (- (aref body 1) (aref body 0)))
  601. (cond ((string= encoding "base64")
  602. (setq size (/ (* size 3) 4)))
  603. ((string= encoding "quoted-printable")
  604. (setq size (/ (* size 7) 3)))))))
  605. (cond
  606. ((string-match "text/" content-type)
  607. (setq type 'text))
  608. ((string-match "image/\\(.*\\)" content-type)
  609. (setq type (image-type-from-file-name
  610. (concat "." (match-string 1 content-type))))
  611. (if (and (memq type image-types)
  612. (image-type-available-p type))
  613. (if (and rmail-mime-show-images
  614. (not (eq rmail-mime-show-images 'button))
  615. (or (not (numberp rmail-mime-show-images))
  616. (< size rmail-mime-show-images)))
  617. (setq to-show t))
  618. (setq type nil))))
  619. (setcar bulk-data size)
  620. (setcdr bulk-data type)
  621. to-show))
  622. (defun rmail-mime-insert-bulk (entity)
  623. "Presentation handler for an attachment MIME entity."
  624. (let* ((content-type (rmail-mime-entity-type entity))
  625. (content-disposition (rmail-mime-entity-disposition entity))
  626. (current (aref (rmail-mime-entity-display entity) 0))
  627. (new (aref (rmail-mime-entity-display entity) 1))
  628. (header (rmail-mime-entity-header entity))
  629. (tagline (rmail-mime-entity-tagline entity))
  630. (bulk-data (aref tagline 1))
  631. (body (rmail-mime-entity-body entity))
  632. ;; Find the default directory for this media type.
  633. (directory (catch 'directory
  634. (dolist (entry rmail-mime-attachment-dirs-alist)
  635. (when (string-match (car entry) (car content-type))
  636. (dolist (dir (cdr entry))
  637. (when (file-directory-p dir)
  638. (throw 'directory dir)))))))
  639. (filename (or (cdr (assq 'name (cdr content-type)))
  640. (cdr (assq 'filename (cdr content-disposition)))
  641. "noname"))
  642. (units '(B kB MB GB))
  643. (segment (rmail-mime-entity-segment (point) entity))
  644. beg data size)
  645. (if (or (integerp (aref body 0)) (markerp (aref body 0)))
  646. (setq data entity
  647. size (car bulk-data))
  648. (if (stringp (aref body 0))
  649. (setq data (aref body 0))
  650. (setq data (string-as-unibyte (buffer-string)))
  651. (aset body 0 data)
  652. (rmail-mime-set-bulk-data entity)
  653. (delete-region (point-min) (point-max)))
  654. (setq size (length data)))
  655. (while (and (> size 1024.0) ; cribbed from gnus-agent-expire-done-message
  656. (cdr units))
  657. (setq size (/ size 1024.0)
  658. units (cdr units)))
  659. (setq beg (point))
  660. ;; header
  661. (if (eq (rmail-mime-display-header current)
  662. (rmail-mime-display-header new))
  663. (goto-char (aref segment 2))
  664. (if (rmail-mime-display-header current)
  665. (delete-char (- (aref segment 2) (aref segment 1))))
  666. (if (rmail-mime-display-header new)
  667. (rmail-mime-insert-header header)))
  668. ;; tagline
  669. (if (eq (rmail-mime-display-tagline current)
  670. (rmail-mime-display-tagline new))
  671. (if (or (not (rmail-mime-display-tagline current))
  672. (eq (rmail-mime-display-body current)
  673. (rmail-mime-display-body new)))
  674. (forward-char (- (aref segment 3) (aref segment 2)))
  675. (rmail-mime-update-tagline entity))
  676. (if (rmail-mime-display-tagline current)
  677. (delete-char (- (aref segment 3) (aref segment 2))))
  678. (if (rmail-mime-display-tagline new)
  679. (rmail-mime-insert-tagline
  680. entity
  681. " Save:"
  682. (list filename
  683. :type 'rmail-mime-save
  684. 'help-echo "mouse-2, RET: Save attachment"
  685. 'filename filename
  686. 'directory (file-name-as-directory directory)
  687. 'data data)
  688. (format " (%.0f%s)" size (car units))
  689. ;; We don't need this button because the "type" string of a
  690. ;; tagline is the button to do this.
  691. ;; (if (cdr bulk-data)
  692. ;; " ")
  693. ;; (if (cdr bulk-data)
  694. ;; (list "Toggle show/hide"
  695. ;; :type 'rmail-mime-image
  696. ;; 'help-echo "mouse-2, RET: Toggle show/hide"
  697. ;; 'image-type (cdr bulk-data)
  698. ;; 'image-data data))
  699. )))
  700. ;; body
  701. (if (eq (rmail-mime-display-body current)
  702. (rmail-mime-display-body new))
  703. (forward-char (- (aref segment 4) (aref segment 3)))
  704. (if (rmail-mime-display-body current)
  705. (delete-char (- (aref segment 4) (aref segment 3))))
  706. (if (rmail-mime-display-body new)
  707. (cond ((eq (cdr bulk-data) 'text)
  708. (rmail-mime-insert-decoded-text entity))
  709. ((cdr bulk-data)
  710. (rmail-mime-insert-image entity))
  711. (t
  712. ;; As we don't know how to display the body, just
  713. ;; insert it as a text.
  714. (rmail-mime-insert-decoded-text entity)))))
  715. (put-text-property beg (point) 'rmail-mime-entity entity)))
  716. (defun rmail-mime-multipart-handler (content-type
  717. content-disposition
  718. content-transfer-encoding)
  719. "Handle the current buffer as a multipart MIME body.
  720. The current buffer should be narrowed to the body. CONTENT-TYPE,
  721. CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING are the values
  722. of the respective parsed headers. See `rmail-mime-handle' for their
  723. format."
  724. (rmail-mime-process-multipart
  725. content-type content-disposition content-transfer-encoding nil)
  726. t)
  727. (defun rmail-mime-process-multipart (content-type
  728. content-disposition
  729. content-transfer-encoding
  730. parse-tag)
  731. "Process the current buffer as a multipart MIME body.
  732. If PARSE-TAG is nil, modify the current buffer directly for
  733. showing the MIME body and return nil.
  734. Otherwise, PARSE-TAG is a string indicating the depth and index
  735. number of the entity. In this case, parse the current buffer and
  736. return a list of MIME-entity objects.
  737. The other arguments are the same as `rmail-mime-multipart-handler'."
  738. ;; Some MUAs start boundaries with "--", while it should start
  739. ;; with "CRLF--", as defined by RFC 2046:
  740. ;; The boundary delimiter MUST occur at the beginning of a line,
  741. ;; i.e., following a CRLF, and the initial CRLF is considered to
  742. ;; be attached to the boundary delimiter line rather than part
  743. ;; of the preceding part.
  744. ;; We currently don't handle that.
  745. (let ((boundary (cdr (assq 'boundary content-type)))
  746. (subtype (cadr (split-string (car content-type) "/")))
  747. (index 0)
  748. beg end next entities truncated)
  749. (unless boundary
  750. (rmail-mm-get-boundary-error-message
  751. "No boundary defined" content-type content-disposition
  752. content-transfer-encoding))
  753. (setq boundary (concat "\n--" boundary))
  754. ;; Hide the body before the first bodypart
  755. (goto-char (point-min))
  756. (when (and (search-forward boundary nil t)
  757. (looking-at "[ \t]*\n"))
  758. (if parse-tag
  759. (narrow-to-region (match-end 0) (point-max))
  760. (delete-region (point-min) (match-end 0))))
  761. ;; Change content-type to the proper default one for the children.
  762. (cond ((string-match "mixed" subtype)
  763. (setq content-type '("text/plain")))
  764. ((string-match "digest" subtype)
  765. (setq content-type '("message/rfc822")))
  766. (t
  767. (setq content-type nil)))
  768. ;; Loop over all body parts, where beg points at the beginning of
  769. ;; the part and end points at the end of the part. next points at
  770. ;; the beginning of the next part. The current point is just
  771. ;; after the boundary tag.
  772. (setq beg (point-min))
  773. (while (or (and (search-forward boundary nil t)
  774. (setq truncated nil end (match-beginning 0)))
  775. ;; If the boundary does not appear at all,
  776. ;; the message was truncated.
  777. ;; Handle the rest of the truncated message
  778. ;; (if it isn't empty) by pretending that the boundary
  779. ;; appears at the end of the message.
  780. (and (save-excursion
  781. (skip-chars-forward "\n")
  782. (> (point-max) (point)))
  783. (setq truncated t end (point-max))))
  784. ;; If this is the last boundary according to RFC 2046, hide the
  785. ;; epilogue, else hide the boundary only. Use a marker for
  786. ;; `next' because `rmail-mime-show' may change the buffer.
  787. (cond ((looking-at "--[ \t]*$")
  788. (setq next (point-max-marker)))
  789. ((looking-at "[ \t]*\n")
  790. (setq next (copy-marker (match-end 0) t)))
  791. (truncated
  792. ;; We're handling what's left of a truncated message.
  793. (setq next (point-max-marker)))
  794. (t
  795. ;; The original code signaled an error as below, but
  796. ;; this line may be a boundary of nested multipart. So,
  797. ;; we just set `next' to nil to skip this line
  798. ;; (rmail-mm-get-boundary-error-message
  799. ;; "Malformed boundary" content-type content-disposition
  800. ;; content-transfer-encoding)
  801. (setq next nil)))
  802. (when next
  803. (setq index (1+ index))
  804. ;; Handle the part.
  805. (if parse-tag
  806. (save-restriction
  807. (narrow-to-region beg end)
  808. (let ((child (rmail-mime-process
  809. nil (format "%s/%d" parse-tag index)
  810. content-type content-disposition)))
  811. ;; Display a tagline.
  812. (aset (aref (rmail-mime-entity-display child) 1) 1
  813. (aset (rmail-mime-entity-tagline child) 2 t))
  814. (rmail-mime-entity-set-truncated child truncated)
  815. (push child entities)))
  816. (delete-region end next)
  817. (save-restriction
  818. (narrow-to-region beg end)
  819. (rmail-mime-show)))
  820. (goto-char (setq beg next))))
  821. (when parse-tag
  822. (setq entities (nreverse entities))
  823. (if (string-match "alternative" subtype)
  824. ;; Find the best entity to show, and hide all the others.
  825. (let (best second)
  826. (dolist (child entities)
  827. (if (string= (or (car (rmail-mime-entity-disposition child))
  828. (car content-disposition))
  829. "inline")
  830. (if (string-match "text/plain"
  831. (car (rmail-mime-entity-type child)))
  832. (setq best child)
  833. (if (string-match "text/.*"
  834. (car (rmail-mime-entity-type child)))
  835. (setq second child)))))
  836. (or best (not second) (setq best second))
  837. (dolist (child entities)
  838. (unless (eq best child)
  839. (aset (rmail-mime-entity-body child) 2 nil)
  840. (rmail-mime-hidden-mode child)))))
  841. entities)))
  842. (defun rmail-mime-insert-multipart (entity)
  843. "Presentation handler for a multipart MIME entity."
  844. (let ((current (aref (rmail-mime-entity-display entity) 0))
  845. (new (aref (rmail-mime-entity-display entity) 1))
  846. (header (rmail-mime-entity-header entity))
  847. (tagline (rmail-mime-entity-tagline entity))
  848. (body (rmail-mime-entity-body entity))
  849. (beg (point))
  850. (segment (rmail-mime-entity-segment (point) entity)))
  851. ;; header
  852. (if (eq (rmail-mime-display-header current)
  853. (rmail-mime-display-header new))
  854. (goto-char (aref segment 2))
  855. (if (rmail-mime-display-header current)
  856. (delete-char (- (aref segment 2) (aref segment 1))))
  857. (if (rmail-mime-display-header new)
  858. (rmail-mime-insert-header header)))
  859. ;; tagline
  860. (if (eq (rmail-mime-display-tagline current)
  861. (rmail-mime-display-tagline new))
  862. (if (or (not (rmail-mime-display-tagline current))
  863. (eq (rmail-mime-display-body current)
  864. (rmail-mime-display-body new)))
  865. (forward-char (- (aref segment 3) (aref segment 2)))
  866. (rmail-mime-update-tagline entity))
  867. (if (rmail-mime-display-tagline current)
  868. (delete-char (- (aref segment 3) (aref segment 2))))
  869. (if (rmail-mime-display-tagline new)
  870. (rmail-mime-insert-tagline entity)))
  871. (put-text-property beg (point) 'rmail-mime-entity entity)
  872. ;; body
  873. (if (eq (rmail-mime-display-body current)
  874. (rmail-mime-display-body new))
  875. (forward-char (- (aref segment 4) (aref segment 3)))
  876. (dolist (child (rmail-mime-entity-children entity))
  877. (rmail-mime-insert child)))
  878. entity))
  879. ;;; Main code
  880. (defun rmail-mime-handle (content-type
  881. content-disposition
  882. content-transfer-encoding)
  883. "Handle the current buffer as a MIME part.
  884. The current buffer should be narrowed to the respective body, and
  885. point should be at the beginning of the body.
  886. CONTENT-TYPE, CONTENT-DISPOSITION, and CONTENT-TRANSFER-ENCODING
  887. are the values of the respective parsed headers. The latter should
  888. be lower-case. The parsed headers for CONTENT-TYPE and CONTENT-DISPOSITION
  889. have the form
  890. \(VALUE . ALIST)
  891. In other words:
  892. \(VALUE (ATTRIBUTE . VALUE) (ATTRIBUTE . VALUE) ...)
  893. VALUE is a string and ATTRIBUTE is a symbol.
  894. Consider the following header, for example:
  895. Content-Type: multipart/mixed;
  896. boundary=\"----=_NextPart_000_0104_01C617E4.BDEC4C40\"
  897. The parsed header value:
  898. \(\"multipart/mixed\"
  899. \(\"boundary\" . \"----=_NextPart_000_0104_01C617E4.BDEC4C40\"))"
  900. ;; Handle the content transfer encodings we know. Unknown transfer
  901. ;; encodings will be passed on to the various handlers.
  902. (cond ((string= content-transfer-encoding "base64")
  903. (when (ignore-errors
  904. (base64-decode-region (point) (point-max)))
  905. (setq content-transfer-encoding nil)))
  906. ((string= content-transfer-encoding "quoted-printable")
  907. (quoted-printable-decode-region (point) (point-max))
  908. (setq content-transfer-encoding nil))
  909. ((string= content-transfer-encoding "8bit")
  910. ;; FIXME: Is this the correct way?
  911. ;; No, of course not, it just means there's no decoding to do.
  912. ;; (set-buffer-multibyte nil)
  913. (setq content-transfer-encoding nil)
  914. ))
  915. ;; Inline stuff requires work. Attachments are handled by the bulk
  916. ;; handler.
  917. (if (string= "inline" (car content-disposition))
  918. (let ((stop nil))
  919. (dolist (entry rmail-mime-media-type-handlers-alist)
  920. (when (and (string-match (car entry) (car content-type)) (not stop))
  921. (progn
  922. (setq stop (funcall (cadr entry) content-type
  923. content-disposition
  924. content-transfer-encoding))))))
  925. ;; Everything else is an attachment.
  926. (rmail-mime-bulk-handler content-type
  927. content-disposition
  928. content-transfer-encoding))
  929. (save-restriction
  930. (widen)
  931. (let ((entity (get-text-property (1- (point)) 'rmail-mime-entity))
  932. current new)
  933. (when entity
  934. (setq current (aref (rmail-mime-entity-display entity) 0)
  935. new (aref (rmail-mime-entity-display entity) 1))
  936. (dotimes (i 3)
  937. (aset current i (aref new i)))))))
  938. (defun rmail-mime-show (&optional show-headers)
  939. "Handle the current buffer as a MIME message.
  940. If SHOW-HEADERS is non-nil, then the headers of the current part
  941. will shown as usual for a MIME message. The headers are also
  942. shown for the content type message/rfc822. This function will be
  943. called recursively if multiple parts are available.
  944. The current buffer must contain a single message. It will be
  945. modified."
  946. (rmail-mime-process show-headers nil))
  947. (defun rmail-mime-process (show-headers parse-tag &optional
  948. default-content-type
  949. default-content-disposition)
  950. (let ((end (point-min))
  951. content-type
  952. content-transfer-encoding
  953. content-disposition)
  954. ;; `point-min' returns the beginning and `end' points at the end
  955. ;; of the headers.
  956. (goto-char (point-min))
  957. ;; If we're showing a part without headers, then it will start
  958. ;; with a newline.
  959. (if (eq (char-after) ?\n)
  960. (setq end (1+ (point)))
  961. (when (search-forward "\n\n" nil t)
  962. (setq end (match-end 0))
  963. (save-restriction
  964. (narrow-to-region (point-min) end)
  965. ;; FIXME: Default disposition of the multipart entities should
  966. ;; be inherited.
  967. (setq content-type
  968. (mail-fetch-field "Content-Type")
  969. content-transfer-encoding
  970. (mail-fetch-field "Content-Transfer-Encoding")
  971. content-disposition
  972. (mail-fetch-field "Content-Disposition")))))
  973. ;; Per RFC 2045, C-T-E is case insensitive (bug#5070), but the others
  974. ;; are not completely so. Hopefully mail-header-parse-* DTRT.
  975. (if content-transfer-encoding
  976. (setq content-transfer-encoding (downcase content-transfer-encoding)))
  977. (setq content-type
  978. (if content-type
  979. (or (mail-header-parse-content-type content-type)
  980. '("text/plain"))
  981. (or default-content-type '("text/plain"))))
  982. (setq content-disposition
  983. (if content-disposition
  984. (mail-header-parse-content-disposition content-disposition)
  985. ;; If none specified, we are free to choose what we deem
  986. ;; suitable according to RFC 2183. We like inline.
  987. (or default-content-disposition '("inline"))))
  988. ;; Unrecognized disposition types are to be treated like
  989. ;; attachment according to RFC 2183.
  990. (unless (member (car content-disposition) '("inline" "attachment"))
  991. (setq content-disposition '("attachment")))
  992. (if parse-tag
  993. (let* ((is-inline (string= (car content-disposition) "inline"))
  994. (hdr-end (copy-marker end))
  995. (header (vector (point-min-marker) hdr-end nil))
  996. (tagline (vector parse-tag (cons nil nil) t))
  997. (body (vector hdr-end (point-max-marker) is-inline))
  998. (new (vector (aref header 2) (aref tagline 2) (aref body 2)))
  999. children handler entity)
  1000. (cond ((string-match "multipart/.*" (car content-type))
  1001. (save-restriction
  1002. (narrow-to-region (1- end) (point-max))
  1003. (setq children (rmail-mime-process-multipart
  1004. content-type
  1005. content-disposition
  1006. content-transfer-encoding
  1007. parse-tag)
  1008. handler 'rmail-mime-insert-multipart)))
  1009. ((string-match "message/rfc822" (car content-type))
  1010. (save-restriction
  1011. (narrow-to-region end (point-max))
  1012. (let* ((msg (rmail-mime-process t parse-tag
  1013. '("text/plain") '("inline")))
  1014. (msg-new (aref (rmail-mime-entity-display msg) 1)))
  1015. ;; Show header of the child.
  1016. (aset msg-new 0 t)
  1017. (aset (rmail-mime-entity-header msg) 2 t)
  1018. ;; Hide tagline of the child.
  1019. (aset msg-new 1 nil)
  1020. (aset (rmail-mime-entity-tagline msg) 2 nil)
  1021. (setq children (list msg)
  1022. handler 'rmail-mime-insert-multipart))))
  1023. ((and is-inline (string-match "text/" (car content-type)))
  1024. ;; Don't need a tagline.
  1025. (aset new 1 (aset tagline 2 nil))
  1026. (setq handler 'rmail-mime-insert-text))
  1027. (t
  1028. ;; Force hidden mode.
  1029. (aset new 1 (aset tagline 2 t))
  1030. (aset new 2 (aset body 2 nil))
  1031. (setq handler 'rmail-mime-insert-bulk)))
  1032. (setq entity (rmail-mime-entity content-type
  1033. content-disposition
  1034. content-transfer-encoding
  1035. (vector (vector nil nil nil) new)
  1036. header tagline body children handler))
  1037. (if (and (eq handler 'rmail-mime-insert-bulk)
  1038. (rmail-mime-set-bulk-data entity))
  1039. ;; Show the body.
  1040. (aset new 2 (aset body 2 t)))
  1041. entity)
  1042. ;; Hide headers and handle the part.
  1043. (put-text-property (point-min) (point-max) 'rmail-mime-entity
  1044. (rmail-mime-entity
  1045. content-type content-disposition
  1046. content-transfer-encoding
  1047. (vector (vector 'raw nil 'raw) (vector 'raw nil 'raw))
  1048. (vector nil nil 'raw) (vector "" (cons nil nil) nil)
  1049. (vector nil nil 'raw) nil nil))
  1050. (save-restriction
  1051. (cond ((string= (car content-type) "message/rfc822")
  1052. (narrow-to-region end (point-max)))
  1053. ((not show-headers)
  1054. (delete-region (point-min) end)))
  1055. (rmail-mime-handle content-type content-disposition
  1056. content-transfer-encoding)))))
  1057. (defun rmail-mime-parse ()
  1058. "Parse the current Rmail message as a MIME message.
  1059. The value is a MIME-entity object (see `rmail-mime-entity').
  1060. If an error occurs, return an error message string."
  1061. (let ((rmail-mime-mbox-buffer (if (rmail-buffers-swapped-p)
  1062. rmail-view-buffer
  1063. (current-buffer))))
  1064. (condition-case err
  1065. (with-current-buffer rmail-mime-mbox-buffer
  1066. (save-excursion
  1067. (goto-char (point-min))
  1068. (let* ((entity (rmail-mime-process t ""
  1069. '("text/plain") '("inline")))
  1070. (new (aref (rmail-mime-entity-display entity) 1)))
  1071. ;; Show header.
  1072. (aset new 0 (aset (rmail-mime-entity-header entity) 2 t))
  1073. ;; Show tagline if and only if body is not shown.
  1074. (if (aref new 2)
  1075. (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 nil))
  1076. (aset new 1 (aset (rmail-mime-entity-tagline entity) 2 t)))
  1077. entity)))
  1078. (error (format "%s" err)))))
  1079. (defun rmail-mime-insert (entity)
  1080. "Insert a MIME-entity ENTITY in the current buffer.
  1081. This function will be called recursively if multiple parts are
  1082. available."
  1083. (let ((current (aref (rmail-mime-entity-display entity) 0))
  1084. (new (aref (rmail-mime-entity-display entity) 1)))
  1085. (if (not (eq (rmail-mime-display-header new) 'raw))
  1086. ;; Not a raw-mode. Each handler should handle it.
  1087. (funcall (rmail-mime-entity-handler entity) entity)
  1088. (let ((header (rmail-mime-entity-header entity))
  1089. (tagline (rmail-mime-entity-tagline entity))
  1090. (body (rmail-mime-entity-body entity))
  1091. (beg (point))
  1092. (segment (rmail-mime-entity-segment (point) entity)))
  1093. ;; header
  1094. (if (eq (rmail-mime-display-header current)
  1095. (rmail-mime-display-header new))
  1096. (goto-char (aref segment 2))
  1097. (if (rmail-mime-display-header current)
  1098. (delete-char (- (aref segment 2) (aref segment 1))))
  1099. (insert-buffer-substring rmail-mime-mbox-buffer
  1100. (aref header 0) (aref header 1)))
  1101. ;; tagline
  1102. (if (rmail-mime-display-tagline current)
  1103. (delete-char (- (aref segment 3) (aref segment 2))))
  1104. ;; body
  1105. (let ((children (rmail-mime-entity-children entity)))
  1106. (if children
  1107. (progn
  1108. (put-text-property beg (point) 'rmail-mime-entity entity)
  1109. (dolist (child children)
  1110. (rmail-mime-insert child)))
  1111. (if (eq (rmail-mime-display-body current)
  1112. (rmail-mime-display-body new))
  1113. (forward-char (- (aref segment 4) (aref segment 3)))
  1114. (if (rmail-mime-display-body current)
  1115. (delete-char (- (aref segment 4) (aref segment 3))))
  1116. (insert-buffer-substring rmail-mime-mbox-buffer
  1117. (aref body 0) (aref body 1))
  1118. (or (bolp) (insert "\n")))
  1119. (put-text-property beg (point) 'rmail-mime-entity entity)))))
  1120. (dotimes (i 3)
  1121. (aset current i (aref new i)))))
  1122. (define-derived-mode rmail-mime-mode fundamental-mode "RMIME"
  1123. "Major mode used in `rmail-mime' buffers."
  1124. (setq font-lock-defaults '(rmail-font-lock-keywords t t nil nil)))
  1125. ;;;###autoload
  1126. (defun rmail-mime (&optional arg state)
  1127. "Toggle the display of a MIME message.
  1128. The actual behavior depends on the value of `rmail-enable-mime'.
  1129. If `rmail-enable-mime' is non-nil (the default), this command toggles
  1130. the display of a MIME message between decoded presentation form and
  1131. raw data. With optional prefix argument ARG, it toggles the display only
  1132. of the MIME entity at point, if there is one. The optional argument
  1133. STATE forces a particular display state, rather than toggling.
  1134. `raw' forces raw mode, any other non-nil value forces decoded mode.
  1135. If `rmail-enable-mime' is nil, this creates a temporary \"*RMAIL*\"
  1136. buffer holding a decoded copy of the message. Inline content-types are
  1137. handled according to `rmail-mime-media-type-handlers-alist'.
  1138. By default, this displays text and multipart messages, and offers to
  1139. download attachments as specified by `rmail-mime-attachment-dirs-alist'.
  1140. The arguments ARG and STATE have no effect in this case."
  1141. (interactive (list current-prefix-arg nil))
  1142. (if rmail-enable-mime
  1143. (with-current-buffer rmail-buffer
  1144. (if (rmail-mime-message-p)
  1145. (let ((rmail-mime-mbox-buffer rmail-view-buffer)
  1146. (rmail-mime-view-buffer rmail-buffer)
  1147. (entity (get-text-property
  1148. (progn
  1149. (or arg (goto-char (point-min)))
  1150. (point)) 'rmail-mime-entity)))
  1151. (if (or (not arg) entity) (rmail-mime-toggle-raw state)))
  1152. (message "Not a MIME message, just toggling headers")
  1153. (rmail-toggle-header)))
  1154. (let* ((data (rmail-apply-in-message rmail-current-message 'buffer-string))
  1155. (buf (get-buffer-create "*RMAIL*"))
  1156. (rmail-mime-mbox-buffer rmail-view-buffer)
  1157. (rmail-mime-view-buffer buf))
  1158. (set-buffer buf)
  1159. (setq buffer-undo-list t)
  1160. (let ((inhibit-read-only t))
  1161. ;; Decoding the message in fundamental mode for speed, only
  1162. ;; switching to rmail-mime-mode at the end for display. Eg
  1163. ;; quoted-printable-decode-region gets very slow otherwise (Bug#4993).
  1164. (fundamental-mode)
  1165. (erase-buffer)
  1166. (insert data)
  1167. (rmail-mime-show t)
  1168. (rmail-mime-mode)
  1169. (set-buffer-modified-p nil))
  1170. (view-buffer buf))))
  1171. (defun rmail-mm-get-boundary-error-message (message type disposition encoding)
  1172. "Return MESSAGE with more information on the main MIME components."
  1173. (error "%s; type: %s; disposition: %s; encoding: %s"
  1174. message type disposition encoding))
  1175. (defun rmail-show-mime ()
  1176. "Function to use for the value of `rmail-show-mime-function'."
  1177. (let ((entity (rmail-mime-parse))
  1178. (rmail-mime-mbox-buffer rmail-buffer)
  1179. (rmail-mime-view-buffer rmail-view-buffer)
  1180. (rmail-mime-coding-system nil))
  1181. ;; If ENTITY is not a vector, it is a string describing an error.
  1182. (if (vectorp entity)
  1183. (with-current-buffer rmail-mime-view-buffer
  1184. (erase-buffer)
  1185. ;; This condition-case is for catching an error in the
  1186. ;; internal MIME decoding (e.g. incorrect BASE64 form) that
  1187. ;; may be signaled by rmail-mime-insert.
  1188. ;; FIXME: The current code doesn't set a proper error symbol
  1189. ;; in ERR. We must find a way to propagate a correct error
  1190. ;; symbol that is caused in the very deep code of text
  1191. ;; decoding (e.g. an error by base64-decode-region called by
  1192. ;; post-read-conversion function of utf-7).
  1193. (condition-case err
  1194. (progn
  1195. (rmail-mime-insert entity)
  1196. (if (consp rmail-mime-coding-system)
  1197. ;; Decoding is done by rfc2047-decode-region only for a
  1198. ;; header. But, as the used coding system may have been
  1199. ;; overridden by mm-charset-override-alist, we can't
  1200. ;; trust (car rmail-mime-coding-system). So, here we
  1201. ;; try the decoding again with mm-charset-override-alist
  1202. ;; bound to nil.
  1203. (let ((mm-charset-override-alist nil))
  1204. (setq rmail-mime-coding-system
  1205. (rmail-mime-find-header-encoding
  1206. (rmail-mime-entity-header entity)))))
  1207. (set-buffer-file-coding-system
  1208. (if rmail-mime-coding-system
  1209. (coding-system-base rmail-mime-coding-system)
  1210. 'undecided)
  1211. t t))
  1212. (error (setq entity (format "%s" err))))))
  1213. ;; Re-check ENTITY. It may be set to an error string.
  1214. (when (stringp entity)
  1215. ;; Decoding failed. ENTITY is an error message. Insert the
  1216. ;; original message body as is, and show warning.
  1217. (let ((region (with-current-buffer rmail-mime-mbox-buffer
  1218. (goto-char (point-min))
  1219. (re-search-forward "^$" nil t)
  1220. (forward-line 1)
  1221. (vector (point-min) (point) (point-max)))))
  1222. (with-current-buffer rmail-mime-view-buffer
  1223. (let ((inhibit-read-only t))
  1224. (erase-buffer)
  1225. (rmail-mime-insert-header region)
  1226. (insert-buffer-substring rmail-mime-mbox-buffer
  1227. (aref region 1) (aref region 2))))
  1228. (set-buffer-file-coding-system 'no-conversion t t)
  1229. (message "MIME decoding failed: %s" entity)))))
  1230. (setq rmail-show-mime-function 'rmail-show-mime)
  1231. (defun rmail-insert-mime-forwarded-message (forward-buffer)
  1232. "Insert the message in FORWARD-BUFFER as a forwarded message.
  1233. This is the usual value of `rmail-insert-mime-forwarded-message-function'."
  1234. (let ((message-buffer
  1235. (with-current-buffer forward-buffer
  1236. (if rmail-buffer-swapped
  1237. forward-buffer
  1238. rmail-view-buffer))))
  1239. (save-restriction
  1240. (narrow-to-region (point) (point))
  1241. (message-forward-make-body-mime message-buffer))))
  1242. (setq rmail-insert-mime-forwarded-message-function
  1243. 'rmail-insert-mime-forwarded-message)
  1244. (defun rmail-insert-mime-resent-message (forward-buffer)
  1245. "Function to set in `rmail-insert-mime-resent-message-function' (which see)."
  1246. (insert-buffer-substring
  1247. (with-current-buffer forward-buffer rmail-view-buffer))
  1248. (goto-char (point-min))
  1249. (when (looking-at "From ")
  1250. (forward-line 1)
  1251. (delete-region (point-min) (point))))
  1252. (setq rmail-insert-mime-resent-message-function
  1253. 'rmail-insert-mime-resent-message)
  1254. (defun rmail-search-mime-message (msg regexp)
  1255. "Function to set in `rmail-search-mime-message-function' (which see)."
  1256. (save-restriction
  1257. (narrow-to-region (rmail-msgbeg msg) (rmail-msgend msg))
  1258. (let* ((rmail-mime-mbox-buffer (current-buffer))
  1259. (rmail-mime-view-buffer rmail-view-buffer)
  1260. (header-end (save-excursion
  1261. (re-search-forward "^$" nil 'move) (point)))
  1262. (body-end (point-max))
  1263. (entity (rmail-mime-parse)))
  1264. (or
  1265. ;; At first, just search the headers.
  1266. (with-temp-buffer
  1267. (insert-buffer-substring rmail-mime-mbox-buffer nil header-end)
  1268. (rfc2047-decode-region (point-min) (point))
  1269. (goto-char (point-min))
  1270. (re-search-forward regexp nil t))
  1271. ;; Next, search the body.
  1272. (if (and entity
  1273. ;; RMS: I am not sure why, but sometimes this is a string.
  1274. (not (stringp entity))
  1275. (let* ((content-type (rmail-mime-entity-type entity))
  1276. (charset (cdr (assq 'charset (cdr content-type)))))
  1277. (or (not (string-match "text/.*" (car content-type)))
  1278. (and charset
  1279. (not (string= (downcase charset) "us-ascii"))))))
  1280. ;; Search the decoded MIME message.
  1281. (with-temp-buffer
  1282. (rmail-mime-insert entity)
  1283. (goto-char (point-min))
  1284. (re-search-forward regexp nil t))
  1285. ;; Search the body without decoding.
  1286. (goto-char header-end)
  1287. (re-search-forward regexp nil t))))))
  1288. (setq rmail-search-mime-message-function 'rmail-search-mime-message)
  1289. (provide 'rmailmm)
  1290. ;; Local Variables:
  1291. ;; generated-autoload-file: "rmail.el"
  1292. ;; End:
  1293. ;;; rmailmm.el ends here