#!/usr/bin/ruby # call it from inetd from = nil to = [] data = nil def response line $stdout.puts line $stdout.flush end site = `hostname`.chomp response "220 #{site} FakeSMTPd" for line in $stdin if data.nil? case line when /^(HELO|EHLO)/i then response "250 ok\r" when /^MAIL FROM:\s*<([^>]+)>/i then from = $1 response "250 ok\r" when /^MAIL FROM:/i then from = $'.strip response "250 ok\r" when /^RCPT TO:\s*<([^>]+)>\s*$/i then to.push $1 response "250 ok\r" when /^RCPT TO:/i then to.push $'.strip response "250 ok\r" when /^DATA/i then response "354 go ahead\r" data = [] when /^NOOP/i then response "250 ok\r" when /^RSET/i then from = nil to = [] response "250 ok\r" when /^QUIT/i then response "221 #{site}\r" exit 0 else response "500 syntax error\r" end else case line when /^\.\r?$/ then cmd = "/usr/sbin/sendmail #{to.join(' ')}" begin IO.popen(cmd, 'w') {|fp| data.each { |line| fp.write line } } response "250 ok\r" rescue Exception => e response "500 #{e.class}\r" end data = nil else data.push line end end end