app_db.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. * Copyright (C) 2003, Jefferson Noxon
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. * 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 Database access functions
  23. *
  24. * \ingroup applications
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/options.h"
  34. #include "asterisk/file.h"
  35. #include "asterisk/logger.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/astdb.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/options.h"
  42. static char *tdesc = "Database Access Functions";
  43. static char *g_descrip =
  44. " DBget(varname=family/key[|options]): This application will retrieve a value\n"
  45. "from the Asterisk database and store it in the given variable.\n"
  46. " Options:\n"
  47. " j - Jump to priority n+101 if the requested family/key isn't found.\n"
  48. " This application sets the following channel variable upon completion:\n"
  49. " DBGETSTATUS - This variable will contain the status of the attempt\n"
  50. " FOUND | NOTFOUND \n"
  51. " This application has been deprecated in favor of the DB function.\n";
  52. static char *p_descrip =
  53. " DBput(family/key=value): This application will store the given value in the\n"
  54. "specified location in the Asterisk database.\n"
  55. " This application has been deprecated in favor of the DB function.\n";
  56. static char *d_descrip =
  57. " DBdel(family/key): This applicaiton will delete a key from the Asterisk\n"
  58. "database.\n";
  59. static char *dt_descrip =
  60. " DBdeltree(family[/keytree]): This application will delete a family or keytree\n"
  61. "from the Asterisk database\n";
  62. static char *g_app = "DBget";
  63. static char *p_app = "DBput";
  64. static char *d_app = "DBdel";
  65. static char *dt_app = "DBdeltree";
  66. static char *g_synopsis = "Retrieve a value from the database";
  67. static char *p_synopsis = "Store a value in the database";
  68. static char *d_synopsis = "Delete a key from the database";
  69. static char *dt_synopsis = "Delete a family or keytree from the database";
  70. STANDARD_LOCAL_USER;
  71. LOCAL_USER_DECL;
  72. static int deltree_exec(struct ast_channel *chan, void *data)
  73. {
  74. char *argv, *family, *keytree;
  75. struct localuser *u;
  76. LOCAL_USER_ADD(u);
  77. argv = ast_strdupa(data);
  78. if (!argv) {
  79. ast_log(LOG_ERROR, "Memory allocation failed\n");
  80. LOCAL_USER_REMOVE(u);
  81. return 0;
  82. }
  83. if (strchr(argv, '/')) {
  84. family = strsep(&argv, "/");
  85. keytree = strsep(&argv, "\0");
  86. if (!family || !keytree) {
  87. ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  88. LOCAL_USER_REMOVE(u);
  89. return 0;
  90. }
  91. if (ast_strlen_zero(keytree))
  92. keytree = 0;
  93. } else {
  94. family = argv;
  95. keytree = 0;
  96. }
  97. if (option_verbose > 2) {
  98. if (keytree)
  99. ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
  100. else
  101. ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
  102. }
  103. if (ast_db_deltree(family, keytree)) {
  104. if (option_verbose > 2)
  105. ast_verbose(VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
  106. }
  107. LOCAL_USER_REMOVE(u);
  108. return 0;
  109. }
  110. static int del_exec(struct ast_channel *chan, void *data)
  111. {
  112. char *argv, *family, *key;
  113. struct localuser *u;
  114. LOCAL_USER_ADD(u);
  115. argv = ast_strdupa(data);
  116. if (!argv) {
  117. ast_log (LOG_ERROR, "Memory allocation failed\n");
  118. LOCAL_USER_REMOVE(u);
  119. return 0;
  120. }
  121. if (strchr(argv, '/')) {
  122. family = strsep(&argv, "/");
  123. key = strsep(&argv, "\0");
  124. if (!family || !key) {
  125. ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  126. LOCAL_USER_REMOVE(u);
  127. return 0;
  128. }
  129. if (option_verbose > 2)
  130. ast_verbose(VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
  131. if (ast_db_del(family, key)) {
  132. if (option_verbose > 2)
  133. ast_verbose(VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
  134. }
  135. } else {
  136. ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
  137. }
  138. LOCAL_USER_REMOVE(u);
  139. return 0;
  140. }
  141. static int put_exec(struct ast_channel *chan, void *data)
  142. {
  143. char *argv, *value, *family, *key;
  144. static int dep_warning = 0;
  145. struct localuser *u;
  146. LOCAL_USER_ADD(u);
  147. if (!dep_warning) {
  148. ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
  149. dep_warning = 1;
  150. }
  151. argv = ast_strdupa(data);
  152. if (!argv) {
  153. ast_log(LOG_ERROR, "Memory allocation failed\n");
  154. LOCAL_USER_REMOVE(u);
  155. return 0;
  156. }
  157. if (strchr(argv, '/') && strchr(argv, '=')) {
  158. family = strsep(&argv, "/");
  159. key = strsep(&argv, "=");
  160. value = strsep(&argv, "\0");
  161. if (!value || !family || !key) {
  162. ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  163. LOCAL_USER_REMOVE(u);
  164. return 0;
  165. }
  166. if (option_verbose > 2)
  167. ast_verbose(VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value);
  168. if (ast_db_put(family, key, value)) {
  169. if (option_verbose > 2)
  170. ast_verbose(VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n");
  171. }
  172. } else {
  173. ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
  174. }
  175. LOCAL_USER_REMOVE(u);
  176. return 0;
  177. }
  178. static int get_exec(struct ast_channel *chan, void *data)
  179. {
  180. char *argv, *varname, *family, *key, *options = NULL;
  181. char dbresult[256];
  182. static int dep_warning = 0;
  183. int priority_jump = 0;
  184. struct localuser *u;
  185. LOCAL_USER_ADD(u);
  186. if (!dep_warning) {
  187. ast_log(LOG_WARNING, "This application has been deprecated, please use the ${DB(family/key)} function instead.\n");
  188. dep_warning = 1;
  189. }
  190. argv = ast_strdupa(data);
  191. if (!argv) {
  192. ast_log(LOG_ERROR, "Memory allocation failed\n");
  193. LOCAL_USER_REMOVE(u);
  194. return 0;
  195. }
  196. if (strchr(argv, '=') && strchr(argv, '/')) {
  197. varname = strsep(&argv, "=");
  198. family = strsep(&argv, "/");
  199. if (strchr((void *)&argv, '|')) {
  200. key = strsep(&argv, "|");
  201. options = strsep(&argv, "\0");
  202. } else
  203. key = strsep(&argv, "\0");
  204. if (!varname || !family || !key) {
  205. ast_log(LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  206. LOCAL_USER_REMOVE(u);
  207. return 0;
  208. }
  209. if (options) {
  210. if (strchr(options, 'j'))
  211. priority_jump = 1;
  212. }
  213. if (option_verbose > 2)
  214. ast_verbose(VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key);
  215. if (!ast_db_get(family, key, dbresult, sizeof (dbresult) - 1)) {
  216. pbx_builtin_setvar_helper(chan, varname, dbresult);
  217. if (option_verbose > 2)
  218. ast_verbose(VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult);
  219. pbx_builtin_setvar_helper(chan, "DBGETSTATUS", "FOUND");
  220. } else {
  221. if (option_verbose > 2)
  222. ast_verbose(VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
  223. if (priority_jump || option_priority_jumping) {
  224. /* Send the call to n+101 priority, where n is the current priority */
  225. ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
  226. }
  227. pbx_builtin_setvar_helper(chan, "DBGETSTATUS", "NOTFOUND");
  228. }
  229. } else {
  230. ast_log(LOG_DEBUG, "Ignoring, no parameters\n");
  231. }
  232. LOCAL_USER_REMOVE(u);
  233. return 0;
  234. }
  235. int unload_module(void)
  236. {
  237. int retval;
  238. retval = ast_unregister_application(dt_app);
  239. retval |= ast_unregister_application(d_app);
  240. retval |= ast_unregister_application(p_app);
  241. retval |= ast_unregister_application(g_app);
  242. STANDARD_HANGUP_LOCALUSERS;
  243. return retval;
  244. }
  245. int load_module(void)
  246. {
  247. int retval;
  248. retval = ast_register_application(g_app, get_exec, g_synopsis, g_descrip);
  249. retval |= ast_register_application(p_app, put_exec, p_synopsis, p_descrip);
  250. retval |= ast_register_application(d_app, del_exec, d_synopsis, d_descrip);
  251. retval |= ast_register_application(dt_app, deltree_exec, dt_synopsis, dt_descrip);
  252. return retval;
  253. }
  254. char *description(void)
  255. {
  256. return tdesc;
  257. }
  258. int usecount(void)
  259. {
  260. int res;
  261. STANDARD_USECOUNT(res);
  262. return res;
  263. }
  264. char *key()
  265. {
  266. return ASTERISK_GPL_KEY;
  267. }