9 $opts = Trollop::options do
10 version "sup v#{Redwood::VERSION}"
12 Sup is a curses-based email client.
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.)"
24 Redwood::HookManager.print_hooks
28 Thread.abort_on_exception = true # make debugging possible
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'
52 Ncurses.stdscr.keypad 1
59 return unless $cursing
64 module_function :start_cursing, :stop_cursing
69 rescue Index::LockError => e
74 h.say Index.fancy_lock_error_message_for(e)
76 case h.ask("Should I ask that process to kill itself? ")
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."
86 Ok, giving up. If the process crashed and left a stale lockfile, you
87 can fix this by manually deleting #{Index.lockfile}.
97 if(s = Index.source_for DraftManager.source_name)
98 DraftManager.source = s
100 Index.add_source DraftManager.new_source
103 if(s = Index.source_for SentManager.source_name)
104 SentManager.source = s
106 Index.add_source SentManager.new_source
109 log "starting curses"
112 log "initializing colormap"
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,
118 c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
120 c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
121 c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK,
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,
136 c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
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,
144 c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
146 c.add :completion_character_color, Ncurses::COLOR_WHITE,
147 Ncurses::COLOR_BLACK, Ncurses::A_BOLD
150 log "initializing buffer manager"
151 bm = BufferManager.new
153 log "initializing mail index buffer"
154 imode = InboxMode.new
155 ibuf = bm.spawn "Inbox", imode
157 log "ready for interaction!"
163 Index.usual_sources.each { |s| s.check }
165 # do nothing! we'll report it at the next step
167 Redwood::report_broken_sources
169 Index.usual_sources.each do |s|
173 rescue SourceError => e
174 Redwood::log "fatal error loading from #{s}: #{e.message}"
176 end if s.respond_to? :connect
179 imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } unless $opts[:no_threads] }
181 unless $opts[:no_threads]
184 Index.start_lock_update_thread
187 until $exception || SuicideManager.die?
188 c = Ncurses.nonblocking_getch
192 unless bm.handle_input(c)
193 x = global_keymap.action_for c
196 break if bm.kill_all_buffers_safely
198 curmode = bm.focus_buf.mode
199 bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
202 when :roll_buffers_backwards
203 bm.roll_buffers_backwards
205 bm.kill_buffer_safely bm.focus_buf
207 bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
209 b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
210 b.mode.load_in_background
212 text = bm.ask :search, "query: "
213 next unless text && text !~ /^\s*$/
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."
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?
233 BufferManager.raise_to_front InboxMode.instance.buffer
235 b = BufferManager.spawn_unless_exists("All threads with label '#{user_label}'") do
236 mode = LabelSearchResultsMode.new([user_label])
238 b.mode.load_threads :num => b.content_height
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]
246 mode = ComposeMode.new :to => to, :cc => cc, :bcc => bcc
247 bm.spawn "New Message", mode
250 reporting_thread { PollManager.poll }
252 case Index.num_results_for :label => :draft
254 bm.flash "No draft messages."
257 Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
258 r = ResumeMode.new(m)
259 BufferManager.spawn "Edit message", r
262 b = BufferManager.spawn_unless_exists("All drafts") do
263 mode = LabelSearchResultsMode.new [:draft]
265 b.mode.load_threads :num => b.content_height
269 bm.completely_redraw_screen
271 bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
277 rescue Exception => e
280 unless $opts[:no_threads]
281 PollManager.stop if PollManager.instantiated?
282 SuicideManager.stop if PollManager.instantiated?
283 Index.stop_lock_update_thread
288 Redwood::log "stopped cursing"
290 if SuicideManager.instantiated? && SuicideManager.die?
291 Redwood::log "I've been ordered to commit sepuku. I obey!"
296 Redwood::log "no fatal errors. good job, william."
299 Redwood::log "oh crap, an exception"
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
316 ----------------------------------------------------------------
318 The problem was: '#{$exception.message}' (error type #{$exception.class.name})