app_parkandannounce.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Author: Ben Miller <bgmiller@dccinc.com>
  9. * With TONS of help from Mark!
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. /*! \file
  22. *
  23. * \brief ParkAndAnnounce application for Asterisk
  24. *
  25. * \author Ben Miller <bgmiller@dccinc.com>
  26. * \arg With TONS of help from Mark!
  27. *
  28. * \ingroup applications
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include "asterisk/file.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/features.h"
  40. #include "asterisk/say.h"
  41. #include "asterisk/lock.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/app.h"
  44. /*** DOCUMENTATION
  45. <application name="ParkAndAnnounce" language="en_US">
  46. <synopsis>
  47. Park and Announce.
  48. </synopsis>
  49. <syntax>
  50. <parameter name="announce_template" required="true" argsep=":">
  51. <argument name="announce" required="true">
  52. <para>Colon-separated list of files to announce. The word
  53. <literal>PARKED</literal> will be replaced by a say_digits of the extension in which
  54. the call is parked.</para>
  55. </argument>
  56. <argument name="announce1" multiple="true" />
  57. </parameter>
  58. <parameter name="timeout" required="true">
  59. <para>Time in seconds before the call returns into the return
  60. context.</para>
  61. </parameter>
  62. <parameter name="dial" required="true">
  63. <para>The app_dial style resource to call to make the
  64. announcement. Console/dsp calls the console.</para>
  65. </parameter>
  66. <parameter name="return_context">
  67. <para>The goto-style label to jump the call back into after
  68. timeout. Default <literal>priority+1</literal>.</para>
  69. </parameter>
  70. </syntax>
  71. <description>
  72. <para>Park a call into the parkinglot and announce the call to another channel.</para>
  73. <para>The variable <variable>PARKEDAT</variable> will contain the parking extension
  74. into which the call was placed. Use with the Local channel to allow the dialplan to make
  75. use of this information.</para>
  76. </description>
  77. <see-also>
  78. <ref type="application">Park</ref>
  79. <ref type="application">ParkedCall</ref>
  80. </see-also>
  81. </application>
  82. ***/
  83. static char *app = "ParkAndAnnounce";
  84. static int parkandannounce_exec(struct ast_channel *chan, const char *data)
  85. {
  86. int res = -1;
  87. int lot, timeout = 0, dres;
  88. char *dialtech, *tmp[100], buf[13];
  89. int looptemp, i;
  90. char *s;
  91. struct ast_channel *dchan;
  92. struct outgoing_helper oh = { 0, };
  93. int outstate;
  94. struct ast_format tmpfmt;
  95. struct ast_format_cap *cap_slin = ast_format_cap_alloc_nolock();
  96. AST_DECLARE_APP_ARGS(args,
  97. AST_APP_ARG(template);
  98. AST_APP_ARG(timeout);
  99. AST_APP_ARG(dial);
  100. AST_APP_ARG(return_context);
  101. );
  102. if (ast_strlen_zero(data)) {
  103. ast_log(LOG_WARNING, "ParkAndAnnounce requires arguments: (announce:template|timeout|dial|[return_context])\n");
  104. res = -1;
  105. goto parkcleanup;
  106. }
  107. if (!cap_slin) {
  108. res = -1;
  109. goto parkcleanup;
  110. }
  111. ast_format_cap_add(cap_slin, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  112. s = ast_strdupa(data);
  113. AST_STANDARD_APP_ARGS(args, s);
  114. if (args.timeout)
  115. timeout = atoi(args.timeout) * 1000;
  116. if (ast_strlen_zero(args.dial)) {
  117. ast_log(LOG_WARNING, "PARK: A dial resource must be specified i.e: Console/dsp or DAHDI/g1/5551212\n");
  118. res = -1;
  119. goto parkcleanup;
  120. }
  121. dialtech = strsep(&args.dial, "/");
  122. ast_verb(3, "Dial Tech,String: (%s,%s)\n", dialtech, args.dial);
  123. if (!ast_strlen_zero(args.return_context)) {
  124. ast_clear_flag(chan, AST_FLAG_IN_AUTOLOOP);
  125. ast_parseable_goto(chan, args.return_context);
  126. }
  127. ast_verb(3, "Return Context: (%s,%s,%d) ID: %s\n", chan->context, chan->exten,
  128. chan->priority,
  129. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, ""));
  130. if (!ast_exists_extension(chan, chan->context, chan->exten, chan->priority,
  131. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
  132. ast_verb(3, "Warning: Return Context Invalid, call will return to default|s\n");
  133. }
  134. /* we are using masq_park here to protect * from touching the channel once we park it. If the channel comes out of timeout
  135. before we are done announcing and the channel is messed with, Kablooeee. So we use Masq to prevent this. */
  136. res = ast_masq_park_call(chan, NULL, timeout, &lot);
  137. if (res == -1) {
  138. goto parkcleanup;
  139. }
  140. ast_verb(3, "Call Parking Called, lot: %d, timeout: %d, context: %s\n", lot, timeout, args.return_context);
  141. /* Now place the call to the extension */
  142. snprintf(buf, sizeof(buf), "%d", lot);
  143. oh.parent_channel = chan;
  144. oh.vars = ast_variable_new("_PARKEDAT", buf, "");
  145. dchan = __ast_request_and_dial(dialtech, cap_slin, chan, args.dial, 30000,
  146. &outstate,
  147. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
  148. S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, NULL),
  149. &oh);
  150. if (dchan) {
  151. if (dchan->_state == AST_STATE_UP) {
  152. ast_verb(4, "Channel %s was answered.\n", dchan->name);
  153. } else {
  154. ast_verb(4, "Channel %s was never answered.\n", dchan->name);
  155. ast_log(LOG_WARNING, "PARK: Channel %s was never answered for the announce.\n", dchan->name);
  156. ast_hangup(dchan);
  157. res = -1;
  158. goto parkcleanup;
  159. }
  160. } else {
  161. ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n");
  162. res = -1;
  163. goto parkcleanup;
  164. }
  165. ast_stopstream(dchan);
  166. /* now we have the call placed and are ready to play stuff to it */
  167. ast_verb(4, "Announce Template:%s\n", args.template);
  168. for (looptemp = 0; looptemp < ARRAY_LEN(tmp); looptemp++) {
  169. if ((tmp[looptemp] = strsep(&args.template, ":")) != NULL)
  170. continue;
  171. else
  172. break;
  173. }
  174. for (i = 0; i < looptemp; i++) {
  175. ast_verb(4, "Announce:%s\n", tmp[i]);
  176. if (!strcmp(tmp[i], "PARKED")) {
  177. ast_say_digits(dchan, lot, "", dchan->language);
  178. } else {
  179. dres = ast_streamfile(dchan, tmp[i], dchan->language);
  180. if (!dres) {
  181. dres = ast_waitstream(dchan, "");
  182. } else {
  183. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", tmp[i], dchan->name);
  184. dres = 0;
  185. }
  186. }
  187. }
  188. ast_stopstream(dchan);
  189. ast_hangup(dchan);
  190. parkcleanup:
  191. cap_slin = ast_format_cap_destroy(cap_slin);
  192. return res;
  193. }
  194. static int unload_module(void)
  195. {
  196. return ast_unregister_application(app);
  197. }
  198. static int load_module(void)
  199. {
  200. /* return ast_register_application(app, park_exec); */
  201. return ast_register_application_xml(app, parkandannounce_exec);
  202. }
  203. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Parking and Announce Application");