288a05e32ab94f616d2ebbd0da02a8ce20f773ee
[gnus] / lisp / spam-report.el
1 ;;; spam-report.el --- Reporting spam
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
4 ;; Author: Teodor Zlatanov <tzz@lifelogs.com>
5 ;; Keywords: network
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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 (defgroup spam-report nil
35   "Spam reporting configuration.")
36
37 (defcustom spam-report-gmane-regex nil
38   "Regexp matching Gmane newsgroups, e.g. \"^nntp\\+.*:gmane\\.\"
39 If you are using spam.el, consider setting gnus-spam-process-newsgroups 
40 or the gnus-group-spam-exit-processor-report-gmane group/topic parameter 
41 instead."
42   :type '(radio (const nil)
43                 (regexp :format "%t: %v\n" :size 0 :value "^nntp\+.*:gmane\."))
44   :group 'spam-report)
45
46 (defcustom spam-report-gmane-spam-header 
47   "^X-Report-Spam: http://\\([^/]+\\)\\(.*\\)$"
48   "String matching Gmane spam-reporting header.  Two match groups are needed."
49   :type 'regexp
50   :group 'spam-report)
51
52 (defcustom spam-report-gmane-use-article-number t
53   "Whether the article number (faster!) or the header should be used."
54   :type 'boolean
55   :group 'spam-report)
56
57 (defun spam-report-gmane (article)
58   "Report an article as spam through Gmane"
59   (interactive "nEnter the article number: ")
60   (when (and gnus-newsgroup-name
61              (or (null spam-report-gmane-regex)
62                  (string-match spam-report-gmane-regex gnus-newsgroup-name)))
63     (gnus-message 6 "Reporting spam article %d to spam.gmane.org..." article)
64       (if spam-report-gmane-use-article-number
65           (spam-report-url-ping "spam.gmane.org" 
66                     (format "/%s:%d"
67                             (gnus-group-real-name gnus-newsgroup-name)
68                             article))
69         (with-current-buffer nntp-server-buffer
70           (gnus-request-head article gnus-newsgroup-name)
71           (goto-char (point-min))
72           (if (re-search-forward spam-report-gmane-spam-header nil t)
73               (let* ((host (match-string 1))
74                      (report (match-string 2))
75                      (url (format "http://%s%s" host report)))
76                 (gnus-message 10 "Reporting spam through URL %s..." url)
77                 (spam-report-url-ping host report))
78             (gnus-message 10 "Could not find X-Report-Spam in article %d..."
79                           article))))))
80
81
82 (defun spam-report-url-ping (host report)
83   "Ping a host through HTTP, addressing a specific GET resource"
84   (let ((tcp-connection))
85     (with-temp-buffer
86       (or (setq tcp-connection
87                 (open-network-stream 
88                  "URL ping"
89                  (buffer-name)
90                  host
91                  80))
92           (error "Could not open connection to %s" host))
93       (set-marker (process-mark tcp-connection) (point-min))
94       (process-send-string tcp-connection
95                            (format "GET %s HTTP/1.1\nHost: %s\n\n"
96                                    report host)))))
97
98 (provide 'spam-report)
99
100 ;;; spam-report.el ends here.