pbx_realtime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.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 Realtime PBX Module
  21. *
  22. * \arg See also: \ref AstARA
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <signal.h>
  30. #include "asterisk/file.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/frame.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/manager.h"
  39. #include "asterisk/cli.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/linkedlists.h"
  42. #include "asterisk/chanvars.h"
  43. #include "asterisk/sched.h"
  44. #include "asterisk/io.h"
  45. #include "asterisk/utils.h"
  46. #include "asterisk/astdb.h"
  47. #include "asterisk/app.h"
  48. #include "asterisk/astobj2.h"
  49. #include "asterisk/stasis_channels.h"
  50. #define MODE_MATCH 0
  51. #define MODE_MATCHMORE 1
  52. #define MODE_CANMATCH 2
  53. #define EXT_DATA_SIZE 256
  54. enum option_flags {
  55. OPTION_PATTERNS_DISABLED = (1 << 0),
  56. };
  57. AST_APP_OPTIONS(switch_opts, {
  58. AST_APP_OPTION('p', OPTION_PATTERNS_DISABLED),
  59. });
  60. struct cache_entry {
  61. struct timeval when;
  62. struct ast_variable *var;
  63. int priority;
  64. char *context;
  65. char exten[2];
  66. };
  67. struct ao2_container *cache;
  68. pthread_t cleanup_thread = 0;
  69. static int cache_hash(const void *obj, const int flags)
  70. {
  71. const struct cache_entry *e = obj;
  72. return ast_str_case_hash(e->exten) + e->priority;
  73. }
  74. static int cache_cmp(void *obj, void *arg, int flags)
  75. {
  76. struct cache_entry *e = obj, *f = arg;
  77. return e->priority != f->priority ? 0 :
  78. strcmp(e->exten, f->exten) ? 0 :
  79. strcmp(e->context, f->context) ? 0 :
  80. CMP_MATCH;
  81. }
  82. static struct ast_variable *dup_vars(struct ast_variable *v)
  83. {
  84. struct ast_variable *new, *list = NULL;
  85. for (; v; v = v->next) {
  86. if (!(new = ast_variable_new(v->name, v->value, v->file))) {
  87. ast_variables_destroy(list);
  88. return NULL;
  89. }
  90. /* Reversed list in cache, but when we duplicate out of the cache,
  91. * it's back to correct order. */
  92. new->next = list;
  93. list = new;
  94. }
  95. return list;
  96. }
  97. static void free_entry(void *obj)
  98. {
  99. struct cache_entry *e = obj;
  100. ast_variables_destroy(e->var);
  101. }
  102. static int purge_old_fn(void *obj, void *arg, int flags)
  103. {
  104. struct cache_entry *e = obj;
  105. struct timeval *now = arg;
  106. return ast_tvdiff_ms(*now, e->when) >= 1000 ? CMP_MATCH : 0;
  107. }
  108. static void *cleanup(void *unused)
  109. {
  110. struct timespec forever = { 999999999, 0 }, one_second = { 1, 0 };
  111. struct timeval now;
  112. for (;;) {
  113. pthread_testcancel();
  114. if (ao2_container_count(cache) == 0) {
  115. nanosleep(&forever, NULL);
  116. }
  117. pthread_testcancel();
  118. now = ast_tvnow();
  119. ao2_callback(cache, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, purge_old_fn, &now);
  120. pthread_testcancel();
  121. nanosleep(&one_second, NULL);
  122. }
  123. return NULL;
  124. }
  125. /* Realtime switch looks up extensions in the supplied realtime table.
  126. [context@][realtimetable][/options]
  127. If the realtimetable is omitted it is assumed to be "extensions". If no context is
  128. specified the context is assumed to be whatever is the container.
  129. The realtime table should have entries for context,exten,priority,app,args
  130. The realtime table currently does not support callerid fields.
  131. */
  132. static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode, struct ast_flags flags)
  133. {
  134. struct ast_variable *var;
  135. struct ast_config *cfg;
  136. char pri[20];
  137. char *ematch;
  138. char rexten[AST_MAX_EXTENSION + 20]="";
  139. int match;
  140. /* Optimization: since we don't support hints in realtime, it's silly to
  141. * query for a hint here, since we won't actually do anything with it.
  142. * This just wastes CPU time and resources. */
  143. if (priority < 0) {
  144. return NULL;
  145. }
  146. snprintf(pri, sizeof(pri), "%d", priority);
  147. switch(mode) {
  148. case MODE_MATCHMORE:
  149. ematch = "exten LIKE";
  150. snprintf(rexten, sizeof(rexten), "%s_%%", exten);
  151. break;
  152. case MODE_CANMATCH:
  153. ematch = "exten LIKE";
  154. snprintf(rexten, sizeof(rexten), "%s%%", exten);
  155. break;
  156. case MODE_MATCH:
  157. default:
  158. ematch = "exten";
  159. ast_copy_string(rexten, exten, sizeof(rexten));
  160. }
  161. var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, SENTINEL);
  162. if (!var && !ast_test_flag(&flags, OPTION_PATTERNS_DISABLED)) {
  163. cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, SENTINEL);
  164. if (cfg) {
  165. char *cat = ast_category_browse(cfg, NULL);
  166. while(cat) {
  167. switch(mode) {
  168. case MODE_MATCHMORE:
  169. match = ast_extension_close(cat, exten, 1);
  170. break;
  171. case MODE_CANMATCH:
  172. match = ast_extension_close(cat, exten, 0);
  173. break;
  174. case MODE_MATCH:
  175. default:
  176. match = ast_extension_match(cat, exten);
  177. }
  178. if (match) {
  179. var = ast_category_detach_variables(ast_category_get(cfg, cat, NULL));
  180. break;
  181. }
  182. cat = ast_category_browse(cfg, cat);
  183. }
  184. ast_config_destroy(cfg);
  185. }
  186. }
  187. return var;
  188. }
  189. static struct ast_variable *realtime_common(const char *context, const char *exten, int priority, const char *data, int mode)
  190. {
  191. const char *ctx = NULL;
  192. char *table;
  193. struct ast_variable *var=NULL;
  194. struct ast_flags flags = { 0, };
  195. struct cache_entry *ce;
  196. struct {
  197. struct cache_entry ce;
  198. char exten[AST_MAX_EXTENSION];
  199. } cache_search = { { .priority = priority, .context = (char *) context }, };
  200. char *buf = ast_strdupa(data);
  201. /* "Realtime" prefix is stripped off in the parent engine. The
  202. * remaining string is: [[context@]table][/opts] */
  203. char *opts = strchr(buf, '/');
  204. if (opts)
  205. *opts++ = '\0';
  206. table = strchr(buf, '@');
  207. if (table) {
  208. *table++ = '\0';
  209. ctx = buf;
  210. }
  211. ctx = S_OR(ctx, context);
  212. table = S_OR(table, "extensions");
  213. if (!ast_strlen_zero(opts)) {
  214. ast_app_parse_options(switch_opts, &flags, NULL, opts);
  215. }
  216. ast_copy_string(cache_search.exten, exten, sizeof(cache_search.exten));
  217. if (mode == MODE_MATCH && (ce = ao2_find(cache, &cache_search, OBJ_POINTER))) {
  218. var = dup_vars(ce->var);
  219. ao2_ref(ce, -1);
  220. } else {
  221. var = realtime_switch_common(table, ctx, exten, priority, mode, flags);
  222. do {
  223. struct ast_variable *new;
  224. /* Only cache matches */
  225. if (mode != MODE_MATCH) {
  226. break;
  227. }
  228. if (!(new = dup_vars(var))) {
  229. break;
  230. }
  231. if (!(ce = ao2_alloc(sizeof(*ce) + strlen(exten) + strlen(context), free_entry))) {
  232. ast_variables_destroy(new);
  233. break;
  234. }
  235. ce->context = ce->exten + strlen(exten) + 1;
  236. strcpy(ce->exten, exten); /* SAFE */
  237. strcpy(ce->context, context); /* SAFE */
  238. ce->priority = priority;
  239. ce->var = new;
  240. ce->when = ast_tvnow();
  241. ao2_link(cache, ce);
  242. pthread_kill(cleanup_thread, SIGURG);
  243. ao2_ref(ce, -1);
  244. } while (0);
  245. }
  246. return var;
  247. }
  248. static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  249. {
  250. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
  251. if (var) {
  252. ast_variables_destroy(var);
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  258. {
  259. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH);
  260. if (var) {
  261. ast_variables_destroy(var);
  262. return 1;
  263. }
  264. return 0;
  265. }
  266. static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  267. {
  268. int res = -1;
  269. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
  270. if (var) {
  271. char *appdata_tmp = "";
  272. char *app = NULL;
  273. struct ast_variable *v;
  274. for (v = var; v ; v = v->next) {
  275. if (!strcasecmp(v->name, "app"))
  276. app = ast_strdupa(v->value);
  277. else if (!strcasecmp(v->name, "appdata")) {
  278. appdata_tmp = ast_strdupa(v->value);
  279. }
  280. }
  281. ast_variables_destroy(var);
  282. if (!ast_strlen_zero(app)) {
  283. struct ast_app *a = pbx_findapp(app);
  284. if (a) {
  285. char appdata[512];
  286. char tmp1[80];
  287. char tmp2[80];
  288. char tmp3[EXT_DATA_SIZE];
  289. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  290. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  291. appdata[0] = 0; /* just in case the substitute var func isn't called */
  292. if(!ast_strlen_zero(appdata_tmp))
  293. pbx_substitute_variables_helper(chan, appdata_tmp, appdata, sizeof(appdata) - 1);
  294. ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
  295. ast_channel_exten(chan), ast_channel_context(chan), ast_channel_priority(chan),
  296. term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
  297. term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)),
  298. term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
  299. if (ast_channel_snapshot_type()) {
  300. ast_channel_lock(chan);
  301. snapshot = ast_channel_snapshot_create(chan);
  302. ast_channel_unlock(chan);
  303. }
  304. if (snapshot) {
  305. /* pbx_exec sets application name and data, but we don't want to log
  306. * every exec. Just update the snapshot here instead.
  307. */
  308. ast_string_field_set(snapshot, appl, app);
  309. ast_string_field_set(snapshot, data, !ast_strlen_zero(appdata) ? appdata : "(NULL)");
  310. msg = stasis_message_create(ast_channel_snapshot_type(), snapshot);
  311. if (msg) {
  312. stasis_publish(ast_channel_topic(chan), msg);
  313. }
  314. }
  315. res = pbx_exec(chan, a, appdata);
  316. } else
  317. ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
  318. } else {
  319. ast_log(LOG_WARNING, "No application specified for realtime extension '%s' in context '%s'\n", exten, context);
  320. }
  321. }
  322. return res;
  323. }
  324. static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  325. {
  326. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE);
  327. if (var) {
  328. ast_variables_destroy(var);
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static struct ast_switch realtime_switch =
  334. {
  335. .name = "Realtime",
  336. .description = "Realtime Dialplan Switch",
  337. .exists = realtime_exists,
  338. .canmatch = realtime_canmatch,
  339. .exec = realtime_exec,
  340. .matchmore = realtime_matchmore,
  341. };
  342. static int unload_module(void)
  343. {
  344. ast_unregister_switch(&realtime_switch);
  345. pthread_cancel(cleanup_thread);
  346. pthread_kill(cleanup_thread, SIGURG);
  347. pthread_join(cleanup_thread, NULL);
  348. /* Destroy all remaining entries */
  349. ao2_ref(cache, -1);
  350. return 0;
  351. }
  352. static int load_module(void)
  353. {
  354. if (!(cache = ao2_container_alloc(573, cache_hash, cache_cmp))) {
  355. return AST_MODULE_LOAD_FAILURE;
  356. }
  357. if (ast_pthread_create(&cleanup_thread, NULL, cleanup, NULL)) {
  358. return AST_MODULE_LOAD_FAILURE;
  359. }
  360. if (ast_register_switch(&realtime_switch))
  361. return AST_MODULE_LOAD_FAILURE;
  362. return AST_MODULE_LOAD_SUCCESS;
  363. }
  364. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Realtime Switch");