app_page.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004 - 2006 Digium, Inc. All rights reserved.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * This code is released under the GNU General Public License
  9. * version 2.0. See LICENSE for more information.
  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. */
  18. /*! \file
  19. *
  20. * \brief page() - Paging application
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <depend>app_confbridge</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/file.h"
  36. #include "asterisk/app.h"
  37. #include "asterisk/chanvars.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/devicestate.h"
  40. #include "asterisk/dial.h"
  41. /*** DOCUMENTATION
  42. <application name="Page" language="en_US">
  43. <synopsis>
  44. Page series of phones
  45. </synopsis>
  46. <syntax>
  47. <parameter name="Technology/Resource" required="true" argsep="&amp;">
  48. <argument name="Technology/Resource" required="true">
  49. <para>Specification of the device(s) to dial. These must be in the format of
  50. <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
  51. represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
  52. available to that particular channel driver.</para>
  53. </argument>
  54. <argument name="Technology2/Resource2" multiple="true">
  55. <para>Optional extra devices to dial in parallel</para>
  56. <para>If you need more than one, enter them as Technology2/Resource2&amp;
  57. Technology3/Resourse3&amp;.....</para>
  58. </argument>
  59. </parameter>
  60. <parameter name="options">
  61. <optionlist>
  62. <option name="d">
  63. <para>Full duplex audio</para>
  64. </option>
  65. <option name="i">
  66. <para>Ignore attempts to forward the call</para>
  67. </option>
  68. <option name="q">
  69. <para>Quiet, do not play beep to caller</para>
  70. </option>
  71. <option name="r">
  72. <para>Record the page into a file (<literal>CONFBRIDGE(bridge,record_conference)</literal>)</para>
  73. </option>
  74. <option name="s">
  75. <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
  76. </option>
  77. <option name="A">
  78. <argument name="x" required="true">
  79. <para>The announcement to playback to all devices</para>
  80. </argument>
  81. <para>Play an announcement to all paged participants</para>
  82. </option>
  83. <option name="n">
  84. <para>Do not play announcement to caller (alters <literal>A(x)</literal> behavior)</para>
  85. </option>
  86. </optionlist>
  87. </parameter>
  88. <parameter name="timeout">
  89. <para>Specify the length of time that the system will attempt to connect a call.
  90. After this duration, any page calls that have not been answered will be hung up by the
  91. system.</para>
  92. </parameter>
  93. </syntax>
  94. <description>
  95. <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
  96. and dumps them into a conference bridge as muted participants. The original
  97. caller is dumped into the conference as a speaker and the room is
  98. destroyed when the original caller leaves.</para>
  99. </description>
  100. <see-also>
  101. <ref type="application">ConfBridge</ref>
  102. </see-also>
  103. </application>
  104. ***/
  105. static const char * const app_page= "Page";
  106. enum page_opt_flags {
  107. PAGE_DUPLEX = (1 << 0),
  108. PAGE_QUIET = (1 << 1),
  109. PAGE_RECORD = (1 << 2),
  110. PAGE_SKIP = (1 << 3),
  111. PAGE_IGNORE_FORWARDS = (1 << 4),
  112. PAGE_ANNOUNCE = (1 << 5),
  113. PAGE_NOCALLERANNOUNCE = (1 << 6),
  114. };
  115. enum {
  116. OPT_ARG_ANNOUNCE = 0,
  117. OPT_ARG_ARRAY_SIZE = 1,
  118. };
  119. AST_APP_OPTIONS(page_opts, {
  120. AST_APP_OPTION('d', PAGE_DUPLEX),
  121. AST_APP_OPTION('q', PAGE_QUIET),
  122. AST_APP_OPTION('r', PAGE_RECORD),
  123. AST_APP_OPTION('s', PAGE_SKIP),
  124. AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
  125. AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
  126. AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
  127. });
  128. /* We use this structure as a way to pass this to all dialed channels */
  129. struct page_options {
  130. char *opts[OPT_ARG_ARRAY_SIZE];
  131. struct ast_flags flags;
  132. };
  133. /*!
  134. * \internal
  135. * \brief Setup the page bridge profile.
  136. *
  137. * \param chan Setup bridge profile on this channel.
  138. * \param options Options to setup bridge profile.
  139. *
  140. * \return Nothing
  141. */
  142. static void setup_profile_bridge(struct ast_channel *chan, struct page_options *options)
  143. {
  144. /* Use default_bridge as a starting point */
  145. ast_func_write(chan, "CONFBRIDGE(bridge,template)", "");
  146. if (ast_test_flag(&options->flags, PAGE_RECORD)) {
  147. ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
  148. }
  149. }
  150. /*!
  151. * \internal
  152. * \brief Setup the paged user profile.
  153. *
  154. * \param chan Setup user profile on this channel.
  155. * \param options Options to setup paged user profile.
  156. *
  157. * \return Nothing
  158. */
  159. static void setup_profile_paged(struct ast_channel *chan, struct page_options *options)
  160. {
  161. /* Use default_user as a starting point */
  162. ast_func_write(chan, "CONFBRIDGE(user,template)", "");
  163. ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
  164. ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
  165. if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
  166. ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
  167. }
  168. if (ast_test_flag(&options->flags, PAGE_ANNOUNCE)
  169. && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
  170. ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
  171. }
  172. }
  173. /*!
  174. * \internal
  175. * \brief Setup the caller user profile.
  176. *
  177. * \param chan Setup user profile on this channel.
  178. * \param options Options to setup caller user profile.
  179. *
  180. * \return Nothing
  181. */
  182. static void setup_profile_caller(struct ast_channel *chan, struct page_options *options)
  183. {
  184. /* Use default_user as a starting point if not already setup. */
  185. ast_func_write(chan, "CONFBRIDGE(user,template)", "");
  186. ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
  187. ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
  188. if (!ast_test_flag(&options->flags, PAGE_NOCALLERANNOUNCE)
  189. && ast_test_flag(&options->flags, PAGE_ANNOUNCE)
  190. && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
  191. ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
  192. }
  193. }
  194. static void page_state_callback(struct ast_dial *dial)
  195. {
  196. struct ast_channel *chan;
  197. struct page_options *options;
  198. if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
  199. !(chan = ast_dial_answered(dial)) ||
  200. !(options = ast_dial_get_user_data(dial))) {
  201. return;
  202. }
  203. setup_profile_bridge(chan, options);
  204. setup_profile_paged(chan, options);
  205. }
  206. static int page_exec(struct ast_channel *chan, const char *data)
  207. {
  208. char *tech, *resource, *tmp;
  209. char confbridgeopts[128], originator[AST_CHANNEL_NAME];
  210. struct page_options options = { { 0, }, { 0, } };
  211. unsigned int confid = ast_random();
  212. struct ast_app *app;
  213. int res = 0, pos = 0, i = 0;
  214. struct ast_dial **dial_list;
  215. unsigned int num_dials;
  216. int timeout = 0;
  217. char *parse;
  218. AST_DECLARE_APP_ARGS(args,
  219. AST_APP_ARG(devices);
  220. AST_APP_ARG(options);
  221. AST_APP_ARG(timeout);
  222. );
  223. if (ast_strlen_zero(data)) {
  224. ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
  225. return -1;
  226. }
  227. if (!(app = pbx_findapp("ConfBridge"))) {
  228. ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
  229. return -1;
  230. };
  231. parse = ast_strdupa(data);
  232. AST_STANDARD_APP_ARGS(args, parse);
  233. ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
  234. if ((tmp = strchr(originator, '-'))) {
  235. *tmp = '\0';
  236. }
  237. if (!ast_strlen_zero(args.options)) {
  238. ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
  239. }
  240. if (!ast_strlen_zero(args.timeout)) {
  241. timeout = atoi(args.timeout);
  242. }
  243. snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
  244. /* Count number of extensions in list by number of ampersands + 1 */
  245. num_dials = 1;
  246. tmp = args.devices;
  247. while (*tmp) {
  248. if (*tmp == '&') {
  249. num_dials++;
  250. }
  251. tmp++;
  252. }
  253. if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
  254. ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
  255. return -1;
  256. }
  257. /* Go through parsing/calling each device */
  258. while ((tech = strsep(&args.devices, "&"))) {
  259. int state = 0;
  260. struct ast_dial *dial = NULL;
  261. /* don't call the originating device */
  262. if (!strcasecmp(tech, originator))
  263. continue;
  264. /* If no resource is available, continue on */
  265. if (!(resource = strchr(tech, '/'))) {
  266. ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
  267. continue;
  268. }
  269. /* Ensure device is not in use if skip option is enabled */
  270. if (ast_test_flag(&options.flags, PAGE_SKIP)) {
  271. state = ast_device_state(tech);
  272. if (state == AST_DEVICE_UNKNOWN) {
  273. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
  274. } else if (state != AST_DEVICE_NOT_INUSE) {
  275. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
  276. continue;
  277. }
  278. }
  279. *resource++ = '\0';
  280. /* Create a dialing structure */
  281. if (!(dial = ast_dial_create())) {
  282. ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
  283. continue;
  284. }
  285. /* Append technology and resource */
  286. if (ast_dial_append(dial, tech, resource, NULL) == -1) {
  287. ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
  288. ast_dial_destroy(dial);
  289. continue;
  290. }
  291. /* Set ANSWER_EXEC as global option */
  292. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
  293. if (timeout) {
  294. ast_dial_set_global_timeout(dial, timeout * 1000);
  295. }
  296. if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
  297. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
  298. }
  299. ast_dial_set_state_callback(dial, &page_state_callback);
  300. ast_dial_set_user_data(dial, &options);
  301. /* Run this dial in async mode */
  302. ast_dial_run(dial, chan, 1);
  303. /* Put in our dialing array */
  304. dial_list[pos++] = dial;
  305. }
  306. if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
  307. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  308. if (!res)
  309. res = ast_waitstream(chan, "");
  310. }
  311. if (!res) {
  312. setup_profile_bridge(chan, &options);
  313. setup_profile_caller(chan, &options);
  314. snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
  315. pbx_exec(chan, app, confbridgeopts);
  316. }
  317. /* Go through each dial attempt cancelling, joining, and destroying */
  318. for (i = 0; i < pos; i++) {
  319. struct ast_dial *dial = dial_list[i];
  320. /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
  321. ast_dial_join(dial);
  322. /* Hangup all channels */
  323. ast_dial_hangup(dial);
  324. /* Destroy dialing structure */
  325. ast_dial_destroy(dial);
  326. }
  327. ast_free(dial_list);
  328. return -1;
  329. }
  330. static int unload_module(void)
  331. {
  332. return ast_unregister_application(app_page);
  333. }
  334. static int load_module(void)
  335. {
  336. return ast_register_application_xml(app_page, page_exec);
  337. }
  338. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");