<!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][15023] </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=15023">15023</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-09-30 12:23:01 -0500 (Wed, 30 Sep 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>mod_cidlookup: add whitepages.com support</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkconfautoload_configscidlookupconfxml">freeswitch/trunk/conf/autoload_configs/cidlookup.conf.xml</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc">freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkconfautoload_configscidlookupconfxml"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/conf/autoload_configs/cidlookup.conf.xml (15022 => 15023)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/conf/autoload_configs/cidlookup.conf.xml        2009-09-30 14:52:19 UTC (rev 15022)
+++ freeswitch/trunk/conf/autoload_configs/cidlookup.conf.xml        2009-09-30 17:23:01 UTC (rev 15023)
</span><span class="lines">@@ -3,6 +3,10 @@
</span><span class="cx"> <!-- comment out url to not setup a url based lookup -->
</span><span class="cx"> <param name="url" value="http://query.voipcnam.com/query.php?api_key=MYAPIKEY&number=${caller_id_number}"/>
</span><span class="cx">
</span><ins>+ <!-- comment out whitepages-apikey to not use whitepages.com, you must
+ get an API key from http://developer.whitepages.com/ -->
+ <param name="whitepages-apikey" value="MYAPIKEY"/>
+
</ins><span class="cx"> <!-- set to false to not cache (in memcache) results from the url query -->
</span><span class="cx"> <param name="cache" value="true"/>
</span><span class="cx"> <!-- expire is in seconds -->
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c (15022 => 15023)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-09-30 14:52:19 UTC (rev 15022)
+++ freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-09-30 17:23:01 UTC (rev 15023)
</span><span class="lines">@@ -46,6 +46,8 @@
</span><span class="cx">
</span><span class="cx"> static struct {
</span><span class="cx">         char *url;
</span><ins>+        
+        char *whitepages_apikey;
</ins><span class="cx">
</span><span class="cx">         switch_bool_t cache;
</span><span class="cx">         int cache_expire;
</span><span class="lines">@@ -145,6 +147,7 @@
</span><span class="cx"> static switch_xml_config_item_t instructions[] = {
</span><span class="cx">         /* parameter name type reloadable pointer default value options structure */
</span><span class="cx">         SWITCH_CONFIG_ITEM_STRING_STRDUP("url", CONFIG_RELOAD, &globals.url, NULL, "http://server.example.com/app?number=${caller_id_number}", "URL for the CID lookup service"),
</span><ins>+        SWITCH_CONFIG_ITEM_STRING_STRDUP("whitepages-apikey", CONFIG_RELOAD, &globals.whitepages_apikey, NULL, "api key for whitepages.com", "api key for whitepages.com"),
</ins><span class="cx">         SWITCH_CONFIG_ITEM("cache", SWITCH_CONFIG_BOOL, CONFIG_RELOAD, &globals.cache, SWITCH_FALSE, NULL, "true|false", "whether to cache via cidlookup"),
</span><span class="cx">         SWITCH_CONFIG_ITEM("cache-expire", SWITCH_CONFIG_INT, CONFIG_RELOAD, &globals.cache_expire, (void *)300, NULL, "expire", "seconds to preserve num->name cache"),
</span><span class="cx">         SWITCH_CONFIG_ITEM_STRING_STRDUP("sql", CONFIG_RELOAD, &globals.sql, "", "sql whre number=${caller_id_number}", "SQL to run if overriding CID"),
</span><span class="lines">@@ -286,9 +289,8 @@
</span><span class="cx">         return realsize;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static char *do_lookup_url(switch_memory_pool_t *pool, switch_event_t *event, const char *num) {
</del><ins>+static char *do_lookup_url(switch_memory_pool_t *pool, switch_event_t *event, const char *query) {
</ins><span class="cx">         char *name = NULL;
</span><del>-        char *newurl = NULL;
</del><span class="cx">         
</span><span class="cx">         CURL *curl_handle = NULL;
</span><span class="cx">         long httpRes = 0;
</span><span class="lines">@@ -298,24 +300,22 @@
</span><span class="cx">         
</span><span class="cx">         memset(&http_data, 0, sizeof(http_data));
</span><span class="cx">         
</span><del>-        http_data.max_bytes = 1024;
</del><ins>+        http_data.max_bytes = 10240;
</ins><span class="cx">         SWITCH_STANDARD_STREAM(http_data.stream);
</span><span class="cx">
</span><span class="cx">         gethostname(hostname, sizeof(hostname));
</span><span class="cx">         
</span><del>-        newurl = switch_event_expand_headers(event, globals.url);
-        
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "url: %s\n", newurl);
</del><ins>+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "url: %s\n", query);
</ins><span class="cx">         curl_handle = curl_easy_init();
</span><span class="cx">         
</span><del>-        if (!strncasecmp(newurl, "https", 5)) {
</del><ins>+        if (!strncasecmp(query, "https", 5)) {
</ins><span class="cx">                 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
</span><span class="cx">                 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
</span><span class="cx">         }
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_POST, SWITCH_FALSE);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 10);
</span><del>-        curl_easy_setopt(curl_handle, CURLOPT_URL, newurl);
</del><ins>+        curl_easy_setopt(curl_handle, CURLOPT_URL, query);
</ins><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &http_data);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-cidlookup/1.0");
</span><span class="lines">@@ -331,13 +331,83 @@
</span><span class="cx">                 name = switch_core_strdup(pool, http_data.stream.data);
</span><span class="cx">         }
</span><span class="cx">         
</span><del>-        if (newurl != globals.url) {
-                switch_safe_free(newurl);
-        }
</del><span class="cx">         switch_safe_free(http_data.stream.data);
</span><span class="cx">         return name;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+static char *do_whitepages_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, switch_bool_t *areaonly)
+{
+        char *xml_s = NULL;
+        char *query = NULL;
+        char *name = NULL;
+        char *city = NULL;
+        char *state = NULL;
+        switch_xml_t xml = NULL;
+        switch_xml_t node = NULL;
+
+        /* NANPA check */
+        if (strlen(num) == 11 && num[0] == '1') {
+                num++; /* skip past leading 1 */
+        } else {
+                goto done;
+        }
+        
+        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "whitepages-cid", num);
+        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "whitepages-api-key", globals.whitepages_apikey);
+        
+        query = switch_event_expand_headers(event, "http://api.whitepages.com/reverse_phone/1.0/?phone=${whitepages-cid};api_key=${whitepages-api-key}");
+        xml_s = do_lookup_url(pool, event, query);
+        
+        xml = switch_xml_parse_str_dup(xml_s);
+        
+        if (!xml) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", xml_s);
+                goto done;
+        }
+
+        *areaonly = SWITCH_FALSE;
+        
+        /* try for bizname first */
+        node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:business", 0, "wp:businessname", -1);
+        if (node) {
+                name = switch_core_strdup(pool, switch_xml_txt(node));
+                goto done;
+        }
+        
+        node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:displayname", -1);
+        if (node) {
+                name = switch_core_strdup(pool, switch_xml_txt(node));
+                goto done;
+        }
+        
+        node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:address", 0, "wp:city", -1);
+        if (node) {
+                city = switch_xml_txt(node);
+        }
+
+        node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:address", 0, "wp:state", -1);
+        if (node) {
+                state = switch_xml_txt(node);
+        }
+        
+        if (city || state) {
+                *areaonly = SWITCH_TRUE;
+                name = switch_core_sprintf(pool, "%s %s", city ? city : "", state ? state : "");
+        }
+
+done:
+
+        if (query) {
+                switch_safe_free(query);
+        }
+        if (xml) {
+                switch_xml_free(xml);
+        }
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "whitepages XML: %s\n", xml_s);
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "whitepages name: %s\n", name ? name : "(null)");
+        return name;
+}
+
</ins><span class="cx"> static char *do_db_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, const char *sql) {
</span><span class="cx">         char *name = NULL;
</span><span class="cx">         char *newsql = NULL;
</span><span class="lines">@@ -362,6 +432,9 @@
</span><span class="cx"> static char *do_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, switch_bool_t skipurl, switch_bool_t skipcitystate) {
</span><span class="cx">         char *number = NULL;
</span><span class="cx">         char *name = NULL;
</span><ins>+        char *area = NULL;
+        char *url_query = NULL;
+        switch_bool_t areaonly = SWITCH_FALSE;
</ins><span class="cx">         
</span><span class="cx">         number = string_digitsonly(pool, num);
</span><span class="cx">         switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "caller_id_number", number);
</span><span class="lines">@@ -376,11 +449,32 @@
</span><span class="cx">                         name = check_cache(pool, number);
</span><span class="cx">                 }
</span><span class="cx">                 if (!skipurl && !name) {
</span><del>-                        name = do_lookup_url(pool, event, number);
</del><ins>+                        name = do_whitepages_lookup(pool, event, number, &areaonly);
+                        if (areaonly) {
+                                if (!skipcitystate) {
+                                        area = name; /* preserve if we're not skipping city/state */
+                                }
+                                name = NULL; /* always clear out name */
+                        }
</ins><span class="cx">                         if (globals.cache && name) {
</span><span class="cx">                                 set_cache(pool, number, name);
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><ins>+                if (!skipurl && !name) {
+                        url_query = switch_event_expand_headers(event, globals.url);
+                        name = do_lookup_url(pool, event, url_query);
+                        if (url_query != globals.url) {
+                                switch_safe_free(url_query);
+                        }
+                        
+                        /* store and use preserved area info */
+                        if (!name && area) {
+                                name = area;
+                        }
+                        if (globals.cache && name) {
+                                set_cache(pool, number, name);
+                        }
+                }
</ins><span class="cx">         }
</span><span class="cx">         /* only do the below if it is a nanpa number */
</span><span class="cx">         if (!skipcitystate && strlen(number) == 11 && number[0] == '1' &&
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>