app_page.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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>dahdi</depend>
  28. <depend>app_meetme</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/file.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/chanvars.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/devicestate.h"
  41. #include "asterisk/dial.h"
  42. /*** DOCUMENTATION
  43. <application name="Page" language="en_US">
  44. <synopsis>
  45. Page series of phones
  46. </synopsis>
  47. <syntax>
  48. <parameter name="Technology/Resource" required="true" argsep="&amp;">
  49. <argument name="Technology/Resource" required="true">
  50. <para>Specification of the device(s) to dial. These must be in the format of
  51. <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
  52. represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
  53. available to that particular channel driver.</para>
  54. </argument>
  55. <argument name="Technology2/Resource2" multiple="true">
  56. <para>Optional extra devices to dial inparallel</para>
  57. <para>If you need more then one enter them as Technology2/Resource2&amp;
  58. Technology3/Resourse3&amp;.....</para>
  59. </argument>
  60. </parameter>
  61. <parameter name="options">
  62. <optionlist>
  63. <option name="d">
  64. <para>Full duplex audio</para>
  65. </option>
  66. <option name="i">
  67. <para>Ignore attempts to forward the call</para>
  68. </option>
  69. <option name="q">
  70. <para>Quiet, do not play beep to caller</para>
  71. </option>
  72. <option name="r">
  73. <para>Record the page into a file (meetme option <literal>r</literal>)</para>
  74. </option>
  75. <option name="s">
  76. <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
  77. </option>
  78. <option name="A">
  79. <argument name="x" required="true">
  80. <para>The announcement to playback in all devices</para>
  81. </argument>
  82. <para>Play an announcement simultaneously to all paged participants</para>
  83. </option>
  84. <option name="n">
  85. <para>Do not play simultaneous announcement to caller (implies <literal>A(x)</literal>)</para>
  86. </option>
  87. </optionlist>
  88. </parameter>
  89. <parameter name="timeout">
  90. <para>Specify the length of time that the system will attempt to connect a call.
  91. After this duration, any intercom calls that have not been answered will be hung up by the
  92. system.</para>
  93. </parameter>
  94. </syntax>
  95. <description>
  96. <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
  97. and dumps them into a conference bridge as muted participants. The original
  98. caller is dumped into the conference as a speaker and the room is
  99. destroyed when the original callers leaves.</para>
  100. </description>
  101. <see-also>
  102. <ref type="application">MeetMe</ref>
  103. </see-also>
  104. </application>
  105. ***/
  106. static const char * const app_page= "Page";
  107. enum page_opt_flags {
  108. PAGE_DUPLEX = (1 << 0),
  109. PAGE_QUIET = (1 << 1),
  110. PAGE_RECORD = (1 << 2),
  111. PAGE_SKIP = (1 << 3),
  112. PAGE_IGNORE_FORWARDS = (1 << 4),
  113. PAGE_ANNOUNCE = (1 << 5),
  114. PAGE_NOCALLERANNOUNCE = (1 << 6),
  115. };
  116. enum {
  117. OPT_ARG_ANNOUNCE = 0,
  118. OPT_ARG_ARRAY_SIZE = 1,
  119. };
  120. AST_APP_OPTIONS(page_opts, {
  121. AST_APP_OPTION('d', PAGE_DUPLEX),
  122. AST_APP_OPTION('q', PAGE_QUIET),
  123. AST_APP_OPTION('r', PAGE_RECORD),
  124. AST_APP_OPTION('s', PAGE_SKIP),
  125. AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
  126. AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
  127. AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
  128. AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
  129. });
  130. static int page_exec(struct ast_channel *chan, const char *data)
  131. {
  132. char *tech, *resource, *tmp;
  133. char meetmeopts[128], originator[AST_CHANNEL_NAME], *opts[OPT_ARG_ARRAY_SIZE];
  134. struct ast_flags flags = { 0 };
  135. unsigned int confid = ast_random();
  136. struct ast_app *app;
  137. int res = 0, pos = 0, i = 0;
  138. struct ast_dial **dial_list;
  139. unsigned int num_dials;
  140. int timeout = 0;
  141. char *parse;
  142. AST_DECLARE_APP_ARGS(args,
  143. AST_APP_ARG(devices);
  144. AST_APP_ARG(options);
  145. AST_APP_ARG(timeout);
  146. );
  147. if (ast_strlen_zero(data)) {
  148. ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
  149. return -1;
  150. }
  151. if (!(app = pbx_findapp("MeetMe"))) {
  152. ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
  153. return -1;
  154. };
  155. parse = ast_strdupa(data);
  156. AST_STANDARD_APP_ARGS(args, parse);
  157. ast_copy_string(originator, chan->name, sizeof(originator));
  158. if ((tmp = strchr(originator, '-'))) {
  159. *tmp = '\0';
  160. }
  161. if (!ast_strlen_zero(args.options)) {
  162. ast_app_parse_options(page_opts, &flags, opts, args.options);
  163. }
  164. if (!ast_strlen_zero(args.timeout)) {
  165. timeout = atoi(args.timeout);
  166. }
  167. if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE])) {
  168. snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)G(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
  169. (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
  170. } else {
  171. snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
  172. (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
  173. }
  174. /* Count number of extensions in list by number of ampersands + 1 */
  175. num_dials = 1;
  176. tmp = args.devices;
  177. while (*tmp) {
  178. if (*tmp == '&') {
  179. num_dials++;
  180. }
  181. tmp++;
  182. }
  183. if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
  184. ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
  185. return -1;
  186. }
  187. /* Go through parsing/calling each device */
  188. while ((tech = strsep(&args.devices, "&"))) {
  189. int state = 0;
  190. struct ast_dial *dial = NULL;
  191. /* don't call the originating device */
  192. if (!strcasecmp(tech, originator))
  193. continue;
  194. /* If no resource is available, continue on */
  195. if (!(resource = strchr(tech, '/'))) {
  196. ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
  197. continue;
  198. }
  199. /* Ensure device is not in use if skip option is enabled */
  200. if (ast_test_flag(&flags, PAGE_SKIP)) {
  201. state = ast_device_state(tech);
  202. if (state == AST_DEVICE_UNKNOWN) {
  203. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
  204. } else if (state != AST_DEVICE_NOT_INUSE) {
  205. ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
  206. continue;
  207. }
  208. }
  209. *resource++ = '\0';
  210. /* Create a dialing structure */
  211. if (!(dial = ast_dial_create())) {
  212. ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
  213. continue;
  214. }
  215. /* Append technology and resource */
  216. if (ast_dial_append(dial, tech, resource) == -1) {
  217. ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
  218. continue;
  219. }
  220. /* Set ANSWER_EXEC as global option */
  221. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, meetmeopts);
  222. if (timeout) {
  223. ast_dial_set_global_timeout(dial, timeout * 1000);
  224. }
  225. if (ast_test_flag(&flags, PAGE_IGNORE_FORWARDS)) {
  226. ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
  227. }
  228. /* Run this dial in async mode */
  229. ast_dial_run(dial, chan, 1);
  230. /* Put in our dialing array */
  231. dial_list[pos++] = dial;
  232. }
  233. if (!ast_test_flag(&flags, PAGE_QUIET)) {
  234. res = ast_streamfile(chan, "beep", chan->language);
  235. if (!res)
  236. res = ast_waitstream(chan, "");
  237. }
  238. if (!res) {
  239. /* Default behaviour */
  240. snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"),
  241. (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
  242. if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE]) &&
  243. !ast_test_flag(&flags, PAGE_NOCALLERANNOUNCE)) {
  244. snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxdG(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"),
  245. (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
  246. }
  247. pbx_exec(chan, app, meetmeopts);
  248. }
  249. /* Go through each dial attempt cancelling, joining, and destroying */
  250. for (i = 0; i < pos; i++) {
  251. struct ast_dial *dial = dial_list[i];
  252. /* We have to wait for the async thread to exit as it's possible Meetme won't throw them out immediately */
  253. ast_dial_join(dial);
  254. /* Hangup all channels */
  255. ast_dial_hangup(dial);
  256. /* Destroy dialing structure */
  257. ast_dial_destroy(dial);
  258. }
  259. return -1;
  260. }
  261. static int unload_module(void)
  262. {
  263. return ast_unregister_application(app_page);
  264. }
  265. static int load_module(void)
  266. {
  267. return ast_register_application_xml(app_page, page_exec);
  268. }
  269. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");