Sat Apr 26 2014 22:02:01

Asterisk developer's documentation


chan_agent.c File Reference

Implementation of Agents (proxy channel) More...

#include "asterisk.h"
#include <sys/socket.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/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/devicestate.h"
#include "asterisk/monitor.h"
#include "asterisk/stringfields.h"
#include "asterisk/event.h"
#include "asterisk/data.h"
Include dependency graph for chan_agent.c:

Go to the source code of this file.

Data Structures

struct  agent_pvt
 Structure representing an agent. More...
struct  agents

Defines

#define AST_MAX_AGENT   80
#define AST_MAX_BUF   256
#define AST_MAX_FILENAME_LEN   256
#define CHECK_FORMATS(ast, p)
#define CLEANUP(ast, p)
 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 DATA_EXPORT_AGENT(MEMBER)
#define DEFAULT_ACCEPTDTMF   '#'
#define DEFAULT_ENDDTMF   '*'
#define GETAGENTBYCALLERID   "AGENTBYCALLERID"
#define PA_MAX_LEN   2048

Enumerations

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)
}

Functions

static int __agent_start_monitoring (struct ast_channel *ast, struct agent_pvt *p, int needlock)
static void __reg_module (void)
static void __unreg_module (void)
static int action_agent_logoff (struct mansession *s, const struct message *m)
static int action_agents (struct mansession *s, const struct message *m)
static struct agent_pvtadd_agent (const char *agent, int pending)
static int agent_ack_sleep (struct agent_pvt *p)
static int agent_answer (struct ast_channel *ast)
static struct ast_channelagent_bridgedchannel (struct ast_channel *chan, struct ast_channel *bridge)
static int agent_call (struct ast_channel *ast, const char *dest, int timeout)
static int agent_cleanup (struct agent_pvt *p)
static int agent_cont_sleep (void *data)
static int agent_devicestate (const char *data)
 Part of PBX channel interface.
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_fixup (struct ast_channel *oldchan, struct ast_channel *newchan)
static struct ast_channelagent_get_base_channel (struct ast_channel *chan)
 return the channel or base channel if one exists. This function assumes the channel it is called on is already locked
static int agent_hangup (struct ast_channel *ast)
static int agent_indicate (struct ast_channel *ast, int condition, const void *data, size_t datalen)
static struct ast_channelagent_lock_owner (struct agent_pvt *pvt)
 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.
static int agent_logoff (const char *agent, int soft)
static char * agent_logoff_cmd (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static struct ast_channelagent_new (struct agent_pvt *p, int state, const char *linkedid, struct ast_callid *callid)
 Create new agent channel.
static void agent_pvt_destroy (struct agent_pvt *doomed)
static struct ast_frameagent_read (struct ast_channel *ast)
static struct ast_channelagent_request (const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
 Part of the Asterisk PBX interface.
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_start_monitoring (struct ast_channel *ast, int needlock)
static int agent_write (struct ast_channel *ast, struct ast_frame *f)
static int agentmonitoroutgoing_exec (struct ast_channel *chan, const char *data)
 Called by the AgentMonitorOutgoing application (from the dial plan).
static int agents_data_provider_get (const struct ast_data_search *search, struct ast_data *data_root)
static char * agents_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * agents_show_online (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 AST_DATA_STRUCTURE (agent_pvt, DATA_EXPORT_AGENT)
static int check_availability (struct agent_pvt *newlyavailable, int needlock)
static int check_beep (struct agent_pvt *newlyavailable, int needlock)
static char * complete_agent_logoff_cmd (const char *line, const char *word, int pos, int state)
static struct agent_pvtfind_agent (char *agentid)
static int function_agent (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int load_module (void)
 Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other things it registers applications, cli commands and reads the cofiguration file.
static int login_exec (struct ast_channel *chan, const char *data)
 Log in agent application.
static force_inline int powerof (unsigned int d)
static int read_agent_config (int reload)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Agent Proxy Channel" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CHANNEL_DRIVER, .nonoptreq = "res_monitor,chan_local", }
static char acceptdtmf = DEFAULT_ACCEPTDTMF
static int ackcall
static struct ast_custom_function agent_function
static const char agent_logoff_usage [] = " If 'soft' is specified, do not hangup existing calls.\n"
static struct ast_channel_tech agent_tech
 Channel interface description for PBX integration.
static char agentgoodbye [AST_MAX_FILENAME_LEN] = "vm-goodbye"
static struct agents agents
static struct ast_data_handler agents_data_provider
static struct ast_data_entry agents_data_providers []
static const char app [] = "AgentLogin"
static const char app3 [] = "AgentMonitorOutgoing"
static struct ast_module_infoast_module_info = &__mod_info
static int autologoff
static int autologoffunavail = 0
static char beep [AST_MAX_BUF] = "beep"
static struct ast_cli_entry cli_agents []
static const char config [] = "agents.conf"
static int endcall
static char enddtmf = DEFAULT_ENDDTMF
static ast_group_t group
static int maxlogintries = 3
static char moh [80] = "default"
static const char pa_family [] = "Agents"
static int recordagentcalls = 0
static char recordformat [AST_MAX_BUF] = ""
static char recordformatext [AST_MAX_BUF] = ""
static char savecallsin [AST_MAX_BUF] = ""
static const char tdesc [] = "Call Agent Proxy Channel"
static int updatecdr = 0
static char urlprefix [AST_MAX_BUF] = ""
static int wrapuptime

Detailed Description

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.

See also

Definition in file chan_agent.c.


Define Documentation

#define AST_MAX_AGENT   80

Agent ID or Password max length

Definition at line 211 of file chan_agent.c.

Referenced by agentmonitoroutgoing_exec(), complete_agent_logoff_cmd(), and login_exec().

#define AST_MAX_BUF   256
#define AST_MAX_FILENAME_LEN   256

Definition at line 213 of file chan_agent.c.

Referenced by login_exec().

#define CHECK_FORMATS (   ast,
 
)

Definition at line 296 of file chan_agent.c.

Referenced by agent_read(), and agent_write().

#define CLEANUP (   ast,
 
)

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.

Definition at line 318 of file chan_agent.c.

Referenced by agent_call(), agent_read(), and agent_write().

#define DATA_EXPORT_AGENT (   MEMBER)

Definition at line 282 of file chan_agent.c.

#define DEFAULT_ACCEPTDTMF   '#'

Definition at line 218 of file chan_agent.c.

#define DEFAULT_ENDDTMF   '*'

Definition at line 219 of file chan_agent.c.

#define GETAGENTBYCALLERID   "AGENTBYCALLERID"

Definition at line 241 of file chan_agent.c.

Referenced by agentmonitoroutgoing_exec().

#define PA_MAX_LEN   2048

The maximum length of each persistent member agent database entry

Definition at line 216 of file chan_agent.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
AGENT_FLAG_ACKCALL 
AGENT_FLAG_AUTOLOGOFF 
AGENT_FLAG_WRAPUPTIME 
AGENT_FLAG_ACCEPTDTMF 
AGENT_FLAG_ENDDTMF 

Definition at line 243 of file chan_agent.c.

     {
   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),
};

Function Documentation

static int __agent_start_monitoring ( struct ast_channel ast,
struct agent_pvt p,
int  needlock 
) [static]

Definition at line 574 of file chan_agent.c.

References agent_pvt::agent, ast_cdr_alloc(), ast_cdr_setuserfield(), ast_channel_cdr(), ast_channel_cdr_set(), ast_channel_monitor(), ast_channel_uniqueid(), ast_log(), AST_MAX_BUF, ast_monitor_setjoinfiles(), ast_monitor_start(), ast_verbose(), LOG_ERROR, X_REC_IN, and X_REC_OUT.

Referenced by agent_start_monitoring(), and agentmonitoroutgoing_exec().

{
   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, '.')))
         *pointer = '-';
      snprintf(tmp, sizeof(tmp), "%s%s", savecallsin, filename);
      ast_monitor_start(ast, recordformat, tmp, needlock, X_REC_IN | X_REC_OUT);
      ast_monitor_setjoinfiles(ast, 1);
      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 void __reg_module ( void  ) [static]

Definition at line 2593 of file chan_agent.c.

static void __unreg_module ( void  ) [static]

Definition at line 2593 of file chan_agent.c.

static int action_agent_logoff ( struct mansession s,
const struct message m 
) [static]

Sets an agent as no longer logged in in the Manager API. It is registered on load_module() and it gets called by the manager backend.

Parameters:
s
m
Returns:
See also:
action_agents(), load_module().

Definition at line 1666 of file chan_agent.c.

References agent_pvt::agent, agent_logoff(), ast_strlen_zero(), ast_true(), astman_get_header(), astman_send_ack(), and astman_send_error().

Referenced by load_module().

{
   const char *agent = astman_get_header(m, "Agent");
   const char *soft_s = astman_get_header(m, "Soft"); /* "true" is don't hangup */
   int soft;
   int ret; /* return value of agent_logoff */

   if (ast_strlen_zero(agent)) {
      astman_send_error(s, m, "No agent specified");
      return 0;
   }

   soft = ast_true(soft_s) ? 1 : 0;
   ret = agent_logoff(agent, soft);
   if (ret == 0)
      astman_send_ack(s, m, "Agent logged out");
   else
      astman_send_error(s, m, "No such agent");

   return 0;
}
static int action_agents ( struct mansession s,
const struct message m 
) [static]

Lists agents and their status to the Manager API. It is registered on load_module() and it gets called by the manager backend. This function locks both the pvt and the channel that owns it for a while, but does not keep these locks.

Parameters:
s
m
Returns:
See also:
action_agent_logoff(), load_module().

Definition at line 1507 of file chan_agent.c.

References agent_pvt::agent, agent_lock_owner(), ast_bridged_channel(), ast_channel_caller(), ast_channel_internal_bridged_channel(), ast_channel_name(), ast_channel_unlock, ast_channel_unref, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_ack(), ast_channel::bridge, agent_pvt::chan, ast_party_caller::id, agent_pvt::lock, agent_pvt::loginstart, agent_pvt::name, ast_party_id::number, agent_pvt::owner, S_COR, S_OR, status, ast_party_number::str, and ast_party_number::valid.

Referenced by load_module().

