Add arch taglines
[bpt/emacs.git] / lisp / mail / mailalias.el
CommitLineData
48919e0f 1;;; mailalias.el --- expand and complete mailing address aliases
6594deb0 2
e694581d 3;; Copyright (C) 1985, 1987, 1995, 1996, 1997 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: mail
e5167999 7
80a677d9
JA
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
80a677d9
JA
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
b578f267
EN
21;; along with GNU Emacs; 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.
80a677d9 24
e41b2db1
ER
25;;; Commentary:
26
27;; Basic functions for defining and expanding mail aliases.
28;; These seal off the interface to the alias-definition parts of a
29;; .mailrc file formatted for BSD's Mail or USL's mailx.
30
e5167999 31;;; Code:
80a677d9 32
594906dd 33(require 'sendmail)
c2db7f31 34
31407d5d
RS
35(defgroup mailalias nil
36 "Expanding mail aliases"
37 :group 'mail)
48919e0f 38
31407d5d
RS
39(defcustom mail-passwd-files '("/etc/passwd")
40 "*List of files from which to determine valid user names."
41 :type '(repeat string)
42 :group 'mailalias)
8e3beae0 43
31407d5d
RS
44(defcustom mail-passwd-command nil
45 "*Shell command to retrieve text to add to `/etc/passwd', or nil."
46 :type '(choice string (const nil))
47 :group 'mailalias)
8e3beae0 48
48919e0f
RS
49(defvar mail-directory-names t
50 "Alist of mail address directory entries.
51When t this still needs to be initialized.")
52
53(defvar mail-address-field-regexp
54 "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):")
55
31407d5d 56(defcustom mail-complete-alist
5695bf0c
RS
57 ;; Don't use backquote here; we don't want backquote to get loaded
58 ;; just because of loading this file.
310d0d40
RS
59 ;; Don't refer to mail-address-field-regexp here;
60 ;; that confuses some things such as cus-dep.el.
61 (cons '("^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):"
62 . (mail-get-names pattern))
5695bf0c
RS
63 '(("Newsgroups:" . (if (boundp 'gnus-active-hashtb)
64 gnus-active-hashtb
65 (if (boundp news-group-article-assoc)
66 news-group-article-assoc)))
67 ("Followup-To:" . (mail-sentto-newsgroups))
68 ;;("Distribution:" ???)
69 ))
31407d5d
RS
70 "*Alist of header field and expression to return alist for completion.
71The expression may reference the variable `pattern'
72which will hold the string being completed.
73If not on matching header, `mail-complete-function' gets called instead."
bd09e573 74 :type 'alist
31407d5d
RS
75 :group 'mailalias)
76(put 'mail-complete-alist 'risky-local-variable t)
48919e0f 77
e694581d 78;;;###autoload
31407d5d 79(defcustom mail-complete-style 'angles
e694581d
RS
80 "*Specifies how \\[mail-complete] formats the full name when it completes.
81If `nil', they contain just the return address like:
82 king@grassland.com
83If `parens', they look like:
84 king@grassland.com (Elvis Parsley)
85If `angles', they look like:
31407d5d
RS
86 Elvis Parsley <king@grassland.com>"
87 :type '(choice (const angles) (const parens) (const nil))
88 :group 'mailalias)
48919e0f 89
31407d5d
RS
90(defcustom mail-complete-function 'ispell-complete-word
91 "*Function to call when completing outside `mail-complete-alist'-header."
92 :type '(choice function (const nil))
93 :group 'mailalias)
48919e0f 94
31407d5d 95(defcustom mail-directory-function nil
25345117 96 "*Function to get completions from directory service or nil for none.
31407d5d
RS
97See `mail-directory-requery'."
98 :type '(choice function (const nil))
99 :group 'mailalias)
48919e0f
RS
100
101;; This is for when the directory is huge, or changes frequently.
31407d5d 102(defcustom mail-directory-requery nil
25345117 103 "*When non-nil call `mail-directory-function' for each completion.
48919e0f 104In that case, one argument gets passed to the function, the partial string
31407d5d
RS
105entered so far."
106 :type 'boolean
107 :group 'mailalias)
48919e0f 108
31407d5d
RS
109(defcustom mail-directory-process nil
110 "*Shell command to get the list of names from a mail directory.
111This value is used when the value of `mail-directory-function'
112is `mail-directory-process'. The value should be a list
113of the form (COMMAND ARG ...), where each of the list elements
114is evaluated. When `mail-directory-requery' is non-nil, during
115evaluation of these elements, the variable `pattern' contains
116the partial input being completed.
48919e0f 117
31407d5d 118The value might look like this:
48919e0f
RS
119
120 '(remote-shell-program \"HOST\" \"-nl\" \"USER\" \"COMMAND\")
121
31407d5d 122or like this:
48919e0f 123
31407d5d
RS
124 '(remote-shell-program \"HOST\" \"-n\" \"COMMAND '^\" pattern \"'\")"
125 :type 'sexp
126 :group 'mailalias)
127(put 'mail-directory-process 'risky-local-variable t)
48919e0f 128
31407d5d
RS
129(defcustom mail-directory-stream nil
130 "*List of (HOST SERVICE) for stream connection to mail directory."
131 :type 'sexp
132 :group 'mailalias)
133(put 'mail-directory-stream 'risky-local-variable t)
48919e0f 134
31407d5d
RS
135(defcustom mail-directory-parser nil
136 "*How to interpret the output of `mail-directory-function'.
48919e0f
RS
137Three types of values are possible:
138
139 - nil means to gather each line as one name
140 - regexp means first \\(grouping\\) in successive matches is name
31407d5d 141 - function called at beginning of buffer that returns an alist of names"
94955307 142 :type '(choice (const nil) regexp function)
31407d5d
RS
143 :group 'mailalias)
144(put 'mail-directory-parser 'risky-local-variable t)
48919e0f 145
31407d5d
RS
146;; Internal variables.
147
148(defvar mail-names t
149 "Alist of local users, aliases and directory entries as available.
150Elements have the form (MAILNAME) or (MAILNAME . FULLNAME).
151If the value means t, it means the real value should be calculated
152for the next use. this is used in `mail-complete'.")
153
154(defvar mail-local-names t
155 "Alist of local users.
156When t this still needs to be initialized.")
157\f
48919e0f 158
80a677d9
JA
159;; Called from sendmail-send-it, or similar functions,
160;; only if some mail aliases are defined.
e28449ed 161;;;###autoload
80a677d9
JA
162(defun expand-mail-aliases (beg end &optional exclude)
163 "Expand all mail aliases in suitable header fields found between BEG and END.
30eaaa34 164If interactive, expand in header fields.
1abe8488
KH
165Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and
166their `Resent-' variants.
167
96846422
RS
168Optional second arg EXCLUDE may be a regular expression defining text to be
169removed from alias expansions."
e28449ed
SM
170 (interactive
171 (save-excursion
172 (list (goto-char (point-min))
30eaaa34 173 (mail-header-end))))
e934d700 174 (sendmail-sync-aliases)
e28449ed
SM
175 (when (eq mail-aliases t)
176 (setq mail-aliases nil)
177 (build-mail-aliases))
178 (save-excursion
179 (goto-char beg)
180 (setq end (set-marker (make-marker) end))
181 (let ((case-fold-search nil))
182 (while (let ((case-fold-search t))
183 (re-search-forward mail-address-field-regexp end t))
184 (skip-chars-forward " \t")
185 (let ((beg1 (point))
186 end1 pos epos seplen
187 ;; DISABLED-ALIASES records aliases temporarily disabled
188 ;; while we scan text that resulted from expanding those aliases.
189 ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN
190 ;; is where to reenable the alias (expressed as number of chars
191 ;; counting from END1).
192 (disabled-aliases nil))
193 (re-search-forward "^[^ \t]" end 'move)
194 (beginning-of-line)
195 (skip-chars-backward " \t\n")
196 (setq end1 (point-marker))
197 (goto-char beg1)
198 (while (< (point) end1)
199 (setq pos (point))
200 ;; Reenable any aliases which were disabled for ranges
201 ;; that we have passed out of.
202 (while (and disabled-aliases
203 (> pos (- end1 (cdr (car disabled-aliases)))))
204 (setq disabled-aliases (cdr disabled-aliases)))
205 ;; EPOS gets position of end of next name;
206 ;; SEPLEN gets length of whitespace&separator that follows it.
207 (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t)
208 (setq epos (match-beginning 0)
209 seplen (- (point) epos))
210 (setq epos (marker-position end1) seplen 0))
211 (let ((string (buffer-substring-no-properties pos epos))
212 translation)
213 (if (and (not (assoc string disabled-aliases))
214 (setq translation (cdr (assoc string mail-aliases))))
215 (progn
216 ;; This name is an alias. Disable it.
217 (setq disabled-aliases (cons (cons string (- end1 epos))
218 disabled-aliases))
219 ;; Replace the alias with its expansion
220 ;; then rescan the expansion for more aliases.
221 (goto-char pos)
222 (insert translation)
223 (when exclude
224 (let ((regexp (concat "\\b\\(" exclude "\\)\\b"))
80a677d9
JA
225 (end (point-marker)))
226 (goto-char pos)
227 (while (re-search-forward regexp end t)
228 (replace-match ""))
229 (goto-char end)))
e28449ed
SM
230 (delete-region (point) (+ (point) (- epos pos)))
231 (goto-char pos))
232 ;; Name is not an alias. Skip to start of next name.
233 (goto-char epos)
234 (forward-char seplen))))
235 (set-marker end1 nil)))
236 (set-marker end nil))))
80a677d9 237
9d73ab0d
NF
238;; Called by mail-setup, or similar functions, only if the file specified
239;; by mail-personal-alias-file (usually `~/.mailrc') exists.
80a677d9 240(defun build-mail-aliases (&optional file)
9d73ab0d
NF
241 "Read mail aliases from personal aliases file and set `mail-aliases'.
242By default, this is the file specified by `mail-personal-alias-file'."
243 (setq file (expand-file-name (or file mail-personal-alias-file)))
3c3d71d9
RS
244 ;; In case mail-aliases is t, make sure define-mail-alias
245 ;; does not recursively call build-mail-aliases.
246 (setq mail-aliases nil)
80a677d9
JA
247 (let ((buffer nil)
248 (obuf (current-buffer)))
249 (unwind-protect
250 (progn
48919e0f 251 (setq buffer (generate-new-buffer " mailrc"))
80a677d9 252 (set-buffer buffer)
3c55fda0
RS
253 (while file
254 (cond ((get-file-buffer file)
255 (insert (save-excursion
256 (set-buffer (get-file-buffer file))
599d82c8
RS
257 (buffer-substring-no-properties
258 (point-min) (point-max)))))
3c55fda0
RS
259 ((file-exists-p file) (insert-file-contents file))
260 ((file-exists-p (setq file (concat "~/" file)))
261 (insert-file-contents file))
262 (t (setq file nil)))
263 ;; Don't lose if no final newline.
264 (goto-char (point-max))
265 (or (eq (preceding-char) ?\n) (newline))
266 (goto-char (point-min))
267 ;; handle "\\\n" continuation lines
268 (while (not (eobp))
269 (end-of-line)
270 (if (= (preceding-char) ?\\)
271 (progn (delete-char -1) (delete-char 1) (insert ?\ ))
80a677d9 272 (forward-char 1)))
3c55fda0
RS
273 (goto-char (point-min))
274 ;; handle `source' directives -- Eddy/1994/May/25
275 (cond ((re-search-forward "^source[ \t]+" nil t)
276 (re-search-forward "\\S-+")
599d82c8
RS
277 (setq file (buffer-substring-no-properties
278 (match-beginning 0) (match-end 0)))
3c55fda0
RS
279 (beginning-of-line)
280 (insert "# ") ; to ensure we don't re-process this file
281 (beginning-of-line))
282 (t (setq file nil))))
80a677d9 283 (goto-char (point-min))
d9817d8c 284 (while (re-search-forward
c051dfe5 285 "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t\n]+\\)" nil t)
d9817d8c 286 (let* ((name (match-string 2))
c051dfe5
KH
287 (start (progn (skip-chars-forward " \t") (point)))
288 value)
80a677d9 289 (end-of-line)
c051dfe5
KH
290 (setq value (buffer-substring-no-properties start (point)))
291 (unless (equal value "")
292 (define-mail-alias name value t))))
80a677d9
JA
293 mail-aliases)
294 (if buffer (kill-buffer buffer))
295 (set-buffer obuf))))
296
297;; Always autoloadable in case the user wants to define aliases
298;; interactively or in .emacs.
7229064d 299;;;###autoload
37e379dd 300(defun define-mail-alias (name definition &optional from-mailrc-file)
96846422 301 "Define NAME as a mail alias that translates to DEFINITION.
80a677d9 302This means that sending a message to NAME will actually send to DEFINITION.
fa786521
RS
303
304Normally, the addresses in DEFINITION must be separated by commas.
a1506d29 305If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION
fa786521
RS
306can be separated by spaces; an address can contain spaces
307if it is quoted with double-quotes."
308
80a677d9
JA
309 (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ")
310 ;; Read the defaults first, if we have not done so.
3c3d71d9
RS
311 ;; But not if we are doing that already right now.
312 (unless from-mailrc-file
313 (sendmail-sync-aliases))
80a677d9
JA
314 (if (eq mail-aliases t)
315 (progn
316 (setq mail-aliases nil)
9d73ab0d 317 (if (file-exists-p mail-personal-alias-file)
80a677d9 318 (build-mail-aliases))))
3af1a1f3
RS
319 ;; strip garbage from front and end
320 (if (string-match "\\`[ \t\n,]+" definition)
aa228418 321 (setq definition (substring definition (match-end 0))))
3af1a1f3 322 (if (string-match "[ \t\n,]+\\'" definition)
aa228418 323 (setq definition (substring definition 0 (match-beginning 0))))
37e379dd 324 (let ((result '())
cc4b6c02
RS
325 ;; If DEFINITION is null string, avoid looping even once.
326 (start (and (not (equal definition "")) 0))
37e379dd 327 (L (length definition))
255359cb 328 convert-backslash
37e379dd
RS
329 end tem)
330 (while start
255359cb 331 (setq convert-backslash nil)
37e379dd
RS
332 ;; If we're reading from the mailrc file, then addresses are delimited
333 ;; by spaces, and addresses with embedded spaces must be surrounded by
334 ;; double-quotes. Otherwise, addresses are separated by commas.
335 (if from-mailrc-file
336 (if (eq ?\" (aref definition start))
8332fa80
RS
337 ;; The following test on `found' compensates for a bug
338 ;; in match-end, which does not return nil when match
339 ;; failed.
340 (let ((found (string-match "[^\\]\\(\\([\\][\\]\\)*\\)\"[ \t,]*"
341 definition start)))
342 (setq start (1+ start)
343 end (and found (match-end 1))
344 convert-backslash t))
cc4b6c02
RS
345 (setq end (string-match "[ \t,]+" definition start)))
346 (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start)))
255359cb
RS
347 (let ((temp (substring definition start end))
348 (pos 0))
349 (setq start (and end
350 (/= (match-end 0) L)
351 (match-end 0)))
352 (if convert-backslash
353 (while (string-match "[\\]" temp pos)
354 (setq temp (replace-match "" t t temp))
a1506d29 355 (if start
255359cb
RS
356 (setq start (1- start)))
357 (setq pos (match-end 0))))
358 (setq result (cons temp result))))
37e379dd
RS
359 (setq definition (mapconcat (function identity)
360 (nreverse result)
361 ", "))
80a677d9
JA
362 (setq tem (assoc name mail-aliases))
363 (if tem
364 (rplacd tem definition)
48919e0f
RS
365 (setq mail-aliases (cons (cons name definition) mail-aliases)
366 mail-names t))))
367
c0fd4267 368;;;###autoload
48919e0f
RS
369(defun mail-complete (arg)
370 "Perform completion on header field or word preceding point.
371Completable headers are according to `mail-complete-alist'. If none matches
372current header, calls `mail-complete-function' and passes prefix arg if any."
373 (interactive "P")
9a03a6f4
SM
374 ;; Read the defaults first, if we have not done so.
375 (sendmail-sync-aliases)
376 (if (eq mail-aliases t)
377 (progn
378 (setq mail-aliases nil)
379 (if (file-exists-p mail-personal-alias-file)
380 (build-mail-aliases))))
48919e0f 381 (let ((list mail-complete-alist))
baaddce7 382 (if (and (< 0 (mail-header-end))
48919e0f
RS
383 (save-excursion
384 (if (re-search-backward "^[^\t]" nil t)
385 (while list
386 (if (looking-at (car (car list)))
387 (setq arg (cdr (car list))
388 list ())
389 (setq list (cdr list)))))
390 arg))
391 (let* ((end (point))
392 (beg (save-excursion
393 (skip-chars-backward "^ \t<,:")
394 (point)))
395 (pattern (buffer-substring beg end))
396 completion)
397 (setq list (eval arg)
398 completion (try-completion pattern list))
399 (cond ((eq completion t))
400 ((null completion)
401 (message "Can't find completion for \"%s\"" pattern)
402 (ding))
403 ((not (string= pattern completion))
404 (delete-region beg end)
e694581d
RS
405 (let ((alist-elt (assoc completion mail-names)))
406 (if (cdr alist-elt)
407 (cond ((eq mail-complete-style 'parens)
408 (insert completion " (" (cdr alist-elt) ")"))
409 ((eq mail-complete-style 'angles)
410 (insert (cdr alist-elt) " <" completion ">"))
411 (t
412 (insert completion)))
413 (insert completion))))
48919e0f
RS
414 (t
415 (message "Making completion list...")
416 (with-output-to-temp-buffer "*Completions*"
417 (display-completion-list
418 (all-completions pattern list)))
419 (message "Making completion list...%s" "done"))))
420 (funcall mail-complete-function arg))))
421
422(defun mail-get-names (pattern)
e694581d 423 "Fetch local users and global mail addresses for completion.
48919e0f 424Consults `/etc/passwd' and a directory service if one is set up via
e694581d
RS
425`mail-directory-function'.
426PATTERN is the string we want to complete."
48919e0f
RS
427 (if (eq mail-local-names t)
428 (save-excursion
429 (set-buffer (generate-new-buffer " passwd"))
8e3beae0
RS
430 (let ((files mail-passwd-files))
431 (while files
432 (insert-file-contents (car files) nil nil nil t)
433 (setq files (cdr files))))
434 (if mail-passwd-command
435 (call-process shell-file-name nil t nil
436 shell-command-switch mail-passwd-command))
e694581d 437 (beginning-of-buffer)
8e3beae0 438 (setq mail-local-names nil)
48919e0f 439 (while (not (eobp))
8bc49f8f
RS
440 ;;Recognize lines like
441 ;; nobody:*:65534:65534::/:
442 ;; +demo::::::/bin/csh
443 ;; +ethanb
444 ;;while skipping
445 ;; +@SOFTWARE
e694581d
RS
446 ;; The second \(...\) matches the user id.
447 (if (looking-at "\\+?\\([^:@\n+]+\\):[^:\n]*:\\([^\n:]*\\):")
448 (add-to-list 'mail-local-names
449 (cons (match-string 1)
450 (user-full-name
451 (string-to-int (match-string 2))))))
48919e0f
RS
452 (beginning-of-line 2))
453 (kill-buffer (current-buffer))))
454 (if (or (eq mail-names t)
e694581d 455 (eq mail-directory-names t))
48919e0f
RS
456 (let (directory)
457 (and mail-directory-function
458 (eq mail-directory-names t)
459 (setq directory
460 (mail-directory (if mail-directory-requery pattern))))
e694581d
RS
461 (or mail-directory-requery
462 (setq mail-directory-names directory))
48919e0f
RS
463 (if (or directory
464 (eq mail-names t))
465 (setq mail-names
e694581d
RS
466 (sort (append (if (consp mail-aliases)
467 (mapcar
468 (function (lambda (a) (list (car a))))
469 mail-aliases))
48919e0f
RS
470 (if (consp mail-local-names)
471 mail-local-names)
e694581d 472 (or directory mail-directory-names))
48919e0f
RS
473 (lambda (a b)
474 ;; should cache downcased strings
475 (string< (downcase (car a))
e694581d 476 (downcase (car b)))))))))
48919e0f
RS
477 mail-names)
478
479
480(defun mail-directory (pattern)
25345117 481 "Call directory to get names matching PATTERN or all if nil.
48919e0f
RS
482Calls `mail-directory-function' and applies `mail-directory-parser' to output."
483 (save-excursion
484 (message "Querying directory...")
485 (set-buffer (generate-new-buffer " *mail-directory*"))
486 (funcall mail-directory-function pattern)
487 (goto-char 1)
488 (let (directory)
489 (if (stringp mail-directory-parser)
490 (while (re-search-forward mail-directory-parser nil t)
491 (setq directory
e694581d 492 (cons (match-string 1) directory)))
48919e0f
RS
493 (if mail-directory-parser
494 (setq directory (funcall mail-directory-parser))
495 (while (not (eobp))
496 (setq directory
e694581d
RS
497 (cons (buffer-substring (point)
498 (progn
499 (forward-line)
500 (if (bolp)
501 (1- (point))
502 (point))))
503 directory)))))
48919e0f
RS
504 (kill-buffer (current-buffer))
505 (message "Querying directory...done")
506 directory)))
507
508
509(defun mail-directory-process (pattern)
31407d5d 510 "Run a shell command to output names in directory.
48919e0f
RS
511See `mail-directory-process'."
512 (apply 'call-process (eval (car mail-directory-process)) nil t nil
513 (mapcar 'eval (cdr mail-directory-process))))
514
515;; This should handle a dialog. Currently expects port to spit out names.
516(defun mail-directory-stream (pattern)
517 "Open a stream to retrieve names in directory.
518See `mail-directory-stream'."
519 (let (mailalias-done)
520 (set-process-sentinel
521 (apply 'open-network-stream "mailalias" (current-buffer)
522 mail-directory-stream)
906f211e 523 (lambda (x y)
48919e0f
RS
524 (setq mailalias-done t)))
525 (while (not mailalias-done)
526 (sit-for .1))))
527
528(defun mail-sentto-newsgroups ()
529 "Return all entries from Newsgroups: header as completion alist."
530 (save-excursion
531 (if (mail-position-on-field "newsgroups" t)
532 (let ((point (point))
533 list)
534 (while (< (skip-chars-backward "^:, \t\n") 0)
535 (setq list `((,(buffer-substring (point) point))
536 ,@list))
537 (skip-chars-backward ", \t\n")
538 (setq point (point)))
539 list))))
6594deb0 540
e8a57935
JB
541(provide 'mailalias)
542
ab5796a9 543;;; arch-tag: 1d6a0f87-eb34-4d45-8816-60c1b952cf46
6594deb0 544;;; mailalias.el ends here