--- a/libpurple/protocols/qq/buddy_list.c Sat Jun 21 16:34:02 2008 +0000 +++ b/libpurple/protocols/qq/buddy_list.c Tue Jun 24 11:58:57 2008 +0000 @@ -64,23 +64,23 @@ void qq_send_packet_get_buddies_online(PurpleConnection *gc, guint8 position) { qq_data *qd; - guint8 *raw_data, *cursor; + guint8 *raw_data; + gint bytes = 0; qd = (qq_data *) gc->proto_data; raw_data = g_newa(guint8, 5); - cursor = raw_data; /* 000-000 get online friends cmd * only 0x02 and 0x03 returns info from server, other valuse all return 0xff * I can also only send the first byte (0x02, or 0x03) * and the result is the same */ - create_packet_b(raw_data, &cursor, QQ_GET_ONLINE_BUDDY_02); + bytes += qq_put8(raw_data + bytes, QQ_GET_ONLINE_BUDDY_02); /* 001-001 seems it supports 255 online buddies at most */ - create_packet_b(raw_data, &cursor, position); + bytes += qq_put8(raw_data + bytes, position); /* 002-002 */ - create_packet_b(raw_data, &cursor, 0x00); + bytes += qq_put8(raw_data + bytes, 0x00); /* 003-004 */ - create_packet_w(raw_data, &cursor, 0x0000); + bytes += qq_put16(raw_data + bytes, 0x0000); qq_send_cmd(gc, QQ_CMD_GET_FRIENDS_ONLINE, TRUE, 0, TRUE, raw_data, 5); qd->last_get_online = time(NULL); @@ -90,42 +90,36 @@ * server may return a position tag if list is too long for one packet */ void qq_send_packet_get_buddies_list(PurpleConnection *gc, guint16 position) { - guint8 *raw_data, *cursor; - gint data_len; + guint8 raw_data[16] = {0}; + gint bytes = 0; - data_len = 3; - raw_data = g_newa(guint8, data_len); - cursor = raw_data; /* 000-001 starting position, can manually specify */ - create_packet_w(raw_data, &cursor, position); + bytes += qq_put16(raw_data + bytes, position); /* before Mar 18, 2004, any value can work, and we sent 00 * I do not know what data QQ server is expecting, as QQ2003iii 0304 itself * even can sending packets 00 and get no response. * Now I tested that 00,00,00,00,00,01 work perfectly * March 22, found the 00,00,00 starts to work as well */ - create_packet_b(raw_data, &cursor, 0x00); + bytes += qq_put8(raw_data + bytes, 0x00); - qq_send_cmd(gc, QQ_CMD_GET_FRIENDS_LIST, TRUE, 0, TRUE, raw_data, data_len); + qq_send_cmd(gc, QQ_CMD_GET_FRIENDS_LIST, TRUE, 0, TRUE, raw_data, bytes); } /* get all list, buddies & Quns with groupsid support */ void qq_send_packet_get_all_list_with_group(PurpleConnection *gc, guint32 position) { - guint8 *raw_data, *cursor; - gint data_len; + guint8 raw_data[16] = {0}; + gint bytes = 0; - data_len = 10; - raw_data = g_newa(guint8, data_len); - cursor = raw_data; /* 0x01 download, 0x02, upload */ - create_packet_b(raw_data, &cursor, 0x01); + bytes += qq_put8(raw_data + bytes, 0x01); /* unknown 0x02 */ - create_packet_b(raw_data, &cursor, 0x02); + bytes += qq_put8(raw_data + bytes, 0x02); /* unknown 00 00 00 00 */ - create_packet_dw(raw_data, &cursor, 0x00000000); - create_packet_dw(raw_data, &cursor, position); + bytes += qq_put32(raw_data + bytes, 0x00000000); + bytes += qq_put32(raw_data + bytes, position); - qq_send_cmd(gc, QQ_CMD_GET_ALL_LIST_WITH_GROUP, TRUE, 0, TRUE, raw_data, data_len); + qq_send_cmd(gc, QQ_CMD_GET_ALL_LIST_WITH_GROUP, TRUE, 0, TRUE, raw_data, bytes); } static void _qq_buddies_online_reply_dump_unclear(qq_friends_online_entry *fe) @@ -151,8 +145,8 @@ void qq_process_get_buddies_online_reply(guint8 *buf, gint buf_len, PurpleConnection *gc) { qq_data *qd; - gint len, bytes; - guint8 *data, *cursor, position; + gint len, bytes, bytes_buddy; + guint8 *data, position; PurpleBuddy *b; qq_buddy *q_bud; qq_friends_online_entry *fe; @@ -162,96 +156,100 @@ qd = (qq_data *) gc->proto_data; len = buf_len; data = g_newa(guint8, len); - cursor = data; purple_debug(PURPLE_DEBUG_INFO, "QQ", "processing get_buddies_online_reply\n"); - - if (qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { - _qq_show_packet("Get buddies online reply packet", data, len); - - read_packet_b(data, &cursor, len, &position); - - fe = g_newa(qq_friends_online_entry, 1); - fe->s = g_newa(qq_buddy_status, 1); + if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies online"); + return; + } - while (cursor < (data + len)) { - /* based on one online buddy entry */ - bytes = 0; - /* 000-030 qq_buddy_status */ - bytes += qq_buddy_status_read(data, &cursor, len, fe->s); - /* 031-032: unknown4 */ - bytes += read_packet_w(data, &cursor, len, &fe->unknown1); - /* 033-033: flag1 */ - bytes += read_packet_b(data, &cursor, len, &fe->flag1); - /* 034-034: comm_flag */ - bytes += read_packet_b(data, &cursor, len, &fe->comm_flag); - /* 035-036: */ - bytes += read_packet_w(data, &cursor, len, &fe->unknown2); - /* 037-037: */ - bytes += read_packet_b(data, &cursor, len, &fe->ending); /* 0x00 */ + _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); - if (fe->s->uid == 0 || bytes != QQ_ONLINE_BUDDY_ENTRY_LEN) { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", - "uid=0 or entry complete len(%d) != %d", - bytes, 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); + while (bytes < len) { + /* 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); + /* 034-034: comm_flag */ + bytes += qq_get8(&fe->comm_flag, data + bytes); + /* 035-036: */ + bytes += qq_get16(&fe->unknown2, data + bytes); + /* 037-037: */ + bytes += qq_get8(&fe->ending, data + bytes); /* 0x00 */ - /* update buddy information */ - b = purple_find_buddy(purple_connection_get_account(gc), uid_to_purple_name(fe->s->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); - } else { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", - "Got an online buddy %d, but not in my buddy list\n", fe->s->uid); - } - + if (fe->s->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); - } - - if(cursor > (data + len)) { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", - "qq_process_get_buddies_online_reply: Dangerous error! maybe protocol changed, notify developers!\n"); + continue; + } /* check if it is a valid entry */ + + if (QQ_DEBUG) { + _qq_buddies_online_reply_dump_unclear(fe); } - if (position != QQ_FRIENDS_ONLINE_POSITION_END) { - purple_debug(PURPLE_DEBUG_INFO, "QQ", "Has more online buddies, position from %d\n", position); + /* update buddy information */ + b = purple_find_buddy(purple_connection_get_account(gc), uid_to_purple_name(fe->s->uid)); + q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data; - qq_send_packet_get_buddies_online(gc, position); + 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); } else { - qq_send_packet_get_buddies_levels(gc); - qq_refresh_all_buddy_status(gc); + purple_debug(PURPLE_DEBUG_ERROR, "QQ", + "Got an online buddy %d, but not in my buddy list\n", fe->s->uid); } + g_free(fe->s->ip); + g_free(fe->s->unknown_key); + } + + if(bytes > len) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", + "qq_process_get_buddies_online_reply: Dangerous error! maybe protocol changed, notify developers!\n"); + } + + if (position != QQ_FRIENDS_ONLINE_POSITION_END) { + purple_debug(PURPLE_DEBUG_INFO, "QQ", "Has more online buddies, position from %d\n", position); + + qq_send_packet_get_buddies_online(gc, position); } else { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies online"); + qq_send_packet_get_buddies_levels(gc); + qq_refresh_all_buddy_status(gc); } } + /* process reply for get_buddies_list */ void qq_process_get_buddies_list_reply(guint8 *buf, gint buf_len, PurpleConnection *gc) { qq_data *qd; qq_buddy *q_bud; - gint len, bytes, bytes_expected, i; + gint len, bytes_expected, i; + gint bytes, buddy_bytes; guint16 position, unknown; - guint8 *data, *cursor, pascal_len; + guint8 *data, pascal_len; gchar *name; PurpleBuddy *b; @@ -260,81 +258,84 @@ qd = (qq_data *) gc->proto_data; len = buf_len; data = g_newa(guint8, len); - cursor = data; + + if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies list"); + return; + } + bytes = 0; + bytes += qq_get16(&position, data + bytes); + /* the following data is buddy list in this packet */ + i = 0; + while (bytes < len) { + q_bud = g_new0(qq_buddy, 1); + /* set flag */ + buddy_bytes = bytes; + /* 000-003: uid */ + bytes += qq_get32(&q_bud->uid, data + bytes); + /* 004-005: icon index (1-255) */ + bytes += qq_get16(&q_bud->face, data + bytes); + /* 006-006: age */ + bytes += qq_get8(&q_bud->age, data + bytes); + /* 007-007: gender */ + bytes += qq_get8(&q_bud->gender, data + bytes); - if (qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { - read_packet_w(data, &cursor, len, &position); - /* the following data is buddy list in this packet */ - i = 0; - while (cursor < (data + len)) { - q_bud = g_new0(qq_buddy, 1); - bytes = 0; - /* 000-003: uid */ - bytes += read_packet_dw(data, &cursor, len, &q_bud->uid); - /* 004-005: icon index (1-255) */ - bytes += read_packet_w(data, &cursor, len, &q_bud->face); - /* 006-006: age */ - bytes += read_packet_b(data, &cursor, len, &q_bud->age); - /* 007-007: gender */ - bytes += read_packet_b(data, &cursor, len, &q_bud->gender); - pascal_len = convert_as_pascal_string(cursor, &q_bud->nickname, QQ_CHARSET_DEFAULT); - cursor += pascal_len; - bytes += pascal_len; - bytes += read_packet_w(data, &cursor, len, &unknown); - /* 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 += read_packet_b(data, &cursor, len, &q_bud->flag1); - bytes += read_packet_b(data, &cursor, len, &q_bud->comm_flag); + pascal_len = convert_as_pascal_string(data + bytes, &q_bud->nickname, QQ_CHARSET_DEFAULT); + bytes += pascal_len; - bytes_expected = 12 + pascal_len; - - if (q_bud->uid == 0 || bytes != bytes_expected) { - purple_debug(PURPLE_DEBUG_INFO, "QQ", - "Buddy entry, expect %d bytes, read %d bytes\n", bytes_expected, bytes); - g_free(q_bud->nickname); - g_free(q_bud); - continue; - } else { - i++; - } + 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->comm_flag, data + bytes); - if (QQ_DEBUG) { - purple_debug(PURPLE_DEBUG_INFO, "QQ", - "buddy [%09d]: flag1=0x%02x, comm_flag=0x%02x\n", - q_bud->uid, q_bud->flag1, q_bud->comm_flag); - } + bytes_expected = 12 + pascal_len; - name = uid_to_purple_name(q_bud->uid); - b = purple_find_buddy(gc->account, name); - g_free(name); - - if (b == NULL) - b = qq_add_buddy_by_recv_packet(gc, q_bud->uid, TRUE, FALSE); - - b->proto_data = q_bud; - qd->buddies = g_list_append(qd->buddies, q_bud); - qq_update_buddy_contact(gc, q_bud); + if (q_bud->uid == 0 || (bytes - buddy_bytes) != bytes_expected) { + purple_debug(PURPLE_DEBUG_INFO, "QQ", + "Buddy entry, expect %d bytes, read %d bytes\n", bytes_expected, bytes - buddy_bytes); + g_free(q_bud->nickname); + g_free(q_bud); + continue; + } else { + i++; } - if(cursor > (data + len)) { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", - "qq_process_get_buddies_list_reply: Dangerous error! maybe protocol changed, notify developers!"); - } - if (position == QQ_FRIENDS_LIST_POSITION_END) { - purple_debug(PURPLE_DEBUG_INFO, "QQ", "Get friends list done, %d buddies\n", i); - qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START); - } else { - qq_send_packet_get_buddies_list(gc, position); + if (QQ_DEBUG) { + purple_debug(PURPLE_DEBUG_INFO, "QQ", + "buddy [%09d]: flag1=0x%02x, comm_flag=0x%02x\n", + q_bud->uid, q_bud->flag1, q_bud->comm_flag); + } + + name = uid_to_purple_name(q_bud->uid); + b = purple_find_buddy(gc->account, name); + g_free(name); + + if (b == NULL) { + b = qq_add_buddy_by_recv_packet(gc, q_bud->uid, TRUE, FALSE); } + + b->proto_data = q_bud; + qd->buddies = g_list_append(qd->buddies, q_bud); + qq_update_buddy_contact(gc, q_bud); + } + + if(bytes > len) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", + "qq_process_get_buddies_list_reply: Dangerous error! maybe protocol changed, notify developers!"); + } + if (position == QQ_FRIENDS_LIST_POSITION_END) { + purple_debug(PURPLE_DEBUG_INFO, "QQ", "Get friends list done, %d buddies\n", i); + qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START); } else { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt buddies list"); + qq_send_packet_get_buddies_list(gc, position); } } @@ -342,7 +343,8 @@ { qq_data *qd; gint len, i, j; - guint8 *data, *cursor; + gint bytes = 0; + guint8 *data; guint8 sub_cmd, reply_code; guint32 unknown, position; guint32 uid; @@ -354,62 +356,66 @@ qd = (qq_data *) gc->proto_data; len = buf_len; data = g_newa(guint8, len); - cursor = data; + + if (!qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt all list with group"); + return; + } + + bytes += qq_get8(&sub_cmd, data + bytes); + g_return_if_fail(sub_cmd == 0x01); + + bytes += qq_get8(&reply_code, data + bytes); + if(0 != reply_code) { + purple_debug(PURPLE_DEBUG_WARNING, "QQ", + "Get all list with group reply, reply_code(%d) is not zero", reply_code); + } - if (qq_decrypt(buf, buf_len, qd->session_key, data, &len)) { - read_packet_b(data, &cursor, len, &sub_cmd); - g_return_if_fail(sub_cmd == 0x01); - read_packet_b(data, &cursor, len, &reply_code); - if(0 != reply_code) { - purple_debug(PURPLE_DEBUG_WARNING, "QQ", - "Get all list with group reply, reply_code(%d) is not zero", reply_code); + bytes += qq_get32(&unknown, data + bytes); + bytes += qq_get32(&position, data + bytes); + /* the following data is all list in this packet */ + i = 0; + j = 0; + while (bytes < len) { + /* 00-03: uid */ + bytes += qq_get32(&uid, data + bytes); + /* 04: type 0x1:buddy 0x4:Qun */ + bytes += qq_get8(&type, data + bytes); + /* 05: groupid*4 */ /* seems to always be 0 */ + bytes += qq_get8(&groupid, data + bytes); + /* + purple_debug(PURPLE_DEBUG_INFO, "QQ", "groupid: %i\n", groupid); + groupid >>= 2; + */ + if (uid == 0 || (type != 0x1 && type != 0x4)) { + purple_debug(PURPLE_DEBUG_INFO, "QQ", + "Buddy entry, uid=%d, type=%d", uid, type); + continue; + } + if(0x1 == type) { /* a buddy */ + /* don't do anything but count - buddies are handled by + * qq_send_packet_get_buddies_list */ + ++i; + } else { /* a group */ + group = qq_group_find_by_id(gc, uid, QQ_INTERNAL_ID); + if(group == NULL) { + qq_set_pending_id(&qd->adding_groups_from_server, uid, TRUE); + group = g_newa(qq_group, 1); + group->internal_group_id = uid; + qq_send_cmd_group_get_group_info(gc, group); + } else { + group->my_status = QQ_GROUP_MEMBER_STATUS_IS_MEMBER; + qq_group_refresh(gc, group); + qq_send_cmd_group_get_group_info(gc, group); + } + ++j; } - read_packet_dw(data, &cursor, len, &unknown); - read_packet_dw(data, &cursor, len, &position); - /* the following data is all list in this packet */ - i = 0; - j = 0; - while (cursor < (data + len)) { - /* 00-03: uid */ - read_packet_dw(data, &cursor, len, &uid); - /* 04: type 0x1:buddy 0x4:Qun */ - read_packet_b(data, &cursor, len, &type); - /* 05: groupid*4 */ /* seems to always be 0 */ - read_packet_b(data, &cursor, len, &groupid); - /* - purple_debug(PURPLE_DEBUG_INFO, "QQ", "groupid: %i\n", groupid); - groupid >>= 2; - */ - if (uid == 0 || (type != 0x1 && type != 0x4)) { - purple_debug(PURPLE_DEBUG_INFO, "QQ", - "Buddy entry, uid=%d, type=%d", uid, type); - continue; - } - if(0x1 == type) { /* a buddy */ - /* don't do anything but count - buddies are handled by - * qq_send_packet_get_buddies_list */ - ++i; - } else { /* a group */ - group = qq_group_find_by_id(gc, uid, QQ_INTERNAL_ID); - if(group == NULL) { - qq_set_pending_id(&qd->adding_groups_from_server, uid, TRUE); - group = g_newa(qq_group, 1); - group->internal_group_id = uid; - qq_send_cmd_group_get_group_info(gc, group); - } else { - group->my_status = QQ_GROUP_MEMBER_STATUS_IS_MEMBER; - qq_group_refresh(gc, group); - qq_send_cmd_group_get_group_info(gc, group); - } - ++j; - } - } - if(cursor > (data + len)) { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", - "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); - } else { - purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Error decrypt all list with group"); } + + if(bytes > len) { + purple_debug(PURPLE_DEBUG_ERROR, "QQ", + "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); }