{
   const char *id = astman_get_header(m,"ActionID");
   char idText[256] = "";
   struct agent_pvt *p;
   char *username = NULL;
   char *loginChan = NULL;
   char *talkingto = NULL;
   char *talkingtoChan = NULL;
   char *status = NULL;
   struct ast_channel *bridge;

   if (!ast_strlen_zero(id))
      snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
   astman_send_ack(s, m, "Agents will follow");
   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      struct ast_channel *owner;
      ast_mutex_lock(&p->lock);
      owner = agent_lock_owner(p);

      /* Status Values:
         AGENT_LOGGEDOFF - Agent isn't logged in
         AGENT_IDLE      - Agent is logged in, and waiting for call
         AGENT_ONCALL    - Agent is logged in, and on a call
         AGENT_UNKNOWN   - Don't know anything about agent. Shouldn't ever get this. */

      username = S_OR(p->name, "None");

      /* Set a default status. It 'should' get changed. */
      status = "AGENT_UNKNOWN";

      if (p->chan) {
         loginChan = ast_strdupa(ast_channel_name(p->chan));
         if (owner && ast_channel_internal_bridged_channel(owner)) {
            talkingto = S_COR(ast_channel_caller(p->chan)->id.number.valid,
               ast_channel_caller(p->chan)->id.number.str, "n/a");
            if ((bridge = ast_bridged_channel(owner))) {
               talkingtoChan = ast_strdupa(ast_channel_name(bridge));
            } else {
               talkingtoChan = "n/a";
            }
            status = "AGENT_ONCALL";
         } else {
            talkingto = "n/a";
            talkingtoChan = "n/a";
            status = "AGENT_IDLE";
         }
      } else {
         loginChan = "n/a";
         talkingto = "n/a";
         talkingtoChan = "n/a";
         status = "AGENT_LOGGEDOFF";
      }

      if (owner) {
         ast_channel_unlock(owner);
         owner = ast_channel_unref(owner);
      }

      astman_append(s, "Event: Agents\r\n"
         "Agent: %s\r\n"
         "Name: %s\r\n"
         "Status: %s\r\n"
         "LoggedInChan: %s\r\n"
         "LoggedInTime: %d\r\n"
         "TalkingTo: %s\r\n"
         "TalkingToChan: %s\r\n"
         "%s"
         "\r\n",
         p->agent, username, status, loginChan, (int)p->loginstart, talkingto, talkingtoChan, idText);
      ast_mutex_unlock(&p->lock);
   }
   AST_LIST_UNLOCK(&agents);
   astman_append(s, "Event: AgentsComplete\r\n"
      "%s"
      "\r\n",idText);
   return 0;
}
static struct agent_pvt* add_agent ( const char *  agent,
int  pending 
) [static, read]

Adds an agent to the global list of agents.

Parameters:
agentA string with the username, password and real name of an agent. As defined in agents.conf. Example: "13,169,John Smith"
pendingIf it is pending or not.
Returns:
The just created agent.
See also:
agent_pvt, agents.

Definition at line 433 of file chan_agent.c.

References agent_pvt::acceptdtmf, agent_pvt::ackcall, agent_pvt::agent, AGENT_FLAG_ACCEPTDTMF, AGENT_FLAG_ACKCALL, AGENT_FLAG_AUTOLOGOFF, AGENT_FLAG_ENDDTMF, AGENT_FLAG_WRAPUPTIME, agent_pvt::app_complete_cond, agent_pvt::app_lock_flag, agent_pvt::app_sleep_cond, args, AST_APP_ARG, ast_calloc, ast_cond_init, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_mutex_init, AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_test_flag, ast_tvnow(), agent_pvt::autologoff, agent_pvt::dead, agent_pvt::enddtmf, agent_pvt::group, agent_pvt::lastdisc, agent_pvt::lock, LOG_WARNING, agent_pvt::login_wait_cond, agent_pvt::moh, agent_pvt::name, parse(), agent_pvt::password, agent_pvt::pending, and agent_pvt::wrapuptime.

Referenced by agent_request(), and read_agent_config().

{
   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_STANDARD_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++;
   }

   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) {
      // 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_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);
   }
   
   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;
      }
   }
   p->wrapuptime = wrapuptime;

   if (pending)
      p->dead = 1;
   else
      p->dead = 0;
   return p;
}
static int agent_ack_sleep ( struct agent_pvt p) [static]

Definition at line 1003 of file chan_agent.c.

References agent_pvt::acceptdtmf, agent_pvt::app_sleep_cond, AST_FRAME_DTMF, ast_frfree, ast_mutex_lock, ast_mutex_unlock, ast_read(), ast_remaining_ms(), ast_tvdiff_ms(), ast_tvnow(), ast_waitfor(), agent_pvt::chan, f, ast_frame::frametype, ast_frame_subclass::integer, agent_pvt::lastdisc, agent_pvt::lock, and ast_frame::subclass.

Referenced by login_exec().

{
   int digit;
   int to = 1000;
   struct ast_frame *f;
   struct timeval start = ast_tvnow();
   int ms;

   /* Wait a second and look for something */
   while ((ms = ast_remaining_ms(start, to))) {
      ms = ast_waitfor(p->chan, ms);
      if (ms < 0) {
         return -1;
      }
      if (ms == 0) {
         return 0;
      }
      f = ast_read(p->chan);
      if (!f) {
         return -1;
      }
      if (f->frametype == AST_FRAME_DTMF) {
         digit = f->subclass.integer;
      } else {
         digit = 0;
      }
      ast_frfree(f);
      ast_mutex_lock(&p->lock);
      if (!p->app_sleep_cond) {
         ast_mutex_unlock(&p->lock);
         return 0;
      }
      if (digit == p->acceptdtmf) {
         ast_mutex_unlock(&p->lock);
         return 1;
      }
      if (p->lastdisc.tv_sec) {
         if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
            ast_mutex_unlock(&p->lock);
            return 0;
         }
      }
      ast_mutex_unlock(&p->lock);
   }
   return 0;
}
static int agent_answer ( struct ast_channel ast) [static]

Definition at line 568 of file chan_agent.c.

References ast_log(), and LOG_WARNING.

{
   ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
   return -1;
}
static struct ast_channel * agent_bridgedchannel ( struct ast_channel chan,
struct ast_channel bridge 
) [static, read]

Definition at line 1050 of file chan_agent.c.

References ast_channel_internal_bridged_channel(), ast_channel_name(), ast_channel_tech_pvt(), ast_debug, and agent_pvt::chan.

{
   struct agent_pvt *p = ast_channel_tech_pvt(bridge);
   struct ast_channel *ret = NULL;

   if (p) {
      if (chan == p->chan)
         ret = ast_channel_internal_bridged_channel(bridge);
      else if (chan == ast_channel_internal_bridged_channel(bridge))
         ret = p->chan;
   }

   ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning '%s'\n", ast_channel_name(chan), ast_channel_name(bridge), ret ? ast_channel_name(ret) : "<none>");
   return ret;
}
static int agent_call ( struct ast_channel ast,
const char *  dest,
int  timeout 
) [static]

Definition at line 831 of file chan_agent.c.

References agent_pvt::ackcall, agent_pvt::acknowledged, agent_pvt::agent, agent_start_monitoring(), ast_assert, ast_channel_language(), ast_channel_name(), ast_channel_nativeformats(), ast_channel_tech_pvt(), ast_debug, ast_getformatname(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_set_read_format_from_cap(), ast_set_write_format_from_cap(), ast_setstate(), AST_STATE_DIALING, AST_STATE_RINGING, AST_STATE_UP, ast_streamfile(), ast_verb, ast_waitstream(), agent_pvt::chan, CLEANUP, agent_pvt::lock, LOG_DEBUG, LOG_WARNING, and agent_pvt::pending.

{
   struct agent_pvt *p = ast_channel_tech_pvt(ast);
   int res;
   int newstate=0;

   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);
   if (!res) {
      res = ast_waitstream(p->chan, "");
      ast_debug(3, "Waited for stream, result '%d'\n", res);
   }
   
   ast_mutex_lock(&p->lock);

   if (!res) {
      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);
      if (res)
         ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
   }

   if (!res) {
      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);
      if (res)
         ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
   }
   if(!res) {
      /* Call is immediately up, or might need ack */
      if (p->ackcall) {
         newstate = AST_STATE_RINGING;
      } else {
         newstate = AST_STATE_UP;
         if (recordagentcalls)
            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;
}
static int agent_cleanup ( struct agent_pvt p) [static]

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.
Parameters:
pAgent to be deleted.
Returns:
Always 0.

Definition at line 544 of file chan_agent.c.

References agent_pvt_destroy(), agent_pvt::app_complete_cond, agent_pvt::app_lock_flag, agent_pvt::app_sleep_cond, ast_channel_release(), ast_channel_tech_pvt_set(), ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, agent_pvt::chan, agent_pvt::dead, agent_pvt::lock, and agent_pvt::owner.

Referenced by check_availability().

{
   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_sleep_cond = 1;
   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);
   }
   if (p->dead) {
      ast_mutex_unlock(&p->lock);
      agent_pvt_destroy(p);
   } else {
      ast_mutex_unlock(&p->lock);
   }
   return 0;
}
static int agent_cont_sleep ( void *  data) [static]

Definition at line 980 of file chan_agent.c.

References agent_pvt::app_sleep_cond, ast_debug, ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_ms(), ast_tvnow(), agent_pvt::lastdisc, and agent_pvt::lock.

Referenced by login_exec().

{
   struct agent_pvt *p;
   int res;

   p = (struct agent_pvt *) data;

   ast_mutex_lock(&p->lock);
   res = p->app_sleep_cond;
   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);
   }

   return res;
}
static int agent_devicestate ( const char *  data) [static]

Part of PBX channel interface.

Definition at line 2294 of file chan_agent.c.

References agent_pvt::agent, AST_DEVICE_BUSY, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, AST_DEVICE_UNAVAILABLE, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, agent_pvt::chan, agent_pvt::deferlogoff, agent_pvt::lastdisc, agent_pvt::lock, agent_pvt::owner, and agent_pvt::pending.

