Class: IMAPProcessor::Client

Inherits:
IMAPProcessor show all
Defined in:
lib/imap_processor/client.rb

Overview

This class only exists to transition from IMAPCleanse to imap_processor

Direct Known Subclasses

Cleanse, Flag, Learn

Constant Summary

Constants inherited from IMAPProcessor

VERSION

Instance Attribute Summary

Attributes inherited from IMAPProcessor

#imap, #options

Instance Method Summary collapse

Methods inherited from IMAPProcessor

add_move, #capability, #create_mailbox, #delete_messages, #each_message, #each_part, #log, #mime_parts, #move_messages, process_args, run, #show_messages, #verbose?

Constructor Details

#initialize(options) ⇒ Client

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


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/imap_processor/client.rb', line 30

def initialize(options)
  super

  @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]
end

Instance Method Details

#connect(host, port, ssl, username, password, auth = nil) ⇒ Object

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/imap_processor/client.rb', line 85

def connect(host, port, ssl, username, password, auth = nil)
  @imap = Net::IMAP.new host, port, ssl, nil, false
  log "Connected to #{host}:#{port}"

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

  auth = auth.upcase
  log "Trying #{auth} authentication"
  @imap.authenticate auth, username, password
  log "Logged in as #{username}"
end

#find_mailboxesObject

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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 []
  end

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

  @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
end

#mark(messages, flags) ⇒ Object

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



139
140
141
142
143
144
# File 'lib/imap_processor/client.rb', line 139

def mark(messages, flags)
  messages.each_slice(500) do |chunk|
    @imap.store chunk, '+FLAGS.SILENT', flags
  end
  log "Marked messages with flags"
end

#run(message, flags) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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
    @imap.select @mailbox
    log "Selected #{@mailbox}"

    messages = find_messages

    next if messages.empty?

    message_count += messages.length

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

    yield messages if block_given?
  end

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

#search(query, message) ⇒ Object

Searches for messages matching query in the selected mailbox (see Net::IMAP#select). Logs ‘Scanning for message’ before searching.



128
129
130
131
132
133
# File 'lib/imap_processor/client.rb', line 128

def search(query, message)
  log "  Scanning for #{message}"
  messages = @imap.search query
  log "    Found #{messages.length} messages"
  return messages
end