bin/sup-import in sup-0.0.2 vs bin/sup-import in sup-0.0.3

- old
+ new

@@ -1,9 +1,15 @@ #!/usr/bin/env ruby +require 'uri' +require 'rubygems' +require 'highline/import' require "sup" + +Thread.abort_on_exception = true # make debugging possible + class Float def to_s; sprintf '%.2f', self; end end class Numeric @@ -55,10 +61,43 @@ --help: don't do anything, just show this message. EOS exit end +## for sources that require login information, prompt the user for +## that. also provide a list of previously-defined login info to +## choose from, if any. +def get_login_info uri, sources + uri = URI(uri) + accounts = sources.map do |s| + next unless s.respond_to?(:username) + suri = URI(s.uri) + [suri.host, s.username, s.password] + end.compact.uniq.sort_by { |h, u, p| h == uri.host ? 0 : 1 } + + username, password = nil, nil + unless accounts.empty? + say "Would you like to use the same account as for a previous source?" + choose do |menu| + accounts.each do |host, olduser, oldpw| + menu.choice("Use the account info for #{olduser}@#{host}") { username, password = olduser, oldpw } + end + menu.choice("Use a new account") { } + menu.prompt = "Account selection? " + end + end + + unless username && password + username = ask("Username for #{uri.host}: "); + password = ask("Password for #{uri.host}: ") { |q| q.echo = false } + puts # why? + end + + [username, password] +end + + educate_user if ARGV.member? '--help' archive = ARGV.delete "--archive" unusual = ARGV.delete "--unusual" force_archive = ARGV.delete "--force-archive" @@ -77,29 +116,31 @@ if(o = ARGV.find { |x| x =~ /^--/ }) $stderr.puts "error: unknown option #{o}" educate_user end -puts "loading index..." +$terminal.wrap_at = :auto +Redwood::start index = Redwood::Index.new index.load -puts "loaded index of #{index.size} messages" -sources = ARGV.map do |fn| - fn = "mbox://#{fn}" unless fn =~ %r!://! - source = index.source_for fn +sources = ARGV.map do |uri| + uri = "mbox://#{uri}" unless uri =~ %r!://! + source = index.source_for uri unless source source = - case fn + case uri + when %r!^mbox\+ssh://! + say "For SSH connections, if you will use public key authentication, you may leave the username and password blank." + say "\n" + username, password = get_login_info uri, index.sources + Redwood::MBox::SSHLoader.new(uri, username, password, nil, !unusual, !!archive) when %r!^imaps?://! - print "Username for #{fn}: " - username = $stdin.gets.chomp - print "Password for #{fn} (warning: cleartext): " - password = $stdin.gets.chomp - Redwood::IMAP.new(fn, username, password, nil, !unusual, !!archive) + username, password = get_login_info uri, index.sources + Redwood::IMAP.new(uri, username, password, nil, !unusual, !!archive) else - Redwood::MBox::Loader.new(fn, nil, !unusual, !!archive) + Redwood::MBox::Loader.new(uri, nil, !unusual, !!archive) end index.add_source source end source end @@ -116,11 +157,11 @@ found = {} start = Time.now begin sources.each do |source| if source.broken? - puts "error loading messages from #{source}: #{source.broken_msg}" + $stderr.puts "error loading messages from #{source}: #{source.broken_msg}" next end next if source.done? puts "loading from #{source}... " num = 0 @@ -135,12 +176,13 @@ puts "skipping duplicate message #{m.id}" next else found[m.id] = true end + m.remove_label :unread if m.status == "RO" unless force_read - puts "# message at #{offset}, labels: #{labels * ', '}" unless rebuild || force_rebuild + puts "# message at #{offset}, labels: #{labels * ', '}" if (rebuild || force_rebuild) && (docid, entry = index.load_entry_for_id(m.id)) && entry if force_rebuild || entry[:source_info].to_i != offset puts "replacing message #{m.id} labels #{entry[:label].inspect} (offset #{entry[:source_info]} => #{offset})" m.labels = entry[:label].split.map { |l| l.intern } @@ -160,10 +202,12 @@ end end puts "loaded #{num} messages" unless num == 0 end ensure + $stderr.puts "saving index and sources..." index.save + Redwood::finish end if rebuild || force_rebuild puts "deleting missing messages from the index..." numdel = num = 0