Index: libfaim/util.c
===================================================================
--- libfaim/util.c	(revision 862371bb6a5ff5a6491550028e73824b698508bb)
+++ libfaim/util.c	(revision e374dee70435f8c596358571092675644a9ba731)
@@ -1,6 +1,7 @@
 /*
- *
- *
- *
+ * A little bit of this
+ * A little bit of that
+ * It started with a kiss
+ * Now we're up to bat
  */
 
@@ -9,5 +10,25 @@
 #include <ctype.h>
 
-faim_export faim_shortfunc int aimutil_putstr(u_char *dest, const char *src, int len)
+#ifdef _WIN32
+#include "win32dep.h"
+#endif
+
+faim_internal void faimdprintf(aim_session_t *sess, int dlevel, const char *format, ...)
+{
+	if (!sess) {
+		fprintf(stderr, "faimdprintf: no session! boo! (%d, %s)\n", dlevel, format);
+		return;
+	}
+	if ((dlevel <= sess->debug) && sess->debugcb) {
+		va_list ap;
+		va_start(ap, format);
+		sess->debugcb(sess, dlevel, format, ap);
+		va_end(ap);
+	}
+
+	return;
+}
+
+faim_export int aimutil_putstr(char *dest, const char *src, int len)
 {
 	memcpy(dest, src, len);
@@ -20,5 +41,5 @@
  *
  */
-faim_export int aimutil_tokslen(char *toSearch, int index, char dl)
+faim_export int aimutil_tokslen(char *toSearch, int theindex, char dl)
 {
 	int curCount = 1;
@@ -30,5 +51,5 @@
 	next = strchr(toSearch, dl);
 
-	while(curCount < index && next != NULL) {
+	while(curCount < theindex && next != NULL) {
 		curCount++;
 		last = next + 1;
@@ -36,5 +57,5 @@
 	}
 
-	if ((curCount < index) || (next == NULL))
+	if ((curCount < theindex) || (next == NULL))
 		toReturn = strlen(toSearch) - (curCount - 1);
 	else
@@ -61,5 +82,5 @@
 }
 
-faim_export char *aimutil_itemidx(char *toSearch, int index, char dl)
+faim_export char *aimutil_itemindex(char *toSearch, int theindex, char dl)
 {
 	int curCount;
@@ -73,5 +94,5 @@
 	next = strchr(toSearch, dl);
 
-	while (curCount < index && next != NULL) {
+	while (curCount < theindex && next != NULL) {
 		curCount++;
 		last = next + 1;
@@ -79,5 +100,5 @@
 	}
 
-	if (curCount < index) {
+	if (curCount < theindex) {
 		toReturn = malloc(sizeof(char));
 		*toReturn = '\0';
@@ -85,5 +106,5 @@
 	next = strchr(last, dl);
 
-	if (curCount < index) {
+	if (curCount < theindex) {
 		toReturn = malloc(sizeof(char));
 		*toReturn = '\0';
@@ -101,4 +122,40 @@
 }
 
+/**
+ * Calculate the checksum of a given icon.
+ *
+ */
+faim_export fu16_t aimutil_iconsum(const fu8_t *buf, int buflen)
+{
+	fu32_t sum;
+	int i;
+
+	for (i=0, sum=0; i+1<buflen; i+=2)
+		sum += (buf[i+1] << 8) + buf[i];
+	if (i < buflen)
+		sum += buf[i];
+	sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff);
+
+	return sum;
+}
+
+faim_export int aim_util_getlocalip(fu8_t *ip)
+{
+	struct hostent *hptr;
+	char localhost[129];
+
+	/* XXX if available, use getaddrinfo() */
+	/* XXX allow client to specify which IP to use for multihomed boxes */
+
+	if (gethostname(localhost, 128) < 0)
+		return -1;
+
+	if (!(hptr = gethostbyname(localhost)))
+		return -1;
+	memcpy(ip, hptr->h_addr_list[0], 4);
+
+	return 0;
+}
+
 /*
 * int snlen(const char *)
@@ -112,14 +169,12 @@
 {
 	int i = 0;
-	const char *curPtr = NULL;
 
 	if (!sn)
 		return 0;
 
-	curPtr = sn;
-	while ( (*curPtr) != (char) NULL) {
-		if ((*curPtr) != ' ')
-		i++;
-		curPtr++;
+	while (*sn != '\0') {
+		if (*sn != ' ')
+			i++;
+		sn++;
 	}
 
@@ -133,5 +188,6 @@
 * on screen names for AIM/AOL.  Mainly, this means case and space
 * insensitivity (all case differences and spacing differences are
-* ignored).
+* ignored, with the exception that screen names can not start with 
+* a space).
 *
 * Return: 0 if equal
@@ -141,65 +197,14 @@
 faim_export int aim_sncmp(const char *sn1, const char *sn2)
 {
-	const char *curPtr1 = NULL, *curPtr2 = NULL;
-
-	if (aim_snlen(sn1) != aim_snlen(sn2))
-		return 1;
-
-	curPtr1 = sn1;
-	curPtr2 = sn2;
-	while ( (*curPtr1 != (char) NULL) && (*curPtr2 != (char) NULL) ) {
-		if ( (*curPtr1 == ' ') || (*curPtr2 == ' ') ) {
-			if (*curPtr1 == ' ')
-				curPtr1++;
-			if (*curPtr2 == ' ')
-				curPtr2++;
-		} else {
-			if ( toupper(*curPtr1) != toupper(*curPtr2))
-				return 1;
-			curPtr1++;
-			curPtr2++;
-		}
-	}
-
-	/* Should both be NULL */
-	if (*curPtr1 != *curPtr2)
-		return 1;
+
+	do {
+		while (*sn2 == ' ')
+			sn2++;
+		while (*sn1 == ' ')
+			sn1++;
+		if (toupper(*sn1) != toupper(*sn2))
+			return 1;
+	} while ((*sn1 != '\0') && sn1++ && sn2++);
 
 	return 0;
 }
-
-/* strsep Copyright (C) 1992, 1993 Free Software Foundation, Inc.
-strsep is part of the GNU C Library.
-
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
-
-/* Minor changes by and1000 on 15/1/97 to make it go under Nemesis */
-
-faim_export char *aim_strsep(char **pp, const char *delim)
-{
-	char *p, *q;
-
-	if (!(p = *pp))
-		return 0;
-
-	if ((q = strpbrk (p, delim))) {
-		*pp = q + 1;
-		*q = '\0';
-	} else
-		*pp = 0;
-
-	return p;
-}
