Index: protocols/yahoo/libyahoo2.c
===================================================================
--- protocols/yahoo/libyahoo2.c	(revision 4fefb772f530cd15ef3e92606532d3c6b193d96b)
+++ protocols/yahoo/libyahoo2.c	(revision c3e349e0847b5b936d1040c56ea427a7f2ce0d7c)
@@ -92,7 +92,4 @@
 
 static void yahoo_process_auth_response(struct http_request *req);
-
-/* What's this used for actually? */
-static void _yahoo_http_connected(int id, int fd, int error, void *data);
 
 #ifdef USE_STRUCT_CALLBACKS
@@ -1533,4 +1530,5 @@
 			break;
 		case 65:
+			g_free(cur_group);
 			cur_group = strdup(pair->value);
 			break;
@@ -1551,4 +1549,6 @@
 		}
 	}
+	
+	g_free(cur_group);
 
 	/* we could be getting multiple packets here */
@@ -1557,5 +1557,4 @@
 
 	YAHOO_CALLBACK(ext_yahoo_got_buddies)(yd->client_id, yd->buddies);
-
 
 	/*** We login at the very end of the packet communication */
@@ -1637,5 +1636,4 @@
 			} 
 
-			// if(yd->cookie_y && yd->cookie_t && yd->cookie_c)
 			if(yd->cookie_y && yd->cookie_t)
 				YAHOO_CALLBACK(ext_yahoo_got_cookies)(yd->client_id);
@@ -3645,5 +3643,5 @@
 }
 
-//#define LOG(x...) printf x
+/* #define LOG(x...) printf x */
 
 static void yahoo_process_auth_response(struct http_request *req)
@@ -3656,13 +3654,20 @@
 	int is_ymsgr = 0;
 
-	struct yahoo_data *yd = NULL;
 	struct yahoo_input_data *yid = req->data;
 
-	unsigned char crypt_hash[25];
+	char crypt_hash[25];
 
 	md5_byte_t result[16];
 	md5_state_t ctx;
 
-	struct yahoo_packet *packet =NULL;
+	struct yahoo_packet *packet = NULL;
+	
+	if (y_list_find(inputs, yid) == NULL)
+		return;
+	
+	if (req->status_code != 200) {
+		error_code = 3000 + req->status_code;
+		goto FAIL;
+	}
 
 	token = req->reply_body;
@@ -3672,52 +3677,44 @@
 		*line_end = '\0';
 
-		line_end+=2;
-	}
-
-	error_code = atoi((char *)token);
+		line_end += 2;
+	}
+	
+	if (sscanf(token, "%d", &error_code) != 1) {
+		error_code = 3000;
+		goto FAIL;
+	}
 
 	switch(error_code) {
 		case 0:
 			/* successful */
-			LOG(("successful\n"));
-			break;
+			break;
+
 		case 1212:
-			/* Incorrect ID or password */
 			LOG(("Incorrect ID or password\n"));
-
-			return;
+			error_code = YAHOO_LOGIN_PASSWD;
+			goto FAIL;
+
 		case 1213:
-			/* Security lock from too many failed login attempts */
 			LOG(("Security lock from too many failed login attempts\n"));
-
-			return;
+			error_code = YAHOO_LOGIN_LOCK;
+			goto FAIL;
 
 		case 1214:
-			/* Security lock */
 			LOG(("Security lock\n"));
-
-			return;
+			goto FAIL;
 
 		case 1235:
-			/* User ID not taken yet */
 			LOG(("User ID not taken yet\n"));
-
-			return;
+			error_code = YAHOO_LOGIN_UNAME;
+			goto FAIL;
 
 		case 1216:
-			/* Seems to be a lock, but shows the same generic User ID/Password failure */
 			LOG(("Seems to be a lock, but shows the same generic User ID/Password failure\n"));
-
-			return;
-		case 100:
-			/* Username and password cannot be blank */
-			LOG(("Username and password cannot be blank\n"));
-
-			return;
+			goto FAIL;
+
 		default:
 			/* Unknown error code */
 			LOG(("Unknown Error\n"));
-
-			return;
+			goto FAIL;
 	}
 
@@ -3728,6 +3725,6 @@
 		LOG(("Oops! There was no ymsgr=. Where do I get my token from now :("));
 		LOG(("I got this:\n\n%s\n",line_end));
-		return;
-		/* Error */
+		error_code = 2201;
+		goto FAIL;
 	}
 
