app_queue: Added initialization for "context" parameter
[asterisk/asterisk.git] / apps / app_agent_pool.c
index 699be58..68bcfde 100644 (file)
@@ -33,7 +33,7 @@
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include "asterisk/cli.h"
 #include "asterisk/app.h"
@@ -108,13 +108,16 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        <parameter name="AgentId" required="true" />
                </syntax>
                <description>
-                       <para>Request an agent to connect with the channel.  Failure to find and
-                       alert an agent will continue in the dialplan with <variable>AGENT_STATUS</variable> set.</para>
+                       <para>Request an agent to connect with the channel.  Failure to find,
+                       alert the agent, or acknowledge the call will continue in the dialplan
+                       with <variable>AGENT_STATUS</variable> set.</para>
                        <para><variable>AGENT_STATUS</variable> enumeration values:</para>
                        <enumlist>
                                <enum name = "INVALID"><para>The specified agent is invalid.</para></enum>
                                <enum name = "NOT_LOGGED_IN"><para>The agent is not available.</para></enum>
                                <enum name = "BUSY"><para>The agent is on another call.</para></enum>
+                               <enum name = "NOT_CONNECTED"><para>The agent did not connect with the
+                               call.  The agent most likely did not acknowledge the call.</para></enum>
                                <enum name = "ERROR"><para>Alerting the agent failed.</para></enum>
                        </enumlist>
                </description>
@@ -545,18 +548,14 @@ static int load_config(void)
        aco_option_register(&cfg_info, "wrapuptime", ACO_EXACT, agent_types, "0", OPT_UINT_T, 0, FLDSET(struct agent_cfg, wrapup_time));
        aco_option_register(&cfg_info, "musiconhold", ACO_EXACT, agent_types, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, moh));
        aco_option_register(&cfg_info, "recordagentcalls", ACO_EXACT, agent_types, "no", OPT_BOOL_T, 1, FLDSET(struct agent_cfg, record_agent_calls));
-       aco_option_register(&cfg_info, "custom_beep", ACO_EXACT, agent_types, "beep", OPT_STRINGFIELD_T, 1, STRFLDSET(struct agent_cfg, beep_sound));
+       aco_option_register(&cfg_info, "custom_beep", ACO_EXACT, agent_types, "beep", OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, beep_sound));
        aco_option_register(&cfg_info, "fullname", ACO_EXACT, agent_types, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct agent_cfg, full_name));
 
        if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
-               goto error;
+               return -1;
        }
 
        return 0;
-
-error:
-       destroy_config();
-       return -1;
 }
 
 enum agent_state {
@@ -727,12 +726,17 @@ static struct ast_channel *agent_lock_logged(struct agent_pvt *agent)
  */
 static enum ast_device_state agent_pvt_devstate_get(const char *agent_id)
 {
-       RAII_VAR(struct agent_pvt *, agent, ao2_find(agents, agent_id, OBJ_KEY), ao2_cleanup);
+       enum ast_device_state dev_state = AST_DEVICE_INVALID;
+       struct agent_pvt *agent;
 
+       agent = ao2_find(agents, agent_id, OBJ_KEY);
        if (agent) {
-               return agent->devstate;
+               agent_lock(agent);
+               dev_state = agent->devstate;
+               agent_unlock(agent);
+               ao2_ref(agent, -1);
        }
-       return AST_DEVICE_INVALID;
+       return dev_state;
 }
 
 /*!
@@ -761,7 +765,7 @@ static void agent_pvt_destructor(void *vdoomed)
 
        ast_party_connected_line_free(&doomed->waiting_colp);
        if (doomed->caller_bridge) {
-               ast_bridge_destroy(doomed->caller_bridge, AST_CAUSE_USER_BUSY);
+               ast_bridge_destroy(doomed->caller_bridge, 0);
                doomed->caller_bridge = NULL;
        }
        if (doomed->logged) {
@@ -995,6 +999,23 @@ AST_MUTEX_DEFINE_STATIC(agent_holding_lock);
 
 /*!
  * \internal
+ * \brief Callback to clear AGENT_STATUS on the caller channel.
+ *
+ * \param bridge_channel Which channel to operate on.
+ * \param payload Data to pass to the callback. (NULL if none).
+ * \param payload_size Size of the payload if payload is non-NULL.  A number otherwise.
+ *
+ * \note The payload MUST NOT have any resources that need to be freed.
+ *
+ * \return Nothing
+ */
+static void clear_agent_status(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
+{
+       pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", NULL);
+}
+
+/*!
+ * \internal
  * \brief Connect the agent with the waiting caller.
  * \since 12.0.0
  *
@@ -1028,12 +1049,18 @@ static void agent_connect_caller(struct ast_bridge_channel *bridge_channel, stru
                NULL, 0);
        if (res) {
                /* Reset agent. */
-               ast_bridge_destroy(caller_bridge, AST_CAUSE_USER_BUSY);
+               ast_bridge_destroy(caller_bridge, 0);
                ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
                        AST_CAUSE_NORMAL_CLEARING);
                return;
        }
