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