Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2012, Digium, Inc.
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*
* \author Mark Spencer <markster@digium.com>
*
* This file is the implementation of Agents modules.
* It is a dynamic module that is loaded by Asterisk.
* \par See also
* \arg \ref Config_agent
*
* \ingroup channel_drivers
/*** MODULEINFO
<depend>chan_local</depend>
<support_level>core</support_level>
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
Kevin P. Fleming
committed
#include <sys/socket.h>
Kevin P. Fleming
committed
#include <fcntl.h>
#include <netdb.h>
Kevin P. Fleming
committed
#include <netinet/in.h>
Kevin P. Fleming
committed
#include <arpa/inet.h>
#include <sys/signal.h>
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/features.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/astdb.h"
Kevin P. Fleming
committed
#include "asterisk/devicestate.h"
#include "asterisk/stringfields.h"
#include "asterisk/data.h"
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*** DOCUMENTATION
<application name="AgentLogin" language="en_US">
<synopsis>
Call agent login.
</synopsis>
<syntax>
<parameter name="AgentNo" />
<parameter name="options">
<optionlist>
<option name="s">
<para>silent login - do not announce the login ok segment after
agent logged on/off</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>Asks the agent to login to the system. Always returns <literal>-1</literal>.
While logged in, the agent can receive calls and will hear a <literal>beep</literal>
when a new call comes in. The agent can dump the call by pressing the star key.</para>
</description>
<see-also>
<ref type="application">Queue</ref>
<ref type="application">AddQueueMember</ref>
<ref type="application">RemoveQueueMember</ref>
<ref type="application">PauseQueueMember</ref>
<ref type="application">UnpauseQueueMember</ref>
<ref type="function">AGENT</ref>
<ref type="filename">agents.conf</ref>
<ref type="filename">queues.conf</ref>
</see-also>
</application>
<application name="AgentMonitorOutgoing" language="en_US">
<synopsis>
Record agent's outgoing call.
</synopsis>
<syntax>
<parameter name="options">
<optionlist>
<option name="d">
<para>make the app return <literal>-1</literal> if there is an error condition.</para>
</option>
<option name="c">
<para>change the CDR so that the source of the call is
<literal>Agent/agent_id</literal></para>
</option>
<option name="n">
<para>don't generate the warnings when there is no callerid or the
agentid is not known. It's handy if you want to have one context
for agent and non-agent calls.</para>
</option>
</optionlist>
</parameter>
</syntax>
<description>
<para>Tries to figure out the id of the agent who is placing outgoing call based on
comparison of the callerid of the current interface and the global variable
placed by the AgentCallbackLogin application. That's why it should be used only
with the AgentCallbackLogin app. Uses the monitoring functions in chan_agent
instead of Monitor application. That has to be configured in the
<filename>agents.conf</filename> file.</para>
<para>Normally the app returns <literal>0</literal> unless the options are passed.</para>
</description>
<see-also>
<ref type="filename">agents.conf</ref>
</see-also>
</application>
<function name="AGENT" language="en_US">
<synopsis>
Gets information about an Agent
</synopsis>
<syntax argsep=":">
<parameter name="agentid" required="true" />
<parameter name="item">
<para>The valid items to retrieve are:</para>
<enumlist>
<enum name="status">
<para>(default) The status of the agent (LOGGEDIN | LOGGEDOUT)</para>
</enum>
<enum name="password">
<para>The password of the agent</para>
</enum>
<enum name="name">
<para>The name of the agent</para>
</enum>
<enum name="mohclass">
<para>MusicOnHold class</para>
</enum>
<enum name="channel">
<para>The name of the active channel for the Agent (AgentLogin)</para>
</enum>
<enum name="fullchannel">
<para>The untruncated name of the active channel for the Agent (AgentLogin)</para>
</enum>
</enumlist>
</parameter>
</syntax>
</function>
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<manager name="Agents" language="en_US">
<synopsis>
Lists agents and their status.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
</syntax>
<description>
<para>Will list info about all possible agents.</para>
</description>
</manager>
<manager name="AgentLogoff" language="en_US">
<synopsis>
Sets an agent as no longer logged in.
</synopsis>
<syntax>
<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
<parameter name="Agent" required="true">
<para>Agent ID of the agent to log off.</para>
</parameter>
<parameter name="Soft">
<para>Set to <literal>true</literal> to not hangup existing calls.</para>
</parameter>
</syntax>
<description>
<para>Sets an agent as no longer logged in.</para>
</description>
</manager>
static const char tdesc[] = "Call Agent Proxy Channel";
static const char config[] = "agents.conf";
static const char app[] = "AgentLogin";
static const char app3[] = "AgentMonitorOutgoing";
#define AST_MAX_AGENT 80 /*!< Agent ID or Password max length */
Martin Pycko
committed
#define AST_MAX_BUF 256
#define AST_MAX_FILENAME_LEN 256
static const char pa_family[] = "Agents"; /*!< Persistent Agents astdb family */
#define PA_MAX_LEN 2048 /*!< The maximum length of each persistent member agent database entry */
Mark Michelson
committed
#define DEFAULT_ACCEPTDTMF '#'
#define DEFAULT_ENDDTMF '*'
static int endcall;
BJ Weschke
committed
static int autologoffunavail = 0;
Mark Michelson
committed
static char acceptdtmf = DEFAULT_ACCEPTDTMF;
static char enddtmf = DEFAULT_ENDDTMF;
static int maxlogintries = 3;
static char agentgoodbye[AST_MAX_FILENAME_LEN] = "vm-goodbye";
static int recordagentcalls = 0;
static char recordformat[AST_MAX_BUF] = "";
static char recordformatext[AST_MAX_BUF] = "";
static char urlprefix[AST_MAX_BUF] = "";
static char savecallsin[AST_MAX_BUF] = "";
static char beep[AST_MAX_BUF] = "beep";
Martin Pycko
committed
Martin Pycko
committed
#define GETAGENTBYCALLERID "AGENTBYCALLERID"
enum {
AGENT_FLAG_ACKCALL = (1 << 0),
AGENT_FLAG_AUTOLOGOFF = (1 << 1),
AGENT_FLAG_WRAPUPTIME = (1 << 2),
AGENT_FLAG_ACCEPTDTMF = (1 << 3),
AGENT_FLAG_ENDDTMF = (1 << 4),
};
/*! \brief Structure representing an agent. */
ast_mutex_t lock; /*!< Channel private lock */
int dead; /*!< Poised for destruction? */
int pending; /*!< Not a real agent -- just pending a match */
int abouttograb; /*!< About to grab */
int autologoff; /*!< Auto timeout time */
int ackcall; /*!< ackcall */
int deferlogoff; /*!< Defer logoff to hangup */
Mark Michelson
committed
char acceptdtmf;
char enddtmf;
time_t loginstart; /*!< When agent first logged in (0 when logged off) */
time_t start; /*!< When call started */
struct timeval lastdisc; /*!< When last disconnected */
int wrapuptime; /*!< Wrapup time in ms */
ast_group_t group; /*!< Group memberships */
int acknowledged; /*!< Acknowledged */
char moh[80]; /*!< Which music on hold */
char agent[AST_MAX_AGENT]; /*!< Agent ID */
char password[AST_MAX_AGENT]; /*!< Password for Agent login */
int app_lock_flag;
ast_cond_t app_complete_cond;
ast_cond_t login_wait_cond;
int app_sleep_cond; /*!< Non-zero if the login app should sleep. */
struct ast_channel *owner; /*!< Agent */
struct ast_channel *chan; /*!< Channel we use */
unsigned int flags; /*!< Flags show if settings were applied with channel vars */
AST_LIST_ENTRY(agent_pvt) list;/*!< Next Agent in the linked list. */
#define DATA_EXPORT_AGENT(MEMBER) \
MEMBER(agent_pvt, autologoff, AST_DATA_INTEGER) \
MEMBER(agent_pvt, ackcall, AST_DATA_BOOLEAN) \
MEMBER(agent_pvt, deferlogoff, AST_DATA_BOOLEAN) \
MEMBER(agent_pvt, wrapuptime, AST_DATA_MILLISECONDS) \
MEMBER(agent_pvt, acknowledged, AST_DATA_BOOLEAN) \
MEMBER(agent_pvt, name, AST_DATA_STRING) \
MEMBER(agent_pvt, password, AST_DATA_PASSWORD) \
MEMBER(agent_pvt, acceptdtmf, AST_DATA_CHARACTER)
AST_DATA_STRUCTURE(agent_pvt, DATA_EXPORT_AGENT);
static AST_LIST_HEAD_STATIC(agents, agent_pvt); /*!< Holds the list of agents (loaded form agents.conf). */
#define CHECK_FORMATS(ast, p) do { \
if (p->chan) {\
if (!(ast_format_cap_identical(ast_channel_nativeformats(ast), ast_channel_nativeformats(p->chan)))) { \
char tmp1[256], tmp2[256]; \
ast_debug(1, "Native formats changing from '%s' to '%s'\n", ast_getformatname_multiple(tmp1, sizeof(tmp1), ast_channel_nativeformats(ast)), ast_getformatname_multiple(tmp2, sizeof(tmp2), ast_channel_nativeformats(p->chan))); \
/* Native formats changed, reset things */ \
ast_format_cap_copy(ast_channel_nativeformats(ast), ast_channel_nativeformats(p->chan)); \
ast_debug(1, "Resetting read to '%s' and write to '%s'\n", ast_getformatname(ast_channel_readformat(ast)), ast_getformatname(ast_channel_writeformat(ast)));\
ast_set_read_format(ast, ast_channel_readformat(ast)); \
ast_set_write_format(ast, ast_channel_writeformat(ast)); \
if ((ast_format_cmp(ast_channel_readformat(p->chan), ast_channel_rawreadformat(ast)) != AST_FORMAT_CMP_EQUAL) && !ast_channel_generator(p->chan)) \
ast_set_read_format(p->chan, ast_channel_rawreadformat(ast)); \
if ((ast_format_cmp(ast_channel_writeformat(p->chan), ast_channel_rawwriteformat(ast)) != AST_FORMAT_CMP_EQUAL) && !ast_channel_generator(p->chan)) \
ast_set_write_format(p->chan, ast_channel_rawwriteformat(ast)); \
/*! \brief Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
properly for a timingfd XXX This might need more work if agents were logged in as agents or other
totally impractical combinations XXX */
#define CLEANUP(ast, p) do { \
int x; \
if (p->chan) { \
for (x = 0; x < AST_MAX_FDS; x++) { \
if (x != AST_TIMING_FD) { \
ast_channel_set_fd(ast, x, ast_channel_fd(p->chan, x)); \
} \
ast_channel_set_fd(ast, AST_AGENT_FD, ast_channel_fd(p->chan, AST_TIMING_FD)); \
/*--- Forward declarations */
static struct ast_channel *agent_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
static int agent_devicestate(const char *data);
static int agent_digit_begin(struct ast_channel *ast, char digit);
static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int agent_call(struct ast_channel *ast, const char *dest, int timeout);
static int agent_hangup(struct ast_channel *ast);
static int agent_answer(struct ast_channel *ast);
static struct ast_frame *agent_read(struct ast_channel *ast);
static int agent_write(struct ast_channel *ast, struct ast_frame *f);
Mark Spencer
committed
static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
static int agent_sendtext(struct ast_channel *ast, const char *text);
Kevin P. Fleming
committed
static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state);
static struct ast_channel* agent_get_base_channel(struct ast_channel *chan);
static int agent_logoff(const char *agent, int soft);
/*! \brief Channel interface description for PBX integration */
static struct ast_channel_tech agent_tech = {
.description = tdesc,
.requester = agent_request,
.devicestate = agent_devicestate,
.send_digit_begin = agent_digit_begin,
.send_digit_end = agent_digit_end,
.call = agent_call,
.hangup = agent_hangup,
.answer = agent_answer,
.read = agent_read,
.write = agent_write,
.send_html = agent_sendhtml,
.send_text = agent_sendtext,
.exception = agent_read,
.indicate = agent_indicate,
.fixup = agent_fixup,
.bridged_channel = agent_bridgedchannel,
.get_base_channel = agent_get_base_channel,
};
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*!
* \brief Locks the owning channel for a LOCKED pvt while obeying locking order. The pvt
* must enter this function locked and will be returned locked, but this function will
* unlock the pvt for a short time, so it can't be used while expecting the pvt to remain
* static. If function returns a non NULL channel, it will need to be unlocked and
* unrefed once it is no longer needed.
*
* \param pvt Pointer to the LOCKED agent_pvt for which the owner is needed
* \ret locked channel which owns the pvt at the time of completion. NULL if not available.
*/
static struct ast_channel *agent_lock_owner(struct agent_pvt *pvt)
{
struct ast_channel *owner;
for (;;) {
if (!pvt->owner) { /* No owner. Nothing to do. */
return NULL;
}
/* If we don't ref the owner, it could be killed when we unlock the pvt. */
owner = ast_channel_ref(pvt->owner);
/* Locking order requires us to lock channel, then pvt. */
ast_mutex_unlock(&pvt->lock);
ast_channel_lock(owner);
ast_mutex_lock(&pvt->lock);
/* Check if owner changed during pvt unlock period */
if (owner != pvt->owner) { /* Channel changed. Unref and do another pass. */
ast_channel_unlock(owner);
owner = ast_channel_unref(owner);
} else { /* Channel stayed the same. Return it. */
return owner;
}
}
}
/*!
* \internal
* \brief Destroy an agent pvt struct.
*
* \param doomed Agent pvt to destroy.
*
* \return Nothing
*/
static void agent_pvt_destroy(struct agent_pvt *doomed)
{
ast_mutex_destroy(&doomed->lock);
ast_cond_destroy(&doomed->app_complete_cond);
ast_cond_destroy(&doomed->login_wait_cond);
ast_free(doomed);
}
* Adds an agent to the global list of agents.
*
* \param agent A string with the username, password and real name of an agent. As defined in agents.conf. Example: "13,169,John Smith"
* \param pending If it is pending or not.
* @return The just created agent.
* \sa agent_pvt, agents.
static struct agent_pvt *add_agent(const char *agent, int pending)
char *parse;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(agt);
AST_APP_ARG(password);
AST_APP_ARG(name);
);
char *password = NULL;
char *name = NULL;
char *agt = NULL;
struct agent_pvt *p;
Russell Bryant
committed
parse = ast_strdupa(agent);
/* Extract username (agt), password and name from agent (args). */
Mark Michelson
committed
AST_STANDARD_APP_ARGS(args, parse);
ast_log(LOG_WARNING, "A blank agent line!\n");
return NULL;
}
if(ast_strlen_zero(args.agt) ) {
ast_log(LOG_WARNING, "An agent line with no agentid!\n");
return NULL;
} else
agt = args.agt;
if(!ast_strlen_zero(args.password)) {
password = args.password;
while (*password && *password < 33) password++;
}
if(!ast_strlen_zero(args.name)) {
name = args.name;
while (*name && *name < 33) name++;
if (!pending) {
/* Are we searching for the agent here ? To see if it exists already ? */
AST_LIST_TRAVERSE(&agents, p, list) {
if (!strcmp(p->agent, agt)) {
break;
}
}
} else {
p = NULL;
if (!(p = ast_calloc(1, sizeof(*p))))
ast_copy_string(p->agent, agt, sizeof(p->agent));
ast_mutex_init(&p->lock);
ast_cond_init(&p->app_complete_cond, NULL);
ast_cond_init(&p->login_wait_cond, NULL);
p->app_sleep_cond = 1;
p->group = group;
p->pending = pending;
AST_LIST_INSERT_TAIL(&agents, p, list);
ast_copy_string(p->password, password ? password : "", sizeof(p->password));
ast_copy_string(p->name, name ? name : "", sizeof(p->name));
ast_copy_string(p->moh, moh, sizeof(p->moh));
if (!ast_test_flag(p, AGENT_FLAG_ACKCALL)) {
p->ackcall = ackcall;
}
if (!ast_test_flag(p, AGENT_FLAG_AUTOLOGOFF)) {
p->autologoff = autologoff;
}
if (!ast_test_flag(p, AGENT_FLAG_ACCEPTDTMF)) {
p->acceptdtmf = acceptdtmf;
}
if (!ast_test_flag(p, AGENT_FLAG_ENDDTMF)) {
p->enddtmf = enddtmf;
}
Matthew Fredrickson
committed
/* If someone reduces the wrapuptime and reloads, we want it
* to change the wrapuptime immediately on all calls */
if (!ast_test_flag(p, AGENT_FLAG_WRAPUPTIME) && p->wrapuptime > wrapuptime) {
Kevin P. Fleming
committed
struct timeval now = ast_tvnow();
/* XXX check what is this exactly */
Matthew Fredrickson
committed
/* We won't be pedantic and check the tv_usec val */
if (p->lastdisc.tv_sec > (now.tv_sec + wrapuptime/1000)) {
p->lastdisc.tv_sec = now.tv_sec + wrapuptime/1000;
p->lastdisc.tv_usec = now.tv_usec;
}
}
Matthew Fredrickson
committed
Mark Spencer
committed
if (pending)
p->dead = 1;
else
p->dead = 0;
return p;
* Deletes an agent after doing some clean up.
* Further documentation: How safe is this function ? What state should the agent be to be cleaned.
*
* \warning XXX This function seems to be very unsafe.
* Potential for double free and use after free among other
* problems.
*
* \param p Agent to be deleted.
* \returns Always 0.
struct ast_channel *chan;
ast_mutex_lock(&p->lock);
chan = p->owner;
p->owner = NULL;
/* Release ownership of the agent to other threads (presumably running the login app). */
p->app_lock_flag = 0;
ast_cond_signal(&p->app_complete_cond);
if (chan) {
ast_channel_tech_pvt_set(chan, NULL);
chan = ast_channel_release(chan);
}
Mark Spencer
committed
if (p->dead) {
ast_mutex_unlock(&p->lock);
} else {
ast_mutex_unlock(&p->lock);
}
static int agent_answer(struct ast_channel *ast)
{
ast_log(LOG_WARNING, "Huh? Agent is being asked to answer?\n");
return -1;
}
Martin Pycko
committed
static int __agent_start_monitoring(struct ast_channel *ast, struct agent_pvt *p, int needlock)
Martin Pycko
committed
{
char tmp[AST_MAX_BUF],tmp2[AST_MAX_BUF], *pointer;
char filename[AST_MAX_BUF];
int res = -1;
if (!p)
return -1;
if (!ast_channel_monitor(ast)) {
snprintf(filename, sizeof(filename), "agent-%s-%s",p->agent, ast_channel_uniqueid(ast));
Martin Pycko
committed
/* substitute . for - */
if ((pointer = strchr(filename, '.')))
Martin Pycko
committed
*pointer = '-';
Joshua Colp
committed
snprintf(tmp, sizeof(tmp), "%s%s", savecallsin, filename);
Jason Parker
committed
ast_monitor_start(ast, recordformat, tmp, needlock, X_REC_IN | X_REC_OUT);
Martin Pycko
committed
ast_monitor_setjoinfiles(ast, 1);
Joshua Colp
committed
snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix, filename, recordformatext);
Martin Pycko
committed
#if 0
ast_verbose("name is %s, link is %s\n",tmp, tmp2);
#endif
if (!ast_channel_cdr(ast))
ast_channel_cdr_set(ast, ast_cdr_alloc());
Martin Pycko
committed
ast_cdr_setuserfield(ast, tmp2);
res = 0;
} else
ast_log(LOG_ERROR, "Recording already started on that call.\n");
return res;
}
Martin Pycko
committed
static int agent_start_monitoring(struct ast_channel *ast, int needlock)
{
return __agent_start_monitoring(ast, ast_channel_tech_pvt(ast), needlock);
Martin Pycko
committed
}
static struct ast_frame *agent_read(struct ast_channel *ast)
struct agent_pvt *p = ast_channel_tech_pvt(ast);
static struct ast_frame answer_frame = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
struct ast_channel *owner;
owner = agent_lock_owner(p);
if (!p->start) {
p->start = cur_time;
}
ast_copy_flags(ast_channel_flags(p->chan), ast_channel_flags(ast), AST_FLAG_EXCEPTION);
ast_channel_fdno_set(p->chan, (ast_channel_fdno(ast) == AST_AGENT_FD) ? AST_TIMING_FD : ast_channel_fdno(ast));
ast_channel_fdno_set(ast, -1);
f = &ast_null_frame;
if (f) {
/* if acknowledgement is not required, and the channel is up, we may have missed
an AST_CONTROL_ANSWER (if there was one), so mark the call acknowledged anyway */
if (!p->ackcall && !p->acknowledged && p->chan && (ast_channel_state(p->chan) == AST_STATE_UP)) {
p->acknowledged = 1;
}
if (!p->acknowledged) {
int howlong = cur_time - p->start;
if (p->autologoff && (howlong >= p->autologoff)) {
ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
if (owner || p->chan) {
if (owner) {
ast_softhangup(owner, AST_SOFTHANGUP_EXPLICIT);
ast_channel_unlock(owner);
owner = ast_channel_unref(owner);
}
while (p->chan && ast_channel_trylock(p->chan)) {
DEADLOCK_AVOIDANCE(&p->lock);
}
if (p->chan) {
ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
ast_channel_unlock(p->chan);
}
}
}
}
switch (f->frametype) {
case AST_FRAME_CONTROL:
if (f->subclass.integer == AST_CONTROL_ANSWER) {
ast_verb(3, "%s answered, waiting for '%c' to acknowledge\n", ast_channel_name(p->chan), p->acceptdtmf);
/* Don't pass answer along */
ast_frfree(f);
f = &ast_null_frame;
} else {
p->acknowledged = 1;
/* Use the builtin answer frame for the
ast_frfree(f);
f = &answer_frame;
}
}
break;
case AST_FRAME_DTMF_BEGIN:
/*ignore DTMF begin's as it can cause issues with queue announce files*/
if((!p->acknowledged && f->subclass.integer == p->acceptdtmf) || (f->subclass.integer == p->enddtmf && endcall)){
ast_frfree(f);
f = &ast_null_frame;
}
break;
if (!p->acknowledged && (f->subclass.integer == p->acceptdtmf)) {
if (p->chan) {
ast_verb(3, "%s acknowledged\n", ast_channel_name(p->chan));
}
p->acknowledged = 1;
ast_frfree(f);
f = &answer_frame;
} else if (f->subclass.integer == p->enddtmf && endcall) {
/* terminates call */
ast_frfree(f);
f = NULL;
}
break;
case AST_FRAME_VOICE:
case AST_FRAME_VIDEO:
/* don't pass voice or video until the call is acknowledged */
if (!p->acknowledged) {
ast_frfree(f);
f = &ast_null_frame;
}
default:
/* pass everything else on through */
break;
if (owner) {
ast_channel_unlock(owner);
owner = ast_channel_unref(owner);
}
if (p->chan && !ast_channel_internal_bridged_channel(p->chan)) {
if (strcasecmp(ast_channel_tech(p->chan)->type, "Local")) {
ast_channel_internal_bridged_channel_set(p->chan, ast);
ast_debug(1, "Bridge on '%s' being set to '%s' (3)\n", ast_channel_name(p->chan), ast_channel_name(ast_channel_internal_bridged_channel(p->chan)));
}
}
ast_mutex_unlock(&p->lock);
if (recordagentcalls && f == &answer_frame)
Martin Pycko
committed
agent_start_monitoring(ast,0);
Mark Spencer
committed
static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
struct agent_pvt *p = ast_channel_tech_pvt(ast);
int res = -1;
ast_mutex_lock(&p->lock);
if (p->chan)
res = ast_channel_sendhtml(p->chan, subclass, data, datalen);
ast_mutex_unlock(&p->lock);
return res;
}
static int agent_sendtext(struct ast_channel *ast, const char *text)
{
struct agent_pvt *p = ast_channel_tech_pvt(ast);
int res = -1;
ast_mutex_lock(&p->lock);
if (p->chan)
res = ast_sendtext(p->chan, text);
ast_mutex_unlock(&p->lock);
return res;
}
static int agent_write(struct ast_channel *ast, struct ast_frame *f)
{
struct agent_pvt *p = ast_channel_tech_pvt(ast);
ast_mutex_lock(&p->lock);
(ast_format_cmp(&f->subclass.format, ast_channel_writeformat(p->chan)) != AST_FORMAT_CMP_NOT_EQUAL)) {
ast_debug(1, "Dropping one incompatible %s frame on '%s' to '%s'\n",
f->frametype == AST_FRAME_VOICE ? "audio" : "video",
ast_channel_name(ast), ast_channel_name(p->chan));
ast_mutex_unlock(&p->lock);
static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
struct agent_pvt *p = ast_channel_tech_pvt(newchan);
ast_mutex_lock(&p->lock);
if (p->owner != oldchan) {
ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
ast_mutex_unlock(&p->lock);
ast_mutex_unlock(&p->lock);
Kevin P. Fleming
committed
static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
struct agent_pvt *p = ast_channel_tech_pvt(ast);
ast_mutex_lock(&p->lock);
if (p->chan && !ast_check_hangup(p->chan)) {
ast_channel_unlock(ast);
ast_channel_lock(p->chan);
res = ast_channel_tech(p->chan)->indicate
? ast_channel_tech(p->chan)->indicate(p->chan, condition, data, datalen)
: -1;
ast_mutex_unlock(&p->lock);
ast_channel_lock(ast);
} else {
ast_mutex_unlock(&p->lock);
Mark Spencer
committed
res = 0;
static int agent_digit_begin(struct ast_channel *ast, char digit)
struct agent_pvt *p = ast_channel_tech_pvt(ast);
ast_mutex_lock(&p->lock);
if (p->chan) {
ast_senddigit_begin(p->chan, digit);
}
ast_mutex_unlock(&p->lock);
}
static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
{
struct agent_pvt *p = ast_channel_tech_pvt(ast);
ast_mutex_lock(&p->lock);
if (p->chan) {
ast_senddigit_end(p->chan, digit, duration);
}
ast_mutex_unlock(&p->lock);
static int agent_call(struct ast_channel *ast, const char *dest, int timeout)
struct agent_pvt *p = ast_channel_tech_pvt(ast);
int res;
ast_mutex_lock(&p->lock);
p->acknowledged = 0;
if (p->pending) {
ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
ast_mutex_unlock(&p->lock);
ast_setstate(ast, AST_STATE_DIALING);
return 0;
}
ast_assert(p->chan != NULL);
ast_verb(3, "agent_call, call to agent '%s' call on '%s'\n", p->agent, ast_channel_name(p->chan));
ast_debug(3, "Playing beep, lang '%s'\n", ast_channel_language(p->chan));
ast_mutex_unlock(&p->lock);
res = ast_streamfile(p->chan, beep, ast_channel_language(p->chan));
ast_debug(3, "Played beep, result '%d'\n", res);
res = ast_waitstream(p->chan, "");
ast_debug(3, "Waited for stream, result '%d'\n", res);
ast_mutex_lock(&p->lock);
struct ast_format tmpfmt;
res = ast_set_read_format_from_cap(p->chan, ast_channel_nativeformats(p->chan));
ast_debug(3, "Set read format, result '%d'\n", res);
ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
struct ast_format tmpfmt;
res = ast_set_write_format_from_cap(p->chan, ast_channel_nativeformats(p->chan));
ast_debug(3, "Set write format, result '%d'\n", res);
ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
/* Call is immediately up, or might need ack */
agent_start_monitoring(ast, 0);
p->acknowledged = 1;
}
CLEANUP(ast, p);
ast_mutex_unlock(&p->lock);
if (newstate)
ast_setstate(ast, newstate);
return res ? -1 : 0;
/*! \brief return the channel or base channel if one exists. This function assumes the channel it is called on is already locked */
struct ast_channel* agent_get_base_channel(struct ast_channel *chan)
{
struct ast_channel *base = chan;
/* chan is locked by the calling function */
if (!chan || !ast_channel_tech_pvt(chan)) {
ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) with a tech_pvt (0x%ld) to get a base channel.\n", (long)chan, (chan)?(long)ast_channel_tech_pvt(chan):(long)NULL);
p = ast_channel_tech_pvt(chan);
if (p->chan)
base = p->chan;
static int agent_hangup(struct ast_channel *ast)
{
struct agent_pvt *p = ast_channel_tech_pvt(ast);
struct ast_channel *indicate_chan = NULL;
char *tmp_moh; /* moh buffer for indicating after unlocking p */
if (p->pending) {
AST_LIST_LOCK(&agents);
AST_LIST_REMOVE(&agents, p, list);
AST_LIST_UNLOCK(&agents);
}
ast_mutex_lock(&p->lock);
ast_channel_tech_pvt_set(ast, NULL);
p->acknowledged = 0;
/* if they really are hung up then set start to 0 so the test
* later if we're called on an already downed channel
* doesn't cause an agent to be logged out like when
* agent_request() is followed immediately by agent_hangup()
* as in apps/app_chanisavail.c:chanavail_exec()
*/
ast_debug(1, "Hangup called for state %s\n", ast_state2str(ast_channel_state(ast)));
ast_channel_internal_bridged_channel_set(p->chan, NULL);
/* If they're dead, go ahead and hang up on the agent now */
indicate_chan = ast_channel_ref(p->chan);
tmp_moh = ast_strdupa(p->moh);
Mark Spencer
committed
}
ast_mutex_unlock(&p->lock);
if (indicate_chan) {
ast_indicate_data(indicate_chan, AST_CONTROL_HOLD,
S_OR(tmp_moh, NULL),
!ast_strlen_zero(tmp_moh) ? strlen(tmp_moh) + 1 : 0);
indicate_chan = ast_channel_unref(indicate_chan);
}
ast_mutex_lock(&p->lock);
if (p->abouttograb) {
/* Let the "about to grab" thread know this isn't valid anymore, and let it
kill it later */
p->abouttograb = 0;
} else if (p->dead) {
ast_mutex_unlock(&p->lock);
return 0;
} else {
/* Store last disconnect time */
p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
Mark Spencer
committed
}
/* Release ownership of the agent to other threads (presumably running the login app). */
p->app_sleep_cond = 1;
p->app_lock_flag = 0;
ast_cond_signal(&p->app_complete_cond);
ast_mutex_unlock(&p->lock);
static int agent_cont_sleep(void *data)
p = (struct agent_pvt *) data;
ast_mutex_lock(&p->lock);
if (res && p->lastdisc.tv_sec) {
if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
res = 0;
}
ast_mutex_unlock(&p->lock);
if (!res) {
ast_debug(5, "agent_cont_sleep() returning %d\n", res);
}