comparison libpurple/protocols/qq/buddy_list.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 0d43518fd943
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /**
2 * @file buddy_list.c
3 *
4 * gaim
5 *
6 * Gaim is the legal property of its developers, whose names are too numerous
7 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * source distribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <string.h>
26 #include "debug.h"
27
28 #include "notify.h"
29 #include "utils.h"
30 #include "packet_parse.h"
31 #include "buddy_info.h"
32 #include "buddy_list.h"
33 #include "buddy_status.h"
34 #include "buddy_opt.h"
35 #include "char_conv.h"
36 #include "crypt.h"
37 #include "header_info.h"
38 #include "keep_alive.h"
39 #include "send_core.h"
40 #include "qq.h"
41 #include "group.h"
42 #include "group_find.h"
43 #include "group_internal.h"
44 #include "group_info.h"
45
46 #include "qq_proxy.h"
47
48 #define QQ_GET_ONLINE_BUDDY_02 0x02
49 #define QQ_GET_ONLINE_BUDDY_03 0x03 /* unknown function */
50
51 #define QQ_ONLINE_BUDDY_ENTRY_LEN 38
52
53 typedef struct _qq_friends_online_entry {
54 qq_buddy_status *s;
55 guint16 unknown1;
56 guint8 flag1;
57 guint8 comm_flag;
58 guint16 unknown2;
59 guint8 ending; /* 0x00 */
60 } qq_friends_online_entry;
61
62 /* get a list of online_buddies */
63 void qq_send_packet_get_buddies_online(GaimConnection *gc, guint8 position)
64 {
65 qq_data *qd;
66 guint8 *raw_data, *cursor;
67
68 qd = (qq_data *) gc->proto_data;
69 raw_data = g_newa(guint8, 5);
70 cursor = raw_data;
71
72 /* 000-000 get online friends cmd
73 * only 0x02 and 0x03 returns info from server, other valuse all return 0xff
74 * I can also only send the first byte (0x02, or 0x03)
75 * and the result is the same */
76 create_packet_b(raw_data, &cursor, QQ_GET_ONLINE_BUDDY_02);
77 /* 001-001 seems it supports 255 online buddies at most */
78 create_packet_b(raw_data, &cursor, position);
79 /* 002-002 */
80 create_packet_b(raw_data, &cursor, 0x00);
81 /* 003-004 */
82 create_packet_w(raw_data, &cursor, 0x0000);
83
84 qq_send_cmd(gc, QQ_CMD_GET_FRIENDS_ONLINE, TRUE, 0, TRUE, raw_data, 5);
85 qd->last_get_online = time(NULL);
86 }
87
88 /* position starts with 0x0000,
89 * server may return a position tag if list is too long for one packet */
90 void qq_send_packet_get_buddies_list(GaimConnection *gc, guint16 position)
91 {
92 guint8 *raw_data, *cursor;
93 gint data_len;
94
95 data_len = 3;
96 raw_data = g_newa(guint8, data_len);
97 cursor = raw_data;
98 /* 000-001 starting position, can manually specify */
99 create_packet_w(raw_data, &cursor, position);
100 /* before Mar 18, 2004, any value can work, and we sent 00
101 * I do not know what data QQ server is expecting, as QQ2003iii 0304 itself
102 * even can sending packets 00 and get no response.
103 * Now I tested that 00,00,00,00,00,01 work perfectly
104 * March 22, found the 00,00,00 starts to work as well */
105 create_packet_b(raw_data, &cursor, 0x00);
106
107 qq_send_cmd(gc, QQ_CMD_GET_FRIENDS_LIST, TRUE, 0, TRUE, raw_data, data_len);
108 }
109
110 /* get all list, buddies & Quns with groupsid support */
111 void qq_send_packet_get_all_list_with_group(GaimConnection *gc, guint32 position)
112 {
113 guint8 *raw_data, *cursor;
114 gint data_len;
115
116 data_len = 10;
117 raw_data = g_newa(guint8, data_len);
118 cursor = raw_data;
119 /* 0x01 download, 0x02, upload */
120 create_packet_b(raw_data, &cursor, 0x01);
121 /* unknown 0x02 */
122 create_packet_b(raw_data, &cursor, 0x02);
123 /* unknown 00 00 00 00 */
124 create_packet_dw(raw_data, &cursor, 0x00000000);
125 create_packet_dw(raw_data, &cursor, position);
126
127 qq_send_cmd(gc, QQ_CMD_GET_ALL_LIST_WITH_GROUP, TRUE, 0, TRUE, raw_data, data_len);
128 }
129
130 static void _qq_buddies_online_reply_dump_unclear(qq_friends_online_entry *fe)
131 {
132 GString *dump;
133
134 g_return_if_fail(fe != NULL);
135
136 qq_buddy_status_dump_unclear(fe->s);
137
138 dump = g_string_new("");
139 g_string_append_printf(dump, "unclear fields for [%d]:\n", fe->s->uid);
140 g_string_append_printf(dump, "031-032: %04x (unknown)\n", fe->unknown1);
141 g_string_append_printf(dump, "033: %02x (flag1)\n", fe->flag1);
142 g_string_append_printf(dump, "034: %02x (comm_flag)\n", fe->comm_flag);
143 g_string_append_printf(dump, "035-036: %04x (unknown)\n", fe->unknown2);
144
145 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Online buddy entry, %s", dump->str);
146 g_string_free(dump, TRUE);
147 }
148
149 /* process the reply packet for get_buddies_online packet */
150 void qq_process_get_buddies_online_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
151 {
152 qq_data *qd;
153 gint len, bytes;
154 guint8 *data, *cursor, position;
155 GaimBuddy *b;
156 qq_buddy *q_bud;
157 qq_friends_online_entry *fe;
158
159 g_return_if_fail(buf != NULL && buf_len != 0);
160
161 qd = (qq_data *) gc->proto_data;
162 len = buf_len;
163 data = g_newa(guint8, len);
164 cursor = data;
165
166 gaim_debug(GAIM_DEBUG_INFO, "QQ", "processing get_buddies_online_reply\n");
167
168 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
169
170 _qq_show_packet("Get buddies online reply packet", data, len);
171
172 read_packet_b(data, &cursor, len, &position);
173
174 fe = g_newa(qq_friends_online_entry, 1);
175 fe->s = g_newa(qq_buddy_status, 1);
176
177 while (cursor < (data + len)) {
178 /* based on one online buddy entry */
179 bytes = 0;
180 /* 000-030 qq_buddy_status */
181 bytes += qq_buddy_status_read(data, &cursor, len, fe->s);
182 /* 031-032: unknown4 */
183 bytes += read_packet_w(data, &cursor, len, &fe->unknown1);
184 /* 033-033: flag1 */
185 bytes += read_packet_b(data, &cursor, len, &fe->flag1);
186 /* 034-034: comm_flag */
187 bytes += read_packet_b(data, &cursor, len, &fe->comm_flag);
188 /* 035-036: */
189 bytes += read_packet_w(data, &cursor, len, &fe->unknown2);
190 /* 037-037: */
191 bytes += read_packet_b(data, &cursor, len, &fe->ending); /* 0x00 */
192
193 if (fe->s->uid == 0 || bytes != QQ_ONLINE_BUDDY_ENTRY_LEN) {
194 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
195 "uid=0 or entry complete len(%d) != %d",
196 bytes, QQ_ONLINE_BUDDY_ENTRY_LEN);
197 g_free(fe->s->ip);
198 g_free(fe->s->unknown_key);
199 continue;
200 } /* check if it is a valid entry */
201
202 if (QQ_DEBUG)
203 _qq_buddies_online_reply_dump_unclear(fe);
204
205 /* update buddy information */
206 b = gaim_find_buddy(gaim_connection_get_account(gc), uid_to_gaim_name(fe->s->uid));
207 q_bud = (b == NULL) ? NULL : (qq_buddy *) b->proto_data;
208
209 if (q_bud != NULL) { /* we find one and update qq_buddy */
210 if(0 != fe->s->client_version)
211 q_bud->client_version = fe->s->client_version;
212 g_memmove(q_bud->ip, fe->s->ip, 4);
213 q_bud->port = fe->s->port;
214 q_bud->status = fe->s->status;
215 q_bud->flag1 = fe->flag1;
216 q_bud->comm_flag = fe->comm_flag;
217 qq_update_buddy_contact(gc, q_bud);
218 }
219 else {
220 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
221 "Got an online buddy %d, but not in my buddy list\n", fe->s->uid);
222 }
223
224 g_free(fe->s->ip);
225 g_free(fe->s->unknown_key);
226 }
227
228 if(cursor > (data + len)) {
229 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
230 "qq_process_get_buddies_online_reply: Dangerous error! maybe protocol changed, notify developers!\n");
231 }
232
233 if (position != QQ_FRIENDS_ONLINE_POSITION_END) {
234 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Has more online buddies, position from %d\n", position);
235
236 qq_send_packet_get_buddies_online(gc, position);
237 }
238 else {
239 qq_send_packet_get_buddies_levels(gc);
240 qq_refresh_all_buddy_status(gc);
241 }
242
243 } else {
244 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt buddies online");
245 }
246 }
247
248 /* process reply for get_buddies_list */
249 void qq_process_get_buddies_list_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
250 {
251 qq_data *qd;
252 qq_buddy *q_bud;
253 gint len, bytes, bytes_expected, i;
254 guint16 position, unknown;
255 guint8 *data, *cursor, pascal_len;
256 gchar *name;
257 GaimBuddy *b;
258
259 g_return_if_fail(buf != NULL && buf_len != 0);
260
261 qd = (qq_data *) gc->proto_data;
262 len = buf_len;
263 data = g_newa(guint8, len);
264 cursor = data;
265
266 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
267 read_packet_w(data, &cursor, len, &position);
268 /* the following data is buddy list in this packet */
269 i = 0;
270 while (cursor < (data + len)) {
271 q_bud = g_new0(qq_buddy, 1);
272 bytes = 0;
273 /* 000-003: uid */
274 bytes += read_packet_dw(data, &cursor, len, &q_bud->uid);
275 /* 004-005: icon index (1-255) */
276 bytes += read_packet_w(data, &cursor, len, &q_bud->face);
277 /* 006-006: age */
278 bytes += read_packet_b(data, &cursor, len, &q_bud->age);
279 /* 007-007: gender */
280 bytes += read_packet_b(data, &cursor, len, &q_bud->gender);
281 pascal_len = convert_as_pascal_string(cursor, &q_bud->nickname, QQ_CHARSET_DEFAULT);
282 cursor += pascal_len;
283 bytes += pascal_len;
284 bytes += read_packet_w(data, &cursor, len, &unknown);
285 /* flag1: (0-7)
286 * bit1 => qq show
287 * comm_flag: (0-7)
288 * bit1 => member
289 * bit4 => TCP mode
290 * bit5 => open mobile QQ
291 * bit6 => bind to mobile
292 * bit7 => whether having a video
293 */
294 bytes += read_packet_b(data, &cursor, len, &q_bud->flag1);
295 bytes += read_packet_b(data, &cursor, len, &q_bud->comm_flag);
296
297 bytes_expected = 12 + pascal_len;
298
299 if (q_bud->uid == 0 || bytes != bytes_expected) {
300 gaim_debug(GAIM_DEBUG_INFO, "QQ",
301 "Buddy entry, expect %d bytes, read %d bytes\n", bytes_expected, bytes);
302 g_free(q_bud->nickname);
303 g_free(q_bud);
304 continue;
305 } else {
306 i++;
307 }
308
309 if (QQ_DEBUG) {
310 gaim_debug(GAIM_DEBUG_INFO, "QQ",
311 "buddy [%09d]: flag1=0x%02x, comm_flag=0x%02x\n",
312 q_bud->uid, q_bud->flag1, q_bud->comm_flag);
313 }
314
315 name = uid_to_gaim_name(q_bud->uid);
316 b = gaim_find_buddy(gc->account, name);
317 g_free(name);
318
319 if (b == NULL)
320 b = qq_add_buddy_by_recv_packet(gc, q_bud->uid, TRUE, FALSE);
321
322 b->proto_data = q_bud;
323 qd->buddies = g_list_append(qd->buddies, q_bud);
324 qq_update_buddy_contact(gc, q_bud);
325 }
326
327 if(cursor > (data + len)) {
328 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
329 "qq_process_get_buddies_list_reply: Dangerous error! maybe protocol changed, notify developers!");
330 }
331 if (position == QQ_FRIENDS_LIST_POSITION_END) {
332 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Get friends list done, %d buddies\n", i);
333 qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START);
334 } else {
335 qq_send_packet_get_buddies_list(gc, position);
336 }
337 } else {
338 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt buddies list");
339 }
340 }
341
342 void qq_process_get_all_list_with_group_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
343 {
344 qq_data *qd;
345 gint len, i, j;
346 guint8 *data, *cursor;
347 guint8 sub_cmd, reply_code;
348 guint32 unknown, position;
349 guint32 uid;
350 guint8 type, groupid;
351 qq_group *group;
352
353 g_return_if_fail(buf != NULL && buf_len != 0);
354
355 qd = (qq_data *) gc->proto_data;
356 len = buf_len;
357 data = g_newa(guint8, len);
358 cursor = data;
359
360 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
361 read_packet_b(data, &cursor, len, &sub_cmd);
362 g_return_if_fail(sub_cmd == 0x01);
363 read_packet_b(data, &cursor, len, &reply_code);
364 if(0 != reply_code) {
365 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
366 "Get all list with group reply, reply_code(%d) is not zero", reply_code);
367 }
368 read_packet_dw(data, &cursor, len, &unknown);
369 read_packet_dw(data, &cursor, len, &position);
370 /* the following data is all list in this packet */
371 i = 0;
372 j = 0;
373 while (cursor < (data + len)) {
374 /* 00-03: uid */
375 read_packet_dw(data, &cursor, len, &uid);
376 /* 04: type 0x1:buddy 0x4:Qun */
377 read_packet_b(data, &cursor, len, &type);
378 /* 05: groupid*4 */ /* seems to always be 0 */
379 read_packet_b(data, &cursor, len, &groupid);
380 /*
381 gaim_debug(GAIM_DEBUG_INFO, "QQ", "groupid: %i\n", groupid);
382 groupid >>= 2;
383 */
384 if (uid == 0 || (type != 0x1 && type != 0x4)) {
385 gaim_debug(GAIM_DEBUG_INFO, "QQ",
386 "Buddy entry, uid=%d, type=%d", uid, type);
387 continue;
388 }
389 if(0x1 == type) { /* a buddy */
390 /* don't do anything but count - buddies are handled by
391 * qq_send_packet_get_buddies_list */
392 ++i;
393 } else { /* a group */
394 group = qq_group_find_by_id(gc, uid, QQ_INTERNAL_ID);
395 if(group == NULL) {
396 qq_set_pending_id(&qd->adding_groups_from_server, uid, TRUE);
397 group = g_newa(qq_group, 1);
398 group->internal_group_id = uid;
399 qq_send_cmd_group_get_group_info(gc, group);
400 } else {
401 group->my_status = QQ_GROUP_MEMBER_STATUS_IS_MEMBER;
402 qq_group_refresh(gc, group);
403 qq_send_cmd_group_get_group_info(gc, group);
404 }
405 ++j;
406 }
407 }
408 if(cursor > (data + len)) {
409 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
410 "qq_process_get_all_list_with_group_reply: Dangerous error! maybe protocol changed, notify developers!");
411 }
412 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Get all list done, %d buddies and %d Quns\n", i, j);
413 } else {
414 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt all list with group");
415 }
416 }