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;
409 static int require_curl(const char *url, const char *unused, va_list ap)
411 struct ast_str *query;
412 char *elm, field[256], buffer[128];
414 const int EncodeSpecialChars = 1;
416 if (!ast_custom_function_find("CURL")) {
417 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
421 if (!(query = ast_str_create(100))) {
425 ast_str_set(&query, 0, "${CURL(%s/require,", url);
427 while ((elm = va_arg(ap, char *))) {
428 type = va_arg(ap, require_type);
429 size = va_arg(ap, int);
430 ast_uri_encode(elm, field, sizeof(field), EncodeSpecialChars);
431 ast_str_append(&query, 0, "%s=%s%%3A%d", field,
432 type == RQ_CHAR ? "char" :
433 type == RQ_INTEGER1 ? "integer1" :
434 type == RQ_UINTEGER1 ? "uinteger1" :
435 type == RQ_INTEGER2 ? "integer2" :
436 type == RQ_UINTEGER2 ? "uinteger2" :
437 type == RQ_INTEGER3 ? "integer3" :
438 type == RQ_UINTEGER3 ? "uinteger3" :
439 type == RQ_INTEGER4 ? "integer4" :
440 type == RQ_UINTEGER4 ? "uinteger4" :
441 type == RQ_INTEGER8 ? "integer8" :
442 type == RQ_UINTEGER8 ? "uinteger8" :
443 type == RQ_DATE ? "date" :
444 type == RQ_DATETIME ? "datetime" :
445 type == RQ_FLOAT ? "float" :
450 ast_str_append(&query, 0, ")}");
451 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
455 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, const char *who_asked)
457 struct ast_str *query;
459 char *stringp, *line, *pair, *key;
460 const int EncodeSpecialChars = 1, bufsize = 256000;
461 int last_cat_metric = -1, cat_metric = -1;
462 struct ast_category *cat=NULL;
463 char *buffer, *cur_cat = "";
464 char *category = "", *var_name = "", *var_val = "";
465 struct ast_flags loader_flags = { 0 };
467 if (!ast_custom_function_find("CURL")) {
468 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
472 if (!(query = ast_str_create(1000)))
475 if (!(buffer = ast_malloc(bufsize))) {
480 ast_uri_encode(file, buf1, sizeof(buf1), EncodeSpecialChars);
481 ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1);
483 /* Do the CURL query */
484 pbx_substitute_variables_helper(NULL, query->str, buffer, bufsize);
486 /* Line oriented output */
488 cat = ast_config_get_current_category(cfg);
490 while ((line = strsep(&stringp, "\r\n"))) {
491 if (ast_strlen_zero(line))
494 while ((pair = strsep(&line, "&"))) {
495 key = strsep(&pair, "=");
498 ast_uri_decode(pair);
500 if (!strcasecmp(key, "category"))
501 category = S_OR(pair, "");
502 else if (!strcasecmp(key, "var_name"))
503 var_name = S_OR(pair, "");
504 else if (!strcasecmp(key, "var_val"))
505 var_val = S_OR(pair, "");
506 else if (!strcasecmp(key, "cat_metric"))
507 cat_metric = pair ? atoi(pair) : 0;
510 if (!strcmp(var_name, "#include")) {
511 if (!ast_config_internal_load(var_val, cfg, loader_flags, "", who_asked))
515 if (strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
516 if (!(cat = ast_category_new(category, "", 99999)))
519 last_cat_metric = cat_metric;
520 ast_category_append(cfg, cat);
522 ast_variable_append(cat, ast_variable_new(var_name, var_val, ""));
530 static struct ast_config_engine curl_engine = {
532 .load_func = config_curl,
533 .realtime_func = realtime_curl,
534 .realtime_multi_func = realtime_multi_curl,
535 .store_func = store_curl,
536 .destroy_func = destroy_curl,
537 .update_func = update_curl,
538 .require_func = require_curl,
541 static int unload_module(void)
543 ast_config_engine_deregister(&curl_engine);
544 ast_verb(1, "res_config_curl unloaded.\n");
548 static int load_module(void)
550 if (!ast_module_check("res_curl.so")) {
551 if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
552 ast_log(LOG_ERROR, "Cannot load res_curl, so res_config_curl cannot be loaded\n");
553 return AST_MODULE_LOAD_DECLINE;
557 ast_config_engine_register(&curl_engine);
558 ast_verb(1, "res_config_curl loaded.\n");
562 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Curl configuration");