app_queue.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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. if (f)
  828. ast_frfree(f);
  829. return NULL;
  830. }
  831. if ((f->frametype == AST_FRAME_DTMF) && (f->subclass != '*') && valid_exit(qe, f->subclass)) {
  832. if (option_verbose > 3)
  833. ast_verbose(VERBOSE_PREFIX_3 "User pressed digit: %c\n", f->subclass);
  834. *to=0;
  835. *digit=f->subclass;
  836. if (f)
  837. ast_frfree(f);
  838. return NULL;
  839. }
  840. if (f)
  841. ast_frfree(f);
  842. }
  843. if (!*to && (option_verbose > 2))
  844. ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
  845. }
  846. return peer;
  847. }
  848. static int is_our_turn(struct queue_ent *qe)
  849. {
  850. struct queue_ent *ch;
  851. int res;
  852. /* Atomically read the parent head -- does not need a lock */
  853. ch = qe->parent->head;
  854. /* If we are now at the top of the head, break out */
  855. if (ch == qe) {
  856. if (option_debug)
  857. ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
  858. res = 1;
  859. } else {
  860. if (option_debug)
  861. ast_log(LOG_DEBUG, "It's not our turn (%s).\n", qe->chan->name);
  862. res = 0;
  863. }
  864. return res;
  865. }
  866. static int wait_our_turn(struct queue_ent *qe, int ringing)
  867. {
  868. struct queue_ent *ch;
  869. int res = 0;
  870. time_t now;
  871. /* This is the holding pen for callers 2 through maxlen */
  872. for (;;) {
  873. /* Atomically read the parent head -- does not need a lock */
  874. ch = qe->parent->head;
  875. /* If we are now at the top of the head, break out */
  876. if (ch == qe) {
  877. if (option_debug)
  878. ast_log(LOG_DEBUG, "It's our turn (%s).\n", qe->chan->name);
  879. break;
  880. }
  881. /* If we have timed out, break out */
  882. if ( qe->queuetimeout ) {
  883. time(&now);
  884. if ( (now - qe->start) >= qe->queuetimeout )
  885. break;
  886. }
  887. /* leave the queue if no agents, if enabled */
  888. if (!(qe->parent->members) && qe->parent->leavewhenempty) {
  889. leave_queue(qe);
  890. break;
  891. }
  892. /* Make a position announcement, if enabled */
  893. if (qe->parent->announcefrequency && !ringing)
  894. say_position(qe);
  895. /* Wait a second before checking again */
  896. res = ast_waitfordigit(qe->chan, RECHECK * 1000);
  897. if (res)
  898. break;
  899. }
  900. return res;
  901. }
  902. static int update_queue(struct ast_call_queue *q, struct member *member)
  903. {
  904. struct member *cur;
  905. /* Since a reload could have taken place, we have to traverse the list to
  906. be sure it's still valid */
  907. ast_mutex_lock(&q->lock);
  908. cur = q->members;
  909. while(cur) {
  910. if (member == cur) {
  911. time(&cur->lastcall);
  912. cur->calls++;
  913. break;
  914. }
  915. cur = cur->next;
  916. }
  917. q->callscompleted++;
  918. ast_mutex_unlock(&q->lock);
  919. return 0;
  920. }
  921. static int calc_metric(struct ast_call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct localuser *tmp)
  922. {
  923. switch (q->strategy) {
  924. case QUEUE_STRATEGY_RINGALL:
  925. /* Everyone equal, except for penalty */
  926. tmp->metric = mem->penalty * 1000000;
  927. break;
  928. case QUEUE_STRATEGY_ROUNDROBIN:
  929. if (!pos) {
  930. if (!q->wrapped) {
  931. /* No more channels, start over */
  932. q->rrpos = 0;
  933. } else {
  934. /* Prioritize next entry */
  935. q->rrpos++;
  936. }
  937. q->wrapped = 0;
  938. }
  939. /* Fall through */
  940. case QUEUE_STRATEGY_RRMEMORY:
  941. if (pos < q->rrpos) {
  942. tmp->metric = 1000 + pos;
  943. } else {
  944. if (pos > q->rrpos) {
  945. /* Indicate there is another priority */
  946. q->wrapped = 1;
  947. }
  948. tmp->metric = pos;
  949. }
  950. tmp->metric += mem->penalty * 1000000;
  951. break;
  952. case QUEUE_STRATEGY_RANDOM:
  953. tmp->metric = rand() % 1000;
  954. tmp->metric += mem->penalty * 1000000;
  955. break;
  956. case QUEUE_STRATEGY_FEWESTCALLS:
  957. tmp->metric = mem->calls;
  958. tmp->metric += mem->penalty * 1000000;
  959. break;
  960. case QUEUE_STRATEGY_LEASTRECENT:
  961. if (!mem->lastcall)
  962. tmp->metric = 0;
  963. else
  964. tmp->metric = 1000000 - (time(NULL) - mem->lastcall);
  965. tmp->metric += mem->penalty * 1000000;
  966. break;
  967. default:
  968. ast_log(LOG_WARNING, "Can't calculate metric for unknown strategy %d\n", q->strategy);
  969. break;
  970. }
  971. return 0;
  972. }
  973. static int try_calling(struct queue_ent *qe, char *options, char *announceoverride, char *url, int *go_on)
  974. {
  975. struct member *cur;
  976. struct localuser *outgoing=NULL, *tmp = NULL;
  977. int to;
  978. int allowredir_in=0;
  979. int allowredir_out=0;
  980. int allowdisconnect_in=0;
  981. int allowdisconnect_out=0;
  982. char restofit[AST_MAX_EXTENSION];
  983. char oldexten[AST_MAX_EXTENSION]="";
  984. char oldcontext[AST_MAX_EXTENSION]="";
  985. char queuename[256]="";
  986. char *newnum;
  987. char *monitorfilename;
  988. struct ast_channel *peer;
  989. struct localuser *lpeer;
  990. struct member *member;
  991. int res = 0, bridge = 0;
  992. int zapx = 2;
  993. int numbusies = 0;
  994. int x=0;
  995. char *announce = NULL;
  996. char digit = 0;
  997. time_t callstart;
  998. time_t now;
  999. struct ast_bridge_config config;
  1000. /* Hold the lock while we setup the outgoing calls */
  1001. ast_mutex_lock(&qe->parent->lock);
  1002. if (option_debug)
  1003. ast_log(LOG_DEBUG, "%s is trying to call a queue member.\n",
  1004. qe->chan->name);
  1005. strncpy(queuename, qe->parent->name, sizeof(queuename) - 1);
  1006. time(&now);
  1007. cur = qe->parent->members;
  1008. if (!ast_strlen_zero(qe->announce))
  1009. announce = qe->announce;
  1010. if (announceoverride && !ast_strlen_zero(announceoverride))
  1011. announce = announceoverride;
  1012. while(cur) {
  1013. /* Get a technology/[device:]number pair */
  1014. tmp = malloc(sizeof(struct localuser));
  1015. if (!tmp) {
  1016. ast_mutex_unlock(&qe->parent->lock);
  1017. ast_log(LOG_WARNING, "Out of memory\n");
  1018. goto out;
  1019. }
  1020. memset(tmp, 0, sizeof(struct localuser));
  1021. tmp->stillgoing = -1;
  1022. if (options) {
  1023. if (strchr(options, 't'))
  1024. tmp->allowredirect_in = 1;
  1025. if (strchr(options, 'T'))
  1026. tmp->allowredirect_out = 1;
  1027. if (strchr(options, 'r'))
  1028. tmp->ringbackonly = 1;
  1029. if (strchr(options, 'm'))
  1030. tmp->musiconhold = 1;
  1031. if (strchr(options, 'd'))
  1032. tmp->dataquality = 1;
  1033. if (strchr(options, 'h'))
  1034. tmp->allowdisconnect_in = 1;
  1035. if (strchr(options, 'H'))
  1036. tmp->allowdisconnect_out = 1;
  1037. if ((strchr(options, 'n')) && (now - qe->start >= qe->parent->timeout))
  1038. *go_on = 1;
  1039. }
  1040. if (option_debug) {
  1041. if (url)
  1042. ast_log(LOG_DEBUG, "Queue with URL=%s_\n", url);
  1043. else
  1044. ast_log(LOG_DEBUG, "Simple queue (no URL)\n");
  1045. }
  1046. tmp->member = cur; /* Never directly dereference! Could change on reload */
  1047. strncpy(tmp->tech, cur->tech, sizeof(tmp->tech)-1);
  1048. strncpy(tmp->numsubst, cur->loc, sizeof(tmp->numsubst)-1);
  1049. tmp->lastcall = cur->lastcall;
  1050. /* If we're dialing by extension, look at the extension to know what to dial */
  1051. if ((newnum = strstr(tmp->numsubst, "BYEXTENSION"))) {
  1052. strncpy(restofit, newnum + strlen("BYEXTENSION"), sizeof(restofit)-1);
  1053. snprintf(newnum, sizeof(tmp->numsubst) - (newnum - tmp->numsubst), "%s%s", qe->chan->exten,restofit);
  1054. if (option_debug)
  1055. ast_log(LOG_DEBUG, "Dialing by extension %s\n", tmp->numsubst);
  1056. }
  1057. /* Special case: If we ring everyone, go ahead and ring them, otherwise
  1058. just calculate their metric for the appropriate strategy */
  1059. calc_metric(qe->parent, cur, x++, qe, tmp);
  1060. /* Put them in the list of outgoing thingies... We're ready now.
  1061. XXX If we're forcibly removed, these outgoing calls won't get
  1062. hung up XXX */
  1063. tmp->next = outgoing;
  1064. outgoing = tmp;
  1065. /* If this line is up, don't try anybody else */
  1066. if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
  1067. break;
  1068. cur = cur->next;
  1069. }
  1070. if (qe->parent->timeout)
  1071. to = qe->parent->timeout * 1000;
  1072. else
  1073. to = -1;
  1074. ring_one(qe, outgoing, &numbusies);
  1075. ast_mutex_unlock(&qe->parent->lock);
  1076. lpeer = wait_for_answer(qe, outgoing, &to, &allowredir_in, &allowredir_out, &allowdisconnect_in, &allowdisconnect_out, &digit, numbusies);
  1077. ast_mutex_lock(&qe->parent->lock);
  1078. if (qe->parent->strategy == QUEUE_STRATEGY_RRMEMORY) {
  1079. store_next(qe, outgoing);
  1080. }
  1081. ast_mutex_unlock(&qe->parent->lock);
  1082. if (lpeer)
  1083. peer = lpeer->chan;
  1084. else
  1085. peer = NULL;
  1086. if (!peer) {
  1087. if (to) {
  1088. /* Musta gotten hung up */
  1089. record_abandoned(qe);
  1090. res = -1;
  1091. } else {
  1092. if (digit && valid_exit(qe, digit))
  1093. res=digit;
  1094. else
  1095. /* Nobody answered, next please? */
  1096. res=0;
  1097. }
  1098. if (option_debug)
  1099. ast_log(LOG_DEBUG, "%s: Nobody answered.\n", qe->chan->name);
  1100. goto out;
  1101. }
  1102. if (peer) {
  1103. /* Ah ha! Someone answered within the desired timeframe. Of course after this
  1104. we will always return with -1 so that it is hung up properly after the
  1105. conversation. */
  1106. qe->handled++;
  1107. if (!strcmp(qe->chan->type,"Zap")) {
  1108. if (tmp->dataquality) zapx = 0;
  1109. ast_channel_setoption(qe->chan,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
  1110. }
  1111. if (!strcmp(peer->type,"Zap")) {
  1112. if (tmp->dataquality) zapx = 0;
  1113. ast_channel_setoption(peer,AST_OPTION_TONE_VERIFY,&zapx,sizeof(char),0);
  1114. }
  1115. /* Update parameters for the queue */
  1116. recalc_holdtime(qe);
  1117. member = lpeer->member;
  1118. hanguptree(outgoing, peer);
  1119. outgoing = NULL;
  1120. if (announce) {
  1121. int res2;
  1122. res2 = ast_autoservice_start(qe->chan);
  1123. if (!res2) {
  1124. res2 = ast_streamfile(peer, announce, peer->language);
  1125. if (!res2)
  1126. res2 = ast_waitstream(peer, "");
  1127. else {
  1128. ast_log(LOG_WARNING, "Announcement file '%s' is unavailable, continuing anyway...\n", announce);
  1129. res2 = 0;
  1130. }
  1131. }
  1132. res2 |= ast_autoservice_stop(qe->chan);
  1133. if (res2) {
  1134. /* Agent must have hung up */
  1135. ast_log(LOG_WARNING, "Agent on %s hungup on the customer. They're going to be pissed.\n", peer->name);
  1136. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "AGENTDUMP", "%s", "");
  1137. ast_hangup(peer);
  1138. return -1;
  1139. }
  1140. }
  1141. /* Stop music on hold */
  1142. ast_moh_stop(qe->chan);
  1143. /* If appropriate, log that we have a destination channel */
  1144. if (qe->chan->cdr)
  1145. ast_cdr_setdestchan(qe->chan->cdr, peer->name);
  1146. /* Make sure channels are compatible */
  1147. res = ast_channel_make_compatible(qe->chan, peer);
  1148. if (res < 0) {
  1149. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "SYSCOMPAT", "%s", "");
  1150. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", qe->chan->name, peer->name);
  1151. ast_hangup(peer);
  1152. return -1;
  1153. }
  1154. /* Begin Monitoring */
  1155. if (qe->parent->monfmt && *qe->parent->monfmt) {
  1156. monitorfilename = pbx_builtin_getvar_helper( qe->chan, "MONITOR_FILENAME");
  1157. if(monitorfilename) {
  1158. ast_monitor_start( peer, qe->parent->monfmt, monitorfilename, 1 );
  1159. } else {
  1160. ast_monitor_start( peer, qe->parent->monfmt, qe->chan->cdr->uniqueid, 1 );
  1161. }
  1162. if(qe->parent->monjoin) {
  1163. ast_monitor_setjoinfiles( peer, 1);
  1164. }
  1165. }
  1166. /* Drop out of the queue at this point, to prepare for next caller */
  1167. leave_queue(qe);
  1168. if( url && !ast_strlen_zero(url) && ast_channel_supports_html(peer) ) {
  1169. if (option_debug)
  1170. ast_log(LOG_DEBUG, "app_queue: sendurl=%s.\n", url);
  1171. ast_channel_sendurl( peer, url );
  1172. }
  1173. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "CONNECT", "%ld", (long)time(NULL) - qe->start);
  1174. strncpy(oldcontext, qe->chan->context, sizeof(oldcontext) - 1);
  1175. strncpy(oldexten, qe->chan->exten, sizeof(oldexten) - 1);
  1176. time(&callstart);
  1177. memset(&config,0,sizeof(struct ast_bridge_config));
  1178. config.allowredirect_in = allowredir_in;
  1179. config.allowredirect_out = allowredir_out;
  1180. config.allowdisconnect_in = allowdisconnect_in;
  1181. config.allowdisconnect_out = allowdisconnect_out;
  1182. bridge = ast_bridge_call(qe->chan,peer,&config);
  1183. if (strcasecmp(oldcontext, qe->chan->context) || strcasecmp(oldexten, qe->chan->exten)) {
  1184. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "TRANSFER", "%s|%s", qe->chan->exten, qe->chan->context);
  1185. } else if (qe->chan->_softhangup) {
  1186. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETECALLER", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
  1187. } else {
  1188. ast_queue_log(queuename, qe->chan->uniqueid, peer->name, "COMPLETEAGENT", "%ld|%ld", (long)(callstart - qe->start), (long)(time(NULL) - callstart));
  1189. }
  1190. if(bridge != AST_PBX_NO_HANGUP_PEER)
  1191. ast_hangup(peer);
  1192. update_queue(qe->parent, member);
  1193. if( bridge == 0 ) res=1; /* JDG: bridge successfull, leave app_queue */
  1194. else res = bridge; /* bridge error, stay in the queue */
  1195. }
  1196. out:
  1197. hanguptree(outgoing, NULL);
  1198. return res;
  1199. }
  1200. static int wait_a_bit(struct queue_ent *qe)
  1201. {
  1202. /* Don't need to hold the lock while we setup the outgoing calls */
  1203. int retrywait = qe->parent->retry * 1000;
  1204. return ast_waitfordigit(qe->chan, retrywait);
  1205. }
  1206. // [PHM 06/26/03]
  1207. static struct member * interface_exists( struct ast_call_queue * q, char * interface )
  1208. {
  1209. struct member * ret = NULL ;
  1210. struct member *mem;
  1211. char buf[500] ;
  1212. if( q != NULL )
  1213. {
  1214. mem = q->members ;
  1215. while( mem != NULL ) {
  1216. snprintf( buf, sizeof(buf), "%s/%s", mem->tech, mem->loc);
  1217. if( strcmp( buf, interface ) == 0 ) {
  1218. ret = mem ;
  1219. break ;
  1220. }
  1221. else
  1222. mem = mem->next ;
  1223. }
  1224. }
  1225. return( ret ) ;
  1226. }
  1227. static struct member * create_queue_node( char * interface, int penalty )
  1228. {
  1229. struct member * cur ;
  1230. char * tmp ;
  1231. /* Add a new member */
  1232. cur = malloc(sizeof(struct member));
  1233. if (cur) {
  1234. memset(cur, 0, sizeof(struct member));
  1235. cur->penalty = penalty;
  1236. strncpy(cur->tech, interface, sizeof(cur->tech) - 1);
  1237. if ((tmp = strchr(cur->tech, '/')))
  1238. *tmp = '\0';
  1239. if ((tmp = strchr(interface, '/'))) {
  1240. tmp++;
  1241. strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
  1242. } else
  1243. ast_log(LOG_WARNING, "No location at interface '%s'\n", interface);
  1244. }
  1245. return( cur ) ;
  1246. }
  1247. static int remove_from_queue(char *queuename, char *interface)
  1248. {
  1249. struct ast_call_queue *q;
  1250. struct member *last_member, *look;
  1251. int res = RES_NOSUCHQUEUE;
  1252. ast_mutex_lock(&qlock);
  1253. for (q = queues ; q ; q = q->next) {
  1254. ast_mutex_lock(&q->lock);
  1255. if (!strcmp(q->name, queuename)) {
  1256. if ((last_member = interface_exists(q, interface))) {
  1257. if ((look = q->members) == last_member) {
  1258. q->members = last_member->next;
  1259. } else {
  1260. while (look != NULL) {
  1261. if (look->next == last_member) {
  1262. look->next = last_member->next;
  1263. break;
  1264. } else {
  1265. look = look->next;
  1266. }
  1267. }
  1268. }
  1269. free(last_member);
  1270. res = RES_OKAY;
  1271. } else {
  1272. res = RES_EXISTS;
  1273. }
  1274. ast_mutex_unlock(&q->lock);
  1275. break;
  1276. }
  1277. ast_mutex_unlock(&q->lock);
  1278. }
  1279. ast_mutex_unlock(&qlock);
  1280. return res;
  1281. }
  1282. static int add_to_queue(char *queuename, char *interface, int penalty)
  1283. {
  1284. struct ast_call_queue *q;
  1285. struct member *new_member;
  1286. int res = RES_NOSUCHQUEUE;
  1287. ast_mutex_lock(&qlock);
  1288. for (q = queues ; q ; q = q->next) {
  1289. ast_mutex_lock(&q->lock);
  1290. if (!strcmp(q->name, queuename)) {
  1291. if (interface_exists(q, interface) == NULL) {
  1292. new_member = create_queue_node(interface, penalty);
  1293. if (new_member != NULL) {
  1294. new_member->dynamic = 1;
  1295. new_member->next = q->members;
  1296. q->members = new_member;
  1297. res = RES_OKAY;
  1298. } else {
  1299. res = RES_OUTOFMEMORY;
  1300. }
  1301. } else {
  1302. res = RES_EXISTS;
  1303. }
  1304. ast_mutex_unlock(&q->lock);
  1305. break;
  1306. }
  1307. ast_mutex_unlock(&q->lock);
  1308. }
  1309. ast_mutex_unlock(&qlock);
  1310. return res;
  1311. }
  1312. static int rqm_exec(struct ast_channel *chan, void *data)
  1313. {
  1314. int res=-1;
  1315. struct localuser *u;
  1316. char *info, *queuename;
  1317. char tmpchan[256]="";
  1318. char *interface = NULL;
  1319. if (!data) {
  1320. ast_log(LOG_WARNING, "RemoveQueueMember requires an argument (queuename[|interface])\n");
  1321. return -1;
  1322. }
  1323. info = ast_strdupa((char *)data);
  1324. if (!info) {
  1325. ast_log(LOG_ERROR, "Out of memory\n");
  1326. return -1;
  1327. }
  1328. LOCAL_USER_ADD(u);
  1329. queuename = info;
  1330. if (queuename) {
  1331. interface = strchr(queuename, '|');
  1332. if (interface) {
  1333. *interface = '\0';
  1334. interface++;
  1335. }
  1336. else {
  1337. strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
  1338. interface = strrchr(tmpchan, '-');
  1339. if (interface)
  1340. *interface = '\0';
  1341. interface = tmpchan;
  1342. }
  1343. }
  1344. switch (remove_from_queue(queuename, interface)) {
  1345. case RES_OKAY:
  1346. ast_log(LOG_NOTICE, "Removed interface '%s' from queue '%s'\n", interface, queuename);
  1347. res = 0;
  1348. break;
  1349. case RES_EXISTS:
  1350. ast_log(LOG_WARNING, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
  1351. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  1352. chan->priority += 100;
  1353. }
  1354. res = 0;
  1355. break;
  1356. case RES_NOSUCHQUEUE:
  1357. ast_log(LOG_WARNING, "Unable to remove interface from queue '%s': No such queue\n", queuename);
  1358. res = 0;
  1359. break;
  1360. case RES_OUTOFMEMORY:
  1361. ast_log(LOG_ERROR, "Out of memory\n");
  1362. break;
  1363. }
  1364. LOCAL_USER_REMOVE(u);
  1365. return res;
  1366. }
  1367. static int aqm_exec(struct ast_channel *chan, void *data)
  1368. {
  1369. int res=-1;
  1370. struct localuser *u;
  1371. char *queuename;
  1372. char *info;
  1373. char tmpchan[512]="";
  1374. char *interface=NULL;
  1375. char *penaltys=NULL;
  1376. int penalty = 0;
  1377. if (!data) {
  1378. ast_log(LOG_WARNING, "AddQueueMember requires an argument (queuename[|[interface][|penalty]])\n");
  1379. return -1;
  1380. }
  1381. info = ast_strdupa((char *)data);
  1382. if (!info) {
  1383. ast_log(LOG_ERROR, "Out of memory\n");
  1384. return -1;
  1385. }
  1386. LOCAL_USER_ADD(u);
  1387. queuename = info;
  1388. if (queuename) {
  1389. interface = strchr(queuename, '|');
  1390. if (interface) {
  1391. *interface = '\0';
  1392. interface++;
  1393. }
  1394. if (interface) {
  1395. penaltys = strchr(interface, '|');
  1396. if (penaltys) {
  1397. *penaltys = '\0';
  1398. penaltys++;
  1399. }
  1400. }
  1401. if (!interface || ast_strlen_zero(interface)) {
  1402. strncpy(tmpchan, chan->name, sizeof(tmpchan) - 1);
  1403. interface = strrchr(tmpchan, '-');
  1404. if (interface)
  1405. *interface = '\0';
  1406. interface = tmpchan;
  1407. }
  1408. if (penaltys && strlen(penaltys)) {
  1409. if ((sscanf(penaltys, "%d", &penalty) != 1) || penalty < 0) {
  1410. ast_log(LOG_WARNING, "Penalty '%s' is invalid, must be an integer >= 0\n", penaltys);
  1411. penalty = 0;
  1412. }
  1413. }
  1414. }
  1415. switch (add_to_queue(queuename, interface, penalty)) {
  1416. case RES_OKAY:
  1417. ast_log(LOG_NOTICE, "Added interface '%s' to queue '%s'\n", interface, queuename);
  1418. res = 0;
  1419. break;
  1420. case RES_EXISTS:
  1421. ast_log(LOG_WARNING, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
  1422. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  1423. chan->priority += 100;
  1424. }
  1425. res = 0;
  1426. break;
  1427. case RES_NOSUCHQUEUE:
  1428. ast_log(LOG_WARNING, "Unable to add interface to queue '%s': No such queue\n", queuename);
  1429. res = 0;
  1430. break;
  1431. case RES_OUTOFMEMORY:
  1432. ast_log(LOG_ERROR, "Out of memory\n");
  1433. break;
  1434. }
  1435. LOCAL_USER_REMOVE(u);
  1436. return res;
  1437. }
  1438. static int queue_exec(struct ast_channel *chan, void *data)
  1439. {
  1440. int res=-1;
  1441. int ringing=0;
  1442. struct localuser *u;
  1443. char *queuename;
  1444. char info[512];
  1445. char *options = NULL;
  1446. char *url = NULL;
  1447. char *announceoverride = NULL;
  1448. char *user_priority;
  1449. int prio;
  1450. char *queuetimeoutstr = NULL;
  1451. /* whether to exit Queue application after the timeout hits */
  1452. int go_on = 0;
  1453. /* Our queue entry */
  1454. struct queue_ent qe;
  1455. if (!data) {
  1456. ast_log(LOG_WARNING, "Queue requires an argument (queuename[|[timeout][|URL]])\n");
  1457. return -1;
  1458. }
  1459. LOCAL_USER_ADD(u);
  1460. /* Setup our queue entry */
  1461. memset(&qe, 0, sizeof(qe));
  1462. /* Parse our arguments XXX Check for failure XXX */
  1463. strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
  1464. queuename = info;
  1465. if (queuename) {
  1466. options = strchr(queuename, '|');
  1467. if (options) {
  1468. *options = '\0';
  1469. options++;
  1470. url = strchr(options, '|');
  1471. if (url) {
  1472. *url = '\0';
  1473. url++;
  1474. announceoverride = strchr(url, '|');
  1475. if (announceoverride) {
  1476. *announceoverride = '\0';
  1477. announceoverride++;
  1478. queuetimeoutstr = strchr(announceoverride, '|');
  1479. if (queuetimeoutstr) {
  1480. *queuetimeoutstr = '\0';
  1481. queuetimeoutstr++;
  1482. qe.queuetimeout = atoi(queuetimeoutstr);
  1483. } else {
  1484. qe.queuetimeout = 0;
  1485. }
  1486. }
  1487. }
  1488. }
  1489. }
  1490. /* Get the priority from the variable ${QUEUE_PRIO} */
  1491. user_priority = pbx_builtin_getvar_helper(chan, "QUEUE_PRIO");
  1492. if (user_priority) {
  1493. if (sscanf(user_priority, "%d", &prio) == 1) {
  1494. if (option_debug)
  1495. ast_log(LOG_DEBUG, "%s: Got priority %d from ${QUEUE_PRIO}.\n",
  1496. chan->name, prio);
  1497. } else {
  1498. ast_log(LOG_WARNING, "${QUEUE_PRIO}: Invalid value (%s), channel %s.\n",
  1499. user_priority, chan->name);
  1500. prio = 0;
  1501. }
  1502. } else {
  1503. if (option_debug)
  1504. ast_log(LOG_DEBUG, "NO QUEUE_PRIO variable found. Using default.\n");
  1505. prio = 0;
  1506. }
  1507. if (options) {
  1508. if (strchr(options, 'r')) {
  1509. ringing = 1;
  1510. }
  1511. }
  1512. // if (option_debug)
  1513. ast_log(LOG_DEBUG, "queue: %s, options: %s, url: %s, announce: %s, timeout: %d, priority: %d\n",
  1514. queuename, options, url, announceoverride, qe.queuetimeout, (int)prio);
  1515. qe.chan = chan;
  1516. qe.start = time(NULL);
  1517. qe.prio = (int)prio;
  1518. qe.last_pos_said = 0;
  1519. qe.last_pos = 0;
  1520. if (!join_queue(queuename, &qe)) {
  1521. ast_queue_log(queuename, chan->uniqueid, "NONE", "ENTERQUEUE", "%s|%s", url ? url : "", chan->callerid ? chan->callerid : "");
  1522. /* Start music on hold */
  1523. check_turns:
  1524. if (ringing) {
  1525. ast_indicate(chan, AST_CONTROL_RINGING);
  1526. } else {
  1527. ast_moh_start(chan, qe.moh);
  1528. }
  1529. for (;;) {
  1530. /* This is the wait loop for callers 2 through maxlen */
  1531. res = wait_our_turn(&qe, ringing);
  1532. /* If they hungup, return immediately */
  1533. if (res < 0) {
  1534. /* Record this abandoned call */
  1535. record_abandoned(&qe);
  1536. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1537. if (option_verbose > 2) {
  1538. ast_verbose(VERBOSE_PREFIX_3 "User disconnected while waiting their turn\n");
  1539. res = -1;
  1540. }
  1541. break;
  1542. }
  1543. if (!res)
  1544. break;
  1545. if (valid_exit(&qe, res)) {
  1546. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1547. break;
  1548. }
  1549. }
  1550. if (!res) {
  1551. for (;;) {
  1552. /* This is the wait loop for the head caller*/
  1553. /* To exit, they may get their call answered; */
  1554. /* they may dial a digit from the queue context; */
  1555. /* or, they may timeout. */
  1556. /* Leave if we have exceeded our queuetimeout */
  1557. if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
  1558. res = 0;
  1559. break;
  1560. }
  1561. /* leave the queue if no agents, if enabled */
  1562. if (!((qe.parent)->members) && (qe.parent)->leavewhenempty) {
  1563. leave_queue(&qe);
  1564. break;
  1565. }
  1566. /* Make a position announcement, if enabled */
  1567. if (qe.parent->announcefrequency && !ringing)
  1568. say_position(&qe);
  1569. /* Try calling all queue members for 'timeout' seconds */
  1570. res = try_calling(&qe, options, announceoverride, url, &go_on);
  1571. if (res) {
  1572. if (res < 0) {
  1573. if (!qe.handled)
  1574. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1575. } else if (res > 0)
  1576. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1577. break;
  1578. }
  1579. /* Leave if we have exceeded our queuetimeout */
  1580. if (qe.queuetimeout && ( (time(NULL) - qe.start) >= qe.queuetimeout) ) {
  1581. res = 0;
  1582. break;
  1583. }
  1584. /* OK, we didn't get anybody; wait for 'retry' seconds; may get a digit to exit with */
  1585. res = wait_a_bit(&qe);
  1586. if (res < 0) {
  1587. ast_queue_log(queuename, chan->uniqueid, "NONE", "ABANDON", "%d|%d|%ld", qe.pos, qe.opos, (long)time(NULL) - qe.start);
  1588. if (option_verbose > 2) {
  1589. ast_verbose(VERBOSE_PREFIX_3 "User disconnected when they almost made it\n");
  1590. res = -1;
  1591. }
  1592. break;
  1593. }
  1594. if (res && valid_exit(&qe, res)) {
  1595. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHKEY", "%c|%d", res, qe.pos);
  1596. break;
  1597. }
  1598. /* exit after 'timeout' cycle if 'n' option enabled */
  1599. if (go_on) {
  1600. if (option_verbose > 2) {
  1601. ast_verbose(VERBOSE_PREFIX_3 "Exiting on time-out cycle\n");
  1602. res = -1;
  1603. }
  1604. ast_queue_log(queuename, chan->uniqueid, "NONE", "EXITWITHTIMEOUT", "%d", qe.pos);
  1605. res = 0;
  1606. break;
  1607. }
  1608. /* Since this is a priority queue and
  1609. * it is not sure that we are still at the head
  1610. * of the queue, go and check for our turn again.
  1611. */
  1612. if (!is_our_turn(&qe)) {
  1613. ast_log(LOG_DEBUG, "Darn priorities, going back in queue (%s)!\n",
  1614. qe.chan->name);
  1615. goto check_turns;
  1616. }
  1617. }
  1618. }
  1619. /* Don't allow return code > 0 */
  1620. if ((res == 0) || (res > 0 && res != AST_PBX_KEEPALIVE)) {
  1621. res = 0;
  1622. if (ringing) {
  1623. ast_indicate(chan, -1);
  1624. } else {
  1625. ast_moh_stop(chan);
  1626. }
  1627. ast_stopstream(chan);
  1628. }
  1629. leave_queue(&qe);
  1630. } else {
  1631. ast_log(LOG_WARNING, "Unable to join queue '%s'\n", queuename);
  1632. res = 0;
  1633. }
  1634. LOCAL_USER_REMOVE(u);
  1635. return res;
  1636. }
  1637. static void reload_queues(void)
  1638. {
  1639. struct ast_call_queue *q, *ql, *qn;
  1640. struct ast_config *cfg;
  1641. char *cat, *tmp;
  1642. struct ast_variable *var;
  1643. struct member *prev, *cur;
  1644. int new;
  1645. cfg = ast_load("queues.conf");
  1646. if (!cfg) {
  1647. ast_log(LOG_NOTICE, "No call queueing config file, so no call queues\n");
  1648. return;
  1649. }
  1650. ast_mutex_lock(&qlock);
  1651. /* Mark all queues as dead for the moment */
  1652. q = queues;
  1653. while(q) {
  1654. q->dead = 1;
  1655. q = q->next;
  1656. }
  1657. /* Chug through config file */
  1658. cat = ast_category_browse(cfg, NULL);
  1659. while(cat) {
  1660. if (strcasecmp(cat, "general")) {
  1661. /* Look for an existing one */
  1662. q = queues;
  1663. while(q) {
  1664. if (!strcmp(q->name, cat))
  1665. break;
  1666. q = q->next;
  1667. }
  1668. if (!q) {
  1669. /* Make one then */
  1670. q = malloc(sizeof(struct ast_call_queue));
  1671. if (q) {
  1672. /* Initialize it */
  1673. memset(q, 0, sizeof(struct ast_call_queue));
  1674. ast_mutex_init(&q->lock);
  1675. strncpy(q->name, cat, sizeof(q->name) - 1);
  1676. new = 1;
  1677. } else new = 0;
  1678. } else
  1679. new = 0;
  1680. if (q) {
  1681. if (!new)
  1682. ast_mutex_lock(&q->lock);
  1683. /* Re-initialize the queue */
  1684. q->dead = 0;
  1685. q->retry = 0;
  1686. q->timeout = -1;
  1687. q->maxlen = 0;
  1688. q->announcefrequency = 0;
  1689. q->announceholdtime = 0;
  1690. q->roundingseconds = 0; /* Default - don't announce seconds */
  1691. q->holdtime = 0;
  1692. q->callscompleted = 0;
  1693. q->callsabandoned = 0;
  1694. q->callscompletedinsl = 0;
  1695. q->servicelevel = 0;
  1696. q->wrapuptime = 0;
  1697. free_members(q, 0);
  1698. q->moh[0] = '\0';
  1699. q->announce[0] = '\0';
  1700. q->context[0] = '\0';
  1701. q->monfmt[0] = '\0';
  1702. strncpy(q->sound_next, "queue-youarenext", sizeof(q->sound_next) - 1);
  1703. strncpy(q->sound_thereare, "queue-thereare", sizeof(q->sound_thereare) - 1);
  1704. strncpy(q->sound_calls, "queue-callswaiting", sizeof(q->sound_calls) - 1);
  1705. strncpy(q->sound_holdtime, "queue-holdtime", sizeof(q->sound_holdtime) - 1);
  1706. strncpy(q->sound_minutes, "queue-minutes", sizeof(q->sound_minutes) - 1);
  1707. strncpy(q->sound_seconds, "queue-seconds", sizeof(q->sound_seconds) - 1);
  1708. strncpy(q->sound_thanks, "queue-thankyou", sizeof(q->sound_thanks) - 1);
  1709. strncpy(q->sound_lessthan, "queue-less-than", sizeof(q->sound_lessthan) - 1);
  1710. prev = q->members;
  1711. if (prev) {
  1712. /* find the end of any dynamic members */
  1713. while(prev->next)
  1714. prev = prev->next;
  1715. }
  1716. var = ast_variable_browse(cfg, cat);
  1717. while(var) {
  1718. if (!strcasecmp(var->name, "member")) {
  1719. /* Add a new member */
  1720. cur = malloc(sizeof(struct member));
  1721. if (cur) {
  1722. memset(cur, 0, sizeof(struct member));
  1723. strncpy(cur->tech, var->value, sizeof(cur->tech) - 1);
  1724. if ((tmp = strchr(cur->tech, ','))) {
  1725. *tmp = '\0';
  1726. tmp++;
  1727. cur->penalty = atoi(tmp);
  1728. if (cur->penalty < 0)
  1729. cur->penalty = 0;
  1730. }
  1731. if ((tmp = strchr(cur->tech, '/')))
  1732. *tmp = '\0';
  1733. if ((tmp = strchr(var->value, '/'))) {
  1734. tmp++;
  1735. strncpy(cur->loc, tmp, sizeof(cur->loc) - 1);
  1736. if ((tmp = strchr(cur->loc, ',')))
  1737. *tmp = '\0';
  1738. } else
  1739. ast_log(LOG_WARNING, "No location at line %d of queue.conf\n", var->lineno);
  1740. if (prev)
  1741. prev->next = cur;
  1742. else
  1743. q->members = cur;
  1744. prev = cur;
  1745. }
  1746. } else if (!strcasecmp(var->name, "music") || !strcasecmp(var->name, "musiconhold")) {
  1747. strncpy(q->moh, var->value, sizeof(q->moh) - 1);
  1748. } else if (!strcasecmp(var->name, "announce")) {
  1749. strncpy(q->announce, var->value, sizeof(q->announce) - 1);
  1750. } else if (!strcasecmp(var->name, "context")) {
  1751. strncpy(q->context, var->value, sizeof(q->context) - 1);
  1752. } else if (!strcasecmp(var->name, "timeout")) {
  1753. q->timeout = atoi(var->value);
  1754. } else if (!strcasecmp(var->name, "monitor-join")) {
  1755. q->monjoin = ast_true(var->value);
  1756. } else if (!strcasecmp(var->name, "monitor-format")) {
  1757. strncpy(q->monfmt, var->value, sizeof(q->monfmt) - 1);
  1758. } else if (!strcasecmp(var->name, "queue-youarenext")) {
  1759. strncpy(q->sound_next, var->value, sizeof(q->sound_next) - 1);
  1760. } else if (!strcasecmp(var->name, "queue-thereare")) {
  1761. strncpy(q->sound_thereare, var->value, sizeof(q->sound_thereare) - 1);
  1762. } else if (!strcasecmp(var->name, "queue-callswaiting")) {
  1763. strncpy(q->sound_calls, var->value, sizeof(q->sound_calls) - 1);
  1764. } else if (!strcasecmp(var->name, "queue-holdtime")) {
  1765. strncpy(q->sound_holdtime, var->value, sizeof(q->sound_holdtime) - 1);
  1766. } else if (!strcasecmp(var->name, "queue-minutes")) {
  1767. strncpy(q->sound_minutes, var->value, sizeof(q->sound_minutes) - 1);
  1768. } else if (!strcasecmp(var->name, "queue-seconds")) {
  1769. strncpy(q->sound_seconds, var->value, sizeof(q->sound_seconds) - 1);
  1770. } else if (!strcasecmp(var->name, "queue-lessthan")) {
  1771. strncpy(q->sound_lessthan, var->value, sizeof(q->sound_lessthan) - 1);
  1772. } else if (!strcasecmp(var->name, "queue-thankyou")) {
  1773. strncpy(q->sound_thanks, var->value, sizeof(q->sound_thanks) - 1);
  1774. } else if (!strcasecmp(var->name, "announce-frequency")) {
  1775. q->announcefrequency = atoi(var->value);
  1776. } else if (!strcasecmp(var->name, "announce-round-seconds")) {
  1777. q->roundingseconds = atoi(var->value);
  1778. if(q->roundingseconds>60 || q->roundingseconds<0) {
  1779. 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);
  1780. q->roundingseconds=0;
  1781. }
  1782. } else if (!strcasecmp(var->name, "announce-holdtime")) {
  1783. q->announceholdtime = (!strcasecmp(var->value,"once")) ? 1 : ast_true(var->value);
  1784. } else if (!strcasecmp(var->name, "retry")) {
  1785. q->retry = atoi(var->value);
  1786. } else if (!strcasecmp(var->name, "wrapuptime")) {
  1787. q->wrapuptime = atoi(var->value);
  1788. } else if (!strcasecmp(var->name, "maxlen")) {
  1789. q->maxlen = atoi(var->value);
  1790. } else if (!strcasecmp(var->name, "servicelevel")) {
  1791. q->servicelevel= atoi(var->value);
  1792. } else if (!strcasecmp(var->name, "strategy")) {
  1793. q->strategy = strat2int(var->value);
  1794. if (q->strategy < 0) {
  1795. ast_log(LOG_WARNING, "'%s' isn't a valid strategy, using ringall instead\n", var->value);
  1796. q->strategy = 0;
  1797. }
  1798. } else if (!strcasecmp(var->name, "joinempty")) {
  1799. q->joinempty = ast_true(var->value);
  1800. } else if (!strcasecmp(var->name, "leavewhenempty")) {
  1801. q->leavewhenempty = ast_true(var->value);
  1802. } else if (!strcasecmp(var->name, "eventwhencalled")) {
  1803. q->eventwhencalled = ast_true(var->value);
  1804. } else {
  1805. ast_log(LOG_WARNING, "Unknown keyword in queue '%s': %s at line %d of queue.conf\n", cat, var->name, var->lineno);
  1806. }
  1807. var = var->next;
  1808. }
  1809. if (q->retry < 1)
  1810. q->retry = DEFAULT_RETRY;
  1811. if (q->timeout < 0)
  1812. q->timeout = DEFAULT_TIMEOUT;
  1813. if (q->maxlen < 0)
  1814. q->maxlen = 0;
  1815. if (!new)
  1816. ast_mutex_unlock(&q->lock);
  1817. if (new) {
  1818. q->next = queues;
  1819. queues = q;
  1820. }
  1821. }
  1822. }
  1823. cat = ast_category_browse(cfg, cat);
  1824. }
  1825. ast_destroy(cfg);
  1826. q = queues;
  1827. ql = NULL;
  1828. while(q) {
  1829. qn = q->next;
  1830. if (q->dead) {
  1831. if (ql)
  1832. ql->next = q->next;
  1833. else
  1834. queues = q->next;
  1835. if (!q->count) {
  1836. free(q);
  1837. } else
  1838. ast_log(LOG_WARNING, "XXX Leaking a little memory :( XXX\n");
  1839. } else
  1840. ql = q;
  1841. q = qn;
  1842. }
  1843. ast_mutex_unlock(&qlock);
  1844. }
  1845. static int __queues_show(int fd, int argc, char **argv, int queue_show)
  1846. {
  1847. struct ast_call_queue *q;
  1848. struct queue_ent *qe;
  1849. struct member *mem;
  1850. int pos;
  1851. time_t now;
  1852. char max[80] = "";
  1853. char calls[80] = "";
  1854. float sl = 0;
  1855. time(&now);
  1856. if ((!queue_show && argc != 2) || (queue_show && argc != 3))
  1857. return RESULT_SHOWUSAGE;
  1858. ast_mutex_lock(&qlock);
  1859. q = queues;
  1860. if (!q) {
  1861. ast_mutex_unlock(&qlock);
  1862. if (queue_show)
  1863. ast_cli(fd, "No such queue: %s.\n",argv[2]);
  1864. else
  1865. ast_cli(fd, "No queues.\n");
  1866. return RESULT_SUCCESS;
  1867. }
  1868. while(q) {
  1869. ast_mutex_lock(&q->lock);
  1870. if (queue_show) {
  1871. if (strcasecmp(q->name, argv[2]) != 0) {
  1872. ast_mutex_unlock(&q->lock);
  1873. q = q->next;
  1874. if (!q) {
  1875. ast_cli(fd, "No such queue: %s.\n",argv[2]);
  1876. break;
  1877. }
  1878. continue;
  1879. }
  1880. }
  1881. if (q->maxlen)
  1882. snprintf(max, sizeof(max), "%d", q->maxlen);
  1883. else
  1884. strncpy(max, "unlimited", sizeof(max) - 1);
  1885. sl = 0;
  1886. if(q->callscompleted > 0)
  1887. sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
  1888. 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",
  1889. q->name, q->count, max, int2strat(q->strategy), q->holdtime, q->callscompleted, q->callsabandoned,sl,q->servicelevel);
  1890. if (q->members) {
  1891. ast_cli(fd, " Members: \n");
  1892. for (mem = q->members; mem; mem = mem->next) {
  1893. if (mem->penalty)
  1894. snprintf(max, sizeof(max) - 20, " with penalty %d", mem->penalty);
  1895. else
  1896. max[0] = '\0';
  1897. if (mem->dynamic)
  1898. strncat(max, " (dynamic)", sizeof(max) - strlen(max) - 1);
  1899. if (mem->calls) {
  1900. snprintf(calls, sizeof(calls), " has taken %d calls (last was %ld secs ago)",
  1901. mem->calls, (long)(time(NULL) - mem->lastcall));
  1902. } else
  1903. strncpy(calls, " has taken no calls yet", sizeof(calls) - 1);
  1904. ast_cli(fd, " %s/%s%s%s\n", mem->tech, mem->loc, max, calls);
  1905. }
  1906. } else
  1907. ast_cli(fd, " No Members\n");
  1908. if (q->head) {
  1909. pos = 1;
  1910. ast_cli(fd, " Callers: \n");
  1911. for (qe = q->head; qe; qe = qe->next)
  1912. ast_cli(fd, " %d. %s (wait: %ld:%2.2ld, prio: %d)\n", pos++, qe->chan->name,
  1913. (long)(now - qe->start) / 60, (long)(now - qe->start) % 60, qe->prio);
  1914. } else
  1915. ast_cli(fd, " No Callers\n");
  1916. ast_cli(fd, "\n");
  1917. ast_mutex_unlock(&q->lock);
  1918. q = q->next;
  1919. if (queue_show)
  1920. break;
  1921. }
  1922. ast_mutex_unlock(&qlock);
  1923. return RESULT_SUCCESS;
  1924. }
  1925. static int queues_show(int fd, int argc, char **argv)
  1926. {
  1927. return __queues_show(fd, argc, argv, 0);
  1928. }
  1929. static int queue_show(int fd, int argc, char **argv)
  1930. {
  1931. return __queues_show(fd, argc, argv, 1);
  1932. }
  1933. static char *complete_queue(char *line, char *word, int pos, int state)
  1934. {
  1935. struct ast_call_queue *q;
  1936. int which=0;
  1937. ast_mutex_lock(&qlock);
  1938. for (q = queues; q; q = q->next) {
  1939. if (!strncasecmp(word, q->name, strlen(word))) {
  1940. if (++which > state)
  1941. break;
  1942. }
  1943. }
  1944. ast_mutex_unlock(&qlock);
  1945. return q ? strdup(q->name) : NULL;
  1946. }
  1947. /* JDG: callback to display queues status in manager */
  1948. static int manager_queues_show( struct mansession *s, struct message *m )
  1949. {
  1950. char *a[] = { "show", "queues" };
  1951. return queues_show( s->fd, 2, a );
  1952. } /* /JDG */
  1953. /* Dump queue status */
  1954. static int manager_queues_status( struct mansession *s, struct message *m )
  1955. {
  1956. time_t now;
  1957. int pos;
  1958. char *id = astman_get_header(m,"ActionID");
  1959. char idText[256] = "";
  1960. struct ast_call_queue *q;
  1961. struct queue_ent *qe;
  1962. float sl = 0;
  1963. struct member *mem;
  1964. astman_send_ack(s, m, "Queue status will follow");
  1965. time(&now);
  1966. ast_mutex_lock(&qlock);
  1967. if (!ast_strlen_zero(id)) {
  1968. snprintf(idText,256,"ActionID: %s\r\n",id);
  1969. }
  1970. for (q = queues; q; q = q->next) {
  1971. ast_mutex_lock(&q->lock);
  1972. /* List queue properties */
  1973. if(q->callscompleted > 0)
  1974. sl = 100*((float)q->callscompletedinsl/(float)q->callscompleted);
  1975. ast_mutex_lock(&s->lock);
  1976. ast_cli(s->fd, "Event: QueueParams\r\n"
  1977. "Queue: %s\r\n"
  1978. "Max: %d\r\n"
  1979. "Calls: %d\r\n"
  1980. "Holdtime: %d\r\n"
  1981. "Completed: %d\r\n"
  1982. "Abandoned: %d\r\n"
  1983. "ServiceLevel: %d\r\n"
  1984. "ServicelevelPerf: %2.1f\r\n"
  1985. "%s"
  1986. "\r\n",
  1987. q->name, q->maxlen, q->count, q->holdtime, q->callscompleted,
  1988. q->callsabandoned, q->servicelevel, sl, idText);
  1989. /* List Queue Members */
  1990. for (mem = q->members; mem; mem = mem->next)
  1991. ast_cli(s->fd, "Event: QueueMember\r\n"
  1992. "Queue: %s\r\n"
  1993. "Location: %s/%s\r\n"
  1994. "Membership: %s\r\n"
  1995. "Penalty: %d\r\n"
  1996. "CallsTaken: %d\r\n"
  1997. "LastCall: %ld\r\n"
  1998. "%s"
  1999. "\r\n",
  2000. q->name, mem->tech, mem->loc, mem->dynamic ? "dynamic" : "static",
  2001. mem->penalty, mem->calls, mem->lastcall, idText);
  2002. /* List Queue Entries */
  2003. pos = 1;
  2004. for (qe = q->head; qe; qe = qe->next)
  2005. ast_cli(s->fd, "Event: QueueEntry\r\n"
  2006. "Queue: %s\r\n"
  2007. "Position: %d\r\n"
  2008. "Channel: %s\r\n"
  2009. "CallerID: %s\r\n"
  2010. "Wait: %ld\r\n"
  2011. "%s"
  2012. "\r\n",
  2013. q->name, pos++, qe->chan->name, (qe->chan->callerid ? qe->chan->callerid : ""), (long)(now - qe->start), idText);
  2014. ast_mutex_unlock(&s->lock);
  2015. ast_mutex_unlock(&q->lock);
  2016. }
  2017. ast_mutex_unlock(&qlock);
  2018. return RESULT_SUCCESS;
  2019. }
  2020. static int manager_add_queue_member(struct mansession *s, struct message *m)
  2021. {
  2022. char *queuename, *interface, *penalty_s;
  2023. int penalty = 0;
  2024. queuename = astman_get_header(m, "Queue");
  2025. interface = astman_get_header(m, "Interface");
  2026. penalty_s = astman_get_header(m, "Penalty");
  2027. if (ast_strlen_zero(queuename)) {
  2028. astman_send_error(s, m, "'Queue' not specified.");
  2029. return 0;
  2030. }
  2031. if (ast_strlen_zero(interface)) {
  2032. astman_send_error(s, m, "'Interface' not specified.");
  2033. return 0;
  2034. }
  2035. if (ast_strlen_zero(penalty_s))
  2036. penalty = 0;
  2037. else if (sscanf(penalty_s, "%d", &penalty) != 1) {
  2038. penalty = 0;
  2039. }
  2040. switch (add_to_queue(queuename, interface, penalty)) {
  2041. case RES_OKAY:
  2042. astman_send_ack(s, m, "Added interface to queue");
  2043. break;
  2044. case RES_EXISTS:
  2045. astman_send_error(s, m, "Unable to add interface: Already there");
  2046. break;
  2047. case RES_NOSUCHQUEUE:
  2048. astman_send_error(s, m, "Unable to add interface to queue: No such queue");
  2049. break;
  2050. case RES_OUTOFMEMORY:
  2051. astman_send_error(s, m, "Out of memory");
  2052. break;
  2053. }
  2054. return 0;
  2055. }
  2056. static int manager_remove_queue_member(struct mansession *s, struct message *m)
  2057. {
  2058. char *queuename, *interface;
  2059. queuename = astman_get_header(m, "Queue");
  2060. interface = astman_get_header(m, "Interface");
  2061. if (ast_strlen_zero(queuename) || ast_strlen_zero(interface)) {
  2062. astman_send_error(s, m, "Need 'Queue' and 'Interface' parameters.");
  2063. return 0;
  2064. }
  2065. switch (remove_from_queue(queuename, interface)) {
  2066. case RES_OKAY:
  2067. astman_send_ack(s, m, "Removed interface from queue");
  2068. break;
  2069. case RES_EXISTS:
  2070. astman_send_error(s, m, "Unable to remove interface: Not there");
  2071. break;
  2072. case RES_NOSUCHQUEUE:
  2073. astman_send_error(s, m, "Unable to remove interface from queue: No such queue");
  2074. break;
  2075. case RES_OUTOFMEMORY:
  2076. astman_send_error(s, m, "Out of memory");
  2077. break;
  2078. }
  2079. return 0;
  2080. }
  2081. static int handle_add_queue_member(int fd, int argc, char *argv[])
  2082. {
  2083. char *queuename, *interface;
  2084. int penalty;
  2085. if ((argc != 6) && (argc != 8)) {
  2086. return RESULT_SHOWUSAGE;
  2087. } else if (strcmp(argv[4], "to")) {
  2088. return RESULT_SHOWUSAGE;
  2089. } else if ((argc == 8) && strcmp(argv[6], "penalty")) {
  2090. return RESULT_SHOWUSAGE;
  2091. }
  2092. queuename = argv[5];
  2093. interface = argv[3];
  2094. if (argc == 8) {
  2095. if (sscanf(argv[7], "%d", &penalty) == 1) {
  2096. if (penalty < 0) {
  2097. ast_cli(fd, "Penalty must be >= 0\n");
  2098. penalty = 0;
  2099. }
  2100. } else {
  2101. ast_cli(fd, "Penalty must be an integer >= 0\n");
  2102. penalty = 0;
  2103. }
  2104. } else {
  2105. penalty = 0;
  2106. }
  2107. switch (add_to_queue(queuename, interface, penalty)) {
  2108. case RES_OKAY:
  2109. ast_cli(fd, "Added interface '%s' to queue '%s'\n", interface, queuename);
  2110. return RESULT_SUCCESS;
  2111. case RES_EXISTS:
  2112. ast_cli(fd, "Unable to add interface '%s' to queue '%s': Already there\n", interface, queuename);
  2113. return RESULT_FAILURE;
  2114. case RES_NOSUCHQUEUE:
  2115. ast_cli(fd, "Unable to add interface to queue '%s': No such queue\n", queuename);
  2116. return RESULT_FAILURE;
  2117. case RES_OUTOFMEMORY:
  2118. ast_cli(fd, "Out of memory\n");
  2119. return RESULT_FAILURE;
  2120. default:
  2121. return RESULT_FAILURE;
  2122. }
  2123. }
  2124. static char *complete_add_queue_member(char *line, char *word, int pos, int state)
  2125. {
  2126. /* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue>; 6 - penalty; 7 - <penalty> */
  2127. switch (pos) {
  2128. case 3:
  2129. /* Don't attempt to complete name of member (infinite possibilities) */
  2130. return NULL;
  2131. case 4:
  2132. if (state == 0) {
  2133. return strdup("to");
  2134. } else {
  2135. return NULL;
  2136. }
  2137. case 5:
  2138. /* No need to duplicate code */
  2139. return complete_queue(line, word, pos, state);
  2140. case 6:
  2141. if (state == 0) {
  2142. return strdup("penalty");
  2143. } else {
  2144. return NULL;
  2145. }
  2146. case 7:
  2147. if (state < 100) { /* 0-99 */
  2148. char *num = malloc(3);
  2149. if (num) {
  2150. sprintf(num, "%d", state);
  2151. }
  2152. return num;
  2153. } else {
  2154. return NULL;
  2155. }
  2156. default:
  2157. return NULL;
  2158. }
  2159. }
  2160. static int handle_remove_queue_member(int fd, int argc, char *argv[])
  2161. {
  2162. char *queuename, *interface;
  2163. if (argc != 6) {
  2164. return RESULT_SHOWUSAGE;
  2165. } else if (strcmp(argv[4], "from")) {
  2166. return RESULT_SHOWUSAGE;
  2167. }
  2168. queuename = argv[5];
  2169. interface = argv[3];
  2170. switch (remove_from_queue(queuename, interface)) {
  2171. case RES_OKAY:
  2172. ast_cli(fd, "Removed interface '%s' from queue '%s'\n", interface, queuename);
  2173. return RESULT_SUCCESS;
  2174. case RES_EXISTS:
  2175. ast_cli(fd, "Unable to remove interface '%s' from queue '%s': Not there\n", interface, queuename);
  2176. return RESULT_FAILURE;
  2177. case RES_NOSUCHQUEUE:
  2178. ast_cli(fd, "Unable to remove interface from queue '%s': No such queue\n", queuename);
  2179. return RESULT_FAILURE;
  2180. case RES_OUTOFMEMORY:
  2181. ast_cli(fd, "Out of memory\n");
  2182. return RESULT_FAILURE;
  2183. default:
  2184. return RESULT_FAILURE;
  2185. }
  2186. }
  2187. static char *complete_remove_queue_member(char *line, char *word, int pos, int state)
  2188. {
  2189. int which = 0;
  2190. struct ast_call_queue *q;
  2191. struct member *m;
  2192. /* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue> */
  2193. if ((pos > 5) || (pos < 3)) {
  2194. return NULL;
  2195. }
  2196. if (pos == 4) {
  2197. if (state == 0) {
  2198. return strdup("from");
  2199. } else {
  2200. return NULL;
  2201. }
  2202. }
  2203. if (pos == 5) {
  2204. /* No need to duplicate code */
  2205. return complete_queue(line, word, pos, state);
  2206. }
  2207. if (queues != NULL) {
  2208. for (q = queues ; q ; q = q->next) {
  2209. ast_mutex_lock(&q->lock);
  2210. for (m = q->members ; m ; m = m->next) {
  2211. if (++which > state) {
  2212. char *tmp = malloc(strlen(m->tech) + strlen(m->loc) + 2);
  2213. if (tmp) {
  2214. sprintf(tmp, "%s/%s", m->tech, m->loc);
  2215. } else {
  2216. ast_log(LOG_ERROR, "Out of memory\n");
  2217. }
  2218. ast_mutex_unlock(&q->lock);
  2219. return tmp;
  2220. }
  2221. }
  2222. ast_mutex_unlock(&q->lock);
  2223. }
  2224. }
  2225. return NULL;
  2226. }
  2227. static char show_queues_usage[] =
  2228. "Usage: show queues\n"
  2229. " Provides summary information on call queues.\n";
  2230. static struct ast_cli_entry cli_show_queues = {
  2231. { "show", "queues", NULL }, queues_show,
  2232. "Show status of queues", show_queues_usage, NULL };
  2233. static char show_queue_usage[] =
  2234. "Usage: show queue\n"
  2235. " Provides summary information on a specified queue.\n";
  2236. static struct ast_cli_entry cli_show_queue = {
  2237. { "show", "queue", NULL }, queue_show,
  2238. "Show status of a specified queue", show_queue_usage, complete_queue };
  2239. static char aqm_cmd_usage[] =
  2240. "Usage: add queue member <channel> to <queue> [penalty <penalty>]\n";
  2241. static struct ast_cli_entry cli_add_queue_member = {
  2242. { "add", "queue", "member", NULL }, handle_add_queue_member,
  2243. "Add a channel to a specified queue", aqm_cmd_usage, complete_add_queue_member };
  2244. static char rqm_cmd_usage[] =
  2245. "Usage: remove queue member <channel> from <queue>\n";
  2246. static struct ast_cli_entry cli_remove_queue_member = {
  2247. { "remove", "queue", "member", NULL }, handle_remove_queue_member,
  2248. "Removes a channel from a specified queue", rqm_cmd_usage, complete_remove_queue_member };
  2249. int unload_module(void)
  2250. {
  2251. STANDARD_HANGUP_LOCALUSERS;
  2252. ast_cli_unregister(&cli_show_queue);
  2253. ast_cli_unregister(&cli_show_queues);
  2254. ast_cli_unregister(&cli_add_queue_member);
  2255. ast_cli_unregister(&cli_remove_queue_member);
  2256. ast_manager_unregister("Queues");
  2257. ast_manager_unregister("QueueStatus");
  2258. ast_manager_unregister("QueueAdd");
  2259. ast_manager_unregister("QueueRemove");
  2260. ast_unregister_application(app_aqm);
  2261. ast_unregister_application(app_rqm);
  2262. return ast_unregister_application(app);
  2263. }
  2264. int load_module(void)
  2265. {
  2266. int res;
  2267. res = ast_register_application(app, queue_exec, synopsis, descrip);
  2268. if (!res) {
  2269. ast_cli_register(&cli_show_queue);
  2270. ast_cli_register(&cli_show_queues);
  2271. ast_cli_register(&cli_add_queue_member);
  2272. ast_cli_register(&cli_remove_queue_member);
  2273. ast_manager_register( "Queues", 0, manager_queues_show, "Queues" );
  2274. ast_manager_register( "QueueStatus", 0, manager_queues_status, "Queue Status" );
  2275. ast_manager_register( "QueueAdd", EVENT_FLAG_AGENT, manager_add_queue_member, "Add interface to queue." );
  2276. ast_manager_register( "QueueRemove", EVENT_FLAG_AGENT, manager_remove_queue_member, "Remove interface from queue." );
  2277. ast_register_application(app_aqm, aqm_exec, app_aqm_synopsis, app_aqm_descrip) ;
  2278. ast_register_application(app_rqm, rqm_exec, app_rqm_synopsis, app_rqm_descrip) ;
  2279. }
  2280. reload_queues();
  2281. return res;
  2282. }
  2283. int reload(void)
  2284. {
  2285. reload_queues();
  2286. return 0;
  2287. }
  2288. char *description(void)
  2289. {
  2290. return tdesc;
  2291. }
  2292. int usecount(void)
  2293. {
  2294. int res;
  2295. STANDARD_USECOUNT(res);
  2296. return res;
  2297. }
  2298. char *key()
  2299. {
  2300. return ASTERISK_GPL_KEY;
  2301. }