changeset 2270:d82efea341ef

[gaim-migrate @ 2280] new libfaim. stupid bugs. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Wed, 12 Sep 2001 00:39:51 +0000
parents 7ff6170d84a0
children 3598a8521533
files src/conversation.c src/protocols/oscar/CHANGES src/protocols/oscar/aim.h src/protocols/oscar/aim_internal.h src/protocols/oscar/chat.c src/protocols/oscar/conn.c src/protocols/oscar/faimconfig.h src/protocols/oscar/im.c src/protocols/oscar/login.c src/protocols/oscar/md5.c src/protocols/oscar/rxqueue.c src/protocols/oscar/snac.c src/protocols/oscar/tlv.c src/protocols/oscar/txqueue.c
diffstat 14 files changed, 758 insertions(+), 245 deletions(-) [+]
line wrap: on
line diff
--- a/src/conversation.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/conversation.c	Wed Sep 12 00:39:51 2001 +0000
@@ -1322,7 +1322,10 @@
 	char buf2[BUF_LONG];
 	char mdate[64];
 
-	if (!g_list_find(conversations, c))
+	if (!c->is_chat && !g_list_find(conversations, c))
+		return;
+
+	if (c->is_chat && (!c->gc || !g_slist_find(c->gc->buddy_chats, c)))
 		return;
 
 	strftime(mdate, sizeof(mdate), "%H:%M:%S", localtime(&mtime));
--- a/src/protocols/oscar/CHANGES	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/CHANGES	Wed Sep 12 00:39:51 2001 +0000
@@ -1,6 +1,31 @@
 
 No release numbers
 ------------------
+ - Tue Sep 11 16:41:11 PDT 2001
+  - Fix chat invites.
+  - Keep faimtest from passing va_list's. That scares me.
+
+ - Tue Sep 11 04:42:28 PDT 2001
+  - Fix faim_internal/export confusion for the TLV functions
+  - Add aim_addtlvtochain_userinfo() (used by AP)
+  - Clean up ICBM Channel 1 parsing ever so slightly
+  - Add multipart message support in both directions, and some 
+      fun examples of it in faimtest.
+  - Yes, I'm insane.
+
+ - Mon Sep 10 06:15:43 PDT 2001
+  - Remove $Id line from md5.h. I really really hate those things.
+  - Remove all that locking crap. It never worked right anyway.
+  - Add conn->inside for things that should never ever be seen by clients
+  - Store supported groups list for each connection
+  - Support migrations.
+   - faimtest now theoretically supports doing this.  It happens so rarely
+       that it makes it difficult to test.  For more complicated clients, a
+       flag will have to be kept so that some things that happen during login
+       don't get triggered the second time around.  But faimtest doesn't have
+       such problems.
+  - Added some random commentary.
+
  - Sun Sep  9 03:01:28 PDT 2001
   - Three fixes in directim for gaim.  It works now. Yay.
 
--- a/src/protocols/oscar/aim.h	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/aim.h	Wed Sep 12 00:39:51 2001 +0000
@@ -16,10 +16,6 @@
 #include <faimconfig.h>
 #include <aim_cbtypes.h>
 
-#if !defined(FAIM_USEPTHREADS) && !defined(FAIM_USEFAKELOCKS) && !defined(FAIM_USENOPLOCKS)
-#error pthreads, fakelocks, or noplocks are currently required.
-#endif
-
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
@@ -46,40 +42,6 @@
 typedef fu32_t aim_snacid_t;
 typedef fu16_t flap_seqnum_t;
 
-#ifdef FAIM_USEPTHREADS
-#include <pthread.h>
-#define faim_mutex_t pthread_mutex_t 
-#define faim_mutex_init(x) pthread_mutex_init(x, NULL)
-#define faim_mutex_lock(x) pthread_mutex_lock(x)
-#define faim_mutex_unlock(x) pthread_mutex_unlock(x)
-#define faim_mutex_destroy(x) pthread_mutex_destroy(x)
-#elif defined(FAIM_USEFAKELOCKS)
-/*
- * For platforms without pthreads, we also assume
- * we're not linking against a threaded app.  Which
- * means we don't have to do real locking.  The 
- * macros below do nothing really.  They're a joke.
- * But they get it to compile.
- * 
- * XXX NOTE that locking hasn't really been tested in a long time,
- * and most code written after dec2000 --is not thread safe--.  You'll
- * want to audit locking use before you use less-than-library level
- * concurrency.
- *
- */
-#define faim_mutex_t fu8_t 
-#define faim_mutex_init(x) *x = 0
-#define faim_mutex_lock(x) while(*x != 0) {/* spin */}; *x = 1;
-#define faim_mutex_unlock(x) while(*x != 0) {/* spin spin spin */}; *x = 0;
-#define faim_mutex_destroy(x) while(*x != 0) {/* spiiiinnn */}; *x = 0;
-#elif defined(FAIM_USENOPLOCKS)
-#define faim_mutex_t fu8_t 
-#define faim_mutex_init(x)
-#define faim_mutex_lock(x)
-#define faim_mutex_unlock(x)
-#define faim_mutex_destroy(x)
-#endif
-
 /* Portability stuff (DMP) */
 
 #ifdef _WIN32
@@ -272,9 +234,8 @@
 	time_t lastactivity; /* time of last transmit */
 	int forcedlatency; 
 	void *handlerlist;
-	faim_mutex_t active; /* lock around read/writes */
-	faim_mutex_t seqnum_lock; /* lock around ->seqnum changes */
 	void *sessv; /* pointer to parent session */
+	void *inside; /* only accessible from inside libfaim */
 	struct aim_conn_s *next;
 } aim_conn_t;
 
@@ -352,7 +313,6 @@
 
 	/* Connection information */
 	aim_conn_t *connlist;
-	faim_mutex_t connlistlock;
 
 	/*
 	 * Transmit/receive queues.
@@ -389,7 +349,6 @@
 	 * XXX: Should these be per-connection? -mid
 	 */
 	void *snac_hash[FAIM_SNAC_HASH_SIZE];
-	faim_mutex_t snac_hash_locks[FAIM_SNAC_HASH_SIZE];
 	aim_snacid_t snacid_next;
 
 	struct {
@@ -486,9 +445,10 @@
 faim_internal int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v);
 faim_internal int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu16_t caps);
 faim_internal int aim_addtlvtochain_noval(aim_tlvlist_t **list, const fu16_t type);
+faim_internal int aim_addtlvtochain_userinfo(aim_tlvlist_t **list, fu16_t type, struct aim_userinfo_s *ui);
 faim_internal int aim_addtlvtochain_frozentlvlist(aim_tlvlist_t **list, fu16_t type, aim_tlvlist_t **tl);
 faim_internal int aim_counttlvchain(aim_tlvlist_t **list);