{
   struct agent_pvt *p;
   const char *device = data;
   int res = AST_DEVICE_INVALID;

   if (device[0] == '@' || device[0] == ':') {
      /* Device state of groups not supported. */
      return AST_DEVICE_INVALID;
   }

   /* Want device state of a specific agent. */
   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      ast_mutex_lock(&p->lock);
      if (!p->pending && !strcmp(device, p->agent)) {
         if (p->owner) {
            res = AST_DEVICE_BUSY;
         } else if (p->chan) {
            if (p->lastdisc.tv_sec || p->deferlogoff) {
               /* Agent is in wrapup time so unavailable for another call. */
               res = AST_DEVICE_INUSE;
            } else {
               res = AST_DEVICE_NOT_INUSE;
            }
         } else {
            res = AST_DEVICE_UNAVAILABLE;
         }
         ast_mutex_unlock(&p->lock);
         break;
      }
      ast_mutex_unlock(&p->lock);
   }
   AST_LIST_UNLOCK(&agents);
   return res;
}
static int agent_digit_begin ( struct ast_channel ast,
char  digit 
) [static]
static int agent_digit_end ( struct ast_channel ast,
char  digit,
unsigned int  duration 
) [static]

Definition at line 820 of file chan_agent.c.

References ast_channel_tech_pvt(), ast_mutex_lock, ast_mutex_unlock, ast_senddigit_end(), agent_pvt::chan, and agent_pvt::lock.

{
   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);
   return 0;
}
static int agent_fixup ( struct ast_channel oldchan,
struct ast_channel newchan 
) [static]

Definition at line 773 of file chan_agent.c.

References ast_channel_tech_pvt(), ast_log(), ast_mutex_lock, ast_mutex_unlock, agent_pvt::lock, LOG_WARNING, and agent_pvt::owner.

{
   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);
      return -1;
   }
   p->owner = newchan;
   ast_mutex_unlock(&p->lock);
   return 0;
}
struct ast_channel * agent_get_base_channel ( struct ast_channel chan) [static, read]

return the channel or base channel if one exists. This function assumes the channel it is called on is already locked

Definition at line 896 of file chan_agent.c.

References ast_channel_tech_pvt(), ast_log(), agent_pvt::chan, and LOG_ERROR.

{
   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);
      return NULL;
   }
   p = ast_channel_tech_pvt(chan);
   if (p->chan) 
      base = p->chan;
   return base;
}
static int agent_hangup ( struct ast_channel ast) [static]

Definition at line 912 of file chan_agent.c.

References agent_pvt::abouttograb, agent_pvt::acknowledged, agent_pvt_destroy(), agent_pvt::app_complete_cond, agent_pvt::app_lock_flag, agent_pvt::app_sleep_cond, ast_channel_internal_bridged_channel_set(), ast_channel_ref, ast_channel_tech_pvt(), ast_channel_tech_pvt_set(), ast_channel_unref, ast_cond_signal, AST_CONTROL_HOLD, ast_debug, ast_indicate_data(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_samp2tv(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, ast_state2str(), ast_strlen_zero(), ast_tvadd(), ast_tvnow(), agent_pvt::chan, agent_pvt::dead, agent_pvt::lastdisc, agent_pvt::lock, agent_pvt::loginstart, agent_pvt::moh, agent_pvt::owner, agent_pvt::pending, S_OR, agent_pvt::start, and agent_pvt::wrapuptime.

{
   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);
   p->owner = NULL;
   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)));
   p->start = 0;
   if (p->chan) {
      ast_channel_internal_bridged_channel_set(p->chan, NULL);
      /* If they're dead, go ahead and hang up on the agent now */
      if (p->dead) {
         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
      } else if (p->loginstart) {
         indicate_chan = ast_channel_ref(p->chan);
         tmp_moh = ast_strdupa(p->moh);
      }
   }
   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);
      agent_pvt_destroy(p);
      return 0;
   } else {
      /* 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);
   return 0;
}
static int agent_indicate ( struct ast_channel ast,
int  condition,
const void *  data,
size_t  datalen 
) [static]
static struct ast_channel* agent_lock_owner ( struct agent_pvt pvt) [static, read]

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.

Parameters:
pvtPointer to the LOCKED agent_pvt for which the owner is needed locked channel which owns the pvt at the time of completion. NULL if not available.

Definition at line 382 of file chan_agent.c.

References ast_channel_lock, ast_channel_ref, ast_channel_unlock, ast_channel_unref, ast_mutex_lock, ast_mutex_unlock, agent_pvt::lock, and agent_pvt::owner.

Referenced by action_agents(), agent_logoff(), agent_read(), agents_data_provider_get(), agents_show(), and agents_show_online().

{
   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;
      }
   }
}
static int agent_logoff ( const char *  agent,
int  soft 
) [static]
static char* agent_logoff_cmd ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 1628 of file chan_agent.c.

References agent_pvt::agent, agent_logoff(), ast_cli_args::argc, ast_cli_args::argv, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, complete_agent_logoff_cmd(), ast_cli_args::fd, ast_cli_args::line, ast_cli_args::n, ast_cli_args::pos, ast_cli_entry::usage, and ast_cli_args::word.

{
   int ret;
   const char *agent;

   switch (cmd) {
   case CLI_INIT:
      e->command = "agent logoff";
      e->usage =
         "Usage: agent logoff <channel> [soft]\n"
         "       Sets an agent as no longer logged in.\n"
         "       If 'soft' is specified, do not hangup existing calls.\n";
      return NULL;
   case CLI_GENERATE:
      return complete_agent_logoff_cmd(a->line, a->word, a->pos, a->n); 
   }

   if (a->argc < 3 || a->argc > 4)
      return CLI_SHOWUSAGE;
   if (a->argc == 4 && strcasecmp(a->argv[3], "soft"))
      return CLI_SHOWUSAGE;

   agent = a->argv[2] + 6;
   ret = agent_logoff(agent, a->argc == 4);
   if (ret == 0)
      ast_cli(a->fd, "Logging out %s\n", agent);

   return CLI_SUCCESS;
}
static struct ast_channel* agent_new ( struct agent_pvt p,
int  state,
const char *  linkedid,
struct ast_callid callid 
) [static, read]

Create new agent channel.

Definition at line 1067 of file chan_agent.c.

References agent_pvt::agent, agent_tech, ast_channel_alloc(), ast_channel_callid_set(), ast_channel_context(), ast_channel_context_set(), ast_channel_exten(), ast_channel_exten_set(), ast_channel_language(), ast_channel_nativeformats(), ast_channel_priority_set(), ast_channel_rawreadformat(), ast_channel_rawwriteformat(), ast_channel_readformat(), ast_channel_tech_pvt_set(), ast_channel_tech_set(), ast_channel_writeformat(), ast_format_cap_add(), ast_format_cap_copy(), ast_format_copy(), ast_format_set(), AST_FORMAT_SLINEAR, ast_log(), ast_random(), agent_pvt::chan, LOG_WARNING, agent_pvt::owner, and agent_pvt::pending.

Referenced by agent_request(), and check_availability().

{
   struct ast_channel *tmp;
#if 0
   if (!p->chan) {
      ast_log(LOG_WARNING, "No channel? :(\n");
      return NULL;
   }
#endif   
   if (p->pending)
      tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? ast_channel_exten(p->chan):"", p->chan ? ast_channel_context(p->chan):"", linkedid, 0, "Agent/P%s-%d", p->agent, (int) ast_random() & 0xffff);
   else
      tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? ast_channel_exten(p->chan):"", p->chan ? ast_channel_context(p->chan):"", linkedid, 0, "Agent/%s", p->agent);
   if (!tmp) {
      ast_log(LOG_WARNING, "Unable to allocate agent channel structure\n");
      return NULL;
   }

   if (callid) {
      ast_channel_callid_set(tmp, callid);
   }

   ast_channel_tech_set(tmp, &agent_tech);
   if (p->chan) {
      ast_format_cap_copy(ast_channel_nativeformats(tmp), ast_channel_nativeformats(p->chan));
      ast_format_copy(ast_channel_writeformat(tmp), ast_channel_writeformat(p->chan));
      ast_format_copy(ast_channel_rawwriteformat(tmp), ast_channel_writeformat(p->chan));
      ast_format_copy(ast_channel_readformat(tmp), ast_channel_readformat(p->chan));
      ast_format_copy(ast_channel_rawreadformat(tmp), ast_channel_readformat(p->chan));
      ast_channel_language_set(tmp, ast_channel_language(p->chan));
      ast_channel_context_set(tmp, ast_channel_context(p->chan));
      ast_channel_exten_set(tmp, ast_channel_exten(p->chan));
      /* XXX Is this really all we copy form the originating channel?? */
   } else {
      ast_format_set(ast_channel_writeformat(tmp), AST_FORMAT_SLINEAR, 0);
      ast_format_set(ast_channel_rawwriteformat(tmp), AST_FORMAT_SLINEAR, 0);
      ast_format_set(ast_channel_readformat(tmp), AST_FORMAT_SLINEAR, 0);
      ast_format_set(ast_channel_rawreadformat(tmp), AST_FORMAT_SLINEAR, 0);
      ast_format_cap_add(ast_channel_nativeformats(tmp), ast_channel_writeformat(tmp));
   }
   /* Safe, agentlock already held */
   ast_channel_tech_pvt_set(tmp, p);
   p->owner = tmp;
   ast_channel_priority_set(tmp, 1);
   return tmp;
}
static struct ast_frame * agent_read ( struct ast_channel ast) [static, read]

Definition at line 607 of file chan_agent.c.

References agent_pvt::acceptdtmf, agent_pvt::ackcall, agent_pvt::acknowledged, agent_lock_owner(), agent_start_monitoring(), AST_AGENT_FD, ast_channel_fdno(), ast_channel_fdno_set(), ast_channel_flags(), ast_channel_internal_bridged_channel(), ast_channel_internal_bridged_channel_set(), ast_channel_name(), ast_channel_tech(), ast_channel_tech_pvt(), ast_channel_trylock, ast_channel_unlock, ast_channel_unref, AST_CONTROL_ANSWER, ast_copy_flags, ast_debug, AST_FLAG_EXCEPTION, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_null_frame, ast_read(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, AST_STATE_UP, AST_TIMING_FD, ast_verb, agent_pvt::autologoff, agent_pvt::chan, CHECK_FORMATS, CLEANUP, DEADLOCK_AVOIDANCE, agent_pvt::enddtmf, f, ast_frame::frametype, ast_frame_subclass::integer, agent_pvt::lock, LOG_NOTICE, agent_pvt::name, agent_pvt::owner, agent_pvt::start, ast_frame::subclass, and ast_channel_tech::type.

{
   struct agent_pvt *p = ast_channel_tech_pvt(ast);
   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));
      f = ast_read(p->chan);
      ast_channel_fdno_set(ast, -1);
   } else
      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) {
            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;
      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;
      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);
   }

   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)));
      }
   }
   ast_mutex_unlock(&p->lock);
   if (recordagentcalls && f == &answer_frame)
      agent_start_monitoring(ast,0);
   return f;
}
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, read]

