Browse code

XJAB module

Daniel-Constantin Mierla authored on 05/11/2002 13:25:13
Showing 7 changed files
... ...
@@ -86,25 +86,43 @@ int **pipes = NULL;
86 86
 
87 87
 static int mod_init(void);
88 88
 static int child_init(int rank);
89
-static int jab_send_message(struct sip_msg*, char*, char* );
89
+
90
+int xjab_manage_sipmsg(struct sip_msg *msg, int type);
91
+
92
+static int jab_send_message(struct sip_msg*, char*, char*);
93
+static int jab_send_bye(struct sip_msg*, char*, char*);
94
+static int jab_join_jconf(struct sip_msg*, char*, char*);
95
+static int jab_exit_jconf(struct sip_msg*, char*, char*);
96
+static int jab_go_online(struct sip_msg*, char*, char*);
97
+static int jab_go_offline(struct sip_msg*, char*, char*);
90 98
 
91 99
 void destroy(void);
92 100
 
93 101
 struct module_exports exports= {
94 102
 	"jabber",
95 103
 	(char*[]){
96
-		"jab_send_message"
104
+		"jab_send_message",
105
+		"jab_send_bye",
106
+		"jab_join_jconf",
107
+		"jab_exit_jconf",
108
+		"jab_go_online",
109
+		"jab_go_offline"
97 110
 	},
98 111
 	(cmd_function[]){
99
-		jab_send_message
112
+		jab_send_message,
113
+		jab_send_bye,
114
+		jab_join_jconf,
115
+		jab_exit_jconf,
116
+		jab_go_online,
117
+		jab_go_offline
100 118
 	},
101 119
 	(int[]){
102
-		0
120
+		0, 0, 0, 0, 0, 0
103 121
 	},
104 122
 	(fixup_function[]){
105
-		0
123
+		0, 0, 0, 0, 0, 0
106 124
 	},
107
-	1,
125
+	6,
108 126
 
109 127
 	(char*[]) {   /* Module parameter names */
110 128
 		"contact",
... ...
@@ -243,7 +261,8 @@ static int mod_init(void)
243 261
 			pipes[i][1]);
244 262
 	}
245 263
 	
246
-	if((jwl = xj_wlist_init(pipes, nrw, max_jobs)) == NULL)
264
+	if((jwl = xj_wlist_init(pipes,nrw,max_jobs,cache_time,sleep_time,
265
+				delay_time)) == NULL)
247 266
 	{
248 267
 		DBG("XJAB:mod_init: error initializing workers list\n");
249 268
 		return -1;
... ...
@@ -293,8 +312,7 @@ static int child_init(int rank)
293 312
 			if (pids[i] == 0)
294 313
 			{
295 314
 				close(pipes[i][1]);
296
-				xj_worker_process(jwl, jaddress, jport, pipes[i][0], max_jobs,
297
-					cache_time,	sleep_time, delay_time, db_con[i]);
315
+				xj_worker_process(jwl,jaddress,jport,pipes[i][0],db_con[i]);
298 316
 				exit(0);
299 317
 			}
300 318
 		}
... ...
@@ -317,22 +335,77 @@ static int child_init(int rank)
317 335
 }
318 336
 
319 337
 /**
320
- * send the SIP message through Jabber
338
+ * send the SIP MESSAGE through Jabber
321 339
  */
322 340
 static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
323 341
 {
324
-	str body, dst, /*host, user,*/ *p;
342
+	DBG("XJAB: processing SIP MESSAGE\n");
343
+	return xjab_manage_sipmsg(msg, XJ_SEND_MESSAGE);
344
+}
345
+
346
+/**
347
+ * send the SIP BYE through Jabber
348
+ */
349
+static int jab_send_bye(struct sip_msg *msg, char* foo1, char * foo2)
350
+{
351
+	DBG("XJAB: processing SIP BYE\n");
352
+	return xjab_manage_sipmsg(msg, XJ_SEND_BYE);
353
+}
354
+
355
+/**
356
+ * join a Jabber conference
357
+ */
358
+static int jab_join_jconf(struct sip_msg *msg, char* foo1, char * foo2)
359
+{
360
+	DBG("XJAB: join a Jabber conference\n");
361
+	return xjab_manage_sipmsg(msg, XJ_JOIN_JCONF);
362
+}
363
+
364
+/**
365
+ * exit from Jabber conference
366
+ */
367
+static int jab_exit_jconf(struct sip_msg *msg, char* foo1, char * foo2)
368
+{
369
+	DBG("XJAB: exit from a Jabber conference\n");
370
+	return xjab_manage_sipmsg(msg, XJ_EXIT_JCONF);
371
+}
372
+
373
+/**
374
+ * go online in Jabber network
375
+ */
376
+static int jab_go_online(struct sip_msg *msg, char* foo1, char * foo2)
377
+{
378
+	DBG("XJAB: go online in Jabber network\n");
379
+	return xjab_manage_sipmsg(msg, XJ_GO_ONLINE);
380
+}
381
+
382
+/**
383
+ * go offline in Jabber network
384
+ */
385
+static int jab_go_offline(struct sip_msg *msg, char* foo1, char * foo2)
386
+{
387
+	DBG("XJAB: go offline in Jabber network\n");
388
+	return xjab_manage_sipmsg(msg, XJ_GO_OFFLINE);
389
+}
390
+
391
+/**
392
+ * manage SIP message
393
+ */
394
+int xjab_manage_sipmsg(struct sip_msg *msg, int type)
395
+{
396
+	str body, dst;
325 397
 	xj_sipmsg jsmsg;
326 398
 	struct to_body to, from;
327 399
 	struct sip_uri _uri;
328 400
 	int pipe, fl;
329 401
 	struct to_param *foo,*bar;
330 402
 	char   *cp, *buf=0;
403
+	t_xj_jkey jkey, *p;
331 404
 
332 405
 	// extract message body - after that whole SIP MESSAGE is parsed
333
-	if ( imb.im_extract_body(msg,&body)==-1 )
406
+	if (type==XJ_SEND_MESSAGE && imb.im_extract_body(msg,&body)==-1 )
334 407
 	{
335
-		LOG(L_ERR,"ERROR:XJAB:xjab_send_message: cannot extract body"
408
+		LOG(L_ERR,"XJAB:xjab_manage_sipmsg: ERROR:cannot extract body"
336 409
 				" from sip msg!\n");
337 410
 		goto error;
338 411
 	}
... ...
@@ -341,7 +414,7 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
341 414
 	// check for FROM header
342 415
 	if(!msg->from)
343 416
 	{
344
-		LOG(L_ERR,"XJAB: xjab_send_message: cannot find FROM HEADER!\n");
417
+		LOG(L_ERR,"XJAB:xjab_manage_sipmsg: cannot find FROM HEADER!\n");
345 418
 		goto error;
346 419
 	}
347 420
 	
... ...
@@ -351,14 +424,14 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
351 424
 	buf = (char*)pkg_malloc(msg->from->body.len+1);
352 425
 	if (!buf) 
353 426
 	{
354
-		DBG("XJAB: xjab_send_message: error no free pkg memory\n");
427
+		DBG("XJAB:xjab_manage_sipmsg: error no free pkg memory\n");
355 428
 		goto error;
356 429
 	}
357 430
 	memcpy(buf,cp,msg->from->body.len+1);
358 431
 	parse_to(buf,buf+msg->from->body.len+1,&from);
359 432
 	if (from.error!=PARSE_OK ) 
360 433
 	{
361
-		DBG("XJAB: xjab_send_message: error cannot parse from header\n");
434
+		DBG("XJAB:xjab_manage_sipmsg: error cannot parse from header\n");
362 435
 		goto error;
363 436
 	}
364 437
 	/* we are not intrested in from param-> le's free them now*/
... ...
@@ -367,33 +440,55 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
367 440
 		bar = foo->next;
368 441
 		pkg_free(foo);
369 442
 	}
370
-	
443
+
444
+	jkey.hash = xj_get_hash(&from.uri, NULL);
445
+	jkey.id = &from.uri;
371 446
 	// get the communication pipe with the worker
372
-	if((pipe = xj_wlist_get(jwl, &from.uri, &p)) < 0)
447
+	switch(type)
373 448
 	{
374
-		DBG("XJAB: xjab_send_message: cannot find pipe of the worker!\n");
375
-		goto error;
449
+		case XJ_SEND_MESSAGE:
450
+		case XJ_JOIN_JCONF:
451
+		case XJ_GO_ONLINE:
452
+			if((pipe = xj_wlist_get(jwl, &jkey, &p)) < 0)
453
+			{
454
+				DBG("XJAB:xjab_manage_sipmsg: cannot find pipe of the worker!\n");
455
+				goto error;
456
+			}
457
+		break;
458
+		case XJ_SEND_BYE:
459
+		case XJ_EXIT_JCONF:
460
+		case XJ_GO_OFFLINE:
461
+			if((pipe = xj_wlist_check(jwl, &jkey, &p)) < 0)
462
+			{
463
+				DBG("XJAB:xjab_manage_sipmsg: no open Jabber session for"
464
+						" <%.*s>!\n", from.uri.len, from.uri.s);
465
+				goto error;
466
+			}
467
+		break;
468
+		default:
469
+			DBG("XJAB:xjab_manage_sipmsg: ERROR:strange SIP msg type!\n");
470
+			goto error;
376 471
 	}
377 472
 	
378 473
 	// determination of destination
379 474
 	dst.len = 0;
380 475
 	if( msg->new_uri.len > 0 )
381 476
 	{
382
-		DBG("XJAB: xjab_send_message: using NEW URI for destination\n");
477
+		DBG("XJAB:xjab_manage_sipmsg: using NEW URI for destination\n");
383 478
 		dst.s = msg->new_uri.s;
384 479
 		dst.len = msg->new_uri.len;
385 480
 	} else if ( msg->first_line.u.request.uri.len > 0 )
386 481
 	{
387
-		DBG("XJAB: xjab_send_message: parsing URI from first line\n");
482
+		DBG("XJAB:xjab_manage_sipmsg: parsing URI from first line\n");
388 483
 		if(parse_uri(msg->first_line.u.request.uri.s,
389 484
 					msg->first_line.u.request.uri.len, &_uri) < 0)
390 485
 		{
391
-			DBG("XJAB: xjab_send_message: ERROR parsing URI from first line\n");
486
+			DBG("XJAB:xjab_manage_sipmsg: ERROR parsing URI from first line\n");
392 487
 			goto error;
393 488
 		}
394 489
 		if(_uri.user.len > 0)
395 490
 		{
396
-			DBG("XJAB: xjab_send_message: using URI for destination\n");
491
+			DBG("XJAB:xjab_manage_sipmsg: using URI for destination\n");
397 492
 			dst.s = msg->first_line.u.request.uri.s;
398 493
 			dst.len = msg->first_line.u.request.uri.len;
399 494
 		}
... ...
@@ -406,20 +501,20 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
406 501
 				&to);
407 502
 		if(to.uri.len > 0) // to.error == PARSE_OK)
408 503
 		{
409
-			DBG("XJAB: xjab_send_message: TO parsed OK <%.*s>.\n",
504
+			DBG("XJAB:xjab_manage_sipmsg: TO parsed OK <%.*s>.\n",
410 505
 				to.uri.len, to.uri.s);
411 506
 			dst.s = to.uri.s;
412 507
 			dst.len = to.uri.len;
413 508
 		}
414 509
 		else
415 510
 		{
416
-			DBG("XJAB: xjab_send_message: TO NOT parsed\n");
511
+			DBG("XJAB:xjab_manage_sipmsg: TO NOT parsed\n");
417 512
 			goto error;
418 513
 		}
419 514
 	}
420 515
 	if(dst.len == 0)
421 516
 	{
422
-		DBG("XJAB: xjab_send_message: destination not found in SIP message\n");
517
+		DBG("XJAB:xjab_manage_sipmsg: destination not found in SIP message\n");
423 518
 		goto error;
424 519
 	}
425 520
 	
... ...
@@ -441,7 +536,7 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
441 536
 			dst.len += 3;
442 537
 		}
