class TinyIRC::Command

Attributes

log[R]
branches[R]
name[R]
plugin[R]

Public Class Methods

new(plugin, name) click to toggle source
# File lib/tinyirc/command.rb, line 25
def initialize(plugin, name)
  @plugin   = plugin
  @name     = name
  @branches = {}
  TinyIRC::Command.log.info "commands += #{@plugin.name}/#{name}"
end

Public Instance Methods

branch(id, definition, &handler) click to toggle source
# File lib/tinyirc/command.rb, line 32
def branch(id, definition, &handler)
  @branches[id] = Branch.new(self, id, ParticleCMD::Definition.from_string(@name, definition), handler)
  TinyIRC::Command.log.info "#{@plugin.name}/#{@name} += #{id}: #{@branches[id].definition.command_signature(true)}"
  @branches[id]
end
checkcd(h, branch) click to toggle source
# File lib/tinyirc/command.rb, line 48
def checkcd(h, branch)
  return true if h[:socket].has_group(h[:host], 'admin')
  unless branch.last_uses[h[:socket].name]
    branch.last_uses[h[:socket].name] = {}
    return true
  end
  current = Time.now.to_i
  last = branch.last_uses[h[:socket].name][h[:nick]].to_i
  diff = current - last
  if diff >= branch.cooldown || diff < 0
    branch.last_uses[h[:socket].name][h[:nick]] = current
    true
  else
    false
  end
end
checkperm(h, fname) click to toggle source
# File lib/tinyirc/command.rb, line 44
def checkperm(h, fname)
  h[:socket].has_perm(h[:host], fname)
end
handle_command(h, cmd_info) click to toggle source
# File lib/tinyirc/command.rb, line 38
def handle_command(h, cmd_info)
  pname = h[:cmd_info][:plugin]
  pname = @plugin.name if pname == :any
  cname = h[:cmd_info][:command]
  bname = h[:cmd_info][:branch]
  
  def checkperm(h, fname)
    h[:socket].has_perm(h[:host], fname)
  end

  def checkcd(h, branch)
    return true if h[:socket].has_group(h[:host], 'admin')
    unless branch.last_uses[h[:socket].name]
      branch.last_uses[h[:socket].name] = {}
      return true
    end
    current = Time.now.to_i
    last = branch.last_uses[h[:socket].name][h[:nick]].to_i
    diff = current - last
    if diff >= branch.cooldown || diff < 0
      branch.last_uses[h[:socket].name][h[:nick]] = current
      true
    else
      false
    end
  end

  if (bname != :any)
    if @branches.include? bname
      branch = @branches[bname]
      res = branch.definition.match cmd_info
      if res
        bname = branch.id
        fname = pname + '/' + cname + '/' + bname
        unless checkperm(h, fname)
          h.nreply "Sorry, but you cannot execute this command (#{fname})"
          return :denied
        end
        unless checkcd(h, branch)
          h.nreply "Sorry, but you cannot execute this command now (cooldown - #{branch.cooldown}s)"
          return :cooldown
        end
        branch.handler.(h, res)
        true
      else
        false
      end
    else
      false
    end
  else
    @branches.each_pair do |id, branch|
      res = branch.definition.match cmd_info
      if res
        bname = branch.id
        fname = pname + '/' + cname + '/' + bname
        unless checkperm(h, fname)
          h.nreply "Sorry, but you cannot execute this command (#{fname})"
          return :denied
        end
        unless checkcd(h, branch)
          h.nreply "Sorry, but you cannot execute this command now (cooldown - #{branch.cooldown}s)"
          return :cooldown
        end
        branch.handler.(h, res)
        return true
      end
    end
    false
  end
end