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
|
14242
|
23 #include "cipher.h"
|
|
24 #include "limits.h"
|
14192
|
25 #include "stdlib.h"
|
|
26 #include "string.h"
|
|
27
|
|
28 #ifdef _WIN32
|
|
29 #include "win32dep.h"
|
|
30 #endif
|
|
31
|
|
32 #include "char_conv.h"
|
|
33 #include "debug.h"
|
|
34 #include "prefs.h"
|
14242
|
35 #include "qq.h"
|
14192
|
36 #include "util.h"
|
|
37 #include "utils.h"
|
|
38
|
14319
|
39 #define QQ_NAME_FORMAT "%d"
|
14192
|
40
|
|
41 gchar *get_name_by_index_str(gchar **array, const gchar *index_str, gint amount)
|
|
42 {
|
|
43 gint index;
|
|
44
|
|
45 index = atoi(index_str);
|
|
46 if (index < 0 || index >= amount)
|
|
47 index = 0;
|
|
48
|
|
49 return array[index];
|
|
50 }
|
|
51
|
|
52 gchar *get_index_str_by_name(gchar **array, const gchar *name, gint amount)
|
|
53 {
|
|
54 gint index;
|
|
55
|
|
56 for (index = 0; index <= amount; index++)
|
|
57 if (g_ascii_strcasecmp(array[index], name) == 0)
|
|
58 break;
|
|
59
|
|
60 if (index >= amount)
|
|
61 index = 0; /* meaning no match */
|
|
62 return g_strdup_printf("%d", index);
|
|
63 }
|
|
64
|
|
65 gint qq_string_to_dec_value(const gchar *str)
|
|
66 {
|
|
67 g_return_val_if_fail(str != NULL, 0);
|
|
68 return strtol(str, NULL, 10);
|
|
69 }
|
|
70
|
|
71 /* split the given data(len) with delimit,
|
|
72 * check the number of field matches the expected_fields (<=0 means all)
|
|
73 * return gchar* array (needs to be freed by g_strfreev later), or NULL */
|
|
74 gchar **split_data(guint8 *data, gint len, const gchar *delimit, gint expected_fields)
|
|
75 {
|
|
76 guint8 *input;
|
|
77 gchar **segments;
|
|
78 gint i, j;
|
|
79
|
|
80 g_return_val_if_fail(data != NULL && len != 0 && delimit != 0, NULL);
|
|
81
|
|
82 /* as the last field would be string, but data is not ended with 0x00
|
|
83 * we have to duplicate the data and append a 0x00 at the end */
|
|
84 input = g_newa(guint8, len + 1);
|
|
85 g_memmove(input, data, len);
|
|
86 input[len] = 0x00;
|
|
87
|
|
88 segments = g_strsplit((gchar *) input, delimit, 0);
|
|
89 if (expected_fields <= 0)
|
|
90 return segments;
|
|
91
|
|
92 for (i = 0; segments[i] != NULL; i++) {;
|
|
93 }
|
|
94 if (i < expected_fields) { /* not enough fields */
|
|
95 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
96 "Invalid data, expect %d fields, found only %d, discard\n", expected_fields, i);
|
|
97 g_strfreev(segments);
|
|
98 return NULL;
|
|
99 } else if (i > expected_fields) { /* more fields, OK */
|
|
100 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
101 "Dangerous data, expect %d fields, found %d, return all\n", expected_fields, i);
|
|
102 /* free up those not used */
|
|
103 for (j = expected_fields; j < i; j++) {
|
|
104 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "field[%d] is %s\n", j, segments[j]);
|
|
105 g_free(segments[j]);
|
|
106 }
|
|
107
|
|
108 segments[expected_fields] = NULL;
|
|
109 }
|
|
110
|
|
111 return segments;
|
|
112 }
|
|
113
|
14242
|
114 /* generate a md5 key using uid and session_key */
|
|
115 guint8 *_gen_session_md5(gint uid, guint8 *session_key)
|
|
116 {
|
|
117 guint8 *src, md5_str[QQ_KEY_LENGTH];
|
|
118 GaimCipher *cipher;
|
|
119 GaimCipherContext *context;
|
|
120
|
|
121 src = g_newa(guint8, 20);
|
|
122 memcpy(src, &uid, 4);
|
|
123 memcpy(src, session_key, QQ_KEY_LENGTH);
|
|
124
|
|
125 cipher = gaim_ciphers_find_cipher("md5");
|
|
126 context = gaim_cipher_context_new(cipher, NULL);
|
|
127 gaim_cipher_context_append(context, src, 20);
|
|
128 gaim_cipher_context_digest(context, sizeof(md5_str), md5_str, NULL);
|
|
129 gaim_cipher_context_destroy(context);
|
|
130
|
|
131 return g_memdup(md5_str, QQ_KEY_LENGTH);
|
|
132 }
|
|
133
|
14192
|
134 /* given a four-byte ip data, convert it into a human readable ip string
|
|
135 * the return needs to be freed */
|
|
136 gchar *gen_ip_str(guint8 *ip)
|
|
137 {
|
|
138 gchar *ret;
|
|
139 if (ip == NULL || ip[0] == 0) {
|
|
140 ret = g_new(gchar, 1);
|
|
141 *ret = '\0';
|
|
142 return ret;
|
|
143 } else {
|
|
144 return g_strdup_printf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
|
145 }
|
|
146 }
|
|
147
|
|
148 guint8 *str_ip_gen(gchar *str) {
|
|
149 guint8 *ip = g_new(guint8, 4);
|
14236
|
150 gint a, b, c, d;
|
|
151
|
14192
|
152 sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d);
|
|
153 ip[0] = a;
|
|
154 ip[1] = b;
|
|
155 ip[2] = c;
|
|
156 ip[3] = d;
|
|
157 return ip;
|
|
158 }
|
|
159
|
|
160 /* return the QQ icon file name
|
|
161 * the return needs to be freed */
|
14265
|
162 gchar *get_icon_name(gint set)
|
14192
|
163 {
|
14265
|
164 return g_strdup_printf("qq_%d", set);
|
14192
|
165 }
|
|
166
|
14319
|
167 /* convert Gaim name to original QQ UID */
|
14236
|
168 guint32 gaim_name_to_uid(const gchar *const name)
|
14192
|
169 {
|
14319
|
170 guint32 ret;
|
|
171 g_return_val_if_fail(name != NULL, 0);
|
14192
|
172
|
14319
|
173 ret = strtol(name, NULL, 10);
|
|
174 if (errno == ERANGE)
|
|
175 return 0;
|
|
176 else
|
|
177 return ret;
|
14192
|
178 }
|
|
179
|
14404
|
180 /* convert a QQ UID to a unique name of Gaim
|
|
181 * the return needs to be freed */
|
|
182 gchar *uid_to_gaim_name(guint32 uid)
|
|
183 {
|
|
184 return g_strdup_printf(QQ_NAME_FORMAT, uid);
|
|
185 }
|
|
186
|
|
187 /* convert name displayed in a chat channel to original QQ UID */
|
|
188 gchar *chat_name_to_gaim_name(const gchar *const name)
|
|
189 {
|
|
190 const gchar *tmp;
|
|
191 gchar *ret;
|
|
192
|
|
193 g_return_val_if_fail(name != NULL, NULL);
|
|
194
|
|
195 tmp = (gchar *) gaim_strcasestr(name, "(qq-");
|
|
196 ret = g_strndup(tmp + 4, strlen(name) - (tmp - name) - 4 - 1);
|
|
197
|
|
198 return ret;
|
|
199 }
|
|
200
|
14192
|
201 /* try to dump the data as GBK */
|
14236
|
202 void try_dump_as_gbk(const guint8 *const data, gint len)
|
14192
|
203 {
|
|
204 gint i;
|
|
205 guint8 *incoming;
|
|
206 gchar *msg_utf8;
|
|
207
|
|
208 incoming = g_newa(guint8, len + 1);
|
|
209 g_memmove(incoming, data, len);
|
|
210 incoming[len] = 0x00;
|
|
211 /* GBK code:
|
|
212 * Single-byte ASCII: 0x21-0x7E
|
|
213 * GBK first byte range: 0x81-0xFE
|
|
214 * GBK second byte range: 0x40-0x7E and 0x80-0xFE */
|
|
215 for (i = 0; i < len; i++)
|
|
216 if (incoming[i] >= 0x81)
|
|
217 break;
|
|
218
|
|
219 msg_utf8 = i < len ? qq_to_utf8((gchar *) &incoming[i], QQ_CHARSET_DEFAULT) : NULL;
|
|
220
|
|
221 if (msg_utf8 != NULL) {
|
|
222 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Try extract GB msg: %s\n", msg_utf8);
|
|
223 g_free(msg_utf8);
|
|
224 }
|
|
225 }
|
|
226
|
|
227 /* strips whitespace */
|
14236
|
228 static gchar *strstrip(const gchar *const buffer)
|
14192
|
229 {
|
|
230 GString *stripped;
|
14237
|
231 gchar *ret, cur;
|
|
232 gint i;
|
14192
|
233
|
|
234 g_return_val_if_fail(buffer != NULL, NULL);
|
|
235
|
|
236 stripped = g_string_new("");
|
|
237 for (i=0; i<strlen(buffer); i++) {
|
14237
|
238 cur = buffer[i];
|
|
239 if (cur != ' ' && cur != '\n')
|
14192
|
240 g_string_append_c(stripped, buffer[i]);
|
|
241 }
|
|
242 ret = stripped->str;
|
|
243 g_string_free(stripped, FALSE);
|
|
244
|
|
245 return ret;
|
|
246 }
|
|
247
|
14236
|
248 /* Attempts to dump an ASCII hex string to a string of bytes.
|
|
249 * The return should be freed later. */
|
|
250 guint8 *hex_str_to_bytes(const gchar *const buffer, gint *out_len)
|
14192
|
251 {
|
|
252 gchar *hex_str, *hex_buffer, *cursor, tmp;
|
|
253 guint8 *bytes, nibble1, nibble2;
|
|
254 gint index;
|
|
255
|
|
256 g_return_val_if_fail(buffer != NULL, NULL);
|
|
257
|
|
258 hex_buffer = strstrip(buffer);
|
|
259
|
|
260 if (strlen(hex_buffer) % 2 != 0) {
|
|
261 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
262 "Unable to convert an odd number of nibbles to a string of bytes!\n");
|
|
263 g_free(hex_buffer);
|
|
264 return NULL;
|
|
265 }
|
|
266 bytes = g_newa(guint8, strlen(hex_buffer) / 2);
|
|
267 hex_str = g_ascii_strdown(hex_buffer, -1);
|
|
268 g_free(hex_buffer);
|
|
269 index = 0;
|
|
270 for (cursor = hex_str; cursor < hex_str + sizeof(gchar) * (strlen(hex_str)) - 1; cursor++) {
|
|
271 if (g_ascii_isdigit(*cursor)) {
|
|
272 tmp = *cursor; nibble1 = atoi(&tmp);
|
|
273 } else if (g_ascii_isalpha(*cursor) && (gint) *cursor - 87 < 16) {
|
|
274 nibble1 = (gint) *cursor - 87;
|
|
275 } else {
|
|
276 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
14237
|
277 "Invalid char \'%c\' found in hex string!\n", *cursor);
|
14192
|
278 g_free(hex_str);
|
|
279 return NULL;
|
|
280 }
|
|
281 nibble1 = nibble1 << 4;
|
|
282 cursor++;
|
|
283 if (g_ascii_isdigit(*cursor)) {
|
|
284 tmp = *cursor; nibble2 = atoi(&tmp);
|
|
285 } else if (g_ascii_isalpha(*cursor) && (gint) (*cursor - 87) < 16) {
|
|
286 nibble2 = (gint) *cursor - 87;
|
|
287 } else {
|
|
288 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
|
|
289 "Invalid char found in hex string!\n");
|
|
290 g_free(hex_str);
|
|
291 return NULL;
|
|
292 }
|
|
293 bytes[index++] = nibble1 + nibble2;
|
|
294 }
|
|
295 *out_len = strlen(hex_str) / 2;
|
|
296 g_free(hex_str);
|
|
297 return g_memdup(bytes, *out_len);
|
|
298 }
|
|
299
|
14236
|
300 /* Dumps a chunk of raw data into an ASCII hex string.
|
|
301 * The return should be freed later. */
|
|
302 gchar *hex_dump_to_str(const guint8 *const buffer, gint bytes)
|
14192
|
303 {
|
|
304 GString *str;
|
|
305 gchar *ret;
|
|
306 gint i, j, ch;
|
|
307
|
|
308 str = g_string_new("");
|
|
309 for (i = 0; i < bytes; i += 16) {
|
|
310 /* length label */
|
|
311 g_string_append_printf(str, "%04d: ", i);
|
|
312
|
|
313 /* dump hex value */
|
|
314 for (j = 0; j < 16; j++)
|
|
315 if ((i + j) < bytes)
|
|
316 g_string_append_printf(str, " %02X", buffer[i + j]);
|
|
317 else
|
|
318 g_string_append(str, " ");
|
|
319 g_string_append(str, " ");
|
|
320
|
|
321 /* dump ascii value */
|
|
322 for (j = 0; j < 16 && (i + j) < bytes; j++) {
|
|
323 ch = buffer[i + j] & 127;
|
|
324 if (ch < ' ' || ch == 127)
|
|
325 g_string_append_c(str, '.');
|
|
326 else
|
|
327 g_string_append_c(str, ch);
|
|
328 }
|
|
329 g_string_append_c(str, '\n');
|
|
330 }
|
|
331
|
|
332 ret = str->str;
|
|
333 /* GString can be freed without freeing it character data */
|
|
334 g_string_free(str, FALSE);
|
|
335
|
|
336 return ret;
|
|
337 }
|