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

Contents of /smsdaemon/PluginManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 521 - (show annotations) (download)
Sun Dec 27 18:01:36 2009 UTC (14 years, 4 months ago) by torben
File size: 4016 byte(s)
Deprecate the old spam plugin and make the new one the default

1 #include "PluginManager.h"
2
3 #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"
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 #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;
24
25 PluginManager::PluginManager()
26 {
27 }
28
29 PluginManager::~PluginManager()
30 {
31 }
32
33
34 void PluginManager::AddPlugin(Plugin* plugin)
35 {
36 if (plugin != 0)
37 {
38 if (plugin->IsHelper())
39 {
40 _helper_plugins.push_back(plugin);
41 }
42 else
43 {
44 std::string command = plugin->GetCommand();
45
46 if ( _plugins[ command ] == 0)
47 _plugins[ command ] = plugin;
48 else
49 Logger::logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);
50 }
51 }
52 else
53 {
54 Logger::logMessage("AddPlugin() -- cannot register a null pointer");
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()
101 {
102 Logger::logMessage("-------- PluginList --------");
103 std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
104
105 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 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 {
140 AddPlugin(pl);
141 ParseCommonOptions(name,args);
142 }
143 else
144 Logger::logMessage( std::string("Unknown plugin: ")+name);
145
146 }
147
148
149 for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
150 {
151 Plugin* pl = (*it).second;
152 if (pl != 0)
153 Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );
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)
166 {
167 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
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 }

  ViewVC Help
Powered by ViewVC 1.1.20