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;
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(64000))) {
80 ast_str_set(&query, 0, "${CURL(%s,", 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, sizeof(buffer));
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;
134 int i, EncodeSpecialChars = 1;
135 struct ast_variable *var=NULL;
136 struct ast_config *cfg=NULL;
137 struct ast_category *cat=NULL;
140 if (!ast_custom_function_find("CURL")) {
141 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
145 if (!(query = ast_str_create(1000)))
148 if (!(buffer = ast_malloc(256000))) {
153 ast_str_set(&query, 0, "${CURL(%s/multi,", url);
155 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
156 newval = va_arg(ap, const char *);
158 initfield = ast_strdupa(newparam);
159 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
160 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
161 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
165 ast_str_append(&query, 0, ")}");
167 /* Do the CURL query */
168 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
170 if (!(cfg = ast_config_new()))
173 /* Line oriented output */
175 while ((line = strsep(&stringp, "\r\n"))) {
176 if (ast_strlen_zero(line))
179 if (!(cat = ast_category_new("", "", 99999)))
182 while ((pair = strsep(&line, "&"))) {
183 key = strsep(&pair, "=");
186 ast_uri_decode(pair);
188 if (!strcasecmp(key, initfield) && pair)
189 ast_category_rename(cat, pair);
191 if (!ast_strlen_zero(key)) {
192 var = ast_variable_new(key, S_OR(pair, ""), "");
193 ast_variable_append(cat, var);
196 ast_category_append(cfg, cat);
206 * \brief Execute an UPDATE query
209 * \param keyfield where clause field
210 * \param lookup value of field for where clause
211 * \param ap list containing one or more field/value set(s).
213 * Update a database table, prepare the sql statement using keyfield and lookup
214 * control the number of records to change. All values to be changed are stored in ap list.
215 * Sub-in the values to the prepared statement and execute it.
217 * \retval number of rows affected
218 * \retval -1 on failure
220 static int update_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
222 struct ast_str *query;
223 char buf1[200], buf2[200];
224 const char *newparam, *newval;
226 int i, rowcount = -1;
227 const int EncodeSpecialChars = 1;
230 if (!ast_custom_function_find("CURL")) {
231 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
235 if (!(query = ast_str_create(1000)))
238 if (!(buffer = ast_malloc(100))) {
243 ast_uri_encode(keyfield, buf1, sizeof(buf1), EncodeSpecialChars);
244 ast_uri_encode(lookup, buf2, sizeof(buf2), EncodeSpecialChars);
245 ast_str_set(&query, 0, "${CURL(%s/update?%s=%s,", url, buf1, buf2);
247 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
248 newval = va_arg(ap, const char *);
249 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
250 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
251 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
255 ast_str_append(&query, 0, ")}");
256 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
258 /* Line oriented output */
260 while (*stringp <= ' ')
262 sscanf(stringp, "%d", &rowcount);
268 return (int)rowcount;
274 * \brief Execute an INSERT query
277 * \param ap list containing one or more field/value set(s)
279 * Insert a new record into database table, prepare the sql statement.
280 * All values to be changed are stored in ap list.
281 * Sub-in the values to the prepared statement and execute it.
283 * \retval number of rows affected
284 * \retval -1 on failure
286 static int store_curl(const char *url, const char *unused, va_list ap)
288 struct ast_str *query;
289 char buf1[200], buf2[200];
290 const char *newparam, *newval;
292 int i, rowcount = -1;
293 const int EncodeSpecialChars = 1;
296 if (!ast_custom_function_find("CURL")) {
297 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
301 if (!(query = ast_str_create(1000)))
304 if (!(buffer = ast_malloc(100))) {
309 ast_str_set(&query, 0, "${CURL(%s/store,", url);
311 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
312 newval = va_arg(ap, const char *);
313 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
314 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
315 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
319 ast_str_append(&query, 0, ")}");
320 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
323 while (*stringp <= ' ')
325 sscanf(stringp, "%d", &rowcount);
331 return (int)rowcount;
337 * \brief Execute an DELETE query
340 * \param keyfield where clause field
341 * \param lookup value of field for where clause
342 * \param ap list containing one or more field/value set(s)
344 * Delete a row from a database table, prepare the sql statement using keyfield and lookup
345 * control the number of records to change. Additional params to match rows are stored in ap list.
346 * Sub-in the values to the prepared statement and execute it.
348 * \retval number of rows affected
349 * \retval -1 on failure
351 static int destroy_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
353 struct ast_str *query;
354 char buf1[200], buf2[200];
355 const char *newparam, *newval;
357 int i, rowcount = -1;
358 const int EncodeSpecialChars = 1;
361 if (!ast_custom_function_find("CURL")) {
362 ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
366 if (!(query = ast_str_create(1000)))
369 if (!(buffer = ast_malloc(100))) {
374 ast_uri_encode(keyfield, buf1, sizeof(buf1), EncodeSpecialChars);
375 ast_uri_encode(lookup, buf2, sizeof(buf2), EncodeSpecialChars);
376 ast_str_set(&query, 0, "${CURL(%s/destroy,%s=%s&", url, buf1, buf2);
378 for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
379 newval = va_arg(ap, const char *);
380 ast_uri_encode(newparam, buf1, sizeof(buf1), EncodeSpecialChars);
381 ast_uri_encode(newval, buf2, sizeof(buf2), EncodeSpecialChars);
382 ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
386 ast_str_append(&query, 0, ")}");
387 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
389 /* Line oriented output */
391 while (*stringp <= ' ')
393 sscanf(stringp, "%d", &rowcount);
399 return (int)rowcount;
405 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)
407 struct ast_str *query;
409 char *stringp, *line, *pair, *key;
410 int EncodeSpecialChars = 1, last_cat_metric = -1, cat_metric;
411 struct ast_category *cat=NULL;
412 char *buffer, *cur_cat = "";
413 char *category = "", *var_name = "", *var_val = "";
414 struct ast_flags loader_flags = { 0 };
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(1000)))
424 if (!(buffer = ast_malloc(256000))) {
429 ast_uri_encode(file, buf1, sizeof(buf1), EncodeSpecialChars);
430 ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1);
432 /* Do the CURL query */
433 pbx_substitute_variables_helper(NULL, query->str, buffer, sizeof(buffer));
435 /* Line oriented output */
437 cat = ast_config_get_current_category(cfg);
439 while ((line = strsep(&stringp, "\r\n"))) {
440 if (ast_strlen_zero(line))
443 while ((pair = strsep(&line, "&"))) {
444 key = strsep(&pair, "=");
447 ast_uri_decode(pair);
449 if (!strcasecmp(key, "category"))
450 category = S_OR(pair, "");
451 else if (!strcasecmp(key, "var_name"))
452 var_name = S_OR(pair, "");
453 else if (!strcasecmp(key, "var_val"))
454 var_val = S_OR(pair, "");
455 else if (!strcasecmp(key, "cat_metric"))
456 cat_metric = pair ? atoi(pair) : 0;
459 if (!strcmp(var_name, "#include")) {
460 if (!ast_config_internal_load(var_val, cfg, loader_flags, ""))
464 if (strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
465 if (!(cat = ast_category_new(category, "", 99999)))
468 last_cat_metric = cat_metric;
469 ast_category_append(cfg, cat);
471 ast_variable_append(cat, ast_variable_new(var_name, var_val, ""));
479 static struct ast_config_engine curl_engine = {
481 .load_func = config_curl,
482 .realtime_func = realtime_curl,
483 .realtime_multi_func = realtime_multi_curl,
484 .store_func = store_curl,
485 .destroy_func = destroy_curl,
486 .update_func = update_curl
489 static int unload_module (void)
491 ast_config_engine_deregister(&curl_engine);
492 ast_verb(1, "res_config_curl unloaded.\n");
496 static int load_module (void)
498 ast_config_engine_register(&curl_engine);
499 ast_verb(1, "res_config_curl loaded.\n");
503 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Curl configuration");