Class: MailDiode::Server

Inherits:
GServer
  • Object
show all
Defined in:
lib/server.rb

Constant Summary collapse

DEFAULT_PORT =
10025
DEFAULT_MAX_CONNECTIONS =
20
TIMEOUT_SECONDS =

djb recommends 1200

1200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine_factory, settings) ⇒ Server

Returns a new instance of Server.



26
27
28
29
30
31
# File 'lib/server.rb', line 26

def initialize(engine_factory, settings)
  load_settings(settings)

  @factory = engine_factory
  super(@port, @ip, @max_connections)
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



23
24
25
# File 'lib/server.rb', line 23

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



24
25
26
# File 'lib/server.rb', line 24

def port
  @port
end

Instance Method Details

#discard_early_input(client) ⇒ Object



78
79
80
81
82
# File 'lib/server.rb', line 78

def discard_early_input(client)
  while(client.ready?)
    client.getc
  end
end

#get_line(input, timeout_seconds) ⇒ Object



71
72
73
74
75
76
# File 'lib/server.rb', line 71

def get_line(input, timeout_seconds)
  line = nil
  t = Thread.new { line = input.gets(NEWLINE) }
  t.join(timeout_seconds)
  return line
end

#load_settings(settings) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/server.rb', line 88

def load_settings(settings)
    @hostname = settings.get_setting('server', 'hostname')
    @ip = settings.get_setting('server', 'ip')
    @ip.untaint
    @port = settings.get_int('server', 'port', DEFAULT_PORT)
    @port.untaint
    @max_connections = settings.get_int('server', 'maxconnections', DEFAULT_MAX_CONNECTIONS)
end

#response(text) ⇒ Object



84
85
86
# File 'lib/server.rb', line 84

def response(text)
  return "#{text}#{NEWLINE}"
end

#serve(client) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/server.rb', line 33

def serve(client)
  begin
    MailDiode::log_debug("Accepting client")
    if(connections > 1)
      MailDiode::log_info("Connections: #{connections}")
    end
    client.binmode
    engine = @factory.create_engine(@hostname)
    family, port, hostname, ip = client.addr
    greeting = engine.start(ip)
    MailDiode::log_debug("Greeting: #{greeting}")
    client << response(greeting)
    while !engine.terminate?
      line = get_line(client, TIMEOUT_SECONDS)
      if(!line)
        MailDiode::log_debug "Client disconnected"
        return
      end
      line.chomp!(NEWLINE)
      result = engine.process_line(line)
      if(result)
        discard_early_input(client)
        client << response(result)
      end
    end
  rescue Errno::ECONNRESET
    MailDiode::log_debug "Client dropped connection"
  rescue Errno::ETIMEDOUT
    MailDiode::log_info "Client input timeout"
  rescue Exception => e
    MailDiode::log_error("Exception #{e.class}: #{e}")
    e.backtrace.each do | line |
      MailDiode::log_error(line)
    end
    client << response("451 Internal Server Error")
  end
end