816f973ccb15a9bbe6ee8c1ad603b9fba1577034
[gnus] / lisp / spam-report.el
1 ;;; spam-report.el --- Reporting spam
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: network, spam, mail, gmane, report
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; This module addresses a few aspects of spam reporting under Gnus.  Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
29
30 ;;; Code:
31 (require 'gnus)
32 (require 'gnus-sum)
33
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 (defconst spam-report-gmane-max-requests 4
121   "Number of reports to send before waiting for a response.")
122
123 (defvar spam-report-gmane-wait nil
124   "When non-nil, wait until we get a server response.
125 This makes sure we don't DOS the host, if many reports are
126 submitted at once.  Internal variable.")
127
128 (defun spam-report-gmane-ham (&rest articles)
129   "Report ARTICLES as ham (unregister) through Gmane."
130   (interactive (gnus-summary-work-articles current-prefix-arg))
131   (let ((count 0))
132     (dolist (article articles)
133       (setq count (1+ count))
134       (let ((spam-report-gmane-wait
135              (zerop (% count spam-report-gmane-max-requests))))
136         (spam-report-gmane-internal t article)))))
137
138 (defun spam-report-gmane-spam (&rest articles)
139   "Report ARTICLES as spam through Gmane."
140   (interactive (gnus-summary-work-articles current-prefix-arg))
141   (let ((count 0))
142     (dolist (article articles)
143       (setq count (1+ count))
144       (let ((spam-report-gmane-wait
145              (zerop (% count spam-report-gmane-max-requests))))
146         (spam-report-gmane-internal nil article)))))
147
148 ;; `spam-report-gmane' was an interactive entry point, so we should provide an
149 ;; alias.
150 (defalias 'spam-report-gmane 'spam-report-gmane-spam)
151
152 (defun spam-report-gmane-internal (unspam article)
153   "Report ARTICLE as spam or not-spam through Gmane, depending on UNSPAM."
154   (when (and gnus-newsgroup-name
155              (or (null spam-report-gmane-regex)
156                  (string-match spam-report-gmane-regex gnus-newsgroup-name)))
157     (let ((rpt-host (if unspam "unspam.gmane.org" "spam.gmane.org")))
158       (gnus-message 6 "Reporting article %d to %s..." article rpt-host)
159       (cond
160        ;; Special-case nnweb groups -- these have the URL to use in
161        ;; the Xref headers.
162        ((eq (car (gnus-find-method-for-group gnus-newsgroup-name)) 'nnweb)
163         (spam-report-url-ping
164          rpt-host
165          (concat
166           "/"
167           (gnus-replace-in-string
168            (gnus-replace-in-string
169             (gnus-replace-in-string
170              (mail-header-xref (gnus-summary-article-header article))
171              "/raw" ":silent")
172             "^.*article.gmane.org/" "")
173            "/" ":"))))
174        (spam-report-gmane-use-article-number
175         (spam-report-url-ping
176          rpt-host
177          (format "/%s:%d"
178                  (gnus-group-real-name gnus-newsgroup-name)
179                  article)))
180        (t
181         (with-current-buffer nntp-server-buffer
182           (erase-buffer)
183           (gnus-request-head article gnus-newsgroup-name)
184           (let ((case-fold-search t)
185                 field host report url)
186             ;; First check for X-Report-Spam because it's more specific to
187             ;; spam reporting than Archived-At.  OTOH, all new articles on
188             ;; Gmane don't have X-Report-Spam anymore (unless Lars changes his
189             ;; mind :-)).
190             ;;
191             ;; There might be more than one Archived-At header so we need to
192             ;; find (and transform) the one related to Gmane.
193             (setq field (or (gnus-fetch-field "X-Report-Spam")
194                             (gnus-fetch-field "X-Report-Unspam")
195                             (gnus-fetch-field "Archived-At")))
196             (if (not (stringp field))
197                 (if (and (setq field (gnus-fetch-field "Xref"))
198                          (string-match "[^ ]+ +\\([^ ]+\\)" field))
199                     (setq report (concat "/" (match-string 1 field))
200                           host rpt-host))
201               (setq host
202                     (progn
203                       (string-match
204                        (concat "http://\\([a-z]+\\.gmane\\.org\\)"
205                                "\\(/[^:/]+[:/][0-9]+\\)")
206                        field)
207                       (match-string 1 field)))
208               (setq report (match-string 2 field)))
209             (when host
210               (when (string-equal "permalink.gmane.org" host)
211                 (setq host rpt-host)
212                 (setq report (gnus-replace-in-string
213                               report "/\\([0-9]+\\)$" ":\\1")))
214               (setq url (format "http://%s%s" host report)))
215             (if (not (and host report url))
216                 (gnus-message
217                  3 "Could not find a spam report header in article %d..."
218                  article)
219               (gnus-message 7 "Reporting article through URL %s..." url)
220               (spam-report-url-ping host report)))))))))
221
222 (defun spam-report-url-ping (host report)
223   "Ping a host through HTTP, addressing a specific GET resource using
224 the function specified by `spam-report-url-ping-function'."
225   ;; Example:
226   ;; host: "spam.gmane.org"
227   ;; report: "/gmane.some.group:123456"
228   (funcall spam-report-url-ping-function host report))
229
230 (defcustom spam-report-user-mail-address
231   (and (stringp user-mail-address)
232        (gnus-replace-in-string user-mail-address "@" "<at>"))
233   "Mail address of this user used for spam reports to Gmane.
234 This is initialized based on `user-mail-address'."
235   :type '(choice string
236                  (const :tag "Don't expose address" nil))
237   :version "23.1" ;; No Gnus
238   :group 'spam-report)
239
240 (defvar spam-report-user-agent
241   (if spam-report-user-mail-address
242       (format "%s (%s) %s" "spam-report.el"
243               spam-report-user-mail-address
244               (gnus-extended-version))
245     (format "%s %s" "spam-report.el"
246             (gnus-extended-version))))
247
248 (defun spam-report-url-ping-plain (host report)
249   "Ping a host through HTTP, addressing a specific GET resource."
250   (let ((tcp-connection))
251     (with-temp-buffer
252       (or (setq tcp-connection
253                 (open-network-stream
254                  "URL ping"
255                  (buffer-name)
256                  host
257                  80))
258           (error "Could not open connection to %s" host))
259       (set-marker (process-mark tcp-connection) (point-min))
260       (process-send-string
261        tcp-connection
262        (format "GET %s HTTP/1.1\nUser-Agent: %s\nHost: %s\n\n"
263                report spam-report-user-agent host))
264       ;; Wait until we get something so we don't DOS the host, if
265       ;; `spam-report-gmane-wait' is let-bound to t.
266       (when spam-report-gmane-wait
267         (gnus-message 7 "Waiting for response from %s..." host)
268         (while (and (memq (process-status tcp-connection) '(open run))
269                     (zerop (buffer-size)))
270           (accept-process-output tcp-connection))
271         (gnus-message 7 "Waiting for response from %s... done" host)))))
272
273 ;;;###autoload
274 (defun spam-report-process-queue (&optional file keep)
275   "Report all queued requests from `spam-report-requests-file'.
276
277 If FILE is given, use it instead of `spam-report-requests-file'.
278 If KEEP is t, leave old requests in the file.  If KEEP is the
279 symbol `ask', query before flushing the queue file."
280   (interactive
281    (list (read-file-name
282           "File: "
283           (file-name-directory spam-report-requests-file)
284           spam-report-requests-file
285           nil
286           (file-name-nondirectory spam-report-requests-file))
287          current-prefix-arg))
288   (if (eq spam-report-url-ping-function 'spam-report-url-to-file)
289       (error (concat "Cannot process requests when "
290                      "`spam-report-url-ping-function' is "
291                      "`spam-report-url-to-file'."))
292     (gnus-message 7 "Processing requests using `%s'."
293                   spam-report-url-ping-function))
294   (or file (setq file spam-report-requests-file))
295   (save-excursion
296     (set-buffer (find-file-noselect file))
297     (goto-char (point-min))
298     (while (and (not (eobp))
299                 (re-search-forward
300                  "http://\\([^/]+\\)\\(/.*\\) *$" (point-at-eol) t))
301       (let ((spam-report-gmane-wait
302              (zerop (% (mm-line-number-at-pos)
303                        spam-report-gmane-max-requests))))
304         (gnus-message 6 "Reporting %s%s..."
305                       (match-string 1) (match-string 2))
306         (funcall spam-report-url-ping-function
307                  (match-string 1) (match-string 2)))
308       (forward-line 1))
309     (if (or (eq keep nil)
310             (and (eq keep 'ask)
311                  (y-or-n-p
312                   (format
313                    "Flush requests from `%s'? " (current-buffer)))))
314         (progn
315           (gnus-message 7 "Flushing request file `%s'"
316                         spam-report-requests-file)
317           (erase-buffer)
318           (save-buffer)
319           (kill-buffer (current-buffer)))
320       (gnus-message 7 "Keeping requests in `%s'" spam-report-requests-file))))
321
322 ;;;###autoload
323 (defun spam-report-url-ping-mm-url (host report)
324   "Ping a host through HTTP, addressing a specific GET resource. Use
325 the external program specified in `mm-url-program' to connect to
326 server."
327   (with-temp-buffer
328     (let ((url (format "http://%s%s" host report)))
329       (mm-url-insert url t))))
330
331 ;;;###autoload
332 (defun spam-report-url-to-file (host report)
333   "Collect spam report requests in `spam-report-requests-file'.
334 Customize `spam-report-url-ping-function' to use this function."
335   (let ((url (format "http://%s%s" host report))
336         (file spam-report-requests-file))
337     (gnus-make-directory (file-name-directory file))
338     (gnus-message 9 "Writing URL `%s' to file `%s'" url file)
339     (with-temp-buffer
340       (insert url)
341       (newline)
342       (append-to-file (point-min) (point-max) file))))
343
344 ;;;###autoload
345 (defun spam-report-agentize ()
346   "Add spam-report support to the Agent.
347 Spam reports will be queued with \\[spam-report-url-to-file] when
348 the Agent is unplugged, and will be submitted in a batch when the
349 Agent is plugged."
350   (interactive)
351   (add-hook 'gnus-agent-plugged-hook 'spam-report-plug-agent)
352   (add-hook 'gnus-agent-unplugged-hook 'spam-report-unplug-agent))
353
354 ;;;###autoload
355 (defun spam-report-deagentize ()
356   "Remove spam-report support from the Agent.
357 Spam reports will be queued with the method used when
358 \\[spam-report-agentize] was run."
359   (interactive)
360   (remove-hook 'gnus-agent-plugged-hook 'spam-report-plug-agent)
361   (remove-hook 'gnus-agent-unplugged-hook 'spam-report-unplug-agent))
362
363 (defun spam-report-plug-agent ()
364   "Adjust spam report settings for plugged state.
365 Process queued spam reports."
366   ;; Process the queue, unless the user only wanted to report to a file
367   ;; anyway.
368   (unless (equal spam-report-url-ping-temp-agent-function
369                  'spam-report-url-to-file)
370     (spam-report-process-queue))
371   ;; Set the reporting function, if we have memorized something otherwise,
372   ;; stick with plain URL reporting.
373   (setq spam-report-url-ping-function
374         (or spam-report-url-ping-temp-agent-function
375             'spam-report-url-ping-plain)))
376
377 (defun spam-report-unplug-agent ()
378   "Restore spam report settings for unplugged state."
379   ;; save the old value
380   (setq spam-report-url-ping-temp-agent-function
381         spam-report-url-ping-function)
382   ;; store all reports to file
383   (setq spam-report-url-ping-function
384         'spam-report-url-to-file))
385
386 (provide 'spam-report)
387
388 ;; arch-tag: f6683295-ec89-4ab5-8803-8cc842293022
389 ;;; spam-report.el ends here.