Class: Mocksmtpd
Constant Summary collapse
- VERSION =
'0.0.3'
- TEMPLATE_DIR =
Pathname.new(File.dirname(__FILE__)) + "../templates"
Instance Method Summary collapse
- #console ⇒ Object
- #create_logger(file = nil) ⇒ Object
- #create_pid_file ⇒ Object
- #delete_pid_file ⇒ Object
- #init ⇒ Object
- #init_permission ⇒ Object
-
#initialize(argv) ⇒ Mocksmtpd
constructor
A new instance of Mocksmtpd.
- #load_conf ⇒ Object
- #load_templates ⇒ Object
- #opterror(msg) ⇒ Object
- #parse_mail(src, sender, recipients) ⇒ Object
- #recieve_mail(src, sender, recipients) ⇒ Object
- #resolve_conf_path(path) ⇒ Object
- #run ⇒ Object
- #save_index(mail) ⇒ Object
- #save_mail(mail) ⇒ Object
- #smtpd ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #template(name) ⇒ Object
Constructor Details
#initialize(argv) ⇒ Mocksmtpd
Returns a new instance of Mocksmtpd.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mocksmtpd.rb', line 16 def initialize(argv) @opt = OptionParser.new @opt. = "Usage: #$0 [options] [start|stop|init PATH]" @opt.on("-f FILE", "--config=FILE", "Specify mocksmtpd.conf") do |v| @conf_file = v end @opt.on("--version", "Show version string `#{VERSION}'") do puts VERSION exit end @opt.on("--silent", "Suppress all output") do @silent = true end @opt.parse!(argv) if argv.empty? @command = "console" else @command = argv.shift commands = %w(start stop init) unless commands.include? @command opterror "No such command: #{@command}" exit 1 end end if @command == "init" @init_dir = argv.shift || "mocksmtpd" if test(?e, @init_dir) opterror("Init path already exists: #{@init_dir}") exit 1 end end end |
Instance Method Details
#console ⇒ Object
159 160 161 162 163 164 |
# File 'lib/mocksmtpd.rb', line 159 def console load_conf @logger = create_logger @daemon = false smtpd end |
#create_logger(file = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/mocksmtpd.rb', line 140 def create_logger(file = nil) file = file.to_s.strip file = nil if file.empty? lvstr = @conf[:LogLevel].to_s.strip lvstr = "ERROR" if @silent lvstr = "INFO" unless %w{FATAL ERROR WARN INFO DEBUG}.include?(lvstr) level = WEBrick::BasicLog.const_get(lvstr) logger = WEBrick::Log.new(file, level) logger.debug("Logger initialized") return logger end |
#create_pid_file ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mocksmtpd.rb', line 166 def create_pid_file if @pidfile.exist? pid = @pidfile.read @logger.warn("pid file already exists: pid=#{pid}") exit 1 end pid = Process.pid open(@pidfile, "w") do |io| io << pid end @logger.debug("pid file saved: pid=#{pid} file=#{@pidfile}") end |
#delete_pid_file ⇒ Object
179 180 181 182 |
# File 'lib/mocksmtpd.rb', line 179 def delete_pid_file File.delete(@pidfile) @logger.debug("pid file deleted: file=#{@pidfile}") end |
#init ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mocksmtpd.rb', line 107 def init Dir.mkdir(@init_dir) puts "Created: #{@init_dir}/" unless @silent path = Pathname.new(@init_dir) Dir.mkdir(path + "inbox") puts "Created: #{path + 'inbox'}/" unless @silent Dir.mkdir(path + "log") puts "Created: #{path + 'log'}/" unless @silent open(path + "mocksmtpd.conf", "w") do |io| io << template("mocksmtpd.conf").result(binding) end puts "Created: #{path + 'mocksmtpd.conf'}" unless @silent end |
#init_permission ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mocksmtpd.rb', line 184 def File.umask(@conf[:Umask]) unless @conf[:Umask].nil? stat = File::Stat.new(@conf_file) uid = stat.uid gid = stat.gid begin Process.egid = gid Process.euid = uid rescue NotImplementedError => e @logger.debug("Process.euid= not implemented.") rescue Errno::EPERM => e @logger.warn("could not change euid/egid. #{e}") end end |
#load_conf ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mocksmtpd.rb', line 59 def load_conf @conf_file = Pathname.new(@conf_file || "./mocksmtpd.conf") unless @conf_file.exist? && @conf_file.readable? opterror "Can't load config file: #{@conf_file}" exit 1 end @conf_file = @conf_file.realpath @conf = {} YAML.load_file(@conf_file).each do |k,v| @conf[k.intern] = v end @inbox = resolve_conf_path(@conf[:InboxDir]) @logfile = resolve_conf_path(@conf[:LogFile]) @pidfile = resolve_conf_path(@conf[:PidFile]) @templates = load_templates end |
#load_templates ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/mocksmtpd.rb', line 93 def load_templates result = {} result[:mail] = template("html/mail") result[:index] = template("html/index") result[:index_entry] = template("html/index_entry") return result end |
#opterror(msg) ⇒ Object
54 55 56 57 |
# File 'lib/mocksmtpd.rb', line 54 def opterror(msg) puts("Error: #{msg}") puts(@opt.help) end |
#parse_mail(src, sender, recipients) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/mocksmtpd.rb', line 251 def parse_mail(src, sender, recipients) src = NKF.nkf("-wm", src) subject = src.match(/^Subject:\s*(.+)/i).to_a[1].to_s.strip date = src.match(/^Date:\s*(.+)/i).to_a[1].to_s.strip src = ERB::Util.h(src) src = src.gsub(%r{https?://[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+},'<a href="\0">\0</a>') src = src.gsub(/(?:\r\n|\r|\n)/, "<br />\n") if date.empty? date = Time.now else date = Time.parse(date) end mail = { :source => src, :sender => sender, :recipients => recipients, :subject => subject, :date => date, } format = "%Y%m%d%H%M%S" fname = date.strftime(format) + ".html" while @inbox.join(fname).exist? date += 1 fname = date.strftime(format) + ".html" end mail[:file] = fname mail[:path] = @inbox.join(fname) return mail end |
#recieve_mail(src, sender, recipients) ⇒ Object
242 243 244 245 246 247 248 249 |
# File 'lib/mocksmtpd.rb', line 242 def recieve_mail(src, sender, recipients) @logger.info "mail recieved from #{sender}" mail = parse_mail(src, sender, recipients) save_mail(mail) save_index(mail) end |
#resolve_conf_path(path) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/mocksmtpd.rb', line 79 def resolve_conf_path(path) result = nil if path[0] == ?/ result = Pathname.new(path) else result = @conf_file.parent + path end return result.cleanpath end |
#run ⇒ Object
89 90 91 |
# File 'lib/mocksmtpd.rb', line 89 def run send(@command) end |
#save_index(mail) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/mocksmtpd.rb', line 294 def save_index(mail) path = @inbox + "index.html" unless File.exist?(path) open(path, "w") do |io| io << @templates[:index].result(binding) end end htmlsrc = File.read(path) add = @templates[:index_entry].result(binding) htmlsrc.sub!(/<!-- ADD -->/, add) open(path, "w") do |io| io << htmlsrc end @logger.debug("index saved: #{path}") end |
#save_mail(mail) ⇒ Object
287 288 289 290 291 292 |
# File 'lib/mocksmtpd.rb', line 287 def save_mail(mail) open(mail[:path], "w") do |io| io << @templates[:mail].result(binding) end @logger.debug("mail saved: #{mail[:path]}") end |
#smtpd ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/mocksmtpd.rb', line 199 def smtpd start_cb = Proc.new do @logger.info("Inbox: #{@inbox}") @logger.debug("LogFile: #{@logfile}") @logger.debug("PidFile: #{@pidfile}") begin create_pid_file #if @daemon rescue => e @logger.error("Start: #{e}") raise e end end stop_cb = Proc.new do begin delete_pid_file #if @daemon rescue => e @logger.error("Stop: #{e}") raise e end end data_cb = Proc.new do |src, sender, recipients| recieve_mail(src, sender, recipients) end @conf[:ServerType] = @daemon ? WEBrick::Daemon : nil @conf[:Logger] = @logger @conf[:StartCallback] = start_cb @conf[:StopCallback] = stop_cb @conf[:DataHook] = data_cb server = SMTPServer.new(@conf) [:INT, :TERM].each do |signal| Signal.trap(signal) { server.shutdown } end server.start end |
#start ⇒ Object
152 153 154 155 156 157 |
# File 'lib/mocksmtpd.rb', line 152 def start load_conf @logger = create_logger(@logfile) @daemon = true smtpd end |
#stop ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mocksmtpd.rb', line 122 def stop load_conf unless @pidfile.exist? puts "ERROR: pid file does not exist: #{@pidfile}" exit 1 end unless @pidfile.readable? puts "ERROR: Can't read pid file: #{@pidfile}" exit 1 end pid = File.read(@pidfile) print "Stopping #{pid}..." unless @silent #system "kill -TERM #{pid}" system "taskkill /F /PID #{pid} 1>NUL" puts "done" unless @silent end |
#template(name) ⇒ Object
101 102 103 104 105 |
# File 'lib/mocksmtpd.rb', line 101 def template(name) path = TEMPLATE_DIR + "#{name}.erb" src = path.read return ERB.new(src, nil, "%-") end |