Browse code

XJAB module

Daniel-Constantin Mierla authored on 28/10/2002 11:47:12
Showing 4 changed files
... ...
@@ -76,6 +76,7 @@ char *jaddress = "127.0.0.1";
76 76
 int jport = 5222;
77 77
 
78 78
 char *jaliases = NULL;
79
+char *jdomain  = NULL;
79 80
 
80 81
 int delay_time = 90;
81 82
 int sleep_time = 20;
... ...
@@ -110,6 +111,7 @@ struct module_exports exports= {
110 110
 		"db_url",
111 111
 		"jaddress",
112 112
 		"aliases",
113
+		"jdomain",
113 114
 		"jport",
114 115
 		"workers",
115 116
 		"max_jobs",
... ...
@@ -122,6 +124,7 @@ struct module_exports exports= {
122 122
 		STR_PARAM,
123 123
 		STR_PARAM,
124 124
 		STR_PARAM,
125
+		STR_PARAM,
125 126
 		INT_PARAM,
126 127
 		INT_PARAM,
127 128
 		INT_PARAM,
... ...
@@ -134,6 +137,7 @@ struct module_exports exports= {
134 134
 		&db_url,
135 135
 		&jaddress,
136 136
 		&jaliases,
137
+		&jdomain,
137 138
 		&jport,
138 139
 		&nrw,
139 140
 		&max_jobs,
... ...
@@ -141,7 +145,7 @@ struct module_exports exports= {
141 141
 		&delay_time,
142 142
 		&sleep_time
143 143
 	},
144
-	10,      /* Number of module paramers */
144
+	11,      /* Number of module paramers */
145 145
 	
146 146
 	mod_init,   /* module initialization function */
147 147
 	(response_function) 0,
... ...
@@ -251,7 +255,7 @@ static int mod_init(void)
251 251
 		return -1;
252 252
 	}
253 253
 
254
-	if(xj_wlist_set_aliases(jwl, jaliases) < 0)
254
+	if(xj_wlist_set_aliases(jwl, jaliases, jdomain) < 0)
255 255
 	{
256 256
 		DBG("XJAB:mod_init: error setting aliases\n");
257 257
 		return -1;
... ...
@@ -322,6 +326,8 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
322 322
 	struct to_body to, from;
323 323
 	struct sip_uri _uri;
324 324
 	int pipe, fl;
325
+	struct to_param *foo,*bar;
326
+	char   *cp, *buf=0;
325 327
 
326 328
 	// extract message body - after that whole SIP MESSAGE is parsed
327 329
 	if ( imb.im_extract_body(msg,&body)==-1 )
... ...
@@ -330,25 +336,38 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
330 330
 				" from sip msg!\n");
331 331
 		goto error;
332 332
 	}
333
+
334
+	
333 335
 	// check for FROM header
334
-	if(msg->from != NULL)
336
+	if(!msg->from)
335 337
 	{
336
-		memset( &from , 0, sizeof(from) );
337
-		parse_to(msg->from->body.s, msg->from->body.s + msg->from->body.len+1,
338
-						&from);
339
-		if(from.error == PARSE_OK)
340
-			DBG("XJAB: xjab_send_message: From parsed OK.\n");
341
-		else
342
-		{
343
-			DBG("XJAB: xjab_send_message: From NOT parsed\n");
344
-			goto error;
345
-		}
338
+		LOG(L_ERR,"XJAB: xjab_send_message: cannot find FROM HEADER!\n");
339
+		goto error;
346 340
 	}
347
-	else
341
+	
342
+	/* parsing from header */
343
+	memset(&from,0,sizeof(from));
344
+	cp = translate_pointer(msg->orig,msg->buf,msg->from->body.s);
345
+	buf = (char*)pkg_malloc(msg->from->body.len+1);
346
+	if (!buf) 
347
+	{
348
+		DBG("XJAB: xjab_send_message: error no free pkg memory\n");
349
+		goto error;
350
+	}
351
+	memcpy(buf,cp,msg->from->body.len+1);
352
+	parse_to(buf,buf+msg->from->body.len+1,&from);
353
+	if (from.error!=PARSE_OK ) 
348 354
 	{
349
-		DBG("XJAB: xjab_send_message: cannot find FROM HEADER!\n");
355
+		DBG("XJAB: xjab_send_message: error cannot parse from header\n");
350 356
 		goto error;
351 357
 	}
358
+	/* we are not intrested in from param-> le's free them now*/
359
+	for(foo=from.param_lst ; foo ; foo=bar)
360
+	{
361
+		bar = foo->next;
362
+		pkg_free(foo);
363
+	}
364
+	
352 365
 	// get the communication pipe with the worker
353 366
 	if((pipe = xj_wlist_get(jwl, &from.uri, &p)) < 0)
354 367
 	{
... ...
@@ -460,8 +479,12 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
460 460
 		goto error;
461 461
 	}
462 462
 	
463
+	if (buf) 
464
+		pkg_free(buf);	
463 465
 	return 1;
464 466
 error:
467
+	if (buf) 
468
+		pkg_free(buf);
465 469
 	return -1;
466 470
 }