-faim_export int aim_sizetlvchain(aim_tlvlist_t **list);
+faim_internal int aim_sizetlvchain(aim_tlvlist_t **list);
 #endif /* FAIM_INTERNAL */
 
 /*
@@ -540,6 +500,7 @@
 faim_export int aim_conn_addhandler(aim_session_t *, aim_conn_t *conn, u_short family, u_short type, aim_rxcallback_t newhandler, u_short flags);
 faim_export int aim_clearhandlers(aim_conn_t *conn);
 
+faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group);
 faim_export aim_session_t *aim_conn_getsess(aim_conn_t *conn);
 faim_export void aim_conn_close(aim_conn_t *deadconn);
 faim_export aim_conn_t *aim_newconn(aim_session_t *, int type, const char *dest);
@@ -571,6 +532,7 @@
 
 #define AIM_WARN_ANON                     0x01
 
+faim_export int aim_sendpauseack(aim_session_t *sess, aim_conn_t *conn);
 faim_export int aim_send_warning(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu32_t flags);
 faim_export int aim_bos_nop(aim_session_t *, aim_conn_t *);
 faim_export int aim_flap_nop(aim_session_t *sess, aim_conn_t *conn);
@@ -669,15 +631,50 @@
 #define AIM_IMFLAGS_SUBENC_MACINTOSH	0x0040 /* damn that Steve Jobs! */
 #define AIM_IMFLAGS_CUSTOMFEATURES 	0x0080 /* features field present */
 #define AIM_IMFLAGS_EXTDATA		0x0100
+#define AIM_IMFLAGS_CUSTOMCHARSET	0x0200 /* charset fields set */
+#define AIM_IMFLAGS_MULTIPART		0x0400 /* ->mpmsg section valid */
 
+/*
+ * Multipart message structures.
+ */
+typedef struct aim_mpmsg_section_s {
+	fu16_t charset;
+	fu16_t charsubset;
+	fu8_t *data;
+	fu16_t datalen;
+	struct aim_mpmsg_section_s *next;
+} aim_mpmsg_section_t;
+
+typedef struct aim_mpmsg_s {
+	int numparts;
+	aim_mpmsg_section_t *parts;
+} aim_mpmsg_t;
+
+faim_export int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm);
+faim_export int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, const fu8_t *data, fu16_t datalen);
+faim_export int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii);
+faim_export int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const fu16_t *unicode, fu16_t unicodelen);
+faim_export void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm);
+
+/*
+ * Arguments to aim_send_im_ext().
+ *
+ * This is really complicated.  But immensely versatile.
+ *
+ */
 struct aim_sendimext_args {
 
 	/* These are _required_ */
 	const char *destsn;
-	fu32_t flags;
+	fu32_t flags; /* often 0 */
+
+	/* Only required if not using multipart messages */
 	const char *msg;
 	int msglen;
 
+	/* Required if ->msg is not provided */
+	aim_mpmsg_t *mpmsg;
+
 	/* Only used if AIM_IMFLAGS_HASICON is set */
 	fu32_t iconlen;
 	time_t iconstamp;
@@ -686,16 +683,34 @@
 	/* Only used if AIM_IMFLAGS_CUSTOMFEATURES is set */
 	fu8_t *features;
 	fu8_t featureslen;
+
+	/* Only used if AIM_IMFLAGS_CUSTOMCHARSET is set and mpmsg not used */
+	fu16_t charset;
+	fu16_t charsubset;
 };
 
+/*
+ * This information is provided in the Incoming ICBM callback for
+ * Channel 1 ICBM's.  
+ *
+ * Note that although CUSTOMFEATURES and CUSTOMCHARSET say they
+ * are optional, both are always set by the current libfaim code.
+ * That may or may not change in the future.  It is mainly for
+ * consistency with aim_sendimext_args.
+ *
+ * Multipart messages require some explanation. If you want to use them,
+ * I suggest you read all the comments in im.c.
+ *
+ */
 struct aim_incomingim_ch1_args {
 
 	/* Always provided */
+	aim_mpmsg_t mpmsg;
+	fu32_t icbmflags; /* some flags apply only to ->msg, not all mpmsg */
+	
+	/* Only provided if message has a human-readable section */
 	char *msg;
 	int msglen;
-	fu32_t icbmflags;
-	fu16_t flag1;
-	fu16_t flag2;
 
 	/* Only provided if AIM_IMFLAGS_HASICON is set */
 	time_t iconstamp;
@@ -709,6 +724,10 @@
 	/* Only provided if AIM_IMFLAGS_EXTDATA is set */
 	fu8_t extdatalen;
 	fu8_t *extdata;
+
+	/* Only used if AIM_IMFLAGS_CUSTOMCHARSET is set */
+	fu16_t charset;
+	fu16_t charsubset;
 };
 
 struct aim_incomingim_ch2_args {
--- a/src/protocols/oscar/aim_internal.h	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/aim_internal.h	Wed Sep 12 00:39:51 2001 +0000
@@ -127,6 +127,28 @@
 	fu16_t toolversion;
 };
 
+/* 
+ * In SNACland, the terms 'family' and 'group' are synonymous -- the former
+ * is my term, the latter is AOL's.
+ */
+struct snacgroup {
+	fu16_t group;
+	struct snacgroup *next;
+};
+
+/*
+ * This is inside every connection.  But it is a void * to anything
+ * outside of libfaim.  It should remain that way.  It's called data
+ * abstraction.  Maybe you've heard of it.  (Probably not if you're a 
+ * libfaim user.)
+ * 
+ */
+typedef struct aim_conn_inside_s {
+	struct snacgroup *groups;
+} aim_conn_inside_t;
+
+faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group);
+
 faim_internal fu16_t aim_getcap(aim_session_t *sess, aim_bstream_t *bs, int len);
 faim_internal int aim_putcap(aim_bstream_t *bs, fu16_t caps);
 
--- a/src/protocols/oscar/chat.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/chat.c	Wed Sep 12 00:39:51 2001 +0000
@@ -20,11 +20,11 @@
 	return (char *)conn->priv; /* yuck ! */
 }
 
+/* XXX get this into conn.c -- evil!! */
 faim_export aim_conn_t *aim_chat_getconn(aim_session_t *sess, const char *name)
 {
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist; cur; cur = cur->next) {
 		if (cur->type != AIM_CONN_TYPE_CHAT)
 			continue;
@@ -35,7 +35,6 @@
 		if (strcmp((char *)cur->priv, name) == 0)
 			break;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return cur;
 }
