Class: ImapMogura::IMAPHandler
- Inherits:
-
Object
- Object
- ImapMogura::IMAPHandler
- Defined in:
- lib/imap_mogura/imap_handler.rb
Defined Under Namespace
Classes: Error, MailFetchError
Instance Method Summary collapse
- #all_mailbox_list ⇒ Object
- #close ⇒ Object
- #close_operation_for_mailbox(_) ⇒ Object
- #fetch_envelope(mailbox, message_id) ⇒ Object
- #fetch_header(mailbox, message_id) ⇒ Object
- #find_and_handle_mails(mailbox, search_keys, &block) ⇒ Object
- #handle_all_mails(mailbox, &block) ⇒ Object
-
#initialize(host, port = 143, starttls: true, usessl: false, certs: nil, verify: true, auth_info: nil) ⇒ IMAPHandler
constructor
A new instance of IMAPHandler.
- #monitor_events(mailbox, events, &block) ⇒ Object
- #move(src_mailbox, src_message_id, dst_mailbox) ⇒ Object
- #touch_mailbox(mailbox) ⇒ Object
- #wait_event_with_idle(mailbox, expected_response_names) ⇒ Object
Constructor Details
#initialize(host, port = 143, starttls: true, usessl: false, certs: nil, verify: true, auth_info: nil) ⇒ IMAPHandler
Returns a new instance of IMAPHandler.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/imap_mogura/imap_handler.rb', line 22 def initialize(host, port = 143, starttls: true, usessl: false, certs: nil, verify: true, auth_info: nil) @imap = Net::IMAP.new(host, port, usessl, certs, verify) if usessl || starttls @imap.starttls(certs, verify) if !usessl && starttls # in case with TLS, just authenticate with LOGIN command @imap.login(auth_info[:user], auth_info[:password]) else # in plain text session, use AUTHENTICATE command @imap.authenticate(auth_info[:auth_type], auth_info[:user], auth_info[:password]) end initialize_selected_mailbox end |
Instance Method Details
#all_mailbox_list ⇒ Object
66 67 68 |
# File 'lib/imap_mogura/imap_handler.rb', line 66 def all_mailbox_list @imap.list("", "*").map(&:name) end |
#close ⇒ Object
39 40 41 42 |
# File 'lib/imap_mogura/imap_handler.rb', line 39 def close close_mailbox @imap.disconnect end |
#close_operation_for_mailbox(_) ⇒ Object
117 118 119 |
# File 'lib/imap_mogura/imap_handler.rb', line 117 def close_operation_for_mailbox(_) close_mailbox end |
#fetch_envelope(mailbox, message_id) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/imap_mogura/imap_handler.rb', line 84 def fetch_envelope(mailbox, ) with_mailbox_selected(mailbox) do @imap.fetch(, "ENVELOPE")[0].attr["ENVELOPE"] rescue Net::IMAP::BadResponseError => e raise MailFetchError.new(mailbox, , e.) end end |
#fetch_header(mailbox, message_id) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/imap_mogura/imap_handler.rb', line 92 def fetch_header(mailbox, ) with_mailbox_selected(mailbox) do fetch_data = @imap.fetch(, "BODY.PEEK[HEADER]")[0].attr["BODY[HEADER]"] Mail.read_from_string(fetch_data) rescue Net::IMAP::BadResponseError => e raise MailFetchError.new(mailbox, , e.) end end |
#find_and_handle_mails(mailbox, search_keys, &block) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/imap_mogura/imap_handler.rb', line 74 def find_and_handle_mails(mailbox, search_keys, &block) select_mailbox(mailbox) @imap.search(search_keys).each do || break unless block block[] end end |
#handle_all_mails(mailbox, &block) ⇒ Object
70 71 72 |
# File 'lib/imap_mogura/imap_handler.rb', line 70 def handle_all_mails(mailbox, &block) find_and_handle_mails(mailbox, ["ALL"], &block) end |
#monitor_events(mailbox, events, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/imap_mogura/imap_handler.rb', line 44 def monitor_events(mailbox, events, &block) loop do resp = wait_event_with_idle(mailbox, events) break unless block block[resp] end end |
#move(src_mailbox, src_message_id, dst_mailbox) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/imap_mogura/imap_handler.rb', line 106 def move(src_mailbox, , dst_mailbox) return if src_mailbox == dst_mailbox # skip moving if src_mailbox is the same with dst_mailbox with_mailbox_selected(src_mailbox, readonly: false) do @imap.copy([], dst_mailbox) @imap.store(, "+FLAGS", [:Deleted]) end dst_mailbox end |
#touch_mailbox(mailbox) ⇒ Object
102 103 104 |
# File 'lib/imap_mogura/imap_handler.rb', line 102 def touch_mailbox(mailbox) @imap.create(mailbox) if @imap.list("", mailbox).empty? end |
#wait_event_with_idle(mailbox, expected_response_names) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/imap_mogura/imap_handler.rb', line 54 def wait_event_with_idle(mailbox, expected_response_names) select_mailbox(mailbox) @imap.idle do |resp| if expected_response_names.include? resp.name @imap.idle_done return resp end end end |