Part of the Asterisk PBX interface.

Definition at line 1378 of file chan_agent.c.

References add_agent(), agent_pvt::agent, agent_new(), agent_pvt_destroy(), agent_pvt::app_lock_flag, agent_pvt::app_sleep_cond, ast_callid_unref, AST_CAUSE_BUSY, AST_CAUSE_UNREGISTERED, ast_channel_linkedid(), ast_cond_wait, AST_CONTROL_UNHOLD, ast_debug, ast_hangup(), ast_indicate(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_null_frame, ast_queue_frame(), ast_read_threadstorage_callid(), AST_STATE_DOWN, ast_tv(), ast_tvdiff_ms(), ast_tvnow(), agent_pvt::chan, agent_pvt::group, agent_pvt::lastdisc, agent_pvt::lock, agent_pvt::login_wait_cond, agent_pvt::loginstart, agent_pvt::owner, and agent_pvt::pending.

{
   struct agent_pvt *p;
   struct ast_channel *chan = NULL;
   const char *s;
   ast_group_t groupmatch;
   int groupoff;
   int waitforagent=0;
   int hasagent = 0;
   struct timeval now;
   struct ast_callid *callid = ast_read_threadstorage_callid();

   s = data;
   if ((s[0] == '@') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
      groupmatch = (1 << groupoff);
   } else if ((s[0] == ':') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
      groupmatch = (1 << groupoff);
      waitforagent = 1;
   } else 
      groupmatch = 0;

   /* Check actual logged in agents first */
   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      ast_mutex_lock(&p->lock);
      if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
         if (p->chan) {
            hasagent++;
         }
         now = ast_tvnow();
         if (p->loginstart
            && (!p->lastdisc.tv_sec || ast_tvdiff_ms(now, p->lastdisc) > 0)) {
            p->lastdisc = ast_tv(0, 0);
            /* Agent must be registered, but not have any active call, and not be in a waiting state */
            if (!p->owner && p->chan) {
               /* Fixed agent */
               chan = agent_new(p, AST_STATE_DOWN, requestor ? ast_channel_linkedid(requestor) : NULL, callid);
            }
            if (chan) {
               ast_mutex_unlock(&p->lock);
               break;
            }
         }
      }
      ast_mutex_unlock(&p->lock);
   }

   if (!chan && waitforagent) {
      /* No agent available -- but we're requesting to wait for one.
         Allocate a place holder */
      if (hasagent) {
         ast_debug(1, "Creating place holder for '%s'\n", s);
         p = add_agent(data, 1);
         if (p) {
            p->group = groupmatch;
            chan = agent_new(p, AST_STATE_DOWN, requestor ? ast_channel_linkedid(requestor) : NULL, callid);
            if (!chan) {
               AST_LIST_REMOVE(&agents, p, list);
               agent_pvt_destroy(p);
            }
         }
      } else {
         ast_debug(1, "Not creating place holder for '%s' since nobody logged in\n", s);
      }
   }
   *cause = hasagent ? AST_CAUSE_BUSY : AST_CAUSE_UNREGISTERED;
   AST_LIST_UNLOCK(&agents);

   if (callid) {
      callid = ast_callid_unref(callid);
   }

   if (chan) {
      ast_mutex_lock(&p->lock);
      if (p->pending) {
         ast_mutex_unlock(&p->lock);
         return chan;
      }

      if (!p->chan) {
         ast_debug(1, "Agent disconnected before we could connect the call\n");
         ast_mutex_unlock(&p->lock);
         ast_hangup(chan);
         *cause = AST_CAUSE_UNREGISTERED;
         return NULL;
      }

      /* we need to take control of the channel from the login app
       * thread */
      p->app_sleep_cond = 0;
      p->app_lock_flag = 1;
      ast_queue_frame(p->chan, &ast_null_frame);
      ast_cond_wait(&p->login_wait_cond, &p->lock);

      if (!p->chan) {
         ast_debug(1, "Agent disconnected while we were connecting the call\n");
         ast_mutex_unlock(&p->lock);
         ast_hangup(chan);
         *cause = AST_CAUSE_UNREGISTERED;
         return NULL;
      }

      ast_indicate(p->chan, AST_CONTROL_UNHOLD);
      ast_mutex_unlock(&p->lock);
   }

   return chan;
}
static int agent_sendhtml ( struct ast_channel ast,
int  subclass,
const char *  data,
int  datalen 
) [static]

Definition at line 726 of file chan_agent.c.

References ast_channel_sendhtml(), ast_channel_tech_pvt(), ast_mutex_lock, ast_mutex_unlock, agent_pvt::chan, and agent_pvt::lock.

{
   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 
) [static]

Definition at line 737 of file chan_agent.c.

References ast_channel_tech_pvt(), ast_mutex_lock, ast_mutex_unlock, ast_sendtext(), agent_pvt::chan, and agent_pvt::lock.

{
   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_start_monitoring ( struct ast_channel ast,
int  needlock 
) [static]

Definition at line 602 of file chan_agent.c.

References __agent_start_monitoring(), and ast_channel_tech_pvt().

Referenced by agent_call(), and agent_read().

{
   return __agent_start_monitoring(ast, ast_channel_tech_pvt(ast), needlock);
}
static int agent_write ( struct ast_channel ast,
struct ast_frame f 
) [static]
static int agentmonitoroutgoing_exec ( struct ast_channel chan,
const char *  data 
) [static]

Called by the AgentMonitorOutgoing application (from the dial plan).

Parameters:
chan
data
Returns:
See also:
login_exec(), load_module().

Definition at line 2241 of file chan_agent.c.

References __agent_start_monitoring(), agent_pvt::agent, ast_channel_caller(), ast_channel_cdr(), ast_copy_string(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), AST_MAX_AGENT, AST_MAX_BUF, ast_strlen_zero(), GETAGENTBYCALLERID, LOG_WARNING, and pbx_builtin_getvar_helper().

Referenced by load_module().

{
   int exitifnoagentid = 0;
   int nowarnings = 0;
   int changeoutgoing = 0;
   int res = 0;
   char agent[AST_MAX_AGENT];

   if (data) {
      if (strchr(data, 'd'))
         exitifnoagentid = 1;
      if (strchr(data, 'n'))
         nowarnings = 1;
      if (strchr(data, 'c'))
         changeoutgoing = 1;
   }
   if (ast_channel_caller(chan)->id.number.valid
      && !ast_strlen_zero(ast_channel_caller(chan)->id.number.str)) {
      const char *tmp;
      char agentvar[AST_MAX_BUF];
      snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID,
         ast_channel_caller(chan)->id.number.str);
      if ((tmp = pbx_builtin_getvar_helper(NULL, agentvar))) {
         struct agent_pvt *p;
         ast_copy_string(agent, tmp, sizeof(agent));
         AST_LIST_LOCK(&agents);
         AST_LIST_TRAVERSE(&agents, p, list) {
            if (!strcasecmp(p->agent, tmp)) {
               if (changeoutgoing) snprintf(ast_channel_cdr(chan)->channel, sizeof(ast_channel_cdr(chan)->channel), "Agent/%s", p->agent);
               __agent_start_monitoring(chan, p, 1);
               break;
            }
         }
         AST_LIST_UNLOCK(&agents);
         
      } else {
         res = -1;
         if (!nowarnings)
            ast_log(LOG_WARNING, "Couldn't find the global variable %s, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n", agentvar);
      }
   } else {
      res = -1;
      if (!nowarnings)
         ast_log(LOG_WARNING, "There is no callerid on that call, so I can't figure out which agent (if it's an agent) is placing outgoing call.\n");
   }
   if (res) {
      if (exitifnoagentid)
         return res;
   }
   return 0;
}
static int agents_data_provider_get ( const struct ast_data_search search,
struct ast_data data_root 
) [static]

Definition at line 2425 of file chan_agent.c.

References agent_pvt::agent, agent_lock_owner(), ast_bridged_channel(), ast_channel_data_add_structure(), ast_channel_unlock, ast_channel_unref, ast_data_add_bool(), ast_data_add_node(), ast_data_add_str(), ast_data_add_structure, ast_data_remove_node(), ast_data_search_match(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, agent_pvt::chan, agent_pvt::lock, agent_pvt::moh, agent_pvt::owner, and agent_pvt::pending.

{
   struct agent_pvt *p;
   struct ast_data *data_agent, *data_channel, *data_talkingto;

   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      struct ast_channel *owner;

      data_agent = ast_data_add_node(data_root, "agent");
      if (!data_agent) {
         continue;
      }

      ast_mutex_lock(&p->lock);
      owner = agent_lock_owner(p);

      if (!(p->pending)) {
         ast_data_add_str(data_agent, "id", p->agent);
         ast_data_add_structure(agent_pvt, data_agent, p);

         ast_data_add_bool(data_agent, "logged", p->chan ? 1 : 0);
         if (p->chan) {
            data_channel = ast_data_add_node(data_agent, "loggedon");
            if (!data_channel) {
               ast_mutex_unlock(&p->lock);
               ast_data_remove_node(data_root, data_agent);
               if (owner) {
                  ast_channel_unlock(owner);
                  owner = ast_channel_unref(owner);
               }
               continue;
            }
            ast_channel_data_add_structure(data_channel, p->chan, 0);
            if (owner && ast_bridged_channel(owner)) {
               data_talkingto = ast_data_add_node(data_agent, "talkingto");
               if (!data_talkingto) {
                  ast_mutex_unlock(&p->lock);
                  ast_data_remove_node(data_root, data_agent);
                  if (owner) {
                     ast_channel_unlock(owner);
                     owner = ast_channel_unref(owner);
                  }
                  continue;
               }
               ast_channel_data_add_structure(data_talkingto, ast_bridged_channel(owner), 0);
            }
         } else {
            ast_data_add_node(data_agent, "talkingto");
            ast_data_add_node(data_agent, "loggedon");
         }
         ast_data_add_str(data_agent, "musiconhold", p->moh);
      }

      if (owner) {
         ast_channel_unlock(owner);
         owner = ast_channel_unref(owner);
      }

      ast_mutex_unlock(&p->lock);

      /* if this agent doesn't match remove the added agent. */
      if (!ast_data_search_match(search, data_agent)) {
         ast_data_remove_node(data_root, data_agent);
      }
   }
   AST_LIST_UNLOCK(&agents);

   return 0;
}
static char* agents_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Show agents in cli.

