app_dial.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Trivial application to dial a channel and send an URL on answer
  5. *
  6. * Copyright (C) 1999-2004, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/options.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/translate.h>
  21. #include <asterisk/say.h>
  22. #include <asterisk/config.h>
  23. #include <asterisk/features.h>
  24. #include <asterisk/musiconhold.h>
  25. #include <asterisk/callerid.h>
  26. #include <asterisk/utils.h>
  27. #include <asterisk/app.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <sys/time.h>
  35. #include <sys/signal.h>
  36. #include <netinet/in.h>
  37. static char *tdesc = "Dialing Application";
  38. static char *app = "Dial";
  39. static char *synopsis = "Place a call and connect to the current channel";
  40. static char *descrip =
  41. " Dial(Technology/resource[&Technology2/resource2...][|timeout][|options][|URL]):\n"
  42. "Requests one or more channels and places specified outgoing calls on them.\n"
  43. "As soon as a channel answers, the Dial app will answer the originating\n"
  44. "channel (if it needs to be answered) and will bridge a call with the channel\n"
  45. "which first answered. All other calls placed by the Dial app will be hung up\n"
  46. "If a timeout is not specified, the Dial application will wait indefinitely\n"
  47. "until either one of the called channels answers, the user hangs up, or all\n"
  48. "channels return busy or error. In general, the dialer will return 0 if it\n"
  49. "was unable to place the call, or the timeout expired. However, if all\n"
  50. "channels were busy, and there exists an extension with priority n+101 (where\n"
  51. "n is the priority of the dialer instance), then it will be the next\n"
  52. "executed extension (this allows you to setup different behavior on busy from\n"
  53. "no-answer).\n"
  54. " This application returns -1 if the originating channel hangs up, or if the\n"
  55. "call is bridged and either of the parties in the bridge terminate the call.\n"
  56. "The option string may contain zero or more of the following characters:\n"
  57. " 't' -- allow the called user transfer the calling user by hitting #.\n"
  58. " 'T' -- allow the calling user to transfer the call by hitting #.\n"
  59. " 'f' -- Forces callerid to be set as the extension of the line \n"
  60. " making/redirecting the outgoing call. For example, some PSTNs\n"
  61. " don't allow callerids from other extensions then the ones\n"
  62. " that are assigned to you.\n"
  63. " 'r' -- indicate ringing to the calling party, pass no audio until answered.\n"
  64. " 'm' -- provide hold music to the calling party until answered.\n"
  65. " 'M(x) -- Executes the macro (x) upon connect of the call\n"
  66. " 'h' -- allow callee to hang up by hitting *.\n"
  67. " 'H' -- allow caller to hang up by hitting *.\n"
  68. " 'C' -- reset call detail record for this call.\n"
  69. " 'P[(x)]' -- privacy mode, using 'x' as database if provided.\n"
  70. " 'g' -- goes on in context if the destination channel hangs up\n"
  71. " 'A(x)' -- play an announcement to the called party, using x as file\n"
  72. " 'S(x)' -- hangup the call after x seconds AFTER called party picked up\n"
  73. " 'D([digits])' -- Send DTMF digit string *after* called party has answered\n"
  74. " but before the bridge. (w=500ms sec pause)\n"
  75. " 'L(x[:y][:z])' -- Limit the call to 'x' ms warning when 'y' ms are left\n"
  76. " repeated every 'z' ms) Only 'x' is required, 'y' and 'z' are optional.\n"
  77. " The following special variables are optional:\n"
  78. " * LIMIT_PLAYAUDIO_CALLER yes|no (default yes)\n"
  79. " Play sounds to the caller.\n"
  80. " * LIMIT_PLAYAUDIO_CALLEE yes|no\n"
  81. " Play sounds to the callee.\n"
  82. " * LIMIT_TIMEOUT_FILE File to play when time is up.\n"
  83. " * LIMIT_CONNECT_FILE File to play when call begins.\n"
  84. " * LIMIT_WARNING_FILE File to play as warning if 'y' is defined.\n"
  85. " 'timeleft' is a special sound macro to auto-say the time \n"
  86. " left and is the default.\n\n"
  87. " In addition to transferring the call, a call may be parked and then picked\n"
  88. "up by another user.\n"
  89. " The optional URL will be sent to the called party if the channel supports it.\n"
  90. " This application sets the following channel variables upon completion:\n"
  91. " DIALEDTIME Time from dial to answer\n"
  92. " ANSWEREDTIME Time for actual call\n"
  93. " DIALSTATUS The status of the call as a text string, one of\n"
  94. " CHANUNAVAIL | CONGESTION | NOANSWER | BUSY | ANSWER | CANCEL\n"
  95. "";
  96. /* We define a customer "local user" structure because we
  97. use it not only for keeping track of what is in use but
  98. also for keeping track of who we're dialing. */
  99. struct localuser {
  100. struct ast_channel *chan;
  101. int stillgoing;
  102. int allowredirect_in;
  103. int allowredirect_out;
  104. int ringbackonly;
  105. int musiconhold;
  106. int allowdisconnect_in;
  107. int allowdisconnect_out;
  108. int forcecallerid;
  109. int noforwardhtml;
  110. struct localuser *next;
  111. };
  112. LOCAL_USER_DECL;
  113. static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
  114. {
  115. /* Hang up a tree of stuff */
  116. struct localuser *oo;
  117. while(outgoing) {
  118. /* Hangup any existing lines we have open */
  119. if (outgoing->chan && (outgoing->chan != exception))
  120. ast_hangup(outgoing->chan);
  121. oo = outgoing;
  122. outgoing=outgoing->next;
  123. free(oo);
  124. }
  125. }
  126. #define AST_MAX_WATCHERS 256
  127. static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir_in, int *allowredir_out, int *allowdisconnect_in, int *allowdisconnect_out, int *noforwardhtml, int *sentringing, char *status, size_t statussize)
  128. {
  129. struct localuser *o;
  130. int found;
  131. int numlines;
  132. int numbusy = 0;
  133. int numcongestion = 0;
  134. int numnochan = 0;
  135. int orig = *to;
  136. struct ast_frame *f;
  137. struct ast_channel *peer = NULL;
  138. struct ast_channel *watchers[AST_MAX_WATCHERS];
  139. int pos;
  140. int single;
  141. struct ast_channel *winner;
  142. single = (outgoing && !outgoing->next && !outgoing->musiconhold && !outgoing->ringbackonly);
  143. if (single) {
  144. /* Turn off hold music, etc */
  145. ast_deactivate_generator(in);
  146. /* If we are calling a single channel, make them compatible for in-band tone purpose */
  147. ast_channel_make_compatible(outgoing->chan, in);
  148. }
  149. while(*to && !peer) {
  150. o = outgoing;
  151. found = -1;
  152. pos = 1;
  153. numlines = 0;
  154. watchers[0] = in;
  155. while(o) {
  156. /* Keep track of important channels */
  157. if (o->stillgoing && o->chan) {
  158. watchers[pos++] = o->chan;
  159. found = 1;
  160. }
  161. o = o->next;
  162. numlines++;
  163. }
  164. if (found < 0) {
  165. if (numlines == (numbusy + numcongestion + numnochan)) {
  166. if (option_verbose > 2)
  167. ast_verbose( VERBOSE_PREFIX_2 "Everyone is busy/congested at this time\n");
  168. if (numbusy)
  169. strncpy(status, "BUSY", statussize - 1);
  170. else if (numcongestion)
  171. strncpy(status, "CONGESTION", statussize - 1);
  172. else if (numnochan)
  173. strncpy(status, "CHANUNAVAIL", statussize - 1);
  174. /* See if there is a special busy message */
  175. if (ast_exists_extension(in, in->context, in->exten, in->priority + 101, in->callerid))
  176. in->priority+=100;
  177. } else {
  178. if (option_verbose > 2)
  179. ast_verbose( VERBOSE_PREFIX_2 "No one is available to answer at this time\n");
  180. }
  181. *to = 0;
  182. return NULL;
  183. }
  184. winner = ast_waitfor_n(watchers, pos, to);
  185. o = outgoing;
  186. while(o) {
  187. if (o->stillgoing && o->chan && (o->chan->_state == AST_STATE_UP)) {
  188. if (!peer) {
  189. if (option_verbose > 2)
  190. ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
  191. peer = o->chan;
  192. *allowredir_in = o->allowredirect_in;
  193. *allowredir_out = o->allowredirect_out;
  194. *allowdisconnect_in = o->allowdisconnect_in;
  195. *allowdisconnect_out = o->allowdisconnect_out;
  196. *noforwardhtml = o->noforwardhtml;
  197. }
  198. } else if (o->chan && (o->chan == winner)) {
  199. if (!ast_strlen_zero(o->chan->call_forward)) {
  200. char tmpchan[256]="";
  201. char *stuff;
  202. char *tech;
  203. strncpy(tmpchan, o->chan->call_forward, sizeof(tmpchan) - 1);
  204. if ((stuff = strchr(tmpchan, '/'))) {
  205. *stuff = '\0';
  206. stuff++;
  207. tech = tmpchan;
  208. } else {
  209. snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
  210. stuff = tmpchan;
  211. tech = "Local";
  212. }
  213. /* Before processing channel, go ahead and check for forwarding */
  214. if (option_verbose > 2)
  215. ast_verbose(VERBOSE_PREFIX_3 "Now forwarding %s to '%s/%s' (thanks to %s)\n", in->name, tech, stuff, o->chan->name);
  216. /* Setup parameters */
  217. o->chan = ast_request(tech, in->nativeformats, stuff);
  218. if (!o->chan) {
  219. ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s'\n", tech, stuff);
  220. o->stillgoing = 0;
  221. numnochan++;
  222. } else {
  223. if (o->chan->callerid)
  224. free(o->chan->callerid);
  225. o->chan->callerid = NULL;
  226. if (o->forcecallerid) {
  227. char *newcid = NULL;
  228. if (strlen(in->macroexten))
  229. newcid = in->macroexten;
  230. else
  231. newcid = in->exten;
  232. o->chan->callerid = strdup(newcid);
  233. strncpy(o->chan->accountcode, winner->accountcode, sizeof(o->chan->accountcode) - 1);
  234. o->chan->cdrflags = winner->cdrflags;
  235. if (!o->chan->callerid)
  236. ast_log(LOG_WARNING, "Out of memory\n");
  237. } else {
  238. if (in->callerid) {
  239. o->chan->callerid = strdup(in->callerid);
  240. if (!o->chan->callerid)
  241. ast_log(LOG_WARNING, "Out of memory\n");
  242. }
  243. strncpy(o->chan->accountcode, in->accountcode, sizeof(o->chan->accountcode) - 1);
  244. o->chan->cdrflags = in->cdrflags;
  245. }
  246. if (in->ani) {
  247. if (o->chan->ani)
  248. free(o->chan->ani);
  249. o->chan->ani = malloc(strlen(in->ani) + 1);
  250. if (o->chan->ani)
  251. strncpy(o->chan->ani, in->ani, strlen(in->ani));
  252. else
  253. ast_log(LOG_WARNING, "Out of memory\n");
  254. }
  255. if (o->chan->rdnis)
  256. free(o->chan->rdnis);
  257. if (!ast_strlen_zero(in->macroexten))
  258. o->chan->rdnis = strdup(in->macroexten);
  259. else
  260. o->chan->rdnis = strdup(in->exten);
  261. if (ast_call(o->chan, tmpchan, 0)) {
  262. ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
  263. o->stillgoing = 0;
  264. ast_hangup(o->chan);
  265. o->chan = NULL;
  266. numnochan++;
  267. }
  268. }
  269. /* Hangup the original channel now, in case we needed it */
  270. ast_hangup(winner);
  271. continue;
  272. }
  273. f = ast_read(winner);
  274. if (f) {
  275. if (f->frametype == AST_FRAME_CONTROL) {
  276. switch(f->subclass) {
  277. case AST_CONTROL_ANSWER:
  278. /* This is our guy if someone answered. */
  279. if (!peer) {
  280. if (option_verbose > 2)
  281. ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
  282. peer = o->chan;
  283. *allowredir_in = o->allowredirect_in;
  284. *allowredir_out = o->allowredirect_out;
  285. *allowdisconnect_in = o->allowdisconnect_in;
  286. *allowdisconnect_out = o->allowdisconnect_out;
  287. *noforwardhtml = o->noforwardhtml;
  288. }
  289. break;
  290. case AST_CONTROL_BUSY:
  291. if (option_verbose > 2)
  292. ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
  293. in->hangupcause = o->chan->hangupcause;
  294. ast_hangup(o->chan);
  295. o->chan = NULL;
  296. o->stillgoing = 0;
  297. if (in->cdr)
  298. ast_cdr_busy(in->cdr);
  299. numbusy++;
  300. break;
  301. case AST_CONTROL_CONGESTION:
  302. if (option_verbose > 2)
  303. ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
  304. in->hangupcause = o->chan->hangupcause;
  305. ast_hangup(o->chan);
  306. o->chan = NULL;
  307. o->stillgoing = 0;
  308. if (in->cdr)
  309. ast_cdr_busy(in->cdr);
  310. numcongestion++;
  311. break;
  312. case AST_CONTROL_RINGING:
  313. if (option_verbose > 2)
  314. ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
  315. if (!(*sentringing) && !outgoing->musiconhold) {
  316. ast_indicate(in, AST_CONTROL_RINGING);
  317. (*sentringing)++;
  318. }
  319. break;
  320. case AST_CONTROL_PROGRESS:
  321. if (option_verbose > 2)
  322. ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", o->chan->name,in->name);
  323. if (!outgoing->ringbackonly)
  324. ast_indicate(in, AST_CONTROL_PROGRESS);
  325. break;
  326. case AST_CONTROL_OFFHOOK:
  327. /* Ignore going off hook */
  328. break;
  329. case -1:
  330. if (!(outgoing->ringbackonly || outgoing->musiconhold)) {
  331. if (option_verbose > 2)
  332. ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", o->chan->name);
  333. ast_indicate(in, -1);
  334. (*sentringing) = 0;
  335. }
  336. break;
  337. default:
  338. ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
  339. }
  340. } else if (single && (f->frametype == AST_FRAME_VOICE) &&
  341. !(outgoing->ringbackonly || outgoing->musiconhold)) {
  342. if (ast_write(in, f))
  343. ast_log(LOG_WARNING, "Unable to forward frame\n");
  344. } else if (single && (f->frametype == AST_FRAME_IMAGE) &&
  345. !(outgoing->ringbackonly || outgoing->musiconhold)) {
  346. if (ast_write(in, f))
  347. ast_log(LOG_WARNING, "Unable to forward image\n");
  348. } else if (single && (f->frametype == AST_FRAME_TEXT) &&
  349. !(outgoing->ringbackonly || outgoing->musiconhold)) {
  350. if (ast_write(in, f))
  351. ast_log(LOG_WARNING, "Unable to text\n");
  352. } else if (single && (f->frametype == AST_FRAME_HTML) && !outgoing->noforwardhtml) {
  353. ast_channel_sendhtml(in, f->subclass, f->data, f->datalen);
  354. }
  355. ast_frfree(f);
  356. } else {
  357. in->hangupcause = o->chan->hangupcause;
  358. ast_hangup(o->chan);
  359. o->chan = NULL;
  360. o->stillgoing = 0;
  361. }
  362. }
  363. o = o->next;
  364. }
  365. if (winner == in) {
  366. f = ast_read(in);
  367. #if 0
  368. if (f && (f->frametype != AST_FRAME_VOICE))
  369. printf("Frame type: %d, %d\n", f->frametype, f->subclass);
  370. else if (!f || (f->frametype != AST_FRAME_VOICE))
  371. printf("Hangup received on %s\n", in->name);
  372. #endif
  373. if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
  374. /* Got hung up */
  375. *to=-1;
  376. strncpy(status, "CANCEL", statussize - 1);
  377. if (f)
  378. ast_frfree(f);
  379. return NULL;
  380. }
  381. if (f && (f->frametype == AST_FRAME_DTMF) && *allowdisconnect_out &&
  382. (f->subclass == '*')) {
  383. if (option_verbose > 3)
  384. ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
  385. *to=0;
  386. strcpy(status, "CANCEL");
  387. ast_frfree(f);
  388. return NULL;
  389. }
  390. if (single && f && (f->frametype == AST_FRAME_HTML) && !outgoing->noforwardhtml)
  391. ast_channel_sendhtml(outgoing->chan, f->subclass, f->data, f->datalen);
  392. if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF))) {
  393. if (ast_write(outgoing->chan, f))
  394. ast_log(LOG_WARNING, "Unable to forward voice\n");
  395. }
  396. ast_frfree(f);
  397. }
  398. if (!*to && (option_verbose > 2))
  399. ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
  400. }
  401. return peer;
  402. }
  403. static int dial_exec(struct ast_channel *chan, void *data)
  404. {
  405. int res=-1;
  406. struct localuser *u;
  407. char *info, *peers, *timeout, *tech, *number, *rest, *cur;
  408. char privdb[256] = "", *s;
  409. char announcemsg[256] = "", *ann;
  410. struct localuser *outgoing=NULL, *tmp;
  411. struct ast_channel *peer;
  412. int to;
  413. int allowredir_in=0;
  414. int allowredir_out=0;
  415. int allowdisconnect_in=0;
  416. int allowdisconnect_out=0;
  417. int noforwardhtml=0;
  418. int hasmacro = 0;
  419. int privacy=0;
  420. int announce=0;
  421. int resetcdr=0;
  422. char numsubst[AST_MAX_EXTENSION];
  423. char restofit[AST_MAX_EXTENSION];
  424. char *transfer = NULL;
  425. char *newnum;
  426. char callerid[256] = "", *l, *n;
  427. char *url=NULL; /* JDG */
  428. struct ast_var_t *current;
  429. struct varshead *headp, *newheadp;
  430. struct ast_var_t *newvar;
  431. int go_on=0;
  432. unsigned int calldurationlimit=0;
  433. char *cdl;
  434. time_t now;
  435. struct ast_bridge_config config;
  436. long timelimit = 0;
  437. long play_warning = 0;
  438. long warning_freq=0;
  439. char *warning_sound=NULL;
  440. char *end_sound=NULL;
  441. char *start_sound=NULL;
  442. char *limitptr;
  443. char limitdata[256];
  444. char *sdtmfptr;
  445. char sdtmfdata[256] = "";
  446. char *stack,*var;
  447. char *mac = NULL, macroname[256] = "";
  448. char status[256]="";
  449. char toast[80];
  450. int play_to_caller=0,play_to_callee=0;
  451. int playargs=0, sentringing=0, moh=0;
  452. int digit = 0;
  453. time_t start_time, answer_time, end_time;
  454. if (!data) {
  455. ast_log(LOG_WARNING, "Dial requires an argument (technology1/number1&technology2/number2...|optional timeout|options)\n");
  456. return -1;
  457. }
  458. if (!(info = ast_strdupa(data))) {
  459. ast_log(LOG_WARNING, "Unable to dupe data :(\n");
  460. return -1;
  461. }
  462. LOCAL_USER_ADD(u);
  463. peers = info;
  464. if (peers) {
  465. timeout = strchr(info, '|');
  466. if (timeout) {
  467. *timeout = '\0';
  468. timeout++;
  469. transfer = strchr(timeout, '|');
  470. if (transfer) {
  471. *transfer = '\0';
  472. transfer++;
  473. /* JDG */
  474. url = strchr(transfer, '|');
  475. if (url) {
  476. *url = '\0';
  477. url++;
  478. ast_log(LOG_DEBUG, "DIAL WITH URL=%s_\n", url);
  479. } else
  480. ast_log(LOG_DEBUG, "SIMPLE DIAL (NO URL)\n");
  481. /* /JDG */
  482. }
  483. }
  484. } else
  485. timeout = NULL;
  486. if (!peers || ast_strlen_zero(peers)) {
  487. ast_log(LOG_WARNING, "Dial argument takes format (technology1/number1&technology2/number2...|optional timeout)\n");
  488. goto out;
  489. }
  490. if (transfer) {
  491. /* Extract call duration limit */
  492. if ((cdl = strstr(transfer, "S("))) {
  493. calldurationlimit=atoi(cdl+2);
  494. if (option_verbose > 2)
  495. ast_verbose(VERBOSE_PREFIX_3 "Setting call duration limit to %i seconds.\n",calldurationlimit);
  496. }
  497. /* DTMF SCRIPT*/
  498. if ((sdtmfptr = strstr(transfer, "D("))) {
  499. strncpy(sdtmfdata, sdtmfptr + 2, sizeof(sdtmfdata) - 1);
  500. /* Overwrite with X's what was the sdtmf info */
  501. while (*sdtmfptr && (*sdtmfptr != ')'))
  502. *(sdtmfptr++) = 'X';
  503. if (*sdtmfptr)
  504. *sdtmfptr = 'X';
  505. /* Now find the end */
  506. sdtmfptr = strchr(sdtmfdata, ')');
  507. if (sdtmfptr)
  508. *sdtmfptr = '\0';
  509. else
  510. ast_log(LOG_WARNING, "D( Data lacking trailing ')'\n");
  511. }
  512. /* XXX LIMIT SUPPORT */
  513. if ((limitptr = strstr(transfer, "L("))) {
  514. strncpy(limitdata, limitptr + 2, sizeof(limitdata) - 1);
  515. /* Overwrite with X's what was the limit info */
  516. while(*limitptr && (*limitptr != ')'))
  517. *(limitptr++) = 'X';
  518. if (*limitptr)
  519. *limitptr = 'X';
  520. /* Now find the end */
  521. limitptr = strchr(limitdata, ')');
  522. if (limitptr)
  523. *limitptr = '\0';
  524. else
  525. ast_log(LOG_WARNING, "Limit Data lacking trailing ')'\n");
  526. var = pbx_builtin_getvar_helper(chan,"LIMIT_PLAYAUDIO_CALLER");
  527. play_to_caller = var ? ast_true(var) : 1;
  528. var = pbx_builtin_getvar_helper(chan,"LIMIT_PLAYAUDIO_CALLEE");
  529. play_to_callee = var ? ast_true(var) : 0;
  530. if (!play_to_caller && !play_to_callee)
  531. play_to_caller=1;
  532. var = pbx_builtin_getvar_helper(chan,"LIMIT_WARNING_FILE");
  533. warning_sound = var ? var : "timeleft";
  534. var = pbx_builtin_getvar_helper(chan,"LIMIT_TIMEOUT_FILE");
  535. end_sound = var ? var : NULL;
  536. var = pbx_builtin_getvar_helper(chan,"LIMIT_CONNECT_FILE");
  537. start_sound = var ? var : NULL;
  538. var=stack=limitdata;
  539. var = strsep(&stack, ":");
  540. if (var) {
  541. timelimit = atol(var);
  542. playargs++;
  543. var = strsep(&stack, ":");
  544. if (var) {
  545. play_warning = atol(var);
  546. playargs++;
  547. var = strsep(&stack, ":");
  548. if(var) {
  549. warning_freq = atol(var);
  550. playargs++;
  551. }
  552. }
  553. }
  554. if (!timelimit) {
  555. timelimit=play_to_caller=play_to_callee=play_warning=warning_freq=0;
  556. warning_sound=NULL;
  557. }
  558. /* undo effect of S(x) in case they are both used */
  559. calldurationlimit=0;
  560. /* more efficient do it like S(x) does since no advanced opts*/
  561. if (!play_warning && !start_sound && !end_sound && timelimit) {
  562. calldurationlimit=timelimit/1000;
  563. timelimit=play_to_caller=play_to_callee=play_warning=warning_freq=0;
  564. } else if (option_verbose > 2) {
  565. ast_verbose(VERBOSE_PREFIX_3"Limit Data:\n");
  566. ast_verbose(VERBOSE_PREFIX_3"timelimit=%ld\n",timelimit);
  567. ast_verbose(VERBOSE_PREFIX_3"play_warning=%ld\n",play_warning);
  568. ast_verbose(VERBOSE_PREFIX_3"play_to_caller=%s\n",play_to_caller ? "yes" : "no");
  569. ast_verbose(VERBOSE_PREFIX_3"play_to_callee=%s\n",play_to_callee ? "yes" : "no");
  570. ast_verbose(VERBOSE_PREFIX_3"warning_freq=%ld\n",warning_freq);
  571. ast_verbose(VERBOSE_PREFIX_3"start_sound=%s\n",start_sound ? start_sound : "UNDEF");
  572. ast_verbose(VERBOSE_PREFIX_3"warning_sound=%s\n",warning_sound ? warning_sound : "UNDEF");
  573. ast_verbose(VERBOSE_PREFIX_3"end_sound=%s\n",end_sound ? end_sound : "UNDEF");
  574. }
  575. }
  576. /* XXX ANNOUNCE SUPPORT */
  577. if ((ann = strstr(transfer, "A("))) {
  578. announce = 1;
  579. strncpy(announcemsg, ann + 2, sizeof(announcemsg) - 1);
  580. /* Overwrite with X's what was the announce info */
  581. while(*ann && (*ann != ')'))
  582. *(ann++) = 'X';
  583. if (*ann)
  584. *ann = 'X';
  585. /* Now find the end of the privdb */
  586. ann = strchr(announcemsg, ')');
  587. if (ann)
  588. *ann = '\0';
  589. else {
  590. ast_log(LOG_WARNING, "Transfer with Announce spec lacking trailing ')'\n");
  591. announce = 0;
  592. }
  593. }
  594. /* Get the macroname from the dial option string */
  595. if ((mac = strstr(transfer, "M("))) {
  596. hasmacro = 1;
  597. strncpy(macroname, mac + 2, sizeof(macroname) - 1);
  598. while (*mac && (*mac != ')'))
  599. *(mac++) = 'X';
  600. if (*mac)
  601. *mac = 'X';
  602. else {
  603. ast_log(LOG_WARNING, "Could not find macro to which we should jump.\n");
  604. hasmacro = 0;
  605. }
  606. mac = strchr(macroname, ')');
  607. if (mac)
  608. *mac = '\0';
  609. else {
  610. ast_log(LOG_WARNING, "Macro flag set without trailing ')'\n");
  611. hasmacro = 0;
  612. }
  613. }
  614. /* Extract privacy info from transfer */
  615. if ((s = strstr(transfer, "P("))) {
  616. privacy = 1;
  617. strncpy(privdb, s + 2, sizeof(privdb) - 1);
  618. /* Overwrite with X's what was the privacy info */
  619. while(*s && (*s != ')'))
  620. *(s++) = 'X';
  621. if (*s)
  622. *s = 'X';
  623. /* Now find the end of the privdb */
  624. s = strchr(privdb, ')');
  625. if (s)
  626. *s = '\0';
  627. else {
  628. ast_log(LOG_WARNING, "Transfer with privacy lacking trailing ')'\n");
  629. privacy = 0;
  630. }
  631. } else if (strchr(transfer, 'P')) {
  632. /* No specified privdb */
  633. privacy = 1;
  634. } else if (strchr(transfer, 'C')) {
  635. resetcdr = 1;
  636. }
  637. }
  638. if (resetcdr && chan->cdr)
  639. ast_cdr_reset(chan->cdr, 0);
  640. if (ast_strlen_zero(privdb) && privacy) {
  641. /* If privdb is not specified and we are using privacy, copy from extension */
  642. strncpy(privdb, chan->exten, sizeof(privdb) - 1);
  643. }
  644. if (privacy) {
  645. if (chan->callerid)
  646. strncpy(callerid, chan->callerid, sizeof(callerid) - 1);
  647. else
  648. callerid[0] = '\0';
  649. ast_callerid_parse(callerid, &n, &l);
  650. if (l) {
  651. ast_shrink_phone_number(l);
  652. } else
  653. l = "";
  654. ast_log(LOG_NOTICE, "Privacy DB is '%s', privacy is %d, clid is '%s'\n", privdb, privacy, l);
  655. }
  656. cur = peers;
  657. do {
  658. /* Remember where to start next time */
  659. rest = strchr(cur, '&');
  660. if (rest) {
  661. *rest = 0;
  662. rest++;
  663. }
  664. /* Get a technology/[device:]number pair */
  665. tech = cur;
  666. number = strchr(tech, '/');
  667. if (!number) {
  668. ast_log(LOG_WARNING, "Dial argument takes format (technology1/[device:]number1&technology2/[device:]number2...|optional timeout)\n");
  669. goto out;
  670. }
  671. *number = '\0';
  672. number++;
  673. tmp = malloc(sizeof(struct localuser));
  674. if (!tmp) {
  675. ast_log(LOG_WARNING, "Out of memory\n");
  676. goto out;
  677. }
  678. memset(tmp, 0, sizeof(struct localuser));
  679. if (transfer) {
  680. if (strchr(transfer, 't'))
  681. tmp->allowredirect_in = 1;
  682. else tmp->allowredirect_in = 0;
  683. if (strchr(transfer, 'T'))
  684. tmp->allowredirect_out = 1;
  685. else tmp->allowredirect_out = 0;
  686. if (strchr(transfer, 'r'))
  687. tmp->ringbackonly = 1;
  688. else tmp->ringbackonly = 0;
  689. if (strchr(transfer, 'm'))
  690. tmp->musiconhold = 1;
  691. else tmp->musiconhold = 0;
  692. if (strchr(transfer, 'H'))
  693. allowdisconnect_out = tmp->allowdisconnect_out = 1;
  694. else allowdisconnect_out = tmp->allowdisconnect_out = 0;
  695. if(strchr(transfer, 'h'))
  696. allowdisconnect_in = tmp->allowdisconnect_in = 1;
  697. else allowdisconnect_in = tmp->allowdisconnect_in = 0;
  698. if(strchr(transfer, 'g'))
  699. go_on=1;
  700. if (strchr(transfer, 'f'))
  701. tmp->forcecallerid = 1;
  702. else tmp->forcecallerid = 0;
  703. if (url)
  704. tmp->noforwardhtml = 1;
  705. else tmp->noforwardhtml = 0;
  706. }
  707. strncpy(numsubst, number, sizeof(numsubst)-1);
  708. /* If we're dialing by extension, look at the extension to know what to dial */
  709. if ((newnum = strstr(numsubst, "BYEXTENSION"))) {
  710. strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)-1);
  711. snprintf(newnum, sizeof(numsubst) - (newnum - numsubst), "%s%s", chan->exten,restofit);
  712. if (option_debug)
  713. ast_log(LOG_DEBUG, "Dialing by extension %s\n", numsubst);
  714. }
  715. /* Request the peer */
  716. tmp->chan = ast_request(tech, chan->nativeformats, numsubst);
  717. if (!tmp->chan) {
  718. /* If we can't, just go on to the next call */
  719. ast_log(LOG_NOTICE, "Unable to create channel of type '%s'\n", tech);
  720. if (chan->cdr)
  721. ast_cdr_busy(chan->cdr);
  722. free(tmp);
  723. cur = rest;
  724. continue;
  725. }
  726. pbx_builtin_setvar_helper(tmp->chan, "DIALEDPEERNUMBER", numsubst);
  727. if (!ast_strlen_zero(tmp->chan->call_forward)) {
  728. char tmpchan[256]="";
  729. char *stuff;
  730. char *tech;
  731. strncpy(tmpchan, tmp->chan->call_forward, sizeof(tmpchan) - 1);
  732. if ((stuff = strchr(tmpchan, '/'))) {
  733. *stuff = '\0';
  734. stuff++;
  735. tech = tmpchan;
  736. } else {
  737. snprintf(tmpchan, sizeof(tmpchan), "%s@%s", tmp->chan->call_forward, tmp->chan->context);
  738. stuff = tmpchan;
  739. tech = "Local";
  740. }
  741. /* Before processing channel, go ahead and check for forwarding */
  742. if (option_verbose > 2)
  743. ast_verbose(VERBOSE_PREFIX_3 "Forwarding %s to '%s/%s' (thanks to %s)\n", chan->name, tech, stuff, tmp->chan->name);
  744. /* Setup parameters */
  745. ast_hangup(tmp->chan);
  746. tmp->chan = ast_request(tech, chan->nativeformats, stuff);
  747. if (!tmp->chan) {
  748. ast_log(LOG_NOTICE, "Unable to create local channel for call forward to '%s/%s'\n", tech, stuff);
  749. free(tmp);
  750. cur = rest;
  751. continue;
  752. }
  753. }
  754. /* If creating a SIP channel, look for a variable called */
  755. /* VXML_URL in the calling channel and copy it to the */
  756. /* new channel. */
  757. /* Check for ALERT_INFO in the SetVar list. This is for */
  758. /* SIP distinctive ring as per the RFC. For Cisco 7960s, */
  759. /* SetVar(ALERT_INFO=<x>) where x is an integer value 1-5. */
  760. /* However, the RFC says it should be a URL. -km- */
  761. headp=&chan->varshead;
  762. AST_LIST_TRAVERSE(headp,current,entries) {
  763. if (!strcasecmp(ast_var_name(current),"VXML_URL") ||
  764. !strcasecmp(ast_var_name(current), "ALERT_INFO") ||
  765. !strcasecmp(ast_var_name(current), "OSPTOKEN") ||
  766. !strcasecmp(ast_var_name(current), "OSPHANDLE"))
  767. {
  768. newvar=ast_var_assign(ast_var_name(current),ast_var_value(current));
  769. newheadp=&tmp->chan->varshead;
  770. AST_LIST_INSERT_HEAD(newheadp,newvar,entries);
  771. }
  772. }
  773. tmp->chan->appl = "AppDial";
  774. tmp->chan->data = "(Outgoing Line)";
  775. tmp->chan->whentohangup = 0;
  776. if (tmp->chan->callerid)
  777. free(tmp->chan->callerid);
  778. if (tmp->chan->ani)
  779. free(tmp->chan->ani);
  780. if (chan->callerid)
  781. tmp->chan->callerid = strdup(chan->callerid);
  782. else
  783. tmp->chan->callerid = NULL;
  784. /* Copy language from incoming to outgoing */
  785. strncpy(tmp->chan->language, chan->language, sizeof(tmp->chan->language) - 1);
  786. strncpy(tmp->chan->accountcode, chan->accountcode, sizeof(tmp->chan->accountcode) - 1);
  787. tmp->chan->cdrflags = chan->cdrflags;
  788. if (ast_strlen_zero(tmp->chan->musicclass))
  789. strncpy(tmp->chan->musicclass, chan->musicclass, sizeof(tmp->chan->musicclass) - 1);
  790. if (chan->ani)
  791. tmp->chan->ani = strdup(chan->ani);
  792. else
  793. tmp->chan->ani = NULL;
  794. /* Pass hidecallerid setting */
  795. tmp->chan->restrictcid = chan->restrictcid;
  796. /* Pass callingpres setting */
  797. tmp->chan->callingpres = chan->callingpres;
  798. /* Presense of ADSI CPE on outgoing channel follows ours */
  799. tmp->chan->adsicpe = chan->adsicpe;
  800. /* pass the digital flag */
  801. ast_dup_flag(tmp->chan, chan, AST_FLAG_DIGITAL);
  802. /* Place the call, but don't wait on the answer */
  803. res = ast_call(tmp->chan, numsubst, 0);
  804. /* Save the info in cdr's that we called them */
  805. if (chan->cdr)
  806. ast_cdr_setdestchan(chan->cdr, tmp->chan->name);
  807. /* check the results of ast_call */
  808. if (res) {
  809. /* Again, keep going even if there's an error */
  810. if (option_debug)
  811. ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
  812. else if (option_verbose > 2)
  813. ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", numsubst);
  814. ast_hangup(tmp->chan);
  815. free(tmp);
  816. cur = rest;
  817. continue;
  818. } else
  819. if (option_verbose > 2)
  820. ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
  821. /* Put them in the list of outgoing thingies... We're ready now.
  822. XXX If we're forcibly removed, these outgoing calls won't get
  823. hung up XXX */
  824. tmp->stillgoing = -1;
  825. tmp->next = outgoing;
  826. outgoing = tmp;
  827. /* If this line is up, don't try anybody else */
  828. if (outgoing->chan->_state == AST_STATE_UP)
  829. break;
  830. cur = rest;
  831. } while(cur);
  832. if (timeout && !ast_strlen_zero(timeout)) {
  833. to = atoi(timeout);
  834. if (to > 0)
  835. to *= 1000;
  836. else
  837. ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", timeout);
  838. } else
  839. to = -1;
  840. if (outgoing) {
  841. /* Our status will at least be NOANSWER */
  842. strncpy(status, "NOANSWER", sizeof(status) - 1);
  843. if (outgoing->musiconhold) {
  844. moh=1;
  845. ast_moh_start(chan, NULL);
  846. } else if (outgoing->ringbackonly) {
  847. ast_indicate(chan, AST_CONTROL_RINGING);
  848. sentringing++;
  849. }
  850. } else
  851. strncpy(status, "CHANUNAVAIL", sizeof(status) - 1);
  852. time(&start_time);
  853. peer = wait_for_answer(chan, outgoing, &to, &allowredir_in, &allowredir_out, &allowdisconnect_in, &allowdisconnect_out, &sentringing, &noforwardhtml, status, sizeof(status));
  854. if (!peer) {
  855. if (to)
  856. /* Musta gotten hung up */
  857. res = -1;
  858. else
  859. /* Nobody answered, next please? */
  860. res=0;
  861. goto out;
  862. }
  863. if (peer) {
  864. time(&answer_time);
  865. #ifdef OSP_SUPPORT
  866. /* Once call is answered, ditch the OSP Handle */
  867. pbx_builtin_setvar_helper(chan, "OSPHANDLE", "");
  868. #endif
  869. strncpy(status, "ANSWER", sizeof(status) - 1);
  870. /* Ah ha! Someone answered within the desired timeframe. Of course after this
  871. we will always return with -1 so that it is hung up properly after the
  872. conversation. */
  873. hanguptree(outgoing, peer);
  874. outgoing = NULL;
  875. /* If appropriate, log that we have a destination channel */
  876. if (chan->cdr)
  877. ast_cdr_setdestchan(chan->cdr, peer->name);
  878. if (peer->name)
  879. pbx_builtin_setvar_helper(chan, "DIALEDPEERNAME", peer->name);
  880. number = pbx_builtin_getvar_helper(peer, "DIALEDPEERNUMBER");
  881. if (!number)
  882. number = numsubst;
  883. pbx_builtin_setvar_helper(chan, "DIALEDPEERNUMBER", number);
  884. /* JDG: sendurl */
  885. if( url && !ast_strlen_zero(url) && ast_channel_supports_html(peer) ) {
  886. ast_log(LOG_DEBUG, "app_dial: sendurl=%s.\n", url);
  887. ast_channel_sendurl( peer, url );
  888. } /* /JDG */
  889. if (announce && announcemsg) {
  890. // Start autoservice on the other chan
  891. res = ast_autoservice_start(chan);
  892. // Now Stream the File
  893. if (!res)
  894. res = ast_streamfile(peer,announcemsg,peer->language);
  895. if (!res) {
  896. digit = ast_waitstream(peer, AST_DIGIT_ANY);
  897. }
  898. // Ok, done. stop autoservice
  899. res = ast_autoservice_stop(chan);
  900. if (digit > 0 && !res)
  901. res = ast_senddigit(chan, digit);
  902. else
  903. res = digit;
  904. } else
  905. res = 0;
  906. if (hasmacro && macroname) {
  907. void *app = NULL;
  908. res = ast_autoservice_start(chan);
  909. if (res) {
  910. ast_log(LOG_ERROR, "Unable to start autoservice on calling channel\n");
  911. res = -1;
  912. }
  913. app = pbx_findapp("Macro");
  914. if (app && !res) {
  915. res = pbx_exec(peer, app, macroname, 1);
  916. ast_log(LOG_DEBUG, "Macro exited with status %d\n", res);
  917. res = 0;
  918. } else {
  919. ast_log(LOG_ERROR, "Could not find application Macro\n");
  920. res = -1;
  921. }
  922. if (ast_autoservice_stop(chan) < 0) {
  923. ast_log(LOG_ERROR, "Could not stop autoservice on calling channel\n");
  924. res = -1;
  925. }
  926. }
  927. if (!res) {
  928. if (calldurationlimit > 0) {
  929. time(&now);
  930. chan->whentohangup = now + calldurationlimit;
  931. }
  932. if (!ast_strlen_zero(sdtmfdata))
  933. res = ast_dtmf_stream(peer,chan,sdtmfdata,250);
  934. }
  935. if (!res) {
  936. memset(&config,0,sizeof(struct ast_bridge_config));
  937. config.play_to_caller=play_to_caller;
  938. config.play_to_callee=play_to_callee;
  939. config.allowredirect_in = allowredir_in;
  940. config.allowredirect_out = allowredir_out;
  941. config.allowdisconnect_in = allowdisconnect_in;
  942. config.allowdisconnect_out = allowdisconnect_out;
  943. config.timelimit = timelimit;
  944. config.play_warning = play_warning;
  945. config.warning_freq = warning_freq;
  946. config.warning_sound = warning_sound;
  947. config.end_sound = end_sound;
  948. config.start_sound = start_sound;
  949. if (moh) {
  950. moh = 0;
  951. ast_moh_stop(chan);
  952. } else if (sentringing) {
  953. sentringing = 0;
  954. ast_indicate(chan, -1);
  955. }
  956. /* Be sure no generators are left on it */
  957. ast_deactivate_generator(chan);
  958. /* Make sure channels are compatible */
  959. res = ast_channel_make_compatible(chan, peer);
  960. if (res < 0) {
  961. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", chan->name, peer->name);
  962. ast_hangup(peer);
  963. return -1;
  964. }
  965. res = ast_bridge_call(chan,peer,&config);
  966. time(&end_time);
  967. snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
  968. pbx_builtin_setvar_helper(chan, "DIALEDTIME", toast);
  969. snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
  970. pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", toast);
  971. } else
  972. res = -1;
  973. if (res != AST_PBX_NO_HANGUP_PEER) {
  974. if (!chan->_softhangup)
  975. chan->hangupcause = peer->hangupcause;
  976. ast_hangup(peer);
  977. }
  978. }
  979. out:
  980. if (moh) {
  981. moh = 0;
  982. ast_moh_stop(chan);
  983. } else if (sentringing) {
  984. sentringing = 0;
  985. ast_indicate(chan, -1);
  986. }
  987. hanguptree(outgoing, NULL);
  988. pbx_builtin_setvar_helper(chan, "DIALSTATUS", status);
  989. ast_log(LOG_DEBUG, "Exiting with DIALSTATUS=%s.\n", status);
  990. LOCAL_USER_REMOVE(u);
  991. if((go_on>0) && (!chan->_softhangup) && (res != AST_PBX_KEEPALIVE))
  992. res=0;
  993. return res;
  994. }
  995. int unload_module(void)
  996. {
  997. STANDARD_HANGUP_LOCALUSERS;
  998. return ast_unregister_application(app);
  999. }
  1000. int load_module(void)
  1001. {
  1002. int res;
  1003. res = ast_register_application(app, dial_exec, synopsis, descrip);
  1004. return res;
  1005. }
  1006. char *description(void)
  1007. {
  1008. return tdesc;
  1009. }
  1010. int usecount(void)
  1011. {
  1012. int res;
  1013. STANDARD_USECOUNT(res);
  1014. return res;
  1015. }
  1016. char *key()
  1017. {
  1018. return ASTERISK_GPL_KEY;
  1019. }