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.
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.
192 (with-current-buffer (get-buffer-create " *temp*")
195 (set-syntax-table lisp-mode-syntax-table
)
197 (while (search-forward "(" nil t
)
199 (skip-chars-backward " \t")
200 (delete-region (point)
204 (error (goto-char (point-max))))
206 (setq address
(buffer-string))
208 ;; Strip non-nested comments an easier way.
209 (while (setq pos
(string-match
210 ;; This doesn't hack rfc822 nested comments
211 ;; `(xyzzy (foo) whinge)' properly. Big deal.
212 "[ \t]*(\\([^)\\]\\|\\\\.\\|\\\\\n\\)*)"
214 (setq address
(replace-match "" nil nil address
0))))
216 ;; strip surrounding whitespace
217 (string-match "\\`[ \t\n]*" address
)
218 (setq address
(substring address
220 (string-match "[ \t\n]*\\'" address
223 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
225 (while (setq pos
(string-match
226 "\\([ \t]?\\)\\([ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*\\)"
228 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
229 (if (and (> (length address
) (match-end 0))
230 (= (aref address
(match-end 0)) ?
@))
231 (setq pos
(match-end 0))
232 ;; Otherwise discard the "..." part.
233 (setq address
(replace-match "" nil nil address
2))))
234 ;; If this address contains <...>, replace it with just
235 ;; the part between the <...>.
236 (while (setq pos
(string-match "\\(,\\s-*\\|\\`\\)\\([^,]*<\\([^>,:]*\\)>[^,]*\\)\\(\\s-*,\\|\\'\\)"
238 (setq address
(replace-match (match-string 3 address
)
239 nil
'literal address
2)))
242 ;;; The following piece of ugliness is legacy code. The name was an
243 ;;; unfortunate choice --- a flagrant violation of the Emacs Lisp
244 ;;; coding conventions. `mail-dont-reply-to' would have been
245 ;;; infinitely better. Also, `rmail-dont-reply-to-names' might have
246 ;;; been better named `mail-dont-reply-to-names' and sourced from this
247 ;;; file instead of in rmail.el. Yuck. -pmr
248 (defun rmail-dont-reply-to (destinations)
249 "Prune addresses from DESTINATIONS, a list of recipient addresses.
250 All addresses matching `rmail-dont-reply-to-names' are removed from
251 the comma-separated list. The pruned list is returned."
252 ;; FIXME this (setting a user option the first time a command is used)
253 ;; is somewhat strange. Normally one would never set the option,
254 ;; but instead fall back to the default so long as it was nil.
255 ;; Or just set the default directly in the defcustom.
256 (if (null rmail-dont-reply-to-names
)
257 (setq rmail-dont-reply-to-names
258 (concat (if rmail-default-dont-reply-to-names
259 (concat rmail-default-dont-reply-to-names
"\\|")
261 (if (and user-mail-address
262 (not (equal user-mail-address user-login-name
)))
263 ;; Anchor the login name and email address so
264 ;; that we don't match substrings: if the
265 ;; login name is "foo", we shouldn't match
268 (regexp-quote user-mail-address
)
271 (concat "\\`" (regexp-quote user-login-name
) "@"))))
272 ;; Split up DESTINATIONS and match each element separately.
273 (let ((start-pos 0) (cur-pos 0)
274 (case-fold-search t
))
276 (setq cur-pos
(string-match "[,\"]" destinations cur-pos
))
277 (if (and cur-pos
(equal (match-string 0 destinations
) "\""))
278 ;; Search for matching quote.
279 (let ((next-pos (string-match "\"" destinations
(1+ cur-pos
))))
281 (setq cur-pos
(1+ next-pos
))
282 ;; If the open-quote has no close-quote,
283 ;; delete the open-quote to get something well-defined.
284 ;; This case is not valid, but it can happen if things
285 ;; are weird elsewhere.
286 (setq destinations
(concat (substring destinations
0 cur-pos
)
287 (substring destinations
(1+ cur-pos
))))
288 (setq cur-pos start-pos
)))
289 (let* ((address (substring destinations start-pos cur-pos
))
290 (naked-address (mail-strip-quoted-names address
)))
291 (if (string-match rmail-dont-reply-to-names naked-address
)
292 (setq destinations
(concat (substring destinations
0 start-pos
)
293 (and cur-pos
(substring destinations
296 (setq cur-pos
(and cur-pos
(1+ cur-pos
))
297 start-pos cur-pos
))))))
298 ;; get rid of any trailing commas
299 (let ((pos (string-match "[ ,\t\n]*\\'" destinations
)))
301 (setq destinations
(substring destinations
0 pos
))))
302 ;; remove leading spaces. they bother me.
303 (if (string-match "\\(\\s \\|,\\)*" destinations
)
304 (substring destinations
(match-end 0))
309 (defun mail-fetch-field (field-name &optional last all list
)
310 "Return the value of the header field whose type is FIELD-NAME.
311 If second arg LAST is non-nil, use the last field of type FIELD-NAME.
312 If third arg ALL is non-nil, concatenate all such fields with commas between.
313 If 4th arg LIST is non-nil, return a list of all such fields.
314 The buffer should be narrowed to just the header, else false
315 matches may be returned from the message body."
317 (goto-char (point-min))
318 (let ((case-fold-search t
)
319 (name (concat "^" (regexp-quote field-name
) "[ \t]*:[ \t]*")))
321 (let ((value (if all
"")))
322 (while (re-search-forward name nil t
)
323 (let ((opoint (point)))
324 (while (progn (forward-line 1)
325 (looking-at "[ \t]")))
326 ;; Back up over newline, then trailing spaces or tabs
328 (skip-chars-backward " \t" opoint
)
330 (setq value
(cons (buffer-substring-no-properties
333 (setq value
(concat value
334 (if (string= value
"") "" ", ")
335 (buffer-substring-no-properties
339 (and (not (string= value
"")) value
)))
340 (if (re-search-forward name nil t
)
342 (if last
(while (re-search-forward name nil t
)))
343 (let ((opoint (point)))
344 (while (progn (forward-line 1)
345 (looking-at "[ \t]")))
346 ;; Back up over newline, then trailing spaces or tabs
348 (skip-chars-backward " \t" opoint
)
349 (buffer-substring-no-properties opoint
(point)))))))))
351 ;; Parse a list of tokens separated by commas.
352 ;; It runs from point to the end of the visible part of the buffer.
353 ;; Whitespace before or after tokens is ignored,
354 ;; but whitespace within tokens is kept.
355 (defun mail-parse-comma-list ()
358 (skip-chars-forward " \t\n")
361 (skip-chars-forward "^,")
362 (skip-chars-backward " \t\n")
364 (cons (buffer-substring-no-properties beg
(point))
366 (skip-chars-forward "^,")
367 (skip-chars-forward ", \t\n"))
370 (defun mail-comma-list-regexp (labels)
372 (setq pos
(or (string-match "[^ \t]" labels
) 0))
373 ;; Remove leading and trailing whitespace.
374 (setq labels
(substring labels pos
(string-match "[ \t]*$" labels pos
)))
375 ;; Change each comma to \|, and flush surrounding whitespace.
376 (while (setq pos
(string-match "[ \t]*,[ \t]*" labels
))
378 (concat (substring labels
0 pos
)
380 (substring labels
(match-end 0))))))
383 (defun mail-rfc822-time-zone (time)
384 (let* ((sec (or (car (current-time-zone time
)) 0))
385 (absmin (/ (abs sec
) 60)))
386 (format "%c%02d%02d" (if (< sec
0) ?- ?
+) (/ absmin
60) (% absmin
60))))
388 (defun mail-rfc822-date ()
389 (let* ((time (current-time))
390 (s (current-time-string time
)))
391 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s
)
392 (concat (substring s
(match-beginning 2) (match-end 2)) " "
393 (substring s
(match-beginning 1) (match-end 1)) " "
394 (substring s
(match-beginning 4) (match-end 4)) " "
395 (substring s
(match-beginning 3) (match-end 3)) " "
396 (mail-rfc822-time-zone time
))))
398 (defun mail-mbox-from ()
399 "Return an mbox \"From \" line for the current message.
400 The buffer should be narrowed to just the header."
401 (let ((from (or (mail-fetch-field "from")
402 (mail-fetch-field "really-from")
403 (mail-fetch-field "sender")
405 (date (mail-fetch-field "date")))
406 (format "From %s %s\n" (mail-strip-quoted-names from
)
409 (current-time-string (date-to-time date
))))
410 (current-time-string)))))
412 (provide 'mail-utils
)
414 ;; arch-tag: b24aec2f-fd65-4ceb-9e39-3cc2827036fd
415 ;;; mail-utils.el ends here