Skip to content

Commit

Permalink
screen popup - extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Radek Bucek committed Jan 29, 2014
1 parent be9dfe7 commit 6168d80
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
42 changes: 37 additions & 5 deletions manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,13 +1450,22 @@ bool ManagerClientThread_screen_popup::parseCommand() {

void ManagerClientThread_screen_popup::onCall(int sipResponseNum, const char *callerName, const char *callerNum, const char *calledNum,
unsigned int sipSaddr, unsigned int sipDaddr) {
/*
cout << "** call 01" << endl;
cout << "** - called num : " << calledNum << endl;
struct in_addr _in;
_in.s_addr = sipSaddr;
cout << "** - src ip : " << inet_ntoa(_in) << endl;
cout << "** - reg_match : " << reg_match(calledNum, this->dest_number.empty() ? this->username.c_str() : this->dest_number.c_str()) << endl;
cout << "** - check ip : " << this->src_ip.checkIP(htonl(sipSaddr)) << endl;
*/
if(!(((this->popup_on == "200" && sipResponseNum == 200) ||
(this->popup_on == "183/180" && (sipResponseNum == 183 || sipResponseNum == 180)) ||
(this->popup_on == "183/180_200" && (sipResponseNum == 200 || (sipResponseNum == 183 || sipResponseNum == 180)))) &&
(this->regex_calling_number.empty() ||
reg_match(calledNum, this->regex_calling_number.c_str())) &&
reg_match(calledNum, this->dest_number.empty() ? this->username.c_str() : this->dest_number.c_str()) &&
(this->non_numeric_caller_id ||
this->isNumericId(calledNum)))) {
this->isNumericId(calledNum)) &&
this->src_ip.checkIP(htonl(sipSaddr)))) {
return;
}
char rsltString[4096];
Expand All @@ -1467,6 +1476,15 @@ void ManagerClientThread_screen_popup::onCall(int sipResponseNum, const char *ca
strcpy(sipSaddrIP, inet_ntoa(in));
in.s_addr = sipDaddr;
strcpy(sipDaddrIP, inet_ntoa(in));
string callerNumStr = callerNum;
for(size_t i = 0; i < this->regex_calling_number.size(); i++) {
string temp = reg_replace(callerNumStr.c_str(),
this->regex_calling_number[i].pattern.c_str(),
this->regex_calling_number[i].replace.c_str());
if(!temp.empty()) {
callerNumStr = temp;
}
}
sprintf(rsltString,
"call_data: "
"sipresponse:[[%i]] "
Expand All @@ -1477,7 +1495,7 @@ void ManagerClientThread_screen_popup::onCall(int sipResponseNum, const char *ca
"sipcalledip:[[%s]]\n",
sipResponseNum,
callerName,
callerNum,
callerNumStr.c_str(),
calledNum,
sipSaddrIP,
sipDaddrIP);
Expand All @@ -1497,12 +1515,15 @@ bool ManagerClientThread_screen_popup::parseUserPassword() {
string(
"select u.username,\
u.name,\
u.dest_number,\
p.name as profile_name,\
p.auto_popup,\
p.show_ip,\
p.popup_on,\
p.non_numeric_caller_id,\
p.regex_calling_number,\
p.src_ip_whitelist,\
p.src_ip_blacklist,\
p.app_launch,\
p.app_launch_args_or_url\
from screen_popup_users u\
Expand All @@ -1518,12 +1539,23 @@ bool ManagerClientThread_screen_popup::parseUserPassword() {
rslt = true;
username = row["username"];
name = row["name"];
dest_number = row["dest_number"];
profile_name = row["profile_name"];
auto_popup = atoi(row["auto_popup"].c_str());
show_ip = atoi(row["show_ip"].c_str());
popup_on = row["popup_on"];
non_numeric_caller_id = atoi(row["non_numeric_caller_id"].c_str());
regex_calling_number = row["regex_calling_number"];
if(!row["regex_calling_number"].empty()) {
vector<string> items = split(row["regex_calling_number"].c_str(), split("\r|\n", "|"), true);
for(size_t i = 0; i < items.size(); i++) {
vector<string> patternReplace = split(items[i].c_str(), "|", true);
if(patternReplace.size() == 2) {
this->regex_calling_number.push_back(RegexReplace(patternReplace[0].c_str(), patternReplace[1].c_str()));
}
}
}
src_ip.addWhite(row["src_ip_whitelist"].c_str());
src_ip.addBlack(row["src_ip_blacklist"].c_str());
app_launch = row["app_launch"];
app_launch_args_or_url = row["app_launch_args_or_url"];
if(!opt_php_path[0]) {
Expand Down
13 changes: 12 additions & 1 deletion manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class ManagerClientThread {
};

class ManagerClientThread_screen_popup : public ManagerClientThread {
public:
struct RegexReplace {
RegexReplace(const char *pattern, const char *replace) {
this->pattern = pattern;
this->replace = replace;
}
string pattern;
string replace;
};
public:
ManagerClientThread_screen_popup(int client, const char *command, int commandLength = 0);
bool parseCommand();
Expand All @@ -45,12 +54,14 @@ class ManagerClientThread_screen_popup : public ManagerClientThread {
private:
string username;
string name;
string dest_number;
string profile_name;
bool auto_popup;
bool show_ip;
string popup_on;
bool non_numeric_caller_id;
string regex_calling_number;
vector<RegexReplace> regex_calling_number;
ListIP_wb src_ip;
string app_launch;
string app_launch_args_or_url;
};
Expand Down
36 changes: 36 additions & 0 deletions tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,42 @@ int reg_match(const char *string, const char *pattern) {
return(status == 0);
}

string reg_replace(const char *str, const char *pattern, const char *replace) {
int status;
regex_t re;
if(regcomp(&re, pattern, REG_EXTENDED) != 0) {
syslog(LOG_ERR, "regcomp %s error", pattern);
return("");
}
int match_max = 20;
regmatch_t match[match_max];
memset(match, 0, sizeof(match));
status = regexec(&re, str, match_max, match, 0);
regfree(&re);
if(status == 0) {
string rslt = replace;
int match_count = 0;
for(int i = 0; i < match_max; i ++) {
if(match[i].rm_so == -1 && match[i].rm_eo == -1) {
break;
}
++match_count;
}
for(int i = match_count - 1; i > 0; i--) {
for(int j = 0; j < 2; j++) {
char findStr[10];
sprintf(findStr, j ? "{$%i}" : "$%i", i);
size_t findPos;
while((findPos = rslt.find(findStr)) != string::npos) {
rslt.replace(findPos, strlen(findStr), string(str).substr(match[i].rm_so, match[i].rm_eo - match[i].rm_so));
}
}
}
return(rslt);
}
return("");
}


void ListIP::addComb(string &ip) {
addComb(ip.c_str());
Expand Down
15 changes: 8 additions & 7 deletions tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ std::vector<std::string> split(const std::string &s, char delim);
std::vector<std::string> split(const char *s, const char *delim, bool enableTrim = false);
std::vector<std::string> split(const char *s, std::vector<std::string> delim, bool enableTrim = false);
int reg_match(const char *string, const char *pattern);
string reg_replace(const char *string, const char *pattern, const char *replace);

class CircularBuffer
{
Expand Down Expand Up @@ -229,12 +230,12 @@ class IP {
*maskSeparator = 0;
in_addr ips;
inet_aton(ip, &ips);
this->ip = ips.s_addr;
this->ip = htonl(ips.s_addr);
*maskSeparator = '/';
} else {
in_addr ips;
inet_aton(ip, &ips);
this->ip = ips.s_addr;
this->ip = htonl(ips.s_addr);
mask_length = 32;
for(int i = 0; i < 32; i++) {
if(this->ip == this->ip >> i << i) {
Expand All @@ -255,7 +256,7 @@ class IP {
bool checkIP(const char *check_ip) {
in_addr ips;
inet_aton(check_ip, &ips);
return(checkIP(ips.s_addr));
return(checkIP(htonl(ips.s_addr)));
}
public:
uint ip;
Expand Down Expand Up @@ -315,7 +316,7 @@ class ListIP {
bool checkIP(const char *check_ip) {
in_addr ips;
inet_aton(check_ip, &ips);
return(checkIP(ips.s_addr));
return(checkIP(htonl(ips.s_addr)));
}
void clear() {
if(autoLock) lock();
Expand Down Expand Up @@ -390,13 +391,13 @@ class ListIP_wb {
void addBlack(string &ip);
void addBlack(const char *ip);
bool checkIP(uint check_ip) {
return(!white.size() && white.checkIP(check_ip) &&
return((!white.size() || white.checkIP(check_ip)) &&
!black.checkIP(check_ip));
}
bool checkIP(const char *check_ip) {
in_addr ips;
inet_aton(check_ip, &ips);
return(checkIP(ips.s_addr));
return(checkIP(htonl(ips.s_addr)));
}
private:
ListIP white;
Expand All @@ -411,7 +412,7 @@ class ListPhoneNumber_wb {
void addBlack(string &number);
void addBlack(const char *number);
bool checkNumber(const char *check_number) {
return(!white.size() && white.checkNumber(check_number) &&
return((!white.size() || white.checkNumber(check_number)) &&
!black.checkNumber(check_number));
}
private:
Expand Down
14 changes: 13 additions & 1 deletion voipmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3394,7 +3394,7 @@ void test() {

case 1:
{
/*
CountryPrefixes *cp = new CountryPrefixes();
cp->load();
vector<string> countries;
Expand All @@ -3410,6 +3410,18 @@ void test() {
ipc->load();
cout << ipc->getCountry(3755988991) << endl;
delete ipc;
*/

cout << reg_match("123456789", "456") << endl;
cout << reg_replace("123456789", "(.*)(456)(.*)", "$1-$2-$3") << endl;

ListIP_wb ip;
ip.addWhite("192.168.1.0/24\r192.168.2.3");
ip.addBlack("192.168.1.13");
cout << ip.checkIP("192.168.1.12") << endl;
cout << ip.checkIP("192.168.1.13") << endl;
cout << ip.checkIP("192.168.2.3") << endl;
cout << ip.checkIP("192.168.2.5") << endl;

/*
char *test = "+123 456";
Expand Down

0 comments on commit 6168d80

Please sign in to comment.