/[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 208 by torben, Sun Dec 21 18:41:08 2008 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  #include "plugins/EchoPlugin.h"  #include "plugins/EchoPlugin.h"
9  #include "plugins/SpamPlugin.h"  #include "plugins/SpamPlugin.h"
# Line 18  typedef std::map<std::string, Plugin*>:: Line 20  typedef std::map<std::string, Plugin*>::
20  PluginManager::PluginManager()  PluginManager::PluginManager()
21  {  {
22  }  }
23            
24  PluginManager::~PluginManager()  PluginManager::~PluginManager()
25  {  {
26  }  }
# Line 48  void PluginManager::AddPlugin(Plugin* pl Line 50  void PluginManager::AddPlugin(Plugin* pl
50          }          }
51  }  }
52    
53  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
54  {  {
55          static EchoPlugin echo;          if (pluginName == "echo")
56                    return new EchoPlugin();
57    
58            if (pluginName == "spam")
59                    return new SpamPlugin();
60    
61            if (pluginName == "tog")
62                    return new TogPlugin();
63    
64            if (pluginName == "status")
65                    return new StatusPlugin();
66    
67            if (pluginName == "list")
68                    return new ListPlugin();
69    
70          static SpamPlugin spam;          if (pluginName == "hoststatus")
71                    return new HostStatusPlugin();
72    
73            if (pluginName == "weather")
74                    return new WeatherPlugin();
75    
76            if (pluginName == "shellexec")
77                    return new ShellExecPlugin(args);
78    
79            if (pluginName == "urltrigger")
80                    return new UrlTriggerPlugin(args);
81            
82            return 0;
83    }
84    
85    
86    void PluginManager::DestroyPlugins()
87    {
88            for (std::map<std::string,Plugin*>::iterator it=_plugins.begin(); it!=_plugins.end(); ++it)
89            {
90                    delete it->second;
91            }
92            _plugins.clear();
93    }
94    
95          static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false);  void PluginManager::LoadPlugins()
96          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);  {
97            Logger::logMessage("-------- PluginList --------");
98            std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
99    
100          static TogPlugin tog;          for (unsigned i=0; i<pluginlist.size(); i++)
101            {
102                    std::string current = pluginlist[i];
103                    
104                    std::string name;
105                    unsigned pos = current.find(' ');
106                    std::map<std::string,std::string> args;
107    
108          static StatusPlugin status;                  std::string argstr;
109    
110          static ListPlugin list;                  if (pos == std::string::npos)
111                    {
112                            name = current;
113                    }
114                    else
115                    {
116                            name = Util::str_trim(current.substr(0,pos));
117                            argstr = Util::str_trim(current.substr(pos+1,1024));
118                            args = ConfigHelper::ParseArguments(argstr);
119                    }
120    
121          static HostStatusPlugin hoststatus;                  Plugin* pl = 0;
122                    try
123                    {      
124                            pl = CreatePlugin(name, args );
125                    }
126                    catch (std::exception& e)
127                    {
128                            Logger::logMessage(std::string("Failed to load plugin ") + name + " with args: " + argstr);
129                            Logger::logMessage(std::string("Reason: ") + e.what());
130                            continue;
131                    }
132    
133          static WeatherPlugin weather;                  if (pl)
134                            AddPlugin(pl);
135                    else
136                            Logger::logMessage( std::string("Unknown plugin: ")+name);
137                    
138            }      
139    
         static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");  
140    
141          for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)          for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
142          {          {
143                  Plugin* pl = (*it).second;                  Plugin* pl = (*it).second;
144                  if (pl != 0)                  if (pl != 0)
# Line 77  void PluginManager::LoadPlugins() Line 146  void PluginManager::LoadPlugins()
146          }          }
147  }  }
148    
149    
150  Plugin* PluginManager::GetPlugin(const std::string& pluginname)  Plugin* PluginManager::GetPlugin(const std::string& pluginname)
151  {  {
152          return _plugins[ pluginname ];          return _plugins[ pluginname ];

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

  ViewVC Help
Powered by ViewVC 1.1.20