]> code.delx.au - gnu-emacs/blob - lisp/mail/metamail.el
(rmail-spam-filter): Only check white list if `message-sender' is non-nil.
[gnu-emacs] / lisp / mail / metamail.el
1 ;;; metamail.el --- Metamail interface for GNU Emacs
2
3 ;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
6 ;; Keywords: mail, news, mime, multimedia
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 ;; Note: Metamail does not have all the options which are compatible with
28 ;; the environment variables. For that reason, metamail.el has to
29 ;; hack the environment variables. In addition, there is no way to
30 ;; display all header fields without extra informative body messages
31 ;; which are suppressed by the "-q" option.
32
33 ;; The idea of using metamail to process MIME messages is from
34 ;; gnus-mime.el by Spike <Spike@world.std.com>.
35
36 ;;; Code:
37
38 (defgroup metamail nil
39 "Metamail interface for Emacs."
40 :group 'mail
41 :group 'hypermedia
42 :group 'processes)
43
44 (defcustom metamail-program-name "metamail"
45 "*Metamail program name."
46 :type 'string
47 :group 'metamail)
48
49 (defcustom metamail-mailer-name "emacs"
50 "*Mailer name set to MM_MAILER environment variable."
51 :type 'string
52 :group 'metamail)
53
54 (defvar metamail-environment '("KEYHEADS=*" "MM_QUIET=1")
55 "*Environment variables passed to `metamail'.
56 It must be a list of strings that have the format ENVVARNAME=VALUE.
57 It is not expected to be altered globally by `set' or `setq'.
58 Instead, change its value temporary using `let' or `let*' form.")
59
60 (defcustom metamail-switches '("-x" "-d" "-z")
61 "*Switches for `metamail' program.
62 `-z' is required to remove zap file.
63 It is not expected to be altered globally by `set' or `setq'.
64 Instead, change its value temporary using `let' or `let*' form.
65 `-m MAILER' argument is automatically generated from the
66 `metamail-mailer-name' variable."
67 :type '(repeat (string :tag "Switch"))
68 :group 'metamail)
69
70 ;;;###autoload
71 (defun metamail-interpret-header ()
72 "Interpret a header part of a MIME message in current buffer.
73 Its body part is not interpreted at all."
74 (interactive)
75 (save-excursion
76 (let* ((buffer-read-only nil)
77 (metamail-switches ;Inhibit processing an empty body.
78 (append metamail-switches '("-c" "text/plain" "-E" "7bit")))
79 (end (progn
80 (goto-char (point-min))
81 (search-forward "\n\n" nil 'move)
82 ;; An extra newline is inserted by metamail if there
83 ;; is no body part. So, insert a dummy body by
84 ;; itself.
85 (insert "\n")
86 (point))))
87 (metamail-region (point-min) end nil nil 'nodisplay)
88 ;; Remove an extra newline inserted by myself.
89 (goto-char (point-min))
90 (if (search-forward "\n\n\n" nil t)
91 (delete-char -1))
92 )))
93
94 ;;;###autoload
95 (defun metamail-interpret-body (&optional viewmode nodisplay)
96 "Interpret a body part of a MIME message in current buffer.
97 Optional argument VIEWMODE specifies the value of the
98 EMACS_VIEW_MODE environment variable (defaulted to 1).
99 Optional argument NODISPLAY non-nil means buffer is not
100 redisplayed as output is inserted.
101 Its header part is not interpreted at all."
102 (interactive "p")
103 (save-excursion
104 (let ((contype nil)
105 (encoding nil)
106 (end (progn
107 (goto-char (point-min))
108 (search-forward "\n\n" nil t)
109 (point))))
110 ;; Find Content-Type and Content-Transfer-Encoding from the header.
111 (save-restriction
112 (narrow-to-region (point-min) end)
113 (setq contype
114 (or (mail-fetch-field "Content-Type") "text/plain"))
115 (setq encoding
116 (or (mail-fetch-field "Content-Transfer-Encoding") "7bit")))
117 ;; Interpret the body part only.
118 (let ((metamail-switches ;Process body part only.
119 (append metamail-switches
120 (list "-b" "-c" contype "-E" encoding))))
121 (metamail-region end (point-max) viewmode nil nodisplay))
122 ;; Mode specific hack.
123 (cond ((eq major-mode 'rmail-mode)
124 ;; Adjust the marker of this message if in Rmail mode buffer.
125 (set-marker (aref rmail-message-vector (1+ rmail-current-message))
126 (point-max))))
127 )))
128
129 ;;;###autoload
130 (defun metamail-buffer (&optional viewmode buffer nodisplay)
131 "Process current buffer through `metamail'.
132 Optional argument VIEWMODE specifies the value of the
133 EMACS_VIEW_MODE environment variable (defaulted to 1).
134 Optional argument BUFFER specifies a buffer to be filled (nil
135 means current).
136 Optional argument NODISPLAY non-nil means buffer is not
137 redisplayed as output is inserted."
138 (interactive "p")
139 (metamail-region (point-min) (point-max) viewmode buffer nodisplay))
140
141 ;;;###autoload
142 (defun metamail-region (beg end &optional viewmode buffer nodisplay)
143 "Process current region through 'metamail'.
144 Optional argument VIEWMODE specifies the value of the
145 EMACS_VIEW_MODE environment variable (defaulted to 1).
146 Optional argument BUFFER specifies a buffer to be filled (nil
147 means current).
148 Optional argument NODISPLAY non-nil means buffer is not
149 redisplayed as output is inserted."
150 (interactive "r\np")
151 (let ((curbuf (current-buffer))
152 (buffer-read-only nil)
153 (metafile (make-temp-file "metamail"))
154 (option-environment
155 (list (format "EMACS_VIEW_MODE=%d"
156 (if (numberp viewmode) viewmode 1)))))
157 (save-excursion
158 ;; Gee! Metamail does not ouput to stdout if input comes from
159 ;; stdin.
160 (let ((selective-display nil)) ;Disable ^M to nl translation.
161 (write-region beg end metafile nil 'nomessage))
162 (if buffer
163 (set-buffer buffer))
164 (setq buffer-read-only nil)
165 ;; Clear destination buffer.
166 (if (eq curbuf (current-buffer))
167 (delete-region beg end)
168 (delete-region (point-min) (point-max)))
169 ;; We have to pass the environment variable KEYHEADS to display
170 ;; all header fields. Metamail should have an optional argument
171 ;; to pass such information directly.
172 (let ((process-environment
173 (append process-environment
174 metamail-environment option-environment))
175 (coding-system-for-read 'undecided))
176 (apply (function call-process)
177 metamail-program-name
178 nil
179 t ;Output to current buffer
180 (not nodisplay) ;Force redisplay
181 (append metamail-switches
182 (list "-m" (or metamail-mailer-name "emacs"))
183 (list metafile))))
184 ;; `metamail' may not delete the temporary file!
185 (condition-case error
186 (delete-file metafile)
187 (error nil))
188 )))
189
190 (provide 'metamail)
191
192 ;;; arch-tag: 52c0cb6f-d800-4776-9789-f0275cb5490e
193 ;;; metamail.el ends here