func_realtime.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2006, BJ Weschke. All rights reserved.
  5. *
  6. * BJ Weschke <bweschke@btwtech.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief REALTIME dialplan function
  20. *
  21. * \author BJ Weschke <bweschke@btwtech.com>
  22. *
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/file.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/app.h"
  38. /*** DOCUMENTATION
  39. <function name="REALTIME" language="en_US">
  40. <synopsis>
  41. RealTime Read/Write Functions.
  42. </synopsis>
  43. <syntax>
  44. <parameter name="family" required="true" />
  45. <parameter name="fieldmatch" required="true" />
  46. <parameter name="matchvalue" />
  47. <parameter name="delim1|field">
  48. <para>Use <replaceable>delim1</replaceable> with <replaceable>delim2</replaceable> on
  49. read and <replaceable>field</replaceable> without <replaceable>delim2</replaceable> on
  50. write</para>
  51. <para>If we are reading and <replaceable>delim1</replaceable> is not specified, defaults
  52. to <literal>,</literal></para>
  53. </parameter>
  54. <parameter name="delim2">
  55. <para>Parameter only used when reading, if not specified defaults to <literal>=</literal></para>
  56. </parameter>
  57. </syntax>
  58. <description>
  59. <para>This function will read or write values from/to a RealTime repository.
  60. REALTIME(....) will read names/values from the repository, and
  61. REALTIME(....)= will write a new value/field to the repository. On a
  62. read, this function returns a delimited text string. The name/value
  63. pairs are delimited by <replaceable>delim1</replaceable>, and the name and value are delimited
  64. between each other with delim2.
  65. If there is no match, NULL will be returned by the function.
  66. On a write, this function will always return NULL.</para>
  67. </description>
  68. <see-also>
  69. <ref type="function">REALTIME_STORE</ref>
  70. <ref type="function">REALTIME_DESTROY</ref>
  71. <ref type="function">REALTIME_FIELD</ref>
  72. <ref type="function">REALTIME_HASH</ref>
  73. </see-also>
  74. </function>
  75. <function name="REALTIME_STORE" language="en_US">
  76. <synopsis>
  77. RealTime Store Function.
  78. </synopsis>
  79. <syntax>
  80. <parameter name="family" required="true" />
  81. <parameter name="field1" required="true" />
  82. <parameter name="fieldN" required="true" multiple="true" />
  83. <parameter name="field30" required="true" />
  84. </syntax>
  85. <description>
  86. <para>This function will insert a new set of values into the RealTime repository.
  87. If RT engine provides an unique ID of the stored record, REALTIME_STORE(...)=..
  88. creates channel variable named RTSTOREID, which contains value of unique ID.
  89. Currently, a maximum of 30 field/value pairs is supported.</para>
  90. </description>
  91. <see-also>
  92. <ref type="function">REALTIME</ref>
  93. <ref type="function">REALTIME_DESTROY</ref>
  94. <ref type="function">REALTIME_FIELD</ref>
  95. <ref type="function">REALTIME_HASH</ref>
  96. </see-also>
  97. </function>
  98. <function name="REALTIME_DESTROY" language="en_US">
  99. <synopsis>
  100. RealTime Destroy Function.
  101. </synopsis>
  102. <syntax>
  103. <parameter name="family" required="true" />
  104. <parameter name="fieldmatch" required="true" />
  105. <parameter name="matchvalue" />
  106. <parameter name="delim1" />
  107. <parameter name="delim2" />
  108. </syntax>
  109. <description>
  110. <para>This function acts in the same way as REALTIME(....) does, except that
  111. it destroys the matched record in the RT engine.</para>
  112. <note>
  113. <para>If <literal>live_dangerously</literal> in <literal>asterisk.conf</literal>
  114. is set to <literal>no</literal>, this function can only be read from the
  115. dialplan, and not directly from external protocols. It can, however, be
  116. executed as a write operation (<literal>REALTIME_DESTROY(family, fieldmatch)=ignored</literal>)</para>
  117. </note>
  118. </description>
  119. <see-also>
  120. <ref type="function">REALTIME</ref>
  121. <ref type="function">REALTIME_STORE</ref>
  122. <ref type="function">REALTIME_FIELD</ref>
  123. <ref type="function">REALTIME_HASH</ref>
  124. </see-also>
  125. </function>
  126. <function name="REALTIME_FIELD" language="en_US">
  127. <synopsis>
  128. RealTime query function.
  129. </synopsis>
  130. <syntax>
  131. <parameter name="family" required="true" />
  132. <parameter name="fieldmatch" required="true" />
  133. <parameter name="matchvalue" required="true" />
  134. <parameter name="fieldname" required="true" />
  135. </syntax>
  136. <description>
  137. <para>This function retrieves a single item, <replaceable>fieldname</replaceable>
  138. from the RT engine, where <replaceable>fieldmatch</replaceable> contains the value
  139. <replaceable>matchvalue</replaceable>. When written to, the REALTIME_FIELD() function
  140. performs identically to the REALTIME() function.</para>
  141. </description>
  142. <see-also>
  143. <ref type="function">REALTIME</ref>
  144. <ref type="function">REALTIME_STORE</ref>
  145. <ref type="function">REALTIME_DESTROY</ref>
  146. <ref type="function">REALTIME_HASH</ref>
  147. </see-also>
  148. </function>
  149. <function name="REALTIME_HASH" language="en_US">
  150. <synopsis>
  151. RealTime query function.
  152. </synopsis>
  153. <syntax>
  154. <parameter name="family" required="true" />
  155. <parameter name="fieldmatch" required="true" />
  156. <parameter name="matchvalue" required="true" />
  157. </syntax>
  158. <description>
  159. <para>This function retrieves a single record from the RT engine, where
  160. <replaceable>fieldmatch</replaceable> contains the value
  161. <replaceable>matchvalue</replaceable> and formats the output suitably, such that
  162. it can be assigned to the HASH() function. The HASH() function then provides
  163. a suitable method for retrieving each field value of the record.</para>
  164. </description>
  165. <see-also>
  166. <ref type="function">REALTIME</ref>
  167. <ref type="function">REALTIME_STORE</ref>
  168. <ref type="function">REALTIME_DESTROY</ref>
  169. <ref type="function">REALTIME_FIELD</ref>
  170. </see-also>
  171. </function>
  172. ***/
  173. AST_THREADSTORAGE(buf1);
  174. AST_THREADSTORAGE(buf2);
  175. AST_THREADSTORAGE(buf3);
  176. static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  177. {
  178. struct ast_variable *var, *head;
  179. struct ast_str *out;
  180. size_t resultslen;
  181. int n;
  182. AST_DECLARE_APP_ARGS(args,
  183. AST_APP_ARG(family);
  184. AST_APP_ARG(fieldmatch);
  185. AST_APP_ARG(value);
  186. AST_APP_ARG(delim1);
  187. AST_APP_ARG(delim2);
  188. );
  189. if (ast_strlen_zero(data)) {
  190. ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
  191. return -1;
  192. }
  193. AST_STANDARD_APP_ARGS(args, data);
  194. if (!args.delim1)
  195. args.delim1 = ",";
  196. if (!args.delim2)
  197. args.delim2 = "=";
  198. if (chan)
  199. ast_autoservice_start(chan);
  200. head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
  201. if (!head) {
  202. if (chan)
  203. ast_autoservice_stop(chan);
  204. return -1;
  205. }
  206. resultslen = 0;
  207. n = 0;
  208. for (var = head; var; n++, var = var->next)
  209. resultslen += strlen(var->name) + strlen(var->value);
  210. /* add space for delimiters and final '\0' */
  211. resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
  212. if (resultslen > len) {
  213. ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
  214. if (chan) {
  215. ast_autoservice_stop(chan);
  216. }
  217. return -1;
  218. }
  219. /* len is going to be sensible, so we don't need to check for stack
  220. * overflows here. */
  221. out = ast_str_alloca(resultslen);
  222. for (var = head; var; var = var->next)
  223. ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
  224. ast_copy_string(buf, ast_str_buffer(out), len);
  225. ast_variables_destroy(head);
  226. if (chan)
  227. ast_autoservice_stop(chan);
  228. return 0;
  229. }
  230. static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  231. {
  232. int res = 0;
  233. AST_DECLARE_APP_ARGS(args,
  234. AST_APP_ARG(family);
  235. AST_APP_ARG(fieldmatch);
  236. AST_APP_ARG(value);
  237. AST_APP_ARG(field);
  238. );
  239. if (ast_strlen_zero(data)) {
  240. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
  241. return -1;
  242. }
  243. AST_STANDARD_APP_ARGS(args, data);
  244. if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
  245. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
  246. return -1;
  247. }
  248. if (chan) {
  249. ast_autoservice_start(chan);
  250. }
  251. res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
  252. if (res < 0) {
  253. ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
  254. }
  255. if (chan) {
  256. ast_autoservice_stop(chan);
  257. }
  258. return res;
  259. }
  260. static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  261. {
  262. struct ast_variable *var, *head;
  263. struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
  264. struct ast_str *fields = ast_str_thread_get(&buf2, 16);
  265. struct ast_str *values = ast_str_thread_get(&buf3, 16);
  266. int first = 0;
  267. enum { rtfield, rthash } which;
  268. AST_DECLARE_APP_ARGS(args,
  269. AST_APP_ARG(family);
  270. AST_APP_ARG(fieldmatch);
  271. AST_APP_ARG(value);
  272. AST_APP_ARG(fieldname);
  273. );
  274. if (!strcmp(cmd, "REALTIME_FIELD")) {
  275. which = rtfield;
  276. } else {
  277. which = rthash;
  278. }
  279. if (ast_strlen_zero(data)) {
  280. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
  281. return -1;
  282. }
  283. AST_STANDARD_APP_ARGS(args, data);
  284. if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
  285. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
  286. return -1;
  287. }
  288. if (chan) {
  289. ast_autoservice_start(chan);
  290. }
  291. if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
  292. if (chan) {
  293. ast_autoservice_stop(chan);
  294. }
  295. return -1;
  296. }
  297. ast_str_reset(fields);
  298. ast_str_reset(values);
  299. for (var = head; var; var = var->next) {
  300. if (which == rtfield) {
  301. ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
  302. if (!strcasecmp(var->name, args.fieldname)) {
  303. ast_debug(1, "Match! Value is %s\n", var->value);
  304. ast_copy_string(buf, var->value, len);
  305. break;
  306. }
  307. } else if (which == rthash) {
  308. ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
  309. ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
  310. ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
  311. first = 0;
  312. }
  313. }
  314. ast_variables_destroy(head);
  315. if (which == rthash) {
  316. pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
  317. ast_copy_string(buf, ast_str_buffer(values), len);
  318. }
  319. if (chan) {
  320. ast_autoservice_stop(chan);
  321. }
  322. return 0;
  323. }
  324. static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  325. {
  326. int res = 0;
  327. char storeid[32];
  328. char *valcopy;
  329. AST_DECLARE_APP_ARGS(a,
  330. AST_APP_ARG(family);
  331. AST_APP_ARG(f)[30]; /* fields */
  332. );
  333. AST_DECLARE_APP_ARGS(v,
  334. AST_APP_ARG(v)[30]; /* values */
  335. );
  336. if (ast_strlen_zero(data)) {
  337. ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
  338. return -1;
  339. }
  340. if (chan)
  341. ast_autoservice_start(chan);
  342. valcopy = ast_strdupa(value);
  343. AST_STANDARD_APP_ARGS(a, data);
  344. AST_STANDARD_APP_ARGS(v, valcopy);
  345. res = ast_store_realtime(a.family,
  346. a.f[0], v.v[0], a.f[1], v.v[1], a.f[2], v.v[2], a.f[3], v.v[3], a.f[4], v.v[4],
  347. a.f[5], v.v[5], a.f[6], v.v[6], a.f[7], v.v[7], a.f[8], v.v[8], a.f[9], v.v[9],
  348. a.f[10], v.v[10], a.f[11], v.v[11], a.f[12], v.v[12], a.f[13], v.v[13], a.f[14], v.v[14],
  349. a.f[15], v.v[15], a.f[16], v.v[16], a.f[17], v.v[17], a.f[18], v.v[18], a.f[19], v.v[19],
  350. a.f[20], v.v[20], a.f[21], v.v[21], a.f[22], v.v[22], a.f[23], v.v[23], a.f[24], v.v[24],
  351. a.f[25], v.v[25], a.f[26], v.v[26], a.f[27], v.v[27], a.f[28], v.v[28], a.f[29], v.v[29], SENTINEL
  352. );
  353. if (res < 0) {
  354. ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
  355. } else {
  356. snprintf(storeid, sizeof(storeid), "%d", res);
  357. pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
  358. }
  359. if (chan)
  360. ast_autoservice_stop(chan);
  361. return 0;
  362. }
  363. static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  364. {
  365. struct ast_variable *var, *head;
  366. struct ast_str *out;
  367. size_t resultslen;
  368. int n;
  369. AST_DECLARE_APP_ARGS(args,
  370. AST_APP_ARG(family);
  371. AST_APP_ARG(fieldmatch);
  372. AST_APP_ARG(value);
  373. AST_APP_ARG(delim1);
  374. AST_APP_ARG(delim2);
  375. );
  376. if (ast_strlen_zero(data)) {
  377. ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
  378. return -1;
  379. }
  380. AST_STANDARD_APP_ARGS(args, data);
  381. if (!args.delim1)
  382. args.delim1 = ",";
  383. if (!args.delim2)
  384. args.delim2 = "=";
  385. if (chan)
  386. ast_autoservice_start(chan);
  387. head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
  388. if (!head) {
  389. if (chan)
  390. ast_autoservice_stop(chan);
  391. return -1;
  392. }
  393. if (len > 0) {
  394. resultslen = 0;
  395. n = 0;
  396. for (var = head; var; n++, var = var->next) {
  397. resultslen += strlen(var->name) + strlen(var->value);
  398. }
  399. /* add space for delimiters and final '\0' */
  400. resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
  401. if (resultslen > len) {
  402. /* Unfortunately this does mean that we cannot destroy
  403. * the row anymore. But OTOH, we're not destroying
  404. * someones data without giving him the chance to look
  405. * at it. */
  406. ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
  407. if (chan) {
  408. ast_autoservice_stop(chan);
  409. }
  410. return -1;
  411. }
  412. /* len is going to be sensible, so we don't need to check for
  413. * stack overflows here. */
  414. out = ast_str_alloca(resultslen);
  415. for (var = head; var; var = var->next) {
  416. ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
  417. }
  418. ast_copy_string(buf, ast_str_buffer(out), len);
  419. }
  420. ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
  421. ast_variables_destroy(head);
  422. if (chan)
  423. ast_autoservice_stop(chan);
  424. return 0;
  425. }
  426. /*!
  427. * \brief Wrapper to execute REALTIME_DESTROY from a write operation. Allows
  428. * execution even if live_dangerously is disabled.
  429. */
  430. static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  431. {
  432. return function_realtime_readdestroy(chan, cmd, data, NULL, 0);
  433. }
  434. static struct ast_custom_function realtime_function = {
  435. .name = "REALTIME",
  436. .read = function_realtime_read,
  437. .write = function_realtime_write,
  438. };
  439. static struct ast_custom_function realtimefield_function = {
  440. .name = "REALTIME_FIELD",
  441. .read = realtimefield_read,
  442. .write = function_realtime_write,
  443. };
  444. static struct ast_custom_function realtimehash_function = {
  445. .name = "REALTIME_HASH",
  446. .read = realtimefield_read,
  447. };
  448. static struct ast_custom_function realtime_store_function = {
  449. .name = "REALTIME_STORE",
  450. .write = function_realtime_store,
  451. };
  452. static struct ast_custom_function realtime_destroy_function = {
  453. .name = "REALTIME_DESTROY",
  454. .read = function_realtime_readdestroy,
  455. .write = function_realtime_writedestroy,
  456. };
  457. static int unload_module(void)
  458. {
  459. int res = 0;
  460. res |= ast_custom_function_unregister(&realtime_function);
  461. res |= ast_custom_function_unregister(&realtime_store_function);
  462. res |= ast_custom_function_unregister(&realtime_destroy_function);
  463. res |= ast_custom_function_unregister(&realtimefield_function);
  464. res |= ast_custom_function_unregister(&realtimehash_function);
  465. return res;
  466. }
  467. static int load_module(void)
  468. {
  469. int res = 0;
  470. res |= ast_custom_function_register(&realtime_function);
  471. res |= ast_custom_function_register(&realtime_store_function);
  472. res |= ast_custom_function_register_escalating(&realtime_destroy_function, AST_CFE_READ);
  473. res |= ast_custom_function_register(&realtimefield_function);
  474. res |= ast_custom_function_register(&realtimehash_function);
  475. return res;
  476. }
  477. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository");