class WebServiceModule
Public Class Methods
new()
click to toggle source
Calls superclass method
# File lib/rbot/core/webservice.rb, line 446 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
Public Instance Methods
cleanup()
click to toggle source
Calls superclass method
# File lib/rbot/core/webservice.rb, line 496 def cleanup stop_service super end
handle_dispatch(m, params)
click to toggle source
# File lib/rbot/core/webservice.rb, line 527 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 command.empty? m.send_plaintext('wrong syntax', 400) return end if not m.source botuser = Auth::defaultbotuser else botuser = m.source 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) # TODO if delegation failed due to wrong auth, it should be reported # as an error, not 200 OK if m.req['Accept'] == 'application/json' { :reply => user.response } else m.send_plaintext(user.response.join("\n") + "\n") end end
handle_ping(m, params)
click to toggle source
# File lib/rbot/core/webservice.rb, line 523 def handle_ping(m, params) m.send_plaintext("pong\n") end
handle_start(m, params)
click to toggle source
# File lib/rbot/core/webservice.rb, line 501 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
handle_stop(m, params)
click to toggle source
# File lib/rbot/core/webservice.rb, line 514 def handle_stop(m, params) if @server stop_service m.reply 'web service stopped' else m.reply 'web service not running' end end
start_service()
click to toggle source
# File lib/rbot/core/webservice.rb, line 459 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.exist? cert and File.exist? 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
stop_service()
click to toggle source
# File lib/rbot/core/webservice.rb, line 491 def stop_service @server.shutdown if @server @server = nil end