bin/sup-config (6765B) - raw
1 #!/usr/bin/env ruby
2
3 $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
5 require 'optimist'
6 require "sup"
7 require 'sup/util/axe'
8
9 $opts = Optimist::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 No options.
19 EOS
20 end
21
22 def build_cmd cmd
23 (ENV["RUBY_INVOCATION"] ? ENV["RUBY_INVOCATION"] + " " : "") + File.join(File.dirname($0), cmd)
24 end
25
26 def add_source
27 require "sup/util/uri"
28
29 type = nil
30
31 @cli.say "Ok, adding a new source."
32 @cli.choose do |menu|
33 menu.prompt = "What type of mail source is it? "
34 menu.choice("mbox file") { type = :mbox }
35 menu.choice("maildir directory") { type = :maildir }
36 menu.choice("Get me out of here!") { return }
37 end
38
39 while true do
40 @cli.say "Ok, now for the details."
41
42 default_labels, components = case type
43 when :mbox
44 $last_fn ||= ENV["MAIL"]
45 fn = axe "What's the full path to the mbox file?", $last_fn
46 return if fn.nil? || fn.empty?
47
48 $last_fn = fn
49 [Redwood::MBox.suggest_labels_for(fn),
50 { :scheme => "mbox", :path => fn }]
51 when :maildir
52 $last_fn ||= ENV["MAIL"]
53 fn = axe "What's the full path to the maildir directory?", $last_fn
54 return if fn.nil? || fn.empty?
55
56 $last_fn = fn
57 [Redwood::Maildir.suggest_labels_for(fn),
58 { :scheme => "maildir", :path => fn }]
59 end
60
61 uri = begin
62 Redwood::Util::Uri.build components
63 rescue URI::Error => e
64 @cli.say "Whoopsie! I couldn't build a URI from that: #{e.message}"
65 if axe_yes("Try again?") then next else return end
66 end
67
68 @cli.say "I'm going to add this source: #{uri}"
69 unless axe("Does that look right?", "y") =~ /^y|yes$/i
70 if axe_yes("Try again?") then next else return end
71 end
72
73 usual = axe_yes "Does this source ever receive new messages?", "y"
74 archive = usual ? axe_yes("Should new messages be automatically archived? (I.e. not appear in your inbox, though still be accessible via search.)") : false
75
76 sync_back = (type == :maildir) ? axe_yes("Should the original Maildir messages be modified to reflect changes like read status, starred messages, etc.?", "y") : false
77
78 labels_str = axe("Enter any labels to be automatically added to all messages from this source, separated by spaces (or 'none')", default_labels.join(","))
79
80 labels = if labels_str =~ /^\s*none\s*$/i
81 nil
82 else
83 labels_str.split(/\s+/)
84 end
85
86 cmd = build_cmd "sup-add"
87 cmd += " --unusual" unless usual
88 cmd += " --archive" if archive
89 cmd += " --no-sync-back" unless sync_back
90 cmd += " --labels=#{labels.join(',')}" if labels && !labels.empty?
91 cmd += " #{uri}"
92
93 puts "Ok, trying to run \"#{cmd}\"..."
94
95 system cmd
96 if $?.success?
97 @cli.say "Great! Added!"
98 break
99 else
100 @cli.say "Rats, that failed. You may have to do it manually."
101 if axe_yes("Try again?") then next else return end
102 end
103 end
104 end
105
106 @cli.wrap_at = :auto
107 Redwood::start
108 index = Redwood::Index.init
109 Redwood::SourceManager.load_sources
110
111 @cli.say <<EOS
112 Howdy neighbor! This here's sup-config, ready to help you jack in to
113 the next generation of digital cyberspace: the text-based email
114 program. Get ready to be the envy of everyone in your internets
115 with your amazing keyboarding skills! Jump from email to email with
116 nary a click of the mouse!
117
118 Just answer these simple questions and you'll be on your way.
119
120 EOS
121
122 account = $config[:accounts][:default]
123
124 name = axe "What's your name?", account[:name]
125 email = axe "What's your (primary) email address?", account[:email]
126
127 @cli.say "Ok, your from header will look like this:"
128 @cli.say " From: #{name} <#{email}>"
129
130 @cli.say "\nDo you have any alternate email addresses that also receive email?"
131 @cli.say "If so, enter them now, separated by spaces."
132 alts = axe("Alternate email addresses", account[:alternates].join(" ")).split(/\s+/)
133
134 sigfn = axe "What file contains your signature?", account[:signature]
135 editor = axe "What editor would you like to use?", $config[:editor]
136
137 time_mode = axe "Would like to display time in 12h (type 12h) or in 24h (type 24h)?", $config[:time_mode]
138
139 $config[:accounts][:default][:name] = name
140 $config[:accounts][:default][:email] = email
141 $config[:accounts][:default][:alternates] = alts
142 $config[:accounts][:default][:signature] = sigfn
143 $config[:editor] = editor
144 $config[:time_mode] = time_mode
145
146 done = false
147 until done
148 @cli.say "\nNow, we'll tell Sup where to find all your email."
149 Redwood::SourceManager.load_sources
150 @cli.say "Current sources:"
151 if Redwood::SourceManager.sources.empty?
152 @cli.say " No sources!"
153 else
154 Redwood::SourceManager.sources.each { |s| puts "* #{s}" }
155 end
156
157 @cli.say "\n"
158 @cli.choose do |menu|
159 menu.prompt = "Your wish? "
160 menu.choice("Add a new source.") { add_source }
161 menu.choice("Done adding sources!") { done = true }
162 end
163 end
164
165 @cli.say "\nSup needs to know where to store your sent messages."
166 @cli.say "Only sources capable of storing mail will be listed.\n\n"
167
168 Redwood::SourceManager.load_sources
169 if Redwood::SourceManager.sources.empty?
170 @cli.say "\nUsing the default sup://sent, since you haven't configured other sources yet."
171 $config[:sent_source] = 'sup://sent'
172 else
173 # this handles the event that source.yaml already contains the SentLoader
174 # source.
175 have_sup_sent = false
176
177 @cli.choose do |menu|
178 menu.prompt = "Store my sent mail in? "
179
180 menu.choice('Default (an mbox in ~/.sup, aka sup://sent)') { $config[:sent_source] = 'sup://sent'} unless have_sup_sent
181
182 valid_sents = Redwood::SourceManager.sources.each do |s|
183 have_sup_sent = true if s.to_s.eql?('sup://sent')
184 menu.choice(s.to_s) { $config[:sent_source] = s.to_s } if s.respond_to? :store_message
185 end
186 end
187 end
188
189 Redwood::save_yaml_obj $config, Redwood::CONFIG_FN, false, true
190
191 @cli.say "Ok, I've saved you up a nice lil' #{Redwood::CONFIG_FN}."
192
193 @cli.say <<EOS
194
195 The final step is to import all your messages into the Sup index.
196 Depending on how many messages are in the sources, this could take
197 quite a while.
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 @cli.say "Great! It worked!"
208 break
209 else
210 @cli.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 @cli.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 #{build_cmd "sup"}
226
227 Have fun!
228 EOS