func_realtime.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. return -1;
  215. }
  216. /* len is going to be sensible, so we don't need to check for stack
  217. * overflows here. */
  218. out = ast_str_alloca(resultslen);
  219. for (var = head; var; var = var->next)
  220. ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
  221. ast_copy_string(buf, ast_str_buffer(out), len);
  222. ast_variables_destroy(head);
  223. if (chan)
  224. ast_autoservice_stop(chan);
  225. return 0;
  226. }
  227. static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  228. {
  229. int res = 0;
  230. AST_DECLARE_APP_ARGS(args,
  231. AST_APP_ARG(family);
  232. AST_APP_ARG(fieldmatch);
  233. AST_APP_ARG(value);
  234. AST_APP_ARG(field);
  235. );
  236. if (ast_strlen_zero(data)) {
  237. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
  238. return -1;
  239. }
  240. AST_STANDARD_APP_ARGS(args, data);
  241. if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) {
  242. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd);
  243. return -1;
  244. }
  245. if (chan) {
  246. ast_autoservice_start(chan);
  247. }
  248. res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL);
  249. if (res < 0) {
  250. ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
  251. }
  252. if (chan) {
  253. ast_autoservice_stop(chan);
  254. }
  255. return res;
  256. }
  257. static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  258. {
  259. struct ast_variable *var, *head;
  260. struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
  261. struct ast_str *fields = ast_str_thread_get(&buf2, 16);
  262. struct ast_str *values = ast_str_thread_get(&buf3, 16);
  263. int first = 0;
  264. enum { rtfield, rthash } which;
  265. AST_DECLARE_APP_ARGS(args,
  266. AST_APP_ARG(family);
  267. AST_APP_ARG(fieldmatch);
  268. AST_APP_ARG(value);
  269. AST_APP_ARG(fieldname);
  270. );
  271. if (!strcmp(cmd, "REALTIME_FIELD")) {
  272. which = rtfield;
  273. } else {
  274. which = rthash;
  275. }
  276. if (ast_strlen_zero(data)) {
  277. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
  278. return -1;
  279. }
  280. AST_STANDARD_APP_ARGS(args, data);
  281. if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
  282. ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
  283. return -1;
  284. }
  285. if (chan) {
  286. ast_autoservice_start(chan);
  287. }
  288. if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
  289. if (chan) {
  290. ast_autoservice_stop(chan);
  291. }
  292. return -1;
  293. }
  294. ast_str_reset(fields);
  295. ast_str_reset(values);
  296. for (var = head; var; var = var->next) {
  297. if (which == rtfield) {
  298. ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
  299. if (!strcasecmp(var->name, args.fieldname)) {
  300. ast_debug(1, "Match! Value is %s\n", var->value);
  301. ast_copy_string(buf, var->value, len);
  302. break;
  303. }
  304. } else if (which == rthash) {
  305. ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
  306. ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX));
  307. ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX));
  308. first = 0;
  309. }
  310. }
  311. ast_variables_destroy(head);
  312. if (which == rthash) {
  313. pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields));
  314. ast_copy_string(buf, ast_str_buffer(values), len);
  315. }
  316. if (chan) {
  317. ast_autoservice_stop(chan);
  318. }
  319. return 0;
  320. }
  321. static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  322. {
  323. int res = 0;
  324. char storeid[32];
  325. char *valcopy;
  326. AST_DECLARE_APP_ARGS(a,
  327. AST_APP_ARG(family);
  328. AST_APP_ARG(f)[30]; /* fields */
  329. );
  330. AST_DECLARE_APP_ARGS(v,
  331. AST_APP_ARG(v)[30]; /* values */
  332. );
  333. if (ast_strlen_zero(data)) {
  334. ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n");
  335. return -1;
  336. }
  337. if (chan)
  338. ast_autoservice_start(chan);
  339. valcopy = ast_strdupa(value);
  340. AST_STANDARD_APP_ARGS(a, data);
  341. AST_STANDARD_APP_ARGS(v, valcopy);
  342. res = ast_store_realtime(a.family,
  343. 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],
  344. 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],
  345. 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],
  346. 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],
  347. 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],
  348. 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
  349. );
  350. if (res < 0) {
  351. ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n");
  352. } else {
  353. snprintf(storeid, sizeof(storeid), "%d", res);
  354. pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid);
  355. }
  356. if (chan)
  357. ast_autoservice_stop(chan);
  358. return 0;
  359. }
  360. static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  361. {
  362. struct ast_variable *var, *head;
  363. struct ast_str *out;
  364. size_t resultslen;
  365. int n;
  366. AST_DECLARE_APP_ARGS(args,
  367. AST_APP_ARG(family);
  368. AST_APP_ARG(fieldmatch);
  369. AST_APP_ARG(value);
  370. AST_APP_ARG(delim1);
  371. AST_APP_ARG(delim2);
  372. );
  373. if (ast_strlen_zero(data)) {
  374. ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n");
  375. return -1;
  376. }
  377. AST_STANDARD_APP_ARGS(args, data);
  378. if (!args.delim1)
  379. args.delim1 = ",";
  380. if (!args.delim2)
  381. args.delim2 = "=";
  382. if (chan)
  383. ast_autoservice_start(chan);
  384. head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL);
  385. if (!head) {
  386. if (chan)
  387. ast_autoservice_stop(chan);
  388. return -1;
  389. }
  390. if (len > 0) {
  391. resultslen = 0;
  392. n = 0;
  393. for (var = head; var; n++, var = var->next) {
  394. resultslen += strlen(var->name) + strlen(var->value);
  395. }
  396. /* add space for delimiters and final '\0' */
  397. resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1;
  398. if (resultslen > len) {
  399. /* Unfortunately this does mean that we cannot destroy
  400. * the row anymore. But OTOH, we're not destroying
  401. * someones data without giving him the chance to look
  402. * at it. */
  403. ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len);
  404. return -1;
  405. }
  406. /* len is going to be sensible, so we don't need to check for
  407. * stack overflows here. */
  408. out = ast_str_alloca(resultslen);
  409. for (var = head; var; var = var->next) {
  410. ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
  411. }
  412. ast_copy_string(buf, ast_str_buffer(out), len);
  413. }
  414. ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL);
  415. ast_variables_destroy(head);
  416. if (chan)
  417. ast_autoservice_stop(chan);
  418. return 0;
  419. }
  420. /*!
  421. * \brief Wrapper to execute REALTIME_DESTROY from a write operation. Allows
  422. * execution even if live_dangerously is disabled.
  423. */
  424. static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  425. {
  426. return function_realtime_readdestroy(chan, cmd, data, NULL, 0);
  427. }
  428. static struct ast_custom_function realtime_function = {
  429. .name = "REALTIME",
  430. .read = function_realtime_read,
  431. .write = function_realtime_write,
  432. };
  433. static struct ast_custom_function realtimefield_function = {
  434. .name = "REALTIME_FIELD",
  435. .read = realtimefield_read,
  436. .write = function_realtime_write,
  437. };
  438. static struct ast_custom_function realtimehash_function = {
  439. .name = "REALTIME_HASH",
  440. .read = realtimefield_read,
  441. };
  442. static struct ast_custom_function realtime_store_function = {
  443. .name = "REALTIME_STORE",
  444. .write = function_realtime_store,
  445. };
  446. static struct ast_custom_function realtime_destroy_function = {
  447. .name = "REALTIME_DESTROY",
  448. .read = function_realtime_readdestroy,
  449. .write = function_realtime_writedestroy,
  450. };
  451. static int unload_module(void)
  452. {
  453. int res = 0;
  454. res |= ast_custom_function_unregister(&realtime_function);
  455. res |= ast_custom_function_unregister(&realtime_store_function);
  456. res |= ast_custom_function_unregister(&realtime_destroy_function);
  457. res |= ast_custom_function_unregister(&realtimefield_function);
  458. res |= ast_custom_function_unregister(&realtimehash_function);
  459. return res;
  460. }
  461. static int load_module(void)
  462. {
  463. int res = 0;
  464. res |= ast_custom_function_register(&realtime_function);
  465. res |= ast_custom_function_register(&realtime_store_function);
  466. res |= ast_custom_function_register_escalating(&realtime_destroy_function, AST_CFE_READ);
  467. res |= ast_custom_function_register(&realtimefield_function);
  468. res |= ast_custom_function_register(&realtimehash_function);
  469. return res;
  470. }
  471. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository");