* spam-report.el (spam-report-url-ping-plain): Include a
[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 (eval-and-compile
35   (autoload 'mm-url-insert "mm-url"))
36
37 (defgroup spam-report nil
38   "Spam reporting configuration.")
39
40 (defcustom spam-report-gmane-regex nil
41   "Regexp matching Gmane newsgroups, e.g. \"^nntp\\+.*:gmane\\.\"
42 If you are using spam.el, consider setting gnus-spam-process-newsgroups 
43 or the gnus-group-spam-exit-processor-report-gmane group/topic parameter 
44 instead."
45   :type '(radio (const nil)
46                 (regexp :format "%t: %v\n" :size 0 :value "^nntp\+.*:gmane\."))
47   :group 'spam-report)
48
49 (defcustom spam-report-gmane-spam-header 
50   "^X-Report-Spam: http://\\([^/]+\\)\\(.*\\)$"
51   "String matching Gmane spam-reporting header.  Two match groups are needed."
52   :type 'regexp
53   :group 'spam-report)
54
55 (defcustom spam-report-gmane-use-article-number t
56   "Whether the article number (faster!) or the header should be used."
57   :type 'boolean
58   :group 'spam-report)
59
60 (defcustom spam-report-url-ping-function
61   'spam-report-url-ping-plain
62   "Function to use for url ping spam reporting."
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   :group 'spam-report)
69
70 (defun spam-report-gmane (article)
71   "Report an article as spam through Gmane"
72   (interactive "nEnter the article number: ")
73   (when (and gnus-newsgroup-name
74              (or (null spam-report-gmane-regex)
75                  (string-match spam-report-gmane-regex gnus-newsgroup-name)))
76     (gnus-message 6 "Reporting spam article %d to spam.gmane.org..." article)
77       (if spam-report-gmane-use-article-number
78           (spam-report-url-ping "spam.gmane.org" 
79                     (format "/%s:%d"
80                             (gnus-group-real-name gnus-newsgroup-name)
81                             article))
82         (with-current-buffer nntp-server-buffer
83           (gnus-request-head article gnus-newsgroup-name)
84           (goto-char (point-min))
85           (if (re-search-forward spam-report-gmane-spam-header nil t)
86               (let* ((host (match-string 1))
87                      (report (match-string 2))
88                      (url (format "http://%s%s" host report)))
89                 (gnus-message 10 "Reporting spam through URL %s..." url)
90                 (spam-report-url-ping host report))
91             (gnus-message 10 "Could not find X-Report-Spam in article %d..."
92                           article))))))
93
94 (defun spam-report-url-ping (host report)
95   "Ping a host through HTTP, addressing a specific GET resource using
96 the function specified by `spam-report-url-ping-function'."
97   (funcall spam-report-url-ping-function host report))
98
99 (defun spam-report-url-ping-plain (host report)
100   "Ping a host through HTTP, addressing a specific GET resource."
101   (let ((tcp-connection))
102     (with-temp-buffer
103       (or (setq tcp-connection
104                 (open-network-stream 
105                  "URL ping"
106                  (buffer-name)
107                  host
108                  80))
109           (error "Could not open connection to %s" host))
110       (set-marker (process-mark tcp-connection) (point-min))
111       (process-send-string
112        tcp-connection
113        (format "GET %s HTTP/1.1\nUser-Agent: %s (spam-report.el)\nHost: %s\n\n"
114                report (gnus-emacs-version) host)))))
115
116 (defun spam-report-url-ping-mm-url (host report)
117   "Ping a host through HTTP, addressing a specific GET resource. Use
118 the external program specified in `mm-url-program' to connect to
119 server."
120   (with-temp-buffer
121     (let ((url (concat "http://" host "/" report)))
122       (mm-url-insert url t))))
123
124 (provide 'spam-report)
125
126 ;;; spam-report.el ends here.