443 538
 		
444
-		DBG("XJAB: xjab_send_message: DESTINATION corrected <%.*s>.\n", 
539
+		DBG("XJAB:xjab_manage_sipmsg: DESTINATION corrected [%.*s].\n", 
445 540
 				dst.len, dst.s);
446 541
 	}
447 542
 	
... ...
@@ -449,33 +544,55 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
449 544
     jsmsg = (xj_sipmsg)shm_malloc(sizeof(t_xj_sipmsg));
450 545
     if(jsmsg == NULL)
451 546
     	return -1;
452
-	jsmsg->to.len = dst.len;
453
-	jsmsg->to.s = (char*)shm_malloc(jsmsg->to.len+1);
454
-	if(jsmsg->to.s == NULL)
547
+	
548
+	switch(type)
455 549
 	{
456
-		shm_free(jsmsg);
457
-		goto error;
550
+		case XJ_SEND_MESSAGE:
551
+			jsmsg->msg.len = body.len;
552
+			if((jsmsg->msg.s = (char*)shm_malloc(jsmsg->msg.len+1)) == NULL)
553
+			{
554
+				shm_free(jsmsg);
555
+				goto error;
556
+			}
557
+			strncpy(jsmsg->msg.s, body.s, jsmsg->msg.len);
558
+		break;
559
+		case XJ_SEND_BYE:
560
+		case XJ_JOIN_JCONF:
561
+		case XJ_EXIT_JCONF:
562
+		case XJ_GO_ONLINE:
563
+		case XJ_GO_OFFLINE:
564
+			jsmsg->msg.len = 0;
565
+			jsmsg->msg.s = NULL;
566
+		break;
567
+		default:
568
+			DBG("XJAB:xjab_manage_sipmsg: this SHOULD NOT appear\n");
569
+			shm_free(jsmsg);
570
+			goto error;
458 571
 	}
459
-	strncpy(jsmsg->to.s, dst.s, jsmsg->to.len);
460
-	
461
-	jsmsg->msg.len = body.len;
462
-	jsmsg->msg.s = (char*)shm_malloc(jsmsg->msg.len+1);
463
-	if(jsmsg->msg.s == NULL)
572
+	jsmsg->to.len = dst.len;
573
+	if((jsmsg->to.s = (char*)shm_malloc(jsmsg->to.len+1)) == NULL)
464 574
 	{
465
-		shm_free(jsmsg->to.s);
575
+		if(type == XJ_SEND_MESSAGE)
576
+			shm_free(jsmsg->msg.s);
466 577
 		shm_free(jsmsg);
467 578
 		goto error;
468 579
 	}
