Advertisement
Guest User

Untitled

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