res_config_curl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Tilghman Lesher <res_config_curl_v1@the-tilghman.com>
  7. *
  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.
  13. *
  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.
  17. */
  18. /*! \file
  19. *
  20. * \brief curl plugin for portable configuration engine
  21. *
  22. * \author Tilghman Lesher <res_config_curl_v1@the-tilghman.com>
  23. *
  24. * \extref Depends on the CURL library - http://curl.haxx.se/
  25. *
  26. */
  27. /*** MODULEINFO
  28. <depend>curl</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <curl/curl.h>
  34. #include "asterisk/file.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/config.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/lock.h"
  40. #include "asterisk/utils.h"
  41. #include "asterisk/threadstorage.h"
  42. AST_THREADSTORAGE(query_buf);
  43. AST_THREADSTORAGE(result_buf);
  44. /*!
  45. * \brief Execute a curl query and return ast_variable list
  46. * \param url The base URL from which to retrieve data
  47. * \param unused Not currently used
  48. * \param ap list containing one or more field/operator/value set.
  49. *
  50. * \retval var on success
  51. * \retval NULL on failure
  52. */
  53. static struct ast_variable *realtime_curl(const char *url, const char *unused, va_list ap)
  54. {
  55. struct ast_str *query, *buffer;
  56. char buf1[256], buf2[256];
  57. const char *newparam, *newval;
  58. char *stringp, *pair, *key;
  59. int i;
  60. struct ast_variable *var = NULL, *prev = NULL;
  61. if (!ast_custom_function_find("CURL")) {
  62. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  63. return NULL;
  64. }
  65. if (!(query = ast_str_thread_get(&query_buf, 16))) {
  66. return NULL;
  67. }
  68. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  69. return NULL;
  70. }
  71. ast_str_set(&query, 0, "${CURL(%s/single,", url);
  72. for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
  73. newval = va_arg(ap, const char *);
  74. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  75. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  76. ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
  77. }
  78. va_end(ap);
  79. ast_str_append(&query, 0, ")}");
  80. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  81. /* Remove any trailing newline characters */
  82. if ((stringp = strchr(ast_str_buffer(buffer), '\r')) || (stringp = strchr(ast_str_buffer(buffer), '\n'))) {
  83. *stringp = '\0';
  84. }
  85. stringp = ast_str_buffer(buffer);
  86. while ((pair = strsep(&stringp, "&"))) {
  87. key = strsep(&pair, "=");
  88. ast_uri_decode(key, ast_uri_http);
  89. if (pair) {
  90. ast_uri_decode(pair, ast_uri_http);
  91. }
  92. if (!ast_strlen_zero(key)) {
  93. if (prev) {
  94. prev->next = ast_variable_new(key, S_OR(pair, ""), "");
  95. if (prev->next) {
  96. prev = prev->next;
  97. }
  98. } else {
  99. prev = var = ast_variable_new(key, S_OR(pair, ""), "");
  100. }
  101. }
  102. }
  103. return var;
  104. }
  105. /*!
  106. * \brief Excute an Select query and return ast_config list
  107. * \param url
  108. * \param unused
  109. * \param ap list containing one or more field/operator/value set.
  110. *
  111. * \retval struct ast_config pointer on success
  112. * \retval NULL on failure
  113. */
  114. static struct ast_config *realtime_multi_curl(const char *url, const char *unused, va_list ap)
  115. {
  116. struct ast_str *query, *buffer;
  117. char buf1[256], buf2[256];
  118. const char *newparam, *newval;
  119. char *stringp, *line, *pair, *key, *initfield = NULL;
  120. int i;
  121. struct ast_variable *var = NULL;
  122. struct ast_config *cfg = NULL;
  123. struct ast_category *cat = NULL;
  124. if (!ast_custom_function_find("CURL")) {
  125. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  126. return NULL;
  127. }
  128. if (!(query = ast_str_thread_get(&query_buf, 16))) {
  129. return NULL;
  130. }
  131. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  132. return NULL;
  133. }
  134. ast_str_set(&query, 0, "${CURL(%s/multi,", url);
  135. for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
  136. newval = va_arg(ap, const char *);
  137. if (i == 0) {
  138. char *op;
  139. initfield = ast_strdupa(newparam);
  140. if ((op = strchr(initfield, ' ')))
  141. *op = '\0';
  142. }
  143. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  144. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  145. ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
  146. }
  147. va_end(ap);
  148. ast_str_append(&query, 0, ")}");
  149. /* Do the CURL query */
  150. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  151. if (!(cfg = ast_config_new())) {
  152. return NULL;
  153. }
  154. /* Line oriented output */
  155. stringp = ast_str_buffer(buffer);
  156. while ((line = strsep(&stringp, "\r\n"))) {
  157. if (ast_strlen_zero(line)) {
  158. continue;
  159. }
  160. if (!(cat = ast_category_new("", "", 99999))) {
  161. continue;
  162. }
  163. while ((pair = strsep(&line, "&"))) {
  164. key = strsep(&pair, "=");
  165. ast_uri_decode(key, ast_uri_http);
  166. if (pair) {
  167. ast_uri_decode(pair, ast_uri_http);
  168. }
  169. if (!strcasecmp(key, initfield) && pair) {
  170. ast_category_rename(cat, pair);
  171. }
  172. if (!ast_strlen_zero(key)) {
  173. var = ast_variable_new(key, S_OR(pair, ""), "");
  174. ast_variable_append(cat, var);
  175. }
  176. }
  177. ast_category_append(cfg, cat);
  178. }
  179. return cfg;
  180. }
  181. /*!
  182. * \brief Execute an UPDATE query
  183. * \param url
  184. * \param unused
  185. * \param keyfield where clause field
  186. * \param lookup value of field for where clause
  187. * \param ap list containing one or more field/value set(s).
  188. *
  189. * Update a database table, prepare the sql statement using keyfield and lookup
  190. * control the number of records to change. All values to be changed are stored in ap list.
  191. * Sub-in the values to the prepared statement and execute it.
  192. *
  193. * \retval number of rows affected
  194. * \retval -1 on failure
  195. */
  196. static int update_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
  197. {
  198. struct ast_str *query, *buffer;
  199. char buf1[256], buf2[256];
  200. const char *newparam, *newval;
  201. char *stringp;
  202. int i, rowcount = -1;
  203. if (!ast_custom_function_find("CURL")) {
  204. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  205. return -1;
  206. }
  207. if (!(query = ast_str_thread_get(&query_buf, 16))) {
  208. return -1;
  209. }
  210. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  211. return -1;
  212. }
  213. ast_uri_encode(keyfield, buf1, sizeof(buf1), ast_uri_http);
  214. ast_uri_encode(lookup, buf2, sizeof(buf2), ast_uri_http);
  215. ast_str_set(&query, 0, "${CURL(%s/update?%s=%s,", url, buf1, buf2);
  216. for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
  217. newval = va_arg(ap, const char *);
  218. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  219. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  220. ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
  221. }
  222. va_end(ap);
  223. ast_str_append(&query, 0, ")}");
  224. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  225. /* Line oriented output */
  226. stringp = ast_str_buffer(buffer);
  227. while (*stringp <= ' ') {
  228. stringp++;
  229. }
  230. sscanf(stringp, "%30d", &rowcount);
  231. if (rowcount >= 0) {
  232. return (int)rowcount;
  233. }
  234. return -1;
  235. }
  236. static int update2_curl(const char *url, const char *unused, va_list ap)
  237. {
  238. struct ast_str *query, *buffer;
  239. char buf1[200], buf2[200];
  240. const char *newparam, *newval;
  241. char *stringp;
  242. int rowcount = -1, lookup = 1, first = 1;
  243. if (!ast_custom_function_find("CURL")) {
  244. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  245. return -1;
  246. }
  247. if (!(query = ast_str_thread_get(&query_buf, 1000)))
  248. return -1;
  249. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  250. return -1;
  251. }
  252. ast_str_set(&query, 0, "${CURL(%s/update?", url);
  253. for (;;) {
  254. if ((newparam = va_arg(ap, const char *)) == SENTINEL) {
  255. if (lookup) {
  256. lookup = 0;
  257. ast_str_append(&query, 0, ",");
  258. /* Back to the first parameter; we don't need a starting '&' */
  259. first = 1;
  260. continue;
  261. } else {
  262. break;
  263. }
  264. }
  265. newval = va_arg(ap, const char *);
  266. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  267. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  268. ast_str_append(&query, 0, "%s%s=%s", first ? "" : "&", buf1, buf2);
  269. first = 0;
  270. }
  271. va_end(ap);
  272. ast_str_append(&query, 0, ")}");
  273. /* Proxies work, by setting CURLOPT options in the [globals] section of
  274. * extensions.conf. Unfortunately, this means preloading pbx_config.so
  275. * so that they have an opportunity to be set prior to startup realtime
  276. * queries. */
  277. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  278. /* Line oriented output */
  279. stringp = ast_str_buffer(buffer);
  280. while (*stringp <= ' ') {
  281. stringp++;
  282. }
  283. sscanf(stringp, "%30d", &rowcount);
  284. if (rowcount >= 0) {
  285. return (int)rowcount;
  286. }
  287. return -1;
  288. }
  289. /*!
  290. * \brief Execute an INSERT query
  291. * \param url
  292. * \param unused
  293. * \param ap list containing one or more field/value set(s)
  294. *
  295. * Insert a new record into database table, prepare the sql statement.
  296. * All values to be changed are stored in ap list.
  297. * Sub-in the values to the prepared statement and execute it.
  298. *
  299. * \retval number of rows affected
  300. * \retval -1 on failure
  301. */
  302. static int store_curl(const char *url, const char *unused, va_list ap)
  303. {
  304. struct ast_str *query, *buffer;
  305. char buf1[256], buf2[256];
  306. const char *newparam, *newval;
  307. char *stringp;
  308. int i, rowcount = -1;
  309. if (!ast_custom_function_find("CURL")) {
  310. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  311. return -1;
  312. }
  313. if (!(query = ast_str_thread_get(&query_buf, 1000))) {
  314. return -1;
  315. }
  316. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  317. return -1;
  318. }
  319. ast_str_set(&query, 0, "${CURL(%s/store,", url);
  320. for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
  321. newval = va_arg(ap, const char *);
  322. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  323. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  324. ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
  325. }
  326. va_end(ap);
  327. ast_str_append(&query, 0, ")}");
  328. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  329. stringp = ast_str_buffer(buffer);
  330. while (*stringp <= ' ') {
  331. stringp++;
  332. }
  333. sscanf(stringp, "%30d", &rowcount);
  334. if (rowcount >= 0) {
  335. return rowcount;
  336. }
  337. return -1;
  338. }
  339. /*!
  340. * \brief Execute an DELETE query
  341. * \param url
  342. * \param unused
  343. * \param keyfield where clause field
  344. * \param lookup value of field for where clause
  345. * \param ap list containing one or more field/value set(s)
  346. *
  347. * Delete a row from a database table, prepare the sql statement using keyfield and lookup
  348. * control the number of records to change. Additional params to match rows are stored in ap list.
  349. * Sub-in the values to the prepared statement and execute it.
  350. *
  351. * \retval number of rows affected
  352. * \retval -1 on failure
  353. */
  354. static int destroy_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, va_list ap)
  355. {
  356. struct ast_str *query, *buffer;
  357. char buf1[200], buf2[200];
  358. const char *newparam, *newval;
  359. char *stringp;
  360. int i, rowcount = -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");
  363. return -1;
  364. }
  365. if (!(query = ast_str_thread_get(&query_buf, 1000))) {
  366. return -1;
  367. }
  368. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  369. return -1;
  370. }
  371. ast_uri_encode(keyfield, buf1, sizeof(buf1), ast_uri_http);
  372. ast_uri_encode(lookup, buf2, sizeof(buf2), ast_uri_http);
  373. ast_str_set(&query, 0, "${CURL(%s/destroy,%s=%s&", url, buf1, buf2);
  374. for (i = 0; (newparam = va_arg(ap, const char *)); i++) {
  375. newval = va_arg(ap, const char *);
  376. ast_uri_encode(newparam, buf1, sizeof(buf1), ast_uri_http);
  377. ast_uri_encode(newval, buf2, sizeof(buf2), ast_uri_http);
  378. ast_str_append(&query, 0, "%s%s=%s", i > 0 ? "&" : "", buf1, buf2);
  379. }
  380. va_end(ap);
  381. ast_str_append(&query, 0, ")}");
  382. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  383. /* Line oriented output */
  384. stringp = ast_str_buffer(buffer);
  385. while (*stringp <= ' ') {
  386. stringp++;
  387. }
  388. sscanf(stringp, "%30d", &rowcount);
  389. if (rowcount >= 0) {
  390. return (int)rowcount;
  391. }
  392. return -1;
  393. }
  394. static int require_curl(const char *url, const char *unused, va_list ap)
  395. {
  396. struct ast_str *query, *buffer;
  397. char *elm, field[256];
  398. int type, size;
  399. if (!ast_custom_function_find("CURL")) {
  400. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  401. return -1;
  402. }
  403. if (!(query = ast_str_thread_get(&query_buf, 100))) {
  404. return -1;
  405. }
  406. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  407. return -1;
  408. }
  409. ast_str_set(&query, 0, "${CURL(%s/require,", url);
  410. while ((elm = va_arg(ap, char *))) {
  411. type = va_arg(ap, require_type);
  412. size = va_arg(ap, int);
  413. ast_uri_encode(elm, field, sizeof(field), ast_uri_http);
  414. ast_str_append(&query, 0, "%s=%s%%3A%d", field,
  415. type == RQ_CHAR ? "char" :
  416. type == RQ_INTEGER1 ? "integer1" :
  417. type == RQ_UINTEGER1 ? "uinteger1" :
  418. type == RQ_INTEGER2 ? "integer2" :
  419. type == RQ_UINTEGER2 ? "uinteger2" :
  420. type == RQ_INTEGER3 ? "integer3" :
  421. type == RQ_UINTEGER3 ? "uinteger3" :
  422. type == RQ_INTEGER4 ? "integer4" :
  423. type == RQ_UINTEGER4 ? "uinteger4" :
  424. type == RQ_INTEGER8 ? "integer8" :
  425. type == RQ_UINTEGER8 ? "uinteger8" :
  426. type == RQ_DATE ? "date" :
  427. type == RQ_DATETIME ? "datetime" :
  428. type == RQ_FLOAT ? "float" :
  429. "unknown", size);
  430. }
  431. va_end(ap);
  432. ast_str_append(&query, 0, ")}");
  433. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  434. return atoi(ast_str_buffer(buffer));
  435. }
  436. 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)
  437. {
  438. struct ast_str *query, *buffer;
  439. char buf1[200];
  440. char *stringp, *line, *pair, *key;
  441. int last_cat_metric = -1, cat_metric = -1;
  442. struct ast_category *cat = NULL;
  443. char *cur_cat = "";
  444. char *category = "", *var_name = "", *var_val = "";
  445. struct ast_flags loader_flags = { 0 };
  446. if (!ast_custom_function_find("CURL")) {
  447. ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n");
  448. return NULL;
  449. }
  450. if (!(query = ast_str_thread_get(&query_buf, 100))) {
  451. return NULL;
  452. }
  453. if (!(buffer = ast_str_thread_get(&result_buf, 16))) {
  454. return NULL;
  455. }
  456. ast_uri_encode(file, buf1, sizeof(buf1), ast_uri_http);
  457. ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1);
  458. /* Do the CURL query */
  459. ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query));
  460. /* Line oriented output */
  461. stringp = ast_str_buffer(buffer);
  462. cat = ast_config_get_current_category(cfg);
  463. while ((line = strsep(&stringp, "\r\n"))) {
  464. if (ast_strlen_zero(line)) {
  465. continue;
  466. }
  467. while ((pair = strsep(&line, "&"))) {
  468. key = strsep(&pair, "=");
  469. ast_uri_decode(key, ast_uri_http);
  470. if (pair) {
  471. ast_uri_decode(pair, ast_uri_http);
  472. }
  473. if (!strcasecmp(key, "category")) {
  474. category = S_OR(pair, "");
  475. } else if (!strcasecmp(key, "var_name")) {
  476. var_name = S_OR(pair, "");
  477. } else if (!strcasecmp(key, "var_val")) {
  478. var_val = S_OR(pair, "");
  479. } else if (!strcasecmp(key, "cat_metric")) {
  480. cat_metric = pair ? atoi(pair) : 0;
  481. }
  482. }
  483. if (!strcmp(var_name, "#include")) {
  484. if (!ast_config_internal_load(var_val, cfg, loader_flags, "", who_asked))
  485. return NULL;
  486. }
  487. if (!cat || strcmp(category, cur_cat) || last_cat_metric != cat_metric) {
  488. if (!(cat = ast_category_new(category, "", 99999)))
  489. break;
  490. cur_cat = category;
  491. last_cat_metric = cat_metric;
  492. ast_category_append(cfg, cat);
  493. }
  494. ast_variable_append(cat, ast_variable_new(var_name, var_val, ""));
  495. }
  496. return cfg;
  497. }
  498. static struct ast_config_engine curl_engine = {
  499. .name = "curl",
  500. .load_func = config_curl,
  501. .realtime_func = realtime_curl,
  502. .realtime_multi_func = realtime_multi_curl,
  503. .store_func = store_curl,
  504. .destroy_func = destroy_curl,
  505. .update_func = update_curl,
  506. .update2_func = update2_curl,
  507. .require_func = require_curl,
  508. };
  509. static int reload_module(void)
  510. {
  511. struct ast_flags flags = { CONFIG_FLAG_NOREALTIME };
  512. struct ast_config *cfg;
  513. struct ast_variable *var;
  514. if (!(cfg = ast_config_load("res_curl.conf", flags))) {
  515. return 0;
  516. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  517. ast_log(LOG_WARNING, "res_curl.conf could not be parsed!\n");
  518. return 0;
  519. }
  520. if (!(var = ast_variable_browse(cfg, "globals")) && !(var = ast_variable_browse(cfg, "global")) && !(var = ast_variable_browse(cfg, "general"))) {
  521. ast_log(LOG_WARNING, "[globals] not found in res_curl.conf\n");
  522. ast_config_destroy(cfg);
  523. return 0;
  524. }
  525. for (; var; var = var->next) {
  526. if (strncmp(var->name, "CURLOPT(", 8)) {
  527. char name[256];
  528. snprintf(name, sizeof(name), "CURLOPT(%s)", var->name);
  529. pbx_builtin_setvar_helper(NULL, name, var->value);
  530. } else {
  531. pbx_builtin_setvar_helper(NULL, var->name, var->value);
  532. }
  533. }
  534. ast_config_destroy(cfg);
  535. return 0;
  536. }
  537. static int unload_module(void)
  538. {
  539. ast_config_engine_deregister(&curl_engine);
  540. ast_verb(1, "res_config_curl unloaded.\n");
  541. return 0;
  542. }
  543. static int load_module(void)
  544. {
  545. if (!ast_module_check("res_curl.so")) {
  546. if (ast_load_resource("res_curl.so") != AST_MODULE_LOAD_SUCCESS) {
  547. ast_log(LOG_ERROR, "Cannot load res_curl, so res_config_curl cannot be loaded\n");
  548. return AST_MODULE_LOAD_DECLINE;
  549. }
  550. }
  551. if (!ast_module_check("func_curl.so")) {
  552. if (ast_load_resource("func_curl.so") != AST_MODULE_LOAD_SUCCESS) {
  553. ast_log(LOG_ERROR, "Cannot load func_curl, so res_config_curl cannot be loaded\n");
  554. return AST_MODULE_LOAD_DECLINE;
  555. }
  556. }
  557. reload_module();
  558. ast_config_engine_register(&curl_engine);
  559. ast_verb(1, "res_config_curl loaded.\n");
  560. return 0;
  561. }
  562. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Realtime Curl configuration",
  563. .load = load_module,
  564. .unload = unload_module,
  565. .reload = reload_module,
  566. .load_pri = AST_MODPRI_REALTIME_DRIVER,
  567. );