summaryrefslogtreecommitdiffstats
path: root/repost.rb
blob: b8e7bb3838af85afb25c19dcf2f1c2313a624778 (plain)
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
class Repost < EventMachine::IRC::Client

    attr_accessor :triggers

    def dispatch_raw_message(message = {})
        return if Config[:ignore].include?(message[:prefix])
        self.triggers.each do |trigger|
            # Having per-command lists of blocks of code to call would be
            # faster, but it's not a problem for now. Might refactor this loop
            # and register_trigger() method if it comes to that.
            begin
                trigger[:code].call(message) if message[:command] == trigger[:command]
            rescue => e
                puts "inspect!"
                puts e.inspect
                puts "backtrace!"
                puts e.backtrace
            end
        end
    end

    def register_trigger(command, &code)
        # Append a Hash containing the command it should be called at and the
        # and the code.
        self.triggers << {:command => command, :code => code}
    end

    def load_plugins
        self.triggers = []
        Config[:plugins].each do |plugin|
            puts "Loading plugin #{plugin}"
                load File.dirname($0) + '/plugins/' + plugin + '.rb'
        end if not Config[:plugins].nil?
    end

    def load_config(file = "config.rb")
        load File.dirname($0) + "/" + file
    end

end