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

Diff of /smsdaemon/PluginManager.cpp

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

revision 193 by torben, Wed Dec 17 23:43:26 2008 UTC revision 521 by torben, Sun Dec 27 18:01:36 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 13  Line 17 
17  #include "plugins/WeatherPlugin.h"  #include "plugins/WeatherPlugin.h"
18  #include "plugins/UrlTriggerPlugin.h"  #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  }  }
# Line 48  void PluginManager::AddPlugin(Plugin* pl Line 55  void PluginManager::AddPlugin(Plugin* pl
55          }          }
56  }  }
57    
58  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
59  {  {
60          static EchoPlugin echo;          if (pluginName == "echo")
61                    return new EchoPlugin();
62    
63            if (pluginName == "spam")
64                    return new SpamPlugin();
65    
66          static SpamPlugin spam;          if (pluginName == "tog")
67                    return new TogPlugin();
68    
69          static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false);          if (pluginName == "status")
70          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);                  return new StatusPlugin();
71    
72          static TogPlugin tog;          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          static StatusPlugin status;                  std::string argstr;
114    
115          static ListPlugin list;                  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 HostStatusPlugin hoststatus;                  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 WeatherPlugin weather;                  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 UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");  
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)
# Line 77  void PluginManager::LoadPlugins() Line 154  void PluginManager::LoadPlugins()
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    

Legend:
Removed from v.193  
changed lines
  Added in v.521

  ViewVC Help
Powered by ViewVC 1.1.20