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