diff --git a/Makefile b/Makefile
index 0dc248632aca21fcdbe03a15f5abd7f29e0acb2b..21d916b03c06efb407d2403e982daa197f85c4a6 100755
--- a/Makefile
+++ b/Makefile
@@ -28,8 +28,8 @@ PROC=$(shell uname -m)
 
 DEBUG=-g #-pg
 INCLUDE=-Iinclude -I../include
-CFLAGS=-pipe  -Wall -Wmissing-prototypes -Wmissing-declarations -O6 $(DEBUG) $(INCLUDE) -D_REENTRANT -D_GNU_SOURCE
-#CFLAGS+=-Werror
+CFLAGS=-pipe  -Wall -Wmissing-prototypes -Wmissing-declarations $(DEBUG) $(INCLUDE) -D_REENTRANT -D_GNU_SOURCE #-DMAKE_VALGRIND_HAPPY
+CFLAGS+=-O6
 CFLAGS+=$(shell if $(CC) -march=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-march=$(PROC)"; fi)
 CFLAGS+=$(shell if uname -m | grep -q ppc; then echo "-fsigned-char"; fi)
 
@@ -42,12 +42,12 @@ CFLAGS+= -DDO_CRASH -DDEBUG_THREADS
 # Uncomment next one to enable ast_frame tracing (for debugging)
 #CLFAGS+= -DTRACE_FRAMES
 CFLAGS+=# -fomit-frame-pointer 
-SUBDIRS=res channels pbx apps codecs formats agi cdr
+SUBDIRS=res channels pbx apps codecs formats agi cdr astman
 LIBS=-ldl -lpthread -lreadline -lncurses -lm #-lnjamd
 OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
 	translate.o file.o say.o pbx.o cli.o md5.o term.o \
 	ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
-	cdr.o tdd.o acl.o rtp.o asterisk.o 
+	cdr.o tdd.o acl.o rtp.o manager.o asterisk.o ast_expr.o chanvars.o
 CC=gcc
 INSTALL=install
 
@@ -67,6 +67,11 @@ _version:
 
 .version: _version
 
+.y.c:
+	bison $< --name-prefix=ast_yy -o $@
+
+ast_expr.o: ast_expr.c
+
 build.h:
 	./make_build_h
 
@@ -79,14 +84,15 @@ subdirs:
 clean:
 	for x in $(SUBDIRS); do $(MAKE) -C $$x clean || exit 1 ; done
 	rm -f *.o *.so asterisk
-	rm -f build.h
+	rm -f build.h 
+	rm -f ast_expr.c
 
 datafiles: all
 	mkdir -p $(INSTALL_PREFIX)/var/lib/asterisk/sounds/digits
 	for x in sounds/digits/*; do \
 		install $$x $(INSTALL_PREFIX)/var/lib/asterisk/sounds/digits ; \
 	done
-	for x in sounds/vm-* sounds/transfer* sounds/pbx-* sounds/ss-* sounds/beep* sounds/dir-* sounds/conf-*; do \
+	for x in sounds/vm-* sounds/transfer* sounds/pbx-* sounds/ss-* sounds/beep* sounds/dir-* sounds/conf-* sounds/agent-*; do \
 		install $$x $(INSTALL_PREFIX)/var/lib/asterisk/sounds ; \
 	done
 	mkdir -p $(INSTALL_PREFIX)/var/lib/asterisk/mohmp3
diff --git a/apps/app_dial.c b/apps/app_dial.c
index 20db11b0f179804dc71215181b8aa9934c88b6d6..ee49dc2bd956b415ffd2dd1109d733014636b28d 100755
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -61,6 +61,8 @@ static char *descrip =
 "      'T' -- to allow the calling user to transfer the call.\n"
 "      'r' -- indicate ringing to the calling party, pass no audio until answered.\n"
 "      'm' -- provide hold music to the calling party until answered.\n"
+"      'd' -- data-quality (modem) call (minimum delay).\n"
+"      'H' -- allow caller to hang up by hitting *.\n"
 "  In addition to transferring the call, a call may be parked and then picked\n"
 "up by another user.\n";
 
@@ -74,6 +76,8 @@ struct localuser {
 	int allowredirect;
 	int ringbackonly;
 	int musiconhold;
+	int dataquality;
+	int allowdisconnect;
 	struct localuser *next;
 };
 
@@ -95,7 +99,7 @@ static void hanguptree(struct localuser *outgoing, struct ast_channel *exception
 
 #define MAX 256
 
-static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir)
+static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localuser *outgoing, int *to, int *allowredir, int *allowdisconnect)
 {
 	struct localuser *o;
 	int found;
@@ -161,7 +165,15 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 		winner = ast_waitfor_n(watchers, pos, to);
 		o = outgoing;
 		while(o) {
-			if (o->chan == winner) {
+			if (o->stillgoing && (o->chan->_state == AST_STATE_UP)) {
+				if (!peer) {
+					if (option_verbose > 2)
+						ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
+					peer = o->chan;
+					*allowredir = o->allowredirect;
+					*allowdisconnect = o->allowdisconnect;
+				}
+			} else if (o->chan == winner) {
 				f = ast_read(winner);
 				if (f) {
 					if (f->frametype == AST_FRAME_CONTROL) {
@@ -173,6 +185,7 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 									ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", o->chan->name, in->name);
 								peer = o->chan;
 								*allowredir = o->allowredirect;
+								*allowdisconnect = o->allowdisconnect;
 							}
 							break;
 						case AST_CONTROL_BUSY:
@@ -234,6 +247,17 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct localu
 				*to=-1;
 				return NULL;
 			}
+			if (f && (f->frametype == AST_FRAME_DTMF) && allowdisconnect &&
+				(f->subclass == '*')) {
+			    if (option_verbose > 3)
+				ast_verbose(VERBOSE_PREFIX_3 "User hit %c to disconnect call.\n", f->subclass);
+				*to=0;
+				return NULL;
+			}
+			if (single && ((f->frametype == AST_FRAME_VOICE) || (f->frametype == AST_FRAME_DTMF)))  {
+				if (ast_write(outgoing->chan, f))
+					ast_log(LOG_WARNING, "Unable to forward voice\n");
+			}
 		}
 		if (!*to && (option_verbose > 2))
 			ast_verbose( VERBOSE_PREFIX_3 "Nobody picked up in %d ms\n", orig);
@@ -252,11 +276,12 @@ static int dial_exec(struct ast_channel *chan, void *data)
 {
 	int res=-1;
 	struct localuser *u;
-	char *info, *peers, *timeout, *tech, *number, *rest, *cur;
+	char info[256], *peers, *timeout, *tech, *number, *rest, *cur;
 	struct localuser *outgoing=NULL, *tmp;
 	struct ast_channel *peer;
 	int to;
 	int allowredir=0;
+	int allowdisconnect=0;
 	char numsubst[AST_MAX_EXTENSION];
 	char restofit[AST_MAX_EXTENSION];
 	char *transfer = NULL;
@@ -270,11 +295,6 @@ static int dial_exec(struct ast_channel *chan, void *data)
 	LOCAL_USER_ADD(u);
 	
 	/* Parse our arguments XXX Check for failure XXX */
-	info = malloc(strlen((char *)data) + AST_MAX_EXTENSION);
-	if (!info) {
-		ast_log(LOG_WARNING, "Out of memory\n");
-		return -1;
-	}
 	strncpy(info, (char *)data, strlen((char *)data) + AST_MAX_EXTENSION-1);
 	peers = info;
 	if (peers) {
@@ -325,6 +345,10 @@ static int dial_exec(struct ast_channel *chan, void *data)
 				tmp->ringbackonly = 1;
 			if (strchr(transfer, 'm'))
 				tmp->musiconhold = 1;
+			if (strchr(transfer, 'd'))
+				tmp->dataquality = 1;
+			if (strchr(transfer, 'H'))
+				tmp->allowdisconnect = 1;
 		}
 		strncpy(numsubst, number, sizeof(numsubst)-1);
 		/* If we're dialing by extension, look at the extension to know what to dial */
@@ -396,6 +420,9 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		tmp->stillgoing = -1;
 		tmp->next = outgoing;
 		outgoing = tmp;
+		/* If this line is up, don't try anybody else */
+		if (outgoing->chan->_state == AST_STATE_UP)
+			break;
 		cur = rest;
 	} while(cur);
 	
