changeset 4230:9f729d6d88a6

[gaim-migrate @ 4475] This is 128KB of raw kickassyness. AKA ICQ SSI. I've rewritten all the important parts of ssi.c. Things should be better. One thing I like a lot is that gaim will store the alias you assign to buddies in your server list for both AIM and ICQ. WinICQ supports this, but WinAIM doesn't. However, it doesn't seem to interfere with WinAIM, and Gaim can still use it. I dunno, I just think it's neat. Anyway, go nuts. Let me know if something doesn't work, because that's bad. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Tue, 07 Jan 2003 21:19:05 +0000
parents c1857c9c912d
children 7b224c9b00dc
files src/protocols/oscar/aim.h src/protocols/oscar/aim_cbtypes.h src/protocols/oscar/auth.c src/protocols/oscar/conn.c src/protocols/oscar/oscar.c src/protocols/oscar/ssi.c src/protocols/oscar/tlv.c
diffstat 7 files changed, 1873 insertions(+), 1228 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/oscar/aim.h	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/aim.h	Tue Jan 07 21:19:05 2003 +0000
@@ -370,11 +370,12 @@
 	/* Server-stored information (ssi) */
 	struct {
 		int received_data;
-		fu16_t revision;
-		struct aim_ssi_item *items;
+		fu16_t numitems;
+		struct aim_ssi_item *official;
+		struct aim_ssi_item *local;
+		struct aim_ssi_tmp *pending;
 		time_t timestamp;
 		int waiting_for_ack;
-		aim_frame_t *holding_queue;
 	} ssi;
 
 	struct aim_authresp_info *authinfo;
@@ -534,6 +535,8 @@
 /* TLV list handling. */
 faim_internal aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs);
 faim_internal aim_tlvlist_t *aim_readtlvchain_num(aim_bstream_t *bs, fu16_t num);
+faim_internal aim_tlvlist_t *aim_readtlvchain_len(aim_bstream_t *bs, fu16_t len);
+faim_internal aim_tlvlist_t *aim_tlvlist_copy(aim_tlvlist_t *orig);
 faim_internal void aim_freetlvchain(aim_tlvlist_t **list);
 faim_internal aim_tlv_t *aim_gettlv(aim_tlvlist_t *, fu16_t t, const int n);
 faim_internal char *aim_gettlv_str(aim_tlvlist_t *, const fu16_t t, const int n);
@@ -1122,6 +1125,8 @@
 faim_export int aim_chat_leaveroom(aim_session_t *sess, const char *name);
 
 
+
+/* ssi.c */
 #define AIM_SSI_TYPE_BUDDY         0x0000
 #define AIM_SSI_TYPE_GROUP         0x0001
 #define AIM_SSI_TYPE_PERMIT        0x0002
@@ -1129,44 +1134,67 @@
 #define AIM_SSI_TYPE_PDINFO        0x0004
 #define AIM_SSI_TYPE_PRESENCEPREFS 0x0005
 
+#define AIM_SSI_ACK_SUCCESS      0x0000
+#define AIM_SSI_ACK_ITEMNOTFOUND 0x0002
+#define AIM_SSI_ACK_IDNUMINUSE   0x000a
+#define AIM_SSI_ACK_ATMAX        0x000c
+#define AIM_SSI_ACK_INVALIDNAME  0x000d
+#define AIM_SSI_ACK_AUTHREQUIRED 0x000e
+
 struct aim_ssi_item {
 	char *name;
 	fu16_t gid;
 	fu16_t bid;
 	fu16_t type;
-	void *data;
+	struct aim_tlvlist_s *data;
 	struct aim_ssi_item *next;
 };
 
+struct aim_ssi_tmp {
+	fu16_t action;
+	fu16_t ack;
+	char *name;
+	struct aim_ssi_item *item;
+	struct aim_ssi_tmp *next;
+};
+
 /* These build the actual SNACs and queue them to be sent */
-faim_export int aim_ssi_reqrights(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_reqdata(aim_session_t *sess, aim_conn_t *conn, time_t localstamp, fu16_t localrev);
-faim_export int aim_ssi_enable(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn, struct aim_ssi_item **items, unsigned int num, fu16_t subtype);
-faim_export int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0002 */ faim_export int aim_ssi_reqrights(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0005 */ faim_export int aim_ssi_reqdata(aim_session_t *sess, aim_conn_t *conn, time_t localstamp, fu16_t localrev);
+/* 0x0007 */ faim_export int aim_ssi_enable(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0008 */ faim_export int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0011 */ faim_export int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0012 */ faim_export int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn);
+/* 0x0014 */ faim_export int aim_ssi_sendauth(aim_session_t *sess, aim_conn_t *conn, char *sn, char *msg);
+/* 0x0018 */ faim_export int aim_ssi_sendauthrequest(aim_session_t *sess, aim_conn_t *conn, char *sn, char *msg);
+/* 0x001a */ faim_export int aim_ssi_sendauthreply(aim_session_t *sess, aim_conn_t *conn, char *sn, fu8_t reply, char *msg);
 
-/* These handle the local variables */
+/* Client functions for retrieving SSI data */
 faim_export struct aim_ssi_item *aim_ssi_itemlist_find(struct aim_ssi_item *list, fu16_t gid, fu16_t bid);
 faim_export struct aim_ssi_item *aim_ssi_itemlist_finditem(struct aim_ssi_item *list, const char *gn, const char *sn, fu16_t type);
-faim_export struct aim_ssi_item *aim_ssi_itemlist_findparent(struct aim_ssi_item *list, char *sn);
+faim_export struct aim_ssi_item *aim_ssi_itemlist_exists(struct aim_ssi_item *list, const char *sn);
+faim_export char *aim_ssi_itemlist_findparentname(struct aim_ssi_item *list, const char *sn);
 faim_export int aim_ssi_getpermdeny(struct aim_ssi_item *list);
 faim_export fu32_t aim_ssi_getpresence(struct aim_ssi_item *list);
-faim_export int aim_ssi_cleanlist(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_addbuddies(aim_session_t *sess, aim_conn_t *conn, const char *gn, const char **sn, unsigned int num);
-faim_export int aim_ssi_addmastergroup(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_addgroups(aim_session_t *sess, aim_conn_t *conn, const char **gn, unsigned int num);
-faim_export int aim_ssi_addpord(aim_session_t *sess, aim_conn_t *conn, const char **sn, unsigned int num, fu16_t type);
+faim_export char *aim_ssi_getalias(struct aim_ssi_item *list, char *gn, char *sn);
+
+/* Client functions for changing SSI data */
+faim_export int aim_ssi_addbuddy(aim_session_t *sess, aim_conn_t *conn, const char *name, const char *group, const char *alias, const char *comment, const char *smsnum, int needauth);
+faim_export int aim_ssi_addpermit(aim_session_t *sess, aim_conn_t *conn, const char *name);
+faim_export int aim_ssi_adddeny(aim_session_t *sess, aim_conn_t *conn, const char *name);
+faim_export int aim_ssi_delbuddy(aim_session_t *sess, aim_conn_t *conn, const char *name, const char *group);
+faim_export int aim_ssi_delpermit(aim_session_t *sess, aim_conn_t *conn, const char *name);
+faim_export int aim_ssi_deldeny(aim_session_t *sess, aim_conn_t *conn, const char *name);
 faim_export int aim_ssi_movebuddy(aim_session_t *sess, aim_conn_t *conn, const char *oldgn, const char *newgn, const char *sn);
 faim_export int aim_ssi_rename_group(aim_session_t *sess, aim_conn_t *conn, const char *oldgn, const char *newgn);
-faim_export int aim_ssi_delbuddies(aim_session_t *sess, aim_conn_t *conn, const char *gn, char **sn, unsigned int num);
-faim_export int aim_ssi_delmastergroup(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_delgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num);
+faim_export int aim_ssi_cleanlist(aim_session_t *sess, aim_conn_t *conn);
 faim_export int aim_ssi_deletelist(aim_session_t *sess, aim_conn_t *conn);
-faim_export int aim_ssi_delpord(aim_session_t *sess, aim_conn_t *conn, const char **sn, unsigned int num, fu16_t type);
 faim_export int aim_ssi_setpermdeny(aim_session_t *sess, aim_conn_t *conn, fu8_t permdeny, fu32_t vismask);
 faim_export int aim_ssi_setpresence(aim_session_t *sess, aim_conn_t *conn, fu32_t presence);
 
+
+
+/* icq.c */
 struct aim_icq_offlinemsg {
 	fu32_t sender;
 	fu16_t year;
--- a/src/protocols/oscar/aim_cbtypes.h	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/aim_cbtypes.h	Tue Jan 07 21:19:05 2003 +0000
@@ -213,6 +213,13 @@
 #define AIM_CB_SSI_NOLIST 0x000F
 #define AIM_CB_SSI_EDITSTART 0x0011
 #define AIM_CB_SSI_EDITSTOP 0x0012
+#define AIM_CB_SSI_SENDAUTH 0x0014
+#define AIM_CB_SSI_RECVAUTH 0x0015
+#define AIM_CB_SSI_SENDAUTHREQ 0x0018
+#define AIM_CB_SSI_RECVAUTHREQ 0x0019
+#define AIM_CB_SSI_SENDAUTHREP 0x001a
+#define AIM_CB_SSI_RECVAUTHREP 0x001b
+#define AIM_CB_SSI_ADDED 0x001c
 
 /*
  * SNAC Family: Authorizer
--- a/src/protocols/oscar/auth.c	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/auth.c	Tue Jan 07 21:19:05 2003 +0000
@@ -481,6 +481,10 @@
 	keylen = aimbs_get16(bs);
 	keystr = aimbs_getstr(bs, keylen);
 
+	/* XXX - When GiantGrayPanda signed on AIM I got a thing asking me to register 
+	 * for the netscape network.  This SNAC had a type 0x0058 TLV with length 10.  
+	 * Data is 0x0007 0004 3e19 ae1e 0006 0004 0000 0005 */
+
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
 		ret = userfunc(sess, rx, keystr);
 
--- a/src/protocols/oscar/conn.c	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/conn.c	Tue Jan 07 21:19:05 2003 +0000
@@ -879,11 +879,12 @@
 	sess->modlistv = NULL;
 
 	sess->ssi.received_data = 0;
+	sess->ssi.numitems = 0;
+	sess->ssi.official = NULL;
+	sess->ssi.local = NULL;
+	sess->ssi.pending = NULL;
+	sess->ssi.timestamp = (time_t)0;
 	sess->ssi.waiting_for_ack = 0;
-	sess->ssi.holding_queue = NULL;
-	sess->ssi.revision = 0;
-	sess->ssi.items = NULL;
-	sess->ssi.timestamp = (time_t)0;
 
 	sess->authinfo = NULL;
 	sess->emailinfo = NULL;
--- a/src/protocols/oscar/oscar.c	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/oscar.c	Tue Jan 07 21:19:05 2003 +0000
@@ -129,8 +129,9 @@
 	GSList *evilhack;
 
 	struct {
+		guint maxwatchers; /* max users who can watch you */
 		guint maxbuddies; /* max users you can watch */
-		guint maxwatchers; /* max users who can watch you */
+		guint maxgroups; /* max groups in server list */
 		guint maxpermits; /* max users on permit list */
 		guint maxdenies; /* max users on deny list */
 		guint maxsiglen; /* max size (bytes) of profile */
@@ -195,12 +196,18 @@
 	gboolean request;
 };
 
-struct channel4_data {
+struct name_data {
 	struct gaim_connection *gc;
-	gchar *uin;
+	gchar *name;
 	gchar *nick;
 };
 
+static void gaim_free_name_data(struct name_data *data) {
+	g_free(data->name);
+	g_free(data->nick);
+	g_free(data);
+}
+
 static struct direct_im *find_direct_im(struct oscar_data *od, const char *who) {
 	GSList *d = od->direct_ims;
 	char *n = g_strdup(normalize(who));
@@ -370,8 +377,15 @@
 static int gaim_icqsimpleinfo    (aim_session_t *, aim_frame_t *, ...);
 static int gaim_icqallinfo       (aim_session_t *, aim_frame_t *, ...);
 static int gaim_popup            (aim_session_t *, aim_frame_t *, ...);
+#ifndef NOSSI
 static int gaim_ssi_parserights  (aim_session_t *, aim_frame_t *, ...);
 static int gaim_ssi_parselist    (aim_session_t *, aim_frame_t *, ...);
+static int gaim_ssi_parseack     (aim_session_t *, aim_frame_t *, ...);
+static int gaim_ssi_authgiven    (aim_session_t *, aim_frame_t *, ...);
+static int gaim_ssi_authrequest  (aim_session_t *, aim_frame_t *, ...);
+static int gaim_ssi_authreply    (aim_session_t *, aim_frame_t *, ...);
+static int gaim_ssi_gotadded     (aim_session_t *, aim_frame_t *, ...);
+#endif
 
 static int gaim_directim_initiate(aim_session_t *, aim_frame_t *, ...);
 static int gaim_directim_incoming(aim_session_t *, aim_frame_t *, ...);
@@ -889,9 +903,16 @@
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_POP, 0x0002, gaim_popup, 0);
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ICQ, AIM_CB_ICQ_SIMPLEINFO, gaim_icqsimpleinfo, 0);
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_ICQ, AIM_CB_ICQ_ALLINFO, gaim_icqallinfo, 0);
+#ifndef NOSSI
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_RIGHTSINFO, gaim_ssi_parserights, 0);
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_LIST, gaim_ssi_parselist, 0);
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_NOLIST, gaim_ssi_parselist, 0);
+	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_SRVACK, gaim_ssi_parseack, 0);
+	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_RECVAUTH, gaim_ssi_authgiven, 0);
+	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_RECVAUTHREQ, gaim_ssi_authrequest, 0);
+	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_RECVAUTHREP, gaim_ssi_authreply, 0);
+	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SSI, AIM_CB_SSI_ADDED, gaim_ssi_gotadded, 0);
+#endif
 	aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_MSGTIMEOUT, oscar_sendfile_timeout, 0);
 
 	((struct oscar_data *)gc->proto_data)->conn = bosconn;
@@ -2031,42 +2052,65 @@
 }
 
 /*
- * Next 2 functions are for when other people ask you for authorization
+ * Authorization Functions
+ * Most of these are callbacks from dialogs.  They're used by both 
+ * methods of authorization (SSI and old-school channel 4 ICBM)
  */