-       ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_ANSWER, NULL, 0);
+       res = ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_ANSWER, NULL, 0)
+               || ast_bridge_channel_write_callback(bridge_channel, 0, clear_agent_status, NULL, 0);
+       if (res) {
+               /* Reset agent. */
+               ast_bridge_destroy(caller_bridge, 0);
+               return;
+       }
 
        if (record_agent_calls) {
                struct ast_bridge_features_automixmonitor options = {
@@ -1046,6 +1073,8 @@ static void agent_connect_caller(struct ast_bridge_channel *bridge_channel, stru
                 */
                ast_bridge_features_do(AST_BRIDGE_BUILTIN_AUTOMIXMON, bridge_channel, &options);
        }
+
+       ao2_t_ref(caller_bridge, -1, "Agent successfully in caller_bridge");
 }
 
 static int bridge_agent_hold_ack(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
@@ -1414,7 +1443,7 @@ static void send_agent_login(struct ast_channel *chan, const char *agent)
                return;
        }
 
-       ast_channel_publish_blob(chan, ast_channel_agent_login_type(), blob);
+       ast_channel_publish_cached_blob(chan, ast_channel_agent_login_type(), blob);
 }
 
 static void send_agent_logoff(struct ast_channel *chan, const char *agent, long logintime)
@@ -1430,7 +1459,7 @@ static void send_agent_logoff(struct ast_channel *chan, const char *agent, long
                return;
        }
 
-       ast_channel_publish_blob(chan, ast_channel_agent_logoff_type(), blob);
+       ast_channel_publish_cached_blob(chan, ast_channel_agent_logoff_type(), blob);
 }
 
 /*!
@@ -1462,7 +1491,7 @@ static void agent_logout(struct agent_pvt *agent)
        agent_devstate_changed(agent->username);
 
        if (caller_bridge) {
-               ast_bridge_destroy(caller_bridge, AST_CAUSE_USER_BUSY);
+               ast_bridge_destroy(caller_bridge, 0);
        }
 
        ast_channel_lock(logged);
@@ -1548,7 +1577,7 @@ static void agent_run(struct agent_pvt *agent, struct ast_channel *logged)
                agent_unlock(agent);
                ao2_ref(cfg_old, -1);
                if (caller_bridge) {
-                       ast_bridge_destroy(caller_bridge, AST_CAUSE_USER_BUSY);
+                       ast_bridge_destroy(caller_bridge, 0);
                }
 
                if (agent->state == AGENT_STATE_LOGGING_OUT
@@ -1674,7 +1703,7 @@ static void caller_abort_agent(struct agent_pvt *agent)
                agent->caller_bridge = NULL;
                agent_unlock(agent);
                if (caller_bridge) {
-                       ast_bridge_destroy(caller_bridge, AST_CAUSE_USER_BUSY);
+                       ast_bridge_destroy(caller_bridge, 0);
                }
                return;
        }
@@ -1690,9 +1719,11 @@ static int caller_safety_timeout(struct ast_bridge_channel *bridge_channel, void
        struct agent_pvt *agent = hook_pvt;
 
        if (agent->state == AGENT_STATE_CALL_PRESENT) {
-               ast_verb(3, "Agent '%s' did not respond.  Safety timeout.\n", agent->username);
-               ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
-                       AST_CAUSE_USER_BUSY);
+               ast_log(LOG_WARNING, "Agent '%s' process did not respond.  Safety timeout.\n",
+                       agent->username);
+               pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
+
+               ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
                caller_abort_agent(agent);
        }
 
@@ -1703,7 +1734,10 @@ static void agent_alert(struct ast_bridge_channel *bridge_channel, const void *p
 {
        const char *agent_id = payload;
        const char *playfile;
-       RAII_VAR(struct agent_pvt *, agent, NULL, ao2_cleanup);
+       const char *dtmf_accept;
+       struct agent_pvt *agent;
+       int digit;
+       char dtmf[2];
 
        agent = ao2_find(agents, agent_id, OBJ_KEY);
        if (!agent) {
@@ -1718,30 +1752,56 @@ static void agent_alert(struct ast_bridge_channel *bridge_channel, const void *p
        ast_bridge_channel_establish_roles(bridge_channel);
        ast_bridge_unlock(bridge_channel->bridge);
 
-       /* Alert the agent. */
        agent_lock(agent);
        playfile = ast_strdupa(agent->cfg->beep_sound);
