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