-static void gaim_icq_authgrant(struct channel4_data *data) {
+static void gaim_auth_request(struct name_data *data) {
+	struct gaim_connection *gc = data->gc;
+	struct oscar_data *od = gc->proto_data;
+	struct buddy *buddy = find_buddy(gc, data->name);
+	struct group *group = find_group_by_buddy(gc, data->name);
+	if (buddy && group) {
+		debug_printf("ssi: adding buddy %s to group %s\n", buddy->name, group->name);
+		aim_ssi_sendauthrequest(od->sess, od->conn, data->name, "Please authorize me so I can add you to my buddy list.");
+		aim_ssi_addbuddy(od->sess, od->conn, buddy->name, group->name, get_buddy_alias_only(buddy), NULL, NULL, 1);
+	}
+	gaim_free_name_data(data);
+}
+
+static void gaim_auth_dontrequest(struct name_data *data) {
+/*	struct gaim_connection *gc = data->gc; */
+/*	struct oscar_data *od = gc->proto_data; */
+	/* XXX - Take the buddy out of our buddy list */
+	gaim_free_name_data(data);
+}
+
+/* When other people ask you for authorization */
+static void gaim_auth_grant(struct name_data *data) {
+	struct gaim_connection *gc = data->gc;
+	struct oscar_data *od = gc->proto_data;
 	gchar message;
-	struct oscar_data *od = (struct oscar_data *)data->gc->proto_data;
 	message = 0;
-	aim_send_im_ch4(od->sess, data->uin, AIM_ICQMSG_AUTHGRANTED, &message);
-	show_got_added(data->gc, NULL, data->uin, NULL, NULL);
-	g_free(data->uin);
-	g_free(data);
+#ifdef NOSSI
+	aim_send_im_ch4(od->sess, data->name, AIM_ICQMSG_AUTHGRANTED, &message);
+	show_got_added(gc, NULL, data->name, NULL, NULL);
+#else
+	aim_ssi_sendauthreply(od->sess, od->conn, data->name, 0x01, &message);
+#endif
+	gaim_free_name_data(data);
 }
 
-static void gaim_icq_authdeny(struct channel4_data *data) {
+/* When other people ask you for authorization */
+static void gaim_auth_dontgrant(struct name_data *data) {
+	struct gaim_connection *gc = data->gc;
+	struct oscar_data *od = gc->proto_data;
 	gchar *message;
-	struct oscar_data *od = (struct oscar_data *)data->gc->proto_data;
-	message = g_strdup_printf("No reason given.");
-	aim_send_im_ch4(od->sess, data->uin, AIM_ICQMSG_AUTHDENIED, message);
-	g_free(data->uin);
+	message = g_strdup_printf(_("No reason given."));
+#ifdef NOSSI
+	aim_send_im_ch4(od->sess, data->name, AIM_ICQMSG_AUTHDENIED, message);
+#else
+	aim_ssi_sendauthreply(od->sess, od->conn, data->name, 0x00, message);
+#endif
 	g_free(message);
-	g_free(data);
+	gaim_free_name_data(data);
 }
 
-/*
- * Next 2 functions are for when someone sends you contacts
- */
-static void gaim_icq_contactadd(struct channel4_data *data) {
-	show_add_buddy(data->gc, data->uin, NULL, data->nick);
-	free(data->uin);
-	free(data->nick);
-	g_free(data);
-}
-
-static void gaim_icq_contactdontadd(struct channel4_data *data) {
-	free(data->uin);
-	free(data->nick);
-	g_free(data);
+/* When someone sends you contacts  */
+static void gaim_icq_contactadd(struct name_data *data) {
+	struct gaim_connection *gc = data->gc;
+	show_add_buddy(gc, data->name, NULL, data->nick);
+	gaim_free_name_data(data);
 }
 
 static int incomingim_chan4(aim_session_t *sess, aim_conn_t *conn, aim_userinfo_t *userinfo, struct aim_incomingim_ch4_args *args, time_t t) {
@@ -2117,12 +2161,13 @@
 
 		case 0x06: { /* Someone requested authorization */
 			if (i >= 6) {
-				struct channel4_data *data = g_new(struct channel4_data, 1);
+				struct name_data *data = g_new(struct name_data, 1);
 				gchar *dialog_msg = g_strdup_printf(_("The user %lu wants to add you to their buddy list for the following reason: %s"), args->uin, msg2[5] ? msg2[5] : _("No reason given."));
 				debug_printf("Received an authorization request from UIN %lu\n", args->uin);
 				data->gc = gc;
-				data->uin = g_strdup_printf("%lu", args->uin);
-				do_ask_dialog(_("Authorization Request"), dialog_msg, data, _("Authorize"), gaim_icq_authgrant, _("Deny"), gaim_icq_authdeny);
+				data->name = g_strdup_printf("%lu", args->uin);
+				data->nick = NULL;
+				do_ask_dialog(_("Authorization Request"), dialog_msg, data, _("Authorize"), gaim_auth_grant, _("Deny"), gaim_auth_dontgrant);
 				g_free(dialog_msg);
 			}
 		} break;
@@ -2130,7 +2175,7 @@
 		case 0x07: { /* Someone has denied you authorization */
 			if (i >= 1) {
 				gchar *dialog_msg = g_strdup_printf(_("The user %lu has denied your request to add them to your contact list for the following reason:\n%s"), args->uin, msg2[0] ? msg2[0] : _("No reason given."));
-				do_error_dialog(_("ICQ authorization denied."), dialog_msg, GAIM_ERROR);
+				do_error_dialog(_("ICQ authorization denied."), dialog_msg, GAIM_INFO);
 				g_free(dialog_msg);
 			}
 		} break;
@@ -2171,12 +2216,12 @@
 				for (i=0; i<strlen(text[0]); i++)
 					num = num*10 + text[0][i]-48;
 				for (i=0; i<num; i++) {
-					struct channel4_data *data = g_new(struct channel4_data, 1);
+					struct name_data *data = g_new(struct name_data, 1);
 					gchar *message = g_strdup_printf(_("ICQ user %lu has sent you a contact: %s (%s)"), args->uin, text[i*2+2], text[i*2+1]);
 					data->gc = gc;
-					data->uin = g_strdup(text[i*2+2]);
+					data->name = g_strdup(text[i*2+2]);
 					data->nick = g_strdup(text[i*2+1]);
-					do_ask_dialog(message, _("Do you want to add this contact to your Buddy List?"), data, _("Add"), gaim_icq_contactadd, _("Deny"), gaim_icq_contactdontadd);
+					do_ask_dialog(message, _("Do you want to add this contact to your Buddy List?"), data, _("Add"), gaim_icq_contactadd, _("Deny"), gaim_free_name_data);
 					g_free(message);
 				}
 				g_strfreev(text);
@@ -3055,21 +3100,24 @@
 }
 
 static int conninitdone_bos(aim_session_t *sess, aim_frame_t *fr, ...) {
-	struct gaim_connection *gc = sess->aux_data;
-	struct oscar_data *od = gc->proto_data;
 
 	aim_reqpersonalinfo(sess, fr->conn);
+
+#ifndef NOSSI
+	debug_printf("ssi: requesting ssi list\n");
+	aim_ssi_reqrights(sess, fr->conn);
+	aim_ssi_reqdata(sess, fr->conn, sess->ssi.timestamp, sess->ssi.numitems);
+#endif
+
 	aim_bos_reqlocaterights(sess, fr->conn);
 	aim_bos_reqbuddyrights(sess, fr->conn);
-
 	aim_reqicbmparams(sess);
-
 	aim_bos_reqrights(sess, fr->conn);
-	if (od->icq) {
-		aim_bos_setgroupperm(sess, fr->conn, AIM_FLAG_ALLUSERS);
-		aim_bos_setprivacyflags(sess, fr->conn, AIM_PRIVFLAGS_ALLOWIDLE |
-			AIM_PRIVFLAGS_ALLOWMEMBERSINCE);
-	}
+
+#ifdef NOSSI
+	aim_bos_setgroupperm(sess, fr->conn, AIM_FLAG_ALLUSERS);
+	aim_bos_setprivacyflags(sess, fr->conn, AIM_PRIVFLAGS_ALLOWIDLE | AIM_PRIVFLAGS_ALLOWMEMBERSINCE);
+#endif
 
 	return 1;
 }
@@ -3226,21 +3274,15 @@
 
 	aim_clientready(sess, fr->conn);
 
-	/* XXX - Should call aim_bos_setidle with 0x0000 */
-
-	/* XXX - Should only call reqofflinemsgs when using ICQ? */
+	/* AAA - Should call aim_bos_setidle with 0x0000 */
+
+	/* AAA - Should only call reqofflinemsgs when using ICQ? */
 	aim_icq_reqofflinemsgs(sess);
 
 	aim_reqservice(sess, fr->conn, AIM_CONN_TYPE_CHATNAV);
 	if (sess->authinfo->email)
 		aim_reqservice(sess, fr->conn, AIM_CONN_TYPE_EMAIL);
 
-	if (!odata->icq) {
-		debug_printf("ssi: requesting ssi list\n");
-		aim_ssi_reqrights(sess, fr->conn);
-		aim_ssi_reqdata(sess, fr->conn, sess->ssi.timestamp, sess->ssi.revision);
-	}
-
 	return 1;
 }
 
@@ -3703,7 +3745,7 @@
 
 static void oscar_set_dir(struct gaim_connection *g, const char *first, const char *middle, const char *last,
 			  const char *maiden, const char *city, const char *state, const char *country, int web) {
-	/* FIXME : some of these things are wrong, but i'm lazy */
+	/* AAA: some of these things are wrong, but i'm lazy */
 	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
 	aim_setdirectoryinfo(odata->sess, odata->conn, first, middle, last,
 				maiden, NULL, NULL, city, state, NULL, 0, web);
@@ -3857,168 +3899,166 @@
 	return;
 }
 
-static void oscar_warn(struct gaim_connection *g, char *name, int anon) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
+static void oscar_warn(struct gaim_connection *gc, char *name, int anon) {
+	struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
 	aim_send_warning(odata->sess, odata->conn, name, anon ? AIM_WARN_ANON : 0);
 }
 
-static void oscar_dir_search(struct gaim_connection *g, const char *first, const char *middle, const char *last,
+static void oscar_dir_search(struct gaim_connection *gc, const char *first, const char *middle, const char *last,
 			     const char *maiden, const char *city, const char *state, const char *country, const char *email) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
+	struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
 	if (strlen(email))
 		aim_usersearch_address(odata->sess, odata->conn, email);
 }
 
-static void oscar_add_buddy(struct gaim_connection *g, const char *name) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (odata->icq) {
-		aim_add_buddy(odata->sess, odata->conn, name);
-	} else {
-		if ((odata->sess->ssi.received_data) && !(aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, name, 0x0000))) {
-			debug_printf("ssi: adding buddy %s to group %s\n", name, find_group_by_buddy(g, name)->name);
-			aim_ssi_addbuddies(odata->sess, odata->conn, find_group_by_buddy(g, name)->name, &name, 1);
+static void oscar_add_buddy(struct gaim_connection *gc, const char *name) {
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+#ifdef NOSSI
+	aim_add_buddy(od->sess, od->conn, name);
+#else
+	if ((od->sess->ssi.received_data) && !(aim_ssi_itemlist_exists(od->sess->ssi.local, name))) {
+		struct buddy *buddy = find_buddy(gc, name);
+		struct group *group = find_group_by_buddy(gc, name);
+		if (buddy && group) {
+			debug_printf("ssi: adding buddy %s to group %s\n", name, group->name);
+			aim_ssi_addbuddy(od->sess, od->conn, buddy->name, group->name, get_buddy_alias_only(buddy), NULL, NULL, 0);
+		}
+	}
+#endif
+}
+
+static void oscar_add_buddies(struct gaim_connection *gc, GList *buddies) {
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+#ifdef NOSSI
+	char buf[MSG_LEN];
+	int n=0;
+	while (buddies) {
+		if (n > MSG_LEN - 18) {
+			aim_bos_setbuddylist(od->sess, od->conn, buf);
+			n = 0;
 		}
+		n += g_snprintf(buf + n, sizeof(buf) - n, "%s&", (char *)buddies->data);
+		buddies = buddies->next;
+	}
+	aim_bos_setbuddylist(od->sess, od->conn, buf);
+#else
+	if (od->sess->ssi.received_data) {
+		while (buddies) {
+			struct buddy *buddy = find_buddy(gc, (const char *)buddies->data);
+			struct group *group = find_group_by_buddy(gc, (const char *)buddies->data);
+			if (buddy && group) {
+				debug_printf("ssi: adding buddy %s to group %s\n", (const char *)buddies->data, group->name);
+				aim_ssi_addbuddy(od->sess, od->conn, buddy->name, group->name, get_buddy_alias_only(buddy), NULL, NULL, 0);
+			}
+			buddies = buddies->next;
+		}
+	}
+#endif
+}
+
+#ifndef NOSSI
+static void oscar_move_buddy(struct gaim_connection *gc, const char *name, const char *old_group, const char *new_group) {
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+	if (od->sess->ssi.received_data) {
+		debug_printf("ssi: moving buddy %s from group %s to group %s\n", name, old_group, new_group);
+		aim_ssi_movebuddy(od->sess, od->conn, old_group, new_group, name);
 	}
 }
-
-static void oscar_add_buddies(struct gaim_connection *g, GList *buddies) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (odata->icq) {
-		char buf[MSG_LEN];
-		int n=0;
+#endif
+
+static void oscar_remove_buddy(struct gaim_connection *gc, char *name, char *group) {
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+#ifdef NOSSI
+	aim_remove_buddy(od->sess, od->conn, name);
+#else
+	if (od->sess->ssi.received_data) {
+		debug_printf("ssi: deleting buddy %s from group %s\n", name, group);
+		aim_ssi_delbuddy(od->sess, od->conn, name, group);
+	}
+#endif
+}
+
+static void oscar_remove_buddies(struct gaim_connection *gc, GList *buddies, const char *group) {
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+#ifdef NOSSI
+	GList *cur;
+	for (cur=buddies; cur; cur=cur->next)
+		aim_remove_buddy(od->sess, od->conn, cur->data);
+#else
+	if (od->sess->ssi.received_data) {
 		while (buddies) {
-			if (n > MSG_LEN - 18) {
-				aim_bos_setbuddylist(odata->sess, odata->conn, buf);
-				n = 0;
-			}
-			n += g_snprintf(buf + n, sizeof(buf) - n, "%s&", (char *)buddies->data);
+			debug_printf("ssi: deleting buddy %s from group %s\n", (char *)buddies->data, group);
+			aim_ssi_delbuddy(od->sess, od->conn, buddies->data, group);
 			buddies = buddies->next;
 		}
-		aim_bos_setbuddylist(odata->sess, odata->conn, buf);
-	} else {
-		if (odata->sess->ssi.received_data) {
-			int tmp;
-			GSList *curgrp, *curbud;
-			for (curgrp=g->groups; curgrp; curgrp=g_slist_next(curgrp)) {
-				tmp = 0;
-				for (curbud=((struct group*)curgrp->data)->members; curbud; curbud=curbud->next)
-					if (!aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, ((struct buddy*)curbud->data)->name, 0x0000))
-						tmp++;
-				if (tmp) {
-					char **sns = (char **)malloc(tmp*sizeof(char*));
-					tmp = 0;
-					for (curbud=((struct group*)curgrp->data)->members; curbud; curbud=curbud->next)
-						if (!aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, ((struct buddy*)curbud->data)->name, 0x0000)) {
-							debug_printf("ssi: adding buddy %s to group %s\n", ((struct buddy*)curbud->data)->name, ((struct group*)curgrp->data)->name);
-							sns[tmp] = (char *)((struct buddy*)curbud->data)->name;
-							tmp++;
-						}
-					aim_ssi_addbuddies(odata->sess, odata->conn, ((struct group*)curgrp->data)->name, (const char**)sns, tmp);
-					free(sns);
-				}
-			}
+	}
+#endif
+}
+
+#ifndef NOSSI
+static void oscar_rename_group(struct gaim_connection *g, const char *old_group, const char *new_group, GList *members) {
+	struct oscar_data *od = (struct oscar_data *)g->proto_data;
+
+	if (od->sess->ssi.received_data) {
+		if (aim_ssi_itemlist_finditem(od->sess->ssi.local, new_group, NULL, AIM_SSI_TYPE_GROUP)) {
+			oscar_remove_buddies(g, members, old_group);
+			oscar_add_buddies(g, members);
+			debug_printf("ssi: moved all buddies from group %s to %s\n", old_group, new_group);
+		} else {
+			aim_ssi_rename_group(od->sess, od->conn, old_group, new_group);
+			debug_printf("ssi: renamed group %s to %s\n", old_group, new_group);
 		}
 	}
 }
 
-static void oscar_move_buddy(struct gaim_connection *g, const char *name, const char *old_group, const char *new_group) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (!odata->icq)
-		if (odata->sess->ssi.received_data) {
-			aim_ssi_movebuddy(odata->sess, odata->conn, old_group, new_group, name);
-			debug_printf("ssi: moved buddy %s from group %s to group %s\n", name, old_group, new_group);
-		}
-}
-
-static void oscar_remove_buddy(struct gaim_connection *g, char *name, char *group) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (odata->icq) {
-		aim_remove_buddy(odata->sess, odata->conn, name);
-	} else {
-		if (odata->sess->ssi.received_data) {
-			struct aim_ssi_item *ssigroup;
-			while (aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, name, 0x0000) && (ssigroup = aim_ssi_itemlist_findparent(odata->sess->ssi.items, name)) && !aim_ssi_delbuddies(odata->sess, odata->conn, ssigroup->name, &name, 1))
-				debug_printf("ssi: deleted buddy %s from group %s\n", name, group);
-		}
-	}
-}
-
-static void oscar_remove_buddies(struct gaim_connection *g, GList *buddies, const char *group) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (odata->icq) {
-		GList *cur;
-		for (cur=buddies; cur; cur=cur->next)
-			aim_remove_buddy(odata->sess, odata->conn, cur->data);
-	} else {
-		if (odata->sess->ssi.received_data) {
-			GList *cur;
-			int tmp = 0;
-			for (cur=buddies; cur; cur=cur->next)
-				if (aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, cur->data, 0x0000))
-					tmp++;
-			if (tmp) {
-				char **sns;
-				sns = (char **)malloc(tmp*sizeof(char*));
-				tmp = 0;
-				for (cur=buddies; cur; cur=cur->next)
-					if (aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, cur->data, 0x0000)) {
-						debug_printf("ssi: deleting buddy %s from group %s\n", (char *)cur->data, group);
-						sns[tmp] = cur->data;
-						tmp++;
-					}
-				aim_ssi_delbuddies(odata->sess, odata->conn, group, sns, tmp);
-				free(sns);
-			}
-		}
-	}
-}
-
-static void oscar_rename_group(struct gaim_connection *g, const char *old_group, const char *new_group, GList *members) {
-	struct oscar_data *odata = (struct oscar_data *)g->proto_data;
-	if (!odata->icq)
-		if (odata->sess->ssi.received_data) {
-			if (aim_ssi_itemlist_finditem(odata->sess->ssi.items, NULL, new_group, 0x0001)) {
-				oscar_remove_buddies(g, members, old_group);
-				oscar_add_buddies(g, members);
-				debug_printf("ssi: moved all buddies from group %s to %s\n", old_group, new_group);
-			} else {
-				aim_ssi_rename_group(odata->sess, odata->conn, old_group, new_group);
-				debug_printf("ssi: renamed group %s to %s\n", old_group, new_group);
-			}
-		}
-}
-
 static int gaim_ssi_parserights(aim_session_t *sess, aim_frame_t *fr, ...) {
-/*	XXX - Fix parsing of the ssi rights packet and pass us the data
-	fu16_t maxbuddies, maxgroups, maxpermits, maxdenies;
+	struct gaim_connection *gc = sess->aux_data;
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
+	int numtypes, i;
+	fu16_t *maxitems;
 	va_list ap;
 
 	va_start(ap, fr);
-	maxbuddies = (fu16_t)va_arg(ap, unsigned int);
-	maxgroupss = (fu16_t)va_arg(ap, unsigned int);
-	maxpermits = (fu16_t)va_arg(ap, unsigned int);
-	maxdenies = (fu16_t)va_arg(ap, unsigned int);
+	numtypes = va_arg(ap, int);
+	maxitems = va_arg(ap, fu16_t *);
 	va_end(ap);
 
-	debug_printf("ssi rights: Max buddies = %d / Max groups = %d / Max permits = %d / Max denies = %d\n", maxbuddies, maxgroups, maxpermits, maxdenies);
-*/
+	debug_printf("ssi rights:");
+	for (i=0; i<numtypes; i++)
+		debug_printf(" max type %d = %hu, ", i, maxitems[i]);
+
+	if (numtypes >= 0)
+		od->rights.maxbuddies = maxitems[0];
+	if (numtypes >= 1)
+		od->rights.maxgroups = maxitems[1];
+	if (numtypes >= 2)
+		od->rights.maxpermits = maxitems[2];
+	if (numtypes >= 3)
+		od->rights.maxdenies = maxitems[3];
 
 	return 1;
 }
 
 static int gaim_ssi_parselist(aim_session_t *sess, aim_frame_t *fr, ...) {
 	struct gaim_connection *gc = sess->aux_data;
-	struct oscar_data *odata = (struct oscar_data *)gc->proto_data;
+	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
 	struct aim_ssi_item *curitem;
 	int tmp;
-	const char **sns;
+	/* AAA - use these?
+	va_list ap;
+
+	va_start(ap, fr);
+	fmtver = (fu16_t)va_arg(ap, int);
+	numitems = (fu16_t)va_arg(ap, int);
+	items = va_arg(ap, struct aim_ssi_item);
+	timestamp = va_arg(ap, fu32_t);
+	va_end(ap); */
 
 	debug_printf("ssi: syncing local list and server list\n");
 
-	if (odata->icq)
-		return 1;
-
 	/* Activate SSI */
+	/* Sending the enable causes other people to be able to see you */
+	/* Maybe send it after merging the lists? */
 	debug_printf("ssi: activating server-stored buddy list\n");
 	aim_ssi_enable(sess, fr->conn);
 
@@ -4027,24 +4067,27 @@
 
 	/* Add from server list to local list */
 	tmp = 0;
-	for (curitem=sess->ssi.items; curitem; curitem=curitem->next) {
+	for (curitem=sess->ssi.local; curitem; curitem=curitem->next) {
 		switch (curitem->type) {
-			case 0x0000: /* Buddy */
+			case 0x0000: { /* Buddy */
 				if ((curitem->name) && (!find_buddy(gc, curitem->name))) {
-					struct aim_ssi_item *curgroup = sess->ssi.items;
-					while (curgroup) {
-						if ((curgroup->type == 0x0001) && (curgroup->gid == curitem->gid) && (curgroup->name)) {
-							debug_printf("ssi: adding buddy %s to group %s to local list\n", curitem->name, curgroup->name);
-							add_buddy(gc, curgroup->name, curitem->name, 0);
-							tmp++;
-						}
-						curgroup = curgroup->next;
-					}
+					char *gname = aim_ssi_itemlist_findparentname(od->sess->ssi.local, curitem->name);
+					char *alias = aim_ssi_getalias(sess->ssi.local, gname, curitem->name);
+					debug_printf("ssi: adding buddy %s to group %s to local list\n", curitem->name, gname);
+					add_buddy(gc, (gname ? gname : "orphans"), curitem->name, alias);
+					free(alias);
+					tmp++;
 				}
-				break;
-
-			case 0x0002: /* Permit buddy */
+			} break;
+
+			case 0x0001: { /* Group */
+				if (curitem->name && !find_group(gc, curitem->name))
+					add_group(gc, curitem->name);
+			} break;
+
+			case 0x0002: { /* Permit buddy */
 				if (curitem->name) {
+					/* if (!find_permdeny_by_name(gc->permit, curitem->name)) { AAA */
 					GSList *list;
 					for (list=gc->permit; (list && aim_sncmp(curitem->name, list->data)); list=list->next);
 					if (!list) {
@@ -4056,9 +4099,9 @@
 						tmp++;
 					}
 				}
-				break;
-
-			case 0x0003: /* Deny buddy */
+			} break;
+
+			case 0x0003: { /* Deny buddy */
 				if (curitem->name) {
 					GSList *list;
 					for (list=gc->deny; (list && aim_sncmp(curitem->name, list->data)); list=list->next);
@@ -4071,24 +4114,26 @@
 						tmp++;
 					}
 				}
-				break;
-
-			case 0x0004: /* Permit/deny setting */
+			} break;
+
+			case 0x0004: { /* Permit/deny setting */
 				if (curitem->data) {
 					fu8_t permdeny;
-					if ((permdeny = aim_ssi_getpermdeny(sess->ssi.items)) && (permdeny != gc->permdeny)) {
-						debug_printf("ssi: changing permdeny from %d to %d\n", gc->permdeny, permdeny);
+					if ((permdeny = aim_ssi_getpermdeny(sess->ssi.local)) && (permdeny != gc->permdeny)) {
+						debug_printf("ssi: changing permdeny from %d to %hhu\n", gc->permdeny, permdeny);
 						gc->permdeny = permdeny;
 						tmp++;
 					}
 				}
-				break;
-
-			case 0x0005: /* Presence setting */
+			} break;
+
+			case 0x0005: { /* Presence setting */
 				/* We don't want to change Gaim's setting because it applies to all accounts */
-				break;
+			} break;
 		} /* End of switch on curitem->type */
 	} /* End of for loop */
+
+	/* If changes were made, then flush buddy list to file */
 	if (tmp)
 		do_export(gc);
 
@@ -4100,67 +4145,34 @@
 		cur = gc->groups;
 		while (cur) {
 			GSList *curbud;
-			tmp = 0;
 			for (curbud=((struct group*)cur->data)->members; curbud; curbud=curbud->next)
-				if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, ((struct buddy*)curbud->data)->name, 0x0000))
-					tmp++;
-			if (tmp) {
-				sns = malloc(tmp*sizeof(char*));
-				tmp = 0;
-				for (curbud=((struct group*)cur->data)->members; curbud; curbud=curbud->next)
-					if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, ((struct buddy*)curbud->data)->name, 0x0000)) {
-						debug_printf("ssi: adding buddy %s from local list to server list\n", ((struct buddy*)curbud->data)->name);
-						sns[tmp] = ((char *)((struct buddy*)curbud->data)->name);
-						tmp++;
-					}
-				aim_ssi_addbuddies(sess, fr->conn, ((struct group*)cur->data)->name, sns, tmp);
-				free(sns);
-			}
+				if (!aim_ssi_itemlist_exists(sess->ssi.local, ((struct buddy*)curbud->data)->name)) {
+					debug_printf("ssi: adding buddy %s from local list to server list\n", ((struct buddy*)curbud->data)->name);
+					aim_ssi_addbuddy(od->sess, od->conn, ((struct buddy*)curbud->data)->name, ((struct group*)cur->data)->name, get_buddy_alias_only((struct buddy *)curbud->data), NULL, NULL, 0);
+				}
 			cur = g_slist_next(cur);
 		}
 
 		/* Permit list */
 		if (gc->permit) {
-			tmp = 0;
 			for (cur=gc->permit; cur; cur=cur->next)
-				if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, cur->data, 0x0002))
-					tmp++;
-			if (tmp) {
-				sns = malloc(tmp*sizeof(char*));
-				tmp = 0;
-				for (cur=gc->permit; cur; cur=cur->next)
-					if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, cur->data, 0x0002)) {
-						debug_printf("ssi: adding permit %s from local list to server list\n", (char *)cur->data);
-						sns[tmp] = cur->data;
-						tmp++;
-					}
-				aim_ssi_addpord(sess, fr->conn, sns, tmp, AIM_SSI_TYPE_PERMIT);
-				free(sns);
-			}
+				if (!aim_ssi_itemlist_finditem(sess->ssi.local, NULL, cur->data, AIM_SSI_TYPE_PERMIT)) {
+					debug_printf("ssi: adding permit %s from local list to server list\n", (char *)cur->data);
+					aim_ssi_addpermit(od->sess, od->conn, cur->data);
+				}
 		}
 
 		/* Deny list */
 		if (gc->deny) {
-			tmp = 0;
 			for (cur=gc->deny; cur; cur=cur->next)
-				if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, cur->data, 0x0003))
-					tmp++;
-			if (tmp) {
-				sns = malloc(tmp*sizeof(char*));
-				tmp = 0;
-				for (cur=gc->deny; cur; cur=cur->next)
-					if (!aim_ssi_itemlist_finditem(sess->ssi.items, NULL, cur->data, 0x0003)) {
-						debug_printf("ssi: adding deny %s from local list to server list\n", (char *)cur->data);
-						sns[tmp] = cur->data;
-						tmp++;
-					}
-				aim_ssi_addpord(sess, fr->conn, sns, tmp, AIM_SSI_TYPE_DENY);
-				free(sns);
-			}
+				if (!aim_ssi_itemlist_finditem(sess->ssi.local, NULL, cur->data, AIM_SSI_TYPE_DENY)) {
+					debug_printf("ssi: adding deny %s from local list to server list\n", (char *)cur->data);
+					aim_ssi_adddeny(od->sess, od->conn, cur->data);
+				}
 		}
 
 		/* Presence settings (idle time visibility) */
