Class: IMAPProcessor::Client
- Inherits:
-
IMAPProcessor
- Object
- IMAPProcessor
- IMAPProcessor::Client
- Defined in:
- lib/imap_processor/client.rb
Overview
This class only exists to transition from IMAPCleanse to imap_processor
Constant Summary
Constants inherited from IMAPProcessor
Instance Attribute Summary
Attributes inherited from IMAPProcessor
Instance Method Summary collapse
-
#connect(host, port, ssl, username, password, auth = nil) ⇒ Object
Connects to IMAP server
host
atport
using ssl ifssl
is true then logs in asusername
withpassword
. -
#find_mailboxes ⇒ Object
Finds mailboxes with messages that were selected by the :Boxes option.
-
#initialize(options) ⇒ Client
constructor
Creates a new IMAPClient from
options
. -
#mark(messages, flags) ⇒ Object
Marks
messages
in the currently selected mailbox withflags
(see Net::IMAP#store). -
#run(message, flags) ⇒ Object
Selects messages from mailboxes then marking them with
flags
. -
#search(query, message) ⇒ Object
Searches for messages matching
query
in the selected mailbox (see Net::IMAP#select).
Methods inherited from IMAPProcessor
add_move, #capability, #create_mailbox, #delete_messages, #each_message, #each_part, #log, #mime_parts, #move_messages, #noop?, 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() super @noop = [:Noop] @root = [:Root] root = @root root += "/" unless root.empty? connect [:Host], [:Port], [:SSL], [:Username], [:Password], [: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_mailboxes ⇒ Object
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(, flags) .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(, flags) log = 0 mailboxes = find_mailboxes mailboxes.each do |mailbox| @mailbox = mailbox @imap.select @mailbox log "Selected #{@mailbox}" = next if .empty? += .length unless @noop then mark , flags else log "Noop - not marking" end yield if block_given? end log "Done. Found #{} 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, ) log " Scanning for #{}" = @imap.search query log " Found #{.length} messages" return end |