Index: skype/skype.c
===================================================================
--- skype/skype.c	(revision dd8163ebbde5b38fb59c6b73b304a24465b5d2b6)
+++ skype/skype.c	(revision 7daec06ebeafa481e57ef99f558cdf9f05f0d8c4)
@@ -1,9 +1,27 @@
-/* 
- * This is the most simple possible BitlBee plugin. To use, compile it as 
- * a shared library and place it in the plugin directory: 
+/*
+ *  skype.c - Skype plugin for BitlBee
+ * 
+ *  Copyright (c) 2007 by Miklos Vajna <vmiklos@frugalware.org>
  *
- * gcc -o example.so -shared example.c `pkg-config --cflags bitlbee`
- * cp example.so /usr/local/lib/bitlbee
+ *  Several ideas are used from the BitlBee Jabber plugin, which is
+ *
+ *  Copyright (c) 2006 by Wilmer van der Gaast <wilmer@gaast.net>
+ * 
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
+ *  USA.
  */
+
 #include <stdio.h>
 #include <poll.h>
@@ -11,4 +29,8 @@
 
 #define SKYPE_PORT_DEFAULT "2727"
+
+/*
+ * Structures
+ */
 
 struct skype_data
@@ -20,7 +42,7 @@
 	int tx_len;
 	int r_inpa, w_inpa;
-	// when we receive a new message id, we query the handle, then the body
-	// store the handle here
-	// TODO: it would be nicer to use a hashmap for this or something
+	/* When we receive a new message id, we query the handle, then the
+	 * body. Store the handle here so that we imcb_buddy_msg() when we got
+	 * the body. */
 	char *handle;
 };
@@ -31,4 +53,14 @@
 	char *full_name;
 };
+
+struct skype_buddy_ask_data
+{
+	struct im_connection *ic;
+	char *handle;
+};
+
+/*
+ * Tables
+ */
 
 const struct skype_away_state skype_away_state_list[] =
@@ -44,9 +76,7 @@
 };
 
-struct skype_buddy_ask_data
-{
-	struct im_connection *ic;
-	char *handle;
-};
+/*
+ * Functions
+ */
 
 static void skype_init( account_t *acc )
@@ -69,4 +99,6 @@
 	pfd[0].events = POLLOUT;
 
+	/* This poll is necessary or we'll get a SIGPIPE when we write() to
+	 * sd->fd. */
 	poll(pfd, 1, 1000);
 	if(pfd[0].revents & POLLHUP)
@@ -76,5 +108,4 @@
 		return FALSE;
 	}
-	printf("write(): %s", buf);
 	write( sd->fd, buf, len );
 
@@ -123,9 +154,10 @@
 	if( !sd || sd->fd == -1 )
 		return FALSE;
+	/* Read the whole data. */
 	st = read( sd->fd, buf, sizeof( buf ) );
 	if( st > 0 )
 	{
 		buf[st] = '\0';
-		printf("read(): '%s'\n", buf);
+		/* Then split it up to lines. */
 		lines = g_strsplit(buf, "\n", 0);
 		lineptr = lines;
@@ -134,5 +166,4 @@
 			if(!strlen(line))
 				break;
-			printf("skype_read_callback() new line: '%s'\n", line);
 			if(!strncmp(line, "USERS ", 6))
 			{
@@ -159,5 +190,7 @@
 				*ptr = '\0';
 				ptr++;
-				if(!strncmp(ptr, "ONLINESTATUS ", 13) && strcmp(user, sd->username) != 0 && strcmp(user, "echo123") != 0)
+				if(!strncmp(ptr, "ONLINESTATUS ", 13) &&
+						strcmp(user, sd->username) != 0
+						&& strcmp(user, "echo123") != 0)
 				{
 					ptr = g_strdup_printf("%s@skype.com", user);
@@ -197,6 +230,9 @@
 					if(!strcmp(info, "STATUS RECEIVED"))
 					{
-						// new message, request its body
-						printf("new received message  #%s\n", id);
+						/* New message ID:
+						 * (1) Request its from field
+						 * (2) Request its body
+						 * (3) Mark it as seen
+						 */
 						g_snprintf(buf, 1024, "GET CHATMESSAGE %s FROM_HANDLE\n", id);
 						skype_write( ic, buf, strlen( buf ) );
@@ -209,17 +245,20 @@
 					{
 						info += 12;
-						// new handle
+						/* New from field value. Store
+						 * it, then we can later use it
+						 * when we got the message's
+						 * body. */
 						sd->handle = g_strdup_printf("%s@skype.com", info);
-						printf("new handle: '%s'\n", info);
 					}
 					else if(!strncmp(info, "BODY ", 5))
 					{
 						info += 5;
-						// new body
-						printf("<%s> %s\n", sd->handle, info);
 						if(sd->handle)
+						{
+							/* New body, we have everything to use imcb_buddy_msg() now! */
 							imcb_buddy_msg(ic, sd->handle, info, 0, 0);
-						g_free(sd->handle);
-						sd->handle = NULL;
+							g_free(sd->handle);
+							sd->handle = NULL;
+						}
 					}
 				}
@@ -238,6 +277,4 @@
 		return FALSE;
 	}
-
-	/* EAGAIN/etc or a successful read. */
 	return TRUE;
 }
@@ -255,5 +292,5 @@
 		sd->r_inpa = b_input_add( sd->fd, GAIM_INPUT_READ, skype_read_callback, ic );
 
-	// download buddies
+	/* This will download all buddies. */
 	buf = g_strdup_printf("SEARCH FRIENDS\n");
 	st = skype_write( ic, buf, strlen( buf ) );
@@ -280,10 +317,5 @@
 
 	imcb_log( ic, "Connecting" );
-	printf("%s:%d\n", acc->server, set_getint( &acc->set, "port"));
 	sd->fd = proxy_connect(acc->server, set_getint( &acc->set, "port" ), skype_connected, ic );
-	printf("sd->fd: %d\n", sd->fd);
-	/*imcb_add_buddy(ic, "test@skype.com", NULL);
-	imcb_buddy_status(ic, "test@skype.com", OPT_LOGGED_IN, NULL, NULL);
-	imcb_buddy_msg(ic, "test@skype.com", "test from skype plugin", 0, 0);*/
 	sd->username = g_strdup( acc->user );
 
@@ -343,5 +375,4 @@
 	else
 		state = skype_away_state_by_name( state_txt );
-	printf("would set to: '%s'\n", state->code);
 	buf = g_strdup_printf("SET USERSTATUS %s\n", state->code);
 	skype_write( ic, buf, strlen( buf ) );
@@ -370,5 +401,4 @@
 	buf = g_strdup_printf("SET USER %s BUDDYSTATUS 2 Please authorize me\n", nick);
 	skype_write( ic, buf, strlen( buf ) );
-	printf("add '%s'\n", nick);
 	g_free(nick);
 }
@@ -384,5 +414,4 @@
 	buf = g_strdup_printf("SET USER %s BUDDYSTATUS 1\n", nick);
 	skype_write( ic, buf, strlen( buf ) );
-	printf("remove '%s'\n", nick);
 	g_free(nick);
 }
@@ -397,6 +426,6 @@
 	ret->logout = skype_logout;
 	ret->buddy_msg = skype_buddy_msg;
+	ret->away_states = skype_away_states;
 	ret->set_away = skype_set_away;
-	ret->away_states = skype_away_states;
 	ret->add_buddy = skype_add_buddy;
 	ret->remove_buddy = skype_remove_buddy;
