mml.el 57 KB

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