changeset 1106:5bc8fdacd2cb

[gaim-migrate @ 1116] lots of changes. buddy.c: just in general tried to get things to work better. moving things in the edit list window and signing off should be handled better in the main buddy list window (watch out for flashes). gaim.h: removed toc-specific things and moved them to toc.c and rvous.c as needed. gtkhtml.c: possible fix for AOL 6.0 problems (I wasn't able to reproduce the problem before or after the fix, but i fixed what i think might have been causing the problem). multi.c: moved LOGIN_STEPS from gaim.h here and actually use it now oscar.c: moved an oscar-specific struct definition from gaim.h here and also handle problems better perl.c: fix for stupid problem rvous.c: first pass at attempt to be able to remove toc.c and rvous.c (though this will never happen; gaim will support toc as long as aol does) without cruft. gaim is now only dependent on toc.c and rvous.c for toc_build_config and parse_toc_buddy_list, which gaim needs to save and read its buddy list. toc.c: rewrote the signin process so that the read()'s won't block. it's not actually a non-blocking read; it's just that it won't ever get to the read until there's data to be read (thanks to the gdk_input watcher). this means the cancel button should work after it's connected, but it's still not a non-blocking connect. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Mon, 20 Nov 2000 07:24:18 +0000
parents c964df5b2a84
children 9500ab01e595
files src/buddy.c src/gaim.h src/gtkhtml.c src/multi.c src/oscar.c src/perl.c src/rvous.c src/toc.c
diffstat 8 files changed, 518 insertions(+), 749 deletions(-) [+]
line wrap: on
line diff
--- a/src/buddy.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/buddy.c	Mon Nov 20 07:24:18 2000 +0000
@@ -103,8 +103,9 @@
 static int group_number(char *group);
 static int buddy_number(char *group, char *buddy);
 static struct group_show *new_group_show(char *group);
-static struct buddy_show *new_buddy_show(struct group_show *gs, struct buddy *buddy);
+static struct buddy_show *new_buddy_show(struct group_show *gs, struct buddy *buddy, char **xpm);
 static struct group_show *find_gs_by_bs(struct buddy_show *b);
+static void redo_buddy_list();
 
 void destroy_buddy()
 {
@@ -190,7 +191,10 @@
 	int total = 0, on = 0;
 	char buf[256];
 
-	if (!g_slist_find(shows, gs)) return;
+	if (!g_slist_find(shows, gs)) {
+		debug_printf("update_num_group called for unfound group_show %s\n", gs->name);
+		return;
+	}
 
 	while (c) {
 		gc = (struct gaim_connection *)c->data;
@@ -259,55 +263,6 @@
 #endif
 
 
-static void destroy_buddies(struct gaim_connection *gc) {
-	GSList *s = shows;
-	struct group_show *g;
-	GSList *m;
-	struct buddy_show *b;
-	gboolean remove_group;
-
-	while (s) {
-		remove_group = FALSE;
-		g = (struct group_show *)s->data;
-		m = g->members;
-		while (m) {
-			b = (struct buddy_show *)m->data;
-			if ((g_slist_length(b->connlist) == 1) && (b->connlist->data == gc)) {
-				if (b->log_timer > 0)
-					gtk_timeout_remove(b->log_timer);
-				b->log_timer = 0;
-				b->connlist = g_slist_remove(b->connlist, gc);
-				gtk_container_remove(GTK_CONTAINER(g->tree), b->item);
-				m = g->members = g_slist_remove(g->members, b);
-				if ((g->members == NULL) && (display_options & OPT_DISP_NO_MT_GRP)) {
-					shows = g_slist_remove(shows, g);
-					gtk_container_remove(GTK_CONTAINER(buddies), g->item);
-					g_free(g->name);
-					g_free(g);
-					m = NULL;
-					remove_group = TRUE;
-				} else
-					update_num_group(g);
-				g_free(b->name);
-				g_free(b->show);
-				g_free(b);
-			} else if (g_slist_find(b->connlist, gc)) {
-				if (g_slist_find(b->connlist, gc)) {
-					b->connlist = g_slist_remove(b->connlist, gc);
-					update_num_group(g);
-				}
-				m = g_slist_next(m);
-			} else
-				m = g_slist_next(m);
-		}
-		if (remove_group)
-			s = shows;
-		else
-			s = g_slist_next(s);
-	}
-}
-
-
 void signoff_all(GtkWidget *w, gpointer d)
 {
 	GSList *c = connections;
@@ -322,10 +277,10 @@
 
 void signoff(struct gaim_connection *gc)
 {
-	destroy_buddies(gc);
 	plugin_event(event_signoff, gc, 0, 0, 0);
 	update_keepalive(gc, FALSE);
 	serv_close(gc);
+	redo_buddy_list();
 
 	if (connections) return;
 
@@ -651,11 +606,71 @@
 	 * just have to go through and reorder everything. remember, nothing is going to
 	 * change connections, so we can assume that we don't have to change any user
 	 * data or anything. this is just a simple reordering. so calm down. */
+	/* note: we only have to do this if we want to strongly enforce order; however,
+	 * order doesn't particularly matter to the stability of the program. but, it's
+	 * kind of nice to have */
+	/* the easy way to implement this is just to go through shows and destroy all the
+	 * group_shows, then go through the connections and put everything back. though,
+	 * there are slight complications with that; most of them deal with timeouts and
+	 * people not seeing the login icon for the full 10 seconds. butt fuck them. */
 	GSList *s = shows;
-	struct group_show *g;
+	struct group_show *gs;
 	GSList *m;
-	struct buddy_show *b;
+	struct buddy_show *bs;
+	GSList *c = connections;
 	struct gaim_connection *gc;
+	GSList *gr;
+	struct group *g;
+	struct buddy *b;
+
+	while (s) {
+		gs = (struct group_show *)s->data;
+		s = g_slist_remove(s, gs);
+		m = gs->members;
+		gtk_container_remove(GTK_CONTAINER(buddies), gs->item);
+		while (m) {
+			bs = (struct buddy_show *)m->data;
+			m = g_slist_remove(m, bs);
+			if (bs->log_timer > 0)
+				gtk_timeout_remove(bs->log_timer);
+			g_free(bs->show);
+			g_free(bs->name);
+			g_free(bs);
+		}
+		g_free(gs->name);
+		g_free(gs);
+	}
+	shows = NULL;
+	while (c) {
+		gc = (struct gaim_connection *)c->data;
+		c = c->next;
+		gr = gc->groups;
+		while (gr) {
+			g = (struct group *)gr->data;
+			gr = gr->next;
+			gs = find_group_show(g->name);
+			if (!gs && !(display_options & OPT_DISP_NO_MT_GRP))
+				gs = new_group_show(g->name);
+			m = g->members;
+			while (m) {
+				b = (struct buddy *)m->data;
+				m = m->next;
+				if (b->present) {
+					if (!gs)
+						gs = new_group_show(g->name);
+					bs = find_buddy_show(gs, b->name);
+					if (!bs) {
+						if (gc->prpl->list_icon)
+							bs = new_buddy_show(gs, b,
+								(*gc->prpl->list_icon)(b->uc));
+						else
+							bs = new_buddy_show(gs, b, (char **)no_icon_xpm);
+					}
+					bs->connlist = g_slist_append(bs->connlist, gc);
+				}
+			}
+		}
+	}
 }
 
 static void edit_tree_move (GtkCTree *ctree, GtkCTreeNode *child, GtkCTreeNode *parent,
@@ -1393,14 +1408,16 @@
 static struct group_show *find_group_show(char *group) {
 	GSList *m = shows;
 	struct group_show *g = NULL;
+	char *who = g_strdup(normalize(group));
 
 	while (m) {
 		g = (struct group_show *)m->data;
-		if (!strcmp(g->name, group))
+		if (!strcasecmp(normalize(g->name), who))
 			break;
 		g = NULL;
 		m = m->next;
 	}
+	g_free(who);
 
 	return g;
 }
@@ -1412,7 +1429,7 @@
 
 	while (m) {
 		b = (struct buddy_show *)m->data;
-		if (!strcmp(normalize(b->name), who))
+		if (!strcasecmp(normalize(b->name), who))
 			break;
 		b = NULL;
 		m = m->next;
@@ -1509,7 +1526,7 @@
 	return g;
 }
 
-static struct buddy_show *new_buddy_show(struct group_show *gs, struct buddy *buddy) {
+static struct buddy_show *new_buddy_show(struct group_show *gs, struct buddy *buddy, char **xpm) {
 	struct buddy_show *b = g_new0(struct buddy_show, 1);
 	GtkWidget *box;
 	GdkPixmap *pm;
@@ -1530,7 +1547,7 @@
 	gtk_container_add(GTK_CONTAINER(b->item), box);
 	gtk_widget_show(box);
 
-	pm = gdk_pixmap_create_from_xpm_d(blist->window, &bm, NULL, (char **)login_icon_xpm);
+	pm = gdk_pixmap_create_from_xpm_d(blist->window, &bm, NULL, xpm);
 	b->pix = gtk_pixmap_new(pm, bm);
 	gtk_box_pack_start(GTK_BOX(box), b->pix, FALSE, FALSE, 1);
 	gtk_widget_show(b->pix);
@@ -1588,6 +1605,8 @@
 			g_free(g->name);
 			g_free(g);
 		}
+		gtk_timeout_remove(b->log_timer);
+		b->log_timer = 0;
 		g_free(b->name);
 		g_free(b->show);
 		g_free(b);
@@ -1609,9 +1628,9 @@
 			BuddyTickerSetPixmap(b->name, pm, bm);
 		gdk_pixmap_unref(pm);
 		gdk_bitmap_unref(bm);
+		gtk_timeout_remove(b->log_timer);
+		b->log_timer = 0;
 	}
-	gtk_timeout_remove(b->log_timer);
-	b->log_timer = 0;
 	return 0;
 }
 
@@ -1753,7 +1772,7 @@
 		if ((gs = find_group_show(g->name)) == NULL)
 			gs = new_group_show(g->name);
 		if ((bs = find_buddy_show(gs, b->name)) == NULL)
-			bs = new_buddy_show(gs, b);
+			bs = new_buddy_show(gs, b, (char **)login_icon_xpm);
 		if (b->present == 1) {
 			play_sound(BUDDY_ARRIVE);
 			pm = gdk_pixmap_create_from_xpm_d(blist->window, &bm,
@@ -1768,15 +1787,14 @@
 			gdk_pixmap_unref(pm);
 			gdk_bitmap_unref(bm);
 			b->present = 2;
-			if (bs->log_timer > 0)
-				gtk_timeout_remove(bs->log_timer);
-			bs->log_timer = 0;
 			if (!g_slist_find(bs->connlist, gc))
 				bs->connlist = g_slist_append(bs->connlist, gc);
 			else
 				debug_printf("already got signon for %s from %s\n", b->name, gc->username);
+			if (bs->log_timer > 0)
+				gtk_timeout_remove(bs->log_timer);
+			bs->log_timer = gtk_timeout_add(10000, (GtkFunction)log_timeout, bs);
 			update_num_group(gs);
-			bs->log_timer = gtk_timeout_add(10000, (GtkFunction)log_timeout, bs);
 			if (display_options & OPT_DISP_SHOW_LOGON) {
 				struct conversation *c = find_conversation(b->name);
 				if (c) {
@@ -1811,11 +1829,12 @@
 					      buddies that have already signed
 					      off */
 		play_sound(BUDDY_LEAVE);
+
 		bs->connlist = g_slist_remove(bs->connlist, gc);
-		update_num_group(gs);
 		if (bs->log_timer > 0)
 			gtk_timeout_remove(bs->log_timer);
 		bs->log_timer = gtk_timeout_add(10000, (GtkFunction)log_timeout, bs);
+		update_num_group(gs);
 		pm = gdk_pixmap_create_from_xpm_d(blist->window, &bm, NULL, logout_icon_xpm);
 		gtk_widget_hide(bs->pix);
 		gtk_pixmap_set(GTK_PIXMAP(bs->pix), pm, bm);
--- a/src/gaim.h	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/gaim.h	Mon Nov 20 07:24:18 2000 +0000
@@ -47,13 +47,6 @@
 	6.  actually done..
 */
 
-#define STATE_OFFLINE 0
-#define STATE_FLAPON 1
-#define STATE_SIGNON_REQUEST 2
-#define STATE_SIGNON_ACK 3
-#define STATE_CONFIG 4
-#define STATE_ONLINE 5
-
 #define BROWSER_NETSCAPE              0
 #define BROWSER_KFM                   1
 #define BROWSER_MANUAL                2
@@ -269,13 +262,6 @@
         int exchange;
 };
 
-struct chat_connection {
-	char *name;
-	int fd; /* this is redundant since we have the conn below */
-	struct aim_conn_t *conn;
-	int inpa;
-};
-
 struct debug_window {
 	GtkWidget *window;
 	GtkWidget *entry;
@@ -340,39 +326,6 @@
 	gboolean is_chat;
 };
 
-struct file_header {
-	char  magic[4];		/* 0 */
-	short hdrlen;		/* 4 */
-	short hdrtype;		/* 6 */
-	char  bcookie[8];	/* 8 */
-	short encrypt;		/* 16 */
-	short compress;		/* 18 */
-	short totfiles;		/* 20 */
-	short filesleft;	/* 22 */
-	short totparts;		/* 24 */
-	short partsleft;	/* 26 */
-	long  totsize;		/* 28 */
-	long  size;		/* 32 */
-	long  modtime;		/* 36 */
-	long  checksum;		/* 40 */
-	long  rfrcsum;		/* 44 */
-	long  rfsize;		/* 48 */
-	long  cretime;		/* 52 */
-	long  rfcsum;		/* 56 */
-	long  nrecvd;		/* 60 */
-	long  recvcsum;		/* 64 */
-	char  idstring[32];	/* 68 */
-	char  flags;		/* 100 */
-	char  lnameoffset;	/* 101 */
-	char  lsizeoffset;	/* 102 */
-	char  dummy[69];	/* 103 */
-	char  macfileinfo[16];	/* 172 */
-	short nencode;		/* 188 */
-	short nlanguage;	/* 190 */
-	char  name[64];		/* 192 */
-				/* 256 */
-};
-
 struct file_transfer {
         GtkWidget *window;
         char *cookie;
@@ -390,22 +343,6 @@
 	struct gaim_connection *gc;
 };
 
-struct sflap_hdr {
-	unsigned char ast;
-	unsigned char type;
-	unsigned short seqno;
-	unsigned short len;
-};
-
-struct signon {
-	unsigned int ver;
-	unsigned short tag;
-	unsigned short namelen;
-	char username[80];
-};
-
-#define LOGIN_STEPS 5
-
 #define CONVERSATION_TITLE "Gaim - Conversation with %s"
 #define LOG_CONVERSATION_TITLE "Gaim - Conversation with %s (logged)"
 
@@ -419,12 +356,6 @@
 
 /* These should all be runtime selectable */
 
-#define TOC_HOST "toc.oscar.aol.com"
-#define TOC_PORT 9898
-#define AUTH_HOST "login.oscar.aol.com"
-#define AUTH_PORT 5190
-#define LANGUAGE "english"
-
 #define MSG_LEN 2048
 /* The above should normally be the same as BUF_LEN,
  * but just so we're explictly asking for the max message
@@ -433,17 +364,6 @@
 #define BUF_LONG BUF_LEN * 2
 
 
-#define TYPE_SIGNON    1  
-#define TYPE_DATA      2
-#define TYPE_ERROR     3
-#define TYPE_SIGNOFF   4
-#define TYPE_KEEPALIVE 5
-
-#define FLAPON "FLAPON\r\n\r\n"
-
-#define ROAST "Tic/Toc"
-
-
 #define BUDDY_ARRIVE 0
 #define BUDDY_LEAVE 1
 #define RECEIVE 2
@@ -466,10 +386,6 @@
 extern GdkColor fgcolor;
 extern int smiley_array[FACE_TOTAL];
 
-/* Globals in network.c */
-
-/* Globals in toc.c */
-
 /* Globals in aim.c */
 extern GList *log_conversations;
 extern GList *buddy_pounces;
@@ -657,6 +573,7 @@
 extern void serv_chat_leave(struct gaim_connection *, int);
 extern void serv_chat_whisper(struct gaim_connection *, int, char *, char *);
 extern void serv_chat_send(struct gaim_connection *, int, char *);
+extern void update_keepalive(struct gaim_connection *, gboolean);
 
 /* output from serv */
 extern void serv_got_update(struct gaim_connection *, char *, int, int, time_t, time_t, int, gushort);
@@ -695,25 +612,9 @@
 extern void set_font_face(char *, struct conversation *);
 extern void redo_convo_menus();
 
-/* Functions in network.c */
-extern unsigned int *get_address(char *);
-extern int connect_address(unsigned int, unsigned short);
-
-/* Functions in oscar.c */
-extern void oscar_login(struct aim_user *);
-extern void oscar_close(struct gaim_connection *);
-extern struct chat_connection *find_oscar_chat(struct gaim_connection *, char *name);
-extern void update_keepalive(struct gaim_connection *, gboolean);
-
 /* Functions in toc.c */
-extern void toc_close();
-extern void toc_login(struct aim_user *);
-extern int toc_wait_signon(struct gaim_connection *);
-extern char *toc_wait_config(struct gaim_connection *);
-extern int sflap_send(struct gaim_connection *, char *, int , int );
 extern void parse_toc_buddy_list(struct gaim_connection *, char *, int);
 
-
 /* Functions in buddy.c */
 extern void destroy_buddy();
 extern void update_button_pix();
--- a/src/gtkhtml.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/gtkhtml.c	Mon Nov 20 07:24:18 2000 +0000
@@ -3427,6 +3427,16 @@
 					html_strtok(tag, ' ');
 					while ((d = html_strtok(NULL, ' ')))
 					{
+						if (!strncasecmp(d, "style=", strlen("style=")))
+						{
+							d += strlen("style=");
+							if (*d == '\"') {
+								d++;
+								while (*d != '\0' && *d != '\"') d++;
+								if (*d == '\0')
+									html_strtok(tag, ' ');
+							}
+						}
 						if (!strncasecmp(d, "COLOR=", strlen("COLOR=")))
 						{
 							d += strlen("COLOR=");
--- a/src/multi.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/multi.c	Mon Nov 20 07:24:18 2000 +0000
@@ -33,6 +33,8 @@
 #include "pixmaps/cancel.xpm"
 #include "pixmaps/ok.xpm"
 
+#define LOGIN_STEPS 5
+
 GSList *connections;
 
 static GtkWidget *acctedit = NULL;
@@ -834,7 +836,7 @@
 		gtk_widget_show(gc->meter);
 	}
 
-	gtk_progress_bar_update(GTK_PROGRESS_BAR(gc->progress), howfar / 5);
+	gtk_progress_bar_update(GTK_PROGRESS_BAR(gc->progress), howfar / LOGIN_STEPS);
 	gtk_statusbar_pop(GTK_STATUSBAR(gc->status), 1);
 	gtk_statusbar_push(GTK_STATUSBAR(gc->status), 1, message);
 }
--- a/src/oscar.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/oscar.c	Mon Nov 20 07:24:18 2000 +0000
@@ -72,6 +72,13 @@
 	GSList *direct_ims;
 };
 
+struct chat_connection {
+	char *name;
+	int fd; /* this is redundant since we have the conn below */
+	struct aim_conn_t *conn;
+	int inpa;
+};
+
 struct direct_im {
 	struct gaim_connection *gc;
 	char name[80];
@@ -351,7 +358,7 @@
 	if (conn == NULL) {
 		debug_print(_("internal connection error\n"));
 		hide_login_progress(gc, _("Unable to login to AIM"));
-		serv_close(gc);
+		signoff(gc);
 		return;
 	} else if (conn->fd == -1) {
 		if (conn->status & AIM_CONN_STATUS_RESOLVERR) {
@@ -363,7 +370,7 @@
 			debug_print(debug_buff); debug_print("\n");
 			hide_login_progress(gc, debug_buff);
 		}
-		serv_close(gc);
+		signoff(gc);
 		return;
 	}
 	g_snprintf(buf, sizeof(buf), _("Signon: %s"), gc->username);
@@ -467,14 +474,14 @@
 		set_user_state(offline);
 #endif
 		hide_login_progress(gc, _("Internal Error"));
-		serv_close(gc);
+		signoff(gc);
 		return -1;
 	} else if (bosconn->status & AIM_CONN_STATUS_CONNERR) {
 #ifdef USE_APPLET
 		set_user_state(offline);
 #endif
 		hide_login_progress(gc, _("Could Not Connect"));
-		serv_close(gc);
+		signoff(gc);
 		return -1;
 	}
 
--- a/src/perl.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/perl.c	Mon Nov 20 07:24:18 2000 +0000
@@ -426,7 +426,7 @@
 		else signoff_all(NULL, NULL);
 	} else if (!strncasecmp(command, "away", 4)) {
 		char *message = SvPV(ST(1), junk);
-		struct away_message a;
+		static struct away_message a;
 		g_snprintf(a.message, sizeof(a.message), "%s", message);
 		do_away_message(NULL, &a);
 	} else if (!strncasecmp(command, "back", 4)) {
--- a/src/rvous.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/rvous.c	Mon Nov 20 07:24:18 2000 +0000
@@ -40,6 +40,41 @@
 #include "proxy.h"
 #include "gaim.h"
 
+#define TYPE_DATA 2 /* from toc.c */
+
+struct file_header {
+	char  magic[4];		/* 0 */
+	short hdrlen;		/* 4 */
+	short hdrtype;		/* 6 */
+	char  bcookie[8];	/* 8 */
+	short encrypt;		/* 16 */
+	short compress;		/* 18 */
+	short totfiles;		/* 20 */
+	short filesleft;	/* 22 */
+	short totparts;		/* 24 */
+	short partsleft;	/* 26 */
+	long  totsize;		/* 28 */
+	long  size;		/* 32 */
+	long  modtime;		/* 36 */
+	long  checksum;		/* 40 */
+	long  rfrcsum;		/* 44 */
+	long  rfsize;		/* 48 */
+	long  cretime;		/* 52 */
+	long  rfcsum;		/* 56 */
+	long  nrecvd;		/* 60 */
+	long  recvcsum;		/* 64 */
+	char  idstring[32];	/* 68 */
+	char  flags;		/* 100 */
+	char  lnameoffset;	/* 101 */
+	char  lsizeoffset;	/* 102 */
+	char  dummy[69];	/* 103 */
+	char  macfileinfo[16];	/* 172 */
+	short nencode;		/* 188 */
+	short nlanguage;	/* 190 */
+	char  name[64];		/* 192 */
+				/* 256 */
+};
+
 static void do_send_file(GtkWidget *, struct file_transfer *);
 static void do_get_file (GtkWidget *, struct file_transfer *);
 
--- a/src/toc.c	Sun Nov 19 10:01:53 2000 +0000
+++ b/src/toc.c	Mon Nov 20 07:24:18 2000 +0000
@@ -47,21 +47,48 @@
 #include "pixmaps/dt_icon.xpm"
 #include "pixmaps/free_icon.xpm"
 
-#define REVISION "gaim:$Revision: 1110 $"
+#define REVISION "gaim:$Revision: 1116 $"
+
+#define TYPE_SIGNON    1
+#define TYPE_DATA      2
+#define TYPE_ERROR     3
+#define TYPE_SIGNOFF   4
+#define TYPE_KEEPALIVE 5
+
+#define FLAPON "FLAPON\r\n\r\n"
+#define ROAST "Tic/Toc"
+
+#define TOC_HOST "toc.oscar.aol.com"
+#define TOC_PORT 9898
+#define AUTH_HOST "login.oscar.aol.com"
+#define AUTH_PORT 5190
+#define LANGUAGE "english"
+
+#define STATE_OFFLINE 0
+#define STATE_FLAPON 1
+#define STATE_SIGNON_REQUEST 2
+#define STATE_ONLINE 3
+#define STATE_PAUSE 4
 
 struct toc_data {
-	int toc_fd;
-	int seqno;
-	int state;
+        int toc_fd;
+        int seqno;
+        int state;
 };
 
+struct sflap_hdr {
+	unsigned char ast;
+	unsigned char type;
+	unsigned short seqno;
+	unsigned short len;
+};
 
-static unsigned int peer_ver=0;
-#ifdef _WIN32
-static int win32_r;
-#endif
-
-static int toc_signon(struct gaim_connection *);
+struct signon {
+	unsigned int ver;
+	unsigned short tag;
+	unsigned short namelen;
+	char username[80];
+};
 
 /* constants to identify proto_opts */
 #define USEROPT_AUTH      0
@@ -70,22 +97,23 @@
 #define USEROPT_SOCKSPORT 3
 #define USEROPT_PROXYTYPE 4
 
+static void toc_callback(gpointer, gint, GdkInputCondition);
+static unsigned char *roast_password(char *);
+
 /* ok. this function used to take username/password, and return 0 on success.
  * now, it takes username/password, and returns NULL on error or a new gaim_connection
  * on success. */
-void toc_login(struct aim_user *user)
-{
-	char *config;
+static void toc_login(struct aim_user *user) {
 	struct gaim_connection *gc;
 	struct toc_data *tdt;
 	char buf[80];
-	char buf2[2048];
 
 	gc = new_gaim_conn(user);
 	gc->proto_data = tdt = g_new0(struct toc_data, 1);
-	
-	g_snprintf(buf, sizeof(buf), "Looking up %s", 
-		user->proto_opt[USEROPT_AUTH][0] ? user->proto_opt[USEROPT_AUTH] : TOC_HOST);
+
+	g_snprintf(buf, sizeof buf, "Lookin up %s",
+			user->proto_opt[USEROPT_AUTH][0] ? user->proto_opt[USEROPT_AUTH] : TOC_HOST);
+	/* this is such a hack */
 	set_login_progress(gc, 1, buf);
 	while (gtk_events_pending())
 		gtk_main_iteration();
@@ -94,143 +122,73 @@
 
 	tdt->toc_fd = proxy_connect(
 		user->proto_opt[USEROPT_AUTH][0] ? user->proto_opt[USEROPT_AUTH] : TOC_HOST,
-		user->proto_opt[USEROPT_AUTHPORT][0] ? atoi(user->proto_opt[USEROPT_AUTHPORT]) : TOC_PORT,
+		user->proto_opt[USEROPT_AUTHPORT][0] ? atoi(user->proto_opt[USEROPT_AUTHPORT]):TOC_PORT,
 		user->proto_opt[USEROPT_SOCKSHOST], atoi(user->proto_opt[USEROPT_SOCKSPORT]),
 		atoi(user->proto_opt[USEROPT_PROXYTYPE]));
 
-        if (tdt->toc_fd < 0) {
+	debug_printf("* Client connects to TOC\n");
+	if (tdt->toc_fd < 0) {
 		g_snprintf(buf, sizeof(buf), "Connect to %s failed",
-			 user->proto_opt[USEROPT_AUTH]);
+				user->proto_opt[USEROPT_AUTH]);
 		hide_login_progress(gc, buf);
-		serv_close(gc);
-		return;
-        }
-
-	g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username);
-	set_login_progress(gc, 3, buf);
-	while (gtk_events_pending())
-		gtk_main_iteration();
-	if (!g_slist_find(connections, gc))
-		return;
-	
-	if (toc_signon(gc) < 0) {
-		hide_login_progress(gc, "Disconnected.");
-		serv_close(gc);
-		return;
-	}
-
-	g_snprintf(buf, sizeof(buf), "Waiting for reply...");
-	set_login_progress(gc, 4, buf);
-	while (gtk_events_pending())
-		gtk_main_iteration();
-	if (!g_slist_find(connections, gc))
-		return;
-	if (toc_wait_signon(gc) < 0) {
-		hide_login_progress(gc, "Authentication Failed");
-		serv_close(gc);
+		signoff(gc);
 		return;
 	}
 
-	gc->options = user->options;
-	save_prefs();
-
-	g_snprintf(buf, sizeof(buf), "Retrieving config...");
-	set_login_progress(gc, 5, buf);
-	while (gtk_events_pending())
-		gtk_main_iteration();
-	if (!g_slist_find(connections, gc))
+	debug_printf("* Client sends \"FLAPON\\r\\n\\r\\n\"\n");
+	if (write(tdt->toc_fd, FLAPON, strlen(FLAPON)) < 0) {
+		hide_login_progress(gc, "Disconnected.");
+		signoff(gc);
 		return;
-
-	account_online(gc);
-	serv_finish_login(gc);
-
-	config = toc_wait_config(gc);
-	tdt->state = STATE_ONLINE;
+	}
+	tdt->state = STATE_FLAPON;
 
-	if (config != NULL)
-		parse_toc_buddy_list(gc, config, 0);
-	else
-		do_import(0, gc);
-        
-	g_snprintf(buf2, sizeof(buf2), "toc_init_done");
-	sflap_send(gc, buf2, -1, TYPE_DATA);
+	/* i know a lot of people like to look at gaim to see how TOC works. so i'll comment
+	 * on what this does. it's really simple. when there's data ready to be read from the
+	 * toc_fd file descriptor, toc_callback is called, with gc passed as its data arg. */
+	gc->inpa = gdk_input_add(tdt->toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, gc);
 
-	g_snprintf(buf2, sizeof(buf2), "toc_set_caps %s %s %s %s %s",
-		   FILE_SEND_UID, FILE_GET_UID, B_ICON_UID, IMAGE_UID,
-		   VOICE_UID);
-	sflap_send(gc, buf2, -1, TYPE_DATA);
-
-	if (gc->keepalive < 0)
-		update_keepalive(gc, gc->options & OPT_USR_KEEPALV);
+	g_snprintf(buf, sizeof(buf), "Signon: %s", gc->username);
+	set_login_progress(gc, 2, buf);
 }
 
-void toc_close(struct gaim_connection *gc)
-{
-	if (gc->protocol != PROTO_TOC) return; /* how did this happen? */
-        if (gc->inpa > 0)
+static void toc_set_config(struct gaim_connection *gc) {
+	char buf[MSG_LEN], snd[MSG_LEN];
+	toc_build_config(gc, buf, MSG_LEN, FALSE);
+	g_snprintf(snd, MSG_LEN, "toc_set_config \"%s\"", buf);
+	sflap_send(gc, snd, -1, TYPE_DATA);
+}
+
+static void toc_close(struct gaim_connection *gc) {
+	toc_set_config(gc);
+	if (gc->inpa > 0)
 		gdk_input_remove(gc->inpa);
 	gc->inpa = -1;
 	close(((struct toc_data *)gc->proto_data)->toc_fd);
 }
 
-unsigned char *roast_password(char *pass)
-{
-	/* Trivial "encryption" */
-	static char rp[256];
-	static char *roast = ROAST;
-	int pos=2;
-	int x;
-	strcpy(rp, "0x");
-	for (x=0;(x<150) && pass[x]; x++) 
-		pos+=sprintf(&rp[pos],"%02x", pass[x] ^ roast[x % strlen(roast)]);
-	rp[pos]='\0';
-        return rp;
-}
-
-
-char *print_header(void *hdr_v)
-{
-	static char s[80];
-	struct sflap_hdr *hdr = (struct sflap_hdr *)hdr_v;
-	g_snprintf(s,sizeof(s), "[ ast: %c, type: %d, seqno: %d, len: %d ]",
-		hdr->ast, hdr->type, ntohs(hdr->seqno), ntohs(hdr->len));
-	return s;
-}
-
-void print_buffer(char *buf, int len)
-{
-#if 0
-	int x;
-	printf("[ ");
-	for (x=0;x<len;x++) 
-		printf("%d ", buf[x]);
-	printf("]\n");
-	printf("[ ");
-	for (x=0;x<len;x++)
-		printf("%c ", buf[x]);
-	printf("]\n");
-#endif
-}
-
-int sflap_send(struct gaim_connection *gc, char *buf, int olen, int type)
-{
+int sflap_send(struct gaim_connection *gc, char *buf, int olen, int type) {
 	int len;
 	int slen=0;
 	struct sflap_hdr hdr;
 	char obuf[MSG_LEN];
 	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
 
+	if (tdt->state == STATE_PAUSE)
+		/* TOC has given us the PAUSE message; sending could cause a disconnect
+		 * so we just return here like everything went through fine */
+		return 0;
+
 	/* One _last_ 2048 check here!  This shouldn't ever
-	 * get hit though, hopefully.  If it gets hit on an IM
-	 * It'll lose the last " and the message won't go through,
-	 * but this'll stop a segfault. */
+	* get hit though, hopefully.  If it gets hit on an IM
+	* It'll lose the last " and the message won't go through,
+	* but this'll stop a segfault. */
 	if (strlen(buf) > (MSG_LEN - sizeof(hdr))) {
+		debug_printf("message too long, truncating\n");
 		buf[MSG_LEN - sizeof(hdr) - 3] = '"';
 		buf[MSG_LEN - sizeof(hdr) - 2] = '\0';
 	}
 
-	debug_printf("%s [Len %d]\n", buf, strlen(buf));
-	
 	if (olen < 0)
 		len = escape_message(buf);
 	else
@@ -238,10 +196,7 @@
 	hdr.ast = '*';
 	hdr.type = type;
 	hdr.seqno = htons(tdt->seqno++ & 0xffff);
-        hdr.len = htons(len + (type == TYPE_SIGNON ? 0 : 1));
-
-    sprintf(debug_buff,"Escaped message is '%s'\n",buf);
-	debug_print(debug_buff);
+	hdr.len = htons(len + (type == TYPE_SIGNON ? 0 : 1));
 
 	memcpy(obuf, &hdr, sizeof(hdr));
 	slen += sizeof(hdr);
@@ -251,147 +206,190 @@
 		obuf[slen]='\0';
 		slen += 1;
 	}
-	print_buffer(obuf, slen);
 
 	return write(tdt->toc_fd, obuf, slen);
 }
 
-
-static int wait_reply(struct gaim_connection *gc, char *buffer, size_t buflen)
-{
-        size_t res=-1;
-	int read_rv = -1;
-	struct sflap_hdr *hdr=(struct sflap_hdr *)buffer;
+static int wait_reply(struct gaim_connection *gc, char *buffer, size_t buflen) {
 	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
-        char *c;
+	struct sflap_hdr *hdr;
+	int ret;
 
-	if(buflen < sizeof(struct sflap_hdr)) {
-	    do_error_dialog(_("Unable to read from server: Buffer too small"),
-			    _("Gaim - Error (internal)"));
-	    return -1;
+	if (read(tdt->toc_fd, buffer, sizeof(struct sflap_hdr)) < 0) {
+		debug_printf("error, couldn't read flap header\n");
+		return -1;
 	}
 
-        while((read_rv = read(tdt->toc_fd, buffer, 1))) {
-		if (read_rv < 0 || read_rv > 1)
-			return -1;
-		if (buffer[0] == '*')
-                        break;
+	hdr = (struct sflap_hdr *)buffer;
 
+	if (buflen < ntohs(hdr->len)) {
+		/* fake like there's a read error */
+		debug_printf("buffer too small (have %d, need %d)\n", buflen, ntohs(hdr->len));
+		return -1;
 	}
 
-	read_rv = read(tdt->toc_fd, buffer+1, sizeof(struct sflap_hdr) - 1);
-
-        if (read_rv < 0)
-		return read_rv;
+	if (ntohs(hdr->len) > 0) {
+		ret = read(tdt->toc_fd, buffer + sizeof(struct sflap_hdr), ntohs(hdr->len));
+		buffer[sizeof(struct sflap_hdr) + ret] = '\0';
+		return ret;
+	} else return 0;
+}
 
-	res = read_rv + 1;
-	
-        
-	sprintf(debug_buff, "Rcv: %s %s\n",print_header(buffer), "");
-	debug_print(debug_buff);
+static unsigned char *roast_password(char *pass) {
+	/* Trivial "encryption" */
+	static char rp[256];
+	static char *roast = ROAST;
+	int pos=2;
+	int x;
+	strcpy(rp, "0x");
+	for (x=0;(x<150) && pass[x]; x++)
+		pos+=sprintf(&rp[pos],"%02x", pass[x] ^ roast[x % strlen(roast)]);
+	rp[pos]='\0';
+	return rp;
+}
 
+static void toc_callback(gpointer data, gint source, GdkInputCondition condition) {
+	struct gaim_connection *gc = (struct gaim_connection *)data;
+	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
+	struct sflap_hdr *hdr;
+	struct signon so;
+	char buf[8 * 1024], *c;
+	char snd[MSG_LEN];
 
-	if(buflen < sizeof(struct sflap_hdr) + ntohs(hdr->len) + 1) {
-	    do_error_dialog(_("Unable to read from server: Too much information"),
-			    _("Gaim - Error (internal)"));
-	    return -1;
+	if (condition & GDK_INPUT_EXCEPTION) {
+		debug_printf("gdk_input exception! check internet connection\n");
+		hide_login_progress(gc, _("Connection Closed"));
+		signoff(gc);
+		return;
 	}
 
-        while (res < (sizeof(struct sflap_hdr) + ntohs(hdr->len))) {
-		read_rv = read(tdt->toc_fd, buffer + res, (ntohs(hdr->len) + sizeof(struct sflap_hdr)) - res);
-		if(read_rv < 0) return read_rv;
-		res += read_rv;
-		/* my feeling is this will kill us. if there's data pending then we'll come right back
-		 * to where we are now. possible workarounds are to remove the input watcher until
-		 * we're done with this part
-		while(gtk_events_pending())
-			gtk_main_iteration();
-		 */
+	/* there's data waiting to be read, so read it. */
+	if (wait_reply(gc, buf, 8 * 1024) < 0) {
+		hide_login_progress(gc, _("Connection Closed"));
+		signoff(gc);
+		return;
 	}
-        
-        if (res >= sizeof(struct sflap_hdr)) 
-		buffer[res]='\0';
-	else
-		return res - sizeof(struct sflap_hdr);
-		
-	switch(hdr->type) {
-	case TYPE_SIGNON:
-		memcpy(&peer_ver, buffer + sizeof(struct sflap_hdr), 4);
-		peer_ver = ntohl(peer_ver);
+
+	if (tdt->state == STATE_FLAPON) {
+		hdr = (struct sflap_hdr *)buf;
+		if (hdr->type != TYPE_SIGNON)
+			debug_printf("problem, hdr->type != TYPE_SIGNON\n");
+		else
+			debug_printf("* TOC sends Client FLAP SIGNON\n");
 		tdt->seqno = ntohs(hdr->seqno);
 		tdt->state = STATE_SIGNON_REQUEST;
-		break;
-	case TYPE_DATA:
-		if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "SIGN_ON:", strlen("SIGN_ON:")))
-			tdt->state = STATE_SIGNON_ACK;
-		else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "CONFIG:", strlen("CONFIG:"))) {
-			tdt->state = STATE_CONFIG;
-		} else if (!strncasecmp(buffer + sizeof(struct sflap_hdr), "ERROR:", strlen("ERROR:"))) {
-			c = strtok(buffer + sizeof(struct sflap_hdr) + strlen("ERROR:"), ":");
-			show_error_dialog(c);
+
+		debug_printf("* Client sends TOC FLAP SIGNON\n");
+		g_snprintf(so.username, sizeof(so.username), "%s", gc->username);
+		so.ver = htonl(1);
+		so.tag = htons(1);
+		so.namelen = htons(strlen(so.username));
+		if (sflap_send(gc, (char *)&so, ntohs(so.namelen) + 8, TYPE_SIGNON) < 0) {
+			hide_login_progress(gc, _("Disconnected."));
+			signoff(gc);
+			return;
+		}
+
+		debug_printf("* Client sends TOC \"toc_signon\" message\n");
+		g_snprintf(snd, sizeof snd, "toc_signon %s %d %s %s %s \"%s\"",
+				AUTH_HOST, AUTH_PORT, normalize(gc->username),
+				roast_password(gc->password), LANGUAGE, REVISION);
+		if (sflap_send(gc, snd, -1, TYPE_DATA) < 0) {
+			hide_login_progress(gc, _("Disconnected."));
+			signoff(gc);
+			return;
 		}
 
-		sprintf(debug_buff, "Data: %s\n",buffer + sizeof(struct sflap_hdr));
-		debug_print(debug_buff);
+		set_login_progress(gc, 3, _("Waiting for reply..."));
+		return;
+	}
 
-		break;
-	default:
-			sprintf(debug_buff, "Unknown/unimplemented packet type %d\n",hdr->type);
-			debug_print(debug_buff);
-	}
-        return res;
-}
+	if (tdt->state == STATE_SIGNON_REQUEST) {
+		debug_printf("* TOC sends client SIGN_ON reply\n");
+		if (strncasecmp(buf + sizeof(struct sflap_hdr), "SIGN_ON", strlen("SIGN_ON"))) {
+			debug_printf("Didn't get SIGN_ON! buf was: %s\n", buf+sizeof(struct sflap_hdr));
+			hide_login_progress(gc, _("Authentication Failed"));
+			signoff(gc);
+			return;
+		}
+		/* we're supposed to check that it's really TOC v1 here but we know it is ;) */
+		debug_printf("TOC version: %s\n", buf + sizeof(struct sflap_hdr) + 4);
 
-
+		/* we used to check for the CONFIG here, but we'll wait until we've sent our
+		 * version of the config and then the toc_init_done message. we'll come back to
+		 * the callback in a better state if we get CONFIG anyway */
 
-void toc_callback( gpointer          data,
-                   gint              source,
-                   GdkInputCondition condition )
-{
-        char *buf;
-	char *c;
-        char *l;
-	struct gaim_connection *gc = (struct gaim_connection *)data;
+		gc->options = gc->user->options;
+		tdt->state = STATE_ONLINE;
+
+		account_online(gc);
+		serv_finish_login(gc);
+
+		do_import(0, gc);
 
-        buf = g_malloc(2 * BUF_LONG);
-        if (wait_reply(gc, buf, 2 * BUF_LONG) < 0) {
-                hide_login_progress(gc, "Connection Closed");
-                signoff(gc); /* this will free gc for us */
-                g_free(buf);
+		/* Client sends TOC toc_init_done message */
+		debug_printf("* Client sends TOC toc_init_done message\n");
+		g_snprintf(snd, sizeof snd, "toc_init_done");
+		sflap_send(gc, snd, -1, TYPE_DATA);
+
+		g_snprintf(snd, sizeof snd, "toc_set_caps %s %s %s %s %s",
+			FILE_SEND_UID, FILE_GET_UID, B_ICON_UID, IMAGE_UID, VOICE_UID);
+		sflap_send(gc, snd, -1, TYPE_DATA);
+
+		if (gc->keepalive < 0)
+			update_keepalive(gc, gc->options & OPT_USR_KEEPALV);
+
 		return;
-        }
-                         
-        
-	c=strtok(buf+sizeof(struct sflap_hdr),":");	/* Ditch the first part */
-	if (!strcasecmp(c,"UPDATE_BUDDY")) {
-		char *uc;
+	}
+
+	debug_printf("From TOC server: %s\n", buf + sizeof(struct sflap_hdr));
+
+	c = strtok(buf + sizeof(struct sflap_hdr), ":"); /* Ditch the first part */
+
+	if (!strcasecmp(c, "SIGN_ON")) {
+		/* we should only get here after a PAUSE */
+		if (tdt->state != STATE_PAUSE)
+			debug_printf("got SIGN_ON but not PAUSE!\n");
+		else {
+			tdt->state = STATE_ONLINE;
+			do_error_dialog(_("TOC has come back from its pause. You may now send"
+					" messages again."), _("TOC Resume"));
+			do_import(0, gc);
+			g_snprintf(snd, sizeof snd, "toc_init_done");
+			sflap_send(gc, snd, -1, TYPE_DATA);
+		}
+	} else if (!strcasecmp(c, "CONFIG")) {
+		c = strtok(NULL, ":");
+		parse_toc_buddy_list(gc, c, 0);
+	} else if (!strcasecmp(c, "NICK")) {
+		c = strtok(NULL, ":");
+		g_snprintf(gc->username, sizeof(gc->username), "%s", c);
+	} else if (!strcasecmp(c, "IM_IN")) {
+		char *away, *message;
+		int a = 0;
+
+		c = strtok(NULL, ":");
+		away = strtok(NULL, ":");
+		message = strtok(NULL, ":");
+		a = (away && (*away == 'T')) ? 1 : 0;
+
+		serv_got_im(gc, c, message, a);
+	} else if (!strcasecmp(c, "UPDATE_BUDDY")) {
+		char *l, *uc;
 		int logged, evil, idle, type = 0;
-                time_t signon;
-                time_t time_idle;
-		
-		c = strtok(NULL,":"); /* c is name */
+		time_t signon, time_idle;
 
-		l = strtok(NULL,":"); /* l is T/F logged status */
-       	
+		c = strtok(NULL, ":"); /* name */
+		l = strtok(NULL, ":"); /* online */
 		sscanf(strtok(NULL, ":"), "%d", &evil);
-		
-		sscanf(strtok(NULL, ":"), "%ld", &signon);
-		
+		sscanf(strtok(NULL, ":"), "%d", &signon);
 		sscanf(strtok(NULL, ":"), "%d", &idle);
-		
-                uc = strtok(NULL, ":");
-
+		uc = strtok(NULL, ":");
 
-		if (!strncasecmp(l,"T",1))
-			logged = 1;
-		else
-			logged = 0;
+		logged = (l && (*l == 'T')) ? 1 : 0;
 
-
-		if (uc[0] == 'A')
-			type |= UC_AOL;
-		
-		switch(uc[1]) {
+		if (uc[0] == 'A') type |= UC_AOL;
+		switch (uc[1]) {
 		case 'A':
 			type |= UC_ADMIN;
 			break;
@@ -404,408 +402,203 @@
 		default:
 			break;
 		}
-
-                switch(uc[2]) {
-		case 'U':
-			type |= UC_UNAVAILABLE;
-			break;
-		default:
-			break;
-		}
+		if (uc[0] == 'U') type |= UC_UNAVAILABLE;
 
-                if (idle) {
-                        time(&time_idle);
-                        time_idle -= idle*60;
-                } else
-                        time_idle = 0;
-		
-                serv_got_update(gc, c, logged, evil, signon, time_idle, type, 0);
+		if (idle) { time(&time_idle); time_idle -= idle * 60; }
+		else time_idle = 0;
 
-	} else if (!strcasecmp(c, "CONFIG")) {
-		/* do we want to load the buddy list again here? */
-		c = strtok(NULL,":");
-		parse_toc_buddy_list(gc, c, 0);
+		serv_got_update(gc, c, logged, evil, signon, time_idle, type, 0);
 	} else if (!strcasecmp(c, "ERROR")) {
-		/* This should be handled by wait_reply
-		c = strtok(NULL,":");
+		c = strtok(NULL, ":");
 		show_error_dialog(c);
-		*/
-	} else if (!strcasecmp(c, "NICK")) {
-		c = strtok(NULL,":");
-		g_snprintf(gc->username, sizeof(gc->username), "%s", c);
-	} else if (!strcasecmp(c, "IM_IN")) {
-		char *away, *message;
-                int a = 0;
-                
-		c = strtok(NULL,":");
-		away = strtok(NULL,":");
-
-		message = away;
-
-                while(*message && (*message != ':'))
-                        message++;
-
-                message++;
+	} else if (!strcasecmp(c, "EVILED")) {
+		int lev;
+		char *name;
 
-		if (!strncasecmp(away, "T", 1))
-			a = 1;
-                serv_got_im(gc, c, message, a);
-		
-	} else if (!strcasecmp(c, "GOTO_URL")) {
-		char *name;
-		char *url;
-
-		char tmp[256];
-		
+		sscanf(strtok(NULL, ":"), "%d", &lev);
 		name = strtok(NULL, ":");
-		url = strtok(NULL, ":");
-
-
-		g_snprintf(tmp, sizeof(tmp), "http://%s:%d/%s", TOC_HOST, TOC_PORT, url);
-		g_show_info(gc->user, tmp);
-        } else if (!strcasecmp(c, "EVILED")) {
-                int lev;
-		char *name = NULL;
-
-                sscanf(strtok(NULL, ":"), "%d", &lev);
-                name = strtok(NULL, ":");
-
-                sprintf(debug_buff,"%s | %d\n", name, lev);
-				debug_print(debug_buff);
 
 		serv_got_eviled(gc, name, lev);
-		
-        } else if (!strcasecmp(c, "CHAT_JOIN")) {
-                char *name;
-                int id;
-                
+	} else if (!strcasecmp(c, "CHAT_JOIN")) {
+		char *name;
+		int id;
+
+		sscanf(strtok(NULL, ":"), "%d", &id);
+		name = strtok(NULL, ":");
+
+		serv_got_joined_chat(gc, id, name);
+	} else if (!strcasecmp(c, "CHAT_IN")) {
+		int id, w;
+		char *m, *who, *whisper;
 
 		sscanf(strtok(NULL, ":"), "%d", &id);
-                name = strtok(NULL, ":");
-                serv_got_joined_chat(gc, id, name);
+		who = strtok(NULL, ":");
+		whisper = strtok(NULL, ":");
+		m = strtok(NULL, ":");
 
-	} else if (!strcasecmp(c, "DIR_STATUS")) {
-	} else if (!strcasecmp(c, "ADMIN_PASSWD_STATUS")) {
-		do_error_dialog("Password Change Successeful", "Gaim - Password Change");
+		w = (whisper && (*whisper == 'T')) ? 1 : 0;
+
+		serv_got_chat_in(gc, id, who, w, m);
 	} else if (!strcasecmp(c, "CHAT_UPDATE_BUDDY")) {
 		int id;
-		char *in;
-		char *buddy;
-                GSList *bcs = gc->buddy_chats;
+		char *in, *buddy;
+		GSList *bcs = gc->buddy_chats;
 		struct conversation *b = NULL;
-		
+
 		sscanf(strtok(NULL, ":"), "%d", &id);
-
 		in = strtok(NULL, ":");
 
 		while(bcs) {
 			b = (struct conversation *)bcs->data;
 			if (id == b->id)
-				break;	
+				break;
 			bcs = bcs->next;
-                        b = NULL;
-		}
-		
-		if (!b) {
-			g_free(buf); 
-			return;
+			b = NULL;
 		}
 
-		
-		if (!strcasecmp(in, "T")) {
-			while((buddy = strtok(NULL, ":")) != NULL) {
+		if (!b) return;
+
+		if (in && (*in == 'T'))
+			while ((buddy = strtok(NULL, ":")) != NULL)
 				add_chat_buddy(b, buddy);
-			}
-		} else {
-			while((buddy = strtok(NULL, ":")) != NULL) {
+		else
+			while ((buddy = strtok(NULL, ":")) != NULL)
 				remove_chat_buddy(b, buddy);
-			}
-		}
+	} else if (!strcasecmp(c, "CHAT_INVITE")) {
+		char *name, *who, *message;
+		int id;
 
+		name = strtok(NULL, ":");
+		sscanf(strtok(NULL, ":"), "%d", &id);
+		who = strtok(NULL, ":");
+		message = strtok(NULL, ":");
+
+		serv_got_chat_invite(gc, name, id, who, message);
 	} else if (!strcasecmp(c, "CHAT_LEFT")) {
 		int id;
 
-
-                sscanf(strtok(NULL, ":"), "%d", &id);
-
-                serv_got_chat_left(gc, id);
-
-
-	} else if (!strcasecmp(c, "CHAT_IN")) {
-
-		int id, w;
-		char *m;
-		char *who, *whisper;
+		sscanf(strtok(NULL, ":"), "%d", &id);
 
-	
-		sscanf(strtok(NULL, ":"), "%d", &id);
-		who = strtok(NULL, ":");
-		whisper = strtok(NULL, ":");
-		m = whisper;
-                while(*m && (*m != ':')) m++;
-                m++;
+		serv_got_chat_left(gc, id);
+	} else if (!strcasecmp(c, "GOTO_URL")) {
+		char *name, *url, tmp[256];
 
-                if (!strcasecmp(whisper, "T"))
-			w = 1;
-		else
-			w = 0;
-
-		serv_got_chat_in(gc, id, who, w, m);
-
-
-	} else if (!strcasecmp(c, "CHAT_INVITE")) {
-		char *name;
-		char *who;
-		char *message;
-                int id;
+		name = strtok(NULL, ":");
+		url = strtok(NULL, ":");
 
-               
-		name = strtok(NULL, ":");
-		sscanf(strtok(NULL, ":"), "%d", &id);
-		who = strtok(NULL, ":");
-                message = strtok(NULL, ":");
-
-                serv_got_chat_invite(gc, name, id, who, message);
-
+		g_snprintf(tmp, sizeof(tmp), "http://%s:%d/%s", TOC_HOST, TOC_PORT, url);
+		g_show_info(gc->user, tmp);
+	} else if (!strcasecmp(c, "DIR_STATUS")) {
+	} else if (!strcasecmp(c, "ADMIN_NICK_STATUS")) {
+	} else if (!strcasecmp(c, "ADMIN_PASSWD_STATUS")) {
+		do_error_dialog(_("Password Change Successeful"), _("Gaim - Password Change"));
+	} else if (!strcasecmp(c, "PAUSE")) {
+		tdt->state = STATE_PAUSE;
+		do_error_dialog(_("TOC has sent a PAUSE command. When this happens, TOC ignores"
+				" any messages sent to it, and may kick you off if you send a"
+				" message. Gaim will prevent anything from going through. This"
+				" is only temporary, please be patient."), _("TOC Pause"));
+	} else if (!strcasecmp(c, "RVOUS_PROPOSE")) {
+		char *user, *uuid, *cookie;
+		int seq;
+		char *rip, *pip, *vip;
+		int port;
 
-        } else if (!strcasecmp(c, "RVOUS_PROPOSE")) {
-                char *user;
-                char *uuid;
-                char *cookie;
-                int seq;
-                char *rip, *pip, *vip;
-                int port;
-                int unk[4];
-                char *messages[4];
-                int subtype, files, totalsize;
-                char *name;
-                char *tmp;
-                int i;
-                struct file_transfer *ft;
-                
-
-                user = strtok(NULL, ":");
-                uuid = strtok(NULL, ":");
-                cookie = strtok(NULL, ":");
-                sscanf(strtok(NULL, ":"), "%d", &seq);
-                rip = strtok(NULL, ":");
-                pip = strtok(NULL, ":");
-                vip = strtok(NULL, ":");
-                sscanf(strtok(NULL, ":"), "%d", &port);
+		user = strtok(NULL, ":");
+		uuid = strtok(NULL, ":");
+		cookie = strtok(NULL, ":");
+		sscanf(strtok(NULL, ":"), "%d", &seq);
+		rip = strtok(NULL, ":");
+		pip = strtok(NULL, ":");
+		vip = strtok(NULL, ":");
+		sscanf(strtok(NULL, ":"), "%d", &port);
 
 		if (!strcmp(uuid, FILE_SEND_UID)) {
-			/* we're getting a file */
-	                for (i=0; i<4; i++) {
-	                        sscanf(strtok(NULL, ":"), "%d", &unk[i]);
-	                        if (unk[i] == 10001)
-	                                break;
-	                        messages[i] = frombase64(strtok(NULL, ":"));
-	                }
-	                tmp = frombase64(strtok(NULL, ":"));
-	                subtype = tmp[1];
-	                files = tmp[3]; /* These are fine */
+			/* they want us to get a file */
+			int unk[4], i;
+			char *messages[4], *tmp, *name;
+			int subtype, files, totalsize = 0;
+			struct file_transfer *ft;
 
-			totalsize = 0;
+			for (i = 0; i < 4; i++) {
+				sscanf(strtok(NULL, ":"), "%d", unk + i);
+				if (unk[1] == 10001) break;
+				messages[i] = frombase64(strtok(NULL, ":"));
+			}
+			tmp = frombase64(strtok(NULL, ":"));
+
+			subtype = tmp[1];
+			files = tmp[3];
+			
 			totalsize |= (tmp[4] << 24) & 0xff000000;
 			totalsize |= (tmp[5] << 16) & 0x00ff0000;
 			totalsize |= (tmp[6] <<  8) & 0x0000ff00;
 			totalsize |= (tmp[7] <<  0) & 0x000000ff;
 
-	                name = tmp + 8;
-
-	                ft = g_new0(struct file_transfer, 1);
+			if (!totalsize) {
+				g_free(tmp);
+				for (i--; i >= 0; i--) g_free(messages[i]);
+				return;
+			}
 
-	                ft->cookie = g_strdup(cookie);
-	                ft->ip = g_strdup(pip);
-	                ft->port = port;
-	                if (i)
-	                        ft->message = g_strdup(messages[0]);
-	                else
-	                        ft->message = NULL;
-	                ft->filename = g_strdup(name);
-	                ft->user = g_strdup(user);
-	                ft->size = totalsize;
-			sprintf(ft->UID, "%s", FILE_SEND_UID);
-			ft->gc = gc;
-                
-	                g_free(tmp);
+			name = tmp + 8;
 
-	                for (i--; i >= 0; i--)
-	                        g_free(messages[i]);
-                
-			if (totalsize) /* sanity check */
-				accept_file_dialog(ft);
-		} else if (!strcmp(uuid, FILE_GET_UID)) {
-			/* we're sending a file */
-	                for (i=0; i<4; i++) {
-	                        sscanf(strtok(NULL, ":"), "%d", &unk[i]);
-	                        if (unk[i] == 10001)
-	                                break;
-	                        messages[i] = frombase64(strtok(NULL, ":"));
-	                }
-	                tmp = frombase64(strtok(NULL, ":"));
 			ft = g_new0(struct file_transfer, 1);
-
 			ft->cookie = g_strdup(cookie);
 			ft->ip = g_strdup(pip);
 			ft->port = port;
-			if (i)
-				ft->message = g_strdup(messages[0]);
-			else
-				ft->message = NULL;
+			if (i) ft->message = g_strdup(messages[0]);
+			else ft->message = NULL;
+			ft->filename = g_strdup(name);
 			ft->user = g_strdup(user);
-			sprintf(ft->UID, "%s", FILE_GET_UID);
+			ft->size = totalsize;
+			g_snprintf(ft->UID, sizeof(ft->UID), "%s", FILE_SEND_UID);
 			ft->gc = gc;
 
 			g_free(tmp);
+			for (i--; i >= 0; i--) g_free(messages[i]);
 
-			for (i--; i >= 0; i--)
-				g_free(messages[i]);
+			accept_file_dialog(ft);
+		} else if (!strcmp(uuid, FILE_GET_UID)) {
+			/* they want us to send a file */
+			int unk[4], i;
+			char *messages[4], *tmp;
+			struct file_transfer *ft;
+
+			for (i = 0; i < 4; i++) {
+				sscanf(strtok(NULL, ":"), "%d", unk + i);
+				if (unk[i] == 10001) break;
+				messages[i] = frombase64(strtok(NULL, ":"));
+			}
+			tmp = frombase64(strtok(NULL, ":"));
+
+			ft = g_new0(struct file_transfer, 1);
+			ft->cookie = g_strdup(cookie);
+			ft->ip = g_strdup(pip);
+			ft->port = port;
+			if (i) ft->message = g_strdup(messages[0]);
+			else ft->message = NULL;
+			ft->user = g_strdup(user);
+			g_snprintf(ft->UID, sizeof(ft->UID), "%s", FILE_GET_UID);
+			ft->gc = gc;
+
+			g_free(tmp);
+			for (i--; i >= 0; i--) g_free(messages[i]);
 
 			accept_file_dialog(ft);
 		} else if (!strcmp(uuid, VOICE_UID)) {
 			/* oh goody. voice over ip. fun stuff. */
-
-		/*
 		} else if (!strcmp(uuid, B_ICON_UID)) {
+			/* buddy icon... */
 		} else if (!strcmp(uuid, IMAGE_UID)) {
-		*/
-
+			/* aka Direct IM */
 		} else {
-			sprintf(debug_buff,"don't know what to do with %s\n",
-					uuid);
-			debug_print(debug_buff);
-			tmp = g_malloc(BUF_LEN);
-			name = frombase64(cookie);
-			snprintf(tmp, BUF_LEN, "toc_rvous_cancel %s %s %s",
-					user, name, uuid);
-			sflap_send(gc, tmp, strlen(tmp), TYPE_DATA);
-			free(name);
-			free(tmp);
+			debug_printf("Don't know what to do with RVOUS UUID %s\n", uuid);
+			/* do we have to do anything here? i think it just times out */
 		}
 	} else {
-		sprintf(debug_buff,"don't know what to do with %s\n", c);
-		debug_print(debug_buff);
-	}
-        g_free(buf);
-}
-
-
-int toc_signon(struct gaim_connection *gc)
-{
-	char buf[BUF_LONG];
-	int res;
-	struct signon so;
-	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
-
-        sprintf(debug_buff,"State = %d\n", tdt->state);
-	debug_print(debug_buff);
-
-	if ((res = write(tdt->toc_fd, FLAPON, strlen(FLAPON))) < 0)
-		return res;
-	/* Wait for signon packet */
-
-	tdt->state = STATE_FLAPON;
-
-	if ((res = wait_reply(gc, buf, sizeof(buf)) < 0))
-		return res;
-	
-	if (tdt->state != STATE_SIGNON_REQUEST) {
-			sprintf(debug_buff, "State should be %d, but is %d instead\n", STATE_SIGNON_REQUEST, tdt->state);
-			debug_print(debug_buff);
-			return -1;
+		debug_printf("don't know what to do with %s\n", c);
 	}
-	
-	/* Compose a response */
-	
-	g_snprintf(so.username, sizeof(so.username), "%s", gc->username);
-	so.ver = ntohl(1);
-	so.tag = ntohs(1);
-	so.namelen = htons(strlen(so.username));	
-	
-	sflap_send(gc, (char *)&so, ntohs(so.namelen) + 8, TYPE_SIGNON);
-	
-	g_snprintf(buf, sizeof(buf), 
-		"toc_signon %s %d %s %s %s \"%s\"",
-		AUTH_HOST, AUTH_PORT, normalize(gc->username), roast_password(gc->password),
-		LANGUAGE, REVISION);
-
-        sprintf(debug_buff,"Send: %s\n", buf);
-		debug_print(debug_buff);
-
-	return sflap_send(gc, buf, -1, TYPE_DATA);
-}
-
-int toc_wait_signon(struct gaim_connection *gc)
-{
-	/* Wait for the SIGNON to be approved */
-	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
-	char buf[BUF_LONG];
-	int res;
-	res = wait_reply(gc, buf, sizeof(buf));
-	if (res < 0)
-		return res;
-	if (tdt->state != STATE_SIGNON_ACK) {
-			sprintf(debug_buff, "State should be %d, but is %d instead\n",STATE_SIGNON_ACK, tdt->state);
-			debug_print(debug_buff);
-		return -1;
-	}
-	return 0;
-}
-
-#ifdef _WIN32
-gint win32_read()
-{
-        int ret;
-        struct fd_set fds;
-        struct timeval tv;
-
-        FD_ZERO(&fds);
-
-        tv.tv_sec = 0;
-        tv.tv_usec = 200;
-
-        FD_SET(toc_fd, &fds);
-
-        ret = select(toc_fd + 1, &fds, NULL, NULL, &tv);
-
-        if (ret == 0) {
-                return TRUE;
-        }
-
-        toc_callback(NULL, 0, (GdkInputCondition)0);
-        return TRUE;
-}
-#endif
-
-
-char *toc_wait_config(struct gaim_connection *gc)
-{
-	/* Waits for configuration packet, returning the contents of the packet */
-	struct toc_data *tdt = (struct toc_data *)gc->proto_data;
-	static char buf[BUF_LONG];
-	int res;
-	res = wait_reply(gc, buf, sizeof(buf));
-	if (res < 0)
-		return NULL;
-/* Apparently, the toc_config is optional.  *VERY* Optional
-*/
-	if (tdt->state != STATE_CONFIG) {
-		res = 0;
-	} else {
-		res = 1;
-	}
-	/* At this point, it's time to setup automatic handling of incoming packets */
-	tdt->state = STATE_ONLINE;
-#ifdef _WIN32
-	win32_r = gtk_timeout_add(1000, (GtkFunction)win32_read, NULL);
-#else
-	gc->inpa = gdk_input_add(tdt->toc_fd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION, toc_callback, gc);
-#endif
-	if (res)
-		return buf;
-	else
-		return NULL;
 }
 
 void toc_build_config(struct gaim_connection *gc, char *s, int len, gboolean show)
@@ -1022,6 +815,7 @@
 	char buf[1024]; 
 	g_snprintf(buf, sizeof(buf), "toc_add_buddy %s", normalize(name));
 	sflap_send(g, buf, -1, TYPE_DATA);
+	toc_set_config(g);
 }
 
 static void toc_add_buddies(struct gaim_connection *g, GList *buddies) {
@@ -1044,6 +838,7 @@
 	char buf[1024]; 
 	g_snprintf(buf, sizeof(buf), "toc_remove_buddy %s", normalize(name));
 	sflap_send(g, buf, -1, TYPE_DATA);
+	toc_set_config(g);
 }
 
 static void toc_set_idle(struct gaim_connection *g, int time) {