summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias H <apoc@sixserv.org>2014-02-24 07:05:05 +0100
committerMatthias H <apoc@sixserv.org>2014-02-24 07:06:53 +0100
commite8e8a2c78c95c24f99c2a18aeeaff8ac243a41ef (patch)
tree5ed9c25f1d91a57fba3c388bfa612aa872778865
parent95b52115351db106a93613e6ea15f0e6acbeee42 (diff)
[webservice] response as json if asked to
-rw-r--r--lib/rbot/core/webservice.rb42
1 files changed, 23 insertions, 19 deletions
diff --git a/lib/rbot/core/webservice.rb b/lib/rbot/core/webservice.rb
index 464f0ab3..7183a245 100644
--- a/lib/rbot/core/webservice.rb
+++ b/lib/rbot/core/webservice.rb
@@ -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