X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Frbot%2Fcore%2Fwebservice.rb;h=c823594ad437fecc8473e29fd8b35118b625f984;hb=592c800425410f63b1a6ebf467d32273ccd377b4;hp=42365a6ae4c8a8181a78bf25ee68f5592ab59228;hpb=d935d59d0430c40c9e51664693c370563d5d09cb;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/webservice.rb b/lib/rbot/core/webservice.rb index 42365a6a..c823594a 100644 --- a/lib/rbot/core/webservice.rb +++ b/lib/rbot/core/webservice.rb @@ -23,15 +23,46 @@ class Bot # A WebMessage is a web request and response object combined with helper methods. # class WebMessage - attr_reader :bot, :method, :bot, :req, :res, :post, :client, :path, :source + # Bot instance + # + attr_reader :bot + # HTTP method (POST, GET, etc.) + # + attr_reader :method + # Request object, a instance of WEBrick::HTTPRequest ({http://www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPRequest.html docs}) + # + attr_reader :req + # Response object, a instance of WEBrick::HTTPResponse ({http://www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPResponse.html docs}) + # + attr_reader :res + # Parsed post request parameters. + # + attr_reader :post + # Parsed url parameters. + # + attr_reader :args + # Client IP. + # + attr_reader :client + # URL Path. + # + attr_reader :path + # The bot user issuing the command. + # + attr_reader :source def initialize(bot, req, res) @bot = bot @req = req @res = res @method = req.request_method + @post = {} if req.body and not req.body.empty? - @post = CGI::parse(req.body) + @post = parse_query(req.body) + end + @args = {} + if req.query_string and not req.query_string.empty? + @args = parse_query(req.query_string) end @client = req.peeraddr[3] @@ -42,8 +73,9 @@ class Bot if botuser and botuser.password == password @source = botuser true + else + false end - false else true # no need to request auth at this point end @@ -53,6 +85,14 @@ class Bot debug '@path = ' + @path.inspect end + def parse_query(query) + params = CGI::parse(query) + params.each_pair do |key, val| + params[key] = val.last + end + params + end + # The target of a RemoteMessage def target @bot @@ -63,12 +103,27 @@ class Bot true end - # Sends a plaintext response - def send_plaintext(body, status=200) + # Sends a response with the specified body, status and content type. + def send_response(body, status, type) @res.status = status - @res['Content-Type'] = 'text/plain' + @res['Content-Type'] = type @res.body = body end + + # Sends a plaintext response + def send_plaintext(body, status=200) + send_response(body, status, 'text/plain') + end + + # Sends a json response + def send_json(body, status=200) + send_response(body, status, 'application/json') + end + + # Sends a html response + def send_html(body, status=200) + send_response(body, status, 'text/html') + end end # works similar to a message mapper but for url paths @@ -198,8 +253,8 @@ class Bot if m.bot.auth.permit?(m.source || Auth::defaultbotuser, auth, '?') debug "template match found and auth'd: #{action.inspect} #{params.inspect}" response = botmodule.send(action, m, params) - if m.res.sent_size == 0 - m.send_plaintext(response.to_json) + if m.res.sent_size == 0 and m.res.body.empty? + m.send_json(response.to_json) end return true end @@ -262,6 +317,16 @@ class Bot web_cleanup end end + + # And just because I like consistency: + # + module WebCoreBotModule + include WebBotModule + end + + module WebPlugin + include WebBotModule + end end end # Bot end # Irc @@ -287,6 +352,11 @@ class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet begin m = WebMessage.new(@bot, req, res) @bot.web_dispatcher.handle m + rescue WEBrick::HTTPStatus::Unauthorized + res.status = 401 + res['Content-Type'] = 'text/plain' + res.body = 'Authentication Required!' + error 'authentication error (wrong password)' rescue res.status = 500 res['Content-Type'] = 'text/plain' @@ -307,7 +377,7 @@ end class WebServiceModule < CoreBotModule - include WebBotModule + include WebCoreBotModule Config.register Config::BooleanValue.new('webservice.autostart', :default => false, @@ -430,7 +500,7 @@ class WebServiceModule < CoreBotModule return end - command = m.post['command'][0] + command = m.post['command'] if not m.source botuser = Auth::defaultbotuser else @@ -438,12 +508,18 @@ class WebServiceModule < CoreBotModule 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) - { :reply => user.response } + if m.req['Accept'] == 'application/json' + { :reply => user.response } + else + m.send_plaintext(user.response.join("\n") + "\n") + end end end