Class: Vmail::InboxPoller

Inherits:
ImapClient show all
Defined in:
lib/vmail/inbox_poller.rb

Constant Summary

Constants included from ShowingHeaders

ShowingHeaders::FLAGMAP

Instance Attribute Summary

Attributes inherited from ImapClient

#max_seqno

Instance Method Summary collapse

Methods inherited from ImapClient

#append_to_file, #check_for_new_messages, #clear_cached_message, #close, #create_if_necessary, daemon, #decrement_max_seqno, #deliver, #format_headers, #format_sent_message, #forward_template, #get_highest_message_id, #get_mailbox_status, #handle_error, #initialize, #list_mailboxes, #mailbox_aliases, #mailboxes, #more_messages, #new_mail_from_input, #new_message_template, #open, #open_html_part, #prime_connection, #reconnect_if_necessary, #reload_mailbox, #revive_connection, #save_attachments, #select_mailbox, #signature, #signature_script, #smtp_settings, #spawn_thread_if_tty, start, #window_width=, #with_open

Methods included from ReplyTemplating

#reply_cc, #reply_headers, #reply_recipient, #reply_template

Methods included from FlaggingAndMoving

#convert_to_message_ids, #copy_to, #flag, #move_to

Methods included from ShowingMessage

#cached_full_message?, #current_mail, #current_message, #fetch_and_cache, #format_parts_info, #show_message

Methods included from ShowingHeaders

#extract_address, #fetch_and_cache_headers, #format_flags, #format_header_for_list, #get_message_headers, #with_more_message_line

Methods included from Searching

#search, #search_query?

Methods included from AddressQuoter

#quote_addresses

Methods included from Helpers

#divider, #number_to_human_size, #retry_if_needed

Constructor Details

This class inherits a constructor from Vmail::ImapClient

Instance Method Details

#log(string) ⇒ Object



69
70
71
72
73
74
# File 'lib/vmail/inbox_poller.rb', line 69

def log(string)
  if string.is_a?(::Net::IMAP::TaggedResponse)
    string = string.raw_data
  end
  @logger.debug "[INBOX POLLER]: #{string}"
end

#start_pollingObject

This is a second IMAP client operating in a separate process



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vmail/inbox_poller.rb', line 7

def start_polling
  n = [`which notify-send`.chomp, `which growlnotify`.chomp].detect {|c| c != ''}
  if n
    log "Using notify tool: #{n}"
    @notifier = case n
      when /notify-send/
        Proc.new {|t, m| `#{n} '#{t}' '#{m}'` }
      when /growlnotify/
        Proc.new {|t, m| `#{n} -t '#{t}' -m '#{m}'` }
      end
  else
    log "No notification tool detected. INBOX polling aborted."
    return
  end
 
  sleep 30
  log "INBOX POLLER: started polling"
  @mailboxes.unshift "INBOX"
  select_mailbox "INBOX"
  search "ALL"
  loop do
    log "INBOX POLLER: checking inbox"
    update
    sleep 30
  end
end

#uncached_headers(id_set) ⇒ Object

doesn’t try to access Sequel / sqlite3



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vmail/inbox_poller.rb', line 46

def uncached_headers(id_set)
  log "Fetching headers for #{id_set.size} messages"
  results = reconnect_if_necessary do 
    @imap.fetch(id_set, ["FLAGS", "ENVELOPE", "RFC822.SIZE", "UID"])
  end
  results.reverse.map do |x| 
    envelope = x.attr["ENVELOPE"]
    message_id = envelope.message_id
    subject = Mail::Encodings.unquote_and_convert_to((envelope.subject || ''), 'UTF-8')
    recipients = ((envelope.to || []) + (envelope.cc || [])).map {|a| extract_address(a)}.join(', ')
    sender = extract_address envelope.from.first
    uid = x.attr["UID"]
    params = {
      subject: (subject || ''),
      flags: x.attr['FLAGS'].join(','),
      date: Time.parse(envelope.date).localtime.to_s,
      size: x.attr['RFC822.SIZE'],
      sender: sender,
      recipients: recipients
    }
  end
end

#updateObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/vmail/inbox_poller.rb', line 34

def update
  new_ids = check_for_new_messages 
  if !new_ids.empty?
    @ids = @ids + new_ids
    res = uncached_headers(new_ids).map {|m| m[:sender] }.join(", ")
    @notifier.call "Vmail: new email", "from #{res}"
  end
rescue
  log "VMAIL_ERROR: #{[$!.message, $!.backtrace].join("\n")}"
end