pbx_loopback.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\n", newexten, newcontext, newpriority); \
  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. res = ast_exists_extension(chan, newcontext, newexten, newpriority, callerid);
  118. if (newpattern && !ast_extension_match(newpattern, exten))
  119. res = 0;
  120. return res;
  121. }
  122. static int loopback_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  123. {
  124. LOOPBACK_COMMON;
  125. res = ast_canmatch_extension(chan, newcontext, newexten, newpriority, callerid);
  126. if (newpattern && !ast_extension_match(newpattern, exten))
  127. res = 0;
  128. return res;
  129. }
  130. static int loopback_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  131. {
  132. int found;
  133. LOOPBACK_COMMON;
  134. res = ast_spawn_extension(chan, newcontext, newexten, newpriority, callerid, &found, 0);
  135. return res;
  136. }
  137. static int loopback_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  138. {
  139. LOOPBACK_COMMON;
  140. res = ast_matchmore_extension(chan, newcontext, newexten, newpriority, callerid);
  141. if (newpattern && !ast_extension_match(newpattern, exten))
  142. res = 0;
  143. return res;
  144. }
  145. static struct ast_switch loopback_switch =
  146. {
  147. .name = "Loopback",
  148. .description = "Loopback Dialplan Switch",
  149. .exists = loopback_exists,
  150. .canmatch = loopback_canmatch,
  151. .exec = loopback_exec,
  152. .matchmore = loopback_matchmore,
  153. };
  154. static int unload_module(void)
  155. {
  156. ast_unregister_switch(&loopback_switch);
  157. return 0;
  158. }
  159. static int load_module(void)
  160. {
  161. if (ast_register_switch(&loopback_switch))
  162. return AST_MODULE_LOAD_FAILURE;
  163. return AST_MODULE_LOAD_SUCCESS;
  164. }
  165. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Loopback Switch");