Class: Opensrs::Email::Gateway

Inherits:
Object
  • Object
show all
Includes:
Domain, Mailbox, Workgroup
Defined in:
lib/opensrs/email.rb

Constant Summary

Constants included from Workgroup

Workgroup::WORKGROUP_COMMANDS, Workgroup::WORKGROUP_REQUIRED_ARGS

Constants included from Mailbox

Mailbox::MAIL_COMMANDS, Mailbox::MAIL_REQUIRED_ARGS

Constants included from Domain

Domain::DOMAIN_COMMANDS, Domain::DOMAIN_REQUIRED_ARGS

Instance Method Summary collapse

Constructor Details

#initialize(server, port, user, domain, password, version = '3.4', logger = LOGGER) ⇒ Gateway

Returns a new instance of Gateway.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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 = 
  @logger.info("Login response:\n#{response}")

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

Instance Method Details

#build_response(partial = "") ⇒ Object



140
141
142
143
144
145
146
147
# 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 = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# 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_connectionObject



79
80
81
82
83
84
# 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?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/opensrs/email.rb', line 50

def loggedin?
  @loggedin
end

#loginObject



68
69
70
71
72
# File 'lib/opensrs/email.rb', line 68

def 
  command = "LOGIN USER=\"#{@user}\" DOMAIN=\"#{@domain}\" PASSWORD=\"#{@password}\"\r\n.\r\n"
  send_command(command)
  receive_response
end

#make_command_string(command, attributes) ⇒ Object



86
87
88
89
90
91
92
# 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_connectionObject



54
55
56
57
58
59
60
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

#quitObject



74
75
76
77
# File 'lib/opensrs/email.rb', line 74

def quit
  command = "QUIT\r\n.\r\n"
  send_command(command)
end

#receive_lineObject



149
150
151
# File 'lib/opensrs/email.rb', line 149

def receive_line
  @socket.gets
end

#receive_responseObject



113
114
115
116
# File 'lib/opensrs/email.rb', line 113

def receive_response
  response = build_response
  parse_response(response)
end

#send_command(command) ⇒ Object



107
108
109
110
111
# File 'lib/opensrs/email.rb', line 107

def send_command(command)
  if @socket and not @socket.closed?
    @socket.write(command)
  end
end

#send_versionObject



62
63
64
65
66
# File 'lib/opensrs/email.rb', line 62

def send_version
  command = "VER VER=\"#{@version}\"\r\n.\r\n"
  send_command(command)
  receive_response
end