From d5930d1fe84a2e34289911f5bbd0b700fc2fb32a Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Mon, 13 May 2002 22:29:39 +0000
Subject: [PATCH] Version 0.1.12 from FTP

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@446 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_directory.c           |   6 +-
 apps/app_disa.c                | 175 +++++-------
 apps/app_echo.c                |   6 +-
 apps/app_getcpeid.c            | 149 ++++++++++
 apps/app_image.c               |   1 +
 apps/app_intercom.c            |   3 +-
 apps/app_meetme.c              | 501 +++++++++++++++++++++++++++++++++
 apps/app_mp3.c                 |   1 +
 apps/app_playback.c            |   1 +
 apps/app_qcall.c               |  86 +++---
 apps/app_url.c                 |   1 +
 apps/app_zapras.c              |   5 +-
 cdr.c                          |   3 +-
 channels/chan_modem_aopen.c    |   4 +-
 channels/chan_modem_bestdata.c |   4 +-
 channels/chan_modem_i4l.c      |  12 +-
 cli.c                          |  31 +-
 codecs/codec_a_mu.c            |   3 +-
 codecs/codec_adpcm.c           |   3 +-
 codecs/codec_alaw.c            |   3 +-
 codecs/codec_g723_1.c          |   2 +-
 codecs/codec_gsm.c             |   3 +-
 codecs/codec_lpc10.c           |   3 +-
 codecs/codec_mp3_d.c           |   3 +-
 codecs/codec_ulaw.c            |   3 +-
 formats/format_g723.c          |   3 +-
 formats/format_gsm.c           |   3 +-
 formats/format_mp3.c           |   3 +-
 formats/format_pcm.c           |   3 +-
 formats/format_vox.c           |   3 +-
 formats/format_wav_gsm.c       |   3 +-
 image.c                        |   2 +-
 include/asterisk/module.h      |   2 +-
 pbx/pbx_config.c               |   8 +-
 pbx/pbx_gtkconsole.c           |   2 +-
 res/res_crypto.c               |   2 +-
 36 files changed, 853 insertions(+), 193 deletions(-)
 create mode 100755 apps/app_getcpeid.c
 create mode 100755 apps/app_meetme.c

diff --git a/apps/app_directory.c b/apps/app_directory.c
index 4ee7cdcbe0..8618030fce 100755
--- a/apps/app_directory.c
+++ b/apps/app_directory.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
@@ -131,7 +132,8 @@ static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *
 	memset(ext, 0, sizeof(ext));
 	ext[0] = digit;
 	res = 0;
