/[projects]/smsdaemon/plugins/ShellExecPlugin.cpp
ViewVC logotype

Annotation of /smsdaemon/plugins/ShellExecPlugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 205 - (hide annotations) (download)
Fri Dec 19 22:29:25 2008 UTC (15 years, 5 months ago) by torben
File size: 1622 byte(s)
Enable selection and configuration of loaded plugins to be specified in the configuration file

1 torben 35 #include "ShellExecPlugin.h"
2 torben 149 #include "ISmsTransceiver.h"
3 torben 90 #include "Sms.h"
4 torben 35
5 torben 158 #include "Util.h"
6 torben 205 #include "ConfigFile.h"
7 torben 35
8 torben 205 #include <stdexcept>
9    
10     ShellExecPlugin::ShellExecPlugin(std::map<std::string,std::string> args)
11     : Plugin("", "")
12 torben 35 {
13 torben 205 _command = args["trigger"];
14     _exeCommand = args["cmd"];
15     _description = std::string("ShellExec: ")+_exeCommand;
16     std::string allowParms = args["allowparms"];
17     std::string returnOutput = args["returnoutput"];
18    
19     if (_command == "" || _exeCommand == "" || allowParms == "" || returnOutput == "")
20     throw std::runtime_error("Usage: plugin = shellexec trigger=<trigger>; cmd=<command>; allowparms=<0|1>; returnoutput=<0|1>");
21    
22    
23     _allowParameters = ( ConfigHelper::StringToInt(allowParms) == 1);
24     _returnOutput = ( ConfigHelper::StringToInt(returnOutput) == 1);
25 torben 35 }
26    
27 torben 149 void ShellExecPlugin::Execute(ISmsTransceiver& modem, SMS& sms)
28 torben 35 {
29 torben 56 std::string command = _exeCommand;
30 torben 35
31 torben 56 if (_allowParameters)
32     {
33 torben 142 std::string parameters = sms.ExtractParameters();
34 torben 56
35     parameters = Util::str_replace(parameters, "||");
36     parameters = Util::str_replace(parameters, "&&");
37     parameters = Util::str_replace(parameters, ";");
38     parameters = Util::str_replace(parameters, "'");
39     parameters = Util::str_replace(parameters, "\"");
40    
41     command += " ";
42     command += parameters;
43     }
44 torben 107 std::string output;
45     std::string* out_p = 0;
46 torben 35
47 torben 107 if (_returnOutput)
48     out_p = &output;
49    
50     int retval = Util::my_system( command.c_str(), out_p );
51    
52 torben 35 std::string message;
53     if (retval == -1)
54     message = "Error";
55     else
56 torben 107 {
57     if (_returnOutput)
58     message = output;
59     else
60     message = "OK";
61     }
62 torben 196
63 torben 142 modem.SendSms(sms.GetSender(), message, false );
64 torben 35 }

  ViewVC Help
Powered by ViewVC 1.1.20