comparison libgaim/protocols/qq/group_join.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 8ff8f1c897b5
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 #include "server.h"
27
28 #include "buddy_opt.h"
29 #include "char_conv.h"
30 #include "group_conv.h"
31 #include "group_find.h"
32 #include "group_free.h"
33 #include "group_hash.h"
34 #include "group_info.h"
35 #include "group_join.h"
36 #include "group_opt.h"
37 #include "group_network.h"
38
39 enum {
40 QQ_GROUP_JOIN_OK = 0x01,
41 QQ_GROUP_JOIN_NEED_AUTH = 0x02,
42 };
43
44 static void _qq_group_exit_with_gc_and_id(gc_and_uid *g)
45 {
46 GaimConnection *gc;
47 guint32 internal_group_id;
48 qq_group *group;
49
50 g_return_if_fail(g != NULL && g->gc != NULL && g->uid > 0);
51 gc = g->gc;
52 internal_group_id = g->uid;
53
54 group = qq_group_find_by_internal_group_id(gc, internal_group_id);
55 g_return_if_fail(group != NULL);
56
57 qq_send_cmd_group_exit_group(gc, group);
58 }
59
60 /* send packet to join a group without auth */
61 static void _qq_send_cmd_group_join_group(GaimConnection *gc, qq_group *group)
62 {
63 guint8 *raw_data, *cursor;
64 gint bytes, data_len;
65
66 g_return_if_fail(gc != NULL && group != NULL);
67 if (group->my_status == QQ_GROUP_MEMBER_STATUS_NOT_MEMBER) {
68 group->my_status = QQ_GROUP_MEMBER_STATUS_APPLYING;
69 qq_group_refresh(gc, group);
70 }
71
72 data_len = 5;
73 raw_data = g_newa(guint8, data_len);
74 cursor = raw_data;
75
76 bytes = 0;
77 bytes += create_packet_b(raw_data, &cursor, QQ_GROUP_CMD_JOIN_GROUP);
78 bytes += create_packet_dw(raw_data, &cursor, group->internal_group_id);
79
80 if (bytes != data_len)
81 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
82 "Fail create packet for %s\n", qq_group_cmd_get_desc(QQ_GROUP_CMD_JOIN_GROUP));
83 else
84 qq_send_group_cmd(gc, group, raw_data, data_len);
85 }
86
87 static void _qq_group_join_auth_with_gc_and_id(gc_and_uid *g, const gchar *reason_utf8)
88 {
89 GaimConnection *gc;
90 qq_group *group;
91 guint32 internal_group_id;
92
93 g_return_if_fail(g != NULL && g->gc != NULL && g->uid > 0);
94 gc = g->gc;
95 internal_group_id = g->uid;
96
97 group = qq_group_find_by_internal_group_id(gc, internal_group_id);
98 if (group == NULL) {
99 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Can not find qq_group by internal_id: %d\n", internal_group_id);
100 return;
101 } else { /* everything is OK */
102 qq_send_cmd_group_auth(gc, group, QQ_GROUP_AUTH_REQUEST_APPLY, 0, reason_utf8);
103 }
104 }
105
106 static void _qq_group_join_auth(GaimConnection *gc, qq_group *group)
107 {
108 gchar *msg;
109 gc_and_uid *g;
110 g_return_if_fail(gc != NULL && group != NULL);
111
112 gaim_debug(GAIM_DEBUG_INFO, "QQ",
113 "Group (internal id: %d) needs authentication\n", group->internal_group_id);
114
115 msg = g_strdup_printf("Group \"%s\" needs authentication\n", group->group_name_utf8);
116 g = g_new0(gc_and_uid, 1);
117 g->gc = gc;
118 g->uid = group->internal_group_id;
119 gaim_request_input(gc, NULL, msg,
120 _("Input request here"),
121 _("Would you be my friend?"), TRUE, FALSE, NULL,
122 _("Send"),
123 G_CALLBACK(_qq_group_join_auth_with_gc_and_id),
124 _("Cancel"), G_CALLBACK(qq_do_nothing_with_gc_and_uid), g);
125 g_free(msg);
126 }
127
128 void qq_send_cmd_group_auth(GaimConnection *gc, qq_group *group, guint8 opt, guint32 uid, const gchar *reason_utf8)
129 {
130 guint8 *raw_data, *cursor;
131 gchar *reason_qq;
132 gint bytes, data_len;
133
134 g_return_if_fail(gc != NULL && group != NULL);
135
136 if (reason_utf8 == NULL || strlen(reason_utf8) == 0)
137 reason_qq = g_strdup("");
138 else
139 reason_qq = utf8_to_qq(reason_utf8, QQ_CHARSET_DEFAULT);
140
141 if (opt == QQ_GROUP_AUTH_REQUEST_APPLY) {
142 group->my_status = QQ_GROUP_MEMBER_STATUS_APPLYING;
143 qq_group_refresh(gc, group);
144 uid = 0;
145 }
146
147 data_len = 10 + strlen(reason_qq) + 1;
148 raw_data = g_newa(guint8, data_len);
149 cursor = raw_data;
150
151 bytes = 0;
152 bytes += create_packet_b(raw_data, &cursor, QQ_GROUP_CMD_JOIN_GROUP_AUTH);
153 bytes += create_packet_dw(raw_data, &cursor, group->internal_group_id);
154 bytes += create_packet_b(raw_data, &cursor, opt);
155 bytes += create_packet_dw(raw_data, &cursor, uid);
156 bytes += create_packet_b(raw_data, &cursor, strlen(reason_qq));
157 bytes += create_packet_data(raw_data, &cursor, (guint8 *) reason_qq, strlen(reason_qq));
158
159 if (bytes != data_len)
160 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
161 "Fail create packet for %s\n", qq_group_cmd_get_desc(QQ_GROUP_CMD_JOIN_GROUP_AUTH));
162 else
163 qq_send_group_cmd(gc, group, raw_data, data_len);
164 }
165
166 /* send packet to exit one group
167 * In fact, this will never be used for GAIM
168 * when we remove a GaimChat node, there is no user controlable callback
169 * so we only remove the GaimChat node,
170 * but we never use this cmd to update the server side
171 * anyway, it is function, as when we remove the GaimChat node,
172 * user has no way to start up the chat conversation window
173 * therefore even we are still in it,
174 * the group IM will not show up to bother us. (Limited by GAIM) */
175 void qq_send_cmd_group_exit_group(GaimConnection *gc, qq_group *group)
176 {
177 guint8 *raw_data, *cursor;
178 gint bytes, data_len;
179
180 g_return_if_fail(gc != NULL && group != NULL);
181
182 data_len = 5;
183 raw_data = g_newa(guint8, data_len);
184 cursor = raw_data;
185
186 bytes = 0;
187 bytes += create_packet_b(raw_data, &cursor, QQ_GROUP_CMD_EXIT_GROUP);
188 bytes += create_packet_dw(raw_data, &cursor, group->internal_group_id);
189
190 if (bytes != data_len)
191 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
192 "Fail create packet for %s\n", qq_group_cmd_get_desc(QQ_GROUP_CMD_EXIT_GROUP));
193 else
194 qq_send_group_cmd(gc, group, raw_data, data_len);
195 }
196
197 /* If comes here, cmd is OK already */
198 void qq_process_group_cmd_exit_group(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
199 {
200 gint bytes, expected_bytes;
201 guint32 internal_group_id;
202 GaimChat *chat;
203 qq_group *group;
204 qq_data *qd;
205
206 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
207 g_return_if_fail(data != NULL && len > 0);
208 qd = (qq_data *) gc->proto_data;
209
210 bytes = 0;
211 expected_bytes = 4;
212 bytes += read_packet_dw(data, cursor, len, &internal_group_id);
213
214 if (bytes == expected_bytes) {
215 group = qq_group_find_by_internal_group_id(gc, internal_group_id);
216 if (group != NULL) {
217 chat =
218 gaim_blist_find_chat
219 (gaim_connection_get_account(gc), g_strdup_printf("%d", group->external_group_id));
220 if (chat != NULL)
221 gaim_blist_remove_chat(chat);
222 qq_group_remove_by_internal_group_id(qd, internal_group_id);
223 }
224 gaim_notify_info(gc, _("QQ Qun Operation"), _("You have successfully exit group"), NULL);
225 } else {
226 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
227 "Invalid exit group reply, expect %d bytes, read %d bytes\n", expected_bytes, bytes);
228 }
229 }
230
231 /* Process the reply to group_auth subcmd */
232 void qq_process_group_cmd_join_group_auth(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
233 {
234 gint bytes, expected_bytes;
235 guint32 internal_group_id;
236 qq_data *qd;
237
238 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
239 g_return_if_fail(data != NULL && len > 0);
240 qd = (qq_data *) gc->proto_data;
241
242 bytes = 0;
243 expected_bytes = 4;
244 bytes += read_packet_dw(data, cursor, len, &internal_group_id);
245 g_return_if_fail(internal_group_id > 0);
246
247 if (bytes == expected_bytes)
248 gaim_notify_info
249 (gc, _("QQ Group Auth"), _("You authorization operation has been accepted by QQ server"), NULL);
250 else
251 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
252 "Invalid join group reply, expect %d bytes, read %d bytes\n", expected_bytes, bytes);
253 }
254
255 /* process group cmd reply "join group" */
256 void qq_process_group_cmd_join_group(guint8 *data, guint8 **cursor, gint len, GaimConnection *gc)
257 {
258 gint bytes, expected_bytes;
259 guint32 internal_group_id;
260 guint8 reply;
261 qq_group *group;
262
263 g_return_if_fail(gc != NULL && data != NULL && len > 0);
264
265 bytes = 0;
266 expected_bytes = 5;
267 bytes += read_packet_dw(data, cursor, len, &internal_group_id);
268 bytes += read_packet_b(data, cursor, len, &reply);
269
270 if (bytes != expected_bytes) {
271 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
272 "Invalid join group reply, expect %d bytes, read %d bytes\n", expected_bytes, bytes);
273 return;
274 } else { /* join group OK */
275 group = qq_group_find_by_internal_group_id(gc, internal_group_id);
276 /* need to check if group is NULL or not. */
277 g_return_if_fail(group != NULL);
278 switch (reply) {
279 case QQ_GROUP_JOIN_OK:
280 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Succeed joining group \"%s\"\n", group->group_name_utf8);
281 group->my_status = QQ_GROUP_MEMBER_STATUS_IS_MEMBER;
282 qq_group_refresh(gc, group);
283 /* this must be show before getting online member */
284 qq_group_conv_show_window(gc, group);
285 qq_send_cmd_group_get_group_info(gc, group);
286 break;
287 case QQ_GROUP_JOIN_NEED_AUTH:
288 gaim_debug(GAIM_DEBUG_INFO, "QQ",
289 "Fail joining group [%d] %s, needs authentication\n",
290 group->external_group_id, group->group_name_utf8);
291 group->my_status = QQ_GROUP_MEMBER_STATUS_NOT_MEMBER;
292 qq_group_refresh(gc, group);
293 _qq_group_join_auth(gc, group);
294 break;
295 default:
296 gaim_debug(GAIM_DEBUG_INFO, "QQ",
297 "Error joining group [%d] %s, unknown reply: 0x%02x\n",
298 group->external_group_id, group->group_name_utf8, reply);
299 }
300 }
301 }
302
303 /* Apply to join one group without auth */
304 void qq_group_join(GaimConnection *gc, GHashTable *data)
305 {
306 gchar *internal_group_id_ptr;
307 guint32 internal_group_id;
308 qq_group *group;
309
310 g_return_if_fail(gc != NULL && data != NULL);
311
312 internal_group_id_ptr = g_hash_table_lookup(data, "internal_group_id");
313 internal_group_id = strtol(internal_group_id_ptr, NULL, 10);
314
315 g_return_if_fail(internal_group_id > 0);
316
317 /* for those we have subscribed, they should have been put into
318 * qd->groups in qq_group_init subroutine */
319 group = qq_group_find_by_internal_group_id(gc, internal_group_id);
320 if (group == NULL)
321 group = qq_group_from_hashtable(gc, data);
322
323 g_return_if_fail(group != NULL);
324
325 switch (group->auth_type) {
326 case QQ_GROUP_AUTH_TYPE_NO_AUTH:
327 case QQ_GROUP_AUTH_TYPE_NEED_AUTH:
328 _qq_send_cmd_group_join_group(gc, group);
329 break;
330 case QQ_GROUP_AUTH_TYPE_NO_ADD:
331 gaim_notify_warning(gc, NULL, _("This group does not allow others to join"), NULL);
332 break;
333 default:
334 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Unknown group auth type: %d\n", group->auth_type);
335 }
336 }
337
338 void qq_group_exit(GaimConnection *gc, GHashTable *data)
339 {
340 gchar *internal_group_id_ptr;
341 guint32 internal_group_id;
342 gc_and_uid *g;
343
344 g_return_if_fail(gc != NULL && data != NULL);
345
346 internal_group_id_ptr = g_hash_table_lookup(data, "internal_group_id");
347 internal_group_id = strtol(internal_group_id_ptr, NULL, 10);
348
349 g_return_if_fail(internal_group_id > 0);
350
351 g = g_new0(gc_and_uid, 1);
352 g->gc = gc;
353 g->uid = internal_group_id;
354
355 gaim_request_action(gc, _("QQ Qun Operation"),
356 _("Are you sure to exit this Qun?"),
357 _
358 ("Note, if you are the creator, \nthis operation will eventually remove this Qun."),
359 1, g, 2, _("Cancel"),
360 G_CALLBACK(qq_do_nothing_with_gc_and_uid),
361 _("Go ahead"), G_CALLBACK(_qq_group_exit_with_gc_and_id));
362 }