Index: asterisk/channels/chan_h323.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_h323.c,v
retrieving revision 1.12
diff -u -B -b -r1.12 chan_h323.c
--- asterisk/channels/chan_h323.c	10 Dec 2003 23:34:47 -0000	1.12
+++ asterisk/channels/chan_h323.c	28 Jan 2004 15:04:51 -0000
@@ -23,7 +23,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  *
- * Version Info: $Id: chan_h323.c,v 1.12 2003/12/10 23:34:47 jeremy Exp $
+ * Version Info: $Id: chan_h323.c,v 1.9 2003/10/21 08:52:51 jeremy Exp $
  */
 
 
@@ -75,12 +75,6 @@
 static int	port = 1720;
 static int  gkroute = 0;
 
-/* to find user by alias is default, alternative is the incomming call's source IP 
-address*/
-static int  userbyalias = 1;
-
-static int  bridge_default = 1;
-
 /* Just about everybody seems to support ulaw, so make it a nice default */
 static int capability = AST_FORMAT_ULAW;
 
@@ -211,8 +205,6 @@
 				strncpy(alias->context,  v->value, sizeof(alias->context)-1);
 			} else if (!strcasecmp(v->name, "secret")) {
 				strncpy(alias->secret,  v->value, sizeof(alias->secret)-1);
-			} else {
-					ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
 			}
 			v = v->next;
 		}
@@ -233,7 +225,7 @@
 		/* set the usage flag to a sane starting value*/
 		user->inUse = 0;
 		/* Assume we can native bridge */
-		user->bridge = bridge_default; 
+		user->bridge = 1; 
 
 		while(v) {
 			if (!strcasecmp(v->name, "context")) {
@@ -634,6 +626,25 @@
 	return 0;
 }
 
+
+struct oh323_user *find_user(const char *source_aliases)
+{
+	struct oh323_user *u;
+
+	u = userl.users;
+
+	while(u) {
+
+		if (!strcasecmp(u->name, source_aliases)) {
+			break;
+		}
+		u = u->next;
+	}
+	return u;
+
+}
+
+
 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
 {
 	struct ast_channel *ch;
@@ -684,7 +695,29 @@
 		usecnt++;
 		ast_mutex_unlock(&usecnt_lock);
 		ast_update_use_count();
-		strncpy(ch->context, i->context, sizeof(ch->context)-1);
+
+		//strncpy(ch->context, i->context, sizeof(ch->context)-1);
+		// **PATCH** 
+		if (!i->username) {
+			strcpy (i->username, "");
+			strncpy (ch->context, i->context, sizeof(ch->context)-1);
+		}
+		else {
+			struct oh323_user *u;
+			u = find_user(i->username);	
+			if (!u) {
+				/*if (!strlen(default_context))
+					ast_log(LOG_ERROR, "--JORGE-- No hay contexto asociado a la llamada");
+				else 
+					strncpy (ch->context, default_context, sizeof(ch->context)-1);*/
+				strcpy (i->username, "");
+				//strncpy (ch->context, i->context, sizeof(ch->context)-1);
+			}
+			else
+				strncpy(ch->context, u->context, sizeof(ch->context)-1);
+		}
+		ast_log(LOG_WARNING, "Contexto: %s\t Usuario: %s\n", ch->context, i->username);
+			
 		strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);		
 		ch->priority = 1;
 		if (strlen(i->callerid))
@@ -728,7 +761,7 @@
 	ast_mutex_init(&p->lock);
 	
 	p->cd.call_reference = callid;
-	p->bridge = bridge_default;
+	p->bridge = 1;
 	
 	p->dtmfmode = dtmfmode;
 	if (p->dtmfmode & H323_DTMF_RFC2833)
@@ -824,6 +857,10 @@
 
 	if (ext)
 		strncpy(p->username, ext, sizeof(p->username) - 1);
+	/**PATCH**/
+	else
+	        strncpy(p->username, host, sizeof(p->username) - 1);
+
 	tmpc = oh323_new(p, AST_STATE_DOWN, host);
 	if (!tmpc)
 		oh323_destroy(p);
@@ -849,32 +886,7 @@
 	return a;
 }
 
-struct oh323_user *find_user(const call_details_t cd)
-{
-	struct oh323_user *u;
 
-	u = userl.users;
-	if(userbyalias == 1){
-		while(u) {
-			if (!strcasecmp(u->name, cd.call_source_aliases)) {
-				break;
-			}
-			u = u->next;
-		}
-
-	} else {
-		while(u) {
-			if (!strcasecmp(cd.sourceIp, inet_ntoa(u->addr.sin_addr))) {
-				break;
-			}
-			u = u->next;
-		}
-
-	
-	}
-	return u;
-
-}
 
 struct oh323_peer *find_peer(char *dest_peer)
 {
@@ -1024,7 +1036,7 @@
 		   or we are not allowing gk routed calls */
 		
 
-		user  = find_user(cd);
+		user  = find_user(cd.call_source_aliases);
 
 
 		if (!user) {
@@ -1089,6 +1101,7 @@
 /* I know this is horrid, don't kill me saddam */
 exit:
 	/* allocate a channel and tell asterisk about it */
+	/**PATCH**/ /*ast_log(LOG_WARNING, "Username (2): %s\n",p->username);*/
 	c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
 	if (!c) {
 		ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
@@ -1192,7 +1205,7 @@
 
 	/* Decrement usage counter */
 	if (!p->outgoing) {
-		user = find_user(cd);
+		user = find_user(cd.call_source_aliases);
 		
 		if(user)
 			user->inUse--;
@@ -1203,7 +1216,7 @@
 		peer = find_peer(cd.call_dest_alias);
 		peer->inUse--;
 	} else {
-		user = find_user(cd);
+		user = find_user(cd.call_source_aliases);
 		user->inUse--;
 	}
 #endif
@@ -1410,9 +1423,6 @@
 		return 1;
 	}
 	
-       /* fire up the H.323 Endpoint */
-        h323_end_point_create();        
-
 	h323debug=0;
 	dtmfmode = H323_DTMF_RFC2833;
 
@@ -1485,10 +1495,6 @@
 				ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
 				dtmfmode = H323_DTMF_RFC2833;
 			}
-		} else if (!strcasecmp(v->name, "UserByAlias")) {
-                        userbyalias = ast_true(v->value);
-                } else if (!strcasecmp(v->name, "bridge")) {
-                        bridge_default = ast_true(v->value);
                 }
 		v = v->next;	
 	}
@@ -1721,11 +1727,11 @@
 {
 	int res;
 
-	res = reload_config();
+	/* fire up the H.323 Endpoint */ 
+	h323_end_point_create();
 
-	if (res) {
-		return 0;
-	} else {
+	res = reload_config();
+	if (!res) {
 		/* Make sure we can register our channel type */
 		if (ast_channel_register(type, tdesc, capability, oh323_request)) {
 			ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
@@ -1777,6 +1783,8 @@
 		}
 		/* And start the monitor for the first time */
 		restart_monitor();
+	} else {
+		h323_end_process();
 	}
 	return res;
 }