--- a/src/protocols/oscar/conn.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/conn.c	Wed Sep 12 00:39:51 2001 +0000
@@ -1,6 +1,6 @@
 
 /*
- *  aim_conn.c
+ * conn.c
  *
  * Does all this gloriously nifty connection handling stuff...
  *
@@ -15,6 +15,109 @@
 #include <netinet/in.h>
 #endif
 
+/*
+ * In OSCAR, every connection has a set of SNAC groups associated
+ * with it.  These are the groups that you can send over this connection
+ * without being guarenteed a "Not supported" SNAC error.  
+ *
+ * The grand theory of things says that these associations transcend 
+ * what libfaim calls "connection types" (conn->type).  You can probably
+ * see the elegance here, but since I want to revel in it for a bit, you 
+ * get to hear it all spelled out.
+ *
+ * So let us say that you have your core BOS connection running.  One
+ * of your modules has just given you a SNAC of the group 0x0004 to send
+ * you.  Maybe an IM destined for some twit in Greenland.  So you start
+ * at the top of your connection list, looking for a connection that 
+ * claims to support group 0x0004.  You find one.  Why, that neat BOS
+ * connection of yours can do that.  So you send it on its way.
+ *
+ * Now, say, that fellow from Greenland has friends and they all want to
+ * meet up with you in a lame chat room.  This has landed you a SNAC
+ * in the family 0x000e and you have to admit you're a bit lost.  You've
+ * searched your connection list for someone who wants to make your life
+ * easy and deliver this SNAC for you, but there isn't one there.
+ *
+ * Here comes the good bit.  Without even letting anyone know, particularly
+ * the module that decided to send this SNAC, and definitly not that twit
+ * in Greenland, you send out a service request.  In this request, you have
+ * marked the need for a connection supporting group 0x000e.  A few seconds
+ * later, you receive a service redirect with an IP address and a cookie in
+ * it.  Great, you say.  Now I have something to do.  Off you go, making
+ * that connection.  One of the first things you get from this new server
+ * is a message saying that indeed it does support the group you were looking
+ * for.  So you continue and send rate confirmation and all that.  
+ * 
+ * Then you remember you had that SNAC to send, and now you have a means to
+ * do it, and you do, and everyone is happy.  Except the Greenlander, who is
+ * still stuck in the bitter cold.
+ *
+ * Oh, and this is useful for building the Migration SNACs, too.  In the
+ * future, this may help convince me to implement rate limit mitigation
+ * for real.  We'll see.
+ *
+ * Just to make me look better, I'll say that I've known about this great
+ * scheme for quite some time now.  But I still haven't convinced myself
+ * to make libfaim work that way.  It would take a fair amount of effort,
+ * and probably some client API changes as well.  (Whenever I don't want
+ * to do something, I just say it would change the client API.  Then I 
+ * instantly have a couple of supporters of not doing it.)
+ *
+ * Generally, addgroup is only called by the internal handling of the
+ * server ready SNAC.  So if you want to do something before that, you'll
+ * have to be more creative.  That is done rather early, though, so I don't
+ * think you have to worry about it.  Unless you're me.  I care deeply
+ * about such inane things.
+ *
+ */
+faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group)
+{
+	aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside;
+	struct snacgroup *sg;
+
+	if (!(sg = malloc(sizeof(struct snacgroup))))
+		return;
+
+	faimdprintf(aim_conn_getsess(conn), 1, "adding group 0x%04x\n", group);
+	sg->group = group;
+
+	sg->next = ins->groups;
+	ins->groups = sg;
+
+	return;
+}
+
+faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group)
+{
+	aim_conn_t *cur;
+
+	for (cur = sess->connlist; cur; cur = cur->next) {
+		aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside;
+		struct snacgroup *sg;
+
+		for (sg = ins->groups; sg; sg = sg->next) {
+			if (sg->group == group)
+				return cur;
+		}
+	}
+
+	return NULL;
+}
+
+static struct snacgroup *connkill_snacgroups(struct snacgroup *sg)
+{
+
+	while (sg) {
+		struct snacgroup *tmp;
+
+		tmp = sg->next;
+		free(sg);
+		sg = tmp;
+	}
+
+	return NULL;
+}
+
 static void connkill_real(aim_session_t *sess, aim_conn_t **deadconn)
 {
 
@@ -38,6 +141,14 @@
 	if ((*deadconn)->type == AIM_CONN_TYPE_RENDEZVOUS)
 		aim_conn_kill_rend(sess, *deadconn);
 
+	if ((*deadconn)->inside) {
+		aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside;
+
+		inside->groups = connkill_snacgroups(inside->groups);
+
+		free(inside);
+	}
+
 	free(*deadconn);
 	deadconn = NULL;
 
@@ -54,8 +165,6 @@
 static void aim_connrst(aim_session_t *sess)
 {
 
-	faim_mutex_init(&sess->connlistlock);
-
 	if (sess->connlist) {
 		aim_conn_t *cur = sess->connlist, *tmp;
 
@@ -93,8 +202,7 @@
 	deadconn->forcedlatency = 0;
 	deadconn->handlerlist = NULL;
 	deadconn->priv = NULL;
-	faim_mutex_init(&deadconn->active);
-	faim_mutex_init(&deadconn->seqnum_lock);
+	memset(deadconn->inside, 0, sizeof(aim_conn_inside_t));
 
 	return;
 }
@@ -114,6 +222,12 @@
 		return NULL;
 	memset(newconn, 0, sizeof(aim_conn_t));
 
+	if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) {
+		free(newconn);
+		return NULL;
+	}
+	memset(newconn->inside, 0, sizeof(aim_conn_inside_t));
+
 	aim_conn_init(newconn);
 
 	newconn->next = sess->connlist;
@@ -168,8 +282,6 @@
 faim_export void aim_conn_close(aim_conn_t *deadconn)
 {
 
-	faim_mutex_destroy(&deadconn->active);
-	faim_mutex_destroy(&deadconn->seqnum_lock);
 	if (deadconn->fd >= 3)
 		close(deadconn->fd);
 	deadconn->fd = -1;
@@ -178,7 +290,6 @@
 	if (deadconn->type == AIM_CONN_TYPE_RENDEZVOUS)
 		aim_conn_close_rend((aim_session_t *)deadconn->sessv, deadconn);
 
-
 	return;
 }
 
@@ -191,18 +302,18 @@
  * specified session.  Returns the first connection of that
  * type found.
  *
+ * XXX except for RENDEZVOUS, all uses of this should be removed and
+ * use aim_conn_findbygroup() instead.
  */
 faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type)
 {
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist; cur; cur = cur->next) {
 		if ((cur->type == type) && 
 				!(cur->status & AIM_CONN_STATUS_INPROGRESS))
 			break;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return cur;
 }
@@ -211,12 +322,10 @@
 {
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist; cur; cur = cur->next) {
 		if (cur->type == type)
 			break;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return cur;
 }
@@ -226,12 +335,10 @@
 {
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist; cur; cur = cur->next) {
 		if (cur->fd == fd)
 			break;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return cur;
 }
@@ -425,8 +532,6 @@
 	if (!(conn = aim_conn_getnext(sess)))
 		return NULL;
 
-	faim_mutex_lock(&conn->active);
-
 	conn->fd = src->fd;
 	conn->type = src->type;
 	conn->subtype = src->subtype;
@@ -438,7 +543,15 @@
 	conn->sessv = src->sessv;
 	aim_clonehandlers(sess, conn, src);
 
-	faim_mutex_unlock(&conn->active);
+	if (src->inside) {
+		/*
+		 * XXX should clone this section as well, but since currently
+		 * this function only gets called for some of that rendezvous
+		 * crap, and not on SNAC connections, its probably okay for
+		 * now. 
+		 *
+		 */
+	}
 
 	return conn;
 }
@@ -467,15 +580,12 @@
 	if (!(connstruct = aim_conn_getnext(sess)))
 		return NULL;
 
-	faim_mutex_lock(&connstruct->active);
-
 	connstruct->sessv = (void *)sess;
 	connstruct->type = type;
 
 	if (!dest) { /* just allocate a struct */
 		connstruct->fd = -1;
 		connstruct->status = 0;
-		faim_mutex_unlock(&connstruct->active);
 		return connstruct;
 	}
 
@@ -503,13 +613,10 @@
 		connstruct->fd = -1;
 		connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
 		free(host);
-		faim_mutex_unlock(&connstruct->active);
 		return connstruct;
 	} else
 		connstruct->fd = ret;
 
-	faim_mutex_unlock(&connstruct->active);
-
 	free(host);
 
 	return connstruct;
@@ -528,12 +635,10 @@
 	int j;
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist, j = 0; cur; cur = cur->next) {
 		if (cur->fd > j)
 			j = cur->fd;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return j;
 }
