func_db.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu>
  5. *
  6. * func_db.c adapted from the old app_db.c, copyright by the following people
  7. * Copyright (C) 2005, Mark Spencer <markster@digium.com>
  8. * Copyright (C) 2003, Jefferson Noxon <jeff@debian.org>
  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. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Functions for interaction with the Asterisk database
  23. *
  24. * \author Russell Bryant <russelb@clemson.edu>
  25. *
  26. * \ingroup functions
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <regex.h>
  34. #include "asterisk/module.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/utils.h"
  38. #include "asterisk/app.h"
  39. #include "asterisk/astdb.h"
  40. /*** DOCUMENTATION
  41. <function name="DB" language="en_US">
  42. <synopsis>
  43. Read from or write to the Asterisk database.
  44. </synopsis>
  45. <syntax argsep="/">
  46. <parameter name="family" required="true" />
  47. <parameter name="key" required="true" />
  48. </syntax>
  49. <description>
  50. <para>This function will read from or write a value to the Asterisk database. On a
  51. read, this function returns the corresponding value from the database, or blank
  52. if it does not exist. Reading a database value will also set the variable
  53. DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS
  54. function.</para>
  55. </description>
  56. <see-also>
  57. <ref type="application">DBdel</ref>
  58. <ref type="function">DB_DELETE</ref>
  59. <ref type="application">DBdeltree</ref>
  60. <ref type="function">DB_EXISTS</ref>
  61. </see-also>
  62. </function>
  63. <function name="DB_EXISTS" language="en_US">
  64. <synopsis>
  65. Check to see if a key exists in the Asterisk database.
  66. </synopsis>
  67. <syntax argsep="/">
  68. <parameter name="family" required="true" />
  69. <parameter name="key" required="true" />
  70. </syntax>
  71. <description>
  72. <para>This function will check to see if a key exists in the Asterisk
  73. database. If it exists, the function will return <literal>1</literal>. If not,
  74. it will return <literal>0</literal>. Checking for existence of a database key will
  75. also set the variable DB_RESULT to the key's value if it exists.</para>
  76. </description>
  77. <see-also>
  78. <ref type="function">DB</ref>
  79. </see-also>
  80. </function>
  81. <function name="DB_DELETE" language="en_US">
  82. <synopsis>
  83. Return a value from the database and delete it.
  84. </synopsis>
  85. <syntax argsep="/">
  86. <parameter name="family" required="true" />
  87. <parameter name="key" required="true" />
  88. </syntax>
  89. <description>
  90. <para>This function will retrieve a value from the Asterisk database
  91. and then remove that key from the database. <variable>DB_RESULT</variable>
  92. will be set to the key's value if it exists.</para>
  93. </description>
  94. <see-also>
  95. <ref type="application">DBdel</ref>
  96. <ref type="function">DB</ref>
  97. <ref type="application">DBdeltree</ref>
  98. </see-also>
  99. </function>
  100. ***/
  101. static int function_db_read(struct ast_channel *chan, const char *cmd,
  102. char *parse, char *buf, size_t len)
  103. {
  104. AST_DECLARE_APP_ARGS(args,
  105. AST_APP_ARG(family);
  106. AST_APP_ARG(key);
  107. );
  108. buf[0] = '\0';
  109. if (ast_strlen_zero(parse)) {
  110. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
  111. return -1;
  112. }
  113. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  114. if (args.argc < 2) {
  115. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
  116. return -1;
  117. }
  118. if (ast_db_get(args.family, args.key, buf, len - 1)) {
  119. ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
  120. } else {
  121. pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
  122. }
  123. return 0;
  124. }
  125. static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
  126. const char *value)
  127. {
  128. AST_DECLARE_APP_ARGS(args,
  129. AST_APP_ARG(family);
  130. AST_APP_ARG(key);
  131. );
  132. if (ast_strlen_zero(parse)) {
  133. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
  134. return -1;
  135. }
  136. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  137. if (args.argc < 2) {
  138. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
  139. return -1;
  140. }
  141. if (ast_db_put(args.family, args.key, value)) {
  142. ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
  143. }
  144. return 0;
  145. }
  146. static struct ast_custom_function db_function = {
  147. .name = "DB",
  148. .read = function_db_read,
  149. .write = function_db_write,
  150. };
  151. static int function_db_exists(struct ast_channel *chan, const char *cmd,
  152. char *parse, char *buf, size_t len)
  153. {
  154. AST_DECLARE_APP_ARGS(args,
  155. AST_APP_ARG(family);
  156. AST_APP_ARG(key);
  157. );
  158. buf[0] = '\0';
  159. if (ast_strlen_zero(parse)) {
  160. ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
  161. return -1;
  162. }
  163. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  164. if (args.argc < 2) {
  165. ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
  166. return -1;
  167. }
  168. if (ast_db_get(args.family, args.key, buf, len - 1)) {
  169. strcpy(buf, "0");
  170. } else {
  171. pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
  172. strcpy(buf, "1");
  173. }
  174. return 0;
  175. }
  176. static struct ast_custom_function db_exists_function = {
  177. .name = "DB_EXISTS",
  178. .read = function_db_exists,
  179. .read_max = 2,
  180. };
  181. static int function_db_delete(struct ast_channel *chan, const char *cmd,
  182. char *parse, char *buf, size_t len)
  183. {
  184. AST_DECLARE_APP_ARGS(args,
  185. AST_APP_ARG(family);
  186. AST_APP_ARG(key);
  187. );
  188. buf[0] = '\0';
  189. if (ast_strlen_zero(parse)) {
  190. ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
  191. return -1;
  192. }
  193. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  194. if (args.argc < 2) {
  195. ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
  196. return -1;
  197. }
  198. if (ast_db_get(args.family, args.key, buf, len - 1)) {
  199. ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
  200. } else {
  201. if (ast_db_del(args.family, args.key)) {
  202. ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
  203. }
  204. }
  205. pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
  206. return 0;
  207. }
  208. static struct ast_custom_function db_delete_function = {
  209. .name = "DB_DELETE",
  210. .read = function_db_delete,
  211. };
  212. static int unload_module(void)
  213. {
  214. int res = 0;
  215. res |= ast_custom_function_unregister(&db_function);
  216. res |= ast_custom_function_unregister(&db_exists_function);
  217. res |= ast_custom_function_unregister(&db_delete_function);
  218. return res;
  219. }
  220. static int load_module(void)
  221. {
  222. int res = 0;
  223. res |= ast_custom_function_register(&db_function);
  224. res |= ast_custom_function_register(&db_exists_function);
  225. res |= ast_custom_function_register(&db_delete_function);
  226. return res;
  227. }
  228. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");