(mail-quote-printable-region): New function.
[emacs.git] / lisp / mail / mail-utils.el
blobf939f19d06c5dea628729e4d00e37fc1d96bfe93
1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
3 ;; Copyright (C) 1985, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail, news
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Utility functions for mail and netnews handling. These handle fine
27 ;; points of header parsing.
29 ;;; Code:
31 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
32 ;;; been initialized.
33 (require 'lisp-mode)
35 ;;;###autoload
36 (defcustom mail-use-rfc822 nil "\
37 *If non-nil, use a full, hairy RFC822 parser on mail addresses.
38 Otherwise, (the default) use a smaller, somewhat faster, and
39 often correct parser."
40 :type 'boolean
41 :group 'mail)
43 ;; Returns t if file FILE is an Rmail file.
44 ;;;###autoload
45 (defun mail-file-babyl-p (file)
46 (let ((buf (generate-new-buffer " *rmail-file-p*")))
47 (unwind-protect
48 (save-excursion
49 (set-buffer buf)
50 (insert-file-contents file nil 0 100)
51 (looking-at "BABYL OPTIONS:"))
52 (kill-buffer buf))))
54 (defun mail-string-delete (string start end)
55 "Returns a string containing all of STRING except the part
56 from START (inclusive) to END (exclusive)."
57 (if (null end) (substring string 0 start)
58 (concat (substring string 0 start)
59 (substring string end nil))))
61 ;;;###autoload
62 (defun mail-quote-printable (string &optional wrapper)
63 "Convert a string to the \"quoted printable\" Q encoding.
64 If the optional argument WRAPPER is non-nil,
65 we add the wrapper characters =?ISO-8859-1?Q?....?=."
66 (let ((i 0) (result ""))
67 (save-match-data
68 (while (string-match "[?=\"\200-\377]" string i)
69 (setq result
70 (concat result (substring string i (match-beginning 0))
71 (upcase (format "=%02x"
72 (aref string (match-beginning 0))))))
73 (setq i (match-end 0)))
74 (if wrapper
75 (concat "=?ISO-8859-1?Q?"
76 result (substring string i)
77 "?=")
78 (concat result (substring string i))))))
80 ;;;###autoload
81 (defun mail-quote-printable-region (beg end &optional wrapper)
82 "Convert the region to the \"quoted printable\" Q encoding.
83 If the optional argument WRAPPER is non-nil,
84 we add the wrapper characters =?ISO-8859-1?Q?....?=."
85 (interactive "r\nP")
86 (save-match-data
87 (save-excursion
88 (goto-char beg)
89 (save-restriction
90 (narrow-to-region beg end)
91 (while (re-search-forward "[?=\"\200-\377]" nil t)
92 (replace-match (upcase (format "=%02x" (preceding-char)))
93 t t))
94 (when wrapper
95 (goto-char beg)
96 (insert "=?ISO-8859-1?Q?")
97 (goto-char end)
98 (insert "?="))))))
100 (defun mail-unquote-printable-hexdigit (char)
101 (setq char (upcase char))
102 (if (>= char ?A)
103 (+ (- char ?A) 10)
104 (- char ?0)))
106 ;;;###autoload
107 (defun mail-unquote-printable (string &optional wrapper)
108 "Undo the \"quoted printable\" encoding.
109 If the optional argument WRAPPER is non-nil,
110 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=."
111 (save-match-data
112 (and wrapper
113 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string)
114 (setq string (match-string 1 string)))
115 (let ((i 0) strings)
116 (while (string-match "=\\(..\\|\n\\)" string i)
117 (setq strings (cons (substring string i (match-beginning 0)) strings))
118 (unless (= (aref string (match-beginning 1)) ?\n)
119 (setq strings
120 (cons (make-string 1
121 (+ (* 16 (mail-unquote-printable-hexdigit
122 (aref string (match-beginning 1))))
123 (mail-unquote-printable-hexdigit
124 (aref string (1+ (match-beginning 1))))))
125 strings)))
126 (setq i (match-end 0)))
127 (apply 'concat (nreverse (cons (substring string i) strings))))))
129 ;;;###autoload
130 (defun mail-unquote-printable-region (beg end &optional wrapper noerror
131 unibyte)
132 "Undo the \"quoted printable\" encoding in buffer from BEG to END.
133 If the optional argument WRAPPER is non-nil,
134 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
135 If NOERROR is non-nil, return t if successful.
136 If UNIBYTE is non-nil, insert converted characters as unibyte.
137 That is useful if you are going to character code decoding afterward,
138 as Rmail does."
139 (interactive "r\nP")
140 (let (failed)
141 (save-match-data
142 (save-excursion
143 (save-restriction
144 (narrow-to-region beg end)
145 (goto-char (point-min))
146 (when (and wrapper
147 (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
148 (delete-region (match-end 1) end)
149 (delete-region (point) (match-beginning 1)))
150 (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t)
151 (goto-char (match-end 0))
152 (cond ((= (char-after (match-beginning 1)) ?\n)
153 (replace-match ""))
154 ((= (char-after (match-beginning 1)) ?=)
155 (replace-match "="))
156 ((match-beginning 2)
157 (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
158 (char-after (match-beginning 2))))
159 (mail-unquote-printable-hexdigit
160 (char-after (1+ (match-beginning 2)))))))
161 (if unibyte
162 (progn
163 (replace-match "")
164 ;; insert-byte will insert this as a
165 ;; corresponding eight-bit character.
166 (insert-byte char 1))
167 (replace-match (make-string 1 char) t t))))
168 (noerror
169 (setq failed t))
171 (error "Malformed MIME quoted-printable message"))))
172 (not failed))))))
174 (eval-when-compile (require 'rfc822))
176 (defun mail-strip-quoted-names (address)
177 "Delete comments and quoted strings in an address list ADDRESS.
178 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
179 Return a modified address list."
180 (if (null address)
182 (if mail-use-rfc822
183 (progn (require 'rfc822)
184 (mapconcat 'identity (rfc822-addresses address) ", "))
185 (let (pos)
187 ;; Detect nested comments.
188 (if (string-match "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*(" address)
189 ;; Strip nested comments.
190 (with-current-buffer (get-buffer-create " *temp*")
191 (erase-buffer)
192 (insert address)
193 (set-syntax-table lisp-mode-syntax-table)
194 (goto-char 1)
195 (while (search-forward "(" nil t)
196 (forward-char -1)
197 (skip-chars-backward " \t")
198 (delete-region (point)
199 (save-excursion
200 (condition-case ()
201 (forward-sexp 1)
202 (error (goto-char (point-max))))
203 (point))))
204 (setq address (buffer-string))
205 (erase-buffer))
206 ;; Strip non-nested comments an easier way.
207 (while (setq pos (string-match
208 ;; This doesn't hack rfc822 nested comments
209 ;; `(xyzzy (foo) whinge)' properly. Big deal.
210 "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)"
211 address))
212 (setq address (replace-match "" nil nil address 0))))
214 ;; strip surrounding whitespace
215 (string-match "\\`[ \t\n]*" address)
216 (setq address (substring address
217 (match-end 0)
218 (string-match "[ \t\n]*\\'" address
219 (match-end 0))))
221 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
222 (setq pos 0)
223 (while (setq pos (string-match
224 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
225 address pos))
226 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
227 (if (and (> (length address) (match-end 0))
228 (= (aref address (match-end 0)) ?@))
229 (setq pos (match-end 0))
230 ;; Otherwise discard the "..." part.
231 (setq address (replace-match "" nil nil address 2))))
232 ;; If this address contains <...>, replace it with just
233 ;; the part between the <...>.
234 (while (setq pos (string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
235 address))
236 (setq address (replace-match (match-string 3 address)
237 nil 'literal address 2)))
238 address))))
240 ;;; The following piece of ugliness is legacy code. The name was an
241 ;;; unfortunate choice --- a flagrant violation of the Emacs Lisp
242 ;;; coding conventions. `mail-dont-reply-to' would have been
243 ;;; infinitely better. Also, `rmail-dont-reply-to-names' might have
244 ;;; been better named `mail-dont-reply-to-names' and sourced from this
245 ;;; file instead of in rmail.el. Yuck. -pmr
246 (defun rmail-dont-reply-to (destinations)
247 "Prune addresses from DESTINATIONS, a list of recipient addresses.
248 All addresses matching `rmail-dont-reply-to-names' are removed from
249 the comma-separated list. The pruned list is returned."
250 (if (null rmail-dont-reply-to-names)
251 (setq rmail-dont-reply-to-names
252 (concat (if rmail-default-dont-reply-to-names
253 (concat rmail-default-dont-reply-to-names "\\|")
255 (if (and user-mail-address
256 (not (equal user-mail-address user-login-name)))
257 ;; Anchor the login name and email address so
258 ;; that we don't match substrings: if the
259 ;; login name is "foo", we shouldn't match
260 ;; "barfoo@baz.com".
261 (concat "\\`"
262 (regexp-quote user-mail-address)
263 "\\'\\|")
265 (concat "\\`" (regexp-quote user-login-name) "@"))))
266 ;; Split up DESTINATIONS and match each element separately.
267 (let ((start-pos 0) (cur-pos 0)
268 (case-fold-search t))
269 (while start-pos
270 (setq cur-pos (string-match "[,\"]" destinations cur-pos))
271 (if (and cur-pos (equal (match-string 0 destinations) "\""))
272 ;; Search for matching quote.
273 (let ((next-pos (string-match "\"" destinations (1+ cur-pos))))
274 (if next-pos
275 (setq cur-pos (1+ next-pos))
276 ;; If the open-quote has no close-quote,
277 ;; delete the open-quote to get something well-defined.
278 ;; This case is not valid, but it can happen if things
279 ;; are weird elsewhere.
280 (setq destinations (concat (substring destinations 0 cur-pos)
281 (substring destinations (1+ cur-pos))))
282 (setq cur-pos start-pos)))
283 (let* ((address (substring destinations start-pos cur-pos))
284 (naked-address (mail-strip-quoted-names address)))
285 (if (string-match rmail-dont-reply-to-names naked-address)
286 (setq destinations (concat (substring destinations 0 start-pos)
287 (and cur-pos (substring destinations
288 (1+ cur-pos))))
289 cur-pos start-pos)
290 (setq cur-pos (and cur-pos (1+ cur-pos))
291 start-pos cur-pos))))))
292 ;; get rid of any trailing commas
293 (let ((pos (string-match "[ ,\t\n]*\\'" destinations)))
294 (if pos
295 (setq destinations (substring destinations 0 pos))))
296 ;; remove leading spaces. they bother me.
297 (if (string-match "\\(\\s \\|,\\)*" destinations)
298 (substring destinations (match-end 0))
299 destinations))
302 ;;;###autoload
303 (defun mail-fetch-field (field-name &optional last all list)
304 "Return the value of the header field whose type is FIELD-NAME.
305 The buffer is expected to be narrowed to just the header of the message.
306 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
307 If third arg ALL is non-nil, concatenate all such fields with commas between.
308 If 4th arg LIST is non-nil, return a list of all such fields."
309 (save-excursion
310 (goto-char (point-min))
311 (let ((case-fold-search t)
312 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
313 (if (or all list)
314 (let ((value (if all "")))
315 (while (re-search-forward name nil t)
316 (let ((opoint (point)))
317 (while (progn (forward-line 1)
318 (looking-at "[ \t]")))
319 ;; Back up over newline, then trailing spaces or tabs
320 (forward-char -1)
321 (skip-chars-backward " \t" opoint)
322 (if list
323 (setq value (cons (buffer-substring-no-properties
324 opoint (point))
325 value))
326 (setq value (concat value
327 (if (string= value "") "" ", ")
328 (buffer-substring-no-properties
329 opoint (point)))))))
330 (if list
331 value
332 (and (not (string= value "")) value)))
333 (if (re-search-forward name nil t)
334 (progn
335 (if last (while (re-search-forward name nil t)))
336 (let ((opoint (point)))
337 (while (progn (forward-line 1)
338 (looking-at "[ \t]")))
339 ;; Back up over newline, then trailing spaces or tabs
340 (forward-char -1)
341 (skip-chars-backward " \t" opoint)
342 (buffer-substring-no-properties opoint (point)))))))))
344 ;; Parse a list of tokens separated by commas.
345 ;; It runs from point to the end of the visible part of the buffer.
346 ;; Whitespace before or after tokens is ignored,
347 ;; but whitespace within tokens is kept.
348 (defun mail-parse-comma-list ()
349 (let (accumulated
350 beg)
351 (skip-chars-forward " \t\n")
352 (while (not (eobp))
353 (setq beg (point))
354 (skip-chars-forward "^,")
355 (skip-chars-backward " \t\n")
356 (setq accumulated
357 (cons (buffer-substring-no-properties beg (point))
358 accumulated))
359 (skip-chars-forward "^,")
360 (skip-chars-forward ", \t\n"))
361 accumulated))
363 (defun mail-comma-list-regexp (labels)
364 (let (pos)
365 (setq pos (or (string-match "[^ \t]" labels) 0))
366 ;; Remove leading and trailing whitespace.
367 (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
368 ;; Change each comma to \|, and flush surrounding whitespace.
369 (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
370 (setq labels
371 (concat (substring labels 0 pos)
372 "\\|"
373 (substring labels (match-end 0))))))
374 labels)
376 (defun mail-rfc822-time-zone (time)
377 (let* ((sec (or (car (current-time-zone time)) 0))
378 (absmin (/ (abs sec) 60)))
379 (format "%c%02d%02d" (if (< sec 0) ?- ?+) (/ absmin 60) (% absmin 60))))
381 (defun mail-rfc822-date ()
382 (let* ((time (current-time))
383 (s (current-time-string time)))
384 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s)
385 (concat (substring s (match-beginning 2) (match-end 2)) " "
386 (substring s (match-beginning 1) (match-end 1)) " "
387 (substring s (match-beginning 4) (match-end 4)) " "
388 (substring s (match-beginning 3) (match-end 3)) " "
389 (mail-rfc822-time-zone time))))
391 (provide 'mail-utils)
393 ;; arch-tag: b24aec2f-fd65-4ceb-9e39-3cc2827036fd
394 ;;; mail-utils.el ends here