12 OPTIONPARSERSUCKS = "\n" + " " * 38
13 OptionParser.new do |opts|
15 Usage: sup-recover-sources [options] <source>+
17 Rebuilds a lost sources.yaml file by reading messages from a list of
18 sources and determining, for each source, the most prevalent
19 'source_id' field of messages from that source in the index.
21 The only non-deterministic component to this is that if the same
22 message appears in multiple sources, those sources may be
23 mis-diagnosed by this program.
25 If the first N messages (--scan-num below) all have the same source_id
26 in the index, the source will be added to sources.yaml. Otherwise, the
27 distribution will be printed, and you will have to add it by hand.
29 The offset pointer into the sources will be set to the end of the source,
30 so you will have to run sup-import --rebuild for each new source after
36 opts.on("--unusual", "Mark sources as 'unusual'. Only usual#{OPTIONPARSERSUCKS}sources will be polled by hand. Default:#{OPTIONPARSERSUCKS}#{$opts[:unusual]}.") { $opts[:unusual] = true }
38 opts.on("--archive", "Mark sources as 'archive'. New messages#{OPTIONPARSERSUCKS}from these sources will not appear in#{OPTIONPARSERSUCKS}the inbox. Default: #{$opts[:archive]}.") { $opts[:archive] = true }
40 opts.on("--scan-num N", Integer, "Number of messages to scan per source.#{OPTIONPARSERSUCKS}Default: #{$opts[:scan_num]}.") do |n|
44 opts.on_tail("-h", "--help", "Show this message") do
52 puts "loading index..."
53 index = Redwood::Index.new
55 puts "loaded index of #{index.size} messages"
58 next if Redwood::SourceManager.source_for fn
60 ## TODO: merge this code with the same snippet in import
64 print "Username for #{fn}: "
65 username = $stdin.gets.chomp
66 print "Password for #{fn} (warning: cleartext): "
67 password = $stdin.gets.chomp
68 Redwood::IMAP.new(fn, username, password, nil, !$opts[:unusual], $opts[:archive])
70 Redwood::MBox::Loader.new(fn, nil, !$opts[:unusual], $opts[:archive])
73 source_ids = Hash.new 0
75 source.each do |offset, labels|
76 m = Redwood::Message.new :source => source, :source_info => offset
78 source_id = Redwood::SourceManager.source_for_id m.id
80 source_ids[source_id] += 1
82 break if count == $opts[:scan_num]
85 if source_ids.size == 1
86 id = source_ids.keys.first.to_i
87 puts "assigned #{source} to #{source_ids.keys.first}"
89 Redwood::SourceManager.add_source source
91 puts ">> unable to determine #{source}: #{source_ids.inspect}"