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