@@ -551,14 +656,10 @@
 {
 	aim_conn_t *cur;
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist; cur; cur = cur->next) {
-		if (cur == conn) {
-			faim_mutex_unlock(&sess->connlistlock);
+		if (cur == conn)
 			return 1;
-		}
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	return 0;
 }
@@ -578,8 +679,6 @@
  *    1  outgoing data pending (%NULL returned)
  *    2  incoming data pending (connection with pending data returned)
  *
- * XXX: we could probably stand to do a little courser locking here.
- *
  */ 
 faim_export aim_conn_t *aim_select(aim_session_t *sess, struct timeval *timeout, int *status)
 {
@@ -587,23 +686,18 @@
 	fd_set fds, wfds;
 	int maxfd, i, haveconnecting = 0;
 
-	faim_mutex_lock(&sess->connlistlock);
 	if (!sess->connlist) {
-		faim_mutex_unlock(&sess->connlistlock);
 		*status = -1;
 		return NULL;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	FD_ZERO(&fds);
 	FD_ZERO(&wfds);
 
-	faim_mutex_lock(&sess->connlistlock);
 	for (cur = sess->connlist, maxfd = 0; cur; cur = cur->next) {
 		if (cur->fd == -1) {
 			/* don't let invalid/dead connections sit around */
 			*status = 2;
-			faim_mutex_unlock(&sess->connlistlock);
 			return cur;
 		} else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
 			FD_SET(cur->fd, &wfds);
@@ -614,7 +708,6 @@
 		if (cur->fd > maxfd)
 			maxfd = cur->fd;
 	}
-	faim_mutex_unlock(&sess->connlistlock);
 
 	/* 
 	 * If we have data waiting to be sent, return
@@ -636,14 +729,12 @@
 	} 
 
 	if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
-		faim_mutex_lock(&sess->connlistlock);
 		for (cur = sess->connlist; cur; cur = cur->next) {
 			if ((FD_ISSET(cur->fd, &fds)) || 
 					((cur->status & AIM_CONN_STATUS_INPROGRESS) && 
 					FD_ISSET(cur->fd, &wfds))) {
 				*status = 2;
-				faim_mutex_unlock(&sess->connlistlock);
-				return cur; /* XXX race condition here -- shouldnt unlock connlist */
+				return cur;
 			}
 		}
 		*status = 0; /* shouldn't happen */
@@ -652,8 +743,6 @@
 	else
 		*status = i; /* can be 0 or -1 */
 
-	faim_mutex_unlock(&sess->connlistlock);
-
 	return NULL;  /* no waiting or error, return */
 }
 
@@ -677,10 +766,8 @@
 	if (!conn)
 		return -1;
 
-	faim_mutex_lock(&conn->active);
 	conn->forcedlatency = newval;
 	conn->lastactivity = 0; /* reset this just to make sure */
-	faim_mutex_unlock(&conn->active);
 
 	return 0;
 }
--- a/src/protocols/oscar/faimconfig.h	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/faimconfig.h	Wed Sep 12 00:39:51 2001 +0000
@@ -40,31 +40,6 @@
 #define FAIM_LOGIN_PORT 5190
 
 /*
- * What type of synchronisation to use.
- * 
- * We don't actually use threads, but can use the POSIX mutex
- * in order to maintain thread safety.  You can use the fake locking
- * if you really don't like pthreads (which I don't) or if you don't
- * have it.
- *
- *   USEPTHREADS - Use POSIX mutecies
- *   USEFAKELOCKS - Use little stub spinners to help find locking bugs
- *   USENOPLOCKS - No-op out all synchro calls at compile time
- * 
- * Default: use noplocks by default.
- *
- * !!!NOTE: Even with USEPTHREADS turned on, libfaim is not fully thread
- *          safe.  It will still take some effort to add locking calls to
- *          the places that need them.  In fact, this feature in general
- *          is in danger of being officially deprecated and removed from 
- *          the code.
- *
- */
-#undef FAIM_USEPTHREADS
-#undef FAIM_USEFAKELOCKS
-#define FAIM_USENOPLOCKS
-
-/*
  * Size of the SNAC caching hash.
  *
  * Default: 16
--- a/src/protocols/oscar/im.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/im.c	Wed Sep 12 00:39:51 2001 +0000
@@ -138,7 +138,8 @@
  * supposed to be layed out. Most obviously, tlvlists should be used 
  * instead of writing out the bytes manually. 
  *
- * XXX support multipart
+ * XXX more precise verification that we never send SNACs larger than 8192
+ * XXX check SNAC size for multipart
  *
  */
 faim_export int aim_send_im_ext(aim_session_t *sess, aim_conn_t *conn, struct aim_sendimext_args *args)
@@ -153,19 +154,40 @@
 	if (!sess || !conn || !args)
 		return -EINVAL;
 
-	if (!args->msg || (args->msglen <= 0))
-		return -EINVAL;
+	if (args->flags & AIM_IMFLAGS_MULTIPART) {
+		if (args->mpmsg->numparts <= 0)
+			return -EINVAL;
+	} else {
+		if (!args->msg || (args->msglen <= 0))
+			return -EINVAL;
 
-	if (args->msglen >= MAXMSGLEN)
-		return -E2BIG;
+		if (args->msglen >= MAXMSGLEN)
+			return -E2BIG;
+	}
+
+	/* Painfully calculate the size of the message TLV */
+	msgtlvlen = 1 + 1; /* 0501 */
 
-	msgtlvlen = 12 + args->msglen;
 	if (args->flags & AIM_IMFLAGS_CUSTOMFEATURES)
-		msgtlvlen += args->featureslen;
+		msgtlvlen += 2 + args->featureslen;
 	else
-		msgtlvlen += sizeof(deffeatures);
-		
-	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, args->msglen+512)))
+		msgtlvlen += 2 + sizeof(deffeatures);
+
+	if (args->flags & AIM_IMFLAGS_MULTIPART) {
+		aim_mpmsg_section_t *sec;
+
+		for (sec = args->mpmsg->parts; sec; sec = sec->next) {
+			msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
+			msgtlvlen += 4 /* charset */ + sec->datalen;
+		}
+
+	} else {
+		msgtlvlen += 2 /* 0101 */ + 2 /* block len */;
+		msgtlvlen += 4 /* charset */ + args->msglen;
+	}
+
+
+	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, msgtlvlen+128)))
 		return -ENOMEM;
 
 	/* XXX should be optional */	
@@ -216,29 +238,50 @@
 		aimbs_putraw(&fr->data, deffeatures, sizeof(deffeatures));
 	}
 
