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