#include "PluginManager.h" #include "Logger.h" #include "ConfigFile.h" #include "Common.h" #include "Util.h" #include "ProxyPlugin.h" #include "plugins/EchoPlugin.h" #include "plugins/SpamPlugin.h" #include "plugins/ShellExecPlugin.h" #include "plugins/TogPlugin.h" #include "plugins/StatusPlugin.h" #include "plugins/ListPlugin.h" #include "plugins/HostStatusPlugin.h" #include "plugins/WeatherPlugin.h" #include "plugins/UrlTriggerPlugin.h" #include "AccessManager.h" typedef std::map::iterator MapIterator; PluginManager::PluginManager() { } PluginManager::~PluginManager() { } void PluginManager::AddPlugin(Plugin* plugin) { if (plugin != 0) { if (plugin->IsHelper()) { _helper_plugins.push_back(plugin); } else { std::string command = plugin->GetCommand(); if ( _plugins[ command ] == 0) _plugins[ command ] = plugin; else Logger::logMessage( std::string("AddPlugin() -- already have a plugin called ") + command); } } else { Logger::logMessage("AddPlugin() -- cannot register a null pointer"); } } Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map& args) { if (pluginName == "echo") return new EchoPlugin(); if (pluginName == "spam") return new SpamPlugin(); if (pluginName == "tog") return new TogPlugin(); if (pluginName == "status") return new StatusPlugin(); if (pluginName == "list") return new ListPlugin(); if (pluginName == "hoststatus") return new HostStatusPlugin(); if (pluginName == "weather") return new WeatherPlugin(); if (pluginName == "shellexec") return new ShellExecPlugin(args); if (pluginName == "urltrigger") return new UrlTriggerPlugin(args); return 0; } void PluginManager::DestroyPlugins() { for (std::map::iterator it=_plugins.begin(); it!=_plugins.end(); ++it) { delete it->second; } _plugins.clear(); } void PluginManager::LoadPlugins() { Logger::logMessage("-------- PluginList --------"); std::vector pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin"); for (unsigned i=0; i args; std::string argstr; if (pos == std::string::npos) { name = current; } else { name = Util::str_trim(current.substr(0,pos)); argstr = Util::str_trim(current.substr(pos+1,1024)); args = ConfigHelper::ParseArguments(argstr); } Plugin* pl = 0; try { pl = CreatePlugin(name, args ); } catch (std::exception& e) { Logger::logMessage(std::string("Failed to load plugin ") + name + " with args: " + argstr); Logger::logMessage(std::string("Reason: ") + e.what()); continue; } if (pl) { AddPlugin(pl); ParseCommonOptions(name,args); } else Logger::logMessage( std::string("Unknown plugin: ")+name); } for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) { Plugin* pl = (*it).second; if (pl != 0) Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() ); } } void PluginManager::ParseCommonOptions(const std::string& pluginName, std::map& args) { if (args["privileged"] == "1") { AccessManager::AddPrivPlugin(pluginName); } } Plugin* PluginManager::GetPlugin(const std::string& pluginname) { static ProxyPlugin proxy; std::map::iterator it = _plugins.find(pluginname ); if (it != _plugins.end() ) { proxy.SetPlugin(it->second); return &proxy; } else { return 0; } } std::vector PluginManager::GetPluginList() { typedef std::map::iterator MapIterator; std::vector plugin_list; for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) { Plugin* pl = (*it).second; plugin_list.push_back(pl); } return plugin_list; }