-		if ((tmp = aim_ssi_getpresence(sess->ssi.items)) != 0xFFFFFFFF)
+		if ((tmp = aim_ssi_getpresence(sess->ssi.local)) != 0xFFFFFFFF)
 			if (report_idle && !(tmp & 0x400))
 				aim_ssi_setpresence(sess, fr->conn, tmp | 0x400);
 
@@ -4169,10 +4181,10 @@
 			struct group* gr = (struct group*)cur->data;
 			tmp = tmp + g_slist_length(gr->members);
 		}
-		if (tmp > odata->rights.maxbuddies) {
+		if (tmp > od->rights.maxbuddies) {
 			char *dialog_msg = g_strdup_printf(_("The maximum number of buddies allowed in your buddy list is %d, and you have %d."
 							     "  Until you are below the limit, some buddies will not show up as online."), 
-							   odata->rights.maxbuddies, tmp);
+							   od->rights.maxbuddies, tmp);
 			do_error_dialog("Maximum buddy list length exceeded.", dialog_msg, GAIM_WARNING);
 			g_free(dialog_msg);
 		}
@@ -4181,6 +4193,139 @@
 
 	return 1;
 }
+#endif
+
+static int gaim_ssi_parseack(aim_session_t *sess, aim_frame_t *fr, ...) {
+	struct gaim_connection *gc = sess->aux_data;
+	va_list ap;
+	struct aim_ssi_tmp *retval;
+
+	va_start(ap, fr);
+	retval = va_arg(ap, struct aim_ssi_tmp *);
+	va_end(ap);
+
+	while (retval) {
+		debug_printf("ssi: status is 0x%04hx for a 0x%04hx action with name %s\n", retval->ack,  retval->action, retval->item ? retval->item->name : "no item");
+
+		if (retval->ack != 0xffff)
+		switch (retval->ack) {
+			case 0x0000: { /* added successfully */
+			} break;
+
+			case 0x000e: { /* contact requires authorization */
+				if (retval->action == AIM_CB_SSI_ADD) {
+					struct name_data *data = g_new(struct name_data, 1);
+					gchar *dialog_msg = g_strdup_printf(_("The user %s requires authorization before being added to a buddy list.  Do you want to send an authorization request?"), retval->name);
+					data->gc = gc;
+					data->name = g_strdup(retval->name);
+					data->nick = NULL;
+					do_ask_dialog(_("Request Authorization"), dialog_msg, data, _("Request Authorization"), gaim_auth_request, _("Cancel"), gaim_auth_dontrequest);
+					g_free(dialog_msg);
+				}
+			} break;
+
+			default: { /* La la la */
+				debug_printf("ssi: Action 0x%04hx was unsuccessful with error 0x%04hx\n", retval->action, retval->ack);
+				/* Should remove buddy from local list and give an error message? */
+			} break;
+		}
+
+		retval = retval->next;
+	}
+
+	return 1;
+}
+
+static int gaim_ssi_authgiven(aim_session_t *sess, aim_frame_t *fr, ...) {
+	struct gaim_connection *gc = sess->aux_data;
+	va_list ap;
+	char *sn, *msg;
+	gchar *dialog_msg;
+	struct name_data *data;
+
+	va_start(ap, fr);
+	sn = va_arg(ap, char *);
+	msg = va_arg(ap, char *);
+	va_end(ap);
+
+	debug_printf("ssi: %s has given you permission to add him to your buddy list\n", sn);
+
+	dialog_msg = g_strdup_printf(_("The user %s has given you permission to add you to their buddy list.  Do you want to add them?"), sn);
+	data = g_new(struct name_data, 1);
+	data->gc = gc;
+	data->name = g_strdup(sn);
+	data->nick = NULL;
+	do_ask_dialog(_("Authorization Given"), dialog_msg, data, _("Yes"), gaim_icq_contactadd, _("No"), gaim_free_name_data);
+	g_free(dialog_msg);
+
+	return 1;
+}
+
+static int gaim_ssi_authrequest(aim_session_t *sess, aim_frame_t *fr, ...) {
+	struct gaim_connection *gc = sess->aux_data;
+	va_list ap;
+	char *sn, *msg;
+	gchar *dialog_msg;
+	struct name_data *data;
+
+	va_start(ap, fr);
+	sn = va_arg(ap, char *);
+	msg = va_arg(ap, char *);
+	va_end(ap);
+
+	debug_printf("ssi: received authorization request from %s\n", sn);
+
+	dialog_msg = g_strdup_printf(_("The user %s wants to add you to their buddy list for the following reason: %s"), sn, msg ? msg : _("No reason given."));
+	data = g_new(struct name_data, 1);
+	data->gc = gc;
+	data->name = g_strdup(sn);
+	data->nick = NULL;
+	do_ask_dialog(_("Authorization Request"), dialog_msg, data, _("Authorize"), gaim_auth_grant, _("Deny"), gaim_auth_dontgrant);
+	g_free(dialog_msg);
+
+	return 1;
+}
+
+static int gaim_ssi_authreply(aim_session_t *sess, aim_frame_t *fr, ...) {
+	va_list ap;
+	char *sn, *msg;
+	gchar *dialog_msg;
+	fu8_t reply;
+
+	va_start(ap, fr);
+	sn = va_arg(ap, char *);
+	reply = (fu8_t)va_arg(ap, int);
+	msg = va_arg(ap, char *);
+	va_end(ap);
+
+	if (reply) {
+		/* Granted */
+		dialog_msg = g_strdup_printf(_("The user %s has granted your request to add them to your contact list."), sn);
+		do_error_dialog(_("Authorization Granted"), dialog_msg, GAIM_INFO);
+	} else {
+		/* Denied */
+		dialog_msg = g_strdup_printf(_("The user %s has denied your request to add them to your contact list for the following reason:\n%s"), sn, msg ? msg : _("No reason given."));
+		do_error_dialog(_("Authorization Denied"), dialog_msg, GAIM_INFO);
+	}
+	g_free(dialog_msg);
+
+	return 1;
+}
+
+static int gaim_ssi_gotadded(aim_session_t *sess, aim_frame_t *fr, ...) {
+	struct gaim_connection *gc = sess->aux_data;
+	va_list ap;
+	char *sn;
+
+	va_start(ap, fr);
+	sn = va_arg(ap, char *);
+	va_end(ap);
+
+	debug_printf("ssi: %s added you to their buddy list\n", sn);
+	show_got_added(gc, NULL, sn, NULL, NULL);
+
+	return 1;
+}
 
 static GList *oscar_chat_info(struct gaim_connection *gc) {
 	GList *m = NULL;
@@ -4725,105 +4870,105 @@
 
 static void oscar_set_permit_deny(struct gaim_connection *gc) {
 	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
-	if (od->icq) {
-		GSList *list, *g;
-		char buf[MAXMSGLEN];
-		int at;
-
-		switch(gc->permdeny) {
-		case 1:
-			aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, gc->username);
-			break;
-		case 2:
-			aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, gc->username);
-			break;
-		case 3:
-			list = gc->permit;
-			at = 0;
+#ifdef NOSSI
+	GSList *list, *g;
+	char buf[MAXMSGLEN];
+	int at;
+
+	switch(gc->permdeny) {
+	case 1:
+		aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, gc->username);
+		break;
+	case 2:
+		aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, gc->username);
+		break;
+	case 3:
+		list = gc->permit;
+		at = 0;
+		while (list) {
+			at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
+			list = list->next;
+		}
+		aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, buf);
+		break;
+	case 4:
+		list = gc->deny;
+		at = 0;
+		while (list) {
+			at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
+			list = list->next;
+		}
+		aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, buf);
+		break;
+	case 5:
+		g = gc->groups;
+		at = 0;
+		while (g) {
+		        list = ((struct group *)g->data)->members;
 			while (list) {
-				at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
+				at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", ((struct buddy *)list->data)->name);
 				list = list->next;
 			}
-			aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, buf);
-			break;
-		case 4:
-			list = gc->deny;
-			at = 0;
-			while (list) {
-				at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", (char *)list->data);
-				list = list->next;
-			}
-			aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_DENYADD, buf);
-			break;
-		case 5:
-			g = gc->groups;
-			at = 0;
-			while (g) {
-			        list = ((struct group *)g->data)->members;
-				while (list) {
-					at += g_snprintf(buf + at, sizeof(buf) - at, "%s&", ((struct buddy *)list->data)->name);
-					list = list->next;
-				}
-				g = g->next;
-			}			
-			aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, buf);
-			break;
-		default:
-			break;
-		}
-		signoff_blocked(gc);
-	} else {
-		if (od->sess->ssi.received_data)
-			aim_ssi_setpermdeny(od->sess, od->conn, gc->permdeny, 0xffffffff);
+			g = g->next;
+		}			
+		aim_bos_changevisibility(od->sess, od->conn, AIM_VISIBILITYCHANGE_PERMITADD, buf);
+		break;
+	default:
+		break;
 	}
+	signoff_blocked(gc);
+#else
+	if (od->sess->ssi.received_data)
+		aim_ssi_setpermdeny(od->sess, od->conn, gc->permdeny, 0xffffffff);
+#endif
 }
 
 static void oscar_add_permit(struct gaim_connection *gc, char *who) {
 	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
-	if (od->icq) {
-		if (gc->permdeny != 3) return;
-		oscar_set_permit_deny(gc);
-	} else {
-		debug_printf("ssi: About to add a permit\n");
-		if (od->sess->ssi.received_data)
-			aim_ssi_addpord(od->sess, od->conn, (const char **) &who, 1, AIM_SSI_TYPE_PERMIT);
-	}
+#ifdef NOSSI
+	if (gc->permdeny != 3) return;
+	oscar_set_permit_deny(gc);
+#else
+	debug_printf("ssi: About to add a permit\n");
+	if (od->sess->ssi.received_data)
+		aim_ssi_addpermit(od->sess, od->conn, who);
+#endif
 }
 
 static void oscar_add_deny(struct gaim_connection *gc, char *who) {
 	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
-	if (od->icq) {
-		if (gc->permdeny != 4) return;
-		oscar_set_permit_deny(gc);
-	} else {
-		debug_printf("ssi: About to add a deny\n");
-		if (od->sess->ssi.received_data)
-			aim_ssi_addpord(od->sess, od->conn, (const char **) &who, 1, AIM_SSI_TYPE_DENY);
-	}
+#ifdef NOSSI
+	if (gc->permdeny != 4) return;
+	oscar_set_permit_deny(gc);
+#else
+	debug_printf("ssi: About to add a deny\n");
+	if (od->sess->ssi.received_data)
+		aim_ssi_adddeny(od->sess, od->conn, who);
+#endif
 }
 
 static void oscar_rem_permit(struct gaim_connection *gc, char *who) {
 	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
-	if (od->icq) {
-		if (gc->permdeny != 3) return;
-		oscar_set_permit_deny(gc);
-	} else {
-		debug_printf("ssi: About to delete a permit\n");
-		if (od->sess->ssi.received_data)
-			aim_ssi_delpord(od->sess, od->conn, (const char **) &who, 1, AIM_SSI_TYPE_PERMIT);
-	}
+#ifdef NOSSI
+	if (gc->permdeny != 3) return;
+	oscar_set_permit_deny(gc);
+#else
+	debug_printf("ssi: About to delete a permit\n");
+	if (od->sess->ssi.received_data)
+		aim_ssi_delpermit(od->sess, od->conn, who);
+#endif
 }
 
 static void oscar_rem_deny(struct gaim_connection *gc, char *who) {
 	struct oscar_data *od = (struct oscar_data *)gc->proto_data;
-	if (od->icq) {
-		if (gc->permdeny != 4) return;
-		oscar_set_permit_deny(gc);
-	} else {
-		debug_printf("ssi: About to delete a deny\n");
-		if (od->sess->ssi.received_data)
-			aim_ssi_delpord(od->sess, od->conn, (const char **) &who, 1, AIM_SSI_TYPE_DENY);
-	}
+#ifdef NOSSI
+	if (gc->permdeny != 4) return;
+	oscar_set_permit_deny(gc);
+#else
+	debug_printf("ssi: About to delete a deny\n");
+	if (od->sess->ssi.received_data)
+		aim_ssi_deldeny(od->sess, od->conn, who);
+#endif
 }
 
 static GList *oscar_away_states(struct gaim_connection *gc)
