0
|
1 ;;; mail-utils.el --- utility functions used both by rmail and rnews
|
|
2
|
|
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: mail, news
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
16
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
0
|
24
|
|
25 ;;; Synched up with: FSF 19.30.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Utility functions for mail and netnews handling. These handle fine
|
|
30 ;; points of header parsing.
|
|
31
|
|
32 ;;; Code:
|
|
33
|
|
34 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
|
|
35 ;;; been initialized.
|
|
36 (require 'lisp-mode)
|
|
37
|
|
38 ;;;###autoload
|
|
39 (defvar mail-use-rfc822 nil "\
|
|
40 *If non-nil, use a full, hairy RFC822 parser on mail addresses.
|
|
41 Otherwise, (the default) use a smaller, somewhat faster, and
|
|
42 often correct parser.")
|
|
43
|
|
44 ;; Returns t if file FILE is an Rmail file.
|
|
45 ;;;###autoload
|
|
46 (defun mail-file-babyl-p (file)
|
|
47 (let ((buf (generate-new-buffer " *rmail-file-p*")))
|
|
48 (unwind-protect
|
|
49 (save-excursion
|
|
50 (set-buffer buf)
|
|
51 (insert-file-contents file nil 0 100)
|
|
52 (looking-at "BABYL OPTIONS:"))
|
|
53 (kill-buffer buf))))
|
|
54
|
|
55 (defun mail-string-delete (string start end)
|
|
56 "Returns a string containing all of STRING except the part
|
|
57 from START (inclusive) to END (exclusive)."
|
|
58 (if (null end) (substring string 0 start)
|
|
59 (concat (substring string 0 start)
|
|
60 (substring string end nil))))
|
|
61
|
|
62 (defun mail-strip-quoted-names (address)
|
|
63 "Delete comments and quoted strings in an address list ADDRESS.
|
|
64 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
|
|
65 Return a modified address list."
|
|
66 (if (null address)
|
|
67 nil
|
|
68 (if mail-use-rfc822
|
|
69 (progn (require 'rfc822)
|
|
70 (mapconcat 'identity (rfc822-addresses address) ", "))
|
|
71 (let (pos)
|
|
72 (string-match "\\`[ \t\n]*" address)
|
|
73 ;; strip surrounding whitespace
|
|
74 (setq address (substring address
|
|
75 (match-end 0)
|
|
76 (string-match "[ \t\n]*\\'" address
|
|
77 (match-end 0))))
|
|
78
|
|
79 ;; Detect nested comments.
|
|
80 (if (string-match "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*(" address)
|
|
81 ;; Strip nested comments.
|
|
82 (save-excursion
|
|
83 (set-buffer (get-buffer-create " *temp*"))
|
|
84 (erase-buffer)
|
|
85 (insert address)
|
|
86 (set-syntax-table lisp-mode-syntax-table)
|
|
87 (goto-char 1)
|
|
88 (while (search-forward "(" nil t)
|
|
89 (forward-char -1)
|
|
90 (skip-chars-backward " \t")
|
|
91 (delete-region (point)
|
|
92 (save-excursion
|
|
93 (condition-case ()
|
|
94 (forward-sexp 1)
|
|
95 (error (goto-char (point-max))))
|
|
96 (point))))
|
|
97 (setq address (buffer-string))
|
|
98 (erase-buffer))
|
|
99 ;; Strip non-nested comments an easier way.
|
|
100 (while (setq pos (string-match
|
|
101 ;; This doesn't hack rfc822 nested comments
|
|
102 ;; `(xyzzy (foo) whinge)' properly. Big deal.
|
|
103 "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*)"
|
|
104 address))
|
|
105 (setq address
|
|
106 (mail-string-delete address
|
|
107 pos (match-end 0)))))
|
|
108
|
|
109 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
|
|
110 (setq pos 0)
|
|
111 (while (setq pos (string-match
|
|
112 "[ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*"
|
|
113 address pos))
|
|
114 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
|
|
115 (if (and (> (length address) (match-end 0))
|
|
116 (= (aref address (match-end 0)) ?@))
|
|
117 (setq pos (match-end 0))
|
|
118 (setq address
|
|
119 (mail-string-delete address
|
|
120 pos (match-end 0)))))
|
|
121 ;; Retain only part of address in <> delims, if there is such a thing.
|
|
122 (while (setq pos (string-match "\\(,\\s-*\\|\\`\\)[^,]*<\\([^>,]*>\\)"
|
|
123 address))
|
|
124 (let ((junk-beg (match-end 1))
|
|
125 (junk-end (match-beginning 2))
|
|
126 (close (match-end 0)))
|
|
127 (setq address (mail-string-delete address (1- close) close))
|
|
128 (setq address (mail-string-delete address junk-beg junk-end))))
|
|
129 address))))
|
|
130
|
|
131 (or (and (boundp 'rmail-default-dont-reply-to-names)
|
|
132 (not (null rmail-default-dont-reply-to-names)))
|
|
133 (setq rmail-default-dont-reply-to-names "info-"))
|
|
134
|
|
135 ; rmail-dont-reply-to-names is defined in loaddefs
|
|
136 (defun rmail-dont-reply-to (userids)
|
|
137 "Returns string of mail addresses USERIDS sans any recipients
|
|
138 that start with matches for `rmail-dont-reply-to-names'.
|
|
139 Usenet paths ending in an element that matches are removed also."
|
|
140 (if (null rmail-dont-reply-to-names)
|
|
141 (setq rmail-dont-reply-to-names
|
|
142 (concat (if rmail-default-dont-reply-to-names
|
|
143 (concat rmail-default-dont-reply-to-names "\\|")
|
|
144 "")
|
|
145 (concat (regexp-quote (user-login-name))
|
|
146 "\\>"))))
|
116
|
147 (let ((match (concat "\\(^\\|,\\)[ \t\n]*\\([^,\n]*[!<]\\|\\)\\("
|
0
|
148 rmail-dont-reply-to-names
|
|
149 "\\)"))
|
|
150 (case-fold-search t)
|
|
151 pos epos)
|
|
152 (while (setq pos (string-match match userids))
|
|
153 (if (> pos 0) (setq pos (match-beginning 2)))
|
|
154 (setq epos
|
|
155 ;; Delete thru the next comma, plus whitespace after.
|
|
156 (if (string-match ",[ \t\n]+" userids (match-end 0))
|
|
157 (match-end 0)
|
|
158 (length userids)))
|
|
159 (setq userids
|
|
160 (mail-string-delete
|
|
161 userids pos epos)))
|
|
162 ;; get rid of any trailing commas
|
|
163 (if (setq pos (string-match "[ ,\t\n]*\\'" userids))
|
|
164 (setq userids (substring userids 0 pos)))
|
|
165 ;; remove leading spaces. they bother me.
|
|
166 (if (string-match "\\s *" userids)
|
|
167 (substring userids (match-end 0))
|
|
168 userids)))
|
|
169
|
|
170 ;;;###autoload
|
|
171 (defun mail-fetch-field (field-name &optional last all)
|
|
172 "Return the value of the header field FIELD-NAME.
|
|
173 The buffer is expected to be narrowed to just the headers of the message.
|
|
174 If second arg LAST is non-nil, use the last such field if there are several.
|
|
175 If third arg ALL is non-nil, concatenate all such fields with commas between."
|
|
176 (save-excursion
|
|
177 (goto-char (point-min))
|
|
178 (let ((case-fold-search t)
|
|
179 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
|
|
180 (if all
|
|
181 (let ((value ""))
|
|
182 (while (re-search-forward name nil t)
|
|
183 (let ((opoint (point)))
|
|
184 (while (progn (forward-line 1)
|
|
185 (looking-at "[ \t]")))
|
|
186 ;; Back up over newline, then trailing spaces or tabs
|
|
187 (forward-char -1)
|
|
188 (while (member (preceding-char) '(? ?\t))
|
|
189 (forward-char -1))
|
|
190 (setq value (concat value
|
|
191 (if (string= value "") "" ", ")
|
|
192 (buffer-substring-no-properties
|
|
193 opoint (point))))))
|
|
194 (and (not (string= value "")) value))
|
|
195 (if (re-search-forward name nil t)
|
|
196 (progn
|
|
197 (if last (while (re-search-forward name nil t)))
|
|
198 (let ((opoint (point)))
|
|
199 (while (progn (forward-line 1)
|
|
200 (looking-at "[ \t]")))
|
|
201 ;; Back up over newline, then trailing spaces or tabs
|
|
202 (forward-char -1)
|
|
203 (while (member (preceding-char) '(? ?\t))
|
|
204 (forward-char -1))
|
|
205 (buffer-substring-no-properties opoint (point)))))))))
|
|
206
|
|
207 ;; Parse a list of tokens separated by commas.
|
|
208 ;; It runs from point to the end of the visible part of the buffer.
|
|
209 ;; Whitespace before or after tokens is ignored,
|
|
210 ;; but whitespace within tokens is kept.
|
|
211 (defun mail-parse-comma-list ()
|
|
212 (let (accumulated
|
|
213 beg)
|
|
214 (skip-chars-forward " ")
|
|
215 (while (not (eobp))
|
|
216 (setq beg (point))
|
|
217 (skip-chars-forward "^,")
|
|
218 (skip-chars-backward " ")
|
|
219 (setq accumulated
|
|
220 (cons (buffer-substring beg (point))
|
|
221 accumulated))
|
|
222 (skip-chars-forward "^,")
|
|
223 (skip-chars-forward ", "))
|
|
224 accumulated))
|
|
225
|
|
226 (defun mail-comma-list-regexp (labels)
|
|
227 (let (pos)
|
|
228 (setq pos (or (string-match "[^ \t]" labels) 0))
|
|
229 ;; Remove leading and trailing whitespace.
|
|
230 (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
|
|
231 ;; Change each comma to \|, and flush surrounding whitespace.
|
|
232 (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
|
|
233 (setq labels
|
|
234 (concat (substring labels 0 pos)
|
|
235 "\\|"
|
|
236 (substring labels (match-end 0))))))
|
|
237 labels)
|
|
238
|
|
239 (defun mail-rfc822-time-zone (time)
|
|
240 (let* ((sec (or (car (current-time-zone time)) 0))
|
|
241 (absmin (/ (abs sec) 60)))
|
|
242 (format "%c%02d%02d" (if (< sec 0) ?- ?+) (/ absmin 60) (% absmin 60))))
|
|
243
|
|
244 (defun mail-rfc822-date ()
|
|
245 (let* ((time (current-time))
|
|
246 (s (current-time-string time)))
|
|
247 (string-match "[^ ]+ +\\([^ ]+\\) +\\([^ ]+\\) \\([^ ]+\\) \\([^ ]+\\)" s)
|
|
248 (concat (substring s (match-beginning 2) (match-end 2)) " "
|
|
249 (substring s (match-beginning 1) (match-end 1)) " "
|
|
250 (substring s (match-beginning 4) (match-end 4)) " "
|
|
251 (substring s (match-beginning 3) (match-end 3)) " "
|
|
252 (mail-rfc822-time-zone time))))
|
|
253
|
|
254 (provide 'mail-utils)
|
|
255
|
|
256 ;;; mail-utils.el ends here
|