core/protocols/qq/im.c

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

mercurial