Class: Mitten::Bot
- Inherits:
-
Net::IRC::Client
- Object
- Net::IRC::Client
- Mitten::Bot
- Defined in:
- lib/mitten.rb
Class Method Summary collapse
Instance Method Summary collapse
- #boot ⇒ Object
- #connect ⇒ Object
-
#initialize ⇒ Bot
constructor
A new instance of Bot.
- #instance_categorize(plugins) ⇒ Object
- #instantiation(class_tables) ⇒ Object
- #load_configs ⇒ Object
- #load_plugins(plugin_dir, plugin_configs) ⇒ Object
- #run_plugins ⇒ Object
Constructor Details
#initialize ⇒ Bot
Returns a new instance of Bot.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mitten.rb', line 23 def initialize load_configs super(@server['host'], @server['port'], { :nick => @server['nick'], :user => @server['user'], :real => @server['real'], :pass => @server['pass'], :channel => @server['channel'] }) end |
Class Method Details
.boot ⇒ Object
19 20 21 |
# File 'lib/mitten.rb', line 19 def self.boot new.boot end |
Instance Method Details
#boot ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mitten.rb', line 54 def boot begin @socket = connect @socket.gets post(PASS, @opts.pass) if @opts.pass post(NICK, @opts.nick) post(USER, @opts.user, '0', '*', @opts.real) post(JOIN, @opts.channel) if @opts.channel run_plugins rescue Exception => e post(NOTICE, @opts.channel, "#{e.class} #{e.to_s}") if @opts.channel @log.error "#{e.class} #{e.to_s}" ensure finish end end |
#connect ⇒ Object
49 50 51 52 |
# File 'lib/mitten.rb', line 49 def connect puts "TCPSocket open to #{@server['host']}:#{@server['port']}" TCPSocket.open(@server['host'], @server['port']) end |
#instance_categorize(plugins) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/mitten.rb', line 136 def instance_categorize(plugins) @response_plugins = [] @notify_plugins = [] plugins.each do |plugin| @response_plugins << plugin if plugin.respond_to? 'on_privmsg' @notify_plugins << plugin if plugin.respond_to? 'main' end end |
#instantiation(class_tables) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/mitten.rb', line 126 def instantiation(class_tables) plugins = [] class_tables.each do |name, plugin| plugins << plugin[:class].new(plugin[:configs], @opts, @socket) puts "Plugin: #{name} is loaded" end plugins end |
#load_configs ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mitten.rb', line 35 def load_configs @mode = 'production' config_file = DEFAULT_CONFIG_FILE_PATH ARGV. do |o| o.on('-c', "--config-file CONFIG_FILE", " (default: #{config_file})") { |v| config_file = v } o.on('-d', "--development") { |v| @mode = 'development' } o.parse! end @config = OpenStruct.new(YAML.load_file config_file) @server = @config.method(@mode).call end |
#load_plugins(plugin_dir, plugin_configs) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mitten.rb', line 102 def load_plugins(plugin_dir, plugin_configs) class_tables = {} Pathname.glob("#{plugin_dir}/*.rb") do |file| plugin = {} m = Module.new m.module_eval(file.read, file.to_s) m.constants.each do |name| break unless plugin_configs.has_key? name.to_s const = m.const_get(name) if const.is_a? Class plugin[name] = { :class => const, :file => file, :configs => plugin_configs[name.to_s] } end end class_tables.update(plugin) end plugins = instantiation(class_tables) instance_categorize(plugins) end |
#run_plugins ⇒ Object
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 |
# File 'lib/mitten.rb', line 73 def run_plugins threads = [] load_plugins(@server['plugin_dir'], @config.plugins) threads.push( Thread.fork do while line = @socket.gets = Message.parse(line) if .command.upcase == 'PRIVMSG' @response_plugins.each do |plugin| plugin.response(.prefix, [0], [1]) end else next if () === true name = "on_#{(COMMANDS[.command.upcase] || .command).downcase}" send(name, ) if respond_to?(name) end end end ) @notify_plugins.each do |plugin| plugin.before_hook threads.push(Thread.fork { plugin.notify }) end threads.each { |t| t.join } end |