]> code.delx.au - gnu-emacs/blob - lisp/mail/mail-utils.el
Added or corrected Commentary sections
[gnu-emacs] / lisp / mail / mail-utils.el
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 GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it 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 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Utility functions for maol and netnews handling. These handle fine
27 ;; points of header parsing.
28
29 ;;; Code:
30
31 ;;; We require lisp-mode to make sure that lisp-mode-syntax-table has
32 ;;; been initialized.
33 (require 'lisp-mode)
34
35 ;;;###autoload
36 (defvar 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
41 (defun mail-string-delete (string start end)
42 "Returns a string containing all of STRING except the part
43 from START (inclusive) to END (exclusive)."
44 (if (null end) (substring string 0 start)
45 (concat (substring string 0 start)
46 (substring string end nil))))
47
48 (defun mail-strip-quoted-names (address)
49 "Delete comments and quoted strings in an address list ADDRESS.
50 Also delete leading/trailing whitespace and replace FOO <BAR> with just BAR.
51 Return a modified address list."
52 (if (null address)
53 nil
54 (if mail-use-rfc822
55 (progn (require 'rfc822)
56 (mapconcat 'identity (rfc822-addresses address) ", "))
57 (let (pos)
58 (string-match "\\`[ \t\n]*" address)
59 ;; strip surrounding whitespace
60 (setq address (substring address
61 (match-end 0)
62 (string-match "[ \t\n]*\\'" address
63 (match-end 0))))
64
65 ;; Detect nested comments.
66 (if (string-match "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*(" address)
67 ;; Strip nested comments.
68 (save-excursion
69 (set-buffer (get-buffer-create " *temp*"))
70 (erase-buffer)
71 (insert address)
72 (set-syntax-table lisp-mode-syntax-table)
73 (goto-char 1)
74 (while (search-forward "(" nil t)
75 (forward-char -1)
76 (skip-chars-backward " \t")
77 (delete-region (point)
78 (save-excursion (forward-sexp 1) (point))))
79 (setq address (buffer-string))
80 (erase-buffer))
81 ;; Strip non-nested comments an easier way.
82 (while (setq pos (string-match
83 ;; This doesn't hack rfc822 nested comments
84 ;; `(xyzzy (foo) whinge)' properly. Big deal.
85 "[ \t]*(\\([^)\"\\]\\|\\\\.\\|\\\\\n\\)*)"
86 address))
87 (setq address
88 (mail-string-delete address
89 pos (match-end 0)))))
90
91 ;; strip `quoted' names (This is supposed to hack `"Foo Bar" <bar@host>')
92 (setq pos 0)
93 (while (setq pos (string-match
94 "[ \t]*\"\\([^\"\\]\\|\\\\.\\|\\\\\n\\)*\"[ \t\n]*"
95 address pos))
96 ;; If the next thing is "@", we have "foo bar"@host. Leave it.
97 (if (and (> (length address) (match-end 0))
98 (= (aref address (match-end 0)) ?@))
99 (setq pos (match-end 0))
100 (setq address
101 (mail-string-delete address
102 pos (match-end 0)))))
103 ;; Retain only part of address in <> delims, if there is such a thing.
104 (while (setq pos (string-match "\\(,\\|\\`\\)[^,]*<\\([^>,]*>\\)"
105 address))
106 (let ((junk-beg (match-end 1))
107 (junk-end (match-beginning 2))
108 (close (match-end 0)))
109 (setq address (mail-string-delete address (1- close) close))
110 (setq address (mail-string-delete address junk-beg junk-end))))
111 address))))
112
113 (or (and (boundp 'rmail-default-dont-reply-to-names)
114 (not (null rmail-default-dont-reply-to-names)))
115 (setq rmail-default-dont-reply-to-names "info-"))
116
117 ; rmail-dont-reply-to-names is defined in loaddefs
118 (defun rmail-dont-reply-to (userids)
119 "Returns string of mail addresses USERIDS sans any recipients
120 that start with matches for `rmail-dont-reply-to-names'.
121 Usenet paths ending in an element that matches are removed also."
122 (if (null rmail-dont-reply-to-names)
123 (setq rmail-dont-reply-to-names
124 (concat (if rmail-default-dont-reply-to-names
125 (concat rmail-default-dont-reply-to-names "\\|")
126 "")
127 (concat (regexp-quote (user-original-login-name))
128 "\\>"))))
129 (let ((match (concat "\\(^\\|,\\)[ \t\n]*\\([^,\n]*!\\|\\)\\("
130 rmail-dont-reply-to-names
131 "\\)"))
132 (case-fold-search t)
133 pos epos)
134 (while (setq pos (string-match match userids))
135 (if (> pos 0) (setq pos (1+ pos)))
136 (setq epos
137 (if (string-match "[ \t\n,]+" userids (match-end 0))
138 (match-end 0)
139 (length userids)))
140 (setq userids
141 (mail-string-delete
142 userids pos epos)))
143 ;; get rid of any trailing commas
144 (if (setq pos (string-match "[ ,\t\n]*\\'" userids))
145 (setq userids (substring userids 0 pos)))
146 ;; remove leading spaces. they bother me.
147 (if (string-match "\\s *" userids)
148 (substring userids (match-end 0))
149 userids)))
150 \f
151 (defun mail-fetch-field (field-name &optional last all)
152 "Return the value of the header field FIELD-NAME.
153 The buffer is expected to be narrowed to just the headers of the message.
154 If second arg LAST is non-nil, use the last such field if there are several.
155 If third arg ALL is non-nil, concatenate all such fields with commas between."
156 (save-excursion
157 (goto-char (point-min))
158 (let ((case-fold-search t)
159 (name (concat "^" (regexp-quote field-name) "[ \t]*:[ \t]*")))
160 (goto-char (point-min))
161 (if all
162 (let ((value ""))
163 (while (re-search-forward name nil t)
164 (let ((opoint (point)))
165 (while (progn (forward-line 1)
166 (looking-at "[ \t]")))
167 (setq value (concat value
168 (if (string= value "") "" ", ")
169 (buffer-substring opoint (1- (point)))))))
170 (and (not (string= value "")) value))
171 (if (re-search-forward name nil t)
172 (progn
173 (if last (while (re-search-forward name nil t)))
174 (let ((opoint (point)))
175 (while (progn (forward-line 1)
176 (looking-at "[ \t]")))
177 (buffer-substring opoint (1- (point))))))))))
178 \f
179 ;; Parse a list of tokens separated by commas.
180 ;; It runs from point to the end of the visible part of the buffer.
181 ;; Whitespace before or after tokens is ignored,
182 ;; but whitespace within tokens is kept.
183 (defun mail-parse-comma-list ()
184 (let (accumulated
185 beg)
186 (skip-chars-forward " ")
187 (while (not (eobp))
188 (setq beg (point))
189 (skip-chars-forward "^,")
190 (skip-chars-backward " ")
191 (setq accumulated
192 (cons (buffer-substring beg (point))
193 accumulated))
194 (skip-chars-forward "^,")
195 (skip-chars-forward ", "))
196 accumulated))
197
198 (defun mail-comma-list-regexp (labels)
199 (let (pos)
200 (setq pos (or (string-match "[^ \t]" labels) 0))
201 ;; Remove leading and trailing whitespace.
202 (setq labels (substring labels pos (string-match "[ \t]*$" labels pos)))
203 ;; Change each comma to \|, and flush surrounding whitespace.
204 (while (setq pos (string-match "[ \t]*,[ \t]*" labels))
205 (setq labels
206 (concat (substring labels 0 pos)
207 "\\|"
208 (substring labels (match-end 0))))))
209 labels)
210
211 (provide 'mail-utils)
212
213 ;;; mail-utils.el ends here