Class: SMTPServer
- Inherits:
-
GenericServer
- Object
- GenericServer
- SMTPServer
- Defined in:
- lib/smtp_server.rb
Overview
Basic SMTP server
Instance Attribute Summary collapse
-
#client_data ⇒ Object
Returns the value of attribute client_data.
Instance Method Summary collapse
-
#append_data(client, full_data) ⇒ Object
Adds full_data to incoming mail message.
-
#auth(client) ⇒ Object
Authenticates client.
-
#data(client) ⇒ Object
Markes client sending data.
-
#greet(client) ⇒ Object
Send a greeting to client.
-
#initialize(port) ⇒ SMTPServer
constructor
Create new server listening on port 25.
-
#mail_from(client, full_data) ⇒ Object
Stores sender address.
-
#process(client, command, full_data) ⇒ Object
Process command.
-
#quit(client) ⇒ Object
Closes connection.
-
#rcpt_to(client, full_data) ⇒ Object
Stores recepient address.
-
#rset(client) ⇒ Object
Resets local client store.
Constructor Details
#initialize(port) ⇒ SMTPServer
Create new server listening on port 25
10 11 12 13 |
# File 'lib/smtp_server.rb', line 10 def initialize(port) self.client_data = Hash.new super(:port => port) end |
Instance Attribute Details
#client_data ⇒ Object
Returns the value of attribute client_data.
7 8 9 |
# File 'lib/smtp_server.rb', line 7 def client_data @client_data end |
Instance Method Details
#append_data(client, full_data) ⇒ Object
Adds full_data to incoming mail message
We’ll store the mail when full_data == “.”
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/smtp_server.rb', line 87 def append_data(client, full_data) if full_data.gsub(/[\r\n]/,"") == "." Store.instance.add( get_client_data(client, :from).to_s, get_client_data(client, :to).to_s, get_client_data(client, :data).to_s ) respond(client, 250) $log.info "Received mail from #{get_client_data(client, :from).to_s} with recipient #{get_client_data(client, :to).to_s}" else self.client_data[client.object_id][:data] << full_data end end |
#auth(client) ⇒ Object
Authenticates client
80 81 82 |
# File 'lib/smtp_server.rb', line 80 def auth(client) respond(client, 235) end |
#data(client) ⇒ Object
Markes client sending data
68 69 70 71 72 |
# File 'lib/smtp_server.rb', line 68 def data(client) set_client_data(client, :sending_data, true) set_client_data(client, :data, "") respond(client, 354) end |
#greet(client) ⇒ Object
Send a greeting to client
16 17 18 |
# File 'lib/smtp_server.rb', line 16 def greet(client) respond(client, 220) end |
#mail_from(client, full_data) ⇒ Object
Stores sender address
48 49 50 51 52 53 54 55 |
# File 'lib/smtp_server.rb', line 48 def mail_from(client, full_data) if /^MAIL FROM:/ =~ full_data.upcase set_client_data(client, :from, full_data.gsub(/^MAIL FROM:\s*/i,"").gsub(/[\r\n]/,"")) respond(client, 250) else respond(client, 500) end end |
#process(client, command, full_data) ⇒ Object
Process command
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/smtp_server.rb', line 21 def process(client, command, full_data) case command when 'DATA' then data(client) when 'HELO', 'EHLO' then respond(client, 250) when 'NOOP' then respond(client, 250) when 'MAIL' then mail_from(client, full_data) when 'QUIT' then quit(client) when 'RCPT' then rcpt_to(client, full_data) when 'RSET' then rset(client) when 'AUTH' then auth(client) else begin if get_client_data(client, :sending_data) append_data(client, full_data) else respond(client, 500) end end end end |
#quit(client) ⇒ Object
Closes connection
42 43 44 45 |
# File 'lib/smtp_server.rb', line 42 def quit(client) respond(client, 221) client.close end |
#rcpt_to(client, full_data) ⇒ Object
Stores recepient address
58 59 60 61 62 63 64 65 |
# File 'lib/smtp_server.rb', line 58 def rcpt_to(client, full_data) if /^RCPT TO:/ =~ full_data.upcase set_client_data(client, :to, full_data.gsub(/^RCPT TO:\s*/i,"").gsub(/[\r\n]/,"")) respond(client, 250) else respond(client, 500) end end |
#rset(client) ⇒ Object
Resets local client store
75 76 77 |
# File 'lib/smtp_server.rb', line 75 def rset(client) self.client_data[client.object_id] = Hash.new end |