spam-report.el 14 KB

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