Przeglądaj źródła

xmpp: clang-format for coherent indentation and coding style

Victor Seva 2 lat temu
rodzic
commit
709163fa7e

+ 33 - 35
src/modules/xmpp/network.c

@@ -45,42 +45,41 @@ int net_listen(char *server, int port)
 	int fd;
 	int fd;
 	struct sockaddr_in sin;
 	struct sockaddr_in sin;
 	int on = 1;
 	int on = 1;
-	
+
 	memset(&sin, 0, sizeof(struct sockaddr_in));
 	memset(&sin, 0, sizeof(struct sockaddr_in));
 	sin.sin_family = AF_INET;
 	sin.sin_family = AF_INET;
 	sin.sin_port = htons(port);
 	sin.sin_port = htons(port);
-	
-	if (!inet_aton(server, &sin.sin_addr)) {
+
+	if(!inet_aton(server, &sin.sin_addr)) {
 		struct hostent *host;
 		struct hostent *host;
-		
+
 		LM_DBG("resolving %s...\n", server);
 		LM_DBG("resolving %s...\n", server);
-		
-		if (!(host = gethostbyname(server))) {
-			LM_ERR("resolving %s failed (%s).\n", server,
-					hstrerror(h_errno));
+
+		if(!(host = gethostbyname(server))) {
+			LM_ERR("resolving %s failed (%s).\n", server, hstrerror(h_errno));
 			return -1;
 			return -1;
 		}
 		}
 		memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
 		memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
 	}
 	}
-	
-	if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+
+	if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
 		LM_ERR("socket() failed: %s\n", strerror(errno));
 		LM_ERR("socket() failed: %s\n", strerror(errno));
 		return -1;
 		return -1;
 	}
 	}
-	
+
 	LM_DBG("listening on %s:%d\n", inet_ntoa(sin.sin_addr), port);
 	LM_DBG("listening on %s:%d\n", inet_ntoa(sin.sin_addr), port);
-	
-	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
-		LM_WARN("setsockopt(SO_REUSEADDR) failed: %s\n",strerror(errno));
+
+	if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
+		LM_WARN("setsockopt(SO_REUSEADDR) failed: %s\n", strerror(errno));
 	}
 	}
 
 
