diff libpurple/protocols/qq/buddy_list.c @ 23683:1c50f12b1c52

2008.08.02 - csyfek <csyfek(at)gmail.com> * Commit to Pidgin * Tickets: Fixes #1861 Fixes #1902 References #5112 2008.08.02 - ccpaging <ecc_hy(at)hotmail.com> * Store all keys and md5 values of qq_data in char[QQ_KEY_LENGTH] * Use random value in inikey * TEA header padding in crypt.c * Rewrite login part of qq_process 2008.07.31 - ccpaging <ecc_hy(at)hotmail.com> * Fixed: send reply when get duplicate server command. The server may not get our reply before. * Tag custom picture as text "(Broken)" 2008.07.30 - ccpaging <ecc_hy(at)hotmail.com>, csyfek <csyfek(at)gmail.com> * Change some debug message * Modify buddy status flag according to eva for QQ2006 * Modify buddy status parse and correspond to eva2 * Add getIP/putIP functions to packet_parse.c, and replace some gen_ip_str * Replace guint32 *ip with struct in_addr, and reduce g_new/g_free operation * Source file changed: Merge buddy_status into buddy_list Change login_logout to qq_base Merge keep_alive into qq_base New qq_process extract from qq_network * Fixed: Byte alignment bug in crypt.c, tested in ARM PDA * Fixed: group chat message may get in before getting group info, and so group info is empty * Add qq_send_cmd_group_get_group_info when joined a group chat in group_im.c * Add some new group command identify according eva but further program * Add some new QQ client version identify * Fixed: Identify buddy's client version by IM packet, and not by status * Add some new info in buddy's tooltip text * Add video falg to buddy's emblem. But those flag in buddy status may not prasing correctly * Use new timeout function to handle send keep_alive, resend packet, update buddy status * Add new advanced options: The end user may change interval of keep_alive, resend packet, update buddy status to feed their need. For example, saving network flow when use mobile phone. Keep alive packet must be sent in 60-120 seconds whatever client rcved data of not. The intervals of keep alive and update status should be multiple of resend's interval, Since we use counter not time() in a single timeout function for efficiency. * Rewrite qq_trans.c, and use one g_list to manage: Store server packet before login, and prase all of them when get login Store client send packet for resend scanning, confirm server reply, filter duplicate server reply Store server packet for filter out duplicate * Add QQ_MSG_SYS_NOTICE = 0x06 in sys_msg.c * Rewrite qq_proc_cmd_reply and qq_proc_cmd_server: In QQ protocol, one packet reply may need a new packet send later. We may call it packet trigger. The triggers always is hided in every qq_process_reply. Now we try to extract those triggers and put into a single function, and then every trigger should be obviously and easy to manage.
author SHiNE CsyFeK <csyfek@gmail.com>
date Sat, 02 Aug 2008 15:00:46 +0000
parents bdb38a8bf721
children 5f454b975a99
line wrap: on
line diff
--- a/libpurple/protocols/qq/buddy_list.c	Sat Aug 02 02:40:46 2008 +0000
+++ b/libpurple/protocols/qq/buddy_list.c	Sat Aug 02 15:00:46 2008 +0000
@@ -32,12 +32,11 @@
 #include "packet_parse.h"
 #include "buddy_info.h"
 #include "buddy_list.h"
-#include "buddy_status.h"
 #include "buddy_opt.h"
 #include "char_conv.h"
 #include "crypt.h"
 #include "header_info.h"
-#include "keep_alive.h"
+#include "qq_base.h"
 #include "group.h"
 #include "group_find.h"
 #include "group_internal.h"
@@ -48,16 +47,14 @@
 #define QQ_GET_ONLINE_BUDDY_02          0x02
 #define QQ_GET_ONLINE_BUDDY_03          0x03	/* unknown function */
 