< Number of agents configured

< Number of online agents

< Number of offline agents

Definition at line 1715 of file chan_agent.c.

References agent_pvt::agent, agent_lock_owner(), ast_cli_args::argc, ast_bridged_channel(), ast_channel_name(), ast_channel_unlock, ast_channel_unref, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, AST_MAX_BUF, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), agent_pvt::chan, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, count_agents(), ast_cli_args::fd, agent_pvt::group, agent_pvt::lock, agent_pvt::moh, agent_pvt::name, agent_pvt::owner, agent_pvt::pending, powerof(), and ast_cli_entry::usage.

{
   struct agent_pvt *p;
   char username[AST_MAX_BUF];
   char location[AST_MAX_BUF] = "";
   char talkingto[AST_MAX_BUF] = "";
   char music[AST_MAX_BUF];
   int count_agents = 0;      /*!< Number of agents configured */
   int online_agents = 0;     /*!< Number of online agents */
   int offline_agents = 0;    /*!< Number of offline agents */

   switch (cmd) {
   case CLI_INIT:
      e->command = "agent show";
      e->usage =
         "Usage: agent show\n"
         "       Provides summary information on agents.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 2)
      return CLI_SHOWUSAGE;

   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      struct ast_channel *owner;
      ast_mutex_lock(&p->lock);
      owner = agent_lock_owner(p);
      if (p->pending) {
         if (p->group)
            ast_cli(a->fd, "-- Pending call to group %d\n", powerof(p->group));
         else
            ast_cli(a->fd, "-- Pending call to agent %s\n", p->agent);
      } else {
         if (!ast_strlen_zero(p->name))
            snprintf(username, sizeof(username), "(%s) ", p->name);
         else
            username[0] = '\0';
         if (p->chan) {
            snprintf(location, sizeof(location), "logged in on %s", ast_channel_name(p->chan));
            if (owner && ast_bridged_channel(owner)) {
               snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_channel_name(ast_bridged_channel(p->owner)));
            } else {
               strcpy(talkingto, " is idle");
            }
            online_agents++;
         } else {
            strcpy(location, "not logged in");
            talkingto[0] = '\0';
            offline_agents++;
         }
         if (!ast_strlen_zero(p->moh))
            snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
         ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent, 
            username, location, talkingto, music);
         count_agents++;
      }

      if (owner) {
         ast_channel_unlock(owner);
         owner = ast_channel_unref(owner);
      }
      ast_mutex_unlock(&p->lock);
   }
   AST_LIST_UNLOCK(&agents);
   if ( !count_agents ) 
      ast_cli(a->fd, "No Agents are configured in %s\n",config);
   else 
      ast_cli(a->fd, "%d agents configured [%d online , %d offline]\n",count_agents, online_agents, offline_agents);
   ast_cli(a->fd, "\n");
                   
   return CLI_SUCCESS;
}
static char* agents_show_online ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
) [static]

Definition at line 1792 of file chan_agent.c.

References agent_pvt::agent, agent_lock_owner(), ast_cli_args::argc, ast_bridged_channel(), ast_channel_name(), ast_channel_unlock, ast_channel_unref, ast_cli(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, AST_MAX_BUF, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), agent_pvt::chan, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, count_agents(), ast_cli_args::fd, agent_pvt::lock, agent_pvt::moh, agent_pvt::name, agent_pvt::owner, and ast_cli_entry::usage.

{
   struct agent_pvt *p;
   char username[AST_MAX_BUF];
   char location[AST_MAX_BUF] = "";
   char talkingto[AST_MAX_BUF] = "";
   char music[AST_MAX_BUF];
   int count_agents = 0;           /* Number of agents configured */
   int online_agents = 0;          /* Number of online agents */
   int agent_status = 0;           /* 0 means offline, 1 means online */

   switch (cmd) {
   case CLI_INIT:
      e->command = "agent show online";
      e->usage =
         "Usage: agent show online\n"
         "       Provides a list of all online agents.\n";
      return NULL;
   case CLI_GENERATE:
      return NULL;
   }

   if (a->argc != 3)
      return CLI_SHOWUSAGE;

   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      struct ast_channel *owner;

      agent_status = 0;       /* reset it to offline */
      ast_mutex_lock(&p->lock);
      owner = agent_lock_owner(p);

      if (!ast_strlen_zero(p->name))
         snprintf(username, sizeof(username), "(%s) ", p->name);
      else
         username[0] = '\0';
      if (p->chan) {
         snprintf(location, sizeof(location), "logged in on %s", ast_channel_name(p->chan));
         if (p->owner && ast_bridged_channel(p->owner)) {
            snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_channel_name(ast_bridged_channel(p->owner)));
         } else {
            strcpy(talkingto, " is idle");
         }
         agent_status = 1;
         online_agents++;
      }

      if (owner) {
         ast_channel_unlock(owner);
         owner = ast_channel_unref(owner);
      }

      if (!ast_strlen_zero(p->moh))
         snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
      if (agent_status)
         ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent, username, location, talkingto, music);
      count_agents++;
      ast_mutex_unlock(&p->lock);
   }
   AST_LIST_UNLOCK(&agents);
   if (!count_agents) 
      ast_cli(a->fd, "No Agents are configured in %s\n", config);
   else
      ast_cli(a->fd, "%d agents online\n", online_agents);
   ast_cli(a->fd, "\n");
   return CLI_SUCCESS;
}
static int check_availability ( struct agent_pvt newlyavailable,
int  needlock 
) [static]

Definition at line 1278 of file chan_agent.c.

References agent_pvt::abouttograb, agent_pvt::ackcall, agent_pvt::acknowledged, agent_pvt::agent, agent_cleanup(), agent_new(), ast_channel_context(), ast_channel_context_set(), ast_channel_language(), ast_channel_linkedid(), ast_channel_masquerade(), ast_channel_name(), ast_debug, ast_hangup(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_setstate(), AST_STATE_DOWN, AST_STATE_UP, ast_streamfile(), ast_waitstream(), agent_pvt::chan, agent_pvt::group, agent_pvt::lock, agent_pvt::owner, and agent_pvt::pending.

Referenced by login_exec().

{
   struct ast_channel *chan=NULL, *parent=NULL;
   struct agent_pvt *p;
   int res;

   ast_debug(1, "Checking availability of '%s'\n", newlyavailable->agent);
   if (needlock)
      AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      if (p == newlyavailable) {
         continue;
      }
      ast_mutex_lock(&p->lock);
      if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
         ast_debug(1, "Call '%s' looks like a winner for agent '%s'\n", ast_channel_name(p->owner), newlyavailable->agent);
         /* We found a pending call, time to merge */
         chan = agent_new(newlyavailable, AST_STATE_DOWN, p->owner ? ast_channel_linkedid(p->owner) : NULL, NULL);
         parent = p->owner;
         p->abouttograb = 1;
         ast_mutex_unlock(&p->lock);
         break;
      }
      ast_mutex_unlock(&p->lock);
   }
   if (needlock)
      AST_LIST_UNLOCK(&agents);
   if (parent && chan)  {
      if (newlyavailable->ackcall) {
         /* Don't do beep here */
         res = 0;
      } else {
         ast_debug(3, "Playing beep, lang '%s'\n", ast_channel_language(newlyavailable->chan));
         res = ast_streamfile(newlyavailable->chan, beep, ast_channel_language(newlyavailable->chan));
         ast_debug(3, "Played beep, result '%d'\n", res);
         if (!res) {
            res = ast_waitstream(newlyavailable->chan, "");
            ast_debug(1, "Waited for stream, result '%d'\n", res);
         }
      }
      if (!res) {
         /* Note -- parent may have disappeared */
         if (p->abouttograb) {
            newlyavailable->acknowledged = 1;
            /* Safe -- agent lock already held */
            ast_setstate(parent, AST_STATE_UP);
            ast_setstate(chan, AST_STATE_UP);
            ast_channel_context_set(parent, ast_channel_context(chan));
            ast_channel_masquerade(parent, chan);
            ast_hangup(chan);
            p->abouttograb = 0;
         } else {
            ast_debug(1, "Sneaky, parent disappeared in the mean time...\n");
            agent_cleanup(newlyavailable);
         }
      } else {
         ast_debug(1, "Ugh...  Agent hung up at exactly the wrong time\n");
         agent_cleanup(newlyavailable);
      }
   }
   return 0;
}
static int check_beep ( struct agent_pvt newlyavailable,
int  needlock 
) [static]

Definition at line 1341 of file chan_agent.c.

References agent_pvt::abouttograb, agent_pvt::agent, ast_channel_language(), ast_channel_name(), ast_debug, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_streamfile(), ast_waitstream(), agent_pvt::chan, agent_pvt::group, agent_pvt::lock, agent_pvt::owner, and agent_pvt::pending.

Referenced by login_exec().

{
   struct agent_pvt *p;
   int res=0;

   ast_debug(1, "Checking beep availability of '%s'\n", newlyavailable->agent);
   if (needlock)
      AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      if (p == newlyavailable) {
         continue;
      }
      ast_mutex_lock(&p->lock);
      if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
         ast_debug(1, "Call '%s' looks like a would-be winner for agent '%s'\n", ast_channel_name(p->owner), newlyavailable->agent);
         ast_mutex_unlock(&p->lock);
         break;
      }
      ast_mutex_unlock(&p->lock);
   }
   if (needlock)
      AST_LIST_UNLOCK(&agents);
   if (p) {
      ast_mutex_unlock(&newlyavailable->lock);
      ast_debug(3, "Playing beep, lang '%s'\n", ast_channel_language(newlyavailable->chan));
      res = ast_streamfile(newlyavailable->chan, beep, ast_channel_language(newlyavailable->chan));
      ast_debug(1, "Played beep, result '%d'\n", res);
      if (!res) {
         res = ast_waitstream(newlyavailable->chan, "");
         ast_debug(1, "Waited for stream, result '%d'\n", res);
      }
      ast_mutex_lock(&newlyavailable->lock);
   }
   return res;
}
static char * complete_agent_logoff_cmd ( const char *  line,
const char *  word,
int  pos,
int  state 
) [static]

