class Opensrs::Email::Gateway

Public Class Methods

new(server, port, user, domain, password, version = '3.4', logger = LOGGER) click to toggle source
# File lib/opensrs/email.rb, line 22
def initialize(server, port, user, domain, password, version = '3.4', logger = LOGGER)
  @server = server
  @port = port
  @user = user
  @domain = domain
  @password = password
  @version = version
  @logger = logger
  @loggedin = false

  open_connection

  response = receive_response
  @logger.info("Initial response:\n#{response}")
  
  response = send_version
  @logger.info("Version response:\n#{response}")
  
  response = login
  @logger.info("Login response:\n#{response}")

  if response[:status] == 'OK'
    @loggedin = true
  else
    @loggedin = false
  end
end

Public Instance Methods

build_response(partial="") click to toggle source
# File lib/opensrs/email.rb, line 140
def build_response(partial="")
  line = receive_line
  if line == ".\r\n"
    partial
  else
    build_response(partial + line)
  end
end
call(command, attributes = {}) click to toggle source
# File lib/opensrs/email.rb, line 94
def call(command, attributes = {})
  if not (MAIL_COMMANDS.include?(command) or
          DOMAIN_COMMANDS.include?(command) or
          WORKGROUP_COMMANDS.include?(command))
    raise "Command #{command.to_s} invalid"
  else
    @logger.info("Sending command: #{command.to_s}")
    cmd = make_command_string(command, attributes)
    send_command(cmd)
    receive_response
  end
end
close_connection() click to toggle source
# File lib/opensrs/email.rb, line 79
def close_connection
  @socket.close if @socket and not @socket.closed?
  @connection.close if @connection and not @connection.closed?

  @socket = @connection = nil
end
loggedin?() click to toggle source
# File lib/opensrs/email.rb, line 50
def loggedin?
  @loggedin
end
login() click to toggle source
# File lib/opensrs/email.rb, line 68
def login
  command = "LOGIN USER=\"#{@user}\" DOMAIN=\"#{@domain}\" PASSWORD=\"#{@password}\"\r\n.\r\n"
  send_command(command)
  receive_response
end
make_command_string(command, attributes) click to toggle source
# File lib/opensrs/email.rb, line 86
def make_command_string(command, attributes)
  cmd = command.to_s
  attr = attributes.map {|k, v|
    "#{k.to_s}=\"#{v.to_s}\""
  } .join(' ')
  cmd << " #{attr}\r\n.\r\n"
end
open_connection() click to toggle source
# File lib/opensrs/email.rb, line 54
def open_connection
  @connection = TCPSocket.new(@server, @port)
  @socket = OpenSSL::SSL::SSLSocket.new(@connection) if @connection

  @socket.sync_close = true # synchronise connection close of ssl layer and underlying tcp socket
  @socket.connect
end
parse_response(response) click to toggle source
# File lib/opensrs/email.rb, line 118
def parse_response(response)
  match_data = /^(OK|ER) (\d+).*/.match(response)
  
  status = if match_data and match_data.length > 2 then
             match_data[1]
           else
             nil
           end
  
  status_code = if match_data and match_data.length > 2 then
                  match_data[2].to_i
                else
                  nil
                end
  
  return {
    :status => status,
    :status_code => status_code,
    :response_body => response
  }
end
quit() click to toggle source
# File lib/opensrs/email.rb, line 74
def quit
  command = "QUIT\r\n.\r\n"
  send_command(command)
end
receive_line() click to toggle source
# File lib/opensrs/email.rb, line 149
def receive_line
  @socket.gets
end
receive_response() click to toggle source
# File lib/opensrs/email.rb, line 113
def receive_response
  response = build_response
  parse_response(response)
end
send_command(command) click to toggle source
# File lib/opensrs/email.rb, line 107
def send_command(command)
  if @socket and not @socket.closed?
    @socket.write(command)
  end
end
send_version() click to toggle source
# File lib/opensrs/email.rb, line 62
def send_version
  command = "VER VER=\"#{@version}\"\r\n.\r\n"
  send_command(command)
  receive_response
end