2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Tilghman Lesher <res_config_curl_v1@the-tilghman.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief curl plugin for portable configuration engine
23 * \author Tilghman Lesher <res_config_curl_v1@the-tilghman.com>
25 * \extref Depends on the CURL library - http://curl.haxx.se/
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <curl/curl.h>
39 #include "asterisk/file.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/config.h"
43 #include "asterisk/module.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/utils.h"
48 * \brief Execute a curl query and return ast_variable list
49 * \param url The base URL from which to retrieve data
50 * \param unused Not currently used
51 * \param ap list containing one or more field/operator/value set.
53 * \retval var on success
54 * \retval NULL on failure
56 static struct ast_variable *realtime_curl(const char *url, const char *unused, va_list ap)
58 struct ast_str *query;
59 char buf1[200], buf2[200];
60 const char *newparam, *newval;
61 char *stringp, *pair, *key;
63 struct ast_variable *var=NULL, *prev=NULL;
64 const int EncodeSpecialChars = 1, bufsize = 64000;
67 if (!ast_custom_function_find("CURL")) {
68 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
72 if (!(query = ast_str_create(1000)))
75 if (!(buffer = ast_malloc(bufsize))) {
80 ast_str_set(&query, 0, "${CURL(%s/single,", url);
82 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
83 newval = va_arg(ap, const char *);
84 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
85 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
86 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
90 ast_str_append(&query, 0, ")}");
91 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
93 /* Remove any trailing newline characters */
94 if ((stringp = strchr(buffer, '\r')) || (stringp = strchr(buffer, '\n')))
98 while ((pair = strsep(&stringp, "&"))) {
99 key = strsep(&pair, "=");
102 ast_uri_decode(pair);
104 if (!ast_strlen_zero(key)) {
106 prev->next = ast_variable_new(key, S_OR(pair, ""), "");
110 prev = var = ast_variable_new(key, S_OR(pair, ""), "");
120 * \brief Excute an Select query and return ast_config list
123 * \param ap list containing one or more field/operator/value set.
125 * \retval struct ast_config pointer on success
126 * \retval NULL on failure
128 static struct ast_config *realtime_multi_curl(const char *url, const char *unused, va_list ap)
130 struct ast_str *query;
131 char buf1[200], buf2[200];
132 const char *newparam, *newval;
133 char *stringp, *line, *pair, *key, *initfield = NULL;
135 const int EncodeSpecialChars = 1, bufsize = 256000;
136 struct ast_variable *var=NULL;
137 struct ast_config *cfg=NULL;
138 struct ast_category *cat=NULL;
141 if (!ast_custom_function_find("CURL")) {
142 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
146 if (!(query = ast_str_create(1000)))
149 if (!(buffer = ast_malloc(bufsize))) {
154 ast_str_set(&query, 0, "${CURL(%s/multi,", url);
156 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
157 newval = va_arg(ap, const char *);
160 initfield = ast_strdupa(newparam);
161 if ((op = strchr(initfield, ' ')))
164 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
165 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
166 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
170 ast_str_append(&query, 0, ")}");
172 /* Do the CURL query */
173 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
175 if (!(cfg = ast_config_new()))
178 /* Line oriented output */
180 while ((line = strsep(&stringp, "\r\n"))) {
181 if (ast_strlen_zero(line))
184 if (!(cat = ast_category_new("", "", 99999)))
187 while ((pair = strsep(&line, "&"))) {
188 key = strsep(&pair, "=");
191 ast_uri_decode(pair);
193 if (!strcasecmp(key, initfield) && pair)
194 ast_category_rename(cat, pair);
196 if (!ast_strlen_zero(key)) {
197 var = ast_variable_new(key, S_OR(pair, ""), "");
198 ast_variable_append(cat, var);
201 ast_category_append(cfg, cat);
211 * \brief Execute an UPDATE query
214 * \param keyfield where clause field
215 * \param lookup value of field for where clause
216 * \param ap list containing one or more field/value set(s).
218 * Update a database table, prepare the sql statement using keyfield and lookup
219 * control the number of records to change. All values to be changed are stored in ap list.
220 * Sub-in the values to the prepared statement and execute it.
222 * \retval number of rows affected
223 * \retval -1 on failure
225 static int update_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
227 struct ast_str *query;
228 char buf1[200], buf2[200];
229 const char *newparam, *newval;
231 int i, rowcount = -1;
232 const int EncodeSpecialChars = 1, bufsize = 100;
235 if (!ast_custom_function_find("CURL")) {
236 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
240 if (!(query = ast_str_create(1000)))
243 if (!(buffer = ast_malloc(bufsize))) {
248 ast_uri_encode(keyfield, buf1, sizeof(buf1), EncodeSpecialChars);
249 ast_uri_encode(lookup, buf2, sizeof(buf2), EncodeSpecialChars);
250 ast_str_set(&query, 0, "${CURL(%s/update?%s=%s,", url, buf1, buf2);
252 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
253 newval = va_arg(ap, const char *);
254 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
255 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
256 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
260 ast_str_append(&query, 0, ")}");
261 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
263 /* Line oriented output */
265 while (*stringp <= ' ')
267 sscanf(stringp, "%d", &rowcount);
273 return (int)rowcount;
279 * \brief Execute an INSERT query
282 * \param ap list containing one or more field/value set(s)
284 * Insert a new record into database table, prepare the sql statement.
285 * All values to be changed are stored in ap list.
286 * Sub-in the values to the prepared statement and execute it.
288 * \retval number of rows affected
289 * \retval -1 on failure
291 static int store_curl(const char *url, const char *unused, va_list ap)
293 struct ast_str *query;
294 char buf1[200], buf2[200];
295 const char *newparam, *newval;
297 int i, rowcount = -1;
298 const int EncodeSpecialChars = 1, bufsize = 100;
301 if (!ast_custom_function_find("CURL")) {
302 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
306 if (!(query = ast_str_create(1000)))
309 if (!(buffer = ast_malloc(bufsize))) {
314 ast_str_set(&query, 0, "${CURL(%s/store,", url);
316 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
317 newval = va_arg(ap, const char *);
318 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
319 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
320 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
324 ast_str_append(&query, 0, ")}");
325 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
328 while (*stringp <= ' ')
330 sscanf(stringp, "%d", &rowcount);
336 return (int)rowcount;
342 * \brief Execute an DELETE query
345 * \param keyfield where clause field
346 * \param lookup value of field for where clause
347 * \param ap list containing one or more field/value set(s)
349 * Delete a row from a database table, prepare the sql statement using keyfield and lookup
350 * control the number of records to change. Additional params to match rows are stored in ap list.
351 * Sub-in the values to the prepared statement and execute it.
353 * \retval number of rows affected
354 * \retval -1 on failure
356 static int destroy_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
358 struct ast_str *query;
359 char buf1[200], buf2[200];
360 const char *newparam, *newval;
362 int i, rowcount = -1;
363 const int EncodeSpecialChars = 1, bufsize = 100;
366 if (!ast_custom_function_find("CURL")) {
367 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
371 if (!(query = ast_str_create(1000)))
374 if (!(buffer = ast_malloc(bufsize))) {
379 ast_uri_encode(keyfield, buf1, sizeof(buf1), EncodeSpecialChars);
380 ast_uri_encode(lookup, buf2, sizeof(buf2), EncodeSpecialChars);
381 ast_str_set(&query, 0, "${CURL(%s/destroy,%s=%s&", url, buf1, buf2);
383 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
384 newval = va_arg(ap, const char *);
385 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
386 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
387 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
391 ast_str_append(&query, 0, ")}");
392 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
394 /* Line oriented output */
396 while (*stringp <= ' ')
398 sscanf(stringp, "%d", &rowcount);
404 return (int)rowcount;
410 static struct ast_config *config_curl(const char *url, const char *unused, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl)
412 struct ast_str *query;
414 char *stringp, *line, *pair, *key;
415 const int EncodeSpecialChars = 1, bufsize = 256000;
416 int last_cat_metric = -1, cat_metric = -1;
417 struct ast_category *cat=NULL;
418 char *buffer, *cur_cat = "";
419 char *category = "", *var_name = "", *var_val = "";
420 struct ast_flags loader_flags = { 0 };
422 if (!ast_custom_function_find("CURL")) {
423 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
427 if (!(query = ast_str_create(1000)))
430 if (!(buffer = ast_malloc(bufsize))) {
435 ast_uri_encode(file, buf1, sizeof(buf1), EncodeSpecialChars);
436 ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1);
438 /* Do the CURL query */
439 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
441 /* Line oriented output */
443 cat = ast_config_get_current_category(cfg);
445 while ((line = strsep(&stringp, "\r\n"))) {
446 if (ast_strlen_zero(line))
449 while ((pair = strsep(&line, "&"))) {
450 key = strsep(&pair, "=");
453 ast_uri_decode(pair);
455 if (!strcasecmp(key, "category"))
456 category = S_OR(pair, "");
457 else if (!strcasecmp(key, "var_name"))
458 var_name = S_OR(pair, "");
459 else if (!strcasecmp(key, "var_val"))
460 var_val = S_OR(pair, "");
461 else if (!strcasecmp(key, "cat_metric"))
462 cat_metric = pair ? atoi(pair) : 0;
465 if (!strcmp(var_name, "#include")) {
466 if (!ast_config_internal_load(var_val, cfg, loader_flags, ""))
470 if (strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
471 if (!(cat = ast_category_new(category, "", 99999)))
474 last_cat_metric = cat_metric;
475 ast_category_append(cfg, cat);
477 ast_variable_append(cat, ast_variable_new(var_name, var_val, ""));
485 static struct ast_config_engine curl_engine = {
487 .load_func = config_curl,
488 .realtime_func = realtime_curl,
489 .realtime_multi_func = realtime_multi_curl,
490 .store_func = store_curl,
491 .destroy_func = destroy_curl,
492 .update_func = update_curl
495 static int unload_module (void)
497 ast_config_engine_deregister(&curl_engine);
498 ast_verb(1, "res_config_curl unloaded.\n");
502 static int load_module (void)
504 ast_config_engine_register(&curl_engine);
505 ast_verb(1, "res_config_curl loaded.\n");
509 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Curl configuration");