-	aimbs_put16(&fr->data, 0x0101);
+	if (args->flags & AIM_IMFLAGS_MULTIPART) {
+		aim_mpmsg_section_t *sec;
 
-	/* 
-	 * Message block length.
-	 */
-	aimbs_put16(&fr->data, args->msglen + 0x04);
+		for (sec = args->mpmsg->parts; sec; sec = sec->next) {
+			aimbs_put16(&fr->data, 0x0101);
+			aimbs_put16(&fr->data, sec->datalen + 4);
+			aimbs_put16(&fr->data, sec->charset);
+			aimbs_put16(&fr->data, sec->charsubset);
+			aimbs_putraw(&fr->data, sec->data, sec->datalen);
+		}
+
+	} else {
+
+		aimbs_put16(&fr->data, 0x0101);
+
+		/* 
+		 * Message block length.
+		 */
+		aimbs_put16(&fr->data, args->msglen + 0x04);
 
-	/*
-	 * Character set.
-	 */
-	if (args->flags & AIM_IMFLAGS_UNICODE)
-		aimbs_put16(&fr->data, 0x0002);
-	else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
-		aimbs_put16(&fr->data, 0x0003);
-	else
-		aimbs_put16(&fr->data, 0x0000);
+		/*
+		 * Character set.
+		 */
+		if (args->flags & AIM_IMFLAGS_CUSTOMCHARSET) {
+
+			aimbs_put16(&fr->data, args->charset);
+			aimbs_put16(&fr->data, args->charsubset);
 
-	aimbs_put16(&fr->data, 0x0000);
+		} else {
+			if (args->flags & AIM_IMFLAGS_UNICODE)
+				aimbs_put16(&fr->data, 0x0002);
+			else if (args->flags & AIM_IMFLAGS_ISO_8859_1)
+				aimbs_put16(&fr->data, 0x0003);
+			else
+				aimbs_put16(&fr->data, 0x0000);
 
-	/*
-	 * Message.  Not terminated.
-	 */
-	aimbs_putraw(&fr->data, args->msg, args->msglen);
+			aimbs_put16(&fr->data, 0x0000);
+		}
+
+		/*
+		 * Message.  Not terminated.
+		 */
+		aimbs_putraw(&fr->data, args->msg, args->msglen);
+	}
 
 	/*
 	 * Set the Request Acknowledge flag.  
@@ -304,13 +347,13 @@
 	args.msglen = strlen(msg);
 
 	/* Make these don't get set by accident -- they need aim_send_im_ext */
-	args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON);
+	args.flags &= ~(AIM_IMFLAGS_CUSTOMFEATURES | AIM_IMFLAGS_HASICON | AIM_IMFLAGS_MULTIPART);
 
 	return aim_send_im_ext(sess, conn, &args);
 }
 
 /*
- * This is also performance sensative. (If you can believe it...)
+ * This is also performance sensitive. (If you can believe it...)
  *
  */
 faim_export int aim_send_icon(aim_session_t *sess, aim_conn_t *conn, const char *sn, const fu8_t *icon, int iconlen, time_t stamp, fu32_t iconsum)