-#define QQ_ONLINE_BUDDY_ENTRY_LEN       38
-
-typedef struct _qq_friends_online_entry {
-	qq_buddy_status *s;
+typedef struct _qq_buddy_online {
+	qq_buddy_status bs;
 	guint16 unknown1;
-	guint8 flag1;
+	guint8 ext_flag;
 	guint8 comm_flag;
 	guint16 unknown2;
 	guint8 ending;		/* 0x00 */
-} qq_friends_online_entry;
+} qq_buddy_online;
 
 /* get a list of online_buddies */
 void qq_send_packet_get_buddies_online(PurpleConnection *gc, guint8 position)
@@ -81,7 +78,7 @@
 	/* 003-004 */
 	bytes += qq_put16(raw_data + bytes, 0x0000);
 
-	qq_send_cmd(qd, QQ_CMD_GET_FRIENDS_ONLINE, raw_data, 5);
+	qq_send_cmd(qd, QQ_CMD_GET_BUDDIES_ONLINE, raw_data, 5);
 	qd->last_get_online = time(NULL);
 }
 
@@ -102,7 +99,7 @@
 	 * March 22, found the 00,00,00 starts to work as well */
 	bytes += qq_put8(raw_data + bytes, 0x00);
 
-	qq_send_cmd(qd, QQ_CMD_GET_FRIENDS_LIST, raw_data, bytes);
+	qq_send_cmd(qd, QQ_CMD_GET_BUDDIES_LIST, raw_data, bytes);
 }
 
 /* get all list, buddies & Quns with groupsid support */
@@ -123,27 +120,45 @@
 	qq_send_cmd(qd, QQ_CMD_GET_ALL_LIST_WITH_GROUP, raw_data, bytes);
 }
 
-static void _qq_buddies_online_reply_dump_unclear(qq_friends_online_entry *fe)
+/* parse the data into qq_buddy_status */
+static gint get_buddy_status(qq_buddy_status *bs, guint8 *data)
 {
-	GString *dump;
+	gint bytes = 0;
 
-	g_return_if_fail(fe != NULL);
-
-	qq_buddy_status_dump_unclear(fe->s);
+	g_return_val_if_fail(data != NULL && bs != NULL, -1);
 
-	dump = g_string_new("");
-	g_string_append_printf(dump, "unclear fields for [%d]:\n", fe->s->uid);
-	g_string_append_printf(dump, "031-032: %04x (unknown)\n", fe->unknown1);
-	g_string_append_printf(dump, "033:     %02x   (flag1)\n", fe->flag1);
-	g_string_append_printf(dump, "034:     %02x   (comm_flag)\n", fe->comm_flag);
-	g_string_append_printf(dump, "035-036: %04x (unknown)\n", fe->unknown2);
+	/* 000-003: uid */
+	bytes += qq_get32(&bs->uid, data + bytes);
+	/* 004-004: 0x01 */
+	bytes += qq_get8(&bs->unknown1, data + bytes);
+	/* this is no longer the IP, it seems QQ (as of 2006) no longer sends
+	 * the buddy's IP in this packet. all 0s */
+	/* 005-008: ip */
+	bytes += qq_getIP(&bs->ip, data + bytes);
+	/* port info is no longer here either */
+	/* 009-010: port */
+	bytes += qq_get16(&bs->port, data + bytes);
+	/* 011-011: 0x00 */
+	bytes += qq_get8(&bs->unknown2, data + bytes);
+	/* 012-012: status */
+	bytes += qq_get8(&bs->status, data + bytes);
+	/* 013-014: client_version */
+	bytes += qq_get16(&bs->unknown3, data + bytes);
+	/* 015-030: unknown key */
+	bytes += qq_getdata(&(bs->unknown_key[0]), QQ_KEY_LENGTH, data + bytes);
 
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Online buddy entry, %s", dump->str);
-	g_string_free(dump, TRUE);
+	purple_debug(PURPLE_DEBUG_INFO, "QQ_STATUS", 
+			"uid: %d, U1: %d, ip: %s:%d, U2:%d, status:%d, U3:%04X\n", 
+			bs->uid, bs->unknown1, inet_ntoa(bs->ip), bs->port,
+			bs->unknown2, bs->status, bs->unknown3);
+
+	return bytes;
 }
 
+#define QQ_ONLINE_BUDDY_ENTRY_LEN       38
+
 /* process the reply packet for get_buddies_online packet */
