Class: ProtonBot::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/protonbot/bot.rb,
lib/protonbot/bot_conf.rb,
lib/protonbot/bot_plugins.rb

Overview

Main bot class. Use it to create the bot

Defined Under Namespace

Modules: Messages

Config collapse

DEFAULT_SERVER_CONFIG =

Default server-config

{
  'host' => '127.0.0.1',
  'port' => 6667,
  'user' => 'ProtonBot',
  'nick' => 'ProtonBot',
  'rnam' => 'An IRC bot in Ruby',
  'queue_delay' => 0.7,
  'cmdchar' => '\\',
  'encoding' => 'UTF-8',
  'autojoin' => []
}.freeze

Instance Attribute Summary collapse

Config collapse

Plugins collapse

Instance Method Summary collapse

Constructor Details

#initialize { ... } ⇒ Bot

Returns a new instance of Bot.

Yields:

  • Main bot’s block. You’ll use it for configuring bot.



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/protonbot/bot.rb', line 6

def initialize(&block)
  @_log = ProtonBot::Log.new
  @log = @_log.wrap('main')
  @log.info('Hi there!')

  instance_exec(&block)
  @log.info('Processed main block')

  @conf = {}
  configure
  @log.info('Processed config block')

  @plugins = {}
  plugins_load
  @log.info('Processed plugins block')

  @dbs = {}
  @plugs = {}
  @plugthrs = {}
  Dir.mkdir('dbs/') unless File.exist?(File.expand_path('./dbs/'))
  @conf['servers'].each do |k_, v_|
    k = k_.clone
    v = v_.clone
    @dbs[k] = Heliodor::DB.new("dbs/#{k}.db", true) unless k.nil?
    @plugs[k] = ProtonBot::Plug.new(self, k.clone, v.clone)
    begin
      if v['enabled'] || v['enabled'].nil?
        Thread.new do
          @plugs[k].connect!
        end
      end
    rescue => e
      @plugs[k].log_err(e)
    end
  end

  Signal.trap('INT') do
    @plugs.each do |_k, v|
      @log.info('Stopping...')
      v.write_('QUIT :Stopping...')
      v.running = false
    end
    @_log.stop
    exit
  end

  @plugs.each do |_, p|
    p.thrjoin
  end

  @_log.stop
end

Instance Attribute Details

#_logObject (readonly)

Returns the value of attribute _log.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def _log
  @_log
end

#confObject (readonly)

Returns the value of attribute conf.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def conf
  @conf
end

#dbsObject (readonly)

Returns the value of attribute dbs.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def dbs
  @dbs
end

#logObject (readonly)

Returns the value of attribute log.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def log
  @log
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def plugins
  @plugins
end

#plugsObject (readonly)

Returns the value of attribute plugs.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def plugs
  @plugs
end

#plugthrsObject (readonly)

Returns the value of attribute plugthrs.



3
4
5
# File 'lib/protonbot/bot.rb', line 3

def plugthrs
  @plugthrs
end

Instance Method Details

#configure { ... } ⇒ Object

Yields:

  • Do your config-loading here

See Also:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/protonbot/bot_conf.rb', line 22

def configure(&block)
  if block
    @configure_block = block
  else
    @configure_block.call if @configure_block
    set 'tsafe', true
    @conf['servers'].each do |k, v|
      @conf['servers'][k] =
        DEFAULT_SERVER_CONFIG.merge(@conf['global'].to_h).merge(v.to_h)
    end
  end
end

#gem(gemname) ⇒ Bot

Loads plugin from gem

Parameters:

  • gemname (String)

    Name of gem

Returns:

  • (Bot)

    self



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/protonbot/bot_plugins.rb', line 78

def gem(gemname)
  if Gem.loaded_specs[gemname]
    require gemname.gsub(/-/, '/')
    path = "#{Gem.loaded_specs[gemname].lib_dirs_glob.split(':')[0]}/" \
      "#{gemname.gsub(/-/, '/')}/plugin.rb"
    if File.exist? path
      pl = pluginr(path)
      raise ProtonBot::PluginError, "`#{path}` did not return plugin!" unless
        pl.instance_of? ProtonBot::Plugin
      @plugins[gemname] = pl
    else
      raise IOError, "No such file or directory: #{path}"
    end
  else
    raise ArgumentError, "No such gem: #{gemname}"
  end
  self
