13f080ebb26dc58faf4b33686119bb27bda7ff31
[asterisk/asterisk.git] / channels / chan_agent.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19
20 /*! \file
21  * 
22  * \brief Implementation of Agents (proxy channel)
23  *
24  * \author Mark Spencer <markster@digium.com>
25  *
26  * This file is the implementation of Agents modules.
27  * It is a dynamic module that is loaded by Asterisk. 
28  * \par See also
29  * \arg \ref Config_agent
30  *
31  * \ingroup channel_drivers
32  */
33 /*** MODULEINFO
34         <depend>chan_local</depend>
35         <depend>res_monitor</depend>
36         <support_level>core</support_level>
37  ***/
38
39 #include "asterisk.h"
40
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42
43 #include <sys/socket.h>
44 #include <fcntl.h>
45 #include <netdb.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <sys/signal.h>
49
50 #include "asterisk/lock.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/config.h"
53 #include "asterisk/module.h"
54 #include "asterisk/pbx.h"
55 #include "asterisk/sched.h"
56 #include "asterisk/io.h"
57 #include "asterisk/acl.h"
58 #include "asterisk/callerid.h"
59 #include "asterisk/file.h"
60 #include "asterisk/cli.h"
61 #include "asterisk/app.h"
62 #include "asterisk/musiconhold.h"
63 #include "asterisk/manager.h"
64 #include "asterisk/features.h"
65 #include "asterisk/utils.h"
66 #include "asterisk/causes.h"
67 #include "asterisk/astdb.h"
68 #include "asterisk/devicestate.h"
69 #include "asterisk/monitor.h"
70 #include "asterisk/stringfields.h"
71 #include "asterisk/event.h"
72 #include "asterisk/data.h"
73
74 /*** DOCUMENTATION
75         <application name="AgentLogin" language="en_US">
76                 <synopsis>
77                         Call agent login.
78                 </synopsis>
79                 <syntax>
80                         <parameter name="AgentNo" />
81                         <parameter name="options">
82                                 <optionlist>
83                                         <option name="s">
84                                                 <para>silent login - do not announce the login ok segment after
85                                                 agent logged on/off</para>
86                                         </option>
87                                 </optionlist>
88                         </parameter>
89                 </syntax>
90                 <description>
91                         <para>Asks the agent to login to the system. Always returns <literal>-1</literal>.
92                         While logged in, the agent can receive calls and will hear a <literal>beep</literal>
93                         when a new call comes in. The agent can dump the call by pressing the star key.</para>
94                 </description>
95                 <see-also>
96                         <ref type="application">Queue</ref>
97                         <ref type="application">AddQueueMember</ref>
98                         <ref type="application">RemoveQueueMember</ref>
99                         <ref type="application">PauseQueueMember</ref>
100                         <ref type="application">UnpauseQueueMember</ref>
101                         <ref type="function">AGENT</ref>
102                         <ref type="filename">agents.conf</ref>
103                         <ref type="filename">queues.conf</ref>
104                 </see-also>
105         </application>
106         <application name="AgentMonitorOutgoing" language="en_US">
107                 <synopsis>
108                         Record agent's outgoing call.
109                 </synopsis>
110                 <syntax>
111                         <parameter name="options">
112                                 <optionlist>
113                                         <option name="d">
114                                                 <para>make the app return <literal>-1</literal> if there is an error condition.</para>
115                                         </option>
116                                         <option name="c">
117                                                 <para>change the CDR so that the source of the call is
118                                                 <literal>Agent/agent_id</literal></para>
119                                         </option>
120                                         <option name="n">
121                                                 <para>don't generate the warnings when there is no callerid or the
122                                                 agentid is not known. It's handy if you want to have one context
123                                                 for agent and non-agent calls.</para>
124                                         </option>
125                                 </optionlist>
126                         </parameter>
127                 </syntax>
128                 <description>
129                         <para>Tries to figure out the id of the agent who is placing outgoing call based on
130                         comparison of the callerid of the current interface and the global variable
131                         placed by the AgentCallbackLogin application. That's why it should be used only
132                         with the AgentCallbackLogin app. Uses the monitoring functions in chan_agent
133                         instead of Monitor application. That has to be configured in the
134                         <filename>agents.conf</filename> file.</para>
135                         <para>Normally the app returns <literal>0</literal> unless the options are passed.</para>
136                 </description>
137                 <see-also>
138                         <ref type="filename">agents.conf</ref>
139                 </see-also>
140         </application>
141         <function name="AGENT" language="en_US">
142                 <synopsis>
143                         Gets information about an Agent
144                 </synopsis>
145                 <syntax argsep=":">
146                         <parameter name="agentid" required="true" />
147                         <parameter name="item">
148                                 <para>The valid items to retrieve are:</para>
149                                 <enumlist>
150                                         <enum name="status">
151                                                 <para>(default) The status of the agent (LOGGEDIN | LOGGEDOUT)</para>
152                                         </enum>
153                                         <enum name="password">
154                                                 <para>The password of the agent</para>
155                                         </enum>
156                                         <enum name="name">
157                                                 <para>The name of the agent</para>
158                                         </enum>
159                                         <enum name="mohclass">
160                                                 <para>MusicOnHold class</para>
161                                         </enum>
162                                         <enum name="channel">
163                                                 <para>The name of the active channel for the Agent (AgentLogin)</para>
164                                         </enum>
165                                         <enum name="fullchannel">
166                                                 <para>The untruncated name of the active channel for the Agent (AgentLogin)</para>
167                                         </enum>
168                                 </enumlist>
169                         </parameter>
170                 </syntax>
171                 <description></description>
172         </function>
173         <manager name="Agents" language="en_US">
174                 <synopsis>
175                         Lists agents and their status.
176                 </synopsis>
177                 <syntax>
178                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
179                 </syntax>
180                 <description>
181                         <para>Will list info about all possible agents.</para>
182                 </description>
183         </manager>
184         <manager name="AgentLogoff" language="en_US">
185                 <synopsis>
186                         Sets an agent as no longer logged in.
187                 </synopsis>
188                 <syntax>
189                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
190                         <parameter name="Agent" required="true">
191                                 <para>Agent ID of the agent to log off.</para>
192                         </parameter>
193                         <parameter name="Soft">
194                                 <para>Set to <literal>true</literal> to not hangup existing calls.</para>
195                         </parameter>
196                 </syntax>
197                 <description>
198                         <para>Sets an agent as no longer logged in.</para>
199                 </description>
200         </manager>
201  ***/
202
203 static const char tdesc[] = "Call Agent Proxy Channel";
204 static const char config[] = "agents.conf";
205
206 static const char app[] = "AgentLogin";
207 static const char app3[] = "AgentMonitorOutgoing";
208
209 static char moh[80] = "default";
210
211 #define AST_MAX_AGENT   80                          /*!< Agent ID or Password max length */
212 #define AST_MAX_BUF     256
213 #define AST_MAX_FILENAME_LEN    256
214
215 static const char pa_family[] = "Agents";          /*!< Persistent Agents astdb family */
216 #define PA_MAX_LEN 2048                             /*!< The maximum length of each persistent member agent database entry */
217
218 #define DEFAULT_ACCEPTDTMF '#'
219 #define DEFAULT_ENDDTMF '*'
220
221 static ast_group_t group;
222 static int autologoff;
223 static int wrapuptime;
224 static int ackcall;
225 static int endcall;
226 static int multiplelogin = 1;
227 static int autologoffunavail = 0;
228 static char acceptdtmf = DEFAULT_ACCEPTDTMF;
229 static char enddtmf = DEFAULT_ENDDTMF;
230
231 static int maxlogintries = 3;
232 static char agentgoodbye[AST_MAX_FILENAME_LEN] = "vm-goodbye";
233
234 static int recordagentcalls = 0;
235 static char recordformat[AST_MAX_BUF] = "";
236 static char recordformatext[AST_MAX_BUF] = "";
237 static char urlprefix[AST_MAX_BUF] = "";
238 static char savecallsin[AST_MAX_BUF] = "";
239 static int updatecdr = 0;
240 static char beep[AST_MAX_BUF] = "beep";
241
242 #define GETAGENTBYCALLERID      "AGENTBYCALLERID"
243
244 enum {
245         AGENT_FLAG_ACKCALL = (1 << 0),
246         AGENT_FLAG_AUTOLOGOFF = (1 << 1),
247         AGENT_FLAG_WRAPUPTIME = (1 << 2),
248         AGENT_FLAG_ACCEPTDTMF = (1 << 3),
249         AGENT_FLAG_ENDDTMF = (1 << 4),
250 };
251
252 /*! \brief Structure representing an agent.  */
253 struct agent_pvt {
254         ast_mutex_t lock;              /*!< Channel private lock */
255         int dead;                      /*!< Poised for destruction? */
256         int pending;                   /*!< Not a real agent -- just pending a match */
257         int abouttograb;               /*!< About to grab */
258         int autologoff;                /*!< Auto timeout time */
259         int ackcall;                   /*!< ackcall */
260         int deferlogoff;               /*!< Defer logoff to hangup */
261         char acceptdtmf;
262         char enddtmf;
263         time_t loginstart;             /*!< When agent first logged in (0 when logged off) */
264         time_t start;                  /*!< When call started */
265         struct timeval lastdisc;       /*!< When last disconnected */
266         int wrapuptime;                /*!< Wrapup time in ms */
267         ast_group_t group;             /*!< Group memberships */
268         int acknowledged;              /*!< Acknowledged */
269         char moh[80];                  /*!< Which music on hold */
270         char agent[AST_MAX_AGENT];     /*!< Agent ID */
271         char password[AST_MAX_AGENT];  /*!< Password for Agent login */
272         char name[AST_MAX_AGENT];
273         int app_lock_flag;
274         ast_cond_t app_complete_cond;
275         ast_cond_t login_wait_cond;
276         volatile int app_sleep_cond;   /**< Sleep condition for the login app */
277         struct ast_channel *owner;     /**< Agent */
278         char logincallerid[80];        /**< Caller ID they had when they logged in */
279         struct ast_channel *chan;      /**< Channel we use */
280         unsigned int flags;            /**< Flags show if settings were applied with channel vars */
281         AST_LIST_ENTRY(agent_pvt) list; /**< Next Agent in the linked list. */
282 };
283
284 #define DATA_EXPORT_AGENT(MEMBER)                               \
285         MEMBER(agent_pvt, autologoff, AST_DATA_INTEGER)         \
286         MEMBER(agent_pvt, ackcall, AST_DATA_BOOLEAN)            \
287         MEMBER(agent_pvt, deferlogoff, AST_DATA_BOOLEAN)        \
288         MEMBER(agent_pvt, wrapuptime, AST_DATA_MILLISECONDS)    \
289         MEMBER(agent_pvt, acknowledged, AST_DATA_BOOLEAN)       \
290         MEMBER(agent_pvt, name, AST_DATA_STRING)                \
291         MEMBER(agent_pvt, password, AST_DATA_PASSWORD)          \
292         MEMBER(agent_pvt, acceptdtmf, AST_DATA_CHARACTER)       \
293         MEMBER(agent_pvt, logincallerid, AST_DATA_STRING)
294
295 AST_DATA_STRUCTURE(agent_pvt, DATA_EXPORT_AGENT);
296
297 static AST_LIST_HEAD_STATIC(agents, agent_pvt); /*!< Holds the list of agents (loaded form agents.conf). */
298
299 #define CHECK_FORMATS(ast, p) do { \
300         if (p->chan) {\
301                 if (!(ast_format_cap_identical(ast->nativeformats, p->chan->nativeformats))) { \
302                         char tmp1[256], tmp2[256]; \
303                         ast_debug(1, "Native formats changing from '%s' to '%s'\n", ast_getformatname_multiple(tmp1, sizeof(tmp1), ast->nativeformats), ast_getformatname_multiple(tmp2, sizeof(tmp2), p->chan->nativeformats)); \
304                         /* Native formats changed, reset things */ \
305                         ast_format_cap_copy(ast->nativeformats, p->chan->nativeformats); \
306                         ast_debug(1, "Resetting read to '%s' and write to '%s'\n", ast_getformatname(&ast->readformat), ast_getformatname(&ast->writeformat));\
307                         ast_set_read_format(ast, &ast->readformat); \
308                         ast_set_write_format(ast, &ast->writeformat); \
309                 } \
310                 if ((ast_format_cmp(&p->chan->readformat, &ast->rawreadformat) != AST_FORMAT_CMP_EQUAL) && !p->chan->generator)  \
311                         ast_set_read_format(p->chan, &ast->rawreadformat); \
312                 if ((ast_format_cmp(&p->chan->writeformat, &ast->rawwriteformat) != AST_FORMAT_CMP_EQUAL) && !p->chan->generator) \
313                         ast_set_write_format(p->chan, &ast->rawwriteformat); \
314         } \
315 } while(0)
316
317 /*! \brief Cleanup moves all the relevant FD's from the 2nd to the first, but retains things
318    properly for a timingfd XXX This might need more work if agents were logged in as agents or other
319    totally impractical combinations XXX */
320
321 #define CLEANUP(ast, p) do { \
322         int x; \
323         if (p->chan) { \
324                 for (x=0;x<AST_MAX_FDS;x++) {\
325                         if (x != AST_TIMING_FD) \
326                                 ast_channel_set_fd(ast, x, p->chan->fds[x]); \
327                 } \
328                 ast_channel_set_fd(ast, AST_AGENT_FD, p->chan->fds[AST_TIMING_FD]); \
329         } \
330 } while(0)
331
332 /*--- Forward declarations */
333 static struct ast_channel *agent_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
334 static int agent_devicestate(void *data);
335 static int agent_digit_begin(struct ast_channel *ast, char digit);
336 static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
337 static int agent_call(struct ast_channel *ast, char *dest, int timeout);
338 static int agent_hangup(struct ast_channel *ast);
339 static int agent_answer(struct ast_channel *ast);
340 static struct ast_frame *agent_read(struct ast_channel *ast);
341 static int agent_write(struct ast_channel *ast, struct ast_frame *f);
342 static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
343 static int agent_sendtext(struct ast_channel *ast, const char *text);
344 static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
345 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
346 static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
347 static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state);
348 static struct ast_channel* agent_get_base_channel(struct ast_channel *chan);
349 static int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base);
350 static int agent_logoff(const char *agent, int soft);
351
352 /*! \brief Channel interface description for PBX integration */
353 static struct ast_channel_tech agent_tech = {
354         .type = "Agent",
355         .description = tdesc,
356         .requester = agent_request,
357         .devicestate = agent_devicestate,
358         .send_digit_begin = agent_digit_begin,
359         .send_digit_end = agent_digit_end,
360         .call = agent_call,
361         .hangup = agent_hangup,
362         .answer = agent_answer,
363         .read = agent_read,
364         .write = agent_write,
365         .write_video = agent_write,
366         .send_html = agent_sendhtml,
367         .send_text = agent_sendtext,
368         .exception = agent_read,
369         .indicate = agent_indicate,
370         .fixup = agent_fixup,
371         .bridged_channel = agent_bridgedchannel,
372         .get_base_channel = agent_get_base_channel,
373         .set_base_channel = agent_set_base_channel,
374 };
375
376 /*!
377  * Adds an agent to the global list of agents.
378  *
379  * \param agent A string with the username, password and real name of an agent. As defined in agents.conf. Example: "13,169,John Smith"
380  * \param pending If it is pending or not.
381  * @return The just created agent.
382  * \sa agent_pvt, agents.
383  */
384 static struct agent_pvt *add_agent(const char *agent, int pending)
385 {
386         char *parse;
387         AST_DECLARE_APP_ARGS(args,
388                 AST_APP_ARG(agt);
389                 AST_APP_ARG(password);
390                 AST_APP_ARG(name);
391         );
392         char *password = NULL;
393         char *name = NULL;
394         char *agt = NULL;
395         struct agent_pvt *p;
396
397         parse = ast_strdupa(agent);
398
399         /* Extract username (agt), password and name from agent (args). */
400         AST_STANDARD_APP_ARGS(args, parse);
401
402         if(args.argc == 0) {
403                 ast_log(LOG_WARNING, "A blank agent line!\n");
404                 return NULL;
405         }
406
407         if(ast_strlen_zero(args.agt) ) {
408                 ast_log(LOG_WARNING, "An agent line with no agentid!\n");
409                 return NULL;
410         } else
411                 agt = args.agt;
412
413         if(!ast_strlen_zero(args.password)) {
414                 password = args.password;
415                 while (*password && *password < 33) password++;
416         }
417         if(!ast_strlen_zero(args.name)) {
418                 name = args.name;
419                 while (*name && *name < 33) name++;
420         }
421         
422         /* Are we searching for the agent here ? To see if it exists already ? */
423         AST_LIST_TRAVERSE(&agents, p, list) {
424                 if (!pending && !strcmp(p->agent, agt))
425                         break;
426         }
427         if (!p) {
428                 // Build the agent.
429                 if (!(p = ast_calloc(1, sizeof(*p))))
430                         return NULL;
431                 ast_copy_string(p->agent, agt, sizeof(p->agent));
432                 ast_mutex_init(&p->lock);
433                 ast_cond_init(&p->app_complete_cond, NULL);
434                 ast_cond_init(&p->login_wait_cond, NULL);
435                 p->app_lock_flag = 0;
436                 p->app_sleep_cond = 1;
437                 p->group = group;
438                 p->pending = pending;
439                 AST_LIST_INSERT_TAIL(&agents, p, list);
440         }
441         
442         ast_copy_string(p->password, password ? password : "", sizeof(p->password));
443         ast_copy_string(p->name, name ? name : "", sizeof(p->name));
444         ast_copy_string(p->moh, moh, sizeof(p->moh));
445         if (!ast_test_flag(p, AGENT_FLAG_ACKCALL)) {
446                 p->ackcall = ackcall;
447         }
448         if (!ast_test_flag(p, AGENT_FLAG_AUTOLOGOFF)) {
449                 p->autologoff = autologoff;
450         }
451         if (!ast_test_flag(p, AGENT_FLAG_ACCEPTDTMF)) {
452                 p->acceptdtmf = acceptdtmf;
453         }
454         if (!ast_test_flag(p, AGENT_FLAG_ENDDTMF)) {
455                 p->enddtmf = enddtmf;
456         }
457
458         /* If someone reduces the wrapuptime and reloads, we want it
459          * to change the wrapuptime immediately on all calls */
460         if (!ast_test_flag(p, AGENT_FLAG_WRAPUPTIME) && p->wrapuptime > wrapuptime) {
461                 struct timeval now = ast_tvnow();
462                 /* XXX check what is this exactly */
463
464                 /* We won't be pedantic and check the tv_usec val */
465                 if (p->lastdisc.tv_sec > (now.tv_sec + wrapuptime/1000)) {
466                         p->lastdisc.tv_sec = now.tv_sec + wrapuptime/1000;
467                         p->lastdisc.tv_usec = now.tv_usec;
468                 }
469         }
470         p->wrapuptime = wrapuptime;
471
472         if (pending)
473                 p->dead = 1;
474         else
475                 p->dead = 0;
476         return p;
477 }
478
479 /*!
480  * Deletes an agent after doing some clean up.
481  * Further documentation: How safe is this function ? What state should the agent be to be cleaned.
482  * \param p Agent to be deleted.
483  * \returns Always 0.
484  */
485 static int agent_cleanup(struct agent_pvt *p)
486 {
487         struct ast_channel *chan = NULL;
488         ast_mutex_lock(&p->lock);
489         chan = p->owner;
490         p->owner = NULL;
491         chan->tech_pvt = NULL;
492         /* Release ownership of the agent to other threads (presumably running the login app). */
493         p->app_sleep_cond = 1;
494         p->app_lock_flag = 0;
495         ast_cond_signal(&p->app_complete_cond);
496         if (chan) {
497                 chan = ast_channel_release(chan);
498         }
499         if (p->dead) {
500                 ast_mutex_unlock(&p->lock);
501                 ast_mutex_destroy(&p->lock);
502                 ast_cond_destroy(&p->app_complete_cond);
503                 ast_cond_destroy(&p->login_wait_cond);
504                 ast_free(p);
505         }
506         return 0;
507 }
508
509 static int check_availability(struct agent_pvt *newlyavailable, int needlock);
510
511 static int agent_answer(struct ast_channel *ast)
512 {
513         ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
514         return -1;
515 }
516
517 static int __agent_start_monitoring(struct ast_channel *ast, struct agent_pvt *p, int needlock)
518 {
519         char tmp[AST_MAX_BUF],tmp2[AST_MAX_BUF], *pointer;
520         char filename[AST_MAX_BUF];
521         int res = -1;
522         if (!p)
523                 return -1;
524         if (!ast->monitor) {
525                 snprintf(filename, sizeof(filename), "agent-%s-%s",p->agent, ast->uniqueid);
526                 /* substitute . for - */
527                 if ((pointer = strchr(filename, '.')))
528                         *pointer = '-';
529                 snprintf(tmp, sizeof(tmp), "%s%s", savecallsin, filename);
530                 ast_monitor_start(ast, recordformat, tmp, needlock, X_REC_IN | X_REC_OUT);
531                 ast_monitor_setjoinfiles(ast, 1);
532                 snprintf(tmp2, sizeof(tmp2), "%s%s.%s", urlprefix, filename, recordformatext);
533 #if 0
534                 ast_verbose("name is %s, link is %s\n",tmp, tmp2);
535 #endif
536                 if (!ast->cdr)
537                         ast->cdr = ast_cdr_alloc();
538                 ast_cdr_setuserfield(ast, tmp2);
539                 res = 0;
540         } else
541                 ast_log(LOG_ERROR, "Recording already started on that call.\n");
542         return res;
543 }
544
545 static int agent_start_monitoring(struct ast_channel *ast, int needlock)
546 {
547         return __agent_start_monitoring(ast, ast->tech_pvt, needlock);
548 }
549
550 static struct ast_frame *agent_read(struct ast_channel *ast)
551 {
552         struct agent_pvt *p = ast->tech_pvt;
553         struct ast_frame *f = NULL;
554         static struct ast_frame answer_frame = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
555         int cur_time = time(NULL);
556         ast_mutex_lock(&p->lock);
557         CHECK_FORMATS(ast, p);
558         if (!p->start) {
559                 p->start = cur_time;
560         }
561         if (p->chan) {
562                 ast_copy_flags(p->chan, ast, AST_FLAG_EXCEPTION);
563                 p->chan->fdno = (ast->fdno == AST_AGENT_FD) ? AST_TIMING_FD : ast->fdno;
564                 f = ast_read(p->chan);
565         } else
566                 f = &ast_null_frame;
567         if (!f) {
568                 /* If there's a channel, make it NULL */
569                 if (p->chan) {
570                         p->chan->_bridge = NULL;
571                         p->chan = NULL;
572                         ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "Agent/%s", p->agent);
573                         p->acknowledged = 0;
574                 }
575         } else {
576                 /* if acknowledgement is not required, and the channel is up, we may have missed
577                         an AST_CONTROL_ANSWER (if there was one), so mark the call acknowledged anyway */
578                 if (!p->ackcall && !p->acknowledged && p->chan && (p->chan->_state == AST_STATE_UP)) {
579                         p->acknowledged = 1;
580                 }
581
582                 if (!p->acknowledged) {
583                         int howlong = cur_time - p->start;
584                         if (p->autologoff && (howlong >= p->autologoff)) {
585                                 ast_log(LOG_NOTICE, "Agent '%s' didn't answer/confirm within %d seconds (waited %d)\n", p->name, p->autologoff, howlong);
586                                 if (p->owner || p->chan) {
587                                         while (p->owner && ast_channel_trylock(p->owner)) {
588                                                 DEADLOCK_AVOIDANCE(&p->lock);
589                                         }
590                                         if (p->owner) {
591                                                 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
592                                                 ast_channel_unlock(p->owner);
593                                         }
594
595                                         while (p->chan && ast_channel_trylock(p->chan)) {
596                                                 DEADLOCK_AVOIDANCE(&p->lock);
597                                         }
598                                         if (p->chan) {
599                                                 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
600                                                 ast_channel_unlock(p->chan);
601                                         }
602                                 }
603                         }
604                 }
605                 switch (f->frametype) {
606                 case AST_FRAME_CONTROL:
607                         if (f->subclass.integer == AST_CONTROL_ANSWER) {
608                                 if (p->ackcall) {
609                                         ast_verb(3, "%s answered, waiting for '%c' to acknowledge\n", p->chan->name, p->acceptdtmf);
610                                         /* Don't pass answer along */
611                                         ast_frfree(f);
612                                         f = &ast_null_frame;
613                                 } else {
614                                         p->acknowledged = 1;
615                                         /* Use the builtin answer frame for the 
616                                            recording start check below. */
617                                         ast_frfree(f);
618                                         f = &answer_frame;
619                                 }
620                         }
621                         break;
622                 case AST_FRAME_DTMF_BEGIN:
623                         /*ignore DTMF begin's as it can cause issues with queue announce files*/
624                         if((!p->acknowledged && f->subclass.integer == p->acceptdtmf) || (f->subclass.integer == p->enddtmf && endcall)){
625                                 ast_frfree(f);
626                                 f = &ast_null_frame;
627                         }
628                         break;
629                 case AST_FRAME_DTMF_END:
630                         if (!p->acknowledged && (f->subclass.integer == p->acceptdtmf)) {
631                                 ast_verb(3, "%s acknowledged\n", p->chan->name);
632                                 p->acknowledged = 1;
633                                 ast_frfree(f);
634                                 f = &answer_frame;
635                         } else if (f->subclass.integer == p->enddtmf && endcall) {
636                                 /* terminates call */
637                                 ast_frfree(f);
638                                 f = NULL;
639                         }
640                         break;
641                 case AST_FRAME_VOICE:
642                 case AST_FRAME_VIDEO:
643                         /* don't pass voice or video until the call is acknowledged */
644                         if (!p->acknowledged) {
645                                 ast_frfree(f);
646                                 f = &ast_null_frame;
647                         }
648                 default:
649                         /* pass everything else on through */
650                         break;
651                 }
652         }
653
654         CLEANUP(ast,p);
655         if (p->chan && !p->chan->_bridge) {
656                 if (strcasecmp(p->chan->tech->type, "Local")) {
657                         p->chan->_bridge = ast;
658                         if (p->chan)
659                                 ast_debug(1, "Bridge on '%s' being set to '%s' (3)\n", p->chan->name, p->chan->_bridge->name);
660                 }
661         }
662         ast_mutex_unlock(&p->lock);
663         if (recordagentcalls && f == &answer_frame)
664                 agent_start_monitoring(ast,0);
665         return f;
666 }
667
668 static int agent_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
669 {
670         struct agent_pvt *p = ast->tech_pvt;
671         int res = -1;
672         ast_mutex_lock(&p->lock);
673         if (p->chan) 
674                 res = ast_channel_sendhtml(p->chan, subclass, data, datalen);
675         ast_mutex_unlock(&p->lock);
676         return res;
677 }
678
679 static int agent_sendtext(struct ast_channel *ast, const char *text)
680 {
681         struct agent_pvt *p = ast->tech_pvt;
682         int res = -1;
683         ast_mutex_lock(&p->lock);
684         if (p->chan) 
685                 res = ast_sendtext(p->chan, text);
686         ast_mutex_unlock(&p->lock);
687         return res;
688 }
689
690 static int agent_write(struct ast_channel *ast, struct ast_frame *f)
691 {
692         struct agent_pvt *p = ast->tech_pvt;
693         int res = -1;
694         CHECK_FORMATS(ast, p);
695         ast_mutex_lock(&p->lock);
696         if (!p->chan) 
697                 res = 0;
698         else {
699                 if ((f->frametype != AST_FRAME_VOICE) ||
700                     (f->frametype != AST_FRAME_VIDEO) ||
701                     (ast_format_cmp(&f->subclass.format, &p->chan->writeformat) != AST_FORMAT_CMP_NOT_EQUAL)) {
702                         res = ast_write(p->chan, f);
703                 } else {
704                         ast_debug(1, "Dropping one incompatible %s frame on '%s' to '%s'\n", 
705                                 f->frametype == AST_FRAME_VOICE ? "audio" : "video",
706                                 ast->name, p->chan->name);
707                         res = 0;
708                 }
709         }
710         CLEANUP(ast, p);
711         ast_mutex_unlock(&p->lock);
712         return res;
713 }
714
715 static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
716 {
717         struct agent_pvt *p = newchan->tech_pvt;
718         ast_mutex_lock(&p->lock);
719         if (p->owner != oldchan) {
720                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
721                 ast_mutex_unlock(&p->lock);
722                 return -1;
723         }
724         p->owner = newchan;
725         ast_mutex_unlock(&p->lock);
726         return 0;
727 }
728
729 static int agent_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
730 {
731         struct agent_pvt *p = ast->tech_pvt;
732         int res = -1;
733         ast_mutex_lock(&p->lock);
734         if (p->chan && !ast_check_hangup(p->chan)) {
735                 while (ast_channel_trylock(p->chan)) {
736                         int res;
737                         if ((res = ast_channel_unlock(ast))) {
738                                 ast_log(LOG_ERROR, "chan_agent bug! Channel was not locked upon entry to agent_indicate: %s\n", strerror(res));
739                                 ast_mutex_unlock(&p->lock);
740                                 return -1;
741                         }
742                         usleep(1);
743                         ast_channel_lock(ast);
744                 }
745                 res = p->chan->tech->indicate ? p->chan->tech->indicate(p->chan, condition, data, datalen) : -1;
746                 ast_channel_unlock(p->chan);
747         } else
748                 res = 0;
749         ast_mutex_unlock(&p->lock);
750         return res;
751 }
752
753 static int agent_digit_begin(struct ast_channel *ast, char digit)
754 {
755         struct agent_pvt *p = ast->tech_pvt;
756         ast_mutex_lock(&p->lock);
757         if (p->chan) {
758                 ast_senddigit_begin(p->chan, digit);
759         }
760         ast_mutex_unlock(&p->lock);
761         return 0;
762 }
763
764 static int agent_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
765 {
766         struct agent_pvt *p = ast->tech_pvt;
767         ast_mutex_lock(&p->lock);
768         if (p->chan) {
769                 ast_senddigit_end(p->chan, digit, duration);
770         }
771         ast_mutex_unlock(&p->lock);
772         return 0;
773 }
774
775 static int agent_call(struct ast_channel *ast, char *dest, int timeout)
776 {
777         struct agent_pvt *p = ast->tech_pvt;
778         int res = -1;
779         int newstate=0;
780         struct ast_channel *chan;
781
782         ast_mutex_lock(&p->lock);
783         p->acknowledged = 0;
784
785         if (p->pending) {
786                 ast_log(LOG_DEBUG, "Pretending to dial on pending agent\n");
787                 ast_mutex_unlock(&p->lock);
788                 ast_setstate(ast, AST_STATE_DIALING);
789                 return 0;
790         }
791
792         if (!p->chan) {
793                 ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
794                 ast_mutex_unlock(&p->lock);
795                 return res;
796         }
797         ast_verb(3, "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
798         ast_debug(3, "Playing beep, lang '%s'\n", p->chan->language);
799         
800         chan = p->chan;
801         ast_mutex_unlock(&p->lock);
802
803         res = ast_streamfile(chan, beep, chan->language);
804         ast_debug(3, "Played beep, result '%d'\n", res);
805         if (!res) {
806                 res = ast_waitstream(chan, "");
807                 ast_debug(3, "Waited for stream, result '%d'\n", res);
808         }
809         
810         ast_mutex_lock(&p->lock);
811         if (!p->chan) {
812                 /* chan went away while we were streaming, this shouldn't be possible */
813                 res = -1;
814         }
815
816         if (!res) {
817                 struct ast_format tmpfmt;
818                 res = ast_set_read_format_from_cap(p->chan, p->chan->nativeformats);
819                 ast_debug(3, "Set read format, result '%d'\n", res);
820                 if (res)
821                         ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
822         } else {
823                 /* Agent hung-up */
824                 p->chan = NULL;
825                 ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "Agent/%s", p->agent);
826         }
827
828         if (!res) {
829                 struct ast_format tmpfmt;
830                 res = ast_set_write_format_from_cap(p->chan, p->chan->nativeformats);
831                 ast_debug(3, "Set write format, result '%d'\n", res);
832                 if (res)
833                         ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
834         }
835         if(!res) {
836                 /* Call is immediately up, or might need ack */
837                 if (p->ackcall) {
838                         newstate = AST_STATE_RINGING;
839                 } else {
840                         newstate = AST_STATE_UP;
841                         if (recordagentcalls)
842                                 agent_start_monitoring(ast, 0);
843                         p->acknowledged = 1;
844                 }
845                 res = 0;
846         }
847         CLEANUP(ast, p);
848         ast_mutex_unlock(&p->lock);
849         if (newstate)
850                 ast_setstate(ast, newstate);
851         return res;
852 }
853
854 /*! \brief return the channel or base channel if one exists.  This function assumes the channel it is called on is already locked */
855 struct ast_channel* agent_get_base_channel(struct ast_channel *chan)
856 {
857         struct agent_pvt *p = NULL;
858         struct ast_channel *base = chan;
859
860         /* chan is locked by the calling function */
861         if (!chan || !chan->tech_pvt) {
862                 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)chan->tech_pvt:(long)NULL);
863                 return NULL;
864         }
865         p = chan->tech_pvt;
866         if (p->chan) 
867                 base = p->chan;
868         return base;
869 }
870
871 int agent_set_base_channel(struct ast_channel *chan, struct ast_channel *base)
872 {
873         struct agent_pvt *p = NULL;
874         
875         if (!chan || !base) {
876                 ast_log(LOG_ERROR, "whoa, you need a channel (0x%ld) and a base channel (0x%ld) for setting.\n", (long)chan, (long)base);
877                 return -1;
878         }
879         p = chan->tech_pvt;
880         if (!p) {
881                 ast_log(LOG_ERROR, "whoa, channel %s is missing his tech_pvt structure!!.\n", chan->name);
882                 return -1;
883         }
884         p->chan = base;
885         return 0;
886 }
887
888 static int agent_hangup(struct ast_channel *ast)
889 {
890         struct agent_pvt *p = ast->tech_pvt;
891
892         ast_mutex_lock(&p->lock);
893         p->owner = NULL;
894         ast->tech_pvt = NULL;
895         p->app_sleep_cond = 1;
896         p->acknowledged = 0;
897
898         /* Release ownership of the agent to other threads (presumably running the login app). */
899         p->app_lock_flag = 0;
900         ast_cond_signal(&p->app_complete_cond);
901
902         /* if they really are hung up then set start to 0 so the test
903          * later if we're called on an already downed channel
904          * doesn't cause an agent to be logged out like when
905          * agent_request() is followed immediately by agent_hangup()
906          * as in apps/app_chanisavail.c:chanavail_exec()
907          */
908
909         ast_debug(1, "Hangup called for state %s\n", ast_state2str(ast->_state));
910         if (p->start && (ast->_state != AST_STATE_UP)) {
911                 p->start = 0;
912         } else
913                 p->start = 0; 
914         if (p->chan) {
915                 p->chan->_bridge = NULL;
916                 /* If they're dead, go ahead and hang up on the agent now */
917                 if (p->dead) {
918                         ast_channel_lock(p->chan);
919                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
920                         ast_channel_unlock(p->chan);
921                 } else if (p->loginstart) {
922                         ast_channel_lock(p->chan);
923                         ast_indicate_data(p->chan, AST_CONTROL_HOLD, 
924                                 S_OR(p->moh, NULL),
925                                 !ast_strlen_zero(p->moh) ? strlen(p->moh) + 1 : 0);
926                         ast_channel_unlock(p->chan);
927                 }
928         }
929         ast_mutex_unlock(&p->lock);
930
931         /* Only register a device state change if the agent is still logged in */
932         if (!p->loginstart) {
933                 p->logincallerid[0] = '\0';
934         } else {
935                 ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Agent/%s", p->agent);
936         }
937
938         if (p->pending) {
939                 AST_LIST_LOCK(&agents);
940                 AST_LIST_REMOVE(&agents, p, list);
941                 AST_LIST_UNLOCK(&agents);
942         }
943         if (p->abouttograb) {
944                 /* Let the "about to grab" thread know this isn't valid anymore, and let it
945                    kill it later */
946                 p->abouttograb = 0;
947         } else if (p->dead) {
948                 ast_mutex_destroy(&p->lock);
949                 ast_cond_destroy(&p->app_complete_cond);
950                 ast_cond_destroy(&p->login_wait_cond);
951                 ast_free(p);
952         } else {
953                 if (p->chan) {
954                         /* Not dead -- check availability now */
955                         ast_mutex_lock(&p->lock);
956                         /* Store last disconnect time */
957                         p->lastdisc = ast_tvadd(ast_tvnow(), ast_samp2tv(p->wrapuptime, 1000));
958                         ast_mutex_unlock(&p->lock);
959                 }
960         }
961         return 0;
962 }
963
964 static int agent_cont_sleep( void *data )
965 {
966         struct agent_pvt *p;
967         int res;
968
969         p = (struct agent_pvt *)data;
970
971         ast_mutex_lock(&p->lock);
972         res = p->app_sleep_cond;
973         if (p->lastdisc.tv_sec) {
974                 if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) 
975                         res = 1;
976         }
977         ast_mutex_unlock(&p->lock);
978
979         if (!res)
980                 ast_debug(5, "agent_cont_sleep() returning %d\n", res );
981
982         return res;
983 }
984
985 static int agent_ack_sleep(void *data)
986 {
987         struct agent_pvt *p;
988         int res=0;
989         int to = 1000;
990         struct ast_frame *f;
991
992         /* Wait a second and look for something */
993
994         p = (struct agent_pvt *) data;
995         if (!p->chan) 
996                 return -1;
997
998         for(;;) {
999                 to = ast_waitfor(p->chan, to);
1000                 if (to < 0) 
1001                         return -1;
1002                 if (!to) 
1003                         return 0;
1004                 f = ast_read(p->chan);
1005                 if (!f) 
1006                         return -1;
1007                 if (f->frametype == AST_FRAME_DTMF)
1008                         res = f->subclass.integer;
1009                 else
1010                         res = 0;
1011                 ast_frfree(f);
1012                 ast_mutex_lock(&p->lock);
1013                 if (!p->app_sleep_cond) {
1014                         ast_mutex_unlock(&p->lock);
1015                         return 0;
1016                 } else if (res == p->acceptdtmf) {
1017                         ast_mutex_unlock(&p->lock);
1018                         return 1;
1019                 }
1020                 ast_mutex_unlock(&p->lock);
1021                 res = 0;
1022         }
1023         return res;
1024 }
1025
1026 static struct ast_channel *agent_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
1027 {
1028         struct agent_pvt *p = bridge->tech_pvt;
1029         struct ast_channel *ret = NULL;
1030
1031         if (p) {
1032                 if (chan == p->chan)
1033                         ret = bridge->_bridge;
1034                 else if (chan == bridge->_bridge)
1035                         ret = p->chan;
1036         }
1037
1038         ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning '%s'\n", chan->name, bridge->name, ret ? ret->name : "<none>");
1039         return ret;
1040 }
1041
1042 /*! \brief Create new agent channel */
1043 static struct ast_channel *agent_new(struct agent_pvt *p, int state, const char *linkedid)
1044 {
1045         struct ast_channel *tmp;
1046 #if 0
1047         if (!p->chan) {
1048                 ast_log(LOG_WARNING, "No channel? :(\n");
1049                 return NULL;
1050         }
1051 #endif  
1052         if (p->pending)
1053                 tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? p->chan->exten:"", p->chan ? p->chan->context:"", linkedid, 0, "Agent/P%s-%d", p->agent, (int) ast_random() & 0xffff);
1054         else
1055                 tmp = ast_channel_alloc(0, state, 0, 0, "", p->chan ? p->chan->exten:"", p->chan ? p->chan->context:"", linkedid, 0, "Agent/%s", p->agent);
1056         if (!tmp) {
1057                 ast_log(LOG_WARNING, "Unable to allocate agent channel structure\n");
1058                 return NULL;
1059         }
1060
1061         tmp->tech = &agent_tech;
1062         if (p->chan) {
1063                 ast_format_cap_copy(tmp->nativeformats, p->chan->nativeformats);
1064                 ast_format_copy(&tmp->writeformat, &p->chan->writeformat);
1065                 ast_format_copy(&tmp->rawwriteformat, &p->chan->writeformat);
1066                 ast_format_copy(&tmp->readformat, &p->chan->readformat);
1067                 ast_format_copy(&tmp->rawreadformat, &p->chan->readformat);
1068                 ast_string_field_set(tmp, language, p->chan->language);
1069                 ast_copy_string(tmp->context, p->chan->context, sizeof(tmp->context));
1070                 ast_copy_string(tmp->exten, p->chan->exten, sizeof(tmp->exten));
1071                 /* XXX Is this really all we copy form the originating channel?? */
1072         } else {
1073                 ast_format_set(&tmp->writeformat, AST_FORMAT_SLINEAR, 0);
1074                 ast_format_set(&tmp->rawwriteformat, AST_FORMAT_SLINEAR, 0);
1075                 ast_format_set(&tmp->readformat, AST_FORMAT_SLINEAR, 0);
1076                 ast_format_set(&tmp->rawreadformat, AST_FORMAT_SLINEAR, 0);
1077                 ast_format_cap_add(tmp->nativeformats, &tmp->writeformat);
1078         }
1079         /* Safe, agentlock already held */
1080         tmp->tech_pvt = p;
1081         p->owner = tmp;
1082         tmp->priority = 1;
1083         return tmp;
1084 }
1085
1086
1087 /*!
1088  * Read configuration data. The file named agents.conf.
1089  *
1090  * \returns Always 0, or so it seems.
1091  */
1092 static int read_agent_config(int reload)
1093 {
1094         struct ast_config *cfg;
1095         struct ast_config *ucfg;
1096         struct ast_variable *v;
1097         struct agent_pvt *p;
1098         const char *catname;
1099         const char *hasagent;
1100         int genhasagent;
1101         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1102
1103         group = 0;
1104         autologoff = 0;
1105         wrapuptime = 0;
1106         ackcall = 0;
1107         endcall = 1;
1108         cfg = ast_config_load(config, config_flags);
1109         if (!cfg) {
1110                 ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
1111                 return 0;
1112         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
1113                 return -1;
1114         } else if (cfg == CONFIG_STATUS_FILEINVALID) {
1115                 ast_log(LOG_ERROR, "%s contains a parsing error.  Aborting\n", config);
1116                 return 0;
1117         }
1118         if ((ucfg = ast_config_load("users.conf", config_flags))) {
1119                 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
1120                         ucfg = NULL;
1121                 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
1122                         ast_log(LOG_ERROR, "users.conf contains a parsing error.  Aborting\n");
1123                         return 0;
1124                 }
1125         }
1126
1127         AST_LIST_LOCK(&agents);
1128         AST_LIST_TRAVERSE(&agents, p, list) {
1129                 p->dead = 1;
1130         }
1131         strcpy(moh, "default");
1132         /* set the default recording values */
1133         recordagentcalls = 0;
1134         strcpy(recordformat, "wav");
1135         strcpy(recordformatext, "wav");
1136         urlprefix[0] = '\0';
1137         savecallsin[0] = '\0';
1138
1139         /* Read in [general] section for persistence */
1140         multiplelogin = ast_true(ast_variable_retrieve(cfg, "general", "multiplelogin"));
1141
1142         /* Read in the [agents] section */
1143         v = ast_variable_browse(cfg, "agents");
1144         while(v) {
1145                 /* Create the interface list */
1146                 if (!strcasecmp(v->name, "agent")) {
1147                         add_agent(v->value, 0);
1148                 } else if (!strcasecmp(v->name, "group")) {
1149                         group = ast_get_group(v->value);
1150                 } else if (!strcasecmp(v->name, "autologoff")) {
1151                         autologoff = atoi(v->value);
1152                         if (autologoff < 0)
1153                                 autologoff = 0;
1154                 } else if (!strcasecmp(v->name, "ackcall")) {
1155                         if (ast_true(v->value) || !strcasecmp(v->value, "always")) {
1156                                 ackcall = 1;
1157                         }
1158                 } else if (!strcasecmp(v->name, "endcall")) {
1159                         endcall = ast_true(v->value);
1160                 } else if (!strcasecmp(v->name, "acceptdtmf")) {
1161                         acceptdtmf = *(v->value);
1162                         ast_log(LOG_NOTICE, "Set acceptdtmf to %c\n", acceptdtmf);
1163                 } else if (!strcasecmp(v->name, "enddtmf")) {
1164                         enddtmf = *(v->value);
1165                 } else if (!strcasecmp(v->name, "wrapuptime")) {
1166                         wrapuptime = atoi(v->value);
1167                         if (wrapuptime < 0)
1168                                 wrapuptime = 0;
1169                 } else if (!strcasecmp(v->name, "maxlogintries") && !ast_strlen_zero(v->value)) {
1170                         maxlogintries = atoi(v->value);
1171                         if (maxlogintries < 0)
1172                                 maxlogintries = 0;
1173                 } else if (!strcasecmp(v->name, "goodbye") && !ast_strlen_zero(v->value)) {
1174                         strcpy(agentgoodbye,v->value);
1175                 } else if (!strcasecmp(v->name, "musiconhold")) {
1176                         ast_copy_string(moh, v->value, sizeof(moh));
1177                 } else if (!strcasecmp(v->name, "updatecdr")) {
1178                         if (ast_true(v->value))
1179                                 updatecdr = 1;
1180                         else
1181                                 updatecdr = 0;
1182                 } else if (!strcasecmp(v->name, "autologoffunavail")) {
1183                         if (ast_true(v->value))
1184                                 autologoffunavail = 1;
1185                         else
1186                                 autologoffunavail = 0;
1187                 } else if (!strcasecmp(v->name, "recordagentcalls")) {
1188                         recordagentcalls = ast_true(v->value);
1189                 } else if (!strcasecmp(v->name, "recordformat")) {
1190                         ast_copy_string(recordformat, v->value, sizeof(recordformat));
1191                         if (!strcasecmp(v->value, "wav49"))
1192                                 strcpy(recordformatext, "WAV");
1193                         else
1194                                 ast_copy_string(recordformatext, v->value, sizeof(recordformatext));
1195                 } else if (!strcasecmp(v->name, "urlprefix")) {
1196                         ast_copy_string(urlprefix, v->value, sizeof(urlprefix));
1197                         if (urlprefix[strlen(urlprefix) - 1] != '/')
1198                                 strncat(urlprefix, "/", sizeof(urlprefix) - strlen(urlprefix) - 1);
1199                 } else if (!strcasecmp(v->name, "savecallsin")) {
1200                         if (v->value[0] == '/')
1201                                 ast_copy_string(savecallsin, v->value, sizeof(savecallsin));
1202                         else
1203                                 snprintf(savecallsin, sizeof(savecallsin) - 2, "/%s", v->value);
1204                         if (savecallsin[strlen(savecallsin) - 1] != '/')
1205                                 strncat(savecallsin, "/", sizeof(savecallsin) - strlen(savecallsin) - 1);
1206                 } else if (!strcasecmp(v->name, "custom_beep")) {
1207                         ast_copy_string(beep, v->value, sizeof(beep));
1208                 }
1209                 v = v->next;
1210         }
1211         if (ucfg) {
1212                 genhasagent = ast_true(ast_variable_retrieve(ucfg, "general", "hasagent"));
1213                 catname = ast_category_browse(ucfg, NULL);
1214                 while(catname) {
1215                         if (strcasecmp(catname, "general")) {
1216                                 hasagent = ast_variable_retrieve(ucfg, catname, "hasagent");
1217                                 if (ast_true(hasagent) || (!hasagent && genhasagent)) {
1218                                         char tmp[256];
1219                                         const char *fullname = ast_variable_retrieve(ucfg, catname, "fullname");
1220                                         const char *secret = ast_variable_retrieve(ucfg, catname, "secret");
1221                                         if (!fullname)
1222                                                 fullname = "";
1223                                         if (!secret)
1224                                                 secret = "";
1225                                         snprintf(tmp, sizeof(tmp), "%s,%s,%s", catname, secret,fullname);
1226                                         add_agent(tmp, 0);
1227                                 }
1228                         }
1229                         catname = ast_category_browse(ucfg, catname);
1230                 }
1231                 ast_config_destroy(ucfg);
1232         }
1233         AST_LIST_TRAVERSE_SAFE_BEGIN(&agents, p, list) {
1234                 if (p->dead) {
1235                         AST_LIST_REMOVE_CURRENT(list);
1236                         /* Destroy if  appropriate */
1237                         if (!p->owner) {
1238                                 if (!p->chan) {
1239                                         ast_mutex_destroy(&p->lock);
1240                                         ast_cond_destroy(&p->app_complete_cond);
1241                                         ast_cond_destroy(&p->login_wait_cond);
1242                                         ast_free(p);
1243                                 } else {
1244                                         /* Cause them to hang up */
1245                                         ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
1246                                 }
1247                         }
1248                 }
1249         }
1250         AST_LIST_TRAVERSE_SAFE_END;
1251         AST_LIST_UNLOCK(&agents);
1252         ast_config_destroy(cfg);
1253         return 1;
1254 }
1255
1256 static int check_availability(struct agent_pvt *newlyavailable, int needlock)
1257 {
1258         struct ast_channel *chan=NULL, *parent=NULL;
1259         struct agent_pvt *p;
1260         int res;
1261
1262         ast_debug(1, "Checking availability of '%s'\n", newlyavailable->agent);
1263         if (needlock)
1264                 AST_LIST_LOCK(&agents);
1265         AST_LIST_TRAVERSE(&agents, p, list) {
1266                 if (p == newlyavailable) {
1267                         continue;
1268                 }
1269                 ast_mutex_lock(&p->lock);
1270                 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
1271                         ast_debug(1, "Call '%s' looks like a winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
1272                         /* We found a pending call, time to merge */
1273                         chan = agent_new(newlyavailable, AST_STATE_DOWN, p->owner ? p->owner->linkedid : NULL);
1274                         parent = p->owner;
1275                         p->abouttograb = 1;
1276                         ast_mutex_unlock(&p->lock);
1277                         break;
1278                 }
1279                 ast_mutex_unlock(&p->lock);
1280         }
1281         if (needlock)
1282                 AST_LIST_UNLOCK(&agents);
1283         if (parent && chan)  {
1284                 if (newlyavailable->ackcall) {
1285                         /* Don't do beep here */
1286                         res = 0;
1287                 } else {
1288                         ast_debug(3, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
1289                         res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
1290                         ast_debug(3, "Played beep, result '%d'\n", res);
1291                         if (!res) {
1292                                 res = ast_waitstream(newlyavailable->chan, "");
1293                                 ast_debug(1, "Waited for stream, result '%d'\n", res);
1294                         }
1295                 }
1296                 if (!res) {
1297                         /* Note -- parent may have disappeared */
1298                         if (p->abouttograb) {
1299                                 newlyavailable->acknowledged = 1;
1300                                 /* Safe -- agent lock already held */
1301                                 ast_setstate(parent, AST_STATE_UP);
1302                                 ast_setstate(chan, AST_STATE_UP);
1303                                 ast_copy_string(parent->context, chan->context, sizeof(parent->context));
1304                                 ast_channel_masquerade(parent, chan);
1305                                 ast_hangup(chan);
1306                                 p->abouttograb = 0;
1307                         } else {
1308                                 ast_debug(1, "Sneaky, parent disappeared in the mean time...\n");
1309                                 agent_cleanup(newlyavailable);
1310                         }
1311                 } else {
1312                         ast_debug(1, "Ugh...  Agent hung up at exactly the wrong time\n");
1313                         agent_cleanup(newlyavailable);
1314                 }
1315         }
1316         return 0;
1317 }
1318
1319 static int check_beep(struct agent_pvt *newlyavailable, int needlock)
1320 {
1321         struct agent_pvt *p;
1322         int res=0;
1323
1324         ast_debug(1, "Checking beep availability of '%s'\n", newlyavailable->agent);
1325         if (needlock)
1326                 AST_LIST_LOCK(&agents);
1327         AST_LIST_TRAVERSE(&agents, p, list) {
1328                 if (p == newlyavailable) {
1329                         continue;
1330                 }
1331                 ast_mutex_lock(&p->lock);
1332                 if (!p->abouttograb && p->pending && ((p->group && (newlyavailable->group & p->group)) || !strcmp(p->agent, newlyavailable->agent))) {
1333                         ast_debug(1, "Call '%s' looks like a would-be winner for agent '%s'\n", p->owner->name, newlyavailable->agent);
1334                         ast_mutex_unlock(&p->lock);
1335                         break;
1336                 }
1337                 ast_mutex_unlock(&p->lock);
1338         }
1339         if (needlock)
1340                 AST_LIST_UNLOCK(&agents);
1341         if (p) {
1342                 ast_mutex_unlock(&newlyavailable->lock);
1343                 ast_debug(3, "Playing beep, lang '%s'\n", newlyavailable->chan->language);
1344                 res = ast_streamfile(newlyavailable->chan, beep, newlyavailable->chan->language);
1345                 ast_debug(1, "Played beep, result '%d'\n", res);
1346                 if (!res) {
1347                         res = ast_waitstream(newlyavailable->chan, "");
1348                         ast_debug(1, "Waited for stream, result '%d'\n", res);
1349                 }
1350                 ast_mutex_lock(&newlyavailable->lock);
1351         }
1352         return res;
1353 }
1354
1355 /*! \brief Part of the Asterisk PBX interface */
1356 static struct ast_channel *agent_request(const char *type, struct ast_format_cap *cap, const struct ast_channel* requestor, void *data, int *cause)
1357 {
1358         struct agent_pvt *p;
1359         struct ast_channel *chan = NULL;
1360         char *s;
1361         ast_group_t groupmatch;
1362         int groupoff;
1363         int waitforagent=0;
1364         int hasagent = 0;
1365         struct timeval now;
1366
1367         s = data;
1368         if ((s[0] == '@') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
1369                 groupmatch = (1 << groupoff);
1370         } else if ((s[0] == ':') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
1371                 groupmatch = (1 << groupoff);
1372                 waitforagent = 1;
1373         } else 
1374                 groupmatch = 0;
1375
1376         /* Check actual logged in agents first */
1377         AST_LIST_LOCK(&agents);
1378         AST_LIST_TRAVERSE(&agents, p, list) {
1379                 ast_mutex_lock(&p->lock);
1380                 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
1381                         if (p->chan)
1382                                 hasagent++;
1383                         now = ast_tvnow();
1384                         if (!p->lastdisc.tv_sec || (now.tv_sec >= p->lastdisc.tv_sec)) {
1385                                 p->lastdisc = ast_tv(0, 0);
1386                                 /* Agent must be registered, but not have any active call, and not be in a waiting state */
1387                                 if (!p->owner && p->chan) {
1388                                         /* Fixed agent */
1389                                         chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
1390                                 }
1391                                 if (chan) {
1392                                         ast_mutex_unlock(&p->lock);
1393                                         break;
1394                                 }
1395                         }
1396                 }
1397                 ast_mutex_unlock(&p->lock);
1398         }
1399         if (!p) {
1400                 AST_LIST_TRAVERSE(&agents, p, list) {
1401                         ast_mutex_lock(&p->lock);
1402                         if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
1403                                 if (p->chan) {
1404                                         hasagent++;
1405                                 }
1406                                 now = ast_tvnow();
1407                                 if (!p->lastdisc.tv_sec || (now.tv_sec >= p->lastdisc.tv_sec)) {
1408                                         p->lastdisc = ast_tv(0, 0);
1409                                         /* Agent must be registered, but not have any active call, and not be in a waiting state */
1410                                         if (!p->owner && p->chan) {
1411                                                 /* Could still get a fixed agent */
1412                                                 chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
1413                                         }
1414                                         if (chan) {
1415                                                 ast_mutex_unlock(&p->lock);
1416                                                 break;
1417                                         }
1418                                 }
1419                         }
1420                         ast_mutex_unlock(&p->lock);
1421                 }
1422         }
1423
1424         if (!chan && waitforagent) {
1425                 /* No agent available -- but we're requesting to wait for one.
1426                    Allocate a place holder */
1427                 if (hasagent) {
1428                         ast_debug(1, "Creating place holder for '%s'\n", s);
1429                         p = add_agent(data, 1);
1430                         p->group = groupmatch;
1431                         chan = agent_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
1432                         if (!chan) 
1433                                 ast_log(LOG_WARNING, "Weird...  Fix this to drop the unused pending agent\n");
1434                 } else {
1435                         ast_debug(1, "Not creating place holder for '%s' since nobody logged in\n", s);
1436                 }
1437         }
1438         *cause = hasagent ? AST_CAUSE_BUSY : AST_CAUSE_UNREGISTERED;
1439         AST_LIST_UNLOCK(&agents);
1440
1441         if (chan) {
1442                 ast_mutex_lock(&p->lock);
1443                 if (p->pending) {
1444                         ast_mutex_unlock(&p->lock);
1445                         return chan;
1446                 }
1447
1448                 if (!p->chan) {
1449                         ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
1450                         *cause = AST_CAUSE_UNREGISTERED;
1451                         ast_mutex_unlock(&p->lock);
1452                         agent_hangup(chan);
1453                         return NULL;
1454                 }
1455
1456                 /* we need to take control of the channel from the login app
1457                  * thread */
1458                 p->app_sleep_cond = 0;
1459                 p->app_lock_flag = 1;
1460
1461                 ast_queue_frame(p->chan, &ast_null_frame);
1462                 ast_cond_wait(&p->login_wait_cond, &p->lock);
1463
1464                 if (!p->chan) {
1465                         ast_log(LOG_DEBUG, "Agent disconnected while we were connecting the call\n");
1466                         p->app_sleep_cond = 1;
1467                         p->app_lock_flag = 0;
1468                         ast_cond_signal(&p->app_complete_cond);
1469                         ast_mutex_unlock(&p->lock);
1470                         *cause = AST_CAUSE_UNREGISTERED;
1471                         agent_hangup(chan);
1472                         return NULL;
1473                 }
1474
1475                 ast_indicate(p->chan, AST_CONTROL_UNHOLD);
1476                 ast_mutex_unlock(&p->lock);
1477         }
1478
1479         return chan;
1480 }
1481
1482 static force_inline int powerof(unsigned int d)
1483 {
1484         int x = ffs(d);
1485
1486         if (x)
1487                 return x - 1;
1488
1489         return 0;
1490 }
1491
1492 /*!
1493  * Lists agents and their status to the Manager API.
1494  * It is registered on load_module() and it gets called by the manager backend.
1495  * \param s
1496  * \param m
1497  * \returns 
1498  * \sa action_agent_logoff(), load_module().
1499  */
1500 static int action_agents(struct mansession *s, const struct message *m)
1501 {
1502         const char *id = astman_get_header(m,"ActionID");
1503         char idText[256] = "";
1504         struct agent_pvt *p;
1505         char *username = NULL;
1506         char *loginChan = NULL;
1507         char *talkingto = NULL;
1508         char *talkingtoChan = NULL;
1509         char *status = NULL;
1510         struct ast_channel *bridge;
1511
1512         if (!ast_strlen_zero(id))
1513                 snprintf(idText, sizeof(idText) ,"ActionID: %s\r\n", id);
1514         astman_send_ack(s, m, "Agents will follow");
1515         AST_LIST_LOCK(&agents);
1516         AST_LIST_TRAVERSE(&agents, p, list) {
1517                 ast_mutex_lock(&p->lock);
1518
1519                 /* Status Values:
1520                    AGENT_LOGGEDOFF - Agent isn't logged in
1521                    AGENT_IDLE      - Agent is logged in, and waiting for call
1522                    AGENT_ONCALL    - Agent is logged in, and on a call
1523                    AGENT_UNKNOWN   - Don't know anything about agent. Shouldn't ever get this. */
1524
1525                 username = S_OR(p->name, "None");
1526
1527                 /* Set a default status. It 'should' get changed. */
1528                 status = "AGENT_UNKNOWN";
1529
1530                 if (p->chan) {
1531                         loginChan = ast_strdupa(p->chan->name);
1532                         if (p->owner && p->owner->_bridge) {
1533                                 talkingto = S_COR(p->chan->caller.id.number.valid,
1534                                         p->chan->caller.id.number.str, "n/a");
1535                                 ast_channel_lock(p->owner);
1536                                 if ((bridge = ast_bridged_channel(p->owner))) {
1537                                         talkingtoChan = ast_strdupa(bridge->name);
1538                                 } else {
1539                                         talkingtoChan = "n/a";
1540                                 }
1541                                 ast_channel_unlock(p->owner);
1542                                 status = "AGENT_ONCALL";
1543                         } else {
1544                                 talkingto = "n/a";
1545                                 talkingtoChan = "n/a";
1546                                 status = "AGENT_IDLE";
1547                         }
1548                 } else {
1549                         loginChan = "n/a";
1550                         talkingto = "n/a";
1551                         talkingtoChan = "n/a";
1552                         status = "AGENT_LOGGEDOFF";
1553                 }
1554
1555                 astman_append(s, "Event: Agents\r\n"
1556                         "Agent: %s\r\n"
1557                         "Name: %s\r\n"
1558                         "Status: %s\r\n"
1559                         "LoggedInChan: %s\r\n"
1560                         "LoggedInTime: %d\r\n"
1561                         "TalkingTo: %s\r\n"
1562                         "TalkingToChan: %s\r\n"
1563                         "%s"
1564                         "\r\n",
1565                         p->agent, username, status, loginChan, (int)p->loginstart, talkingto, talkingtoChan, idText);
1566                 ast_mutex_unlock(&p->lock);
1567         }
1568         AST_LIST_UNLOCK(&agents);
1569         astman_append(s, "Event: AgentsComplete\r\n"
1570                 "%s"
1571                 "\r\n",idText);
1572         return 0;
1573 }
1574
1575 static int agent_logoff(const char *agent, int soft)
1576 {
1577         struct agent_pvt *p;
1578         int ret = -1; /* Return -1 if no agent if found */
1579
1580         AST_LIST_LOCK(&agents);
1581         AST_LIST_TRAVERSE(&agents, p, list) {
1582                 if (!strcasecmp(p->agent, agent)) {
1583                         ret = 0;
1584                         if (p->owner || p->chan) {
1585                                 if (!soft) {
1586                                         ast_mutex_lock(&p->lock);
1587
1588                                         while (p->owner && ast_channel_trylock(p->owner)) {
1589                                                 DEADLOCK_AVOIDANCE(&p->lock);
1590                                         }
1591                                         if (p->owner) {
1592                                                 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
1593                                                 ast_channel_unlock(p->owner);
1594                                         }
1595
1596                                         while (p->chan && ast_channel_trylock(p->chan)) {
1597                                                 DEADLOCK_AVOIDANCE(&p->lock);
1598                                         }
1599                                         if (p->chan) {
1600                                                 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
1601                                                 ast_channel_unlock(p->chan);
1602                                         }
1603
1604                                         ast_mutex_unlock(&p->lock);
1605                                 } else
1606                                         p->deferlogoff = 1;
1607                         }
1608                         break;
1609                 }
1610         }
1611         AST_LIST_UNLOCK(&agents);
1612
1613         return ret;
1614 }
1615
1616 static char *agent_logoff_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1617 {
1618         int ret;
1619         const char *agent;
1620
1621         switch (cmd) {
1622         case CLI_INIT:
1623                 e->command = "agent logoff";
1624                 e->usage =
1625                         "Usage: agent logoff <channel> [soft]\n"
1626                         "       Sets an agent as no longer logged in.\n"
1627                         "       If 'soft' is specified, do not hangup existing calls.\n";
1628                 return NULL;
1629         case CLI_GENERATE:
1630                 return complete_agent_logoff_cmd(a->line, a->word, a->pos, a->n); 
1631         }
1632
1633         if (a->argc < 3 || a->argc > 4)
1634                 return CLI_SHOWUSAGE;
1635         if (a->argc == 4 && strcasecmp(a->argv[3], "soft"))
1636                 return CLI_SHOWUSAGE;
1637
1638         agent = a->argv[2] + 6;
1639         ret = agent_logoff(agent, a->argc == 4);
1640         if (ret == 0)
1641                 ast_cli(a->fd, "Logging out %s\n", agent);
1642
1643         return CLI_SUCCESS;
1644 }
1645
1646 /*!
1647  * Sets an agent as no longer logged in in the Manager API.
1648  * It is registered on load_module() and it gets called by the manager backend.
1649  * \param s
1650  * \param m
1651  * \returns 
1652  * \sa action_agents(), load_module().
1653  */
1654 static int action_agent_logoff(struct mansession *s, const struct message *m)
1655 {
1656         const char *agent = astman_get_header(m, "Agent");
1657         const char *soft_s = astman_get_header(m, "Soft"); /* "true" is don't hangup */
1658         int soft;
1659         int ret; /* return value of agent_logoff */
1660
1661         if (ast_strlen_zero(agent)) {
1662                 astman_send_error(s, m, "No agent specified");
1663                 return 0;
1664         }
1665
1666         soft = ast_true(soft_s) ? 1 : 0;
1667         ret = agent_logoff(agent, soft);
1668         if (ret == 0)
1669                 astman_send_ack(s, m, "Agent logged out");
1670         else
1671                 astman_send_error(s, m, "No such agent");
1672
1673         return 0;
1674 }
1675
1676 static char *complete_agent_logoff_cmd(const char *line, const char *word, int pos, int state)
1677 {
1678         char *ret = NULL;
1679
1680         if (pos == 2) {
1681                 struct agent_pvt *p;
1682                 char name[AST_MAX_AGENT];
1683                 int which = 0, len = strlen(word);
1684
1685                 AST_LIST_LOCK(&agents);
1686                 AST_LIST_TRAVERSE(&agents, p, list) {
1687                         snprintf(name, sizeof(name), "Agent/%s", p->agent);
1688                         if (!strncasecmp(word, name, len) && p->loginstart && ++which > state) {
1689                                 ret = ast_strdup(name);
1690                                 break;
1691                         }
1692                 }
1693                 AST_LIST_UNLOCK(&agents);
1694         } else if (pos == 3 && state == 0) 
1695                 return ast_strdup("soft");
1696         
1697         return ret;
1698 }
1699
1700 /*!
1701  * Show agents in cli.
1702  */
1703 static char *agents_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1704 {
1705         struct agent_pvt *p;
1706         char username[AST_MAX_BUF];
1707         char location[AST_MAX_BUF] = "";
1708         char talkingto[AST_MAX_BUF] = "";
1709         char music[AST_MAX_BUF];
1710         int count_agents = 0;           /*!< Number of agents configured */
1711         int online_agents = 0;          /*!< Number of online agents */
1712         int offline_agents = 0;         /*!< Number of offline agents */
1713
1714         switch (cmd) {
1715         case CLI_INIT:
1716                 e->command = "agent show";
1717                 e->usage =
1718                         "Usage: agent show\n"
1719                         "       Provides summary information on agents.\n";
1720                 return NULL;
1721         case CLI_GENERATE:
1722                 return NULL;
1723         }
1724
1725         if (a->argc != 2)
1726                 return CLI_SHOWUSAGE;
1727
1728         AST_LIST_LOCK(&agents);
1729         AST_LIST_TRAVERSE(&agents, p, list) {
1730                 ast_mutex_lock(&p->lock);
1731                 if (p->pending) {
1732                         if (p->group)
1733                                 ast_cli(a->fd, "-- Pending call to group %d\n", powerof(p->group));
1734                         else
1735                                 ast_cli(a->fd, "-- Pending call to agent %s\n", p->agent);
1736                 } else {
1737                         if (!ast_strlen_zero(p->name))
1738                                 snprintf(username, sizeof(username), "(%s) ", p->name);
1739                         else
1740                                 username[0] = '\0';
1741                         if (p->chan) {
1742                                 snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
1743                                 if (p->owner && ast_bridged_channel(p->owner))
1744                                         snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name);
1745                                  else 
1746                                         strcpy(talkingto, " is idle");
1747                                 online_agents++;
1748                         } else {
1749                                 strcpy(location, "not logged in");
1750                                 talkingto[0] = '\0';
1751                                 offline_agents++;
1752                         }
1753                         if (!ast_strlen_zero(p->moh))
1754                                 snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
1755                         ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent, 
1756                                 username, location, talkingto, music);
1757                         count_agents++;
1758                 }
1759                 ast_mutex_unlock(&p->lock);
1760         }
1761         AST_LIST_UNLOCK(&agents);
1762         if ( !count_agents ) 
1763                 ast_cli(a->fd, "No Agents are configured in %s\n",config);
1764         else 
1765                 ast_cli(a->fd, "%d agents configured [%d online , %d offline]\n",count_agents, online_agents, offline_agents);
1766         ast_cli(a->fd, "\n");
1767                         
1768         return CLI_SUCCESS;
1769 }
1770
1771
1772 static char *agents_show_online(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1773 {
1774         struct agent_pvt *p;
1775         char username[AST_MAX_BUF];
1776         char location[AST_MAX_BUF] = "";
1777         char talkingto[AST_MAX_BUF] = "";
1778         char music[AST_MAX_BUF];
1779         int count_agents = 0;           /* Number of agents configured */
1780         int online_agents = 0;          /* Number of online agents */
1781         int agent_status = 0;           /* 0 means offline, 1 means online */
1782
1783         switch (cmd) {
1784         case CLI_INIT:
1785                 e->command = "agent show online";
1786                 e->usage =
1787                         "Usage: agent show online\n"
1788                         "       Provides a list of all online agents.\n";
1789                 return NULL;
1790         case CLI_GENERATE:
1791                 return NULL;
1792         }
1793
1794         if (a->argc != 3)
1795                 return CLI_SHOWUSAGE;
1796
1797         AST_LIST_LOCK(&agents);
1798         AST_LIST_TRAVERSE(&agents, p, list) {
1799                 agent_status = 0;       /* reset it to offline */
1800                 ast_mutex_lock(&p->lock);
1801                 if (!ast_strlen_zero(p->name))
1802                         snprintf(username, sizeof(username), "(%s) ", p->name);
1803                 else
1804                         username[0] = '\0';
1805                 if (p->chan) {
1806                         snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
1807                         if (p->owner && ast_bridged_channel(p->owner)) 
1808                                 snprintf(talkingto, sizeof(talkingto), " talking to %s", ast_bridged_channel(p->owner)->name);
1809                         else 
1810                                 strcpy(talkingto, " is idle");
1811                         agent_status = 1;
1812                         online_agents++;
1813                 }
1814                 if (!ast_strlen_zero(p->moh))
1815                         snprintf(music, sizeof(music), " (musiconhold is '%s')", p->moh);
1816                 if (agent_status)
1817                         ast_cli(a->fd, "%-12.12s %s%s%s%s\n", p->agent, username, location, talkingto, music);
1818                 count_agents++;
1819                 ast_mutex_unlock(&p->lock);
1820         }
1821         AST_LIST_UNLOCK(&agents);
1822         if (!count_agents) 
1823                 ast_cli(a->fd, "No Agents are configured in %s\n", config);
1824         else
1825                 ast_cli(a->fd, "%d agents online\n", online_agents);
1826         ast_cli(a->fd, "\n");
1827         return CLI_SUCCESS;
1828 }
1829
1830 static const char agent_logoff_usage[] =
1831 "Usage: agent logoff <channel> [soft]\n"
1832 "       Sets an agent as no longer logged in.\n"
1833 "       If 'soft' is specified, do not hangup existing calls.\n";
1834
1835 static struct ast_cli_entry cli_agents[] = {
1836         AST_CLI_DEFINE(agents_show, "Show status of agents"),
1837         AST_CLI_DEFINE(agents_show_online, "Show all online agents"),
1838         AST_CLI_DEFINE(agent_logoff_cmd, "Sets an agent offline"),
1839 };
1840
1841 /*!
1842  * Called by the AgentLogin application (from the dial plan).
1843  * 
1844  * \brief Log in agent application.
1845  *
1846  * \param chan
1847  * \param data
1848  * \returns
1849  * \sa agentmonitoroutgoing_exec(), load_module().
1850  */
1851 static int login_exec(struct ast_channel *chan, const char *data)
1852 {
1853         int res=0;
1854         int tries = 0;
1855         int max_login_tries = maxlogintries;
1856         struct agent_pvt *p;
1857         struct ast_module_user *u;
1858         char user[AST_MAX_AGENT] = "";
1859         char pass[AST_MAX_AGENT];
1860         char agent[AST_MAX_AGENT] = "";
1861         char xpass[AST_MAX_AGENT] = "";
1862         char *errmsg;
1863         char *parse;
1864         AST_DECLARE_APP_ARGS(args,
1865                              AST_APP_ARG(agent_id);
1866                              AST_APP_ARG(options);
1867                              AST_APP_ARG(extension);
1868                 );
1869         const char *tmpoptions = NULL;
1870         int play_announcement = 1;
1871         char agent_goodbye[AST_MAX_FILENAME_LEN];
1872         int update_cdr = updatecdr;
1873         char *filename = "agent-loginok";
1874
1875         u = ast_module_user_add(chan);
1876
1877         parse = ast_strdupa(data);
1878
1879         AST_STANDARD_APP_ARGS(args, parse);
1880
1881         ast_copy_string(agent_goodbye, agentgoodbye, sizeof(agent_goodbye));
1882
1883         ast_channel_lock(chan);
1884         /* Set Channel Specific Login Overrides */
1885         if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTLMAXLOGINTRIES"))) {
1886                 max_login_tries = atoi(pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES"));
1887                 if (max_login_tries < 0)
1888                         max_login_tries = 0;
1889                 tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTMAXLOGINTRIES");
1890                 ast_verb(3, "Saw variable AGENTMAXLOGINTRIES=%s, setting max_login_tries to: %d on Channel '%s'.\n",tmpoptions,max_login_tries,chan->name);
1891         }
1892         if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR"))) {
1893                 if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR")))
1894                         update_cdr = 1;
1895                 else
1896                         update_cdr = 0;
1897                 tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTUPDATECDR");
1898                 ast_verb(3, "Saw variable AGENTUPDATECDR=%s, setting update_cdr to: %d on Channel '%s'.\n",tmpoptions,update_cdr,chan->name);
1899         }
1900         if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"))) {
1901                 strcpy(agent_goodbye, pbx_builtin_getvar_helper(chan, "AGENTGOODBYE"));
1902                 tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTGOODBYE");
1903                 ast_verb(3, "Saw variable AGENTGOODBYE=%s, setting agent_goodbye to: %s on Channel '%s'.\n",tmpoptions,agent_goodbye,chan->name);
1904         }
1905         ast_channel_unlock(chan);
1906         /* End Channel Specific Login Overrides */
1907         
1908         if (!ast_strlen_zero(args.options)) {
1909                 if (strchr(args.options, 's')) {
1910                         play_announcement = 0;
1911                 }
1912         }
1913
1914         if (chan->_state != AST_STATE_UP)
1915                 res = ast_answer(chan);
1916         if (!res) {
1917                 if (!ast_strlen_zero(args.agent_id))
1918                         ast_copy_string(user, args.agent_id, AST_MAX_AGENT);
1919                 else
1920                         res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
1921         }
1922         while (!res && (max_login_tries==0 || tries < max_login_tries)) {
1923                 tries++;
1924                 /* Check for password */
1925                 AST_LIST_LOCK(&agents);
1926                 AST_LIST_TRAVERSE(&agents, p, list) {
1927                         if (!strcmp(p->agent, user) && !p->pending)
1928                                 ast_copy_string(xpass, p->password, sizeof(xpass));
1929                 }
1930                 AST_LIST_UNLOCK(&agents);
1931                 if (!res) {
1932                         if (!ast_strlen_zero(xpass))
1933                                 res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
1934                         else
1935                                 pass[0] = '\0';
1936                 }
1937                 errmsg = "agent-incorrect";
1938
1939 #if 0
1940                 ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
1941 #endif          
1942
1943                 /* Check again for accuracy */
1944                 AST_LIST_LOCK(&agents);
1945                 AST_LIST_TRAVERSE(&agents, p, list) {
1946                         int unlock_channel = 1;
1947                         ast_channel_lock(chan);
1948                         ast_mutex_lock(&p->lock);
1949                         if (!strcmp(p->agent, user) &&
1950                             !strcmp(p->password, pass) && !p->pending) {
1951
1952                                 /* Ensure we can't be gotten until we're done */
1953                                 p->lastdisc = ast_tvnow();
1954                                 p->lastdisc.tv_sec++;
1955
1956                                 /* Set Channel Specific Agent Overrides */
1957                                 if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
1958                                         if (ast_true(pbx_builtin_getvar_helper(chan, "AGENTACKCALL"))) {
1959                                                 p->ackcall = 1;
1960                                         } else {
1961                                                 p->ackcall = 0;
1962                                         }
1963                                         tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTACKCALL");
1964                                         ast_verb(3, "Saw variable AGENTACKCALL=%s, setting ackcall to: %d for Agent '%s'.\n", tmpoptions, p->ackcall, p->agent);
1965                                         ast_set_flag(p, AGENT_FLAG_ACKCALL);
1966                                 } else {
1967                                         p->ackcall = ackcall;
1968                                 }
1969                                 if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"))) {
1970                                         p->autologoff = atoi(pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF"));
1971                                         if (p->autologoff < 0)
1972                                                 p->autologoff = 0;
1973                                         tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTAUTOLOGOFF");
1974                                         ast_verb(3, "Saw variable AGENTAUTOLOGOFF=%s, setting autologff to: %d for Agent '%s'.\n", tmpoptions, p->autologoff, p->agent);
1975                                         ast_set_flag(p, AGENT_FLAG_AUTOLOGOFF);
1976                                 } else {
1977                                         p->autologoff = autologoff;
1978                                 }
1979                                 if (!ast_strlen_zero(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"))) {
1980                                         p->wrapuptime = atoi(pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME"));
1981                                         if (p->wrapuptime < 0)
1982                                                 p->wrapuptime = 0;
1983                                         tmpoptions=pbx_builtin_getvar_helper(chan, "AGENTWRAPUPTIME");
1984                                         ast_verb(3, "Saw variable AGENTWRAPUPTIME=%s, setting wrapuptime to: %d for Agent '%s'.\n", tmpoptions, p->wrapuptime, p->agent);
1985                                         ast_set_flag(p, AGENT_FLAG_WRAPUPTIME);
1986                                 } else {
1987                                         p->wrapuptime = wrapuptime;
1988                                 }
1989                                 tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTACCEPTDTMF");
1990                                 if (!ast_strlen_zero(tmpoptions)) {
1991                                         p->acceptdtmf = *tmpoptions;
1992                                         ast_verb(3, "Saw variable AGENTACCEPTDTMF=%s, setting acceptdtmf to: %c for Agent '%s'.\n", tmpoptions, p->acceptdtmf, p->agent);
1993                                         ast_set_flag(p, AGENT_FLAG_ACCEPTDTMF);
1994                                 }
1995                                 tmpoptions = pbx_builtin_getvar_helper(chan, "AGENTENDDTMF");
1996                                 if (!ast_strlen_zero(tmpoptions)) {
1997                                         p->enddtmf = *tmpoptions;
1998                                         ast_verb(3, "Saw variable AGENTENDDTMF=%s, setting enddtmf to: %c for Agent '%s'.\n", tmpoptions, p->enddtmf, p->agent);
1999                                         ast_set_flag(p, AGENT_FLAG_ENDDTMF);
2000                                 }
2001                                 ast_channel_unlock(chan);
2002                                 unlock_channel = 0;
2003                                 /* End Channel Specific Agent Overrides */
2004                                 if (!p->chan) {
2005                                         long logintime;
2006                                         snprintf(agent, sizeof(agent), "Agent/%s", p->agent);
2007
2008                                         p->logincallerid[0] = '\0';
2009                                         p->acknowledged = 0;
2010                                         
2011                                         ast_mutex_unlock(&p->lock);
2012                                         AST_LIST_UNLOCK(&agents);
2013                                         if( !res && play_announcement==1 )
2014                                                 res = ast_streamfile(chan, filename, chan->language);
2015                                         if (!res)
2016                                                 ast_waitstream(chan, "");
2017                                         AST_LIST_LOCK(&agents);
2018                                         ast_mutex_lock(&p->lock);
2019                                         if (!res) {
2020                                                 struct ast_format tmpfmt;
2021                                                 res = ast_set_read_format_from_cap(chan, chan->nativeformats);
2022                                                 if (res) {
2023                                                         ast_log(LOG_WARNING, "Unable to set read format to %s\n", ast_getformatname(&tmpfmt));
2024                                                 }
2025                                         }
2026                                         if (!res) {
2027                                                 struct ast_format tmpfmt;
2028                                                 res = ast_set_write_format_from_cap(chan, chan->nativeformats);
2029                                                 if (res) {
2030                                                         ast_log(LOG_WARNING, "Unable to set write format to %s\n", ast_getformatname(&tmpfmt));
2031                                                 }
2032                                         }
2033                                         /* Check once more just in case */
2034                                         if (p->chan)
2035                                                 res = -1;
2036                                         if (!res) {
2037                                                 ast_indicate_data(chan, AST_CONTROL_HOLD, 
2038                                                         S_OR(p->moh, NULL), 
2039                                                         !ast_strlen_zero(p->moh) ? strlen(p->moh) + 1 : 0);
2040                                                 if (p->loginstart == 0)
2041                                                         time(&p->loginstart);
2042                                                 manager_event(EVENT_FLAG_AGENT, "Agentlogin",
2043                                                               "Agent: %s\r\n"
2044                                                               "Channel: %s\r\n"
2045                                                               "Uniqueid: %s\r\n",
2046                                                               p->agent, chan->name, chan->uniqueid);
2047                                                 if (update_cdr && chan->cdr)
2048                                                         snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
2049                                                 ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGIN", "%s", chan->name);
2050                                                 ast_verb(2, "Agent '%s' logged in (format %s/%s)\n", p->agent,
2051                                                                     ast_getformatname(&chan->readformat), ast_getformatname(&chan->writeformat));
2052                                                 /* Login this channel and wait for it to go away */
2053                                                 p->chan = chan;
2054                                                 if (p->ackcall) {
2055                                                         check_beep(p, 0);
2056                                                 } else {
2057                                                         check_availability(p, 0);
2058                                                 }
2059                                                 ast_mutex_unlock(&p->lock);
2060                                                 AST_LIST_UNLOCK(&agents);
2061                                                 ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Agent/%s", p->agent);
2062                                                 while (res >= 0) {
2063                                                         ast_mutex_lock(&p->lock);
2064                                                         if (p->deferlogoff && p->chan) {
2065                                                                 ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
2066                                                                 p->deferlogoff = 0;
2067                                                         }
2068                                                         if (p->chan != chan)
2069                                                                 res = -1;
2070                                                         ast_mutex_unlock(&p->lock);
2071                                                         /* Yield here so other interested threads can kick in. */
2072                                                         sched_yield();
2073                                                         if (res)
2074                                                                 break;
2075
2076                                                         AST_LIST_LOCK(&agents);
2077                                                         ast_mutex_lock(&p->lock);
2078                                                         if (p->lastdisc.tv_sec) {
2079                                                                 if (ast_tvdiff_ms(ast_tvnow(), p->lastdisc) > 0) {
2080                                                                         ast_debug(1, "Wrapup time for %s expired!\n", p->agent);
2081                                                                         p->lastdisc = ast_tv(0, 0);
2082                                                                         ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Agent/%s", p->agent);
2083                                                                         if (p->ackcall) {
2084                                                                                 check_beep(p, 0);
2085                                                                         } else {
2086                                                                                 check_availability(p, 0);
2087                                                                         }
2088                                                                 }
2089                                                         }
2090                                                         ast_mutex_unlock(&p->lock);
2091                                                         AST_LIST_UNLOCK(&agents);
2092
2093                                                         /*      Synchronize channel ownership between call to agent and itself. */
2094                                                         ast_mutex_lock(&p->lock);
2095                                                         if (p->app_lock_flag == 1) {
2096                                                                 ast_cond_signal(&p->login_wait_cond);
2097                                                                 ast_cond_wait(&p->app_complete_cond, &p->lock);
2098                                                         }
2099                                                         ast_mutex_unlock(&p->lock);
2100                                                         if (p->ackcall) {
2101                                                                 res = agent_ack_sleep(p);
2102                                                         } else {
2103                                                                 res = ast_safe_sleep_conditional( chan, 1000, agent_cont_sleep, p );
2104                                                         }
2105                                                         if (p->ackcall && (res == 1)) {
2106                                                                 AST_LIST_LOCK(&agents);
2107                                                                 ast_mutex_lock(&p->lock);
2108                                                                 check_availability(p, 0);
2109                                                                 ast_mutex_unlock(&p->lock);
2110                                                                 AST_LIST_UNLOCK(&agents);
2111                                                                 res = 0;
2112                                                         }
2113                                                         sched_yield();
2114                                                 }
2115                                                 ast_mutex_lock(&p->lock);
2116                                                 /* Log us off if appropriate */
2117                                                 if (p->chan == chan) {
2118                                                         p->chan = NULL;
2119                                                 }
2120
2121                                                 /* Synchronize channel ownership between call to agent and itself. */
2122                                                 if (p->app_lock_flag == 1) {
2123                                                         ast_cond_signal(&p->login_wait_cond);
2124                                                         ast_cond_wait(&p->app_complete_cond, &p->lock);
2125                                                 }
2126
2127                                                 if (res && p->owner)
2128                                                         ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
2129
2130                                                 p->acknowledged = 0;
2131                                                 logintime = time(NULL) - p->loginstart;
2132                                                 p->loginstart = 0;
2133                                                 ast_mutex_unlock(&p->lock);
2134                                                 manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
2135                                                               "Agent: %s\r\n"
2136                                                               "Logintime: %ld\r\n"
2137                                                               "Uniqueid: %s\r\n",
2138                                                               p->agent, logintime, chan->uniqueid);
2139                                                 ast_queue_log("NONE", chan->uniqueid, agent, "AGENTLOGOFF", "%s|%ld", chan->name, logintime);
2140                                                 ast_verb(2, "Agent '%s' logged out\n", p->agent);
2141                                                 /* If there is no owner, go ahead and kill it now */
2142                                                 ast_devstate_changed(AST_DEVICE_UNAVAILABLE, "Agent/%s", p->agent);
2143                                                 if (p->dead && !p->owner) {
2144                                                         ast_mutex_destroy(&p->lock);
2145                                                         ast_cond_destroy(&p->app_complete_cond);
2146                                                         ast_cond_destroy(&p->login_wait_cond);
2147                                                         ast_free(p);
2148                                                 }
2149                                         }
2150                                         else {
2151                                                 ast_mutex_unlock(&p->lock);
2152                                                 p = NULL;
2153                                         }
2154                                         res = -1;
2155                                 } else {
2156                                         ast_mutex_unlock(&p->lock);
2157                                         errmsg = "agent-alreadyon";
2158                                         p = NULL;
2159                                 }
2160                                 break;
2161                         }
2162                         ast_mutex_unlock(&p->lock);
2163                         if (unlock_channel) {
2164                                 ast_channel_unlock(chan);
2165                         }
2166                 }
2167                 if (!p)
2168                         AST_LIST_UNLOCK(&agents);
2169
2170                 if (!res && (max_login_tries==0 || tries < max_login_tries))
2171                         res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
2172         }
2173                 
2174         if (!res)
2175                 res = ast_safe_sleep(chan, 500);
2176
2177         ast_module_user_remove(u);
2178         
2179         return -1;
2180 }
2181
2182 /*!
2183  *  \brief Called by the AgentMonitorOutgoing application (from the dial plan).
2184  *
2185  * \param chan
2186  * \param data
2187  * \returns
2188  * \sa login_exec(), load_module().
2189  */
2190 static int agentmonitoroutgoing_exec(struct ast_channel *chan, const char *data)
2191 {
2192         int exitifnoagentid = 0;
2193         int nowarnings = 0;
2194         int changeoutgoing = 0;
2195         int res = 0;
2196         char agent[AST_MAX_AGENT];
2197
2198         if (data) {
2199                 if (strchr(data, 'd'))
2200                         exitifnoagentid = 1;
2201                 if (strchr(data, 'n'))
2202                         nowarnings = 1;
2203                 if (strchr(data, 'c'))
2204                         changeoutgoing = 1;
2205         }
2206         if (chan->caller.id.number.valid
2207                 && !ast_strlen_zero(chan->caller.id.number.str)) {
2208                 const char *tmp;
2209                 char agentvar[AST_MAX_BUF];
2210                 snprintf(agentvar, sizeof(agentvar), "%s_%s", GETAGENTBYCALLERID,
2211                         chan->caller.id.number.str);
2212                 if ((tmp = pbx_builtin_getvar_helper(NULL, agentvar))) {
2213                         struct agent_pvt *p;
2214                         ast_copy_string(agent, tmp, sizeof(agent));
2215                         AST_LIST_LOCK(&agents);
2216                         AST_LIST_TRAVERSE(&agents, p, list) {
2217                                 if (!strcasecmp(p->agent, tmp)) {
2218                                         if (changeoutgoing) snprintf(chan->cdr->channel, sizeof(chan->cdr->channel), "Agent/%s", p->agent);
2219                                         __agent_start_monitoring(chan, p, 1);
2220                                         break;
2221                                 }
2222                         }
2223                         AST_LIST_UNLOCK(&agents);
2224                         
2225                 } else {
2226                         res = -1;
2227                         if (!nowarnings)
2228                                 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);
2229                 }
2230         } else {
2231                 res = -1;
2232                 if (!nowarnings)
2233                         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");
2234         }
2235         if (res) {
2236                 if (exitifnoagentid)
2237                         return res;
2238         }
2239         return 0;
2240 }
2241
2242 /*! \brief Part of PBX channel interface */
2243 static int agent_devicestate(void *data)
2244 {
2245         struct agent_pvt *p;
2246         char *s;
2247         ast_group_t groupmatch;
2248         int groupoff;
2249         int res = AST_DEVICE_INVALID;
2250         
2251         s = data;
2252         if ((s[0] == '@') && (sscanf(s + 1, "%30d", &groupoff) == 1))
2253                 groupmatch = (1 << groupoff);
2254         else if ((s[0] == ':') && (sscanf(s + 1, "%30d", &groupoff) == 1)) {
2255                 groupmatch = (1 << groupoff);
2256         } else 
2257                 groupmatch = 0;
2258
2259         /* Check actual logged in agents first */
2260         AST_LIST_LOCK(&agents);
2261         AST_LIST_TRAVERSE(&agents, p, list) {
2262                 ast_mutex_lock(&p->lock);
2263                 if (!p->pending && ((groupmatch && (p->group & groupmatch)) || !strcmp(data, p->agent))) {
2264                         if (p->owner) {
2265                                 if (res != AST_DEVICE_INUSE)
2266                                         res = AST_DEVICE_BUSY;
2267                         } else {
2268                                 if (res == AST_DEVICE_BUSY)
2269                                         res = AST_DEVICE_INUSE;
2270                                 if (p->chan) {
2271                                         if (res == AST_DEVICE_INVALID)
2272                                                 res = AST_DEVICE_UNKNOWN;
2273                                 } else if (res == AST_DEVICE_INVALID)   
2274                                         res = AST_DEVICE_UNAVAILABLE;
2275                         }
2276                         if (!strcmp(data, p->agent)) {
2277                                 ast_mutex_unlock(&p->lock);
2278                                 break;
2279                         }
2280                 }
2281                 ast_mutex_unlock(&p->lock);
2282         }
2283         AST_LIST_UNLOCK(&agents);
2284         return res;
2285 }
2286
2287 /*!
2288  * \note This function expects the agent list to be locked
2289  */
2290 static struct agent_pvt *find_agent(char *agentid)
2291 {
2292         struct agent_pvt *cur;
2293
2294         AST_LIST_TRAVERSE(&agents, cur, list) {
2295                 if (!strcmp(cur->agent, agentid))
2296                         break;  
2297         }
2298
2299         return cur;     
2300 }
2301
2302 static int function_agent(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
2303 {
2304         char *parse;    
2305         AST_DECLARE_APP_ARGS(args,
2306                 AST_APP_ARG(agentid);
2307                 AST_APP_ARG(item);
2308         );
2309         char *tmp;
2310         struct agent_pvt *agent;
2311
2312         buf[0] = '\0';
2313
2314         if (ast_strlen_zero(data)) {
2315                 ast_log(LOG_WARNING, "The AGENT function requires an argument - agentid!\n");
2316                 return -1;
2317         }
2318
2319         parse = ast_strdupa(data);
2320
2321         AST_NONSTANDARD_APP_ARGS(args, parse, ':');
2322         if (!args.item)
2323                 args.item = "status";
2324
2325         AST_LIST_LOCK(&agents);
2326
2327         if (!(agent = find_agent(args.agentid))) {
2328                 AST_LIST_UNLOCK(&agents);
2329                 ast_log(LOG_WARNING, "Agent '%s' not found!\n", args.agentid);
2330                 return -1;
2331         }
2332
2333         if (!strcasecmp(args.item, "status")) {
2334                 char *status = "LOGGEDOUT";
2335                 if (agent->chan) {
2336                         status = "LOGGEDIN";
2337                 }
2338                 ast_copy_string(buf, status, len);
2339         } else if (!strcasecmp(args.item, "password")) 
2340                 ast_copy_string(buf, agent->password, len);
2341         else if (!strcasecmp(args.item, "name"))
2342                 ast_copy_string(buf, agent->name, len);
2343         else if (!strcasecmp(args.item, "mohclass"))
2344                 ast_copy_string(buf, agent->moh, len);
2345         else if (!strcasecmp(args.item, "channel")) {
2346                 if (agent->chan) {
2347                         ast_channel_lock(agent->chan);
2348                         ast_copy_string(buf, agent->chan->name, len);
2349                         ast_channel_unlock(agent->chan);
2350                         tmp = strrchr(buf, '-');
2351                         if (tmp)
2352                                 *tmp = '\0';
2353                 } 
2354         } else if (!strcasecmp(args.item, "fullchannel")) {
2355                 if (agent->chan) {
2356                         ast_channel_lock(agent->chan);
2357                         ast_copy_string(buf, agent->chan->name, len);
2358                         ast_channel_unlock(agent->chan);
2359                 } 
2360         } else if (!strcasecmp(args.item, "exten")) {
2361                 buf[0] = '\0';
2362         }
2363
2364         AST_LIST_UNLOCK(&agents);
2365
2366         return 0;
2367 }
2368
2369 static struct ast_custom_function agent_function = {
2370         .name = "AGENT",
2371         .read = function_agent,
2372 };
2373
2374 /*!
2375  * \internal
2376  * \brief Callback used to generate the agents tree.
2377  * \param[in] search The search pattern tree.
2378  * \retval NULL on error.
2379  * \retval non-NULL The generated tree.
2380  */
2381 static int agents_data_provider_get(const struct ast_data_search *search,
2382         struct ast_data *data_root)
2383 {
2384         struct agent_pvt *p;
2385         struct ast_data *data_agent, *data_channel, *data_talkingto;
2386
2387         AST_LIST_LOCK(&agents);
2388         AST_LIST_TRAVERSE(&agents, p, list) {
2389                 data_agent = ast_data_add_node(data_root, "agent");
2390                 if (!data_agent) {
2391                         continue;
2392                 }
2393
2394                 ast_mutex_lock(&p->lock);
2395                 if (!(p->pending)) {
2396                         ast_data_add_str(data_agent, "id", p->agent);
2397                         ast_data_add_structure(agent_pvt, data_agent, p);
2398
2399                         ast_data_add_bool(data_agent, "logged", p->chan ? 1 : 0);
2400                         if (p->chan) {
2401                                 data_channel = ast_data_add_node(data_agent, "loggedon");
2402                                 if (!data_channel) {
2403                                         ast_mutex_unlock(&p->lock);
2404                                         ast_data_remove_node(data_root, data_agent);
2405                                         continue;
2406                                 }
2407                                 ast_channel_data_add_structure(data_channel, p->chan, 0);
2408                                 if (p->owner && ast_bridged_channel(p->owner)) {
2409                                         data_talkingto = ast_data_add_node(data_agent, "talkingto");
2410                                         if (!data_talkingto) {
2411                                                 ast_mutex_unlock(&p->lock);
2412                                                 ast_data_remove_node(data_root, data_agent);
2413                                                 continue;
2414                                         }
2415                                         ast_channel_data_add_structure(data_talkingto, ast_bridged_channel(p->owner), 0);
2416                                 }
2417                         } else {
2418                                 ast_data_add_node(data_agent, "talkingto");
2419                                 ast_data_add_node(data_agent, "loggedon");
2420                         }
2421                         ast_data_add_str(data_agent, "musiconhold", p->moh);
2422                 }
2423                 ast_mutex_unlock(&p->lock);
2424
2425                 /* if this agent doesn't match remove the added agent. */
2426                 if (!ast_data_search_match(search, data_agent)) {
2427                         ast_data_remove_node(data_root, data_agent);
2428                 }
2429         }
2430         AST_LIST_UNLOCK(&agents);
2431
2432         return 0;
2433 }
2434
2435 static const struct ast_data_handler agents_data_provider = {
2436         .version = AST_DATA_HANDLER_VERSION,
2437         .get = agents_data_provider_get
2438 };
2439
2440 static const struct ast_data_entry agents_data_providers[] = {
2441         AST_DATA_ENTRY("asterisk/channel/agent/list", &agents_data_provider),
2442 };
2443
2444 /*!
2445  * \brief Initialize the Agents module.
2446  * This function is being called by Asterisk when loading the module. 
2447  * Among other things it registers applications, cli commands and reads the cofiguration file.
2448  *
2449  * \returns int Always 0.
2450  */
2451 static int load_module(void)
2452 {
2453         if (!(agent_tech.capabilities = ast_format_cap_alloc())) {
2454                 ast_log(LOG_ERROR, "ast_format_cap_alloc_nolock fail.\n");
2455                 return AST_MODULE_LOAD_FAILURE;
2456         }
2457         ast_format_cap_add_all(agent_tech.capabilities);
2458         /* Make sure we can register our agent channel type */
2459         if (ast_channel_register(&agent_tech)) {
2460                 ast_log(LOG_ERROR, "Unable to register channel class 'Agent'\n");
2461                 return AST_MODULE_LOAD_FAILURE;
2462         }
2463         /* Read in the config */
2464         if (!read_agent_config(0))
2465                 return AST_MODULE_LOAD_DECLINE;
2466         /* Dialplan applications */
2467         ast_register_application_xml(app, login_exec);
2468         ast_register_application_xml(app3, agentmonitoroutgoing_exec);
2469
2470         /* data tree */
2471         ast_data_register_multiple(agents_data_providers, ARRAY_LEN(agents_data_providers));
2472
2473         /* Manager commands */
2474         ast_manager_register_xml("Agents", EVENT_FLAG_AGENT, action_agents);
2475         ast_manager_register_xml("AgentLogoff", EVENT_FLAG_AGENT, action_agent_logoff);
2476
2477         /* CLI Commands */
2478         ast_cli_register_multiple(cli_agents, ARRAY_LEN(cli_agents));
2479
2480         /* Dialplan Functions */
2481         ast_custom_function_register(&agent_function);
2482
2483         return AST_MODULE_LOAD_SUCCESS;
2484 }
2485
2486 static int reload(void)
2487 {
2488         return read_agent_config(1);
2489 }
2490
2491 static int unload_module(void)
2492 {
2493         struct agent_pvt *p;
2494         /* First, take us out of the channel loop */
2495         ast_channel_unregister(&agent_tech);
2496         /* Unregister dialplan functions */
2497         ast_custom_function_unregister(&agent_function);        
2498         /* Unregister CLI commands */
2499         ast_cli_unregister_multiple(cli_agents, ARRAY_LEN(cli_agents));
2500         /* Unregister dialplan applications */
2501         ast_unregister_application(app);
2502         ast_unregister_application(app3);
2503         /* Unregister manager command */
2504         ast_manager_unregister("Agents");
2505         ast_manager_unregister("AgentLogoff");
2506         /* Unregister the data tree */
2507         ast_data_unregister(NULL);
2508         /* Unregister channel */
2509         AST_LIST_LOCK(&agents);
2510         /* Hangup all interfaces if they have an owner */
2511         while ((p = AST_LIST_REMOVE_HEAD(&agents, list))) {
2512                 if (p->owner)
2513                         ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
2514                 ast_free(p);
2515         }
2516         AST_LIST_UNLOCK(&agents);
2517
2518         agent_tech.capabilities = ast_format_cap_destroy(agent_tech.capabilities);
2519         return 0;
2520 }
2521
2522 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Agent Proxy Channel",
2523                 .load = load_module,
2524                 .unload = unload_module,
2525                 .reload = reload,
2526                 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
2527                 .nonoptreq = "res_monitor,chan_local",
2528                );