Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add two new options to the IMAP fetcher: processed_folder and error_f…

…older.

These folders are now created if they don't exist.

Change the IMAP code to use uid_* methods instead of the message sequence
ones, because the uid methods work better with GMail (and should work with
every IMAP server).
  • Loading branch information...
commit 7ed61c8f5599b590e28b23cb18fd83009ebb0085 1 parent e4540b9
Luke Francl authored

Showing 1 changed file with 21 additions and 4 deletions. Show diff stats Hide diff stats

  1. +21 4 lib/fetcher/imap.rb
25 lib/fetcher/imap.rb
@@ -12,11 +12,15 @@ class Imap < Base
12 12 # * <tt>:port</tt> - port to use (defaults to 143)
13 13 # * <tt>:ssl</tt> - use SSL to connect
14 14 # * <tt>:use_login</tt> - use LOGIN instead of AUTHENTICATE to connect (some IMAP servers, like GMail, do not support AUTHENTICATE)
  15 + # * <tt>:processed_folder</tt> - if set to the name of a mailbox, messages will be moved to that mailbox instead of deleted after processing. The mailbox will be created if it does not exist.
  16 + # * <tt>:error_folder:</tt> - the name of a mailbox where messages that cannot be processed (i.e., your receiver throws an exception) will be moved. Defaults to "bogus". The mailbox will be created if it does not exist.
15 17 def initialize(options={})
16 18 @authentication = options.delete(:authentication) || 'PLAIN'
17 19 @port = options.delete(:port) || PORT
18 20 @ssl = options.delete(:ssl)
19 21 @use_login = options.delete(:use_login)
  22 + @processed_folder = options.delete(:processed_folder)
  23 + @error_folder = options.delete(:error_folder) || 'bogus'
20 24 super(options)
21 25 end
22 26
@@ -33,21 +37,23 @@ def establish_connection
33 37 # Retrieve messages from server
34 38 def get_messages
35 39 @connection.select('INBOX')
36   - @connection.search(['ALL']).each do |message_id|
37   - msg = @connection.fetch(message_id,'RFC822')[0].attr['RFC822']
  40 + @connection.uid_search(['ALL']).each do |uid|
  41 + msg = @connection.uid_fetch(uid,'RFC822').first.attr['RFC822']
38 42 begin
39 43 process_message(msg)
  44 + add_to_processed_folder(uid) if @processed_folder
40 45 rescue
41 46 handle_bogus_message(msg)
42 47 end
43 48 # Mark message as deleted
44   - @connection.store(message_id, "+FLAGS", [:Deleted])
  49 + @connection.uid_store(uid, "+FLAGS", [:Seen, :Deleted])
45 50 end
46 51 end
47 52
48 53 # Store the message for inspection if the receiver errors
49 54 def handle_bogus_message(message)
50   - @connection.append('bogus', message)
  55 + create_mailbox(@error_folder)
  56 + @connection.append(@error_folder, message)
51 57 end
52 58
53 59 # Delete messages and log out
@@ -57,5 +63,16 @@ def close_connection
57 63 @connection.disconnect
58 64 end
59 65
  66 + def add_to_processed_folder(uid)
  67 + create_mailbox(@processed_folder)
  68 + @connection.uid_copy(uid, @processed_folder)
  69 + end
  70 +
  71 + def create_mailbox(mailbox)
  72 + unless @connection.list("", mailbox)
  73 + @connection.create(mailbox)
  74 + end
  75 + end
  76 +
60 77 end
61 78 end

0 comments on commit 7ed61c8

Please sign in to comment.
Something went wrong with that request. Please try again.