Skip to content
Snippets Groups Projects
chan_agent.c 77.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2012, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * 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.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * 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.
    
    Mark Spencer's avatar
    Mark Spencer committed
     */
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \brief Implementation of Agents (proxy channel)
    
     * \author Mark Spencer <markster@digium.com>
     *
    
     * This file is the implementation of Agents modules.
    
    Russell Bryant's avatar
    Russell Bryant committed
     * 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>
    
            <depend>res_monitor</depend>
    
    	<support_level>core</support_level>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #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"
    
    #include "asterisk/monitor.h"
    
    #include "asterisk/stringfields.h"
    
    #include "asterisk/event.h"
    
    /*** 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>
    
    		<description></description>
    
    	<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";
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static char moh[80] = "default";
    
    
    #define AST_MAX_AGENT	80                          /*!< Agent ID or Password max length */
    
    #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 */
    
    
    #define DEFAULT_ACCEPTDTMF '#'
    #define DEFAULT_ENDDTMF '*'
    
    
    static ast_group_t group;
    
    static int autologoff;
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int wrapuptime;
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int ackcall;
    
    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 int updatecdr = 0;
    
    static char beep[AST_MAX_BUF] = "beep";
    
    #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.  */
    
    struct agent_pvt {
    
    	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 */
    
    	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 */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char name[AST_MAX_AGENT];
    
    	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)); \
    
    	} \
    } while(0)
    
    
    /*! \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 */
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    #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)); \
    
    Mark Spencer's avatar
    Mark Spencer committed
    	} \
    } while(0)
    
    
    /*--- 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);
    
    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);
    
    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);
    
    Jason Parker's avatar
    Jason Parker committed
    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,
    
    	.write_video = agent_write,
    
    	.send_text = agent_sendtext,
    
    	.exception = agent_read,
    	.indicate = agent_indicate,
    	.fixup = agent_fixup,
    	.bridged_channel = agent_bridgedchannel,
    
    	.get_base_channel = agent_get_base_channel,
    
    /*!
     * \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.
    
    static struct agent_pvt *add_agent(const char *agent, int pending)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	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;
    
    
    	/* Extract username (agt), password and name from agent (args). */
    
    
    	if(args.argc == 0) {
    
    		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++;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    
    	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;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (!p) {
    
    		// Build the agent.
    
    		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_lock_flag = 0;
    
    		p->app_sleep_cond = 1;
    		p->group = group;
    		p->pending = pending;
    
    		AST_LIST_INSERT_TAIL(&agents, p, list);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	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;
    	}
    
    
    	/* 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) {
    
    		struct timeval now = ast_tvnow();
    		/* XXX check what is this exactly */
    
    
    		/* 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;
    		}
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->wrapuptime = wrapuptime;
    
     * 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.
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int agent_cleanup(struct agent_pvt *p)
    {
    
    	ast_mutex_lock(&p->lock);
    	chan = p->owner;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->owner = NULL;
    	/* 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_channel_tech_pvt_set(chan, NULL);
    
    		ast_mutex_unlock(&p->lock);
    
    		agent_pvt_destroy(p);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int agent_answer(struct ast_channel *ast)
    {
    	ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
    	return -1;
    }
    
    
    static int __agent_start_monitoring(struct ast_channel *ast, struct agent_pvt *p, int needlock)
    
    {
    	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));
    
    		/* substitute . for - */
    		if ((pointer = strchr(filename, '.')))
    
    		snprintf(tmp, sizeof(tmp), "%s%s", savecallsin, filename);
    
    		ast_monitor_start(ast, recordformat, tmp, needlock, X_REC_IN | X_REC_OUT);
    
    		snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix, filename, recordformatext);
    
    #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());
    
    		ast_cdr_setuserfield(ast, tmp2);
    		res = 0;
    	} else
    		ast_log(LOG_ERROR, "Recording already started on that call.\n");
    	return res;
    }
    
    
    static int agent_start_monitoring(struct ast_channel *ast, int needlock)
    {
    
    	return __agent_start_monitoring(ast, ast_channel_tech_pvt(ast), needlock);
    
    static struct ast_frame *agent_read(struct ast_channel *ast)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct ast_frame *f = NULL;
    
    	static struct ast_frame answer_frame = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
    
    	int cur_time = time(NULL);
    
    	struct ast_channel *owner;
    
    
    	ast_mutex_lock(&p->lock);
    
    	owner = agent_lock_owner(p);
    
    
    	CHECK_FORMATS(ast, p);
    
    	if (!p->start) {
    		p->start = cur_time;
    	}
    
    	if (p->chan) {
    
    		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));
    
    Mark Spencer's avatar
    Mark Spencer committed
    		f = ast_read(p->chan);
    
    	} else
    
    		/* 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) {
    
    				if (p->ackcall) {
    
    					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 
    
    					   recording start check below. */
    
    					ast_frfree(f);
    					f = &answer_frame;
    				}
    			}
    			break;
    
    			/*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;
    
    		case AST_FRAME_DTMF_END:
    
    			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);
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	CLEANUP(ast,p);
    
    	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)));
    
    	if (recordagentcalls && f == &answer_frame)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return f;
    }
    
    
    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;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int agent_write(struct ast_channel *ast, struct ast_frame *f)
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = -1;
    
    	CHECK_FORMATS(ast, p);
    
    	if (!p->chan) 
    		res = 0;
    	else {
    
    		if ((f->frametype != AST_FRAME_VOICE) ||
    
    		    (f->frametype != AST_FRAME_VIDEO) ||
    
    		    (ast_format_cmp(&f->subclass.format, ast_channel_writeformat(p->chan)) != AST_FORMAT_CMP_NOT_EQUAL)) {
    
    			res = ast_write(p->chan, f);
    		} else {
    
    			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));
    
    			res = 0;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	CLEANUP(ast, p);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    }
    
    
    static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(newchan);
    
    	ast_mutex_lock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->owner != oldchan) {
    		ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		return -1;
    	}
    	p->owner = newchan;
    
    	ast_mutex_unlock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int res = -1;
    
    	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_channel_unlock(p->chan);
    
    		ast_mutex_unlock(&p->lock);
    		ast_channel_lock(ast);
    	} else {
    		ast_mutex_unlock(&p->lock);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;
    }
    
    
    static int agent_digit_begin(struct ast_channel *ast, char digit)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    	if (p->chan) {
    		ast_senddigit_begin(p->chan, digit);
    	}
    
    static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    	if (p->chan) {
    		ast_senddigit_end(p->chan, digit, duration);
    	}
    
    static int agent_call(struct ast_channel *ast, const char *dest, int timeout)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct agent_pvt *p = ast_channel_tech_pvt(ast);
    
    	int newstate=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_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);
    
    	if (!res) {
    
    		ast_debug(3, "Waited for stream, result '%d'\n", res);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!res) {
    
    		res = ast_set_read_format_from_cap(p->chan, ast_channel_nativeformats(p->chan));
    
    		ast_debug(3, "Set read format, result '%d'\n", res);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (res)
    
    			ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!res) {
    
    		res = ast_set_write_format_from_cap(p->chan, ast_channel_nativeformats(p->chan));
    
    		ast_debug(3, "Set write format, result '%d'\n", res);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		if (res)
    
    			ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    		/* Call is immediately up, or might need ack */
    
    			newstate = AST_STATE_RINGING;
    
    			newstate = AST_STATE_UP;
    
    			if (recordagentcalls)
    
    				agent_start_monitoring(ast, 0);
    
    	if (newstate)
    		ast_setstate(ast, newstate);
    
    /*! \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 agent_pvt *p;
    
    	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;
    
    Mark Spencer's avatar
    Mark Spencer committed
    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);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	p->owner = NULL;
    
    	ast_channel_tech_pvt_set(ast, NULL);
    
    
    	/* 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)));
    
    	p->start = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (p->chan) {
    
    		ast_channel_internal_bridged_channel_set(p->chan, NULL);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		/* If they're dead, go ahead and hang up on the agent now */
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
    
    		} else if (p->loginstart) {
    
    			indicate_chan = ast_channel_ref(p->chan);
    			tmp_moh = ast_strdupa(p->moh);
    
    	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);
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	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) {
    
    		agent_pvt_destroy(p);
    
    		/* Store last disconnect time */
    		p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
    
    
    	/* 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);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }
    
    
    static int agent_cont_sleep(void *data)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	struct agent_pvt *p;
    	int res;
    
    
    	p = (struct agent_pvt *) data;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	res = p->app_sleep_cond;
    
    	if (res && p->lastdisc.tv_sec) {
    		if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
    			res = 0;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (!res) {
    		ast_debug(5, "agent_cont_sleep() returning %d\n", res);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return res;