<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14270] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14270">14270</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-07-16 10:38:16 -0500 (Thu, 16 Jul 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>add support for intra[state|lata] calls.
use npanxx table to determine intra status
changed intra field to intrastate_rate</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_lcrmod_lcrc">freeswitch/trunk/src/mod/applications/mod_lcr/mod_lcr.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_lcrmod_lcrc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_lcr/mod_lcr.c (14269 => 14270)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_lcr/mod_lcr.c        2009-07-16 05:06:36 UTC (rev 14269)
+++ freeswitch/trunk/src/mod/applications/mod_lcr/mod_lcr.c        2009-07-16 15:38:16 UTC (rev 14270)
</span><span class="lines">@@ -114,7 +114,9 @@
</span><span class="cx">         char *custom_sql;
</span><span class="cx">         switch_bool_t custom_sql_has_percent;
</span><span class="cx">         switch_bool_t custom_sql_has_vars;
</span><del>-        switch_bool_t profile_has_intra;
</del><ins>+        switch_bool_t profile_has_intrastate;
+        switch_bool_t profile_has_intralata;
+        switch_bool_t profile_has_npanxx;
</ins><span class="cx">         
</span><span class="cx">         switch_bool_t reorder_by_rate;
</span><span class="cx">         switch_bool_t quote_in_list;
</span><span class="lines">@@ -130,6 +132,7 @@
</span><span class="cx">         char *lookup_number;
</span><span class="cx">         char *cid;
</span><span class="cx">         switch_bool_t intrastate;
</span><ins>+        switch_bool_t intralata;
</ins><span class="cx">         profile_t *profile;
</span><span class="cx">         switch_core_session_t *session;
</span><span class="cx">         switch_event_t *event;
</span><span class="lines">@@ -615,6 +618,66 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+static int intrastatelata_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        int count = 0;
+        callback_t *cbt = (callback_t *) pArg;
+        
+        count = atoi(argv[1]);
+        if (count == 1) {
+                if (!strcmp(argv[0], "state")) {
+                        cbt->intrastate = SWITCH_TRUE;
+                } else if (!strcmp(argv[0], "lata")) {
+                        cbt->intralata = SWITCH_TRUE;
+                }
+        }
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Type: %s, Count: %d\n", argv[0], count);
+        
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t is_intrastatelata(callback_t *cb_struct)
+{
+        char *sql = NULL;
+        
+        /* extract npa nxx - make some assumptions about format:
+                e164 format without the +
+                NANP only (so 11 digits starting with 1)
+        */
+        if (!cb_strcut->lookup_number && strlen(cb_struct->lookup_number) != 11 && *cb_struct->lookup_number != '1') {
+                /* dest doesn't appear to be NANP number */
+                return SWITCH_STATUS_GENERR;
+        }
+        if (!cb_struct->cid && strlen(cb_struct->cid) != 11 && *cb_struct->cid != '1') {
+                /* cid not NANP */
+                return SWITCH_STATUS_GENERR;
+        }
+        
+        /* assume that if the area code (plus leading 1) are the same we're intrastate */
+        /* probably a bad assumption */
+        /*
+        if (!strncmp(cb_struct->lookup_number, cb_struct->cid, 4)) {
+                cb_struct->intrastate = SWITCH_TRUE;
+                return SWITCH_STATUS_SUCCESS;
+        }
+        */
+        
+        sql = switch_core_sprintf(cb_struct->pool,
+                                                                "SELECT 'state', count(DISTINCT state) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)"
+                                                                " UNION "
+                                                                "SELECT 'lata', count(DISTINCT lata) FROM npa_nxx_company_ocn WHERE (npa=%3.3s AND nxx=%3.3s) OR (npa=%3.3s AND nxx=%3.3s)",
+                                                                cb_struct->lookup_number+1, cb_struct->lookup_number+4,
+                                                                cb_struct->cid+1, cb_struct->cid+4,
+                                                                cb_struct->lookup_number+1, cb_struct->lookup_number+4,
+                                                                cb_struct->cid+1, cb_struct->cid+4);
+
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
+
+        return(lcr_execute_sql_callback(sql, intrastatelata_callback, cb_struct));
+        
+}
+
</ins><span class="cx"> static switch_status_t lcr_do_lookup(callback_t *cb_struct)
</span><span class="cx"> {
</span><span class="cx">         switch_stream_handle_t sql_stream = { 0 };
</span><span class="lines">@@ -643,14 +706,20 @@
</span><span class="cx">         
</span><span class="cx">         digits_expanded = expand_digits(cb_struct->pool, digits_copy, cb_struct->profile->quote_in_list);
</span><span class="cx">         
</span><ins>+        if (profile->profile_has_npanxx == SWITCH_TRUE) {
+                is_intrastatelata(cb_struct);
+        }
+        
</ins><span class="cx">         /* set our rate field based on env and profile */
</span><del>-        if (cb_struct->intrastate == SWITCH_TRUE && profile->profile_has_intra == SWITCH_TRUE) {
-                rate_field = switch_core_strdup(cb_struct->pool, "intra");
</del><ins>+        if (cb_struct->intralata == SWITCH_TRUE && profile->profile_has_intralata == SWITCH_TRUE) {
+                rate_field = switch_core_strdup(cb_struct->pool, "intralata_rate");
+        } else if (cb_struct->intrastate == SWITCH_TRUE && profile->profile_has_intrastate == SWITCH_TRUE) {
+                rate_field = switch_core_strdup(cb_struct->pool, "intrastate_rate");
</ins><span class="cx">         } else {
</span><span class="cx">                 rate_field = switch_core_strdup(cb_struct->pool, "rate");
</span><span class="cx">         }
</span><del>-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "intrastate routing [%d] so rate field is [%s]\n",
-                                         cb_struct->intrastate, rate_field);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "intra routing [state:%d lata:%d] so rate field is [%s]\n",
+                                         cb_struct->intrastate, cb_struct->intralata, rate_field);
</ins><span class="cx">         
</span><span class="cx">         /* set some channel vars if we have a session */
</span><span class="cx">         if (cb_struct->session) {
</span><span class="lines">@@ -901,12 +970,25 @@
</span><span class="cx">                                 }
</span><span class="cx">                                 
</span><span class="cx">                                 
</span><del>-                                profile->profile_has_intra = db_check("SELECT intra FROM lcr LIMIT 1");
-                                if (profile->profile_has_intra != SWITCH_TRUE) {
</del><ins>+                                profile->profile_has_intralata = db_check("SELECT intralata_rate FROM lcr LIMIT 1");
+                                if (profile->profile_has_intralata != SWITCH_TRUE) {
</ins><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
</span><del>-                                                                         "no \"intra\" field found in the \"lcr\" table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n"
</del><ins>+                                                                         "no \"intralata_rate\" field found in the \"lcr\" table, routing by intralata rates will be disabled until the field is added and mod_lcr is reloaded\n"
</ins><span class="cx">                                                                          );
</span><span class="cx">                                 }
</span><ins>+                                profile->profile_has_intrastate = db_check("SELECT intrastate_rate FROM lcr LIMIT 1");
+                                if (profile->profile_has_intrastate != SWITCH_TRUE) {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                                                         "no \"intrastate_rate\" field found in the \"lcr\" table, routing by intrastate rates will be disabled until the field is added and mod_lcr is reloaded\n"
+                                                                         );
+                                }
+                                
+                                profile->profile_has_npanxx = db_check("SELECT npa, nxx, state FROM npa_nxx_company_ocn LIMIT 1");
+                                if (profile->profile_has_npanxx != SWITCH_TRUE) {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
+                                                                         "no \"npa_nxx_company_ocn\" table found in the \"lcr\" database, automatic intrastate detection will be disabled until the table is added and mod_lcr is reloaded\n"
+                                                                         );
+                                }
</ins><span class="cx">
</span><span class="cx">                                 if (switch_string_var_check_const(custom_sql) || switch_string_has_escaped_data(custom_sql)) {
</span><span class="cx">                                         profile->custom_sql_has_vars = SWITCH_TRUE;
</span><span class="lines">@@ -973,7 +1055,8 @@
</span><span class="cx">         char *lcr_profile = NULL;
</span><span class="cx">         switch_memory_pool_t *pool = NULL;
</span><span class="cx">         switch_event_t *event = NULL;
</span><del>-        const char *intra = NULL;
</del><ins>+        const char *intrastate = NULL;
+        const char *intralata = NULL;
</ins><span class="cx">
</span><span class="cx">         if (session) {
</span><span class="cx">                 pool = switch_core_session_get_pool(session);
</span><span class="lines">@@ -989,14 +1072,19 @@
</span><span class="cx">                 goto end;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        intra = switch_channel_get_variable(channel, "intrastate");
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "intrastate channel var is [%s]\n", intra);
-        if (switch_strlen_zero(intra) || strcasecmp((char *)intra, "true")) {
</del><ins>+        intrastate = switch_channel_get_variable(channel, "intrastate");
+        intralata = switch_channel_get_variable(channel, "intralata");
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "intrastate channel var is [%s]\n", intrastate);
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "intralata channel var is [%s]\n", intralata);
+        if (!switch_strlen_zero(intralata) && !strcasecmp((char *)intralata, "true")) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on intralata rates\n");
+                routes.intralata = SWITCH_FALSE;
+        } else if (!switch_strlen_zero(intrastate) && !strcasecmp((char *)intrastate, "true")) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on intrastate rates\n");
+                routes.intrastate = SWITCH_TRUE;
+        } else {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on interstate rates\n");
</span><span class="cx">                 routes.intrastate = SWITCH_FALSE;
</span><del>-        } else {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Select routes based on intrastate rates\n");
-                routes.intrastate = SWITCH_TRUE;
</del><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         if (!caller_profile) {
</span><span class="lines">@@ -1340,6 +1428,9 @@
</span><span class="cx">                                         stream->write_function(stream, " has %%:\t\t%s\n", profile->custom_sql_has_percent ? "true" : "false");
</span><span class="cx">                                         stream->write_function(stream, " has vars:\t%s\n", profile->custom_sql_has_vars ? "true" : "false");
</span><span class="cx">                                 }
</span><ins>+                                stream->write_function(stream, " has intrastate:\t%s\n", profile->profile_has_intrastate ? "true" : "false");
+                                stream->write_function(stream, " has intralata:\t%s\n", profile->profile_has_intralata ? "true" : "false");
+                                stream->write_function(stream, " has npanxx:\t%s\n", profile->profile_has_npanxx ? "true" : "false");
</ins><span class="cx">                                 stream->write_function(stream, " Reorder rate:\t%s\n",
</span><span class="cx">                                                                                 profile->reorder_by_rate ? "enabled" : "disabled");
</span><span class="cx">                                 stream->write_function(stream, " Info in headers:\t%s\n",
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>