/[projects]/smsdaemon/PluginManager.cpp
ViewVC logotype

Annotation of /smsdaemon/PluginManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 205 - (hide annotations) (download)
Fri Dec 19 22:29:25 2008 UTC (15 years, 5 months ago) by torben
File size: 3629 byte(s)
Enable selection and configuration of loaded plugins to be specified in the configuration file

1 torben 196
2 torben 26 #include "PluginManager.h"
3    
4 torben 157 #include "Logger.h"
5 torben 205 #include "ConfigFile.h"
6     #include "Common.h"
7     #include "Util.h"
8 torben 26
9 torben 132 #include "plugins/EchoPlugin.h"
10     #include "plugins/SpamPlugin.h"
11     #include "plugins/ShellExecPlugin.h"
12     #include "plugins/TogPlugin.h"
13     #include "plugins/StatusPlugin.h"
14     #include "plugins/ListPlugin.h"
15 torben 137 #include "plugins/HostStatusPlugin.h"
16 torben 188 #include "plugins/WeatherPlugin.h"
17 torben 193 #include "plugins/UrlTriggerPlugin.h"
18 torben 26
19     typedef std::map<std::string, Plugin*>::iterator MapIterator;
20    
21     PluginManager::PluginManager()
22     {
23     }
24 torben 196
25 torben 26 PluginManager::~PluginManager()
26     {
27     }
28    
29    
30 torben 28 void PluginManager::AddPlugin(Plugin* plugin)
31     {
32 torben 35 if (plugin != 0)
33     {
34 torben 184 if (plugin->IsHelper())
35     {
36     _helper_plugins.push_back(plugin);
37     }
38     else
39     {
40     std::string command = plugin->GetCommand();
41 torben 35
42 torben 184 if ( _plugins[ command ] == 0)
43     _plugins[ command ] = plugin;
44     else
45     Logger::logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);
46     }
47 torben 35 }
48     else
49     {
50 torben 157 Logger::logMessage("AddPlugin() -- cannot register a null pointer");
51 torben 35 }
52 torben 28 }
53    
54 torben 205 Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
55     {
56     if (pluginName == "echo")
57     return new EchoPlugin();
58    
59     if (pluginName == "spam")
60     return new SpamPlugin();
61    
62     if (pluginName == "tog")
63     return new TogPlugin();
64    
65     if (pluginName == "status")
66     return new StatusPlugin();
67    
68     if (pluginName == "list")
69     return new ListPlugin();
70    
71     if (pluginName == "hoststatus")
72     return new HostStatusPlugin();
73    
74     if (pluginName == "weather")
75     return new WeatherPlugin();
76    
77     if (pluginName == "shellexec")
78     return new ShellExecPlugin(args);
79    
80     if (pluginName == "urltrigger")
81     return new UrlTriggerPlugin(args);
82    
83     return 0;
84     }
85    
86 torben 26 void PluginManager::LoadPlugins()
87     {
88 torben 205 /*
89 torben 28
90 torben 203 static ShellExecPlugin wake("wake", "/usr/local/bin/wake", false, false);
91 torben 107 static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);
92 torben 35
93 torben 49
94 torben 101
95 torben 114
96 torben 205 static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");
97     */
98 torben 137
99 torben 205 std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
100 torben 188
101 torben 205 for (unsigned i=0; i<pluginlist.size(); i++)
102     {
103     std::string current = pluginlist[i];
104    
105     std::string name;
106     unsigned pos = current.find(' ');
107     std::map<std::string,std::string> args;
108 torben 193
109 torben 205 std::string argstr;
110    
111     if (pos == std::string::npos)
112     {
113     name = current;
114     }
115     else
116     {
117     name = Util::str_trim(current.substr(0,pos));
118     argstr = Util::str_trim(current.substr(pos+1,1024));
119     args = ConfigHelper::ParseArguments(argstr);
120     }
121    
122     Plugin* pl = 0;
123     try
124     {
125     pl = CreatePlugin(name, args );
126     }
127     catch (std::exception& e)
128     {
129     Logger::logMessage(std::string("Failed to load plugin ") + name + " with args: " + argstr);
130     Logger::logMessage(std::string("Reason: ") + e.what());
131     continue;
132     }
133    
134     if (pl)
135     AddPlugin(pl);
136     else
137     Logger::logMessage( std::string("Unknown plugin: ")+name);
138    
139     }
140    
141    
142    
143 torben 196 for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
144 torben 26 {
145     Plugin* pl = (*it).second;
146 torben 35 if (pl != 0)
147 torben 157 Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );
148 torben 26 }
149     }
150    
151 torben 205 void PluginManager::DestroyPlugins()
152     {
153     }
154    
155 torben 26 Plugin* PluginManager::GetPlugin(const std::string& pluginname)
156     {
157     return _plugins[ pluginname ];
158     }
159    
160    
161     std::vector<Plugin*> PluginManager::GetPluginList()
162     {
163     typedef std::map<std::string, Plugin*>::iterator MapIterator;
164     std::vector<Plugin*> plugin_list;
165    
166     for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
167     {
168     Plugin* pl = (*it).second;
169     plugin_list.push_back(pl);
170     }
171    
172    
173     return plugin_list;
174     }

  ViewVC Help
Powered by ViewVC 1.1.20