2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* regexroute.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* Regular expressions based routing
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
|
|
|
* Copyright (C) 2004 Null Team
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2004-05-22 00:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-18 18:16:59 +00:00
|
|
|
#include <yatengine.h>
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
|
|
|
static Configuration s_cfg;
|
2005-02-10 13:24:30 +00:00
|
|
|
static bool s_extended;
|
|
|
|
static bool s_insensitive;
|
2005-01-13 22:46:31 +00:00
|
|
|
static Mutex s_mutex;
|
|
|
|
static ObjList s_extra;
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
class RouteHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RouteHandler(int prio)
|
2004-12-21 04:16:09 +00:00
|
|
|
: MessageHandler("call.route",prio) { }
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
2004-11-29 21:01:04 +00:00
|
|
|
|
2005-02-10 13:24:30 +00:00
|
|
|
// handle ${paramname} replacements
|
|
|
|
static void replaceParams(const Message &msg, String &str)
|
|
|
|
{
|
|
|
|
int p1;
|
|
|
|
while ((p1 = str.find("${")) >= 0) {
|
|
|
|
int p2 = str.find('}',p1+2);
|
|
|
|
if (p2 > 0) {
|
|
|
|
String v = str.substr(p1+2,p2-p1-2);
|
|
|
|
v.trimBlanks();
|
|
|
|
DDebug("RegexRoute",DebugAll,"Replacing parameter '%s'",
|
|
|
|
v.c_str());
|
|
|
|
str = str.substr(0,p1) + msg.getValue(v) + str.substr(p2+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle ;paramname[=value] assignments
|
2004-11-29 21:01:04 +00:00
|
|
|
static void setMessage(Message &msg, String &line)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2004-11-29 21:01:04 +00:00
|
|
|
ObjList *strs = line.split(';');
|
|
|
|
bool first = true;
|
|
|
|
for (ObjList *p = strs; p; p=p->next()) {
|
|
|
|
String *s = static_cast<String*>(p->get());
|
2005-02-10 13:24:30 +00:00
|
|
|
if (s)
|
|
|
|
replaceParams(msg,*s);
|
2004-11-29 21:01:04 +00:00
|
|
|
if (first) {
|
|
|
|
first = false;
|
2005-01-14 16:29:45 +00:00
|
|
|
line = s ? *s : String::empty();
|
2004-11-29 21:01:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (s && !s->trimBlanks().null()) {
|
|
|
|
int q = s->find('=');
|
|
|
|
if (q > 0) {
|
|
|
|
String n = s->substr(0,q);
|
|
|
|
String v = s->substr(q+1);
|
|
|
|
n.trimBlanks();
|
|
|
|
v.trimBlanks();
|
|
|
|
DDebug("RegexRoute",DebugAll,"Setting '%s' to '%s'",n.c_str(),v.c_str());
|
|
|
|
msg.setParam(n,v);
|
|
|
|
}
|
2005-02-10 13:24:30 +00:00
|
|
|
else {
|
|
|
|
DDebug("RegexRoute",DebugAll,"Clearing parameter '%s'",s->c_str());
|
|
|
|
msg.clearParam(s);
|
|
|
|
}
|
2004-11-29 21:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
strs->destruct();
|
|
|
|
}
|
|
|
|
|
2005-02-10 13:24:30 +00:00
|
|
|
// process one context, can call itself recursively
|
2004-12-01 00:54:04 +00:00
|
|
|
static bool oneContext(Message &msg, String &str, const String &context, String &ret, int depth = 0)
|
2004-11-29 21:01:04 +00:00
|
|
|
{
|
2005-01-13 22:46:31 +00:00
|
|
|
if (context.null())
|
2004-05-22 00:05:20 +00:00
|
|
|
return false;
|
2004-11-29 21:01:04 +00:00
|
|
|
if (depth > 5) {
|
2004-12-01 00:54:04 +00:00
|
|
|
Debug("RegexRoute",DebugWarn,"Possible loop detected, current context '%s'",context.c_str());
|
2004-11-29 21:01:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
NamedList *l = s_cfg.getSection(context);
|
|
|
|
if (l) {
|
|
|
|
unsigned int len = l->length();
|
|
|
|
for (unsigned int i=0; i<len; i++) {
|
|
|
|
NamedString *n = l->getParam(i);
|
|
|
|
if (n) {
|
2005-02-10 13:24:30 +00:00
|
|
|
Regexp r(n->name(),s_extended,s_insensitive);
|
2004-12-01 00:54:04 +00:00
|
|
|
String val;
|
|
|
|
if (r.startsWith("${")) {
|
2005-02-10 13:24:30 +00:00
|
|
|
// handle special matching by param ${paramname}regexp
|
2004-12-01 00:54:04 +00:00
|
|
|
int p = r.find('}');
|
|
|
|
if (p < 3) {
|
|
|
|
Debug("RegexRoute",DebugWarn,"Invalid parameter match '%s' in rule #%u in context '%s'",
|
|
|
|
r.c_str(),i+1,context.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
val = r.substr(2,p-2);
|
|
|
|
r = r.substr(p+1);
|
|
|
|
val.trimBlanks();
|
|
|
|
r.trimBlanks();
|
|
|
|
if (val.null() || r.null()) {
|
|
|
|
Debug("RegexRoute",DebugWarn,"Missing parameter or rule in rule #%u in context '%s'",
|
|
|
|
i+1,context.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-10 13:24:30 +00:00
|
|
|
DDebug("RegexRoute",DebugAll,"Using message parameter '%s'",
|
2004-12-01 00:54:04 +00:00
|
|
|
val.c_str());
|
|
|
|
val = msg.getValue(val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
val = str;
|
|
|
|
val.trimBlanks();
|
|
|
|
|
|
|
|
if (val.matches(r)) {
|
|
|
|
val = val.replaceMatches(*n);
|
2005-02-10 13:24:30 +00:00
|
|
|
if (val.startSkip("echo") || val.startSkip("output")) {
|
|
|
|
// special case: display the line but don't set params
|
|
|
|
replaceParams(msg,val);
|
|
|
|
Output("%s",val.safe());
|
|
|
|
continue;
|
|
|
|
}
|
2004-11-29 21:01:04 +00:00
|
|
|
setMessage(msg,val);
|
|
|
|
val.trimBlanks();
|
2004-12-01 00:54:04 +00:00
|
|
|
if (val.null()) {
|
|
|
|
// special case: do nothing on empty target
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (val == "return") {
|
|
|
|
NDebug("RegexRoute",DebugAll,"Returning false from context '%s'", context.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2004-12-01 00:56:53 +00:00
|
|
|
else if (val.startSkip("goto") || val.startSkip("jump")) {
|
2004-12-01 00:54:04 +00:00
|
|
|
NDebug("RegexRoute",DebugAll,"Jumping to context '%s' by rule #%u '%s'",
|
|
|
|
val.c_str(),i+1,n->name().c_str());
|
|
|
|
return oneContext(msg,str,val,ret,depth+1);
|
|
|
|
}
|
2004-12-01 00:56:53 +00:00
|
|
|
else if (val.startSkip("include") || val.startSkip("call")) {
|
2004-12-01 00:54:04 +00:00
|
|
|
NDebug("RegexRoute",DebugAll,"Including context '%s' by rule #%u '%s'",
|
|
|
|
val.c_str(),i+1,n->name().c_str());
|
|
|
|
if (oneContext(msg,str,val,ret,depth+1)) {
|
|
|
|
DDebug("RegexRoute",DebugAll,"Returning true from context '%s'", context.c_str());
|
2004-11-29 21:01:04 +00:00
|
|
|
return true;
|
2004-12-01 00:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
2004-12-01 00:56:53 +00:00
|
|
|
else if (val.startSkip("match") || val.startSkip("newmatch")) {
|
2004-12-01 00:54:04 +00:00
|
|
|
if (!val.null()) {
|
|
|
|
NDebug("RegexRoute",DebugAll,"Setting match string '%s' by rule #%u '%s' in context '%s'",
|
|
|
|
val.c_str(),i+1,n->name().c_str(),context.c_str());
|
|
|
|
str = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DDebug("RegexRoute",DebugAll,"Returning '%s' for '%s' in context '%s' by rule #%u '%s'",
|
|
|
|
val.c_str(),str.c_str(),context.c_str(),i+1,n->name().c_str());
|
|
|
|
ret = val;
|
|
|
|
return true;
|
2004-11-29 21:01:04 +00:00
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-01 00:54:04 +00:00
|
|
|
DDebug("RegexRoute",DebugAll,"Returning false at end of context '%s'", context.c_str());
|
2004-11-29 21:01:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RouteHandler::received(Message &msg)
|
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
u_int64_t tmr = Time::now();
|
2004-11-29 21:01:04 +00:00
|
|
|
String called(msg.getValue("called"));
|
|
|
|
if (called.null())
|
|
|
|
return false;
|
|
|
|
const char *context = msg.getValue("context","default");
|
2004-12-01 00:54:04 +00:00
|
|
|
String ret;
|
2005-01-13 22:46:31 +00:00
|
|
|
Lock lock(s_mutex);
|
2004-12-01 00:54:04 +00:00
|
|
|
if (oneContext(msg,called,context,ret)) {
|
|
|
|
Debug(DebugInfo,"Routing call to '%s' in context '%s' via '%s' in %llu usec",
|
|
|
|
called.c_str(),context,ret.c_str(),Time::now()-tmr);
|
|
|
|
msg.retValue() = ret;
|
2004-11-29 21:01:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
Debug(DebugInfo,"Could not route call to '%s' in context '%s', wasted %llu usec",
|
2004-11-29 21:01:04 +00:00
|
|
|
called.c_str(),context,Time::now()-tmr);
|
2004-05-22 00:05:20 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PrerouteHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrerouteHandler(int prio)
|
2004-12-21 04:16:09 +00:00
|
|
|
: MessageHandler("call.preroute",prio) { }
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
2005-01-13 22:46:31 +00:00
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
bool PrerouteHandler::received(Message &msg)
|
|
|
|
{
|
2005-04-02 00:49:38 +00:00
|
|
|
u_int64_t tmr = Time::now();
|
2004-05-22 00:05:20 +00:00
|
|
|
// return immediately if there is already a context
|
|
|
|
if (msg.getValue("context"))
|
|
|
|
return false;
|
2004-12-01 00:54:04 +00:00
|
|
|
|
|
|
|
String caller(msg.getValue("caller"));
|
|
|
|
if (caller.null()) {
|
|
|
|
caller << msg.getValue("driver") << "/";
|
|
|
|
caller << msg.getValue("span") << "/";
|
|
|
|
caller << msg.getValue("channel");
|
|
|
|
}
|
|
|
|
if (caller == "//")
|
2004-05-22 00:05:20 +00:00
|
|
|
return false;
|
2004-12-01 00:54:04 +00:00
|
|
|
|
|
|
|
String ret;
|
2005-01-13 22:46:31 +00:00
|
|
|
Lock lock(s_mutex);
|
2004-12-01 00:54:04 +00:00
|
|
|
if (oneContext(msg,caller,"contexts",ret)) {
|
|
|
|
Debug(DebugInfo,"Classifying caller '%s' in context '%s' in %llu usec",
|
|
|
|
caller.c_str(),ret.c_str(),Time::now()-tmr);
|
|
|
|
msg.addParam("context",ret);
|
|
|
|
return true;
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
Debug(DebugInfo,"Could not classify call from '%s', wasted %llu usec",
|
2004-12-01 00:54:04 +00:00
|
|
|
caller.c_str(),Time::now()-tmr);
|
2004-05-22 00:05:20 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2005-01-13 22:46:31 +00:00
|
|
|
class GenericHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GenericHandler(const char* name, int prio)
|
|
|
|
: MessageHandler(name,prio)
|
|
|
|
{
|
|
|
|
Debug(DebugAll,"Installing generic handler for '%s' prio %d [%p]",c_str(),prio,this);
|
|
|
|
s_extra.append(this);
|
|
|
|
}
|
|
|
|
~GenericHandler()
|
|
|
|
{ s_extra.remove(this,false); }
|
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool GenericHandler::received(Message &msg)
|
|
|
|
{
|
|
|
|
DDebug(DebugAll,"Handling message '%s' [%p]",c_str(),this);
|
|
|
|
String ret,what(*this);
|
|
|
|
Lock lock(s_mutex);
|
|
|
|
return oneContext(msg,what,*this,ret);
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
class RegexRoutePlugin : public Plugin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RegexRoutePlugin();
|
|
|
|
virtual void initialize();
|
|
|
|
private:
|
|
|
|
MessageHandler *m_preroute, *m_route;
|
|
|
|
};
|
|
|
|
|
|
|
|
RegexRoutePlugin::RegexRoutePlugin()
|
|
|
|
: m_preroute(0), m_route(0)
|
|
|
|
{
|
|
|
|
Output("Loaded module RegexRoute");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegexRoutePlugin::initialize()
|
|
|
|
{
|
|
|
|
Output("Initializing module RegexRoute");
|
2005-01-13 22:46:31 +00:00
|
|
|
Lock lock(s_mutex);
|
2004-05-22 00:05:20 +00:00
|
|
|
s_cfg = Engine::configFile("regexroute");
|
|
|
|
s_cfg.load();
|
2005-02-10 13:24:30 +00:00
|
|
|
s_extended = s_cfg.getBoolValue("priorities","extended",false);
|
|
|
|
s_insensitive = s_cfg.getBoolValue("priorities","insensitive",false);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_preroute) {
|
|
|
|
delete m_preroute;
|
|
|
|
m_preroute = 0;
|
|
|
|
}
|
|
|
|
if (m_route) {
|
|
|
|
delete m_route;
|
|
|
|
m_route = 0;
|
|
|
|
}
|
2005-01-13 22:46:31 +00:00
|
|
|
s_extra.clear();
|
2004-05-22 00:05:20 +00:00
|
|
|
unsigned priority = s_cfg.getIntValue("priorities","preroute",100);
|
|
|
|
if (priority) {
|
|
|
|
m_preroute = new PrerouteHandler(priority);
|
|
|
|
Engine::install(m_preroute);
|
|
|
|
}
|
|
|
|
priority = s_cfg.getIntValue("priorities","route",100);
|
|
|
|
if (priority) {
|
|
|
|
m_route = new RouteHandler(priority);
|
|
|
|
Engine::install(m_route);
|
|
|
|
}
|
2005-01-13 22:46:31 +00:00
|
|
|
NamedList *l = s_cfg.getSection("extra");
|
|
|
|
if (l) {
|
|
|
|
unsigned int len = l->length();
|
|
|
|
for (unsigned int i=0; i<len; i++) {
|
|
|
|
NamedString *n = l->getParam(i);
|
|
|
|
if (n)
|
|
|
|
Engine::install(new GenericHandler(n->name(),n->toInteger()));
|
|
|
|
}
|
|
|
|
}
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INIT_PLUGIN(RegexRoutePlugin);
|
|
|
|
|
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|