res_parking.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Routines implementing call parking
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  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/parking.h>
  24. #include <asterisk/musiconhold.h>
  25. #include <asterisk/config.h>
  26. #include <asterisk/cli.h>
  27. #include <asterisk/manager.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. #include <pthread.h>
  38. #define DEFAULT_PARK_TIME 45000
  39. static char *parkedcall = "ParkedCall";
  40. /* No more than 45 seconds parked before you do something with them */
  41. static int parkingtime = DEFAULT_PARK_TIME;
  42. /* Context for which parking is made accessible */
  43. static char parking_con[AST_MAX_EXTENSION] = "parkedcalls";
  44. /* Extension you type to park the call */
  45. static char parking_ext[AST_MAX_EXTENSION] = "700";
  46. static char pickup_ext[AST_MAX_EXTENSION] = "*8";
  47. /* First available extension for parking */
  48. static int parking_start = 701;
  49. /* Last available extension for parking */
  50. static int parking_stop = 750;
  51. /* Registrar for operations */
  52. static char *registrar = "res_parking";
  53. static char *synopsis = "Answer a parked call";
  54. static char *descrip = "ParkedCall(exten):"
  55. "Used to connect to a parked call. This Application is always\n"
  56. "registered internally and does not need to be explicitly added\n"
  57. "into the dialplan, although you should include the 'parkedcalls'\n"
  58. "context.\n";
  59. struct parkeduser {
  60. struct ast_channel *chan;
  61. struct timeval start;
  62. int parkingnum;
  63. /* Where to go if our parking time expires */
  64. char context[AST_MAX_EXTENSION];
  65. char exten[AST_MAX_EXTENSION];
  66. int priority;
  67. int parkingtime;
  68. struct parkeduser *next;
  69. };
  70. static struct parkeduser *parkinglot;
  71. static ast_mutex_t parking_lock = AST_MUTEX_INITIALIZER;
  72. static pthread_t parking_thread;
  73. STANDARD_LOCAL_USER;
  74. LOCAL_USER_DECL;
  75. char *ast_parking_ext(void)
  76. {
  77. return parking_ext;
  78. }
  79. char *ast_pickup_ext(void)
  80. {
  81. return pickup_ext;
  82. }
  83. int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
  84. {
  85. /* We put the user in the parking list, then wake up the parking thread to be sure it looks
  86. after these channels too */
  87. struct parkeduser *pu, *cur;
  88. int x;
  89. pu = malloc(sizeof(struct parkeduser));
  90. if (pu) {
  91. ast_mutex_lock(&parking_lock);
  92. for (x=parking_start;x<=parking_stop;x++) {
  93. cur = parkinglot;
  94. while(cur) {
  95. if (cur->parkingnum == x)
  96. break;
  97. cur = cur->next;
  98. }
  99. if (!cur)
  100. break;
  101. }
  102. if (x <= parking_stop) {
  103. chan->appl = "Parked Call";
  104. chan->data = NULL;
  105. pu->chan = chan;
  106. /* Start music on hold */
  107. ast_moh_start(pu->chan, NULL);
  108. gettimeofday(&pu->start, NULL);
  109. pu->parkingnum = x;
  110. if (timeout > 0)
  111. pu->parkingtime = timeout;
  112. else
  113. pu->parkingtime = parkingtime;
  114. if (extout)
  115. *extout = x;
  116. /* Remember what had been dialed, so that if the parking
  117. expires, we try to come back to the same place */
  118. if (strlen(chan->macrocontext))
  119. strncpy(pu->context, chan->macrocontext, sizeof(pu->context)-1);
  120. else
  121. strncpy(pu->context, chan->context, sizeof(pu->context)-1);
  122. if (strlen(chan->macroexten))
  123. strncpy(pu->exten, chan->macroexten, sizeof(pu->exten)-1);
  124. else
  125. strncpy(pu->exten, chan->exten, sizeof(pu->exten)-1);
  126. if (chan->macropriority)
  127. pu->priority = chan->macropriority;
  128. else
  129. pu->priority = chan->priority;
  130. pu->next = parkinglot;
  131. parkinglot = pu;
  132. ast_mutex_unlock(&parking_lock);
  133. /* Wake up the (presumably select()ing) thread */
  134. pthread_kill(parking_thread, SIGURG);
  135. if (option_verbose > 1)
  136. ast_verbose(VERBOSE_PREFIX_2 "Parked %s on %d\n", pu->chan->name, pu->parkingnum);
  137. manager_event(EVENT_FLAG_CALL, "ParkedCall",
  138. "Exten: %d\r\n"
  139. "Channel: %s\r\n"
  140. "From: %s\r\n"
  141. "Timeout: %ld\r\n"
  142. "CallerID: %s\r\n"
  143. ,pu->parkingnum, pu->chan->name, peer->name
  144. ,(long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL)
  145. ,(pu->chan->callerid ? pu->chan->callerid : "")
  146. );
  147. if (peer)
  148. ast_say_digits(peer, pu->parkingnum, "", peer->language);
  149. return 0;
  150. } else {
  151. ast_log(LOG_WARNING, "No more parking spaces\n");
  152. free(pu);
  153. ast_mutex_unlock(&parking_lock);
  154. return -1;
  155. }
  156. } else {
  157. ast_log(LOG_WARNING, "Out of memory\n");
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
  163. {
  164. struct ast_channel *chan;
  165. struct ast_frame *f;
  166. /* Make a new, fake channel that we'll use to masquerade in the real one */
  167. chan = ast_channel_alloc(0);
  168. if (chan) {
  169. /* Let us keep track of the channel name */
  170. snprintf(chan->name, sizeof (chan->name), "Parked/%s",rchan->name);
  171. /* Make formats okay */
  172. chan->readformat = rchan->readformat;
  173. chan->writeformat = rchan->writeformat;
  174. ast_channel_masquerade(chan, rchan);
  175. /* Setup the extensions and such */
  176. strncpy(chan->context, rchan->context, sizeof(chan->context) - 1);
  177. strncpy(chan->exten, rchan->exten, sizeof(chan->exten) - 1);
  178. chan->priority = rchan->priority;
  179. /* Make the masq execute */
  180. f = ast_read(chan);
  181. if (f)
  182. ast_frfree(f);
  183. ast_park_call(chan, peer, timeout, extout);
  184. } else {
  185. ast_log(LOG_WARNING, "Unable to create parked channel\n");
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, int allowredirect_in, int allowredirect_out, int allowdisconnect)
  191. {
  192. /* Copy voice back and forth between the two channels. Give the peer
  193. the ability to transfer calls with '#<extension' syntax. */
  194. int len;
  195. struct ast_frame *f;
  196. struct ast_channel *who;
  197. char newext[256], *ptr;
  198. int res;
  199. struct ast_option_header *aoh;
  200. struct ast_channel *transferer;
  201. struct ast_channel *transferee;
  202. char *transferer_real_context;
  203. /* Answer if need be */
  204. if (ast_answer(chan))
  205. return -1;
  206. peer->appl = "Bridged Call";
  207. peer->data = chan->name;
  208. /* copy the userfield from the B-leg to A-leg if applicable */
  209. if (chan->cdr && peer->cdr && strlen(peer->cdr->userfield)) {
  210. char tmp[256];
  211. if (strlen(chan->cdr->userfield)) {
  212. snprintf(tmp, sizeof(tmp), "%s;%s",chan->cdr->userfield, peer->cdr->userfield);
  213. ast_cdr_appenduserfield(chan, tmp);
  214. } else
  215. ast_cdr_setuserfield(chan, peer->cdr->userfield);
  216. /* free the peer's cdr without ast_cdr_free complaining */
  217. free(peer->cdr);
  218. peer->cdr = NULL;
  219. }
  220. for (;;) {
  221. res = ast_channel_bridge(chan, peer, (allowdisconnect||allowredirect_out ? AST_BRIDGE_DTMF_CHANNEL_0 : 0) + (allowredirect_in ? AST_BRIDGE_DTMF_CHANNEL_1 : 0), &f, &who);
  222. if (res < 0) {
  223. ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
  224. return -1;
  225. }
  226. if (!f || ((f->frametype == AST_FRAME_CONTROL) && ((f->subclass == AST_CONTROL_HANGUP) || (f->subclass == AST_CONTROL_BUSY) ||
  227. (f->subclass == AST_CONTROL_CONGESTION)))) {
  228. res = -1;
  229. break;
  230. }
  231. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RINGING)) {
  232. if (who == chan)
  233. ast_indicate(peer, AST_CONTROL_RINGING);
  234. else
  235. ast_indicate(chan, AST_CONTROL_RINGING);
  236. }
  237. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == -1)) {
  238. if (who == chan)
  239. ast_indicate(peer, -1);
  240. else
  241. ast_indicate(chan, -1);
  242. }
  243. if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_OPTION)) {
  244. aoh = f->data;
  245. /* Forward option Requests */
  246. if (aoh && (aoh->flag == AST_OPTION_FLAG_REQUEST)) {
  247. if (who == chan)
  248. ast_channel_setoption(peer, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
  249. else
  250. ast_channel_setoption(chan, ntohs(aoh->option), aoh->data, f->datalen - sizeof(struct ast_option_header), 0);
  251. }
  252. }
  253. if (f && (f->frametype == AST_FRAME_DTMF) && (who == chan) && allowdisconnect &&
  254. (f->subclass == '*')) {
  255. if (option_verbose > 3)
  256. ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
  257. res = -1;
  258. break;
  259. }
  260. if ((f->frametype == AST_FRAME_DTMF) &&
  261. ((allowredirect_in && who == peer) || (allowredirect_out && who == chan)) &&
  262. (f->subclass == '#')) {
  263. if(allowredirect_in && who == peer) {
  264. transferer = peer;
  265. transferee = chan;
  266. }
  267. else {
  268. transferer = chan;
  269. transferee = peer;
  270. }
  271. /* Use the non-macro context to transfer the call */
  272. if(strlen(transferer->macrocontext))
  273. transferer_real_context=transferer->macrocontext;
  274. else
  275. transferer_real_context=transferer->context;
  276. /* Start autoservice on chan while we talk
  277. to the originator */
  278. ast_autoservice_start(transferee);
  279. ast_moh_start(transferee, NULL);
  280. memset(newext, 0, sizeof(newext));
  281. ptr = newext;
  282. /* Transfer */
  283. if ((res=ast_streamfile(transferer, "pbx-transfer", transferer->language))) {
  284. ast_moh_stop(transferee);
  285. ast_autoservice_stop(transferee);
  286. break;
  287. }
  288. if ((res=ast_waitstream(transferer, AST_DIGIT_ANY)) < 0) {
  289. ast_moh_stop(transferee);
  290. ast_autoservice_stop(transferee);
  291. break;
  292. }
  293. ast_stopstream(transferer);
  294. if (res > 0) {
  295. /* If they've typed a digit already, handle it */
  296. newext[0] = res;
  297. ptr++;
  298. len --;
  299. }
  300. res = 0;
  301. while(strlen(newext) < sizeof(newext) - 1) {
  302. res = ast_waitfordigit(transferer, 3000);
  303. if (res < 1)
  304. break;
  305. if (res == '#')
  306. break;
  307. *(ptr++) = res;
  308. if (!ast_matchmore_extension(transferer, transferer_real_context
  309. , newext, 1, transferer->callerid)) {
  310. break;
  311. }
  312. }
  313. if (res < 0) {
  314. ast_moh_stop(transferee);
  315. ast_autoservice_stop(transferee);
  316. break;
  317. }
  318. if (!strcmp(newext, ast_parking_ext())) {
  319. ast_moh_stop(transferee);
  320. if (ast_autoservice_stop(transferee))
  321. res = -1;
  322. else if (!ast_park_call(transferee, transferer, 0, NULL)) {
  323. /* We return non-zero, but tell the PBX not to hang the channel when
  324. the thread dies -- We have to be careful now though. We are responsible for
  325. hanging up the channel, else it will never be hung up! */
  326. if(transferer==peer)
  327. res=AST_PBX_KEEPALIVE;
  328. else
  329. res=AST_PBX_NO_HANGUP_PEER;
  330. break;
  331. } else {
  332. ast_log(LOG_WARNING, "Unable to park call %s\n", transferee->name);
  333. }
  334. /* XXX Maybe we should have another message here instead of invalid extension XXX */
  335. } else if (ast_exists_extension(transferee, transferer_real_context, newext, 1, transferer->callerid)) {
  336. ast_moh_stop(transferee);
  337. res=ast_autoservice_stop(transferee);
  338. if (!transferee->pbx) {
  339. /* Doh! Use our handy async_goto funcitons */
  340. if (option_verbose > 2)
  341. ast_verbose(VERBOSE_PREFIX_3 "Transferring %s to '%s' (context %s) priority 1\n"
  342. ,transferee->name, newext, transferer_real_context);
  343. if (ast_async_goto(transferee, transferer_real_context, newext, 1, 1))
  344. ast_log(LOG_WARNING, "Async goto fialed :(\n");
  345. res = -1;
  346. } else {
  347. /* Set the channel's new extension, since it exists, using transferer context */
  348. strncpy(transferee->exten, newext, sizeof(transferee->exten)-1);
  349. strncpy(transferee->context, transferer_real_context, sizeof(transferee->context)-1);
  350. transferee->priority = 0;
  351. ast_frfree(f);
  352. }
  353. break;
  354. } else {
  355. if (option_verbose > 2)
  356. ast_verbose(VERBOSE_PREFIX_3 "Unable to find extension '%s' in context '%s'\n", newext, transferer_real_context);
  357. }
  358. res = ast_streamfile(transferer, "pbx-invalid", transferee->language);
  359. if (res) {
  360. ast_moh_stop(transferee);
  361. ast_autoservice_stop(transferee);
  362. break;
  363. }
  364. res = ast_waitstream(transferer, AST_DIGIT_ANY);
  365. ast_stopstream(transferer);
  366. ast_moh_stop(transferee);
  367. res = ast_autoservice_stop(transferee);
  368. if (res) {
  369. if (option_verbose > 1)
  370. ast_verbose(VERBOSE_PREFIX_2 "Hungup during autoservice stop on '%s'\n", transferee->name);
  371. }
  372. } else {
  373. if (f && (f->frametype == AST_FRAME_DTMF)) {
  374. if (who == peer)
  375. ast_write(chan, f);
  376. else
  377. ast_write(peer, f);
  378. }
  379. #if 1
  380. ast_log(LOG_DEBUG, "Read from %s (%d,%d)\n", who->name, f->frametype, f->subclass);
  381. #endif
  382. }
  383. if (f)
  384. ast_frfree(f);
  385. }
  386. return res;
  387. }
  388. static void *do_parking_thread(void *ignore)
  389. {
  390. int ms, tms, max;
  391. struct parkeduser *pu, *pl, *pt = NULL;
  392. struct timeval tv;
  393. struct ast_frame *f;
  394. int x;
  395. fd_set rfds, efds;
  396. fd_set nrfds, nefds;
  397. FD_ZERO(&rfds);
  398. FD_ZERO(&efds);
  399. for (;;) {
  400. ms = -1;
  401. max = -1;
  402. ast_mutex_lock(&parking_lock);
  403. pl = NULL;
  404. pu = parkinglot;
  405. gettimeofday(&tv, NULL);
  406. FD_ZERO(&nrfds);
  407. FD_ZERO(&nefds);
  408. while(pu) {
  409. tms = (tv.tv_sec - pu->start.tv_sec) * 1000 + (tv.tv_usec - pu->start.tv_usec) / 1000;
  410. if (tms > pu->parkingtime) {
  411. /* They've been waiting too long, send them back to where they came. Theoretically they
  412. should have their original extensions and such, but we copy to be on the safe side */
  413. strncpy(pu->chan->exten, pu->exten, sizeof(pu->chan->exten)-1);
  414. strncpy(pu->chan->context, pu->context, sizeof(pu->chan->context)-1);
  415. pu->chan->priority = pu->priority;
  416. /* Stop music on hold */
  417. ast_moh_stop(pu->chan);
  418. /* Start up the PBX, or hang them up */
  419. if (ast_pbx_start(pu->chan)) {
  420. ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
  421. ast_hangup(pu->chan);
  422. }
  423. /* And take them out of the parking lot */
  424. if (pl)
  425. pl->next = pu->next;
  426. else
  427. parkinglot = pu->next;
  428. pt = pu;
  429. pu = pu->next;
  430. free(pt);
  431. } else {
  432. for (x=0;x<AST_MAX_FDS;x++) {
  433. if ((pu->chan->fds[x] > -1) && (FD_ISSET(pu->chan->fds[x], &rfds) || FD_ISSET(pu->chan->fds[x], &efds))) {
  434. if (FD_ISSET(pu->chan->fds[x], &efds))
  435. pu->chan->exception = 1;
  436. pu->chan->fdno = x;
  437. /* See if they need servicing */
  438. f = ast_read(pu->chan);
  439. if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
  440. /* There's a problem, hang them up*/
  441. if (option_verbose > 1)
  442. ast_verbose(VERBOSE_PREFIX_2 "%s got tired of being parked\n", pu->chan->name);
  443. ast_hangup(pu->chan);
  444. /* And take them out of the parking lot */
  445. if (pl)
  446. pl->next = pu->next;
  447. else
  448. parkinglot = pu->next;
  449. pt = pu;
  450. pu = pu->next;
  451. free(pt);
  452. break;
  453. } else {
  454. /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
  455. ast_frfree(f);
  456. goto std; /* XXX Ick: jumping into an else statement??? XXX */
  457. }
  458. }
  459. }
  460. if (x >= AST_MAX_FDS) {
  461. std: for (x=0;x<AST_MAX_FDS;x++) {
  462. /* Keep this one for next one */
  463. if (pu->chan->fds[x] > -1) {
  464. FD_SET(pu->chan->fds[x], &nrfds);
  465. FD_SET(pu->chan->fds[x], &nefds);
  466. if (pu->chan->fds[x] > max)
  467. max = pu->chan->fds[x];
  468. }
  469. }
  470. /* Keep track of our longest wait */
  471. if ((tms < ms) || (ms < 0))
  472. ms = tms;
  473. pl = pu;
  474. pu = pu->next;
  475. }
  476. }
  477. }
  478. ast_mutex_unlock(&parking_lock);
  479. rfds = nrfds;
  480. efds = nefds;
  481. tv.tv_sec = ms / 1000;
  482. tv.tv_usec = (ms % 1000) * 1000;
  483. /* Wait for something to happen */
  484. ast_select(max + 1, &rfds, NULL, &efds, (ms > -1) ? &tv : NULL);
  485. pthread_testcancel();
  486. }
  487. return NULL; /* Never reached */
  488. }
  489. static int park_exec(struct ast_channel *chan, void *data)
  490. {
  491. int res=0;
  492. struct localuser *u;
  493. struct ast_channel *peer=NULL;
  494. struct parkeduser *pu, *pl=NULL;
  495. int park;
  496. int dres;
  497. if (!data) {
  498. ast_log(LOG_WARNING, "Park requires an argument (extension number)\n");
  499. return -1;
  500. }
  501. LOCAL_USER_ADD(u);
  502. park = atoi((char *)data);
  503. ast_mutex_lock(&parking_lock);
  504. pu = parkinglot;
  505. while(pu) {
  506. if (pu->parkingnum == park) {
  507. if (pl)
  508. pl->next = pu->next;
  509. else
  510. parkinglot = pu->next;
  511. break;
  512. }
  513. pl = pu;
  514. pu = pu->next;
  515. }
  516. ast_mutex_unlock(&parking_lock);
  517. if (pu) {
  518. peer = pu->chan;
  519. free(pu);
  520. }
  521. /* JK02: it helps to answer the channel if not already up */
  522. if (chan->_state != AST_STATE_UP) {
  523. ast_answer(chan);
  524. }
  525. if (peer) {
  526. ast_moh_stop(peer);
  527. res = ast_channel_make_compatible(chan, peer);
  528. if (res < 0) {
  529. ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
  530. ast_hangup(peer);
  531. return -1;
  532. }
  533. /* This runs sorta backwards, since we give the incoming channel control, as if it
  534. were the person called. */
  535. if (option_verbose > 2)
  536. ast_verbose(VERBOSE_PREFIX_3 "Channel %s connected to parked call %d\n", chan->name, park);
  537. res = ast_bridge_call(peer, chan, 1, 1, 0);
  538. /* Simulate the PBX hanging up */
  539. if (res != AST_PBX_KEEPALIVE)
  540. ast_hangup(peer);
  541. return -1;
  542. } else {
  543. /* XXX Play a message XXX */
  544. dres = ast_streamfile(chan, "pbx-invalidpark", chan->language);
  545. if (!dres)
  546. dres = ast_waitstream(chan, "");
  547. else {
  548. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
  549. dres = 0;
  550. }
  551. if (option_verbose > 2)
  552. ast_verbose(VERBOSE_PREFIX_3 "Channel %s tried to talk to non-existant parked call %d\n", chan->name, park);
  553. res = -1;
  554. }
  555. LOCAL_USER_REMOVE(u);
  556. return res;
  557. }
  558. static int handle_parkedcalls(int fd, int argc, char *argv[])
  559. {
  560. struct parkeduser *cur;
  561. ast_cli(fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
  562. , "Context", "Extension", "Pri", "Timeout");
  563. ast_mutex_lock(&parking_lock);
  564. cur=parkinglot;
  565. while(cur) {
  566. ast_cli(fd, "%4d %25s (%-15s %-12s %-4d) %6lds\n"
  567. ,cur->parkingnum, cur->chan->name, cur->context, cur->exten
  568. ,cur->priority, cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL));
  569. cur = cur->next;
  570. }
  571. ast_mutex_unlock(&parking_lock);
  572. return RESULT_SUCCESS;
  573. }
  574. static char showparked_help[] =
  575. "Usage: show parkedcalls\n"
  576. " Lists currently parked calls.\n";
  577. static struct ast_cli_entry showparked =
  578. { { "show", "parkedcalls", NULL }, handle_parkedcalls, "Lists parked calls", showparked_help };
  579. /* Dump lot status */
  580. static int manager_parking_status( struct mansession *s, struct message *m )
  581. {
  582. struct parkeduser *cur;
  583. astman_send_ack(s, m, "Parked calls will follow");
  584. ast_mutex_lock(&parking_lock);
  585. cur=parkinglot;
  586. while(cur) {
  587. ast_cli(s->fd, "Event: ParkedCall\r\n"
  588. "Exten: %d\r\n"
  589. "Channel: %s\r\n"
  590. "Timeout: %ld\r\n"
  591. "CallerID: %s\r\n"
  592. "\r\n"
  593. ,cur->parkingnum, cur->chan->name
  594. ,(long)cur->start.tv_sec + (long)(cur->parkingtime/1000) - (long)time(NULL)
  595. ,(cur->chan->callerid ? cur->chan->callerid : "")
  596. );
  597. cur = cur->next;
  598. }
  599. ast_mutex_unlock(&parking_lock);
  600. return RESULT_SUCCESS;
  601. }
  602. int load_module(void)
  603. {
  604. int res;
  605. int x;
  606. int start, end;
  607. struct ast_context *con;
  608. char exten[AST_MAX_EXTENSION];
  609. struct ast_config *cfg;
  610. struct ast_variable *var;
  611. ast_cli_register(&showparked);
  612. cfg = ast_load("parking.conf");
  613. if (cfg) {
  614. var = ast_variable_browse(cfg, "general");
  615. while(var) {
  616. if (!strcasecmp(var->name, "parkext")) {
  617. strncpy(parking_ext, var->value, sizeof(parking_ext) - 1);
  618. } else if (!strcasecmp(var->name, "context")) {
  619. strncpy(parking_con, var->value, sizeof(parking_con) - 1);
  620. } else if (!strcasecmp(var->name, "parkingtime")) {
  621. if ((sscanf(var->value, "%d", &parkingtime) != 1) || (parkingtime < 1)) {
  622. ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
  623. parkingtime = DEFAULT_PARK_TIME;
  624. } else
  625. parkingtime = parkingtime * 1000;
  626. } else if (!strcasecmp(var->name, "parkpos")) {
  627. if (sscanf(var->value, "%i-%i", &start, &end) != 2) {
  628. 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);
  629. } else {
  630. parking_start = start;
  631. parking_stop = end;
  632. }
  633. }
  634. var = var->next;
  635. }
  636. ast_destroy(cfg);
  637. }
  638. con = ast_context_find(parking_con);
  639. if (!con) {
  640. con = ast_context_create(NULL,parking_con, registrar);
  641. if (!con) {
  642. ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parking_con);
  643. return -1;
  644. }
  645. }
  646. for(x=parking_start; x<=parking_stop;x++) {
  647. snprintf(exten, sizeof(exten), "%d", x);
  648. ast_add_extension2(con, 1, exten, 1, NULL, parkedcall, strdup(exten), free, registrar);
  649. }
  650. pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
  651. res = ast_register_application(parkedcall, park_exec, synopsis, descrip);
  652. if (!res) {
  653. ast_manager_register( "ParkedCalls", 0, manager_parking_status, "List parked calls" );
  654. }
  655. return res;
  656. }
  657. int ast_pickup_call(struct ast_channel *chan)
  658. {
  659. struct ast_channel *cur;
  660. int res = -1;
  661. cur = ast_channel_walk(NULL);
  662. while(cur) {
  663. if (!cur->pbx &&
  664. (cur != chan) &&
  665. (chan->pickupgroup & cur->callgroup) &&
  666. ((cur->_state == AST_STATE_RINGING) ||
  667. (cur->_state == AST_STATE_RING))) {
  668. break;
  669. }
  670. cur = ast_channel_walk(cur);
  671. }
  672. if (cur) {
  673. ast_log(LOG_DEBUG, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
  674. res = ast_answer(chan);
  675. if (res)
  676. ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
  677. res = ast_queue_control(chan, AST_CONTROL_ANSWER, 0);
  678. if (res)
  679. ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
  680. res = ast_channel_masquerade(cur, chan);
  681. if (res)
  682. ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name); /* Done */
  683. } else {
  684. ast_log(LOG_DEBUG, "No call pickup possible...\n");
  685. }
  686. return res;
  687. }
  688. int unload_module(void)
  689. {
  690. STANDARD_HANGUP_LOCALUSERS;
  691. ast_manager_unregister( "ParkedCalls" );
  692. ast_cli_unregister(&showparked);
  693. return ast_unregister_application(parkedcall);
  694. }
  695. char *description(void)
  696. {
  697. return "Call Parking Resource";
  698. }
  699. int usecount(void)
  700. {
  701. /* Never allow parking to be unloaded because it will
  702. unresolve needed symbols in the dialer */
  703. #if 0
  704. int res;
  705. STANDARD_USECOUNT(res);
  706. return res;
  707. #else
  708. return 1;
  709. #endif
  710. }
  711. char *key()
  712. {
  713. return ASTERISK_GPL_KEY;
  714. }