app_mysql.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004, Constantine Filin and Christos Ricudis
  5. *
  6. * Christos Ricudis <ricudis@itc.auth.gr>
  7. * Constantine Filin <cf@intermedia.net>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. * \brief MYSQL dialplan application
  22. * \ingroup applications
  23. */
  24. /*! \li \ref app_mysql.c uses the configuration file \ref app_mysql.conf
  25. * \addtogroup configuration_file Configuration Files
  26. */
  27. /*!
  28. * \page app_mysql.conf app_mysql.conf
  29. * \verbinclude app_mysql.conf.sample
  30. */
  31. /*** MODULEINFO
  32. <depend>mysqlclient</depend>
  33. <defaultenabled>no</defaultenabled>
  34. <support_level>deprecated</support_level>
  35. <replacement>func_odbc</replacement>
  36. ***/
  37. #include "asterisk.h"
  38. #include <mysql/mysql.h>
  39. #include "asterisk/file.h"
  40. #include "asterisk/logger.h"
  41. #include "asterisk/channel.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/module.h"
  44. #include "asterisk/linkedlists.h"
  45. #include "asterisk/chanvars.h"
  46. #include "asterisk/lock.h"
  47. #include "asterisk/options.h"
  48. #include "asterisk/app.h"
  49. #include "asterisk/config.h"
  50. #define EXTRA_LOG 0
  51. enum { NULLSTRING, NULLVALUE, EMPTYSTRING } nullvalue = NULLSTRING;
  52. static const char app[] = "MYSQL";
  53. static const char synopsis[] = "Do several mySQLy things";
  54. static const char descrip[] =
  55. "MYSQL(): Do several mySQLy things\n"
  56. "Syntax:\n"
  57. " MYSQL(Set timeout <num>)\n"
  58. " Set the connection timeout, in seconds.\n"
  59. " MYSQL(Connect connid dhhost[:dbport] dbuser dbpass dbname [dbcharset])\n"
  60. " Connects to a database. Arguments contain standard MySQL parameters\n"
  61. " passed to function mysql_real_connect. Optional parameter dbcharset\n"
  62. " defaults to 'latin1'. Connection identifer returned in ${connid}\n"
  63. " MYSQL(Query resultid ${connid} query-string)\n"
  64. " Executes standard MySQL query contained in query-string using established\n"
  65. " connection identified by ${connid}. Result of query is stored in ${resultid}.\n"
  66. " MYSQL(Nextresult resultid ${connid}\n"
  67. " If last query returned more than one result set, it stores the next\n"
  68. " result set in ${resultid}. It's useful with stored procedures\n"
  69. " MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
  70. " Fetches a single row from a result set contained in ${result_identifier}.\n"
  71. " Assigns returned fields to ${var1} ... ${varn}. ${fetchid} is set TRUE\n"
  72. " if additional rows exist in result set.\n"
  73. " MYSQL(Clear ${resultid})\n"
  74. " Frees memory and datastructures associated with result set.\n"
  75. " MYSQL(Disconnect ${connid})\n"
  76. " Disconnects from named connection to MySQL.\n"
  77. " On exit, always returns 0. Sets MYSQL_STATUS to 0 on success and -1 on error.\n";
  78. /*
  79. EXAMPLES OF USE :
  80. exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit utf8)
  81. exten => s,3,MYSQL(Query resultid ${connid} SELECT username,credit FROM credit WHERE callerid=${CALLERIDNUM})
  82. exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
  83. exten => s,5,GotoIf(${fetchid}?6:8)
  84. exten => s,6,Festival("User ${datavar1} currently has credit balance of ${datavar2} dollars.")
  85. exten => s,7,Goto(s,4)
  86. exten => s,8,MYSQL(Clear ${resultid})
  87. exten => s,9,MYSQL(Disconnect ${connid})
  88. */
  89. AST_MUTEX_DEFINE_STATIC(_mysql_mutex);
  90. #define MYSQL_CONFIG "app_mysql.conf"
  91. #define MYSQL_CONFIG_OLD "mysql.conf"
  92. #define AST_MYSQL_ID_DUMMY 0
  93. #define AST_MYSQL_ID_CONNID 1
  94. #define AST_MYSQL_ID_RESID 2
  95. #define AST_MYSQL_ID_FETCHID 3
  96. static int autoclear = 0;
  97. static void mysql_ds_destroy(void *data);
  98. static void mysql_ds_fixup(void *data, struct ast_channel *oldchan, struct ast_channel *newchan);
  99. static const struct ast_datastore_info mysql_ds_info = {
  100. .type = "APP_ADDON_SQL_MYSQL",
  101. .destroy = mysql_ds_destroy,
  102. .chan_fixup = mysql_ds_fixup,
  103. };
  104. struct ast_MYSQL_id {
  105. struct ast_channel *owner;
  106. int identifier_type; /* 0=dummy, 1=connid, 2=resultid */
  107. int identifier;
  108. void *data;
  109. AST_LIST_ENTRY(ast_MYSQL_id) entries;
  110. } *ast_MYSQL_id;
  111. AST_LIST_HEAD(MYSQLidshead,ast_MYSQL_id) _mysql_ids_head;
  112. static void mysql_ds_destroy(void *data)
  113. {
  114. /* Destroy any IDs owned by the channel */
  115. struct ast_MYSQL_id *i;
  116. if (AST_LIST_LOCK(&_mysql_ids_head)) {
  117. ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
  118. } else {
  119. AST_LIST_TRAVERSE_SAFE_BEGIN(&_mysql_ids_head, i, entries) {
  120. if (i->owner == data) {
  121. AST_LIST_REMOVE_CURRENT(entries);
  122. if (i->identifier_type == AST_MYSQL_ID_CONNID) {
  123. /* Drop connection */
  124. mysql_close(i->data);
  125. } else if (i->identifier_type == AST_MYSQL_ID_RESID) {
  126. /* Drop result */
  127. mysql_free_result(i->data);
  128. }
  129. ast_free(i);
  130. }
  131. }
  132. AST_LIST_TRAVERSE_SAFE_END
  133. AST_LIST_UNLOCK(&_mysql_ids_head);
  134. }
  135. }
  136. static void mysql_ds_fixup(void *data, struct ast_channel *oldchan, struct ast_channel *newchan)
  137. {
  138. /* Destroy any IDs owned by the channel */
  139. struct ast_MYSQL_id *i;
  140. if (AST_LIST_LOCK(&_mysql_ids_head)) {
  141. ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
  142. } else {
  143. AST_LIST_TRAVERSE_SAFE_BEGIN(&_mysql_ids_head, i, entries) {
  144. if (i->owner == data) {
  145. AST_LIST_REMOVE_CURRENT(entries);
  146. if (i->identifier_type == AST_MYSQL_ID_CONNID) {
  147. /* Drop connection */
  148. mysql_close(i->data);
  149. } else if (i->identifier_type == AST_MYSQL_ID_RESID) {
  150. /* Drop result */
  151. mysql_free_result(i->data);
  152. }
  153. ast_free(i);
  154. }
  155. }
  156. AST_LIST_TRAVERSE_SAFE_END
  157. AST_LIST_UNLOCK(&_mysql_ids_head);
  158. }
  159. }
  160. /* helpful procs */
  161. static void *find_identifier(int identifier, int identifier_type)
  162. {
  163. struct MYSQLidshead *headp = &_mysql_ids_head;
  164. struct ast_MYSQL_id *i;
  165. void *res=NULL;
  166. int found=0;
  167. if (AST_LIST_LOCK(headp)) {
  168. ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
  169. } else {
  170. AST_LIST_TRAVERSE(headp, i, entries) {
  171. if ((i->identifier == identifier) && (i->identifier_type == identifier_type)) {
  172. found = 1;
  173. res = i->data;
  174. break;
  175. }
  176. }
  177. if (!found) {
  178. ast_log(LOG_WARNING, "Identifier %d, identifier_type %d not found in identifier list\n", identifier, identifier_type);
  179. }
  180. AST_LIST_UNLOCK(headp);
  181. }
  182. return res;
  183. }
  184. static int add_identifier(struct ast_channel *chan, int identifier_type, void *data)
  185. {
  186. struct ast_MYSQL_id *i = NULL, *j = NULL;
  187. struct MYSQLidshead *headp = &_mysql_ids_head;
  188. int maxidentifier = 0;
  189. if (AST_LIST_LOCK(headp)) {
  190. ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
  191. return -1;
  192. } else {
  193. i = malloc(sizeof(*i));
  194. AST_LIST_TRAVERSE(headp, j, entries) {
  195. if (j->identifier > maxidentifier) {
  196. maxidentifier = j->identifier;
  197. }
  198. }
  199. i->identifier = maxidentifier + 1;
  200. i->identifier_type = identifier_type;
  201. i->data = data;
  202. i->owner = chan;
  203. AST_LIST_INSERT_HEAD(headp, i, entries);
  204. AST_LIST_UNLOCK(headp);
  205. }
  206. return i->identifier;
  207. }
  208. static int del_identifier(int identifier, int identifier_type)
  209. {
  210. struct ast_MYSQL_id *i;
  211. struct MYSQLidshead *headp = &_mysql_ids_head;
  212. int found = 0;
  213. if (AST_LIST_LOCK(headp)) {
  214. ast_log(LOG_WARNING, "Unable to lock identifiers list\n");
  215. } else {
  216. AST_LIST_TRAVERSE(headp, i, entries) {
  217. if ((i->identifier == identifier) &&
  218. (i->identifier_type == identifier_type)) {
  219. AST_LIST_REMOVE(headp, i, entries);
  220. free(i);
  221. found = 1;
  222. break;
  223. }
  224. }
  225. AST_LIST_UNLOCK(headp);
  226. }
  227. if (found == 0) {
  228. ast_log(LOG_WARNING, "Could not find identifier %d, identifier_type %d in list to delete\n", identifier, identifier_type);
  229. return -1;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. static int set_asterisk_int(struct ast_channel *chan, char *varname, int id)
  235. {
  236. if (id >= 0) {
  237. char s[12] = "";
  238. snprintf(s, sizeof(s), "%d", id);
  239. ast_debug(5, "MYSQL: setting var '%s' to value '%s'\n", varname, s);
  240. pbx_builtin_setvar_helper(chan, varname, s);
  241. }
  242. return id;
  243. }
  244. static int add_identifier_and_set_asterisk_int(struct ast_channel *chan, char *varname, int identifier_type, void *data)
  245. {
  246. return set_asterisk_int(chan, varname, add_identifier(chan, identifier_type, data));
  247. }
  248. static int safe_scan_int(char **data, char *delim, int def)
  249. {
  250. char *end;
  251. int res = def;
  252. char *s = strsep(data, delim);
  253. if (s) {
  254. res = strtol(s, &end, 10);
  255. if (*end)
  256. res = def; /* not an integer */
  257. }
  258. return res;
  259. }
  260. static int aMYSQL_set(struct ast_channel *chan, const char *data)
  261. {
  262. char *var, *tmp, *parse;
  263. AST_DECLARE_APP_ARGS(args,
  264. AST_APP_ARG(set);
  265. AST_APP_ARG(variable);
  266. AST_APP_ARG(value);
  267. );
  268. parse = ast_strdupa(data);
  269. AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
  270. if (args.argc == 3) {
  271. var = ast_alloca(6 + strlen(args.variable) + 1);
  272. sprintf(var, "MYSQL_%s", args.variable);
  273. /* Make the parameter case-insensitive */
  274. for (tmp = var + 6; *tmp; tmp++)
  275. *tmp = toupper(*tmp);
  276. pbx_builtin_setvar_helper(chan, var, args.value);
  277. }
  278. return 0;
  279. }
  280. /* MYSQL operations */
  281. static int aMYSQL_connect(struct ast_channel *chan, const char *data)
  282. {
  283. AST_DECLARE_APP_ARGS(args,
  284. AST_APP_ARG(connect);
  285. AST_APP_ARG(connid);
  286. AST_APP_ARG(dbhost);
  287. AST_APP_ARG(dbuser);
  288. AST_APP_ARG(dbpass);
  289. AST_APP_ARG(dbname);
  290. AST_APP_ARG(dbcharset);
  291. );
  292. MYSQL *mysql;
  293. int timeout;
  294. const char *ctimeout;
  295. unsigned int port = 0;
  296. char *port_str;
  297. char *parse = ast_strdupa(data);
  298. AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
  299. if (args.argc < 6) {
  300. ast_log(LOG_WARNING, "MYSQL_connect is missing some arguments\n");
  301. return -1;
  302. }
  303. if (!(mysql = mysql_init(NULL))) {
  304. ast_log(LOG_WARNING, "mysql_init returned NULL\n");
  305. return -1;
  306. }
  307. ctimeout = pbx_builtin_getvar_helper(chan, "MYSQL_TIMEOUT");
  308. if (ctimeout && sscanf(ctimeout, "%30d", &timeout) == 1) {
  309. mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (void *)&timeout);
  310. }
  311. if(args.dbcharset && strlen(args.dbcharset) > 2){
  312. char set_names[255];
  313. char statement[512];
  314. snprintf(set_names, sizeof(set_names), "SET NAMES %s", args.dbcharset);
  315. mysql_real_escape_string(mysql, statement, set_names, sizeof(set_names));
  316. mysql_options(mysql, MYSQL_INIT_COMMAND, set_names);
  317. mysql_options(mysql, MYSQL_SET_CHARSET_NAME, args.dbcharset);
  318. }
  319. if ((port_str = strchr(args.dbhost, ':'))) {
  320. *port_str++ = '\0';
  321. if (sscanf(port_str, "%u", &port) != 1) {
  322. ast_log(LOG_WARNING, "Invalid port: '%s'\n", port_str);
  323. port = 0;
  324. }
  325. }
  326. if (!mysql_real_connect(mysql, args.dbhost, args.dbuser, args.dbpass, args.dbname, port, NULL,
  327. #ifdef CLIENT_MULTI_STATEMENTS
  328. CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS
  329. #elif defined(CLIENT_MULTI_QUERIES)
  330. CLIENT_MULTI_QUERIES
  331. #else
  332. 0
  333. #endif
  334. )) {
  335. ast_log(LOG_WARNING, "mysql_real_connect(mysql,%s,%s,dbpass,%s,...) failed(%d): %s\n",
  336. args.dbhost, args.dbuser, args.dbname, mysql_errno(mysql), mysql_error(mysql));
  337. return -1;
  338. }
  339. add_identifier_and_set_asterisk_int(chan, args.connid, AST_MYSQL_ID_CONNID, mysql);
  340. return 0;
  341. }
  342. static int aMYSQL_query(struct ast_channel *chan, const char *data)
  343. {
  344. AST_DECLARE_APP_ARGS(args,
  345. AST_APP_ARG(query);
  346. AST_APP_ARG(resultid);
  347. AST_APP_ARG(connid);
  348. AST_APP_ARG(sql);
  349. );
  350. MYSQL *mysql;
  351. MYSQL_RES *mysqlres;
  352. int connid;
  353. int mysql_query_res;
  354. char *parse = ast_strdupa(data);
  355. AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
  356. if (args.argc != 4 || (connid = atoi(args.connid)) == 0) {
  357. ast_log(LOG_WARNING, "missing some arguments\n");
  358. return -1;
  359. }
  360. if (!(mysql = find_identifier(connid, AST_MYSQL_ID_CONNID))) {
  361. ast_log(LOG_WARNING, "Invalid connection identifier %s passed in aMYSQL_query\n", args.connid);
  362. return -1;
  363. }
  364. if ((mysql_query_res = mysql_query(mysql, args.sql)) != 0) {
  365. ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
  366. return -1;
  367. }
  368. if ((mysqlres = mysql_store_result(mysql))) {
  369. add_identifier_and_set_asterisk_int(chan, args.resultid, AST_MYSQL_ID_RESID, mysqlres);
  370. return 0;
  371. } else if (!mysql_field_count(mysql)) {
  372. return 0;
  373. } else
  374. ast_log(LOG_WARNING, "mysql_store_result() failed on query %s\n", args.sql);
  375. return -1;
  376. }
  377. static int aMYSQL_nextresult(struct ast_channel *chan, const char *data)
  378. {
  379. MYSQL *mysql;
  380. MYSQL_RES *mysqlres;
  381. AST_DECLARE_APP_ARGS(args,
  382. AST_APP_ARG(nextresult);
  383. AST_APP_ARG(resultid);
  384. AST_APP_ARG(connid);
  385. );
  386. int connid = -1;
  387. char *parse = ast_strdupa(data);
  388. AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
  389. sscanf(args.connid, "%30d", &connid);
  390. if (args.argc != 3 || connid <= 0) {
  391. ast_log(LOG_WARNING, "missing some arguments\n");
  392. return -1;
  393. }
  394. if (!(mysql = find_identifier(connid, AST_MYSQL_ID_CONNID))) {
  395. ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aMYSQL_query\n", connid);
  396. return -1;
  397. }
  398. if (mysql_more_results(mysql)) {
  399. mysql_next_result(mysql);
  400. if ((mysqlres = mysql_store_result(mysql))) {
  401. add_identifier_and_set_asterisk_int(chan, args.resultid, AST_MYSQL_ID_RESID, mysqlres);
  402. return 0;
  403. } else if (!mysql_field_count(mysql)) {
  404. return 0;
  405. } else
  406. ast_log(LOG_WARNING, "mysql_store_result() failed on storing next_result\n");
  407. } else
  408. ast_log(LOG_WARNING, "mysql_more_results() result set has no more results\n");
  409. return 0;
  410. }
  411. static int aMYSQL_fetch(struct ast_channel *chan, const char *data)
  412. {
  413. MYSQL_RES *mysqlres;
  414. MYSQL_ROW mysqlrow;
  415. AST_DECLARE_APP_ARGS(args,
  416. AST_APP_ARG(fetch);
  417. AST_APP_ARG(resultvar);
  418. AST_APP_ARG(fetchid);
  419. AST_APP_ARG(vars);
  420. );
  421. char *s5, *parse;
  422. int resultid = -1, numFields, j;
  423. parse = ast_strdupa(data);
  424. AST_NONSTANDARD_APP_ARGS(args, parse, ' ');
  425. sscanf(args.fetchid, "%30d", &resultid);
  426. if (args.resultvar && (resultid >= 0) ) {
  427. if ((mysqlres = find_identifier(resultid, AST_MYSQL_ID_RESID)) != NULL) {
  428. /* Grab the next row */
  429. if ((mysqlrow = mysql_fetch_row(mysqlres)) != NULL) {
  430. numFields = mysql_num_fields(mysqlres);
  431. for (j = 0; j < numFields; j++) {
  432. s5 = strsep(&args.vars, " ");
  433. if (s5 == NULL) {
  434. ast_log(LOG_WARNING, "ast_MYSQL_fetch: More fields (%d) than variables (%d)\n", numFields, j);
  435. break;
  436. }
  437. pbx_builtin_setvar_helper(chan, s5, mysqlrow[j] ? mysqlrow[j] :
  438. nullvalue == NULLSTRING ? "NULL" :
  439. nullvalue == EMPTYSTRING ? "" :
  440. NULL);
  441. }
  442. ast_debug(5, "ast_MYSQL_fetch: numFields=%d\n", numFields);
  443. set_asterisk_int(chan, args.resultvar, 1); /* try more rows */
  444. } else {
  445. ast_debug(5, "ast_MYSQL_fetch : EOF\n");
  446. set_asterisk_int(chan, args.resultvar, 0); /* no more rows */
  447. }
  448. return 0;
  449. } else {
  450. set_asterisk_int(chan, args.resultvar, 0);
  451. ast_log(LOG_WARNING, "aMYSQL_fetch: Invalid result identifier %d passed\n", resultid);
  452. }
  453. } else {
  454. ast_log(LOG_WARNING, "aMYSQL_fetch: missing some arguments\n");
  455. }
  456. return -1;
  457. }
  458. static int aMYSQL_clear(struct ast_channel *chan, const char *data)
  459. {
  460. MYSQL_RES *mysqlres;
  461. int id;
  462. char *parse = ast_strdupa(data);
  463. strsep(&parse, " "); /* eat the first token, we already know it :P */
  464. id = safe_scan_int(&parse, " \n", -1);
  465. if ((mysqlres = find_identifier(id, AST_MYSQL_ID_RESID)) == NULL) {
  466. ast_log(LOG_WARNING, "Invalid result identifier %d passed in aMYSQL_clear\n", id);
  467. } else {
  468. mysql_free_result(mysqlres);
  469. del_identifier(id, AST_MYSQL_ID_RESID);
  470. }
  471. return 0;
  472. }
  473. static int aMYSQL_disconnect(struct ast_channel *chan, const char *data)
  474. {
  475. MYSQL *mysql;
  476. int id;
  477. char *parse = ast_strdupa(data);
  478. strsep(&parse, " "); /* eat the first token, we already know it :P */
  479. id = safe_scan_int(&parse, " \n", -1);
  480. if ((mysql = find_identifier(id, AST_MYSQL_ID_CONNID)) == NULL) {
  481. ast_log(LOG_WARNING, "Invalid connection identifier %d passed in aMYSQL_disconnect\n", id);
  482. } else {
  483. mysql_close(mysql);
  484. del_identifier(id, AST_MYSQL_ID_CONNID);
  485. }
  486. return 0;
  487. }
  488. static int MYSQL_exec(struct ast_channel *chan, const char *data)
  489. {
  490. int result;
  491. char sresult[10];
  492. ast_debug(5, "MYSQL: data=%s\n", data);
  493. if (!data) {
  494. ast_log(LOG_WARNING, "MYSQL requires an argument (see manual)\n");
  495. return -1;
  496. }
  497. result = 0;
  498. if (autoclear) {
  499. struct ast_datastore *mysql_store = NULL;
  500. ast_channel_lock(chan);
  501. mysql_store = ast_channel_datastore_find(chan, &mysql_ds_info, NULL);
  502. if (!mysql_store) {
  503. if (!(mysql_store = ast_datastore_alloc(&mysql_ds_info, NULL))) {
  504. ast_log(LOG_WARNING, "Unable to allocate new datastore.\n");
  505. } else {
  506. mysql_store->data = chan;
  507. ast_channel_datastore_add(chan, mysql_store);
  508. }
  509. }
  510. ast_channel_unlock(chan);
  511. }
  512. ast_mutex_lock(&_mysql_mutex);
  513. if (strncasecmp("connect", data, strlen("connect")) == 0) {
  514. result = aMYSQL_connect(chan, data);
  515. } else if (strncasecmp("query", data, strlen("query")) == 0) {
  516. result = aMYSQL_query(chan, data);
  517. } else if (strncasecmp("nextresult", data, strlen("nextresult")) == 0) {
  518. result = aMYSQL_nextresult(chan, data);
  519. } else if (strncasecmp("fetch", data, strlen("fetch")) == 0) {
  520. result = aMYSQL_fetch(chan, data);
  521. } else if (strncasecmp("clear", data, strlen("clear")) == 0) {
  522. result = aMYSQL_clear(chan, data);
  523. } else if (strncasecmp("disconnect", data, strlen("disconnect")) == 0) {
  524. result = aMYSQL_disconnect(chan, data);
  525. } else if (strncasecmp("set", data, 3) == 0) {
  526. result = aMYSQL_set(chan, data);
  527. } else {
  528. ast_log(LOG_WARNING, "Unknown argument to MYSQL application : %s\n", data);
  529. result = -1;
  530. }
  531. ast_mutex_unlock(&_mysql_mutex);
  532. snprintf(sresult, sizeof(sresult), "%d", result);
  533. pbx_builtin_setvar_helper(chan, "MYSQL_STATUS", sresult);
  534. return 0;
  535. }
  536. static int unload_module(void)
  537. {
  538. return ast_unregister_application(app);
  539. }
  540. /*!
  541. * \brief Load the module
  542. *
  543. * Module loading including tests for configuration or dependencies.
  544. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  545. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  546. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  547. * configuration file or other non-critical problem return
  548. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  549. */
  550. static int load_module(void)
  551. {
  552. struct MYSQLidshead *headp = &_mysql_ids_head;
  553. struct ast_flags config_flags = { 0 };
  554. struct ast_config *cfg = ast_config_load(MYSQL_CONFIG, config_flags);
  555. const char *temp;
  556. if (!cfg) {
  557. /* Backwards compatibility ftw */
  558. cfg = ast_config_load(MYSQL_CONFIG_OLD, config_flags);
  559. }
  560. if (cfg) {
  561. if ((temp = ast_variable_retrieve(cfg, "general", "nullvalue"))) {
  562. if (!strcasecmp(temp, "nullstring")) {
  563. nullvalue = NULLSTRING;
  564. } else if (!strcasecmp(temp, "emptystring")) {
  565. nullvalue = EMPTYSTRING;
  566. } else if (!strcasecmp(temp, "null")) {
  567. nullvalue = NULLVALUE;
  568. } else {
  569. ast_log(LOG_WARNING, "Illegal value for 'nullvalue': '%s' (must be 'nullstring', 'null', or 'emptystring')\n", temp);
  570. }
  571. }
  572. if ((temp = ast_variable_retrieve(cfg, "general", "autoclear")) && ast_true(temp)) {
  573. autoclear = 1;
  574. }
  575. ast_config_destroy(cfg);
  576. }
  577. AST_LIST_HEAD_INIT(headp);
  578. return ast_register_application(app, MYSQL_exec, synopsis, descrip);
  579. }
  580. AST_MODULE_INFO_STANDARD_DEPRECATED(ASTERISK_GPL_KEY, "Simple Mysql Interface");