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

Annotation of /smsdaemon/PluginManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 217 - (hide annotations) (download)
Tue Dec 23 14:20:43 2008 UTC (15 years, 4 months ago) by torben
File size: 3953 byte(s)
Solved ToDo item:
'- Create a filtering method:
-  Incoming: which phonenumbers are allowed to invoke which plugins
-  Incoming could be solved with a PluginProxy() which intercepts the ExecutePlugin calls (only 
PluginManager needs to know about this one)'

Move the Access related items to AccessManager.*


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

  ViewVC Help
Powered by ViewVC 1.1.20