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