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