Index: libfaim/ssi.c
===================================================================
--- libfaim/ssi.c	(revision a053de89ecf8ca17b1ea41aadc0baa4b6a84f37e)
+++ libfaim/ssi.c	(revision a0a517919ef86311a49f1e5652564bf3c3408ef0)
@@ -41,11 +41,11 @@
   int newlen;
   struct aim_ssi_item *cur, *group;
-  
-  if (!list)
-    return NULL;
+
+  owl_function_debugmsg("aim_ssi_itemlist_rebuildgroup: in for group %s", name?name:"NULL");
+  
+  if (!list) return(NULL);
   
   /* Find the group */
-  if (!(group = aim_ssi_itemlist_finditem(list, name, NULL, AIM_SSI_TYPE_GROUP)))
-    return NULL;
+  if (!(group = aim_ssi_itemlist_finditem(list, name, NULL, AIM_SSI_TYPE_GROUP))) return(NULL);
   
   /* Free the old data */
@@ -64,4 +64,5 @@
 	newlen += 2;
   }
+  owl_function_debugmsg("aim_ssi_itemlist_rebuildgroup: newlen is %i", newlen);
   
   /* Build the new TLV list */
@@ -69,6 +70,5 @@
     fu8_t *newdata;
     
-    if (!(newdata = (fu8_t *)malloc((newlen)*sizeof(fu8_t))))
-      return NULL;
+    if (!(newdata = (fu8_t *)malloc((newlen)*sizeof(fu8_t)))) return NULL;
     newlen = 0;
     if (group->gid == 0x0000) {
@@ -85,5 +85,6 @@
     free(newdata);
   }
-  
+
+  owl_function_debugmsg("aim_ssi_itemlist_rebuildgroup: exiting");
   return group;
 }
@@ -177,14 +178,18 @@
   if (!list || !(*list) || !del) return -EINVAL;
 
+  owl_function_debugmsg("aim_ssi_itemlist_del: in");
   /* Remove the item from the list */
   if (*list == del) {
     *list = (*list)->next;
+    owl_function_debugmsg("aim_ssi_itemlist_del: deleted %s from beginning of list", del->name);
   } 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;
+    owl_function_debugmsg("aim_ssi_itemlist_del: deleted %s from middle of list", del->name);
   }
 
   /* Free the deleted item */
+  owl_function_debugmsg("aim_ssi_itemlist_del: freeing");
   free(del->name);
   aim_freetlvchain(&del->data);
@@ -450,11 +455,14 @@
   struct aim_ssi_item *cur1, *cur2;
   struct aim_ssi_tmp *cur, *new;
-  
-  if (!sess)
-    return -EINVAL;
-  
+
+  owl_function_debugmsg("aim_ssi_sync: beginning");
+  
+  if (!sess) return (-EINVAL);
+
   /* If we're waiting for an ack, we shouldn't do anything else */
-  if (sess->ssi.waiting_for_ack)
+  if (sess->ssi.waiting_for_ack) {
+    owl_function_debugmsg("Aborting aim_ssi_sync, waiting for ack");
     return 0;
+  }
   
   /*
@@ -466,9 +474,10 @@
    * buddy ID#s, which makes things more efficient.  I think.
    */
-  
+
   /* 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)) {
+	owl_function_debugmsg("aim_ssi_sync: doing addition");
 	new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
 	new->action = AIM_CB_SSI_ADD;
@@ -490,4 +499,5 @@
     for (cur1=sess->ssi.official; cur1; cur1=cur1->next) {
       if (!aim_ssi_itemlist_find(sess->ssi.local, cur1->gid, cur1->bid)) {
+	owl_function_debugmsg("aim_ssi_sync: doing deletion");
 	new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
 	new->action = AIM_CB_SSI_DEL;
@@ -510,4 +520,5 @@
       cur2 = aim_ssi_itemlist_find(sess->ssi.official, cur1->gid, cur1->bid);
       if (cur2 && (aim_ssi_itemlist_cmp(cur1, cur2))) {
+	owl_function_debugmsg("aim_ssi_sync: doing modification");
 	new = (struct aim_ssi_tmp *)malloc(sizeof(struct aim_ssi_tmp));
 	new->action = AIM_CB_SSI_MOD;
@@ -527,4 +538,5 @@
   /* We're out of stuff to do, so tell the AIM servers we're done and exit */
   if (!sess->ssi.pending) {
+    owl_function_debugmsg("aim_ssi_sync: telling server we're done modifying SSI data.");
     aim_ssi_modend(sess);
     return 0;
@@ -533,7 +545,9 @@
   /* Make sure we don't send anything else between now 
    * and when we receive the ack for the following operation */
+  owl_function_debugmsg("aim_ssi_sync: setting SSI waiting_for_ack");
   sess->ssi.waiting_for_ack = 1;
   
   /* Now go mail off our data and wait 4 to 6 weeks */
+  owl_function_debugmsg("aim_ssi_sync: about to call addmoddel to send SNACs to server");
   aim_ssi_addmoddel(sess);
   
@@ -789,5 +803,5 @@
   /* 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);
@@ -807,4 +821,5 @@
   
   /* Sync our local list with the server list */
+  owl_function_debugmsg("aim_ssi_delbuddy: about to sync");
   aim_ssi_sync(sess);
   
@@ -1261,6 +1276,8 @@
   struct aim_ssi_tmp *cur;
   
-  if (!sess || !(conn = aim_conn_findbygroup(sess, AIM_CB_FAM_SSI)) || !sess->ssi.pending || !sess->ssi.pending->item)
+  if (!sess || !(conn = aim_conn_findbygroup(sess, AIM_CB_FAM_SSI)) || !sess->ssi.pending || !sess->ssi.pending->item) {
+    owl_function_debugmsg("aim_ssi_addmoddel: aborting early");
     return -EINVAL;
+  }
   
   /* Calculate total SNAC size */
@@ -1268,12 +1285,9 @@
   for (cur=sess->ssi.pending; cur; cur=cur->next) {
     snaclen += 10; /* For length, GID, BID, type, and length */
-    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;
+    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, sess->ssi.pending->action, 0x0000, NULL, 0);
@@ -1282,16 +1296,15 @@
   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));
