Module: EventMachine::IMAP::ResponseParser

Included in:
Connection
Defined in:
lib/em-imap/response_parser.rb

Overview

Intercepts the receive_data event and generates receive_response events with parsed data.

Instance Method Summary collapse

Instance Method Details

#post_initObject


6
7
8
9
10
# File 'lib/em-imap/response_parser.rb', line 6

def post_init
  super
  @parser = Net::IMAP::ResponseParser.new
  @buffer = ""
end

#receive_data(data) ⇒ Object

This is a translation of Net::IMAP#get_response


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/em-imap/response_parser.rb', line 13

def receive_data(data)
  @buffer << data

  until @buffer.empty?

    eol = @buffer.index(CRLF)
    
    # Include IMAP literals on the same line.
    # The format for a literal is "{8}\r\n........"
    # so the size would be at the end of what we thought was the line.
    # We then skip over that much, and try looking for the next newline.
    # (The newline after a literal is the end of the actual line,
    # there's no termination marker for literals).
    while eol && @buffer[0, eol][/\{(\d+)\}\z/]
      eol = @buffer.index(CRLF, eol + CRLF.size + $1.to_i)
    end

    # The current line is not yet complete, wait for more data.
    return unless eol

    line = @buffer.slice!(0, eol + CRLF.size)

    receive_response parse(line)
  end
end

#receive_response(response) ⇒ Object

Callback used by receive data.


40
# File 'lib/em-imap/response_parser.rb', line 40

def receive_response(response); end