+
+       /* Determine which DTMF digits interrupt the alerting signal. */
+       if (ast_test_flag(agent, AGENT_FLAG_ACK_CALL)
+               ? agent->override_ack_call : agent->cfg->ack_call) {
+               dtmf_accept = ast_test_flag(agent, AGENT_FLAG_DTMF_ACCEPT)
+                       ? agent->override_dtmf_accept : agent->cfg->dtmf_accept;
+
+               /* Only the first digit of the ack will stop playback. */
+               dtmf[0] = *dtmf_accept;
+               dtmf[1] = '\0';
+               dtmf_accept = dtmf;
+       } else {
+               dtmf_accept = NULL;
+       }
        agent_unlock(agent);
-       ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
+
+       /* Alert the agent. */
+       digit = ast_stream_and_wait(bridge_channel->chan, playfile,
+               ast_strlen_zero(dtmf_accept) ? AST_DIGIT_ANY : dtmf_accept);
+       ast_stopstream(bridge_channel->chan);
 
        agent_lock(agent);
        switch (agent->state) {
        case AGENT_STATE_CALL_PRESENT:
-               if (ast_test_flag(agent, AGENT_FLAG_ACK_CALL)
-                       ? agent->override_ack_call : agent->cfg->ack_call) {
+               if (!ast_strlen_zero(dtmf_accept)) {
                        agent->state = AGENT_STATE_CALL_WAIT_ACK;
                        agent->ack_time = ast_tvnow();
+
+                       if (0 < digit) {
+                               /* Playback was interrupted by a digit. */
+                               agent_unlock(agent);
+                               ao2_ref(agent, -1);
+                               ast_bridge_channel_feature_digit(bridge_channel, digit);
+                               return;
+                       }
                        break;
                }
 
                /* Connect to caller now. */
                ast_debug(1, "Agent %s: Immediately connecting to call.\n", agent->username);
                agent_connect_caller(bridge_channel, agent);/* Will unlock agent. */
+               ao2_ref(agent, -1);
                return;
        default:
                break;
        }
        agent_unlock(agent);
+       ao2_ref(agent, -1);
 }
 
 static int send_alert_to_agent(struct ast_bridge_channel *bridge_channel, const char *agent_id)
