Class: Mitten::Bot

Inherits:
Net::IRC::Client
  • Object
show all
Defined in:
lib/mitten.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBot

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

.bootObject



19
20
21
# File 'lib/mitten.rb', line 19

def self.boot
  new.boot
end

Instance Method Details

#bootObject



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

#connectObject



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_configsObject



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.options 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_pluginsObject



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 = Message.parse(line)
        if message.command.upcase == 'PRIVMSG'
          @response_plugins.each do |plugin|
            plugin.response(message.prefix, message[0], message[1])
          end
        else
          next if on_message(message) === true
          name = "on_#{(COMMANDS[message.command.upcase] || message.command).downcase}"
          send(name, message) 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