comparison libpurple/protocols/qq/im.c @ 15373: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 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file im.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 "conversation.h"
26 #include "debug.h"
27 #include "internal.h"
28 #include "notify.h"
29 #include "server.h"
30 #include "util.h"
31
32 #include "buddy_info.h"
33 #include "buddy_list.h"
34 #include "buddy_opt.h"
35 #include "char_conv.h"
36 #include "crypt.h"
37 #include "group_im.h"
38 #include "header_info.h"
39 #include "im.h"
40 #include "packet_parse.h"
41 #include "send_core.h"
42 #include "send_file.h"
43 #include "utils.h"
44
45 #define QQ_SEND_IM_REPLY_OK 0x00
46 #define DEFAULT_FONT_NAME_LEN 4
47
48 /* a debug function */
49 void _qq_show_packet(const gchar *desc, const guint8 *buf, gint len);
50
51 enum
52 {
53 QQ_NORMAL_IM_TEXT = 0x000b,
54 QQ_NORMAL_IM_FILE_REQUEST_TCP = 0x0001,
55 QQ_NORMAL_IM_FILE_APPROVE_TCP = 0x0003,
56 QQ_NORMAL_IM_FILE_REJECT_TCP = 0x0005,
57 QQ_NORMAL_IM_FILE_REQUEST_UDP = 0x0035,
58 QQ_NORMAL_IM_FILE_APPROVE_UDP = 0x0037,
59 QQ_NORMAL_IM_FILE_REJECT_UDP = 0x0039,
60 QQ_NORMAL_IM_FILE_NOTIFY = 0x003b,
61 QQ_NORMAL_IM_FILE_PASV = 0x003f, /* are you behind a firewall? */
62 QQ_NORMAL_IM_FILE_CANCEL = 0x0049,
63 QQ_NORMAL_IM_FILE_EX_REQUEST_UDP = 0x81,
64 QQ_NORMAL_IM_FILE_EX_REQUEST_ACCEPT = 0x83,
65 QQ_NORMAL_IM_FILE_EX_REQUEST_CANCEL = 0x85,
66 QQ_NORMAL_IM_FILE_EX_NOTIFY_IP = 0x87
67 };
68
69 enum {
70 QQ_RECV_SYS_IM_KICK_OUT = 0x01
71 };
72
73 typedef struct _qq_recv_im_header qq_recv_im_header;
74 typedef struct _qq_recv_normal_im_text qq_recv_normal_im_text;
75 typedef struct _qq_recv_normal_im_common qq_recv_normal_im_common;
76 typedef struct _qq_recv_normal_im_unprocessed qq_recv_normal_im_unprocessed;
77
78 struct _qq_recv_normal_im_common {
79 /* this is the common part of normal_text */
80 guint16 sender_ver;
81 guint32 sender_uid;
82 guint32 receiver_uid;
83 guint8 *session_md5;
84 guint16 normal_im_type;
85 };
86
87 struct _qq_recv_normal_im_text {
88 qq_recv_normal_im_common *common;
89 /* now comes the part for text only */
90 guint16 msg_seq;
91 guint32 send_time;
92 guint16 sender_icon;
93 guint8 unknown2[3];
94 guint8 is_there_font_attr;
95 guint8 unknown3[4];
96 guint8 msg_type;
97 gchar *msg; /* no fixed length, ends with 0x00 */
98 guint8 *font_attr;
99 gint font_attr_len;
100 };
101
102 struct _qq_recv_normal_im_unprocessed {
103 qq_recv_normal_im_common *common;
104 /* now comes the part of unprocessed */
105 guint8 *unknown; /* no fixed length */
106 gint length;
107 };
108
109 struct _qq_recv_im_header {
110 guint32 sender_uid;
111 guint32 receiver_uid;
112 guint32 server_im_seq;
113 guint8 sender_ip[4];
114 guint16 sender_port;
115 guint16 im_type;
116 };
117
118 #define QQ_SEND_IM_AFTER_MSG_HEADER_LEN 8
119 #define DEFAULT_FONT_NAME "\0xcb\0xce\0xcc\0xe5"
120
121 guint8 *qq_get_send_im_tail(const gchar *font_color,
122 const gchar *font_size,
123 const gchar *font_name,
124 gboolean is_bold, gboolean is_italic, gboolean is_underline, gint tail_len)
125 {
126 gchar *s1, *s2;
127 unsigned char *rgb;
128 gint font_name_len;
129 guint8 *send_im_tail;
130 const guint8 simsun[] = { 0xcb, 0xce, 0xcc, 0xe5 };
131
132 if (font_name) {
133 font_name_len = strlen(font_name);
134 } else {
135 font_name_len = DEFAULT_FONT_NAME_LEN;
136 font_name = (const gchar *) simsun;
137 }
138
139 send_im_tail = g_new0(guint8, tail_len);
140
141 g_strlcpy((gchar *) (send_im_tail + QQ_SEND_IM_AFTER_MSG_HEADER_LEN),
142 font_name, tail_len - QQ_SEND_IM_AFTER_MSG_HEADER_LEN);
143 send_im_tail[tail_len - 1] = (guint8) tail_len;
144
145 send_im_tail[0] = 0x00;
146 if (font_size) {
147 send_im_tail[1] = (guint8) (atoi(font_size) * 3 + 1);
148 } else {
149 send_im_tail[1] = 10;
150 }
151 if (is_bold)
152 send_im_tail[1] |= 0x20;
153 if (is_italic)
154 send_im_tail[1] |= 0x40;
155 if (is_underline)
156 send_im_tail[1] |= 0x80;
157
158 if (font_color) {
159 s1 = g_strndup(font_color + 1, 6);
160 /* Henry: maybe this is a bug of gaim, the string should have
161 * the length of odd number @_@
162 */
163 s2 = g_strdup_printf("%sH", s1);
164 rgb = gaim_base16_decode(s2, NULL);
165 g_free(s1);
166 g_free(s2);
167 memcpy(send_im_tail + 2, rgb, 3);
168 g_free(rgb);
169 } else {
170 send_im_tail[2] = send_im_tail[3] = send_im_tail[4] = 0;
171 }
172
173 send_im_tail[5] = 0x00;
174 send_im_tail[6] = 0x86;
175 send_im_tail[7] = 0x22; /* encoding, 0x8622=GB, 0x0000=EN, define BIG5 support here */
176 _qq_show_packet("QQ_MESG", send_im_tail, tail_len);
177 return (guint8 *) send_im_tail;
178 }
179
180 static const gchar *qq_get_recv_im_type_str(gint type)
181 {
182 switch (type) {
183 case QQ_RECV_IM_TO_BUDDY:
184 return "QQ_RECV_IM_TO_BUDDY";
185 case QQ_RECV_IM_TO_UNKNOWN:
186 return "QQ_RECV_IM_TO_UNKNOWN";
187 case QQ_RECV_IM_UNKNOWN_QUN_IM:
188 return "QQ_RECV_IM_UNKNOWN_QUN_IM";
189 case QQ_RECV_IM_ADD_TO_QUN:
190 return "QQ_RECV_IM_ADD_TO_QUN";
191 case QQ_RECV_IM_DEL_FROM_QUN:
192 return "QQ_RECV_IM_DEL_FROM_QUN";
193 case QQ_RECV_IM_APPLY_ADD_TO_QUN:
194 return "QQ_RECV_IM_APPLY_ADD_TO_QUN";
195 case QQ_RECV_IM_CREATE_QUN:
196 return "QQ_RECV_IM_CREATE_QUN";
197 case QQ_RECV_IM_SYS_NOTIFICATION:
198 return "QQ_RECV_IM_SYS_NOTIFICATION";
199 case QQ_RECV_IM_APPROVE_APPLY_ADD_TO_QUN:
200 return "QQ_RECV_IM_APPROVE_APPLY_ADD_TO_QUN";
201 case QQ_RECV_IM_REJCT_APPLY_ADD_TO_QUN:
202 return "QQ_RECV_IM_REJCT_APPLY_ADD_TO_QUN";
203 case QQ_RECV_IM_TEMP_QUN_IM:
204 return "QQ_RECV_IM_TEMP_QUN_IM";
205 case QQ_RECV_IM_QUN_IM:
206 return "QQ_RECV_IM_QUN_IM";
207 default:
208 return "QQ_RECV_IM_UNKNOWN";
209 }
210 }
211
212 /* when we receive a message,
213 * we send an ACK which is the first 16 bytes of incoming packet */
214 static void _qq_send_packet_recv_im_ack(GaimConnection *gc, guint16 seq, guint8 *data)
215 {
216 qq_send_cmd(gc, QQ_CMD_RECV_IM, FALSE, seq, FALSE, data, 16);
217 }
218
219 /* read the common parts of the normal_im,
220 * returns the bytes read if succeed, or -1 if there is any error */
221 static gint _qq_normal_im_common_read(guint8 *data, guint8 **cursor, gint len, qq_recv_normal_im_common *common)
222 {
223 gint bytes;
224 g_return_val_if_fail(data != NULL && len != 0 && common != NULL, -1);
225
226 bytes = 0;
227 /* now push data into common header */
228 bytes += read_packet_w(data, cursor, len, &(common->sender_ver));
229 bytes += read_packet_dw(data, cursor, len, &(common->sender_uid));
230 bytes += read_packet_dw(data, cursor, len, &(common->receiver_uid));
231
232 common->session_md5 = g_memdup(*cursor, QQ_KEY_LENGTH);
233 bytes += QQ_KEY_LENGTH;
234 *cursor += QQ_KEY_LENGTH;
235
236 bytes += read_packet_w(data, cursor, len, &(common->normal_im_type));
237
238 if (bytes != 28) { /* read common place fail */
239 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Expect 28 bytes, read %d bytes\n", bytes);
240 return -1;
241 }
242
243 return bytes;
244 }
245
246 /* process received normal text IM */
247 static void _qq_process_recv_normal_im_text
248 (guint8 *data, guint8 **cursor, gint len, qq_recv_normal_im_common *common, GaimConnection *gc)
249 {
250 guint16 gaim_msg_type;
251 gchar *name;
252 gchar *msg_with_gaim_smiley;
253 gchar *msg_utf8_encoded;
254 qq_data *qd;
255 qq_recv_normal_im_text *im_text;
256
257 g_return_if_fail(common != NULL);
258 qd = (qq_data *) gc->proto_data;
259
260 /* now it is QQ_NORMAL_IM_TEXT */
261 if (*cursor >= (data + len - 1)) {
262 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Received normal IM text is empty\n");
263 return;
264 } else
265 im_text = g_newa(qq_recv_normal_im_text, 1);
266
267 im_text->common = common;
268
269 /* push data into im_text */
270 read_packet_w(data, cursor, len, &(im_text->msg_seq));
271 read_packet_dw(data, cursor, len, &(im_text->send_time));
272 read_packet_w(data, cursor, len, &(im_text->sender_icon));
273 read_packet_data(data, cursor, len, (guint8 *) & (im_text->unknown2), 3);
274 read_packet_b(data, cursor, len, &(im_text->is_there_font_attr));
275 /**
276 * from lumaqq for unknown3
277 * totalFragments = buf.get() & 255;
278 * fragmentSequence = buf.get() & 255;
279 * messageId = buf.getChar();
280 */
281 read_packet_data(data, cursor, len, (guint8 *) & (im_text->unknown3), 4);
282 read_packet_b(data, cursor, len, &(im_text->msg_type));
283
284 /* we need to check if this is auto-reply
285 * QQ2003iii build 0304, returns the msg without font_attr
286 * even the is_there_font_attr shows 0x01, and msg does not ends with 0x00 */
287 if (im_text->msg_type == QQ_IM_AUTO_REPLY) {
288 im_text->is_there_font_attr = 0x00; /* indeed there is no this flag */
289 im_text->msg = g_strndup(*(gchar **) cursor, data + len - *cursor);
290 } else { /* it is normal mesasge */
291 if (im_text->is_there_font_attr) {
292 im_text->msg = g_strdup(*(gchar **) cursor);
293 *cursor += strlen(im_text->msg) + 1;
294 im_text->font_attr_len = data + len - *cursor;
295 im_text->font_attr = g_memdup(*cursor, im_text->font_attr_len);
296 } else /* not im_text->is_there_font_attr */
297 im_text->msg = g_strndup(*(gchar **) cursor, data + len - *cursor);
298 } /* if im_text->msg_type */
299 _qq_show_packet("QQ_MESG recv", data, *cursor - data);
300
301 name = uid_to_gaim_name(common->sender_uid);
302 if (gaim_find_buddy(gc->account, name) == NULL)
303 qq_add_buddy_by_recv_packet(gc, common->sender_uid, FALSE, TRUE);
304
305 gaim_msg_type = (im_text->msg_type == QQ_IM_AUTO_REPLY) ? GAIM_MESSAGE_AUTO_RESP : 0;
306
307 msg_with_gaim_smiley = qq_smiley_to_gaim(im_text->msg);
308 msg_utf8_encoded = im_text->is_there_font_attr ?
309 qq_encode_to_gaim(im_text->font_attr,
310 im_text->font_attr_len,
311 msg_with_gaim_smiley) : qq_to_utf8(msg_with_gaim_smiley, QQ_CHARSET_DEFAULT);
312
313 /* send encoded to gaim, note that we use im_text->send_time,
314 * not the time we receive the message
315 * as it may have been delayed when I am not online. */
316 serv_got_im(gc, name, msg_utf8_encoded, gaim_msg_type, (time_t) im_text->send_time);
317
318 g_free(msg_utf8_encoded);
319 g_free(msg_with_gaim_smiley);
320 g_free(name);
321 g_free(im_text->msg);
322 if (im_text->is_there_font_attr)
323 g_free(im_text->font_attr);
324 }
325
326 /* it is a normal IM, maybe text or video request */
327 static void _qq_process_recv_normal_im(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
328 {
329 gint bytes;
330 qq_recv_normal_im_common *common;
331 qq_recv_normal_im_unprocessed *im_unprocessed;
332 gchar *hex_dump;
333
334 g_return_if_fail (data != NULL && len != 0);
335
336 if (*cursor >= (data + len - 1)) {
337 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
338 "Received normal IM is empty\n");
339 return;
340 }
341 else
342 common = g_newa (qq_recv_normal_im_common, 1);
343
344 bytes = _qq_normal_im_common_read (data, cursor, len, common);
345 if (bytes < 0) {
346 gaim_debug (GAIM_DEBUG_ERROR, "QQ",
347 "Fail read the common part of normal IM\n");
348 return;
349 }
350
351 switch (common->normal_im_type) {
352 case QQ_NORMAL_IM_TEXT:
353 gaim_debug (GAIM_DEBUG_INFO,
354 "QQ",
355 "Normal IM, text type:\n [%d] => [%d], src: %s\n",
356 common->sender_uid, common->receiver_uid,
357 qq_get_source_str (common->sender_ver));
358 _qq_process_recv_normal_im_text (data, cursor, len, common,
359 gc);
360 break;
361 case QQ_NORMAL_IM_FILE_REJECT_UDP:
362 qq_process_recv_file_reject (data, cursor, len,
363 common->sender_uid, gc);
364 break;
365 case QQ_NORMAL_IM_FILE_APPROVE_UDP:
366 qq_process_recv_file_accept (data, cursor, len,
367 common->sender_uid, gc);
368 break;
369 case QQ_NORMAL_IM_FILE_REQUEST_UDP:
370 qq_process_recv_file_request (data, cursor, len,
371 common->sender_uid, gc);
372 break;
373 case QQ_NORMAL_IM_FILE_CANCEL:
374 qq_process_recv_file_cancel (data, cursor, len,
375 common->sender_uid, gc);
376 break;
377 case QQ_NORMAL_IM_FILE_NOTIFY:
378 qq_process_recv_file_notify (data, cursor, len,
379 common->sender_uid, gc);
380 break;
381 default:
382 im_unprocessed = g_newa (qq_recv_normal_im_unprocessed, 1);
383 im_unprocessed->common = common;
384 im_unprocessed->unknown = *cursor;
385 im_unprocessed->length = data + len - *cursor;
386 /* a simple process here, maybe more later */
387 gaim_debug (GAIM_DEBUG_WARNING, "QQ",
388 "Normal IM, unprocessed type [0x%04x]\n",
389 common->normal_im_type);
390 hex_dump = hex_dump_to_str(im_unprocessed->unknown, im_unprocessed->length);
391 gaim_debug (GAIM_DEBUG_WARNING, "QQ", "Dump unknown part.\n%s", hex_dump);
392 g_free(hex_dump);
393 g_free (common->session_md5);
394 return;
395 }
396
397 g_free (common->session_md5);
398 }
399
400 /* process im from system administrator */
401 static void _qq_process_recv_sys_im(guint8 *data, guint8 **cursor, gint data_len, GaimConnection *gc)
402 {
403 gint len;
404 guint8 reply;
405 gchar **segments, *msg_utf8;
406
407 g_return_if_fail(data != NULL && data_len != 0);
408
409 if (*cursor >= (data + data_len - 1)) {
410 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Received sys IM is empty\n");
411 return;
412 }
413
414 len = data + data_len - *cursor;
415
416 if (NULL == (segments = split_data(*cursor, len, "\x2f", 2)))
417 return;
418
419 reply = strtol(segments[0], NULL, 10);
420 if (reply == QQ_RECV_SYS_IM_KICK_OUT)
421 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "We are kicked out by QQ server\n");
422 msg_utf8 = qq_to_utf8(segments[1], QQ_CHARSET_DEFAULT);
423 gaim_notify_warning(gc, NULL, _("System Message"), msg_utf8);
424 }
425
426 /* send an IM to to_uid */
427 void qq_send_packet_im(GaimConnection *gc, guint32 to_uid, gchar *msg, gint type)
428 {
429 qq_data *qd;
430 guint8 *cursor, *raw_data, *send_im_tail;
431 guint16 client_tag, normal_im_type;
432 gint msg_len, raw_len, font_name_len, tail_len, bytes;
433 time_t now;
434 gchar *msg_filtered;
435 GData *attribs;
436 gchar *font_size = NULL, *font_color = NULL, *font_name = NULL, *tmp;
437 gboolean is_bold = FALSE, is_italic = FALSE, is_underline = FALSE;
438 const gchar *start, *end, *last;
439
440 qd = (qq_data *) gc->proto_data;
441 client_tag = QQ_CLIENT;
442 normal_im_type = QQ_NORMAL_IM_TEXT;
443
444 last = msg;
445 while (gaim_markup_find_tag("font", last, &start, &end, &attribs)) {
446 tmp = g_datalist_get_data(&attribs, "size");
447 if (tmp) {
448 if (font_size)
449 g_free(font_size);
450 font_size = g_strdup(tmp);
451 }
452 tmp = g_datalist_get_data(&attribs, "color");
453 if (tmp) {
454 if (font_color)
455 g_free(font_color);
456 font_color = g_strdup(tmp);
457 }
458 tmp = g_datalist_get_data(&attribs, "face");
459 if (tmp) {
460 if (font_name)
461 g_free(font_name);
462 font_name = g_strdup(tmp);
463 }
464
465 g_datalist_clear(&attribs);
466 last = end + 1;
467 }
468
469 if (gaim_markup_find_tag("b", msg, &start, &end, &attribs)) {
470 is_bold = TRUE;
471 g_datalist_clear(&attribs);
472 }
473
474 if (gaim_markup_find_tag("i", msg, &start, &end, &attribs)) {
475 is_italic = TRUE;
476 g_datalist_clear(&attribs);
477 }
478
479 if (gaim_markup_find_tag("u", msg, &start, &end, &attribs)) {
480 is_underline = TRUE;
481 g_datalist_clear(&attribs);
482 }
483
484 gaim_debug(GAIM_DEBUG_INFO, "QQ_MESG", "send mesg: %s\n", msg);
485 msg_filtered = gaim_markup_strip_html(msg);
486 msg_len = strlen(msg_filtered);
487 now = time(NULL);
488
489 font_name_len = (font_name) ? strlen(font_name) : DEFAULT_FONT_NAME_LEN;
490 tail_len = font_name_len + QQ_SEND_IM_AFTER_MSG_HEADER_LEN + 1;
491
492 raw_len = QQ_SEND_IM_BEFORE_MSG_LEN + msg_len + tail_len;
493 raw_data = g_newa(guint8, raw_len);
494 cursor = raw_data;
495 bytes = 0;
496
497 /* 000-003: receiver uid */
498 bytes += create_packet_dw(raw_data, &cursor, qd->uid);
499 /* 004-007: sender uid */
500 bytes += create_packet_dw(raw_data, &cursor, to_uid);
501 /* 008-009: sender client version */
502 bytes += create_packet_w(raw_data, &cursor, client_tag);
503 /* 010-013: receiver uid */
504 bytes += create_packet_dw(raw_data, &cursor, qd->uid);
505 /* 014-017: sender uid */
506 bytes += create_packet_dw(raw_data, &cursor, to_uid);
507 /* 018-033: md5 of (uid+session_key) */
508 bytes += create_packet_data(raw_data, &cursor, qd->session_md5, 16);
509 /* 034-035: message type */
510 bytes += create_packet_w(raw_data, &cursor, normal_im_type);
511 /* 036-037: sequence number */
512 bytes += create_packet_w(raw_data, &cursor, qd->send_seq);
513 /* 038-041: send time */
514 bytes += create_packet_dw(raw_data, &cursor, (guint32) now);
515 /* 042-043: sender icon */
516 bytes += create_packet_w(raw_data, &cursor, qd->my_icon);
517 /* 044-046: always 0x00 */
518 bytes += create_packet_w(raw_data, &cursor, 0x0000);
519 bytes += create_packet_b(raw_data, &cursor, 0x00);
520 /* 047-047: we use font attr */
521 bytes += create_packet_b(raw_data, &cursor, 0x01);
522 /* 048-051: always 0x00 */
523 bytes += create_packet_dw(raw_data, &cursor, 0x00000000);
524 /* 052-052: text message type (normal/auto-reply) */
525 bytes += create_packet_b(raw_data, &cursor, type);
526 /* 053- : msg ends with 0x00 */
527 bytes += create_packet_data(raw_data, &cursor, (guint8 *) msg_filtered, msg_len);
528 send_im_tail = qq_get_send_im_tail(font_color, font_size, font_name, is_bold,
529 is_italic, is_underline, tail_len);
530 _qq_show_packet("QQ_MESG debug", send_im_tail, tail_len);
531 bytes += create_packet_data(raw_data, &cursor, send_im_tail, tail_len);
532
533 _qq_show_packet("QQ_MESG raw", raw_data, cursor - raw_data);
534
535 if (bytes == raw_len) /* create packet OK */
536 qq_send_cmd(gc, QQ_CMD_SEND_IM, TRUE, 0, TRUE, raw_data, cursor - raw_data);
537 else
538 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
539 "Fail creating send_im packet, expect %d bytes, build %d bytes\n", raw_len, bytes);
540
541 if (font_color)
542 g_free(font_color);
543 if (font_size)
544 g_free(font_size);
545 g_free(send_im_tail);
546 g_free(msg_filtered);
547 }
548
549 /* parse the reply to send_im */
550 void qq_process_send_im_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
551 {
552 qq_data *qd;
553 gint len;
554 guint8 *data, *cursor, reply;
555
556 g_return_if_fail(buf != NULL && buf_len != 0);
557
558 qd = gc->proto_data;
559 len = buf_len;
560 data = g_newa(guint8, len);
561
562 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
563 cursor = data;
564 read_packet_b(data, &cursor, len, &reply);
565 if (reply != QQ_SEND_IM_REPLY_OK) {
566 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Send IM fail\n");
567 gaim_notify_error(gc, _("Server ACK"), _("Send IM fail\n"), NULL);
568 }
569 else
570 gaim_debug(GAIM_DEBUG_INFO, "QQ", "IM ACK OK\n");
571 } else {
572 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt send im reply\n");
573 }
574 }
575
576 /* I receive a message, mainly it is text msg,
577 * but we need to proess other types (group etc) */
578 void qq_process_recv_im(guint8 *buf, gint buf_len, guint16 seq, GaimConnection *gc)
579 {
580 qq_data *qd;
581 gint len, bytes;
582 guint8 *data, *cursor;
583 qq_recv_im_header *im_header;
584
585 g_return_if_fail(buf != NULL && buf_len != 0);
586
587 qd = (qq_data *) gc->proto_data;
588 len = buf_len;
589 data = g_newa(guint8, len);
590
591 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
592 if (len < 16) { /* we need to ack with the first 16 bytes */
593 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "IM is too short\n");
594 return;
595 } else
596 _qq_send_packet_recv_im_ack(gc, seq, data);
597
598 cursor = data;
599 bytes = 0;
600 im_header = g_newa(qq_recv_im_header, 1);
601 bytes += read_packet_dw(data, &cursor, len, &(im_header->sender_uid));
602 bytes += read_packet_dw(data, &cursor, len, &(im_header->receiver_uid));
603 bytes += read_packet_dw(data, &cursor, len, &(im_header->server_im_seq));
604 /* if the message is delivered via server, it is server IP/port */
605 bytes += read_packet_data(data, &cursor, len, (guint8 *) & (im_header->sender_ip), 4);
606 bytes += read_packet_w(data, &cursor, len, &(im_header->sender_port));
607 bytes += read_packet_w(data, &cursor, len, &(im_header->im_type));
608
609 if (bytes != 20) { /* length of im_header */
610 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
611 "Fail read recv IM header, expect 20 bytes, read %d bytes\n", bytes);
612 return;
613 }
614
615 if (im_header->receiver_uid != qd->uid) { /* should not happen */
616 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "IM to [%d], NOT me\n", im_header->receiver_uid);
617 return;
618 }
619
620 switch (im_header->im_type) {
621 case QQ_RECV_IM_TO_BUDDY:
622 gaim_debug(GAIM_DEBUG_INFO, "QQ",
623 "IM from buddy [%d], I am in his/her buddy list\n", im_header->sender_uid);
624 _qq_process_recv_normal_im(data, &cursor, len, gc);
625 break;
626 case QQ_RECV_IM_TO_UNKNOWN:
627 gaim_debug(GAIM_DEBUG_INFO, "QQ",
628 "IM from buddy [%d], I am a stranger to him/her\n", im_header->sender_uid);
629 _qq_process_recv_normal_im(data, &cursor, len, gc);
630 break;
631 case QQ_RECV_IM_UNKNOWN_QUN_IM:
632 case QQ_RECV_IM_TEMP_QUN_IM:
633 case QQ_RECV_IM_QUN_IM:
634 gaim_debug(GAIM_DEBUG_INFO, "QQ", "IM from group, internal_id [%d]\n", im_header->sender_uid);
635 /* sender_uid is in fact internal_group_id */
636 qq_process_recv_group_im(data, &cursor, len, im_header->sender_uid, gc, im_header->im_type);
637 break;
638 case QQ_RECV_IM_ADD_TO_QUN:
639 gaim_debug(GAIM_DEBUG_INFO, "QQ",
640 "IM from group, added by group internal_id [%d]\n", im_header->sender_uid);
641 /* sender_uid is in fact internal_group_id
642 * we need this to create a dummy group and add to blist */
643 qq_process_recv_group_im_been_added(data, &cursor, len, im_header->sender_uid, gc);
644 break;
645 case QQ_RECV_IM_DEL_FROM_QUN:
646 gaim_debug(GAIM_DEBUG_INFO, "QQ",
647 "IM from group, removed by group internal_ID [%d]\n", im_header->sender_uid);
648 /* sender_uid is in fact internal_group_id */
649 qq_process_recv_group_im_been_removed(data, &cursor, len, im_header->sender_uid, gc);
650 break;
651 case QQ_RECV_IM_APPLY_ADD_TO_QUN:
652 gaim_debug(GAIM_DEBUG_INFO, "QQ",
653 "IM from group, apply to join group internal_ID [%d]\n", im_header->sender_uid);
654 /* sender_uid is in fact internal_group_id */
655 qq_process_recv_group_im_apply_join(data, &cursor, len, im_header->sender_uid, gc);
656 break;
657 case QQ_RECV_IM_APPROVE_APPLY_ADD_TO_QUN:
658 gaim_debug(GAIM_DEBUG_INFO, "QQ",
659 "IM for group system info, approved by group internal_id [%d]\n",
660 im_header->sender_uid);
661 /* sender_uid is in fact internal_group_id */
662 qq_process_recv_group_im_been_approved(data, &cursor, len, im_header->sender_uid, gc);
663 break;
664 case QQ_RECV_IM_REJCT_APPLY_ADD_TO_QUN:
665 gaim_debug(GAIM_DEBUG_INFO, "QQ",
666 "IM for group system info, rejected by group internal_id [%d]\n",
667 im_header->sender_uid);
668 /* sender_uid is in fact internal_group_id */
669 qq_process_recv_group_im_been_rejected(data, &cursor, len, im_header->sender_uid, gc);
670 break;
671 case QQ_RECV_IM_SYS_NOTIFICATION:
672 gaim_debug(GAIM_DEBUG_INFO, "QQ",
673 "IM from [%d], should be a system administrator\n", im_header->sender_uid);
674 _qq_process_recv_sys_im(data, &cursor, len, gc);
675 break;
676 default:
677 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
678 "IM from [%d], [0x%02x] %s is not processed\n",
679 im_header->sender_uid,
680 im_header->im_type, qq_get_recv_im_type_str(im_header->im_type));
681 }
682 } else {
683 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt rev im\n");
684 }
685 }