Class: WebServiceModule

Inherits:
CoreBotModule
  • Object
show all
Includes:
WebCoreBotModule
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb

Instance Method Summary (collapse)

Constructor Details

- (WebServiceModule) initialize

Returns a new instance of WebServiceModule



416
417
418
419
420
421
422
423
424
425
426
427
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 416

def initialize
  super
  @port = @bot.config['webservice.port']
  @host = @bot.config['webservice.host']
  @server = nil
  @bot.webservice = self
  begin
    start_service if @bot.config['webservice.autostart']
  rescue => e
    error "couldn't start web service provider: #{e.inspect}"
  end
end

Instance Method Details

- (Object) cleanup



466
467
468
469
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 466

def cleanup
  stop_service
  super
end

- (Object) handle_dispatch(m, params)



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 497

def handle_dispatch(m, params)
  if not @bot.config['webservice.allow_dispatch']
    m.send_plaintext('dispatch forbidden by configuration', 403)
    return
  end

  command = m.post['command']
  if not m.source
    botuser = Auth::defaultbotuser
  else
    botuser = m.source.botuser
  end
  netmask = '%s!%s@%s' % [botuser.username, botuser.username, m.client]

  debug 'dispatch command: ' + command

  user = WebServiceUser.new(netmask, botuser)
  message = Irc::PrivMessage.new(@bot, nil, user, @bot.myself, command)

  res = @bot.plugins.irc_delegate('privmsg', message)

  if m.req['Accept'] == 'application/json'
    { :reply => user.response }
  else
    m.send_plaintext(user.response.join("\n") + "\n")
  end
end

- (Object) handle_ping(m, params)



493
494
495
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 493

def handle_ping(m, params)
  m.send_plaintext("pong\n")
end

- (Object) handle_start(m, params)



471
472
473
474
475
476
477
478
479
480
481
482
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 471

def handle_start(m, params)
  if @server
    m.reply 'web service already running'
  else
    begin
      start_service
      m.reply 'web service started'
    rescue
      m.reply 'unable to start web service, error: ' + $!.to_s
    end
  end
end

- (Object) handle_stop(m, params)



484
485
486
487
488
489
490
491
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 484

def handle_stop(m, params)
  if @server
    stop_service
    m.reply 'web service stopped'
  else
    m.reply 'web service not running'
  end
end

- (Object) start_service



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 429

def start_service
  raise "Remote service provider already running" if @server
  opts = {:BindAddress => @host, :Port => @port}
  if @bot.config['webservice.ssl']
    opts.merge! :SSLEnable => true
    cert = File.expand_path @bot.config['webservice.ssl_cert']
    key = File.expand_path @bot.config['webservice.ssl_key']
    if File.exists? cert and File.exists? key
      debug 'using ssl certificate files'
      opts.merge!({
        :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(cert)),
        :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(key))
      })
    else
      debug 'using on-the-fly generated ssl certs'
      opts.merge! :SSLCertName => [ %w[CN localhost] ]
      # the problem with this is that it will always use the same
      # serial number which makes this feature pretty much useless.
    end
  end
  # Logging to file in ~/.rbot
  logfile = File.open(@bot.path('webservice.log'), 'a+')
  opts.merge!({
    :Logger => WEBrick::Log.new(logfile),
    :AccessLog => [[logfile, WEBrick::AccessLog::COMBINED_LOG_FORMAT]]
  })
  @server = WEBrick::HTTPServer.new(opts)
  debug 'webservice started: ' + opts.inspect
  @server.mount('/', DispatchServlet, @bot)
  Thread.new { @server.start }
end

- (Object) stop_service



461
462
463
464
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/webservice.rb', line 461

def stop_service
  @server.shutdown if @server
  @server = nil
end