-	if (ast_readstring(chan, ext + 1, NUMDIGITS, 3000, 3000, "#") < 0) res = -1;
+	if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
+	printf("Res: %d, ext: %s\n", res, ext);
 	if (!res) {
 		/* Search for all names which start with those digits */
 		v = ast_variable_browse(cfg, context);
@@ -166,7 +168,7 @@ static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *
 			if (v) {
 				/* We have a match -- play a greeting if they have it */
 				snprintf(fn, sizeof(fn), "%s/vm/%s/greet", AST_SPOOL_DIR, v->name);
-				if (ast_fileexists(fn, NULL, chan->language)) {
+				if (ast_fileexists(fn, NULL, chan->language) > 0) {
 					res = ast_streamfile(chan, fn, chan->language);
 					if (!res)
 						res = ast_waitstream(chan, AST_DIGIT_ANY);
diff --git a/apps/app_disa.c b/apps/app_disa.c
index 509dcbe49d..2043f687e4 100755
--- a/apps/app_disa.c
+++ b/apps/app_disa.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
@@ -25,7 +26,9 @@
 #include <pthread.h>
 #include <sys/time.h>
 
-#define	TONE_BLOCK_SIZE 200
+/*
+#define	TONE_BLOCK_SIZE 320
+*/
 
 static char *tdesc = "DISA (Direct Inward System Access) Application";
 
@@ -72,19 +75,17 @@ STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-static float loudness=8192.0;
-
-int firstdigittimeout = 10000; /* 10 seconds first digit timeout */
-int digittimeout = 5000; /* 5 seconds subsequent digit timeout */
+static float loudness=4096.0;
 
-static void make_tone_block(unsigned char *data, float f1, float f2, int *x);
+int firstdigittimeout = 20000; /* 20 seconds first digit timeout */
+int digittimeout = 10000; /* 10 seconds subsequent digit timeout */
 
-static void make_tone_block(unsigned char *data, float f1, float f2, int *x)
+static void make_tone_block(unsigned char *data, float f1, float f2, int len, int *x)
 {
 int	i;
 float	val;
 
-	for(i = 0; i < TONE_BLOCK_SIZE; i++)
+	for(i = 0; i < len; i++)
 	{
 		val = loudness * sin((f1 * 2.0 * M_PI * (*x))/8000.0);
 		val += loudness * sin((f2 * 2.0 * M_PI * (*x)++)/8000.0);
@@ -109,12 +110,11 @@ static int disa_exec(struct ast_channel *chan, void *data)
 	int i,j,k,x;
 	struct localuser *u;
 	char tmp[256],exten[AST_MAX_EXTENSION],acctcode[20];
-	unsigned char tone_block[TONE_BLOCK_SIZE],sil_block[TONE_BLOCK_SIZE];
+	unsigned char tone_block[640],sil_block[640];
 	char *ourcontext;
 	struct ast_frame *f,wf;
-	fd_set	readfds;
-	int waitfor_notime;
-	struct timeval notime = { 0,0 }, lastout, now, lastdigittime;
+	struct timeval lastout, now, lastdigittime;
+	int res;
 	FILE *fp;
 
 	if (ast_set_write_format(chan,AST_FORMAT_ULAW))
@@ -129,7 +129,7 @@ static int disa_exec(struct ast_channel *chan, void *data)
 	}
 	lastout.tv_sec = lastout.tv_usec = 0;
 	  /* make block of silence */
-	memset(sil_block,0x7f,TONE_BLOCK_SIZE);
+	memset(sil_block,0x7f,sizeof(sil_block));
 	if (!data || !strlen((char *)data)) {
 		ast_log(LOG_WARNING, "disa requires an argument (passcode/passcode file)\n");
 		return -1;
@@ -165,33 +165,11 @@ static int disa_exec(struct ast_channel *chan, void *data)
 				((k) ? "extension" : "password"),chan->name);
 			goto reorder;
 		}
-		  /* if first digit or ignore, send dialtone */
-		if ((!i) || (ast_ignore_pattern(ourcontext,exten) && k)) 
-		{
-			gettimeofday(&now,NULL);
-			if (lastout.tv_sec && 
-				(ms_diff(&now,&lastout) < 25)) continue;
-			lastout.tv_sec = now.tv_sec;
-			lastout.tv_usec = now.tv_usec;
-			wf.frametype = AST_FRAME_VOICE;
-			wf.subclass = AST_FORMAT_ULAW;
-			wf.offset = AST_FRIENDLY_OFFSET;
-			wf.mallocd = 0;
-			wf.data = tone_block;
-			wf.datalen = TONE_BLOCK_SIZE;				
-			/* make this tone block */
-			make_tone_block(tone_block,350.0,440.0,&x);
-			wf.timelen = wf.datalen / 8;
-		        if (ast_write(chan, &wf)) 
-			{
-		                ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-				LOCAL_USER_REMOVE(u);
-				return -1;
-			}
+		if ((res = ast_waitfor(chan, -1) < 0)) {
+			ast_log(LOG_DEBUG, "Waitfor returned %d\n", res);
+			continue;
 		}
-		waitfor_notime = notime.tv_usec + notime.tv_sec * 1000;
-		if (!ast_waitfor_nandfds(&chan, 1, &(chan->fds[0]), 1, NULL, NULL,
-			&waitfor_notime)) continue;
+			
 		f = ast_read(chan);
 		if (f == NULL) 
 		{
@@ -204,6 +182,27 @@ static int disa_exec(struct ast_channel *chan, void *data)
 			ast_frfree(f);
 			LOCAL_USER_REMOVE(u);
 			return -1;
+		}
+		if (f->frametype == AST_FRAME_VOICE) {
+			if (!i || (ast_ignore_pattern(ourcontext, exten) && k)) {
+				wf.frametype = AST_FRAME_VOICE;
+				wf.subclass = AST_FORMAT_ULAW;
+				wf.offset = AST_FRIENDLY_OFFSET;
+				wf.mallocd = 0;
+				wf.data = tone_block;
+				wf.datalen = f->datalen;
+				make_tone_block(tone_block, 350, 440, f->datalen, &x);
+				wf.timelen = wf.datalen / 8;
+				ast_frfree(f);
+			    if (ast_write(chan, &wf)) 
+				{
+			        ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
+					LOCAL_USER_REMOVE(u);
+					return -1;
+				}
+			} else
+				ast_frfree(f);
+			continue;
 		}
 		  /* if not DTMF, just do it again */
 		if (f->frametype != AST_FRAME_DTMF) 
@@ -211,8 +210,10 @@ static int disa_exec(struct ast_channel *chan, void *data)
 			ast_frfree(f);
 			continue;
 		}
+
 		j = f->subclass;  /* save digit */
 		ast_frfree(f);
+		
 		gettimeofday(&lastdigittime,NULL);
 		  /* got a DTMF tone */
 		if (i < AST_MAX_EXTENSION) /* if still valid number of digits */
@@ -258,6 +259,7 @@ static int disa_exec(struct ast_channel *chan, void *data)
 
 					}
 					 /* password good, set to dial state */
+						ast_log(LOG_WARNING,"DISA on chan %s password is good\n",chan->name);
 					k = 1;
 					i = 0;  /* re-set buffer pointer */
 					exten[sizeof(acctcode)] = 0;
@@ -285,95 +287,46 @@ static int disa_exec(struct ast_channel *chan, void *data)
 			if (ast_canmatch_extension(chan,ourcontext,exten,1, chan->callerid)) continue;
 		}
 reorder:
+		
 		/* something is invalid, give em reorder forever */
 		x = 0;
+		k = 0;	/* k = 0 means busy tone, k = 1 means silence) */
+		i = 0;	/* Number of samples we've done */
 		for(;;)
 		{
-			for(i = 0; i < 10; i++)
-			{
-				do gettimeofday(&now,NULL);
-				while (lastout.tv_sec && 
-					(ms_diff(&now,&lastout) < 25)) ;
-				lastout.tv_sec = now.tv_sec;
-				lastout.tv_usec = now.tv_usec;
+			if (ast_waitfor(chan, -1) < 0)
+				break;
+			f = ast_read(chan);
+			if (!f)
+				break;
+			if (f->frametype == AST_FRAME_VOICE) {
 				wf.frametype = AST_FRAME_VOICE;
 				wf.subclass = AST_FORMAT_ULAW;
 				wf.offset = AST_FRIENDLY_OFFSET;
 				wf.mallocd = 0;
 				wf.data = tone_block;
-				wf.datalen = TONE_BLOCK_SIZE;
-				/* make this tone block */
-				make_tone_block(tone_block,480.0,620.0,&x);
-				wf.timelen = wf.datalen / 8;
-			        if (ast_write(chan, &wf)) 
-				{
-			                ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-					LOCAL_USER_REMOVE(u);
-					return -1;
-				}
-				FD_ZERO(&readfds);
-				FD_SET(chan->fds[0],&readfds);
-				  /* if no read avail, do send again */
-				if (select(chan->fds[0] + 1,&readfds,NULL,
-					NULL,&notime) < 1) continue;
-				  /* read frame */
-				f = ast_read(chan);
-				if (f == NULL) 
-				{
-					LOCAL_USER_REMOVE(u);
-					return -1;
-				}
-				if ((f->frametype == AST_FRAME_CONTROL) &&
-				    (f->subclass == AST_CONTROL_HANGUP))
-				{
-					ast_frfree(f);
-					LOCAL_USER_REMOVE(u);
-					return -1;
-				}
-				ast_frfree(f);
-			}
-			for(i = 0; i < 10; i++)
-			{
-				do gettimeofday(&now,NULL);
-				while (lastout.tv_sec && 
-					(ms_diff(&now,&lastout) < 25)) ;
-				lastout.tv_sec = now.tv_sec;
-				lastout.tv_usec = now.tv_usec;
-				wf.frametype = AST_FRAME_VOICE;
-				wf.subclass = AST_FORMAT_ULAW;
-				wf.offset = AST_FRIENDLY_OFFSET;
-				wf.mallocd = 0;
-				wf.data = sil_block;
-				wf.datalen = TONE_BLOCK_SIZE;
+				wf.datalen = f->datalen;
 				wf.timelen = wf.datalen / 8;
-			        if (ast_write(chan, &wf)) 
-				{
-			                ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
-					LOCAL_USER_REMOVE(u);
-					return -1;
+				if (k) 
+					memset(tone_block, 0x7f, wf.datalen);
+				else
+					make_tone_block(tone_block,480.0, 620.0,wf.datalen, &x);
+				i += wf.datalen / 8;
+				if (i > 250) {
+					i = 0;
+					k = !k;
 				}
-				FD_ZERO(&readfds);
-				FD_SET(chan->fds[0],&readfds);
-				  /* if no read avail, do send again */
-				if (select(chan->fds[0] + 1,&readfds,NULL,
-					NULL,&notime) < 1) continue;
-				  /* read frame */
-				f = ast_read(chan);
-				if (f == NULL) 
+			    if (ast_write(chan, &wf)) 
 				{
+			        ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
 					LOCAL_USER_REMOVE(u);
 					return -1;
 				}
-				if ((f->frametype == AST_FRAME_CONTROL) &&
-				    (f->subclass == AST_CONTROL_HANGUP))
-				{
-					ast_frfree(f);
-					LOCAL_USER_REMOVE(u);
-					return -1;
-				}
-				ast_frfree(f);
 			}
+			ast_frfree(f);
 		}
+		LOCAL_USER_REMOVE(u);
+		return -1;
 	}
 }
 
diff --git a/apps/app_echo.c b/apps/app_echo.c
index cacfe07d98..7690209ac5 100755
--- a/apps/app_echo.c
+++ b/apps/app_echo.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
@@ -47,7 +48,10 @@ static int echo_exec(struct ast_channel *chan, void *data)
 	ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
 	ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
 	/* Do our thing here */
-	while((f = ast_read(chan))) {
+	while(ast_waitfor(chan, -1) > -1) {
+		f = ast_read(chan);
+		if (!f)
+			break;
 		if (f->frametype == AST_FRAME_VOICE) {
 			if (ast_write(chan, f)) 
 				break;
diff --git a/apps/app_getcpeid.c b/apps/app_getcpeid.c
new file mode 100755
index 0000000000..b4367499ba
--- /dev/null
+++ b/apps/app_getcpeid.c
@@ -0,0 +1,149 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Execute arbitrary system commands
+ * 
+ * Copyright (C) 1999, Mark Spencer
+ *
+ * Mark Spencer <markster@linux-support.net>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+#include <asterisk/lock.h>
+#include <asterisk/file.h>
+#include <asterisk/logger.h>
+#include <asterisk/channel.h>
+#include <asterisk/pbx.h>
+#include <asterisk/module.h>
+#include <asterisk/adsi.h>
+#include <asterisk/options.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+
+
+static char *tdesc = "Get ADSI CPE ID";
+
+static char *app = "GetCPEID";
+
+static char *synopsis = "Get ADSI CPE ID";
+
+static char *descrip =
+"  GetCPEID: Obtains and displays CPE ID and other information in order to\n"
+"properly setup zapata.conf for on-hook operations.  Returns -1 on hanup\n"
+"only.";
+
+STANDARD_LOCAL_USER;
+
+LOCAL_USER_DECL;
+
+static int cpeid_setstatus(struct ast_channel *chan, char *stuff[], int voice)
+{
+	int justify[5] = { ADSI_JUST_CENT, ADSI_JUST_LEFT, ADSI_JUST_LEFT, ADSI_JUST_LEFT };
+	char *tmp[5];
+	int x;
+	for (x=0;x<4;x++)
+		tmp[x] = stuff[x];
+	tmp[4] = NULL;
+	return adsi_print(chan, tmp, justify, voice);
+}
+
+static int cpeid_exec(struct ast_channel *chan, void *idata)
+{
+	int res=0;
+	struct localuser *u;
+	unsigned char cpeid[4];
+	int gotgeometry = 0;
+	int gotcpeid = 0;
+	int width, height, buttons;
+	char data[4][80];
+	char *stuff[4];
+
+	LOCAL_USER_ADD(u);
+	stuff[0] = data[0];
+	stuff[1] = data[1];
+	stuff[2] = data[2];
+	stuff[3] = data[3];
+	memset(data, 0, sizeof(data));
+	strcpy(stuff[0], "** CPE Info **");
+	strcpy(stuff[1], "Identifying CPE...");
+	strcpy(stuff[2], "Please wait...");
+	res = adsi_load_session(chan, NULL, 0, 1);
+	if (res > 0) {
+		cpeid_setstatus(chan, stuff, 0);
+		res = adsi_get_cpeid(chan, cpeid, 0);
+		if (res > 0) {
+			gotcpeid = 1;
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "Got CPEID of '%02x:%02x:%02x:%02x' on '%s'\n", cpeid[0], cpeid[1], cpeid[2], cpeid[3], chan->name);
+		}
+		if (res > -1) {
+			strcpy(stuff[1], "Measuring CPE...");
+			strcpy(stuff[2], "Please wait...");
+			cpeid_setstatus(chan, stuff, 0);
+			res = adsi_get_cpeinfo(chan, &width, &height, &buttons, 0);
+			if (res > -1) {
+				if (option_verbose > 2)
+					ast_verbose(VERBOSE_PREFIX_3 "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name);
+				gotgeometry = 1;
+			}
+		}
+		if (res > -1) {
+			if (gotcpeid)
+				sprintf(stuff[1], "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]);
+			else
+				strcpy(stuff[1], "CPEID Unknown");
+			if (gotgeometry) 
+				sprintf(stuff[2], "Geom: %dx%d, %d buttons", width, height, buttons);
+			else
+				strcpy(stuff[2], "Geometry unknown");
+			strcpy(stuff[3], "Press # to exit");
+			cpeid_setstatus(chan, stuff, 1);
+			for(;;) {
+				res = ast_waitfordigit(chan, 1000);
+				if (res < 0)
+					break;
+				if (res == '#') {
+					res = 0;
+					break;
+				}
+			}
+			adsi_unload_session(chan);
+		}
+	}
+	LOCAL_USER_REMOVE(u);
+	return res;
+}
+
+int unload_module(void)
+{
+	STANDARD_HANGUP_LOCALUSERS;
+	return ast_unregister_application(app);
+}
+
+int load_module(void)
+{
+	return ast_register_application(app, cpeid_exec, synopsis, descrip);
+}
+
+char *description(void)
+{
+	return tdesc;
+}
+
+int usecount(void)
+{
+	int res;
+	STANDARD_USECOUNT(res);
+	return res;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}
diff --git a/apps/app_image.c b/apps/app_image.c
index cc677ff5c3..6a46f03880 100755
--- a/apps/app_image.c
+++ b/apps/app_image.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
diff --git a/apps/app_intercom.c b/apps/app_intercom.c
index 39f645700a..72ae2dc085 100755
--- a/apps/app_intercom.c
+++ b/apps/app_intercom.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/frame.h>
 #include <asterisk/logger.h>
@@ -47,7 +48,7 @@ STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-static pthread_mutex_t sound_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t sound_lock = AST_MUTEX_INITIALIZER;
 static int sound = -1;
 
 static int write_audio(short *data, int len)
diff --git a/apps/app_meetme.c b/apps/app_meetme.c
new file mode 100755
index 0000000000..7a0ad67cf9
--- /dev/null
+++ b/apps/app_meetme.c
@@ -0,0 +1,501 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Meet me conference bridge
+ * 
+ * Copyright (C) 1999, Mark Spencer
+ *
+ * Mark Spencer <markster@linux-support.net>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+#include <asterisk/lock.h>
+#include <asterisk/file.h>
+#include <asterisk/logger.h>
+#include <asterisk/channel.h>
+#include <asterisk/pbx.h>
+#include <asterisk/module.h>
+#include <asterisk/config.h>
+#include <asterisk/app.h>
+#include <asterisk/options.h>
+#include <asterisk/cli.h>
+#include <asterisk/say.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+
+#include <pthread.h>
+#include <linux/zaptel.h>
+
+static char *tdesc = "Simple MeetMe conference bridge";
+
+static char *app = "MeetMe";
+static char *app2 = "MeetMeCount";
+
+static char *synopsis = "Simple MeetMe conference bridge";
+static char *synopsis2 = "MeetMe participant count";
+
+static char *descrip =
+"  MeetMe(confno): Enters the user into a specified MeetMe conference.\n"
+"If the conference number is omitted, the user will be prompted to enter\n"
+"one.  This application always returns -1. A ZAPTEL INTERFACE MUST BE\n"
+"INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
+
+static char *descrip2 =
+"  MeetMe2(confno): Plays back the number of users in the specified MeetMe\n"
+"conference.  Returns 0 on success or -1 on a hangup.  A ZAPTEL INTERFACE\n"
+"MUST BE INSTALLED FOR CONFERENCING FUNCTIONALITY.\n";
+
+STANDARD_LOCAL_USER;
+
+LOCAL_USER_DECL;
+
+static struct conf {
+	char confno[80];		/* Conference */
+	int fd;				/* Announcements fd */
+	int zapconf;			/* Zaptel Conf # */
+	int users;			/* Number of active users */
+	time_t start;			/* Start time (s) */
+	struct conf *next;
+} *confs;
+
+static pthread_mutex_t conflock = AST_MUTEX_INITIALIZER;
+
+#include "enter.h"
+#include "leave.h"
+
+#define ENTER	0
+#define LEAVE	1
+
+#define CONF_SIZE 160
+
+static int careful_write(int fd, unsigned char *data, int len)
+{
+	int res;
+	while(len) {
+		res = write(fd, data, len);
+		if (res < 1) {
+			if (errno != EAGAIN) {
+				ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
+				return -1;
+			} else
+				return 0;
+		}
+		len -= res;
+		data += res;
+	}
+	return 0;
+}
+
+static void conf_play(struct conf *conf, int sound)
+{
+	unsigned char *data;
+	int len;
+	ast_pthread_mutex_lock(&conflock);
+	switch(sound) {
+	case ENTER:
+		data = enter;
+		len = sizeof(enter);
+		break;
+	case LEAVE:
+		data = leave;
+		len = sizeof(leave);
+		break;
+	default:
+		data = NULL;
+		len = 0;
+	}
+	if (data) 
+		careful_write(conf->fd, data, len);
+	pthread_mutex_unlock(&conflock);
+}
+
+static struct conf *build_conf(char *confno, int make)
+{
+	struct conf *cnf;
+	struct zt_confinfo ztc;
+	ast_pthread_mutex_lock(&conflock);
+	cnf = confs;
+	while(cnf) {
+		if (!strcmp(confno, cnf->confno)) 
+			break;
+		cnf = cnf->next;
+	}
+	if (!cnf && make) {
+		cnf = malloc(sizeof(struct conf));
+		if (cnf) {
+			/* Make a new one */
+			memset(cnf, 0, sizeof(struct conf));
+			strncpy(cnf->confno, confno, sizeof(cnf->confno) - 1);
+			cnf->fd = open("/dev/zap/pseudo", O_RDWR);
+			if (cnf->fd < 0) {
+				ast_log(LOG_WARNING, "Unable to open pseudo channel\n");
+				free(cnf);
+				cnf = NULL;
+				goto cnfout;
+			}
+			memset(&ztc, 0, sizeof(ztc));
+			/* Setup a new zap conference */
+			ztc.chan = 0;	
+			ztc.confno = -1;
+			ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
+			if (ioctl(cnf->fd, ZT_SETCONF, &ztc)) {
+				ast_log(LOG_WARNING, "Error setting conference\n");
+				close(cnf->fd);
+				free(cnf);
+				cnf = NULL;
+				goto cnfout;
+			}
+			cnf->start = time(NULL);
+			cnf->zapconf = ztc.confno;
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "Crated ZapTel conference %d for conference '%s'\n", cnf->zapconf, cnf->confno);
+			cnf->next = confs;
+			confs = cnf;
+		} else	
+			ast_log(LOG_WARNING, "Out of memory\n");
+	}
+cnfout:
+	if (cnf && make) 
+		cnf->users++;
+	ast_pthread_mutex_unlock(&conflock);
+	return cnf;
+}
+
+static int confs_show(int fd, int argc, char **argv)
+{
+	struct conf *conf;
+	int hr, min, sec;
+	time_t now;
+
+	now = time(NULL);
+	if (argc != 2)
+		return RESULT_SHOWUSAGE;
+	conf = confs;
+	if (!conf) {	
+		ast_cli(fd, "No active conferences.\n");
+		return RESULT_SUCCESS;
+	}
+	ast_cli(fd, "Conf Num    Parties          Activity\n");
+	while(conf) {
+		hr = (now - conf->start) / 3600;
+		min = ((now - conf->start) % 3600) / 60;
+		sec = (now - conf->start) % 60;
+
+		ast_cli(fd, "%-12.12s   %4.4d          %02d:%02d:%02d\n", 
+			conf->confno, conf->users, hr, min, sec);
+		conf = conf->next;
+	}
+	return RESULT_SUCCESS;
+}
+
+static char show_confs_usage[] = 
+"Usage: show conferences\n"
+"       Provides summary information on conferences with active\n"
+"       participation.\n";
+
+static struct ast_cli_entry cli_show_confs = {
+	{ "show", "conferences", NULL }, confs_show, 
+	"Show status of conferences", show_confs_usage, NULL };
+
+static void conf_run(struct ast_channel *chan, struct conf *conf)
+{
+	struct conf *prev=NULL, *cur;
+	int fd;
+	struct zt_confinfo ztc;
+	struct ast_frame *f;
+	struct ast_channel *c;
+	struct ast_frame fr;
+	int outfd;
+	int ms;
+	int nfds;
+	int res;
+	int flags;
+	int retryzap=0;
+
+	ZT_BUFFERINFO bi;
+	char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
+	char *buf = __buf + AST_FRIENDLY_OFFSET;
+
+	/* Set it into U-law mode (write) */
+	if (ast_set_write_format(chan, AST_FORMAT_ULAW) < 0) {
+		ast_log(LOG_WARNING, "Unable to set '%s' to write ulaw mode\n", chan->name);
+		goto outrun;
+	}
+
+	/* Set it into U-law mode (read) */
+	if (ast_set_read_format(chan, AST_FORMAT_ULAW) < 0) {
+		ast_log(LOG_WARNING, "Unable to set '%s' to read ulaw mode\n", chan->name);
+		goto outrun;
+	}
+zapretry:
+
+	if (retryzap || strcasecmp(chan->type, "Zap")) {
+		fd = open("/dev/zap/pseudo", O_RDWR);
+		if (fd < 0) {
+			ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
+			goto outrun;
+		}
+		/* Make non-blocking */
+		flags = fcntl(fd, F_GETFL);
+		if (flags < 0) {
+			ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
+			close(fd);
+			goto outrun;
+		}
+		if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
+			ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
+			close(fd);
+			goto outrun;
+		}
+		/* Setup buffering information */
+		memset(&bi, 0, sizeof(bi));
+		bi.bufsize = CONF_SIZE;
+		bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
+		bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
+		bi.numbufs = 4;
+		if (ioctl(fd, ZT_SET_BUFINFO, &bi)) {
+			ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
+			close(fd);
+			goto outrun;
+		}
+		nfds = 1;
+	} else {
+		/* XXX Make sure we're not running on a pseudo channel XXX */
+		fd = chan->fds[0];
+		nfds = 0;
+	}
+	memset(&ztc, 0, sizeof(ztc));
+	/* Check to see if we're in a conference... */
+	ztc.chan = 0;	
+	if (ioctl(fd, ZT_GETCONF, &ztc)) {
+		ast_log(LOG_WARNING, "Error getting conference\n");
+		close(fd);
+		goto outrun;
+	}
+	if (ztc.confmode) {
+		/* Whoa, already in a conference...  Retry... */
+		if (!retryzap) {
+			ast_log(LOG_DEBUG, "Zap channel is in a conference already, retrying with pseudo\n");
+			retryzap = 1;
+			goto zapretry;
+		}
+	}
+	memset(&ztc, 0, sizeof(ztc));
+	/* Add us to the conference */
+	ztc.chan = 0;	
+	ztc.confno = conf->zapconf;
+	ztc.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
+	if (ioctl(fd, ZT_SETCONF, &ztc)) {
+		ast_log(LOG_WARNING, "Error setting conference\n");
+		close(fd);
+		goto outrun;
+	}
+	ast_log(LOG_DEBUG, "Placed channel %s in ZAP conf %d\n", chan->name, conf->zapconf);
+	/* Run the conference enter tone... */
+	conf_play(conf, ENTER);
+
+	for(;;) {
+		outfd = -1;
+		ms = -1;
+		c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
+		if (c) {
+			f = ast_read(c);
+			if (!f) 
+				break;
+			if (fd != chan->fds[0]) {
+				if (f->frametype == AST_FRAME_VOICE) {
+					if (f->subclass == AST_FORMAT_ULAW) {
+						/* Carefully write */
+						careful_write(fd, f->data, f->datalen);
+					} else
+						ast_log(LOG_WARNING, "Huh?  Got a non-ulaw (%d) frame in the conference\n", f->subclass);
+				}
+			}
+			ast_frfree(f);
+		} else if (outfd > -1) {
+			res = read(outfd, buf, CONF_SIZE);
+			if (res > 0) {
+				memset(&fr, 0, sizeof(fr));
+				fr.frametype = AST_FRAME_VOICE;
+				fr.subclass = AST_FORMAT_ULAW;
+				fr.datalen = res;
+				fr.timelen = res / 8;
+				fr.data = buf;
+				fr.offset = AST_FRIENDLY_OFFSET;
+				if (ast_write(chan, &fr) < 0) {
+					ast_log(LOG_WARNING, "Unable to write frame to channel: %s\n", strerror(errno));
+					/* break; */
+				}
+			} else 
+				ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
+		}
+	}
+	if (fd != chan->fds[0])
+		close(fd);
+
+	conf_play(conf, LEAVE);
+
+outrun:
+
+	ast_pthread_mutex_lock(&conflock);
+	/* Clean up */
+	conf->users--;
+	if (!conf->users) {
+		/* No more users -- close this one out */
+		cur = confs;
+		while(cur) {
+			if (cur == conf) {
+				if (prev)
+					prev->next = conf->next;
+				else
+					confs = conf->next;
+				break;
+			}
+			prev = cur;
+			cur = cur->next;
+		}
+		if (!cur) 
+			ast_log(LOG_WARNING, "Conference not found\n");
+		close(conf->fd);
+		free(conf);
+	}
+	pthread_mutex_unlock(&conflock);
+}
+
+static struct conf *find_conf(char *confno, int make)
+{
+	struct ast_config *cfg;
+	struct ast_variable *var;
+	struct conf *cnf = NULL;
+	cfg = ast_load("meetme.conf");
+	if (!cfg) {
+		ast_log(LOG_WARNING, "No meetme.conf file :(\n");
+		return NULL;
+	}
+	var = ast_variable_browse(cfg, "rooms");
+	while(var) {
+		if (!strcasecmp(var->name, "conf") &&
+		    !strcasecmp(var->value, confno)) {
+			/* Bingo it's a valid conference */
+			cnf = build_conf(confno, make);
+			break;
+		}
+		var = var->next;
+	}
+	if (!var) {
+		ast_log(LOG_DEBUG, "%s isn't a valid conference\n", confno);
+	}
+	ast_destroy(cfg);
+	return cnf;
+}
+
+static int count_exec(struct ast_channel *chan, void *data)
+{
+	struct localuser *u;
+	int res = 0;
+	struct conf *conf;
+	int cnt;
+	if (!data || !strlen(data)) {
+		ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
+		return -1;
+	}
+	LOCAL_USER_ADD(u);
+	conf = find_conf(data, 0);
+	if (conf)
+		cnt = conf->users;
+	else
+		cnt = 0;
+	if (chan->state != AST_STATE_UP)
+		ast_answer(chan);
+	res = ast_say_number(chan, cnt, "", chan->language);
+	LOCAL_USER_REMOVE(u);
+	return res;
+}
+
+static int conf_exec(struct ast_channel *chan, void *data)
+{
+	int res=-1;
+	struct localuser *u;
+	char confno[80] = "";
+	int allowretry = 0;
+	int retrycnt = 0;
+	struct conf *cnf;
+
+	if (!data || !strlen(data)) {
+		allowretry = 1;
+		data = "";
+	}
+	LOCAL_USER_ADD(u);
+	if (chan->state != AST_STATE_UP)
+		ast_answer(chan);
+retry:
+	/* Parse out the stuff */
+	strncpy(confno, data, sizeof(confno) - 1);
+
+	while(!strlen(confno) && (++retrycnt < 4)) {
+		/* Prompt user for conference number */
+		res = ast_app_getdata(chan, "conf-getconfno",confno, sizeof(confno) - 1, 0);
+		if (res <0) goto out;
+	}
+	if (strlen(confno)) {
+		/* Check the validity of the conference */
+		cnf = find_conf(confno, 1);
+		if (!cnf) {
+			res = ast_streamfile(chan, "conf-invalid", chan->language);
+			if (res < 0)
+				goto out;
+			if (!res)
+				res = ast_waitstream(chan, "");
+			res = -1;
+			if (allowretry) {
+				strcpy(confno, "");
+				goto retry;
+			}
+		} else {
+			/* Run the conference */
+			conf_run(chan, cnf);
+			res = -1;
+		}
+	}
+out:
+	/* Do the conference */
+	LOCAL_USER_REMOVE(u);
+	return res;
+}
+
+int unload_module(void)
+{
+	STANDARD_HANGUP_LOCALUSERS;
+	ast_cli_unregister(&cli_show_confs);
+	ast_unregister_application(app2);
+	return ast_unregister_application(app);
+}
+
+int load_module(void)
+{
+	ast_cli_register(&cli_show_confs);
+	ast_register_application(app2, count_exec, synopsis2, descrip2);
+	return ast_register_application(app, conf_exec, synopsis, descrip);
+}
+
+char *description(void)
+{
+	return tdesc;
+}
+
+int usecount(void)
+{
+	int res;
+	STANDARD_USECOUNT(res);
+	return res;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}
diff --git a/apps/app_mp3.c b/apps/app_mp3.c
index 30d6bdcccf..5ef5f9bb0d 100755
--- a/apps/app_mp3.c
+++ b/apps/app_mp3.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
diff --git a/apps/app_playback.c b/apps/app_playback.c
index 11356e4b3b..5ca2626608 100755
--- a/apps/app_playback.c
+++ b/apps/app_playback.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
diff --git a/apps/app_qcall.c b/apps/app_qcall.c
index 5e153e19c0..1a7887bb3d 100755
--- a/apps/app_qcall.c
+++ b/apps/app_qcall.c
@@ -16,13 +16,14 @@
  * The queue is a directory containing files with the call request information
  * as a single line of text as follows:
  * 
- * Dialstring Caller-ID Extension Maxsecs Identifier [Required-response]
+ * Dialstring Caller-ID Extension Maxsecs [Identifier] [Required-response]
  *
  *  Dialstring -- A Dial String (The number to be called) in the
  *  format Technology/Number, such IAX/mysys/1234 or Zap/g1/1234
  * 
  *  Caller-ID -- A Standard nomalized representation of the Caller-ID of
- *  the number being dialed (generally 10 digits in the US).
+ *  the number being dialed (generally 10 digits in the US). Leave as
+ *  "asreceived" to use the default Caller*ID
  *
  *  Extension -- The Extension (optionally Extension@context) that the
  *  user should be "transferred" to after acceptance of the call.
@@ -36,7 +37,7 @@
  *  (generally a "thank you" recording), is the specified string with "-ok" 
  *  added to the end. So, if you specify "foo" as the identifier, your first
  *  prompt file that will be played will be "foo" and the second one will be
- *  "foo-ok".
+ *  "foo-ok".  If omitted no prompt is given
  *
  *  Required-Response (Optional) -- Specify a digit string to be used as the
  *  acceptance "code" if you desire it to be something other then "1". This
@@ -49,6 +50,7 @@
  *
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
@@ -78,7 +80,7 @@ STANDARD_LOCAL_USER;
 LOCAL_USER_DECL;
 
 #define	OLDESTOK	14400		/* not any more then this number of secs old */
-#define	INITIALONE	20		/* initial wait before the first one in secs */
+#define	INITIALONE	1		/* initial wait before the first one in secs */
 #define	NEXTONE		600		/* wait before trying it again in secs */
 #define	MAXWAITFORANSWER 45000		/* max call time before answer */
 /* define either one or both of these two if your application requires it */
@@ -132,7 +134,8 @@ pthread_attr_t attr;
 			  /* if not yet .... */
 			if (mystat.st_atime == mystat.st_ctime)
 			   {  /* first time */
-				if ((mystat.st_atime + INITIALONE) > t) continue;
+				if ((mystat.st_atime + INITIALONE) > t) 
+					continue;
 			   }
 			else
 			   { /* already looked at once */
@@ -195,8 +198,9 @@ time_t	t;
 		pthread_exit(NULL);
 	   }
 	strcpy(reqinp,"1");  /* default required input for acknowledgement */
+	strcpy(ident, "");	/* default no ident */
 	if (fscanf(fp,"%s %s %s %d %s %s",dialstr,clid,
-		extstr,&maxsecs,ident,reqinp) < 5)
+		extstr,&maxsecs,ident,reqinp) < 4)
 	   {
 		fprintf(stderr,"qcall_do:file line invalid in file %s:\n",fname);
 		pthread_exit(NULL);
@@ -216,10 +220,12 @@ time_t	t;
 	   {
 		ast_set_read_format(channel,AST_FORMAT_SLINEAR);
 		ast_set_write_format(channel,AST_FORMAT_SLINEAR);
-		channel->callerid = NULL;
-		channel->ani = NULL;
 #ifdef	OURCLID
+		if (channel->callerid)
+			free(channel->callerid);
 		channel->callerid = strdup(OURCLID);
+		if (channel->ani)
+			free(channel->ani);
 		channel->ani = strdup(OURCLID);
 #endif		
 		channel->whentohangup = 0;
@@ -235,10 +241,12 @@ time_t	t;
 		fprintf(stderr,"qcall_do:Sorry unable to obtain channel\n");
 		pthread_exit(NULL);
 	   }
-	if (channel->callerid) free(channel->callerid);
-	channel->callerid = NULL;
-	if (channel->ani) free(channel->ani);
-	channel->ani = NULL;
+	if (strcasecmp(clid, "asreceived")) {
+		if (channel->callerid) free(channel->callerid);
+		channel->callerid = NULL;
+		if (channel->ani) free(channel->ani);
+		channel->ani = NULL;
+	}
 	if (channel->state == AST_STATE_UP)
 	if (debug) printf("@@@@ Autodial:Line is Up\n");
 	if (option_verbose > 2)
@@ -251,7 +259,6 @@ time_t	t;
 		if (!f)
 		   {
 			if (debug) printf("@@@@ qcall_do:Hung Up\n");
-			ast_frfree(f);
 			unlink(fname);
 			break;
 		   }
@@ -274,22 +281,24 @@ time_t	t;
 						ast_verbose(VERBOSE_PREFIX_3 "Qcall got answer on %s\n",
 							channel->name);
 					usleep(1500000);
-					ast_streamfile(channel,ident,0);
-					if (ast_readstring(channel,buf,strlen(reqinp),10000,5000,"#"))
-					   {
+					if (strlen(ident)) {
+						ast_streamfile(channel,ident,0);
+						if (ast_readstring(channel,buf,strlen(reqinp),10000,5000,"#"))
+						{
+							ast_stopstream(channel);
+							if (debug) printf("@@@@ qcall_do: timeout or hangup in dtmf read\n");
+							ast_frfree(f);
+							break;
+						}
 						ast_stopstream(channel);
-						if (debug) printf("@@@@ qcall_do: timeout or hangup in dtmf read\n");
+						if (strcmp(buf,reqinp)) /* if not match */
+						{
+							if (debug) printf("@@@@ qcall_do: response (%s) does not match required (%s)\n",buf,reqinp);
+							ast_frfree(f);
+							break;
+						}
 						ast_frfree(f);
-						break;
-					   }
-					ast_stopstream(channel);
-					if (strcmp(buf,reqinp)) /* if not match */
-					   {
-						if (debug) printf("@@@@ qcall_do: response (%s) does not match required (%s)\n",buf,reqinp);
-						ast_frfree(f);
-						break;
-					   }
-					ast_frfree(f);
+					}
 					/* okay, now we go for it */
 					context = strchr(extstr,'@');
 					if (!context) context = "default";
@@ -297,15 +306,19 @@ time_t	t;
 					if (option_verbose > 2)
 						ast_verbose(VERBOSE_PREFIX_3 "Qcall got accept, now putting through to %s@%s on %s\n",
 							extstr,context,channel->name);
-					strcat(ident,"-ok");
-					  /* if file existant, play it */
-					if (!ast_streamfile(channel,ident,0))
-					   {
-						ast_waitstream(channel,"");
-						ast_stopstream(channel);
-					   }
-					channel->callerid = strdup(clid);
-					channel->ani = strdup(clid);
+					if (strlen(ident)) {
+						strcat(ident,"-ok");
+						/* if file existant, play it */
+						if (!ast_streamfile(channel,ident,0))
+						{
+							ast_waitstream(channel,"");
+							ast_stopstream(channel);
+						}
+					}
+					if (strcasecmp(clid, "asreceived")) {
+						channel->callerid = strdup(clid);
+						channel->ani = strdup(clid);
+					}
 					channel->language[0] = 0;
 					channel->dnid = strdup(extstr);
 #ifdef	AMAFLAGS
@@ -324,6 +337,7 @@ time_t	t;
 					strcpy(channel->exten,extstr);
 					strcpy(channel->context,context);
 					channel->priority = 1;
+					printf("Caller ID is %s\n", channel->callerid);
 					ast_pbx_run(channel);
 					pthread_exit(NULL);
 				}
diff --git a/apps/app_url.c b/apps/app_url.c
index 7e6036140f..53a589481a 100755
--- a/apps/app_url.c
+++ b/apps/app_url.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
diff --git a/apps/app_zapras.c b/apps/app_zapras.c
index 53f306cdc3..82614f120f 100755
--- a/apps/app_zapras.c
+++ b/apps/app_zapras.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
@@ -125,7 +126,7 @@ static void run_ras(struct ast_channel *chan, char *args)
 			if (!res) {
 				/* Check for hangup */
 				if (chan->softhangup && !signalled) {
-					ast_log(LOG_DEBUG, "Channel hungup.  Signalling RAS at %d to die...\n", pid);
+					ast_log(LOG_DEBUG, "Channel '%s' hungup.  Signalling RAS at %d to die...\n", chan->name, pid);
 					kill(pid, SIGTERM);
 					signalled=1;
 				}
@@ -147,7 +148,7 @@ static void run_ras(struct ast_channel *chan, char *args)
 				}
 			}
 			/* Throw back into audio mode */
-			x = 0;
+			x = 1;
 			ioctl(chan->fds[0], ZT_AUDIOMODE, &x);
 
 			/* Double check buffering too */
diff --git a/cdr.c b/cdr.c
index 91b76f0e4a..ee654173c6 100755
--- a/cdr.c
+++ b/cdr.c
@@ -14,6 +14,7 @@
  *
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/cdr.h>
 #include <asterisk/logger.h>
@@ -25,7 +26,7 @@
 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
 char ast_default_accountcode[20] = "";
 
-static pthread_mutex_t cdrlock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t cdrlock = AST_MUTEX_INITIALIZER;
 
 static struct ast_cdr_beitem {
 	char name[20];
diff --git a/channels/chan_modem_aopen.c b/channels/chan_modem_aopen.c
index 80a1402f91..b832f80923 100755
--- a/channels/chan_modem_aopen.c
+++ b/channels/chan_modem_aopen.c
@@ -16,7 +16,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <pthread.h>
+#include <asterisk/lock.h>
 #include <asterisk/vmodem.h>
 #include <asterisk/module.h>
 #include <asterisk/frame.h>
@@ -34,7 +34,7 @@ static char *breakcmd = "\0x10\0x03";
 static char *desc = "A/Open (Rockwell Chipset) ITU-2 VoiceModem Driver";
 
 int usecnt;
-pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 static char *aopen_idents[] = {
 	/* Identify A/Open Modem */
diff --git a/channels/chan_modem_bestdata.c b/channels/chan_modem_bestdata.c
index a360e32fd5..b9f56d3b19 100755
--- a/channels/chan_modem_bestdata.c
+++ b/channels/chan_modem_bestdata.c
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <pthread.h>
+#include <asterisk/lock.h>
 #include <asterisk/vmodem.h>
 #include <asterisk/module.h>
 #include <asterisk/frame.h>
@@ -38,7 +38,7 @@ static char *breakcmd = "\020!";
 static char *desc = "BestData (Conexant V.90 Chipset) VoiceModem Driver";
 
 int usecnt;
-pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 static char *bestdata_idents[] = {
 	/* Identify BestData Modem */
diff --git a/channels/chan_modem_i4l.c b/channels/chan_modem_i4l.c
index b02757f6f2..609fefffbd 100755
--- a/channels/chan_modem_i4l.c
+++ b/channels/chan_modem_i4l.c
@@ -17,7 +17,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <unistd.h>
-#include <pthread.h>
+#include <asterisk/lock.h>
 #include <asterisk/vmodem.h>
 #include <asterisk/module.h>
 #include <asterisk/frame.h>
@@ -33,7 +33,7 @@ static char *breakcmd = "\0x10\0x14\0x10\0x3";
 static char *desc = "ISDN4Linux Emulated Modem Driver";
 
 int usecnt;
-pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 static char *i4l_idents[] = {
 	/* Identify ISDN4Linux Driver */
@@ -386,7 +386,7 @@ static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
 
 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
 {
-#define MAX_WRITE_SIZE 512
+#define MAX_WRITE_SIZE 1024
 	unsigned char result[MAX_WRITE_SIZE << 1];
 	unsigned char b;
 	int bpos=0, x;
@@ -416,8 +416,10 @@ static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
 	res = write(p->fd, result, bpos);
 #endif
 	if (res < 1) {
-		ast_log(LOG_WARNING, "Failed to write buffer\n");
-		return -1;
+		if (errno != EAGAIN) {
+			ast_log(LOG_WARNING, "Failed to write buffer\n");
+			return -1;
+		}
 	}
 #if 0
 	printf("Result of write is %d\n", res);
diff --git a/cli.c b/cli.c
index b747997c98..32bc459aef 100755
--- a/cli.c
+++ b/cli.c
@@ -43,7 +43,7 @@ void ast_cli(int fd, char *fmt, ...)
 	write(fd, stuff, strlen(stuff));
 }
 
-pthread_mutex_t clilock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t clilock = AST_MUTEX_INITIALIZER;
 
 
 struct ast_cli_entry *helpers = NULL;
@@ -154,7 +154,7 @@ static int handle_unload(int fd, int argc, char *argv[])
 #define MODLIST_FORMAT  "%-20s %-40.40s %-10d\n"
 #define MODLIST_FORMAT2 "%-20s %-40.40s %-10s\n"
 
-static pthread_mutex_t climodentrylock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t climodentrylock = AST_MUTEX_INITIALIZER;
 static int climodentryfd = -1;
 
 static int modlist_modentry(char *module, char *description, int usecnt)
@@ -441,13 +441,17 @@ int ast_cli_unregister(struct ast_cli_entry *e)
 	cur = helpers;
 	while(cur) {
 		if (e == cur) {
-			/* Rewrite */
-			if (l)
-				l->next = e->next;
-			else
-				helpers = e->next;
-			e->next = NULL;
-			break;
+			if (e->inuse) {
+				ast_log(LOG_WARNING, "Can't remove command that is in use\n");
+			} else {
+				/* Rewrite */
+				if (l)
+					l->next = e->next;
+				else
+					helpers = e->next;
+				e->next = NULL;
+				break;
+			}
 		}
 		l = cur;
 		cur = cur->next;
@@ -719,6 +723,9 @@ int ast_cli_command(int fd, char *s)
 		if (x > 0) {
 			ast_pthread_mutex_lock(&clilock);
 			e = find_cli(argv, 0);
+			if (e)
+				e->inuse++;
+			ast_pthread_mutex_unlock(&clilock);
 			if (e) {
 				switch(e->handler(fd, x, argv)) {
 				case RESULT_SHOWUSAGE:
@@ -728,7 +735,11 @@ int ast_cli_command(int fd, char *s)
 				}
 			} else 
 				ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
-			ast_pthread_mutex_unlock(&clilock);
+			if (e) {
+				ast_pthread_mutex_lock(&clilock);
+				e->inuse--;
+				ast_pthread_mutex_unlock(&clilock);
+			}
 		}
 		free(dup);
 	} else {
diff --git a/codecs/codec_a_mu.c b/codecs/codec_a_mu.c
index 96fa2acfcb..c7d52b447f 100755
--- a/codecs/codec_a_mu.c
+++ b/codecs/codec_a_mu.c
@@ -10,6 +10,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/logger.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
@@ -26,7 +27,7 @@
 
 #define BUFFER_SIZE   8096	/* size for the translation buffers */
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt = 0;
 
 static char *tdesc = "A-law and Mulaw direct Coder/Decoder";
diff --git a/codecs/codec_adpcm.c b/codecs/codec_adpcm.c
index c2aa090d5e..c07aba5381 100755
--- a/codecs/codec_adpcm.c
+++ b/codecs/codec_adpcm.c
@@ -13,6 +13,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/logger.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
@@ -27,7 +28,7 @@
 
 #define BUFFER_SIZE   8096	/* size for the translation buffers */
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt = 0;
 
 static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
diff --git a/codecs/codec_alaw.c b/codecs/codec_alaw.c
index 9089fccbef..8330923650 100755
--- a/codecs/codec_alaw.c
+++ b/codecs/codec_alaw.c
@@ -10,6 +10,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/logger.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
@@ -25,7 +26,7 @@
 
 #define BUFFER_SIZE   8096	/* size for the translation buffers */
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt = 0;
 
 static char *tdesc = "A-law Coder/Decoder";
diff --git a/codecs/codec_g723_1.c b/codecs/codec_g723_1.c
index 848e0c8580..1d655551dd 100755
--- a/codecs/codec_g723_1.c
+++ b/codecs/codec_g723_1.c
@@ -54,7 +54,7 @@
 #include "slin_g723_ex.h"
 #include "g723_slin_ex.h"
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt=0;
 
 #ifdef ANNEX_B
diff --git a/codecs/codec_gsm.c b/codecs/codec_gsm.c
index ebad1225f3..c84ee28978 100755
--- a/codecs/codec_gsm.c
+++ b/codecs/codec_gsm.c
@@ -19,6 +19,7 @@
 #define TYPE_LOW	 0x1
 #define TYPE_MASK	 0x3
 
+#include <asterisk/lock.h>
 #include <asterisk/translate.h>
 #include <asterisk/module.h>
 #include <asterisk/logger.h>
@@ -37,7 +38,7 @@
 #include "slin_gsm_ex.h"
 #include "gsm_slin_ex.h"
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt=0;
 
 static char *tdesc = "GSM/PCM16 (signed linear) Codec Translator";
diff --git a/codecs/codec_lpc10.c b/codecs/codec_lpc10.c
index d8baa24586..a2f6bd9111 100755
--- a/codecs/codec_lpc10.c
+++ b/codecs/codec_lpc10.c
@@ -17,6 +17,7 @@
  */
 
 
+#include <asterisk/lock.h>
 #include <asterisk/translate.h>
 #include <asterisk/module.h>
 #include <asterisk/logger.h>
@@ -42,7 +43,7 @@
 
 #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt=0;
 
 static char *tdesc = "LPC10 2.4kbps (signed linear) Voice Coder";
diff --git a/codecs/codec_mp3_d.c b/codecs/codec_mp3_d.c
index d271dfe512..f417453efe 100755
--- a/codecs/codec_mp3_d.c
+++ b/codecs/codec_mp3_d.c
@@ -14,6 +14,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/translate.h>
 #include <asterisk/module.h>
 #include <asterisk/logger.h>
@@ -40,7 +41,7 @@
 #define MAX_FRAME_SIZE 1441
 #define MAX_OUTPUT_LEN 2304
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt=0;
 
 static char *tdesc = "MP3/PCM16 (signed linear) Translator (Decoder only)";
diff --git a/codecs/codec_ulaw.c b/codecs/codec_ulaw.c
index 4042ad0602..175da76f59 100755
--- a/codecs/codec_ulaw.c
+++ b/codecs/codec_ulaw.c
@@ -10,6 +10,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/logger.h>
 #include <asterisk/module.h>
 #include <asterisk/translate.h>
@@ -25,7 +26,7 @@
 
 #define BUFFER_SIZE   8096	/* size for the translation buffers */
 
-static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
 static int localusecnt = 0;
 
 static char *tdesc = "Mu-law Coder/Decoder";
diff --git a/formats/format_g723.c b/formats/format_g723.c
index e93ed04149..c54a640b3c 100755
--- a/formats/format_g723.c
+++ b/formats/format_g723.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -44,7 +45,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t g723_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t g723_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "g723sf";
diff --git a/formats/format_gsm.c b/formats/format_gsm.c
index 327d5b5c77..1ac9869341 100755
--- a/formats/format_gsm.c
+++ b/formats/format_gsm.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -49,7 +50,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t gsm_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t gsm_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "gsm";
diff --git a/formats/format_mp3.c b/formats/format_mp3.c
index 83c8e1c46c..e1ee086519 100755
--- a/formats/format_mp3.c
+++ b/formats/format_mp3.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -46,7 +47,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t mp3_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t mp3_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "mp3";
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index dfd4ec3668..befa05698a 100755
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -47,7 +48,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t pcm_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t pcm_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "pcm";
diff --git a/formats/format_vox.c b/formats/format_vox.c
index 76c002dfc1..67934219a3 100755
--- a/formats/format_vox.c
+++ b/formats/format_vox.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -49,7 +50,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t vox_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t vox_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "vox";
diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c
index 679b714a95..b1a8020d93 100755
--- a/formats/format_wav_gsm.c
+++ b/formats/format_wav_gsm.c
@@ -11,6 +11,7 @@
  * the GNU General Public License
  */
  
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/file.h>
 #include <asterisk/logger.h>
@@ -53,7 +54,7 @@ struct ast_filestream {
 
 
 static struct ast_filestream *glist = NULL;
-static pthread_mutex_t wav_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t wav_lock = AST_MUTEX_INITIALIZER;
 static int glistcnt = 0;
 
 static char *name = "wav49";
diff --git a/image.c b/image.c
index f51f751c56..785e8ddfd0 100755
--- a/image.c
+++ b/image.c
@@ -32,7 +32,7 @@
 #include "asterisk.h"
 
 static struct ast_imager *list;
-static pthread_mutex_t listlock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t listlock = AST_MUTEX_INITIALIZER;
 
 int ast_image_register(struct ast_imager *img)
 {
diff --git a/include/asterisk/module.h b/include/asterisk/module.h
index 214e38f026..b270c111cd 100755
--- a/include/asterisk/module.h
+++ b/include/asterisk/module.h
@@ -149,7 +149,7 @@ void ast_module_reload(void);
 								struct localuser *next; \
 							}
 
-#define LOCAL_USER_DECL static pthread_mutex_t localuser_lock = PTHREAD_MUTEX_INITIALIZER; \
+#define LOCAL_USER_DECL static pthread_mutex_t localuser_lock = AST_MUTEX_INITIALIZER; \
 						static struct localuser *localusers = NULL; \
 						static int localusecnt = 0;
 
diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c
index fc5bc25868..38c6e1f39c 100755
--- a/pbx/pbx_config.c
+++ b/pbx/pbx_config.c
@@ -31,7 +31,7 @@ static char *registrar = "pbx_config";
 static int static_config = 0;
 static int write_protect_config = 1;
 
-static pthread_mutex_t save_dialplan_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t save_dialplan_lock = AST_MUTEX_INITIALIZER;
 
 /*
  * Help for commands provided by this module ...
@@ -1496,8 +1496,12 @@ static int pbx_load_module(void)
 							appl="";
 						data = strtok(NULL, ",");
 
+						cidmatch = strchr(ext, '/');
+						if (cidmatch) {
+							*cidmatch = '\0';
+							cidmatch++;
+						}
 						strtok(ext, "/");
-						cidmatch = strtok(NULL, "/");
 
 						if (!data)
 							data="";
diff --git a/pbx/pbx_gtkconsole.c b/pbx/pbx_gtkconsole.c
index 5d3f057737..d93ef6fa98 100755
--- a/pbx/pbx_gtkconsole.c
+++ b/pbx/pbx_gtkconsole.c
@@ -40,7 +40,7 @@
 /* For where to put dynamic tables */
 #include "../asterisk.h"
 
-static pthread_mutex_t verb_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t verb_lock = AST_MUTEX_INITIALIZER;
 
 static pthread_t console_thread;
 
diff --git a/res/res_crypto.c b/res/res_crypto.c
index 8aa4e9d6ea..6d9b4353ec 100755
--- a/res/res_crypto.c
+++ b/res/res_crypto.c
@@ -55,7 +55,7 @@
 static char base64[64];
 static char b2a[256];
 
-static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t keylock = AST_MUTEX_INITIALIZER;
 
 #define KEY_NEEDS_PASSCODE (1 << 16)
 
-- 
GitLab