| 1 | ;;; mailalias.el --- expand mailing address aliases defined in ~/.mailrc. |
| 2 | |
| 3 | ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: FSF |
| 6 | ;; Keywords: mail |
| 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 the |
| 22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | ;; Boston, MA 02111-1307, USA. |
| 24 | |
| 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 | |
| 31 | ;;; Code: |
| 32 | |
| 33 | (require 'sendmail) |
| 34 | |
| 35 | ;; Called from sendmail-send-it, or similar functions, |
| 36 | ;; only if some mail aliases are defined. |
| 37 | (defun expand-mail-aliases (beg end &optional exclude) |
| 38 | "Expand all mail aliases in suitable header fields found between BEG and END. |
| 39 | Suitable header fields are `To', `From', `CC' and `BCC', `Reply-to', and |
| 40 | their `Resent-' variants. |
| 41 | |
| 42 | Optional second arg EXCLUDE may be a regular expression defining text to be |
| 43 | removed from alias expansions." |
| 44 | (sendmail-sync-aliases) |
| 45 | (if (eq mail-aliases t) |
| 46 | (progn (setq mail-aliases nil) (build-mail-aliases))) |
| 47 | (goto-char beg) |
| 48 | (setq end (set-marker (make-marker) end)) |
| 49 | (let ((case-fold-search nil)) |
| 50 | (while (let ((case-fold-search t)) |
| 51 | (re-search-forward "^\\(Resent-\\)?\\(To\\|From\\|CC\\|BCC\\|Reply-to\\):" end t)) |
| 52 | (skip-chars-forward " \t") |
| 53 | (let ((beg1 (point)) |
| 54 | end1 pos epos seplen |
| 55 | ;; DISABLED-ALIASES records aliases temporarily disabled |
| 56 | ;; while we scan text that resulted from expanding those aliases. |
| 57 | ;; Each element is (ALIAS . TILL-WHEN), where TILL-WHEN |
| 58 | ;; is where to reenable the alias (expressed as number of chars |
| 59 | ;; counting from END1). |
| 60 | (disabled-aliases nil)) |
| 61 | (re-search-forward "^[^ \t]" end 'move) |
| 62 | (beginning-of-line) |
| 63 | (skip-chars-backward " \t\n") |
| 64 | (setq end1 (point-marker)) |
| 65 | (goto-char beg1) |
| 66 | (while (< (point) end1) |
| 67 | (setq pos (point)) |
| 68 | ;; Reenable any aliases which were disabled for ranges |
| 69 | ;; that we have passed out of. |
| 70 | (while (and disabled-aliases (> pos (- end1 (cdr (car disabled-aliases))))) |
| 71 | (setq disabled-aliases (cdr disabled-aliases))) |
| 72 | ;; EPOS gets position of end of next name; |
| 73 | ;; SEPLEN gets length of whitespace&separator that follows it. |
| 74 | (if (re-search-forward "[ \t]*[\n,][ \t]*" end1 t) |
| 75 | (setq epos (match-beginning 0) |
| 76 | seplen (- (point) epos)) |
| 77 | (setq epos (marker-position end1) seplen 0)) |
| 78 | (let (translation |
| 79 | (string (buffer-substring-no-properties pos epos))) |
| 80 | (if (and (not (assoc string disabled-aliases)) |
| 81 | (setq translation |
| 82 | (cdr (assoc string mail-aliases)))) |
| 83 | (progn |
| 84 | ;; This name is an alias. Disable it. |
| 85 | (setq disabled-aliases (cons (cons string (- end1 epos)) |
| 86 | disabled-aliases)) |
| 87 | ;; Replace the alias with its expansion |
| 88 | ;; then rescan the expansion for more aliases. |
| 89 | (goto-char pos) |
| 90 | (insert translation) |
| 91 | (if exclude |
| 92 | (let ((regexp |
| 93 | (concat "\\b\\(" exclude "\\)\\b")) |
| 94 | (end (point-marker))) |
| 95 | (goto-char pos) |
| 96 | (while (re-search-forward regexp end t) |
| 97 | (replace-match "")) |
| 98 | (goto-char end))) |
| 99 | (delete-region (point) (+ (point) (- epos pos))) |
| 100 | (goto-char pos)) |
| 101 | ;; Name is not an alias. Skip to start of next name. |
| 102 | (goto-char epos) |
| 103 | (forward-char seplen)))) |
| 104 | (set-marker end1 nil))) |
| 105 | (set-marker end nil))) |
| 106 | |
| 107 | ;; Called by mail-setup, or similar functions, only if the file specified |
| 108 | ;; by mail-personal-alias-file (usually `~/.mailrc') exists. |
| 109 | (defun build-mail-aliases (&optional file) |
| 110 | "Read mail aliases from personal aliases file and set `mail-aliases'. |
| 111 | By default, this is the file specified by `mail-personal-alias-file'." |
| 112 | (setq file (expand-file-name (or file mail-personal-alias-file))) |
| 113 | (let ((buffer nil) |
| 114 | (obuf (current-buffer))) |
| 115 | (unwind-protect |
| 116 | (progn |
| 117 | (setq buffer (generate-new-buffer "mailrc")) |
| 118 | (buffer-disable-undo buffer) |
| 119 | (set-buffer buffer) |
| 120 | (while file |
| 121 | (cond ((get-file-buffer file) |
| 122 | (insert (save-excursion |
| 123 | (set-buffer (get-file-buffer file)) |
| 124 | (buffer-substring-no-properties |
| 125 | (point-min) (point-max))))) |
| 126 | ((file-exists-p file) (insert-file-contents file)) |
| 127 | ((file-exists-p (setq file (concat "~/" file))) |
| 128 | (insert-file-contents file)) |
| 129 | (t (setq file nil))) |
| 130 | ;; Don't lose if no final newline. |
| 131 | (goto-char (point-max)) |
| 132 | (or (eq (preceding-char) ?\n) (newline)) |
| 133 | (goto-char (point-min)) |
| 134 | ;; handle "\\\n" continuation lines |
| 135 | (while (not (eobp)) |
| 136 | (end-of-line) |
| 137 | (if (= (preceding-char) ?\\) |
| 138 | (progn (delete-char -1) (delete-char 1) (insert ?\ )) |
| 139 | (forward-char 1))) |
| 140 | (goto-char (point-min)) |
| 141 | ;; handle `source' directives -- Eddy/1994/May/25 |
| 142 | (cond ((re-search-forward "^source[ \t]+" nil t) |
| 143 | (re-search-forward "\\S-+") |
| 144 | (setq file (buffer-substring-no-properties |
| 145 | (match-beginning 0) (match-end 0))) |
| 146 | (beginning-of-line) |
| 147 | (insert "# ") ; to ensure we don't re-process this file |
| 148 | (beginning-of-line)) |
| 149 | (t (setq file nil)))) |
| 150 | (goto-char (point-min)) |
| 151 | (while (re-search-forward |
| 152 | "^\\(a\\|alias\\|g\\|group\\)[ \t]+\\([^ \t]+\\)" nil t) |
| 153 | (let* ((name (match-string 2)) |
| 154 | (start (progn (skip-chars-forward " \t") (point)))) |
| 155 | (end-of-line) |
| 156 | (define-mail-alias |
| 157 | name |
| 158 | (buffer-substring-no-properties start (point)) |
| 159 | t))) |
| 160 | mail-aliases) |
| 161 | (if buffer (kill-buffer buffer)) |
| 162 | (set-buffer obuf)))) |
| 163 | |
| 164 | ;; Always autoloadable in case the user wants to define aliases |
| 165 | ;; interactively or in .emacs. |
| 166 | ;;;###autoload |
| 167 | (defun define-mail-alias (name definition &optional from-mailrc-file) |
| 168 | "Define NAME as a mail alias that translates to DEFINITION. |
| 169 | This means that sending a message to NAME will actually send to DEFINITION. |
| 170 | |
| 171 | Normally, the addresses in DEFINITION must be separated by commas. |
| 172 | If FROM-MAILRC-FILE is non-nil, then addresses in DEFINITION |
| 173 | can be separated by spaces; an address can contain spaces |
| 174 | if it is quoted with double-quotes." |
| 175 | |
| 176 | (interactive "sDefine mail alias: \nsDefine %s as mail alias for: ") |
| 177 | ;; Read the defaults first, if we have not done so. |
| 178 | (sendmail-sync-aliases) |
| 179 | (if (eq mail-aliases t) |
| 180 | (progn |
| 181 | (setq mail-aliases nil) |
| 182 | (if (file-exists-p mail-personal-alias-file) |
| 183 | (build-mail-aliases)))) |
| 184 | ;; strip garbage from front and end |
| 185 | (if (string-match "\\`[ \t\n,]+" definition) |
| 186 | (setq definition (substring definition (match-end 0)))) |
| 187 | (if (string-match "[ \t\n,]+\\'" definition) |
| 188 | (setq definition (substring definition 0 (match-beginning 0)))) |
| 189 | (let ((result '()) |
| 190 | ;; If DEFINITION is null string, avoid looping even once. |
| 191 | (start (and (not (equal definition "")) 0)) |
| 192 | (L (length definition)) |
| 193 | end tem) |
| 194 | (while start |
| 195 | ;; If we're reading from the mailrc file, then addresses are delimited |
| 196 | ;; by spaces, and addresses with embedded spaces must be surrounded by |
| 197 | ;; double-quotes. Otherwise, addresses are separated by commas. |
| 198 | (if from-mailrc-file |
| 199 | (if (eq ?\" (aref definition start)) |
| 200 | (setq start (1+ start) |
| 201 | end (string-match "\"[ \t,]*" definition start)) |
| 202 | (setq end (string-match "[ \t,]+" definition start))) |
| 203 | (setq end (string-match "[ \t\n,]*,[ \t\n,]*" definition start))) |
| 204 | (setq result (cons (substring definition start end) result)) |
| 205 | (setq start (and end |
| 206 | (/= (match-end 0) L) |
| 207 | (match-end 0)))) |
| 208 | (setq definition (mapconcat (function identity) |
| 209 | (nreverse result) |
| 210 | ", ")) |
| 211 | (setq tem (assoc name mail-aliases)) |
| 212 | (if tem |
| 213 | (rplacd tem definition) |
| 214 | (setq mail-aliases (cons (cons name definition) mail-aliases))))) |
| 215 | |
| 216 | (provide 'mailalias) |
| 217 | |
| 218 | ;;; mailalias.el ends here |