class Irc::Bot::WebMessage

A WebMessage is a web request and response object combined with helper methods.

Attributes

args[R]

Parsed url parameters.

bot[R]

Bot instance

client[R]

Client IP.

method[R]

HTTP method (POST, GET, etc.)

path[R]

URL Path.

post[R]

Parsed post request parameters.

req[R]

Request object, a instance of WEBrick::HTTPRequest ({www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPRequest.html docs})

res[R]

Response object, a instance of WEBrick::HTTPResponse ({www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPResponse.html docs})

source[R]

The bot user issuing the command.

Public Class Methods

new(bot, req, res) click to toggle source
# File lib/rbot/core/webservice.rb, line 55
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 = 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]

  # login a botuser with http authentication
  WEBrick::HTTPAuth.basic_auth(req, res, 'RBotAuth') { |username, password|
    if username
      botuser = @bot.auth.get_botuser(Auth::BotUser.sanitize_username(username))
      if botuser and botuser.password == password
        @source = botuser
        true
      else
        false
      end
    else
      true # no need to request auth at this point
    end
  }

  @path = req.path

  @load_path = [File.join(Config::datadir, 'web')]
  @load_path += @bot.plugins.core_module_dirs
  @load_path += @bot.plugins.plugin_dirs
end

Public Instance Methods

get_load_path(filename) click to toggle source

Expands a relative filename to absolute using a list of load paths.

# File lib/rbot/core/webservice.rb, line 134
def get_load_path(filename)
  @load_path.each do |path|
    file = File.join(path, filename) 
    return file if File.exist?(file)
  end
end
parse_query(query) click to toggle source
# File lib/rbot/core/webservice.rb, line 93
def parse_query(query)
  params = CGI::parse(query)
  params.each_pair do |key, val|
    params[key] = val.last
  end
  params
end
private?() click to toggle source

Remote messages are always ‘private’

# File lib/rbot/core/webservice.rb, line 107
def private?
  true
end
render(template, args={}) click to toggle source

Renders a erb template and responds it

# File lib/rbot/core/webservice.rb, line 142
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
send_html(body, status=200) click to toggle source

Sends a html response

# File lib/rbot/core/webservice.rb, line 129
def send_html(body, status=200)
  send_response(body, status, 'text/html')
end
send_json(body, status=200) click to toggle source

Sends a json response

# File lib/rbot/core/webservice.rb, line 124
def send_json(body, status=200)
  send_response(body, status, 'application/json')
end
send_plaintext(body, status=200) click to toggle source

Sends a plaintext response

# File lib/rbot/core/webservice.rb, line 119
def send_plaintext(body, status=200)
  send_response(body, status, 'text/plain')
end
send_response(body, status, type) click to toggle source

Sends a response with the specified body, status and content type.

# File lib/rbot/core/webservice.rb, line 112
def send_response(body, status, type)
  @res.status = status
  @res['Content-Type'] = type
  @res.body = body
end
target() click to toggle source

The target of a RemoteMessage

# File lib/rbot/core/webservice.rb, line 102
def target
  @bot
end