@@ -403,7 +430,7 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		to = atoi(timeout) * 1000;
 	else
 		to = -1;
-	peer = wait_for_answer(chan, outgoing, &to, &allowredir);
+	peer = wait_for_answer(chan, outgoing, &to, &allowredir, &allowdisconnect);
 	if (!peer) {
 		if (to) 
 			/* Musta gotten hung up */
@@ -432,18 +459,19 @@ static int dial_exec(struct ast_channel *chan, void *data)
 		}
 		if (!strcmp(chan->type,"Zap")) {
 			int x = 2;
+			if (tmp->dataquality) x = 0;
 			ast_channel_setoption(chan,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
 		}			
 		if (!strcmp(peer->type,"Zap")) {
 			int x = 2;
+			if (tmp->dataquality) x = 0;
 			ast_channel_setoption(peer,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
 		}			
-		res = ast_bridge_call(chan, peer, allowredir);
+		res = ast_bridge_call(chan, peer, allowredir, allowdisconnect);
 		ast_hangup(peer);
 	}	
 out:
 	hanguptree(outgoing, NULL);
-	free(info);
 	LOCAL_USER_REMOVE(u);
 	return res;
 }
diff --git a/channels/Makefile b/channels/Makefile
index dc5cabba427a2d44e8910b7ea06ce6b816bedb17..7255f0e81a3b42d3ee5475eab6777de39bb075c4 100755
--- a/channels/Makefile
+++ b/channels/Makefile
@@ -13,7 +13,8 @@
 
 CHANNEL_LIBS=chan_modem.so chan_iax.so chan_sip.so \
 	     chan_modem_aopen.so chan_oss.so \
