13627
|
1 /**
|
|
2 * @file msn-utils.c Utility functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24 #include "msn.h"
|
|
25 #include "msn-utils.h"
|
19785
|
26 //#include <openssl/md5.h>
|
13627
|
27
|
|
28 void
|
|
29 msn_parse_format(const char *mime, char **pre_ret, char **post_ret)
|
|
30 {
|
|
31 char *cur;
|
|
32 GString *pre = g_string_new(NULL);
|
|
33 GString *post = g_string_new(NULL);
|
|
34 unsigned int colors[3];
|
|
35
|
|
36 if (pre_ret != NULL) *pre_ret = NULL;
|
|
37 if (post_ret != NULL) *post_ret = NULL;
|
|
38
|
|
39 cur = strstr(mime, "FN=");
|
|
40
|
|
41 if (cur && (*(cur = cur + 3) != ';'))
|
|
42 {
|
|
43 pre = g_string_append(pre, "<FONT FACE=\"");
|
|
44
|
|
45 while (*cur && *cur != ';')
|
|
46 {
|
|
47 pre = g_string_append_c(pre, *cur);
|
|
48 cur++;
|
|
49 }
|
|
50
|
|
51 pre = g_string_append(pre, "\">");
|
|
52 post = g_string_prepend(post, "</FONT>");
|
|
53 }
|
|
54
|
|
55 cur = strstr(mime, "EF=");
|
|
56
|
|
57 if (cur && (*(cur = cur + 3) != ';'))
|
|
58 {
|
|
59 while (*cur && *cur != ';')
|
|
60 {
|
|
61 pre = g_string_append_c(pre, '<');
|
|
62 pre = g_string_append_c(pre, *cur);
|
|
63 pre = g_string_append_c(pre, '>');
|
|
64 post = g_string_prepend_c(post, '>');
|
|
65 post = g_string_prepend_c(post, *cur);
|
|
66 post = g_string_prepend_c(post, '/');
|
|
67 post = g_string_prepend_c(post, '<');
|
|
68 cur++;
|
|
69 }
|
|
70 }
|
|
71
|
|
72 cur = strstr(mime, "CO=");
|
|
73
|
|
74 if (cur && (*(cur = cur + 3) != ';'))
|
|
75 {
|
|
76 int i;
|
|
77
|
|
78 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]);
|
|
79
|
|
80 if (i > 0)
|
|
81 {
|
|
82 char tag[64];
|
|
83
|
|
84 if (i == 1)
|
|
85 {
|
|
86 colors[1] = 0;
|
|
87 colors[2] = 0;
|
|
88 }
|
|
89 else if (i == 2)
|
|
90 {
|
|
91 unsigned int temp = colors[0];
|
|
92
|
|
93 colors[0] = colors[1];
|
|
94 colors[1] = temp;
|
|
95 colors[2] = 0;
|
|
96 }
|
|
97 else if (i == 3)
|
|
98 {
|
|
99 unsigned int temp = colors[2];
|
|
100
|
|
101 colors[2] = colors[0];
|
|
102 colors[0] = temp;
|
|
103 }
|
|
104
|
|
105 g_snprintf(tag, sizeof(tag),
|
|
106 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">",
|
|
107 colors[0], colors[1], colors[2]);
|
|
108
|
|
109 pre = g_string_append(pre, tag);
|
|
110 post = g_string_prepend(post, "</FONT>");
|
|
111 }
|
|
112 }
|
|
113
|
|
114 cur = g_strdup(gaim_url_decode(pre->str));
|
|
115 g_string_free(pre, TRUE);
|
|
116
|
|
117 if (pre_ret != NULL)
|
|
118 *pre_ret = cur;
|
|
119 else
|
|
120 g_free(cur);
|
|
121
|
|
122 cur = g_strdup(gaim_url_decode(post->str));
|
|
123 g_string_free(post, TRUE);
|
|
124
|
|
125 if (post_ret != NULL)
|
|
126 *post_ret = cur;
|
|
127 else
|
|
128 g_free(cur);
|
|
129 }
|
|
130
|
|
131 /*
|
|
132 * We need this because we're only supposed to encode spaces in the font
|
|
133 * names. gaim_url_encode() isn't acceptable.
|
|
134 */
|
|
135 static const char *
|
|
136 encode_spaces(const char *str)
|
|
137 {
|
|
138 static char buf[BUF_LEN];
|
|
139 const char *c;
|
|
140 char *d;
|
|
141
|
|
142 g_return_val_if_fail(str != NULL, NULL);
|
|
143
|
|
144 for (c = str, d = buf; *c != '\0'; c++)
|
|
145 {
|
|
146 if (*c == ' ')
|
|
147 {
|
|
148 *d++ = '%';
|
|
149 *d++ = '2';
|
|
150 *d++ = '0';
|
|
151 }
|
|
152 else
|
|
153 *d++ = *c;
|
|
154 }
|
|
155
|
|
156 return buf;
|
|
157 }
|
|
158
|
|
159 /*
|
|
160 * Taken from the zephyr plugin.
|
|
161 * This parses HTML formatting (put out by one of the gtkimhtml widgets
|
|
162 * and converts it to msn formatting. It doesn't deal with the tag closing,
|
|
163 * but gtkimhtml widgets give valid html.
|
|
164 * It currently deals properly with <b>, <u>, <i>, <font face=...>,
|
|
165 * <font color=...>.
|
|
166 * It ignores <font back=...> and <font size=...>
|
|
167 */
|
|
168 void
|
|
169 msn_import_html(const char *html, char **attributes, char **message)
|
|
170 {
|
|
171 int len, retcount = 0;
|
|
172 const char *c;
|
|
173 char *msg;
|
|
174 char *fontface = NULL;
|
|
175 char fonteffect[4];
|
|
176 char fontcolor[7];
|
|
177
|
|
178 g_return_if_fail(html != NULL);
|
|
179 g_return_if_fail(attributes != NULL);
|
|
180 g_return_if_fail(message != NULL);
|
|
181
|
|
182 len = strlen(html);
|
|
183 msg = g_malloc0(len + 1);
|
|
184
|
|
185 memset(fontcolor, 0, sizeof(fontcolor));
|
|
186 strcat(fontcolor, "0");
|
|
187 memset(fonteffect, 0, sizeof(fonteffect));
|
|
188
|
|
189 for (c = html; *c != '\0';)
|
|
190 {
|
|
191 if (*c == '<')
|
|
192 {
|
|
193 if (!g_ascii_strncasecmp(c + 1, "br>", 3))
|
|
194 {
|
|
195 msg[retcount++] = '\r';
|
|
196 msg[retcount++] = '\n';
|
|
197 c += 4;
|
|
198 }
|
|
199 else if (!g_ascii_strncasecmp(c + 1, "i>", 2))
|
|
200 {
|
|
201 strcat(fonteffect, "I");
|
|
202 c += 3;
|
|
203 }
|
|
204 else if (!g_ascii_strncasecmp(c + 1, "b>", 2))
|
|
205 {
|
|
206 strcat(fonteffect, "B");
|
|
207 c += 3;
|
|
208 }
|
|
209 else if (!g_ascii_strncasecmp(c + 1, "u>", 2))
|
|
210 {
|
|
211 strcat(fonteffect, "U");
|
|
212 c += 3;
|
|
213 }
|
|
214 else if (!g_ascii_strncasecmp(c + 1, "s>", 2))
|
|
215 {
|
|
216 strcat(fonteffect, "S");
|
|
217 c += 3;
|
|
218 }
|
|
219 else if (!g_ascii_strncasecmp(c + 1, "a href=\"", 8))
|
|
220 {
|
|
221 c += 9;
|
|
222
|
|
223 if (!g_ascii_strncasecmp(c, "mailto:", 7))
|
|
224 c += 7;
|
|
225
|
|
226 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
|
|
227 msg[retcount++] = *c++;
|
|
228
|
|
229 if (*c != '\0')
|
|
230 c += 2;
|
|
231
|
|
232 /* ignore descriptive string */
|
|
233 while ((*c != '\0') && g_ascii_strncasecmp(c, "</a>", 4))
|
|
234 c++;
|
|
235
|
|
236 if (*c != '\0')
|
|
237 c += 4;
|
|
238 }
|
|
239 else if (!g_ascii_strncasecmp(c + 1, "font", 4))
|
|
240 {
|
|
241 c += 5;
|
|
242
|
|
243 while ((*c != '\0') && !g_ascii_strncasecmp(c, " ", 1))
|
|
244 c++;
|
|
245
|
|
246 if (!g_ascii_strncasecmp(c, "color=\"#", 7))
|
|
247 {
|
|
248 c += 8;
|
|
249
|
|
250 fontcolor[0] = *(c + 4);
|
|
251 fontcolor[1] = *(c + 5);
|
|
252 fontcolor[2] = *(c + 2);
|
|
253 fontcolor[3] = *(c + 3);
|
|
254 fontcolor[4] = *c;
|
|
255 fontcolor[5] = *(c + 1);
|
|
256
|
|
257 c += 8;
|
|
258 }
|
|
259 else if (!g_ascii_strncasecmp(c, "face=\"", 6))
|
|
260 {
|
|
261 const char *end = NULL;
|
|
262 const char *comma = NULL;
|
|
263 unsigned int namelen = 0;
|
|
264
|
|
265 c += 6;
|
|
266 end = strchr(c, '\"');
|
|
267 comma = strchr(c, ',');
|
|
268
|
|
269 if (comma == NULL || comma > end)
|
|
270 namelen = (unsigned int)(end - c);
|
|
271 else
|
|
272 namelen = (unsigned int)(comma - c);
|
|
273
|
|
274 fontface = g_strndup(c, namelen);
|
|
275 c = end + 2;
|
|
276 }
|
|
277 else
|
|
278 {
|
|
279 /* Drop all unrecognized/misparsed font tags */
|
|
280 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
|
|
281 c++;
|
|
282
|
|
283 if (*c != '\0')
|
|
284 c += 2;
|
|
285 }
|
|
286 }
|
|
287 else
|
|
288 {
|
|
289 while ((*c != '\0') && (*c != '>'))
|
|
290 c++;
|
|
291 if (*c != '\0')
|
|
292 c++;
|
|
293 }
|
|
294 }
|
|
295 else if (*c == '&')
|
|
296 {
|
|
297 if (!g_ascii_strncasecmp(c, "<", 4))
|
|
298 {
|
|
299 msg[retcount++] = '<';
|
|
300 c += 4;
|
|
301 }
|
|
302 else if (!g_ascii_strncasecmp(c, ">", 4))
|
|
303 {
|
|
304 msg[retcount++] = '>';
|
|
305 c += 4;
|
|
306 }
|
|
307 else if (!g_ascii_strncasecmp(c, " ", 6))
|
|
308 {
|
|
309 msg[retcount++] = ' ';
|
|
310 c += 6;
|
|
311 }
|
|
312 else if (!g_ascii_strncasecmp(c, """, 6))
|
|
313 {
|
|
314 msg[retcount++] = '"';
|
|
315 c += 6;
|
|
316 }
|
|
317 else if (!g_ascii_strncasecmp(c, "&", 5))
|
|
318 {
|
|
319 msg[retcount++] = '&';
|
|
320 c += 5;
|
|
321 }
|
|
322 else if (!g_ascii_strncasecmp(c, "'", 6))
|
|
323 {
|
|
324 msg[retcount++] = '\'';
|
|
325 c += 6;
|
|
326 }
|
|
327 else
|
|
328 msg[retcount++] = *c++;
|
|
329 }
|
|
330 else
|
|
331 msg[retcount++] = *c++;
|
|
332 }
|
|
333
|
|
334 if (fontface == NULL)
|
|
335 fontface = g_strdup("MS Sans Serif");
|
|
336
|
|
337 *attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0",
|
|
338 encode_spaces(fontface),
|
|
339 fonteffect, fontcolor);
|
|
340 *message = g_strdup(msg);
|
|
341
|
|
342 g_free(fontface);
|
|
343 g_free(msg);
|
|
344 }
|
|
345
|
|
346 void
|
|
347 msn_parse_socket(const char *str, char **ret_host, int *ret_port)
|
|
348 {
|
|
349 char *host;
|
|
350 char *c;
|
|
351 int port;
|
|
352
|
|
353 host = g_strdup(str);
|
|
354
|
19784
|
355 if ((c = strchr(host, ':')) != NULL){
|
13627
|
356 *c = '\0';
|
|
357 port = atoi(c + 1);
|
19784
|
358 }else{
|
|
359 port = 1863;
|
13627
|
360 }
|
|
361
|
|
362 *ret_host = host;
|
|
363 *ret_port = port;
|
|
364 }
|
19784
|
365
|
19785
|
366 /*check the edian of system*/
|
|
367 int
|
|
368 isBigEndian(void)
|
|
369 {
|
|
370 short int word = 0x0100;
|
|
371 char *byte = (char *)&word;
|
|
372
|
|
373 return(byte[0]);
|
|
374 }
|
|
375
|
|
376 /*swap utility*/
|
|
377 unsigned int
|
|
378 swapInt(unsigned int dw)
|
|
379 {
|
|
380 unsigned int tmp;
|
|
381 tmp = (dw & 0x000000FF);
|
|
382 tmp = ((dw & 0x0000FF00) >> 0x08) | (tmp << 0x08);
|
|
383 tmp = ((dw & 0x00FF0000) >> 0x10) | (tmp << 0x08);
|
|
384 tmp = ((dw & 0xFF000000) >> 0x18) | (tmp << 0x08);
|
|
385 return(tmp);
|
|
386 }
|
|
387
|
|
388 /*
|
19788
|
389 * Handle MSN Chanllege computation
|
19785
|
390 *This algorithm reference with http://msnpiki.msnfanatic.com/index.php/MSNP11:Challenges
|
|
391 */
|
|
392 #define BUFSIZE 256
|
|
393 void
|
|
394 msn_handle_chl(char *input, char *output)
|
|
395 {
|
|
396 GaimCipher *cipher;
|
|
397 GaimCipherContext *context;
|
|
398 char *productKey = MSNP13_WLM_PRODUCT_KEY,
|
|
399 *productID = MSNP13_WLM_PRODUCT_ID,
|
|
400 *hexChars = "0123456789abcdef",
|
|
401 buf[BUFSIZE];
|
|
402 unsigned char md5Hash[16], *newHash;
|
|
403 unsigned int *md5Parts, *chlStringParts, newHashParts[5];
|
|
404
|
|
405 long long nHigh=0, nLow=0;
|
|
406
|
|
407 int i, bigEndian;
|
|
408
|
|
409 /* Determine our endianess */
|
|
410 bigEndian = isBigEndian();
|
|
411
|
|
412 /* Create the MD5 hash by using Gaim MD5 algorithm*/
|
|
413 cipher = gaim_ciphers_find_cipher("md5");
|
|
414 context = gaim_cipher_context_new(cipher, NULL);
|
|
415
|
|
416 gaim_cipher_context_append(context, (const guchar *)input,
|
|
417 strlen(input));
|
|
418 gaim_cipher_context_append(context, (const guchar *)productKey,
|
|
419 strlen(productKey));
|
|
420 gaim_cipher_context_digest(context, sizeof(md5Hash), md5Hash, NULL);
|
|
421 gaim_cipher_context_destroy(context);
|
|
422
|
|
423 /* Split it into four integers */
|
|
424 md5Parts = (unsigned int *)md5Hash;
|
|
425 for(i=0; i<4; i++){
|
|
426 /* check for endianess */
|
|
427 if(bigEndian)
|
|
428 md5Parts[i] = swapInt(md5Parts[i]);
|
|
429
|
|
430 /* & each integer with 0x7FFFFFFF */
|
|
431 /* and save one unmodified array for later */
|
|
432 newHashParts[i] = md5Parts[i];
|
|
433 md5Parts[i] &= 0x7FFFFFFF;
|
|
434 }
|
|
435
|
|
436 /* make a new string and pad with '0' */
|
|
437 snprintf(buf, BUFSIZE-5, "%s%s", input, productID);
|
|
438 i = strlen(buf);
|
|
439 memset(&buf[i], '0', 8 - (i % 8));
|
|
440 buf[i + (8 - (i % 8))]='\0';
|
|
441
|
|
442 /* split into integers */
|
|
443 chlStringParts = (unsigned int *)buf;
|
|
444
|
|
445 /* this is magic */
|
|
446 for (i=0; i<(strlen(buf)/4)-1; i+=2){
|
|
447 long long temp;
|
|
448
|
|
449 if(bigEndian){
|
|
450 chlStringParts[i] = swapInt(chlStringParts[i]);
|
|
451 chlStringParts[i+1] = swapInt(chlStringParts[i+1]);
|
|
452 }
|
|
453
|
|
454 temp=(md5Parts[0] * (((0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF)+nHigh) + md5Parts[1])%0x7FFFFFFF;
|
|
455 nHigh=(md5Parts[2] * (((long long)chlStringParts[i+1]+temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
|
|
456 nLow=nLow + nHigh + temp;
|
|
457 }
|
|
458 nHigh=(nHigh+md5Parts[1]) % 0x7FFFFFFF;
|
|
459 nLow=(nLow+md5Parts[3]) % 0x7FFFFFFF;
|
|
460
|
|
461 newHashParts[0]^=nHigh;
|
|
462 newHashParts[1]^=nLow;
|
|
463 newHashParts[2]^=nHigh;
|
|
464 newHashParts[3]^=nLow;
|
|
465
|
|
466 /* swap more bytes if big endian */
|
|
467 for(i=0; i<4 && bigEndian; i++)
|
|
468 newHashParts[i] = swapInt(newHashParts[i]);
|
|
469
|
|
470 /* make a string of the parts */
|
|
471 newHash = (unsigned char *)newHashParts;
|
|
472
|
|
473 /* convert to hexadecimal */
|
|
474 for (i=0; i<16; i++)
|
|
475 {
|
|
476 output[i*2]=hexChars[(newHash[i]>>4)&0xF];
|
|
477 output[(i*2)+1]=hexChars[newHash[i]&0xF];
|
|
478 }
|
|
479
|
|
480 output[32]='\0';
|
|
481
|
|
482 // gaim_debug_info("MaYuan","chl output{%s}\n",output);
|
|
483 }
|
|
484
|