rfc2047.el 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
  18. ;; Three: Message Header Extensions for Non-ASCII Text".
  19. ;;; Code:
  20. (eval-when-compile
  21. (require 'cl))
  22. (defvar message-posting-charset)
  23. (require 'mm-util)
  24. (require 'ietf-drums)
  25. ;; Fixme: Avoid this (used for mail-parse-charset) mm dependence on gnus.
  26. (require 'mail-prsvr)
  27. (require 'rfc2045) ;; rfc2045-encode-string
  28. (autoload 'mm-body-7-or-8 "mm-bodies")
  29. (defvar rfc2047-header-encoding-alist
  30. '(("Newsgroups" . nil)
  31. ("Followup-To" . nil)
  32. ("Message-ID" . nil)
  33. ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|\\(In-\\)?Reply-To\\|Sender\
  34. \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\)" . address-mime)
  35. (t . mime))
  36. "*Header/encoding method alist.
  37. The list is traversed sequentially. The keys can either be
  38. header regexps or t.
  39. The values can be:
  40. 1) nil, in which case no encoding is done;
  41. 2) `mime', in which case the header will be encoded according to RFC2047;
  42. 3) `address-mime', like `mime', but takes account of the rules for address
  43. fields (where quoted strings and comments must be treated separately);
  44. 4) a charset, in which case it will be encoded as that charset;
  45. 5) `default', in which case the field will be encoded as the rest
  46. of the article.")
  47. (defvar rfc2047-charset-encoding-alist
  48. '((us-ascii . nil)
  49. (iso-8859-1 . Q)
  50. (iso-8859-2 . Q)
  51. (iso-8859-3 . Q)
  52. (iso-8859-4 . Q)
  53. (iso-8859-5 . B)
  54. (koi8-r . B)
  55. (iso-8859-7 . B)
  56. (iso-8859-8 . B)
  57. (iso-8859-9 . Q)
  58. (iso-8859-14 . Q)
  59. (iso-8859-15 . Q)
  60. (iso-2022-jp . B)
  61. (iso-2022-kr . B)
  62. (gb2312 . B)
  63. (gbk . B)
  64. (gb18030 . B)
  65. (big5 . B)
  66. (cn-big5 . B)
  67. (cn-gb . B)
  68. (cn-gb-2312 . B)
  69. (euc-kr . B)
  70. (iso-2022-jp-2 . B)
  71. (iso-2022-int-1 . B)
  72. (viscii . Q))
  73. "Alist of MIME charsets to RFC2047 encodings.
  74. Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
  75. quoted-printable and base64 respectively.")
  76. (defvar rfc2047-encode-function-alist
  77. '((Q . rfc2047-q-encode-string)
  78. (B . rfc2047-b-encode-string)
  79. (nil . identity))
  80. "Alist of RFC2047 encodings to encoding functions.")
  81. (defvar rfc2047-encode-encoded-words t
  82. "Whether encoded words should be encoded again.")
  83. (defvar rfc2047-allow-irregular-q-encoded-words t
  84. "*Whether to decode irregular Q-encoded words.")
  85. (eval-and-compile ;; Necessary to hard code them in `rfc2047-decode-region'.
  86. (defconst rfc2047-encoded-word-regexp
  87. "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\\?\
  88. \\(B\\?[+/0-9A-Za-z]*=*\
  89. \\|Q\\?[ ->@-~]*\
  90. \\)\\?="
  91. "Regexp that matches encoded word."
  92. ;; The patterns for the B encoding and the Q encoding, i.e. the ones
  93. ;; beginning with "B" and "Q" respectively, are restricted into only
  94. ;; the characters that those encodings may generally use.
  95. )
  96. (defconst rfc2047-encoded-word-regexp-loose
  97. "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\\?\
  98. \\(B\\?[+/0-9A-Za-z]*=*\
  99. \\|Q\\?\\(?:\\?+[ -<>@-~]\\)?\\(?:[ ->@-~]+\\?+[ -<>@-~]\\)*[ ->@-~]*\\?*\
  100. \\)\\?="
  101. "Regexp that matches encoded word allowing loose Q encoding."
  102. ;; The pattern for the Q encoding, i.e. the one beginning with "Q",
  103. ;; is similar to:
  104. ;; "Q\\?\\(\\?+[^\n=?]\\)?\\([^\n?]+\\?+[^\n=?]\\)*[^\n?]*\\?*"
  105. ;; <--------1-------><----------2,3----------><--4--><-5->
  106. ;; They mean:
  107. ;; 1. After "Q?", allow "?"s that follow a character other than "=".
  108. ;; 2. Allow "=" after "Q?"; it isn't regarded as the terminator.
  109. ;; 3. In the middle of an encoded word, allow "?"s that follow a
  110. ;; character other than "=".
  111. ;; 4. Allow any characters other than "?" in the middle of an
  112. ;; encoded word.
  113. ;; 5. At the end, allow "?"s.
  114. ))
  115. ;;;
  116. ;;; Functions for encoding RFC2047 messages
  117. ;;;
  118. (defun rfc2047-qp-or-base64 ()
  119. "Return the type with which to encode the buffer.
  120. This is either `base64' or `quoted-printable'."
  121. (save-excursion
  122. (let ((limit (min (point-max) (+ 2000 (point-min))))
  123. (n8bit 0))
  124. (goto-char (point-min))
  125. (skip-chars-forward "\x20-\x7f\r\n\t" limit)
  126. (while (< (point) limit)
  127. (incf n8bit)
  128. (forward-char 1)
  129. (skip-chars-forward "\x20-\x7f\r\n\t" limit))
  130. (if (or (< (* 6 n8bit) (- limit (point-min)))
  131. ;; Don't base64, say, a short line with a single
  132. ;; non-ASCII char when splitting parts by charset.
  133. (= n8bit 1))
  134. 'quoted-printable
  135. 'base64))))
  136. (defun rfc2047-narrow-to-field ()
  137. "Narrow the buffer to the header on the current line."
  138. (beginning-of-line)
  139. (narrow-to-region
  140. (point)
  141. (progn
  142. (forward-line 1)
  143. (if (re-search-forward "^[^ \n\t]" nil t)
  144. (point-at-bol)
  145. (point-max))))
  146. (goto-char (point-min)))
  147. (defun rfc2047-field-value ()
  148. "Return the value of the field at point."
  149. (save-excursion
  150. (save-restriction
  151. (rfc2047-narrow-to-field)
  152. (re-search-forward ":[ \t\n]*" nil t)
  153. (buffer-substring-no-properties (point) (point-max)))))
  154. (defun rfc2047-quote-special-characters-in-quoted-strings (&optional
  155. encodable-regexp)
  156. "Quote special characters with `\\'s in quoted strings.
  157. Quoting will not be done in a quoted string if it contains characters
  158. matching ENCODABLE-REGEXP or it is within parentheses."
  159. (goto-char (point-min))
  160. (let ((tspecials (concat "[" ietf-drums-tspecials "]"))
  161. (start (point))
  162. beg end)
  163. (with-syntax-table (standard-syntax-table)
  164. (while (not (eobp))
  165. (if (ignore-errors
  166. (forward-list 1)
  167. (eq (char-before) ?\)))
  168. (forward-list -1)
  169. (goto-char (point-max)))
  170. (save-restriction
  171. (narrow-to-region start (point))
  172. (goto-char start)
  173. (while (search-forward "\"" nil t)
  174. (setq beg (match-beginning 0))
  175. (unless (eq (char-before beg) ?\\)
  176. (goto-char beg)
  177. (setq beg (1+ beg))
  178. (condition-case nil
  179. (progn
  180. (forward-sexp)
  181. (setq end (1- (point)))
  182. (goto-char beg)
  183. (if (and encodable-regexp
  184. (re-search-forward encodable-regexp end t))
  185. (goto-char (1+ end))
  186. (save-restriction
  187. (narrow-to-region beg end)
  188. (while (re-search-forward tspecials nil 'move)
  189. (if (eq (char-before) ?\\)
  190. (if (looking-at tspecials) ;; Already quoted.
  191. (forward-char)
  192. (insert "\\"))
  193. (goto-char (match-beginning 0))
  194. (insert "\\")
  195. (forward-char))))
  196. (forward-char)))
  197. (error
  198. (goto-char beg)))))
  199. (goto-char (point-max)))
  200. (forward-list 1)
  201. (setq start (point))))))
  202. (defvar rfc2047-encoding-type 'address-mime
  203. "The type of encoding done by `rfc2047-encode-region'.
  204. This should be dynamically bound around calls to
  205. `rfc2047-encode-region' to either `mime' or `address-mime'. See
  206. `rfc2047-header-encoding-alist', for definitions.")
  207. (defun rfc2047-encode-message-header ()
  208. "Encode the message header according to `rfc2047-header-encoding-alist'.
  209. Should be called narrowed to the head of the message."
  210. (interactive "*")
  211. (save-excursion
  212. (goto-char (point-min))
  213. (let (alist elem method)
  214. (while (not (eobp))
  215. (save-restriction
  216. (rfc2047-narrow-to-field)
  217. (setq method nil
  218. alist rfc2047-header-encoding-alist)
  219. (while (setq elem (pop alist))
  220. (when (or (and (stringp (car elem))
  221. (looking-at (car elem)))
  222. (eq (car elem) t))
  223. (setq alist nil
  224. method (cdr elem))))
  225. (if (not (rfc2047-encodable-p))
  226. (prog2
  227. (when (eq method 'address-mime)
  228. (rfc2047-quote-special-characters-in-quoted-strings))
  229. (if (and (eq (mm-body-7-or-8) '8bit)
  230. (mm-multibyte-p)
  231. (mm-coding-system-p
  232. (car message-posting-charset)))
  233. ;; 8 bit must be decoded.
  234. (mm-encode-coding-region
  235. (point-min) (point-max)
  236. (mm-charset-to-coding-system
  237. (car message-posting-charset))))
  238. ;; No encoding necessary, but folding is nice
  239. (when nil
  240. (rfc2047-fold-region
  241. (save-excursion
  242. (goto-char (point-min))
  243. (skip-chars-forward "^:")
  244. (when (looking-at ": ")
  245. (forward-char 2))
  246. (point))
  247. (point-max))))
  248. ;; We found something that may perhaps be encoded.
  249. (re-search-forward "^[^:]+: *" nil t)
  250. (cond
  251. ((eq method 'address-mime)
  252. (rfc2047-encode-region (point) (point-max)))
  253. ((eq method 'mime)
  254. (let ((rfc2047-encoding-type 'mime))
  255. (rfc2047-encode-region (point) (point-max))))
  256. ((eq method 'default)
  257. (if (and (featurep 'mule)
  258. (if (boundp 'enable-multibyte-characters)
  259. (default-value 'enable-multibyte-characters))
  260. mail-parse-charset)
  261. (mm-encode-coding-region (point) (point-max)
  262. mail-parse-charset)))
  263. ;; We get this when CC'ing messages to newsgroups with
  264. ;; 8-bit names. The group name mail copy just got
  265. ;; unconditionally encoded. Previously, it would ask
  266. ;; whether to encode, which was quite confusing for the
  267. ;; user. If the new behavior is wrong, tell me. I have
  268. ;; left the old code commented out below.
  269. ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-07.
  270. ;; Modified by Dave Love, with the commented-out code changed
  271. ;; in accordance with changes elsewhere.
  272. ((null method)
  273. (rfc2047-encode-region (point) (point-max)))
  274. ;;; ((null method)
  275. ;;; (if (or (message-options-get
  276. ;;; 'rfc2047-encode-message-header-encode-any)
  277. ;;; (message-options-set
  278. ;;; 'rfc2047-encode-message-header-encode-any
  279. ;;; (y-or-n-p
  280. ;;; "Some texts are not encoded. Encode anyway?")))
  281. ;;; (rfc2047-encode-region (point-min) (point-max))
  282. ;;; (error "Cannot send unencoded text")))
  283. ((mm-coding-system-p method)
  284. (if (or (and (featurep 'mule)
  285. (if (boundp 'enable-multibyte-characters)
  286. (default-value 'enable-multibyte-characters)))
  287. (featurep 'file-coding))
  288. (mm-encode-coding-region (point) (point-max) method)))
  289. ;; Hm.
  290. (t)))
  291. (goto-char (point-max)))))))
  292. ;; Fixme: This, and the require below may not be the Right Thing, but
  293. ;; should be safe just before release. -- fx 2001-02-08
  294. (defun rfc2047-encodable-p ()
  295. "Return non-nil if any characters in current buffer need encoding in headers.
  296. The buffer may be narrowed."
  297. (require 'message) ; for message-posting-charset
  298. (let ((charsets
  299. (mm-find-mime-charset-region (point-min) (point-max))))
  300. (goto-char (point-min))
  301. (or (and rfc2047-encode-encoded-words
  302. (prog1
  303. (re-search-forward rfc2047-encoded-word-regexp nil t)
  304. (goto-char (point-min))))
  305. (and charsets
  306. (not (equal charsets (list (car message-posting-charset))))))))
  307. ;; Use this syntax table when parsing into regions that may need
  308. ;; encoding. Double quotes are string delimiters, backslash is
  309. ;; character quoting, and all other RFC 2822 special characters are
  310. ;; treated as punctuation so we can use forward-sexp/forward-word to
  311. ;; skip to the end of regions appropriately. Nb. ietf-drums does
  312. ;; things differently.
  313. (defconst rfc2047-syntax-table
  314. ;; (make-char-table 'syntax-table '(2)) only works in Emacs.
  315. (let ((table (make-syntax-table)))
  316. ;; The following is done to work for setting all elements of the table;
  317. ;; it appears to be the cleanest way.
  318. ;; Play safe and don't assume the form of the word syntax entry --
  319. ;; copy it from ?a.
  320. (if (featurep 'xemacs)
  321. (put-char-table t (get-char-table ?a (standard-syntax-table)) table)
  322. (set-char-table-range table t (aref (standard-syntax-table) ?a)))
  323. (modify-syntax-entry ?\\ "\\" table)
  324. (modify-syntax-entry ?\" "\"" table)
  325. (modify-syntax-entry ?\( "(" table)
  326. (modify-syntax-entry ?\) ")" table)
  327. (modify-syntax-entry ?\< "." table)
  328. (modify-syntax-entry ?\> "." table)
  329. (modify-syntax-entry ?\[ "." table)
  330. (modify-syntax-entry ?\] "." table)
  331. (modify-syntax-entry ?: "." table)
  332. (modify-syntax-entry ?\; "." table)
  333. (modify-syntax-entry ?, "." table)
  334. (modify-syntax-entry ?@ "." table)
  335. table))
  336. (defun rfc2047-encode-region (b e &optional dont-fold)
  337. "Encode words in region B to E that need encoding.
  338. By default, the region is treated as containing RFC2822 addresses.
  339. Dynamically bind `rfc2047-encoding-type' to change that."
  340. (save-restriction
  341. (narrow-to-region b e)
  342. (let ((encodable-regexp (if rfc2047-encode-encoded-words
  343. "[^\000-\177]+\\|=\\?"
  344. "[^\000-\177]+"))
  345. start ; start of current token
  346. end begin csyntax
  347. ;; Whether there's an encoded word before the current token,
  348. ;; either immediately or separated by space.
  349. last-encoded
  350. (orig-text (buffer-substring-no-properties b e)))
  351. (if (eq 'mime rfc2047-encoding-type)
  352. ;; Simple case. Continuous words in which all those contain
  353. ;; non-ASCII characters are encoded collectively. Encoding
  354. ;; ASCII words, including `Re:' used in Subject headers, is
  355. ;; avoided for interoperability with non-MIME clients and
  356. ;; for making it easy to find keywords.
  357. (progn
  358. (goto-char (point-min))
  359. (while (progn (skip-chars-forward " \t\n")
  360. (not (eobp)))
  361. (setq start (point))
  362. (while (and (looking-at "[ \t\n]*\\([^ \t\n]+\\)")
  363. (progn
  364. (setq end (match-end 0))
  365. (re-search-forward encodable-regexp end t)))
  366. (goto-char end))
  367. (if (> (point) start)
  368. (rfc2047-encode start (point))
  369. (goto-char end))))
  370. ;; `address-mime' case -- take care of quoted words, comments.
  371. (rfc2047-quote-special-characters-in-quoted-strings encodable-regexp)
  372. (with-syntax-table rfc2047-syntax-table
  373. (goto-char (point-min))
  374. (condition-case err ; in case of unbalanced quotes
  375. ;; Look for rfc2822-style: sequences of atoms, quoted
  376. ;; strings, specials, whitespace. (Specials mustn't be
  377. ;; encoded.)
  378. (while (not (eobp))
  379. ;; Skip whitespace.
  380. (skip-chars-forward " \t\n")
  381. (setq start (point))
  382. (cond
  383. ((not (char-after))) ; eob
  384. ;; else token start
  385. ((eq ?\" (setq csyntax (char-syntax (char-after))))
  386. ;; Quoted word.
  387. (forward-sexp)
  388. (setq end (point))
  389. ;; Does it need encoding?
  390. (goto-char start)
  391. (if (re-search-forward encodable-regexp end 'move)
  392. ;; It needs encoding. Strip the quotes first,
  393. ;; since encoded words can't occur in quotes.
  394. (progn
  395. (goto-char end)
  396. (delete-char -1)
  397. (goto-char start)
  398. (delete-char 1)
  399. (when last-encoded
  400. ;; There was a preceding quoted word. We need
  401. ;; to include any separating whitespace in this
  402. ;; word to avoid it getting lost.
  403. (skip-chars-backward " \t")
  404. ;; A space is needed between the encoded words.
  405. (insert ? )
  406. (setq start (point)
  407. end (1+ end)))
  408. ;; Adjust the end position for the deleted quotes.
  409. (rfc2047-encode start (- end 2))
  410. (setq last-encoded t)) ; record that it was encoded
  411. (setq last-encoded nil)))
  412. ((eq ?. csyntax)
  413. ;; Skip other delimiters, but record that they've
  414. ;; potentially separated quoted words.
  415. (forward-char)
  416. (setq last-encoded nil))
  417. ((eq ?\) csyntax)
  418. (error "Unbalanced parentheses"))
  419. ((eq ?\( csyntax)
  420. ;; Look for the end of parentheses.
  421. (forward-list)
  422. ;; Encode text as an unstructured field.
  423. (let ((rfc2047-encoding-type 'mime))
  424. (rfc2047-encode-region (1+ start) (1- (point))))
  425. (skip-chars-forward ")"))
  426. (t ; normal token/whitespace sequence
  427. ;; Find the end.
  428. ;; Skip one ASCII word, or encode continuous words
  429. ;; in which all those contain non-ASCII characters.
  430. (setq end nil)
  431. (while (not (or end (eobp)))
  432. (when (looking-at "[\000-\177]+")
  433. (setq begin (point)
  434. end (match-end 0))
  435. (when (progn
  436. (while (and (or (re-search-forward
  437. "[ \t\n]\\|\\Sw" end 'move)
  438. (setq end nil))
  439. (eq ?\\ (char-syntax (char-before))))
  440. ;; Skip backslash-quoted characters.
  441. (forward-char))
  442. end)
  443. (setq end (match-beginning 0))
  444. (if rfc2047-encode-encoded-words
  445. (progn
  446. (goto-char begin)
  447. (when (search-forward "=?" end 'move)
  448. (goto-char (match-beginning 0))
  449. (setq end nil)))
  450. (goto-char end))))
  451. ;; Where the value nil of `end' means there may be
  452. ;; text to have to be encoded following the point.
  453. ;; Otherwise, the point reached to the end of ASCII
  454. ;; words separated by whitespace or a special char.
  455. (unless end
  456. (when (looking-at encodable-regexp)
  457. (goto-char (setq begin (match-end 0)))
  458. (while (and (looking-at "[ \t\n]+\\([^ \t\n]+\\)")
  459. (setq end (match-end 0))
  460. (progn
  461. (while (re-search-forward
  462. encodable-regexp end t))
  463. (< begin (point)))
  464. (goto-char begin)
  465. (or (not (re-search-forward "\\Sw" end t))
  466. (progn
  467. (goto-char (match-beginning 0))
  468. nil)))
  469. (goto-char end))
  470. (when (looking-at "[^ \t\n]+")
  471. (setq end (match-end 0))
  472. (if (re-search-forward "\\Sw+" end t)
  473. ;; There are special characters better
  474. ;; to be encoded so that MTAs may parse
  475. ;; them safely.
  476. (cond ((= end (point)))
  477. ((looking-at (concat "\\sw*\\("
  478. encodable-regexp
  479. "\\)"))
  480. (setq end nil))
  481. (t
  482. (goto-char (1- (match-end 0)))
  483. (unless (= (point) (match-beginning 0))
  484. ;; Separate encodable text and
  485. ;; delimiter.
  486. (insert " "))))
  487. (goto-char end)
  488. (skip-chars-forward " \t\n")
  489. (if (and (looking-at "[^ \t\n]+")
  490. (string-match encodable-regexp
  491. (match-string 0)))
  492. (setq end nil)
  493. (goto-char end)))))))
  494. (skip-chars-backward " \t\n")
  495. (setq end (point))
  496. (goto-char start)
  497. (if (re-search-forward encodable-regexp end 'move)
  498. (progn
  499. (unless (memq (char-before start) '(nil ?\t ? ))
  500. (if (progn
  501. (goto-char start)
  502. (skip-chars-backward "^ \t\n")
  503. (and (looking-at "\\Sw+")
  504. (= (match-end 0) start)))
  505. ;; Also encode bogus delimiters.
  506. (setq start (point))
  507. ;; Separate encodable text and delimiter.
  508. (goto-char start)
  509. (insert " ")
  510. (setq start (1+ start)
  511. end (1+ end))))
  512. (rfc2047-encode start end)
  513. (setq last-encoded t))
  514. (setq last-encoded nil)))))
  515. (error
  516. (if (or debug-on-quit debug-on-error)
  517. (signal (car err) (cdr err))
  518. (error "Invalid data for rfc2047 encoding: %s"
  519. (mm-replace-in-string orig-text "[ \t\n]+" " "))))))))
  520. (unless dont-fold
  521. (rfc2047-fold-region b (point)))
  522. (goto-char (point-max))))
  523. (defun rfc2047-encode-string (string &optional dont-fold)
  524. "Encode words in STRING.
  525. By default, the string is treated as containing addresses (see
  526. `rfc2047-encoding-type')."
  527. (mm-with-multibyte-buffer
  528. (insert string)
  529. (rfc2047-encode-region (point-min) (point-max) dont-fold)
  530. (buffer-string)))
  531. ;; From RFC 2047:
  532. ;; 2. Syntax of encoded-words
  533. ;; [...]
  534. ;; While there is no limit to the length of a multiple-line header
  535. ;; field, each line of a header field that contains one or more
  536. ;; 'encoded-word's is limited to 76 characters.
  537. ;;
  538. ;; In `rfc2047-encode-parameter' it is bound to nil, so don't defconst it.
  539. (defvar rfc2047-encode-max-chars 76
  540. "Maximum characters of each header line that contain encoded-words.
  541. According to RFC 2047, it is 76. If it is nil, encoded-words
  542. will not be folded. Too small value may cause an error. You
  543. should not change this value.")
  544. (defun rfc2047-encode-1 (column string cs encoder start crest tail
  545. &optional eword)
  546. "Subroutine used by `rfc2047-encode'."
  547. (cond ((string-equal string "")
  548. (or eword ""))
  549. ((not rfc2047-encode-max-chars)
  550. (concat start
  551. (funcall encoder (if cs
  552. (mm-encode-coding-string string cs)
  553. string))
  554. "?="))
  555. ((>= column rfc2047-encode-max-chars)
  556. (when eword
  557. (cond ((string-match "\n[ \t]+\\'" eword)
  558. ;; Remove a superfluous empty line.
  559. (setq eword (substring eword 0 (match-beginning 0))))
  560. ((string-match "(+\\'" eword)
  561. ;; Break the line before the open parenthesis.
  562. (setq crest (concat crest (match-string 0 eword))
  563. eword (substring eword 0 (match-beginning 0))))))
  564. (rfc2047-encode-1 (length crest) string cs encoder start " " tail
  565. (concat eword "\n" crest)))
  566. (t
  567. (let ((index 0)
  568. (limit (1- (length string)))
  569. (prev "")
  570. next len)
  571. (while (and prev
  572. (<= index limit))
  573. (setq next (concat start
  574. (funcall encoder
  575. (if cs
  576. (mm-encode-coding-string
  577. (substring string 0 (1+ index))
  578. cs)
  579. (substring string 0 (1+ index))))
  580. "?=")
  581. len (+ column (length next)))
  582. (if (> len rfc2047-encode-max-chars)
  583. (setq next prev
  584. prev nil)
  585. (if (or (< index limit)
  586. (<= (+ len (or (string-match "\n" tail)
  587. (length tail)))
  588. rfc2047-encode-max-chars))
  589. (setq prev next
  590. index (1+ index))
  591. (if (string-match "\\`)+" tail)
  592. ;; Break the line after the close parenthesis.
  593. (setq tail (concat (substring tail 0 (match-end 0))
  594. "\n "
  595. (substring tail (match-end 0)))
  596. prev next
  597. index (1+ index))
  598. (setq next prev
  599. prev nil)))))
  600. (if (> index limit)
  601. (concat eword next tail)
  602. (if (= 0 index)
  603. (if (and eword
  604. (string-match "(+\\'" eword))
  605. (setq crest (concat crest (match-string 0 eword))
  606. eword (substring eword 0 (match-beginning 0)))
  607. (setq eword (concat eword next)))
  608. (setq crest " "
  609. eword (concat eword next)))
  610. (when (string-match "\n[ \t]+\\'" eword)
  611. ;; Remove a superfluous empty line.
  612. (setq eword (substring eword 0 (match-beginning 0))))
  613. (rfc2047-encode-1 (length crest) (substring string index)
  614. cs encoder start " " tail
  615. (concat eword "\n" crest)))))))
  616. (defun rfc2047-encode (b e)
  617. "Encode the word(s) in the region B to E.
  618. Point moves to the end of the region."
  619. (let ((mime-charset (or (mm-find-mime-charset-region b e) (list 'us-ascii)))
  620. cs encoding tail crest eword)
  621. ;; Use utf-8 as a last resort if determining charset of text fails.
  622. (if (memq nil mime-charset)
  623. (setq mime-charset (list 'utf-8)))
  624. (cond ((> (length mime-charset) 1)
  625. (error "Can't rfc2047-encode `%s'"
  626. (buffer-substring-no-properties b e)))
  627. ((= (length mime-charset) 1)
  628. (setq mime-charset (car mime-charset)
  629. cs (mm-charset-to-coding-system mime-charset))
  630. (unless (and (mm-multibyte-p)
  631. (mm-coding-system-p cs))
  632. (setq cs nil))
  633. (save-restriction
  634. (narrow-to-region b e)
  635. (setq encoding
  636. (or (cdr (assq mime-charset
  637. rfc2047-charset-encoding-alist))
  638. ;; For the charsets that don't have a preferred
  639. ;; encoding, choose the one that's shorter.
  640. (if (eq (rfc2047-qp-or-base64) 'base64)
  641. 'B
  642. 'Q)))
  643. (widen)
  644. (goto-char e)
  645. (skip-chars-forward "^ \t\n")
  646. ;; `tail' may contain a close parenthesis.
  647. (setq tail (buffer-substring-no-properties e (point)))
  648. (goto-char b)
  649. (setq b (point-marker)
  650. e (set-marker (make-marker) e))
  651. (rfc2047-fold-region (point-at-bol) b)
  652. (goto-char b)
  653. (skip-chars-backward "^ \t\n")
  654. (unless (= 0 (skip-chars-backward " \t"))
  655. ;; `crest' may contain whitespace and an open parenthesis.
  656. (setq crest (buffer-substring-no-properties (point) b)))
  657. (setq eword (rfc2047-encode-1
  658. (- b (point-at-bol))
  659. (mm-replace-in-string
  660. (buffer-substring-no-properties b e)
  661. "\n\\([ \t]?\\)" "\\1")
  662. cs
  663. (or (cdr (assq encoding
  664. rfc2047-encode-function-alist))
  665. 'identity)
  666. (concat "=?" (downcase (symbol-name mime-charset))
  667. "?" (upcase (symbol-name encoding)) "?")
  668. (or crest " ")
  669. tail))
  670. (delete-region (if (eq (aref eword 0) ?\n)
  671. (if (bolp)
  672. ;; The line was folded before encoding.
  673. (1- (point))
  674. (point))
  675. (goto-char b))
  676. (+ e (length tail)))
  677. ;; `eword' contains `crest' and `tail'.
  678. (insert eword)
  679. (set-marker b nil)
  680. (set-marker e nil)
  681. (unless (or (/= 0 (length tail))
  682. (eobp)
  683. (looking-at "[ \t\n)]"))
  684. (insert " "))))
  685. (t
  686. (goto-char e)))))
  687. (defun rfc2047-fold-field ()
  688. "Fold the current header field."
  689. (save-excursion
  690. (save-restriction
  691. (rfc2047-narrow-to-field)
  692. (rfc2047-fold-region (point-min) (point-max)))))
  693. (defun rfc2047-fold-region (b e)
  694. "Fold long lines in region B to E."
  695. (save-restriction
  696. (narrow-to-region b e)
  697. (goto-char (point-min))
  698. (let ((break nil)
  699. (qword-break nil)
  700. (first t)
  701. (bol (save-restriction
  702. (widen)
  703. (point-at-bol))))
  704. (while (not (eobp))
  705. (when (and (or break qword-break)
  706. (> (- (point) bol) 76))
  707. (goto-char (or break qword-break))
  708. (setq break nil
  709. qword-break nil)
  710. (skip-chars-backward " \t")
  711. (if (looking-at "[ \t]")
  712. (insert ?\n)
  713. (insert "\n "))
  714. (setq bol (1- (point)))
  715. ;; Don't break before the first non-LWSP characters.
  716. (skip-chars-forward " \t")
  717. (unless (eobp)
  718. (forward-char 1)))
  719. (cond
  720. ((eq (char-after) ?\n)
  721. (forward-char 1)
  722. (setq bol (point)
  723. break nil
  724. qword-break nil)
  725. (skip-chars-forward " \t")
  726. (unless (or (eobp) (eq (char-after) ?\n))
  727. (forward-char 1)))
  728. ((eq (char-after) ?\r)
  729. (forward-char 1))
  730. ((memq (char-after) '(? ?\t))
  731. (skip-chars-forward " \t")
  732. (unless first ;; Don't break just after the header name.
  733. (setq break (point))))
  734. ((not break)
  735. (if (not (looking-at "=\\?[^=]"))
  736. (if (eq (char-after) ?=)
  737. (forward-char 1)
  738. (skip-chars-forward "^ \t\n\r="))
  739. ;; Don't break at the start of the field.
  740. (unless (= (point) b)
  741. (setq qword-break (point)))
  742. (skip-chars-forward "^ \t\n\r")))
  743. (t
  744. (skip-chars-forward "^ \t\n\r")))
  745. (setq first nil))
  746. (when (and (or break qword-break)
  747. (> (- (point) bol) 76))
  748. (goto-char (or break qword-break))
  749. (setq break nil
  750. qword-break nil)
  751. (if (or (> 0 (skip-chars-backward " \t"))
  752. (looking-at "[ \t]"))
  753. (insert ?\n)
  754. (insert "\n "))
  755. (setq bol (1- (point)))
  756. ;; Don't break before the first non-LWSP characters.
  757. (skip-chars-forward " \t")
  758. (unless (eobp)
  759. (forward-char 1))))))
  760. (defun rfc2047-unfold-field ()
  761. "Fold the current line."
  762. (save-excursion
  763. (save-restriction
  764. (rfc2047-narrow-to-field)
  765. (rfc2047-unfold-region (point-min) (point-max)))))
  766. (defun rfc2047-unfold-region (b e)
  767. "Unfold lines in region B to E."
  768. (save-restriction
  769. (narrow-to-region b e)
  770. (goto-char (point-min))
  771. (let ((bol (save-restriction
  772. (widen)
  773. (point-at-bol)))
  774. (eol (point-at-eol)))
  775. (forward-line 1)
  776. (while (not (eobp))
  777. (if (and (looking-at "[ \t]")
  778. (< (- (point-at-eol) bol) 76))
  779. (delete-region eol (progn
  780. (goto-char eol)
  781. (skip-chars-forward "\r\n")
  782. (point)))
  783. (setq bol (point-at-bol)))
  784. (setq eol (point-at-eol))
  785. (forward-line 1)))))
  786. (defun rfc2047-b-encode-string (string)
  787. "Base64-encode the header contained in STRING."
  788. (base64-encode-string string t))
  789. (autoload 'quoted-printable-encode-region "qp")
  790. (defun rfc2047-q-encode-string (string)
  791. "Quoted-printable-encode the header in STRING."
  792. (mm-with-unibyte-buffer
  793. (insert string)
  794. (quoted-printable-encode-region
  795. (point-min) (point-max) nil
  796. ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
  797. ;; Avoid using 8bit characters.
  798. ;; This list excludes `especials' (see the RFC2047 syntax),
  799. ;; meaning that some characters in non-structured fields will
  800. ;; get encoded when they con't need to be. The following is
  801. ;; what it used to be.
  802. ;;; ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
  803. ;;; "\010\012\014\040-\074\076\100-\136\140-\177")
  804. "-\b\n\f !#-'*+0-9A-Z\\^`-~\d")
  805. (subst-char-in-region (point-min) (point-max) ? ?_)
  806. (buffer-string)))
  807. (defun rfc2047-encode-parameter (param value)
  808. "Return and PARAM=VALUE string encoded in the RFC2047-like style.
  809. This is a substitution for the `rfc2231-encode-string' function, that
  810. is the standard but many mailers don't support it."
  811. (let ((rfc2047-encoding-type 'mime)
  812. (rfc2047-encode-max-chars nil))
  813. (rfc2045-encode-string param (rfc2047-encode-string value t))))
  814. ;;;
  815. ;;; Functions for decoding RFC2047 messages
  816. ;;;
  817. (defvar rfc2047-quote-decoded-words-containing-tspecials nil
  818. "If non-nil, quote decoded words containing special characters.")
  819. (defvar rfc2047-allow-incomplete-encoded-text t
  820. "*Non-nil means allow incomplete encoded-text in successive encoded-words.
  821. Dividing of encoded-text in the place other than character boundaries
  822. violates RFC2047 section 5, while we have a capability to decode it.
  823. If it is non-nil, the decoder will decode B- or Q-encoding in each
  824. encoded-word, concatenate them, and decode it by charset. Otherwise,
  825. the decoder will fully decode each encoded-word before concatenating
  826. them.")
  827. (defun rfc2047-strip-backslashes-in-quoted-strings ()
  828. "Strip backslashes in quoted strings. `\\\"' remains."
  829. (goto-char (point-min))
  830. (let (beg)
  831. (with-syntax-table (standard-syntax-table)
  832. (while (search-forward "\"" nil t)
  833. (unless (eq (char-before) ?\\)
  834. (setq beg (match-end 0))
  835. (goto-char (match-beginning 0))
  836. (condition-case nil
  837. (progn
  838. (forward-sexp)
  839. (save-restriction
  840. (narrow-to-region beg (1- (point)))
  841. (goto-char beg)
  842. (while (search-forward "\\" nil 'move)
  843. (unless (memq (char-after) '(?\"))
  844. (delete-char -1))
  845. (forward-char)))
  846. (forward-char))
  847. (error
  848. (goto-char beg))))))))
  849. (defun rfc2047-charset-to-coding-system (charset &optional allow-override)
  850. "Return coding-system corresponding to MIME CHARSET.
  851. If your Emacs implementation can't decode CHARSET, return nil.
  852. If allow-override is given, use `mm-charset-override-alist' to
  853. map undesired charset names to their replacement. This should
  854. only be used for decoding, not for encoding."
  855. (when (stringp charset)
  856. (setq charset (intern (downcase charset))))
  857. (when (or (not charset)
  858. (eq 'gnus-all mail-parse-ignored-charsets)
  859. (memq 'gnus-all mail-parse-ignored-charsets)
  860. (memq charset mail-parse-ignored-charsets))
  861. (setq charset mail-parse-charset))
  862. (let ((cs (mm-charset-to-coding-system charset nil allow-override)))
  863. (cond ((eq cs 'ascii)
  864. (setq cs (or (mm-charset-to-coding-system mail-parse-charset)
  865. 'raw-text)))
  866. ((mm-coding-system-p cs))
  867. ((and charset
  868. (listp mail-parse-ignored-charsets)
  869. (memq 'gnus-unknown mail-parse-ignored-charsets))
  870. (setq cs (mm-charset-to-coding-system mail-parse-charset))))
  871. (if (eq cs 'ascii)
  872. 'raw-text
  873. cs)))
  874. (autoload 'quoted-printable-decode-string "qp")
  875. (defun rfc2047-decode-encoded-words (words)
  876. "Decode successive encoded-words in WORDS and return a decoded string.
  877. Each element of WORDS looks like (CHARSET ENCODING ENCODED-TEXT
  878. ENCODED-WORD)."
  879. (let (word charset cs encoding text rest)
  880. (while words
  881. (setq word (pop words))
  882. (if (and (setq cs (rfc2047-charset-to-coding-system
  883. (setq charset (car word)) t))
  884. (condition-case code
  885. (cond ((char-equal ?B (nth 1 word))
  886. (setq text (base64-decode-string
  887. (rfc2047-pad-base64 (nth 2 word)))))
  888. ((char-equal ?Q (nth 1 word))
  889. (setq text (quoted-printable-decode-string
  890. (mm-subst-char-in-string
  891. ?_ ? (nth 2 word) t)))))
  892. (error
  893. (message "%s" (error-message-string code))
  894. nil)))
  895. (if (and rfc2047-allow-incomplete-encoded-text
  896. (eq cs (caar rest)))
  897. ;; Concatenate text of which the charset is the same.
  898. (setcdr (car rest) (concat (cdar rest) text))
  899. (push (cons cs text) rest))
  900. ;; Don't decode encoded-word.
  901. (push (cons nil (nth 3 word)) rest)))
  902. (while rest
  903. (setq words (concat
  904. (or (and (setq cs (caar rest))
  905. (condition-case code
  906. (mm-decode-coding-string (cdar rest) cs)
  907. (error
  908. (message "%s" (error-message-string code))
  909. nil)))
  910. (concat (when (cdr rest) " ")
  911. (cdar rest)
  912. (when (and words
  913. (not (eq (string-to-char words) ? )))
  914. " ")))
  915. words)
  916. rest (cdr rest)))
  917. words))
  918. ;; Fixme: This should decode in place, not cons intermediate strings.
  919. ;; Also check whether it needs to worry about delimiting fields like
  920. ;; encoding.
  921. ;; In fact it's reported that (invalid) encoding of mailboxes in
  922. ;; addr-specs is in use, so delimiting fields might help. Probably
  923. ;; not decoding a word which isn't properly delimited is good enough
  924. ;; and worthwhile (is it more correct or not?), e.g. something like
  925. ;; `=?iso-8859-1?q?foo?=@'.
  926. (defun rfc2047-decode-region (start end &optional address-mime)
  927. "Decode MIME-encoded words in region between START and END.
  928. If ADDRESS-MIME is non-nil, strip backslashes which precede characters
  929. other than `\"' and `\\' in quoted strings."
  930. (interactive "r")
  931. (let ((case-fold-search t)
  932. (eword-regexp
  933. (if rfc2047-allow-irregular-q-encoded-words
  934. (eval-when-compile
  935. (concat "[\n\t ]*\\(" rfc2047-encoded-word-regexp-loose "\\)"))
  936. (eval-when-compile
  937. (concat "[\n\t ]*\\(" rfc2047-encoded-word-regexp "\\)"))))
  938. b e match words)
  939. (save-excursion
  940. (save-restriction
  941. (narrow-to-region start end)
  942. (when address-mime
  943. (rfc2047-strip-backslashes-in-quoted-strings))
  944. (goto-char (setq b start))
  945. ;; Look for the encoded-words.
  946. (while (setq match (re-search-forward eword-regexp nil t))
  947. (setq e (match-beginning 1)
  948. end (match-end 0)
  949. words nil)
  950. (while match
  951. (push (list (match-string 2) ;; charset
  952. (char-after (match-beginning 3)) ;; encoding
  953. (substring (match-string 3) 2) ;; encoded-text
  954. (match-string 1)) ;; encoded-word
  955. words)
  956. ;; Look for the subsequent encoded-words.
  957. (when (setq match (looking-at eword-regexp))
  958. (goto-char (setq end (match-end 0)))))
  959. ;; Replace the encoded-words with the decoded one.
  960. (delete-region e end)
  961. (insert (rfc2047-decode-encoded-words (nreverse words)))
  962. (save-restriction
  963. (narrow-to-region e (point))
  964. (goto-char e)
  965. ;; Remove newlines between decoded words, though such
  966. ;; things essentially must not be there.
  967. (while (re-search-forward "[\n\r]+" nil t)
  968. (replace-match " "))
  969. (setq end (point-max))
  970. ;; Quote decoded words if there are special characters
  971. ;; which might violate RFC2822.
  972. (when (and rfc2047-quote-decoded-words-containing-tspecials
  973. (let ((regexp (car (rassq
  974. 'address-mime
  975. rfc2047-header-encoding-alist))))
  976. (when regexp
  977. (save-restriction
  978. (widen)
  979. (and
  980. ;; Don't quote words if already quoted.
  981. (not (and (eq (char-before e) ?\")
  982. (eq (char-after end) ?\")))
  983. (progn
  984. (beginning-of-line)
  985. (while (and (memq (char-after) '(? ?\t))
  986. (zerop (forward-line -1))))
  987. (looking-at regexp)))))))
  988. (let (quoted)
  989. (goto-char e)
  990. (skip-chars-forward " \t")
  991. (setq start (point))
  992. (setq quoted (eq (char-after) ?\"))
  993. (goto-char (point-max))
  994. (skip-chars-backward " \t" start)
  995. (if (setq quoted (and quoted
  996. (> (point) (1+ start))
  997. (eq (char-before) ?\")))
  998. (progn
  999. (backward-char)
  1000. (setq start (1+ start)
  1001. end (point-marker)))
  1002. (setq end (point-marker)))
  1003. (goto-char start)
  1004. (while (search-forward "\"" end t)
  1005. (when (prog2
  1006. (backward-char)
  1007. (zerop (% (skip-chars-backward "\\\\") 2))
  1008. (goto-char (match-beginning 0)))
  1009. (insert "\\"))
  1010. (forward-char))
  1011. (when (and (not quoted)
  1012. (progn
  1013. (goto-char start)
  1014. (re-search-forward
  1015. (concat "[" ietf-drums-tspecials "]")
  1016. end t)))
  1017. (goto-char start)
  1018. (insert "\"")
  1019. (goto-char end)
  1020. (insert "\""))
  1021. (set-marker end nil)))
  1022. (goto-char (point-max)))
  1023. (when (and (mm-multibyte-p)
  1024. mail-parse-charset
  1025. (not (eq mail-parse-charset 'us-ascii))
  1026. (not (eq mail-parse-charset 'gnus-decoded)))
  1027. (mm-decode-coding-region b e mail-parse-charset))
  1028. (setq b (point)))
  1029. (when (and (mm-multibyte-p)
  1030. mail-parse-charset
  1031. (not (eq mail-parse-charset 'us-ascii))
  1032. (not (eq mail-parse-charset 'gnus-decoded)))
  1033. (mm-decode-coding-region b (point-max) mail-parse-charset))))))
  1034. (defun rfc2047-decode-address-region (start end)
  1035. "Decode MIME-encoded words in region between START and END.
  1036. Backslashes which precede characters other than `\"' and `\\' in quoted
  1037. strings are stripped."
  1038. (rfc2047-decode-region start end t))
  1039. (defun rfc2047-decode-string (string &optional address-mime)
  1040. "Decode MIME-encoded STRING and return the result.
  1041. If ADDRESS-MIME is non-nil, strip backslashes which precede characters
  1042. other than `\"' and `\\' in quoted strings."
  1043. ;; (let ((m (mm-multibyte-p)))
  1044. (if (string-match "=\\?" string)
  1045. (with-temp-buffer
  1046. ;; We used to only call mm-enable-multibyte if `m' is non-nil,
  1047. ;; but this can't be the right criterion. Don't just revert this
  1048. ;; change if it encounters a bug. Please help me fix it
  1049. ;; right instead. --Stef
  1050. ;; The string returned should always be multibyte in a multibyte
  1051. ;; session, i.e. the buffer should be multibyte before
  1052. ;; `buffer-string' is called.
  1053. (mm-enable-multibyte)
  1054. (insert string)
  1055. (inline
  1056. (rfc2047-decode-region (point-min) (point-max) address-mime))
  1057. (buffer-string))
  1058. (when address-mime
  1059. (setq string
  1060. (with-temp-buffer
  1061. (when (mm-multibyte-string-p string)
  1062. (mm-enable-multibyte))
  1063. (insert string)
  1064. (rfc2047-strip-backslashes-in-quoted-strings)
  1065. (buffer-string))))
  1066. ;; Fixme: As above, `m' here is inappropriate.
  1067. (if (and ;; m
  1068. mail-parse-charset
  1069. (not (eq mail-parse-charset 'us-ascii))
  1070. (not (eq mail-parse-charset 'gnus-decoded)))
  1071. ;; `decode-coding-string' in Emacs offers a third optional
  1072. ;; arg NOCOPY to avoid consing a new string if the decoding
  1073. ;; is "trivial". Unfortunately it currently doesn't
  1074. ;; consider anything else than a `nil' coding system
  1075. ;; trivial.
  1076. ;; `rfc2047-decode-string' is called multiple times for each
  1077. ;; article during summary buffer generation, and we really
  1078. ;; want to avoid unnecessary consing. So we bypass
  1079. ;; `decode-coding-string' if the string is purely ASCII.
  1080. (if (and (fboundp 'detect-coding-string)
  1081. ;; string is purely ASCII
  1082. (eq (detect-coding-string string t) 'undecided))
  1083. string
  1084. (mm-decode-coding-string string mail-parse-charset))
  1085. (mm-string-to-multibyte string)))) ;; )
  1086. (defun rfc2047-decode-address-string (string)
  1087. "Decode MIME-encoded STRING and return the result.
  1088. Backslashes which precede characters other than `\"' and `\\' in quoted
  1089. strings are stripped."
  1090. (rfc2047-decode-string string t))
  1091. (defun rfc2047-pad-base64 (string)
  1092. "Pad STRING to quartets."
  1093. ;; Be more liberal to accept buggy base64 strings. If
  1094. ;; base64-decode-string accepts buggy strings, this function could
  1095. ;; be aliased to identity.
  1096. (if (= 0 (mod (length string) 4))
  1097. string
  1098. (when (string-match "=+$" string)
  1099. (setq string (substring string 0 (match-beginning 0))))
  1100. (case (mod (length string) 4)
  1101. (0 string)
  1102. (1 string) ;; Error, don't pad it.
  1103. (2 (concat string "=="))
  1104. (3 (concat string "=")))))
  1105. (provide 'rfc2047)
  1106. ;;; rfc2047.el ends here