Class: Milter::MilterConnectionHandler

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/milter.rb

Constant Summary collapse

@@milter_class =
Milter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMilterConnectionHandler

Returns a new instance of MilterConnectionHandler.



180
181
182
183
# File 'lib/milter.rb', line 180

def initialize
  @data = ''
  @milter = @@milter_class.new
end

Class Method Details

.register(milter_class) ⇒ Object



311
312
313
# File 'lib/milter.rb', line 311

def register( milter_class )
  @@milter_class = milter_class
end

Instance Method Details

#parse_abort(data) ⇒ Object

SMFIC_ABORT, no args



255
256
257
# File 'lib/milter.rb', line 255

def parse_abort( data )
  return []
end

#parse_body(data) ⇒ Object

SMFIC_BODY, one arg (string)



240
241
242
# File 'lib/milter.rb', line 240

def parse_body( data )
  return [ data.delete("\0") ]
end

#parse_connect(data) ⇒ Object

SMFIC_CONNECT, two args (strings)



203
204
205
206
207
208
209
# File 'lib/milter.rb', line 203

def parse_connect( data )
  hostname, val = data.split("\0", 2)
  family = val[0].unpack('C')
  port = val[1...3].unpack('n')
  address = val[3..-1]
  return [hostname, family, port, address]
end

#parse_data(data) ⇒ Object

SMFIC_DATA, no args



260
261
262
# File 'lib/milter.rb', line 260

def parse_data( data )
  return []
end

#parse_end_body(data) ⇒ Object

SMFIC_BODYEOB, one arg (string)



245
246
247
# File 'lib/milter.rb', line 245

def parse_end_body( data )
  return [data]
end

#parse_end_headers(data) ⇒ Object

SMFIC_EOH, no args



235
236
237
# File 'lib/milter.rb', line 235

def parse_end_headers( data )
  return []
end

#parse_header(data) ⇒ Object

SMFIC_HEADER, two args (strings)



229
230
231
232
# File 'lib/milter.rb', line 229

def parse_header( data )
  k,v = data.split("\0", 2)
  return [k, v.delete("\0")]
end

#parse_helo(data) ⇒ Object

SMFIC_HELO, one arg (string)



212
213
214
# File 'lib/milter.rb', line 212

def parse_helo( data )
  return [data]
end

#parse_macro(data) ⇒ Object

SMFIC_MACRO, 0 separated list of args, NULL-terminated



197
198
199
200
# File 'lib/milter.rb', line 197

def parse_macro( data )
  cmd, macros = data[0].chr, data[1..-1].split("\0" )
  return [cmd, Hash[*macros]]
end

#parse_mail_from(data) ⇒ Object

SMFIC_MAIL, 0 separated list of args, NULL-terminated



217
218
219
220
# File 'lib/milter.rb', line 217

def parse_mail_from( data )
  mailfrom, esmtp_info = data.split("\0", 2 )
  return [mailfrom, esmtp_info.split("\0")]
end

#parse_opt_neg(data) ⇒ Object

SMFIC_OPTNEG, two integers



191
192
193
194
# File 'lib/milter.rb', line 191

def parse_opt_neg( data )
  ver, actions, protocol = data.unpack('NNN')
  return [ver, actions, protocol]
end

#parse_quit_new_connection(data) ⇒ Object

SMFIC_QUIT_NC, no args



270
271
272
# File 'lib/milter.rb', line 270

def parse_quit_new_connection( data )
  return []
end

#parse_rcpt_to(data) ⇒ Object

SMFIC_RCPT, 0 separated list of args, NULL-terminated



223
224
225
226
# File 'lib/milter.rb', line 223

def parse_rcpt_to( data )
  mailto, esmtp_info = data.split("\0", 2 )
  return [mailto, esmtp_info.split("\0")]
end

#parse_unknown(data) ⇒ Object

SMFIC_UNKNOWN, one arg (string)



265
266
267
# File 'lib/milter.rb', line 265

def parse_unknown( data )
  return data
end

#prase_quit(data) ⇒ Object

SMFIC_QUIT, no args



250
251
252
# File 'lib/milter.rb', line 250

def prase_quit( data )
  return []
end

#receive_data(data) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/milter.rb', line 274

def receive_data( data )
  # puts "Data: #{data.bytes.map(&:chr)}"
  @data << data
  while @data.size >= MILTER_LEN_BYTES
    pkt_len = @data[0...MILTER_LEN_BYTES].unpack('N').first
    if @data.size >= MILTER_LEN_BYTES + pkt_len
      @data.slice!(0, MILTER_LEN_BYTES)
      pkt = @data.slice!(0, pkt_len)
      cmd, val = pkt[0].chr, pkt[1..-1]

      if COMMANDS.include?(cmd) and @milter.respond_to?(COMMANDS[cmd])
        method_name = COMMANDS[cmd]
        args = []
        args = self.send('parse_' + method_name.to_s, val ) if self.respond_to?('parse_' + method_name.to_s )
        ret = @milter.send(method_name, *args )

        close_connection and return if cmd == SMFIC_QUIT
        next if cmd == SMFIC_MACRO

        if not ret.is_a? Array
          ret = [ ret ]
        end

        ret.each do |r|
          send_milter_response(r)
        end
      else
        next if cmd == SMFIC_MACRO
        send_milter_response(RESPONSE[:continue])
      end
    else
      break
    end
  end
end

#send_milter_response(res) ⇒ Object



185
186
187
188
# File 'lib/milter.rb', line 185

def send_milter_response( res )
  r = [ res.size ].pack('N') + res
  send_data(r)
end