end

#gset(dat) ⇒ Object

Parameters:

  • dat (Hash)

    Sets config variable to given value

Raises:

  • (ArgumentError)


36
37
38
39
# File 'lib/protonbot/bot_conf.rb', line 36

def gset(dat)
  raise(ArgumentError, '`dat` is not hash!') unless dat.instance_of?(Hash)
  @conf = dat
end

#inspectObject



59
60
61
# File 'lib/protonbot/bot.rb', line 59

def inspect
  %(<#ProtonBot::Bot:#{object_id.to_s(16)}>)
end

#plugin(dat) ⇒ Bot

Loads given plugin by name

Parameters:

  • dat (String)

    Plugin name in file system

Returns:

  • (Bot)

    self



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/protonbot/bot_plugins.rb', line 56

def plugin(dat)
  pl = nil
  if dat.instance_of? Array
    dat.each do |i|
      pl = pluginr(File.expand_path("plugins/#{i}/plugin.rb"))
      raise ProtonBot::PluginError, "`plugins/#{i}/plugin.rb` did not return plugin!" unless
        pl.instance_of? ProtonBot::Plugin
    end
  elsif dat.instance_of? String
    pl = pluginr(File.expand_path("plugins/#{dat}/plugin.rb"))
    raise ProtonBot::PluginError, "`plugins/#{dat}/plugin.rb` did not return plugin!" unless
      pl.instance_of? ProtonBot::Plugin
  else
    raise ArgumentError, 'Unknown type of `dat` plugin! Use Array or String!'
  end
  @plugins[pl.name] = pl
  self
end

#plugin_loader(file = nil, &block) ⇒ Object

Use it to load external plugins. You cannot provide both arguments

Examples:

plugin_loader do
  plugin('foo') # Will load plugin from ./plugins/foo/main.rb
  gem('bar') # Will load plugin from path_to_gem_bar/lib/bar/plugin.rb
end
plugin_loader 'foo' # Will load plugin from ./plugins/foo/main.rb

Parameters:

  • file (String) (defaults to: nil)

    Filename

  • block (Proc)

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/protonbot/bot_plugins.rb', line 14

def plugin_loader(file = nil, &block)
  raise(ArgumentError, 'Both filename and load-block are nil!') if
    file.nil? && block.nil?

  raise(ArgumentError, 'Both filename and load-block are not nil!') if
    !file.nil? && !block.nil?

  if file
    @plugin_loader = lambda do
      load(File.expand_path('./' + file))
    end
  end

  @plugin_loader = block if block
  self
end

#pluginr(path) ⇒ Plugin

Loads plugin by path

Parameters:

  • path (String)

    Path

Returns:



100
101
102
103
104
105
106
107
108
# File 'lib/protonbot/bot_plugins.rb', line 100

def pluginr(path)
  if File.exist? path
    pl = eval(File.read(path), nil, %r{.*/(.+/.+)}.match(path)[1])
    pl.path = File.dirname(path)
    pl
  else
    raise IOError, 'No such file or directory: #{path}'
  end
end

#plugins_loadBot

Loads all plugins by calling ‘@plugin_loader` and initializing each plugin

Returns:

  • (Bot)

    self



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/protonbot/bot_plugins.rb', line 41

def plugins_load
  @plugins['core'] =
    pluginr "#{Gem.loaded_specs['protonbot'].lib_dirs_glob.split(':')[0]}/protonbot/core_plugin/plugin.rb"
  @plugin_loader.call if @plugin_loader
  @plugins.each do |k, v|
    v.bot = self
    v.log = @_log.wrap("?#{k}")
    v.launch
  end
  self
end

#plugins_unloadBot

Basically clears plugin hash

Returns:

  • (Bot)

    self



33
34
35
36
# File 'lib/protonbot/bot_plugins.rb', line 33

def plugins_unload
  @plugins = {}
  self
end

#set(key, val) ⇒ Object

Binds ‘val` to `key` in `@conf`

Parameters:

  • key (Symbol)
  • val (Object)


44
45
46
# File 'lib/protonbot/bot_conf.rb', line 44

def set(key, val)
  @conf[key.to_sym] = val
end