@@ -3738,5 +3735,5 @@
 	if(line_end) {
 		*line_end = '\0';
-		line_end+=2;
+		line_end += 2;
 	}
 
@@ -3748,6 +3745,7 @@
 			"https://login.yahoo.com/config/pwtoken_login?"
 			"src=ymsgr&ts=&token=%s", token);
-        	
+
 		http_dorequest_url(url, yahoo_process_auth_response, yid);
+
         	g_free(url);
 
@@ -3761,37 +3759,27 @@
 	}
 
-	cookie = strstr(req->reply_headers, "Set-Cookie: Y=");
-
-	if(!cookie) {
+	if((cookie = strstr(req->reply_headers, "Set-Cookie: Y=")) &&
+	   (line_end = strstr(cookie + 14, "\r\n"))) {
+		*line_end = '\0';
+		yid->yd->cookie_y = strdup(cookie + 14);
+		*line_end = ';';
+	} else {
 		/* Cry. */
 		LOG(("NO Y Cookie!"));
-		return;
-	}
-
-	cookie+=14;
-
-	line_end = strstr(cookie, "\r\n");
-	*line_end = '\0';
-
-	LOG(("Cookie length: %d", strlen(cookie)));
-
-	yid->yd->cookie_y = strdup(cookie);
-	*line_end = ';';
-
-	cookie = strstr(req->reply_headers, "Set-Cookie: T=");
-	if(!cookie) {
+		error_code = 2202;
+		goto FAIL;
+	}
+
+	if((cookie = strstr(req->reply_headers, "Set-Cookie: T=")) &&
+	   (line_end = strstr(cookie + 14, "\r\n"))) {
+		*line_end = '\0';
+		yid->yd->cookie_t = strdup(cookie + 14);
+		*line_end = ';';
+	} else {
+		/* Cry. */
 		LOG(("NO T Cookie!"));
-		/* Cry. */
-		return;
-	}
-
-	cookie+=14;
-
-	line_end = strstr(cookie, "\r\n");
-	*line_end = '\0';
-
-	yid->yd->cookie_t = strdup(cookie);
-
-	LOG(("My Cookies ::\n Y: %s\nT: %s\n\n", yid->yd->cookie_y, yid->yd->cookie_t));
+		error_code = 2203;
+		goto FAIL;
+	}
 
 	md5_init(&ctx);
@@ -3800,18 +3788,14 @@
 	md5_finish(&ctx, result);
 
-	to_y64(crypt_hash, result, 16);
-
-	yd = yid->yd;
-
-	//yid = find_input_by_id_and_type(yd->client_id, YAHOO_CONNECTION_PAGER);
-
-	packet = yahoo_packet_new(YAHOO_SERVICE_AUTHRESP, yd->initial_status, yd->session_id);
-	yahoo_packet_hash(packet, 1, yd->user);
-	yahoo_packet_hash(packet, 0, yd->user);
-	yahoo_packet_hash(packet, 277, yd->cookie_y);
-	yahoo_packet_hash(packet, 278, yd->cookie_t);
+	to_y64((unsigned char*)crypt_hash, result, 16);
+
+	packet = yahoo_packet_new(YAHOO_SERVICE_AUTHRESP, yid->yd->initial_status, yid->yd->session_id);
+	yahoo_packet_hash(packet, 1, yid->yd->user);
+	yahoo_packet_hash(packet, 0, yid->yd->user);
+	yahoo_packet_hash(packet, 277, yid->yd->cookie_y);
+	yahoo_packet_hash(packet, 278, yid->yd->cookie_t);
 	yahoo_packet_hash(packet, 307, crypt_hash);
 	yahoo_packet_hash(packet, 244, "2097087");	/* Rekkanoryo says this is the build number */
-	yahoo_packet_hash(packet, 2, yd->user);
+	yahoo_packet_hash(packet, 2, yid->yd->user);
 	yahoo_packet_hash(packet, 2, "1");
 	yahoo_packet_hash(packet, 98, "us");		/* TODO Put country code */
@@ -3823,6 +3807,11 @@
 
 	/* We don't need this anymore */
-	free(yd->login_cookie);
-	yd->login_cookie = NULL;
+	free(yid->yd->login_cookie);
+	yid->yd->login_cookie = NULL;
+	
+	return;
+
+FAIL:
+	YAHOO_CALLBACK(ext_yahoo_login_response)(yid->yd->client_id, error_code, NULL);
 }
 
