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