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