-void qq_process_get_buddies_online_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
+guint8 qq_process_get_buddies_online_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
 {
 	qq_data *qd;
 	gint len, bytes, bytes_buddy;
@@ -151,9 +166,9 @@
 	guint8 *data, position;
 	PurpleBuddy *b;
 	qq_buddy *q_bud;
-	qq_friends_online_entry *fe;
+	qq_buddy_online bo;
 
-	g_return_if_fail(buf != NULL && buf_len != 0);
+	g_return_val_if_fail(buf != NULL && buf_len != 0, -1);
 
 	qd = (qq_data *) gc->proto_data;
 	len = buf_len;
@@ -163,70 +178,68 @@
 
 	if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) {
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies online");
-		return;
+		return -1;
 	}
 
-	qq_show_packet("Get buddies online reply packet", data, len);
+	/* qq_show_packet("Get buddies online reply packet", data, len); */
 
 	bytes = 0;
 	bytes += qq_get8(&position, data + bytes);
 
-	fe = g_newa(qq_friends_online_entry, 1);
-	fe->s = g_newa(qq_buddy_status, 1);
-
 	count = 0;
 	while (bytes < len) {
+		if (len - bytes < QQ_ONLINE_BUDDY_ENTRY_LEN) {
+			purple_debug(PURPLE_DEBUG_ERROR, "QQ", 
+					"[buddies online] only %d, need %d", 
+					(len - bytes), QQ_ONLINE_BUDDY_ENTRY_LEN);
+			break;
+		}
+		memset(&bo, 0 ,sizeof(bo));
+		
 		/* set flag */
 		bytes_buddy = bytes;
 		/* based on one online buddy entry */
-		/* ATTTENTION! NEWED in the sub function, but FREED here */
 		/* 000-030 qq_buddy_status */
-		bytes += qq_buddy_status_read(fe->s, data + bytes);
-		/* 031-032: unknown4 */
-		bytes += qq_get16(&fe->unknown1, data + bytes);
-		/* 033-033: flag1 */
-		bytes += qq_get8(&fe->flag1, data + bytes);
+		bytes += get_buddy_status(&(bo.bs), data + bytes);
+		/* 031-032: */
+		bytes += qq_get16(&bo.unknown1, data + bytes);
+		/* 033-033: ext_flag */
+		bytes += qq_get8(&bo.ext_flag, data + bytes);
 		/* 034-034: comm_flag */
-		bytes += qq_get8(&fe->comm_flag, data + bytes);
+		bytes += qq_get8(&bo.comm_flag, data + bytes);
 		/* 035-036: */
-		bytes += qq_get16(&fe->unknown2, data + bytes);
+		bytes += qq_get16(&bo.unknown2, data + bytes);
 		/* 037-037: */
-		bytes += qq_get8(&fe->ending, data + bytes);	/* 0x00 */
+		bytes += qq_get8(&bo.ending, data + bytes);	/* 0x00 */
 
-		if (fe->s->uid == 0 || (bytes - bytes_buddy) != QQ_ONLINE_BUDDY_ENTRY_LEN) {
+		if (bo.bs.uid == 0 || (bytes - bytes_buddy) != QQ_ONLINE_BUDDY_ENTRY_LEN) {
 			purple_debug(PURPLE_DEBUG_ERROR, "QQ", 
 					"uid=0 or entry complete len(%d) != %d", 
 					(bytes - bytes_buddy), QQ_ONLINE_BUDDY_ENTRY_LEN);
-			g_free(fe->s->ip);
-			g_free(fe->s->unknown_key);
 			continue;
 		}	/* check if it is a valid entry */
 
-		if (QQ_DEBUG) {
-			_qq_buddies_online_reply_dump_unclear(fe);
-		}
-
 		/* update buddy information */
-		b = purple_find_buddy(purple_connection_get_account(gc), uid_to_purple_name(fe->s->uid));
+		b = purple_find_buddy(purple_connection_get_account(gc), 
+												uid_to_purple_name(bo.bs.uid) );
 		q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data;
-
-		if (q_bud != NULL) {	/* we find one and update qq_buddy */
-			if(0 != fe->s->client_version)
-				q_bud->client_version = fe->s->client_version;
-			g_memmove(q_bud->ip, fe->s->ip, 4);
-			q_bud->port = fe->s->port;
-			q_bud->status = fe->s->status;
-			q_bud->flag1 = fe->flag1;
-			q_bud->comm_flag = fe->comm_flag;
-			qq_update_buddy_contact(gc, q_bud);
-			count++;
-		} else {
+		if (q_bud == NULL) {
 			purple_debug(PURPLE_DEBUG_ERROR, "QQ", 
-					"Got an online buddy %d, but not in my buddy list\n", fe->s->uid);
+					"Got an online buddy %d, but not in my buddy list\n", bo.bs.uid);
+			continue;
 		}
-
-		g_free(fe->s->ip);
-		g_free(fe->s->unknown_key);
+		/* we find one and update qq_buddy */
+		/*
+		if(0 != fe->s->client_version)
+			q_bud->client_version = fe->s->client_version;
+		*/
+		q_bud->ip.s_addr = bo.bs.ip.s_addr;
+		q_bud->port = bo.bs.port;
+		q_bud->status = bo.bs.status;
+		q_bud->ext_flag = bo.ext_flag;
+		q_bud->comm_flag = bo.comm_flag;
+		qq_update_buddy_contact(gc, q_bud);
+		count++;
 	}
 
 	if(bytes > len) {
@@ -236,20 +249,12 @@
 
 	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Received %d online buddies, nextposition=%u\n",
 							count, (guint) position);
-	if (position != QQ_FRIENDS_ONLINE_POSITION_END
-		  && position != QQ_FRIENDS_ONLINE_POSITION_START) {
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "Requesting for more online buddies\n"); 
-		qq_send_packet_get_buddies_online(gc, position);
-	} else {
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "All online buddies received\n"); 
-		qq_send_packet_get_buddies_levels(gc);
-		qq_refresh_all_buddy_status(gc);
-	}
+	return position;
 }
 
 
 /* process reply for get_buddies_list */
