app_parkandannounce.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * ParkAndAnnounce application for Asterisk
  5. * Author: Ben Miller <bgmiller@dccinc.com>
  6. * With TONS of help from Mark!
  7. *
  8. * Asterisk is Copyrighted as follows
  9. * Copyright (C) 1999, Mark Spencer
  10. *
  11. * Mark Spencer <markster@linux-support.net>
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License
  15. */
  16. #include <sys/types.h>
  17. #include <asterisk/file.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/channel.h>
  20. #include <asterisk/channel_pvt.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/module.h>
  23. #include <asterisk/features.h>
  24. #include <asterisk/options.h>
  25. #include <asterisk/logger.h>
  26. #include <asterisk/say.h>
  27. #include <asterisk/lock.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. static char *tdesc = "Call Parking and Announce Application";
  33. static char *app = "ParkAndAnnounce";
  34. static char *synopsis = "Park and Announce";
  35. static char *descrip =
  36. " ParkAndAnnounce(announce:template|timeout|dial|[return_context]):\n"
  37. "Park a call into the parkinglot and announce the call over the console.\n"
  38. "announce template: colon separated list of files to announce, the word PARKED\n"
  39. " will be replaced by a say_digits of the ext the call is parked in\n"
  40. "timeout: time in seconds before the call returns into the return context.\n"
  41. "dial: The app_dial style resource to call to make the announcement. Console/dsp calls the console.\n"
  42. "return_context: the goto style label to jump the call back into after timeout. default=prio+1\n";
  43. STANDARD_LOCAL_USER;
  44. LOCAL_USER_DECL;
  45. static int parkandannounce_exec(struct ast_channel *chan, void *data)
  46. {
  47. int res=0;
  48. char *return_context;
  49. int l, lot, timeout = 0, dres;
  50. char *working, *context, *exten, *priority, *dial, *dialtech, *dialstr;
  51. char *template, *tpl_working, *tpl_current;
  52. char *tmp[100];
  53. int looptemp=0,i=0;
  54. char *s,*orig_s;
  55. struct ast_channel *dchan;
  56. int outstate;
  57. struct localuser *u;
  58. if(!data || (data && !strlen(data))) {
  59. ast_log(LOG_WARNING, "ParkAndAnnounce requires arguments: (announce:template|timeout|dial|[return_context])\n");
  60. return -1;
  61. }
  62. l=strlen(data)+2;
  63. orig_s=malloc(l);
  64. if(!orig_s) {
  65. ast_log(LOG_WARNING, "Out of memory\n");
  66. return -1;
  67. }
  68. s=orig_s;
  69. strncpy(s,data,l);
  70. template=strsep(&s,"|");
  71. if(! template) {
  72. ast_log(LOG_WARNING, "PARK: An announce template must be defined\n");
  73. free(orig_s);
  74. return -1;
  75. }
  76. if(s) {
  77. timeout = atoi(strsep(&s, "|"));
  78. timeout *= 1000;
  79. }
  80. dial=strsep(&s, "|");
  81. if(!dial) {
  82. ast_log(LOG_WARNING, "PARK: A dial resource must be specified i.e: Console/dsp or Zap/g1/5551212\n");
  83. free(orig_s);
  84. return -1;
  85. } else {
  86. dialtech=strsep(&dial, "/");
  87. dialstr=dial;
  88. ast_verbose( VERBOSE_PREFIX_3 "Dial Tech,String: (%s,%s)\n", dialtech,dialstr);
  89. }
  90. return_context = s;
  91. if(return_context != NULL) {
  92. /* set the return context. Code borrowed from the Goto builtin */
  93. working = return_context;
  94. context = strsep(&working, "|");
  95. exten = strsep(&working, "|");
  96. if(!exten) {
  97. /* Only a priority in this one */
  98. priority = context;
  99. exten = NULL;
  100. context = NULL;
  101. } else {
  102. priority = strsep(&working, "|");
  103. if(!priority) {
  104. /* Only an extension and priority in this one */
  105. priority = exten;
  106. exten = context;
  107. context = NULL;
  108. }
  109. }
  110. if(atoi(priority) < 0) {
  111. ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", priority);
  112. free(orig_s);
  113. return -1;
  114. }
  115. /* At this point we have a priority and maybe an extension and a context */
  116. chan->priority = atoi(priority);
  117. if(exten && strcasecmp(exten, "BYEXTENSION"))
  118. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  119. if(context)
  120. strncpy(chan->context, context, sizeof(chan->context)-1);
  121. } else { /* increment the priority by default*/
  122. chan->priority++;
  123. }
  124. if(option_verbose > 2) {
  125. ast_verbose( VERBOSE_PREFIX_3 "Return Context: (%s,%s,%d) ID: %s\n", chan->context,chan->exten, chan->priority, chan->callerid);
  126. if(!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid)) {
  127. ast_verbose( VERBOSE_PREFIX_3 "Warning: Return Context Invalid, call will return to default|s\n");
  128. }
  129. }
  130. LOCAL_USER_ADD(u);
  131. /* we are using masq_park here to protect * from touching the channel once we park it. If the channel comes out of timeout
  132. before we are done announcing and the channel is messed with, Kablooeee. So we use Masq to prevent this. */
  133. ast_masq_park_call(chan, NULL, timeout, &lot);
  134. res=-1;
  135. ast_verbose( VERBOSE_PREFIX_3 "Call Parking Called, lot: %d, timeout: %d, context: %s\n", lot, timeout, return_context);
  136. /* Now place the call to the extention */
  137. dchan = ast_request_and_dial(dialtech, AST_FORMAT_SLINEAR, dialstr,30000, &outstate, chan->callerid);
  138. if(dchan) {
  139. if(dchan->_state == AST_STATE_UP) {
  140. if(option_verbose > 3)
  141. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was answered.\n", dchan->name);
  142. } else {
  143. if(option_verbose > 3)
  144. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was never answered.\n", dchan->name);
  145. ast_log(LOG_WARNING, "PARK: Channel %s was never answered for the announce.\n", dchan->name);
  146. ast_hangup(dchan);
  147. free(orig_s);
  148. LOCAL_USER_REMOVE(u);
  149. return -1;
  150. }
  151. } else {
  152. ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n");
  153. free(orig_s);
  154. LOCAL_USER_REMOVE(u);
  155. return -1;
  156. }
  157. ast_stopstream(dchan);
  158. /* now we have the call placed and are ready to play stuff to it */
  159. ast_verbose(VERBOSE_PREFIX_4 "Announce Template:%s\n", template);
  160. tpl_working = template;
  161. tpl_current=strsep(&tpl_working, ":");
  162. while(tpl_current && looptemp < sizeof(tmp)) {
  163. tmp[looptemp]=tpl_current;
  164. looptemp++;
  165. tpl_current=strsep(&tpl_working,":");
  166. }
  167. for(i=0; i<looptemp; i++) {
  168. ast_verbose(VERBOSE_PREFIX_4 "Announce:%s\n", tmp[i]);
  169. if(!strcmp(tmp[i], "PARKED")) {
  170. ast_say_digits(dchan, lot, "", dchan->language);
  171. } else {
  172. dres = ast_streamfile(dchan, tmp[i], dchan->language);
  173. if(!dres) {
  174. dres = ast_waitstream(dchan, "");
  175. } else {
  176. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", tmp[i], dchan->name);
  177. dres = 0;
  178. }
  179. }
  180. }
  181. ast_stopstream(dchan);
  182. ast_hangup(dchan);
  183. LOCAL_USER_REMOVE(u);
  184. free(orig_s);
  185. return res;
  186. }
  187. int unload_module(void)
  188. {
  189. STANDARD_HANGUP_LOCALUSERS;
  190. return ast_unregister_application(app);
  191. }
  192. int load_module(void)
  193. {
  194. /* return ast_register_application(app, park_exec); */
  195. return ast_register_application(app, parkandannounce_exec, synopsis, descrip);
  196. }
  197. char *description(void)
  198. {
  199. return tdesc;
  200. }
  201. int usecount(void)
  202. {
  203. int res;
  204. STANDARD_USECOUNT(res);
  205. return res;
  206. }
  207. char *key()
  208. {
  209. return ASTERISK_GPL_KEY;
  210. }