--- smsdaemon/PluginManager.cpp 2008/06/16 16:41:18 101 +++ smsdaemon/PluginManager.cpp 2008/12/21 17:42:30 207 @@ -1,20 +1,26 @@ - #include "PluginManager.h" -#include "common.h" - -#include "EchoPlugin.h" -#include "SpamPlugin.h" -#include "ShellExecPlugin.h" -#include "TogPlugin.h" -#include "StatusPlugin.h" +#include "Logger.h" +#include "ConfigFile.h" +#include "Common.h" +#include "Util.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" typedef std::map::iterator MapIterator; PluginManager::PluginManager() { } - + PluginManager::~PluginManager() { } @@ -22,45 +28,129 @@ void PluginManager::AddPlugin(Plugin* plugin) { - Common* cmn = Common::instance(); - if (plugin != 0) { - std::string command = plugin->GetCommand(); - - if ( _plugins[ command ] == 0) - _plugins[ command ] = plugin; + if (plugin->IsHelper()) + { + _helper_plugins.push_back(plugin); + } else - cmn->logMessage( std::string("AddPlugin() -- already have a plugin called ") + command); + { + 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 { - cmn->logMessage("AddPlugin() -- cannot register a null pointer"); + 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::LoadPlugins() { - Common* cmn = Common::instance(); +/* + + 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 EchoPlugin echo; - static SpamPlugin spam; + static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php"); +*/ - static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false); + Logger::logMessage("-------- PluginList --------"); + std::vector pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin"); - static TogPlugin tog; + 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); + + } - static StatusPlugin status; - 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) - cmn->logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() ); + Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() ); } } +void PluginManager::DestroyPlugins() +{ +} + Plugin* PluginManager::GetPlugin(const std::string& pluginname) { return _plugins[ pluginname ];