467 471
 
... ...
@@ -53,17 +53,7 @@
53 53
 
54 54
 #define JB_CLIENT_OPEN_STREAM	"<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
55 55
 
56
-#define JB_IQ_AUTH_REQ			"<iq id='%s%d' type='get'><query xmlns='jabber:iq:auth'><username>%s</username></query></iq>"
57
-
58
-#define JB_IQ_AUTH_PL_SEND		"<iq id='%s%d' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><password>%s</password><resource>%s</resource></query></iq>"
59
-
60
-#define JB_IQ_AUTH_DG_SEND		"<iq id='%s%d' type='set'><query xmlns='jabber:iq:auth'><username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>"
61
-
62
-#define JB_IQ_ROSTER_GET		"<iq type='get'><query xmlns='jabber:iq:roster'/></iq>"
63
-
64
-#define JB_MSG_NORMAL			"<message to='%s' type='normal'><body>%s</body></message>"
65
-
66
-#define JB_MSG_CHAT				"<message to='%s' type='chat'><body>%s</body></message>"
56
+#define JB_IQ_ROSTER_GET	"<iq type='get'><query xmlns='jabber:iq:roster'/></iq>"
67 57
 
68 58
 
69 59
 /**
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- * eXtended JABber module - Jabber connections pool
2
+ * eXtended JABber module - worker implemetation
3 3
  *
4 4
  *
5 5
  * Copyright (C) 2001-2003 Fhg Fokus
... ...
@@ -49,6 +49,8 @@
49 49
 
50 50
 #include "mdefines.h"
51 51
 
52
+#define XJAB_RESOURCE "serXjab"
53
+
52 54
 /** TM bind */
53 55
 extern struct tm_binds tmb;
54 56
 
... ...
@@ -240,15 +242,19 @@ void xj_wlist_free(xj_wlist jwl)
240 240
 		_M_SHM_FREE(jwl->workers);
241 241
 	}
242 242
 
243
-	if(jwl->aliases != NULL && jwl->aliases->size > 0)
243
+	if(jwl->aliases != NULL)
244 244
 	{
245
-		for(i=0; i<jwl->aliases->size; i++)
245
+		if(jwl->aliases->jdm != NULL)
246
+		{
247
+			_M_SHM_FREE(jwl->aliases->jdm->s);
248
+			_M_SHM_FREE(jwl->aliases->jdm);
249
+		}
250
+		if(jwl->aliases->size > 0)
246 251
 		{
247
-			_M_SHM_FREE(jwl->aliases->a[i].s);
248
-			_M_SHM_FREE(jwl->aliases->b[i].s);
252
+			for(i=0; i<jwl->aliases->size; i++)
253
+				_M_SHM_FREE(jwl->aliases->a[i].s);
254
+			_M_SHM_FREE(jwl->aliases->a);
249 255
 		}
250
-		_M_SHM_FREE(jwl->aliases->a);
251
-		_M_SHM_FREE(jwl->aliases->b);
252 256
 		_M_SHM_FREE(jwl->aliases);
253 257
 		jwl->aliases = NULL;
254 258
 	}
... ...
@@ -330,94 +336,113 @@ int xj_wlist_get(xj_wlist jwl, str *sid, str **p)
330 330
 	return -1;
331 331
 }
332 332
 