-void qq_process_get_buddies_list_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
+guint16 qq_process_get_buddies_list_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
 {
 	qq_data *qd;
 	qq_buddy *q_bud;
@@ -260,7 +265,7 @@
 	gchar *name;
 	PurpleBuddy *b;
 
-	g_return_if_fail(buf != NULL && buf_len != 0);
+	g_return_val_if_fail(buf != NULL && buf_len != 0, -1);
 
 	qd = (qq_data *) gc->proto_data;
 	len = buf_len;
@@ -268,7 +273,7 @@
 
 	if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) {
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies list");
-		return;
+		return -1;
 	}
 	bytes = 0;
 	bytes += qq_get16(&position, data + bytes);
@@ -291,16 +296,7 @@
 		bytes += pascal_len;
 
 		bytes += qq_get16(&unknown, data + bytes);
-		/* flag1: (0-7)
-		 *        bit1 => qq show
-		 * comm_flag: (0-7)
-		 *        bit1 => member
-		 *        bit4 => TCP mode
-		 *        bit5 => open mobile QQ
-		 *        bit6 => bind to mobile
-		 *        bit7 => whether having a video
-		 */
-		bytes += qq_get8(&q_bud->flag1, data + bytes);
+		bytes += qq_get8(&q_bud->ext_flag, data + bytes);
 		bytes += qq_get8(&q_bud->comm_flag, data + bytes);
 
 		bytes_expected = 12 + pascal_len;
@@ -317,8 +313,8 @@
 
 		if (QQ_DEBUG) {
 			purple_debug(PURPLE_DEBUG_INFO, "QQ",
-					"buddy [%09d]: flag1=0x%02x, comm_flag=0x%02x, nick=%s\n",
-					q_bud->uid, q_bud->flag1, q_bud->comm_flag, q_bud->nickname);
+					"buddy [%09d]: ext_flag=0x%02x, comm_flag=0x%02x, nick=%s\n",
+					q_bud->uid, q_bud->ext_flag, q_bud->comm_flag, q_bud->nickname);
 		}
 
 		name = uid_to_purple_name(q_bud->uid);