@@ -457,6 +500,277 @@
 }
 
 /*
+ * Ahh, the joys of nearly ridiculous over-engineering.
+ *
+ * Not only do AIM ICBM's support multiple channels.  Not only do they
+ * support multiple character sets.  But they support multiple character 
+ * sets / encodings within the same ICBM.
+ *
+ * These multipart messages allow for complex space savings techniques, which
+ * seem utterly unnecessary by today's standards.  In fact, there is only
+ * one client still in popular use that still uses this method: AOL for the
+ * Macintosh, Version 5.0.  Obscure, yes, I know.  
+ *
+ * In modern (non-"legacy") clients, if the user tries to send a character
+ * that is not ISO-8859-1 or ASCII, the client will send the entire message
+ * as UNICODE, meaning that every character in the message will occupy the
+ * full 16 bit UNICODE field, even if the high order byte would be zero.
+ * Multipart messages prevent this wasted space by allowing the client to
+ * only send the characters in UNICODE that need to be sent that way, and
+ * the rest of the message can be sent in whatever the native character 
+ * set is (probably ASCII).
+ *
+ * An important note is that sections will be displayed in the order that
+ * they appear in the ICBM.  There is no facility for merging or rearranging
+ * sections at run time.  So if you have, say, ASCII then UNICODE then ASCII,
+ * you must supply two ASCII sections with a UNICODE in the middle, and incur
+ * the associated overhead.
+ *
+ * Normally I would have laughed and given a firm 'no' to supporting this
+ * seldom-used feature, but something is attracting me to it.  In the future,
+ * it may be possible to abuse this to send mixed-media messages to other
+ * open source clients (like encryption or something) -- see faimtest for
+ * examples of how to do this.
+ *
+ * I would definitly recommend avoiding this feature unless you really
+ * know what you are doing, and/or you have something neat to do with it.
+ *
+ */
+faim_export int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm)
+{
+
+	memset(mpm, 0, sizeof(aim_mpmsg_t));
+
+	return 0;
+}
+
+static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, fu8_t *data, fu16_t datalen)
+{
+	aim_mpmsg_section_t *sec; 
+	
+	if (!(sec = malloc(sizeof(aim_mpmsg_section_t))))
+		return -1;
+
+	sec->charset = charset;
+	sec->charsubset = charsubset;
+	sec->data = data;
+	sec->datalen = datalen;
+	sec->next = NULL;
+
+	if (!mpm->parts)
+		mpm->parts = sec;
+	else {
+		aim_mpmsg_section_t *cur;
+
+		for (cur = mpm->parts; cur->next; cur = cur->next)
+			;
+		cur->next = sec;
+	}
+
+	mpm->numparts++;
+
+	return 0;
+}
+
+faim_export int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, const fu8_t *data, fu16_t datalen)
+{
+	fu8_t *dup;
+
+	if (!(dup = malloc(datalen)))
+		return -1;
+	memcpy(dup, data, datalen);
+
+	if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) {
+		free(dup);
+		return -1;
+	}
+
+	return 0;
+}
+
+/* XXX should provide a way of saying ISO-8859-1 specifically */
+faim_export int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii)
+{
+	fu8_t *dup;
+
+	if (!(dup = strdup(ascii))) 
+		return -1;
+
+	if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, dup, strlen(ascii)) == -1) {
+		free(dup);
+		return -1;
+	}
+
+	return 0;
+}
+
+faim_export int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const fu16_t *unicode, fu16_t unicodelen)
+{
+	fu8_t *buf;
+	aim_bstream_t bs;
+	int i;
+
+	if (!(buf = malloc(unicodelen * 2)))
+		return -1;
+
+	aim_bstream_init(&bs, buf, unicodelen * 2);
+
+	/* We assume unicode is in /host/ byte order -- convert to network */
+	for (i = 0; i < unicodelen; i++)
+		aimbs_put16(&bs, unicode[i]);
+
+	if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) {
+		free(buf);
+		return -1;
+	}
+	
+	return 0;
+}
+
+faim_export void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm)
+{
+	aim_mpmsg_section_t *cur;
+
+	for (cur = mpm->parts; cur; ) {
+		aim_mpmsg_section_t *tmp;
+		
+		tmp = cur->next;
+		free(cur->data);
+		free(cur);
+		cur = tmp;
+	}
+	
+	mpm->numparts = 0;
+	mpm->parts = NULL;
+
+	return;
+}
+
+/*
+ * Start by building the multipart structures, then pick the first 
+ * human-readable section and stuff it into args->msg so no one gets
+ * suspicious.
+ *
+ */
+static int incomingim_ch1_parsemsgs(aim_session_t *sess, fu8_t *data, int len, struct aim_incomingim_ch1_args *args)
+{
+	static const fu16_t charsetpri[] = {
+		0x0000, /* ASCII first */
+		0x0003, /* then ISO-8859-1 */
+		0x0002, /* UNICODE as last resort */
+	};
+	static const int charsetpricount = 3;
+	int i;
+	aim_bstream_t mbs;
+	aim_mpmsg_section_t *sec;
+
+	aim_bstream_init(&mbs, data, len);
+
+	while (aim_bstream_empty(&mbs)) {
+		fu16_t msglen, flag1, flag2;
+		fu8_t *msgbuf;
+
+		aimbs_get8(&mbs); /* 01 */
+		aimbs_get8(&mbs); /* 01 */
+
+		/* Message string length, including character set info. */
+		msglen = aimbs_get16(&mbs);
+
+		/* Character set info */
+		flag1 = aimbs_get16(&mbs);
+		flag2 = aimbs_get16(&mbs);
+
+		/* Message. */
+		msglen -= 4;
+
+		/*
+		 * For now, we don't care what the encoding is.  Just copy
+		 * it into a multipart struct and deal with it later. However,
+		 * always pad the ending with a NULL.  This makes it easier
+		 * to treat ASCII sections as strings.  It won't matter for
+		 * UNICODE or binary data, as you should never read past
+		 * the specified data length, which will not include the pad.
+		 *
+		 * XXX There's an API bug here.  For sending, the UNICODE is
+		 * given in host byte order (aim_mpmsg_addunicode), but here
+		 * the received messages are given in network byte order.
+		 *
+		 */
+		msgbuf = aimbs_getstr(&mbs, msglen);
+		mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, msgbuf, msglen);
+
+	} /* while */
+
+	args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */
+
+	/*
+	 * Clients that support multiparts should never use args->msg, as it
+	 * will point to an arbitrary section.
+	 *
+	 * Here, we attempt to provide clients that do not support multipart
+	 * messages with something to look at -- hopefully a human-readable
+	 * string.  But, failing that, a UNICODE message, or nothing at all.
+	 *
+	 * Which means that even if args->msg is NULL, it does not mean the
+	 * message was blank.
+	 *
+	 */
+	for (i = 0; i < charsetpricount; i++) {
+		for (sec = args->mpmsg.parts; sec; sec = sec->next) {
+
+			if (sec->charset != charsetpri[i])
+				continue;
+
+			/* Great. We found one.  Fill it in. */
+			args->charset = sec->charset;
+			args->charsubset = sec->charsubset;
+			args->icbmflags |= AIM_IMFLAGS_CUSTOMCHARSET;
+
+			/* Set up the simple flags */
+			if (args->charset == 0x0000)
+				; /* ASCII */
+			else if (args->charset == 0x0002)
+				args->icbmflags |= AIM_IMFLAGS_UNICODE;
+			else if (args->charset == 0x0003)
+				args->icbmflags |= AIM_IMFLAGS_ISO_8859_1;
+			else if (args->charset == 0xffff)
+				; /* no encoding (yeep!) */
+
+			if (args->charsubset == 0x0000)
+				; /* standard subencoding? */
+			else if (args->charsubset == 0x000b)
+				args->icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
+			else if (args->charsubset == 0xffff)
+				; /* no subencoding */
+#if 0
+			/* XXX this isn't really necesary... */	
+			if (	((args.flag1 != 0x0000) &&
+				 (args.flag1 != 0x0002) &&
+				 (args.flag1 != 0x0003) &&
+				 (args.flag1 != 0xffff)) ||
+				((args.flag2 != 0x0000) &&
+				 (args.flag2 != 0x000b) &&
+				 (args.flag2 != 0xffff))) {
+				faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
+			}
+#endif
+
+			args->msg = sec->data;
+			args->msglen = sec->datalen;
+
+			return 0;
+		}
+	}
+
+	/* No human-readable sections found.  Oh well. */
+	args->charset = args->charsubset = 0xffff;
+	args->msg = NULL;
+	args->msglen = 0;
+
+	return 0;
+}
+
+/*
  *
  * This should use tlvlists, but doesn't for performance reasons.
  *
@@ -490,6 +804,8 @@
 
 	memset(&args, 0, sizeof(args));
 
+	aim_mpmsg_init(sess, &args.mpmsg);
+
 	/*
 	 * This used to be done using tlvchains.  For performance reasons,
 	 * I've changed it to process the TLVs in-place.  This avoids lots
@@ -522,58 +838,11 @@
 			aim_bstream_advance(bs, args.featureslen);
 			args.icbmflags |= AIM_IMFLAGS_CUSTOMFEATURES;
 
-			aimbs_get8(bs); /* 01 */
-			aimbs_get8(bs); /* 01 */
-
-			/* Message string length, including flag words. */
-			args.msglen = aimbs_get16(bs);
-
-			/* Flag words. */
-			args.flag1 = aimbs_get16(bs);
-			if (args.flag1 == 0x0000)
-				; /* ASCII */
-			else if (args.flag1 == 0x0002)
-				args.icbmflags |= AIM_IMFLAGS_UNICODE;
-			else if (args.flag1 == 0x0003)
-				args.icbmflags |= AIM_IMFLAGS_ISO_8859_1;
-			else if (args.flag1 == 0xffff)
-				; /* no encoding (yeep!) */
-
-			args.flag2 = aimbs_get16(bs);
-			if (args.flag2 == 0x0000)
-				; /* standard subencoding? */
-			else if (args.flag2 == 0x000b)
-				args.icbmflags |= AIM_IMFLAGS_SUBENC_MACINTOSH;
-			else if (args.flag2 == 0xffff)
-				; /* no subencoding */
-
-			/* XXX this isn't really necesary... */	
-			if (	((args.flag1 != 0x0000) &&
-				 (args.flag1 != 0x0002) &&
-				 (args.flag1 != 0x0003) &&
-				 (args.flag1 != 0xffff)) ||
-				((args.flag2 != 0x0000) &&
-				 (args.flag2 != 0x000b) &&
-				 (args.flag2 != 0xffff))) {
-				faimdprintf(sess, 0, "icbm: **warning: encoding flags are being used! {%04x, %04x}\n", args.flag1, args.flag2);
-			}
-
-			/* Message. */
-			args.msglen -= 4;
-			if (args.icbmflags & AIM_IMFLAGS_UNICODE) {
-				fu8_t *umsg;
-
-				/* Can't use getstr because of wide null */
-				umsg = aimbs_getraw(bs, args.msglen);
-				args.msg = malloc(args.msglen+2);
-				memcpy(args.msg, umsg, args.msglen);
-				args.msg[args.msglen] = '\0'; /* wide NULL */
-				args.msg[args.msglen+1] = '\0';
-
-				free(umsg);
-
-			} else
-				args.msg = aimbs_getstr(bs, args.msglen);
+			/*
+			 * The rest of the TLV contains one or more message
+			 * blocks...
+			 */
+			incomingim_ch1_parsemsgs(sess, bs->data + bs->offset /* XXX evil!!! */, length - 2 - 2 - args.featureslen, &args);
 
 		} else if (type == 0x0003) { /* Server Ack Requested */
 
@@ -618,8 +887,8 @@
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
 		ret = userfunc(sess, rx, channel, userinfo, &args);
 
+	aim_mpmsg_free(sess, &args.mpmsg);
 	free(args.extdata);
-	free(args.msg);
 
 	return ret;
 }
@@ -730,7 +999,7 @@
 	/* XXX: implement all this */
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) 
-		ret = userfunc(sess, rx, 0x0002, userinfo, &args);
+		ret = userfunc(sess, rx, 0x0002, userinfo, args);
 
 	return ret;
 }
@@ -757,7 +1026,7 @@
 		args->info.chat.lang = aim_gettlv_str(list2, 0x000e, 1);
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
-		ret = userfunc(sess, rx, 0x0002, userinfo, &args);
+		ret = userfunc(sess, rx, 0x0002, userinfo, args);
 
 	/* XXX free_roominfo */
 	free(args->info.chat.roominfo.name);
