]> git.cworth.org Git - sup/blob - bin/sup-add
Add new :crypto_default configuration option.
[sup] / bin / sup-add
1 #!/usr/bin/env ruby
2
3 require 'uri'
4 require 'rubygems'
5 require 'highline/import'
6 require 'trollop'
7 require "sup"
8
9 $opts = Trollop::options do
10   version "sup-add (sup #{Redwood::VERSION})"
11   banner <<EOS
12 Adds a source to the Sup source list.
13
14 Usage:
15   sup-add [options] <source uri>+
16
17 where <source uri>+ is one or more source URIs.
18
19 For mbox files on local disk, use the form:
20     mbox:<path to mbox file>, or
21     mbox://<path to mbox file>
22
23 For mbox files on remote machines, use the form:
24     mbox+ssh://<machine name>/<path to mbox file>
25
26 For IMAP folders, use the form (note no username or password!):
27     imap://<machine name>/          # unsecure, "INBOX" folder  
28     imap://<machine name>/<folder>  # unsecure, arbitrary
29     imaps://<machine name>/         # secure, "INBOX" folder
30     imaps://<machine name>/<folder> # secure, arbitrary folder 
31
32 For Maildir folders, use the form:
33     maildir:<path to Maildir directory>; or
34     maildir://<path to Maildir directory>
35
36 Options are:
37 EOS
38   opt :archive, "Automatically archive all new messages from these sources."
39   opt :unusual, "Do not automatically poll these sources for new messages."
40   opt :labels, "A comma-separated set of labels to apply to all messages from this source", :type => String
41   opt :force_new, "Create a new account for this source, even if one already exists."
42 end
43
44 Trollop::die "require one or more sources" if ARGV.empty?
45
46 ## for sources that require login information, prompt the user for
47 ## that. also provide a list of previously-defined login info to
48 ## choose from, if any.
49 def get_login_info uri, sources
50   uri = URI(uri)
51   accounts = sources.map do |s|
52     next unless s.respond_to?(:username)
53     suri = URI(s.uri)
54     [suri.host, s.username, s.password]
55   end.compact.uniq.sort_by { |h, u, p| h == uri.host ? 0 : 1 }
56
57   username, password = nil, nil
58   unless accounts.empty? || $opts[:force_new]
59     say "Would you like to use the same account as for a previous source for #{uri}?"
60     choose do |menu|
61       accounts.each do |host, olduser, oldpw|
62         menu.choice("Use the account info for #{olduser}@#{host}") { username, password = olduser, oldpw }
63       end
64       menu.choice("Use a new account") { }
65       menu.prompt = "Account selection? "
66     end
67   end
68
69   unless username && password
70     username = ask("Username for #{uri.host}: ");
71     password = ask("Password for #{uri.host}: ") { |q| q.echo = false }
72     puts # why?
73   end
74
75   [username, password]
76 end
77
78 $terminal.wrap_at = :auto
79 Redwood::start
80 index = Redwood::Index.init
81
82 index.lock_interactively or exit
83
84 begin
85   Redwood::SourceManager.load_sources
86
87   ARGV.each do |uri|
88     labels = $opts[:labels] ? $opts[:labels].split(/\s*,\s*/).uniq : []
89
90     if !$opts[:force_new] && Redwood::SourceManager.source_for(uri)
91       say "Already know about #{uri}; skipping."
92       next
93     end
94
95     parsed_uri = URI(uri)
96
97     source = 
98       case parsed_uri.scheme
99       when "mbox+ssh"
100         say "For SSH connections, if you will use public key authentication, you may leave the username and password blank."
101         say ""
102         username, password = get_login_info uri, Redwood::SourceManager.sources
103         Redwood::MBox::SSHLoader.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
104       when "imap", "imaps"
105         username, password = get_login_info uri, Redwood::SourceManager.sources
106         Redwood::IMAP.new uri, username, password, nil, !$opts[:unusual], $opts[:archive], nil, labels
107       when "maildir"
108         Redwood::Maildir.new uri, nil, !$opts[:unusual], $opts[:archive], nil, labels
109       when "mbox"
110         Redwood::MBox::Loader.new uri, nil, !$opts[:unusual], $opts[:archive], nil, labels
111       when nil
112         Trollop::die "Sources must be specified with an URI"
113       else
114         Trollop::die "Unknown source type #{parsed_uri.scheme.inspect}"      
115       end
116     say "Adding #{source}..."
117     Redwood::SourceManager.add_source source
118   end
119 ensure
120   index.save
121   index.unlock
122   Redwood::finish
123 end