chan_agent.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. /*! \file
  19. *
  20. * \brief Implementation of Agents (proxy channel)
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * This file is the implementation of Agents modules.
  25. * It is a dynamic module that is loaded by Asterisk.
  26. * \par See also
  27. * \arg \ref Config_agent
  28. *
  29. * \ingroup channel_drivers
  30. */
  31. /*** MODULEINFO
  32. <depend>chan_local</depend>
  33. <depend>res_monitor</depend>
  34. <support_level>core</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include <sys/socket.h>
  39. #include <fcntl.h>
  40. #include <netdb.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <sys/signal.h>
  44. #include "asterisk/lock.h"
  45. #include "asterisk/channel.h"
  46. #include "asterisk/config.h"
  47. #include "asterisk/module.h"
  48. #include "asterisk/pbx.h"
  49. #include "asterisk/sched.h"
  50. #include "asterisk/io.h"
  51. #include "asterisk/acl.h"
  52. #include "asterisk/callerid.h"
  53. #include "asterisk/file.h"
  54. #include "asterisk/cli.h"
  55. #include "asterisk/app.h"
  56. #include "asterisk/musiconhold.h"
  57. #include "asterisk/manager.h"
  58. #include "asterisk/features.h"
  59. #include "asterisk/utils.h"
  60. #include "asterisk/causes.h"
  61. #include "asterisk/astdb.h"
  62. #include "asterisk/devicestate.h"
  63. #include "asterisk/monitor.h"
  64. #include "asterisk/stringfields.h"
  65. #include "asterisk/event.h"
  66. #include "asterisk/data.h"
  67. /*** DOCUMENTATION
  68. <application name="AgentLogin" language="en_US">
  69. <synopsis>
  70. Call agent login.
  71. </synopsis>
  72. <syntax>
  73. <parameter name="AgentNo" />
  74. <parameter name="options">
  75. <optionlist>
  76. <option name="s">
  77. <para>silent login - do not announce the login ok segment after
  78. agent logged on/off</para>
  79. </option>
  80. </optionlist>
  81. </parameter>
  82. </syntax>
  83. <description>
  84. <para>Asks the agent to login to the system. Always returns <literal>-1</literal>.
  85. While logged in, the agent can receive calls and will hear a <literal>beep</literal>
  86. when a new call comes in. The agent can dump the call by pressing the star key.</para>
  87. </description>
  88. <see-also>
  89. <ref type="application">Queue</ref>
  90. <ref type="application">AddQueueMember</ref>
  91. <ref type="application">RemoveQueueMember</ref>
  92. <ref type="application">PauseQueueMember</ref>
  93. <ref type="application">UnpauseQueueMember</ref>
  94. <ref type="function">AGENT</ref>
  95. <ref type="filename">agents.conf</ref>
  96. <ref type="filename">queues.conf</ref>
  97. </see-also>
  98. </application>
  99. <application name="AgentMonitorOutgoing" language="en_US">
  100. <synopsis>
  101. Record agent's outgoing call.
  102. </synopsis>
  103. <syntax>
  104. <parameter name="options">
  105. <optionlist>
  106. <option name="d">
  107. <para>make the app return <literal>-1</literal> if there is an error condition.</para>
  108. </option>
  109. <option name="c">
  110. <para>change the CDR so that the source of the call is
  111. <literal>Agent/agent_id</literal></para>
  112. </option>
  113. <option name="n">
  114. <para>don't generate the warnings when there is no callerid or the
  115. agentid is not known. It's handy if you want to have one context
  116. for agent and non-agent calls.</para>
  117. </option>
  118. </optionlist>
  119. </parameter>
  120. </syntax>
  121. <description>
  122. <para>Tries to figure out the id of the agent who is placing outgoing call based on
  123. comparison of the callerid of the current interface and the global variable
  124. placed by the AgentCallbackLogin application. That's why it should be used only
  125. with the AgentCallbackLogin app. Uses the monitoring functions in chan_agent
  126. instead of Monitor application. That has to be configured in the
  127. <filename>agents.conf</filename> file.</para>
  128. <para>Normally the app returns <literal>0</literal> unless the options are passed.</para>
  129. </description>
  130. <see-also>
  131. <ref type="filename">agents.conf</ref>
  132. </see-also>
  133. </application>
  134. <function name="AGENT" language="en_US">
  135. <synopsis>
  136. Gets information about an Agent
  137. </synopsis>
  138. <syntax argsep=":">
  139. <parameter name="agentid" required="true" />
  140. <parameter name="item">
  141. <para>The valid items to retrieve are:</para>
  142. <enumlist>
  143. <enum name="status">
  144. <para>(default) The status of the agent (LOGGEDIN | LOGGEDOUT)</para>
  145. </enum>
  146. <enum name="password">
  147. <para>The password of the agent</para>
  148. </enum>
  149. <enum name="name">
  150. <para>The name of the agent</para>
  151. </enum>
  152. <enum name="mohclass">
  153. <para>MusicOnHold class</para>
  154. </enum>
  155. <enum name="channel">
  156. <para>The name of the active channel for the Agent (AgentLogin)</para>
  157. </enum>
  158. <enum name="fullchannel">
  159. <para>The untruncated name of the active channel for the Agent (AgentLogin)</para>
  160. </enum>
  161. </enumlist>
  162. </parameter>
  163. </syntax>
  164. <description></description>
  165. </function>
  166. <manager name="Agents" language="en_US">
  167. <synopsis>
  168. Lists agents and their status.
  169. </synopsis>
  170. <syntax>
  171. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  172. </syntax>
  173. <description>
  174. <para>Will list info about all possible agents.</para>
  175. </description>
  176. </manager>
  177. <manager name="AgentLogoff" language="en_US">
  178. <synopsis>
  179. Sets an agent as no longer logged in.
  180. </synopsis>
  181. <syntax>
  182. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  183. <parameter name="Agent" required="true">
  184. <para>Agent ID of the agent to log off.</para>
  185. </parameter>
  186. <parameter name="Soft">
  187. <para>Set to <literal>true</literal> to not hangup existing calls.</para>
  188. </parameter>
  189. </syntax>
  190. <description>
  191. <para>Sets an agent as no longer logged in.</para>
  192. </description>
  193. </manager>
  194. ***/
  195. static const char tdesc[] = "Call Agent Proxy Channel";
  196. static const char config[] = "agents.conf";
  197. static const char app[] = "AgentLogin";
  198. static const char app3[] = "AgentMonitorOutgoing";
  199. static char moh[80] = "default";
  200. #define AST_MAX_AGENT 80 /*!< Agent ID or Password max length */
  201. #define AST_MAX_BUF 256
  202. #define AST_MAX_FILENAME_LEN 256
  203. static const char pa_family[] = "Agents"; /*!< Persistent Agents astdb family */
  204. #define PA_MAX_LEN 2048 /*!< The maximum length of each persistent member agent database entry */
  205. #define DEFAULT_ACCEPTDTMF '#'
  206. #define DEFAULT_ENDDTMF '*'
  207. static ast_group_t group;
  208. static int autologoff;
  209. static int wrapuptime;
  210. static int ackcall;
  211. static int endcall;
  212. static int autologoffunavail = 0;
  213. static char acceptdtmf = DEFAULT_ACCEPTDTMF;
  214. static char enddtmf = DEFAULT_ENDDTMF;
  215. static int maxlogintries = 3;
  216. static char agentgoodbye[AST_MAX_FILENAME_LEN] = "vm-goodbye";
  217. static int recordagentcalls = 0;
  218. static char recordformat[AST_MAX_BUF] = "";
  219. static char recordformatext[AST_MAX_BUF] = "";
  220. static char urlprefix[AST_MAX_BUF] = "";
  221. static char savecallsin[AST_MAX_BUF] = "";
  222. static int updatecdr = 0;
  223. static char beep[AST_MAX_BUF] = "beep";
  224. #define GETAGENTBYCALLERID "AGENTBYCALLERID"
  225. enum {
  226. AGENT_FLAG_ACKCALL = (1 << 0),
  227. AGENT_FLAG_AUTOLOGOFF = (1 << 1),
  228. AGENT_FLAG_WRAPUPTIME = (1 << 2),
  229. AGENT_FLAG_ACCEPTDTMF = (1 << 3),
  230. AGENT_FLAG_ENDDTMF = (1 << 4),
  231. };
  232. /*! \brief Structure representing an agent. */
  233. struct agent_pvt {
  234. ast_mutex_t lock; /*!< Channel private lock */
  235. int dead; /*!< Poised for destruction? */
  236. int pending; /*!< Not a real agent -- just pending a match */
  237. int abouttograb; /*!< About to grab */
  238. int autologoff; /*!< Auto timeout time */
  239. int ackcall; /*!< ackcall */
  240. int deferlogoff; /*!< Defer logoff to hangup */
  241. char acceptdtmf;
  242. char enddtmf;
  243. time_t loginstart; /*!< When agent first logged in (0 when logged off) */
  244. time_t start; /*!< When call started */
  245. struct timeval lastdisc; /*!< When last disconnected */
  246. int wrapuptime; /*!< Wrapup time in ms */
  247. ast_group_t group; /*!< Group memberships */
  248. int acknowledged; /*!< Acknowledged */
  249. char moh[80]; /*!< Which music on hold */
  250. char agent[AST_MAX_AGENT]; /*!< Agent ID */
  251. char password[AST_MAX_AGENT]; /*!< Password for Agent login */
  252. char name[AST_MAX_AGENT];
  253. int app_lock_flag;
  254. ast_cond_t app_complete_cond;
  255. ast_cond_t login_wait_cond;
  256. volatile int app_sleep_cond; /**< Sleep condition for the login app */
  257. struct ast_channel *owner; /**< Agent */
  258. char logincallerid[80]; /**< Caller ID they had when they logged in */
  259. struct ast_channel *chan; /**< Channel we use */
  260. unsigned int flags; /**< Flags show if settings were applied with channel vars */
  261. AST_LIST_ENTRY(agent_pvt) list; /**< Next Agent in the linked list. */
  262. };
  263. #define DATA_EXPORT_AGENT(MEMBER) \
  264. MEMBER(agent_pvt, autologoff, AST_DATA_INTEGER) \
  265. MEMBER(agent_pvt, ackcall, AST_DATA_BOOLEAN) \
  266. MEMBER(agent_pvt, deferlogoff, AST_DATA_BOOLEAN) \
  267. MEMBER(agent_pvt, wrapuptime, AST_DATA_MILLISECONDS) \
  268. MEMBER(agent_pvt, acknowledged, AST_DATA_BOOLEAN) \
  269. MEMBER(agent_pvt, name, AST_DATA_STRING) \
  270. MEMBER(agent_pvt, password, AST_DATA_PASSWORD) \
  271. MEMBER(agent_pvt, acceptdtmf, AST_DATA_CHARACTER) \
  272. MEMBER(agent_pvt, logincallerid, AST_DATA_STRING)
  273. AST_DATA_STRUCTURE(agent_pvt, DATA_EXPORT_AGENT);
  274. static AST_LIST_HEAD_STATIC(agents, agent_pvt); /*!< Holds the list of agents (loaded form agents.conf). */
  275. #define CHECK_FORMATS(ast, p) do { \
  276. if (p->chan) {\
  277. if (!(ast_format_cap_identical(ast->nativeformats, p->chan->nativeformats))) { \
  278. char tmp1[256], tmp2[256]; \
  279. ast_debug(1, "Native formats changing from '%s' to '%s'\n", ast_getformatname_multiple(tmp1, sizeof(tmp1), ast->nativeformats), ast_getformatname_multiple(tmp2, sizeof(tmp2), p->chan->nativeformats)); \
  280. /* Native formats changed, reset things */ \
  281. ast_format_cap_copy(ast->nativeformats, p->chan->nativeformats); \
  282. ast_debug(1, "Resetting read to '%s' and write to '%s'\n", ast_getformatname(&ast->readformat), ast_getformatname(&ast->writeformat));\
  283. ast_set_read_format(ast, &ast->readformat); \
  284. ast_set_write_format(ast, &ast->writeformat); \
  285. } \
  286. if ((ast_format_cmp(&p->chan->readformat, &ast->rawreadformat) != AST_FORMAT_CMP_EQUAL) && !p->chan->generator) \
  287. ast_set_read_format(p->chan, &ast->rawreadformat); \
  288. if ((ast_format_cmp(&p->chan->writeformat, &ast->rawwriteformat) != AST_FORMAT_CMP_EQUAL) && !p->chan->generator) \
  289. ast_set_write_format(p->chan, &ast->rawwriteformat); \
  290. } \
  291. } while(0)
  292. /*! \brief Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
  293. properly for a timingfd XXX This might need more work if agents were logged in as agents or other
  294. totally impractical combinations XXX */
  295. #define CLEANUP(ast, p) do { \
  296. int x; \
  297. if (p->chan) { \
  298. for (x=0;x<AST_MAX_FDS;x++) {\
  299. if (x != AST_TIMING_FD) \
  300. ast_channel_set_fd(ast, x, p->chan->fds[x]); \
  301. } \
  302. ast_channel_set_fd(ast, AST_AGENT_FD, p->chan->fds[AST_TIMING_FD]); \
  303. } \
  304. } while(0)
  305. /*--- Forward declarations */
  306. static struct ast_channel *agent_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
  307. static int agent_devicestate(void *data);
  308. static int agent_digit_begin(struct ast_channel *ast, char digit);
  309. static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  310. static int agent_call(struct ast_channel *ast, char *dest, int timeout);
  311. static int agent_hangup(struct ast_channel *ast);
  312. static int agent_answer(struct ast_channel *ast);
  313. static struct ast_frame *agent_read(struct ast_channel *ast);
  314. static int agent_write(struct ast_channel *ast, struct ast_frame *f);
  315. static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
  316. static int agent_sendtext(struct ast_channel *ast, const char *text);
  317. static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  318. static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  319. static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
  320. static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state);
  321. static struct ast_channel* agent_get_base_channel(struct ast_channel *chan);
  322. static int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base);
  323. static int agent_logoff(const char *agent, int soft);
  324. /*! \brief Channel interface description for PBX integration */
  325. static struct ast_channel_tech agent_tech = {
  326. .type = "Agent",
  327. .description = tdesc,
  328. .requester = agent_request,
  329. .devicestate = agent_devicestate,
  330. .send_digit_begin = agent_digit_begin,
  331. .send_digit_end = agent_digit_end,
  332. .call = agent_call,
  333. .hangup = agent_hangup,
  334. .answer = agent_answer,
  335. .read = agent_read,
  336. .write = agent_write,
  337. .write_video = agent_write,
  338. .send_html = agent_sendhtml,
  339. .send_text = agent_sendtext,
  340. .exception = agent_read,
  341. .indicate = agent_indicate,
  342. .fixup = agent_fixup,
  343. .bridged_channel = agent_bridgedchannel,
  344. .get_base_channel = agent_get_base_channel,
  345. .set_base_channel = agent_set_base_channel,
  346. };
  347. /*!
  348. * \brief Locks the owning channel for a LOCKED pvt while obeying locking order. The pvt
  349. * must enter this function locked and will be returned locked, but this function will
  350. * unlock the pvt for a short time, so it can't be used while expecting the pvt to remain
  351. * static. If function returns a non NULL channel, it will need to be unlocked and
  352. * unrefed once it is no longer needed.
  353. *
  354. * \param pvt Pointer to the LOCKED agent_pvt for which the owner is needed
  355. * \ret locked channel which owns the pvt at the time of completion. NULL if not available.
  356. */
  357. static struct ast_channel *agent_lock_owner(struct agent_pvt *pvt)
  358. {
  359. struct ast_channel *owner;
  360. for (;;) {
  361. if (!pvt->owner) { /* No owner. Nothing to do. */
  362. return NULL;
  363. }
  364. /* If we don't ref the owner, it could be killed when we unlock the pvt. */
  365. owner = ast_channel_ref(pvt->owner);
  366. /* Locking order requires us to lock channel, then pvt. */
  367. ast_mutex_unlock(&pvt->lock);
  368. ast_channel_lock(owner);
  369. ast_mutex_lock(&pvt->lock);
  370. /* Check if owner changed during pvt unlock period */
  371. if (owner != pvt->owner) { /* Channel changed. Unref and do another pass. */
  372. ast_channel_unlock(owner);
  373. owner = ast_channel_unref(owner);
  374. } else { /* Channel stayed the same. Return it. */
  375. return owner;
  376. }
  377. }
  378. }
  379. /*!
  380. * Adds an agent to the global list of agents.
  381. *
  382. * \param agent A string with the username, password and real name of an agent. As defined in agents.conf. Example: "13,169,John Smith"
  383. * \param pending If it is pending or not.
  384. * @return The just created agent.
  385. * \sa agent_pvt, agents.
  386. */
  387. static struct agent_pvt *add_agent(const char *agent, int pending)
  388. {
  389. char *parse;
  390. AST_DECLARE_APP_ARGS(args,
  391. AST_APP_ARG(agt);
  392. AST_APP_ARG(password);
  393. AST_APP_ARG(name);
  394. );
  395. char *password = NULL;
  396. char *name = NULL;
  397. char *agt = NULL;
  398. struct agent_pvt *p;
  399. parse = ast_strdupa(agent);
  400. /* Extract username (agt), password and name from agent (args). */
  401. AST_STANDARD_APP_ARGS(args, parse);
  402. if(args.argc == 0) {
  403. ast_log(LOG_WARNING, "A blank agent line!\n");
  404. return NULL;
  405. }
  406. if(ast_strlen_zero(args.agt) ) {
  407. ast_log(LOG_WARNING, "An agent line with no agentid!\n");
  408. return NULL;
  409. } else
  410. agt = args.agt;
  411. if(!ast_strlen_zero(args.password)) {
  412. password = args.password;
  413. while (*password && *password < 33) password++;
  414. }
  415. if(!ast_strlen_zero(args.name)) {
  416. name = args.name;
  417. while (*name && *name < 33) name++;
  418. }
  419. /* Are we searching for the agent here ? To see if it exists already ? */
  420. AST_LIST_TRAVERSE(&agents, p, list) {
  421. if (!pending && !strcmp(p->agent, agt))
  422. break;
  423. }
  424. if (!p) {
  425. // Build the agent.
  426. if (!(p = ast_calloc(1, sizeof(*p))))
  427. return NULL;
  428. ast_copy_string(p->agent, agt, sizeof(p->agent));
  429. ast_mutex_init(&p->lock);
  430. ast_cond_init(&p->app_complete_cond, NULL);
  431. ast_cond_init(&p->login_wait_cond, NULL);
  432. p->app_lock_flag = 0;
  433. p->app_sleep_cond = 1;
  434. p->group = group;
  435. p->pending = pending;
  436. AST_LIST_INSERT_TAIL(&agents, p, list);
  437. }
  438. ast_copy_string(p->password, password ? password : "", sizeof(p->password));
  439. ast_copy_string(p->name, name ? name : "", sizeof(p->name));
  440. ast_copy_string(p->moh, moh, sizeof(p->moh));
  441. if (!ast_test_flag(p, AGENT_FLAG_ACKCALL)) {
  442. p->ackcall = ackcall;
  443. }
  444. if (!ast_test_flag(p, AGENT_FLAG_AUTOLOGOFF)) {
  445. p->autologoff = autologoff;
  446. }
  447. if (!ast_test_flag(p, AGENT_FLAG_ACCEPTDTMF)) {
  448. p->acceptdtmf = acceptdtmf;
  449. }
  450. if (!ast_test_flag(p, AGENT_FLAG_ENDDTMF)) {
  451. p->enddtmf = enddtmf;
  452. }
  453. /* If someone reduces the wrapuptime and reloads, we want it
  454. * to change the wrapuptime immediately on all calls */
  455. if (!ast_test_flag(p, AGENT_FLAG_WRAPUPTIME) && p->wrapuptime > wrapuptime) {
  456. struct timeval now = ast_tvnow();
  457. /* XXX check what is this exactly */
  458. /* We won't be pedantic and check the tv_usec val */
  459. if (p->lastdisc.tv_sec > (now.tv_sec + wrapuptime/1000)) {
  460. p->lastdisc.tv_sec = now.tv_sec + wrapuptime/1000;
  461. p->lastdisc.tv_usec = now.tv_usec;
  462. }
  463. }
  464. p->wrapuptime = wrapuptime;
  465. if (pending)
  466. p->dead = 1;
  467. else
  468. p->dead = 0;
  469. return p;
  470. }
  471. /*!
  472. * Deletes an agent after doing some clean up.
  473. * Further documentation: How safe is this function ? What state should the agent be to be cleaned.
  474. *
  475. * \warning XXX This function seems to be very unsafe.
  476. * Potential for double free and use after free among other
  477. * problems.
  478. *
  479. * \param p Agent to be deleted.
  480. * \returns Always 0.
  481. */
  482. static int agent_cleanup(struct agent_pvt *p)
  483. {
  484. struct ast_channel *chan;
  485. ast_mutex_lock(&p->lock);
  486. chan = p->owner;
  487. p->owner = NULL;
  488. /* Release ownership of the agent to other threads (presumably running the login app). */
  489. p->app_sleep_cond = 1;
  490. p->app_lock_flag = 0;
  491. ast_cond_signal(&p->app_complete_cond);
  492. if (chan) {
  493. chan->tech_pvt = NULL;
  494. chan = ast_channel_release(chan);
  495. }
  496. if (p->dead) {
  497. ast_mutex_unlock(&p->lock);
  498. ast_mutex_destroy(&p->lock);
  499. ast_cond_destroy(&p->app_complete_cond);
  500. ast_cond_destroy(&p->login_wait_cond);
  501. ast_free(p);
  502. } else {
  503. ast_mutex_unlock(&p->lock);
  504. }
  505. return 0;
  506. }
  507. static int check_availability(struct agent_pvt *newlyavailable, int needlock);
  508. static int agent_answer(struct ast_channel *ast)
  509. {
  510. ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
  511. return -1;
  512. }
  513. static int __agent_start_monitoring(struct ast_channel *ast, struct agent_pvt *p, int needlock)
  514. {
  515. char tmp[AST_MAX_BUF],tmp2[AST_MAX_BUF], *pointer;
  516. char filename[AST_MAX_BUF];
  517. int res = -1;
  518. if (!p)
  519. return -1;
  520. if (!ast->monitor) {
  521. snprintf(filename, sizeof(filename), "agent-%s-%s",p->agent, ast->uniqueid);
  522. /* substitute . for - */
  523. if ((pointer = strchr(filename, '.')))
  524. *pointer = '-';
  525. snprintf(tmp, sizeof(tmp), "%s%s", savecallsin, filename);
  526. ast_monitor_start(ast, recordformat, tmp, needlock, X_REC_IN | X_REC_OUT);
  527. ast_monitor_setjoinfiles(ast, 1);
  528. snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix, filename, recordformatext);
  529. #if 0
  530. ast_verbose("name is %s, link is %s\n",tmp, tmp2);
  531. #endif
  532. if (!ast->cdr)
  533. ast->cdr = ast_cdr_alloc();
  534. ast_cdr_setuserfield(ast, tmp2);
  535. res = 0;
  536. } else
  537. ast_log(LOG_ERROR, "Recording already started on that call.\n");
  538. return res;
  539. }
  540. static int agent_start_monitoring(struct ast_channel *ast, int needlock)
  541. {
  542. return __agent_start_monitoring(ast, ast->tech_pvt, needlock);
  543. }
  544. static struct ast_frame *agent_read(struct ast_channel *ast)
  545. {
  546. struct agent_pvt *p = ast->tech_pvt;
  547. struct ast_frame *f = NULL;
  548. static struct ast_frame answer_frame = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
  549. int cur_time = time(NULL);
  550. struct ast_channel *owner;
  551. ast_mutex_lock(&p->lock);
  552. owner = agent_lock_owner(p);
  553. CHECK_FORMATS(ast, p);
  554. if (!p->start) {
  555. p->start = cur_time;
  556. }
  557. if (p->chan) {
  558. ast_copy_flags(p->chan, ast, AST_FLAG_EXCEPTION);
  559. p->chan->fdno = (ast->fdno == AST_AGENT_FD) ? AST_TIMING_FD : ast->fdno;
  560. f = ast_read(p->chan);
  561. } else
  562. f = &ast_null_frame;
  563. if (!f) {
  564. /* If there's a channel, make it NULL */
  565. if (p->chan) {
  566. p->chan->_bridge = NULL;
  567. p->chan = NULL;
  568. ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  569. p->acknowledged = 0;
  570. }
  571. } else {
  572. /* if acknowledgement is not required, and the channel is up, we may have missed
  573. an AST_CONTROL_ANSWER (if there was one), so mark the call acknowledged anyway */
  574. if (!p->ackcall && !p->acknowledged && p->chan && (p->chan->_state == AST_STATE_UP)) {
  575. p->acknowledged = 1;
  576. }
  577. if (!p->acknowledged) {
  578. int howlong = cur_time - p->start;
  579. if (p->autologoff && (howlong >= p->autologoff)) {
  580. ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
  581. if (owner || p->chan) {
  582. if (owner) {
  583. ast_softhangup(owner, AST_SOFTHANGUP_EXPLICIT);
  584. ast_channel_unlock(owner);
  585. owner = ast_channel_unref(owner);
  586. }
  587. while (p->chan && ast_channel_trylock(p->chan)) {
  588. DEADLOCK_AVOIDANCE(&p->lock);
  589. }
  590. if (p->chan) {
  591. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  592. ast_channel_unlock(p->chan);
  593. }
  594. }
  595. }
  596. }
  597. switch (f->frametype) {
  598. case AST_FRAME_CONTROL:
  599. if (f->subclass.integer == AST_CONTROL_ANSWER) {
  600. if (p->ackcall) {
  601. ast_verb(3, "%s answered, waiting for '%c' to acknowledge\n", p->chan->name, p->acceptdtmf);
  602. /* Don't pass answer along */
  603. ast_frfree(f);
  604. f = &ast_null_frame;
  605. } else {
  606. p->acknowledged = 1;
  607. /* Use the builtin answer frame for the
  608. recording start check below. */
  609. ast_frfree(f);
  610. f = &answer_frame;
  611. }
  612. }
  613. break;
  614. case AST_FRAME_DTMF_BEGIN:
  615. /*ignore DTMF begin's as it can cause issues with queue announce files*/
  616. if((!p->acknowledged && f->subclass.integer == p->acceptdtmf) || (f->subclass.integer == p->enddtmf && endcall)){
  617. ast_frfree(f);
  618. f = &ast_null_frame;
  619. }
  620. break;
  621. case AST_FRAME_DTMF_END:
  622. if (!p->acknowledged && (f->subclass.integer == p->acceptdtmf)) {
  623. if (p->chan) {
  624. ast_verb(3, "%s acknowledged\n", p->chan->name);
  625. }
  626. p->acknowledged = 1;
  627. ast_frfree(f);
  628. f = &answer_frame;
  629. } else if (f->subclass.integer == p->enddtmf && endcall) {
  630. /* terminates call */
  631. ast_frfree(f);
  632. f = NULL;
  633. }
  634. break;
  635. case AST_FRAME_VOICE:
  636. case AST_FRAME_VIDEO:
  637. /* don't pass voice or video until the call is acknowledged */
  638. if (!p->acknowledged) {
  639. ast_frfree(f);
  640. f = &ast_null_frame;
  641. }
  642. default:
  643. /* pass everything else on through */
  644. break;
  645. }
  646. }
  647. if (owner) {
  648. ast_channel_unlock(owner);
  649. owner = ast_channel_unref(owner);
  650. }
  651. CLEANUP(ast,p);
  652. if (p->chan && !p->chan->_bridge) {
  653. if (strcasecmp(p->chan->tech->type, "Local")) {
  654. p->chan->_bridge = ast;
  655. if (p->chan)
  656. ast_debug(1, "Bridge on '%s' being set to '%s' (3)\n", p->chan->name, p->chan->_bridge->name);
  657. }
  658. }
  659. ast_mutex_unlock(&p->lock);
  660. if (recordagentcalls && f == &answer_frame)
  661. agent_start_monitoring(ast,0);
  662. return f;
  663. }
  664. static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
  665. {
  666. struct agent_pvt *p = ast->tech_pvt;
  667. int res = -1;
  668. ast_mutex_lock(&p->lock);
  669. if (p->chan)
  670. res = ast_channel_sendhtml(p->chan, subclass, data, datalen);
  671. ast_mutex_unlock(&p->lock);
  672. return res;
  673. }
  674. static int agent_sendtext(struct ast_channel *ast, const char *text)
  675. {
  676. struct agent_pvt *p = ast->tech_pvt;
  677. int res = -1;
  678. ast_mutex_lock(&p->lock);
  679. if (p->chan)
  680. res = ast_sendtext(p->chan, text);
  681. ast_mutex_unlock(&p->lock);
  682. return res;
  683. }
  684. static int agent_write(struct ast_channel *ast, struct ast_frame *f)
  685. {
  686. struct agent_pvt *p = ast->tech_pvt;
  687. int res = -1;
  688. CHECK_FORMATS(ast, p);
  689. ast_mutex_lock(&p->lock);
  690. if (!p->chan)
  691. res = 0;
  692. else {
  693. if ((f->frametype != AST_FRAME_VOICE) ||
  694. (f->frametype != AST_FRAME_VIDEO) ||
  695. (ast_format_cmp(&f->subclass.format, &p->chan->writeformat) != AST_FORMAT_CMP_NOT_EQUAL)) {
  696. res = ast_write(p->chan, f);
  697. } else {
  698. ast_debug(1, "Dropping one incompatible %s frame on '%s' to '%s'\n",
  699. f->frametype == AST_FRAME_VOICE ? "audio" : "video",
  700. ast->name, p->chan->name);
  701. res = 0;
  702. }
  703. }
  704. CLEANUP(ast, p);
  705. ast_mutex_unlock(&p->lock);
  706. return res;
  707. }
  708. static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  709. {
  710. struct agent_pvt *p = newchan->tech_pvt;
  711. ast_mutex_lock(&p->lock);
  712. if (p->owner != oldchan) {
  713. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
  714. ast_mutex_unlock(&p->lock);
  715. return -1;
  716. }
  717. p->owner = newchan;
  718. ast_mutex_unlock(&p->lock);
  719. return 0;
  720. }
  721. static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  722. {
  723. struct agent_pvt *p = ast->tech_pvt;
  724. int res = -1;
  725. ast_mutex_lock(&p->lock);
  726. if (p->chan && !ast_check_hangup(p->chan)) {
  727. while (ast_channel_trylock(p->chan)) {
  728. int res;
  729. if ((res = ast_channel_unlock(ast))) {
  730. ast_log(LOG_ERROR, "chan_agent bug! Channel was not locked upon entry to agent_indicate: %s\n", res > 0 ? strerror(res) : "Bad ao2obj data");
  731. ast_mutex_unlock(&p->lock);
  732. return -1;
  733. }
  734. usleep(1);
  735. ast_channel_lock(ast);
  736. }
  737. res = p->chan->tech->indicate ? p->chan->tech->indicate(p->chan, condition, data, datalen) : -1;
  738. ast_channel_unlock(p->chan);
  739. } else
  740. res = 0;
  741. ast_mutex_unlock(&p->lock);
  742. return res;
  743. }
  744. static int agent_digit_begin(struct ast_channel *ast, char digit)
  745. {
  746. struct agent_pvt *p = ast->tech_pvt;
  747. ast_mutex_lock(&p->lock);
  748. if (p->chan) {
  749. ast_senddigit_begin(p->chan, digit);
  750. }
  751. ast_mutex_unlock(&p->lock);
  752. return 0;
  753. }
  754. static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  755. {
  756. struct agent_pvt *p = ast->tech_pvt;
  757. ast_mutex_lock(&p->lock);
  758. if (p->chan) {
  759. ast_senddigit_end(p->chan, digit, duration);
  760. }
  761. ast_mutex_unlock(&p->lock);
  762. return 0;
  763. }
  764. static int agent_call(struct ast_channel *ast, char *dest, int timeout)
  765. {
  766. struct agent_pvt *p = ast->tech_pvt;
  767. int res = -1;
  768. int newstate=0;
  769. struct ast_channel *chan;
  770. ast_mutex_lock(&p->lock);
  771. p->acknowledged = 0;
  772. if (p->pending) {
  773. ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
  774. ast_mutex_unlock(&p->lock);
  775. ast_setstate(ast, AST_STATE_DIALING);
  776. return 0;
  777. }
  778. if (!p->chan) {
  779. ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
  780. ast_mutex_unlock(&p->lock);
  781. return res;
  782. }
  783. ast_verb(3, "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
  784. ast_debug(3, "Playing beep, lang '%s'\n", p->chan->language);
  785. chan = p->chan;
  786. ast_mutex_unlock(&p->lock);
  787. res = ast_streamfile(chan, beep, chan->language);
  788. ast_debug(3, "Played beep, result '%d'\n", res);
  789. if (!res) {
  790. res = ast_waitstream(chan, "");
  791. ast_debug(3, "Waited for stream, result '%d'\n", res);
  792. }
  793. ast_mutex_lock(&p->lock);
  794. if (!p->chan) {
  795. /* chan went away while we were streaming, this shouldn't be possible */
  796. res = -1;
  797. }
  798. if (!res) {
  799. struct ast_format tmpfmt;
  800. res = ast_set_read_format_from_cap(p->chan, p->chan->nativeformats);
  801. ast_debug(3, "Set read format, result '%d'\n", res);
  802. if (res)
  803. ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
  804. } else {
  805. /* Agent hung-up */
  806. p->chan = NULL;
  807. ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  808. }
  809. if (!res) {
  810. struct ast_format tmpfmt;
  811. res = ast_set_write_format_from_cap(p->chan, p->chan->nativeformats);
  812. ast_debug(3, "Set write format, result '%d'\n", res);
  813. if (res)
  814. ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
  815. }
  816. if(!res) {
  817. /* Call is immediately up, or might need ack */
  818. if (p->ackcall) {
  819. newstate = AST_STATE_RINGING;
  820. } else {
  821. newstate = AST_STATE_UP;
  822. if (recordagentcalls)
  823. agent_start_monitoring(ast, 0);
  824. p->acknowledged = 1;
  825. }
  826. res = 0;
  827. }
  828. CLEANUP(ast, p);
  829. ast_mutex_unlock(&p->lock);
  830. if (newstate)
  831. ast_setstate(ast, newstate);
  832. return res;
  833. }
  834. /*! \brief return the channel or base channel if one exists. This function assumes the channel it is called on is already locked */
  835. struct ast_channel* agent_get_base_channel(struct ast_channel *chan)
  836. {
  837. struct agent_pvt *p = NULL;
  838. struct ast_channel *base = chan;
  839. /* chan is locked by the calling function */
  840. if (!chan || !chan->tech_pvt) {
  841. ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) with a tech_pvt (0x%ld) to get a base channel.\n", (long)chan, (chan)?(long)chan->tech_pvt:(long)NULL);
  842. return NULL;
  843. }
  844. p = chan->tech_pvt;
  845. if (p->chan)
  846. base = p->chan;
  847. return base;
  848. }
  849. int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base)
  850. {
  851. struct agent_pvt *p = NULL;
  852. if (!chan || !base) {
  853. ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) and a base channel (0x%ld) for setting.\n", (long)chan, (long)base);
  854. return -1;
  855. }
  856. p = chan->tech_pvt;
  857. if (!p) {
  858. ast_log(LOG_ERROR, "whoa, channel %s is missing his tech_pvt structure!!.\n", chan->name);
  859. return -1;
  860. }
  861. p->chan = base;
  862. return 0;
  863. }
  864. static int agent_hangup(struct ast_channel *ast)
  865. {
  866. struct agent_pvt *p = ast->tech_pvt;
  867. struct ast_channel *indicate_chan = NULL;
  868. char *tmp_moh; /* moh buffer for indicating after unlocking p */
  869. if (p->pending) {
  870. AST_LIST_LOCK(&agents);
  871. AST_LIST_REMOVE(&agents, p, list);
  872. AST_LIST_UNLOCK(&agents);
  873. }
  874. ast_mutex_lock(&p->lock);
  875. p->owner = NULL;
  876. ast->tech_pvt = NULL;
  877. p->app_sleep_cond = 1;
  878. p->acknowledged = 0;
  879. /* Release ownership of the agent to other threads (presumably running the login app). */
  880. p->app_lock_flag = 0;
  881. ast_cond_signal(&p->app_complete_cond);
  882. /* if they really are hung up then set start to 0 so the test
  883. * later if we're called on an already downed channel
  884. * doesn't cause an agent to be logged out like when
  885. * agent_request() is followed immediately by agent_hangup()
  886. * as in apps/app_chanisavail.c:chanavail_exec()
  887. */
  888. ast_debug(1, "Hangup called for state %s\n", ast_state2str(ast->_state));
  889. if (p->start && (ast->_state != AST_STATE_UP)) {
  890. p->start = 0;
  891. } else
  892. p->start = 0;
  893. if (p->chan) {
  894. p->chan->_bridge = NULL;
  895. /* If they're dead, go ahead and hang up on the agent now */
  896. if (p->dead) {
  897. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  898. } else if (p->loginstart) {
  899. indicate_chan = ast_channel_ref(p->chan);
  900. tmp_moh = ast_strdupa(p->moh);
  901. }
  902. }
  903. ast_mutex_unlock(&p->lock);
  904. if (indicate_chan) {
  905. ast_indicate_data(indicate_chan, AST_CONTROL_HOLD,
  906. S_OR(tmp_moh, NULL),
  907. !ast_strlen_zero(tmp_moh) ? strlen(tmp_moh) + 1 : 0);
  908. indicate_chan = ast_channel_unref(indicate_chan);
  909. }
  910. /* Only register a device state change if the agent is still logged in */
  911. if (!p->loginstart) {
  912. p->logincallerid[0] = '\0';
  913. } else {
  914. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  915. }
  916. if (p->abouttograb) {
  917. /* Let the "about to grab" thread know this isn't valid anymore, and let it
  918. kill it later */
  919. p->abouttograb = 0;
  920. } else if (p->dead) {
  921. ast_mutex_destroy(&p->lock);
  922. ast_cond_destroy(&p->app_complete_cond);
  923. ast_cond_destroy(&p->login_wait_cond);
  924. ast_free(p);
  925. } else {
  926. if (p->chan) {
  927. /* Not dead -- check availability now */
  928. ast_mutex_lock(&p->lock);
  929. /* Store last disconnect time */
  930. p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
  931. ast_mutex_unlock(&p->lock);
  932. }
  933. }
  934. return 0;
  935. }
  936. static int agent_cont_sleep( void *data )
  937. {
  938. struct agent_pvt *p;
  939. int res;
  940. p = (struct agent_pvt *)data;
  941. ast_mutex_lock(&p->lock);
  942. res = p->app_sleep_cond;
  943. if (p->lastdisc.tv_sec) {
  944. if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0)
  945. res = 1;
  946. }
  947. ast_mutex_unlock(&p->lock);
  948. if (!res)
  949. ast_debug(5, "agent_cont_sleep() returning %d\n", res );
  950. return res;
  951. }
  952. static int agent_ack_sleep(void *data)
  953. {
  954. struct agent_pvt *p;
  955. int res=0;
  956. int to = 1000;
  957. struct ast_frame *f;
  958. struct timeval start = ast_tvnow();
  959. int ms;
  960. /* Wait a second and look for something */
  961. p = (struct agent_pvt *) data;
  962. if (!p->chan)
  963. return -1;
  964. while ((ms = ast_remaining_ms(start, to))) {
  965. ms = ast_waitfor(p->chan, ms);
  966. if (ms < 0) {
  967. return -1;
  968. }
  969. if (ms == 0) {
  970. return 0;
  971. }
  972. f = ast_read(p->chan);
  973. if (!f)
  974. return -1;
  975. if (f->frametype == AST_FRAME_DTMF)
  976. res = f->subclass.integer;
  977. else
  978. res = 0;
  979. ast_frfree(f);
  980. ast_mutex_lock(&p->lock);
  981. if (!p->app_sleep_cond) {
  982. ast_mutex_unlock(&p->lock);
  983. return 0;
  984. } else if (res == p->acceptdtmf) {
  985. ast_mutex_unlock(&p->lock);
  986. return 1;
  987. }
  988. ast_mutex_unlock(&p->lock);
  989. res = 0;
  990. }
  991. return 0;
  992. }
  993. static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
  994. {
  995. struct agent_pvt *p = bridge->tech_pvt;
  996. struct ast_channel *ret = NULL;
  997. if (p) {
  998. if (chan == p->chan)
  999. ret = bridge->_bridge;
  1000. else if (chan == bridge->_bridge)
  1001. ret = p->chan;
  1002. }
  1003. ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning '%s'\n", chan->name, bridge->name, ret ? ret->name : "<none>");
  1004. return ret;
  1005. }
  1006. /*! \brief Create new agent channel */
  1007. static struct ast_channel *agent_new(struct agent_pvt *p, int state, const char *linkedid)
  1008. {
  1009. struct ast_channel *tmp;
  1010. #if 0
  1011. if (!p->chan) {
  1012. ast_log(LOG_WARNING, "No channel? :(\n");
  1013. return NULL;
  1014. }
  1015. #endif
  1016. if (p->pending)
  1017. tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? p->chan->exten:"", p->chan ? p->chan->context:"", linkedid, 0, "Agent/P%s-%d", p->agent, (int) ast_random() & 0xffff);
  1018. else
  1019. tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? p->chan->exten:"", p->chan ? p->chan->context:"", linkedid, 0, "Agent/%s", p->agent);
  1020. if (!tmp) {
  1021. ast_log(LOG_WARNING, "Unable to allocate agent channel structure\n");
  1022. return NULL;
  1023. }
  1024. tmp->tech = &agent_tech;
  1025. if (p->chan) {
  1026. ast_format_cap_copy(tmp->nativeformats, p->chan->nativeformats);
  1027. ast_format_copy(&tmp->writeformat, &p->chan->writeformat);
  1028. ast_format_copy(&tmp->rawwriteformat, &p->chan->writeformat);
  1029. ast_format_copy(&tmp->readformat, &p->chan->readformat);
  1030. ast_format_copy(&tmp->rawreadformat, &p->chan->readformat);
  1031. ast_string_field_set(tmp, language, p->chan->language);
  1032. ast_copy_string(tmp->context, p->chan->context, sizeof(tmp->context));
  1033. ast_copy_string(tmp->exten, p->chan->exten, sizeof(tmp->exten));
  1034. /* XXX Is this really all we copy form the originating channel?? */
  1035. } else {
  1036. ast_format_set(&tmp->writeformat, AST_FORMAT_SLINEAR, 0);
  1037. ast_format_set(&tmp->rawwriteformat, AST_FORMAT_SLINEAR, 0);
  1038. ast_format_set(&tmp->readformat, AST_FORMAT_SLINEAR, 0);
  1039. ast_format_set(&tmp->rawreadformat, AST_FORMAT_SLINEAR, 0);
  1040. ast_format_cap_add(tmp->nativeformats, &tmp->writeformat);
  1041. }
  1042. /* Safe, agentlock already held */
  1043. tmp->tech_pvt = p;
  1044. p->owner = tmp;
  1045. tmp->priority = 1;
  1046. return tmp;
  1047. }
  1048. /*!
  1049. * Read configuration data. The file named agents.conf.
  1050. *
  1051. * \returns Always 0, or so it seems.
  1052. */
  1053. static int read_agent_config(int reload)
  1054. {
  1055. struct ast_config *cfg;
  1056. struct ast_config *ucfg;
  1057. struct ast_variable *v;
  1058. struct agent_pvt *p;
  1059. const char *catname;
  1060. const char *hasagent;
  1061. int genhasagent;
  1062. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  1063. group = 0;
  1064. autologoff = 0;
  1065. wrapuptime = 0;
  1066. ackcall = 0;
  1067. endcall = 1;
  1068. cfg = ast_config_load(config, config_flags);
  1069. if (!cfg) {
  1070. ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
  1071. return 0;
  1072. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  1073. return -1;
  1074. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  1075. ast_log(LOG_ERROR, "%s contains a parsing error. Aborting\n", config);
  1076. return 0;
  1077. }
  1078. if ((ucfg = ast_config_load("users.conf", config_flags))) {
  1079. if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
  1080. ucfg = NULL;
  1081. } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
  1082. ast_log(LOG_ERROR, "users.conf contains a parsing error. Aborting\n");
  1083. return 0;
  1084. }
  1085. }
  1086. AST_LIST_LOCK(&agents);
  1087. AST_LIST_TRAVERSE(&agents, p, list) {
  1088. p->dead = 1;
  1089. }
  1090. strcpy(moh, "default");
  1091. /* set the default recording values */
  1092. recordagentcalls = 0;
  1093. strcpy(recordformat, "wav");
  1094. strcpy(recordformatext, "wav");
  1095. urlprefix[0] = '\0';
  1096. savecallsin[0] = '\0';
  1097. /* Read in the [agents] section */
  1098. v = ast_variable_browse(cfg, "agents");
  1099. while(v) {
  1100. /* Create the interface list */
  1101. if (!strcasecmp(v->name, "agent")) {
  1102. add_agent(v->value, 0);
  1103. } else if (!strcasecmp(v->name, "group")) {
  1104. group = ast_get_group(v->value);
  1105. } else if (!strcasecmp(v->name, "autologoff")) {
  1106. autologoff = atoi(v->value);
  1107. if (autologoff < 0)
  1108. autologoff = 0;
  1109. } else if (!strcasecmp(v->name, "ackcall")) {
  1110. if (ast_true(v->value) || !strcasecmp(v->value, "always")) {
  1111. ackcall = 1;
  1112. }
  1113. } else if (!strcasecmp(v->name, "endcall")) {
  1114. endcall = ast_true(v->value);
  1115. } else if (!strcasecmp(v->name, "acceptdtmf")) {
  1116. acceptdtmf = *(v->value);
  1117. ast_log(LOG_NOTICE, "Set acceptdtmf to %c\n", acceptdtmf);
  1118. } else if (!strcasecmp(v->name, "enddtmf")) {
  1119. enddtmf = *(v->value);
  1120. } else if (!strcasecmp(v->name, "wrapuptime")) {
  1121. wrapuptime = atoi(v->value);
  1122. if (wrapuptime < 0)
  1123. wrapuptime = 0;
  1124. } else if (!strcasecmp(v->name, "maxlogintries") && !ast_strlen_zero(v->value)) {
  1125. maxlogintries = atoi(v->value);
  1126. if (maxlogintries < 0)
  1127. maxlogintries = 0;
  1128. } else if (!strcasecmp(v->name, "goodbye") && !ast_strlen_zero(v->value)) {
  1129. strcpy(agentgoodbye,v->value);
  1130. } else if (!strcasecmp(v->name, "musiconhold")) {
  1131. ast_copy_string(moh, v->value, sizeof(moh));
  1132. } else if (!strcasecmp(v->name, "updatecdr")) {
  1133. if (ast_true(v->value))
  1134. updatecdr = 1;
  1135. else
  1136. updatecdr = 0;
  1137. } else if (!strcasecmp(v->name, "autologoffunavail")) {
  1138. if (ast_true(v->value))
  1139. autologoffunavail = 1;
  1140. else
  1141. autologoffunavail = 0;
  1142. } else if (!strcasecmp(v->name, "recordagentcalls")) {
  1143. recordagentcalls = ast_true(v->value);
  1144. } else if (!strcasecmp(v->name, "recordformat")) {
  1145. ast_copy_string(recordformat, v->value, sizeof(recordformat));
  1146. if (!strcasecmp(v->value, "wav49"))
  1147. strcpy(recordformatext, "WAV");
  1148. else
  1149. ast_copy_string(recordformatext, v->value, sizeof(recordformatext));
  1150. } else if (!strcasecmp(v->name, "urlprefix")) {
  1151. ast_copy_string(urlprefix, v->value, sizeof(urlprefix));
  1152. if (urlprefix[strlen(urlprefix) - 1] != '/')
  1153. strncat(urlprefix, "/", sizeof(urlprefix) - strlen(urlprefix) - 1);
  1154. } else if (!strcasecmp(v->name, "savecallsin")) {
  1155. if (v->value[0] == '/')
  1156. ast_copy_string(savecallsin, v->value, sizeof(savecallsin));
  1157. else
  1158. snprintf(savecallsin, sizeof(savecallsin) - 2, "/%s", v->value);
  1159. if (savecallsin[strlen(savecallsin) - 1] != '/')
  1160. strncat(savecallsin, "/", sizeof(savecallsin) - strlen(savecallsin) - 1);
  1161. } else if (!strcasecmp(v->name, "custom_beep")) {
  1162. ast_copy_string(beep, v->value, sizeof(beep));
  1163. }
  1164. v = v->next;
  1165. }
  1166. if (ucfg) {
  1167. genhasagent = ast_true(ast_variable_retrieve(ucfg, "general", "hasagent"));
  1168. catname = ast_category_browse(ucfg, NULL);
  1169. while(catname) {
  1170. if (strcasecmp(catname, "general")) {
  1171. hasagent = ast_variable_retrieve(ucfg, catname, "hasagent");
  1172. if (ast_true(hasagent) || (!hasagent && genhasagent)) {
  1173. char tmp[256];
  1174. const char *fullname = ast_variable_retrieve(ucfg, catname, "fullname");
  1175. const char *secret = ast_variable_retrieve(ucfg, catname, "secret");
  1176. if (!fullname)
  1177. fullname = "";
  1178. if (!secret)
  1179. secret = "";
  1180. snprintf(tmp, sizeof(tmp), "%s,%s,%s", catname, secret,fullname);
  1181. add_agent(tmp, 0);
  1182. }
  1183. }
  1184. catname = ast_category_browse(ucfg, catname);
  1185. }
  1186. ast_config_destroy(ucfg);
  1187. }
  1188. AST_LIST_TRAVERSE_SAFE_BEGIN(&agents, p, list) {
  1189. if (p->dead) {
  1190. AST_LIST_REMOVE_CURRENT(list);
  1191. /* Destroy if appropriate */
  1192. if (!p->owner) {
  1193. if (!p->chan) {
  1194. ast_mutex_destroy(&p->lock);
  1195. ast_cond_destroy(&p->app_complete_cond);
  1196. ast_cond_destroy(&p->login_wait_cond);
  1197. ast_free(p);
  1198. } else {
  1199. /* Cause them to hang up */
  1200. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. AST_LIST_TRAVERSE_SAFE_END;
  1206. AST_LIST_UNLOCK(&agents);
  1207. ast_config_destroy(cfg);
  1208. return 1;
  1209. }
  1210. static int check_availability(struct agent_pvt *newlyavailable, int needlock)
  1211. {
  1212. struct ast_channel *chan=NULL, *parent=NULL;
  1213. struct agent_pvt *p;
  1214. int res;
  1215. ast_debug(1, "Checking availability of '%s'\n", newlyavailable->agent);
  1216. if (needlock)
  1217. AST_LIST_LOCK(&agents);
  1218. AST_LIST_TRAVERSE(&agents, p, list) {
  1219. if (p == newlyavailable) {
  1220. continue;
  1221. }
  1222. ast_mutex_lock(&p->lock);
  1223. if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
  1224. ast_debug(1, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
  1225. /* We found a pending call, time to merge */
  1226. chan = agent_new(newlyavailable, AST_STATE_DOWN, p->owner ? p->owner->linkedid : NULL);
  1227. parent = p->owner;
  1228. p->abouttograb = 1;
  1229. ast_mutex_unlock(&p->lock);
  1230. break;
  1231. }
  1232. ast_mutex_unlock(&p->lock);
  1233. }
  1234. if (needlock)
  1235. AST_LIST_UNLOCK(&agents);
  1236. if (parent && chan) {
  1237. if (newlyavailable->ackcall) {
  1238. /* Don't do beep here */
  1239. res = 0;
  1240. } else {
  1241. ast_debug(3, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
  1242. res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
  1243. ast_debug(3, "Played beep, result '%d'\n", res);
  1244. if (!res) {
  1245. res = ast_waitstream(newlyavailable->chan, "");
  1246. ast_debug(1, "Waited for stream, result '%d'\n", res);
  1247. }
  1248. }
  1249. if (!res) {
  1250. /* Note -- parent may have disappeared */
  1251. if (p->abouttograb) {
  1252. newlyavailable->acknowledged = 1;
  1253. /* Safe -- agent lock already held */
  1254. ast_setstate(parent, AST_STATE_UP);
  1255. ast_setstate(chan, AST_STATE_UP);
  1256. ast_copy_string(parent->context, chan->context, sizeof(parent->context));
  1257. ast_channel_masquerade(parent, chan);
  1258. ast_hangup(chan);
  1259. p->abouttograb = 0;
  1260. } else {
  1261. ast_debug(1, "Sneaky, parent disappeared in the mean time...\n");
  1262. agent_cleanup(newlyavailable);
  1263. }
  1264. } else {
  1265. ast_debug(1, "Ugh... Agent hung up at exactly the wrong time\n");
  1266. agent_cleanup(newlyavailable);
  1267. }
  1268. }
  1269. return 0;
  1270. }
  1271. static int check_beep(struct agent_pvt *newlyavailable, int needlock)
  1272. {
  1273. struct agent_pvt *p;
  1274. int res=0;
  1275. ast_debug(1, "Checking beep availability of '%s'\n", newlyavailable->agent);
  1276. if (needlock)
  1277. AST_LIST_LOCK(&agents);
  1278. AST_LIST_TRAVERSE(&agents, p, list) {
  1279. if (p == newlyavailable) {
  1280. continue;
  1281. }
  1282. ast_mutex_lock(&p->lock);
  1283. if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
  1284. ast_debug(1, "Call '%s' looks like a would-be winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
  1285. ast_mutex_unlock(&p->lock);
  1286. break;
  1287. }
  1288. ast_mutex_unlock(&p->lock);
  1289. }
  1290. if (needlock)
  1291. AST_LIST_UNLOCK(&agents);
  1292. if (p) {
  1293. ast_mutex_unlock(&newlyavailable->lock);
  1294. ast_debug(3, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
  1295. res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
  1296. ast_debug(1, "Played beep, result '%d'\n", res);
  1297. if (!res) {
  1298. res = ast_waitstream(newlyavailable->chan, "");
  1299. ast_debug(1, "Waited for stream, result '%d'\n", res);
  1300. }
  1301. ast_mutex_lock(&newlyavailable->lock);
  1302. }
  1303. return res;
  1304. }
  1305. /*! \brief Part of the Asterisk PBX interface */
  1306. static struct ast_channel *agent_request(const char *type, struct ast_format_cap *cap, const struct ast_channel* requestor, void *data, int *cause)
  1307. {
  1308. struct agent_pvt *p;
  1309. struct ast_channel *chan = NULL;
  1310. char *s;
  1311. ast_group_t groupmatch;
  1312. int groupoff;
  1313. int waitforagent=0;
  1314. int hasagent = 0;
  1315. struct timeval now;
  1316. s = data;
  1317. if ((s[0] == '@') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
  1318. groupmatch = (1 << groupoff);
  1319. } else if ((s[0] == ':') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
  1320. groupmatch = (1 << groupoff);
  1321. waitforagent = 1;
  1322. } else
  1323. groupmatch = 0;
  1324. /* Check actual logged in agents first */
  1325. AST_LIST_LOCK(&agents);
  1326. AST_LIST_TRAVERSE(&agents, p, list) {
  1327. ast_mutex_lock(&p->lock);
  1328. if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
  1329. if (p->chan)
  1330. hasagent++;
  1331. now = ast_tvnow();
  1332. if (!p->lastdisc.tv_sec || (now.tv_sec >= p->lastdisc.tv_sec)) {
  1333. p->lastdisc = ast_tv(0, 0);
  1334. /* Agent must be registered, but not have any active call, and not be in a waiting state */
  1335. if (!p->owner && p->chan) {
  1336. /* Fixed agent */
  1337. chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  1338. }
  1339. if (chan) {
  1340. ast_mutex_unlock(&p->lock);
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. ast_mutex_unlock(&p->lock);
  1346. }
  1347. if (!p) {
  1348. AST_LIST_TRAVERSE(&agents, p, list) {
  1349. ast_mutex_lock(&p->lock);
  1350. if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
  1351. if (p->chan) {
  1352. hasagent++;
  1353. }
  1354. now = ast_tvnow();
  1355. if (!p->lastdisc.tv_sec || (now.tv_sec >= p->lastdisc.tv_sec)) {
  1356. p->lastdisc = ast_tv(0, 0);
  1357. /* Agent must be registered, but not have any active call, and not be in a waiting state */
  1358. if (!p->owner && p->chan) {
  1359. /* Could still get a fixed agent */
  1360. chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  1361. }
  1362. if (chan) {
  1363. ast_mutex_unlock(&p->lock);
  1364. break;
  1365. }
  1366. }
  1367. }
  1368. ast_mutex_unlock(&p->lock);
  1369. }
  1370. }
  1371. if (!chan && waitforagent) {
  1372. /* No agent available -- but we're requesting to wait for one.
  1373. Allocate a place holder */
  1374. if (hasagent) {
  1375. ast_debug(1, "Creating place holder for '%s'\n", s);
  1376. p = add_agent(data, 1);
  1377. p->group = groupmatch;
  1378. chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  1379. if (!chan)
  1380. ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
  1381. } else {
  1382. ast_debug(1, "Not creating place holder for '%s' since nobody logged in\n", s);
  1383. }
  1384. }
  1385. *cause = hasagent ? AST_CAUSE_BUSY : AST_CAUSE_UNREGISTERED;
  1386. AST_LIST_UNLOCK(&agents);
  1387. if (chan) {
  1388. ast_mutex_lock(&p->lock);
  1389. if (p->pending) {
  1390. ast_mutex_unlock(&p->lock);
  1391. return chan;
  1392. }
  1393. if (!p->chan) {
  1394. ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
  1395. *cause = AST_CAUSE_UNREGISTERED;
  1396. ast_mutex_unlock(&p->lock);
  1397. agent_hangup(chan);
  1398. return NULL;
  1399. }
  1400. /* we need to take control of the channel from the login app
  1401. * thread */
  1402. p->app_sleep_cond = 0;
  1403. p->app_lock_flag = 1;
  1404. ast_queue_frame(p->chan, &ast_null_frame);
  1405. ast_cond_wait(&p->login_wait_cond, &p->lock);
  1406. if (!p->chan) {
  1407. ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
  1408. p->app_sleep_cond = 1;
  1409. p->app_lock_flag = 0;
  1410. ast_cond_signal(&p->app_complete_cond);
  1411. ast_mutex_unlock(&p->lock);
  1412. *cause = AST_CAUSE_UNREGISTERED;
  1413. agent_hangup(chan);
  1414. return NULL;
  1415. }
  1416. ast_indicate(p->chan, AST_CONTROL_UNHOLD);
  1417. ast_mutex_unlock(&p->lock);
  1418. }
  1419. return chan;
  1420. }
  1421. static force_inline int powerof(unsigned int d)
  1422. {
  1423. int x = ffs(d);
  1424. if (x)
  1425. return x - 1;
  1426. return 0;
  1427. }
  1428. /*!
  1429. * Lists agents and their status to the Manager API.
  1430. * It is registered on load_module() and it gets called by the manager backend.
  1431. * This function locks both the pvt and the channel that owns it for a while, but
  1432. * does not keep these locks.
  1433. * \param s
  1434. * \param m
  1435. * \returns
  1436. * \sa action_agent_logoff(), load_module().
  1437. */
  1438. static int action_agents(struct mansession *s, const struct message *m)
  1439. {
  1440. const char *id = astman_get_header(m,"ActionID");
  1441. char idText[256] = "";
  1442. struct agent_pvt *p;
  1443. char *username = NULL;
  1444. char *loginChan = NULL;
  1445. char *talkingto = NULL;
  1446. char *talkingtoChan = NULL;
  1447. char *status = NULL;
  1448. struct ast_channel *bridge;
  1449. if (!ast_strlen_zero(id))
  1450. snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
  1451. astman_send_ack(s, m, "Agents will follow");
  1452. AST_LIST_LOCK(&agents);
  1453. AST_LIST_TRAVERSE(&agents, p, list) {
  1454. struct ast_channel *owner;
  1455. ast_mutex_lock(&p->lock);
  1456. owner = agent_lock_owner(p);
  1457. /* Status Values:
  1458. AGENT_LOGGEDOFF - Agent isn't logged in
  1459. AGENT_IDLE - Agent is logged in, and waiting for call
  1460. AGENT_ONCALL - Agent is logged in, and on a call
  1461. AGENT_UNKNOWN - Don't know anything about agent. Shouldn't ever get this. */
  1462. username = S_OR(p->name, "None");
  1463. /* Set a default status. It 'should' get changed. */
  1464. status = "AGENT_UNKNOWN";
  1465. if (p->chan) {
  1466. loginChan = ast_strdupa(p->chan->name);
  1467. if (owner && owner->_bridge) {
  1468. talkingto = S_COR(p->chan->caller.id.number.valid,
  1469. p->chan->caller.id.number.str, "n/a");
  1470. if ((bridge = ast_bridged_channel(owner))) {
  1471. talkingtoChan = ast_strdupa(bridge->name);
  1472. } else {
  1473. talkingtoChan = "n/a";
  1474. }
  1475. status = "AGENT_ONCALL";
  1476. } else {
  1477. talkingto = "n/a";
  1478. talkingtoChan = "n/a";
  1479. status = "AGENT_IDLE";
  1480. }
  1481. } else {
  1482. loginChan = "n/a";
  1483. talkingto = "n/a";
  1484. talkingtoChan = "n/a";
  1485. status = "AGENT_LOGGEDOFF";
  1486. }
  1487. if (owner) {
  1488. ast_channel_unlock(owner);
  1489. owner = ast_channel_unref(owner);
  1490. }
  1491. astman_append(s, "Event: Agents\r\n"
  1492. "Agent: %s\r\n"
  1493. "Name: %s\r\n"
  1494. "Status: %s\r\n"
  1495. "LoggedInChan: %s\r\n"
  1496. "LoggedInTime: %d\r\n"
  1497. "TalkingTo: %s\r\n"
  1498. "TalkingToChan: %s\r\n"
  1499. "%s"
  1500. "\r\n",
  1501. p->agent, username, status, loginChan, (int)p->loginstart, talkingto, talkingtoChan, idText);
  1502. ast_mutex_unlock(&p->lock);
  1503. }
  1504. AST_LIST_UNLOCK(&agents);
  1505. astman_append(s, "Event: AgentsComplete\r\n"
  1506. "%s"
  1507. "\r\n",idText);
  1508. return 0;
  1509. }
  1510. static int agent_logoff(const char *agent, int soft)
  1511. {
  1512. struct agent_pvt *p;
  1513. int ret = -1; /* Return -1 if no agent if found */
  1514. AST_LIST_LOCK(&agents);
  1515. AST_LIST_TRAVERSE(&agents, p, list) {
  1516. if (!strcasecmp(p->agent, agent)) {
  1517. ret = 0;
  1518. if (p->owner || p->chan) {
  1519. if (!soft) {
  1520. struct ast_channel *owner;
  1521. ast_mutex_lock(&p->lock);
  1522. owner = agent_lock_owner(p);
  1523. if (owner) {
  1524. ast_softhangup(owner, AST_SOFTHANGUP_EXPLICIT);
  1525. ast_channel_unlock(owner);
  1526. owner = ast_channel_unref(owner);
  1527. }
  1528. while (p->chan && ast_channel_trylock(p->chan)) {
  1529. DEADLOCK_AVOIDANCE(&p->lock);
  1530. }
  1531. if (p->chan) {
  1532. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  1533. ast_channel_unlock(p->chan);
  1534. }
  1535. ast_mutex_unlock(&p->lock);
  1536. } else
  1537. p->deferlogoff = 1;
  1538. }
  1539. break;
  1540. }
  1541. }
  1542. AST_LIST_UNLOCK(&agents);
  1543. return ret;
  1544. }
  1545. static char *agent_logoff_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1546. {
  1547. int ret;
  1548. const char *agent;
  1549. switch (cmd) {
  1550. case CLI_INIT:
  1551. e->command = "agent logoff";
  1552. e->usage =
  1553. "Usage: agent logoff <channel> [soft]\n"
  1554. " Sets an agent as no longer logged in.\n"
  1555. " If 'soft' is specified, do not hangup existing calls.\n";
  1556. return NULL;
  1557. case CLI_GENERATE:
  1558. return complete_agent_logoff_cmd(a->line, a->word, a->pos, a->n);
  1559. }
  1560. if (a->argc < 3 || a->argc > 4)
  1561. return CLI_SHOWUSAGE;
  1562. if (a->argc == 4 && strcasecmp(a->argv[3], "soft"))
  1563. return CLI_SHOWUSAGE;
  1564. agent = a->argv[2] + 6;
  1565. ret = agent_logoff(agent, a->argc == 4);
  1566. if (ret == 0)
  1567. ast_cli(a->fd, "Logging out %s\n", agent);
  1568. return CLI_SUCCESS;
  1569. }
  1570. /*!
  1571. * Sets an agent as no longer logged in in the Manager API.
  1572. * It is registered on load_module() and it gets called by the manager backend.
  1573. * \param s
  1574. * \param m
  1575. * \returns
  1576. * \sa action_agents(), load_module().
  1577. */
  1578. static int action_agent_logoff(struct mansession *s, const struct message *m)
  1579. {
  1580. const char *agent = astman_get_header(m, "Agent");
  1581. const char *soft_s = astman_get_header(m, "Soft"); /* "true" is don't hangup */
  1582. int soft;
  1583. int ret; /* return value of agent_logoff */
  1584. if (ast_strlen_zero(agent)) {
  1585. astman_send_error(s, m, "No agent specified");
  1586. return 0;
  1587. }
  1588. soft = ast_true(soft_s) ? 1 : 0;
  1589. ret = agent_logoff(agent, soft);
  1590. if (ret == 0)
  1591. astman_send_ack(s, m, "Agent logged out");
  1592. else
  1593. astman_send_error(s, m, "No such agent");
  1594. return 0;
  1595. }
  1596. static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state)
  1597. {
  1598. char *ret = NULL;
  1599. if (pos == 2) {
  1600. struct agent_pvt *p;
  1601. char name[AST_MAX_AGENT];
  1602. int which = 0, len = strlen(word);
  1603. AST_LIST_LOCK(&agents);
  1604. AST_LIST_TRAVERSE(&agents, p, list) {
  1605. snprintf(name, sizeof(name), "Agent/%s", p->agent);
  1606. if (!strncasecmp(word, name, len) && p->loginstart && ++which > state) {
  1607. ret = ast_strdup(name);
  1608. break;
  1609. }
  1610. }
  1611. AST_LIST_UNLOCK(&agents);
  1612. } else if (pos == 3 && state == 0)
  1613. return ast_strdup("soft");
  1614. return ret;
  1615. }
  1616. /*!
  1617. * Show agents in cli.
  1618. */
  1619. static char *agents_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1620. {
  1621. struct agent_pvt *p;
  1622. char username[AST_MAX_BUF];
  1623. char location[AST_MAX_BUF] = "";
  1624. char talkingto[AST_MAX_BUF] = "";
  1625. char music[AST_MAX_BUF];
  1626. int count_agents = 0; /*!< Number of agents configured */
  1627. int online_agents = 0; /*!< Number of online agents */
  1628. int offline_agents = 0; /*!< Number of offline agents */
  1629. switch (cmd) {
  1630. case CLI_INIT:
  1631. e->command = "agent show";
  1632. e->usage =
  1633. "Usage: agent show\n"
  1634. " Provides summary information on agents.\n";
  1635. return NULL;
  1636. case CLI_GENERATE:
  1637. return NULL;
  1638. }
  1639. if (a->argc != 2)
  1640. return CLI_SHOWUSAGE;
  1641. AST_LIST_LOCK(&agents);
  1642. AST_LIST_TRAVERSE(&agents, p, list) {
  1643. struct ast_channel *owner;
  1644. ast_mutex_lock(&p->lock);
  1645. owner = agent_lock_owner(p);
  1646. if (p->pending) {
  1647. if (p->group)
  1648. ast_cli(a->fd, "-- Pending call to group %d\n", powerof(p->group));
  1649. else
  1650. ast_cli(a->fd, "-- Pending call to agent %s\n", p->agent);
  1651. } else {
  1652. if (!ast_strlen_zero(p->name))
  1653. snprintf(username, sizeof(username), "(%s) ", p->name);
  1654. else
  1655. username[0] = '\0';
  1656. if (p->chan) {
  1657. snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
  1658. if (owner && ast_bridged_channel(owner)) {
  1659. snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name);
  1660. } else {
  1661. strcpy(talkingto, " is idle");
  1662. }
  1663. online_agents++;
  1664. } else {
  1665. strcpy(location, "not logged in");
  1666. talkingto[0] = '\0';
  1667. offline_agents++;
  1668. }
  1669. if (!ast_strlen_zero(p->moh))
  1670. snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
  1671. ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent,
  1672. username, location, talkingto, music);
  1673. count_agents++;
  1674. }
  1675. if (owner) {
  1676. ast_channel_unlock(owner);
  1677. owner = ast_channel_unref(owner);
  1678. }
  1679. ast_mutex_unlock(&p->lock);
  1680. }
  1681. AST_LIST_UNLOCK(&agents);
  1682. if ( !count_agents )
  1683. ast_cli(a->fd, "No Agents are configured in %s\n",config);
  1684. else
  1685. ast_cli(a->fd, "%d agents configured [%d online , %d offline]\n",count_agents, online_agents, offline_agents);
  1686. ast_cli(a->fd, "\n");
  1687. return CLI_SUCCESS;
  1688. }
  1689. static char *agents_show_online(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1690. {
  1691. struct agent_pvt *p;
  1692. char username[AST_MAX_BUF];
  1693. char location[AST_MAX_BUF] = "";
  1694. char talkingto[AST_MAX_BUF] = "";
  1695. char music[AST_MAX_BUF];
  1696. int count_agents = 0; /* Number of agents configured */
  1697. int online_agents = 0; /* Number of online agents */
  1698. int agent_status = 0; /* 0 means offline, 1 means online */
  1699. switch (cmd) {
  1700. case CLI_INIT:
  1701. e->command = "agent show online";
  1702. e->usage =
  1703. "Usage: agent show online\n"
  1704. " Provides a list of all online agents.\n";
  1705. return NULL;
  1706. case CLI_GENERATE:
  1707. return NULL;
  1708. }
  1709. if (a->argc != 3)
  1710. return CLI_SHOWUSAGE;
  1711. AST_LIST_LOCK(&agents);
  1712. AST_LIST_TRAVERSE(&agents, p, list) {
  1713. struct ast_channel *owner;
  1714. agent_status = 0; /* reset it to offline */
  1715. ast_mutex_lock(&p->lock);
  1716. owner = agent_lock_owner(p);
  1717. if (!ast_strlen_zero(p->name))
  1718. snprintf(username, sizeof(username), "(%s) ", p->name);
  1719. else
  1720. username[0] = '\0';
  1721. if (p->chan) {
  1722. snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
  1723. if (owner && ast_bridged_channel(owner)) {
  1724. snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(owner)->name);
  1725. } else {
  1726. strcpy(talkingto, " is idle");
  1727. }
  1728. agent_status = 1;
  1729. online_agents++;
  1730. }
  1731. if (owner) {
  1732. ast_channel_unlock(owner);
  1733. owner = ast_channel_unref(owner);
  1734. }
  1735. if (!ast_strlen_zero(p->moh))
  1736. snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
  1737. if (agent_status)
  1738. ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent, username, location, talkingto, music);
  1739. count_agents++;
  1740. ast_mutex_unlock(&p->lock);
  1741. }
  1742. AST_LIST_UNLOCK(&agents);
  1743. if (!count_agents)
  1744. ast_cli(a->fd, "No Agents are configured in %s\n", config);
  1745. else
  1746. ast_cli(a->fd, "%d agents online\n", online_agents);
  1747. ast_cli(a->fd, "\n");
  1748. return CLI_SUCCESS;
  1749. }
  1750. static const char agent_logoff_usage[] =
  1751. "Usage: agent logoff <channel> [soft]\n"
  1752. " Sets an agent as no longer logged in.\n"
  1753. " If 'soft' is specified, do not hangup existing calls.\n";
  1754. static struct ast_cli_entry cli_agents[] = {
  1755. AST_CLI_DEFINE(agents_show, "Show status of agents"),
  1756. AST_CLI_DEFINE(agents_show_online, "Show all online agents"),
  1757. AST_CLI_DEFINE(agent_logoff_cmd, "Sets an agent offline"),
  1758. };
  1759. /*!
  1760. * Called by the AgentLogin application (from the dial plan).
  1761. *
  1762. * \brief Log in agent application.
  1763. *
  1764. * \param chan
  1765. * \param data
  1766. * \returns
  1767. * \sa agentmonitoroutgoing_exec(), load_module().
  1768. */
  1769. static int login_exec(struct ast_channel *chan, const char *data)
  1770. {
  1771. int res=0;
  1772. int tries = 0;
  1773. int max_login_tries = maxlogintries;
  1774. struct agent_pvt *p;
  1775. struct ast_module_user *u;
  1776. char user[AST_MAX_AGENT] = "";
  1777. char pass[AST_MAX_AGENT];
  1778. char agent[AST_MAX_AGENT] = "";
  1779. char xpass[AST_MAX_AGENT] = "";
  1780. char *errmsg;
  1781. char *parse;
  1782. AST_DECLARE_APP_ARGS(args,
  1783. AST_APP_ARG(agent_id);
  1784. AST_APP_ARG(options);
  1785. AST_APP_ARG(extension);
  1786. );
  1787. const char *tmpoptions = NULL;
  1788. int play_announcement = 1;
  1789. char agent_goodbye[AST_MAX_FILENAME_LEN];
  1790. int update_cdr = updatecdr;
  1791. char *filename = "agent-loginok";
  1792. u = ast_module_user_add(chan);
  1793. parse = ast_strdupa(data);
  1794. AST_STANDARD_APP_ARGS(args, parse);
  1795. ast_copy_string(agent_goodbye, agentgoodbye, sizeof(agent_goodbye));
  1796. ast_channel_lock(chan);
  1797. /* Set Channel Specific Login Overrides */
  1798. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTLMAXLOGINTRIES"))) {
  1799. max_login_tries = atoi(pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES"));
  1800. if (max_login_tries < 0)
  1801. max_login_tries = 0;
  1802. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES");
  1803. ast_verb(3, "Saw variable AGENTMAXLOGINTRIES=%s, setting max_login_tries to: %d on Channel '%s'.\n",tmpoptions,max_login_tries,chan->name);
  1804. }
  1805. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR"))) {
  1806. if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR")))
  1807. update_cdr = 1;
  1808. else
  1809. update_cdr = 0;
  1810. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR");
  1811. ast_verb(3, "Saw variable AGENTUPDATECDR=%s, setting update_cdr to: %d on Channel '%s'.\n",tmpoptions,update_cdr,chan->name);
  1812. }
  1813. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"))) {
  1814. strcpy(agent_goodbye, pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"));
  1815. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTGOODBYE");
  1816. ast_verb(3, "Saw variable AGENTGOODBYE=%s, setting agent_goodbye to: %s on Channel '%s'.\n",tmpoptions,agent_goodbye,chan->name);
  1817. }
  1818. ast_channel_unlock(chan);
  1819. /* End Channel Specific Login Overrides */
  1820. if (!ast_strlen_zero(args.options)) {
  1821. if (strchr(args.options, 's')) {
  1822. play_announcement = 0;
  1823. }
  1824. }
  1825. if (chan->_state != AST_STATE_UP)
  1826. res = ast_answer(chan);
  1827. if (!res) {
  1828. if (!ast_strlen_zero(args.agent_id))
  1829. ast_copy_string(user, args.agent_id, AST_MAX_AGENT);
  1830. else
  1831. res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
  1832. }
  1833. while (!res && (max_login_tries==0 || tries < max_login_tries)) {
  1834. tries++;
  1835. /* Check for password */
  1836. AST_LIST_LOCK(&agents);
  1837. AST_LIST_TRAVERSE(&agents, p, list) {
  1838. if (!strcmp(p->agent, user) && !p->pending)
  1839. ast_copy_string(xpass, p->password, sizeof(xpass));
  1840. }
  1841. AST_LIST_UNLOCK(&agents);
  1842. if (!res) {
  1843. if (!ast_strlen_zero(xpass))
  1844. res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
  1845. else
  1846. pass[0] = '\0';
  1847. }
  1848. errmsg = "agent-incorrect";
  1849. #if 0
  1850. ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
  1851. #endif
  1852. /* Check again for accuracy */
  1853. AST_LIST_LOCK(&agents);
  1854. AST_LIST_TRAVERSE(&agents, p, list) {
  1855. int unlock_channel = 1;
  1856. ast_channel_lock(chan);
  1857. ast_mutex_lock(&p->lock);
  1858. if (!strcmp(p->agent, user) &&
  1859. !strcmp(p->password, pass) && !p->pending) {
  1860. /* Ensure we can't be gotten until we're done */
  1861. p->lastdisc = ast_tvnow();
  1862. p->lastdisc.tv_sec++;
  1863. /* Set Channel Specific Agent Overrides */
  1864. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
  1865. if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
  1866. p->ackcall = 1;
  1867. } else {
  1868. p->ackcall = 0;
  1869. }
  1870. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTACKCALL");
  1871. ast_verb(3, "Saw variable AGENTACKCALL=%s, setting ackcall to: %d for Agent '%s'.\n", tmpoptions, p->ackcall, p->agent);
  1872. ast_set_flag(p, AGENT_FLAG_ACKCALL);
  1873. } else {
  1874. p->ackcall = ackcall;
  1875. }
  1876. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"))) {
  1877. p->autologoff = atoi(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"));
  1878. if (p->autologoff < 0)
  1879. p->autologoff = 0;
  1880. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF");
  1881. ast_verb(3, "Saw variable AGENTAUTOLOGOFF=%s, setting autologff to: %d for Agent '%s'.\n", tmpoptions, p->autologoff, p->agent);
  1882. ast_set_flag(p, AGENT_FLAG_AUTOLOGOFF);
  1883. } else {
  1884. p->autologoff = autologoff;
  1885. }
  1886. if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"))) {
  1887. p->wrapuptime = atoi(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"));
  1888. if (p->wrapuptime < 0)
  1889. p->wrapuptime = 0;
  1890. tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME");
  1891. ast_verb(3, "Saw variable AGENTWRAPUPTIME=%s, setting wrapuptime to: %d for Agent '%s'.\n", tmpoptions, p->wrapuptime, p->agent);
  1892. ast_set_flag(p, AGENT_FLAG_WRAPUPTIME);
  1893. } else {
  1894. p->wrapuptime = wrapuptime;
  1895. }
  1896. tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTACCEPTDTMF");
  1897. if (!ast_strlen_zero(tmpoptions)) {
  1898. p->acceptdtmf = *tmpoptions;
  1899. ast_verb(3, "Saw variable AGENTACCEPTDTMF=%s, setting acceptdtmf to: %c for Agent '%s'.\n", tmpoptions, p->acceptdtmf, p->agent);
  1900. ast_set_flag(p, AGENT_FLAG_ACCEPTDTMF);
  1901. }
  1902. tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTENDDTMF");
  1903. if (!ast_strlen_zero(tmpoptions)) {
  1904. p->enddtmf = *tmpoptions;
  1905. ast_verb(3, "Saw variable AGENTENDDTMF=%s, setting enddtmf to: %c for Agent '%s'.\n", tmpoptions, p->enddtmf, p->agent);
  1906. ast_set_flag(p, AGENT_FLAG_ENDDTMF);
  1907. }
  1908. ast_channel_unlock(chan);
  1909. unlock_channel = 0;
  1910. /* End Channel Specific Agent Overrides */
  1911. if (!p->chan) {
  1912. long logintime;
  1913. snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
  1914. p->logincallerid[0] = '\0';
  1915. p->acknowledged = 0;
  1916. ast_mutex_unlock(&p->lock);
  1917. AST_LIST_UNLOCK(&agents);
  1918. if( !res && play_announcement==1 )
  1919. res = ast_streamfile(chan, filename, chan->language);
  1920. if (!res)
  1921. ast_waitstream(chan, "");
  1922. AST_LIST_LOCK(&agents);
  1923. ast_mutex_lock(&p->lock);
  1924. if (!res) {
  1925. struct ast_format tmpfmt;
  1926. res = ast_set_read_format_from_cap(chan, chan->nativeformats);
  1927. if (res) {
  1928. ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
  1929. }
  1930. }
  1931. if (!res) {
  1932. struct ast_format tmpfmt;
  1933. res = ast_set_write_format_from_cap(chan, chan->nativeformats);
  1934. if (res) {
  1935. ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
  1936. }
  1937. }
  1938. /* Check once more just in case */
  1939. if (p->chan)
  1940. res = -1;
  1941. if (!res) {
  1942. ast_indicate_data(chan, AST_CONTROL_HOLD,
  1943. S_OR(p->moh, NULL),
  1944. !ast_strlen_zero(p->moh) ? strlen(p->moh) + 1 : 0);
  1945. if (p->loginstart == 0)
  1946. time(&p->loginstart);
  1947. manager_event(EVENT_FLAG_AGENT, "Agentlogin",
  1948. "Agent: %s\r\n"
  1949. "Channel: %s\r\n"
  1950. "Uniqueid: %s\r\n",
  1951. p->agent, chan->name, chan->uniqueid);
  1952. if (update_cdr && chan->cdr)
  1953. snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
  1954. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGIN", "%s", chan->name);
  1955. ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", p->agent,
  1956. ast_getformatname(&chan->readformat), ast_getformatname(&chan->writeformat));
  1957. /* Login this channel and wait for it to go away */
  1958. p->chan = chan;
  1959. if (p->ackcall) {
  1960. check_beep(p, 0);
  1961. } else {
  1962. check_availability(p, 0);
  1963. }
  1964. ast_mutex_unlock(&p->lock);
  1965. AST_LIST_UNLOCK(&agents);
  1966. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  1967. while (res >= 0) {
  1968. ast_mutex_lock(&p->lock);
  1969. if (p->deferlogoff && p->chan) {
  1970. ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
  1971. p->deferlogoff = 0;
  1972. }
  1973. if (p->chan != chan)
  1974. res = -1;
  1975. ast_mutex_unlock(&p->lock);
  1976. /* Yield here so other interested threads can kick in. */
  1977. sched_yield();
  1978. if (res)
  1979. break;
  1980. AST_LIST_LOCK(&agents);
  1981. ast_mutex_lock(&p->lock);
  1982. if (p->lastdisc.tv_sec) {
  1983. if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
  1984. ast_debug(1, "Wrapup time for %s expired!\n", p->agent);
  1985. p->lastdisc = ast_tv(0, 0);
  1986. ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  1987. if (p->ackcall) {
  1988. check_beep(p, 0);
  1989. } else {
  1990. check_availability(p, 0);
  1991. }
  1992. }
  1993. }
  1994. ast_mutex_unlock(&p->lock);
  1995. AST_LIST_UNLOCK(&agents);
  1996. /* Synchronize channel ownership between call to agent and itself. */
  1997. ast_mutex_lock(&p->lock);
  1998. if (p->app_lock_flag == 1) {
  1999. ast_cond_signal(&p->login_wait_cond);
  2000. ast_cond_wait(&p->app_complete_cond, &p->lock);
  2001. }
  2002. ast_mutex_unlock(&p->lock);
  2003. if (p->ackcall) {
  2004. res = agent_ack_sleep(p);
  2005. } else {
  2006. res = ast_safe_sleep_conditional( chan, 1000, agent_cont_sleep, p );
  2007. }
  2008. if (p->ackcall && (res == 1)) {
  2009. AST_LIST_LOCK(&agents);
  2010. ast_mutex_lock(&p->lock);
  2011. check_availability(p, 0);
  2012. ast_mutex_unlock(&p->lock);
  2013. AST_LIST_UNLOCK(&agents);
  2014. res = 0;
  2015. }
  2016. sched_yield();
  2017. }
  2018. ast_mutex_lock(&p->lock);
  2019. /* Log us off if appropriate */
  2020. if (p->chan == chan) {
  2021. p->chan = NULL;
  2022. }
  2023. /* Synchronize channel ownership between call to agent and itself. */
  2024. if (p->app_lock_flag == 1) {
  2025. ast_cond_signal(&p->login_wait_cond);
  2026. ast_cond_wait(&p->app_complete_cond, &p->lock);
  2027. }
  2028. if (res && p->owner)
  2029. ast_log(LOG_WARNING, "Huh? We broke out when there was still an owner?\n");
  2030. p->acknowledged = 0;
  2031. logintime = time(NULL) - p->loginstart;
  2032. p->loginstart = 0;
  2033. ast_mutex_unlock(&p->lock);
  2034. manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
  2035. "Agent: %s\r\n"
  2036. "Logintime: %ld\r\n"
  2037. "Uniqueid: %s\r\n",
  2038. p->agent, logintime, chan->uniqueid);
  2039. ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGOFF", "%s|%ld", chan->name, logintime);
  2040. ast_verb(2, "Agent '%s' logged out\n", p->agent);
  2041. /* If there is no owner, go ahead and kill it now */
  2042. ast_devstate_changed(AST_DEVICE_UNAVAILABLE, AST_DEVSTATE_CACHABLE, "Agent/%s", p->agent);
  2043. if (p->dead && !p->owner) {
  2044. ast_mutex_destroy(&p->lock);
  2045. ast_cond_destroy(&p->app_complete_cond);
  2046. ast_cond_destroy(&p->login_wait_cond);
  2047. ast_free(p);
  2048. }
  2049. }
  2050. else {
  2051. ast_mutex_unlock(&p->lock);
  2052. p = NULL;
  2053. }
  2054. res = -1;
  2055. } else {
  2056. ast_mutex_unlock(&p->lock);
  2057. errmsg = "agent-alreadyon";
  2058. p = NULL;
  2059. }
  2060. break;
  2061. }
  2062. ast_mutex_unlock(&p->lock);
  2063. if (unlock_channel) {
  2064. ast_channel_unlock(chan);
  2065. }
  2066. }
  2067. if (!p)
  2068. AST_LIST_UNLOCK(&agents);
  2069. if (!res && (max_login_tries==0 || tries < max_login_tries))
  2070. res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
  2071. }
  2072. if (!res)
  2073. res = ast_safe_sleep(chan, 500);
  2074. ast_module_user_remove(u);
  2075. return -1;
  2076. }
  2077. /*!
  2078. * \brief Called by the AgentMonitorOutgoing application (from the dial plan).
  2079. *
  2080. * \param chan
  2081. * \param data
  2082. * \returns
  2083. * \sa login_exec(), load_module().
  2084. */
  2085. static int agentmonitoroutgoing_exec(struct ast_channel *chan, const char *data)
  2086. {
  2087. int exitifnoagentid = 0;
  2088. int nowarnings = 0;
  2089. int changeoutgoing = 0;
  2090. int res = 0;
  2091. char agent[AST_MAX_AGENT];
  2092. if (data) {
  2093. if (strchr(data, 'd'))
  2094. exitifnoagentid = 1;
  2095. if (strchr(data, 'n'))
  2096. nowarnings = 1;
  2097. if (strchr(data, 'c'))
  2098. changeoutgoing = 1;
  2099. }
  2100. if (chan->caller.id.number.valid
  2101. && !ast_strlen_zero(chan->caller.id.number.str)) {
  2102. const char *tmp;
  2103. char agentvar[AST_MAX_BUF];
  2104. snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID,
  2105. chan->caller.id.number.str);
  2106. if ((tmp = pbx_builtin_getvar_helper(NULL, agentvar))) {
  2107. struct agent_pvt *p;
  2108. ast_copy_string(agent, tmp, sizeof(agent));
  2109. AST_LIST_LOCK(&agents);
  2110. AST_LIST_TRAVERSE(&agents, p, list) {
  2111. if (!strcasecmp(p->agent, tmp)) {
  2112. if (changeoutgoing) snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
  2113. __agent_start_monitoring(chan, p, 1);
  2114. break;
  2115. }
  2116. }
  2117. AST_LIST_UNLOCK(&agents);
  2118. } else {
  2119. res = -1;
  2120. if (!nowarnings)
  2121. ast_log(LOG_WARNING, "Couldn't find the global variable %s, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n", agentvar);
  2122. }
  2123. } else {
  2124. res = -1;
  2125. if (!nowarnings)
  2126. ast_log(LOG_WARNING, "There is no callerid on that call, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n");
  2127. }
  2128. if (res) {
  2129. if (exitifnoagentid)
  2130. return res;
  2131. }
  2132. return 0;
  2133. }
  2134. /*! \brief Part of PBX channel interface */
  2135. static int agent_devicestate(void *data)
  2136. {
  2137. struct agent_pvt *p;
  2138. char *s;
  2139. ast_group_t groupmatch;
  2140. int groupoff;
  2141. int res = AST_DEVICE_INVALID;
  2142. s = data;
  2143. if ((s[0] == '@') && (sscanf(s + 1, "%30d", &groupoff) == 1))
  2144. groupmatch = (1 << groupoff);
  2145. else if ((s[0] == ':') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
  2146. groupmatch = (1 << groupoff);
  2147. } else
  2148. groupmatch = 0;
  2149. /* Check actual logged in agents first */
  2150. AST_LIST_LOCK(&agents);
  2151. AST_LIST_TRAVERSE(&agents, p, list) {
  2152. ast_mutex_lock(&p->lock);
  2153. if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
  2154. if (p->owner) {
  2155. if (res != AST_DEVICE_INUSE)
  2156. res = AST_DEVICE_BUSY;
  2157. } else {
  2158. if (res == AST_DEVICE_BUSY)
  2159. res = AST_DEVICE_INUSE;
  2160. if (p->chan) {
  2161. if (res == AST_DEVICE_INVALID)
  2162. res = AST_DEVICE_UNKNOWN;
  2163. } else if (res == AST_DEVICE_INVALID)
  2164. res = AST_DEVICE_UNAVAILABLE;
  2165. }
  2166. if (!strcmp(data, p->agent)) {
  2167. ast_mutex_unlock(&p->lock);
  2168. break;
  2169. }
  2170. }
  2171. ast_mutex_unlock(&p->lock);
  2172. }
  2173. AST_LIST_UNLOCK(&agents);
  2174. return res;
  2175. }
  2176. /*!
  2177. * \note This function expects the agent list to be locked
  2178. */
  2179. static struct agent_pvt *find_agent(char *agentid)
  2180. {
  2181. struct agent_pvt *cur;
  2182. AST_LIST_TRAVERSE(&agents, cur, list) {
  2183. if (!strcmp(cur->agent, agentid))
  2184. break;
  2185. }
  2186. return cur;
  2187. }
  2188. static int function_agent(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  2189. {
  2190. char *parse;
  2191. AST_DECLARE_APP_ARGS(args,
  2192. AST_APP_ARG(agentid);
  2193. AST_APP_ARG(item);
  2194. );
  2195. char *tmp;
  2196. struct agent_pvt *agent;
  2197. buf[0] = '\0';
  2198. if (ast_strlen_zero(data)) {
  2199. ast_log(LOG_WARNING, "The AGENT function requires an argument - agentid!\n");
  2200. return -1;
  2201. }
  2202. parse = ast_strdupa(data);
  2203. AST_NONSTANDARD_APP_ARGS(args, parse, ':');
  2204. if (!args.item)
  2205. args.item = "status";
  2206. AST_LIST_LOCK(&agents);
  2207. if (!(agent = find_agent(args.agentid))) {
  2208. AST_LIST_UNLOCK(&agents);
  2209. ast_log(LOG_WARNING, "Agent '%s' not found!\n", args.agentid);
  2210. return -1;
  2211. }
  2212. if (!strcasecmp(args.item, "status")) {
  2213. char *status = "LOGGEDOUT";
  2214. if (agent->chan) {
  2215. status = "LOGGEDIN";
  2216. }
  2217. ast_copy_string(buf, status, len);
  2218. } else if (!strcasecmp(args.item, "password"))
  2219. ast_copy_string(buf, agent->password, len);
  2220. else if (!strcasecmp(args.item, "name"))
  2221. ast_copy_string(buf, agent->name, len);
  2222. else if (!strcasecmp(args.item, "mohclass"))
  2223. ast_copy_string(buf, agent->moh, len);
  2224. else if (!strcasecmp(args.item, "channel")) {
  2225. if (agent->chan) {
  2226. ast_channel_lock(agent->chan);
  2227. ast_copy_string(buf, agent->chan->name, len);
  2228. ast_channel_unlock(agent->chan);
  2229. tmp = strrchr(buf, '-');
  2230. if (tmp)
  2231. *tmp = '\0';
  2232. }
  2233. } else if (!strcasecmp(args.item, "fullchannel")) {
  2234. if (agent->chan) {
  2235. ast_channel_lock(agent->chan);
  2236. ast_copy_string(buf, agent->chan->name, len);
  2237. ast_channel_unlock(agent->chan);
  2238. }
  2239. } else if (!strcasecmp(args.item, "exten")) {
  2240. buf[0] = '\0';
  2241. }
  2242. AST_LIST_UNLOCK(&agents);
  2243. return 0;
  2244. }
  2245. static struct ast_custom_function agent_function = {
  2246. .name = "AGENT",
  2247. .read = function_agent,
  2248. };
  2249. /*!
  2250. * \internal
  2251. * \brief Callback used to generate the agents tree.
  2252. * \param[in] search The search pattern tree.
  2253. * \retval NULL on error.
  2254. * \retval non-NULL The generated tree.
  2255. */
  2256. static int agents_data_provider_get(const struct ast_data_search *search,
  2257. struct ast_data *data_root)
  2258. {
  2259. struct agent_pvt *p;
  2260. struct ast_data *data_agent, *data_channel, *data_talkingto;
  2261. AST_LIST_LOCK(&agents);
  2262. AST_LIST_TRAVERSE(&agents, p, list) {
  2263. struct ast_channel *owner;
  2264. data_agent = ast_data_add_node(data_root, "agent");
  2265. if (!data_agent) {
  2266. continue;
  2267. }
  2268. ast_mutex_lock(&p->lock);
  2269. owner = agent_lock_owner(p);
  2270. if (!(p->pending)) {
  2271. ast_data_add_str(data_agent, "id", p->agent);
  2272. ast_data_add_structure(agent_pvt, data_agent, p);
  2273. ast_data_add_bool(data_agent, "logged", p->chan ? 1 : 0);
  2274. if (p->chan) {
  2275. data_channel = ast_data_add_node(data_agent, "loggedon");
  2276. if (!data_channel) {
  2277. ast_mutex_unlock(&p->lock);
  2278. ast_data_remove_node(data_root, data_agent);
  2279. if (owner) {
  2280. ast_channel_unlock(owner);
  2281. owner = ast_channel_unref(owner);
  2282. }
  2283. continue;
  2284. }
  2285. ast_channel_data_add_structure(data_channel, p->chan, 0);
  2286. if (owner && ast_bridged_channel(owner)) {
  2287. data_talkingto = ast_data_add_node(data_agent, "talkingto");
  2288. if (!data_talkingto) {
  2289. ast_mutex_unlock(&p->lock);
  2290. ast_data_remove_node(data_root, data_agent);
  2291. if (owner) {
  2292. ast_channel_unlock(owner);
  2293. owner = ast_channel_unref(owner);
  2294. }
  2295. continue;
  2296. }
  2297. ast_channel_data_add_structure(data_talkingto, ast_bridged_channel(owner), 0);
  2298. }
  2299. } else {
  2300. ast_data_add_node(data_agent, "talkingto");
  2301. ast_data_add_node(data_agent, "loggedon");
  2302. }
  2303. ast_data_add_str(data_agent, "musiconhold", p->moh);
  2304. }
  2305. if (owner) {
  2306. ast_channel_unlock(owner);
  2307. owner = ast_channel_unref(owner);
  2308. }
  2309. ast_mutex_unlock(&p->lock);
  2310. /* if this agent doesn't match remove the added agent. */
  2311. if (!ast_data_search_match(search, data_agent)) {
  2312. ast_data_remove_node(data_root, data_agent);
  2313. }
  2314. }
  2315. AST_LIST_UNLOCK(&agents);
  2316. return 0;
  2317. }
  2318. static const struct ast_data_handler agents_data_provider = {
  2319. .version = AST_DATA_HANDLER_VERSION,
  2320. .get = agents_data_provider_get
  2321. };
  2322. static const struct ast_data_entry agents_data_providers[] = {
  2323. AST_DATA_ENTRY("asterisk/channel/agent/list", &agents_data_provider),
  2324. };
  2325. /*!
  2326. * \brief Initialize the Agents module.
  2327. * This function is being called by Asterisk when loading the module.
  2328. * Among other things it registers applications, cli commands and reads the cofiguration file.
  2329. *
  2330. * \returns int Always 0.
  2331. */
  2332. static int load_module(void)
  2333. {
  2334. if (!(agent_tech.capabilities = ast_format_cap_alloc())) {
  2335. ast_log(LOG_ERROR, "ast_format_cap_alloc_nolock fail.\n");
  2336. return AST_MODULE_LOAD_FAILURE;
  2337. }
  2338. ast_format_cap_add_all(agent_tech.capabilities);
  2339. /* Make sure we can register our agent channel type */
  2340. if (ast_channel_register(&agent_tech)) {
  2341. agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
  2342. ast_log(LOG_ERROR, "Unable to register channel class 'Agent'\n");
  2343. return AST_MODULE_LOAD_FAILURE;
  2344. }
  2345. /* Read in the config */
  2346. if (!read_agent_config(0)) {
  2347. agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
  2348. return AST_MODULE_LOAD_DECLINE;
  2349. }
  2350. /* Dialplan applications */
  2351. ast_register_application_xml(app, login_exec);
  2352. ast_register_application_xml(app3, agentmonitoroutgoing_exec);
  2353. /* data tree */
  2354. ast_data_register_multiple(agents_data_providers, ARRAY_LEN(agents_data_providers));
  2355. /* Manager commands */
  2356. ast_manager_register_xml("Agents", EVENT_FLAG_AGENT, action_agents);
  2357. ast_manager_register_xml("AgentLogoff", EVENT_FLAG_AGENT, action_agent_logoff);
  2358. /* CLI Commands */
  2359. ast_cli_register_multiple(cli_agents, ARRAY_LEN(cli_agents));
  2360. /* Dialplan Functions */
  2361. ast_custom_function_register(&agent_function);
  2362. return AST_MODULE_LOAD_SUCCESS;
  2363. }
  2364. static int reload(void)
  2365. {
  2366. return read_agent_config(1);
  2367. }
  2368. static int unload_module(void)
  2369. {
  2370. struct agent_pvt *p;
  2371. /* First, take us out of the channel loop */
  2372. ast_channel_unregister(&agent_tech);
  2373. /* Unregister dialplan functions */
  2374. ast_custom_function_unregister(&agent_function);
  2375. /* Unregister CLI commands */
  2376. ast_cli_unregister_multiple(cli_agents, ARRAY_LEN(cli_agents));
  2377. /* Unregister dialplan applications */
  2378. ast_unregister_application(app);
  2379. ast_unregister_application(app3);
  2380. /* Unregister manager command */
  2381. ast_manager_unregister("Agents");
  2382. ast_manager_unregister("AgentLogoff");
  2383. /* Unregister the data tree */
  2384. ast_data_unregister(NULL);
  2385. /* Unregister channel */
  2386. AST_LIST_LOCK(&agents);
  2387. /* Hangup all interfaces if they have an owner */
  2388. while ((p = AST_LIST_REMOVE_HEAD(&agents, list))) {
  2389. if (p->owner)
  2390. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  2391. ast_free(p);
  2392. }
  2393. AST_LIST_UNLOCK(&agents);
  2394. agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
  2395. return 0;
  2396. }
  2397. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Agent Proxy Channel",
  2398. .load = load_module,
  2399. .unload = unload_module,
  2400. .reload = reload,
  2401. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  2402. .nonoptreq = "res_monitor,chan_local",
  2403. );