Index: asterisk/channels/chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.130
diff -u -r1.130 chan_sip.c
--- asterisk/channels/chan_sip.c	29 Jun 2003 03:25:00 -0000	1.130
+++ asterisk/channels/chan_sip.c	17 Sep 2003 15:24:19 -0000
@@ -129,6 +129,8 @@
 
 static int globaldtmfmode = SIP_DTMF_RFC2833;
 
+static int globalcallwaiting = 1 ; //[PHM 17/09/03]
+
 /* Expire slowly */
 static int expiry = 900;
 
@@ -291,6 +293,7 @@
 	int insecure;
 	int nat;
 	int canreinvite;
+	int callwaiting; //[PHM 17/09/03]
 	unsigned int callgroup;
 	unsigned int pickupgroup;
         int dtmfmode;
@@ -5059,6 +5062,47 @@
 	return res;
 }
 
+
+static int can_accept_more_calls(char *peer)
+{
+	int ret = 1 ;
+	struct sip_peer *p;
+	ast_pthread_mutex_lock(&peerl.lock);
+	p = peerl.peers;
+	while(p) {
+		if (!strcasecmp(p->name, peer) && !p->callwaiting ) {
+			ret = 0 ; break ;
+		}
+
+		p = p->next ;
+	}
+	ast_pthread_mutex_unlock(&peerl.lock);
+	return(ret) ;
+}
+
+
+static int available(char *peer)
+{
+	int ret = 1 ;
+	struct sip_pvt *cur;
+
+	ast_pthread_mutex_lock(&iflock);
+	cur = iflist;
+
+	while(cur){
+		if (!cur->subscribed) {
+			if( !strcasecmp(cur->username,peer) && !can_accept_more_calls(peer)){
+				ret = 0 ;
+				break ;
+			}
+		}
+		cur = cur->next;
+	}
+	ast_pthread_mutex_unlock(&iflock);
+	return( ret ) ;		
+}
+
+
 static struct ast_channel *sip_request(char *type, int format, void *data)
 {
 	int oldformat;
@@ -5081,6 +5125,7 @@
 	}
 
 	strncpy(tmp, dest, sizeof(tmp) - 1);
+
 	host = strchr(tmp, '@');
 	if (host) {
 		*host = '\0';
@@ -5094,10 +5139,16 @@
 	/* Assign a default capability */
 	p->capability = capability;
 
+	//[PHM 17/09/03]
+	if( !available(host) ){
+		sip_destroy(p);
+		return NULL;
+	}
 	if (create_addr(p, host)) {
 		sip_destroy(p);
 		return NULL;
 	}
+
 	if (!strlen(p->peername) && ext)
 		strncpy(p->peername, ext, sizeof(p->peername) - 1);
 	/* Recalculate our side, and recalculate Call ID */
@@ -5232,6 +5283,7 @@
 		/* Assume can reinvite */
 		peer->canreinvite = REINVITE_INVITE;
 		peer->dtmfmode = 0;
+		peer->callwaiting = globalcallwaiting; //[PHM 17/09/03]
 		while(v) {
 			if (!strcasecmp(v->name, "secret")) 
 				strncpy(peer->secret, v->value, sizeof(peer->secret)-1);
@@ -5244,6 +5296,8 @@
 					peer->canreinvite = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "nat")) 
 				peer->nat = ast_true(v->value);
+			else if (!strcasecmp(v->name, "callwaiting")) 
+				peer->callwaiting = ast_true(v->value); //[PHM 17/09/03]
 			else if (!strcasecmp(v->name, "context"))
 				strncpy(peer->context, v->value, sizeof(peer->context)-1);
 			else if (!strcasecmp(v->name, "fromdomain"))
@@ -5402,6 +5456,8 @@
 			}
 		} else if (!strcasecmp(v->name, "videosupport")) {
 			videosupport = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "callwaiting")) { //[PHM 17/09/03]
+			globalcallwaiting = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "notifymimetype")) {
 			strncpy(notifymime, v->value, sizeof(notifymime) - 1);
 		} else if (!strcasecmp(v->name, "language")) {
