spam.el 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. ;;; spam.el --- Identifying spam
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
  5. ;; Keywords: network, spam, mail, bogofilter, BBDB, dspam, dig, whitelist, blacklist, gmane, hashcash, spamassassin, bsfilter, ifile, stat, crm114, spamoracle
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; This module addresses a few aspects of spam control under Gnus. Page
  19. ;;; breaks are used for grouping declarations and documentation relating to
  20. ;;; each particular aspect.
  21. ;;; The integration with Gnus is not yet complete. See various `FIXME'
  22. ;;; comments, below, for supplementary explanations or discussions.
  23. ;;; Several TODO items are marked as such
  24. ;; TODO: cross-server splitting, remote processing, training through files
  25. ;;; Code:
  26. ;;{{{ compilation directives and autoloads/requires
  27. ;; For Emacs <22.2 and XEmacs.
  28. (eval-and-compile
  29. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  30. (eval-when-compile (require 'cl))
  31. (require 'message) ;for the message-fetch-field functions
  32. (require 'gnus-sum)
  33. (require 'gnus-uu) ; because of key prefix issues
  34. ;;; for the definitions of group content classification and spam processors
  35. (require 'gnus)
  36. (eval-when-compile (require 'spam-report))
  37. (eval-when-compile (require 'hashcash))
  38. ;; for nnimap-split-download-body-default
  39. (eval-when-compile (require 'nnimap))
  40. ;; autoload query-dig
  41. (autoload 'query-dig "dig")
  42. ;; autoload spam-report
  43. (eval-and-compile
  44. (autoload 'spam-report-gmane "spam-report")
  45. (autoload 'spam-report-gmane-spam "spam-report")
  46. (autoload 'spam-report-gmane-ham "spam-report")
  47. (autoload 'spam-report-resend "spam-report"))
  48. ;; autoload gnus-registry
  49. (autoload 'gnus-registry-group-count "gnus-registry")
  50. (autoload 'gnus-registry-get-id-key "gnus-registry")
  51. (autoload 'gnus-registry-set-id-key "gnus-registry")
  52. (autoload 'gnus-registry-handle-action "gnus-registry")
  53. ;; autoload dns-query
  54. (autoload 'dns-query "dns")
  55. ;;}}}
  56. ;;{{{ Main parameters.
  57. (defvar spam-backends nil
  58. "List of spam.el backends with all the pertinent data.
  59. Populated by `spam-install-backend-super'.")
  60. (defgroup spam nil
  61. "Spam configuration."
  62. :version "22.1"
  63. :group 'mail
  64. :group 'news)
  65. (defcustom spam-summary-exit-behavior 'default
  66. "Exit behavior at the time of summary exit.
  67. Note that setting the `spam-use-move' or `spam-use-copy' backends on
  68. a group through group/topic parameters overrides this mechanism."
  69. :type '(choice
  70. (const
  71. 'default
  72. :tag "Move spam out of all groups and ham out of spam groups.")
  73. (const
  74. 'move-all
  75. :tag "Move spam out of all groups and ham out of all groups.")
  76. (const
  77. 'move-none
  78. :tag "Never move spam or ham out of any groups."))
  79. :group 'spam)
  80. (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
  81. "Directory for spam whitelists and blacklists."
  82. :type 'directory
  83. :group 'spam)
  84. (defcustom spam-mark-new-messages-in-spam-group-as-spam t
  85. "Whether new messages in a spam group should get the spam-mark."
  86. :type 'boolean
  87. ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
  88. :group 'spam)
  89. (defcustom spam-log-to-registry nil
  90. "Whether spam/ham processing should be logged in the registry."
  91. :type 'boolean
  92. :group 'spam)
  93. (defcustom spam-split-symbolic-return nil
  94. "Whether `spam-split' should work with symbols or group names."
  95. :type 'boolean
  96. :group 'spam)
  97. (defcustom spam-split-symbolic-return-positive nil
  98. "Whether `spam-split' should ALWAYS work with symbols or group names.
  99. Do not set this if you use `spam-split' in a fancy split method."
  100. :type 'boolean
  101. :group 'spam)
  102. (defcustom spam-mark-only-unseen-as-spam t
  103. "Whether only unseen articles should be marked as spam in spam groups.
  104. When nil, all unread articles in a spam group are marked as
  105. spam. Set this if you want to leave an article unread in a spam group
  106. without losing it to the automatic spam-marking process."
  107. :type 'boolean
  108. :group 'spam)
  109. (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
  110. "Whether ham should be marked unread before it's moved.
  111. The article is moved out of a spam group according to `ham-process-destination'.
  112. This variable is an official entry in the international Longest Variable Name
  113. Competition."
  114. :type 'boolean
  115. :group 'spam)
  116. (defcustom spam-disable-spam-split-during-ham-respool nil
  117. "Whether `spam-split' should be ignored while resplitting ham.
  118. This is useful to prevent ham from ending up in the same spam
  119. group after the resplit. Don't set this to t if you have `spam-split' as the
  120. last rule in your split configuration."
  121. :type 'boolean
  122. :group 'spam)
  123. (defcustom spam-autodetect-recheck-messages nil
  124. "Should spam.el recheck all messages when autodetecting?
  125. Normally this is nil, so only unseen messages will be checked."
  126. :type 'boolean
  127. :group 'spam)
  128. (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
  129. "The location of the whitelist.
  130. The file format is one regular expression per line.
  131. The regular expression is matched against the address."
  132. :type 'file
  133. :group 'spam)
  134. (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
  135. "The location of the blacklist.
  136. The file format is one regular expression per line.
  137. The regular expression is matched against the address."
  138. :type 'file
  139. :group 'spam)
  140. (defcustom spam-use-dig t
  141. "Whether `query-dig' should be used instead of `dns-query'."
  142. :type 'boolean
  143. :group 'spam)
  144. (defcustom spam-use-gmane-xref nil
  145. "Whether the Gmane spam xref should be used by `spam-split'."
  146. :type 'boolean
  147. :group 'spam)
  148. (defcustom spam-use-blacklist nil
  149. "Whether the blacklist should be used by `spam-split'."
  150. :type 'boolean
  151. :group 'spam)
  152. (defcustom spam-blacklist-ignored-regexes nil
  153. "Regular expressions that the blacklist should ignore."
  154. :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
  155. :group 'spam)
  156. (defcustom spam-use-whitelist nil
  157. "Whether the whitelist should be used by `spam-split'."
  158. :type 'boolean
  159. :group 'spam)
  160. (defcustom spam-use-whitelist-exclusive nil
  161. "Whether whitelist-exclusive should be used by `spam-split'.
  162. Exclusive whitelisting means that all messages from senders not in the whitelist
  163. are considered spam."
  164. :type 'boolean
  165. :group 'spam)
  166. (defcustom spam-use-blackholes nil
  167. "Whether blackholes should be used by `spam-split'."
  168. :type 'boolean
  169. :group 'spam)
  170. (defcustom spam-use-hashcash nil
  171. "Whether hashcash payments should be detected by `spam-split'."
  172. :type 'boolean
  173. :group 'spam)
  174. (defcustom spam-use-regex-headers nil
  175. "Whether a header regular expression match should be used by `spam-split'.
  176. Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
  177. :type 'boolean
  178. :group 'spam)
  179. (defcustom spam-use-regex-body nil
  180. "Whether a body regular expression match should be used by `spam-split'.
  181. Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
  182. :type 'boolean
  183. :group 'spam)
  184. (defcustom spam-use-bogofilter-headers nil
  185. "Whether bogofilter headers should be used by `spam-split'.
  186. Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
  187. :type 'boolean
  188. :group 'spam)
  189. (defcustom spam-use-bogofilter nil
  190. "Whether bogofilter should be invoked by `spam-split'.
  191. Enable this if you want Gnus to invoke Bogofilter on new messages."
  192. :type 'boolean
  193. :group 'spam)
  194. (defcustom spam-use-bsfilter-headers nil
  195. "Whether bsfilter headers should be used by `spam-split'.
  196. Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
  197. :type 'boolean
  198. :group 'spam)
  199. (defcustom spam-use-bsfilter nil
  200. "Whether bsfilter should be invoked by `spam-split'.
  201. Enable this if you want Gnus to invoke Bsfilter on new messages."
  202. :type 'boolean
  203. :group 'spam)
  204. (defcustom spam-use-BBDB nil
  205. "Whether BBDB should be used by `spam-split'."
  206. :type 'boolean
  207. :group 'spam)
  208. (defcustom spam-use-BBDB-exclusive nil
  209. "Whether BBDB-exclusive should be used by `spam-split'.
  210. Exclusive BBDB means that all messages from senders not in the BBDB are
  211. considered spam."
  212. :type 'boolean
  213. :group 'spam)
  214. (defcustom spam-use-ifile nil
  215. "Whether ifile should be used by `spam-split'."
  216. :type 'boolean
  217. :group 'spam)
  218. (defcustom spam-use-stat nil
  219. "Whether `spam-stat' should be used by `spam-split'."
  220. :type 'boolean
  221. :group 'spam)
  222. (defcustom spam-use-spamoracle nil
  223. "Whether spamoracle should be used by `spam-split'."
  224. :type 'boolean
  225. :group 'spam)
  226. (defcustom spam-use-spamassassin nil
  227. "Whether spamassassin should be invoked by `spam-split'.
  228. Enable this if you want Gnus to invoke SpamAssassin on new messages."
  229. :type 'boolean
  230. :group 'spam)
  231. (defcustom spam-use-spamassassin-headers nil
  232. "Whether spamassassin headers should be checked by `spam-split'.
  233. Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
  234. them."
  235. :type 'boolean
  236. :group 'spam)
  237. (defcustom spam-use-crm114 nil
  238. "Whether the CRM114 Mailfilter should be used by `spam-split'."
  239. :type 'boolean
  240. :group 'spam)
  241. (defcustom spam-install-hooks (or
  242. spam-use-dig
  243. spam-use-gmane-xref
  244. spam-use-blacklist
  245. spam-use-whitelist
  246. spam-use-whitelist-exclusive
  247. spam-use-blackholes
  248. spam-use-hashcash
  249. spam-use-regex-headers
  250. spam-use-regex-body
  251. spam-use-bogofilter
  252. spam-use-bogofilter-headers
  253. spam-use-spamassassin
  254. spam-use-spamassassin-headers
  255. spam-use-bsfilter
  256. spam-use-bsfilter-headers
  257. spam-use-BBDB
  258. spam-use-BBDB-exclusive
  259. spam-use-ifile
  260. spam-use-stat
  261. spam-use-spamoracle
  262. spam-use-crm114)
  263. "Whether the spam hooks should be installed.
  264. Default to t if one of the spam-use-* variables is set."
  265. :group 'spam
  266. :type 'boolean)
  267. (defcustom spam-split-group "spam"
  268. "Group name where incoming spam should be put by `spam-split'."
  269. :type 'string
  270. :group 'spam)
  271. ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
  272. ;;; not regular expressions
  273. (defcustom spam-junk-mailgroups (cons
  274. spam-split-group
  275. '("mail.junk" "poste.pourriel"))
  276. "Mailgroups with spam contents.
  277. All unmarked article in such group receive the spam mark on group entry."
  278. :type '(repeat (string :tag "Group"))
  279. :group 'spam)
  280. (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
  281. "The group where spam xrefs can be found on Gmane.
  282. Only meaningful if you enable `spam-use-gmane-xref'."
  283. :type 'string
  284. :group 'spam)
  285. (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
  286. "dev.null.dk" "relays.visi.com")
  287. "List of blackhole servers.
  288. Only meaningful if you enable `spam-use-blackholes'."
  289. :type '(repeat (string :tag "Server"))
  290. :group 'spam)
  291. (defcustom spam-blackhole-good-server-regex nil
  292. "String matching IP addresses that should not be checked in the blackholes.
  293. Only meaningful if you enable `spam-use-blackholes'."
  294. :type '(radio (const nil) regexp)
  295. :group 'spam)
  296. (defface spam
  297. '((((class color) (type tty) (background dark))
  298. (:foreground "gray80" :background "gray50"))
  299. (((class color) (type tty) (background light))
  300. (:foreground "gray50" :background "gray80"))
  301. (((class color) (background dark))
  302. (:foreground "ivory2"))
  303. (((class color) (background light))
  304. (:foreground "ivory4"))
  305. (t :inverse-video t))
  306. "Face for spam-marked articles."
  307. :group 'spam)
  308. ;; backward-compatibility alias
  309. (put 'spam-face 'face-alias 'spam)
  310. (put 'spam-face 'obsolete-face "22.1")
  311. (defcustom spam-face 'spam
  312. "Face for spam-marked articles."
  313. :type 'face
  314. :group 'spam)
  315. (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
  316. "Regular expression for positive header spam matches.
  317. Only meaningful if you enable `spam-use-regex-headers'."
  318. :type '(repeat (regexp :tag "Regular expression to match spam header"))
  319. :group 'spam)
  320. (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
  321. "Regular expression for positive header ham matches.
  322. Only meaningful if you enable `spam-use-regex-headers'."
  323. :type '(repeat (regexp :tag "Regular expression to match ham header"))
  324. :group 'spam)
  325. (defcustom spam-regex-body-spam '()
  326. "Regular expression for positive body spam matches.
  327. Only meaningful if you enable `spam-use-regex-body'."
  328. :type '(repeat (regexp :tag "Regular expression to match spam body"))
  329. :group 'spam)
  330. (defcustom spam-regex-body-ham '()
  331. "Regular expression for positive body ham matches.
  332. Only meaningful if you enable `spam-use-regex-body'."
  333. :type '(repeat (regexp :tag "Regular expression to match ham body"))
  334. :group 'spam)
  335. (defcustom spam-summary-score-preferred-header nil
  336. "Preferred header to use for `spam-summary-score'."
  337. :type '(choice :tag "Header name"
  338. (symbol :tag "SpamAssassin etc" X-Spam-Status)
  339. (symbol :tag "Bogofilter" X-Bogosity)
  340. (const :tag "No preference, take best guess." nil))
  341. :group 'spam)
  342. (defgroup spam-ifile nil
  343. "Spam ifile configuration."
  344. :group 'spam)
  345. (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program
  346. "Gnus 5.10.9 (Emacs 22.1)")
  347. (defcustom spam-ifile-program (executable-find "ifile")
  348. "Name of the ifile program."
  349. :type '(choice (file :tag "Location of ifile")
  350. (const :tag "ifile is not installed"))
  351. :group 'spam-ifile)
  352. (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database
  353. "Gnus 5.10.9 (Emacs 22.1)")
  354. (defcustom spam-ifile-database nil
  355. "File name of the ifile database."
  356. :type '(choice (file :tag "Location of the ifile database")
  357. (const :tag "Use the default"))
  358. :group 'spam-ifile)
  359. (defcustom spam-ifile-spam-category "spam"
  360. "Name of the spam ifile category."
  361. :type 'string
  362. :group 'spam-ifile)
  363. (defcustom spam-ifile-ham-category nil
  364. "Name of the ham ifile category.
  365. If nil, the current group name will be used."
  366. :type '(choice (string :tag "Use a fixed category")
  367. (const :tag "Use the current group name"))
  368. :group 'spam-ifile)
  369. (defcustom spam-ifile-all-categories nil
  370. "Whether the ifile check will return all categories, or just spam.
  371. Set this to t if you want to use the `spam-split' invocation of ifile as
  372. your main source of newsgroup names."
  373. :type 'boolean
  374. :group 'spam-ifile)
  375. (defgroup spam-bogofilter nil
  376. "Spam bogofilter configuration."
  377. :group 'spam)
  378. (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program
  379. "Gnus 5.10.9 (Emacs 22.1)")
  380. (defcustom spam-bogofilter-program (executable-find "bogofilter")
  381. "Name of the Bogofilter program."
  382. :type '(choice (file :tag "Location of bogofilter")
  383. (const :tag "Bogofilter is not installed"))
  384. :group 'spam-bogofilter)
  385. (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
  386. (defcustom spam-bogofilter-header "X-Bogosity"
  387. "The header that Bogofilter inserts in messages."
  388. :type 'string
  389. :group 'spam-bogofilter)
  390. (defcustom spam-bogofilter-spam-switch "-s"
  391. "The switch that Bogofilter uses to register spam messages."
  392. :type 'string
  393. :group 'spam-bogofilter)
  394. (defcustom spam-bogofilter-ham-switch "-n"
  395. "The switch that Bogofilter uses to register ham messages."
  396. :type 'string
  397. :group 'spam-bogofilter)
  398. (defcustom spam-bogofilter-spam-strong-switch "-S"
  399. "The switch that Bogofilter uses to unregister ham messages."
  400. :type 'string
  401. :group 'spam-bogofilter)
  402. (defcustom spam-bogofilter-ham-strong-switch "-N"
  403. "The switch that Bogofilter uses to unregister spam messages."
  404. :type 'string
  405. :group 'spam-bogofilter)
  406. (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
  407. "The regex on `spam-bogofilter-header' for positive spam identification."
  408. :type 'regexp
  409. :group 'spam-bogofilter)
  410. (defcustom spam-bogofilter-database-directory nil
  411. "Location of the Bogofilter database.
  412. When nil, use the default location."
  413. :type '(choice (directory
  414. :tag "Location of the Bogofilter database directory")
  415. (const :tag "Use the default"))
  416. :group 'spam-bogofilter)
  417. (defgroup spam-bsfilter nil
  418. "Spam bsfilter configuration."
  419. :group 'spam)
  420. (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program
  421. "Gnus 5.10.9 (Emacs 22.1)")
  422. (defcustom spam-bsfilter-program (executable-find "bsfilter")
  423. "Name of the Bsfilter program."
  424. :type '(choice (file :tag "Location of bsfilter")
  425. (const :tag "Bsfilter is not installed"))
  426. :group 'spam-bsfilter)
  427. (defcustom spam-bsfilter-header "X-Spam-Flag"
  428. "The header inserted by Bsfilter to flag spam."
  429. :type 'string
  430. :group 'spam-bsfilter)
  431. (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
  432. "The header that Bsfilter inserts in messages."
  433. :type 'string
  434. :group 'spam-bsfilter)
  435. (defcustom spam-bsfilter-spam-switch "--add-spam"
  436. "The switch that Bsfilter uses to register spam messages."
  437. :type 'string
  438. :group 'spam-bsfilter)
  439. (defcustom spam-bsfilter-ham-switch "--add-clean"
  440. "The switch that Bsfilter uses to register ham messages."
  441. :type 'string
  442. :group 'spam-bsfilter)
  443. (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
  444. "The switch that Bsfilter uses to unregister ham messages."
  445. :type 'string
  446. :group 'spam-bsfilter)
  447. (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
  448. "The switch that Bsfilter uses to unregister spam messages."
  449. :type 'string
  450. :group 'spam-bsfilter)
  451. (defcustom spam-bsfilter-database-directory nil
  452. "Directory path of the Bsfilter databases."
  453. :type '(choice (directory
  454. :tag "Location of the Bsfilter database directory")
  455. (const :tag "Use the default"))
  456. :group 'spam-bsfilter)
  457. (defgroup spam-spamoracle nil
  458. "Spam spamoracle configuration."
  459. :group 'spam)
  460. (defcustom spam-spamoracle-database nil
  461. "Location of spamoracle database file.
  462. When nil, use the default spamoracle database."
  463. :type '(choice (directory :tag "Location of spamoracle database file.")
  464. (const :tag "Use the default"))
  465. :group 'spam-spamoracle)
  466. (defcustom spam-spamoracle-binary (executable-find "spamoracle")
  467. "Location of the spamoracle binary."
  468. :type '(choice (directory :tag "Location of the spamoracle binary")
  469. (const :tag "Use the default"))
  470. :group 'spam-spamoracle)
  471. (defgroup spam-spamassassin nil
  472. "Spam SpamAssassin configuration."
  473. :group 'spam)
  474. (make-obsolete-variable 'spam-spamassassin-path
  475. 'spam-spamassassin-program "Gnus 5.10.9 (Emacs 22.1)")
  476. (defcustom spam-assassin-program (executable-find "spamassassin")
  477. "Name of the spamassassin program.
  478. Hint: set this to \"spamc\" if you have spamd running. See the spamc and
  479. spamd man pages for more information on these programs."
  480. :type '(choice (file :tag "Location of spamc")
  481. (const :tag "spamassassin is not installed"))
  482. :group 'spam-spamassassin)
  483. (defcustom spam-spamassassin-arguments ()
  484. "Arguments to pass to the spamassassin executable.
  485. This must be a list. For example, `(\"-C\" \"configfile\")'."
  486. :type '(restricted-sexp :match-alternatives (listp))
  487. :group 'spam-spamassassin)
  488. (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
  489. "The header inserted by SpamAssassin to flag spam."
  490. :type 'string
  491. :group 'spam-spamassassin)
  492. (defcustom spam-spamassassin-positive-spam-flag-header "YES"
  493. "The regex on `spam-spamassassin-spam-flag-header' for positive spam
  494. identification"
  495. :type 'string
  496. :group 'spam-spamassassin)
  497. (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
  498. "The header inserted by SpamAssassin, giving extended scoring information"
  499. :type 'string
  500. :group 'spam-spamassassin)
  501. (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program
  502. "Gnus 5.10.9 (Emacs 22.1)")
  503. (defcustom spam-sa-learn-program (executable-find "sa-learn")
  504. "Name of the sa-learn program."
  505. :type '(choice (file :tag "Location of spamassassin")
  506. (const :tag "spamassassin is not installed"))
  507. :group 'spam-spamassassin)
  508. (defcustom spam-sa-learn-rebuild t
  509. "Whether sa-learn should rebuild the database every time it is called
  510. Enable this if you want sa-learn to rebuild the database automatically. Doing
  511. this will slightly increase the running time of the spam registration process.
  512. If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
  513. order for SpamAssassin to recognize the new registered spam."
  514. :type 'boolean
  515. :group 'spam-spamassassin)
  516. (defcustom spam-sa-learn-spam-switch "--spam"
  517. "The switch that sa-learn uses to register spam messages."
  518. :type 'string
  519. :group 'spam-spamassassin)
  520. (defcustom spam-sa-learn-ham-switch "--ham"
  521. "The switch that sa-learn uses to register ham messages."
  522. :type 'string
  523. :group 'spam-spamassassin)
  524. (defcustom spam-sa-learn-unregister-switch "--forget"
  525. "The switch that sa-learn uses to unregister messages messages."
  526. :type 'string
  527. :group 'spam-spamassassin)
  528. (defgroup spam-crm114 nil
  529. "Spam CRM114 Mailfilter configuration."
  530. :group 'spam)
  531. (defcustom spam-crm114-program (executable-find "mailfilter.crm")
  532. "File path of the CRM114 Mailfilter executable program."
  533. :type '(choice (file :tag "Location of CRM114 Mailfilter")
  534. (const :tag "CRM114 Mailfilter is not installed"))
  535. :group 'spam-crm114)
  536. (defcustom spam-crm114-header "X-CRM114-Status"
  537. "The header that CRM114 Mailfilter inserts in messages."
  538. :type 'string
  539. :group 'spam-crm114)
  540. (defcustom spam-crm114-spam-switch "--learnspam"
  541. "The switch that CRM114 Mailfilter uses to register spam messages."
  542. :type 'string
  543. :group 'spam-crm114)
  544. (defcustom spam-crm114-ham-switch "--learnnonspam"
  545. "The switch that CRM114 Mailfilter uses to register ham messages."
  546. :type 'string
  547. :group 'spam-crm114)
  548. (defcustom spam-crm114-spam-strong-switch "--unlearn"
  549. "The switch that CRM114 Mailfilter uses to unregister ham messages."
  550. :type 'string
  551. :group 'spam-crm114)
  552. (defcustom spam-crm114-ham-strong-switch "--unlearn"
  553. "The switch that CRM114 Mailfilter uses to unregister spam messages."
  554. :type 'string
  555. :group 'spam-crm114)
  556. (defcustom spam-crm114-positive-spam-header "^SPAM"
  557. "The regex on `spam-crm114-header' for positive spam identification."
  558. :type 'regexp
  559. :group 'spam-crm114)
  560. (defcustom spam-crm114-database-directory nil
  561. "Directory path of the CRM114 Mailfilter databases."
  562. :type '(choice (directory
  563. :tag "Location of the CRM114 Mailfilter database directory")
  564. (const :tag "Use the default"))
  565. :group 'spam-crm114)
  566. ;;; Key bindings for spam control.
  567. (gnus-define-keys gnus-summary-mode-map
  568. "St" spam-generic-score
  569. "Sx" gnus-summary-mark-as-spam
  570. "Mst" spam-generic-score
  571. "Msx" gnus-summary-mark-as-spam
  572. "\M-d" gnus-summary-mark-as-spam
  573. "$" gnus-summary-mark-as-spam)
  574. (defvar spam-cache-lookups t
  575. "Whether spam.el will try to cache lookups using `spam-caches'.")
  576. (defvar spam-caches (make-hash-table
  577. :size 10
  578. :test 'equal)
  579. "Cache of spam detection entries.")
  580. (defvar spam-old-articles nil
  581. "List of old ham and spam articles, generated when a group is entered.")
  582. (defvar spam-split-disabled nil
  583. "If non-nil, `spam-split' is disabled, and always returns nil.")
  584. (defvar spam-split-last-successful-check nil
  585. "Internal variable.
  586. `spam-split' will set this to nil or a spam-use-XYZ check if it
  587. finds ham or spam.")
  588. ;; internal variables for backends
  589. ;; TODO: find a way to create these on the fly in spam-install-backend-super
  590. (defvar spam-use-copy nil)
  591. (defvar spam-use-move nil)
  592. (defvar spam-use-gmane nil)
  593. (defvar spam-use-resend nil)
  594. ;;}}}
  595. ;;{{{ convenience functions
  596. (defun spam-clear-cache (symbol)
  597. "Clear the `spam-caches' entry for a check."
  598. (remhash symbol spam-caches))
  599. (defun spam-xor (a b)
  600. "Logical A xor B."
  601. (and (or a b) (not (and a b))))
  602. (defun spam-set-difference (list1 list2)
  603. "Return a set difference of LIST1 and LIST2.
  604. When either list is nil, the other is returned."
  605. (if (and list1 list2)
  606. ;; we have two non-nil lists
  607. (progn
  608. (dolist (item (append list1 list2))
  609. (when (and (memq item list1) (memq item list2))
  610. (setq list1 (delq item list1))
  611. (setq list2 (delq item list2))))
  612. (append list1 list2))
  613. ;; if either of the lists was nil, return the other one
  614. (if list1 list1 list2)))
  615. (defun spam-group-ham-mark-p (group mark &optional spam)
  616. "Checks if MARK is considered a ham mark in GROUP."
  617. (when (stringp group)
  618. (let* ((marks (spam-group-ham-marks group spam))
  619. (marks (if (symbolp mark)
  620. marks
  621. (mapcar 'symbol-value marks))))
  622. (memq mark marks))))
  623. (defun spam-group-spam-mark-p (group mark)
  624. "Checks if MARK is considered a spam mark in GROUP."
  625. (spam-group-ham-mark-p group mark t))
  626. (defun spam-group-ham-marks (group &optional spam)
  627. "In GROUP, get all the ham marks."
  628. (when (stringp group)
  629. (let* ((marks (if spam
  630. (gnus-parameter-spam-marks group)
  631. (gnus-parameter-ham-marks group)))
  632. (marks (car marks))
  633. (marks (if (listp (car marks)) (car marks) marks)))
  634. marks)))
  635. (defun spam-group-spam-marks (group)
  636. "In GROUP, get all the spam marks."
  637. (spam-group-ham-marks group t))
  638. (defun spam-group-spam-contents-p (group)
  639. "Is GROUP a spam group?"
  640. (if (and (stringp group) (< 0 (length group)))
  641. (or (member group spam-junk-mailgroups)
  642. (memq 'gnus-group-spam-classification-spam
  643. (gnus-parameter-spam-contents group)))
  644. nil))
  645. (defun spam-group-ham-contents-p (group)
  646. "Is GROUP a ham group?"
  647. (if (stringp group)
  648. (memq 'gnus-group-spam-classification-ham
  649. (gnus-parameter-spam-contents group))
  650. nil))
  651. (defun spam-classifications ()
  652. "Return list of valid classifications"
  653. '(spam ham))
  654. (defun spam-classification-valid-p (classification)
  655. "Is CLASSIFICATION a valid spam/ham classification?"
  656. (memq classification (spam-classifications)))
  657. (defun spam-backend-properties ()
  658. "Return list of valid classifications."
  659. '(statistical mover check hrf srf huf suf))
  660. (defun spam-backend-property-valid-p (property)
  661. "Is PROPERTY a valid backend property?"
  662. (memq property (spam-backend-properties)))
  663. (defun spam-backend-function-type-valid-p (type)
  664. (or (eq type 'registration)
  665. (eq type 'unregistration)))
  666. (defun spam-process-type-valid-p (process-type)
  667. (or (eq process-type 'incoming)
  668. (eq process-type 'process)))
  669. (defun spam-list-articles (articles classification)
  670. (let ((mark-check (if (eq classification 'spam)
  671. 'spam-group-spam-mark-p
  672. 'spam-group-ham-mark-p))
  673. alist mark-cache-yes mark-cache-no)
  674. (dolist (article articles)
  675. (let ((mark (gnus-summary-article-mark article)))
  676. (unless (or (memq mark mark-cache-yes)
  677. (memq mark mark-cache-no))
  678. (if (funcall mark-check
  679. gnus-newsgroup-name
  680. mark)
  681. (push mark mark-cache-yes)
  682. (push mark mark-cache-no)))
  683. (when (memq mark mark-cache-yes)
  684. (push article alist))))
  685. alist))
  686. ;;}}}
  687. ;;{{{ backend installation functions and procedures
  688. (defun spam-install-backend-super (backend &rest properties)
  689. "Install BACKEND for spam.el.
  690. Accepts incoming CHECK, ham registration function HRF, spam
  691. registration function SRF, ham unregistration function HUF, spam
  692. unregistration function SUF, and an indication whether the
  693. backend is STATISTICAL."
  694. (setq spam-backends (add-to-list 'spam-backends backend))
  695. (while properties
  696. (let ((property (pop properties))
  697. (value (pop properties)))
  698. (if (spam-backend-property-valid-p property)
  699. (put backend property value)
  700. (gnus-error
  701. 5
  702. "spam-install-backend-super got an invalid property %s"
  703. property)))))
  704. (defun spam-backend-list (&optional type)
  705. "Return a list of all the backend symbols, constrained by TYPE.
  706. When TYPE is 'non-mover, only non-mover backends are returned.
  707. When TYPE is 'mover, only mover backends are returned."
  708. (let (list)
  709. (dolist (backend spam-backends)
  710. (when (or
  711. (null type) ;either no type was requested
  712. ;; or the type is 'mover and the backend is a mover
  713. (and
  714. (eq type 'mover)
  715. (spam-backend-mover-p backend))
  716. ;; or the type is 'non-mover and the backend is not a mover
  717. (and
  718. (eq type 'non-mover)
  719. (not (spam-backend-mover-p backend))))
  720. (push backend list)))
  721. list))
  722. (defun spam-backend-check (backend)
  723. "Get the check function for BACKEND.
  724. Each individual check may return nil, t, or a mailgroup name.
  725. The value nil means that the check does not yield a decision, and
  726. so, that further checks are needed. The value t means that the
  727. message is definitely not spam, and that further spam checks
  728. should be inhibited. Otherwise, a mailgroup name or the symbol
  729. 'spam (depending on `spam-split-symbolic-return') is returned where
  730. the mail should go, and further checks are also inhibited. The
  731. usual mailgroup name is the value of `spam-split-group', meaning
  732. that the message is definitely a spam."
  733. (get backend 'check))
  734. (defun spam-backend-valid-p (backend)
  735. "Is BACKEND valid?"
  736. (member backend (spam-backend-list)))
  737. (defun spam-backend-info (backend)
  738. "Return information about BACKEND."
  739. (if (spam-backend-valid-p backend)
  740. (let (info)
  741. (setq info (format "Backend %s has the following properties:\n"
  742. backend))
  743. (dolist (property (spam-backend-properties))
  744. (setq info (format "%s%s=%s\n"
  745. info
  746. property
  747. (get backend property))))
  748. info)
  749. (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
  750. backend)))
  751. (defun spam-backend-function (backend classification type)
  752. "Get the BACKEND function for CLASSIFICATION and TYPE.
  753. TYPE is 'registration or 'unregistration.
  754. CLASSIFICATION is 'ham or 'spam."
  755. (if (and
  756. (spam-classification-valid-p classification)
  757. (spam-backend-function-type-valid-p type))
  758. (let ((retrieval
  759. (intern
  760. (format "spam-backend-%s-%s-function"
  761. classification
  762. type))))
  763. (funcall retrieval backend))
  764. (gnus-error
  765. 5
  766. "%s was passed invalid backend %s, classification %s, or type %s"
  767. "spam-backend-function"
  768. backend
  769. classification
  770. type)))
  771. (defun spam-backend-article-list-property (classification
  772. &optional unregister)
  773. "Property name of article list with CLASSIFICATION and UNREGISTER."
  774. (let* ((r (if unregister "unregister" "register"))
  775. (prop (format "%s-%s" classification r)))
  776. prop))
  777. (defun spam-backend-get-article-todo-list (backend
  778. classification
  779. &optional unregister)
  780. "Get the articles to be processed for BACKEND and CLASSIFICATION.
  781. With UNREGISTER, get articles to be unregistered.
  782. This is a temporary storage function - nothing here persists."
  783. (get
  784. backend
  785. (intern (spam-backend-article-list-property classification unregister))))
  786. (defun spam-backend-put-article-todo-list (backend classification list
  787. &optional unregister)
  788. "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
  789. With UNREGISTER, set articles to be unregistered.
  790. This is a temporary storage function - nothing here persists."
  791. (put
  792. backend
  793. (intern (spam-backend-article-list-property classification unregister))
  794. list))
  795. (defun spam-backend-ham-registration-function (backend)
  796. "Get the ham registration function for BACKEND."
  797. (get backend 'hrf))
  798. (defun spam-backend-spam-registration-function (backend)
  799. "Get the spam registration function for BACKEND."
  800. (get backend 'srf))
  801. (defun spam-backend-ham-unregistration-function (backend)
  802. "Get the ham unregistration function for BACKEND."
  803. (get backend 'huf))
  804. (defun spam-backend-spam-unregistration-function (backend)
  805. "Get the spam unregistration function for BACKEND."
  806. (get backend 'suf))
  807. (defun spam-backend-statistical-p (backend)
  808. "Is BACKEND statistical?"
  809. (get backend 'statistical))
  810. (defun spam-backend-mover-p (backend)
  811. "Is BACKEND a mover?"
  812. (get backend 'mover))
  813. (defun spam-install-backend-alias (backend alias)
  814. "Add ALIAS to an existing BACKEND.
  815. The previous backend settings for ALIAS are erased."
  816. ;; install alias with no properties at first
  817. (spam-install-backend-super alias)
  818. (dolist (property (spam-backend-properties))
  819. (put alias property (get backend property))))
  820. (defun spam-install-checkonly-backend (backend check)
  821. "Install a BACKEND than can only CHECK for spam."
  822. (spam-install-backend-super backend 'check check))
  823. (defun spam-install-mover-backend (backend hrf srf huf suf)
  824. "Install a BACKEND than can move articles at summary exit.
  825. Accepts ham registration function HRF, spam registration function
  826. SRF, ham unregistration function HUF, spam unregistration
  827. function SUF. The backend has no incoming check and can't be
  828. statistical."
  829. (spam-install-backend-super
  830. backend
  831. 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
  832. (defun spam-install-nocheck-backend (backend hrf srf huf suf)
  833. "Install a BACKEND than has no check.
  834. Accepts ham registration function HRF, spam registration function
  835. SRF, ham unregistration function HUF, spam unregistration
  836. function SUF. The backend has no incoming check and can't be
  837. statistical (it could be, but in practice that doesn't happen)."
  838. (spam-install-backend-super
  839. backend
  840. 'hrf hrf 'srf srf 'huf huf 'suf suf))
  841. (defun spam-install-backend (backend check hrf srf huf suf)
  842. "Install a BACKEND.
  843. Accepts incoming CHECK, ham registration function HRF, spam
  844. registration function SRF, ham unregistration function HUF, spam
  845. unregistration function SUF. The backend won't be
  846. statistical (use `spam-install-statistical-backend' for that)."
  847. (spam-install-backend-super
  848. backend
  849. 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
  850. (defun spam-install-statistical-backend (backend check hrf srf huf suf)
  851. "Install a BACKEND.
  852. Accepts incoming CHECK, ham registration function HRF, spam
  853. registration function SRF, ham unregistration function HUF, spam
  854. unregistration function SUF. The backend will be
  855. statistical (use `spam-install-backend' for non-statistical
  856. backends)."
  857. (spam-install-backend-super
  858. backend
  859. 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
  860. (defun spam-install-statistical-checkonly-backend (backend check)
  861. "Install a statistical BACKEND than can only CHECK for spam."
  862. (spam-install-backend-super
  863. backend
  864. 'check check 'statistical t))
  865. ;;}}}
  866. ;;{{{ backend installations
  867. (spam-install-checkonly-backend 'spam-use-blackholes
  868. 'spam-check-blackholes)
  869. (spam-install-checkonly-backend 'spam-use-hashcash
  870. 'spam-check-hashcash)
  871. (spam-install-checkonly-backend 'spam-use-spamassassin-headers
  872. 'spam-check-spamassassin-headers)
  873. (spam-install-checkonly-backend 'spam-use-bogofilter-headers
  874. 'spam-check-bogofilter-headers)
  875. (spam-install-checkonly-backend 'spam-use-bsfilter-headers
  876. 'spam-check-bsfilter-headers)
  877. (spam-install-checkonly-backend 'spam-use-gmane-xref
  878. 'spam-check-gmane-xref)
  879. (spam-install-checkonly-backend 'spam-use-regex-headers
  880. 'spam-check-regex-headers)
  881. (spam-install-statistical-checkonly-backend 'spam-use-regex-body
  882. 'spam-check-regex-body)
  883. ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy)
  884. (spam-install-mover-backend 'spam-use-move
  885. 'spam-move-ham-routine
  886. 'spam-move-spam-routine
  887. nil
  888. nil)
  889. (spam-install-nocheck-backend 'spam-use-copy
  890. 'spam-copy-ham-routine
  891. 'spam-copy-spam-routine
  892. nil
  893. nil)
  894. (spam-install-nocheck-backend 'spam-use-gmane
  895. 'spam-report-gmane-unregister-routine
  896. 'spam-report-gmane-register-routine
  897. 'spam-report-gmane-register-routine
  898. 'spam-report-gmane-unregister-routine)
  899. (spam-install-nocheck-backend 'spam-use-resend
  900. 'spam-report-resend-register-ham-routine
  901. 'spam-report-resend-register-routine
  902. nil
  903. nil)
  904. (spam-install-backend 'spam-use-BBDB
  905. 'spam-check-BBDB
  906. 'spam-BBDB-register-routine
  907. nil
  908. 'spam-BBDB-unregister-routine
  909. nil)
  910. (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
  911. (spam-install-backend 'spam-use-blacklist
  912. 'spam-check-blacklist
  913. nil
  914. 'spam-blacklist-register-routine
  915. nil
  916. 'spam-blacklist-unregister-routine)
  917. (spam-install-backend 'spam-use-whitelist
  918. 'spam-check-whitelist
  919. 'spam-whitelist-register-routine
  920. nil
  921. 'spam-whitelist-unregister-routine
  922. nil)
  923. (spam-install-statistical-backend 'spam-use-ifile
  924. 'spam-check-ifile
  925. 'spam-ifile-register-ham-routine
  926. 'spam-ifile-register-spam-routine
  927. 'spam-ifile-unregister-ham-routine
  928. 'spam-ifile-unregister-spam-routine)
  929. (spam-install-statistical-backend 'spam-use-spamoracle
  930. 'spam-check-spamoracle
  931. 'spam-spamoracle-learn-ham
  932. 'spam-spamoracle-learn-spam
  933. 'spam-spamoracle-unlearn-ham
  934. 'spam-spamoracle-unlearn-spam)
  935. (spam-install-statistical-backend 'spam-use-stat
  936. 'spam-check-stat
  937. 'spam-stat-register-ham-routine
  938. 'spam-stat-register-spam-routine
  939. 'spam-stat-unregister-ham-routine
  940. 'spam-stat-unregister-spam-routine)
  941. (spam-install-statistical-backend 'spam-use-spamassassin
  942. 'spam-check-spamassassin
  943. 'spam-spamassassin-register-ham-routine
  944. 'spam-spamassassin-register-spam-routine
  945. 'spam-spamassassin-unregister-ham-routine
  946. 'spam-spamassassin-unregister-spam-routine)
  947. (spam-install-statistical-backend 'spam-use-bogofilter
  948. 'spam-check-bogofilter
  949. 'spam-bogofilter-register-ham-routine
  950. 'spam-bogofilter-register-spam-routine
  951. 'spam-bogofilter-unregister-ham-routine
  952. 'spam-bogofilter-unregister-spam-routine)
  953. (spam-install-statistical-backend 'spam-use-bsfilter
  954. 'spam-check-bsfilter
  955. 'spam-bsfilter-register-ham-routine
  956. 'spam-bsfilter-register-spam-routine
  957. 'spam-bsfilter-unregister-ham-routine
  958. 'spam-bsfilter-unregister-spam-routine)
  959. (spam-install-statistical-backend 'spam-use-crm114
  960. 'spam-check-crm114
  961. 'spam-crm114-register-ham-routine
  962. 'spam-crm114-register-spam-routine
  963. 'spam-crm114-unregister-ham-routine
  964. 'spam-crm114-unregister-spam-routine)
  965. ;;}}}
  966. ;;{{{ scoring and summary formatting
  967. (defun spam-necessary-extra-headers ()
  968. "Return the extra headers spam.el thinks are necessary."
  969. (let (list)
  970. (when (or spam-use-spamassassin
  971. spam-use-spamassassin-headers
  972. spam-use-regex-headers)
  973. (push 'X-Spam-Status list))
  974. (when (or spam-use-bogofilter
  975. spam-use-regex-headers)
  976. (push 'X-Bogosity list))
  977. (when (or spam-use-crm114
  978. spam-use-regex-headers)
  979. (push 'X-CRM114-Status list))
  980. list))
  981. (defun spam-user-format-function-S (headers)
  982. (when headers
  983. (format "%3.2f"
  984. (spam-summary-score headers spam-summary-score-preferred-header))))
  985. (defun spam-article-sort-by-spam-status (h1 h2)
  986. "Sort articles by score."
  987. (let (result)
  988. (dolist (header (spam-necessary-extra-headers))
  989. (let ((s1 (spam-summary-score h1 header))
  990. (s2 (spam-summary-score h2 header)))
  991. (unless (= s1 s2)
  992. (setq result (< s1 s2))
  993. (return))))
  994. result))
  995. (defvar spam-spamassassin-score-regexp
  996. ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
  997. "Regexp matching SpamAssassin score header.
  998. The first group must match the number.")
  999. (defun spam-extra-header-to-number (header headers)
  1000. "Transform an extra HEADER to a number, using list of HEADERS.
  1001. Note this has to be fast."
  1002. (let ((header-content (gnus-extra-header header headers)))
  1003. (if header-content
  1004. (cond
  1005. ((eq header 'X-Spam-Status)
  1006. (string-to-number (gnus-replace-in-string
  1007. header-content
  1008. spam-spamassassin-score-regexp
  1009. "\\1")))
  1010. ;; for CRM checking, it's probably faster to just do the string match
  1011. ((string-match "( pR: \\([0-9.-]+\\)" header-content)
  1012. (- (string-to-number (match-string 1 header-content))))
  1013. ((eq header 'X-Bogosity)
  1014. (string-to-number (gnus-replace-in-string
  1015. (gnus-replace-in-string
  1016. header-content
  1017. ".*spamicity=" "")
  1018. ",.*" "")))
  1019. (t nil))
  1020. nil)))
  1021. (defun spam-summary-score (headers &optional specific-header)
  1022. "Score an article for the summary buffer, as fast as possible.
  1023. With SPECIFIC-HEADER, returns only that header's score.
  1024. Will not return a nil score."
  1025. (let (score)
  1026. (dolist (header
  1027. (if specific-header
  1028. (list specific-header)
  1029. (spam-necessary-extra-headers)))
  1030. (setq score
  1031. (spam-extra-header-to-number header headers))
  1032. (when score
  1033. (return)))
  1034. (or score 0)))
  1035. (defun spam-generic-score (&optional recheck)
  1036. "Invoke whatever scoring method we can."
  1037. (interactive "P")
  1038. (cond
  1039. ((or spam-use-spamassassin spam-use-spamassassin-headers)
  1040. (spam-spamassassin-score recheck))
  1041. ((or spam-use-bsfilter spam-use-bsfilter-headers)
  1042. (spam-bsfilter-score recheck))
  1043. (spam-use-crm114
  1044. (spam-crm114-score))
  1045. (t (spam-bogofilter-score recheck))))
  1046. ;;}}}
  1047. ;;{{{ set up widening, processor checks
  1048. ;;; set up IMAP widening if it's necessary
  1049. (defun spam-setup-widening ()
  1050. (when (spam-widening-needed-p)
  1051. (setq nnimap-split-download-body-default t)))
  1052. (defun spam-widening-needed-p (&optional force-symbols)
  1053. (let (found)
  1054. (dolist (backend (spam-backend-list))
  1055. (when (and (spam-backend-statistical-p backend)
  1056. (or (symbol-value backend)
  1057. (memq backend force-symbols)))
  1058. (setq found backend)))
  1059. found))
  1060. (defvar spam-list-of-processors
  1061. ;; note the nil processors are not defined in gnus.el
  1062. '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
  1063. (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
  1064. (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
  1065. (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
  1066. (gnus-group-spam-exit-processor-stat spam spam-use-stat)
  1067. (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
  1068. (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
  1069. (gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) ;; Buggy?
  1070. (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
  1071. (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
  1072. (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
  1073. (gnus-group-ham-exit-processor-stat ham spam-use-stat)
  1074. (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
  1075. (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
  1076. (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
  1077. (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
  1078. (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
  1079. "The OBSOLETE `spam-list-of-processors' list.
  1080. This list contains pairs associating the obsolete ham/spam exit
  1081. processor variables with a classification and a spam-use-*
  1082. variable. When the processor variable is nil, just the
  1083. classification and spam-use-* check variable are used. This is
  1084. superseded by the new spam backend code, so it's only consulted
  1085. for backwards compatibility.")
  1086. (make-obsolete-variable 'spam-list-of-processors nil "22.1")
  1087. (defun spam-group-processor-p (group backend &optional classification)
  1088. "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
  1089. Also accepts the obsolete processors, which can be found in
  1090. gnus.el and in spam-list-of-processors. In the case of mover
  1091. backends, checks the setting of `spam-summary-exit-behavior' in
  1092. addition to the set values for the group."
  1093. (if (and (stringp group)
  1094. (symbolp backend))
  1095. (let ((old-style (assq backend spam-list-of-processors))
  1096. (parameters (nth 0 (gnus-parameter-spam-process group)))
  1097. found)
  1098. (if old-style ; old-style processor
  1099. (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
  1100. ;; now search for the parameter
  1101. (dolist (parameter parameters)
  1102. (when (and (null found)
  1103. (listp parameter)
  1104. (eq classification (nth 0 parameter))
  1105. (eq backend (nth 1 parameter)))
  1106. (setq found t)))
  1107. ;; now, if the parameter was not found, do the
  1108. ;; spam-summary-exit-behavior-logic for mover backends
  1109. (unless found
  1110. (when (spam-backend-mover-p backend)
  1111. (setq
  1112. found
  1113. (cond
  1114. ((eq spam-summary-exit-behavior 'move-all) t)
  1115. ((eq spam-summary-exit-behavior 'move-none) nil)
  1116. ((eq spam-summary-exit-behavior 'default)
  1117. (or (eq classification 'spam) ;move spam out of all groups
  1118. ;; move ham out of spam groups
  1119. (and (eq classification 'ham)
  1120. (spam-group-spam-contents-p group))))
  1121. (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
  1122. spam-summary-exit-behavior))))))
  1123. found))
  1124. nil))
  1125. ;;}}}
  1126. ;;{{{ Summary entry and exit processing.
  1127. (defun spam-mark-junk-as-spam-routine ()
  1128. ;; check the global list of group names spam-junk-mailgroups and the
  1129. ;; group parameters
  1130. (when (spam-group-spam-contents-p gnus-newsgroup-name)
  1131. (gnus-message 6 "Marking %s articles as spam"
  1132. (if spam-mark-only-unseen-as-spam
  1133. "unseen"
  1134. "unread"))
  1135. (let ((articles (if spam-mark-only-unseen-as-spam
  1136. gnus-newsgroup-unseen
  1137. gnus-newsgroup-unreads)))
  1138. (if spam-mark-new-messages-in-spam-group-as-spam
  1139. (dolist (article articles)
  1140. (gnus-summary-mark-article article gnus-spam-mark))
  1141. (gnus-message 9 "Did not mark new messages as spam.")))))
  1142. (defun spam-summary-prepare ()
  1143. (setq spam-old-articles
  1144. (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
  1145. (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
  1146. (spam-mark-junk-as-spam-routine))
  1147. ;; The spam processors are invoked for any group, spam or ham or neither
  1148. (defun spam-summary-prepare-exit ()
  1149. (unless gnus-group-is-exiting-without-update-p
  1150. (gnus-message 6 "Exiting summary buffer and applying spam rules")
  1151. ;; before we begin, remove any article limits
  1152. ; (ignore-errors
  1153. ; (gnus-summary-pop-limit t))
  1154. ;; first of all, unregister any articles that are no longer ham or spam
  1155. ;; we have to iterate over the processors, or else we'll be too slow
  1156. (dolist (classification (spam-classifications))
  1157. (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
  1158. (new-articles (spam-list-articles
  1159. gnus-newsgroup-articles
  1160. classification))
  1161. (changed-articles (spam-set-difference new-articles old-articles)))
  1162. ;; now that we have the changed articles, we go through the processors
  1163. (dolist (backend (spam-backend-list))
  1164. (let (unregister-list)
  1165. (dolist (article changed-articles)
  1166. (let ((id (spam-fetch-field-message-id-fast article)))
  1167. (when (spam-log-unregistration-needed-p
  1168. id 'process classification backend)
  1169. (push article unregister-list))))
  1170. ;; call spam-register-routine with specific articles to unregister,
  1171. ;; when there are articles to unregister and the check is enabled
  1172. (when (and unregister-list (symbol-value backend))
  1173. (spam-backend-put-article-todo-list backend
  1174. classification
  1175. unregister-list
  1176. t))))))
  1177. ;; do the non-moving backends first, then the moving ones
  1178. (dolist (backend-type '(non-mover mover))
  1179. (dolist (classification (spam-classifications))
  1180. (dolist (backend (spam-backend-list backend-type))
  1181. (when (spam-group-processor-p
  1182. gnus-newsgroup-name
  1183. backend
  1184. classification)
  1185. (spam-backend-put-article-todo-list backend
  1186. classification
  1187. (spam-list-articles
  1188. gnus-newsgroup-articles
  1189. classification))))))
  1190. (spam-resolve-registrations-routine) ; do the registrations now
  1191. ;; we mark all the leftover spam articles as expired at the end
  1192. (dolist (article (spam-list-articles
  1193. gnus-newsgroup-articles
  1194. 'spam))
  1195. (gnus-summary-mark-article article gnus-expirable-mark)))
  1196. (setq spam-old-articles nil))
  1197. ;;}}}
  1198. ;;{{{ spam-use-move and spam-use-copy backend support functions
  1199. (defun spam-copy-or-move-routine (copy groups articles classification)
  1200. (when (and (car-safe groups) (listp (car-safe groups)))
  1201. (setq groups (pop groups)))
  1202. (unless (listp groups)
  1203. (setq groups (list groups)))
  1204. ;; remove the current process mark
  1205. (gnus-summary-kill-process-mark)
  1206. (let ((backend-supports-deletions
  1207. (gnus-check-backend-function
  1208. 'request-move-article gnus-newsgroup-name))
  1209. (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
  1210. article mark deletep respool valid-move-destinations)
  1211. (when (member 'respool groups)
  1212. (setq respool t) ; boolean for later
  1213. (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
  1214. ;; exclude invalid move destinations
  1215. (dolist (group groups)
  1216. (unless
  1217. (or
  1218. (and
  1219. (eq classification 'spam)
  1220. (spam-group-spam-contents-p gnus-newsgroup-name)
  1221. (spam-group-spam-contents-p group)
  1222. (gnus-message
  1223. 3
  1224. "Sorry, can't move spam from spam group %s to spam group %s"
  1225. gnus-newsgroup-name
  1226. group))
  1227. (and
  1228. (eq classification 'ham)
  1229. (spam-group-ham-contents-p gnus-newsgroup-name)
  1230. (spam-group-ham-contents-p group)
  1231. (gnus-message
  1232. 3
  1233. "Sorry, can't move ham from ham group %s to ham group %s"
  1234. gnus-newsgroup-name
  1235. group)))
  1236. (push group valid-move-destinations)))
  1237. (setq groups (nreverse valid-move-destinations))
  1238. ;; now do the actual move
  1239. (dolist (group groups)
  1240. (when (and articles (stringp group))
  1241. ;; first, mark the article with the process mark and, if needed,
  1242. ;; the unread or expired mark (for ham and spam respectively)
  1243. (dolist (article articles)
  1244. (when (and (eq classification 'ham)
  1245. spam-mark-ham-unread-before-move-from-spam-group)
  1246. (gnus-message 9 "Marking ham article %d unread before move"
  1247. article)
  1248. (gnus-summary-mark-article article gnus-unread-mark))
  1249. (when (and (eq classification 'spam)
  1250. (not copy))
  1251. (gnus-message 9 "Marking spam article %d expirable before move"
  1252. article)
  1253. (gnus-summary-mark-article article gnus-expirable-mark))
  1254. (gnus-summary-set-process-mark article)
  1255. (if respool ; respooling is with a "fake" group
  1256. (let ((spam-split-disabled
  1257. (or spam-split-disabled
  1258. (and (eq classification 'ham)
  1259. spam-disable-spam-split-during-ham-respool))))
  1260. (gnus-message 9 "Respooling article %d with method %s"
  1261. article respool-method)
  1262. (gnus-summary-respool-article nil respool-method))
  1263. ;; else, we are not respooling
  1264. (if (or (not backend-supports-deletions)
  1265. (> (length groups) 1))
  1266. (progn ; if copying, copy and set deletep
  1267. (gnus-message 9 "Copying article %d to group %s"
  1268. article group)
  1269. (gnus-summary-copy-article nil group)
  1270. (setq deletep t))
  1271. (gnus-message 9 "Moving article %d to group %s"
  1272. article group)
  1273. (gnus-summary-move-article nil group)))))) ; else move articles
  1274. ;; now delete the articles, unless a) copy is t, and there was a copy done
  1275. ;; b) a move was done to a single group
  1276. ;; c) backend-supports-deletions is nil
  1277. (unless copy
  1278. (when (and deletep backend-supports-deletions)
  1279. (dolist (article articles)
  1280. (gnus-summary-set-process-mark article)
  1281. (gnus-message 9 "Deleting article %d" article))
  1282. (when articles
  1283. (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
  1284. (gnus-summary-delete-article nil)))))
  1285. (gnus-summary-yank-process-mark)
  1286. (length articles)))
  1287. (defun spam-copy-spam-routine (articles)
  1288. (spam-copy-or-move-routine
  1289. t
  1290. (gnus-parameter-spam-process-destination gnus-newsgroup-name)
  1291. articles
  1292. 'spam))
  1293. (defun spam-move-spam-routine (articles)
  1294. (spam-copy-or-move-routine
  1295. nil
  1296. (gnus-parameter-spam-process-destination gnus-newsgroup-name)
  1297. articles
  1298. 'spam))
  1299. (defun spam-copy-ham-routine (articles)
  1300. (spam-copy-or-move-routine
  1301. t
  1302. (gnus-parameter-ham-process-destination gnus-newsgroup-name)
  1303. articles
  1304. 'ham))
  1305. (defun spam-move-ham-routine (articles)
  1306. (spam-copy-or-move-routine
  1307. nil
  1308. (gnus-parameter-ham-process-destination gnus-newsgroup-name)
  1309. articles
  1310. 'ham))
  1311. ;;}}}
  1312. ;;{{{ article and field retrieval code
  1313. (defun spam-get-article-as-string (article)
  1314. (when (numberp article)
  1315. (with-temp-buffer
  1316. (gnus-request-article-this-buffer
  1317. article
  1318. gnus-newsgroup-name)
  1319. (buffer-string))))
  1320. ;; disabled for now
  1321. ;; (defun spam-get-article-as-filename (article)
  1322. ;; (let ((article-filename))
  1323. ;; (when (numberp article)
  1324. ;; (nnml-possibly-change-directory
  1325. ;; (gnus-group-real-name gnus-newsgroup-name))
  1326. ;; (setq article-filename (expand-file-name
  1327. ;; (int-to-string article) nnml-current-directory)))
  1328. ;; (if (file-exists-p article-filename)
  1329. ;; article-filename
  1330. ;; nil)))
  1331. (defun spam-fetch-field-fast (article field &optional prepared-data-header)
  1332. "Fetch a FIELD for ARTICLE with the internal `gnus-data-list' function.
  1333. When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
  1334. When FIELD is 'number, ARTICLE can be any number (since we want
  1335. to find it out)."
  1336. (when (numberp article)
  1337. (let* ((data-header (or prepared-data-header
  1338. (spam-fetch-article-header article))))
  1339. (cond
  1340. ((not (arrayp data-header))
  1341. (gnus-message 6 "Article %d has a nil data header" article))
  1342. ((equal field 'number)
  1343. (mail-header-number data-header))
  1344. ((equal field 'from)
  1345. (mail-header-from data-header))
  1346. ((equal field 'message-id)
  1347. (mail-header-message-id data-header))
  1348. ((equal field 'subject)
  1349. (mail-header-subject data-header))
  1350. ((equal field 'references)
  1351. (mail-header-references data-header))
  1352. ((equal field 'date)
  1353. (mail-header-date data-header))
  1354. ((equal field 'xref)
  1355. (mail-header-xref data-header))
  1356. ((equal field 'extra)
  1357. (mail-header-extra data-header))
  1358. (t
  1359. (gnus-error
  1360. 5
  1361. "spam-fetch-field-fast: unknown field %s requested"
  1362. field)
  1363. nil)))))
  1364. (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
  1365. (spam-fetch-field-fast article 'from prepared-data-header))
  1366. (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
  1367. (spam-fetch-field-fast article 'subject prepared-data-header))
  1368. (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
  1369. (spam-fetch-field-fast article 'message-id prepared-data-header))
  1370. (defun spam-generate-fake-headers (article)
  1371. (let ((dh (spam-fetch-article-header article)))
  1372. (if dh
  1373. (concat
  1374. (format
  1375. ;; 80-character limit makes for strange constructs
  1376. (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
  1377. "Date: %s\nReferences: %s\nXref: %s\n")
  1378. (spam-fetch-field-fast article 'from dh)
  1379. (spam-fetch-field-fast article 'subject dh)
  1380. (spam-fetch-field-fast article 'message-id dh)
  1381. (spam-fetch-field-fast article 'date dh)
  1382. (spam-fetch-field-fast article 'references dh)
  1383. (spam-fetch-field-fast article 'xref dh))
  1384. (when (spam-fetch-field-fast article 'extra dh)
  1385. (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
  1386. (gnus-message
  1387. 5
  1388. "spam-generate-fake-headers: article %d didn't have a valid header"
  1389. article))))
  1390. (defun spam-fetch-article-header (article)
  1391. (with-current-buffer gnus-summary-buffer
  1392. (gnus-read-header article)
  1393. (nth 3 (assq article gnus-newsgroup-data))))
  1394. ;;}}}
  1395. ;;{{{ Spam determination.
  1396. (defun spam-split (&rest specific-checks)
  1397. "Split this message into the `spam' group if it is spam.
  1398. This function can be used as an entry in the variable `nnmail-split-fancy',
  1399. for example like this: (: spam-split). It can take checks as
  1400. parameters. A string as a parameter will set the
  1401. `spam-split-group' to that string.
  1402. See the Info node `(gnus)Fancy Mail Splitting' for more details."
  1403. (interactive)
  1404. (setq spam-split-last-successful-check nil)
  1405. (unless spam-split-disabled
  1406. (let ((spam-split-group-choice spam-split-group))
  1407. (dolist (check specific-checks)
  1408. (when (stringp check)
  1409. (setq spam-split-group-choice check)
  1410. (setq specific-checks (delq check specific-checks))))
  1411. (let ((spam-split-group spam-split-group-choice)
  1412. (widening-needed-check (spam-widening-needed-p specific-checks)))
  1413. (save-excursion
  1414. (save-restriction
  1415. (when widening-needed-check
  1416. (widen)
  1417. (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
  1418. widening-needed-check))
  1419. (let ((backends (spam-backend-list))
  1420. decision)
  1421. (while (and backends (not decision))
  1422. (let* ((backend (pop backends))
  1423. (check-function (spam-backend-check backend))
  1424. (spam-split-group (if spam-split-symbolic-return
  1425. 'spam
  1426. spam-split-group)))
  1427. (when (or
  1428. ;; either, given specific checks, this is one of them
  1429. (memq backend specific-checks)
  1430. ;; or, given no specific checks, spam-use-CHECK is set
  1431. (and (null specific-checks) (symbol-value backend)))
  1432. (gnus-message 6 "spam-split: calling the %s function"
  1433. check-function)
  1434. (setq decision (funcall check-function))
  1435. ;; if we got a decision at all, save the current check
  1436. (when decision
  1437. (setq spam-split-last-successful-check backend))
  1438. (when (eq decision 'spam)
  1439. (unless spam-split-symbolic-return
  1440. (gnus-error
  1441. 5
  1442. (format "spam-split got %s but %s is nil"
  1443. decision
  1444. spam-split-symbolic-return)))))))
  1445. (if (eq decision t)
  1446. (if spam-split-symbolic-return-positive 'ham nil)
  1447. decision))))))))
  1448. (defun spam-find-spam ()
  1449. "Detect spam in the current newsgroup using `spam-split'."
  1450. (interactive)
  1451. (let* ((group gnus-newsgroup-name)
  1452. (autodetect (gnus-parameter-spam-autodetect group))
  1453. (methods (gnus-parameter-spam-autodetect-methods group))
  1454. (first-method (nth 0 methods))
  1455. (articles (if spam-autodetect-recheck-messages
  1456. gnus-newsgroup-articles
  1457. gnus-newsgroup-unseen))
  1458. article-cannot-be-faked)
  1459. (dolist (backend methods)
  1460. (when (spam-backend-statistical-p backend)
  1461. (setq article-cannot-be-faked t)
  1462. (return)))
  1463. (when (memq 'default methods)
  1464. (setq article-cannot-be-faked t))
  1465. (when (and autodetect
  1466. (not (equal first-method 'none)))
  1467. (mapcar
  1468. (lambda (article)
  1469. (let ((id (spam-fetch-field-message-id-fast article))
  1470. (subject (spam-fetch-field-subject-fast article))
  1471. (sender (spam-fetch-field-from-fast article))
  1472. registry-lookup)
  1473. (unless id
  1474. (gnus-message 6 "Article %d has no message ID!" article))
  1475. (when (and id spam-log-to-registry)
  1476. (setq registry-lookup (spam-log-registration-type id 'incoming))
  1477. (when registry-lookup
  1478. (gnus-message
  1479. 9
  1480. "spam-find-spam: message %s was already registered incoming"
  1481. id)))
  1482. (let* ((spam-split-symbolic-return t)
  1483. (spam-split-symbolic-return-positive t)
  1484. (fake-headers (spam-generate-fake-headers article))
  1485. (split-return
  1486. (or registry-lookup
  1487. (with-temp-buffer
  1488. (if article-cannot-be-faked
  1489. (gnus-request-article-this-buffer
  1490. article
  1491. group)
  1492. ;; else, we fake the article
  1493. (when fake-headers (insert fake-headers)))
  1494. (if (or (null first-method)
  1495. (equal first-method 'default))
  1496. (spam-split)
  1497. (apply 'spam-split methods))))))
  1498. (if (equal split-return 'spam)
  1499. (gnus-summary-mark-article article gnus-spam-mark))
  1500. (when (and id split-return spam-log-to-registry)
  1501. (when (zerop (gnus-registry-group-count id))
  1502. (gnus-registry-handle-action id nil group subject sender))
  1503. (unless registry-lookup
  1504. (spam-log-processing-to-registry
  1505. id
  1506. 'incoming
  1507. split-return
  1508. spam-split-last-successful-check
  1509. group))))))
  1510. articles))))
  1511. ;;}}}
  1512. ;;{{{ registration/unregistration functions
  1513. (defun spam-resolve-registrations-routine ()
  1514. "Go through the backends and register or unregister articles as needed."
  1515. (dolist (backend-type '(non-mover mover))
  1516. (dolist (classification (spam-classifications))
  1517. (dolist (backend (spam-backend-list backend-type))
  1518. (let ((rlist (spam-backend-get-article-todo-list
  1519. backend classification))
  1520. (ulist (spam-backend-get-article-todo-list
  1521. backend classification t))
  1522. (delcount 0))
  1523. ;; clear the old lists right away
  1524. (spam-backend-put-article-todo-list backend
  1525. classification
  1526. nil
  1527. nil)
  1528. (spam-backend-put-article-todo-list backend
  1529. classification
  1530. nil
  1531. t)
  1532. ;; eliminate duplicates
  1533. (dolist (article (copy-sequence ulist))
  1534. (when (memq article rlist)
  1535. (incf delcount)
  1536. (setq rlist (delq article rlist))
  1537. (setq ulist (delq article ulist))))
  1538. (unless (zerop delcount)
  1539. (gnus-message
  1540. 9
  1541. "%d messages did not have to unregister and then register"
  1542. delcount))
  1543. ;; unregister articles
  1544. (unless (zerop (length ulist))
  1545. (let ((num (spam-unregister-routine classification backend ulist)))
  1546. (when (> num 0)
  1547. (gnus-message
  1548. 6
  1549. "%d %s messages were unregistered by backend %s."
  1550. num
  1551. classification
  1552. backend))))
  1553. ;; register articles
  1554. (unless (zerop (length rlist))
  1555. (let ((num (spam-register-routine classification backend rlist)))
  1556. (when (> num 0)
  1557. (gnus-message
  1558. 6
  1559. "%d %s messages were registered by backend %s."
  1560. num
  1561. classification
  1562. backend)))))))))
  1563. (defun spam-unregister-routine (classification
  1564. backend
  1565. specific-articles)
  1566. (spam-register-routine classification backend specific-articles t))
  1567. (defun spam-register-routine (classification
  1568. backend
  1569. specific-articles
  1570. &optional unregister)
  1571. (when (and (spam-classification-valid-p classification)
  1572. (spam-backend-valid-p backend))
  1573. (let* ((register-function
  1574. (spam-backend-function backend classification 'registration))
  1575. (unregister-function
  1576. (spam-backend-function backend classification 'unregistration))
  1577. (run-function (if unregister
  1578. unregister-function
  1579. register-function))
  1580. (log-function (if unregister
  1581. 'spam-log-undo-registration
  1582. 'spam-log-processing-to-registry))
  1583. article articles)
  1584. (when run-function
  1585. ;; make list of articles, using specific-articles if given
  1586. (setq articles (or specific-articles
  1587. (spam-list-articles
  1588. gnus-newsgroup-articles
  1589. classification)))
  1590. ;; process them
  1591. (when (> (length articles) 0)
  1592. (gnus-message 5 "%s %d %s articles as %s using backend %s"
  1593. (if unregister "Unregistering" "Registering")
  1594. (length articles)
  1595. (if specific-articles "specific" "")
  1596. classification
  1597. backend)
  1598. (funcall run-function articles)
  1599. ;; now log all the registrations (or undo them, depending on
  1600. ;; unregister)
  1601. (dolist (article articles)
  1602. (funcall log-function
  1603. (spam-fetch-field-message-id-fast article)
  1604. 'process
  1605. classification
  1606. backend
  1607. gnus-newsgroup-name))))
  1608. ;; return the number of articles processed
  1609. (length articles))))
  1610. ;;; log a ham- or spam-processor invocation to the registry
  1611. (defun spam-log-processing-to-registry (id type classification backend group)
  1612. (when spam-log-to-registry
  1613. (if (and (stringp id)
  1614. (stringp group)
  1615. (spam-process-type-valid-p type)
  1616. (spam-classification-valid-p classification)
  1617. (spam-backend-valid-p backend))
  1618. (let ((cell-list (gnus-registry-get-id-key id type))
  1619. (cell (list classification backend group)))
  1620. (push cell cell-list)
  1621. (gnus-registry-set-id-key id type cell-list))
  1622. (gnus-error
  1623. 7
  1624. (format
  1625. "%s call with bad ID, type, classification, spam-backend, or group"
  1626. "spam-log-processing-to-registry")))))
  1627. ;;; check if a ham- or spam-processor registration has been done
  1628. (defun spam-log-registered-p (id type)
  1629. (when spam-log-to-registry
  1630. (if (and (stringp id)
  1631. (spam-process-type-valid-p type))
  1632. (gnus-registry-get-id-key id type)
  1633. (progn
  1634. (gnus-error
  1635. 7
  1636. (format "%s called with bad ID, type, classification, or spam-backend"
  1637. "spam-log-registered-p"))
  1638. nil))))
  1639. ;;; check what a ham- or spam-processor registration says
  1640. ;;; returns nil if conflicting registrations are found
  1641. (defun spam-log-registration-type (id type)
  1642. (let ((count 0)
  1643. decision)
  1644. (dolist (reg (spam-log-registered-p id type))
  1645. (let ((classification (nth 0 reg)))
  1646. (when (spam-classification-valid-p classification)
  1647. (when (and decision
  1648. (not (eq classification decision)))
  1649. (setq count (+ 1 count)))
  1650. (setq decision classification))))
  1651. (if (< 0 count)
  1652. nil
  1653. decision)))
  1654. ;;; check if a ham- or spam-processor registration needs to be undone
  1655. (defun spam-log-unregistration-needed-p (id type classification backend)
  1656. (when spam-log-to-registry
  1657. (if (and (stringp id)
  1658. (spam-process-type-valid-p type)
  1659. (spam-classification-valid-p classification)
  1660. (spam-backend-valid-p backend))
  1661. (let ((cell-list (gnus-registry-get-id-key id type))
  1662. found)
  1663. (dolist (cell cell-list)
  1664. (unless found
  1665. (when (and (eq classification (nth 0 cell))
  1666. (eq backend (nth 1 cell)))
  1667. (setq found t))))
  1668. found)
  1669. (progn
  1670. (gnus-error
  1671. 7
  1672. (format "%s called with bad ID, type, classification, or spam-backend"
  1673. "spam-log-unregistration-needed-p"))
  1674. nil))))
  1675. ;;; undo a ham- or spam-processor registration (the group is not used)
  1676. (defun spam-log-undo-registration (id type classification backend
  1677. &optional group)
  1678. (when (and spam-log-to-registry
  1679. (spam-log-unregistration-needed-p id type classification backend))
  1680. (if (and (stringp id)
  1681. (spam-process-type-valid-p type)
  1682. (spam-classification-valid-p classification)
  1683. (spam-backend-valid-p backend))
  1684. (let ((cell-list (gnus-registry-get-id-key id type))
  1685. new-cell-list found)
  1686. (dolist (cell cell-list)
  1687. (unless (and (eq classification (nth 0 cell))
  1688. (eq backend (nth 1 cell)))
  1689. (push cell new-cell-list)))
  1690. (gnus-registry-set-id-key id type new-cell-list))
  1691. (progn
  1692. (gnus-error 7 (format
  1693. "%s call with bad ID, type, spam-backend, or group"
  1694. "spam-log-undo-registration"))
  1695. nil))))
  1696. ;;}}}
  1697. ;;{{{ backend functions
  1698. ;;{{{ Gmane xrefs
  1699. (defun spam-check-gmane-xref ()
  1700. (let ((header (or
  1701. (message-fetch-field "Xref")
  1702. (message-fetch-field "Newsgroups"))))
  1703. (when header ; return nil when no header
  1704. (when (string-match spam-gmane-xref-spam-group
  1705. header)
  1706. spam-split-group))))
  1707. ;;}}}
  1708. ;;{{{ Regex body
  1709. (defun spam-check-regex-body ()
  1710. (let ((spam-regex-headers-ham spam-regex-body-ham)
  1711. (spam-regex-headers-spam spam-regex-body-spam))
  1712. (spam-check-regex-headers t)))
  1713. ;;}}}
  1714. ;;{{{ Regex headers
  1715. (defun spam-check-regex-headers (&optional body)
  1716. (let ((type (if body "body" "header"))
  1717. ret found)
  1718. (dolist (h-regex spam-regex-headers-ham)
  1719. (unless found
  1720. (goto-char (point-min))
  1721. (when (re-search-forward h-regex nil t)
  1722. (message "Ham regex %s search positive." type)
  1723. (setq found t))))
  1724. (dolist (s-regex spam-regex-headers-spam)
  1725. (unless found
  1726. (goto-char (point-min))
  1727. (when (re-search-forward s-regex nil t)
  1728. (message "Spam regex %s search positive." type)
  1729. (setq found t)
  1730. (setq ret spam-split-group))))
  1731. ret))
  1732. ;;}}}
  1733. ;;{{{ Blackholes.
  1734. (defun spam-reverse-ip-string (ip)
  1735. (when (stringp ip)
  1736. (mapconcat 'identity
  1737. (nreverse (split-string ip "\\."))
  1738. ".")))
  1739. (defun spam-check-blackholes ()
  1740. "Check the Received headers for blackholed relays."
  1741. (let ((headers (message-fetch-field "received"))
  1742. ips matches)
  1743. (when headers
  1744. (with-temp-buffer
  1745. (insert headers)
  1746. (goto-char (point-min))
  1747. (gnus-message 6 "Checking headers for relay addresses")
  1748. (while (re-search-forward
  1749. "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
  1750. (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
  1751. (push (spam-reverse-ip-string (match-string 1))
  1752. ips)))
  1753. (dolist (server spam-blackhole-servers)
  1754. (dolist (ip ips)
  1755. (unless (and spam-blackhole-good-server-regex
  1756. ;; match against the reversed (again) IP string
  1757. (string-match
  1758. spam-blackhole-good-server-regex
  1759. (spam-reverse-ip-string ip)))
  1760. (unless matches
  1761. (let ((query-string (concat ip "." server)))
  1762. (if spam-use-dig
  1763. (let ((query-result (query-dig query-string)))
  1764. (when query-result
  1765. (gnus-message 6 "(DIG): positive blackhole check '%s'"
  1766. query-result)
  1767. (push (list ip server query-result)
  1768. matches)))
  1769. ;; else, if not using dig.el
  1770. (when (dns-query query-string)
  1771. (gnus-message 6 "positive blackhole check")
  1772. (push (list ip server (dns-query query-string 'TXT))
  1773. matches)))))))))
  1774. (when matches
  1775. spam-split-group)))
  1776. ;;}}}
  1777. ;;{{{ Hashcash.
  1778. (defun spam-check-hashcash ()
  1779. "Check the headers for hashcash payments."
  1780. (ignore-errors (mail-check-payment))) ;mail-check-payment returns a boolean
  1781. ;;}}}
  1782. ;;{{{ BBDB
  1783. ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
  1784. ;;; <sacha@giotto.sj.ru>
  1785. ;; all this is done inside a condition-case to trap errors
  1786. (eval-when-compile
  1787. (autoload 'bbdb-buffer "bbdb")
  1788. (autoload 'bbdb-create-internal "bbdb")
  1789. (autoload 'bbdb-search-simple "bbdb"))
  1790. ;; Autoloaded in message, which we require.
  1791. (declare-function gnus-extract-address-components "gnus-util" (from))
  1792. (eval-and-compile
  1793. (when (condition-case nil
  1794. (progn
  1795. (require 'bbdb)
  1796. (require 'bbdb-com))
  1797. (file-error
  1798. ;; `bbdb-records' should not be bound as an autoload function
  1799. ;; before loading bbdb because of `bbdb-hashtable-size'.
  1800. (defalias 'bbdb-records 'ignore)
  1801. (defalias 'spam-BBDB-register-routine 'ignore)
  1802. (defalias 'spam-enter-ham-BBDB 'ignore)
  1803. nil))
  1804. ;; when the BBDB changes, we want to clear out our cache
  1805. (defun spam-clear-cache-BBDB (&rest immaterial)
  1806. (spam-clear-cache 'spam-use-BBDB))
  1807. (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
  1808. (defun spam-enter-ham-BBDB (addresses &optional remove)
  1809. "Enter an address into the BBDB; implies ham (non-spam) sender"
  1810. (dolist (from addresses)
  1811. (when (stringp from)
  1812. (let* ((parsed-address (gnus-extract-address-components from))
  1813. (name (or (nth 0 parsed-address) "Ham Sender"))
  1814. (remove-function (if remove
  1815. 'bbdb-delete-record-internal
  1816. 'ignore))
  1817. (net-address (nth 1 parsed-address))
  1818. (record (and net-address
  1819. (bbdb-search-simple nil net-address))))
  1820. (when net-address
  1821. (gnus-message 6 "%s address %s %s BBDB"
  1822. (if remove "Deleting" "Adding")
  1823. from
  1824. (if remove "from" "to"))
  1825. (if record
  1826. (funcall remove-function record)
  1827. (bbdb-create-internal name nil net-address nil nil
  1828. "ham sender added by spam.el")))))))
  1829. (defun spam-BBDB-register-routine (articles &optional unregister)
  1830. (let (addresses)
  1831. (dolist (article articles)
  1832. (when (stringp (spam-fetch-field-from-fast article))
  1833. (push (spam-fetch-field-from-fast article) addresses)))
  1834. ;; now do the register/unregister action
  1835. (spam-enter-ham-BBDB addresses unregister)))
  1836. (defun spam-BBDB-unregister-routine (articles)
  1837. (spam-BBDB-register-routine articles t))
  1838. (defun spam-check-BBDB ()
  1839. "Mail from people in the BBDB is classified as ham or non-spam"
  1840. (let ((who (message-fetch-field "from")))
  1841. (when who
  1842. (setq who (nth 1 (gnus-extract-address-components who)))
  1843. (if
  1844. (if (fboundp 'bbdb-search)
  1845. (bbdb-search (bbdb-records) who) ;; v3
  1846. (bbdb-search-simple nil who)) ;; v2
  1847. t
  1848. (if spam-use-BBDB-exclusive
  1849. spam-split-group
  1850. nil)))))))
  1851. ;;}}}
  1852. ;;{{{ ifile
  1853. ;;; check the ifile backend; return nil if the mail was NOT classified
  1854. ;;; as spam
  1855. (defun spam-get-ifile-database-parameter ()
  1856. "Return the command-line parameter for ifile's database.
  1857. See `spam-ifile-database'."
  1858. (if spam-ifile-database
  1859. (format "--db-file=%s" spam-ifile-database)
  1860. nil))
  1861. (defun spam-check-ifile ()
  1862. "Check the ifile backend for the classification of this message."
  1863. (let ((article-buffer-name (buffer-name))
  1864. category return)
  1865. (with-temp-buffer
  1866. (let ((temp-buffer-name (buffer-name))
  1867. (db-param (spam-get-ifile-database-parameter)))
  1868. (with-current-buffer article-buffer-name
  1869. (apply 'call-process-region
  1870. (point-min) (point-max) spam-ifile-program
  1871. nil temp-buffer-name nil "-c"
  1872. (if db-param `(,db-param "-q") `("-q"))))
  1873. ;; check the return now (we're back in the temp buffer)
  1874. (goto-char (point-min))
  1875. (if (not (eobp))
  1876. (setq category (buffer-substring (point) (point-at-eol))))
  1877. (when (not (zerop (length category))) ; we need a category here
  1878. (if spam-ifile-all-categories
  1879. (setq return category)
  1880. ;; else, if spam-ifile-all-categories is not set...
  1881. (when (string-equal spam-ifile-spam-category category)
  1882. (setq return spam-split-group)))))) ; note return is nil otherwise
  1883. return))
  1884. (defun spam-ifile-register-with-ifile (articles category &optional unregister)
  1885. "Register an article, given as a string, with a category.
  1886. Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
  1887. (let ((category (or category gnus-newsgroup-name))
  1888. (add-or-delete-option (if unregister "-d" "-i"))
  1889. (db (spam-get-ifile-database-parameter))
  1890. parameters)
  1891. (with-temp-buffer
  1892. (dolist (article articles)
  1893. (let ((article-string (spam-get-article-as-string article)))
  1894. (when (stringp article-string)
  1895. (insert article-string))))
  1896. (apply 'call-process-region
  1897. (point-min) (point-max) spam-ifile-program
  1898. nil nil nil
  1899. add-or-delete-option category
  1900. (if db `(,db "-h") `("-h"))))))
  1901. (defun spam-ifile-register-spam-routine (articles &optional unregister)
  1902. (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
  1903. (defun spam-ifile-unregister-spam-routine (articles)
  1904. (spam-ifile-register-spam-routine articles t))
  1905. (defun spam-ifile-register-ham-routine (articles &optional unregister)
  1906. (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
  1907. (defun spam-ifile-unregister-ham-routine (articles)
  1908. (spam-ifile-register-ham-routine articles t))
  1909. ;;}}}
  1910. ;;{{{ spam-stat
  1911. (eval-when-compile
  1912. (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
  1913. (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
  1914. (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
  1915. (autoload 'spam-stat-buffer-is-spam "spam-stat")
  1916. (autoload 'spam-stat-load "spam-stat")
  1917. (autoload 'spam-stat-save "spam-stat")
  1918. (autoload 'spam-stat-split-fancy "spam-stat"))
  1919. (require 'spam-stat)
  1920. (defun spam-check-stat ()
  1921. "Check the spam-stat backend for the classification of this message"
  1922. (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
  1923. (spam-stat-buffer (buffer-name)) ; stat the current buffer
  1924. category return)
  1925. (spam-stat-split-fancy)))
  1926. (defun spam-stat-register-spam-routine (articles &optional unregister)
  1927. (dolist (article articles)
  1928. (let ((article-string (spam-get-article-as-string article)))
  1929. (with-temp-buffer
  1930. (insert article-string)
  1931. (if unregister
  1932. (spam-stat-buffer-change-to-non-spam)
  1933. (spam-stat-buffer-is-spam))))))
  1934. (defun spam-stat-unregister-spam-routine (articles)
  1935. (spam-stat-register-spam-routine articles t))
  1936. (defun spam-stat-register-ham-routine (articles &optional unregister)
  1937. (dolist (article articles)
  1938. (let ((article-string (spam-get-article-as-string article)))
  1939. (with-temp-buffer
  1940. (insert article-string)
  1941. (if unregister
  1942. (spam-stat-buffer-change-to-spam)
  1943. (spam-stat-buffer-is-non-spam))))))
  1944. (defun spam-stat-unregister-ham-routine (articles)
  1945. (spam-stat-register-ham-routine articles t))
  1946. (defun spam-maybe-spam-stat-load ()
  1947. (when spam-use-stat (spam-stat-load)))
  1948. (defun spam-maybe-spam-stat-save ()
  1949. (when spam-use-stat (spam-stat-save)))
  1950. ;;}}}
  1951. ;;{{{ Blacklists and whitelists.
  1952. (defvar spam-whitelist-cache nil)
  1953. (defvar spam-blacklist-cache nil)
  1954. (defun spam-kill-whole-line ()
  1955. (beginning-of-line)
  1956. (let ((kill-whole-line t))
  1957. (kill-line)))
  1958. ;;; address can be a list, too
  1959. (defun spam-enter-whitelist (address &optional remove)
  1960. "Enter ADDRESS (list or single) into the whitelist.
  1961. With a non-nil REMOVE, remove them."
  1962. (interactive "sAddress: ")
  1963. (spam-enter-list address spam-whitelist remove)
  1964. (setq spam-whitelist-cache nil)
  1965. (spam-clear-cache 'spam-use-whitelist))
  1966. ;;; address can be a list, too
  1967. (defun spam-enter-blacklist (address &optional remove)
  1968. "Enter ADDRESS (list or single) into the blacklist.
  1969. With a non-nil REMOVE, remove them."
  1970. (interactive "sAddress: ")
  1971. (spam-enter-list address spam-blacklist remove)
  1972. (setq spam-blacklist-cache nil)
  1973. (spam-clear-cache 'spam-use-whitelist))
  1974. (defun spam-enter-list (addresses file &optional remove)
  1975. "Enter ADDRESSES into the given FILE.
  1976. Either the whitelist or the blacklist files can be used.
  1977. With a non-nil REMOVE, remove the ADDRESSES."
  1978. (if (stringp addresses)
  1979. (spam-enter-list (list addresses) file remove)
  1980. ;; else, we have a list of addresses here
  1981. (unless (file-exists-p (file-name-directory file))
  1982. (make-directory (file-name-directory file) t))
  1983. (with-current-buffer
  1984. (find-file-noselect file)
  1985. (dolist (a addresses)
  1986. (when (stringp a)
  1987. (goto-char (point-min))
  1988. (if (re-search-forward (regexp-quote a) nil t)
  1989. ;; found the address
  1990. (when remove
  1991. (spam-kill-whole-line))
  1992. ;; else, the address was not found
  1993. (unless remove
  1994. (goto-char (point-max))
  1995. (unless (bobp)
  1996. (insert "\n"))
  1997. (insert a "\n")))))
  1998. (save-buffer))))
  1999. (defun spam-filelist-build-cache (type)
  2000. (let ((cache (if (eq type 'spam-use-blacklist)
  2001. spam-blacklist-cache
  2002. spam-whitelist-cache))
  2003. parsed-cache)
  2004. (unless (gethash type spam-caches)
  2005. (while cache
  2006. (let ((address (pop cache)))
  2007. (unless (zerop (length address)) ; 0 for a nil address too
  2008. (setq address (regexp-quote address))
  2009. ;; fix regexp-quote's treatment of user-intended regexes
  2010. (while (string-match "\\\\\\*" address)
  2011. (setq address (replace-match ".*" t t address))))
  2012. (push address parsed-cache)))
  2013. (puthash type parsed-cache spam-caches))))
  2014. (defun spam-filelist-check-cache (type from)
  2015. (when (stringp from)
  2016. (spam-filelist-build-cache type)
  2017. (let (found)
  2018. (dolist (address (gethash type spam-caches))
  2019. (when (and address (string-match address from))
  2020. (setq found t)
  2021. (return)))
  2022. found)))
  2023. ;;; returns t if the sender is in the whitelist, nil or
  2024. ;;; spam-split-group otherwise
  2025. (defun spam-check-whitelist ()
  2026. ;; FIXME! Should it detect when file timestamps change?
  2027. (unless spam-whitelist-cache
  2028. (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
  2029. (if (spam-from-listed-p 'spam-use-whitelist)
  2030. t
  2031. (if spam-use-whitelist-exclusive
  2032. spam-split-group
  2033. nil)))
  2034. (defun spam-check-blacklist ()
  2035. ;; FIXME! Should it detect when file timestamps change?
  2036. (unless spam-blacklist-cache
  2037. (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
  2038. (and (spam-from-listed-p 'spam-use-blacklist)
  2039. spam-split-group))
  2040. (defun spam-parse-list (file)
  2041. (when (file-readable-p file)
  2042. (let (contents address)
  2043. (with-temp-buffer
  2044. (insert-file-contents file)
  2045. (while (not (eobp))
  2046. (setq address (buffer-substring (point) (point-at-eol)))
  2047. (forward-line 1)
  2048. ;; insert the e-mail address if detected, otherwise the raw data
  2049. (unless (zerop (length address))
  2050. (let ((pure-address
  2051. (nth 1 (gnus-extract-address-components address))))
  2052. (push (or pure-address address) contents)))))
  2053. (nreverse contents))))
  2054. (defun spam-from-listed-p (type)
  2055. (let ((from (message-fetch-field "from"))
  2056. found)
  2057. (spam-filelist-check-cache type from)))
  2058. (defun spam-filelist-register-routine (articles blacklist &optional unregister)
  2059. (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
  2060. (declassification (if blacklist 'ham 'spam))
  2061. (enter-function
  2062. (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
  2063. (remove-function
  2064. (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
  2065. from addresses unregister-list article-unregister-list)
  2066. (dolist (article articles)
  2067. (let ((from (spam-fetch-field-from-fast article))
  2068. (id (spam-fetch-field-message-id-fast article))
  2069. sender-ignored)
  2070. (when (stringp from)
  2071. (dolist (ignore-regex spam-blacklist-ignored-regexes)
  2072. (when (and (not sender-ignored)
  2073. (stringp ignore-regex)
  2074. (string-match ignore-regex from))
  2075. (setq sender-ignored t)))
  2076. ;; remember the messages we need to unregister, unless remove is set
  2077. (when (and
  2078. (null unregister)
  2079. (spam-log-unregistration-needed-p
  2080. id 'process declassification de-symbol))
  2081. (push article article-unregister-list)
  2082. (push from unregister-list))
  2083. (unless sender-ignored
  2084. (push from addresses)))))
  2085. (if unregister
  2086. (funcall enter-function addresses t) ; unregister all these addresses
  2087. ;; else, register normally and unregister what we need to
  2088. (funcall remove-function unregister-list t)
  2089. (dolist (article article-unregister-list)
  2090. (spam-log-undo-registration
  2091. (spam-fetch-field-message-id-fast article)
  2092. 'process
  2093. declassification
  2094. de-symbol))
  2095. (funcall enter-function addresses nil))))
  2096. (defun spam-blacklist-unregister-routine (articles)
  2097. (spam-blacklist-register-routine articles t))
  2098. (defun spam-blacklist-register-routine (articles &optional unregister)
  2099. (spam-filelist-register-routine articles t unregister))
  2100. (defun spam-whitelist-unregister-routine (articles)
  2101. (spam-whitelist-register-routine articles t))
  2102. (defun spam-whitelist-register-routine (articles &optional unregister)
  2103. (spam-filelist-register-routine articles nil unregister))
  2104. ;;}}}
  2105. ;;{{{ Spam-report glue (gmane and resend reporting)
  2106. (defun spam-report-gmane-register-routine (articles)
  2107. (when articles
  2108. (apply 'spam-report-gmane-spam articles)))
  2109. (defun spam-report-gmane-unregister-routine (articles)
  2110. (when articles
  2111. (apply 'spam-report-gmane-ham articles)))
  2112. (defun spam-report-resend-register-ham-routine (articles)
  2113. (spam-report-resend-register-routine articles t))
  2114. (defun spam-report-resend-register-routine (articles &optional ham)
  2115. (let* ((resend-to-gp
  2116. (if ham
  2117. (gnus-parameter-ham-resend-to gnus-newsgroup-name)
  2118. (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
  2119. (spam-report-resend-to (or (car-safe resend-to-gp)
  2120. spam-report-resend-to)))
  2121. (spam-report-resend articles ham)))
  2122. ;;}}}
  2123. ;;{{{ Bogofilter
  2124. (defun spam-check-bogofilter-headers (&optional score)
  2125. (let ((header (message-fetch-field spam-bogofilter-header)))
  2126. (when header ; return nil when no header
  2127. (if score ; scoring mode
  2128. (if (string-match "spamicity=\\([0-9.]+\\)" header)
  2129. (match-string 1 header)
  2130. "0")
  2131. ;; spam detection mode
  2132. (when (string-match spam-bogofilter-bogosity-positive-spam-header
  2133. header)
  2134. spam-split-group)))))
  2135. ;; return something sensible if the score can't be determined
  2136. (defun spam-bogofilter-score (&optional recheck)
  2137. "Get the Bogofilter spamicity score."
  2138. (interactive "P")
  2139. (save-window-excursion
  2140. (gnus-summary-show-article t)
  2141. (set-buffer gnus-article-buffer)
  2142. (let ((score (or (unless recheck
  2143. (spam-check-bogofilter-headers t))
  2144. (spam-check-bogofilter t))))
  2145. (gnus-summary-show-article)
  2146. (message "Spamicity score %s" score)
  2147. (or score "0"))))
  2148. (defun spam-verify-bogofilter ()
  2149. "Verify the Bogofilter version is sufficient."
  2150. (when (eq spam-bogofilter-valid 'unknown)
  2151. (setq spam-bogofilter-valid
  2152. (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
  2153. (shell-command-to-string
  2154. (format "%s -V" spam-bogofilter-program))))))
  2155. spam-bogofilter-valid)
  2156. (defun spam-check-bogofilter (&optional score)
  2157. "Check the Bogofilter backend for the classification of this message."
  2158. (if (spam-verify-bogofilter)
  2159. (let ((article-buffer-name (buffer-name))
  2160. (db spam-bogofilter-database-directory)
  2161. return)
  2162. (with-temp-buffer
  2163. (let ((temp-buffer-name (buffer-name)))
  2164. (with-current-buffer article-buffer-name
  2165. (apply 'call-process-region
  2166. (point-min) (point-max)
  2167. spam-bogofilter-program
  2168. nil temp-buffer-name nil
  2169. (if db `("-d" ,db "-v") `("-v"))))
  2170. (setq return (spam-check-bogofilter-headers score))))
  2171. return)
  2172. (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
  2173. (defun spam-bogofilter-register-with-bogofilter (articles
  2174. spam
  2175. &optional unregister)
  2176. "Register an article, given as a string, as spam or non-spam."
  2177. (if (spam-verify-bogofilter)
  2178. (dolist (article articles)
  2179. (let ((article-string (spam-get-article-as-string article))
  2180. (db spam-bogofilter-database-directory)
  2181. (switch (if unregister
  2182. (if spam
  2183. spam-bogofilter-spam-strong-switch
  2184. spam-bogofilter-ham-strong-switch)
  2185. (if spam
  2186. spam-bogofilter-spam-switch
  2187. spam-bogofilter-ham-switch))))
  2188. (when (stringp article-string)
  2189. (with-temp-buffer
  2190. (insert article-string)
  2191. (apply 'call-process-region
  2192. (point-min) (point-max)
  2193. spam-bogofilter-program
  2194. nil nil nil switch
  2195. (if db `("-d" ,db "-v") `("-v")))))))
  2196. (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
  2197. (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
  2198. (spam-bogofilter-register-with-bogofilter articles t unregister))
  2199. (defun spam-bogofilter-unregister-spam-routine (articles)
  2200. (spam-bogofilter-register-spam-routine articles t))
  2201. (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
  2202. (spam-bogofilter-register-with-bogofilter articles nil unregister))
  2203. (defun spam-bogofilter-unregister-ham-routine (articles)
  2204. (spam-bogofilter-register-ham-routine articles t))
  2205. ;;}}}
  2206. ;;{{{ spamoracle
  2207. (defun spam-check-spamoracle ()
  2208. "Run spamoracle on an article to determine whether it's spam."
  2209. (let ((article-buffer-name (buffer-name)))
  2210. (with-temp-buffer
  2211. (let ((temp-buffer-name (buffer-name)))
  2212. (with-current-buffer article-buffer-name
  2213. (let ((status
  2214. (apply 'call-process-region
  2215. (point-min) (point-max)
  2216. spam-spamoracle-binary
  2217. nil temp-buffer-name nil
  2218. (if spam-spamoracle-database
  2219. `("-f" ,spam-spamoracle-database "mark")
  2220. '("mark")))))
  2221. (if (eq 0 status)
  2222. (progn
  2223. (set-buffer temp-buffer-name)
  2224. (goto-char (point-min))
  2225. (when (re-search-forward "^X-Spam: yes;" nil t)
  2226. spam-split-group))
  2227. (error "Error running spamoracle: %s" status))))))))
  2228. (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
  2229. "Run spamoracle in training mode."
  2230. (with-temp-buffer
  2231. (let ((temp-buffer-name (buffer-name)))
  2232. (save-excursion
  2233. (goto-char (point-min))
  2234. (dolist (article articles)
  2235. (insert (spam-get-article-as-string article)))
  2236. (let* ((arg (if (spam-xor unregister article-is-spam-p)
  2237. "-spam"
  2238. "-good"))
  2239. (status
  2240. (apply 'call-process-region
  2241. (point-min) (point-max)
  2242. spam-spamoracle-binary
  2243. nil temp-buffer-name nil
  2244. (if spam-spamoracle-database
  2245. `("-f" ,spam-spamoracle-database
  2246. "add" ,arg)
  2247. `("add" ,arg)))))
  2248. (unless (eq 0 status)
  2249. (error "Error running spamoracle: %s" status)))))))
  2250. (defun spam-spamoracle-learn-ham (articles &optional unregister)
  2251. (spam-spamoracle-learn articles nil unregister))
  2252. (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
  2253. (spam-spamoracle-learn-ham articles t))
  2254. (defun spam-spamoracle-learn-spam (articles &optional unregister)
  2255. (spam-spamoracle-learn articles t unregister))
  2256. (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
  2257. (spam-spamoracle-learn-spam articles t))
  2258. ;;}}}
  2259. ;;{{{ SpamAssassin
  2260. ;;; based mostly on the bogofilter code
  2261. (defun spam-check-spamassassin-headers (&optional score)
  2262. "Check the SpamAssassin headers for the classification of this message."
  2263. (if score ; scoring mode
  2264. (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
  2265. (when header
  2266. (if (string-match spam-spamassassin-score-regexp header)
  2267. (match-string 1 header)
  2268. "0")))
  2269. ;; spam detection mode
  2270. (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
  2271. (when header ; return nil when no header
  2272. (when (string-match spam-spamassassin-positive-spam-flag-header
  2273. header)
  2274. spam-split-group)))))
  2275. (defun spam-check-spamassassin (&optional score)
  2276. "Check the SpamAssassin backend for the classification of this message."
  2277. (let ((article-buffer-name (buffer-name)))
  2278. (with-temp-buffer
  2279. (let ((temp-buffer-name (buffer-name)))
  2280. (with-current-buffer article-buffer-name
  2281. (apply 'call-process-region
  2282. (point-min) (point-max) spam-assassin-program
  2283. nil temp-buffer-name nil spam-spamassassin-arguments))
  2284. ;; check the return now (we're back in the temp buffer)
  2285. (goto-char (point-min))
  2286. (spam-check-spamassassin-headers score)))))
  2287. ;; return something sensible if the score can't be determined
  2288. (defun spam-spamassassin-score (&optional recheck)
  2289. "Get the SpamAssassin score"
  2290. (interactive "P")
  2291. (save-window-excursion
  2292. (gnus-summary-show-article t)
  2293. (set-buffer gnus-article-buffer)
  2294. (let ((score (or (unless recheck
  2295. (spam-check-spamassassin-headers t))
  2296. (spam-check-spamassassin t))))
  2297. (gnus-summary-show-article)
  2298. (message "SpamAssassin score %s" score)
  2299. (or score "0"))))
  2300. (defun spam-spamassassin-register-with-sa-learn (articles spam
  2301. &optional unregister)
  2302. "Register articles with spamassassin's sa-learn as spam or non-spam."
  2303. (if articles
  2304. (let ((action (if unregister spam-sa-learn-unregister-switch
  2305. (if spam spam-sa-learn-spam-switch
  2306. spam-sa-learn-ham-switch)))
  2307. (summary-buffer-name (buffer-name)))
  2308. (with-temp-buffer
  2309. ;; group the articles into mbox format
  2310. (dolist (article articles)
  2311. (let (article-string)
  2312. (with-current-buffer summary-buffer-name
  2313. (setq article-string (spam-get-article-as-string article)))
  2314. (when (stringp article-string)
  2315. ;; mbox separator
  2316. (insert (concat "From nobody " (current-time-string) "\n"))
  2317. (insert article-string)
  2318. (insert "\n"))))
  2319. ;; call sa-learn on all messages at the same time
  2320. (apply 'call-process-region
  2321. (point-min) (point-max)
  2322. spam-sa-learn-program
  2323. nil nil nil "--mbox"
  2324. (if spam-sa-learn-rebuild
  2325. (list action)
  2326. `("--no-rebuild" ,action)))))))
  2327. (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
  2328. (spam-spamassassin-register-with-sa-learn articles t unregister))
  2329. (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
  2330. (spam-spamassassin-register-with-sa-learn articles nil unregister))
  2331. (defun spam-spamassassin-unregister-spam-routine (articles)
  2332. (spam-spamassassin-register-with-sa-learn articles t t))
  2333. (defun spam-spamassassin-unregister-ham-routine (articles)
  2334. (spam-spamassassin-register-with-sa-learn articles nil t))
  2335. ;;}}}
  2336. ;;{{{ Bsfilter
  2337. ;;; based mostly on the bogofilter code
  2338. (defun spam-check-bsfilter-headers (&optional score)
  2339. (if score
  2340. (or (nnmail-fetch-field spam-bsfilter-probability-header)
  2341. "0")
  2342. (let ((header (nnmail-fetch-field spam-bsfilter-header)))
  2343. (when header ; return nil when no header
  2344. (when (string-match "YES" header)
  2345. spam-split-group)))))
  2346. ;; return something sensible if the score can't be determined
  2347. (defun spam-bsfilter-score (&optional recheck)
  2348. "Get the Bsfilter spamicity score."
  2349. (interactive "P")
  2350. (save-window-excursion
  2351. (gnus-summary-show-article t)
  2352. (set-buffer gnus-article-buffer)
  2353. (let ((score (or (unless recheck
  2354. (spam-check-bsfilter-headers t))
  2355. (spam-check-bsfilter t))))
  2356. (gnus-summary-show-article)
  2357. (message "Spamicity score %s" score)
  2358. (or score "0"))))
  2359. (defun spam-check-bsfilter (&optional score)
  2360. "Check the Bsfilter backend for the classification of this message."
  2361. (let ((article-buffer-name (buffer-name))
  2362. (dir spam-bsfilter-database-directory)
  2363. return)
  2364. (with-temp-buffer
  2365. (let ((temp-buffer-name (buffer-name)))
  2366. (with-current-buffer article-buffer-name
  2367. (apply 'call-process-region
  2368. (point-min) (point-max)
  2369. spam-bsfilter-program
  2370. nil temp-buffer-name nil
  2371. "--pipe"
  2372. "--insert-flag"
  2373. "--insert-probability"
  2374. (when dir
  2375. (list "--homedir" dir))))
  2376. (setq return (spam-check-bsfilter-headers score))))
  2377. return))
  2378. (defun spam-bsfilter-register-with-bsfilter (articles
  2379. spam
  2380. &optional unregister)
  2381. "Register an article, given as a string, as spam or non-spam."
  2382. (dolist (article articles)
  2383. (let ((article-string (spam-get-article-as-string article))
  2384. (switch (if unregister
  2385. (if spam
  2386. spam-bsfilter-spam-strong-switch
  2387. spam-bsfilter-ham-strong-switch)
  2388. (if spam
  2389. spam-bsfilter-spam-switch
  2390. spam-bsfilter-ham-switch))))
  2391. (when (stringp article-string)
  2392. (with-temp-buffer
  2393. (insert article-string)
  2394. (apply 'call-process-region
  2395. (point-min) (point-max)
  2396. spam-bsfilter-program
  2397. nil nil nil switch
  2398. "--update"
  2399. (when spam-bsfilter-database-directory
  2400. (list "--homedir"
  2401. spam-bsfilter-database-directory))))))))
  2402. (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
  2403. (spam-bsfilter-register-with-bsfilter articles t unregister))
  2404. (defun spam-bsfilter-unregister-spam-routine (articles)
  2405. (spam-bsfilter-register-spam-routine articles t))
  2406. (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
  2407. (spam-bsfilter-register-with-bsfilter articles nil unregister))
  2408. (defun spam-bsfilter-unregister-ham-routine (articles)
  2409. (spam-bsfilter-register-ham-routine articles t))
  2410. ;;}}}
  2411. ;;{{{ CRM114 Mailfilter
  2412. (defun spam-check-crm114-headers (&optional score)
  2413. (let ((header (message-fetch-field spam-crm114-header)))
  2414. (when header ; return nil when no header
  2415. (if score ; scoring mode
  2416. (if (string-match "( pR: \\([0-9.-]+\\)" header)
  2417. (match-string 1 header)
  2418. "0")
  2419. ;; spam detection mode
  2420. (when (string-match spam-crm114-positive-spam-header
  2421. header)
  2422. spam-split-group)))))
  2423. ;; return something sensible if the score can't be determined
  2424. (defun spam-crm114-score ()
  2425. "Get the CRM114 Mailfilter pR."
  2426. (interactive)
  2427. (save-window-excursion
  2428. (gnus-summary-show-article t)
  2429. (set-buffer gnus-article-buffer)
  2430. (let ((score (or (spam-check-crm114-headers t)
  2431. (spam-check-crm114 t))))
  2432. (gnus-summary-show-article)
  2433. (message "pR: %s" score)
  2434. (or score "0"))))
  2435. (defun spam-check-crm114 (&optional score)
  2436. "Check the CRM114 Mailfilter backend for the classification of this message."
  2437. (let ((article-buffer-name (buffer-name))
  2438. (db spam-crm114-database-directory)
  2439. return)
  2440. (with-temp-buffer
  2441. (let ((temp-buffer-name (buffer-name)))
  2442. (with-current-buffer article-buffer-name
  2443. (apply 'call-process-region
  2444. (point-min) (point-max)
  2445. spam-crm114-program
  2446. nil temp-buffer-name nil
  2447. (when db (list (concat "--fileprefix=" db)))))
  2448. (setq return (spam-check-crm114-headers score))))
  2449. return))
  2450. (defun spam-crm114-register-with-crm114 (articles
  2451. spam
  2452. &optional unregister)
  2453. "Register an article, given as a string, as spam or non-spam."
  2454. (dolist (article articles)
  2455. (let ((article-string (spam-get-article-as-string article))
  2456. (db spam-crm114-database-directory)
  2457. (switch (if unregister
  2458. (if spam
  2459. spam-crm114-spam-strong-switch
  2460. spam-crm114-ham-strong-switch)
  2461. (if spam
  2462. spam-crm114-spam-switch
  2463. spam-crm114-ham-switch))))
  2464. (when (stringp article-string)
  2465. (with-temp-buffer
  2466. (insert article-string)
  2467. (apply 'call-process-region
  2468. (point-min) (point-max)
  2469. spam-crm114-program
  2470. nil nil nil
  2471. (when db (list switch (concat "--fileprefix=" db)))))))))
  2472. (defun spam-crm114-register-spam-routine (articles &optional unregister)
  2473. (spam-crm114-register-with-crm114 articles t unregister))
  2474. (defun spam-crm114-unregister-spam-routine (articles)
  2475. (spam-crm114-register-spam-routine articles t))
  2476. (defun spam-crm114-register-ham-routine (articles &optional unregister)
  2477. (spam-crm114-register-with-crm114 articles nil unregister))
  2478. (defun spam-crm114-unregister-ham-routine (articles)
  2479. (spam-crm114-register-ham-routine articles t))
  2480. ;;}}}
  2481. ;;}}}
  2482. ;;{{{ Hooks
  2483. ;;;###autoload
  2484. (defun spam-initialize (&rest symbols)
  2485. "Install the spam.el hooks and do other initialization.
  2486. When SYMBOLS is given, set those variables to t. This is so you
  2487. can call `spam-initialize' before you set spam-use-* variables on
  2488. explicitly, and matters only if you need the extra headers
  2489. installed through `spam-necessary-extra-headers'."
  2490. (interactive)
  2491. (dolist (var symbols)
  2492. (set var t))
  2493. (dolist (header (spam-necessary-extra-headers))
  2494. (add-to-list 'nnmail-extra-headers header)
  2495. (add-to-list 'gnus-extra-headers header))
  2496. (setq spam-install-hooks t)
  2497. ;; TODO: How do we redo this every time the `spam' face is customized?
  2498. (push '((eq mark gnus-spam-mark) . spam)
  2499. gnus-summary-highlight)
  2500. ;; Add hooks for loading and saving the spam stats
  2501. (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
  2502. (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
  2503. (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
  2504. (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
  2505. (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
  2506. (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
  2507. (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
  2508. (defun spam-unload-hook ()
  2509. "Uninstall the spam.el hooks."
  2510. (interactive)
  2511. (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
  2512. (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
  2513. (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
  2514. (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
  2515. (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
  2516. (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
  2517. (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
  2518. (add-hook 'spam-unload-hook 'spam-unload-hook)
  2519. (when spam-install-hooks
  2520. (spam-initialize))
  2521. ;;}}}
  2522. (provide 'spam)
  2523. ;;; spam.el ends here