summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/feed2imap-cleaner32
-rw-r--r--lib/feed2imap/imap.rb31
2 files changed, 63 insertions, 0 deletions
diff --git a/bin/feed2imap-cleaner b/bin/feed2imap-cleaner
new file mode 100755
index 0000000..dc119f6
--- /dev/null
+++ b/bin/feed2imap-cleaner
@@ -0,0 +1,32 @@
+#!/usr/bin/ruby
+
+$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
+
+require 'feed2imap/feed2imap'
+require 'optparse'
+
+configf = ENV['HOME'] + '/.feed2imaprc'
+dryrun = false
+
+opts = OptionParser::new do |opts|
+ opts.banner = "Usage: feed2imap-cleaner [options]"
+ opts.separator ""
+ opts.separator "Options:"
+ opts.on("-d", "--dry-run", "Dont really remove messages") do |v|
+ dryrun = true
+ end
+ opts.on("-f", "--config <file>", "Select alternate config file") do |f|
+ configf = f
+ end
+end
+opts.parse!(ARGV)
+
+config = nil
+File::open(configf) { |f| config = F2IConfig::new(f) }
+config.imap_accounts.each_value do |ac|
+ ac.connect
+end
+config.feeds.each do |f|
+ f.imapaccount.cleanup(f.folder, dryrun)
+end
+
diff --git a/lib/feed2imap/imap.rb b/lib/feed2imap/imap.rb
index 8c4c912..bb905e6 100644
--- a/lib/feed2imap/imap.rb
+++ b/lib/feed2imap/imap.rb
@@ -50,6 +50,7 @@ class ImapAccount
def initialize(uri)
@uri = uri
+ self
end
# connects to the IMAP server
@@ -71,6 +72,7 @@ class ImapAccount
@connection = Net::IMAP::new(uri.host, port, usessl)
user, password = uri.userinfo.split(':',2)
@connection.login(user, password)
+ self
end
# disconnect from the IMAP server
@@ -96,6 +98,7 @@ class ImapAccount
@connection.append(folder, mail)
end
+ # update a mail
def updatemail(folder, mail, idx)
@connection.select(folder)
searchres = @connection.search(['HEADER', 'X-CacheIndex', "-#{idx}-"])
@@ -109,8 +112,36 @@ class ImapAccount
@connection.append(folder, mail, flags)
end
+ # convert to string
def to_s
uri.to_s
end
+
+ # remove mails in a folder according to a criteria
+ def cleanup(folder, dryrun = false)
+ puts "-- Considering #{folder}:"
+ @connection.select(folder)
+ a = ['NOT', 'NEW', 'NOT', 'FLAGGED', 'BEFORE', (Date::today - 10).strftime('%d-%b-%Y')]
+ todel = @connection.search(a)
+ todel.each do |m|
+ f = @connection.fetch(m, "FULL")
+ d = f[0].attr['INTERNALDATE']
+ s = f[0].attr['ENVELOPE'].subject
+ if s =~ /^=\?utf-8\?b\?/
+ s = Base64::decode64(s.gsub(/^=\?utf-8\?b\?(.*)\?=$/, '\1')).toISO_8859_1('utf-8')
+ end
+ if dryrun
+ puts "To remove: #{s} (#{d})"
+ else
+ puts "Removing: #{s} (#{d})"
+ @connection.store(m, "+FLAGS", [:Deleted])
+ end
+ end
+ puts "-- Deleted #{todel.length} messages."
+ if not dryrun
+ @connection.expunge
+ end
+ return todel.length
+ end
end