]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/core/webservice.rb
[webservice] response as json if asked to
[user/henk/code/ruby/rbot.git] / lib / rbot / core / webservice.rb
index 464f0ab3461e4269abb2e76bbbfad2afca34efa3..7183a245276b58997cee8885079206c7837026a9 100644 (file)
@@ -16,6 +16,7 @@ require 'webrick'
 require 'webrick/https'
 require 'openssl'
 require 'cgi'
+require 'json'
 
 class ::WebServiceUser < Irc::User
   def initialize(str, botuser, opts={})
@@ -33,36 +34,39 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet
     @bot = bot
   end
 
+  def dispatch_command(command, botuser, ip)
+    netmask = '%s!%s@%s' % [botuser.username, botuser.username, ip]
+
+    user = WebServiceUser.new(netmask, botuser)
+    message = Irc::PrivMessage.new(@bot, nil, user, @bot.myself, command)
+
+    @bot.plugins.irc_delegate('privmsg', message)
+
+    { :reply => user.response }
+  end
+
+  # Handle a dispatch request.
   def do_POST(req, res)
-    # NOTE: still wip.
-    uri = req.path_info
     post = CGI::parse(req.body)
     ip = req.peeraddr[3]
 
-    if post['command']
-      command = post['command'].first
-    else
-      command = uri.gsub('/', ' ').strip
-    end
-
     username = post['username'].first
     password = post['password'].first
+    command = post['command'].first
 
     botuser = @bot.auth.get_botuser(username)
-    netmask = '%s!%s@%s' % [botuser.username, botuser.username, ip]
-
-    if not botuser or botuser.password != password
-      raise 'Permission Denied'
-    end
+    raise 'Permission Denied' if not botuser or botuser.password != password
 
-    ws_user = WebServiceUser.new(netmask, botuser)
-    message = Irc::PrivMessage.new(@bot, nil, ws_user, @bot.myself, command)
-
-    @bot.plugins.irc_delegate('privmsg', message)
+    ret = dispatch_command(command, botuser, ip)
 
     res.status = 200
-    res['Content-Type'] = 'text/plain'
-    res.body = ws_user.response.join("\n") + "\n"
+    if req['Accept'] == 'application/json'
+      res['Content-Type'] = 'application/json'
+      res.body = JSON.dump ret
+    else
+      res['Content-Type'] = 'text/plain'
+      res.body = ret[:reply].join("\n") + "\n"
+    end
   end
 end