@@ -341,17 +337,10 @@
 
 	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Received %d buddies, nextposition=%u\n",
 		count, (guint) position);
-	if (position != QQ_FRIENDS_LIST_POSITION_START
-		&& position != QQ_FRIENDS_LIST_POSITION_END) { 
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "Requesting for more buddies\n"); 
-		qq_send_packet_get_buddies_list(gc, position);
-	} else {
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "All buddies received. Requesting for online buddies list\n");
-		qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_LIST_POSITION_START); 
-	}
+	return position;
 }
 
-void qq_process_get_all_list_with_group_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
+guint32 qq_process_get_all_list_with_group_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
 {
 	qq_data *qd;
 	gint len, i, j;
@@ -363,7 +352,7 @@
 	guint8 type, groupid;
 	qq_group *group;
 
-	g_return_if_fail(buf != NULL && buf_len != 0);
+	g_return_val_if_fail(buf != NULL && buf_len != 0, -1);
 
 	qd = (qq_data *) gc->proto_data;
 	len = buf_len;
@@ -371,11 +360,11 @@
 
 	if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) {
 		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt all list with group");
-		return;
+		return -1;
 	}
 
 	bytes += qq_get8(&sub_cmd, data + bytes);
-	g_return_if_fail(sub_cmd == 0x01);
+	g_return_val_if_fail(sub_cmd == 0x01, -1);
 
 	bytes += qq_get8(&reply_code, data + bytes);
 	if(0 != reply_code) {
@@ -429,14 +418,273 @@
 				"qq_process_get_all_list_with_group_reply: Dangerous error! maybe protocol changed, notify developers!");
 	}
 
-	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Get all list done, %d buddies and %d Quns\n", i, j);
 	purple_debug(PURPLE_DEBUG_INFO, "QQ", "Received %d buddies and %d groups, nextposition=%u\n", i, j, (guint) position);
