summaryrefslogtreecommitdiffstats
path: root/repost.rb
blob: 4d500004629a8fff3536558f554e07a4b1f0d259 (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
41
42
43
44
45
class Repost < EventMachine::IRC::Client

    attr_accessor :triggers
    attr :network

    def dispatch_raw_message(message = {})
        message[:scope] = {
            "network" => @network,
            "target" => message[:params][0],
            "person" => message[:prefix],
        }

        return if Config.lookup("ignore", [], message[:scope]).include? message[:prefix]
        self.triggers.each do |trigger|
            Config.lookup("plugins::disabled", [], message[:scope]).include? trigger[:name]

            # 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, name, &code)
        # Append a Hash containing the command it should be called at and the
        # and the code.
        self.triggers << {:command => command, :code => code, :name => name}
    end

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

end