comparison src/protocols/qq/im.c @ 13870:983fd420e86b

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