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