pbx_loopback.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Loopback PBX Module
  21. *
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/file.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/options.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/frame.h"
  38. #include "asterisk/file.h"
  39. #include "asterisk/cli.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/md5.h"
  42. #include "asterisk/linkedlists.h"
  43. #include "asterisk/chanvars.h"
  44. #include "asterisk/sched.h"
  45. #include "asterisk/io.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/crypto.h"
  48. #include "asterisk/astdb.h"
  49. static char *tdesc = "Loopback Switch";
  50. /* Loopback switch substitutes ${EXTEN}, ${CONTEXT}, and ${PRIORITY} into
  51. the data passed to it to try to get a string of the form:
  52. [exten]@context[:priority][/extramatch]
  53. Where exten, context, and priority are another extension, context, and priority
  54. to lookup and "extramatch" is an extra match restriction the *original* number
  55. must fit if specified. The "extramatch" begins with _ like an exten pattern
  56. if it is specified. Note that the search context MUST be a different context
  57. from the current context or the search will not succeed in an effort to reduce
  58. the likelihood of loops (they're still possible if you try hard, so be careful!)
  59. */
  60. #define LOOPBACK_COMMON \
  61. char buf[1024]; \
  62. int res; \
  63. char *newexten=(char *)exten, *newcontext=(char *)context; \
  64. int newpriority=priority; \
  65. char *newpattern=NULL; \
  66. loopback_helper(buf, sizeof(buf), exten, context, priority, data); \
  67. loopback_subst(&newexten, &newcontext, &newpriority, &newpattern, buf); \
  68. ast_log(LOG_DEBUG, "Parsed into %s @ %s priority %d\n", newexten, newcontext, newpriority); \
  69. if (!strcasecmp(newcontext, context)) return -1
  70. static char *loopback_helper(char *buf, int buflen, const char *exten, const char *context, int priority, const char *data)
  71. {
  72. struct ast_var_t *newvariable;
  73. struct varshead headp;
  74. char tmp[80];
  75. snprintf(tmp, sizeof(tmp), "%d", priority);
  76. memset(buf, 0, buflen);
  77. AST_LIST_HEAD_INIT_NOLOCK(&headp);
  78. newvariable = ast_var_assign("EXTEN", exten);
  79. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  80. newvariable = ast_var_assign("CONTEXT", context);
  81. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  82. newvariable = ast_var_assign("PRIORITY", tmp);
  83. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  84. pbx_substitute_variables_varshead(&headp, data, buf, buflen);
  85. /* Substitute variables */
  86. while (!AST_LIST_EMPTY(&headp)) { /* List Deletion. */
  87. newvariable = AST_LIST_REMOVE_HEAD(&headp, entries);
  88. ast_var_delete(newvariable);
  89. }
  90. return buf;
  91. }
  92. static void loopback_subst(char **newexten, char **newcontext, int *priority, char **newpattern, char *buf)
  93. {
  94. char *con;
  95. char *pri;
  96. *newpattern = strchr(buf, '/');
  97. if (*newpattern) {
  98. *(*newpattern) = '\0';
  99. (*newpattern)++;
  100. }
  101. con = strchr(buf, '@');
  102. if (con) {
  103. *con = '\0';
  104. con++;
  105. pri = strchr(con, ':');
  106. } else
  107. pri = strchr(buf, ':');
  108. if (!ast_strlen_zero(buf))
  109. *newexten = buf;
  110. if (!ast_strlen_zero(con))
  111. *newcontext = con;
  112. if (!ast_strlen_zero(pri))
  113. sscanf(pri, "%d", priority);
  114. }
  115. static int loopback_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  116. {
  117. LOOPBACK_COMMON;
  118. res = ast_exists_extension(chan, newcontext, newexten, newpriority, callerid);
  119. if (newpattern && !ast_extension_match(newpattern, exten))
  120. res = 0;
  121. return res;
  122. }
  123. static int loopback_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  124. {
  125. LOOPBACK_COMMON;
  126. res = ast_canmatch_extension(chan, newcontext, newexten, newpriority, callerid);
  127. if (newpattern && !ast_extension_match(newpattern, exten))
  128. res = 0;
  129. return res;
  130. }
  131. static int loopback_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, int newstack, const char *data)
  132. {
  133. LOOPBACK_COMMON;
  134. if (newstack)
  135. res = ast_spawn_extension(chan, newcontext, newexten, newpriority, callerid);
  136. else
  137. res = ast_exec_extension(chan, newcontext, newexten, newpriority, callerid);
  138. if (newpattern && !ast_extension_match(newpattern, exten))
  139. res = -1;
  140. return res;
  141. }
  142. static int loopback_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  143. {
  144. LOOPBACK_COMMON;
  145. res = ast_matchmore_extension(chan, newcontext, newexten, newpriority, callerid);
  146. if (newpattern && !ast_extension_match(newpattern, exten))
  147. res = 0;
  148. return res;
  149. }
  150. static struct ast_switch loopback_switch =
  151. {
  152. name: "Loopback",
  153. description: "Loopback Dialplan Switch",
  154. exists: loopback_exists,
  155. canmatch: loopback_canmatch,
  156. exec: loopback_exec,
  157. matchmore: loopback_matchmore,
  158. };
  159. char *description(void)
  160. {
  161. return tdesc;
  162. }
  163. int usecount(void)
  164. {
  165. return 1;
  166. }
  167. char *key()
  168. {
  169. return ASTERISK_GPL_KEY;
  170. }
  171. int unload_module(void)
  172. {
  173. ast_unregister_switch(&loopback_switch);
  174. return 0;
  175. }
  176. int load_module(void)
  177. {
  178. ast_register_switch(&loopback_switch);
  179. return 0;
  180. }