@@ -5037,8 +5182,10 @@
 	ret->change_passwd = oscar_change_passwd;
 	ret->add_buddy = oscar_add_buddy;
 	ret->add_buddies = oscar_add_buddies;
+#ifndef NOSSI
 	ret->group_buddy = oscar_move_buddy;
 	ret->rename_group = oscar_rename_group;
+#endif
 	ret->file_transfer_cancel = oscar_file_transfer_cancel;
 	ret->file_transfer_in = oscar_file_transfer_in;
 	ret->file_transfer_out = oscar_file_transfer_out;
--- a/src/protocols/oscar/ssi.c	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/ssi.c	Tue Jan 07 21:19:05 2003 +0000
@@ -1,13 +1,17 @@
 /*
- * Server-Side/Stored Information.
+ * Family 0x0013 - Server-Side/Stored Information.
  *
  * Relatively new facility that allows storing of certain types of information,
  * such as a users buddy list, permit/deny list, and permit/deny preferences, 
  * to be stored on the server, so that they can be accessed from any client.
  *
- * We keep a copy of the ssi data in sess->ssi, because the data needs to be 
- * accessed for various reasons.  So all the "aim_ssi_itemlist_bleh" functions 
- * near the top just manage the local data.
+ * We keep 2 copies of SSI data:
+ * 1) An exact copy of what is stored on the AIM servers.
+ * 2) A local copy that we make changes to, and then send diffs 
+ *    between this and the exact copy to keep them in sync.
+ *
+ * All the "aim_ssi_itemlist_bleh" functions near the top just modify the list 
+ * that is given to them (eg. they don't send SNACs).
  *
  * The SNAC sending and receiving functions are lower down in the file, and 
  * they're simpler.  They are in the order of the subtypes they deal with, 
@@ -17,8 +21,7 @@
  * This is entirely too complicated.
  * You don't know the half of it.
  *
- * XXX - Test for memory leaks
- * XXX - Better parsing of rights, and use the rights info to limit adds
+ * XXX - Preserve unknown data in TLV lists
  *
  */
 
@@ -26,141 +29,233 @@
 #include <aim.h>
 
 /**
- * Locally add a new item to the given item list.
+ * Locally rebuild the 0x00c8 TLV in the additional data of the given group.
  *
  * @param list A pointer to a pointer to the current list of items.
- * @param parent A pointer to the parent group, or NULL if the item should have no 
- *        parent group (ie. the group ID# should be 0).
+ * @param name A null terminated string containing the group name, or NULL 
+ *        if you want to modify the master group.
+ * @return Return a pointer to the modified item.
+ */
+static struct aim_ssi_item *aim_ssi_itemlist_rebuildgroup(struct aim_ssi_item *list, const char *name)
+{
+	int newlen;
+	struct aim_ssi_item *cur, *group;
+
+	if (!list)
+		return NULL;
+
+	/* Find the group */
+	if (!(group = aim_ssi_itemlist_finditem(list, name, NULL, AIM_SSI_TYPE_GROUP)))
+		return NULL;
+
+	/* Free the old data */
+	aim_freetlvchain(&group->data);
+	group->data = NULL;
+
+	/* Find the length for the new additional data */
+	newlen = 0;
+	if (group->gid == 0x0000) {
+		for (cur=list; cur; cur=cur->next)
+			if ((cur->type == AIM_SSI_TYPE_GROUP) && (cur->gid != 0x0000))
+				newlen += 2;
+	} else {
+		for (cur=list; cur; cur=cur->next)
+			if ((cur->gid == group->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
+				newlen += 2;
+	}
+
+	/* Build the new TLV list */
+	if (newlen > 0) {
+		fu8_t *newdata;
+
+		if (!(newdata = (fu8_t *)malloc((newlen)*sizeof(fu8_t))))
+			return NULL;
+		newlen = 0;
+		if (group->gid == 0x0000) {
+			for (cur=list; cur; cur=cur->next)
+				if ((cur->type == AIM_SSI_TYPE_GROUP) && (cur->gid != 0x0000))
+						newlen += aimutil_put16(newdata+newlen, cur->gid);
+		} else {
+			for (cur=list; cur; cur=cur->next)
+				if ((cur->gid == group->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
+						newlen += aimutil_put16(newdata+newlen, cur->bid);
+		}
+		aim_addtlvtochain_raw(&group->data, 0x00c8, newlen, newdata);
+
+		free(newdata);
+	}
+
+	return group;
+}
+
+/**
+ * Locally add a new item to the given item list.
+ *
+ * XXX - This should copy data, not just use it.
+ *
+ * @param list A pointer to a pointer to the current list of items.
  * @param name A null terminated string of the name of the new item, or NULL if the 
  *        item should have no name.
- * @param type The type of the item, 0x0001 for a contact, 0x0002 for a group, etc.
- * @return The newly created item.
+ * @param gid The group ID# you want the new item to have, or 0xFFFF if we should pick something.
+ * @param bid The buddy ID# you want the new item to have, or 0xFFFF if we should pick something.
+ * @param type The type of the item, 0x0000 for a contact, 0x0001 for a group, etc.
+ * @param data The additional data for the new item.
+ * @return A pointer to the newly created item.
  */
-static struct aim_ssi_item *aim_ssi_itemlist_add(struct aim_ssi_item **list, struct aim_ssi_item *parent, const char *name, fu16_t type)
+static struct aim_ssi_item *aim_ssi_itemlist_add(struct aim_ssi_item **list, const char *name, fu16_t gid, fu16_t bid, fu16_t type, aim_tlvlist_t *data)
 {
 	int i;
-	struct aim_ssi_item *cur, *newitem;
+	struct aim_ssi_item *cur, *new;
 
-	if (!(newitem = (struct aim_ssi_item *)malloc(sizeof(struct aim_ssi_item))))
+	if (!list)
+		return NULL;
+
+	if (!(new = (struct aim_ssi_item *)malloc(sizeof(struct aim_ssi_item))))
 		return NULL;
 
 	/* Set the name */
 	if (name) {
-		if (!(newitem->name = (char *)malloc((strlen(name)+1)*sizeof(char)))) {
-			free(newitem);
-			return NULL;
-		}
-		strcpy(newitem->name, name);
+		new->name = (char *)malloc((strlen(name)+1)*sizeof(char));
+		strcpy(new->name, name);
 	} else
-		newitem->name = NULL;
+		new->name = NULL;
 
-	/* Set the group ID# and the buddy ID# */
-	newitem->gid = 0x0000;
-	newitem->bid = 0x0000;
+	/* Set the group ID# and buddy ID# */
+	new->gid = gid;
+	new->bid = bid;
 	if (type == AIM_SSI_TYPE_GROUP) {
-		if (name)
+		if ((new->gid == 0xFFFF) && name) {
 			do {
-				newitem->gid += 0x0001;
+				new->gid += 0x0001;
 				for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
-					if ((cur->gid == newitem->gid) && (cur->gid == newitem->gid))
+					if ((cur->type == AIM_SSI_TYPE_GROUP) && (cur->gid == new->gid))
+						i=1;
+			} while (i);
+		}
+	} else {
+		if (new->bid == 0xFFFF) {
+			do {
+				new->bid += 0x0001;
+				for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
+					if ((cur->bid == new->bid) && (cur->gid == new->gid))
 						i=1;
 			} while (i);
-	} else {
-		if (parent)
-			newitem->gid = parent->gid;
-		do {
-			newitem->bid += 0x0001;
-			for (cur=*list, i=0; ((cur) && (!i)); cur=cur->next)
-				if ((cur->bid == newitem->bid) && (cur->gid == newitem->gid))
-					i=1;
-		} while (i);
+		}
 	}
 
-	/* Set the rest */
-	newitem->type = type;
-	newitem->data = NULL;
-	newitem->next = *list;
-	*list = newitem;
+	/* Set the type */
+	new->type = type;
+
+	/* Set the TLV list */
+	new->data = data;
 
-	return newitem;
+	/* Add the item to the list in the correct numerical position.  Fancy, eh? */
+	if (*list) {
+		if ((new->gid < (*list)->gid) || ((new->gid == (*list)->gid) && (new->bid < (*list)->bid))) {
+			new->next = *list;
+			*list = new;
+		} else {
+			struct aim_ssi_item *prev;
+			for ((prev=*list, cur=(*list)->next); (cur && (new->gid > cur->gid || ((new->gid == cur->gid) && (new->bid > cur->bid)))); prev=cur, cur=cur->next);
+			new->next = prev->next;
+			prev->next = new;
+		}
+	} else {
+		new->next = *list;
+		*list = new;
+	}
+
+	return new;
 }
 
 /**
- * Locally rebuild the 0x00c8 TLV in the additional data of the given group.
+ * Locally delete an item from the given item list.
  *
  * @param list A pointer to a pointer to the current list of items.
- * @param parentgroup A pointer to the group who's additional data you want to rebuild.
+ * @param del A pointer to the item you want to remove from the list.
  * @return Return 0 if no errors, otherwise return the error number.
  */
-static int aim_ssi_itemlist_rebuildgroup(struct aim_ssi_item **list, struct aim_ssi_item *parentgroup)
+static int aim_ssi_itemlist_del(struct aim_ssi_item **list, struct aim_ssi_item *del)
 {
-	int newlen;
-	struct aim_ssi_item *cur;
+	if (!list || !(*list) || !del)
+		return -EINVAL;
 
-	/* Free the old additional data */
-	if (parentgroup->data) {
-		aim_freetlvchain((aim_tlvlist_t **)&parentgroup->data);
-		parentgroup->data = NULL;
+	/* Remove the item from the list */
+	if (*list == del) {
+		*list = del->next;
+	} else {
+		struct aim_ssi_item *cur;
+		for (cur=*list; (cur->next && (cur->next!=del)); cur=cur->next);
+		if (cur->next)
+			cur->next = cur->next->next;
 	}
 
-	/* Find the length for the new additional data */
-	newlen = 0;
-	if (parentgroup->gid == 0x0000) {
-		for (cur=*list; cur; cur=cur->next)
-			if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
-				newlen += 2;
-	} else {
-		for (cur=*list; cur; cur=cur->next)
-			if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
-				newlen += 2;
-	}
-
-	/* Rebuild the additional data */
-	if (newlen>0) {
-		fu8_t *newdata;
-
-		if (!(newdata = (fu8_t *)malloc((newlen)*sizeof(fu8_t))))
-			return -ENOMEM;
-		newlen = 0;
-		if (parentgroup->gid == 0x0000) {
-			for (cur=*list; cur; cur=cur->next)
-				if ((cur->gid != 0x0000) && (cur->type == AIM_SSI_TYPE_GROUP))
-						newlen += aimutil_put16(newdata+newlen, cur->gid);
-		} else {
-			for (cur=*list; cur; cur=cur->next)
-				if ((cur->gid == parentgroup->gid) && (cur->type == AIM_SSI_TYPE_BUDDY))
-						newlen += aimutil_put16(newdata+newlen, cur->bid);
-		}
-		aim_addtlvtochain_raw((aim_tlvlist_t **)&(parentgroup->data), 0x00c8, newlen, newdata);
-
-		free(newdata);
-	}
+	/* Free the deleted item */
+	free(del->name);
+	aim_freetlvchain(&del->data);
+	free(del);
 
 	return 0;
 }
 
 /**
- * Locally free all of the stored buddy list information.
+ * Compare two items to see if they have the same data.
  *
- * @param sess The oscar session.
- * @return Return 0 if no errors, otherwise return the error number.
+ * @param cur1 A pointer to a pointer to the first item.
+ * @param cur2 A pointer to a pointer to the second item.
+ * @return Return 0 if no differences, or a number if there are differences.
  */
-static int aim_ssi_freelist(aim_session_t *sess)
+static int aim_ssi_itemlist_cmp(struct aim_ssi_item *cur1, struct aim_ssi_item *cur2)
 {
-	struct aim_ssi_item *cur, *delitem;
+	if (!cur1 || !cur2)
+		return 1;
+
+	if (cur1->data && !cur2->data)
+		return 2;
+
+	if (!cur1->data && cur2->data)
+		return 3;
+
+	if (cur1->data && cur2->data) {
+		/* Write each TLV list to a bstream and then memcmp them */
+		aim_bstream_t bs1, bs2;
 
-	cur = sess->ssi.items;
-	while (cur) {
-		if (cur->name)  free(cur->name);
-		if (cur->data)  aim_freetlvchain((aim_tlvlist_t **)&cur->data);
-		delitem = cur;
-		cur = cur->next;
-		free(delitem);
+		if (aim_sizetlvchain(&cur1->data) != aim_sizetlvchain(&cur2->data))
+			return 4;
+
+		aim_bstream_init(&bs1, ((fu8_t *)malloc(aim_sizetlvchain(&cur1->data)*sizeof(fu8_t))), aim_sizetlvchain(&cur1->data));
+		aim_bstream_init(&bs2, ((fu8_t *)malloc(aim_sizetlvchain(&cur2->data)*sizeof(fu8_t))), aim_sizetlvchain(&cur2->data));
+
+		aim_writetlvchain(&bs1, &cur1->data);
+		aim_writetlvchain(&bs2, &cur2->data);
+
+		if (memcmp(bs1.data, bs2.data, bs1.len)) {
+			free(bs1.data);
+			free(bs2.data);
+			return 4;
+		}
+
+		free(bs1.data);
+		free(bs2.data);
 	}
 
-	sess->ssi.items = NULL;
-	sess->ssi.revision = 0;
-	sess->ssi.timestamp = (time_t)0;
+	if (cur1->name && !cur2->name)
+		return 5;
+
+	if (!cur1->name && cur2->name)
+		return 6;
+
+	if (cur1->name && cur2->name && aim_sncmp(cur1->name, cur2->name))
+		return 7;
+
+	if (cur1->gid != cur2->gid)
+		return 8;
+
+	if (cur1->bid != cur2->bid)
+		return 9;
+
+	if (cur1->type != cur2->type)
+		return 10;
 
 	return 0;
 }
@@ -207,14 +302,23 @@
 						return cur;
 			}
 
-	} else if (sn) { /* For finding groups, permits, denies, and ignores */
-		for (cur=list; cur; cur=cur->next)
-			if ((cur->type == type) && (cur->name) && !(aim_sncmp(cur->name, sn)))
+	} else if (gn) { /* For finding groups */
+		for (cur=list; cur; cur=cur->next) {
+			if ((cur->type == type) && (cur->bid == 0x0000) && (cur->name) && !(aim_sncmp(cur->name, gn))) {
 				return cur;
+			}
+		}
+
+	} else if (sn) { /* For finding permits, denies, and ignores */
+		for (cur=list; cur; cur=cur->next) {
+			if ((cur->type == type) && (cur->gid == 0x0000) && (cur->name) && !(aim_sncmp(cur->name, sn))) {
+				return cur;
+			}
+		}
 
 	/* For stuff without names--permit deny setting, visibility mask, etc. */
 	} else for (cur=list; cur; cur=cur->next) {
-		if (cur->type == type)
+		if ((cur->type == type) && (!cur->name))
 			return cur;
 	}
 
@@ -222,23 +326,40 @@
 }
 
 /**
+ * Check if the given buddy exists in any group in the buddy list.
+ *
+ * @param list A pointer to the current list of items.
+ * @param sn The group name of the desired item.
+ * @return Return a pointer to the name of the item if found, else return NULL;
+ */
+faim_export struct aim_ssi_item *aim_ssi_itemlist_exists(struct aim_ssi_item *list, const char *sn)
+{
+	struct aim_ssi_item *cur;
+	if (!list || !sn)
+		return NULL;
+	for (cur=list; cur; cur=cur->next)
+		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->name) && (!aim_sncmp(cur->name, sn)))
+			return cur;
+	return NULL;
+}
+
+/**
  * Locally find the parent item of the given buddy name.
  *
  * @param list A pointer to the current list of items.
  * @param bn The buddy name of the desired item.
- * @return Return a pointer to the item if found, else return NULL;
+ * @return Return a pointer to the name of the item if found, else return NULL;
  */
-faim_export struct aim_ssi_item *aim_ssi_itemlist_findparent(struct aim_ssi_item *list, char *sn)
+faim_export char *aim_ssi_itemlist_findparentname(struct aim_ssi_item *list, const char *sn)
 {
 	struct aim_ssi_item *cur, *curg;
 	if (!list || !sn)
 		return NULL;
-	if (!(cur = aim_ssi_itemlist_finditem(list, NULL, sn, AIM_SSI_TYPE_BUDDY)))
+	if (!(cur = aim_ssi_itemlist_exists(list, sn)))
 		return NULL;
-	for (curg=list; curg; curg=curg->next)
-		if ((curg->type == AIM_SSI_TYPE_GROUP) && (curg->gid == cur->gid))
-			return curg;
-	return NULL;
+	if (!(curg = aim_ssi_itemlist_find(list, cur->gid, 0x0000)))
+		return NULL;
+	return curg->name;
 }
 
 /**
@@ -284,68 +405,183 @@
 }
 
 /**
- * Add the given packet to the holding queue.  We totally need to send SSI SNACs one at 
- * a time, so we have a local queue where packets get put before they are sent, and 
- * then we send stuff one at a time, nice and orderly-like.
+ * Locally find the alias of the given buddy.
+ *
+ * @param list A pointer to the current list of items.
+ * @return A pointer to a NULL terminated string that is the buddies 
+ *         alias, or NULL if the buddy has no alias.  You should free
+ *         this returned value!
+ */
+faim_export char *aim_ssi_getalias(struct aim_ssi_item *list, char *gn, char *sn)
+{
+	struct aim_ssi_item *cur = aim_ssi_itemlist_finditem(list, gn, sn, AIM_SSI_TYPE_BUDDY);
+	if (cur) {
+		aim_tlvlist_t *tlvlist = cur->data;
+		if (tlvlist) {
+			aim_tlv_t *tlv = aim_gettlv(tlvlist, 0x0131, 1);
+			if (tlv && tlv->length) {
+				char *alias = (char *)(char *)malloc((tlv->length+1)*sizeof(char));
+				strncpy(alias, tlv->value, tlv->length);
+				alias[tlv->length] = 0;
+				return alias;
+			}
+		}
+	}
+	return NULL;
+}
+
+/**
+ * If there are changes, then create temporary items and 
+ * call addmoddel.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
- * @param fr The newly created SNAC that you want to send.
  * @return Return 0 if no errors, otherwise return the error number.
  */
-static int aim_ssi_enqueue(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
+static int aim_ssi_sync(aim_session_t *sess, aim_conn_t *conn)
 {
-	aim_frame_t *cur;
+	struct aim_ssi_item *cur1, *cur2;
+	struct aim_ssi_tmp *cur, *new;
 
-	if (!sess || !conn || !fr)
+	if (!sess || !conn)
 		return -EINVAL;
 
-	fr->next = NULL;
-	if (sess->ssi.holding_queue == NULL) {
-		sess->ssi.holding_queue = fr;
-		if (!sess->ssi.waiting_for_ack)
-			aim_ssi_modbegin(sess, conn);
-	} else {
-		for (cur = sess->ssi.holding_queue; cur->next; cur = cur->next) ;
-		cur->next = fr;
+	/* If we're waiting for an ack, we shouldn't do anything else */
+	if (sess->ssi.waiting_for_ack)
+		return 0;
+
+	/*
+	 * Compare the 2 lists and create an aim_ssi_tmp for each difference.  
+	 * We should only send either additions, modifications, or deletions 
+	 * before waiting for an acknowledgement.  So first do deletions, then 
+	 * additions, then modifications.  Also, both the official and the local 
+	 * list should be in ascending numerical order for the group ID#s and the 
+	 * buddy ID#s, which makes things more efficient.  I think.
+	 */
+
+	/* Deletions */
+	if (!sess->ssi.pending) {
+		for (cur1=sess->ssi.official; cur1; cur1=cur1->next) {
+			if (!aim_ssi_itemlist_find(sess->ssi.local, cur1->gid, cur1->bid)) {
+				new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
+				new->action = AIM_CB_SSI_DEL;
+				new->ack = 0xffff;
+				new->name = NULL;
+				new->item = cur1;
+				new->next = NULL;
+				if (sess->ssi.pending) {
+					for (cur=sess->ssi.pending; cur->next; cur=cur->next);
+					cur->next = new;
+				} else
+					sess->ssi.pending = new;
+			}
+		}
 	}
 
+	/* Additions */
+	if (!sess->ssi.pending) {
+		for (cur1=sess->ssi.local; cur1; cur1=cur1->next) {
+			if (!aim_ssi_itemlist_find(sess->ssi.official, cur1->gid, cur1->bid)) {
+				new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
+				new->action = AIM_CB_SSI_ADD;
+				new->ack = 0xffff;
+				new->name = NULL;
+				new->item = cur1;
+				new->next = NULL;
+				if (sess->ssi.pending) {
+					for (cur=sess->ssi.pending; cur->next; cur=cur->next);
+					cur->next = new;
+				} else
+					sess->ssi.pending = new;
+			}
+		}
+	}
+
+	/* Modifications */
+	if (!sess->ssi.pending) {
+		for (cur1=sess->ssi.local; cur1; cur1=cur1->next) {
+			cur2 = aim_ssi_itemlist_find(sess->ssi.official, cur1->gid, cur1->bid);
+			if (cur2 && (aim_ssi_itemlist_cmp(cur1, cur2))) {
+				new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
+				new->action = AIM_CB_SSI_MOD;
+				new->ack = 0xffff;
+				new->name = NULL;
+				new->item = cur1;
+				new->next = NULL;
+				if (sess->ssi.pending) {
+					for (cur=sess->ssi.pending; cur->next; cur=cur->next);
+					cur->next = new;
+				} else
+					sess->ssi.pending = new;
+			}
+		}
+	}
+
+	/* We're out of stuff to do, so tell the AIM servers we're done and exit */
+	if (!sess->ssi.pending) {
+		aim_ssi_modend(sess, conn);
+		return 0;
+	}
+
+	/* Make sure we don't send anything else between now 
+	 * and when we receive the ack for the following operation */
+	sess->ssi.waiting_for_ack = 1;
+
+	/* Now go mail off our data and wait 4 to 6 weeks */
+	aim_ssi_addmoddel(sess, conn);
+
 	return 0;
 }
 
 /**
- * Send the next SNAC from the holding queue.  This is called 
- * automatically when an ack from an add, mod, or del is received.  
- * If the queue is empty, it sends the modend SNAC.
+ * Free all SSI data.
+ *
+ * This doesn't remove it from the server, that's different.
  *
  * @param sess The oscar session.
- * @param conn The bos connection for this session.
  * @return Return 0 if no errors, otherwise return the error number.
  */
-static int aim_ssi_dispatch(aim_session_t *sess, aim_conn_t *conn)
+static int aim_ssi_freelist(aim_session_t *sess)
 {
-	aim_frame_t *cur;
+	struct aim_ssi_item *cur, *del;
+	struct aim_ssi_tmp *curtmp, *deltmp;
 
-	if (!sess || !conn)
-		return -EINVAL;
+	cur = sess->ssi.official;
+	while (cur) {
+		del = cur;
+		cur = cur->next;
+		free(del->name);
+		aim_freetlvchain(&del->data);
+		free(del);
+	}
 
-	if (!sess->ssi.waiting_for_ack) {
-		if (sess->ssi.holding_queue) {
-			sess->ssi.waiting_for_ack = 1;
-			cur = sess->ssi.holding_queue->next;
-			sess->ssi.holding_queue->next = NULL;
-			aim_tx_enqueue(sess, sess->ssi.holding_queue);
-			sess->ssi.holding_queue = cur;
-		} else
-			aim_ssi_modend(sess, conn);
+	cur = sess->ssi.local;
+	while (cur) {
+		del = cur;
+		cur = cur->next;
+		free(del->name);
+		aim_freetlvchain(&del->data);
+		free(del);
 	}
 
+	curtmp = sess->ssi.pending;
+	while (curtmp) {
+		deltmp = curtmp;
+		curtmp = curtmp->next;
+		free(deltmp);
+	}
+
+	sess->ssi.numitems = 0;
+	sess->ssi.official = NULL;
+	sess->ssi.local = NULL;
+	sess->ssi.pending = NULL;
+	sess->ssi.timestamp = (time_t)0;
+
 	return 0;
 }
 
 /**
- * Send SNACs necessary to remove all SSI data from the server list, 
- * and then free the local copy as well.
+ * Delete all SSI data.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
@@ -353,35 +589,29 @@
  */
 faim_export int aim_ssi_deletelist(aim_session_t *sess, aim_conn_t *conn)
 {
-	int num;
-	struct aim_ssi_item *cur, **items;
-
-	for (cur=sess->ssi.items, num=0; cur; cur=cur->next)
-		num++;
+	struct aim_ssi_item *cur, *del;
 
-	if (!(items = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *))))
-		return -ENOMEM;
-	memset(items, 0, num*sizeof(struct aim_ssi_item *));
-	for (cur=sess->ssi.items, num=0; cur; cur=cur->next) {
-		items[num] = cur;
-		num++;
+	/* Free the local list */
+	cur = sess->ssi.local;
+	while (cur) {
+		del = cur;
+		cur = cur->next;
+		free(del->name);
+		aim_freetlvchain(&del->data);
+		free(del);
 	}
+	sess->ssi.local = NULL;
 
-	aim_ssi_addmoddel(sess, conn, items, num, AIM_CB_SSI_DEL);
-	free(items);
-	aim_ssi_dispatch(sess, conn);
-	aim_ssi_freelist(sess);
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
 
 /**
- * This "cleans" the ssi list.  It does a few things, with the intent of making 
- * sure there ain't nothin' wrong with your SSI.
- *   -Make sure all buddies are in a group, and all groups have the correct 
- *     additional data.
- *   -Make sure there are no empty groups in the list.  While there is nothing 
- *     wrong empty groups in the SSI, it's wiser to not have them.
+ * This "cleans" the ssi list.  It does the following:
+ * 1) Makes sure that all buddies are in a group.
+ * 2) Makes sure there are no empty groups
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
@@ -389,292 +619,231 @@
  */
 faim_export int aim_ssi_cleanlist(aim_session_t *sess, aim_conn_t *conn)
 {
-	unsigned int i;
-	struct aim_ssi_item *cur, *parentgroup;
-
-	/* Make sure we actually need to clean out the list */
-	for (cur=sess->ssi.items, i=0; cur && !i; cur=cur->next)
-		/* Any buddies directly in the master group */
-		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000))
-			i++;
-	if (!i)
-		return 0;
-
-	/* Remove all the additional data from all groups */
-	for (cur=sess->ssi.items; cur; cur=cur->next)
-		if ((cur->data) && (cur->type == AIM_SSI_TYPE_GROUP)) {
-			aim_freetlvchain((aim_tlvlist_t **)&cur->data);
-			cur->data = NULL;
-		}
-
-	/* If there are buddies directly in the master group, make sure  */
-	/* there is a group to put them in.  Any group, any group at all. */
-	for (cur=sess->ssi.items; ((cur) && ((cur->type != AIM_SSI_TYPE_BUDDY) || (cur->gid != 0x0000))); cur=cur->next);
-	if (!cur) {
-		for (parentgroup=sess->ssi.items; ((parentgroup) && (parentgroup->type!=AIM_SSI_TYPE_GROUP) && (parentgroup->gid==0x0000)); parentgroup=parentgroup->next);
-		if (!parentgroup) {
-			char *newgroup;
-			newgroup = (char*)malloc(strlen("Unknown")*sizeof(char));
-			strcpy(newgroup, "Unknown");
-			aim_ssi_addgroups(sess, conn, (const char**)&newgroup, 1);
-		}
-	}
-
-	/* Set parentgroup equal to any arbitray group */
-	for (parentgroup=sess->ssi.items; parentgroup->gid==0x0000 || parentgroup->type!=AIM_SSI_TYPE_GROUP; parentgroup=parentgroup->next);
+	struct aim_ssi_item *cur;
 
 	/* If there are any buddies directly in the master group, put them in a real group */
-	for (cur=sess->ssi.items; cur; cur=cur->next)
-		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000)) {
-			aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_DEL);
-			cur->gid = parentgroup->gid;
-			aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
-		}
+	/* This will kind of mess up if you hit the item limit, but this function isn't too critical */
+	for (cur=sess->ssi.local; cur; cur=cur->next)
+		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000))
+			aim_ssi_addbuddy(sess, conn, cur->name, "orphans", NULL, NULL, NULL, 0);
+
+	/* Now DESTROY any buddies that are directly in the master group */
+	for (cur=sess->ssi.local; cur; cur=cur->next)
+		if ((cur->type == AIM_SSI_TYPE_BUDDY) && (cur->gid == 0x0000))
+			aim_ssi_delbuddy(sess, conn, cur->name, NULL);
+
+	/* Check if there are empty groups */
+	for (cur=sess->ssi.local; cur; cur=cur->next)
+		if ((cur->type == AIM_SSI_TYPE_GROUP) && (!cur->data))
+			aim_ssi_itemlist_del(&sess->ssi.local, cur);
+
+	/* Check if the master group is empty */
+	if ((cur = aim_ssi_itemlist_find(sess->ssi.local, 0x0000, 0x0000)) && (!cur->data))
+		aim_ssi_itemlist_del(&sess->ssi.local, cur);
+
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
+
+	return 0;
+}
 
