mml.el 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. ;;; mml.el --- A package for parsing and validating MML documents
  2. ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (require 'mm-util)
  18. (require 'mm-bodies)
  19. (require 'mm-encode)
  20. (require 'mm-decode)
  21. (require 'mml-sec)
  22. (eval-when-compile (require 'cl))
  23. (eval-when-compile (require 'url))
  24. (eval-when-compile
  25. (when (featurep 'xemacs)
  26. (require 'easy-mmode))) ; for `define-minor-mode'
  27. (autoload 'message-make-message-id "message")
  28. (declare-function gnus-setup-posting-charset "gnus-msg" (group))
  29. (autoload 'gnus-make-local-hook "gnus-util")
  30. (autoload 'gnus-completing-read "gnus-util")
  31. (autoload 'message-fetch-field "message")
  32. (autoload 'message-mark-active-p "message")
  33. (autoload 'message-info "message")
  34. (autoload 'fill-flowed-encode "flow-fill")
  35. (autoload 'message-posting-charset "message")
  36. (autoload 'dnd-get-local-file-name "dnd")
  37. (autoload 'message-options-set "message")
  38. (autoload 'message-narrow-to-head "message")
  39. (autoload 'message-in-body-p "message")
  40. (autoload 'message-mail-p "message")
  41. (defvar gnus-article-mime-handles)
  42. (defvar gnus-mouse-2)
  43. (defvar gnus-newsrc-hashtb)
  44. (defvar message-default-charset)
  45. (defvar message-deletable-headers)
  46. (defvar message-options)
  47. (defvar message-posting-charset)
  48. (defvar message-required-mail-headers)
  49. (defvar message-required-news-headers)
  50. (defvar dnd-protocol-alist)
  51. (defvar mml-dnd-protocol-alist)
  52. (defcustom mml-content-type-parameters
  53. '(name access-type expiration size permission format)
  54. "*A list of acceptable parameters in MML tag.
  55. These parameters are generated in Content-Type header if exists."
  56. :version "22.1"
  57. :type '(repeat (symbol :tag "Parameter"))
  58. :group 'message)
  59. (defcustom mml-content-disposition-parameters
  60. '(filename creation-date modification-date read-date)
  61. "*A list of acceptable parameters in MML tag.
  62. These parameters are generated in Content-Disposition header if exists."
  63. :version "22.1"
  64. :type '(repeat (symbol :tag "Parameter"))
  65. :group 'message)
  66. (defcustom mml-content-disposition-alist
  67. '((text (rtf . "attachment") (t . "inline"))
  68. (t . "attachment"))
  69. "Alist of MIME types or regexps matching file names and default dispositions.
  70. Each element should be one of the following three forms:
  71. (REGEXP . DISPOSITION)
  72. (SUPERTYPE (SUBTYPE . DISPOSITION) (SUBTYPE . DISPOSITION)...)
  73. (TYPE . DISPOSITION)
  74. Where REGEXP is a string which matches the file name (if any) of an
  75. attachment, SUPERTYPE, SUBTYPE and TYPE should be symbols which are a
  76. MIME supertype (e.g., text), a MIME subtype (e.g., plain) and a MIME
  77. type (e.g., text/plain) respectively, and DISPOSITION should be either
  78. the string \"attachment\" or the string \"inline\". The value t for
  79. SUPERTYPE, SUBTYPE or TYPE matches any of those types. The first
  80. match found will be used."
  81. :version "23.1" ;; No Gnus
  82. :type (let ((dispositions '(radio :format "DISPOSITION: %v"
  83. :value "attachment"
  84. (const :format "%v " "attachment")
  85. (const :format "%v\n" "inline"))))
  86. `(repeat
  87. :offset 0
  88. (choice :format "%[Value Menu%]%v"
  89. (cons :tag "(REGEXP . DISPOSITION)" :extra-offset 4
  90. (regexp :tag "REGEXP" :value ".*")
  91. ,dispositions)
  92. (cons :tag "(SUPERTYPE (SUBTYPE . DISPOSITION)...)"
  93. :indent 0
  94. (symbol :tag " SUPERTYPE" :value text)
  95. (repeat :format "%v%i\n" :offset 0 :extra-offset 4
  96. (cons :format "%v" :extra-offset 5
  97. (symbol :tag "SUBTYPE" :value t)
  98. ,dispositions)))
  99. (cons :tag "(TYPE . DISPOSITION)" :extra-offset 4
  100. (symbol :tag "TYPE" :value t)
  101. ,dispositions))))
  102. :group 'message)
  103. (defcustom mml-insert-mime-headers-always t
  104. "If non-nil, always put Content-Type: text/plain at top of empty parts.
  105. It is necessary to work against a bug in certain clients."
  106. :version "24.1"
  107. :type 'boolean
  108. :group 'message)
  109. (defcustom mml-enable-flowed t
  110. "If non-nil, enable format=flowed usage when encoding a message.
  111. This is only performed when filling on text/plain with hard
  112. newlines in the text."
  113. :version "24.1"
  114. :type 'boolean
  115. :group 'message)
  116. (defvar mml-tweak-type-alist nil
  117. "A list of (TYPE . FUNCTION) for tweaking MML parts.
  118. TYPE is a string containing a regexp to match the MIME type. FUNCTION
  119. is a Lisp function which is called with the MML handle to tweak the
  120. part. This variable is used only when no TWEAK parameter exists in
  121. the MML handle.")
  122. (defvar mml-tweak-function-alist nil
  123. "A list of (NAME . FUNCTION) for tweaking MML parts.
  124. NAME is a string containing the name of the TWEAK parameter in the MML
  125. handle. FUNCTION is a Lisp function which is called with the MML
  126. handle to tweak the part.")
  127. (defvar mml-tweak-sexp-alist
  128. '((mml-externalize-attachments . mml-tweak-externalize-attachments))
  129. "A list of (SEXP . FUNCTION) for tweaking MML parts.
  130. SEXP is an s-expression. If the evaluation of SEXP is non-nil, FUNCTION
  131. is called. FUNCTION is a Lisp function which is called with the MML
  132. handle to tweak the part.")
  133. (defvar mml-externalize-attachments nil
  134. "*If non-nil, local-file attachments are generated as external parts.")
  135. (defvar mml-generate-multipart-alist nil
  136. "*Alist of multipart generation functions.
  137. Each entry has the form (NAME . FUNCTION), where
  138. NAME is a string containing the name of the part (without the
  139. leading \"/multipart/\"),
  140. FUNCTION is a Lisp function which is called to generate the part.
  141. The Lisp function has to supply the appropriate MIME headers and the
  142. contents of this part.")
  143. (defvar mml-syntax-table
  144. (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
  145. (modify-syntax-entry ?\\ "/" table)
  146. (modify-syntax-entry ?< "(" table)
  147. (modify-syntax-entry ?> ")" table)
  148. (modify-syntax-entry ?@ "w" table)
  149. (modify-syntax-entry ?/ "w" table)
  150. (modify-syntax-entry ?= " " table)
  151. (modify-syntax-entry ?* " " table)
  152. (modify-syntax-entry ?\; " " table)
  153. (modify-syntax-entry ?\' " " table)
  154. table))
  155. (defvar mml-boundary-function 'mml-make-boundary
  156. "A function called to suggest a boundary.
  157. The function may be called several times, and should try to make a new
  158. suggestion each time. The function is called with one parameter,
  159. which is a number that says how many times the function has been
  160. called for this message.")
  161. (defvar mml-confirmation-set nil
  162. "A list of symbols, each of which disables some warning.
  163. `unknown-encoding': always send messages contain characters with
  164. unknown encoding; `use-ascii': always use ASCII for those characters
  165. with unknown encoding; `multipart': always send messages with more than
  166. one charsets.")
  167. (defvar mml-generate-default-type "text/plain"
  168. "Content type by which the Content-Type header can be omitted.
  169. The Content-Type header will not be put in the MIME part if the type
  170. equals the value and there's no parameter (e.g. charset, format, etc.)
  171. and `mml-insert-mime-headers-always' is nil. The value will be bound
  172. to \"message/rfc822\" when encoding an article to be forwarded as a MIME
  173. part. This is for the internal use, you should never modify the value.")
  174. (defvar mml-buffer-list nil)
  175. (defun mml-generate-new-buffer (name)
  176. (let ((buf (generate-new-buffer name)))
  177. (push buf mml-buffer-list)
  178. buf))
  179. (defun mml-destroy-buffers ()
  180. (let (kill-buffer-hook)
  181. (mapc 'kill-buffer mml-buffer-list)
  182. (setq mml-buffer-list nil)))
  183. (defun mml-parse ()
  184. "Parse the current buffer as an MML document."
  185. (save-excursion
  186. (goto-char (point-min))
  187. (with-syntax-table mml-syntax-table
  188. (mml-parse-1))))
  189. (defun mml-parse-1 ()
  190. "Parse the current buffer as an MML document."
  191. (let (struct tag point contents charsets warn use-ascii no-markup-p raw)
  192. (while (and (not (eobp))
  193. (not (looking-at "<#/multipart")))
  194. (cond
  195. ((looking-at "<#secure")
  196. ;; The secure part is essentially a meta-meta tag, which
  197. ;; expands to either a part tag if there are no other parts in
  198. ;; the document or a multipart tag if there are other parts
  199. ;; included in the message
  200. (let* (secure-mode
  201. (taginfo (mml-read-tag))
  202. (keyfile (cdr (assq 'keyfile taginfo)))
  203. (certfiles (delq nil (mapcar (lambda (tag)
  204. (if (eq (car-safe tag) 'certfile)
  205. (cdr tag)))
  206. taginfo)))
  207. (recipients (cdr (assq 'recipients taginfo)))
  208. (sender (cdr (assq 'sender taginfo)))
  209. (location (cdr (assq 'tag-location taginfo)))
  210. (mode (cdr (assq 'mode taginfo)))
  211. (method (cdr (assq 'method taginfo)))
  212. tags)
  213. (save-excursion
  214. (if (re-search-forward
  215. "<#/?\\(multipart\\|part\\|external\\|mml\\)." nil t)
  216. (setq secure-mode "multipart")
  217. (setq secure-mode "part")))
  218. (save-excursion
  219. (goto-char location)
  220. (re-search-forward "<#secure[^\n]*>\n"))
  221. (delete-region (match-beginning 0) (match-end 0))
  222. (cond ((string= mode "sign")
  223. (setq tags (list "sign" method)))
  224. ((string= mode "encrypt")
  225. (setq tags (list "encrypt" method)))
  226. ((string= mode "signencrypt")
  227. (setq tags (list "sign" method "encrypt" method)))
  228. (t
  229. (error "Unknown secure mode %s" mode)))
  230. (eval `(mml-insert-tag ,secure-mode
  231. ,@tags
  232. ,(if keyfile "keyfile")
  233. ,keyfile
  234. ,@(apply #'append
  235. (mapcar (lambda (certfile)
  236. (list "certfile" certfile))
  237. certfiles))
  238. ,(if recipients "recipients")
  239. ,recipients
  240. ,(if sender "sender")
  241. ,sender))
  242. ;; restart the parse
  243. (goto-char location)))
  244. ((looking-at "<#multipart")
  245. (push (nconc (mml-read-tag) (mml-parse-1)) struct))
  246. ((looking-at "<#external")
  247. (push (nconc (mml-read-tag) (list (cons 'contents (mml-read-part))))
  248. struct))
  249. (t
  250. (if (or (looking-at "<#part") (looking-at "<#mml"))
  251. (setq tag (mml-read-tag)
  252. no-markup-p nil
  253. warn nil)
  254. (setq tag (list 'part '(type . "text/plain"))
  255. no-markup-p t
  256. warn t))
  257. (setq raw (cdr (assq 'raw tag))
  258. point (point)
  259. contents (mml-read-part (eq 'mml (car tag)))
  260. charsets (cond
  261. (raw nil)
  262. ((assq 'charset tag)
  263. (list
  264. (intern (downcase (cdr (assq 'charset tag))))))
  265. (t
  266. (mm-find-mime-charset-region point (point)
  267. mm-hack-charsets))))
  268. (when (and (not raw) (memq nil charsets))
  269. (if (or (memq 'unknown-encoding mml-confirmation-set)
  270. (message-options-get 'unknown-encoding)
  271. (and (y-or-n-p "\
  272. Message contains characters with unknown encoding. Really send? ")
  273. (message-options-set 'unknown-encoding t)))
  274. (if (setq use-ascii
  275. (or (memq 'use-ascii mml-confirmation-set)
  276. (message-options-get 'use-ascii)
  277. (and (y-or-n-p "Use ASCII as charset? ")
  278. (message-options-set 'use-ascii t))))
  279. (setq charsets (delq nil charsets))
  280. (setq warn nil))
  281. (error "Edit your message to remove those characters")))
  282. (if (or raw
  283. (eq 'mml (car tag))
  284. (< (length charsets) 2))
  285. (if (or (not no-markup-p)
  286. (string-match "[^ \t\r\n]" contents))
  287. ;; Don't create blank parts.
  288. (push (nconc tag (list (cons 'contents contents)))
  289. struct))
  290. (let ((nstruct (mml-parse-singlepart-with-multiple-charsets
  291. tag point (point) use-ascii)))
  292. (when (and warn
  293. (not (memq 'multipart mml-confirmation-set))
  294. (not (message-options-get 'multipart))
  295. (not (and (y-or-n-p (format "\
  296. A message part needs to be split into %d charset parts. Really send? "
  297. (length nstruct)))
  298. (message-options-set 'multipart t))))
  299. (error "Edit your message to use only one charset"))
  300. (setq struct (nconc nstruct struct)))))))
  301. (unless (eobp)
  302. (forward-line 1))
  303. (nreverse struct)))
  304. (defun mml-parse-singlepart-with-multiple-charsets
  305. (orig-tag beg end &optional use-ascii)
  306. (save-excursion
  307. (save-restriction
  308. (narrow-to-region beg end)
  309. (goto-char (point-min))
  310. (let ((current (or (mm-mime-charset (mm-charset-after))
  311. (and use-ascii 'us-ascii)))
  312. charset struct space newline paragraph)
  313. (while (not (eobp))
  314. (setq charset (mm-mime-charset (mm-charset-after)))
  315. (cond
  316. ;; The charset remains the same.
  317. ((eq charset 'us-ascii))
  318. ((or (and use-ascii (not charset))
  319. (eq charset current))
  320. (setq space nil
  321. newline nil
  322. paragraph nil))
  323. ;; The initial charset was ascii.
  324. ((eq current 'us-ascii)
  325. (setq current charset
  326. space nil
  327. newline nil
  328. paragraph nil))
  329. ;; We have a change in charsets.
  330. (t
  331. (push (append
  332. orig-tag
  333. (list (cons 'contents
  334. (buffer-substring-no-properties
  335. beg (or paragraph newline space (point))))))
  336. struct)
  337. (setq beg (or paragraph newline space (point))
  338. current charset
  339. space nil
  340. newline nil
  341. paragraph nil)))
  342. ;; Compute places where it might be nice to break the part.
  343. (cond
  344. ((memq (following-char) '(? ?\t))
  345. (setq space (1+ (point))))
  346. ((and (eq (following-char) ?\n)
  347. (not (bobp))
  348. (eq (char-after (1- (point))) ?\n))
  349. (setq paragraph (point)))
  350. ((eq (following-char) ?\n)
  351. (setq newline (1+ (point)))))
  352. (forward-char 1))
  353. ;; Do the final part.
  354. (unless (= beg (point))
  355. (push (append orig-tag
  356. (list (cons 'contents
  357. (buffer-substring-no-properties
  358. beg (point)))))
  359. struct))
  360. struct))))
  361. (defun mml-read-tag ()
  362. "Read a tag and return the contents."
  363. (let ((orig-point (point))
  364. contents name elem val)
  365. (forward-char 2)
  366. (setq name (buffer-substring-no-properties
  367. (point) (progn (forward-sexp 1) (point))))
  368. (skip-chars-forward " \t\n")
  369. (while (not (looking-at ">[ \t]*\n?"))
  370. (setq elem (buffer-substring-no-properties
  371. (point) (progn (forward-sexp 1) (point))))
  372. (skip-chars-forward "= \t\n")
  373. (setq val (buffer-substring-no-properties
  374. (point) (progn (forward-sexp 1) (point))))
  375. (when (string-match "\\`\"" val)
  376. (setq val (read val))) ;; inverse of prin1 in mml-insert-tag
  377. (push (cons (intern elem) val) contents)
  378. (skip-chars-forward " \t\n"))
  379. (goto-char (match-end 0))
  380. ;; Don't skip the leading space.
  381. ;;(skip-chars-forward " \t\n")
  382. ;; Put the tag location into the returned contents
  383. (setq contents (append (list (cons 'tag-location orig-point)) contents))
  384. (cons (intern name) (nreverse contents))))
  385. (defun mml-buffer-substring-no-properties-except-hard-newlines (start end)
  386. (let ((str (buffer-substring-no-properties start end))
  387. (bufstart start) tmp)
  388. (while (setq tmp (text-property-any start end 'hard 't))
  389. (set-text-properties (- tmp bufstart) (- tmp bufstart -1)
  390. '(hard t) str)
  391. (setq start (1+ tmp)))
  392. str))
  393. (defun mml-read-part (&optional mml)
  394. "Return the buffer up till the next part, multipart or closing part or multipart.
  395. If MML is non-nil, return the buffer up till the correspondent mml tag."
  396. (let ((beg (point)) (count 1))
  397. ;; If the tag ended at the end of the line, we go to the next line.
  398. (when (looking-at "[ \t]*\n")
  399. (forward-line 1))
  400. (if mml
  401. (progn
  402. (while (and (> count 0) (not (eobp)))
  403. (if (re-search-forward "<#\\(/\\)?mml." nil t)
  404. (setq count (+ count (if (match-beginning 1) -1 1)))
  405. (goto-char (point-max))))
  406. (mml-buffer-substring-no-properties-except-hard-newlines
  407. beg (if (> count 0)
  408. (point)
  409. (match-beginning 0))))
  410. (if (re-search-forward
  411. "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)." nil t)
  412. (prog1
  413. (mml-buffer-substring-no-properties-except-hard-newlines
  414. beg (match-beginning 0))
  415. (if (or (not (match-beginning 1))
  416. (equal (match-string 2) "multipart"))
  417. (goto-char (match-beginning 0))
  418. (when (looking-at "[ \t]*\n")
  419. (forward-line 1))))
  420. (mml-buffer-substring-no-properties-except-hard-newlines
  421. beg (goto-char (point-max)))))))
  422. (defvar mml-boundary nil)
  423. (defvar mml-base-boundary "-=-=")
  424. (defvar mml-multipart-number 0)
  425. (defvar mml-inhibit-compute-boundary nil)
  426. (declare-function libxml-parse-html-region "xml.c"
  427. (start end &optional base-url discard-comments))
  428. (defun mml-generate-mime (&optional multipart-type)
  429. "Generate a MIME message based on the current MML document.
  430. MULTIPART-TYPE defaults to \"mixed\", but can also
  431. be \"related\" or \"alternate\"."
  432. (let ((cont (mml-parse))
  433. (mml-multipart-number mml-multipart-number)
  434. (options message-options))
  435. (if (not cont)
  436. nil
  437. (when (and (consp (car cont))
  438. (= (length cont) 1)
  439. (fboundp 'libxml-parse-html-region)
  440. (equal (cdr (assq 'type (car cont))) "text/html"))
  441. (setq cont (mml-expand-html-into-multipart-related (car cont))))
  442. (prog1
  443. (mm-with-multibyte-buffer
  444. (setq message-options options)
  445. (cond
  446. ((and (consp (car cont))
  447. (= (length cont) 1))
  448. (mml-generate-mime-1 (car cont)))
  449. ((eq (car cont) 'multipart)
  450. (mml-generate-mime-1 cont))
  451. (t
  452. (mml-generate-mime-1
  453. (nconc (list 'multipart (cons 'type (or multipart-type "mixed")))
  454. cont))))
  455. (setq options message-options)
  456. (buffer-string))
  457. (setq message-options options)))))
  458. (defun mml-expand-html-into-multipart-related (cont)
  459. (let ((new-parts nil)
  460. (cid 1))
  461. (mm-with-multibyte-buffer
  462. (insert (cdr (assq 'contents cont)))
  463. (goto-char (point-min))
  464. (with-syntax-table mml-syntax-table
  465. (while (re-search-forward "<img\\b" nil t)
  466. (goto-char (match-beginning 0))
  467. (let* ((start (point))
  468. (img (nth 2
  469. (nth 2
  470. (libxml-parse-html-region
  471. (point) (progn (forward-sexp) (point))))))
  472. (end (point))
  473. (parsed (url-generic-parse-url (cdr (assq 'src (cadr img))))))
  474. (when (and (null (url-type parsed))
  475. (url-filename parsed)
  476. (file-exists-p (url-filename parsed)))
  477. (goto-char start)
  478. (when (search-forward (url-filename parsed) end t)
  479. (let ((cid (format "fsf.%d" cid)))
  480. (replace-match (concat "cid:" cid) t t)
  481. (push (list cid (url-filename parsed)) new-parts))
  482. (setq cid (1+ cid)))))))
  483. ;; We have local images that we want to include.
  484. (if (not new-parts)
  485. (list cont)
  486. (setcdr (assq 'contents cont) (buffer-string))
  487. (setq cont
  488. (nconc (list 'multipart (cons 'type "related"))
  489. (list cont)))
  490. (dolist (new-part (nreverse new-parts))
  491. (setq cont
  492. (nconc cont
  493. (list `(part (type . "image/png")
  494. (filename . ,(nth 1 new-part))
  495. (id . ,(concat "<" (nth 0 new-part)
  496. ">")))))))
  497. cont))))
  498. (defun mml-generate-mime-1 (cont)
  499. (let ((mm-use-ultra-safe-encoding
  500. (or mm-use-ultra-safe-encoding (assq 'sign cont))))
  501. (save-restriction
  502. (narrow-to-region (point) (point))
  503. (mml-tweak-part cont)
  504. (cond
  505. ((or (eq (car cont) 'part) (eq (car cont) 'mml))
  506. (let* ((raw (cdr (assq 'raw cont)))
  507. (filename (cdr (assq 'filename cont)))
  508. (type (or (cdr (assq 'type cont))
  509. (if filename
  510. (or (mm-default-file-encoding filename)
  511. "application/octet-stream")
  512. "text/plain")))
  513. (charset (cdr (assq 'charset cont)))
  514. (coding (mm-charset-to-coding-system charset))
  515. encoding flowed coded)
  516. (cond ((eq coding 'ascii)
  517. (setq charset nil
  518. coding nil))
  519. (charset
  520. ;; The value of `charset' might be a bogus alias that
  521. ;; `mm-charset-synonym-alist' provides, like `utf8',
  522. ;; so we prefer the MIME charset that Emacs knows for
  523. ;; the coding system `coding'.
  524. (setq charset (or (mm-coding-system-to-mime-charset coding)
  525. (intern (downcase charset))))))
  526. (if (and (not raw)
  527. (member (car (split-string type "/")) '("text" "message")))
  528. (progn
  529. (with-temp-buffer
  530. (cond
  531. ((cdr (assq 'buffer cont))
  532. (insert-buffer-substring (cdr (assq 'buffer cont))))
  533. ((and filename
  534. (not (equal (cdr (assq 'nofile cont)) "yes")))
  535. (let ((coding-system-for-read coding))
  536. (mm-insert-file-contents filename)))
  537. ((eq 'mml (car cont))
  538. (insert (cdr (assq 'contents cont))))
  539. (t
  540. (save-restriction
  541. (narrow-to-region (point) (point))
  542. (insert (cdr (assq 'contents cont)))
  543. ;; Remove quotes from quoted tags.
  544. (goto-char (point-min))
  545. (while (re-search-forward
  546. "<#!+/?\\(part\\|multipart\\|external\\|mml\\|secure\\)"
  547. nil t)
  548. (delete-region (+ (match-beginning 0) 2)
  549. (+ (match-beginning 0) 3))))))
  550. (cond
  551. ((eq (car cont) 'mml)
  552. (let ((mml-boundary (mml-compute-boundary cont))
  553. ;; It is necessary for the case where this
  554. ;; function is called recursively since
  555. ;; `m-g-d-t' will be bound to "message/rfc822"
  556. ;; when encoding an article to be forwarded.
  557. (mml-generate-default-type "text/plain"))
  558. (mml-to-mime)
  559. ;; Update handle so mml-compute-boundary can
  560. ;; detect collisions with the nested parts.
  561. (unless mml-inhibit-compute-boundary
  562. (setcdr (assoc 'contents cont) (buffer-string))))
  563. (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
  564. ;; ignore 0x1b, it is part of iso-2022-jp
  565. (setq encoding (mm-body-7-or-8))))
  566. ((string= (car (split-string type "/")) "message")
  567. (let ((mm-7bit-chars (concat mm-7bit-chars "\x1b")))
  568. ;; ignore 0x1b, it is part of iso-2022-jp
  569. (setq encoding (mm-body-7-or-8))))
  570. (t
  571. ;; Only perform format=flowed filling on text/plain
  572. ;; parts where there either isn't a format parameter
  573. ;; in the mml tag or it says "flowed" and there
  574. ;; actually are hard newlines in the text.
  575. (let (use-hard-newlines)
  576. (when (and mml-enable-flowed
  577. (string= type "text/plain")
  578. (not (string= (cdr (assq 'sign cont)) "pgp"))
  579. (or (null (assq 'format cont))
  580. (string= (cdr (assq 'format cont))
  581. "flowed"))
  582. (setq use-hard-newlines
  583. (text-property-any
  584. (point-min) (point-max) 'hard 't)))
  585. (fill-flowed-encode)
  586. ;; Indicate that `mml-insert-mime-headers' should
  587. ;; insert a "; format=flowed" string unless the
  588. ;; user has already specified it.
  589. (setq flowed (null (assq 'format cont)))))
  590. ;; Prefer `utf-8' for text/calendar parts.
  591. (if (or charset
  592. (not (string= type "text/calendar")))
  593. (setq charset (mm-encode-body charset))
  594. (let ((mm-coding-system-priorities
  595. (cons 'utf-8 mm-coding-system-priorities)))
  596. (setq charset (mm-encode-body))))
  597. (setq encoding (mm-body-encoding
  598. charset (cdr (assq 'encoding cont))))))
  599. (setq coded (buffer-string)))
  600. (mml-insert-mime-headers cont type charset encoding flowed)
  601. (insert "\n")
  602. (insert coded))
  603. (mm-with-unibyte-buffer
  604. (cond
  605. ((cdr (assq 'buffer cont))
  606. (insert (mm-string-as-unibyte
  607. (with-current-buffer (cdr (assq 'buffer cont))
  608. (buffer-string)))))
  609. ((and filename
  610. (not (equal (cdr (assq 'nofile cont)) "yes")))
  611. (let ((coding-system-for-read mm-binary-coding-system))
  612. (mm-insert-file-contents filename nil nil nil nil t))
  613. (unless charset
  614. (setq charset (mm-coding-system-to-mime-charset
  615. (mm-find-buffer-file-coding-system
  616. filename)))))
  617. (t
  618. (let ((contents (cdr (assq 'contents cont))))
  619. (if (if (featurep 'xemacs)
  620. (string-match "[^\000-\377]" contents)
  621. (mm-multibyte-string-p contents))
  622. (progn
  623. (mm-enable-multibyte)
  624. (insert contents)
  625. (unless raw
  626. (setq charset (mm-encode-body charset))))
  627. (insert contents)))))
  628. (if (setq encoding (cdr (assq 'encoding cont)))
  629. (setq encoding (intern (downcase encoding))))
  630. (setq encoding (mm-encode-buffer type encoding)
  631. coded (mm-string-as-multibyte (buffer-string))))
  632. (mml-insert-mime-headers cont type charset encoding nil)
  633. (insert "\n" coded))))
  634. ((eq (car cont) 'external)
  635. (insert "Content-Type: message/external-body")
  636. (let ((parameters (mml-parameter-string
  637. cont '(expiration size permission)))
  638. (name (cdr (assq 'name cont)))
  639. (url (cdr (assq 'url cont))))
  640. (when name
  641. (setq name (mml-parse-file-name name))
  642. (if (stringp name)
  643. (mml-insert-parameter
  644. (mail-header-encode-parameter "name" name)
  645. "access-type=local-file")
  646. (mml-insert-parameter
  647. (mail-header-encode-parameter
  648. "name" (file-name-nondirectory (nth 2 name)))
  649. (mail-header-encode-parameter "site" (nth 1 name))
  650. (mail-header-encode-parameter
  651. "directory" (file-name-directory (nth 2 name))))
  652. (mml-insert-parameter
  653. (concat "access-type="
  654. (if (member (nth 0 name) '("ftp@" "anonymous@"))
  655. "anon-ftp"
  656. "ftp")))))
  657. (when url
  658. (mml-insert-parameter
  659. (mail-header-encode-parameter "url" url)
  660. "access-type=url"))
  661. (when parameters
  662. (mml-insert-parameter-string
  663. cont '(expiration size permission)))
  664. (insert "\n\n")
  665. (insert "Content-Type: "
  666. (or (cdr (assq 'type cont))
  667. (if name
  668. (or (mm-default-file-encoding name)
  669. "application/octet-stream")
  670. "text/plain"))
  671. "\n")
  672. (insert "Content-ID: " (message-make-message-id) "\n")
  673. (insert "Content-Transfer-Encoding: "
  674. (or (cdr (assq 'encoding cont)) "binary"))
  675. (insert "\n\n")
  676. (insert (or (cdr (assq 'contents cont))))
  677. (insert "\n")))
  678. ((eq (car cont) 'multipart)
  679. (let* ((type (or (cdr (assq 'type cont)) "mixed"))
  680. (mml-generate-default-type (if (equal type "digest")
  681. "message/rfc822"
  682. "text/plain"))
  683. (handler (assoc type mml-generate-multipart-alist)))
  684. (if handler
  685. (funcall (cdr handler) cont)
  686. ;; No specific handler. Use default one.
  687. (let ((mml-boundary (mml-compute-boundary cont)))
  688. (insert (format "Content-Type: multipart/%s; boundary=\"%s\""
  689. type mml-boundary)
  690. (if (cdr (assq 'start cont))
  691. (format "; start=\"%s\"\n" (cdr (assq 'start cont)))
  692. "\n"))
  693. (let ((cont cont) part)
  694. (while (setq part (pop cont))
  695. ;; Skip `multipart' and attributes.
  696. (when (and (consp part) (consp (cdr part)))
  697. (insert "\n--" mml-boundary "\n")
  698. (mml-generate-mime-1 part)
  699. (goto-char (point-max)))))
  700. (insert "\n--" mml-boundary "--\n")))))
  701. (t
  702. (error "Invalid element: %S" cont)))
  703. ;; handle sign & encrypt tags in a semi-smart way.
  704. (let ((sign-item (assoc (cdr (assq 'sign cont)) mml-sign-alist))
  705. (encrypt-item (assoc (cdr (assq 'encrypt cont))
  706. mml-encrypt-alist))
  707. sender recipients)
  708. (when (or sign-item encrypt-item)
  709. (when (setq sender (cdr (assq 'sender cont)))
  710. (message-options-set 'mml-sender sender)
  711. (message-options-set 'message-sender sender))
  712. (if (setq recipients (cdr (assq 'recipients cont)))
  713. (message-options-set 'message-recipients recipients))
  714. (let ((style (mml-signencrypt-style
  715. (first (or sign-item encrypt-item)))))
  716. ;; check if: we're both signing & encrypting, both methods
  717. ;; are the same (why would they be different?!), and that
  718. ;; the signencrypt style allows for combined operation.
  719. (if (and sign-item encrypt-item (equal (first sign-item)
  720. (first encrypt-item))
  721. (equal style 'combined))
  722. (funcall (nth 1 encrypt-item) cont t)
  723. ;; otherwise, revert to the old behavior.
  724. (when sign-item
  725. (funcall (nth 1 sign-item) cont))
  726. (when encrypt-item
  727. (funcall (nth 1 encrypt-item) cont)))))))))
  728. (defun mml-compute-boundary (cont)
  729. "Return a unique boundary that does not exist in CONT."
  730. (let ((mml-boundary (funcall mml-boundary-function
  731. (incf mml-multipart-number))))
  732. (unless mml-inhibit-compute-boundary
  733. ;; This function tries again and again until it has found
  734. ;; a unique boundary.
  735. (while (not (catch 'not-unique
  736. (mml-compute-boundary-1 cont)))))
  737. mml-boundary))
  738. (defun mml-compute-boundary-1 (cont)
  739. (cond
  740. ((member (car cont) '(part mml))
  741. (mm-with-multibyte-buffer
  742. (let ((mml-inhibit-compute-boundary t)
  743. (mml-multipart-number 0)
  744. mml-sign-alist mml-encrypt-alist)
  745. (mml-generate-mime-1 cont))
  746. (goto-char (point-min))
  747. (when (re-search-forward (concat "^--" (regexp-quote mml-boundary))
  748. nil t)
  749. (setq mml-boundary (funcall mml-boundary-function
  750. (incf mml-multipart-number)))
  751. (throw 'not-unique nil))))
  752. ((eq (car cont) 'multipart)
  753. (mapc 'mml-compute-boundary-1 (cddr cont))))
  754. t)
  755. (defun mml-make-boundary (number)
  756. (concat (make-string (% number 60) ?=)
  757. (if (> number 17)
  758. (format "%x" number)
  759. "")
  760. mml-base-boundary))
  761. (defun mml-content-disposition (type &optional filename)
  762. "Return a default disposition name suitable to TYPE or FILENAME."
  763. (let ((defs mml-content-disposition-alist)
  764. disposition def types)
  765. (while (and (not disposition) defs)
  766. (setq def (pop defs))
  767. (cond ((stringp (car def))
  768. (when (and filename
  769. (string-match (car def) filename))
  770. (setq disposition (cdr def))))
  771. ((consp (cdr def))
  772. (when (string= (car (setq types (split-string type "/")))
  773. (car def))
  774. (setq type (cadr types)
  775. types (cdr def))
  776. (while (and (not disposition) types)
  777. (setq def (pop types))
  778. (when (or (eq (car def) t) (string= type (car def)))
  779. (setq disposition (cdr def))))))
  780. (t
  781. (when (or (eq (car def) t) (string= type (car def)))
  782. (setq disposition (cdr def))))))
  783. (or disposition "attachment")))
  784. (defun mml-insert-mime-headers (cont type charset encoding flowed)
  785. (let (parameters id disposition description)
  786. (setq parameters
  787. (mml-parameter-string
  788. cont mml-content-type-parameters))
  789. (when (or charset
  790. parameters
  791. flowed
  792. (not (equal type mml-generate-default-type))
  793. mml-insert-mime-headers-always)
  794. (when (consp charset)
  795. (error
  796. "Can't encode a part with several charsets"))
  797. (insert "Content-Type: " type)
  798. (when charset
  799. (mml-insert-parameter
  800. (mail-header-encode-parameter "charset" (symbol-name charset))))
  801. (when flowed
  802. (mml-insert-parameter "format=flowed"))
  803. (when parameters
  804. (mml-insert-parameter-string
  805. cont mml-content-type-parameters))
  806. (insert "\n"))
  807. (when (setq id (cdr (assq 'id cont)))
  808. (insert "Content-ID: " id "\n"))
  809. (setq parameters
  810. (mml-parameter-string
  811. cont mml-content-disposition-parameters))
  812. (when (or (setq disposition (cdr (assq 'disposition cont)))
  813. parameters)
  814. (insert "Content-Disposition: "
  815. (or disposition
  816. (mml-content-disposition type (cdr (assq 'filename cont)))))
  817. (when parameters
  818. (mml-insert-parameter-string
  819. cont mml-content-disposition-parameters))
  820. (insert "\n"))
  821. (unless (eq encoding '7bit)
  822. (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
  823. (when (setq description (cdr (assq 'description cont)))
  824. (insert "Content-Description: ")
  825. (setq description (prog1
  826. (point)
  827. (insert description "\n")))
  828. (mail-encode-encoded-word-region description (point)))))
  829. (defun mml-parameter-string (cont types)
  830. (let ((string "")
  831. value type)
  832. (while (setq type (pop types))
  833. (when (setq value (cdr (assq type cont)))
  834. ;; Strip directory component from the filename parameter.
  835. (when (eq type 'filename)
  836. (setq value (file-name-nondirectory value)))
  837. (setq string (concat string "; "
  838. (mail-header-encode-parameter
  839. (symbol-name type) value)))))
  840. (when (not (zerop (length string)))
  841. string)))
  842. (defun mml-insert-parameter-string (cont types)
  843. (let (value type)
  844. (while (setq type (pop types))
  845. (when (setq value (cdr (assq type cont)))
  846. ;; Strip directory component from the filename parameter.
  847. (when (eq type 'filename)
  848. (setq value (file-name-nondirectory value)))
  849. (mml-insert-parameter
  850. (mail-header-encode-parameter
  851. (symbol-name type) value))))))
  852. (defvar ange-ftp-name-format)
  853. (defvar efs-path-regexp)
  854. (defun mml-parse-file-name (path)
  855. (if (if (boundp 'efs-path-regexp)
  856. (string-match efs-path-regexp path)
  857. (if (boundp 'ange-ftp-name-format)
  858. (string-match (car ange-ftp-name-format) path)))
  859. (list (match-string 1 path) (match-string 2 path)
  860. (substring path (1+ (match-end 2))))
  861. path))
  862. (defun mml-insert-buffer (buffer)
  863. "Insert BUFFER at point and quote any MML markup."
  864. (save-restriction
  865. (narrow-to-region (point) (point))
  866. (insert-buffer-substring buffer)
  867. (mml-quote-region (point-min) (point-max))
  868. (goto-char (point-max))))
  869. ;;;
  870. ;;; Transforming MIME to MML
  871. ;;;
  872. ;; message-narrow-to-head autoloads message.
  873. (declare-function message-remove-header "message"
  874. (header &optional is-regexp first reverse))
  875. (defun mime-to-mml (&optional handles)
  876. "Translate the current buffer (which should be a message) into MML.
  877. If HANDLES is non-nil, use it instead reparsing the buffer."
  878. ;; First decode the head.
  879. (save-restriction
  880. (message-narrow-to-head)
  881. (let ((rfc2047-quote-decoded-words-containing-tspecials t))
  882. (mail-decode-encoded-word-region (point-min) (point-max))))
  883. (unless handles
  884. (setq handles (mm-dissect-buffer t)))
  885. (goto-char (point-min))
  886. (search-forward "\n\n" nil t)
  887. (delete-region (point) (point-max))
  888. (if (stringp (car handles))
  889. (mml-insert-mime handles)
  890. (mml-insert-mime handles t))
  891. (mm-destroy-parts handles)
  892. (save-restriction
  893. (message-narrow-to-head)
  894. ;; Remove them, they are confusing.
  895. (message-remove-header "Content-Type")
  896. (message-remove-header "MIME-Version")
  897. (message-remove-header "Content-Disposition")
  898. (message-remove-header "Content-Transfer-Encoding")))
  899. (autoload 'message-encode-message-body "message")
  900. (declare-function message-narrow-to-headers-or-head "message" ())
  901. ;;;###autoload
  902. (defun mml-to-mime ()
  903. "Translate the current buffer from MML to MIME."
  904. ;; `message-encode-message-body' will insert an encoded Content-Description
  905. ;; header in the message header if the body contains a single part
  906. ;; that is specified by a user with a MML tag containing a description
  907. ;; token. So, we encode the message header first to prevent the encoded
  908. ;; Content-Description header from being encoded again.
  909. (save-restriction
  910. (message-narrow-to-headers-or-head)
  911. ;; Skip past any From_ headers.
  912. (while (looking-at "From ")
  913. (forward-line 1))
  914. (let ((mail-parse-charset message-default-charset))
  915. (mail-encode-encoded-word-buffer)))
  916. (message-encode-message-body))
  917. (defun mml-insert-mime (handle &optional no-markup)
  918. (let (textp buffer mmlp)
  919. ;; Determine type and stuff.
  920. (unless (stringp (car handle))
  921. (unless (setq textp (equal (mm-handle-media-supertype handle) "text"))
  922. (with-current-buffer (setq buffer (mml-generate-new-buffer " *mml*"))
  923. (if (eq (mail-content-type-get (mm-handle-type handle) 'charset)
  924. 'gnus-decoded)
  925. ;; A part that mm-uu dissected from a non-MIME message
  926. ;; because of `gnus-article-emulate-mime'.
  927. (progn
  928. (mm-enable-multibyte)
  929. (insert-buffer-substring (mm-handle-buffer handle)))
  930. (mm-insert-part handle 'no-cache)
  931. (if (setq mmlp (equal (mm-handle-media-type handle)
  932. "message/rfc822"))
  933. (mime-to-mml))))))
  934. (if mmlp
  935. (mml-insert-mml-markup handle nil t t)
  936. (unless (and no-markup
  937. (equal (mm-handle-media-type handle) "text/plain"))
  938. (mml-insert-mml-markup handle buffer textp)))
  939. (cond
  940. (mmlp
  941. (insert-buffer-substring buffer)
  942. (goto-char (point-max))
  943. (insert "<#/mml>\n"))
  944. ((stringp (car handle))
  945. (mapc 'mml-insert-mime (cdr handle))
  946. (insert "<#/multipart>\n"))
  947. (textp
  948. (let ((charset (mail-content-type-get
  949. (mm-handle-type handle) 'charset))
  950. (start (point)))
  951. (if (eq charset 'gnus-decoded)
  952. (mm-insert-part handle)
  953. (insert (mm-decode-string (mm-get-part handle) charset)))
  954. (mml-quote-region start (point)))
  955. (goto-char (point-max)))
  956. (t
  957. (insert "<#/part>\n")))))
  958. (defun mml-insert-mml-markup (handle &optional buffer nofile mmlp)
  959. "Take a MIME handle and insert an MML tag."
  960. (if (stringp (car handle))
  961. (progn
  962. (insert "<#multipart type=" (mm-handle-media-subtype handle))
  963. (let ((start (mm-handle-multipart-ctl-parameter handle 'start)))
  964. (when start
  965. (insert " start=\"" start "\"")))
  966. (insert ">\n"))
  967. (if mmlp
  968. (insert "<#mml type=" (mm-handle-media-type handle))
  969. (insert "<#part type=" (mm-handle-media-type handle)))
  970. (dolist (elem (append (cdr (mm-handle-type handle))
  971. (cdr (mm-handle-disposition handle))))
  972. (unless (symbolp (cdr elem))
  973. (insert " " (symbol-name (car elem)) "=\"" (cdr elem) "\"")))
  974. (when (mm-handle-id handle)
  975. (insert " id=\"" (mm-handle-id handle) "\""))
  976. (when (mm-handle-disposition handle)
  977. (insert " disposition=" (car (mm-handle-disposition handle))))
  978. (when buffer
  979. (insert " buffer=\"" (buffer-name buffer) "\""))
  980. (when nofile
  981. (insert " nofile=yes"))
  982. (when (mm-handle-description handle)
  983. (insert " description=\"" (mm-handle-description handle) "\""))
  984. (insert ">\n")))
  985. (defun mml-insert-parameter (&rest parameters)
  986. "Insert PARAMETERS in a nice way."
  987. (let (start end)
  988. (dolist (param parameters)
  989. (insert ";")
  990. (setq start (point))
  991. (insert " " param)
  992. (setq end (point))
  993. (goto-char start)
  994. (end-of-line)
  995. (if (> (current-column) 76)
  996. (progn
  997. (goto-char start)
  998. (insert "\n")
  999. (goto-char (1+ end)))
  1000. (goto-char end)))))
  1001. ;;;
  1002. ;;; Mode for inserting and editing MML forms
  1003. ;;;
  1004. (defvar mml-mode-map
  1005. (let ((sign (make-sparse-keymap))
  1006. (encrypt (make-sparse-keymap))
  1007. (signpart (make-sparse-keymap))
  1008. (encryptpart (make-sparse-keymap))
  1009. (map (make-sparse-keymap))
  1010. (main (make-sparse-keymap)))
  1011. (define-key map "\C-s" 'mml-secure-message-sign)
  1012. (define-key map "\C-c" 'mml-secure-message-encrypt)
  1013. (define-key map "\C-e" 'mml-secure-message-sign-encrypt)
  1014. (define-key map "\C-p\C-s" 'mml-secure-sign)
  1015. (define-key map "\C-p\C-c" 'mml-secure-encrypt)
  1016. (define-key sign "p" 'mml-secure-message-sign-pgpmime)
  1017. (define-key sign "o" 'mml-secure-message-sign-pgp)
  1018. (define-key sign "s" 'mml-secure-message-sign-smime)
  1019. (define-key signpart "p" 'mml-secure-sign-pgpmime)
  1020. (define-key signpart "o" 'mml-secure-sign-pgp)
  1021. (define-key signpart "s" 'mml-secure-sign-smime)
  1022. (define-key encrypt "p" 'mml-secure-message-encrypt-pgpmime)
  1023. (define-key encrypt "o" 'mml-secure-message-encrypt-pgp)
  1024. (define-key encrypt "s" 'mml-secure-message-encrypt-smime)
  1025. (define-key encryptpart "p" 'mml-secure-encrypt-pgpmime)
  1026. (define-key encryptpart "o" 'mml-secure-encrypt-pgp)
  1027. (define-key encryptpart "s" 'mml-secure-encrypt-smime)
  1028. (define-key map "\C-n" 'mml-unsecure-message)
  1029. (define-key map "f" 'mml-attach-file)
  1030. (define-key map "b" 'mml-attach-buffer)
  1031. (define-key map "e" 'mml-attach-external)
  1032. (define-key map "q" 'mml-quote-region)
  1033. (define-key map "m" 'mml-insert-multipart)
  1034. (define-key map "p" 'mml-insert-part)
  1035. (define-key map "v" 'mml-validate)
  1036. (define-key map "P" 'mml-preview)
  1037. (define-key map "s" sign)
  1038. (define-key map "S" signpart)
  1039. (define-key map "c" encrypt)
  1040. (define-key map "C" encryptpart)
  1041. ;;(define-key map "n" 'mml-narrow-to-part)
  1042. ;; `M-m' conflicts with `back-to-indentation'.
  1043. ;; (define-key main "\M-m" map)
  1044. (define-key main "\C-c\C-m" map)
  1045. main))
  1046. (easy-menu-define
  1047. mml-menu mml-mode-map ""
  1048. `("Attachments"
  1049. ["Attach File..." mml-attach-file
  1050. ,@(if (featurep 'xemacs) '(t)
  1051. '(:help "Attach a file at point"))]
  1052. ["Attach Buffer..." mml-attach-buffer
  1053. ,@(if (featurep 'xemacs) '(t)
  1054. '(:help "Attach a buffer to the outgoing message"))]
  1055. ["Attach External..." mml-attach-external
  1056. ,@(if (featurep 'xemacs) '(t)
  1057. '(:help "Attach reference to an external file"))]
  1058. ;; FIXME: Is it possible to do this without using
  1059. ;; `gnus-gcc-externalize-attachments'?
  1060. ["Externalize Attachments"
  1061. (lambda ()
  1062. (interactive)
  1063. (if (not (and (boundp 'gnus-gcc-externalize-attachments)
  1064. (memq gnus-gcc-externalize-attachments
  1065. '(all t nil))))
  1066. ;; Stupid workaround for XEmacs not honoring :visible.
  1067. (message "Can't handle this value of `gnus-gcc-externalize-attachments'")
  1068. (setq gnus-gcc-externalize-attachments
  1069. (not gnus-gcc-externalize-attachments))
  1070. (message "gnus-gcc-externalize-attachments is `%s'."
  1071. gnus-gcc-externalize-attachments)))
  1072. ;; XEmacs barfs on :visible.
  1073. ,@(if (featurep 'xemacs) nil
  1074. '(:visible (and (boundp 'gnus-gcc-externalize-attachments)
  1075. (memq gnus-gcc-externalize-attachments
  1076. '(all t nil)))))
  1077. :style toggle
  1078. :selected gnus-gcc-externalize-attachments
  1079. ,@(if (featurep 'xemacs) nil
  1080. '(:help "Save attachments as external parts in Gcc copies"))]
  1081. "----"
  1082. ;;
  1083. ("Change Security Method"
  1084. ["PGP/MIME"
  1085. (lambda () (interactive) (setq mml-secure-method "pgpmime"))
  1086. ,@(if (featurep 'xemacs) nil
  1087. '(:help "Set Security Method to PGP/MIME"))
  1088. :style radio
  1089. :selected (equal mml-secure-method "pgpmime") ]
  1090. ["S/MIME"
  1091. (lambda () (interactive) (setq mml-secure-method "smime"))
  1092. ,@(if (featurep 'xemacs) nil
  1093. '(:help "Set Security Method to S/MIME"))
  1094. :style radio
  1095. :selected (equal mml-secure-method "smime") ]
  1096. ["Inline PGP"
  1097. (lambda () (interactive) (setq mml-secure-method "pgp"))
  1098. ,@(if (featurep 'xemacs) nil
  1099. '(:help "Set Security Method to inline PGP"))
  1100. :style radio
  1101. :selected (equal mml-secure-method "pgp") ] )
  1102. ;;
  1103. ["Sign Message" mml-secure-message-sign t]
  1104. ["Encrypt Message" mml-secure-message-encrypt t]
  1105. ["Sign and Encrypt Message" mml-secure-message-sign-encrypt t]
  1106. ["Encrypt/Sign off" mml-unsecure-message
  1107. ,@(if (featurep 'xemacs) '(t)
  1108. '(:help "Don't Encrypt/Sign Message"))]
  1109. ;; Do we have separate encrypt and encrypt/sign commands for parts?
  1110. ["Sign Part" mml-secure-sign t]
  1111. ["Encrypt Part" mml-secure-encrypt t]
  1112. "----"
  1113. ;; Maybe we could remove these, because people who write MML most probably
  1114. ;; don't use the menu:
  1115. ["Insert Part..." mml-insert-part
  1116. :active (message-in-body-p)]
  1117. ["Insert Multipart..." mml-insert-multipart
  1118. :active (message-in-body-p)]
  1119. ;;
  1120. ;;["Narrow" mml-narrow-to-part t]
  1121. ["Quote MML in region" mml-quote-region
  1122. :active (message-mark-active-p)
  1123. ,@(if (featurep 'xemacs) nil
  1124. '(:help "Quote MML tags in region"))]
  1125. ["Validate MML" mml-validate t]
  1126. ["Preview" mml-preview t]
  1127. "----"
  1128. ["Emacs MIME manual" (lambda () (interactive) (message-info 4))
  1129. ,@(if (featurep 'xemacs) '(t)
  1130. '(:help "Display the Emacs MIME manual"))]
  1131. ["PGG manual" (lambda () (interactive) (message-info mml2015-use))
  1132. ;; XEmacs barfs on :visible.
  1133. ,@(if (featurep 'xemacs) nil
  1134. '(:visible (and (boundp 'mml2015-use) (equal mml2015-use 'pgg))))
  1135. ,@(if (featurep 'xemacs) '(t)
  1136. '(:help "Display the PGG manual"))]
  1137. ["EasyPG manual" (lambda () (interactive) (require 'mml2015) (message-info mml2015-use))
  1138. ;; XEmacs barfs on :visible.
  1139. ,@(if (featurep 'xemacs) nil
  1140. '(:visible (and (boundp 'mml2015-use) (equal mml2015-use 'epg))))
  1141. ,@(if (featurep 'xemacs) '(t)
  1142. '(:help "Display the EasyPG manual"))]))
  1143. (define-minor-mode mml-mode
  1144. "Minor mode for editing MML.
  1145. MML is the MIME Meta Language, a minor mode for composing MIME articles.
  1146. See Info node `(emacs-mime)Composing'.
  1147. \\{mml-mode-map}"
  1148. :lighter " MML" :keymap mml-mode-map
  1149. (when mml-mode
  1150. (easy-menu-add mml-menu mml-mode-map)
  1151. (when (boundp 'dnd-protocol-alist)
  1152. (set (make-local-variable 'dnd-protocol-alist)
  1153. (append mml-dnd-protocol-alist dnd-protocol-alist)))))
  1154. ;;;
  1155. ;;; Helper functions for reading MIME stuff from the minibuffer and
  1156. ;;; inserting stuff to the buffer.
  1157. ;;;
  1158. (defcustom mml-default-directory mm-default-directory
  1159. "The default directory where mml will find files.
  1160. If not set, `default-directory' will be used."
  1161. :type '(choice directory (const :tag "Default" nil))
  1162. :version "23.1" ;; No Gnus
  1163. :group 'message)
  1164. (defun mml-minibuffer-read-file (prompt)
  1165. (let* ((completion-ignored-extensions nil)
  1166. (file (read-file-name prompt
  1167. (or mml-default-directory default-directory)
  1168. nil t)))
  1169. ;; Prevent some common errors. This is inspired by similar code in
  1170. ;; VM.
  1171. (when (file-directory-p file)
  1172. (error "%s is a directory, cannot attach" file))
  1173. (unless (file-exists-p file)
  1174. (error "No such file: %s" file))
  1175. (unless (file-readable-p file)
  1176. (error "Permission denied: %s" file))
  1177. file))
  1178. (declare-function mailcap-parse-mimetypes "mailcap" (&optional path force))
  1179. (declare-function mailcap-mime-types "mailcap" ())
  1180. (defun mml-minibuffer-read-type (name &optional default)
  1181. (require 'mailcap)
  1182. (mailcap-parse-mimetypes)
  1183. (let* ((default (or default
  1184. (mm-default-file-encoding name)
  1185. ;; Perhaps here we should check what the file
  1186. ;; looks like, and offer text/plain if it looks
  1187. ;; like text/plain.
  1188. "application/octet-stream"))
  1189. (string (gnus-completing-read
  1190. "Content type"
  1191. (mailcap-mime-types)
  1192. nil nil nil default)))
  1193. (if (not (equal string ""))
  1194. string
  1195. default)))
  1196. (defun mml-minibuffer-read-description (&optional default)
  1197. (let ((description (read-string "One line description: " default)))
  1198. (when (string-match "\\`[ \t]*\\'" description)
  1199. (setq description nil))
  1200. description))
  1201. (defun mml-minibuffer-read-disposition (type &optional default filename)
  1202. (unless default
  1203. (setq default (mml-content-disposition type filename)))
  1204. (let ((disposition (gnus-completing-read
  1205. "Disposition"
  1206. '("attachment" "inline")
  1207. t nil nil default)))
  1208. (if (not (equal disposition ""))
  1209. disposition
  1210. default)))
  1211. (defun mml-quote-region (beg end)
  1212. "Quote the MML tags in the region."
  1213. (interactive "r")
  1214. (save-excursion
  1215. (save-restriction
  1216. ;; Temporarily narrow the region to defend from changes
  1217. ;; invalidating END.
  1218. (narrow-to-region beg end)
  1219. (goto-char (point-min))
  1220. ;; Quote parts.
  1221. (while (re-search-forward
  1222. "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)" nil t)
  1223. ;; Insert ! after the #.
  1224. (goto-char (+ (match-beginning 0) 2))
  1225. (insert "!")))))
  1226. (defun mml-insert-tag (name &rest plist)
  1227. "Insert an MML tag described by NAME and PLIST."
  1228. (when (symbolp name)
  1229. (setq name (symbol-name name)))
  1230. (insert "<#" name)
  1231. (while plist
  1232. (let ((key (pop plist))
  1233. (value (pop plist)))
  1234. (when value
  1235. ;; Quote VALUE if it contains suspicious characters.
  1236. (when (string-match "[\"'\\~/*;() \t\n]" value)
  1237. (setq value (with-output-to-string
  1238. (let (print-escape-nonascii)
  1239. (prin1 value)))))
  1240. (insert (format " %s=%s" key value)))))
  1241. (insert ">\n"))
  1242. (defun mml-insert-empty-tag (name &rest plist)
  1243. "Insert an empty MML tag described by NAME and PLIST."
  1244. (when (symbolp name)
  1245. (setq name (symbol-name name)))
  1246. (apply #'mml-insert-tag name plist)
  1247. (insert "<#/" name ">\n"))
  1248. ;;; Attachment functions.
  1249. (defcustom mml-dnd-protocol-alist
  1250. '(("^file:///" . mml-dnd-attach-file)
  1251. ("^file://" . dnd-open-file)
  1252. ("^file:" . mml-dnd-attach-file))
  1253. "The functions to call when a drop in `mml-mode' is made.
  1254. See `dnd-protocol-alist' for more information. When nil, behave
  1255. as in other buffers."
  1256. :type '(choice (repeat (cons (regexp) (function)))
  1257. (const :tag "Behave as in other buffers" nil))
  1258. :version "22.1" ;; Gnus 5.10.9
  1259. :group 'message)
  1260. (defcustom mml-dnd-attach-options nil
  1261. "Which options should be queried when attaching a file via drag and drop.
  1262. If it is a list, valid members are `type', `description' and
  1263. `disposition'. `disposition' implies `type'. If it is nil,
  1264. don't ask for options. If it is t, ask the user whether or not
  1265. to specify options."
  1266. :type '(choice
  1267. (const :tag "None" nil)
  1268. (const :tag "Query" t)
  1269. (list :value (type description disposition)
  1270. (set :inline t
  1271. (const type)
  1272. (const description)
  1273. (const disposition))))
  1274. :version "22.1" ;; Gnus 5.10.9
  1275. :group 'message)
  1276. ;;;###autoload
  1277. (defun mml-attach-file (file &optional type description disposition)
  1278. "Attach a file to the outgoing MIME message.
  1279. The file is not inserted or encoded until you send the message with
  1280. `\\[message-send-and-exit]' or `\\[message-send]' in Message mode,
  1281. or `\\[mail-send-and-exit]' or `\\[mail-send]' in Mail mode.
  1282. FILE is the name of the file to attach. TYPE is its
  1283. content-type, a string of the form \"type/subtype\". DESCRIPTION
  1284. is a one-line description of the attachment. The DISPOSITION
  1285. specifies how the attachment is intended to be displayed. It can
  1286. be either \"inline\" (displayed automatically within the message
  1287. body) or \"attachment\" (separate from the body)."
  1288. (interactive
  1289. (let* ((file (mml-minibuffer-read-file "Attach file: "))
  1290. (type (mml-minibuffer-read-type file))
  1291. (description (mml-minibuffer-read-description))
  1292. (disposition (mml-minibuffer-read-disposition type nil file)))
  1293. (list file type description disposition)))
  1294. ;; If in the message header, attach at the end and leave point unchanged.
  1295. (let ((head (unless (message-in-body-p) (point))))
  1296. (if head (goto-char (point-max)))
  1297. (mml-insert-empty-tag 'part
  1298. 'type type
  1299. ;; icicles redefines read-file-name and returns a
  1300. ;; string w/ text properties :-/
  1301. 'filename (mm-substring-no-properties file)
  1302. 'disposition (or disposition "attachment")
  1303. 'description description)
  1304. ;; When using Mail mode, make sure it does the mime encoding
  1305. ;; when you send the message.
  1306. (or (eq mail-user-agent 'message-user-agent)
  1307. (setq mail-encode-mml t))
  1308. (when head
  1309. (unless (pos-visible-in-window-p)
  1310. (message "The file \"%s\" has been attached at the end of the message"
  1311. (file-name-nondirectory file)))
  1312. (goto-char head))))
  1313. (defun mml-dnd-attach-file (uri action)
  1314. "Attach a drag and drop file.
  1315. Ask for type, description or disposition according to
  1316. `mml-dnd-attach-options'."
  1317. (let ((file (dnd-get-local-file-name uri t)))
  1318. (when (and file (file-regular-p file))
  1319. (let ((mml-dnd-attach-options mml-dnd-attach-options)
  1320. type description disposition)
  1321. (setq mml-dnd-attach-options
  1322. (when (and (eq mml-dnd-attach-options t)
  1323. (not
  1324. (y-or-n-p
  1325. "Use default type, disposition and description? ")))
  1326. '(type description disposition)))
  1327. (when (or (memq 'type mml-dnd-attach-options)
  1328. (memq 'disposition mml-dnd-attach-options))
  1329. (setq type (mml-minibuffer-read-type file)))
  1330. (when (memq 'description mml-dnd-attach-options)
  1331. (setq description (mml-minibuffer-read-description)))
  1332. (when (memq 'disposition mml-dnd-attach-options)
  1333. (setq disposition (mml-minibuffer-read-disposition type nil file)))
  1334. (mml-attach-file file type description disposition)))))
  1335. (defun mml-attach-buffer (buffer &optional type description disposition)
  1336. "Attach a buffer to the outgoing MIME message.
  1337. BUFFER is the name of the buffer to attach. See
  1338. `mml-attach-file' for details of operation."
  1339. (interactive
  1340. (let* ((buffer (read-buffer "Attach buffer: "))
  1341. (type (mml-minibuffer-read-type buffer "text/plain"))
  1342. (description (mml-minibuffer-read-description))
  1343. (disposition (mml-minibuffer-read-disposition type nil)))
  1344. (list buffer type description disposition)))
  1345. ;; If in the message header, attach at the end and leave point unchanged.
  1346. (let ((head (unless (message-in-body-p) (point))))
  1347. (if head (goto-char (point-max)))
  1348. (mml-insert-empty-tag 'part 'type type 'buffer buffer
  1349. 'disposition disposition
  1350. 'description description)
  1351. ;; When using Mail mode, make sure it does the mime encoding
  1352. ;; when you send the message.
  1353. (or (eq mail-user-agent 'message-user-agent)
  1354. (setq mail-encode-mml t))
  1355. (when head
  1356. (unless (pos-visible-in-window-p)
  1357. (message
  1358. "The buffer \"%s\" has been attached at the end of the message"
  1359. buffer))
  1360. (goto-char head))))
  1361. (defun mml-attach-external (file &optional type description)
  1362. "Attach an external file into the buffer.
  1363. FILE is an ange-ftp/efs specification of the part location.
  1364. TYPE is the MIME type to use."
  1365. (interactive
  1366. (let* ((file (mml-minibuffer-read-file "Attach external file: "))
  1367. (type (mml-minibuffer-read-type file))
  1368. (description (mml-minibuffer-read-description)))
  1369. (list file type description)))
  1370. ;; If in the message header, attach at the end and leave point unchanged.
  1371. (let ((head (unless (message-in-body-p) (point))))
  1372. (if head (goto-char (point-max)))
  1373. (mml-insert-empty-tag 'external 'type type 'name file
  1374. 'disposition "attachment" 'description description)
  1375. ;; When using Mail mode, make sure it does the mime encoding
  1376. ;; when you send the message.
  1377. (or (eq mail-user-agent 'message-user-agent)
  1378. (setq mail-encode-mml t))
  1379. (when head
  1380. (unless (pos-visible-in-window-p)
  1381. (message "The file \"%s\" has been attached at the end of the message"
  1382. (file-name-nondirectory file)))
  1383. (goto-char head))))
  1384. (defun mml-insert-multipart (&optional type)
  1385. (interactive (if (message-in-body-p)
  1386. (list (gnus-completing-read "Multipart type"
  1387. '("mixed" "alternative"
  1388. "digest" "parallel"
  1389. "signed" "encrypted")
  1390. nil "mixed"))
  1391. (error "Use this command in the message body")))
  1392. (or type
  1393. (setq type "mixed"))
  1394. (mml-insert-empty-tag "multipart" 'type type)
  1395. ;; When using Mail mode, make sure it does the mime encoding
  1396. ;; when you send the message.
  1397. (or (eq mail-user-agent 'message-user-agent)
  1398. (setq mail-encode-mml t))
  1399. (forward-line -1))
  1400. (defun mml-insert-part (&optional type)
  1401. (interactive (if (message-in-body-p)
  1402. (list (mml-minibuffer-read-type ""))
  1403. (error "Use this command in the message body")))
  1404. ;; When using Mail mode, make sure it does the mime encoding
  1405. ;; when you send the message.
  1406. (or (eq mail-user-agent 'message-user-agent)
  1407. (setq mail-encode-mml t))
  1408. (mml-insert-tag 'part 'type type 'disposition "inline")
  1409. (save-excursion
  1410. (mml-insert-tag '/part)))
  1411. (declare-function message-subscribed-p "message" ())
  1412. (declare-function message-make-mail-followup-to "message"
  1413. (&optional only-show-subscribed))
  1414. (declare-function message-position-on-field "message" (header &rest afters))
  1415. (defun mml-preview-insert-mail-followup-to ()
  1416. "Insert a Mail-Followup-To header before previewing an article.
  1417. Should be adopted if code in `message-send-mail' is changed."
  1418. (when (and (message-mail-p)
  1419. (message-subscribed-p)
  1420. (not (mail-fetch-field "mail-followup-to"))
  1421. (message-make-mail-followup-to))
  1422. (message-position-on-field "Mail-Followup-To" "X-Draft-From")
  1423. (insert (message-make-mail-followup-to))))
  1424. (defvar mml-preview-buffer nil)
  1425. (autoload 'gnus-make-hashtable "gnus-util")
  1426. (autoload 'widget-button-press "wid-edit" nil t)
  1427. (declare-function widget-event-point "wid-edit" (event))
  1428. ;; If gnus-buffer-configuration is bound this is loaded.
  1429. (declare-function gnus-configure-windows "gnus-win" (setting &optional force))
  1430. ;; Called after message-mail-p, which autoloads message.
  1431. (declare-function message-news-p "message" ())
  1432. (declare-function message-options-set-recipient "message" ())
  1433. (declare-function message-generate-headers "message" (headers))
  1434. (declare-function message-sort-headers "message" ())
  1435. (defun mml-preview (&optional raw)
  1436. "Display current buffer with Gnus, in a new buffer.
  1437. If RAW, display a raw encoded MIME message.
  1438. The window layout for the preview buffer is controlled by the variables
  1439. `special-display-buffer-names', `special-display-regexps', or
  1440. `gnus-buffer-configuration' (the first match made will be used),
  1441. or the `pop-to-buffer' function."
  1442. (interactive "P")
  1443. (setq mml-preview-buffer (generate-new-buffer
  1444. (concat (if raw "*Raw MIME preview of "
  1445. "*MIME preview of ") (buffer-name))))
  1446. (require 'gnus-msg) ; for gnus-setup-posting-charset
  1447. (save-excursion
  1448. (let* ((buf (current-buffer))
  1449. (article-editing (eq major-mode 'gnus-article-edit-mode))
  1450. (message-options message-options)
  1451. (message-this-is-mail (message-mail-p))
  1452. (message-this-is-news (message-news-p))
  1453. (message-posting-charset (or (gnus-setup-posting-charset
  1454. (save-restriction
  1455. (message-narrow-to-headers-or-head)
  1456. (message-fetch-field "Newsgroups")))
  1457. message-posting-charset)))
  1458. (message-options-set-recipient)
  1459. (when (boundp 'gnus-buffers)
  1460. (push mml-preview-buffer gnus-buffers))
  1461. (save-restriction
  1462. (widen)
  1463. (set-buffer mml-preview-buffer)
  1464. (erase-buffer)
  1465. (insert-buffer-substring buf))
  1466. (mml-preview-insert-mail-followup-to)
  1467. (let ((message-deletable-headers (if (message-news-p)
  1468. nil
  1469. message-deletable-headers))
  1470. (mail-header-separator (if article-editing
  1471. ""
  1472. mail-header-separator)))
  1473. (message-generate-headers
  1474. (copy-sequence (if (message-news-p)
  1475. message-required-news-headers
  1476. message-required-mail-headers)))
  1477. (unless article-editing
  1478. (if (re-search-forward
  1479. (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
  1480. (replace-match "\n"))
  1481. (setq mail-header-separator ""))
  1482. (message-sort-headers)
  1483. (mml-to-mime))
  1484. (if raw
  1485. (when (fboundp 'set-buffer-multibyte)
  1486. (let ((s (buffer-string)))
  1487. ;; Insert the content into unibyte buffer.
  1488. (erase-buffer)
  1489. (mm-disable-multibyte)
  1490. (insert s)))
  1491. (let ((gnus-newsgroup-charset (car message-posting-charset))
  1492. gnus-article-prepare-hook gnus-original-article-buffer
  1493. gnus-displaying-mime)
  1494. (run-hooks 'gnus-article-decode-hook)
  1495. (let ((gnus-newsgroup-name "dummy")
  1496. (gnus-newsrc-hashtb (or gnus-newsrc-hashtb
  1497. (gnus-make-hashtable 5))))
  1498. (gnus-article-prepare-display))))
  1499. ;; Disable article-mode-map.
  1500. (use-local-map nil)
  1501. (gnus-make-local-hook 'kill-buffer-hook)
  1502. (add-hook 'kill-buffer-hook
  1503. (lambda ()
  1504. (mm-destroy-parts gnus-article-mime-handles)) nil t)
  1505. (setq buffer-read-only t)
  1506. (local-set-key "q" (lambda () (interactive) (kill-buffer nil)))
  1507. (local-set-key "=" (lambda () (interactive) (delete-other-windows)))
  1508. (local-set-key "\r"
  1509. (lambda ()
  1510. (interactive)
  1511. (widget-button-press (point))))
  1512. (local-set-key gnus-mouse-2
  1513. (lambda (event)
  1514. (interactive "@e")
  1515. (widget-button-press (widget-event-point event) event)))
  1516. ;; FIXME: Buffer is in article mode, but most tool bar commands won't
  1517. ;; work. Maybe only keep the following icons: search, print, quit
  1518. (goto-char (point-min))))
  1519. (if (and (not (mm-special-display-p (buffer-name mml-preview-buffer)))
  1520. (boundp 'gnus-buffer-configuration)
  1521. (assq 'mml-preview gnus-buffer-configuration))
  1522. (let ((gnus-message-buffer (current-buffer)))
  1523. (gnus-configure-windows 'mml-preview))
  1524. (pop-to-buffer mml-preview-buffer)))
  1525. (defun mml-validate ()
  1526. "Validate the current MML document."
  1527. (interactive)
  1528. (mml-parse))
  1529. (defun mml-tweak-part (cont)
  1530. "Tweak a MML part."
  1531. (let ((tweak (cdr (assq 'tweak cont)))
  1532. func)
  1533. (cond
  1534. (tweak
  1535. (setq func
  1536. (or (cdr (assoc tweak mml-tweak-function-alist))
  1537. (intern tweak))))
  1538. (mml-tweak-type-alist
  1539. (let ((alist mml-tweak-type-alist)
  1540. (type (or (cdr (assq 'type cont)) "text/plain")))
  1541. (while alist
  1542. (if (string-match (caar alist) type)
  1543. (setq func (cdar alist)
  1544. alist nil)
  1545. (setq alist (cdr alist)))))))
  1546. (if func
  1547. (funcall func cont)
  1548. cont)
  1549. (let ((alist mml-tweak-sexp-alist))
  1550. (while alist
  1551. (if (eval (caar alist))
  1552. (funcall (cdar alist) cont))
  1553. (setq alist (cdr alist)))))
  1554. cont)
  1555. (defun mml-tweak-externalize-attachments (cont)
  1556. "Tweak attached files as external parts."
  1557. (let (filename-cons)
  1558. (when (and (eq (car cont) 'part)
  1559. (not (cdr (assq 'buffer cont)))
  1560. (and (setq filename-cons (assq 'filename cont))
  1561. (not (equal (cdr (assq 'nofile cont)) "yes"))))
  1562. (setcar cont 'external)
  1563. (setcar filename-cons 'name))))
  1564. (provide 'mml)
  1565. ;;; mml.el ends here