]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/webservice.rb
[webservice] expose webservice to plugins
[user/henk/code/ruby/rbot.git] / lib / rbot / core / webservice.rb
index 0eb655224ae3acdfa23722a7763999d79a83f11c..26465285f65b5d476112c1a41ffa0310cdc76fe7 100644 (file)
@@ -28,6 +28,18 @@ class ::WebServiceUser < Irc::User
   attr_accessor :response
 end
 
+class PingServlet < WEBrick::HTTPServlet::AbstractServlet
+  def initialize(server, bot)
+    super server
+    @bot = bot
+  end
+
+  def do_GET(req, res)
+    res['Content-Type'] = 'text/plain'
+    res.body = "pong\r\n"
+  end
+end
+
 class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
   def initialize(server, bot)
     super server
@@ -57,7 +69,12 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
     botuser = @bot.auth.get_botuser(username)
     raise 'Permission Denied' if not botuser or botuser.password != password
 
-    ret = dispatch_command(command, botuser, ip)
+    begin
+      ret = dispatch_command(command, botuser, ip)
+    rescue
+      debug '[webservice] error: ' + $!.to_s
+      debug $@.join("\n")
+    end
 
     res.status = 200
     if req['Accept'] == 'application/json'
@@ -107,6 +124,7 @@ class WebServiceModule < CoreBotModule
     @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
@@ -143,6 +161,7 @@ class WebServiceModule < CoreBotModule
     @server = WEBrick::HTTPServer.new(opts)
     debug 'webservice started: ' + opts.inspect
     @server.mount('/dispatch', DispatchServlet, @bot)
+    @server.mount('/ping', PingServlet, @bot)
     Thread.new { @server.start }
   end
 
@@ -171,6 +190,10 @@ class WebServiceModule < CoreBotModule
     m.reply s
   end
 
+  def register_servlet(plugin, servlet)
+    @server.mount('/plugin/%s' % plugin.name, servlet, plugin, @bot)
+  end
+
 end
 
 webservice = WebServiceModule.new