]> git.cworth.org Git - sup/blob - bin/sup-config
ready for 0.0.8
[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 "Now for the details."
56
57     components = 
58       case type
59       when :mbox
60         fn = axe "What's the full path to the mbox file?", ENV["MAIL"] #"srm
61         return if fn.nil? || fn.empty?
62         { :scheme => "mbox", :path => fn }
63       when :maildir
64         fn = axe "What's the full path to the maildir directory?", ENV["MAIL"] #"srm
65         return if fn.nil? || fn.empty?
66         { :scheme => "maildir", :path => fn }
67       when :mboxssh
68         srv = axe "What server is the mbox file located on?", $last_server 
69         return if srv.nil? || srv.empty?
70         $last_server = srv
71         fn = axe "What's the full path to the mbox file?", ENV["MAIL"] #"srm
72         return if fn.nil? || fn.empty?
73         fn = "/#{fn}" # lame
74         { :scheme => "mbox+ssh", :host => srv, :path => fn }
75       when :imap, :imaps
76         srv = axe "What is the IMAP server?", $last_server
77         return if srv.nil? || srv.empty?
78         $last_server = srv
79         fn = axe "What's the folder path?", "INBOX" #"srm 
80         return if fn.nil? || fn.empty?
81         fn = "/#{fn}" # lame
82         { :scheme => type.to_s, :host => srv, :path => fn }
83       end
84     
85     uri = 
86       begin
87         URI::Generic.build components
88       rescue URI::Error => e
89         say "Whoopsie! I couldn't build a URI from that: #{e.message}"
90         if axe_yes("Try again?") then next else return end
91       end
92
93     say "I'm going to add this source: #{uri}."
94     unless axe("Does that look right?", "y") =~ /^y|yes$/i
95       if axe_yes("Try again?") then next else return end
96     end
97
98     usual = axe_yes "Does this source ever receive new messages?", "y"
99     archive = usual ? axe_yes("Should those new messages be automatically archived?") : false
100     
101     cmd = build_cmd "sup-add"
102     cmd += " --unusual" unless usual
103     cmd += " --archive" if archive
104     cmd += " #{uri}"
105
106     puts "Ok, trying to run \"#{cmd}\"..."
107
108     system cmd
109     if $?.success?
110       say "Great! Added!"
111       break 
112     else
113       say "Rats, that failed. You may have to do it manually."
114       if axe_yes("Try again?") then next else return end
115     end
116   end
117 end
118
119 $terminal.wrap_at = :auto
120 Redwood::start
121 index = Redwood::Index.new
122 index.load_sources
123
124 say <<EOS
125 Howdy neighbor! This here's sup-config, ready to help you jack in to
126 the next generation of digital cyberspace: the text-based email
127 program. Get ready to be the envy of everyone in your internets
128 with your amazing keyboarding skills! Jump from email to email with
129 nary a click of the mouse!
130
131 Just answer these simple questions and you'll be on your way! Press
132 enter at any point to accept the default answer.
133
134 EOS
135 #' stupid ruby-mode
136
137 account = $config[:accounts][:default]
138
139 name = axe "What's your name?", account[:name]
140 email = axe "What's your email address?", account[:email] #'srm
141
142 say "Ok, your header will look like this:"
143 say "  From: #{name} <#{email}>"
144
145 say "\nDo you have any alternate email addresses that also receive email?"
146 say "If so, enter them now, separated by spaces."
147 alts = axe("Alternate email addresses", account[:alternates].join(" ")).split(/\s+/)
148
149 sigfn = axe "What file contains your signature?", account[:signature]
150 editor = axe "What editor would you like to use?", $config[:editor]
151
152 $config[:accounts][:default][:name] = name
153 $config[:accounts][:default][:email] = email
154 $config[:accounts][:default][:alternates] = alts
155 $config[:accounts][:default][:signature] = sigfn
156 $config[:editor] = editor
157
158 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN
159
160 say "Ok, I've saved you up a nice lil' #{Redwood::CONFIG_FN}."
161
162 done = false
163 until done
164   say "\nNow, we'll tell Sup where to find all your email."
165   index.load_sources
166   say "Current sources:"
167   if index.sources.empty?
168     say "  No sources!"
169   else
170     index.sources.each { |s| puts "* #{s}" }
171   end
172
173   say "\n"
174   choose do |menu|
175     menu.prompt = "Your wish?"
176     menu.choice("Add a new source.") { add_source }
177     menu.choice("Done adding sources!") { done = true }
178   end
179 end
180
181 say <<EOS
182
183 Ok. The final step is to import all your messages into the Sup index.
184 Depending on how many messages are in the sources, this could take
185 quite a while.
186
187 IMPORTANT NOTE: this import will archive messages if the source is
188 marked archival, and won't otherwise. It will preserve read/unread
189 status as given by the source, and it will automatically add one label
190 per source. All of this behavior can be controlled on per-source
191 basis by running sup-sync manually.
192
193 EOS
194 #'
195 if axe_yes "Run sup-sync to import all messages now?"
196   while true
197     cmd = build_cmd("sup-sync") + " --all-sources"
198     puts "Ok, trying to run \"#{cmd}\"..."
199     system cmd
200     if $?.success?
201       say "Great! It worked!"
202       break 
203     else
204       say "Rats, that failed. You may have to do it manually."
205       if axe_yes("Try again?") then next else break end
206     end
207   end
208 end
209
210 index.load
211
212 say <<EOS
213
214 Okee doke, you've got yourself an index of #{index.size} messages. Looks
215 like you're ready to jack in to cyberspace there, cowboy.
216
217 Just one last command:
218
219   sup
220
221 Have fun!
222 EOS