1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
3 ;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
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/>.
26 ;; Utility functions for mail and netnews handling. These handle fine
27 ;; points of header parsing.
32 (defcustom mail-use-rfc822 nil
33 "If non-nil, use a full, hairy RFC822 parser on mail addresses.
34 Otherwise, (the default) use a smaller, somewhat faster, and
35 often correct parser."
39 ;; Returns t if file FILE is an Rmail file.
41 (defun mail-file-babyl-p (file)
42 "Return non-nil if FILE is a Babyl file."
44 (insert-file-contents file nil
0 100)
45 (looking-at "BABYL OPTIONS:")))
47 (defun mail-string-delete (string start end
)
48 "Returns a string containing all of STRING except the part
49 from START (inclusive) to END (exclusive)."
50 (if (null end
) (substring string
0 start
)
51 (concat (substring string
0 start
)
52 (substring string end nil
))))
55 (defun mail-quote-printable (string &optional wrapper
)
56 "Convert a string to the \"quoted printable\" Q encoding.
57 If the optional argument WRAPPER is non-nil,
58 we add the wrapper characters =?ISO-8859-1?Q?....?=."
59 (let ((i 0) (result ""))
61 (while (string-match "[?=\"\200-\377]" string i
)
63 (concat result
(substring string i
(match-beginning 0))
64 (upcase (format "=%02x"
65 (aref string
(match-beginning 0))))))
66 (setq i
(match-end 0)))
68 (concat "=?ISO-8859-1?Q?"
69 result
(substring string i
)
71 (concat result
(substring string i
))))))
74 (defun mail-quote-printable-region (beg end
&optional wrapper
)
75 "Convert the region to the \"quoted printable\" Q encoding.
76 If the optional argument WRAPPER is non-nil,
77 we add the wrapper characters =?ISO-8859-1?Q?....?=."
83 (narrow-to-region beg end
)
84 (while (re-search-forward "[?=\"\200-\377]" nil t
)
85 (replace-match (upcase (format "=%02x" (preceding-char)))
89 (insert "=?ISO-8859-1?Q?")
93 (defun mail-unquote-printable-hexdigit (char)
94 (setq char
(upcase char
))
100 (defun mail-unquote-printable (string &optional wrapper
)
101 "Undo the \"quoted printable\" encoding.
102 If the optional argument WRAPPER is non-nil,
103 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=."
106 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string
)
107 (setq string
(match-string 1 string
)))
109 (while (string-match "=\\(..\\|\n\\)" string i
)
110 (setq strings
(cons (substring string i
(match-beginning 0)) strings
))
111 (unless (= (aref string
(match-beginning 1)) ?
\n)
114 (+ (* 16 (mail-unquote-printable-hexdigit
115 (aref string
(match-beginning 1))))
116 (mail-unquote-printable-hexdigit
117 (aref string
(1+ (match-beginning 1))))))
119 (setq i
(match-end 0)))
120 (apply 'concat
(nreverse (cons (substring string i
) strings
))))))
122 ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
124 (defun mail-unquote-printable-region (beg end
&optional wrapper noerror
126 "Undo the \"quoted printable\" encoding in buffer from BEG to END.
127 If the optional argument WRAPPER is non-nil,
128 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
129 On encountering malformed quoted-printable text, exits with an error,
130 unless NOERROR is non-nil, in which case it continues, and returns nil
131 when finished. Returns non-nil on successful completion.
132 If UNIBYTE is non-nil, insert converted characters as unibyte.
133 That is useful if you are going to character code decoding afterward,
135 ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
136 ;; specific handling should be removed (or moved elsewhere and generalized).
142 (narrow-to-region beg end
)
143 (goto-char (point-min))
145 (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
146 (delete-region (match-end 1) end
)
147 (delete-region (point) (match-beginning 1)))
148 (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t
)
149 (goto-char (match-end 0))
150 (cond ((= (char-after (match-beginning 1)) ?
\n)
152 ((= (char-after (match-beginning 1)) ?
=)
155 (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
156 (char-after (match-beginning 2))))
157 (mail-unquote-printable-hexdigit
158 (char-after (1+ (match-beginning 2)))))))
162 ;; insert-byte will insert this as a
163 ;; corresponding eight-bit character.
164 (insert-byte char
1))
165 (replace-match (make-string 1 char
) t t
))))
169 (error "Malformed MIME quoted-printable message"))))
172 (eval-when-compile (require 'rfc822
))
174 (defun mail-strip-quoted-names (address)
175 "Delete comments and quoted strings in an address list ADDRESS.
176 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
177 Return a modified address list."
181 (progn (require 'rfc822
)
182 (mapconcat 'identity
(rfc822-addresses address
) ", "))
185 ;; Detect nested comments.
186 (if (string-match "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*(" address
)
187 ;; Strip nested comments.
188 (with-current-buffer (get-buffer-create " *temp*")
191 (set-syntax-table lisp-mode-syntax-table
)
193 (while (search-forward "(" nil t
)
195 (skip-chars-backward " \t")
196 (delete-region (point)
200 (error (goto-char (point-max))))
202 (setq address
(buffer-string))
204 ;; Strip non-nested comments an easier way.
205 (while (setq pos
(string-match
206 ;; This doesn't hack rfc822 nested comments
207 ;; `(xyzzy (foo) whinge)' properly. Big deal.
208 "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)"
210 (setq address
(replace-match "" nil nil address
0))))
212 ;; strip surrounding whitespace
213 (string-match "\\`[ \t\n]*" address
)
214 (setq address
(substring address
216 (string-match "[ \t\n]*\\'" address
219 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
221 (while (setq pos
(string-match
222 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
224 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
225 (if (and (> (length address
) (match-end 0))
226 (= (aref address
(match-end 0)) ?
@))
227 (setq pos
(match-end 0))
228 ;; Otherwise discard the "..." part.
229 (setq address
(replace-match "" nil nil address
2))))
230 ;; If this address contains <...>, replace it with just
231 ;; the part between the <...>.
232 (while (setq pos
(string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
234 (setq address
(replace-match (match-string 3 address
)
235 nil
'literal address
2)))
238 ;;; The following piece of ugliness is legacy code. The name was an
239 ;;; unfortunate choice --- a flagrant violation of the Emacs Lisp
240 ;;; coding conventions. `mail-dont-reply-to' would have been
241 ;;; infinitely better. Also, `rmail-dont-reply-to-names' might have
242 ;;; been better named `mail-dont-reply-to-names' and sourced from this
243 ;;; file instead of in rmail.el. Yuck. -pmr
244 (defun rmail-dont-reply-to (destinations)
245 "Prune addresses from DESTINATIONS, a list of recipient addresses.
246 All addresses matching `rmail-dont-reply-to-names' are removed from
247 the comma-separated list. The pruned list is returned."
248 ;; FIXME this (setting a user option the first time a command is used)
249 ;; is somewhat strange. Normally one would never set the option,
250 ;; but instead fall back to the default so long as it was nil.
251 ;; Or just set the default directly in the defcustom.
252 (if (null rmail-dont-reply-to-names
)
253 (setq rmail-dont-reply-to-names
254 (concat (if rmail-default-dont-reply-to-names
255 (concat rmail-default-dont-reply-to-names
"\\|")
257 (if (and user-mail-address
258 (not (equal user-mail-address user-login-name
)))
259 ;; Anchor the login name and email address so
260 ;; that we don't match substrings: if the
261 ;; login name is "foo", we shouldn't match
264 (regexp-quote user-mail-address
)
267 (concat "\\`" (regexp-quote user-login-name
) "@"))))
268 ;; Split up DESTINATIONS and match each element separately.
269 (let ((start-pos 0) (cur-pos 0)
270 (case-fold-search t
))
272 (setq cur-pos
(string-match "[,\"]" destinations cur-pos
))
273 (if (and cur-pos
(equal (match-string 0 destinations
) "\""))
274 ;; Search for matching quote.
275 (let ((next-pos (string-match "\"" destinations
(1+ cur-pos
))))
277 (setq cur-pos
(1+ next-pos
))
278 ;; If the open-quote has no close-quote,
279 ;; delete the open-quote to get something well-defined.
280 ;; This case is not valid, but it can happen if things
281 ;; are weird elsewhere.
282 (setq destinations
(concat (substring destinations
0 cur-pos
)
283 (substring destinations
(1+ cur-pos
))))
284 (setq cur-pos start-pos
)))
285 (let* ((address (substring destinations start-pos cur-pos
))
286 (naked-address (mail-strip-quoted-names address
)))
287 (if (string-match rmail-dont-reply-to-names naked-address
)
288 (setq destinations
(concat (substring destinations
0 start-pos
)
289 (and cur-pos
(substring destinations
292 (setq cur-pos
(and cur-pos
(1+ cur-pos
))
293 start-pos cur-pos
))))))
294 ;; get rid of any trailing commas
295 (let ((pos (string-match "[ ,\t\n]*\\'" destinations
)))
297 (setq destinations
(substring destinations
0 pos
))))
298 ;; remove leading spaces. they bother me.
299 (if (string-match "\\(\\s \\|,\\)*" destinations
)
300 (substring destinations
(match-end 0))
305 (defun mail-fetch-field (field-name &optional last all list
)
306 "Return the value of the header field whose type is FIELD-NAME.
307 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
308 If third arg ALL is non-nil, concatenate all such fields with commas between.
309 If 4th arg LIST is non-nil, return a list of all such fields.
310 The buffer should be narrowed to just the header, else false
311 matches may be returned from the message body."
313 (goto-char (point-min))
314 (let ((case-fold-search t
)
315 (name (concat "^" (regexp-quote field-name
) "[ \t]*:[ \t]*")))
317 (let ((value (if all
"")))
318 (while (re-search-forward name nil t
)
319 (let ((opoint (point)))
320 (while (progn (forward-line 1)
321 (looking-at "[ \t]")))
322 ;; Back up over newline, then trailing spaces or tabs
324 (skip-chars-backward " \t" opoint
)
326 (setq value
(cons (buffer-substring-no-properties
329 (setq value
(concat value
330 (if (string= value
"") "" ", ")
331 (buffer-substring-no-properties
335 (and (not (string= value
"")) value
)))
336 (if (re-search-forward name nil t
)
338 (if last
(while (re-search-forward name nil t
)))
339 (let ((opoint (point)))
340 (while (progn (forward-line 1)
341 (looking-at "[ \t]")))
342 ;; Back up over newline, then trailing spaces or tabs
344 (skip-chars-backward " \t" opoint
)
345 (buffer-substring-no-properties opoint
(point)))))))))
347 ;; Parse a list of tokens separated by commas.
348 ;; It runs from point to the end of the visible part of the buffer.
349 ;; Whitespace before or after tokens is ignored,
350 ;; but whitespace within tokens is kept.
351 (defun mail-parse-comma-list ()
354 (skip-chars-forward " \t\n")
357 (skip-chars-forward "^,")
358 (skip-chars-backward " \t\n")
360 (cons (buffer-substring-no-properties beg
(point))
362 (skip-chars-forward "^,")
363 (skip-chars-forward ", \t\n"))
366 (defun mail-comma-list-regexp (labels)
368 (setq pos
(or (string-match "[^ \t]" labels
) 0))
369 ;; Remove leading and trailing whitespace.
370 (setq labels
(substring labels pos
(string-match "[ \t]*$" labels pos
)))
371 ;; Change each comma to \|, and flush surrounding whitespace.
372 (while (setq pos
(string-match "[ \t]*,[ \t]*" labels
))
374 (concat (substring labels
0 pos
)
376 (substring labels
(match-end 0))))))
379 (defun mail-rfc822-time-zone (time)
380 (let* ((sec (or (car (current-time-zone time
)) 0))
381 (absmin (/ (abs sec
) 60)))
382 (format "%c%02d%02d" (if (< sec
0) ?- ?
+) (/ absmin
60) (% absmin
60))))
384 (defun mail-rfc822-date ()
385 (let* ((time (current-time))
386 (s (current-time-string time
)))
387 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s
)
388 (concat (substring s
(match-beginning 2) (match-end 2)) " "
389 (substring s
(match-beginning 1) (match-end 1)) " "
390 (substring s
(match-beginning 4) (match-end 4)) " "
391 (substring s
(match-beginning 3) (match-end 3)) " "
392 (mail-rfc822-time-zone time
))))
394 (defun mail-mbox-from ()
395 "Return an mbox \"From \" line for the current message.
396 The buffer should be narrowed to just the header."
397 (let ((from (or (mail-fetch-field "from")
398 (mail-fetch-field "really-from")
399 (mail-fetch-field "sender")
400 (mail-fetch-field "return-path")
402 (date (mail-fetch-field "date")))
403 (format "From %s %s\n" (mail-strip-quoted-names from
)
406 (current-time-string (date-to-time date
))))
407 (current-time-string)))))
409 (provide 'mail-utils
)
411 ;;; mail-utils.el ends here