Class: Hermeneutics::Cli::POP3

Inherits:
Protocol
  • Object
show all
Defined in:
lib/hermeneutics/cli/pop3.rb

Defined Under Namespace

Classes: AuthFail, Check, UnspecError, Unused

Constant Summary collapse

CRLF =
true

Instance Attribute Summary collapse

Attributes inherited from Protocol

#timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Protocol

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

Constructor Details

#initialize(*args) ⇒ POP3

Returns a new instance of POP3.



32
33
34
35
# File 'lib/hermeneutics/cli/pop3.rb', line 32

def initialize *args
  super
  @stamp = get_response.slice /<[!-~]+@[!-~]+>/
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



30
31
32
# File 'lib/hermeneutics/cli/pop3.rb', line 30

def last_response
  @last_response
end

Class Method Details

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



24
25
26
27
# File 'lib/hermeneutics/cli/pop3.rb', line 24

def open host, port = nil, timeout: nil, ssl: false
  port ||= ssl ? PORT_SSL : PORT
  super host, port, timeout: timeout, ssl: ssl
end

Instance Method Details

#apop(name, pwd) ⇒ Object



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

def apop name, pwd
  require "digest/md5"
  hash = Digest::MD5.hexdigest "#@stamp#{pwd}"
  writeline "APOP #{name} #{hash}"
  get_response_auth
end

#authenticate(name, pwd) ⇒ Object



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

def authenticate name, pwd
  if @stamp then
    apop name, pwd
  else
    user name
    pass pwd
  end
end

#capaObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hermeneutics/cli/pop3.rb', line 63

def capa
  if block_given? then
    writeline "CAPA"
    get_response do |_|
      get_data { |l|
        c, *rest = l.split
        yield c, rest
      }
    end
  else
    r = Hash.new do |h,k| h[k] = [] end
    capa do |c,v|
      if v.notempty? then
        r[c].concat v
      else
        r[c] = true
      end
    end
    r
  end
end

#dele(n) ⇒ Object



172
173
174
175
# File 'lib/hermeneutics/cli/pop3.rb', line 172

def dele n
  writeline "DELE #{n}"
  get_response
end

#list(n = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hermeneutics/cli/pop3.rb', line 95

def list n = nil
  n = n.to_i.nonzero?
  if block_given? then
    cmd = "LIST"
    cmd << " #{n}" if n
    writeline cmd
    if n then
      n_, len = split_num_len get_response
      n == n_ or raise Check, "Wrong LIST response: #{n} <-> #{n_}"
      yield n, len
    else
      get_response do |_|
        get_data do |l|
          n_, len = split_num_len l
          yield n_, len
        end
      end
    end
  else
    if n then
      list n do |*a| a end
    else
      h = {}
      list n do |n_,len|
        h[ n_] = len
      end
      h
    end
  end
end

#noopObject



182
183
184
185
# File 'lib/hermeneutics/cli/pop3.rb', line 182

def noop
  writeline "NOOP"
  get_response
end

#pass(pwd) ⇒ Object



51
52
53
54
# File 'lib/hermeneutics/cli/pop3.rb', line 51

def pass pwd
  writeline "PASS #{pwd}"
  get_response_auth
end

#quitObject



187
188
189
190
# File 'lib/hermeneutics/cli/pop3.rb', line 187

def quit
  writeline "QUIT"
  get_response
end

#retr(n, &block) ⇒ Object



157
158
159
160
161
162
# File 'lib/hermeneutics/cli/pop3.rb', line 157

def retr n, &block
  writeline "RETR #{n}"
  get_response do |_|
    get_data_str &block
  end
end

#rsetObject



177
178
179
180
# File 'lib/hermeneutics/cli/pop3.rb', line 177

def rset
  writeline "RSET"
  get_response
end

#statObject



85
86
87
88
89
90
91
92
93
# File 'lib/hermeneutics/cli/pop3.rb', line 85

def stat
  if block_given? then
    writeline "STAT"
    n, r = split_num_len get_response
    yield n, r
  else
    stat do |*a| a end
  end
end

#top(n, x, &block) ⇒ Object



164
165
166
167
168
169
# File 'lib/hermeneutics/cli/pop3.rb', line 164

def top n, x, &block
  writeline "TOP #{n} #{x}"
  get_response do |_|
    get_data_str &block
  end
end

#uidl(n = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hermeneutics/cli/pop3.rb', line 126

def uidl n = nil
  n = n.to_i.nonzero?
  if block_given? then
    cmd = "UIDL"
    cmd << " #{n}" if n
    writeline cmd
    if n then
      n_, id = split_num get_response
      n == n_ or raise Check, "Wrong UIDL response: #{n} <-> #{n_}"
      yield n, id
    else
      get_response do |_|
        get_data do |l|
          n_, id = split_num l
          yield n_, id
        end
      end
    end
  else
    if n then
      uidl n do |*a| a end
    else
      h = {}
      uidl n do |n_,id|
        h[ n_] = id
      end
      h
    end
  end
end

#user(name) ⇒ Object



46
47
48
49
# File 'lib/hermeneutics/cli/pop3.rb', line 46

def user name
  writeline "USER #{name}"
  get_response
end