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