/[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 222 by torben, Fri Apr 10 20:00:07 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    #include "AccessManager.h"
21    
22  typedef std::map<std::string, Plugin*>::iterator MapIterator;  typedef std::map<std::string, Plugin*>::iterator MapIterator;
23    
24  PluginManager::PluginManager()  PluginManager::PluginManager()
25  {  {
26  }  }
27            
28  PluginManager::~PluginManager()  PluginManager::~PluginManager()
29  {  {
30  }  }
# Line 48  void PluginManager::AddPlugin(Plugin* pl Line 54  void PluginManager::AddPlugin(Plugin* pl
54          }          }
55  }  }
56    
57  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
58  {  {
59          static EchoPlugin echo;          if (pluginName == "echo")
60                    return new EchoPlugin();
61    
62          static SpamPlugin spam;          if (pluginName == "spam")
63                    return new SpamPlugin();
64    
65          static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false);          if (pluginName == "tog")
66          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);                  return new TogPlugin();
67    
68          static TogPlugin tog;          if (pluginName == "status")
69                    return new StatusPlugin();
70    
71          static StatusPlugin status;          if (pluginName == "list")
72                    return new ListPlugin();
73    
74          static ListPlugin list;          if (pluginName == "hoststatus")
75                    return new HostStatusPlugin();
76    
77          static HostStatusPlugin hoststatus;          if (pluginName == "weather")
78                    return new WeatherPlugin();
79    
80          static WeatherPlugin weather;          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    
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          static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");  void PluginManager::LoadPlugins()
100    {
101            Logger::logMessage("-------- PluginList --------");
102            std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
103    
104          for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)          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    
112                    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                    {
139                            AddPlugin(pl);
140                            ParseCommonOptions(name,args);
141                    }
142                    else
143                            Logger::logMessage( std::string("Unknown plugin: ")+name);
144                    
145            }      
146    
147    
148            for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
149          {          {
150                  Plugin* pl = (*it).second;                  Plugin* pl = (*it).second;
151                  if (pl != 0)                  if (pl != 0)
# Line 77  void PluginManager::LoadPlugins() Line 153  void PluginManager::LoadPlugins()
153          }          }
154  }  }
155    
156    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    
164  Plugin* PluginManager::GetPlugin(const std::string& pluginname)  Plugin* PluginManager::GetPlugin(const std::string& pluginname)
165  {  {
166          return _plugins[ pluginname ];          static ProxyPlugin proxy;
167    
168            std::map<std::string,Plugin*>::iterator it = _plugins.find(pluginname );
169    
170            if (it != _plugins.end() )
171            {
172                    proxy.SetPlugin(it->second);
173                    return &proxy;
174            }
175            else
176            {
177                    return 0;
178            }
179  }  }
180    
181    

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

  ViewVC Help
Powered by ViewVC 1.1.20