app_page.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/Resource3&amp;.....</para>
  58. </argument>
  59. </parameter>
  60. <parameter name="options">
  61. <optionlist>
  62. <option name="b" argsep="^">
  63. <para>Before initiating an outgoing call, Gosub to the specified
  64. location using the newly created channel. The Gosub will be
  65. executed for each destination channel.</para>
  66. <argument name="context" required="false" />
  67. <argument name="exten" required="false" />
  68. <argument name="priority" required="true" hasparams="optional" argsep="^">
  69. <argument name="arg1" multiple="true" required="true" />
  70. <argument name="argN" />
  71. </argument>
  72. </option>
  73. <option name="B" argsep="^">
  74. <para>Before initiating the outgoing call(s), Gosub to the specified
  75. location using the current channel.</para>
  76. <argument name="context" required="false" />
  77. <argument name="exten" required="false" />
  78. <argument name="priority" required="true" hasparams="optional" argsep="^">
  79. <argument name="arg1" multiple="true" required="true" />
  80. <argument name="argN" />
  81. </argument>
  82. </option>
  83. <option name="d">
  84. <para>Full duplex audio</para>
  85. </option>
  86. <option name="i">
  87. <para>Ignore attempts to forward the call</para>
  88. </option>
  89. <option name="q">
  90. <para>Quiet, do not play beep to caller</para>
  91. </option>
  92. <option name="r">
  93. <para>Record the page into a file (<literal>CONFBRIDGE(bridge,record_conference)</literal>)</para>
  94. </option>
  95. <option name="s">
  96. <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
  97. </option>
  98. <option name="A">
  99. <argument name="x" required="true">
  100. <para>The announcement to playback to all devices</para>
  101. </argument>
  102. <para>Play an announcement to all paged participants</para>
  103. </option>
  104. <option name="n">
  105. <para>Do not play announcement to caller (alters <literal>A(x)</literal> behavior)</para>
  106. </option>
  107. </optionlist>
  108. </parameter>
  109. <parameter name="timeout">
  110. <para>Specify the length of time that the system will attempt to connect a call.
  111. After this duration, any page calls that have not been answered will be hung up by the
  112. system.</para>
  113. </parameter>
  114. </syntax>
  115. <description>
  116. <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
  117. and dumps them into a conference bridge as muted participants. The original
  118. caller is dumped into the conference as a speaker and the room is
  119. destroyed when the original caller leaves.</para>
  120. </description>
  121. <see-also>
  122. <ref type="application">ConfBridge</ref>
  123. </see-also>
  124. </application>
  125. ***/
  126. static const char * const app_page= "Page";
  127. enum page_opt_flags {
  128. PAGE_DUPLEX = (1 << 0),
  129. PAGE_QUIET = (1 << 1),
  130. PAGE_RECORD = (1 << 2),
  131. PAGE_SKIP = (1 << 3),
  132. PAGE_IGNORE_FORWARDS = (1 << 4),
  133. PAGE_ANNOUNCE = (1 << 5),
  134. PAGE_NOCALLERANNOUNCE = (1 << 6),
  135. PAGE_PREDIAL_CALLEE = (1 << 7),
  136. PAGE_PREDIAL_CALLER = (1 << 8),
  137. };
  138. enum {
  139. OPT_ARG_ANNOUNCE = 0,
  140. OPT_ARG_PREDIAL_CALLEE = 1,
  141. OPT_ARG_PREDIAL_CALLER = 2,
  142. OPT_ARG_ARRAY_SIZE = 3,
  143. };
  144. AST_APP_OPTIONS(page_opts, {
  145. AST_APP_OPTION_ARG('b', PAGE_PREDIAL_CALLEE, OPT_ARG_PREDIAL_CALLEE),
  146. AST_APP_OPTION_ARG('B', PAGE_PREDIAL_CALLER, OPT_ARG_PREDIAL_CALLER),
  147. AST_APP_OPTION('d', PAGE_DUPLEX),
  148. AST_APP_OPTION('q', PAGE_QUIET),
  149. AST_APP_OPTION('r', PAGE_RECORD),
  150. AST_APP_OPTION('s', PAGE_SKIP),
  151. AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
  152. AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
  153. AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
  154. });
  155. /* We use this structure as a way to pass this to all dialed channels */
  156. struct page_options {
  157. char *opts[OPT_ARG_ARRAY_SIZE];
  158. struct ast_flags flags;
  159. };
  160. /*!
  161. * \internal
  162. * \brief Setup the page bridge profile.
  163. *
  164. * \param chan Setup bridge profile on this channel.
  165. * \param options Options to setup bridge profile.
  166. *
  167. * \return Nothing
  168. */
  169. static void setup_profile_bridge(struct ast_channel *chan, struct page_options *options)
  170. {
  171. /* Use default_bridge as a starting point */
  172. ast_func_write(chan, "CONFBRIDGE(bridge,template)", "");
  173. if (ast_test_flag(&options->flags, PAGE_RECORD)) {
  174. ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
  175. }
  176. }
  177. /*!
  178. * \internal
  179. * \brief Setup the paged user profile.
  180. *
  181. * \param chan Setup user profile on this channel.
  182. * \param options Options to setup paged user profile.
  183. *
  184. * \return Nothing
  185. */
  186. static void setup_profile_paged(struct ast_channel *chan, struct page_options *options)
  187. {
  188. /* Use default_user as a starting point */
  189. ast_func_write(chan, "CONFBRIDGE(user,template)", "");
  190. ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
  191. ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
  192. if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
  193. ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
  194. }
  195. if (ast_test_flag(&options->flags, PAGE_ANNOUNCE)
  196. && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
  197. ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
  198. }
  199. }
  200. /*!
  201. * \internal
  202. * \brief Setup the caller user profile.
  203. *
  204. * \param chan Setup user profile on this channel.
  205. * \param options Options to setup caller user profile.
  206. *
  207. * \return Nothing
  208. */
  209. static void setup_profile_caller(struct ast_channel *chan, struct page_options *options)
  210. {
  211. /* Use default_user as a starting point if not already setup. */
  212. ast_func_write(chan, "CONFBRIDGE(user,template)", "");
  213. ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
  214. ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
  215. if (!ast_test_flag(&options->flags, PAGE_NOCALLERANNOUNCE)
  216. && ast_test_flag(&options->flags, PAGE_ANNOUNCE)
  217. && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
  218. ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
  219. }
  220. }
  221. static void page_state_callback(struct ast_dial *dial)
  222. {
  223. struct ast_channel *chan;
  224. struct page_options *options;
  225. if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
  226. !(chan = ast_dial_answered(dial)) ||
  227. !(options = ast_dial_get_user_data(dial))) {
  228. return;
  229. }
  230. setup_profile_bridge(chan, options);
  231. setup_profile_paged(chan, options);
  232. }
  233. static int page_exec(struct ast_channel *chan, const char *data)
  234. {
  235. char *tech;
  236. char *resource;
  237. char *tmp;
  238. char *predial_callee = NULL;
  239. char confbridgeopts[128];
  240. char originator[AST_CHANNEL_NAME];
  241. struct page_options options = { { 0, }, { 0, } };
  242. unsigned int confid = ast_random();
  243. struct ast_app *app;
  244. int res = 0;
  245. int pos = 0;
  246. int i = 0;
  247. struct ast_dial **dial_list;
  248. unsigned int num_dials;
  249. int timeout = 0;
  250. char *parse;
  251. AST_DECLARE_APP_ARGS(args,
  252. AST_APP_ARG(devices);
  253. AST_APP_ARG(options);
  254. AST_APP_ARG(timeout);
  255. );
  256. if (ast_strlen_zero(data)) {
  257. ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
  258. return -1;
  259. }
  260. if (!(app = pbx_findapp("ConfBridge"))) {
  261. ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
  262. return -1;
  263. };
  264. parse = ast_strdupa(data);
  265. AST_STANDARD_APP_ARGS(args, parse);
  266. ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
  267. if ((tmp = strchr(originator, '-'))) {
  268. *tmp = '\0';
  269. }
  270. if (!ast_strlen_zero(args.options)) {
  271. ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
  272. }
  273. if (!ast_strlen_zero(args.timeout)) {
  274. timeout = atoi(args.timeout);
  275. }
  276. snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
  277. /* Count number of extensions in list by number of ampersands + 1 */
  278. num_dials = 1;
  279. tmp = args.devices;
  280. while (*tmp) {
  281. if (*tmp == '&') {
  282. num_dials++;
  283. }
  284. tmp++;
  285. }
  286. if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
  287. ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
  288. return -1;
  289. }
  290. /* PREDIAL: Preprocess any callee gosub arguments. */
  291. if (ast_test_flag(&options.flags, PAGE_PREDIAL_CALLEE)
  292. && !ast_strlen_zero(options.opts[OPT_ARG_PREDIAL_CALLEE])) {
  293. ast_replace_subargument_delimiter(options.opts[OPT_ARG_PREDIAL_CALLEE]);
  294. predial_callee =
  295. (char *) ast_app_expand_sub_args(chan, options.opts[OPT_ARG_PREDIAL_CALLEE]);
  296. }
  297. /* PREDIAL: Run gosub on the caller's channel */
  298. if (ast_test_flag(&options.flags, PAGE_PREDIAL_CALLER)
  299. && !ast_strlen_zero(options.opts[OPT_ARG_PREDIAL_CALLER])) {
  300. ast_replace_subargument_delimiter(options.opts[OPT_ARG_PREDIAL_CALLER]);
  301. ast_app_exec_sub(NULL, chan, options.opts[OPT_ARG_PREDIAL_CALLER], 0);
  302. }
  303. /* Go through parsing/calling each device */
  304. while ((tech = strsep(&args.devices, "&"))) {
  305. int state = 0;
  306. struct ast_dial *dial = NULL;
  307. /* don't call the originating device */
  308. if (!strcasecmp(tech, originator))
  309. continue;
  310. /* If no resource is available, continue on */
  311. if (!(resource = strchr(tech, '/'))) {
  312. ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
  313. continue;
  314. }
  315. /* Ensure device is not in use if skip option is enabled */
  316. if (ast_test_flag(&options.flags, PAGE_SKIP)) {
  317. state = ast_device_state(tech);
  318. if (state == AST_DEVICE_UNKNOWN) {
  319. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
  320. } else if (state != AST_DEVICE_NOT_INUSE) {
  321. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
  322. continue;
  323. }
  324. }
  325. *resource++ = '\0';
  326. /* Create a dialing structure */
  327. if (!(dial = ast_dial_create())) {
  328. ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
  329. continue;
  330. }
  331. /* Append technology and resource */
  332. if (ast_dial_append(dial, tech, resource, NULL) == -1) {
  333. ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
  334. ast_dial_destroy(dial);
  335. continue;
  336. }
  337. /* Set ANSWER_EXEC as global option */
  338. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
  339. if (predial_callee) {
  340. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_PREDIAL, predial_callee);
  341. }
  342. if (timeout) {
  343. ast_dial_set_global_timeout(dial, timeout * 1000);
  344. }
  345. if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
  346. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
  347. }
  348. ast_dial_set_state_callback(dial, &page_state_callback);
  349. ast_dial_set_user_data(dial, &options);
  350. /* Run this dial in async mode */
  351. ast_dial_run(dial, chan, 1);
  352. /* Put in our dialing array */
  353. dial_list[pos++] = dial;
  354. }
  355. ast_free(predial_callee);
  356. if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
  357. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  358. if (!res)
  359. res = ast_waitstream(chan, "");
  360. }
  361. if (!res) {
  362. setup_profile_bridge(chan, &options);
  363. setup_profile_caller(chan, &options);
  364. snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
  365. pbx_exec(chan, app, confbridgeopts);
  366. }
  367. /* Go through each dial attempt cancelling, joining, and destroying */
  368. for (i = 0; i < pos; i++) {
  369. struct ast_dial *dial = dial_list[i];
  370. /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
  371. ast_dial_join(dial);
  372. /* Hangup all channels */
  373. ast_dial_hangup(dial);
  374. /* Destroy dialing structure */
  375. ast_dial_destroy(dial);
  376. }
  377. ast_free(dial_list);
  378. return -1;
  379. }
  380. static int unload_module(void)
  381. {
  382. return ast_unregister_application(app_page);
  383. }
  384. static int load_module(void)
  385. {
  386. return ast_register_application_xml(app_page, page_exec);
  387. }
  388. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");