Guest User

Untitled

a guest
Feb 28th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. #
  4. # Train dspam by checking if users moved any messages
  5. # between the inbox and the junkbox
  6. #
  7.  
  8. JUNK_BOX = '.Junk/'
  9. DSPAM = 'dspam'
  10.  
  11. require 'rubygems'
  12. require_gem 'activerecord'
  13.  
  14. `logger -p mail.info Training dspam`
  15.  
  16. class User < ActiveRecord::Base
  17. establish_connection :adapter => 'mysql',
  18. :database => 'cura_prod',
  19. :host => 'localhost',
  20. :username => 'cura',
  21. :password => 'yakubrub'
  22. end
  23.  
  24. def log(msg)
  25. `logger -t train_dspam -p mail.info #{msg}`
  26. end
  27.  
  28. User.find(:all).each do |user|
  29. # Look at the user's inbox for mail
  30. # incorrectly labeled as spam
  31. log "Processing #{user.email} - inbox"
  32. Dir.glob(user.maildir + '{cur,new}/*').each do |file|
  33. `grep 'X-DSPAM-Result: Spam' #{file} 1>/dev/null`
  34. if $?.to_i == 0
  35. # If such a message is found, tell dspam about it and
  36. # change the header so we won't process it again
  37. log "Correcting a false positive"
  38. `#{DSPAM} --user #{user.email} --class=innocent --source=error < #{file}`
  39. `sed 's/X-DSPAM-Result: Spam/X-DSPAM-Result: Innocent/g' #{file} > #{file}_dspam_tmp && mv #{file}_dspam_tmp #{file}`
  40. end
  41. end
  42.  
  43. # Now look at the junk folder
  44. # for mail which failed to be identified as spam
  45. log "Processing #{user.email} - junkbox"
  46. Dir.glob(user.maildir + "#{JUNK_BOX}/{cur,new}/*").each do |file|
  47. `grep "X-DSPAM-Result: Innocent" #{file} 1>/dev/null`
  48. if $?.to_i == 0
  49. # If such a message is found, tell dspam about it and
  50. # since it is spam we can just delete it
  51. log "Correcting a false negative"
  52. `#{DSPAM} --user #{user.email} --class=spam --source=error < #{file}`
  53. `rm -f #{file}`
  54. end
  55. end
  56. end
Add Comment
Please, Sign In to add comment