@@ -808,7 +1077,7 @@
 	memcpy(args->info.getfile.cookie, args->cookie, 8);
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
-		ret = userfunc(sess, rx, 0x0002, userinfo, &args);
+		ret = userfunc(sess, rx, 0x0002, userinfo, args);
 
 	return ret;
 }
--- a/src/protocols/oscar/login.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/login.c	Wed Sep 12 00:39:51 2001 +0000
@@ -382,6 +382,10 @@
 
 /*
  * Send Server Ready.  (Non-client)
+ * 
+ * XXX If anyone cares, this should be made to use the conn-stored group
+ * system.
+ *
  */
 faim_export int aim_sendserverready(aim_session_t *sess, aim_conn_t *conn)
 {
@@ -412,7 +416,6 @@
 	return 0;
 }
 
-
 /* 
  * Send service redirect.  (Non-Client)
  */
@@ -440,7 +443,13 @@
 	return 0;
 }
 
-
+/*
+ * See comments in conn.c about how the group associations are supposed
+ * to work, and how they really work.
+ *
+ * This info probably doesn't even need to make it to the client.
+ *
+ */
 static int hostonline(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
 {
 	aim_rxcallback_t userfunc;
@@ -451,8 +460,10 @@
 	if (!(families = malloc(aim_bstream_empty(bs))))
 		return 0;
 
-	for (famcount = 0; aim_bstream_empty(bs); famcount++)
+	for (famcount = 0; aim_bstream_empty(bs); famcount++) {
 		families[famcount] = aimbs_get16(bs);
+		aim_conn_addgroup(rx->conn, families[famcount]);
+	}
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
 		ret = userfunc(sess, rx, famcount, families);
@@ -622,6 +633,114 @@
 	return 0;
 }
 
+/*
+ * How Migrations work.  
+ *
+ * The server sends a Server Pause message, which the client should respond to 
+ * with a Server Pause Ack, which contains the families it needs on this 
+ * connection. The server will send a Migration Notice with an IP address, and 
+ * then disconnect. Next the client should open the connection and send the 
+ * cookie.  Repeat the normal login process and pretend this never happened.
+ *
+ * The Server Pause contains no data.
+ *
+ */
+static int serverpause(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	aim_rxcallback_t userfunc;
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		return userfunc(sess, rx);
+
+	return 0;
+}
+
+/*
+ * It is rather important that aim_sendpauseack() gets called for the exact
+ * same connection that the Server Pause callback was called for, since
+ * libfaim extracts the data for the SNAC from the connection structure.
+ *
+ * Of course, if you don't do that, more bad things happen than just what
+ * libfaim can cause.
+ *
+ */
+faim_export int aim_sendpauseack(aim_session_t *sess, aim_conn_t *conn)
+{
+	aim_frame_t *fr;
+	aim_snacid_t snacid;
+	aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside;
+	struct snacgroup *sg;
+
+	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1024)))
+		return -ENOMEM;
+
+	snacid = aim_cachesnac(sess, 0x0001, 0x000c, 0x0000, NULL, 0);
+	aim_putsnac(&fr->data, 0x0001, 0x000c, 0x0000, snacid);
+
+	/*
+	 * This list should have all the groups that the original 
+	 * Host Online / Server Ready said this host supports.  And 
+	 * we want them all back after the migration.
+	 */
+	for (sg = ins->groups; sg; sg = sg->next)
+		aimbs_put16(&fr->data, sg->group);
+
+	aim_tx_enqueue(sess, fr);
+
+	return 0;
+}
+
+/*
+ * This is the final SNAC sent on the original connection during a migration.
+ * It contains the IP and cookie used to connect to the new server, and 
+ * optionally a list of the SNAC groups being migrated.
+ *
+ */
+static int migrate(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	aim_rxcallback_t userfunc;
+	int ret = 0;
+	fu16_t groupcount, i;
+	aim_tlvlist_t *tl;
+	char *ip = NULL;
+	aim_tlv_t *cktlv;
+
+	/*
+	 * Apparently there's some fun stuff that can happen right here. The
+	 * migration can actually be quite selective about what groups it
+	 * moves to the new server.  When not all the groups for a connection
+	 * are migrated, or they are all migrated but some groups are moved
+	 * to a different server than others, it is called a bifurcated 
+	 * migration.
+	 *
+	 * Let's play dumb and not support that.
+	 *
+	 */
+	groupcount = aimbs_get16(bs);
+	for (i = 0; i < groupcount; i++) {
+		fu16_t group;
+
+		group = aimbs_get16(bs);
+
+		faimdprintf(sess, 0, "bifurcated migration unsupported -- group 0x%04x\n", group);
+	}
+
+	tl = aim_readtlvchain(bs);
+
+	if (aim_gettlv(tl, 0x0005, 1))
+		ip = aim_gettlv_str(tl, 0x0005, 1);
+
+	cktlv = aim_gettlv(tl, 0x0006, 1);
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		ret = userfunc(sess, rx, ip, cktlv ? cktlv->value : NULL);
+
+	aim_freetlvchain(&tl);
+	free(ip);
+
+	return ret;
+}
+
 static int motd(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
 {
 	aim_rxcallback_t userfunc;
@@ -857,10 +976,14 @@
 		return rateresp(sess, mod, rx, snac, bs);
 	else if (snac->subtype == 0x000a)
 		return ratechange(sess, mod, rx, snac, bs);
+	else if (snac->subtype == 0x000b)
+		return serverpause(sess, mod, rx, snac, bs);
 	else if (snac->subtype == 0x000f)
 		return selfinfo(sess, mod, rx, snac, bs);
 	else if (snac->subtype == 0x0010)
 		return evilnotify(sess, mod, rx, snac, bs);
+	else if (snac->subtype == 0x0012)
+		return migrate(sess, mod, rx, snac, bs);
 	else if (snac->subtype == 0x0013)
 		return motd(sess, mod, rx, snac, bs);
 	else if (snac->subtype == 0x0018)
--- a/src/protocols/oscar/md5.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/md5.c	Wed Sep 12 00:39:51 2001 +0000
@@ -21,7 +21,6 @@
   ghost@aladdin.com
 
  */
-/*$Id: md5.c 2132 2001-08-04 01:37:49Z mid $ */
 /*
   Independent implementation of MD5 (RFC 1321).
 
--- a/src/protocols/oscar/rxqueue.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/rxqueue.c	Wed Sep 12 00:39:51 2001 +0000
@@ -318,10 +318,8 @@
 	 *   2 short -- Sequence number 
 	 *   4 short -- Number of data bytes that follow.
 	 */
-	faim_mutex_lock(&conn->active);
 	if (aim_bstream_recv(&flaphdr, conn->fd, 6) < 6) {
 		aim_conn_close(conn);
-		faim_mutex_unlock(&conn->active);
 		return -1;
 	}
 
@@ -334,15 +332,12 @@
 	if (aimbs_get8(&flaphdr) != 0x2a) {
 		faimdprintf(sess, 0, "FLAP framing disrupted");
 		aim_conn_close(conn);
-		faim_mutex_unlock(&conn->active);
 		return -1;
 	}	
 
 	/* allocate a new struct */
-	if (!(newrx = (aim_frame_t *)malloc(sizeof(aim_frame_t)))) {
-		faim_mutex_unlock(&conn->active);
+	if (!(newrx = (aim_frame_t *)malloc(sizeof(aim_frame_t))))
 		return -1;
-	}
 	memset(newrx, 0, sizeof(aim_frame_t));
 
 	/* we're doing FLAP if we're here */
@@ -359,7 +354,6 @@
 
 		if (!(payload = (fu8_t *) malloc(payloadlen))) {
 			aim_frame_destroy(newrx);
-			faim_mutex_unlock(&conn->active);
 			return -1;
 		}
 
@@ -370,13 +364,11 @@
 			free(payload);
 			aim_frame_destroy(newrx);
 			aim_conn_close(conn);
-			faim_mutex_unlock(&conn->active);
 			return -1;
 		}
 	} else
 		aim_bstream_init(&newrx->data, NULL, 0);
 
