--- smsdaemon/PluginManager.cpp 2008/12/17 23:43:26 193 +++ smsdaemon/PluginManager.cpp 2008/12/21 17:42:30 207 @@ -1,7 +1,9 @@ - #include "PluginManager.h" #include "Logger.h" +#include "ConfigFile.h" +#include "Common.h" +#include "Util.h" #include "plugins/EchoPlugin.h" #include "plugins/SpamPlugin.h" @@ -18,7 +20,7 @@ PluginManager::PluginManager() { } - + PluginManager::~PluginManager() { } @@ -48,28 +50,96 @@ } } -void PluginManager::LoadPlugins() +Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map& args) { - static EchoPlugin echo; + if (pluginName == "echo") + return new EchoPlugin(); - static SpamPlugin spam; + if (pluginName == "spam") + return new SpamPlugin(); - static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false); - static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true); + 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(); - static TogPlugin tog; + if (pluginName == "shellexec") + return new ShellExecPlugin(args); - static StatusPlugin status; + if (pluginName == "urltrigger") + return new UrlTriggerPlugin(args); + + return 0; +} + +void PluginManager::LoadPlugins() +{ +/* + + static ShellExecPlugin wake("wake", "/usr/local/bin/wake", false, false); + static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true); - static ListPlugin list; - static HostStatusPlugin hoststatus; - static WeatherPlugin weather; static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php"); +*/ + + 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); + else + Logger::logMessage( std::string("Unknown plugin: ")+name); + + } - for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) + + for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) { Plugin* pl = (*it).second; if (pl != 0) @@ -77,6 +147,10 @@ } } +void PluginManager::DestroyPlugins() +{ +} + Plugin* PluginManager::GetPlugin(const std::string& pluginname) { return _plugins[ pluginname ];