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

Diff of /smsdaemon/PluginManager.cpp

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

revision 35 by torben, Tue Jun 10 14:38:59 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 "common.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"
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    
 #include "EchoPlugin.h"  
 #include "SpamPlugin.h"  
 #include "ShellExecPlugin.h"  
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 21  PluginManager::~PluginManager() Line 33  PluginManager::~PluginManager()
33    
34  void PluginManager::AddPlugin(Plugin* plugin)  void PluginManager::AddPlugin(Plugin* plugin)
35  {  {
         Common* cmn = Common::instance();  
   
36          if (plugin != 0)          if (plugin != 0)
37          {          {
38                  std::string command = plugin->GetCommand();                  if (plugin->IsHelper())
39                    {
40                  if ( _plugins[ command ] == 0)                          _helper_plugins.push_back(plugin);
41                          _plugins[ command ] = plugin;                  }
42                  else                  else
43                          cmn->logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);                  {
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          else
53          {          {
54                  cmn->logMessage("AddPlugin() -- cannot register a null pointer");                  Logger::logMessage("AddPlugin() -- cannot register a null pointer");
55          }          }
56  }  }
57    
58  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
59  {  {
60          Common* cmn = Common::instance();          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          static EchoPlugin echo;          if (pluginName == "weather")
79                    return new WeatherPlugin();
80    
81          static SpamPlugin spam;          if (pluginName == "shellexec")
82                    return new ShellExecPlugin(args);
83    
84          static ShellExecPlugin wake("wake", "/home/torben/bin/wake");          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)          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)
153                          cmn->logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );                          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)  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.35  
changed lines
  Added in v.521

  ViewVC Help
Powered by ViewVC 1.1.20