]> 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 7183a245276b58997cee8885079206c7837026a9..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
@@ -134,9 +152,16 @@ class WebServiceModule < CoreBotModule
         # 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('/dispatch', DispatchServlet, @bot)
+    @server.mount('/ping', PingServlet, @bot)
     Thread.new { @server.start }
   end
 
@@ -165,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