1 ;;; uce.el --- facilitate reply to unsolicited commercial email
3 ;; Copyright (C) 1996, 1998, 2000 Free Software Foundation, Inc.
5 ;; Author: stanislav shalunov <shalunov@mccme.ru>
6 ;; Created: 10 Dec 1996
7 ;; Keywords: uce, unsolicited commercial email
9 ;; This file is part of GNU Emacs.
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; without any warranty; without even the implied warranty of
18 ;; merchantability or fitness for a particular purpose. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Code in this file provides semi-automatic means of replying to
29 ;; UCE's you might get. It works currently only with Rmail and Gnus.
30 ;; If you would like to make it work with other mail readers,
31 ;; Rmail-specific section is marked below. If you want to play with
32 ;; code, please let me know about your changes so I can incorporate
33 ;; them. I'd appreciate it.
35 ;; Function uce-reply-to-uce, if called when current message in RMAIL
36 ;; buffer is a UCE, will setup *mail* buffer in the following way: it
37 ;; scans full headers of message for 1) normal return address of
38 ;; sender (From, Reply-To lines); and puts these addresses into To:
39 ;; header, it also puts abuse@offenders.host address there 2) mailhub
40 ;; that first saw this message; and puts address of its postmaster
41 ;; into To: header 3) finally, it looks at Message-Id and adds
42 ;; posmaster of that host to the list of addresses.
44 ;; Then, we add "Errors-To: nobody@localhost" header, so that if some
45 ;; of these addresses are not actually correct, we will never see
46 ;; bounced mail. Also, mail-self-blind and mail-archive-file-name
47 ;; take no effect: the ideology is that we don't want to save junk or
50 ;; Then we put template into buffer (customizable message that
51 ;; explains what has happened), customizable signature, and the
52 ;; original message with full headers and envelope for postmasters.
53 ;; Then buffer is left for editing.
55 ;; The reason that function uce-reply-to-uce is Rmail dependant is
56 ;; that we want full headers of the original message, nothing
57 ;; stripped. If we use normal means of inserting of the original
58 ;; message into *mail* buffer headers like Received: (not really
59 ;; headers, but envelope lines) will be stripped while they bear
60 ;; valuable for us and postmasters information. I do wish that there
61 ;; would be some way to write this function in some portable way, but
62 ;; I am not aware of any.
66 ;; Dec 10, 1996 -- posted draft version to gnu.sources.emacs
68 ;; Dec 11, 1996 -- fixed some typos, and Francesco Potorti`
69 ;; <F.Potorti@cnuce.cnr.it> pointed out that my use of defvar was
70 ;; weird, suggested fix, and added let form.
72 ;; Dec 17, 1996 -- made scanning for host names little bit more clever
73 ;; (obviously bogus stuff like localhost is now ignored).
75 ;; Nov 11, 1997 -- incorporated changes from Mikael Djurfeldt
76 ;; <mdj@nada.kth.se> to make uce.el work with Gnus. Changed the text
77 ;; of message that is sent.
79 ;; Dec 3, 1997 -- changes from Gareth Jones <gdj1@gdjones.demon.co.uk>
80 ;; handling Received headers following some line like `From:'.
82 ;; Aug 16, 2000 -- changes from Detlev Zundel
83 ;; <detlev.zundel@stud.uni-karlsruhe.de> to make uce.el work with the
84 ;; latest Gnus. Lars told him it should work for all versions of Gnus
85 ;; younger than three years.
89 ;; Add the following line to your ~/.emacs:
91 ;; (autoload 'uce-reply-to-uce "uce" "Reply to UCEs" t nil)
93 ;; If you want to use it with Gnus also use
95 ;; (setq uce-mail-reader 'gnus)
97 ;; store this file (uce.el) somewhere in load-path and byte-compile it.
101 ;; uce-message-text is template that will be inserted into buffer. It
102 ;; has reasonable default. If you want to write some scarier one,
103 ;; please do so and send it to me. Please keep it polite.
105 ;; uce-signature behaves just like mail-signature. If nil, nothing is
106 ;; inserted, if t, file ~/.signature is used, if a string, its
107 ;; contents are inserted into buffer.
109 ;; uce-uce-separator is line that separates your message from the UCE
112 ;; uce-subject-line will be used as subject of outgoing message. If
118 ;; Those sections of code which are dependent upon
119 ;; RMAIL are only evaluated if we have received a message with RMAIL...
123 "Facilitate reply to unsolicited commercial email."
127 (defcustom uce-mail-reader
'rmail
128 "A symbol indicating which mail reader you are using.
129 Choose from: `gnus', `rmail'."
130 :type
'(choice (const gnus
) (const rmail
))
134 (defcustom uce-setup-hook nil
135 "Hook to run after UCE rant message is composed.
136 This hook is run after `mail-setup-hook', which is run as well."
140 (defcustom uce-message-text
141 "Recently, I have received an Unsolicited Commercial E-mail from you.
142 I do not like UCE's and I would like to inform you that sending
143 unsolicited messages to someone while he or she may have to pay for
144 reading your message may be illegal. Anyway, it is highly annoying
145 and not welcome by anyone. It is rude, after all.
147 If you think that this is a good way to advertise your products or
148 services you are mistaken. Spamming will only make people hate you, not
151 If you have any list of people you send unsolicited commercial emails to,
152 REMOVE me from such list immediately. I suggest that you make this list
155 ----------------------------------------------------
157 If you are not an administrator of any site and still have received
158 this message then your email address is being abused by some spammer.
159 They fake your address in From: or Reply-To: header. In this case,
160 you might want to show this message to your system administrator, and
161 ask him/her to investigate this matter.
163 Note to the postmaster(s): I append the text of UCE in question to
164 this message; I would like to hear from you about action(s) taken.
165 This message has been sent to postmasters at the host that is
166 mentioned as original sender's host (I do realize that it may be
167 faked, but I think that if your domain name is being abused this way
168 you might want to learn about it, and take actions) and to the
169 postmaster whose host was used as mail relay for this message. If
170 message was sent not by your user, could you please compare time when
171 this message was sent (use time in Received: field of the envelope
172 rather than Date: field) with your sendmail logs and see what host was
173 using your sendmail at this moment of time.
177 "This is the text that `uce-reply-to-uce' command will put in reply buffer.
178 Some of spamming programs in use will be set up to read all incoming
179 to spam address email, and will remove people who put the word `remove'
180 on beginning of some line from the spamming list. So, when you set it
181 up, it might be a good idea to actually use this feature.
183 Value nil means insert no text by default, lets you type it in."
187 (defcustom uce-uce-separator
188 "----- original unsolicited commercial email follows -----"
189 "Line that will begin quoting of the UCE.
190 Value nil means use no separator."
191 :type
'(choice (const nil
) string
)
194 (defcustom uce-signature mail-signature
195 "Text to put as your signature after the note to UCE sender.
196 Value nil means none, t means insert `~/.signature' file (if it happens
197 to exist), if this variable is a string this string will be inserted
199 :type
'(choice (const nil
) (const t
) string
)
202 (defcustom uce-default-headers
203 "Errors-To: nobody@localhost\nPrecedence: bulk\n"
204 "Additional headers to use when responding to a UCE with \\[uce-reply-to-uce].
205 These are mostly meant for headers that prevent delivery errors reporting."
209 (defcustom uce-subject-line
210 "Spam alert: unsolicited commercial e-mail"
211 "Subject of the message that will be sent in response to a UCE."
215 (defun uce-reply-to-uce (&optional ignored
)
216 "Send reply to UCE in Rmail.
217 UCE stands for unsolicited commercial email. Function will set up reply
218 buffer with default To: to the sender, his postmaster, his abuse@
219 address, and postmaster of the mail relay used."
221 (let ((message-buffer
222 (cond ((eq uce-mail-reader
'gnus
) gnus-original-article-buffer
)
223 ((eq uce-mail-reader
'rmail
) "RMAIL")
225 "Variable uce-mail-reader set to unrecognized value")))))
226 (or (get-buffer message-buffer
)
227 (error (concat "No buffer " message-buffer
", cannot find UCE")))
228 (switch-to-buffer message-buffer
)
229 (let ((to (mail-strip-quoted-names (mail-fetch-field "from" t
)))
230 (reply-to (mail-fetch-field "reply-to"))
232 ;; Initial setting of the list of recipients of our message; that's
233 ;; what they are pretending to be.
235 (setq to
(format "%s" (mail-strip-quoted-names to
)))
238 (setq to
(format "%s, %s" to
(mail-strip-quoted-names reply-to
))))
239 (let (first-at-sign end-of-hostname sender-host
)
240 (setq first-at-sign
(string-match "@" to
)
241 end-of-hostname
(string-match "[ ,>]" to first-at-sign
)
242 sender-host
(substring to first-at-sign end-of-hostname
))
243 (if (string-match "\\." sender-host
)
244 (setq to
(format "%s, postmaster%s, abuse%s"
245 to sender-host sender-host
))))
246 (setq mail-send-actions nil
)
247 (setq mail-reply-buffer nil
)
248 (cond ((eq uce-mail-reader
'gnus
)
249 (copy-region-as-kill (point-min) (point-max)))
250 ((eq uce-mail-reader
'rmail
)
254 (rmail-maybe-set-message-counters)
255 (copy-region-as-kill (rmail-msgbeg rmail-current-message
)
256 (rmail-msgend rmail-current-message
))))))
257 (switch-to-buffer "*mail*")
262 (if (eq uce-mail-reader
'rmail
)
265 (let ((case-fold-search t
))
266 (while (looking-at "Summary-Line:\\|Mail-From:")
268 (delete-region temp
(point))))
269 ;; Now find the mail hub that first accepted this message.
270 ;; This should try to find the last Received: header.
271 ;; Sometimes there may be other headers inbetween Received: headers.
272 (cond ((eq uce-mail-reader
'gnus
)
273 ;; Does Gnus always have Lines: in the end?
274 (re-search-forward "^Lines:")
276 ((eq uce-mail-reader
'rmail
)
277 (beginning-of-buffer)
278 (search-forward "*** EOOH ***\n")
281 (re-search-backward "^Received:")
283 ;; Is this always good? It's the only thing I saw when I checked
285 (let ((eol (save-excursion (end-of-line) (point))))
286 ;;(if (not (re-search-forward ": \\(from\\|by\\) " eol t))
287 (if (not (re-search-forward "\\(from\\|by\\) " eol t
))
290 (if (looking-at "[ \t\n]+\\(from\\|by\\) ")
291 (goto-char (match-end 0))
292 (error "Failed to extract hub address")))))
296 ;; And add its postmaster to the list of addresses.
297 (if (string-match "\\." (buffer-substring temp
(point)))
298 (setq to
(format "%s, postmaster@%s"
299 to
(buffer-substring temp
(point)))))
300 ;; Also look at the message-id, it helps *very* often.
301 (if (and (search-forward "\nMessage-Id: " nil t
)
302 ;; Not all Message-Id:'s have an `@' sign.
308 (search-forward "@" eol t
)))
313 (if (string-match "\\." (buffer-substring temp
(point)))
314 (setq to
(format "%s, postmaster@%s"
315 to
(buffer-substring temp
(point)))))))
316 (cond ((eq uce-mail-reader
'gnus
)
317 ;; Does Gnus always have Lines: in the end?
318 (re-search-forward "^Lines:")
320 ((eq uce-mail-reader
'rmail
)
321 (search-forward "\n*** EOOH ***\n")
324 (search-forward "\n\n" nil t
)
325 (if (eq uce-mail-reader
'gnus
)
327 (delete-region temp
(point))
328 ;; End of Rmail dependent section.
329 (auto-save-mode auto-save-default
)
331 (goto-char (point-min))
335 (let ((fill-prefix "\t")
336 (address-start (point)))
338 (fill-region-as-paragraph address-start
(point)))
340 (insert "Subject: " uce-subject-line
"\n")
341 (if uce-default-headers
342 (insert uce-default-headers
))
343 (if mail-default-headers
344 (insert mail-default-headers
))
345 (if mail-default-reply-to
346 (insert "Reply-to: " mail-default-reply-to
"\n"))
347 (insert mail-header-separator
"\n")
348 ;; Insert all our text. Then go back to the place where we started.
349 (if to
(setq to
(point)))
352 (insert uce-message-text
))
354 (cond ((eq uce-signature t
)
355 (if (file-exists-p "~/.signature")
358 (insert-file "~/.signature")
359 ;; Function insert-file leaves point where it was,
360 ;; while we want to place signature in the ``middle''
362 (exchange-point-and-mark))))
364 (insert "\n\n-- \n" uce-signature
)))
365 ;; And text of the original message.
366 (if uce-uce-separator
367 (insert "\n\n" uce-uce-separator
"\n"))
368 ;; If message doesn't end with a newline, insert it.
369 (goto-char (point-max))
370 (or (bolp) (newline)))
371 ;; And go back to the beginning of text.
372 (if to
(goto-char to
))
373 (or to
(set-buffer-modified-p nil
))
374 ;; Run hooks before we leave buffer for editing. Reasonable usage
375 ;; might be to set up special key bindings, replace standart
376 ;; functions in mail-mode, etc.
377 (run-hooks 'mail-setup-hook
'uce-setup-hook
))))
379 (defun uce-insert-ranting (&optional ignored
)
380 "Insert text of the usual reply to UCE into current buffer."
382 (insert uce-message-text
))