res_features.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Routines implementing call parking
  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/channel_pvt.h>
  23. #include <asterisk/features.h>
  24. #include <asterisk/musiconhold.h>
  25. #include <asterisk/config.h>
  26. #include <asterisk/cli.h>
  27. #include <asterisk/manager.h>
  28. #include <asterisk/utils.h>
  29. #include <asterisk/adsi.h>
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <sys/time.h>
  37. #include <sys/signal.h>
  38. #include <netinet/in.h>
  39. #ifdef __AST_DEBUG_MALLOC
  40. static void FREE(void *ptr)
  41. {
  42. free(ptr);
  43. }
  44. #else
  45. #define FREE free
  46. #endif
  47. #define DEFAULT_PARK_TIME 45000
  48. #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
  49. static char *parkedcall = "ParkedCall";
  50. /* No more than 45 seconds parked before you do something with them */
  51. static int parkingtime = DEFAULT_PARK_TIME;
  52. /* Context for which parking is made accessible */
  53. static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
  54. /* Context for dialback for parking (KLUDGE) */
  55. static char parking_con_dial[AST_MAX_EXTENSION] = "park-dial";
  56. /* Extension you type to park the call */
  57. static char parking_ext[AST_MAX_EXTENSION] = "700";
  58. static char pickup_ext[AST_MAX_EXTENSION] = "*8";
  59. /* First available extension for parking */
  60. static int parking_start = 701;
  61. /* Last available extension for parking */
  62. static int parking_stop = 750;
  63. static int adsipark = 0;
  64. static int transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
  65. /* Default courtesy tone played when party joins conference */
  66. static char courtesytone[256] = "";
  67. /* Registrar for operations */
  68. static char *registrar = "res_features";
  69. static char *synopsis = "Answer a parked call";
  70. static char *descrip = "ParkedCall(exten):"
  71. "Used to connect to a parked call. This Application is always\n"
  72. "registered internally and does not need to be explicitly added\n"
  73. "into the dialplan, although you should include the 'parkedcalls'\n"
  74. "context.\n";
  75. static char *parkcall = "Park";
  76. static char *synopsis2 = "Park yourself";
  77. static char *descrip2 = "Park(exten):"
  78. "Used to park yourself (typically in combination with a supervised\n"
  79. "transfer to know the parking space. This Application is always\n"
  80. "registered internally and does not need to be explicitly added\n"
  81. "into the dialplan, although you should include the 'parkedcalls'\n"
  82. "context.\n";
  83. static struct ast_app *monitor_app=NULL;
  84. static int monitor_ok=1;
  85. struct parkeduser {
  86. struct ast_channel *chan;
  87. struct timeval start;
  88. int parkingnum;
  89. /* Where to go if our parking time expires */
  90. char context[AST_MAX_EXTENSION];
  91. char exten[AST_MAX_EXTENSION];
  92. int priority;
  93. int parkingtime;
  94. int notquiteyet;
  95. char peername[1024];
  96. unsigned char moh_trys;
  97. struct parkeduser *next;
  98. };
  99. static struct parkeduser *parkinglot;
  100. AST_MUTEX_DEFINE_STATIC(parking_lock);
  101. static pthread_t parking_thread;
  102. STANDARD_LOCAL_USER;
  103. LOCAL_USER_DECL;
  104. char *ast_parking_ext(void)
  105. {
  106. return parking_ext;
  107. }
  108. char *ast_pickup_ext(void)
  109. {
  110. return pickup_ext;
  111. }
  112. static int adsi_announce_park(struct ast_channel *chan, int parkingnum)
  113. {
  114. int res;
  115. int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
  116. char tmp[256] = "";
  117. char *message[5] = {NULL, NULL, NULL, NULL, NULL};
  118. snprintf(tmp, sizeof(tmp), "Parked on %d", parkingnum);
  119. message[0] = tmp;
  120. res = adsi_load_session(chan, NULL, 0, 1);
  121. if (res == -1) {
  122. return res;
  123. }
  124. return adsi_print(chan, message, justify, 1);
  125. }
  126. int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
  127. {
  128. /* We put the user in the parking list, then wake up the parking thread to be sure it looks
  129. after these channels too */
  130. struct parkeduser *pu, *cur;
  131. int x;
  132. char exten[AST_MAX_EXTENSION];
  133. struct ast_context *con;
  134. pu = malloc(sizeof(struct parkeduser));
  135. if (pu) {
  136. memset(pu,0,sizeof(struct parkeduser));
  137. ast_mutex_lock(&parking_lock);
  138. for (x=parking_start;x<=parking_stop;x++) {
  139. cur = parkinglot;
  140. while(cur) {
  141. if (cur->parkingnum == x)
  142. break;
  143. cur = cur->next;
  144. }
  145. if (!cur)
  146. break;
  147. }
  148. if (x <= parking_stop) {
  149. chan->appl = "Parked Call";
  150. chan->data = NULL;
  151. pu->chan = chan;
  152. /* Start music on hold */
  153. if (chan != peer)
  154. ast_moh_start(pu->chan, NULL);
  155. gettimeofday(&pu->start, NULL);
  156. pu->parkingnum = x;
  157. if (timeout > 0)
  158. pu->parkingtime = timeout;
  159. else
  160. pu->parkingtime = parkingtime;
  161. if (extout)
  162. *extout = x;
  163. if (peer)
  164. strncpy(pu->peername, peer->name, sizeof(pu->peername) - 1);
  165. /* Remember what had been dialed, so that if the parking
  166. expires, we try to come back to the same place */
  167. if (!ast_strlen_zero(chan->macrocontext))
  168. strncpy(pu->context, chan->macrocontext, sizeof(pu->context)-1);
  169. else
  170. strncpy(pu->context, chan->context, sizeof(pu->context)-1);
  171. if (!ast_strlen_zero(chan->macroexten))
  172. strncpy(pu->exten, chan->macroexten, sizeof(pu->exten)-1);
  173. else
  174. strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
  175. if (chan->macropriority)
  176. pu->priority = chan->macropriority;
  177. else
  178. pu->priority = chan->priority;
  179. pu->next = parkinglot;
  180. parkinglot = pu;
  181. /* If parking a channel directly, don't quiet yet get parking running on it */
  182. if (peer == chan)
  183. pu->notquiteyet = 1;
  184. ast_mutex_unlock(&parking_lock);
  185. /* Wake up the (presumably select()ing) thread */
  186. pthread_kill(parking_thread, SIGURG);
  187. if (option_verbose > 1)
  188. ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d. Will timeout back to %s,%s,%d in %d seconds\n", pu->chan->name, pu->parkingnum, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
  189. manager_event(EVENT_FLAG_CALL, "ParkedCall",
  190. "Exten: %d\r\n"
  191. "Channel: %s\r\n"
  192. "From: %s\r\n"
  193. "Timeout: %ld\r\n"
  194. "CallerID: %s\r\n"
  195. ,pu->parkingnum, pu->chan->name, peer->name
  196. ,(long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL)
  197. ,(pu->chan->callerid ? pu->chan->callerid : "")
  198. );
  199. if (peer) {
  200. if (adsipark && adsi_available(peer)) {
  201. adsi_announce_park(peer, pu->parkingnum);
  202. }
  203. ast_say_digits(peer, pu->parkingnum, "", peer->language);
  204. if (adsipark && adsi_available(peer)) {
  205. adsi_unload_session(peer);
  206. }
  207. }
  208. con = ast_context_find(parking_con);
  209. if (!con) {
  210. con = ast_context_create(NULL,parking_con, registrar);
  211. if (!con) {
  212. ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
  213. }
  214. }
  215. if (con) {
  216. snprintf(exten, sizeof(exten), "%d", x);
  217. ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), FREE, registrar);
  218. }
  219. if (pu->notquiteyet) {
  220. /* Wake up parking thread if we're really done */
  221. ast_moh_start(pu->chan, NULL);
  222. pu->notquiteyet = 0;
  223. pthread_kill(parking_thread, SIGURG);
  224. }
  225. return 0;
  226. } else {
  227. ast_log(LOG_WARNING, "No more parking spaces\n");
  228. free(pu);
  229. ast_mutex_unlock(&parking_lock);
  230. return -1;
  231. }
  232. } else {
  233. ast_log(LOG_WARNING, "Out of memory\n");
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
  239. {
  240. struct ast_channel *chan;
  241. struct ast_frame *f;
  242. /* Make a new, fake channel that we'll use to masquerade in the real one */
  243. chan = ast_channel_alloc(0);
  244. if (chan) {
  245. /* Let us keep track of the channel name */
  246. snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
  247. /* Make formats okay */
  248. chan->readformat = rchan->readformat;
  249. chan->writeformat = rchan->writeformat;
  250. ast_channel_masquerade(chan, rchan);
  251. /* Setup the extensions and such */
  252. strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
  253. strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
  254. chan->priority = rchan->priority;
  255. /* Make the masq execute */
  256. f = ast_read(chan);
  257. if (f)
  258. ast_frfree(f);
  259. ast_park_call(chan, peer, timeout, extout);
  260. } else {
  261. ast_log(LOG_WARNING, "Unable to create parked channel\n");
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
  267. {
  268. /* Copy voice back and forth between the two channels. Give the peer
  269. the ability to transfer calls with '#<extension' syntax. */
  270. int len;
  271. struct ast_frame *f;
  272. struct ast_channel *who;
  273. char newext[256], *ptr;
  274. int res;
  275. int diff;
  276. struct ast_option_header *aoh;
  277. struct ast_channel *transferer;
  278. struct ast_channel *transferee;
  279. struct timeval start, end;
  280. char *transferer_real_context;
  281. int allowdisconnect_in,allowdisconnect_out,allowredirect_in,allowredirect_out;
  282. char *monitor_exec;
  283. if (chan && peer) {
  284. pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
  285. pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
  286. } else if (chan)
  287. pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
  288. if (monitor_ok) {
  289. if (!monitor_app) {
  290. if (!(monitor_app = pbx_findapp("Monitor")))
  291. monitor_ok=0;
  292. }
  293. if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR")))
  294. pbx_exec(chan, monitor_app, monitor_exec, 1);
  295. else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
  296. pbx_exec(peer, monitor_app, monitor_exec, 1);
  297. }
  298. allowdisconnect_in = config->allowdisconnect_in;
  299. allowdisconnect_out = config->allowdisconnect_out;
  300. allowredirect_in = config->allowredirect_in;
  301. allowredirect_out = config->allowredirect_out;
  302. config->firstpass = 1;
  303. /* Answer if need be */
  304. if (ast_answer(chan))
  305. return -1;
  306. peer->appl = "Bridged Call";
  307. peer->data = chan->name;
  308. /* copy the userfield from the B-leg to A-leg if applicable */
  309. if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
  310. char tmp[256];
  311. if (!ast_strlen_zero(chan->cdr->userfield)) {
  312. snprintf(tmp, sizeof(tmp), "%s;%s",chan->cdr->userfield, peer->cdr->userfield);
  313. ast_cdr_appenduserfield(chan, tmp);
  314. } else
  315. ast_cdr_setuserfield(chan, peer->cdr->userfield);
  316. /* free the peer's cdr without ast_cdr_free complaining */
  317. free(peer->cdr);
  318. peer->cdr = NULL;
  319. }
  320. for (;;) {
  321. if (config->timelimit)
  322. gettimeofday(&start, NULL);
  323. res = ast_channel_bridge(chan,peer,config,&f, &who);
  324. if (config->timelimit) {
  325. /* Update time limit for next pass */
  326. gettimeofday(&end, NULL);
  327. diff = (end.tv_sec - start.tv_sec) * 1000;
  328. diff += (end.tv_usec - start.tv_usec) / 1000;
  329. config->timelimit -= diff;
  330. if (config->timelimit <=0) {
  331. /* We ran out of time */
  332. config->timelimit = 0;
  333. who = chan;
  334. f = NULL;
  335. res = 0;
  336. }
  337. }
  338. if (res < 0) {
  339. ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
  340. return -1;
  341. }
  342. if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) ||
  343. (f->subclass == AST_CONTROL_CONGESTION)))) {
  344. res = -1;
  345. break;
  346. }
  347. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
  348. if (who == chan)
  349. ast_indicate(peer, AST_CONTROL_RINGING);
  350. else
  351. ast_indicate(chan, AST_CONTROL_RINGING);
  352. }
  353. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == -1)) {
  354. if (who == chan)
  355. ast_indicate(peer, -1);
  356. else
  357. ast_indicate(chan, -1);
  358. }
  359. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
  360. aoh = f->data;
  361. /* Forward option Requests */
  362. if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
  363. if (who == chan)
  364. ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
  365. else
  366. ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
  367. }
  368. }
  369. /* check for '*', if we find it it's time to disconnect */
  370. if (f && (f->frametype == AST_FRAME_DTMF) &&
  371. (((who == chan) && allowdisconnect_out) || ((who == peer) && allowdisconnect_in)) &&
  372. (f->subclass == '*')) {
  373. if (option_verbose > 3)
  374. ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
  375. res = -1;
  376. break;
  377. }
  378. if ((f->frametype == AST_FRAME_DTMF) &&
  379. ((allowredirect_in && who == peer) || (allowredirect_out && who == chan)) &&
  380. (f->subclass == '#')) {
  381. if (allowredirect_in && who == peer) {
  382. transferer = peer;
  383. transferee = chan;
  384. } else {
  385. transferer = chan;
  386. transferee = peer;
  387. }
  388. if (!(transferer_real_context=pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT")) &&
  389. !(transferer_real_context=pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT"))) {
  390. /* Use the non-macro context to transfer the call */
  391. if (!ast_strlen_zero(transferer->macrocontext))
  392. transferer_real_context = transferer->macrocontext;
  393. else
  394. transferer_real_context = transferer->context;
  395. }
  396. /* Start autoservice on chan while we talk
  397. to the originator */
  398. ast_autoservice_start(transferee);
  399. ast_moh_start(transferee, NULL);
  400. memset(newext, 0, sizeof(newext));
  401. ptr = newext;
  402. /* Transfer */
  403. if ((res=ast_streamfile(transferer, "pbx-transfer", transferer->language))) {
  404. ast_moh_stop(transferee);
  405. ast_autoservice_stop(transferee);
  406. break;
  407. }
  408. if ((res=ast_waitstream(transferer, AST_DIGIT_ANY)) < 0) {
  409. ast_moh_stop(transferee);
  410. ast_autoservice_stop(transferee);
  411. break;
  412. }
  413. ast_stopstream(transferer);
  414. if (res > 0) {
  415. /* If they've typed a digit already, handle it */
  416. newext[0] = res;
  417. ptr++;
  418. len --;
  419. }
  420. res = 0;
  421. while (strlen(newext) < sizeof(newext) - 1) {
  422. res = ast_waitfordigit(transferer, transferdigittimeout);
  423. if (res < 1)
  424. break;
  425. if (res == '#')
  426. break;
  427. *(ptr++) = res;
  428. if (!ast_matchmore_extension(transferer, transferer_real_context
  429. , newext, 1, transferer->callerid)) {
  430. break;
  431. }
  432. }
  433. if (res < 0) {
  434. ast_moh_stop(transferee);
  435. ast_autoservice_stop(transferee);
  436. break;
  437. }
  438. if (!strcmp(newext, ast_parking_ext())) {
  439. ast_moh_stop(transferee);
  440. if (ast_autoservice_stop(transferee))
  441. res = -1;
  442. else if (!ast_park_call(transferee, transferer, 0, NULL)) {
  443. /* We return non-zero, but tell the PBX not to hang the channel when
  444. the thread dies -- We have to be careful now though. We are responsible for
  445. hanging up the channel, else it will never be hung up! */
  446. if (transferer==peer)
  447. res=AST_PBX_KEEPALIVE;
  448. else
  449. res=AST_PBX_NO_HANGUP_PEER;
  450. break;
  451. } else {
  452. ast_log(LOG_WARNING, "Unable to park call %s\n", transferee->name);
  453. }
  454. /* XXX Maybe we should have another message here instead of invalid extension XXX */
  455. } else if (ast_exists_extension(transferee, transferer_real_context, newext, 1, transferer->callerid)) {
  456. pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", chan->name);
  457. pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", peer->name);
  458. ast_moh_stop(transferee);
  459. res=ast_autoservice_stop(transferee);
  460. if (!transferee->pbx) {
  461. /* Doh! Use our handy async_goto funcitons */
  462. if (option_verbose > 2)
  463. ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n"
  464. ,transferee->name, newext, transferer_real_context);
  465. if (ast_async_goto(transferee, transferer_real_context, newext, 1))
  466. ast_log(LOG_WARNING, "Async goto fialed :(\n");
  467. res = -1;
  468. } else {
  469. /* Set the channel's new extension, since it exists, using transferer context */
  470. strncpy(transferee->exten, newext, sizeof(transferee->exten)-1);
  471. strncpy(transferee->context, transferer_real_context, sizeof(transferee->context)-1);
  472. transferee->priority = 0;
  473. ast_frfree(f);
  474. }
  475. break;
  476. } else {
  477. if (option_verbose > 2)
  478. ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context '%s'\n", newext, transferer_real_context);
  479. }
  480. res = ast_streamfile(transferer, "pbx-invalid", transferee->language);
  481. if (res) {
  482. ast_moh_stop(transferee);
  483. ast_autoservice_stop(transferee);
  484. break;
  485. }
  486. res = ast_waitstream(transferer, AST_DIGIT_ANY);
  487. ast_stopstream(transferer);
  488. ast_moh_stop(transferee);
  489. res = ast_autoservice_stop(transferee);
  490. if (res) {
  491. if (option_verbose > 1)
  492. ast_verbose(VERBOSE_PREFIX_2 "Hungup during autoservice stop on '%s'\n", transferee->name);
  493. }
  494. } else {
  495. if (f && (f->frametype == AST_FRAME_DTMF)) {
  496. if (who == peer)
  497. ast_write(chan, f);
  498. else
  499. ast_write(peer, f);
  500. }
  501. #if 1
  502. ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
  503. #endif
  504. }
  505. if (f)
  506. ast_frfree(f);
  507. }
  508. return res;
  509. }
  510. static void *do_parking_thread(void *ignore)
  511. {
  512. int ms, tms, max;
  513. struct parkeduser *pu, *pl, *pt = NULL;
  514. struct timeval tv;
  515. struct ast_frame *f;
  516. char exten[AST_MAX_EXTENSION];
  517. char *peername,*cp;
  518. char returnexten[AST_MAX_EXTENSION];
  519. struct ast_context *con;
  520. int x;
  521. fd_set rfds, efds;
  522. fd_set nrfds, nefds;
  523. FD_ZERO(&rfds);
  524. FD_ZERO(&efds);
  525. for (;;) {
  526. ms = -1;
  527. max = -1;
  528. ast_mutex_lock(&parking_lock);
  529. pl = NULL;
  530. pu = parkinglot;
  531. FD_ZERO(&nrfds);
  532. FD_ZERO(&nefds);
  533. while(pu) {
  534. if (pu->notquiteyet) {
  535. /* Pretend this one isn't here yet */
  536. pl = pu;
  537. pu = pu->next;
  538. continue;
  539. }
  540. gettimeofday(&tv, NULL);
  541. tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
  542. if (tms > pu->parkingtime) {
  543. /* Stop music on hold */
  544. ast_moh_stop(pu->chan);
  545. /* Get chan, exten from derived kludge */
  546. if (pu->peername[0]) {
  547. peername = ast_strdupa(pu->peername);
  548. cp = strrchr(peername, '-');
  549. if (cp)
  550. *cp = 0;
  551. con = ast_context_find(parking_con_dial);
  552. if (!con) {
  553. con = ast_context_create(NULL, parking_con_dial, registrar);
  554. if (!con) {
  555. ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", parking_con_dial);
  556. }
  557. }
  558. if (con) {
  559. snprintf(returnexten, sizeof(returnexten), "%s||t", peername);
  560. ast_add_extension2(con, 1, peername, 1, NULL, "Dial", strdup(returnexten), free, registrar);
  561. }
  562. strncpy(pu->chan->exten, peername, sizeof(pu->chan->exten) - 1);
  563. strncpy(pu->chan->context, parking_con_dial, sizeof(pu->chan->context) - 1);
  564. pu->chan->priority = 1;
  565. } else {
  566. /* They've been waiting too long, send them back to where they came. Theoretically they
  567. should have their original extensions and such, but we copy to be on the safe side */
  568. strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
  569. strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
  570. pu->chan->priority = pu->priority;
  571. }
  572. if (option_verbose > 1)
  573. ast_verbose(VERBOSE_PREFIX_2 "Timeout for %s parked on %d. Returning to %s,%s,%d\n", pu->chan->name, pu->parkingnum, pu->chan->context, pu->chan->exten, pu->chan->priority);
  574. /* Start up the PBX, or hang them up */
  575. if (ast_pbx_start(pu->chan)) {
  576. ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
  577. ast_hangup(pu->chan);
  578. }
  579. /* And take them out of the parking lot */
  580. if (pl)
  581. pl->next = pu->next;
  582. else
  583. parkinglot = pu->next;
  584. pt = pu;
  585. pu = pu->next;
  586. con = ast_context_find(parking_con);
  587. if (con) {
  588. snprintf(exten, sizeof(exten), "%d", pt->parkingnum);
  589. if (ast_context_remove_extension2(con, exten, 1, NULL))
  590. ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
  591. } else
  592. ast_log(LOG_WARNING, "Whoa, no parking context?\n");
  593. free(pt);
  594. } else {
  595. for (x=0; x<AST_MAX_FDS; x++) {
  596. if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
  597. if (FD_ISSET(pu->chan->fds[x], &efds))
  598. pu->chan->exception = 1;
  599. else
  600. pu->chan->exception = 0;
  601. pu->chan->fdno = x;
  602. /* See if they need servicing */
  603. f = ast_read(pu->chan);
  604. if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
  605. /* There's a problem, hang them up*/
  606. if (option_verbose > 1)
  607. ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
  608. ast_hangup(pu->chan);
  609. /* And take them out of the parking lot */
  610. if (pl)
  611. pl->next = pu->next;
  612. else
  613. parkinglot = pu->next;
  614. pt = pu;
  615. pu = pu->next;
  616. con = ast_context_find(parking_con);
  617. if (con) {
  618. snprintf(exten, sizeof(exten), "%d", pt->parkingnum);
  619. if (ast_context_remove_extension2(con, exten, 1, NULL))
  620. ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
  621. } else
  622. ast_log(LOG_WARNING, "Whoa, no parking context?\n");
  623. free(pt);
  624. break;
  625. } else {
  626. /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
  627. ast_frfree(f);
  628. if (pu->moh_trys < 3 && !pu->chan->generatordata) {
  629. ast_log(LOG_DEBUG, "MOH on parked call stopped by outside source. Restarting.\n");
  630. ast_moh_start(pu->chan, NULL);
  631. pu->moh_trys++;
  632. }
  633. goto std; /* XXX Ick: jumping into an else statement??? XXX */
  634. }
  635. }
  636. }
  637. if (x >= AST_MAX_FDS) {
  638. std: for (x=0; x<AST_MAX_FDS; x++) {
  639. /* Keep this one for next one */
  640. if (pu->chan->fds[x] > -1) {
  641. FD_SET(pu->chan->fds[x], &nrfds);
  642. FD_SET(pu->chan->fds[x], &nefds);
  643. if (pu->chan->fds[x] > max)
  644. max = pu->chan->fds[x];
  645. }
  646. }
  647. /* Keep track of our longest wait */
  648. if ((tms < ms) || (ms < 0))
  649. ms = tms;
  650. pl = pu;
  651. pu = pu->next;
  652. }
  653. }
  654. }
  655. ast_mutex_unlock(&parking_lock);
  656. rfds = nrfds;
  657. efds = nefds;
  658. tv.tv_sec = ms / 1000;
  659. tv.tv_usec = (ms % 1000) * 1000;
  660. /* Wait for something to happen */
  661. ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
  662. pthread_testcancel();
  663. }
  664. return NULL; /* Never reached */
  665. }
  666. static int park_call_exec(struct ast_channel *chan, void *data)
  667. {
  668. /* Data is unused at the moment but could contain a parking
  669. lot context eventually */
  670. int res=0;
  671. struct localuser *u;
  672. LOCAL_USER_ADD(u);
  673. /* Setup the exten/priority to be s/1 since we don't know
  674. where this call should return */
  675. strcpy(chan->exten, "s");
  676. chan->priority = 1;
  677. if (chan->_state != AST_STATE_UP)
  678. res = ast_answer(chan);
  679. if (!res)
  680. res = ast_safe_sleep(chan, 1000);
  681. if (!res)
  682. res = ast_park_call(chan, chan, 0, NULL);
  683. LOCAL_USER_REMOVE(u);
  684. if (!res)
  685. res = AST_PBX_KEEPALIVE;
  686. return res;
  687. }
  688. static int park_exec(struct ast_channel *chan, void *data)
  689. {
  690. int res=0;
  691. struct localuser *u;
  692. struct ast_channel *peer=NULL;
  693. struct parkeduser *pu, *pl=NULL;
  694. char exten[AST_MAX_EXTENSION];
  695. struct ast_context *con;
  696. int park;
  697. int dres;
  698. struct ast_bridge_config config;
  699. if (!data) {
  700. ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
  701. return -1;
  702. }
  703. LOCAL_USER_ADD(u);
  704. park = atoi((char *)data);
  705. ast_mutex_lock(&parking_lock);
  706. pu = parkinglot;
  707. while(pu) {
  708. if (pu->parkingnum == park) {
  709. if (pl)
  710. pl->next = pu->next;
  711. else
  712. parkinglot = pu->next;
  713. break;
  714. }
  715. pl = pu;
  716. pu = pu->next;
  717. }
  718. ast_mutex_unlock(&parking_lock);
  719. if (pu) {
  720. peer = pu->chan;
  721. con = ast_context_find(parking_con);
  722. if (con) {
  723. snprintf(exten, sizeof(exten), "%d", pu->parkingnum);
  724. if (ast_context_remove_extension2(con, exten, 1, NULL))
  725. ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
  726. } else
  727. ast_log(LOG_WARNING, "Whoa, no parking context?\n");
  728. free(pu);
  729. }
  730. /* JK02: it helps to answer the channel if not already up */
  731. if (chan->_state != AST_STATE_UP) {
  732. ast_answer(chan);
  733. }
  734. if (peer) {
  735. /* Play a courtesy beep in the calling channel to prefix the bridge connecting */
  736. if (!ast_strlen_zero(courtesytone)) {
  737. if (!ast_streamfile(chan, courtesytone, chan->language)) {
  738. if (ast_waitstream(chan, "") < 0) {
  739. ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
  740. ast_hangup(peer);
  741. return -1;
  742. }
  743. }
  744. }
  745. ast_moh_stop(peer);
  746. res = ast_channel_make_compatible(chan, peer);
  747. if (res < 0) {
  748. ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
  749. ast_hangup(peer);
  750. return -1;
  751. }
  752. /* This runs sorta backwards, since we give the incoming channel control, as if it
  753. were the person called. */
  754. if (option_verbose > 2)
  755. ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
  756. memset(&config,0,sizeof(struct ast_bridge_config));
  757. config.allowredirect_in = 1;
  758. config.allowredirect_out = 1;
  759. config.allowdisconnect_out = 0;
  760. config.allowdisconnect_in = 0;
  761. config.timelimit = 0;
  762. config.play_warning = 0;
  763. config.warning_freq = 0;
  764. config.warning_sound=NULL;
  765. res = ast_bridge_call(chan,peer,&config);
  766. /* Simulate the PBX hanging up */
  767. if (res != AST_PBX_NO_HANGUP_PEER)
  768. ast_hangup(peer);
  769. return res;
  770. } else {
  771. /* XXX Play a message XXX */
  772. dres = ast_streamfile(chan, "pbx-invalidpark", chan->language);
  773. if (!dres)
  774. dres = ast_waitstream(chan, "");
  775. else {
  776. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
  777. dres = 0;
  778. }
  779. if (option_verbose > 2)
  780. ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to nonexistent parked call %d\n", chan->name, park);
  781. res = -1;
  782. }
  783. LOCAL_USER_REMOVE(u);
  784. return res;
  785. }
  786. static int handle_parkedcalls(int fd, int argc, char *argv[])
  787. {
  788. struct parkeduser *cur;
  789. ast_cli(fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
  790. , "Context", "Extension", "Pri", "Timeout");
  791. ast_mutex_lock(&parking_lock);
  792. cur=parkinglot;
  793. while(cur) {
  794. ast_cli(fd, "%4d %25s (%-15s %-12s %-4d) %6lds\n"
  795. ,cur->parkingnum, cur->chan->name, cur->context, cur->exten
  796. ,cur->priority, cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL));
  797. cur = cur->next;
  798. }
  799. ast_mutex_unlock(&parking_lock);
  800. return RESULT_SUCCESS;
  801. }
  802. static char showparked_help[] =
  803. "Usage: show parkedcalls\n"
  804. " Lists currently parked calls.\n";
  805. static struct ast_cli_entry showparked =
  806. { { "show", "parkedcalls", NULL }, handle_parkedcalls, "Lists parked calls", showparked_help };
  807. /* Dump lot status */
  808. static int manager_parking_status( struct mansession *s, struct message *m )
  809. {
  810. struct parkeduser *cur;
  811. char *id = astman_get_header(m,"ActionID");
  812. char idText[256] = "";
  813. if (id && !ast_strlen_zero(id))
  814. snprintf(idText,256,"ActionID: %s\r\n",id);
  815. astman_send_ack(s, m, "Parked calls will follow");
  816. ast_mutex_lock(&parking_lock);
  817. cur=parkinglot;
  818. while(cur) {
  819. ast_mutex_lock(&s->lock);
  820. ast_cli(s->fd, "Event: ParkedCall\r\n"
  821. "Exten: %d\r\n"
  822. "Channel: %s\r\n"
  823. "Timeout: %ld\r\n"
  824. "CallerID: %s\r\n"
  825. "%s"
  826. "\r\n"
  827. ,cur->parkingnum, cur->chan->name
  828. ,(long)cur->start.tv_sec + (long)(cur->parkingtime/1000) - (long)time(NULL)
  829. ,(cur->chan->callerid ? cur->chan->callerid : "")
  830. ,idText);
  831. ast_mutex_unlock(&s->lock);
  832. cur = cur->next;
  833. }
  834. ast_cli(s->fd,
  835. "Event: ParkedCallsComplete\r\n"
  836. "%s"
  837. "\r\n",idText);
  838. ast_mutex_unlock(&parking_lock);
  839. return RESULT_SUCCESS;
  840. }
  841. int load_module(void)
  842. {
  843. int res;
  844. int start, end;
  845. struct ast_context *con;
  846. struct ast_config *cfg;
  847. struct ast_variable *var;
  848. ast_cli_register(&showparked);
  849. cfg = ast_load("features.conf");
  850. if (!cfg) {
  851. cfg = ast_load("parking.conf");
  852. if (cfg)
  853. ast_log(LOG_NOTICE, "parking.conf is deprecated in favor of 'features.conf'. Please rename it.\n");
  854. }
  855. if (cfg) {
  856. var = ast_variable_browse(cfg, "general");
  857. while(var) {
  858. if (!strcasecmp(var->name, "parkext")) {
  859. strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
  860. } else if (!strcasecmp(var->name, "context")) {
  861. strncpy(parking_con, var->value, sizeof(parking_con) - 1);
  862. } else if (!strcasecmp(var->name, "parkingtime")) {
  863. if ((sscanf(var->value, "%d", &parkingtime) != 1) || (parkingtime < 1)) {
  864. ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
  865. parkingtime = DEFAULT_PARK_TIME;
  866. } else
  867. parkingtime = parkingtime * 1000;
  868. } else if (!strcasecmp(var->name, "parkpos")) {
  869. if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
  870. ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of parking.conf\n", var->lineno);
  871. } else {
  872. parking_start = start;
  873. parking_stop = end;
  874. }
  875. } else if (!strcasecmp(var->name, "adsipark")) {
  876. adsipark = ast_true(var->value);
  877. } else if (!strcasecmp(var->name, "transferdigittimeout")) {
  878. if ((sscanf(var->value, "%d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
  879. ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
  880. transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
  881. } else
  882. transferdigittimeout = transferdigittimeout * 1000;
  883. } else if (!strcasecmp(var->name, "courtesytone")) {
  884. strncpy(courtesytone, var->value, sizeof(courtesytone) - 1);
  885. } else if (!strcasecmp(var->name, "pickupexten")) {
  886. strncpy(pickup_ext, var->value, sizeof(pickup_ext) - 1);
  887. }
  888. var = var->next;
  889. }
  890. ast_destroy(cfg);
  891. }
  892. con = ast_context_find(parking_con);
  893. if (!con) {
  894. con = ast_context_create(NULL,parking_con, registrar);
  895. if (!con) {
  896. ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
  897. return -1;
  898. }
  899. }
  900. ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, parkcall, strdup(""), FREE, registrar);
  901. ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
  902. res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
  903. if (!res)
  904. res = ast_register_application(parkcall, park_call_exec, synopsis2, descrip2);
  905. if (!res) {
  906. ast_manager_register( "ParkedCalls", 0, manager_parking_status, "List parked calls" );
  907. }
  908. return res;
  909. }
  910. int ast_pickup_call(struct ast_channel *chan)
  911. {
  912. struct ast_channel *cur;
  913. int res = -1;
  914. cur = ast_channel_walk_locked(NULL);
  915. while(cur) {
  916. if (!cur->pbx &&
  917. (cur != chan) &&
  918. (chan->pickupgroup & cur->callgroup) &&
  919. ((cur->_state == AST_STATE_RINGING) ||
  920. (cur->_state == AST_STATE_RING))) {
  921. break;
  922. }
  923. ast_mutex_unlock(&cur->lock);
  924. cur = ast_channel_walk_locked(cur);
  925. }
  926. if (cur) {
  927. ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
  928. res = ast_answer(chan);
  929. if (res)
  930. ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
  931. res = ast_queue_control(chan, AST_CONTROL_ANSWER);
  932. if (res)
  933. ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
  934. res = ast_channel_masquerade(cur, chan);
  935. if (res)
  936. ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name); /* Done */
  937. ast_mutex_unlock(&cur->lock);
  938. } else {
  939. ast_log(LOG_DEBUG, "No call pickup possible...\n");
  940. }
  941. return res;
  942. }
  943. int unload_module(void)
  944. {
  945. STANDARD_HANGUP_LOCALUSERS;
  946. ast_manager_unregister( "ParkedCalls" );
  947. ast_cli_unregister(&showparked);
  948. ast_unregister_application(parkcall);
  949. return ast_unregister_application(parkedcall);
  950. }
  951. char *description(void)
  952. {
  953. return "Call Parking Resource";
  954. }
  955. int usecount(void)
  956. {
  957. /* Never allow parking to be unloaded because it will
  958. unresolve needed symbols in the dialer */
  959. #if 0
  960. int res;
  961. STANDARD_USECOUNT(res);
  962. return res;
  963. #else
  964. return 1;
  965. #endif
  966. }
  967. char *key()
  968. {
  969. return ASTERISK_GPL_KEY;
  970. }