app_agent_pool.c 77 KB

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