require 'openssl'
require 'cgi'
require 'json'
+require 'erb'
+require 'ostruct'
module ::Irc
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)
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
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
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'
: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,