Module: Mailer

Included in:
BScan
Defined in:
lib/bscan/utils/mailer.rb

Constant Summary collapse

MARKER =
"ABCDEFFEDCBA"
HEADERS =
%q{From: BScan-<ver> <from>
To:   <to>
Subject: <subject>
Content-Type: multipart/mixed; boundary="<MARKER>"
MIME-Version: 1.0

--<MARKER>
}
SIMPLE =
%q{From: BScan-<ver> <from>
To:   <to>
Subject: <subject>
Content-Type: text/plain; charset="UTF-8";

<BODY>}
BODY =
%q{
  Start Time: <start_time> 
  End   Time: <end_time> 
  High #:     <high>
  Med  #:     <med>
  Low  #:     <low>
  Issues:     <issues>
  URL's :
<urls> 
}
MESSAGE =
%q{Content-Type: text/plain
Content-Transfer-Encoding:8bit

<BODY>
--<MARKER>
}
ATTACHMENT =
%q{Content-Type: application/zip; name="<filename>"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename="<filename>"

<encoded>
--<MARKER>--
}

Instance Method Summary collapse

Instance Method Details

#pv(par, defv = nil) ⇒ Object



56
57
58
59
# File 'lib/bscan/utils/mailer.rb', line 56

def pv par,defv=nil
  val = bscan_config[@m_pref + par] 
  val ? val : defv
end

#send_emailObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bscan/utils/mailer.rb', line 70

def send_email 
  begin
    
    @m_pref = 'bscan.smtp.'
    srv = pv 'server',nil
    ssl = pv 'ssl',false
    defport = ssl ? 465 : 25 
    port = pv 'port',defport
    from = pv 'from',nil
    dfrom = $1 if from =~ /@(.+)/
    domain = pv 'domain',dfrom
    user = pv 'user',nil
    pwd = pv 'pwd',nil
    attach = pv 'include_report','false'
    attach = attach=='true' ? true:false
    to = pv 'to', nil
    file = @stat['issues']
    file ||= ''
    raise "Can't send '#{file}' that doesn't exist" if attach && !File.file?(file) 
    file = File.absolute_path(file)
    
    @stat['subject'] = sv 'subject', "BScan @  #{Socket.gethostname}"

    Log 2,"Mailer.send_mail #{@stat} #{pv 'server'} #{pv 'from'} #{pv 'to'} #{pv 'ssl'} #{pv 'port'} #{pv 'domwin'} #{pv 'user'} "

    raise "Params 'server', 'from', 'to', 'domain' are mandatory" if !srv or !from or !to or !domain
    
    if (attach)
      msg = HEADERS + MESSAGE + ATTACHMENT
      msg.gsub!( /<MARKER>/ , MARKER)  
      msg.gsub!( /<filename>/ , File.basename(sv('issues'),'.*') + '.zip')  
      msg.sub!(/<encoded>/,zip_encode(file))
    else
      msg = SIMPLE
    end
    
    msg.sub!( /<BODY>/ , BODY)  

    @stat.each_key do |k|
      Log 2,"Mailer.send_mail replacing <#{k}>"
      msg.gsub!( /<#{k}>/ , k=='issues'?file:sv(k,'').to_s)  
    end
    
    msg.sub!( /<from>/, "<#{from}>")  
    msg.sub!( /<to>/  , "<#{to}>")  
    msg.sub!( /<ver>/ , ver)  

    msg.gsub!(/\r?\n/,"\r\n")
    
    to = to.split(',')
    
    smtp = Net::SMTP.new srv,port
    smtp.enable_ssl if ssl
    if (user)
      smtp.start(domain,user,pwd)
    else
      smtp.start(domain)
    end
    Log 2,"Mailer.send_mail send_message #{from} #{to}\n#{msg}"
    smtp.send_message msg, from, to
    smtp.finish
  rescue  Exception => e
    Log 0, "Mailer.send Exception: #{e.message}"
    Log 0, e.backtrace.join("\n")
 end
end

#sv(par, defv = nil) ⇒ Object



61
62
63
64
# File 'lib/bscan/utils/mailer.rb', line 61

def sv par,defv=nil
  val = @stat[ par] 
  val ? val : defv
end

#verObject



65
66
67
68
# File 'lib/bscan/utils/mailer.rb', line 65

def ver
  file=::File.expand_path(File.join(::File.dirname(__FILE__), "../../../VERSION"))
  ::File.file?(file) ? File.read(file).chomp : ''
end

#zip_encode(f) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bscan/utils/mailer.rb', line 137

def zip_encode f
  require 'zip/zip'
  fb  = File.basename(f, '.*')
  fbe = File.basename(f)
  zipf = fb + '.zip'
  zipp = File.join(File.dirname(f), zipf)
  zip = Zip::ZipFile.open(zipp, Zip::ZipFile::CREATE)
  zip.find_entry(fbe)?zip.replace(fbe,f):zip.add(fbe,f)
  zip.close
  content = File.read(zipp)
  [content].pack("m")
end