9 $opts = Trollop::options do
10 version "sup v#{Redwood::VERSION}"
12 Sup is a curses-based email client.
19 opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
22 Thread.abort_on_exception = true # make debugging possible
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'
46 Ncurses.stdscr.keypad 1
53 return unless $cursing
58 module_function :start_cursing, :stop_cursing
63 rescue Index::LockError => e
68 h.say Index.fancy_lock_error_message_for(e)
70 case h.ask("Should I ask that process to kill itself? ")
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."
80 Ok, giving up. If the process crashed and left a stale lockfile, you
81 can fix this by manually deleting #{Index.lockfile}.
91 if(s = Index.source_for DraftManager.source_name)
92 DraftManager.source = s
94 Index.add_source DraftManager.new_source
97 if(s = Index.source_for SentManager.source_name)
98 SentManager.source = s
100 Index.add_source SentManager.new_source
103 log "starting curses"
106 log "initializing colormap"
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,
112 c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
114 c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
115 c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK,
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 :attachment_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,
130 c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
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,
138 c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
140 c.add :completion_character_color, Ncurses::COLOR_WHITE,
141 Ncurses::COLOR_BLACK, Ncurses::A_BOLD
144 log "initializing buffer manager"
145 bm = BufferManager.new
147 log "initializing mail index buffer"
148 imode = InboxMode.new
149 ibuf = bm.spawn "Inbox", imode
151 log "ready for interaction!"
157 Index.usual_sources.each { |s| s.check }
159 # do nothing! we'll report it at the next step
161 Redwood::report_broken_sources
163 Index.usual_sources.each do |s|
167 rescue SourceError => e
168 Redwood::log "fatal error loading from #{s}: #{e.message}"
170 end if s.respond_to? :connect
173 imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } unless $opts[:no_threads] }
175 unless $opts[:no_threads]
178 Index.start_lock_update_thread
181 until $exception || SuicideManager.die?
182 c = Ncurses.nonblocking_getch
186 unless bm.handle_input(c)
187 x = global_keymap.action_for c
190 break if bm.kill_all_buffers_safely
192 curmode = bm.focus_buf.mode
193 bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
196 when :roll_buffers_backwards
197 bm.roll_buffers_backwards
199 bm.kill_buffer_safely bm.focus_buf
201 bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
203 b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
204 b.mode.load_in_background
206 text = bm.ask :search, "query: "
207 next unless text && text !~ /^\s*$/
210 qobj = Index.parse_user_query_string text
211 short_text = text.length < 20 ? text : text[0 ... 20] + "..."
212 log "built query from #{text.inspect}: #{qobj}"
213 mode = SearchResultsMode.new qobj
214 bm.spawn "search: \"#{short_text}\"", mode
215 mode.load_threads :num => mode.buffer.content_height
216 rescue Ferret::QueryParser::QueryParseException => e
217 bm.flash "Couldn't parse query."
220 labels = LabelManager.listable_labels.map { |l| LabelManager.string_for l }
221 user_label = bm.ask_with_completions :label, "Show threads with label (enter for listing): ", labels
222 user_label = bm.spawn_modal("Label list", LabelListMode.new) if user_label && user_label.empty?
227 BufferManager.raise_to_front InboxMode.instance.buffer
229 b = BufferManager.spawn_unless_exists("All threads with label '#{user_label}'") do
230 mode = LabelSearchResultsMode.new([user_label])
232 b.mode.load_threads :num => b.content_height
236 to = BufferManager.ask_for_contacts(:people, "To: ") or next
237 cc = BufferManager.ask_for_contacts(:people, "Cc: ") or next if $config[:ask_for_cc]
238 bcc = BufferManager.ask_for_contacts(:people, "Bcc: ") or next if $config[:ask_for_bcc]
240 mode = ComposeMode.new :to => to, :cc => cc, :bcc => bcc
241 bm.spawn "New Message", mode
244 # bm.raise_to_front PollManager.buffer
245 reporting_thread { PollManager.poll }
247 case Index.num_results_for :label => :draft
249 bm.flash "No draft messages."
252 Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
253 r = ResumeMode.new(m)
254 BufferManager.spawn "Edit message", r
257 b = BufferManager.spawn_unless_exists("All drafts") do
258 mode = LabelSearchResultsMode.new [:draft]
260 b.mode.load_threads :num => b.content_height
264 bm.completely_redraw_screen
266 bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
272 rescue Exception => e
275 unless $opts[:no_threads]
276 PollManager.stop if PollManager.instantiated?
277 SuicideManager.stop if PollManager.instantiated?
278 Index.stop_lock_update_thread
283 Redwood::log "stopped cursing"
285 if SuicideManager.instantiated? && SuicideManager.die?
286 Redwood::log "I've been ordered to commit sepuku. I obey!"
291 Redwood::log "no fatal errors. good job, william."
294 Redwood::log "oh crap, an exception"
302 ----------------------------------------------------------------
303 I'm very sorry, but it seems that an error occurred in Sup.
304 Please accept my sincere apologies. If you don't mind, please
305 send the backtrace below and a brief report of the circumstances
306 to sup-talk at rubyforge dot orgs so that I might address this
311 ----------------------------------------------------------------
313 The problem was: '#{$exception.message}' (error type #{$exception.class.name})