app_agent_pool.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Call center agent pool.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. * \arg \ref Config_agent
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/cli.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/bridge.h"
  39. #include "asterisk/bridge_internal.h"
  40. #include "asterisk/bridge_basic.h"
  41. #include "asterisk/bridge_after.h"
  42. #include "asterisk/config_options.h"
  43. #include "asterisk/features_config.h"
  44. #include "asterisk/astobj2.h"
  45. #include "asterisk/stringfields.h"
  46. #include "asterisk/stasis_channels.h"
  47. #include "asterisk/causes.h"
  48. /*** DOCUMENTATION
  49. <application name="AgentLogin" language="en_US">
  50. <synopsis>
  51. Login an agent.
  52. </synopsis>
  53. <syntax argsep=",">
  54. <parameter name="AgentId" required="true" />
  55. <parameter name="options">
  56. <optionlist>
  57. <option name="s">
  58. <para>silent login - do not announce the login ok segment after
  59. agent logged on.</para>
  60. </option>
  61. </optionlist>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Login an agent to the system. Any agent authentication is assumed to
  66. already be done by dialplan. While logged in, the agent can receive calls
  67. and will hear the sound file specified by the config option custom_beep
  68. when a new call comes in for the agent. Login failures will continue in
  69. the dialplan with <variable>AGENT_STATUS</variable> set.</para>
  70. <para>Before logging in, you can setup on the real agent channel the
  71. CHANNEL(dtmf-features) an agent will have when talking to a caller
  72. and you can setup on the channel running this application the
  73. CONNECTEDLINE() information the agent will see while waiting for a
  74. caller.</para>
  75. <para><variable>AGENT_STATUS</variable> enumeration values:</para>
  76. <enumlist>
  77. <enum name = "INVALID"><para>The specified agent is invalid.</para></enum>
  78. <enum name = "ALREADY_LOGGED_IN"><para>The agent is already logged in.</para></enum>
  79. </enumlist>
  80. <note><para>The Agents:<replaceable>AgentId</replaceable> device state is
  81. available to monitor the status of the agent.</para></note>
  82. </description>
  83. <see-also>
  84. <ref type="application">Authenticate</ref>
  85. <ref type="application">Queue</ref>
  86. <ref type="application">AddQueueMember</ref>
  87. <ref type="application">RemoveQueueMember</ref>
  88. <ref type="application">PauseQueueMember</ref>
  89. <ref type="application">UnpauseQueueMember</ref>
  90. <ref type="function">AGENT</ref>
  91. <ref type="function">CHANNEL(dtmf-features)</ref>
  92. <ref type="function">CONNECTEDLINE()</ref>
  93. <ref type="filename">agents.conf</ref>
  94. <ref type="filename">queues.conf</ref>
  95. </see-also>
  96. </application>
  97. <application name="AgentRequest" language="en_US">
  98. <synopsis>
  99. Request an agent to connect with the channel.
  100. </synopsis>
  101. <syntax argsep=",">
  102. <parameter name="AgentId" required="true" />
  103. </syntax>
  104. <description>
  105. <para>Request an agent to connect with the channel. Failure to find,
  106. alert the agent, or acknowledge the call will continue in the dialplan
  107. with <variable>AGENT_STATUS</variable> set.</para>
  108. <para><variable>AGENT_STATUS</variable> enumeration values:</para>
  109. <enumlist>
  110. <enum name = "INVALID"><para>The specified agent is invalid.</para></enum>
  111. <enum name = "NOT_LOGGED_IN"><para>The agent is not available.</para></enum>
  112. <enum name = "BUSY"><para>The agent is on another call.</para></enum>
  113. <enum name = "NOT_CONNECTED"><para>The agent did not connect with the
  114. call. The agent most likely did not acknowledge the call.</para></enum>
  115. <enum name = "ERROR"><para>Alerting the agent failed.</para></enum>
  116. </enumlist>
  117. </description>
  118. <see-also>
  119. <ref type="application">AgentLogin</ref>
  120. </see-also>
  121. </application>
  122. <function name="AGENT" language="en_US">
  123. <synopsis>
  124. Gets information about an Agent
  125. </synopsis>
  126. <syntax argsep=":">
  127. <parameter name="AgentId" required="true" />
  128. <parameter name="item">
  129. <para>The valid items to retrieve are:</para>
  130. <enumlist>
  131. <enum name="status">
  132. <para>(default) The status of the agent (LOGGEDIN | LOGGEDOUT)</para>
  133. </enum>
  134. <enum name="password">
  135. <para>Deprecated. The dialplan handles any agent authentication.</para>
  136. </enum>
  137. <enum name="name">
  138. <para>The name of the agent</para>
  139. </enum>
  140. <enum name="mohclass">
  141. <para>MusicOnHold class</para>
  142. </enum>
  143. <enum name="channel">
  144. <para>The name of the active channel for the Agent (AgentLogin)</para>
  145. </enum>
  146. <enum name="fullchannel">
  147. <para>The untruncated name of the active channel for the Agent (AgentLogin)</para>
  148. </enum>
  149. </enumlist>
  150. </parameter>
  151. </syntax>
  152. <description></description>
  153. </function>
  154. <manager name="Agents" language="en_US">
  155. <synopsis>
  156. Lists agents and their status.
  157. </synopsis>
  158. <syntax>
  159. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  160. </syntax>
  161. <description>
  162. <para>Will list info about all defined agents.</para>
  163. </description>
  164. <see-also>
  165. <ref type="managerEvent">Agents</ref>
  166. <ref type="managerEvent">AgentsComplete</ref>
  167. </see-also>
  168. </manager>
  169. <managerEvent language="en_US" name="Agents">
  170. <managerEventInstance class="EVENT_FLAG_AGENT">
  171. <synopsis>
  172. Response event in a series to the Agents AMI action containing
  173. information about a defined agent.
  174. </synopsis>
  175. <syntax>
  176. <parameter name="Agent">
  177. <para>Agent ID of the agent.</para>
  178. </parameter>
  179. <parameter name="Name">
  180. <para>User friendly name of the agent.</para>
  181. </parameter>
  182. <parameter name="Status">
  183. <para>Current status of the agent.</para>
  184. <para>The valid values are:</para>
  185. <enumlist>
  186. <enum name="AGENT_LOGGEDOFF" />
  187. <enum name="AGENT_IDLE" />
  188. <enum name="AGENT_ONCALL" />
  189. </enumlist>
  190. </parameter>
  191. <parameter name="TalkingToChan">
  192. <para><variable>BRIDGEPEER</variable> value on agent channel.</para>
  193. <para>Present if Status value is <literal>AGENT_ONCALL</literal>.</para>
  194. </parameter>
  195. <parameter name="CallStarted">
  196. <para>Epoche time when the agent started talking with the caller.</para>
  197. <para>Present if Status value is <literal>AGENT_ONCALL</literal>.</para>
  198. </parameter>
  199. <parameter name="LoggedInTime">
  200. <para>Epoche time when the agent logged in.</para>
  201. <para>Present if Status value is <literal>AGENT_IDLE</literal> or <literal>AGENT_ONCALL</literal>.</para>
  202. </parameter>
  203. <channel_snapshot/>
  204. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  205. </syntax>
  206. <description>
  207. <para>The channel snapshot is present if the Status value is <literal>AGENT_IDLE</literal> or <literal>AGENT_ONCALL</literal>.</para>
  208. </description>
  209. <see-also>
  210. <ref type="manager">Agents</ref>
  211. </see-also>
  212. </managerEventInstance>
  213. </managerEvent>
  214. <managerEvent language="en_US" name="AgentsComplete">
  215. <managerEventInstance class="EVENT_FLAG_AGENT">
  216. <synopsis>
  217. Final response event in a series of events to the Agents AMI action.
  218. </synopsis>
  219. <syntax>
  220. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  221. </syntax>
  222. <see-also>
  223. <ref type="manager">Agents</ref>
  224. </see-also>
  225. </managerEventInstance>
  226. </managerEvent>
  227. <manager name="AgentLogoff" language="en_US">
  228. <synopsis>
  229. Sets an agent as no longer logged in.
  230. </synopsis>
  231. <syntax>
  232. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  233. <parameter name="Agent" required="true">
  234. <para>Agent ID of the agent to log off.</para>
  235. </parameter>
  236. <parameter name="Soft">
  237. <para>Set to <literal>true</literal> to not hangup existing calls.</para>
  238. </parameter>
  239. </syntax>
  240. <description>
  241. <para>Sets an agent as no longer logged in.</para>
  242. </description>
  243. </manager>
  244. <configInfo name="app_agent_pool" language="en_US">
  245. <synopsis>Agent pool applications</synopsis>
  246. <description>
  247. <note><para>Option changes take effect on agent login or after an agent
  248. disconnects from a call.</para></note>
  249. </description>
  250. <configFile name="agents.conf">
  251. <configObject name="global">
  252. <synopsis>Unused, but reserved.</synopsis>
  253. </configObject>
  254. <configObject name="agent-id">
  255. <synopsis>Configure an agent for the pool.</synopsis>
  256. <description>
  257. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  258. </description>
  259. <configOption name="ackcall">
  260. <synopsis>Enable to require the agent to acknowledge a call.</synopsis>
  261. <description>
  262. <para>Enable to require the agent to give a DTMF acknowledgement
  263. when the agent receives a call.</para>
  264. <note><para>The option is overridden by <variable>AGENTACKCALL</variable> on agent login.</para></note>
  265. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  266. </description>
  267. </configOption>
  268. <configOption name="acceptdtmf">
  269. <synopsis>DTMF key sequence the agent uses to acknowledge a call.</synopsis>
  270. <description>
  271. <note><para>The option is overridden by <variable>AGENTACCEPTDTMF</variable> on agent login.</para></note>
  272. <note><para>The option is ignored unless the ackcall option is enabled.</para></note>
  273. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  274. </description>
  275. </configOption>
  276. <configOption name="autologoff">
  277. <synopsis>Time the agent has to acknowledge a call before being logged off.</synopsis>
  278. <description>
  279. <para>Set how many seconds a call for the agent has to wait for the
  280. agent to acknowledge the call before the agent is automatically
  281. logged off. If set to zero then the call will wait forever for
  282. the agent to acknowledge.</para>
  283. <note><para>The option is overridden by <variable>AGENTAUTOLOGOFF</variable> on agent login.</para></note>
  284. <note><para>The option is ignored unless the ackcall option is enabled.</para></note>
  285. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  286. </description>
  287. </configOption>
  288. <configOption name="wrapuptime">
  289. <synopsis>Minimum time the agent has between calls.</synopsis>
  290. <description>
  291. <para>Set the minimum amount of time in milliseconds after
  292. disconnecting a call before the agent can receive a new call.</para>
  293. <note><para>The option is overridden by <variable>AGENTWRAPUPTIME</variable> on agent login.</para></note>
  294. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  295. </description>
  296. </configOption>
  297. <configOption name="musiconhold">
  298. <synopsis>Music on hold class the agent listens to between calls.</synopsis>
  299. <description>
  300. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  301. </description>
  302. </configOption>
  303. <configOption name="recordagentcalls">
  304. <synopsis>Enable to automatically record calls the agent takes.</synopsis>
  305. <description>
  306. <para>Enable recording calls the agent takes automatically by
  307. invoking the automixmon DTMF feature when the agent connects
  308. to a caller. See <filename>features.conf.sample</filename> for information about
  309. the automixmon feature.</para>
  310. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  311. </description>
  312. </configOption>
  313. <configOption name="custom_beep">
  314. <synopsis>Sound file played to alert the agent when a call is present.</synopsis>
  315. <description>
  316. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  317. </description>
  318. </configOption>
  319. <configOption name="fullname">
  320. <synopsis>A friendly name for the agent used in log messages.</synopsis>
  321. <description>
  322. <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
  323. </description>
  324. </configOption>
  325. </configObject>
  326. </configFile>
  327. </configInfo>
  328. ***/
  329. /* ------------------------------------------------------------------- */
  330. #define AST_MAX_BUF 256
  331. /*! Maximum wait time (in ms) for the custom_beep file to play announcing the caller. */
  332. #define CALLER_SAFETY_TIMEOUT_TIME (2 * 60 * 1000)
  333. /*! Number of seconds to wait for local channel optimizations to complete. */
  334. #define LOGIN_WAIT_TIMEOUT_TIME 5
  335. static const char app_agent_login[] = "AgentLogin";
  336. static const char app_agent_request[] = "AgentRequest";
  337. /*! Agent config parameters. */
  338. struct agent_cfg {
  339. AST_DECLARE_STRING_FIELDS(
  340. /*! Identification of the agent. (agents config container key) */
  341. AST_STRING_FIELD(username);
  342. /*! Name of agent for logging and querying purposes */
  343. AST_STRING_FIELD(full_name);
  344. /*!
  345. * \brief DTMF string for an agent to accept a call.
  346. *
  347. * \note The channel variable AGENTACCEPTDTMF overrides on login.
  348. */
  349. AST_STRING_FIELD(dtmf_accept);
  350. /*! Beep sound file to use. Alert the agent a call is waiting. */
  351. AST_STRING_FIELD(beep_sound);
  352. /*! MOH class to use while agent waiting for call. */
  353. AST_STRING_FIELD(moh);
  354. );
  355. /*!
  356. * \brief Number of seconds for agent to ack a call before being logged off.
  357. *
  358. * \note The channel variable AGENTAUTOLOGOFF overrides on login.
  359. * \note If zero then timer is disabled.
  360. */
  361. unsigned int auto_logoff;
  362. /*!
  363. * \brief Time after a call in ms before the agent can get a new call.
  364. *
  365. * \note The channel variable AGENTWRAPUPTIME overrides on login.
  366. */
  367. unsigned int wrapup_time;
  368. /*!
  369. * \brief TRUE if agent needs to ack a call to accept it.
  370. *
  371. * \note The channel variable AGENTACKCALL overrides on login.
  372. */
  373. int ack_call;
  374. /*! TRUE if agent calls are automatically recorded. */
  375. int record_agent_calls;
  376. };
  377. /*!
  378. * \internal
  379. * \brief Agent config ao2 container sort function.
  380. * \since 12.0.0
  381. *
  382. * \param obj_left pointer to the (user-defined part) of an object.
  383. * \param obj_right pointer to the (user-defined part) of an object.
  384. * \param flags flags from ao2_callback()
  385. * OBJ_POINTER - if set, 'obj_right', is an object.
  386. * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
  387. * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  388. *
  389. * \retval <0 if obj_left < obj_right
  390. * \retval =0 if obj_left == obj_right
  391. * \retval >0 if obj_left > obj_right
  392. */
  393. static int agent_cfg_sort_cmp(const void *obj_left, const void *obj_right, int flags)
  394. {
  395. const struct agent_cfg *cfg_left = obj_left;
  396. const struct agent_cfg *cfg_right = obj_right;
  397. const char *right_key = obj_right;
  398. int cmp;
  399. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  400. default:
  401. case OBJ_POINTER:
  402. right_key = cfg_right->username;
  403. /* Fall through */
  404. case OBJ_KEY:
  405. cmp = strcmp(cfg_left->username, right_key);
  406. break;
  407. case OBJ_PARTIAL_KEY:
  408. cmp = strncmp(cfg_left->username, right_key, strlen(right_key));
  409. break;
  410. }
  411. return cmp;
  412. }
  413. static void agent_cfg_destructor(void *vdoomed)
  414. {
  415. struct agent_cfg *doomed = vdoomed;
  416. ast_string_field_free_memory(doomed);
  417. }
  418. static void *agent_cfg_alloc(const char *name)
  419. {
  420. struct agent_cfg *cfg;
  421. cfg = ao2_alloc_options(sizeof(*cfg), agent_cfg_destructor,
  422. AO2_ALLOC_OPT_LOCK_NOLOCK);
  423. if (!cfg || ast_string_field_init(cfg, 64)) {
  424. return NULL;
  425. }
  426. ast_string_field_set(cfg, username, name);
  427. return cfg;
  428. }
  429. static void *agent_cfg_find(struct ao2_container *agents, const char *username)
  430. {
  431. return ao2_find(agents, username, OBJ_KEY);
  432. }
  433. /*! Agents configuration */
  434. struct agents_cfg {
  435. /*! Master configured agents container. */
  436. struct ao2_container *agents;
  437. };
  438. static struct aco_type agent_type = {
  439. .type = ACO_ITEM,
  440. .name = "agent-id",
  441. .category_match = ACO_BLACKLIST,
  442. .category = "^(general|agents)$",
  443. .item_alloc = agent_cfg_alloc,
  444. .item_find = agent_cfg_find,
  445. .item_offset = offsetof(struct agents_cfg, agents),
  446. };
  447. static struct aco_type *agent_types[] = ACO_TYPES(&agent_type);
  448. /* The general category is reserved, but unused */
  449. static struct aco_type general_type = {
  450. .type = ACO_GLOBAL,
  451. .name = "global",
  452. .category_match = ACO_WHITELIST,
  453. .category = "^general$",
  454. };
  455. static struct aco_file agents_conf = {
  456. .filename = "agents.conf",
  457. .types = ACO_TYPES(&general_type, &agent_type),
  458. };
  459. static AO2_GLOBAL_OBJ_STATIC(cfg_handle);
  460. static void agents_cfg_destructor(void *vdoomed)
  461. {
  462. struct agents_cfg *doomed = vdoomed;
  463. ao2_cleanup(doomed->agents);
  464. doomed->agents = NULL;
  465. }
  466. /*!
  467. * \internal
  468. * \brief Create struct agents_cfg object.
  469. * \since 12.0.0
  470. *
  471. * \note A lock is not needed for the object or any secondary
  472. * created cfg objects. These objects are immutable after the
  473. * config is loaded and applied.
  474. *
  475. * \retval New struct agents_cfg object.
  476. * \retval NULL on error.
  477. */
  478. static void *agents_cfg_alloc(void)
  479. {
  480. struct agents_cfg *cfg;
  481. cfg = ao2_alloc_options(sizeof(*cfg), agents_cfg_destructor,
  482. AO2_ALLOC_OPT_LOCK_NOLOCK);
  483. if (!cfg) {
  484. return NULL;
  485. }
  486. cfg->agents = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK,
  487. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, agent_cfg_sort_cmp, NULL);
  488. if (!cfg->agents) {
  489. ao2_ref(cfg, -1);
  490. cfg = NULL;
  491. }
  492. return cfg;
  493. }
  494. static void agents_post_apply_config(void);
  495. CONFIG_INFO_STANDARD(cfg_info, cfg_handle, agents_cfg_alloc,
  496. .files = ACO_FILES(&agents_conf),
  497. .post_apply_config = agents_post_apply_config,
  498. );
  499. static void destroy_config(void)
  500. {
  501. ao2_global_obj_release(cfg_handle);
  502. aco_info_destroy(&cfg_info);
  503. }
  504. static int load_config(void)
  505. {
  506. if (aco_info_init(&cfg_info)) {
  507. return -1;
  508. }
  509. /* Agent options */
  510. aco_option_register(&cfg_info, "ackcall", ACO_EXACT, agent_types, "no", OPT_BOOL_T, 1, FLDSET(struct agent_cfg, ack_call));
  511. aco_option_register(&cfg_info, "acceptdtmf", ACO_EXACT, agent_types, "#", OPT_STRINGFIELD_T, 1, STRFLDSET(struct agent_cfg, dtmf_accept));
  512. aco_option_register(&cfg_info, "autologoff", ACO_EXACT, agent_types, "0", OPT_UINT_T, 0, FLDSET(struct agent_cfg, auto_logoff));
  513. aco_option_register(&cfg_info, "wrapuptime", ACO_EXACT, agent_types, "0", OPT_UINT_T, 0, FLDSET(struct agent_cfg, wrapup_time));
  514. aco_option_register(&cfg_info, "musiconhold", ACO_EXACT, agent_types, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, moh));
  515. aco_option_register(&cfg_info, "recordagentcalls", ACO_EXACT, agent_types, "no", OPT_BOOL_T, 1, FLDSET(struct agent_cfg, record_agent_calls));
  516. aco_option_register(&cfg_info, "custom_beep", ACO_EXACT, agent_types, "beep", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, beep_sound));
  517. aco_option_register(&cfg_info, "fullname", ACO_EXACT, agent_types, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, full_name));
  518. if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
  519. return -1;
  520. }
  521. return 0;
  522. }
  523. enum agent_state {
  524. /*! The agent is defined but an agent is not present. */
  525. AGENT_STATE_LOGGED_OUT,
  526. /*! Forced initial login wait to allow any local channel optimizations to happen. */
  527. AGENT_STATE_PROBATION_WAIT,
  528. /*! The agent is ready for a call. */
  529. AGENT_STATE_READY_FOR_CALL,
  530. /*! The agent has a call waiting to connect. */
  531. AGENT_STATE_CALL_PRESENT,
  532. /*! The agent needs to ack the call. */
  533. AGENT_STATE_CALL_WAIT_ACK,
  534. /*! The agent is connected with a call. */
  535. AGENT_STATE_ON_CALL,
  536. /*! The agent is resting between calls. */
  537. AGENT_STATE_CALL_WRAPUP,
  538. /*! The agent is being kicked out. */
  539. AGENT_STATE_LOGGING_OUT,
  540. };
  541. /*! Agent config option override flags. */
  542. enum agent_override_flags {
  543. AGENT_FLAG_ACK_CALL = (1 << 0),
  544. AGENT_FLAG_DTMF_ACCEPT = (1 << 1),
  545. AGENT_FLAG_AUTO_LOGOFF = (1 << 2),
  546. AGENT_FLAG_WRAPUP_TIME = (1 << 3),
  547. };
  548. /*! \brief Structure representing an agent. */
  549. struct agent_pvt {
  550. AST_DECLARE_STRING_FIELDS(
  551. /*! Identification of the agent. (agents container key) */
  552. AST_STRING_FIELD(username);
  553. /*! Login override DTMF string for an agent to accept a call. */
  554. AST_STRING_FIELD(override_dtmf_accept);
  555. );
  556. /*! Connected line information to send when reentering the holding bridge. */
  557. struct ast_party_connected_line waiting_colp;
  558. /*! Flags show if settings were overridden by channel vars. */
  559. unsigned int flags;
  560. /*! Login override number of seconds for agent to ack a call before being logged off. */
  561. unsigned int override_auto_logoff;
  562. /*! Login override time after a call in ms before the agent can get a new call. */
  563. unsigned int override_wrapup_time;
  564. /*! Login override if agent needs to ack a call to accept it. */
  565. unsigned int override_ack_call:1;
  566. /*! TRUE if the agent is requested to logoff when the current call ends. */
  567. unsigned int deferred_logoff:1;
  568. /*! Mark and sweep config update to determine if an agent is dead. */
  569. unsigned int the_mark:1;
  570. /*!
  571. * \brief TRUE if the agent is no longer configured and is being destroyed.
  572. *
  573. * \note Agents cannot log in if they are dead.
  574. */
  575. unsigned int dead:1;
  576. /*! Agent control state variable. */
  577. enum agent_state state;
  578. /*! Custom device state of agent. */
  579. enum ast_device_state devstate;
  580. /*! When agent first logged in */
  581. time_t login_start;
  582. /*! When agent login probation started. */
  583. time_t probation_start;
  584. /*! When call started */
  585. time_t call_start;
  586. /*! When ack timer started */
  587. struct timeval ack_time;
  588. /*! When last disconnected */
  589. struct timeval last_disconnect;
  590. /*! Caller is waiting in this bridge for agent to join. (Holds ref) */
  591. struct ast_bridge *caller_bridge;
  592. /*! Agent is logged in with this channel. (Holds ref) (NULL if not logged in.) */
  593. struct ast_channel *logged;
  594. /*! Active config values from config file. (Holds ref) */
  595. struct agent_cfg *cfg;
  596. };
  597. /*! Container of defined agents. */
  598. static struct ao2_container *agents;
  599. /*!
  600. * \brief Lock the agent.
  601. *
  602. * \param agent Agent to lock
  603. *
  604. * \return Nothing
  605. */
  606. #define agent_lock(agent) _agent_lock(agent, __FILE__, __PRETTY_FUNCTION__, __LINE__, #agent)
  607. static inline void _agent_lock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
  608. {
  609. __ao2_lock(agent, AO2_LOCK_REQ_MUTEX, file, function, line, var);
  610. }
  611. /*!
  612. * \brief Unlock the agent.
  613. *
  614. * \param agent Agent to unlock
  615. *
  616. * \return Nothing
  617. */
  618. #define agent_unlock(agent) _agent_unlock(agent, __FILE__, __PRETTY_FUNCTION__, __LINE__, #agent)
  619. static inline void _agent_unlock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
  620. {
  621. __ao2_unlock(agent, file, function, line, var);
  622. }
  623. /*!
  624. * \internal
  625. * \brief Obtain the agent logged in channel lock if it exists.
  626. * \since 12.0.0
  627. *
  628. * \param agent Pointer to the LOCKED agent_pvt.
  629. *
  630. * \note Assumes the agent lock is already obtained.
  631. *
  632. * \note Defined locking order is channel lock then agent lock.
  633. *
  634. * \return Nothing
  635. */
  636. static struct ast_channel *agent_lock_logged(struct agent_pvt *agent)
  637. {
  638. struct ast_channel *logged;
  639. for (;;) {
  640. if (!agent->logged) { /* No owner. Nothing to do. */
  641. return NULL;
  642. }
  643. /* If we don't ref the logged, it could be killed when we unlock the agent. */
  644. logged = ast_channel_ref(agent->logged);
  645. /* Locking logged requires us to lock channel, then agent. */
  646. agent_unlock(agent);
  647. ast_channel_lock(logged);
  648. agent_lock(agent);
  649. /* Check if logged changed during agent unlock period */
  650. if (logged != agent->logged) {
  651. /* Channel changed. Unref and do another pass. */
  652. ast_channel_unlock(logged);
  653. ast_channel_unref(logged);
  654. } else {
  655. /* Channel stayed the same. Return it. */
  656. return logged;
  657. }
  658. }
  659. }
  660. /*!
  661. * \internal
  662. * \brief Get the Agent:agent_id device state.
  663. * \since 12.0.0
  664. *
  665. * \param agent_id Username of the agent.
  666. *
  667. * \details
  668. * Search the agents container for the agent and return the
  669. * current state.
  670. *
  671. * \return Device state of the agent.
  672. */
  673. static enum ast_device_state agent_pvt_devstate_get(const char *agent_id)
  674. {
  675. enum ast_device_state dev_state = AST_DEVICE_INVALID;
  676. struct agent_pvt *agent;
  677. agent = ao2_find(agents, agent_id, OBJ_KEY);
  678. if (agent) {
  679. agent_lock(agent);
  680. dev_state = agent->devstate;
  681. agent_unlock(agent);
  682. ao2_ref(agent, -1);
  683. }
  684. return dev_state;
  685. }
  686. /*!
  687. * \internal
  688. * \brief Request an agent device state be updated.
  689. * \since 12.0.0
  690. *
  691. * \param agent_id Which agent needs the device state updated.
  692. *
  693. * \return Nothing
  694. */
  695. static void agent_devstate_changed(const char *agent_id)
  696. {
  697. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "Agent:%s", agent_id);
  698. }
  699. static void agent_pvt_destructor(void *vdoomed)
  700. {
  701. struct agent_pvt *doomed = vdoomed;
  702. /* Make sure device state reflects agent destruction. */
  703. if (!ast_strlen_zero(doomed->username)) {
  704. ast_debug(1, "Agent %s: Destroyed.\n", doomed->username);
  705. agent_devstate_changed(doomed->username);
  706. }
  707. ast_party_connected_line_free(&doomed->waiting_colp);
  708. if (doomed->caller_bridge) {
  709. ast_bridge_destroy(doomed->caller_bridge, 0);
  710. doomed->caller_bridge = NULL;
  711. }
  712. if (doomed->logged) {
  713. doomed->logged = ast_channel_unref(doomed->logged);
  714. }
  715. ao2_cleanup(doomed->cfg);
  716. doomed->cfg = NULL;
  717. ast_string_field_free_memory(doomed);
  718. }
  719. static struct agent_pvt *agent_pvt_new(struct agent_cfg *cfg)
  720. {
  721. struct agent_pvt *agent;
  722. agent = ao2_alloc(sizeof(*agent), agent_pvt_destructor);
  723. if (!agent) {
  724. return NULL;
  725. }
  726. if (ast_string_field_init(agent, 32)) {
  727. ao2_ref(agent, -1);
  728. return NULL;
  729. }
  730. ast_string_field_set(agent, username, cfg->username);
  731. ast_party_connected_line_init(&agent->waiting_colp);
  732. ao2_ref(cfg, +1);
  733. agent->cfg = cfg;
  734. agent->devstate = AST_DEVICE_UNAVAILABLE;
  735. return agent;
  736. }
  737. /*!
  738. * \internal
  739. * \brief Agents ao2 container sort function.
  740. * \since 12.0.0
  741. *
  742. * \param obj_left pointer to the (user-defined part) of an object.
  743. * \param obj_right pointer to the (user-defined part) of an object.
  744. * \param flags flags from ao2_callback()
  745. * OBJ_POINTER - if set, 'obj_right', is an object.
  746. * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
  747. * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  748. *
  749. * \retval <0 if obj_left < obj_right
  750. * \retval =0 if obj_left == obj_right
  751. * \retval >0 if obj_left > obj_right
  752. */
  753. static int agent_pvt_sort_cmp(const void *obj_left, const void *obj_right, int flags)
  754. {
  755. const struct agent_pvt *agent_left = obj_left;
  756. const struct agent_pvt *agent_right = obj_right;
  757. const char *right_key = obj_right;
  758. int cmp;
  759. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  760. default:
  761. case OBJ_POINTER:
  762. right_key = agent_right->username;
  763. /* Fall through */
  764. case OBJ_KEY:
  765. cmp = strcmp(agent_left->username, right_key);
  766. break;
  767. case OBJ_PARTIAL_KEY:
  768. cmp = strncmp(agent_left->username, right_key, strlen(right_key));
  769. break;
  770. }
  771. return cmp;
  772. }
  773. /*!
  774. * \internal
  775. * \brief ao2_find() callback function.
  776. * \since 12.0.0
  777. *
  778. * Usage:
  779. * found = ao2_find(agents, agent, OBJ_POINTER);
  780. * found = ao2_find(agents, "agent-id", OBJ_KEY);
  781. * found = ao2_find(agents, agent->logged, 0);
  782. */
  783. static int agent_pvt_cmp(void *obj, void *arg, int flags)
  784. {
  785. const struct agent_pvt *agent = obj;
  786. int cmp;
  787. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  788. case OBJ_POINTER:
  789. case OBJ_KEY:
  790. case OBJ_PARTIAL_KEY:
  791. cmp = CMP_MATCH;
  792. break;
  793. default:
  794. if (agent->logged == arg) {
  795. cmp = CMP_MATCH;
  796. } else {
  797. cmp = 0;
  798. }
  799. break;
  800. }
  801. return cmp;
  802. }
  803. static int agent_mark(void *obj, void *arg, int flags)
  804. {
  805. struct agent_pvt *agent = obj;
  806. agent_lock(agent);
  807. agent->the_mark = 1;
  808. agent_unlock(agent);
  809. return 0;
  810. }
  811. static void agents_mark(void)
  812. {
  813. ao2_callback(agents, 0, agent_mark, NULL);
  814. }
  815. static int agent_sweep(void *obj, void *arg, int flags)
  816. {
  817. struct agent_pvt *agent = obj;
  818. int cmp = 0;
  819. agent_lock(agent);
  820. if (agent->the_mark) {
  821. agent->the_mark = 0;
  822. agent->dead = 1;
  823. /* Unlink dead agents immediately. */
  824. cmp = CMP_MATCH;
  825. }
  826. agent_unlock(agent);
  827. return cmp;
  828. }
  829. static void agents_sweep(void)
  830. {
  831. struct ao2_iterator *iter;
  832. struct agent_pvt *agent;
  833. struct ast_channel *logged;
  834. iter = ao2_callback(agents, OBJ_MULTIPLE | OBJ_UNLINK, agent_sweep, NULL);
  835. if (!iter) {
  836. return;
  837. }
  838. for (; (agent = ao2_iterator_next(iter)); ao2_ref(agent, -1)) {
  839. agent_lock(agent);
  840. if (agent->logged) {
  841. logged = ast_channel_ref(agent->logged);
  842. } else {
  843. logged = NULL;
  844. }
  845. agent_unlock(agent);
  846. if (!logged) {
  847. continue;
  848. }
  849. ast_log(LOG_NOTICE,
  850. "Forced logoff of agent %s(%s). Agent no longer configured.\n",
  851. agent->username, ast_channel_name(logged));
  852. ast_softhangup(logged, AST_SOFTHANGUP_EXPLICIT);
  853. ast_channel_unref(logged);
  854. }
  855. ao2_iterator_destroy(iter);
  856. }
  857. static void agents_post_apply_config(void)
  858. {
  859. struct ao2_iterator iter;
  860. struct agent_cfg *cfg;
  861. RAII_VAR(struct agents_cfg *, cfgs, ao2_global_obj_ref(cfg_handle), ao2_cleanup);
  862. ast_assert(cfgs != NULL);
  863. agents_mark();
  864. iter = ao2_iterator_init(cfgs->agents, 0);
  865. for (; (cfg = ao2_iterator_next(&iter)); ao2_ref(cfg, -1)) {
  866. RAII_VAR(struct agent_pvt *, agent, ao2_find(agents, cfg->username, OBJ_KEY), ao2_cleanup);
  867. if (agent) {
  868. agent_lock(agent);
  869. agent->the_mark = 0;
  870. if (!agent->logged) {
  871. struct agent_cfg *cfg_old;
  872. /* Replace the config of agents not logged in. */
  873. cfg_old = agent->cfg;
  874. ao2_ref(cfg, +1);
  875. agent->cfg = cfg;
  876. ao2_cleanup(cfg_old);
  877. }
  878. agent_unlock(agent);
  879. continue;
  880. }
  881. agent = agent_pvt_new(cfg);
  882. if (!agent) {
  883. continue;
  884. }
  885. ao2_link(agents, agent);
  886. ast_debug(1, "Agent %s: Created.\n", agent->username);
  887. agent_devstate_changed(agent->username);
  888. }
  889. ao2_iterator_destroy(&iter);
  890. agents_sweep();
  891. }
  892. static int agent_logoff_request(const char *agent_id, int soft)
  893. {
  894. struct ast_channel *logged;
  895. RAII_VAR(struct agent_pvt *, agent, ao2_find(agents, agent_id, OBJ_KEY), ao2_cleanup);
  896. if (!agent) {
  897. return -1;
  898. }
  899. agent_lock(agent);
  900. logged = agent_lock_logged(agent);
  901. if (logged) {
  902. if (soft) {
  903. agent->deferred_logoff = 1;
  904. } else {
  905. ast_softhangup(logged, AST_SOFTHANGUP_EXPLICIT);
  906. }
  907. ast_channel_unlock(logged);
  908. ast_channel_unref(logged);
  909. }
  910. agent_unlock(agent);
  911. return 0;
  912. }
  913. /*! Agent holding bridge instance holder. */
  914. static AO2_GLOBAL_OBJ_STATIC(agent_holding);
  915. /*! Agent holding bridge deferred creation lock. */
  916. AST_MUTEX_DEFINE_STATIC(agent_holding_lock);
  917. /*!
  918. * \internal
  919. * \brief Callback to clear AGENT_STATUS on the caller channel.
  920. *
  921. * \param bridge_channel Which channel to operate on.
  922. * \param payload Data to pass to the callback. (NULL if none).
  923. * \param payload_size Size of the payload if payload is non-NULL. A number otherwise.
  924. *
  925. * \note The payload MUST NOT have any resources that need to be freed.
  926. *
  927. * \return Nothing
  928. */
  929. static void clear_agent_status(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
  930. {
  931. pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", NULL);
  932. }
  933. /*!
  934. * \internal
  935. * \brief Connect the agent with the waiting caller.
  936. * \since 12.0.0
  937. *
  938. * \param bridge_channel Agent channel connecting to the caller.
  939. * \param agent Which agent is connecting to the caller.
  940. *
  941. * \note The agent is locked on entry and not locked on exit.
  942. *
  943. * \return Nothing
  944. */
  945. static void agent_connect_caller(struct ast_bridge_channel *bridge_channel, struct agent_pvt *agent)
  946. {
  947. struct ast_bridge *caller_bridge;
  948. int record_agent_calls;
  949. int res;
  950. record_agent_calls = agent->cfg->record_agent_calls;
  951. caller_bridge = agent->caller_bridge;
  952. agent->caller_bridge = NULL;
  953. agent->state = AGENT_STATE_ON_CALL;
  954. time(&agent->call_start);
  955. agent_unlock(agent);
  956. if (!caller_bridge) {
  957. /* Reset agent. */
  958. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  959. AST_CAUSE_NORMAL_CLEARING);
  960. return;
  961. }
  962. res = ast_bridge_move(caller_bridge, bridge_channel->bridge, bridge_channel->chan,
  963. NULL, 0);
  964. if (res) {
  965. /* Reset agent. */
  966. ast_bridge_destroy(caller_bridge, 0);
  967. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  968. AST_CAUSE_NORMAL_CLEARING);
  969. return;
  970. }
  971. res = ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_ANSWER, NULL, 0)
  972. || ast_bridge_channel_write_callback(bridge_channel, 0, clear_agent_status, NULL, 0);
  973. if (res) {
  974. /* Reset agent. */
  975. ast_bridge_destroy(caller_bridge, 0);
  976. return;
  977. }
  978. if (record_agent_calls) {
  979. struct ast_bridge_features_automixmonitor options = {
  980. .start_stop = AUTO_MONITOR_START,
  981. };
  982. /*
  983. * The agent is in the new bridge so we can invoke the
  984. * mixmonitor hook to only start recording.
  985. */
  986. ast_bridge_features_do(AST_BRIDGE_BUILTIN_AUTOMIXMON, bridge_channel, &options);
  987. }
  988. ao2_t_ref(caller_bridge, -1, "Agent successfully in caller_bridge");
  989. }
  990. static int bridge_agent_hold_ack(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  991. {
  992. struct agent_pvt *agent = hook_pvt;
  993. agent_lock(agent);
  994. switch (agent->state) {
  995. case AGENT_STATE_CALL_WAIT_ACK:
  996. /* Connect to caller now. */
  997. ast_debug(1, "Agent %s: Acked call.\n", agent->username);
  998. agent_connect_caller(bridge_channel, agent);/* Will unlock agent. */
  999. return 0;
  1000. default:
  1001. break;
  1002. }
  1003. agent_unlock(agent);
  1004. return 0;
  1005. }
  1006. static int bridge_agent_hold_heartbeat(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  1007. {
  1008. struct agent_pvt *agent = hook_pvt;
  1009. int probation_timedout = 0;
  1010. int ack_timedout = 0;
  1011. int wrapup_timedout = 0;
  1012. int deferred_logoff;
  1013. unsigned int wrapup_time;
  1014. unsigned int auto_logoff;
  1015. agent_lock(agent);
  1016. deferred_logoff = agent->deferred_logoff;
  1017. if (deferred_logoff) {
  1018. agent->state = AGENT_STATE_LOGGING_OUT;
  1019. }
  1020. switch (agent->state) {
  1021. case AGENT_STATE_PROBATION_WAIT:
  1022. probation_timedout =
  1023. LOGIN_WAIT_TIMEOUT_TIME <= (time(NULL) - agent->probation_start);
  1024. if (probation_timedout) {
  1025. /* Now ready for a caller. */
  1026. agent->state = AGENT_STATE_READY_FOR_CALL;
  1027. agent->devstate = AST_DEVICE_NOT_INUSE;
  1028. }
  1029. break;
  1030. case AGENT_STATE_CALL_WAIT_ACK:
  1031. /* Check ack call time. */
  1032. auto_logoff = agent->cfg->auto_logoff;
  1033. if (ast_test_flag(agent, AGENT_FLAG_AUTO_LOGOFF)) {
  1034. auto_logoff = agent->override_auto_logoff;
  1035. }
  1036. if (auto_logoff) {
  1037. auto_logoff *= 1000;
  1038. ack_timedout = ast_tvdiff_ms(ast_tvnow(), agent->ack_time) > auto_logoff;
  1039. if (ack_timedout) {
  1040. agent->state = AGENT_STATE_LOGGING_OUT;
  1041. }
  1042. }
  1043. break;
  1044. case AGENT_STATE_CALL_WRAPUP:
  1045. /* Check wrapup time. */
  1046. wrapup_time = agent->cfg->wrapup_time;
  1047. if (ast_test_flag(agent, AGENT_FLAG_WRAPUP_TIME)) {
  1048. wrapup_time = agent->override_wrapup_time;
  1049. }
  1050. wrapup_timedout = ast_tvdiff_ms(ast_tvnow(), agent->last_disconnect) > wrapup_time;
  1051. if (wrapup_timedout) {
  1052. agent->state = AGENT_STATE_READY_FOR_CALL;
  1053. agent->devstate = AST_DEVICE_NOT_INUSE;
  1054. }
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. agent_unlock(agent);
  1060. if (deferred_logoff) {
  1061. ast_debug(1, "Agent %s: Deferred logoff.\n", agent->username);
  1062. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  1063. AST_CAUSE_NORMAL_CLEARING);
  1064. } else if (probation_timedout) {
  1065. ast_debug(1, "Agent %s: Login complete.\n", agent->username);
  1066. agent_devstate_changed(agent->username);
  1067. } else if (ack_timedout) {
  1068. ast_debug(1, "Agent %s: Ack call timeout.\n", agent->username);
  1069. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  1070. AST_CAUSE_NORMAL_CLEARING);
  1071. } else if (wrapup_timedout) {
  1072. ast_debug(1, "Agent %s: Wrapup timeout. Ready for new call.\n", agent->username);
  1073. agent_devstate_changed(agent->username);
  1074. }
  1075. return 0;
  1076. }
  1077. static void agent_after_bridge_cb(struct ast_channel *chan, void *data);
  1078. static void agent_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data);
  1079. /*!
  1080. * \internal
  1081. * \brief ast_bridge agent_hold push method.
  1082. * \since 12.0.0
  1083. *
  1084. * \param self Bridge to operate upon.
  1085. * \param bridge_channel Bridge channel to push.
  1086. * \param swap Bridge channel to swap places with if not NULL.
  1087. *
  1088. * \note On entry, self is already locked.
  1089. *
  1090. * \retval 0 on success
  1091. * \retval -1 on failure
  1092. */
  1093. static int bridge_agent_hold_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  1094. {
  1095. int res = 0;
  1096. unsigned int wrapup_time;
  1097. char dtmf[AST_FEATURE_MAX_LEN];
  1098. struct ast_channel *chan;
  1099. const char *moh_class;
  1100. RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
  1101. chan = bridge_channel->chan;
  1102. agent = ao2_find(agents, swap ? swap->chan : chan, 0);
  1103. if (!agent) {
  1104. /* Could not find the agent. */
  1105. return -1;
  1106. }
  1107. /* Setup agent entertainment */
  1108. agent_lock(agent);
  1109. moh_class = ast_strdupa(agent->cfg->moh);
  1110. agent_unlock(agent);
  1111. res |= ast_channel_add_bridge_role(chan, "holding_participant");
  1112. res |= ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "musiconhold");
  1113. res |= ast_channel_set_bridge_role_option(chan, "holding_participant", "moh_class", moh_class);
  1114. /* Add DTMF acknowledge hook. */
  1115. dtmf[0] = '\0';
  1116. agent_lock(agent);
  1117. if (ast_test_flag(agent, AGENT_FLAG_ACK_CALL)
  1118. ? agent->override_ack_call : agent->cfg->ack_call) {
  1119. const char *dtmf_accept;
  1120. dtmf_accept = ast_test_flag(agent, AGENT_FLAG_DTMF_ACCEPT)
  1121. ? agent->override_dtmf_accept : agent->cfg->dtmf_accept;
  1122. ast_copy_string(dtmf, dtmf_accept, sizeof(dtmf));
  1123. }
  1124. agent_unlock(agent);
  1125. if (!ast_strlen_zero(dtmf)) {
  1126. ao2_ref(agent, +1);
  1127. if (ast_bridge_dtmf_hook(bridge_channel->features, dtmf, bridge_agent_hold_ack,
  1128. agent, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  1129. ao2_ref(agent, -1);
  1130. res = -1;
  1131. }
  1132. }
  1133. /* Add heartbeat interval hook. */
  1134. ao2_ref(agent, +1);
  1135. if (ast_bridge_interval_hook(bridge_channel->features, 0, 1000,
  1136. bridge_agent_hold_heartbeat, agent, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  1137. ao2_ref(agent, -1);
  1138. res = -1;
  1139. }
  1140. res |= ast_bridge_base_v_table.push(self, bridge_channel, swap);
  1141. if (res) {
  1142. ast_channel_remove_bridge_role(chan, "holding_participant");
  1143. return -1;
  1144. }
  1145. if (swap) {
  1146. res = ast_bridge_set_after_callback(chan, agent_after_bridge_cb,
  1147. agent_after_bridge_cb_failed, chan);
  1148. if (res) {
  1149. ast_channel_remove_bridge_role(chan, "holding_participant");
  1150. return -1;
  1151. }
  1152. agent_lock(agent);
  1153. ast_channel_unref(agent->logged);
  1154. agent->logged = ast_channel_ref(chan);
  1155. agent_unlock(agent);
  1156. /*
  1157. * Kick the channel out so it can come back in fully controlled.
  1158. * Otherwise, the after bridge callback will linger and the
  1159. * agent will have some slightly different behavior in corner
  1160. * cases.
  1161. */
  1162. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
  1163. AST_CAUSE_NORMAL_CLEARING);
  1164. return 0;
  1165. }
  1166. agent_lock(agent);
  1167. switch (agent->state) {
  1168. case AGENT_STATE_LOGGED_OUT:
  1169. /*!
  1170. * \todo XXX the login probation time should be only if it is needed.
  1171. *
  1172. * Need to determine if there are any local channels that can
  1173. * optimize and wait until they actually do before leaving the
  1174. * AGENT_STATE_PROBATION_WAIT state. For now, the blind
  1175. * timer of LOGIN_WAIT_TIMEOUT_TIME will do.
  1176. */
  1177. /*
  1178. * Start the login probation timer.
  1179. *
  1180. * We cannot handle an agent local channel optimization when the
  1181. * agent is on a call. The optimization may kick the agent
  1182. * channel we know about out of the call without our being able
  1183. * to switch to the replacement channel. Get any agent local
  1184. * channel optimization out of the way while the agent is in the
  1185. * holding bridge.
  1186. */
  1187. time(&agent->probation_start);
  1188. agent->state = AGENT_STATE_PROBATION_WAIT;
  1189. agent_unlock(agent);
  1190. break;
  1191. case AGENT_STATE_PROBATION_WAIT:
  1192. /* Restart the probation timer. */
  1193. time(&agent->probation_start);
  1194. agent_unlock(agent);
  1195. break;
  1196. case AGENT_STATE_READY_FOR_CALL:
  1197. /*
  1198. * Likely someone manually kicked us out of the holding bridge
  1199. * and we came right back in.
  1200. */
  1201. agent_unlock(agent);
  1202. break;
  1203. default:
  1204. /* Unexpected agent state. */
  1205. ast_assert(0);
  1206. /* Fall through */
  1207. case AGENT_STATE_CALL_PRESENT:
  1208. case AGENT_STATE_CALL_WAIT_ACK:
  1209. agent->state = AGENT_STATE_READY_FOR_CALL;
  1210. agent->devstate = AST_DEVICE_NOT_INUSE;
  1211. agent_unlock(agent);
  1212. ast_debug(1, "Agent %s: Call abort recovery complete.\n", agent->username);
  1213. agent_devstate_changed(agent->username);
  1214. break;
  1215. case AGENT_STATE_ON_CALL:
  1216. case AGENT_STATE_CALL_WRAPUP:
  1217. wrapup_time = agent->cfg->wrapup_time;
  1218. if (ast_test_flag(agent, AGENT_FLAG_WRAPUP_TIME)) {
  1219. wrapup_time = agent->override_wrapup_time;
  1220. }
  1221. if (wrapup_time) {
  1222. agent->state = AGENT_STATE_CALL_WRAPUP;
  1223. } else {
  1224. agent->state = AGENT_STATE_READY_FOR_CALL;
  1225. agent->devstate = AST_DEVICE_NOT_INUSE;
  1226. }
  1227. agent_unlock(agent);
  1228. if (!wrapup_time) {
  1229. /* No wrapup time. */
  1230. ast_debug(1, "Agent %s: Ready for new call.\n", agent->username);
  1231. agent_devstate_changed(agent->username);
  1232. }
  1233. break;
  1234. }
  1235. return 0;
  1236. }
  1237. /*!
  1238. * \internal
  1239. * \brief ast_bridge agent_hold pull method.
  1240. *
  1241. * \param self Bridge to operate upon.
  1242. * \param bridge_channel Bridge channel to pull.
  1243. *
  1244. * \details
  1245. * Remove any channel hooks controlled by the bridge. Release
  1246. * any resources held by bridge_channel->bridge_pvt and release
  1247. * bridge_channel->bridge_pvt.
  1248. *
  1249. * \note On entry, self is already locked.
  1250. *
  1251. * \return Nothing
  1252. */
  1253. static void bridge_agent_hold_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  1254. {
  1255. ast_channel_remove_bridge_role(bridge_channel->chan, "holding_participant");
  1256. ast_bridge_base_v_table.pull(self, bridge_channel);
  1257. }
  1258. /*!
  1259. * \brief The bridge is being dissolved.
  1260. *
  1261. * \param self Bridge to operate upon.
  1262. *
  1263. * \details
  1264. * The bridge is being dissolved. Remove any external
  1265. * references to the bridge so it can be destroyed.
  1266. *
  1267. * \note On entry, self must NOT be locked.
  1268. *
  1269. * \return Nothing
  1270. */
  1271. static void bridge_agent_hold_dissolving(struct ast_bridge *self)
  1272. {
  1273. ao2_global_obj_release(agent_holding);
  1274. ast_bridge_base_v_table.dissolving(self);
  1275. }
  1276. static struct ast_bridge_methods bridge_agent_hold_v_table;
  1277. static struct ast_bridge *bridge_agent_hold_new(void)
  1278. {
  1279. struct ast_bridge *bridge;
  1280. bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_agent_hold_v_table);
  1281. bridge = bridge_base_init(bridge, AST_BRIDGE_CAPABILITY_HOLDING,
  1282. AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
  1283. | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED,
  1284. "AgentPool", NULL, NULL);
  1285. bridge = bridge_register(bridge);
  1286. return bridge;
  1287. }
  1288. static void bridge_init_agent_hold(void)
  1289. {
  1290. /* Setup bridge agent_hold subclass v_table. */
  1291. bridge_agent_hold_v_table = ast_bridge_base_v_table;
  1292. bridge_agent_hold_v_table.name = "agent_hold";
  1293. bridge_agent_hold_v_table.dissolving = bridge_agent_hold_dissolving;
  1294. bridge_agent_hold_v_table.push = bridge_agent_hold_push;
  1295. bridge_agent_hold_v_table.pull = bridge_agent_hold_pull;
  1296. }
  1297. static int bridge_agent_hold_deferred_create(void)
  1298. {
  1299. RAII_VAR(struct ast_bridge *, holding, ao2_global_obj_ref(agent_holding), ao2_cleanup);
  1300. if (!holding) {
  1301. ast_mutex_lock(&agent_holding_lock);
  1302. holding = ao2_global_obj_ref(agent_holding);
  1303. if (!holding) {
  1304. holding = bridge_agent_hold_new();
  1305. ao2_global_obj_replace_unref(agent_holding, holding);
  1306. }
  1307. ast_mutex_unlock(&agent_holding_lock);
  1308. if (!holding) {
  1309. ast_log(LOG_ERROR, "Could not create agent holding bridge.\n");
  1310. return -1;
  1311. }
  1312. }
  1313. return 0;
  1314. }
  1315. static void send_agent_login(struct ast_channel *chan, const char *agent)
  1316. {
  1317. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  1318. ast_assert(agent != NULL);
  1319. blob = ast_json_pack("{s: s}",
  1320. "agent", agent);
  1321. if (!blob) {
  1322. return;
  1323. }
  1324. ast_channel_publish_cached_blob(chan, ast_channel_agent_login_type(), blob);
  1325. }
  1326. static void send_agent_logoff(struct ast_channel *chan, const char *agent, long logintime)
  1327. {
  1328. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  1329. ast_assert(agent != NULL);
  1330. blob = ast_json_pack("{s: s, s: i}",
  1331. "agent", agent,
  1332. "logintime", logintime);
  1333. if (!blob) {
  1334. return;
  1335. }
  1336. ast_channel_publish_cached_blob(chan, ast_channel_agent_logoff_type(), blob);
  1337. }
  1338. /*!
  1339. * \internal
  1340. * \brief Logout the agent.
  1341. * \since 12.0.0
  1342. *
  1343. * \param agent Which agent logging out.
  1344. *
  1345. * \note On entry agent is already locked. On exit it is no longer locked.
  1346. *
  1347. * \return Nothing
  1348. */
  1349. static void agent_logout(struct agent_pvt *agent)
  1350. {
  1351. struct ast_channel *logged;
  1352. struct ast_bridge *caller_bridge;
  1353. long time_logged_in;
  1354. time_logged_in = time(NULL) - agent->login_start;
  1355. logged = agent->logged;
  1356. agent->logged = NULL;
  1357. caller_bridge = agent->caller_bridge;
  1358. agent->caller_bridge = NULL;
  1359. agent->state = AGENT_STATE_LOGGED_OUT;
  1360. agent->devstate = AST_DEVICE_UNAVAILABLE;
  1361. ast_clear_flag(agent, AST_FLAGS_ALL);
  1362. agent_unlock(agent);
  1363. agent_devstate_changed(agent->username);
  1364. if (caller_bridge) {
  1365. ast_bridge_destroy(caller_bridge, 0);
  1366. }
  1367. ast_channel_lock(logged);
  1368. send_agent_logoff(logged, agent->username, time_logged_in);
  1369. ast_channel_unlock(logged);
  1370. ast_verb(2, "Agent '%s' logged out. Logged in for %ld seconds.\n",
  1371. agent->username, time_logged_in);
  1372. ast_channel_unref(logged);
  1373. }
  1374. /*!
  1375. * \internal
  1376. * \brief Agent driver loop.
  1377. * \since 12.0.0
  1378. *
  1379. * \param agent Which agent.
  1380. * \param logged The logged in channel.
  1381. *
  1382. * \return Nothing
  1383. */
  1384. static void agent_run(struct agent_pvt *agent, struct ast_channel *logged)
  1385. {
  1386. struct ast_bridge_features features;
  1387. if (ast_bridge_features_init(&features)) {
  1388. ast_channel_hangupcause_set(logged, AST_CAUSE_NORMAL_CLEARING);
  1389. goto agent_run_cleanup;
  1390. }
  1391. for (;;) {
  1392. struct agents_cfg *cfgs;
  1393. struct agent_cfg *cfg_new;
  1394. struct agent_cfg *cfg_old;
  1395. struct ast_bridge *holding;
  1396. struct ast_bridge *caller_bridge;
  1397. ast_channel_hangupcause_set(logged, AST_CAUSE_NORMAL_CLEARING);
  1398. holding = ao2_global_obj_ref(agent_holding);
  1399. if (!holding) {
  1400. ast_debug(1, "Agent %s: Someone destroyed the agent holding bridge.\n",
  1401. agent->username);
  1402. break;
  1403. }
  1404. /*
  1405. * When the agent channel leaves the bridging system we usually
  1406. * want to put the agent back into the holding bridge for the
  1407. * next caller.
  1408. */
  1409. ast_bridge_join(holding, logged, NULL, &features, NULL,
  1410. AST_BRIDGE_JOIN_PASS_REFERENCE);
  1411. if (logged != agent->logged) {
  1412. /* This channel is no longer the logged in agent. */
  1413. break;
  1414. }
  1415. if (agent->dead) {
  1416. /* The agent is no longer configured. */
  1417. break;
  1418. }
  1419. /* Update the agent's config before rejoining the holding bridge. */
  1420. cfgs = ao2_global_obj_ref(cfg_handle);
  1421. if (!cfgs) {
  1422. /* There is no agent configuration. All agents were destroyed. */
  1423. break;
  1424. }
  1425. cfg_new = ao2_find(cfgs->agents, agent->username, OBJ_KEY);
  1426. ao2_ref(cfgs, -1);
  1427. if (!cfg_new) {
  1428. /* The agent is no longer configured. */
  1429. break;
  1430. }
  1431. agent_lock(agent);
  1432. cfg_old = agent->cfg;
  1433. agent->cfg = cfg_new;
  1434. agent->last_disconnect = ast_tvnow();
  1435. /* Clear out any caller bridge before rejoining the holding bridge. */
  1436. caller_bridge = agent->caller_bridge;
  1437. agent->caller_bridge = NULL;
  1438. agent_unlock(agent);
  1439. ao2_ref(cfg_old, -1);
  1440. if (caller_bridge) {
  1441. ast_bridge_destroy(caller_bridge, 0);
  1442. }
  1443. if (agent->state == AGENT_STATE_LOGGING_OUT
  1444. || agent->deferred_logoff
  1445. || ast_check_hangup_locked(logged)) {
  1446. /* The agent was requested to logout or hungup. */
  1447. break;
  1448. }
  1449. /*
  1450. * It is safe to access agent->waiting_colp without a lock. It
  1451. * is only setup on agent login and not changed.
  1452. */
  1453. ast_channel_update_connected_line(logged, &agent->waiting_colp, NULL);
  1454. }
  1455. ast_bridge_features_cleanup(&features);
  1456. agent_run_cleanup:
  1457. agent_lock(agent);
  1458. if (logged != agent->logged) {
  1459. /*
  1460. * We are no longer the agent channel because of local channel
  1461. * optimization.
  1462. */
  1463. agent_unlock(agent);
  1464. ast_debug(1, "Agent %s: Channel %s is no longer the agent.\n",
  1465. agent->username, ast_channel_name(logged));
  1466. return;
  1467. }
  1468. agent_logout(agent);
  1469. }
  1470. static void agent_after_bridge_cb(struct ast_channel *chan, void *data)
  1471. {
  1472. struct agent_pvt *agent;
  1473. agent = ao2_find(agents, chan, 0);
  1474. if (!agent) {
  1475. return;
  1476. }
  1477. ast_debug(1, "Agent %s: New agent channel %s.\n",
  1478. agent->username, ast_channel_name(chan));
  1479. agent_run(agent, chan);
  1480. ao2_ref(agent, -1);
  1481. }
  1482. static void agent_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
  1483. {
  1484. struct ast_channel *chan = data;
  1485. struct agent_pvt *agent;
  1486. agent = ao2_find(agents, chan, 0);
  1487. if (!agent) {
  1488. return;
  1489. }
  1490. ast_log(LOG_WARNING, "Agent %s: Forced logout. Lost control of %s because: %s\n",
  1491. agent->username, ast_channel_name(chan),
  1492. ast_bridge_after_cb_reason_string(reason));
  1493. agent_lock(agent);
  1494. agent_logout(agent);
  1495. ao2_ref(agent, -1);
  1496. }
  1497. /*!
  1498. * \internal
  1499. * \brief Get the lock on the agent bridge_channel and return it.
  1500. * \since 12.0.0
  1501. *
  1502. * \param agent Whose bridge_channel to get.
  1503. *
  1504. * \retval bridge_channel on success (Reffed and locked).
  1505. * \retval NULL on error.
  1506. */
  1507. static struct ast_bridge_channel *agent_bridge_channel_get_lock(struct agent_pvt *agent)
  1508. {
  1509. struct ast_channel *logged;
  1510. struct ast_bridge_channel *bc;
  1511. for (;;) {
  1512. agent_lock(agent);
  1513. logged = agent->logged;
  1514. if (!logged) {
  1515. agent_unlock(agent);
  1516. return NULL;
  1517. }
  1518. ast_channel_ref(logged);
  1519. agent_unlock(agent);
  1520. ast_channel_lock(logged);
  1521. bc = ast_channel_get_bridge_channel(logged);
  1522. ast_channel_unlock(logged);
  1523. ast_channel_unref(logged);
  1524. if (!bc) {
  1525. if (agent->logged != logged) {
  1526. continue;
  1527. }
  1528. return NULL;
  1529. }
  1530. ast_bridge_channel_lock(bc);
  1531. if (bc->chan != logged || agent->logged != logged) {
  1532. ast_bridge_channel_unlock(bc);
  1533. ao2_ref(bc, -1);
  1534. continue;
  1535. }
  1536. return bc;
  1537. }
  1538. }
  1539. static void caller_abort_agent(struct agent_pvt *agent)
  1540. {
  1541. struct ast_bridge_channel *logged;
  1542. logged = agent_bridge_channel_get_lock(agent);
  1543. if (!logged) {
  1544. struct ast_bridge *caller_bridge;
  1545. ast_debug(1, "Agent '%s' no longer logged in.\n", agent->username);
  1546. agent_lock(agent);
  1547. caller_bridge = agent->caller_bridge;
  1548. agent->caller_bridge = NULL;
  1549. agent_unlock(agent);
  1550. if (caller_bridge) {
  1551. ast_bridge_destroy(caller_bridge, 0);
  1552. }
  1553. return;
  1554. }
  1555. /* Kick the agent out of the holding bridge to reset it. */
  1556. ast_bridge_channel_leave_bridge_nolock(logged, BRIDGE_CHANNEL_STATE_END,
  1557. AST_CAUSE_NORMAL_CLEARING);
  1558. ast_bridge_channel_unlock(logged);
  1559. }
  1560. static int caller_safety_timeout(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  1561. {
  1562. struct agent_pvt *agent = hook_pvt;
  1563. if (agent->state == AGENT_STATE_CALL_PRESENT) {
  1564. ast_log(LOG_WARNING, "Agent '%s' process did not respond. Safety timeout.\n",
  1565. agent->username);
  1566. pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
  1567. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
  1568. caller_abort_agent(agent);
  1569. }
  1570. return -1;
  1571. }
  1572. static void agent_alert(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
  1573. {
  1574. const char *agent_id = payload;
  1575. const char *playfile;
  1576. const char *dtmf_accept;
  1577. struct agent_pvt *agent;
  1578. int digit;
  1579. char dtmf[2];
  1580. agent = ao2_find(agents, agent_id, OBJ_KEY);
  1581. if (!agent) {
  1582. ast_debug(1, "Agent '%s' does not exist. Where did it go?\n", agent_id);
  1583. return;
  1584. }
  1585. /* Change holding bridge participant role's idle mode to silence */
  1586. ast_bridge_channel_lock_bridge(bridge_channel);
  1587. ast_bridge_channel_clear_roles(bridge_channel);
  1588. ast_channel_set_bridge_role_option(bridge_channel->chan, "holding_participant", "idle_mode", "silence");
  1589. ast_bridge_channel_establish_roles(bridge_channel);
  1590. ast_bridge_unlock(bridge_channel->bridge);
  1591. agent_lock(agent);
  1592. playfile = ast_strdupa(agent->cfg->beep_sound);
  1593. /* Determine which DTMF digits interrupt the alerting signal. */
  1594. if (ast_test_flag(agent, AGENT_FLAG_ACK_CALL)
  1595. ? agent->override_ack_call : agent->cfg->ack_call) {
  1596. dtmf_accept = ast_test_flag(agent, AGENT_FLAG_DTMF_ACCEPT)
  1597. ? agent->override_dtmf_accept : agent->cfg->dtmf_accept;
  1598. /* Only the first digit of the ack will stop playback. */
  1599. dtmf[0] = *dtmf_accept;
  1600. dtmf[1] = '\0';
  1601. dtmf_accept = dtmf;
  1602. } else {
  1603. dtmf_accept = NULL;
  1604. }
  1605. agent_unlock(agent);
  1606. /* Alert the agent. */
  1607. digit = ast_stream_and_wait(bridge_channel->chan, playfile,
  1608. ast_strlen_zero(dtmf_accept) ? AST_DIGIT_ANY : dtmf_accept);
  1609. ast_stopstream(bridge_channel->chan);
  1610. agent_lock(agent);
  1611. switch (agent->state) {
  1612. case AGENT_STATE_CALL_PRESENT:
  1613. if (!ast_strlen_zero(dtmf_accept)) {
  1614. agent->state = AGENT_STATE_CALL_WAIT_ACK;
  1615. agent->ack_time = ast_tvnow();
  1616. if (0 < digit) {
  1617. /* Playback was interrupted by a digit. */
  1618. agent_unlock(agent);
  1619. ao2_ref(agent, -1);
  1620. ast_bridge_channel_feature_digit(bridge_channel, digit);
  1621. return;
  1622. }
  1623. break;
  1624. }
  1625. /* Connect to caller now. */
  1626. ast_debug(1, "Agent %s: Immediately connecting to call.\n", agent->username);
  1627. agent_connect_caller(bridge_channel, agent);/* Will unlock agent. */
  1628. ao2_ref(agent, -1);
  1629. return;
  1630. default:
  1631. break;
  1632. }
  1633. agent_unlock(agent);
  1634. ao2_ref(agent, -1);
  1635. }
  1636. static int send_alert_to_agent(struct ast_bridge_channel *bridge_channel, const char *agent_id)
  1637. {
  1638. return ast_bridge_channel_queue_callback(bridge_channel,
  1639. AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA, agent_alert, agent_id, strlen(agent_id) + 1);
  1640. }
  1641. static int send_colp_to_agent(struct ast_bridge_channel *bridge_channel, struct ast_party_connected_line *connected)
  1642. {
  1643. struct ast_set_party_connected_line update = {
  1644. .id.name = 1,
  1645. .id.number = 1,
  1646. .id.subaddress = 1,
  1647. };
  1648. unsigned char data[1024]; /* This should be large enough */
  1649. size_t datalen;
  1650. datalen = ast_connected_line_build_data(data, sizeof(data), connected, &update);
  1651. if (datalen == (size_t) -1) {
  1652. return 0;
  1653. }
  1654. return ast_bridge_channel_queue_control_data(bridge_channel,
  1655. AST_CONTROL_CONNECTED_LINE, data, datalen);
  1656. }
  1657. /*!
  1658. * \internal
  1659. * \brief Caller joined the bridge event callback.
  1660. *
  1661. * \param bridge_channel Channel executing the feature
  1662. * \param hook_pvt Private data passed in when the hook was created
  1663. *
  1664. * \retval 0 Keep the callback hook.
  1665. * \retval -1 Remove the callback hook.
  1666. */
  1667. static int caller_joined_bridge(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  1668. {
  1669. struct agent_pvt *agent = hook_pvt;
  1670. struct ast_bridge_channel *logged;
  1671. int res;
  1672. logged = agent_bridge_channel_get_lock(agent);
  1673. if (!logged) {
  1674. ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
  1675. pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_LOGGED_IN");
  1676. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
  1677. caller_abort_agent(agent);
  1678. return -1;
  1679. }
  1680. res = send_alert_to_agent(logged, agent->username);
  1681. ast_bridge_channel_unlock(logged);
  1682. ao2_ref(logged, -1);
  1683. if (res) {
  1684. ast_verb(3, "Agent '%s': Failed to alert the agent.\n", agent->username);
  1685. pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
  1686. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
  1687. caller_abort_agent(agent);
  1688. return -1;
  1689. }
  1690. pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_CONNECTED");
  1691. ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
  1692. return -1;
  1693. }
  1694. /*!
  1695. * \brief Dialplan AgentRequest application to locate an agent to talk with.
  1696. *
  1697. * \param chan Channel wanting to talk with an agent.
  1698. * \param data Application parameters
  1699. *
  1700. * \retval 0 To continue in dialplan.
  1701. * \retval -1 To hangup.
  1702. */
  1703. static int agent_request_exec(struct ast_channel *chan, const char *data)
  1704. {
  1705. struct ast_bridge *caller_bridge;
  1706. struct ast_bridge_channel *logged;
  1707. char *parse;
  1708. int res;
  1709. struct ast_bridge_features caller_features;
  1710. struct ast_party_connected_line connected;
  1711. AST_DECLARE_APP_ARGS(args,
  1712. AST_APP_ARG(agent_id);
  1713. AST_APP_ARG(other); /* Any remaining unused arguments */
  1714. );
  1715. RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
  1716. if (bridge_agent_hold_deferred_create()) {
  1717. return -1;
  1718. }
  1719. parse = ast_strdupa(data);
  1720. AST_STANDARD_APP_ARGS(args, parse);
  1721. if (ast_strlen_zero(args.agent_id)) {
  1722. ast_log(LOG_WARNING, "AgentRequest requires an AgentId\n");
  1723. return -1;
  1724. }
  1725. /* Find the agent. */
  1726. agent = ao2_find(agents, args.agent_id, OBJ_KEY);
  1727. if (!agent) {
  1728. ast_verb(3, "Agent '%s' does not exist.\n", args.agent_id);
  1729. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "INVALID");
  1730. return 0;
  1731. }
  1732. if (ast_bridge_features_init(&caller_features)) {
  1733. return -1;
  1734. }
  1735. /* Add safety timeout hook. */
  1736. ao2_ref(agent, +1);
  1737. if (ast_bridge_interval_hook(&caller_features, 0, CALLER_SAFETY_TIMEOUT_TIME,
  1738. caller_safety_timeout, agent, __ao2_cleanup, AST_BRIDGE_HOOK_REMOVE_ON_PULL)) {
  1739. ao2_ref(agent, -1);
  1740. ast_bridge_features_cleanup(&caller_features);
  1741. return -1;
  1742. }
  1743. /* Setup the alert agent on caller joining the bridge hook. */
  1744. ao2_ref(agent, +1);
  1745. if (ast_bridge_join_hook(&caller_features, caller_joined_bridge, agent,
  1746. __ao2_cleanup, 0)) {
  1747. ao2_ref(agent, -1);
  1748. ast_bridge_features_cleanup(&caller_features);
  1749. return -1;
  1750. }
  1751. caller_bridge = ast_bridge_basic_new();
  1752. if (!caller_bridge) {
  1753. ast_bridge_features_cleanup(&caller_features);
  1754. return -1;
  1755. }
  1756. agent_lock(agent);
  1757. switch (agent->state) {
  1758. case AGENT_STATE_LOGGED_OUT:
  1759. case AGENT_STATE_LOGGING_OUT:
  1760. agent_unlock(agent);
  1761. ast_bridge_destroy(caller_bridge, 0);
  1762. ast_bridge_features_cleanup(&caller_features);
  1763. ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
  1764. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "NOT_LOGGED_IN");
  1765. return 0;
  1766. case AGENT_STATE_READY_FOR_CALL:
  1767. ao2_ref(caller_bridge, +1);
  1768. agent->caller_bridge = caller_bridge;
  1769. agent->state = AGENT_STATE_CALL_PRESENT;
  1770. agent->devstate = AST_DEVICE_INUSE;
  1771. break;
  1772. default:
  1773. agent_unlock(agent);
  1774. ast_bridge_destroy(caller_bridge, 0);
  1775. ast_bridge_features_cleanup(&caller_features);
  1776. ast_verb(3, "Agent '%s' is busy.\n", agent->username);
  1777. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "BUSY");
  1778. return 0;
  1779. }
  1780. agent_unlock(agent);
  1781. agent_devstate_changed(agent->username);
  1782. /* Get COLP for agent. */
  1783. ast_party_connected_line_init(&connected);
  1784. ast_channel_lock(chan);
  1785. ast_connected_line_copy_from_caller(&connected, ast_channel_caller(chan));
  1786. ast_channel_unlock(chan);
  1787. logged = agent_bridge_channel_get_lock(agent);
  1788. if (!logged) {
  1789. ast_party_connected_line_free(&connected);
  1790. caller_abort_agent(agent);
  1791. ast_bridge_destroy(caller_bridge, 0);
  1792. ast_bridge_features_cleanup(&caller_features);
  1793. ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
  1794. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "NOT_LOGGED_IN");
  1795. return 0;
  1796. }
  1797. send_colp_to_agent(logged, &connected);
  1798. ast_bridge_channel_unlock(logged);
  1799. ao2_ref(logged, -1);
  1800. ast_party_connected_line_free(&connected);
  1801. if (ast_bridge_join(caller_bridge, chan, NULL, &caller_features, NULL,
  1802. AST_BRIDGE_JOIN_PASS_REFERENCE)) {
  1803. caller_abort_agent(agent);
  1804. ast_verb(3, "Agent '%s': Caller %s failed to join the bridge.\n",
  1805. agent->username, ast_channel_name(chan));
  1806. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ERROR");
  1807. }
  1808. ast_bridge_features_cleanup(&caller_features);
  1809. /* Determine if we need to continue in the dialplan after the bridge. */
  1810. ast_channel_lock(chan);
  1811. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  1812. /*
  1813. * The bridge was broken for a hangup that isn't real.
  1814. * Don't run the h extension, because the channel isn't
  1815. * really hung up. This should really only happen with
  1816. * AST_SOFTHANGUP_ASYNCGOTO.
  1817. */
  1818. res = 0;
  1819. } else {
  1820. res = ast_check_hangup(chan)
  1821. || ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  1822. || ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENT_STATUS"));
  1823. }
  1824. ast_channel_unlock(chan);
  1825. return res ? -1 : 0;
  1826. }
  1827. /*!
  1828. * \internal
  1829. * \brief Get agent config values from the login channel.
  1830. * \since 12.0.0
  1831. *
  1832. * \param agent What to setup channel config values on.
  1833. * \param chan Channel logging in as an agent.
  1834. *
  1835. * \return Nothing
  1836. */
  1837. static void agent_login_channel_config(struct agent_pvt *agent, struct ast_channel *chan)
  1838. {
  1839. struct ast_flags opts = { 0 };
  1840. struct ast_party_connected_line connected;
  1841. unsigned int override_ack_call = 0;
  1842. unsigned int override_auto_logoff = 0;
  1843. unsigned int override_wrapup_time = 0;
  1844. const char *override_dtmf_accept = NULL;
  1845. const char *var;
  1846. ast_party_connected_line_init(&connected);
  1847. /* Get config values from channel. */
  1848. ast_channel_lock(chan);
  1849. ast_party_connected_line_copy(&connected, ast_channel_connected(chan));
  1850. var = pbx_builtin_getvar_helper(chan, "AGENTACKCALL");
  1851. if (!ast_strlen_zero(var)) {
  1852. override_ack_call = ast_true(var) ? 1 : 0;
  1853. ast_set_flag(&opts, AGENT_FLAG_ACK_CALL);
  1854. }
  1855. var = pbx_builtin_getvar_helper(chan, "AGENTACCEPTDTMF");
  1856. if (!ast_strlen_zero(var)) {
  1857. override_dtmf_accept = ast_strdupa(var);
  1858. ast_set_flag(&opts, AGENT_FLAG_DTMF_ACCEPT);
  1859. }
  1860. var = pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF");
  1861. if (!ast_strlen_zero(var)) {
  1862. if (sscanf(var, "%u", &override_auto_logoff) == 1) {
  1863. ast_set_flag(&opts, AGENT_FLAG_AUTO_LOGOFF);
  1864. }
  1865. }
  1866. var = pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME");
  1867. if (!ast_strlen_zero(var)) {
  1868. if (sscanf(var, "%u", &override_wrapup_time) == 1) {
  1869. ast_set_flag(&opts, AGENT_FLAG_WRAPUP_TIME);
  1870. }
  1871. }
  1872. ast_channel_unlock(chan);
  1873. /* Set config values on agent. */
  1874. agent_lock(agent);
  1875. ast_party_connected_line_free(&agent->waiting_colp);
  1876. agent->waiting_colp = connected;
  1877. ast_string_field_set(agent, override_dtmf_accept, override_dtmf_accept);
  1878. ast_copy_flags(agent, &opts, AST_FLAGS_ALL);
  1879. agent->override_auto_logoff = override_auto_logoff;
  1880. agent->override_wrapup_time = override_wrapup_time;
  1881. agent->override_ack_call = override_ack_call;
  1882. agent_unlock(agent);
  1883. }
  1884. enum AGENT_LOGIN_OPT_FLAGS {
  1885. OPT_SILENT = (1 << 0),
  1886. };
  1887. AST_APP_OPTIONS(agent_login_opts, BEGIN_OPTIONS
  1888. AST_APP_OPTION('s', OPT_SILENT),
  1889. END_OPTIONS);
  1890. /*!
  1891. * \brief Dialplan AgentLogin application to log in an agent.
  1892. *
  1893. * \param chan Channel attempting to login as an agent.
  1894. * \param data Application parameters
  1895. *
  1896. * \retval 0 To continue in dialplan.
  1897. * \retval -1 To hangup.
  1898. */
  1899. static int agent_login_exec(struct ast_channel *chan, const char *data)
  1900. {
  1901. char *parse;
  1902. struct ast_flags opts;
  1903. AST_DECLARE_APP_ARGS(args,
  1904. AST_APP_ARG(agent_id);
  1905. AST_APP_ARG(options);
  1906. AST_APP_ARG(other); /* Any remaining unused arguments */
  1907. );
  1908. RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
  1909. if (bridge_agent_hold_deferred_create()) {
  1910. return -1;
  1911. }
  1912. if (ast_channel_state(chan) != AST_STATE_UP && ast_answer(chan)) {
  1913. return -1;
  1914. }
  1915. parse = ast_strdupa(data);
  1916. AST_STANDARD_APP_ARGS(args, parse);
  1917. if (ast_strlen_zero(args.agent_id)) {
  1918. ast_log(LOG_WARNING, "AgentLogin requires an AgentId\n");
  1919. return -1;
  1920. }
  1921. if (ast_app_parse_options(agent_login_opts, &opts, NULL, args.options)) {
  1922. /* General invalid option syntax. */
  1923. return -1;
  1924. }
  1925. /* Find the agent. */
  1926. agent = ao2_find(agents, args.agent_id, OBJ_KEY);
  1927. if (!agent) {
  1928. ast_verb(3, "Agent '%s' does not exist.\n", args.agent_id);
  1929. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "INVALID");
  1930. return 0;
  1931. }
  1932. /* Has someone already logged in as this agent already? */
  1933. agent_lock(agent);
  1934. if (agent->logged) {
  1935. agent_unlock(agent);
  1936. ast_verb(3, "Agent '%s' already logged in.\n", agent->username);
  1937. pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ALREADY_LOGGED_IN");
  1938. return 0;
  1939. }
  1940. agent->logged = ast_channel_ref(chan);
  1941. agent->last_disconnect = ast_tvnow();
  1942. time(&agent->login_start);
  1943. agent->deferred_logoff = 0;
  1944. agent_unlock(agent);
  1945. agent_login_channel_config(agent, chan);
  1946. if (!ast_test_flag(&opts, OPT_SILENT)) {
  1947. ast_stream_and_wait(chan, "agent-loginok", AST_DIGIT_NONE);
  1948. }
  1949. ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", agent->username,
  1950. ast_format_get_name(ast_channel_readformat(chan)),
  1951. ast_format_get_name(ast_channel_writeformat(chan)));
  1952. ast_channel_lock(chan);
  1953. send_agent_login(chan, agent->username);
  1954. ast_channel_unlock(chan);
  1955. agent_run(agent, chan);
  1956. return -1;
  1957. }
  1958. static int agent_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  1959. {
  1960. char *parse;
  1961. struct agent_pvt *agent;
  1962. struct ast_channel *logged;
  1963. AST_DECLARE_APP_ARGS(args,
  1964. AST_APP_ARG(agentid);
  1965. AST_APP_ARG(item);
  1966. );
  1967. buf[0] = '\0';
  1968. parse = ast_strdupa(data ?: "");
  1969. AST_NONSTANDARD_APP_ARGS(args, parse, ':');
  1970. if (ast_strlen_zero(args.agentid)) {
  1971. ast_log(LOG_WARNING, "The AGENT function requires an argument - agentid!\n");
  1972. return -1;
  1973. }
  1974. if (!args.item) {
  1975. args.item = "status";
  1976. }
  1977. agent = ao2_find(agents, args.agentid, OBJ_KEY);
  1978. if (!agent) {
  1979. ast_log(LOG_WARNING, "Agent '%s' not found!\n", args.agentid);
  1980. return -1;
  1981. }
  1982. agent_lock(agent);
  1983. if (!strcasecmp(args.item, "status")) {
  1984. const char *status;
  1985. if (agent->logged) {
  1986. status = "LOGGEDIN";
  1987. } else {
  1988. status = "LOGGEDOUT";
  1989. }
  1990. ast_copy_string(buf, status, len);
  1991. } else if (!strcasecmp(args.item, "name")) {
  1992. ast_copy_string(buf, agent->cfg->full_name, len);
  1993. } else if (!strcasecmp(args.item, "mohclass")) {
  1994. ast_copy_string(buf, agent->cfg->moh, len);
  1995. } else if (!strcasecmp(args.item, "channel")) {
  1996. logged = agent_lock_logged(agent);
  1997. if (logged) {
  1998. char *pos;
  1999. ast_copy_string(buf, ast_channel_name(logged), len);
  2000. ast_channel_unlock(logged);
  2001. ast_channel_unref(logged);
  2002. pos = strrchr(buf, '-');
  2003. if (pos) {
  2004. *pos = '\0';
  2005. }
  2006. }
  2007. } else if (!strcasecmp(args.item, "fullchannel")) {
  2008. logged = agent_lock_logged(agent);
  2009. if (logged) {
  2010. ast_copy_string(buf, ast_channel_name(logged), len);
  2011. ast_channel_unlock(logged);
  2012. ast_channel_unref(logged);
  2013. }
  2014. }
  2015. agent_unlock(agent);
  2016. ao2_ref(agent, -1);
  2017. return 0;
  2018. }
  2019. static struct ast_custom_function agent_function = {
  2020. .name = "AGENT",
  2021. .read = agent_function_read,
  2022. };
  2023. struct agent_complete {
  2024. /*! Nth match to return. */
  2025. int state;
  2026. /*! Which match currently on. */
  2027. int which;
  2028. };
  2029. static int complete_agent_search(void *obj, void *arg, void *data, int flags)
  2030. {
  2031. struct agent_complete *search = data;
  2032. if (++search->which > search->state) {
  2033. return CMP_MATCH;
  2034. }
  2035. return 0;
  2036. }
  2037. static char *complete_agent(const char *word, int state)
  2038. {
  2039. char *ret;
  2040. struct agent_pvt *agent;
  2041. struct agent_complete search = {
  2042. .state = state,
  2043. };
  2044. agent = ao2_callback_data(agents, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
  2045. complete_agent_search, (char *) word, &search);
  2046. if (!agent) {
  2047. return NULL;
  2048. }
  2049. ret = ast_strdup(agent->username);
  2050. ao2_ref(agent, -1);
  2051. return ret;
  2052. }
  2053. static int complete_agent_logoff_search(void *obj, void *arg, void *data, int flags)
  2054. {
  2055. struct agent_pvt *agent = obj;
  2056. struct agent_complete *search = data;
  2057. if (!agent->logged) {
  2058. return 0;
  2059. }
  2060. if (++search->which > search->state) {
  2061. return CMP_MATCH;
  2062. }
  2063. return 0;
  2064. }
  2065. static char *complete_agent_logoff(const char *word, int state)
  2066. {
  2067. char *ret;
  2068. struct agent_pvt *agent;
  2069. struct agent_complete search = {
  2070. .state = state,
  2071. };
  2072. agent = ao2_callback_data(agents, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
  2073. complete_agent_logoff_search, (char *) word, &search);
  2074. if (!agent) {
  2075. return NULL;
  2076. }
  2077. ret = ast_strdup(agent->username);
  2078. ao2_ref(agent, -1);
  2079. return ret;
  2080. }
  2081. static void agent_show_requested(struct ast_cli_args *a, int online_only)
  2082. {
  2083. #define FORMAT_HDR "%-8s %-20s %-11s %-30s %s\n"
  2084. #define FORMAT_ROW "%-8s %-20s %-11s %-30s %s\n"
  2085. struct ao2_iterator iter;
  2086. struct agent_pvt *agent;
  2087. struct ast_str *out = ast_str_alloca(512);
  2088. unsigned int agents_total = 0;
  2089. unsigned int agents_logged_in = 0;
  2090. unsigned int agents_talking = 0;
  2091. ast_cli(a->fd, FORMAT_HDR, "Agent-ID", "Name", "State", "Channel", "Talking with");
  2092. iter = ao2_iterator_init(agents, 0);
  2093. for (; (agent = ao2_iterator_next(&iter)); ao2_ref(agent, -1)) {
  2094. struct ast_channel *logged;
  2095. ++agents_total;
  2096. agent_lock(agent);
  2097. logged = agent_lock_logged(agent);
  2098. if (logged) {
  2099. const char *talking_with;
  2100. ++agents_logged_in;
  2101. talking_with = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
  2102. if (!ast_strlen_zero(talking_with)) {
  2103. ++agents_talking;
  2104. } else {
  2105. talking_with = "";
  2106. }
  2107. ast_str_set(&out, 0, FORMAT_ROW, agent->username, agent->cfg->full_name,
  2108. ast_devstate_str(agent->devstate), ast_channel_name(logged), talking_with);
  2109. ast_channel_unlock(logged);
  2110. ast_channel_unref(logged);
  2111. } else {
  2112. ast_str_set(&out, 0, FORMAT_ROW, agent->username, agent->cfg->full_name,
  2113. ast_devstate_str(agent->devstate), "", "");
  2114. }
  2115. agent_unlock(agent);
  2116. if (!online_only || logged) {
  2117. ast_cli(a->fd, "%s", ast_str_buffer(out));
  2118. }
  2119. }
  2120. ao2_iterator_destroy(&iter);
  2121. ast_cli(a->fd, "\nDefined agents: %u, Logged in: %u, Talking: %u\n",
  2122. agents_total, agents_logged_in, agents_talking);
  2123. #undef FORMAT_HDR
  2124. #undef FORMAT_ROW
  2125. }
  2126. static char *agent_handle_show_online(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2127. {
  2128. switch (cmd) {
  2129. case CLI_INIT:
  2130. e->command = "agent show online";
  2131. e->usage =
  2132. "Usage: agent show online\n"
  2133. " Provides summary information for logged in agents.\n";
  2134. return NULL;
  2135. case CLI_GENERATE:
  2136. return NULL;
  2137. }
  2138. if (a->argc != 3) {
  2139. return CLI_SHOWUSAGE;
  2140. }
  2141. agent_show_requested(a, 1);
  2142. return CLI_SUCCESS;
  2143. }
  2144. static char *agent_handle_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2145. {
  2146. switch (cmd) {
  2147. case CLI_INIT:
  2148. e->command = "agent show all";
  2149. e->usage =
  2150. "Usage: agent show all\n"
  2151. " Provides summary information for all agents.\n";
  2152. return NULL;
  2153. case CLI_GENERATE:
  2154. return NULL;
  2155. }
  2156. if (a->argc != 3) {
  2157. return CLI_SHOWUSAGE;
  2158. }
  2159. agent_show_requested(a, 0);
  2160. return CLI_SUCCESS;
  2161. }
  2162. static char *agent_handle_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2163. {
  2164. struct agent_pvt *agent;
  2165. struct ast_channel *logged;
  2166. struct ast_str *out = ast_str_alloca(4096);
  2167. switch (cmd) {
  2168. case CLI_INIT:
  2169. e->command = "agent show";
  2170. e->usage =
  2171. "Usage: agent show <agent-id>\n"
  2172. " Show information about the <agent-id> agent\n";
  2173. return NULL;
  2174. case CLI_GENERATE:
  2175. if (a->pos == 2) {
  2176. return complete_agent(a->word, a->n);
  2177. }
  2178. return NULL;
  2179. }
  2180. if (a->argc != 3) {
  2181. return CLI_SHOWUSAGE;
  2182. }
  2183. agent = ao2_find(agents, a->argv[2], OBJ_KEY);
  2184. if (!agent) {
  2185. ast_cli(a->fd, "Agent '%s' not found\n", a->argv[2]);
  2186. return CLI_SUCCESS;
  2187. }
  2188. agent_lock(agent);
  2189. logged = agent_lock_logged(agent);
  2190. ast_str_set(&out, 0, "Id: %s\n", agent->username);
  2191. ast_str_append(&out, 0, "Name: %s\n", agent->cfg->full_name);
  2192. ast_str_append(&out, 0, "Beep: %s\n", agent->cfg->beep_sound);
  2193. ast_str_append(&out, 0, "MOH: %s\n", agent->cfg->moh);
  2194. ast_str_append(&out, 0, "RecordCalls: %s\n", AST_CLI_YESNO(agent->cfg->record_agent_calls));
  2195. ast_str_append(&out, 0, "State: %s\n", ast_devstate_str(agent->devstate));
  2196. if (logged) {
  2197. const char *talking_with;
  2198. ast_str_append(&out, 0, "LoggedInChannel: %s\n", ast_channel_name(logged));
  2199. ast_str_append(&out, 0, "LoggedInTime: %ld\n", (long) agent->login_start);
  2200. talking_with = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
  2201. if (!ast_strlen_zero(talking_with)) {
  2202. ast_str_append(&out, 0, "TalkingWith: %s\n", talking_with);
  2203. ast_str_append(&out, 0, "CallStarted: %ld\n", (long) agent->call_start);
  2204. }
  2205. ast_channel_unlock(logged);
  2206. ast_channel_unref(logged);
  2207. }
  2208. agent_unlock(agent);
  2209. ao2_ref(agent, -1);
  2210. ast_cli(a->fd, "%s", ast_str_buffer(out));
  2211. return CLI_SUCCESS;
  2212. }
  2213. static char *agent_handle_logoff_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2214. {
  2215. switch (cmd) {
  2216. case CLI_INIT:
  2217. e->command = "agent logoff";
  2218. e->usage =
  2219. "Usage: agent logoff <agent-id> [soft]\n"
  2220. " Sets an agent as no longer logged in.\n"
  2221. " If 'soft' is specified, do not hangup existing calls.\n";
  2222. return NULL;
  2223. case CLI_GENERATE:
  2224. if (a->pos == 2) {
  2225. return complete_agent_logoff(a->word, a->n);
  2226. } else if (a->pos == 3 && a->n == 0
  2227. && (ast_strlen_zero(a->word)
  2228. || !strncasecmp("soft", a->word, strlen(a->word)))) {
  2229. return ast_strdup("soft");
  2230. }
  2231. return NULL;
  2232. }
  2233. if (a->argc < 3 || 4 < a->argc) {
  2234. return CLI_SHOWUSAGE;
  2235. }
  2236. if (a->argc == 4 && strcasecmp(a->argv[3], "soft")) {
  2237. return CLI_SHOWUSAGE;
  2238. }
  2239. if (!agent_logoff_request(a->argv[2], a->argc == 4)) {
  2240. ast_cli(a->fd, "Logging out %s\n", a->argv[2]);
  2241. }
  2242. return CLI_SUCCESS;
  2243. }
  2244. static struct ast_cli_entry cli_agents[] = {
  2245. AST_CLI_DEFINE(agent_handle_show_online, "Show status of online agents"),
  2246. AST_CLI_DEFINE(agent_handle_show_all, "Show status of all agents"),
  2247. AST_CLI_DEFINE(agent_handle_show_specific, "Show information about an agent"),
  2248. AST_CLI_DEFINE(agent_handle_logoff_cmd, "Sets an agent offline"),
  2249. };
  2250. static int action_agents(struct mansession *s, const struct message *m)
  2251. {
  2252. const char *id = astman_get_header(m, "ActionID");
  2253. char id_text[AST_MAX_BUF];
  2254. struct ao2_iterator iter;
  2255. struct agent_pvt *agent;
  2256. struct ast_str *out = ast_str_alloca(4096);
  2257. int num_agents = 0;
  2258. if (!ast_strlen_zero(id)) {
  2259. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  2260. } else {
  2261. id_text[0] = '\0';
  2262. }
  2263. astman_send_listack(s, m, "Agents will follow", "start");
  2264. iter = ao2_iterator_init(agents, 0);
  2265. for (; (agent = ao2_iterator_next(&iter)); ao2_ref(agent, -1)) {
  2266. struct ast_channel *logged;
  2267. agent_lock(agent);
  2268. logged = agent_lock_logged(agent);
  2269. /*
  2270. * Status Values:
  2271. * AGENT_LOGGEDOFF - Agent isn't logged in
  2272. * AGENT_IDLE - Agent is logged in, and waiting for call
  2273. * AGENT_ONCALL - Agent is logged in, and on a call
  2274. * AGENT_UNKNOWN - Don't know anything about agent. Shouldn't ever get this.
  2275. */
  2276. ast_str_set(&out, 0, "Agent: %s\r\n", agent->username);
  2277. ast_str_append(&out, 0, "Name: %s\r\n", agent->cfg->full_name);
  2278. if (logged) {
  2279. const char *talking_to_chan;
  2280. struct ast_str *logged_headers;
  2281. RAII_VAR(struct ast_channel_snapshot *, logged_snapshot, ast_channel_snapshot_create(logged), ao2_cleanup);
  2282. if (!logged_snapshot
  2283. || !(logged_headers =
  2284. ast_manager_build_channel_state_string(logged_snapshot))) {
  2285. ast_channel_unlock(logged);
  2286. ast_channel_unref(logged);
  2287. agent_unlock(agent);
  2288. continue;
  2289. }
  2290. talking_to_chan = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
  2291. if (!ast_strlen_zero(talking_to_chan)) {
  2292. ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_ONCALL");
  2293. ast_str_append(&out, 0, "TalkingToChan: %s\r\n", talking_to_chan);
  2294. ast_str_append(&out, 0, "CallStarted: %ld\n", (long) agent->call_start);
  2295. } else {
  2296. ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_IDLE");
  2297. }
  2298. ast_str_append(&out, 0, "LoggedInTime: %ld\r\n", (long) agent->login_start);
  2299. ast_str_append(&out, 0, "%s", ast_str_buffer(logged_headers));
  2300. ast_channel_unlock(logged);
  2301. ast_channel_unref(logged);
  2302. ast_free(logged_headers);
  2303. } else {
  2304. ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_LOGGEDOFF");
  2305. }
  2306. agent_unlock(agent);
  2307. astman_append(s, "Event: Agents\r\n"
  2308. "%s%s\r\n",
  2309. ast_str_buffer(out), id_text);
  2310. ++num_agents;
  2311. }
  2312. ao2_iterator_destroy(&iter);
  2313. astman_send_list_complete_start(s, m, "AgentsComplete", num_agents);
  2314. astman_send_list_complete_end(s);
  2315. return 0;
  2316. }
  2317. static int action_agent_logoff(struct mansession *s, const struct message *m)
  2318. {
  2319. const char *agent = astman_get_header(m, "Agent");
  2320. const char *soft_s = astman_get_header(m, "Soft"); /* "true" is don't hangup */
  2321. if (ast_strlen_zero(agent)) {
  2322. astman_send_error(s, m, "No agent specified");
  2323. return 0;
  2324. }
  2325. if (!agent_logoff_request(agent, ast_true(soft_s))) {
  2326. astman_send_ack(s, m, "Agent logged out");
  2327. } else {
  2328. astman_send_error(s, m, "No such agent");
  2329. }
  2330. return 0;
  2331. }
  2332. static int unload_module(void)
  2333. {
  2334. struct ast_bridge *holding;
  2335. /* Unregister dialplan applications */
  2336. ast_unregister_application(app_agent_login);
  2337. ast_unregister_application(app_agent_request);
  2338. /* Unregister dialplan functions */
  2339. ast_custom_function_unregister(&agent_function);
  2340. /* Unregister manager command */
  2341. ast_manager_unregister("Agents");
  2342. ast_manager_unregister("AgentLogoff");
  2343. /* Unregister CLI commands */
  2344. ast_cli_unregister_multiple(cli_agents, ARRAY_LEN(cli_agents));
  2345. ast_devstate_prov_del("Agent");
  2346. /* Destroy agent holding bridge. */
  2347. holding = ao2_global_obj_replace(agent_holding, NULL);
  2348. if (holding) {
  2349. ast_bridge_destroy(holding, 0);
  2350. }
  2351. destroy_config();
  2352. ao2_cleanup(agents);
  2353. agents = NULL;
  2354. return 0;
  2355. }
  2356. static int load_module(void)
  2357. {
  2358. int res = 0;
  2359. agents = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX,
  2360. AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, agent_pvt_sort_cmp, agent_pvt_cmp);
  2361. if (!agents) {
  2362. return AST_MODULE_LOAD_FAILURE;
  2363. }
  2364. /* Init agent holding bridge v_table. */
  2365. bridge_init_agent_hold();
  2366. /* Setup to provide Agent:agent-id device state. */
  2367. res |= ast_devstate_prov_add("Agent", agent_pvt_devstate_get);
  2368. /* CLI Commands */
  2369. res |= ast_cli_register_multiple(cli_agents, ARRAY_LEN(cli_agents));
  2370. /* Manager commands */
  2371. res |= ast_manager_register_xml("Agents", EVENT_FLAG_AGENT, action_agents);
  2372. res |= ast_manager_register_xml("AgentLogoff", EVENT_FLAG_AGENT, action_agent_logoff);
  2373. /* Dialplan Functions */
  2374. res |= ast_custom_function_register(&agent_function);
  2375. /* Dialplan applications */
  2376. res |= ast_register_application_xml(app_agent_login, agent_login_exec);
  2377. res |= ast_register_application_xml(app_agent_request, agent_request_exec);
  2378. if (res) {
  2379. unload_module();
  2380. return AST_MODULE_LOAD_FAILURE;
  2381. }
  2382. if (load_config()) {
  2383. ast_log(LOG_ERROR, "Unable to load config. Not loading module.\n");
  2384. unload_module();
  2385. return AST_MODULE_LOAD_DECLINE;
  2386. }
  2387. return AST_MODULE_LOAD_SUCCESS;
  2388. }
  2389. static int reload(void)
  2390. {
  2391. if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
  2392. /* Just keep the config we already have in place. */
  2393. return -1;
  2394. }
  2395. return 0;
  2396. }
  2397. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Call center agent pool applications",
  2398. .support_level = AST_MODULE_SUPPORT_CORE,
  2399. .load = load_module,
  2400. .unload = unload_module,
  2401. .reload = reload,
  2402. .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
  2403. );