Class: ModSpox::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/mod_spox/Pipeline.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ Pipeline

Create a new Pipeline



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mod_spox/Pipeline.rb', line 9

def initialize(pool)
    @pool = pool
    @hooks = Hash.new
    @plugins = Hash.new
    @admin = Models::Group.filter(:name => 'admin').first
    @populate_lock = Mutex.new
    populate_triggers
    populate_signatures
    hook(self, :populate_triggers, :Internal_TriggersUpdate)
    hook(self, :populate_signatures, :Internal_SignaturesUpdate)
end

Instance Method Details

#<<(message) ⇒ Object

message

Message to send down pipeline

Queues a message to send down pipeline



23
24
25
26
# File 'lib/mod_spox/Pipeline.rb', line 23

def <<(message)
    Logger.info("Message added to pipeline queue: #{message}")
    message_processor(message)
end

#clearObject

Clears all hooks from the pipeline (Commonly used when reloading plugins)



78
79
80
81
82
# File 'lib/mod_spox/Pipeline.rb', line 78

def clear
    Logger.info("All hooks have been cleared from pipeline")
    @hooks.clear
    @plugins.clear
end

#hook(object, method, type) ⇒ Object

plugin

Plugin to hook to pipeline

method

Plugin method pipeline should call to process message

type

Type of message the plugin wants to process

Hooks a plugin into the pipeline for a specific type of message



51
52
53
54
55
56
57
58
59
# File 'lib/mod_spox/Pipeline.rb', line 51

def hook(object, method, type)
    Logger.info("Object #{object.class.to_s} hooking into messages of type: #{type}")
    type = type.gsub(/::/, '_').to_sym unless type.is_a?(Symbol)
    method = method.to_sym unless method.is_a?(Symbol)
    name = object.class.to_s.gsub(/^.+:/, '')
    @hooks[type] = Hash.new unless @hooks.has_key?(type)
    @hooks[type][name.to_sym] = Array.new unless @hooks[type][name.to_sym].is_a?(Array)
    @hooks[type][name.to_sym] << {:object => object, :method => method}
end

#hook_plugin(plugin) ⇒ Object

plugin

Plugin to hook to pipeline

Hooks a plugin into the pipeline so it can be called directly when it matches a trigger



31
32
33
34
# File 'lib/mod_spox/Pipeline.rb', line 31

def hook_plugin(plugin)
    Logger.info("Plugin #{plugin.name} hooking into pipeline")
    @plugins[plugin.name.to_sym] = plugin
end

#populate_signatures(m = nil) ⇒ Object

Repopulate the active signatures list



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/mod_spox/Pipeline.rb', line 93

def populate_signatures(m=nil)
    @populate_lock.synchronize do
        @signatures = {}
        a = Models::Signature.filter(:enabled => false)
        Logger.warn("Killing #{a.count} signatures")
        a.destroy
        Models::Signature.all.each do |s|
            c = s.signature[0].chr.downcase
            if(c =~ /^[a-z]$/)
                type = c.to_sym
            elsif(c =~ /^[0-9]$/)
                type = :digit
            else
                type = :other
            end
            @signatures[type] = [] unless @signatures[type]
            unless @signatures.include?(s)
                @signatures[type] << s
            end
        end
    end
end

#populate_triggers(m = nil) ⇒ Object

Repopulate the active trigger list



85
86
87
88
89
90
# File 'lib/mod_spox/Pipeline.rb', line 85

def populate_triggers(m=nil)
    @populate_lock.synchronize do
        @triggers = []
        Models::Trigger.filter(:active => true).each{|t|@triggers << t.trigger}
    end
end

#unhook(object, method, type) ⇒ Object

plugin

Plugin to unhook from pipeline

type

Type of message the plugin no longer wants to process

This will remove the hook a plugin has for a specific message type



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mod_spox/Pipeline.rb', line 64

def unhook(object, method, type)
    Logger.info("Object #{object.class.to_s} unhooking from messages of type: #{type}")
    type = type.gsub(/::/, '_').to_sym unless type.is_a?(Symbol)
    name = object.class.to_s.gsub(/^.+:/, '').to_sym
    raise Exceptions::InvalidValue.new("Unknown hook type given: #{type.to_s}") unless @hooks.has_key?(type)
    raise Exceptions::InvalidValue.new("Unknown object hooked: #{name.to_s}") unless @hooks[type].has_key?(name)
    @hooks[type][name].each{|hook|
        @hooks[type][name].delete(hook) if hook[:method] == method
    }
    @hooks[type].delete(name) if @hooks[type][name].empty
    @hooks.delete(type) if @hooks[type].empty?
end

#unhook_plugin(plugin) ⇒ Object

plugin

Plugin to unhook from pipeline

Unhooks a plugin from the pipeline (This does not unhook it from the standard hooks)



39
40
41
42
43
44
45
# File 'lib/mod_spox/Pipeline.rb', line 39

def unhook_plugin(plugin)
    Logger.info("Plugin #{plugin.name} unhooking from pipeline")
    @plugins.delete(plugin.name.to_sym)
    @hooks.each_pair do |type, things|
        things.delete(plugin.name.to_sym) if things.has_key?(plugin.name.to_sym)
    end
end