lib/inbox-sync/sync.rb in inbox-sync-0.2.1 vs lib/inbox-sync/sync.rb in inbox-sync-0.3.0

- old
+ new

@@ -1,9 +1,10 @@ require 'net/imap' require 'net/smtp' require 'inbox-sync/config' +require 'inbox-sync/filter_actions' require 'inbox-sync/notice/sync_mail_item_error' module InboxSync class Sync @@ -54,13 +55,14 @@ def run(runner=nil) return if runner && runner.shutdown? each_source_mail_item(runner) do |mail_item| begin + logger.debug "** #{mail_item.inspect}" response = send_to_dest(mail_item) dest_uid = parse_append_response_uid(response) - logger.debug "** dest uid: #{dest_uid.inspect}" + apply_dest_filters(dest_uid) rescue Exception => err log_error(err) notify(Notice::SyncMailItemError.new(@notify_smtp, @config.notify, { :error => err, :mail_item => mail_item, @@ -104,11 +106,10 @@ items.each do |mail_item| if runner && runner.shutdown? logger.info "* the runner has been shutdown - aborting the sync" break end - logger.debug "** #{mail_item.inspect}" yield mail_item end items = nil end @@ -141,10 +142,25 @@ using_dest_imap do |imap| imap.append(inbox, mail_s, flags, date) end end + def apply_dest_filters(dest_uid) + logger.info "** applying filters for dest uid: #{dest_uid.inspect}" + using_dest_imap do |imap| + dest_mail_item = MailItem.new(imap, dest_uid) + logger.debug "** #{dest_mail_item.inspect}" + + actions = FilterActions.new(dest_mail_item.message) + actions.match!(@config.filters) + logger.debug "** flag as: #{actions.flags.inspect}" + logger.debug "** copy to: #{actions.copies.inspect}" + + actions.apply!(imap, dest_uid) + end + end + def archive_on_source(mail_item) folder = @config.archive_folder if !folder.nil? && !folder.empty? logger.debug "** Archiving #{mail_item.uid.inspect}" @@ -162,14 +178,11 @@ logger.debug "** Copying #{mail_item.uid.inspect} to #{folder.inspect}" @source_imap.uid_copy(mail_item.uid, folder) end mark_as_deleted(@source_imap, mail_item.uid) - expunge_imap(@source_imap, @config.source) - - @source_imap.expunge end def using_dest_imap dest_imap = login_imap(:dest, @config.dest) result = yield dest_imap @@ -235,10 +248,10 @@ # Given a response like this: # #<struct Net::IMAP::TaggedResponse tag="RUBY0012", name="OK", data=#<struct Net::IMAP::ResponseText code=#<struct Net::IMAP::ResponseCode name="APPENDUID", data="6 9">, text=" (Success)">, raw_data="RUBY0012 OK [APPENDUID 6 9] (Success)\r\n"> # (here '9' is the UID) def parse_append_response_uid(response) - response.data.code.data.split(/\s+/).last + response.data.code.data.split(/\s+/).last.to_i end def mark_as_seen(imap, uid) logger.debug "** Marking #{uid.inspect} as :Seen" imap.uid_store(uid, "+FLAGS", [:Seen])