333
-int  xj_wlist_set_aliases(xj_wlist jwl, char *als)
333
+int  xj_wlist_set_aliases(xj_wlist jwl, char *als, char *jd)
334 334
 {
335
-	char *p, *p0, *p1;
335
+	char *p, *p0;
336 336
 	int i;
337 337
 	
338 338
 	DBG("XJAB:xj_wlist_set_aliases\n");
339 339
 	if(jwl == NULL)
340 340
 		return -1;
341
-	if(als == NULL || strlen(als)<2)
341
+	if(!jd) // || !als || strlen(als)<2)
342 342
 		return 0;
343 343
 	
344
-	if((p = strchr(als, ';')) == NULL)
344
+	if((jwl->aliases = (xj_jalias)_M_SHM_MALLOC(sizeof(t_xj_jalias)))==NULL)
345 345
 	{
346
-		DBG("XJAB:xj_wlist_set_aliases: bad parameter value\n");
346
+		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory.\n");
347 347
 		return -1;
348 348
 	}
349
-	//*p = 0;
350
-	if((jwl->aliases = (xj_jalias)_M_SHM_MALLOC(sizeof(t_xj_jalias)))==NULL)
349
+	
350
+	jwl->aliases->jdm = NULL;
351
+	jwl->aliases->size = 0;
352
+	jwl->aliases->a = NULL;
353
+	
354
+	if(jd != NULL && strlen(jd)>2)
351 355
 	{
352
-		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory.\n");
356
+		if((jwl->aliases->jdm = (str*)_M_SHM_MALLOC(sizeof(str)))== NULL)
357
+		{
358
+			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!?\n");
359
+			_M_SHM_FREE(jwl->aliases);
360
+			jwl->aliases = NULL;
361
+			return -1;		
362
+		}
363
+		jwl->aliases->jdm->len = strlen(jd);
364
+		if((jwl->aliases->jdm->s=(char*)_M_SHM_MALLOC(jwl->aliases->jdm->len))
365
+				== NULL)
366
+		{
367
+			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!?!\n");
368
+			_M_SHM_FREE(jwl->aliases->jdm);
369
+			_M_SHM_FREE(jwl->aliases);
370
+			jwl->aliases = NULL;
371
+		}
372
+		strncpy(jwl->aliases->jdm->s, jd, jwl->aliases->jdm->len);
373
+	}
374
+	
375
+	if(!als || strlen(als)<2)
376
+		return 0;
377
+	
378
+	if((p = strchr(als, ';')) == NULL)
379
+	{
380
+		DBG("XJAB:xj_wlist_set_aliases: bad parameter value\n");
353 381
 		return -1;
354 382
 	}
383
+	
355 384
 	if((jwl->aliases->size = atoi(als)) <= 0)
356 385
 	{
357 386
 		DBG("XJAB:xj_wlist_set_aliases: wrong number of aliases\n");
358
-		_M_SHM_FREE(jwl->aliases);
359
-		jwl->aliases = NULL;
360
-		return -1;
387
+		return 0;
361 388
 	}
389
+	
362 390
 	if((jwl->aliases->a = (str*)_M_SHM_MALLOC(jwl->aliases->size*sizeof(str)))
363 391
 			== NULL)
364 392
 	{
365 393
 		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory..\n");
394
+		if(jwl->aliases->jdm)
395
+		{
396
+			_M_SHM_FREE(jwl->aliases->jdm->s);
397
+			_M_SHM_FREE(jwl->aliases->jdm);
398
+		}
366 399
 		_M_SHM_FREE(jwl->aliases);
367 400
 		jwl->aliases = NULL;
368 401
 		return -1;
369 402
 	}
370
-	if((jwl->aliases->b = (str*)_M_SHM_MALLOC(jwl->aliases->size*sizeof(str)))
371
-			== NULL)
372
-	{
373
-		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory...\n");
374
-		_M_SHM_FREE(jwl->aliases->a);
375
-		_M_SHM_FREE(jwl->aliases);
376
-		jwl->aliases = NULL;
377
-		return -1;
378
-	}
403
+	
379 404
 	p++;
380 405
 	for(i=0; i<jwl->aliases->size; i++)
381 406
 	{
382
-		if((p0 = strchr(p, ';'))==NULL || (p1 = strchr(p, '='))==NULL
383
-				|| p1 >= p0)
407
+		if((p0 = strchr(p, ';'))==NULL)
384 408
 		{
385 409
 			DBG("XJAB:xj_wlist_set_aliases: bad parameter value format\n");
386 410
 			goto clean;
387 411
 		}
388
-		jwl->aliases->a[i].len = p1 - p;
412
+		jwl->aliases->a[i].len = p0 - p;
389 413
 		if((jwl->aliases->a[i].s = (char*)_M_SHM_MALLOC(jwl->aliases->a[i].len))
390 414
 				== NULL)
391 415
 		{
392 416
 			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!\n");
393 417
 			goto clean;
394 418
 		}
395
-	
396
-		p1++;
397
-		jwl->aliases->b[i].len = p0 - p1;
398
-		if((jwl->aliases->b[i].s = (char*)_M_SHM_MALLOC(jwl->aliases->b[i].len))
399
-				== NULL)
400
-		{
401
-			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!!\n");
402
-			_M_SHM_FREE(jwl->aliases->a[i].s);
403
-			goto clean;
404
-		}
405
-
419
+			
406 420
 		strncpy(jwl->aliases->a[i].s, p, jwl->aliases->a[i].len);
407
-		strncpy(jwl->aliases->b[i].s, p1, jwl->aliases->b[i].len);
421
+		DBG("XJAB:xj_wlist_set_aliases: alias[%d/%d]=%.*s\n", 
422
+				i+1, jwl->aliases->size, jwl->aliases->a[i].len, 
423
+				jwl->aliases->a[i].s);
408 424
 		p = p0 + 1;
409 425
 	}
410 426
 	return 0;
411 427
 
412 428
 clean:
429
+	if(jwl->aliases->jdm)
430
+	{
431
+		_M_SHM_FREE(jwl->aliases->jdm->s);
432
+		_M_SHM_FREE(jwl->aliases->jdm);
433
+	}
413 434
 	while(i>0)
414 435
 	{
415 436
 		_M_SHM_FREE(jwl->aliases->a[i-1].s);
416
-		_M_SHM_FREE(jwl->aliases->b[i-1].s);
417 437
 		i--;
418 438
 	}
419 439
 	_M_SHM_FREE(jwl->aliases->a);
420
-	_M_SHM_FREE(jwl->aliases->b);
421 440
 	_M_SHM_FREE(jwl->aliases);
422 441
 	jwl->aliases = NULL;
423 442
 	return -1;
... ...
@@ -536,63 +561,84 @@ int xj_send_sip_msgz(str *to, str *from, str *contact, char *msg)
536 536
  */
537 537
 int xj_address_translation(str *src, str *dst, xj_jalias als, int flag)
538 538
 {
539
-	char *p;
539
+	char *p, *p0;
540 540
 	int i;
541
-	str *x, *y;
542 541
 	
543 542
 	if(!src || !dst || !src->s || !dst->s )
544 543
 		return -1; 
545 544
 	
546
-	DBG("XJAB:xj_address_translation:%d: - checking alias\n", 
547
-			_xj_pid);	
548
-	
549
-	if(!als || als->size <= 0)
550
-	{
551
-		dst->s = src->s;
552
-		dst->len = src->len;
553
-		return 0;
554
-	}
545
+	if(!als || !als->jdm || !als->jdm->s || als->jdm->len <= 0)
546
+		goto done;
555 547
 	
556 548
 	dst->len = 0;
557
-	
549
+	DBG("XJAB:xj_address_translation:%d: - checking aliases\n", 
550
+			_xj_pid);
558 551
 	p = src->s;
552
+
559 553
 	while(p<(src->s + src->len)	&& *p != '@') 
560 554
 		p++;
561
-	if(*p == '@')
562
-	{
563
-		p++;
555
+	if(*p != '@')
556
+		goto done;
557
+
558
+	p++;
559
+	
560
+	/*** checking aliases */
561
+	if(als->size > 0)
564 562
 		for(i=0; i<als->size; i++)
563
+			if(!strncasecmp(p, als->a[i].s, als->a[i].len))
564
+				goto done;
565
+	
566
+	DBG("XJAB:xj_address_translation:%d: - doing address corection\n", 
567
+			_xj_pid);	
568
+
569
+	if(!flag)
570
+	{
571
+		if(!strncasecmp(p, als->jdm->s, als->jdm->len))
565 572
 		{
566
-			if(!flag)
567
-			{
568
-				x = &als->a[i];
569
-				y = &als->b[i];
570
-			}
573
+			DBG("XJAB:xj_address_translation:%d: - that is for"
574
+				" Jabber network\n", _xj_pid);
575
+			dst->len = p - src->s - 1;
576
+			strncpy(dst->s, src->s, dst->len);
577
+			dst->s[dst->len]=0;
578
+			if((p = strchr(dst->s, '%')) != NULL)
579
+				*p = '@';
571 580
 			else
572 581
 			{
573
-				x = &als->b[i];
574
-				y = &als->a[i];
575
-			}
576
-	
577
-			if(!strncasecmp(p, x->s, x->len))
578
-			{
579
-				DBG("XJAB:xj_address_translation:%d: - alias corection\n", 
580
-						_xj_pid);
581
-				strncpy(dst->s, src->s, p - src->s);
582
-				dst->s[p - src->s]=0;
583
-				strncat(dst->s, y->s, y->len);
584
-				dst->len  = strlen(dst->s);
585
-				break;
582
+				DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
583
+				" destination\n", _xj_pid);
584
+				return -1;
586 585
 			}
586
+			return 0;
587 587
 		}
588
+		DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
589
+			" destination!\n", _xj_pid);
590
+		return -1;		
588 591
 	}
589
-	
590
-	if(dst->len <= 0)
592
+	else
591 593
 	{
592
-		dst->s = src->s;
593
-		dst->len = src->len;
594
+		*(p-1) = '%';
595
+		p0 = src->s + src->len;
596
+		while(p0 > p)
597
+		{
598
+			if(*p0 == '/')
599
+			{
600
+				src->len = p0 - src->s;
601
+				*p0 = 0;
602
+			}
603
+			p0--;
604
+		}
605
+		strncpy(dst->s, src->s, src->len);
606
+		dst->s[src->len] = '@';
607
+		dst->s[src->len+1] = 0;
608
+		strncat(dst->s, als->jdm->s, als->jdm->len);
609
+		dst->len = strlen(dst->s);
610
+		return 0;
594 611
 	}
595
-	return 0;
612
+
613
+done:
614
+	dst->s = src->s;
615
+	dst->len = src->len;
616
+	return 0;	
596 617
 }
