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