30e62dd6250cd1f8e2d0e7fe1386e0219ef2dc69
[asterisk/asterisk.git] / apps / app_meetme.c
1
2 /*
3  * Asterisk -- An open source telephony toolkit.
4  *
5  * Copyright (C) 1999 - 2005, Digium, Inc.
6  *
7  * Mark Spencer <markster@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Meet me conference bridge
23  *
24  * \author Mark Spencer <markster@digium.com>
25  * 
26  * \ingroup applications
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <sys/ioctl.h>
35 #ifdef __linux__
36 #include <linux/zaptel.h>
37 #else
38 #include <zaptel.h>
39 #endif /* __linux__ */
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include "asterisk/lock.h"
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/module.h"
51 #include "asterisk/config.h"
52 #include "asterisk/app.h"
53 #include "asterisk/dsp.h"
54 #include "asterisk/musiconhold.h"
55 #include "asterisk/manager.h"
56 #include "asterisk/options.h"
57 #include "asterisk/cli.h"
58 #include "asterisk/say.h"
59 #include "asterisk/utils.h"
60 #include "asterisk/translate.h"
61 #include "asterisk/ulaw.h"
62
63 static const char *tdesc = "MeetMe conference bridge";
64
65 static const char *app = "MeetMe";
66 static const char *app2 = "MeetMeCount";
67 static const char *app3 = "MeetMeAdmin";
68
69 static const char *synopsis = "MeetMe conference bridge";
70 static const char *synopsis2 = "MeetMe participant count";
71 static const char *synopsis3 = "MeetMe conference Administration";
72
73 static const char *descrip =
74 "  MeetMe([confno][,[options][,pin]]): Enters the user into a specified MeetMe\n"
75 "conference.  If the conference number is omitted, the user will be prompted\n"
76 "to enter one.  User can exit the conference by hangup, or if the 'p' option\n"
77 "is specified, by pressing '#'.\n"
78 "Please note: A ZAPTEL INTERFACE MUST BE INSTALLED FOR CONFERENCING TO WORK!\n\n"
79
80 "The option string may contain zero or more of the following characters:\n"
81 "      'a' -- set admin mode\n"
82 "      'A' -- set marked mode\n"
83 "      'b' -- run AGI script specified in ${MEETME_AGI_BACKGROUND}\n"
84 "             Default: conf-background.agi  (Note: This does not work with\n"
85 "             non-Zap channels in the same conference)\n"
86 "      'c' -- announce user(s) count on joining a conference\n"
87 "      'd' -- dynamically add conference\n"
88 "      'D' -- dynamically add conference, prompting for a PIN\n"
89 "      'e' -- select an empty conference\n"
90 "      'E' -- select an empty pinless conference\n"
91 "      'i' -- announce user join/leave\n"
92 "      'm' -- set monitor only mode (Listen only, no talking)\n"
93 "      'M' -- enable music on hold when the conference has a single caller\n"
94 "      'o' -- set talker optimization - treats talkers who aren't speaking as\n"
95 "             being muted, meaning (a) No encode is done on transmission and\n"
96 "             (b) Received audio that is not registered as talking is omitted\n"
97 "             causing no buildup in background noise\n"
98 "      'p' -- allow user to exit the conference by pressing '#'\n"
99 "      'P' -- always prompt for the pin even if it is specified\n"
100 "      'q' -- quiet mode (don't play enter/leave sounds)\n"
101 "      'r' -- Record conference (records as ${MEETME_RECORDINGFILE}\n"
102 "             using format ${MEETME_RECORDINGFORMAT}). Default filename is\n"
103 "             meetme-conf-rec-${CONFNO}-${UNIQUEID} and the default format is\n"
104 "             wav.\n"
105 "      's' -- Present menu (user or admin) when '*' is received ('send' to menu)\n"
106 "      't' -- set talk only mode. (Talk only, no listening)\n"
107 "      'T' -- set talker detection (sent to manager interface and meetme list)\n"
108 "      'v' -- video mode\n"
109 "      'w' -- wait until the marked user enters the conference\n"
110 "      'x' -- close the conference when last marked user exits\n"
111 "      'X' -- allow user to exit the conference by entering a valid single\n"
112 "             digit extension ${MEETME_EXIT_CONTEXT} or the current context\n"
113 "             if that variable is not defined.\n"
114 "      '1' -- do not play message when first person enters\n";
115
116 static const char *descrip2 =
117 "  MeetMeCount(confno[|var]): Plays back the number of users in the specified\n"
118 "MeetMe conference. If var is specified, playback will be skipped and the value\n"
119 "will be returned in the variable. Upon app completion, MeetMeCount will hangup\n"
120 "the channel, unless priority n+1 exists, in which case priority progress will\n"
121 "continue.\n"
122 "A ZAPTEL INTERFACE MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
123
124 static const char *descrip3 = 
125 "  MeetMeAdmin(confno,command[,user]): Run admin command for conference\n"
126 "      'e' -- Eject last user that joined\n"
127 "      'k' -- Kick one user out of conference\n"
128 "      'K' -- Kick all users out of conference\n"
129 "      'l' -- Unlock conference\n"
130 "      'L' -- Lock conference\n"
131 "      'm' -- Unmute conference\n"
132 "      'M' -- Mute conference\n"
133 "      'n' -- Unmute entire conference (except admin)\n"
134 "      'N' -- Mute entire conference (except admin)\n"
135 "";
136
137 #define CONFIG_FILE_NAME "meetme.conf"
138
139 LOCAL_USER_DECL;
140
141 struct ast_conference {
142         ast_mutex_t playlock;                           /* Conference specific lock (players) */
143         ast_mutex_t listenlock;                         /* Conference specific lock (listeners) */
144         char confno[AST_MAX_EXTENSION];         /* Conference */
145         struct ast_channel *chan;               /* Announcements channel */
146         struct ast_channel *lchan;              /* Listen/Record channel */
147         int fd;                                 /* Announcements fd */
148         int zapconf;                            /* Zaptel Conf # */
149         int users;                              /* Number of active users */
150         int markedusers;                        /* Number of marked users */
151         struct ast_conf_user *firstuser;        /* Pointer to the first user struct */
152         struct ast_conf_user *lastuser;         /* Pointer to the last user struct */
153         time_t start;                           /* Start time (s) */
154         int refcount;                           /* reference count of usage */
155         unsigned int recording:2;                               /* recording status */
156         unsigned int isdynamic:1;                               /* Created on the fly? */
157         unsigned int locked:1;                          /* Is the conference locked? */
158         pthread_t recordthread;                 /* thread for recording */
159         pthread_attr_t attr;                    /* thread attribute */
160         const char *recordingfilename;          /* Filename to record the Conference into */
161         const char *recordingformat;                    /* Format to record the Conference in */
162         char pin[AST_MAX_EXTENSION];            /* If protected by a PIN */
163         char pinadmin[AST_MAX_EXTENSION];       /* If protected by a admin PIN */
164         struct ast_frame *transframe[32];
165         struct ast_frame *origframe;
166         struct ast_trans_pvt *transpath[32];
167         AST_LIST_ENTRY(ast_conference) list;
168 };
169
170 static AST_LIST_HEAD_STATIC(confs, ast_conference);
171
172 struct volume {
173         int desired;                            /* Desired volume adjustment */
174         int actual;                             /* Actual volume adjustment (for channels that can't adjust) */
175 };
176
177 struct ast_conf_user {
178         int user_no;                            /* User Number */
179         struct ast_conf_user *prevuser;         /* Pointer to the previous user */
180         struct ast_conf_user *nextuser;         /* Pointer to the next user */
181         int userflags;                          /* Flags as set in the conference */
182         int adminflags;                         /* Flags set by the Admin */
183         struct ast_channel *chan;               /* Connected channel */
184         int talking;                            /* Is user talking */
185         int zapchannel;                         /* Is a Zaptel channel */
186         char usrvalue[50];                      /* Custom User Value */
187         char namerecloc[AST_MAX_EXTENSION];     /* Name Recorded file Location */
188         time_t jointime;                        /* Time the user joined the conference */
189         struct volume talk;
190         struct volume listen;
191 };
192
193 static int audio_buffers;                       /* The number of audio buffers to be allocated on pseudo channels
194                                                    when in a conference
195                                                 */
196
197 #define DEFAULT_AUDIO_BUFFERS 32                /* each buffer is 20ms, so this is 640ms total */
198
199 #define ADMINFLAG_MUTED (1 << 1)                /* User is muted */
200 #define ADMINFLAG_KICKME (1 << 2)               /* User is kicked */
201 #define MEETME_DELAYDETECTTALK          300
202 #define MEETME_DELAYDETECTENDTALK       1000
203
204 #define AST_FRAME_BITS 32
205
206 enum volume_action {
207         VOL_UP,
208         VOL_DOWN,
209 };
210
211 static int admin_exec(struct ast_channel *chan, void *data);
212
213 static void *recordthread(void *args);
214
215 #include "enter.h"
216 #include "leave.h"
217
218 #define ENTER   0
219 #define LEAVE   1
220
221 #define MEETME_RECORD_OFF       0
222 #define MEETME_RECORD_STARTED   1
223 #define MEETME_RECORD_ACTIVE    2
224 #define MEETME_RECORD_TERMINATE 3
225
226 #define CONF_SIZE 320
227
228 #define CONFFLAG_ADMIN  (1 << 1)                /* If set the user has admin access on the conference */
229 #define CONFFLAG_MONITOR (1 << 2)               /* If set the user can only receive audio from the conference */
230 #define CONFFLAG_POUNDEXIT (1 << 3)             /* If set asterisk will exit conference when '#' is pressed */
231 #define CONFFLAG_STARMENU (1 << 4)              /* If set asterisk will provide a menu to the user when '*' is pressed */
232 #define CONFFLAG_TALKER (1 << 5)                /* If set the use can only send audio to the conference */
233 #define CONFFLAG_QUIET (1 << 6)                 /* If set there will be no enter or leave sounds */
234 #define CONFFLAG_VIDEO (1 << 7)                 /* Set to enable video mode */
235 #define CONFFLAG_AGI (1 << 8)                   /* Set to run AGI Script in Background */
236 #define CONFFLAG_MOH (1 << 9)                   /* Set to have music on hold when user is alone in conference */
237 #define CONFFLAG_MARKEDEXIT (1 << 10)           /* If set the MeetMe will return if all marked with this flag left */
238 #define CONFFLAG_WAITMARKED (1 << 11)           /* If set, the MeetMe will wait until a marked user enters */
239 #define CONFFLAG_EXIT_CONTEXT (1 << 12)         /* If set, the MeetMe will exit to the specified context */
240 #define CONFFLAG_MARKEDUSER (1 << 13)           /* If set, the user will be marked */
241 #define CONFFLAG_INTROUSER (1 << 14)            /* If set, user will be ask record name on entry of conference */
242 #define CONFFLAG_RECORDCONF (1<< 15)            /* If set, the MeetMe will be recorded */
243 #define CONFFLAG_MONITORTALKER (1 << 16)        /* If set, the user will be monitored if the user is talking or not */
244 #define CONFFLAG_DYNAMIC (1 << 17)
245 #define CONFFLAG_DYNAMICPIN (1 << 18)
246 #define CONFFLAG_EMPTY (1 << 19)
247 #define CONFFLAG_EMPTYNOPIN (1 << 20)
248 #define CONFFLAG_ALWAYSPROMPT (1 << 21)
249 #define CONFFLAG_ANNOUNCEUSERCOUNT (1 << 22)    /* If set, when user joins the conference, they will be told the number of users that are already in */
250 #define CONFFLAG_OPTIMIZETALKER (1 << 23)       /* If set, treats talking users as muted users */
251 #define CONFFLAG_NOONLYPERSON (1 << 24)         /* If set, won't speak the extra prompt when the first person enters the conference */
252
253
254 AST_APP_OPTIONS(meetme_opts, {
255         AST_APP_OPTION('A', CONFFLAG_MARKEDUSER ),
256         AST_APP_OPTION('a', CONFFLAG_ADMIN ),
257         AST_APP_OPTION('b', CONFFLAG_AGI ),
258         AST_APP_OPTION('c', CONFFLAG_ANNOUNCEUSERCOUNT ),
259         AST_APP_OPTION('D', CONFFLAG_DYNAMICPIN ),
260         AST_APP_OPTION('d', CONFFLAG_DYNAMIC ),
261         AST_APP_OPTION('E', CONFFLAG_EMPTYNOPIN ),
262         AST_APP_OPTION('e', CONFFLAG_EMPTY ),
263         AST_APP_OPTION('i', CONFFLAG_INTROUSER ),
264         AST_APP_OPTION('M', CONFFLAG_MOH ),
265         AST_APP_OPTION('m', CONFFLAG_MONITOR ),
266         AST_APP_OPTION('o', CONFFLAG_OPTIMIZETALKER ),
267         AST_APP_OPTION('P', CONFFLAG_ALWAYSPROMPT ),
268         AST_APP_OPTION('p', CONFFLAG_POUNDEXIT ),
269         AST_APP_OPTION('q', CONFFLAG_QUIET ),
270         AST_APP_OPTION('r', CONFFLAG_RECORDCONF ),
271         AST_APP_OPTION('s', CONFFLAG_STARMENU ),
272         AST_APP_OPTION('T', CONFFLAG_MONITORTALKER ),
273         AST_APP_OPTION('t', CONFFLAG_TALKER ),
274         AST_APP_OPTION('w', CONFFLAG_WAITMARKED ),
275         AST_APP_OPTION('X', CONFFLAG_EXIT_CONTEXT ),
276         AST_APP_OPTION('x', CONFFLAG_MARKEDEXIT ),
277         AST_APP_OPTION('1', CONFFLAG_NOONLYPERSON ),
278 });
279
280 static char *istalking(int x)
281 {
282         if (x > 0)
283                 return "(talking)";
284         else if (x < 0)
285                 return "(unmonitored)";
286         else 
287                 return "(not talking)";
288 }
289
290 static int careful_write(int fd, unsigned char *data, int len, int block)
291 {
292         int res;
293         int x;
294
295         while (len) {
296                 if (block) {
297                         x = ZT_IOMUX_WRITE | ZT_IOMUX_SIGEVENT;
298                         res = ioctl(fd, ZT_IOMUX, &x);
299                 } else
300                         res = 0;
301                 if (res >= 0)
302                         res = write(fd, data, len);
303                 if (res < 1) {
304                         if (errno != EAGAIN) {
305                                 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
306                                 return -1;
307                         } else
308                                 return 0;
309                 }
310                 len -= res;
311                 data += res;
312         }
313
314         return 0;
315 }
316
317 /* Map 'volume' levels from -5 through +5 into
318    decibel (dB) settings for channel drivers
319    Note: these are not a straight linear-to-dB
320    conversion... the numbers have been modified
321    to give the user a better level of adjustability
322 */
323 static signed char gain_map[] = {
324         -15,
325         -13,
326         -10,
327         -6,
328         0,
329         0,
330         0,
331         6,
332         10,
333         13,
334         15,
335 };
336
337 static int set_talk_volume(struct ast_conf_user *user, int volume)
338 {
339         signed char gain_adjust;
340
341         /* attempt to make the adjustment in the channel driver;
342            if successful, don't adjust in the frame reading routine
343         */
344         gain_adjust = gain_map[volume + 5];
345
346         return ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
347 }
348
349 static int set_listen_volume(struct ast_conf_user *user, int volume)
350 {
351         signed char gain_adjust;
352
353         /* attempt to make the adjustment in the channel driver;
354            if successful, don't adjust in the frame reading routine
355         */
356         gain_adjust = gain_map[volume + 5];
357
358         return ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
359 }
360
361 static void tweak_volume(struct volume *vol, enum volume_action action)
362 {
363         switch (action) {
364         case VOL_UP:
365                 switch (vol->desired) {
366                 case 5:
367                         break;
368                 case 0:
369                         vol->desired = 2;
370                         break;
371                 case -2:
372                         vol->desired = 0;
373                         break;
374                 default:
375                         vol->desired++;
376                         break;
377                 }
378                 break;
379         case VOL_DOWN:
380                 switch (vol->desired) {
381                 case -5:
382                         break;
383                 case 2:
384                         vol->desired = 0;
385                         break;
386                 case 0:
387                         vol->desired = -2;
388                         break;
389                 default:
390                         vol->desired--;
391                         break;
392                 }
393         }
394 }
395
396 static void tweak_talk_volume(struct ast_conf_user *user, enum volume_action action)
397 {
398         tweak_volume(&user->talk, action);
399         /* attempt to make the adjustment in the channel driver;
400            if successful, don't adjust in the frame reading routine
401         */
402         if (!set_talk_volume(user, user->talk.desired))
403                 user->talk.actual = 0;
404         else
405                 user->talk.actual = user->talk.desired;
406 }
407
408 static void tweak_listen_volume(struct ast_conf_user *user, enum volume_action action)
409 {
410         tweak_volume(&user->listen, action);
411         /* attempt to make the adjustment in the channel driver;
412            if successful, don't adjust in the frame reading routine
413         */
414         if (!set_listen_volume(user, user->listen.desired))
415                 user->listen.actual = 0;
416         else
417                 user->listen.actual = user->listen.desired;
418 }
419
420 static void reset_volumes(struct ast_conf_user *user)
421 {
422         signed char zero_volume = 0;
423
424         ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
425         ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &zero_volume, sizeof(zero_volume), 0);
426 }
427
428 static void conf_play(struct ast_channel *chan, struct ast_conference *conf, int sound)
429 {
430         unsigned char *data;
431         int len;
432         int res = -1;
433
434         if (!chan->_softhangup)
435                 res = ast_autoservice_start(chan);
436
437         AST_LIST_LOCK(&confs);
438
439         switch(sound) {
440         case ENTER:
441                 data = enter;
442                 len = sizeof(enter);
443                 break;
444         case LEAVE:
445                 data = leave;
446                 len = sizeof(leave);
447                 break;
448         default:
449                 data = NULL;
450                 len = 0;
451         }
452         if (data) {
453                 careful_write(conf->fd, data, len, 1);
454         }
455
456         AST_LIST_UNLOCK(&confs);
457
458         if (!res) 
459                 ast_autoservice_stop(chan);
460 }
461
462 static struct ast_conference *build_conf(char *confno, char *pin, char *pinadmin, int make, int dynamic, int refcount)
463 {
464         struct ast_conference *cnf;
465         struct zt_confinfo ztc;
466
467         AST_LIST_LOCK(&confs);
468
469         AST_LIST_TRAVERSE(&confs, cnf, list) {
470                 if (!strcmp(confno, cnf->confno)) 
471                         break;
472         }
473
474         if (!cnf && (make || dynamic)) {
475                 /* Make a new one */
476                 if ((cnf = ast_calloc(1, sizeof(*cnf)))) {
477                         ast_mutex_init(&cnf->playlock);
478                         ast_mutex_init(&cnf->listenlock);
479                         ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
480                         ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
481                         ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
482                         cnf->refcount = 0;
483                         cnf->markedusers = 0;
484                         cnf->chan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
485                         if (cnf->chan) {
486                                 ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
487                                 ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
488                                 cnf->fd = cnf->chan->fds[0];    /* for use by conf_play() */
489                         } else {
490                                 ast_log(LOG_WARNING, "Unable to open pseudo channel - trying device\n");
491                                 cnf->fd = open("/dev/zap/pseudo", O_RDWR);
492                                 if (cnf->fd < 0) {
493                                         ast_log(LOG_WARNING, "Unable to open pseudo device\n");
494                                         free(cnf);
495                                         cnf = NULL;
496                                         goto cnfout;
497                                 }
498                         }
499                         memset(&ztc, 0, sizeof(ztc));
500                         /* Setup a new zap conference */
501                         ztc.chan = 0;
502                         ztc.confno = -1;
503                         ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
504                         if (ioctl(cnf->fd, ZT_SETCONF, &ztc)) {
505                                 ast_log(LOG_WARNING, "Error setting conference\n");
506                                 if (cnf->chan)
507                                         ast_hangup(cnf->chan);
508                                 else
509                                         close(cnf->fd);
510                                 free(cnf);
511                                 cnf = NULL;
512                                 goto cnfout;
513                         }
514                         cnf->lchan = ast_request("zap", AST_FORMAT_SLINEAR, "pseudo", NULL);
515                         if (cnf->lchan) {
516                                 ast_set_read_format(cnf->lchan, AST_FORMAT_SLINEAR);
517                                 ast_set_write_format(cnf->lchan, AST_FORMAT_SLINEAR);
518                                 ztc.chan = 0;
519                                 ztc.confmode = ZT_CONF_CONFANN | ZT_CONF_CONFANNMON;
520                                 if (ioctl(cnf->lchan->fds[0], ZT_SETCONF, &ztc)) {
521                                         ast_log(LOG_WARNING, "Error setting conference\n");
522                                         ast_hangup(cnf->lchan);
523                                         cnf->lchan = NULL;
524                                 }
525                         }
526                         /* Fill the conference struct */
527                         cnf->start = time(NULL);
528                         cnf->zapconf = ztc.confno;
529                         cnf->isdynamic = dynamic ? 1 : 0;
530                         cnf->firstuser = NULL;
531                         cnf->lastuser = NULL;
532                         cnf->locked = 0;
533                         if (option_verbose > 2)
534                                 ast_verbose(VERBOSE_PREFIX_3 "Created MeetMe conference %d for conference '%s'\n", cnf->zapconf, cnf->confno);
535                         AST_LIST_INSERT_HEAD(&confs, cnf, list);
536                 } 
537         }
538  cnfout:
539         if (cnf){ 
540                 cnf->refcount += refcount;
541         }
542         AST_LIST_UNLOCK(&confs);
543         return cnf;
544 }
545
546 static int confs_show(int fd, int argc, char **argv)
547 {
548         ast_cli(fd, "Deprecated! Please use 'meetme' instead.\n");
549
550         return RESULT_SUCCESS;
551 }
552
553 static char show_confs_usage[] =
554 "Deprecated! Please use 'meetme' instead.\n";
555
556 static struct ast_cli_entry cli_show_confs = {
557         { "show", "conferences", NULL }, confs_show,
558         "Show status of conferences", show_confs_usage, NULL };
559         
560 static int conf_cmd(int fd, int argc, char **argv) {
561         /* Process the command */
562         struct ast_conference *cnf;
563         struct ast_conf_user *user;
564         int hr, min, sec;
565         int i = 0, total = 0;
566         time_t now;
567         char *header_format = "%-14s %-14s %-10s %-8s  %-8s\n";
568         char *data_format = "%-12.12s   %4.4d         %4.4s       %02d:%02d:%02d  %-8s\n";
569         char cmdline[1024] = "";
570
571         if (argc > 8)
572                 ast_cli(fd, "Invalid Arguments.\n");
573         /* Check for length so no buffer will overflow... */
574         for (i = 0; i < argc; i++) {
575                 if (strlen(argv[i]) > 100)
576                         ast_cli(fd, "Invalid Arguments.\n");
577         }
578         if (argc == 1) {
579                 /* 'MeetMe': List all the conferences */        
580                 now = time(NULL);
581                 if (AST_LIST_EMPTY(&confs)) {
582                         ast_cli(fd, "No active MeetMe conferences.\n");
583                         return RESULT_SUCCESS;
584                 }
585                 ast_cli(fd, header_format, "Conf Num", "Parties", "Marked", "Activity", "Creation");
586                 AST_LIST_TRAVERSE(&confs, cnf, list) {
587                         if (cnf->markedusers == 0)
588                                 strcpy(cmdline, "N/A ");
589                         else 
590                                 snprintf(cmdline, sizeof(cmdline), "%4.4d", cnf->markedusers);
591                         hr = (now - cnf->start) / 3600;
592                         min = ((now - cnf->start) % 3600) / 60;
593                         sec = (now - cnf->start) % 60;
594
595                         ast_cli(fd, data_format, cnf->confno, cnf->users, cmdline, hr, min, sec, cnf->isdynamic ? "Dynamic" : "Static");
596
597                         total += cnf->users;    
598                 }
599                 ast_cli(fd, "* Total number of MeetMe users: %d\n", total);
600                 return RESULT_SUCCESS;
601         }
602         if (argc < 3)
603                 return RESULT_SHOWUSAGE;
604         ast_copy_string(cmdline, argv[2], sizeof(cmdline));     /* Argv 2: conference number */
605         if (strstr(argv[1], "lock")) {  
606                 if (strcmp(argv[1], "lock") == 0) {
607                         /* Lock */
608                         strncat(cmdline, "|L", sizeof(cmdline) - strlen(cmdline) - 1);
609                 } else {
610                         /* Unlock */
611                         strncat(cmdline, "|l", sizeof(cmdline) - strlen(cmdline) - 1);
612                 }
613         } else if (strstr(argv[1], "mute")) { 
614                 if (argc < 4)
615                         return RESULT_SHOWUSAGE;
616                 if (strcmp(argv[1], "mute") == 0) {
617                         /* Mute */
618                         if (strcmp(argv[3], "all") == 0) {
619                                 strncat(cmdline, "|N", sizeof(cmdline) - strlen(cmdline) - 1);
620                         } else {
621                                 strncat(cmdline, "|M|", sizeof(cmdline) - strlen(cmdline) - 1); 
622                                 strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
623                         }
624                 } else {
625                         /* Unmute */
626                         if (strcmp(argv[3], "all") == 0) {
627                                 strncat(cmdline, "|n", sizeof(cmdline) - strlen(cmdline) - 1);
628                         } else {
629                                 strncat(cmdline, "|m|", sizeof(cmdline) - strlen(cmdline) - 1);
630                                 strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
631                         }
632                 }
633         } else if (strcmp(argv[1], "kick") == 0) {
634                 if (argc < 4)
635                         return RESULT_SHOWUSAGE;
636                 if (strcmp(argv[3], "all") == 0) {
637                         /* Kick all */
638                         strncat(cmdline, "|K", sizeof(cmdline) - strlen(cmdline) - 1);
639                 } else {
640                         /* Kick a single user */
641                         strncat(cmdline, "|k|", sizeof(cmdline) - strlen(cmdline) - 1);
642                         strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
643                 }       
644         } else if(strcmp(argv[1], "list") == 0) {
645                 /* List all the users in a conference */
646                 if (AST_LIST_EMPTY(&confs)) {
647                         ast_cli(fd, "No active conferences.\n");
648                         return RESULT_SUCCESS;  
649                 }
650                 /* Find the right conference */
651                 AST_LIST_TRAVERSE(&confs, cnf, list) {
652                         if (strcmp(cnf->confno, argv[2]) == 0)
653                                 break;
654                 }
655                 if (!cnf) {
656                         ast_cli(fd, "No such conference: %s.\n",argv[2]);
657                         return RESULT_SUCCESS;
658                 }
659                 /* Show all the users */
660                 for (user = cnf->firstuser; user; user = user->nextuser){
661                         now = time(NULL);
662                         hr = (now - user->jointime) / 3600;
663                         min = ((now - user->jointime) % 3600) / 60;
664                         sec = (now - user->jointime) % 60;
665
666
667                         ast_cli(fd, "User #: %-2.2d %12.12s %-20.20s Channel: %s %s %s %s %s %02d:%02d:%02d\n",
668                                 user->user_no,
669                                 user->chan->cid.cid_num ? user->chan->cid.cid_num : "<unknown>",
670                                 user->chan->cid.cid_name ? user->chan->cid.cid_name : "<no name>",
671                                 user->chan->name,
672                                 user->userflags & CONFFLAG_ADMIN ? "(Admin)" : "",
673                                 user->userflags & CONFFLAG_MONITOR ? "(Listen only)" : "",
674                                 user->adminflags & ADMINFLAG_MUTED ? "(Admn Muted)" : "",
675                                 istalking(user->talking), hr, min, sec);
676                 }
677                 ast_cli(fd,"%d users in that conference.\n",cnf->users);
678
679                 return RESULT_SUCCESS;
680         } else 
681                 return RESULT_SHOWUSAGE;
682         ast_log(LOG_DEBUG, "Cmdline: %s\n", cmdline);
683         admin_exec(NULL, cmdline);
684
685         return 0;
686 }
687
688 static char *complete_confcmd(const char *line, const char *word, int pos, int state) {
689 #define CONF_COMMANDS 6
690         int which = 0, x = 0;
691         struct ast_conference *cnf = NULL;
692         struct ast_conf_user *usr = NULL;
693         char *confno = NULL;
694         char usrno[50] = "";
695         char cmds[CONF_COMMANDS][20] = {"lock", "unlock", "mute", "unmute", "kick", "list"};
696         char *myline;
697         
698         if (pos == 1) {
699                 /* Command */
700                 for (x = 0;x < CONF_COMMANDS; x++) {
701                         if (!strncasecmp(cmds[x], word, strlen(word))) {
702                                 if (++which > state) {
703                                         return strdup(cmds[x]);
704                                 }
705                         }
706                 }
707         } else if (pos == 2) {
708                 /* Conference Number */
709                 AST_LIST_LOCK(&confs);
710                 AST_LIST_TRAVERSE(&confs, cnf, list) {
711                         if (!strncasecmp(word, cnf->confno, strlen(word))) {
712                                 if (++which > state)
713                                         break;
714                         }
715                 }
716                 AST_LIST_UNLOCK(&confs);
717                 return cnf ? strdup(cnf->confno) : NULL;
718         } else if (pos == 3) {
719                 /* User Number || Conf Command option*/
720                 if (strstr(line, "mute") || strstr(line, "kick")) {
721                         if ((state == 0) && (strstr(line, "kick") || strstr(line,"mute")) && !(strncasecmp(word, "all", strlen(word)))) {
722                                 return strdup("all");
723                         }
724                         which++;
725                         AST_LIST_LOCK(&confs);
726
727                         /* TODO: Find the conf number from the cmdline (ignore spaces) <- test this and make it fail-safe! */
728                         myline = ast_strdupa(line);
729                         if (strsep(&myline, " ") && strsep(&myline, " ") && !confno) {
730                                 while((confno = strsep(&myline, " ")) && (strcmp(confno, " ") == 0))
731                                         ;
732                         }
733                         
734                         AST_LIST_TRAVERSE(&confs, cnf, list) {
735                                 if (!strcmp(confno, cnf->confno))
736                                     break;
737                         }
738
739                         if (cnf) {
740                                 /* Search for the user */
741                                 for (usr = cnf->firstuser; usr; usr = usr->nextuser) {
742                                         snprintf(usrno, sizeof(usrno), "%d", usr->user_no);
743                                         if (!strncasecmp(word, usrno, strlen(word))) {
744                                                 if (++which > state)
745                                                         break;
746                                         }
747                                 }
748                         }
749                         AST_LIST_UNLOCK(&confs);
750                         return usr ? strdup(usrno) : NULL;
751                 }
752         }
753
754         return NULL;
755 }
756         
757 static char conf_usage[] =
758 "Usage: meetme  (un)lock|(un)mute|kick|list <confno> <usernumber>\n"
759 "       Executes a command for the conference or on a conferee\n";
760
761 static struct ast_cli_entry cli_conf = {
762         {"meetme", NULL, NULL }, conf_cmd,
763         "Execute a command on a conference or conferee", conf_usage, complete_confcmd};
764
765 static void conf_flush(int fd, struct ast_channel *chan)
766 {
767         int x;
768
769         /* read any frames that may be waiting on the channel
770            and throw them away
771         */
772         if (chan) {
773                 struct ast_frame *f;
774
775                 /* when no frames are available, this will wait
776                    for 1 millisecond maximum
777                 */
778                 while (ast_waitfor(chan, 1)) {
779                         f = ast_read(chan);
780                         if (f)
781                                 ast_frfree(f);
782                 }
783         }
784
785         /* flush any data sitting in the pseudo channel */
786         x = ZT_FLUSH_ALL;
787         if (ioctl(fd, ZT_FLUSH, &x))
788                 ast_log(LOG_WARNING, "Error flushing channel\n");
789
790 }
791
792 /* Remove the conference from the list and free it.
793    We assume that this was called while holding conflock. */
794 static int conf_free(struct ast_conference *conf)
795 {
796         int x;
797         
798         AST_LIST_REMOVE(&confs, conf, list);
799
800         if (conf->recording == MEETME_RECORD_ACTIVE) {
801                 conf->recording = MEETME_RECORD_TERMINATE;
802                 AST_LIST_UNLOCK(&confs);
803                 while (1) {
804                         AST_LIST_LOCK(&confs);
805                         if (conf->recording == MEETME_RECORD_OFF)
806                                 break;
807                         AST_LIST_UNLOCK(&confs);
808                 }
809         }
810
811         for (x=0;x<AST_FRAME_BITS;x++) {
812                 if (conf->transframe[x])
813                         ast_frfree(conf->transframe[x]);
814                 if (conf->transpath[x])
815                         ast_translator_free_path(conf->transpath[x]);
816         }
817         if (conf->origframe)
818                 ast_frfree(conf->origframe);
819         if (conf->lchan)
820                 ast_hangup(conf->lchan);
821         if (conf->chan)
822                 ast_hangup(conf->chan);
823         else
824                 close(conf->fd);
825         
826         free(conf);
827
828         return 0;
829 }
830
831 static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int confflags)
832 {
833         struct ast_conf_user *user = NULL;
834         struct ast_conf_user *usr = NULL;
835         int fd;
836         struct zt_confinfo ztc, ztc_empty;
837         struct ast_frame *f;
838         struct ast_channel *c;
839         struct ast_frame fr;
840         int outfd;
841         int ms;
842         int nfds;
843         int res;
844         int flags;
845         int retryzap;
846         int origfd;
847         int musiconhold = 0;
848         int firstpass = 0;
849         int lastmarked = 0;
850         int currentmarked = 0;
851         int ret = -1;
852         int x;
853         int menu_active = 0;
854         int using_pseudo = 0;
855         int duration=20;
856         int hr, min, sec;
857         time_t now;
858         struct ast_dsp *dsp=NULL;
859         struct ast_app *app;
860         const char *agifile;
861         const char *agifiledefault = "conf-background.agi";
862         char meetmesecs[30] = "";
863         char exitcontext[AST_MAX_CONTEXT] = "";
864         char recordingtmp[AST_MAX_EXTENSION] = "";
865         char members[10] = "";
866         int dtmf;
867         ZT_BUFFERINFO bi;
868         char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
869         char *buf = __buf + AST_FRIENDLY_OFFSET;
870
871         if (!(user = ast_calloc(1, sizeof(*user)))) {
872                 AST_LIST_LOCK(&confs);
873                 conf->refcount--;
874                 if (!conf->refcount){
875                         conf_free(conf);
876                 }
877                 AST_LIST_UNLOCK(&confs);
878                 return ret;
879         }
880
881         if (confflags & CONFFLAG_RECORDCONF) {
882                 if (!conf->recordingfilename) {
883                         conf->recordingfilename = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE");
884                         if (!conf->recordingfilename) {
885                                 snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", conf->confno, chan->uniqueid);
886                                 conf->recordingfilename = ast_strdupa(recordingtmp);
887                         }
888                         conf->recordingformat = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT");
889                         if (!conf->recordingformat) {
890                                 snprintf(recordingtmp, sizeof(recordingtmp), "wav");
891                                 conf->recordingformat = ast_strdupa(recordingtmp);
892                         }
893                         ast_verbose(VERBOSE_PREFIX_4 "Starting recording of MeetMe Conference %s into file %s.%s.\n",
894                                     conf->confno, conf->recordingfilename, conf->recordingformat);
895                 }
896         }
897
898         if ((conf->recording == MEETME_RECORD_OFF) && ((confflags & CONFFLAG_RECORDCONF) || (conf->lchan))) {
899                 pthread_attr_init(&conf->attr);
900                 pthread_attr_setdetachstate(&conf->attr, PTHREAD_CREATE_DETACHED);
901                 ast_pthread_create(&conf->recordthread, &conf->attr, recordthread, conf);
902         }
903
904         time(&user->jointime);
905
906         if (conf->locked && (!(confflags & CONFFLAG_ADMIN))) {
907                 /* Sorry, but this confernce is locked! */      
908                 if (!ast_streamfile(chan, "conf-locked", chan->language))
909                         ast_waitstream(chan, "");
910                 goto outrun;
911         }
912
913         if (confflags & CONFFLAG_MARKEDUSER)
914                 conf->markedusers++;
915       
916         ast_mutex_lock(&conf->playlock);
917         if (!conf->firstuser) {
918                 /* Fill the first new User struct */
919                 user->user_no = 1;
920                 conf->firstuser = user;
921                 conf->lastuser = user;
922         } else {
923                 /* Fill the new user struct */  
924                 user->user_no = conf->lastuser->user_no + 1; 
925                 user->prevuser = conf->lastuser;
926                 if (conf->lastuser->nextuser) {
927                         ast_log(LOG_WARNING, "Error in User Management!\n");
928                         ast_mutex_unlock(&conf->playlock);
929                         goto outrun;
930                 } else {
931                         conf->lastuser->nextuser = user;
932                         conf->lastuser = user;
933                 }
934         }
935
936         user->chan = chan;
937         user->userflags = confflags;
938         user->adminflags = 0;
939         user->talking = -1;
940         conf->users++;
941         /* Update table */
942         snprintf(members, sizeof(members), "%d", conf->users);
943         ast_update_realtime("meetme", "confno", conf->confno, "members", members , NULL);
944
945         ast_mutex_unlock(&conf->playlock);
946
947         if (confflags & CONFFLAG_EXIT_CONTEXT) {
948                 if ((agifile = pbx_builtin_getvar_helper(chan, "MEETME_EXIT_CONTEXT"))) 
949                         ast_copy_string(exitcontext, agifile, sizeof(exitcontext));
950                 else if (!ast_strlen_zero(chan->macrocontext)) 
951                         ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext));
952                 else
953                         ast_copy_string(exitcontext, chan->context, sizeof(exitcontext));
954         }
955
956         if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_INTROUSER)) {
957                 snprintf(user->namerecloc, sizeof(user->namerecloc),
958                          "%s/meetme/meetme-username-%s-%d", ast_config_AST_SPOOL_DIR,
959                          conf->confno, user->user_no);
960                 ast_record_review(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL);
961         }
962
963         if ( !(confflags & (CONFFLAG_QUIET | CONFFLAG_NOONLYPERSON)) ) {
964                 if (conf->users == 1 && !(confflags & CONFFLAG_WAITMARKED))
965                         if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
966                                 ast_waitstream(chan, "");
967                 if ((confflags & CONFFLAG_WAITMARKED) && conf->markedusers == 0)
968                         if (!ast_streamfile(chan, "conf-waitforleader", chan->language))
969                                 ast_waitstream(chan, "");
970         }
971
972         if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_ANNOUNCEUSERCOUNT) && conf->users > 1) {
973                 int keepplaying = 1;
974
975                 if (conf->users == 2) { 
976                         if (!ast_streamfile(chan,"conf-onlyone",chan->language)) {
977                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
978                                 if (res > 0)
979                                         keepplaying=0;
980                                 else if (res == -1)
981                                         goto outrun;
982                         }
983                 } else { 
984                         if (!ast_streamfile(chan, "conf-thereare", chan->language)) {
985                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
986                                 if (res > 0)
987                                         keepplaying=0;
988                                 else if (res == -1)
989                                         goto outrun;
990                         }
991                         if (keepplaying) {
992                                 res = ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
993                                 if (res > 0)
994                                         keepplaying=0;
995                                 else if (res == -1)
996                                         goto outrun;
997                         }
998                         if (keepplaying && !ast_streamfile(chan, "conf-otherinparty", chan->language)) {
999                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
1000                                 if (res > 0)
1001                                         keepplaying=0;
1002                                 else if (res == -1) 
1003                                         goto outrun;
1004                         }
1005                 }
1006         }
1007
1008         ast_indicate(chan, -1);
1009
1010         if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
1011                 ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", chan->name);
1012                 goto outrun;
1013         }
1014
1015         if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
1016                 ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", chan->name);
1017                 goto outrun;
1018         }
1019
1020         retryzap = strcasecmp(chan->tech->type, "Zap");
1021         user->zapchannel = !retryzap;
1022
1023  zapretry:
1024         origfd = chan->fds[0];
1025         if (retryzap) {
1026                 fd = open("/dev/zap/pseudo", O_RDWR);
1027                 if (fd < 0) {
1028                         ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1029                         goto outrun;
1030                 }
1031                 using_pseudo = 1;
1032                 /* Make non-blocking */
1033                 flags = fcntl(fd, F_GETFL);
1034                 if (flags < 0) {
1035                         ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
1036                         close(fd);
1037                         goto outrun;
1038                 }
1039                 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
1040                         ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
1041                         close(fd);
1042                         goto outrun;
1043                 }
1044                 /* Setup buffering information */
1045                 memset(&bi, 0, sizeof(bi));
1046                 bi.bufsize = CONF_SIZE/2;
1047                 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1048                 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1049                 bi.numbufs = audio_buffers;
1050                 if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
1051                         ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
1052                         close(fd);
1053                         goto outrun;
1054                 }
1055                 x = 1;
1056                 if (ioctl(fd, ZT_SETLINEAR, &x)) {
1057                         ast_log(LOG_WARNING, "Unable to set linear mode: %s\n", strerror(errno));
1058                         close(fd);
1059                         goto outrun;
1060                 }
1061                 nfds = 1;
1062         } else {
1063                 /* XXX Make sure we're not running on a pseudo channel XXX */
1064                 fd = chan->fds[0];
1065                 nfds = 0;
1066         }
1067         memset(&ztc, 0, sizeof(ztc));
1068         memset(&ztc_empty, 0, sizeof(ztc_empty));
1069         /* Check to see if we're in a conference... */
1070         ztc.chan = 0;   
1071         if (ioctl(fd, ZT_GETCONF, &ztc)) {
1072                 ast_log(LOG_WARNING, "Error getting conference\n");
1073                 close(fd);
1074                 goto outrun;
1075         }
1076         if (ztc.confmode) {
1077                 /* Whoa, already in a conference...  Retry... */
1078                 if (!retryzap) {
1079                         ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
1080                         retryzap = 1;
1081                         goto zapretry;
1082                 }
1083         }
1084         memset(&ztc, 0, sizeof(ztc));
1085         /* Add us to the conference */
1086         ztc.chan = 0;   
1087         ztc.confno = conf->zapconf;
1088
1089         ast_mutex_lock(&conf->playlock);
1090
1091         if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_INTROUSER) && conf->users > 1) {
1092                 if (conf->chan && ast_fileexists(user->namerecloc, NULL, NULL)) {
1093                         if (!ast_streamfile(conf->chan, user->namerecloc, chan->language))
1094                                 ast_waitstream(conf->chan, "");
1095                         if (!ast_streamfile(conf->chan, "conf-hasjoin", chan->language))
1096                                 ast_waitstream(conf->chan, "");
1097                 }
1098         }
1099
1100         if (confflags & CONFFLAG_MONITOR)
1101                 ztc.confmode = ZT_CONF_CONFMON | ZT_CONF_LISTENER;
1102         else if (confflags & CONFFLAG_TALKER)
1103                 ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER;
1104         else 
1105                 ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1106
1107         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1108                 ast_log(LOG_WARNING, "Error setting conference\n");
1109                 close(fd);
1110                 ast_mutex_unlock(&conf->playlock);
1111                 goto outrun;
1112         }
1113         ast_log(LOG_DEBUG, "Placed channel %s in ZAP conf %d\n", chan->name, conf->zapconf);
1114
1115         manager_event(EVENT_FLAG_CALL, "MeetmeJoin", 
1116                       "Channel: %s\r\n"
1117                       "Uniqueid: %s\r\n"
1118                       "Meetme: %s\r\n"
1119                       "Usernum: %d\r\n",
1120                       chan->name, chan->uniqueid, conf->confno, user->user_no);
1121
1122         if (!firstpass && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN)) {
1123                 firstpass = 1;
1124                 if (!(confflags & CONFFLAG_QUIET))
1125                         if (!(confflags & CONFFLAG_WAITMARKED) || (conf->markedusers >= 1))
1126                                 conf_play(chan, conf, ENTER);
1127         }
1128
1129         ast_mutex_unlock(&conf->playlock);
1130
1131         conf_flush(fd, chan);
1132
1133         if (confflags & CONFFLAG_AGI) {
1134                 /* Get name of AGI file to run from $(MEETME_AGI_BACKGROUND)
1135                    or use default filename of conf-background.agi */
1136
1137                 agifile = pbx_builtin_getvar_helper(chan, "MEETME_AGI_BACKGROUND");
1138                 if (!agifile)
1139                         agifile = agifiledefault;
1140
1141                 if (user->zapchannel) {
1142                         /*  Set CONFMUTE mode on Zap channel to mute DTMF tones */
1143                         x = 1;
1144                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
1145                 }
1146                 /* Find a pointer to the agi app and execute the script */
1147                 app = pbx_findapp("agi");
1148                 if (app) {
1149                         char *s = ast_strdupa(agifile);
1150                         ret = pbx_exec(chan, app, s, 1);
1151                 } else {
1152                         ast_log(LOG_WARNING, "Could not find application (agi)\n");
1153                         ret = -2;
1154                 }
1155                 if (user->zapchannel) {
1156                         /*  Remove CONFMUTE mode on Zap channel */
1157                         x = 0;
1158                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
1159                 }
1160         } else {
1161                 if (user->zapchannel && (confflags & CONFFLAG_STARMENU)) {
1162                         /*  Set CONFMUTE mode on Zap channel to mute DTMF tones when the menu is enabled */
1163                         x = 1;
1164                         ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
1165                 }       
1166                 if (confflags & (CONFFLAG_MONITORTALKER | CONFFLAG_OPTIMIZETALKER) && !(dsp = ast_dsp_new())) {
1167                         ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
1168                         res = -1;
1169                 }
1170                 for(;;) {
1171                         int menu_was_active = 0;
1172
1173                         outfd = -1;
1174                         ms = -1;
1175                         
1176                         /* if we have just exited from the menu, and the user had a channel-driver
1177                            volume adjustment, restore it
1178                         */
1179                         if (!menu_active && menu_was_active && user->listen.desired && !user->listen.actual)
1180                                 set_talk_volume(user, user->listen.desired);
1181
1182                         menu_was_active = menu_active;
1183
1184                         currentmarked = conf->markedusers;
1185                         if (!(confflags & CONFFLAG_QUIET) &&
1186                             (confflags & CONFFLAG_MARKEDUSER) &&
1187                             (confflags & CONFFLAG_WAITMARKED) &&
1188                             lastmarked == 0) {
1189                                 if (currentmarked == 1 && conf->users > 1) {
1190                                         ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
1191                                         if (conf->users - 1 == 1) {
1192                                                 if (!ast_streamfile(chan, "conf-userwilljoin", chan->language))
1193                                                         ast_waitstream(chan, "");
1194                                         } else {
1195                                                 if (!ast_streamfile(chan, "conf-userswilljoin", chan->language))
1196                                                         ast_waitstream(chan, "");
1197                                         }
1198                                 }
1199                                 if (conf->users == 1 && ! (confflags & CONFFLAG_MARKEDUSER))
1200                                         if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
1201                                                 ast_waitstream(chan, "");
1202                         }
1203
1204                         c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
1205                         
1206                         
1207                         /* Update the struct with the actual confflags */
1208                         user->userflags = confflags;
1209                         
1210                         if (confflags & CONFFLAG_WAITMARKED) {
1211                                 if(currentmarked == 0) {
1212                                         if (lastmarked != 0) {
1213                                                 if (!(confflags & CONFFLAG_QUIET))
1214                                                         if (!ast_streamfile(chan, "conf-leaderhasleft", chan->language))
1215                                                                 ast_waitstream(chan, "");
1216                                                 if(confflags & CONFFLAG_MARKEDEXIT)
1217                                                         break;
1218                                                 else {
1219                                                         ztc.confmode = ZT_CONF_CONF;
1220                                                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1221                                                                 ast_log(LOG_WARNING, "Error setting conference\n");
1222                                                                 close(fd);
1223                                                                 goto outrun;
1224                                                         }
1225                                                 }
1226                                         }
1227                                         if (musiconhold == 0 && (confflags & CONFFLAG_MOH)) {
1228                                                 ast_moh_start(chan, NULL);
1229                                                 musiconhold = 1;
1230                                         } else {
1231                                                 ztc.confmode = ZT_CONF_CONF;
1232                                                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
1233                                                         ast_log(LOG_WARNING, "Error setting conference\n");
1234                                                         close(fd);
1235                                                         goto outrun;
1236                                                 }
1237                                         }
1238                                 } else if(currentmarked >= 1 && lastmarked == 0) {
1239                                         if (confflags & CONFFLAG_MONITOR)
1240                                                 ztc.confmode = ZT_CONF_CONFMON | ZT_CONF_LISTENER;
1241                                         else if (confflags & CONFFLAG_TALKER)
1242                                                 ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER;
1243                                         else
1244                                                 ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1245                                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1246                                                 ast_log(LOG_WARNING, "Error setting conference\n");
1247                                                 close(fd);
1248                                                 goto outrun;
1249                                         }
1250                                         if (musiconhold && (confflags & CONFFLAG_MOH)) {
1251                                                 ast_moh_stop(chan);
1252                                                 musiconhold = 0;
1253                                         }
1254                                         if ( !(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MARKEDUSER)) {
1255                                                 if (!ast_streamfile(chan, "conf-placeintoconf", chan->language))
1256                                                         ast_waitstream(chan, "");
1257                                                 conf_play(chan, conf, ENTER);
1258                                         }
1259                                 }
1260                         }
1261
1262                         /* trying to add moh for single person conf */
1263                         if ((confflags & CONFFLAG_MOH) && !(confflags & CONFFLAG_WAITMARKED)) {
1264                                 if (conf->users == 1) {
1265                                         if (musiconhold == 0) {
1266                                                 ast_moh_start(chan, NULL);
1267                                                 musiconhold = 1;
1268                                         } 
1269                                 } else {
1270                                         if (musiconhold) {
1271                                                 ast_moh_stop(chan);
1272                                                 musiconhold = 0;
1273                                         }
1274                                 }
1275                         }
1276                         
1277                         /* Leave if the last marked user left */
1278                         if (currentmarked == 0 && lastmarked != 0 && (confflags & CONFFLAG_MARKEDEXIT)) {
1279                                 ret = -1;
1280                                 break;
1281                         }
1282         
1283                         /* Check if the admin changed my modes */
1284                         if (user->adminflags) {                 
1285                                 /* Set the new modes */
1286                                 if ((user->adminflags & ADMINFLAG_MUTED) && (ztc.confmode & ZT_CONF_TALKER)) {
1287                                         ztc.confmode ^= ZT_CONF_TALKER;
1288                                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1289                                                 ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
1290                                                 ret = -1;
1291                                                 break;
1292                                         }
1293                                 }
1294                                 if (!(user->adminflags & ADMINFLAG_MUTED) && !(confflags & CONFFLAG_MONITOR) && !(ztc.confmode & ZT_CONF_TALKER)) {
1295                                         ztc.confmode |= ZT_CONF_TALKER;
1296                                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1297                                                 ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
1298                                                 ret = -1;
1299                                                 break;
1300                                         }
1301                                 }
1302                                 if (user->adminflags & ADMINFLAG_KICKME) {
1303                                         /* You have been kicked. */
1304                                         if (!ast_streamfile(chan, "conf-kicked", chan->language))
1305                                                 ast_waitstream(chan, "");
1306                                         ret = 0;
1307                                         break;
1308                                 }
1309                         } else if (!(confflags & CONFFLAG_MONITOR) && !(ztc.confmode & ZT_CONF_TALKER)) {
1310                                 ztc.confmode |= ZT_CONF_TALKER;
1311                                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
1312                                         ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
1313                                         ret = -1;
1314                                         break;
1315                                 }
1316                         }
1317
1318                         if (c) {
1319                                 if (c->fds[0] != origfd) {
1320                                         if (using_pseudo) {
1321                                                 /* Kill old pseudo */
1322                                                 close(fd);
1323                                                 using_pseudo = 0;
1324                                         }
1325                                         ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
1326                                         retryzap = strcasecmp(c->tech->type, "Zap");
1327                                         user->zapchannel = !retryzap;
1328                                         goto zapretry;
1329                                 }
1330                                 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & ADMINFLAG_MUTED))
1331                                         f = ast_read_noaudio(c);
1332                                 else
1333                                         f = ast_read(c);
1334                                 if (!f)
1335                                         break;
1336                                 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
1337                                         if (user->talk.actual)
1338                                                 ast_frame_adjust_volume(f, user->talk.actual);
1339
1340                                         if (confflags & (CONFFLAG_MONITORTALKER | CONFFLAG_OPTIMIZETALKER)) {
1341                                                 int totalsilence;
1342
1343                                                 if (user->talking == -1)
1344                                                         user->talking = 0;
1345
1346                                                 res = ast_dsp_silence(dsp, f, &totalsilence);
1347                                                 if (!user->talking && totalsilence < MEETME_DELAYDETECTTALK) {
1348                                                         user->talking = 1;
1349                                                         if (confflags & CONFFLAG_MONITORTALKER)
1350                                                                 manager_event(EVENT_FLAG_CALL, "MeetmeTalking",
1351                                                                       "Channel: %s\r\n"
1352                                                                       "Uniqueid: %s\r\n"
1353                                                                       "Meetme: %s\r\n"
1354                                                                       "Usernum: %d\r\n",
1355                                                                       chan->name, chan->uniqueid, conf->confno, user->user_no);
1356                                                 }
1357                                                 if (user->talking && totalsilence > MEETME_DELAYDETECTENDTALK) {
1358                                                         user->talking = 0;
1359                                                         if (confflags & CONFFLAG_MONITORTALKER)
1360                                                                 manager_event(EVENT_FLAG_CALL, "MeetmeStopTalking",
1361                                                                       "Channel: %s\r\n"
1362                                                                       "Uniqueid: %s\r\n"
1363                                                                       "Meetme: %s\r\n"
1364                                                                       "Usernum: %d\r\n",
1365                                                                       chan->name, chan->uniqueid, conf->confno, user->user_no);
1366                                                 }
1367                                         }
1368                                         if (using_pseudo) {
1369                                                 /* Absolutely do _not_ use careful_write here...
1370                                                    it is important that we read data from the channel
1371                                                    as fast as it arrives, and feed it into the conference.
1372                                                    The buffering in the pseudo channel will take care of any
1373                                                    timing differences, unless they are so drastic as to lose
1374                                                    audio frames (in which case carefully writing would only
1375                                                    have delayed the audio even further).
1376                                                 */
1377                                                 /* As it turns out, we do want to use careful write.  We just
1378                                                    don't want to block, but we do want to at least *try*
1379                                                    to write out all the samples.
1380                                                  */
1381                                                 if (user->talking || !(confflags & CONFFLAG_OPTIMIZETALKER))
1382                                                         careful_write(fd, f->data, f->datalen, 0);
1383                                         }
1384                                 } else if ((f->frametype == AST_FRAME_DTMF) && (confflags & CONFFLAG_EXIT_CONTEXT)) {
1385                                         char tmp[2];
1386
1387                                         tmp[0] = f->subclass;
1388                                         tmp[1] = '\0';
1389                                         if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
1390                                                 ast_log(LOG_DEBUG, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext);
1391                                                 ret = 0;
1392                                                 break;
1393                                         } else if (option_debug > 1)
1394                                                 ast_log(LOG_DEBUG, "Exit by single digit did not work in meetme. Extension %s does not exist in context %s\n", tmp, exitcontext);
1395                                 } else if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#') && (confflags & CONFFLAG_POUNDEXIT)) {
1396                                         ret = 0;
1397                                         break;
1398                                 } else if (((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*') && (confflags & CONFFLAG_STARMENU)) || ((f->frametype == AST_FRAME_DTMF) && menu_active)) {
1399                                         if (ioctl(fd, ZT_SETCONF, &ztc_empty)) {
1400                                                 ast_log(LOG_WARNING, "Error setting conference\n");
1401                                                 close(fd);
1402                                                 goto outrun;
1403                                         }
1404
1405                                         /* if we are entering the menu, and the user has a channel-driver
1406                                            volume adjustment, clear it
1407                                         */
1408                                         if (!menu_active && user->talk.desired && !user->talk.actual)
1409                                                 set_talk_volume(user, 0);
1410
1411                                         if (musiconhold) {
1412                                                 ast_moh_stop(chan);
1413                                         }
1414                                         if ((confflags & CONFFLAG_ADMIN)) {
1415                                                 /* Admin menu */
1416                                                 if (!menu_active) {
1417                                                         menu_active = 1;
1418                                                         /* Record this sound! */
1419                                                         if (!ast_streamfile(chan, "conf-adminmenu", chan->language)) {
1420                                                                 dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
1421                                                                 ast_stopstream(chan);
1422                                                         } else 
1423                                                                 dtmf = 0;
1424                                                 } else 
1425                                                         dtmf = f->subclass;
1426                                                 if (dtmf) {
1427                                                         switch(dtmf) {
1428                                                         case '1': /* Un/Mute */
1429                                                                 menu_active = 0;
1430                                                                 if (ztc.confmode & ZT_CONF_TALKER) {
1431                                                                         ztc.confmode = ZT_CONF_CONF | ZT_CONF_LISTENER;
1432                                                                         confflags |= CONFFLAG_MONITOR ^ CONFFLAG_TALKER;
1433                                                                 } else {
1434                                                                         ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1435                                                                         confflags ^= CONFFLAG_MONITOR | CONFFLAG_TALKER;
1436                                                                 }
1437                                                                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
1438                                                                         ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
1439                                                                         ret = -1;
1440                                                                         break;
1441                                                                 }
1442                                                                 if (ztc.confmode & ZT_CONF_TALKER) {
1443                                                                         if (!ast_streamfile(chan, "conf-unmuted", chan->language))
1444                                                                                 ast_waitstream(chan, "");
1445                                                                 } else {
1446                                                                         if (!ast_streamfile(chan, "conf-muted", chan->language))
1447                                                                                 ast_waitstream(chan, "");
1448                                                                 }
1449                                                                 break;
1450                                                         case '2': /* Un/Lock the Conference */
1451                                                                 menu_active = 0;
1452                                                                 if (conf->locked) {
1453                                                                         conf->locked = 0;
1454                                                                         if (!ast_streamfile(chan, "conf-unlockednow", chan->language))
1455                                                                                 ast_waitstream(chan, "");
1456                                                                 } else {
1457                                                                         conf->locked = 1;
1458                                                                         if (!ast_streamfile(chan, "conf-lockednow", chan->language))
1459                                                                                 ast_waitstream(chan, "");
1460                                                                 }
1461                                                                 break;
1462                                                         case '3': /* Eject last user */
1463                                                                 menu_active = 0;
1464                                                                 usr = conf->lastuser;
1465                                                                 if ((usr->chan->name == chan->name)||(usr->userflags & CONFFLAG_ADMIN)) {
1466                                                                         if(!ast_streamfile(chan, "conf-errormenu", chan->language))
1467                                                                                 ast_waitstream(chan, "");
1468                                                                 } else 
1469                                                                         usr->adminflags |= ADMINFLAG_KICKME;
1470                                                                 ast_stopstream(chan);
1471                                                                 break;  
1472                                                         case '4':
1473                                                                 tweak_listen_volume(user, VOL_DOWN);
1474                                                                 break;
1475                                                         case '6':
1476                                                                 tweak_listen_volume(user, VOL_UP);
1477                                                                 break;
1478                                                         case '7':
1479                                                                 tweak_talk_volume(user, VOL_DOWN);
1480                                                                 break;
1481                                                         case '8':
1482                                                                 menu_active = 0;
1483                                                                 break;
1484                                                         case '9':
1485                                                                 tweak_talk_volume(user, VOL_UP);
1486                                                                 break;
1487                                                         default:
1488                                                                 menu_active = 0;
1489                                                                 /* Play an error message! */
1490                                                                 if (!ast_streamfile(chan, "conf-errormenu", chan->language))
1491                                                                         ast_waitstream(chan, "");
1492                                                                 break;
1493                                                         }
1494                                                 }
1495                                         } else {
1496                                                 /* User menu */
1497                                                 if (!menu_active) {
1498                                                         menu_active = 1;
1499                                                         if (!ast_streamfile(chan, "conf-usermenu", chan->language)) {
1500                                                                 dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
1501                                                                 ast_stopstream(chan);
1502                                                         } else
1503                                                                 dtmf = 0;
1504                                                 } else 
1505                                                         dtmf = f->subclass;
1506                                                 if (dtmf) {
1507                                                         switch(dtmf) {
1508                                                         case '1': /* Un/Mute */
1509                                                                 menu_active = 0;
1510                                                                 if (ztc.confmode & ZT_CONF_TALKER) {
1511                                                                         ztc.confmode = ZT_CONF_CONF | ZT_CONF_LISTENER;
1512                                                                         confflags |= CONFFLAG_MONITOR ^ CONFFLAG_TALKER;
1513                                                                 } else if (!(user->adminflags & ADMINFLAG_MUTED)) {
1514                                                                         ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1515                                                                         confflags ^= CONFFLAG_MONITOR | CONFFLAG_TALKER;
1516                                                                 }
1517                                                                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
1518                                                                         ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
1519                                                                         ret = -1;
1520                                                                         break;
1521                                                                 }
1522                                                                 if (ztc.confmode & ZT_CONF_TALKER) {
1523                                                                         if (!ast_streamfile(chan, "conf-unmuted", chan->language))
1524                                                                                 ast_waitstream(chan, "");
1525                                                                 } else {
1526                                                                         if (!ast_streamfile(chan, "conf-muted", chan->language))
1527                                                                                 ast_waitstream(chan, "");
1528                                                                 }
1529                                                                 break;
1530                                                         case '4':
1531                                                                 tweak_listen_volume(user, VOL_DOWN);
1532                                                                 break;
1533                                                         case '6':
1534                                                                 tweak_listen_volume(user, VOL_UP);
1535                                                                 break;
1536                                                         case '7':
1537                                                                 tweak_talk_volume(user, VOL_DOWN);
1538                                                                 break;
1539                                                         case '8':
1540                                                                 menu_active = 0;
1541                                                                 break;
1542                                                         case '9':
1543                                                                 tweak_talk_volume(user, VOL_UP);
1544                                                                 break;
1545                                                         default:
1546                                                                 menu_active = 0;
1547                                                                 if (!ast_streamfile(chan, "conf-errormenu", chan->language))
1548                                                                         ast_waitstream(chan, "");
1549                                                                 break;
1550                                                         }
1551                                                 }
1552                                         }
1553                                         if (musiconhold)
1554                                                 ast_moh_start(chan, NULL);
1555
1556                                         if (ioctl(fd, ZT_SETCONF, &ztc)) {
1557                                                 ast_log(LOG_WARNING, "Error setting conference\n");
1558                                                 close(fd);
1559                                                 AST_LIST_UNLOCK(&confs);
1560                                                 goto outrun;
1561                                         }
1562
1563                                         conf_flush(fd, chan);
1564                                 } else if (option_debug) {
1565                                         ast_log(LOG_DEBUG,
1566                                                 "Got unrecognized frame on channel %s, f->frametype=%d,f->subclass=%d\n",
1567                                                 chan->name, f->frametype, f->subclass);
1568                                 }
1569                                 ast_frfree(f);
1570                         } else if (outfd > -1) {
1571                                 res = read(outfd, buf, CONF_SIZE);
1572                                 if (res > 0) {
1573                                         memset(&fr, 0, sizeof(fr));
1574                                         fr.frametype = AST_FRAME_VOICE;
1575                                         fr.subclass = AST_FORMAT_SLINEAR;
1576                                         fr.datalen = res;
1577                                         fr.samples = res/2;
1578                                         fr.data = buf;
1579                                         fr.offset = AST_FRIENDLY_OFFSET;
1580                                         if (!user->listen.actual && 
1581                                                 ((confflags & CONFFLAG_MONITOR) || 
1582                                                  (user->adminflags & ADMINFLAG_MUTED) ||
1583                                                  (!user->talking && (confflags & CONFFLAG_OPTIMIZETALKER))
1584                                                  )) {
1585                                                 int index;
1586                                                 for (index=0;index<AST_FRAME_BITS;index++)
1587                                                         if (chan->rawwriteformat & (1 << index))
1588                                                                 break;
1589                                                 if (index >= AST_FRAME_BITS)
1590                                                         goto bailoutandtrynormal;
1591                                                 ast_mutex_lock(&conf->listenlock);
1592                                                 if (!conf->transframe[index]) {
1593                                                         if (conf->origframe) {
1594                                                                 if (!conf->transpath[index])
1595                                                                         conf->transpath[index] = ast_translator_build_path((1 << index), AST_FORMAT_SLINEAR);
1596                                                                 if (conf->transpath[index]) {
1597                                                                         conf->transframe[index] = ast_translate(conf->transpath[index], conf->origframe, 0);
1598                                                                         if (!conf->transframe[index])
1599                                                                                 conf->transframe[index] = &ast_null_frame;
1600                                                                 }
1601                                                         }
1602                                                 }
1603                                                 if (conf->transframe[index]) {
1604                                                         if (conf->transframe[index]->frametype != AST_FRAME_NULL) {
1605                                                                 if (ast_write(chan, conf->transframe[index]))
1606                                                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
1607                                                         }
1608                                                 } else {
1609                                                         ast_mutex_unlock(&conf->listenlock);
1610                                                         goto bailoutandtrynormal;
1611                                                 }
1612                                                 ast_mutex_unlock(&conf->listenlock);
1613                                         } else {
1614 bailoutandtrynormal:                                    
1615                                                 if (user->listen.actual)
1616                                                         ast_frame_adjust_volume(&fr, user->listen.actual);
1617                                                 if (ast_write(chan, &fr) < 0) {
1618                                                         ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
1619                                                 }
1620                                         }
1621                                 } else 
1622                                         ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
1623                         }
1624                         lastmarked = currentmarked;
1625                 }
1626         }
1627
1628         if (musiconhold)
1629                 ast_moh_stop(chan);
1630         
1631         if (using_pseudo)
1632                 close(fd);
1633         else {
1634                 /* Take out of conference */
1635                 ztc.chan = 0;   
1636                 ztc.confno = 0;
1637                 ztc.confmode = 0;
1638                 if (ioctl(fd, ZT_SETCONF, &ztc)) {
1639                         ast_log(LOG_WARNING, "Error setting conference\n");
1640                 }
1641         }
1642
1643         reset_volumes(user);
1644
1645         AST_LIST_LOCK(&confs);
1646         if (!(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN))
1647                 conf_play(chan, conf, LEAVE);
1648
1649         if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_INTROUSER)) {
1650                 if (ast_fileexists(user->namerecloc, NULL, NULL)) {
1651                         if ((conf->chan) && (conf->users > 1)) {
1652                                 if (!ast_streamfile(conf->chan, user->namerecloc, chan->language))
1653                                         ast_waitstream(conf->chan, "");
1654                                 if (!ast_streamfile(conf->chan, "conf-hasleft", chan->language))
1655                                         ast_waitstream(conf->chan, "");
1656                         }
1657                         ast_filedelete(user->namerecloc, NULL);
1658                 }
1659         }
1660         AST_LIST_UNLOCK(&confs);
1661
1662  outrun:
1663         AST_LIST_LOCK(&confs);
1664
1665         if (dsp)
1666                 ast_dsp_free(dsp);
1667         
1668         if (user->user_no) { /* Only cleanup users who really joined! */
1669                 now = time(NULL);
1670                 hr = (now - user->jointime) / 3600;
1671                 min = ((now - user->jointime) % 3600) / 60;
1672                 sec = (now - user->jointime) % 60;
1673
1674                 manager_event(EVENT_FLAG_CALL, "MeetmeLeave",
1675                         "Channel: %s\r\n"
1676                         "Uniqueid: %s\r\n"
1677                         "Meetme: %s\r\n"
1678                         "Usernum: %d\r\n"
1679                         "CIDnum: %s\r\n"
1680                         "CIDname: %s\r\n"
1681                         "Duration: %02d:%02d:%02d\r\n",
1682                          chan->name, chan->uniqueid, conf->confno, 
1683                           user->user_no,
1684                           user->chan->cid.cid_num ? user->chan->cid.cid_num :
1685                          "<unknown>",
1686                          user->chan->cid.cid_name ? user->chan->cid.cid_name :
1687                           "<no name>", hr, min, sec);
1688
1689                 conf->users--;
1690                 conf->refcount--;
1691                 /* Update table */
1692                 snprintf(members, sizeof(members), "%d", conf->users);
1693                 ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
1694                 if (confflags & CONFFLAG_MARKEDUSER) 
1695                         conf->markedusers--;
1696                 if (!conf->users) {
1697                         /* close this one when no more users and no references*/
1698                         if (!conf->refcount){
1699                                 conf_free(conf);
1700                         }
1701                 } else {
1702                         /* Remove the user struct */ 
1703                         if (user == conf->firstuser) {
1704                                 if (user->nextuser) {
1705                                         /* There is another entry */
1706                                         user->nextuser->prevuser = NULL;
1707                                 } else {
1708                                         /* We are the only entry */
1709                                         conf->lastuser = NULL;
1710                                 }
1711                                 /* In either case */
1712                                 conf->firstuser = user->nextuser;
1713                         } else if (user == conf->lastuser){
1714                                 if (user->prevuser)
1715                                         user->prevuser->nextuser = NULL;
1716                                 else
1717                                         ast_log(LOG_ERROR, "Bad bad bad!  We're the last, not the first, but nobody before us??\n");
1718                                 conf->lastuser = user->prevuser;
1719                         } else {
1720                                 if (user->nextuser)
1721                                         user->nextuser->prevuser = user->prevuser;
1722                                 else
1723                                         ast_log(LOG_ERROR, "Bad! Bad! Bad! user->nextuser is NULL but we're not the end!\n");
1724                                 if (user->prevuser)
1725                                         user->prevuser->nextuser = user->nextuser;
1726                                 else
1727                                         ast_log(LOG_ERROR, "Bad! Bad! Bad! user->prevuser is NULL but we're not the beginning!\n");
1728                         }
1729                 }
1730                 /* Return the number of seconds the user was in the conf */
1731                 snprintf(meetmesecs, sizeof(meetmesecs), "%d", (int) (time(NULL) - user->jointime));
1732                 pbx_builtin_setvar_helper(chan, "MEETMESECS", meetmesecs);
1733         }
1734         free(user);
1735         AST_LIST_UNLOCK(&confs);
1736
1737         return ret;
1738 }
1739
1740 /*
1741   This function looks for a conference via the RealTime module
1742 */
1743 static struct ast_conference *find_conf_realtime(struct ast_channel *chan, char *confno, int make, int dynamic, char *dynamic_pin, int refcount)
1744 {
1745
1746         struct ast_variable *var;
1747         struct ast_conference *cnf;
1748
1749         /* Check first in the conference list */
1750         AST_LIST_LOCK(&confs);
1751         AST_LIST_TRAVERSE(&confs, cnf, list) {
1752                 if (!strcmp(confno, cnf->confno)) 
1753                         break;
1754         }
1755         if (cnf){
1756                 cnf->refcount += refcount;
1757         }
1758         AST_LIST_UNLOCK(&confs);
1759
1760         if (!cnf) {
1761                 char *pin = NULL, *pinadmin = NULL; /* For temp use */
1762
1763                 cnf = ast_calloc(1, sizeof(struct ast_conference));
1764                 if (!cnf) {
1765                         ast_log(LOG_ERROR, "Out of memory\n");
1766                         return NULL;
1767                 }
1768
1769                 var = ast_load_realtime("meetme", "confno", confno, NULL);
1770                 while (var) {
1771                         if (!strcasecmp(var->name, "confno")) {
1772                                 ast_copy_string(cnf->confno, var->value, sizeof(cnf->confno));
1773                         } else if (!strcasecmp(var->name, "pin")) {
1774                                 pin = ast_strdupa(var->value);
1775                         } else if (!strcasecmp(var->name, "adminpin")) {
1776                                 pinadmin = ast_strdupa(var->value);
1777                         }
1778                         var = var->next;
1779                 }
1780                 ast_variables_destroy(var);
1781
1782                 cnf = build_conf(confno, pin ? pin : "", pinadmin ? pinadmin : "", make, dynamic, refcount);
1783         }
1784
1785         return cnf;
1786 }
1787
1788
1789 static struct ast_conference *find_conf(struct ast_channel *chan, char *confno, int make, int dynamic, char *dynamic_pin, int refcount)
1790 {
1791         struct ast_config *cfg;
1792         struct ast_variable *var;
1793         struct ast_conference *cnf;
1794         char *parse;
1795         AST_DECLARE_APP_ARGS(args,
1796                 AST_APP_ARG(confno);
1797                 AST_APP_ARG(pin);
1798                 AST_APP_ARG(pinadmin);
1799         );
1800
1801         /* Check first in the conference list */
1802         AST_LIST_LOCK(&confs);
1803         AST_LIST_TRAVERSE(&confs, cnf, list) {
1804                 if (!strcmp(confno, cnf->confno)) 
1805                         break;
1806         }
1807         if (cnf){
1808                 cnf->refcount += refcount;
1809         }
1810         AST_LIST_UNLOCK(&confs);
1811
1812         if (!cnf) {
1813                 if (dynamic) {
1814                         /* No need to parse meetme.conf */
1815                         ast_log(LOG_DEBUG, "Building dynamic conference '%s'\n", confno);
1816                         if (dynamic_pin) {
1817                                 if (dynamic_pin[0] == 'q') {
1818                                         /* Query the user to enter a PIN */
1819                                         if (ast_app_getdata(chan, "conf-getpin", dynamic_pin, AST_MAX_EXTENSION - 1, 0) < 0)
1820                                                 return NULL;
1821                                 }
1822                                 cnf = build_conf(confno, dynamic_pin, "", make, dynamic, refcount);
1823                         } else {
1824                                 cnf = build_conf(confno, "", "", make, dynamic, refcount);
1825                         }
1826                 } else {
1827                         /* Check the config */
1828                         cfg = ast_config_load(CONFIG_FILE_NAME);
1829                         if (!cfg) {
1830                                 ast_log(LOG_WARNING, "No %s file :(\n", CONFIG_FILE_NAME);
1831                                 return NULL;
1832                         }
1833                         for (var = ast_variable_browse(cfg, "rooms"); var; var = var->next) {
1834                                 if (strcasecmp(var->name, "conf"))
1835                                         continue;
1836                                 
1837                                 if (!(parse = ast_strdupa(var->value)))
1838                                         return NULL;
1839                                 
1840                                 AST_NONSTANDARD_APP_ARGS(args, parse, ',');
1841                                 if (!strcasecmp(args.confno, confno)) {
1842                                         /* Bingo it's a valid conference */
1843                                         cnf = build_conf(args.confno,
1844                                                         ast_strlen_zero(args.pin) ? "" : args.pin,
1845                                                         ast_strlen_zero(args.pinadmin) ? "" : args.pinadmin,
1846                                                         make, dynamic, refcount);
1847                                         break;
1848                                 }
1849                         }
1850                         if (!var) {
1851                                 ast_log(LOG_DEBUG, "%s isn't a valid conference\n", confno);
1852                         }
1853                         ast_config_destroy(cfg);
1854                 }
1855         } else if (dynamic_pin) {
1856                 /* Correct for the user selecting 'D' instead of 'd' to have
1857                    someone join into a conference that has already been created
1858                    with a pin. */
1859                 if (dynamic_pin[0] == 'q')
1860                         dynamic_pin[0] = '\0';
1861         }
1862
1863         return cnf;
1864 }
1865
1866 /*! \brief The MeetmeCount application */
1867 static int count_exec(struct ast_channel *chan, void *data)
1868 {
1869         struct localuser *u;
1870         int res = 0;
1871         struct ast_conference *conf;
1872         int count;
1873         char *localdata;
1874         char val[80] = "0"; 
1875         AST_DECLARE_APP_ARGS(args,
1876                 AST_APP_ARG(confno);
1877                 AST_APP_ARG(varname);
1878         );
1879
1880         if (ast_strlen_zero(data)) {
1881                 ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
1882                 return -1;
1883         }
1884
1885         LOCAL_USER_ADD(u);
1886         
1887         if (!(localdata = ast_strdupa(data))) {
1888                 LOCAL_USER_REMOVE(u);
1889                 return -1;
1890         }
1891
1892         AST_STANDARD_APP_ARGS(args, localdata);
1893         
1894         conf = find_conf(chan, args.confno, 0, 0, NULL, 0);
1895         if (conf)
1896                 count = conf->users;
1897         else
1898                 count = 0;
1899
1900         if (!ast_strlen_zero(args.varname)){
1901                 /* have var so load it and exit */
1902                 snprintf(val, sizeof(val), "%d",count);
1903                 pbx_builtin_setvar_helper(chan, args.varname, val);
1904         } else {
1905                 if (chan->_state != AST_STATE_UP)
1906                         ast_answer(chan);
1907                 res = ast_say_number(chan, count, "", chan->language, (char *) NULL); /* Needs gender */
1908         }
1909         LOCAL_USER_REMOVE(u);
1910
1911         return res;
1912 }
1913
1914 /*! \brief The meetme() application */
1915 static int conf_exec(struct ast_channel *chan, void *data)
1916 {
1917         int res=-1;
1918         struct localuser *u;
1919         char confno[AST_MAX_EXTENSION] = "";
1920         int allowretry = 0;
1921         int retrycnt = 0;
1922         struct ast_conference *cnf;
1923         struct ast_flags confflags = {0};
1924         int dynamic = 0;
1925         int empty = 0, empty_no_pin = 0;
1926         int always_prompt = 0;
1927         char *notdata, *info, the_pin[AST_MAX_EXTENSION] = "";
1928         AST_DECLARE_APP_ARGS(args,
1929                 AST_APP_ARG(confno);
1930                 AST_APP_ARG(options);
1931                 AST_APP_ARG(pin);
1932         );
1933
1934         LOCAL_USER_ADD(u);
1935
1936         if (ast_strlen_zero(data)) {
1937                 allowretry = 1;
1938                 notdata = "";
1939         } else {
1940                 notdata = data;
1941         }
1942         
1943         if (chan->_state != AST_STATE_UP)
1944                 ast_answer(chan);
1945
1946         info = ast_strdupa(notdata);
1947
1948         AST_STANDARD_APP_ARGS(args, info);      
1949
1950         if (args.confno) {
1951                 ast_copy_string(confno, args.confno, sizeof(confno));
1952                 if (ast_strlen_zero(confno)) {
1953                         allowretry = 1;
1954                 }
1955         }
1956         
1957         if (args.pin)
1958                 ast_copy_string(the_pin, args.pin, sizeof(the_pin));
1959
1960         if (args.options) {
1961                 ast_app_parse_options(meetme_opts, &confflags, NULL, args.options);
1962                 dynamic = ast_test_flag(&confflags, CONFFLAG_DYNAMIC | CONFFLAG_DYNAMICPIN);
1963                 if (ast_test_flag(&confflags, CONFFLAG_DYNAMICPIN) && !args.pin)
1964                         strcpy(the_pin, "q");
1965
1966                 empty = ast_test_flag(&confflags, CONFFLAG_EMPTY | CONFFLAG_EMPTYNOPIN);
1967                 empty_no_pin = ast_test_flag(&confflags, CONFFLAG_EMPTYNOPIN);
1968                 always_prompt = ast_test_flag(&confflags, CONFFLAG_ALWAYSPROMPT);
1969         }
1970
1971         do {
1972                 if (retrycnt > 3)
1973                         allowretry = 0;
1974                 if (empty) {
1975                         int i, map[1024] = { 0, };
1976                         struct ast_config *cfg;
1977                         struct ast_variable *var;
1978                         int confno_int;
1979
1980                         AST_LIST_LOCK(&confs);
1981                         AST_LIST_TRAVERSE(&confs, cnf, list) {
1982                                 if (sscanf(cnf->confno, "%d", &confno_int) == 1) {
1983                                         /* Disqualify in use conference */
1984                                         if (confno_int >= 0 && confno_int < 1024)
1985                                                 map[confno_int]++;
1986                                 }
1987                         }
1988                         AST_LIST_UNLOCK(&confs);
1989
1990                         /* We only need to load the config file for static and empty_no_pin (otherwise we don't care) */
1991                         if ((empty_no_pin) || (!dynamic)) {
1992                                 cfg = ast_config_load(CONFIG_FILE_NAME);
1993                                 if (cfg) {
1994                                         var = ast_variable_browse(cfg, "rooms");
1995                                         while (var) {
1996                                                 if (!strcasecmp(var->name, "conf")) {
1997                                                         char *stringp = ast_strdupa(var->value);
1998                                                         if (stringp) {
1999                                                                 char *confno_tmp = strsep(&stringp, "|,");
2000                                                                 int found = 0;
2001                                                                 if (sscanf(confno_tmp, "%d", &confno_int) == 1) {
2002                                                                         if ((confno_int >= 0) && (confno_int < 1024)) {
2003                                                                                 if (stringp && empty_no_pin) {
2004                                                                                         map[confno_int]++;
2005                                                                                 }
2006                                                                         }
2007                                                                 }
2008                                                                 if (!dynamic) {
2009                                                                         /* For static:  run through the list and see if this conference is empty */
2010                                                                         AST_LIST_LOCK(&confs);
2011                                                                         AST_LIST_TRAVERSE(&confs, cnf, list) {
2012                                                                                 if (!strcmp(confno_tmp, cnf->confno)) {
2013                                                                                         /* The conference exists, therefore it's not empty */
2014                                                                                         found = 1;
2015                                                                                         break;
2016                                                                                 }
2017                                                                         }
2018                                                                         AST_LIST_UNLOCK(&confs);
2019                                                                         if (!found) {
2020                                                                                 /* At this point, we have a confno_tmp (static conference) that is empty */
2021                                                                                 if ((empty_no_pin && ast_strlen_zero(stringp)) || (!empty_no_pin)) {
2022                                                                                         /* Case 1:  empty_no_pin and pin is nonexistent (NULL)
2023                                                                                          * Case 2:  empty_no_pin and pin is blank (but not NULL)
2024                                                                                          * Case 3:  not empty_no_pin
2025                                                                                          */
2026                                                                                         ast_copy_string(confno, confno_tmp, sizeof(confno));
2027                                                                                         break;
2028                                                                                         /* XXX the map is not complete (but we do have a confno) */
2029                                                                                 }
2030                                                                         }
2031                                                                 }
2032                                                         }
2033                                                 }
2034                                                 var = var->next;
2035                                         }
2036                                         ast_config_destroy(cfg);
2037                                 }
2038                         }
2039
2040                         /* Select first conference number not in use */
2041                         if (ast_strlen_zero(confno) && dynamic) {
2042                                 for (i = 0; i < sizeof(map) / sizeof(map[0]); i++) {
2043                                         if (!map[i]) {
2044                                                 snprintf(confno, sizeof(confno), "%d", i);
2045                                                 break;
2046                                         }
2047                                 }
2048                         }
2049
2050                         /* Not found? */
2051                         if (ast_strlen_zero(confno)) {
2052                                 res = ast_streamfile(chan, "conf-noempty", chan->language);
2053                                 if (!res)
2054                                         ast_waitstream(chan, "");
2055                         } else {
2056                                 if (sscanf(confno, "%d", &confno_int) == 1) {
2057                                         res = ast_streamfile(chan, "conf-enteringno", chan->language);
2058                                         if (!res) {
2059                                                 ast_waitstream(chan, "");
2060                                                 res = ast_say_digits(chan, confno_int, "", chan->language);
2061                                         }
2062                                 } else {
2063                                         ast_log(LOG_ERROR, "Could not scan confno '%s'\n", confno);
2064                                 }
2065                         }
2066                 }
2067
2068                 while (allowretry && (ast_strlen_zero(confno)) && (++retrycnt < 4)) {
2069                         /* Prompt user for conference number */
2070                         res = ast_app_getdata(chan, "conf-getconfno", confno, sizeof(confno) - 1, 0);
2071                         if (res < 0) {
2072                                 /* Don't try to validate when we catch an error */
2073                                 confno[0] = '\0';
2074                                 allowretry = 0;
2075                                 break;
2076                         }
2077                 }
2078                 if (!ast_strlen_zero(confno)) {
2079                         /* Check the validity of the conference */
2080                         cnf = find_conf(chan, confno, 1, dynamic, the_pin, 1);
2081                         if (!cnf) {
2082                                 cnf = find_conf_realtime(chan, confno, 1, dynamic, the_pin, 1);
2083                         }
2084                         if (!cnf) {
2085                                 res = ast_streamfile(chan, "conf-invalid", chan->language);
2086                                 if (!res)
2087                                         ast_waitstream(chan, "");
2088                                 res = -1;
2089                                 if (allowretry)
2090                                         confno[0] = '\0';
2091                         } else {
2092                                 if ((!ast_strlen_zero(cnf->pin) &&
2093                                      !ast_test_flag(&confflags, CONFFLAG_ADMIN)) ||
2094                                     (!ast_strlen_zero(cnf->pinadmin) &&
2095                                      ast_test_flag(&confflags, CONFFLAG_ADMIN))) {
2096                                         char pin[AST_MAX_EXTENSION]="";
2097                                         int j;
2098
2099                                         /* Allow the pin to be retried up to 3 times */
2100                                         for (j = 0; j < 3; j++) {
2101                                                 if (*the_pin && (always_prompt == 0)) {
2102                                                         ast_copy_string(pin, the_pin, sizeof(pin));
2103                                                         res = 0;
2104                                                 } else {
2105                                                         /* Prompt user for pin if pin is required */
2106                                                         res = ast_app_getdata(chan, "conf-getpin", pin + strlen(pin), sizeof(pin) - 1 - strlen(pin), 0);
2107                                                 }
2108                                                 if (res >= 0) {
2109                                                         if (!strcasecmp(pin, cnf->pin) ||
2110                                                             (!ast_strlen_zero(cnf->pinadmin) &&
2111                                                              !strcasecmp(pin, cnf->pinadmin))) {
2112                                                                 /* Pin correct */
2113                                                                 allowretry = 0;
2114                                                                 if (!ast_strlen_zero(cnf->pinadmin) && !strcasecmp(pin, cnf->pinadmin)) 
2115                                                                         ast_set_flag(&confflags, CONFFLAG_ADMIN);
2116                                                                 /* Run the conference */
2117                                                                 res = conf_run(chan, cnf, confflags.flags);
2118                                                                 break;
2119                                                         } else {
2120                                                                 /* Pin invalid */
2121                                                                 res = ast_streamfile(chan, "conf-invalidpin", chan->language);
2122                                                                 if (!res)
2123                                                                         ast_waitstream(chan, AST_DIGIT_ANY);
2124                                                                 if (res < 0) {
2125                                                                         AST_LIST_LOCK(&confs);
2126                                                                         cnf->refcount--;
2127                                                                         if (!cnf->refcount){
2128                                                                                 conf_free(cnf);
2129                                                                         }
2130                                                                         AST_LIST_UNLOCK(&confs);
2131                                                                         break;
2132                                                                 }
2133                                                                 pin[0] = res;
2134                                                                 pin[1] = '\0';
2135                                                                 res = -1;
2136                                                                 if (allowretry)
2137                                                                         confno[0] = '\0';
2138                                                         }
2139                                                 } else {
2140                                                         /* failed when getting the pin */
2141                                                         res = -1;
2142                                                         allowretry = 0;
2143                                                         /* see if we need to get rid of the conference */
2144                                                         AST_LIST_LOCK(&confs);
2145                                                         cnf->refcount--;
2146                                                         if (!cnf->refcount) {
2147                                                                 conf_free(cnf);
2148                                                         }
2149                                                         AST_LIST_UNLOCK(&confs);
2150                                                         break;
2151                                                 }
2152
2153                                                 /* Don't retry pin with a static pin */
2154                                                 if (*the_pin && (always_prompt==0)) {
2155                                                         break;
2156                                                 }
2157                                         }
2158                                 } else {
2159                                         /* No pin required */
2160                                         allowretry = 0;
2161
2162                                         /* Run the conference */
2163                                         res = conf_run(chan, cnf, confflags.flags);
2164                                 }
2165                         }
2166                 }
2167         } while (allowretry);
2168         
2169         LOCAL_USER_REMOVE(u);
2170         
2171         return res;
2172 }
2173
2174 static struct ast_conf_user* find_user(struct ast_conference *conf, char *callerident) 
2175 {
2176         struct ast_conf_user *user = NULL;
2177         int cid;
2178         
2179         sscanf(callerident, "%i", &cid);
2180         if (conf && callerident) {
2181                 user = conf->firstuser;
2182                 while (user) {
2183                         if (cid == user->user_no)
2184                                 return user;
2185                         user = user->nextuser;
2186                 }
2187         }
2188         return NULL;
2189 }
2190
2191 /*! \brief The MeetMeadmin application */
2192 /* MeetMeAdmin(confno, command, caller) */
2193 static int admin_exec(struct ast_channel *chan, void *data) {
2194         char *params;
2195         struct ast_conference *cnf;
2196         struct ast_conf_user *user = NULL;
2197         struct localuser *u;
2198         AST_DECLARE_APP_ARGS(args,
2199                 AST_APP_ARG(confno);
2200                 AST_APP_ARG(command);
2201                 AST_APP_ARG(user);
2202         );
2203         
2204         LOCAL_USER_ADD(u);
2205
2206         AST_LIST_LOCK(&confs);
2207         /* The param has the conference number the user and the command to execute */
2208         if (!ast_strlen_zero(data)) {           
2209                 params = ast_strdupa((char *) data);
2210
2211                 AST_STANDARD_APP_ARGS(args, params);
2212
2213                 if (!args.command) {
2214                         ast_log(LOG_WARNING, "MeetmeAdmin requires a command!\n");
2215                         AST_LIST_UNLOCK(&confs);
2216                         LOCAL_USER_REMOVE(u);
2217                         return -1;
2218                 }
2219                 AST_LIST_TRAVERSE(&confs, cnf, list) {
2220                         if (!strcmp(cnf->confno, args.confno))
2221                                 break;
2222                 }
2223                 
2224                 if (args.user)
2225                         user = find_user(cnf, args.user);
2226                 
2227                 if (cnf) {
2228                         switch((int) (*args.command)) {
2229                         case 76: /* L: Lock */ 
2230                                 cnf->locked = 1;
2231                                 break;
2232                         case 108: /* l: Unlock */ 
2233                                 cnf->locked = 0;
2234                                 break;
2235                         case 75: /* K: kick all users*/
2236                                 user = cnf->firstuser;
2237                                 while(user) {
2238                                         user->adminflags |= ADMINFLAG_KICKME;
2239                                         if (user->nextuser) {
2240                                                 user = user->nextuser;
2241                                         } else {
2242                                                 break;
2243                                         }
2244                                 }
2245                                 break;
2246                         case 101: /* e: Eject last user*/
2247                                 user = cnf->lastuser;
2248                                 if (!(user->userflags & CONFFLAG_ADMIN)) {
2249                                         user->adminflags |= ADMINFLAG_KICKME;
2250                                         break;
2251                                 } else
2252                                         ast_log(LOG_NOTICE, "Not kicking last user, is an Admin!\n");
2253                                 break;
2254                         case 77: /* M: Mute */ 
2255                                 if (user) {
2256                                         user->adminflags |= ADMINFLAG_MUTED;
2257                                 } else {
2258                                         ast_log(LOG_NOTICE, "Specified User not found!\n");
2259                                 }
2260                                 break;
2261                         case 78: /* N: Mute all users */
2262                                 user = cnf->firstuser;
2263                                 while(user) {
2264                                         if (user && !(user->userflags & CONFFLAG_ADMIN))
2265                                                 user->adminflags |= ADMINFLAG_MUTED;
2266                                         if (user->nextuser) {
2267                                                 user = user->nextuser;
2268                                         } else {
2269                                                 break;
2270                                         }
2271                                 }
2272                                 break;                                  
2273                         case 109: /* m: Unmute */ 
2274                                 if (user && (user->adminflags & ADMINFLAG_MUTED)) {
2275                                         user->adminflags ^= ADMINFLAG_MUTED;
2276                                 } else {
2277                                         ast_log(LOG_NOTICE, "Specified User not found or he muted himself!\n");
2278                                 }
2279                                 break;
2280                         case  110: /* n: Unmute all users */
2281                                 user = cnf->firstuser;
2282                                 while(user) {
2283                                         if (user && (user-> adminflags & ADMINFLAG_MUTED)) {
2284                                                 user->adminflags ^= ADMINFLAG_MUTED;
2285                                         }
2286                                         if (user->nextuser) {
2287                                                 user = user->nextuser;
2288                                         } else {
2289                                                 break;
2290                                         }
2291                                 }
2292                                 break;
2293                         case 107: /* k: Kick user */ 
2294                                 if (user) {
2295                                         user->adminflags |= ADMINFLAG_KICKME;
2296                                 } else {
2297                                         ast_log(LOG_NOTICE, "Specified User not found!");
2298                                 }
2299                                 break;
2300                         }
2301                 } else {
2302                         ast_log(LOG_NOTICE, "Conference Number not found\n");
2303                 }
2304         }
2305         AST_LIST_UNLOCK(&confs);
2306
2307         LOCAL_USER_REMOVE(u);
2308         
2309         return 0;
2310 }
2311
2312 static void *recordthread(void *args)
2313 {
2314         struct ast_conference *cnf = args;
2315         struct ast_frame *f=NULL;
2316         int flags;
2317         struct ast_filestream *s=NULL;
2318         int res=0;
2319         int x;
2320         const char *oldrecordingfilename = NULL;
2321
2322         if (!cnf || !cnf->lchan) {
2323                 pthread_exit(0);
2324         }
2325
2326         ast_stopstream(cnf->lchan);
2327         flags = O_CREAT|O_TRUNC|O_WRONLY;
2328
2329
2330         cnf->recording = MEETME_RECORD_ACTIVE;
2331         while (ast_waitfor(cnf->lchan, -1) > -1) {
2332                 if (cnf->recording == MEETME_RECORD_TERMINATE) {
2333                         AST_LIST_LOCK(&confs);
2334                         AST_LIST_UNLOCK(&confs);
2335                         break;
2336                 }
2337                 if (!s && cnf->recordingfilename && (cnf->recordingfilename != oldrecordingfilename)) {
2338                         s = ast_writefile(cnf->recordingfilename, cnf->recordingformat, NULL, flags, 0, 0644);
2339                         oldrecordingfilename = cnf->recordingfilename;
2340                 }
2341                 
2342                 f = ast_read(cnf->lchan);
2343                 if (!f) {
2344                         res = -1;
2345                         break;
2346                 }
2347                 if (f->frametype == AST_FRAME_VOICE) {
2348                         ast_mutex_lock(&cnf->listenlock);
2349                         for (x=0;x<AST_FRAME_BITS;x++) {
2350                                 /* Free any translations that have occured */
2351                                 if (cnf->transframe[x]) {
2352                                         ast_frfree(cnf->transframe[x]);
2353                                         cnf->transframe[x] = NULL;
2354                                 }
2355                         }
2356                         if (cnf->origframe)
2357                                 ast_frfree(cnf->origframe);
2358                         cnf->origframe = f;
2359                         ast_mutex_unlock(&cnf->listenlock);
2360                         if (s)
2361                                 res = ast_writestream(s, f);
2362                         if (res) {
2363                                 ast_frfree(f);
2364                                 break;
2365                         }
2366                 }
2367                 ast_frfree(f);
2368         }
2369         cnf->recording = MEETME_RECORD_OFF;
2370         if (s)
2371                 ast_closestream(s);
2372         
2373         pthread_exit(0);
2374 }
2375
2376 static void load_config(void)
2377 {
2378         struct ast_config *cfg;
2379         char *val;
2380
2381         audio_buffers = DEFAULT_AUDIO_BUFFERS;
2382
2383         if (!(cfg = ast_config_load(CONFIG_FILE_NAME)))
2384                 return;
2385
2386         if ((val = ast_variable_retrieve(cfg, "general", "audiobuffers"))) {
2387                 if ((sscanf(val, "%d", &audio_buffers) != 1)) {
2388                         ast_log(LOG_WARNING, "audiobuffers setting must be a number, not '%s'\n", val);
2389                         audio_buffers = DEFAULT_AUDIO_BUFFERS;
2390                 } else if ((audio_buffers < ZT_DEFAULT_NUM_BUFS) || (audio_buffers > ZT_MAX_NUM_BUFS)) {
2391                         ast_log(LOG_WARNING, "audiobuffers setting must be between %d and %d\n",
2392                                 ZT_DEFAULT_NUM_BUFS, ZT_MAX_NUM_BUFS);
2393                         audio_buffers = DEFAULT_AUDIO_BUFFERS;
2394                 }
2395                 if (audio_buffers != DEFAULT_AUDIO_BUFFERS)
2396                         ast_log(LOG_NOTICE, "Audio buffers per channel set to %d\n", audio_buffers);
2397         }
2398
2399         ast_config_destroy(cfg);
2400 }
2401
2402 int unload_module(void)
2403 {
2404         int res;
2405         
2406         res = ast_cli_unregister(&cli_show_confs);
2407         res |= ast_cli_unregister(&cli_conf);
2408         res |= ast_unregister_application(app3);
2409         res |= ast_unregister_application(app2);
2410         res |= ast_unregister_application(app);
2411
2412         STANDARD_HANGUP_LOCALUSERS;
2413
2414         return res;
2415 }
2416
2417 int load_module(void)
2418 {
2419         int res;
2420
2421         load_config();
2422
2423         res = ast_cli_register(&cli_show_confs);
2424         res |= ast_cli_register(&cli_conf);
2425         res |= ast_register_application(app3, admin_exec, synopsis3, descrip3);
2426         res |= ast_register_application(app2, count_exec, synopsis2, descrip2);
2427         res |= ast_register_application(app, conf_exec, synopsis, descrip);
2428
2429         return res;
2430 }
2431
2432 int reload(void)
2433 {
2434         load_config();
2435
2436         return 0;
2437 }
2438
2439 char *description(void)
2440 {
2441         return (char *) tdesc;
2442 }
2443
2444 int usecount(void)
2445 {
2446         int res;
2447
2448         STANDARD_USECOUNT(res);
2449
2450         return res;
2451 }
2452
2453 char *key()
2454 {
2455         return ASTERISK_GPL_KEY;
2456 }
2457