@@ -1770,6 +1830,49 @@ static int send_colp_to_agent(struct ast_bridge_channel *bridge_channel, struct
 }
 
 /*!
+ * \internal
+ * \brief Caller joined the bridge event callback.
+ *
+ * \param bridge_channel Channel executing the feature
+ * \param hook_pvt Private data passed in when the hook was created
+ *
+ * \retval 0 Keep the callback hook.
+ * \retval -1 Remove the callback hook.
+ */
+static int caller_joined_bridge(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
+{
+       struct agent_pvt *agent = hook_pvt;
+       struct ast_bridge_channel *logged;
+       int res;
+
+       logged = agent_bridge_channel_get_lock(agent);
+       if (!logged) {
+               ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
+               pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_LOGGED_IN");
+
+               ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
+               caller_abort_agent(agent);
+               return -1;
+       }
+
+       res = send_alert_to_agent(logged, agent->username);
+       ast_bridge_channel_unlock(logged);
+       ao2_ref(logged, -1);
+       if (res) {
+               ast_verb(3, "Agent '%s': Failed to alert the agent.\n", agent->username);
+               pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "ERROR");
+
+               ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
+               caller_abort_agent(agent);
+               return -1;
+       }
+
+       pbx_builtin_setvar_helper(bridge_channel->chan, "AGENT_STATUS", "NOT_CONNECTED");
+       ast_indicate(bridge_channel->chan, AST_CONTROL_RINGING);
+       return -1;
+}
+
+/*!
  * \brief Dialplan AgentRequest application to locate an agent to talk with.
  *
  * \param chan Channel wanting to talk with an agent.
@@ -1826,24 +1929,26 @@ static int agent_request_exec(struct ast_channel *chan, const char *data)
                return -1;
        }
 
+       /* Setup the alert agent on caller joining the bridge hook. */
+       ao2_ref(agent, +1);
+       if (ast_bridge_join_hook(&caller_features, caller_joined_bridge, agent,
+               __ao2_cleanup, 0)) {
+               ao2_ref(agent, -1);
+               ast_bridge_features_cleanup(&caller_features);
+               return -1;
+       }
+
        caller_bridge = ast_bridge_basic_new();
        if (!caller_bridge) {
                ast_bridge_features_cleanup(&caller_features);
                return -1;
        }
 
-       /* Get COLP for agent. */
-       ast_party_connected_line_init(&connected);
-       ast_channel_lock(chan);
-       ast_connected_line_copy_from_caller(&connected, ast_channel_caller(chan));
-       ast_channel_unlock(chan);
-
        agent_lock(agent);
        switch (agent->state) {
        case AGENT_STATE_LOGGED_OUT:
        case AGENT_STATE_LOGGING_OUT:
                agent_unlock(agent);
-               ast_party_connected_line_free(&connected);
                ast_bridge_destroy(caller_bridge, 0);
                ast_bridge_features_cleanup(&caller_features);
                ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
@@ -1857,7 +1962,6 @@ static int agent_request_exec(struct ast_channel *chan, const char *data)
                break;
        default:
                agent_unlock(agent);
-               ast_party_connected_line_free(&connected);
                ast_bridge_destroy(caller_bridge, 0);
                ast_bridge_features_cleanup(&caller_features);
                ast_verb(3, "Agent '%s' is busy.\n", agent->username);
@@ -1867,38 +1971,55 @@ static int agent_request_exec(struct ast_channel *chan, const char *data)
        agent_unlock(agent);
        agent_devstate_changed(agent->username);
 
+       /* Get COLP for agent. */
+       ast_party_connected_line_init(&connected);
+       ast_channel_lock(chan);
+       ast_connected_line_copy_from_caller(&connected, ast_channel_caller(chan));
+       ast_channel_unlock(chan);
+
        logged = agent_bridge_channel_get_lock(agent);
        if (!logged) {
                ast_party_connected_line_free(&connected);
+               caller_abort_agent(agent);
                ast_bridge_destroy(caller_bridge, 0);
                ast_bridge_features_cleanup(&caller_features);
                ast_verb(3, "Agent '%s' not logged in.\n", agent->username);
                pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "NOT_LOGGED_IN");
-               caller_abort_agent(agent);
                return 0;
        }
 
        send_colp_to_agent(logged, &connected);
-       ast_party_connected_line_free(&connected);
-
-       res = send_alert_to_agent(logged, agent->username);
        ast_bridge_channel_unlock(logged);
        ao2_ref(logged, -1);
