Class: TinyIRC::Plugin

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, name) ⇒ Plugin

Returns a new instance of Plugin.

Raises:

  • (RuntimeError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tinyirc/plugin.rb', line 4

def initialize(bot, name)
  @loaded     = false
  
  @l_init     = lambda {}
  @l_postinit = lambda {}
  @l_config   = lambda {|cfg|}
  
  @bot      = bot
  @fullname = name
  @name     = File.basename(name)
  
  @commands       = {}
  @event_handlers = {}
  @groups         = {}

  @log = ParticleLog.new('?' + @name, ParticleLog::INFO) 
  @log.info 'Loading...'

  lp = "./#{name}.rb"
  ok = false
  if File.exists? lp
    instance_eval(File.read(lp), lp) 
    ok = true
  else
    $LOAD_PATH.each do |f|
      lp = File.join(f, name + '.rb')
      if File.exists? lp
        instance_eval(File.read(lp), lp)
        ok = true
      end
    end
  end

  raise RuntimeError, "Cannot find the `#{name}` plugin" unless ok

  _l_init
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def commands
  @commands
end

#event_handlersObject (readonly)

Returns the value of attribute event_handlers.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def event_handlers
  @event_handlers
end

#fullnameObject (readonly)

Returns the value of attribute fullname.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def fullname
  @fullname
end

#groupsObject (readonly)

Returns the value of attribute groups.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def groups
  @groups
end

#loadedObject (readonly)

Returns the value of attribute loaded.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def loaded
  @loaded
end

#logObject (readonly)

Returns the value of attribute log.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def log
  @log
end

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/tinyirc/plugin.rb', line 2

def name
  @name
end

Instance Method Details

#_l_config(cfg) ⇒ Object



57
58
59
60
# File 'lib/tinyirc/plugin.rb', line 57

def _l_config(cfg)
  @log.info 'Configuring...'
  @l_config.(cfg)
end

#_l_initObject



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

def _l_init()
  @log.info 'Initializing...'
  @l_init.()
end

#_l_postinitObject



49
50
51
52
53
54
# File 'lib/tinyirc/plugin.rb', line 49

def _l_postinit()
  @log.info 'Post-Initializing...'
  @l_postinit.()
  @loaded = true
  @log.important "Hello, bot!"
end

#cmd(name) ⇒ Object



76
77
78
79
# File 'lib/tinyirc/plugin.rb', line 76

def cmd(name)
  @commands[name] = TinyIRC::Command.new(self, name)
  @commands[name]
end

#configure(&b) ⇒ Object



56
# File 'lib/tinyirc/plugin.rb', line 56

def configure(&b) @l_config = b end

#group(name) ⇒ Object



90
91
92
93
94
# File 'lib/tinyirc/plugin.rb', line 90

def group(name)
  n = "#{@name}/#{name}"
  @groups[n] = TinyIRC::Group.new n
  @groups[n]
end

#handle_command(h, cmd_info) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/tinyirc/plugin.rb', line 81

def handle_command(h, cmd_info)
  return false if h[:cmd_info][:plugin] != @name && h[:cmd_info][:plugin] != :any
  x = if @commands.include? h[:cmd_info][:command]
    @commands[h[:cmd_info][:command]].handle_command(h, cmd_info)
  else
    false
  end
end

#init(&b) ⇒ Object



42
# File 'lib/tinyirc/plugin.rb', line 42

def init(&b) @l_init = b end

#on(type, **pattern, &handler) ⇒ Object



62
63
64
65
66
# File 'lib/tinyirc/plugin.rb', line 62

def on(type, pattern, &block) 
  pattern[:type] = type
  pattern[:_block] = block
  @event_handlers[type] = pattern
end

#postinit(&b) ⇒ Object



48
# File 'lib/tinyirc/plugin.rb', line 48

def postinit(&b) @l_postinit = b end