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
58
59
60
# 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')

  Dir.mkdir('dbs/') unless File.exist?(File.expand_path('./dbs/'))
  @db_cross = Heliodor::DB.new("dbs/pb-cross.db", true)

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

  @dbs = {}
  @plugs = {}
  @plugthrs = {}
  @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

#coreObject (readonly)

Returns the value of attribute core.



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

def core
  @core
end

#db_crossObject (readonly)

Returns the value of attribute db_cross.



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

def db_cross
  @db_cross
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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/protonbot/bot_plugins.rb', line 88

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
      @parr << 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



62
63
64
# File 'lib/protonbot/bot.rb', line 62

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/protonbot/bot_plugins.rb', line 66

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
  @parr << pl
  self
end

#plugin_array(arr) ⇒ Bot

Loads plugins by array of strings. If first char of item is period, then plugin is loaded from gem

Parameters:

  • plugins (Array<String>)

Returns:

  • (Bot)

    self



111
112
113
114
115
116
117
118
119
# File 'lib/protonbot/bot_plugins.rb', line 111

def plugin_array(arr)
  arr.each do |pl|
    if pl[0] == '.'
      gem pl[1,pl.length-1]
    else
      plugin pl
    end
  end
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:



124
125
126
127
128
129
130
131
132
# File 'lib/protonbot/bot_plugins.rb', line 124

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/protonbot/bot_plugins.rb', line 42

def plugins_load
  core = pluginr "#{Gem.loaded_specs['protonbot'].lib_dirs_glob.split(':')[0]}/protonbot/core_plugin/plugin.rb"
  core.bot  = self
  core.core = core
  core.launch
  @core = core
  @plugins[core.name] = core
  core.log = @_log.wrap("?#{core.name}")
  core.log.info("Started plugin `#{core.name} v#{core.version}` successfully!")
  @plugin_loader.call if @plugin_loader
  @parr.each do |pl|
    pl.bot  = self
    pl.core = @plugins[core.name]
    pl.launch
    @plugins[pl.name] = pl
    pl.log = @_log.wrap("?#{pl.name}")
    pl.log.info("Started plugin `#{pl.name} v#{pl.version}` successfully!")
  end
  self
end

#plugins_unloadBot

Basically clears plugin hash

Returns:

  • (Bot)

    self



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

def plugins_unload
  @parr    = []
  @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