]> git.cworth.org Git - sup/blob - bin/sup
Merge branch 'master' of git+ssh://wmorgan@repo.or.cz/srv/git/sup
[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 :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
142     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
143            Ncurses::A_BOLD
144     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
145     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
146     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
147     c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
148     c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
149     c.add :attachment_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
150     c.add :cryptosig_valid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
151     c.add :cryptosig_unknown_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
152     c.add :cryptosig_invalid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_RED, Ncurses::A_BOLD
153     c.add :generic_notice_patina_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
154     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
155     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
156     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
157     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
158     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
159     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
160           Ncurses::A_BOLD
161     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
162           Ncurses::A_BOLD
163     c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
164           Ncurses::COLOR_BLUE, Ncurses::A_BOLD
165     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
166     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
167     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
168           Ncurses::A_BOLD
169     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
170           Ncurses::A_BOLD
171     c.add :completion_character_color, Ncurses::COLOR_WHITE,
172           Ncurses::COLOR_BLACK, Ncurses::A_BOLD
173     c.add :horizontal_selector_selected_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
174     c.add :horizontal_selector_unselected_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
175     c.add :search_highlight_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_YELLOW, Ncurses::A_BOLD, :highlight => :search_highlight_color
176   end
177
178   bm = BufferManager.new
179
180   log "initializing mail index buffer"
181   imode = InboxMode.new
182   ibuf = bm.spawn "Inbox", imode
183
184   log "ready for interaction!"
185   Logger.make_buf
186
187   bm.draw_screen
188
189   Index.usual_sources.each do |s|
190     next unless s.respond_to? :connect
191     reporting_thread("call #connect on #{s}") do
192       begin
193         s.connect
194       rescue SourceError => e
195         Redwood::log "fatal error loading from #{s}: #{e.message}"
196       end
197     end
198   end unless $opts[:no_initial_poll]
199   
200   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] }
201
202   if $opts[:compose]
203     ComposeMode.spawn_nicely :to_default => $opts[:compose]
204   end
205
206   unless $opts[:no_threads]
207     PollManager.start
208     SuicideManager.start
209     Index.start_lock_update_thread
210   end
211
212   if $opts[:search]
213     SearchResultsMode.spawn_from_query $opts[:search]
214   end
215
216   until $exceptions.nonempty? || SuicideManager.die?
217     c = Ncurses.nonblocking_getch
218     next unless c
219     bm.erase_flash
220
221     action =
222       begin
223         if bm.handle_input c
224           :nothing
225         else
226           bm.resolve_input_with_keymap c, global_keymap
227         end
228       rescue InputSequenceAborted
229         :nothing
230       end
231
232     case action
233     when :quit
234       break if bm.kill_all_buffers_safely
235     when :help
236       curmode = bm.focus_buf.mode
237       bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
238     when :roll_buffers
239       bm.roll_buffers
240     when :roll_buffers_backwards
241       bm.roll_buffers_backwards
242     when :kill_buffer
243       bm.kill_buffer_safely bm.focus_buf
244     when :list_buffers
245       bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
246     when :list_contacts
247       b, new = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
248       b.mode.load_in_background if new
249     when :search
250       query = BufferManager.ask :search, "search all messages: "
251       next unless query && query !~ /^\s*$/
252       SearchResultsMode.spawn_from_query query
253     when :list_labels
254       labels = LabelManager.listable_labels.map { |l| LabelManager.string_for l }
255       user_label = bm.ask_with_completions :label, "Show threads with label (enter for listing): ", labels
256       unless user_label.nil?
257         if user_label.empty?
258           bm.spawn_unless_exists("Label list") { LabelListMode.new } if user_label && user_label.empty?
259         else
260           LabelSearchResultsMode.spawn_nicely user_label
261         end
262       end
263     when :compose
264       ComposeMode.spawn_nicely
265     when :poll
266       reporting_thread("user-invoked poll") { PollManager.poll }
267     when :recall_draft
268       case Index.num_results_for :label => :draft
269       when 0
270         bm.flash "No draft messages."
271       when 1
272         m = nil
273         Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
274         r = ResumeMode.new(m)
275         BufferManager.spawn "Edit message", r
276         r.edit_message
277       else
278         b, new = BufferManager.spawn_unless_exists("All drafts") { LabelSearchResultsMode.new [:draft] }
279         b.mode.load_threads :num => b.content_height if new
280       end
281     when :nothing, InputSequenceAborted
282     when :redraw
283       bm.completely_redraw_screen
284     else
285       bm.flash "Unknown keypress '#{c.to_character}' for #{bm.focus_buf.mode.name}."
286     end
287
288     bm.draw_screen
289   end
290
291   bm.kill_all_buffers if SuicideManager.die?
292 rescue Exception => e
293   $exceptions << [e, "main"]
294 ensure
295   unless $opts[:no_threads]
296     PollManager.stop if PollManager.instantiated?
297     SuicideManager.stop if PollManager.instantiated?
298     Index.stop_lock_update_thread
299   end
300
301   Redwood::finish
302   stop_cursing
303   Redwood::log "stopped cursing"
304
305   if SuicideManager.instantiated? && SuicideManager.die?
306     Redwood::log "I've been ordered to commit seppuku. I obey!"
307   end
308
309   if $exceptions.empty?
310     Redwood::log "no fatal errors. good job, william."
311     Index.save
312   else
313     Redwood::log "oh crap, an exception"
314   end
315
316   Index.unlock
317 end
318
319 unless $exceptions.empty?
320   File.open("sup-exception-log.txt", "w") do |f|
321     $exceptions.each do |e, name|
322       f.puts "--- #{e.class.name} from thread: #{name}"
323       f.puts e.message, e.backtrace
324     end
325   end
326   $stderr.puts <<EOS
327 ----------------------------------------------------------------
328 I'm very sorry. It seems that an error occurred in Sup. Please
329 accept my sincere apologies. If you don't mind, please send the
330 contents of sup-exception-log.txt and a brief report of the
331 circumstances to sup-talk at rubyforge dot orgs so that I might
332 address this problem. Thank you!
333
334 Sincerely,
335 William
336 ----------------------------------------------------------------
337 EOS
338   $exceptions.each do |e, name|
339     puts "--- #{e.class.name} from thread: #{name}"
340     puts e.message, e.backtrace
341   end
342 end
343
344 end