Definition at line 1688 of file chan_agent.c.

References agent_pvt::agent, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, AST_MAX_AGENT, ast_strdup, len(), agent_pvt::loginstart, and agent_pvt::name.

Referenced by agent_logoff_cmd().

{
   char *ret = NULL;

   if (pos == 2) {
      struct agent_pvt *p;
      char name[AST_MAX_AGENT];
      int which = 0, len = strlen(word);

      AST_LIST_LOCK(&agents);
      AST_LIST_TRAVERSE(&agents, p, list) {
         snprintf(name, sizeof(name), "Agent/%s", p->agent);
         if (!strncasecmp(word, name, len) && p->loginstart && ++which > state) {
            ret = ast_strdup(name);
            break;
         }
      }
      AST_LIST_UNLOCK(&agents);
   } else if (pos == 3 && state == 0) 
      return ast_strdup("soft");
   
   return ret;
}
static struct agent_pvt* find_agent ( char *  agentid) [static, read]
Note:
This function expects the agent list to be locked

Definition at line 2334 of file chan_agent.c.

References agent_pvt::agent, and AST_LIST_TRAVERSE.

Referenced by function_agent().

{
   struct agent_pvt *cur;

   AST_LIST_TRAVERSE(&agents, cur, list) {
      if (!strcmp(cur->agent, agentid))
         break;   
   }

   return cur; 
}
static int function_agent ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 2346 of file chan_agent.c.

References agent_pvt::agent, args, AST_APP_ARG, ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_copy_string(), AST_DECLARE_APP_ARGS, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), AST_NONSTANDARD_APP_ARGS, ast_strlen_zero(), agent_pvt::chan, find_agent(), LOG_WARNING, agent_pvt::moh, agent_pvt::name, parse(), agent_pvt::password, and status.

{
   char *parse;    
   AST_DECLARE_APP_ARGS(args,
      AST_APP_ARG(agentid);
      AST_APP_ARG(item);
   );
   char *tmp;
   struct agent_pvt *agent;

   buf[0] = '\0';

   if (ast_strlen_zero(data)) {
      ast_log(LOG_WARNING, "The AGENT function requires an argument - agentid!\n");
      return -1;
   }

   parse = ast_strdupa(data);

   AST_NONSTANDARD_APP_ARGS(args, parse, ':');
   if (!args.item)
      args.item = "status";

   AST_LIST_LOCK(&agents);

   if (!(agent = find_agent(args.agentid))) {
      AST_LIST_UNLOCK(&agents);
      ast_log(LOG_WARNING, "Agent '%s' not found!\n", args.agentid);
      return -1;
   }

   if (!strcasecmp(args.item, "status")) {
      char *status = "LOGGEDOUT";
      if (agent->chan) {
         status = "LOGGEDIN";
      }
      ast_copy_string(buf, status, len);
   } else if (!strcasecmp(args.item, "password")) 
      ast_copy_string(buf, agent->password, len);
   else if (!strcasecmp(args.item, "name"))
      ast_copy_string(buf, agent->name, len);
   else if (!strcasecmp(args.item, "mohclass"))
      ast_copy_string(buf, agent->moh, len);
   else if (!strcasecmp(args.item, "channel")) {
      if (agent->chan) {
         ast_channel_lock(agent->chan);
         ast_copy_string(buf, ast_channel_name(agent->chan), len);
         ast_channel_unlock(agent->chan);
         tmp = strrchr(buf, '-');
         if (tmp)
            *tmp = '\0';
      } 
   } else if (!strcasecmp(args.item, "fullchannel")) {
      if (agent->chan) {
         ast_channel_lock(agent->chan);
         ast_copy_string(buf, ast_channel_name(agent->chan), len);
         ast_channel_unlock(agent->chan);
      } 
   } else if (!strcasecmp(args.item, "exten")) {
      buf[0] = '\0';
   }

   AST_LIST_UNLOCK(&agents);

   return 0;
}
static int load_module ( void  ) [static]

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other things it registers applications, cli commands and reads the cofiguration file.

Returns:
int Always 0.

Definition at line 2513 of file chan_agent.c.

References action_agent_logoff(), action_agents(), agent_function, agent_tech, agentmonitoroutgoing_exec(), agents_data_providers, ARRAY_LEN, ast_channel_register(), ast_cli_register_multiple(), ast_custom_function_register, ast_data_register_multiple, ast_format_cap_add_all(), ast_format_cap_alloc(), ast_format_cap_destroy(), ast_log(), ast_manager_register_xml, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_application_xml, ast_channel_tech::capabilities, cli_agents, EVENT_FLAG_AGENT, LOG_ERROR, login_exec(), and read_agent_config().

static int login_exec ( struct ast_channel chan,
const char *  data 
) [static]

Log in agent application.

Called by the AgentLogin application (from the dial plan).

Parameters:
chan
data
Returns:
See also:
agentmonitoroutgoing_exec(), load_module().

Definition at line 1882 of file chan_agent.c.

