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, 2011 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.
31 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
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."
43 ;; Returns t if file FILE is an Rmail file.
45 (defun mail-file-babyl-p (file)
46 "Return non-nil if FILE is a Babyl file."
48 (insert-file-contents file nil
0 100)
49 (looking-at "BABYL OPTIONS:")))
51 (defun mail-string-delete (string start end
)
52 "Returns a string containing all of STRING except the part
53 from START (inclusive) to END (exclusive)."
54 (if (null end
) (substring string
0 start
)
55 (concat (substring string
0 start
)
56 (substring string end nil
))))
59 (defun mail-quote-printable (string &optional wrapper
)
60 "Convert a string to the \"quoted printable\" Q encoding.
61 If the optional argument WRAPPER is non-nil,
62 we add the wrapper characters =?ISO-8859-1?Q?....?=."
63 (let ((i 0) (result ""))
65 (while (string-match "[?=\"\200-\377]" string i
)
67 (concat result
(substring string i
(match-beginning 0))
68 (upcase (format "=%02x"
69 (aref string
(match-beginning 0))))))
70 (setq i
(match-end 0)))
72 (concat "=?ISO-8859-1?Q?"
73 result
(substring string i
)
75 (concat result
(substring string i
))))))
78 (defun mail-quote-printable-region (beg end
&optional wrapper
)
79 "Convert the region to the \"quoted printable\" Q encoding.
80 If the optional argument WRAPPER is non-nil,
81 we add the wrapper characters =?ISO-8859-1?Q?....?=."
87 (narrow-to-region beg end
)
88 (while (re-search-forward "[?=\"\200-\377]" nil t
)
89 (replace-match (upcase (format "=%02x" (preceding-char)))
93 (insert "=?ISO-8859-1?Q?")
97 (defun mail-unquote-printable-hexdigit (char)
98 (setq char
(upcase char
))
104 (defun mail-unquote-printable (string &optional wrapper
)
105 "Undo the \"quoted printable\" encoding.
106 If the optional argument WRAPPER is non-nil,
107 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=."
110 (string-match "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?" string
)
111 (setq string
(match-string 1 string
)))
113 (while (string-match "=\\(..\\|\n\\)" string i
)
114 (setq strings
(cons (substring string i
(match-beginning 0)) strings
))
115 (unless (= (aref string
(match-beginning 1)) ?
\n)
118 (+ (* 16 (mail-unquote-printable-hexdigit
119 (aref string
(match-beginning 1))))
120 (mail-unquote-printable-hexdigit
121 (aref string
(1+ (match-beginning 1))))))
123 (setq i
(match-end 0)))
124 (apply 'concat
(nreverse (cons (substring string i
) strings
))))))
126 ;; FIXME Gnus for some reason has `quoted-printable-decode-region' in qp.el.
128 (defun mail-unquote-printable-region (beg end
&optional wrapper noerror
130 "Undo the \"quoted printable\" encoding in buffer from BEG to END.
131 If the optional argument WRAPPER is non-nil,
132 we expect to find and remove the wrapper characters =?ISO-8859-1?Q?....?=.
133 On encountering malformed quoted-printable text, exits with an error,
134 unless NOERROR is non-nil, in which case it continues, and returns nil
135 when finished. Returns non-nil on successful completion.
136 If UNIBYTE is non-nil, insert converted characters as unibyte.
137 That is useful if you are going to character code decoding afterward,
139 ;; FIXME: `unibyte' should always be non-nil, and the iso-latin-1
140 ;; specific handling should be removed (or moved elsewhere and generalized).
146 (narrow-to-region beg end
)
147 (goto-char (point-min))
149 (looking-at "\\`=\\?ISO-8859-1\\?Q\\?\\([^?]*\\)\\?"))
150 (delete-region (match-end 1) end
)
151 (delete-region (point) (match-beginning 1)))
152 (while (re-search-forward "=\\(\\([0-9A-F][0-9A-F]\\)\\|[=\n]\\|..\\)" nil t
)
153 (goto-char (match-end 0))
154 (cond ((= (char-after (match-beginning 1)) ?
\n)
156 ((= (char-after (match-beginning 1)) ?
=)
159 (let ((char (+ (* 16 (mail-unquote-printable-hexdigit
160 (char-after (match-beginning 2))))
161 (mail-unquote-printable-hexdigit
162 (char-after (1+ (match-beginning 2)))))))
166 ;; insert-byte will insert this as a
167 ;; corresponding eight-bit character.
168 (insert-byte char
1))
169 (replace-match (make-string 1 char
) t t
))))
173 (error "Malformed MIME quoted-printable message"))))
176 (eval-when-compile (require 'rfc822
))
178 (defun mail-strip-quoted-names (address)
179 "Delete comments and quoted strings in an address list ADDRESS.
180 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
181 Return a modified address list."
185 (progn (require 'rfc822
)
186 (mapconcat 'identity
(rfc822-addresses address
) ", "))
189 ;; Detect nested comments.
190 (if (string-match "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*(" address
)
191 ;; Strip nested comments.
194 (set-syntax-table lisp-mode-syntax-table
)
196 (while (search-forward "(" nil t
)
198 (skip-chars-backward " \t")
199 (delete-region (point)
203 (error (goto-char (point-max))))
205 (setq address
(buffer-string)))
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\\)*)"
212 (setq address
(replace-match "" nil nil address
0))))
214 ;; strip surrounding whitespace
215 (string-match "\\`[ \t\n]*" address
)
216 (setq address
(substring address
218 (string-match "[ \t\n]*\\'" address
221 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
223 (while (setq pos
(string-match
224 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
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-*,\\|\\'\\)"
236 (setq address
(replace-match (match-string 3 address
)
237 nil
'literal address
2)))
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 ;; FIXME this (setting a user option the first time a command is used)
251 ;; is somewhat strange. Normally one would never set the option,
252 ;; but instead fall back to the default so long as it was nil.
253 ;; Or just set the default directly in the defcustom.
254 (if (null rmail-dont-reply-to-names
)
255 (setq rmail-dont-reply-to-names
256 (concat (if rmail-default-dont-reply-to-names
257 (concat rmail-default-dont-reply-to-names
"\\|")
259 (if (and user-mail-address
260 (not (equal user-mail-address user-login-name
)))
261 ;; Anchor the login name and email address so
262 ;; that we don't match substrings: if the
263 ;; login name is "foo", we shouldn't match
266 (regexp-quote user-mail-address
)
269 (concat "\\`" (regexp-quote user-login-name
) "@"))))
270 ;; Split up DESTINATIONS and match each element separately.
271 (let ((start-pos 0) (cur-pos 0)
272 (case-fold-search t
))
274 (setq cur-pos
(string-match "[,\"]" destinations cur-pos
))
275 (if (and cur-pos
(equal (match-string 0 destinations
) "\""))
276 ;; Search for matching quote.
277 (let ((next-pos (string-match "\"" destinations
(1+ cur-pos
))))
279 (setq cur-pos
(1+ next-pos
))
280 ;; If the open-quote has no close-quote,
281 ;; delete the open-quote to get something well-defined.
282 ;; This case is not valid, but it can happen if things
283 ;; are weird elsewhere.
284 (setq destinations
(concat (substring destinations
0 cur-pos
)
285 (substring destinations
(1+ cur-pos
))))
286 (setq cur-pos start-pos
)))
287 (let* ((address (substring destinations start-pos cur-pos
))
288 (naked-address (mail-strip-quoted-names address
)))
289 (if (string-match rmail-dont-reply-to-names naked-address
)
290 (setq destinations
(concat (substring destinations
0 start-pos
)
291 (and cur-pos
(substring destinations
294 (setq cur-pos
(and cur-pos
(1+ cur-pos
))
295 start-pos cur-pos
))))))
296 ;; get rid of any trailing commas
297 (let ((pos (string-match "[ ,\t\n]*\\'" destinations
)))
299 (setq destinations
(substring destinations
0 pos
))))
300 ;; remove leading spaces. they bother me.
301 (if (string-match "\\(\\s \\|,\\)*" destinations
)
302 (substring destinations
(match-end 0))
307 (defun mail-fetch-field (field-name &optional last all list
)
308 "Return the value of the header field whose type is FIELD-NAME.
309 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
310 If third arg ALL is non-nil, concatenate all such fields with commas between.
311 If 4th arg LIST is non-nil, return a list of all such fields.
312 The buffer should be narrowed to just the header, else false
313 matches may be returned from the message body."
315 (goto-char (point-min))
316 (let ((case-fold-search t
)
317 (name (concat "^" (regexp-quote field-name
) "[ \t]*:[ \t]*")))
319 (let ((value (if all
"")))
320 (while (re-search-forward name nil t
)
321 (let ((opoint (point)))
322 (while (progn (forward-line 1)
323 (looking-at "[ \t]")))
324 ;; Back up over newline, then trailing spaces or tabs
326 (skip-chars-backward " \t" opoint
)
328 (setq value
(cons (buffer-substring-no-properties
331 (setq value
(concat value
332 (if (string= value
"") "" ", ")
333 (buffer-substring-no-properties
337 (and (not (string= value
"")) value
)))
338 (if (re-search-forward name nil t
)
340 (if last
(while (re-search-forward name nil t
)))
341 (let ((opoint (point)))
342 (while (progn (forward-line 1)
343 (looking-at "[ \t]")))
344 ;; Back up over newline, then trailing spaces or tabs
346 (skip-chars-backward " \t" opoint
)
347 (buffer-substring-no-properties opoint
(point)))))))))
349 ;; Parse a list of tokens separated by commas.
350 ;; It runs from point to the end of the visible part of the buffer.
351 ;; Whitespace before or after tokens is ignored,
352 ;; but whitespace within tokens is kept.
353 (defun mail-parse-comma-list ()
356 (skip-chars-forward " \t\n")
359 (skip-chars-forward "^,")
360 (skip-chars-backward " \t\n")
362 (cons (buffer-substring-no-properties beg
(point))
364 (skip-chars-forward "^,")
365 (skip-chars-forward ", \t\n"))
368 (defun mail-comma-list-regexp (labels)
370 (setq pos
(or (string-match "[^ \t]" labels
) 0))
371 ;; Remove leading and trailing whitespace.
372 (setq labels
(substring labels pos
(string-match "[ \t]*$" labels pos
)))
373 ;; Change each comma to \|, and flush surrounding whitespace.
374 (while (setq pos
(string-match "[ \t]*,[ \t]*" labels
))
376 (concat (substring labels
0 pos
)
378 (substring labels
(match-end 0))))))
381 (defun mail-rfc822-time-zone (time)
382 (let* ((sec (or (car (current-time-zone time
)) 0))
383 (absmin (/ (abs sec
) 60)))
384 (format "%c%02d%02d" (if (< sec
0) ?- ?
+) (/ absmin
60) (% absmin
60))))
386 (defun mail-rfc822-date ()
387 (let* ((time (current-time))
388 (s (current-time-string time
)))
389 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s
)
390 (concat (substring s
(match-beginning 2) (match-end 2)) " "
391 (substring s
(match-beginning 1) (match-end 1)) " "
392 (substring s
(match-beginning 4) (match-end 4)) " "
393 (substring s
(match-beginning 3) (match-end 3)) " "
394 (mail-rfc822-time-zone time
))))
396 (defun mail-mbox-from ()
397 "Return an mbox \"From \" line for the current message.
398 The buffer should be narrowed to just the header."
399 (let* ((from (mail-strip-quoted-names (or (mail-fetch-field "from")
400 (mail-fetch-field "really-from")
401 (mail-fetch-field "sender")
402 (mail-fetch-field "return-path")
404 (date (mail-fetch-field "date"))
405 ;; A From: header can contain multiple addresses, a "From "
406 ;; line must contain only one. (Bug#7760)
407 ;; See eg RFC 5322, 3.6.2. Originator Fields.
408 (end (string-match "[ \t]*[,\n]" from
)))
409 (format "From %s %s\n" (if end
410 (substring from
0 end
)
414 (current-time-string (date-to-time date
))))
415 (current-time-string)))))
417 (provide 'mail-utils
)
419 ;;; mail-utils.el ends here