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