Class: ProtonBot::Bot
- Inherits:
-
Object
- Object
- ProtonBot::Bot
- 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
-
#_log ⇒ Object
readonly
Returns the value of attribute _log.
-
#conf ⇒ Object
readonly
Returns the value of attribute conf.
-
#dbs ⇒ Object
readonly
Returns the value of attribute dbs.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
-
#plugs ⇒ Object
readonly
Returns the value of attribute plugs.
-
#plugthrs ⇒ Object
readonly
Returns the value of attribute plugthrs.
Config collapse
- #configure { ... } ⇒ Object
- #gset(dat) ⇒ Object
-
#set(key, val) ⇒ Object
Binds ‘val` to `key` in `@conf`.
Plugins collapse
-
#gem(gemname) ⇒ Bot
Loads plugin from gem.
-
#plugin(dat) ⇒ Bot
Loads given plugin by name.
-
#plugin_loader(file = nil, &block) ⇒ Object
Use it to load external plugins.
-
#pluginr(path) ⇒ Plugin
Loads plugin by path.
-
#plugins_load ⇒ Bot
Loads all plugins by calling ‘@plugin_loader` and initializing each plugin.
-
#plugins_unload ⇒ Bot
Basically clears plugin hash.
Instance Method Summary collapse
-
#initialize { ... } ⇒ Bot
constructor
A new instance of Bot.
- #inspect ⇒ Object
Constructor Details
#initialize { ... } ⇒ Bot
Returns a new instance of 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.('./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
#_log ⇒ Object (readonly)
Returns the value of attribute _log.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def _log @_log end |
#conf ⇒ Object (readonly)
Returns the value of attribute conf.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def conf @conf end |
#dbs ⇒ Object (readonly)
Returns the value of attribute dbs.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def dbs @dbs end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def log @log end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def plugins @plugins end |
#plugs ⇒ Object (readonly)
Returns the value of attribute plugs.
3 4 5 |
# File 'lib/protonbot/bot.rb', line 3 def plugs @plugs end |
#plugthrs ⇒ Object (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
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
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
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 |
#inspect ⇒ Object
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
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.("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.("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
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.('./' + file)) end end @plugin_loader = block if block self end |
#pluginr(path) ⇒ Plugin
Loads plugin by path
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_load ⇒ Bot
Loads all plugins by calling ‘@plugin_loader` and initializing each plugin
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_unload ⇒ Bot
Basically clears plugin hash
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`
44 45 46 |
# File 'lib/protonbot/bot_conf.rb', line 44 def set(key, val) @conf[key.to_sym] = val end |