+    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);
+    if (cur->item->data) aim_writetlvchain(&fr->data, &cur->item->data);
   }
   
   aim_tx_enqueue(sess, fr);
-  
+
+  owl_function_debugmsg("aim_ssi_addmoddel: exiting normally");
   return 0;
 }
@@ -1441,4 +1454,6 @@
   aim_rxcallback_t userfunc;
   struct aim_ssi_tmp *cur, *del;
+
+  owl_function_debugmsg("Handling SNAC SSI ACK");
   
   /* Read in the success/failure flags from the ack SNAC */
@@ -1455,8 +1470,10 @@
    */
   for (cur=sess->ssi.pending; (cur && (cur->ack != 0xffff)); cur=cur->next) {
+    owl_function_debugmsg("parseack: processing a change");
     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) {
+	  owl_function_debugmsg("parseack: unsuccesful add, reverting");
 	  /* Remove the item from the local list */
 	  /* Make sure cur->item is still valid memory */
@@ -1472,4 +1489,5 @@
 	} else if (cur->action == AIM_CB_SSI_MOD) {
 	  /* Replace the local item with the item from the official list */
+	  owl_function_debugmsg("parseack: unsuccesful modify, reverting");
 	  if (aim_ssi_itemlist_valid(sess->ssi.local, cur->item)) {
 	    struct aim_ssi_item *cur1;
@@ -1489,4 +1507,5 @@
 	} else if (cur->action == AIM_CB_SSI_DEL) {
 	  /* Add the item back into the local list */
+	  owl_function_debugmsg("parseack: unsuccesful delete, reverting");
 	  if (aim_ssi_itemlist_valid(sess->ssi.official, cur->item)) {
 	    aim_ssi_itemlist_add(&sess->ssi.local, cur->item->name, cur->item->gid, cur->item->bid, cur->item->type, cur->item->data);
@@ -1499,4 +1518,5 @@
 	if (cur->action == AIM_CB_SSI_ADD) {
 	  /* Add the local item to the official list */
+	  owl_function_debugmsg("parseack: succesful add");
 	  if (aim_ssi_itemlist_valid(sess->ssi.local, cur->item)) {
 	    aim_ssi_itemlist_add(&sess->ssi.official, cur->item->name, cur->item->gid, cur->item->bid, cur->item->type, cur->item->data);
@@ -1506,4 +1526,5 @@
 	} else if (cur->action == AIM_CB_SSI_MOD) {
 	  /* Replace the official item with the item from the local list */
+	  owl_function_debugmsg("parseack: succesful modify");
 	  if (aim_ssi_itemlist_valid(sess->ssi.local, cur->item)) {
 	    struct aim_ssi_item *cur1;
@@ -1523,4 +1544,5 @@
 	} else if (cur->action == AIM_CB_SSI_DEL) {
 	  /* Remove the item from the official list */
+	  owl_function_debugmsg("parseack: succesful delete");
 	  if (aim_ssi_itemlist_valid(sess->ssi.official, cur->item))
 	    aim_ssi_itemlist_del(&sess->ssi.official, cur->item);
@@ -1546,9 +1568,13 @@
   
   /* 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;
+    owl_function_debugmsg("parseack: Clearing SSI waiting_for_ack");
     aim_ssi_sync(sess);
   }
+
+  owl_function_debugmsg("parseack: returning with %i", ret);
   
   return ret;
@@ -1601,6 +1627,5 @@
   aim_conn_t *conn;
   
-  if (!sess || !(conn = aim_conn_findbygroup(sess, AIM_CB_FAM_SSI)))
-    return -EINVAL;
+  if (!sess || !(conn = aim_conn_findbygroup(sess, AIM_CB_FAM_SSI))) return -EINVAL;
   
   return aim_genericreq_n(sess, conn, AIM_CB_FAM_SSI, AIM_CB_SSI_EDITSTOP);