-	if (bind(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) {
+	if(bind(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
 		LM_ERR("bind() failed: %s\n", strerror(errno));
 		LM_ERR("bind() failed: %s\n", strerror(errno));
 		close(fd);
 		close(fd);
 		return -1;
 		return -1;
 	}
 	}
 
 
-	if (listen(fd, 1) < 0) {
+	if(listen(fd, 1) < 0) {
 		LM_ERR("listen() failed: %s\n", strerror(errno));
 		LM_ERR("listen() failed: %s\n", strerror(errno));
 		close(fd);
 		close(fd);
 		return -1;
 		return -1;
@@ -93,32 +92,31 @@ int net_connect(char *server, int port)
 {
 {
 	int fd;
 	int fd;
 	struct sockaddr_in sin;
 	struct sockaddr_in sin;
-	
+
 	memset(&sin, 0, sizeof(struct sockaddr_in));
 	memset(&sin, 0, sizeof(struct sockaddr_in));
 	sin.sin_family = AF_INET;
 	sin.sin_family = AF_INET;
 	sin.sin_port = htons(port);
 	sin.sin_port = htons(port);
-	
-	if (!inet_aton(server, &sin.sin_addr)) {
+
+	if(!inet_aton(server, &sin.sin_addr)) {
 		struct hostent *host;
 		struct hostent *host;
-		
+
 		LM_DBG("resolving %s...\n", server);
 		LM_DBG("resolving %s...\n", server);
-		
-		if (!(host = gethostbyname(server))) {
-			LM_ERR("resolving %s failed (%s).\n", server,
-					hstrerror(h_errno));
+
+		if(!(host = gethostbyname(server))) {
+			LM_ERR("resolving %s failed (%s).\n", server, hstrerror(h_errno));
 			return -1;
 			return -1;
 		}
 		}
 		memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
 		memcpy(&sin.sin_addr, host->h_addr_list[0], host->h_length);
 	}
 	}
-	
-	if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+
+	if((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
 		LM_ERR("socket() failed: %s\n", strerror(errno));
 		LM_ERR("socket() failed: %s\n", strerror(errno));
 		return -1;
 		return -1;
 	}
 	}
-	
+
 	LM_DBG("connecting to %s:%d...\n", inet_ntoa(sin.sin_addr), port);
 	LM_DBG("connecting to %s:%d...\n", inet_ntoa(sin.sin_addr), port);
-	
-	if (connect(fd, (struct sockaddr *) &sin, sizeof(struct sockaddr_in)) < 0) {
+
+	if(connect(fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) < 0) {
 		LM_ERR("connect() failed: %s\n", strerror(errno));
 		LM_ERR("connect() failed: %s\n", strerror(errno));
 		close(fd);
 		close(fd);
 		return -1;
 		return -1;
@@ -135,11 +133,11 @@ int net_send(int fd, const char *buf, int len)
 
 
 	do {
 	do {
 		res = send(fd, p, len, 0);
 		res = send(fd, p, len, 0);
-		if (res <= 0)
+		if(res <= 0)
 			return res;
 			return res;
 		len -= res;
 		len -= res;
 		p += res;
 		p += res;
-	} while (len);
+	} while(len);
 
 
 	return (p - buf);
 	return (p - buf);
 }
 }
@@ -148,13 +146,13 @@ int net_printf(int fd, char *format, ...)
 {
 {
 	va_list args;
 	va_list args;
 	char buf[4096];
 	char buf[4096];
-	
+
 	va_start(args, format);
 	va_start(args, format);
 	vsnprintf(buf, sizeof(buf) - 1, format, args);
 	vsnprintf(buf, sizeof(buf) - 1, format, args);
 	va_end(args);
 	va_end(args);
 
 
 	LM_DBG("net_printf: [%s]\n", buf);
 	LM_DBG("net_printf: [%s]\n", buf);
-	
+
 	return net_send(fd, buf, strlen(buf));
 	return net_send(fd, buf, strlen(buf));
 }
 }
 
 
@@ -164,11 +162,11 @@ char *net_read_static(int fd)
 	int res;
 	int res;
 
 
 	res = recv(fd, buf, sizeof(buf) - 1, 0);
 	res = recv(fd, buf, sizeof(buf) - 1, 0);
-	if (res < 0) {
+	if(res < 0) {
 		LM_ERR("recv() failed: %s\n", strerror(errno));
 		LM_ERR("recv() failed: %s\n", strerror(errno));
 		return NULL;
 		return NULL;
 	}
 	}
-	if (!res)
+	if(!res)
 		return NULL;
 		return NULL;
 	buf[res] = 0;
 	buf[res] = 0;
 	return buf;
 	return buf;

+ 2 - 1
src/modules/xmpp/network.h

@@ -31,7 +31,8 @@
 extern int net_listen(char *server, int port);
 extern int net_listen(char *server, int port);
 extern int net_connect(char *server, int port);
 extern int net_connect(char *server, int port);
 extern int net_send(int fd, const char *buf, int len);
 extern int net_send(int fd, const char *buf, int len);
-extern int net_printf(int fd, char *format, ...) __attribute__ ((format (printf, 2, 3)));
+extern int net_printf(int fd, char *format, ...)
+		__attribute__((format(printf, 2, 3)));
 extern char *net_read_chunk(int fd, int size);
 extern char *net_read_chunk(int fd, int size);
 extern char *net_read_static(int fd);
 extern char *net_read_static(int fd);
 
 

+ 110 - 124
src/modules/xmpp/sha.c

@@ -52,24 +52,23 @@
 #include "../../core/endianness.h"
 #include "../../core/endianness.h"
 
 
 #ifndef MACOS
 #ifndef MACOS
-#  include <sys/stat.h>
-#  include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
 #endif
 #endif
 #ifndef WIN32
 #ifndef WIN32
-#  include <unistd.h>
-#  define INT64 long long
+#include <unistd.h>
+#define INT64 long long
 #else
 #else
-#  define snprintf _snprintf
-#  define INT64 __int64
+#define snprintf _snprintf
+#define INT64 __int64
 #endif
 #endif
 
 
-#define switch_endianness(x) (x<<24 & 0xff000000) | \
-                             (x<<8  & 0x00ff0000) | \
-                             (x>>8  & 0x0000ff00) | \
-                             (x>>24 & 0x000000ff)
+#define switch_endianness(x)                                               \
+	(x << 24 & 0xff000000) | (x << 8 & 0x00ff0000) | (x >> 8 & 0x0000ff00) \
+			| (x >> 24 & 0x000000ff)
 
 
 /* Initial hash values */
 /* Initial hash values */
-#define Ai 0x67452301 
+#define Ai 0x67452301
 #define Bi 0xefcdab89
 #define Bi 0xefcdab89
 #define Ci 0x98badcfe
 #define Ci 0x98badcfe
 #define Di 0x10325476
 #define Di 0x10325476
@@ -78,93 +77,87 @@
 /* SHA1 round constants */
 /* SHA1 round constants */
 #define K1 0x5a827999
 #define K1 0x5a827999
 #define K2 0x6ed9eba1
 #define K2 0x6ed9eba1
-#define K3 0x8f1bbcdc 
+#define K3 0x8f1bbcdc
 #define K4 0xca62c1d6
 #define K4 0xca62c1d6
 
 
 /* Round functions.  Note that f2() is used in both rounds 2 and 4 */
 /* Round functions.  Note that f2() is used in both rounds 2 and 4 */
-#define f1(B,C,D) ((B & C) | ((~B) & D))
-#define f2(B,C,D) (B ^ C ^ D)
-#define f3(B,C,D) ((B & C) | (B & D) | (C & D))
+#define f1(B, C, D) ((B & C) | ((~B) & D))
+#define f2(B, C, D) (B ^ C ^ D)
+#define f3(B, C, D) ((B & C) | (B & D) | (C & D))
 
 
 /* left circular shift functions (rotate left) */
 /* left circular shift functions (rotate left) */
-#define rol1(x) ((x<<1) | ((x>>31) & 1))
-#define rol5(A) ((A<<5) | ((A>>27) & 0x1f))
-#define rol30(B) ((B<<30) | ((B>>2) & 0x3fffffff))
+#define rol1(x) ((x << 1) | ((x >> 31) & 1))
+#define rol5(A) ((A << 5) | ((A >> 27) & 0x1f))
+#define rol30(B) ((B << 30) | ((B >> 2) & 0x3fffffff))
 
 
 /*
 /*
   Hashes 'data', which should be a pointer to 512 bits of data (sixteen
   Hashes 'data', which should be a pointer to 512 bits of data (sixteen
   32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints)
   32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints)
   'hash'
   'hash'
 */
 */
-int 
-sha_hash(int *data, int *hash)  
+int sha_hash(int *data, int *hash)
 {
 {
-  int W[80];
-  unsigned int A=hash[0], B=hash[1], C=hash[2], D=hash[3], E=hash[4];
-  unsigned int t, x, TEMP;
+	int W[80];
+	unsigned int A = hash[0], B = hash[1], C = hash[2], D = hash[3],
+				 E = hash[4];
+	unsigned int t, x, TEMP;
 
 
-  for (t=0; t<16; t++) 
-    {
+	for(t = 0; t < 16; t++) {
 #ifndef __IS_BIG_ENDIAN
 #ifndef __IS_BIG_ENDIAN
-      W[t]=switch_endianness(data[t]);
-#else 
-      W[t]=data[t];
+		W[t] = switch_endianness(data[t]);
+#else
+		W[t] = data[t];
 #endif
 #endif
-    }
+	}
 
 
 
 
-  /* SHA1 Data expansion */
-  for (t=16; t<80; t++) 
-    {
-      x=W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
-      W[t]=rol1(x);
-    }
+	/* SHA1 Data expansion */
+	for(t = 16; t < 80; t++) {
+		x = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
+		W[t] = rol1(x);
+	}
 
 
-  /* SHA1 main loop (t=0 to 79) 
+	/* SHA1 main loop (t=0 to 79) 
    This is broken down into four subloops in order to use
    This is broken down into four subloops in order to use
    the correct round function and constant */
    the correct round function and constant */
-  for (t=0; t<20; t++) 
-    {
-      TEMP=rol5(A) + f1(B,C,D) + E + W[t] + K1;
-      E=D;
-      D=C;
-      C=rol30(B);
-      B=A;
-      A=TEMP;
-    }
-  for (; t<40; t++) 
-    {
-      TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K2;
-      E=D;
-      D=C;
-      C=rol30(B);
-      B=A;
-      A=TEMP;
-    }
-  for (; t<60; t++) 
-    {
-      TEMP=rol5(A) + f3(B,C,D) + E + W[t] + K3;
-      E=D;
-      D=C;
-      C=rol30(B);
-      B=A;
-      A=TEMP;
-    }
-  for (; t<80; t++) 
-    {
-      TEMP=rol5(A) + f2(B,C,D) + E + W[t] + K4;
-      E=D;
-      D=C;
-      C=rol30(B);
-      B=A;
-      A=TEMP;
-    }
-  hash[0]+=A; 
-  hash[1]+=B;
-  hash[2]+=C;
-  hash[3]+=D;
-  hash[4]+=E;
-  return 0;
+	for(t = 0; t < 20; t++) {
+		TEMP = rol5(A) + f1(B, C, D) + E + W[t] + K1;
+		E = D;
+		D = C;
+		C = rol30(B);
+		B = A;
+		A = TEMP;
+	}
+	for(; t < 40; t++) {
+		TEMP = rol5(A) + f2(B, C, D) + E + W[t] + K2;
+		E = D;
+		D = C;
+		C = rol30(B);
+		B = A;
+		A = TEMP;
+	}
+	for(; t < 60; t++) {
+		TEMP = rol5(A) + f3(B, C, D) + E + W[t] + K3;
+		E = D;
+		D = C;
+		C = rol30(B);
+		B = A;
+		A = TEMP;
+	}
+	for(; t < 80; t++) {
+		TEMP = rol5(A) + f2(B, C, D) + E + W[t] + K4;
+		E = D;
+		D = C;
+		C = rol30(B);
+		B = A;
+		A = TEMP;
+	}
+	hash[0] += A;
+	hash[1] += B;
+	hash[2] += C;
+	hash[3] += D;
+	hash[4] += E;
+	return 0;
 }
 }
 
 
 /*
 /*
@@ -172,25 +165,23 @@ sha_hash(int *data, int *hash)
   initializes it to the start constants of the SHA1 algorithm.  This
   initializes it to the start constants of the SHA1 algorithm.  This
   must be called before using hash in the call to sha_hash
   must be called before using hash in the call to sha_hash
 */
 */
-int 
-sha_init(int *hash) 
+int sha_init(int *hash)
 {
 {
-  hash[0]=Ai;
-  hash[1]=Bi;
-  hash[2]=Ci;
-  hash[3]=Di;
-  hash[4]=Ei;
-  return 0;
+	hash[0] = Ai;
+	hash[1] = Bi;
+	hash[2] = Ci;
+	hash[3] = Di;
+	hash[4] = Ei;
+	return 0;
 }
 }
 
 
-int strprintsha(char *dest, int *hashval) 
+int strprintsha(char *dest, int *hashval)
 {
 {
 	int x;
 	int x;
 	char *hashstr = dest;
 	char *hashstr = dest;
-	for (x=0; x<5; x++) 
-	{
+	for(x = 0; x < 5; x++) {
 		snprintf(hashstr, 9, "%08x", hashval[x]);
 		snprintf(hashstr, 9, "%08x", hashval[x]);
-		hashstr+=8;
+		hashstr += 8;
 	}
 	}
 	/*old way */
 	/*old way */
 	//snprintf(hashstr++, 1, "\0");
 	//snprintf(hashstr++, 1, "\0");
@@ -200,13 +191,13 @@ int strprintsha(char *dest, int *hashval)
 	return 0;
 	return 0;
 }
 }
 
 
-char *shahash(const char *str) 
+char *shahash(const char *str)
 {
 {
 	char read_buffer[65];
 	char read_buffer[65];
 	//int read_buffer[64];
 	//int read_buffer[64];
-	int c=1, i;
-       
-	INT64 length=0;
+	int c = 1, i;
+
+	INT64 length = 0;
 
 
 	int strsz;
 	int strsz;
 	static char final[40];
 	static char final[40];
@@ -218,48 +209,43 @@ char *shahash(const char *str)
 
 
 	strsz = strlen(str);
 	strsz = strlen(str);
 
 
-	if(strsz == 0) 
-	{
-	     memset(read_buffer, 0, 65);
-	     read_buffer[0] = 0x80;
-	     sha_hash((int *)read_buffer, hashval);
+	if(strsz == 0) {
+		memset(read_buffer, 0, 65);
+		read_buffer[0] = 0x80;
+		sha_hash((int *)read_buffer, hashval);
 	}
 	}
 
 
-	while (strsz>0) 
-	{
+	while(strsz > 0) {
 		memset(read_buffer, 0, 65);
 		memset(read_buffer, 0, 65);
-		strncpy((char*)read_buffer, str, 64);
+		strncpy((char *)read_buffer, str, 64);
 		c = strlen((char *)read_buffer);
 		c = strlen((char *)read_buffer);
-		length+=c;
-		strsz-=c;
-		if (strsz<=0) 
-		{
-			length<<=3;	
-			read_buffer[c]=(char)0x80;
-			for (i=c+1; i<64; i++) 
-				read_buffer[i]=0;
-			if (c>55) 
-			{
+		length += c;
+		strsz -= c;
+		if(strsz <= 0) {
+			length <<= 3;
+			read_buffer[c] = (char)0x80;
+			for(i = c + 1; i < 64; i++)
+				read_buffer[i] = 0;
+			if(c > 55) {
 				/* we need to do an entire new block */
 				/* we need to do an entire new block */
 				sha_hash((int *)read_buffer, hashval);
 				sha_hash((int *)read_buffer, hashval);
-				for (i=0; i<14; i++) 
-					((int*)read_buffer)[i]=0;
-			}      
+				for(i = 0; i < 14; i++)
+					((int *)read_buffer)[i] = 0;
+			}
 #ifndef __IS_BIG_ENDIAN
 #ifndef __IS_BIG_ENDIAN
-			for (i=0; i<8; i++) 
-			{
-				read_buffer[56+i]=(char)(length>>(56-(i*8))) & 0xff;
+			for(i = 0; i < 8; i++) {
+				read_buffer[56 + i] = (char)(length >> (56 - (i * 8))) & 0xff;
 			}
 			}
-#else	
-			memcpy(read_buffer+56, &length, 8);
+#else
+			memcpy(read_buffer + 56, &length, 8);
 #endif
 #endif
 		}
 		}
-		
+
 		sha_hash((int *)read_buffer, hashval);
 		sha_hash((int *)read_buffer, hashval);
-		str+=64;
+		str += 64;
 	}
 	}
 
 
-	strprintsha((char *)final, hashval);
+	strprintsha((char *) final, hashval);
 	free(hashval);
 	free(hashval);
-	return (char *)final;
+	return (char *) final;
 }
 }

+ 52 - 74
src/modules/xmpp/util.c

@@ -48,31 +48,28 @@ char *decode_uri_sip_xmpp(char *uri)
 	char *p;
 	char *p;
 	param_t *it = NULL;
 	param_t *it = NULL;
 
 
-	if (!uri)
+	if(!uri)
 		return NULL;
 		return NULL;
-	if (parse_uri(uri, strlen(uri), &puri) < 0) {
+	if(parse_uri(uri, strlen(uri), &puri) < 0) {
 		LM_ERR("failed to parse URI\n");
 		LM_ERR("failed to parse URI\n");
 		return NULL;
 		return NULL;
 	}
 	}
-	if(_xmpp_gwmap_list==0)
-	{
+	if(_xmpp_gwmap_list == 0) {
 		strncpy(buf, puri.user.s, sizeof(buf));
 		strncpy(buf, puri.user.s, sizeof(buf));
 		buf[puri.user.len] = 0;
 		buf[puri.user.len] = 0;
-	
+
 		/* replace domain separator */
 		/* replace domain separator */
-		if ((p = strchr(buf, domain_separator)))
+		if((p = strchr(buf, domain_separator)))
 			*p = '@';
 			*p = '@';
 	} else {
 	} else {
-		for(it=_xmpp_gwmap_list; it; it=it->next)
-		{
-			if(it->name.len==puri.host.len
-					&& strncasecmp(it->name.s, puri.host.s, it->name.len)==0)
-			{
+		for(it = _xmpp_gwmap_list; it; it = it->next) {
+			if(it->name.len == puri.host.len
+					&& strncasecmp(it->name.s, puri.host.s, it->name.len)
+							   == 0) {
 				break;
 				break;
 			}
 			}
 		}
 		}
-		if(it && it->body.len>0)
-		{
+		if(it && it->body.len > 0) {
 			snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
 					it->body.len, it->body.s);
 					it->body.len, it->body.s);
 		} else {
 		} else {
@@ -92,30 +89,24 @@ char *encode_uri_sip_xmpp(char *uri)
 	static char buf[512];
 	static char buf[512];
 	param_t *it = NULL;
 	param_t *it = NULL;
 
 
-	if (!uri)
+	if(!uri)
 		return NULL;
 		return NULL;
-	if (parse_uri(uri, strlen(uri), &puri) < 0) {
+	if(parse_uri(uri, strlen(uri), &puri) < 0) {
 		LM_ERR("failed to parse URI\n");
 		LM_ERR("failed to parse URI\n");
 		return NULL;
 		return NULL;
 	}
 	}
-	if(_xmpp_gwmap_list==0)
-	{
-		snprintf(buf, sizeof(buf), "%.*s%c%.*s@%s",
-			puri.user.len, puri.user.s,
-			domain_separator,
-			puri.host.len, puri.host.s,
-			xmpp_domain);
+	if(_xmpp_gwmap_list == 0) {
+		snprintf(buf, sizeof(buf), "%.*s%c%.*s@%s", puri.user.len, puri.user.s,
+				domain_separator, puri.host.len, puri.host.s, xmpp_domain);
 	} else {
 	} else {
-		for(it=_xmpp_gwmap_list; it; it=it->next)
-		{
-			if(it->name.len==puri.host.len
-					&& strncasecmp(it->name.s, puri.host.s, it->name.len)==0)
-			{
+		for(it = _xmpp_gwmap_list; it; it = it->next) {
+			if(it->name.len == puri.host.len
+					&& strncasecmp(it->name.s, puri.host.s, it->name.len)
+							   == 0) {
 				break;
 				break;
 			}
 			}
 		}
 		}
-		if(it && it->body.len>0)
-		{
+		if(it && it->body.len > 0) {
 			snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "%.*s@%.*s", puri.user.len, puri.user.s,
 					it->body.len, it->body.s);
 					it->body.len, it->body.s);
 		} else {
 		} else {
@@ -138,55 +129,49 @@ char *decode_uri_xmpp_sip(char *jid)
 	str sd;
 	str sd;
 	param_t *it = NULL;
 	param_t *it = NULL;
 
 
-	if (!jid)
+	if(!jid)
 		return NULL;
 		return NULL;
 
 
-	if(_xmpp_gwmap_list==0)
-	{
+	if(_xmpp_gwmap_list == 0) {
 		snprintf(buf, sizeof(buf), "sip:%s", jid);
 		snprintf(buf, sizeof(buf), "sip:%s", jid);
 
 
 		/* strip off resource */
 		/* strip off resource */
-		if ((p = strchr(buf, '/')))
+		if((p = strchr(buf, '/')))
 			*p = 0;
 			*p = 0;
 		/* strip off domain */
 		/* strip off domain */
-		if ((p = strchr(buf, '@')))
+		if((p = strchr(buf, '@')))
 			*p = 0;
 			*p = 0;
 		/* replace domain separator */
 		/* replace domain separator */
-		if ((p = strchr(buf, domain_separator)))
+		if((p = strchr(buf, domain_separator)))
 			*p = '@';
 			*p = '@';
 	} else {
 	} else {
 		snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
 		snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
 
 
 		/* strip off resource */
 		/* strip off resource */
-		if ((p = strchr(tbuf, '/')))
+		if((p = strchr(tbuf, '/')))
 			*p = 0;
 			*p = 0;
-		if (parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
+		if(parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
 			LM_ERR("failed to parse URI\n");
 			LM_ERR("failed to parse URI\n");
 			return NULL;
 			return NULL;
 		}
 		}
-		for(it=_xmpp_gwmap_list; it; it=it->next)
-		{
-			if(it->body.len>0)
-			{
+		for(it = _xmpp_gwmap_list; it; it = it->next) {
+			if(it->body.len > 0) {
 				sd = it->body;
 				sd = it->body;
 			} else {
 			} else {
 				sd = it->name;
 				sd = it->name;
 			}
 			}
-			if(sd.len==puri.host.len
-					&& strncasecmp(sd.s, puri.host.s, sd.len)==0)
-			{
+			if(sd.len == puri.host.len
+					&& strncasecmp(sd.s, puri.host.s, sd.len) == 0) {
 				break;
 				break;
 			}
 			}
 		}
 		}
-		if(it)
-		{
+		if(it) {
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 					it->name.len, it->name.s);
 					it->name.len, it->name.s);
 		} else {
 		} else {
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 					puri.host.len, puri.host.s);
 					puri.host.len, puri.host.s);
 		}
 		}
-
 	}
 	}
 	return buf;
 	return buf;
 }
 }
@@ -203,50 +188,44 @@ char *encode_uri_xmpp_sip(char *jid)
 	str sd;
 	str sd;
 	param_t *it = NULL;
 	param_t *it = NULL;
 
 
-	if (!jid)
+	if(!jid)
 		return NULL;
 		return NULL;
 
 
-	if(_xmpp_gwmap_list==0)
-	{
+	if(_xmpp_gwmap_list == 0) {
 		/* TODO: maybe not modify jid? */
 		/* TODO: maybe not modify jid? */
-		if ((p = strchr(jid, '/')))
+		if((p = strchr(jid, '/')))
 			*p = 0;
 			*p = 0;
-		if ((p = strchr(jid, '@')))
+		if((p = strchr(jid, '@')))
 			*p = domain_separator;
 			*p = domain_separator;
 		snprintf(buf, sizeof(buf), "sip:%s@%s", jid, gateway_domain);
 		snprintf(buf, sizeof(buf), "sip:%s@%s", jid, gateway_domain);
 	} else {
 	} else {
 		snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
 		snprintf(tbuf, sizeof(tbuf), "sip:%s", jid);
 
 
 		/* strip off resource */
 		/* strip off resource */
-		if ((p = strchr(tbuf, '/')))
+		if((p = strchr(tbuf, '/')))
 			*p = 0;
 			*p = 0;
-		if (parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
+		if(parse_uri(tbuf, strlen(tbuf), &puri) < 0) {
 			LM_ERR("failed to parse URI\n");
 			LM_ERR("failed to parse URI\n");
 			return NULL;
 			return NULL;
 		}
 		}
-		for(it=_xmpp_gwmap_list; it; it=it->next)
-		{
-			if(it->body.len>0)
-			{
+		for(it = _xmpp_gwmap_list; it; it = it->next) {
+			if(it->body.len > 0) {
 				sd = it->body;
 				sd = it->body;
 			} else {
 			} else {
 				sd = it->name;
 				sd = it->name;
 			}
 			}
-			if(sd.len==puri.host.len
-					&& strncasecmp(sd.s, puri.host.s, sd.len)==0)
-			{
+			if(sd.len == puri.host.len
+					&& strncasecmp(sd.s, puri.host.s, sd.len) == 0) {
 				break;
 				break;
 			}
 			}
 		}
 		}
-		if(it)
-		{
+		if(it) {
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 					it->name.len, it->name.s);
 					it->name.len, it->name.s);
 		} else {
 		} else {
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 			snprintf(buf, 512, "sip:%.*s@%.*s", puri.user.len, puri.user.s,
 					puri.host.len, puri.host.s);
 					puri.host.len, puri.host.s);
 		}
 		}
-
 	}
 	}
 
 
 	return buf;
 	return buf;
@@ -255,10 +234,10 @@ char *encode_uri_xmpp_sip(char *jid)
 char *extract_domain(char *jid)
 char *extract_domain(char *jid)
 {
 {
 	char *p;
 	char *p;
-	
-	if ((p = strchr(jid, '/')))
+
+	if((p = strchr(jid, '/')))
 		*p = 0;
 		*p = 0;
-	if ((p = strchr(jid, '@'))) {
+	if((p = strchr(jid, '@'))) {
 		*p++ = 0;
 		*p++ = 0;
 		return p;
 		return p;
 	}
 	}
@@ -270,11 +249,11 @@ char *random_secret(void)
 	static char secret[41];
 	static char secret[41];
 	int i, r;
 	int i, r;
 
 
-        for (i = 0; i < 40; i++) {
-            r = (int) (36.0 * kam_rand() / KAM_RAND_MAX);
-            secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
-        }
-        secret[40] = '\0';
+	for(i = 0; i < 40; i++) {
+		r = (int)(36.0 * kam_rand() / KAM_RAND_MAX);
+		secret[i] = (r >= 0 && r <= 9) ? (r + 48) : (r + 87);
+	}
+	secret[40] = '\0';
 
 
 	return secret;
 	return secret;
 }
 }
@@ -283,7 +262,7 @@ char *db_key(char *secret, char *domain, char *id)
 {
 {
 	char buf[1024];
 	char buf[1024];
 	char *hash;
 	char *hash;
-	
+
 	snprintf(buf, sizeof(buf), "%s", secret);
 	snprintf(buf, sizeof(buf), "%s", secret);
 	hash = shahash(buf);
 	hash = shahash(buf);
 
 
@@ -294,4 +273,3 @@ char *db_key(char *secret, char *domain, char *id)
 	hash = shahash(buf);
 	hash = shahash(buf);
 	return hash;
 	return hash;
 }
 }
-

+ 92 - 97
src/modules/xmpp/xmpp.c

@@ -127,20 +127,20 @@
 #include <arpa/inet.h>
 #include <arpa/inet.h>
 
 
 /* XXX hack */
 /* XXX hack */
-#define DB_KEY	"this-be-a-random-key"
+#define DB_KEY "this-be-a-random-key"
 
 
 MODULE_VERSION
 MODULE_VERSION
 
 
 struct tm_binds tmb;
 struct tm_binds tmb;
 
 
-static int  mod_init(void);
-static int  child_init(int rank);
+static int mod_init(void);
+static int child_init(int rank);
 static void xmpp_process(int rank);
 static void xmpp_process(int rank);
-static int  cmd_send_message(struct sip_msg* msg, char* _foo, char* _bar);
+static int cmd_send_message(struct sip_msg *msg, char *_foo, char *_bar);
 
 
 int xmpp_gwmap_param(modparam_t type, void *val);
 int xmpp_gwmap_param(modparam_t type, void *val);
 
 
-static int pipe_fds[2] = {-1,-1};
+static int pipe_fds[2] = {-1, -1};
 
 
 /*
 /*
  * Configuration
  * Configuration
@@ -153,7 +153,7 @@ char *xmpp_domain = "xmpp2sip.example.net";
 char *xmpp_host = "xmpp.example.com";
 char *xmpp_host = "xmpp.example.com";
 int xmpp_port = 0;
 int xmpp_port = 0;
 char *xmpp_password = "secret";
 char *xmpp_password = "secret";
-str outbound_proxy= {0, 0};
+str outbound_proxy = {0, 0};
 
 
 param_t *_xmpp_gwmap_list = NULL;
 param_t *_xmpp_gwmap_list = NULL;
 
 
@@ -164,75 +164,73 @@ param_t *_xmpp_gwmap_list = NULL;
  * Exported functions
  * Exported functions
  */
  */
 static cmd_export_t cmds[] = {
 static cmd_export_t cmds[] = {
-	{"xmpp_send_message", (cmd_function)cmd_send_message, 0, 0, 0, REQUEST_ROUTE},
-	{"bind_xmpp",         (cmd_function)bind_xmpp,        0, 0, 0, 0},
-	{0, 0, 0, 0, 0, 0}
-};
+		{"xmpp_send_message", (cmd_function)cmd_send_message, 0, 0, 0,
+				REQUEST_ROUTE},
+		{"bind_xmpp", (cmd_function)bind_xmpp, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}};
 
 
 /*
 /*
  * Exported parameters
  * Exported parameters
  */
  */
-static param_export_t params[] = {
-	{ "backend",			PARAM_STRING, &backend },
-	{ "domain_separator",	PARAM_STRING, &domain_sep_str },
-	{ "gateway_domain",		PARAM_STRING, &gateway_domain },
-	{ "xmpp_domain",		PARAM_STRING, &xmpp_domain },
-	{ "xmpp_host",			PARAM_STRING, &xmpp_host },
-	{ "xmpp_port",			INT_PARAM, &xmpp_port },
-	{ "xmpp_password",		PARAM_STRING, &xmpp_password },
-	{ "outbound_proxy",		PARAM_STR, &outbound_proxy},
-	{ "gwmap",              PARAM_STRING|USE_FUNC_PARAM, (void*)xmpp_gwmap_param},
-	{0, 0, 0}
-};
+static param_export_t params[] = {{"backend", PARAM_STRING, &backend},
+		{"domain_separator", PARAM_STRING, &domain_sep_str},
+		{"gateway_domain", PARAM_STRING, &gateway_domain},
+		{"xmpp_domain", PARAM_STRING, &xmpp_domain},
+		{"xmpp_host", PARAM_STRING, &xmpp_host},
+		{"xmpp_port", INT_PARAM, &xmpp_port},
+		{"xmpp_password", PARAM_STRING, &xmpp_password},
+		{"outbound_proxy", PARAM_STR, &outbound_proxy},
+		{"gwmap", PARAM_STRING | USE_FUNC_PARAM, (void *)xmpp_gwmap_param},
+		{0, 0, 0}};
 
 
 /*
 /*
  * Module description
  * Module description
  */
  */
 struct module_exports exports = {
 struct module_exports exports = {
-	"xmpp",          /* module name */
-	DEFAULT_DLFLAGS, /* dlopen flags */
-	cmds,            /* exported functions */
-	params,          /* exported parameters */
-	0,               /* exported rpc functions */
-	0,               /* exported pseudo-variables */
-	0,               /* response handling function */
-	mod_init,        /* module init function */
-	child_init,      /* child init function */
-	0                /* module destroy function */
+		"xmpp",			 /* module name */
+		DEFAULT_DLFLAGS, /* dlopen flags */
+		cmds,			 /* exported functions */
+		params,			 /* exported parameters */
+		0,				 /* exported rpc functions */
+		0,				 /* exported pseudo-variables */
+		0,				 /* response handling function */
+		mod_init,		 /* module init function */
+		child_init,		 /* child init function */
+		0				 /* module destroy function */
 };
 };
 
 
 /*
 /*
  * initialize module
  * initialize module
  */
  */
-static int mod_init(void) {
+static int mod_init(void)
+{
 
 
-	if (load_tm_api(&tmb)) {
+	if(load_tm_api(&tmb)) {
 		LM_ERR("failed to load tm API\n");
 		LM_ERR("failed to load tm API\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
-	if (strcmp(backend, "component") && strcmp(backend, "server")) {
+	if(strcmp(backend, "component") && strcmp(backend, "server")) {
 		LM_ERR("invalid backend '%s'\n", backend);
 		LM_ERR("invalid backend '%s'\n", backend);
 		return -1;
 		return -1;
 	}
 	}
 
 
-	if (!xmpp_port) {
-		if (!strcmp(backend, "component"))
+	if(!xmpp_port) {
+		if(!strcmp(backend, "component"))
 			xmpp_port = DEFAULT_COMPONENT_PORT;
 			xmpp_port = DEFAULT_COMPONENT_PORT;
-		else if (!strcmp(backend, "server"))
+		else if(!strcmp(backend, "server"))
 			xmpp_port = DEFAULT_SERVER_PORT;
 			xmpp_port = DEFAULT_SERVER_PORT;
 	}
 	}
 
 
 	/* fix up the domain separator -- we only need 1 char */
 	/* fix up the domain separator -- we only need 1 char */
-	if (domain_sep_str && *domain_sep_str)
+	if(domain_sep_str && *domain_sep_str)
 		domain_separator = *domain_sep_str;
 		domain_separator = *domain_sep_str;
 
 
-	if(init_xmpp_cb_list()<0){
+	if(init_xmpp_cb_list() < 0) {
 		LM_ERR("failed to init callback list\n");
 		LM_ERR("failed to init callback list\n");
 		return -1;
 		return -1;
 	}
 	}
 
 
-	if (pipe(pipe_fds) < 0) {
+	if(pipe(pipe_fds) < 0) {
 		LM_ERR("pipe() failed\n");
 		LM_ERR("pipe() failed\n");
 		return -1;
 		return -1;
 	}
 	}
@@ -252,14 +250,14 @@ static int child_init(int rank)
 {
 {
 	int pid;
 	int pid;
 
 
-	if (rank==PROC_MAIN) {
-		pid=fork_process(PROC_NOCHLDINIT, "XMPP Manager", 1);
-		if (pid<0)
+	if(rank == PROC_MAIN) {
+		pid = fork_process(PROC_NOCHLDINIT, "XMPP Manager", 1);
+		if(pid < 0)
 			return -1; /* error */
 			return -1; /* error */
-		if(pid==0){
+		if(pid == 0) {
 			/* child */
 			/* child */
 			/* initialize the config framework */
 			/* initialize the config framework */
-			if (cfg_child_init())
+			if(cfg_child_init())
 				return -1;
 				return -1;
 
 
 			xmpp_process(1);
 			xmpp_process(1);
@@ -278,9 +276,9 @@ static void xmpp_process(int rank)
 	close(pipe_fds[1]);
 	close(pipe_fds[1]);
 
 
 	LM_DBG("started child connection process\n");
 	LM_DBG("started child connection process\n");
-	if (!strcmp(backend, "component"))
+	if(!strcmp(backend, "component"))
 		xmpp_component_child_process(pipe_fds[0]);
 		xmpp_component_child_process(pipe_fds[0]);
-	else if (!strcmp(backend, "server"))
+	else if(!strcmp(backend, "server"))
 		xmpp_server_child_process(pipe_fds[0]);
 		xmpp_server_child_process(pipe_fds[0]);
 }
 }
 
 
@@ -294,11 +292,11 @@ static void xmpp_process(int rank)
 */
 */
 int xmpp_send_sip_msg(char *from, char *to, char *msg)
 int xmpp_send_sip_msg(char *from, char *to, char *msg)
 {
 {
-	str msg_type = { "MESSAGE", 7 };
+	str msg_type = {"MESSAGE", 7};
 	str hdr, fromstr, tostr, msgstr;
 	str hdr, fromstr, tostr, msgstr;
 	char buf[512];
 	char buf[512];
 	uac_req_t uac_r;
 	uac_req_t uac_r;
-	
+
 	hdr.s = buf;
 	hdr.s = buf;
 	hdr.len = snprintf(buf, sizeof(buf),
 	hdr.len = snprintf(buf, sizeof(buf),
 			"Content-type: text/plain" CRLF "Contact: %s" CRLF, from);
 			"Content-type: text/plain" CRLF "Contact: %s" CRLF, from);
@@ -311,13 +309,11 @@ int xmpp_send_sip_msg(char *from, char *to, char *msg)
 	msgstr.len = strlen(msg);
 	msgstr.len = strlen(msg);
 
 
 	set_uac_req(&uac_r, &msg_type, &hdr, &msgstr, 0, 0, 0, 0);
 	set_uac_req(&uac_r, &msg_type, &hdr, &msgstr, 0, 0, 0, 0);
-	return tmb.t_request(
-			&uac_r,
-			0,							/*!< Request-URI */
-			&tostr,							/*!< To */
-			&fromstr,						/*!< From */
-			(outbound_proxy.s)?&outbound_proxy:NULL/* Outbound proxy*/
-			);
+	return tmb.t_request(&uac_r, 0,						/*!< Request-URI */
+			&tostr,										/*!< To */
+			&fromstr,									/*!< From */
+			(outbound_proxy.s) ? &outbound_proxy : NULL /* Outbound proxy*/
+	);
 }
 }
 
 
 
 
@@ -325,24 +321,24 @@ int xmpp_send_sip_msg(char *from, char *to, char *msg)
 
 
 void xmpp_free_pipe_cmd(struct xmpp_pipe_cmd *cmd)
 void xmpp_free_pipe_cmd(struct xmpp_pipe_cmd *cmd)
 {
 {
-	if (cmd->from)
+	if(cmd->from)
 		shm_free(cmd->from);
 		shm_free(cmd->from);
-	if (cmd->to)
+	if(cmd->to)
 		shm_free(cmd->to);
 		shm_free(cmd->to);
-	if (cmd->body)
+	if(cmd->body)
 		shm_free(cmd->body);
 		shm_free(cmd->body);
-	if (cmd->id)
+	if(cmd->id)
 		shm_free(cmd->id);
 		shm_free(cmd->id);
 	shm_free(cmd);
 	shm_free(cmd);
 }
 }
 
 
-static int xmpp_send_pipe_cmd(enum xmpp_pipe_cmd_type type, str *from, str *to, 
-		str *body, str *id)
+static int xmpp_send_pipe_cmd(
+		enum xmpp_pipe_cmd_type type, str *from, str *to, str *body, str *id)
 {
 {
 	struct xmpp_pipe_cmd *cmd;
 	struct xmpp_pipe_cmd *cmd;
-	
+
 	/*! \todo: make shm allocation for one big chunk to include all fields */
 	/*! \todo: make shm allocation for one big chunk to include all fields */
-	cmd = (struct xmpp_pipe_cmd *) shm_malloc(sizeof(struct xmpp_pipe_cmd));
+	cmd = (struct xmpp_pipe_cmd *)shm_malloc(sizeof(struct xmpp_pipe_cmd));
 	memset(cmd, 0, sizeof(struct xmpp_pipe_cmd));
 	memset(cmd, 0, sizeof(struct xmpp_pipe_cmd));
 
 
 	cmd->type = type;
 	cmd->type = type;
@@ -351,7 +347,7 @@ static int xmpp_send_pipe_cmd(enum xmpp_pipe_cmd_type type, str *from, str *to,
 	cmd->body = shm_str2char_dup(body);
 	cmd->body = shm_str2char_dup(body);
 	cmd->id = shm_str2char_dup(id);
 	cmd->id = shm_str2char_dup(id);
 
 
-	if (write(pipe_fds[1], &cmd, sizeof(cmd)) != sizeof(cmd)) {
+	if(write(pipe_fds[1], &cmd, sizeof(cmd)) != sizeof(cmd)) {
 		LM_ERR("failed to write to command pipe: %s\n", strerror(errno));
 		LM_ERR("failed to write to command pipe: %s\n", strerror(errno));
 		xmpp_free_pipe_cmd(cmd);
 		xmpp_free_pipe_cmd(cmd);
 		return -1;
 		return -1;
@@ -359,66 +355,66 @@ static int xmpp_send_pipe_cmd(enum xmpp_pipe_cmd_type type, str *from, str *to,
 	return 0;
 	return 0;
 }
 }
 
 
-static int cmd_send_message(struct sip_msg* msg, char* _foo, char* _bar)
+static int cmd_send_message(struct sip_msg *msg, char *_foo, char *_bar)
 {
 {
 	str body, from_uri, dst, tagid;
 	str body, from_uri, dst, tagid;
 	int mime;
 	int mime;
 
 
 	LM_DBG("cmd_send_message\n");
 	LM_DBG("cmd_send_message\n");
-	
+
 	/* extract body */
 	/* extract body */
-	if (!(body.s = get_body(msg))) {
+	if(!(body.s = get_body(msg))) {
 		LM_ERR("failed to extract body\n");
 		LM_ERR("failed to extract body\n");
 		return -1;
 		return -1;
 	}
 	}
-	if (!msg->content_length) {
+	if(!msg->content_length) {
 		LM_ERR("no content-length found\n");
 		LM_ERR("no content-length found\n");
 		return -1;
 		return -1;
 	}
 	}
 	body.len = get_content_length(msg);
 	body.len = get_content_length(msg);
-	if ((mime = parse_content_type_hdr(msg)) < 1) {
+	if((mime = parse_content_type_hdr(msg)) < 1) {
 		LM_ERR("failed parse content-type\n");
 		LM_ERR("failed parse content-type\n");
 		return -1;
 		return -1;
 	}
 	}
-	if (mime != (TYPE_TEXT << 16) + SUBTYPE_PLAIN 
+	if(mime != (TYPE_TEXT << 16) + SUBTYPE_PLAIN
 			&& mime != (TYPE_MESSAGE << 16) + SUBTYPE_CPIM) {
 			&& mime != (TYPE_MESSAGE << 16) + SUBTYPE_CPIM) {
 		LM_ERR("invalid content-type 0x%x\n", mime);
 		LM_ERR("invalid content-type 0x%x\n", mime);
 		return -1;
 		return -1;
 	}
 	}
 
 
 	/* extract sender */
 	/* extract sender */
-	if (parse_headers(msg, HDR_TO_F | HDR_FROM_F, 0) == -1 || !msg->to
+	if(parse_headers(msg, HDR_TO_F | HDR_FROM_F, 0) == -1 || !msg->to
 			|| !msg->from) {
 			|| !msg->from) {
 		LM_ERR("no To/From headers\n");
 		LM_ERR("no To/From headers\n");
 		return -1;
 		return -1;
 	}
 	}
-	if (parse_from_header(msg) < 0 || !msg->from->parsed) {
+	if(parse_from_header(msg) < 0 || !msg->from->parsed) {
 		LM_ERR("failed to parse From header\n");
 		LM_ERR("failed to parse From header\n");
 		return -1;
 		return -1;
 	}
 	}
-	from_uri = ((struct to_body *) msg->from->parsed)->uri;
-	tagid = ((struct to_body *) msg->from->parsed)->tag_value;
+	from_uri = ((struct to_body *)msg->from->parsed)->uri;
+	tagid = ((struct to_body *)msg->from->parsed)->tag_value;
 	LM_DBG("message from <%.*s>\n", from_uri.len, from_uri.s);
 	LM_DBG("message from <%.*s>\n", from_uri.len, from_uri.s);
 
 
 	/* extract recipient */
 	/* extract recipient */
 	dst.len = 0;
 	dst.len = 0;
-	if (msg->new_uri.len > 0) {
+	if(msg->new_uri.len > 0) {
 		LM_DBG("using new URI as destination\n");
 		LM_DBG("using new URI as destination\n");
 		dst = msg->new_uri;
 		dst = msg->new_uri;
-	} else if (msg->first_line.u.request.uri.s 
-			&& msg->first_line.u.request.uri.len > 0) {
+	} else if(msg->first_line.u.request.uri.s
+			  && msg->first_line.u.request.uri.len > 0) {
 		LM_DBG("using R-URI as destination\n");
 		LM_DBG("using R-URI as destination\n");
 		dst = msg->first_line.u.request.uri;
 		dst = msg->first_line.u.request.uri;
-	} else if (msg->to->parsed) {
+	} else if(msg->to->parsed) {
 		LM_DBG("using TO-URI as destination\n");
 		LM_DBG("using TO-URI as destination\n");
-		dst = ((struct to_body *) msg->to->parsed)->uri;
+		dst = ((struct to_body *)msg->to->parsed)->uri;
 	} else {
 	} else {
 		LM_ERR("failed to find a valid destination\n");
 		LM_ERR("failed to find a valid destination\n");
 		return -1;
 		return -1;
 	}
 	}
-	
-	if (!xmpp_send_pipe_cmd(XMPP_PIPE_SEND_MESSAGE, &from_uri, &dst, &body,
-				&tagid))
+
+	if(!xmpp_send_pipe_cmd(
+			   XMPP_PIPE_SEND_MESSAGE, &from_uri, &dst, &body, &tagid))
 		return 1;
 		return 1;
 	return -1;
 	return -1;
 }
 }
@@ -429,7 +425,7 @@ static int cmd_send_message(struct sip_msg* msg, char* _foo, char* _bar)
  */
  */
 int xmpp_send_xpacket(str *from, str *to, str *msg, str *id)
 int xmpp_send_xpacket(str *from, str *to, str *msg, str *id)
 {
 {
-	if(from==NULL || to==NULL || msg==NULL || id==NULL)
+	if(from == NULL || to == NULL || msg == NULL || id == NULL)
 		return -1;
 		return -1;
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PACKET, from, to, msg, id);
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PACKET, from, to, msg, id);
 }
 }
@@ -439,7 +435,7 @@ int xmpp_send_xpacket(str *from, str *to, str *msg, str *id)
  */
  */
 int xmpp_send_xmessage(str *from, str *to, str *msg, str *id)
 int xmpp_send_xmessage(str *from, str *to, str *msg, str *id)
 {
 {
-	if(from==NULL || to==NULL || msg==NULL || id==NULL)
+	if(from == NULL || to == NULL || msg == NULL || id == NULL)
 		return -1;
 		return -1;
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_MESSAGE, from, to, msg, id);
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_MESSAGE, from, to, msg, id);
 }
 }
@@ -449,7 +445,7 @@ int xmpp_send_xmessage(str *from, str *to, str *msg, str *id)
  */
  */
 int xmpp_send_xsubscribe(str *from, str *to, str *msg, str *id)
 int xmpp_send_xsubscribe(str *from, str *to, str *msg, str *id)
 {
 {
-	if(from==NULL || to==NULL || msg==NULL || id==NULL)
+	if(from == NULL || to == NULL || msg == NULL || id == NULL)
 		return -1;
 		return -1;
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PSUBSCRIBE, from, to, msg, id);
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PSUBSCRIBE, from, to, msg, id);
 }
 }
@@ -459,7 +455,7 @@ int xmpp_send_xsubscribe(str *from, str *to, str *msg, str *id)
  */
  */
 int xmpp_send_xnotify(str *from, str *to, str *msg, str *id)
 int xmpp_send_xnotify(str *from, str *to, str *msg, str *id)
 {
 {
-	if(from==NULL || to==NULL || msg==NULL || id==NULL)
+	if(from == NULL || to == NULL || msg == NULL || id == NULL)
 		return -1;
 		return -1;
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PNOTIFY, from, to, msg, id);
 	return xmpp_send_pipe_cmd(XMPP_PIPE_SEND_PNOTIFY, from, to, msg, id);
 }
 }
@@ -474,26 +470,25 @@ int xmpp_gwmap_param(modparam_t type, void *val)
 	param_t *params = NULL;
 	param_t *params = NULL;
 	param_t *it = NULL;
 	param_t *it = NULL;
 
 
-	if(val==NULL)
+	if(val == NULL)
 		return -1;
 		return -1;
-	inv.s = (char*)val;
+	inv.s = (char *)val;
 	inv.len = strlen(inv.s);
 	inv.len = strlen(inv.s);
-	if(inv.len<=0)
+	if(inv.len <= 0)
 		return -1;
 		return -1;
 
 
-	if(inv.s[inv.len-1]==';')
+	if(inv.s[inv.len - 1] == ';')
 		inv.len--;
 		inv.len--;
-	if (parse_params(&inv, CLASS_ANY, &phooks, &params)<0)
-	{
+	if(parse_params(&inv, CLASS_ANY, &phooks, &params) < 0) {
 		LM_ERR("failed parsing params value\n");
 		LM_ERR("failed parsing params value\n");
 		return -1;
 		return -1;
 	}
 	}
-	if(_xmpp_gwmap_list==NULL)
-	{
+	if(_xmpp_gwmap_list == NULL) {
 		_xmpp_gwmap_list = params;
 		_xmpp_gwmap_list = params;
 	} else {
 	} else {
 		it = _xmpp_gwmap_list;
 		it = _xmpp_gwmap_list;
-		while(it->next) it = it->next;
+		while(it->next)
+			it = it->next;
 		it->next = params;
 		it->next = params;
 	}
 	}
 	return 0;
 	return 0;

+ 8 - 6
src/modules/xmpp/xmpp.h

@@ -29,14 +29,16 @@
 #ifndef _MOD_XMPP_H
 #ifndef _MOD_XMPP_H
 #define _MOD_XMPP_H
 #define _MOD_XMPP_H
 
 
-enum xmpp_pipe_cmd_type {
-	XMPP_PIPE_SEND_PACKET     = 1,
-	XMPP_PIPE_SEND_MESSAGE    = 2,
+enum xmpp_pipe_cmd_type
+{
+	XMPP_PIPE_SEND_PACKET = 1,
+	XMPP_PIPE_SEND_MESSAGE = 2,
 	XMPP_PIPE_SEND_PSUBSCRIBE = 4,
 	XMPP_PIPE_SEND_PSUBSCRIBE = 4,
-	XMPP_PIPE_SEND_PNOTIFY    = 8
+	XMPP_PIPE_SEND_PNOTIFY = 8
 };
 };
 
 
-struct xmpp_pipe_cmd {
+struct xmpp_pipe_cmd
+{
 	enum xmpp_pipe_cmd_type type;
 	enum xmpp_pipe_cmd_type type;
 	char *from, *to, *body, *id;
 	char *from, *to, *body, *id;
 };
 };
@@ -58,7 +60,7 @@ extern void xmpp_free_pipe_cmd(struct xmpp_pipe_cmd *cmd);
 char *extract_domain(char *jid);
 char *extract_domain(char *jid);
 char *random_secret(void);
 char *random_secret(void);
 char *db_key(char *secret, char *domain, char *id);
 char *db_key(char *secret, char *domain, char *id);
- 
+
 
 
 /* xmpp_server.c */
 /* xmpp_server.c */
 int xmpp_server_child_process(int data_pipe);
 int xmpp_server_child_process(int data_pipe);

+ 13 - 18
src/modules/xmpp/xmpp_api.c

@@ -41,8 +41,8 @@ xmpp_cb_list_t *_xmpp_cb_list = 0;
 
 
 int init_xmpp_cb_list(void)
 int init_xmpp_cb_list(void)
 {
 {
-	_xmpp_cb_list = (xmpp_cb_list_t*)shm_malloc(sizeof(xmpp_cb_list_t));
-	if (_xmpp_cb_list==0) {
+	_xmpp_cb_list = (xmpp_cb_list_t *)shm_malloc(sizeof(xmpp_cb_list_t));
+	if(_xmpp_cb_list == 0) {
 		SHM_MEM_ERROR;
 		SHM_MEM_ERROR;
 		return -1;
 		return -1;
 	}
 	}
@@ -55,10 +55,10 @@ void destroy_xmpp_cb_list(void)
 {
 {
 	xmpp_callback_t *it, *it1;
 	xmpp_callback_t *it, *it1;
 
 
-	if (_xmpp_cb_list==0)
+	if(_xmpp_cb_list == 0)
 		return;
 		return;
 
 
-	for(it=_xmpp_cb_list->first; it; ) {
+	for(it = _xmpp_cb_list->first; it;) {
 		it1 = it;
 		it1 = it;
 		it = it->next;
 		it = it->next;
 		shm_free(it1);
 		shm_free(it1);
@@ -69,28 +69,25 @@ void destroy_xmpp_cb_list(void)
 }
 }
 
 
 
 
-
 /*! \brief register a callback function 'f' for 'types' mask of events;
 /*! \brief register a callback function 'f' for 'types' mask of events;
 */
 */
-int register_xmpp_cb( int types, xmpp_cb_f f, void *param )
+int register_xmpp_cb(int types, xmpp_cb_f f, void *param)
 {
 {
 	xmpp_callback_t *it;
 	xmpp_callback_t *it;
 
 
-	if(_xmpp_cb_list==0)
-	{
+	if(_xmpp_cb_list == 0) {
 		LM_CRIT("null callback list\n");
 		LM_CRIT("null callback list\n");
 		return E_BUG;
 		return E_BUG;
 	}
 	}
 
 
 	/* check null functions */
 	/* check null functions */
-	if (f==0) {
+	if(f == 0) {
 		LM_CRIT("null callback function\n");
 		LM_CRIT("null callback function\n");
 		return E_BUG;
 		return E_BUG;
 	}
 	}
 
 
 	/* build callback structure */
 	/* build callback structure */
-	if (!(it=(xmpp_callback_t*)shm_malloc(sizeof(xmpp_callback_t))))
-	{
+	if(!(it = (xmpp_callback_t *)shm_malloc(sizeof(xmpp_callback_t)))) {
 		SHM_MEM_ERROR;
 		SHM_MEM_ERROR;
 		return E_OUT_OF_MEM;
 		return E_OUT_OF_MEM;
 	}
 	}
@@ -108,18 +105,17 @@ int register_xmpp_cb( int types, xmpp_cb_f f, void *param )
 }
 }
 
 
 
 
-int bind_xmpp(xmpp_api_t* api)
+int bind_xmpp(xmpp_api_t *api)
 {
 {
-	if (api==NULL)
-	{
+	if(api == NULL) {
 		LM_ERR("invalid parameter value\n");
 		LM_ERR("invalid parameter value\n");
 		return -1;
 		return -1;
 	}
 	}
 	api->register_callback = register_xmpp_cb;
 	api->register_callback = register_xmpp_cb;
-	api->xpacket    = xmpp_send_xpacket;
-	api->xmessage   = xmpp_send_xmessage;
+	api->xpacket = xmpp_send_xpacket;
+	api->xmessage = xmpp_send_xmessage;
 	api->xsubscribe = xmpp_send_xsubscribe;
 	api->xsubscribe = xmpp_send_xsubscribe;
-	api->xnotify    = xmpp_send_xnotify;
+	api->xnotify = xmpp_send_xnotify;
 	api->decode_uri_sip_xmpp = decode_uri_sip_xmpp;
 	api->decode_uri_sip_xmpp = decode_uri_sip_xmpp;
 	api->encode_uri_sip_xmpp = encode_uri_sip_xmpp;
 	api->encode_uri_sip_xmpp = encode_uri_sip_xmpp;
 	api->decode_uri_xmpp_sip = decode_uri_xmpp_sip;
 	api->decode_uri_xmpp_sip = decode_uri_xmpp_sip;
@@ -127,4 +123,3 @@ int bind_xmpp(xmpp_api_t* api)
 
 
 	return 0;
 	return 0;
 }
 }
-

+ 19 - 21
src/modules/xmpp/xmpp_api.h

@@ -30,19 +30,19 @@
 #ifndef _XMPP_API_H_
 #ifndef _XMPP_API_H_
 #define _XMPP_API_H_
 #define _XMPP_API_H_
 
 
-#define XMPP_RCV_MESSAGE      (1<<0)
-#define XMPP_RCV_PRESENCE     (1<<1)
-#define XMPP_RCV_IQ			  (1<<2)
+#define XMPP_RCV_MESSAGE (1 << 0)
+#define XMPP_RCV_PRESENCE (1 << 1)
+#define XMPP_RCV_IQ (1 << 2)
 
 
-typedef void (xmpp_cb_f) (char *msg, int type, void *param);
+typedef void(xmpp_cb_f)(char *msg, int type, void *param);
 typedef int (*register_xmpp_cb_t)(int types, xmpp_cb_f f, void *param);
 typedef int (*register_xmpp_cb_t)(int types, xmpp_cb_f f, void *param);
 
 
 
 
 typedef struct xmpp_callback_
 typedef struct xmpp_callback_
 {
 {
-	int types;                   /*!< types of events that trigger the callback*/
-	xmpp_cb_f *cbf;              /*!< callback function */
-	void *cbp;                   /*!< param to be passed to callback function */
+	int types;		/*!< types of events that trigger the callback*/
+	xmpp_cb_f *cbf; /*!< callback function */
+	void *cbp;		/*!< param to be passed to callback function */
 	struct xmpp_callback_ *next;
 	struct xmpp_callback_ *next;
 } xmpp_callback_t;
 } xmpp_callback_t;
 
 
@@ -53,11 +53,10 @@ typedef struct xmpp_cb_list_
 } xmpp_cb_list_t;
 } xmpp_cb_list_t;
 
 
 
 
-extern xmpp_cb_list_t*  _xmpp_cb_list;
+extern xmpp_cb_list_t *_xmpp_cb_list;
 
 
 
 
-#define xmpp_isset_cb_type(_types_) \
-	((_xmpp_cb_list->types)|(_types_) )
+#define xmpp_isset_cb_type(_types_) ((_xmpp_cb_list->types) | (_types_))
 
 
 
 
 int init_xmpp_cb_list(void);
 int init_xmpp_cb_list(void);
@@ -65,18 +64,18 @@ int init_xmpp_cb_list(void);
 void destroy_xmpp_cb_list(void);
 void destroy_xmpp_cb_list(void);
 
 
 
 
-int register_xmpp_cb( int types, xmpp_cb_f f, void *param );
+int register_xmpp_cb(int types, xmpp_cb_f f, void *param);
 
 
 /*! \brief run all transaction callbacks for an event type */
 /*! \brief run all transaction callbacks for an event type */
-static inline void run_xmpp_callbacks( int type, char *msg)
+static inline void run_xmpp_callbacks(int type, char *msg)
 {
 {
 	xmpp_callback_t *it;
 	xmpp_callback_t *it;
 
 
-	for (it=_xmpp_cb_list->first; it; it=it->next)  {
-		if(it->types&type) {
-			LM_DBG("cb: msg=%p, callback type %d/%d fired\n",
-				msg, type, it->types );
-			it->cbf( msg, type, it->cbp );
+	for(it = _xmpp_cb_list->first; it; it = it->next) {
+		if(it->types & type) {
+			LM_DBG("cb: msg=%p, callback type %d/%d fired\n", msg, type,
+					it->types);
+			it->cbf(msg, type, it->cbp);
 		}
 		}
 	}
 	}
 }
 }
@@ -93,7 +92,7 @@ int xmpp_send_xsubscribe(str *from, str *to, str *msg, str *id);
 typedef int (*xmpp_send_xnotify_f)(str *from, str *to, str *msg, str *id);
 typedef int (*xmpp_send_xnotify_f)(str *from, str *to, str *msg, str *id);
 int xmpp_send_xnotify(str *from, str *to, str *msg, str *id);
 int xmpp_send_xnotify(str *from, str *to, str *msg, str *id);
 
 
-typedef char* (*xmpp_translate_uri_f)(char *uri);
+typedef char *(*xmpp_translate_uri_f)(char *uri);
 char *decode_uri_sip_xmpp(char *uri);
 char *decode_uri_sip_xmpp(char *uri);
 char *encode_uri_sip_xmpp(char *uri);
 char *encode_uri_sip_xmpp(char *uri);
 char *decode_uri_xmpp_sip(char *jid);
 char *decode_uri_xmpp_sip(char *jid);
@@ -112,8 +111,7 @@ typedef struct xmpp_api_
 	xmpp_translate_uri_f encode_uri_xmpp_sip;
 	xmpp_translate_uri_f encode_uri_xmpp_sip;
 } xmpp_api_t;
 } xmpp_api_t;
 
 
-typedef int (*bind_xmpp_t)(xmpp_api_t* api);
-int bind_xmpp(xmpp_api_t* api);
+typedef int (*bind_xmpp_t)(xmpp_api_t *api);
+int bind_xmpp(xmpp_api_t *api);
 
 
 #endif
 #endif
-

+ 103 - 103
src/modules/xmpp/xmpp_component.c

@@ -52,8 +52,9 @@
 #include "network.h"
 #include "network.h"
 #include "xode.h"
 #include "xode.h"
 
 
-struct xmpp_private_data {
-	int fd;		/* socket */
+struct xmpp_private_data
+{
+	int fd; /* socket */
 	int running;
 	int running;
 };
 };
 
 
@@ -61,10 +62,10 @@ static int xode_send(int fd, xode x)
 {
 {
 	char *str = xode_to_str(x);
 	char *str = xode_to_str(x);
 	int len = strlen(str);
 	int len = strlen(str);
-	
+
 	LM_DBG("xode_send [%s]\n", str);
 	LM_DBG("xode_send [%s]\n", str);
 
 
-	if (net_send(fd, str, len) != len) {
+	if(net_send(fd, str, len) != len) {
 		LM_ERR("send() error: %s\n", strerror(errno));
 		LM_ERR("send() error: %s\n", strerror(errno));
 		return -1;
 		return -1;
 	}
 	}
@@ -72,71 +73,70 @@ static int xode_send(int fd, xode x)
 	return len;
 	return len;
 }
 }
 
 
-static void stream_node_callback(int type, xode node, void *arg) 
+static void stream_node_callback(int type, xode node, void *arg)
 {
 {
-	struct xmpp_private_data *priv = (struct xmpp_private_data *) arg;
+	struct xmpp_private_data *priv = (struct xmpp_private_data *)arg;
 	char *id, *hash, *tag;
 	char *id, *hash, *tag;
 	char buf[4096];
 	char buf[4096];
 	xode x;
 	xode x;
 
 
-	LM_DBG("stream callback: %d: %s\n", type, node ? xode_get_name(node) : "n/a");
-	switch (type) {
-	case XODE_STREAM_ROOT:
-		id = xode_get_attrib(node, "id");
-		snprintf(buf, sizeof(buf), "%s%s", id, xmpp_password);
-		hash = shahash(buf);
-		
-		x = xode_new_tag("handshake");
-		xode_insert_cdata(x, hash, -1);
-		xode_send(priv->fd, x);
-		xode_free(x);
-		break;
-	case XODE_STREAM_NODE:
-		tag = xode_get_name(node);
-		if (!strcmp(tag, "handshake")) {
-			LM_DBG("handshake succeeded\n");
-		} else if (!strcmp(tag, "message")) {
-			LM_DBG("XMPP IM received\n");
-			char *from = xode_get_attrib(node, "from");
-			char *to = xode_get_attrib(node, "to");
-			char *type = xode_get_attrib(node, "type");
-			xode body = xode_get_tag(node, "body");
-			char *msg;
-			
-			if (!type)
-				type = "chat";
-			if (!strcmp(type, "error")) {	
-				LM_DBG("received message error stanza\n");
-				goto out;
-			}
-			
-			if (!from || !to || !body) {
-				LM_DBG("invalid <message/> attributes\n");
-				goto out;
-			}
+	LM_DBG("stream callback: %d: %s\n", type,
+			node ? xode_get_name(node) : "n/a");
+	switch(type) {
+		case XODE_STREAM_ROOT:
+			id = xode_get_attrib(node, "id");
+			snprintf(buf, sizeof(buf), "%s%s", id, xmpp_password);
+			hash = shahash(buf);
 
 
-			if (!(msg = xode_get_data(body)))
-				msg = "";
-			xmpp_send_sip_msg(
-				encode_uri_xmpp_sip(from),
-				decode_uri_xmpp_sip(to),
-				msg);
-		} else if (!strcmp(tag, "presence")) {
-			/* call presence callbacks */
-			LM_DBG("XMPP Presence received\n");
-			run_xmpp_callbacks(XMPP_RCV_PRESENCE, xode_to_str(node));
-		}else if (!strcmp(tag, "iq")) {
-			/* call presence callbacks */
-			LM_DBG("XMPP IQ received\n");
-			run_xmpp_callbacks(XMPP_RCV_IQ, xode_to_str(node));
-		}
-		break;
-	case XODE_STREAM_ERROR:
-		LM_ERR("stream error\n");
-		/* fall-through */
-	case XODE_STREAM_CLOSE:
-		priv->running = 0;
-		break;
+			x = xode_new_tag("handshake");
+			xode_insert_cdata(x, hash, -1);
+			xode_send(priv->fd, x);
+			xode_free(x);
+			break;
+		case XODE_STREAM_NODE:
+			tag = xode_get_name(node);
+			if(!strcmp(tag, "handshake")) {
+				LM_DBG("handshake succeeded\n");
+			} else if(!strcmp(tag, "message")) {
+				LM_DBG("XMPP IM received\n");
+				char *from = xode_get_attrib(node, "from");
+				char *to = xode_get_attrib(node, "to");
+				char *type = xode_get_attrib(node, "type");
+				xode body = xode_get_tag(node, "body");
+				char *msg;
+
+				if(!type)
+					type = "chat";
+				if(!strcmp(type, "error")) {
+					LM_DBG("received message error stanza\n");
+					goto out;
+				}
+
+				if(!from || !to || !body) {
+					LM_DBG("invalid <message/> attributes\n");
+					goto out;
+				}
+
+				if(!(msg = xode_get_data(body)))
+					msg = "";
+				xmpp_send_sip_msg(encode_uri_xmpp_sip(from),
+						decode_uri_xmpp_sip(to), msg);
+			} else if(!strcmp(tag, "presence")) {
+				/* call presence callbacks */
+				LM_DBG("XMPP Presence received\n");
+				run_xmpp_callbacks(XMPP_RCV_PRESENCE, xode_to_str(node));
+			} else if(!strcmp(tag, "iq")) {
+				/* call presence callbacks */
+				LM_DBG("XMPP IQ received\n");
+				run_xmpp_callbacks(XMPP_RCV_IQ, xode_to_str(node));
+			}
+			break;
+		case XODE_STREAM_ERROR:
+			LM_ERR("stream error\n");
+			/* fall-through */
+		case XODE_STREAM_CLOSE:
+			priv->running = 0;
+			break;
 	}
 	}
 out:
 out:
 	xode_free(node);
 	xode_free(node);
@@ -145,13 +145,13 @@ out:
 /*!
 /*!
  *
  *
  */
  */
-static int do_send_message_component(struct xmpp_private_data *priv,
-		struct xmpp_pipe_cmd *cmd)
+static int do_send_message_component(
+		struct xmpp_private_data *priv, struct xmpp_pipe_cmd *cmd)
 {
 {
 	xode x;
 	xode x;
 
 
-	LM_DBG("do_send_message_component from=[%s] to=[%s] body=[%s]\n",
-			cmd->from, cmd->to, cmd->body);
+	LM_DBG("do_send_message_component from=[%s] to=[%s] body=[%s]\n", cmd->from,
+			cmd->to, cmd->body);
 
 
 	x = xode_new_tag("message");
 	x = xode_new_tag("message");
 	xode_put_attrib(x, "id", cmd->id); // XXX
 	xode_put_attrib(x, "id", cmd->id); // XXX
@@ -159,7 +159,7 @@ static int do_send_message_component(struct xmpp_private_data *priv,
 	xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
 	xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
 	xode_put_attrib(x, "type", "chat");
 	xode_put_attrib(x, "type", "chat");
 	xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
 	xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
-			
+
 	xode_send(priv->fd, x);
 	xode_send(priv->fd, x);
 	xode_free(x);
 	xode_free(x);
 
 
@@ -167,16 +167,16 @@ static int do_send_message_component(struct xmpp_private_data *priv,
 	return 0;
 	return 0;
 }
 }
 
 
-static int do_send_bulk_message_component(struct xmpp_private_data *priv,
-		struct xmpp_pipe_cmd *cmd)
+static int do_send_bulk_message_component(
+		struct xmpp_private_data *priv, struct xmpp_pipe_cmd *cmd)
 {
 {
 	int len;
 	int len;
 
 
 	LM_DBG("do_send_bulk_message_component from=[%s] to=[%s] body=[%s]\n",
 	LM_DBG("do_send_bulk_message_component from=[%s] to=[%s] body=[%s]\n",
 			cmd->from, cmd->to, cmd->body);
 			cmd->from, cmd->to, cmd->body);
 	len = strlen(cmd->body);
 	len = strlen(cmd->body);
-	if (net_send(priv->fd, cmd->body, len) != len) {
-		LM_ERR("do_send_bulk_message_component: %s\n",strerror(errno));
+	if(net_send(priv->fd, cmd->body, len) != len) {
+		LM_ERR("do_send_bulk_message_component: %s\n", strerror(errno));
 		return -1;
 		return -1;
 	}
 	}
 	return 0;
 	return 0;
@@ -191,74 +191,74 @@ int xmpp_component_child_process(int data_pipe)
 	xode_stream stream;
 	xode_stream stream;
 	struct xmpp_private_data priv;
 	struct xmpp_private_data priv;
 	struct xmpp_pipe_cmd *cmd;
 	struct xmpp_pipe_cmd *cmd;
-	
-	while (1) {
+
+	while(1) {
 		fd = net_connect(xmpp_host, xmpp_port);
 		fd = net_connect(xmpp_host, xmpp_port);
-		if (fd < 0) {
+		if(fd < 0) {
 			sleep(3);
 			sleep(3);
 			continue;
 			continue;
 		}
 		}
-		
+
 		priv.fd = fd;
 		priv.fd = fd;
 		priv.running = 1;
 		priv.running = 1;
-		
+
 		pool = xode_pool_new();
 		pool = xode_pool_new();
 		stream = xode_stream_new(pool, stream_node_callback, &priv);
 		stream = xode_stream_new(pool, stream_node_callback, &priv);
-		
+
 		net_printf(fd,
 		net_printf(fd,
-			"<?xml version='1.0'?>"
-			"<stream:stream xmlns='jabber:component:accept' to='%s' "
-			"version='1.0' xmlns:stream='http://etherx.jabber.org/streams'>",
-			xmpp_domain);
-		
-		while (priv.running) {
+				"<?xml version='1.0'?>"
+				"<stream:stream xmlns='jabber:component:accept' to='%s' "
+				"version='1.0' "
+				"xmlns:stream='http://etherx.jabber.org/streams'>",
+				xmpp_domain);
+
+		while(priv.running) {
 			FD_ZERO(&fdset);
 			FD_ZERO(&fdset);
 			FD_SET(data_pipe, &fdset);
 			FD_SET(data_pipe, &fdset);
 			FD_SET(fd, &fdset);
 			FD_SET(fd, &fdset);
 			maxfd = fd > data_pipe ? fd : data_pipe;
 			maxfd = fd > data_pipe ? fd : data_pipe;
 			rv = select(maxfd + 1, &fdset, NULL, NULL, NULL);
 			rv = select(maxfd + 1, &fdset, NULL, NULL, NULL);
-			
+
 			/* update the local config framework structures */
 			/* update the local config framework structures */
 			cfg_update();
 			cfg_update();
 
 
-			if (rv < 0) {
+			if(rv < 0) {
 				LM_ERR("select() failed: %s\n", strerror(errno));
 				LM_ERR("select() failed: %s\n", strerror(errno));
-			} else if (!rv) {
+			} else if(!rv) {
 				/* timeout */
 				/* timeout */
-			} else if (FD_ISSET(fd, &fdset)) {
+			} else if(FD_ISSET(fd, &fdset)) {
 				char *buf = net_read_static(fd);
 				char *buf = net_read_static(fd);
 
 
-				if (!buf)
+				if(!buf)
 					/* connection closed */
 					/* connection closed */
 					break;
 					break;
 
 
 				LM_DBG("server read\n[%s]\n", buf);
 				LM_DBG("server read\n[%s]\n", buf);
 				xode_stream_eat(stream, buf, strlen(buf));
 				xode_stream_eat(stream, buf, strlen(buf));
-			} else if (FD_ISSET(data_pipe, &fdset)) {
-				if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
+			} else if(FD_ISSET(data_pipe, &fdset)) {
+				if(read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
 					LM_ERR("failed to read from command pipe: %s\n",
 					LM_ERR("failed to read from command pipe: %s\n",
 							strerror(errno));
 							strerror(errno));
 				} else {
 				} else {
 					LM_DBG("got pipe cmd %d\n", cmd->type);
 					LM_DBG("got pipe cmd %d\n", cmd->type);
-					switch (cmd->type) {
-					case XMPP_PIPE_SEND_MESSAGE:
-						do_send_message_component(&priv, cmd);
-						break;
-					case XMPP_PIPE_SEND_PACKET:
-					case XMPP_PIPE_SEND_PSUBSCRIBE:
-					case XMPP_PIPE_SEND_PNOTIFY:
-						do_send_bulk_message_component(&priv, cmd);
-						break;
+					switch(cmd->type) {
+						case XMPP_PIPE_SEND_MESSAGE:
+							do_send_message_component(&priv, cmd);
+							break;
+						case XMPP_PIPE_SEND_PACKET:
+						case XMPP_PIPE_SEND_PSUBSCRIBE:
+						case XMPP_PIPE_SEND_PNOTIFY:
+							do_send_bulk_message_component(&priv, cmd);
+							break;
 					}
 					}
 					xmpp_free_pipe_cmd(cmd);
 					xmpp_free_pipe_cmd(cmd);
 				}
 				}
 			}
 			}
 		}
 		}
-		
+
 		xode_pool_free(pool);
 		xode_pool_free(pool);
-		
+
 		close(fd);
 		close(fd);
 	}
 	}
 	return 0;
 	return 0;
 }
 }
-

+ 239 - 224
src/modules/xmpp/xmpp_server.c

@@ -98,20 +98,22 @@
 #include <arpa/inet.h>
 #include <arpa/inet.h>
 
 
 /* XXX hack */
 /* XXX hack */
-#define DB_KEY	"this-be-a-random-key"
+#define DB_KEY "this-be-a-random-key"
 
 
-#define CONN_DEAD	0
-#define CONN_INBOUND	1
-#define CONN_OUTBOUND	2
+#define CONN_DEAD 0
+#define CONN_INBOUND 1
+#define CONN_OUTBOUND 2
 
 
-struct xmpp_private_data {
-	int fd;		/* outgoing stream socket */
-	int listen_fd;	/* listening socket */
-	int in_fd;	/* incoming stream socket */
+struct xmpp_private_data
+{
+	int fd;		   /* outgoing stream socket */
+	int listen_fd; /* listening socket */
+	int in_fd;	   /* incoming stream socket */
 	int running;
 	int running;
 };
 };
 
 
-struct xmpp_connection {
+struct xmpp_connection
+{
 	struct xmpp_connection *next;
 	struct xmpp_connection *next;
 
 
 	char *domain;
 	char *domain;
@@ -120,10 +122,12 @@ struct xmpp_connection {
 	char *stream_id;
 	char *stream_id;
 	xode_pool pool;
 	xode_pool pool;
 	xode_stream stream;
 	xode_stream stream;
-	xode todo;	/* backlog of outgoing messages, if any */
+	xode todo; /* backlog of outgoing messages, if any */
 };
 };
 
 
-static char local_secret[64] = { 0, };
+static char local_secret[64] = {
+		0,
+};
 
 
 static void in_stream_node_callback(int type, xode node, void *arg);
 static void in_stream_node_callback(int type, xode node, void *arg);
 static void out_stream_node_callback(int type, xode node, void *arg);
 static void out_stream_node_callback(int type, xode node, void *arg);
@@ -133,15 +137,14 @@ static struct xmpp_connection *conn_list = NULL;
 static struct xmpp_connection *conn_new(int type, int fd, char *domain)
 static struct xmpp_connection *conn_new(int type, int fd, char *domain)
 {
 {
 	struct xmpp_connection *conn = NULL;
 	struct xmpp_connection *conn = NULL;
-	
+
 	conn = malloc(sizeof(struct xmpp_connection));
 	conn = malloc(sizeof(struct xmpp_connection));
 
 
-	if(conn==NULL) 
-	{
+	if(conn == NULL) {
 		LM_ERR("out of memory\n");
 		LM_ERR("out of memory\n");
 		return NULL;
 		return NULL;
 	}
 	}
-	
+
 	memset(conn, 0, sizeof(struct xmpp_connection));
 	memset(conn, 0, sizeof(struct xmpp_connection));
 	conn->domain = domain ? strdup(domain) : NULL;
 	conn->domain = domain ? strdup(domain) : NULL;
 	conn->type = type;
 	conn->type = type;
@@ -150,9 +153,10 @@ static struct xmpp_connection *conn_new(int type, int fd, char *domain)
 
 
 	conn->pool = xode_pool_new();
 	conn->pool = xode_pool_new();
 	conn->stream = xode_stream_new(conn->pool,
 	conn->stream = xode_stream_new(conn->pool,
-		(type==CONN_INBOUND)?in_stream_node_callback:out_stream_node_callback,
-		conn);
-	
+			(type == CONN_INBOUND) ? in_stream_node_callback
+								   : out_stream_node_callback,
+			conn);
+
 	conn->next = conn_list;
 	conn->next = conn_list;
 	conn_list = conn;
 	conn_list = conn;
 	return conn;
 	return conn;
@@ -161,24 +165,24 @@ static struct xmpp_connection *conn_new(int type, int fd, char *domain)
 static void conn_free(struct xmpp_connection *conn)
 static void conn_free(struct xmpp_connection *conn)
 {
 {
 	struct xmpp_connection **last_p, *link;
 	struct xmpp_connection **last_p, *link;
-	
+
 	last_p = &conn_list;
 	last_p = &conn_list;
-	for (link = conn_list; link; link = link->next) {
-		if (link == conn) {
+	for(link = conn_list; link; link = link->next) {
+		if(link == conn) {
 			*last_p = link->next;
 			*last_p = link->next;
 			break;
 			break;
 		}
 		}
 		last_p = &link->next;
 		last_p = &link->next;
 	}
 	}
 
 
-	if (conn->todo)
+	if(conn->todo)
 		xode_free(conn->todo);
 		xode_free(conn->todo);
 	xode_pool_free(conn->pool);
 	xode_pool_free(conn->pool);
-	if (conn->fd != -1)
+	if(conn->fd != -1)
 		close(conn->fd);
 		close(conn->fd);
-	if (conn->stream_id)
+	if(conn->stream_id)
 		free(conn->stream_id);
 		free(conn->stream_id);
-	if (conn->domain)
+	if(conn->domain)
 		free(conn->domain);
 		free(conn->domain);
 	free(conn);
 	free(conn);
 }
 }
@@ -186,9 +190,9 @@ static void conn_free(struct xmpp_connection *conn)
 static struct xmpp_connection *conn_find_domain(char *domain, int type)
 static struct xmpp_connection *conn_find_domain(char *domain, int type)
 {
 {
 	struct xmpp_connection *conn;
 	struct xmpp_connection *conn;
-	
-	for (conn = conn_list; conn; conn = conn->next)
-		if (conn->domain && !strcasecmp(conn->domain, domain) 
+
+	for(conn = conn_list; conn; conn = conn->next)
+		if(conn->domain && !strcasecmp(conn->domain, domain)
 				&& conn->type == type)
 				&& conn->type == type)
 			return conn;
 			return conn;
 	return NULL;
 	return NULL;
@@ -212,10 +216,10 @@ static int xode_send(int fd, xode x)
 {
 {
 	char *str = xode_to_str(x);
 	char *str = xode_to_str(x);
 	int len = strlen(str);
 	int len = strlen(str);
-	
+
 	LM_DBG("xode_send->%d [%s]\n", fd, str);
 	LM_DBG("xode_send->%d [%s]\n", fd, str);
 
 
-	if (net_send(fd, str, len) != len) {
+	if(net_send(fd, str, len) != len) {
 		LM_ERR("send() failed: %s\n", strerror(errno));
 		LM_ERR("send() failed: %s\n", strerror(errno));
 		return -1;
 		return -1;
 	}
 	}
@@ -226,11 +230,11 @@ static int xode_send_domain(char *domain, xode x)
 {
 {
 	struct xmpp_connection *conn;
 	struct xmpp_connection *conn;
 
 
-	if ((conn = conn_find_domain(domain, CONN_OUTBOUND))) {
+	if((conn = conn_find_domain(domain, CONN_OUTBOUND))) {
 		xode_send(conn->fd, x);
 		xode_send(conn->fd, x);
 		xode_free(x);
 		xode_free(x);
 	} else {
 	} else {
-		if((conn = conn_new(CONN_OUTBOUND, -1, domain))==0)
+		if((conn = conn_new(CONN_OUTBOUND, -1, domain)) == 0)
 			return -1;
 			return -1;
 		xode_insert_node(conn->todo, x);
 		xode_insert_node(conn->todo, x);
 	}
 	}
@@ -239,188 +243,196 @@ static int xode_send_domain(char *domain, xode x)
 
 
 static void out_stream_node_callback(int type, xode node, void *arg)
 static void out_stream_node_callback(int type, xode node, void *arg)
 {
 {
-	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
+	struct xmpp_connection *conn = (struct xmpp_connection *)arg;
 	struct xmpp_connection *in_conn = NULL;
 	struct xmpp_connection *in_conn = NULL;
 	char *tag;
 	char *tag;
 	xode x;
 	xode x;
 
 
-	LM_DBG("outstream callback: %d: %s\n", type, 
-			node?xode_get_name(node):"n/a");
+	LM_DBG("outstream callback: %d: %s\n", type,
+			node ? xode_get_name(node) : "n/a");
 
 
-	if (conn->domain)
+	if(conn->domain)
 		in_conn = conn_find_domain(conn->domain, CONN_INBOUND);
 		in_conn = conn_find_domain(conn->domain, CONN_INBOUND);
 
 
-	switch (type) {
-	case XODE_STREAM_ROOT:
-		x = xode_new_tag("db:result");
-		xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
-		xode_put_attrib(x, "from", xmpp_domain);
-		xode_put_attrib(x, "to", conn->domain);
-		//xode_insert_cdata(x, DB_KEY, -1);
-		xode_insert_cdata(x, db_key(local_secret, conn->domain,
-					xode_get_attrib(node, "id")), -1);
-		xode_send(conn->fd, x);
-		xode_free(x);
+	switch(type) {
+		case XODE_STREAM_ROOT:
+			x = xode_new_tag("db:result");
+			xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
+			xode_put_attrib(x, "from", xmpp_domain);
+			xode_put_attrib(x, "to", conn->domain);
+			//xode_insert_cdata(x, DB_KEY, -1);
+			xode_insert_cdata(x,
+					db_key(local_secret, conn->domain,
+							xode_get_attrib(node, "id")),
+					-1);
+			xode_send(conn->fd, x);
+			xode_free(x);
 
 
-		break;
-	case XODE_STREAM_NODE:
-		tag = xode_get_name(node);
-
-		if (!strcmp(tag, "db:verify")) {
-			char *from = xode_get_attrib(node, "from");
-			char *to = xode_get_attrib(node, "to");
-			char *id = xode_get_attrib(node, "id");
-			char *type = xode_get_attrib(node, "type");
-			/* char *cdata = xode_get_data(node); */
-			
-			if (!strcmp(type, "valid") || !strcmp(type, "invalid")) {
-				/* got a reply, report it */
-				x = xode_new_tag("db:result");
-				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
-				xode_put_attrib(x, "from", to);
-				xode_put_attrib(x, "to", from);
-				xode_put_attrib(x, "id", id);
-				xode_put_attrib(x, "type", type);
-				if (in_conn)
-					xode_send(in_conn->fd, x);
-				else
-					LM_ERR("need to send reply to domain '%s', but no inbound"
-							" connection found\n", from);
-				xode_free(x);
-			}
-		} else if (!strcmp(tag, "db:result")) {
-			char *type = xode_get_attrib(node, "type");
-			
-			if (type && !strcmp(type, "valid")) {
-				/* the remote server has successfully authenticated us,
+			break;
+		case XODE_STREAM_NODE:
+			tag = xode_get_name(node);
+
+			if(!strcmp(tag, "db:verify")) {
+				char *from = xode_get_attrib(node, "from");
+				char *to = xode_get_attrib(node, "to");
+				char *id = xode_get_attrib(node, "id");
+				char *type = xode_get_attrib(node, "type");
+				/* char *cdata = xode_get_data(node); */
+
+				if(!strcmp(type, "valid") || !strcmp(type, "invalid")) {
+					/* got a reply, report it */
+					x = xode_new_tag("db:result");
+					xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
+					xode_put_attrib(x, "from", to);
+					xode_put_attrib(x, "to", from);
+					xode_put_attrib(x, "id", id);
+					xode_put_attrib(x, "type", type);
+					if(in_conn)
+						xode_send(in_conn->fd, x);
+					else
+						LM_ERR("need to send reply to domain '%s', but no "
+							   "inbound"
+							   " connection found\n",
+								from);
+					xode_free(x);
+				}
+			} else if(!strcmp(tag, "db:result")) {
+				char *type = xode_get_attrib(node, "type");
+
+				if(type && !strcmp(type, "valid")) {
+					/* the remote server has successfully authenticated us,
 				 * we can now send data */
 				 * we can now send data */
-				for (x = xode_get_firstchild(conn->todo); x;
-						x = xode_get_nextsibling(x)) {
-					LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
-					xode_send(conn->fd, x);
+					for(x = xode_get_firstchild(conn->todo); x;
+							x = xode_get_nextsibling(x)) {
+						LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
+						xode_send(conn->fd, x);
+					}
+					xode_free(conn->todo);
+					conn->todo = NULL;
 				}
 				}
-				xode_free(conn->todo);
-				conn->todo = NULL;
 			}
 			}
-		}
-		break;
-	case XODE_STREAM_ERROR:
-		LM_ERR("outstream error\n");
-		/* fall-through */
-	case XODE_STREAM_CLOSE:
-		conn->type = CONN_DEAD;
-		break;
+			break;
+		case XODE_STREAM_ERROR:
+			LM_ERR("outstream error\n");
+			/* fall-through */
+		case XODE_STREAM_CLOSE:
+			conn->type = CONN_DEAD;
+			break;
 	}
 	}
 	xode_free(node);
 	xode_free(node);
 }
 }
 
 
-static void in_stream_node_callback(int type, xode node, void *arg) 
+static void in_stream_node_callback(int type, xode node, void *arg)
 {
 {
-	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
+	struct xmpp_connection *conn = (struct xmpp_connection *)arg;
 	char *tag;
 	char *tag;
 	xode x;
 	xode x;
 
 
-	LM_DBG("instream callback: %d: %s\n",
-			type, node ? xode_get_name(node) : "n/a");
-	switch (type) {
-	case XODE_STREAM_ROOT:
-		conn->stream_id = strdup(random_secret());
-		net_printf(conn->fd,
-			"<?xml version='1.0'?>"
-			"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0'"
-			" xmlns:db='jabber:server:dialback' id='%s' from='%s'>", conn->stream_id, xmpp_domain);
-		net_printf(conn->fd,"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
-		break;
-	case XODE_STREAM_NODE:
-		tag = xode_get_name(node);
-
-		if (!strcmp(tag, "db:result")) {
-			char *from = xode_get_attrib(node, "from");
-			char *to = xode_get_attrib(node, "to");
-			/* char *id = xode_get_attrib(node, "id"); */
-			char *type = xode_get_attrib(node, "type");
-			char *cdata = xode_get_data(node);
-			
-			if (!type) {
-				if (conn->domain) {
-					LM_DBG("connection %d has old domain '%s'\n",conn->fd,
+	LM_DBG("instream callback: %d: %s\n", type,
+			node ? xode_get_name(node) : "n/a");
+	switch(type) {
+		case XODE_STREAM_ROOT:
+			conn->stream_id = strdup(random_secret());
+			net_printf(conn->fd,
+					"<?xml version='1.0'?>"
+					"<stream:stream "
+					"xmlns:stream='http://etherx.jabber.org/streams' "
+					"xmlns='jabber:server' version='1.0'"
+					" xmlns:db='jabber:server:dialback' id='%s' from='%s'>",
+					conn->stream_id, xmpp_domain);
+			net_printf(conn->fd,
+					"<stream:features "
+					"xmlns:stream='http://etherx.jabber.org/streams'/>");
+			break;
+		case XODE_STREAM_NODE:
+			tag = xode_get_name(node);
+
+			if(!strcmp(tag, "db:result")) {
+				char *from = xode_get_attrib(node, "from");
+				char *to = xode_get_attrib(node, "to");
+				/* char *id = xode_get_attrib(node, "id"); */
+				char *type = xode_get_attrib(node, "type");
+				char *cdata = xode_get_data(node);
+
+				if(!type) {
+					if(conn->domain) {
+						LM_DBG("connection %d has old domain '%s'\n", conn->fd,
+								conn->domain);
+						free(conn->domain);
+					}
+					conn->domain = strdup(from);
+					LM_DBG("connection %d set domain '%s'\n", conn->fd,
 							conn->domain);
 							conn->domain);
-					free(conn->domain);
+
+					/* it's a request; send verification over outgoing connection */
+					x = xode_new_tag("db:verify");
+					xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
+					xode_put_attrib(x, "from", to);
+					xode_put_attrib(x, "to", from);
+					//xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
+					xode_put_attrib(x, "id", conn->stream_id);
+					xode_insert_cdata(x, cdata, -1);
+					xode_send_domain(from, x);
 				}
 				}
-				conn->domain = strdup(from);
-				LM_DBG("connection %d set domain '%s'\n",
-						conn->fd, conn->domain);
-
-				/* it's a request; send verification over outgoing connection */
-				x = xode_new_tag("db:verify");
-				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
-				xode_put_attrib(x, "from", to);
-				xode_put_attrib(x, "to", from);
-				//xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
-				xode_put_attrib(x, "id", conn->stream_id);
-				xode_insert_cdata(x, cdata, -1);
-				xode_send_domain(from, x);
-			}			
-		} else if (!strcmp(tag, "db:verify")) {
-			char *from = xode_get_attrib(node, "from");
-			char *to = xode_get_attrib(node, "to");
-			char *id = xode_get_attrib(node, "id");
-			char *type = xode_get_attrib(node, "type");
-			char *cdata = xode_get_data(node);
-			
-			if (!type) {
-				/* it's a request */
-				x = xode_new_tag("db:verify");
-				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
-				xode_put_attrib(x, "from", to);
-				xode_put_attrib(x, "to", from);
-				xode_put_attrib(x, "id", id);
-				//if (cdata && !strcmp(cdata, DB_KEY)) {
-				if (cdata && !strcmp(cdata, db_key(local_secret, from, id))) {
-					xode_put_attrib(x, "type", "valid");
-				} else {
-					xode_put_attrib(x, "type", "invalid");
+			} else if(!strcmp(tag, "db:verify")) {
+				char *from = xode_get_attrib(node, "from");
+				char *to = xode_get_attrib(node, "to");
+				char *id = xode_get_attrib(node, "id");
+				char *type = xode_get_attrib(node, "type");
+				char *cdata = xode_get_data(node);
+
+				if(!type) {
+					/* it's a request */
+					x = xode_new_tag("db:verify");
+					xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
+					xode_put_attrib(x, "from", to);
+					xode_put_attrib(x, "to", from);
+					xode_put_attrib(x, "id", id);
+					//if (cdata && !strcmp(cdata, DB_KEY)) {
+					if(cdata
+							&& !strcmp(cdata, db_key(local_secret, from, id))) {
+						xode_put_attrib(x, "type", "valid");
+					} else {
+						xode_put_attrib(x, "type", "invalid");
+					}
+					xode_send(conn->fd, x);
+					xode_free(x);
 				}
 				}
-				xode_send(conn->fd, x);
-				xode_free(x);
-			}			
-		} else if (!strcmp(tag, "message")) {
-			char *from = xode_get_attrib(node, "from");
-			char *to = xode_get_attrib(node, "to");
-			char *type = xode_get_attrib(node, "type");
-			xode body = xode_get_tag(node, "body");
-			char *msg;
-			
-			if (!type)
-				type = "chat";
-			if (!strcmp(type, "error")) {	
-				LM_DBG("received message error stanza\n");
-				goto out;
-			}
-			
-			if (!from || !to || !body) {
-				LM_DBG("invalid <message/> attributes\n");
-				goto out;
+			} else if(!strcmp(tag, "message")) {
+				char *from = xode_get_attrib(node, "from");
+				char *to = xode_get_attrib(node, "to");
+				char *type = xode_get_attrib(node, "type");
+				xode body = xode_get_tag(node, "body");
+				char *msg;
+
+				if(!type)
+					type = "chat";
+				if(!strcmp(type, "error")) {
+					LM_DBG("received message error stanza\n");
+					goto out;
+				}
+
+				if(!from || !to || !body) {
+					LM_DBG("invalid <message/> attributes\n");
+					goto out;
+				}
+
+				if(!(msg = xode_get_data(body)))
+					msg = "";
+				xmpp_send_sip_msg(encode_uri_xmpp_sip(from),
+						decode_uri_xmpp_sip(to), msg);
+			} else if(!strcmp(tag, "presence")) {
+				/* run presence callbacks */
 			}
 			}
+			break;
 
 
-			if (!(msg = xode_get_data(body)))
-				msg = "";
-			xmpp_send_sip_msg(
-				encode_uri_xmpp_sip(from),
-				decode_uri_xmpp_sip(to),
-				msg);
-		} else if (!strcmp(tag, "presence")) {
-			/* run presence callbacks */
-		}
-		break;
-
-		break;
-	case XODE_STREAM_ERROR:
-		LM_ERR("instream error\n");
-		/* fall-through */
-	case XODE_STREAM_CLOSE:
-		conn->type = CONN_DEAD;
-		break;
+			break;
+		case XODE_STREAM_ERROR:
+			LM_ERR("instream error\n");
+			/* fall-through */
+		case XODE_STREAM_CLOSE:
+			conn->type = CONN_DEAD;
+			break;
 	}
 	}
 out:
 out:
 	xode_free(node);
 	xode_free(node);
@@ -431,7 +443,7 @@ static void do_send_message_server(struct xmpp_pipe_cmd *cmd)
 	char *domain;
 	char *domain;
 	xode x;
 	xode x;
 
 
-	LM_DBG("rom=[%s] to=[%s] body=[%s]\n", cmd->from,cmd->to, cmd->body);
+	LM_DBG("rom=[%s] to=[%s] body=[%s]\n", cmd->from, cmd->to, cmd->body);
 
 
 	x = xode_new_tag("message");
 	x = xode_new_tag("message");
 	xode_put_attrib(x, "xmlns", "jabber:client");
 	xode_put_attrib(x, "xmlns", "jabber:client");
@@ -454,21 +466,21 @@ int xmpp_server_child_process(int data_pipe)
 
 
 	snprintf(local_secret, sizeof(local_secret), "%s", random_secret());
 	snprintf(local_secret, sizeof(local_secret), "%s", random_secret());
 
 
-	while ((listen_fd = net_listen(xmpp_domain, xmpp_port)) < 0) {
+	while((listen_fd = net_listen(xmpp_domain, xmpp_port)) < 0) {
 		/* ugh. */
 		/* ugh. */
 		sleep(3);
 		sleep(3);
 	}
 	}
 
 
-	while (1) {
+	while(1) {
 		FD_ZERO(&fdset);
 		FD_ZERO(&fdset);
 		FD_SET(data_pipe, &fdset);
 		FD_SET(data_pipe, &fdset);
 		FD_SET(listen_fd, &fdset);
 		FD_SET(listen_fd, &fdset);
 
 
 		/* check for dead connections */
 		/* check for dead connections */
-		for (conn = conn_list; conn; ) {
+		for(conn = conn_list; conn;) {
 			struct xmpp_connection *next = conn->next;
 			struct xmpp_connection *next = conn->next;
 
 
-			if (conn->type == CONN_DEAD) {
+			if(conn->type == CONN_DEAD) {
 				if(conn == conn_list) {
 				if(conn == conn_list) {
 					conn_list = next;
 					conn_list = next;
 				}
 				}
@@ -477,24 +489,27 @@ int xmpp_server_child_process(int data_pipe)
 			conn = next;
 			conn = next;
 		}
 		}
 
 
-		for (conn = conn_list; conn; conn = conn->next) {
+		for(conn = conn_list; conn; conn = conn->next) {
 			/* check if we need to set up a connection */
 			/* check if we need to set up a connection */
-			if (conn->type == CONN_OUTBOUND && conn->fd == -1) {
-				if ((conn->fd = net_connect(conn->domain, xmpp_port)) >= 0)
-				{
+			if(conn->type == CONN_OUTBOUND && conn->fd == -1) {
+				if((conn->fd = net_connect(conn->domain, xmpp_port)) >= 0) {
 					net_printf(conn->fd,
 					net_printf(conn->fd,
-						"<?xml version='1.0'?>"
-						"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0' "
-						"xmlns:db='jabber:server:dialback' to='%s' from='%s'>",
-						conn->domain, xmpp_domain);
-					net_printf(conn->fd,
-                           "<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
+							"<?xml version='1.0'?>"
+							"<stream:stream "
+							"xmlns:stream='http://etherx.jabber.org/streams' "
+							"xmlns='jabber:server' version='1.0' "
+							"xmlns:db='jabber:server:dialback' to='%s' "
+							"from='%s'>",
+							conn->domain, xmpp_domain);
+					net_printf(conn->fd, "<stream:features "
+										 "xmlns:stream='http://"
+										 "etherx.jabber.org/streams'/>");
 				} else {
 				} else {
 					conn->type = CONN_DEAD;
 					conn->type = CONN_DEAD;
 				}
 				}
 			}
 			}
 
 
-			if (conn->fd != -1)
+			if(conn->fd != -1)
 				FD_SET(conn->fd, &fdset);
 				FD_SET(conn->fd, &fdset);
 		}
 		}
 
 
@@ -503,15 +518,15 @@ int xmpp_server_child_process(int data_pipe)
 		/* update the local config framework structures */
 		/* update the local config framework structures */
 		cfg_update();
 		cfg_update();
 
 
-		if (rv < 0) {
+		if(rv < 0) {
 			LM_ERR("select() failed: %s\n", strerror(errno));
 			LM_ERR("select() failed: %s\n", strerror(errno));
-		} else if (!rv) {
+		} else if(!rv) {
 			/* timeout */
 			/* timeout */
 		} else {
 		} else {
-			for (conn = conn_list; conn; conn = conn->next) {
-				if (conn->fd != -1 && FD_ISSET(conn->fd, &fdset)) {
+			for(conn = conn_list; conn; conn = conn->next) {
+				if(conn->fd != -1 && FD_ISSET(conn->fd, &fdset)) {
 					char *buf = net_read_static(conn->fd);
 					char *buf = net_read_static(conn->fd);
-					if (!buf) {
+					if(!buf) {
 						conn->type = CONN_DEAD;
 						conn->type = CONN_DEAD;
 					} else {
 					} else {
 						LM_DBG("stream (fd %d, domain '%s') read\n[%s]\n",
 						LM_DBG("stream (fd %d, domain '%s') read\n[%s]\n",
@@ -521,12 +536,13 @@ int xmpp_server_child_process(int data_pipe)
 				}
 				}
 			}
 			}
 
 
-			if (FD_ISSET(listen_fd, &fdset)) {
+			if(FD_ISSET(listen_fd, &fdset)) {
 				struct sockaddr_in sin;
 				struct sockaddr_in sin;
 				unsigned int len = sizeof(sin);
 				unsigned int len = sizeof(sin);
 				int fd;
 				int fd;
 
 
-				if ((fd = accept(listen_fd,(struct sockaddr*)&sin, &len))<0) {
+				if((fd = accept(listen_fd, (struct sockaddr *)&sin, &len))
+						< 0) {
 					LM_ERR("accept() failed: %s\n", strerror(errno));
 					LM_ERR("accept() failed: %s\n", strerror(errno));
 				} else {
 				} else {
 					LM_DBG("accept()ed connection from %s:%d\n",
 					LM_DBG("accept()ed connection from %s:%d\n",
@@ -535,22 +551,22 @@ int xmpp_server_child_process(int data_pipe)
 				}
 				}
 			}
 			}
 
 
-			if (FD_ISSET(data_pipe, &fdset)) {
+			if(FD_ISSET(data_pipe, &fdset)) {
 				struct xmpp_pipe_cmd *cmd;
 				struct xmpp_pipe_cmd *cmd;
 
 
-				if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
+				if(read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
 					LM_ERR("failed to read from command pipe: %s\n",
 					LM_ERR("failed to read from command pipe: %s\n",
 							strerror(errno));
 							strerror(errno));
 				} else {
 				} else {
 					LM_DBG("got pipe cmd %d\n", cmd->type);
 					LM_DBG("got pipe cmd %d\n", cmd->type);
-					switch (cmd->type) {
-					case XMPP_PIPE_SEND_MESSAGE:
-						do_send_message_server(cmd);
-						break;
-					case XMPP_PIPE_SEND_PACKET:
-					case XMPP_PIPE_SEND_PSUBSCRIBE:
-					case XMPP_PIPE_SEND_PNOTIFY:
-						break;
+					switch(cmd->type) {
+						case XMPP_PIPE_SEND_MESSAGE:
+							do_send_message_server(cmd);
+							break;
+						case XMPP_PIPE_SEND_PACKET:
+						case XMPP_PIPE_SEND_PSUBSCRIBE:
+						case XMPP_PIPE_SEND_PNOTIFY:
+							break;
 					}
 					}
 					xmpp_free_pipe_cmd(cmd);
 					xmpp_free_pipe_cmd(cmd);
 				}
 				}
@@ -559,4 +575,3 @@ int xmpp_server_child_process(int data_pipe)
 	}
 	}
 	return 0;
 	return 0;
 }
 }
-

+ 506 - 521
src/modules/xmpp/xode.c

@@ -25,193 +25,184 @@
 
 
 static int _xode_strcmp(const char *a, const char *b)
 static int _xode_strcmp(const char *a, const char *b)
 {
 {
-    if(a == NULL || b == NULL) return -1;
+	if(a == NULL || b == NULL)
+		return -1;
 
 
-    return strcmp(a,b);
+	return strcmp(a, b);
 }
 }
 
 
 /*! \brief Internal routines */
 /*! \brief Internal routines */
-static xode _xode_new(xode_pool p, const char* name, unsigned int type)
+static xode _xode_new(xode_pool p, const char *name, unsigned int type)
 {
 {
-    xode result = NULL;
-    if (type > XODE_TYPE_LAST)
-        return NULL;
+	xode result = NULL;
+	if(type > XODE_TYPE_LAST)
+		return NULL;
 
 
-    if (type != XODE_TYPE_CDATA && name == NULL)
-        return NULL;
+	if(type != XODE_TYPE_CDATA && name == NULL)
+		return NULL;
 
 
-    if (p == NULL)
-    {
-        p = xode_pool_heap(1*1024);
-    }
+	if(p == NULL) {
+		p = xode_pool_heap(1 * 1024);
+	}
 
 
-    /* Allocate & zero memory */
-    result = (xode)xode_pool_malloc(p, sizeof(_xode));
-    memset(result, '\0', sizeof(_xode));
+	/* Allocate & zero memory */
+	result = (xode)xode_pool_malloc(p, sizeof(_xode));
+	memset(result, '\0', sizeof(_xode));
 
 
-    /* Initialize fields */
-    if (type != XODE_TYPE_CDATA)
-        result->name = xode_pool_strdup(p,name);
-    result->type = type;
-    result->p = p;
-    return result;
+	/* Initialize fields */
+	if(type != XODE_TYPE_CDATA)
+		result->name = xode_pool_strdup(p, name);
+	result->type = type;
+	result->p = p;
+	return result;
 }
 }
 
 
-static xode _xode_appendsibling(xode lastsibling, const char* name, unsigned int type)
+static xode _xode_appendsibling(
+		xode lastsibling, const char *name, unsigned int type)
 {
 {
-    xode result;
+	xode result;
 
 
-    result = _xode_new(xode_get_pool(lastsibling), name, type);
-    if (result != NULL)
-    {
-        /* Setup sibling pointers */
-        result->prev = lastsibling;
-        lastsibling->next = result;
-    }
-    return result;
+	result = _xode_new(xode_get_pool(lastsibling), name, type);
+	if(result != NULL) {
+		/* Setup sibling pointers */
+		result->prev = lastsibling;
+		lastsibling->next = result;
+	}
+	return result;
 }
 }
 
 
-static xode _xode_insert(xode parent, const char* name, unsigned int type)
+static xode _xode_insert(xode parent, const char *name, unsigned int type)
 {
 {
-    xode result;
-
-    if(parent == NULL || name == NULL) return NULL;
+	xode result;
 
 
-    /* If parent->firstchild is NULL, simply create a new node for the first child */
-    if (parent->firstchild == NULL)
-    {
-        result = _xode_new(parent->p, name, type);
-        parent->firstchild = result;
-    }
-    /* Otherwise, append this to the lastchild */
-    else
-    {
-        result= _xode_appendsibling(parent->lastchild, name, type);
-    }
-    result->parent = parent;
-    parent->lastchild = result;
-    return result;
+	if(parent == NULL || name == NULL)
+		return NULL;
 
 
+	/* If parent->firstchild is NULL, simply create a new node for the first child */
+	if(parent->firstchild == NULL) {
+		result = _xode_new(parent->p, name, type);
+		parent->firstchild = result;
+	}
+	/* Otherwise, append this to the lastchild */
+	else {
+		result = _xode_appendsibling(parent->lastchild, name, type);
+	}
+	result->parent = parent;
+	parent->lastchild = result;
+	return result;
 }
 }
 
 
-static xode _xode_search(xode firstsibling, const char* name, unsigned int type)
+static xode _xode_search(xode firstsibling, const char *name, unsigned int type)
 {
 {
-    xode current;
+	xode current;
 
 
-    /* Walk the sibling list, looking for a XODE_TYPE_TAG xode with
+	/* Walk the sibling list, looking for a XODE_TYPE_TAG xode with
     the specified name */
     the specified name */
-    current = firstsibling;
-    while (current != NULL)
-    {
-        if (name != NULL && (current->type == type) && (_xode_strcmp(current->name, name) == 0))
-            return current;
-        else
-            current = current->next;
-    }
-    return NULL;
+	current = firstsibling;
+	while(current != NULL) {
+		if(name != NULL && (current->type == type)
+				&& (_xode_strcmp(current->name, name) == 0))
+			return current;
+		else
+			current = current->next;
+	}
+	return NULL;
 }
 }
 
 
-static char* _xode_merge(xode_pool p, char* dest, unsigned int destsize, const char* src, unsigned int srcsize)
+static char *_xode_merge(xode_pool p, char *dest, unsigned int destsize,
+		const char *src, unsigned int srcsize)
 {
 {
-    char* result;
-    result = (char*)xode_pool_malloc(p, destsize + srcsize + 1);
-    memcpy(result, dest, destsize);
-    memcpy(result+destsize, src, srcsize);
-    result[destsize + srcsize] = '\0';
+	char *result;
+	result = (char *)xode_pool_malloc(p, destsize + srcsize + 1);
+	memcpy(result, dest, destsize);
+	memcpy(result + destsize, src, srcsize);
+	result[destsize + srcsize] = '\0';
 
 
-    /* WARNING: major ugly hack: since we're throwing the old data away, let's jump in the xode_pool and subtract it from the size, this is for xmlstream's big-node checking */
-    p->size -= destsize;
+	/* WARNING: major ugly hack: since we're throwing the old data away, let's jump in the xode_pool and subtract it from the size, this is for xmlstream's big-node checking */
+	p->size -= destsize;
 
 
-    return result;
+	return result;
 }
 }
 
 
 static void _xode_hidesibling(xode child)
 static void _xode_hidesibling(xode child)
 {
 {
-    if(child == NULL)
-        return;
+	if(child == NULL)
+		return;
 
 
-    if(child->prev != NULL)
-        child->prev->next = child->next;
-    if(child->next != NULL)
-        child->next->prev = child->prev;
+	if(child->prev != NULL)
+		child->prev->next = child->next;
+	if(child->next != NULL)
+		child->next->prev = child->prev;
 }
 }
 
 
 static void _xode_tag2str(xode_spool s, xode node, int flag)
 static void _xode_tag2str(xode_spool s, xode node, int flag)
 {
 {
-    xode tmp;
-
-    if(flag==0 || flag==1)
-    {
-	    xode_spooler(s,"<",xode_get_name(node),s);
-	    tmp = xode_get_firstattrib(node);
-	    while(tmp) {
-	        xode_spooler(s," ",xode_get_name(tmp),"='",xode_strescape(xode_get_pool(node),xode_get_data(tmp)),"'",s);
-	        tmp = xode_get_nextsibling(tmp);
-	    }
-	    if(flag==0)
-	        xode_spool_add(s,"/>");
-	    else
-	        xode_spool_add(s,">");
-    }
-    else
-    {
-	    xode_spooler(s,"</",xode_get_name(node),">",s);
-    }
+	xode tmp;
+
+	if(flag == 0 || flag == 1) {
+		xode_spooler(s, "<", xode_get_name(node), s);
+		tmp = xode_get_firstattrib(node);
+		while(tmp) {
+			xode_spooler(s, " ", xode_get_name(tmp), "='",
+					xode_strescape(xode_get_pool(node), xode_get_data(tmp)),
+					"'", s);
+			tmp = xode_get_nextsibling(tmp);
+		}
+		if(flag == 0)
+			xode_spool_add(s, "/>");
+		else
+			xode_spool_add(s, ">");
+	} else {
+		xode_spooler(s, "</", xode_get_name(node), ">", s);
+	}
 }
 }
 
 
 static xode_spool _xode_tospool(xode node)
 static xode_spool _xode_tospool(xode node)
 {
 {
-    xode_spool s;
-    int level=0,dir=0;
-    xode tmp;
-
-    if(!node || xode_get_type(node) != XODE_TYPE_TAG)
-	return NULL;
+	xode_spool s;
+	int level = 0, dir = 0;
+	xode tmp;
+
+	if(!node || xode_get_type(node) != XODE_TYPE_TAG)
+		return NULL;
+
+	s = xode_spool_newfrompool(xode_get_pool(node));
+	if(!s)
+		return (NULL);
+
+	while(1) {
+		if(dir == 0) {
+			if(xode_get_type(node) == XODE_TYPE_TAG) {
+				if(xode_has_children(node)) {
+					_xode_tag2str(s, node, 1);
+					node = xode_get_firstchild(node);
+					level++;
+					continue;
+				} else {
+					_xode_tag2str(s, node, 0);
+				}
+			} else {
+				xode_spool_add(s, xode_strescape(xode_get_pool(node),
+										  xode_get_data(node)));
+			}
+		}
+
+		tmp = xode_get_nextsibling(node);
+		if(!tmp) {
+			node = xode_get_parent(node);
+			level--;
+			if(level >= 0)
+				_xode_tag2str(s, node, 2);
+			if(level < 1)
+				break;
+			dir = 1;
+		} else {
+			node = tmp;
+			dir = 0;
+		}
+	}
 
 
-    s = xode_spool_newfrompool(xode_get_pool(node));
-    if(!s) return(NULL);
-
-    while(1)
-    {
-        if(dir==0)
-        {
-    	    if(xode_get_type(node) == XODE_TYPE_TAG)
-            {
-		        if(xode_has_children(node))
-                {
-		            _xode_tag2str(s,node,1);
-        		    node = xode_get_firstchild(node);
-		            level++;
-		            continue;
-        		}
-                else
-                {
-		            _xode_tag2str(s,node,0);
-		        }
-	        }
-            else
-            {
-		        xode_spool_add(s,xode_strescape(xode_get_pool(node),xode_get_data(node)));
-	        }
-	    }
-
-    	tmp = xode_get_nextsibling(node);
-	    if(!tmp)
-        {
-	        node = xode_get_parent(node);
-	        level--;
-	        if(level>=0) _xode_tag2str(s,node,2);
-	        if(level<1) break;
-	        dir = 1;
-	    }
-        else
-        {
-	        node = tmp;
-	        dir = 0;
-	    }
-    }
-
-    return s;
+	return s;
 }
 }
 
 
 
 
@@ -229,17 +220,17 @@ static xode_spool _xode_tospool(xode node)
  *      a pointer to the tag node
  *      a pointer to the tag node
  *      or NULL if it was unsuccessful
  *      or NULL if it was unsuccessful
  */
  */
-xode xode_new(const char* name)
+xode xode_new(const char *name)
 {
 {
-    return _xode_new(NULL, name, XODE_TYPE_TAG);
+	return _xode_new(NULL, name, XODE_TYPE_TAG);
 }
 }
 
 
 /*
 /*
  * alias for 'xode_new'
  * alias for 'xode_new'
  */
  */
-xode xode_new_tag(const char* name)
+xode xode_new_tag(const char *name)
 {
 {
-    return _xode_new(NULL, name, XODE_TYPE_TAG);
+	return _xode_new(NULL, name, XODE_TYPE_TAG);
 }
 }
 
 
 /*
 /*
@@ -253,9 +244,9 @@ xode xode_new_tag(const char* name)
  *      a pointer to the tag node
  *      a pointer to the tag node
  *      or NULL if it was unsuccessful
  *      or NULL if it was unsuccessful
  */
  */
-xode xode_new_frompool(xode_pool p, const char* name)
+xode xode_new_frompool(xode_pool p, const char *name)
 {
 {
-    return _xode_new(p, name, XODE_TYPE_TAG);
+	return _xode_new(p, name, XODE_TYPE_TAG);
 }
 }
 
 
 
 
@@ -270,9 +261,9 @@ xode xode_new_frompool(xode_pool p, const char* name)
  *      a pointer to the child tag node
  *      a pointer to the child tag node
  *      or NULL if it was unsuccessful
  *      or NULL if it was unsuccessful
  */
  */
-xode xode_insert_tag(xode parent, const char* name)
+xode xode_insert_tag(xode parent, const char *name)
 {
 {
-    return _xode_insert(parent, name, XODE_TYPE_TAG);
+	return _xode_insert(parent, name, XODE_TYPE_TAG);
 }
 }
 
 
 
 
@@ -291,35 +282,33 @@ xode xode_insert_tag(xode parent, const char* name)
  *      a pointer to the child CDATA node
  *      a pointer to the child CDATA node
  *      or NULL if it was unsuccessful
  *      or NULL if it was unsuccessful
  */
  */
-xode xode_insert_cdata(xode parent, const char* CDATA, unsigned int size)
-{
-    xode result;
-
-    if(CDATA == NULL || parent == NULL)
-        return NULL;
-
-    if(size == -1)
-        size = strlen(CDATA);
-
-    if ((parent->lastchild != NULL) && (parent->lastchild->type == XODE_TYPE_CDATA))
-    {
-        result = parent->lastchild;
-        result->data = _xode_merge(result->p, result->data, result->data_sz, CDATA, size);
-        result->data_sz = result->data_sz + size;
-    }
-    else
-    {
-        result = _xode_insert(parent, "", XODE_TYPE_CDATA);
-        if (result != NULL)
-        {
-            result->data = (char*)xode_pool_malloc(result->p, size + 1);
-            memcpy(result->data, CDATA, size);
-            result->data[size] = '\0';
-            result->data_sz = size;
-        }
-    }
+xode xode_insert_cdata(xode parent, const char *CDATA, unsigned int size)
+{
+	xode result;
+
+	if(CDATA == NULL || parent == NULL)
+		return NULL;
+
+	if(size == -1)
+		size = strlen(CDATA);
+
+	if((parent->lastchild != NULL)
+			&& (parent->lastchild->type == XODE_TYPE_CDATA)) {
+		result = parent->lastchild;
+		result->data = _xode_merge(
+				result->p, result->data, result->data_sz, CDATA, size);
+		result->data_sz = result->data_sz + size;
+	} else {
+		result = _xode_insert(parent, "", XODE_TYPE_CDATA);
+		if(result != NULL) {
+			result->data = (char *)xode_pool_malloc(result->p, size + 1);
+			memcpy(result->data, CDATA, size);
+			result->data[size] = '\0';
+			result->data_sz = size;
+		}
+	}
 
 
-    return result;
+	return result;
 }
 }
 
 
 
 
@@ -338,320 +327,316 @@ xode xode_insert_cdata(xode parent, const char* CDATA, unsigned int size)
  *      a pointer to the tag matching search criteria
  *      a pointer to the tag matching search criteria
  *      or NULL if search was unsuccessful
  *      or NULL if search was unsuccessful
  */
  */
-xode xode_get_tag(xode parent, const char* name)
+xode xode_get_tag(xode parent, const char *name)
 {
 {
-    char *str, *slash, *qmark, *equals;
-    xode step, ret;
+	char *str, *slash, *qmark, *equals;
+	xode step, ret;
 
 
-    if(parent == NULL || parent->firstchild == NULL || name == NULL || *name == '\0') return NULL;
+	if(parent == NULL || parent->firstchild == NULL || name == NULL
+			|| *name == '\0')
+		return NULL;
 
 
-    if(strstr(name, "/") == NULL && strstr(name,"?") == NULL)
-        return _xode_search(parent->firstchild, name, XODE_TYPE_TAG);
+	if(strstr(name, "/") == NULL && strstr(name, "?") == NULL)
+		return _xode_search(parent->firstchild, name, XODE_TYPE_TAG);
 
 
-    /* jer's note: why can't I modify the name directly, why do I have to strdup it?  damn c grrr! */
-    str = strdup(name);
-    slash = strstr(str, "/");
-    qmark = strstr(str, "?");
-    equals = strstr(str, "=");
+	/* jer's note: why can't I modify the name directly, why do I have to strdup it?  damn c grrr! */
+	str = strdup(name);
+	slash = strstr(str, "/");
+	qmark = strstr(str, "?");
+	equals = strstr(str, "=");
 
 
-    if(qmark != NULL && (slash == NULL || qmark < slash))
-    { /* of type ?attrib */
+	if(qmark != NULL
+			&& (slash == NULL || qmark < slash)) { /* of type ?attrib */
 
 
-        *qmark = '\0';
-        qmark++;
-        if(equals != NULL)
-        {
-            *equals = '\0';
-            equals++;
-        }
+		*qmark = '\0';
+		qmark++;
+		if(equals != NULL) {
+			*equals = '\0';
+			equals++;
+		}
 
 
-        for(step = parent->firstchild; step != NULL; step = xode_get_nextsibling(step))
-        {
-            if(xode_get_type(step) != XODE_TYPE_TAG)
-                continue;
+		for(step = parent->firstchild; step != NULL;
+				step = xode_get_nextsibling(step)) {
+			if(xode_get_type(step) != XODE_TYPE_TAG)
+				continue;
 
 
-            if(*str != '\0')
-                if(_xode_strcmp(xode_get_name(step),str) != 0)
-                    continue;
+			if(*str != '\0')
+				if(_xode_strcmp(xode_get_name(step), str) != 0)
+					continue;
 
 
-            if(xode_get_attrib(step,qmark) == NULL)
-                continue;
+			if(xode_get_attrib(step, qmark) == NULL)
+				continue;
 
 
-            if(equals != NULL && _xode_strcmp(xode_get_attrib(step,qmark),equals) != 0)
-                continue;
+			if(equals != NULL
+					&& _xode_strcmp(xode_get_attrib(step, qmark), equals) != 0)
+				continue;
 
 
-            break;
-        }
+			break;
+		}
 
 
-        free(str);
-        return step;
-    }
+		free(str);
+		return step;
+	}
 
 
 
 
-    *slash = '\0';
-    ++slash;
+	*slash = '\0';
+	++slash;
 
 
-    for(step = parent->firstchild; step != NULL; step = xode_get_nextsibling(step))
-    {
-        if(xode_get_type(step) != XODE_TYPE_TAG) continue;
+	for(step = parent->firstchild; step != NULL;
+			step = xode_get_nextsibling(step)) {
+		if(xode_get_type(step) != XODE_TYPE_TAG)
+			continue;
 
 
-        if(_xode_strcmp(xode_get_name(step),str) != 0)
-            continue;
+		if(_xode_strcmp(xode_get_name(step), str) != 0)
+			continue;
 
 
-        ret = xode_get_tag(step, slash);
-        if(ret != NULL)
-        {
-            free(str);
-            return ret;
-        }
-    }
+		ret = xode_get_tag(step, slash);
+		if(ret != NULL) {
+			free(str);
+			return ret;
+		}
+	}
 
 
-    free(str);
-    return NULL;
+	free(str);
+	return NULL;
 }
 }
 
 
 
 
 /* return the cdata from any tag */
 /* return the cdata from any tag */
 char *xode_get_tagdata(xode parent, const char *name)
 char *xode_get_tagdata(xode parent, const char *name)
 {
 {
-    xode tag;
+	xode tag;
 
 
-    tag = xode_get_tag(parent, name);
-    if(tag == NULL) return NULL;
+	tag = xode_get_tag(parent, name);
+	if(tag == NULL)
+		return NULL;
 
 
-    return xode_get_data(tag);
+	return xode_get_data(tag);
 }
 }
 
 
 
 
-void xode_put_attrib(xode owner, const char* name, const char* value)
+void xode_put_attrib(xode owner, const char *name, const char *value)
 {
 {
-    xode attrib;
+	xode attrib;
 
 
-    if(owner == NULL || name == NULL || value == NULL) return;
+	if(owner == NULL || name == NULL || value == NULL)
+		return;
 
 
-    /* If there are no existing attributes, allocate a new one to start
+	/* If there are no existing attributes, allocate a new one to start
     the list */
     the list */
-    if (owner->firstattrib == NULL)
-    {
-        attrib = _xode_new(owner->p, name, XODE_TYPE_ATTRIB);
-        owner->firstattrib = attrib;
-        owner->lastattrib  = attrib;
-    }
-    else
-    {
-        attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
-        if(attrib == NULL)
-        {
-            attrib = _xode_appendsibling(owner->lastattrib, name, XODE_TYPE_ATTRIB);
-            owner->lastattrib = attrib;
-        }
-    }
-    /* Update the value of the attribute */
-    attrib->data_sz = strlen(value);
-    attrib->data    = xode_pool_strdup(owner->p, value);
-
-}
-
-char* xode_get_attrib(xode owner, const char* name)
-{
-    xode attrib;
-
-    if (owner != NULL && owner->firstattrib != NULL)
-    {
-        attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
-        if (attrib != NULL)
-            return (char*)attrib->data;
-    }
-    return NULL;
-}
-
-void xode_put_vattrib(xode owner, const char* name, void *value)
-{
-    xode attrib;
-
-    if (owner != NULL)
-    {
-        attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
-        if (attrib == NULL)
-        {
-            xode_put_attrib(owner, name, "");
-            attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
-        }
-        if (attrib != NULL)
-            attrib->firstchild = (xode)value;
-    }
-}
-
-void* xode_get_vattrib(xode owner, const char* name)
-{
-    xode attrib;
-
-    if (owner != NULL && owner->firstattrib != NULL)
-    {
-        attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
-        if (attrib != NULL)
-            return (void*)attrib->firstchild;
-    }
-    return NULL;
+	if(owner->firstattrib == NULL) {
+		attrib = _xode_new(owner->p, name, XODE_TYPE_ATTRIB);
+		owner->firstattrib = attrib;
+		owner->lastattrib = attrib;
+	} else {
+		attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
+		if(attrib == NULL) {
+			attrib = _xode_appendsibling(
+					owner->lastattrib, name, XODE_TYPE_ATTRIB);
+			owner->lastattrib = attrib;
+		}
+	}
+	/* Update the value of the attribute */
+	attrib->data_sz = strlen(value);
+	attrib->data = xode_pool_strdup(owner->p, value);
+}
+
+char *xode_get_attrib(xode owner, const char *name)
+{
+	xode attrib;
+
+	if(owner != NULL && owner->firstattrib != NULL) {
+		attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
+		if(attrib != NULL)
+			return (char *)attrib->data;
+	}
+	return NULL;
+}
+
+void xode_put_vattrib(xode owner, const char *name, void *value)
+{
+	xode attrib;
+
+	if(owner != NULL) {
+		attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
+		if(attrib == NULL) {
+			xode_put_attrib(owner, name, "");
+			attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
+		}
+		if(attrib != NULL)
+			attrib->firstchild = (xode)value;
+	}
+}
+
+void *xode_get_vattrib(xode owner, const char *name)
+{
+	xode attrib;
+
+	if(owner != NULL && owner->firstattrib != NULL) {
+		attrib = _xode_search(owner->firstattrib, name, XODE_TYPE_ATTRIB);
+		if(attrib != NULL)
+			return (void *)attrib->firstchild;
+	}
+	return NULL;
 }
 }
 
 
 xode xode_get_firstattrib(xode parent)
 xode xode_get_firstattrib(xode parent)
 {
 {
-    if (parent != NULL)
-        return parent->firstattrib;
-    return NULL;
+	if(parent != NULL)
+		return parent->firstattrib;
+	return NULL;
 }
 }
 
 
 xode xode_get_firstchild(xode parent)
 xode xode_get_firstchild(xode parent)
 {
 {
-    if (parent != NULL)
-        return parent->firstchild;
-    return NULL;
+	if(parent != NULL)
+		return parent->firstchild;
+	return NULL;
 }
 }
 
 
 xode xode_get_lastchild(xode parent)
 xode xode_get_lastchild(xode parent)
 {
 {
-    if (parent != NULL)
-        return parent->lastchild;
-    return NULL;
+	if(parent != NULL)
+		return parent->lastchild;
+	return NULL;
 }
 }
 
 
 xode xode_get_nextsibling(xode sibling)
 xode xode_get_nextsibling(xode sibling)
 {
 {
-    if (sibling != NULL)
-        return sibling->next;
-    return NULL;
+	if(sibling != NULL)
+		return sibling->next;
+	return NULL;
 }
 }
 
 
 xode xode_get_prevsibling(xode sibling)
 xode xode_get_prevsibling(xode sibling)
 {
 {
-    if (sibling != NULL)
-        return sibling->prev;
-    return NULL;
+	if(sibling != NULL)
+		return sibling->prev;
+	return NULL;
 }
 }
 
 
 xode xode_get_parent(xode node)
 xode xode_get_parent(xode node)
 {
 {
-    if (node != NULL)
-        return node->parent;
-    return NULL;
+	if(node != NULL)
+		return node->parent;
+	return NULL;
 }
 }
 
 
-char* xode_get_name(xode node)
+char *xode_get_name(xode node)
 {
 {
-    if (node != NULL)
-        return node->name;
-    return NULL;
+	if(node != NULL)
+		return node->name;
+	return NULL;
 }
 }
 
 
-char* xode_get_data(xode node)
+char *xode_get_data(xode node)
 {
 {
-    xode cur;
+	xode cur;
 
 
-    if(node == NULL) return NULL;
+	if(node == NULL)
+		return NULL;
 
 
-    if(xode_get_type(node) == XODE_TYPE_TAG) /* loop till we find a CDATA */
-    {
-        for(cur = xode_get_firstchild(node); cur != NULL; cur = xode_get_nextsibling(cur))
-            if(xode_get_type(cur) == XODE_TYPE_CDATA)
-                return cur->data;
-    }else{
-        return node->data;
-    }
-    return NULL;
+	if(xode_get_type(node) == XODE_TYPE_TAG) /* loop till we find a CDATA */
+	{
+		for(cur = xode_get_firstchild(node); cur != NULL;
+				cur = xode_get_nextsibling(cur))
+			if(xode_get_type(cur) == XODE_TYPE_CDATA)
+				return cur->data;
+	} else {
+		return node->data;
+	}
+	return NULL;
 }
 }
 
 
 int xode_get_datasz(xode node)
 int xode_get_datasz(xode node)
 {
 {
-	
-    if( node == NULL )
-    {
-        return (int)(long)NULL;	    
-    }	    
-    else if(xode_get_type(node) == XODE_TYPE_TAG) /* loop till we find a CDATA */
-    {
-    	xode cur;	
-        for(cur = xode_get_firstchild(node); cur != NULL; cur = xode_get_nextsibling(cur))
-            if(xode_get_type(cur) == XODE_TYPE_CDATA)
-                return cur->data_sz;
-    }else{
-        return node->data_sz;
-    }
-    return (int)(long)NULL;
+
+	if(node == NULL) {
+		return (int)(long)NULL;
+	} else if(xode_get_type(node)
+			  == XODE_TYPE_TAG) /* loop till we find a CDATA */
+	{
+		xode cur;
+		for(cur = xode_get_firstchild(node); cur != NULL;
+				cur = xode_get_nextsibling(cur))
+			if(xode_get_type(cur) == XODE_TYPE_CDATA)
+				return cur->data_sz;
+	} else {
+		return node->data_sz;
+	}
+	return (int)(long)NULL;
 }
 }
 
 
 int xode_get_type(xode node)
 int xode_get_type(xode node)
 {
 {
-    if (node != NULL)
-    {
-        return node->type;
-    }
-    return (int)(long)NULL;
+	if(node != NULL) {
+		return node->type;
+	}
+	return (int)(long)NULL;
 }
 }
 
 
 int xode_has_children(xode node)
 int xode_has_children(xode node)
 {
 {
-    if ((node != NULL) && (node->firstchild != NULL))
-        return 1;
-    return 0;
+	if((node != NULL) && (node->firstchild != NULL))
+		return 1;
+	return 0;
 }
 }
 
 
 int xode_has_attribs(xode node)
 int xode_has_attribs(xode node)
 {
 {
-    if ((node != NULL) && (node->firstattrib != NULL))
-        return 1;
-    return 0;
+	if((node != NULL) && (node->firstattrib != NULL))
+		return 1;
+	return 0;
 }
 }
 
 
 xode_pool xode_get_pool(xode node)
 xode_pool xode_get_pool(xode node)
 {
 {
-    if (node != NULL)
-        return node->p;
-    return (xode_pool)NULL;
+	if(node != NULL)
+		return node->p;
+	return (xode_pool)NULL;
 }
 }
 
 
 void xode_hide(xode child)
 void xode_hide(xode child)
 {
 {
-    xode parent;
+	xode parent;
 
 
-    if(child == NULL || child->parent == NULL)
-        return;
+	if(child == NULL || child->parent == NULL)
+		return;
 
 
-    parent = child->parent;
+	parent = child->parent;
 
 
-    /* first fix up at the child level */
-    _xode_hidesibling(child);
+	/* first fix up at the child level */
+	_xode_hidesibling(child);
 
 
-    /* next fix up at the parent level */
-    if(parent->firstchild == child)
-        parent->firstchild = child->next;
-    if(parent->lastchild == child)
-        parent->lastchild = child->prev;
+	/* next fix up at the parent level */
+	if(parent->firstchild == child)
+		parent->firstchild = child->next;
+	if(parent->lastchild == child)
+		parent->lastchild = child->prev;
 }
 }
 
 
 void xode_hide_attrib(xode parent, const char *name)
 void xode_hide_attrib(xode parent, const char *name)
 {
 {
-    xode attrib;
+	xode attrib;
 
 
-    if(parent == NULL || parent->firstattrib == NULL || name == NULL)
-        return;
+	if(parent == NULL || parent->firstattrib == NULL || name == NULL)
+		return;
 
 
-    attrib = _xode_search(parent->firstattrib, name, XODE_TYPE_ATTRIB);
-    if(attrib == NULL)
-        return;
+	attrib = _xode_search(parent->firstattrib, name, XODE_TYPE_ATTRIB);
+	if(attrib == NULL)
+		return;
 
 
-    /* first fix up at the child level */
-    _xode_hidesibling(attrib);
+	/* first fix up at the child level */
+	_xode_hidesibling(attrib);
 
 
-    /* next fix up at the parent level */
-    if(parent->firstattrib == attrib)
-        parent->firstattrib = attrib->next;
-    if(parent->lastattrib == attrib)
-        parent->lastattrib = attrib->prev;
+	/* next fix up at the parent level */
+	if(parent->firstattrib == attrib)
+		parent->firstattrib = attrib->next;
+	if(parent->lastattrib == attrib)
+		parent->lastattrib = attrib->prev;
 }
 }
 
 
 
 
-
 /*
 /*
  *  xode2str -- convert given xode tree into a string
  *  xode2str -- convert given xode tree into a string
  *
  *
@@ -664,204 +649,204 @@ void xode_hide_attrib(xode parent, const char *name)
  */
  */
 char *xode_to_str(xode node)
 char *xode_to_str(xode node)
 {
 {
-     return xode_spool_tostr(_xode_tospool(node));
+	return xode_spool_tostr(_xode_tospool(node));
 }
 }
 
 
 
 
 /* loop through both a and b comparing everything, attribs, cdata, children, etc */
 /* loop through both a and b comparing everything, attribs, cdata, children, etc */
 int xode_cmp(xode a, xode b)
 int xode_cmp(xode a, xode b)
 {
 {
-    int ret = 0;
-
-    while(1)
-    {
-        if(a == NULL && b == NULL)
-            return 0;
-
-        if(a == NULL || b == NULL)
-            return -1;
-
-        if(xode_get_type(a) != xode_get_type(b))
-            return -1;
-
-        switch(xode_get_type(a))
-        {
-        case XODE_TYPE_ATTRIB:
-            ret = _xode_strcmp(xode_get_name(a), xode_get_name(b));
-            if(ret != 0)
-                return -1;
-            ret = _xode_strcmp(xode_get_data(a), xode_get_data(b));
-            if(ret != 0)
-                return -1;
-            break;
-        case XODE_TYPE_TAG:
-            ret = _xode_strcmp(xode_get_name(a), xode_get_name(b));
-            if(ret != 0)
-                return -1;
-            ret = xode_cmp(xode_get_firstattrib(a), xode_get_firstattrib(b));
-            if(ret != 0)
-                return -1;
-            ret = xode_cmp(xode_get_firstchild(a), xode_get_firstchild(b));
-            if(ret != 0)
-                return -1;
-            break;
-        case XODE_TYPE_CDATA:
-            ret = _xode_strcmp(xode_get_data(a), xode_get_data(b));
-            if(ret != 0)
-                return -1;
-        }
-        a = xode_get_nextsibling(a);
-        b = xode_get_nextsibling(b);
-    }
+	int ret = 0;
+
+	while(1) {
+		if(a == NULL && b == NULL)
+			return 0;
+
+		if(a == NULL || b == NULL)
+			return -1;
+
+		if(xode_get_type(a) != xode_get_type(b))
+			return -1;
+
+		switch(xode_get_type(a)) {
+			case XODE_TYPE_ATTRIB:
+				ret = _xode_strcmp(xode_get_name(a), xode_get_name(b));
+				if(ret != 0)
+					return -1;
+				ret = _xode_strcmp(xode_get_data(a), xode_get_data(b));
+				if(ret != 0)
+					return -1;
+				break;
+			case XODE_TYPE_TAG:
+				ret = _xode_strcmp(xode_get_name(a), xode_get_name(b));
+				if(ret != 0)
+					return -1;
+				ret = xode_cmp(
+						xode_get_firstattrib(a), xode_get_firstattrib(b));
+				if(ret != 0)
+					return -1;
+				ret = xode_cmp(xode_get_firstchild(a), xode_get_firstchild(b));
+				if(ret != 0)
+					return -1;
+				break;
+			case XODE_TYPE_CDATA:
+				ret = _xode_strcmp(xode_get_data(a), xode_get_data(b));
+				if(ret != 0)
+					return -1;
+		}
+		a = xode_get_nextsibling(a);
+		b = xode_get_nextsibling(b);
+	}
 }
 }
 
 
 
 
 xode xode_insert_tagnode(xode parent, xode node)
 xode xode_insert_tagnode(xode parent, xode node)
 {
 {
-    xode child;
+	xode child;
 
 
-    child = xode_insert_tag(parent, xode_get_name(node));
-    if (xode_has_attribs(node))
-        xode_insert_node(child, xode_get_firstattrib(node));
-    if (xode_has_children(node))
-        xode_insert_node(child, xode_get_firstchild(node));
+	child = xode_insert_tag(parent, xode_get_name(node));
+	if(xode_has_attribs(node))
+		xode_insert_node(child, xode_get_firstattrib(node));
+	if(xode_has_children(node))
+		xode_insert_node(child, xode_get_firstchild(node));
 
 
-    return child;
+	return child;
 }
 }
 
 
 /* places copy of node and node's siblings in parent */
 /* places copy of node and node's siblings in parent */
 void xode_insert_node(xode parent, xode node)
 void xode_insert_node(xode parent, xode node)
 {
 {
-    if(node == NULL || parent == NULL)
-        return;
-
-    while(node != NULL)
-    {
-        switch(xode_get_type(node))
-        {
-        case XODE_TYPE_ATTRIB:
-            xode_put_attrib(parent, xode_get_name(node), xode_get_data(node));
-            break;
-        case XODE_TYPE_TAG:
-            xode_insert_tagnode(parent, node);
-            break;
-        case XODE_TYPE_CDATA:
-            xode_insert_cdata(parent, xode_get_data(node), xode_get_datasz(node));
-        }
-        node = xode_get_nextsibling(node);
-    }
+	if(node == NULL || parent == NULL)
+		return;
+
+	while(node != NULL) {
+		switch(xode_get_type(node)) {
+			case XODE_TYPE_ATTRIB:
+				xode_put_attrib(
+						parent, xode_get_name(node), xode_get_data(node));
+				break;
+			case XODE_TYPE_TAG:
+				xode_insert_tagnode(parent, node);
+				break;
+			case XODE_TYPE_CDATA:
+				xode_insert_cdata(
+						parent, xode_get_data(node), xode_get_datasz(node));
+		}
+		node = xode_get_nextsibling(node);
+	}
 }
 }
 
 
 
 
 /* produce full duplicate of x with a new xode_pool, x must be a tag! */
 /* produce full duplicate of x with a new xode_pool, x must be a tag! */
 xode xode_dup(xode x)
 xode xode_dup(xode x)
 {
 {
-    xode x2;
+	xode x2;
 
 
-    if(x == NULL)
-        return NULL;
+	if(x == NULL)
+		return NULL;
 
 
-    x2 = xode_new(xode_get_name(x));
+	x2 = xode_new(xode_get_name(x));
 
 
-    if (xode_has_attribs(x))
-        xode_insert_node(x2, xode_get_firstattrib(x));
-    if (xode_has_children(x))
-        xode_insert_node(x2, xode_get_firstchild(x));
+	if(xode_has_attribs(x))
+		xode_insert_node(x2, xode_get_firstattrib(x));
+	if(xode_has_children(x))
+		xode_insert_node(x2, xode_get_firstchild(x));
 
 
-    return x2;
+	return x2;
 }
 }
 
 
 xode xode_dup_frompool(xode_pool p, xode x)
 xode xode_dup_frompool(xode_pool p, xode x)
 {
 {
-    xode x2;
+	xode x2;
 
 
-    if(x == NULL)
-        return NULL;
+	if(x == NULL)
+		return NULL;
 
 
-    x2 = xode_new_frompool(p, xode_get_name(x));
+	x2 = xode_new_frompool(p, xode_get_name(x));
 
 
-    if (xode_has_attribs(x))
-        xode_insert_node(x2, xode_get_firstattrib(x));
-    if (xode_has_children(x))
-        xode_insert_node(x2, xode_get_firstchild(x));
+	if(xode_has_attribs(x))
+		xode_insert_node(x2, xode_get_firstattrib(x));
+	if(xode_has_children(x))
+		xode_insert_node(x2, xode_get_firstchild(x));
 
 
-    return x2;
+	return x2;
 }
 }
 
 
-xode xode_wrap(xode x,const char *wrapper)
+xode xode_wrap(xode x, const char *wrapper)
 {
 {
-    xode wrap;
-    if(x==NULL||wrapper==NULL) return NULL;
-    wrap=xode_new_frompool(xode_get_pool(x),wrapper);
-    if(wrap==NULL) return NULL;
-    wrap->firstchild=x;
-    wrap->lastchild=x;
-    x->parent=wrap;
-    return wrap;
+	xode wrap;
+	if(x == NULL || wrapper == NULL)
+		return NULL;
+	wrap = xode_new_frompool(xode_get_pool(x), wrapper);
+	if(wrap == NULL)
+		return NULL;
+	wrap->firstchild = x;
+	wrap->lastchild = x;
+	x->parent = wrap;
+	return wrap;
 }
 }
 
 
 void xode_free(xode node)
 void xode_free(xode node)
 {
 {
-    if(node == NULL)
-        return;
+	if(node == NULL)
+		return;
 
 
-    xode_pool_free(node->p);
+	xode_pool_free(node->p);
 }
 }
 
 
 
 
-void
-_xode_to_prettystr( xode_spool s, xode x, int deep )
+void _xode_to_prettystr(xode_spool s, xode x, int deep)
 {
 {
 	int i;
 	int i;
 	xode y;
 	xode y;
 
 
-	if(xode_get_type(x) != XODE_TYPE_TAG) return;
-	
-	for(i=0; i<deep; i++) xode_spool_add(s, "\t");	
+	if(xode_get_type(x) != XODE_TYPE_TAG)
+		return;
 
 
-	xode_spooler( s , "<" , xode_get_name(x) ,  s );
+	for(i = 0; i < deep; i++)
+		xode_spool_add(s, "\t");
+
+	xode_spooler(s, "<", xode_get_name(x), s);
 
 
 	y = xode_get_firstattrib(x);
 	y = xode_get_firstattrib(x);
-	while( y )
-	{
-		xode_spooler( s , " " , xode_get_name(y) , "='", xode_get_data(y) , "'" , s );
+	while(y) {
+		xode_spooler(s, " ", xode_get_name(y), "='", xode_get_data(y), "'", s);
 
 
-		y = xode_get_nextsibling( y );
+		y = xode_get_nextsibling(y);
 	}
 	}
-	xode_spool_add(s,">");
-	xode_spool_add(s,"\n");
-		
-	if( xode_get_data(x))
-	{
-		for(i=0; i<=deep; i++) xode_spool_add(s, "\t");	
-		xode_spool_add( s , xode_get_data(x)); 
+	xode_spool_add(s, ">");
+	xode_spool_add(s, "\n");
+
+	if(xode_get_data(x)) {
+		for(i = 0; i <= deep; i++)
+			xode_spool_add(s, "\t");
+		xode_spool_add(s, xode_get_data(x));
 	}
 	}
-			
+
 	y = xode_get_firstchild(x);
 	y = xode_get_firstchild(x);
-	while( y )
-	{
-		_xode_to_prettystr(s , y, deep+1);
+	while(y) {
+		_xode_to_prettystr(s, y, deep + 1);
 		y = xode_get_nextsibling(y);
 		y = xode_get_nextsibling(y);
-		xode_spool_add(s,"\n");
+		xode_spool_add(s, "\n");
 	}
 	}
-		
-	for(i=0; i<deep; i++) xode_spool_add(s, "\t");	
-	xode_spooler( s , "</" , xode_get_name(x) , ">" , s );
+
+	for(i = 0; i < deep; i++)
+		xode_spool_add(s, "\t");
+	xode_spooler(s, "</", xode_get_name(x), ">", s);
 
 
 	return;
 	return;
 }
 }
 
 
-char * 
-xode_to_prettystr( xode x )
+char *xode_to_prettystr(xode x)
 {
 {
 	xode_spool s;
 	xode_spool s;
 
 
-	if( !x) return NULL;
-	
-	s = xode_spool_newfrompool( xode_get_pool(x));
+	if(!x)
+		return NULL;
 
 
-	_xode_to_prettystr( s , x, 0 );
+	s = xode_spool_newfrompool(xode_get_pool(x));
+
+	_xode_to_prettystr(s, x, 0);
 
 
 	return xode_spool_tostr(s);
 	return xode_spool_tostr(s);
 }
 }
-

+ 213 - 201
src/modules/xmpp/xode.h

@@ -40,24 +40,24 @@
 **  Arrange to use either varargs or stdargs
 **  Arrange to use either varargs or stdargs
 */
 */
 
 
-#define MAXSHORTSTR	203		/* max short string length */
-#define QUAD_T	unsigned long long
+#define MAXSHORTSTR 203 /* max short string length */
+#define QUAD_T unsigned long long
 
 
 #ifdef __STDC__
 #ifdef __STDC__
 
 
 #include <stdarg.h>
 #include <stdarg.h>
 
 
-# define VA_LOCAL_DECL	va_list ap;
-# define VA_START(f)	va_start(ap, f)
-# define VA_END		va_end(ap)
+#define VA_LOCAL_DECL va_list ap;
+#define VA_START(f) va_start(ap, f)
+#define VA_END va_end(ap)
 
 
 #else /* __STDC__ */
 #else /* __STDC__ */
 
 
-# include <varargs.h>
+#include <varargs.h>
 
 
-# define VA_LOCAL_DECL	va_list ap;
-# define VA_START(f)	va_start(ap)
-# define VA_END		va_end(ap)
+#define VA_LOCAL_DECL va_list ap;
+#define VA_START(f) va_start(ap)
+#define VA_END va_end(ap)
 
 
 #endif /* __STDC__ */
 #endif /* __STDC__ */
 
 
@@ -66,108 +66,116 @@
 #define INCL_LIBXODE_H
 #define INCL_LIBXODE_H
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
-extern "C" {
+extern "C"
+{
 #endif
 #endif
 
 
 #ifndef __OS_darwin
 #ifndef __OS_darwin
 #ifndef HAVE_SNPRINTF
 #ifndef HAVE_SNPRINTF
-extern int ap_snprintf(char *, size_t, const char *, ...);
+	extern int ap_snprintf(char *, size_t, const char *, ...);
 #define snprintf ap_snprintf
 #define snprintf ap_snprintf
 #endif
 #endif
 
 
 #ifndef HAVE_VSNPRINTF
 #ifndef HAVE_VSNPRINTF
-extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
+	extern int ap_vsnprintf(char *, size_t, const char *, va_list ap);
 #define vsnprintf ap_vsnprintf
 #define vsnprintf ap_vsnprintf
 #endif
 #endif
 #endif
 #endif
 
 
-/* --------------------------------------------------------- */
-/*                                                           */
-/* Pool-based memory management routines                     */
-/*                                                           */
-/* --------------------------------------------------------- */
+	/* --------------------------------------------------------- */
+	/*                                                           */
+	/* Pool-based memory management routines                     */
+	/*                                                           */
+	/* --------------------------------------------------------- */
 
 
 
 
-/* xode_pool_cleaner - callback type which is associated
+	/* xode_pool_cleaner - callback type which is associated
    with a pool entry; invoked when the pool entry is 
    with a pool entry; invoked when the pool entry is 
    free'd */
    free'd */
-typedef void (*xode_pool_cleaner)(void *arg);
+	typedef void (*xode_pool_cleaner)(void *arg);
 
 
 
 
-/* pheap - singular allocation of memory */
-struct xode_pool_heap
-{
-    void *block;
-    int size, used;
-};
+	/* pheap - singular allocation of memory */
+	struct xode_pool_heap
+	{
+		void *block;
+		int size, used;
+	};
 
 
-/* pool - base node for a pool. Maintains a linked list
+	/* pool - base node for a pool. Maintains a linked list
    of pool entries (pool_free) */
    of pool entries (pool_free) */
-typedef struct xode_pool_struct
-{
-    int size;
-    struct xode_pool_free *cleanup;
-    struct xode_pool_heap *heap;
-} _xode_pool, *xode_pool;
-
-/* pool creation routines */
-xode_pool xode_pool_heap(int bytes);
-xode_pool xode_pool_new(void);
-
-/* pool wrappers for malloc */
-void *xode_pool_malloc  (xode_pool p, int size);
-void *xode_pool_mallocx (xode_pool p, int size, char c); 
-void *xode_pool_malloco (xode_pool p, int size); 
-
-/* wrapper around strdup, gains mem from pool */
-char *xode_pool_strdup  (xode_pool p, const char *src); 
-
-/* calls f(arg) before the pool is freed during cleanup */
-void xode_pool_cleanup  (xode_pool p, xode_pool_cleaner f, void *arg); 
-
-/* pool wrapper for free, called on a pool */
-void xode_pool_free     (xode_pool p); 
-
-/* returns total bytes allocated in this pool */
-int  xode_pool_size     (xode_pool p); 
-
-/* --------------------------------------------------------- */
-/*                                                           */
-/* XML escaping utils                                        */
-/*                                                           */
-/* --------------------------------------------------------- */
-char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
-char *xode_strunescape(xode_pool p, char *buf);
-
-
-/* --------------------------------------------------------- */
-/*                                                           */
-/* String pools (spool) functions                            */
-/*                                                           */
-/* --------------------------------------------------------- */
-struct xode_spool_node
-{
-    char *c;
-    struct xode_spool_node *next;
-};
-
-typedef struct xode_spool_struct
-{
-    xode_pool p;
-    int len;
-    struct xode_spool_node *last;
-    struct xode_spool_node *first;
-} *xode_spool;
-
-xode_spool xode_spool_new         ( void                          ); /* create a string pool on a new pool */
-xode_spool xode_spool_newfrompool ( xode_pool        p            ); /* create a string pool from an existing pool */
-xode_pool  xode_spool_getpool     ( const xode_spool s            ); /* returns the xode_pool used by this xode_spool */
-void       xode_spooler           ( xode_spool       s, ...       ); /* append all the char * args to the pool, terminate args with s again */
-char       *xode_spool_tostr      ( xode_spool       s            ); /* return a big string */
-void       xode_spool_add         ( xode_spool       s, char *str ); /* add a single char to the pool */
-char       *xode_spool_str        ( xode_pool        p, ...       ); /* wrap all the spooler stuff in one function, the happy fun ball! */
-int        xode_spool_getlen      ( const xode_spool s            ); /* returns the total length of the string contained in the pool */
-void       xode_spool_free        ( xode_spool       s            ); /* Free's the pool associated with the xode_spool */
+	typedef struct xode_pool_struct
+	{
+		int size;
+		struct xode_pool_free *cleanup;
+		struct xode_pool_heap *heap;
+	} _xode_pool, *xode_pool;
+
+	/* pool creation routines */
+	xode_pool xode_pool_heap(int bytes);
+	xode_pool xode_pool_new(void);
+
+	/* pool wrappers for malloc */
+	void *xode_pool_malloc(xode_pool p, int size);
+	void *xode_pool_mallocx(xode_pool p, int size, char c);
+	void *xode_pool_malloco(xode_pool p, int size);
+
+	/* wrapper around strdup, gains mem from pool */
+	char *xode_pool_strdup(xode_pool p, const char *src);
+
+	/* calls f(arg) before the pool is freed during cleanup */
+	void xode_pool_cleanup(xode_pool p, xode_pool_cleaner f, void *arg);
+
+	/* pool wrapper for free, called on a pool */
+	void xode_pool_free(xode_pool p);
+
+	/* returns total bytes allocated in this pool */
+	int xode_pool_size(xode_pool p);
+
+	/* --------------------------------------------------------- */
+	/*                                                           */
+	/* XML escaping utils                                        */
+	/*                                                           */
+	/* --------------------------------------------------------- */
+	char *xode_strescape(xode_pool p, char *buf); /* Escape <>&'" chars */
+	char *xode_strunescape(xode_pool p, char *buf);
+
+
+	/* --------------------------------------------------------- */
+	/*                                                           */
+	/* String pools (spool) functions                            */
+	/*                                                           */
+	/* --------------------------------------------------------- */
+	struct xode_spool_node
+	{
+		char *c;
+		struct xode_spool_node *next;
+	};
+
+	typedef struct xode_spool_struct
+	{
+		xode_pool p;
+		int len;
+		struct xode_spool_node *last;
+		struct xode_spool_node *first;
+	} * xode_spool;
+
+	xode_spool xode_spool_new(void); /* create a string pool on a new pool */
+	xode_spool xode_spool_newfrompool(
+			xode_pool p); /* create a string pool from an existing pool */
+	xode_pool xode_spool_getpool(const xode_spool
+					s); /* returns the xode_pool used by this xode_spool */
+	void xode_spooler(xode_spool s,
+			...); /* append all the char * args to the pool, terminate args with s again */
+	char *xode_spool_tostr(xode_spool s); /* return a big string */
+	void xode_spool_add(
+			xode_spool s, char *str); /* add a single char to the pool */
+	char *xode_spool_str(xode_pool p,
+			...); /* wrap all the spooler stuff in one function, the happy fun ball! */
+	int xode_spool_getlen(const xode_spool
+					s); /* returns the total length of the string contained in the pool */
+	void xode_spool_free(
+			xode_spool s); /* Free's the pool associated with the xode_spool */
 
 
 
 
 /* --------------------------------------------------------- */
 /* --------------------------------------------------------- */
@@ -175,128 +183,132 @@ void       xode_spool_free        ( xode_spool       s            ); /* Free's t
 /* xodes - Document Object Model                          */
 /* xodes - Document Object Model                          */
 /*                                                           */
 /*                                                           */
 /* --------------------------------------------------------- */
 /* --------------------------------------------------------- */
-#define XODE_TYPE_TAG    0
+#define XODE_TYPE_TAG 0
 #define XODE_TYPE_ATTRIB 1
 #define XODE_TYPE_ATTRIB 1
-#define XODE_TYPE_CDATA  2
+#define XODE_TYPE_CDATA 2
 
 
-#define XODE_TYPE_LAST   2
-#define XODE_TYPE_UNDEF  -1
+#define XODE_TYPE_LAST 2
+#define XODE_TYPE_UNDEF -1
 
 
-/* -------------------------------------------------------------------------- 
+	/* -------------------------------------------------------------------------- 
    Node structure. Do not use directly! Always use accessors macros 
    Node structure. Do not use directly! Always use accessors macros 
    and methods!
    and methods!
    -------------------------------------------------------------------------- */
    -------------------------------------------------------------------------- */
-typedef struct xode_struct
-{
-     char*                name;
-     unsigned short       type;
-     char*                data;
-     int                  data_sz;
-     int                  complete;
-     xode_pool            p;
-     struct xode_struct*  parent;
-     struct xode_struct*  firstchild; 
-     struct xode_struct*  lastchild;
-     struct xode_struct*  prev; 
-     struct xode_struct*  next;
-     struct xode_struct*  firstattrib;
-     struct xode_struct*  lastattrib;
-} _xode, *xode;
-
-/* Node creation routines */
-xode  xode_wrap(xode x,const char* wrapper);
-xode  xode_new(const char* name);
-xode  xode_new_tag(const char* name);
-xode  xode_new_frompool(xode_pool p, const char* name);
-xode  xode_insert_tag(xode parent, const char* name); 
-xode  xode_insert_cdata(xode parent, const char* CDATA, unsigned int size);
-xode  xode_insert_tagnode(xode parent, xode node);
-void  xode_insert_node(xode parent, xode node);
-xode  xode_from_str(char *str, int len);
-xode  xode_from_strx(char *str, int len, int *err, int *pos);
-xode  xode_from_file(char *file);
-xode  xode_dup(xode x); /* duplicate x */
-xode  xode_dup_frompool(xode_pool p, xode x);
-
-/* Node Memory Pool */
-xode_pool xode_get_pool(xode node);
-
-/* Node editing */
-void xode_hide(xode child);
-void xode_hide_attrib(xode parent, const char *name);
-
-/* Node deletion routine, also frees the node pool! */
-void xode_free(xode node);
-
-/* Locates a child tag by name and returns it */
-xode  xode_get_tag(xode parent, const char* name);
-char* xode_get_tagdata(xode parent, const char* name);
-
-/* Attribute accessors */
-void     xode_put_attrib(xode owner, const char* name, const char* value);
-char*    xode_get_attrib(xode owner, const char* name);
-
-/* Bastard am I, but these are fun for internal use ;-) */
-void     xode_put_vattrib(xode owner, const char* name, void *value);
-void*    xode_get_vattrib(xode owner, const char* name);
-
-/* Node traversal routines */
-xode  xode_get_firstattrib(xode parent);
-xode  xode_get_firstchild(xode parent);
-xode  xode_get_lastchild(xode parent);
-xode  xode_get_nextsibling(xode sibling);
-xode  xode_get_prevsibling(xode sibling);
-xode  xode_get_parent(xode node);
-
-/* Node information routines */
-char*    xode_get_name(xode node);
-char*    xode_get_data(xode node);
-int      xode_get_datasz(xode node);
-int      xode_get_type(xode node);
-
-int      xode_has_children(xode node);
-int      xode_has_attribs(xode node);
-
-/* Node-to-string translation */
-char*    xode_to_str(xode node);
-char*    xode_to_prettystr(xode node);  /* Puts \t and \n to make a human-easily readable string */
-
-int      xode_cmp(xode a, xode b); /* compares a and b for equality */
-
-int      xode_to_file(char *file, xode node); /* writes node to file */
-
-
-/***********************
+	typedef struct xode_struct
+	{
+		char *name;
+		unsigned short type;
+		char *data;
+		int data_sz;
+		int complete;
+		xode_pool p;
+		struct xode_struct *parent;
+		struct xode_struct *firstchild;
+		struct xode_struct *lastchild;
+		struct xode_struct *prev;
+		struct xode_struct *next;
+		struct xode_struct *firstattrib;
+		struct xode_struct *lastattrib;
+	} _xode, *xode;
+
+	/* Node creation routines */
+	xode xode_wrap(xode x, const char *wrapper);
+	xode xode_new(const char *name);
+	xode xode_new_tag(const char *name);
+	xode xode_new_frompool(xode_pool p, const char *name);
+	xode xode_insert_tag(xode parent, const char *name);
+	xode xode_insert_cdata(xode parent, const char *CDATA, unsigned int size);
+	xode xode_insert_tagnode(xode parent, xode node);
+	void xode_insert_node(xode parent, xode node);
+	xode xode_from_str(char *str, int len);
+	xode xode_from_strx(char *str, int len, int *err, int *pos);
+	xode xode_from_file(char *file);
+	xode xode_dup(xode x); /* duplicate x */
+	xode xode_dup_frompool(xode_pool p, xode x);
+
+	/* Node Memory Pool */
+	xode_pool xode_get_pool(xode node);
+
+	/* Node editing */
+	void xode_hide(xode child);
+	void xode_hide_attrib(xode parent, const char *name);
+
+	/* Node deletion routine, also frees the node pool! */
+	void xode_free(xode node);
+
+	/* Locates a child tag by name and returns it */
+	xode xode_get_tag(xode parent, const char *name);
+	char *xode_get_tagdata(xode parent, const char *name);
+
+	/* Attribute accessors */
+	void xode_put_attrib(xode owner, const char *name, const char *value);
+	char *xode_get_attrib(xode owner, const char *name);
+
+	/* Bastard am I, but these are fun for internal use ;-) */
+	void xode_put_vattrib(xode owner, const char *name, void *value);
+	void *xode_get_vattrib(xode owner, const char *name);
+
+	/* Node traversal routines */
+	xode xode_get_firstattrib(xode parent);
+	xode xode_get_firstchild(xode parent);
+	xode xode_get_lastchild(xode parent);
+	xode xode_get_nextsibling(xode sibling);
+	xode xode_get_prevsibling(xode sibling);
+	xode xode_get_parent(xode node);
+
+	/* Node information routines */
+	char *xode_get_name(xode node);
+	char *xode_get_data(xode node);
+	int xode_get_datasz(xode node);
+	int xode_get_type(xode node);
+
+	int xode_has_children(xode node);
+	int xode_has_attribs(xode node);
+
+	/* Node-to-string translation */
+	char *xode_to_str(xode node);
+	char *xode_to_prettystr(
+			xode node); /* Puts \t and \n to make a human-easily readable string */
+
+	int xode_cmp(xode a, xode b); /* compares a and b for equality */
+
+	int xode_to_file(char *file, xode node); /* writes node to file */
+
+
+	/***********************
  * XSTREAM Section
  * XSTREAM Section
  ***********************/
  ***********************/
 
 
 #define XODE_STREAM_MAXNODE 1000000
 #define XODE_STREAM_MAXNODE 1000000
 #define XODE_STREAM_MAXDEPTH 100
 #define XODE_STREAM_MAXDEPTH 100
 
 
-#define XODE_STREAM_ROOT        0 /* root element */
-#define XODE_STREAM_NODE        1 /* normal node */
-#define XODE_STREAM_CLOSE       2 /* closed root node */
-#define XODE_STREAM_ERROR       4 /* parser error */
-
-typedef void (*xode_stream_onNode)(int type, xode x, void *arg); /* xstream event handler */
-
-typedef struct xode_stream_struct
-{
-    XML_Parser parser;
-    xode node;
-    char *cdata;
-    int cdata_len;
-    xode_pool p;
-    xode_stream_onNode f;
-    void *arg;
-    int status;
-    int depth;
-} *xode_stream, _xode_stream;
-
-xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg); /* create a new xstream */
-int xode_stream_eat(xode_stream xs, char *buff, int len); /* parse new data for this xstream, returns last XSTREAM_* status */
-
-/* convenience functions */
+#define XODE_STREAM_ROOT 0	/* root element */
+#define XODE_STREAM_NODE 1	/* normal node */
+#define XODE_STREAM_CLOSE 2 /* closed root node */
+#define XODE_STREAM_ERROR 4 /* parser error */
+
+	typedef void (*xode_stream_onNode)(
+			int type, xode x, void *arg); /* xstream event handler */
+
+	typedef struct xode_stream_struct
+	{
+		XML_Parser parser;
+		xode node;
+		char *cdata;
+		int cdata_len;
+		xode_pool p;
+		xode_stream_onNode f;
+		void *arg;
+		int status;
+		int depth;
+	} * xode_stream, _xode_stream;
+
+	xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f,
+			void *arg); /* create a new xstream */
+	int xode_stream_eat(xode_stream xs, char *buff,
+			int len); /* parse new data for this xstream, returns last XSTREAM_* status */
+
+	/* convenience functions */
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }

+ 155 - 166
src/modules/xmpp/xode_from.c

@@ -30,207 +30,196 @@
 
 
 static void _xode_put_expatattribs(xode current, const char **atts)
 static void _xode_put_expatattribs(xode current, const char **atts)
 {
 {
-    int i = 0;
-    if (atts == NULL) return;
-    while (*(atts[i]) != '\0')
-    {
-        xode_put_attrib(current, atts[i], atts[i+1]);
-        i += 2;
-    }
+	int i = 0;
+	if(atts == NULL)
+		return;
+	while(*(atts[i]) != '\0') {
+		xode_put_attrib(current, atts[i], atts[i + 1]);
+		i += 2;
+	}
 }
 }
 
 
-static void _xode_expat_startElement(void* userdata, const char* name, const char** atts)
+static void _xode_expat_startElement(
+		void *userdata, const char *name, const char **atts)
 {
 {
-    /* get the xmlnode pointed to by the userdata */
-    xode *x = userdata;
-    xode current = *x;
-
-    if (current == NULL)
-    {
-        /* allocate a base node */
-        current = xode_new(name);
-        _xode_put_expatattribs(current, atts);
-        *x = current;
-    }
-    else
-    {
-        *x = xode_insert_tag(current, name);
-        _xode_put_expatattribs(*x, atts);
-    }
+	/* get the xmlnode pointed to by the userdata */
+	xode *x = userdata;
+	xode current = *x;
+
+	if(current == NULL) {
+		/* allocate a base node */
+		current = xode_new(name);
+		_xode_put_expatattribs(current, atts);
+		*x = current;
+	} else {
+		*x = xode_insert_tag(current, name);
+		_xode_put_expatattribs(*x, atts);
+	}
 }
 }
 
 
-static void _xode_expat_endElement(void* userdata, const char* name)
+static void _xode_expat_endElement(void *userdata, const char *name)
 {
 {
-    xode *x = userdata;
-    xode current = *x;
+	xode *x = userdata;
+	xode current = *x;
 
 
-    current->complete = 1;
-    current = xode_get_parent(current);
+	current->complete = 1;
+	current = xode_get_parent(current);
 
 
-    /* if it's NULL we've hit the top folks, otherwise back up a level */
-    if(current != NULL)
-        *x = current;
+	/* if it's NULL we've hit the top folks, otherwise back up a level */
+	if(current != NULL)
+		*x = current;
 }
 }
 
 
-static void _xode_expat_charData(void* userdata, const char* s, int len)
+static void _xode_expat_charData(void *userdata, const char *s, int len)
 {
 {
-    xode *x = userdata;
-    xode current = *x;
+	xode *x = userdata;
+	xode current = *x;
 
 
-    xode_insert_cdata(current, s, len);
+	xode_insert_cdata(current, s, len);
 }
 }
 
 
 
 
 xode xode_from_str(char *str, int len)
 xode xode_from_str(char *str, int len)
 {
 {
-    XML_Parser p;
-    xode *x, node; /* pointer to an xmlnode */
-
-    if(NULL == str)
-        return NULL;
-
-    if(len == -1)
-        len = strlen(str);
-
-    x = malloc(sizeof(void *));
-
-    *x = NULL; /* pointer to NULL */
-    p = XML_ParserCreate(NULL);
-    XML_SetUserData(p, x);
-    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
-    XML_SetCharacterDataHandler(p, _xode_expat_charData);
-    if(!XML_Parse(p, str, len, 1))
-    {
-        /*        jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
-        xode_free(*x);
-        *x = NULL;
-    }
-    node = *x;
-    free(x);
-    XML_ParserFree(p);
-    return node; /* return the xmlnode x points to */
+	XML_Parser p;
+	xode *x, node; /* pointer to an xmlnode */
+
+	if(NULL == str)
+		return NULL;
+
+	if(len == -1)
+		len = strlen(str);
+
+	x = malloc(sizeof(void *));
+
+	*x = NULL; /* pointer to NULL */
+	p = XML_ParserCreate(NULL);
+	XML_SetUserData(p, x);
+	XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
+	XML_SetCharacterDataHandler(p, _xode_expat_charData);
+	if(!XML_Parse(p, str, len, 1)) {
+		/*        jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
+		xode_free(*x);
+		*x = NULL;
+	}
+	node = *x;
+	free(x);
+	XML_ParserFree(p);
+	return node; /* return the xmlnode x points to */
 }
 }
 
 
 xode xode_from_strx(char *str, int len, int *err, int *pos)
 xode xode_from_strx(char *str, int len, int *err, int *pos)
 {
 {
-    XML_Parser p;
-    xode *x, node; /* pointer to an xmlnode */
+	XML_Parser p;
+	xode *x, node; /* pointer to an xmlnode */
 
 
-    if(NULL == str)
-        return NULL;
+	if(NULL == str)
+		return NULL;
 
 
-    if(len == -1)
-        len = strlen(str);
+	if(len == -1)
+		len = strlen(str);
 
 
-    x = malloc(sizeof(void *));
+	x = malloc(sizeof(void *));
 
 
-    *x = NULL; /* pointer to NULL */
-    p = XML_ParserCreate(NULL);
-    XML_SetUserData(p, x);
-    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
-    XML_SetCharacterDataHandler(p, _xode_expat_charData);
-    XML_Parse(p, str, len, 0);
+	*x = NULL; /* pointer to NULL */
+	p = XML_ParserCreate(NULL);
+	XML_SetUserData(p, x);
+	XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
+	XML_SetCharacterDataHandler(p, _xode_expat_charData);
+	XML_Parse(p, str, len, 0);
 	if(err != NULL)
 	if(err != NULL)
 		*err = XML_GetErrorCode(p);
 		*err = XML_GetErrorCode(p);
 	if(pos != NULL)
 	if(pos != NULL)
-		*pos = XML_GetCurrentByteIndex(p);		
-    node = *x;
-    free(x);
-    XML_ParserFree(p);
-    
+		*pos = XML_GetCurrentByteIndex(p);
+	node = *x;
+	free(x);
+	XML_ParserFree(p);
+
 	return node; /* return the xmlnode x points to */
 	return node; /* return the xmlnode x points to */
 }
 }
 
 
 xode xode_from_file(char *file)
 xode xode_from_file(char *file)
 {
 {
-    XML_Parser p;
-    xode *x, node; /* pointer to an xmlnode */
-    char buf[BUFSIZ];
-    int done, fd, len;
-    char _file[1000];
-
-    if(NULL == file)
-        return NULL;
-
-    /* perform tilde expansion */
-    if(*file == '~')
-    {
-        char *env = getenv("HOME");
-        if(env != NULL)
-            snprintf((char*)_file, 1000, "%s%s", env, file + 1);
-        else
-            snprintf((char*)_file, 1000, "%s", file);
-    }
-    else
-    {
-        snprintf((char*)_file, 1000, "%s", file);
-    }
-
-    fd = open((char*)&_file,O_RDONLY);
-    if(fd < 0)
-        return NULL;
-
-    x = malloc(sizeof(void *));
-
-    *x = NULL; /* pointer to NULL */
-    p = XML_ParserCreate(NULL);
-    XML_SetUserData(p, x);
-    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
-    XML_SetCharacterDataHandler(p, _xode_expat_charData);
-    do{
-        len = read(fd, buf, BUFSIZ);
-        done = len < BUFSIZ;
-        if(!XML_Parse(p, buf, len, done))
-        {
-            /*            jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
-            xode_free(*x);
-            *x = NULL;
-            done = 1;
-        }
-    }while(!done);
-
-    node = *x;
-    XML_ParserFree(p);
-    free(x);
-    close(fd);
-    return node; /* return the xmlnode x points to */
+	XML_Parser p;
+	xode *x, node; /* pointer to an xmlnode */
+	char buf[BUFSIZ];
+	int done, fd, len;
+	char _file[1000];
+
+	if(NULL == file)
+		return NULL;
+
+	/* perform tilde expansion */
+	if(*file == '~') {
+		char *env = getenv("HOME");
+		if(env != NULL)
+			snprintf((char *)_file, 1000, "%s%s", env, file + 1);
+		else
+			snprintf((char *)_file, 1000, "%s", file);
+	} else {
+		snprintf((char *)_file, 1000, "%s", file);
+	}
+
+	fd = open((char *)&_file, O_RDONLY);
+	if(fd < 0)
+		return NULL;
+
+	x = malloc(sizeof(void *));
+
+	*x = NULL; /* pointer to NULL */
+	p = XML_ParserCreate(NULL);
+	XML_SetUserData(p, x);
+	XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
+	XML_SetCharacterDataHandler(p, _xode_expat_charData);
+	do {
+		len = read(fd, buf, BUFSIZ);
+		done = len < BUFSIZ;
+		if(!XML_Parse(p, buf, len, done)) {
+			/*            jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
+			xode_free(*x);
+			*x = NULL;
+			done = 1;
+		}
+	} while(!done);
+
+	node = *x;
+	XML_ParserFree(p);
+	free(x);
+	close(fd);
+	return node; /* return the xmlnode x points to */
 }
 }
 
 
 int xode_to_file(char *file, xode node)
 int xode_to_file(char *file, xode node)
 {
 {
-    char *doc;
-    int fd, i;
-    char _file[1000];
-
-    if(file == NULL || node == NULL)
-        return -1;
-
-    /* perform tilde expansion */
-    if(*file == '~')
-    {
-        char *env = getenv("HOME");
-        if(env != NULL)
-            snprintf((char*)_file, 1000, "%s%s", env, file + 1);
-        else
-            snprintf((char*)_file, 1000, "%s", file);
-    }
-    else
-    {
-        snprintf((char*)_file, 1000, "%s", file);
-    }
-
-    fd = open((char*)&_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
-    if(fd < 0)
-        return -1;
-
-    doc = xode_to_str(node);
-    i = write(fd,doc,strlen(doc));
-    if(i < 0) {
-        close(fd);
-        return -1;
-    }
-
-    close(fd);
-    return 1;
+	char *doc;
+	int fd, i;
+	char _file[1000];
+
+	if(file == NULL || node == NULL)
+		return -1;
+
+	/* perform tilde expansion */
+	if(*file == '~') {
+		char *env = getenv("HOME");
+		if(env != NULL)
+			snprintf((char *)_file, 1000, "%s%s", env, file + 1);
+		else
+			snprintf((char *)_file, 1000, "%s", file);
+	} else {
+		snprintf((char *)_file, 1000, "%s", file);
+	}
+
+	fd = open((char *)&_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
+	if(fd < 0)
+		return -1;
+
+	doc = xode_to_str(node);
+	i = write(fd, doc, strlen(doc));
+	if(i < 0) {
+		close(fd);
+		return -1;
+	}
+
+	close(fd);
+	return 1;
 }
 }
-

+ 182 - 186
src/modules/xmpp/xode_str.c

@@ -26,249 +26,245 @@
 
 
 xode_pool xode_spool_getpool(const xode_spool s)
 xode_pool xode_spool_getpool(const xode_spool s)
 {
 {
-    if(s == NULL)
-        return NULL;
+	if(s == NULL)
+		return NULL;
 
 
-    return s->p;
+	return s->p;
 }
 }
 
 
 int xode_spool_getlen(const xode_spool s)
 int xode_spool_getlen(const xode_spool s)
 {
 {
-    if(s == NULL)
-        return 0;
+	if(s == NULL)
+		return 0;
 
 
-    return s->len;    
+	return s->len;
 }
 }
 
 
 void xode_spool_free(xode_spool s)
 void xode_spool_free(xode_spool s)
 {
 {
-    xode_pool_free(xode_spool_getpool(s));
+	xode_pool_free(xode_spool_getpool(s));
 }
 }
 
 
 xode_spool xode_spool_newfrompool(xode_pool p)
 xode_spool xode_spool_newfrompool(xode_pool p)
 {
 {
-    xode_spool s;
-
-    s = xode_pool_malloc(p, sizeof(struct xode_spool_struct));
-    s->p = p;
-    s->len = 0;
-    s->last = NULL;
-    s->first = NULL;
-    return s;
+	xode_spool s;
+
+	s = xode_pool_malloc(p, sizeof(struct xode_spool_struct));
+	s->p = p;
+	s->len = 0;
+	s->last = NULL;
+	s->first = NULL;
+	return s;
 }
 }
 
 
 xode_spool xode_spool_new(void)
 xode_spool xode_spool_new(void)
 {
 {
-    return xode_spool_newfrompool(xode_pool_heap(512));
+	return xode_spool_newfrompool(xode_pool_heap(512));
 }
 }
 
 
 void xode_spool_add(xode_spool s, char *str)
 void xode_spool_add(xode_spool s, char *str)
 {
 {
-    struct xode_spool_node *sn;
-    int len;
-
-    if(str == NULL)
-        return;
-
-    len = strlen(str);
-    if(len == 0)
-        return;
-
-    sn = xode_pool_malloc(s->p, sizeof(struct xode_spool_node));
-    sn->c = xode_pool_strdup(s->p, str);
-    sn->next = NULL;
-
-    s->len += len;
-    if(s->last != NULL)
-        s->last->next = sn;
-    s->last = sn;
-    if(s->first == NULL)
-        s->first = sn;
+	struct xode_spool_node *sn;
+	int len;
+
+	if(str == NULL)
+		return;
+
+	len = strlen(str);
+	if(len == 0)
+		return;
+
+	sn = xode_pool_malloc(s->p, sizeof(struct xode_spool_node));
+	sn->c = xode_pool_strdup(s->p, str);
+	sn->next = NULL;
+
+	s->len += len;
+	if(s->last != NULL)
+		s->last->next = sn;
+	s->last = sn;
+	if(s->first == NULL)
+		s->first = sn;
 }
 }
 
 
 void xode_spooler(xode_spool s, ...)
 void xode_spooler(xode_spool s, ...)
 {
 {
-    va_list ap;
-    char *arg = NULL;
+	va_list ap;
+	char *arg = NULL;
 
 
-    if(s == NULL)
-        return;
+	if(s == NULL)
+		return;
 
 
-    va_start(ap, s);
+	va_start(ap, s);
 
 
-    /* loop till we hit our end flag, the first arg */
-    while(1)
-    {
-        arg = va_arg(ap,char *);
-        if((void*)arg == (void*)s || arg == NULL)
-            break;
-        else
-            xode_spool_add(s, arg);
-    }
+	/* loop till we hit our end flag, the first arg */
+	while(1) {
+		arg = va_arg(ap, char *);
+		if((void *)arg == (void *)s || arg == NULL)
+			break;
+		else
+			xode_spool_add(s, arg);
+	}
 
 
-    va_end(ap);
+	va_end(ap);
 }
 }
 
 
 char *xode_spool_tostr(xode_spool s)
 char *xode_spool_tostr(xode_spool s)
 {
 {
-    char *ret,*tmp;
-    struct xode_spool_node *next;
+	char *ret, *tmp;
+	struct xode_spool_node *next;
 
 
-    if(s == NULL || s->len == 0 || s->first == NULL)
-        return NULL;
+	if(s == NULL || s->len == 0 || s->first == NULL)
+		return NULL;
 
 
-    ret = xode_pool_malloc(s->p, s->len + 1);
-    *ret = '\0';
+	ret = xode_pool_malloc(s->p, s->len + 1);
+	*ret = '\0';
 
 
-    next = s->first;
-    tmp = ret;
-    while(next != NULL)
-    {
-        tmp = strcat(tmp,next->c);
-        next = next->next;
-    }
+	next = s->first;
+	tmp = ret;
+	while(next != NULL) {
+		tmp = strcat(tmp, next->c);
+		next = next->next;
+	}
 
 
-    return ret;
+	return ret;
 }
 }
 
 
 /* convenience :) */
 /* convenience :) */
 char *xode_spool_str(xode_pool p, ...)
 char *xode_spool_str(xode_pool p, ...)
 {
 {
-    va_list ap;
-    xode_spool s;
-    char *arg = NULL;
+	va_list ap;
+	xode_spool s;
+	char *arg = NULL;
 
 
-    if(p == NULL)
-        return NULL;
+	if(p == NULL)
+		return NULL;
 
 
-    s = xode_spool_newfrompool(p);
+	s = xode_spool_newfrompool(p);
 
 
-    va_start(ap, p);
+	va_start(ap, p);
 
 
-    /* loop till we hit our end flag, the first arg */
-    while(1)
-    {
-        arg = va_arg(ap,char *);
-        if((void*)arg == (void*)p)
-            break;
-        else
-            xode_spool_add(s, arg);
-    }
+	/* loop till we hit our end flag, the first arg */
+	while(1) {
+		arg = va_arg(ap, char *);
+		if((void *)arg == (void *)p)
+			break;
+		else
+			xode_spool_add(s, arg);
+	}
 
 
-    va_end(ap);
+	va_end(ap);
 
 
-    return xode_spool_tostr(s);
+	return xode_spool_tostr(s);
 }
 }
 
 
 
 
 char *xode_strunescape(xode_pool p, char *buf)
 char *xode_strunescape(xode_pool p, char *buf)
 {
 {
-    int i,j=0;
-    char *temp;
-
-    if (p == NULL || buf == NULL) return(NULL);
-
-    if (strchr(buf,'&') == NULL) return(buf);
-
-    temp = xode_pool_malloc(p,strlen(buf)+1);
-
-    if (temp == NULL) return(NULL);
-
-    for(i=0;i<strlen(buf);i++)
-    {
-        if (buf[i]=='&')
-        {
-            if (strncmp(&buf[i],"&amp;",5)==0)
-            {
-                temp[j] = '&';
-                i += 4;
-            } else if (strncmp(&buf[i],"&quot;",6)==0) {
-                temp[j] = '\"';
-                i += 5;
-            } else if (strncmp(&buf[i],"&apos;",6)==0) {
-                temp[j] = '\'';
-                i += 5;
-            } else if (strncmp(&buf[i],"&lt;",4)==0) {
-                temp[j] = '<';
-                i += 3;
-            } else if (strncmp(&buf[i],"&gt;",4)==0) {
-                temp[j] = '>';
-                i += 3;
-            }
-        } else {
-            temp[j]=buf[i];
-        }
-        j++;
-    }
-    temp[j]='\0';
-    return(temp);
+	int i, j = 0;
+	char *temp;
+
+	if(p == NULL || buf == NULL)
+		return (NULL);
+
+	if(strchr(buf, '&') == NULL)
+		return (buf);
+
+	temp = xode_pool_malloc(p, strlen(buf) + 1);
+
+	if(temp == NULL)
+		return (NULL);
+
+	for(i = 0; i < strlen(buf); i++) {
+		if(buf[i] == '&') {
+			if(strncmp(&buf[i], "&amp;", 5) == 0) {
+				temp[j] = '&';
+				i += 4;
+			} else if(strncmp(&buf[i], "&quot;", 6) == 0) {
+				temp[j] = '\"';
+				i += 5;
+			} else if(strncmp(&buf[i], "&apos;", 6) == 0) {
+				temp[j] = '\'';
+				i += 5;
+			} else if(strncmp(&buf[i], "&lt;", 4) == 0) {
+				temp[j] = '<';
+				i += 3;
+			} else if(strncmp(&buf[i], "&gt;", 4) == 0) {
+				temp[j] = '>';
+				i += 3;
+			}
+		} else {
+			temp[j] = buf[i];
+		}
+		j++;
+	}
+	temp[j] = '\0';
+	return (temp);
 }
 }
 
 
 
 
 char *xode_strescape(xode_pool p, char *buf)
 char *xode_strescape(xode_pool p, char *buf)
 {
 {
-    int i,j,oldlen,newlen;
-    char *temp;
-
-    if (p == NULL || buf == NULL) return(NULL);
-
-    oldlen = newlen = strlen(buf);
-    for(i=0;i<oldlen;i++)
-    {
-        switch(buf[i])
-        {
-        case '&':
-            newlen+=5;
-            break;
-        case '\'':
-            newlen+=6;
-            break;
-        case '\"':
-            newlen+=6;
-            break;
-        case '<':
-            newlen+=4;
-            break;
-        case '>':
-            newlen+=4;
-            break;
-        }
-    }
-
-    if(oldlen == newlen) return buf;
-
-    temp = xode_pool_malloc(p,newlen+1);
-
-    if (temp==NULL) return(NULL);
-
-    for(i=j=0;i<oldlen;i++)
-    {
-        switch(buf[i])
-        {
-        case '&':
-            memcpy(&temp[j],"&amp;",5);
-            j += 5;
-            break;
-        case '\'':
-            memcpy(&temp[j],"&apos;",6);
-            j += 6;
-            break;
-        case '\"':
-            memcpy(&temp[j],"&quot;",6);
-            j += 6;
-            break;
-        case '<':
-            memcpy(&temp[j],"&lt;",4);
-            j += 4;
-            break;
-        case '>':
-            memcpy(&temp[j],"&gt;",4);
-            j += 4;
-            break;
-        default:
-            temp[j++] = buf[i];
-        }
-    }
-    temp[j] = '\0';
-    return temp;
+	int i, j, oldlen, newlen;
+	char *temp;
+
+	if(p == NULL || buf == NULL)
+		return (NULL);
+
+	oldlen = newlen = strlen(buf);
+	for(i = 0; i < oldlen; i++) {
+		switch(buf[i]) {
+			case '&':
+				newlen += 5;
+				break;
+			case '\'':
+				newlen += 6;
+				break;
+			case '\"':
+				newlen += 6;
+				break;
+			case '<':
+				newlen += 4;
+				break;
+			case '>':
+				newlen += 4;
+				break;
+		}
+	}
+
+	if(oldlen == newlen)
+		return buf;
+
+	temp = xode_pool_malloc(p, newlen + 1);
+
+	if(temp == NULL)
+		return (NULL);
+
+	for(i = j = 0; i < oldlen; i++) {
+		switch(buf[i]) {
+			case '&':
+				memcpy(&temp[j], "&amp;", 5);
+				j += 5;
+				break;
+			case '\'':
+				memcpy(&temp[j], "&apos;", 6);
+				j += 6;
+				break;
+			case '\"':
+				memcpy(&temp[j], "&quot;", 6);
+				j += 6;
+				break;
+			case '<':
+				memcpy(&temp[j], "&lt;", 4);
+				j += 4;
+				break;
+			case '>':
+				memcpy(&temp[j], "&gt;", 4);
+				j += 4;
+				break;
+			default:
+				temp[j++] = buf[i];
+		}
+	}
+	temp[j] = '\0';
+	return temp;
 }
 }

+ 124 - 116
src/modules/xmpp/xpool.c

@@ -42,209 +42,217 @@
 //#include "config.h"
 //#include "config.h"
 
 
 #define _xode_pool__malloc malloc
 #define _xode_pool__malloc malloc
-#define _xode_pool__free   free
+#define _xode_pool__free free
 
 
 /* xode_pfree - a linked list node which stores an
 /* xode_pfree - a linked list node which stores an
    allocation chunk, plus a callback */
    allocation chunk, plus a callback */
 struct xode_pool_free
 struct xode_pool_free
 {
 {
-    xode_pool_cleaner f;
-    void *arg;
-    struct xode_pool_heap *heap;
-    struct xode_pool_free *next;
+	xode_pool_cleaner f;
+	void *arg;
+	struct xode_pool_heap *heap;
+	struct xode_pool_free *next;
 };
 };
 
 
 /* make an empty pool */
 /* make an empty pool */
 xode_pool _xode_pool_new(void)
 xode_pool _xode_pool_new(void)
 {
 {
-    xode_pool p;
-    while((p = _xode_pool__malloc(sizeof(_xode_pool))) == NULL) sleep(1);
-    p->cleanup = NULL;
-    p->heap = NULL;
-    p->size = 0;
-
-    return p;
+	xode_pool p;
+	while((p = _xode_pool__malloc(sizeof(_xode_pool))) == NULL)
+		sleep(1);
+	p->cleanup = NULL;
+	p->heap = NULL;
+	p->size = 0;
+
+	return p;
 }
 }
 
 
 /* free a heap */
 /* free a heap */
 void _xode_pool_heapfree(void *arg)
 void _xode_pool_heapfree(void *arg)
 {
 {
-    struct xode_pool_heap *h = (struct xode_pool_heap *)arg;
+	struct xode_pool_heap *h = (struct xode_pool_heap *)arg;
 
 
-    _xode_pool__free(h->block);
-    _xode_pool__free(h);
+	_xode_pool__free(h->block);
+	_xode_pool__free(h);
 }
 }
 
 
 /* mem should always be freed last */
 /* mem should always be freed last */
 void _xode_pool_cleanup_append(xode_pool p, struct xode_pool_free *pf)
 void _xode_pool_cleanup_append(xode_pool p, struct xode_pool_free *pf)
 {
 {
-    struct xode_pool_free *cur;
+	struct xode_pool_free *cur;
 
 
-    if(p->cleanup == NULL)
-    {
-        p->cleanup = pf;
-        return;
-    }
+	if(p->cleanup == NULL) {
+		p->cleanup = pf;
+		return;
+	}
 
 
-    /* fast forward to end of list */
-    for(cur = p->cleanup; cur->next != NULL; cur = cur->next);
+	/* fast forward to end of list */
+	for(cur = p->cleanup; cur->next != NULL; cur = cur->next)
+		;
 
 
-    cur->next = pf;
+	cur->next = pf;
 }
 }
 
 
 /* create a cleanup tracker */
 /* create a cleanup tracker */
-struct xode_pool_free *_xode_pool_free(xode_pool p, xode_pool_cleaner f, void *arg)
+struct xode_pool_free *_xode_pool_free(
+		xode_pool p, xode_pool_cleaner f, void *arg)
 {
 {
-    struct xode_pool_free *ret;
+	struct xode_pool_free *ret;
 
 
-    /* make the storage for the tracker */
-    while((ret = _xode_pool__malloc(sizeof(struct xode_pool_free))) == NULL) sleep(1);
-    ret->f = f;
-    ret->arg = arg;
-    ret->next = NULL;
+	/* make the storage for the tracker */
+	while((ret = _xode_pool__malloc(sizeof(struct xode_pool_free))) == NULL)
+		sleep(1);
+	ret->f = f;
+	ret->arg = arg;
+	ret->next = NULL;
 
 
-    return ret;
+	return ret;
 }
 }
 
 
 /* create a heap and make sure it get's cleaned up */
 /* create a heap and make sure it get's cleaned up */
 struct xode_pool_heap *_xode_pool_heap(xode_pool p, int size)
 struct xode_pool_heap *_xode_pool_heap(xode_pool p, int size)
 {
 {
-    struct xode_pool_heap *ret;
-    struct xode_pool_free *clean;
-
-    /* make the return heap */
-    while((ret = _xode_pool__malloc(sizeof(struct xode_pool_heap))) == NULL) sleep(1);
-    while((ret->block = _xode_pool__malloc(size)) == NULL) sleep(1);
-    ret->size = size;
-    p->size += size;
-    ret->used = 0;
-
-    /* append to the cleanup list */
-    clean = _xode_pool_free(p, _xode_pool_heapfree, (void *)ret);
-    clean->heap = ret; /* for future use in finding used mem for pstrdup */
-    _xode_pool_cleanup_append(p, clean);
-
-    return ret;
+	struct xode_pool_heap *ret;
+	struct xode_pool_free *clean;
+
+	/* make the return heap */
+	while((ret = _xode_pool__malloc(sizeof(struct xode_pool_heap))) == NULL)
+		sleep(1);
+	while((ret->block = _xode_pool__malloc(size)) == NULL)
+		sleep(1);
+	ret->size = size;
+	p->size += size;
+	ret->used = 0;
+
+	/* append to the cleanup list */
+	clean = _xode_pool_free(p, _xode_pool_heapfree, (void *)ret);
+	clean->heap = ret; /* for future use in finding used mem for pstrdup */
+	_xode_pool_cleanup_append(p, clean);
+
+	return ret;
 }
 }
 
 
 xode_pool _xode_pool_newheap(int bytes)
 xode_pool _xode_pool_newheap(int bytes)
 {
 {
-    xode_pool p;
-    p = _xode_pool_new();
-    p->heap = _xode_pool_heap(p,bytes);
-    return p;
+	xode_pool p;
+	p = _xode_pool_new();
+	p->heap = _xode_pool_heap(p, bytes);
+	return p;
 }
 }
 
 
 void *xode_pool_malloc(xode_pool p, int size)
 void *xode_pool_malloc(xode_pool p, int size)
 {
 {
-    void *block;
-
-    if(p == NULL)
-    {
-        fprintf(stderr,"Memory Leak! xode_pmalloc received NULL pool, unable to track allocation, exiting]\n");
-        abort();
-    }
-
-    /* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */
-    if(p->heap == NULL || size > (p->heap->size / 2))
-    {
-        while((block = _xode_pool__malloc(size)) == NULL) sleep(1);
-        p->size += size;
-        _xode_pool_cleanup_append(p, _xode_pool_free(p, _xode_pool__free, block));
-        return block;
-    }
-
-    /* we have to preserve boundaries, long story :) */
-    if(size >= 4)
-        while(p->heap->used&7) p->heap->used++;
-
-    /* if we don't fit in the old heap, replace it */
-    if(size > (p->heap->size - p->heap->used))
-        p->heap = _xode_pool_heap(p, p->heap->size);
-
-    /* the current heap has room */
-    block = (char *)p->heap->block + p->heap->used;
-    p->heap->used += size;
-    return block;
+	void *block;
+
+	if(p == NULL) {
+		fprintf(stderr, "Memory Leak! xode_pmalloc received NULL pool, unable "
+						"to track allocation, exiting]\n");
+		abort();
+	}
+
+	/* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */
+	if(p->heap == NULL || size > (p->heap->size / 2)) {
+		while((block = _xode_pool__malloc(size)) == NULL)
+			sleep(1);
+		p->size += size;
+		_xode_pool_cleanup_append(
+				p, _xode_pool_free(p, _xode_pool__free, block));
+		return block;
+	}
+
+	/* we have to preserve boundaries, long story :) */
+	if(size >= 4)
+		while(p->heap->used & 7)
+			p->heap->used++;
+
+	/* if we don't fit in the old heap, replace it */
+	if(size > (p->heap->size - p->heap->used))
+		p->heap = _xode_pool_heap(p, p->heap->size);
+
+	/* the current heap has room */
+	block = (char *)p->heap->block + p->heap->used;
+	p->heap->used += size;
+	return block;
 }
 }
 
 
 void *xode_pool_mallocx(xode_pool p, int size, char c)
 void *xode_pool_mallocx(xode_pool p, int size, char c)
 {
 {
-   void* result = xode_pool_malloc(p, size);
-   if (result != NULL)
-           memset(result, c, size);
-   return result;
-}  
+	void *result = xode_pool_malloc(p, size);
+	if(result != NULL)
+		memset(result, c, size);
+	return result;
+}
 
 
 /* easy safety utility (for creating blank mem for structs, etc) */
 /* easy safety utility (for creating blank mem for structs, etc) */
 void *xode_pool_malloco(xode_pool p, int size)
 void *xode_pool_malloco(xode_pool p, int size)
 {
 {
-    void *block = xode_pool_malloc(p, size);
-    memset(block, 0, size);
-    return block;
-}  
+	void *block = xode_pool_malloc(p, size);
+	memset(block, 0, size);
+	return block;
+}
 
 
 /* XXX efficient: move this to const char * and then loop through the existing heaps to see if src is within a block in this pool */
 /* XXX efficient: move this to const char * and then loop through the existing heaps to see if src is within a block in this pool */
 char *xode_pool_strdup(xode_pool p, const char *src)
 char *xode_pool_strdup(xode_pool p, const char *src)
 {
 {
-    char *ret;
+	char *ret;
 
 
-    if(src == NULL)
-        return NULL;
+	if(src == NULL)
+		return NULL;
 
 
-    ret = xode_pool_malloc(p,strlen(src) + 1);
-    strcpy(ret,src);
+	ret = xode_pool_malloc(p, strlen(src) + 1);
+	strcpy(ret, src);
 
 
-    return ret;
+	return ret;
 }
 }
 
 
 /* when move above, this one would actually return a new block */
 /* when move above, this one would actually return a new block */
 char *xode_pool_strdupx(xode_pool p, const char *src)
 char *xode_pool_strdupx(xode_pool p, const char *src)
 {
 {
-    return xode_pool_strdup(p, src);
+	return xode_pool_strdup(p, src);
 }
 }
 
 
 int xode_pool_size(xode_pool p)
 int xode_pool_size(xode_pool p)
 {
 {
-    if(p == NULL) return 0;
+	if(p == NULL)
+		return 0;
 
 
-    return p->size;
+	return p->size;
 }
 }
 
 
 void xode_pool_free(xode_pool p)
 void xode_pool_free(xode_pool p)
 {
 {
-    struct xode_pool_free *cur, *stub;
+	struct xode_pool_free *cur, *stub;
 
 
-    if(p == NULL) return;
+	if(p == NULL)
+		return;
 
 
-    cur = p->cleanup;
-    while(cur != NULL)
-    {
-        (*cur->f)(cur->arg);
-        stub = cur->next;
-        _xode_pool__free(cur);
-        cur = stub;
-    }
+	cur = p->cleanup;
+	while(cur != NULL) {
+		(*cur->f)(cur->arg);
+		stub = cur->next;
+		_xode_pool__free(cur);
+		cur = stub;
+	}
 
 
-    _xode_pool__free(p);
+	_xode_pool__free(p);
 }
 }
 
 
 /* public cleanup utils, insert in a way that they are run FIFO, before mem frees */
 /* public cleanup utils, insert in a way that they are run FIFO, before mem frees */
 void xode_pool_cleanup(xode_pool p, xode_pool_cleaner f, void *arg)
 void xode_pool_cleanup(xode_pool p, xode_pool_cleaner f, void *arg)
 {
 {
-    struct xode_pool_free *clean;
+	struct xode_pool_free *clean;
 
 
-    clean = _xode_pool_free(p, f, arg);
-    clean->next = p->cleanup;
-    p->cleanup = clean;
+	clean = _xode_pool_free(p, f, arg);
+	clean->next = p->cleanup;
+	p->cleanup = clean;
 }
 }
 
 
 xode_pool xode_pool_new(void)
 xode_pool xode_pool_new(void)
 {
 {
-    return _xode_pool_new();
+	return _xode_pool_new();
 }
 }
 
 
 xode_pool xode_pool_heap(const int bytes)
 xode_pool xode_pool_heap(const int bytes)
 {
 {
-    return _xode_pool_newheap(bytes);
+	return _xode_pool_newheap(bytes);
 }
 }

Plik diff jest za duży
+ 523 - 529
src/modules/xmpp/xsnprintf.c


+ 134 - 130
src/modules/xmpp/xstream.c

@@ -27,167 +27,171 @@
 /* xode_stream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
 /* xode_stream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
 
 
 
 
-static void _xode_put_expatattribs(xode owner, const char** atts)
+static void _xode_put_expatattribs(xode owner, const char **atts)
 {
 {
-    int i = 0;
-    if (atts == NULL) return;
-    while (*(atts[i]) != '\0')
-    {
-        xode_put_attrib(owner, atts[i], atts[i+1]);
-        i += 2;
-    }
+	int i = 0;
+	if(atts == NULL)
+		return;
+	while(*(atts[i]) != '\0') {
+		xode_put_attrib(owner, atts[i], atts[i + 1]);
+		i += 2;
+	}
 }
 }
 
 
 /******* internal expat callbacks *********/
 /******* internal expat callbacks *********/
-static void _xode_stream_startElement(xode_stream xs, const char* name, const char** atts)
+static void _xode_stream_startElement(
+		xode_stream xs, const char *name, const char **atts)
 {
 {
-    xode_pool p;
-
-    /* if xode_stream is bad, get outa here */
-    if(xs->status > XODE_STREAM_NODE) return;
-
-    if(xs->node == NULL)
-    {
-        p = xode_pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
-        xs->node = xode_new_frompool(p,name);
-        _xode_put_expatattribs(xs->node, atts);
-
-        if(xs->status == XODE_STREAM_ROOT)
-        {
-            xs->status = XODE_STREAM_NODE; /* flag status that we're processing nodes now */
-            (xs->f)(XODE_STREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
-            xs->node = NULL;
-        }
-    }else{
-        xs->node = xode_insert_tag(xs->node, name);
-        _xode_put_expatattribs(xs->node, atts);
-    }
-
-    /* depth check */
-    xs->depth++;
-    if(xs->depth > XODE_STREAM_MAXDEPTH)
-        xs->status = XODE_STREAM_ERROR;
+	xode_pool p;
+
+	/* if xode_stream is bad, get outa here */
+	if(xs->status > XODE_STREAM_NODE)
+		return;
+
+	if(xs->node == NULL) {
+		p = xode_pool_heap(
+				5
+				* 1024); /* 5k, typically 1-2k each plus copy of self and workspace */
+		xs->node = xode_new_frompool(p, name);
+		_xode_put_expatattribs(xs->node, atts);
+
+		if(xs->status == XODE_STREAM_ROOT) {
+			xs->status =
+					XODE_STREAM_NODE; /* flag status that we're processing nodes now */
+			(xs->f)(XODE_STREAM_ROOT, xs->node,
+					xs->arg); /* send the root, f must free all nodes */
+			xs->node = NULL;
+		}
+	} else {
+		xs->node = xode_insert_tag(xs->node, name);
+		_xode_put_expatattribs(xs->node, atts);
+	}
+
+	/* depth check */
+	xs->depth++;
+	if(xs->depth > XODE_STREAM_MAXDEPTH)
+		xs->status = XODE_STREAM_ERROR;
 }
 }
 
 
 
 
-static void _xode_stream_endElement(xode_stream xs, const char* name)
+static void _xode_stream_endElement(xode_stream xs, const char *name)
 {
 {
-    xode parent;
-
-    /* if xode_stream is bad, get outa here */
-    if(xs->status > XODE_STREAM_NODE) return;
-
-    /* if it's already NULL we've received </stream>, tell the app and we're outta here */
-    if(xs->node == NULL)
-    {
-        xs->status = XODE_STREAM_CLOSE;
-        (xs->f)(XODE_STREAM_CLOSE, NULL, xs->arg);
-    }else{
-        parent = xode_get_parent(xs->node);
-
-        /* we are the top-most node, feed to the app who is responsible to delete it */
-        if(parent == NULL)
-            (xs->f)(XODE_STREAM_NODE, xs->node, xs->arg);
-
-        xs->node = parent;
-    }
-    xs->depth--;
+	xode parent;
+
+	/* if xode_stream is bad, get outa here */
+	if(xs->status > XODE_STREAM_NODE)
+		return;
+
+	/* if it's already NULL we've received </stream>, tell the app and we're outta here */
+	if(xs->node == NULL) {
+		xs->status = XODE_STREAM_CLOSE;
+		(xs->f)(XODE_STREAM_CLOSE, NULL, xs->arg);
+	} else {
+		parent = xode_get_parent(xs->node);
+
+		/* we are the top-most node, feed to the app who is responsible to delete it */
+		if(parent == NULL)
+			(xs->f)(XODE_STREAM_NODE, xs->node, xs->arg);
+
+		xs->node = parent;
+	}
+	xs->depth--;
 }
 }
 
 
 
 
 static void _xode_stream_charData(xode_stream xs, const char *str, int len)
 static void _xode_stream_charData(xode_stream xs, const char *str, int len)
 {
 {
-    /* if xode_stream is bad, get outa here */
-    if(xs->status > XODE_STREAM_NODE) return;
+	/* if xode_stream is bad, get outa here */
+	if(xs->status > XODE_STREAM_NODE)
+		return;
 
 
-    if(xs->node == NULL)
-    {
-        /* we must be in the root of the stream where CDATA is irrelevant */
-        return;
-    }
+	if(xs->node == NULL) {
+		/* we must be in the root of the stream where CDATA is irrelevant */
+		return;
+	}
 
 
-    xode_insert_cdata(xs->node, str, len);
+	xode_insert_cdata(xs->node, str, len);
 }
 }
 
 
 
 
 static void _xode_stream_cleanup(void *arg)
 static void _xode_stream_cleanup(void *arg)
 {
 {
-    xode_stream xs = (xode_stream)arg;
+	xode_stream xs = (xode_stream)arg;
 
 
-    xode_free(xs->node); /* cleanup anything left over */
-    XML_ParserFree(xs->parser);
+	xode_free(xs->node); /* cleanup anything left over */
+	XML_ParserFree(xs->parser);
 }
 }
 
 
 
 
 /* creates a new xode_stream with given pool, xode_stream will be cleaned up w/ pool */
 /* creates a new xode_stream with given pool, xode_stream will be cleaned up w/ pool */
 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg)
 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg)
 {
 {
-    xode_stream newx;
-
-    if(p == NULL || f == NULL)
-    {
-        fprintf(stderr,"Fatal Programming Error: xode_streamnew() was improperly called with NULL.\n");
-        return NULL;
-    }
-
-    newx = xode_pool_malloco(p, sizeof(_xode_stream));
-    newx->p = p;
-    newx->f = f;
-    newx->arg = arg;
-
-    /* create expat parser and ensure cleanup */
-    newx->parser = XML_ParserCreate(NULL);
-    XML_SetUserData(newx->parser, (void *)newx);
-    XML_SetElementHandler(newx->parser,
-		(void (*)(void*, const char*, const char**))_xode_stream_startElement,
-		(void (*)(void*, const char*))_xode_stream_endElement);
-    XML_SetCharacterDataHandler(newx->parser, 
-		(void (*)(void*, const char*, int))_xode_stream_charData);
-    xode_pool_cleanup(p, _xode_stream_cleanup, (void *)newx);
-
-    return newx;
+	xode_stream newx;
+
+	if(p == NULL || f == NULL) {
+		fprintf(stderr, "Fatal Programming Error: xode_streamnew() was "
+						"improperly called with NULL.\n");
+		return NULL;
+	}
+
+	newx = xode_pool_malloco(p, sizeof(_xode_stream));
+	newx->p = p;
+	newx->f = f;
+	newx->arg = arg;
+
+	/* create expat parser and ensure cleanup */
+	newx->parser = XML_ParserCreate(NULL);
+	XML_SetUserData(newx->parser, (void *)newx);
+	XML_SetElementHandler(newx->parser,
+			(void (*)(void *, const char *,
+					const char **))_xode_stream_startElement,
+			(void (*)(void *, const char *))_xode_stream_endElement);
+	XML_SetCharacterDataHandler(newx->parser,
+			(void (*)(void *, const char *, int))_xode_stream_charData);
+	xode_pool_cleanup(p, _xode_stream_cleanup, (void *)newx);
+
+	return newx;
 }
 }
 
 
 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
 int xode_stream_eat(xode_stream xs, char *buff, int len)
 int xode_stream_eat(xode_stream xs, char *buff, int len)
 {
 {
-    char *err;
-    xode xerr;
-    static char maxerr[] = "maximum node size reached";
-    static char deeperr[] = "maximum node depth reached";
-
-    if(xs == NULL)
-    {
-        fprintf(stderr,"Fatal Programming Error: xode_streameat() was improperly called with NULL.\n");
-        return XODE_STREAM_ERROR;
-    }
-
-    if(len == 0 || buff == NULL)
-        return xs->status;
-
-    if(len == -1) /* easy for hand-fed eat calls */
-        len = strlen(buff);
-
-    if(!XML_Parse(xs->parser, buff, len, 0))
-    {
-        err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
-        xs->status = XODE_STREAM_ERROR;
-    }else if(xode_pool_size(xode_get_pool(xs->node)) > XODE_STREAM_MAXNODE || xs->cdata_len > XODE_STREAM_MAXNODE){
-        err = maxerr;
-        xs->status = XODE_STREAM_ERROR;
-    }else if(xs->status == XODE_STREAM_ERROR){ /* set within expat handlers */
-        err = deeperr;
-    }else{
-        err = deeperr;
-    }
-
-    /* fire parsing error event, make a node containing the error string */
-    if(xs->status == XODE_STREAM_ERROR)
-    {
-        xerr = xode_new("error");
-        xode_insert_cdata(xerr,err,-1);
-        (xs->f)(XODE_STREAM_ERROR, xerr, xs->arg);
-    }
-
-    return xs->status;
+	char *err;
+	xode xerr;
+	static char maxerr[] = "maximum node size reached";
+	static char deeperr[] = "maximum node depth reached";
+
+	if(xs == NULL) {
+		fprintf(stderr, "Fatal Programming Error: xode_streameat() was "
+						"improperly called with NULL.\n");
+		return XODE_STREAM_ERROR;
+	}
+
+	if(len == 0 || buff == NULL)
+		return xs->status;
+
+	if(len == -1) /* easy for hand-fed eat calls */
+		len = strlen(buff);
+
+	if(!XML_Parse(xs->parser, buff, len, 0)) {
+		err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
+		xs->status = XODE_STREAM_ERROR;
+	} else if(xode_pool_size(xode_get_pool(xs->node)) > XODE_STREAM_MAXNODE
+			  || xs->cdata_len > XODE_STREAM_MAXNODE) {
+		err = maxerr;
+		xs->status = XODE_STREAM_ERROR;
+	} else if(xs->status == XODE_STREAM_ERROR) { /* set within expat handlers */
+		err = deeperr;
+	} else {
+		err = deeperr;
+	}
+
+	/* fire parsing error event, make a node containing the error string */
+	if(xs->status == XODE_STREAM_ERROR) {
+		xerr = xode_new("error");
+		xode_insert_cdata(xerr, err, -1);
+		(xs->f)(XODE_STREAM_ERROR, xerr, xs->arg);
+	}
+
+	return xs->status;
 }
 }

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików