--- app_enumlookup.c Sun Dec 7 09:19:18 2003 +++ app_enumlookup_iain.c Sun Dec 7 09:11:53 2003 @@ -1,7 +1,7 @@ /* * Asterisk -- A telephony toolkit for Linux. * - * Time of day - Report the time of day + * ENUM lookup - search the DNS for E.164 number translations * * Copyright (C) 1999, Mark Spencer * @@ -36,14 +36,17 @@ static char *synopsis = "Lookup number in ENUM"; static char *descrip = -" EnumLookup(exten): Looks up an extension via ENUM and sets\n" +" EnumLookup(exten, countrycode): Looks up an extension via ENUM and sets\n" "the variable 'ENUM'. For VoIP URIs this variable will \n" "look like 'TECHNOLOGY/URI' with the appropriate technology.\n" +"Only the first NAPTR record for an E.164 number is processed. \n" "Returns -1 on hangup, or 0 on completion regardless of whether the \n" -"lookup was successful. \n" +"lookup was successful. \n\n" "Currently, the enumservices SIP, H323, IAX, IAX2 and TEL are recognized. \n" "A good SIP, H323, IAX or IAX2 entry will result in normal priority handling, \n" "whereas a good TEL entry will increase the priority by 51 (if existing).\n" +"If an optional country code is specified then a matching country code in a\n" +"a retrieved TEL entry will increase the priority by 61.\n" "If the lookup was *not* successful and there exists a priority n + 101,\n" "then that priority will be taken next.\n" ; @@ -58,22 +61,46 @@ static int enumlookup_exec(struct ast_channel *chan, void *data) { - int res=0; + int res=0, cclen=0; char tech[80]; char dest[80]; char tmp[256]; - char *c,*t; + char *c,*t, *cptr; struct localuser *u; if (!data || !strlen(data)) { ast_log(LOG_WARNING, "EnumLookup requires an argument (extension)\n"); res = 1; } + + if( (cptr=strrchr( data, '|')) ) { + // There's a country code argument + *cptr++= '\0'; + while( cclen < strlen( cptr)) { + if( isdigit( *(cptr+cclen))) break; + cclen++; + } + if( cclen != strlen( cptr)) { + cptr+=cclen; + cclen = 1; + while( cclen < strlen( cptr)) { + if( !isdigit(*(cptr+cclen))) break; + cclen++; + } + } + else + cclen = 0; + ast_log(LOG_DEBUG, "Country code = %s, Country code length = %d \n", cptr, cclen); + } + LOCAL_USER_ADD(u); if (!res) { res = ast_get_enum(chan, data, dest, sizeof(dest), tech, sizeof(tech)); printf("ENUM got '%d'\n", res); } LOCAL_USER_REMOVE(u); + + + /* Parse it out */ if (res > 0) { if (!strcasecmp(tech, "SIP")) { @@ -113,7 +140,7 @@ ast_log(LOG_NOTICE, "tel: uri must start with a \"+\" (got '%s')\n", c); res = 0; } else { -/* now copy over the number, skipping all non-digits and stop at ; or NULL */ + /* now copy over the number, skipping all non-digits and stop at ; or NULL */ t = tmp; while( *c && (*c != ';') && (t - tmp < (sizeof(tmp) - 1))) { if (isdigit(*c)) @@ -121,12 +148,26 @@ c++; } *t = 0; - pbx_builtin_setvar_helper(chan, "ENUM", tmp); - ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", tmp); - if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 51, chan->callerid)) - chan->priority += 50; - else - res = 0; + + if( cclen && !strncmp( tmp, cptr, cclen)) { + cptr=tmp+cclen; + if (ast_exists_extension(chan, chan->context, + chan->exten, chan->priority + 61, chan->callerid)) + chan->priority += 60; + else + res=0; + } + else { + cptr=tmp; + if (ast_exists_extension(chan, chan->context, + chan->exten, chan->priority + 51, chan->callerid)) + chan->priority += 50; + else + res=0; + + } + pbx_builtin_setvar_helper(chan, "ENUM", cptr); + ast_log(LOG_NOTICE, "tel: ENUM set to \"%s\"\n", cptr); } } else if (strlen(tech)) { ast_log(LOG_NOTICE, "Don't know how to handle technology '%s'\n", tech);