Created
August 8, 2010 15:37
-
-
Save asim/514156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'socket' | |
class Smtp < EM::P::SmtpServer | |
require 'ostruct' | |
@@received = 0 | |
def initialize(*args) | |
super | |
@cache = "/tmp/cache" | |
end | |
def post_init | |
port, @ip = Socket.unpack_sockaddr_in(get_peername) | |
puts "[#{Time.now}] - Connection from #{@ip}" | |
send_server_greeting | |
end | |
def get_server_domain | |
"fu.com" | |
end | |
def get_server_greeting | |
"Its the EM fu yo" | |
end | |
def receive_sender(sender) | |
current.sender = sender | |
true | |
end | |
def receive_recipient(recipient) | |
current.recipient = recipient | |
true | |
end | |
def receive_data_chunk(data) | |
current.data << data.join("\n") | |
end | |
def receive_data_command | |
current.data = "" | |
true | |
end | |
def receive_ehlo_domain(domain) | |
@ehlo_domain = domain | |
true | |
end | |
def receive_message | |
# do stuff if you've collected in ivar or sent to file, clear ivar | |
current.received = true | |
current.completed_at = Time.now | |
File.open("#{@cache}/#{current.completed_at.to_i}", 'w') { |f| f.puts current } | |
puts "[#{Time.now}] - Received message from: #{current.sender} to: #{current.recipient} - Saved" | |
@current = OpenStruct.new | |
@@received += 1 | |
puts "received: #{@@received}" | |
true | |
end | |
def receive_transaction | |
if @ehlo_domain | |
current.ehlo_domain = @ehlo_domain | |
@ehlo_domain = nil | |
end | |
true | |
end | |
def current | |
@current ||= OpenStruct.new | |
end | |
def unbind | |
puts "[#{Time.now}] - Disconnect from #{@ip}" | |
end | |
end | |
EM.run { | |
EM.start_server( '0.0.0.0', 2025, Smtp ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment