Class: Remailer::IMAP::Client

Inherits:
AbstractConnection show all
Includes:
Support
Defined in:
lib/remailer/imap/client.rb

Defined Under Namespace

Classes: Interpreter

Constant Summary collapse

DEFAUT_TIMEOUT =

Constants ============================================================

60

Constants inherited from AbstractConnection

AbstractConnection::DEFAULT_TIMEOUT, AbstractConnection::NOTIFICATIONS

Constants included from Constants

Constants::CRLF, Constants::IMAPS_PORT, Constants::LINE_REGEXP, Constants::SMTP_PORT, Constants::SOCKS5_PORT

Instance Attribute Summary

Attributes inherited from AbstractConnection

#error, #error_message, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

#quoted

Methods inherited from AbstractConnection

#after_complete, #after_ready, #auth_support?, #cancel_timer!, #check_for_timeouts!, #close_connection, #closed?, #connect_notification, #connected?, #connection_completed, #debug_notification, #error?, #error_notification, establish!, #initialize, #interpreter_entered_state, #message_callback, #post_init, #proxy_connection_initiated!, #proxy_connection_initiated?, #receive_data, report_exception, #requires_authentication?, #reset_timeout!, #resolve_hostname, #send_callback, #send_line, #send_notification, #set_timer!, #start_tls, #state, #time_remaning, #timeout=, #tls_support?, #unbind, #unbound?, #use_tls?, #using_proxy?, warn_about_arguments

Constructor Details

This class inherits a constructor from Remailer::AbstractConnection

Class Method Details

.default_portObject



24
25
26
# File 'lib/remailer/imap/client.rb', line 24

def self.default_port
  IMAPS_PORT
end

.default_timeoutObject

Class Methods ========================================================



20
21
22
# File 'lib/remailer/imap/client.rb', line 20

def self.default_timeout
  DEFAULT_TIMEOUT
end

.open(imap_server, options = nil, &block) ⇒ Object

Opens a connection to a specific IMAP server. Options can be specified:

  • port => Numerical port number (default is 993)

  • require_tls => If true will fail connections to non-TLS capable servers (default is false)

  • username => Username to authenticate with the IMAP server

  • password => Password to authenticate with the IMAP server

  • use_tls => Will use TLS if availble (default is true)

  • debug => Where to send debugging output (IO or Proc)

  • connect => Where to send a connection notification (IO or Proc)

  • error => Where to send errors (IO or Proc)

  • on_connect => Called upon successful connection (Proc)

  • on_error => Called upon connection error (Proc)

  • on_disconnect => Called when connection is closed (Proc)

A block can be supplied in which case it will stand in as the :connect option. The block will recieve a first argument that is the status of the connection, and an optional second that is a diagnostic message.



44
45
46
# File 'lib/remailer/imap/client.rb', line 44

def self.open(imap_server, options = nil, &block)
  super(imap_server, options, &block)
end

Instance Method Details

#after_initializeObject

Instance Methods =====================================================



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/remailer/imap/client.rb', line 50

def after_initialize
  if (using_proxy?)
    @connecting_to_proxy = true
    use_socks5_interpreter!
  else
    if (@options[:use_tls])
      self.start_tls
    end
    
    use_imap_interpreter!
  end
  
  @command_tags = { }
  @issued_command = { }
end

#after_proxy_connectedObject

Callback receiver for when the proxy connection has been completed.



67
68
69
70
71
72
73
# File 'lib/remailer/imap/client.rb', line 67

def after_proxy_connected
  if (@options[:use_tls])
    self.start_tls
  end
  
  use_imap_interpreter!
end

#after_unbindObject



75
76
77
78
79
80
81
# File 'lib/remailer/imap/client.rb', line 75

def after_unbind
  debug_notification(:disconnect, "Disconnected by remote.")
  
  @command_tags.each do |tag, callback|
    callback[1].call(nil)
  end
end

#capabilityObject

– Commands ————————————————————-



92
93
94
95
96
# File 'lib/remailer/imap/client.rb', line 92

def capability
  self.issue_command('CAPABILITY') do |status, message, additional|
    yield(additional)
  end
end

#examine(mailbox_name = 'INBOX') ⇒ Object



116
117
118
119
120
# File 'lib/remailer/imap/client.rb', line 116

def examine(mailbox_name = 'INBOX')
  self.issue_command('EXAMINE', quoted(mailbox_name)) do |status, message, additional|
    yield(status, message, additional)
  end
end

#fetch(range, *options) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/remailer/imap/client.rb', line 139

def fetch(range, *options)
  fetch_options = { }
  
  options.each do |option|
    case (option)
    when Hash
      fetch_options.merge(option)
    else
      fetch_options[option] = true
    end
  end
  
  if (fetch_options.empty?)
    fetch_options[:all] = true
  end
  
  sequence_set =
    case (range)
    when Range
      "#{range.min}:#{range.max}"
    else
      range.to_s
    end
    
  items =
    if (fetch_options[:all])
      'ALL'
    elsif (fetch_options[:fast])
      'FAST'
    elsif (fetch_options[:full])
      'FULL'
    elsif (body_options = fetch_options[:body])
      case (body_options)
      when Hash
        # ...
      else
        'BODY'
      end
    end
  
  self.issue_command('FETCH', sequence_set, items) do |status, message, additional|
    yield(status, message, additional)
  end
end

#idleObject



184
185
186
187
188
189
190
# File 'lib/remailer/imap/client.rb', line 184

def idle
  self.issue_command('IDLE') do |status, message, additional|
    yield(status, message, additional)
  end
  
  lambda { self.send_line('DONE') }
end

#list(reference_name = '/', mailbox_name = '*') ⇒ Object



104
105
106
107
108
# File 'lib/remailer/imap/client.rb', line 104

def list(reference_name = '/', mailbox_name = '*')
  self.issue_command('LIST', quoted(reference_name), quoted(mailbox_name)) do |status, message, additional|
    yield(status, message, additional)
  end
end

#login(username, password) ⇒ Object



98
99
100
101
102
# File 'lib/remailer/imap/client.rb', line 98

def (username, password)
  self.issue_command('LOGIN', quoted(username), quoted(password)) do |status, message, additional|
    yield(status, message, additional)
  end
end

#noopObject



133
134
135
136
137
# File 'lib/remailer/imap/client.rb', line 133

def noop
  self.issue_command('NOOP') do |status, message, additional|
    yield(status, message, additional)
  end
end

#receive_response(tag, status, message, additional = nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/remailer/imap/client.rb', line 83

def receive_response(tag, status, message, additional = nil)
  if (set = @command_tags.delete(tag))
    @issued_command.delete(set[0])
    set[1].call(status, message, additional)
  end
end

#select(mailbox_name = 'INBOX') ⇒ Object



110
111
112
113
114
# File 'lib/remailer/imap/client.rb', line 110

def select(mailbox_name = 'INBOX')
  self.issue_command('SELECT', quoted(mailbox_name)) do |status, message, additional|
    yield(status, message, additional)
  end
end

#status(mailbox_name = 'INBOX', *flags) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/remailer/imap/client.rb', line 122

def status(mailbox_name = 'INBOX', *flags)
  flags.flatten!
  flags = [ :uidnext, :messages ] if (flags.empty?)
  
  # Valid flags are: MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN
  
  self.issue_command('STATUS', quoted(mailbox_name), "(#{flags.collect { |f| f.to_s.upcase }.join(' ')})") do |status, message, additional|
    yield(status, message, additional)
  end
end