-             chan_modem_bestdata.so chan_modem_i4l.so
+             chan_modem_bestdata.so chan_modem_i4l.so \
+             chan_agent.so
 
 #
 # If you really want VoFR you can have it :-P
diff --git a/channels/chan_agent.c b/channels/chan_agent.c
new file mode 100755
index 0000000000000000000000000000000000000000..d9eef1c663e91ec9d9cb35b2d0b48e435b896bec
--- /dev/null
+++ b/channels/chan_agent.c
@@ -0,0 +1,611 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Implementation of Session Initiation Protocol
+ * 
+ * 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 <stdio.h>
+#include <pthread.h>
+#include <string.h>
+#include <asterisk/lock.h>
+#include <asterisk/channel.h>
+#include <asterisk/channel_pvt.h>
+#include <asterisk/config.h>
+#include <asterisk/logger.h>
+#include <asterisk/module.h>
+#include <asterisk/pbx.h>
+#include <asterisk/options.h>
+#include <asterisk/lock.h>
+#include <asterisk/sched.h>
+#include <asterisk/io.h>
+#include <asterisk/rtp.h>
+#include <asterisk/acl.h>
+#include <asterisk/callerid.h>
+#include <asterisk/file.h>
+#include <asterisk/cli.h>
+#include <asterisk/app.h>
+#include <asterisk/musiconhold.h>
+#include <asterisk/manager.h>
+#include <sys/socket.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <sys/signal.h>
+
+static char *desc = "Agent Proxy Channel";
+static char *type = "Agent";
+static char *tdesc = "Call Agent Proxy Channel";
+static char *config = "agents.conf";
+
+static char *app = "AgentLogin";
+
+static char *synopsis = "Call agent login";
+
+static char *descrip =
+"  AgentLogin():\n"
+"Asks the agent to login to the system.  Always returns -1.  While\n"
+"logged in, the agent can receive calls and will hear a 'beep'\n"
+"when a new call comes in.  The agent can dump the call by pressing\n"
+"the star key.\n";
+
+static char moh[80] = "default";
+
+#define AST_MAX_AGENT	80		/* Agent ID or Password max length */
+
+static int capability = -1;
+
+static int usecnt =0;
+static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
+
+/* Protect the interface list (of sip_pvt's) */
+static pthread_mutex_t agentlock = AST_MUTEX_INITIALIZER;
+
+static struct agent_pvt {
+	pthread_mutex_t lock;				/* Channel private lock */
+	int dead;							/* Poised for destruction? */
+	char moh[80];						/* Which music on hold */
+	char agent[AST_MAX_AGENT];			/* Agent ID */
+	char password[AST_MAX_AGENT];		/* Password for Agent login */
+	char name[AST_MAX_AGENT];
+	struct ast_channel *owner;			/* Agent */
+	struct ast_channel *chan;			/* Channel we use */
+	struct agent_pvt *next;				/* Agent */
+} *agents = NULL;
+
+#define CLEANUP(ast, p) do { \
+	int x; \
+	if (p->chan) { \
+		for (x=0;x<AST_MAX_FDS;x++) \
+			ast->fds[x] = p->chan->fds[x]; \
+	} \
+} while(0)
+
+
+static int add_agent(struct ast_variable *var)
+{
+	char tmp[256];
+	char *password=NULL, *name=NULL;
+	struct agent_pvt *p;
+	
+	strncpy(tmp, var->value, sizeof(tmp));
+	if ((password = strchr(tmp, ','))) {
+		*password = '\0';
+		password++;
+		while (*password < 33) password++;
+	}
+	if (password && (name = strchr(password, ','))) {
+		*name = '\0';
+		name++;
+		while (*name < 33) name++; 
+	}
+	p = agents;
+	while(p) {
+		if (!strcmp(p->agent, tmp))
+			break;
+		p = p->next;
+	}
+	if (!p) {
+		p = malloc(sizeof(struct agent_pvt));
+		if (p) {
+			memset(p, 0, sizeof(struct agent_pvt));
+			strncpy(p->agent, tmp, sizeof(p->agent) -1);
+			p->next = agents;
+			agents = p;
+			
+		}
+	}
+	if (!p)
+		return -1;
+	strncpy(p->password, password ? password : "", sizeof(p->password) - 1);
+	strncpy(p->name, name ? name : "", sizeof(p->name) - 1);
+	strncpy(p->moh, moh, sizeof(p->moh) - 1);
+	p->dead = 0;
+	return 0;
+}
+
+static int agent_answer(struct ast_channel *ast)
+{
+	ast_log(LOG_WARNING, "Huh?  Agent is being asked to answer?\n");
+	return -1;
+}
+
+static struct ast_frame  *agent_read(struct ast_channel *ast)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	struct ast_frame *f = NULL;
+	ast_pthread_mutex_lock(&p->lock);
+	if (p->chan)
+		f = ast_read(p->chan);
+	if (!f) {
+		/* If there's a channel, make it NULL */
+		if (p->chan)
+			p->chan = NULL;
+	}
+	if (f && (f->frametype == AST_FRAME_DTMF) && (f->subclass == '*')) {
+		/* * terminates call */
+		ast_frfree(f);
+		f = NULL;
+	}
+	CLEANUP(ast,p);
+	ast_pthread_mutex_unlock(&p->lock);
+	return f;
+}
+
+static int agent_write(struct ast_channel *ast, struct ast_frame *f)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	int res = -1;
+	ast_pthread_mutex_lock(&p->lock);
+	if (p->chan)
+		res = ast_write(p->chan, f);
+	CLEANUP(ast, p);
+	ast_pthread_mutex_unlock(&p->lock);
+	return res;
+}
+
+static int agent_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
+{
+	struct agent_pvt *p = newchan->pvt->pvt;
+	ast_pthread_mutex_lock(&p->lock);
+	if (p->owner != oldchan) {
+		ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
+		return -1;
+	}
+	p->owner = newchan;
+	ast_pthread_mutex_unlock(&p->lock);
+	return 0;
+}
+
+static int agent_indicate(struct ast_channel *ast, int condition)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	int res = -1;
+	ast_pthread_mutex_lock(&p->lock);
+	if (p->chan)
+		res = ast_indicate(p->chan, condition);
+	ast_pthread_mutex_unlock(&p->lock);
+	return res;
+}
+
+static int agent_digit(struct ast_channel *ast, char digit)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	int res = -1;
+	ast_pthread_mutex_lock(&p->lock);
+	if (p->chan)
+		res = p->chan->pvt->send_digit(p->chan, digit);
+	ast_pthread_mutex_unlock(&p->lock);
+	return res;
+}
+
+static int agent_call(struct ast_channel *ast, char *dest, int timeout)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	int res = -1;
+	ast_pthread_mutex_lock(&p->lock);
+	res = ast_streamfile(p->chan, "beep", p->chan->language);
+	if (!res)
+		res = ast_waitstream(p->chan, "");
+	if (!res) {
+		res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
+		if (res)
+			ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
+	}
+	if (!res) {
+		ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
+		if (res)
+			ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
+	}
+	/* Call is immediately up */
+	ast_setstate(ast, AST_STATE_UP);
+	ast_pthread_mutex_unlock(&p->lock);
+	return res;
+}
+
+static int agent_hangup(struct ast_channel *ast)
+{
+	struct agent_pvt *p = ast->pvt->pvt;
+	ast_pthread_mutex_lock(&p->lock);
+	p->owner = NULL;
+	ast->pvt->pvt = NULL;
+	ast_pthread_mutex_unlock(&p->lock);
+	if (p->chan) {
+		/* If they're dead, go ahead and hang up on the agent now */
+		if (p->dead)
+			ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
+		ast_moh_start(p->chan, p->moh);
+	} else if (p->dead) 
+		/* Go ahead and lose it */
+		free(p);
+	return 0;
+}
+
+static struct ast_channel *agent_new(struct agent_pvt *p, int state)
+{
+	struct ast_channel *tmp;
+	if (!p->chan) {
+		ast_log(LOG_WARNING, "No channel? :(\n");
+		return NULL;
+	}
+	tmp = ast_channel_alloc(0);
+	if (tmp) {
+		tmp->nativeformats = p->chan->nativeformats;
+		snprintf(tmp->name, sizeof(tmp->name), "Agent/%s", p->agent);
+		tmp->type = type;
+		ast_setstate(tmp, state);
+		tmp->writeformat = p->chan->writeformat;
+		tmp->pvt->rawwriteformat = p->chan->writeformat;
+		tmp->readformat = p->chan->readformat;
+		tmp->pvt->rawreadformat = p->chan->readformat;
+		tmp->pvt->pvt = p;
+		tmp->pvt->send_digit = agent_digit;
+		tmp->pvt->call = agent_call;
+		tmp->pvt->hangup = agent_hangup;
+		tmp->pvt->answer = agent_answer;
+		tmp->pvt->read = agent_read;
+		tmp->pvt->write = agent_write;
+		tmp->pvt->exception = agent_read;
+		tmp->pvt->indicate = agent_indicate;
+		tmp->pvt->fixup = agent_fixup;
+		strncpy(tmp->language, p->chan->language, sizeof(tmp->language)-1);
+		p->owner = tmp;
+		ast_pthread_mutex_lock(&usecnt_lock);
+		usecnt++;
+		ast_pthread_mutex_unlock(&usecnt_lock);
+		ast_update_use_count();
+		strncpy(tmp->context, p->chan->context, sizeof(tmp->context)-1);
+		strncpy(tmp->exten, p->chan->exten, sizeof(tmp->exten)-1);
+		tmp->priority = 1;
+		/* Wake up any waiting blockers (by definition the login app) */
+		if (p->chan->blocking) {
+			pthread_kill(p->chan->blocker, SIGURG);
+			/* Wait until the blocker releases it */
+			while(p->chan->blocking)
+				usleep(1000);
+		}
+		ast_moh_stop(p->chan);
+	} else
+		ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
+	return tmp;
+}
+
+
+static int read_agent_config(void)
+{
+	struct ast_config *cfg;
+	struct ast_variable *v;
+	struct agent_pvt *p, *pl, *pn;
+	cfg = ast_load(config);
+	if (!cfg) {
+		ast_log(LOG_NOTICE, "No agent configuration found -- agent support disabled\n");
+		return 0;
+	}
+	ast_pthread_mutex_lock(&agentlock);
+	p = agents;
+	while(p) {
+		p->dead = 1;
+		p = p->next;
+	}
+	strcpy(moh, "default");
+	v = ast_variable_browse(cfg, "agents");
+	while(v) {
+		/* Create the interface list */
+		if (!strcasecmp(v->name, "agent")) {
+			add_agent(v);
+		} else if (!strcasecmp(v->name, "musiconhold")) {
+			strncpy(moh, v->value, sizeof(moh) - 1);
+		}
+		v = v->next;
+	}
+	p = agents;
+	pl = NULL;
+	while(p) {
+		pn = p->next;
+		if (p->dead) {
+			/* Unlink */
+			if (pl)
+				pl->next = p->next;
+			else
+				agents = p->next;
+			/* Destroy if  appropriate */
+			if (!p->owner) {
+				if (!p->chan) {
+					free(p);
+				} else {
+					/* Cause them to hang up */
+					ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
+				}
+			}
+		} else
+			pl = p;
+		p = pn;
+	}
+	ast_pthread_mutex_unlock(&agentlock);
+	ast_destroy(cfg);
+	return 0;
+}
+
+static struct ast_channel *agent_request(char *type, int format, void *data)
+{
+	struct agent_pvt *p;
+	struct ast_channel *chan = NULL;
+	ast_pthread_mutex_lock(&agentlock);
+	p = agents;
+	while(p) {
+		if (!strcmp(data, p->agent)) {
+			ast_pthread_mutex_lock(&p->lock);
+			/* Agent must be registered, but not have any active call */
+			if (!p->owner && p->chan) {
+				chan = agent_new(p, AST_STATE_DOWN);
+			}
+			ast_pthread_mutex_unlock(&p->lock);
+			break;
+		}
+		p = p->next;
+	}
+	ast_pthread_mutex_unlock(&agentlock);
+	return chan;
+}
+
+static int agents_show(int fd, int argc, char **argv)
+{
+	struct agent_pvt *p;
+	char username[256];
+	char location[256];
+	char talkingto[256];
+
+	if (argc != 2)
+		return RESULT_SHOWUSAGE;
+	ast_pthread_mutex_lock(&agentlock);
+	p = agents;
+	while(p) {
+		ast_pthread_mutex_lock(&p->lock);
+		if (strlen(p->name))
+			snprintf(username, sizeof(username), "(%s) ", p->name);
+		else
+			strcpy(username, "");
+		if (p->chan) {
+			snprintf(location, sizeof(location), "logged in on %s", p->chan->name);
+			if (p->owner && p->owner->bridge) {
+				snprintf(talkingto, sizeof(talkingto), " talking to %s", p->owner->bridge->name);
+			} else {
+				strcpy(talkingto, " is idle");
+			}
+		} else {
+			strcpy(location, "not logged in");
+			strcpy(talkingto, "");
+		}
+		ast_pthread_mutex_unlock(&p->lock);
+		ast_cli(fd, "%-12.12s %s%s%s\n", p->agent, 
+				username, location, talkingto);
+		p = p->next;
+	}
+	ast_pthread_mutex_unlock(&agentlock);
+	return RESULT_SUCCESS;
+}
+
+static char show_agents_usage[] = 
+"Usage: show agents\n"
+"       Provides summary information on agents.\n";
+
+static struct ast_cli_entry cli_show_agents = {
+	{ "show", "agents", NULL }, agents_show, 
+	"Show status of agents", show_agents_usage, NULL };
+
+STANDARD_LOCAL_USER;
+LOCAL_USER_DECL;
+
+static int login_exec(struct ast_channel *chan, void *data)
+{
+	int res=0;
+	int tries = 0;
+	struct agent_pvt *p;
+	struct localuser *u;
+	char user[AST_MAX_AGENT];
+	char pass[AST_MAX_AGENT];
+	char xpass[AST_MAX_AGENT] = "";
+	char *errmsg;
+	LOCAL_USER_ADD(u);
+	if (chan->_state != AST_STATE_UP)
+		res = ast_answer(chan);
+	if (!res)
+		res = ast_app_getdata(chan, "agent-user", user, sizeof(user) - 1, 0);
+	while (!res && (tries < 3)) {
+		/* Check for password */
+		ast_pthread_mutex_lock(&agentlock);
+		p = agents;
+		while(p) {
+			if (!strcmp(p->agent, user))
+				strncpy(xpass, p->password, sizeof(xpass) - 1);
+			p = p->next;
+		}
+		ast_pthread_mutex_unlock(&agentlock);
+		if (!res) {
+			if (strlen(xpass) || !p)
+				res = ast_app_getdata(chan, "agent-pass", pass, sizeof(pass) - 1, 0);
+			else
+				strcpy(pass, "");
+		}
+		errmsg = "agent-incorrect";
+
+#if 0
+		ast_log(LOG_NOTICE, "user: %s, pass: %s\n", user, pass);
+#endif		
+
+		/* Check again for accuracy */
+		ast_pthread_mutex_lock(&agentlock);
+		p = agents;
+		while(p) {
+			ast_pthread_mutex_lock(&p->lock);
+			if (!strcmp(p->agent, user) &&
+				!strcmp(p->password, pass)) {
+					if (!p->chan) {
+						res = ast_streamfile(chan, "agent-loginok", chan->language);
+						if (!res)
+							ast_waitstream(chan, "");
+						if (!res) {
+							res = ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
+							if (res)
+								ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(chan->nativeformats));
+						}
+						if (!res) {
+							ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
+							if (res)
+								ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(chan->nativeformats));
+						}
+						/* Check once more just in case */
+						if (p->chan)
+							res = -1;
+						if (!res) {
+							ast_moh_start(chan, p->moh);
+							manager_event(EVENT_FLAG_AGENT, "Agentlogin",
+								"Agent: %s\r\n"
+								"Channel: %s\r\n",
+								p->agent, chan->name);
+							if (option_verbose > 2)
+								ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged in (format %d/%d)\n", p->agent,
+												chan->readformat, chan->writeformat);
+							/* Login this channel and wait for it to
+							   go away */
+							p->chan = chan;
+							ast_pthread_mutex_unlock(&p->lock);
+							ast_pthread_mutex_unlock(&agentlock);
+							while ((res >= 0) && (p->chan == chan)) {
+								/* True sleep here, since we're being monitored
+								   elsewhere instead */
+								if (p->owner) 
+									sleep(1);
+								else
+									res = ast_safe_sleep(chan, 1000);
+							}
+							if (res && p->owner) 
+								ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
+							/* Log us off if appropriate */
+							if (p->chan == chan)
+								p->chan = NULL;
+							if (option_verbose > 2)
+								ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
+							manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
+								"Agent: %s\r\n",
+								p->agent);
+							/* If there is no owner, go ahead and kill it now */
+							if (p->dead && !p->owner)
+								free(p);
+						}
+						res = -1;
+					} else {
+						ast_pthread_mutex_unlock(&p->lock);
+						errmsg = "agent-alreadyon";
+						p = NULL;
+					}
+					break;
+			}
+			ast_pthread_mutex_unlock(&p->lock);
+			p = p->next;
+		}
+		if (!p)
+			ast_pthread_mutex_unlock(&agentlock);
+
+		if (!res)
+			res = ast_app_getdata(chan, errmsg, user, sizeof(user) - 1, 0);
+	}
+		
+	LOCAL_USER_REMOVE(u);
+	/* Always hangup */
+	return -1;
+}
+
+
+int load_module()
+{
+	/* Make sure we can register our sip channel type */
+	if (ast_channel_register(type, tdesc, capability, agent_request)) {
+		ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
+		return -1;
+	}
+	ast_register_application(app, login_exec, synopsis, descrip);
+	ast_cli_register(&cli_show_agents);
+	/* Read in the config */
+	read_agent_config();
+	return 0;
+}
+
+int reload()
+{
+	read_agent_config();
+	return 0;
+}
+
+int unload_module()
+{
+	struct agent_pvt *p;
+	/* First, take us out of the channel loop */
+	ast_cli_unregister(&cli_show_agents);
+	ast_unregister_application(app);
+	ast_channel_unregister(type);
+	if (!ast_pthread_mutex_lock(&agentlock)) {
+		/* Hangup all interfaces if they have an owner */
+		p = agents;
+		while(p) {
+			if (p->owner)
+				ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
+			p = p->next;
+		}
+		agents = NULL;
+		ast_pthread_mutex_unlock(&agentlock);
+	} else {
+		ast_log(LOG_WARNING, "Unable to lock the monitor\n");
+		return -1;
+	}		
+	return 0;
+}
+
+int usecount()
+{
+	int res;
+	ast_pthread_mutex_lock(&usecnt_lock);
+	res = usecnt;
+	ast_pthread_mutex_unlock(&usecnt_lock);
+	return res;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}
+
+char *description()
+{
+	return desc;
+}
+
diff --git a/configs/agents.conf.sample b/configs/agents.conf.sample
new file mode 100755
index 0000000000000000000000000000000000000000..83c2ddb3b349ef2dd6fcf40d1f25d4537bb847d7
--- /dev/null
+++ b/configs/agents.conf.sample
@@ -0,0 +1,11 @@
+;
+; Agent configuration
+;
+;
+[agents]
+;
+; This section contains the agent definitions, in the form:
+;
+; agent => agentid,agentpassword,name
+;
+;agent => 1234,4321,Mark Spencer
diff --git a/configs/manager.conf.sample b/configs/manager.conf.sample
new file mode 100755
index 0000000000000000000000000000000000000000..417730afb216f5978098864a2501552087f83f1b
--- /dev/null
+++ b/configs/manager.conf.sample
@@ -0,0 +1,12 @@
+;
+; Asterisk Call Management support
+;
+[general]
+enabled = no
+port = 5038
+bindaddr = 0.0.0.0
+
+;[mark]
+;secret = mysecret
+;read = system,call,log,verbose,command,agent
+;write = system,call,log,verbose,command,agent
diff --git a/include/asterisk/manager.h b/include/asterisk/manager.h
new file mode 100755
index 0000000000000000000000000000000000000000..5eb4965d5aae0c6bdaa75c72224112b1a24743f6
--- /dev/null
+++ b/include/asterisk/manager.h
@@ -0,0 +1,52 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * External call management support 
+ * 
+ * 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.
+ *
+ * Includes code and algorithms from the Zapata library.
+ *
+ */
+
+#ifndef _ASTERISK_MANAGER_H
+#define _ASTERISK_MANAGER_H
+
+#include <stdarg.h>
+
+/* 
+ * Call management packages are text fields of the form a: b.  There is
+ * always exactly one space after the colon.
+ *
+ * The first header type is the "Event" header.  Other headers vary from
+ * event to event.  Headers end with standard \r\n termination.
+ *
+ * Some standard headers:
+ *
+ * Action: <action>			-- request or notification of a particular action
+ * Response: <response>		-- response code, like "200 OK"
+ *
+ */
+ 
+#define DEFAULT_MANAGER_PORT 5038	/* Default port for Asterisk management via TCP */
+
+#define EVENT_FLAG_SYSTEM 		(1 << 0) /* System events such as module load/unload */
+#define EVENT_FLAG_CALL			(1 << 1) /* Call event, such as state change, etc */
+#define EVENT_FLAG_LOG			(1 << 2) /* Log events */
+#define EVENT_FLAG_VERBOSE		(1 << 3) /* Verbose messages */
+#define EVENT_FLAG_COMMAND		(1 << 4) /* Ability to read/set commands */
+#define EVENT_FLAG_AGENT		(1 << 5) /* Ability to read/set agent info */
+
+/* External routines may send asterisk manager events this way */
+extern int manager_event(int category, char *event, char *contents, ...)
+	__attribute__ ((format (printf, 3,4)));
+
+/* Called by Asterisk initialization */
+extern int init_manager(void);
+extern int reload_manager(void);
+#endif
diff --git a/sounds/agent-alreadyon.gsm b/sounds/agent-alreadyon.gsm
new file mode 100755
index 0000000000000000000000000000000000000000..9cc3f1a10a6564381937252a90c0cf3c78adc06c
Binary files /dev/null and b/sounds/agent-alreadyon.gsm differ
diff --git a/sounds/agent-incorrect.gsm b/sounds/agent-incorrect.gsm
new file mode 100755
index 0000000000000000000000000000000000000000..3fcce85b3043d70d11cf24aa5f004b56c33dbffa
Binary files /dev/null and b/sounds/agent-incorrect.gsm differ
diff --git a/sounds/agent-loginok.gsm b/sounds/agent-loginok.gsm
new file mode 100755
index 0000000000000000000000000000000000000000..233656fd91acbee7f6529fdde458fd8f945e4f65
Binary files /dev/null and b/sounds/agent-loginok.gsm differ
diff --git a/sounds/agent-pass.gsm b/sounds/agent-pass.gsm
new file mode 100755
index 0000000000000000000000000000000000000000..210405d10165a5ae9742f0d89fbb67e6086fb3d7
Binary files /dev/null and b/sounds/agent-pass.gsm differ
diff --git a/sounds/agent-user.gsm b/sounds/agent-user.gsm
new file mode 100755
index 0000000000000000000000000000000000000000..7f1615a331fe18e1596c34ffb73bdd424d29ae4f
Binary files /dev/null and b/sounds/agent-user.gsm differ