1 /*
2 * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3 * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4 *
5 * Version: MPL 1.1
6 *
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18 *
19 * The Initial Developer of the Original Code is
20 * Anthony Minessale II <anthm@freeswitch.org>
21 * Portions created by the Initial Developer are Copyright (C)
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Rupa Schomaker <rupa@rupa.com>
27 *
28 * mod_cidlookup.c -- API for querying cid->name services and local data
29 *
30 */
31
32 #include <switch.h>
33 #include <switch_curl.h>
34
35 #define SWITCH_REWIND_STREAM(s) s.end = s.data
36
37 /* Prototypes */
38 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cidlookup_shutdown);
39 SWITCH_MODULE_RUNTIME_FUNCTION(mod_cidlookup_runtime);
40 SWITCH_MODULE_LOAD_FUNCTION(mod_cidlookup_load);
41
42 /* SWITCH_MODULE_DEFINITION(name, load, shutdown, runtime)
43 * Defines a switch_loadable_module_function_table_t and a static const char[] modname
44 */
45 SWITCH_MODULE_DEFINITION(mod_cidlookup, mod_cidlookup_load, mod_cidlookup_shutdown, NULL);
46
47 static char *SYNTAX = "cidlookup status|number [skipurl] [skipcitystate] [verbose]";
48
49 static struct {
50 char *url;
51 int curl_timeout;
52 int curl_warnduration;
53
54 char *whitepages_apikey;
55
56 switch_bool_t cache;
57 int cache_expire;
58
59 char *odbc_dsn;
60 char *sql;
61 char *citystate_sql;
62
63 switch_memory_pool_t *pool;
64 } globals;
65
66 struct http_data {
67 switch_stream_handle_t stream;
68 switch_size_t bytes;
69 switch_size_t max_bytes;
70 int err;
71 };
72
73 struct cid_data_obj {
74 char *name;
75 char *area;
76 char *src;
77 };
78 typedef struct cid_data_obj cid_data_t;
79
80
81 struct callback_obj {
82 switch_memory_pool_t *pool;
83 char *name;
84 };
85 typedef struct callback_obj callback_t;
86
87 static switch_event_node_t *reload_xml_event = NULL;
88
cidlookup_get_db_handle(void)89 static switch_cache_db_handle_t *cidlookup_get_db_handle(void)
90 {
91 switch_cache_db_handle_t *dbh = NULL;
92 char *dsn;
93
94 if (!zstr(globals.odbc_dsn)) {
95 dsn = globals.odbc_dsn;
96 } else {
97 return NULL;
98 }
99
100 if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
101 dbh = NULL;
102 }
103
104 return dbh;
105 }
106
107
config_callback_dsn(switch_xml_config_item_t * data,const char * newvalue,switch_config_callback_type_t callback_type,switch_bool_t changed)108 static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const char *newvalue, switch_config_callback_type_t callback_type,
109 switch_bool_t changed)
110 {
111 switch_status_t status = SWITCH_STATUS_SUCCESS;
112
113 switch_cache_db_handle_t *dbh = NULL;
114
115 if ((callback_type == CONFIG_LOAD || callback_type == CONFIG_RELOAD) && changed) {
116
117 if (zstr(newvalue)) {
118 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No local database defined.\n");
119 } else {
120 switch_safe_free(globals.odbc_dsn);
121 globals.odbc_dsn = strdup(newvalue);
122 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting to dsn: %s\n", globals.odbc_dsn);
123
124 if (!(dbh = cidlookup_get_db_handle())) {
125 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open Database!\n");
126 switch_goto_status(SWITCH_STATUS_FALSE, done);
127 }
128 }
129 }
130
131 switch_goto_status(SWITCH_STATUS_SUCCESS, done);
132
133 done:
134 switch_cache_db_release_db_handle(&dbh);
135 return status;
136 }
137
138 static switch_xml_config_string_options_t config_opt_dsn = { NULL, 0, NULL }; /* anything is ok here */
139 static switch_xml_config_item_t instructions[] = {
140 /* parameter name type reloadable pointer default value options structure */
141 SWITCH_CONFIG_ITEM_STRING_STRDUP("url", CONFIG_RELOAD, &globals.url, NULL, "http://server.example.com/app?number=${caller_id_number}",
142 "URL for the CID lookup service"),
143 SWITCH_CONFIG_ITEM_STRING_STRDUP("whitepages-apikey", CONFIG_RELOAD, &globals.whitepages_apikey, NULL, "api key for whitepages.com",
144 "api key for whitepages.com"),
145 SWITCH_CONFIG_ITEM("cache", SWITCH_CONFIG_BOOL, CONFIG_RELOAD, &globals.cache, SWITCH_FALSE, NULL, "true|false", "whether to cache via cidlookup"),
146 SWITCH_CONFIG_ITEM("cache-expire", SWITCH_CONFIG_INT, CONFIG_RELOAD, &globals.cache_expire, (void *) 300, NULL, "expire",
147 "seconds to preserve num->name cache"),
148 SWITCH_CONFIG_ITEM("curl-timeout", SWITCH_CONFIG_INT, CONFIG_RELOAD, &globals.curl_timeout, (void *) 2000, NULL, "timeout for curl",
149 "milliseconds to timeout"),
150 SWITCH_CONFIG_ITEM("curl-warning-duration", SWITCH_CONFIG_INT, CONFIG_RELOAD, &globals.curl_warnduration, (void *) 1000, NULL,
151 "warning when curl queries are longer than specified time", "milliseconds"),
152 SWITCH_CONFIG_ITEM_STRING_STRDUP("sql", CONFIG_RELOAD, &globals.sql, NULL, "sql whre number=${caller_id_number}", "SQL to run if overriding CID"),
153 SWITCH_CONFIG_ITEM_STRING_STRDUP("citystate-sql", CONFIG_RELOAD, &globals.citystate_sql, NULL, "sql to look up city/state info",
154 "SQL to run if overriding CID"),
155 SWITCH_CONFIG_ITEM_CALLBACK("odbc-dsn", SWITCH_CONFIG_STRING, CONFIG_RELOAD, &globals.odbc_dsn, "", config_callback_dsn, &config_opt_dsn,
156 "db:user:passwd", "Database to use."),
157 SWITCH_CONFIG_ITEM_END()
158 };
159
do_config(switch_bool_t reload)160 static switch_status_t do_config(switch_bool_t reload)
161 {
162 if (switch_xml_config_parse_module_settings("cidlookup.conf", reload, instructions) != SWITCH_STATUS_SUCCESS) {
163 return SWITCH_STATUS_GENERR;
164 }
165
166 return SWITCH_STATUS_SUCCESS;
167 }
168
event_handler(switch_event_t * event)169 static void event_handler(switch_event_t *event)
170 {
171 do_config(SWITCH_TRUE);
172 }
173
cidlookup_execute_sql_callback(char * sql,switch_core_db_callback_func_t callback,callback_t * cbt,char ** err)174 static switch_bool_t cidlookup_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, callback_t *cbt, char **err)
175 {
176 switch_bool_t retval = SWITCH_FALSE;
177 switch_cache_db_handle_t *dbh = NULL;
178
179 if (!zstr(globals.odbc_dsn) && (dbh = cidlookup_get_db_handle())) {
180 if (switch_cache_db_execute_sql_callback(dbh, sql, callback, (void *) cbt, err) != SWITCH_STATUS_SUCCESS) {
181 retval = SWITCH_FALSE;
182 } else {
183 retval = SWITCH_TRUE;
184 }
185 } else {
186 *err = switch_core_sprintf(cbt->pool, "Unable to get database handle. dsn: [%s]\n", switch_str_nil(globals.odbc_dsn));
187 }
188
189 switch_cache_db_release_db_handle(&dbh);
190 return retval;
191 }
192
cidlookup_callback(void * pArg,int argc,char ** argv,char ** columnNames)193 static int cidlookup_callback(void *pArg, int argc, char **argv, char **columnNames)
194 {
195 callback_t *cbt = (callback_t *) pArg;
196 switch_memory_pool_t *pool = cbt->pool;
197
198 if (argc < 1) {
199 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unexpected number of columns returned for SQL. Returned column count: %d. ", argc);
200 return SWITCH_STATUS_GENERR;
201 }
202 cbt->name = switch_core_strdup(pool, switch_str_nil(argv[0]));
203 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "Name: %s\n", cbt->name);
204
205 return SWITCH_STATUS_SUCCESS;
206 }
207
208 /* make a new string with digits only */
string_digitsonly(switch_memory_pool_t * pool,const char * str)209 static char *string_digitsonly(switch_memory_pool_t *pool, const char *str)
210 {
211 char *p, *np, *newstr;
212 size_t len;
213
214 p = (char *) str;
215
216 len = strlen(str);
217 newstr = switch_core_alloc(pool, len + 1);
218 switch_assert(newstr);
219 np = newstr;
220
221 while (*p) {
222 if (switch_isdigit(*p)) {
223 *np = *p;
224 np++;
225 }
226 p++;
227 }
228 *np = '\0';
229
230 return newstr;
231 }
232
check_cache(switch_memory_pool_t * pool,const char * number)233 static cid_data_t *check_cache(switch_memory_pool_t *pool, const char *number)
234 {
235 char *cmd;
236 char *name = NULL;
237 char *area = NULL;
238 char *src = NULL;
239 cid_data_t *cid = NULL;
240 switch_stream_handle_t stream = { 0 };
241
242 SWITCH_STANDARD_STREAM(stream);
243
244 cmd = switch_core_sprintf(pool, "get fs:cidlookup:name:%s", number);
245 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
246 if (strncmp("-ERR", stream.data, 4)) {
247 name = switch_core_strdup(pool, stream.data);
248 } else {
249 name = NULL;
250 }
251 }
252
253 SWITCH_REWIND_STREAM(stream);
254 cmd = switch_core_sprintf(pool, "get fs:cidlookup:area:%s", number);
255 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
256 if (strncmp("-ERR", stream.data, 4)) {
257 area = switch_core_strdup(pool, stream.data);
258 } else {
259 area = NULL;
260 }
261 }
262
263 SWITCH_REWIND_STREAM(stream);
264 cmd = switch_core_sprintf(pool, "get fs:cidlookup:src:%s", number);
265 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
266 if (strncmp("-ERR", stream.data, 4)) {
267 src = switch_core_strdup(pool, stream.data);
268 } else {
269 src = NULL;
270 }
271 }
272
273 if (name || area || src) {
274 cid = switch_core_alloc(pool, sizeof(cid_data_t));
275 switch_assert(cid);
276 cid->name = name;
277 cid->area = area;
278 cid->src = src;
279 }
280
281 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "memcache: k:'%s', vn:'%s', va:'%s', vs:'%s'\n",
282 cmd, (name) ? name : "(null)", (area) ? area : "(null)", (src) ? src : "(null)");
283 switch_safe_free(stream.data);
284 return cid;
285 }
286
set_cache(switch_memory_pool_t * pool,char * number,cid_data_t * cid)287 switch_bool_t set_cache(switch_memory_pool_t *pool, char *number, cid_data_t *cid)
288 {
289 char *cmd;
290 switch_bool_t success = SWITCH_TRUE;
291 switch_stream_handle_t stream = { 0 };
292
293 SWITCH_STANDARD_STREAM(stream);
294
295 cmd = switch_core_sprintf(pool, "set fs:cidlookup:name:%s '%s' %d", number, cid->name, globals.cache_expire);
296 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "memcache: %s\n", cmd);
297 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
298 if (strncmp("-ERR", stream.data, 4)) {
299 success = SWITCH_TRUE;
300 } else {
301 success = SWITCH_FALSE;
302 goto done;
303 }
304 }
305
306 SWITCH_REWIND_STREAM(stream);
307 cmd = switch_core_sprintf(pool, "set fs:cidlookup:area:%s '%s' %d", number, cid->area, globals.cache_expire);
308 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
309 if (strncmp("-ERR", stream.data, 4)) {
310 success = SWITCH_TRUE;
311 } else {
312 success = SWITCH_FALSE;
313 goto done;
314 }
315 }
316
317 SWITCH_REWIND_STREAM(stream);
318 cmd = switch_core_sprintf(pool, "set fs:cidlookup:src:%s '%s' %d", number, cid->src, globals.cache_expire);
319 if (switch_api_execute("memcache", cmd, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
320 if (strncmp("-ERR", stream.data, 4)) {
321 success = SWITCH_TRUE;
322 } else {
323 success = SWITCH_FALSE;
324 goto done;
325 }
326 }
327
328 done:
329 switch_safe_free(stream.data);
330 return success;
331 }
332
file_callback(void * ptr,size_t size,size_t nmemb,void * data)333 static size_t file_callback(void *ptr, size_t size, size_t nmemb, void *data)
334 {
335 register unsigned int realsize = (unsigned int) (size * nmemb);
336 struct http_data *http_data = data;
337
338 http_data->bytes += realsize;
339
340 if (http_data->bytes > http_data->max_bytes) {
341 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Oversized file detected [%d bytes]\n", (int) http_data->bytes);
342 http_data->err = 1;
343 return 0;
344 }
345
346 http_data->stream.write_function(&http_data->stream, "%.*s", realsize, ptr);
347 return realsize;
348 }
349
do_lookup_url(switch_memory_pool_t * pool,switch_event_t * event,char ** response,const char * query,struct curl_httppost * post,switch_curl_slist_t * headers,int timeout)350 static long do_lookup_url(switch_memory_pool_t *pool, switch_event_t *event, char **response, const char *query, struct curl_httppost *post,
351 switch_curl_slist_t *headers, int timeout)
352 {
353 switch_time_t start_time = switch_micro_time_now();
354 switch_time_t time_diff = 0;
355 CURL *curl_handle = NULL;
356 long httpRes = 0;
357
358 struct http_data http_data;
359
360 memset(&http_data, 0, sizeof(http_data));
361
362 http_data.max_bytes = 10240;
363 SWITCH_STANDARD_STREAM(http_data.stream);
364
365 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "url: %s\n", query);
366 curl_handle = switch_curl_easy_init();
367
368 switch_curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 0);
369 switch_curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
370
371 if (!strncasecmp(query, "https", 5)) {
372 switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
373 switch_curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
374 }
375 if (post) {
376 switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, post);
377 } else {
378 switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
379 }
380 if (headers) {
381 switch_curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
382 }
383 switch_curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
384 switch_curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 10);
385 /*
386 TIMEOUT_MS is introduced in 7.16.2, we have 7.16.0 in tree
387 */
388 #ifdef CURLOPT_TIMEOUT_MS
389 if (timeout > 0) {
390 switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, timeout);
391 } else {
392 switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, globals.curl_timeout);
393 }
394 #else
395 if (timeout > 0) {
396 switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, timeout);
397 } else {
398 switch_curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, globals.curl_timeout / 1000);
399 }
400 #endif
401 switch_curl_easy_setopt(curl_handle, CURLOPT_URL, query);
402 switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
403 switch_curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &http_data);
404 switch_curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-cidlookup/1.0");
405
406 switch_curl_easy_perform(curl_handle);
407 switch_curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpRes);
408 switch_curl_easy_cleanup(curl_handle);
409
410 if (http_data.stream.data && !zstr((char *) http_data.stream.data) && strcmp(" ", http_data.stream.data)) {
411
412 /* don't return UNKNOWN or UNAVAILABLE */
413 if (strcasecmp("UNKNOWN", http_data.stream.data) && strcasecmp("UNAVAILABLE", http_data.stream.data)) {
414 *response = switch_core_strdup(pool, http_data.stream.data);
415 }
416 }
417
418 time_diff = (switch_micro_time_now() - start_time); /* convert to milli from micro */
419
420 if ((time_diff / 1000) >= globals.curl_warnduration) {
421 switch_core_time_duration_t duration;
422 switch_core_measure_time(time_diff, &duration);
423 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SLOW LOOKUP ("
424 "%um, " "%us, " "%ums" "): url: %s\n", duration.min, duration.sec, duration.ms, query);
425 }
426
427 switch_safe_free(http_data.stream.data);
428 return httpRes;
429 }
430
do_whitepages_lookup(switch_memory_pool_t * pool,switch_event_t * event,const char * num)431 static cid_data_t *do_whitepages_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num)
432 {
433 char *xml_s = NULL;
434 char *query = NULL;
435 char *name = NULL;
436 char *city = NULL;
437 char *state = NULL;
438 char *area = NULL;
439 switch_xml_t xml = NULL;
440 switch_xml_t node = NULL;
441 cid_data_t *cid = NULL;
442
443 /* NANPA check */
444 if (strlen(num) == 11 && num[0] == '1') {
445 num++; /* skip past leading 1 */
446 } else {
447 goto done;
448 }
449
450 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "whitepages-cid", num);
451 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "whitepages-api-key", globals.whitepages_apikey);
452
453 query = switch_event_expand_headers(event, "http://api.whitepages.com/reverse_phone/1.0/?phone=${whitepages-cid};api_key=${whitepages-api-key}");
454 do_lookup_url(pool, event, &xml_s, query, NULL, NULL, 0);
455
456 if (zstr(xml_s)) {
457 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No XML returned for number %s\n", num);
458 goto done;
459 }
460
461 xml = switch_xml_parse_str_dup(xml_s);
462
463 if (!xml) {
464 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", xml_s);
465 goto done;
466 }
467
468 /* try for bizname first */
469 node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:business", 0, "wp:businessname", -1);
470 if (node) {
471 name = switch_core_strdup(pool, switch_xml_txt(node));
472 goto area;
473 }
474
475 node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:displayname", -1);
476 if (node) {
477 name = switch_core_strdup(pool, switch_xml_txt(node));
478 }
479
480 area:
481
482 node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:address", 0, "wp:city", -1);
483 if (node) {
484 city = switch_xml_txt(node);
485 }
486
487 node = switch_xml_get(xml, "wp:listings", 0, "wp:listing", 0, "wp:address", 0, "wp:state", -1);
488 if (node) {
489 state = switch_xml_txt(node);
490 }
491
492 if (city || state) {
493 area = switch_core_sprintf(pool, "%s %s", city ? city : "", state ? state : "");
494 }
495
496 done:
497
498 if (query) {
499 switch_safe_free(query);
500 }
501 if (xml) {
502 switch_xml_free(xml);
503 }
504 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "whitepages XML: %s\n", xml_s);
505 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "whitepages name: %s, area: %s\n", name ? name : "(null)", area ? area : "(null)");
506
507 cid = switch_core_alloc(pool, sizeof(cid_data_t));
508 switch_assert(cid);
509
510 cid->name = name;
511 cid->area = area;
512 cid->src = "whitepages";
513 return cid;
514 }
515
do_db_lookup(switch_memory_pool_t * pool,switch_event_t * event,const char * num,const char * sql)516 static char *do_db_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, const char *sql)
517 {
518 char *name = NULL;
519 char *newsql = NULL;
520 char *err = NULL;
521 callback_t cbt = { 0 };
522 cbt.pool = pool;
523
524 if (!zstr(globals.odbc_dsn)) {
525 newsql = switch_event_expand_headers(event, sql);
526 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "SQL: %s\n", newsql);
527 if (cidlookup_execute_sql_callback(newsql, cidlookup_callback, &cbt, &err)) {
528 name = cbt.name;
529 } else {
530 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to lookup cid: %s\n", err ? err : "(null)");
531 }
532 }
533 if (newsql != globals.sql) {
534 switch_safe_free(newsql);
535 }
536 return name;
537 }
538
do_lookup(switch_memory_pool_t * pool,switch_event_t * event,const char * num,switch_bool_t skipurl,switch_bool_t skipcitystate)539 static cid_data_t *do_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, switch_bool_t skipurl, switch_bool_t skipcitystate)
540 {
541 char *number = NULL;
542 char *name = NULL;
543 char *url_query = NULL;
544 cid_data_t *cid = NULL;
545 cid_data_t *cidtmp = NULL;
546 switch_bool_t save_cache = SWITCH_FALSE;
547
548 number = string_digitsonly(pool, num);
549 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "caller_id_number", number);
550
551 /* database always wins */
552 if (globals.odbc_dsn && globals.sql) {
553 name = do_db_lookup(pool, event, number, globals.sql);
554 if (name) {
555 cid = switch_core_alloc(pool, sizeof(cid_data_t));
556 switch_assert(cid);
557 cid->name = name;
558 cid->src = "phone_database";
559 goto done;
560 }
561 }
562
563 if (globals.cache) {
564 cidtmp = check_cache(pool, number);
565 if (cidtmp) {
566 cid = cidtmp;
567 cid->src = switch_core_sprintf(pool, "%s (cache)", cid->src);
568 goto done;
569 }
570 }
571
572 if (!skipurl && globals.whitepages_apikey) {
573 cid = do_whitepages_lookup(pool, event, number);
574 if (cid && cid->name) { /* only cache if we have a name */
575 save_cache = SWITCH_TRUE;
576 goto done;
577 }
578 }
579
580 if (!cid) {
581 cid = switch_core_alloc(pool, sizeof(cid_data_t));
582 switch_assert(cid);
583 }
584
585 if (!skipurl && globals.url) {
586 url_query = switch_event_expand_headers(event, globals.url);
587 do_lookup_url(pool, event, &name, url_query, NULL, NULL, 0);
588 if (name) {
589 cid->name = name;
590 cid->src = "url";
591
592 save_cache = SWITCH_TRUE;
593 }
594 if (url_query != globals.url) {
595 switch_safe_free(url_query);
596 }
597 }
598
599 done:
600 /* append area if we can */
601 if (!cid->area && !skipcitystate && strlen(number) == 11 && number[0] == '1' && globals.odbc_dsn && globals.citystate_sql) {
602
603 /* yes, this is really area */
604 name = do_db_lookup(pool, event, number, globals.citystate_sql);
605 if (name) {
606 cid->area = name;
607 if (cid->src) {
608 cid->src = switch_core_sprintf(pool, "%s,%s", cid->src, "npanxx_database");
609 } else {
610 cid->src = "npanxx_database";
611 }
612 }
613 }
614
615 if (!cid->area) {
616 cid->area = "UNKNOWN";
617 }
618 if (!cid->name) {
619 if (skipcitystate) {
620 if (strlen(number) == 11 && number[0] == '1') {
621 int a, b, c;
622 sscanf(number, "1%3d%3d%4d", &a, &b, &c);
623 cid->name = switch_core_sprintf(pool, "%03d-%03d-%04d", a, b, c);
624 } else {
625 cid->name = number;
626 }
627 } else {
628 cid->name = cid->area;
629 }
630 }
631 if (!cid->src) {
632 cid->src = "UNKNOWN";
633 }
634
635 if (globals.cache && save_cache) {
636 set_cache(pool, number, cid);
637 }
638
639
640 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "cidlookup source: %s\n", cid->src);
641 return cid;
642 }
643
SWITCH_STANDARD_APP(cidlookup_app_function)644 SWITCH_STANDARD_APP(cidlookup_app_function)
645 {
646 char *argv[4] = { 0 };
647 int argc;
648 char *mydata = NULL;
649 int i;
650
651 switch_memory_pool_t *pool = NULL;
652 switch_event_t *event = NULL;
653 switch_channel_t *channel = switch_core_session_get_channel(session);
654 switch_caller_profile_t *profile = switch_channel_get_caller_profile(channel);
655 cid_data_t *cid = NULL;
656 const char *number = NULL;
657 switch_bool_t skipurl = SWITCH_FALSE;
658 switch_bool_t skipcitystate = SWITCH_FALSE;
659
660 pool = switch_core_session_get_pool(session);
661 switch_event_create(&event, SWITCH_EVENT_MESSAGE);
662
663 if (!(mydata = switch_core_session_strdup(session, data))) {
664 return;
665 }
666
667 if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
668 if (argc > 0) {
669 number = switch_core_session_strdup(session, argv[0]);
670 }
671 for (i = 1; i < argc; i++) {
672 if (!strcasecmp(argv[i], "skipurl")) {
673 skipurl = SWITCH_TRUE;
674 } else if (!strcasecmp(argv[i], "skipcitystate")) {
675 skipcitystate = SWITCH_TRUE;
676 }
677 }
678 }
679
680 if (!number && profile) {
681 number = switch_caller_get_field_by_name(profile, "caller_id_number");
682 }
683
684 if (number) {
685 cid = do_lookup(pool, event, number, skipurl, skipcitystate);
686 }
687
688 if (cid && channel) {
689 switch_event_t *event;
690
691 if (switch_string_var_check_const(cid->name)) {
692 switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Invalid CID data {%s} contains a variable\n", cid->name);
693 goto done;
694 }
695
696 switch_channel_set_variable(channel, "original_caller_id_name", switch_core_strdup(pool, profile->caller_id_name));
697 if (!zstr(cid->src)) {
698 switch_channel_set_variable(channel, "cidlookup_source", cid->src);
699 }
700 if (!zstr(cid->area)) {
701 switch_channel_set_variable(channel, "cidlookup_area", cid->area);
702 }
703 profile->caller_id_name = switch_core_strdup(profile->pool, cid->name);;
704
705
706 if (switch_event_create(&event, SWITCH_EVENT_CALL_UPDATE) == SWITCH_STATUS_SUCCESS) {
707 const char *uuid = switch_channel_get_partner_uuid(channel);
708 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Direction", "RECV");
709
710 if (uuid) {
711 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Bridged-To", uuid);
712 }
713 switch_channel_event_set_data(channel, event);
714 switch_event_fire(&event);
715 }
716
717 }
718
719
720 done:
721 if (event) {
722 switch_event_destroy(&event);
723 }
724 if (!session && pool) {
725 switch_core_destroy_memory_pool(&pool);
726 }
727 }
728
SWITCH_STANDARD_API(cidlookup_function)729 SWITCH_STANDARD_API(cidlookup_function)
730 {
731 switch_status_t status;
732 char *argv[4] = { 0 };
733 int argc;
734 int i;
735 cid_data_t *cid = NULL;
736 char *mydata = NULL;
737
738 switch_memory_pool_t *pool = NULL;
739 switch_event_t *event = NULL;
740 switch_bool_t skipurl = SWITCH_FALSE;
741 switch_bool_t skipcitystate = SWITCH_FALSE;
742 switch_bool_t verbose = SWITCH_FALSE;
743
744 if (zstr(cmd)) {
745 switch_goto_status(SWITCH_STATUS_SUCCESS, usage);
746 }
747
748 if (session) {
749 pool = switch_core_session_get_pool(session);
750 } else {
751 switch_core_new_memory_pool(&pool);
752 }
753 switch_event_create(&event, SWITCH_EVENT_MESSAGE);
754
755 mydata = strdup(cmd);
756 if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
757 if (argc < 1) {
758 switch_goto_status(SWITCH_STATUS_SUCCESS, usage);
759 }
760
761 if (!strcmp("status", argv[0])) {
762 stream->write_function(stream, "+OK\n url: %s\n cache: %s\n cache-expire: %d\n",
763 globals.url ? globals.url : "(null)", (globals.cache) ? "true" : "false", globals.cache_expire);
764 stream->write_function(stream, " curl-timeout: %" SWITCH_TIME_T_FMT "\n curl-warn-duration: %" SWITCH_TIME_T_FMT "\n",
765 globals.curl_timeout, globals.curl_warnduration);
766
767 stream->write_function(stream, " odbc-dsn: %s\n sql: %s\n citystate-sql: %s\n",
768 globals.odbc_dsn ? globals.odbc_dsn : "(null)",
769 globals.sql ? globals.sql : "(null)", globals.citystate_sql ? globals.citystate_sql : "(null)");
770
771
772 switch_goto_status(SWITCH_STATUS_SUCCESS, done);
773 }
774 for (i = 1; i < argc; i++) {
775 if (!strcasecmp(argv[i], "skipurl")) {
776 skipurl = SWITCH_TRUE;
777 } else if (!strcasecmp(argv[i], "skipcitystate")) {
778 skipcitystate = SWITCH_TRUE;
779 } else if (!strcasecmp(argv[i], "verbose")) {
780 verbose = SWITCH_TRUE;
781 }
782 }
783
784 cid = do_lookup(pool, event, argv[0], skipurl, skipcitystate);
785 if (cid) {
786 if (switch_string_var_check_const(cid->name)) {
787 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Invalid CID data {%s} contains a variable\n", cid->name);
788 stream->write_function(stream, "-ERR Invalid CID data {%s} contains a variable\n", cid->name);
789 switch_goto_status(SWITCH_STATUS_SUCCESS, done);
790 }
791 stream->write_function(stream, cid->name);
792 if (verbose) {
793 stream->write_function(stream, " (%s) [%s]", cid->area, cid->src);
794 }
795 } else {
796 stream->write_function(stream, "UNKNOWN");
797 }
798 }
799 switch_goto_status(SWITCH_STATUS_SUCCESS, done);
800
801 usage:
802 stream->write_function(stream, "-ERR\n%s\n", SYNTAX);
803 switch_goto_status(status, done);
804
805 done:
806 switch_safe_free(mydata);
807 if (event) {
808 switch_event_destroy(&event);
809 }
810 if (!session && pool) {
811 switch_core_destroy_memory_pool(&pool);
812 }
813 return status;
814 }
815
816 /* Macro expands to: switch_status_t mod_cidlookup_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
SWITCH_MODULE_LOAD_FUNCTION(mod_cidlookup_load)817 SWITCH_MODULE_LOAD_FUNCTION(mod_cidlookup_load)
818 {
819 switch_api_interface_t *api_interface;
820 switch_application_interface_t *app_interface;
821 /* connect my internal structure to the blank pointer passed to me */
822 *module_interface = switch_loadable_module_create_module_interface(pool, modname);
823
824 memset(&globals, 0, sizeof(globals));
825
826 globals.pool = pool;
827
828 do_config(SWITCH_FALSE);
829
830 if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &reload_xml_event) != SWITCH_STATUS_SUCCESS)) {
831 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind event!\n");
832 return SWITCH_STATUS_TERM;
833 }
834
835 SWITCH_ADD_API(api_interface, "cidlookup", "cidlookup API", cidlookup_function, SYNTAX);
836 SWITCH_ADD_APP(app_interface, "cidlookup", "Perform a CID lookup", "Perform a CID lookup",
837 cidlookup_app_function, "[number [skipurl]]", SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC);
838
839 /* indicate that the module should continue to be loaded */
840 return SWITCH_STATUS_SUCCESS;
841 }
842
843 /*
844 Called when the system shuts down
845 Macro expands to: switch_status_t mod_cidlookup_shutdown() */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cidlookup_shutdown)846 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cidlookup_shutdown)
847 {
848 switch_event_unbind(&reload_xml_event);
849 return SWITCH_STATUS_SUCCESS;
850 }
851
852
853 /*
854 If it exists, this is called in it's own thread when the module-load completes
855 If it returns anything but SWITCH_STATUS_TERM it will be called again automatically
856 Macro expands to: switch_status_t mod_cidlookup_runtime()
857 SWITCH_MODULE_RUNTIME_FUNCTION(mod_cidlookup_runtime)
858 {
859 while(looping)
860 {
861 switch_cond_next();
862 }
863 return SWITCH_STATUS_TERM;
864 }
865 */
866
867 /* For Emacs:
868 * Local Variables:
869 * mode:c
870 * indent-tabs-mode:t
871 * tab-width:4
872 * c-basic-offset:4
873 * End:
874 * For VIM:
875 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
876 */
877