References agent_pvt::acceptdtmf, agent_pvt::ackcall, agent_pvt::acknowledged, agent_pvt::agent, agent_ack_sleep(), agent_cont_sleep(), AGENT_FLAG_ACCEPTDTMF, AGENT_FLAG_ACKCALL, AGENT_FLAG_AUTOLOGOFF, AGENT_FLAG_ENDDTMF, AGENT_FLAG_WRAPUPTIME, agent_pvt_destroy(), agent_pvt::app_complete_cond, agent_pvt::app_lock_flag, args, ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_channel_cdr(), ast_channel_language(), ast_channel_lock, ast_channel_name(), ast_channel_nativeformats(), ast_channel_readformat(), ast_channel_uniqueid(), ast_channel_unlock, ast_channel_writeformat(), ast_check_hangup(), ast_cond_signal, ast_cond_wait, AST_CONTROL_HOLD, ast_copy_string(), ast_debug, AST_DECLARE_APP_ARGS, AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_getformatname(), ast_indicate_data(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), AST_MAX_AGENT, AST_MAX_FILENAME_LEN, ast_mutex_lock, ast_mutex_unlock, ast_queue_log(), ast_safe_sleep(), ast_safe_sleep_conditional(), ast_set_flag, ast_set_read_format_from_cap(), ast_set_write_format_from_cap(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_streamfile(), ast_strlen_zero(), ast_true(), ast_tv(), ast_tvdiff_ms(), ast_tvnow(), ast_verb, ast_waitstream(), agent_pvt::autologoff, agent_pvt::chan, check_availability(), check_beep(), agent_pvt::dead, agent_pvt::deferlogoff, agent_pvt::enddtmf, EVENT_FLAG_AGENT, agent_pvt::lastdisc, agent_pvt::lock, LOG_NOTICE, LOG_WARNING, agent_pvt::login_wait_cond, agent_pvt::loginstart, manager_event, maxlogintries, agent_pvt::moh, agent_pvt::owner, parse(), pass, agent_pvt::password, pbx_builtin_getvar_helper(), agent_pvt::pending, S_OR, update_cdr, updatecdr, and agent_pvt::wrapuptime.

Referenced by load_module().

{
   int res=0;
   int tries = 0;
   int max_login_tries = maxlogintries;
   struct agent_pvt *p;
   char user[AST_MAX_AGENT];
   char pass[AST_MAX_AGENT];
   char xpass[AST_MAX_AGENT];
   char *errmsg;
   char *parse;
   AST_DECLARE_APP_ARGS(args,
              AST_APP_ARG(agent_id);
              AST_APP_ARG(options);
              AST_APP_ARG(extension);
      );
   const char *tmpoptions = NULL;
   int play_announcement = 1;
   char agent_goodbye[AST_MAX_FILENAME_LEN];
   int update_cdr = updatecdr;

   user[0] = '\0';
   xpass[0] = '\0';

   parse = ast_strdupa(data);

   AST_STANDARD_APP_ARGS(args, parse);

   ast_copy_string(agent_goodbye, agentgoodbye, sizeof(agent_goodbye));

   ast_channel_lock(chan);
   /* Set Channel Specific Login Overrides */
   if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTLMAXLOGINTRIES"))) {
      max_login_tries = atoi(pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES"));
      if (max_login_tries < 0)
         max_login_tries = 0;
      tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES");
      ast_verb(3, "Saw variable AGENTMAXLOGINTRIES=%s, setting max_login_tries to: %d on Channel '%s'.\n",tmpoptions,max_login_tries,ast_channel_name(chan));
   }
   if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR"))) {
      if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR")))
         update_cdr = 1;
      else
         update_cdr = 0;
      tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR");
      ast_verb(3, "Saw variable AGENTUPDATECDR=%s, setting update_cdr to: %d on Channel '%s'.\n",tmpoptions,update_cdr,ast_channel_name(chan));
   }
   if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"))) {
      strcpy(agent_goodbye, pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"));
      tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTGOODBYE");
      ast_verb(3, "Saw variable AGENTGOODBYE=%s, setting agent_goodbye to: %s on Channel '%s'.\n",tmpoptions,agent_goodbye,ast_channel_name(chan));
   }
   ast_channel_unlock(chan);
   /* End Channel Specific Login Overrides */
   
   if (!ast_strlen_zero(args.options)) {
      if (strchr(args.options, 's')) {
         play_announcement = 0;
      }
   }

   if (ast_channel_state(chan) != AST_STATE_UP)
      res = ast_answer(chan);
   if (!res) {
      if (!ast_strlen_zero(args.agent_id))
         ast_copy_string(user, args.agent_id, AST_MAX_AGENT);
      else
         res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
   }
   while (!res && (max_login_tries==0 || tries < max_login_tries)) {
      tries++;
      /* Check for password */
      AST_LIST_LOCK(&agents);
      AST_LIST_TRAVERSE(&agents, p, list) {
         if (!strcmp(p->agent, user) && !p->pending)
            ast_copy_string(xpass, p->password, sizeof(xpass));
      }
      AST_LIST_UNLOCK(&agents);
      if (!res) {
         if (!ast_strlen_zero(xpass))
            res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
         else
            pass[0] = '\0';
      }
      errmsg = "agent-incorrect";

#if 0
      ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
#endif      

      /* Check again for accuracy */
      AST_LIST_LOCK(&agents);
      AST_LIST_TRAVERSE(&agents, p, list) {
         int unlock_channel = 1;

         ast_channel_lock(chan);
         ast_mutex_lock(&p->lock);
         if (!strcmp(p->agent, user) &&
             !strcmp(p->password, pass) && !p->pending) {

            /* Set Channel Specific Agent Overrides */
            if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
               if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
                  p->ackcall = 1;
               } else {
                  p->ackcall = 0;
               }
               tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTACKCALL");
               ast_verb(3, "Saw variable AGENTACKCALL=%s, setting ackcall to: %d for Agent '%s'.\n", tmpoptions, p->ackcall, p->agent);
               ast_set_flag(p, AGENT_FLAG_ACKCALL);
            } else {
               p->ackcall = ackcall;
            }
            if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"))) {
               p->autologoff = atoi(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"));
               if (p->autologoff < 0)
                  p->autologoff = 0;
               tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF");
               ast_verb(3, "Saw variable AGENTAUTOLOGOFF=%s, setting autologff to: %d for Agent '%s'.\n", tmpoptions, p->autologoff, p->agent);
               ast_set_flag(p, AGENT_FLAG_AUTOLOGOFF);
            } else {
               p->autologoff = autologoff;
            }
            if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"))) {
               p->wrapuptime = atoi(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"));
               if (p->wrapuptime < 0)
                  p->wrapuptime = 0;
               tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME");
               ast_verb(3, "Saw variable AGENTWRAPUPTIME=%s, setting wrapuptime to: %d for Agent '%s'.\n", tmpoptions, p->wrapuptime, p->agent);
               ast_set_flag(p, AGENT_FLAG_WRAPUPTIME);
            } else {
               p->wrapuptime = wrapuptime;
            }
            tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTACCEPTDTMF");
            if (!ast_strlen_zero(tmpoptions)) {
               p->acceptdtmf = *tmpoptions;
               ast_verb(3, "Saw variable AGENTACCEPTDTMF=%s, setting acceptdtmf to: %c for Agent '%s'.\n", tmpoptions, p->acceptdtmf, p->agent);
               ast_set_flag(p, AGENT_FLAG_ACCEPTDTMF);
            }
            tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTENDDTMF");
            if (!ast_strlen_zero(tmpoptions)) {
               p->enddtmf = *tmpoptions;
               ast_verb(3, "Saw variable AGENTENDDTMF=%s, setting enddtmf to: %c for Agent '%s'.\n", tmpoptions, p->enddtmf, p->agent);
               ast_set_flag(p, AGENT_FLAG_ENDDTMF);
            }
            ast_channel_unlock(chan);
            unlock_channel = 0;
            /* End Channel Specific Agent Overrides */

            if (!p->chan) {
               /* Ensure nobody else can be this agent until we're done. */
               p->chan = chan;

               p->acknowledged = 0;

               if (!res) {
                  struct ast_format tmpfmt;
                  res = ast_set_read_format_from_cap(chan, ast_channel_nativeformats(chan));
                  if (res) {
                     ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
                  }
               }
               if (!res) {
                  struct ast_format tmpfmt;
                  res = ast_set_write_format_from_cap(chan, ast_channel_nativeformats(chan));
                  if (res) {
                     ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
                  }
               }
               if (!res && play_announcement == 1) {
                  ast_mutex_unlock(&p->lock);
                  AST_LIST_UNLOCK(&agents);
                  res = ast_streamfile(chan, "agent-loginok", ast_channel_language(chan));
                  if (!res) {
                     ast_waitstream(chan, "");
                  }
                  AST_LIST_LOCK(&agents);
                  ast_mutex_lock(&p->lock);
               }

               if (!res) {
                  long logintime;
                  char agent[AST_MAX_AGENT];

                  snprintf(agent, sizeof(agent), "Agent/%s", p->agent);

                  /* Login this channel and wait for it to go away */
                  ast_indicate_data(chan, AST_CONTROL_HOLD, 
                     S_OR(p->moh, NULL), 
                     !ast_strlen_zero(p->moh) ? strlen(p->moh) + 1 : 0);

                  /* Must be done after starting HOLD. */
                  p->lastdisc = ast_tvnow();
                  time(&p->loginstart);

                  /*** DOCUMENTATION
                     <managerEventInstance>
                        <synopsis>Raised when an Agent has logged in.</synopsis>
                        <syntax>
                           <parameter name="Agent">
                              <para>The name of the agent.</para>
                           </parameter>
                        </syntax>
                        <see-also>
                           <ref type="application">AgentLogin</ref>
                           <ref type="managerEvent">Agentlogoff</ref>
                        </see-also>
                     </managerEventInstance>
                  ***/
                  manager_event(EVENT_FLAG_AGENT, "Agentlogin",
                           "Agent: %s\r\n"
                           "Channel: %s\r\n"
                           "Uniqueid: %s\r\n",
                           p->agent, ast_channel_name(chan), ast_channel_uniqueid(chan));
                  if (update_cdr && ast_channel_cdr(chan))
                     snprintf(ast_channel_cdr(chan)->channel, sizeof(ast_channel_cdr(chan)->channel), "%s", agent);
                  ast_queue_log("NONE", ast_channel_uniqueid(chan), agent, "AGENTLOGIN", "%s", ast_channel_name(chan));
                  ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", p->agent,
                            ast_getformatname(ast_channel_readformat(chan)), ast_getformatname(ast_channel_writeformat(chan)));

                  ast_mutex_unlock(&p->lock);
                  AST_LIST_UNLOCK(&agents);

                  while (res >= 0) {
                     ast_mutex_lock(&p->lock);
                     if (p->deferlogoff) {
                        p->deferlogoff = 0;
                        ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
                        ast_mutex_unlock(&p->lock);
                        break;
                     }
                     ast_mutex_unlock(&p->lock);

                     AST_LIST_LOCK(&agents);
                     ast_mutex_lock(&p->lock);
                     if (p->lastdisc.tv_sec) {
                        if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
                           ast_debug(1, "Wrapup time for %s expired!\n", agent);
                           p->lastdisc = ast_tv(0, 0);
                           ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "%s", agent);
                           if (p->ackcall) {
                              check_beep(p, 0);
                           } else {
                              check_availability(p, 0);
                           }
                        }
                     }
                     ast_mutex_unlock(&p->lock);
                     AST_LIST_UNLOCK(&agents);

                     /* Synchronize channel ownership between call to agent and itself. */
                     ast_mutex_lock(&p->lock);
                     if (p->app_lock_flag) {
                        ast_cond_signal(&p->login_wait_cond);
                        ast_cond_wait(&p->app_complete_cond, &p->lock);
                        if (ast_check_hangup(chan)) {
                           /* Agent hungup */
                           ast_mutex_unlock(&p->lock);
                           break;
                        }
                     }
                     ast_mutex_unlock(&p->lock);

                     if (p->ackcall) {
                        res = agent_ack_sleep(p);
                        if (res == 1) {
                           AST_LIST_LOCK(&agents);
                           ast_mutex_lock(&p->lock);
                           check_availability(p, 0);
                           ast_mutex_unlock(&p->lock);
                           AST_LIST_UNLOCK(&agents);
                        }
                     } else {
                        res = ast_safe_sleep_conditional( chan, 1000, agent_cont_sleep, p );
                     }
                  }
                  ast_mutex_lock(&p->lock);

                  /* Logoff this channel */
                  p->chan = NULL;
                  logintime = time(NULL) - p->loginstart;
                  p->loginstart = 0;

                  /* Synchronize channel ownership between call to agent and itself. */
                  if (p->app_lock_flag) {
                     ast_cond_signal(&p->login_wait_cond);
                     ast_cond_wait(&p->app_complete_cond, &p->lock);
                  }

                  if (p->owner) {
                     ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
                  }

                  p->acknowledged = 0;
                  ast_mutex_unlock(&p->lock);

                  ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "%s", agent);
                  /*** DOCUMENTATION
                     <managerEventInstance>
                        <synopsis>Raised when an Agent has logged off.</synopsis>
                        <syntax>
                           <xi:include xpointer="xpointer(/docs/managerEvent[@name='Agentlogin']/managerEventInstance/syntax/parameter[@name='Agent'])" />
                        </syntax>
                        <see-also>
                           <ref type="managerEvent">Agentlogin</ref>
                        </see-also>
                     </managerEventInstance>
                  ***/
                  manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
                           "Agent: %s\r\n"
                           "Logintime: %ld\r\n"
                           "Uniqueid: %s\r\n",
                           p->agent, logintime, ast_channel_uniqueid(chan));
                  ast_queue_log("NONE", ast_channel_uniqueid(chan), agent, "AGENTLOGOFF", "%s|%ld", ast_channel_name(chan), logintime);
                  ast_verb(2, "Agent '%s' logged out\n", p->agent);

                  /* If there is no owner, go ahead and kill it now */
                  if (p->dead && !p->owner) {
                     agent_pvt_destroy(p);
                  }
                  AST_LIST_LOCK(&agents);
               } else {
                  /* Agent hung up before could be logged in. */
                  p->chan = NULL;

                  ast_mutex_unlock(&p->lock);
               }
               res = -1;
            } else {
               ast_mutex_unlock(&p->lock);
               errmsg = "agent-alreadyon";
            }
            break;
         }
         ast_mutex_unlock(&p->lock);
         if (unlock_channel) {
            ast_channel_unlock(chan);
         }
      }
      AST_LIST_UNLOCK(&agents);

      if (!res && (max_login_tries==0 || tries < max_login_tries))
         res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
   }
      
   if (!res)
      res = ast_safe_sleep(chan, 500);

   return -1;
}
static force_inline int powerof ( unsigned int  d) [static]

Definition at line 1487 of file chan_agent.c.

Referenced by agents_show().

{
   int x = ffs(d);

   if (x)
      return x - 1;

   return 0;
}
static int read_agent_config ( int  reload) [static]

Read configuration data. The file named agents.conf.

Returns:
Always 0, or so it seems.

Definition at line 1120 of file chan_agent.c.

References add_agent(), agent_pvt_destroy(), ast_category_browse(), ast_config_destroy(), ast_config_load, ast_copy_string(), ast_get_group(), AST_LIST_LOCK, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_LIST_UNLOCK, ast_log(), ast_softhangup(), AST_SOFTHANGUP_EXPLICIT, ast_strlen_zero(), ast_true(), ast_variable_browse(), ast_variable_retrieve(), agent_pvt::chan, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, agent_pvt::dead, LOG_ERROR, LOG_NOTICE, ast_variable::name, ast_variable::next, agent_pvt::owner, secret, and ast_variable::value.

