func_db.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, 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. */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <regex.h>
  29. #include "asterisk.h"
  30. /* ASTERISK_FILE_VERSION(__FILE__, "$Revision$") */
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/logger.h"
  34. #include "asterisk/options.h"
  35. #include "asterisk/utils.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/astdb.h"
  38. static char *function_db_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  39. {
  40. int argc;
  41. char *args;
  42. char *argv[2];
  43. char *family;
  44. char *key;
  45. if (ast_strlen_zero(data)) {
  46. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
  47. return buf;
  48. }
  49. args = ast_strdupa(data);
  50. argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
  51. if (argc > 1) {
  52. family = argv[0];
  53. key = argv[1];
  54. } else {
  55. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
  56. return buf;
  57. }
  58. if (ast_db_get(family, key, buf, len-1)) {
  59. ast_log(LOG_DEBUG, "DB: %s/%s not found in database.\n", family, key);
  60. } else
  61. pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
  62. return buf;
  63. }
  64. static void function_db_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
  65. {
  66. int argc;
  67. char *args;
  68. char *argv[2];
  69. char *family;
  70. char *key;
  71. if (ast_strlen_zero(data)) {
  72. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
  73. return;
  74. }
  75. args = ast_strdupa(data);
  76. argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
  77. if (argc > 1) {
  78. family = argv[0];
  79. key = argv[1];
  80. } else {
  81. ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
  82. return;
  83. }
  84. if (ast_db_put(family, key, (char*)value)) {
  85. ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
  86. }
  87. }
  88. #ifndef BUILTIN_FUNC
  89. static
  90. #endif
  91. struct ast_custom_function db_function = {
  92. .name = "DB",
  93. .synopsis = "Read or Write from/to the Asterisk database",
  94. .syntax = "DB(<family>/<key>)",
  95. .desc = "This function will read or write a value from/to the Asterisk database.\n"
  96. "DB(...) will read a value from the database, while DB(...)=value\n"
  97. "will write a value to the database. On a read, this function\n"
  98. "returns the value from the datase, or NULL if it does not exist.\n"
  99. "On a write, this function will always return NULL. Reading a database value\n"
  100. "will also set the variable DB_RESULT.\n",
  101. .read = function_db_read,
  102. .write = function_db_write,
  103. };
  104. static char *function_db_exists(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
  105. {
  106. int argc;
  107. char *args;
  108. char *argv[2];
  109. char *family;
  110. char *key;
  111. if (ast_strlen_zero(data)) {
  112. ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
  113. return buf;
  114. }
  115. args = ast_strdupa(data);
  116. argc = ast_app_separate_args(args, '/', argv, sizeof(argv) / sizeof(argv[0]));
  117. if (argc > 1) {
  118. family = argv[0];
  119. key = argv[1];
  120. } else {
  121. ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
  122. return buf;
  123. }
  124. if (ast_db_get(family, key, buf, len-1))
  125. ast_copy_string(buf, "0", len);
  126. else {
  127. pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
  128. ast_copy_string(buf, "1", len);
  129. }
  130. return buf;
  131. }
  132. #ifndef BUILTIN_FUNC
  133. static
  134. #endif
  135. struct ast_custom_function db_exists_function = {
  136. .name = "DB_EXISTS",
  137. .synopsis = "Check to see if a key exists in the Asterisk database",
  138. .syntax = "DB_EXISTS(<family>/<key>)",
  139. .desc = "This function will check to see if a key exists in the Asterisk\n"
  140. "database. If it exists, the function will return \"1\". If not,\n"
  141. "it will return \"0\". Checking for existence of a database key will\n"
  142. "also set the variable DB_RESULT to the key's value if it exists.\n",
  143. .read = function_db_exists,
  144. };