mh-mime.el 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. ;;; mh-mime.el --- MH-E MIME support
  2. ;; Copyright (C) 1993, 1995, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Bill Wohler <wohler@newt.com>
  4. ;; Maintainer: Bill Wohler <wohler@newt.com>
  5. ;; Keywords: mail
  6. ;; See: mh-e.el
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Message composition of MIME message is done with either MH-style
  20. ;; directives for mhn or mhbuild (MH 6.8 or later) or MML (MIME Meta
  21. ;; Language) tags.
  22. ;; TODO:
  23. ;; Paragraph code should not fill # lines if MIME enabled.
  24. ;; Implement mh-auto-mh-to-mime (if non-nil, \\[mh-send-letter]
  25. ;; invokes mh-mh-to-mime automatically before sending.)
  26. ;; Actually, instead of mh-auto-mh-to-mime,
  27. ;; should read automhnproc from profile.
  28. ;; MIME option to mh-forward command to move to content-description
  29. ;; insertion point.
  30. ;;; Change Log:
  31. ;;; Code:
  32. (require 'mh-e)
  33. (require 'mh-gnus) ;needed because mh-gnus.el not compiled
  34. (require 'font-lock)
  35. (require 'gnus-util)
  36. (require 'mailcap)
  37. (require 'mm-decode)
  38. (require 'mm-view)
  39. (require 'mml)
  40. (autoload 'article-emphasize "gnus-art")
  41. (autoload 'gnus-eval-format "gnus-spec")
  42. (autoload 'mail-content-type-get "mail-parse")
  43. (autoload 'mail-decode-encoded-word-string "mail-parse")
  44. (autoload 'mail-header-parse-content-type "mail-parse")
  45. (autoload 'mail-header-strip "mail-parse")
  46. (autoload 'mail-strip-quoted-names "mail-utils")
  47. (autoload 'message-options-get "message")
  48. (autoload 'message-options-set "message")
  49. (autoload 'message-options-set-recipient "message")
  50. (autoload 'mm-decode-body "mm-bodies")
  51. (autoload 'mm-uu-dissect "mm-uu")
  52. (autoload 'mml-unsecure-message "mml-sec")
  53. (autoload 'rfc2047-decode-region "rfc2047")
  54. (autoload 'widget-convert-button "wid-edit")
  55. ;;; Variables
  56. ;; This has to be a macro, since we do: (setf (mh-buffer-data) ...)
  57. ;;;###mh-autoload
  58. (defmacro mh-buffer-data ()
  59. "Convenience macro to get the MIME data structures of the current buffer."
  60. `(gethash (current-buffer) mh-globals-hash))
  61. ;; Structure to keep track of MIME handles on a per buffer basis.
  62. (mh-defstruct (mh-buffer-data (:conc-name mh-mime-)
  63. (:constructor mh-make-buffer-data))
  64. (handles ()) ; List of MIME handles
  65. (handles-cache (make-hash-table)) ; Cache to avoid multiple decodes of
  66. ; nested messages
  67. (parts-count 0) ; The button number is generated from
  68. ; this number
  69. (part-index-hash (make-hash-table))) ; Avoid incrementing the part number
  70. ; for nested messages
  71. (defvar mh-mm-inline-media-tests
  72. `(("image/jpeg"
  73. mm-inline-image
  74. (lambda (handle)
  75. (mm-valid-and-fit-image-p 'jpeg handle)))
  76. ("image/png"
  77. mm-inline-image
  78. (lambda (handle)
  79. (mm-valid-and-fit-image-p 'png handle)))
  80. ("image/gif"
  81. mm-inline-image
  82. (lambda (handle)
  83. (mm-valid-and-fit-image-p 'gif handle)))
  84. ("image/tiff"
  85. mm-inline-image
  86. (lambda (handle)
  87. (mm-valid-and-fit-image-p 'tiff handle)) )
  88. ("image/xbm"
  89. mm-inline-image
  90. (lambda (handle)
  91. (mm-valid-and-fit-image-p 'xbm handle)))
  92. ("image/x-xbitmap"
  93. mm-inline-image
  94. (lambda (handle)
  95. (mm-valid-and-fit-image-p 'xbm handle)))
  96. ("image/xpm"
  97. mm-inline-image
  98. (lambda (handle)
  99. (mm-valid-and-fit-image-p 'xpm handle)))
  100. ("image/x-pixmap"
  101. mm-inline-image
  102. (lambda (handle)
  103. (mm-valid-and-fit-image-p 'xpm handle)))
  104. ("image/bmp"
  105. mm-inline-image
  106. (lambda (handle)
  107. (mm-valid-and-fit-image-p 'bmp handle)))
  108. ("image/x-portable-bitmap"
  109. mm-inline-image
  110. (lambda (handle)
  111. (mm-valid-and-fit-image-p 'pbm handle)))
  112. ("text/plain" mm-inline-text identity)
  113. ("text/enriched" mm-inline-text identity)
  114. ("text/richtext" mm-inline-text identity)
  115. ("text/x-patch" mm-display-patch-inline
  116. (lambda (handle)
  117. (locate-library "diff-mode")))
  118. ("application/emacs-lisp" mm-display-elisp-inline identity)
  119. ("application/x-emacs-lisp" mm-display-elisp-inline identity)
  120. ("text/html"
  121. ,(if (fboundp 'mm-inline-text-html) 'mm-inline-text-html 'mm-inline-text)
  122. (lambda (handle)
  123. (or (and (boundp 'mm-inline-text-html-renderer)
  124. mm-inline-text-html-renderer)
  125. (and (boundp 'mm-text-html-renderer) mm-text-html-renderer))))
  126. ("text/x-vcard"
  127. mh-mm-inline-text-vcard
  128. (lambda (handle)
  129. (or (featurep 'vcard)
  130. (locate-library "vcard"))))
  131. ("message/delivery-status" mm-inline-text identity)
  132. ("message/rfc822" mh-mm-inline-message identity)
  133. ;;("message/partial" mm-inline-partial identity)
  134. ;;("message/external-body" mm-inline-external-body identity)
  135. ("text/.*" mm-inline-text identity)
  136. ("audio/wav" mm-inline-audio
  137. (lambda (handle)
  138. (and (or (featurep 'nas-sound) (featurep 'native-sound))
  139. (device-sound-enabled-p))))
  140. ("audio/au"
  141. mm-inline-audio
  142. (lambda (handle)
  143. (and (or (featurep 'nas-sound) (featurep 'native-sound))
  144. (device-sound-enabled-p))))
  145. ("application/pgp-signature" ignore identity)
  146. ("application/x-pkcs7-signature" ignore identity)
  147. ("application/pkcs7-signature" ignore identity)
  148. ("application/x-pkcs7-mime" ignore identity)
  149. ("application/pkcs7-mime" ignore identity)
  150. ("multipart/alternative" ignore identity)
  151. ("multipart/mixed" ignore identity)
  152. ("multipart/related" ignore identity)
  153. ;; Disable audio and image
  154. ("audio/.*" ignore ignore)
  155. ("image/.*" ignore ignore)
  156. ;; Default to displaying as text
  157. (".*" mm-inline-text mh-mm-readable-p))
  158. "Alist of media types/tests saying whether types can be displayed inline.")
  159. (defvar mh-mime-save-parts-directory nil
  160. "Default to use for `mh-mime-save-parts-default-directory'.
  161. Set from last use.")
  162. ;; Copied from gnus-art.el (should be checked for other cool things that can
  163. ;; be added to the buttons)
  164. (defvar mh-mime-button-commands
  165. '((mh-press-button "\r" "Toggle Display")))
  166. (defvar mh-mime-button-map
  167. (let ((map (make-sparse-keymap)))
  168. (unless (>= (string-to-number emacs-version) 21)
  169. ;; XEmacs doesn't care.
  170. (set-keymap-parent map mh-show-mode-map))
  171. (mh-do-in-gnu-emacs
  172. (define-key map [mouse-2] 'mh-push-button))
  173. (mh-do-in-xemacs
  174. (define-key map '(button2) 'mh-push-button))
  175. (dolist (c mh-mime-button-commands)
  176. (define-key map (cadr c) (car c)))
  177. map))
  178. (defvar mh-mime-button-line-format-alist
  179. '((?T long-type ?s)
  180. (?d description ?s)
  181. (?p index ?s)
  182. (?e dots ?s)))
  183. (defvar mh-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n")
  184. (defvar mh-mime-security-button-pressed nil)
  185. (defvar mh-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n")
  186. (defvar mh-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n")
  187. (defvar mh-mime-security-button-line-format-alist
  188. '((?t type ?s)
  189. (?i info ?s)
  190. (?d details ?s)
  191. (?D pressed-details ?s)))
  192. (defvar mh-mime-security-button-map
  193. (let ((map (make-sparse-keymap)))
  194. (unless (>= (string-to-number emacs-version) 21)
  195. (set-keymap-parent map mh-show-mode-map))
  196. (define-key map "\r" 'mh-press-button)
  197. (mh-do-in-gnu-emacs
  198. (define-key map [mouse-2] 'mh-push-button))
  199. (mh-do-in-xemacs
  200. (define-key map '(button2) 'mh-push-button))
  201. map))
  202. ;;; MH-Folder Commands
  203. ;; Alphabetical.
  204. ;;;###mh-autoload
  205. (defun mh-display-with-external-viewer (part-index)
  206. "View attachment externally.
  207. If Emacs does not know how to view an attachment, you could save
  208. it into a file and then run some program to open it. It is
  209. easier, however, to launch the program directly from MH-E with
  210. this command. While you'll most likely use this to view
  211. spreadsheets and documents, it is also useful to use your browser
  212. to view HTML attachments with higher fidelity than what Emacs can
  213. provide.
  214. This command displays the attachment associated with the button
  215. under the cursor. If the cursor is not located over a button,
  216. then the cursor first moves to the next button, wrapping to the
  217. beginning of the message if necessary. You can provide a numeric
  218. prefix argument PART-INDEX to view the attachment labeled with
  219. that number.
  220. This command tries to provide a reasonable default for the viewer
  221. by calling the Emacs function `mailcap-mime-info'. This function
  222. usually reads the file \"/etc/mailcap\"."
  223. (interactive "P")
  224. (when (consp part-index) (setq part-index (car part-index)))
  225. (mh-folder-mime-action
  226. part-index
  227. #'(lambda ()
  228. (let* ((part (get-text-property (point) 'mh-data))
  229. (type (mm-handle-media-type part))
  230. (methods (mapcar (lambda (x) (list (cdr (assoc 'viewer x))))
  231. (mailcap-mime-info type 'all)))
  232. (def (caar methods))
  233. (prompt (format "Viewer%s: " (if def
  234. (format " (default %s)" def)
  235. "")))
  236. (method (completing-read prompt methods nil nil nil nil def))
  237. (folder mh-show-folder-buffer)
  238. (buffer-read-only nil))
  239. (when (string-match "^[^% \t]+$" method)
  240. (setq method (concat method " %s")))
  241. (flet ((mm-handle-set-external-undisplayer (handle function)
  242. (mh-handle-set-external-undisplayer folder handle function)))
  243. (unwind-protect (mm-display-external part method)
  244. (set-buffer-modified-p nil)))))
  245. nil))
  246. ;;;###mh-autoload
  247. (defun mh-folder-inline-mime-part (part-index)
  248. "Show attachment verbatim.
  249. You can view the raw contents of an attachment with this command.
  250. This command displays (or hides) the contents of the attachment
  251. associated with the button under the cursor verbatim. If the
  252. cursor is not located over a button, then the cursor first moves
  253. to the next button, wrapping to the beginning of the message if
  254. necessary.
  255. You can also provide a numeric prefix argument PART-INDEX to view
  256. the attachment labeled with that number."
  257. (interactive "P")
  258. (when (consp part-index) (setq part-index (car part-index)))
  259. (mh-folder-mime-action part-index #'mh-mime-inline-part nil))
  260. (defun mh-mime-inline-part ()
  261. "Toggle display of the raw MIME part."
  262. (interactive)
  263. (let* ((buffer-read-only nil)
  264. (data (get-text-property (point) 'mh-data))
  265. (inserted-flag (get-text-property (point) 'mh-mime-inserted))
  266. (displayed-flag (mm-handle-displayed-p data))
  267. (point (point))
  268. start end)
  269. (cond ((and data (not inserted-flag) (not displayed-flag))
  270. (let ((contents (mm-get-part data)))
  271. (add-text-properties (mh-line-beginning-position)
  272. (mh-line-end-position) '(mh-mime-inserted t))
  273. (setq start (point-marker))
  274. (forward-line 1)
  275. (mm-insert-inline data contents)
  276. (setq end (point-marker))
  277. (add-text-properties
  278. start (progn (goto-char start) (mh-line-end-position))
  279. `(mh-region (,start . ,end)))))
  280. ((and data (or inserted-flag displayed-flag))
  281. (mh-press-button)
  282. (message "MIME part already inserted")))
  283. (goto-char point)
  284. (set-buffer-modified-p nil)))
  285. ;;;###mh-autoload
  286. (defun mh-folder-save-mime-part (part-index)
  287. "Save (output) attachment.
  288. This command saves the attachment associated with the button under the
  289. cursor. If the cursor is not located over a button, then the cursor
  290. first moves to the next button, wrapping to the beginning of the
  291. message if necessary.
  292. You can also provide a numeric prefix argument PART-INDEX to save the
  293. attachment labeled with that number.
  294. This command prompts you for a filename and suggests a specific name
  295. if it is available."
  296. (interactive "P")
  297. (when (consp part-index) (setq part-index (car part-index)))
  298. (mh-folder-mime-action part-index #'mh-mime-save-part nil))
  299. (defun mh-mime-save-part ()
  300. "Save MIME part at point."
  301. (interactive)
  302. (let ((data (get-text-property (point) 'mh-data)))
  303. (when data
  304. (let ((mm-default-directory
  305. (file-name-as-directory (or mh-mime-save-parts-directory
  306. default-directory))))
  307. (mh-mm-save-part data)
  308. (setq mh-mime-save-parts-directory mm-default-directory)))))
  309. ;;;###mh-autoload
  310. (defun mh-folder-toggle-mime-part (part-index)
  311. "View attachment.
  312. This command displays (or hides) the attachment associated with
  313. the button under the cursor. If the cursor is not located over a
  314. button, then the cursor first moves to the next button, wrapping
  315. to the beginning of the message if necessary. This command has
  316. the advantage over related commands of working from the MH-Folder
  317. buffer.
  318. You can also provide a numeric prefix argument PART-INDEX to view
  319. the attachment labeled with that number. If Emacs does not know
  320. how to display the attachment, then Emacs offers to save the
  321. attachment in a file."
  322. (interactive "P")
  323. (when (consp part-index) (setq part-index (car part-index)))
  324. (mh-folder-mime-action part-index #'mh-press-button t))
  325. ;;;###mh-autoload
  326. (defun mh-mime-save-parts (prompt)
  327. "Save attachments.
  328. You can save all of the attachments at once with this command.
  329. The attachments are saved in the directory specified by the
  330. option `mh-mime-save-parts-default-directory' unless you use a
  331. prefix argument PROMPT in which case you are prompted for the
  332. directory. These directories may be superseded by MH profile
  333. components, since this function calls on \"mhstore\" (\"mhn\") to
  334. do the work."
  335. (interactive "P")
  336. (let ((msg (if (eq major-mode 'mh-show-mode)
  337. (mh-show-buffer-message-number)
  338. (mh-get-msg-num t)))
  339. (folder (if (eq major-mode 'mh-show-mode)
  340. mh-show-folder-buffer
  341. mh-current-folder))
  342. (command (if (mh-variant-p 'nmh) "mhstore" "mhn"))
  343. (directory
  344. (cond
  345. ((and (or prompt
  346. (equal nil mh-mime-save-parts-default-directory)
  347. (equal t mh-mime-save-parts-default-directory))
  348. (not mh-mime-save-parts-directory))
  349. (read-directory-name "Store in directory: " nil nil t))
  350. ((and (or prompt
  351. (equal t mh-mime-save-parts-default-directory))
  352. mh-mime-save-parts-directory)
  353. (read-directory-name (format
  354. "Store in directory (default %s): "
  355. mh-mime-save-parts-directory)
  356. "" mh-mime-save-parts-directory t ""))
  357. ((stringp mh-mime-save-parts-default-directory)
  358. mh-mime-save-parts-default-directory)
  359. (t
  360. mh-mime-save-parts-directory))))
  361. (if (and (equal directory "") mh-mime-save-parts-directory)
  362. (setq directory mh-mime-save-parts-directory))
  363. (if (not (file-directory-p directory))
  364. (message "No directory specified")
  365. (if (equal nil mh-mime-save-parts-default-directory)
  366. (setq mh-mime-save-parts-directory directory))
  367. (with-current-buffer (get-buffer-create mh-log-buffer)
  368. (cd directory)
  369. (setq mh-mime-save-parts-directory directory)
  370. (let ((initial-size (mh-truncate-log-buffer)))
  371. (apply 'call-process
  372. (expand-file-name command mh-progs) nil t nil
  373. (mh-list-to-string (list folder msg "-auto"
  374. (if (not (mh-variant-p 'nmh))
  375. "-store"))))
  376. (if (> (buffer-size) initial-size)
  377. (save-window-excursion
  378. (switch-to-buffer-other-window mh-log-buffer)
  379. (sit-for 3))))))))
  380. ;;;###mh-autoload
  381. (defun mh-toggle-mh-decode-mime-flag ()
  382. "Toggle the value of `mh-decode-mime-flag'."
  383. (interactive)
  384. (setq mh-decode-mime-flag (not mh-decode-mime-flag))
  385. (mh-show nil t)
  386. (message "%s" (if mh-decode-mime-flag
  387. "Processing attachments normally"
  388. "Displaying raw message")))
  389. ;;;###mh-autoload
  390. (defun mh-toggle-mime-buttons ()
  391. "Toggle option `mh-display-buttons-for-inline-parts-flag'."
  392. (interactive)
  393. (setq mh-display-buttons-for-inline-parts-flag
  394. (not mh-display-buttons-for-inline-parts-flag))
  395. (mh-show nil t))
  396. ;;; MIME Display Routines
  397. (defun mh-mm-inline-message (handle)
  398. "Display message, HANDLE.
  399. The function decodes the message and displays it. It avoids
  400. decoding the same message multiple times."
  401. (let ((b (point))
  402. (clean-message-header mh-clean-message-header-flag)
  403. (invisible-headers mh-invisible-header-fields-compiled)
  404. (visible-headers nil))
  405. (save-excursion
  406. (save-restriction
  407. (narrow-to-region b b)
  408. (mm-insert-part handle)
  409. (mh-mime-display
  410. (or (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
  411. (setf (gethash handle (mh-mime-handles-cache (mh-buffer-data)))
  412. (let ((handles (mm-dissect-buffer nil)))
  413. (if handles
  414. (mh-mm-uu-dissect-text-parts handles)
  415. (setq handles (mm-uu-dissect)))
  416. (setf (mh-mime-handles (mh-buffer-data))
  417. (mh-mm-merge-handles
  418. handles (mh-mime-handles (mh-buffer-data))))
  419. handles))))
  420. (goto-char (point-min))
  421. (mh-show-xface)
  422. (cond (clean-message-header
  423. (mh-clean-msg-header (point-min)
  424. invisible-headers
  425. visible-headers)
  426. (goto-char (point-min)))
  427. (t
  428. (mh-start-of-uncleaned-message)))
  429. (mh-decode-message-header)
  430. (mh-show-addr)
  431. ;; The other highlighting types don't need anything special
  432. (when (eq mh-highlight-citation-style 'gnus)
  433. (mh-gnus-article-highlight-citation))
  434. (goto-char (point-min))
  435. (insert "\n------- Forwarded Message\n\n")
  436. (mh-display-smileys)
  437. (mh-display-emphasis)
  438. (mm-handle-set-undisplayer
  439. handle
  440. `(lambda ()
  441. (let (buffer-read-only)
  442. (if (fboundp 'remove-specifier)
  443. ;; This is only valid on XEmacs.
  444. (mapcar (lambda (prop)
  445. (remove-specifier
  446. (face-property 'default prop) (current-buffer)))
  447. '(background background-pixmap foreground)))
  448. (delete-region ,(point-min-marker) ,(point-max-marker)))))))))
  449. ;;;###mh-autoload
  450. (defun mh-decode-message-header ()
  451. "Decode RFC2047 encoded message header fields."
  452. (when mh-decode-mime-flag
  453. (let ((buffer-read-only nil))
  454. (rfc2047-decode-region (point-min) (mh-mail-header-end)))))
  455. ;;;###mh-autoload
  456. (defun mh-decode-message-subject ()
  457. "Decode RFC2047 encoded message header fields."
  458. (when mh-decode-mime-flag
  459. (save-excursion
  460. (let ((buffer-read-only nil))
  461. (rfc2047-decode-region (progn (mh-goto-header-field "Subject:") (point))
  462. (progn (mh-header-field-end) (point)))))))
  463. ;;;###mh-autoload
  464. (defun mh-mime-display (&optional pre-dissected-handles)
  465. "Display (and possibly decode) MIME handles.
  466. Optional argument, PRE-DISSECTED-HANDLES is a list of MIME
  467. handles. If present they are displayed otherwise the buffer is
  468. parsed and then displayed."
  469. (let ((handles ())
  470. (folder mh-show-folder-buffer)
  471. (raw-message-data (buffer-string)))
  472. (flet ((mm-handle-set-external-undisplayer
  473. (handle function)
  474. (mh-handle-set-external-undisplayer folder handle function)))
  475. (goto-char (point-min))
  476. (unless (search-forward "\n\n" nil t)
  477. (goto-char (point-max))
  478. (insert "\n\n"))
  479. (condition-case err
  480. (progn
  481. ;; If needed dissect the current buffer
  482. (if pre-dissected-handles
  483. (setq handles pre-dissected-handles)
  484. (if (setq handles (mm-dissect-buffer nil))
  485. (mh-mm-uu-dissect-text-parts handles)
  486. (setq handles (mm-uu-dissect)))
  487. (setf (mh-mime-handles (mh-buffer-data))
  488. (mh-mm-merge-handles handles
  489. (mh-mime-handles (mh-buffer-data))))
  490. (unless handles
  491. (mh-decode-message-body)))
  492. (cond ((and handles
  493. (or (not (stringp (car handles)))
  494. (cdr handles)))
  495. ;; Go to start of message body
  496. (goto-char (point-min))
  497. (or (search-forward "\n\n" nil t)
  498. (goto-char (point-max)))
  499. ;; Delete the body
  500. (delete-region (point) (point-max))
  501. ;; Display the MIME handles
  502. (mh-mime-display-part handles))
  503. (t
  504. (mh-signature-highlight))))
  505. (error
  506. (message "Could not display body: %s" (error-message-string err))
  507. (delete-region (point-min) (point-max))
  508. (insert raw-message-data))))))
  509. (defun mh-decode-message-body ()
  510. "Decode message based on charset.
  511. If message has been encoded for transfer take that into account."
  512. (let (ct charset cte)
  513. (goto-char (point-min))
  514. (re-search-forward "\n\n" nil t)
  515. (save-restriction
  516. (narrow-to-region (point-min) (point))
  517. (setq ct (ignore-errors (mail-header-parse-content-type
  518. (message-fetch-field "Content-Type" t)))
  519. charset (mail-content-type-get ct 'charset)
  520. cte (message-fetch-field "Content-Transfer-Encoding")))
  521. (when (stringp cte) (setq cte (mail-header-strip cte)))
  522. (when (or (not ct) (equal (car ct) "text/plain"))
  523. (save-restriction
  524. (narrow-to-region (min (1+ (mh-mail-header-end)) (point-max))
  525. (point-max))
  526. (mm-decode-body charset
  527. (and cte (intern (downcase
  528. (gnus-strip-whitespace cte))))
  529. (car ct))))))
  530. (defun mh-mime-display-part (handle)
  531. "Decides the viewer to call based on the type of HANDLE."
  532. (cond ((null handle)
  533. nil)
  534. ((not (stringp (car handle)))
  535. (mh-mime-display-single handle))
  536. ((equal (car handle) "multipart/alternative")
  537. (mh-mime-display-alternative (cdr handle)))
  538. ((and mh-pgp-support-flag
  539. (or (equal (car handle) "multipart/signed")
  540. (equal (car handle) "multipart/encrypted")))
  541. (mh-mime-display-security handle))
  542. (t
  543. (mh-mime-display-mixed (cdr handle)))))
  544. (defun mh-mime-display-mixed (handles)
  545. "Display the list of MIME parts, HANDLES recursively."
  546. (mapcar #'mh-mime-display-part handles))
  547. (defun mh-mime-display-alternative (handles)
  548. "Choose among the alternatives, HANDLES the part that will be displayed.
  549. If no part is preferred then all the parts are displayed."
  550. (let* ((preferred (mm-preferred-alternative handles))
  551. (others (loop for x in handles unless (eq x preferred) collect x)))
  552. (cond ((and preferred
  553. (stringp (car preferred)))
  554. (mh-mime-display-part preferred)
  555. (mh-mime-maybe-display-alternatives others))
  556. (preferred
  557. (save-restriction
  558. (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
  559. (mh-mime-display-single preferred)
  560. (mh-mime-maybe-display-alternatives others)
  561. (goto-char (point-max))))
  562. (t
  563. (mh-mime-display-mixed handles)))))
  564. (defun mh-mime-maybe-display-alternatives (alternatives)
  565. "Show buttons for ALTERNATIVES.
  566. If `mh-mime-display-alternatives-flag' is non-nil then display
  567. buttons for alternative parts that are usually suppressed."
  568. (when (and mh-display-buttons-for-alternatives-flag alternatives)
  569. (insert "\n----------------------------------------------------\n")
  570. (insert "Alternatives:\n")
  571. (dolist (x alternatives)
  572. (insert "\n")
  573. (mh-insert-mime-button x (mh-mime-part-index x) nil))
  574. (insert "\n----------------------------------------------------\n")))
  575. (defun mh-mime-display-security (handle)
  576. "Display PGP encrypted/signed message, HANDLE."
  577. (save-restriction
  578. (narrow-to-region (point) (point))
  579. (insert "\n")
  580. (mh-insert-mime-security-button handle)
  581. (mh-mime-display-mixed (cdr handle))
  582. (insert "\n")
  583. (let ((mh-mime-security-button-line-format
  584. mh-mime-security-button-end-line-format))
  585. (mh-insert-mime-security-button handle))
  586. (mh-mm-set-handle-multipart-parameter
  587. handle 'mh-region (cons (point-min-marker) (point-max-marker)))))
  588. (defun mh-mime-display-single (handle)
  589. "Display a leaf node, HANDLE in the MIME tree."
  590. (let* ((type (mm-handle-media-type handle))
  591. (small-image-flag (mh-small-image-p handle))
  592. (attachmentp (equal (car (mm-handle-disposition handle))
  593. "attachment"))
  594. (inlinep (and (equal (car (mm-handle-disposition handle)) "inline")
  595. (mm-inlinable-p handle)
  596. (mm-inlined-p handle)))
  597. (displayp (or inlinep ; show if inline OR
  598. (mh-inline-vcard-p handle); inline vcard OR
  599. (and (not attachmentp) ; if not an attachment
  600. (or small-image-flag ; and small image
  601. ; and user wants inline
  602. (and (not (equal
  603. (mm-handle-media-supertype handle)
  604. "image"))
  605. (mm-inlinable-p handle)
  606. (mm-inlined-p handle)))))))
  607. (save-restriction
  608. (narrow-to-region (point) (if (eobp) (point) (1+ (point))))
  609. (cond ((and mh-pgp-support-flag
  610. (equal type "application/pgp-signature"))
  611. nil) ; skip signatures as they are already handled...
  612. ((not displayp)
  613. (insert "\n")
  614. (mh-insert-mime-button handle (mh-mime-part-index handle) nil))
  615. ((and displayp
  616. (not mh-display-buttons-for-inline-parts-flag))
  617. (or (mm-display-part handle)
  618. (mm-display-part handle))
  619. (mh-signature-highlight handle))
  620. ((and displayp
  621. mh-display-buttons-for-inline-parts-flag)
  622. (insert "\n")
  623. (mh-insert-mime-button handle (mh-mime-part-index handle) nil)
  624. (forward-line -1)
  625. (mh-mm-display-part handle)))
  626. (goto-char (point-max)))))
  627. ;; There is a bug in Gnus inline image display due to which an extra line
  628. ;; gets inserted every time it is viewed. To work around that problem we are
  629. ;; using an extra property 'mh-region to remember the region that is added
  630. ;; when the button is clicked. The region is then deleted to make sure that
  631. ;; no extra lines get inserted.
  632. (defun mh-mm-display-part (handle)
  633. "Toggle display of button for MIME part, HANDLE."
  634. (beginning-of-line)
  635. (let ((id (get-text-property (point) 'mh-part))
  636. (point (point))
  637. (window (selected-window))
  638. (mail-parse-charset 'nil)
  639. (mail-parse-ignored-charsets nil)
  640. region buffer-read-only)
  641. (save-excursion
  642. (unwind-protect
  643. (let ((win (get-buffer-window (current-buffer) t)))
  644. (when win
  645. (select-window win))
  646. (goto-char point)
  647. (if (mm-handle-displayed-p handle)
  648. ;; This will remove the part.
  649. (progn
  650. ;; Delete the button and displayed part (if any)
  651. (let ((region (get-text-property point 'mh-region)))
  652. (when region
  653. (mh-funcall-if-exists
  654. remove-images (car region) (cdr region)))
  655. (mm-display-part handle)
  656. (when region
  657. (delete-region (car region) (cdr region))))
  658. ;; Delete button (if it still remains). This happens for
  659. ;; externally displayed parts where the previous step does
  660. ;; nothing.
  661. (unless (eolp)
  662. (delete-region (point) (progn (forward-line) (point)))))
  663. (save-restriction
  664. (delete-region (point) (progn (forward-line 1) (point)))
  665. (narrow-to-region (point) (point))
  666. ;; Maybe we need another unwind-protect here.
  667. (when (equal (mm-handle-media-supertype handle) "image")
  668. (insert "\n"))
  669. (when (and (not (eq (ignore-errors (mm-display-part handle))
  670. 'inline))
  671. (equal (mm-handle-media-supertype handle)
  672. "image"))
  673. (goto-char (point-min))
  674. (delete-char 1))
  675. (when (equal (mm-handle-media-supertype handle) "text")
  676. (when (eq mh-highlight-citation-style 'gnus)
  677. (mh-gnus-article-highlight-citation))
  678. (mh-display-smileys)
  679. (mh-display-emphasis)
  680. (mh-signature-highlight handle))
  681. (setq region (cons (progn (goto-char (point-min))
  682. (point-marker))
  683. (progn (goto-char (point-max))
  684. (point-marker)))))))
  685. (when (window-live-p window)
  686. (select-window window))
  687. (goto-char point)
  688. (beginning-of-line)
  689. (mh-insert-mime-button handle id (mm-handle-displayed-p handle))
  690. (goto-char point)
  691. (when region
  692. (add-text-properties (mh-line-beginning-position)
  693. (mh-line-end-position)
  694. `(mh-region ,region)))))))
  695. (defun mh-mime-part-index (handle)
  696. "Generate the button number for MIME part, HANDLE.
  697. Notice that a hash table is used to display the same number when
  698. buttons need to be displayed multiple times (for instance when
  699. nested messages are opened)."
  700. (or (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
  701. (setf (gethash handle (mh-mime-part-index-hash (mh-buffer-data)))
  702. (incf (mh-mime-parts-count (mh-buffer-data))))))
  703. (defun mh-small-image-p (handle)
  704. "Decide whether HANDLE is a \"small\" image that can be displayed inline.
  705. This is only useful if a Content-Disposition header is not present."
  706. (let ((media-test (caddr (assoc (car (mm-handle-type handle))
  707. mh-mm-inline-media-tests)))
  708. (mm-inline-large-images t))
  709. (and media-test
  710. (equal (mm-handle-media-supertype handle) "image")
  711. (funcall media-test handle) ; Since mm-inline-large-images is T,
  712. ; this only tells us if the image is
  713. ; something that emacs can display
  714. (let* ((image (mm-get-image handle)))
  715. (or (mh-do-in-xemacs
  716. (and (mh-funcall-if-exists glyphp image)
  717. (< (glyph-width image)
  718. (or mh-max-inline-image-width (window-pixel-width)))
  719. (< (glyph-height image)
  720. (or mh-max-inline-image-height
  721. (window-pixel-height)))))
  722. (mh-do-in-gnu-emacs
  723. (let ((size (mh-funcall-if-exists image-size image)))
  724. (and size
  725. (< (cdr size) (or mh-max-inline-image-height
  726. (1- (window-height))))
  727. (< (car size) (or mh-max-inline-image-width
  728. (window-width)))))))))))
  729. (defun mh-inline-vcard-p (handle)
  730. "Decide if HANDLE is a vcard that must be displayed inline."
  731. (let ((type (mm-handle-type handle)))
  732. (and (or (featurep 'vcard) (fboundp 'vcard-pretty-print))
  733. (consp type)
  734. (equal (car type) "text/x-vcard")
  735. (save-excursion
  736. (save-restriction
  737. (widen)
  738. (goto-char (point-min))
  739. (not (mh-signature-separator-p)))))))
  740. (defun mh-signature-highlight (&optional handle)
  741. "Highlight message signature in HANDLE.
  742. The optional argument, HANDLE is a MIME handle if the function is
  743. being used to highlight the signature in a MIME part."
  744. (let ((regexp
  745. (cond ((not handle) "^-- $")
  746. ((not (and (equal (mm-handle-media-supertype handle) "text")
  747. (equal (mm-handle-media-subtype handle) "html")))
  748. "^-- $")
  749. ((eq (mh-mm-text-html-renderer) 'lynx) "^ --$")
  750. (t "^--$"))))
  751. (save-excursion
  752. (goto-char (point-max))
  753. (when (re-search-backward regexp nil t)
  754. (mh-do-in-gnu-emacs
  755. (let ((ov (make-overlay (point) (point-max))))
  756. (overlay-put ov 'face 'mh-show-signature)
  757. (overlay-put ov 'evaporate t)))
  758. (mh-do-in-xemacs
  759. (set-extent-property (make-extent (point) (point-max))
  760. 'face 'mh-show-signature))))))
  761. ;;; Button Display
  762. ;; Shush compiler.
  763. (mh-do-in-xemacs
  764. (defvar dots)
  765. (defvar type)
  766. (defvar ov))
  767. (defun mh-insert-mime-button (handle index displayed)
  768. "Insert MIME button for HANDLE.
  769. INDEX is the part number that will be DISPLAYED. It is also used
  770. by commands like \"K v\" which operate on individual MIME parts."
  771. ;; The button could be displayed by a previous decode. In that case
  772. ;; undisplay it if we need a hidden button.
  773. (when (and (mm-handle-displayed-p handle) (not displayed))
  774. (mm-display-part handle))
  775. (let ((name (or (mail-content-type-get (mm-handle-type handle) 'name)
  776. (mail-content-type-get (mm-handle-disposition handle)
  777. 'filename)
  778. (mail-content-type-get (mm-handle-type handle) 'url)
  779. ""))
  780. (type (mm-handle-media-type handle))
  781. (description (mail-decode-encoded-word-string
  782. (or (mm-handle-description handle) "")))
  783. (dots (if (or displayed (mm-handle-displayed-p handle)) " " "..."))
  784. long-type begin end)
  785. (if (string-match ".*/" name) (setq name (substring name (match-end 0))))
  786. (setq long-type (concat type (and (not (equal name ""))
  787. (concat "; " name))))
  788. (unless (equal description "")
  789. (setq long-type (concat " --- " long-type)))
  790. (unless (bolp) (insert "\n"))
  791. (setq begin (point))
  792. (gnus-eval-format
  793. mh-mime-button-line-format mh-mime-button-line-format-alist
  794. `(,@(mh-gnus-local-map-property mh-mime-button-map)
  795. mh-callback mh-mm-display-part
  796. mh-part ,index
  797. mh-data ,handle))
  798. (setq end (point))
  799. (widget-convert-button
  800. 'link begin end
  801. :mime-handle handle
  802. :action 'mh-widget-press-button
  803. :button-keymap mh-mime-button-map
  804. :help-echo
  805. "Mouse-2 click or press RET (in show buffer) to toggle display")
  806. (dolist (ov (mh-funcall-if-exists overlays-in begin end))
  807. (mh-funcall-if-exists overlay-put ov 'evaporate t))))
  808. ;; Shush compiler.
  809. (defvar mm-verify-function-alist) ; < Emacs 22
  810. (defvar mm-decrypt-function-alist) ; < Emacs 22
  811. (mh-do-in-xemacs
  812. (defvar pressed-details))
  813. (defun mh-insert-mime-security-button (handle)
  814. "Display buttons for PGP message, HANDLE."
  815. (let* ((protocol (mh-mm-handle-multipart-ctl-parameter handle 'protocol))
  816. (crypto-type (or (nth 2 (assoc protocol mm-verify-function-alist))
  817. (nth 2 (assoc protocol mm-decrypt-function-alist))
  818. "Unknown"))
  819. (type (concat crypto-type
  820. (if (equal (car handle) "multipart/signed")
  821. " Signed" " Encrypted")
  822. " Part"))
  823. (info (or (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
  824. "Undecided"))
  825. (details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details))
  826. pressed-details begin end face)
  827. (setq details (if details (concat "\n" details) ""))
  828. (setq pressed-details (if mh-mime-security-button-pressed details ""))
  829. (setq face (mh-mime-security-button-face info))
  830. (unless (bolp) (insert "\n"))
  831. (setq begin (point))
  832. (gnus-eval-format
  833. mh-mime-security-button-line-format
  834. mh-mime-security-button-line-format-alist
  835. `(,@(mh-gnus-local-map-property mh-mime-security-button-map)
  836. mh-button-pressed ,mh-mime-security-button-pressed
  837. mh-callback mh-mime-security-press-button
  838. mh-line-format ,mh-mime-security-button-line-format
  839. mh-data ,handle))
  840. (setq end (point))
  841. (widget-convert-button 'link begin end
  842. :mime-handle handle
  843. :action 'mh-widget-press-button
  844. :button-keymap mh-mime-security-button-map
  845. :button-face face
  846. :help-echo "Mouse-2 click or press RET (in show buffer) to see security details.")
  847. (dolist (ov (mh-funcall-if-exists overlays-in begin end))
  848. (mh-funcall-if-exists overlay-put ov 'evaporate t))
  849. (when (equal info "Failed")
  850. (let* ((type (if (equal (car handle) "multipart/signed")
  851. "verification" "decryption"))
  852. (warning (if (equal type "decryption")
  853. "(passphrase may be incorrect)" "")))
  854. (message "%s %s failed %s" crypto-type type warning)))))
  855. (defun mh-mime-security-button-face (info)
  856. "Return the button face to use for encrypted/signed mail based on INFO."
  857. (cond ((string-match "OK" info) ;Decrypted mail
  858. 'mh-show-pgg-good)
  859. ((string-match "Failed" info) ;Decryption failed or signature invalid
  860. 'mh-show-pgg-bad)
  861. ((string-match "Undecided" info);Unprocessed mail
  862. 'mh-show-pgg-unknown)
  863. ((string-match "Untrusted" info);Key not trusted
  864. 'mh-show-pgg-unknown)
  865. (t
  866. 'mh-show-pgg-good)))
  867. ;;; Button Handlers
  868. (defun mh-folder-mime-action (part-index action include-security-flag)
  869. "Go to PART-INDEX and carry out ACTION.
  870. If PART-INDEX is nil then go to the next part in the buffer. The
  871. search for the next buffer wraps around if end of buffer is reached.
  872. If argument INCLUDE-SECURITY-FLAG is non-nil then include security
  873. info buttons when searching for a suitable parts."
  874. (unless mh-showing-mode
  875. (mh-show))
  876. (mh-in-show-buffer (mh-show-buffer)
  877. (let ((criterion
  878. (cond (part-index
  879. (lambda (p)
  880. (let ((part (get-text-property p 'mh-part)))
  881. (and (integerp part) (= part part-index)))))
  882. (t (lambda (p)
  883. (if include-security-flag
  884. (get-text-property p 'mh-data)
  885. (integerp (get-text-property p 'mh-part)))))))
  886. (point (point)))
  887. (cond ((and (get-text-property point 'mh-part)
  888. (or (null part-index)
  889. (= (get-text-property point 'mh-part) part-index)))
  890. (funcall action))
  891. ((and (get-text-property point 'mh-data)
  892. include-security-flag
  893. (null part-index))
  894. (funcall action))
  895. (t
  896. (mh-goto-next-button nil criterion)
  897. (if (= (point) point)
  898. (message "No matching MIME part found")
  899. (funcall action)))))))
  900. ;;;###mh-autoload
  901. (defun mh-goto-next-button (backward-flag &optional criterion)
  902. "Search for next button satisfying criterion.
  903. If BACKWARD-FLAG is non-nil search backward in the buffer for a mime
  904. button.
  905. If CRITERION is a function or a symbol which has a function binding
  906. then that function must return non-nil at the button we stop."
  907. (unless (or (and (symbolp criterion) (fboundp criterion))
  908. (functionp criterion))
  909. (setq criterion (lambda (x) t)))
  910. ;; Move to the next button in the buffer satisfying criterion
  911. (goto-char (or (save-excursion
  912. (beginning-of-line)
  913. ;; Find point before current button
  914. (let ((point-before-current-button
  915. (save-excursion
  916. (while (get-text-property (point) 'mh-data)
  917. (unless (= (forward-line
  918. (if backward-flag 1 -1))
  919. 0)
  920. (if backward-flag
  921. (goto-char (point-min))
  922. (goto-char (point-max)))))
  923. (point))))
  924. ;; Skip over current button
  925. (while (and (get-text-property (point) 'mh-data)
  926. (not (if backward-flag (bobp) (eobp))))
  927. (forward-line (if backward-flag -1 1)))
  928. ;; Stop at next MIME button if any exists.
  929. (block loop
  930. (while (/= (progn
  931. (unless (= (forward-line
  932. (if backward-flag -1 1))
  933. 0)
  934. (if backward-flag
  935. (goto-char (point-max))
  936. (goto-char (point-min)))
  937. (beginning-of-line))
  938. (point))
  939. point-before-current-button)
  940. (when (and (get-text-property (point) 'mh-data)
  941. (funcall criterion (point)))
  942. (return-from loop (point))))
  943. nil)))
  944. (point))))
  945. (defun mh-widget-press-button (widget el)
  946. "Callback for widget, WIDGET.
  947. Parameter EL is unused."
  948. (goto-char (widget-get widget :from))
  949. (mh-press-button))
  950. (defun mh-press-button ()
  951. "View contents of button.
  952. This command is a toggle so if you use it again on the same
  953. attachment, the attachment is hidden."
  954. (interactive)
  955. (let ((mm-inline-media-tests mh-mm-inline-media-tests)
  956. (data (get-text-property (point) 'mh-data))
  957. (function (get-text-property (point) 'mh-callback))
  958. (buffer-read-only nil)
  959. (folder mh-show-folder-buffer))
  960. (flet ((mm-handle-set-external-undisplayer
  961. (handle function)
  962. (mh-handle-set-external-undisplayer folder handle function)))
  963. (when (and function (eolp))
  964. (backward-char))
  965. (unwind-protect (and function (funcall function data))
  966. (set-buffer-modified-p nil)))))
  967. (defun mh-push-button (event)
  968. "Click MIME button for EVENT.
  969. If the MIME part is visible then it is removed. Otherwise the
  970. part is displayed. This function is called when the mouse is used
  971. to click the MIME button."
  972. (interactive "e")
  973. (mh-do-at-event-location event
  974. (let ((folder mh-show-folder-buffer)
  975. (mm-inline-media-tests mh-mm-inline-media-tests)
  976. (data (get-text-property (point) 'mh-data))
  977. (function (get-text-property (point) 'mh-callback)))
  978. (flet ((mm-handle-set-external-undisplayer (handle func)
  979. (mh-handle-set-external-undisplayer folder handle func)))
  980. (and function (funcall function data))))))
  981. (defun mh-handle-set-external-undisplayer (folder handle function)
  982. "Replacement for `mm-handle-set-external-undisplayer'.
  983. This is only called in recent versions of Gnus. The MIME handles
  984. are stored in data structures corresponding to MH-E folder buffer
  985. FOLDER instead of in Gnus (as in the original). The MIME part,
  986. HANDLE is associated with the undisplayer FUNCTION."
  987. (if (mh-mm-keep-viewer-alive-p handle)
  988. (let ((new-handle (copy-sequence handle)))
  989. (mm-handle-set-undisplayer new-handle function)
  990. (mm-handle-set-undisplayer handle nil)
  991. (with-current-buffer folder
  992. (push new-handle (mh-mime-handles (mh-buffer-data)))))
  993. (mm-handle-set-undisplayer handle function)))
  994. (defun mh-mime-security-press-button (handle)
  995. "Callback from security button for part HANDLE."
  996. (if (mh-mm-handle-multipart-ctl-parameter handle 'gnus-info)
  997. (mh-mime-security-show-details handle)
  998. (let ((region (mh-mm-handle-multipart-ctl-parameter handle 'mh-region))
  999. point)
  1000. (setq point (point))
  1001. (goto-char (car region))
  1002. (delete-region (car region) (cdr region))
  1003. (with-current-buffer (mh-mm-handle-multipart-ctl-parameter handle 'buffer)
  1004. (let* ((mm-verify-option 'known)
  1005. (mm-decrypt-option 'known)
  1006. (new (mh-mm-possibly-verify-or-decrypt (cdr handle) handle)))
  1007. (unless (eq new (cdr handle))
  1008. (mh-mm-destroy-parts (cdr handle))
  1009. (setcdr handle new))))
  1010. (mh-mime-display-security handle)
  1011. (goto-char point))))
  1012. ;; I rewrote the security part because Gnus doesn't seem to ever minimize
  1013. ;; the button. That is once the mime-security button is pressed there seems
  1014. ;; to be no way of getting rid of the inserted text.
  1015. (defun mh-mime-security-show-details (handle)
  1016. "Toggle display of detailed security info for HANDLE."
  1017. (let ((details (mh-mm-handle-multipart-ctl-parameter handle 'gnus-details)))
  1018. (when details
  1019. (let ((mh-mime-security-button-pressed
  1020. (not (get-text-property (point) 'mh-button-pressed)))
  1021. (mh-mime-security-button-line-format
  1022. (get-text-property (point) 'mh-line-format)))
  1023. (forward-char -1)
  1024. (while (eq (get-text-property (point) 'mh-line-format)
  1025. mh-mime-security-button-line-format)
  1026. (forward-char -1))
  1027. (forward-char)
  1028. (save-restriction
  1029. (narrow-to-region (point) (point))
  1030. (mh-insert-mime-security-button handle))
  1031. (delete-region
  1032. (point)
  1033. (or (text-property-not-all
  1034. (point) (point-max)
  1035. 'mh-line-format mh-mime-security-button-line-format)
  1036. (point-max)))
  1037. (forward-line -1)))))
  1038. ;;; Miscellaneous Article Washing
  1039. ;;;###mh-autoload
  1040. (defun mh-add-missing-mime-version-header ()
  1041. "Some mail programs don't put a MIME-Version header.
  1042. I have seen this only in spam, so maybe we shouldn't fix
  1043. this ;-)"
  1044. (save-excursion
  1045. (goto-char (point-min))
  1046. (re-search-forward "\n\n" nil t)
  1047. (save-restriction
  1048. (narrow-to-region (point-min) (point))
  1049. (when (and (message-fetch-field "content-type")
  1050. (not (message-fetch-field "mime-version")))
  1051. (goto-char (point-min))
  1052. (insert "MIME-Version: 1.0\n")))))
  1053. ;;;###mh-autoload
  1054. (defun mh-display-smileys ()
  1055. "Display smileys."
  1056. (when (and mh-graphical-smileys-flag (mh-small-show-buffer-p))
  1057. (mh-funcall-if-exists smiley-region (point-min) (point-max))))
  1058. ;;;###mh-autoload
  1059. (defun mh-display-emphasis ()
  1060. "Display graphical emphasis."
  1061. (when (and mh-graphical-emphasis-flag (mh-small-show-buffer-p))
  1062. (flet ((article-goto-body ())) ; shadow this function to do nothing
  1063. (save-excursion
  1064. (goto-char (point-min))
  1065. (article-emphasize)))))
  1066. (defun mh-small-show-buffer-p ()
  1067. "Check if show buffer is small.
  1068. This is used to decide if smileys and graphical emphasis should be
  1069. displayed."
  1070. (let ((max nil))
  1071. (when (and (boundp 'font-lock-maximum-size) font-lock-maximum-size)
  1072. (cond ((numberp font-lock-maximum-size)
  1073. (setq max font-lock-maximum-size))
  1074. ((listp font-lock-maximum-size)
  1075. (setq max (cdr (or (assoc 'mh-show-mode font-lock-maximum-size)
  1076. (assoc t font-lock-maximum-size)))))))
  1077. (or (not (numberp max)) (>= (/ max 8) (buffer-size)))))
  1078. ;;; MH-Letter Commands
  1079. ;; MH-E commands are alphabetical; specific support routines follow command.
  1080. ;;;###mh-autoload
  1081. (defun mh-compose-forward (&optional description folder range)
  1082. "Add tag to forward a message.
  1083. You are prompted for a content DESCRIPTION, the name of the
  1084. FOLDER in which the messages to forward are located, and a RANGE
  1085. of messages, which defaults to the current message in that
  1086. folder. Check the documentation of `mh-interactive-range' to see
  1087. how RANGE is read in interactive use.
  1088. The option `mh-compose-insertion' controls what type of tags are inserted."
  1089. (interactive
  1090. (let* ((description
  1091. (mml-minibuffer-read-description))
  1092. (folder
  1093. (mh-prompt-for-folder "Message from"
  1094. mh-sent-from-folder nil))
  1095. (default
  1096. (if (and (equal folder mh-sent-from-folder)
  1097. (numberp mh-sent-from-msg))
  1098. mh-sent-from-msg
  1099. (nth 0 (mh-translate-range folder "cur"))))
  1100. (range
  1101. (mh-read-range "Forward" folder
  1102. (or (and default
  1103. (number-to-string default))
  1104. t)
  1105. t t)))
  1106. (list description folder range)))
  1107. (let ((messages (mapconcat 'identity (mh-list-to-string range) " ")))
  1108. (dolist (message (mh-translate-range folder messages))
  1109. (if (equal mh-compose-insertion 'mml)
  1110. (mh-mml-forward-message description folder (format "%s" message))
  1111. (mh-mh-forward-message description folder (format "%s" message))))))
  1112. ;;;###mh-autoload
  1113. (defun mh-mml-forward-message (description folder message)
  1114. "Forward a message as attachment.
  1115. The function will prompt the user for a DESCRIPTION, a FOLDER and
  1116. MESSAGE number."
  1117. (let ((msg (if (and (equal message "") (numberp mh-sent-from-msg))
  1118. mh-sent-from-msg
  1119. (string-to-number message))))
  1120. (cond ((integerp msg)
  1121. (mml-attach-file (format "%s%s/%d"
  1122. mh-user-path (substring folder 1) msg)
  1123. "message/rfc822"
  1124. (if (string= "" description) nil description)
  1125. "inline"))
  1126. (t (error "The message number, %s, is not a integer" msg)))))
  1127. (defun mh-mh-forward-message (&optional description folder messages)
  1128. "Add tag to forward a message.
  1129. You are prompted for a content DESCRIPTION, the name of the
  1130. FOLDER in which the messages to forward are located, and the
  1131. MESSAGES' numbers.
  1132. See also \\[mh-mh-to-mime]."
  1133. (interactive (list
  1134. (mml-minibuffer-read-description)
  1135. (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
  1136. (read-string (concat "Messages"
  1137. (if (numberp mh-sent-from-msg)
  1138. (format " (default %d): "
  1139. mh-sent-from-msg)
  1140. ": ")))))
  1141. (beginning-of-line)
  1142. (insert "#forw [")
  1143. (and description
  1144. (not (string= description ""))
  1145. (insert description))
  1146. (insert "]")
  1147. (and folder
  1148. (not (string= folder ""))
  1149. (insert " " folder))
  1150. (if (and messages
  1151. (not (string= messages "")))
  1152. (let ((start (point)))
  1153. (insert " " messages)
  1154. (subst-char-in-region start (point) ?, ? ))
  1155. (if (numberp mh-sent-from-msg)
  1156. (insert " " (int-to-string mh-sent-from-msg))))
  1157. (insert "\n"))
  1158. ;;;###mh-autoload
  1159. (defun mh-compose-insertion (&optional inline)
  1160. "Add tag to include a file such as an image or sound.
  1161. You are prompted for the filename containing the object, the
  1162. media type if it cannot be determined automatically, and a
  1163. content description. If you're using MH-style directives, you
  1164. will also be prompted for additional attributes.
  1165. The option `mh-compose-insertion' controls what type of tags are
  1166. inserted. Optional argument INLINE means make it an inline
  1167. attachment."
  1168. (interactive "P")
  1169. (if (equal mh-compose-insertion 'mml)
  1170. (if inline
  1171. (mh-mml-attach-file "inline")
  1172. (mh-mml-attach-file))
  1173. (call-interactively 'mh-mh-attach-file)))
  1174. (defun mh-mml-attach-file (&optional disposition)
  1175. "Add a tag to insert a MIME message part from a file.
  1176. You are prompted for the filename containing the object, the
  1177. media type if it cannot be determined automatically, a content
  1178. description and the DISPOSITION of the attachment.
  1179. This is basically `mml-attach-file' from Gnus, modified such that a prefix
  1180. argument yields an \"inline\" disposition and Content-Type is determined
  1181. automatically."
  1182. (let* ((file (mml-minibuffer-read-file "Attach file: "))
  1183. (type (mh-minibuffer-read-type file))
  1184. (description (mml-minibuffer-read-description))
  1185. (dispos (or disposition
  1186. (mh-mml-minibuffer-read-disposition type))))
  1187. (mml-insert-empty-tag 'part 'type type 'filename file
  1188. 'disposition dispos 'description description)))
  1189. (defun mh-mh-attach-file (filename type description attributes)
  1190. "Add a tag to insert a MIME message part from a file.
  1191. You are prompted for the FILENAME containing the object, the
  1192. media TYPE if it cannot be determined automatically, and a
  1193. content DESCRIPTION. In addition, you are also prompted for
  1194. additional ATTRIBUTES.
  1195. See also \\[mh-mh-to-mime]."
  1196. (interactive (let ((filename (mml-minibuffer-read-file "Attach file: ")))
  1197. (list
  1198. filename
  1199. (mh-minibuffer-read-type filename)
  1200. (mml-minibuffer-read-description)
  1201. (read-string "Attributes: "
  1202. (concat "name=\""
  1203. (file-name-nondirectory filename)
  1204. "\"")))))
  1205. (mh-mh-compose-type filename type description attributes))
  1206. (defun mh-mh-compose-type (filename type
  1207. &optional description attributes comment)
  1208. "Insert an MH-style directive to insert a file.
  1209. The file specified by FILENAME is encoded as TYPE. An optional
  1210. DESCRIPTION is used as the Content-Description field, optional
  1211. set of ATTRIBUTES and an optional COMMENT can also be included."
  1212. (beginning-of-line)
  1213. (insert "#" type)
  1214. (and attributes
  1215. (insert "; " attributes))
  1216. (and comment
  1217. (insert " (" comment ")"))
  1218. (insert " [")
  1219. (and description
  1220. (insert description))
  1221. (insert "] " (expand-file-name filename))
  1222. (insert "\n"))
  1223. ;;;###mh-autoload
  1224. (defun mh-mh-compose-anon-ftp (host filename type description)
  1225. "Add tag to include anonymous ftp reference to a file.
  1226. You can have your message initiate an \"ftp\" transfer when the
  1227. recipient reads the message. You are prompted for the remote HOST
  1228. and FILENAME, the media TYPE, and the content DESCRIPTION.
  1229. See also \\[mh-mh-to-mime]."
  1230. (interactive (list
  1231. (read-string "Remote host: ")
  1232. (read-string "Remote filename: ")
  1233. (mh-minibuffer-read-type "DUMMY-FILENAME")
  1234. (mml-minibuffer-read-description)))
  1235. (mh-mh-compose-external-type "anon-ftp" host filename
  1236. type description))
  1237. ;;;###mh-autoload
  1238. (defun mh-mh-compose-external-compressed-tar (host filename description)
  1239. "Add tag to include anonymous ftp reference to a compressed tar file.
  1240. In addition to retrieving the file via anonymous \"ftp\" as per
  1241. the command \\[mh-mh-compose-anon-ftp], the file will also be
  1242. uncompressed and untarred. You are prompted for the remote HOST
  1243. and FILENAME and the content DESCRIPTION.
  1244. See also \\[mh-mh-to-mime]."
  1245. (interactive (list
  1246. (read-string "Remote host: ")
  1247. (read-string "Remote filename: ")
  1248. (mml-minibuffer-read-description)))
  1249. (mh-mh-compose-external-type "anon-ftp" host filename
  1250. "application/octet-stream"
  1251. description
  1252. "type=tar; conversions=x-compress"
  1253. "mode=image"))
  1254. ;; RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One:
  1255. ;; Format of Internet Message Bodies.
  1256. ;; RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two:
  1257. ;; Media Types.
  1258. ;; RFC 2049 - Multipurpose Internet Mail Extensions (MIME) Part Five:
  1259. ;; Conformance Criteria and Examples.
  1260. ;; RFC 2017 - Definition of the URL MIME External-Body Access-Type
  1261. ;; RFC 1738 - Uniform Resource Locators (URL)
  1262. (defvar mh-access-types
  1263. '(("anon-ftp") ; RFC2046 Anonymous File Transfer Protocol
  1264. ("file") ; RFC1738 Host-specific file names
  1265. ("ftp") ; RFC2046 File Transfer Protocol
  1266. ("gopher") ; RFC1738 The Gopher Protocol
  1267. ("http") ; RFC1738 Hypertext Transfer Protocol
  1268. ("local-file") ; RFC2046 Local file access
  1269. ("mail-server") ; RFC2046 mail-server Electronic mail address
  1270. ("mailto") ; RFC1738 Electronic mail address
  1271. ("news") ; RFC1738 Usenet news
  1272. ("nntp") ; RFC1738 Usenet news using NNTP access
  1273. ("prospero") ; RFC1738 Prospero Directory Service
  1274. ("telnet") ; RFC1738 Telnet
  1275. ("tftp") ; RFC2046 Trivial File Transfer Protocol
  1276. ("url") ; RFC2017 URL scheme MIME access-type Protocol
  1277. ("wais")) ; RFC1738 Wide Area Information Servers
  1278. "Valid MIME access-type values.")
  1279. ;;;###mh-autoload
  1280. (defun mh-mh-compose-external-type (access-type host filename type
  1281. &optional description
  1282. attributes parameters
  1283. comment)
  1284. "Add tag to refer to a remote file.
  1285. This command is a general utility for referencing external files.
  1286. In fact, all of the other commands that insert directives to
  1287. access external files call this command. You are prompted for the
  1288. ACCESS-TYPE, remote HOST and FILENAME, and content TYPE. If you
  1289. provide a prefix argument, you are also prompted for a content
  1290. DESCRIPTION, ATTRIBUTES, PARAMETERS, and a COMMENT.
  1291. See also \\[mh-mh-to-mime]."
  1292. (interactive (list
  1293. (completing-read "Access type: " mh-access-types)
  1294. (read-string "Remote host: ")
  1295. (read-string "Remote filename: ")
  1296. (mh-minibuffer-read-type "DUMMY-FILENAME")
  1297. (if current-prefix-arg (mml-minibuffer-read-description))
  1298. (if current-prefix-arg (read-string "Attributes: "))
  1299. (if current-prefix-arg (read-string "Parameters: "))
  1300. (if current-prefix-arg (read-string "Comment: "))))
  1301. (beginning-of-line)
  1302. (insert "#@" type)
  1303. (and attributes
  1304. (insert "; " attributes))
  1305. (and comment
  1306. (insert " (" comment ") "))
  1307. (insert " [")
  1308. (and description
  1309. (insert description))
  1310. (insert "] ")
  1311. (insert "access-type=" access-type "; ")
  1312. (insert "site=" host)
  1313. (insert "; name=" (file-name-nondirectory filename))
  1314. (let ((directory (file-name-directory filename)))
  1315. (and directory
  1316. (insert "; directory=\"" directory "\"")))
  1317. (and parameters
  1318. (insert "; " parameters))
  1319. (insert "\n"))
  1320. (defvar mh-mh-to-mime-args nil
  1321. "Extra arguments for \\[mh-mh-to-mime] to pass to the \"mhbuild\" command.
  1322. The arguments are passed to \"mhbuild\" if \\[mh-mh-to-mime] is
  1323. given a prefix argument. Normally default arguments to
  1324. \"mhbuild\" are specified in the MH profile.")
  1325. ;;;###mh-autoload
  1326. (defun mh-mh-to-mime (&optional extra-args)
  1327. "Compose MIME message from MH-style directives.
  1328. Typically, you send a message with attachments just like any other
  1329. message. However, you may take a sneak preview of the MIME encoding if
  1330. you wish by running this command.
  1331. If you wish to pass additional arguments to \"mhbuild\" (\"mhn\")
  1332. to affect how it builds your message, use the option
  1333. `mh-mh-to-mime-args'. For example, you can build a consistency
  1334. check into the message by setting `mh-mh-to-mime-args' to
  1335. \"-check\". The recipient of your message can then run \"mhbuild
  1336. -check\" on the message--\"mhbuild\" (\"mhn\") will complain if
  1337. the message has been corrupted on the way. This command only
  1338. consults this option when given a prefix argument EXTRA-ARGS.
  1339. The hook `mh-mh-to-mime-hook' is called after the message has been
  1340. formatted.
  1341. The effects of this command can be undone by running
  1342. \\[mh-mh-to-mime-undo]."
  1343. (interactive "*P")
  1344. (mh-mh-quote-unescaped-sharp)
  1345. (save-buffer)
  1346. (message "Running %s..." (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
  1347. (cond
  1348. ((mh-variant-p 'nmh)
  1349. (mh-exec-cmd-error nil
  1350. "mhbuild"
  1351. (if extra-args mh-mh-to-mime-args)
  1352. buffer-file-name))
  1353. (t
  1354. (mh-exec-cmd-error (format "mhdraft=%s" buffer-file-name)
  1355. "mhn"
  1356. (if extra-args mh-mh-to-mime-args)
  1357. buffer-file-name)))
  1358. (revert-buffer t t t)
  1359. (message "Running %s...done" (if (mh-variant-p 'nmh) "mhbuild" "mhn"))
  1360. (run-hooks 'mh-mh-to-mime-hook))
  1361. (defun mh-mh-quote-unescaped-sharp ()
  1362. "Quote \"#\" characters that haven't been quoted for \"mhbuild\".
  1363. If the \"#\" character is present in the first column, but it isn't
  1364. part of a MH-style directive then \"mhbuild\" gives an error.
  1365. This function will quote all such characters."
  1366. (save-excursion
  1367. (goto-char (point-min))
  1368. (while (re-search-forward "^#" nil t)
  1369. (beginning-of-line)
  1370. (unless (mh-mh-directive-present-p (point) (mh-line-end-position))
  1371. (insert "#"))
  1372. (goto-char (mh-line-end-position)))))
  1373. ;;;###mh-autoload
  1374. (defun mh-mh-to-mime-undo (noconfirm)
  1375. "Undo effects of \\[mh-mh-to-mime].
  1376. It does this by reverting to a backup file. You are prompted to
  1377. confirm this action, but you can avoid the confirmation by adding
  1378. a prefix argument NOCONFIRM."
  1379. (interactive "*P")
  1380. (if (null buffer-file-name)
  1381. (error "Buffer does not seem to be associated with any file"))
  1382. (let ((backup-strings '("," "#"))
  1383. backup-file)
  1384. (while (and backup-strings
  1385. (not (file-exists-p
  1386. (setq backup-file
  1387. (concat (file-name-directory buffer-file-name)
  1388. (car backup-strings)
  1389. (file-name-nondirectory buffer-file-name)
  1390. ".orig")))))
  1391. (setq backup-strings (cdr backup-strings)))
  1392. (or backup-strings
  1393. (error "Backup file for %s no longer exists" buffer-file-name))
  1394. (or noconfirm
  1395. (yes-or-no-p (format "Revert buffer from file %s? "
  1396. backup-file))
  1397. (error "Revert not confirmed"))
  1398. (let ((buffer-read-only nil))
  1399. (erase-buffer)
  1400. (insert-file-contents backup-file))
  1401. (after-find-file nil nil nil nil t)))
  1402. ;; Shush compiler.
  1403. (defvar mh-identity-pgg-default-user-id)
  1404. ;;;###mh-autoload
  1405. (defun mh-mml-secure-message-encrypt (method)
  1406. "Add tag to encrypt the message.
  1407. A proper multipart message is created for you when you send the
  1408. message. Use the command \\[mh-mml-unsecure-message] to remove
  1409. this tag. Use a prefix argument METHOD to be prompted for one of
  1410. the possible security methods (see `mh-mml-method-default')."
  1411. (interactive (list (mh-mml-query-cryptographic-method)))
  1412. (mh-secure-message method "encrypt" mh-identity-pgg-default-user-id))
  1413. ;;;###mh-autoload
  1414. (defun mh-mml-secure-message-sign (method)
  1415. "Add tag to sign the message.
  1416. A proper multipart message is created for you when you send the
  1417. message. Use the command \\[mh-mml-unsecure-message] to remove
  1418. this tag. Use a prefix argument METHOD to be prompted for one of
  1419. the possible security methods (see `mh-mml-method-default')."
  1420. (interactive (list (mh-mml-query-cryptographic-method)))
  1421. (mh-secure-message method "sign" mh-identity-pgg-default-user-id))
  1422. ;;;###mh-autoload
  1423. (defun mh-mml-secure-message-signencrypt (method)
  1424. "Add tag to encrypt and sign the message.
  1425. A proper multipart message is created for you when you send the
  1426. message. Use the command \\[mh-mml-unsecure-message] to remove
  1427. this tag. Use a prefix argument METHOD to be prompted for one of
  1428. the possible security methods (see `mh-mml-method-default')."
  1429. (interactive (list (mh-mml-query-cryptographic-method)))
  1430. (mh-secure-message method "signencrypt" mh-identity-pgg-default-user-id))
  1431. (defvar mh-mml-cryptographic-method-history ())
  1432. (defun mh-mml-query-cryptographic-method ()
  1433. "Read the cryptographic method to use."
  1434. (if current-prefix-arg
  1435. (let ((def (or (car mh-mml-cryptographic-method-history)
  1436. mh-mml-method-default)))
  1437. (completing-read (format "Method (default %s): " def)
  1438. '(("pgp") ("pgpmime") ("smime"))
  1439. nil t nil 'mh-mml-cryptographic-method-history def))
  1440. mh-mml-method-default))
  1441. (defun mh-secure-message (method mode &optional identity)
  1442. "Add tag to encrypt or sign message.
  1443. METHOD should be one of: \"pgpmime\", \"pgp\", \"smime\".
  1444. MODE should be one of: \"sign\", \"encrypt\", \"signencrypt\", \"none\".
  1445. IDENTITY is optionally the default-user-id to use."
  1446. (if (not mh-pgp-support-flag)
  1447. (error "Your version of Gnus does not support PGP/GPG")
  1448. ;; Check the arguments
  1449. (let ((valid-methods (list "pgpmime" "pgp" "smime"))
  1450. (valid-modes (list "sign" "encrypt" "signencrypt" "none")))
  1451. (if (not (member method valid-methods))
  1452. (error "Method %s is invalid" method))
  1453. (if (not (member mode valid-modes))
  1454. (error "Mode %s is invalid" mode))
  1455. (mml-unsecure-message)
  1456. (if (not (string= mode "none"))
  1457. (save-excursion
  1458. (goto-char (point-min))
  1459. (mh-goto-header-end 1)
  1460. (if mh-identity-pgg-default-user-id
  1461. (mml-insert-tag 'secure 'method method 'mode mode
  1462. 'sender mh-identity-pgg-default-user-id)
  1463. (mml-insert-tag 'secure 'method method 'mode mode)))))))
  1464. ;;;###mh-autoload
  1465. (defun mh-mml-to-mime ()
  1466. "Compose MIME message from MML tags.
  1467. Typically, you send a message with attachments just like any
  1468. other message. However, you may take a sneak preview of the MIME
  1469. encoding if you wish by running this command.
  1470. This action can be undone by running \\[undo]."
  1471. (interactive)
  1472. (require 'message)
  1473. (when mh-pgp-support-flag
  1474. ;; PGP requires actual e-mail addresses, not aliases.
  1475. ;; Parse the recipients and sender from the message.
  1476. (message-options-set-recipient)
  1477. ;; Do an alias lookup on sender (if From field is present).
  1478. (when (message-options-get 'message-sender)
  1479. (message-options-set 'message-sender
  1480. (mail-strip-quoted-names
  1481. (mh-alias-expand
  1482. (message-options-get 'message-sender)))))
  1483. ;; Do an alias lookup on recipients
  1484. (message-options-set 'message-recipients
  1485. (mapconcat
  1486. (lambda (ali)
  1487. (mail-strip-quoted-names (mh-alias-expand ali)))
  1488. (split-string (message-options-get 'message-recipients) "[, ]+")
  1489. ", ")))
  1490. (let ((saved-text (buffer-string))
  1491. (buffer (current-buffer))
  1492. (modified-flag (buffer-modified-p)))
  1493. (condition-case err (mml-to-mime)
  1494. (error
  1495. (with-current-buffer buffer
  1496. (delete-region (point-min) (point-max))
  1497. (insert saved-text)
  1498. (set-buffer-modified-p modified-flag))
  1499. (error (error-message-string err))))))
  1500. ;;;###mh-autoload
  1501. (defun mh-mml-unsecure-message ()
  1502. "Remove any secure message tags."
  1503. (interactive)
  1504. (if (not mh-pgp-support-flag)
  1505. (error "Your version of Gnus does not support PGP/GPG")
  1506. (mml-unsecure-message)))
  1507. ;;; Support Routines for MH-Letter Commands
  1508. ;;;###mh-autoload
  1509. (defun mh-mml-tag-present-p ()
  1510. "Check if the current buffer has text which may be a MML tag."
  1511. (save-excursion
  1512. (goto-char (point-min))
  1513. (re-search-forward
  1514. (concat
  1515. "\\(<#\\(mml\\|part\\)\\(.\\|\n\\)*>[ \n\t]*<#/\\(mml\\|part\\)>\\|"
  1516. "^<#secure.+>$\\)")
  1517. nil t)))
  1518. (defvar mh-media-type-regexp
  1519. (concat (regexp-opt '("text" "image" "audio" "video" "application"
  1520. "multipart" "message") t)
  1521. "/[-.+a-zA-Z0-9]+")
  1522. "Regexp matching valid media types used in MIME attachment compositions.")
  1523. ;;;###mh-autoload
  1524. (defun mh-mh-directive-present-p (&optional begin end)
  1525. "Check if the text between BEGIN and END might be a MH-style directive.
  1526. The optional argument BEGIN defaults to the beginning of the
  1527. buffer, while END defaults to the end of the buffer."
  1528. (unless begin (setq begin (point-min)))
  1529. (unless end (setq end (point-max)))
  1530. (save-excursion
  1531. (block search-for-mh-directive
  1532. (goto-char begin)
  1533. (while (re-search-forward "^#" end t)
  1534. (let ((s (buffer-substring-no-properties
  1535. (point) (mh-line-end-position))))
  1536. (cond ((equal s ""))
  1537. ((string-match "^forw[ \t\n]+" s)
  1538. (return-from search-for-mh-directive t))
  1539. (t (let ((first-token (car (split-string s "[ \t;@]"))))
  1540. (when (and first-token
  1541. (string-match mh-media-type-regexp
  1542. first-token))
  1543. (return-from search-for-mh-directive t)))))))
  1544. nil)))
  1545. (defun mh-minibuffer-read-type (filename &optional default)
  1546. "Return the content type associated with the given FILENAME.
  1547. If the \"file\" command exists and recognizes the given file,
  1548. then its value is returned\; otherwise, the user is prompted for
  1549. a type (see `mailcap-mime-types').
  1550. Optional argument DEFAULT is returned if a type isn't entered."
  1551. (mailcap-parse-mimetypes)
  1552. (let* ((default (or default
  1553. (mm-default-file-encoding filename)
  1554. "application/octet-stream"))
  1555. (probed-type (mh-file-mime-type filename))
  1556. (type (or (and (not (equal probed-type "application/octet-stream"))
  1557. probed-type)
  1558. (completing-read
  1559. (format "Content type (default %s): " default)
  1560. (mapcar 'list (mailcap-mime-types))))))
  1561. (if (not (equal type ""))
  1562. type
  1563. default)))
  1564. ;;;###mh-autoload
  1565. (defun mh-file-mime-type (filename)
  1566. "Return MIME type of FILENAME from file command.
  1567. Returns nil if file command not on system."
  1568. (cond
  1569. ((not (mh-have-file-command))
  1570. nil) ;no file command, exit now
  1571. ((not (and (file-exists-p filename)
  1572. (file-readable-p filename)))
  1573. nil) ;no file or not readable, ditto
  1574. (t
  1575. (let ((tmp-buffer (get-buffer-create mh-temp-buffer)))
  1576. (with-current-buffer tmp-buffer
  1577. (unwind-protect
  1578. (progn
  1579. (call-process "file" nil '(t nil) nil "-b" "-i"
  1580. (expand-file-name filename))
  1581. (goto-char (point-min))
  1582. (if (not (re-search-forward mh-media-type-regexp nil t))
  1583. nil
  1584. (mh-file-mime-type-substitute (match-string 0) filename)))
  1585. (kill-buffer tmp-buffer)))))))
  1586. (defvar mh-file-mime-type-substitutions
  1587. '(("application/msword" "\.xls" "application/ms-excel")
  1588. ("application/msword" "\.ppt" "application/ms-powerpoint")
  1589. ("text/plain" "\.vcf" "text/x-vcard")
  1590. ("text/rtf" "\.rtf" "application/rtf")
  1591. ("application/x-zip" "\.sxc" "application/vnd.sun.xml.calc")
  1592. ("application/x-zip" "\.sxd" "application/vnd.sun.xml.draw")
  1593. ("application/x-zip" "\.sxi" "application/vnd.sun.xml.impress")
  1594. ("application/x-zip" "\.sxw" "application/vnd.sun.xml.writer")
  1595. ("application/x-zip" "\.odg" "application/vnd.oasis.opendocument.graphics")
  1596. ("application/x-zip" "\.odi" "application/vnd.oasis.opendocument.image")
  1597. ("application/x-zip" "\.odp"
  1598. "application/vnd.oasis.opendocument.presentation")
  1599. ("application/x-zip" "\.ods"
  1600. "application/vnd.oasis.opendocument.spreadsheet")
  1601. ("application/x-zip" "\.odt" "application/vnd.oasis.opendocument.text"))
  1602. "Substitutions to make for Content-Type returned from file command.
  1603. The first element is the Content-Type returned by the file command.
  1604. The second element is a regexp matching the file name, usually the
  1605. extension.
  1606. The third element is the Content-Type to replace with.")
  1607. (defun mh-file-mime-type-substitute (content-type filename)
  1608. "Return possibly changed CONTENT-TYPE on the FILENAME.
  1609. Substitutions are made from the `mh-file-mime-type-substitutions'
  1610. variable."
  1611. (let ((subst mh-file-mime-type-substitutions)
  1612. (type) (match) (answer content-type)
  1613. (case-fold-search t))
  1614. (while subst
  1615. (setq type (car (car subst))
  1616. match (elt (car subst) 1))
  1617. (if (and (string-equal content-type type)
  1618. (string-match match filename))
  1619. (setq answer (elt (car subst) 2)
  1620. subst nil)
  1621. (setq subst (cdr subst))))
  1622. answer))
  1623. (defvar mh-have-file-command 'undefined
  1624. "Cached value of function `mh-have-file-command'.
  1625. Do not reference this variable directly as it might not have been
  1626. initialized. Always use the command `mh-have-file-command'.")
  1627. ;;;###mh-autoload
  1628. (defun mh-have-file-command ()
  1629. "Return t if 'file' command is on the system.
  1630. 'file -i' is used to get MIME type of composition insertion."
  1631. (when (eq mh-have-file-command 'undefined)
  1632. (setq mh-have-file-command
  1633. (and (fboundp 'executable-find)
  1634. (executable-find "file") ; file command exists
  1635. ; and accepts -i and -b args.
  1636. (zerop (call-process "file" nil nil nil "-i" "-b"
  1637. (expand-file-name "inc" mh-progs))))))
  1638. mh-have-file-command)
  1639. ;;; MIME Cleanup
  1640. ;;;###mh-autoload
  1641. (defun mh-mime-cleanup ()
  1642. "Free the decoded MIME parts."
  1643. (let ((mime-data (gethash (current-buffer) mh-globals-hash)))
  1644. ;; This is for Emacs, what about XEmacs?
  1645. (mh-funcall-if-exists remove-images (point-min) (point-max))
  1646. (when mime-data
  1647. (mh-mm-destroy-parts (mh-mime-handles mime-data))
  1648. (remhash (current-buffer) mh-globals-hash))))
  1649. ;;;###mh-autoload
  1650. (defun mh-destroy-postponed-handles ()
  1651. "Free MIME data for externally displayed MIME parts."
  1652. (let ((mime-data (mh-buffer-data)))
  1653. (when mime-data
  1654. (mh-mm-destroy-parts (mh-mime-handles mime-data)))
  1655. (remhash (current-buffer) mh-globals-hash)))
  1656. (provide 'mh-mime)
  1657. ;; Local Variables:
  1658. ;; indent-tabs-mode: nil
  1659. ;; sentence-end-double-space: nil
  1660. ;; End:
  1661. ;;; mh-mime.el ends here