SMTPの仕様に従っていない、SMTPもどきサーバー。 by Ruby
-----
require 'socket'
require 'kconv'
def log(*str)
str.each{|s|
print s.to_s.tosjis
}
end
class String
def startwith(str)
self.slice(0, str.length) === str
end
end
gs = TCPServer.open(25)
addr = gs.addr
addr.shift
printf("server is on %s\n", addr.join(':'))
while true
Thread.start(gs.accept) do |s|
log s, " is accepted.\n"
s.write "220 OK\r\n"
line = s.gets
if line.startwith('EHLO')
s.write "502 ERR\r\n"
line = s.gets
end
if !line.startwith('HELO')
s.write "500 ERR\r\n"
s.close
return
end
s.write "250 OK\r\n"
while !(line = s.gets).startwith('DATA')
s.write "250 OK\r\n"
log line
end
s.write "354 ENTER\r\n"
log line
while !((line = s.gets) === ".\r\n")
log line
end
s.write "250 OK\r\n"
if !(line = s.gets).startwith('QUIT')
s.write "500 ERR\r\n"
s.close
return
end
s.write "221 CLOSE\r\n"