469
-	strncpy(jsmsg->msg.s, body.s, jsmsg->msg.len);
470
-	
471
-	jsmsg->from = p;
472
-	
473
-	DBG("XJAB: xjab_send_message:%d: sending <%p> to worker through <%d>\n",
580
+	strncpy(jsmsg->to.s, dst.s, jsmsg->to.len);
581
+
582
+	jsmsg->jkey = p;
583
+	jsmsg->type = type;
584
+	jsmsg->jkey->hash = jkey.hash;
585
+
586
+	DBG("XJAB:xjab_manage_sipmsg:%d: sending <%p> to worker through <%d>\n",
474 587
 			getpid(), jsmsg, pipe);
475 588
 	// sending the SHM pointer of SIP message to the worker
476 589
 	if(write(pipe, &jsmsg, sizeof(jsmsg)) != sizeof(jsmsg))
477 590
 	{
478
-		DBG("XJAB: xjab_send_message: error when writting to worker pipe!\n");
591
+		DBG("XJAB:xjab_manage_sipmsg: error when writting to worker pipe!\n");
592
+		if(type == XJ_SEND_MESSAGE)
593
+			shm_free(jsmsg->msg.s);
594
+		shm_free(jsmsg->to.s);
595
+		shm_free(jsmsg);
479 596
 		goto error;
480 597
 	}
481 598
 	
... ...
@@ -55,27 +55,44 @@
55 55
 
56 56
 #define JB_IQ_ROSTER_GET	"<iq type='get'><query xmlns='jabber:iq:roster'/></iq>"
57 57
 
58
+#define XJ_MAX_JCONF	12
59
+
58 60
 
59 61
 /**
60 62
  * init a JABBER connection
61 63
  */
62 64
 xj_jcon xj_jcon_init(char *hostname, int port)
63 65
 {
64
-	xj_jcon jbc = (xj_jcon)_M_MALLOC(sizeof(struct _xj_jcon));
66
+	xj_jcon jbc = NULL;
67
+	if(hostname==NULL || strlen(hostname)<=0)
68
+		return NULL;
69
+	
70
+	jbc = (xj_jcon)_M_MALLOC(sizeof(struct _xj_jcon));
65 71
 	if(jbc == NULL)
66 72
 		return NULL;
67 73
 	jbc->sock=-1;
68 74
     jbc->port = port;
69 75
     jbc->juid = -1;
70 76
 	jbc->seq_nr = 0;
71
-    jbc->hostname = (char*)_M_MALLOC(strlen(hostname));
77
+    jbc->hostname = (char*)_M_MALLOC(strlen(hostname)+1);
72 78
 	if(jbc->hostname == NULL)
73 79
 	{
74 80
 		_M_FREE(jbc);
75 81
 		return NULL;
76 82
 	}
83
+	if((jbc->close = (int*)_M_SHM_MALLOC(sizeof(int)))==NULL)
84
+	{
85
+		_M_FREE(jbc->hostname);
86
+		_M_FREE(jbc);
87
+		return NULL;
88
+
89
+	}
90
+	*jbc->close = 0;
77 91
     strcpy(jbc->hostname, hostname);
78 92
 	jbc->allowed = jbc->ready = XJ_NET_NUL;
93
+	jbc->jconf = NULL;
94
+	jbc->nrjconf = 0;
95
+
79 96
     return jbc;
80 97
 }
81 98
 
... ...
@@ -148,7 +165,7 @@ int xj_jcon_disconnect(xj_jcon jbc)
148 165
 		return -1;
149 166
 	DBG("XJAB:xj_jcon_disconnect: -----START-----\n");
150 167
     DBG("XJAB:xj_jcon_disconnect: socket [%d]\n", jbc->sock);
151
-	xj_jcon_send_presence(jbc, "unavailable", NULL, NULL);
168
+	xj_jcon_send_presence(jbc, NULL, "unavailable", NULL, NULL);
152 169
 	if(send(jbc->sock, "</stream:stream>", 16, 0) < 16)
153 170
 		DBG("XJAB:xj_jcon_disconnect: error closing stream\n");
154 171
 	if(close(jbc->sock) == -1)
... ...
@@ -305,7 +322,7 @@ int xj_jcon_user_auth(xj_jcon jbc, char *username, char *passwd,
305 322
 			strncasecmp(xode_get_attrib(x, "type"), "result", 6))
306 323
 		goto errorx;
307 324
 	
308
-	jbc->resource = (char*)_M_MALLOC(strlen(resource));
325
+	jbc->resource = (char*)_M_MALLOC(strlen(resource)+1);
309 326
 	strcpy(jbc->resource, resource);
310 327
 
311 328
 	jbc->allowed = XJ_NET_ALL;
... ...
@@ -334,7 +351,8 @@ int xj_jcon_get_roster(xj_jcon jbc)
334 351
  * send a message through a JABBER connection
335 352
  * params are pairs (buffer, len)
336 353
  */
337
-int xj_jcon_send_msg(xj_jcon jbc, char *to, int tol, char *msg, int msgl)
354
+int xj_jcon_send_msg(xj_jcon jbc, char *to, int tol, char *msg, 
355
+		int msgl, int type)
338 356
 {
339 357
 	char msg_buff[4096], *p;
340 358
 	int n;
... ...
@@ -350,7 +368,17 @@ int xj_jcon_send_msg(xj_jcon jbc, char *to, int tol, char *msg, int msgl)
350 368
 	strncpy(msg_buff, to, tol);
351 369
 	msg_buff[tol] = 0;
352 370
 	xode_put_attrib(x, "to", msg_buff);
353
-	xode_put_attrib(x, "type", "chat");
371
+	switch(type)
372
+	{
373
+		case XJ_JMSG_CHAT:
374
+			xode_put_attrib(x, "type", "chat");
375
+			break;
376
+		case XJ_JMSG_GROUPCHAT:
377
+			xode_put_attrib(x, "type", "groupchat");
378
+			break;
379
+		default:
380
+			xode_put_attrib(x, "type", "normal");
381
+	}
354 382
 
355 383
 	p = xode_to_str(x);
356 384
 	n = strlen(p);
... ...
@@ -386,7 +414,7 @@ int xj_jcon_recv_msg(xj_jcon jbc, char *from, char *msg)
386 414
  * status - "online", "away", "unavailable" ...
387 415
  * priority - "0", "1", ...
388 416
  */
389
-int xj_jcon_send_presence(xj_jcon jbc, char *type, char *status,
417
+int xj_jcon_send_presence(xj_jcon jbc, char *sto, char *type, char *status,
390 418
 				char *priority)
391 419
 {
392 420
 	char *p;
... ...
@@ -398,6 +426,8 @@ int xj_jcon_send_presence(xj_jcon jbc, char *type, char *status,
398 426
 	DBG("XJAB:xj_jcon_send_presence: -----START-----\n");
399 427
 	
400 428
 	x= xode_new_tag("presence");
429
+	if(sto != NULL)
430
+		xode_put_attrib(x, "to", sto);
401 431
 	if(type != NULL)
402 432
 		xode_put_attrib(x, "type", type);
403 433
 	if(status != NULL)
... ...
@@ -420,6 +450,7 @@ int xj_jcon_send_presence(xj_jcon jbc, char *type, char *status,
420 450
 		goto error;
421 451
 	}
422 452
 	xode_free(x);
453
+	DBG("XJAB:xj_jcon_send_presence: presence status was sent\n");
423 454
 	return 0;
424 455
 error:
425 456
 	xode_free(x);
... ...
@@ -431,6 +462,8 @@ error:
431 462
  */
432 463
 int xj_jcon_free(xj_jcon jbc)
433 464
 {
465
+	xj_jconf jcf;
466
+	
434 467
 	if(jbc == NULL)
435 468
 		return -1;
436 469
 	DBG("XJAB:xj_jcon_free: -----START-----\n");
... ...
@@ -441,15 +474,22 @@ int xj_jcon_free(xj_jcon jbc)
441 474
 		_M_FREE(jbc->hostname);
442 475
 	if(jbc->stream_id != NULL)
443 476
 		_M_FREE(jbc->stream_id);
444
-    /*******
445
-	if(jbc->username != NULL)
446
-		_M_FREE(jbc->username);
447
-	if(jbc->passwd != NULL)
448
-		_M_FREE(jbc->passwd);
449
-	*/
477
+	DBG("XJAB:xj_jcon_free: -----STEP A\n");	
478
+	if(jbc->close != NULL)
479
+	{
480
+		_M_SHM_FREE(jbc->close);
481
+		jbc->close = NULL;
482
+	}
483
+	DBG("XJAB:xj_jcon_free: -----STEP B\n");	
450 484
 	if(jbc->resource != NULL)
451 485
 		_M_FREE(jbc->resource);
452
-
486
+	DBG("XJAB:xj_jcon_free: %d conferences\n", jbc->nrjconf);
487
+	while(jbc->nrjconf > 0)
488
+	{
489
+		if((jcf=delpos234(jbc->jconf,0))!=NULL)
490
+			xj_jconf_free(jcf);
491
+		jbc->nrjconf--;
492
+	}
453 493
 	_M_FREE(jbc);
454 494
 	DBG("XJAB:xj_jcon_free: -----END-----\n");
455 495
 	return 0;
... ...
@@ -463,13 +503,13 @@ int xj_jcon_free(xj_jcon jbc)
463 503
  * - delay_time : time needed to became an active connection
464 504
  * #return : pointer to the structure or NULL on error
465 505
  */
466
-int xj_jcon_set_attrs(xj_jcon jbc, str *id, int cache_time, 
506
+int xj_jcon_set_attrs(xj_jcon jbc, xj_jkey jkey, int cache_time, 
467 507
 				int delay_time)
468 508
 {
469 509
 	int t;
470
-	if(jbc==NULL || id==NULL)
510
+	if(jbc==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
471 511
 		return -1;
472
-	jbc->id = id;
512
+	jbc->jkey = jkey;
473 513
 	t = get_ticks();
474 514
 	jbc->expire = t + cache_time;
475 515
 	jbc->ready = t + delay_time;
... ...
@@ -486,7 +526,8 @@ int xj_jcon_update(xj_jcon jbc, int cache_time)
486 526
 {
487 527
 	if(jbc == NULL)
488 528
 		return -1;
489
-	DBG("XJAB: xj_jcon_update -----START-----\n");
529
+	DBG("XJAB: xj_jcon_update [%.*s] %d\n", 
530
+			jbc->jkey->id->len, jbc->jkey->id->s, cache_time);
490 531
 	jbc->expire = get_ticks() + cache_time;
491 532
 	return 0;	
492 533
 }
... ...
@@ -494,9 +535,24 @@ int xj_jcon_update(xj_jcon jbc, int cache_time)
494 535
 int xj_jcon_is_ready(xj_jcon jbc, char *to, int tol)
495 536
 {
496 537
 	char *p;
538
+	str sto;
539
+	xj_jconf jcf = NULL;
497 540
 	if(!jbc || !to || tol <= 0)
498 541
 		return -1;
499 542
 	
543
+	sto.s = to;
544
+	sto.len = tol;
545
+	if(!xj_jconf_check_addr(&sto))
546
+	{
547
+		DBG("XJAB: xj_jcon_is_ready: destination=conference\n");
548
+		
549
+		if((jcf=xj_jcon_get_jconf(jbc, &sto))!=NULL)
550
+			return (jcf->status & XJ_JCONF_READY)?0:1;
551
+		
552
+		DBG("XJAB: xj_jcon_is_ready: conference does not exist\n");
553
+		return -1;
554
+	}
555
+	
500 556
 	p = to;
501 557
 	while(p < to+tol && *p!='@') 
502 558
 		p++;
... ...
@@ -514,10 +570,126 @@ int xj_jcon_is_ready(xj_jcon jbc, char *to, int tol)
514 570
 
515 571
 	if(!strncasecmp(p, XJ_YAH_NAME, XJ_YAH_LEN))
516 572
 		return (jbc->ready & XJ_NET_YAH)?0:((jbc->allowed & XJ_NET_YAH)?1:2);
517
-	
573
+
574
+	DBG("XJAB: xj_jcon_is_ready: destination=jabber\n");	
518 575
 	return 0;
519 576
 }
520 577
 
578
+xj_jconf  xj_jcon_get_jconf(xj_jcon jbc, str* sid)
579
+{
580
+	xj_jconf jcf = NULL, p;
581
+
582
+	if(!jbc || !sid || !sid->s || sid->len <= 0)
583
+		return NULL;
584
+	DBG("XJAB: xj_jcon_get_jconf: looking for conference\n");	
585
+	
586
+	if((jcf = xj_jconf_new(sid))==NULL)
587
+		return NULL;
588
+	if(xj_jconf_init_sip(jcf, jbc->jkey->id))
589
+		goto clean;
590
+	if(jbc->nrjconf && (p = find234(jbc->jconf, (void*)jcf, NULL)) != NULL)
591
+	{
592
+		DBG("XJAB: xj_jcon_get_jconf: conference found\n");
593
+		xj_jconf_free(jcf);
594
+		return p;
595
+	}
596
+	
597
+	if(jbc->nrjconf >= XJ_MAX_JCONF)
598
+		goto clean;
599
+
600
+	if(jbc->nrjconf==0)
601
+		if(jbc->jconf==NULL)
602
+			if((jbc->jconf = newtree234(xj_jconf_cmp)) == NULL)
603
+				goto clean;
604
+
605
+	if((p = add234(jbc->jconf, (void*)jcf)) != NULL)
606
+	{
607
+		DBG("XJAB: xj_jcon_get_jconf: new conference created\n");
608
+		jbc->nrjconf++;
609
+		return p;
610
+	}
611
+
612
+clean:
613
+	DBG("XJAB: xj_jcon_get_jconf: error looking for conference\n");
614
+	xj_jconf_free(jcf);
615
+	return NULL;
616
+}
617
+
618
+xj_jconf xj_jcon_check_jconf(xj_jcon jbc, char* id)
619
+{
620
+	str sid;
621
+	xj_jconf jcf = NULL, p = NULL;
622
+
623
+	if(!jbc || !id || !jbc->nrjconf)
624
+		return NULL;
625
+	DBG("XJAB: xj_jcon_get_jconf: looking for conference\n");	
626
+	
627
+	sid.s = id;
628
+	sid.len = strlen(id);
629
+	if((jcf = xj_jconf_new(&sid))==NULL)
630
+		return NULL;
631
+	if(xj_jconf_init_jab(jcf))
632
+		goto clean;
633
+	if((p = find234(jbc->jconf, (void*)jcf, NULL)) != NULL)
634
+	{
635
+		DBG("XJAB: xj_jcon_get_jconf: conference found\n");
636
+		xj_jconf_free(jcf);
637
+		return p;
638
+	}
639
+clean:
640
+	DBG("XJAB: xj_jcon_get_jconf: conference not found\n");
641
+	xj_jconf_free(jcf);
642
+	return NULL;	
643
+}
644
+
645
+int xj_jcon_jconf_presence(xj_jcon jbc, xj_jconf jcf, char* type, 
646
+		char* status)
647
+{
648
+	char buff[256];
649
+	
650
+	strncpy(buff, jcf->room.s, 
651
+			jcf->room.len + jcf->server.len +1);
652
+	buff[jcf->room.len + jcf->server.len +1] = '/';
653
+	buff[jcf->room.len + jcf->server.len +2] = 0;
654
+	buff[jcf->room.len] = '@';
655
+	strncat(buff, jcf->nick.s, jcf->nick.len);
656
+
657
+	return xj_jcon_send_presence(jbc,buff,type,status,NULL);
658
+}
659
+
660
+int  xj_jcon_del_jconf(xj_jcon jbc, str *sid, int flag)
661
+{
662
+	xj_jconf jcf = NULL, p = NULL;
663
+	
664
+	if(!jbc || !sid || !sid->s || sid->len <= 0)
665
+		return -1;
666
+	
667
+	DBG("XJAB: xj_jcon_del_jconf: deleting conference of <%.*s>\n",
668
+			sid->len, sid->s);
669
+	
670
+	if((jcf = xj_jconf_new(sid))==NULL)
671
+		return -1;
672
+	if(xj_jconf_init_sip(jcf, jbc->jkey->id))
673
+	{
674
+		xj_jconf_free(jcf);
675
+		return -1;
676
+	}
677
+	
678
+	p = del234(jbc->jconf, (void*)jcf);
679
+
680
+	if(p != NULL)
681
+	{
682
+		if(flag == XJ_JCMD_UNSUBSCRIBE)
683
+			xj_jcon_jconf_presence(jbc, jcf, "unavailable", NULL);
684
+		jbc->nrjconf--;
685
+		xj_jconf_free(p);
686
+		DBG("XJAB: xj_jcon_del_jconf: conference deleted\n");
687
+	}
688
+
689
+	xj_jconf_free(jcf);
690
+
691
+	return 0;
692
+}
521 693
 
522 694
 /**********    *********/
523 695
 
... ...
@@ -31,6 +31,9 @@
31 31
 #define _XJAB_JCON_H_
32 32
 
33 33
 #include "../../str.h"
34
+#include "xjab_jconf.h"
35
+#include "xjab_base.h"
36
+#include "tree234.h"
34 37
 
35 38
 #define XJ_NET_NUL	0
36 39
 #define XJ_NET_ALL	0xFFFFFFFF
... ...
@@ -49,6 +52,13 @@
49 52
 #define XJ_YAH_NAME "yahoo."
50 53
 #define XJ_YAH_LEN  6
51 54
 
55
+#define XJ_JMSG_NORMAL		1
56
+#define XJ_JMSG_CHAT		2
57
+#define XJ_JMSG_GROUPCHAT	4
58
+
59
+#define XJ_JCMD_SUBSCRIBE	1
60
+#define XJ_JCMD_UNSUBSCRIBE	2
61
+
52 62
 typedef struct _xj_jcon
53 63
 {
54 64
 	int sock;        // communication socket
... ...
@@ -58,17 +68,16 @@ typedef struct _xj_jcon
58 68
 	char *hostname;  // hosname of the Jabber server
59 69
 	char *stream_id; // stream id of the session
60 70
 
61
-	/****
62
-	char *username;
63
-	char *passwd;
64
-	*/
65 71
 	char *resource;  // resource ID
66 72
 
67
-	str *id;		// id of connection
73
+	xj_jkey jkey;		// id of connection
68 74
 	int expire;		// time when the open connection is expired
69 75
 	int allowed;	// allowed IM networks
70 76
 	int ready;		// time when the connection is ready for sending messages
71 77
 
78
+	int *close;		// t_uac callback parameter
79
+	int nrjconf;	// number of open conferences
80
+	tree234 *jconf; // open conferences
72 81
 } t_xj_jcon, *xj_jcon;
73 82
 
74 83
 /** --- **/
... ...
@@ -84,16 +93,21 @@ int  xj_jcon_get_juid(xj_jcon);
84 93
 int xj_jcon_get_roster(xj_jcon);
85 94
 
86 95
 int xj_jcon_user_auth(xj_jcon, char*, char*, char*);
87
-int xj_jcon_send_presence(xj_jcon, char*, char*, char*);
96
+int xj_jcon_send_presence(xj_jcon, char*, char*, char*, char*);
88 97
 
89
-int xj_jcon_send_msg(xj_jcon, char*, int, char*, int);
98
+int xj_jcon_send_msg(xj_jcon, char*, int, char*, int, int);
90 99
 int xj_jcon_send_sig_msg(xj_jcon, char*, int, char*, int, char*, int);
91 100
 
92 101
 int xj_jcon_is_ready(xj_jcon, char *, int);
93 102
 
103
+xj_jconf xj_jcon_get_jconf(xj_jcon, str*);
104
+xj_jconf xj_jcon_check_jconf(xj_jcon, char*);
105
+int xj_jcon_del_jconf(xj_jcon, str*, int);
106
+int xj_jcon_jconf_presence(xj_jcon, xj_jconf, char*, char*);
107
+
94 108
 /**********             ***/
95 109
 
96
-int xj_jcon_set_attrs(xj_jcon, str*, int, int);
110
+int xj_jcon_set_attrs(xj_jcon, xj_jkey, int, int);
97 111
 int xj_jcon_update(xj_jcon, int);
98 112
 
99 113
 /**********             ***/
... ...
@@ -65,20 +65,23 @@ xj_jcon_pool xj_jcon_pool_init(int size, int jlen, int ch)
65 65
 	jcp->jmqueue.len = jlen;
66 66
 	jcp->jmqueue.size = 0;
67 67
 	jcp->jmqueue.cache = (ch>0)?ch:90;
68
-	if((jcp->jmqueue.expire = (int*)_M_MALLOC(jlen*sizeof(int)))==NULL)
68
+	jcp->jmqueue.expire = (int*)_M_MALLOC(jlen*sizeof(int));
69
+	if(jcp->jmqueue.expire == NULL)
69 70
 	{
70 71
 		_M_FREE(jcp->ojc);
71 72
 		_M_FREE(jcp);
72 73
 		return NULL;
73 74
 	}
74
-	if((jcp->jmqueue.jsm=(xj_sipmsg*)_M_MALLOC(jlen*sizeof(xj_sipmsg)))==NULL)
75
+	jcp->jmqueue.jsm=(xj_sipmsg*)_M_MALLOC(jlen*sizeof(xj_sipmsg));
76
+	if(jcp->jmqueue.jsm == NULL)
75 77
 	{
76 78
 		_M_FREE(jcp->jmqueue.expire);
77 79
 		_M_FREE(jcp->ojc);
78 80
 		_M_FREE(jcp);
79 81
 		return NULL;
80 82
 	}
81
-	if((jcp->jmqueue.ojc = (xj_jcon*)_M_MALLOC(jlen*sizeof(xj_jcon)))==NULL)
83
+	jcp->jmqueue.ojc = (xj_jcon*)_M_MALLOC(jlen*sizeof(xj_jcon));
84
+	if(jcp->jmqueue.ojc == NULL)
82 85
 	{
83 86
 		_M_FREE(jcp->jmqueue.expire);
84 87
 		_M_FREE(jcp->jmqueue.jsm);
... ...
@@ -132,7 +135,7 @@ int xj_jcon_pool_del_jmsg(xj_jcon_pool jcp, int idx)
132 135
 {
133 136
 	if(jcp == NULL)
134 137
 		return -1;
135
-	if(jcp->jmqueue.size == 0)
138
+	if(jcp->jmqueue.size <= 0)
136 139
 		return -2;
137 140
 	jcp->jmqueue.size--;
138 141
 	jcp->jmqueue.jsm[idx] = NULL;
... ...
@@ -169,20 +172,20 @@ int xj_jcon_pool_add(xj_jcon_pool jcp, xj_jcon jc)
169 172
  * - id : id of the Jabber connection
170 173
  * #return : pointer to the open connection to Jabber structure or NULL on error
171 174
  */
172
-xj_jcon xj_jcon_pool_get(xj_jcon_pool jcp, str *id)
175
+xj_jcon xj_jcon_pool_get(xj_jcon_pool jcp, xj_jkey jkey)
173 176
 {
174 177
 	int i = 0;
175 178
 	xj_jcon _ojc;
176 179
 	
177
-	if(jcp == NULL || id == NULL)
180
+	if(jcp==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
178 181
 		return NULL;
179 182
 
180 183
 	DBG("XJAB:xj_jcon_pool_get: looking for the connection of <%.*s>"
181
-		" into the pool\n", id->len, id->s);
184
+		" into the pool\n", jkey->id->len, jkey->id->s);
182 185
 	while(i < jcp->len)
183 186
 	{
184
-	 	if((jcp->ojc[i]!=NULL) && (!strncmp(jcp->ojc[i]->id->s, id->s, 
185
-				id->len)))
187
+	 	if((jcp->ojc[i]!=NULL) && jcp->ojc[i]->jkey->hash==jkey->hash && 
188
+			(!strncmp(jcp->ojc[i]->jkey->id->s, jkey->id->s, jkey->id->len)))
186 189
 	 	{
187 190
 	 		_ojc = jcp->ojc[i];
188 191
 	 		//jcp->ojc[i] = NULL;
... ...
@@ -200,19 +203,19 @@ xj_jcon xj_jcon_pool_get(xj_jcon_pool jcp, str *id)
200 203
  * - id : id of the Jabber connection
201 204
  * #return : 0 on success or <0 on error
202 205
  */
203
-int xj_jcon_pool_del(xj_jcon_pool jcp, str *id)
206
+int xj_jcon_pool_del(xj_jcon_pool jcp, xj_jkey jkey)
204 207
 {
205 208
 	int i = 0;
206 209
 	
207
-	if(jcp == NULL)
210
+	if(jcp==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
208 211
 		return -1;
209 212
 
210 213
 	DBG("XJAB:xj_jcon_pool_del: removing a connection from the pool\n");
211 214
 
212 215
 	while(i < jcp->len)
213 216
 	{
214
-	 	if((jcp->ojc[i]!=NULL) && (!strncmp(jcp->ojc[i]->id->s, id->s,
215
-			id->len)))
217
+	 	if((jcp->ojc[i]!=NULL) && jcp->ojc[i]->jkey->hash==jkey->hash && 
218
+			(!strncmp(jcp->ojc[i]->jkey->id->s,jkey->id->s,jkey->id->len)))
216 219
 	 	{
217 220
 	 		xj_jcon_free(jcp->ojc[i]);
218 221
 	 		jcp->ojc[i] = NULL;
... ...
@@ -28,29 +28,11 @@
28 28
 
29 29
 
30 30
 #ifndef _XJAB_UTIL_H_
31
-#define _XHAB_UTIL_H_
32
-
33
-#include<sys/types.h>
34
-#include<sys/socket.h>
35
-#include<netinet/in.h>
36
-#include<netdb.h>
37
-#include<sys/un.h>
31
+#define _XJAB_UTIL_H_
38 32
 
33
+#include "xjab_base.h"
39 34
 #include "xjab_jcon.h"
40
-#include "tree234.h"
41 35
 #include "../../str.h"
42
-#include "../../db/db.h"
43
-#include "lock.h"
44
-#include "../tm/tm_load.h"
45
-
46
-/**********             ***/
47
-
48
-typedef struct _xj_sipmsg
49
-{
50
-	str *from; // pointer to FROM
51
-	str to;    // destination
52
-	str msg;   // message body
53
-} t_xj_sipmsg, *xj_sipmsg;
54 36
 
55 37
 /**********             ***/
56 38
 
... ...
@@ -77,8 +59,8 @@ typedef struct _xj_jcon_pool
77 59
 
78 60
 xj_jcon_pool xj_jcon_pool_init(int, int, int);
79 61
 int xj_jcon_pool_add(xj_jcon_pool, xj_jcon);
80
-xj_jcon xj_jcon_pool_get(xj_jcon_pool, str*);
81
-int xj_jcon_pool_del(xj_jcon_pool, str*);
62
+xj_jcon xj_jcon_pool_get(xj_jcon_pool, xj_jkey);
63
+int xj_jcon_pool_del(xj_jcon_pool, xj_jkey);
82 64
 void xj_jccon_pool_free(xj_jcon_pool);
83 65
 void xj_jcon_pool_print(xj_jcon_pool);
84 66
 int xj_jcon_pool_add_jmsg(xj_jcon_pool, xj_sipmsg, xj_jcon);
... ...
@@ -86,7 +68,5 @@ int xj_jcon_pool_del_jmsg(xj_jcon_pool, int);
86 68
 
87 69
 /**********             ***/
88 70
 
89
-char *shahash(const char *);
90
-
91 71
 #endif
92 72
 
... ...
@@ -34,13 +34,13 @@
34 34
 #include <sys/types.h>
35 35
 #include <fcntl.h>
36 36
 #include <errno.h>
37
+#include <signal.h>
37 38
 
38 39
 #include "../../dprint.h"
39 40
 #include "../../timer.h"
40 41
 #include "../../mem/mem.h"
41 42
 #include "../../mem/shm_mem.h"
42
-#include "../tm/t_funcs.h"
43
-#include "../tm/uac.h"
43
+#include "../tm/tm_load.h"
44 44
 
45 45
 #include "xjab_worker.h"
46 46
 #include "xjab_util.h"
... ...
@@ -51,60 +51,19 @@
51 51
 
52 52
 #define XJAB_RESOURCE "serXjab"
53 53
 
54
+#define XJ_ADDRTR_NUL	0
55
+#define XJ_ADDRTR_A2B	1
56
+#define XJ_ADDRTR_B2A	2
57
+#define XJ_ADDRTR_CON	4
58
+
59
+#define XJ_POOL_SIZE	10
60
+
54 61
 /** TM bind */
55 62
 extern struct tm_binds tmb;
56 63
 
57 64
 /** debug info */
58 65
 int _xj_pid = 0;
59
-
60
-/**
61
- * function used to compare two elements in B-Tree
62
- */
63
-int k_cmp(void *a, void *b)
64
-{
65
-	int n;
66
-	if(a == NULL)
67
-	    return -1;
68
-	if(a == NULL)
69
-	    return 1;
70
-	// DBG("JABBER: k_kmp: comparing <%.*s> / <%.*s>\n", ((str *)a)->len,
71
-	// 		((str *)a)->s, ((str *)b)->len, ((str *)b)->s);
72
-	if(((str *)a)->len != ((str *)b)->len)
73
-		return (((str *)a)->len < ((str *)b)->len)?-1:1;
74
-	n = strncmp(((str *)a)->s, ((str *)b)->s, ((str *)a)->len);
75
-	if(n!=0)
76
-		return (n<0)?-1:1;
77
-	return 0;
78
-}
79
-
80
-/**
81
- * free the information from a B-Tree node
82
- */
83
-void free_str_p(void *p)
84
-{
85
-	if(p == NULL)
86
-		return;
87
-	if(((str*)p)->s != NULL)
88
-		_M_SHM_FREE(((str*)p)->s);
89
-	_M_SHM_FREE(p);
90
-}
91
-
92
-/**
93
- * free a pointer to a t_jab_sipmsg structure
94
- * > element where points 'from' MUST be eliberated separated
95
- */
96
-void xj_sipmsg_free(xj_sipmsg jsmsg)
97
-{
98
-	if(jsmsg == NULL)
99
-		return;
100
-	if(jsmsg->to.s != NULL)
101
-		_M_SHM_FREE(jsmsg->to.s);
102
-//	if(jsmsg->from.s != NULL)
103
-//		_M_SHM_FREE(jsmsg->from.s);
104
-	if(jsmsg->msg.s != NULL)
105
-		_M_SHM_FREE(jsmsg->msg.s);
106
-	_M_SHM_FREE(jsmsg);
107
-}
66
+int main_loop = 1;
108 67
 
109 68
 /**
110 69
  * init a workers list
... ...
@@ -113,7 +72,8 @@ void xj_sipmsg_free(xj_sipmsg jsmsg)
113 72
  * - max : maximum number of jobs per worker
114 73
  * #return : pointer to workers list or NULL on error
115 74
  */
116
-xj_wlist xj_wlist_init(int **pipes, int size, int max)
75
+xj_wlist xj_wlist_init(int **pipes, int size, int max, int cache_time,
76
+		int sleep_time, int delay_time)
117 77
 {
118 78
 	int i;
119 79
 	xj_wlist jwl = NULL;
... ...
@@ -128,32 +88,48 @@ xj_wlist xj_wlist_init(int **pipes, int size, int max)
128 88
 		return NULL;
129 89
 	jwl->len = size;
130 90
 	jwl->maxj = max;
91
+	
92
+	jwl->cachet = cache_time;
93
+	jwl->delayt = delay_time;
94
+	jwl->sleept = sleep_time;
95
+
131 96
 	jwl->contact_h = NULL;
132 97
 	jwl->aliases = NULL;
98
+	jwl->sems = NULL;
99
+	i = 0;
133 100
 	if((jwl->sems = create_semaphores(size)) == NULL)
134
-	{
135
-		_M_SHM_FREE(jwl);
136
-		return NULL;
137
-	}
101
+		goto clean;
138 102
 	jwl->workers = (xj_worker)_M_SHM_MALLOC(size*sizeof(t_xj_worker));
139 103
 	if(jwl->workers == NULL)
140
-	{
141
-		_M_SHM_FREE(jwl);
142
-		return NULL;
143
-	}
104
+		goto clean;
144 105
 
145 106
 	for(i = 0; i < size; i++)
146 107
 	{
147 108
 		jwl->workers[i].nr = 0;
148 109
 		jwl->workers[i].pipe = pipes[i][1];
149
-		if((jwl->workers[i].sip_ids = newtree234(k_cmp)) == NULL)
150
-		{
151
-			_M_SHM_FREE(jwl);
152
-			return NULL;
153
-		}
110
+		if((jwl->workers[i].sip_ids = newtree234(xj_jkey_cmp)) == NULL)
111
+			goto clean;
154 112
 	}	
155 113
 
156 114
 	return jwl;
115
+
116
+clean:
117
+	DBG("XJAB:xj_wlist_init: error ocurred -> cleaning\n");
118
+	if(jwl->sems != NULL)
119
+		destroy_semaphores(jwl->sems);
120
+	if(jwl->workers != NULL)
121
+	{
122
+		while(i>=0)
123
+		{
124
+			if(jwl->workers[i].sip_ids == NULL)
125
+				free2tree234(jwl->workers[i].sip_ids, xj_jkey_free_p);
126
+			i--;
127
+		}
128
+		_M_SHM_FREE(jwl->workers);
129
+	}
130
+	_M_SHM_FREE(jwl);
131
+	return NULL;
132
+
157 133
 }
158 134
 
159 135
 /**
... ...
@@ -236,7 +212,7 @@ void xj_wlist_free(xj_wlist jwl)
236 212
 	if(jwl->workers != NULL)
237 213
 	{
238 214
 		for(i=0; i<jwl->len; i++)
239
-			free2tree234(jwl->workers[i].sip_ids, free_str_p);
215
+			free2tree234(jwl->workers[i].sip_ids, xj_jkey_free_p);
240 216
 		_M_SHM_FREE(jwl->workers);
241 217
 	}
242 218
 
... ...
@@ -264,6 +240,41 @@ void xj_wlist_free(xj_wlist jwl)
264 240
 	_M_SHM_FREE(jwl);
265 241
 }
266 242
 
243
+/**
244
+ * return communication pipe with the worker that will process the message for
245
+ * 		the id 'sid' only if it exsists, or -1 if error
246
+ * - jwl : pointer to the workers list
247
+ * - sid : id of the entity (connection to Jabber - usually SHOULD be FROM
248
+ *   header of the incomming SIP message)
249
+ * - p : will point to the SHM location of the 'sid' in jwl
250
+ */
251
+int xj_wlist_check(xj_wlist jwl, xj_jkey jkey, xj_jkey *p)
252
+{
253
+	int i;
254
+	if(jwl==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
255
+		return -1;
256
+	
257
+	i = 0;
258
+	*p = NULL;
259
+	while(i < jwl->len)
260
+	{
261
+		s_lock_at(jwl->sems, i);
262
+		if((*p = find234(jwl->workers[i].sip_ids, (void*)jkey, NULL)) != NULL)
263
+		{
264
+			s_unlock_at(jwl->sems, i);
265
+			DBG("XJAB:xj_wlist_check: entry exists for <%.*s> in the"
266
+				" pool of <%d> [%d]\n",jkey->id->len, jkey->id->s,
267
+				jwl->workers[i].pid,i);
268
+			return jwl->workers[i].pipe;
269
+		}
270
+		s_unlock_at(jwl->sems, i);
271
+		i++;
272
+	}
273
+	DBG("XJAB:xj_wlist_check: entry does not exist for <%.*s>\n",
274
+			jkey->id->len, jkey->id->s);	
275
+	return -1;
276
+}
277
+
267 278
 /**
268 279
  * return communication pipe with the worker that will process the message for
269 280
  * 		the id 'sid', or -1 if error
... ...
@@ -272,28 +283,26 @@ void xj_wlist_free(xj_wlist jwl)
272 283
  *   header of the incomming SIP message)
273 284
  * - p : will point to the SHM location of the 'sid' in jwl
274 285
  */
275
-int xj_wlist_get(xj_wlist jwl, str *sid, str **p)
286
+int xj_wlist_get(xj_wlist jwl, xj_jkey jkey, xj_jkey *p)
276 287
 {
277 288
 	int i = 0, pos = -1, min = 100000;
278
-	str *msid = NULL;
289
+	xj_jkey msid = NULL;
279 290
 	
280
-	if(jwl == NULL)
291
+	if(jwl==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
281 292
 		return -1;
282
-	DBG("XJAB:xj_wlist_get: -----START-----\n");
283
-	//mutex_lock(jwl->semid);
284 293
 
285 294
 	*p = NULL;
286 295
 	while(i < jwl->len)
287 296
 	{
288 297
 		s_lock_at(jwl->sems, i);
289
-		if((*p = find234(jwl->workers[i].sip_ids, (void*)sid, NULL)) != NULL)
298
+		if((*p = find234(jwl->workers[i].sip_ids, (void*)jkey, NULL))!=NULL)
290 299
 		{
291
-			//mutex_unlock(jwl->semid);
292 300
 			if(pos >= 0)
293 301
 				s_unlock_at(jwl->sems, pos);
294 302
 			s_unlock_at(jwl->sems, i);
295 303
 			DBG("XJAB:xj_wlist_get: entry already exists for <%.*s> in the"
296
-				" pool of <%d> [%d]\n",sid->len, sid->s,jwl->workers[i].pid,i);
304
+				" pool of <%d> [%d]\n",jkey->id->len, jkey->id->s,
305
+				jwl->workers[i].pid,i);
297 306
 			return jwl->workers[i].pipe;
298 307
 		}
299 308
 		if(min > jwl->workers[i].nr)
... ...
@@ -311,26 +320,46 @@ int xj_wlist_get(xj_wlist jwl, str *sid, str **p)
311 320
 	{
312 321
 		jwl->workers[pos].nr++;
313 322
 
314
-		msid = (str*)_M_SHM_MALLOC(sizeof(str));
315
-		if((msid != NULL) &&
316
-			(*p = add234(jwl->workers[pos].sip_ids, msid)) != NULL)
323
+		msid = (xj_jkey)_M_SHM_MALLOC(sizeof(t_xj_jkey));
324
+		if(msid == NULL)
325
+			goto error;
326
+		msid->id = (str*)_M_SHM_MALLOC(sizeof(str));
327
+		if(msid->id == NULL)
328
+		{
329
+			_M_SHM_FREE(msid);
330
+			goto error;
331
+		}
332
+		
333
+		msid->id->s = (char*)_M_SHM_MALLOC(jkey->id->len);
334
+		if(msid->id == NULL)
335
+		{
336
+			_M_SHM_FREE(msid->id);
337
+			_M_SHM_FREE(msid);
338
+			goto error;
339
+		}
340
+		
341
+		if((*p = add234(jwl->workers[pos].sip_ids, msid)) != NULL)
317 342
 		{
318
-			msid->s = (char*)_M_SHM_MALLOC(sid->len);
319
-			msid->len = sid->len;
320
-			memcpy(msid->s, sid->s, sid->len);
321
-			s_unlock_at(jwl->sems, pos);
322
-			//mutex_unlock(jwl->semid);
343
+			msid->id->len = jkey->id->len;
344
+			memcpy(msid->id->s, jkey->id->s, jkey->id->len);
345
+			msid->hash = jkey->hash;
346
+						s_unlock_at(jwl->sems, pos);
323 347
 			DBG("XJAB:xj_wlist_get: new entry for <%.*s> in the pool of"
324
-				" <%d> - [%d]\n", sid->len, sid->s,
348
+				" <%d> - [%d]\n", jkey->id->len, jkey->id->s,
325 349
 				jwl->workers[pos].pid, pos);
326 350
 			return jwl->workers[pos].pipe;
327 351
 		}
352
+		_M_SHM_FREE(msid->id->s);
353
+		_M_SHM_FREE(msid->id);
354
+		_M_SHM_FREE(msid);
328 355
 	}
329 356
 
357
+error:
330 358
 	if(pos >= 0)
331 359
 		s_unlock_at(jwl->sems, pos);
332
-	//mutex_unlock(jwl->semid);
333 360
 
361
+	DBG("XJAB:xj_wlist_get: can not create a new entry for <%.*s>\n",
362
+				jkey->id->len, jkey->id->s);
334 363
 	return -1;
335 364
 }
336 365
 
... ...
@@ -453,11 +482,11 @@ clean:
453 482
  *   header of the incomming SIP message
454 483
  * - _pid : process id of the worker
455 484
  */
456
-void xj_wlist_del(xj_wlist jwl, str *sid, int _pid)
485
+void xj_wlist_del(xj_wlist jwl, xj_jkey jkey, int _pid)
457 486
 {
458 487
 	int i;
459 488
 	void *p;
460
-	if(jwl == NULL || sid == NULL)
489
+	if(jwl==NULL || jkey==NULL || jkey->id==NULL || jkey->id->s==NULL)
461 490
 		return;
462 491
 	for(i=0; i < jwl->len; i++)
463 492
 		if(jwl->workers[i].pid == _pid)
... ...
@@ -465,94 +494,27 @@ void xj_wlist_del(xj_wlist jwl, str *sid, int _pid)
465 494
 	if(i >= jwl->len)
466 495
 	{
467 496
 		DBG("XJAB:xj_wlist_del:%d: key <%.*s> not found in [%d]...\n",
468
-			_pid, sid->len, sid->s, i);
497
+			_pid, jkey->id->len, jkey->id->s, i);
469 498
 		return;
470 499
 	}
471 500
 	DBG("XJAB:xj_wlist_del:%d: trying to delete entry for <%.*s>...\n",
472
-		_pid, sid->len, sid->s);
501
+		_pid, jkey->id->len, jkey->id->s);
473 502
 
474 503
 	s_lock_at(jwl->sems, i);
475
-	p = del234(jwl->workers[i].sip_ids, (void*)sid);
504
+	p = del234(jwl->workers[i].sip_ids, (void*)jkey);
476 505
 
477 506
 	if(p != NULL)
478 507
 	{
479 508
 		jwl->workers[i].nr--;
480 509
 
481 510
 		DBG("XJAB:xj_wlist_del:%d: sip id <%.*s> deleted\n", _pid,
482
-			sid->len, sid->s);
483
-		free_str_p(p);
511
+			jkey->id->len, jkey->id->s);
512
+		xj_jkey_free_p(p);
484 513
 	}
485 514
 
486 515
 	s_unlock_at(jwl->sems, i);
487 516
 }
488 517
 
489
-/**
490
- * send a SIP MESSAGE message
491
- * - to : destination
492
- * - from : origin
493
- * - contact : contact header
494
- * - msg : body of the message
495
- * #return : 0 on success or <0 on error
496
- */
497
-int xj_send_sip_msg(str *to, str *from, str *contact, str *msg)
498
-{
499
-	str  msg_type = { "MESSAGE", 7};
500
-	char buf[512];
501
-	str  tfrom;
502
-	str  str_hdr;
503
-	char buf1[1024];
504
-
505
-	// from correction
506
-	strcpy(buf, "<sip:");
507
-	strncat(buf, from->s, from->len);
508
-	tfrom.len = from->len;
509
-	if(strstr(buf+5, "sip:") == NULL)
510
-	{
511
-		tfrom.len += 5;
512
-		buf[tfrom.len++] = '>';
513
-		tfrom.s = buf;
514
-	} else
515
-		tfrom.s = buf+5;
516
-	// building Contact and Content-Type
517
-	strcpy(buf1,"Content-Type: text/plain"CRLF"Contact: ");
518
-	str_hdr.len = 24 + CRLF_LEN + 9;
519
-	if(contact != NULL && contact->len > 2) {
520
-		strncat(buf1,contact->s,contact->len);
521
-		str_hdr.len += contact->len;
522
-	} else {
523
-		strncat(buf1,tfrom.s,tfrom.len);
524
-		str_hdr.len += tfrom.len;
525
-	}
526
-	strcat(buf1, CRLF);
527
-	str_hdr.len += CRLF_LEN;
528
-	str_hdr.s = buf1;
529
-
530
-	return tmb.t_uac( &msg_type, to, &str_hdr , msg, &tfrom, 0 , 0, 0);
531
-}
532
-
533
-/**
534
- * send a SIP MESSAGE message
535
- * - to : destination
536
- * - from : origin
537
- * - contact : contact header
538
- * - msg : body of the message, string terminated by zero
539
- * #return : 0 on success or <0 on error
540
- */
541
-int xj_send_sip_msgz(str *to, str *from, str *contact, char *msg)
542
-{
543
-	str tstr;
544
-	int n;
545
-	tstr.s = msg;
546
-	tstr.len = strlen(msg);
547
-	if((n = xj_send_sip_msg(to, from, contact, &tstr)) < 0)
548
-		DBG("JABBER: jab_send_sip_msgz: ERROR SIP MESSAGE wasn't sent to"
549
-			" [%.*s]...\n", tstr.len, tstr.s);
550
-	else
551
-		DBG("JABBER: jab_send_sip_msgz: SIP MESSAGE was sent to [%.*s]...\n",
552
-			tstr.len, tstr.s);
553
-	return n;
554
-}
555
-
556 518
 /**
557 519
  * address corection
558 520
  * alias A~B: flag == 0 => A->B, otherwise B->A
... ...
@@ -589,30 +551,55 @@ int xj_address_translation(str *src, str *dst, xj_jalias als, int flag)
589 551
 	DBG("XJAB:xj_address_translation:%d: - doing address corection\n", 
590 552
 			_xj_pid);	
591 553
 
592
-	if(!flag)
554
+	if(flag & XJ_ADDRTR_A2B)
593 555
 	{
594
-		if(!strncasecmp(p, als->jdm->s, als->jdm->len))
556
+		if(strncasecmp(p, als->jdm->s, als->jdm->len))
557
+		{
558
+			DBG("XJA:xj_address_translation:%d: - wrong Jabber"
559
+				" destination!\n", _xj_pid);
560
+			return -1;
561
+		}
562
+		if(flag & XJ_ADDRTR_CON)
595 563
 		{
596 564
 			DBG("XJAB:xj_address_translation:%d: - that is for"
597
-				" Jabber network\n", _xj_pid);
598
-			dst->len = p - src->s - 1;
599
-			strncpy(dst->s, src->s, dst->len);
565
+				" Jabber conference\n", _xj_pid);
566
+			p0 = p-1;
567
+			while(p0 > src->s && *p0 != '%')
568
+				p0--;
569
+			if(p0 <= src->s)
570
+				return -1;
571
+			p0--;
572
+			while(p0 > src->s && *p0 != '%')
573
+				p0--;
574
+			if(*p0 != '%')
575
+				return -1;
576
+			dst->len = p - p0 - 2;
577
+			strncpy(dst->s, p0+1, dst->len);
600 578
 			dst->s[dst->len]=0;
601
-			if((p = strchr(dst->s, '%')) != NULL)
579
+			p = dst->s;
580
+			while(p < (dst->s + dst->len) && *p!='%')
581
+				p++;
582
+			if(*p=='%')
602 583
 				*p = '@';
603
-			else
604
-			{
605
-				DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
606
-				" destination\n", _xj_pid);
607
-				return -1;
608
-			}
609 584
 			return 0;
610 585
 		}
611
-		DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
612
-			" destination!\n", _xj_pid);
613
-		return -1;		
586
+		
587
+		DBG("XJAB:xj_address_translation:%d: - that is for"
588
+			" Jabber network\n", _xj_pid);
589
+		dst->len = p - src->s - 1;
590
+		strncpy(dst->s, src->s, dst->len);
591
+		dst->s[dst->len]=0;
592
+		if((p = strchr(dst->s, '%')) != NULL)
593
+			*p = '@';
594
+		else
595
+		{
596
+			DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
597
+			" destination\n", _xj_pid);
598
+			return -1;
599
+		}
600
+		return 0;
614 601
 	}
615
-	else
602
+	if(flag & XJ_ADDRTR_B2A)
616 603
 	{
617 604
 		*(p-1) = '%';
618 605
 		p0 = src->s + src->len;
... ...
@@ -653,65 +640,85 @@ done:
653 640
  * #return : 0 on success or <0 on error
654 641
  */
655 642
 int xj_worker_process(xj_wlist jwl, char* jaddress, int jport, int pipe,
656
-		int size, int ctime, int wtime, int dtime, db_con_t* db_con)
643
+		db_con_t* db_con)
657 644
 {
658
-	int ret, i, pos, maxfd;
645
+	int ret, i, pos, maxfd, flag;
659 646
 	xj_jcon_pool jcp;
660 647
 	struct timeval tmv;
661 648
 	fd_set set, mset;
662 649
 	xj_sipmsg jsmsg;
663 650
 	xj_jcon jbc;
651
+	xj_jconf jcf;
664 652
 	char *p, buff[1024], recv_buff[4096];
665 653
 	int flags, nr, ltime = 0;
666 654
 	str sto;
667 655
 	
668
-	db_key_t keys[] = {"sip_id"};
669
-	db_val_t vals[] = {{DB_STRING, 0, {.string_val = buff}}};
656
+	db_key_t keys[] = {"sip_id", "type"};
657
+	db_val_t vals[] = { {DB_STRING, 0, {.string_val = buff}},
658
+						{DB_INT, 0, {.int_val = 0}} };
670 659
 	db_key_t col[] = {"jab_id", "jab_passwd"};
671 660
 	db_res_t* res = NULL;
672 661
 
673 662
 	_xj_pid = getpid();
674 663
 	
664
+	signal(SIGTERM, xj_sig_handler);
665
+	signal(SIGINT, xj_sig_handler);
666
+	signal(SIGQUIT, xj_sig_handler);
667
+	
675 668
 	DBG("XJAB:xj_worker:%d: started - pipe=<%d> : 1st message delay"
676
-		" <%d>\n", _xj_pid, pipe, dtime);
669
+		" <%d>\n", _xj_pid, pipe, jwl->delayt);
677 670
 
678
-	if((jcp = xj_jcon_pool_init(size, 10, dtime)) == NULL)
671
+	if((jcp=xj_jcon_pool_init(jwl->maxj,XJ_POOL_SIZE,jwl->delayt))==NULL)
679 672
 	{
680 673
 		DBG("XJAB:xj_worker: cannot allocate the pool\n");
681 674
 		return -1;
682 675
 	}
683 676
 
684 677
 	maxfd = pipe;
685
-	tmv.tv_sec = wtime;
678
+	tmv.tv_sec = jwl->sleept;
686 679
 	tmv.tv_usec = 0;
687 680
 
688 681
 	FD_ZERO(&set);
689 682
 	FD_SET(pipe, &set);
690
-	while(1)
683
+	while(main_loop)
691 684
 	{
692 685
 		mset = set;