Class: TinyIRC::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyirc/command.rb

Defined Under Namespace

Classes: Branch

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name) ⇒ Command

Returns a new instance of Command.



25
26
27
28
29
30
# 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

Class Attribute Details

.logObject (readonly)

Returns the value of attribute log.



3
4
5
# File 'lib/tinyirc/command.rb', line 3

def log
  @log
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



23
24
25
# File 'lib/tinyirc/command.rb', line 23

def branches
  @branches
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/tinyirc/command.rb', line 23

def name
  @name
end

#pluginObject (readonly)

Returns the value of attribute plugin.



23
24
25
# File 'lib/tinyirc/command.rb', line 23

def plugin
  @plugin
end

Instance Method Details

#branch(id, definition, &handler) ⇒ Object



32
33
34
35
36
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



44
45
46
# File 'lib/tinyirc/command.rb', line 44

def checkperm(h, fname)
  h[:socket].has_perm(h[:host], fname)
end

#handle_command(h, cmd_info) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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