597 618
 
598 619
 /**
... ...
@@ -648,7 +694,8 @@ int xj_worker_process(xj_wlist jwl, char* jaddress, int jport, int pipe,
648 648
 		mset = set;
649 649
 
650 650
 		tmv.tv_sec = (jcp->jmqueue.size == 0)?wtime:1;
651
-		DBG("XJAB:xj_worker:%d: select waiting %ds\n",_xj_pid,(int)tmv.tv_sec);
651
+		DBG("XJAB:xj_worker:%d: select waiting %ds - queue=%d\n",_xj_pid,
652
+				(int)tmv.tv_sec, jcp->jmqueue.size);
652 653
 		tmv.tv_usec = 0;
653 654
 
654 655
 		ret = select(maxfd+1, &mset, NULL, NULL, &tmv);
... ...
@@ -762,7 +809,7 @@ int xj_worker_process(xj_wlist jwl, char* jaddress, int jport, int pipe,
762 762
 		if(xj_jcon_user_auth(jbc,
763 763
 			(char*)(ROW_VALUES(RES_ROWS(res))[0].val.string_val),
764 764
 			(char*)(ROW_VALUES(RES_ROWS(res))[1].val.string_val),
765
-			"jbcl") < 0)
765
+			XJAB_RESOURCE) < 0)
766 766
 		{
767 767
 			DBG("XJAB:xj_worker:%d: Authentication to the Jabber server"
768 768
 				" failed ...\n", _xj_pid);
... ...
@@ -48,8 +48,8 @@
48 48
 typedef struct _xj_jalias
49 49
 {
50 50
 	int size;
51
+	str *jdm;
51 52
 	str *a;
52
-	str *b;
53 53
 } t_xj_jalias, *xj_jalias;
54 54
 
55 55
 typedef struct _xj_worker
... ...
@@ -79,7 +79,7 @@ int  xj_wlist_set_pids(xj_wlist, int *, int);
79 79
 int  xj_wlist_get(xj_wlist, str *, str **);
80 80
 void xj_wlist_del(xj_wlist, str *, int);
81 81
 void xj_wlist_free(xj_wlist);
82
-int  xj_wlist_set_aliases(xj_wlist, char *);
82
+int  xj_wlist_set_aliases(xj_wlist, char *, char *);
83 83
  
84 84
 int xj_worker_process(xj_wlist, char*, int, int, int, int, int, int, db_con_t*);
85 85