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

Diff of /smsdaemon/PluginManager.cpp

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

revision 114 by torben, Sun Nov 2 20:14:20 2008 UTC revision 207 by torben, Sun Dec 21 17:42:30 2008 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 "EchoPlugin.h"  #include "Common.h"
6  #include "SpamPlugin.h"  #include "Util.h"
7  #include "ShellExecPlugin.h"  
8  #include "TogPlugin.h"  #include "plugins/EchoPlugin.h"
9  #include "StatusPlugin.h"  #include "plugins/SpamPlugin.h"
10  #include "ListPlugin.h"  #include "plugins/ShellExecPlugin.h"
11    #include "plugins/TogPlugin.h"
12    #include "plugins/StatusPlugin.h"
13    #include "plugins/ListPlugin.h"
14    #include "plugins/HostStatusPlugin.h"
15    #include "plugins/WeatherPlugin.h"
16    #include "plugins/UrlTriggerPlugin.h"
17    
18  typedef std::map<std::string, Plugin*>::iterator MapIterator;  typedef std::map<std::string, Plugin*>::iterator MapIterator;
19    
20  PluginManager::PluginManager()  PluginManager::PluginManager()
21  {  {
22  }  }
23            
24  PluginManager::~PluginManager()  PluginManager::~PluginManager()
25  {  {
26  }  }
# Line 23  PluginManager::~PluginManager() Line 28  PluginManager::~PluginManager()
28    
29  void PluginManager::AddPlugin(Plugin* plugin)  void PluginManager::AddPlugin(Plugin* plugin)
30  {  {
         Common* cmn = Common::instance();  
   
31          if (plugin != 0)          if (plugin != 0)
32          {          {
33                  std::string command = plugin->GetCommand();                  if (plugin->IsHelper())
34                    {
35                  if ( _plugins[ command ] == 0)                          _helper_plugins.push_back(plugin);
36                          _plugins[ command ] = plugin;                  }
37                  else                  else
38                          cmn->logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);                  {
39                            std::string command = plugin->GetCommand();
40    
41                            if ( _plugins[ command ] == 0)
42                                    _plugins[ command ] = plugin;
43                            else
44                                    Logger::logMessage( std::string("AddPlugin() -- already have a plugin called ") + command);
45                    }
46          }          }
47          else          else
48          {          {
49                  cmn->logMessage("AddPlugin() -- cannot register a null pointer");                  Logger::logMessage("AddPlugin() -- cannot register a null pointer");
50          }          }
51  }  }
52    
53  void PluginManager::LoadPlugins()  Plugin* PluginManager::CreatePlugin(const std::string& pluginName, const std::map<std::string,std::string>& args)
54  {  {
55          Common* cmn = Common::instance();          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            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          static EchoPlugin echo;          if (pluginName == "urltrigger")
80                    return new UrlTriggerPlugin(args);
81            
82            return 0;
83    }
84    
85          static SpamPlugin spam;  void PluginManager::LoadPlugins()
86    {
87    /*
88    
89          static ShellExecPlugin wake("wake", "/home/torben/bin/wake", false, false);          static ShellExecPlugin wake("wake", "/usr/local/bin/wake", false, false);
90          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);          static ShellExecPlugin ping("ping", "nmap -sP 192.168.10.1-255 | grep appears | awk '{print $2}'", false,true);
91    
         static TogPlugin tog;  
92    
         static StatusPlugin status;  
93    
         static ListPlugin list;  
94    
95          for(MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)          static UrlTriggerPlugin urltrigger("tou", "http://t-hoerup.dk/test.php");
96    */
97    
98            Logger::logMessage("-------- PluginList --------");
99            std::vector<Value> pluginlist = Common::instance()->GetConfigfile()->GetValues("smsdaemon", "plugin");
100    
101            for (unsigned i=0; i<pluginlist.size(); i++)
102            {
103                    std::string current = pluginlist[i];
104                    
105                    std::string name;
106                    unsigned pos = current.find(' ');
107                    std::map<std::string,std::string> args;
108    
109                    std::string argstr;
110    
111                    if (pos == std::string::npos)
112                    {
113                            name = current;
114                    }
115                    else
116                    {
117                            name = Util::str_trim(current.substr(0,pos));
118                            argstr = Util::str_trim(current.substr(pos+1,1024));
119                            args = ConfigHelper::ParseArguments(argstr);
120                    }
121    
122                    Plugin* pl = 0;
123                    try
124                    {      
125                            pl = CreatePlugin(name, args );
126                    }
127                    catch (std::exception& e)
128                    {
129                            Logger::logMessage(std::string("Failed to load plugin ") + name + " with args: " + argstr);
130                            Logger::logMessage(std::string("Reason: ") + e.what());
131                            continue;
132                    }
133    
134                    if (pl)
135                            AddPlugin(pl);
136                    else
137                            Logger::logMessage( std::string("Unknown plugin: ")+name);
138                    
139            }      
140    
141    
142            for (MapIterator it = _plugins.begin(); it != _plugins.end(); ++it)
143          {          {
144                  Plugin* pl = (*it).second;                  Plugin* pl = (*it).second;
145                  if (pl != 0)                  if (pl != 0)
146                          cmn->logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );                          Logger::logMessage( std::string("Loaded plugin \"") + pl->GetCommand() + "\" - " + pl->GetDescription() );
147          }          }
148  }  }
149    
150    void PluginManager::DestroyPlugins()
151    {
152    }
153    
154  Plugin* PluginManager::GetPlugin(const std::string& pluginname)  Plugin* PluginManager::GetPlugin(const std::string& pluginname)
155  {  {
156          return _plugins[ pluginname ];          return _plugins[ pluginname ];

Legend:
Removed from v.114  
changed lines
  Added in v.207

  ViewVC Help
Powered by ViewVC 1.1.20