Code cleanup while trying to understand this channel...

- please check


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@36181 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Olle Johansson 20 years ago
parent f12f5b7c6c
commit f48a573439

@ -171,7 +171,7 @@ static char beep[AST_MAX_BUF] = "beep";
#define GETAGENTBYCALLERID "AGENTBYCALLERID" #define GETAGENTBYCALLERID "AGENTBYCALLERID"
/*! \brief * Structure representing an agent. */ /*! \brief Structure representing an agent. */
struct agent_pvt { struct agent_pvt {
ast_mutex_t lock; /*!< Channel private lock */ ast_mutex_t lock; /*!< Channel private lock */
int dead; /*!< Poised for destruction? */ int dead; /*!< Poised for destruction? */
@ -368,7 +368,7 @@ static struct agent_pvt *add_agent(char *agent, int pending)
* Deletes an agent after doing some clean up. * Deletes an agent after doing some clean up.
* Further documentation: How safe is this function ? What state should the agent be to be cleaned. * Further documentation: How safe is this function ? What state should the agent be to be cleaned.
* \param p Agent to be deleted. * \param p Agent to be deleted.
* @returns Always 0. * \returns Always 0.
*/ */
static int agent_cleanup(struct agent_pvt *p) static int agent_cleanup(struct agent_pvt *p)
{ {
@ -653,16 +653,20 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
return res; return res;
} }
ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name); ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language); if (option_debug > 2)
ast_log(LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
res = ast_streamfile(p->chan, beep, p->chan->language); res = ast_streamfile(p->chan, beep, p->chan->language);
ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res); if (option_debug > 2)
ast_log(LOG_DEBUG, "Played beep, result '%d'\n", res);
if (!res) { if (!res) {
res = ast_waitstream(p->chan, ""); res = ast_waitstream(p->chan, "");
ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res); if (option_debug > 2)
ast_log(LOG_DEBUG, "Waited for stream, result '%d'\n", res);
} }
if (!res) { if (!res) {
res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats)); res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res); if (option_debug > 2)
ast_log(LOG_DEBUG, "Set read format, result '%d'\n", res);
if (res) if (res)
ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats))); ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
} else { } else {
@ -671,32 +675,32 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
} }
if (!res) { if (!res) {
ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats)); res = ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res); if (option_debug > 2)
ast_log(LOG_DEBUG, "Set write format, result '%d'\n", res);
if (res) if (res)
ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats))); ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(ast_best_codec(p->chan->nativeformats)));
} }
if( !res ) if(!res) {
{
/* Call is immediately up, or might need ack */ /* Call is immediately up, or might need ack */
if (p->ackcall > 1) if (p->ackcall > 1)
newstate = AST_STATE_RINGING; newstate = AST_STATE_RINGING;
else { else {
newstate = AST_STATE_UP; newstate = AST_STATE_UP;
if (recordagentcalls) if (recordagentcalls)
agent_start_monitoring(ast,0); agent_start_monitoring(ast, 0);
p->acknowledged = 1; p->acknowledged = 1;
} }
res = 0; res = 0;
} }
CLEANUP(ast,p); CLEANUP(ast, p);
ast_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
if (newstate) if (newstate)
ast_setstate(ast, newstate); ast_setstate(ast, newstate);
return res; return res;
} }
/* store/clear the global variable that stores agentid based on the callerid */ /*! \brief store/clear the global variable that stores agentid based on the callerid */
static void set_agentbycallerid(const char *callerid, const char *agent) static void set_agentbycallerid(const char *callerid, const char *agent)
{ {
char buf[AST_MAX_BUF]; char buf[AST_MAX_BUF];
@ -705,7 +709,7 @@ static void set_agentbycallerid(const char *callerid, const char *agent)
if (ast_strlen_zero(callerid)) if (ast_strlen_zero(callerid))
return; return;
snprintf(buf, sizeof(buf), "%s_%s",GETAGENTBYCALLERID, callerid); snprintf(buf, sizeof(buf), "%s_%s", GETAGENTBYCALLERID, callerid);
pbx_builtin_setvar_helper(NULL, buf, agent); pbx_builtin_setvar_helper(NULL, buf, agent);
} }
@ -730,13 +734,14 @@ static int agent_hangup(struct ast_channel *ast)
ast_atomic_fetchadd_int(&__mod_desc->usecnt, -1); ast_atomic_fetchadd_int(&__mod_desc->usecnt, -1);
/* XXX do we need ast_update_use_count(); */ /* XXX do we need ast_update_use_count(); */
ast_log(LOG_DEBUG, "Hangup called for state %s\n", ast_state2str(ast->_state)); if (option_debug)
ast_log(LOG_DEBUG, "Hangup called for state %s\n", ast_state2str(ast->_state));
if (p->start && (ast->_state != AST_STATE_UP)) { if (p->start && (ast->_state != AST_STATE_UP)) {
howlong = time(NULL) - p->start; howlong = time(NULL) - p->start;
p->start = 0; p->start = 0;
} else if (ast->_state == AST_STATE_RESERVED) { } else if (ast->_state == AST_STATE_RESERVED)
howlong = 0; howlong = 0;
} else else
p->start = 0; p->start = 0;
if (p->chan) { if (p->chan) {
p->chan->_bridge = NULL; p->chan->_bridge = NULL;
@ -767,13 +772,13 @@ static int agent_hangup(struct ast_channel *ast)
agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Autologoff"); agent_logoff_maintenance(p, p->loginchan, logintime, ast->uniqueid, "Autologoff");
} }
} else if (p->dead) { } else if (p->dead) {
ast_mutex_lock(&p->chan->lock); ast_channel_lock(p->chan);
ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT); ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
ast_mutex_unlock(&p->chan->lock); ast_channel_unlock(p->chan);
} else { } else {
ast_mutex_lock(&p->chan->lock); ast_channel_lock(p->chan);
ast_moh_start(p->chan, p->moh); ast_moh_start(p->chan, p->moh);
ast_mutex_unlock(&p->chan->lock); ast_channel_unlock(p->chan);
} }
} }
ast_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
@ -820,14 +825,14 @@ static int agent_cont_sleep( void *data )
res = 1; res = 1;
} }
ast_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
#if 0
if( !res ) if(option_debug > 4 && !res )
ast_log( LOG_DEBUG, "agent_cont_sleep() returning %d\n", res ); ast_log(LOG_DEBUG, "agent_cont_sleep() returning %d\n", res );
#endif
return res; return res;
} }
static int agent_ack_sleep( void *data ) static int agent_ack_sleep(void *data)
{ {
struct agent_pvt *p; struct agent_pvt *p;
int res=0; int res=0;
@ -836,50 +841,42 @@ static int agent_ack_sleep( void *data )
/* Wait a second and look for something */ /* Wait a second and look for something */
p = (struct agent_pvt *)data; p = (struct agent_pvt *) data;
if (p->chan) { if (!p->chan)
for(;;) { return -1;
to = ast_waitfor(p->chan, to);
if (to < 0) { for(;;) {
res = -1; to = ast_waitfor(p->chan, to);
break; if (to < 0)
} return -1;
if (!to) { if (!to)
res = 0; return 0;
break; f = ast_read(p->chan);
} if (!f)
f = ast_read(p->chan); return -1;
if (!f) { if (f->frametype == AST_FRAME_DTMF)
res = -1; res = f->subclass;
break; else
}
if (f->frametype == AST_FRAME_DTMF)
res = f->subclass;
else
res = 0;
ast_frfree(f);
ast_mutex_lock(&p->lock);
if (!p->app_sleep_cond) {
ast_mutex_unlock(&p->lock);
res = 0;
break;
} else if (res == '#') {
ast_mutex_unlock(&p->lock);
res = 1;
break;
}
ast_mutex_unlock(&p->lock);
res = 0; res = 0;
ast_frfree(f);
ast_mutex_lock(&p->lock);
if (!p->app_sleep_cond) {
ast_mutex_unlock(&p->lock);
return 0;
} else if (res == '#') {
ast_mutex_unlock(&p->lock);
return 1;
} }
} else ast_mutex_unlock(&p->lock);
res = -1; res = 0;
}
return res; return res;
} }
static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge) static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
{ {
struct agent_pvt *p = bridge->tech_pvt; struct agent_pvt *p = bridge->tech_pvt;
struct ast_channel *ret=NULL; struct ast_channel *ret = NULL;
if (p) { if (p) {
if (chan == p->chan) if (chan == p->chan)
@ -893,7 +890,7 @@ static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct
return ret; return ret;
} }
/*! \brief Create new agent channel ---*/ /*! \brief Create new agent channel */
static struct ast_channel *agent_new(struct agent_pvt *p, int state) static struct ast_channel *agent_new(struct agent_pvt *p, int state)
{ {
struct ast_channel *tmp; struct ast_channel *tmp;
@ -904,74 +901,74 @@ static struct ast_channel *agent_new(struct agent_pvt *p, int state)
} }
#endif #endif
tmp = ast_channel_alloc(0); tmp = ast_channel_alloc(0);
if (tmp) { if (!tmp) {
tmp->tech = &agent_tech; ast_log(LOG_WARNING, "Unable to allocate agent channel structure\n");
return NULL;
}
tmp->tech = &agent_tech;
if (p->chan) {
tmp->nativeformats = p->chan->nativeformats;
tmp->writeformat = p->chan->writeformat;
tmp->rawwriteformat = p->chan->writeformat;
tmp->readformat = p->chan->readformat;
tmp->rawreadformat = p->chan->readformat;
ast_string_field_set(tmp, language, p->chan->language);
ast_copy_string(tmp->context, p->chan->context, sizeof(tmp->context));
ast_copy_string(tmp->exten, p->chan->exten, sizeof(tmp->exten));
/* XXX Is this really all we copy form the originating channel?? */
} else {
tmp->nativeformats = AST_FORMAT_SLINEAR;
tmp->writeformat = AST_FORMAT_SLINEAR;
tmp->rawwriteformat = AST_FORMAT_SLINEAR;
tmp->readformat = AST_FORMAT_SLINEAR;
tmp->rawreadformat = AST_FORMAT_SLINEAR;
}
if (p->pending)
ast_string_field_build(tmp, name, "Agent/P%s-%d", p->agent, ast_random() & 0xffff);
else
ast_string_field_build(tmp, name, "Agent/%s", p->agent);
/* Safe, agentlock already held */
ast_setstate(tmp, state);
tmp->tech_pvt = p;
p->owner = tmp;
ast_atomic_fetchadd_int(&__mod_desc->usecnt, +1);
ast_update_use_count();
tmp->priority = 1;
/* Wake up and wait for other applications (by definition the login app)
* to release this channel). Takes ownership of the agent channel
* to this thread only.
* For signalling the other thread, ast_queue_frame is used until we
* can safely use signals for this purpose. The pselect() needs to be
* implemented in the kernel for this.
*/
p->app_sleep_cond = 0;
if(ast_mutex_trylock(&p->app_lock)) {
if (p->chan) { if (p->chan) {
tmp->nativeformats = p->chan->nativeformats; ast_queue_frame(p->chan, &ast_null_frame);
tmp->writeformat = p->chan->writeformat; ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
tmp->rawwriteformat = p->chan->writeformat; ast_mutex_lock(&p->app_lock);
tmp->readformat = p->chan->readformat; ast_mutex_lock(&p->lock);
tmp->rawreadformat = p->chan->readformat;
ast_string_field_set(tmp, language, p->chan->language);
ast_copy_string(tmp->context, p->chan->context, sizeof(tmp->context));
ast_copy_string(tmp->exten, p->chan->exten, sizeof(tmp->exten));
} else { } else {
tmp->nativeformats = AST_FORMAT_SLINEAR; ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
tmp->writeformat = AST_FORMAT_SLINEAR; p->owner = NULL;
tmp->rawwriteformat = AST_FORMAT_SLINEAR; tmp->tech_pvt = NULL;
tmp->readformat = AST_FORMAT_SLINEAR; p->app_sleep_cond = 1;
tmp->rawreadformat = AST_FORMAT_SLINEAR; ast_channel_free( tmp );
} ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
if (p->pending) ast_mutex_unlock(&p->app_lock);
ast_string_field_build(tmp, name, "Agent/P%s-%d", p->agent, ast_random() & 0xffff); return NULL;
else
ast_string_field_build(tmp, name, "Agent/%s", p->agent);
/* Safe, agentlock already held */
ast_setstate(tmp, state);
tmp->tech_pvt = p;
p->owner = tmp;
ast_atomic_fetchadd_int(&__mod_desc->usecnt, +1);
ast_update_use_count();
tmp->priority = 1;
/* Wake up and wait for other applications (by definition the login app)
* to release this channel). Takes ownership of the agent channel
* to this thread only.
* For signalling the other thread, ast_queue_frame is used until we
* can safely use signals for this purpose. The pselect() needs to be
* implemented in the kernel for this.
*/
p->app_sleep_cond = 0;
if( ast_mutex_trylock(&p->app_lock) )
{
if (p->chan) {
ast_queue_frame(p->chan, &ast_null_frame);
ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
ast_mutex_lock(&p->app_lock);
ast_mutex_lock(&p->lock);
}
if( !p->chan )
{
ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
p->owner = NULL;
tmp->tech_pvt = NULL;
p->app_sleep_cond = 1;
ast_channel_free( tmp );
ast_mutex_unlock(&p->lock); /* For other thread to read the condition. */
ast_mutex_unlock(&p->app_lock);
return NULL;
}
} }
p->owning_app = pthread_self(); }
/* After the above step, there should not be any blockers. */ p->owning_app = pthread_self();
if (p->chan) { /* After the above step, there should not be any blockers. */
if (ast_test_flag(p->chan, AST_FLAG_BLOCKING)) { if (p->chan) {
ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" ); if (ast_test_flag(p->chan, AST_FLAG_BLOCKING)) {
CRASH; ast_log( LOG_ERROR, "A blocker exists after agent channel ownership acquired\n" );
} CRASH;
ast_moh_stop(p->chan);
} }
} else ast_moh_stop(p->chan);
ast_log(LOG_WARNING, "Unable to allocate agent channel structure\n"); }
return tmp; return tmp;
} }
@ -979,7 +976,7 @@ static struct ast_channel *agent_new(struct agent_pvt *p, int state)
/*! /*!
* Read configuration data. The file named agents.conf. * Read configuration data. The file named agents.conf.
* *
* @returns Always 0, or so it seems. * \returns Always 0, or so it seems.
*/ */
static int read_agent_config(void) static int read_agent_config(void)
{ {
@ -1013,10 +1010,7 @@ static int read_agent_config(void)
/* Read in [general] section for persistence */ /* Read in [general] section for persistence */
if ((general_val = ast_variable_retrieve(cfg, "general", "persistentagents"))) if ((general_val = ast_variable_retrieve(cfg, "general", "persistentagents")))
persistent_agents = ast_true(general_val); persistent_agents = ast_true(general_val);
if (ast_false(ast_variable_retrieve(cfg, "general", "multiplelogin") ) ) multiplelogin = ast_true(ast_variable_retrieve(cfg, "general", "multiplelogin"));
multiplelogin=0;
if (ast_true(ast_variable_retrieve(cfg, "general", "multiplelogin") ) )
multiplelogin=1;
/* Read in the [agents] section */ /* Read in the [agents] section */
v = ast_variable_browse(cfg, "agents"); v = ast_variable_browse(cfg, "agents");
@ -1221,16 +1215,17 @@ static int check_beep(struct agent_pvt *newlyavailable, int needlock)
} }
/* return 1 if multiple login is fine, 0 if it is not and we find a match, -1 if multiplelogin is not allowed and we don't find a match. */ /* return 1 if multiple login is fine, 0 if it is not and we find a match, -1 if multiplelogin is not allowed and we don't find a match. */
static int allow_multiple_login(char *chan,char *context) static int allow_multiple_login(char *chan, char *context)
{ {
struct agent_pvt *p; struct agent_pvt *p;
char loginchan[80]; char loginchan[80];
if(multiplelogin) if(multiplelogin)
return 1; return 1;
if(!chan) if(!chan)
return 0; return 0;
snprintf(loginchan, sizeof(loginchan), "%s@%s", chan, S_OR(context,"default")); snprintf(loginchan, sizeof(loginchan), "%s@%s", chan, S_OR(context, "default"));
AST_LIST_TRAVERSE(&agents, p, list) { AST_LIST_TRAVERSE(&agents, p, list) {
if(!strcasecmp(chan, p->loginchan)) if(!strcasecmp(chan, p->loginchan))
@ -1239,7 +1234,7 @@ static int allow_multiple_login(char *chan,char *context)
return -1; return -1;
} }
/*! \brief Part of the Asterisk PBX interface ---*/ /*! \brief Part of the Asterisk PBX interface */
static struct ast_channel *agent_request(const char *type, int format, void *data, int *cause) static struct ast_channel *agent_request(const char *type, int format, void *data, int *cause)
{ {
struct agent_pvt *p; struct agent_pvt *p;
@ -1257,9 +1252,8 @@ static struct ast_channel *agent_request(const char *type, int format, void *dat
} else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupoff) == 1)) { } else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupoff) == 1)) {
groupmatch = (1 << groupoff); groupmatch = (1 << groupoff);
waitforagent = 1; waitforagent = 1;
} else { } else
groupmatch = 0; groupmatch = 0;
}
/* Check actual logged in agents first */ /* Check actual logged in agents first */
AST_LIST_LOCK(&agents); AST_LIST_LOCK(&agents);
@ -1324,16 +1318,12 @@ static struct ast_channel *agent_request(const char *type, int format, void *dat
p = add_agent(data, 1); p = add_agent(data, 1);
p->group = groupmatch; p->group = groupmatch;
chan = agent_new(p, AST_STATE_DOWN); chan = agent_new(p, AST_STATE_DOWN);
if (!chan) { if (!chan)
ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n"); ast_log(LOG_WARNING, "Weird... Fix this to drop the unused pending agent\n");
}
} else } else
ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s); ast_log(LOG_DEBUG, "Not creating place holder for '%s' since nobody logged in\n", s);
} }
if (hasagent) *cause = hasagent ? AST_CAUSE_BUSY : AST_CAUSE_UNREGISTERED;
*cause = AST_CAUSE_BUSY;
else
*cause = AST_CAUSE_UNREGISTERED;
AST_LIST_UNLOCK(&agents); AST_LIST_UNLOCK(&agents);
return chan; return chan;
} }
@ -1353,7 +1343,7 @@ static force_inline int powerof(unsigned int d)
* It is registered on load_module() and it gets called by the manager backend. * It is registered on load_module() and it gets called by the manager backend.
* \param s * \param s
* \param m * \param m
* @returns * \returns
* \sa action_agent_logoff(), action_agent_callback_login(), load_module(). * \sa action_agent_logoff(), action_agent_callback_login(), load_module().
*/ */
static int action_agents(struct mansession *s, struct message *m) static int action_agents(struct mansession *s, struct message *m)
@ -1380,11 +1370,7 @@ static int action_agents(struct mansession *s, struct message *m)
AGENT_ONCALL - Agent is logged in, and on a call AGENT_ONCALL - Agent is logged in, and on a call
AGENT_UNKNOWN - Don't know anything about agent. Shouldn't ever get this. */ AGENT_UNKNOWN - Don't know anything about agent. Shouldn't ever get this. */
if(!ast_strlen_zero(p->name)) { username = S_OR(p->name, "None");
username = p->name;
} else {
username = "None";
}
/* Set a default status. It 'should' get changed. */ /* Set a default status. It 'should' get changed. */
status = "AGENT_UNKNOWN"; status = "AGENT_UNKNOWN";
@ -1438,9 +1424,9 @@ static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long
if (!ast_strlen_zero(logcommand)) if (!ast_strlen_zero(logcommand))
tmp = logcommand; tmp = logcommand;
else { else
tmp = ast_strdupa(""); tmp = ast_strdupa("");
}
snprintf(agent, sizeof(agent), "Agent/%s", p->agent); snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
if (!ast_strlen_zero(uniqueid)) { if (!ast_strlen_zero(uniqueid)) {
@ -1451,7 +1437,6 @@ static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long
"Logintime: %ld\r\n" "Logintime: %ld\r\n"
"Uniqueid: %s\r\n", "Uniqueid: %s\r\n",
p->agent, tmp, loginchan, logintime, uniqueid); p->agent, tmp, loginchan, logintime, uniqueid);
ast_queue_log("NONE", uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
} else { } else {
manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff", manager_event(EVENT_FLAG_AGENT, "Agentcallbacklogoff",
"Agent: %s\r\n" "Agent: %s\r\n"
@ -1459,10 +1444,9 @@ static void agent_logoff_maintenance(struct agent_pvt *p, char *loginchan, long
"Loginchan: %s\r\n" "Loginchan: %s\r\n"
"Logintime: %ld\r\n", "Logintime: %ld\r\n",
p->agent, tmp, loginchan, logintime); p->agent, tmp, loginchan, logintime);
ast_queue_log("NONE", "NONE", agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
} }
ast_queue_log("NONE", ast_strlen_zero(uniqueid) ? "NONE" : uniqueid, agent, "AGENTCALLBACKLOGOFF", "%s|%ld|%s", loginchan, logintime, tmp);
set_agentbycallerid(p->logincallerid, NULL); set_agentbycallerid(p->logincallerid, NULL);
p->loginchan[0] ='\0'; p->loginchan[0] ='\0';
p->logincallerid[0] = '\0'; p->logincallerid[0] = '\0';
@ -1481,12 +1465,10 @@ static int agent_logoff(char *agent, int soft)
AST_LIST_TRAVERSE(&agents, p, list) { AST_LIST_TRAVERSE(&agents, p, list) {
if (!strcasecmp(p->agent, agent)) { if (!strcasecmp(p->agent, agent)) {
if (!soft) { if (!soft) {
if (p->owner) { if (p->owner)
ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT); ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
} if (p->chan)
if (p->chan) {
ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT); ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
}
} }
ret = 0; /* found an agent => return 0 */ ret = 0; /* found an agent => return 0 */
logintime = time(NULL) - p->loginstart; logintime = time(NULL) - p->loginstart;
@ -1522,7 +1504,7 @@ static int agent_logoff_cmd(int fd, int argc, char **argv)
* It is registered on load_module() and it gets called by the manager backend. * It is registered on load_module() and it gets called by the manager backend.
* \param s * \param s
* \param m * \param m
* @returns * \returns
* \sa action_agents(), action_agent_callback_login(), load_module(). * \sa action_agents(), action_agent_callback_login(), load_module().
*/ */
static int action_agent_logoff(struct mansession *s, struct message *m) static int action_agent_logoff(struct mansession *s, struct message *m)
@ -1537,11 +1519,7 @@ static int action_agent_logoff(struct mansession *s, struct message *m)
return 0; return 0;
} }
if (ast_true(soft_s)) soft = ast_true(soft_s) ? 1 : 0;
soft = 1;
else
soft = 0;
ret = agent_logoff(agent, soft); ret = agent_logoff(agent, soft);
if (ret == 0) if (ret == 0)
astman_send_ack(s, m, "Agent logged out"); astman_send_ack(s, m, "Agent logged out");
@ -1563,9 +1541,9 @@ static char *complete_agent_logoff_cmd(const char *line, const char *word, int p
if (!strncasecmp(word, name, len) && ++which > state) if (!strncasecmp(word, name, len) && ++which > state)
return ast_strdup(name); return ast_strdup(name);
} }
} else if (pos == 3 && state == 0) { } else if (pos == 3 && state == 0)
return ast_strdup("soft"); return ast_strdup("soft");
}
return NULL; return NULL;
} }
@ -1579,9 +1557,9 @@ static int agents_show(int fd, int argc, char **argv)
char location[AST_MAX_BUF] = ""; char location[AST_MAX_BUF] = "";
char talkingto[AST_MAX_BUF] = ""; char talkingto[AST_MAX_BUF] = "";
char moh[AST_MAX_BUF]; char moh[AST_MAX_BUF];
int count_agents = 0; /* Number of agents configured */ int count_agents = 0; /*!< Number of agents configured */
int online_agents = 0; /* Number of online agents */ int online_agents = 0; /*!< Number of online agents */
int offline_agents = 0; /* Number of offline agents */ int offline_agents = 0; /*!< Number of offline agents */
if (argc != 2) if (argc != 2)
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
AST_LIST_LOCK(&agents); AST_LIST_LOCK(&agents);
@ -1599,11 +1577,10 @@ static int agents_show(int fd, int argc, char **argv)
username[0] = '\0'; username[0] = '\0';
if (p->chan) { if (p->chan) {
snprintf(location, sizeof(location), "logged in on %s", p->chan->name); snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
if (p->owner && ast_bridged_channel(p->owner)) { if (p->owner && ast_bridged_channel(p->owner))
snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name); snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name);
} else { else
strcpy(talkingto, " is idle"); strcpy(talkingto, " is idle");
}
online_agents++; online_agents++;
} else if (!ast_strlen_zero(p->loginchan)) { } else if (!ast_strlen_zero(p->loginchan)) {
if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0 || !(p->lastdisc.tv_sec)) if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0 || !(p->lastdisc.tv_sec))
@ -1628,11 +1605,10 @@ static int agents_show(int fd, int argc, char **argv)
ast_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
} }
AST_LIST_UNLOCK(&agents); AST_LIST_UNLOCK(&agents);
if ( !count_agents ) { if ( !count_agents )
ast_cli(fd, "No Agents are configured in %s\n",config); ast_cli(fd, "No Agents are configured in %s\n",config);
} else { else
ast_cli(fd, "%d agents configured [%d online , %d offline]\n",count_agents, online_agents, offline_agents); ast_cli(fd, "%d agents configured [%d online , %d offline]\n",count_agents, online_agents, offline_agents);
}
ast_cli(fd, "\n"); ast_cli(fd, "\n");
return RESULT_SUCCESS; return RESULT_SUCCESS;
@ -1661,11 +1637,10 @@ static int agents_show_online(int fd, int argc, char **argv)
username[0] = '\0'; username[0] = '\0';
if (p->chan) { if (p->chan) {
snprintf(location, sizeof(location), "logged in on %s", p->chan->name); snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
if (p->owner && ast_bridged_channel(p->owner)) { if (p->owner && ast_bridged_channel(p->owner))
snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name); snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name);
} else { else
strcpy(talkingto, " is idle"); strcpy(talkingto, " is idle");
}
agent_status = 1; agent_status = 1;
online_agents++; online_agents++;
} else if (!ast_strlen_zero(p->loginchan)) { } else if (!ast_strlen_zero(p->loginchan)) {
@ -1684,10 +1659,10 @@ static int agents_show_online(int fd, int argc, char **argv)
ast_mutex_unlock(&p->lock); ast_mutex_unlock(&p->lock);
} }
AST_LIST_UNLOCK(&agents); AST_LIST_UNLOCK(&agents);
if ( !count_agents ) if (!count_agents)
ast_cli(fd, "No Agents are configured in %s\n",config); ast_cli(fd, "No Agents are configured in %s\n", config);
else else
ast_cli(fd, "%d agents online\n",online_agents); ast_cli(fd, "%d agents online\n", online_agents);
ast_cli(fd, "\n"); ast_cli(fd, "\n");
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
@ -2010,8 +1985,7 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
if (option_verbose > 1) if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Agent '%s' logged in (format %s/%s)\n", p->agent, ast_verbose(VERBOSE_PREFIX_2 "Agent '%s' logged in (format %s/%s)\n", p->agent,
ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat)); ast_getformatname(chan->readformat), ast_getformatname(chan->writeformat));
/* Login this channel and wait for it to /* Login this channel and wait for it to go away */
go away */
p->chan = chan; p->chan = chan;
if (p->ackcall > 1) if (p->ackcall > 1)
check_beep(p, 0); check_beep(p, 0);
@ -2053,8 +2027,7 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
if (p->ackcall > 1) if (p->ackcall > 1)
res = agent_ack_sleep(p); res = agent_ack_sleep(p);
else else
res = ast_safe_sleep_conditional( chan, 1000, res = ast_safe_sleep_conditional( chan, 1000, agent_cont_sleep, p );
agent_cont_sleep, p );
ast_mutex_unlock( &p->app_lock ); ast_mutex_unlock( &p->app_lock );
if ((p->ackcall > 1) && (res == 1)) { if ((p->ackcall > 1) && (res == 1)) {
AST_LIST_LOCK(&agents); AST_LIST_LOCK(&agents);
@ -2120,21 +2093,16 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
if (!callbackmode) { if (!callbackmode) {
LOCAL_USER_REMOVE(u); LOCAL_USER_REMOVE(u);
return -1; return -1;
} } else { /* AgentCallbackLogin() exit*/
/* AgentCallbackLogin() exit*/
else {
/* Set variables */ /* Set variables */
if (login_state > 0) { if (login_state > 0) {
pbx_builtin_setvar_helper(chan, "AGENTNUMBER", user); pbx_builtin_setvar_helper(chan, "AGENTNUMBER", user);
if (login_state==1) { if (login_state==1) {
pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "on"); pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "on");
pbx_builtin_setvar_helper(chan, "AGENTEXTEN", args.extension); pbx_builtin_setvar_helper(chan, "AGENTEXTEN", args.extension);
} } else
else {
pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "off"); pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "off");
} } else {
}
else {
pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "fail"); pbx_builtin_setvar_helper(chan, "AGENTSTATUS", "fail");
} }
if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 1, chan->cid.cid_num)) { if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 1, chan->cid.cid_num)) {
@ -2164,7 +2132,7 @@ static int __login_exec(struct ast_channel *chan, void *data, int callbackmode)
* *
* \param chan * \param chan
* \param data * \param data
* @returns * \returns
* \sa callback_login_exec(), agentmonitoroutgoing_exec(), load_module(). * \sa callback_login_exec(), agentmonitoroutgoing_exec(), load_module().
*/ */
static int login_exec(struct ast_channel *chan, void *data) static int login_exec(struct ast_channel *chan, void *data)
@ -2177,7 +2145,7 @@ static int login_exec(struct ast_channel *chan, void *data)
* *
* \param chan * \param chan
* \param data * \param data
* @returns * \returns
* \sa login_exec(), agentmonitoroutgoing_exec(), load_module(). * \sa login_exec(), agentmonitoroutgoing_exec(), load_module().
*/ */
static int callback_exec(struct ast_channel *chan, void *data) static int callback_exec(struct ast_channel *chan, void *data)
@ -2190,7 +2158,7 @@ static int callback_exec(struct ast_channel *chan, void *data)
* It is registered on load_module() and it gets called by the manager backend. * It is registered on load_module() and it gets called by the manager backend.
* \param s * \param s
* \param m * \param m
* @returns * \returns
* \sa action_agents(), action_agent_logoff(), load_module(). * \sa action_agents(), action_agent_logoff(), load_module().
*/ */
static int action_agent_callback_login(struct mansession *s, struct message *m) static int action_agent_callback_login(struct mansession *s, struct message *m)
@ -2215,9 +2183,8 @@ static int action_agent_callback_login(struct mansession *s, struct message *m)
AST_LIST_LOCK(&agents); AST_LIST_LOCK(&agents);
AST_LIST_TRAVERSE(&agents, p, list) { AST_LIST_TRAVERSE(&agents, p, list) {
if (strcmp(p->agent, agent) || p->pending) { if (strcmp(p->agent, agent) || p->pending)
continue; continue;
}
if (p->chan) { if (p->chan) {
login_state = 2; /* already logged in (and on the phone)*/ login_state = 2; /* already logged in (and on the phone)*/
break; break;
@ -2325,8 +2292,7 @@ static int agentmonitoroutgoing_exec(struct ast_channel *chan, void *data)
chan->priority+=100; chan->priority+=100;
if (option_verbose > 2) if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Going to %d priority because there is no callerid or the agentid cannot be found.\n",chan->priority); ast_verbose(VERBOSE_PREFIX_3 "Going to %d priority because there is no callerid or the agentid cannot be found.\n",chan->priority);
} } else if (exitifnoagentid)
else if (exitifnoagentid)
return res; return res;
} }
return 0; return 0;
@ -2389,7 +2355,7 @@ static void reload_agents(void)
ast_mutex_unlock(&cur_agent->lock); ast_mutex_unlock(&cur_agent->lock);
if (!ast_db_get(pa_family, agent_num, agent_data, sizeof(agent_data)-1)) { if (!ast_db_get(pa_family, agent_num, agent_data, sizeof(agent_data)-1)) {
if (option_debug) if (option_debug)
ast_log(LOG_DEBUG, "Reload Agent: %s on %s\n", cur_agent->agent, agent_data); ast_log(LOG_DEBUG, "Reload Agent from AstDB: %s on %s\n", cur_agent->agent, agent_data);
parse = agent_data; parse = agent_data;
agent_chan = strsep(&parse, ";"); agent_chan = strsep(&parse, ";");
agent_callerid = strsep(&parse, ";"); agent_callerid = strsep(&parse, ";");
@ -2422,14 +2388,13 @@ static int agent_devicestate(void *data)
int res = AST_DEVICE_INVALID; int res = AST_DEVICE_INVALID;
s = data; s = data;
if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupoff) == 1)) { if ((s[0] == '@') && (sscanf(s + 1, "%d", &groupoff) == 1))
groupmatch = (1 << groupoff); groupmatch = (1 << groupoff);
} else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupoff) == 1)) { else if ((s[0] == ':') && (sscanf(s + 1, "%d", &groupoff) == 1)) {
groupmatch = (1 << groupoff); groupmatch = (1 << groupoff);
waitforagent = 1; waitforagent = 1;
} else { } else
groupmatch = 0; groupmatch = 0;
}
/* Check actual logged in agents first */ /* Check actual logged in agents first */
AST_LIST_LOCK(&agents); AST_LIST_LOCK(&agents);
@ -2504,22 +2469,21 @@ static int function_agent(struct ast_channel *chan, char *cmd, char *data, char
if (agent->chan || !ast_strlen_zero(agent->loginchan)) if (agent->chan || !ast_strlen_zero(agent->loginchan))
status = "LOGGEDIN"; status = "LOGGEDIN";
ast_copy_string(buf, status, len); ast_copy_string(buf, status, len);
} else if (!strcasecmp(args.item, "password")) { } else if (!strcasecmp(args.item, "password"))
ast_copy_string(buf, agent->password, len); ast_copy_string(buf, agent->password, len);
} else if (!strcasecmp(args.item, "name")) { else if (!strcasecmp(args.item, "name"))
ast_copy_string(buf, agent->name, len); ast_copy_string(buf, agent->name, len);
} else if (!strcasecmp(args.item, "mohclass")) { else if (!strcasecmp(args.item, "mohclass"))
ast_copy_string(buf, agent->moh, len); ast_copy_string(buf, agent->moh, len);
} else if (!strcasecmp(args.item, "channel")) { else if (!strcasecmp(args.item, "channel")) {
if (agent->chan) { if (agent->chan) {
ast_copy_string(buf, agent->chan->name, len); ast_copy_string(buf, agent->chan->name, len);
tmp = strrchr(buf, '-'); tmp = strrchr(buf, '-');
if (tmp) if (tmp)
*tmp = '\0'; *tmp = '\0';
} }
} else if (!strcasecmp(args.item, "exten")) { } else if (!strcasecmp(args.item, "exten"))
ast_copy_string(buf, agent->loginchan, len); ast_copy_string(buf, agent->loginchan, len);
}
return 0; return 0;
} }

Loading…
Cancel
Save