-	faim_mutex_unlock(&conn->active);
 
 	aim_bstream_rewind(&newrx->data);
 
--- a/src/protocols/oscar/snac.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/snac.c	Wed Sep 12 00:39:51 2001 +0000
@@ -22,10 +22,8 @@
 {
 	int i;
 
-	for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
+	for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++)
 		sess->snac_hash[i] = NULL;
-		faim_mutex_init(&sess->snac_hash_locks[i]);
-	}
 
 	return;
 }
@@ -68,10 +66,8 @@
 
 	index = snac->id % FAIM_SNAC_HASH_SIZE;
 
-	faim_mutex_lock(&sess->snac_hash_locks[index]);
 	snac->next = (aim_snac_t *)sess->snac_hash[index];
 	sess->snac_hash[index] = (void *)snac;
-	faim_mutex_unlock(&sess->snac_hash_locks[index]);
 
 	return snac->id;
 }
@@ -90,7 +86,6 @@
 
 	index = id % FAIM_SNAC_HASH_SIZE;
 
-	faim_mutex_lock(&sess->snac_hash_locks[index]);
 	for (prev = (aim_snac_t **)&sess->snac_hash[index]; (cur = *prev); ) {
 		if (cur->id == id) {
 			*prev = cur->next;
@@ -98,7 +93,6 @@
 		} else
 			prev = &cur->next;
 	}
-	faim_mutex_unlock(&sess->snac_hash_locks[index]);
 
 	return cur;
 }
@@ -118,11 +112,8 @@
 		aim_snac_t *cur, **prev;
 		time_t curtime;
 
-		faim_mutex_lock(&sess->snac_hash_locks[i]);
-		if (!sess->snac_hash[i]) {
-			faim_mutex_unlock(&sess->snac_hash_locks[i]);
+		if (!sess->snac_hash[i])
 			continue;
-		}
 
 		curtime = time(NULL); /* done here in case we waited for the lock */
 
@@ -138,7 +129,6 @@
 			} else
 				prev = &cur->next;
 		}
-		faim_mutex_unlock(&sess->snac_hash_locks[i]);
 	}
 
 	return;
--- a/src/protocols/oscar/tlv.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/tlv.c	Wed Sep 12 00:39:51 2001 +0000
@@ -43,7 +43,7 @@
  * in libfaim.
  *
  */
-faim_export aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs)
+faim_internal aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs)
 {
 	aim_tlvlist_t *list = NULL, *cur;
 	fu16_t type, length;
@@ -95,7 +95,7 @@
  * should be removed before calling this.
  *
  */
-faim_export void aim_freetlvchain(aim_tlvlist_t **list)
+faim_internal void aim_freetlvchain(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
 
@@ -124,7 +124,7 @@
  * Returns the number of TLVs stored in the passed chain.
  *
  */
-faim_export int aim_counttlvchain(aim_tlvlist_t **list)
+faim_internal int aim_counttlvchain(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
 	int count;
@@ -146,7 +146,7 @@
  * write the passed TLV chain to a data buffer.
  *
  */
-faim_export int aim_sizetlvchain(aim_tlvlist_t **list)
+faim_internal int aim_sizetlvchain(aim_tlvlist_t **list)
 {
 	aim_tlvlist_t *cur;
 	int size;
@@ -171,7 +171,7 @@
  * to the TLV chain.
  *
  */
-faim_export int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v)
+faim_internal int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v)
 {
 	aim_tlvlist_t *newtlv, *cur;
 
@@ -212,7 +212,7 @@
  * Adds a two-byte unsigned integer to a TLV chain.
  *
  */
-faim_export int aim_addtlvtochain16(aim_tlvlist_t **list, const fu16_t t, const fu16_t v)
+faim_internal int aim_addtlvtochain16(aim_tlvlist_t **list, const fu16_t t, const fu16_t v)
 {
 	fu8_t v16[2];
 
@@ -230,7 +230,7 @@
  * Adds a four-byte unsigned integer to a TLV chain.
  *
  */
-faim_export int aim_addtlvtochain32(aim_tlvlist_t **list, const fu16_t t, const fu32_t v)
+faim_internal int aim_addtlvtochain32(aim_tlvlist_t **list, const fu16_t t, const fu32_t v)
 {
 	fu8_t v32[4];
 
@@ -261,7 +261,7 @@
  *      %AIM_CAPS_SENDFILE    Supports Send File functions
  *
  */
-faim_export int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu16_t caps)
+faim_internal int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu16_t caps)
 {
 	fu8_t buf[16*16]; /* icky fixed length buffer */
 	aim_bstream_t bs;
@@ -273,6 +273,18 @@
 	return aim_addtlvtochain_raw(list, t, aim_bstream_curpos(&bs), buf);
 }
 
+faim_internal int aim_addtlvtochain_userinfo(aim_tlvlist_t **list, fu16_t type, struct aim_userinfo_s *ui)
+{
+	fu8_t buf[1024]; /* bleh */
+	aim_bstream_t bs;
+
+	aim_bstream_init(&bs, buf, sizeof(buf));
+
+	aim_putuserinfo(&bs, ui);
+
+	return aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
+}
+
 /**
  * aim_addtlvtochain_noval - Add a blank TLV to a TLV chain
  * @list: Destination chain
@@ -334,7 +346,7 @@
  *
  * XXX clean this up, make better use of bstreams 
  */
-faim_export int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list)
+faim_internal int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list)
 {
 	int goodbuflen;
 	aim_tlvlist_t *cur;
@@ -372,7 +384,7 @@
  * in a chain.
  *
  */
-faim_export aim_tlv_t *aim_gettlv(aim_tlvlist_t *list, const fu16_t t, const int n)
+faim_internal aim_tlv_t *aim_gettlv(aim_tlvlist_t *list, const fu16_t t, const int n)
 {
 	aim_tlvlist_t *cur;
 	int i;
@@ -400,7 +412,7 @@
  * dynamic buffer and must be freed by the caller.
  *
  */
-faim_export char *aim_gettlv_str(aim_tlvlist_t *list, const fu16_t t, const int n)
+faim_internal char *aim_gettlv_str(aim_tlvlist_t *list, const fu16_t t, const int n)
 {
 	aim_tlv_t *tlv;
 	char *newstr;
--- a/src/protocols/oscar/txqueue.c	Tue Sep 11 21:05:16 2001 +0000
+++ b/src/protocols/oscar/txqueue.c	Wed Sep 12 00:39:51 2001 +0000
@@ -202,9 +202,7 @@
 {
 	flap_seqnum_t ret;
 	
-	faim_mutex_lock(&conn->seqnum_lock);
 	ret = ++conn->seqnum;
-	faim_mutex_unlock(&conn->seqnum_lock);
 
 	return ret;
 }