Class: ModSpox::PluginManager

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

Defined Under Namespace

Classes: PluginFileNotFound, PluginMissing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline) ⇒ PluginManager

pipeline

Pipeline for messages

Create new PluginManager



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mod_spox/PluginManager.rb', line 17

def initialize(pipeline)
    @plugins = Hash.new
    @pipeline = pipeline
    @pipeline.hook(self, :load_plugin, :Internal_PluginLoadRequest)
    @pipeline.hook(self, :unload_plugin, :Internal_PluginUnloadRequest)
    @pipeline.hook(self, :reload_plugins, :Internal_PluginReload)
    @pipeline.hook(self, :send_modules, :Internal_PluginModuleRequest)
    @pipeline.hook(self, :plugin_request, :Internal_PluginRequest)
    @plugins_module = Module.new
    @plugin_lock = Mutex.new
    load_plugins
end

Instance Attribute Details

#pluginsObject (readonly)

Hash of plugins. Defined by class name symbol (i.e. Trivia class: plugins)



13
14
15
# File 'lib/mod_spox/PluginManager.rb', line 13

def plugins
  @plugins
end

Instance Method Details

#destroy_pluginsObject

Destroys plugins



49
50
51
# File 'lib/mod_spox/PluginManager.rb', line 49

def destroy_plugins
    unload_plugins
end

#load_plugin(message) ⇒ Object

message

Messages::Internal::PluginLoadRequest

Loads a plugin



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

def load_plugin(message)
    begin
        path = !message.name ? "#{BotConfig[:userpluginpath]}/#{message.path.gsub(/^.+\//, '')}" : "#{BotConfig[:userpluginpath]}/#{message.name}"
        begin
            File.symlink(message.path, path)
        rescue NotImplementedError => boom
            FileUtils.copy(message.path, path)
        end
        do_load(path)
        @pipeline << Messages::Internal::PluginLoadResponse.new(message.requester, true)
        Logger.log("Loaded new plugin: #{message.path}", 10)
    rescue Object => boom
        Logger.log("Failed to load plugin: #{message.path} Reason: #{boom}", 10)
        @pipeline << Messages::Internal::PluginLoadResponse.new(message.requester, false)
    end
    @pipeline << Messages::Internal::SignaturesUpdate.new
end

#plugin_request(message) ⇒ Object

message

Messages::Internal::PluginRequest

Returns a plugin to requesting object



101
102
103
104
105
106
107
108
# File 'lib/mod_spox/PluginManager.rb', line 101

def plugin_request(message)
    if(@plugins.has_key?(message.plugin))
        response = Messages::Internal::PluginResponse.new(message.requester, @plugins[message.plugin])
    else
        response = Messages::Internal::PluginResponse.new(message.requester, nil)
    end
    @pipeline << response
end

#reload_plugins(message = nil) ⇒ Object

message

Messages::Internal::PluginReload

Destroys and reinitializes plugins



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mod_spox/PluginManager.rb', line 32

def reload_plugins(message=nil)
    @plugin_lock.synchronize do
        if(message.fresh && message.stale)
            do_unload(message.stale)
            FileUtils.remove_file(message.stale)
            FileUtils.copy(message.fresh, BotConfig[:userpluginpath])
            do_load(message.stale)
            Logger.log("Completed reload of plugin: #{message.stale}")
        else
            unload_plugins
            load_plugins
        end
        @pipeline << Messages::Internal::SignaturesUpdate.new
    end
end

#send_modules(message) ⇒ Object

message

Messages::Internal::PluginModuleRequest

Sends the plugins module to the requester



95
96
97
# File 'lib/mod_spox/PluginManager.rb', line 95

def send_modules(message)
    @pipeline << Messages::Internal::PluginModuleResponse.new(message.requester, @plugins_module)
end

#unload_plugin(message) ⇒ Object

message

Messages::Internal::PluginUnloadRequest

Unloads a plugin



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mod_spox/PluginManager.rb', line 75

def unload_plugin(message)
    begin
        do_unload(message.path)
        unless(File.symlink?(message.path))
            unless(message.name.nil?)
                FileUtils.copy(message.path, "#{BotConfig[:userpluginpath]}/#{message.name}")
            end
        end
        File.delete(message.path)
        @pipeline << Messages::Internal::PluginUnloadResponse.new(message.requester, true)
        Logger.log("Unloaded plugin: #{message.path}", 10)
    rescue Object => boom
        Logger.log("Failed to unload plugin: #{message.path} Reason: #{boom}", 10)
        @pipeline << Messages::Internal::PluginUnloadResponse.new(message.requester, false)
    end
    @pipeline << Messages::Internal::SignaturesUpdate.new
end