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