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 111
 		"db_url",
111 112
 		"jaddress",
112 113
 		"aliases",
114
+		"jdomain",
113 115
 		"jport",
114 116
 		"workers",
115 117
 		"max_jobs",
... ...
@@ -122,6 +124,7 @@ struct module_exports exports= {
122 124
 		STR_PARAM,
123 125
 		STR_PARAM,
124 126
 		STR_PARAM,
127
+		STR_PARAM,
125 128
 		INT_PARAM,
126 129
 		INT_PARAM,
127 130
 		INT_PARAM,
... ...
@@ -134,6 +137,7 @@ struct module_exports exports= {
134 137
 		&db_url,
135 138
 		&jaddress,
136 139
 		&jaliases,
140
+		&jdomain,
137 141
 		&jport,
138 142
 		&nrw,
139 143
 		&max_jobs,
... ...
@@ -141,7 +145,7 @@ struct module_exports exports= {
141 145
 		&delay_time,
142 146
 		&sleep_time
143 147
 	},
144
-	10,      /* Number of module paramers */
148
+	11,      /* Number of module paramers */
145 149
 	
146 150
 	mod_init,   /* module initialization function */
147 151
 	(response_function) 0,
... ...
@@ -251,7 +255,7 @@ static int mod_init(void)
251 255
 		return -1;
252 256
 	}
253 257
 
254
-	if(xj_wlist_set_aliases(jwl, jaliases) < 0)
258
+	if(xj_wlist_set_aliases(jwl, jaliases, jdomain) < 0)
255 259
 	{
256 260
 		DBG("XJAB:mod_init: error setting aliases\n");
257 261
 		return -1;
... ...
@@ -322,6 +326,8 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
322 326
 	struct to_body to, from;
323 327
 	struct sip_uri _uri;
324 328
 	int pipe, fl;
329
+	struct to_param *foo,*bar;
330
+	char   *cp, *buf=0;
325 331
 
326 332
 	// extract message body - after that whole SIP MESSAGE is parsed
327 333
 	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 336
 				" from sip msg!\n");
331 337
 		goto error;
332 338
 	}
339
+
340
+	
333 341
 	// check for FROM header
334
-	if(msg->from != NULL)
342
+	if(!msg->from)
335 343
 	{
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
-		}
344
+		LOG(L_ERR,"XJAB: xjab_send_message: cannot find FROM HEADER!\n");
345
+		goto error;
346 346
 	}
347
-	else
347
+	
348
+	/* parsing from header */
349
+	memset(&from,0,sizeof(from));
350
+	cp = translate_pointer(msg->orig,msg->buf,msg->from->body.s);
351
+	buf = (char*)pkg_malloc(msg->from->body.len+1);
352
+	if (!buf) 
353
+	{
354
+		DBG("XJAB: xjab_send_message: error no free pkg memory\n");
355
+		goto error;
356
+	}
357
+	memcpy(buf,cp,msg->from->body.len+1);
358
+	parse_to(buf,buf+msg->from->body.len+1,&from);
359
+	if (from.error!=PARSE_OK ) 
348 360
 	{
349
-		DBG("XJAB: xjab_send_message: cannot find FROM HEADER!\n");
361
+		DBG("XJAB: xjab_send_message: error cannot parse from header\n");
350 362
 		goto error;
351 363
 	}
364
+	/* we are not intrested in from param-> le's free them now*/
365
+	for(foo=from.param_lst ; foo ; foo=bar)
366
+	{
367
+		bar = foo->next;
368
+		pkg_free(foo);
369
+	}
370
+	
352 371
 	// get the communication pipe with the worker
353 372
 	if((pipe = xj_wlist_get(jwl, &from.uri, &p)) < 0)
354 373
 	{
... ...
@@ -460,8 +479,12 @@ static int jab_send_message(struct sip_msg *msg, char* foo1, char * foo2)
460 479
 		goto error;
461 480
 	}
462 481
 	
482
+	if (buf) 
483
+		pkg_free(buf);	
463 484
 	return 1;