-	/* Rebuild additional data for all groups */
-	for (parentgroup=sess->ssi.items; parentgroup; parentgroup=parentgroup->next)
-		if (parentgroup->type == AIM_SSI_TYPE_GROUP)
-			aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);
+/**
+ * Add a buddy to the list.
+ *
+ * @param sess The oscar session.
+ * @param conn The bos connection for this session.
+ * @param name The name of the item.
+ * @param group The group of the item.
+ * @param alias The alias/nickname of the item, or NULL.
+ * @param comment The buddy comment for the item, or NULL.
+ * @param smsnum The locally assigned SMS number, or NULL.
+ * @return Return 0 if no errors, otherwise return the error number.
+ */
+faim_export int aim_ssi_addbuddy(aim_session_t *sess, aim_conn_t *conn, const char *name, const char *group, const char *alias, const char *comment, const char *smsnum, int needauth)
+{
+	struct aim_ssi_item *parent;
+	aim_tlvlist_t *data = NULL;
 
-	/* Send a mod snac for all groups */
-	i = 0;
-	for (cur=sess->ssi.items; cur; cur=cur->next)
-		if (cur->type == AIM_SSI_TYPE_GROUP)
-			i++;
-	if (i > 0) {
-		/* Allocate an array of pointers to each of the groups */
-		struct aim_ssi_item **groups;
-		if (!(groups = (struct aim_ssi_item **)malloc(i*sizeof(struct aim_ssi_item *))))
+	if (!sess || !conn || !name || !group)
+		return -EINVAL;
+
+	/* Find the parent */
+	if (!(parent = aim_ssi_itemlist_finditem(sess->ssi.local, group, NULL, AIM_SSI_TYPE_GROUP))) {
+		/* Find the parent's parent (the master group) */
+		if (!(parent = aim_ssi_itemlist_find(sess->ssi.local, 0x0000, 0x0000)))
+			if (!(parent = aim_ssi_itemlist_add(&sess->ssi.local, NULL, 0x0000, 0x0000, AIM_SSI_TYPE_GROUP, NULL)))
+				return -ENOMEM;
+		/* Add the parent */
+		if (!(parent = aim_ssi_itemlist_add(&sess->ssi.local, group, 0xFFFF, 0x0000, AIM_SSI_TYPE_GROUP, NULL)))
 			return -ENOMEM;
 
-		for (cur=sess->ssi.items, i=0; cur; cur=cur->next)
-			if (cur->type == AIM_SSI_TYPE_GROUP)
-				groups[i] = cur;
-
-		aim_ssi_addmoddel(sess, conn, groups, i, AIM_CB_SSI_MOD);
-		free(groups);
+		/* Modify the parent's parent (the master group) */
+		aim_ssi_itemlist_rebuildgroup(sess->ssi.local, NULL);
 	}
 
-	/* Send a del snac for any empty groups */
-	i = 0;
-	for (cur=sess->ssi.items; cur; cur=cur->next)
-		if ((cur->type == AIM_SSI_TYPE_GROUP) && !(cur->data))
-			i++;
-	if (i > 0) {
-		/* Allocate an array of pointers to each of the groups */
-		struct aim_ssi_item **groups;
-		if (!(groups = (struct aim_ssi_item **)malloc(i*sizeof(struct aim_ssi_item *))))
-			return -ENOMEM;
+	/* Create a TLV list for the new buddy */
+	if (needauth)
+		aim_addtlvtochain_noval(&data, 0x0066);
+	if (alias)
+		aim_addtlvtochain_raw(&data, 0x0131, strlen(alias), alias);
+	if (smsnum)
+		aim_addtlvtochain_raw(&data, 0x013a, strlen(smsnum), smsnum);
+	if (comment)
+		aim_addtlvtochain_raw(&data, 0x013c, strlen(comment), comment);
+
+	/* Add that bad boy */
+	aim_ssi_itemlist_add(&sess->ssi.local, name, parent->gid, 0xFFFF, AIM_SSI_TYPE_BUDDY, data);
+
+	/* Modify the parent group */
+	aim_ssi_itemlist_rebuildgroup(sess->ssi.local, group);
+
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
-		for (cur=sess->ssi.items, i=0; cur; cur=cur->next)
-			if ((cur->type == AIM_SSI_TYPE_GROUP) && !(cur->data))
-				groups[i] = cur;
+	return 0;
+}
 
-		aim_ssi_addmoddel(sess, conn, groups, i, AIM_CB_SSI_DEL);
-		free(groups);
-	}
+/**
+ * Add a permit buddy to the list.
+ *
+ * @param sess The oscar session.
+ * @param conn The bos connection for this session.
+ * @param name The name of the item..
+ * @return Return 0 if no errors, otherwise return the error number.
+ */
+faim_export int aim_ssi_addpermit(aim_session_t *sess, aim_conn_t *conn, const char *name)
+{
+	if (!sess || !conn || !name)
+		return -EINVAL;
 
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
+	/* Add that bad boy */
+	aim_ssi_itemlist_add(&sess->ssi.local, name, 0x0000, 0xFFFF, AIM_SSI_TYPE_PERMIT, NULL);
+
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
 
 /**
- * Add an array of screen names to the given group.
+ * Add a deny buddy to the list.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
- * @param gn The name of the group to which you want to add these names.
- * @param sn An array of null terminated strings of the names you want to add.
- * @param num The number of screen names you are adding (size of the sn array).
+ * @param name The name of the item..
  * @return Return 0 if no errors, otherwise return the error number.
  */
-faim_export int aim_ssi_addbuddies(aim_session_t *sess, aim_conn_t *conn, const char *gn, const char **sn, unsigned int num)
+faim_export int aim_ssi_adddeny(aim_session_t *sess, aim_conn_t *conn, const char *name)
 {
-	struct aim_ssi_item *parentgroup, **newitems;
-	fu16_t i;
-
-	if (!sess || !conn || !gn || !sn || !num)
+	if (!sess || !conn || !name)
 		return -EINVAL;
 
-	/* Look up the parent group */
-	if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP))) {
-		aim_ssi_addgroups(sess, conn, (const char **)&gn, 1);
-		if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
-			return -ENOMEM;
-	}
-
-	/* Allocate an array of pointers to each of the new items */
-	if (!(newitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *))))
-		return -ENOMEM;
-
-	/* Add items to the local list, and index them in the array */
-	for (i=0; i<num; i++)
-		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, sn[i], AIM_SSI_TYPE_BUDDY))) {
-			free(newitems);
-			return -ENOMEM;
-		}
-
-	/* Send the add item SNAC */
-	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
-		free(newitems);
-		return -i;
-	}
-
-	/* Free the array of pointers to each of the new items */
-	free(newitems);
-
-	/* Rebuild the additional data in the parent group */
-	if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
-		return i;
-
-	/* Send the mod item SNAC */
-	if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD)))
-		return i;
+	/* Add that bad boy */
+	aim_ssi_itemlist_add(&sess->ssi.local, name, 0x0000, 0xFFFF, AIM_SSI_TYPE_DENY, NULL);
 
