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



67
68
69
70
# File 'lib/bscan/utils/mailer.rb', line 67

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

#send_emailObject



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
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bscan/utils/mailer.rb', line 81

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



72
73
74
75
# File 'lib/bscan/utils/mailer.rb', line 72

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

#verObject



76
77
78
79
# File 'lib/bscan/utils/mailer.rb', line 76

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

#zip_encode(f) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bscan/utils/mailer.rb', line 148

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