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.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mitten.rb', line 24

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



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

def self.boot
  new.boot
end

Instance Method Details

#bootObject



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

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



50
51
52
53
# File 'lib/mitten.rb', line 50

def connect
  puts "TCPSocket open to #{@server['host']}:#{@server['port']}"
  TCPSocket.open(@server['host'], @server['port'])
end

#instance_categorize(plugins) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/mitten.rb', line 134

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



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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mitten.rb', line 36

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mitten.rb', line 99

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)
    m.constants.each do |name|
      break unless plugin_configs.has_key? name
      const = m.const_get(name)
      if const.is_a? Class 
        plugin[name] = {
          :class   => const,
          :file    => file,
          :configs => plugin_configs[name]
        }
      end
    end
    class_tables.update(plugin)
  end

  plugins = instantiation(class_tables)
  instance_categorize(plugins)
end

#run_pluginsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mitten.rb', line 74

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
        end
      end
    end
  )

  @notify_plugins.each do |plugin|
    plugin.before_hook
    threads.push(Thread.fork { plugin.notify })
  end

  threads.each { |t| t.join }
end