⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chan_agent.c

📁 Asterisk中信道部分的源码 。。。。
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * * 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. *//*! \file *  * \brief Implementation of Agents (proxy channel) * * \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> ***/#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 141366 $")#include <stdio.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/socket.h>#include <stdlib.h>#include <fcntl.h>#include <netdb.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/signal.h>#include "asterisk/lock.h"#include "asterisk/channel.h"#include "asterisk/config.h"#include "asterisk/logger.h"#include "asterisk/module.h"#include "asterisk/pbx.h"#include "asterisk/options.h"#include "asterisk/lock.h"#include "asterisk/sched.h"#include "asterisk/io.h"#include "asterisk/rtp.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/devicestate.h"#include "asterisk/monitor.h"#include "asterisk/stringfields.h"static const char tdesc[] = "Call Agent Proxy Channel";static const char config[] = "agents.conf";static const char app[] = "AgentLogin";static const char app2[] = "AgentCallbackLogin";static const char app3[] = "AgentMonitorOutgoing";static const char synopsis[] = "Call agent login";static const char synopsis2[] = "Call agent callback login";static const char synopsis3[] = "Record agent's outgoing call";static const char descrip[] ="  AgentLogin([AgentNo][|options]):\n""Asks the agent to login to the system.  Always returns -1.  While\n""logged in, the agent can receive calls and will hear a 'beep'\n""when a new call comes in. The agent can dump the call by pressing\n""the star key.\n""The option string may contain zero or more of the following characters:\n""      's' -- silent login - do not announce the login ok segment after agent logged in/off\n";static const char descrip2[] ="  AgentCallbackLogin([AgentNo][|[options][|[exten]@context]]):\n""Asks the agent to login to the system with callback.\n""The agent's callback extension is called (optionally with the specified\n""context).\n""The option string may contain zero or more of the following characters:\n""      's' -- silent login - do not announce the login ok segment agent logged in/off\n";static const char descrip3[] ="  AgentMonitorOutgoing([options]):\n""Tries to figure out the id of the agent who is placing outgoing call based on\n""comparison of the callerid of the current interface and the global variable \n""placed by the AgentCallbackLogin application. That's why it should be used only\n""with the AgentCallbackLogin app. Uses the monitoring functions in chan_agent \n""instead of Monitor application. That have to be configured in the agents.conf file.\n""\nReturn value:\n""Normally the app returns 0 unless the options are passed. Also if the callerid or\n""the agentid are not specified it'll look for n+101 priority.\n""\nOptions:\n""	'd' - make the app return -1 if there is an error condition and there is\n""	      no extension n+101\n""	'c' - change the CDR so that the source of the call is 'Agent/agent_id'\n""	'n' - don't generate the warnings when there is no callerid or the\n""	      agentid is not known.\n""             It's handy if you want to have one context for agent and non-agent calls.\n";static const char mandescr_agents[] ="Description: Will list info about all possible agents.\n""Variables: NONE\n";static const char mandescr_agent_logoff[] ="Description: Sets an agent as no longer logged in.\n""Variables: (Names marked with * are required)\n""	*Agent: Agent ID of the agent to log off\n""	Soft: Set to 'true' to not hangup existing calls\n";static const char mandescr_agent_callback_login[] ="Description: Sets an agent as logged in with callback.\n""Variables: (Names marked with * are required)\n""	*Agent: Agent ID of the agent to login\n""	*Exten: Extension to use for callback\n""	Context: Context to use for callback\n""	AckCall: Set to 'true' to require an acknowledgement by '#' when agent is called back\n""	WrapupTime: the minimum amount of time after disconnecting before the caller can receive a new call\n";static char moh[80] = "default";#define AST_MAX_AGENT	80                          /*!< Agent ID or Password max length */#define AST_MAX_BUF	256#define AST_MAX_FILENAME_LEN	256static const char pa_family[] = "Agents";          /*!< Persistent Agents astdb family */#define PA_MAX_LEN 2048                             /*!< The maximum length of each persistent member agent database entry */static int persistent_agents = 0;                   /*!< queues.conf [general] option */static void dump_agents(void);static ast_group_t group;static int autologoff;static int wrapuptime;static int ackcall;static int endcall;static int multiplelogin = 1;static int autologoffunavail = 0;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"/*! \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 */	char name[AST_MAX_AGENT];	int inherited_devicestate;     /*!< Does the underlying channel have a devicestate to pass? */	ast_mutex_t app_lock;          /**< Synchronization between owning applications */	volatile pthread_t owning_app; /**< Owning application thread id */	volatile int app_sleep_cond;   /**< Sleep condition for the login app */	struct ast_channel *owner;     /**< Agent */	char loginchan[80];            /**< channel they logged in from */	char logincallerid[80];        /**< Caller ID they had when they logged in */	struct ast_channel *chan;      /**< Channel we use */	AST_LIST_ENTRY(agent_pvt) list;	/**< Next Agent in the linked list. */};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->nativeformats != p->chan->nativeformats) { \			ast_log(LOG_DEBUG, "Native formats changing from %d to %d\n", ast->nativeformats, p->chan->nativeformats); \			/* Native formats changed, reset things */ \			ast->nativeformats = p->chan->nativeformats; \			ast_log(LOG_DEBUG, "Resetting read to %d and write to %d\n", ast->readformat, ast->writeformat);\			ast_set_read_format(ast, ast->readformat); \			ast_set_write_format(ast, ast->writeformat); \		} \		if (p->chan->readformat != ast->rawreadformat && !p->chan->generator)  \			ast_set_read_format(p->chan, ast->rawreadformat); \		if (p->chan->writeformat != ast->rawwriteformat && !p->chan->generator) \			ast_set_write_format(p->chan, ast->rawwriteformat); \	} \} 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 */#define CLEANUP(ast, p) do { \	int x; \	if (p->chan) { \		for (x=0;x<AST_MAX_FDS;x++) {\			if (x != AST_TIMING_FD) \				ast->fds[x] = p->chan->fds[x]; \		} \		ast->fds[AST_AGENT_FD] = p->chan->fds[AST_TIMING_FD]; \	} \} while(0)/*--- Forward declarations */static struct ast_channel *agent_request(const char *type, int format, void *data, int *cause);static int agent_devicestate(void *data);static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long logintime, const char *uniqueid, char *logcommand);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, 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);static void set_agentbycallerid(const char *callerid, const char *agent);static struct ast_channel* agent_get_base_channel(struct ast_channel *chan);static int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base);/*! \brief Channel interface description for PBX integration */static const struct ast_channel_tech agent_tech = {	.type = "Agent",	.description = tdesc,	.capabilities = -1,	.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_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,	.set_base_channel = agent_set_base_channel,};static int agent_devicestate_cb(const char *dev, int state, void *data){	int res, i;	struct agent_pvt *p;	char basename[AST_CHANNEL_NAME], *tmp;	/* Skip Agent status */	if (!strncasecmp(dev, "Agent/", 6)) {		return 0;	}	/* Try to be safe, but don't deadlock */	for (i = 0; i < 10; i++) {		if ((res = AST_LIST_TRYLOCK(&agents)) == 0) {			break;		}	}	if (res) {		return -1;	}	AST_LIST_TRAVERSE(&agents, p, list) {		ast_mutex_lock(&p->lock);		if (p->chan) {			ast_copy_string(basename, p->chan->name, sizeof(basename));			if ((tmp = strrchr(basename, '-'))) {				*tmp = '\0';			}			if (strcasecmp(p->chan->name, dev) == 0 || strcasecmp(basename, dev) == 0) {				p->inherited_devicestate = state;				ast_device_state_changed("Agent/%s", p->agent);			}		}		ast_mutex_unlock(&p->lock);	}	AST_LIST_UNLOCK(&agents);	return 0;}/*! * 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(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;	parse = ast_strdupa(agent);	/* Extract username (agt), password and name from agent (args). */	AST_NONSTANDARD_APP_ARGS(args, parse, ',');	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++;	}		/* Are we searching for the agent here ? To see if it exists already ? */	AST_LIST_TRAVERSE(&agents, p, list) {		if (!pending && !strcmp(p->agent, agt))			break;	}	if (!p) {		// Build the agent.		if (!(p = ast_calloc(1, sizeof(*p))))			return NULL;		ast_copy_string(p->agent, agt, sizeof(p->agent));		ast_mutex_init(&p->lock);		ast_mutex_init(&p->app_lock);		p->owning_app = (pthread_t) -1;		p->app_sleep_cond = 1;		p->group = group;		p->pending = pending;		p->inherited_devicestate = -1;		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));	p->ackcall = ackcall;	p->autologoff = autologoff;	/* If someone reduces the wrapuptime and reloads, we want it	 * to change the wrapuptime immediately on all calls */	if (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;		}	}	p->wrapuptime = wrapuptime;	if (pending)		p->dead = 1;	else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -