app_queue.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * True call queues with optional send URL on answer
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * 2004-06-04: Priorities in queues added by inAccess Networks (work funded by Hellas On Line (HOL) www.hol.gr).
  11. *
  12. * These features added by David C. Troy <dave@toad.net>:
  13. * - Per-queue holdtime calculation
  14. * - Estimated holdtime announcement
  15. * - Position announcement
  16. * - Abandoned/completed call counters
  17. * - Failout timer passed as optional app parameter
  18. * - Optional monitoring of calls, started when call is answered
  19. *
  20. * Patch Version 1.07 2003-12-24 01
  21. *
  22. * Added servicelevel statistic by Michiel Betel <michiel@betel.nl>
  23. * Added Priority jumping code for adding and removing queue members by Jonathan Stanton <asterisk@doilooklikeicare.com>
  24. *
  25. * Fixed ot work with CVS as of 2004-02-25 and released as 1.07a
  26. * by Matthew Enger <m.enger@xi.com.au>
  27. *
  28. * This program is free software, distributed under the terms of
  29. * the GNU General Public License
  30. */
  31. #include <asterisk/lock.h>
  32. #include <asterisk/file.h>
  33. #include <asterisk/logger.h>
  34. #include <asterisk/channel.h>
  35. #include <asterisk/pbx.h>
  36. #include <asterisk/options.h>
  37. #include <asterisk/module.h>
  38. #include <asterisk/translate.h>
  39. #include <asterisk/say.h>
  40. #include <asterisk/features.h>
  41. #include <asterisk/musiconhold.h>
  42. #include <asterisk/cli.h>
  43. #include <asterisk/manager.h>
  44. #include <asterisk/config.h>
  45. #include <asterisk/monitor.h>
  46. #include <asterisk/utils.h>
  47. #include <stdlib.h>
  48. #include <errno.h>
  49. #include <unistd.h>
  50. #include <string.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <sys/time.h>
  54. #include <sys/signal.h>
  55. #include <netinet/in.h>
  56. #include "../astconf.h"
  57. #define QUEUE_STRATEGY_RINGALL 0
  58. #define QUEUE_STRATEGY_ROUNDROBIN 1
  59. #define QUEUE_STRATEGY_LEASTRECENT 2
  60. #define QUEUE_STRATEGY_FEWESTCALLS 3
  61. #define QUEUE_STRATEGY_RANDOM 4
  62. #define QUEUE_STRATEGY_RRMEMORY 5
  63. static struct strategy {
  64. int strategy;
  65. char *name;
  66. } strategies[] = {
  67. { QUEUE_STRATEGY_RINGALL, "ringall" },
  68. { QUEUE_STRATEGY_ROUNDROBIN, "roundrobin" },
  69. { QUEUE_STRATEGY_LEASTRECENT, "leastrecent" },
  70. { QUEUE_STRATEGY_FEWESTCALLS, "fewestcalls" },
  71. { QUEUE_STRATEGY_RANDOM, "random" },
  72. { QUEUE_STRATEGY_RRMEMORY, "rrmemory" },
  73. };
  74. #define DEFAULT_RETRY 5
  75. #define DEFAULT_TIMEOUT 15
  76. #define RECHECK 1 /* Recheck every second to see we we're at the top yet */
  77. #define RES_OKAY 0 /* Action completed */
  78. #define RES_EXISTS (-1) /* Entry already exists */
  79. #define RES_OUTOFMEMORY (-2) /* Out of memory */
  80. #define RES_NOSUCHQUEUE (-3) /* No such queue */
  81. static char *tdesc = "True Call Queueing";
  82. static char *app = "Queue";
  83. static char *synopsis = "Queue a call for a call queue";
  84. static char *descrip =
  85. " Queue(queuename[|options[|URL][|announceoverride][|timeout]]):\n"
  86. "Queues an incoming call in a particular call queue as defined in queues.conf.\n"
  87. " This application returns -1 if the originating channel hangs up, or if the\n"
  88. "call is bridged and either of the parties in the bridge terminate the call.\n"
  89. "Returns 0 if the queue is full, nonexistent, or has no members.\n"
  90. "The option string may contain zero or more of the following characters:\n"
  91. " 't' -- allow the called user transfer the calling user\n"
  92. " 'T' -- to allow the calling user to transfer the call.\n"
  93. " 'd' -- data-quality (modem) call (minimum delay).\n"
  94. " 'h' -- allow callee to hang up by hitting *.\n"
  95. " 'H' -- allow caller to hang up by hitting *.\n"
  96. " 'n' -- no retries on the timeout; will exit this application and go to the next step.\n"
  97. " 'r' -- ring instead of playing MOH\n"
  98. " In addition to transferring the call, a call may be parked and then picked\n"
  99. "up by another user.\n"
  100. " The optional URL will be sent to the called party if the channel supports\n"
  101. "it.\n"
  102. " The timeout will cause the queue to fail out after a specified number of\n"
  103. "seconds, checked between each queues.conf 'timeout' and 'retry' cycle.\n";
  104. // [PHM 06/26/03]
  105. static char *app_aqm = "AddQueueMember" ;
  106. static char *app_aqm_synopsis = "Dynamically adds queue members" ;
  107. static char *app_aqm_descrip =
  108. " AddQueueMember(queuename[|interface[|penalty]]):\n"
  109. "Dynamically adds interface to an existing queue.\n"
  110. "If the interface is already in the queue and there exists an n+101 priority\n"
  111. "then it will then jump to this priority. Otherwise it will return an error\n"
  112. "Returns -1 if there is an error.\n"
  113. "Example: AddQueueMember(techsupport|SIP/3000)\n"
  114. "";
  115. static char *app_rqm = "RemoveQueueMember" ;
  116. static char *app_rqm_synopsis = "Dynamically removes queue members" ;
  117. static char *app_rqm_descrip =
  118. " RemoveQueueMember(queuename[|interface]):\n"
  119. "Dynamically removes interface to an existing queue\n"
  120. "If the interface is NOT in the queue and there exists an n+101 priority\n"
  121. "then it will then jump to this priority. Otherwise it will return an error\n"
  122. "Returns -1 if there is an error.\n"
  123. "Example: RemoveQueueMember(techsupport|SIP/3000)\n"
  124. "";
  125. /* We define a customer "local user" structure because we
  126. use it not only for keeping track of what is in use but
  127. also for keeping track of who we're dialing. */
  128. struct localuser {
  129. struct ast_channel *chan;
  130. char numsubst[256];
  131. char tech[40];
  132. int stillgoing;
  133. int metric;
  134. int allowredirect_in;
  135. int allowredirect_out;
  136. int ringbackonly;
  137. int musiconhold;
  138. int dataquality;
  139. int allowdisconnect_in;
  140. int allowdisconnect_out;
  141. time_t lastcall;
  142. struct member *member;
  143. struct localuser *next;
  144. };
  145. LOCAL_USER_DECL;
  146. struct queue_ent {
  147. struct ast_call_queue *parent; /* What queue is our parent */
  148. char moh[80]; /* Name of musiconhold to be used */
  149. char announce[80]; /* Announcement to play for member when call is answered */
  150. char context[80]; /* Context when user exits queue */
  151. int pos; /* Where we are in the queue */
  152. int prio; /* Our priority */
  153. int last_pos_said; /* Last position we told the user */
  154. time_t last_pos; /* Last time we told the user their position */
  155. int opos; /* Where we started in the queue */
  156. int handled; /* Whether our call was handled */
  157. time_t start; /* When we started holding */
  158. int queuetimeout; /* How many seconds before timing out of queue */
  159. struct ast_channel *chan; /* Our channel */
  160. struct queue_ent *next; /* The next queue entry */
  161. };
  162. struct member {
  163. char tech[80]; /* Technology */
  164. char loc[256]; /* Location */
  165. int penalty; /* Are we a last resort? */
  166. int calls; /* Number of calls serviced by this member */
  167. int dynamic; /* Are we dynamically added? */
  168. time_t lastcall; /* When last successful call was hungup */
  169. struct member *next; /* Next member */
  170. };
  171. struct ast_call_queue {
  172. ast_mutex_t lock;
  173. char name[80]; /* Name of the queue */
  174. char moh[80]; /* Name of musiconhold to be used */
  175. char announce[80]; /* Announcement to play when call is answered */
  176. char context[80]; /* Context for this queue */
  177. int strategy; /* Queueing strategy */
  178. int announcefrequency; /* How often to announce their position */
  179. int roundingseconds; /* How many seconds do we round to? */
  180. int announceholdtime; /* When to announce holdtime: 0 = never, -1 = every announcement, 1 = only once */
  181. int holdtime; /* Current avg holdtime for this queue, based on recursive boxcar filter */
  182. int callscompleted; /* Number of queue calls completed */
  183. int callsabandoned; /* Number of queue calls abandoned */
  184. int servicelevel; /* seconds setting for servicelevel*/
  185. int callscompletedinsl; /* Number of queue calls answererd with servicelevel*/
  186. char monfmt[8]; /* Format to use when recording calls */
  187. int monjoin; /* Should we join the two files when we are done with the call */
  188. char sound_next[80]; /* Sound file: "Your call is now first in line" (def. queue-youarenext) */
  189. char sound_thereare[80]; /* Sound file: "There are currently" (def. queue-thereare) */
  190. char sound_calls[80]; /* Sound file: "calls waiting to speak to a representative." (def. queue-callswaiting)*/
  191. char sound_holdtime[80]; /* Sound file: "The current estimated total holdtime is" (def. queue-holdtime) */
  192. char sound_minutes[80]; /* Sound file: "minutes." (def. queue-minutes) */
  193. char sound_lessthan[80]; /* Sound file: "less-than" (def. queue-lessthan) */
  194. char sound_seconds[80]; /* Sound file: "seconds." (def. queue-seconds) */
  195. char sound_thanks[80]; /* Sound file: "Thank you for your patience." (def. queue-thankyou) */
  196. int count; /* How many entries are in the queue */
  197. int maxlen; /* Max number of entries in queue */
  198. int wrapuptime; /* Wrapup Time */
  199. int dead; /* Whether this queue is dead or not */
  200. int retry; /* Retry calling everyone after this amount of time */
  201. int timeout; /* How long to wait for an answer */
  202. /* Queue strategy things */
  203. int rrpos; /* Round Robin - position */
  204. int wrapped; /* Round Robin - wrapped around? */
  205. int joinempty; /* Do we care if the queue has no members? */
  206. int eventwhencalled; /* Generate an event when the agent is called (before pickup) */
  207. int leavewhenempty; /* If all agents leave the queue, remove callers from the queue */
  208. struct member *members; /* Member channels to be tried */
  209. struct queue_ent *head; /* Start of the actual queue */
  210. struct ast_call_queue *next; /* Next call queue */
  211. };
  212. static struct ast_call_queue *queues = NULL;
  213. AST_MUTEX_DEFINE_STATIC(qlock);
  214. static char *int2strat(int strategy)
  215. {
  216. int x;
  217. for (x=0;x<sizeof(strategies) / sizeof(strategies[0]);x++) {
  218. if (strategy == strategies[x].strategy)
  219. return strategies[x].name;
  220. }
  221. return "<unknown>";
  222. }
  223. static int strat2int(char *strategy)
  224. {
  225. int x;
  226. for (x=0;x<sizeof(strategies) / sizeof(strategies[0]);x++) {
  227. if (!strcasecmp(strategy, strategies[x].name))
  228. return strategies[x].strategy;
  229. }
  230. return -1;
  231. }
  232. /* Insert the 'new' entry after the 'prev' entry of queue 'q' */
  233. static inline void insert_entry(struct ast_call_queue *q,
  234. struct queue_ent *prev, struct queue_ent *new, int *pos)
  235. {
  236. struct queue_ent *cur;
  237. if (!q || !new)
  238. return;
  239. if (prev) {
  240. cur = prev->next;
  241. prev->next = new;
  242. } else {
  243. cur = q->head;
  244. q->head = new;
  245. }
  246. new->next = cur;
  247. new->parent = q;
  248. new->pos = ++(*pos);
  249. new->opos = *pos;
  250. }
  251. static int join_queue(char *queuename, struct queue_ent *qe)
  252. {
  253. struct ast_call_queue *q;
  254. struct queue_ent *cur, *prev = NULL;
  255. int res = -1;
  256. int pos = 0;
  257. int inserted = 0;
  258. ast_mutex_lock(&qlock);
  259. for (q = queues; q; q = q->next) {
  260. if (!strcasecmp(q->name, queuename)) {
  261. /* This is our one */
  262. ast_mutex_lock(&q->lock);
  263. if ((q->members || q->joinempty) && (!q->maxlen || (q->count < q->maxlen))) {
  264. /* There's space for us, put us at the right position inside
  265. * the queue.
  266. * Take into account the priority of the calling user */
  267. inserted = 0;
  268. prev = NULL;
  269. cur = q->head;
  270. while(cur) {
  271. /* We have higher priority than the current user, enter
  272. * before him, after all the other users with priority
  273. * higher or equal to our priority. */
  274. if ((!inserted) && (qe->prio > cur->prio)) {
  275. insert_entry(q, prev, qe, &pos);
  276. inserted = 1;
  277. }
  278. cur->pos = ++pos;
  279. prev = cur;
  280. cur = cur->next;
  281. }
  282. /* No luck, join at the end of the queue */
  283. if (!inserted)
  284. insert_entry(q, prev, qe, &pos);
  285. strncpy(qe->moh, q->moh, sizeof(qe->moh) - 1);
  286. strncpy(qe->announce, q->announce, sizeof(qe->announce) - 1);
  287. strncpy(qe->context, q->context, sizeof(qe->context) - 1);
  288. q->count++;
  289. res = 0;
  290. manager_event(EVENT_FLAG_CALL, "Join",
  291. "Channel: %s\r\nCallerID: %s\r\nQueue: %s\r\nPosition: %d\r\nCount: %d\r\n",
  292. qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : "unknown"), q->name, qe->pos, q->count );
  293. #if 0
  294. ast_log(LOG_NOTICE, "Queue '%s' Join, Channel '%s', Position '%d'\n", q->name, qe->chan->name, qe->pos );
  295. #endif
  296. }
  297. ast_mutex_unlock(&q->lock);
  298. break;
  299. }
  300. }
  301. ast_mutex_unlock(&qlock);
  302. return res;
  303. }
  304. static void free_members(struct ast_call_queue *q, int all)
  305. {
  306. /* Free non-dynamic members */
  307. struct member *curm, *next, *prev;
  308. curm = q->members;
  309. prev = NULL;
  310. while(curm) {
  311. next = curm->next;
  312. if (all || !curm->dynamic) {
  313. if (prev)
  314. prev->next = next;
  315. else
  316. q->members = next;
  317. free(curm);
  318. } else
  319. prev = curm;
  320. curm = next;
  321. }
  322. }
  323. static void destroy_queue(struct ast_call_queue *q)
  324. {
  325. struct ast_call_queue *cur, *prev = NULL;
  326. ast_mutex_lock(&qlock);
  327. for (cur = queues; cur; cur = cur->next) {
  328. if (cur == q) {
  329. if (prev)
  330. prev->next = cur->next;
  331. else
  332. queues = cur->next;
  333. } else {
  334. prev = cur;
  335. }
  336. }
  337. ast_mutex_unlock(&qlock);
  338. free_members(q, 1);
  339. ast_mutex_destroy(&q->lock);
  340. free(q);
  341. }
  342. static int play_file(struct ast_channel *chan, char *filename)
  343. {
  344. int res;
  345. ast_stopstream(chan);
  346. res = ast_streamfile(chan, filename, chan->language);
  347. if (!res)
  348. res = ast_waitstream(chan, "");
  349. else
  350. res = 0;
  351. if (res) {
  352. ast_log(LOG_WARNING, "ast_streamfile failed on %s \n", chan->name);
  353. res = 0;
  354. }
  355. ast_stopstream(chan);
  356. return res;
  357. }
  358. static int say_position(struct queue_ent *qe)
  359. {
  360. int res = 0, avgholdmins, avgholdsecs;
  361. time_t now;
  362. /* Check to see if this is ludicrous -- if we just announced position, don't do it again*/
  363. time(&now);
  364. if ( (now - qe->last_pos) < 15 )
  365. return -1;
  366. /* If either our position has changed, or we are over the freq timer, say position */
  367. if ( (qe->last_pos_said == qe->pos) && ((now - qe->last_pos) < qe->parent->announcefrequency) )
  368. return -1;
  369. ast_moh_stop(qe->chan);
  370. /* Say we're next, if we are */
  371. if (qe->pos == 1) {
  372. res += play_file(qe->chan, qe->parent->sound_next);
  373. goto posout;
  374. } else {
  375. res += play_file(qe->chan, qe->parent->sound_thereare);
  376. res += ast_say_number(qe->chan, qe->pos, AST_DIGIT_ANY, qe->chan->language, (char *) NULL); /* Needs gender */
  377. res += play_file(qe->chan, qe->parent->sound_calls);
  378. }
  379. /* Round hold time to nearest minute */
  380. avgholdmins = abs(( (qe->parent->holdtime + 30) - (now - qe->start) ) / 60);
  381. /* If they have specified a rounding then round the seconds as well */
  382. if(qe->parent->roundingseconds) {
  383. avgholdsecs = (abs(( (qe->parent->holdtime + 30) - (now - qe->start) )) - 60 * avgholdmins) / qe->parent->roundingseconds;
  384. avgholdsecs*= qe->parent->roundingseconds;
  385. } else {
  386. avgholdsecs=0;
  387. }
  388. if (option_verbose > 2)
  389. ast_verbose(VERBOSE_PREFIX_3 "Hold time for %s is %d minutes %d seconds\n", qe->parent->name, avgholdmins, avgholdsecs);
  390. /* If the hold time is >1 min, if it's enabled, and if it's not
  391. supposed to be only once and we have already said it, say it */
  392. if ((avgholdmins+avgholdsecs) > 0 && (qe->parent->announceholdtime) && (!(qe->parent->announceholdtime==1 && qe->last_pos)) ) {
  393. res += play_file(qe->chan, qe->parent->sound_holdtime);
  394. if(avgholdmins>0) {
  395. if (avgholdmins < 2) {
  396. res += play_file(qe->chan, qe->parent->sound_lessthan);
  397. res += ast_say_number(qe->chan, 2, AST_DIGIT_ANY, qe->chan->language, (char *)NULL);
  398. } else
  399. res += ast_say_number(qe->chan, avgholdmins, AST_DIGIT_ANY, qe->chan->language, (char*) NULL);
  400. res += play_file(qe->chan, qe->parent->sound_minutes);
  401. }
  402. if(avgholdsecs>0) {
  403. res += ast_say_number(qe->chan, avgholdsecs, AST_DIGIT_ANY, qe->chan->language, (char*) NULL);
  404. res += play_file(qe->chan, qe->parent->sound_seconds);
  405. }
  406. }
  407. posout:
  408. /* Set our last_pos indicators */
  409. qe->last_pos = now;
  410. qe->last_pos_said = qe->pos;
  411. if (option_verbose > 2)
  412. ast_verbose(VERBOSE_PREFIX_3 "Told %s in %s their queue position (which was %d)\n", qe->chan->name, qe->parent->name, qe->pos);
  413. res += play_file(qe->chan, qe->parent->sound_thanks);
  414. ast_moh_start(qe->chan, qe->moh);
  415. return (res>0);
  416. }
  417. static void record_abandoned(struct queue_ent *qe)
  418. {
  419. ast_mutex_lock(&qe->parent->lock);
  420. qe->parent->callsabandoned++;
  421. ast_mutex_unlock(&qe->parent->lock);
  422. }
  423. static void recalc_holdtime(struct queue_ent *qe)
  424. {
  425. int oldvalue, newvalue;
  426. /* Calculate holdtime using a recursive boxcar filter */
  427. /* Thanks to SRT for this contribution */
  428. /* 2^2 (4) is the filter coefficient; a higher exponent would give old entries more weight */
  429. newvalue = time(NULL) - qe->start;
  430. ast_mutex_lock(&qe->parent->lock);
  431. if (newvalue <= qe->parent->servicelevel)
  432. qe->parent->callscompletedinsl++;
  433. oldvalue = qe->parent->holdtime;
  434. qe->parent->holdtime = (((oldvalue << 2) - oldvalue) + newvalue) >> 2;
  435. ast_mutex_unlock(&qe->parent->lock);
  436. }
  437. static void leave_queue(struct queue_ent *qe)
  438. {
  439. struct ast_call_queue *q;
  440. struct queue_ent *cur, *prev = NULL;
  441. int pos = 0;
  442. q = qe->parent;
  443. if (!q)
  444. return;
  445. ast_mutex_lock(&q->lock);
  446. prev = NULL;
  447. cur = q->head;
  448. while(cur) {
  449. if (cur == qe) {
  450. q->count--;
  451. /* Take us out of the queue */
  452. manager_event(EVENT_FLAG_CALL, "Leave",
  453. "Channel: %s\r\nQueue: %s\r\nCount: %d\r\n",
  454. qe->chan->name, q->name, q->count);
  455. #if 0
  456. ast_log(LOG_NOTICE, "Queue '%s' Leave, Channel '%s'\n", q->name, qe->chan->name );
  457. #endif
  458. /* Take us out of the queue */
  459. if (prev)
  460. prev->next = cur->next;
  461. else
  462. q->head = cur->next;
  463. } else {
  464. /* Renumber the people after us in the queue based on a new count */
  465. cur->pos = ++pos;
  466. prev = cur;
  467. }
  468. cur = cur->next;
  469. }
  470. ast_mutex_unlock(&q->lock);
  471. if (q->dead && !q->count) {
  472. /* It's dead and nobody is in it, so kill it */
  473. destroy_queue(q);
  474. }
  475. }
  476. static void hanguptree(struct localuser *outgoing, struct ast_channel *exception)
  477. {
  478. /* Hang up a tree of stuff */
  479. struct localuser *oo;
  480. while(outgoing) {
  481. /* Hangup any existing lines we have open */
  482. if (outgoing->chan && (outgoing->chan != exception))
  483. ast_hangup(outgoing->chan);
  484. oo = outgoing;
  485. outgoing=outgoing->next;
  486. free(oo);
  487. }
  488. }
  489. static int ring_entry(struct queue_ent *qe, struct localuser *tmp, int *busies)
  490. {
  491. int res;
  492. struct ast_var_t *current, *newvar;
  493. struct varshead *headp, *newheadp;
  494. if (qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime)) {
  495. ast_log(LOG_DEBUG, "Wrapuptime not yet expired for %s/%s\n", tmp->tech, tmp->numsubst);
  496. if (qe->chan->cdr)
  497. ast_cdr_busy(qe->chan->cdr);
  498. tmp->stillgoing = 0;
  499. (*busies)++;
  500. return 0;
  501. }
  502. /* Request the peer */
  503. tmp->chan = ast_request(tmp->tech, qe->chan->nativeformats, tmp->numsubst);
  504. if (!tmp->chan) { /* If we can't, just go on to the next call */
  505. #if 0
  506. ast_log(LOG_NOTICE, "Unable to create channel of type '%s'\n", cur->tech);
  507. #endif
  508. if (qe->chan->cdr)
  509. ast_cdr_busy(qe->chan->cdr);
  510. tmp->stillgoing = 0;
  511. (*busies)++;
  512. return 0;
  513. }
  514. /* If creating a SIP channel, look for a variable called */
  515. /* VXML_URL in the calling channel and copy it to the */
  516. /* new channel. */
  517. /* Check for ALERT_INFO in the SetVar list. This is for */
  518. /* SIP distinctive ring as per the RFC. For Cisco 7960s, */
  519. /* SetVar(ALERT_INFO=<x>) where x is an integer value 1-5. */
  520. /* However, the RFC says it should be a URL. -km- */
  521. headp=&qe->chan->varshead;
  522. AST_LIST_TRAVERSE(headp,current,entries) {
  523. if (!strcasecmp(ast_var_name(current),"VXML_URL") ||
  524. !strcasecmp(ast_var_name(current), "ALERT_INFO") ||
  525. !strcasecmp(ast_var_name(current), "OSPTOKEN") ||
  526. !strcasecmp(ast_var_name(current), "OSPHANDLE"))
  527. {
  528. newvar=ast_var_assign(ast_var_name(current),ast_var_value(current));
  529. newheadp=&tmp->chan->varshead;
  530. AST_LIST_INSERT_HEAD(newheadp,newvar,entries);
  531. }
  532. }
  533. tmp->chan->appl = "AppQueue";
  534. tmp->chan->data = "(Outgoing Line)";
  535. tmp->chan->whentohangup = 0;
  536. if (tmp->chan->callerid)
  537. free(tmp->chan->callerid);
  538. if (tmp->chan->ani)
  539. free(tmp->chan->ani);
  540. if (qe->chan->callerid)
  541. tmp->chan->callerid = strdup(qe->chan->callerid);
  542. else
  543. tmp->chan->callerid = NULL;
  544. if (qe->chan->ani)
  545. tmp->chan->ani = strdup(qe->chan->ani);
  546. else
  547. tmp->chan->ani = NULL;
  548. /* Presense of ADSI CPE on outgoing channel follows ours */
  549. tmp->chan->adsicpe = qe->chan->adsicpe;
  550. /* Place the call, but don't wait on the answer */
  551. res = ast_call(tmp->chan, tmp->numsubst, 0);
  552. if (res) {
  553. /* Again, keep going even if there's an error */
  554. if (option_debug)
  555. ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
  556. else if (option_verbose > 2)
  557. ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", tmp->numsubst);
  558. ast_hangup(tmp->chan);
  559. tmp->chan = NULL;
  560. tmp->stillgoing = 0;
  561. (*busies)++;
  562. return 0;
  563. } else {
  564. if (qe->parent->eventwhencalled) {
  565. manager_event(EVENT_FLAG_AGENT, "AgentCalled",
  566. "AgentCalled: %s/%s\r\n"
  567. "ChannelCalling: %s\r\n"
  568. "CallerID: %s\r\n"
  569. "Context: %s\r\n"
  570. "Extension: %s\r\n"
  571. "Priority: %d\r\n",
  572. tmp->tech, tmp->numsubst, qe->chan->name,
  573. tmp->chan->callerid ? tmp->chan->callerid : "unknown <>",
  574. qe->chan->context, qe->chan->exten, qe->chan->priority);
  575. }
  576. if (option_verbose > 2)
  577. ast_verbose(VERBOSE_PREFIX_3 "Called %s/%s\n", tmp->tech, tmp->numsubst);
  578. }
  579. return 1;
  580. }
  581. static int ring_one(struct queue_ent *qe, struct localuser *outgoing, int *busies)
  582. {
  583. struct localuser *cur;
  584. struct localuser *best;
  585. int bestmetric=0;
  586. do {
  587. best = NULL;
  588. cur = outgoing;
  589. while(cur) {
  590. if (cur->stillgoing && /* Not already done */
  591. !cur->chan && /* Isn't already going */
  592. (!best || (cur->metric < bestmetric))) { /* We haven't found one yet, or it's better */
  593. bestmetric = cur->metric;
  594. best = cur;
  595. }
  596. cur = cur->next;
  597. }
  598. if (best) {
  599. if (!qe->parent->strategy) {
  600. /* Ring everyone who shares this best metric (for ringall) */
  601. cur = outgoing;
  602. while(cur) {
  603. if (cur->stillgoing && !cur->chan && (cur->metric <= bestmetric)) {
  604. ast_log(LOG_DEBUG, "(Parallel) Trying '%s/%s' with metric %d\n", cur->tech, cur->numsubst, cur->metric);
  605. ring_entry(qe, cur, busies);
  606. }
  607. cur = cur->next;
  608. }
  609. } else {
  610. /* Ring just the best channel */
  611. if (option_debug)
  612. ast_log(LOG_DEBUG, "Trying '%s/%s' with metric %d\n",
  613. best->tech, best->numsubst, best->metric);
  614. ring_entry(qe, best, busies);
  615. }
  616. }
  617. } while (best && !best->chan);
  618. if (!best) {
  619. if (option_debug)
  620. ast_log(LOG_DEBUG, "Nobody left to try ringing in queue\n");
  621. return 0;
  622. }
  623. return 1;
  624. }
  625. static int store_next(struct queue_ent *qe, struct localuser *outgoing)
  626. {
  627. struct localuser *cur;
  628. struct localuser *best;
  629. int bestmetric=0;
  630. best = NULL;
  631. cur = outgoing;
  632. while(cur) {
  633. if (cur->stillgoing && /* Not already done */
  634. !cur->chan && /* Isn't already going */
  635. (!best || (cur->metric < bestmetric))) { /* We haven't found one yet, or it's better */
  636. bestmetric = cur->metric;
  637. best = cur;
  638. }
  639. cur = cur->next;
  640. }
  641. if (best) {
  642. /* Ring just the best channel */
  643. ast_log(LOG_DEBUG, "Next is '%s/%s' with metric %d\n", best->tech, best->numsubst, best->metric);
  644. qe->parent->rrpos = best->metric % 1000;
  645. } else {
  646. /* Just increment rrpos */
  647. if (!qe->parent->wrapped) {
  648. /* No more channels, start over */
  649. qe->parent->rrpos = 0;
  650. } else {
  651. /* Prioritize next entry */
  652. qe->parent->rrpos++;
  653. }
  654. }
  655. qe->parent->wrapped = 0;
  656. return 0;
  657. }
  658. static int valid_exit(struct queue_ent *qe, char digit)
  659. {
  660. char tmp[2];
  661. if (ast_strlen_zero(qe->context))
  662. return 0;
  663. tmp[0] = digit;
  664. tmp[1] = '\0';
  665. if (ast_exists_extension(qe->chan, qe->context, tmp, 1, qe->chan->callerid)) {
  666. strncpy(qe->chan->context, qe->context, sizeof(qe->chan->context) - 1);
  667. strncpy(qe->chan->exten, tmp, sizeof(qe->chan->exten) - 1);
  668. qe->chan->priority = 0;
  669. return 1;
  670. }
  671. return 0;
  672. }
  673. #define AST_MAX_WATCHERS 256
  674. #define BUILD_STATS do { \
  675. o = outgoing; \
  676. found = -1; \
  677. pos = 1; \
  678. numlines = 0; \
  679. watchers[0] = in; \
  680. while(o) { \
  681. /* Keep track of important channels */ \
  682. if (o->stillgoing) { \
  683. stillgoing = 1; \
  684. if (o->chan) { \
  685. watchers[pos++] = o->chan; \
  686. found = 1; \
  687. } \
  688. } \
  689. o = o->next; \
  690. numlines++; \
  691. } \
  692. } while(0)
  693. static struct localuser *wait_for_answer(struct queue_ent *qe, struct localuser *outgoing, int *to, int *allowredir_in, int *allowredir_out, int *allowdisconnect_in, int *allowdisconnect_out, char *digit, int prebusies)
  694. {
  695. char *queue = qe->parent->name;
  696. struct localuser *o;
  697. int found;
  698. int numlines;
  699. int sentringing = 0;
  700. int numbusies = prebusies;
  701. int orig = *to;
  702. int stillgoing = 0;
  703. struct ast_frame *f;
  704. struct localuser *peer = NULL;
  705. struct ast_channel *watchers[AST_MAX_WATCHERS];
  706. int pos;
  707. struct ast_channel *winner;
  708. struct ast_channel *in = qe->chan;
  709. while(*to && !peer) {
  710. BUILD_STATS;
  711. if ((found < 0) && stillgoing && !qe->parent->strategy) {
  712. /* On "ringall" strategy we only move to the next penalty level
  713. when *all* ringing phones are done in the current penalty level */
  714. ring_one(qe, outgoing, &numbusies);
  715. BUILD_STATS;
  716. }
  717. if (found < 0) {
  718. if (numlines == numbusies) {
  719. ast_log(LOG_DEBUG, "Everyone is busy at this time\n");
  720. } else {
  721. ast_log(LOG_NOTICE, "No one is answering queue '%s' (%d/%d)\n", queue, numlines, numbusies);
  722. }
  723. *to = 0;
  724. return NULL;
  725. }
  726. winner = ast_waitfor_n(watchers, pos, to);
  727. o = outgoing;
  728. while(o) {
  729. if (o->stillgoing && (o->chan) && (o->chan->_state == AST_STATE_UP)) {
  730. if (!peer) {
  731. if (option_verbose > 2)
  732. ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
  733. peer = o;
  734. *allowredir_in = o->allowredirect_in;
  735. *allowredir_out = o->allowredirect_out;
  736. *allowdisconnect_in = o->allowdisconnect_in;
  737. *allowdisconnect_out = o->allowdisconnect_out;
  738. }
  739. } else if (o->chan && (o->chan == winner)) {
  740. f = ast_read(winner);
  741. if (f) {
  742. if (f->frametype == AST_FRAME_CONTROL) {
  743. switch(f->subclass) {
  744. case AST_CONTROL_ANSWER:
  745. /* This is our guy if someone answered. */
  746. if (!peer) {
  747. if (option_verbose > 2)
  748. ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
  749. peer = o;
  750. *allowredir_in = o->allowredirect_in;
  751. *allowredir_out = o->allowredirect_out;
  752. *allowdisconnect_in = o->allowdisconnect_out;
  753. *allowdisconnect_out = o->allowdisconnect_out;
  754. }
  755. break;
  756. case AST_CONTROL_BUSY:
  757. if (option_verbose > 2)
  758. ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
  759. o->stillgoing = 0;
  760. if (in->cdr)
  761. ast_cdr_busy(in->cdr);
  762. ast_hangup(o->chan);
  763. o->chan = NULL;
  764. if (qe->parent->strategy)
  765. ring_one(qe, outgoing, &numbusies);
  766. numbusies++;
  767. break;
  768. case AST_CONTROL_CONGESTION:
  769. if (option_verbose > 2)
  770. ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
  771. o->stillgoing = 0;
  772. if (in->cdr)
  773. ast_cdr_busy(in->cdr);
  774. ast_hangup(o->chan);
  775. o->chan = NULL;
  776. if (qe->parent->strategy)
  777. ring_one(qe, outgoing, &numbusies);
  778. numbusies++;
  779. break;
  780. case AST_CONTROL_RINGING:
  781. if (option_verbose > 2)
  782. ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", o->chan->name);
  783. if (!sentringing) {
  784. #if 0
  785. ast_indicate(in, AST_CONTROL_RINGING);
  786. #endif
  787. sentringing++;
  788. }
  789. break;
  790. case AST_CONTROL_OFFHOOK:
  791. /* Ignore going off hook */
  792. break;
  793. default:
  794. ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
  795. }
  796. }
  797. ast_frfree(f);
  798. } else {
  799. o->stillgoing = 0;
  800. ast_hangup(o->chan);
  801. o->chan = NULL;
  802. if (qe->parent->strategy)
  803. ring_one(qe, outgoing, &numbusies);
  804. }
  805. }
  806. o = o->next;
  807. }
  808. if (winner == in) {
  809. f = ast_read(in);
  810. #if 0
  811. if (f && (f->frametype != AST_FRAME_VOICE))
  812. printf("Frame type: %d, %d\n", f->frametype, f->subclass);
  813. else if (!f || (f->frametype != AST_FRAME_VOICE))
  814. printf("Hangup received on %s\n", in->name);
  815. #endif
  816. if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
  817. /* Got hung up */
  818. *to=-1;
  819. if (f)
  820. ast_frfree(f);
  821. return NULL;
  822. }
  823. if ((f->frametype == AST_FRAME_DTMF) && allowdisconnect_out && (f->subclass == '*')) {
  824. if (option_verbose > 3)
  825. ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
  826. *to=0;
  827. ast_frfree(f);
  828. return NULL;
  829. }
  830. if ((f->frametype == AST_FRAME_DTMF) && (f->subclass != '*') && valid_exit(qe, f->subclass)) {
  831. if (option_verbose > 3)
  832. ast_verbose(VERBOSE_PREFIX_3 "User pressed digit: %c\n", f->subclass);
  833. *to=0;
  834. *digit=f->subclass;
  835. ast_frfree(f);
  836. return NULL;
  837. }
  838. ast_frfree(f);
  839. }
  840. if (!*to && (option_verbose > 2))
  841. ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
  842. }
  843. return peer;
  844. }
  845. static int is_our_turn(struct queue_ent *qe)
  846. {
  847. struct queue_ent *ch;
  848. int res;
  849. /* Atomically read the parent head -- does not need a lock */
  850. ch = qe->parent->head;
  851. /* If we are now at the top of the head, break out */
  852. if (ch == qe) {
  853. if (option_debug)
  854. ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
  855. res = 1;
  856. } else {
  857. if (option_debug)
  858. ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
  859. res = 0;
  860. }
  861. return res;
  862. }
  863. static int wait_our_turn(struct queue_ent *qe, int ringing)
  864. {
  865. struct queue_ent *ch;
  866. int res = 0;
  867. time_t now;
  868. /* This is the holding pen for callers 2 through maxlen */
  869. for (;;) {
  870. /* Atomically read the parent head -- does not need a lock */
  871. ch = qe->parent->head;
  872. /* If we are now at the top of the head, break out */
  873. if (ch == qe) {
  874. if (option_debug)
  875. ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
  876. break;
  877. }
  878. /* If we have timed out, break out */
  879. if ( qe->queuetimeout ) {
  880. time(&now);
  881. if ( (now - qe->start) >= qe->queuetimeout )
  882. break;
  883. }
  884. /* leave the queue if no agents, if enabled */
  885. if (!(qe->parent->members) && qe->parent->leavewhenempty) {
  886. leave_queue(qe);
  887. break;
  888. }
  889. /* Make a position announcement, if enabled */
  890. if (qe->parent->announcefrequency && !ringing)
  891. say_position(qe);
  892. /* Wait a second before checking again */
  893. res = ast_waitfordigit(qe->chan, RECHECK * 1000);
  894. if (res)
  895. break;
  896. }
  897. return res;
  898. }
  899. static int update_queue(struct ast_call_queue *q, struct member *member)
  900. {
  901. struct member *cur;
  902. /* Since a reload could have taken place, we have to traverse the list to
  903. be sure it's still valid */
  904. ast_mutex_lock(&q->lock);
  905. cur = q->members;
  906. while(cur) {
  907. if (member == cur) {
  908. time(&cur->lastcall);
  909. cur->calls++;
  910. break;
  911. }
  912. cur = cur->next;
  913. }
  914. q->callscompleted++;
  915. ast_mutex_unlock(&q->lock);
  916. return 0;
  917. }
  918. static int calc_metric(struct ast_call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct localuser *tmp)
  919. {
  920. switch (q->strategy) {
  921. case QUEUE_STRATEGY_RINGALL:
  922. /* Everyone equal, except for penalty */
  923. tmp->metric = mem->penalty * 1000000;
  924. break;
  925. case QUEUE_STRATEGY_ROUNDROBIN:
  926. if (!pos) {
  927. if (!q->wrapped) {
  928. /* No more channels, start over */
  929. q->rrpos = 0;
  930. } else {
  931. /* Prioritize next entry */
  932. q->rrpos++;
  933. }
  934. q->wrapped = 0;
  935. }
  936. /* Fall through */
  937. case QUEUE_STRATEGY_RRMEMORY:
  938. if (pos < q->rrpos) {
  939. tmp->metric = 1000 + pos;
  940. } else {
  941. if (pos > q->rrpos) {
  942. /* Indicate there is another priority */
  943. q->wrapped = 1;
  944. }
  945. tmp->metric = pos;
  946. }
  947. tmp->metric += mem->penalty * 1000000;
  948. break;
  949. case QUEUE_STRATEGY_RANDOM:
  950. tmp->metric = rand() % 1000;
  951. tmp->metric += mem->penalty * 1000000;
  952. break;
  953. case QUEUE_STRATEGY_FEWESTCALLS:
  954. tmp->metric = mem->calls;
  955. tmp->metric += mem->penalty * 1000000;
  956. break;
  957. case QUEUE_STRATEGY_LEASTRECENT:
  958. if (!mem->lastcall)
  959. tmp->metric = 0;
  960. else
  961. tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
  962. tmp->metric += mem->penalty * 1000000;
  963. break;
  964. default:
  965. ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
  966. break;
  967. }
  968. return 0;
  969. }
  970. static int try_calling(struct queue_ent *qe, char *options, char *announceoverride, char *url, int *go_on)
  971. {
  972. struct member *cur;
  973. struct localuser *outgoing=NULL, *tmp = NULL;
  974. int to;
  975. int allowredir_in=0;
  976. int allowredir_out=0;
  977. int allowdisconnect_in=0;
  978. int allowdisconnect_out=0;
  979. char restofit[AST_MAX_EXTENSION];
  980. char oldexten[AST_MAX_EXTENSION]="";
  981. char oldcontext[AST_MAX_EXTENSION]="";
  982. char queuename[256]="";
  983. char *newnum;
  984. char *monitorfilename;
  985. struct ast_channel *peer;
  986. struct localuser *lpeer;
  987. struct member *member;
  988. int res = 0, bridge = 0;
  989. int zapx = 2;
  990. int numbusies = 0;
  991. int x=0;
  992. char *announce = NULL;
  993. char digit = 0;
  994. time_t callstart;
  995. time_t now;
  996. struct ast_bridge_config config;
  997. /* Hold the lock while we setup the outgoing calls */
  998. ast_mutex_lock(&qe->parent->lock);
  999. if (option_debug)
  1000. ast_log(LOG_DEBUG, "%s is trying to call a queue member.\n",
  1001. qe->chan->name);
  1002. strncpy(queuename, qe->parent->name, sizeof(queuename) - 1);
  1003. time(&now);
  1004. cur = qe->parent->members;
  1005. if (!ast_strlen_zero(qe->announce))
  1006. announce = qe->announce;
  1007. if (announceoverride && !ast_strlen_zero(announceoverride))
  1008. announce = announceoverride;
  1009. while(cur) {
  1010. /* Get a technology/[device:]number pair */
  1011. tmp = malloc(sizeof(struct localuser));
  1012. if (!tmp) {
  1013. ast_mutex_unlock(&qe->parent->lock);
  1014. ast_log(LOG_WARNING, "Out of memory\n");
  1015. goto out;
  1016. }
  1017. memset(tmp, 0, sizeof(struct localuser));
  1018. tmp->stillgoing = -1;
  1019. if (options) {
  1020. if (strchr(options, 't'))
  1021. tmp->allowredirect_in = 1;
  1022. if (strchr(options, 'T'))
  1023. tmp->allowredirect_out = 1;
  1024. if (strchr(options, 'r'))
  1025. tmp->ringbackonly = 1;
  1026. if (strchr(options, 'm'))
  1027. tmp->musiconhold = 1;
  1028. if (strchr(options, 'd'))
  1029. tmp->dataquality = 1;
  1030. if (strchr(options, 'h'))
  1031. tmp->allowdisconnect_in = 1;
  1032. if (strchr(options, 'H'))
  1033. tmp->allowdisconnect_out = 1;
  1034. if ((strchr(options, 'n')) && (now - qe->start >= qe->parent->timeout))
  1035. *go_on = 1;
  1036. }
  1037. if (option_debug) {
  1038. if (url)
  1039. ast_log(LOG_DEBUG, "Queue with URL=%s_\n", url);
  1040. else
  1041. ast_log(LOG_DEBUG, "Simple queue (no URL)\n");
  1042. }
  1043. tmp->member = cur; /* Never directly dereference! Could change on reload */
  1044. strncpy(tmp->tech, cur->tech, sizeof(tmp->tech)-1);
  1045. strncpy(tmp->numsubst, cur->loc, sizeof(tmp->numsubst)-1);
  1046. tmp->lastcall = cur->lastcall;
  1047. /* If we're dialing by extension, look at the extension to know what to dial */
  1048. if ((newnum = strstr(tmp->numsubst, "BYEXTENSION"))) {
  1049. strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)-1);
  1050. snprintf(newnum, sizeof(tmp->numsubst) - (newnum - tmp->numsubst), "%s%s", qe->chan->exten,restofit);
  1051. if (option_debug)
  1052. ast_log(LOG_DEBUG, "Dialing by extension %s\n", tmp->numsubst);
  1053. }
  1054. /* Special case: If we ring everyone, go ahead and ring them, otherwise
  1055. just calculate their metric for the appropriate strategy */
  1056. calc_metric(qe->parent, cur, x++, qe, tmp);
  1057. /* Put them in the list of outgoing thingies... We're ready now.
  1058. XXX If we're forcibly removed, these outgoing calls won't get
  1059. hung up XXX */
  1060. tmp->next = outgoing;
  1061. outgoing = tmp;
  1062. /* If this line is up, don't try anybody else */
  1063. if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
  1064. break;
  1065. cur = cur->next;
  1066. }
  1067. if (qe->parent->timeout)
  1068. to = qe->parent->timeout * 1000;
  1069. else
  1070. to = -1;
  1071. ring_one(qe, outgoing, &numbusies);
  1072. ast_mutex_unlock(&qe->parent->lock);
  1073. lpeer = wait_for_answer(qe, outgoing, &to, &allowredir_in, &allowredir_out, &allowdisconnect_in, &allowdisconnect_out, &digit, numbusies);
  1074. ast_mutex_lock(&qe->parent->lock);
  1075. if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY) {
  1076. store_next(qe, outgoing);
  1077. }
  1078. ast_mutex_unlock(&qe->parent->lock);
  1079. if (lpeer)
  1080. peer = lpeer->chan;
  1081. else
  1082. peer = NULL;
  1083. if (!peer) {
  1084. if (to) {
  1085. /* Musta gotten hung up */
  1086. record_abandoned(qe);
  1087. res = -1;
  1088. } else {
  1089. if (digit && valid_exit(qe, digit))
  1090. res=digit;
  1091. else
  1092. /* Nobody answered, next please? */
  1093. res=0;
  1094. }
  1095. if (option_debug)
  1096. ast_log(LOG_DEBUG, "%s: Nobody answered.\n", qe->chan->name);
  1097. goto out;
  1098. }
  1099. if (peer) {
  1100. /* Ah ha! Someone answered within the desired timeframe. Of course after this
  1101. we will always return with -1 so that it is hung up properly after the
  1102. conversation. */
  1103. qe->handled++;
  1104. if (!strcmp(qe->chan->type,"Zap")) {
  1105. if (tmp->dataquality) zapx = 0;
  1106. ast_channel_setoption(qe->chan,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
  1107. }
  1108. if (!strcmp(peer->type,"Zap")) {
  1109. if (tmp->dataquality) zapx = 0;
  1110. ast_channel_setoption(peer,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
  1111. }
  1112. /* Update parameters for the queue */
  1113. recalc_holdtime(qe);
  1114. member = lpeer->member;
  1115. hanguptree(outgoing, peer);
  1116. outgoing = NULL;
  1117. if (announce) {
  1118. int res2;
  1119. res2 = ast_autoservice_start(qe->chan);
  1120. if (!res2) {
  1121. res2 = ast_streamfile(peer, announce, peer->language);
  1122. if (!res2)
  1123. res2 = ast_waitstream(peer, "");
  1124. else {
  1125. ast_log(LOG_WARNING, "Announcement file '%s' is unavailable, continuing anyway...\n", announce);
  1126. res2 = 0;
  1127. }
  1128. }
  1129. res2 |= ast_autoservice_stop(qe->chan);
  1130. if (res2) {
  1131. /* Agent must have hung up */
  1132. ast_log(LOG_WARNING, "Agent on %s hungup on the customer. They're going to be pissed.\n", peer->name);
  1133. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "AGENTDUMP", "%s", "");
  1134. ast_hangup(peer);
  1135. return -1;
  1136. }
  1137. }
  1138. /* Stop music on hold */
  1139. ast_moh_stop(qe->chan);
  1140. /* If appropriate, log that we have a destination channel */
  1141. if (qe->chan->cdr)
  1142. ast_cdr_setdestchan(qe->chan->cdr, peer->name);
  1143. /* Make sure channels are compatible */
  1144. res = ast_channel_make_compatible(qe->chan, peer);
  1145. if (res < 0) {
  1146. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "SYSCOMPAT", "%s", "");
  1147. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
  1148. ast_hangup(peer);
  1149. return -1;
  1150. }
  1151. /* Begin Monitoring */
  1152. if (qe->parent->monfmt && *qe->parent->monfmt) {
  1153. monitorfilename = pbx_builtin_getvar_helper( qe->chan, "MONITOR_FILENAME");
  1154. if(monitorfilename) {
  1155. ast_monitor_start( peer, qe->parent->monfmt, monitorfilename, 1 );
  1156. } else {
  1157. ast_monitor_start( peer, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1 );
  1158. }
  1159. if(qe->parent->monjoin) {
  1160. ast_monitor_setjoinfiles( peer, 1);
  1161. }
  1162. }
  1163. /* Drop out of the queue at this point, to prepare for next caller */
  1164. leave_queue(qe);
  1165. if( url && !ast_strlen_zero(url) && ast_channel_supports_html(peer) ) {
  1166. if (option_debug)
  1167. ast_log(LOG_DEBUG, "app_queue: sendurl=%s.\n", url);
  1168. ast_channel_sendurl( peer, url );
  1169. }
  1170. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "CONNECT", "%ld", (long)time(NULL) - qe->start);
  1171. strncpy(oldcontext, qe->chan->context, sizeof(oldcontext) - 1);
  1172. strncpy(oldexten, qe->chan->exten, sizeof(oldexten) - 1);
  1173. time(&callstart);
  1174. memset(&config,0,sizeof(struct ast_bridge_config));
  1175. config.allowredirect_in = allowredir_in;
  1176. config.allowredirect_out = allowredir_out;
  1177. config.allowdisconnect_in = allowdisconnect_in;
  1178. config.allowdisconnect_out = allowdisconnect_out;
  1179. bridge = ast_bridge_call(qe->chan,peer,&config);
  1180. if (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten)) {
  1181. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "TRANSFER", "%s|%s", qe->chan->exten, qe->chan->context);
  1182. } else if (qe->chan->_softhangup) {
  1183. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETECALLER", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
  1184. } else {
  1185. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETEAGENT", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
  1186. }
  1187. if(bridge != AST_PBX_NO_HANGUP_PEER)
  1188. ast_hangup(peer);
  1189. update_queue(qe->parent, member);
  1190. if( bridge == 0 ) res=1; /* JDG: bridge successfull, leave app_queue */
  1191. else res = bridge; /* bridge error, stay in the queue */
  1192. }
  1193. out:
  1194. hanguptree(outgoing, NULL);
  1195. return res;
  1196. }
  1197. static int wait_a_bit(struct queue_ent *qe)
  1198. {
  1199. /* Don't need to hold the lock while we setup the outgoing calls */
  1200. int retrywait = qe->parent->retry * 1000;
  1201. return ast_waitfordigit(qe->chan, retrywait);
  1202. }
  1203. // [PHM 06/26/03]
  1204. static struct member * interface_exists( struct ast_call_queue * q, char * interface )
  1205. {
  1206. struct member * ret = NULL ;
  1207. struct member *mem;
  1208. char buf[500] ;
  1209. if( q != NULL )
  1210. {
  1211. mem = q->members ;
  1212. while( mem != NULL ) {
  1213. snprintf( buf, sizeof(buf), "%s/%s", mem->tech, mem->loc);
  1214. if( strcmp( buf, interface ) == 0 ) {
  1215. ret = mem ;
  1216. break ;
  1217. }
  1218. else
  1219. mem = mem->next ;
  1220. }
  1221. }
  1222. return( ret ) ;
  1223. }
  1224. static struct member * create_queue_node( char * interface, int penalty )
  1225. {
  1226. struct member * cur ;
  1227. char * tmp ;
  1228. /* Add a new member */
  1229. cur = malloc(sizeof(struct member));
  1230. if (cur) {
  1231. memset(cur, 0, sizeof(struct member));
  1232. cur->penalty = penalty;
  1233. strncpy(cur->tech, interface, sizeof(cur->tech) - 1);
  1234. if ((tmp = strchr(cur->tech, '/')))
  1235. *tmp = '\0';
  1236. if ((tmp = strchr(interface, '/'))) {
  1237. tmp++;
  1238. strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
  1239. } else
  1240. ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
  1241. }
  1242. return( cur ) ;
  1243. }
  1244. static int remove_from_queue(char *queuename, char *interface)
  1245. {
  1246. struct ast_call_queue *q;
  1247. struct member *last_member, *look;
  1248. int res = RES_NOSUCHQUEUE;
  1249. ast_mutex_lock(&qlock);
  1250. for (q = queues ; q ; q = q->next) {
  1251. ast_mutex_lock(&q->lock);
  1252. if (!strcmp(q->name, queuename)) {
  1253. if ((last_member = interface_exists(q, interface))) {
  1254. if ((look = q->members) == last_member) {
  1255. q->members = last_member->next;
  1256. } else {
  1257. while (look != NULL) {
  1258. if (look->next == last_member) {
  1259. look->next = last_member->next;
  1260. break;
  1261. } else {
  1262. look = look->next;
  1263. }
  1264. }
  1265. }
  1266. free(last_member);
  1267. res = RES_OKAY;
  1268. } else {
  1269. res = RES_EXISTS;
  1270. }
  1271. ast_mutex_unlock(&q->lock);
  1272. break;
  1273. }
  1274. ast_mutex_unlock(&q->lock);
  1275. }
  1276. ast_mutex_unlock(&qlock);
  1277. return res;
  1278. }
  1279. static int add_to_queue(char *queuename, char *interface, int penalty)
  1280. {
  1281. struct ast_call_queue *q;
  1282. struct member *new_member;
  1283. int res = RES_NOSUCHQUEUE;
  1284. ast_mutex_lock(&qlock);
  1285. for (q = queues ; q ; q = q->next) {
  1286. ast_mutex_lock(&q->lock);
  1287. if (!strcmp(q->name, queuename)) {
  1288. if (interface_exists(q, interface) == NULL) {
  1289. new_member = create_queue_node(interface, penalty);
  1290. if (new_member != NULL) {
  1291. new_member->dynamic = 1;
  1292. new_member->next = q->members;
  1293. q->members = new_member;
  1294. res = RES_OKAY;
  1295. } else {
  1296. res = RES_OUTOFMEMORY;
  1297. }
  1298. } else {
  1299. res = RES_EXISTS;
  1300. }
  1301. ast_mutex_unlock(&q->lock);
  1302. break;
  1303. }
  1304. ast_mutex_unlock(&q->lock);
  1305. }
  1306. ast_mutex_unlock(&qlock);
  1307. return res;
  1308. }
  1309. static int rqm_exec(struct ast_channel *chan, void *data)
  1310. {
  1311. int res=-1;
  1312. struct localuser *u;
  1313. char *info, *queuename;
  1314. char tmpchan[256]="";
  1315. char *interface = NULL;
  1316. if (!data) {
  1317. ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename[|interface])\n");
  1318. return -1;
  1319. }
  1320. info = ast_strdupa((char *)data);
  1321. if (!info) {
  1322. ast_log(LOG_ERROR, "Out of memory\n");
  1323. return -1;
  1324. }
  1325. LOCAL_USER_ADD(u);
  1326. queuename = info;
  1327. if (queuename) {
  1328. interface = strchr(queuename, '|');
  1329. if (interface) {
  1330. *interface = '\0';
  1331. interface++;
  1332. }
  1333. else {
  1334. strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
  1335. interface = strrchr(tmpchan, '-');
  1336. if (interface)
  1337. *interface = '\0';
  1338. interface = tmpchan;
  1339. }
  1340. }
  1341. switch (remove_from_queue(queuename, interface)) {
  1342. case RES_OKAY:
  1343. ast_log(LOG_NOTICE, "Removed interface '%s' from queue '%s'\n", interface, queuename);
  1344. res = 0;
  1345. break;
  1346. case RES_EXISTS:
  1347. ast_log(LOG_WARNING, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
  1348. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  1349. chan->priority += 100;
  1350. }
  1351. res = 0;
  1352. break;
  1353. case RES_NOSUCHQUEUE:
  1354. ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", queuename);
  1355. res = 0;
  1356. break;
  1357. case RES_OUTOFMEMORY:
  1358. ast_log(LOG_ERROR, "Out of memory\n");
  1359. break;
  1360. }
  1361. LOCAL_USER_REMOVE(u);
  1362. return res;
  1363. }
  1364. static int aqm_exec(struct ast_channel *chan, void *data)
  1365. {
  1366. int res=-1;
  1367. struct localuser *u;
  1368. char *queuename;
  1369. char *info;
  1370. char tmpchan[512]="";
  1371. char *interface=NULL;
  1372. char *penaltys=NULL;
  1373. int penalty = 0;
  1374. if (!data) {
  1375. ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename[|[interface][|penalty]])\n");
  1376. return -1;
  1377. }
  1378. info = ast_strdupa((char *)data);
  1379. if (!info) {
  1380. ast_log(LOG_ERROR, "Out of memory\n");
  1381. return -1;
  1382. }
  1383. LOCAL_USER_ADD(u);
  1384. queuename = info;
  1385. if (queuename) {
  1386. interface = strchr(queuename, '|');
  1387. if (interface) {
  1388. *interface = '\0';
  1389. interface++;
  1390. }
  1391. if (interface) {
  1392. penaltys = strchr(interface, '|');
  1393. if (penaltys) {
  1394. *penaltys = '\0';
  1395. penaltys++;
  1396. }
  1397. }
  1398. if (!interface || ast_strlen_zero(interface)) {
  1399. strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
  1400. interface = strrchr(tmpchan, '-');
  1401. if (interface)
  1402. *interface = '\0';
  1403. interface = tmpchan;
  1404. }
  1405. if (penaltys && strlen(penaltys)) {
  1406. if ((sscanf(penaltys, "%d", &penalty) != 1) || penalty < 0) {
  1407. ast_log(LOG_WARNING, "Penalty '%s' is invalid, must be an integer >= 0\n", penaltys);
  1408. penalty = 0;
  1409. }
  1410. }
  1411. }
  1412. switch (add_to_queue(queuename, interface, penalty)) {
  1413. case RES_OKAY:
  1414. ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", interface, queuename);
  1415. res = 0;
  1416. break;
  1417. case RES_EXISTS:
  1418. ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
  1419. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  1420. chan->priority += 100;
  1421. }
  1422. res = 0;
  1423. break;
  1424. case RES_NOSUCHQUEUE:
  1425. ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", queuename);
  1426. res = 0;
  1427. break;
  1428. case RES_OUTOFMEMORY:
  1429. ast_log(LOG_ERROR, "Out of memory\n");
  1430. break;
  1431. }
  1432. LOCAL_USER_REMOVE(u);
  1433. return res;
  1434. }
  1435. static int queue_exec(struct ast_channel *chan, void *data)
  1436. {
  1437. int res=-1;
  1438. int ringing=0;
  1439. struct localuser *u;
  1440. char *queuename;
  1441. char info[512];
  1442. char *options = NULL;
  1443. char *url = NULL;
  1444. char *announceoverride = NULL;
  1445. char *user_priority;
  1446. int prio;
  1447. char *queuetimeoutstr = NULL;
  1448. /* whether to exit Queue application after the timeout hits */
  1449. int go_on = 0;
  1450. /* Our queue entry */
  1451. struct queue_ent qe;
  1452. if (!data) {
  1453. ast_log(LOG_WARNING, "Queue requires an argument (queuename[|[timeout][|URL]])\n");
  1454. return -1;
  1455. }
  1456. LOCAL_USER_ADD(u);
  1457. /* Setup our queue entry */
  1458. memset(&qe, 0, sizeof(qe));
  1459. /* Parse our arguments XXX Check for failure XXX */
  1460. strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
  1461. queuename = info;
  1462. if (queuename) {
  1463. options = strchr(queuename, '|');
  1464. if (options) {
  1465. *options = '\0';
  1466. options++;
  1467. url = strchr(options, '|');
  1468. if (url) {
  1469. *url = '\0';
  1470. url++;
  1471. announceoverride = strchr(url, '|');
  1472. if (announceoverride) {
  1473. *announceoverride = '\0';
  1474. announceoverride++;
  1475. queuetimeoutstr = strchr(announceoverride, '|');
  1476. if (queuetimeoutstr) {
  1477. *queuetimeoutstr = '\0';
  1478. queuetimeoutstr++;
  1479. qe.queuetimeout = atoi(queuetimeoutstr);
  1480. } else {
  1481. qe.queuetimeout = 0;
  1482. }
  1483. }
  1484. }
  1485. }
  1486. }
  1487. /* Get the priority from the variable ${QUEUE_PRIO} */
  1488. user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
  1489. if (user_priority) {
  1490. if (sscanf(user_priority, "%d", &prio) == 1) {
  1491. if (option_debug)
  1492. ast_log(LOG_DEBUG, "%s: Got priority %d from ${QUEUE_PRIO}.\n",
  1493. chan->name, prio);
  1494. } else {
  1495. ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
  1496. user_priority, chan->name);
  1497. prio = 0;
  1498. }
  1499. } else {
  1500. if (option_debug)
  1501. ast_log(LOG_DEBUG, "NO QUEUE_PRIO variable found. Using default.\n");
  1502. prio = 0;
  1503. }
  1504. if (options) {
  1505. if (strchr(options, 'r')) {
  1506. ringing = 1;
  1507. }
  1508. }
  1509. // if (option_debug)
  1510. ast_log(LOG_DEBUG, "queue: %s, options: %s, url: %s, announce: %s, timeout: %d, priority: %d\n",
  1511. queuename, options, url, announceoverride, qe.queuetimeout, (int)prio);
  1512. qe.chan = chan;
  1513. qe.start = time(NULL);
  1514. qe.prio = (int)prio;
  1515. qe.last_pos_said = 0;
  1516. qe.last_pos = 0;
  1517. if (!join_queue(queuename, &qe)) {
  1518. ast_queue_log(queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", url ? url : "", chan->callerid ? chan->callerid : "");
  1519. /* Start music on hold */
  1520. check_turns:
  1521. if (ringing) {
  1522. ast_indicate(chan, AST_CONTROL_RINGING);
  1523. } else {
  1524. ast_moh_start(chan, qe.moh);
  1525. }
  1526. for (;;) {
  1527. /* This is the wait loop for callers 2 through maxlen */
  1528. res = wait_our_turn(&qe, ringing);
  1529. /* If they hungup, return immediately */
  1530. if (res < 0) {
  1531. /* Record this abandoned call */
  1532. record_abandoned(&qe);
  1533. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1534. if (option_verbose > 2) {
  1535. ast_verbose(VERBOSE_PREFIX_3 "User disconnected while waiting their turn\n");
  1536. res = -1;
  1537. }
  1538. break;
  1539. }
  1540. if (!res)
  1541. break;
  1542. if (valid_exit(&qe, res)) {
  1543. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1544. break;
  1545. }
  1546. }
  1547. if (!res) {
  1548. for (;;) {
  1549. /* This is the wait loop for the head caller*/
  1550. /* To exit, they may get their call answered; */
  1551. /* they may dial a digit from the queue context; */
  1552. /* or, they may timeout. */
  1553. /* Leave if we have exceeded our queuetimeout */
  1554. if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
  1555. res = 0;
  1556. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
  1557. break;
  1558. }
  1559. /* leave the queue if no agents, if enabled */
  1560. if (!((qe.parent)->members) && (qe.parent)->leavewhenempty) {
  1561. leave_queue(&qe);
  1562. break;
  1563. }
  1564. /* Make a position announcement, if enabled */
  1565. if (qe.parent->announcefrequency && !ringing)
  1566. say_position(&qe);
  1567. /* Try calling all queue members for 'timeout' seconds */
  1568. res = try_calling(&qe, options, announceoverride, url, &go_on);
  1569. if (res) {
  1570. if (res < 0) {
  1571. if (!qe.handled)
  1572. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1573. } else if (res > 0)
  1574. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1575. break;
  1576. }
  1577. /* Leave if we have exceeded our queuetimeout */
  1578. if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
  1579. res = 0;
  1580. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
  1581. break;
  1582. }
  1583. /* OK, we didn't get anybody; wait for 'retry' seconds; may get a digit to exit with */
  1584. res = wait_a_bit(&qe);
  1585. if (res < 0) {
  1586. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1587. if (option_verbose > 2) {
  1588. ast_verbose(VERBOSE_PREFIX_3 "User disconnected when they almost made it\n");
  1589. res = -1;
  1590. }
  1591. break;
  1592. }
  1593. if (res && valid_exit(&qe, res)) {
  1594. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1595. break;
  1596. }
  1597. /* exit after 'timeout' cycle if 'n' option enabled */
  1598. if (go_on) {
  1599. if (option_verbose > 2) {
  1600. ast_verbose(VERBOSE_PREFIX_3 "Exiting on time-out cycle\n");
  1601. res = -1;
  1602. }
  1603. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
  1604. res = 0;
  1605. break;
  1606. }
  1607. /* Since this is a priority queue and
  1608. * it is not sure that we are still at the head
  1609. * of the queue, go and check for our turn again.
  1610. */
  1611. if (!is_our_turn(&qe)) {
  1612. ast_log(LOG_DEBUG, "Darn priorities, going back in queue (%s)!\n",
  1613. qe.chan->name);
  1614. goto check_turns;
  1615. }
  1616. }
  1617. }
  1618. /* Don't allow return code > 0 */
  1619. if ((res == 0) || (res > 0 && res != AST_PBX_KEEPALIVE)) {
  1620. res = 0;
  1621. if (ringing) {
  1622. ast_indicate(chan, -1);
  1623. } else {
  1624. ast_moh_stop(chan);
  1625. }
  1626. ast_stopstream(chan);
  1627. }
  1628. leave_queue(&qe);
  1629. } else {
  1630. ast_log(LOG_WARNING, "Unable to join queue '%s'\n", queuename);
  1631. res = 0;
  1632. }
  1633. LOCAL_USER_REMOVE(u);
  1634. return res;
  1635. }
  1636. static void reload_queues(void)
  1637. {
  1638. struct ast_call_queue *q, *ql, *qn;
  1639. struct ast_config *cfg;
  1640. char *cat, *tmp;
  1641. struct ast_variable *var;
  1642. struct member *prev, *cur;
  1643. int new;
  1644. cfg = ast_load("queues.conf");
  1645. if (!cfg) {
  1646. ast_log(LOG_NOTICE, "No call queueing config file, so no call queues\n");
  1647. return;
  1648. }
  1649. ast_mutex_lock(&qlock);
  1650. /* Mark all queues as dead for the moment */
  1651. q = queues;
  1652. while(q) {
  1653. q->dead = 1;
  1654. q = q->next;
  1655. }
  1656. /* Chug through config file */
  1657. cat = ast_category_browse(cfg, NULL);
  1658. while(cat) {
  1659. if (strcasecmp(cat, "general")) {
  1660. /* Look for an existing one */
  1661. q = queues;
  1662. while(q) {
  1663. if (!strcmp(q->name, cat))
  1664. break;
  1665. q = q->next;
  1666. }
  1667. if (!q) {
  1668. /* Make one then */
  1669. q = malloc(sizeof(struct ast_call_queue));
  1670. if (q) {
  1671. /* Initialize it */
  1672. memset(q, 0, sizeof(struct ast_call_queue));
  1673. ast_mutex_init(&q->lock);
  1674. strncpy(q->name, cat, sizeof(q->name) - 1);
  1675. new = 1;
  1676. } else new = 0;
  1677. } else
  1678. new = 0;
  1679. if (q) {
  1680. if (!new)
  1681. ast_mutex_lock(&q->lock);
  1682. /* Re-initialize the queue */
  1683. q->dead = 0;
  1684. q->retry = 0;
  1685. q->timeout = -1;
  1686. q->maxlen = 0;
  1687. q->announcefrequency = 0;
  1688. q->announceholdtime = 0;
  1689. q->roundingseconds = 0; /* Default - don't announce seconds */
  1690. q->holdtime = 0;
  1691. q->callscompleted = 0;
  1692. q->callsabandoned = 0;
  1693. q->callscompletedinsl = 0;
  1694. q->servicelevel = 0;
  1695. q->wrapuptime = 0;
  1696. free_members(q, 0);
  1697. q->moh[0] = '\0';
  1698. q->announce[0] = '\0';
  1699. q->context[0] = '\0';
  1700. q->monfmt[0] = '\0';
  1701. strncpy(q->sound_next, "queue-youarenext", sizeof(q->sound_next) - 1);
  1702. strncpy(q->sound_thereare, "queue-thereare", sizeof(q->sound_thereare) - 1);
  1703. strncpy(q->sound_calls, "queue-callswaiting", sizeof(q->sound_calls) - 1);
  1704. strncpy(q->sound_holdtime, "queue-holdtime", sizeof(q->sound_holdtime) - 1);
  1705. strncpy(q->sound_minutes, "queue-minutes", sizeof(q->sound_minutes) - 1);
  1706. strncpy(q->sound_seconds, "queue-seconds", sizeof(q->sound_seconds) - 1);
  1707. strncpy(q->sound_thanks, "queue-thankyou", sizeof(q->sound_thanks) - 1);
  1708. strncpy(q->sound_lessthan, "queue-less-than", sizeof(q->sound_lessthan) - 1);
  1709. prev = q->members;
  1710. if (prev) {
  1711. /* find the end of any dynamic members */
  1712. while(prev->next)
  1713. prev = prev->next;
  1714. }
  1715. var = ast_variable_browse(cfg, cat);
  1716. while(var) {
  1717. if (!strcasecmp(var->name, "member")) {
  1718. /* Add a new member */
  1719. cur = malloc(sizeof(struct member));
  1720. if (cur) {
  1721. memset(cur, 0, sizeof(struct member));
  1722. strncpy(cur->tech, var->value, sizeof(cur->tech) - 1);
  1723. if ((tmp = strchr(cur->tech, ','))) {
  1724. *tmp = '\0';
  1725. tmp++;
  1726. cur->penalty = atoi(tmp);
  1727. if (cur->penalty < 0)
  1728. cur->penalty = 0;
  1729. }
  1730. if ((tmp = strchr(cur->tech, '/')))
  1731. *tmp = '\0';
  1732. if ((tmp = strchr(var->value, '/'))) {
  1733. tmp++;
  1734. strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
  1735. if ((tmp = strchr(cur->loc, ',')))
  1736. *tmp = '\0';
  1737. } else
  1738. ast_log(LOG_WARNING, "No location at line %d of queue.conf\n", var->lineno);
  1739. if (prev)
  1740. prev->next = cur;
  1741. else
  1742. q->members = cur;
  1743. prev = cur;
  1744. }
  1745. } else if (!strcasecmp(var->name, "music") || !strcasecmp(var->name, "musiconhold")) {
  1746. strncpy(q->moh, var->value, sizeof(q->moh) - 1);
  1747. } else if (!strcasecmp(var->name, "announce")) {
  1748. strncpy(q->announce, var->value, sizeof(q->announce) - 1);
  1749. } else if (!strcasecmp(var->name, "context")) {
  1750. strncpy(q->context, var->value, sizeof(q->context) - 1);
  1751. } else if (!strcasecmp(var->name, "timeout")) {
  1752. q->timeout = atoi(var->value);
  1753. } else if (!strcasecmp(var->name, "monitor-join")) {
  1754. q->monjoin = ast_true(var->value);
  1755. } else if (!strcasecmp(var->name, "monitor-format")) {
  1756. strncpy(q->monfmt, var->value, sizeof(q->monfmt) - 1);
  1757. } else if (!strcasecmp(var->name, "queue-youarenext")) {
  1758. strncpy(q->sound_next, var->value, sizeof(q->sound_next) - 1);
  1759. } else if (!strcasecmp(var->name, "queue-thereare")) {
  1760. strncpy(q->sound_thereare, var->value, sizeof(q->sound_thereare) - 1);
  1761. } else if (!strcasecmp(var->name, "queue-callswaiting")) {
  1762. strncpy(q->sound_calls, var->value, sizeof(q->sound_calls) - 1);
  1763. } else if (!strcasecmp(var->name, "queue-holdtime")) {
  1764. strncpy(q->sound_holdtime, var->value, sizeof(q->sound_holdtime) - 1);
  1765. } else if (!strcasecmp(var->name, "queue-minutes")) {
  1766. strncpy(q->sound_minutes, var->value, sizeof(q->sound_minutes) - 1);
  1767. } else if (!strcasecmp(var->name, "queue-seconds")) {
  1768. strncpy(q->sound_seconds, var->value, sizeof(q->sound_seconds) - 1);
  1769. } else if (!strcasecmp(var->name, "queue-lessthan")) {
  1770. strncpy(q->sound_lessthan, var->value, sizeof(q->sound_lessthan) - 1);
  1771. } else if (!strcasecmp(var->name, "queue-thankyou")) {
  1772. strncpy(q->sound_thanks, var->value, sizeof(q->sound_thanks) - 1);
  1773. } else if (!strcasecmp(var->name, "announce-frequency")) {
  1774. q->announcefrequency = atoi(var->value);
  1775. } else if (!strcasecmp(var->name, "announce-round-seconds")) {
  1776. q->roundingseconds = atoi(var->value);
  1777. if(q->roundingseconds>60 || q->roundingseconds<0) {
  1778. ast_log(LOG_WARNING, "'%s' isn't a valid value for queue-rounding-seconds using 0 instead at line %d of queue.conf\n", var->value, var->lineno);
  1779. q->roundingseconds=0;
  1780. }
  1781. } else if (!strcasecmp(var->name, "announce-holdtime")) {
  1782. q->announceholdtime = (!strcasecmp(var->value,"once")) ? 1 : ast_true(var->value);
  1783. } else if (!strcasecmp(var->name, "retry")) {
  1784. q->retry = atoi(var->value);
  1785. } else if (!strcasecmp(var->name, "wrapuptime")) {
  1786. q->wrapuptime = atoi(var->value);
  1787. } else if (!strcasecmp(var->name, "maxlen")) {
  1788. q->maxlen = atoi(var->value);
  1789. } else if (!strcasecmp(var->name, "servicelevel")) {
  1790. q->servicelevel= atoi(var->value);
  1791. } else if (!strcasecmp(var->name, "strategy")) {
  1792. q->strategy = strat2int(var->value);
  1793. if (q->strategy < 0) {
  1794. ast_log(LOG_WARNING, "'%s' isn't a valid strategy, using ringall instead\n", var->value);
  1795. q->strategy = 0;
  1796. }
  1797. } else if (!strcasecmp(var->name, "joinempty")) {
  1798. q->joinempty = ast_true(var->value);
  1799. } else if (!strcasecmp(var->name, "leavewhenempty")) {
  1800. q->leavewhenempty = ast_true(var->value);
  1801. } else if (!strcasecmp(var->name, "eventwhencalled")) {
  1802. q->eventwhencalled = ast_true(var->value);
  1803. } else {
  1804. ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queue.conf\n", cat, var->name, var->lineno);
  1805. }
  1806. var = var->next;
  1807. }
  1808. if (q->retry < 1)
  1809. q->retry = DEFAULT_RETRY;
  1810. if (q->timeout < 0)
  1811. q->timeout = DEFAULT_TIMEOUT;
  1812. if (q->maxlen < 0)
  1813. q->maxlen = 0;
  1814. if (!new)
  1815. ast_mutex_unlock(&q->lock);
  1816. if (new) {
  1817. q->next = queues;
  1818. queues = q;
  1819. }
  1820. }
  1821. }
  1822. cat = ast_category_browse(cfg, cat);
  1823. }
  1824. ast_destroy(cfg);
  1825. q = queues;
  1826. ql = NULL;
  1827. while(q) {
  1828. qn = q->next;
  1829. if (q->dead) {
  1830. if (ql)
  1831. ql->next = q->next;
  1832. else
  1833. queues = q->next;
  1834. if (!q->count) {
  1835. free(q);
  1836. } else
  1837. ast_log(LOG_WARNING, "XXX Leaking a little memory :( XXX\n");
  1838. } else
  1839. ql = q;
  1840. q = qn;
  1841. }
  1842. ast_mutex_unlock(&qlock);
  1843. }
  1844. static int __queues_show(int fd, int argc, char **argv, int queue_show)
  1845. {
  1846. struct ast_call_queue *q;
  1847. struct queue_ent *qe;
  1848. struct member *mem;
  1849. int pos;
  1850. time_t now;
  1851. char max[80] = "";
  1852. char calls[80] = "";
  1853. float sl = 0;
  1854. time(&now);
  1855. if ((!queue_show && argc != 2) || (queue_show && argc != 3))
  1856. return RESULT_SHOWUSAGE;
  1857. ast_mutex_lock(&qlock);
  1858. q = queues;
  1859. if (!q) {
  1860. ast_mutex_unlock(&qlock);
  1861. if (queue_show)
  1862. ast_cli(fd, "No such queue: %s.\n",argv[2]);
  1863. else
  1864. ast_cli(fd, "No queues.\n");
  1865. return RESULT_SUCCESS;
  1866. }
  1867. while(q) {
  1868. ast_mutex_lock(&q->lock);
  1869. if (queue_show) {
  1870. if (strcasecmp(q->name, argv[2]) != 0) {
  1871. ast_mutex_unlock(&q->lock);
  1872. q = q->next;
  1873. if (!q) {
  1874. ast_cli(fd, "No such queue: %s.\n",argv[2]);
  1875. break;
  1876. }
  1877. continue;
  1878. }
  1879. }
  1880. if (q->maxlen)
  1881. snprintf(max, sizeof(max), "%d", q->maxlen);
  1882. else
  1883. strncpy(max, "unlimited", sizeof(max) - 1);
  1884. sl = 0;
  1885. if(q->callscompleted > 0)
  1886. sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
  1887. ast_cli(fd, "%-12.12s has %d calls (max %s) in '%s' strategy (%ds holdtime), C:%d, A:%d, SL:%2.1f%% within %ds\n",
  1888. q->name, q->count, max, int2strat(q->strategy), q->holdtime, q->callscompleted, q->callsabandoned,sl,q->servicelevel);
  1889. if (q->members) {
  1890. ast_cli(fd, " Members: \n");
  1891. for (mem = q->members; mem; mem = mem->next) {
  1892. if (mem->penalty)
  1893. snprintf(max, sizeof(max) - 20, " with penalty %d", mem->penalty);
  1894. else
  1895. max[0] = '\0';
  1896. if (mem->dynamic)
  1897. strncat(max, " (dynamic)", sizeof(max) - strlen(max) - 1);
  1898. if (mem->calls) {
  1899. snprintf(calls, sizeof(calls), " has taken %d calls (last was %ld secs ago)",
  1900. mem->calls, (long)(time(NULL) - mem->lastcall));
  1901. } else
  1902. strncpy(calls, " has taken no calls yet", sizeof(calls) - 1);
  1903. ast_cli(fd, " %s/%s%s%s\n", mem->tech, mem->loc, max, calls);
  1904. }
  1905. } else
  1906. ast_cli(fd, " No Members\n");
  1907. if (q->head) {
  1908. pos = 1;
  1909. ast_cli(fd, " Callers: \n");
  1910. for (qe = q->head; qe; qe = qe->next)
  1911. ast_cli(fd, " %d. %s (wait: %ld:%2.2ld, prio: %d)\n", pos++, qe->chan->name,
  1912. (long)(now - qe->start) / 60, (long)(now - qe->start) % 60, qe->prio);
  1913. } else
  1914. ast_cli(fd, " No Callers\n");
  1915. ast_cli(fd, "\n");
  1916. ast_mutex_unlock(&q->lock);
  1917. q = q->next;
  1918. if (queue_show)
  1919. break;
  1920. }
  1921. ast_mutex_unlock(&qlock);
  1922. return RESULT_SUCCESS;
  1923. }
  1924. static int queues_show(int fd, int argc, char **argv)
  1925. {
  1926. return __queues_show(fd, argc, argv, 0);
  1927. }
  1928. static int queue_show(int fd, int argc, char **argv)
  1929. {
  1930. return __queues_show(fd, argc, argv, 1);
  1931. }
  1932. static char *complete_queue(char *line, char *word, int pos, int state)
  1933. {
  1934. struct ast_call_queue *q;
  1935. int which=0;
  1936. ast_mutex_lock(&qlock);
  1937. for (q = queues; q; q = q->next) {
  1938. if (!strncasecmp(word, q->name, strlen(word))) {
  1939. if (++which > state)
  1940. break;
  1941. }
  1942. }
  1943. ast_mutex_unlock(&qlock);
  1944. return q ? strdup(q->name) : NULL;
  1945. }
  1946. /* JDG: callback to display queues status in manager */
  1947. static int manager_queues_show( struct mansession *s, struct message *m )
  1948. {
  1949. char *a[] = { "show", "queues" };
  1950. return queues_show( s->fd, 2, a );
  1951. } /* /JDG */
  1952. /* Dump queue status */
  1953. static int manager_queues_status( struct mansession *s, struct message *m )
  1954. {
  1955. time_t now;
  1956. int pos;
  1957. char *id = astman_get_header(m,"ActionID");
  1958. char idText[256] = "";
  1959. struct ast_call_queue *q;
  1960. struct queue_ent *qe;
  1961. float sl = 0;
  1962. struct member *mem;
  1963. astman_send_ack(s, m, "Queue status will follow");
  1964. time(&now);
  1965. ast_mutex_lock(&qlock);
  1966. if (!ast_strlen_zero(id)) {
  1967. snprintf(idText,256,"ActionID: %s\r\n",id);
  1968. }
  1969. for (q = queues; q; q = q->next) {
  1970. ast_mutex_lock(&q->lock);
  1971. /* List queue properties */
  1972. if(q->callscompleted > 0)
  1973. sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
  1974. ast_mutex_lock(&s->lock);
  1975. ast_cli(s->fd, "Event: QueueParams\r\n"
  1976. "Queue: %s\r\n"
  1977. "Max: %d\r\n"
  1978. "Calls: %d\r\n"
  1979. "Holdtime: %d\r\n"
  1980. "Completed: %d\r\n"
  1981. "Abandoned: %d\r\n"
  1982. "ServiceLevel: %d\r\n"
  1983. "ServicelevelPerf: %2.1f\r\n"
  1984. "%s"
  1985. "\r\n",
  1986. q->name, q->maxlen, q->count, q->holdtime, q->callscompleted,
  1987. q->callsabandoned, q->servicelevel, sl, idText);
  1988. /* List Queue Members */
  1989. for (mem = q->members; mem; mem = mem->next)
  1990. ast_cli(s->fd, "Event: QueueMember\r\n"
  1991. "Queue: %s\r\n"
  1992. "Location: %s/%s\r\n"
  1993. "Membership: %s\r\n"
  1994. "Penalty: %d\r\n"
  1995. "CallsTaken: %d\r\n"
  1996. "LastCall: %ld\r\n"
  1997. "%s"
  1998. "\r\n",
  1999. q->name, mem->tech, mem->loc, mem->dynamic ? "dynamic" : "static",
  2000. mem->penalty, mem->calls, mem->lastcall, idText);
  2001. /* List Queue Entries */
  2002. pos = 1;
  2003. for (qe = q->head; qe; qe = qe->next)
  2004. ast_cli(s->fd, "Event: QueueEntry\r\n"
  2005. "Queue: %s\r\n"
  2006. "Position: %d\r\n"
  2007. "Channel: %s\r\n"
  2008. "CallerID: %s\r\n"
  2009. "Wait: %ld\r\n"
  2010. "%s"
  2011. "\r\n",
  2012. q->name, pos++, qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : ""), (long)(now - qe->start), idText);
  2013. ast_mutex_unlock(&s->lock);
  2014. ast_mutex_unlock(&q->lock);
  2015. }
  2016. ast_mutex_unlock(&qlock);
  2017. return RESULT_SUCCESS;
  2018. }
  2019. static int manager_add_queue_member(struct mansession *s, struct message *m)
  2020. {
  2021. char *queuename, *interface, *penalty_s;
  2022. int penalty = 0;
  2023. queuename = astman_get_header(m, "Queue");
  2024. interface = astman_get_header(m, "Interface");
  2025. penalty_s = astman_get_header(m, "Penalty");
  2026. if (ast_strlen_zero(queuename)) {
  2027. astman_send_error(s, m, "'Queue' not specified.");
  2028. return 0;
  2029. }
  2030. if (ast_strlen_zero(interface)) {
  2031. astman_send_error(s, m, "'Interface' not specified.");
  2032. return 0;
  2033. }
  2034. if (ast_strlen_zero(penalty_s))
  2035. penalty = 0;
  2036. else if (sscanf(penalty_s, "%d", &penalty) != 1) {
  2037. penalty = 0;
  2038. }
  2039. switch (add_to_queue(queuename, interface, penalty)) {
  2040. case RES_OKAY:
  2041. astman_send_ack(s, m, "Added interface to queue");
  2042. break;
  2043. case RES_EXISTS:
  2044. astman_send_error(s, m, "Unable to add interface: Already there");
  2045. break;
  2046. case RES_NOSUCHQUEUE:
  2047. astman_send_error(s, m, "Unable to add interface to queue: No such queue");
  2048. break;
  2049. case RES_OUTOFMEMORY:
  2050. astman_send_error(s, m, "Out of memory");
  2051. break;
  2052. }
  2053. return 0;
  2054. }
  2055. static int manager_remove_queue_member(struct mansession *s, struct message *m)
  2056. {
  2057. char *queuename, *interface;
  2058. queuename = astman_get_header(m, "Queue");
  2059. interface = astman_get_header(m, "Interface");
  2060. if (ast_strlen_zero(queuename) || ast_strlen_zero(interface)) {
  2061. astman_send_error(s, m, "Need 'Queue' and 'Interface' parameters.");
  2062. return 0;
  2063. }
  2064. switch (remove_from_queue(queuename, interface)) {
  2065. case RES_OKAY:
  2066. astman_send_ack(s, m, "Removed interface from queue");
  2067. break;
  2068. case RES_EXISTS:
  2069. astman_send_error(s, m, "Unable to remove interface: Not there");
  2070. break;
  2071. case RES_NOSUCHQUEUE:
  2072. astman_send_error(s, m, "Unable to remove interface from queue: No such queue");
  2073. break;
  2074. case RES_OUTOFMEMORY:
  2075. astman_send_error(s, m, "Out of memory");
  2076. break;
  2077. }
  2078. return 0;
  2079. }
  2080. static int handle_add_queue_member(int fd, int argc, char *argv[])
  2081. {
  2082. char *queuename, *interface;
  2083. int penalty;
  2084. if ((argc != 6) && (argc != 8)) {
  2085. return RESULT_SHOWUSAGE;
  2086. } else if (strcmp(argv[4], "to")) {
  2087. return RESULT_SHOWUSAGE;
  2088. } else if ((argc == 8) && strcmp(argv[6], "penalty")) {
  2089. return RESULT_SHOWUSAGE;
  2090. }
  2091. queuename = argv[5];
  2092. interface = argv[3];
  2093. if (argc == 8) {
  2094. if (sscanf(argv[7], "%d", &penalty) == 1) {
  2095. if (penalty < 0) {
  2096. ast_cli(fd, "Penalty must be >= 0\n");
  2097. penalty = 0;
  2098. }
  2099. } else {
  2100. ast_cli(fd, "Penalty must be an integer >= 0\n");
  2101. penalty = 0;
  2102. }
  2103. } else {
  2104. penalty = 0;
  2105. }
  2106. switch (add_to_queue(queuename, interface, penalty)) {
  2107. case RES_OKAY:
  2108. ast_cli(fd, "Added interface '%s' to queue '%s'\n", interface, queuename);
  2109. return RESULT_SUCCESS;
  2110. case RES_EXISTS:
  2111. ast_cli(fd, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
  2112. return RESULT_FAILURE;
  2113. case RES_NOSUCHQUEUE:
  2114. ast_cli(fd, "Unable to add interface to queue '%s': No such queue\n", queuename);
  2115. return RESULT_FAILURE;
  2116. case RES_OUTOFMEMORY:
  2117. ast_cli(fd, "Out of memory\n");
  2118. return RESULT_FAILURE;
  2119. default:
  2120. return RESULT_FAILURE;
  2121. }
  2122. }
  2123. static char *complete_add_queue_member(char *line, char *word, int pos, int state)
  2124. {
  2125. /* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue>; 6 - penalty; 7 - <penalty> */
  2126. switch (pos) {
  2127. case 3:
  2128. /* Don't attempt to complete name of member (infinite possibilities) */
  2129. return NULL;
  2130. case 4:
  2131. if (state == 0) {
  2132. return strdup("to");
  2133. } else {
  2134. return NULL;
  2135. }
  2136. case 5:
  2137. /* No need to duplicate code */
  2138. return complete_queue(line, word, pos, state);
  2139. case 6:
  2140. if (state == 0) {
  2141. return strdup("penalty");
  2142. } else {
  2143. return NULL;
  2144. }
  2145. case 7:
  2146. if (state < 100) { /* 0-99 */
  2147. char *num = malloc(3);
  2148. if (num) {
  2149. sprintf(num, "%d", state);
  2150. }
  2151. return num;
  2152. } else {
  2153. return NULL;
  2154. }
  2155. default:
  2156. return NULL;
  2157. }
  2158. }
  2159. static int handle_remove_queue_member(int fd, int argc, char *argv[])
  2160. {
  2161. char *queuename, *interface;
  2162. if (argc != 6) {
  2163. return RESULT_SHOWUSAGE;
  2164. } else if (strcmp(argv[4], "from")) {
  2165. return RESULT_SHOWUSAGE;
  2166. }
  2167. queuename = argv[5];
  2168. interface = argv[3];
  2169. switch (remove_from_queue(queuename, interface)) {
  2170. case RES_OKAY:
  2171. ast_cli(fd, "Removed interface '%s' from queue '%s'\n", interface, queuename);
  2172. return RESULT_SUCCESS;
  2173. case RES_EXISTS:
  2174. ast_cli(fd, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
  2175. return RESULT_FAILURE;
  2176. case RES_NOSUCHQUEUE:
  2177. ast_cli(fd, "Unable to remove interface from queue '%s': No such queue\n", queuename);
  2178. return RESULT_FAILURE;
  2179. case RES_OUTOFMEMORY:
  2180. ast_cli(fd, "Out of memory\n");
  2181. return RESULT_FAILURE;
  2182. default:
  2183. return RESULT_FAILURE;
  2184. }
  2185. }
  2186. static char *complete_remove_queue_member(char *line, char *word, int pos, int state)
  2187. {
  2188. int which = 0;
  2189. struct ast_call_queue *q;
  2190. struct member *m;
  2191. /* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue> */
  2192. if ((pos > 5) || (pos < 3)) {
  2193. return NULL;
  2194. }
  2195. if (pos == 4) {
  2196. if (state == 0) {
  2197. return strdup("from");
  2198. } else {
  2199. return NULL;
  2200. }
  2201. }
  2202. if (pos == 5) {
  2203. /* No need to duplicate code */
  2204. return complete_queue(line, word, pos, state);
  2205. }
  2206. if (queues != NULL) {
  2207. for (q = queues ; q ; q = q->next) {
  2208. ast_mutex_lock(&q->lock);
  2209. for (m = q->members ; m ; m = m->next) {
  2210. if (++which > state) {
  2211. char *tmp = malloc(strlen(m->tech) + strlen(m->loc) + 2);
  2212. if (tmp) {
  2213. sprintf(tmp, "%s/%s", m->tech, m->loc);
  2214. } else {
  2215. ast_log(LOG_ERROR, "Out of memory\n");
  2216. }
  2217. ast_mutex_unlock(&q->lock);
  2218. return tmp;
  2219. }
  2220. }
  2221. ast_mutex_unlock(&q->lock);
  2222. }
  2223. }
  2224. return NULL;
  2225. }
  2226. static char show_queues_usage[] =
  2227. "Usage: show queues\n"
  2228. " Provides summary information on call queues.\n";
  2229. static struct ast_cli_entry cli_show_queues = {
  2230. { "show", "queues", NULL }, queues_show,
  2231. "Show status of queues", show_queues_usage, NULL };
  2232. static char show_queue_usage[] =
  2233. "Usage: show queue\n"
  2234. " Provides summary information on a specified queue.\n";
  2235. static struct ast_cli_entry cli_show_queue = {
  2236. { "show", "queue", NULL }, queue_show,
  2237. "Show status of a specified queue", show_queue_usage, complete_queue };
  2238. static char aqm_cmd_usage[] =
  2239. "Usage: add queue member <channel> to <queue> [penalty <penalty>]\n";
  2240. static struct ast_cli_entry cli_add_queue_member = {
  2241. { "add", "queue", "member", NULL }, handle_add_queue_member,
  2242. "Add a channel to a specified queue", aqm_cmd_usage, complete_add_queue_member };
  2243. static char rqm_cmd_usage[] =
  2244. "Usage: remove queue member <channel> from <queue>\n";
  2245. static struct ast_cli_entry cli_remove_queue_member = {
  2246. { "remove", "queue", "member", NULL }, handle_remove_queue_member,
  2247. "Removes a channel from a specified queue", rqm_cmd_usage, complete_remove_queue_member };
  2248. int unload_module(void)
  2249. {
  2250. STANDARD_HANGUP_LOCALUSERS;
  2251. ast_cli_unregister(&cli_show_queue);
  2252. ast_cli_unregister(&cli_show_queues);
  2253. ast_cli_unregister(&cli_add_queue_member);
  2254. ast_cli_unregister(&cli_remove_queue_member);
  2255. ast_manager_unregister("Queues");
  2256. ast_manager_unregister("QueueStatus");
  2257. ast_manager_unregister("QueueAdd");
  2258. ast_manager_unregister("QueueRemove");
  2259. ast_unregister_application(app_aqm);
  2260. ast_unregister_application(app_rqm);
  2261. return ast_unregister_application(app);
  2262. }
  2263. int load_module(void)
  2264. {
  2265. int res;
  2266. res = ast_register_application(app, queue_exec, synopsis, descrip);
  2267. if (!res) {
  2268. ast_cli_register(&cli_show_queue);
  2269. ast_cli_register(&cli_show_queues);
  2270. ast_cli_register(&cli_add_queue_member);
  2271. ast_cli_register(&cli_remove_queue_member);
  2272. ast_manager_register( "Queues", 0, manager_queues_show, "Queues" );
  2273. ast_manager_register( "QueueStatus", 0, manager_queues_status, "Queue Status" );
  2274. ast_manager_register( "QueueAdd", EVENT_FLAG_AGENT, manager_add_queue_member, "Add interface to queue." );
  2275. ast_manager_register( "QueueRemove", EVENT_FLAG_AGENT, manager_remove_queue_member, "Remove interface from queue." );
  2276. ast_register_application(app_aqm, aqm_exec, app_aqm_synopsis, app_aqm_descrip) ;
  2277. ast_register_application(app_rqm, rqm_exec, app_rqm_synopsis, app_rqm_descrip) ;
  2278. }
  2279. reload_queues();
  2280. return res;
  2281. }
  2282. int reload(void)
  2283. {
  2284. reload_queues();
  2285. return 0;
  2286. }
  2287. char *description(void)
  2288. {
  2289. return tdesc;
  2290. }
  2291. int usecount(void)
  2292. {
  2293. int res;
  2294. STANDARD_USECOUNT(res);
  2295. return res;
  2296. }
  2297. char *key()
  2298. {
  2299. return ASTERISK_GPL_KEY;
  2300. }