Wed Jan 8 2020 09:49:50

Asterisk developer's documentation


res_realtime.c
Go to the documentation of this file.
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 
20 /*! \file
21  *
22  * \brief RealTime CLI
23  *
24  * \author Anthony Minessale <anthmct@yahoo.com>
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \ingroup applications
28  */
29 
30 /*** MODULEINFO
31  <support_level>core</support_level>
32  ***/
33 
34 #include "asterisk.h"
35 
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 342869 $")
37 
38 #include "asterisk/file.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/config.h"
42 #include "asterisk/module.h"
43 #include "asterisk/lock.h"
44 #include "asterisk/cli.h"
45 
46 
47 static char *cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
48 {
49 #define CRL_HEADER_FORMAT "%30s %-30s\n"
50  struct ast_variable *var = NULL, *orig_var = NULL;
51 
52  switch (cmd) {
53  case CLI_INIT:
54  e->command = "realtime load";
55  e->usage =
56  "Usage: realtime load <family> <colmatch> <value>\n"
57  " Prints out a list of variables using the RealTime driver.\n"
58  " You must supply a family name, a column to match on, and a value to match to.\n";
59  return NULL;
60  case CLI_GENERATE:
61  return NULL;
62  }
63 
64 
65  if (a->argc < 5)
66  return CLI_SHOWUSAGE;
67 
68  var = ast_load_realtime_all(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
69 
70  if (var) {
71  ast_cli(a->fd, CRL_HEADER_FORMAT, "Column Name", "Column Value");
72  ast_cli(a->fd, CRL_HEADER_FORMAT, "--------------------", "--------------------");
73  orig_var = var;
74  while (var) {
75  ast_cli(a->fd, CRL_HEADER_FORMAT, var->name, var->value);
76  var = var->next;
77  }
78  } else {
79  ast_cli(a->fd, "No rows found matching search criteria.\n");
80  }
81  ast_variables_destroy(orig_var);
82  return CLI_SUCCESS;
83 }
84 
85 static char *cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
86 {
87  int res = 0;
88 
89  switch (cmd) {
90  case CLI_INIT:
91  e->command = "realtime update";
92  e->usage =
93  "Usage: realtime update <family> <colmatch> <valuematch> <colupdate> <newvalue>\n"
94  " Update a single variable using the RealTime driver.\n"
95  " You must supply a family name, a column to update on, a new value, column to match, and value to match.\n"
96  " Ex: realtime update sippeers name bobsphone port 4343\n"
97  " will execute SQL as UPDATE sippeers SET port = 4343 WHERE name = bobsphone\n";
98  return NULL;
99  case CLI_GENERATE:
100  return NULL;
101  }
102 
103  if (a->argc < 7)
104  return CLI_SHOWUSAGE;
105 
106  res = ast_update_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
107 
108  if (res < 0) {
109  ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
110  return CLI_FAILURE;
111  }
112 
113  ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
114 
115  return CLI_SUCCESS;
116 }
117 
118 static char *cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
119 {
120  int res = -1;
121 
122  switch (cmd) {
123  case CLI_INIT:
124  e->command = "realtime update2";
125  e->usage =
126  "Usage: realtime update2 <family> <colmatch> <valuematch> [... <colmatch5> <valuematch5>] NULL <colupdate> <newvalue>\n"
127  " Update a single variable, requiring one or more fields to match using the\n"
128  " RealTime driver. You must supply a family name, a column to update, a new\n"
129  " value, and at least one column and value to match.\n"
130  " Ex: realtime update sippeers name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
131  " will execute SQL as\n"
132  " UPDATE sippeers SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
133  return NULL;
134  case CLI_GENERATE:
135  return NULL;
136  }
137 
138  if (a->argc < 7)
139  return CLI_SHOWUSAGE;
140 
141  if (a->argc == 7) {
142  res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL, a->argv[5], a->argv[6], SENTINEL);
143  } else if (a->argc == 9) {
144  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);
145  } else if (a->argc == 11) {
146  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);
147  } else if (a->argc == 13) {
148  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);
149  } else if (a->argc == 15) {
150  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);
151  } else {
152  return CLI_SHOWUSAGE;
153  }
154 
155  if (res < 0) {
156  ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
157  return CLI_FAILURE;
158  }
159 
160  ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
161 
162  return CLI_SUCCESS;
163 }
164 
165 static char *cli_realtime_store(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
166 {
167  int res = -1;
168 
169  switch (cmd) {
170  case CLI_INIT:
171  e->command = "realtime store";
172  e->usage =
173  "Usage: realtime store <family> <colname1> <value1> [<colname2> <value2> [... <colname5> <value5>]]\n"
174  " Create a stored row using the RealTime driver.\n"
175  " You must supply a family name and name/value pairs (up to 5). If\n"
176  " you need to store more than 5 key/value pairs, start with the first\n"
177  " five, then use 'realtime update' or 'realtime update2' to add\n"
178  " additional columns.\n";
179  return NULL;
180  case CLI_GENERATE:
181  return NULL;
182  }
183 
184  if (a->argc < 5) {
185  return CLI_SHOWUSAGE;
186  } else if (a->argc == 5) {
187  res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
188  } else if (a->argc == 7) {
189  res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
190  } else if (a->argc == 9) {
191  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);
192  } else if (a->argc == 11) {
193  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);
194  } else if (a->argc == 13) {
195  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);
196  } else {
197  return CLI_SHOWUSAGE;
198  }
199 
200  if (res < 0) {
201  ast_cli(a->fd, "Failed to store record. Check the debug log for possible SQL related entries.\n");
202  return CLI_FAILURE;
203  }
204 
205  ast_cli(a->fd, "Stored RealTime record.\n");
206 
207  return CLI_SUCCESS;
208 }
209 
210 static char *cli_realtime_destroy(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
211 {
212  int res = -1;
213 
214  switch (cmd) {
215  case CLI_INIT:
216  e->command = "realtime destroy";
217  e->usage =
218  "Usage: realtime destroy <family> <colmatch1> <valuematch1> [<colmatch2> <valuematch2> [... <colmatch5> <valuematch5>]]\n"
219  " Remove a stored row using the RealTime driver.\n"
220  " You must supply a family name and name/value pairs (up to 5).\n";
221  return NULL;
222  case CLI_GENERATE:
223  return NULL;
224  }
225 
226  if (a->argc < 5) {
227  return CLI_SHOWUSAGE;
228  } else if (a->argc == 5) {
229  res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
230  } else if (a->argc == 7) {
231  res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
232  } else if (a->argc == 9) {
233  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);
234  } else if (a->argc == 11) {
235  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);
236  } else if (a->argc == 13) {
237  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);
238  } else {
239  return CLI_SHOWUSAGE;
240  }
241 
242  if (res < 0) {
243  ast_cli(a->fd, "Failed to remove record. Check the debug log for possible SQL related entries.\n");
244  return CLI_FAILURE;
245  }
246 
247  ast_cli(a->fd, "Removed %d RealTime record%s.\n", res, ESS(res));
248 
249  return CLI_SUCCESS;
250 }
251 
252 static struct ast_cli_entry cli_realtime[] = {
253  AST_CLI_DEFINE(cli_realtime_load, "Used to print out RealTime variables."),
254  AST_CLI_DEFINE(cli_realtime_update, "Used to update RealTime variables."),
255  AST_CLI_DEFINE(cli_realtime_update2, "Used to test the RealTime update2 method"),
256  AST_CLI_DEFINE(cli_realtime_store, "Store a new row into a RealTime database"),
257  AST_CLI_DEFINE(cli_realtime_destroy, "Delete a row from a RealTime database"),
258 };
259 
260 static int unload_module(void)
261 {
262  ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
263  return 0;
264 }
265 
266 static int load_module(void)
267 {
268  ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
270 }
271 
272 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
int ast_store_realtime(const char *family,...) attribute_sentinel
Create realtime configuration.
Definition: config.c:2726
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
descriptor for a cli entry.
Definition: cli.h:165
const int argc
Definition: cli.h:154
int ast_update2_realtime(const char *family,...) attribute_sentinel
Update realtime configuration.
Definition: config.c:2703
static char * cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_realtime.c:85
static struct ast_cli_entry cli_realtime[]
Definition: res_realtime.c:252
struct ast_variable * ast_load_realtime_all(const char *family,...) attribute_sentinel
Definition: config.c:2536
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
Definition: cli.h:146
static char * cli_realtime_destroy(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_realtime.c:210
Configuration File Parser.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: config.c:586
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
int ast_update_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Update realtime configuration.
Definition: config.c:2679
static char * cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_realtime.c:47
#define SENTINEL
Definition: compiler.h:75
static char * cli_realtime_store(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_realtime.c:165
const char * value
Definition: config.h:79
General Asterisk PBX channel definitions.
int ast_destroy_realtime(const char *family, const char *keyfield, const char *lookup,...) attribute_sentinel
Destroy realtime configuration.
Definition: config.c:2750
const int fd
Definition: cli.h:153
const char * name
Definition: config.h:77
Core PBX routines and definitions.
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
static char * cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_realtime.c:118
#define CLI_FAILURE
Definition: cli.h:45
#define ESS(x)
Definition: cli.h:58
char * command
Definition: cli.h:180
#define CRL_HEADER_FORMAT
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
Standard Command Line Interface.
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
struct ast_variable * next
Definition: config.h:82
static int load_module(void)
Definition: res_realtime.c:266
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
static int unload_module(void)
Definition: res_realtime.c:260