rfc2047.el 40 KB

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