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