-	/* Begin sending SSI SNACs */
-	if (!(i = aim_ssi_dispatch(sess, conn)))
-		return i;
-
-	return 0;
-}
-
-/**
- * Add the master group (the group containing all groups).  This is called by 
- * aim_ssi_addgroups, if necessary.
- *
- * @param sess The oscar session.
- * @param conn The bos connection for this session.
- * @return Return 0 if no errors, otherwise return the error number.
- */
-faim_export int aim_ssi_addmastergroup(aim_session_t *sess, aim_conn_t *conn)
-{
-	struct aim_ssi_item *newitem;
-
-	if (!sess || !conn)
-		return -EINVAL;
-
-	/* Add the item to the local list, and keep a pointer to it */
-	if (!(newitem = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_GROUP)))
-		return -ENOMEM;
-
-	/* If there are any existing groups (technically there shouldn't be, but */
-	/* just in case) then add their group ID#'s to the additional data */
-	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, newitem);
-
-	/* Send the add item SNAC */
-	aim_ssi_addmoddel(sess, conn, &newitem, 1, AIM_CB_SSI_ADD);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
 
 /**
- * Add an array of groups to the list.
+ * Deletes a buddy from the list.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
- * @param gn An array of null terminated strings of the names you want to add.
- * @param num The number of groups names you are adding (size of the sn array).
+ * @param name The name of the item, or NULL.
+ * @param group The group of the item, or NULL.
  * @return Return 0 if no errors, otherwise return the error number.
  */
-faim_export int aim_ssi_addgroups(aim_session_t *sess, aim_conn_t *conn, const char **gn, unsigned int num)
+faim_export int aim_ssi_delbuddy(aim_session_t *sess, aim_conn_t *conn, const char *name, const char *group)
 {
-	struct aim_ssi_item *parentgroup, **newitems;
-	fu16_t i;
+	struct aim_ssi_item *del;
 
-	if (!sess || !conn || !gn || !num)
+	if (!sess || !conn || !name || !group)
+		return -EINVAL;
+
+	/* Find the buddy */
+	if (!(del = aim_ssi_itemlist_finditem(sess->ssi.local, group, name, AIM_SSI_TYPE_BUDDY)))
 		return -EINVAL;
 
-	/* Look up the parent group */
-	if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0))) {
-		aim_ssi_addmastergroup(sess, conn);
-		if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
-			return -ENOMEM;
+	/* Remove the item from the list */
+	aim_ssi_itemlist_del(&sess->ssi.local, del);
+
+	/* Modify the parent group */
+	aim_ssi_itemlist_rebuildgroup(sess->ssi.local, group);
+
+	/* Check if we should delete the parent group */
+	if ((del = aim_ssi_itemlist_finditem(sess->ssi.local, group, NULL, AIM_SSI_TYPE_GROUP)) && (!del->data)) {
+		aim_ssi_itemlist_del(&sess->ssi.local, del);
+
+		/* Modify the parent group */
+		aim_ssi_itemlist_rebuildgroup(sess->ssi.local, NULL);
+
+		/* Check if we should delete the parent's parent (the master group) */
+		if ((del = aim_ssi_itemlist_find(sess->ssi.local, 0x0000, 0x0000)) && (!del->data)) {
+			aim_ssi_itemlist_del(&sess->ssi.local, del);
+		}
 	}
 
-	/* Allocate an array of pointers to each of the new items */
-	if (!(newitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *))))
-		return -ENOMEM;
-
-	/* Add items to the local list, and index them in the array */
-	for (i=0; i<num; i++)
-		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, parentgroup, gn[i], AIM_SSI_TYPE_GROUP))) {
-			free(newitems);
-			return -ENOMEM;
-		}
-
-	/* Send the add item SNAC */
-	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
-		free(newitems);
-		return -i;
-	}
-
-	/* Free the array of pointers to each of the new items */
-	free(newitems);
-
-	/* Rebuild the additional data in the parent group */
-	if ((i = aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup)))
-		return i;
-
-	/* Send the mod item SNAC */
-	if ((i = aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD)))
-		return i;
-
-	/* Begin sending SSI SNACs */
-	if (!(i = aim_ssi_dispatch(sess, conn)))
-		return i;
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
 
 /**
- * Add an array of a certain type of item to the list.  This can be used for 
- * permit buddies, deny buddies, ICQ's ignore buddies, and probably other 
- * types, also.
+ * Deletes a permit buddy from the list.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
- * @param sn An array of null terminated strings of the names you want to add.
- * @param num The number of groups names you are adding (size of the sn array).
- * @param type The type of item you want to add.  See the AIM_SSI_TYPE_BLEH 
- *        #defines in aim.h.
+ * @param name The name of the item, or NULL.
  * @return Return 0 if no errors, otherwise return the error number.
  */
-faim_export int aim_ssi_addpord(aim_session_t *sess, aim_conn_t *conn, const char **sn, unsigned int num, fu16_t type)
+faim_export int aim_ssi_delpermit(aim_session_t *sess, aim_conn_t *conn, const char *name)
 {
-	struct aim_ssi_item **newitems;
-	fu16_t i;
+	struct aim_ssi_item *del;
 
-	if (!sess || !conn || !sn || !num)
+	if (!sess || !conn || !name)
+		return -EINVAL;
+
+	/* Find the item */
+	if (!(del = aim_ssi_itemlist_finditem(sess->ssi.local, NULL, name, AIM_SSI_TYPE_PERMIT)))
 		return -EINVAL;
 
-	/* Allocate an array of pointers to each of the new items */
-	if (!(newitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *))))
-		return -ENOMEM;
+	/* Remove the item from the list */
+	aim_ssi_itemlist_del(&sess->ssi.local, del);
 
-	/* Add items to the local list, and index them in the array */
-	for (i=0; i<num; i++)
-		if (!(newitems[i] = aim_ssi_itemlist_add(&sess->ssi.items, NULL, sn[i], type))) {
-			free(newitems);
-			return -ENOMEM;
-		}
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
+
+	return 0;
+}
 
-	/* Send the add item SNAC */
-	if ((i = aim_ssi_addmoddel(sess, conn, newitems, num, AIM_CB_SSI_ADD))) {
-		free(newitems);
-		return -i;
-	}
+/**
+ * Deletes a deny buddy from the list.
+ *
+ * @param sess The oscar session.
+ * @param conn The bos connection for this session.
+ * @param name The name of the item, or NULL.
+ * @return Return 0 if no errors, otherwise return the error number.
+ */
+faim_export int aim_ssi_deldeny(aim_session_t *sess, aim_conn_t *conn, const char *name)
+{
+	struct aim_ssi_item *del;
 
-	/* Free the array of pointers to each of the new items */
-	free(newitems);
+	if (!sess || !conn || !name)
+		return -EINVAL;
 
-	/* Begin sending SSI SNACs */
-	if (!(i = aim_ssi_dispatch(sess, conn)))
-		return i;
+	/* Find the item */
+	if (!(del = aim_ssi_itemlist_finditem(sess->ssi.local, NULL, name, AIM_SSI_TYPE_PERMIT)))
+		return -EINVAL;
+
+	/* Remove the item from the list */
+	aim_ssi_itemlist_del(&sess->ssi.local, del);
+
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
@@ -692,73 +861,13 @@
  */
 faim_export int aim_ssi_movebuddy(aim_session_t *sess, aim_conn_t *conn, const char *oldgn, const char *newgn, const char *sn)
 {
-	struct aim_ssi_item **groups, *buddy, *cur;
-	fu16_t i;
-
-	if (!sess || !conn || !oldgn || !newgn || !sn)
-		return -EINVAL;
-
-	/* Look up the buddy */
-	if (!(buddy = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn, AIM_SSI_TYPE_BUDDY)))
-		return -ENOMEM;
-
-	/* Allocate an array of pointers to the two groups */
-	if (!(groups = (struct aim_ssi_item **)malloc(2*sizeof(struct aim_ssi_item *))))
-		return -ENOMEM;
-
-	/* Look up the old parent group */
-	if (!(groups[0] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, oldgn, AIM_SSI_TYPE_GROUP))) {
-		free(groups);
-		return -ENOMEM;
-	}
-
-	/* Look up the new parent group */
-	if (!(groups[1] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, newgn, AIM_SSI_TYPE_GROUP))) {
-		aim_ssi_addgroups(sess, conn, (const char**)&newgn, 1);
-		if (!(groups[1] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, newgn, AIM_SSI_TYPE_GROUP))) {
-			free(groups);
-			return -ENOMEM;
-		}
-	}
-
-	/* Send the delete item SNAC */
-	aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_DEL);
-
-	/* Put the buddy in the new group */
-	buddy->gid = groups[1]->gid;
-
-	/* Assign a new buddy ID#, because the new group might already have a buddy with this ID# */
-	buddy->bid = 0;
-	do {
-		buddy->bid += 0x0001;
-		for (cur=sess->ssi.items, i=0; ((cur) && (!i)); cur=cur->next)
-			if ((cur->bid == buddy->bid) && (cur->gid == buddy->gid) && (cur->type == AIM_SSI_TYPE_BUDDY) && (cur->name) && aim_sncmp(cur->name, buddy->name))
-				i=1;
-	} while (i);
-
-	/* Rebuild the additional data in the two parent groups */
-	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[0]);
-	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, groups[1]);
-
-	/* Send the add item SNAC */
-	aim_ssi_addmoddel(sess, conn, &buddy, 1, AIM_CB_SSI_ADD);
-
-	/* Send the mod item SNAC */
-	aim_ssi_addmoddel(sess, conn, groups, 2, AIM_CB_SSI_MOD);
-
-	/* Free the temporary array */
-	free(groups);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
-
+	aim_ssi_delbuddy(sess, conn, sn, oldgn);
+	aim_ssi_addbuddy(sess, conn, sn, newgn, NULL, NULL, NULL, 0);
 	return 0;
 }
 
 /**
- * Rename a group.  I really like how this is done.  It turns me on.
- *
- * Did I say that out loud?...
+ * Rename a group.
  *
  * @param sess The oscar session.
  * @param conn The bos connection for this session.
@@ -773,268 +882,15 @@
 	if (!sess || !conn || !oldgn || !newgn)
 		return -EINVAL;
 
-	/* Look up the group */
-	if (!(group = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, oldgn, AIM_SSI_TYPE_GROUP)))
-		return -ENOMEM;
-
-	/* Free the old group name and copy the new one in its place. */
-	if (group->name)
-		free(group->name);
-	if (!(group->name = (char *)malloc((strlen(newgn)+1)*sizeof(char)))) {
-		group->name = NULL;
-		return -ENOMEM;
-	}
-	strcpy(group->name, newgn);
-
-	/* Send the mod item SNAC */
-	aim_ssi_addmoddel(sess, conn, &group, 1, AIM_CB_SSI_MOD);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
-
-	return 0;
-}
-
-/**
- * Delete an array of screen names from the given group.
- *
- * @param sess The oscar session.
- * @param conn The bos connection for this session.
- * @param gn The name of the group from which you want to delete these names.
- * @param sn An array of null terminated strings of the names you want to delete.
- * @param num The number of screen names you are deleting (size of the sn array).
- * @return Return 0 if no errors, otherwise return the error number.
- */
-faim_export int aim_ssi_delbuddies(aim_session_t *sess, aim_conn_t *conn, const char *gn, char **sn, unsigned int num)
-{
-	struct aim_ssi_item *cur, *parentgroup, **delitems;
-	int i;
-
-	if (!sess || !conn || !gn || !sn || !num)
-		return -EINVAL;
-
-	/* Look up the parent group */
-	if (!(parentgroup = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn, AIM_SSI_TYPE_GROUP)))
-		return -EINVAL;
-
-	/* Allocate an array of pointers to each of the items to be deleted */
-	delitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *));
-	memset(delitems, 0, num*sizeof(struct aim_ssi_item *));
-
-	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
-	for (i=0; i<num; i++) {
-		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], AIM_SSI_TYPE_BUDDY))) {
-			free(delitems);
-			return -EINVAL;
-		}
-
-		/* Remove the delitems from the item list */
-		if (sess->ssi.items == delitems[i]) {
-			sess->ssi.items = sess->ssi.items->next;
-		} else {
-			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
-			if (cur->next)
-				cur->next = cur->next->next;
-		}
-	}
-
-	/* Send the del item SNAC */
-	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
-
-	/* Free the items */
-	for (i=0; i<num; i++) {
-		if (delitems[i]->name)
-			free(delitems[i]->name);
-		if (delitems[i]->data)
-			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
-		free(delitems[i]);
-	}
-	free(delitems);
-
-	/* Rebuild the additional data in the parent group */
-	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);
-
-	/* Send the mod item SNAC */
-	aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);
-
-	/* Delete the group, but only if it's empty */
-	if (!parentgroup->data)
-		aim_ssi_delgroups(sess, conn, &parentgroup->name, 1);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
-
-	return 0;
-}
-
-/**
- * Delete the master group from the item list.  There can be only one.
- * Er, so just find the one master group and delete it.
- *
- * @param sess The oscar session.
- * @param conn The bos connection for this session.
- * @return Return 0 if no errors, otherwise return the error number.
- */
-faim_export int aim_ssi_delmastergroup(aim_session_t *sess, aim_conn_t *conn)
-{
-	struct aim_ssi_item *cur, *delitem;
-
-	if (!sess || !conn)
-		return -EINVAL;
-
-	/* Make delitem a pointer to the aim_ssi_item to be deleted */
-	if (!(delitem = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
+	if (!(group = aim_ssi_itemlist_finditem(sess->ssi.local, oldgn, NULL, AIM_SSI_TYPE_GROUP)))
 		return -EINVAL;
 
-	/* Remove delitem from the item list */
-	if (sess->ssi.items == delitem) {
-		sess->ssi.items = sess->ssi.items->next;
-	} else {
-		for (cur=sess->ssi.items; (cur->next && (cur->next!=delitem)); cur=cur->next);
-		if (cur->next)
-			cur->next = cur->next->next;
-	}
-
-	/* Send the del item SNAC */
-	aim_ssi_addmoddel(sess, conn, &delitem, 1, AIM_CB_SSI_DEL);
-
-	/* Free the item */
-	if (delitem->name)
-		free(delitem->name);
-	if (delitem->data)
-		aim_freetlvchain((aim_tlvlist_t **)&delitem->data);
-	free(delitem);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
-
-	return 0;
-}
-
-/**
- * Delete an array of groups.
- *
- * @param sess The oscar session.
- * @param conn The bos connection for this session.
- * @param gn An array of null terminated strings of the groups you want to delete.
- * @param num The number of groups you are deleting (size of the gn array).
- * @return Return 0 if no errors, otherwise return the error number.
- */
-faim_export int aim_ssi_delgroups(aim_session_t *sess, aim_conn_t *conn, char **gn, unsigned int num) {
-	struct aim_ssi_item *cur, *parentgroup, **delitems;
-	int i;
-
-	if (!sess || !conn || !gn || !num)
-		return -EINVAL;
-
-	/* Look up the parent group */
-	if (!(parentgroup = aim_ssi_itemlist_find(sess->ssi.items, 0, 0)))
-		return -EINVAL;
-
-	/* Allocate an array of pointers to each of the items to be deleted */
-	delitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *));
-	memset(delitems, 0, num*sizeof(struct aim_ssi_item *));
-
-	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
-	for (i=0; i<num; i++) {
-		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, gn[i], AIM_SSI_TYPE_GROUP))) {
-			free(delitems);
-			return -EINVAL;
-		}
-
-		/* Remove the delitems from the item list */
-		if (sess->ssi.items == delitems[i]) {
-			sess->ssi.items = sess->ssi.items->next;
-		} else {
-			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
-			if (cur->next)
-				cur->next = cur->next->next;
-		}
-	}
-
-	/* Send the del item SNAC */
-	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
+	free(group->name);
+	group->name = (char *)malloc((strlen(newgn)+1)*sizeof(char));
+	strcpy(group->name, newgn);
 
-	/* Free the items */
-	for (i=0; i<num; i++) {
-		if (delitems[i]->name)
-			free(delitems[i]->name);
-		if (delitems[i]->data)
-			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
-		free(delitems[i]);
-	}
-	free(delitems);
-
-	/* Rebuild the additional data in the parent group */
-	aim_ssi_itemlist_rebuildgroup(&sess->ssi.items, parentgroup);
-
-	/* Send the mod item SNAC */
-	aim_ssi_addmoddel(sess, conn, &parentgroup, 1, AIM_CB_SSI_MOD);
-
-	/* Delete the group, but only if it's empty */
-	if (!parentgroup->data)
-		aim_ssi_delmastergroup(sess, conn);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
-
-	return 0;
-}
-
-/**
- * Delete an array of a certain type of item from the list.  This can be 
- * used for permit buddies, deny buddies, ICQ's ignore buddies, and 
- * probably other types, also.
- *
- * @param sess The oscar session.
- * @param conn The bos connection for this session.
- * @param sn An array of null terminated strings of the items you want to delete.
- * @param num The number of items you are deleting (size of the sn array).
- * @return Return 0 if no errors, otherwise return the error number.
- */
-faim_export int aim_ssi_delpord(aim_session_t *sess, aim_conn_t *conn, const char **sn, unsigned int num, fu16_t type) {
-	struct aim_ssi_item *cur, **delitems;
-	int i;
-
-	if (!sess || !conn || !sn || !num || (type!=AIM_SSI_TYPE_PERMIT && type!=AIM_SSI_TYPE_DENY))
-		return -EINVAL;
-
-	/* Allocate an array of pointers to each of the items to be deleted */
-	delitems = (struct aim_ssi_item **)malloc(num*sizeof(struct aim_ssi_item *));
-	memset(delitems, 0, num*sizeof(struct aim_ssi_item *));
-
-	/* Make the delitems array a pointer to the aim_ssi_item structs to be deleted */
-	for (i=0; i<num; i++) {
-		if (!(delitems[i] = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, sn[i], type))) {
-			free(delitems);
-			return -EINVAL;
-		}
-
-		/* Remove the delitems from the item list */
-		if (sess->ssi.items == delitems[i]) {
-			sess->ssi.items = sess->ssi.items->next;
-		} else {
-			for (cur=sess->ssi.items; (cur->next && (cur->next!=delitems[i])); cur=cur->next);
-			if (cur->next)
-				cur->next = cur->next->next;
-		}
-	}
-
-	/* Send the del item SNAC */
-	aim_ssi_addmoddel(sess, conn, delitems, num, AIM_CB_SSI_DEL);
-
-	/* Free the items */
-	for (i=0; i<num; i++) {
-		if (delitems[i]->name)
-			free(delitems[i]->name);
-		if (delitems[i]->data)
-			aim_freetlvchain((aim_tlvlist_t **)&delitems[i]->data);
-		free(delitems[i]);
-	}
-	free(delitems);
-
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
@@ -1054,57 +910,29 @@
  *        visible.  See the AIM_FLAG_BLEH #defines in aim.h
  * @return Return 0 if no errors, otherwise return the error number.
  */
