Class: Hermeneutics::Cli::SMTP

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

Defined Under Namespace

Classes: NotOk, NotReadyForData, Response, ServerNotReady, Uncaught, 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) ⇒ SMTP

Returns a new instance of SMTP.



36
37
38
39
# File 'lib/hermeneutics/cli/smtp.rb', line 36

def initialize *args
  super
  get_response.ok? or raise ServerNotReady, @last_response.msg
end

Instance Attribute Details

#advertisedObject (readonly)

Returns the value of attribute advertised.



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

def advertised
  @advertised
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#greetObject (readonly)

Returns the value of attribute greet.



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

def greet
  @greet
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



34
35
36
# File 'lib/hermeneutics/cli/smtp.rb', line 34

def last_response
  @last_response
end

Class Method Details

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



26
27
28
29
# File 'lib/hermeneutics/cli/smtp.rb', line 26

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

#authObject



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

def auth
  @advertised && @advertised[ :AUTH]
end

#bdat(data) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hermeneutics/cli/smtp.rb', line 96

def bdat data
  data.each { |d|
    write_cmd "BDAT", d.bytesize
    write d
    get_response_ok
  }
  write_cmd "BDAT", 0, "LAST"
  get_response_ok do |code,msg|
    yield msg if block_given?
  end
end

#data(reader) ⇒ Object



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

def data reader
  write_cmd "DATA"
  get_response.waiting? or raise NotReadyForData, @last_response.msg
  reader.each_line { |l|
    l =~ /\A\./ and l = ".#{l}"
    writeline l
  }
  writeline "."
  get_response_ok
end

#ehlo(host = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hermeneutics/cli/smtp.rb', line 59

def ehlo host = nil
  @advertised = {}
  cmd_hello "EHLO", host do |code,msg|
    unless @domain then
      @domain, @greet = msg.split nil, 2
      next
    end
    keyword, param = msg.split nil, 2
    keyword.upcase!
    keyword = keyword.to_sym
    case keyword
      when :SIZE then param = Integer param
      when :AUTH then param = param.split.map { |p| p.upcase! ; p.to_sym }
    end
    @advertised[ keyword] = param || true
  end
end

#has_auth?(meth) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/hermeneutics/cli/smtp.rb', line 49

def has_auth? meth
  a = auth
  a and a.include? meth
end

#helo(host = nil) ⇒ Object



55
56
57
# File 'lib/hermeneutics/cli/smtp.rb', line 55

def helo host = nil
  cmd_hello "HELO", host
end

#help(str = nil, &block) ⇒ Object



112
113
114
# File 'lib/hermeneutics/cli/smtp.rb', line 112

def help str = nil, &block
  cmd "HELP", str, &block
end

#login(user, password) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/hermeneutics/cli/smtp.rb', line 134

def  user, password
  write_cmd "AUTH", "LOGIN"
  get_response.waiting? or raise NotReadyForData, @last_response.msg
  writeline [user].pack "m0"
  get_response.waiting? or raise NotReadyForData, @last_response.msg
  writeline [password].pack "m0"
  get_response_ok
end

#mail_from(from) ⇒ Object



77
78
79
# File 'lib/hermeneutics/cli/smtp.rb', line 77

def mail_from from
  cmd "MAIL", "FROM:<#{from}>"
end

#noop(str = nil) ⇒ Object



116
117
118
# File 'lib/hermeneutics/cli/smtp.rb', line 116

def noop str = nil
  cmd "NOOP"
end

#plain(user, password) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/hermeneutics/cli/smtp.rb', line 125

def plain user, password
  write_cmd "AUTH", "PLAIN"
  get_response.waiting? or raise NotReadyForData, @last_response.msg
  l = ["\0#{user}\0#{password}"].pack "m0"
  writeline l
  get_response_ok
end

#quitObject



120
121
122
# File 'lib/hermeneutics/cli/smtp.rb', line 120

def quit
  cmd "QUIT"
end

#rcpt_to(to) ⇒ Object



81
82
83
# File 'lib/hermeneutics/cli/smtp.rb', line 81

def rcpt_to to
  cmd "RCPT", "TO:<#{to}>"
end

#rsetObject



108
109
110
# File 'lib/hermeneutics/cli/smtp.rb', line 108

def rset
  cmd "RSET"
end

#sizeObject



41
42
43
# File 'lib/hermeneutics/cli/smtp.rb', line 41

def size
  @advertised && @advertised[ :SIZE]
end