10 def initialize uri, username, password, last_uid=nil, usual=true, archived=false, id=nil
11 raise ArgumentError, "username and password must be specified" unless username && password
13 super uri, last_uid, usual, archived, id
15 @parsed_uri = URI(uri)
20 @labels << mailbox.intern unless mailbox =~ /inbox/i || mailbox.empty?
21 @labels << :inbox unless archived?
27 return false if broken?
29 Redwood::log "connecting to #{@parsed_uri.host} port #{ssl? ? 993 : 143}, ssl=#{ssl?} ..."
31 ## ok, this is FUCKING ANNOYING.
33 ## what imap.rb likes to do is, if an exception occurs, catch it
34 ## and re-raise it on the calling thread. seems reasonable. but
35 ## what that REALLY means is that the only way to reasonably
36 ## initialize imap is in its own thread, because otherwise, you
37 ## will never be able to catch the exception it raises on the
38 ## calling thread, and the backtrace will not make any sense at
39 ## all, and you will waste HOURS of your life on this fucking
45 #raise Net::IMAP::ByeResponseError, "simulated imap failure"
46 @imap = Net::IMAP.new @parsed_uri.host, ssl? ? 993 : 143, ssl?
47 @imap.authenticate 'LOGIN', @username, @password
49 Redwood::log "successfully connected to #{@parsed_uri}, mailbox #{mailbox}"
51 self.broken_msg = e.message.chomp # fucking chomp! fuck!!!
53 Redwood::log "error connecting to IMAP server: #{self.broken_msg}"
61 def mailbox; @parsed_uri.path[1..-1] end ##XXXX TODO handle nil
62 def ssl?; @parsed_uri.scheme == 'imaps' end
64 def load_header uid=nil
65 MBox::read_header StringIO.new(raw_header(uid))
69 RMail::Parser.read raw_full_message(uid)
72 ## load the full header text
74 connect or return broken_msg
76 connect or return broken_msg
78 raise "wtf: #{e.inspect}"
80 @imap.uid_fetch(uid, 'RFC822.HEADER')[0].attr['RFC822.HEADER'].gsub(/\r\n/, "\n")
83 def raw_full_message uid
84 connect or return broken_msg
85 @imap.uid_fetch(uid, 'RFC822')[0].attr['RFC822'].gsub(/\r\n/, "\n")
89 connect or return broken_msg
90 uids = @imap.uid_search ['UID', "#{cur_offset}:#{end_offset}"]
98 def start_offset; 1; end
100 connect or return start_offset
101 @imap.uid_search(['ALL']).last
105 Redwood::register_yaml(IMAP, %w(uri username password offset usual archived id))