summaryrefslogtreecommitdiff
path: root/bin/feed2imap-cleaner
blob: b6165850acfd20173a9692f5fd08120d874ada4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
#!/usr/bin/ruby

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'feed2imap/feed2imap'
require 'optparse'

configf = ENV['HOME'] + '/.feed2imaprc'
dryrun = false
docache = 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("-c", "--cache", "Clean cache instead of messages") do |v|
    docache = 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) }
if docache
  puts 'Initializing cache ...'
  cache = ItemCache::new(true)
  if not File::exist?(config.cache + '.lock')
    f = File::new(config.cache + '.lock', 'w')
    f.close
  end
  if File::new(config.cache + '.lock', 'w').flock(File::LOCK_EX | File::LOCK_NB) == false
    puts "Another instance of feed2imap is already locking the cache file"
    exit(1)
  end
  if File::exist?(config.cache) 
    File::open(config.cache) do |f|
      cache.load(f)
    end
  end

  before = cache.nbchannels

  puts "Cleaning up"
  cache.cleanup(config.feeds)

  after = cache.nbchannels

  if not dryrun
    puts "Saving cache ..."
    begin
      File::open("#{config.cache}.new", 'w') { |f| cache.save(f) }
    rescue
      puts "Exception caught while writing new cache to #{config.cache}.new: #{$!}"
    end
    begin
      File::rename("#{config.cache}.new", config.cache)
    rescue
      puts"Exception caught while renaming #{@config.cache}.new to #{@config.cache}: #{$!}"
    end
  end

  puts "#Channels before: #{before}, after: #{after}"
else
  config.imap_accounts.each_value do |ac|
    ac.connect
  end
  config.feeds.each do |f|
    f.imapaccount.cleanup(f.folder, dryrun)
  end
end