Sha256: da3b2cccacf6b53aa4a159a925b62f732cadd43841cef380bb85d6d17dbaadef

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'imap_processor/client'

##
# Cleanse removes old messages from your IMAP mailboxes so you don't
# have to!
#
# aka part one of my Plan for Total Email Domination.
#
# Cleanse doesn't remove messages you haven't read nor messages you've
# flagged.

class IMAPProcessor::Cleanse < IMAPProcessor::Client

  ##
  # Creates a new Cleanse from +options+.
  #
  # Options include:
  #   +:Age+:: Delete messages older than this many days ago
  #
  # and all options from IMAPClient

  def initialize(options)
    super

    @cleanse = options[:cleanse]
    @boxes = @cleanse.keys
  end

  def self.process_args(args)
    super __FILE__, args, {} do |opts, options|
      opts.banner << <<-EOF
imap_cleanse removes old messages from your IMAP mailboxes.
      EOF
    end
  end

  ##
  # Removes read, unflagged messages from all selected mailboxes...

  def run
    super "Cleansing read, unflagged old messages",
          [:Deleted] do
      @imap.expunge
      log "Expunged deleted messages"
    end
  end

  private

  ##
  # Searches for read, unflagged messages older than :Age in the currently
  # selected mailbox (see Net::IMAP#select).

  def find_messages
    mailbox = @boxes.find { |box| @mailbox =~ /#{Regexp.escape box}/ }
    raise unless mailbox
    age = @cleanse[mailbox]
    before_date = (Time.now - 86400 * age).imapdate

    search [
      'NOT', 'NEW',
      'NOT', 'FLAGGED',
      'BEFORE', before_date
    ], 'read, unflagged messages'
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imap_processor-1.8.1 lib/imap_processor/cleanse.rb