mm-decode.el 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. ;;; mm-decode.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 'mail-parse)
  22. (require 'mm-bodies)
  23. (eval-when-compile (require 'cl)
  24. (require 'term))
  25. (autoload 'gnus-map-function "gnus-util")
  26. (autoload 'gnus-replace-in-string "gnus-util")
  27. (autoload 'gnus-read-shell-command "gnus-util")
  28. (autoload 'mm-inline-partial "mm-partial")
  29. (autoload 'mm-inline-external-body "mm-extern")
  30. (autoload 'mm-extern-cache-contents "mm-extern")
  31. (autoload 'mm-insert-inline "mm-view")
  32. (defvar gnus-current-window-configuration)
  33. (add-hook 'gnus-exit-gnus-hook 'mm-destroy-postponed-undisplay-list)
  34. (defgroup mime-display ()
  35. "Display of MIME in mail and news articles."
  36. :link '(custom-manual "(emacs-mime)Display Customization")
  37. :version "21.1"
  38. :group 'mail
  39. :group 'news
  40. :group 'multimedia)
  41. (defgroup mime-security ()
  42. "MIME security in mail and news articles."
  43. :link '(custom-manual "(emacs-mime)Display Customization")
  44. :group 'mail
  45. :group 'news
  46. :group 'multimedia)
  47. ;;; Convenience macros.
  48. (defmacro mm-handle-buffer (handle)
  49. `(nth 0 ,handle))
  50. (defmacro mm-handle-type (handle)
  51. `(nth 1 ,handle))
  52. (defsubst mm-handle-media-type (handle)
  53. (if (stringp (car handle))
  54. (car handle)
  55. (car (mm-handle-type handle))))
  56. (defsubst mm-handle-media-supertype (handle)
  57. (car (split-string (mm-handle-media-type handle) "/")))
  58. (defsubst mm-handle-media-subtype (handle)
  59. (cadr (split-string (mm-handle-media-type handle) "/")))
  60. (defmacro mm-handle-encoding (handle)
  61. `(nth 2 ,handle))
  62. (defmacro mm-handle-undisplayer (handle)
  63. `(nth 3 ,handle))
  64. (defmacro mm-handle-set-undisplayer (handle function)
  65. `(setcar (nthcdr 3 ,handle) ,function))
  66. (defmacro mm-handle-disposition (handle)
  67. `(nth 4 ,handle))
  68. (defmacro mm-handle-description (handle)
  69. `(nth 5 ,handle))
  70. (defmacro mm-handle-cache (handle)
  71. `(nth 6 ,handle))
  72. (defmacro mm-handle-set-cache (handle contents)
  73. `(setcar (nthcdr 6 ,handle) ,contents))
  74. (defmacro mm-handle-id (handle)
  75. `(nth 7 ,handle))
  76. (defmacro mm-handle-multipart-original-buffer (handle)
  77. `(get-text-property 0 'buffer (car ,handle)))
  78. (defmacro mm-handle-multipart-from (handle)
  79. `(get-text-property 0 'from (car ,handle)))
  80. (defmacro mm-handle-multipart-ctl-parameter (handle parameter)
  81. `(get-text-property 0 ,parameter (car ,handle)))
  82. (defmacro mm-make-handle (&optional buffer type encoding undisplayer
  83. disposition description cache
  84. id)
  85. `(list ,buffer ,type ,encoding ,undisplayer
  86. ,disposition ,description ,cache ,id))
  87. (defcustom mm-text-html-renderer
  88. (cond ((fboundp 'libxml-parse-html-region) 'shr)
  89. ((executable-find "w3m") 'gnus-w3m)
  90. ((executable-find "links") 'links)
  91. ((executable-find "lynx") 'lynx)
  92. ((locate-library "w3") 'w3)
  93. ((locate-library "html2text") 'html2text)
  94. (t nil))
  95. "Render of HTML contents.
  96. It is one of defined renderer types, or a rendering function.
  97. The defined renderer types are:
  98. `shr': use the built-in Gnus HTML renderer;
  99. `gnus-w3m': use Gnus renderer based on w3m;
  100. `w3m': use emacs-w3m;
  101. `w3m-standalone': use plain w3m;
  102. `links': use links;
  103. `lynx': use lynx;
  104. `w3': use Emacs/W3;
  105. `html2text': use html2text;
  106. nil : use external viewer (default web browser)."
  107. :version "24.1"
  108. :type '(choice (const shr)
  109. (const gnus-w3m)
  110. (const w3)
  111. (const w3m :tag "emacs-w3m")
  112. (const w3m-standalone :tag "standalone w3m" )
  113. (const links)
  114. (const lynx)
  115. (const html2text)
  116. (const nil :tag "External viewer")
  117. (function))
  118. :group 'mime-display)
  119. (defcustom mm-inline-text-html-with-images nil
  120. "If non-nil, Gnus will allow retrieving images in HTML contents with
  121. the <img> tags. It has no effect on Emacs/w3. See also the
  122. documentation for the `mm-w3m-safe-url-regexp' variable."
  123. :version "22.1"
  124. :type 'boolean
  125. :group 'mime-display)
  126. (defcustom mm-w3m-safe-url-regexp "\\`cid:"
  127. "Regexp matching URLs which are considered to be safe.
  128. Some HTML mails might contain a nasty trick used by spammers, using
  129. the <img> tag which is far more evil than the [Click Here!] button.
  130. It is most likely intended to check whether the ominous spam mail has
  131. reached your eyes or not, in which case the spammer knows for sure
  132. that your email address is valid. It is done by embedding an
  133. identifier string into a URL that you might automatically retrieve
  134. when displaying the image. The default value is \"\\\\`cid:\" which only
  135. matches parts embedded to the Multipart/Related type MIME contents and
  136. Gnus will never connect to the spammer's site arbitrarily. You may
  137. set this variable to nil if you consider all urls to be safe."
  138. :version "22.1"
  139. :type '(choice (regexp :tag "Regexp")
  140. (const :tag "All URLs are safe" nil))
  141. :group 'mime-display)
  142. (defcustom mm-inline-text-html-with-w3m-keymap t
  143. "If non-nil, use emacs-w3m command keys in the article buffer."
  144. :version "22.1"
  145. :type 'boolean
  146. :group 'mime-display)
  147. (defcustom mm-enable-external t
  148. "Indicate whether external MIME handlers should be used.
  149. If t, all defined external MIME handlers are used. If nil, files are saved by
  150. `mailcap-save-binary-file'. If it is the symbol `ask', you are prompted
  151. before the external MIME handler is invoked."
  152. :version "22.1"
  153. :type '(choice (const :tag "Always" t)
  154. (const :tag "Never" nil)
  155. (const :tag "Ask" ask))
  156. :group 'mime-display)
  157. (defcustom mm-inline-media-tests
  158. '(("image/p?jpeg"
  159. mm-inline-image
  160. (lambda (handle)
  161. (mm-valid-and-fit-image-p 'jpeg handle)))
  162. ("image/png"
  163. mm-inline-image
  164. (lambda (handle)
  165. (mm-valid-and-fit-image-p 'png handle)))
  166. ("image/gif"
  167. mm-inline-image
  168. (lambda (handle)
  169. (mm-valid-and-fit-image-p 'gif handle)))
  170. ("image/tiff"
  171. mm-inline-image
  172. (lambda (handle)
  173. (mm-valid-and-fit-image-p 'tiff handle)))
  174. ("image/xbm"
  175. mm-inline-image
  176. (lambda (handle)
  177. (mm-valid-and-fit-image-p 'xbm handle)))
  178. ("image/x-xbitmap"
  179. mm-inline-image
  180. (lambda (handle)
  181. (mm-valid-and-fit-image-p 'xbm handle)))
  182. ("image/xpm"
  183. mm-inline-image
  184. (lambda (handle)
  185. (mm-valid-and-fit-image-p 'xpm handle)))
  186. ("image/x-xpixmap"
  187. mm-inline-image
  188. (lambda (handle)
  189. (mm-valid-and-fit-image-p 'xpm handle)))
  190. ("image/bmp"
  191. mm-inline-image
  192. (lambda (handle)
  193. (mm-valid-and-fit-image-p 'bmp handle)))
  194. ("image/x-portable-bitmap"
  195. mm-inline-image
  196. (lambda (handle)
  197. (mm-valid-and-fit-image-p 'pbm handle)))
  198. ("text/plain" mm-inline-text identity)
  199. ("text/enriched" mm-inline-text identity)
  200. ("text/richtext" mm-inline-text identity)
  201. ("text/x-patch" mm-display-patch-inline identity)
  202. ;; In case mime.types uses x-diff (as does Debian's mime-support-3.40).
  203. ("text/x-diff" mm-display-patch-inline identity)
  204. ("application/emacs-lisp" mm-display-elisp-inline identity)
  205. ("application/x-emacs-lisp" mm-display-elisp-inline identity)
  206. ("application/x-shellscript" mm-display-shell-script-inline identity)
  207. ("application/x-sh" mm-display-shell-script-inline identity)
  208. ("text/x-sh" mm-display-shell-script-inline identity)
  209. ("application/javascript" mm-display-javascript-inline identity)
  210. ("text/dns" mm-display-dns-inline identity)
  211. ("text/x-org" mm-display-org-inline identity)
  212. ("text/html"
  213. mm-inline-text-html
  214. (lambda (handle)
  215. mm-text-html-renderer))
  216. ("text/x-vcard"
  217. mm-inline-text-vcard
  218. (lambda (handle)
  219. (or (featurep 'vcard)
  220. (locate-library "vcard"))))
  221. ("message/delivery-status" mm-inline-text identity)
  222. ("message/rfc822" mm-inline-message identity)
  223. ("message/partial" mm-inline-partial identity)
  224. ("message/external-body" mm-inline-external-body identity)
  225. ("text/.*" mm-inline-text identity)
  226. ("audio/wav" mm-inline-audio
  227. (lambda (handle)
  228. (and (or (featurep 'nas-sound) (featurep 'native-sound))
  229. (device-sound-enabled-p))))
  230. ("audio/au"
  231. mm-inline-audio
  232. (lambda (handle)
  233. (and (or (featurep 'nas-sound) (featurep 'native-sound))
  234. (device-sound-enabled-p))))
  235. ("application/pgp-signature" ignore identity)
  236. ("application/x-pkcs7-signature" ignore identity)
  237. ("application/pkcs7-signature" ignore identity)
  238. ("application/x-pkcs7-mime" ignore identity)
  239. ("application/pkcs7-mime" ignore identity)
  240. ("multipart/alternative" ignore identity)
  241. ("multipart/mixed" ignore identity)
  242. ("multipart/related" ignore identity)
  243. ("image/.*"
  244. mm-inline-image
  245. (lambda (handle)
  246. (and (mm-valid-image-format-p 'imagemagick)
  247. (mm-with-unibyte-buffer
  248. (mm-insert-part handle)
  249. (let ((image
  250. (ignore-errors
  251. (if (fboundp 'create-image)
  252. (create-image (buffer-string) 'imagemagick 'data-p)
  253. (mm-create-image-xemacs (mm-handle-media-subtype handle))))))
  254. (when image
  255. (setcar (cdr handle) (list "image/imagemagick"))
  256. (mm-image-fit-p handle)))))))
  257. ;; Disable audio and image
  258. ("audio/.*" ignore ignore)
  259. ("image/.*" ignore ignore)
  260. ;; Default to displaying as text
  261. (".*" mm-inline-text mm-readable-p))
  262. "Alist of media types/tests saying whether types can be displayed inline."
  263. :type '(repeat (list (regexp :tag "MIME type")
  264. (function :tag "Display function")
  265. (function :tag "Display test")))
  266. :group 'mime-display)
  267. (defcustom mm-inlined-types
  268. '("image/.*" "text/.*" "message/delivery-status" "message/rfc822"
  269. "message/partial" "message/external-body" "application/emacs-lisp"
  270. "application/x-emacs-lisp"
  271. "application/pgp-signature" "application/x-pkcs7-signature"
  272. "application/pkcs7-signature" "application/x-pkcs7-mime"
  273. "application/pkcs7-mime"
  274. ;; Mutt still uses this even though it has already been withdrawn.
  275. "application/pgp")
  276. "List of media types that are to be displayed inline.
  277. See also `mm-inline-media-tests', which says how to display a media
  278. type inline."
  279. :type '(repeat regexp)
  280. :group 'mime-display)
  281. (defcustom mm-keep-viewer-alive-types
  282. '("application/postscript" "application/msword" "application/vnd.ms-excel"
  283. "application/pdf" "application/x-dvi")
  284. "List of media types for which the external viewer will not be killed
  285. when selecting a different article."
  286. :version "22.1"
  287. :type '(repeat regexp)
  288. :group 'mime-display)
  289. (defcustom mm-automatic-display
  290. '("text/plain" "text/enriched" "text/richtext" "text/html" "text/x-verbatim"
  291. "text/x-vcard" "image/.*" "message/delivery-status" "multipart/.*"
  292. "message/rfc822" "text/x-patch" "text/dns" "application/pgp-signature"
  293. "application/emacs-lisp" "application/x-emacs-lisp"
  294. "application/x-pkcs7-signature"
  295. "application/pkcs7-signature" "application/x-pkcs7-mime"
  296. "application/pkcs7-mime"
  297. ;; Mutt still uses this even though it has already been withdrawn.
  298. "application/pgp\\'"
  299. "text/x-org")
  300. "A list of MIME types to be displayed automatically."
  301. :type '(repeat regexp)
  302. :group 'mime-display)
  303. (defcustom mm-attachment-override-types '("text/x-vcard"
  304. "application/pkcs7-mime"
  305. "application/x-pkcs7-mime"
  306. "application/pkcs7-signature"
  307. "application/x-pkcs7-signature")
  308. "Types to have \"attachment\" ignored if they can be displayed inline."
  309. :type '(repeat regexp)
  310. :group 'mime-display)
  311. (defcustom mm-inline-override-types nil
  312. "Types to be treated as attachments even if they can be displayed inline."
  313. :type '(repeat regexp)
  314. :group 'mime-display)
  315. (defcustom mm-automatic-external-display nil
  316. "List of MIME type regexps that will be displayed externally automatically."
  317. :type '(repeat regexp)
  318. :group 'mime-display)
  319. (defcustom mm-discouraged-alternatives nil
  320. "List of MIME types that are discouraged when viewing multipart/alternative.
  321. Viewing agents are supposed to view the last possible part of a message,
  322. as that is supposed to be the richest. However, users may prefer other
  323. types instead, and this list says what types are most unwanted. If,
  324. for instance, text/html parts are very unwanted, and text/richtext are
  325. somewhat unwanted, then the value of this variable should be set
  326. to:
  327. (\"text/html\" \"text/richtext\")
  328. Adding \"image/.*\" might also be useful. Spammers use it as the
  329. preferred part of multipart/alternative messages. See also
  330. `gnus-buttonized-mime-types', to which adding \"multipart/alternative\"
  331. enables you to choose manually one of two types those mails include."
  332. :type '(repeat regexp) ;; See `mm-preferred-alternative-precedence'.
  333. :group 'mime-display)
  334. (defcustom mm-tmp-directory
  335. (if (fboundp 'temp-directory)
  336. (temp-directory)
  337. (if (boundp 'temporary-file-directory)
  338. temporary-file-directory
  339. "/tmp/"))
  340. "Where mm will store its temporary files."
  341. :type 'directory
  342. :group 'mime-display)
  343. (defcustom mm-inline-large-images nil
  344. "If t, then all images fit in the buffer.
  345. If 'resize, try to resize the images so they fit."
  346. :type '(radio
  347. (const :tag "Inline large images as they are." t)
  348. (const :tag "Resize large images." resize)
  349. (const :tag "Do not inline large images." nil))
  350. :group 'mime-display)
  351. (defcustom mm-file-name-rewrite-functions
  352. '(mm-file-name-delete-control mm-file-name-delete-gotchas)
  353. "List of functions used for rewriting file names of MIME parts.
  354. Each function takes a file name as input and returns a file name.
  355. Ready-made functions include `mm-file-name-delete-control',
  356. `mm-file-name-delete-gotchas' (you should not remove these two
  357. functions), `mm-file-name-delete-whitespace',
  358. `mm-file-name-trim-whitespace', `mm-file-name-collapse-whitespace',
  359. `mm-file-name-replace-whitespace', `capitalize', `downcase',
  360. `upcase', and `upcase-initials'."
  361. :type '(list (set :inline t
  362. (const mm-file-name-delete-control)
  363. (const mm-file-name-delete-gotchas)
  364. (const mm-file-name-delete-whitespace)
  365. (const mm-file-name-trim-whitespace)
  366. (const mm-file-name-collapse-whitespace)
  367. (const mm-file-name-replace-whitespace)
  368. (const capitalize)
  369. (const downcase)
  370. (const upcase)
  371. (const upcase-initials)
  372. (repeat :inline t
  373. :tag "Function"
  374. function)))
  375. :version "23.1" ;; No Gnus
  376. :group 'mime-display)
  377. (defvar mm-path-name-rewrite-functions nil
  378. "*List of functions for rewriting the full file names of MIME parts.
  379. This is used when viewing parts externally, and is meant for
  380. transforming the absolute name so that non-compliant programs can find
  381. the file where it's saved.
  382. Each function takes a file name as input and returns a file name.")
  383. (defvar mm-file-name-replace-whitespace nil
  384. "String used for replacing whitespace characters; default is `\"_\"'.")
  385. (defcustom mm-default-directory nil
  386. "The default directory where mm will save files.
  387. If not set, `default-directory' will be used."
  388. :type '(choice directory (const :tag "Default" nil))
  389. :group 'mime-display)
  390. (defcustom mm-attachment-file-modes 384
  391. "Set the mode bits of saved attachments to this integer."
  392. :version "22.1"
  393. :type 'integer
  394. :group 'mime-display)
  395. (defcustom mm-external-terminal-program "xterm"
  396. "The program to start an external terminal."
  397. :version "22.1"
  398. :type 'string
  399. :group 'mime-display)
  400. ;;; Internal variables.
  401. (defvar mm-last-shell-command "")
  402. (defvar mm-content-id-alist nil)
  403. (defvar mm-postponed-undisplay-list nil)
  404. ;; According to RFC2046, in particular, in a digest, the default
  405. ;; Content-Type value for a body part is changed from "text/plain" to
  406. ;; "message/rfc822".
  407. (defvar mm-dissect-default-type "text/plain")
  408. (autoload 'mml2015-verify "mml2015")
  409. (autoload 'mml2015-verify-test "mml2015")
  410. (autoload 'mml-smime-verify "mml-smime")
  411. (autoload 'mml-smime-verify-test "mml-smime")
  412. (defvar mm-verify-function-alist
  413. '(("application/pgp-signature" mml2015-verify "PGP" mml2015-verify-test)
  414. ("application/x-gnus-pgp-signature" mm-uu-pgp-signed-extract-1 "PGP"
  415. mm-uu-pgp-signed-test)
  416. ("application/pkcs7-signature" mml-smime-verify "S/MIME"
  417. mml-smime-verify-test)
  418. ("application/x-pkcs7-signature" mml-smime-verify "S/MIME"
  419. mml-smime-verify-test)))
  420. (defcustom mm-verify-option 'never
  421. "Option of verifying signed parts.
  422. `never', not verify; `always', always verify;
  423. `known', only verify known protocols. Otherwise, ask user.
  424. When set to `always' or `known', you should add
  425. \"multipart/signed\" to `gnus-buttonized-mime-types' to see
  426. result of the verification."
  427. :version "22.1"
  428. :type '(choice (item always)
  429. (item never)
  430. (item :tag "only known protocols" known)
  431. (item :tag "ask" nil))
  432. :group 'mime-security)
  433. (autoload 'mml2015-decrypt "mml2015")
  434. (autoload 'mml2015-decrypt-test "mml2015")
  435. (defvar mm-decrypt-function-alist
  436. '(("application/pgp-encrypted" mml2015-decrypt "PGP" mml2015-decrypt-test)
  437. ("application/x-gnus-pgp-encrypted" mm-uu-pgp-encrypted-extract-1 "PGP"
  438. mm-uu-pgp-encrypted-test)))
  439. (defcustom mm-decrypt-option nil
  440. "Option of decrypting encrypted parts.
  441. `never', not decrypt; `always', always decrypt;
  442. `known', only decrypt known protocols. Otherwise, ask user."
  443. :version "22.1"
  444. :type '(choice (item always)
  445. (item never)
  446. (item :tag "only known protocols" known)
  447. (item :tag "ask" nil))
  448. :group 'mime-security)
  449. (defvar mm-viewer-completion-map
  450. (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
  451. (set-keymap-parent map minibuffer-local-completion-map)
  452. ;; Should we bind other key to minibuffer-complete-word?
  453. (define-key map " " 'self-insert-command)
  454. map)
  455. "Keymap for input viewer with completion.")
  456. (defvar mm-viewer-completion-map
  457. (let ((map (make-sparse-keymap 'mm-viewer-completion-map)))
  458. (set-keymap-parent map minibuffer-local-completion-map)
  459. ;; Should we bind other key to minibuffer-complete-word?
  460. (define-key map " " 'self-insert-command)
  461. map)
  462. "Keymap for input viewer with completion.")
  463. ;;; The functions.
  464. (defun mm-alist-to-plist (alist)
  465. "Convert association list ALIST into the equivalent property-list form.
  466. The plist is returned. This converts from
  467. \((a . 1) (b . 2) (c . 3))
  468. into
  469. \(a 1 b 2 c 3)
  470. The original alist is not modified. See also `destructive-alist-to-plist'."
  471. (let (plist)
  472. (while alist
  473. (let ((el (car alist)))
  474. (setq plist (cons (cdr el) (cons (car el) plist))))
  475. (setq alist (cdr alist)))
  476. (nreverse plist)))
  477. (defun mm-keep-viewer-alive-p (handle)
  478. "Say whether external viewer for HANDLE should stay alive."
  479. (let ((types mm-keep-viewer-alive-types)
  480. (type (mm-handle-media-type handle))
  481. ty)
  482. (catch 'found
  483. (while (setq ty (pop types))
  484. (when (string-match ty type)
  485. (throw 'found t))))))
  486. (defun mm-handle-set-external-undisplayer (handle function)
  487. "Set the undisplayer for HANDLE to FUNCTION.
  488. Postpone undisplaying of viewers for types in
  489. `mm-keep-viewer-alive-types'."
  490. (if (mm-keep-viewer-alive-p handle)
  491. (let ((new-handle (copy-sequence handle)))
  492. (mm-handle-set-undisplayer new-handle function)
  493. (mm-handle-set-undisplayer handle nil)
  494. (push new-handle mm-postponed-undisplay-list))
  495. (mm-handle-set-undisplayer handle function)))
  496. (defun mm-destroy-postponed-undisplay-list ()
  497. (when mm-postponed-undisplay-list
  498. (message "Destroying external MIME viewers")
  499. (mm-destroy-parts mm-postponed-undisplay-list)))
  500. (autoload 'message-fetch-field "message")
  501. (defun mm-dissect-buffer (&optional no-strict-mime loose-mime from)
  502. "Dissect the current buffer and return a list of MIME handles."
  503. (save-excursion
  504. (let (ct ctl type subtype cte cd description id result)
  505. (save-restriction
  506. (mail-narrow-to-head)
  507. (when (or no-strict-mime
  508. loose-mime
  509. (mail-fetch-field "mime-version"))
  510. (setq ct (mail-fetch-field "content-type")
  511. ctl (and ct (mail-header-parse-content-type ct))
  512. cte (mail-fetch-field "content-transfer-encoding")
  513. cd (or (mail-fetch-field "content-disposition")
  514. (when (and ctl
  515. (eq 'mm-inline-text
  516. (cadr (mm-assoc-string-match
  517. mm-inline-media-tests
  518. (car ctl)))))
  519. "inline"))
  520. ;; Newlines in description should be stripped so as
  521. ;; not to break the MIME tag into two or more lines.
  522. description (message-fetch-field "content-description")
  523. id (mail-fetch-field "content-id"))
  524. (unless from
  525. (setq from (mail-fetch-field "from")))
  526. ;; FIXME: In some circumstances, this code is running within
  527. ;; an unibyte macro. mail-extract-address-components
  528. ;; creates unibyte buffers. This `if', though not a perfect
  529. ;; solution, avoids most of them.
  530. (if from
  531. (setq from (cadr (mail-extract-address-components from))))
  532. (if description
  533. (setq description (mail-decode-encoded-word-string
  534. description)))))
  535. (if (or (not ctl)
  536. (not (string-match "/" (car ctl))))
  537. (mm-dissect-singlepart
  538. (list mm-dissect-default-type)
  539. (and cte (intern (downcase (mail-header-strip cte))))
  540. no-strict-mime
  541. (and cd (mail-header-parse-content-disposition cd))
  542. description)
  543. (setq type (split-string (car ctl) "/"))
  544. (setq subtype (cadr type)
  545. type (car type))
  546. (setq
  547. result
  548. (cond
  549. ((equal type "multipart")
  550. (let ((mm-dissect-default-type (if (equal subtype "digest")
  551. "message/rfc822"
  552. "text/plain"))
  553. (start (cdr (assq 'start (cdr ctl)))))
  554. (add-text-properties 0 (length (car ctl))
  555. (mm-alist-to-plist (cdr ctl)) (car ctl))
  556. ;; what really needs to be done here is a way to link a
  557. ;; MIME handle back to it's parent MIME handle (in a multilevel
  558. ;; MIME article). That would probably require changing
  559. ;; the mm-handle API so we simply store the multipart buffer
  560. ;; name as a text property of the "multipart/whatever" string.
  561. (add-text-properties 0 (length (car ctl))
  562. (list 'buffer (mm-copy-to-buffer)
  563. 'from from
  564. 'start start)
  565. (car ctl))
  566. (cons (car ctl) (mm-dissect-multipart ctl from))))
  567. (t
  568. (mm-possibly-verify-or-decrypt
  569. (mm-dissect-singlepart
  570. ctl
  571. (and cte (intern (downcase (mail-header-strip cte))))
  572. no-strict-mime
  573. (and cd (mail-header-parse-content-disposition cd))
  574. description id)
  575. ctl from))))
  576. (when id
  577. (when (string-match " *<\\(.*\\)> *" id)
  578. (setq id (match-string 1 id)))
  579. (push (cons id result) mm-content-id-alist))
  580. result))))
  581. (defun mm-dissect-singlepart (ctl cte &optional force cdl description id)
  582. (when (or force
  583. (if (equal "text/plain" (car ctl))
  584. (assoc 'format ctl)
  585. t))
  586. (mm-make-handle
  587. (mm-copy-to-buffer) ctl cte nil cdl description nil id)))
  588. (defun mm-dissect-multipart (ctl from)
  589. (goto-char (point-min))
  590. (let* ((boundary (concat "\n--" (mail-content-type-get ctl 'boundary)))
  591. (close-delimiter (concat (regexp-quote boundary) "--[ \t]*$"))
  592. start parts
  593. (end (save-excursion
  594. (goto-char (point-max))
  595. (if (re-search-backward close-delimiter nil t)
  596. (match-beginning 0)
  597. (point-max)))))
  598. (setq boundary (concat (regexp-quote boundary) "[ \t]*$"))
  599. (while (and (< (point) end) (re-search-forward boundary end t))
  600. (goto-char (match-beginning 0))
  601. (when start
  602. (save-excursion
  603. (save-restriction
  604. (narrow-to-region start (point))
  605. (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
  606. (end-of-line 2)
  607. (or (looking-at boundary)
  608. (forward-line 1))
  609. (setq start (point)))
  610. (when (and start (< start end))
  611. (save-excursion
  612. (save-restriction
  613. (narrow-to-region start end)
  614. (setq parts (nconc (list (mm-dissect-buffer t nil from)) parts)))))
  615. (mm-possibly-verify-or-decrypt (nreverse parts) ctl from)))
  616. (defun mm-copy-to-buffer ()
  617. "Copy the contents of the current buffer to a fresh buffer."
  618. (let ((obuf (current-buffer))
  619. (mb (mm-multibyte-p))
  620. beg)
  621. (goto-char (point-min))
  622. (search-forward-regexp "^\n" nil t)
  623. (setq beg (point))
  624. (with-current-buffer
  625. (generate-new-buffer " *mm*")
  626. ;; Preserve the data's unibyteness (for url-insert-file-contents).
  627. (mm-set-buffer-multibyte mb)
  628. (insert-buffer-substring obuf beg)
  629. (current-buffer))))
  630. (defun mm-display-parts (handle &optional no-default)
  631. (if (stringp (car handle))
  632. (mapcar 'mm-display-parts (cdr handle))
  633. (if (bufferp (car handle))
  634. (save-restriction
  635. (narrow-to-region (point) (point))
  636. (mm-display-part handle)
  637. (goto-char (point-max)))
  638. (mapcar 'mm-display-parts handle))))
  639. (autoload 'mailcap-parse-mailcaps "mailcap")
  640. (autoload 'mailcap-mime-info "mailcap")
  641. (defun mm-display-part (handle &optional no-default force)
  642. "Display the MIME part represented by HANDLE.
  643. Returns nil if the part is removed; inline if displayed inline;
  644. external if displayed external."
  645. (save-excursion
  646. (mailcap-parse-mailcaps)
  647. (if (and (not force)
  648. (mm-handle-displayed-p handle))
  649. (mm-remove-part handle)
  650. (let* ((ehandle (if (equal (mm-handle-media-type handle)
  651. "message/external-body")
  652. (progn
  653. (unless (mm-handle-cache handle)
  654. (mm-extern-cache-contents handle))
  655. (mm-handle-cache handle))
  656. handle))
  657. (type (mm-handle-media-type ehandle))
  658. (method (mailcap-mime-info type))
  659. (filename (or (mail-content-type-get
  660. (mm-handle-disposition handle) 'filename)
  661. (mail-content-type-get
  662. (mm-handle-type handle) 'name)
  663. "<file>"))
  664. (external mm-enable-external))
  665. (if (and (mm-inlinable-p ehandle)
  666. (mm-inlined-p ehandle))
  667. (progn
  668. (forward-line 1)
  669. (mm-display-inline handle)
  670. 'inline)
  671. (when (or method
  672. (not no-default))
  673. (if (and (not method)
  674. (equal "text" (car (split-string type "/"))))
  675. (progn
  676. (forward-line 1)
  677. (mm-insert-inline handle (mm-get-part handle))
  678. 'inline)
  679. (setq external
  680. (and method ;; If nil, we always use "save".
  681. (stringp method) ;; 'mailcap-save-binary-file
  682. (or (eq mm-enable-external t)
  683. (and (eq mm-enable-external 'ask)
  684. (y-or-n-p
  685. (concat
  686. "Display part (" type
  687. ") using external program"
  688. ;; Can non-string method ever happen?
  689. (if (stringp method)
  690. (concat
  691. " \"" (format method filename) "\"")
  692. "")
  693. "? "))))))
  694. (if external
  695. (mm-display-external
  696. handle (or method 'mailcap-save-binary-file))
  697. (mm-display-external
  698. handle 'mailcap-save-binary-file)))))))))
  699. (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
  700. (defvar mailcap-mime-extensions) ; mailcap-mime-info autoloads
  701. (defun mm-display-external (handle method)
  702. "Display HANDLE using METHOD."
  703. (let ((outbuf (current-buffer)))
  704. (mm-with-unibyte-buffer
  705. (if (functionp method)
  706. (let ((cur (current-buffer)))
  707. (if (eq method 'mailcap-save-binary-file)
  708. (progn
  709. (set-buffer (generate-new-buffer " *mm*"))
  710. (setq method nil))
  711. (mm-insert-part handle)
  712. (mm-add-meta-html-tag handle)
  713. (let ((win (get-buffer-window cur t)))
  714. (when win
  715. (select-window win)))
  716. (switch-to-buffer (generate-new-buffer " *mm*")))
  717. (buffer-disable-undo)
  718. (mm-set-buffer-file-coding-system mm-binary-coding-system)
  719. (insert-buffer-substring cur)
  720. (goto-char (point-min))
  721. (when method
  722. (message "Viewing with %s" method))
  723. (let ((mm (current-buffer))
  724. (non-viewer (assq 'non-viewer
  725. (mailcap-mime-info
  726. (mm-handle-media-type handle) t))))
  727. (unwind-protect
  728. (if method
  729. (funcall method)
  730. (mm-save-part handle))
  731. (when (and (not non-viewer)
  732. method)
  733. (mm-handle-set-undisplayer handle mm)))))
  734. ;; The function is a string to be executed.
  735. (mm-insert-part handle)
  736. (mm-add-meta-html-tag handle)
  737. (let* ((dir (mm-make-temp-file
  738. (expand-file-name "emm." mm-tmp-directory) 'dir))
  739. (filename (or
  740. (mail-content-type-get
  741. (mm-handle-disposition handle) 'filename)
  742. (mail-content-type-get
  743. (mm-handle-type handle) 'name)))
  744. (mime-info (mailcap-mime-info
  745. (mm-handle-media-type handle) t))
  746. (needsterm (or (assoc "needsterm" mime-info)
  747. (assoc "needsterminal" mime-info)))
  748. (copiousoutput (assoc "copiousoutput" mime-info))
  749. file buffer)
  750. ;; We create a private sub-directory where we store our files.
  751. (set-file-modes dir #o700)
  752. (if filename
  753. (setq file (expand-file-name
  754. (gnus-map-function mm-file-name-rewrite-functions
  755. (file-name-nondirectory filename))
  756. dir))
  757. ;; Use nametemplate (defined in RFC1524) if it is specified
  758. ;; in mailcap.
  759. (let ((suffix (cdr (assoc "nametemplate" mime-info))))
  760. (if (and suffix
  761. (string-match "\\`%s\\(\\..+\\)\\'" suffix))
  762. (setq suffix (match-string 1 suffix))
  763. ;; Otherwise, use a suffix according to
  764. ;; `mailcap-mime-extensions'.
  765. (setq suffix (car (rassoc (mm-handle-media-type handle)
  766. mailcap-mime-extensions))))
  767. (setq file (mm-make-temp-file (expand-file-name "mm." dir)
  768. nil suffix))))
  769. (let ((coding-system-for-write mm-binary-coding-system))
  770. (write-region (point-min) (point-max) file nil 'nomesg))
  771. ;; The file is deleted after the viewer exists. If the users edits
  772. ;; the file, changes will be lost. Set file to read-only to make it
  773. ;; clear.
  774. (set-file-modes file #o400)
  775. (message "Viewing with %s" method)
  776. (cond
  777. (needsterm
  778. (let ((command (mm-mailcap-command
  779. method file (mm-handle-type handle))))
  780. (unwind-protect
  781. (if window-system
  782. (start-process "*display*" nil
  783. mm-external-terminal-program
  784. "-e" shell-file-name
  785. shell-command-switch command)
  786. (require 'term)
  787. (require 'gnus-win)
  788. (set-buffer
  789. (setq buffer
  790. (make-term "display"
  791. shell-file-name
  792. nil
  793. shell-command-switch command)))
  794. (term-mode)
  795. (term-char-mode)
  796. (set-process-sentinel
  797. (get-buffer-process buffer)
  798. `(lambda (process state)
  799. (if (eq 'exit (process-status process))
  800. (gnus-configure-windows
  801. ',gnus-current-window-configuration))))
  802. (gnus-configure-windows 'display-term))
  803. (mm-handle-set-external-undisplayer handle (cons file buffer)))
  804. (message "Displaying %s..." command))
  805. 'external)
  806. (copiousoutput
  807. (with-current-buffer outbuf
  808. (forward-line 1)
  809. (mm-insert-inline
  810. handle
  811. (unwind-protect
  812. (progn
  813. (call-process shell-file-name nil
  814. (setq buffer
  815. (generate-new-buffer " *mm*"))
  816. nil
  817. shell-command-switch
  818. (mm-mailcap-command
  819. method file (mm-handle-type handle)))
  820. (if (buffer-live-p buffer)
  821. (with-current-buffer buffer
  822. (buffer-string))))
  823. (progn
  824. (ignore-errors (delete-file file))
  825. (ignore-errors (delete-directory
  826. (file-name-directory file)))
  827. (ignore-errors (kill-buffer buffer))))))
  828. 'inline)
  829. (t
  830. ;; Deleting the temp file should be postponed for some wrappers,
  831. ;; shell scripts, and so on, which might exit right after having
  832. ;; started a viewer command as a background job.
  833. (let ((command (mm-mailcap-command
  834. method file (mm-handle-type handle))))
  835. (unwind-protect
  836. (progn
  837. (start-process "*display*"
  838. (setq buffer
  839. (generate-new-buffer " *mm*"))
  840. shell-file-name
  841. shell-command-switch command)
  842. (set-process-sentinel
  843. (get-buffer-process buffer)
  844. (lexical-let ;; Don't use `let'.
  845. ;; Function used to remove temp file and directory.
  846. ((fn `(lambda nil
  847. ;; Don't use `ignore-errors'.
  848. (condition-case nil
  849. (delete-file ,file)
  850. (error))
  851. (condition-case nil
  852. (delete-directory
  853. ,(file-name-directory file))
  854. (error))))
  855. ;; Form uses to kill the process buffer and
  856. ;; remove the undisplayer.
  857. (fm `(progn
  858. (kill-buffer ,buffer)
  859. ,(macroexpand
  860. (list 'mm-handle-set-undisplayer
  861. (list 'quote handle)
  862. nil))))
  863. ;; Message to be issued when the process exits.
  864. (done (format "Displaying %s...done" command))
  865. ;; In particular, the timer object (which is
  866. ;; a vector in Emacs but is a list in XEmacs)
  867. ;; requires that it is lexically scoped.
  868. (timer (run-at-time 30.0 nil 'ignore)))
  869. (if (featurep 'xemacs)
  870. (lambda (process state)
  871. (when (eq 'exit (process-status process))
  872. (if (memq timer itimer-list)
  873. (set-itimer-function timer fn)
  874. (funcall fn))
  875. (ignore-errors (eval fm))
  876. (message "%s" done)))
  877. (lambda (process state)
  878. (when (eq 'exit (process-status process))
  879. (if (memq timer timer-list)
  880. (timer-set-function timer fn)
  881. (funcall fn))
  882. (ignore-errors (eval fm))
  883. (message "%s" done)))))))
  884. (mm-handle-set-external-undisplayer
  885. handle (cons file buffer)))
  886. (message "Displaying %s..." command))
  887. 'external)))))))
  888. (defun mm-mailcap-command (method file type-list)
  889. (let ((ctl (cdr type-list))
  890. (beg 0)
  891. (uses-stdin t)
  892. out sub total)
  893. (while (string-match "%{\\([^}]+\\)}\\|'%s'\\|\"%s\"\\|%s\\|%t\\|%%"
  894. method beg)
  895. (push (substring method beg (match-beginning 0)) out)
  896. (setq beg (match-end 0)
  897. total (match-string 0 method)
  898. sub (match-string 1 method))
  899. (cond
  900. ((string= total "%%")
  901. (push "%" out))
  902. ((or (string= total "%s")
  903. ;; We do our own quoting.
  904. (string= total "'%s'")
  905. (string= total "\"%s\""))
  906. (setq uses-stdin nil)
  907. (push (shell-quote-argument
  908. (gnus-map-function mm-path-name-rewrite-functions file)) out))
  909. ((string= total "%t")
  910. (push (shell-quote-argument (car type-list)) out))
  911. (t
  912. (push (shell-quote-argument (or (cdr (assq (intern sub) ctl)) "")) out))))
  913. (push (substring method beg (length method)) out)
  914. (when uses-stdin
  915. (push "<" out)
  916. (push (shell-quote-argument
  917. (gnus-map-function mm-path-name-rewrite-functions file))
  918. out))
  919. (mapconcat 'identity (nreverse out) "")))
  920. (defun mm-remove-parts (handles)
  921. "Remove the displayed MIME parts represented by HANDLES."
  922. (if (and (listp handles)
  923. (bufferp (car handles)))
  924. (mm-remove-part handles)
  925. (let (handle)
  926. (while (setq handle (pop handles))
  927. (cond
  928. ((stringp handle)
  929. (when (buffer-live-p (get-text-property 0 'buffer handle))
  930. (kill-buffer (get-text-property 0 'buffer handle))))
  931. ((and (listp handle)
  932. (stringp (car handle)))
  933. (mm-remove-parts (cdr handle)))
  934. (t
  935. (mm-remove-part handle)))))))
  936. (defun mm-destroy-parts (handles)
  937. "Remove the displayed MIME parts represented by HANDLES."
  938. (if (and (listp handles)
  939. (bufferp (car handles)))
  940. (mm-destroy-part handles)
  941. (let (handle)
  942. (while (setq handle (pop handles))
  943. (cond
  944. ((stringp handle)
  945. (when (buffer-live-p (get-text-property 0 'buffer handle))
  946. (kill-buffer (get-text-property 0 'buffer handle))))
  947. ((and (listp handle)
  948. (stringp (car handle)))
  949. (mm-destroy-parts handle))
  950. (t
  951. (mm-destroy-part handle)))))))
  952. (defun mm-remove-part (handle)
  953. "Remove the displayed MIME part represented by HANDLE."
  954. (when (listp handle)
  955. (let ((object (mm-handle-undisplayer handle)))
  956. (ignore-errors
  957. (cond
  958. ;; Internally displayed part.
  959. ((mm-annotationp object)
  960. (if (featurep 'xemacs)
  961. (delete-annotation object)))
  962. ((or (functionp object)
  963. (and (listp object)
  964. (eq (car object) 'lambda)))
  965. (funcall object))
  966. ;; Externally displayed part.
  967. ((consp object)
  968. (condition-case ()
  969. (while (get-buffer-process (cdr object))
  970. (interrupt-process (get-buffer-process (cdr object)))
  971. (message "Waiting for external displayer to die...")
  972. (sit-for 1))
  973. (quit)
  974. (error))
  975. (ignore-errors (and (cdr object) (kill-buffer (cdr object))))
  976. (message "Waiting for external displayer to die...done")
  977. (ignore-errors (delete-file (car object)))
  978. (ignore-errors (delete-directory (file-name-directory
  979. (car object)))))
  980. ((bufferp object)
  981. (when (buffer-live-p object)
  982. (kill-buffer object)))))
  983. (mm-handle-set-undisplayer handle nil))))
  984. (defun mm-display-inline (handle)
  985. (let* ((type (mm-handle-media-type handle))
  986. (function (cadr (mm-assoc-string-match mm-inline-media-tests type))))
  987. (funcall function handle)
  988. (goto-char (point-min))))
  989. (defun mm-assoc-string-match (alist type)
  990. (dolist (elem alist)
  991. (when (string-match (car elem) type)
  992. (return elem))))
  993. (defun mm-automatic-display-p (handle)
  994. "Say whether the user wants HANDLE to be displayed automatically."
  995. (let ((methods mm-automatic-display)
  996. (type (mm-handle-media-type handle))
  997. method result)
  998. (while (setq method (pop methods))
  999. (when (and (not (mm-inline-override-p handle))
  1000. (string-match method type))
  1001. (setq result t
  1002. methods nil)))
  1003. result))
  1004. (defun mm-inlinable-p (handle &optional type)
  1005. "Say whether HANDLE can be displayed inline.
  1006. TYPE is the mime-type of the object; it defaults to the one given
  1007. in HANDLE."
  1008. (unless type (setq type (mm-handle-media-type handle)))
  1009. (let ((alist mm-inline-media-tests)
  1010. test)
  1011. (while alist
  1012. (when (string-match (caar alist) type)
  1013. (setq test (caddar alist)
  1014. alist nil)
  1015. (setq test (funcall test handle)))
  1016. (pop alist))
  1017. test))
  1018. (defun mm-inlined-p (handle)
  1019. "Say whether the user wants HANDLE to be displayed inline."
  1020. (let ((methods mm-inlined-types)
  1021. (type (mm-handle-media-type handle))
  1022. method result)
  1023. (while (setq method (pop methods))
  1024. (when (and (not (mm-inline-override-p handle))
  1025. (string-match method type))
  1026. (setq result t
  1027. methods nil)))
  1028. result))
  1029. (defun mm-attachment-override-p (handle)
  1030. "Say whether HANDLE should have attachment behavior overridden."
  1031. (let ((types mm-attachment-override-types)
  1032. (type (mm-handle-media-type handle))
  1033. ty)
  1034. (catch 'found
  1035. (while (setq ty (pop types))
  1036. (when (and (string-match ty type)
  1037. (mm-inlinable-p handle))
  1038. (throw 'found t))))))
  1039. (defun mm-inline-override-p (handle)
  1040. "Say whether HANDLE should have inline behavior overridden."
  1041. (let ((types mm-inline-override-types)
  1042. (type (mm-handle-media-type handle))
  1043. ty)
  1044. (catch 'found
  1045. (while (setq ty (pop types))
  1046. (when (string-match ty type)
  1047. (throw 'found t))))))
  1048. (defun mm-automatic-external-display-p (type)
  1049. "Return the user-defined method for TYPE."
  1050. (let ((methods mm-automatic-external-display)
  1051. method result)
  1052. (while (setq method (pop methods))
  1053. (when (string-match method type)
  1054. (setq result t
  1055. methods nil)))
  1056. result))
  1057. (defun mm-destroy-part (handle)
  1058. "Destroy the data structures connected to HANDLE."
  1059. (when (listp handle)
  1060. (mm-remove-part handle)
  1061. (when (buffer-live-p (mm-handle-buffer handle))
  1062. (kill-buffer (mm-handle-buffer handle)))))
  1063. (defun mm-handle-displayed-p (handle)
  1064. "Say whether HANDLE is displayed or not."
  1065. (mm-handle-undisplayer handle))
  1066. ;;;
  1067. ;;; Functions for outputting parts
  1068. ;;;
  1069. (defmacro mm-with-part (handle &rest forms)
  1070. "Run FORMS in the temp buffer containing the contents of HANDLE."
  1071. ;; The handle-buffer's content is a sequence of bytes, not a sequence of
  1072. ;; chars, so the buffer should be unibyte. It may happen that the
  1073. ;; handle-buffer is multibyte for some reason, in which case now is a good
  1074. ;; time to adjust it, since we know at this point that it should
  1075. ;; be unibyte.
  1076. `(let* ((handle ,handle))
  1077. (when (and (mm-handle-buffer handle)
  1078. (buffer-name (mm-handle-buffer handle)))
  1079. (with-temp-buffer
  1080. (mm-disable-multibyte)
  1081. (insert-buffer-substring (mm-handle-buffer handle))
  1082. (mm-decode-content-transfer-encoding
  1083. (mm-handle-encoding handle)
  1084. (mm-handle-media-type handle))
  1085. ,@forms))))
  1086. (put 'mm-with-part 'lisp-indent-function 1)
  1087. (put 'mm-with-part 'edebug-form-spec '(body))
  1088. (defun mm-get-part (handle &optional no-cache)
  1089. "Return the contents of HANDLE as a string.
  1090. If NO-CACHE is non-nil, cached contents of a message/external-body part
  1091. are ignored."
  1092. (if (and (not no-cache)
  1093. (equal (mm-handle-media-type handle) "message/external-body"))
  1094. (progn
  1095. (unless (mm-handle-cache handle)
  1096. (mm-extern-cache-contents handle))
  1097. (with-current-buffer (mm-handle-buffer (mm-handle-cache handle))
  1098. (buffer-string)))
  1099. (mm-with-part handle
  1100. (buffer-string))))
  1101. (defun mm-insert-part (handle &optional no-cache)
  1102. "Insert the contents of HANDLE in the current buffer.
  1103. If NO-CACHE is non-nil, cached contents of a message/external-body part
  1104. are ignored."
  1105. (let ((text (cond ((eq (mail-content-type-get (mm-handle-type handle)
  1106. 'charset)
  1107. 'gnus-decoded)
  1108. (with-current-buffer (mm-handle-buffer handle)
  1109. (buffer-string)))
  1110. ((mm-multibyte-p)
  1111. (mm-string-to-multibyte (mm-get-part handle no-cache)))
  1112. (t
  1113. (mm-get-part handle no-cache)))))
  1114. (save-restriction
  1115. (widen)
  1116. (goto-char
  1117. (prog1
  1118. (point)
  1119. (if (and (eq (get-char-property (max (point-min) (1- (point))) 'face)
  1120. 'mm-uu-extract)
  1121. (eq (get-char-property 0 'face text) 'mm-uu-extract))
  1122. ;; Separate the extracted parts that have the same faces.
  1123. (insert "\n" text)
  1124. (insert text)))))))
  1125. (defun mm-file-name-delete-whitespace (file-name)
  1126. "Remove all whitespace characters from FILE-NAME."
  1127. (while (string-match "\\s-+" file-name)
  1128. (setq file-name (replace-match "" t t file-name)))
  1129. file-name)
  1130. (defun mm-file-name-trim-whitespace (file-name)
  1131. "Remove leading and trailing whitespace characters from FILE-NAME."
  1132. (when (string-match "\\`\\s-+" file-name)
  1133. (setq file-name (substring file-name (match-end 0))))
  1134. (when (string-match "\\s-+\\'" file-name)
  1135. (setq file-name (substring file-name 0 (match-beginning 0))))
  1136. file-name)
  1137. (defun mm-file-name-collapse-whitespace (file-name)
  1138. "Collapse multiple whitespace characters in FILE-NAME."
  1139. (while (string-match "\\s-\\s-+" file-name)
  1140. (setq file-name (replace-match " " t t file-name)))
  1141. file-name)
  1142. (defun mm-file-name-replace-whitespace (file-name)
  1143. "Replace whitespace characters in FILE-NAME with underscores.
  1144. Set the option `mm-file-name-replace-whitespace' to any other
  1145. string if you do not like underscores."
  1146. (let ((s (or mm-file-name-replace-whitespace "_")))
  1147. (while (string-match "\\s-" file-name)
  1148. (setq file-name (replace-match s t t file-name))))
  1149. file-name)
  1150. (defun mm-file-name-delete-control (filename)
  1151. "Delete control characters from FILENAME."
  1152. (gnus-replace-in-string filename "[\x00-\x1f\x7f]" ""))
  1153. (defun mm-file-name-delete-gotchas (filename)
  1154. "Delete shell gotchas from FILENAME."
  1155. (setq filename (gnus-replace-in-string filename "[<>|]" ""))
  1156. (gnus-replace-in-string filename "^[.-]+" ""))
  1157. (defun mm-save-part (handle &optional prompt)
  1158. "Write HANDLE to a file.
  1159. PROMPT overrides the default one used to ask user for a file name."
  1160. (let ((filename (or (mail-content-type-get
  1161. (mm-handle-disposition handle) 'filename)
  1162. (mail-content-type-get
  1163. (mm-handle-type handle) 'name)))
  1164. file)
  1165. (when filename
  1166. (setq filename (gnus-map-function mm-file-name-rewrite-functions
  1167. (file-name-nondirectory filename))))
  1168. (setq file
  1169. (read-file-name
  1170. (or prompt
  1171. (format "Save MIME part to (default %s): "
  1172. (or filename "")))
  1173. (or mm-default-directory default-directory)
  1174. (expand-file-name (or filename "")
  1175. (or mm-default-directory default-directory))))
  1176. (if (file-directory-p file)
  1177. (setq file (expand-file-name filename file))
  1178. (setq file (expand-file-name
  1179. file (or mm-default-directory default-directory))))
  1180. (setq mm-default-directory (file-name-directory file))
  1181. (and (or (not (file-exists-p file))
  1182. (yes-or-no-p (format "File %s already exists; overwrite? "
  1183. file)))
  1184. (progn
  1185. (mm-save-part-to-file handle file)
  1186. file))))
  1187. (defun mm-add-meta-html-tag (handle &optional charset force-charset)
  1188. "Add meta html tag to specify CHARSET of HANDLE in the current buffer.
  1189. CHARSET defaults to the one HANDLE specifies. Existing meta tag that
  1190. specifies charset will not be modified unless FORCE-CHARSET is non-nil.
  1191. Return t if meta tag is added or replaced."
  1192. (when (equal (mm-handle-media-type handle) "text/html")
  1193. (when (or charset
  1194. (setq charset (mail-content-type-get (mm-handle-type handle)
  1195. 'charset)))
  1196. (setq charset (format "\
  1197. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">" charset))
  1198. (let ((case-fold-search t))
  1199. (goto-char (point-min))
  1200. (if (re-search-forward "\
  1201. <meta\\s-+http-equiv=[\"']?content-type[\"']?\\s-+content=[\"']\
  1202. text/\\(\\sw+\\)\\(?:\;\\s-*charset=\\(.+\\)\\)?[\"'][^>]*>" nil t)
  1203. (if (and (not force-charset)
  1204. (match-beginning 2)
  1205. (string-match "\\`html\\'" (match-string 1)))
  1206. ;; Don't modify existing meta tag.
  1207. nil
  1208. ;; Replace it with the one specifying charset.
  1209. (replace-match charset)
  1210. t)
  1211. (if (re-search-forward "<head>\\s-*" nil t)
  1212. (insert charset "\n")
  1213. (re-search-forward "<html\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
  1214. (insert "<head>\n" charset "\n</head>\n"))
  1215. t)))))
  1216. (defun mm-save-part-to-file (handle file)
  1217. (mm-with-unibyte-buffer
  1218. (mm-insert-part handle)
  1219. (mm-add-meta-html-tag handle)
  1220. (let ((current-file-modes (default-file-modes)))
  1221. (set-default-file-modes mm-attachment-file-modes)
  1222. (unwind-protect
  1223. ;; Don't re-compress .gz & al. Arguably we should make
  1224. ;; `file-name-handler-alist' nil, but that would chop
  1225. ;; ange-ftp, which is reasonable to use here.
  1226. (mm-write-region (point-min) (point-max) file nil nil nil 'binary t)
  1227. (set-default-file-modes current-file-modes)))))
  1228. (defun mm-pipe-part (handle &optional cmd)
  1229. "Pipe HANDLE to a process.
  1230. Use CMD as the process."
  1231. (let ((name (mail-content-type-get (mm-handle-type handle) 'name))
  1232. (command (or cmd
  1233. (gnus-read-shell-command
  1234. "Shell command on MIME part: " mm-last-shell-command))))
  1235. (mm-with-unibyte-buffer
  1236. (mm-insert-part handle)
  1237. (mm-add-meta-html-tag handle)
  1238. (let ((coding-system-for-write 'binary))
  1239. (shell-command-on-region (point-min) (point-max) command nil)))))
  1240. (autoload 'gnus-completing-read "gnus-util")
  1241. (defun mm-interactively-view-part (handle)
  1242. "Display HANDLE using METHOD."
  1243. (let* ((type (mm-handle-media-type handle))
  1244. (methods
  1245. (mapcar (lambda (i) (cdr (assoc 'viewer i)))
  1246. (mailcap-mime-info type 'all)))
  1247. (method (let ((minibuffer-local-completion-map
  1248. mm-viewer-completion-map))
  1249. (completing-read "Viewer: " methods))))
  1250. (when (string= method "")
  1251. (error "No method given"))
  1252. (if (string-match "^[^% \t]+$" method)
  1253. (setq method (concat method " %s")))
  1254. (mm-display-external handle method)))
  1255. (defun mm-preferred-alternative (handles &optional preferred)
  1256. "Say which of HANDLES are preferred."
  1257. (let ((prec (if preferred (list preferred)
  1258. (mm-preferred-alternative-precedence handles)))
  1259. p h result type handle)
  1260. (while (setq p (pop prec))
  1261. (setq h handles)
  1262. (while h
  1263. (setq handle (car h))
  1264. (setq type (mm-handle-media-type handle))
  1265. (when (and (equal p type)
  1266. (mm-automatic-display-p handle)
  1267. (or (stringp (car handle))
  1268. (not (mm-handle-disposition handle))
  1269. (equal (car (mm-handle-disposition handle))
  1270. "inline")))
  1271. (setq result handle
  1272. h nil
  1273. prec nil))
  1274. (pop h)))
  1275. result))
  1276. (defun mm-preferred-alternative-precedence (handles)
  1277. "Return the precedence based on HANDLES and `mm-discouraged-alternatives'."
  1278. (setq handles (reverse handles))
  1279. (dolist (disc (reverse mm-discouraged-alternatives))
  1280. (dolist (handle (copy-sequence handles))
  1281. (when (string-match disc (mm-handle-media-type handle))
  1282. (setq handles (nconc (delete handle handles) (list handle))))))
  1283. ;; Remove empty parts.
  1284. (dolist (handle (copy-sequence handles))
  1285. (when (and (bufferp (mm-handle-buffer handle))
  1286. (not (with-current-buffer (mm-handle-buffer handle)
  1287. (goto-char (point-min))
  1288. (re-search-forward "[^ \t\n]" nil t))))
  1289. (setq handles (nconc (delete handle handles) (list handle)))))
  1290. (mapcar #'mm-handle-media-type handles))
  1291. (defun mm-get-content-id (id)
  1292. "Return the handle(s) referred to by ID."
  1293. (cdr (assoc id mm-content-id-alist)))
  1294. (defconst mm-image-type-regexps
  1295. '(("/\\*.*XPM.\\*/" . xpm)
  1296. ("P[1-6]" . pbm)
  1297. ("GIF8" . gif)
  1298. ("\377\330" . jpeg)
  1299. ("\211PNG\r\n" . png)
  1300. ("#define" . xbm)
  1301. ("\\(MM\0\\*\\)\\|\\(II\\*\0\\)" . tiff)
  1302. ("%!PS" . postscript))
  1303. "Alist of (REGEXP . IMAGE-TYPE) pairs used to auto-detect image types.
  1304. When the first bytes of an image file match REGEXP, it is assumed to
  1305. be of image type IMAGE-TYPE.")
  1306. ;; Steal from image.el. image-type-from-data suffers multi-line matching bug.
  1307. (defun mm-image-type-from-buffer ()
  1308. "Determine the image type from data in the current buffer.
  1309. Value is a symbol specifying the image type or nil if type cannot
  1310. be determined."
  1311. (let ((types mm-image-type-regexps)
  1312. type)
  1313. (goto-char (point-min))
  1314. (while (and types (null type))
  1315. (let ((regexp (car (car types)))
  1316. (image-type (cdr (car types))))
  1317. (when (looking-at regexp)
  1318. (setq type image-type))
  1319. (setq types (cdr types))))
  1320. type))
  1321. (defun mm-get-image (handle)
  1322. "Return an image instance based on HANDLE."
  1323. (let ((type (mm-handle-media-subtype handle))
  1324. spec)
  1325. ;; Allow some common translations.
  1326. (setq type
  1327. (cond
  1328. ((equal type "x-pixmap")
  1329. "xpm")
  1330. ((equal type "x-xbitmap")
  1331. "xbm")
  1332. ((equal type "x-portable-bitmap")
  1333. "pbm")
  1334. (t type)))
  1335. (or (mm-handle-cache handle)
  1336. (mm-with-unibyte-buffer
  1337. (mm-insert-part handle)
  1338. (prog1
  1339. (setq spec
  1340. (ignore-errors
  1341. ;; Avoid testing `make-glyph' since W3 may define
  1342. ;; a bogus version of it.
  1343. (if (fboundp 'create-image)
  1344. (create-image (buffer-string)
  1345. (or (mm-image-type-from-buffer)
  1346. (intern type))
  1347. 'data-p)
  1348. (mm-create-image-xemacs type))))
  1349. (mm-handle-set-cache handle spec))))))
  1350. (defun mm-create-image-xemacs (type)
  1351. (when (featurep 'xemacs)
  1352. (cond
  1353. ((equal type "xbm")
  1354. ;; xbm images require special handling, since
  1355. ;; the only way to create glyphs from these
  1356. ;; (without a ton of work) is to write them
  1357. ;; out to a file, and then create a file
  1358. ;; specifier.
  1359. (let ((file (mm-make-temp-file
  1360. (expand-file-name "emm" mm-tmp-directory)
  1361. nil ".xbm")))
  1362. (unwind-protect
  1363. (progn
  1364. (write-region (point-min) (point-max) file)
  1365. (make-glyph (list (cons 'x file))))
  1366. (ignore-errors
  1367. (delete-file file)))))
  1368. (t
  1369. (make-glyph
  1370. (vector
  1371. (or (mm-image-type-from-buffer)
  1372. (intern type))
  1373. :data (buffer-string)))))))
  1374. (declare-function image-size "image.c" (spec &optional pixels frame))
  1375. (defun mm-image-fit-p (handle)
  1376. "Say whether the image in HANDLE will fit the current window."
  1377. (let ((image (mm-get-image handle)))
  1378. (or (not image)
  1379. (if (featurep 'xemacs)
  1380. ;; XEmacs's glyphs can actually tell us about their width, so
  1381. ;; let's be nice and smart about them.
  1382. (or mm-inline-large-images
  1383. (and (<= (glyph-width image) (window-pixel-width))
  1384. (<= (glyph-height image) (window-pixel-height))))
  1385. (let* ((size (image-size image))
  1386. (w (car size))
  1387. (h (cdr size)))
  1388. (or mm-inline-large-images
  1389. (and (<= h (1- (window-height))) ; Don't include mode line.
  1390. (<= w (window-width)))))))))
  1391. (defun mm-valid-image-format-p (format)
  1392. "Say whether FORMAT can be displayed natively by Emacs."
  1393. (cond
  1394. ;; Handle XEmacs
  1395. ((fboundp 'valid-image-instantiator-format-p)
  1396. (valid-image-instantiator-format-p format))
  1397. ;; Handle Emacs
  1398. ((fboundp 'image-type-available-p)
  1399. (and (display-graphic-p)
  1400. (image-type-available-p format)))
  1401. ;; Nobody else can do images yet.
  1402. (t
  1403. nil)))
  1404. (defun mm-valid-and-fit-image-p (format handle)
  1405. "Say whether FORMAT can be displayed natively and HANDLE fits the window."
  1406. (and (mm-valid-image-format-p format)
  1407. (mm-image-fit-p handle)))
  1408. (defun mm-find-part-by-type (handles type &optional notp recursive)
  1409. "Search in HANDLES for part with TYPE.
  1410. If NOTP, returns first non-matching part.
  1411. If RECURSIVE, search recursively."
  1412. (let (handle)
  1413. (while handles
  1414. (if (and recursive (stringp (caar handles)))
  1415. (if (setq handle (mm-find-part-by-type (cdar handles) type
  1416. notp recursive))
  1417. (setq handles nil))
  1418. (if (if notp
  1419. (not (equal (mm-handle-media-type (car handles)) type))
  1420. (equal (mm-handle-media-type (car handles)) type))
  1421. (setq handle (car handles)
  1422. handles nil)))
  1423. (setq handles (cdr handles)))
  1424. handle))
  1425. (defun mm-find-raw-part-by-type (ctl type &optional notp)
  1426. (goto-char (point-min))
  1427. (let* ((boundary (concat "--" (mm-handle-multipart-ctl-parameter ctl
  1428. 'boundary)))
  1429. (close-delimiter (concat "^" (regexp-quote boundary) "--[ \t]*$"))
  1430. start
  1431. (end (save-excursion
  1432. (goto-char (point-max))
  1433. (if (re-search-backward close-delimiter nil t)
  1434. (match-beginning 0)
  1435. (point-max))))
  1436. result)
  1437. (setq boundary (concat "^" (regexp-quote boundary) "[ \t]*$"))
  1438. (while (and (not result)
  1439. (re-search-forward boundary end t))
  1440. (goto-char (match-beginning 0))
  1441. (when start
  1442. (save-excursion
  1443. (save-restriction
  1444. (narrow-to-region start (1- (point)))
  1445. (when (let* ((ct (mail-fetch-field "content-type"))
  1446. (ctl (and ct (mail-header-parse-content-type ct))))
  1447. (if notp
  1448. (not (equal (car ctl) type))
  1449. (equal (car ctl) type)))
  1450. (setq result (buffer-string))))))
  1451. (forward-line 1)
  1452. (setq start (point)))
  1453. (when (and (not result) start)
  1454. (save-excursion
  1455. (save-restriction
  1456. (narrow-to-region start end)
  1457. (when (let* ((ct (mail-fetch-field "content-type"))
  1458. (ctl (and ct (mail-header-parse-content-type ct))))
  1459. (if notp
  1460. (not (equal (car ctl) type))
  1461. (equal (car ctl) type)))
  1462. (setq result (buffer-string))))))
  1463. result))
  1464. (defvar mm-security-handle nil)
  1465. (defsubst mm-set-handle-multipart-parameter (handle parameter value)
  1466. ;; HANDLE could be a CTL.
  1467. (when handle
  1468. (put-text-property 0 (length (car handle)) parameter value
  1469. (car handle))))
  1470. (autoload 'mm-view-pkcs7 "mm-view")
  1471. (defun mm-possibly-verify-or-decrypt (parts ctl &optional from)
  1472. (let ((type (car ctl))
  1473. (subtype (cadr (split-string (car ctl) "/")))
  1474. (mm-security-handle ctl) ;; (car CTL) is the type.
  1475. protocol func functest)
  1476. (cond
  1477. ((or (equal type "application/x-pkcs7-mime")
  1478. (equal type "application/pkcs7-mime"))
  1479. (with-temp-buffer
  1480. (when (and (cond
  1481. ((eq mm-decrypt-option 'never) nil)
  1482. ((eq mm-decrypt-option 'always) t)
  1483. ((eq mm-decrypt-option 'known) t)
  1484. (t (y-or-n-p
  1485. (format "Decrypt (S/MIME) part? "))))
  1486. (mm-view-pkcs7 parts from))
  1487. (setq parts (mm-dissect-buffer t)))))
  1488. ((equal subtype "signed")
  1489. (unless (and (setq protocol
  1490. (mm-handle-multipart-ctl-parameter ctl 'protocol))
  1491. (not (equal protocol "multipart/mixed")))
  1492. ;; The message is broken or draft-ietf-openpgp-multsig-01.
  1493. (let ((protocols mm-verify-function-alist))
  1494. (while protocols
  1495. (if (and (or (not (setq functest (nth 3 (car protocols))))
  1496. (funcall functest parts ctl))
  1497. (mm-find-part-by-type parts (caar protocols) nil t))
  1498. (setq protocol (caar protocols)
  1499. protocols nil)
  1500. (setq protocols (cdr protocols))))))
  1501. (setq func (nth 1 (assoc protocol mm-verify-function-alist)))
  1502. (when (cond
  1503. ((eq mm-verify-option 'never) nil)
  1504. ((eq mm-verify-option 'always) t)
  1505. ((eq mm-verify-option 'known)
  1506. (and func
  1507. (or (not (setq functest
  1508. (nth 3 (assoc protocol
  1509. mm-verify-function-alist))))
  1510. (funcall functest parts ctl))))
  1511. (t
  1512. (y-or-n-p
  1513. (format "Verify signed (%s) part? "
  1514. (or (nth 2 (assoc protocol mm-verify-function-alist))
  1515. (format "protocol=%s" protocol))))))
  1516. (save-excursion
  1517. (if func
  1518. (setq parts (funcall func parts ctl))
  1519. (mm-set-handle-multipart-parameter
  1520. mm-security-handle 'gnus-details
  1521. (format "Unknown sign protocol (%s)" protocol))))))
  1522. ((equal subtype "encrypted")
  1523. (unless (setq protocol
  1524. (mm-handle-multipart-ctl-parameter ctl 'protocol))
  1525. ;; The message is broken.
  1526. (let ((parts parts))
  1527. (while parts
  1528. (if (assoc (mm-handle-media-type (car parts))
  1529. mm-decrypt-function-alist)
  1530. (setq protocol (mm-handle-media-type (car parts))
  1531. parts nil)
  1532. (setq parts (cdr parts))))))
  1533. (setq func (nth 1 (assoc protocol mm-decrypt-function-alist)))
  1534. (when (cond
  1535. ((eq mm-decrypt-option 'never) nil)
  1536. ((eq mm-decrypt-option 'always) t)
  1537. ((eq mm-decrypt-option 'known)
  1538. (and func
  1539. (or (not (setq functest
  1540. (nth 3 (assoc protocol
  1541. mm-decrypt-function-alist))))
  1542. (funcall functest parts ctl))))
  1543. (t
  1544. (y-or-n-p
  1545. (format "Decrypt (%s) part? "
  1546. (or (nth 2 (assoc protocol mm-decrypt-function-alist))
  1547. (format "protocol=%s" protocol))))))
  1548. (save-excursion
  1549. (if func
  1550. (setq parts (funcall func parts ctl))
  1551. (mm-set-handle-multipart-parameter
  1552. mm-security-handle 'gnus-details
  1553. (format "Unknown encrypt protocol (%s)" protocol))))))
  1554. (t nil))
  1555. parts))
  1556. (defun mm-multiple-handles (handles)
  1557. (and (listp handles)
  1558. (> (length handles) 1)
  1559. (or (listp (car handles))
  1560. (stringp (car handles)))))
  1561. (defun mm-complicated-handles (handles)
  1562. (and (listp (car handles))
  1563. (> (length handles) 1)))
  1564. (defun mm-merge-handles (handles1 handles2)
  1565. (append
  1566. (if (listp (car handles1))
  1567. handles1
  1568. (list handles1))
  1569. (if (listp (car handles2))
  1570. handles2
  1571. (list handles2))))
  1572. (defun mm-readable-p (handle)
  1573. "Say whether the content of HANDLE is readable."
  1574. (and (< (with-current-buffer (mm-handle-buffer handle)
  1575. (buffer-size)) 10000)
  1576. (mm-with-unibyte-buffer
  1577. (mm-insert-part handle)
  1578. (and (eq (mm-body-7-or-8) '7bit)
  1579. (not (mm-long-lines-p 76))))))
  1580. (declare-function libxml-parse-html-region "xml.c"
  1581. (start end &optional base-url))
  1582. (declare-function shr-insert-document "shr" (dom))
  1583. (defvar shr-blocked-images)
  1584. (defvar gnus-inhibit-images)
  1585. (autoload 'gnus-blocked-images "gnus-art")
  1586. (defun mm-shr (handle)
  1587. ;; Require since we bind its variables.
  1588. (require 'shr)
  1589. (let ((article-buffer (current-buffer))
  1590. (shr-content-function (lambda (id)
  1591. (let ((handle (mm-get-content-id id)))
  1592. (when handle
  1593. (mm-with-part handle
  1594. (buffer-string))))))
  1595. shr-inhibit-images shr-blocked-images charset char)
  1596. (if (and (boundp 'gnus-summary-buffer)
  1597. (bufferp gnus-summary-buffer)
  1598. (buffer-name gnus-summary-buffer))
  1599. (with-current-buffer gnus-summary-buffer
  1600. (setq shr-inhibit-images gnus-inhibit-images
  1601. shr-blocked-images (gnus-blocked-images)))
  1602. (setq shr-inhibit-images gnus-inhibit-images
  1603. shr-blocked-images (gnus-blocked-images)))
  1604. (unless handle
  1605. (setq handle (mm-dissect-buffer t)))
  1606. (setq charset (mail-content-type-get (mm-handle-type handle) 'charset))
  1607. (save-restriction
  1608. (narrow-to-region (point) (point))
  1609. (shr-insert-document
  1610. (mm-with-part handle
  1611. (insert (prog1
  1612. (if (and charset
  1613. (setq charset
  1614. (mm-charset-to-coding-system charset))
  1615. (not (eq charset 'ascii)))
  1616. (mm-decode-coding-string (buffer-string) charset)
  1617. (mm-string-as-multibyte (buffer-string)))
  1618. (erase-buffer)
  1619. (mm-enable-multibyte)))
  1620. (goto-char (point-min))
  1621. (setq case-fold-search t)
  1622. (while (re-search-forward
  1623. "&#\\(?:x\\([89][0-9a-f]\\)\\|\\(1[2-5][0-9]\\)\\);" nil t)
  1624. (when (setq char
  1625. (cdr (assq (if (match-beginning 1)
  1626. (string-to-number (match-string 1) 16)
  1627. (string-to-number (match-string 2)))
  1628. mm-extra-numeric-entities)))
  1629. (replace-match (char-to-string char))))
  1630. ;; Remove "soft hyphens".
  1631. (goto-char (point-min))
  1632. (while (search-forward "­" nil t)
  1633. (replace-match "" t t))
  1634. (libxml-parse-html-region (point-min) (point-max))))
  1635. (mm-handle-set-undisplayer
  1636. handle
  1637. `(lambda ()
  1638. (let ((inhibit-read-only t))
  1639. (delete-region ,(point-min-marker)
  1640. ,(point-max-marker))))))))
  1641. (defun mm-handle-filename (handle)
  1642. "Return filename of HANDLE if any."
  1643. (or (mail-content-type-get (mm-handle-type handle)
  1644. 'name)
  1645. (mail-content-type-get (mm-handle-disposition handle)
  1646. 'filename)))
  1647. (provide 'mm-decode)
  1648. ;;; mm-decode.el ends here