pbx_loopback.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /*** MODULEINFO
  24. <support_level>core</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/file.h"
  29. #include "asterisk/logger.h"
  30. #include "asterisk/channel.h"
  31. #include "asterisk/config.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/lock.h"
  37. #include "asterisk/md5.h"
  38. #include "asterisk/linkedlists.h"
  39. #include "asterisk/chanvars.h"
  40. #include "asterisk/sched.h"
  41. #include "asterisk/io.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/astdb.h"
  44. /* Loopback switch creates a 'tunnel' to another context. When extension
  45. lookups pass through the 'tunnel', Asterisk expressions can be used
  46. to modify the target extension, context, and priority in any way desired.
  47. If there is a match at the far end, execution jumps through the 'tunnel'
  48. to the matched context, extension, and priority.
  49. Global variables as well as ${CONTEXT}, ${EXTEN}, and ${PRIORITY} are
  50. available for substitution. After substitution Loopback expects to get
  51. 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 a dialplan extension pattern which the *original*
  55. number must match. If exten or priority are empty, the original values are
  56. used.
  57. Note that the search context MUST be a different context from the current
  58. context or the search will not succeed. This is intended to reduce the
  59. likelihood of loops (they're still possible if you try hard, so be careful!)
  60. */
  61. #define LOOPBACK_COMMON \
  62. char buf[1024]; \
  63. int res; \
  64. char *newexten=(char *)exten, *newcontext=(char *)context; \
  65. int newpriority=priority; \
  66. char *newpattern=NULL; \
  67. loopback_subst(buf, sizeof(buf), exten, context, priority, data); \
  68. loopback_parse(&newexten, &newcontext, &newpriority, &newpattern, buf); \
  69. ast_debug(1, "Parsed into %s @ %s priority %d pattern %s\n", newexten, newcontext, newpriority, newpattern); \
  70. if (!strcasecmp(newcontext, context)) return -1
  71. static char *loopback_subst(char *buf, int buflen, const char *exten, const char *context, int priority, const char *data)
  72. {
  73. struct ast_var_t *newvariable;
  74. struct varshead headp;
  75. char tmp[80];
  76. snprintf(tmp, sizeof(tmp), "%d", priority);
  77. AST_LIST_HEAD_INIT_NOLOCK(&headp);
  78. if ((newvariable = ast_var_assign("EXTEN", exten))) {
  79. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  80. }
  81. if ((newvariable = ast_var_assign("CONTEXT", context))) {
  82. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  83. }
  84. if ((newvariable = ast_var_assign("PRIORITY", tmp))) {
  85. AST_LIST_INSERT_HEAD(&headp, newvariable, entries);
  86. }
  87. /* Substitute variables */
  88. pbx_substitute_variables_varshead(&headp, data, buf, buflen);
  89. /* free the list */
  90. while ((newvariable = AST_LIST_REMOVE_HEAD(&headp, entries)))
  91. ast_var_delete(newvariable);
  92. return buf;
  93. }
  94. static void loopback_parse(char **newexten, char **newcontext, int *priority, char **newpattern, char *buf)
  95. {
  96. char *con;
  97. char *pri;
  98. *newpattern = strchr(buf, '/');
  99. if (*newpattern)
  100. *(*newpattern)++ = '\0';
  101. con = strchr(buf, '@');
  102. if (con) {
  103. *con++ = '\0';
  104. pri = strchr(con, ':');
  105. } else
  106. pri = strchr(buf, ':');
  107. if (!ast_strlen_zero(buf))
  108. *newexten = buf;
  109. if (!ast_strlen_zero(con))
  110. *newcontext = con;
  111. if (!ast_strlen_zero(pri))
  112. sscanf(pri, "%30d", priority);
  113. }
  114. static int loopback_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  115. {
  116. LOOPBACK_COMMON;
  117. if (newpattern && !ast_extension_match(newpattern, exten))
  118. res = 0;
  119. else
  120. res = ast_exists_extension(chan, newcontext, newexten, newpriority, callerid);
  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. if (newpattern && !ast_extension_match(newpattern, exten))
  127. res = 0;
  128. else
  129. res = ast_canmatch_extension(chan, newcontext, newexten, newpriority, callerid);
  130. return res;
  131. }
  132. static int loopback_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  133. {
  134. int found;
  135. LOOPBACK_COMMON;
  136. res = ast_spawn_extension(chan, newcontext, newexten, newpriority, callerid, &found, 0);
  137. return res;
  138. }
  139. static int loopback_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  140. {
  141. LOOPBACK_COMMON;
  142. if (newpattern && !ast_extension_match(newpattern, exten))
  143. res = 0;
  144. else
  145. res = ast_matchmore_extension(chan, newcontext, newexten, newpriority, callerid);
  146. return res;
  147. }
  148. static struct ast_switch loopback_switch =
  149. {
  150. .name = "Loopback",
  151. .description = "Loopback Dialplan Switch",
  152. .exists = loopback_exists,
  153. .canmatch = loopback_canmatch,
  154. .exec = loopback_exec,
  155. .matchmore = loopback_matchmore,
  156. };
  157. static int unload_module(void)
  158. {
  159. ast_unregister_switch(&loopback_switch);
  160. return 0;
  161. }
  162. static int load_module(void)
  163. {
  164. if (ast_register_switch(&loopback_switch))
  165. return AST_MODULE_LOAD_FAILURE;
  166. return AST_MODULE_LOAD_SUCCESS;
  167. }
  168. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Loopback Switch");