Class: MailDaemon::Imap::Connection

Inherits:
Object
  • Object
show all
Includes:
Helpers, Statuses
Defined in:
lib/mail_daemon/imap/connection.rb

Constant Summary collapse

SECONDS_IN_MINUTES =
60

Constants included from Statuses

Statuses::CONNECTED, Statuses::CONNECTING, Statuses::DISCONNECTED, Statuses::DISCONNECTING, Statuses::IDLEING, Statuses::INITIALIZING, Statuses::LOGGED_OFF, Statuses::LOGGED_ON, Statuses::LOGGING_OFF, Statuses::LOGGING_ON, Statuses::POLLING

Instance Method Summary collapse

Methods included from Helpers

#default_option, #reload_options, #required_option, #setup_options

Constructor Details

#initialize(options, &block) ⇒ Connection

Returns a new instance of Connection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mail_daemon/imap/connection.rb', line 11

def initialize(options, &block)
  setup_options(options)

  required_option [:host, :username, :password]

  default_option :port, 143
  default_option :folder, "inbox"
  default_option :ssl, false
  default_option :start_tls, false
  default_option :ssl_options, false
  default_option :sleep_time, 10000
  default_option :imap_timeout, 60
  default_option :idle_recycle_time, 29 * SECONDS_IN_MINUTES

  @notify_status_block = block

  puts "initializing new imap connection for #{@options[:username]}:#{@options[:password]}@#{@options[:host]}:#{@options[:port]} " if @options[:debug]

  set_status(INITIALIZING)

end

Instance Method Details

#disconnectObject



134
135
136
137
138
139
140
141
142
# File 'lib/mail_daemon/imap/connection.rb', line 134

def disconnect
  puts "disconnecting" if @options[:debug]
  @idle_required = false
  logout unless @imap.disconnected?

  set_status(DISCONNECTING)
  @imap.disconnect
  set_status(DISCONNECTED)
end

#loginObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mail_daemon/imap/connection.rb', line 33

def 
  set_status(CONNECTING)
  puts "Creating a new imap object" if @options[:debug]
  puts "SSL Options;" if @options[:debug]
  puts @options[:ssl_options] if @options[:debug]
  puts "" if @options[:debug]
  @imap = Net::IMAP.new(@options[:host], :port => @options[:port], :ssl => @options[:ssl_options])
  capabilities = @imap.capability
  puts "Imap server capabilities are as follows:" if @options[:debug]
  puts capabilities if @options[:debug]
  puts "" if @options[:debug]
  @idle_available = capabilities.include?("IDLE")

  if @idle_available
    puts "Idle is available" if @options[:debug]
  else
    puts "Idle is not available" if @options[:debug]
  end

  set_status(LOGGING_ON)

  puts "Logging in..." if @options[:debug]
  @imap.(@options[:username], @options[:password])

  @options[:folder] = "INBOX"
  puts "Selecting folder '#{@options[:folder]}'" if @options[:debug]
  @imap.select(@options[:folder])

  @imap.starttls({}, verify=false) if @options[:start_tls]

  @idle_required = true
  puts "Logged on." if @options[:debug]
  set_status(LOGGED_ON)
end

#logoutObject



126
127
128
129
130
131
132
# File 'lib/mail_daemon/imap/connection.rb', line 126

def logout
  puts "Logging out" if @options[:debug]
  @imap.idle_done if @idleing
  set_status(LOGGING_OFF)
  @imap.logout
  set_status(LOGGED_OFF)
end

#running?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/mail_daemon/imap/connection.rb', line 148

def running?
  !@imap.disconnected?
end

#statusObject



144
145
146
# File 'lib/mail_daemon/imap/connection.rb', line 144

def status
  {:status => @status}
end

#wait_for_messages(&block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mail_daemon/imap/connection.rb', line 68

def wait_for_messages(&block)
  puts "Fetching awaiting messages" if @options[:debug]
  fetch_message_ids.each do |message_id|
    yield fetch_message(message_id)
  end

  @watcher_block = block

  if @idle_available
    while(@idle_required)
      puts "Starting Idle thread..." if @options[:debug]
      puts "Will recycle in #{@options[:idle_recycle_time]} seconds" if @options[:debug]
      recycle_thread = Thread.new(Time.now + @options[:idle_recycle_time]) do |end_time|
        begin
          while Time.now < end_time
            sleep 1
          end
          puts "Recycling idle thread" if @options[:debug]
          @imap.idle_done
        rescue => e
          puts e.message
          puts e.backtrace.join("\n")
        end
      end

      @idleing = true
      @imap.idle do |message|
        begin
          if message.members.include?(:data) && !message[:data].kind_of?(Fixnum) && message[:data].members.include?(:text) && message[:data][:text] == "idling"
            set_status(IDLEING)
          else
            @imap.idle_done
          end
        rescue => e
          puts e.message
          puts e.backtrace.join("\n")
        end
      end
      recycle_thread.kill
      @idleing = false
      puts "Fetching messages" if @options[:debug]
      fetch_message_ids.each do |message_id|
        yield fetch_message(message_id)
      end
    end
  else
    puts "We're in polling mode..." if @options[:debug]
    set_status(POLLING)
    while(@idle_required)
      puts "Checking for messages" if @options[:debug]
      fetch_message_ids.each do |message_id|
        yield fetch_message(message_id)
      end
      sleep @options[:sleep_time]
    end
  end
end