]> git.cworth.org Git - sup/blob - bin/sup-config
labels now fully determined by sources.yaml, and lots of improvements to sup-config
[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 full 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 =~ /^(\w+):(\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
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.new
155 index.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 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
191
192 say "Ok, I've saved you up a nice lil' #{Redwood::CONFIG_FN}."
193
194 done = false
195 until done
196   say "\nNow, we'll tell Sup where to find all your email."
197   index.load_sources
198   say "Current sources:"
199   if index.sources.empty?
200     say "  No sources!"
201   else
202     index.sources.each { |s| puts "* #{s}" }
203   end
204
205   say "\n"
206   choose do |menu|
207     menu.prompt = "Your wish? "
208     menu.choice("Add a new source.") { add_source }
209     menu.choice("Done adding sources!") { done = true }
210   end
211 end
212
213 say <<EOS
214
215 Ok. The final step is to import all your messages into the Sup index.
216 Depending on how many messages are in the sources, this could take
217 quite a while.
218
219 EOS
220 #'
221 if axe_yes "Run sup-sync to import all messages now?"
222   while true
223     cmd = build_cmd("sup-sync") + " --all-sources"
224     puts "Ok, trying to run \"#{cmd}\"..."
225     system cmd
226     if $?.success?
227       say "Great! It worked!"
228       break 
229     else
230       say "Rats, that failed. You may have to do it manually."
231       if axe_yes("Try again?") then next else break end
232     end
233   end
234 end
235
236 index.load
237
238 say <<EOS
239
240 Okee doke, you've got yourself an index of #{index.size} messages. Looks
241 like you're ready to jack in to cyberspace there, cowboy.
242
243 Just one last command:
244
245   #{build_cmd "sup"}
246
247 Have fun!
248 EOS