chan_agent.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Implementation of Agents
  5. *
  6. * Copyright (C) 1999-2004, Digium Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <asterisk/lock.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/channel_pvt.h>
  18. #include <asterisk/config.h>
  19. #include <asterisk/logger.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/pbx.h>
  22. #include <asterisk/options.h>
  23. #include <asterisk/lock.h>
  24. #include <asterisk/sched.h>
  25. #include <asterisk/io.h>
  26. #include <asterisk/rtp.h>
  27. #include <asterisk/acl.h>
  28. #include <asterisk/callerid.h>
  29. #include <asterisk/file.h>
  30. #include <asterisk/cli.h>
  31. #include <asterisk/app.h>
  32. #include <asterisk/musiconhold.h>
  33. #include <asterisk/manager.h>
  34. #include <asterisk/features.h>
  35. #include <asterisk/utils.h>
  36. #include <sys/socket.h>
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <stdlib.h>
  40. #include <fcntl.h>
  41. #include <netdb.h>
  42. #include <arpa/inet.h>
  43. #include <sys/signal.h>
  44. static char *desc = "Agent Proxy Channel";
  45. static char *type = "Agent";
  46. static char *tdesc = "Call Agent Proxy Channel";
  47. static char *config = "agents.conf";
  48. static char *app = "AgentLogin";
  49. static char *app2 = "AgentCallbackLogin";
  50. static char *app3 = "AgentMonitorOutgoing";
  51. static char *synopsis = "Call agent login";
  52. static char *synopsis2 = "Call agent callback login";
  53. static char *synopsis3 = "Record agent's outgoing call";
  54. static char *descrip =
  55. " AgentLogin([AgentNo][|options]):\n"
  56. "Asks the agent to login to the system. Always returns -1. While\n"
  57. "logged in, the agent can receive calls and will hear a 'beep'\n"
  58. "when a new call comes in. The agent can dump the call by pressing\n"
  59. "the star key.\n"
  60. "The option string may contain zero or more of the following characters:\n"
  61. " 's' -- silent login - do not announce the login ok segment\n";
  62. static char *descrip2 =
  63. " AgentCallbackLogin([AgentNo][|[options][exten]@context]):\n"
  64. "Asks the agent to login to the system with callback. Always returns -1.\n"
  65. "The agent's callback extension is called (optionally with the specified\n"
  66. "context. \n";
  67. static char *descrip3 =
  68. " AgentMonitorOutgoing([options]):\n"
  69. "Tries to figure out the id of the agent who is placing outgoing call based on comparision of the callerid of the current interface and the global variable placed by the AgentCallbackLogin application. That's why it should be used only with the AgentCallbackLogin app. Uses the monitoring functions in chan_agent instead of Monitor application. That have to be configured in the agents.conf file. Normally the app returns 0 unless the options are passed. Also if the callerid or the agentid are not specified it'll look for n+101 priority. The options are:\n"
  70. " 'd' - make the app return -1 if there is an error condition and there is no extension n+101\n"
  71. " 'n' - don't generate the warnings when there is no callerid or the agentid is not known. It's handy if you want to have one context for agent and non-agent calls.\n";
  72. static char moh[80] = "default";
  73. #define AST_MAX_AGENT 80 /* Agent ID or Password max length */
  74. #define AST_MAX_BUF 256
  75. static int capability = -1;
  76. static unsigned int group;
  77. static int autologoff;
  78. static int wrapuptime;
  79. static int ackcall;
  80. static int usecnt =0;
  81. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  82. /* Protect the interface list (of sip_pvt's) */
  83. AST_MUTEX_DEFINE_STATIC(agentlock);
  84. static int recordagentcalls = 0;
  85. static char recordformat[AST_MAX_BUF] = "";
  86. static char recordformatext[AST_MAX_BUF] = "";
  87. static int createlink = 0;
  88. static char urlprefix[AST_MAX_BUF] = "";
  89. static char savecallsin[AST_MAX_BUF] = "";
  90. static int updatecdr = 0;
  91. static char beep[AST_MAX_BUF] = "beep";
  92. #define GETAGENTBYCALLERID "AGENTBYCALLERID"
  93. static struct agent_pvt {
  94. ast_mutex_t lock; /* Channel private lock */
  95. int dead; /* Poised for destruction? */
  96. int pending; /* Not a real agent -- just pending a match */
  97. int abouttograb; /* About to grab */
  98. int autologoff; /* Auto timeout time */
  99. int ackcall; /* ackcall */
  100. time_t loginstart; /* When agent first logged in (0 when logged off) */
  101. time_t start; /* When call started */
  102. struct timeval lastdisc; /* When last disconnected */
  103. int wrapuptime; /* Wrapup time in ms */
  104. unsigned int group; /* Group memberships */
  105. int acknowledged; /* Acknowledged */
  106. char moh[80]; /* Which music on hold */
  107. char agent[AST_MAX_AGENT]; /* Agent ID */
  108. char password[AST_MAX_AGENT]; /* Password for Agent login */
  109. char name[AST_MAX_AGENT];
  110. ast_mutex_t app_lock; /* Synchronization between owning applications */
  111. volatile pthread_t owning_app; /* Owning application thread id */
  112. volatile int app_sleep_cond; /* Sleep condition for the login app */
  113. struct ast_channel *owner; /* Agent */
  114. char loginchan[80];
  115. struct ast_channel *chan; /* Channel we use */
  116. struct agent_pvt *next; /* Agent */
  117. } *agents = NULL;
  118. #define CHECK_FORMATS(ast, p) do { \
  119. if (p->chan) {\
  120. if (ast->nativeformats != p->chan->nativeformats) { \
  121. ast_log(LOG_DEBUG, "Native formats changing from %d to %d\n", ast->nativeformats, p->chan->nativeformats); \
  122. /* Native formats changed, reset things */ \
  123. ast->nativeformats = p->chan->nativeformats; \
  124. ast_log(LOG_DEBUG, "Resetting read to %d and write to %d\n", ast->readformat, ast->writeformat);\
  125. ast_set_read_format(ast, ast->readformat); \
  126. ast_set_write_format(ast, ast->writeformat); \
  127. } \
  128. if (p->chan->readformat != ast->pvt->rawreadformat) \
  129. ast_set_read_format(p->chan, ast->pvt->rawreadformat); \
  130. if (p->chan->writeformat != ast->pvt->rawwriteformat) \
  131. ast_set_write_format(p->chan, ast->pvt->rawwriteformat); \
  132. } \
  133. } while(0)
  134. /* Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
  135. properly for a timingfd XXX This might need more work if agents were logged in as agents or other
  136. totally impractical combinations XXX */
  137. #define CLEANUP(ast, p) do { \
  138. int x; \
  139. if (p->chan) { \
  140. for (x=0;x<AST_MAX_FDS;x++) {\
  141. if (x != AST_MAX_FDS - 2) \
  142. ast->fds[x] = p->chan->fds[x]; \
  143. } \
  144. ast->fds[AST_MAX_FDS - 3] = p->chan->fds[AST_MAX_FDS - 2]; \
  145. } \
  146. } while(0)
  147. static void agent_unlink(struct agent_pvt *agent)
  148. {
  149. struct agent_pvt *p, *prev;
  150. prev = NULL;
  151. p = agents;
  152. while(p) {
  153. if (p == agent) {
  154. if (prev)
  155. prev->next = agent->next;
  156. else
  157. agents = agent->next;
  158. break;
  159. }
  160. prev = p;
  161. p = p->next;
  162. }
  163. }
  164. static struct agent_pvt *add_agent(char *agent, int pending)
  165. {
  166. char tmp[AST_MAX_BUF] = "";
  167. char *password=NULL, *name=NULL;
  168. struct agent_pvt *p, *prev;
  169. strncpy(tmp, agent, sizeof(tmp) - 1);
  170. if ((password = strchr(tmp, ','))) {
  171. *password = '\0';
  172. password++;
  173. while (*password && *password < 33) password++;
  174. }
  175. if (password && (name = strchr(password, ','))) {
  176. *name = '\0';
  177. name++;
  178. while (*name && *name < 33) name++;
  179. }
  180. prev=NULL;
  181. p = agents;
  182. while(p) {
  183. if (!pending && !strcmp(p->agent, tmp))
  184. break;
  185. prev = p;
  186. p = p->next;
  187. }
  188. if (!p) {
  189. p = malloc(sizeof(struct agent_pvt));
  190. if (p) {
  191. memset(p, 0, sizeof(struct agent_pvt));
  192. strncpy(p->agent, tmp, sizeof(p->agent) -1);
  193. ast_mutex_init(&p->lock);
  194. ast_mutex_init(&p->app_lock);
  195. p->owning_app = (pthread_t) -1;
  196. p->app_sleep_cond = 1;
  197. p->group = group;
  198. p->pending = pending;
  199. p->next = NULL;
  200. if (prev)
  201. prev->next = p;
  202. else
  203. agents = p;
  204. }
  205. }
  206. if (!p)
  207. return NULL;
  208. strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
  209. strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
  210. strncpy(p->moh, moh, sizeof(p->moh) - 1);
  211. p->ackcall = ackcall;
  212. p->autologoff = autologoff;
  213. p->wrapuptime = wrapuptime;
  214. if (pending)
  215. p->dead = 1;
  216. else
  217. p->dead = 0;
  218. return p;
  219. }
  220. static int agent_cleanup(struct agent_pvt *p)
  221. {
  222. struct ast_channel *chan = p->owner;
  223. p->owner = NULL;
  224. chan->pvt->pvt = NULL;
  225. p->app_sleep_cond = 1;
  226. /* Release ownership of the agent to other threads (presumably running the login app). */
  227. ast_mutex_unlock(&p->app_lock);
  228. if (chan)
  229. ast_channel_free(chan);
  230. if (p->dead) {
  231. ast_mutex_destroy(&p->lock);
  232. ast_mutex_destroy(&p->app_lock);
  233. free(p);
  234. }
  235. return 0;
  236. }
  237. static int check_availability(struct agent_pvt *newlyavailable, int needlock);
  238. static int agent_answer(struct ast_channel *ast)
  239. {
  240. ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
  241. return -1;
  242. }
  243. static int __agent_start_monitoring(struct ast_channel *ast, struct agent_pvt *p, int needlock)
  244. {
  245. char tmp[AST_MAX_BUF],tmp2[AST_MAX_BUF], *pointer;
  246. char filename[AST_MAX_BUF];
  247. int res = -1;
  248. if (!p)
  249. return -1;
  250. if (!ast->monitor) {
  251. snprintf(filename, sizeof(filename), "agent-%s-%s",p->agent, ast->uniqueid);
  252. /* substitute . for - */
  253. if ((pointer = strchr(filename, '.')))
  254. *pointer = '-';
  255. snprintf(tmp, sizeof(tmp), "%s%s",savecallsin ? savecallsin : "", filename);
  256. ast_monitor_start(ast, recordformat, tmp, needlock);
  257. ast_monitor_setjoinfiles(ast, 1);
  258. snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix ? urlprefix : "", filename, recordformatext);
  259. #if 0
  260. ast_verbose("name is %s, link is %s\n",tmp, tmp2);
  261. #endif
  262. if (!ast->cdr)
  263. ast->cdr = ast_cdr_alloc();
  264. ast_cdr_setuserfield(ast, tmp2);
  265. res = 0;
  266. } else
  267. ast_log(LOG_ERROR, "Recording already started on that call.\n");
  268. return res;
  269. }
  270. static int agent_start_monitoring(struct ast_channel *ast, int needlock)
  271. {
  272. return __agent_start_monitoring(ast, ast->pvt->pvt, needlock);
  273. }
  274. static struct ast_frame *agent_read(struct ast_channel *ast)
  275. {
  276. struct agent_pvt *p = ast->pvt->pvt;
  277. struct ast_frame *f = NULL;
  278. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  279. static struct ast_frame answer_frame = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  280. ast_mutex_lock(&p->lock);
  281. CHECK_FORMATS(ast, p);
  282. if (p->chan) {
  283. p->chan->exception = ast->exception;
  284. if (ast->fdno == AST_MAX_FDS - 3)
  285. p->chan->fdno = AST_MAX_FDS - 2;
  286. else
  287. p->chan->fdno = ast->fdno;
  288. f = ast_read(p->chan);
  289. } else
  290. f = &null_frame;
  291. if (!f) {
  292. /* If there's a channel, hang it up (if it's on a callback) make it NULL */
  293. if (p->chan) {
  294. /* Note that we don't hangup if it's not a callback because Asterisk will do it
  295. for us when the PBX instance that called login finishes */
  296. if (!ast_strlen_zero(p->loginchan)) {
  297. ast_hangup(p->chan);
  298. if (p->wrapuptime) {
  299. gettimeofday(&p->lastdisc, NULL);
  300. p->lastdisc.tv_usec += (p->wrapuptime % 1000) * 1000;
  301. if (p->lastdisc.tv_usec > 1000000) {
  302. p->lastdisc.tv_usec -= 1000000;
  303. p->lastdisc.tv_sec++;
  304. }
  305. p->lastdisc.tv_sec += (p->wrapuptime / 1000);
  306. }
  307. }
  308. p->chan = NULL;
  309. p->acknowledged = 0;
  310. }
  311. }
  312. if (f && (f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_ANSWER)) {
  313. /* TC */
  314. if (p->ackcall) {
  315. if (option_verbose > 2)
  316. ast_verbose(VERBOSE_PREFIX_3 "%s answered, waiting for '#' to acknowledge\n", p->chan->name);
  317. /* Don't pass answer along */
  318. ast_frfree(f);
  319. f = &null_frame;
  320. }
  321. else {
  322. p->acknowledged = 1;
  323. f = &answer_frame;
  324. }
  325. }
  326. if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '#')) {
  327. if (!p->acknowledged) {
  328. if (option_verbose > 2)
  329. ast_verbose(VERBOSE_PREFIX_3 "%s acknowledged\n", p->chan->name);
  330. p->acknowledged = 1;
  331. ast_frfree(f);
  332. f = &answer_frame;
  333. }
  334. }
  335. if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
  336. /* * terminates call */
  337. ast_frfree(f);
  338. f = NULL;
  339. }
  340. if (f && (f->frametype == AST_FRAME_VOICE) && !p->acknowledged) {
  341. /* Don't pass along agent audio until call is acknowledged */
  342. ast_frfree(f);
  343. f = &null_frame;
  344. }
  345. CLEANUP(ast,p);
  346. ast_mutex_unlock(&p->lock);
  347. if (recordagentcalls && f == &answer_frame)
  348. agent_start_monitoring(ast,0);
  349. return f;
  350. }
  351. static int agent_sendhtml(struct ast_channel *ast, int subclass, char *data, int datalen)
  352. {
  353. struct agent_pvt *p = ast->pvt->pvt;
  354. int res = -1;
  355. ast_mutex_lock(&p->lock);
  356. if (p->chan)
  357. res = ast_channel_sendhtml(p->chan, subclass, data, datalen);
  358. ast_mutex_unlock(&p->lock);
  359. return res;
  360. }
  361. static int agent_write(struct ast_channel *ast, struct ast_frame *f)
  362. {
  363. struct agent_pvt *p = ast->pvt->pvt;
  364. int res = -1;
  365. CHECK_FORMATS(ast, p);
  366. ast_mutex_lock(&p->lock);
  367. if (p->chan) {
  368. if ((f->frametype != AST_FRAME_VOICE) ||
  369. (f->subclass == p->chan->writeformat)) {
  370. res = ast_write(p->chan, f);
  371. } else {
  372. ast_log(LOG_DEBUG, "Dropping one incompatible voice frame on '%s' to '%s'\n", ast->name, p->chan->name);
  373. res = 0;
  374. }
  375. } else
  376. res = 0;
  377. CLEANUP(ast, p);
  378. ast_mutex_unlock(&p->lock);
  379. return res;
  380. }
  381. static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  382. {
  383. struct agent_pvt *p = newchan->pvt->pvt;
  384. ast_mutex_lock(&p->lock);
  385. if (p->owner != oldchan) {
  386. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
  387. ast_mutex_unlock(&p->lock);
  388. return -1;
  389. }
  390. p->owner = newchan;
  391. ast_mutex_unlock(&p->lock);
  392. return 0;
  393. }
  394. static int agent_indicate(struct ast_channel *ast, int condition)
  395. {
  396. struct agent_pvt *p = ast->pvt->pvt;
  397. int res = -1;
  398. ast_mutex_lock(&p->lock);
  399. if (p->chan)
  400. res = ast_indicate(p->chan, condition);
  401. else
  402. res = 0;
  403. ast_mutex_unlock(&p->lock);
  404. return res;
  405. }
  406. static int agent_digit(struct ast_channel *ast, char digit)
  407. {
  408. struct agent_pvt *p = ast->pvt->pvt;
  409. int res = -1;
  410. ast_mutex_lock(&p->lock);
  411. if (p->chan)
  412. res = p->chan->pvt->send_digit(p->chan, digit);
  413. else
  414. res = 0;
  415. ast_mutex_unlock(&p->lock);
  416. return res;
  417. }
  418. static int agent_call(struct ast_channel *ast, char *dest, int timeout)
  419. {
  420. struct agent_pvt *p = ast->pvt->pvt;
  421. int res = -1;
  422. ast_mutex_lock(&p->lock);
  423. p->acknowledged = 0;
  424. if (!p->chan) {
  425. if (p->pending) {
  426. ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
  427. ast_setstate(ast, AST_STATE_DIALING);
  428. res = 0;
  429. } else {
  430. ast_log(LOG_NOTICE, "Whoa, they hung up between alloc and call... what are the odds of that?\n");
  431. res = -1;
  432. }
  433. ast_mutex_unlock(&p->lock);
  434. return res;
  435. } else if (!ast_strlen_zero(p->loginchan)) {
  436. time(&p->start);
  437. /* Call on this agent */
  438. if (option_verbose > 2)
  439. ast_verbose(VERBOSE_PREFIX_3 "outgoing agentcall, to agent '%s', on '%s'\n", p->agent, p->chan->name);
  440. if (p->chan->callerid)
  441. free(p->chan->callerid);
  442. if (ast->callerid)
  443. p->chan->callerid = strdup(ast->callerid);
  444. else
  445. p->chan->callerid = NULL;
  446. res = ast_call(p->chan, p->loginchan, 0);
  447. CLEANUP(ast,p);
  448. ast_mutex_unlock(&p->lock);
  449. return res;
  450. }
  451. ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
  452. ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
  453. res = ast_streamfile(p->chan, beep, p->chan->language);
  454. ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
  455. if (!res) {
  456. res = ast_waitstream(p->chan, "");
  457. ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
  458. }
  459. if (!res) {
  460. res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
  461. ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
  462. if (res)
  463. ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
  464. } else {
  465. // Agent hung-up
  466. p->chan = NULL;
  467. }
  468. if (!res) {
  469. ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
  470. ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
  471. if (res)
  472. ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
  473. }
  474. if( !res )
  475. {
  476. /* Call is immediately up, or might need ack */
  477. if (p->ackcall > 1)
  478. ast_setstate(ast, AST_STATE_RINGING);
  479. else {
  480. ast_setstate(ast, AST_STATE_UP);
  481. if (recordagentcalls)
  482. agent_start_monitoring(ast,0);
  483. p->acknowledged = 1;
  484. }
  485. res = 0;
  486. }
  487. CLEANUP(ast,p);
  488. ast_mutex_unlock(&p->lock);
  489. return res;
  490. }
  491. static int agent_hangup(struct ast_channel *ast)
  492. {
  493. struct agent_pvt *p = ast->pvt->pvt;
  494. int howlong = 0;
  495. ast_mutex_lock(&p->lock);
  496. p->owner = NULL;
  497. ast->pvt->pvt = NULL;
  498. p->app_sleep_cond = 1;
  499. p->acknowledged = 0;
  500. /* if they really are hung up then set start to 0 so the test
  501. * later if we're called on an already downed channel
  502. * doesn't cause an agent to be logged out like when
  503. * agent_request() is followed immediately by agent_hangup()
  504. * as in apps/app_chanisavail.c:chanavail_exec()
  505. */
  506. ast_mutex_lock(&usecnt_lock);
  507. usecnt--;
  508. ast_mutex_unlock(&usecnt_lock);
  509. ast_log(LOG_DEBUG, "Hangup called for state %s\n", ast_state2str(ast->_state));
  510. if (p->start && (ast->_state != AST_STATE_UP)) {
  511. howlong = time(NULL) - p->start;
  512. p->start = 0;
  513. } else if (ast->_state == AST_STATE_RESERVED) {
  514. howlong = 0;
  515. } else
  516. p->start = 0;
  517. if (p->chan) {
  518. /* If they're dead, go ahead and hang up on the agent now */
  519. if (!ast_strlen_zero(p->loginchan)) {
  520. /* Store last disconnect time */
  521. if (p->wrapuptime) {
  522. gettimeofday(&p->lastdisc, NULL);
  523. p->lastdisc.tv_usec += (p->wrapuptime % 1000) * 1000;
  524. if (p->lastdisc.tv_usec >= 1000000) {
  525. p->lastdisc.tv_usec -= 1000000;
  526. p->lastdisc.tv_sec++;
  527. }
  528. p->lastdisc.tv_sec += (p->wrapuptime / 1000);
  529. } else
  530. memset(&p->lastdisc, 0, sizeof(p->lastdisc));
  531. if (p->chan) {
  532. /* Recognize the hangup and pass it along immediately */
  533. ast_hangup(p->chan);
  534. p->chan = NULL;
  535. }
  536. ast_log(LOG_DEBUG, "Hungup, howlong is %d, autologoff is %d\n", howlong, p->autologoff);
  537. if (howlong && p->autologoff && (howlong > p->autologoff)) {
  538. char agent[AST_MAX_AGENT] = "";
  539. long logintime = time(NULL) - p->loginstart;
  540. p->loginstart = 0;
  541. ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
  542. manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
  543. "Agent: %s\r\n"
  544. "Loginchan: %s\r\n"
  545. "Logintime: %ld\r\n"
  546. "Reason: Autologoff\r\n"
  547. "Uniqueid: %s\r\n",
  548. p->agent, p->loginchan, logintime, ast->uniqueid);
  549. snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
  550. ast_queue_log("NONE", ast->uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", p->loginchan, logintime, "Autologoff");
  551. p->loginchan[0] = '\0';
  552. }
  553. } else if (p->dead) {
  554. ast_mutex_lock(&p->chan->lock);
  555. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  556. ast_mutex_unlock(&p->chan->lock);
  557. } else {
  558. ast_mutex_lock(&p->chan->lock);
  559. ast_moh_start(p->chan, p->moh);
  560. ast_mutex_unlock(&p->chan->lock);
  561. }
  562. }
  563. #if 0
  564. ast_mutex_unlock(&p->lock);
  565. /* Release ownership of the agent to other threads (presumably running the login app). */
  566. ast_mutex_unlock(&p->app_lock);
  567. } else if (p->dead) {
  568. /* Go ahead and lose it */
  569. ast_mutex_unlock(&p->lock);
  570. /* Release ownership of the agent to other threads (presumably running the login app). */
  571. ast_mutex_unlock(&p->app_lock);
  572. } else {
  573. ast_mutex_unlock(&p->lock);
  574. /* Release ownership of the agent to other threads (presumably running the login app). */
  575. ast_mutex_unlock(&p->app_lock);
  576. }
  577. #endif
  578. ast_mutex_unlock(&p->lock);
  579. if (p->pending) {
  580. ast_mutex_lock(&agentlock);
  581. agent_unlink(p);
  582. ast_mutex_unlock(&agentlock);
  583. }
  584. if (p->abouttograb) {
  585. /* Let the "about to grab" thread know this isn't valid anymore, and let it
  586. kill it later */
  587. p->abouttograb = 0;
  588. } else if (p->dead) {
  589. ast_mutex_destroy(&p->lock);
  590. ast_mutex_destroy(&p->app_lock);
  591. free(p);
  592. } else {
  593. if (p->chan) {
  594. /* Not dead -- check availability now */
  595. ast_mutex_lock(&p->lock);
  596. /* Store last disconnect time */
  597. gettimeofday(&p->lastdisc, NULL);
  598. ast_mutex_unlock(&p->lock);
  599. }
  600. /* Release ownership of the agent to other threads (presumably running the login app). */
  601. ast_mutex_unlock(&p->app_lock);
  602. }
  603. return 0;
  604. }
  605. static int agent_cont_sleep( void *data )
  606. {
  607. struct agent_pvt *p;
  608. struct timeval tv;
  609. int res;
  610. p = (struct agent_pvt *)data;
  611. ast_mutex_lock(&p->lock);
  612. res = p->app_sleep_cond;
  613. if (p->lastdisc.tv_sec) {
  614. gettimeofday(&tv, NULL);
  615. if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
  616. (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime)
  617. res = 1;
  618. }
  619. ast_mutex_unlock(&p->lock);
  620. #if 0
  621. if( !res )
  622. ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
  623. #endif
  624. return res;
  625. }
  626. static int agent_ack_sleep( void *data )
  627. {
  628. struct agent_pvt *p;
  629. int res=0;
  630. int to = 1000;
  631. struct ast_frame *f;
  632. /* Wait a second and look for something */
  633. p = (struct agent_pvt *)data;
  634. if (p->chan) {
  635. for(;;) {
  636. to = ast_waitfor(p->chan, to);
  637. if (to < 0) {
  638. res = -1;
  639. break;
  640. }
  641. if (!to) {
  642. res = 0;
  643. break;
  644. }
  645. f = ast_read(p->chan);
  646. if (!f) {
  647. res = -1;
  648. break;
  649. }
  650. if (f->frametype == AST_FRAME_DTMF)
  651. res = f->subclass;
  652. else
  653. res = 0;
  654. ast_frfree(f);
  655. ast_mutex_lock(&p->lock);
  656. if (!p->app_sleep_cond) {
  657. ast_mutex_unlock(&p->lock);
  658. res = 0;
  659. break;
  660. } else if (res == '#') {
  661. ast_mutex_unlock(&p->lock);
  662. res = 1;
  663. break;
  664. }
  665. ast_mutex_unlock(&p->lock);
  666. res = 0;
  667. }
  668. } else
  669. res = -1;
  670. return res;
  671. }
  672. static struct ast_channel *agent_new(struct agent_pvt *p, int state)
  673. {
  674. struct ast_channel *tmp;
  675. struct ast_frame null_frame = { AST_FRAME_NULL };
  676. #if 0
  677. if (!p->chan) {
  678. ast_log(LOG_WARNING, "No channel? :(\n");
  679. return NULL;
  680. }
  681. #endif
  682. tmp = ast_channel_alloc(0);
  683. if (tmp) {
  684. if (p->chan) {
  685. tmp->nativeformats = p->chan->nativeformats;
  686. tmp->writeformat = p->chan->writeformat;
  687. tmp->pvt->rawwriteformat = p->chan->writeformat;
  688. tmp->readformat = p->chan->readformat;
  689. tmp->pvt->rawreadformat = p->chan->readformat;
  690. strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
  691. strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
  692. strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
  693. } else {
  694. tmp->nativeformats = AST_FORMAT_SLINEAR;
  695. tmp->writeformat = AST_FORMAT_SLINEAR;
  696. tmp->pvt->rawwriteformat = AST_FORMAT_SLINEAR;
  697. tmp->readformat = AST_FORMAT_SLINEAR;
  698. tmp->pvt->rawreadformat = AST_FORMAT_SLINEAR;
  699. }
  700. if (p->pending)
  701. snprintf(tmp->name, sizeof(tmp->name), "Agent/P%s-%d", p->agent, rand() & 0xffff);
  702. else
  703. snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
  704. tmp->type = type;
  705. ast_setstate(tmp, state);
  706. tmp->pvt->pvt = p;
  707. tmp->pvt->send_digit = agent_digit;
  708. tmp->pvt->call = agent_call;
  709. tmp->pvt->hangup = agent_hangup;
  710. tmp->pvt->answer = agent_answer;
  711. tmp->pvt->read = agent_read;
  712. tmp->pvt->write = agent_write;
  713. tmp->pvt->send_html = agent_sendhtml;
  714. tmp->pvt->exception = agent_read;
  715. tmp->pvt->indicate = agent_indicate;
  716. tmp->pvt->fixup = agent_fixup;
  717. p->owner = tmp;
  718. ast_mutex_lock(&usecnt_lock);
  719. usecnt++;
  720. ast_mutex_unlock(&usecnt_lock);
  721. ast_update_use_count();
  722. tmp->priority = 1;
  723. /* Wake up and wait for other applications (by definition the login app)
  724. * to release this channel). Takes ownership of the agent channel
  725. * to this thread only.
  726. * For signalling the other thread, ast_queue_frame is used until we
  727. * can safely use signals for this purpose. The pselect() needs to be
  728. * implemented in the kernel for this.
  729. */
  730. p->app_sleep_cond = 0;
  731. if( ast_mutex_trylock(&p->app_lock) )
  732. {
  733. if (p->chan) {
  734. ast_queue_frame(p->chan, &null_frame);
  735. ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
  736. ast_mutex_lock(&p->app_lock);
  737. ast_mutex_lock(&p->lock);
  738. }
  739. if( !p->chan )
  740. {
  741. ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
  742. p->owner = NULL;
  743. tmp->pvt->pvt = NULL;
  744. p->app_sleep_cond = 1;
  745. ast_channel_free( tmp );
  746. ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
  747. ast_mutex_unlock(&p->app_lock);
  748. return NULL;
  749. }
  750. }
  751. p->owning_app = pthread_self();
  752. /* After the above step, there should not be any blockers. */
  753. if (p->chan) {
  754. if (p->chan->blocking) {
  755. ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
  756. CRASH;
  757. }
  758. ast_moh_stop(p->chan);
  759. }
  760. } else
  761. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  762. return tmp;
  763. }
  764. static int read_agent_config(void)
  765. {
  766. struct ast_config *cfg;
  767. struct ast_variable *v;
  768. struct agent_pvt *p, *pl, *pn;
  769. group = 0;
  770. autologoff = 0;
  771. wrapuptime = 0;
  772. ackcall = 1;
  773. cfg = ast_load(config);
  774. if (!cfg) {
  775. ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
  776. return 0;
  777. }
  778. ast_mutex_lock(&agentlock);
  779. p = agents;
  780. while(p) {
  781. p->dead = 1;
  782. p = p->next;
  783. }
  784. strncpy(moh, "default", sizeof(moh) - 1);
  785. /* set the default recording values */
  786. recordagentcalls = 0;
  787. createlink = 0;
  788. strncpy(recordformat, "wav", sizeof(recordformat) - 1);
  789. strncpy(recordformatext, "wav", sizeof(recordformatext) - 1);
  790. urlprefix[0] = '\0';
  791. savecallsin[0] = '\0';
  792. v = ast_variable_browse(cfg, "agents");
  793. while(v) {
  794. /* Create the interface list */
  795. if (!strcasecmp(v->name, "agent")) {
  796. add_agent(v->value, 0);
  797. } else if (!strcasecmp(v->name, "group")) {
  798. group = ast_get_group(v->value);
  799. } else if (!strcasecmp(v->name, "autologoff")) {
  800. autologoff = atoi(v->value);
  801. if (autologoff < 0)
  802. autologoff = 0;
  803. } else if (!strcasecmp(v->name, "ackcall")) {
  804. if (!strcasecmp(v->value, "always"))
  805. ackcall = 2;
  806. else if (ast_true(v->value))
  807. ackcall = 1;
  808. else
  809. ackcall = 0;
  810. } else if (!strcasecmp(v->name, "wrapuptime")) {
  811. wrapuptime = atoi(v->value);
  812. if (wrapuptime < 0)
  813. wrapuptime = 0;
  814. } else if (!strcasecmp(v->name, "musiconhold")) {
  815. strncpy(moh, v->value, sizeof(moh) - 1);
  816. } else if (!strcasecmp(v->name, "updatecdr")) {
  817. updatecdr = ast_true(v->value);
  818. } else if (!strcasecmp(v->name, "recordagentcalls")) {
  819. recordagentcalls = ast_true(v->value);
  820. } else if (!strcasecmp(v->name, "createlink")) {
  821. createlink = ast_true(v->value);
  822. } else if (!strcasecmp(v->name, "recordformat")) {
  823. strncpy(recordformat, v->value, sizeof(recordformat) - 1);
  824. if (!strcasecmp(v->value, "wav49"))
  825. strncpy(recordformatext, "WAV", sizeof(recordformatext) - 1);
  826. else
  827. strncpy(recordformatext, v->value, sizeof(recordformatext) - 1);
  828. } else if (!strcasecmp(v->name, "urlprefix")) {
  829. strncpy(urlprefix, v->value, sizeof(urlprefix) - 2);
  830. if (urlprefix[strlen(urlprefix) - 1] != '/')
  831. strncat(urlprefix, "/", sizeof(urlprefix) - strlen(urlprefix) - 1);
  832. } else if (!strcasecmp(v->name, "savecallsin")) {
  833. if (v->value[0] == '/')
  834. strncpy(savecallsin, v->value, sizeof(savecallsin) - 2);
  835. else
  836. snprintf(savecallsin, sizeof(savecallsin) - 2, "/%s", v->value);
  837. if (savecallsin[strlen(savecallsin) - 1] != '/')
  838. strncat(savecallsin, "/", sizeof(savecallsin) - strlen(savecallsin) - 1);
  839. } else if (!strcasecmp(v->name, "custom_beep")) {
  840. strncpy(beep, v->value, sizeof(beep) - 1);
  841. }
  842. v = v->next;
  843. }
  844. p = agents;
  845. pl = NULL;
  846. while(p) {
  847. pn = p->next;
  848. if (p->dead) {
  849. /* Unlink */
  850. if (pl)
  851. pl->next = p->next;
  852. else
  853. agents = p->next;
  854. /* Destroy if appropriate */
  855. if (!p->owner) {
  856. if (!p->chan) {
  857. ast_mutex_destroy(&p->lock);
  858. ast_mutex_destroy(&p->app_lock);
  859. free(p);
  860. } else {
  861. /* Cause them to hang up */
  862. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  863. }
  864. }
  865. } else
  866. pl = p;
  867. p = pn;
  868. }
  869. ast_mutex_unlock(&agentlock);
  870. ast_destroy(cfg);
  871. return 0;
  872. }
  873. static int check_availability(struct agent_pvt *newlyavailable, int needlock)
  874. {
  875. struct ast_channel *chan=NULL, *parent=NULL;
  876. struct agent_pvt *p;
  877. int res;
  878. ast_log(LOG_DEBUG, "Checking availability of '%s'\n", newlyavailable->agent);
  879. if (needlock)
  880. ast_mutex_lock(&agentlock);
  881. p = agents;
  882. while(p) {
  883. if (p == newlyavailable) {
  884. p = p->next;
  885. continue;
  886. }
  887. ast_mutex_lock(&p->lock);
  888. if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
  889. ast_log(LOG_DEBUG, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
  890. /* We found a pending call, time to merge */
  891. chan = agent_new(newlyavailable, AST_STATE_DOWN);
  892. parent = p->owner;
  893. p->abouttograb = 1;
  894. ast_mutex_unlock(&p->lock);
  895. break;
  896. }
  897. ast_mutex_unlock(&p->lock);
  898. p = p->next;
  899. }
  900. if (needlock)
  901. ast_mutex_unlock(&agentlock);
  902. if (parent && chan) {
  903. if (newlyavailable->ackcall > 1) {
  904. /* Don't do beep here */
  905. res = 0;
  906. } else {
  907. ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
  908. res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
  909. ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
  910. if (!res) {
  911. res = ast_waitstream(newlyavailable->chan, "");
  912. ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
  913. }
  914. }
  915. if (!res) {
  916. /* Note -- parent may have disappeared */
  917. if (p->abouttograb) {
  918. newlyavailable->acknowledged = 1;
  919. ast_setstate(parent, AST_STATE_UP);
  920. ast_setstate(chan, AST_STATE_UP);
  921. strncpy(parent->context, chan->context, sizeof(parent->context) - 1);
  922. /* Go ahead and mark the channel as a zombie so that masquerade will
  923. destroy it for us, and we need not call ast_hangup */
  924. ast_mutex_lock(&parent->lock);
  925. chan->zombie = 1;
  926. ast_channel_masquerade(parent, chan);
  927. ast_mutex_unlock(&parent->lock);
  928. p->abouttograb = 0;
  929. } else {
  930. ast_log(LOG_DEBUG, "Sneaky, parent disappeared in the mean time...\n");
  931. agent_cleanup(newlyavailable);
  932. }
  933. } else {
  934. ast_log(LOG_DEBUG, "Ugh... Agent hung up at exactly the wrong time\n");
  935. agent_cleanup(newlyavailable);
  936. }
  937. }
  938. return 0;
  939. }
  940. static int check_beep(struct agent_pvt *newlyavailable, int needlock)
  941. {
  942. struct agent_pvt *p;
  943. int res=0;
  944. ast_log(LOG_DEBUG, "Checking beep availability of '%s'\n", newlyavailable->agent);
  945. if (needlock)
  946. ast_mutex_lock(&agentlock);
  947. p = agents;
  948. while(p) {
  949. if (p == newlyavailable) {
  950. p = p->next;
  951. continue;
  952. }
  953. ast_mutex_lock(&p->lock);
  954. if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
  955. ast_log(LOG_DEBUG, "Call '%s' looks like a would-be winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
  956. ast_mutex_unlock(&p->lock);
  957. break;
  958. }
  959. ast_mutex_unlock(&p->lock);
  960. p = p->next;
  961. }
  962. if (needlock)
  963. ast_mutex_unlock(&agentlock);
  964. if (p) {
  965. ast_mutex_unlock(&newlyavailable->lock);
  966. ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
  967. res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
  968. ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
  969. if (!res) {
  970. res = ast_waitstream(newlyavailable->chan, "");
  971. ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
  972. }
  973. ast_mutex_lock(&newlyavailable->lock);
  974. }
  975. return res;
  976. }
  977. static struct ast_channel *agent_request(char *type, int format, void *data)
  978. {
  979. struct agent_pvt *p;
  980. struct ast_channel *chan = NULL;
  981. char *s;
  982. unsigned int groupmatch;
  983. int waitforagent=0;
  984. int hasagent = 0;
  985. struct timeval tv;
  986. s = data;
  987. if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
  988. groupmatch = (1 << groupmatch);
  989. } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupmatch) == 1)) {
  990. groupmatch = (1 << groupmatch);
  991. waitforagent = 1;
  992. } else {
  993. groupmatch = 0;
  994. }
  995. /* Check actual logged in agents first */
  996. ast_mutex_lock(&agentlock);
  997. p = agents;
  998. while(p) {
  999. ast_mutex_lock(&p->lock);
  1000. if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent)) &&
  1001. ast_strlen_zero(p->loginchan)) {
  1002. if (p->chan)
  1003. hasagent++;
  1004. if (!p->lastdisc.tv_sec) {
  1005. /* Agent must be registered, but not have any active call, and not be in a waiting state */
  1006. if (!p->owner && p->chan) {
  1007. /* Fixed agent */
  1008. chan = agent_new(p, AST_STATE_DOWN);
  1009. }
  1010. if (chan) {
  1011. ast_mutex_unlock(&p->lock);
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. ast_mutex_unlock(&p->lock);
  1017. p = p->next;
  1018. }
  1019. if (!p) {
  1020. p = agents;
  1021. while(p) {
  1022. ast_mutex_lock(&p->lock);
  1023. if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
  1024. if (p->chan || !ast_strlen_zero(p->loginchan))
  1025. hasagent++;
  1026. gettimeofday(&tv, NULL);
  1027. #if 0
  1028. ast_log(LOG_NOTICE, "Time now: %ld, Time of lastdisc: %ld\n", tv.tv_sec, p->lastdisc.tv_sec);
  1029. #endif
  1030. if (!p->lastdisc.tv_sec || (tv.tv_sec > p->lastdisc.tv_sec)) {
  1031. memset(&p->lastdisc, 0, sizeof(p->lastdisc));
  1032. /* Agent must be registered, but not have any active call, and not be in a waiting state */
  1033. if (!p->owner && p->chan) {
  1034. /* Could still get a fixed agent */
  1035. chan = agent_new(p, AST_STATE_DOWN);
  1036. } else if (!p->owner && !ast_strlen_zero(p->loginchan)) {
  1037. /* Adjustable agent */
  1038. p->chan = ast_request("Local", format, p->loginchan);
  1039. if (p->chan)
  1040. chan = agent_new(p, AST_STATE_DOWN);
  1041. }
  1042. if (chan) {
  1043. ast_mutex_unlock(&p->lock);
  1044. break;
  1045. }
  1046. }
  1047. }
  1048. ast_mutex_unlock(&p->lock);
  1049. p = p->next;
  1050. }
  1051. }
  1052. if (!chan && waitforagent) {
  1053. /* No agent available -- but we're requesting to wait for one.
  1054. Allocate a place holder */
  1055. if (hasagent) {
  1056. ast_log(LOG_DEBUG, "Creating place holder for '%s'\n", s);
  1057. p = add_agent(data, 1);
  1058. p->group = groupmatch;
  1059. chan = agent_new(p, AST_STATE_DOWN);
  1060. if (!chan) {
  1061. ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
  1062. }
  1063. } else
  1064. ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s);
  1065. }
  1066. ast_mutex_unlock(&agentlock);
  1067. return chan;
  1068. }
  1069. static int powerof(unsigned int v)
  1070. {
  1071. int x;
  1072. for (x=0;x<32;x++) {
  1073. if (v & (1 << x)) return x;
  1074. }
  1075. return 0;
  1076. }
  1077. static int agents_show(int fd, int argc, char **argv)
  1078. {
  1079. struct agent_pvt *p;
  1080. char username[AST_MAX_BUF];
  1081. char location[AST_MAX_BUF] = "";
  1082. char talkingto[AST_MAX_BUF] = "";
  1083. char moh[AST_MAX_BUF];
  1084. if (argc != 2)
  1085. return RESULT_SHOWUSAGE;
  1086. ast_mutex_lock(&agentlock);
  1087. p = agents;
  1088. while(p) {
  1089. ast_mutex_lock(&p->lock);
  1090. if (p->pending) {
  1091. if (p->group)
  1092. ast_cli(fd, "-- Pending call to group %d\n", powerof(p->group));
  1093. else
  1094. ast_cli(fd, "-- Pending call to agent %s\n", p->agent);
  1095. } else {
  1096. if (!ast_strlen_zero(p->name))
  1097. snprintf(username, sizeof(username), "(%s) ", p->name);
  1098. else
  1099. username[0] = '\0';
  1100. if (p->chan) {
  1101. snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
  1102. if (p->owner && p->owner->bridge) {
  1103. snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
  1104. } else {
  1105. strncpy(talkingto, " is idle", sizeof(talkingto) - 1);
  1106. }
  1107. } else if (!ast_strlen_zero(p->loginchan)) {
  1108. snprintf(location, sizeof(location) - 20, "available at '%s'", p->loginchan);
  1109. talkingto[0] = '\0';
  1110. if (p->acknowledged)
  1111. strncat(location, " (Confirmed)", sizeof(location) - strlen(location) - 1);
  1112. } else {
  1113. strncpy(location, "not logged in", sizeof(location) - 1);
  1114. talkingto[0] = '\0';
  1115. }
  1116. if (!ast_strlen_zero(p->moh))
  1117. snprintf(moh, sizeof(moh), " (musiconhold is '%s')", p->moh);
  1118. ast_cli(fd, "%-12.12s %s%s%s%s\n", p->agent,
  1119. username, location, talkingto, moh);
  1120. }
  1121. ast_mutex_unlock(&p->lock);
  1122. p = p->next;
  1123. }
  1124. ast_mutex_unlock(&agentlock);
  1125. return RESULT_SUCCESS;
  1126. }
  1127. static char show_agents_usage[] =
  1128. "Usage: show agents\n"
  1129. " Provides summary information on agents.\n";
  1130. static struct ast_cli_entry cli_show_agents = {
  1131. { "show", "agents", NULL }, agents_show,
  1132. "Show status of agents", show_agents_usage, NULL };
  1133. STANDARD_LOCAL_USER;
  1134. LOCAL_USER_DECL;
  1135. static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
  1136. {
  1137. int res=0;
  1138. int tries = 0;
  1139. struct agent_pvt *p;
  1140. struct localuser *u;
  1141. struct timeval tv;
  1142. char user[AST_MAX_AGENT] = "";
  1143. char pass[AST_MAX_AGENT];
  1144. char agent[AST_MAX_AGENT] = "";
  1145. char xpass[AST_MAX_AGENT] = "";
  1146. char *errmsg;
  1147. char info[512];
  1148. char *opt_user = NULL;
  1149. char *options = NULL;
  1150. char *context = NULL;
  1151. char *exten = NULL;
  1152. int play_announcement;
  1153. char *filename = "agent-loginok";
  1154. LOCAL_USER_ADD(u);
  1155. /* Parse the arguments XXX Check for failure XXX */
  1156. strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
  1157. opt_user = info;
  1158. if( opt_user ) {
  1159. options = strchr(opt_user, '|');
  1160. if (options) {
  1161. *options = '\0';
  1162. options++;
  1163. if (callbackmode) {
  1164. context = strchr(options, '@');
  1165. if (context) {
  1166. *context = '\0';
  1167. context++;
  1168. }
  1169. exten = options;
  1170. while(*exten && ((*exten < '0') || (*exten > '9'))) exten++;
  1171. if (!*exten)
  1172. exten = NULL;
  1173. }
  1174. }
  1175. }
  1176. if (chan->_state != AST_STATE_UP)
  1177. res = ast_answer(chan);
  1178. if (!res) {
  1179. if( opt_user && !ast_strlen_zero(opt_user))
  1180. strncpy( user, opt_user, AST_MAX_AGENT - 1);
  1181. else
  1182. res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
  1183. }
  1184. while (!res && (tries < 3)) {
  1185. /* Check for password */
  1186. ast_mutex_lock(&agentlock);
  1187. p = agents;
  1188. while(p) {
  1189. if (!strcmp(p->agent, user) && !p->pending)
  1190. strncpy(xpass, p->password, sizeof(xpass) - 1);
  1191. p = p->next;
  1192. }
  1193. ast_mutex_unlock(&agentlock);
  1194. if (!res) {
  1195. if (!ast_strlen_zero(xpass))
  1196. res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
  1197. else
  1198. pass[0] = '\0';
  1199. }
  1200. errmsg = "agent-incorrect";
  1201. #if 0
  1202. ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
  1203. #endif
  1204. /* Check again for accuracy */
  1205. ast_mutex_lock(&agentlock);
  1206. p = agents;
  1207. while(p) {
  1208. ast_mutex_lock(&p->lock);
  1209. if (!strcmp(p->agent, user) &&
  1210. !strcmp(p->password, pass) && !p->pending) {
  1211. if (!p->chan) {
  1212. char last_loginchan[80] = "";
  1213. long logintime;
  1214. snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
  1215. if (callbackmode) {
  1216. char tmpchan[AST_MAX_BUF] = "";
  1217. int pos = 0;
  1218. /* Retrieve login chan */
  1219. for (;;) {
  1220. if (exten) {
  1221. strncpy(tmpchan, exten, sizeof(tmpchan) - 1);
  1222. res = 0;
  1223. } else
  1224. res = ast_app_getdata(chan, "agent-newlocation", tmpchan+pos, sizeof(tmpchan) - 2, 0);
  1225. if (ast_strlen_zero(tmpchan) || ast_exists_extension(chan, context && !ast_strlen_zero(context) ? context : "default", tmpchan,
  1226. 1, NULL))
  1227. break;
  1228. if (exten) {
  1229. ast_log(LOG_WARNING, "Extension '%s' is not valid for automatic login of agent '%s'\n", exten, p->agent);
  1230. exten = NULL;
  1231. pos = 0;
  1232. } else {
  1233. res = ast_streamfile(chan, "invalid", chan->language);
  1234. if (!res)
  1235. res = ast_waitstream(chan, AST_DIGIT_ANY);
  1236. if (res > 0) {
  1237. tmpchan[0] = res;
  1238. tmpchan[1] = '\0';
  1239. pos = 1;
  1240. } else {
  1241. tmpchan[0] = '\0';
  1242. pos = 0;
  1243. }
  1244. }
  1245. }
  1246. if (!res) {
  1247. if (context && !ast_strlen_zero(context) && !ast_strlen_zero(tmpchan))
  1248. snprintf(p->loginchan, sizeof(p->loginchan), "%s@%s", tmpchan, context);
  1249. else {
  1250. strncpy(last_loginchan, p->loginchan, sizeof(last_loginchan) - 1);
  1251. strncpy(p->loginchan, tmpchan, sizeof(p->loginchan) - 1);
  1252. }
  1253. if (ast_strlen_zero(p->loginchan))
  1254. filename = "agent-loggedoff";
  1255. p->acknowledged = 0;
  1256. /* store/clear the global variable that stores agentid based on the callerid */
  1257. if (chan->callerid) {
  1258. char agentvar[AST_MAX_BUF];
  1259. snprintf(agentvar, sizeof(agentvar), "%s_%s",GETAGENTBYCALLERID, chan->callerid);
  1260. if (ast_strlen_zero(p->loginchan))
  1261. pbx_builtin_setvar_helper(NULL, agentvar, NULL);
  1262. else
  1263. pbx_builtin_setvar_helper(NULL, agentvar, p->agent);
  1264. }
  1265. if(updatecdr && chan->cdr)
  1266. snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
  1267. }
  1268. } else {
  1269. p->loginchan[0] = '\0';
  1270. p->acknowledged = 0;
  1271. }
  1272. play_announcement = 1;
  1273. if( options )
  1274. if( strchr( options, 's' ) )
  1275. play_announcement = 0;
  1276. ast_mutex_unlock(&p->lock);
  1277. ast_mutex_unlock(&agentlock);
  1278. if( !res && play_announcement )
  1279. res = ast_streamfile(chan, filename, chan->language);
  1280. if (!res)
  1281. ast_waitstream(chan, "");
  1282. ast_mutex_lock(&agentlock);
  1283. ast_mutex_lock(&p->lock);
  1284. if (!res) {
  1285. res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
  1286. if (res)
  1287. ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
  1288. }
  1289. if (!res) {
  1290. ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
  1291. if (res)
  1292. ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
  1293. }
  1294. /* Check once more just in case */
  1295. if (p->chan)
  1296. res = -1;
  1297. if (callbackmode && !res) {
  1298. /* Just say goodbye and be done with it */
  1299. if (!ast_strlen_zero(p->loginchan)) {
  1300. if (p->loginstart == 0)
  1301. time(&p->loginstart);
  1302. manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogin",
  1303. "Agent: %s\r\n"
  1304. "Loginchan: %s\r\n"
  1305. "Uniqueid: %s\r\n",
  1306. p->agent, p->loginchan, chan->uniqueid);
  1307. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTCALLBACKLOGIN", "%s", p->loginchan);
  1308. if (option_verbose > 2)
  1309. ast_verbose(VERBOSE_PREFIX_3 "Callback Agent '%s' logged in on %s\n", p->agent, p->loginchan);
  1310. } else {
  1311. logintime = time(NULL) - p->loginstart;
  1312. p->loginstart = 0;
  1313. manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
  1314. "Agent: %s\r\n"
  1315. "Loginchan: %s\r\n"
  1316. "Logintime: %ld\r\n"
  1317. "Uniqueid: %s\r\n",
  1318. p->agent, last_loginchan, logintime, chan->uniqueid);
  1319. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|", last_loginchan, logintime);
  1320. if (option_verbose > 2)
  1321. ast_verbose(VERBOSE_PREFIX_3 "Callback Agent '%s' logged out\n", p->agent);
  1322. }
  1323. ast_mutex_unlock(&agentlock);
  1324. if (!res)
  1325. res = ast_safe_sleep(chan, 500);
  1326. res = ast_streamfile(chan, "vm-goodbye", chan->language);
  1327. if (!res)
  1328. res = ast_waitstream(chan, "");
  1329. if (!res)
  1330. res = ast_safe_sleep(chan, 1000);
  1331. ast_mutex_unlock(&p->lock);
  1332. } else if (!res) {
  1333. #ifdef HONOR_MUSIC_CLASS
  1334. /* check if the moh class was changed with setmusiconhold */
  1335. if (*(chan->musicclass))
  1336. strncpy(p->moh, chan->musicclass, sizeof(p->moh) - 1);
  1337. #endif
  1338. ast_moh_start(chan, p->moh);
  1339. if (p->loginstart == 0)
  1340. time(&p->loginstart);
  1341. manager_event(EVENT_FLAG_AGENT, "Agentlogin",
  1342. "Agent: %s\r\n"
  1343. "Channel: %s\r\n"
  1344. "Uniqueid: %s\r\n",
  1345. p->agent, chan->name, chan->uniqueid);
  1346. if (updatecdr && chan->cdr)
  1347. snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
  1348. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGIN", "%s", chan->name);
  1349. if (option_verbose > 2)
  1350. ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %s/%s)\n", p->agent,
  1351. ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
  1352. /* Login this channel and wait for it to
  1353. go away */
  1354. p->chan = chan;
  1355. if (p->ackcall > 1)
  1356. check_beep(p, 0);
  1357. else
  1358. check_availability(p, 0);
  1359. ast_mutex_unlock(&p->lock);
  1360. ast_mutex_unlock(&agentlock);
  1361. while (res >= 0) {
  1362. ast_mutex_lock(&p->lock);
  1363. if (p->chan != chan)
  1364. res = -1;
  1365. ast_mutex_unlock(&p->lock);
  1366. /* Yield here so other interested threads can kick in. */
  1367. sched_yield();
  1368. if (res)
  1369. break;
  1370. ast_mutex_lock(&agentlock);
  1371. ast_mutex_lock(&p->lock);
  1372. if (p->lastdisc.tv_sec) {
  1373. gettimeofday(&tv, NULL);
  1374. if ((tv.tv_sec - p->lastdisc.tv_sec) * 1000 +
  1375. (tv.tv_usec - p->lastdisc.tv_usec) / 1000 > p->wrapuptime) {
  1376. ast_log(LOG_DEBUG, "Wrapup time expired!\n");
  1377. memset(&p->lastdisc, 0, sizeof(p->lastdisc));
  1378. if (p->ackcall > 1)
  1379. check_beep(p, 0);
  1380. else
  1381. check_availability(p, 0);
  1382. }
  1383. }
  1384. ast_mutex_unlock(&p->lock);
  1385. ast_mutex_unlock(&agentlock);
  1386. /* Synchronize channel ownership between call to agent and itself. */
  1387. ast_mutex_lock( &p->app_lock );
  1388. ast_mutex_lock(&p->lock);
  1389. p->owning_app = pthread_self();
  1390. ast_mutex_unlock(&p->lock);
  1391. if (p->ackcall > 1)
  1392. res = agent_ack_sleep(p);
  1393. else
  1394. res = ast_safe_sleep_conditional( chan, 1000,
  1395. agent_cont_sleep, p );
  1396. ast_mutex_unlock( &p->app_lock );
  1397. if ((p->ackcall > 1) && (res == 1)) {
  1398. ast_mutex_lock(&agentlock);
  1399. ast_mutex_lock(&p->lock);
  1400. check_availability(p, 0);
  1401. ast_mutex_unlock(&p->lock);
  1402. ast_mutex_unlock(&agentlock);
  1403. res = 0;
  1404. }
  1405. sched_yield();
  1406. }
  1407. ast_mutex_lock(&p->lock);
  1408. if (res && p->owner)
  1409. ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
  1410. /* Log us off if appropriate */
  1411. if (p->chan == chan)
  1412. p->chan = NULL;
  1413. p->acknowledged = 0;
  1414. logintime = time(NULL) - p->loginstart;
  1415. p->loginstart = 0;
  1416. ast_mutex_unlock(&p->lock);
  1417. manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
  1418. "Agent: %s\r\n"
  1419. "Logintime: %ld\r\n"
  1420. "Uniqueid: %s\r\n",
  1421. p->agent, logintime, chan->uniqueid);
  1422. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGOFF", "%s|%ld", chan->name, logintime);
  1423. if (option_verbose > 2)
  1424. ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
  1425. /* If there is no owner, go ahead and kill it now */
  1426. if (p->dead && !p->owner) {
  1427. ast_mutex_destroy(&p->lock);
  1428. ast_mutex_destroy(&p->app_lock);
  1429. free(p);
  1430. }
  1431. }
  1432. else {
  1433. ast_mutex_unlock(&p->lock);
  1434. p = NULL;
  1435. }
  1436. res = -1;
  1437. } else {
  1438. ast_mutex_unlock(&p->lock);
  1439. errmsg = "agent-alreadyon";
  1440. p = NULL;
  1441. }
  1442. break;
  1443. }
  1444. ast_mutex_unlock(&p->lock);
  1445. p = p->next;
  1446. }
  1447. if (!p)
  1448. ast_mutex_unlock(&agentlock);
  1449. if (!res)
  1450. res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
  1451. }
  1452. LOCAL_USER_REMOVE(u);
  1453. /* Always hangup */
  1454. return -1;
  1455. }
  1456. static int login_exec(struct ast_channel *chan, void *data)
  1457. {
  1458. return __login_exec(chan, data, 0);
  1459. }
  1460. static int callback_exec(struct ast_channel *chan, void *data)
  1461. {
  1462. return __login_exec(chan, data, 1);
  1463. }
  1464. static int agentmonitoroutgoing_exec(struct ast_channel *chan, void *data)
  1465. {
  1466. int exitifnoagentid = 0;
  1467. int nowarnings = 0;
  1468. int res = 0;
  1469. char agent[AST_MAX_AGENT], *tmp;
  1470. if (data) {
  1471. if (strchr(data, 'd'))
  1472. exitifnoagentid = 1;
  1473. if (strchr(data, 'n'))
  1474. nowarnings = 1;
  1475. }
  1476. if (chan->callerid) {
  1477. char agentvar[AST_MAX_BUF];
  1478. snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID, chan->callerid);
  1479. if ((tmp = pbx_builtin_getvar_helper(NULL, agentvar))) {
  1480. struct agent_pvt *p = agents;
  1481. strncpy(agent, tmp, sizeof(agent) - 1);
  1482. ast_mutex_lock(&agentlock);
  1483. while (p) {
  1484. if (!strcasecmp(p->agent, tmp)) {
  1485. __agent_start_monitoring(chan, p, 1);
  1486. break;
  1487. }
  1488. p = p->next;
  1489. }
  1490. ast_mutex_unlock(&agentlock);
  1491. } else {
  1492. res = -1;
  1493. if (!nowarnings)
  1494. ast_log(LOG_WARNING, "Couldn't find the global variable %s, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n", agentvar);
  1495. }
  1496. } else {
  1497. res = -1;
  1498. if (!nowarnings)
  1499. ast_log(LOG_WARNING, "There is no callerid on that call, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n");
  1500. }
  1501. /* check if there is n + 101 priority */
  1502. if (res) {
  1503. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid)) {
  1504. chan->priority+=100;
  1505. ast_verbose(VERBOSE_PREFIX_3 "Going to %d priority because there is no callerid or the agentid cannot be found.\n",chan->priority);
  1506. }
  1507. else if (exitifnoagentid)
  1508. return res;
  1509. }
  1510. return 0;
  1511. }
  1512. int load_module()
  1513. {
  1514. /* Make sure we can register our sip channel type */
  1515. if (ast_channel_register(type, tdesc, capability, agent_request)) {
  1516. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  1517. return -1;
  1518. }
  1519. ast_register_application(app, login_exec, synopsis, descrip);
  1520. ast_register_application(app2, callback_exec, synopsis2, descrip2);
  1521. ast_register_application(app3, agentmonitoroutgoing_exec, synopsis3, descrip3);
  1522. ast_cli_register(&cli_show_agents);
  1523. /* Read in the config */
  1524. read_agent_config();
  1525. return 0;
  1526. }
  1527. int reload()
  1528. {
  1529. read_agent_config();
  1530. return 0;
  1531. }
  1532. int unload_module()
  1533. {
  1534. struct agent_pvt *p;
  1535. /* First, take us out of the channel loop */
  1536. ast_cli_unregister(&cli_show_agents);
  1537. ast_unregister_application(app);
  1538. ast_unregister_application(app2);
  1539. ast_unregister_application(app3);
  1540. ast_channel_unregister(type);
  1541. if (!ast_mutex_lock(&agentlock)) {
  1542. /* Hangup all interfaces if they have an owner */
  1543. p = agents;
  1544. while(p) {
  1545. if (p->owner)
  1546. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1547. p = p->next;
  1548. }
  1549. agents = NULL;
  1550. ast_mutex_unlock(&agentlock);
  1551. } else {
  1552. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1553. return -1;
  1554. }
  1555. return 0;
  1556. }
  1557. int usecount()
  1558. {
  1559. int res;
  1560. ast_mutex_lock(&usecnt_lock);
  1561. res = usecnt;
  1562. ast_mutex_unlock(&usecnt_lock);
  1563. return res;
  1564. }
  1565. char *key()
  1566. {
  1567. return ASTERISK_GPL_KEY;
  1568. }
  1569. char *description()
  1570. {
  1571. return desc;
  1572. }