Class: Groat::SMTPD::Base

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/groat/smtpd/base.rb

Direct Known Subclasses

SMTPSyntax

Constant Summary collapse

@@numinstances =
0

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



51
52
53
54
55
56
57
58
# File 'lib/groat/smtpd/base.rb', line 51

def initialize
  @response_class = Response
  @instanceid = @@numinstances = @@numinstances + 1
  @s = nil
  @remote_address = nil
  @remote_port = nil
  reset_connection
end

Instance Method Details

#clientdata?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/groat/smtpd/base.rb', line 154

def clientdata?
  IO.select([@s], nil, nil, 0.1)
end

#fromclientObject



127
128
129
130
# File 'lib/groat/smtpd/base.rb', line 127

def fromclient
  line = getline
  log_line(:in, line)
end

#getdata(size) ⇒ Object



123
124
125
# File 'lib/groat/smtpd/base.rb', line 123

def getdata(size)
  sockop_timeout(:read, size)
end

#getlineObject



119
120
121
# File 'lib/groat/smtpd/base.rb', line 119

def getline
  sockop_timeout(:gets, "\n")
end

#log_line(direction, line) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/groat/smtpd/base.rb', line 132

def log_line(direction, line)
  if direction == :in
    if line.nil?
      puts "#{@instanceid}>/nil"
    else
      puts "#{@instanceid}>>" + line
    end
  else
    if line.nil?
      puts "#{@instanceid}</nil"
    else
      puts "#{@instanceid}<<" + line
    end
  end
  line
end

#process_line(line) ⇒ Object



75
76
# File 'lib/groat/smtpd/base.rb', line 75

def process_line(line)
end

#reply(args) ⇒ Object

Raises:

  • (@response_class)


60
61
62
# File 'lib/groat/smtpd/base.rb', line 60

def reply(args)
  raise @response_class, args
end

#reset_connectionObject



84
85
# File 'lib/groat/smtpd/base.rb', line 84

def reset_connection
end

#run(method, *args, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/groat/smtpd/base.rb', line 64

def run(method, *args, &block)
  if block_given?
    yield
  else
    send method, *args
  end
rescue Response => r
  toclient r.reply_text
  not r.terminate?
end

#secure?Boolean

Nothing in the base implements security

Returns:

  • (Boolean)


88
89
90
# File 'lib/groat/smtpd/base.rb', line 88

def secure?
  false
end

#send_greetingObject



78
79
# File 'lib/groat/smtpd/base.rb', line 78

def send_greeting
end

#serve(io) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/groat/smtpd/base.rb', line 97

def serve(io)
  set_socket io
  reset_connection
  run :send_greeting
  continue = true
  while continue do
    line = fromclient
    break if line.nil?
    continue = process_line line
  end
rescue TimeoutError
  run :service_shutdown
end

#service_shutdownObject



81
82
# File 'lib/groat/smtpd/base.rb', line 81

def service_shutdown
end

#set_socket(io) ⇒ Object



92
93
94
95
# File 'lib/groat/smtpd/base.rb', line 92

def set_socket(io)
  @s = io
  x, @remote_port, x, @remote_address = io.peeraddr
end

#sockop_timeout(method, arg, wait = 30) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/groat/smtpd/base.rb', line 111

def sockop_timeout(method, arg, wait = 30)
  begin
    timeout(wait){
      return @s.__send__(method, arg)
    }
  end
end

#toclient(msg) ⇒ Object



149
150
151
152
# File 'lib/groat/smtpd/base.rb', line 149

def toclient(msg)
  log_line(:out, msg)
  @s.print(msg)
end