aboutsummaryrefslogtreecommitdiff
path: root/dinobot.rb
blob: db0440b1b19d21c89c29d11f06611a1e8127bb15 (plain) (blame)
1
2
3
4
5
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
61
62
63
64
65
66
67
68
69
70
71
72
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'timeout'

require_relative 'core/config'
require_relative 'core/irc'
require_relative 'core/logger'
require_relative 'core/messageinfo'
require_relative 'core/store'

module Dinobot
  class Bot
    attr_accessor :trigger
    attr_reader :server, :port, :nick, :pass, :modules, :channels

    def initialize(server, port, nick, pass=nil, &block)
      @server = server
      @port = port
      @nick = nick
      @pass = pass

      @irc = Dinobot::Core::IRC.new(@server, @port, @nick, @pass)
      @config = Dinobot::Core::Config.instance
      @logger = Dinobot::Core::Logger.instance
      @aliases = Dinobot::Core::Store.new('data/aliases')

      @modules = Hash.new
      @channels = Array.new

      instance_eval(&block) if block_given?
    end

    def run
      @irc.connect unless @irc.connected?

      @channels.each do |channel|
        @irc.join channel
      end

      while line = @irc.gets
        process_line(line)
      end

      @irc.disconnect
      @logger.info 'Disconnected.'
    end

    def say(channel, message)
      @irc.privmsg(channel, message)
    end

    def join(channel)
      @channels << channel unless @channels.include?(channel)

      @irc.join(channel) if @irc.connected?
    end

    def part(channel)
      @channels.delete(channel)

      @irc.part(channel)
    end

    def quit(message)
      @irc.quit(message)
    end

    def load_module(mod)
      mod = mod.downcase.intern
      @logger.info "Loading module: #{mod}"

      begin
        load "module/#{mod}.rb"

        m = Dinobot::Module.const_get(
          Dinobot::Module.constants.find { |x| x.downcase == mod })
        @modules[mod] = m.new(self)

        @logger.info "Loaded module: #{mod} (#{m})"
      rescue LoadError, StandardError => e
        @logger.error "Failed to load module: #{mod} (#{e})"
      end
    end

    def unload_module(mod)
      mod = mod.downcase.intern
      @logger.info "Unloading module: #{mod}"

      begin
        raise 'module not loaded' unless @modules.has_key?(mod)

        @modules.delete(mod)
        m = Dinobot::Module.send(:remove_const,
          Dinobot::Module.constants.find { |x| x.downcase == mod })

        @logger.info "Unloaded module: #{mod} (#{m})"
      rescue => e
        @logger.error "Failed to unload module: #{mod} (#{e})"
      end
    end

    def add_alias(from, to)
      @aliases.data[:global] = Hash.new unless @aliases.data[:global]

      @aliases.data[:global][from] = to
      @aliases.save
    end

    def remove_alias(from)
      return unless @aliases.data[:global]

      @aliases.data[:global].delete(from)
      @aliases.save
    end

    private

    def process_line(line)
      Thread.new do
        begin
          Timeout.timeout(30) do
            process_line_thread(line.chomp)
          end
        rescue => e
          @logger.error "Error parsing line. (#{e})"
          @logger.indent *e.backtrace
        end
      end
    end

    def process_line_thread(line)
      @irc.pong line[5..-1] if line =~ /\APING /

      if line =~ /\A(\S+) PRIVMSG (\S+) :(.*)/
        user, channel, message = line.scan(/\A(\S+) PRIVMSG (\S+) :(.*)/).first
        m = Dinobot::Core::MessageInfo.new(user, channel, message)

        return unless message =~
          /\A#{Regexp.escape(@config.data[:trigger][:global])}/

        command = message
          .sub(/\A#{Regexp.escape(@config.data[:trigger][:global])}/, '')

        exec_command(m, command)
        process_response(m) if m.response?
      end
    end

    def exec_command(m, command)
      # FIXME: Improve and add debug output.
      if @aliases.data[:global]
        @aliases.data[:global].each do |k, v|
          command.sub!(/\A#{Regexp.escape(k)}\b/, v)
        end
      end

      command, remainder = command.split(' | ', 2)
      mod = command.scan(/\A\S+/).first.downcase

      return unless @modules.keys.map { |x| x.to_s }.include?(mod)
      mod = mod.intern

      if m.response?
        prev = m.response
        m.response = []

        prev.each do |x|
          if x.first == :say
            @modules[mod].call(m, "#{command} #{x[2]}")
          else
            m.respond x
          end
        end
      else
        @modules[mod].call(m, command)
      end

      exec_command(m, remainder) if remainder
    end

    def process_response(m)
      m.response.each do |x|
        @logger.info "Executing method: #{x.inspect}" if @config.data[:debug]
        send(*x)
      end
    end
  end
end