X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Fwebservice.rb;h=112ec85e7672f859b0daefe69ec8d1bce2183e8d;hb=28502d92c420aefa3832e57561044efa06b9ab8b;hp=4b58b98ef6bbcfea1e9e241fe0bc44f764f858b7;hpb=4db74a4964d8438f1c72a07e2d25600b571540c5;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/webservice.rb b/lib/rbot/core/webservice.rb index 4b58b98e..112ec85e 100644 --- a/lib/rbot/core/webservice.rb +++ b/lib/rbot/core/webservice.rb @@ -17,6 +17,8 @@ require 'webrick/https' require 'openssl' require 'cgi' require 'json' +require 'erb' +require 'ostruct' module ::Irc class Bot @@ -73,15 +75,19 @@ 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 } @path = req.path - debug '@path = ' + @path.inspect + + @load_path = [File.join(Config::datadir, 'web')] + @load_path += @bot.plugins.core_module_dirs + @load_path += @bot.plugins.plugin_dirs end def parse_query(query) @@ -102,18 +108,47 @@ 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) - @res.status = status - @res['Content-Type'] = 'application/json' - @res.body = body + send_response(body, status, 'application/json') + end + + # Sends a html response + def send_html(body, status=200) + send_response(body, status, 'text/html') + end + + # Expands a relative filename to absolute using a list of load paths. + def get_load_path(filename) + @load_path.each do |path| + file = File.join(path, filename) + return file if File.exists?(file) + end + end + + # Renders a erb template and responds it + def render(template, args={}) + file = get_load_path template + if not file + raise 'template not found: ' + template + end + + tmpl = ERB.new(IO.read(file)) + ns = OpenStruct.new(args) + body = tmpl.result(ns.instance_eval { binding }) + send_html(body, 200) end end @@ -208,7 +243,7 @@ class Bot tmpl = @templates[index] raise "Botmodule #{botmodule.name} tried to unmap #{tmpl.inspect} that was handled by #{tmpl.botmodule}" unless tmpl.botmodule == botmodule.name debug "Unmapping #{tmpl.inspect}" - @templates[handle] = nil + @templates[index] = nil @templates.clear unless @templates.compact.size > 0 end @@ -308,6 +343,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 @@ -333,6 +378,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' @@ -353,7 +403,7 @@ end class WebServiceModule < CoreBotModule - include WebBotModule + include WebCoreBotModule Config.register Config::BooleanValue.new('webservice.autostart', :default => false, @@ -370,6 +420,10 @@ class WebServiceModule < CoreBotModule :requires_rescan => true, :desc => 'Host the web service will bind on') + Config.register Config::StringValue.new('webservice.url', + :default => 'http://127.0.0.1:7268', + :desc => 'The public URL of the web service.') + Config.register Config::BooleanValue.new('webservice.ssl', :default => false, :requires_rescan => true, @@ -480,7 +534,7 @@ class WebServiceModule < CoreBotModule if not m.source botuser = Auth::defaultbotuser else - botuser = m.source.botuser + botuser = m.source end netmask = '%s!%s@%s' % [botuser.username, botuser.username, m.client]