+	return position;
+}
+
+#define QQ_MISC_STATUS_HAVING_VIIDEO      0x00000001
+#define QQ_CHANGE_ONLINE_STATUS_REPLY_OK 	0x30	/* ASCII value of "0" */
+
+/* TODO: figure out what's going on with the IP region. Sometimes I get valid IP addresses, 
+ * but the port number's weird, other times I get 0s. I get these simultaneously on the same buddy, 
+ * using different accounts to get info. */
+
+/* check if status means online or offline */
+gboolean is_online(guint8 status)
+{
+	switch(status) {
+		case QQ_BUDDY_ONLINE_NORMAL:
+		case QQ_BUDDY_ONLINE_AWAY:
+		case QQ_BUDDY_ONLINE_INVISIBLE:
+			return TRUE;
+		case QQ_BUDDY_ONLINE_OFFLINE:
+			return FALSE;
+	}
+	return FALSE;
+}
+
+/* Help calculate the correct icon index to tell the server. */
+gint get_icon_offset(PurpleConnection *gc)
+{ 
+	PurpleAccount *account;
+	PurplePresence *presence; 
+
+	account = purple_connection_get_account(gc);
+	presence = purple_account_get_presence(account);
+
+	if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_INVISIBLE)) {
+		return 2;
+	} else if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_AWAY)
+			|| purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_EXTENDED_AWAY)
+			|| purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_UNAVAILABLE)) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+/* send a packet to change my online status */
+void qq_send_packet_change_status(PurpleConnection *gc)
+{
+	qq_data *qd;
+	guint8 raw_data[16] = {0};
+	gint bytes = 0;
+	guint8 away_cmd;
+	guint32 misc_status;
+	gboolean fake_video;
+	PurpleAccount *account;
+	PurplePresence *presence; 
+
+	account = purple_connection_get_account(gc);
+	presence = purple_account_get_presence(account);
 
-	if (position != QQ_FRIENDS_ALL_LIST_POSITION_START
-		&& position != QQ_FRIENDS_ALL_LIST_POSITION_END) {
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "Requesting for more buddies and groups\n");
-		qq_send_packet_get_all_list_with_group(gc, position);
+	qd = (qq_data *) gc->proto_data;
+	if (!qd->logged_in)
+		return;
+
+	if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_INVISIBLE)) {
+		away_cmd = QQ_BUDDY_ONLINE_INVISIBLE;
+	} else if (purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_AWAY)
+			|| purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_EXTENDED_AWAY)
+			|| purple_presence_is_status_primitive_active(presence, PURPLE_STATUS_UNAVAILABLE)) {
+		away_cmd = QQ_BUDDY_ONLINE_AWAY;
 	} else {
-		purple_debug(PURPLE_DEBUG_INFO, "QQ", "All buddies and groups received\n"); 
+		away_cmd = QQ_BUDDY_ONLINE_NORMAL;
+	}
+
+	misc_status = 0x00000000;
+	fake_video = purple_prefs_get_bool("/plugins/prpl/qq/show_fake_video");
+	if (fake_video)
+		misc_status |= QQ_MISC_STATUS_HAVING_VIIDEO;
+
+	bytes = 0;
+	bytes += qq_put8(raw_data + bytes, away_cmd);
+	bytes += qq_put32(raw_data + bytes, misc_status);
+
+	qq_send_cmd(qd, QQ_CMD_CHANGE_ONLINE_STATUS, raw_data, bytes);
+}
+
+/* parse the reply packet for change_status */
+void qq_process_change_status_reply(guint8 *buf, gint buf_len, PurpleConnection *gc)
+{
+	qq_data *qd;
+	gint len, bytes;
+	guint8 *data, reply;
+	PurpleBuddy *b;
+	qq_buddy *q_bud;
+	gchar *name;
+
+	g_return_if_fail(buf != NULL && buf_len != 0);
+
+	qd = (qq_data *) gc->proto_data;
+	len = buf_len;
+	data = g_newa(guint8, len);
+
+	if ( !qq_decrypt(buf, buf_len, qd->session_key, data, &len) ) {
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt chg status reply\n");
+		return;
+	}
+
+	bytes = 0;
+	bytes = qq_get8(&reply, data + bytes);
+	if (reply != QQ_CHANGE_ONLINE_STATUS_REPLY_OK) {
+		purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Change status fail 0x%02X\n", reply);
+		return;
+	}
+
+	/* purple_debug(PURPLE_DEBUG_INFO, "QQ", "Change status OK\n"); */
+	name = uid_to_purple_name(qd->uid);
+	b = purple_find_buddy(gc->account, name);
+	g_free(name);
+	q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data;
+	if (q_bud != NULL) {
+		qq_update_buddy_contact(gc, q_bud);
 	}
 }
