IMAPCleanse 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/imap_client.rb +45 -9
  3. data/lib/imap_flag.rb +9 -7
  4. metadata +2 -2
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ $VERBOSE = nil
9
9
 
10
10
  spec = Gem::Specification.new do |s|
11
11
  s.name = 'IMAPCleanse'
12
- s.version = '1.1.1'
12
+ s.version = '1.2.0'
13
13
  s.summary = 'Removes mailbox oldness, finds mailbox interestingness!'
14
14
  s.description = 'IMAPCleanse removes old, read, unflagged messages from your IMAP mailboxes so you don\'t have to!
15
15
 
@@ -1,3 +1,4 @@
1
+ $TESTING = false unless defined? $TESTING
1
2
  require 'net/imap'
2
3
  require 'imap_sasl_plain'
3
4
  require 'optparse'
@@ -91,8 +92,20 @@ class IMAPClient
91
92
  options[:Password] = password
92
93
  end
93
94
 
95
+ authenticators = Net::IMAP.send :class_variable_get, :@@authenticators
96
+ auth_types = authenticators.keys.sort.join ', '
97
+ opts.on("-a", "--auth AUTH",
98
+ "IMAP authentication type override",
99
+ "Authentication type will be auto-",
100
+ "discovered",
101
+ "Default: #{options[:Auth].inspect}",
102
+ "Valid values: #{auth_types}",
103
+ "Options file name: Auth") do |auth|
104
+ options[:Auth] = auth
105
+ end
106
+
94
107
  opts.separator ''
95
- opts.separator "#{self.class} options:"
108
+ opts.separator "#{self} options:"
96
109
 
97
110
  opts.on("-r", "--root ROOT",
98
111
  "Root of mailbox hierarchy",
@@ -166,6 +179,10 @@ class IMAPClient
166
179
  options = process_args args
167
180
  client = new options
168
181
  client.run
182
+ rescue => e
183
+ $stderr.puts "Failed to finish with exception: #{e.class}:#{e.message}"
184
+ $stderr.puts "\t#{e.backtrace.join "\n\t"}"
185
+ exit 1
169
186
  end
170
187
 
171
188
  ##
@@ -181,22 +198,26 @@ class IMAPClient
181
198
  # +:SSL+:: SSL flag
182
199
  # +:Username+:: IMAP username
183
200
  # +:Password+:: IMAP password
201
+ # +:Auth+:: IMAP authentication type
184
202
 
185
203
  def initialize(options)
186
204
  @verbose = options[:Verbose]
187
205
  @noop = options[:Noop]
188
206
  @root = options[:Root]
189
207
 
190
- boxes = options[:Boxes].split(',').map { |b| Regexp.escape b }
191
- @box_re = /^#{Regexp.escape @root}\/#{Regexp.union(*boxes)}/
208
+ root = @root
209
+ root += "/" unless root.empty?
210
+
211
+ boxes = options[:Boxes].split(/,\s*/)
212
+ @box_re = /^#{Regexp.escape root}#{Regexp.union(*boxes)}/
192
213
 
193
214
  connect options[:Host], options[:Port], options[:SSL],
194
- options[:Username], options[:Password]
215
+ options[:Username], options[:Password], options[:Auth]
195
216
  end
196
217
 
197
218
  ##
198
- # Runs the main selecting messages from mailboxes then marking them
199
- # with +flags+. If a block is given it is run after message marking.
219
+ # Selects messages from mailboxes then marking them with +flags+. If a
220
+ # block is given it is run after message marking.
200
221
  #
201
222
  # Unless :Noop was set, then it just prints out what it would do.
202
223
  #
@@ -231,17 +252,26 @@ class IMAPClient
231
252
  log "Done. Found #{message_count} messages in #{mailboxes.length} mailboxes"
232
253
  end
233
254
 
234
- private
255
+ private unless $TESTING
235
256
 
236
257
  ##
237
258
  # Connects to IMAP server +host+ at +port+ using ssl if +ssl+ is true then
238
259
  # logs in as +username+ with +password+. IMAPClient will really only work
239
260
  # with PLAIN auth on SSL sockets, sorry.
240
261
 
241
- def connect(host, port, ssl, username, password)
262
+ def connect(host, port, ssl, username, password, auth = nil)
242
263
  @imap = Net::IMAP.new host, port, ssl
243
264
  log "Connected to #{host}:#{port}"
244
- @imap.authenticate 'PLAIN', username, password
265
+
266
+ if auth.nil? then
267
+ auth_caps = @imap.capability.select { |c| c =~ /^AUTH/ }
268
+ raise "Couldn't find a supported auth type" if auth_caps.empty?
269
+ auth = auth_caps.first.sub(/AUTH=/, '')
270
+ end
271
+
272
+ auth = auth.upcase
273
+ log "Trying #{auth} authentication"
274
+ @imap.authenticate auth, username, password
245
275
  log "Logged in as #{username}"
246
276
  end
247
277
 
@@ -250,6 +280,12 @@ class IMAPClient
250
280
 
251
281
  def find_mailboxes
252
282
  mailboxes = @imap.list(@root, "*")
283
+
284
+ if mailboxes.nil? then
285
+ log "Found no mailboxes, you may have an incorrect root"
286
+ return []
287
+ end
288
+
253
289
  mailboxes.reject! { |mailbox| mailbox.attr.include? :Noselect }
254
290
  mailboxes.map! { |mailbox| mailbox.name }
255
291
  mailboxes.reject! { |mailbox| mailbox !~ @box_re }
@@ -51,7 +51,7 @@ class IMAPFlag < IMAPClient
51
51
  # and all options from IMAPClient
52
52
 
53
53
  def initialize(options)
54
- @email = options[:Email]
54
+ @email = options[:Email].split(/,\s*/)
55
55
  super
56
56
  end
57
57
 
@@ -86,11 +86,13 @@ class IMAPFlag < IMAPClient
86
86
  # Messages I wrote in the selected mailbox.
87
87
 
88
88
  def wrote_in_curr
89
- search [
90
- 'FROM', @email,
91
- 'NOT', 'FLAGGED',
92
- 'NOT', 'KEYWORD', AUTO_FLAG_KEYWORD
93
- ], 'messages I wrote'
89
+ @email.map { |email|
90
+ search [
91
+ 'FROM', email,
92
+ 'NOT', 'FLAGGED',
93
+ 'NOT', 'KEYWORD', AUTO_FLAG_KEYWORD
94
+ ], "messages by #{email}"
95
+ }
94
96
  end
95
97
 
96
98
  ##
@@ -98,7 +100,7 @@ class IMAPFlag < IMAPClient
98
100
 
99
101
  def responses_in_curr
100
102
  log "Scanning for responses to messages I wrote"
101
- my_mail = @imap.search [ 'FROM', @email ]
103
+ my_mail = @email.map { |email| @imap.search [ 'FROM', email ] }.flatten
102
104
 
103
105
  return [] if my_mail.empty?
104
106
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11.15
3
3
  specification_version: 1
4
4
  name: IMAPCleanse
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.1
7
- date: 2006-05-08 00:00:00 -07:00
6
+ version: 1.2.0
7
+ date: 2006-05-23 00:00:00 -07:00
8
8
  summary: Removes mailbox oldness, finds mailbox interestingness!
9
9
  require_paths:
10
10
  - lib