Class: Minbox::Client
- Inherits:
-
Object
- Object
- Minbox::Client
- Defined in:
- lib/minbox/client.rb
Constant Summary collapse
- COMMANDS =
Hashie::Rash.new( /^AUTH LOGIN/i => AuthLogin.new, /^AUTH PLAIN/i => AuthPlain.new, /^DATA/i => Data.new, /^EHLO/i => Ehlo.new, /^HELO/i => Helo.new, /^MAIL FROM/i => Noop.new, /^NOOP/i => Noop.new, /^QUIT/i => Quit.new, /^RCPT TO/i => Noop.new, /^RSET/i => Noop.new, /^STARTTLS/i => StartTls.new )
- UNSUPPORTED =
Unsupported.new
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #authenticate(username, password) ⇒ Object
- #close ⇒ Object
- #connected? ⇒ Boolean
- #handle(&block) ⇒ Object
-
#initialize(server, socket, logger) ⇒ Client
constructor
A new instance of Client.
- #read ⇒ Object
- #secure_socket! ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize(server, socket, logger) ⇒ Client
Returns a new instance of Client.
108 109 110 111 112 |
# File 'lib/minbox/client.rb', line 108 def initialize(server, socket, logger) @server = server @logger = logger @socket = socket end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
106 107 108 |
# File 'lib/minbox/client.rb', line 106 def logger @logger end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
106 107 108 |
# File 'lib/minbox/client.rb', line 106 def server @server end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
106 107 108 |
# File 'lib/minbox/client.rb', line 106 def socket @socket end |
Instance Method Details
#authenticate(username, password) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/minbox/client.rb', line 153 def authenticate(username, password) logger.debug("#{username}:#{password}") return write '535 Authenticated failed - protocol error' unless username && password write '235 2.7.0 Authentication successful' end |
#close ⇒ Object
144 145 146 147 |
# File 'lib/minbox/client.rb', line 144 def close socket&.close @socket = nil end |
#connected? ⇒ Boolean
149 150 151 |
# File 'lib/minbox/client.rb', line 149 def connected? @socket end |
#handle(&block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/minbox/client.rb', line 114 def handle(&block) write "220 #{server.host} ESMTP" while connected? && (line = read) command = COMMANDS.fetch(line, UNSUPPORTED) command.run(self, line, &block) end close rescue Errno::ECONNRESET, Errno::EPIPE => error logger.error(error) close end |
#read ⇒ Object
138 139 140 141 142 |
# File 'lib/minbox/client.rb', line 138 def read line = socket.gets logger.debug("C: #{line.inspect}") line end |
#secure_socket! ⇒ Object
126 127 128 129 130 |
# File 'lib/minbox/client.rb', line 126 def secure_socket! socket = OpenSSL::SSL::SSLSocket.new(@socket, server.ssl_context) socket.sync_close = true @socket = socket.accept end |
#write(message) ⇒ Object
132 133 134 135 136 |
# File 'lib/minbox/client.rb', line 132 def write() = "#{}\r\n" logger.debug("S: #{.inspect}") socket.puts end |