5 require 'highline/import'
9 Thread.abort_on_exception = true # make debugging possible
11 $opts = Trollop::options do
12 version "sup-add (sup #{Redwood::VERSION})"
14 Adds a source to the Sup source list.
17 sup-add [options] <source>+
19 where <source>+ is one or more sources.
21 For mbox files on local disk, use the form:
22 mbox://<path to mbox file>
26 For mbox files on remote machines, use the form:
27 mbox+ssh://<machine name>/<path to mbox file>
29 For IMAP folders, use the form (note no username or password!):
30 imap://<machine name>/ # unsecure, "INBOX" folder
31 imap://<machine name>/<folder> # unsecure, arbitrary
32 imaps://<machine name>/ # secure, "INBOX" folder
33 imaps://<machine name>/<folder> # secure, arbitrary folder
37 opt :archive, "Automatically archive all new messages from these sources."
38 opt :unusual, "Do not automatically poll these sources for new messages."
39 opt :force_new, "Create a new account for this source, even if one already exists."
42 Trollop::die "require one or more sources" if ARGV.empty?
44 ## for sources that require login information, prompt the user for
45 ## that. also provide a list of previously-defined login info to
46 ## choose from, if any.
47 def get_login_info uri, sources
49 accounts = sources.map do |s|
50 next unless s.respond_to?(:username)
52 [suri.host, s.username, s.password]
53 end.compact.uniq.sort_by { |h, u, p| h == uri.host ? 0 : 1 }
55 username, password = nil, nil
56 unless accounts.empty? || $opts[:force_new]
57 say "Would you like to use the same account as for a previous source for #{uri}?"
59 accounts.each do |host, olduser, oldpw|
60 menu.choice("Use the account info for #{olduser}@#{host}") { username, password = olduser, oldpw }
62 menu.choice("Use a new account") { }
63 menu.prompt = "Account selection? "
67 unless username && password
68 username = ask("Username for #{uri.host}: ");
69 password = ask("Password for #{uri.host}: ") { |q| q.echo = false }
76 $terminal.wrap_at = :auto
78 index = Redwood::Index.new
82 uri = "mbox://#{uri}" unless uri =~ %r!://!
83 if !$opts[:force_new] && index.source_for(uri)
84 say "Already know about #{uri}; skipping."
89 when %r!^mbox\+ssh://!
90 say "For SSH connections, if you will use public key authentication, you may leave the username and password blank."
92 username, password = get_login_info uri, index.sources
93 Redwood::MBox::SSHLoader.new(uri, username, password, nil, !$opts[:unusual], $opts[:archive])
95 username, password = get_login_info uri, index.sources
96 Redwood::IMAP.new(uri, username, password, nil, !$opts[:unusual], $opts[:archive])
98 Redwood::MBox::Loader.new(uri, nil, !$opts[:unusual], $opts[:archive])
100 say "Adding #{source}..."
101 index.add_source source
104 say "Saving source list..."