Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_agent_pool.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013 Digium, Inc.
5 *
6 * Richard Mudgett <rmudgett@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief Call center agent pool.
22 *
23 * \author Richard Mudgett <rmudgett@digium.com>
24 *
25 * See Also:
26 * \arg \ref AstCREDITS
27 * \arg \ref agents.conf "Config_agent"
28 */
29
30/*!
31 * \page agents.conf agents.conf
32 * \verbinclude agents.conf.sample
33 */
34
35/*** MODULEINFO
36 <support_level>core</support_level>
37 ***/
38
39
40#include "asterisk.h"
41
42#include "asterisk/cli.h"
43#include "asterisk/app.h"
44#include "asterisk/pbx.h"
45#include "asterisk/module.h"
46#include "asterisk/channel.h"
47#include "asterisk/bridge.h"
53#include "asterisk/astobj2.h"
56#include "asterisk/causes.h"
57
58/*** DOCUMENTATION
59 <application name="AgentLogin" language="en_US">
60 <since>
61 <version>12.0.0</version>
62 </since>
63 <synopsis>
64 Login an agent.
65 </synopsis>
66 <syntax argsep=",">
67 <parameter name="AgentId" required="true" />
68 <parameter name="options">
69 <optionlist>
70 <option name="s">
71 <para>silent login - do not announce the login ok segment after
72 agent logged on.</para>
73 </option>
74 </optionlist>
75 </parameter>
76 </syntax>
77 <description>
78 <para>Login an agent to the system. Any agent authentication is assumed to
79 already be done by dialplan. While logged in, the agent can receive calls
80 and will hear the sound file specified by the config option custom_beep
81 when a new call comes in for the agent. Login failures will continue in
82 the dialplan with <variable>AGENT_STATUS</variable> set.</para>
83 <para>Before logging in, you can setup on the real agent channel the
84 CHANNEL(dtmf_features) an agent will have when talking to a caller
85 and you can setup on the channel running this application the
86 CONNECTEDLINE() information the agent will see while waiting for a
87 caller.</para>
88 <para><variable>AGENT_STATUS</variable> enumeration values:</para>
89 <enumlist>
90 <enum name = "INVALID"><para>The specified agent is invalid.</para></enum>
91 <enum name = "ALREADY_LOGGED_IN"><para>The agent is already logged in.</para></enum>
92 </enumlist>
93 <note><para>The Agent:<replaceable>AgentId</replaceable> device state is
94 available to monitor the status of the agent.</para></note>
95 </description>
96 <see-also>
97 <ref type="application">Authenticate</ref>
98 <ref type="application">Queue</ref>
99 <ref type="application">AddQueueMember</ref>
100 <ref type="application">RemoveQueueMember</ref>
101 <ref type="application">PauseQueueMember</ref>
102 <ref type="application">UnpauseQueueMember</ref>
103 <ref type="function">AGENT</ref>
104 <ref type="function">CHANNEL</ref>
105 <ref type="function">CONNECTEDLINE</ref>
106 <ref type="filename">agents.conf</ref>
107 <ref type="filename">queues.conf</ref>
108 </see-also>
109 </application>
110 <application name="AgentRequest" language="en_US">
111 <since>
112 <version>12.0.0</version>
113 </since>
114 <synopsis>
115 Request an agent to connect with the channel.
116 </synopsis>
117 <syntax argsep=",">
118 <parameter name="AgentId" required="true" />
119 </syntax>
120 <description>
121 <para>Request an agent to connect with the channel. Failure to find,
122 alert the agent, or acknowledge the call will continue in the dialplan
123 with <variable>AGENT_STATUS</variable> set.</para>
124 <para><variable>AGENT_STATUS</variable> enumeration values:</para>
125 <enumlist>
126 <enum name = "INVALID"><para>The specified agent is invalid.</para></enum>
127 <enum name = "NOT_LOGGED_IN"><para>The agent is not available.</para></enum>
128 <enum name = "BUSY"><para>The agent is on another call.</para></enum>
129 <enum name = "NOT_CONNECTED"><para>The agent did not connect with the
130 call. The agent most likely did not acknowledge the call.</para></enum>
131 <enum name = "ERROR"><para>Alerting the agent failed.</para></enum>
132 </enumlist>
133 </description>
134 <see-also>
135 <ref type="application">AgentLogin</ref>
136 </see-also>
137 </application>
138 <function name="AGENT" language="en_US">
139 <since>
140 <version>12.0.0</version>
141 </since>
142 <synopsis>
143 Gets information about an Agent
144 </synopsis>
145 <syntax argsep=":">
146 <parameter name="AgentId" required="true" />
147 <parameter name="item">
148 <para>The valid items to retrieve are:</para>
149 <enumlist>
150 <enum name="status">
151 <para>(default) The status of the agent (LOGGEDIN | LOGGEDOUT)</para>
152 </enum>
153 <enum name="password">
154 <para>Deprecated. The dialplan handles any agent authentication.</para>
155 </enum>
156 <enum name="name">
157 <para>The name of the agent</para>
158 </enum>
159 <enum name="mohclass">
160 <para>MusicOnHold class</para>
161 </enum>
162 <enum name="channel">
163 <para>The name of the active channel for the Agent (AgentLogin)</para>
164 </enum>
165 <enum name="fullchannel">
166 <para>The untruncated name of the active channel for the Agent (AgentLogin)</para>
167 </enum>
168 </enumlist>
169 </parameter>
170 </syntax>
171 <description></description>
172 </function>
173 <manager name="Agents" language="en_US">
174 <since>
175 <version>1.2.0</version>
176 </since>
177 <synopsis>
178 Lists agents and their status.
179 </synopsis>
180 <syntax>
181 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
182 </syntax>
183 <description>
184 <para>Will list info about all defined agents.</para>
185 </description>
186 <see-also>
187 <ref type="managerEvent">Agents</ref>
188 <ref type="managerEvent">AgentsComplete</ref>
189 </see-also>
190 </manager>
191 <managerEvent language="en_US" name="Agents">
192 <managerEventInstance class="EVENT_FLAG_AGENT">
193 <since>
194 <version>12.0.0</version>
195 </since>
196 <synopsis>
197 Response event in a series to the Agents AMI action containing
198 information about a defined agent.
199 </synopsis>
200 <syntax>
201 <parameter name="Agent">
202 <para>Agent ID of the agent.</para>
203 </parameter>
204 <parameter name="Name">
205 <para>User friendly name of the agent.</para>
206 </parameter>
207 <parameter name="Status">
208 <para>Current status of the agent.</para>
209 <para>The valid values are:</para>
210 <enumlist>
211 <enum name="AGENT_LOGGEDOFF" />
212 <enum name="AGENT_IDLE" />
213 <enum name="AGENT_ONCALL" />
214 </enumlist>
215 </parameter>
216 <parameter name="TalkingToChan">
217 <para><variable>BRIDGEPEER</variable> value on agent channel.</para>
218 <para>Present if Status value is <literal>AGENT_ONCALL</literal>.</para>
219 </parameter>
220 <parameter name="CallStarted">
221 <para>Epoch time when the agent started talking with the caller.</para>
222 <para>Present if Status value is <literal>AGENT_ONCALL</literal>.</para>
223 </parameter>
224 <parameter name="LoggedInTime">
225 <para>Epoch time when the agent logged in.</para>
226 <para>Present if Status value is <literal>AGENT_IDLE</literal> or <literal>AGENT_ONCALL</literal>.</para>
227 </parameter>
228 <channel_snapshot/>
229 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
230 </syntax>
231 <description>
232 <para>The channel snapshot is present if the Status value is <literal>AGENT_IDLE</literal> or <literal>AGENT_ONCALL</literal>.</para>
233 </description>
234 <see-also>
235 <ref type="manager">Agents</ref>
236 </see-also>
237 </managerEventInstance>
238 </managerEvent>
239 <managerEvent language="en_US" name="AgentsComplete">
240 <managerEventInstance class="EVENT_FLAG_AGENT">
241 <since>
242 <version>12.0.0</version>
243 </since>
244 <synopsis>
245 Final response event in a series of events to the Agents AMI action.
246 </synopsis>
247 <syntax>
248 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
249 </syntax>
250 <see-also>
251 <ref type="manager">Agents</ref>
252 </see-also>
253 </managerEventInstance>
254 </managerEvent>
255 <manager name="AgentLogoff" language="en_US">
256 <since>
257 <version>1.2.0</version>
258 </since>
259 <synopsis>
260 Sets an agent as no longer logged in.
261 </synopsis>
262 <syntax>
263 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
264 <parameter name="Agent" required="true">
265 <para>Agent ID of the agent to log off.</para>
266 </parameter>
267 <parameter name="Soft">
268 <para>Set to <literal>true</literal> to not hangup existing calls.</para>
269 </parameter>
270 </syntax>
271 <description>
272 <para>Sets an agent as no longer logged in.</para>
273 </description>
274 </manager>
275 <configInfo name="app_agent_pool" language="en_US">
276 <synopsis>Agent pool applications</synopsis>
277 <description>
278 <note><para>Option changes take effect on agent login or after an agent
279 disconnects from a call.</para></note>
280 </description>
281 <configFile name="agents.conf">
282 <configObject name="global">
283 <synopsis>Unused, but reserved.</synopsis>
284 </configObject>
285 <configObject name="agent-id">
286 <since>
287 <version>12.0.0</version>
288 </since>
289 <synopsis>Configure an agent for the pool.</synopsis>
290 <description>
291 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
292 </description>
293 <configOption name="ackcall">
294 <since>
295 <version>12.0.0</version>
296 </since>
297 <synopsis>Enable to require the agent to acknowledge a call.</synopsis>
298 <description>
299 <para>Enable to require the agent to give a DTMF acknowledgement
300 when the agent receives a call.</para>
301 <note><para>The option is overridden by <variable>AGENTACKCALL</variable> on agent login.</para></note>
302 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
303 </description>
304 </configOption>
305 <configOption name="acceptdtmf">
306 <since>
307 <version>12.0.0</version>
308 </since>
309 <synopsis>DTMF key sequence the agent uses to acknowledge a call.</synopsis>
310 <description>
311 <note><para>The option is overridden by <variable>AGENTACCEPTDTMF</variable> on agent login.</para></note>
312 <note><para>The option is ignored unless the ackcall option is enabled.</para></note>
313 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
314 </description>
315 </configOption>
316 <configOption name="autologoff">
317 <since>
318 <version>12.0.0</version>
319 </since>
320 <synopsis>Time the agent has to acknowledge a call before being logged off.</synopsis>
321 <description>
322 <para>Set how many seconds a call for the agent has to wait for the
323 agent to acknowledge the call before the agent is automatically
324 logged off. If set to zero then the call will wait forever for
325 the agent to acknowledge.</para>
326 <note><para>The option is overridden by <variable>AGENTAUTOLOGOFF</variable> on agent login.</para></note>
327 <note><para>The option is ignored unless the ackcall option is enabled.</para></note>
328 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
329 </description>
330 </configOption>
331 <configOption name="wrapuptime">
332 <since>
333 <version>12.0.0</version>
334 </since>
335 <synopsis>Minimum time the agent has between calls.</synopsis>
336 <description>
337 <para>Set the minimum amount of time in milliseconds after
338 disconnecting a call before the agent can receive a new call.</para>
339 <note><para>The option is overridden by <variable>AGENTWRAPUPTIME</variable> on agent login.</para></note>
340 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
341 </description>
342 </configOption>
343 <configOption name="musiconhold">
344 <since>
345 <version>12.0.0</version>
346 </since>
347 <synopsis>Music on hold class the agent listens to between calls.</synopsis>
348 <description>
349 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
350 </description>
351 </configOption>
352 <configOption name="recordagentcalls">
353 <since>
354 <version>12.0.0</version>
355 </since>
356 <synopsis>Enable to automatically record calls the agent takes.</synopsis>
357 <description>
358 <para>Enable recording calls the agent takes automatically by
359 invoking the automixmon DTMF feature when the agent connects
360 to a caller. See <filename>features.conf.sample</filename> for information about
361 the automixmon feature.</para>
362 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
363 </description>
364 </configOption>
365 <configOption name="custom_beep">
366 <since>
367 <version>12.4.0</version>
368 </since>
369 <synopsis>Sound file played to alert the agent when a call is present.</synopsis>
370 <description>
371 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
372 </description>
373 </configOption>
374 <configOption name="fullname">
375 <since>
376 <version>12.0.0</version>
377 </since>
378 <synopsis>A friendly name for the agent used in log messages.</synopsis>
379 <description>
380 <xi:include xpointer="xpointer(/docs/configInfo[@name='app_agent_pool']/description/note)" />
381 </description>
382 </configOption>
383 </configObject>
384 </configFile>
385 </configInfo>
386 ***/
387
388/* ------------------------------------------------------------------- */
389
390#define AST_MAX_BUF 256
391
392/*! Maximum wait time (in ms) for the custom_beep file to play announcing the caller. */
393#define CALLER_SAFETY_TIMEOUT_TIME (2 * 60 * 1000)
394
395/*! Number of seconds to wait for local channel optimizations to complete. */
396#define LOGIN_WAIT_TIMEOUT_TIME 5
397
398static const char app_agent_login[] = "AgentLogin";
399static const char app_agent_request[] = "AgentRequest";
400
401/*! Agent config parameters. */
402struct agent_cfg {
404 /*! Identification of the agent. (agents config container key) */
406 /*! Name of agent for logging and querying purposes */
408
409 /*!
410 * \brief DTMF string for an agent to accept a call.
411 *
412 * \note The channel variable AGENTACCEPTDTMF overrides on login.
413 */
415 /*! Beep sound file to use. Alert the agent a call is waiting. */
417 /*! MOH class to use while agent waiting for call. */
419 );
420 /*!
421 * \brief Number of seconds for agent to ack a call before being logged off.
422 *
423 * \note The channel variable AGENTAUTOLOGOFF overrides on login.
424 * \note If zero then timer is disabled.
425 */
426 unsigned int auto_logoff;
427 /*!
428 * \brief Time after a call in ms before the agent can get a new call.
429 *
430 * \note The channel variable AGENTWRAPUPTIME overrides on login.
431 */
432 unsigned int wrapup_time;
433 /*!
434 * \brief TRUE if agent needs to ack a call to accept it.
435 *
436 * \note The channel variable AGENTACKCALL overrides on login.
437 */
439 /*! TRUE if agent calls are automatically recorded. */
441};
442
443/*!
444 * \internal
445 * \brief Agent config ao2 container sort function.
446 * \since 12.0.0
447 *
448 * \param obj_left pointer to the (user-defined part) of an object.
449 * \param obj_right pointer to the (user-defined part) of an object.
450 * \param flags flags from ao2_callback()
451 * OBJ_POINTER - if set, 'obj_right', is an object.
452 * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
453 * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
454 *
455 * \retval <0 if obj_left < obj_right
456 * \retval =0 if obj_left == obj_right
457 * \retval >0 if obj_left > obj_right
458 */
459static int agent_cfg_sort_cmp(const void *obj_left, const void *obj_right, int flags)
460{
461 const struct agent_cfg *cfg_left = obj_left;
462 const struct agent_cfg *cfg_right = obj_right;
463 const char *right_key = obj_right;
464 int cmp;
465
466 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
467 default:
468 case OBJ_POINTER:
469 right_key = cfg_right->username;
470 /* Fall through */
471 case OBJ_KEY:
472 cmp = strcmp(cfg_left->username, right_key);
473 break;
474 case OBJ_PARTIAL_KEY:
475 cmp = strncmp(cfg_left->username, right_key, strlen(right_key));
476 break;
477 }
478 return cmp;
479}
480
481static void agent_cfg_destructor(void *vdoomed)
482{
483 struct agent_cfg *doomed = vdoomed;
484
486}
487
488static void *agent_cfg_alloc(const char *name)
489{
490 struct agent_cfg *cfg;
491
492 cfg = ao2_alloc_options(sizeof(*cfg), agent_cfg_destructor,
494 if (!cfg || ast_string_field_init(cfg, 64)) {
495 ao2_cleanup(cfg);
496 return NULL;
497 }
499 return cfg;
500}
501
502static void *agent_cfg_find(struct ao2_container *agents, const char *username)
503{
505}
506
507/*! Agents configuration */
509 /*! Master configured agents container. */
511};
512
513static struct aco_type agent_type = {
514 .type = ACO_ITEM,
515 .name = "agent-id",
516 .category_match = ACO_BLACKLIST_EXACT,
517 .category = "general",
518 .item_alloc = agent_cfg_alloc,
519 .item_find = agent_cfg_find,
520 .item_offset = offsetof(struct agents_cfg, agents),
521};
522
524
525/* The general category is reserved, but unused */
526static struct aco_type general_type = {
527 .type = ACO_GLOBAL,
528 .name = "global",
529 .category_match = ACO_WHITELIST_EXACT,
530 .category = "general",
531};
532
533static struct aco_file agents_conf = {
534 .filename = "agents.conf",
535 .types = ACO_TYPES(&general_type, &agent_type),
536};
537
539
540static void agents_cfg_destructor(void *vdoomed)
541{
542 struct agents_cfg *doomed = vdoomed;
543
544 ao2_cleanup(doomed->agents);
545 doomed->agents = NULL;
546}
547
548/*!
549 * \internal
550 * \brief Create struct agents_cfg object.
551 * \since 12.0.0
552 *
553 * \note A lock is not needed for the object or any secondary
554 * created cfg objects. These objects are immutable after the
555 * config is loaded and applied.
556 *
557 * \retval New struct agents_cfg object.
558 * \retval NULL on error.
559 */
560static void *agents_cfg_alloc(void)
561{
562 struct agents_cfg *cfg;
563
564 cfg = ao2_alloc_options(sizeof(*cfg), agents_cfg_destructor,
566 if (!cfg) {
567 return NULL;
568 }
571 if (!cfg->agents) {
572 ao2_ref(cfg, -1);
573 cfg = NULL;
574 }
575 return cfg;
576}
577
578static void agents_post_apply_config(void);
579
581 .files = ACO_FILES(&agents_conf),
582 .post_apply_config = agents_post_apply_config,
583);
584
585static void destroy_config(void)
586{
588 aco_info_destroy(&cfg_info);
589}
590
591static int load_config(void)
592{
593 if (aco_info_init(&cfg_info)) {
594 return -1;
595 }
596
597 /* Agent options */
598 aco_option_register(&cfg_info, "ackcall", ACO_EXACT, agent_types, "no", OPT_BOOL_T, 1, FLDSET(struct agent_cfg, ack_call));
599 aco_option_register(&cfg_info, "acceptdtmf", ACO_EXACT, agent_types, "#", OPT_STRINGFIELD_T, 1, STRFLDSET(struct agent_cfg, dtmf_accept));
600 aco_option_register(&cfg_info, "autologoff", ACO_EXACT, agent_types, "0", OPT_UINT_T, 0, FLDSET(struct agent_cfg, auto_logoff));
601 aco_option_register(&cfg_info, "wrapuptime", ACO_EXACT, agent_types, "0", OPT_UINT_T, 0, FLDSET(struct agent_cfg, wrapup_time));
602 aco_option_register(&cfg_info, "musiconhold", ACO_EXACT, agent_types, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, moh));
603 aco_option_register(&cfg_info, "recordagentcalls", ACO_EXACT, agent_types, "no", OPT_BOOL_T, 1, FLDSET(struct agent_cfg, record_agent_calls));
604 aco_option_register(&cfg_info, "custom_beep", ACO_EXACT, agent_types, "beep", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, beep_sound));
605 aco_option_register(&cfg_info, "fullname", ACO_EXACT, agent_types, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, full_name));
606
607 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
608 return -1;
609 }
610
611 return 0;
612}
613
615 /*! The agent is defined but an agent is not present. */
617 /*! Forced initial login wait to allow any local channel optimizations to happen. */
619 /*! The agent is ready for a call. */
621 /*! The agent has a call waiting to connect. */
623 /*! The agent needs to ack the call. */
625 /*! The agent is connected with a call. */
627 /*! The agent is resting between calls. */
629 /*! The agent is being kicked out. */
631};
632
633/*! Agent config option override flags. */
639};
640
641/*! \brief Structure representing an agent. */
642struct agent_pvt {
644 /*! Identification of the agent. (agents container key) */
646 /*! Login override DTMF string for an agent to accept a call. */
648 );
649 /*! Connected line information to send when reentering the holding bridge. */
651 /*! Flags show if settings were overridden by channel vars. */
652 unsigned int flags;
653 /*! Login override number of seconds for agent to ack a call before being logged off. */
655 /*! Login override time after a call in ms before the agent can get a new call. */
657 /*! Login override if agent needs to ack a call to accept it. */
658 unsigned int override_ack_call:1;
659
660 /*! TRUE if the agent is requested to logoff when the current call ends. */
661 unsigned int deferred_logoff:1;
662
663 /*! Mark and sweep config update to determine if an agent is dead. */
664 unsigned int the_mark:1;
665 /*!
666 * \brief TRUE if the agent is no longer configured and is being destroyed.
667 *
668 * \note Agents cannot log in if they are dead.
669 */
670 unsigned int dead:1;
671
672 /*! Agent control state variable. */
674 /*! Custom device state of agent. */
676
677 /*! When agent first logged in */
679 /*! When agent login probation started. */
681 /*! When call started */
683 /*! When ack timer started */
684 struct timeval ack_time;
685 /*! When last disconnected */
686 struct timeval last_disconnect;
687
688 /*! Caller is waiting in this bridge for agent to join. (Holds ref) */
690 /*! Agent is logged in with this channel. (Holds ref) (NULL if not logged in.) */
692 /*! Active config values from config file. (Holds ref) */
693 struct agent_cfg *cfg;
694};
695
696/*! Container of defined agents. */
697static struct ao2_container *agents;
698
699/*!
700 * \brief Lock the agent.
701 *
702 * \param agent Agent to lock
703 */
704#define agent_lock(agent) _agent_lock(agent, __FILE__, __PRETTY_FUNCTION__, __LINE__, #agent)
705static inline void _agent_lock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
706{
707 __ao2_lock(agent, AO2_LOCK_REQ_MUTEX, file, function, line, var);
708}
709
710/*!
711 * \brief Unlock the agent.
712 *
713 * \param agent Agent to unlock
714 */
715#define agent_unlock(agent) _agent_unlock(agent, __FILE__, __PRETTY_FUNCTION__, __LINE__, #agent)
716static inline void _agent_unlock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
717{
718 __ao2_unlock(agent, file, function, line, var);
719}
720
721/*!
722 * \internal
723 * \brief Obtain the agent logged in channel lock if it exists.
724 * \since 12.0.0
725 *
726 * \param agent Pointer to the LOCKED agent_pvt.
727 *
728 * \note Assumes the agent lock is already obtained.
729 *
730 * \note Defined locking order is channel lock then agent lock.
731 */
732static struct ast_channel *agent_lock_logged(struct agent_pvt *agent)
733{
734 struct ast_channel *logged;
735
736 for (;;) {
737 if (!agent->logged) { /* No owner. Nothing to do. */
738 return NULL;
739 }
740
741 /* If we don't ref the logged, it could be killed when we unlock the agent. */
742 logged = ast_channel_ref(agent->logged);
743
744 /* Locking logged requires us to lock channel, then agent. */
745 agent_unlock(agent);
746 ast_channel_lock(logged);
747 agent_lock(agent);
748
749 /* Check if logged changed during agent unlock period */
750 if (logged != agent->logged) {
751 /* Channel changed. Unref and do another pass. */
752 ast_channel_unlock(logged);
753 ast_channel_unref(logged);
754 } else {
755 /* Channel stayed the same. Return it. */
756 return logged;
757 }
758 }
759}
760
761/*!
762 * \internal
763 * \brief Get the Agent:agent_id device state.
764 * \since 12.0.0
765 *
766 * \param agent_id Username of the agent.
767 *
768 * \details
769 * Search the agents container for the agent and return the
770 * current state.
771 *
772 * \return Device state of the agent.
773 */
774static enum ast_device_state agent_pvt_devstate_get(const char *agent_id)
775{
776 enum ast_device_state dev_state = AST_DEVICE_INVALID;
777 struct agent_pvt *agent;
778
779 agent = ao2_find(agents, agent_id, OBJ_KEY);
780 if (agent) {
781 agent_lock(agent);
782 dev_state = agent->devstate;
783 agent_unlock(agent);
784 ao2_ref(agent, -1);
785 }
786 return dev_state;
787}
788
789/*!
790 * \internal
791 * \brief Request an agent device state be updated.
792 * \since 12.0.0
793 *
794 * \param agent_id Which agent needs the device state updated.
795 */
796static void agent_devstate_changed(const char *agent_id)
797{
799}
800
801static void agent_pvt_destructor(void *vdoomed)
802{
803 struct agent_pvt *doomed = vdoomed;
804
805 /* Make sure device state reflects agent destruction. */
806 if (!ast_strlen_zero(doomed->username)) {
807 ast_debug(1, "Agent %s: Destroyed.\n", doomed->username);
809 }
810
812 if (doomed->caller_bridge) {
814 doomed->caller_bridge = NULL;
815 }
816 if (doomed->logged) {
817 doomed->logged = ast_channel_unref(doomed->logged);
818 }
819 ao2_cleanup(doomed->cfg);
820 doomed->cfg = NULL;
822}
823
824static struct agent_pvt *agent_pvt_new(struct agent_cfg *cfg)
825{
826 struct agent_pvt *agent;
827
828 agent = ao2_alloc(sizeof(*agent), agent_pvt_destructor);
829 if (!agent) {
830 return NULL;
831 }
832 if (ast_string_field_init(agent, 32)) {
833 ao2_ref(agent, -1);
834 return NULL;
835 }
838 ao2_ref(cfg, +1);
839 agent->cfg = cfg;
841 return agent;
842}
843
844/*!
845 * \internal
846 * \brief Agents ao2 container sort function.
847 * \since 12.0.0
848 *
849 * \param obj_left pointer to the (user-defined part) of an object.
850 * \param obj_right pointer to the (user-defined part) of an object.
851 * \param flags flags from ao2_callback()
852 * OBJ_POINTER - if set, 'obj_right', is an object.
853 * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
854 * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
855 *
856 * \retval <0 if obj_left < obj_right
857 * \retval =0 if obj_left == obj_right
858 * \retval >0 if obj_left > obj_right
859 */
860static int agent_pvt_sort_cmp(const void *obj_left, const void *obj_right, int flags)
861{
862 const struct agent_pvt *agent_left = obj_left;
863 const struct agent_pvt *agent_right = obj_right;
864 const char *right_key = obj_right;
865 int cmp;
866
867 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
868 default:
869 case OBJ_POINTER:
870 right_key = agent_right->username;
871 /* Fall through */
872 case OBJ_KEY:
873 cmp = strcmp(agent_left->username, right_key);
874 break;
875 case OBJ_PARTIAL_KEY:
876 cmp = strncmp(agent_left->username, right_key, strlen(right_key));
877 break;
878 }
879 return cmp;
880}
881
882/*!
883 * \internal
884 * \brief ao2_find() callback function.
885 * \since 12.0.0
886 *
887 * Usage:
888 * found = ao2_find(agents, agent, OBJ_POINTER);
889 * found = ao2_find(agents, "agent-id", OBJ_KEY);
890 * found = ao2_find(agents, agent->logged, 0);
891 */
892static int agent_pvt_cmp(void *obj, void *arg, int flags)
893{
894 const struct agent_pvt *agent = obj;
895 int cmp;
896
897 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
898 case OBJ_POINTER:
899 case OBJ_KEY:
900 case OBJ_PARTIAL_KEY:
901 cmp = CMP_MATCH;
902 break;
903 default:
904 if (agent->logged == arg) {
905 cmp = CMP_MATCH;
906 } else {
907 cmp = 0;
908 }
909 break;
910 }
911 return cmp;
912}
913
914static int agent_mark(void *obj, void *arg, int flags)
915{
916 struct agent_pvt *agent = obj;
917
918 agent_lock(agent);
919 agent->the_mark = 1;
920 agent_unlock(agent);
921 return 0;
922}
923
924static void agents_mark(void)
925{
927}
928
929static int agent_sweep(void *obj, void *arg, int flags)
930{
931 struct agent_pvt *agent = obj;
932 int cmp = 0;
933
934 agent_lock(agent);
935 if (agent->the_mark) {
936 agent->the_mark = 0;
937 agent->dead = 1;
938 /* Unlink dead agents immediately. */
939 cmp = CMP_MATCH;
940 }
941 agent_unlock(agent);
942 return cmp;
943}
944
945static void agents_sweep(void)
946{
947 struct ao2_iterator *iter;
948 struct agent_pvt *agent;
949 struct ast_channel *logged;
950
952 if (!iter) {
953 return;
954 }
955 for (; (agent = ao2_iterator_next(iter)); ao2_ref(agent, -1)) {
956 agent_lock(agent);
957 if (agent->logged) {
958 logged = ast_channel_ref(agent->logged);
959 } else {
960 logged = NULL;
961 }
962 agent_unlock(agent);
963 if (!logged) {
964 continue;
965 }
967 "Forced logoff of agent %s(%s). Agent no longer configured.\n",
968 agent->username, ast_channel_name(logged));
970 ast_channel_unref(logged);
971 }
973}
974
976{
977 struct ao2_iterator iter;
978 struct agent_cfg *cfg;
980
981 ast_assert(cfgs != NULL);
982
983 agents_mark();
984 iter = ao2_iterator_init(cfgs->agents, 0);
985 for (; (cfg = ao2_iterator_next(&iter)); ao2_ref(cfg, -1)) {
986 RAII_VAR(struct agent_pvt *, agent, ao2_find(agents, cfg->username, OBJ_KEY), ao2_cleanup);
987
988 if (agent) {
989 agent_lock(agent);
990 agent->the_mark = 0;
991 if (!agent->logged) {
992 struct agent_cfg *cfg_old;
993
994 /* Replace the config of agents not logged in. */
995 cfg_old = agent->cfg;
996 ao2_ref(cfg, +1);
997 agent->cfg = cfg;
998 ao2_cleanup(cfg_old);
999 }
1000 agent_unlock(agent);
1001 continue;
1002 }
1003 agent = agent_pvt_new(cfg);
1004 if (!agent) {
1005 continue;
1006 }
1007 ao2_link(agents, agent);
1008 ast_debug(1, "Agent %s: Created.\n", agent->username);
1009 agent_devstate_changed(agent->username);
1010 }
1011 ao2_iterator_destroy(&iter);
1012 agents_sweep();
1013}
1014
1015static int agent_logoff_request(const char *agent_id, int soft)
1016{
1017 struct ast_channel *logged;
1018 RAII_VAR(struct agent_pvt *, agent, ao2_find(agents, agent_id, OBJ_KEY), ao2_cleanup);
1019
1020 if (!agent) {
1021 return -1;
1022 }
1023
1024 agent_lock(agent);
1025 logged = agent_lock_logged(agent);
1026 if (logged) {
1027 if (soft) {
1028 agent->deferred_logoff = 1;
1029 } else {
1031 }
1032 ast_channel_unlock(logged);
1033 ast_channel_unref(logged);
1034 }
1035 agent_unlock(agent);
1036 return 0;
1037}
1038
1039/*! Agent holding bridge instance holder. */
1040static AO2_GLOBAL_OBJ_STATIC(agent_holding);
1041
1042/*! Agent holding bridge deferred creation lock. */
1044
1045/*!
1046 * \internal
1047 * \brief Callback to clear AGENT_STATUS on the caller channel.
1048 *
1049 * \param bridge_channel Which channel to operate on.
1050 * \param payload Data to pass to the callback. (NULL if none).
1051 * \param payload_size Size of the payload if payload is non-NULL. A number otherwise.
1052 *
1053 * \note The payload MUST NOT have any resources that need to be freed.
1054 */
1055static void clear_agent_status(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
1056{
1058}
1059
1060/*!
1061 * \internal
1062 * \brief Connect the agent with the waiting caller.
1063 * \since 12.0.0
1064 *
1065 * \param bridge_channel Agent channel connecting to the caller.
1066 * \param agent Which agent is connecting to the caller.
1067 *
1068 * \note The agent is locked on entry and not locked on exit.
1069 */
1071{
1072 struct ast_bridge *caller_bridge;
1073 int record_agent_calls;
1074 int res;
1075
1076 record_agent_calls = agent->cfg->record_agent_calls;
1077 caller_bridge = agent->caller_bridge;
1078 agent->caller_bridge = NULL;
1079 agent->state = AGENT_STATE_ON_CALL;
1080 time(&agent->call_start);
1081 agent_unlock(agent);
1082
1083 if (!caller_bridge) {
1084 /* Reset agent. */
1087 return;
1088 }
1089 res = ast_bridge_move(caller_bridge, bridge_channel->bridge, bridge_channel->chan,
1090 NULL, 0);
1091 if (res) {
1092 /* Reset agent. */
1093 ast_bridge_destroy(caller_bridge, 0);
1096 return;
1097 }
1100 if (res) {
1101 /* Reset agent. */
1102 ast_bridge_destroy(caller_bridge, 0);
1103 return;
1104 }
1105
1106 if (record_agent_calls) {
1108 .start_stop = AUTO_MONITOR_START,
1109 };
1110
1111 /*
1112 * The agent is in the new bridge so we can invoke the
1113 * mixmonitor hook to only start recording.
1114 */
1116 }
1117
1118 ao2_t_ref(caller_bridge, -1, "Agent successfully in caller_bridge");
1119}
1120
1121static int bridge_agent_hold_ack(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1122{
1123 struct agent_pvt *agent = hook_pvt;
1124
1125 agent_lock(agent);
1126 switch (agent->state) {
1128 /* Connect to caller now. */
1129 ast_debug(1, "Agent %s: Acked call.\n", agent->username);
1130 agent_connect_caller(bridge_channel, agent);/* Will unlock agent. */
1131 return 0;
1132 default:
1133 break;
1134 }
1135 agent_unlock(agent);
1136 return 0;
1137}
1138
1139static int bridge_agent_hold_heartbeat(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1140{
1141 struct agent_pvt *agent = hook_pvt;
1142 int probation_timedout = 0;
1143 int ack_timedout = 0;
1144 int wrapup_timedout = 0;
1145 int deferred_logoff;
1146 unsigned int wrapup_time;
1147 unsigned int auto_logoff;
1148
1149 agent_lock(agent);
1151 if (deferred_logoff) {
1153 }
1154
1155 switch (agent->state) {
1157 probation_timedout =
1158 LOGIN_WAIT_TIMEOUT_TIME <= (time(NULL) - agent->probation_start);
1159 if (probation_timedout) {
1160 /* Now ready for a caller. */
1163 }
1164 break;
1166 /* Check ack call time. */
1167 auto_logoff = agent->cfg->auto_logoff;
1169 auto_logoff = agent->override_auto_logoff;
1170 }
1171 if (auto_logoff) {
1172 auto_logoff *= 1000;
1173 ack_timedout = ast_tvdiff_ms(ast_tvnow(), agent->ack_time) > auto_logoff;
1174 if (ack_timedout) {
1176 }
1177 }
1178 break;
1180 /* Check wrapup time. */
1181 wrapup_time = agent->cfg->wrapup_time;
1183 wrapup_time = agent->override_wrapup_time;
1184 }
1185 wrapup_timedout = ast_tvdiff_ms(ast_tvnow(), agent->last_disconnect) > wrapup_time;
1186 if (wrapup_timedout) {
1189 }
1190 break;
1191 default:
1192 break;
1193 }
1194 agent_unlock(agent);
1195
1196 if (deferred_logoff) {
1197 ast_debug(1, "Agent %s: Deferred logoff.\n", agent->username);
1200 } else if (probation_timedout) {
1201 ast_debug(1, "Agent %s: Login complete.\n", agent->username);
1203 } else if (ack_timedout) {
1204 ast_debug(1, "Agent %s: Ack call timeout.\n", agent->username);
1207 } else if (wrapup_timedout) {
1208 ast_debug(1, "Agent %s: Wrapup timeout. Ready for new call.\n", agent->username);
1210 }
1211
1212 return 0;
1213}
1214
1215static void agent_after_bridge_cb(struct ast_channel *chan, void *data);
1216static void agent_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data);
1217
1218/*!
1219 * \internal
1220 * \brief ast_bridge agent_hold push method.
1221 * \since 12.0.0
1222 *
1223 * \param self Bridge to operate upon.
1224 * \param bridge_channel Bridge channel to push.
1225 * \param swap Bridge channel to swap places with if not NULL.
1226 *
1227 * \note On entry, self is already locked.
1228 *
1229 * \retval 0 on success
1230 * \retval -1 on failure
1231 */
1232static int bridge_agent_hold_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
1233{
1234 int res = 0;
1235 unsigned int wrapup_time;
1236 char dtmf[AST_FEATURE_MAX_LEN];
1237 struct ast_channel *chan;
1238 const char *moh_class;
1239 RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
1240
1241 chan = bridge_channel->chan;
1242
1243 agent = ao2_find(agents, swap ? swap->chan : chan, 0);
1244 if (!agent) {
1245 /* Could not find the agent. */
1246 return -1;
1247 }
1248
1249 /* Setup agent entertainment */
1250 agent_lock(agent);
1251 moh_class = ast_strdupa(agent->cfg->moh);
1252 agent_unlock(agent);
1253 res |= ast_channel_add_bridge_role(chan, "holding_participant");
1254 res |= ast_channel_set_bridge_role_option(chan, "holding_participant", "idle_mode", "musiconhold");
1255 res |= ast_channel_set_bridge_role_option(chan, "holding_participant", "moh_class", moh_class);
1256
1257 /* Add DTMF acknowledge hook. */
1258 dtmf[0] = '\0';
1259 agent_lock(agent);
1261 ? agent->override_ack_call : agent->cfg->ack_call) {
1262 const char *dtmf_accept;
1263
1264 dtmf_accept = ast_test_flag(agent, AGENT_FLAG_DTMF_ACCEPT)
1265 ? agent->override_dtmf_accept : agent->cfg->dtmf_accept;
1266 ast_copy_string(dtmf, dtmf_accept, sizeof(dtmf));
1267 }
1268 agent_unlock(agent);
1269 if (!ast_strlen_zero(dtmf)) {
1270 ao2_ref(agent, +1);
1273 ao2_ref(agent, -1);
1274 res = -1;
1275 }
1276 }
1277
1278 /* Add heartbeat interval hook. */
1279 ao2_ref(agent, +1);
1282 ao2_ref(agent, -1);
1283 res = -1;
1284 }
1285
1286 res |= ast_bridge_base_v_table.push(self, bridge_channel, swap);
1287 if (res) {
1288 ast_channel_remove_bridge_role(chan, "holding_participant");
1289 return -1;
1290 }
1291
1292 if (swap) {
1295 if (res) {
1296 ast_channel_remove_bridge_role(chan, "holding_participant");
1297 return -1;
1298 }
1299
1300 agent_lock(agent);
1301 ast_channel_unref(agent->logged);
1302 agent->logged = ast_channel_ref(chan);
1303 agent_unlock(agent);
1304
1305 /*
1306 * Kick the channel out so it can come back in fully controlled.
1307 * Otherwise, the after bridge callback will linger and the
1308 * agent will have some slightly different behavior in corner
1309 * cases.
1310 */
1313 return 0;
1314 }
1315
1316 agent_lock(agent);
1317 switch (agent->state) {
1319 /*!
1320 * \todo XXX the login probation time should be only if it is needed.
1321 *
1322 * Need to determine if there are any local channels that can
1323 * optimize and wait until they actually do before leaving the
1324 * AGENT_STATE_PROBATION_WAIT state. For now, the blind
1325 * timer of LOGIN_WAIT_TIMEOUT_TIME will do.
1326 */
1327 /*
1328 * Start the login probation timer.
1329 *
1330 * We cannot handle an agent local channel optimization when the
1331 * agent is on a call. The optimization may kick the agent
1332 * channel we know about out of the call without our being able
1333 * to switch to the replacement channel. Get any agent local
1334 * channel optimization out of the way while the agent is in the
1335 * holding bridge.
1336 */
1337 time(&agent->probation_start);
1338 agent->state = AGENT_STATE_PROBATION_WAIT;
1339 agent_unlock(agent);
1340 break;
1342 /* Restart the probation timer. */
1343 time(&agent->probation_start);
1344 agent_unlock(agent);
1345 break;
1347 /*
1348 * Likely someone manually kicked us out of the holding bridge
1349 * and we came right back in.
1350 */
1351 agent_unlock(agent);
1352 break;
1353 default:
1354 /* Unexpected agent state. */
1355 ast_assert(0);
1356 /* Fall through */
1359 agent->state = AGENT_STATE_READY_FOR_CALL;
1360 agent->devstate = AST_DEVICE_NOT_INUSE;
1361 agent_unlock(agent);
1362 ast_debug(1, "Agent %s: Call abort recovery complete.\n", agent->username);
1363 agent_devstate_changed(agent->username);
1364 break;
1367 wrapup_time = agent->cfg->wrapup_time;
1369 wrapup_time = agent->override_wrapup_time;
1370 }
1371 if (wrapup_time) {
1372 agent->state = AGENT_STATE_CALL_WRAPUP;
1373 } else {
1374 agent->state = AGENT_STATE_READY_FOR_CALL;
1375 agent->devstate = AST_DEVICE_NOT_INUSE;
1376 }
1377 agent_unlock(agent);
1378 if (!wrapup_time) {
1379 /* No wrapup time. */
1380 ast_debug(1, "Agent %s: Ready for new call.\n", agent->username);
1381 agent_devstate_changed(agent->username);
1382 }
1383 break;
1384 }
1385
1386 return 0;
1387}
1388
1389/*!
1390 * \internal
1391 * \brief ast_bridge agent_hold pull method.
1392 *
1393 * \param self Bridge to operate upon.
1394 * \param bridge_channel Bridge channel to pull.
1395 *
1396 * \details
1397 * Remove any channel hooks controlled by the bridge. Release
1398 * any resources held by bridge_channel->bridge_pvt and release
1399 * bridge_channel->bridge_pvt.
1400 *
1401 * \note On entry, self is already locked.
1402 */
1404{
1405 ast_channel_remove_bridge_role(bridge_channel->chan, "holding_participant");
1407}
1408
1409/*!
1410 * \brief The bridge is being dissolved.
1411 *
1412 * \param self Bridge to operate upon.
1413 *
1414 * \details
1415 * The bridge is being dissolved. Remove any external
1416 * references to the bridge so it can be destroyed.
1417 *
1418 * \note On entry, self must NOT be locked.
1419 */
1421{
1422 ao2_global_obj_release(agent_holding);
1424}
1425
1427
1429{
1430 struct ast_bridge *bridge;
1431
1432 bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_agent_hold_v_table);
1436 "AgentPool", NULL, NULL);
1437 bridge = bridge_register(bridge);
1438 return bridge;
1439}
1440
1441static void bridge_init_agent_hold(void)
1442{
1443 /* Setup bridge agent_hold subclass v_table. */
1445 bridge_agent_hold_v_table.name = "agent_hold";
1449}
1450
1452{
1453 RAII_VAR(struct ast_bridge *, holding, ao2_global_obj_ref(agent_holding), ao2_cleanup);
1454
1455 if (!holding) {
1457 holding = ao2_global_obj_ref(agent_holding);
1458 if (!holding) {
1459 holding = bridge_agent_hold_new();
1460 ao2_global_obj_replace_unref(agent_holding, holding);
1461 }
1463 if (!holding) {
1464 ast_log(LOG_ERROR, "Could not create agent holding bridge.\n");
1465 return -1;
1466 }
1467 }
1468 return 0;
1469}
1470
1471static void send_agent_login(struct ast_channel *chan, const char *agent)
1472{
1473 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1474
1475 ast_assert(agent != NULL);
1476
1477 blob = ast_json_pack("{s: s}",
1478 "agent", agent);
1479 if (!blob) {
1480 return;
1481 }
1482
1484}
1485
1486static void send_agent_logoff(struct ast_channel *chan, const char *agent, long logintime)
1487{
1488 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1489
1490 ast_assert(agent != NULL);
1491
1492 blob = ast_json_pack("{s: s, s: I}",
1493 "agent", agent,
1494 "logintime", (ast_json_int_t)logintime);
1495 if (!blob) {
1496 return;
1497 }
1498
1500}
1501
1502/*!
1503 * \internal
1504 * \brief Logout the agent.
1505 * \since 12.0.0
1506 *
1507 * \param agent Which agent logging out.
1508 *
1509 * \note On entry agent is already locked. On exit it is no longer locked.
1510 */
1511static void agent_logout(struct agent_pvt *agent)
1512{
1513 struct ast_channel *logged;
1514 struct ast_bridge *caller_bridge;
1515 long time_logged_in;
1516
1517 time_logged_in = time(NULL) - agent->login_start;
1518 logged = agent->logged;
1519 agent->logged = NULL;
1520 caller_bridge = agent->caller_bridge;
1521 agent->caller_bridge = NULL;
1525 agent_unlock(agent);
1527
1528 if (caller_bridge) {
1529 ast_bridge_destroy(caller_bridge, 0);
1530 }
1531
1532 ast_channel_lock(logged);
1533 send_agent_logoff(logged, agent->username, time_logged_in);
1534 ast_channel_unlock(logged);
1535 ast_verb(2, "Agent '%s' logged out. Logged in for %ld seconds.\n",
1536 agent->username, time_logged_in);
1537 ast_channel_unref(logged);
1538}
1539
1540/*!
1541 * \internal
1542 * \brief Agent driver loop.
1543 * \since 12.0.0
1544 *
1545 * \param agent Which agent.
1546 * \param logged The logged in channel.
1547 */
1548static void agent_run(struct agent_pvt *agent, struct ast_channel *logged)
1549{
1550 struct ast_bridge_features features;
1551
1552 if (ast_bridge_features_init(&features)) {
1554 goto agent_run_cleanup;
1555 }
1556 for (;;) {
1557 struct agents_cfg *cfgs;
1558 struct agent_cfg *cfg_new;
1559 struct agent_cfg *cfg_old;
1560 struct ast_bridge *holding;
1561 struct ast_bridge *caller_bridge;
1562
1564
1565 holding = ao2_global_obj_ref(agent_holding);
1566 if (!holding) {
1567 ast_debug(1, "Agent %s: Someone destroyed the agent holding bridge.\n",
1568 agent->username);
1569 break;
1570 }
1571
1572 /*
1573 * When the agent channel leaves the bridging system we usually
1574 * want to put the agent back into the holding bridge for the
1575 * next caller.
1576 */
1577 ast_bridge_join(holding, logged, NULL, &features, NULL,
1579 if (logged != agent->logged) {
1580 /* This channel is no longer the logged in agent. */
1581 break;
1582 }
1583
1584 if (agent->dead) {
1585 /* The agent is no longer configured. */
1586 break;
1587 }
1588
1589 /* Update the agent's config before rejoining the holding bridge. */
1591 if (!cfgs) {
1592 /* There is no agent configuration. All agents were destroyed. */
1593 break;
1594 }
1595 cfg_new = ao2_find(cfgs->agents, agent->username, OBJ_KEY);
1596 ao2_ref(cfgs, -1);
1597 if (!cfg_new) {
1598 /* The agent is no longer configured. */
1599 break;
1600 }
1601 agent_lock(agent);
1602 cfg_old = agent->cfg;
1603 agent->cfg = cfg_new;
1604
1605 agent->last_disconnect = ast_tvnow();
1606
1607 /* Clear out any caller bridge before rejoining the holding bridge. */
1608 caller_bridge = agent->caller_bridge;
1609 agent->caller_bridge = NULL;
1610 agent_unlock(agent);
1611 ao2_ref(cfg_old, -1);
1612 if (caller_bridge) {
1613 ast_bridge_destroy(caller_bridge, 0);
1614 }
1615
1616 if (agent->state == AGENT_STATE_LOGGING_OUT
1617 || agent->deferred_logoff
1618 || ast_check_hangup_locked(logged)) {
1619 /* The agent was requested to logout or hungup. */
1620 break;
1621 }
1622
1623 /*
1624 * It is safe to access agent->waiting_colp without a lock. It
1625 * is only setup on agent login and not changed.
1626 */
1628 }
1629 ast_bridge_features_cleanup(&features);
1630
1631agent_run_cleanup:
1632 agent_lock(agent);
1633 if (logged != agent->logged) {
1634 /*
1635 * We are no longer the agent channel because of local channel
1636 * optimization.
1637 */
1638 agent_unlock(agent);
1639 ast_debug(1, "Agent %s: Channel %s is no longer the agent.\n",
1640 agent->username, ast_channel_name(logged));
1641 return;
1642 }
1643 agent_logout(agent);
1644}
1645
1646static void agent_after_bridge_cb(struct ast_channel *chan, void *data)
1647{
1648 struct agent_pvt *agent;
1649
1650 agent = ao2_find(agents, chan, 0);
1651 if (!agent) {
1652 return;
1653 }
1654
1655 ast_debug(1, "Agent %s: New agent channel %s.\n",
1656 agent->username, ast_channel_name(chan));
1657 agent_run(agent, chan);
1658 ao2_ref(agent, -1);
1659}
1660
1662{
1663 struct ast_channel *chan = data;
1664 struct agent_pvt *agent;
1665
1666 agent = ao2_find(agents, chan, 0);
1667 if (!agent) {
1668 return;
1669 }
1670 ast_log(LOG_WARNING, "Agent %s: Forced logout. Lost control of %s because: %s\n",
1671 agent->username, ast_channel_name(chan),
1673 agent_lock(agent);
1674 agent_logout(agent);
1675 ao2_ref(agent, -1);
1676}
1677
1678/*!
1679 * \internal
1680 * \brief Get the lock on the agent bridge_channel and return it.
1681 * \since 12.0.0
1682 *
1683 * \param agent Whose bridge_channel to get.
1684 *
1685 * \retval bridge_channel on success (Reffed and locked).
1686 * \retval NULL on error.
1687 */
1689{
1690 struct ast_channel *logged;
1691 struct ast_bridge_channel *bc;
1692
1693 for (;;) {
1694 agent_lock(agent);
1695 logged = agent->logged;
1696 if (!logged) {
1697 agent_unlock(agent);
1698 return NULL;
1699 }
1700 ast_channel_ref(logged);
1701 agent_unlock(agent);
1702
1703 ast_channel_lock(logged);
1705 ast_channel_unlock(logged);
1706 ast_channel_unref(logged);
1707 if (!bc) {
1708 if (agent->logged != logged) {
1709 continue;
1710 }
1711 return NULL;
1712 }
1713
1715 if (bc->chan != logged || agent->logged != logged) {
1717 ao2_ref(bc, -1);
1718 continue;
1719 }
1720 return bc;
1721 }
1722}
1723
1724static void caller_abort_agent(struct agent_pvt *agent)
1725{
1726 struct ast_bridge_channel *logged;
1727
1728 logged = agent_bridge_channel_get_lock(agent);
1729 if (!logged) {
1730 struct ast_bridge *caller_bridge;
1731
1732 ast_debug(1, "Agent '%s' no longer logged in.\n", agent->username);
1733
1734 agent_lock(agent);
1735 caller_bridge = agent->caller_bridge;
1736 agent->caller_bridge = NULL;
1737 agent_unlock(agent);
1738 if (caller_bridge) {
1739 ast_bridge_destroy(caller_bridge, 0);
1740 }
1741 return;
1742 }
1743
1744 /* Kick the agent out of the holding bridge to reset it. */
1748}
1749
1750static int caller_safety_timeout(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1751{
1752 struct agent_pvt *agent = hook_pvt;
1753
1754 if (agent->state == AGENT_STATE_CALL_PRESENT) {
1755 ast_log(LOG_WARNING, "Agent '%s' process did not respond. Safety timeout.\n",
1756 agent->username);
1757 pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
1758
1760 caller_abort_agent(agent);
1761 }
1762
1763 return -1;
1764}
1765
1766static void agent_alert(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
1767{
1768 const char *agent_id = payload;
1769 const char *playfile;
1770 const char *dtmf_accept;
1771 struct agent_pvt *agent;
1772 int digit;
1773 char dtmf[2];
1774
1775 agent = ao2_find(agents, agent_id, OBJ_KEY);
1776 if (!agent) {
1777 ast_debug(1, "Agent '%s' does not exist. Where did it go?\n", agent_id);
1778 return;
1779 }
1780
1781 /* Change holding bridge participant role's idle mode to silence */
1782 ast_bridge_channel_lock_bridge(bridge_channel);
1783 ast_bridge_channel_clear_roles(bridge_channel);
1784 ast_channel_set_bridge_role_option(bridge_channel->chan, "holding_participant", "idle_mode", "silence");
1785 ast_bridge_channel_establish_roles(bridge_channel);
1786 ast_bridge_unlock(bridge_channel->bridge);
1787
1788 agent_lock(agent);
1789 playfile = ast_strdupa(agent->cfg->beep_sound);
1790
1791 /* Determine which DTMF digits interrupt the alerting signal. */
1793 ? agent->override_ack_call : agent->cfg->ack_call) {
1794 dtmf_accept = ast_test_flag(agent, AGENT_FLAG_DTMF_ACCEPT)
1795 ? agent->override_dtmf_accept : agent->cfg->dtmf_accept;
1796
1797 /* Only the first digit of the ack will stop playback. */
1798 dtmf[0] = *dtmf_accept;
1799 dtmf[1] = '\0';
1800 dtmf_accept = dtmf;
1801 } else {
1802 dtmf_accept = NULL;
1803 }
1804 agent_unlock(agent);
1805
1806 /* Alert the agent. */
1807 digit = ast_stream_and_wait(bridge_channel->chan, playfile,
1808 ast_strlen_zero(dtmf_accept) ? AST_DIGIT_ANY : dtmf_accept);
1809 ast_stopstream(bridge_channel->chan);
1810
1811 agent_lock(agent);
1812 switch (agent->state) {
1814 if (!ast_strlen_zero(dtmf_accept)) {
1816 agent->ack_time = ast_tvnow();
1817
1818 if (0 < digit) {
1819 /* Playback was interrupted by a digit. */
1820 agent_unlock(agent);
1821 ao2_ref(agent, -1);
1822 ast_bridge_channel_feature_digit(bridge_channel, digit);
1823 return;
1824 }
1825 break;
1826 }
1827
1828 /* Connect to caller now. */
1829 ast_debug(1, "Agent %s: Immediately connecting to call.\n", agent->username);
1830 agent_connect_caller(bridge_channel, agent);/* Will unlock agent. */
1831 ao2_ref(agent, -1);
1832 return;
1833 default:
1834 break;
1835 }
1836 agent_unlock(agent);
1837 ao2_ref(agent, -1);
1838}
1839
1840static int send_alert_to_agent(struct ast_bridge_channel *bridge_channel, const char *agent_id)
1841{
1842 return ast_bridge_channel_queue_callback(bridge_channel,
1843 AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA, agent_alert, agent_id, strlen(agent_id) + 1);
1844}
1845
1847{
1849 .id.name = 1,
1850 .id.number = 1,
1851 .id.subaddress = 1,
1852 };
1853 unsigned char data[1024]; /* This should be large enough */
1854 size_t datalen;
1855
1856 datalen = ast_connected_line_build_data(data, sizeof(data), connected, &update);
1857 if (datalen == (size_t) -1) {
1858 return 0;
1859 }
1860
1861 return ast_bridge_channel_queue_control_data(bridge_channel,
1862 AST_CONTROL_CONNECTED_LINE, data, datalen);
1863}
1864
1865/*!
1866 * \internal
1867 * \brief Caller joined the bridge event callback.
1868 *
1869 * \param bridge_channel Channel executing the feature
1870 * \param hook_pvt Private data passed in when the hook was created
1871 *
1872 * \retval 0 Keep the callback hook.
1873 * \retval -1 Remove the callback hook.
1874 */
1875static int caller_joined_bridge(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
1876{
1877 struct agent_pvt *agent = hook_pvt;
1878 struct ast_bridge_channel *logged;
1879 int res;
1880
1881 logged = agent_bridge_channel_get_lock(agent);
1882 if (!logged) {
1883 ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
1884 pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_LOGGED_IN");
1885
1887 caller_abort_agent(agent);
1888 return -1;
1889 }
1890
1891 res = send_alert_to_agent(logged, agent->username);
1893 ao2_ref(logged, -1);
1894 if (res) {
1895 ast_verb(3, "Agent '%s': Failed to alert the agent.\n", agent->username);
1896 pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
1897
1899 caller_abort_agent(agent);
1900 return -1;
1901 }
1902
1903 pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_CONNECTED");
1904 ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
1905 return -1;
1906}
1907
1908/*!
1909 * \brief Dialplan AgentRequest application to locate an agent to talk with.
1910 *
1911 * \param chan Channel wanting to talk with an agent.
1912 * \param data Application parameters
1913 *
1914 * \retval 0 To continue in dialplan.
1915 * \retval -1 To hangup.
1916 */
1917static int agent_request_exec(struct ast_channel *chan, const char *data)
1918{
1919 struct ast_bridge *caller_bridge;
1920 struct ast_bridge_channel *logged;
1921 char *parse;
1922 int res;
1923 struct ast_bridge_features caller_features;
1926 AST_APP_ARG(agent_id);
1927 AST_APP_ARG(other); /* Any remaining unused arguments */
1928 );
1929
1930 RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
1931
1933 return -1;
1934 }
1935
1936 parse = ast_strdupa(data);
1938
1939 if (ast_strlen_zero(args.agent_id)) {
1940 ast_log(LOG_WARNING, "AgentRequest requires an AgentId\n");
1941 return -1;
1942 }
1943
1944 /* Find the agent. */
1945 agent = ao2_find(agents, args.agent_id, OBJ_KEY);
1946 if (!agent) {
1947 ast_verb(3, "Agent '%s' does not exist.\n", args.agent_id);
1948 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "INVALID");
1949 return 0;
1950 }
1951
1952 if (ast_bridge_features_init(&caller_features)) {
1953 return -1;
1954 }
1955
1956 /* Add safety timeout hook. */
1957 ao2_ref(agent, +1);
1960 ao2_ref(agent, -1);
1961 ast_bridge_features_cleanup(&caller_features);
1962 return -1;
1963 }
1964
1965 /* Setup the alert agent on caller joining the bridge hook. */
1966 ao2_ref(agent, +1);
1967 if (ast_bridge_join_hook(&caller_features, caller_joined_bridge, agent,
1968 __ao2_cleanup, 0)) {
1969 ao2_ref(agent, -1);
1970 ast_bridge_features_cleanup(&caller_features);
1971 return -1;
1972 }
1973
1974 caller_bridge = ast_bridge_basic_new();
1975 if (!caller_bridge) {
1976 ast_bridge_features_cleanup(&caller_features);
1977 return -1;
1978 }
1979
1980 agent_lock(agent);
1981 switch (agent->state) {
1984 agent_unlock(agent);
1985 ast_bridge_destroy(caller_bridge, 0);
1986 ast_bridge_features_cleanup(&caller_features);
1987 ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
1988 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "NOT_LOGGED_IN");
1989 return 0;
1991 ao2_ref(caller_bridge, +1);
1992 agent->caller_bridge = caller_bridge;
1993 agent->state = AGENT_STATE_CALL_PRESENT;
1994 agent->devstate = AST_DEVICE_INUSE;
1995 break;
1996 default:
1997 agent_unlock(agent);
1998 ast_bridge_destroy(caller_bridge, 0);
1999 ast_bridge_features_cleanup(&caller_features);
2000 ast_verb(3, "Agent '%s' is busy.\n", agent->username);
2001 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "BUSY");
2002 return 0;
2003 }
2004 agent_unlock(agent);
2005 agent_devstate_changed(agent->username);
2006
2007 /* Get COLP for agent. */
2009 ast_channel_lock(chan);
2011 ast_channel_unlock(chan);
2012
2013 logged = agent_bridge_channel_get_lock(agent);
2014 if (!logged) {
2016 caller_abort_agent(agent);
2017 ast_bridge_destroy(caller_bridge, 0);
2018 ast_bridge_features_cleanup(&caller_features);
2019 ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
2020 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "NOT_LOGGED_IN");
2021 return 0;
2022 }
2023
2024 send_colp_to_agent(logged, &connected);
2026 ao2_ref(logged, -1);
2028
2029 if (ast_bridge_join(caller_bridge, chan, NULL, &caller_features, NULL,
2031 caller_abort_agent(agent);
2032 ast_verb(3, "Agent '%s': Caller %s failed to join the bridge.\n",
2033 agent->username, ast_channel_name(chan));
2034 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ERROR");
2035 }
2036 ast_bridge_features_cleanup(&caller_features);
2037
2038 /* Determine if we need to continue in the dialplan after the bridge. */
2039 ast_channel_lock(chan);
2041 /*
2042 * The bridge was broken for a hangup that isn't real.
2043 * Don't run the h extension, because the channel isn't
2044 * really hung up. This should really only happen with
2045 * AST_SOFTHANGUP_ASYNCGOTO.
2046 */
2047 res = 0;
2048 } else {
2049 res = ast_check_hangup(chan)
2051 || ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENT_STATUS"));
2052 }
2053 ast_channel_unlock(chan);
2054
2055 return res ? -1 : 0;
2056}
2057
2058/*!
2059 * \internal
2060 * \brief Get agent config values from the login channel.
2061 * \since 12.0.0
2062 *
2063 * \param agent What to setup channel config values on.
2064 * \param chan Channel logging in as an agent.
2065 */
2066static void agent_login_channel_config(struct agent_pvt *agent, struct ast_channel *chan)
2067{
2068 struct ast_flags opts = { 0 };
2070 unsigned int override_ack_call = 0;
2071 unsigned int override_auto_logoff = 0;
2072 unsigned int override_wrapup_time = 0;
2073 const char *override_dtmf_accept = NULL;
2074 const char *var;
2075
2077
2078 /* Get config values from channel. */
2079 ast_channel_lock(chan);
2081
2082 var = pbx_builtin_getvar_helper(chan, "AGENTACKCALL");
2083 if (!ast_strlen_zero(var)) {
2084 override_ack_call = ast_true(var) ? 1 : 0;
2086 }
2087
2088 var = pbx_builtin_getvar_helper(chan, "AGENTACCEPTDTMF");
2089 if (!ast_strlen_zero(var)) {
2090 override_dtmf_accept = ast_strdupa(var);
2092 }
2093
2094 var = pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF");
2095 if (!ast_strlen_zero(var)) {
2096 if (sscanf(var, "%u", &override_auto_logoff) == 1) {
2098 }
2099 }
2100
2101 var = pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME");
2102 if (!ast_strlen_zero(var)) {
2103 if (sscanf(var, "%u", &override_wrapup_time) == 1) {
2105 }
2106 }
2107 ast_channel_unlock(chan);
2108
2109 /* Set config values on agent. */
2110 agent_lock(agent);
2112 agent->waiting_colp = connected;
2113
2114 ast_string_field_set(agent, override_dtmf_accept, override_dtmf_accept);
2115 ast_copy_flags(agent, &opts, AST_FLAGS_ALL);
2116 agent->override_auto_logoff = override_auto_logoff;
2117 agent->override_wrapup_time = override_wrapup_time;
2118 agent->override_ack_call = override_ack_call;
2119 agent_unlock(agent);
2120}
2121
2123 OPT_SILENT = (1 << 0),
2124};
2128
2129/*!
2130 * \brief Dialplan AgentLogin application to log in an agent.
2131 *
2132 * \param chan Channel attempting to login as an agent.
2133 * \param data Application parameters
2134 *
2135 * \retval 0 To continue in dialplan.
2136 * \retval -1 To hangup.
2137 */
2138static int agent_login_exec(struct ast_channel *chan, const char *data)
2139{
2140 char *parse;
2141 struct ast_flags opts;
2143 AST_APP_ARG(agent_id);
2145 AST_APP_ARG(other); /* Any remaining unused arguments */
2146 );
2147
2148 RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
2149
2151 return -1;
2152 }
2153
2154 if (ast_channel_state(chan) != AST_STATE_UP && ast_answer(chan)) {
2155 return -1;
2156 }
2157
2158 parse = ast_strdupa(data);
2160
2161 if (ast_strlen_zero(args.agent_id)) {
2162 ast_log(LOG_WARNING, "AgentLogin requires an AgentId\n");
2163 return -1;
2164 }
2165
2166 if (ast_app_parse_options(agent_login_opts, &opts, NULL, args.options)) {
2167 /* General invalid option syntax. */
2168 return -1;
2169 }
2170
2171 /* Find the agent. */
2172 agent = ao2_find(agents, args.agent_id, OBJ_KEY);
2173 if (!agent) {
2174 ast_verb(3, "Agent '%s' does not exist.\n", args.agent_id);
2175 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "INVALID");
2176 return 0;
2177 }
2178
2179 /* Has someone already logged in as this agent already? */
2180 agent_lock(agent);
2181 if (agent->logged) {
2182 agent_unlock(agent);
2183 ast_verb(3, "Agent '%s' already logged in.\n", agent->username);
2184 pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ALREADY_LOGGED_IN");
2185 return 0;
2186 }
2187 agent->logged = ast_channel_ref(chan);
2188 agent->last_disconnect = ast_tvnow();
2189 time(&agent->login_start);
2190 agent->deferred_logoff = 0;
2191 agent_unlock(agent);
2192
2193 agent_login_channel_config(agent, chan);
2194
2195 if (!ast_test_flag(&opts, OPT_SILENT)) {
2196 ast_stream_and_wait(chan, "agent-loginok", AST_DIGIT_NONE);
2197 }
2198
2199 ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", agent->username,
2202 ast_channel_lock(chan);
2203 send_agent_login(chan, agent->username);
2204 ast_channel_unlock(chan);
2205
2206 agent_run(agent, chan);
2207 return -1;
2208}
2209
2210static int agent_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
2211{
2212 char *parse;
2213 struct agent_pvt *agent;
2214 struct ast_channel *logged;
2216 AST_APP_ARG(agentid);
2218 );
2219
2220 buf[0] = '\0';
2221
2222 parse = ast_strdupa(data ?: "");
2223 AST_NONSTANDARD_APP_ARGS(args, parse, ':');
2224
2225 if (ast_strlen_zero(args.agentid)) {
2226 ast_log(LOG_WARNING, "The AGENT function requires an argument - agentid!\n");
2227 return -1;
2228 }
2229 if (!args.item) {
2230 args.item = "status";
2231 }
2232
2233 agent = ao2_find(agents, args.agentid, OBJ_KEY);
2234 if (!agent) {
2235 ast_log(LOG_WARNING, "Agent '%s' not found!\n", args.agentid);
2236 return -1;
2237 }
2238
2239 agent_lock(agent);
2240 if (!strcasecmp(args.item, "status")) {
2241 const char *status;
2242
2243 if (agent->logged) {
2244 status = "LOGGEDIN";
2245 } else {
2246 status = "LOGGEDOUT";
2247 }
2249 } else if (!strcasecmp(args.item, "name")) {
2250 ast_copy_string(buf, agent->cfg->full_name, len);
2251 } else if (!strcasecmp(args.item, "mohclass")) {
2252 ast_copy_string(buf, agent->cfg->moh, len);
2253 } else if (!strcasecmp(args.item, "channel")) {
2254 logged = agent_lock_logged(agent);
2255 if (logged) {
2256 char *pos;
2257
2259 ast_channel_unlock(logged);
2260 ast_channel_unref(logged);
2261
2262 pos = strrchr(buf, '-');
2263 if (pos) {
2264 *pos = '\0';
2265 }
2266 }
2267 } else if (!strcasecmp(args.item, "fullchannel")) {
2268 logged = agent_lock_logged(agent);
2269 if (logged) {
2271 ast_channel_unlock(logged);
2272 ast_channel_unref(logged);
2273 }
2274 }
2275 agent_unlock(agent);
2276 ao2_ref(agent, -1);
2277
2278 return 0;
2279}
2280
2282 .name = "AGENT",
2283 .read = agent_function_read,
2284};
2285
2287 /*! Nth match to return. */
2289 /*! Which match currently on. */
2291};
2292
2293static int complete_agent_search(void *obj, void *arg, void *data, int flags)
2294{
2295 struct agent_complete *search = data;
2296
2297 if (++search->which > search->state) {
2298 return CMP_MATCH;
2299 }
2300 return 0;
2301}
2302
2303static char *complete_agent(const char *word, int state)
2304{
2305 char *ret;
2306 struct agent_pvt *agent;
2307 struct agent_complete search = {
2308 .state = state,
2309 };
2310
2312 complete_agent_search, (char *) word, &search);
2313 if (!agent) {
2314 return NULL;
2315 }
2316 ret = ast_strdup(agent->username);
2317 ao2_ref(agent, -1);
2318 return ret;
2319}
2320
2321static int complete_agent_logoff_search(void *obj, void *arg, void *data, int flags)
2322{
2323 struct agent_pvt *agent = obj;
2324 struct agent_complete *search = data;
2325
2326 if (!agent->logged) {
2327 return 0;
2328 }
2329 if (++search->which > search->state) {
2330 return CMP_MATCH;
2331 }
2332 return 0;
2333}
2334
2335static char *complete_agent_logoff(const char *word, int state)
2336{
2337 char *ret;
2338 struct agent_pvt *agent;
2339 struct agent_complete search = {
2340 .state = state,
2341 };
2342
2344 complete_agent_logoff_search, (char *) word, &search);
2345 if (!agent) {
2346 return NULL;
2347 }
2348 ret = ast_strdup(agent->username);
2349 ao2_ref(agent, -1);
2350 return ret;
2351}
2352
2353static void agent_show_requested(struct ast_cli_args *a, int online_only)
2354{
2355#define FORMAT_HDR "%-8s %-20s %-11s %-30s %s\n"
2356#define FORMAT_ROW "%-8s %-20s %-11s %-30s %s\n"
2357
2358 struct ao2_iterator iter;
2359 struct agent_pvt *agent;
2360 struct ast_str *out = ast_str_alloca(512);
2361 unsigned int agents_total = 0;
2362 unsigned int agents_logged_in = 0;
2363 unsigned int agents_talking = 0;
2364
2365 ast_cli(a->fd, FORMAT_HDR, "Agent-ID", "Name", "State", "Channel", "Talking with");
2366 iter = ao2_iterator_init(agents, 0);
2367 for (; (agent = ao2_iterator_next(&iter)); ao2_ref(agent, -1)) {
2368 struct ast_channel *logged;
2369
2370 ++agents_total;
2371
2372 agent_lock(agent);
2373 logged = agent_lock_logged(agent);
2374 if (logged) {
2375 const char *talking_with;
2376
2377 ++agents_logged_in;
2378
2379 talking_with = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
2380 if (!ast_strlen_zero(talking_with)) {
2381 ++agents_talking;
2382 } else {
2383 talking_with = "";
2384 }
2385 ast_str_set(&out, 0, FORMAT_ROW, agent->username, agent->cfg->full_name,
2386 ast_devstate_str(agent->devstate), ast_channel_name(logged), talking_with);
2387 ast_channel_unlock(logged);
2388 ast_channel_unref(logged);
2389 } else {
2390 ast_str_set(&out, 0, FORMAT_ROW, agent->username, agent->cfg->full_name,
2391 ast_devstate_str(agent->devstate), "", "");
2392 }
2393 agent_unlock(agent);
2394
2395 if (!online_only || logged) {
2396 ast_cli(a->fd, "%s", ast_str_buffer(out));
2397 }
2398 }
2399 ao2_iterator_destroy(&iter);
2400
2401 ast_cli(a->fd, "\nDefined agents: %u, Logged in: %u, Talking: %u\n",
2402 agents_total, agents_logged_in, agents_talking);
2403
2404#undef FORMAT_HDR
2405#undef FORMAT_ROW
2406}
2407
2408static char *agent_handle_show_online(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2409{
2410 switch (cmd) {
2411 case CLI_INIT:
2412 e->command = "agent show online";
2413 e->usage =
2414 "Usage: agent show online\n"
2415 " Provides summary information for logged in agents.\n";
2416 return NULL;
2417 case CLI_GENERATE:
2418 return NULL;
2419 }
2420
2421 if (a->argc != 3) {
2422 return CLI_SHOWUSAGE;
2423 }
2424
2426
2427 return CLI_SUCCESS;
2428}
2429
2430static char *agent_handle_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2431{
2432 switch (cmd) {
2433 case CLI_INIT:
2434 e->command = "agent show all";
2435 e->usage =
2436 "Usage: agent show all\n"
2437 " Provides summary information for all agents.\n";
2438 return NULL;
2439 case CLI_GENERATE:
2440 return NULL;
2441 }
2442
2443 if (a->argc != 3) {
2444 return CLI_SHOWUSAGE;
2445 }
2446
2448
2449 return CLI_SUCCESS;
2450}
2451
2452static char *agent_handle_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2453{
2454 struct agent_pvt *agent;
2455 struct ast_channel *logged;
2456 struct ast_str *out = ast_str_alloca(4096);
2457
2458 switch (cmd) {
2459 case CLI_INIT:
2460 e->command = "agent show";
2461 e->usage =
2462 "Usage: agent show <agent-id>\n"
2463 " Show information about the <agent-id> agent\n";
2464 return NULL;
2465 case CLI_GENERATE:
2466 if (a->pos == 2) {
2467 return complete_agent(a->word, a->n);
2468 }
2469 return NULL;
2470 }
2471
2472 if (a->argc != 3) {
2473 return CLI_SHOWUSAGE;
2474 }
2475
2476 agent = ao2_find(agents, a->argv[2], OBJ_KEY);
2477 if (!agent) {
2478 ast_cli(a->fd, "Agent '%s' not found\n", a->argv[2]);
2479 return CLI_SUCCESS;
2480 }
2481
2482 agent_lock(agent);
2483 logged = agent_lock_logged(agent);
2484 ast_str_set(&out, 0, "Id: %s\n", agent->username);
2485 ast_str_append(&out, 0, "Name: %s\n", agent->cfg->full_name);
2486 ast_str_append(&out, 0, "Beep: %s\n", agent->cfg->beep_sound);
2487 ast_str_append(&out, 0, "MOH: %s\n", agent->cfg->moh);
2488 ast_str_append(&out, 0, "RecordCalls: %s\n", AST_CLI_YESNO(agent->cfg->record_agent_calls));
2489 ast_str_append(&out, 0, "State: %s\n", ast_devstate_str(agent->devstate));
2490 if (logged) {
2491 const char *talking_with;
2492
2493 ast_str_append(&out, 0, "LoggedInChannel: %s\n", ast_channel_name(logged));
2494 ast_str_append(&out, 0, "LoggedInTime: %ld\n", (long) agent->login_start);
2495 talking_with = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
2496 if (!ast_strlen_zero(talking_with)) {
2497 ast_str_append(&out, 0, "TalkingWith: %s\n", talking_with);
2498 ast_str_append(&out, 0, "CallStarted: %ld\n", (long) agent->call_start);
2499 }
2500 ast_channel_unlock(logged);
2501 ast_channel_unref(logged);
2502 }
2503 agent_unlock(agent);
2504 ao2_ref(agent, -1);
2505
2506 ast_cli(a->fd, "%s", ast_str_buffer(out));
2507
2508 return CLI_SUCCESS;
2509}
2510
2511static char *agent_handle_logoff_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2512{
2513 switch (cmd) {
2514 case CLI_INIT:
2515 e->command = "agent logoff";
2516 e->usage =
2517 "Usage: agent logoff <agent-id> [soft]\n"
2518 " Sets an agent as no longer logged in.\n"
2519 " If 'soft' is specified, do not hangup existing calls.\n";
2520 return NULL;
2521 case CLI_GENERATE:
2522 if (a->pos == 2) {
2523 return complete_agent_logoff(a->word, a->n);
2524 } else if (a->pos == 3 && a->n == 0
2525 && (ast_strlen_zero(a->word)
2526 || !strncasecmp("soft", a->word, strlen(a->word)))) {
2527 return ast_strdup("soft");
2528 }
2529 return NULL;
2530 }
2531
2532 if (a->argc < 3 || 4 < a->argc) {
2533 return CLI_SHOWUSAGE;
2534 }
2535 if (a->argc == 4 && strcasecmp(a->argv[3], "soft")) {
2536 return CLI_SHOWUSAGE;
2537 }
2538
2539 if (!agent_logoff_request(a->argv[2], a->argc == 4)) {
2540 ast_cli(a->fd, "Logging out %s\n", a->argv[2]);
2541 }
2542
2543 return CLI_SUCCESS;
2544}
2545
2546static struct ast_cli_entry cli_agents[] = {
2547 AST_CLI_DEFINE(agent_handle_show_online, "Show status of online agents"),
2548 AST_CLI_DEFINE(agent_handle_show_all, "Show status of all agents"),
2549 AST_CLI_DEFINE(agent_handle_show_specific, "Show information about an agent"),
2550 AST_CLI_DEFINE(agent_handle_logoff_cmd, "Sets an agent offline"),
2551};
2552
2553static int action_agents(struct mansession *s, const struct message *m)
2554{
2555 const char *id = astman_get_header(m, "ActionID");
2556 char id_text[AST_MAX_BUF];
2557 struct ao2_iterator iter;
2558 struct agent_pvt *agent;
2559 struct ast_str *out = ast_str_alloca(4096);
2560 int num_agents = 0;
2561
2562 if (!ast_strlen_zero(id)) {
2563 snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
2564 } else {
2565 id_text[0] = '\0';
2566 }
2567 astman_send_listack(s, m, "Agents will follow", "start");
2568
2569 iter = ao2_iterator_init(agents, 0);
2570 for (; (agent = ao2_iterator_next(&iter)); ao2_ref(agent, -1)) {
2571 struct ast_channel *logged;
2572
2573 agent_lock(agent);
2574 logged = agent_lock_logged(agent);
2575
2576 /*
2577 * Status Values:
2578 * AGENT_LOGGEDOFF - Agent isn't logged in
2579 * AGENT_IDLE - Agent is logged in, and waiting for call
2580 * AGENT_ONCALL - Agent is logged in, and on a call
2581 * AGENT_UNKNOWN - Don't know anything about agent. Shouldn't ever get this.
2582 */
2583 ast_str_set(&out, 0, "Agent: %s\r\n", agent->username);
2584 ast_str_append(&out, 0, "Name: %s\r\n", agent->cfg->full_name);
2585
2586 if (logged) {
2587 const char *talking_to_chan;
2588 struct ast_str *logged_headers;
2589 RAII_VAR(struct ast_channel_snapshot *, logged_snapshot, ast_channel_snapshot_create(logged), ao2_cleanup);
2590
2591 if (!logged_snapshot
2592 || !(logged_headers =
2593 ast_manager_build_channel_state_string(logged_snapshot))) {
2594 ast_channel_unlock(logged);
2595 ast_channel_unref(logged);
2596 agent_unlock(agent);
2597 continue;
2598 }
2599
2600 talking_to_chan = pbx_builtin_getvar_helper(logged, "BRIDGEPEER");
2601 if (!ast_strlen_zero(talking_to_chan)) {
2602 ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_ONCALL");
2603 ast_str_append(&out, 0, "TalkingToChan: %s\r\n", talking_to_chan);
2604 ast_str_append(&out, 0, "CallStarted: %ld\n", (long) agent->call_start);
2605 } else {
2606 ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_IDLE");
2607 }
2608 ast_str_append(&out, 0, "LoggedInTime: %ld\r\n", (long) agent->login_start);
2609 ast_str_append(&out, 0, "%s", ast_str_buffer(logged_headers));
2610 ast_channel_unlock(logged);
2611 ast_channel_unref(logged);
2612 ast_free(logged_headers);
2613 } else {
2614 ast_str_append(&out, 0, "Status: %s\r\n", "AGENT_LOGGEDOFF");
2615 }
2616
2617 agent_unlock(agent);
2618
2619 astman_append(s, "Event: Agents\r\n"
2620 "%s%s\r\n",
2621 ast_str_buffer(out), id_text);
2622 ++num_agents;
2623 }
2624 ao2_iterator_destroy(&iter);
2625
2626 astman_send_list_complete_start(s, m, "AgentsComplete", num_agents);
2628 return 0;
2629}
2630
2631static int action_agent_logoff(struct mansession *s, const struct message *m)
2632{
2633 const char *agent = astman_get_header(m, "Agent");
2634 const char *soft_s = astman_get_header(m, "Soft"); /* "true" is don't hangup */
2635
2636 if (ast_strlen_zero(agent)) {
2637 astman_send_error(s, m, "No agent specified");
2638 return 0;
2639 }
2640
2641 if (!agent_logoff_request(agent, ast_true(soft_s))) {
2642 astman_send_ack(s, m, "Agent logged out");
2643 } else {
2644 astman_send_error(s, m, "No such agent");
2645 }
2646
2647 return 0;
2648}
2649
2650static int unload_module(void)
2651{
2652 struct ast_bridge *holding;
2653
2654 /* Unregister dialplan applications */
2657
2658 /* Unregister dialplan functions */
2660
2661 /* Unregister manager command */
2662 ast_manager_unregister("Agents");
2663 ast_manager_unregister("AgentLogoff");
2664
2665 /* Unregister CLI commands */
2667
2668 ast_devstate_prov_del("Agent");
2669
2670 /* Destroy agent holding bridge. */
2671 holding = ao2_global_obj_replace(agent_holding, NULL);
2672 if (holding) {
2673 ast_bridge_destroy(holding, 0);
2674 }
2675
2678 agents = NULL;
2679 return 0;
2680}
2681
2682static int load_module(void)
2683{
2684 int res = 0;
2685
2688 if (!agents) {
2690 }
2691
2692 /* Init agent holding bridge v_table. */
2694
2695 /* Setup to provide Agent:agent-id device state. */
2697
2698 /* CLI Commands */
2700
2701 /* Manager commands */
2704
2705 /* Dialplan Functions */
2707
2708 /* Dialplan applications */
2711
2712 if (res) {
2713 ast_log(LOG_ERROR, "Unable to register application. Not loading module.\n");
2714 unload_module();
2716 }
2717
2718 if (load_config()) {
2719 ast_log(LOG_ERROR, "Unable to load config. Not loading module.\n");
2720 unload_module();
2722 }
2723
2725}
2726
2727static int reload(void)
2728{
2729 if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
2730 /* Just keep the config we already have in place. */
2731 return -1;
2732 }
2733 return 0;
2734}
2735
2736AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Call center agent pool applications",
2737 .support_level = AST_MODULE_SUPPORT_CORE,
2738 .load = load_module,
2739 .unload = unload_module,
2740 .reload = reload,
2741 .load_pri = AST_MODPRI_DEVSTATE_PROVIDER,
static struct ao2_container * agents
static char * agent_handle_logoff_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int action_agents(struct mansession *s, const struct message *m)
static void * agent_cfg_alloc(const char *name)
static int bridge_agent_hold_heartbeat(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
static char * complete_agent(const char *word, int state)
#define CALLER_SAFETY_TIMEOUT_TIME
static struct ast_custom_function agent_function
static void _agent_unlock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
static AO2_GLOBAL_OBJ_STATIC(cfg_handle)
static char * agent_handle_show_online(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int agent_pvt_sort_cmp(const void *obj_left, const void *obj_right, int flags)
static int send_colp_to_agent(struct ast_bridge_channel *bridge_channel, struct ast_party_connected_line *connected)
static int caller_joined_bridge(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
#define AST_MAX_BUF
static const char app_agent_login[]
static int agent_request_exec(struct ast_channel *chan, const char *data)
Dialplan AgentRequest application to locate an agent to talk with.
static enum ast_device_state agent_pvt_devstate_get(const char *agent_id)
static void agents_cfg_destructor(void *vdoomed)
#define FORMAT_ROW
static int agent_sweep(void *obj, void *arg, int flags)
static void agent_show_requested(struct ast_cli_args *a, int online_only)
static struct agent_pvt * agent_pvt_new(struct agent_cfg *cfg)
static void destroy_config(void)
static struct aco_type agent_type
static struct ast_bridge_methods bridge_agent_hold_v_table
static int bridge_agent_hold_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
static char * agent_handle_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int action_agent_logoff(struct mansession *s, const struct message *m)
CONFIG_INFO_STANDARD(cfg_info, cfg_handle, agents_cfg_alloc,.files=ACO_FILES(&agents_conf),.post_apply_config=agents_post_apply_config,)
static void agent_cfg_destructor(void *vdoomed)
static int bridge_agent_hold_deferred_create(void)
static void send_agent_login(struct ast_channel *chan, const char *agent)
static struct aco_type general_type
static struct ast_bridge_channel * agent_bridge_channel_get_lock(struct agent_pvt *agent)
static int send_alert_to_agent(struct ast_bridge_channel *bridge_channel, const char *agent_id)
static void agent_pvt_destructor(void *vdoomed)
static void send_agent_logoff(struct ast_channel *chan, const char *agent, long logintime)
static void bridge_init_agent_hold(void)
static int agent_cfg_sort_cmp(const void *obj_left, const void *obj_right, int flags)
static struct ast_cli_entry cli_agents[]
static const char app_agent_request[]
static void agent_run(struct agent_pvt *agent, struct ast_channel *logged)
static void agent_after_bridge_cb(struct ast_channel *chan, void *data)
static int agent_logoff_request(const char *agent_id, int soft)
static void agent_alert(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
static void agent_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
static int complete_agent_logoff_search(void *obj, void *arg, void *data, int flags)
static char * agent_handle_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static int caller_safety_timeout(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
static void caller_abort_agent(struct agent_pvt *agent)
#define agent_lock(agent)
Lock the agent.
static void bridge_agent_hold_dissolving(struct ast_bridge *self)
The bridge is being dissolved.
static struct ast_bridge * bridge_agent_hold_new(void)
#define LOGIN_WAIT_TIMEOUT_TIME
agent_override_flags
@ AGENT_FLAG_ACK_CALL
@ AGENT_FLAG_AUTO_LOGOFF
@ AGENT_FLAG_WRAPUP_TIME
@ AGENT_FLAG_DTMF_ACCEPT
AGENT_LOGIN_OPT_FLAGS
@ OPT_SILENT
static void _agent_lock(struct agent_pvt *agent, const char *file, const char *function, int line, const char *var)
static const struct ast_app_option agent_login_opts[128]
static int load_module(void)
static int complete_agent_search(void *obj, void *arg, void *data, int flags)
static void agents_post_apply_config(void)
static int agent_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int unload_module(void)
static int reload(void)
static int agent_pvt_cmp(void *obj, void *arg, int flags)
static struct ast_channel * agent_lock_logged(struct agent_pvt *agent)
static int agent_mark(void *obj, void *arg, int flags)
static void agent_connect_caller(struct ast_bridge_channel *bridge_channel, struct agent_pvt *agent)
static void agents_mark(void)
static struct aco_type * agent_types[]
static ast_mutex_t agent_holding_lock
static void agents_sweep(void)
static char * complete_agent_logoff(const char *word, int state)
static struct aco_file agents_conf
agent_state
@ AGENT_STATE_LOGGED_OUT
@ AGENT_STATE_CALL_PRESENT
@ AGENT_STATE_PROBATION_WAIT
@ AGENT_STATE_CALL_WAIT_ACK
@ AGENT_STATE_READY_FOR_CALL
@ AGENT_STATE_ON_CALL
@ AGENT_STATE_LOGGING_OUT
@ AGENT_STATE_CALL_WRAPUP
static void bridge_agent_hold_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
static int agent_login_exec(struct ast_channel *chan, const char *data)
Dialplan AgentLogin application to log in an agent.
static void agent_login_channel_config(struct agent_pvt *agent, struct ast_channel *chan)
static void * agents_cfg_alloc(void)
static void agent_logout(struct agent_pvt *agent)
#define agent_unlock(agent)
Unlock the agent.
static void agent_devstate_changed(const char *agent_id)
static int load_config(void)
static int bridge_agent_hold_ack(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
static void clear_agent_status(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
static void * agent_cfg_find(struct ao2_container *agents, const char *username)
#define FORMAT_HDR
char digit
jack_status_t status
Definition: app_jack.c:149
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_t_ref(o, delta, tag)
Definition: astobj2.h:460
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ CMP_MATCH
Definition: astobj2.h:1027
#define OBJ_KEY
Definition: astobj2.h:1151
#define OBJ_POINTER
Definition: astobj2.h:1150
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_global_obj_replace_unref(holder, obj)
Replace an ao2 object in the global holder, throwing away any old object.
Definition: astobj2.h:901
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
@ AO2_LOCK_REQ_MUTEX
Definition: astobj2.h:702
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_callback_data(container, flags, cb_fn, arg, data)
Definition: astobj2.h:1723
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a red-black tree container.
Definition: astobj2.h:1349
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_global_obj_replace(holder, obj)
Replace an ao2 object in the global holder.
Definition: astobj2.h:878
#define OBJ_PARTIAL_KEY
Definition: astobj2.h:1152
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
void __ao2_cleanup(void *obj)
Definition: astobj2.c:677
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Definition: astobj2.h:859
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
@ OBJ_MULTIPLE
Definition: astobj2.h:1049
@ OBJ_UNLINK
Definition: astobj2.h:1039
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
int __ao2_unlock(void *a, const char *file, const char *func, int line, const char *var)
Unlock an object.
Definition: astobj2.c:288
int __ao2_lock(void *a, enum ao2_lock_req lock_how, const char *file, const char *func, int line, const char *var)
Lock an object.
Definition: astobj2.c:222
@ AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT
Reject objects with duplicate keys in container.
Definition: astobj2.h:1188
@ AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE
Replace objects with duplicate keys in container.
Definition: astobj2.h:1211
Bridging API.
#define ast_bridge_unlock(bridge)
Unlock the bridge.
Definition: bridge.h:485
int ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, struct ast_bridge_tech_optimizations *tech_args, enum ast_bridge_join_flags flags)
Join a channel to a bridge (blocking)
Definition: bridge.c:1690
int ast_bridge_destroy(struct ast_bridge *bridge, int cause)
Destroy a bridge.
Definition: bridge.c:1009
struct ast_bridge_methods ast_bridge_base_v_table
Bridge base class virtual method table.
Definition: bridge.c:988
@ AST_BRIDGE_CAPABILITY_HOLDING
Definition: bridge.h:90
@ AST_BRIDGE_JOIN_PASS_REFERENCE
Definition: bridge.h:539
int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
Move a channel from one bridge to another.
Definition: bridge.c:2529
After Bridge Execution API.
ast_bridge_after_cb_reason
Definition: bridge_after.h:37
int ast_bridge_set_after_callback(struct ast_channel *chan, ast_bridge_after_cb callback, ast_bridge_after_cb_failed failed, void *data)
Setup an after bridge callback for when the channel leaves the bridging system.
Definition: bridge_after.c:251
const char * ast_bridge_after_cb_reason_string(enum ast_bridge_after_cb_reason reason)
Get a string representation of an after bridge callback reason.
Definition: bridge_after.c:288
Basic bridge subclass API.
struct ast_bridge * ast_bridge_basic_new(void)
Create a new basic class bridge.
@ AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA
#define ast_bridge_channel_unlock(bridge_channel)
Unlock the bridge_channel.
void ast_bridge_channel_feature_digit(struct ast_bridge_channel *bridge_channel, int digit)
Add a DTMF digit to the collected digits to match against DTMF features.
void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
Lock the bridge associated with the bridge channel.
int ast_bridge_channel_write_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
Write a control frame into the bridge with data.
#define ast_bridge_channel_lock(bridge_channel)
Lock the bridge_channel.
void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
Set bridge channel state to leave bridge (if not leaving already).
@ BRIDGE_CHANNEL_STATE_END
int ast_bridge_channel_queue_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame onto the bridge channel with data.
void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
Set bridge channel state to leave bridge (if not leaving already).
int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_custom_callback_option flags, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
Queue a bridge action custom callback frame onto the bridge channel.
int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_custom_callback_option flags, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
Write a bridge action custom callback frame into the bridge.
int ast_bridge_features_init(struct ast_bridge_features *features)
Initialize bridge features structure.
Definition: bridge.c:3689
int ast_bridge_interval_hook(struct ast_bridge_features *features, enum ast_bridge_hook_timer_option flags, unsigned int interval, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach an interval hook to a bridge features structure.
Definition: bridge.c:3388
@ AST_BRIDGE_HOOK_REMOVE_ON_PULL
int ast_bridge_dtmf_hook(struct ast_bridge_features *features, const char *dtmf, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a DTMF hook to a bridge features structure.
Definition: bridge.c:3251
@ AST_BRIDGE_BUILTIN_AUTOMIXMON
int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
Invoke a built in feature hook now.
Definition: bridge.c:3159
int ast_bridge_join_hook(struct ast_bridge_features *features, ast_bridge_hook_callback callback, void *hook_pvt, ast_bridge_hook_pvt_destructor destructor, enum ast_bridge_hook_remove_flags remove_flags)
Attach a bridge channel join hook to a bridge features structure.
Definition: bridge.c:3344
@ AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
@ AST_BRIDGE_FLAG_TRANSFER_PROHIBITED
@ AST_BRIDGE_FLAG_MERGE_INHIBIT_TO
@ AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
void ast_bridge_features_cleanup(struct ast_bridge_features *features)
Clean up the contents of a bridge features structure.
Definition: bridge.c:3722
@ AUTO_MONITOR_START
Private Bridging API.
struct ast_bridge * bridge_register(struct ast_bridge *bridge)
Register the new bridge with the system.
Definition: bridge.c:713
struct ast_bridge * bridge_base_init(struct ast_bridge *self, uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
Initialize the base class of the bridge.
Definition: bridge.c:783
struct ast_bridge * bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
Definition: bridge.c:747
int ast_channel_set_bridge_role_option(struct ast_channel *channel, const char *role_name, const char *option, const char *value)
Set a role option on a channel.
Definition: bridge_roles.c:375
void ast_channel_remove_bridge_role(struct ast_channel *chan, const char *role_name)
Removes a bridge role from a channel.
Definition: bridge_roles.c:332
int ast_channel_add_bridge_role(struct ast_channel *chan, const char *role_name)
Adds a bridge role to a channel.
Definition: bridge_roles.c:313
int ast_bridge_channel_establish_roles(struct ast_bridge_channel *bridge_channel)
Clone the roles from a bridge_channel's attached ast_channel onto the bridge_channel's role list.
Definition: bridge_roles.c:443
void ast_bridge_channel_clear_roles(struct ast_bridge_channel *bridge_channel)
Clear all roles from a bridge_channel's role list.
Definition: bridge_roles.c:491
Internal Asterisk hangup causes.
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:106
enum cc_state state
Definition: ccss.c:399
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
Destroy the connected line information contents.
Definition: channel.c:2099
#define ast_channel_lock(chan)
Definition: channel.h:2970
void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
Copy the source connected line information to the destination connected line.
Definition: channel.c:2058
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2995
struct ast_party_connected_line * ast_channel_connected(struct ast_channel *chan)
int ast_check_hangup_locked(struct ast_channel *chan)
Definition: channel.c:459
void ast_channel_update_connected_line(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Indicate that the connected line information has changed.
Definition: channel.c:9119
int ast_check_hangup(struct ast_channel *chan)
Check to see if a channel is needing hang up.
Definition: channel.c:445
int ast_softhangup(struct ast_channel *chan, int cause)
Softly hangup up a channel.
Definition: channel.c:2500
int ast_connected_line_build_data(unsigned char *data, size_t datalen, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Build the connected line information data frame.
Definition: channel.c:8723
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:3006
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
@ AST_FLAG_ZOMBIE
Definition: channel.h:1007
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2049
struct ast_bridge_channel * ast_channel_get_bridge_channel(struct ast_channel *chan)
Get a reference to the channel's bridge pointer.
Definition: channel.c:10608
void ast_connected_line_copy_from_caller(struct ast_party_connected_line *dest, const struct ast_party_caller *src)
Copy the caller information to the connected line information.
Definition: channel.c:8319
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:2834
int ast_indicate(struct ast_channel *chan, int condition)
Indicates condition of channel.
Definition: channel.c:4294
#define ast_channel_unlock(chan)
Definition: channel.h:2971
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
@ AST_SOFTHANGUP_ASYNCGOTO
Definition: channel.h:1146
@ AST_SOFTHANGUP_EXPLICIT
Definition: channel.h:1168
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_UP
Definition: channelstate.h:42
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define AST_CLI_YESNO(x)
Return Yes or No depending on the argument.
Definition: cli.h:71
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static void update(int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)
Definition: codec_g726.c:367
short word
Configuration option-handling.
@ ACO_EXACT
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
@ ACO_PROCESS_ERROR
Their was an error and no changes were applied.
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
#define ACO_FILES(...)
@ OPT_UINT_T
Type for default option handler for unsigned integers.
@ OPT_BOOL_T
Type for default option handler for bools (ast_true/ast_false)
@ OPT_STRINGFIELD_T
Type for default option handler for stringfields.
@ ACO_ITEM
@ ACO_GLOBAL
@ ACO_WHITELIST_EXACT
@ ACO_BLACKLIST_EXACT
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
int ast_devstate_prov_del(const char *label)
Remove device state provider.
Definition: devicestate.c:421
@ AST_DEVSTATE_CACHABLE
Definition: devicestate.h:70
const char * ast_devstate_str(enum ast_device_state devstate) attribute_pure
Convert device state to text string that is easier to parse.
Definition: devicestate.c:258
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:513
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
Add device state provider.
Definition: devicestate.c:394
ast_device_state
Device States.
Definition: devicestate.h:52
@ AST_DEVICE_INUSE
Definition: devicestate.h:55
@ AST_DEVICE_UNKNOWN
Definition: devicestate.h:53
@ AST_DEVICE_INVALID
Definition: devicestate.h:57
@ AST_DEVICE_NOT_INUSE
Definition: devicestate.h:54
@ AST_DEVICE_UNAVAILABLE
Definition: devicestate.h:58
char connected
Definition: eagi_proxy.c:82
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:222
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1886
#define AST_DIGIT_NONE
Definition: file.h:47
#define AST_DIGIT_ANY
Definition: file.h:48
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
static const char name[]
Definition: format_mp3.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition: manager.c:2028
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:1986
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition: manager.c:2064
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:2018
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition: manager.c:1647
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition: manager.c:2072
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:1907
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7697
struct stasis_message_type * ast_channel_agent_logoff_type(void)
Message type for agent logoff on a channel.
struct ast_channel_snapshot * ast_channel_snapshot_create(struct ast_channel *chan)
Generate a snapshot of the channel state. This is an ao2 object, so ao2_cleanup() to deallocate.
struct stasis_message_type * ast_channel_agent_login_type(void)
Message type for agent login on a channel.
void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
Publish a channel blob message.
#define bc
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define END_OPTIONS
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define BEGIN_OPTIONS
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3066
#define AST_FEATURE_MAX_LEN
@ AST_CONTROL_ANSWER
@ AST_CONTROL_RINGING
@ AST_CONTROL_CONNECTED_LINE
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_NOTICE
#define LOG_WARNING
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
AST_JSON_INT_T ast_json_int_t
Primarily used to cast when packing to an "I" type.
Definition: json.h:87
#define ast_mutex_unlock(a)
Definition: lock.h:194
#define ast_mutex_lock(a)
Definition: lock.h:193
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:524
struct ast_str * ast_manager_build_channel_state_string(const struct ast_channel_snapshot *snapshot)
Generate the AMI message body from a channel snapshot.
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:192
#define EVENT_FLAG_AGENT
Definition: manager.h:80
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_DEVSTATE_PROVIDER
Definition: module.h:343
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static corosync_cfg_handle_t cfg_handle
Definition: res_corosync.c:284
#define NULL
Definition: resample.c:96
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
Definition: stringfields.h:341
#define AST_STRING_FIELD(name)
Declare a string field.
Definition: stringfields.h:303
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_alloca(init_len)
Definition: strings.h:848
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
The representation of a single configuration file to be processed.
const char * filename
Type information about a category-level configurable object.
enum aco_type_t type
const ast_string_field username
const ast_string_field moh
const ast_string_field full_name
const ast_string_field dtmf_accept
unsigned int auto_logoff
Number of seconds for agent to ack a call before being logged off.
unsigned int wrapup_time
Time after a call in ms before the agent can get a new call.
int ack_call
TRUE if agent needs to ack a call to accept it.
const ast_string_field beep_sound
int record_agent_calls
Structure representing an agent.
unsigned int override_auto_logoff
enum ast_device_state devstate
unsigned int dead
TRUE if the agent is no longer configured and is being destroyed.
struct ast_party_connected_line waiting_colp
const ast_string_field override_dtmf_accept
enum agent_state state
unsigned int the_mark
time_t probation_start
struct timeval last_disconnect
struct timeval ack_time
const ast_string_field username
struct ast_bridge * caller_bridge
struct ast_channel * logged
unsigned int override_ack_call
time_t call_start
unsigned int override_wrapup_time
unsigned int flags
unsigned int deferred_logoff
time_t login_start
struct agent_cfg * cfg
struct ao2_container * agents
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Structure that contains information regarding a channel in a bridge.
struct ast_bridge * bridge
Bridge this channel is participating in.
struct ast_bridge_features * features
struct ast_channel * chan
Structure that contains features information.
Bridge virtual methods table definition.
Definition: bridge.h:261
ast_bridge_dissolving_fn dissolving
Definition: bridge.h:267
ast_bridge_push_channel_fn push
Definition: bridge.h:269
const char * name
Definition: bridge.h:263
ast_bridge_pull_channel_fn pull
Definition: bridge.h:271
Structure that contains information about a bridge.
Definition: bridge.h:353
Structure representing a snapshot of channel state.
Main Channel structure associated with a channel.
struct ast_bridge_channel * bridge_channel
const char * data
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
Structure used to handle boolean flags.
Definition: utils.h:199
Abstract JSON element (object, array, string, int, ...).
Connected Line/Party information.
Definition: channel.h:458
Indicate what information in ast_party_connected_line should be set.
Definition: channel.h:491
Support for dynamic strings.
Definition: strings.h:623
In case you didn't read that giant block of text above the mansession_session struct,...
Definition: manager.c:327
static struct aco_type item
Definition: test_config.c:1463
const char * args
static struct test_options options
static struct test_val a
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
FILE * out
Definition: utils/frame.c:33
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
#define ast_assert(a)
Definition: utils.h:739
#define ast_clear_flag(p, flag)
Definition: utils.h:77
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define ARRAY_LEN(a)
Definition: utils.h:666
#define ast_copy_flags(dest, src, flagz)
Definition: utils.h:84
#define AST_FLAGS_ALL
Definition: utils.h:196