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

Diff of /smsdaemon/PluginManager.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 203 by torben, Fri Dec 19 07:03:34 2008 UTC revision 520 by torben, Sat Dec 26 23:01:01 2009 UTC
# Line 1  Line 1 
   
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"
# Line 12  Line 16 
16  #include "plugins/HostStatusPlugin.h"  #include "plugins/HostStatusPlugin.h"
17  #include "plugins/WeatherPlugin.h"  #include "plugins/WeatherPlugin.h"
18  #include "plugins/UrlTriggerPlugin.h"  #include "plugins/UrlTriggerPlugin.h"
19    #include "plugins/DelaySpamPlugin.h"
20    
21    
22    #include "AccessManager.h"
23    
24  typedef std::map<std::string, Plugin*>::iterator MapIterator;  typedef std::map<std::string, Plugin*>::iterator MapIterator;
25    
# Line 48  void PluginManager::AddPlugin(Plugin* pl Line 56  void PluginManager::AddPlugin(Plugin* pl
56          }          }
57  }  }
58    
59  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
60  {  {
61          static EchoPlugin echo;          if (pluginName == "echo")
62                    return new EchoPlugin();
63    
64            if (pluginName == "spam")
65                    return new SpamPlugin();
66    
67          static SpamPlugin spam;          if (pluginName == "tog")
68                    return new TogPlugin();
69    
70          static ShellExecPlugin wake("wake", "/usr/local/bin/wake", false, false);          if (pluginName == "status")
71          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);                  return new StatusPlugin();
72    
73          static TogPlugin tog;          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    
88            if (pluginName == "delayspam")
89                    return new DelaySpamPlugin();
90            
91            return 0;
92    }
93    
94    
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    void PluginManager::LoadPlugins()
105    {
106            Logger::logMessage("-------- PluginList --------");
107            std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
108    
109            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    
117          static StatusPlugin status;                  std::string argstr;
118    
119          static ListPlugin list;                  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          static HostStatusPlugin hoststatus;                  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          static WeatherPlugin weather;                  if (pl)
143                    {
144                            AddPlugin(pl);
145                            ParseCommonOptions(name,args);
146                    }
147                    else
148                            Logger::logMessage( std::string("Unknown plugin: ")+name);
149                    
150            }      
151    
         static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");  
152    
153          for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)          for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
154          {          {
# Line 77  void PluginManager::LoadPlugins() Line 158  void PluginManager::LoadPlugins()
158          }          }
159  }  }
160    
161    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    
169  Plugin* PluginManager::GetPlugin(const std::string& pluginname)  Plugin* PluginManager::GetPlugin(const std::string& pluginname)
170  {  {
171          return _plugins[ pluginname ];          static ProxyPlugin proxy;
172    
173            std::map<std::string,Plugin*>::iterator it = _plugins.find(pluginname );
174    
175            if (it != _plugins.end() )
176            {
177                    proxy.SetPlugin(it->second);
178                    return &proxy;
179            }
180            else
181            {
182                    return 0;
183            }
184  }  }
185    
186    

Legend:
Removed from v.203  
changed lines
  Added in v.520

  ViewVC Help
Powered by ViewVC 1.1.20