]> git.cworth.org Git - sup/blob - bin/sup-config
minor sup-config output tweaks
[sup] / bin / sup-config
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'highline/import'
5 require 'yaml'
6 require 'trollop'
7 require "sup"
8
9 $opts = Trollop::options do
10   version "sup-config (sup #{Redwood::VERSION})"
11   banner <<EOS
12 Interactive configuration tool for Sup. Won't destroy existing
13 configuration.
14
15 Usage:
16   sup-config
17
18 Options:
19 EOS
20 end
21
22 def axe q, default=nil
23   ans = if default && !default.empty?
24     ask "#{q} (enter for \"#{default}\"): "
25   else
26     ask "#{q}: "
27   end
28   ans.empty? ? default : ans
29 end
30
31 def axe_yes q, default="n"
32   axe(q, default) =~ /^y|yes$/i
33 end
34
35 def build_cmd cmd
36   (ENV["RUBY_INVOCATION"] ? ENV["RUBY_INVOCATION"] + " " : "") + File.join(File.dirname($0), cmd)
37 end
38
39 def add_source
40   type = nil
41
42   say "Ok, adding a new source."
43   choose do |menu|
44     menu.prompt = "What type of mail source is it? "
45     menu.choice("mbox file") { type = :mbox }
46     menu.choice("maildir directory") { type = :maildir }
47     menu.choice("remote mbox file (accessible via ssh)") { type = :mboxssh }
48     menu.choice("IMAP server (secure)") { type = :imaps }
49     menu.choice("IMAP server (unsecure)") { type = :imap }
50     menu.choice("Get me out of here!") { return }
51   end
52
53   while true do
54     say "Ok, now for the details."
55
56     default_labels, components = case type
57     when :mbox
58       $last_fn ||= ENV["MAIL"]
59       fn = axe "What's the full path to the mbox file?", $last_fn
60       return if fn.nil? || fn.empty?
61
62       $last_fn = fn
63       [Redwood::MBox::Loader.suggest_labels_for(fn),
64        { :scheme => "mbox", :path => fn }]
65     when :maildir
66       $last_fn ||= ENV["MAIL"]
67       fn = axe "What's the full path to the maildir directory?", $last_fn
68       return if fn.nil? || fn.empty?
69
70       $last_fn = fn
71       [Redwood::Maildir.suggest_labels_for(fn),
72        { :scheme => "maildir", :path => fn }]
73     when :mboxssh
74       $last_server ||= "localhost"
75       srv = axe "What machine is the mbox file located on?", $last_server
76       return if srv.nil? || srv.empty?
77       $last_server = srv
78
79       fn = axe "What's the path to the mbox file?", $last_fn
80       return if fn.nil? || fn.empty?
81       $last_fn = fn
82       fn = "/#{fn}" # lame
83       [Redwood::MBox::SSHLoader.suggest_labels_for(fn),
84        { :scheme => "mbox+ssh", :host => srv, :path => fn }]
85     when :imap, :imaps
86       $last_server ||= "localhost"
87       srv = axe "What is the IMAP server (host, or host:port notation)?", $last_server
88       return if srv.nil? || srv.empty?
89       $last_server = srv
90
91       $last_folder ||= "INBOX"
92       fn = axe "What's the folder path?", $last_folder
93       return if fn.nil? || fn.empty?
94       $last_folder = fn
95
96       fn = "/#{fn}"
97       if srv =~ /^(\S+):(\d+)$/
98         host, port = $1, $2.to_i
99       else
100         host, port = srv, nil
101       end
102       [Redwood::IMAP.suggest_labels_for(fn),
103        { :scheme => type.to_s, :host => host, :port => port, :path => fn }]
104     end
105
106     uri = begin
107       URI::Generic.build components
108     rescue URI::Error => e
109       say "Whoopsie! I couldn't build a URI from that: #{e.message}"
110       if axe_yes("Try again?") then next else return end
111     end
112
113     say "I'm going to add this source: #{uri}"
114     unless axe("Does that look right?", "y") =~ /^y|yes$/i
115       if axe_yes("Try again?") then next else return end
116     end
117
118     usual = axe_yes "Does this source ever receive new messages?", "y"
119     archive = usual ? axe_yes("Should new messages be automatically archived? (I.e. not appear in your inbox, though still be accessible via search.)") : false
120
121     labels_str = axe("Enter any labels to be automatically added to all messages from this source, separated by spaces (or 'none')", default_labels.join(","))
122
123     labels = if labels_str =~ /^\s*none\s*$/i
124       nil
125     else
126       labels_str.split(/\s+/)
127     end
128
129     cmd = build_cmd "sup-add"
130     cmd += " --unusual" unless usual
131     cmd += " --archive" if archive
132     cmd += " --labels=#{labels.join(',')}" if labels && !labels.empty?
133     cmd += " #{uri}"
134
135     puts "Ok, trying to run \"#{cmd}\"..."
136
137     system cmd
138     if $?.success?
139       say "Great! Added!"
140       break 
141     else
142       say "Rats, that failed. You may have to do it manually."
143       if axe_yes("Try again?") then next else return end
144     end
145   end
146 end
147
148 $terminal.wrap_at = :auto
149 Redwood::start
150 index = Redwood::Index.init
151 Redwood::SourceManager.load_sources
152
153 say <<EOS
154 Howdy neighbor! This here's sup-config, ready to help you jack in to
155 the next generation of digital cyberspace: the text-based email
156 program. Get ready to be the envy of everyone in your internets
157 with your amazing keyboarding skills! Jump from email to email with
158 nary a click of the mouse!
159
160 Just answer these simple questions and you'll be on your way.
161
162 EOS
163
164 account = $config[:accounts][:default]
165
166 name = axe "What's your name?", account[:name]
167 email = axe "What's your (primary) email address?", account[:email]
168
169 say "Ok, your from header will look like this:"
170 say "  From: #{name} <#{email}>"
171
172 say "\nDo you have any alternate email addresses that also receive email?"
173 say "If so, enter them now, separated by spaces."
174 alts = axe("Alternate email addresses", account[:alternates].join(" ")).split(/\s+/)
175
176 sigfn = axe "What file contains your signature?", account[:signature]
177 editor = axe "What editor would you like to use?", $config[:editor]
178
179 $config[:accounts][:default][:name] = name
180 $config[:accounts][:default][:email] = email
181 $config[:accounts][:default][:alternates] = alts
182 $config[:accounts][:default][:signature] = sigfn
183 $config[:editor] = editor
184
185 done = false
186 until done
187   say "\nNow, we'll tell Sup where to find all your email."
188   Redwood::SourceManager.load_sources
189   say "Current sources:"
190   if Redwood::SourceManager.sources.empty?
191     say "  No sources!"
192   else
193     Redwood::SourceManager.sources.each { |s| puts "* #{s}" }
194   end
195
196   say "\n"
197   choose do |menu|
198     menu.prompt = "Your wish? "
199     menu.choice("Add a new source.") { add_source }
200     menu.choice("Done adding sources!") { done = true }
201   end
202 end
203
204 say "\nSup needs to know where to store your sent messages."
205 say "Only sources capable of storing mail will be listed.\n\n"
206
207 Redwood::SourceManager.load_sources
208 if Redwood::SourceManager.sources.empty?
209   say "\nUsing the default sup://sent, since you haven't configured other sources yet."
210   $config[:sent_source] = 'sup://sent'
211 else
212   # this handles the event that source.yaml already contains the SentLoader
213   # source.
214   have_sup_sent = false
215
216   choose do |menu|
217     menu.prompt = "Store my sent mail in? "
218
219     menu.choice('Default (an mbox in ~/.sup, aka sup://sent)') { $config[:sent_source] = 'sup://sent'} unless have_sup_sent
220
221     valid_sents = Redwood::SourceManager.sources.each do |s|
222       have_sup_sent = true if s.to_s.eql?('sup://sent')
223       menu.choice(s.to_s) { $config[:sent_source] = s.to_s } if s.respond_to? :store_message
224     end
225   end
226 end
227
228 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
229
230 say "Ok, I've saved you up a nice lil' #{Redwood::CONFIG_FN}."
231
232 say <<EOS
233
234 The final step is to import all your messages into the Sup index.
235 Depending on how many messages are in the sources, this could take
236 quite a while.
237
238 EOS
239
240 if axe_yes "Run sup-sync to import all messages now?"
241   while true
242     cmd = build_cmd("sup-sync") + " --all-sources"
243     puts "Ok, trying to run \"#{cmd}\"..."
244     system cmd
245     if $?.success?
246       say "Great! It worked!"
247       break 
248     else
249       say "Rats, that failed. You may have to do it manually."
250       if axe_yes("Try again?") then next else break end
251     end
252   end
253 end
254
255 index.load
256
257 say <<EOS
258
259 Okee doke, you've got yourself an index of #{index.size} messages. Looks
260 like you're ready to jack in to cyberspace there, cowboy.
261
262 Just one last command:
263
264   #{build_cmd "sup"}
265
266 Have fun!
267 EOS