|
|
|
1 |
#include "PluginManager.h" |
#include "PluginManager.h" |
2 |
|
|
3 |
#include "Logger.h" |
#include "Logger.h" |
4 |
|
#include "ConfigFile.h" |
5 |
|
#include "Common.h" |
6 |
|
#include "Util.h" |
7 |
|
|
8 |
|
|
9 |
|
#include "ProxyPlugin.h" |
10 |
#include "plugins/EchoPlugin.h" |
#include "plugins/EchoPlugin.h" |
11 |
#include "plugins/SpamPlugin.h" |
#include "plugins/SpamPlugin.h" |
12 |
#include "plugins/ShellExecPlugin.h" |
#include "plugins/ShellExecPlugin.h" |
14 |
#include "plugins/StatusPlugin.h" |
#include "plugins/StatusPlugin.h" |
15 |
#include "plugins/ListPlugin.h" |
#include "plugins/ListPlugin.h" |
16 |
#include "plugins/HostStatusPlugin.h" |
#include "plugins/HostStatusPlugin.h" |
17 |
|
#include "plugins/WeatherPlugin.h" |
18 |
|
#include "plugins/UrlTriggerPlugin.h" |
19 |
|
|
20 |
|
|
21 |
|
#include "AccessManager.h" |
22 |
|
|
23 |
typedef std::map<std::string, Plugin*>::iterator MapIterator; |
typedef std::map<std::string, Plugin*>::iterator MapIterator; |
24 |
|
|
25 |
PluginManager::PluginManager() |
PluginManager::PluginManager() |
26 |
{ |
{ |
27 |
} |
} |
28 |
|
|
29 |
PluginManager::~PluginManager() |
PluginManager::~PluginManager() |
30 |
{ |
{ |
31 |
} |
} |
55 |
} |
} |
56 |
} |
} |
57 |
|
|
58 |
|
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 |
|
|
87 |
|
return 0; |
88 |
|
} |
89 |
|
|
90 |
|
|
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 |
void PluginManager::LoadPlugins() |
void PluginManager::LoadPlugins() |
101 |
{ |
{ |
102 |
static EchoPlugin echo; |
Logger::logMessage("-------- PluginList --------"); |
103 |
|
std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin"); |
104 |
|
|
105 |
static SpamPlugin spam; |
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 |
|
|
113 |
static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false); |
std::string argstr; |
|
static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true); |
|
114 |
|
|
115 |
static TogPlugin tog; |
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 |
static StatusPlugin status; |
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 |
static ListPlugin list; |
if (pl) |
139 |
|
{ |
140 |
|
AddPlugin(pl); |
141 |
|
ParseCommonOptions(name,args); |
142 |
|
} |
143 |
|
else |
144 |
|
Logger::logMessage( std::string("Unknown plugin: ")+name); |
145 |
|
|
146 |
|
} |
147 |
|
|
|
static HostStatusPlugin hoststatus; |
|
148 |
|
|
149 |
for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) |
for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it) |
150 |
{ |
{ |
151 |
Plugin* pl = (*it).second; |
Plugin* pl = (*it).second; |
152 |
if (pl != 0) |
if (pl != 0) |
154 |
} |
} |
155 |
} |
} |
156 |
|
|
157 |
|
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 |
|
|
165 |
Plugin* PluginManager::GetPlugin(const std::string& pluginname) |
Plugin* PluginManager::GetPlugin(const std::string& pluginname) |
166 |
{ |
{ |
167 |
return _plugins[ pluginname ]; |
static ProxyPlugin proxy; |
168 |
|
|
169 |
|
std::map<std::string,Plugin*>::iterator it = _plugins.find(pluginname ); |
170 |
|
|
171 |
|
if (it != _plugins.end() ) |
172 |
|
{ |
173 |
|
proxy.SetPlugin(it->second); |
174 |
|
return &proxy; |
175 |
|
} |
176 |
|
else |
177 |
|
{ |
178 |
|
return 0; |
179 |
|
} |
180 |
} |
} |
181 |
|
|
182 |
|
|