-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandInterface.cpp
93 lines (87 loc) · 2.91 KB
/
CommandInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <string.h>
#include "QueueFilter.h"
#include "Initialize.h"
#include "xmlParser.h"
#include "CommandInterface.h"
CommandInterface::CommandInterface() : m_prefix("/bin/sh"), m_ignoreError(false), m_debug(0), m_filter(false)
{
}
void CommandInterface::XmlParse(std::string config_file_name)
{
XMLNode xMainNode = XMLNode::openFileHelper(config_file_name.c_str(), "head");
string tmp = xMainNode.getChildNode("debug").getAttribute("start");
if (tmp == "true") m_debug = 1;
XMLNode Node = xMainNode.getChildNode("sersync");
m_watch = Node.getChildNode("localpath").getAttribute("watch");
int num = xMainNode.nChildNode("plugin");
if (num == 0)
{
perror("Error there is no plugin tag in config xml\n");
exit(1);
}
for (int i = 0; i < num; i++)
{
string name = xMainNode.getChildNode("plugin", i).getAttribute("name");
if (name == "command")
{
XMLNode xNode = xMainNode.getChildNode("plugin", i);
m_prefix = xNode.getChildNode("param").getAttribute("prefix");
m_suffix = xNode.getChildNode("param").getAttribute("suffix");
string tmp = xNode.getChildNode("param").getAttribute("ignoreError");
if (tmp == "true")
{
m_ignoreError = true;
}
tmp = xNode.getChildNode("filter").getAttribute("start");
if (tmp == "true")
{
m_filter = true;
int num = xNode.getChildNode("filter").nChildNode("include");
for (int i = 0; i < num; i++)
{
string t = xNode.getChildNode("filter").getChildNode(i).getAttribute("expression");
try
{
ptrRegex tmp(new boost::regex(t));
pattern.push_back(tmp);
} catch (boost::regex_error& e)
{
cout << "plugin command regular expression error" << endl;
cout << "error information is:\t" << e.what() << endl;
exit(1);
}
}
}
break;
}
}
}
int CommandInterface::Execute(Event e)
{
//if (e->operation == 0 || e->mask==256) return 0;
boost::cmatch what;
if (m_filter == true)
{
int valid = 0;
for (int i = 0; i < pattern.size(); i++)
{
if (boost::regex_match((e->path).c_str(), what, *(pattern[i])))
{
//cout<<"this is other"<<what[1]<<endl;
valid = 1;
break;
}
}
if (!valid) return 0;
}
string command = m_prefix + " " + e->path + " " + m_suffix;
if (m_ignoreError == true)
{
command += " >/dev/null 2>&1 ";
}
if (m_debug == 1)
{
cout << command << endl;
}
system(command.c_str());
}