5 class SourceError < StandardError
7 raise "don't instantiate me!" if SourceError.is_a?(self.class)
11 class OutOfSyncSourceError < SourceError; end
12 class FatalSourceError < SourceError; end
15 ## Implementing a new source should be easy, because Sup only needs
17 ## 1. See how many messages it contains
18 ## 2. Get an arbitrary message
19 ## 3. (optional) see whether the source has marked it read or not
21 ## In particular, Sup doesn't need to move messages, mark them as
22 ## read, delete them, or anything else. (Well, it's nice to be able
23 ## to delete them, but that is optional.)
25 ## On the other hand, Sup assumes that you can assign each message a
26 ## unique integer id, such that newer messages have higher ids than
27 ## earlier ones, and that those ids stay constant across sessions
28 ## (in the absence of some other client going in and fucking
29 ## everything up). For example, for mboxes I use the file offset of
30 ## the start of the message. If a source does NOT have that
31 ## capability, e.g. IMAP, then you have to do a little more work to
34 ## To write a new source, subclass this class, and implement:
37 ## - end_offset (exclusive!)
38 ## - load_header offset
39 ## - load_message offset
40 ## - raw_header offset
41 ## - raw_message offset
43 ## - next (or each, if you prefer): should return a message and an
46 ## ... where "offset" really means unique id. (You can tell I
47 ## started with mbox.)
49 ## All exceptions relating to accessing the source must be caught
50 ## and rethrown as FatalSourceErrors or OutOfSyncSourceErrors.
51 ## OutOfSyncSourceErrors should be used for problems that a call to
52 ## sup-sync will fix (namely someone's been playing with the source
53 ## from another client); FatalSourceErrors can be used for anything
54 ## else (e.g. the imap server is down or the maildir is missing.)
56 ## Finally, be sure the source is thread-safe, since it WILL be
57 ## pummelled from multiple threads at once.
59 ## Examples for you to look at: mbox/loader.rb, imap.rb, and
64 ## dirty? means cur_offset has changed, so the source info needs to
65 ## be re-saved to sources.yaml.
66 bool_reader :usual, :archived, :dirty
67 attr_reader :uri, :cur_offset
70 def initialize uri, initial_offset=nil, usual=true, archived=false, id=nil
71 raise ArgumentError, "id must be an integer: #{id.inspect}" unless id.is_a? Fixnum if id
74 @cur_offset = initial_offset
81 def file_path; nil end
83 def to_s; @uri.to_s; end
84 def seek_to! o; self.cur_offset = o; end
85 def reset!; seek_to! start_offset; end
86 def == o; o.uri == uri; end
87 def done?; start_offset.nil? || (self.cur_offset ||= start_offset) >= end_offset; end
88 def is_source_for? uri; uri == @uri; end
90 ## check should throw a FatalSourceError or an OutOfSyncSourcError
91 ## if it can detect a problem. it is called when the sup starts up
92 ## to proactively notify the user of any source problems.
96 self.cur_offset ||= start_offset
99 raise "no message" unless n
104 ## read a raw email header from a filehandle (or anything that responds to
105 ## #gets), and turn it into a hash of key-value pairs.
107 ## WARNING! THIS IS A SPEED-CRITICAL SECTION. Everything you do here will have
108 ## a significant effect on Sup's processing speed of email from ALL sources.
109 ## Little things like string interpolation, regexp interpolation, += vs <<,
110 ## all have DRAMATIC effects. BE CAREFUL WHAT YOU DO!
111 def self.parse_raw_email_header f
117 ## these three can occur multiple times, and we want the first one
118 when /^(Delivered-To|X-Original-To|Envelope-To):\s*(.*?)\s*$/i; header[last = $1.downcase] ||= $2
119 ## mark this guy specially. not sure why i care.
120 when /^([^:\s]+):\s*(.*?)\s*$/i; header[last = $1.downcase] = $2
124 header[last] << " " unless header[last].empty?
125 header[last] << line.strip
130 %w(subject from to cc bcc).each do |k|
131 v = header[k] or next
132 next unless Rfc2047.is_encoded? v
134 Rfc2047.decode_to $encoding, v
135 rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence => e
136 #Redwood::log "warning: error decoding RFC 2047 header (#{e.class.name}): #{e.message}"
145 ## convenience function
146 def parse_raw_email_header f; self.class.parse_raw_email_header f end
148 def Source.expand_filesystem_uri uri
149 uri.gsub "~", File.expand_path("~")