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 "debug.h"
|
|
24 #include "notify.h"
|
|
25 #include "request.h"
|
|
26
|
|
27 #include "buddy_info.h"
|
|
28 #include "char_conv.h"
|
|
29 #include "group_find.h"
|
14404
|
30 #include "group_internal.h"
|
14192
|
31 #include "group_info.h"
|
|
32 #include "group_join.h"
|
|
33 #include "group_network.h"
|
|
34 #include "group_opt.h"
|
|
35 #include "packet_parse.h"
|
|
36 #include "qq.h"
|
|
37 #include "utils.h"
|
|
38
|
|
39 /* TODO: can't we use qsort here? */
|
|
40 /* This implement quick sort algorithm (low->high) */
|
|
41 static void _quick_sort(gint *numbers, gint left, gint right)
|
|
42 {
|
|
43 gint pivot, l_hold, r_hold;
|
|
44
|
|
45 l_hold = left;
|
|
46 r_hold = right;
|
|
47 pivot = numbers[left];
|
|
48 while (left < right) {
|
|
49 while ((numbers[right] >= pivot) && (left < right))
|
|
50 right--;
|
|
51 if (left != right) {
|
|
52 numbers[left] = numbers[right];
|
|
53 left++;
|
|
54 }
|
|
55 while ((numbers[left] <= pivot) && (left < right))
|
|
56 left++;
|
|
57 if (left != right) {
|
|
58 numbers[right] = numbers[left];
|
|
59 right--;
|
|
60 }
|
|
61 }
|
|
62 numbers[left] = pivot;
|
|
63 pivot = left;
|
|
64 left = l_hold;
|
|
65 right = r_hold;
|
|
66 if (left < pivot)
|
|
67 _quick_sort(numbers, left, pivot - 1);
|
|
68 if (right > pivot)
|
|
69 _quick_sort(numbers, pivot + 1, right);
|
|
70 }
|
|
71
|
|
72 static void _sort(guint32 *list)
|
|
73 {
|
|
74 gint i;
|
|
75 for (i = 0; list[i] < 0xffffffff; i++) {;
|
|
76 }
|
14242
|
77 _quick_sort((gint *) list, 0, i - 1);
|
14192
|
78 }
|
|
79
|
|
80 static void _qq_group_member_opt(GaimConnection *gc, qq_group *group, gint operation, guint32 *members)
|
|
81 {
|
|
82 guint8 *data, *cursor;
|
|
83 gint i, count, data_len;
|
|
84 g_return_if_fail(gc != NULL && group != NULL && members != NULL);
|
|
85
|
|
86 for (i = 0; members[i] != 0xffffffff; i++) {;
|
|
87 }
|
|
88 count = i;
|
|
89 data_len = 6 + count * 4;
|
|
90 data = g_newa(guint8, data_len);
|
|
91 cursor = data;
|
|
92 create_packet_b(data, &cursor, QQ_GROUP_CMD_MEMBER_OPT);
|
|
93 create_packet_dw(data, &cursor, group->internal_group_id);
|
|
94 create_packet_b(data, &cursor, operation);
|
|
95 for (i = 0; i < count; i++)
|
|
96 create_packet_dw(data, &cursor, members[i]);
|
|
97 qq_send_group_cmd(gc, group, data, data_len);
|
|
98 }
|
|
99
|
|
100 static void _qq_group_do_nothing_with_struct(group_member_opt *g)
|
|
101 {
|
|
102 if (g != NULL)
|
|
103 g_free(g);
|
|
104 }
|
|
105
|
|
106 static void _qq_group_reject_application_real(group_member_opt *g, gchar *msg_utf8)
|
|
107 {
|
|
108 qq_group *group;
|
|
109 g_return_if_fail(g != NULL && g->gc != NULL && g->internal_group_id > 0 && g->member > 0);
|
14404
|
110 group = qq_group_find_by_id(g->gc, g->internal_group_id, QQ_INTERNAL_ID);
|
14192
|
111 g_return_if_fail(group != NULL);
|
|
112 qq_send_cmd_group_auth(g->gc, group, QQ_GROUP_AUTH_REQUEST_REJECT, g->member, msg_utf8);
|
|
113 g_free(g);
|
|
114 }
|
|
115
|
|
116 void qq_group_search_application_with_struct(group_member_opt *g)
|
|
117 {
|
|
118 g_return_if_fail(g != NULL && g->gc != NULL && g->member > 0);
|
|
119
|
|
120 qq_send_packet_get_info(g->gc, g->member, TRUE); /* we wanna see window */
|
|
121 gaim_request_action
|
|
122 (g->gc, NULL, _("Do you wanna approve the request?"), "", 2, g,
|
|
123 2, _("Reject"),
|
|
124 G_CALLBACK(qq_group_reject_application_with_struct),
|
|
125 _("Approve"), G_CALLBACK(qq_group_approve_application_with_struct));
|
|
126 }
|
|
127
|
|
128 void qq_group_reject_application_with_struct(group_member_opt *g)
|
|
129 {
|
|
130 gchar *msg1, *msg2;
|
|
131 g_return_if_fail(g != NULL && g->gc != NULL && g->member > 0);
|
|
132
|
|
133 msg1 = g_strdup_printf(_("You rejected %d's request"), g->member);
|
|
134 msg2 = g_strdup(_("Input your reason:"));
|
|
135
|
|
136 gaim_request_input(g->gc, NULL, msg1, msg2,
|
|
137 _("Sorry, you are not my type..."), TRUE, FALSE,
|
|
138 NULL, _("Send"),
|
|
139 G_CALLBACK(_qq_group_reject_application_real),
|
|
140 _("Cancel"), G_CALLBACK(_qq_group_do_nothing_with_struct), g);
|
|
141
|
|
142 g_free(msg1);
|
|
143 g_free(msg2);
|
|
144 }
|
|
145
|
|
146 void qq_group_approve_application_with_struct(group_member_opt *g)
|
|
147 {
|
|
148 qq_group *group;
|
|
149 g_return_if_fail(g != NULL && g->gc != NULL && g->internal_group_id > 0 && g->member > 0);
|
14404
|
150 group = qq_group_find_by_id(g->gc, g->internal_group_id, QQ_INTERNAL_ID);
|
14192
|
151 g_return_if_fail(group != NULL);
|
|
152 qq_send_cmd_group_auth(g->gc, group, QQ_GROUP_AUTH_REQUEST_APPROVE, g->member, "");
|
|
153 qq_group_find_or_add_member(g->gc, group, g->member);
|
|
154 g_free(g);
|
|
155 }
|
|
156
|
|
157 void qq_group_modify_members(GaimConnection *gc, qq_group *group, guint32 *new_members)
|
|
158 {
|
|
159 guint32 *old_members, *del_members, *add_members;
|
|
160 qq_buddy *q_bud;
|
|
161 qq_data *qd;
|
|
162 gint i = 0, old = 0, new = 0, del = 0, add = 0;
|
|
163 GList *list;
|
|
164
|
|
165 g_return_if_fail(gc != NULL && gc->proto_data != NULL && group != NULL);
|
|
166 qd = (qq_data *) gc->proto_data;
|
|
167 if (new_members[0] == 0xffffffff)
|
|
168 return;
|
|
169
|
|
170 old_members = g_newa(guint32, QQ_QUN_MEMBER_MAX);
|
|
171 del_members = g_newa(guint32, QQ_QUN_MEMBER_MAX);
|
|
172 add_members = g_newa(guint32, QQ_QUN_MEMBER_MAX);
|
|
173
|
|
174 /* construct the old member list */
|
|
175 list = group->members;
|
|
176 while (list != NULL) {
|
|
177 q_bud = (qq_buddy *) list->data;
|
|
178 if (q_bud != NULL)
|
|
179 old_members[i++] = q_bud->uid;
|
|
180 list = list->next;
|
|
181 }
|
|
182 old_members[i] = 0xffffffff; /* this is the end */
|
|
183
|
|
184 /* sort to speed up making del_members and add_members list */
|
|
185 _sort(old_members);
|
|
186 _sort(new_members);
|
|
187
|
|
188 for (old = 0, new = 0; old_members[old] < 0xffffffff || new_members[new] < 0xffffffff;) {
|
|
189 if (old_members[old] > new_members[new]) {
|
|
190 add_members[add++] = new_members[new++];
|
|
191 } else if (old_members[old] < new_members[new]) {
|
|
192 del_members[del++] = old_members[old++];
|
|
193 } else {
|
|
194 if (old_members[old] < 0xffffffff)
|
|
195 old++;
|
|
196 if (new_members[new] < 0xffffffff)
|
|
197 new++;
|
|
198 }
|
|
199 }
|
|
200 del_members[del] = add_members[add] = 0xffffffff;
|
|
201
|
|
202 for (i = 0; i < del; i++)
|
|
203 qq_group_remove_member_by_uid(group, del_members[i]);
|
|
204 for (i = 0; i < add; i++)
|
|
205 qq_group_find_or_add_member(gc, group, add_members[i]);
|
|
206
|
|
207 if (del > 0)
|
|
208 _qq_group_member_opt(gc, group, QQ_GROUP_MEMBER_DEL, del_members);
|
|
209 if (add > 0)
|
|
210 _qq_group_member_opt(gc, group, QQ_GROUP_MEMBER_ADD, add_members);
|
|
211 }
|
|
212
|
|
213 void qq_group_process_modify_members_reply(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
|
|
214 {
|
|
215 guint32 internal_group_id;
|
|
216 qq_group *group;
|
|
217 g_return_if_fail(data != NULL && gc != NULL);
|
|
218
|
|
219 read_packet_dw(data, cursor, len, &internal_group_id);
|
|
220 g_return_if_fail(internal_group_id > 0);
|
|
221
|
|
222 /* we should have its info locally */
|
14404
|
223 group = qq_group_find_by_id(gc, internal_group_id, QQ_INTERNAL_ID);
|
14192
|
224 g_return_if_fail(group != NULL);
|
|
225
|
|
226 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Succeed in modify members for Qun %d\n", group->external_group_id);
|
|
227
|
|
228 gaim_notify_info(gc, _("QQ Qun Operation"), _("You have successfully modify Qun member"), NULL);
|
|
229 }
|
|
230
|
|
231 void qq_group_modify_info(GaimConnection *gc, qq_group *group)
|
|
232 {
|
|
233 gint data_len, data_written;
|
|
234 guint8 *data, *cursor;
|
|
235 gchar *group_name, *group_desc, *notice;
|
|
236
|
|
237 g_return_if_fail(gc != NULL && group != NULL);
|
|
238
|
|
239 group_name = group->group_name_utf8 == NULL ? "" : utf8_to_qq(group->group_name_utf8, QQ_CHARSET_DEFAULT);
|
|
240 group_desc = group->group_desc_utf8 == NULL ? "" : utf8_to_qq(group->group_desc_utf8, QQ_CHARSET_DEFAULT);
|
|
241 notice = group->notice_utf8 == NULL ? "" : utf8_to_qq(group->notice_utf8, QQ_CHARSET_DEFAULT);
|
|
242
|
|
243 data_len = 13 + 1 + strlen(group_name)
|
|
244 + 1 + strlen(group_desc)
|
|
245 + 1 + strlen(notice);
|
|
246
|
|
247 data = g_newa(guint8, data_len);
|
|
248 cursor = data;
|
|
249 data_written = 0;
|
|
250 /* 000-000 */
|
|
251 data_written += create_packet_b(data, &cursor, QQ_GROUP_CMD_MODIFY_GROUP_INFO);
|
|
252 /* 001-004 */
|
|
253 data_written += create_packet_dw(data, &cursor, group->internal_group_id);
|
|
254 /* 005-005 */
|
|
255 data_written += create_packet_b(data, &cursor, 0x01);
|
|
256 /* 006-006 */
|
|
257 data_written += create_packet_b(data, &cursor, group->auth_type);
|
|
258 /* 007-008 */
|
|
259 data_written += create_packet_w(data, &cursor, 0x0000);
|
|
260 /* 009-010 */
|
|
261 data_written += create_packet_w(data, &cursor, group->group_category);
|
|
262
|
|
263 data_written += create_packet_b(data, &cursor, strlen(group_name));
|
|
264 data_written += create_packet_data(data, &cursor, (guint8 *) group_name, strlen(group_name));
|
|
265
|
|
266 data_written += create_packet_w(data, &cursor, 0x0000);
|
|
267
|
|
268 data_written += create_packet_b(data, &cursor, strlen(notice));
|
|
269 data_written += create_packet_data(data, &cursor, (guint8 *) notice, strlen(notice));
|
|
270
|
|
271 data_written += create_packet_b(data, &cursor, strlen(group_desc));
|
|
272 data_written += create_packet_data(data, &cursor, (guint8 *) group_desc, strlen(group_desc));
|
|
273
|
|
274 if (data_written != data_len)
|
|
275 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
276 "Fail to create group_modify_info packet, expect %d bytes, wrote %d bytes\n",
|
|
277 data_len, data_written);
|
|
278 else
|
|
279 qq_send_group_cmd(gc, group, data, data_len);
|
|
280 }
|
|
281
|
|
282 void qq_group_process_modify_info_reply(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
|
|
283 {
|
|
284 guint32 internal_group_id;
|
|
285 qq_group *group;
|
|
286 g_return_if_fail(data != NULL && gc != NULL);
|
|
287
|
|
288 read_packet_dw(data, cursor, len, &internal_group_id);
|
|
289 g_return_if_fail(internal_group_id > 0);
|
|
290
|
|
291 /* we should have its info locally */
|
14404
|
292 group = qq_group_find_by_id(gc, internal_group_id, QQ_INTERNAL_ID);
|
14192
|
293 g_return_if_fail(group != NULL);
|
|
294
|
|
295 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Succeed in modify info for Qun %d\n", group->external_group_id);
|
|
296 qq_group_refresh(gc, group);
|
|
297
|
|
298 gaim_notify_info(gc, _("QQ Qun Operation"), _("You have successfully modify Qun information"), NULL);
|
|
299 }
|
|
300
|
|
301 /* we create a very simple group first, and then let the user to modify */
|
|
302 void qq_group_create_with_name(GaimConnection *gc, const gchar *name)
|
|
303 {
|
|
304 gint data_len, data_written;
|
|
305 guint8 *data, *cursor;
|
|
306 qq_data *qd;
|
|
307 g_return_if_fail(gc != NULL && name != NULL);
|
|
308
|
|
309 qd = (qq_data *) gc->proto_data;
|
|
310 data_len = 7 + 1 + strlen(name) + 2 + 1 + 1 + 4;
|
|
311 data = g_newa(guint8, data_len);
|
|
312 cursor = data;
|
|
313
|
|
314 data_written = 0;
|
|
315 /* we create the simpleset group, only group name is given */
|
|
316 /* 000 */
|
|
317 data_written += create_packet_b(data, &cursor, QQ_GROUP_CMD_CREATE_GROUP);
|
|
318 /* 001 */
|
|
319 data_written += create_packet_b(data, &cursor, QQ_GROUP_TYPE_PERMANENT);
|
|
320 /* 002 */
|
|
321 data_written += create_packet_b(data, &cursor, QQ_GROUP_AUTH_TYPE_NEED_AUTH);
|
|
322 /* 003-004 */
|
|
323 data_written += create_packet_w(data, &cursor, 0x0000);
|
|
324 /* 005-006 */
|
|
325 data_written += create_packet_w(data, &cursor, 0x0003);
|
|
326 /* 007 */
|
|
327 data_written += create_packet_b(data, &cursor, strlen(name));
|
|
328 data_written += create_packet_data(data, &cursor, (guint8 *) name, strlen(name));
|
|
329 data_written += create_packet_w(data, &cursor, 0x0000);
|
|
330 data_written += create_packet_b(data, &cursor, 0x00); /* no group notice */
|
|
331 data_written += create_packet_b(data, &cursor, 0x00); /* no group desc */
|
|
332 data_written += create_packet_dw(data, &cursor, qd->uid); /* I am member of coz */
|
|
333
|
|
334 if (data_written != data_len)
|
|
335 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
336 "Fail create create_group packet, expect %d bytes, written %d bytes\n",
|
|
337 data_len, data_written);
|
|
338 else
|
|
339 qq_send_group_cmd(gc, NULL, data, data_len);
|
|
340 }
|
|
341
|
|
342 static void qq_group_setup_with_gc_and_uid(gc_and_uid *g)
|
|
343 {
|
|
344 qq_group *group;
|
|
345 g_return_if_fail(g != NULL && g->gc != NULL && g->uid > 0);
|
|
346
|
14404
|
347 group = qq_group_find_by_id(g->gc, g->uid, QQ_INTERNAL_ID);
|
14192
|
348 g_return_if_fail(group != NULL);
|
|
349
|
14404
|
350 /* TODO insert UI code here */
|
14192
|
351 /* qq_group_detail_window_show(g->gc, group); */
|
|
352 g_free(g);
|
|
353 }
|
|
354
|
|
355 void qq_group_process_create_group_reply(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
|
|
356 {
|
|
357 guint32 internal_group_id, external_group_id;
|
|
358 qq_group *group;
|
|
359 gc_and_uid *g;
|
|
360 qq_data *qd;
|
|
361
|
|
362 g_return_if_fail(data != NULL && gc != NULL);
|
|
363 g_return_if_fail(gc->proto_data != NULL);
|
|
364 qd = (qq_data *) gc->proto_data;
|
|
365
|
|
366 read_packet_dw(data, cursor, len, &internal_group_id);
|
|
367 read_packet_dw(data, cursor, len, &external_group_id);
|
|
368 g_return_if_fail(internal_group_id > 0 && external_group_id);
|
|
369
|
14404
|
370 group = qq_group_create_internal_record(gc, internal_group_id, external_group_id, NULL);
|
14192
|
371 group->my_status = QQ_GROUP_MEMBER_STATUS_IS_ADMIN;
|
|
372 group->creator_uid = qd->uid;
|
|
373 qq_group_refresh(gc, group);
|
|
374
|
|
375 qq_group_activate_group(gc, internal_group_id);
|
|
376 qq_send_cmd_group_get_group_info(gc, group);
|
|
377
|
|
378 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Succeed in create Qun, external ID %d\n", group->external_group_id);
|
|
379
|
|
380 g = g_new0(gc_and_uid, 1);
|
|
381 g->gc = gc;
|
|
382 g->uid = internal_group_id;
|
|
383
|
|
384 gaim_request_action(gc, _("QQ Qun Operation"),
|
|
385 _("You have successfully created a Qun"),
|
|
386 _
|
|
387 ("Would you like to set up the Qun details now?"),
|
|
388 1, g, 2, _("Setup"),
|
|
389 G_CALLBACK(qq_group_setup_with_gc_and_uid),
|
|
390 _("Cancel"), G_CALLBACK(qq_do_nothing_with_gc_and_uid));
|
|
391 }
|
|
392
|
|
393 /* we have to activate group after creation, otherwise the group can not be searched */
|
|
394 void qq_group_activate_group(GaimConnection *gc, guint32 internal_group_id)
|
|
395 {
|
|
396 gint data_len, data_written;
|
|
397 guint8 *data, *cursor;
|
|
398 g_return_if_fail(gc != NULL && internal_group_id > 0);
|
|
399
|
|
400 data_len = 5;
|
|
401 data = g_newa(guint8, data_len);
|
|
402 cursor = data;
|
|
403
|
|
404 data_written = 0;
|
|
405 /* we create the simplest group, only group name is given */
|
|
406 /* 000 */
|
|
407 data_written += create_packet_b(data, &cursor, QQ_GROUP_CMD_ACTIVATE_GROUP);
|
|
408 /* 001-005 */
|
|
409 data_written += create_packet_dw(data, &cursor, internal_group_id);
|
|
410
|
|
411 if (data_written != data_len)
|
|
412 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
413 "Fail create activate_group packet, expect %d bytes, written %d bytes\n",
|
|
414 data_len, data_written);
|
|
415 else
|
|
416 qq_send_group_cmd(gc, NULL, data, data_len);
|
|
417 }
|
|
418
|
|
419 void qq_group_process_activate_group_reply(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
|
|
420 {
|
|
421 guint32 internal_group_id;
|
|
422 qq_group *group;
|
|
423 g_return_if_fail(data != NULL && gc != NULL);
|
|
424
|
|
425 read_packet_dw(data, cursor, len, &internal_group_id);
|
|
426 g_return_if_fail(internal_group_id > 0);
|
|
427
|
|
428 /* we should have its info locally */
|
14404
|
429 group = qq_group_find_by_id(gc, internal_group_id, QQ_INTERNAL_ID);
|
14192
|
430 g_return_if_fail(group != NULL);
|
|
431
|
|
432 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Succeed in activate Qun %d\n", group->external_group_id);
|
|
433 }
|
|
434
|
|
435 void qq_group_manage_group(GaimConnection *gc, GHashTable *data)
|
|
436 {
|
|
437 gchar *internal_group_id_ptr;
|
|
438 guint32 internal_group_id;
|
|
439 qq_group *group;
|
|
440
|
|
441 g_return_if_fail(gc != NULL && data != NULL);
|
|
442
|
|
443 internal_group_id_ptr = g_hash_table_lookup(data, "internal_group_id");
|
|
444 internal_group_id = strtol(internal_group_id_ptr, NULL, 10);
|
|
445 g_return_if_fail(internal_group_id > 0);
|
|
446
|
14404
|
447 group = qq_group_find_by_id(gc, internal_group_id, QQ_INTERNAL_ID);
|
14192
|
448 g_return_if_fail(group != NULL);
|
|
449
|
|
450 /* XXX insert UI code here */
|
|
451 /* qq_group_detail_window_show(gc, group); */
|
|
452 }
|