Class: Percolate::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/percolate/listener.rb

Constant Summary collapse

CRLF =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Listener

The constructor for an smtp listener. This has a number of options you can give it, a lot of which already have defaults.

:verbose_debug

Just turns on lots of debugging output.

:responder

The responder class you want to use as a responder. This defaults to, as you might expect, SMTP::Responder, but really I want you to subclass that and write your own mail handling code. Its normal default behaviour is to act as a sort of null MTA, accepting and cheerfully discarding messages.

:ipaddress

The IP address you want this to listen on. Defaults to 0.0.0.0 (all available interfaces)

:port The port to listen on. I have it default

to 10025 rather than, as you might expect,
25, because 25 is a privileged port and so
you have to be root to listen on it.
Unless you're foolish enough to try
building a real MTA on this (just leave
that kind of foolishness to me), just stick
to leaving this at a high port and letting
Postfix or Sendmail or your real MTA of
choice filter through it.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/percolate/listener.rb', line 33

def initialize(opts = {})
    @ipaddress = "0.0.0.0"
    @hostname = "localhost"
    @port = 10025
    @responder = Responder

    @verbose_debug = opts[:debug]
    @ipaddress     = opts[:ipaddr] if opts[:ipaddr]
    @port          = opts[:port] if opts[:port]
    @hostname      = opts[:hostname] if opts[:hostname]
    @responder     = opts[:responder] if opts[:responder]

    @socket = TCPServer.new @ipaddress, @port
end

Instance Attribute Details

#hostnameObject

My current hostname as return by the HELO and EHLO commands



49
50
51
# File 'lib/percolate/listener.rb', line 49

def hostname
  @hostname
end

Instance Method Details

#goObject

Once the listener is running, let it start handling mail by invoking the poorly-named “go” method.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/percolate/listener.rb', line 53

def go
    trap 'CLD' do 
        debug "Got SIGCHLD"
        reap_children 
    end
    trap 'INT' do 
        debug "Got SIGINT"
        cleanup_and_exit 
    end
    
    @pids = []
    while mailsocket=@socket.accept
        debug "Got connection from #{mailsocket.peeraddr[3]}"
        pid = handle_connection mailsocket
        mailsocket.close
        @pids << pid
    end
end