]> git.cworth.org Git - sup/blob - bin/sup
Merge branch 'master' into next
[sup] / bin / sup
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'ncurses'
5 require 'curses'
6 require 'fileutils'
7 require 'trollop'
8 require "sup"
9
10 BIN_VERSION = "git"
11
12 unless Redwood::VERSION == BIN_VERSION
13   $stderr.puts <<EOS
14
15 Error: version mismatch!
16 The sup executable is at version #{BIN_VERSION.inspect}.
17 The sup libraries are at version #{Redwood::VERSION.inspect}.
18
19 Is your development environment conflicting with rubygems?
20 EOS
21   exit(-1)
22 end
23
24 $exceptions = []
25 $opts = Trollop::options do
26   version "sup v#{Redwood::VERSION}"
27   banner <<EOS
28 Sup is a curses-based email client.
29
30 Usage:
31   sup [options]
32
33 Options are:
34 EOS
35   opt :list_hooks, "List all hooks and descriptions, and quit."
36   opt :no_threads, "Turn off threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
37   opt :no_initial_poll, "Don't poll for new messages when starting."
38   opt :search, "Search for this query upon startup", :type => String
39   opt :compose, "Compose message to this recipient upon startup", :type => String
40 end
41
42 if $opts[:list_hooks]
43   Redwood::HookManager.print_hooks
44   exit
45 end
46
47 Thread.abort_on_exception = true # make debugging possible
48
49 module Redwood
50
51 global_keymap = Keymap.new do |k|
52   k.add :quit, "Quit Redwood", 'q'
53   k.add :help, "Show help", 'H', '?'
54   k.add :roll_buffers, "Switch to next buffer", 'b'
55 #  k.add :roll_buffers_backwards, "Switch to previous buffer", 'B'
56   k.add :kill_buffer, "Kill the current buffer", 'x'
57   k.add :list_buffers, "List all buffers", 'B'
58   k.add :list_contacts, "List contacts", 'C'
59   k.add :redraw, "Redraw screen", :ctrl_l
60   k.add :search, "Search all messages", '\\', 'F'
61   k.add :list_labels, "List labels", 'L'
62   k.add :poll, "Poll for new messages", 'P'
63   k.add :compose, "Compose new message", 'm', 'c'
64   k.add :nothing, "Do nothing", :ctrl_g
65   k.add :recall_draft, "Edit most recent draft message", 'R'
66 end
67
68 def start_cursing
69   Ncurses.initscr
70   Ncurses.noecho
71   Ncurses.cbreak
72   Ncurses.stdscr.keypad 1
73   Ncurses.curs_set 0
74   Ncurses.start_color
75   $cursing = true
76 end
77
78 def stop_cursing
79   return unless $cursing
80   Ncurses.curs_set 1
81   Ncurses.echo
82   Ncurses.endwin
83 end
84 module_function :start_cursing, :stop_cursing
85
86 Index.new
87 begin
88   Index.lock
89 rescue Index::LockError => e
90   require 'highline'
91
92   h = HighLine.new
93   h.wrap_at = :auto
94   h.say Index.fancy_lock_error_message_for(e)
95
96   case h.ask("Should I ask that process to kill itself? ")
97   when /^\s*y\s*$/i
98     h.say "Ok, suggesting seppuku..."
99     FileUtils.touch Redwood::SUICIDE_FN
100     sleep SuicideManager::DELAY * 2
101     FileUtils.rm_f Redwood::SUICIDE_FN
102     h.say "Let's try that again."
103     retry
104   else
105     h.say <<EOS
106 Ok, giving up. If the process crashed and left a stale lockfile, you
107 can fix this by manually deleting #{Index.lockfile}.
108 EOS
109     exit
110   end
111 end
112
113 begin
114   Redwood::start
115   Index.load
116
117   if(s = Index.source_for DraftManager.source_name)
118     DraftManager.source = s
119   else
120     Redwood::log "no draft source, auto-adding..."
121     Index.add_source DraftManager.new_source
122   end
123
124   if(s = Index.source_for SentManager.source_name)
125     SentManager.source = s
126   else
127     Redwood::log "no sent mail source, auto-adding..."
128     Index.add_source SentManager.new_source
129   end
130
131   log "starting curses"
132   start_cursing
133
134   Colormap.new do |c|
135     c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
136     c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
137     c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
138            Ncurses::A_BOLD
139     c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
140            Ncurses::A_BOLD
141     c.add :index_draft_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
142            Ncurses::A_BOLD
143     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
144     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
145            Ncurses::A_BOLD
146     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
147     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
148     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
149     c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
150     c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
151     c.add :attachment_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
152     c.add :cryptosig_valid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
153     c.add :cryptosig_unknown_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
154     c.add :cryptosig_invalid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_RED, Ncurses::A_BOLD
155     c.add :generic_notice_patina_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
156     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
157     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
158     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
159     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
160     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
161     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
162           Ncurses::A_BOLD
163     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
164           Ncurses::A_BOLD
165     c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
166           Ncurses::COLOR_BLUE, Ncurses::A_BOLD
167     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
168     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
169     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
170           Ncurses::A_BOLD
171     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
172           Ncurses::A_BOLD
173     c.add :completion_character_color, Ncurses::COLOR_WHITE,
174           Ncurses::COLOR_BLACK, Ncurses::A_BOLD
175     c.add :horizontal_selector_selected_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
176     c.add :horizontal_selector_unselected_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
177     c.add :search_highlight_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_YELLOW, Ncurses::A_BOLD, :highlight => :search_highlight_color
178   end
179
180   bm = BufferManager.new
181
182   log "initializing mail index buffer"
183   imode = InboxMode.new
184   ibuf = bm.spawn "Inbox", imode
185
186   log "ready for interaction!"
187   Logger.make_buf
188
189   bm.draw_screen
190
191   Index.usual_sources.each do |s|
192     next unless s.respond_to? :connect
193     reporting_thread("call #connect on #{s}") do
194       begin
195         s.connect
196       rescue SourceError => e
197         Redwood::log "fatal error loading from #{s}: #{e.message}"
198       end
199     end
200   end unless $opts[:no_initial_poll]
201   
202   imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread("poll after loading inbox") { sleep 1; PollManager.poll } unless $opts[:no_threads] || $opts[:no_initial_poll] }
203
204   if $opts[:compose]
205     ComposeMode.spawn_nicely :to_default => $opts[:compose]
206   end
207
208   unless $opts[:no_threads]
209     PollManager.start
210     SuicideManager.start
211     Index.start_lock_update_thread
212   end
213
214   if $opts[:search]
215     SearchResultsMode.spawn_from_query $opts[:search]
216   end
217
218   until $exceptions.nonempty? || SuicideManager.die?
219     c = Ncurses.nonblocking_getch
220     next unless c
221     bm.erase_flash
222
223     action =
224       begin
225         if bm.handle_input c
226           :nothing
227         else
228           bm.resolve_input_with_keymap c, global_keymap
229         end
230       rescue InputSequenceAborted
231         :nothing
232       end
233
234     case action
235     when :quit
236       break if bm.kill_all_buffers_safely
237     when :help
238       curmode = bm.focus_buf.mode
239       bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
240     when :roll_buffers
241       bm.roll_buffers
242     when :roll_buffers_backwards
243       bm.roll_buffers_backwards
244     when :kill_buffer
245       bm.kill_buffer_safely bm.focus_buf
246     when :list_buffers
247       bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
248     when :list_contacts
249       b, new = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
250       b.mode.load_in_background if new
251     when :search
252       query = BufferManager.ask :search, "search all messages: "
253       next unless query && query !~ /^\s*$/
254       SearchResultsMode.spawn_from_query query
255     when :list_labels
256       labels = LabelManager.listable_labels.map { |l| LabelManager.string_for l }
257       user_label = bm.ask_with_completions :label, "Show threads with label (enter for listing): ", labels
258       unless user_label.nil?
259         if user_label.empty?
260           bm.spawn_unless_exists("Label list") { LabelListMode.new } if user_label && user_label.empty?
261         else
262           LabelSearchResultsMode.spawn_nicely user_label
263         end
264       end
265     when :compose
266       ComposeMode.spawn_nicely
267     when :poll
268       reporting_thread("user-invoked poll") { PollManager.poll }
269     when :recall_draft
270       case Index.num_results_for :label => :draft
271       when 0
272         bm.flash "No draft messages."
273       when 1
274         m = nil
275         Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
276         r = ResumeMode.new(m)
277         BufferManager.spawn "Edit message", r
278         r.edit_message
279       else
280         b, new = BufferManager.spawn_unless_exists("All drafts") { LabelSearchResultsMode.new [:draft] }
281         b.mode.load_threads :num => b.content_height if new
282       end
283     when :nothing, InputSequenceAborted
284     when :redraw
285       bm.completely_redraw_screen
286     else
287       bm.flash "Unknown keypress '#{c.to_character}' for #{bm.focus_buf.mode.name}."
288     end
289
290     bm.draw_screen
291   end
292
293   bm.kill_all_buffers if SuicideManager.die?
294 rescue Exception => e
295   $exceptions << [e, "main"]
296 ensure
297   unless $opts[:no_threads]
298     PollManager.stop if PollManager.instantiated?
299     SuicideManager.stop if PollManager.instantiated?
300     Index.stop_lock_update_thread
301   end
302
303   Redwood::finish
304   stop_cursing
305   Redwood::log "stopped cursing"
306
307   if SuicideManager.instantiated? && SuicideManager.die?
308     Redwood::log "I've been ordered to commit seppuku. I obey!"
309   end
310
311   if $exceptions.empty?
312     Redwood::log "no fatal errors. good job, william."
313     Index.save
314   else
315     Redwood::log "oh crap, an exception"
316   end
317
318   Index.unlock
319 end
320
321 unless $exceptions.empty?
322   File.open("sup-exception-log.txt", "w") do |f|
323     $exceptions.each do |e, name|
324       f.puts "--- #{e.class.name} from thread: #{name}"
325       f.puts e.message, e.backtrace
326     end
327   end
328   $stderr.puts <<EOS
329 ----------------------------------------------------------------
330 I'm very sorry. It seems that an error occurred in Sup. Please
331 accept my sincere apologies. If you don't mind, please send the
332 contents of sup-exception-log.txt and a brief report of the
333 circumstances to sup-talk at rubyforge dot orgs so that I might
334 address this problem. Thank you!
335
336 Sincerely,
337 William
338 ----------------------------------------------------------------
339 EOS
340   $exceptions.each do |e, name|
341     puts "--- #{e.class.name} from thread: #{name}"
342     puts e.message, e.backtrace
343   end
344 end
345
346 end