-faim_export int aim_ssi_setpermdeny(aim_session_t *sess, aim_conn_t *conn, fu8_t permdeny, fu32_t vismask) {
-	struct aim_ssi_item *cur;
-	aim_tlv_t *tlv;
+faim_export int aim_ssi_setpermdeny(aim_session_t *sess, aim_conn_t *conn, fu8_t permdeny, fu32_t vismask)
+{
+	struct aim_ssi_item *tmp;
+	aim_tlvlist_t *data = NULL;
 
 	if (!sess || !conn)
 		return -EINVAL;
 
-	/* Look up the permit/deny settings item */
-	cur = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PDINFO);
+	/* Need to add the x00ca TLV to the TLV chain */
+	aim_addtlvtochain8(&data, 0x00ca, permdeny);
 
-	if (cur) {
-		/* The permit/deny item exists */
-		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00ca, 1))) {
-			/* Just change the value of the x00ca TLV */
-			if (tlv->length != 1) {
-				tlv->length = 1;
-				free(tlv->value);
-				tlv->value = (fu8_t *)malloc(sizeof(fu8_t));
-			}
-			tlv->value[0] = permdeny;
-		} else {
-			/* Need to add the x00ca TLV to the TLV chain */
-			aim_addtlvtochain8((aim_tlvlist_t**)&cur->data, 0x00ca, permdeny);
-		}
+	/* Need to add the x00cb TLV to the TLV chain */
+	aim_addtlvtochain32(&data, 0x00cb, vismask);
 
-		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00cb, 1))) {
-			/* Just change the value of the x00cb TLV */
-			if (tlv->length != 4) {
-				tlv->length = 4;
-				free(tlv->value);
-				tlv->value = (fu8_t *)malloc(4*sizeof(fu8_t));
-			}
-			aimutil_put32(tlv->value, vismask);
-		} else {
-			/* Need to add the x00cb TLV to the TLV chain */
-			aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00cb, vismask);
-		}
-
-		/* Send the mod item SNAC */
-		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_MOD);
+	if ((tmp = aim_ssi_itemlist_finditem(sess->ssi.local, NULL, NULL, AIM_SSI_TYPE_PDINFO))) {
+		aim_freetlvchain(&tmp->data);
+		tmp->data = data;
 	} else {
-		/* Need to add the permit/deny item */
-		if (!(cur = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PDINFO)))
-			return -ENOMEM;
-		aim_addtlvtochain8((aim_tlvlist_t**)&cur->data, 0x00ca, permdeny);
-		aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00cb, vismask);
-		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
+		tmp = aim_ssi_itemlist_add(&sess->ssi.local, NULL, 0x0000, 0xFFFF, AIM_SSI_TYPE_PDINFO, data);
 	}
 
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
@@ -1119,48 +947,30 @@
  * @return Return 0 if no errors, otherwise return the error number.
  */
 faim_export int aim_ssi_setpresence(aim_session_t *sess, aim_conn_t *conn, fu32_t presence) {
-	struct aim_ssi_item *cur;
-	aim_tlv_t *tlv;
+	struct aim_ssi_item *tmp;
+	aim_tlvlist_t *data = NULL;
 
 	if (!sess || !conn)
 		return -EINVAL;
 
-	/* Look up the item */
-	cur = aim_ssi_itemlist_finditem(sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS);
+	/* Need to add the x00c9 TLV to the TLV chain */
+	aim_addtlvtochain32(&data, 0x00c9, presence);
 
-	if (cur) {
-		/* The item exists */
-		if (cur->data && (tlv = aim_gettlv(cur->data, 0x00c9, 1))) {
-			/* Just change the value of the x00c9 TLV */
-			if (tlv->length != 4) {
-				tlv->length = 4;
-				free(tlv->value);
-				tlv->value = (fu8_t *)malloc(4*sizeof(fu8_t));
-			}
-			aimutil_put32(tlv->value, presence);
-		} else {
-			/* Need to add the x00c9 TLV to the TLV chain */
-			aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00c9, presence);
-		}
-
-		/* Send the mod item SNAC */
-		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_MOD);
+	if ((tmp = aim_ssi_itemlist_finditem(sess->ssi.local, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS))) {
+		aim_freetlvchain(&tmp->data);
+		tmp->data = data;
 	} else {
-		/* Need to add the item */
-		if (!(cur = aim_ssi_itemlist_add(&sess->ssi.items, NULL, NULL, AIM_SSI_TYPE_PRESENCEPREFS)))
-			return -ENOMEM;
-		aim_addtlvtochain32((aim_tlvlist_t**)&cur->data, 0x00c9, presence);
-		aim_ssi_addmoddel(sess, conn, &cur, 1, AIM_CB_SSI_ADD);
+		tmp = aim_ssi_itemlist_add(&sess->ssi.local, NULL, 0x0000, 0xFFFF, AIM_SSI_TYPE_PRESENCEPREFS, data);
 	}
 
-	/* Begin sending SSI SNACs */
-	aim_ssi_dispatch(sess, conn);
+	/* Sync our local list with the server list */
+	aim_ssi_sync(sess, conn);
 
 	return 0;
 }
 
 /*
- * Request SSI Rights.
+ * Subtype 0x0002 - Request SSI Rights.
  */
 faim_export int aim_ssi_reqrights(aim_session_t *sess, aim_conn_t *conn)
 {
@@ -1168,21 +978,57 @@
 }
 
 /*
- * SSI Rights Information.
+ * Subtype 0x0003 - SSI Rights Information.
  */
 static int parserights(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
 {
-	int ret = 0;
+	int ret = 0, i;
 	aim_rxcallback_t userfunc;
+	aim_tlvlist_t *tlvlist;
+	aim_tlv_t *tlv;
+	aim_bstream_t bstream;
+	fu16_t *maxitems;
+
+	/* This SNAC is made up of a bunch of TLVs */
+	tlvlist = aim_readtlvchain(bs);
+
+	/* TLV 0x0004 contains the maximum number of each item */
+	if (!(tlv = aim_gettlv(tlvlist, 0x0004, 1))) {
+		aim_freetlvchain(&tlvlist);
+		return 0;
+	}
+
+	aim_bstream_init(&bstream, tlv->value, tlv->length);
+
+	if (!(maxitems = (fu16_t *)malloc((tlv->length/2)*sizeof(fu16_t)))) {
+		aim_freetlvchain(&tlvlist);
+		return 0;
+	}
+
+	for (i=0; i<(tlv->length/2); i++)
+		maxitems[i] = aimbs_get16(&bstream);
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
-		ret = userfunc(sess, rx);
+		ret = userfunc(sess, rx, tlv->length/2, maxitems);
+
+	aim_freetlvchain(&tlvlist);
+	free(maxitems);
 
 	return ret;
 }
 
 /*
- * Request SSI Data.
+ * Subtype 0x0004 - Request SSI Data.
+ * XXX - If you don't a timestamp and revision number?
+ *
+ * Note that the client should never increment the revision, only the server.
+ * 
+ */
+
+
+/*
+ * Subtype 0x0005 - Request SSI Data.
+ * XXX - If you have a timestamp and revision number?
  *
  * The data will only be sent if it is newer than the posted local
  * timestamp and revision.
@@ -1209,70 +1055,96 @@
 
 	aim_tx_enqueue(sess, fr);
 
+	/* Free any current data, just in case */
+	aim_ssi_freelist(sess);
+
 	return 0;
 }
 
 /*
- * SSI Data.
+ * Subtype 0x0006 - SSI Data.
  */
 static int parsedata(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
 {
 	int ret = 0;
 	aim_rxcallback_t userfunc;
-	struct aim_ssi_item *cur = NULL;
+	struct aim_ssi_item *cur, *new;
 	fu8_t fmtver; /* guess */
-	fu16_t revision;
+	fu16_t numitems, namelen;
 	fu32_t timestamp;
 
+	if (snac->flags & 0x0001) {
+		/* XXX - Free all ssi data? */
+	}
+
 	fmtver = aimbs_get8(bs); /* Version of ssi data.  Should be 0x00 */
-	revision = aimbs_get16(bs); /* # of times ssi data has been modified */
-	if (revision != 0)
-		sess->ssi.revision = revision;
-
-	for (cur = sess->ssi.items; cur && cur->next; cur=cur->next) ;
+	numitems = aimbs_get16(bs); /* # of times ssi data has been modified */
+	sess->ssi.numitems += numitems;
 
-	while (aim_bstream_empty(bs) > 4) { /* last four bytes are stamp */
-		fu16_t namelen, tbslen;
-
-		if (!sess->ssi.items) {
-			if (!(sess->ssi.items = malloc(sizeof(struct aim_ssi_item))))
+	/* Read in the list */
+	/* If we already have a partial list, move cur to the end of it */
+	if (sess->ssi.official)
+		for (cur=sess->ssi.official; cur->next; cur=cur->next);
+	while (aim_bstream_empty(bs) > 4) { /* last four bytes are timestamp */
+		if (!sess->ssi.official) {
+			if (!(sess->ssi.official = malloc(sizeof(struct aim_ssi_item))))
 				return -ENOMEM;
-			cur = sess->ssi.items;
+			cur = sess->ssi.official;
 		} else {
 			if (!(cur->next = malloc(sizeof(struct aim_ssi_item))))
 				return -ENOMEM;
 			cur = cur->next;
 		}
-		memset(cur, 0, sizeof(struct aim_ssi_item));
-
 		if ((namelen = aimbs_get16(bs)))
 			cur->name = aimbs_getstr(bs, namelen);
+		else
+			cur->name = NULL;
 		cur->gid = aimbs_get16(bs);
 		cur->bid = aimbs_get16(bs);
 		cur->type = aimbs_get16(bs);
-
-		if ((tbslen = aimbs_get16(bs))) {
-			aim_bstream_t tbs;
-
-			aim_bstream_init(&tbs, bs->data + bs->offset /* XXX */, tbslen);
-			cur->data = (void *)aim_readtlvchain(&tbs);
-			aim_bstream_advance(bs, tbslen);
-		}
+		cur->data = aim_readtlvchain_len(bs, aimbs_get16(bs));
+		cur->next = NULL;
 	}
 
+	/* Read in the timestamp */
 	timestamp = aimbs_get32(bs);
-	if (timestamp != 0)
-		sess->ssi.timestamp = timestamp;
-	sess->ssi.received_data = 1;
+	sess->ssi.timestamp = timestamp;
 
-	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
-		ret = userfunc(sess, rx, fmtver, sess->ssi.revision, sess->ssi.timestamp, sess->ssi.items);
+	if (!(snac->flags & 0x0001)) {
+		/* Make a copy of the list */
+		for (cur=sess->ssi.official; cur; cur=cur->next) {
+			if (!sess->ssi.local) {
+				if (!(sess->ssi.local = malloc(sizeof(struct aim_ssi_item))))
+					return -ENOMEM;
+				new = sess->ssi.local;
+			} else {
+				if (!(new->next = malloc(sizeof(struct aim_ssi_item))))
+					return -ENOMEM;
+				new = new->next;
+			}
+			if (cur->name) {
+				new->name = (char *)malloc((strlen(cur->name)+1)*sizeof(char));
+				strcpy(new->name, cur->name);
+			} else
+				new->name = NULL;
+			new->gid = cur->gid;
+			new->bid = cur->bid;
+			new->type = cur->type;
+			new->data = aim_tlvlist_copy(cur->data);
+		}
+		new->next = NULL;
+
+		sess->ssi.received_data = 1;
+
+		if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+			ret = userfunc(sess, rx, fmtver, sess->ssi.numitems, sess->ssi.official, sess->ssi.timestamp);
+	}
 
 	return ret;
 }
 
 /*
- * SSI Data Enable Presence.
+ * Subtype 0x0007 - SSI Activate Data.
  *
  * Should be sent after receiving 13/6 or 13/f to tell the server you
  * are ready to begin using the list.  It will promptly give you the
@@ -1286,56 +1158,188 @@
 }
 
 /*
- * SSI Add/Mod/Del Item(s).
+ * Subtype 0x0008/0x0009/0x000a - SSI Add/Mod/Del Item(s).
  *
  * Sends the SNAC to add, modify, or delete an item from the server-stored
  * information.  These 3 SNACs all have an identical structure.  The only
  * difference is the subtype that is set for the SNAC.
  * 
  */
-faim_export int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn, struct aim_ssi_item **items, unsigned int num, fu16_t subtype)
+faim_export int aim_ssi_addmoddel(aim_session_t *sess, aim_conn_t *conn)
 {
 	aim_frame_t *fr;
 	aim_snacid_t snacid;
-	int i, snaclen;
+	int snaclen;
+	struct aim_ssi_tmp *cur;
 
-	if (!sess || !conn || !items || !num)
+	if (!sess || !conn || !sess->ssi.pending || !sess->ssi.pending->item)
 		return -EINVAL;
 
+	/* Calculate total SNAC size */
 	snaclen = 10; /* For family, subtype, flags, and SNAC ID */
-	for (i=0; i<num; i++) {
+	for (cur=sess->ssi.pending; cur; cur=cur->next) {
 		snaclen += 10; /* For length, GID, BID, type, and length */
-		if (items[i]->name)
-			snaclen += strlen(items[i]->name);
-		if (items[i]->data)
-			snaclen += aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data);
+		if (cur->item->name)
+			snaclen += strlen(cur->item->name);
+		if (cur->item->data)
+			snaclen += aim_sizetlvchain(&cur->item->data);
 	}
 
 	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, snaclen)))
 		return -ENOMEM;
 
-	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, subtype, 0x0000, NULL, 0);
-	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, subtype, 0x0000, snacid);
+	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, sess->ssi.pending->action, 0x0000, NULL, 0);
+	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, sess->ssi.pending->action, 0x0000, snacid);
 
-	for (i=0; i<num; i++) {
-		aimbs_put16(&fr->data, items[i]->name ? strlen(items[i]->name) : 0);
-		if (items[i]->name)
-			aimbs_putraw(&fr->data, items[i]->name, strlen(items[i]->name));
-		aimbs_put16(&fr->data, items[i]->gid);
-		aimbs_put16(&fr->data, items[i]->bid);
-		aimbs_put16(&fr->data, items[i]->type);
-		aimbs_put16(&fr->data, items[i]->data ? aim_sizetlvchain((aim_tlvlist_t **)&items[i]->data) : 0);
-		if (items[i]->data)
-			aim_writetlvchain(&fr->data, (aim_tlvlist_t **)&items[i]->data);
+	for (cur=sess->ssi.pending; cur; cur=cur->next) {
+		aimbs_put16(&fr->data, cur->item->name ? strlen(cur->item->name) : 0);
+		if (cur->item->name)
+			aimbs_putraw(&fr->data, cur->item->name, strlen(cur->item->name));
+		aimbs_put16(&fr->data, cur->item->gid);
+		aimbs_put16(&fr->data, cur->item->bid);
+		aimbs_put16(&fr->data, cur->item->type);
+		aimbs_put16(&fr->data, cur->item->data ? aim_sizetlvchain(&cur->item->data) : 0);
+		if (cur->item->data)
+			aim_writetlvchain(&fr->data, &cur->item->data);
 	}
 
-	aim_ssi_enqueue(sess, conn, fr);
+	aim_tx_enqueue(sess, fr);
 
 	return 0;
 }
 
 /*
- * SSI Add/Mod/Del Ack.
+ * Subtype 0x0008 - Incoming SSI add.
+ *
+ * XXX - It would probably be good for the client to actually do something when it gets this.
+ */
+static int parseadd(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	char *name;
+	fu16_t len, gid, bid, type;
+	aim_tlvlist_t *data;
+
+	while (aim_bstream_empty(bs)) {
+		if ((len = aimbs_get16(bs)))
+			name = aimbs_getstr(bs, len);
+		else
+			name = NULL;
+		gid = aimbs_get16(bs);
+		bid = aimbs_get16(bs);
+		type = aimbs_get16(bs);
+		if ((len = aimbs_get16(bs)))
+			data = aim_readtlvchain_len(bs, len);
+		else
+			data = NULL;
+
+		aim_ssi_itemlist_add(&sess->ssi.local, name, gid, bid, type, data);
+		aim_ssi_itemlist_add(&sess->ssi.official, name, gid, bid, type, aim_tlvlist_copy(data));
+
+		if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+			ret = userfunc(sess, rx);
+
+		free(name);
+	}
+
+	return ret;
+}
+
+/*
+ * Subtype 0x0009 - Incoming SSI mod.
+ *
+ * XXX - It would probably be good for the client to actually do something when it gets this.
+ */
+static int parsemod(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	char *name;
+	fu16_t len, gid, bid, type;
+	aim_tlvlist_t *data;
+	struct aim_ssi_item *item;
+
+	while (aim_bstream_empty(bs)) {
+		if ((len = aimbs_get16(bs)))
+			name = aimbs_getstr(bs, len);
+		else
+			name = NULL;
+		gid = aimbs_get16(bs);
+		bid = aimbs_get16(bs);
+		type = aimbs_get16(bs);
+		if ((len = aimbs_get16(bs)))
+			data = aim_readtlvchain_len(bs, len);
+		else
+			data = NULL;
+
+		/* Replace the 2 local items with the given one */
+		if ((item = aim_ssi_itemlist_find(sess->ssi.local, gid, bid))) {
+			item->type = type;
+			free(item->name);
+			if (name) {
+				item->name = (char *)malloc((strlen(name)+1)*sizeof(char));
+				strcpy(item->name, name);
+			} else
+				item->name = NULL;
+			aim_freetlvchain(&item->data);
+			item->data = data;
+		}
+
+		if ((item = aim_ssi_itemlist_find(sess->ssi.official, gid, bid))) {
+			item->type = type;
+			free(item->name);
+			if (name) {
+				item->name = (char *)malloc((strlen(name)+1)*sizeof(char));
+				strcpy(item->name, name);
+			} else
+				item->name = NULL;
+			aim_freetlvchain(&item->data);
+			item->data = data;
+		}
+
+		if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+			ret = userfunc(sess, rx);
+
+		free(name);
+	}
+
+	return ret;
+}
+
+/*
+ * Subtype 0x000a - Incoming SSI del.
+ *
+ * XXX - It would probably be good for the client to actually do something when it gets this.
+ */
+static int parsedel(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	fu16_t gid, bid;
+	struct aim_ssi_item *del;
+
+	while (aim_bstream_empty(bs)) {
+		aim_bstream_advance(bs, aimbs_get16(bs));
+		gid = aimbs_get16(bs);
+		bid = aimbs_get16(bs);
+		aimbs_get16(bs);
+		aim_bstream_advance(bs, aimbs_get16(bs));
+
+		del = aim_ssi_itemlist_find(sess->ssi.local, gid, bid);
+		aim_ssi_itemlist_del(&sess->ssi.local, del);
+		del = aim_ssi_itemlist_find(sess->ssi.official, gid, bid);
+		aim_ssi_itemlist_del(&sess->ssi.official, del);
+
+		if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+			ret = userfunc(sess, rx);
+	}
+
+	return ret;
+}
+
+/*
+ * Subtype 0x000e - SSI Add/Mod/Del Ack.
  *
  * Response to add, modify, or delete SNAC (sent with aim_ssi_addmoddel).
  *
@@ -1344,40 +1348,111 @@
 {
 	int ret = 0;
 	aim_rxcallback_t userfunc;
+	struct aim_ssi_tmp *cur, *del;
 
-	sess->ssi.waiting_for_ack = 0;
-	aim_ssi_dispatch(sess, rx->conn);
+	/* Read in the success/failure flags from the ack SNAC */
+	cur = sess->ssi.pending;
+	while (cur && (aim_bstream_empty(bs)>0)) {
+		cur->ack = aimbs_get16(bs);
+		cur = cur->next;
+	}
+
+	/*
+	 * If outcome is 0, then add the item to the item list, or replace the other item, 
+	 * or remove the old item.  If outcome is non-zero, then remove the item from the 
+	 * local list, or unmodify it, or add it.
+	 */
+	for (cur=sess->ssi.pending; (cur && (cur->ack != 0xffff)); cur=cur->next) {
+	if (cur->item) {
+		if (cur->ack) {
+			/* Our action was unsuccessful, so change the local list back to how it was */
+			if (cur->action == AIM_CB_SSI_ADD) {
+				/* Remove the item from the items list */
+				if (cur->item->name) {
+					cur->name = (char *)malloc((strlen(cur->item->name)+1)*sizeof(char));
+					strcpy(cur->name, cur->item->name);
+				}
+				aim_ssi_itemlist_del(&sess->ssi.local, cur->item);
+				cur->item = NULL;
+
+			} else if (cur->action == AIM_CB_SSI_MOD) {
+				/* Replace the new item with the old item in the items list */
+				struct aim_ssi_item *cur1;
+				if ((cur1 = aim_ssi_itemlist_find(sess->ssi.local, cur->item->gid, cur->item->bid))) {
+					free(cur1->name);
+					if (cur->item->name) {
+						cur1->name = (char *)malloc((strlen(cur->item->name)+1)*sizeof(char));
+						strcpy(cur1->name, cur->item->name);
+					} else
+						cur1->name = NULL;
+					aim_freetlvchain(&cur1->data);
+					cur1->data = aim_tlvlist_copy(cur->item->data);
+				}
+
+			} else if (cur->action == AIM_CB_SSI_DEL) {
+				/* Add the item to the items list */
+				aim_tlvlist_t *data;
+				data = aim_tlvlist_copy(cur->item->data);
+				aim_ssi_itemlist_add(&sess->ssi.local, cur->item->name, cur->item->gid, cur->item->bid, cur->item->type, data);
+			}
+
+		} else {
+			/* Do the exact opposite */
+			if (cur->action == AIM_CB_SSI_ADD) {
+				/* Add the item to the items list */
+				aim_tlvlist_t *data;
+				data = aim_tlvlist_copy(cur->item->data);
+				aim_ssi_itemlist_add(&sess->ssi.official, cur->item->name, cur->item->gid, cur->item->bid, cur->item->type, data);
+
+			} else if (cur->action == AIM_CB_SSI_MOD) {
+				/* Replace the old item with the new item in the items list */
+				struct aim_ssi_item *cur1;
+				if ((cur1 = aim_ssi_itemlist_find(sess->ssi.official, cur->item->gid, cur->item->bid))) {
+					free(cur1->name);
+					if (cur->item->name) {
+						cur1->name = (char *)malloc((strlen(cur->item->name)+1)*sizeof(char));
+						strcpy(cur1->name, cur->item->name);
+					} else
+						cur1->name = NULL;
+					aim_freetlvchain(&cur1->data);
+					cur1->data = aim_tlvlist_copy(cur->item->data);
+				}
+
+			} else if (cur->action == AIM_CB_SSI_DEL) {
+				/* Remove the item from the items list */
+				aim_ssi_itemlist_del(&sess->ssi.official, cur->item);
+				cur->item = NULL;
+			}
+
+		}
+	} /* End if (cur->item) */
+	} /* End for loop */
 
 	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
