Unquote lambda functions. Add autoload cookies to functions formerly
[emacs.git] / lisp / mail / uce.el
blob39a1cecd449246dd1de118974eab198e3d797b14
1 ;;; uce.el --- facilitate reply to unsolicited commercial email
3 ;; Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: stanislav shalunov <shalunov@mccme.ru>
7 ;; Created: 10 Dec 1996
8 ;; Keywords: uce, unsolicited commercial email
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Code in this file provides semi-automatic means of replying to
30 ;; UCE's you might get. It works currently only with Rmail and Gnus.
31 ;; If you would like to make it work with other mail readers,
32 ;; Rmail-specific section is marked below. If you want to play with
33 ;; code, please let me know about your changes so I can incorporate
34 ;; them. I'd appreciate it.
36 ;; Function uce-reply-to-uce, if called when current message in RMAIL
37 ;; buffer is a UCE, will setup *mail* buffer in the following way: it
38 ;; scans full headers of message for 1) normal return address of
39 ;; sender (From, Reply-To lines); and puts these addresses into To:
40 ;; header, it also puts abuse@offenders.host address there 2) mailhub
41 ;; that first saw this message; and puts address of its postmaster
42 ;; into To: header 3) finally, it looks at Message-Id and adds
43 ;; posmaster of that host to the list of addresses.
45 ;; Then, we add "Errors-To: nobody@localhost" header, so that if some
46 ;; of these addresses are not actually correct, we will never see
47 ;; bounced mail. Also, mail-self-blind and mail-archive-file-name
48 ;; take no effect: the ideology is that we don't want to save junk or
49 ;; replies to junk.
51 ;; Then we put template into buffer (customizable message that
52 ;; explains what has happened), customizable signature, and the
53 ;; original message with full headers and envelope for postmasters.
54 ;; Then buffer is left for editing.
56 ;; The reason that function uce-reply-to-uce is Rmail dependant is
57 ;; that we want full headers of the original message, nothing
58 ;; stripped. If we use normal means of inserting of the original
59 ;; message into *mail* buffer headers like Received: (not really
60 ;; headers, but envelope lines) will be stripped while they bear
61 ;; valuable for us and postmasters information. I do wish that there
62 ;; would be some way to write this function in some portable way, but
63 ;; I am not aware of any.
65 ;;; Change log:
67 ;; Dec 10, 1996 -- posted draft version to gnu.sources.emacs
69 ;; Dec 11, 1996 -- fixed some typos, and Francesco Potorti`
70 ;; <F.Potorti@cnuce.cnr.it> pointed out that my use of defvar was
71 ;; weird, suggested fix, and added let form.
73 ;; Dec 17, 1996 -- made scanning for host names little bit more clever
74 ;; (obviously bogus stuff like localhost is now ignored).
76 ;; Nov 11, 1997 -- incorporated changes from Mikael Djurfeldt
77 ;; <mdj@nada.kth.se> to make uce.el work with Gnus. Changed the text
78 ;; of message that is sent.
80 ;; Dec 3, 1997 -- changes from Gareth Jones <gdj1@gdjones.demon.co.uk>
81 ;; handling Received headers following some line like `From:'.
83 ;; Aug 16, 2000 -- changes from Detlev Zundel
84 ;; <detlev.zundel@stud.uni-karlsruhe.de> to make uce.el work with the
85 ;; latest Gnus. Lars told him it should work for all versions of Gnus
86 ;; younger than three years.
88 ;; Setup:
90 ;; Add the following line to your ~/.emacs:
92 ;; (autoload 'uce-reply-to-uce "uce" "Reply to UCEs" t nil)
94 ;; If you want to use it with Gnus also use
96 ;; (setq uce-mail-reader 'gnus)
98 ;; store this file (uce.el) somewhere in load-path and byte-compile it.
100 ;;; Variables:
102 ;; uce-message-text is template that will be inserted into buffer. It
103 ;; has reasonable default. If you want to write some scarier one,
104 ;; please do so and send it to me. Please keep it polite.
106 ;; uce-signature behaves just like mail-signature. If nil, nothing is
107 ;; inserted, if t, file ~/.signature is used, if a string, its
108 ;; contents are inserted into buffer.
110 ;; uce-uce-separator is line that separates your message from the UCE
111 ;; that you enclose.
113 ;; uce-subject-line will be used as subject of outgoing message. If
114 ;; nil, left blank.
116 ;;; Code:
118 (defvar gnus-original-article-buffer)
119 (defvar mail-reply-buffer)
120 (defvar rmail-current-message)
122 (require 'sendmail)
123 ;; Those sections of code which are dependent upon
124 ;; RMAIL are only evaluated if we have received a message with RMAIL...
125 ;;(require 'rmail)
127 (defgroup uce nil
128 "Facilitate reply to unsolicited commercial email."
129 :prefix "uce-"
130 :group 'mail)
132 (defcustom uce-mail-reader 'rmail
133 "A symbol indicating which mail reader you are using.
134 Choose from: `gnus', `rmail'."
135 :type '(choice (const gnus) (const rmail))
136 :version "20.3"
137 :group 'uce)
139 (defcustom uce-setup-hook nil
140 "Hook to run after UCE rant message is composed.
141 This hook is run after `mail-setup-hook', which is run as well."
142 :type 'hook
143 :group 'uce)
145 (defcustom uce-message-text
146 "Recently, I have received an Unsolicited Commercial E-mail from you.
147 I do not like UCE's and I would like to inform you that sending
148 unsolicited messages to someone while he or she may have to pay for
149 reading your message may be illegal. Anyway, it is highly annoying
150 and not welcome by anyone. It is rude, after all.
152 If you think that this is a good way to advertise your products or
153 services you are mistaken. Spamming will only make people hate you, not
154 buy from you.
156 If you have any list of people you send unsolicited commercial emails to,
157 REMOVE me from such list immediately. I suggest that you make this list
158 just empty.
160 ----------------------------------------------------
162 If you are not an administrator of any site and still have received
163 this message then your email address is being abused by some spammer.
164 They fake your address in From: or Reply-To: header. In this case,
165 you might want to show this message to your system administrator, and
166 ask him/her to investigate this matter.
168 Note to the postmaster(s): I append the text of UCE in question to
169 this message; I would like to hear from you about action(s) taken.
170 This message has been sent to postmasters at the host that is
171 mentioned as original sender's host (I do realize that it may be
172 faked, but I think that if your domain name is being abused this way
173 you might want to learn about it, and take actions) and to the
174 postmaster whose host was used as mail relay for this message. If
175 message was sent not by your user, could you please compare time when
176 this message was sent (use time in Received: field of the envelope
177 rather than Date: field) with your sendmail logs and see what host was
178 using your sendmail at this moment of time.
180 Thank you."
182 "This is the text that `uce-reply-to-uce' command will put in reply buffer.
183 Some of spamming programs in use will be set up to read all incoming
184 to spam address email, and will remove people who put the word `remove'
185 on beginning of some line from the spamming list. So, when you set it
186 up, it might be a good idea to actually use this feature.
188 Value nil means insert no text by default, lets you type it in."
189 :type 'string
190 :group 'uce)
192 (defcustom uce-uce-separator
193 "----- original unsolicited commercial email follows -----"
194 "Line that will begin quoting of the UCE.
195 Value nil means use no separator."
196 :type '(choice (const nil) string)
197 :group 'uce)
199 (defcustom uce-signature mail-signature
200 "Text to put as your signature after the note to UCE sender.
201 Value nil means none, t means insert `~/.signature' file (if it happens
202 to exist), if this variable is a string this string will be inserted
203 as your signature."
204 :type '(choice (const nil) (const t) string)
205 :group 'uce)
207 (defcustom uce-default-headers
208 "Errors-To: nobody@localhost\nPrecedence: bulk\n"
209 "Additional headers to use when responding to a UCE with \\[uce-reply-to-uce].
210 These are mostly meant for headers that prevent delivery errors reporting."
211 :type 'string
212 :group 'uce)
214 (defcustom uce-subject-line
215 "Spam alert: unsolicited commercial e-mail"
216 "Subject of the message that will be sent in response to a UCE."
217 :type 'string
218 :group 'uce)
220 (declare-function mail-strip-quoted-names "mail-utils" (address))
221 (declare-function rmail-msg-is-pruned "rmail" ())
222 (declare-function rmail-maybe-set-message-counters "rmail" ())
223 (declare-function rmail-msgbeg "rmail" (n))
224 (declare-function rmail-msgend "rmail" (n))
225 (declare-function rmail-toggle-header "rmail" (&optional arg))
228 (defun uce-reply-to-uce (&optional ignored)
229 "Send reply to UCE in Rmail.
230 UCE stands for unsolicited commercial email. Function will set up reply
231 buffer with default To: to the sender, his postmaster, his abuse@
232 address, and postmaster of the mail relay used."
233 (interactive)
234 (let ((message-buffer
235 (cond ((eq uce-mail-reader 'gnus) gnus-original-article-buffer)
236 ((eq uce-mail-reader 'rmail) "RMAIL")
237 (t (error
238 "Variable uce-mail-reader set to unrecognized value"))))
239 (full-header-p (and (eq uce-mail-reader 'rmail)
240 (not (rmail-msg-is-pruned)))))
241 (or (get-buffer message-buffer)
242 (error "No buffer %s, cannot find UCE" message-buffer))
243 (switch-to-buffer message-buffer)
244 ;; We need the message with headers pruned.
245 (if full-header-p
246 (rmail-toggle-header 1))
247 (let ((to (mail-strip-quoted-names (mail-fetch-field "from" t)))
248 (reply-to (mail-fetch-field "reply-to"))
249 temp)
250 ;; Initial setting of the list of recipients of our message; that's
251 ;; what they are pretending to be.
252 (if to
253 (setq to (format "%s" (mail-strip-quoted-names to)))
254 (setq to ""))
255 (if reply-to
256 (setq to (format "%s, %s" to (mail-strip-quoted-names reply-to))))
257 (let (first-at-sign end-of-hostname sender-host)
258 (setq first-at-sign (string-match "@" to)
259 end-of-hostname (string-match "[ ,>]" to first-at-sign)
260 sender-host (substring to first-at-sign end-of-hostname))
261 (if (string-match "\\." sender-host)
262 (setq to (format "%s, postmaster%s, abuse%s"
263 to sender-host sender-host))))
264 (setq mail-send-actions nil)
265 (setq mail-reply-buffer nil)
266 (cond ((eq uce-mail-reader 'gnus)
267 (copy-region-as-kill (point-min) (point-max)))
268 ((eq uce-mail-reader 'rmail)
269 (save-excursion
270 (save-restriction
271 (rmail-toggle-header 1)
272 (widen)
273 (rmail-maybe-set-message-counters)
274 (copy-region-as-kill (rmail-msgbeg rmail-current-message)
275 (rmail-msgend rmail-current-message))))))
276 ;; Restore the pruned header state we found.
277 (if full-header-p
278 (rmail-toggle-header 0))
279 (switch-to-buffer "*mail*")
280 (erase-buffer)
281 (setq temp (point))
282 (yank)
283 (goto-char temp)
284 (if (eq uce-mail-reader 'rmail)
285 (progn
286 (forward-line 2)
287 (let ((case-fold-search t))
288 (while (looking-at "Summary-Line:\\|Mail-From:")
289 (forward-line 1)))
290 (delete-region temp (point))))
291 ;; Now find the mail hub that first accepted this message.
292 ;; This should try to find the last Received: header.
293 ;; Sometimes there may be other headers inbetween Received: headers.
294 (cond ((eq uce-mail-reader 'gnus)
295 ;; Does Gnus always have Lines: in the end?
296 (re-search-forward "^Lines:")
297 (beginning-of-line))
298 ((eq uce-mail-reader 'rmail)
299 (goto-char (point-min))
300 (search-forward "*** EOOH ***\n")
301 (beginning-of-line)
302 (forward-line -1)))
303 (re-search-backward "^Received:")
304 (beginning-of-line)
305 ;; Is this always good? It's the only thing I saw when I checked
306 ;; a few messages.
307 (let ((eol (save-excursion (end-of-line) (point))))
308 ;;(if (not (re-search-forward ": \\(from\\|by\\) " eol t))
309 (if (not (re-search-forward "\\(from\\|by\\) " eol t))
310 (progn
311 (goto-char eol)
312 (if (looking-at "[ \t\n]+\\(from\\|by\\) ")
313 (goto-char (match-end 0))
314 (error "Failed to extract hub address")))))
315 (setq temp (point))
316 (search-forward " ")
317 (forward-char -1)
318 ;; And add its postmaster to the list of addresses.
319 (if (string-match "\\." (buffer-substring temp (point)))
320 (setq to (format "%s, postmaster@%s"
321 to (buffer-substring temp (point)))))
322 ;; Also look at the message-id, it helps *very* often.
323 (if (and (search-forward "\nMessage-Id: " nil t)
324 ;; Not all Message-Id:'s have an `@' sign.
325 (let ((bol (point))
326 eol)
327 (end-of-line)
328 (setq eol (point))
329 (goto-char bol)
330 (search-forward "@" eol t)))
331 (progn
332 (setq temp (point))
333 (search-forward ">")
334 (forward-char -1)
335 (if (string-match "\\." (buffer-substring temp (point)))
336 (setq to (format "%s, postmaster@%s"
337 to (buffer-substring temp (point)))))))
338 (cond ((eq uce-mail-reader 'gnus)
339 ;; Does Gnus always have Lines: in the end?
340 (re-search-forward "^Lines:")
341 (beginning-of-line))
342 ((eq uce-mail-reader 'rmail)
343 (search-forward "\n*** EOOH ***\n")
344 (forward-line -1)))
345 (setq temp (point))
346 (search-forward "\n\n" nil t)
347 (if (eq uce-mail-reader 'gnus)
348 (forward-line -1))
349 (delete-region temp (point))
350 ;; End of Rmail dependent section.
351 (auto-save-mode auto-save-default)
352 (mail-mode)
353 (goto-char (point-min))
354 (insert "To: ")
355 (save-excursion
356 (if to
357 (let ((fill-prefix "\t")
358 (address-start (point)))
359 (insert to "\n")
360 (fill-region-as-paragraph address-start (point)))
361 (newline))
362 (insert "Subject: " uce-subject-line "\n")
363 (if uce-default-headers
364 (insert uce-default-headers))
365 (if mail-default-headers
366 (insert mail-default-headers))
367 (if mail-default-reply-to
368 (insert "Reply-to: " mail-default-reply-to "\n"))
369 (insert mail-header-separator "\n")
370 ;; Insert all our text. Then go back to the place where we started.
371 (if to (setq to (point)))
372 ;; Text of ranting.
373 (if uce-message-text
374 (insert uce-message-text))
375 ;; Signature.
376 (cond ((eq uce-signature t)
377 (if (file-exists-p "~/.signature")
378 (progn
379 (insert "\n\n-- \n")
380 (forward-char (cadr (insert-file-contents "~/.signature"))))))
381 (uce-signature
382 (insert "\n\n-- \n" uce-signature)))
383 ;; And text of the original message.
384 (if uce-uce-separator
385 (insert "\n\n" uce-uce-separator "\n"))
386 ;; If message doesn't end with a newline, insert it.
387 (goto-char (point-max))
388 (or (bolp) (newline)))
389 ;; And go back to the beginning of text.
390 (if to (goto-char to))
391 (or to (set-buffer-modified-p nil))
392 ;; Run hooks before we leave buffer for editing. Reasonable usage
393 ;; might be to set up special key bindings, replace standart
394 ;; functions in mail-mode, etc.
395 (run-hooks 'mail-setup-hook 'uce-setup-hook))))
397 (defun uce-insert-ranting (&optional ignored)
398 "Insert text of the usual reply to UCE into current buffer."
399 (interactive "P")
400 (insert uce-message-text))
402 (provide 'uce)
404 ;;; arch-tag: 44b68c87-9b29-47bd-822c-3feee3883221
405 ;;; uce.el ends here