mirror of https://github.com/sipwise/sems.git
- regex maps are files with regex=>value entries which, once loaded, can be used with $M(key=>mapname) pattern replacement - it is now possible to use replacements in active_profile, and also a list of profiles can be specified here - the first matching will be usedsayer/1.4-spce2.6
parent
ace0355f20
commit
8a9bca719a
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Stefan Sayer
|
||||
*
|
||||
* This file is part of SEMS, a free SIP media server.
|
||||
*
|
||||
* SEMS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* For a license to use the SEMS software under conditions
|
||||
* other than those described here, or to purchase support for this
|
||||
* software, please contact iptel.org by e-mail at the following addresses:
|
||||
* info@iptel.org
|
||||
*
|
||||
* SEMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
#include "RegexMapper.h"
|
||||
#include "log.h"
|
||||
|
||||
bool RegexMapper::mapRegex(const string& mapping_name, const char* test_s,
|
||||
string& result) {
|
||||
lock();
|
||||
std::map<string, RegexMappingVector>::iterator it=regex_mappings.find(mapping_name);
|
||||
if (it == regex_mappings.end()) {
|
||||
unlock();
|
||||
ERROR("regex mapping '%s' is not loaded!\n", mapping_name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
bool res = run_regex_mapping(it->second, test_s, result);
|
||||
unlock();
|
||||
return res;
|
||||
}
|
||||
|
||||
void RegexMapper::setRegexMap(const string& mapping_name, const RegexMappingVector& r) {
|
||||
lock();
|
||||
std::map<string, RegexMappingVector>::iterator it=regex_mappings.find(mapping_name);
|
||||
if (it != regex_mappings.end()) {
|
||||
for (RegexMappingVector::iterator r_it =
|
||||
it->second.begin(); r_it != it->second.end(); r_it++) {
|
||||
regfree(&r_it->first);
|
||||
}
|
||||
}
|
||||
regex_mappings[mapping_name] = r;
|
||||
unlock();
|
||||
}
|
||||
|
||||
std::vector<std::string> RegexMapper::getNames() {
|
||||
std::vector<std::string> res;
|
||||
lock();
|
||||
for (std::map<string, RegexMappingVector>::iterator it=
|
||||
regex_mappings.begin(); it != regex_mappings.end(); it++)
|
||||
res.push_back(it->first);
|
||||
unlock();
|
||||
return res;
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Stefan Sayer
|
||||
*
|
||||
* This file is part of SEMS, a free SIP media server.
|
||||
*
|
||||
* SEMS is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* For a license to use the SEMS software under conditions
|
||||
* other than those described here, or to purchase support for this
|
||||
* software, please contact iptel.org by e-mail at the following addresses:
|
||||
* info@iptel.org
|
||||
*
|
||||
* SEMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef _RegexMapper_h_
|
||||
#define _RegexMapper_h_
|
||||
|
||||
#include "AmUtils.h"
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "AmThread.h"
|
||||
|
||||
struct RegexMapper {
|
||||
|
||||
RegexMapper() { }
|
||||
~RegexMapper() { }
|
||||
|
||||
std::map<string, RegexMappingVector> regex_mappings;
|
||||
AmMutex regex_mappings_mut;
|
||||
|
||||
void lock() { regex_mappings_mut.lock(); }
|
||||
void unlock() { regex_mappings_mut.unlock(); }
|
||||
|
||||
bool mapRegex(const string& mapping_name, const char* test_s,
|
||||
string& result);
|
||||
|
||||
void setRegexMap(const string& mapping_name, const RegexMappingVector& r);
|
||||
|
||||
std::vector<std::string> getNames();
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,7 @@
|
||||
# this is a sample regex mapping to map source address 10.0.* to internal1 profile
|
||||
# and 10.1.* to internal2 profile.
|
||||
# For example, used with this active_profile setting:
|
||||
# active_profile=$M($si=>src_ipmap),refuse
|
||||
# all other calls would be blocked.
|
||||
^10\.0\..*=>internal1
|
||||
^10\.1\..*=>internal2
|
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from xmlrpclib import *
|
||||
|
||||
s = ServerProxy('http://localhost:8090')
|
||||
print s.getRegexMapNames()
|
@ -0,0 +1,13 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from xmlrpclib import *
|
||||
|
||||
if len(sys.argv) != 3:
|
||||
print "usage: %s <regex map name> <full regex map path>" % sys.argv[0]
|
||||
sys.exit(1)
|
||||
|
||||
s = ServerProxy('http://localhost:8090')
|
||||
print "Active calls: %d" % s.calls()
|
||||
p ={ 'name' : sys.argv[1], 'file' : sys.argv[2] }
|
||||
print s.setRegexMap(p)
|
Loading…
Reference in new issue