464 485
 error:
486
+	if (buf) 
487
+		pkg_free(buf);
465 488
 	return -1;
466 489
 }
467 490
 
... ...
@@ -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 242
 		_M_SHM_FREE(jwl->workers);
241 243
 	}
242 244
 
243
-	if(jwl->aliases != NULL && jwl->aliases->size > 0)
245
+	if(jwl->aliases != NULL)
244 246
 	{
245
-		for(i=0; i<jwl->aliases->size; i++)
247
+		if(jwl->aliases->jdm != NULL)
248
+		{
249
+			_M_SHM_FREE(jwl->aliases->jdm->s);
250
+			_M_SHM_FREE(jwl->aliases->jdm);
251
+		}
252
+		if(jwl->aliases->size > 0)
246 253
 		{
247
-			_M_SHM_FREE(jwl->aliases->a[i].s);
248
-			_M_SHM_FREE(jwl->aliases->b[i].s);
254
+			for(i=0; i<jwl->aliases->size; i++)
255
+				_M_SHM_FREE(jwl->aliases->a[i].s);
256
+			_M_SHM_FREE(jwl->aliases->a);
249 257
 		}
250
-		_M_SHM_FREE(jwl->aliases->a);
251
-		_M_SHM_FREE(jwl->aliases->b);
252 258
 		_M_SHM_FREE(jwl->aliases);
253 259
 		jwl->aliases = NULL;
254 260
 	}
... ...
@@ -330,94 +336,113 @@ int xj_wlist_get(xj_wlist jwl, str *sid, str **p)
330 336
 	return -1;
331 337
 }
332 338
 
333
-int  xj_wlist_set_aliases(xj_wlist jwl, char *als)
339
+int  xj_wlist_set_aliases(xj_wlist jwl, char *als, char *jd)
334 340
 {
335
-	char *p, *p0, *p1;
341
+	char *p, *p0;
336 342
 	int i;
337 343
 	
338 344
 	DBG("XJAB:xj_wlist_set_aliases\n");
339 345
 	if(jwl == NULL)
340 346
 		return -1;
341
-	if(als == NULL || strlen(als)<2)
347
+	if(!jd) // || !als || strlen(als)<2)
342 348
 		return 0;
343 349
 	
344
-	if((p = strchr(als, ';')) == NULL)
350
+	if((jwl->aliases = (xj_jalias)_M_SHM_MALLOC(sizeof(t_xj_jalias)))==NULL)
345 351
 	{
346
-		DBG("XJAB:xj_wlist_set_aliases: bad parameter value\n");
352
+		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory.\n");
347 353
 		return -1;
348 354
 	}
349
-	//*p = 0;
350
-	if((jwl->aliases = (xj_jalias)_M_SHM_MALLOC(sizeof(t_xj_jalias)))==NULL)
355
+	
356
+	jwl->aliases->jdm = NULL;
357
+	jwl->aliases->size = 0;
358
+	jwl->aliases->a = NULL;
359
+	
360
+	if(jd != NULL && strlen(jd)>2)
351 361
 	{
352
-		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory.\n");
362
+		if((jwl->aliases->jdm = (str*)_M_SHM_MALLOC(sizeof(str)))== NULL)
363
+		{
364
+			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!?\n");
365
+			_M_SHM_FREE(jwl->aliases);
366
+			jwl->aliases = NULL;
367
+			return -1;		
368
+		}
369
+		jwl->aliases->jdm->len = strlen(jd);
370
+		if((jwl->aliases->jdm->s=(char*)_M_SHM_MALLOC(jwl->aliases->jdm->len))
371
+				== NULL)
372
+		{
373
+			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!?!\n");
374
+			_M_SHM_FREE(jwl->aliases->jdm);
375
+			_M_SHM_FREE(jwl->aliases);
376
+			jwl->aliases = NULL;
377
+		}
378
+		strncpy(jwl->aliases->jdm->s, jd, jwl->aliases->jdm->len);
379
+	}
380
+	
381
+	if(!als || strlen(als)<2)
382
+		return 0;
383
+	
384
+	if((p = strchr(als, ';')) == NULL)
385
+	{
386
+		DBG("XJAB:xj_wlist_set_aliases: bad parameter value\n");
353 387
 		return -1;
354 388
 	}
389
+	
355 390
 	if((jwl->aliases->size = atoi(als)) <= 0)
356 391
 	{
357 392
 		DBG("XJAB:xj_wlist_set_aliases: wrong number of aliases\n");
358
-		_M_SHM_FREE(jwl->aliases);
359
-		jwl->aliases = NULL;
360
-		return -1;
393
+		return 0;
361 394
 	}
395
+	
362 396
 	if((jwl->aliases->a = (str*)_M_SHM_MALLOC(jwl->aliases->size*sizeof(str)))
363 397
 			== NULL)
364 398
 	{
365 399
 		DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory..\n");
400
+		if(jwl->aliases->jdm)
401
+		{
402
+			_M_SHM_FREE(jwl->aliases->jdm->s);
403
+			_M_SHM_FREE(jwl->aliases->jdm);
404
+		}
366 405
 		_M_SHM_FREE(jwl->aliases);
367 406
 		jwl->aliases = NULL;
368 407
 		return -1;
369 408
 	}
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
-	}
409
+	
379 410
 	p++;