-       if (res) {
-               ast_bridge_destroy(caller_bridge, 0);
-               ast_bridge_features_cleanup(&caller_features);
-               ast_verb(3, "Agent '%s': Failed to alert the agent.\n", agent->username);
-               pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ERROR");
+       ast_party_connected_line_free(&connected);
+
+       if (ast_bridge_join(caller_bridge, chan, NULL, &caller_features, NULL,
+               AST_BRIDGE_JOIN_PASS_REFERENCE)) {
                caller_abort_agent(agent);
-               return 0;
+               ast_verb(3, "Agent '%s': Caller %s failed to join the bridge.\n",
+                       agent->username, ast_channel_name(chan));
+               pbx_builtin_setvar_helper(chan, "AGENT_STATUS", "ERROR");
        }
-
-       ast_indicate(chan, AST_CONTROL_RINGING);
-       ast_bridge_join(caller_bridge, chan, NULL, &caller_features, NULL,
-               AST_BRIDGE_JOIN_PASS_REFERENCE);
        ast_bridge_features_cleanup(&caller_features);
 
-       return -1;
+       /* Determine if we need to continue in the dialplan after the bridge. */
+       ast_channel_lock(chan);
+       if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
+               /*
+                * The bridge was broken for a hangup that isn't real.
+                * Don't run the h extension, because the channel isn't
+                * really hung up.  This should really only happen with
+                * AST_SOFTHANGUP_ASYNCGOTO.
+                */
+               res = 0;
+       } else {
+               res = ast_check_hangup(chan)
+                       || ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
+                       || ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENT_STATUS"));
+       }
+       ast_channel_unlock(chan);
+
+       return res ? -1 : 0;
 }
 
 /*!
@@ -2040,14 +2161,13 @@ static int agent_login_exec(struct ast_channel *chan, const char *data)
 
        agent_login_channel_config(agent, chan);
 
-       if (!ast_test_flag(&opts, OPT_SILENT)
-               && !ast_streamfile(chan, "agent-loginok", ast_channel_language(chan))) {
-               ast_waitstream(chan, "");
+       if (!ast_test_flag(&opts, OPT_SILENT)) {
+               ast_stream_and_wait(chan, "agent-loginok", AST_DIGIT_NONE);
        }
 
        ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", agent->username,
-               ast_getformatname(ast_channel_readformat(chan)),
-               ast_getformatname(ast_channel_writeformat(chan)));
+               ast_format_get_name(ast_channel_readformat(chan)),
+               ast_format_get_name(ast_channel_writeformat(chan)));
        ast_channel_lock(chan);
        send_agent_login(chan, agent->username);
        ast_channel_unlock(chan);
@@ -2406,13 +2526,14 @@ static int action_agents(struct mansession *s, const struct message *m)
        struct ao2_iterator iter;
        struct agent_pvt *agent;
        struct ast_str *out = ast_str_alloca(4096);
+       int num_agents = 0;
 
        if (!ast_strlen_zero(id)) {
                snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
        } else {
                id_text[0] = '\0';
        }
-       astman_send_ack(s, m, "Agents will follow");
+       astman_send_listack(s, m, "Agents will follow", "start");
 
        iter = ao2_iterator_init(agents, 0);
        for (; (agent = ao2_iterator_next(&iter)); ao2_ref(agent, -1)) {
@@ -2467,12 +2588,12 @@ static int action_agents(struct mansession *s, const struct message *m)
                astman_append(s, "Event: Agents\r\n"
                        "%s%s\r\n",
                        ast_str_buffer(out), id_text);
+               ++num_agents;
        }
        ao2_iterator_destroy(&iter);
 
-       astman_append(s, "Event: AgentsComplete\r\n"
-               "%s"
-               "\r\n", id_text);
+       astman_send_list_complete_start(s, m, "AgentsComplete", num_agents);
+       astman_send_list_complete_end(s);
        return 0;
 }
 
@@ -2522,7 +2643,7 @@ static int unload_module(void)
        }
 
        destroy_config();
-       ao2_ref(agents, -1);
+       ao2_cleanup(agents);
        agents = NULL;
        return 0;
 }
@@ -2536,12 +2657,6 @@ static int load_module(void)
        if (!agents) {
                return AST_MODULE_LOAD_FAILURE;
        }
-       if (load_config()) {
-               ast_log(LOG_ERROR, "Unable to load config. Not loading module.\n");
-               ao2_ref(agents, -1);
-               agents = NULL;
-               return AST_MODULE_LOAD_DECLINE;
-       }
 
        /* Init agent holding bridge v_table. */
        bridge_init_agent_hold();
@@ -2567,6 +2682,13 @@ static int load_module(void)
                unload_module();
                return AST_MODULE_LOAD_FAILURE;
        }
+
+       if (load_config()) {
+               ast_log(LOG_ERROR, "Unable to load config. Not loading module.\n");
+               unload_module();
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
        return AST_MODULE_LOAD_SUCCESS;
 }
 
@@ -2580,6 +2702,7 @@ static int reload(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Call center agent pool applications",
+       .support_level = AST_MODULE_SUPPORT_CORE,
        .load = load_module,
        .unload = unload_module,
        .reload = reload,