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