+
+/* it is a server message indicating that one of my buddies has changed its status */
+void qq_process_buddy_change_status(guint8 *buf, gint buf_len, PurpleConnection *gc) 
+{
+	qq_data *qd;
+	gint bytes;
+	guint32 my_uid;
+	guint8 *data;
+	gint data_len;
+	PurpleBuddy *b;
+	qq_buddy *q_bud;
+	qq_buddy_status bs;
+	gchar *name;
+
+	g_return_if_fail(buf != NULL && buf_len != 0);
+
+	qd = (qq_data *) gc->proto_data;
+	data_len = buf_len;
+	data = g_newa(guint8, data_len);
+
+	if ( !qq_decrypt(buf, buf_len, qd->session_key, data, &data_len) ) {
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[buddy status change] Failed decrypt\n");
+		return;
+	}
+
+	if (data_len < 35) {
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "[buddy status change] only %d, need 35 bytes\n", data_len);
+		return;
+	}
+	
+	memset(&bs, 0, sizeof(bs));
+	bytes = 0;
+	/* 000-030: qq_buddy_status */
+	bytes += get_buddy_status(&bs, data + bytes);
+	/* 031-034:  Unknow, maybe my uid */ 
+	/* This has a value of 0 when we've changed our status to 
+	 * QQ_BUDDY_ONLINE_INVISIBLE */
+	bytes += qq_get32(&my_uid, data + bytes);
+
+	name = uid_to_purple_name(bs.uid);
+	b = purple_find_buddy(gc->account, name);
+	g_free(name);
+	q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data;
+	if (q_bud == NULL) {
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", 
+				"got information of unknown buddy %d\n", bs.uid);
+		return;
+	}
+
+	purple_debug(PURPLE_DEBUG_INFO, "QQ", "status:.uid = %d, q_bud->uid = %d\n", bs.uid , q_bud->uid);
+	if(bs.ip.s_addr != 0) { 
+		q_bud->ip.s_addr = bs.ip.s_addr;
+		q_bud->port = bs.port;
+	}
+	q_bud->status =bs.status;
+
+	if (q_bud->status == QQ_BUDDY_ONLINE_NORMAL) {
+		qq_send_packet_get_level(gc, q_bud->uid);
+	}
+	qq_update_buddy_contact(gc, q_bud);
+}
+
+/*TODO: maybe this should be qq_update_buddy_status() ?*/
+void qq_update_buddy_contact(PurpleConnection *gc, qq_buddy *q_bud)
+{
+	gchar *name;
+	PurpleBuddy *bud;
+	gchar *status_id;
+	
+	g_return_if_fail(q_bud != NULL);
+
+	name = uid_to_purple_name(q_bud->uid);
+	bud = purple_find_buddy(gc->account, name);
+
+	if (bud == NULL) {
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "unknown buddy: %d\n", q_bud->uid);
+		g_free(name);
+		return;
+	}
+	
+	purple_blist_server_alias_buddy(bud, q_bud->nickname); /* server */
+	q_bud->last_refresh = time(NULL);
+
+	/* purple supports signon and idle time
+	 * but it is not much use for QQ, I do not use them */
+	/* serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc); */
+	status_id = "available";
+	switch(q_bud->status) {
+	case QQ_BUDDY_OFFLINE:
+		status_id = "offline";
+		break;
+	case QQ_BUDDY_ONLINE_NORMAL:
+		status_id = "available";
+		break;
+	case QQ_BUDDY_ONLINE_OFFLINE:
+		status_id = "offline";
+		break;
+	case QQ_BUDDY_ONLINE_AWAY:
+		status_id = "away";
+		break;
+	case QQ_BUDDY_ONLINE_INVISIBLE:
+		status_id = "invisible";
+		break;
+	default:
+		status_id = "invisible";
+		purple_debug(PURPLE_DEBUG_ERROR, "QQ", "unknown status: %x\n", q_bud->status);
+		break;
+	}
+	purple_debug(PURPLE_DEBUG_INFO, "QQ", "buddy %d %s\n", q_bud->uid, status_id);
+	purple_prpl_got_user_status(gc->account, name, status_id, NULL);
+
+	if (q_bud->comm_flag & QQ_COMM_FLAG_MOBILE && q_bud->status != QQ_BUDDY_OFFLINE)
+		purple_prpl_got_user_status(gc->account, name, "mobile", NULL);
+	else
+		purple_prpl_got_user_status_deactive(gc->account, name, "mobile");
+
+	if (q_bud->comm_flag & QQ_COMM_FLAG_VIDEO && q_bud->status != QQ_BUDDY_OFFLINE)
+		purple_prpl_got_user_status(gc->account, name, "video", NULL);
+	else
+		purple_prpl_got_user_status_deactive(gc->account, name, "video");
+
+	g_free(name);
+}
+
+/* refresh all buddies online/offline,
+ * after receiving reply for get_buddies_online packet */
+void qq_refresh_all_buddy_status(PurpleConnection *gc)
+{
+	time_t now;
+	GList *list;
+	qq_data *qd;
+	qq_buddy *q_bud;
+
+	qd = (qq_data *) (gc->proto_data);
+	now = time(NULL);
+	list = qd->buddies;
+
+	while (list != NULL) {
+		q_bud = (qq_buddy *) list->data;
+		if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL
+				&& q_bud->status != QQ_BUDDY_ONLINE_INVISIBLE) {
+			q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
+			qq_update_buddy_contact(gc, q_bud);
+		}
+		list = list->next;
+	}
+}