res_realtime.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Anthony Minessale <anthmct@yahoo.com>
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief RealTime CLI
  22. *
  23. * \author Anthony Minessale <anthmct@yahoo.com>
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \ingroup applications
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/file.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/config.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/lock.h"
  39. #include "asterisk/cli.h"
  40. static char *cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  41. {
  42. #define CRL_HEADER_FORMAT "%30s %-30s\n"
  43. struct ast_variable *var = NULL, *orig_var = NULL;
  44. switch (cmd) {
  45. case CLI_INIT:
  46. e->command = "realtime load";
  47. e->usage =
  48. "Usage: realtime load <family> <colmatch> <value>\n"
  49. " Prints out a list of variables using the RealTime driver.\n"
  50. " You must supply a family name, a column to match on, and a value to match to.\n";
  51. return NULL;
  52. case CLI_GENERATE:
  53. return NULL;
  54. }
  55. if (a->argc < 5)
  56. return CLI_SHOWUSAGE;
  57. var = ast_load_realtime_all(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
  58. if (var) {
  59. ast_cli(a->fd, CRL_HEADER_FORMAT, "Column Name", "Column Value");
  60. ast_cli(a->fd, CRL_HEADER_FORMAT, "--------------------", "--------------------");
  61. orig_var = var;
  62. while (var) {
  63. ast_cli(a->fd, CRL_HEADER_FORMAT, var->name, var->value);
  64. var = var->next;
  65. }
  66. } else {
  67. ast_cli(a->fd, "No rows found matching search criteria.\n");
  68. }
  69. ast_variables_destroy(orig_var);
  70. return CLI_SUCCESS;
  71. }
  72. static char *cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  73. {
  74. int res = 0;
  75. switch (cmd) {
  76. case CLI_INIT:
  77. e->command = "realtime update";
  78. e->usage =
  79. "Usage: realtime update <family> <colmatch> <valuematch> <colupdate> <newvalue>\n"
  80. " Update a single variable using the RealTime driver.\n"
  81. " You must supply a family name, a column to update on, a new value, column to match, and value to match.\n"
  82. " Ex: realtime update sippeers name bobsphone port 4343\n"
  83. " will execute SQL as UPDATE sippeers SET port = 4343 WHERE name = bobsphone\n";
  84. return NULL;
  85. case CLI_GENERATE:
  86. return NULL;
  87. }
  88. if (a->argc < 7)
  89. return CLI_SHOWUSAGE;
  90. res = ast_update_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
  91. if (res < 0) {
  92. ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
  93. return CLI_FAILURE;
  94. }
  95. ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
  96. return CLI_SUCCESS;
  97. }
  98. static char *cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  99. {
  100. int res = -1;
  101. switch (cmd) {
  102. case CLI_INIT:
  103. e->command = "realtime update2";
  104. e->usage =
  105. "Usage: realtime update2 <family> <colmatch> <valuematch> [... <colmatch5> <valuematch5>] NULL <colupdate> <newvalue>\n"
  106. " Update a single variable, requiring one or more fields to match using the\n"
  107. " RealTime driver. You must supply a family name, a column to update, a new\n"
  108. " value, and at least one column and value to match.\n"
  109. " Ex: realtime update sippeers name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
  110. " will execute SQL as\n"
  111. " UPDATE sippeers SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
  112. return NULL;
  113. case CLI_GENERATE:
  114. return NULL;
  115. }
  116. if (a->argc < 7)
  117. return CLI_SHOWUSAGE;
  118. if (a->argc == 7) {
  119. res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL, a->argv[5], a->argv[6], SENTINEL);
  120. } else if (a->argc == 9) {
  121. res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL, a->argv[7], a->argv[8], SENTINEL);
  122. } else if (a->argc == 11) {
  123. res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL, a->argv[9], a->argv[10], SENTINEL);
  124. } else if (a->argc == 13) {
  125. res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL, a->argv[11], a->argv[12], SENTINEL);
  126. } else if (a->argc == 15) {
  127. res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL, a->argv[13], a->argv[14], SENTINEL);
  128. } else {
  129. return CLI_SHOWUSAGE;
  130. }
  131. if (res < 0) {
  132. ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
  133. return CLI_FAILURE;
  134. }
  135. ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
  136. return CLI_SUCCESS;
  137. }
  138. static char *cli_realtime_store(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  139. {
  140. int res = -1;
  141. switch (cmd) {
  142. case CLI_INIT:
  143. e->command = "realtime store";
  144. e->usage =
  145. "Usage: realtime store <family> <colname1> <value1> [<colname2> <value2> [... <colname5> <value5>]]\n"
  146. " Create a stored row using the RealTime driver.\n"
  147. " You must supply a family name and name/value pairs (up to 5). If\n"
  148. " you need to store more than 5 key/value pairs, start with the first\n"
  149. " five, then use 'realtime update' or 'realtime update2' to add\n"
  150. " additional columns.\n";
  151. return NULL;
  152. case CLI_GENERATE:
  153. return NULL;
  154. }
  155. if (a->argc < 5) {
  156. return CLI_SHOWUSAGE;
  157. } else if (a->argc == 5) {
  158. res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
  159. } else if (a->argc == 7) {
  160. res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
  161. } else if (a->argc == 9) {
  162. res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
  163. } else if (a->argc == 11) {
  164. res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
  165. } else if (a->argc == 13) {
  166. res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
  167. } else {
  168. return CLI_SHOWUSAGE;
  169. }
  170. if (res < 0) {
  171. ast_cli(a->fd, "Failed to store record. Check the debug log for possible SQL related entries.\n");
  172. return CLI_FAILURE;
  173. }
  174. ast_cli(a->fd, "Stored RealTime record.\n");
  175. return CLI_SUCCESS;
  176. }
  177. static char *cli_realtime_destroy(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  178. {
  179. int res = -1;
  180. switch (cmd) {
  181. case CLI_INIT:
  182. e->command = "realtime destroy";
  183. e->usage =
  184. "Usage: realtime destroy <family> <colmatch1> <valuematch1> [<colmatch2> <valuematch2> [... <colmatch5> <valuematch5>]]\n"
  185. " Remove a stored row using the RealTime driver.\n"
  186. " You must supply a family name and name/value pairs (up to 5).\n";
  187. return NULL;
  188. case CLI_GENERATE:
  189. return NULL;
  190. }
  191. if (a->argc < 5) {
  192. return CLI_SHOWUSAGE;
  193. } else if (a->argc == 5) {
  194. res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
  195. } else if (a->argc == 7) {
  196. res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
  197. } else if (a->argc == 9) {
  198. res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
  199. } else if (a->argc == 11) {
  200. res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
  201. } else if (a->argc == 13) {
  202. res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
  203. } else {
  204. return CLI_SHOWUSAGE;
  205. }
  206. if (res < 0) {
  207. ast_cli(a->fd, "Failed to remove record. Check the debug log for possible SQL related entries.\n");
  208. return CLI_FAILURE;
  209. }
  210. ast_cli(a->fd, "Removed %d RealTime record%s.\n", res, ESS(res));
  211. return CLI_SUCCESS;
  212. }
  213. static struct ast_cli_entry cli_realtime[] = {
  214. AST_CLI_DEFINE(cli_realtime_load, "Used to print out RealTime variables."),
  215. AST_CLI_DEFINE(cli_realtime_update, "Used to update RealTime variables."),
  216. AST_CLI_DEFINE(cli_realtime_update2, "Used to test the RealTime update2 method"),
  217. AST_CLI_DEFINE(cli_realtime_store, "Store a new row into a RealTime database"),
  218. AST_CLI_DEFINE(cli_realtime_destroy, "Delete a row from a RealTime database"),
  219. };
  220. static int unload_module(void)
  221. {
  222. ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
  223. return 0;
  224. }
  225. static int load_module(void)
  226. {
  227. ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
  228. return AST_MODULE_LOAD_SUCCESS;
  229. }
  230. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");