380 411
 	for(i=0; i<jwl->aliases->size; i++)
381 412
 	{
382
-		if((p0 = strchr(p, ';'))==NULL || (p1 = strchr(p, '='))==NULL
383
-				|| p1 >= p0)
413
+		if((p0 = strchr(p, ';'))==NULL)
384 414
 		{
385 415
 			DBG("XJAB:xj_wlist_set_aliases: bad parameter value format\n");
386 416
 			goto clean;
387 417
 		}
388
-		jwl->aliases->a[i].len = p1 - p;
418
+		jwl->aliases->a[i].len = p0 - p;
389 419
 		if((jwl->aliases->a[i].s = (char*)_M_SHM_MALLOC(jwl->aliases->a[i].len))
390 420
 				== NULL)
391 421
 		{
392 422
 			DBG("XJAB:xj_wlist_set_aliases: not enough SHMemory!\n");
393 423
 			goto clean;
394 424
 		}
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
-
425
+			
406 426
 		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);
427
+		DBG("XJAB:xj_wlist_set_aliases: alias[%d/%d]=%.*s\n", 
428
+				i+1, jwl->aliases->size, jwl->aliases->a[i].len, 
429
+				jwl->aliases->a[i].s);
408 430
 		p = p0 + 1;
409 431
 	}
410 432
 	return 0;
411 433
 
412 434
 clean:
435
+	if(jwl->aliases->jdm)
436
+	{
437
+		_M_SHM_FREE(jwl->aliases->jdm->s);
438
+		_M_SHM_FREE(jwl->aliases->jdm);
439
+	}
413 440
 	while(i>0)
414 441
 	{
415 442
 		_M_SHM_FREE(jwl->aliases->a[i-1].s);
416
-		_M_SHM_FREE(jwl->aliases->b[i-1].s);
417 443
 		i--;
418 444
 	}
419 445
 	_M_SHM_FREE(jwl->aliases->a);
420
-	_M_SHM_FREE(jwl->aliases->b);
421 446
 	_M_SHM_FREE(jwl->aliases);
422 447
 	jwl->aliases = NULL;
423 448
 	return -1;
... ...
@@ -536,63 +561,84 @@ int xj_send_sip_msgz(str *to, str *from, str *contact, char *msg)
536 561
  */
537 562
 int xj_address_translation(str *src, str *dst, xj_jalias als, int flag)
538 563
 {
539
-	char *p;
564
+	char *p, *p0;
540 565
 	int i;
541
-	str *x, *y;
542 566
 	
543 567
 	if(!src || !dst || !src->s || !dst->s )
544 568
 		return -1; 
545 569
 	
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
-	}
570
+	if(!als || !als->jdm || !als->jdm->s || als->jdm->len <= 0)
571
+		goto done;
555 572
 	
