X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Fwebservice.rb;h=112ec85e7672f859b0daefe69ec8d1bce2183e8d;hb=28502d92c420aefa3832e57561044efa06b9ab8b;hp=c823594ad437fecc8473e29fd8b35118b625f984;hpb=592c800425410f63b1a6ebf467d32273ccd377b4;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/webservice.rb b/lib/rbot/core/webservice.rb index c823594a..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 @@ -82,7 +84,10 @@ class Bot } @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) @@ -124,6 +129,27 @@ class Bot 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 # works similar to a message mapper but for url paths @@ -217,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 @@ -394,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, @@ -504,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]