Class: Hermeneutics::Cli::IMAP

Inherits:
Protocol
  • Object
show all
Includes:
ImapTools
Defined in:
lib/hermeneutics/cli/imap.rb

Defined Under Namespace

Classes: Error, NotOk, ServerBye, ServerError, UnspecResponse

Constant Summary collapse

CRLF =
true
TAG_PREFIX =
"H"

Instance Attribute Summary collapse

Attributes inherited from Protocol

#timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Protocol

#done?, #read, #trace!, #write, #writeline

Constructor Details

#initialize(*args) ⇒ IMAP

Returns a new instance of IMAP.



38
39
40
41
42
43
# File 'lib/hermeneutics/cli/imap.rb', line 38

def initialize *args
  super
  @tag = "H%04d" % 0
  @info = [ get_response]
  start_watch
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



45
46
47
# File 'lib/hermeneutics/cli/imap.rb', line 45

def info
  @info
end

Class Method Details

.open(host, port = nil, timeout: nil, ssl: false) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/hermeneutics/cli/imap.rb', line 27

def open host, port = nil, timeout: nil, ssl: false
  port ||= ssl ? PORT_SSL : PORT
  super host, port, timeout: timeout, ssl: ssl do |i|
    yield i
    i.stop_watch
  end
end

Instance Method Details

#auths(data = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/hermeneutics/cli/imap.rb', line 47

def auths data = nil
  a = []
  (data||@info.first.data).params.each { |p|
    p =~ /\AAUTH=/ and a.push $'.to_s
  }
  a
end

#command(cmd, *args, &block) ⇒ Object



55
56
57
58
59
60
# File 'lib/hermeneutics/cli/imap.rb', line 55

def command cmd, *args, &block
  c = cmd.new *args
  r = write_request c, &block
  r.ok? or raise NotOk, r.text
  c.responses
end

#get_responseObject



83
84
85
86
87
# File 'lib/hermeneutics/cli/imap.rb', line 83

def get_response
  r = get_response_plain
  r.bye? and raise ServerBye, "Server closed the connection"
  r
end

#get_response_plainObject



79
80
81
# File 'lib/hermeneutics/cli/imap.rb', line 79

def get_response_plain
  Response.create @tag, self
end

#peeklineObject



68
69
70
# File 'lib/hermeneutics/cli/imap.rb', line 68

def peekline
  @peek ||= readline!
end

#readlineObject



72
73
74
75
76
# File 'lib/hermeneutics/cli/imap.rb', line 72

def readline
  @peek or readline!
ensure
  @peek = nil
end

#start_watchObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/hermeneutics/cli/imap.rb', line 90

def start_watch
  @watch = Thread.new do
    Thread.current.abort_on_exception = true
    Thread.current.report_on_exception = false
    while @socket.wait do
      r = get_response
      @info.push r
    end
  end
end

#stop_watchObject



101
102
103
104
105
106
# File 'lib/hermeneutics/cli/imap.rb', line 101

def stop_watch
  @watch or return
  @watch.kill if @watch.alive?
  @watch.value
  @watch = nil
end

#write_request(cmd) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hermeneutics/cli/imap.rb', line 108

def write_request cmd
  stop_watch
  @tag.succ!
  r = nil
  cmd.stream_lines "#@tag" do |a|
    a.each { |l| writeline l.to_s }
    r = get_response_plain
    r.wait? or break
    if block_given? then  # does only make sense for the IDLE command
      begin
        start_watch
        yield
      ensure
        stop_watch
      end
    end
    r.text
  end
  until r.done? do
    bye ||= r.bye?
    cmd.add_response r
    r = get_response_plain
    r or raise ServerError, cmd.responses.last.to_s
  end
  bye or start_watch
  r
end