556 573
 	dst->len = 0;
557
-	
574
+	DBG("XJAB:xj_address_translation:%d: - checking aliases\n", 
575
+			_xj_pid);
558 576
 	p = src->s;
577
+
559 578
 	while(p<(src->s + src->len)	&& *p != '@') 
560 579
 		p++;
561
-	if(*p == '@')
562
-	{
563
-		p++;
580
+	if(*p != '@')
581
+		goto done;
582
+
583
+	p++;
584
+	
585
+	/*** checking aliases */
586
+	if(als->size > 0)
564 587
 		for(i=0; i<als->size; i++)
588
+			if(!strncasecmp(p, als->a[i].s, als->a[i].len))
589
+				goto done;
590
+	
591
+	DBG("XJAB:xj_address_translation:%d: - doing address corection\n", 
592
+			_xj_pid);	
593
+
594
+	if(!flag)
595
+	{
596
+		if(!strncasecmp(p, als->jdm->s, als->jdm->len))
565 597
 		{
566
-			if(!flag)
567
-			{
568
-				x = &als->a[i];
569
-				y = &als->b[i];
570
-			}
598
+			DBG("XJAB:xj_address_translation:%d: - that is for"
599
+				" Jabber network\n", _xj_pid);
600
+			dst->len = p - src->s - 1;
601
+			strncpy(dst->s, src->s, dst->len);
602
+			dst->s[dst->len]=0;
603
+			if((p = strchr(dst->s, '%')) != NULL)
604
+				*p = '@';
571 605
 			else
572 606
 			{
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;
607
+				DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
608
+				" destination\n", _xj_pid);
609
+				return -1;
586 610
 			}
611
+			return 0;
587 612
 		}
613
+		DBG("XJAB:xj_address_translation:%d: - wrong Jabber"
614
+			" destination!\n", _xj_pid);
615
+		return -1;		
588 616
 	}
589
-	
590
-	if(dst->len <= 0)
617
+	else
591 618
 	{
592
-		dst->s = src->s;
593
-		dst->len = src->len;
619
+		*(p-1) = '%';
620
+		p0 = src->s + src->len;
621
+		while(p0 > p)
622
+		{
623
+			if(*p0 == '/')
624
+			{
625
+				src->len = p0 - src->s;
626
+				*p0 = 0;
627
+			}
628
+			p0--;
629
+		}
630
+		strncpy(dst->s, src->s, src->len);
631
+		dst->s[src->len] = '@';
632
+		dst->s[src->len+1] = 0;
633
+		strncat(dst->s, als->jdm->s, als->jdm->len);
634
+		dst->len = strlen(dst->s);
635
+		return 0;
594 636
 	}
595
-	return 0;
637
+
638
+done:
639
+	dst->s = src->s;
640
+	dst->len = src->len;
641
+	return 0;	
596 642
 }
597 643
 
598 644
 /**
... ...
@@ -648,7 +694,8 @@ int xj_worker_process(xj_wlist jwl, char* jaddress, int jport, int pipe,
648 694
 		mset = set;
649 695
 
650 696
 		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);
697
+		DBG("XJAB:xj_worker:%d: select waiting %ds - queue=%d\n",_xj_pid,
698
+				(int)tmv.tv_sec, jcp->jmqueue.size);
652 699
 		tmv.tv_usec = 0;
653 700
 
654 701
 		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 809
 		if(xj_jcon_user_auth(jbc,
763 810
 			(char*)(ROW_VALUES(RES_ROWS(res))[0].val.string_val),
764 811
 			(char*)(ROW_VALUES(RES_ROWS(res))[1].val.string_val),
765
-			"jbcl") < 0)
812
+			XJAB_RESOURCE) < 0)
766 813
 		{
767 814
 			DBG("XJAB:xj_worker:%d: Authentication to the Jabber server"
768 815
 				" 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