-		ret = userfunc(sess, rx);
+		ret = userfunc(sess, rx, sess->ssi.pending);
+
+	/* Free all aim_ssi_tmp's with an outcome */
+	cur = sess->ssi.pending;
+	while (cur && (cur->ack != 0xffff)) {
+		del = cur;
+		cur = cur->next;
+		free(del->name);
+		free(del);
+	}
+	sess->ssi.pending = cur;
+
+	/* If we're not waiting for any more acks, then send more SNACs */
+	if (!sess->ssi.pending) {
+		sess->ssi.pending = NULL;
+		sess->ssi.waiting_for_ack = 0;
+		aim_ssi_sync(sess, rx->conn);
+	}
 
 	return ret;
 }
 
 /*
- * SSI Begin Data Modification.
- *
- * Tells the server you're going to start modifying data.
- * 
- */
-faim_export int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn)
-{
-	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTART);
-}
-
-/*
- * SSI End Data Modification.
- *
- * Tells the server you're done modifying data.
- *
- */
-faim_export int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn)
-{
-	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTOP);
-}
-
-/*
- * SSI Data Unchanged.
+ * Subtype 0x000f - SSI Data Unchanged.
  *
  * Response to aim_ssi_reqdata() if the server-side data is not newer than
  * posted local stamp/revision.
@@ -1396,6 +1471,285 @@
 	return ret;
 }
 
+/*
+ * Subtype 0x0011 - SSI Begin Data Modification.
+ *
+ * Tells the server you're going to start modifying data.
+ * 
+ */
+faim_export int aim_ssi_modbegin(aim_session_t *sess, aim_conn_t *conn)
+{
+	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTART);
+}
+
+/*
+ * Subtype 0x0012 - SSI End Data Modification.
+ *
+ * Tells the server you're finished modifying data.
+ *
+ */
+faim_export int aim_ssi_modend(aim_session_t *sess, aim_conn_t *conn)
+{
+	return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTOP);
+}
+
+/*
+ * Subtype 0x0014 - Grant authorization
+ *
+ * Authorizes a contact so they can add you to their contact list.
+ *
+ */
+faim_export int aim_ssi_sendauth(aim_session_t *sess, aim_conn_t *conn, char *sn, char *msg)
+{
+	aim_frame_t *fr;
+	aim_snacid_t snacid;
+
+	if (!sess || !conn || !sn)
+		return -EINVAL;
+
+	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn)+2+(msg ? strlen(msg)+1 : 0)+2)))
+		return -ENOMEM;
+
+	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTH, 0x0000, NULL, 0);
+	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTH, 0x0000, snacid);
+
+	/* Screen name */
+	aimbs_put8(&fr->data, strlen(sn));
+	aimbs_putraw(&fr->data, sn, strlen(sn));
+
+	/* Message (null terminated) */
+	aimbs_put16(&fr->data, msg ? strlen(msg) : 0);
+	if (msg) {
+		aimbs_putraw(&fr->data, msg, strlen(msg));
+		aimbs_put8(&fr->data, 0x00);
+	}
+
+	/* Unknown */
+	aimbs_put16(&fr->data, 0x0000);
+
+	aim_tx_enqueue(sess, fr);
+
+	return 0;
+}
+
+/*
+ * Subtype 0x0015 - Receive an authorization grant
+ */
+static int receiveauthgrant(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	fu16_t tmp;
+	char *sn, *msg;
+
+	/* Read screen name */
+	if ((tmp = aimbs_get8(bs)))
+		sn = aimbs_getstr(bs, tmp);
+	else
+		sn = NULL;
+
+	/* Read message (null terminated) */
+	if ((tmp = aimbs_get16(bs)))
+		msg = aimbs_getstr(bs, tmp);
+	else
+		msg = NULL;
+
+	/* Unknown */
+	tmp = aimbs_get16(bs);
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		ret = userfunc(sess, rx, sn, msg);
+
+	free(sn);
+	free(msg);
+
+	return ret;
+}
+
+/*
+ * Subtype 0x0018 - Send authorization request
+ *
+ * Sends a request for authorization to the given contact.  The request will either be
+ * granted, denied, or dropped.
+ *
+ */
+faim_export int aim_ssi_sendauthrequest(aim_session_t *sess, aim_conn_t *conn, char *sn, char *msg)
+{
+	aim_frame_t *fr;
+	aim_snacid_t snacid;
+
+	if (!sess || !conn || !sn)
+		return -EINVAL;
+
+	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+1+strlen(sn)+2+(msg ? strlen(msg)+1 : 0)+2)))
+		return -ENOMEM;
+
+	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, NULL, 0);
+	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREQ, 0x0000, snacid);
+
+	/* Screen name */
+	aimbs_put8(&fr->data, strlen(sn));
+	aimbs_putraw(&fr->data, sn, strlen(sn));
+
+	/* Message (null terminated) */
+	aimbs_put16(&fr->data, msg ? strlen(msg) : 0);
+	if (msg) {
+		aimbs_putraw(&fr->data, msg, strlen(msg));
+		aimbs_put8(&fr->data, 0x00);
+	}
+
+	/* Unknown */
+	aimbs_put16(&fr->data, 0x0000);
+
+	aim_tx_enqueue(sess, fr);
+
+	return 0;
+}
+
+/*
+ * Subtype 0x0019 - Receive an authorization request
+ */
+static int receiveauthrequest(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	fu16_t tmp;
+	char *sn, *msg;
+
+	/* Read screen name */
+	if ((tmp = aimbs_get8(bs)))
+		sn = aimbs_getstr(bs, tmp);
+	else
+		sn = NULL;
+
+	/* Read message (null terminated) */
+	if ((tmp = aimbs_get16(bs)))
+		msg = aimbs_getstr(bs, tmp);
+	else
+		msg = NULL;
+
+	/* Unknown */
+	tmp = aimbs_get16(bs);
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		ret = userfunc(sess, rx, sn, msg);
+
+	free(sn);
+	free(msg);
+
+	return ret;
+}
+
+/*
+ * Subtype 0x001a - Send authorization reply
+ *
+ * Sends a reply to a request for authorization.  The reply can either 
+ * grant authorization or deny authorization.
+ *
+ * if reply=0x00 then deny
+ * if reply=0x01 then grant
+ *
+ */
+faim_export int aim_ssi_sendauthreply(aim_session_t *sess, aim_conn_t *conn, char *sn, fu8_t reply, char *msg)
+{
+	aim_frame_t *fr;
+	aim_snacid_t snacid;
+
+	if (!sess || !conn || !sn)
+		return -EINVAL;
+
+	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10 + 1+strlen(sn) + 1 + 2+(msg ? strlen(msg)+1 : 0) + 2)))
+		return -ENOMEM;
+
+	snacid = aim_cachesnac(sess, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, NULL, 0);
+	aim_putsnac(&fr->data, AIM_CB_FAM_SSI, AIM_CB_SSI_SENDAUTHREP, 0x0000, snacid);
+
+	/* Screen name */
+	aimbs_put8(&fr->data, strlen(sn));
+	aimbs_putraw(&fr->data, sn, strlen(sn));
+
+	/* Grant or deny */
+	aimbs_put8(&fr->data, reply);
+
+	/* Message (null terminated) */
+	aimbs_put16(&fr->data, msg ? (strlen(msg)+1) : 0);
+	if (msg) {
+		aimbs_putraw(&fr->data, msg, strlen(msg));
+		aimbs_put8(&fr->data, 0x00);
+	}
+
+	/* Unknown */
+	aimbs_put16(&fr->data, 0x0000);
+
+	aim_tx_enqueue(sess, fr);
+
+	return 0;
+}
+
+/*
+ * Subtype 0x001b - Receive an authorization reply
+ * You get this bad boy when other people respond to the authorization 
+ * request that you have previously sent them.
+ */
+static int receiveauthreply(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	fu16_t tmp;
+	fu8_t reply;
+	char *sn, *msg;
+
+	/* Read screen name */
+	if ((tmp = aimbs_get8(bs)))
+		sn = aimbs_getstr(bs, tmp);
+	else
+		sn = NULL;
+
+	/* Read reply */
+	reply = aimbs_get8(bs);
+
+	/* Read message (null terminated) */
+	if ((tmp = aimbs_get16(bs)))
+		msg = aimbs_getstr(bs, tmp);
+	else
+		msg = NULL;
+
+	/* Unknown */
+	tmp = aimbs_get16(bs);
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		ret = userfunc(sess, rx, sn, reply, msg);
+
+	free(sn);
+	free(msg);
+
+	return ret;
+}
+
+/*
+ * Subtype 0x001c - Receive a message telling you someone added you to their list.
+ */
+static int receiveadded(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
+{
+	int ret = 0;
+	aim_rxcallback_t userfunc;
+	fu16_t tmp;
+	char *sn;
+
+	/* Read screen name */
+	if ((tmp = aimbs_get8(bs)))
+		sn = aimbs_getstr(bs, tmp);
+	else
+		sn = NULL;
+
+	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
+		ret = userfunc(sess, rx, sn);
+
+	free(sn);
+
+	return ret;
+}
+
 static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
 {
 
@@ -1403,10 +1757,24 @@
 		return parserights(sess, mod, rx, snac, bs);
 	else if (snac->subtype == AIM_CB_SSI_LIST)
 		return parsedata(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_ADD)
+		return parseadd(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_MOD)
+		return parsemod(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_DEL)
+		return parsedel(sess, mod, rx, snac, bs);
 	else if (snac->subtype == AIM_CB_SSI_SRVACK)
 		return parseack(sess, mod, rx, snac, bs);
 	else if (snac->subtype == AIM_CB_SSI_NOLIST)
 		return parsedataunchanged(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_RECVAUTH)
+		return receiveauthgrant(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_RECVAUTHREQ)
+		return receiveauthrequest(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_RECVAUTHREP)
+		return receiveauthreply(sess, mod, rx, snac, bs);
+	else if (snac->subtype == AIM_CB_SSI_ADDED)
+		return receiveadded(sess, mod, rx, snac, bs);
 
 	return 0;
 }
@@ -1422,7 +1790,7 @@
 {
 
 	mod->family = AIM_CB_FAM_SSI;
-	mod->version = 0x0003;
+	mod->version = 0x0004;
 	mod->toolid = 0x0110;
 	mod->toolversion = 0x0629;
 	mod->flags = 0;
--- a/src/protocols/oscar/tlv.c	Tue Jan 07 20:57:48 2003 +0000
+++ b/src/protocols/oscar/tlv.c	Tue Jan 07 21:19:05 2003 +0000
@@ -113,7 +113,10 @@
  * @param num The max number of TLVs that will be read, or -1 if unlimited.  
  *        There are a number of places where you want to read in a tlvchain, 
  *        but the chain is not at the end of the SNAC, and the chain is 
- *        preceeded by the number of TLVs.  So you can limit that.
+ *        preceeded by the number of TLVs.  So you can limit that with this.
+ *
+ * AAA - Change this.  It would never need to read in unlimited, right?
+ * That's what aim_readtlvchain() is for.  Dumb ass.
  *
  * Reads and parses a series of TLV patterns from a data buffer; the
  * returned structure is manipulatable with the rest of the TLV
@@ -176,6 +179,93 @@
 }
 
 /**
+ * aim_readtlvchain_num - Read a TLV chain from a buffer.
+ * @param bs Input bstream
+ * @param len The max length in bytes that will be read.
+ *        There are a number of places where you want to read in a tlvchain, 
+ *        but the chain is not at the end of the SNAC, and the chain is 
+ *        preceeded by the length of the TLVs.  So you can limit that with this.
+ *
+ * Reads and parses a series of TLV patterns from a data buffer; the
+ * returned structure is manipulatable with the rest of the TLV
+ * routines.  When done with a TLV chain, aim_freetlvchain() should
+ * be called to free the dynamic substructures.
+ *
+ * XXX There should be a flag setable here to have the tlvlist contain
+ * bstream references, so that at least the ->value portion of each 
+ * element doesn't need to be malloc/memcpy'd.  This could prove to be
+ * just as effecient as the in-place TLV parsing used in a couple places
+ * in libfaim.
+ *
+ */
+faim_internal aim_tlvlist_t *aim_readtlvchain_len(aim_bstream_t *bs, fu16_t len)
+{
+	aim_tlvlist_t *list = NULL, *cur;
+	
+	while ((aim_bstream_empty(bs) > 0) && (len > 0)) {
+		fu16_t type, length;
+
+		type = aimbs_get16(bs);
+		length = aimbs_get16(bs);
+
+		if (length > aim_bstream_empty(bs)) {
+			aim_freetlvchain(&list);
+			return NULL;
+		}
+
+		cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
+		if (!cur) {
+			aim_freetlvchain(&list);
+			return NULL;
+		}
+
+		memset(cur, 0, sizeof(aim_tlvlist_t));
+
+		cur->tlv = createtlv();
+		if (!cur->tlv) {
+			free(cur);
+			aim_freetlvchain(&list);
+			return NULL;
+		}
+		cur->tlv->type = type;
+		if ((cur->tlv->length = length)) {
+		       cur->tlv->value = aimbs_getraw(bs, length);	
+		       if (!cur->tlv->value) {
+			       freetlv(&cur->tlv);
+			       free(cur);
+			       aim_freetlvchain(&list);
+			       return NULL;
+		       }
+		}
+
+		len -= aim_sizetlvchain(&cur);
+		cur->next = list;
+		list = cur;
+	}
+
+	return list;
+}
+
+/**
+ * aim_tlvlist_copy - Duplicate a TLV chain.
+ * @param orig
+ *
+ * This is pretty pelf exslanatory.
+ *
+ */
+faim_internal aim_tlvlist_t *aim_tlvlist_copy(aim_tlvlist_t *orig)
+{
+	aim_tlvlist_t *new = NULL;
+
+	while (orig) {
+		aim_addtlvtochain_raw(&new, orig->tlv->type, orig->tlv->length, orig->tlv->value);
+		orig = orig->next;
+	}
+
+	return new;
+}
+
+/**
  * aim_freetlvchain - Free a TLV chain structure
  * @list: Chain to be freed
  *