--- smsdaemon/PluginManager.cpp 2008/06/10 14:38:59 35 +++ smsdaemon/PluginManager.cpp 2008/12/19 22:29:25 205 @@ -1,19 +1,27 @@ - -#include "PluginManager.h" - -#include "common.h" -#include "EchoPlugin.h" -#include "SpamPlugin.h" -#include "ShellExecPlugin.h" +#include "PluginManager.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() { } @@ -21,42 +29,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 ShellExecPlugin wake("wake", "/home/torben/bin/wake"); + static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php"); +*/ + + 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; + } - for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) + if (pl) + AddPlugin(pl); + else + Logger::logMessage( std::string("Unknown plugin: ")+name); + + } + + + + 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 ];