Referenced by load_module(), and reload().

{
   struct ast_config *cfg;
   struct ast_config *ucfg;
   struct ast_variable *v;
   struct agent_pvt *p;
   const char *catname;
   const char *hasagent;
   int genhasagent;
   struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };

   group = 0;
   autologoff = 0;
   wrapuptime = 0;
   ackcall = 0;
   endcall = 1;
   cfg = ast_config_load(config, config_flags);
   if (!cfg) {
      ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
      return 0;
   } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
      return -1;
   } else if (cfg == CONFIG_STATUS_FILEINVALID) {
      ast_log(LOG_ERROR, "%s contains a parsing error.  Aborting\n", config);
      return 0;
   }
   if ((ucfg = ast_config_load("users.conf", config_flags))) {
      if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
         ucfg = NULL;
      } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
         ast_log(LOG_ERROR, "users.conf contains a parsing error.  Aborting\n");
         return 0;
      }
   }

   AST_LIST_LOCK(&agents);
   AST_LIST_TRAVERSE(&agents, p, list) {
      p->dead = 1;
   }
   strcpy(moh, "default");
   /* set the default recording values */
   recordagentcalls = 0;
   strcpy(recordformat, "wav");
   strcpy(recordformatext, "wav");
   urlprefix[0] = '\0';
   savecallsin[0] = '\0';

   /* Read in the [agents] section */
   v = ast_variable_browse(cfg, "agents");
   while(v) {
      /* Create the interface list */
      if (!strcasecmp(v->name, "agent")) {
         add_agent(v->value, 0);
      } else if (!strcasecmp(v->name, "group")) {
         group = ast_get_group(v->value);
      } else if (!strcasecmp(v->name, "autologoff")) {
         autologoff = atoi(v->value);
         if (autologoff < 0)
            autologoff = 0;
      } else if (!strcasecmp(v->name, "ackcall")) {
         if (ast_true(v->value) || !strcasecmp(v->value, "always")) {
            ackcall = 1;
         }
      } else if (!strcasecmp(v->name, "endcall")) {
         endcall = ast_true(v->value);
      } else if (!strcasecmp(v->name, "acceptdtmf")) {
         acceptdtmf = *(v->value);
         ast_log(LOG_NOTICE, "Set acceptdtmf to %c\n", acceptdtmf);
      } else if (!strcasecmp(v->name, "enddtmf")) {
         enddtmf = *(v->value);
      } else if (!strcasecmp(v->name, "wrapuptime")) {
         wrapuptime = atoi(v->value);
         if (wrapuptime < 0)
            wrapuptime = 0;
      } else if (!strcasecmp(v->name, "maxlogintries") && !ast_strlen_zero(v->value)) {
         maxlogintries = atoi(v->value);
         if (maxlogintries < 0)
            maxlogintries = 0;
      } else if (!strcasecmp(v->name, "goodbye") && !ast_strlen_zero(v->value)) {
         strcpy(agentgoodbye,v->value);
      } else if (!strcasecmp(v->name, "musiconhold")) {
         ast_copy_string(moh, v->value, sizeof(moh));
      } else if (!strcasecmp(v->name, "updatecdr")) {
         if (ast_true(v->value))
            updatecdr = 1;
         else
            updatecdr = 0;
      } else if (!strcasecmp(v->name, "autologoffunavail")) {
         if (ast_true(v->value))
            autologoffunavail = 1;
         else
            autologoffunavail = 0;
      } else if (!strcasecmp(v->name, "recordagentcalls")) {
         recordagentcalls = ast_true(v->value);
      } else if (!strcasecmp(v->name, "recordformat")) {
         ast_copy_string(recordformat, v->value, sizeof(recordformat));
         if (!strcasecmp(v->value, "wav49"))
            strcpy(recordformatext, "WAV");
         else
            ast_copy_string(recordformatext, v->value, sizeof(recordformatext));
      } else if (!strcasecmp(v->name, "urlprefix")) {
         ast_copy_string(urlprefix, v->value, sizeof(urlprefix));
         if (urlprefix[strlen(urlprefix) - 1] != '/')
            strncat(urlprefix, "/", sizeof(urlprefix) - strlen(urlprefix) - 1);
      } else if (!strcasecmp(v->name, "savecallsin")) {
         if (v->value[0] == '/')
            ast_copy_string(savecallsin, v->value, sizeof(savecallsin));
         else
            snprintf(savecallsin, sizeof(savecallsin) - 2, "/%s", v->value);
         if (savecallsin[strlen(savecallsin) - 1] != '/')
            strncat(savecallsin, "/", sizeof(savecallsin) - strlen(savecallsin) - 1);
      } else if (!strcasecmp(v->name, "custom_beep")) {
         ast_copy_string(beep, v->value, sizeof(beep));
      }
      v = v->next;
   }
   if (ucfg) {
      genhasagent = ast_true(ast_variable_retrieve(ucfg, "general", "hasagent"));
      catname = ast_category_browse(ucfg, NULL);
      while(catname) {
         if (strcasecmp(catname, "general")) {
            hasagent = ast_variable_retrieve(ucfg, catname, "hasagent");
            if (ast_true(hasagent) || (!hasagent && genhasagent)) {
               char tmp[256];
               const char *fullname = ast_variable_retrieve(ucfg, catname, "fullname");
               const char *secret = ast_variable_retrieve(ucfg, catname, "secret");
               if (!fullname)
                  fullname = "";
               if (!secret)
                  secret = "";
               snprintf(tmp, sizeof(tmp), "%s,%s,%s", catname, secret,fullname);
               add_agent(tmp, 0);
            }
         }
         catname = ast_category_browse(ucfg, catname);
      }
      ast_config_destroy(ucfg);
   }
   AST_LIST_TRAVERSE_SAFE_BEGIN(&agents, p, list) {
      if (p->dead) {
         AST_LIST_REMOVE_CURRENT(list);
         /* Destroy if  appropriate */
         if (!p->owner) {
            if (!p->chan) {
               agent_pvt_destroy(p);
            } else {
               /* Cause them to hang up */
               ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
            }
         }
      }
   }
   AST_LIST_TRAVERSE_SAFE_END;
   AST_LIST_UNLOCK(&agents);
   ast_config_destroy(cfg);
   return 1;
}
static int reload ( void  ) [static]

Definition at line 2551 of file chan_agent.c.

References read_agent_config().

{
   return read_agent_config(1);
}

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Agent Proxy Channel" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CHANNEL_DRIVER, .nonoptreq = "res_monitor,chan_local", } [static]

Definition at line 2593 of file chan_agent.c.

char acceptdtmf = DEFAULT_ACCEPTDTMF [static]

Definition at line 227 of file chan_agent.c.

Referenced by play_record_review().

int ackcall [static]

Definition at line 224 of file chan_agent.c.

Initial value:
 {
   .name = "AGENT",
   .read = function_agent,
}

Definition at line 2413 of file chan_agent.c.

Referenced by load_module(), and unload_module().

const char agent_logoff_usage[] = " If 'soft' is specified, do not hangup existing calls.\n" [static]

Definition at line 1861 of file chan_agent.c.

struct ast_channel_tech agent_tech [static]

Channel interface description for PBX integration.

Definition at line 350 of file chan_agent.c.

Referenced by agent_new(), load_module(), and unload_module().

char agentgoodbye[AST_MAX_FILENAME_LEN] = "vm-goodbye" [static]

Definition at line 231 of file chan_agent.c.

struct agents agents [static]

Holds the list of agents (loaded form agents.conf).

Initial value:

Definition at line 2497 of file chan_agent.c.

Initial value:
 {
   AST_DATA_ENTRY("asterisk/channel/agent/list", &agents_data_provider),
}

Definition at line 2502 of file chan_agent.c.

Referenced by load_module().

const char app[] = "AgentLogin" [static]

Definition at line 206 of file chan_agent.c.

const char app3[] = "AgentMonitorOutgoing" [static]

Definition at line 207 of file chan_agent.c.

Definition at line 2593 of file chan_agent.c.

int autologoff [static]

Definition at line 222 of file chan_agent.c.

int autologoffunavail = 0 [static]

Definition at line 226 of file chan_agent.c.

char beep[AST_MAX_BUF] = "beep" [static]

Definition at line 239 of file chan_agent.c.

struct ast_cli_entry cli_agents[] [static]
Initial value:
 {
   AST_CLI_DEFINE(agents_show, "Show status of agents"),
   AST_CLI_DEFINE(agents_show_online, "Show all online agents"),
   AST_CLI_DEFINE(agent_logoff_cmd, "Sets an agent offline"),
}

Definition at line 1866 of file chan_agent.c.

Referenced by load_module(), and unload_module().

const char config[] = "agents.conf" [static]

Definition at line 204 of file chan_agent.c.

int endcall [static]

Definition at line 225 of file chan_agent.c.

char enddtmf = DEFAULT_ENDDTMF [static]

Definition at line 228 of file chan_agent.c.

ast_group_t group [static]

Definition at line 221 of file chan_agent.c.

Referenced by ast_get_group(), and handle_cli_check_permissions().

int maxlogintries = 3 [static]

Definition at line 230 of file chan_agent.c.

Referenced by login_exec().

char moh[80] = "default" [static]
const char pa_family[] = "Agents" [static]

Persistent Agents astdb family

Definition at line 215 of file chan_agent.c.

int recordagentcalls = 0 [static]

Definition at line 233 of file chan_agent.c.

char recordformat[AST_MAX_BUF] = "" [static]

Definition at line 234 of file chan_agent.c.

char recordformatext[AST_MAX_BUF] = "" [static]

Definition at line 235 of file chan_agent.c.

char savecallsin[AST_MAX_BUF] = "" [static]

Definition at line 237 of file chan_agent.c.

const char tdesc[] = "Call Agent Proxy Channel" [static]

Definition at line 203 of file chan_agent.c.

int updatecdr = 0 [static]

Definition at line 238 of file chan_agent.c.

Referenced by login_exec().

char urlprefix[AST_MAX_BUF] = "" [static]

Definition at line 236 of file chan_agent.c.

Referenced by start_monitor_exec().

int wrapuptime [static]

Definition at line 223 of file chan_agent.c.