Merge from emacs--devo--0
[gnus] / lisp / spam-report.el
1 ;;; spam-report.el --- Reporting spam
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: network, spam, mail, gmane, report
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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; This module addresses a few aspects of spam reporting under Gnus.  Page
26 ;;; breaks are used for grouping declarations and documentation relating to
27 ;;; each particular aspect.
28
29 ;;; Code:
30 (require 'gnus)
31 (require 'gnus-sum)
32
33 (eval-and-compile
34   (autoload 'mm-url-insert "mm-url"))
35
36 (defgroup spam-report nil
37   "Spam reporting configuration."
38   :group 'mail
39   :group 'news)
40
41 (defcustom spam-report-gmane-regex nil
42   "Regexp matching Gmane newsgroups, e.g. \"^nntp\\+.*:gmane\\.\"
43 If you are using spam.el, consider setting gnus-spam-process-newsgroups
44 or the gnus-group-spam-exit-processor-report-gmane group/topic parameter
45 instead."
46   :type '(radio (const nil)
47                 (regexp :value "^nntp\+.*:gmane\."))
48   :group 'spam-report)
49
50 (defcustom spam-report-gmane-use-article-number t
51   "Whether the article number (faster!) or the header should be used.
52
53 You must set this to nil if you don't read Gmane groups directly
54 from news.gmane.org, e.g. when using local newsserver such as
55 leafnode."
56   :type 'boolean
57   :group 'spam-report)
58
59 (defcustom spam-report-url-ping-function
60   'spam-report-url-ping-plain
61   "Function to use for url ping spam reporting.
62 The function must accept the arguments `host' and `report'."
63   :type '(choice
64           (const :tag "Connect directly"
65                  spam-report-url-ping-plain)
66           (const :tag "Use the external program specified in `mm-url-program'"
67                  spam-report-url-ping-mm-url)
68           (const :tag "Store request URLs in `spam-report-requests-file'"
69                  spam-report-url-to-file)
70           (function :tag "User defined function" nil))
71   :group 'spam-report)
72
73 (defcustom spam-report-requests-file
74   (nnheader-concat gnus-directory "spam/" "spam-report-requests.url")
75   ;; Is there a convention for the extension of such a file?
76   ;; Should we use `spam-directory'?
77   "File where spam report request are stored."
78   :type 'file
79   :group 'spam-report)
80
81 (defcustom spam-report-resend-to nil
82   "Email address that spam articles are resent to when reporting.
83 If not set, the user will be prompted to enter a value which will be
84 saved for future use."
85   :type 'string
86   :group 'spam-report)
87
88 (defvar spam-report-url-ping-temp-agent-function nil
89   "Internal variable for `spam-report-agentize' and `spam-report-deagentize'.
90 This variable will store the value of `spam-report-url-ping-function' from
91 before `spam-report-agentize' was run, so that `spam-report-deagentize' can
92 undo that change.")
93
94 (defun spam-report-resend (articles &optional ham)
95   "Report an article as spam by resending via email.
96 Reports is as ham when HAM is set."
97   (dolist (article articles)
98     (gnus-message 6 
99                   "Reporting %s article %d to <%s>..."
100                   (if ham "ham" "spam")
101                   article spam-report-resend-to)
102     (unless spam-report-resend-to
103       (customize-set-variable 
104        spam-report-resend-to
105        (read-from-minibuffer "email address to resend SPAM/HAM to? ")))
106     ;; This is ganked from the `gnus-summary-resend-message' function.
107     ;; It involves rendering the SPAM, which is undesirable, but there does
108     ;; not seem to be a nicer way to achieve this.
109     ;; select this particular article
110     (gnus-summary-select-article nil nil nil article)
111     ;; resend it to the destination address
112     (save-excursion
113       (set-buffer gnus-original-article-buffer)
114       (message-resend spam-report-resend-to))))
115
116 (defun spam-report-resend-ham (articles)
117   "Report an article as ham by resending via email."
118   (spam-report-resend articles t))
119
120 (defun spam-report-gmane-ham (&rest articles)
121   "Report ARTICLES as ham (unregister) through Gmane."
122   (interactive (gnus-summary-work-articles current-prefix-arg))
123   (dolist (article articles)
124     (spam-report-gmane-internal t article)))
125
126 (defun spam-report-gmane-spam (&rest articles)
127   "Report ARTICLES as spam through Gmane."
128   (interactive (gnus-summary-work-articles current-prefix-arg))
129   (dolist (article articles)
130     (spam-report-gmane-internal nil article)))
131
132 ;; `spam-report-gmane' was an interactive entry point, so we should provide an
133 ;; alias.
134 (defalias 'spam-report-gmane 'spam-report-gmane-spam)
135
136 (defun spam-report-gmane-internal (unspam article)
137   "Report ARTICLE as spam or not-spam through Gmane, depending on UNSPAM."
138   (when (and gnus-newsgroup-name
139              (or (null spam-report-gmane-regex)
140                  (string-match spam-report-gmane-regex gnus-newsgroup-name)))
141     (let ((rpt-host (if unspam "unspam.gmane.org" "spam.gmane.org")))
142       (gnus-message 6 "Reporting article %d to %s..." article rpt-host)
143       (cond
144        ;; Special-case nnweb groups -- these have the URL to use in
145        ;; the Xref headers.
146        ((eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnweb)
147         (spam-report-url-ping
148          rpt-host
149          (concat
150           "/"
151           (gnus-replace-in-string
152            (gnus-replace-in-string
153             (gnus-replace-in-string
154              (mail-header-xref (gnus-summary-article-header article))
155              "/raw" ":silent")
156             "^.*article.gmane.org/" "")
157            "/" ":"))))
158        (spam-report-gmane-use-article-number
159         (spam-report-url-ping
160          rpt-host
161          (format "/%s:%d"
162                  (gnus-group-real-name gnus-newsgroup-name)
163                  article)))
164        (t
165         (with-current-buffer nntp-server-buffer
166           (erase-buffer)
167           (gnus-request-head article gnus-newsgroup-name)
168           (let ((case-fold-search t)
169                 field host report url)
170             ;; First check for X-Report-Spam because it's more specific to
171             ;; spam reporting than Archived-At.  OTOH, all new articles on
172             ;; Gmane don't have X-Report-Spam anymore (unless Lars changes his
173             ;; mind :-)).
174             ;;
175             ;; There might be more than one Archived-At header so we need to
176             ;; find (and transform) the one related to Gmane.
177             (setq field (or (gnus-fetch-field "X-Report-Spam")
178                             (gnus-fetch-field "X-Report-Unspam")
179                             (gnus-fetch-field "Archived-At")))
180             (if (not (stringp field))
181                 (if (and (setq field (gnus-fetch-field "Xref"))
182                          (string-match "[^ ]+ +\\([^ ]+\\)" field))
183                     (setq report (concat "/" (match-string 1 field))
184                           host rpt-host))
185               (setq host
186                     (progn
187                       (string-match
188                        (concat "http://\\([a-z]+\\.gmane\\.org\\)"
189                                "\\(/[^:/]+[:/][0-9]+\\)")
190                        field)
191                       (match-string 1 field)))
192               (setq report (match-string 2 field)))
193             (when host
194               (when (string-equal "permalink.gmane.org" host)
195                 (setq host rpt-host)
196                 (setq report (gnus-replace-in-string
197                               report "/\\([0-9]+\\)$" ":\\1")))
198               (setq url (format "http://%s%s" host report)))
199             (if (not (and host report url))
200                 (gnus-message
201                  3 "Could not find a spam report header in article %d..."
202                  article)
203               (gnus-message 7 "Reporting article through URL %s..." url)
204               (spam-report-url-ping host report)))))))))
205
206 (defun spam-report-url-ping (host report)
207   "Ping a host through HTTP, addressing a specific GET resource using
208 the function specified by `spam-report-url-ping-function'."
209   ;; Example:
210   ;; host: "spam.gmane.org"
211   ;; report: "/gmane.some.group:123456"
212   (funcall spam-report-url-ping-function host report))
213
214 (defcustom spam-report-user-mail-address
215   (and (stringp user-mail-address)
216        (gnus-replace-in-string user-mail-address "@" "<at>"))
217   "Mail address of this user used for spam reports to Gmane.
218 This is initialized based on `user-mail-address'."
219   :type '(choice string
220                  (const :tag "Don't expose address" nil))
221   :version "23.1" ;; No Gnus
222   :group 'spam-report)
223
224 (defvar spam-report-user-agent
225   (if spam-report-user-mail-address
226       (format "%s (%s) %s" "spam-report.el"
227               spam-report-user-mail-address
228               (gnus-extended-version))
229     (format "%s %s" "spam-report.el"
230             (gnus-extended-version))))
231
232 (defun spam-report-url-ping-plain (host report)
233   "Ping a host through HTTP, addressing a specific GET resource."
234   (let ((tcp-connection))
235     (with-temp-buffer
236       (or (setq tcp-connection
237                 (open-network-stream
238                  "URL ping"
239                  (buffer-name)
240                  host
241                  80))
242           (error "Could not open connection to %s" host))
243       (set-marker (process-mark tcp-connection) (point-min))
244       (process-send-string
245        tcp-connection
246        (format "GET %s HTTP/1.1\nUser-Agent: %s\nHost: %s\n\n"
247                report spam-report-user-agent host))
248       ;; Wait until we get something so we don't DOS the host. 
249       (while (and (memq (process-status tcp-connection) '(open run))
250                   (zerop (buffer-size)))
251         (accept-process-output tcp-connection)))))
252
253 ;;;###autoload
254 (defun spam-report-process-queue (&optional file keep)
255   "Report all queued requests from `spam-report-requests-file'.
256
257 If FILE is given, use it instead of `spam-report-requests-file'.
258 If KEEP is t, leave old requests in the file.  If KEEP is the
259 symbol `ask', query before flushing the queue file."
260   (interactive
261    (list (read-file-name
262           "File: "
263           (file-name-directory spam-report-requests-file)
264           spam-report-requests-file
265           nil
266           (file-name-nondirectory spam-report-requests-file))
267          current-prefix-arg))
268   (if (eq spam-report-url-ping-function 'spam-report-url-to-file)
269       (error (concat "Cannot process requests when "
270                      "`spam-report-url-ping-function' is "
271                      "`spam-report-url-to-file'."))
272     (gnus-message 7 "Processing requests using `%s'."
273                   spam-report-url-ping-function))
274   (or file (setq file spam-report-requests-file))
275   (save-excursion
276     (set-buffer (find-file-noselect file))
277     (goto-char (point-min))
278     (while (and (not (eobp))
279                 (re-search-forward
280                  "http://\\([^/]+\\)\\(/.*\\) *$" (point-at-eol) t))
281       (funcall spam-report-url-ping-function (match-string 1) (match-string 2))
282       (forward-line 1))
283     (if (or (eq keep nil)
284             (and (eq keep 'ask)
285                  (y-or-n-p
286                   (format
287                    "Flush requests from `%s'? " (current-buffer)))))
288         (progn
289           (gnus-message 7 "Flushing request file `%s'"
290                         spam-report-requests-file)
291           (erase-buffer)
292           (save-buffer)
293           (kill-buffer (current-buffer)))
294       (gnus-message 7 "Keeping requests in `%s'" spam-report-requests-file))))
295
296 ;;;###autoload
297 (defun spam-report-url-ping-mm-url (host report)
298   "Ping a host through HTTP, addressing a specific GET resource. Use
299 the external program specified in `mm-url-program' to connect to
300 server."
301   (with-temp-buffer
302     (let ((url (format "http://%s%s" host report)))
303       (mm-url-insert url t))))
304
305 ;;;###autoload
306 (defun spam-report-url-to-file (host report)
307   "Collect spam report requests in `spam-report-requests-file'.
308 Customize `spam-report-url-ping-function' to use this function."
309   (let ((url (format "http://%s%s" host report))
310         (file spam-report-requests-file))
311     (gnus-make-directory (file-name-directory file))
312     (gnus-message 9 "Writing URL `%s' to file `%s'" url file)
313     (with-temp-buffer
314       (insert url)
315       (newline)
316       (append-to-file (point-min) (point-max) file))))
317
318 ;;;###autoload
319 (defun spam-report-agentize ()
320   "Add spam-report support to the Agent.
321 Spam reports will be queued with \\[spam-report-url-to-file] when
322 the Agent is unplugged, and will be submitted in a batch when the
323 Agent is plugged."
324   (interactive)
325   (add-hook 'gnus-agent-plugged-hook 'spam-report-plug-agent)
326   (add-hook 'gnus-agent-unplugged-hook 'spam-report-unplug-agent))
327
328 ;;;###autoload
329 (defun spam-report-deagentize ()
330   "Remove spam-report support from the Agent.
331 Spam reports will be queued with the method used when
332 \\[spam-report-agentize] was run."
333   (interactive)
334   (remove-hook 'gnus-agent-plugged-hook 'spam-report-plug-agent)
335   (remove-hook 'gnus-agent-unplugged-hook 'spam-report-unplug-agent))
336
337 (defun spam-report-plug-agent ()
338   "Adjust spam report settings for plugged state.
339 Process queued spam reports."
340   ;; Process the queue, unless the user only wanted to report to a file
341   ;; anyway.
342   (unless (equal spam-report-url-ping-temp-agent-function
343                  'spam-report-url-to-file)
344     (spam-report-process-queue))
345   ;; Set the reporting function, if we have memorized something otherwise,
346   ;; stick with plain URL reporting.
347   (setq spam-report-url-ping-function
348         (or spam-report-url-ping-temp-agent-function
349             'spam-report-url-ping-plain)))
350
351 (defun spam-report-unplug-agent ()
352   "Restore spam report settings for unplugged state."
353   ;; save the old value
354   (setq spam-report-url-ping-temp-agent-function
355         spam-report-url-ping-function)
356   ;; store all reports to file
357   (setq spam-report-url-ping-function
358         'spam-report-url-to-file))
359
360 (provide 'spam-report)
361
362 ;; arch-tag: f6683295-ec89-4ab5-8803-8cc842293022
363 ;;; spam-report.el ends here.