comparison src/protocols/qq/sys_msg.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 ef8490f9e823
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 "debug.h" // gaim_debug
26 #include "internal.h" // _("get_text")
27 #include "notify.h" // gaim_noitfy_xx
28 #include "request.h" // gaim_request_action
29
30 #include "utils.h" // hex_dump_to_str
31 #include "packet_parse.h" // create_packet_
32 #include "buddy_info.h" // qq_send_packet_get_info
33 #include "buddy_list.h" // qq_send_packet_get_buddies_online
34 #include "buddy_opt.h" // gc_and_uid
35 #include "char_conv.h" // qq_to_utf8
36 #include "crypt.h" // qq_crypt
37 #include "header_info.h" // cmd alias
38 #include "send_core.h" // qq_send_cmd
39 #include "sys_msg.h"
40 #include "qq.h" // qq_data
41
42 enum {
43 QQ_MSG_SYS_BEING_ADDED = 0x01,
44 QQ_MSG_SYS_ADD_CONTACT_REQUEST = 0x02,
45 QQ_MSG_SYS_ADD_CONTACT_APPROVED = 0x03,
46 QQ_MSG_SYS_ADD_CONTACT_REJECTED = 0x04,
47 QQ_MSG_SYS_NEW_VERSION = 0x09,
48 };
49
50 /* Henry: private function for reading/writing of system log */
51 static void _qq_sys_msg_log_write(GaimConnection *gc, gchar *msg, gchar *from)
52 {
53 GaimLog *log;
54 GaimAccount *account;
55
56 account = gaim_connection_get_account(gc);
57 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
58
59 log = gaim_log_new(GAIM_LOG_IM,
60 "systemim",
61 account,
62 NULL, //gfhuang
63 time(NULL),
64 NULL
65 );
66 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, from,
67 time(NULL), msg);
68 gaim_log_free(log);
69 }
70
71 /*****************************************************************************/
72 // suggested by rakescar@linuxsir, can still approve after search
73 static void _qq_search_before_auth_with_gc_and_uid(gc_and_uid * g)
74 {
75 GaimConnection *gc;
76 guint32 uid;
77
78 g_return_if_fail(g != NULL);
79
80 gc = g->gc;
81 uid = g->uid;
82 g_return_if_fail(gc != 0 && uid != 0);
83
84 qq_send_packet_get_info(gc, uid, TRUE); // we wanna see window
85 gaim_request_action
86 (gc, NULL, _("Do you wanna approve the request?"), "", 2, g, 2,
87 _("Reject"),
88 G_CALLBACK(qq_reject_add_request_with_gc_and_uid),
89 _("Approve"), G_CALLBACK(qq_approve_add_request_with_gc_and_uid));
90
91 } // _qq_search_before_auth_with_gc_and_uid
92
93 /*****************************************************************************/
94 static void _qq_search_before_add_with_gc_and_uid(gc_and_uid * g)
95 {
96 GaimConnection *gc;
97 guint32 uid;
98
99 g_return_if_fail(g != NULL);
100
101 gc = g->gc;
102 uid = g->uid;
103 g_return_if_fail(gc != 0 && uid != 0);
104
105 qq_send_packet_get_info(gc, uid, TRUE); // we wanna see window
106 gaim_request_action
107 (gc, NULL, _("Do you wanna add this buddy?"), "", 2, g, 2,
108 _("Cancel"), NULL, _("Add"), G_CALLBACK(qq_add_buddy_with_gc_and_uid));
109
110 } // _qq_search_before_add_with_gc_and_uid
111
112 /*****************************************************************************/
113 // Send ACK if the sys message needs an ACK
114 static void _qq_send_packet_ack_msg_sys(GaimConnection * gc, guint8 code, guint32 from, guint16 seq) {
115 guint8 bar, *ack, *cursor;
116 gchar *str;
117 gint ack_len, bytes;
118
119 str = g_strdup_printf("%d", from);
120 bar = 0x1e;
121 ack_len = 1 + 1 + strlen(str) + 1 + 2;
122 ack = g_newa(guint8, ack_len);
123 cursor = ack;
124 bytes = 0;
125
126 bytes += create_packet_b(ack, &cursor, code);
127 bytes += create_packet_b(ack, &cursor, bar);
128 bytes += create_packet_data(ack, &cursor, str, strlen(str));
129 bytes += create_packet_b(ack, &cursor, bar);
130 bytes += create_packet_w(ack, &cursor, seq);
131
132 g_free(str);
133
134 if (bytes == ack_len) // creation OK
135 qq_send_cmd(gc, QQ_CMD_ACK_SYS_MSG, TRUE, 0, FALSE, ack, ack_len);
136 else
137 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
138 "Fail creating sys msg ACK, expect %d bytes, build %d bytes\n", ack_len, bytes);
139
140 } // _qq_send_packet_ack_msg_sys
141
142 /*****************************************************************************/
143 // when you are added by a person, QQ server will send sys message
144 static void _qq_process_msg_sys_being_added(GaimConnection * gc, gchar * from, gchar * to, gchar * msg_utf8) {
145 gchar *message;
146 GaimBuddy *b;
147 guint32 uid;
148 gc_and_uid *g;
149 gchar *name; //for memory leak bug, by gfhuang
150
151 g_return_if_fail(gc != NULL && from != NULL && to != NULL);
152
153 uid = strtol(from, NULL, 10);
154 name = uid_to_gaim_name(uid); //by gfhuang
155 b = gaim_find_buddy(gc->account, name);
156 g_free(name);
157 if (b == NULL) { // the person is not in my list
158 g = g_new0(gc_and_uid, 1);
159 g->gc = gc;
160 g->uid = uid; // only need to get value
161 message = g_strdup_printf(_("You have been added by %s"), from);
162 _qq_sys_msg_log_write(gc, message, from);
163 gaim_request_action(gc, NULL, message,
164 _("Would like to add him?"), 2, g, 3,
165 _("Cancel"), NULL, _("Add"),
166 G_CALLBACK
167 (qq_add_buddy_with_gc_and_uid),
168 _("Search"), G_CALLBACK(_qq_search_before_add_with_gc_and_uid));
169 } else {
170 message = g_strdup_printf(_("%s has added you [%s]"), from, to);
171 _qq_sys_msg_log_write(gc, message, from);
172 gaim_notify_info(gc, NULL, message, NULL);
173 }
174
175 g_free(message);
176 } // qq_process_msg_sys_being_added
177
178 /*****************************************************************************/
179 // you are rejected by the person
180 static void _qq_process_msg_sys_add_contact_rejected(GaimConnection * gc, gchar * from, gchar * to, gchar * msg_utf8) {
181 gchar *message, *reason;
182
183 g_return_if_fail(gc != NULL && from != NULL && to != NULL);
184
185 message = g_strdup_printf(_("User %s rejected your request"), from);
186 reason = g_strdup_printf(_("Reason: %s"), msg_utf8);
187 _qq_sys_msg_log_write(gc, message, from);
188
189 gaim_notify_info(gc, NULL, message, reason);
190 g_free(message);
191 g_free(reason);
192 } // qq_process_msg_sys_add_contact_rejected
193
194 /*****************************************************************************/
195 // the buddy approves your request of adding him/her as your friend
196 static void _qq_process_msg_sys_add_contact_approved(GaimConnection * gc, gchar * from, gchar * to, gchar * msg_utf8) {
197 gchar *message;
198 qq_data *qd;
199
200 g_return_if_fail(gc != NULL && from != NULL && to != NULL);
201
202 qd = (qq_data *) gc->proto_data;
203 qq_add_buddy_by_recv_packet(gc, strtol(from, NULL, 10), TRUE, TRUE);
204
205 message = g_strdup_printf(_("Use %s has approved your request"), from);
206 _qq_sys_msg_log_write(gc, message, from);
207 gaim_notify_info(gc, NULL, message, NULL);
208
209 g_free(message);
210 } // qq_process_msg_sys_add_contact_approved
211
212 /*****************************************************************************/
213 // someone wants to add you to his buddy list
214 static void _qq_process_msg_sys_add_contact_request(GaimConnection * gc, gchar * from, gchar * to, gchar * msg_utf8) {
215 gchar *message, *reason;
216 guint32 uid;
217 gc_and_uid *g, *g2;
218 GaimBuddy *b;
219 gchar *name; // by gfhuang
220
221 g_return_if_fail(gc != NULL && from != NULL && to != NULL);
222
223 uid = strtol(from, NULL, 10);
224 g = g_new0(gc_and_uid, 1);
225 g->gc = gc;
226 g->uid = uid;
227
228 message = g_strdup_printf(_("%s wanna add you [%s] as friends"), from, to);
229 reason = g_strdup_printf(_("Message: %s"), msg_utf8);
230 _qq_sys_msg_log_write(gc, message, from);
231
232 gaim_request_action
233 (gc, NULL, message, reason, 2, g, 3,
234 _("Reject"),
235 G_CALLBACK(qq_reject_add_request_with_gc_and_uid),
236 _("Approve"),
237 G_CALLBACK(qq_approve_add_request_with_gc_and_uid),
238 _("Search"), G_CALLBACK(_qq_search_before_auth_with_gc_and_uid));
239
240 g_free(message);
241 g_free(reason);
242
243 name = uid_to_gaim_name(uid); //by gfhuang
244 b = gaim_find_buddy(gc->account, name);
245 g_free(name);
246 if (b == NULL) { // the person is not in my list
247 g2 = g_new0(gc_and_uid, 1);
248 g2->gc = gc;
249 g2->uid = strtol(from, NULL, 10);
250 message = g_strdup_printf(_("%s is not in your buddy list"), from);
251 gaim_request_action(gc, NULL, message,
252 _("Would you like to add him?"), 2, g2,
253 3, _("Cancel"), NULL, _("Add"),
254 G_CALLBACK
255 (qq_add_buddy_with_gc_and_uid),
256 _("Search"), G_CALLBACK(_qq_search_before_add_with_gc_and_uid));
257 g_free(message);
258 } // if b== NULL
259
260 } // qq_process_msg_sys_add_contact_request
261
262 /*****************************************************************************/
263 void qq_process_msg_sys(guint8 * buf, gint buf_len, guint16 seq, GaimConnection * gc) {
264 qq_data *qd;
265 gint len;
266 guint8 *data;
267 gchar **segments, *code, *from, *to, *msg, *msg_utf8;
268
269 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
270 g_return_if_fail(buf != NULL && buf_len != 0);
271
272 qd = (qq_data *) gc->proto_data;
273 len = buf_len;
274 data = g_newa(gchar, len);
275
276 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
277 if (NULL == (segments = split_data(data, len, "\x1f", 4)))
278 return;
279 code = segments[0];
280 from = segments[1];
281 to = segments[2];
282 msg = segments[3];
283
284 _qq_send_packet_ack_msg_sys(gc, code[0], strtol(from, NULL, 10), seq);
285
286 if (strtol(to, NULL, 10) != qd->uid) { // not to me
287 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Recv sys msg to [%s], not me!, discard\n", to);
288 g_strfreev(segments);
289 return;
290 }
291
292 msg_utf8 = qq_to_utf8(msg, QQ_CHARSET_DEFAULT);
293 switch (strtol(code, NULL, 10)) {
294 case QQ_MSG_SYS_BEING_ADDED:
295 _qq_process_msg_sys_being_added(gc, from, to, msg_utf8);
296 break;
297 case QQ_MSG_SYS_ADD_CONTACT_REQUEST:
298 _qq_process_msg_sys_add_contact_request(gc, from, to, msg_utf8);
299 break;
300 case QQ_MSG_SYS_ADD_CONTACT_APPROVED:
301 _qq_process_msg_sys_add_contact_approved(gc, from, to, msg_utf8);
302 break;
303 case QQ_MSG_SYS_ADD_CONTACT_REJECTED:
304 _qq_process_msg_sys_add_contact_rejected(gc, from, to, msg_utf8);
305 break;
306 case QQ_MSG_SYS_NEW_VERSION:
307 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
308 "QQ server says there is newer version than %s\n", qq_get_source_str(QQ_CLIENT));
309 break;
310 default:
311 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Recv unknown sys msg code: %s\n", code);
312 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "the msg is : %s\n", msg_utf8);
313 } // switch code
314 g_free(msg_utf8);
315 g_strfreev(segments);
316
317 } else
318 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt recv msg sys\n");
319
320 } // qq_process_msg_sys
321
322 /*****************************************************************************/
323 // END OF FILE