app_db.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Database access functions
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. * Copyright (C) 2003, Jefferson Noxon
  8. *
  9. * Mark Spencer <markster@linux-support.net>
  10. * Jefferson Noxon <jeff@debian.org>
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License
  14. */
  15. #include <sys/types.h>
  16. #include <asterisk/options.h>
  17. #include <asterisk/file.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/channel.h>
  20. #include <asterisk/pbx.h>
  21. #include <asterisk/module.h>
  22. #include <asterisk/astdb.h>
  23. #include <asterisk/lock.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. static char *tdesc = "Database access functions for Asterisk extension logic";
  29. static char *g_descrip =
  30. " DBget(varname=family/key): Retrieves a value from the Asterisk\n"
  31. "database and stores it in the given variable. Always returns 0. If the\n"
  32. "requested key is not found, jumps to priority n+101 if available.\n";
  33. static char *p_descrip =
  34. " DBput(family/key=value): Stores the given value in the Asterisk\n"
  35. "database. Always returns 0.\n";
  36. static char *d_descrip =
  37. " DBdel(family/key): Deletes a key from the Asterisk database. Always\n"
  38. "returns 0.\n";
  39. static char *dt_descrip =
  40. " DBdeltree(family[/keytree]): Deletes a family or keytree from the Asterisk\n"
  41. "database. Always returns 0.\n";
  42. static char *g_app = "DBget";
  43. static char *p_app = "DBput";
  44. static char *d_app = "DBdel";
  45. static char *dt_app = "DBdeltree";
  46. static char *g_synopsis = "Retrieve a value from the database";
  47. static char *p_synopsis = "Store a value in the database";
  48. static char *d_synopsis = "Delete a key from the database";
  49. static char *dt_synopsis = "Delete a family or keytree from the database";
  50. STANDARD_LOCAL_USER;
  51. LOCAL_USER_DECL;
  52. static int deltree_exec (struct ast_channel *chan, void *data)
  53. {
  54. int arglen;
  55. char *argv, *family, *keytree;
  56. arglen = strlen (data);
  57. argv = alloca (arglen + 1);
  58. if (!argv) { /* Why would this fail? */
  59. ast_log (LOG_DEBUG, "Memory allocation failed\n");
  60. return 0;
  61. }
  62. memcpy (argv, data, arglen + 1);
  63. if (strchr (argv, '/')) {
  64. family = strsep (&argv, "/");
  65. keytree = strsep (&argv, "\0");
  66. if (!family || !keytree) {
  67. ast_log (LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  68. return 0;
  69. }
  70. if (!strlen (keytree))
  71. keytree = 0;
  72. } else {
  73. family = argv;
  74. keytree = 0;
  75. }
  76. if (option_verbose > 2) {
  77. if (keytree)
  78. ast_verbose (VERBOSE_PREFIX_3 "DBdeltree: family=%s, keytree=%s\n", family, keytree);
  79. else
  80. ast_verbose (VERBOSE_PREFIX_3 "DBdeltree: family=%s\n", family);
  81. }
  82. if (ast_db_deltree (family, keytree)) {
  83. if (option_verbose > 2)
  84. ast_verbose (VERBOSE_PREFIX_3 "DBdeltree: Error deleting key from database.\n");
  85. }
  86. return 0;
  87. }
  88. static int del_exec (struct ast_channel *chan, void *data)
  89. {
  90. int arglen;
  91. char *argv, *family, *key;
  92. arglen = strlen (data);
  93. argv = alloca (arglen + 1);
  94. if (!argv) { /* Why would this fail? */
  95. ast_log (LOG_DEBUG, "Memory allocation failed\n");
  96. return 0;
  97. }
  98. memcpy (argv, data, arglen + 1);
  99. if (strchr (argv, '/')) {
  100. family = strsep (&argv, "/");
  101. key = strsep (&argv, "\0");
  102. if (!family || !key) {
  103. ast_log (LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  104. return 0;
  105. }
  106. if (option_verbose > 2)
  107. ast_verbose (VERBOSE_PREFIX_3 "DBdel: family=%s, key=%s\n", family, key);
  108. if (ast_db_del (family, key)) {
  109. if (option_verbose > 2)
  110. ast_verbose (VERBOSE_PREFIX_3 "DBdel: Error deleting key from database.\n");
  111. }
  112. } else {
  113. ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
  114. }
  115. return 0;
  116. }
  117. static int put_exec (struct ast_channel *chan, void *data)
  118. {
  119. int arglen;
  120. char *argv, *value, *family, *key;
  121. arglen = strlen (data);
  122. argv = alloca (arglen + 1);
  123. if (!argv) { /* Why would this fail? */
  124. ast_log (LOG_DEBUG, "Memory allocation failed\n");
  125. return 0;
  126. }
  127. memcpy (argv, data, arglen + 1);
  128. if (strchr (argv, '/') && strchr (argv, '=')) {
  129. family = strsep (&argv, "/");
  130. key = strsep (&argv, "=");
  131. value = strsep (&argv, "\0");
  132. if (!value || !family || !key) {
  133. ast_log (LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  134. return 0;
  135. }
  136. if (option_verbose > 2)
  137. ast_verbose (VERBOSE_PREFIX_3 "DBput: family=%s, key=%s, value=%s\n", family, key, value);
  138. if (ast_db_put (family, key, value)) {
  139. if (option_verbose > 2)
  140. ast_verbose (VERBOSE_PREFIX_3 "DBput: Error writing value to database.\n");
  141. }
  142. } else {
  143. ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
  144. }
  145. return 0;
  146. }
  147. static int get_exec (struct ast_channel *chan, void *data)
  148. {
  149. int arglen;
  150. char *argv, *varname, *family, *key;
  151. char dbresult[256];
  152. arglen = strlen (data);
  153. argv = alloca (arglen + 1);
  154. if (!argv) { /* Why would this fail? */
  155. ast_log (LOG_DEBUG, "Memory allocation failed\n");
  156. return 0;
  157. }
  158. memcpy (argv, data, arglen + 1);
  159. if (strchr (argv, '=') && strchr (argv, '/')) {
  160. varname = strsep (&argv, "=");
  161. family = strsep (&argv, "/");
  162. key = strsep (&argv, "\0");
  163. if (!varname || !family || !key) {
  164. ast_log (LOG_DEBUG, "Ignoring; Syntax error in argument\n");
  165. return 0;
  166. }
  167. if (option_verbose > 2)
  168. ast_verbose (VERBOSE_PREFIX_3 "DBget: varname=%s, family=%s, key=%s\n", varname, family, key);
  169. if (!ast_db_get (family, key, dbresult, sizeof (dbresult) - 1)) {
  170. pbx_builtin_setvar_helper (chan, varname, dbresult);
  171. if (option_verbose > 2)
  172. ast_verbose (VERBOSE_PREFIX_3 "DBget: set variable %s to %s\n", varname, dbresult);
  173. } else {
  174. if (option_verbose > 2)
  175. ast_verbose (VERBOSE_PREFIX_3 "DBget: Value not found in database.\n");
  176. /* Send the call to n+101 priority, where n is the current priority */
  177. if (ast_exists_extension (chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  178. chan->priority += 100;
  179. }
  180. } else {
  181. ast_log (LOG_DEBUG, "Ignoring, no parameters\n");
  182. }
  183. return 0;
  184. }
  185. int unload_module (void)
  186. {
  187. int retval;
  188. STANDARD_HANGUP_LOCALUSERS;
  189. retval = ast_unregister_application (dt_app);
  190. retval |= ast_unregister_application (d_app);
  191. retval |= ast_unregister_application (p_app);
  192. retval |= ast_unregister_application (g_app);
  193. return retval;
  194. }
  195. int load_module (void)
  196. {
  197. int retval;
  198. retval = ast_register_application (g_app, get_exec, g_synopsis, g_descrip);
  199. if (!retval)
  200. retval = ast_register_application (p_app, put_exec, p_synopsis, p_descrip);
  201. if (!retval)
  202. retval = ast_register_application (d_app, del_exec, d_synopsis, d_descrip);
  203. if (!retval)
  204. retval = ast_register_application (dt_app, deltree_exec, dt_synopsis, dt_descrip);
  205. return retval;
  206. }
  207. char *description (void)
  208. {
  209. return tdesc;
  210. }
  211. int usecount (void)
  212. {
  213. int res;
  214. STANDARD_USECOUNT (res);
  215. return res;
  216. }
  217. char *key ()
  218. {
  219. return ASTERISK_GPL_KEY;
  220. }