class IMAPProcessor::Client

This class only exists to transition from IMAPCleanse to imap_processor

Public Class Methods

new(options) click to toggle source

Creates a new IMAPClient from options.

Options include:

+:Verbose+:: Verbose flag
+:Noop+:: Don't delete anything flag
+:Root+:: IMAP root path
+:Boxes+:: Comma-separated list of mailbox prefixes to search
+:Host+:: IMAP server
+:Port+:: IMAP server port
+:SSL+:: SSL flag
+:Username+:: IMAP username
+:Password+:: IMAP password
+:Auth+:: IMAP authentication type
Calls superclass method IMAPProcessor::new
# File lib/imap_processor/client.rb, line 30
def initialize(options)

  @noop = options[:Noop]
  @root = options[:Root]

  root = @root
  root += "/" unless root.empty?

  connect options[:Host], options[:Port], options[:SSL],
          options[:Username], options[:Password], options[:Auth]

Public Instance Methods

connect(host, port, ssl, username, password, auth = nil) click to toggle source

Connects to IMAP server host at port using ssl if ssl is true then logs in as username with password. IMAPClient will really only work with PLAIN auth on SSL sockets, sorry.

# File lib/imap_processor/client.rb, line 85
def connect(host, port, ssl, username, password, auth = nil)
  @imap = host, port, ssl, nil, false
  log "Connected to #{host}:#{port}"

  if auth.nil? then
    auth_caps = { |c| c =~ /^AUTH/ }
    raise "Couldn't find a supported auth type" if auth_caps.empty?
    auth = auth_caps.first.sub(/AUTH=/, '')

  auth = auth.upcase
  log "Trying #{auth} authentication"
  @imap.authenticate auth, username, password
  log "Logged in as #{username}"
find_mailboxes() click to toggle source

Finds mailboxes with messages that were selected by the :Boxes option.

# File lib/imap_processor/client.rb, line 104
def find_mailboxes
  mailboxes = @imap.list(@root, "*")

  if mailboxes.nil? then
    log "Found no mailboxes under #{@root.inspect}, you may have an incorrect root"
    return []

  mailboxes.reject! { |mailbox| mailbox.attr.include? :Noselect }! { |mailbox| }

  @box_re = /^#{Regexp.escape @root}#{Regexp.union(*@boxes)}/

  mailboxes.reject! { |mailbox| mailbox !~ @box_re }
  mailboxes = mailboxes.sort_by { |m| m.downcase }
  log "Found #{mailboxes.length} mailboxes to search:"
  mailboxes.each { |mailbox| log "\t#{mailbox}" } if @verbose
  return mailboxes
mark(messages, flags) click to toggle source

Marks messages in the currently selected mailbox with flags (see Net::IMAP#store).

# File lib/imap_processor/client.rb, line 139
def mark(messages, flags)
  messages.each_slice(500) do |chunk| chunk, '+FLAGS.SILENT', flags
  log "Marked messages with flags"
run(message, flags) { |messages| ... } click to toggle source

Selects messages from mailboxes then marking them with flags. If a block is given it is run after message marking.

Unless :Noop was set, then it just prints out what it would do.

Automatically called by IMAPClient::run

# File lib/imap_processor/client.rb, line 51
def run(message, flags)
  log message

  message_count = 0
  mailboxes = find_mailboxes

  mailboxes.each do |mailbox|
    @mailbox = mailbox @mailbox
    log "Selected #{@mailbox}"

    messages = find_messages

    next if messages.empty?

    message_count += messages.length

    unless @noop then
      mark messages, flags
      log "Noop - not marking"

    yield messages if block_given?

  log "Done. Found #{message_count} messages in #{mailboxes.length} mailboxes"