Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. # Ruby version of dspam-retrain perl script.
  3. # Perl version: http://dspamwiki.expass.de/DspamRetrainScript
  4. # Author: wsr@rushforthnetworks.com
  5. # License: BSD
  6. # Abstract: setup postfix to pipe mail into this script
  7. # so that we may then pass it off to dspam in
  8. # an appropriate way.
  9. #
  10. # dspam-retrain.rb handles spam-user@domain.tld, and
  11. # spam@domain.tld formats
  12. #
  13. # If dspam-retrain.rb does not find a signature,
  14. # it will train dspam using corpus or innoculation
  15. # mode.
  16. #
  17. # Requirements: open4 gem is installed
  18. # -----------------------------------------------------------
  19.  
  20. #### Configuration BEGIN ####################################
  21.  
  22. #enable logging?
  23.  
  24. @enable_logging = true
  25. @log_file = '/tmp/dspam_retrain.log'
  26.  
  27. #what mode to train dspam in if we do not find signature?
  28. #corpus or innoculation
  29.  
  30. @alternative_mode = 'corpus'
  31.  
  32. #### Configuration END #####################################
  33.  
  34.  
  35. require 'rubygems'
  36. require 'open4'
  37.  
  38. if @enable_logging
  39. require 'logger'
  40. @logfile = File.new(@log_file, 'a+')
  41. @log = Logger.new(@logfile)
  42. end
  43.  
  44. def logthis(message)
  45. if @enable_logging
  46. @log.info(message)
  47. end
  48. end
  49.  
  50. # Get arguments
  51. spam_class = ARGV[0]
  52. sender = ARGV[1]
  53. recip = ARGV[2]
  54.  
  55. logthis("dspam-retrain Started. Arguments: #{spam_class}, #{sender}, #{recip}")
  56.  
  57. #see if we were passed spam-user@ or just user@
  58. if match = recip.to_s.match(/^(spam|ham)-(\w+)@/)
  59. user = recip.gsub(/#{match[1]}\-/, '')
  60. elsif match = recip.to_s.match(/^(\w+)@/)
  61. user = sender
  62. else
  63. logthis("\tCant't determine user")
  64. exit 75
  65. end
  66.  
  67. signature = String.new
  68. message = String.new
  69.  
  70. #loop through email (passed via stdinput)
  71. #search for signature
  72.  
  73. $stdin.each do |line|
  74. if line.match(/X-DSPAM-Signature/)
  75. signature = line.gsub(/X-DSPAM-Signature:/, '')
  76. #remove any potential whitespace
  77. signature.strip!
  78. #since we found signature, break loop
  79. break
  80. end
  81. message << line
  82. end
  83.  
  84.  
  85. if signature.length.to_i == 0
  86. #we did not find a signature, do normal training
  87.  
  88. mode = 'train'
  89.  
  90. logthis("\tEmail did not have signature passed in. Attempting #{@alternative_mode} train.")
  91.  
  92. #open up dspam with appropriate options
  93. pid, dspam_in, dspam_out, dspam_err = Open4::popen4 "/usr/bin/dspam --source=#{@alternative_mode} --class=#{spam_class} --user #{user}"
  94.  
  95. #attempt to feed message in
  96. begin
  97. dspam_in << message
  98. rescue
  99. #means dspam closed stdinput because our
  100. #options failed
  101. dspam_err.each_line do |o|
  102. logthis("\t" + o.gsub(/\n/, ''))
  103. end
  104. end
  105. #close dspams stdinput
  106. dspam_in.close_write
  107.  
  108. #see if dspam left any messages for us
  109. dspam_out.each_line do |o|
  110. if o.strip.length == 0 then next end
  111. logthis("\t" + o.gsub(/\n/, ''))
  112. end
  113.  
  114. else
  115. #we found signature, so we will only pass that.
  116.  
  117. mode = 'retrain'
  118.  
  119. logthis("\tRetraining Signature: #{signature} for User: #{user} as: #{spam_class}")
  120.  
  121. #open up dspam with appropriate options
  122. pid, dspam_in, dspam_out, dspam_err = Open4::popen4 "/usr/bin/dspam --source=error --signature=#{signature} --class=#{spam_class} --user #{user}"
  123.  
  124. #see if dspam left any messages for us
  125. dspam_out.each_line do |o|
  126. if o.strip.length == 0 then next end
  127. logthis("\t" + o.gsub(/\n/,''))
  128. end
  129. dspam_err.each_line do |o|
  130. if o.strip.length == 0 then next end
  131. logthis("\t" + o.gsub(/\n/,''))
  132. end
  133. end
  134.  
  135. ignored, status = Process::waitpid2 pid
  136.  
  137. #see if we exited cleanly and log it
  138. if status.exitstatus == 0
  139. logthis("\tMessage successfully #{mode}ed as #{spam_class}")
  140. else
  141. logthis("\tMessage NOT #{mode}ed")
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement