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