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