#! /usr/local/bin/ruby require 'smtpfilter' require 'open3' require 'tempfile' class MailScanner attr_accessor :filter def initialize(filter) @filter = filter end def validate_recipient(recip) # validation #if(@filter.from =~ /\.com$/) then # 'Go away !' #else '' #end end def contenttag() tf = Tempfile.new('rscan','/tmp') ah = [] Open3.popen3("/usr/local/bin/spamc") do |i, o, e| @filter.body.each do |l| i.puts l end i.close h = true c = false o.each do |l| l.chomp! tf.puts l h = false if(h && l == '') if(h && ((l =~ /^X-Spam/) || (l =~ /^\s/ && c))) then ah << l c = true else c = false end end o.close e.close end tf.close open("| /usr/local/bin/f-prot '#{tf.path}'") do |p| fpv = '' fpf = [] v = false inf = {} p.each do |l| if(l =~ /Results of virus scanning/i) then break elsif(l =~ /^Program version: (.*)/i) then fpv = "F-prot v#{$1}" elsif(l =~ /Engine version: (.*)/i) then fpv << " (engine #{$1})" elsif(l =~ /(\S+) created (.*)/i) then fpf.push("#{$1} (#{$2})") elsif(l !~ /not scanned/i) then if(l =~ /backdoor"/i) then v = true inf["Backdoor"] = 1 elsif(l =~ /destructive/i || l =~ /unknown/i) then v = true inf["Malware (inconnu)"] = 1 elsif(l =~ /(?:Infect(?:ed|ion)|named|contains):? (\S+)/i) then v = true k = $1 if(k =~ /non-working/) then k.gsub(/ *\(?non-working\)?/, '.dam') end inf[k] = 1; elsif(l =~ /corrupted/i || l =~ /non-working/i) then v = true inf["Malware (endommagé)"] = 1 end end end ah << "X-Virus-Scanner: #{fpv}" unless(fpv.empty?) ah << "X-Virus-Definitions: #{fpf.join(', ')}" unless(fpf.empty?); if(v) then ah << "X-Virus-Flag: YES" ah << "X-Virus-Infection: #{inf.keys.join(', ')}" else ah << "X-Virus-Flag: NO" end end tf.close! @filter.body.insert(0, *ah) end end