#!/usr/bin/env ruby require 'rubygems' require 'uri' require 'tempfile' require 'trollop' require 'enumerator' require "sup" ## save a message 'm' to an open file pointer 'fp' def save m, fp m.source.each_raw_message_line(m.source_info) { |l| fp.print l } end def die msg $stderr.puts "Error: #{msg}" exit(-1) end def has_any_from_source_with_label? index, source, label query = { :source_id => source.id, :label => label, :limit => 1, :load_spam => true, :load_deleted => true, :load_killed => true } not Enumerable::Enumerator.new(index, :each_id, query).map.empty? end opts = Trollop::options do version "sup-sync-back (sup #{Redwood::VERSION})" banner <* where * is zero or more source URIs. If no sources are given, sync back all usual sources. You almost certainly want to run sup-sync --changed after this command. Running this does not change the index. Options include: EOS opt :drop_deleted, "Drop deleted messages.", :default => false, :short => "d" opt :move_deleted, "Move deleted messages to a local mbox file.", :type => String, :short => :none opt :drop_spam, "Drop spam messages.", :default => false, :short => "s" opt :move_spam, "Move spam messages to a local mbox file.", :type => String, :short => :none opt :with_dotlockfile, "Specific dotlockfile location (mbox files only).", :default => "/usr/bin/dotlockfile", :short => :none opt :dont_use_dotlockfile, "Don't use dotlockfile to lock mbox files. Dangerous if other processes modify them concurrently.", :default => false, :short => :none opt :verbose, "Print message ids as they're processed." opt :dry_run, "Don't actually modify the index. Probably only useful with --verbose.", :short => "-n" opt :version, "Show version information", :short => :none conflicts :drop_deleted, :move_deleted conflicts :drop_spam, :move_spam end unless opts[:drop_deleted] || opts[:move_deleted] || opts[:drop_spam] || opts[:move_spam] puts < 0 || num_moved > 0 out_fp.close unless opts[:dry_run] unless opts[:dry_run] || (num_dropped == 0 && num_moved == 0) deleted_fp.flush if deleted_fp spam_fp.flush if spam_fp unless opts[:dont_use_dotlockfile] puts "Locking #{source.file_path}..." system "#{opts[:dotlockfile]} -l #{source.file_path}" puts "Writing #{source.file_path}..." FileUtils.cp out_fp.path, source.file_path puts "Unlocking #{source.file_path}..." system "#{opts[:dotlockfile]} -u #{source.file_path}" end end end unless opts[:dry_run] deleted_fp.close if deleted_fp spam_fp.close if spam_fp end $stderr.puts "Done." unless modified_sources.empty? $stderr.puts "You should now run: sup-sync --changed #{modified_sources.join(' ')}" end rescue Exception => e File.open("sup-exception-log.txt", "w") { |f| f.puts e.backtrace } raise ensure Redwood::finish index.unlock end