Mercurial > pidgin.yaz
annotate libgaim/util.c @ 15106:b2b0839f57d0
[gaim-migrate @ 17891]
Outgoing message throttling. If we're sending messages too quickly,
and Gaim thinks that sending another message will make AIM give us
a warning, then delay sending the message a little bit.
Currently only activated for IMs. Let me know if you see any problems.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 04 Dec 2006 07:47:50 +0000 |
parents | 9a69964d8c18 |
children | b81e4e44b509 |
rev | line source |
---|---|
14192 | 1 /* |
2 * @file util.h Utility Functions | |
3 * @ingroup core | |
4 * | |
5 * Gaim is the legal property of its developers, whose names are too numerous | |
6 * to list here. Please refer to the COPYRIGHT file distributed with this | |
7 * source distribution. | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
23 #include "internal.h" | |
24 | |
25 #include "conversation.h" | |
26 #include "debug.h" | |
27 #include "notify.h" | |
28 #include "prpl.h" | |
29 #include "prefs.h" | |
30 #include "util.h" | |
31 | |
14354 | 32 struct _GaimUtilFetchUrlData |
14192 | 33 { |
14354 | 34 GaimUtilFetchUrlCallback callback; |
14192 | 35 void *user_data; |
36 | |
37 struct | |
38 { | |
39 char *user; | |
40 char *passwd; | |
41 char *address; | |
42 int port; | |
43 char *page; | |
44 | |
45 } website; | |
46 | |
47 char *url; | |
48 gboolean full; | |
49 char *user_agent; | |
50 gboolean http11; | |
51 char *request; | |
52 gsize request_written; | |
53 gboolean include_headers; | |
54 | |
14354 | 55 GaimProxyConnectData *connect_data; |
56 int fd; | |
57 guint inpa; | |
14192 | 58 |
59 gboolean got_headers; | |
60 gboolean has_explicit_data_len; | |
61 char *webdata; | |
62 unsigned long len; | |
63 unsigned long data_len; | |
14354 | 64 }; |
14192 | 65 |
66 static char custom_home_dir[MAXPATHLEN]; | |
67 static char home_dir[MAXPATHLEN]; | |
68 | |
69 GaimMenuAction * | |
70 gaim_menu_action_new(const char *label, GaimCallback callback, gpointer data, | |
71 GList *children) | |
72 { | |
73 GaimMenuAction *act = g_new0(GaimMenuAction, 1); | |
74 act->label = g_strdup(label); | |
75 act->callback = callback; | |
76 act->data = data; | |
77 act->children = children; | |
78 return act; | |
79 } | |
80 | |
81 void | |
82 gaim_menu_action_free(GaimMenuAction *act) | |
83 { | |
84 g_return_if_fail(act != NULL); | |
85 | |
86 g_free(act->label); | |
87 g_free(act); | |
88 } | |
89 | |
90 /************************************************************************** | |
91 * Base16 Functions | |
92 **************************************************************************/ | |
93 gchar * | |
94 gaim_base16_encode(const guchar *data, gsize len) | |
95 { | |
96 int i; | |
97 gchar *ascii = NULL; | |
98 | |
99 g_return_val_if_fail(data != NULL, NULL); | |
100 g_return_val_if_fail(len > 0, NULL); | |
101 | |
102 ascii = g_malloc(len * 2 + 1); | |
103 | |
104 for (i = 0; i < len; i++) | |
105 snprintf(&ascii[i * 2], 3, "%02hhx", data[i]); | |
106 | |
107 return ascii; | |
108 } | |
109 | |
110 guchar * | |
111 gaim_base16_decode(const char *str, gsize *ret_len) | |
112 { | |
113 int len, i, accumulator = 0; | |
114 guchar *data; | |
115 | |
116 g_return_val_if_fail(str != NULL, NULL); | |
117 | |
118 len = strlen(str); | |
119 | |
120 g_return_val_if_fail(strlen(str) > 0, 0); | |
15040 | 121 g_return_val_if_fail(len % 2 == 0, 0); |
14192 | 122 |
123 data = g_malloc(len / 2); | |
124 | |
125 for (i = 0; i < len; i++) | |
126 { | |
127 if ((i % 2) == 0) | |
128 accumulator = 0; | |
129 else | |
130 accumulator <<= 4; | |
131 | |
132 if (isdigit(str[i])) | |
133 accumulator |= str[i] - 48; | |
134 else | |
135 { | |
136 switch(str[i]) | |
137 { | |
138 case 'a': case 'A': accumulator |= 10; break; | |
139 case 'b': case 'B': accumulator |= 11; break; | |
140 case 'c': case 'C': accumulator |= 12; break; | |
141 case 'd': case 'D': accumulator |= 13; break; | |
142 case 'e': case 'E': accumulator |= 14; break; | |
143 case 'f': case 'F': accumulator |= 15; break; | |
144 } | |
145 } | |
146 | |
147 if (i % 2) | |
148 data[(i - 1) / 2] = accumulator; | |
149 } | |
150 | |
151 if (ret_len != NULL) | |
152 *ret_len = len / 2; | |
153 | |
154 return data; | |
155 } | |
156 | |
157 /************************************************************************** | |
158 * Base64 Functions | |
159 **************************************************************************/ | |
160 static const char alphabet[] = | |
161 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
162 "0123456789+/"; | |
163 | |
164 static const char xdigits[] = | |
165 "0123456789abcdef"; | |
166 | |
167 gchar * | |
168 gaim_base64_encode(const guchar *data, gsize len) | |
169 { | |
170 char *out, *rv; | |
171 | |
172 g_return_val_if_fail(data != NULL, NULL); | |
173 g_return_val_if_fail(len > 0, NULL); | |
174 | |
175 rv = out = g_malloc(((len/3)+1)*4 + 1); | |
176 | |
177 for (; len >= 3; len -= 3) | |
178 { | |
179 *out++ = alphabet[data[0] >> 2]; | |
180 *out++ = alphabet[((data[0] << 4) & 0x30) | (data[1] >> 4)]; | |
181 *out++ = alphabet[((data[1] << 2) & 0x3c) | (data[2] >> 6)]; | |
182 *out++ = alphabet[data[2] & 0x3f]; | |
183 data += 3; | |
184 } | |
185 | |
186 if (len > 0) | |
187 { | |
188 unsigned char fragment; | |
189 | |
190 *out++ = alphabet[data[0] >> 2]; | |
191 fragment = (data[0] << 4) & 0x30; | |
192 | |
193 if (len > 1) | |
194 fragment |= data[1] >> 4; | |
195 | |
196 *out++ = alphabet[fragment]; | |
197 *out++ = (len < 2) ? '=' : alphabet[(data[1] << 2) & 0x3c]; | |
198 *out++ = '='; | |
199 } | |
200 | |
201 *out = '\0'; | |
202 | |
203 return rv; | |
204 } | |
205 | |
206 guchar * | |
207 gaim_base64_decode(const char *str, gsize *ret_len) | |
208 { | |
209 guchar *out = NULL; | |
210 char tmp = 0; | |
211 const char *c; | |
212 gint32 tmp2 = 0; | |
213 int len = 0, n = 0; | |
214 | |
215 g_return_val_if_fail(str != NULL, NULL); | |
216 | |
217 c = str; | |
218 | |
219 while (*c) { | |
220 if (*c >= 'A' && *c <= 'Z') { | |
221 tmp = *c - 'A'; | |
222 } else if (*c >= 'a' && *c <= 'z') { | |
223 tmp = 26 + (*c - 'a'); | |
224 } else if (*c >= '0' && *c <= 57) { | |
225 tmp = 52 + (*c - '0'); | |
226 } else if (*c == '+') { | |
227 tmp = 62; | |
228 } else if (*c == '/') { | |
229 tmp = 63; | |
230 } else if (*c == '\r' || *c == '\n') { | |
231 c++; | |
232 continue; | |
233 } else if (*c == '=') { | |
234 if (n == 3) { | |
235 out = g_realloc(out, len + 2); | |
236 out[len] = (guchar)(tmp2 >> 10) & 0xff; | |
237 len++; | |
238 out[len] = (guchar)(tmp2 >> 2) & 0xff; | |
239 len++; | |
240 } else if (n == 2) { | |
241 out = g_realloc(out, len + 1); | |
242 out[len] = (guchar)(tmp2 >> 4) & 0xff; | |
243 len++; | |
244 } | |
245 break; | |
246 } | |
247 tmp2 = ((tmp2 << 6) | (tmp & 0xff)); | |
248 n++; | |
249 if (n == 4) { | |
250 out = g_realloc(out, len + 3); | |
251 out[len] = (guchar)((tmp2 >> 16) & 0xff); | |
252 len++; | |
253 out[len] = (guchar)((tmp2 >> 8) & 0xff); | |
254 len++; | |
255 out[len] = (guchar)(tmp2 & 0xff); | |
256 len++; | |
257 tmp2 = 0; | |
258 n = 0; | |
259 } | |
260 c++; | |
261 } | |
262 | |
263 out = g_realloc(out, len + 1); | |
264 out[len] = 0; | |
265 | |
266 if (ret_len != NULL) | |
267 *ret_len = len; | |
268 | |
269 return out; | |
270 } | |
271 | |
272 /************************************************************************** | |
273 * Quoted Printable Functions (see RFC 2045). | |
274 **************************************************************************/ | |
275 guchar * | |
276 gaim_quotedp_decode(const char *str, gsize *ret_len) | |
277 { | |
278 char *n, *new; | |
279 const char *end, *p; | |
280 | |
281 n = new = g_malloc(strlen (str) + 1); | |
282 end = str + strlen(str); | |
283 | |
284 for (p = str; p < end; p++, n++) { | |
285 if (*p == '=') { | |
286 if (p[1] == '\r' && p[2] == '\n') { /* 5.1 #5 */ | |
287 n -= 1; | |
288 p += 2; | |
289 } else if (p[1] == '\n') { /* fuzzy case for 5.1 #5 */ | |
290 n -= 1; | |
291 p += 1; | |
292 } else if (p[1] && p[2]) { | |
293 char *nibble1 = strchr(xdigits, tolower(p[1])); | |
294 char *nibble2 = strchr(xdigits, tolower(p[2])); | |
295 if (nibble1 && nibble2) { /* 5.1 #1 */ | |
296 *n = ((nibble1 - xdigits) << 4) | (nibble2 - xdigits); | |
297 p += 2; | |
298 } else { /* This should never happen */ | |
299 *n = *p; | |
300 } | |
301 } else { /* This should never happen */ | |
302 *n = *p; | |
303 } | |
304 } | |
305 else if (*p == '_') | |
306 *n = ' '; | |
307 else | |
308 *n = *p; | |
309 } | |
310 | |
311 *n = '\0'; | |
312 | |
313 if (ret_len != NULL) | |
314 *ret_len = n - new; | |
315 | |
316 /* Resize to take less space */ | |
317 /* new = realloc(new, n - new); */ | |
318 | |
319 return (guchar *)new; | |
320 } | |
321 | |
322 /************************************************************************** | |
323 * MIME Functions | |
324 **************************************************************************/ | |
325 char * | |
326 gaim_mime_decode_field(const char *str) | |
327 { | |
328 /* | |
329 * This is wing's version, partially based on revo/shx's version | |
330 * See RFC2047 [which apparently obsoletes RFC1342] | |
331 */ | |
332 typedef enum { | |
333 state_start, state_equal1, state_question1, | |
334 state_charset, state_question2, | |
335 state_encoding, state_question3, | |
336 state_encoded_text, state_question4, state_equal2 = state_start | |
337 } encoded_word_state_t; | |
338 encoded_word_state_t state = state_start; | |
339 const char *cur, *mark; | |
340 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL; | |
341 char *n, *new; | |
342 | |
343 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */ | |
344 #define token_char_p(c) \ | |
345 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c)) | |
346 | |
347 /* But encoded-text must be ASCII; alas, isascii() may not exist */ | |
348 #define encoded_text_char_p(c) \ | |
349 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c)) | |
350 | |
351 #define RECOVER_MARKED_TEXT strncpy(n, mark, cur - mark + 1); \ | |
352 n += cur - mark + 1 | |
353 | |
354 g_return_val_if_fail(str != NULL, NULL); | |
355 | |
356 /* NOTE: Assuming that we need just strlen(str)+1 *may* be wrong. | |
357 * It would be wrong if one byte (in some unknown encoding) could | |
358 * expand to >=4 bytes of UTF-8; I don't know if there are such things. | |
359 */ | |
360 n = new = g_malloc(strlen(str) + 1); | |
361 | |
362 /* Here we will be looking for encoded words and if they seem to be | |
363 * valid then decode them. | |
364 * They are of this form: =?charset?encoding?text?= | |
365 */ | |
366 | |
367 for (cur = str, mark = NULL; *cur; cur += 1) { | |
368 switch (state) { | |
369 case state_equal1: | |
370 if (*cur == '?') { | |
371 state = state_question1; | |
372 } else { | |
373 RECOVER_MARKED_TEXT; | |
374 state = state_start; | |
375 } | |
376 break; | |
377 case state_question1: | |
378 if (token_char_p(*cur)) { | |
379 charset0 = cur; | |
380 state = state_charset; | |
381 } else { /* This should never happen */ | |
382 RECOVER_MARKED_TEXT; | |
383 state = state_start; | |
384 } | |
385 break; | |
386 case state_charset: | |
387 if (*cur == '?') { | |
388 state = state_question2; | |
389 } else if (!token_char_p(*cur)) { /* This should never happen */ | |
390 RECOVER_MARKED_TEXT; | |
391 state = state_start; | |
392 } | |
393 break; | |
394 case state_question2: | |
395 if (token_char_p(*cur)) { | |
396 encoding0 = cur; | |
397 state = state_encoding; | |
398 } else { /* This should never happen */ | |
399 RECOVER_MARKED_TEXT; | |
400 state = state_start; | |
401 } | |
402 break; | |
403 case state_encoding: | |
404 if (*cur == '?') { | |
405 state = state_question3; | |
406 } else if (!token_char_p(*cur)) { /* This should never happen */ | |
407 RECOVER_MARKED_TEXT; | |
408 state = state_start; | |
409 } | |
410 break; | |
411 case state_question3: | |
412 if (encoded_text_char_p(*cur)) { | |
413 encoded_text0 = cur; | |
414 state = state_encoded_text; | |
415 } else if (*cur == '?') { /* empty string */ | |
416 encoded_text0 = cur; | |
417 state = state_question4; | |
418 } else { /* This should never happen */ | |
419 RECOVER_MARKED_TEXT; | |
420 state = state_start; | |
421 } | |
422 break; | |
423 case state_encoded_text: | |
424 if (*cur == '?') { | |
425 state = state_question4; | |
426 } else if (!encoded_text_char_p(*cur)) { | |
427 RECOVER_MARKED_TEXT; | |
428 state = state_start; | |
429 } | |
430 break; | |
431 case state_question4: | |
432 if (*cur == '=') { /* Got the whole encoded-word */ | |
433 char *charset = g_strndup(charset0, encoding0 - charset0 - 1); | |
434 char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1); | |
435 char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1); | |
436 guchar *decoded = NULL; | |
437 gsize dec_len; | |
438 if (g_ascii_strcasecmp(encoding, "Q") == 0) | |
439 decoded = gaim_quotedp_decode(encoded_text, &dec_len); | |
440 else if (g_ascii_strcasecmp(encoding, "B") == 0) | |
441 decoded = gaim_base64_decode(encoded_text, &dec_len); | |
442 else | |
443 decoded = NULL; | |
444 if (decoded) { | |
445 gsize len; | |
446 char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL); | |
447 | |
448 if (converted) { | |
449 n = strncpy(n, converted, len) + len; | |
450 g_free(converted); | |
451 } | |
452 g_free(decoded); | |
453 } | |
454 g_free(charset); | |
455 g_free(encoding); | |
456 g_free(encoded_text); | |
457 state = state_equal2; /* Restart the FSM */ | |
458 } else { /* This should never happen */ | |
459 RECOVER_MARKED_TEXT; | |
460 state = state_start; | |
461 } | |
462 break; | |
463 default: | |
464 if (*cur == '=') { | |
465 mark = cur; | |
466 state = state_equal1; | |
467 } else { | |
468 /* Some unencoded text. */ | |
469 *n = *cur; | |
470 n += 1; | |
471 } | |
472 break; | |
473 } /* switch */ | |
474 } /* for */ | |
475 | |
476 if (state != state_start) { | |
477 RECOVER_MARKED_TEXT; | |
478 } | |
479 *n = '\0'; | |
480 | |
481 return new; | |
482 } | |
483 | |
484 | |
485 /************************************************************************** | |
486 * Date/Time Functions | |
487 **************************************************************************/ | |
488 | |
489 #ifdef _WIN32 | |
490 static long win32_get_tz_offset() { | |
491 TIME_ZONE_INFORMATION tzi; | |
492 DWORD ret; | |
493 long off = -1; | |
494 | |
495 if ((ret = GetTimeZoneInformation(&tzi)) != TIME_ZONE_ID_INVALID) | |
496 { | |
497 off = -(tzi.Bias * 60); | |
498 if (ret == TIME_ZONE_ID_DAYLIGHT) | |
499 off -= tzi.DaylightBias * 60; | |
500 } | |
501 | |
502 return off; | |
503 } | |
504 #endif | |
505 | |
506 #ifndef HAVE_STRFTIME_Z_FORMAT | |
507 static const char *get_tmoff(const struct tm *tm) | |
508 { | |
509 static char buf[6]; | |
510 long off; | |
511 gint8 min; | |
512 gint8 hrs; | |
513 struct tm new_tm = *tm; | |
514 | |
515 mktime(&new_tm); | |
516 | |
517 if (new_tm.tm_isdst < 0) | |
518 g_return_val_if_reached(""); | |
519 | |
520 #ifdef _WIN32 | |
521 if ((off = win32_get_tz_offset()) == -1) | |
522 return ""; | |
523 #else | |
524 # ifdef HAVE_TM_GMTOFF | |
525 off = new_tm.tm_gmtoff; | |
526 # else | |
527 # ifdef HAVE_TIMEZONE | |
528 tzset(); | |
529 off = -timezone; | |
530 # endif /* HAVE_TIMEZONE */ | |
531 # endif /* !HAVE_TM_GMTOFF */ | |
532 #endif /* _WIN32 */ | |
533 | |
534 min = (off / 60) % 60; | |
535 hrs = ((off / 60) - min) / 60; | |
536 | |
537 if (g_snprintf(buf, sizeof(buf), "%+03d%02d", hrs, ABS(min)) > 5) | |
538 g_return_val_if_reached(""); | |
539 | |
540 return buf; | |
541 } | |
542 #endif | |
543 | |
544 /* Windows doesn't HAVE_STRFTIME_Z_FORMAT, but this seems clearer. -- rlaager */ | |
545 #if !defined(HAVE_STRFTIME_Z_FORMAT) || defined(_WIN32) | |
546 static size_t gaim_internal_strftime(char *s, size_t max, const char *format, const struct tm *tm) | |
547 { | |
548 const char *start; | |
549 const char *c; | |
550 char *fmt = NULL; | |
551 | |
552 /* Yes, this is checked in gaim_utf8_strftime(), | |
553 * but better safe than sorry. -- rlaager */ | |
554 g_return_val_if_fail(format != NULL, 0); | |
555 | |
556 /* This is fairly efficient, and it only gets | |
557 * executed on Windows or if the underlying | |
558 * system doesn't support the %z format string, | |
559 * for strftime() so I think it's good enough. | |
560 * -- rlaager */ | |
561 for (c = start = format; *c ; c++) | |
562 { | |
563 if (*c != '%') | |
564 continue; | |
565 | |
566 c++; | |
567 | |
568 #ifndef HAVE_STRFTIME_Z_FORMAT | |
569 if (*c == 'z') | |
570 { | |
571 char *tmp = g_strdup_printf("%s%.*s%s", | |
572 fmt ? fmt : "", | |
573 c - start - 1, | |
574 start, | |
575 get_tmoff(tm)); | |
576 g_free(fmt); | |
577 fmt = tmp; | |
578 start = c + 1; | |
579 } | |
580 #endif | |
581 #ifdef _WIN32 | |
582 if (*c == 'Z') | |
583 { | |
584 char *tmp = g_strdup_printf("%s%.*s%s", | |
585 fmt ? fmt : "", | |
586 c - start - 1, | |
587 start, | |
588 wgaim_get_timezone_abbreviation(tm)); | |
589 g_free(fmt); | |
590 fmt = tmp; | |
591 start = c + 1; | |
592 } | |
593 #endif | |
594 } | |
595 | |
596 if (fmt != NULL) | |
597 { | |
598 size_t ret; | |
599 | |
600 if (*start) | |
601 { | |
602 char *tmp = g_strconcat(fmt, start, NULL); | |
603 g_free(fmt); | |
604 fmt = tmp; | |
605 } | |
606 | |
607 ret = strftime(s, max, fmt, tm); | |
608 g_free(fmt); | |
609 | |
610 return ret; | |
611 } | |
612 | |
613 return strftime(s, max, format, tm); | |
614 } | |
615 #else /* HAVE_STRFTIME_Z_FORMAT && !_WIN32 */ | |
616 #define gaim_internal_strftime strftime | |
617 #endif | |
618 | |
619 const char * | |
620 gaim_utf8_strftime(const char *format, const struct tm *tm) | |
621 { | |
622 static char buf[128]; | |
623 char *locale; | |
624 GError *err = NULL; | |
625 int len; | |
626 char *utf8; | |
627 | |
628 g_return_val_if_fail(format != NULL, NULL); | |
629 | |
630 if (tm == NULL) | |
631 { | |
632 time_t now = time(NULL); | |
633 tm = localtime(&now); | |
634 } | |
635 | |
636 locale = g_locale_from_utf8(format, -1, NULL, NULL, &err); | |
637 if (err != NULL) | |
638 { | |
639 gaim_debug_error("util", "Format conversion failed in gaim_utf8_strftime(): %s", err->message); | |
640 g_error_free(err); | |
641 locale = g_strdup(format); | |
642 } | |
643 | |
644 /* A return value of 0 is either an error (in | |
645 * which case, the contents of the buffer are | |
646 * undefined) or the empty string (in which | |
647 * case, no harm is done here). */ | |
648 if ((len = gaim_internal_strftime(buf, sizeof(buf), locale, tm)) == 0) | |
649 { | |
650 g_free(locale); | |
651 return ""; | |
652 } | |
653 | |
654 g_free(locale); | |
655 | |
656 utf8 = g_locale_to_utf8(buf, len, NULL, NULL, &err); | |
657 if (err != NULL) | |
658 { | |
659 gaim_debug_error("util", "Result conversion failed in gaim_utf8_strftime(): %s", err->message); | |
660 g_error_free(err); | |
661 } | |
662 else | |
663 { | |
664 gaim_strlcpy(buf, utf8); | |
665 g_free(utf8); | |
666 } | |
667 | |
668 return buf; | |
669 } | |
670 | |
671 const char * | |
672 gaim_date_format_short(const struct tm *tm) | |
673 { | |
674 return gaim_utf8_strftime("%x", tm); | |
675 } | |
676 | |
677 const char * | |
678 gaim_date_format_long(const struct tm *tm) | |
679 { | |
680 return gaim_utf8_strftime(_("%x %X"), tm); | |
681 } | |
682 | |
683 const char * | |
684 gaim_date_format_full(const struct tm *tm) | |
685 { | |
686 return gaim_utf8_strftime("%c", tm); | |
687 } | |
688 | |
689 const char * | |
690 gaim_time_format(const struct tm *tm) | |
691 { | |
692 return gaim_utf8_strftime("%X", tm); | |
693 } | |
694 | |
695 time_t | |
696 gaim_time_build(int year, int month, int day, int hour, int min, int sec) | |
697 { | |
698 struct tm tm; | |
699 | |
700 tm.tm_year = year - 1900; | |
701 tm.tm_mon = month - 1; | |
702 tm.tm_mday = day; | |
703 tm.tm_hour = hour; | |
704 tm.tm_min = min; | |
705 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; | |
706 | |
707 return mktime(&tm); | |
708 } | |
709 | |
710 time_t | |
711 gaim_str_to_time(const char *timestamp, gboolean utc, | |
712 struct tm *tm, long *tz_off, const char **rest) | |
713 { | |
714 time_t retval = 0; | |
715 struct tm *t; | |
716 const char *c = timestamp; | |
717 int year = 0; | |
718 long tzoff = GAIM_NO_TZ_OFF; | |
719 | |
720 time(&retval); | |
721 t = localtime(&retval); | |
722 | |
723 /* 4 digit year */ | |
724 if (sscanf(c, "%04d", &year) && year > 1900) | |
725 { | |
726 c += 4; | |
727 if (*c == '-') | |
728 c++; | |
729 t->tm_year = year - 1900; | |
730 } | |
731 | |
732 /* 2 digit month */ | |
733 if (!sscanf(c, "%02d", &t->tm_mon)) | |
734 { | |
735 if (rest != NULL && *c != '\0') | |
736 *rest = c; | |
737 return 0; | |
738 } | |
739 c += 2; | |
740 if (*c == '-' || *c == '/') | |
741 c++; | |
742 t->tm_mon -= 1; | |
743 | |
744 /* 2 digit day */ | |
745 if (!sscanf(c, "%02d", &t->tm_mday)) | |
746 { | |
747 if (rest != NULL && *c != '\0') | |
748 *rest = c; | |
749 return 0; | |
750 } | |
751 c += 2; | |
752 if (*c == '/') | |
753 { | |
754 c++; | |
755 | |
756 if (!sscanf(c, "%04d", &t->tm_year)) | |
757 { | |
758 if (rest != NULL && *c != '\0') | |
759 *rest = c; | |
760 return 0; | |
761 } | |
762 t->tm_year -= 1900; | |
763 } | |
764 else if (*c == 'T' || *c == '.') | |
765 { | |
766 c++; | |
767 /* we have more than a date, keep going */ | |
768 | |
769 /* 2 digit hour */ | |
770 if ((sscanf(c, "%02d:%02d:%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 8)) || | |
771 (sscanf(c, "%02d%02d%02d", &t->tm_hour, &t->tm_min, &t->tm_sec) == 3 && (c = c + 6))) | |
772 { | |
773 gboolean offset_positive = FALSE; | |
774 int tzhrs; | |
775 int tzmins; | |
776 | |
777 t->tm_isdst = -1; | |
778 | |
779 if (*c == '.' && *(c+1) >= '0' && *(c+1) <= '9') /* dealing with precision we don't care about */ | |
780 c += 4; | |
781 if (*c == '+') | |
782 offset_positive = TRUE; | |
783 if (((*c == '+' || *c == '-') && (c = c + 1)) && | |
784 ((sscanf(c, "%02d:%02d", &tzhrs, &tzmins) == 2 && (c = c + 5)) || | |
785 (sscanf(c, "%02d%02d", &tzhrs, &tzmins) == 2 && (c = c + 4)))) | |
786 { | |
787 tzoff = tzhrs*60*60 + tzmins*60; | |
788 if (offset_positive) | |
789 tzoff *= -1; | |
790 /* We don't want the C library doing DST calculations | |
791 * if we know the UTC offset already. */ | |
792 t->tm_isdst = 0; | |
793 } | |
14998
fee45b614eb7
[gaim-migrate @ 17776]
Richard Laager <rlaager@wiktel.com>
parents:
14960
diff
changeset
|
794 else if (utc) |
fee45b614eb7
[gaim-migrate @ 17776]
Richard Laager <rlaager@wiktel.com>
parents:
14960
diff
changeset
|
795 { |
fee45b614eb7
[gaim-migrate @ 17776]
Richard Laager <rlaager@wiktel.com>
parents:
14960
diff
changeset
|
796 t->tm_isdst = 0; |
fee45b614eb7
[gaim-migrate @ 17776]
Richard Laager <rlaager@wiktel.com>
parents:
14960
diff
changeset
|
797 } |
14192 | 798 |
799 if (rest != NULL && *c != '\0') | |
800 { | |
801 if (*c == ' ') | |
802 c++; | |
803 if (*c != '\0') | |
804 *rest = c; | |
805 } | |
806 | |
807 if (tzoff != GAIM_NO_TZ_OFF || utc) | |
808 { | |
809 #if defined(_WIN32) | |
810 long sys_tzoff; | |
811 #endif | |
812 | |
813 #if defined(_WIN32) || defined(HAVE_TM_GMTOFF) || defined (HAVE_TIMEZONE) | |
814 if (tzoff == GAIM_NO_TZ_OFF) | |
815 tzoff = 0; | |
816 #endif | |
817 | |
818 #ifdef _WIN32 | |
819 if ((sys_tzoff = win32_get_tz_offset()) == -1) | |
820 tzoff = GAIM_NO_TZ_OFF; | |
821 else | |
822 tzoff += sys_tzoff; | |
823 #else | |
824 #ifdef HAVE_TM_GMTOFF | |
825 tzoff += t->tm_gmtoff; | |
826 #else | |
827 # ifdef HAVE_TIMEZONE | |
828 tzset(); /* making sure */ | |
829 tzoff -= timezone; | |
830 # endif | |
831 #endif | |
832 #endif /* _WIN32 */ | |
833 } | |
834 } | |
835 else | |
836 { | |
837 if (rest != NULL && *c != '\0') | |
838 *rest = c; | |
839 } | |
840 } | |
841 | |
842 if (tm != NULL) | |
843 { | |
844 *tm = *t; | |
845 tm->tm_isdst = -1; | |
846 mktime(tm); | |
847 } | |
848 | |
849 retval = mktime(t); | |
850 if (tzoff != GAIM_NO_TZ_OFF) | |
851 retval += tzoff; | |
852 | |
853 if (tz_off != NULL) | |
854 *tz_off = tzoff; | |
855 | |
856 return retval; | |
857 } | |
858 | |
859 /************************************************************************** | |
860 * Markup Functions | |
861 **************************************************************************/ | |
862 | |
863 /* Returns a NULL-terminated string after unescaping an entity | |
864 * (eg. &, < & etc.) starting at s. Returns NULL on failure.*/ | |
865 static const char * | |
866 detect_entity(const char *text, int *length) | |
867 { | |
868 const char *pln; | |
869 int len, pound; | |
870 | |
871 if (!text || *text != '&') | |
872 return NULL; | |
873 | |
874 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1))) | |
875 | |
876 if(IS_ENTITY("&")) | |
877 pln = "&"; | |
878 else if(IS_ENTITY("<")) | |
879 pln = "<"; | |
880 else if(IS_ENTITY(">")) | |
881 pln = ">"; | |
882 else if(IS_ENTITY(" ")) | |
883 pln = " "; | |
884 else if(IS_ENTITY("©")) | |
885 pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */ | |
886 else if(IS_ENTITY(""")) | |
887 pln = "\""; | |
888 else if(IS_ENTITY("®")) | |
889 pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */ | |
890 else if(IS_ENTITY("'")) | |
891 pln = "\'"; | |
892 else if(*(text+1) == '#' && (sscanf(text, "&#%u;", £) == 1) && | |
893 pound != 0 && *(text+3+(gint)log10(pound)) == ';') { | |
894 static char buf[7]; | |
895 int buflen = g_unichar_to_utf8((gunichar)pound, buf); | |
896 buf[buflen] = '\0'; | |
897 pln = buf; | |
898 | |
899 len = 2; | |
900 while(isdigit((gint) text[len])) len++; | |
901 if(text[len] == ';') len++; | |
902 } | |
903 else | |
904 return NULL; | |
905 | |
906 if (length) | |
907 *length = len; | |
908 return pln; | |
909 } | |
910 | |
911 gboolean | |
912 gaim_markup_find_tag(const char *needle, const char *haystack, | |
913 const char **start, const char **end, GData **attributes) | |
914 { | |
915 GData *attribs; | |
916 const char *cur = haystack; | |
917 char *name = NULL; | |
918 gboolean found = FALSE; | |
919 gboolean in_tag = FALSE; | |
920 gboolean in_attr = FALSE; | |
921 const char *in_quotes = NULL; | |
922 size_t needlelen; | |
923 | |
924 g_return_val_if_fail( needle != NULL, FALSE); | |
925 g_return_val_if_fail( *needle != '\0', FALSE); | |
926 g_return_val_if_fail( haystack != NULL, FALSE); | |
927 g_return_val_if_fail( *haystack != '\0', FALSE); | |
928 g_return_val_if_fail( start != NULL, FALSE); | |
929 g_return_val_if_fail( end != NULL, FALSE); | |
930 g_return_val_if_fail(attributes != NULL, FALSE); | |
931 | |
932 needlelen = strlen(needle); | |
933 g_datalist_init(&attribs); | |
934 | |
935 while (*cur && !found) { | |
936 if (in_tag) { | |
937 if (in_quotes) { | |
938 const char *close = cur; | |
939 | |
940 while (*close && *close != *in_quotes) | |
941 close++; | |
942 | |
943 /* if we got the close quote, store the value and carry on from * | |
944 * after it. if we ran to the end of the string, point to the NULL * | |
945 * and we're outta here */ | |
946 if (*close) { | |
947 /* only store a value if we have an attribute name */ | |
948 if (name) { | |
949 size_t len = close - cur; | |
950 char *val = g_strndup(cur, len); | |
951 | |
952 g_datalist_set_data_full(&attribs, name, val, g_free); | |
953 g_free(name); | |
954 name = NULL; | |
955 } | |
956 | |
957 in_quotes = NULL; | |
958 cur = close + 1; | |
959 } else { | |
960 cur = close; | |
961 } | |
962 } else if (in_attr) { | |
963 const char *close = cur; | |
964 | |
965 while (*close && *close != '>' && *close != '"' && | |
966 *close != '\'' && *close != ' ' && *close != '=') | |
967 close++; | |
968 | |
969 /* if we got the equals, store the name of the attribute. if we got | |
970 * the quote, save the attribute and go straight to quote mode. | |
971 * otherwise the tag closed or we reached the end of the string, | |
972 * so we can get outta here */ | |
973 switch (*close) { | |
974 case '"': | |
975 case '\'': | |
976 in_quotes = close; | |
977 case '=': | |
978 { | |
979 size_t len = close - cur; | |
980 | |
981 /* don't store a blank attribute name */ | |
982 if (len) { | |
983 g_free(name); | |
984 name = g_ascii_strdown(cur, len); | |
985 } | |
986 | |
987 in_attr = FALSE; | |
988 cur = close + 1; | |
989 break; | |
990 } | |
991 case ' ': | |
992 case '>': | |
993 in_attr = FALSE; | |
994 default: | |
995 cur = close; | |
996 break; | |
997 } | |
998 } else { | |
999 switch (*cur) { | |
1000 case ' ': | |
1001 /* swallow extra spaces inside tag */ | |
1002 while (*cur && *cur == ' ') cur++; | |
1003 in_attr = TRUE; | |
1004 break; | |
1005 case '>': | |
1006 found = TRUE; | |
1007 *end = cur; | |
1008 break; | |
1009 case '"': | |
1010 case '\'': | |
1011 in_quotes = cur; | |
1012 default: | |
1013 cur++; | |
1014 break; | |
1015 } | |
1016 } | |
1017 } else { | |
1018 /* if we hit a < followed by the name of our tag... */ | |
1019 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) { | |
1020 *start = cur; | |
1021 cur = cur + needlelen + 1; | |
1022 | |
1023 /* if we're pointing at a space or a >, we found the right tag. if * | |
1024 * we're not, we've found a longer tag, so we need to skip to the * | |
1025 * >, but not being distracted by >s inside quotes. */ | |
1026 if (*cur == ' ' || *cur == '>') { | |
1027 in_tag = TRUE; | |
1028 } else { | |
1029 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') { | |
1030 if (*cur == '"') { | |
1031 cur++; | |
1032 while (*cur && *cur != '"') | |
1033 cur++; | |
1034 } else if (*cur == '\'') { | |
1035 cur++; | |
1036 while (*cur && *cur != '\'') | |
1037 cur++; | |
1038 } else { | |
1039 cur++; | |
1040 } | |
1041 } | |
1042 } | |
1043 } else { | |
1044 cur++; | |
1045 } | |
1046 } | |
1047 } | |
1048 | |
1049 /* clean up any attribute name from a premature termination */ | |
1050 g_free(name); | |
1051 | |
1052 if (found) { | |
1053 *attributes = attribs; | |
1054 } else { | |
1055 *start = NULL; | |
1056 *end = NULL; | |
1057 *attributes = NULL; | |
1058 } | |
1059 | |
1060 return found; | |
1061 } | |
1062 | |
1063 gboolean | |
1064 gaim_markup_extract_info_field(const char *str, int len, GString *dest, | |
1065 const char *start_token, int skip, | |
1066 const char *end_token, char check_value, | |
1067 const char *no_value_token, | |
1068 const char *display_name, gboolean is_link, | |
1069 const char *link_prefix, | |
1070 GaimInfoFieldFormatCallback format_cb) | |
1071 { | |
1072 const char *p, *q; | |
1073 | |
1074 g_return_val_if_fail(str != NULL, FALSE); | |
1075 g_return_val_if_fail(dest != NULL, FALSE); | |
1076 g_return_val_if_fail(start_token != NULL, FALSE); | |
1077 g_return_val_if_fail(end_token != NULL, FALSE); | |
1078 g_return_val_if_fail(display_name != NULL, FALSE); | |
1079 | |
1080 p = strstr(str, start_token); | |
1081 | |
1082 if (p == NULL) | |
1083 return FALSE; | |
1084 | |
1085 p += strlen(start_token) + skip; | |
1086 | |
1087 if (p >= str + len) | |
1088 return FALSE; | |
1089 | |
1090 if (check_value != '\0' && *p == check_value) | |
1091 return FALSE; | |
1092 | |
1093 q = strstr(p, end_token); | |
1094 | |
1095 /* Trim leading blanks */ | |
1096 while (*p != '\n' && g_ascii_isspace(*p)) { | |
1097 p += 1; | |
1098 } | |
1099 | |
1100 /* Trim trailing blanks */ | |
1101 while (q > p && g_ascii_isspace(*(q - 1))) { | |
1102 q -= 1; | |
1103 } | |
1104 | |
1105 /* Don't bother with null strings */ | |
1106 if (p == q) | |
1107 return FALSE; | |
1108 | |
1109 if (q != NULL && (!no_value_token || | |
1110 (no_value_token && strncmp(p, no_value_token, | |
1111 strlen(no_value_token))))) | |
1112 { | |
1113 g_string_append_printf(dest, _("<b>%s:</b> "), display_name); | |
1114 | |
1115 if (is_link) | |
1116 { | |
1117 g_string_append(dest, "<br><a href=\""); | |
1118 | |
1119 if (link_prefix) | |
1120 g_string_append(dest, link_prefix); | |
1121 | |
1122 if (format_cb != NULL) | |
1123 { | |
1124 char *reformatted = format_cb(p, q - p); | |
1125 g_string_append(dest, reformatted); | |
1126 g_free(reformatted); | |
1127 } | |
1128 else | |
1129 g_string_append_len(dest, p, q - p); | |
1130 g_string_append(dest, "\">"); | |
1131 | |
1132 if (link_prefix) | |
1133 g_string_append(dest, link_prefix); | |
1134 | |
1135 g_string_append_len(dest, p, q - p); | |
1136 g_string_append(dest, "</a>"); | |
1137 } | |
1138 else | |
1139 { | |
1140 if (format_cb != NULL) | |
1141 { | |
1142 char *reformatted = format_cb(p, q - p); | |
1143 g_string_append(dest, reformatted); | |
1144 g_free(reformatted); | |
1145 } | |
1146 else | |
1147 g_string_append_len(dest, p, q - p); | |
1148 } | |
1149 | |
1150 g_string_append(dest, "<br>\n"); | |
1151 | |
1152 return TRUE; | |
1153 } | |
1154 | |
1155 return FALSE; | |
1156 } | |
1157 | |
1158 struct gaim_parse_tag { | |
1159 char *src_tag; | |
1160 char *dest_tag; | |
1161 gboolean ignore; | |
1162 }; | |
1163 | |
1164 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \ | |
1165 const char *o = c + strlen("<" x); \ | |
1166 const char *p = NULL, *q = NULL, *r = NULL; \ | |
1167 GString *innards = g_string_new(""); \ | |
1168 while(o && *o) { \ | |
1169 if(!q && (*o == '\"' || *o == '\'') ) { \ | |
1170 q = o; \ | |
1171 } else if(q) { \ | |
1172 if(*o == *q) { \ | |
1173 char *unescaped = g_strndup(q+1, o-q-1); \ | |
1174 char *escaped = g_markup_escape_text(unescaped, -1); \ | |
1175 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \ | |
1176 g_free(unescaped); \ | |
1177 g_free(escaped); \ | |
1178 q = NULL; \ | |
1179 } else if(*c == '\\') { \ | |
1180 o++; \ | |
1181 } \ | |
1182 } else if(*o == '<') { \ | |
1183 r = o; \ | |
1184 } else if(*o == '>') { \ | |
1185 p = o; \ | |
1186 break; \ | |
1187 } else { \ | |
1188 innards = g_string_append_c(innards, *o); \ | |
1189 } \ | |
1190 o++; \ | |
1191 } \ | |
1192 if(p && !r) { \ | |
1193 if(*(p-1) != '/') { \ | |
1194 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ | |
1195 pt->src_tag = x; \ | |
1196 pt->dest_tag = y; \ | |
1197 tags = g_list_prepend(tags, pt); \ | |
1198 } \ | |
1199 xhtml = g_string_append(xhtml, "<" y); \ | |
1200 c += strlen("<" x ); \ | |
1201 xhtml = g_string_append(xhtml, innards->str); \ | |
1202 xhtml = g_string_append_c(xhtml, '>'); \ | |
1203 c = p + 1; \ | |
1204 } else { \ | |
1205 xhtml = g_string_append(xhtml, "<"); \ | |
1206 plain = g_string_append_c(plain, '<'); \ | |
1207 c++; \ | |
1208 } \ | |
1209 g_string_free(innards, TRUE); \ | |
1210 continue; \ | |
1211 } \ | |
1212 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \ | |
1213 (*(c+strlen("<" x)) == '>' || \ | |
1214 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \ | |
1215 xhtml = g_string_append(xhtml, "<" y); \ | |
1216 c += strlen("<" x); \ | |
1217 if(*c != '/') { \ | |
1218 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); \ | |
1219 pt->src_tag = x; \ | |
1220 pt->dest_tag = y; \ | |
1221 tags = g_list_prepend(tags, pt); \ | |
1222 xhtml = g_string_append_c(xhtml, '>'); \ | |
1223 } else { \ | |
1224 xhtml = g_string_append(xhtml, "/>");\ | |
1225 } \ | |
1226 c = strchr(c, '>') + 1; \ | |
1227 continue; \ | |
1228 } | |
1229 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x) | |
1230 void | |
1231 gaim_markup_html_to_xhtml(const char *html, char **xhtml_out, | |
1232 char **plain_out) | |
1233 { | |
1234 GString *xhtml = g_string_new(""); | |
1235 GString *plain = g_string_new(""); | |
1236 GList *tags = NULL, *tag; | |
1237 const char *c = html; | |
1238 | |
1239 while(c && *c) { | |
1240 if(*c == '<') { | |
1241 if(*(c+1) == '/') { /* closing tag */ | |
1242 tag = tags; | |
1243 while(tag) { | |
1244 struct gaim_parse_tag *pt = tag->data; | |
1245 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') { | |
1246 c += strlen(pt->src_tag) + 3; | |
1247 break; | |
1248 } | |
1249 tag = tag->next; | |
1250 } | |
1251 if(tag) { | |
1252 while(tags) { | |
1253 struct gaim_parse_tag *pt = tags->data; | |
1254 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); | |
1255 if(tags == tag) | |
1256 break; | |
1257 tags = g_list_remove(tags, pt); | |
1258 g_free(pt); | |
1259 } | |
1260 g_free(tag->data); | |
1261 tags = g_list_remove(tags, tag->data); | |
1262 } else { | |
1263 /* a closing tag we weren't expecting... | |
1264 * we'll let it slide, if it's really a tag...if it's | |
1265 * just a </ we'll escape it properly */ | |
1266 const char *end = c+2; | |
1267 while(*end && g_ascii_isalpha(*end)) | |
1268 end++; | |
1269 if(*end == '>') { | |
1270 c = end+1; | |
1271 } else { | |
1272 xhtml = g_string_append(xhtml, "<"); | |
1273 plain = g_string_append_c(plain, '<'); | |
1274 c++; | |
1275 } | |
1276 } | |
1277 } else { /* opening tag */ | |
1278 ALLOW_TAG("a"); | |
1279 ALLOW_TAG("blockquote"); | |
1280 ALLOW_TAG("cite"); | |
1281 ALLOW_TAG("div"); | |
1282 ALLOW_TAG("em"); | |
1283 ALLOW_TAG("h1"); | |
1284 ALLOW_TAG("h2"); | |
1285 ALLOW_TAG("h3"); | |
1286 ALLOW_TAG("h4"); | |
1287 ALLOW_TAG("h5"); | |
1288 ALLOW_TAG("h6"); | |
1289 /* we only allow html to start the message */ | |
1290 if(c == html) | |
1291 ALLOW_TAG("html"); | |
1292 ALLOW_TAG_ALT("i", "em"); | |
1293 ALLOW_TAG_ALT("italic", "em"); | |
1294 ALLOW_TAG("li"); | |
1295 ALLOW_TAG("ol"); | |
1296 ALLOW_TAG("p"); | |
1297 ALLOW_TAG("pre"); | |
1298 ALLOW_TAG("q"); | |
1299 ALLOW_TAG("span"); | |
1300 ALLOW_TAG("strong"); | |
1301 ALLOW_TAG("ul"); | |
1302 | |
1303 /* we skip <HR> because it's not legal in XHTML-IM. However, | |
1304 * we still want to send something sensible, so we put a | |
1305 * linebreak in its place. <BR> also needs special handling | |
1306 * because putting a </BR> to close it would just be dumb. */ | |
1307 if((!g_ascii_strncasecmp(c, "<br", 3) | |
1308 || !g_ascii_strncasecmp(c, "<hr", 3)) | |
1309 && (*(c+3) == '>' || | |
1310 !g_ascii_strncasecmp(c+3, "/>", 2) || | |
1311 !g_ascii_strncasecmp(c+3, " />", 3))) { | |
1312 c = strchr(c, '>') + 1; | |
1313 xhtml = g_string_append(xhtml, "<br/>"); | |
1314 if(*c != '\n') | |
1315 plain = g_string_append_c(plain, '\n'); | |
1316 continue; | |
1317 } | |
14333 | 1318 if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>"))) { |
1319 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1320 pt->src_tag = *(c+2) == '>' ? "b" : "bold"; | |
1321 pt->dest_tag = "span"; | |
1322 tags = g_list_prepend(tags, pt); | |
1323 c = strchr(c, '>') + 1; | |
1324 xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>"); | |
1325 continue; | |
1326 } | |
14192 | 1327 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) { |
1328 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1329 pt->src_tag = *(c+2) == '>' ? "u" : "underline"; | |
1330 pt->dest_tag = "span"; | |
1331 tags = g_list_prepend(tags, pt); | |
1332 c = strchr(c, '>') + 1; | |
1333 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); | |
1334 continue; | |
1335 } | |
1336 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) { | |
1337 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1338 pt->src_tag = *(c+2) == '>' ? "s" : "strike"; | |
1339 pt->dest_tag = "span"; | |
1340 tags = g_list_prepend(tags, pt); | |
1341 c = strchr(c, '>') + 1; | |
1342 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); | |
1343 continue; | |
1344 } | |
1345 if(!g_ascii_strncasecmp(c, "<sub>", 5)) { | |
1346 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1347 pt->src_tag = "sub"; | |
1348 pt->dest_tag = "span"; | |
1349 tags = g_list_prepend(tags, pt); | |
1350 c = strchr(c, '>') + 1; | |
1351 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); | |
1352 continue; | |
1353 } | |
1354 if(!g_ascii_strncasecmp(c, "<sup>", 5)) { | |
1355 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1356 pt->src_tag = "sup"; | |
1357 pt->dest_tag = "span"; | |
1358 tags = g_list_prepend(tags, pt); | |
1359 c = strchr(c, '>') + 1; | |
1360 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); | |
1361 continue; | |
1362 } | |
1363 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { | |
1364 const char *p = c; | |
1365 GString *style = g_string_new(""); | |
1366 struct gaim_parse_tag *pt; | |
1367 while(*p && *p != '>') { | |
1368 if(!g_ascii_strncasecmp(p, "back=", strlen("back="))) { | |
1369 const char *q = p + strlen("back="); | |
1370 GString *color = g_string_new(""); | |
1371 if(*q == '\'' || *q == '\"') | |
1372 q++; | |
1373 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
1374 color = g_string_append_c(color, *q); | |
1375 q++; | |
1376 } | |
1377 g_string_append_printf(style, "background: %s; ", color->str); | |
1378 g_string_free(color, TRUE); | |
1379 p = q; | |
1380 } else if(!g_ascii_strncasecmp(p, "color=", strlen("color="))) { | |
1381 const char *q = p + strlen("color="); | |
1382 GString *color = g_string_new(""); | |
1383 if(*q == '\'' || *q == '\"') | |
1384 q++; | |
1385 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
1386 color = g_string_append_c(color, *q); | |
1387 q++; | |
1388 } | |
1389 g_string_append_printf(style, "color: %s; ", color->str); | |
1390 g_string_free(color, TRUE); | |
1391 p = q; | |
1392 } else if(!g_ascii_strncasecmp(p, "face=", strlen("face="))) { | |
1393 const char *q = p + strlen("face="); | |
1394 gboolean space_allowed = FALSE; | |
1395 GString *face = g_string_new(""); | |
1396 if(*q == '\'' || *q == '\"') { | |
1397 space_allowed = TRUE; | |
1398 q++; | |
1399 } | |
1400 while(*q && *q != '\"' && *q != '\'' && (space_allowed || *q != ' ')) { | |
1401 face = g_string_append_c(face, *q); | |
1402 q++; | |
1403 } | |
1404 g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str)); | |
1405 g_string_free(face, TRUE); | |
1406 p = q; | |
1407 } else if(!g_ascii_strncasecmp(p, "size=", strlen("size="))) { | |
1408 const char *q = p + strlen("size="); | |
1409 int sz; | |
1410 const char *size = "medium"; | |
1411 if(*q == '\'' || *q == '\"') | |
1412 q++; | |
1413 sz = atoi(q); | |
1414 switch (sz) | |
1415 { | |
1416 case 1: | |
1417 size = "xx-small"; | |
1418 break; | |
1419 case 2: | |
1420 size = "x-small"; | |
1421 break; | |
1422 case 3: | |
1423 size = "small"; | |
1424 break; | |
1425 case 4: | |
1426 size = "medium"; | |
1427 break; | |
1428 case 5: | |
1429 size = "large"; | |
1430 break; | |
1431 case 6: | |
1432 size = "x-large"; | |
1433 break; | |
1434 case 7: | |
1435 size = "xx-large"; | |
1436 break; | |
1437 default: | |
1438 break; | |
1439 } | |
1440 g_string_append_printf(style, "font-size: %s; ", size); | |
1441 p = q; | |
1442 } | |
1443 p++; | |
1444 } | |
1445 if ((c = strchr(c, '>')) != NULL) | |
1446 c++; | |
1447 else | |
1448 c = p; | |
1449 pt = g_new0(struct gaim_parse_tag, 1); | |
1450 pt->src_tag = "font"; | |
1451 pt->dest_tag = "span"; | |
1452 tags = g_list_prepend(tags, pt); | |
1453 if(style->len) | |
1454 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str)); | |
1455 else | |
1456 pt->ignore = TRUE; | |
1457 g_string_free(style, TRUE); | |
1458 continue; | |
1459 } | |
1460 if(!g_ascii_strncasecmp(c, "<body ", 6)) { | |
1461 const char *p = c; | |
1462 gboolean did_something = FALSE; | |
1463 while(*p && *p != '>') { | |
1464 if(!g_ascii_strncasecmp(p, "bgcolor=", strlen("bgcolor="))) { | |
1465 const char *q = p + strlen("bgcolor="); | |
1466 struct gaim_parse_tag *pt = g_new0(struct gaim_parse_tag, 1); | |
1467 GString *color = g_string_new(""); | |
1468 if(*q == '\'' || *q == '\"') | |
1469 q++; | |
1470 while(*q && *q != '\"' && *q != '\'' && *q != ' ') { | |
1471 color = g_string_append_c(color, *q); | |
1472 q++; | |
1473 } | |
1474 g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str)); | |
1475 g_string_free(color, TRUE); | |
1476 if ((c = strchr(c, '>')) != NULL) | |
1477 c++; | |
1478 else | |
1479 c = p; | |
1480 pt->src_tag = "body"; | |
1481 pt->dest_tag = "span"; | |
1482 tags = g_list_prepend(tags, pt); | |
1483 did_something = TRUE; | |
1484 break; | |
1485 } | |
1486 p++; | |
1487 } | |
1488 if(did_something) continue; | |
1489 } | |
1490 /* this has to come after the special case for bgcolor */ | |
1491 ALLOW_TAG("body"); | |
1492 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) { | |
1493 char *p = strstr(c + strlen("<!--"), "-->"); | |
1494 if(p) { | |
1495 xhtml = g_string_append(xhtml, "<!--"); | |
1496 c += strlen("<!--"); | |
1497 continue; | |
1498 } | |
1499 } | |
1500 | |
1501 xhtml = g_string_append(xhtml, "<"); | |
1502 plain = g_string_append_c(plain, '<'); | |
1503 c++; | |
1504 } | |
1505 } else if(*c == '&') { | |
1506 char buf[7]; | |
1507 const char *pln; | |
1508 int len; | |
1509 | |
1510 if ((pln = detect_entity(c, &len)) == NULL) { | |
1511 len = 1; | |
1512 g_snprintf(buf, sizeof(buf), "%c", *c); | |
1513 pln = buf; | |
1514 } | |
1515 xhtml = g_string_append_len(xhtml, c, len); | |
1516 plain = g_string_append(plain, pln); | |
1517 c += len; | |
1518 } else { | |
1519 xhtml = g_string_append_c(xhtml, *c); | |
1520 plain = g_string_append_c(plain, *c); | |
1521 c++; | |
1522 } | |
1523 } | |
1524 tag = tags; | |
1525 while(tag) { | |
1526 struct gaim_parse_tag *pt = tag->data; | |
1527 if(!pt->ignore) | |
1528 g_string_append_printf(xhtml, "</%s>", pt->dest_tag); | |
1529 tag = tag->next; | |
1530 } | |
1531 g_list_free(tags); | |
1532 if(xhtml_out) | |
1533 *xhtml_out = g_strdup(xhtml->str); | |
1534 if(plain_out) | |
1535 *plain_out = g_strdup(plain->str); | |
1536 g_string_free(xhtml, TRUE); | |
1537 g_string_free(plain, TRUE); | |
1538 } | |
1539 | |
1540 /* The following are probably reasonable changes: | |
1541 * - \n should be converted to a normal space | |
1542 * - in addition to <br>, <p> and <div> etc. should also be converted into \n | |
1543 * - We want to turn </td>#whitespace<td> sequences into a single tab | |
1544 * - We want to turn <td> into a single tab (for msn profile "parsing") | |
1545 * - We want to turn </tr>#whitespace<tr> sequences into a single \n | |
1546 * - <script>...</script> and <style>...</style> should be completely removed | |
1547 */ | |
1548 | |
1549 char * | |
1550 gaim_markup_strip_html(const char *str) | |
1551 { | |
1552 int i, j, k, entlen; | |
1553 gboolean visible = TRUE; | |
1554 gboolean closing_td_p = FALSE; | |
1555 gchar *str2; | |
1556 const gchar *cdata_close_tag = NULL, *ent; | |
1557 gchar *href = NULL; | |
1558 int href_st = 0; | |
1559 | |
1560 if(!str) | |
1561 return NULL; | |
1562 | |
1563 str2 = g_strdup(str); | |
1564 | |
1565 for (i = 0, j = 0; str2[i]; i++) | |
1566 { | |
1567 if (str2[i] == '<') | |
1568 { | |
1569 if (cdata_close_tag) | |
1570 { | |
1571 /* Note: Don't even assume any other tag is a tag in CDATA */ | |
1572 if (strncasecmp(str2 + i, cdata_close_tag, | |
1573 strlen(cdata_close_tag)) == 0) | |
1574 { | |
1575 i += strlen(cdata_close_tag) - 1; | |
1576 cdata_close_tag = NULL; | |
1577 } | |
1578 continue; | |
1579 } | |
1580 else if (strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p) | |
1581 { | |
1582 str2[j++] = '\t'; | |
1583 visible = TRUE; | |
1584 } | |
1585 else if (strncasecmp(str2 + i, "</td>", 5) == 0) | |
1586 { | |
1587 closing_td_p = TRUE; | |
1588 visible = FALSE; | |
1589 } | |
1590 else | |
1591 { | |
1592 closing_td_p = FALSE; | |
1593 visible = TRUE; | |
1594 } | |
1595 | |
1596 k = i + 1; | |
1597 | |
1598 if(g_ascii_isspace(str2[k])) | |
1599 visible = TRUE; | |
1600 else if (str2[k]) | |
1601 { | |
1602 /* Scan until we end the tag either implicitly (closed start | |
1603 * tag) or explicitly, using a sloppy method (i.e., < or > | |
1604 * inside quoted attributes will screw us up) | |
1605 */ | |
1606 while (str2[k] && str2[k] != '<' && str2[k] != '>') | |
1607 { | |
1608 k++; | |
1609 } | |
1610 | |
1611 /* If we've got an <a> tag with an href, save the address | |
1612 * to print later. */ | |
1613 if (strncasecmp(str2 + i, "<a", 2) == 0 && | |
1614 g_ascii_isspace(str2[i+2])) | |
1615 { | |
1616 int st; /* start of href, inclusive [ */ | |
1617 int end; /* end of href, exclusive ) */ | |
1618 char delim = ' '; | |
1619 /* Find start of href */ | |
1620 for (st = i + 3; st < k; st++) | |
1621 { | |
1622 if (strncasecmp(str2+st, "href=", 5) == 0) | |
1623 { | |
1624 st += 5; | |
1625 if (str2[st] == '"') | |
1626 { | |
1627 delim = '"'; | |
1628 st++; | |
1629 } | |
1630 break; | |
1631 } | |
1632 } | |
1633 /* find end of address */ | |
1634 for (end = st; end < k && str2[end] != delim; end++) | |
1635 { | |
1636 /* All the work is done in the loop construct above. */ | |
1637 } | |
1638 | |
1639 /* If there's an address, save it. If there was | |
1640 * already one saved, kill it. */ | |
1641 if (st < k) | |
1642 { | |
1643 char *tmp; | |
1644 g_free(href); | |
1645 tmp = g_strndup(str2 + st, end - st); | |
1646 href = gaim_unescape_html(tmp); | |
1647 g_free(tmp); | |
1648 href_st = j; | |
1649 } | |
1650 } | |
1651 | |
1652 /* Replace </a> with an ascii representation of the | |
1653 * address the link was pointing to. */ | |
1654 else if (href != NULL && strncasecmp(str2 + i, "</a>", 4) == 0) | |
1655 { | |
1656 | |
1657 size_t hrlen = strlen(href); | |
1658 | |
1659 /* Only insert the href if it's different from the CDATA. */ | |
1660 if ((hrlen != j - href_st || | |
1661 strncmp(str2 + href_st, href, hrlen)) && | |
1662 (hrlen != j - href_st + 7 || /* 7 == strlen("http://") */ | |
1663 strncmp(str2 + href_st, href + 7, hrlen - 7))) | |
1664 { | |
1665 str2[j++] = ' '; | |
1666 str2[j++] = '('; | |
1667 g_memmove(str2 + j, href, hrlen); | |
1668 j += hrlen; | |
1669 str2[j++] = ')'; | |
1670 g_free(href); | |
1671 href = NULL; | |
1672 } | |
1673 } | |
1674 | |
1675 /* Check for tags which should be mapped to newline */ | |
1676 else if (strncasecmp(str2 + i, "<p>", 3) == 0 | |
1677 || strncasecmp(str2 + i, "<tr", 3) == 0 | |
1678 || strncasecmp(str2 + i, "<br", 3) == 0 | |
1679 || strncasecmp(str2 + i, "<li", 3) == 0 | |
1680 || strncasecmp(str2 + i, "<div", 4) == 0 | |
1681 || strncasecmp(str2 + i, "</table>", 8) == 0) | |
1682 { | |
1683 str2[j++] = '\n'; | |
1684 } | |
1685 /* Check for tags which begin CDATA and need to be closed */ | |
1686 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */ | |
1687 else if (strncasecmp(str2 + i, "<option", 7) == 0) | |
1688 { | |
1689 /* FIXME: We should not do this if the OPTION is SELECT'd */ | |
1690 cdata_close_tag = "</option>"; | |
1691 } | |
1692 #endif | |
1693 else if (strncasecmp(str2 + i, "<script", 7) == 0) | |
1694 { | |
1695 cdata_close_tag = "</script>"; | |
1696 } | |
1697 else if (strncasecmp(str2 + i, "<style", 6) == 0) | |
1698 { | |
1699 cdata_close_tag = "</style>"; | |
1700 } | |
1701 /* Update the index and continue checking after the tag */ | |
1702 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k; | |
1703 continue; | |
1704 } | |
1705 } | |
1706 else if (cdata_close_tag) | |
1707 { | |
1708 continue; | |
1709 } | |
1710 else if (!g_ascii_isspace(str2[i])) | |
1711 { | |
1712 visible = TRUE; | |
1713 } | |
1714 | |
1715 if (str2[i] == '&' && (ent = detect_entity(str2 + i, &entlen)) != NULL) | |
1716 { | |
1717 while (*ent) | |
1718 str2[j++] = *ent++; | |
1719 i += entlen - 1; | |
1720 continue; | |
1721 } | |
1722 | |
1723 if (visible) | |
1724 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i]; | |
1725 } | |
1726 | |
1727 g_free(href); | |
1728 | |
1729 str2[j] = '\0'; | |
1730 | |
1731 return str2; | |
1732 } | |
1733 | |
1734 static gboolean | |
1735 badchar(char c) | |
1736 { | |
1737 switch (c) { | |
1738 case ' ': | |
1739 case ',': | |
1740 case '\0': | |
1741 case '\n': | |
1742 case '\r': | |
1743 case '<': | |
1744 case '>': | |
1745 case '"': | |
1746 case '\'': | |
1747 return TRUE; | |
1748 default: | |
1749 return FALSE; | |
1750 } | |
1751 } | |
1752 | |
1753 static gboolean | |
1754 badentity(const char *c) | |
1755 { | |
1756 if (!g_ascii_strncasecmp(c, "<", 4) || | |
1757 !g_ascii_strncasecmp(c, ">", 4) || | |
1758 !g_ascii_strncasecmp(c, """, 6)) { | |
1759 return TRUE; | |
1760 } | |
1761 return FALSE; | |
1762 } | |
1763 | |
1764 char * | |
1765 gaim_markup_linkify(const char *text) | |
1766 { | |
1767 const char *c, *t, *q = NULL; | |
1768 char *tmpurlbuf, *url_buf; | |
1769 gunichar g; | |
1770 gboolean inside_html = FALSE; | |
1771 int inside_paren = 0; | |
1772 GString *ret = g_string_new(""); | |
1773 /* Assumes you have a buffer able to carry at least BUF_LEN * 2 bytes */ | |
1774 | |
1775 c = text; | |
1776 while (*c) { | |
1777 | |
1778 if(*c == '(' && !inside_html) { | |
1779 inside_paren++; | |
1780 ret = g_string_append_c(ret, *c); | |
1781 c++; | |
1782 } | |
1783 | |
1784 if(inside_html) { | |
1785 if(*c == '>') { | |
1786 inside_html = FALSE; | |
1787 } else if(!q && (*c == '\"' || *c == '\'')) { | |
1788 q = c; | |
1789 } else if(q) { | |
1790 if(*c == *q) | |
1791 q = NULL; | |
1792 } | |
1793 } else if(*c == '<') { | |
1794 inside_html = TRUE; | |
1795 if (!g_ascii_strncasecmp(c, "<A", 2)) { | |
1796 while (1) { | |
1797 if (!g_ascii_strncasecmp(c, "/A>", 3)) { | |
1798 inside_html = FALSE; | |
1799 break; | |
1800 } | |
1801 ret = g_string_append_c(ret, *c); | |
1802 c++; | |
1803 if (!(*c)) | |
1804 break; | |
1805 } | |
1806 } | |
1807 } else if ((*c=='h') && (!g_ascii_strncasecmp(c, "http://", 7) || | |
1808 (!g_ascii_strncasecmp(c, "https://", 8)))) { | |
1809 t = c; | |
1810 while (1) { | |
1811 if (badchar(*t) || badentity(t)) { | |
1812 | |
1813 if (*(t) == ',' && (*(t + 1) != ' ')) { | |
1814 t++; | |
1815 continue; | |
1816 } | |
1817 | |
1818 if (*(t - 1) == '.') | |
1819 t--; | |
1820 if ((*(t - 1) == ')' && (inside_paren > 0))) { | |
1821 t--; | |
1822 } | |
1823 | |
1824 url_buf = g_strndup(c, t - c); | |
1825 tmpurlbuf = gaim_unescape_html(url_buf); | |
1826 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", | |
1827 tmpurlbuf, url_buf); | |
1828 g_free(url_buf); | |
1829 g_free(tmpurlbuf); | |
1830 c = t; | |
1831 break; | |
1832 } | |
1833 t++; | |
1834 | |
1835 } | |
1836 } else if (!g_ascii_strncasecmp(c, "www.", 4) && (c == text || badchar(c[-1]) || badentity(c-1))) { | |
1837 if (c[4] != '.') { | |
1838 t = c; | |
1839 while (1) { | |
1840 if (badchar(*t) || badentity(t)) { | |
1841 if (t - c == 4) { | |
1842 break; | |
1843 } | |
1844 | |
1845 if (*(t) == ',' && (*(t + 1) != ' ')) { | |
1846 t++; | |
1847 continue; | |
1848 } | |
1849 | |
1850 if (*(t - 1) == '.') | |
1851 t--; | |
1852 if ((*(t - 1) == ')' && (inside_paren > 0))) { | |
1853 t--; | |
1854 } | |
1855 url_buf = g_strndup(c, t - c); | |
1856 tmpurlbuf = gaim_unescape_html(url_buf); | |
1857 g_string_append_printf(ret, | |
1858 "<A HREF=\"http://%s\">%s</A>", tmpurlbuf, | |
1859 url_buf); | |
1860 g_free(url_buf); | |
1861 g_free(tmpurlbuf); | |
1862 c = t; | |
1863 break; | |
1864 } | |
1865 t++; | |
1866 } | |
1867 } | |
14321 | 1868 } else if (!g_ascii_strncasecmp(c, "ftp://", 6) || !g_ascii_strncasecmp(c, "sftp://", 7)) { |
14192 | 1869 t = c; |
1870 while (1) { | |
1871 if (badchar(*t) || badentity(t)) { | |
1872 if (*(t - 1) == '.') | |
1873 t--; | |
1874 if ((*(t - 1) == ')' && (inside_paren > 0))) { | |
1875 t--; | |
1876 } | |
1877 url_buf = g_strndup(c, t - c); | |
1878 tmpurlbuf = gaim_unescape_html(url_buf); | |
1879 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", | |
1880 tmpurlbuf, url_buf); | |
1881 g_free(url_buf); | |
1882 g_free(tmpurlbuf); | |
1883 c = t; | |
1884 break; | |
1885 } | |
1886 if (!t) | |
1887 break; | |
1888 t++; | |
1889 | |
1890 } | |
1891 } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && (c == text || badchar(c[-1]) || badentity(c-1))) { | |
1892 if (c[4] != '.') { | |
1893 t = c; | |
1894 while (1) { | |
1895 if (badchar(*t) || badentity(t)) { | |
1896 if (t - c == 4) { | |
1897 break; | |
1898 } | |
1899 if (*(t - 1) == '.') | |
1900 t--; | |
1901 if ((*(t - 1) == ')' && (inside_paren > 0))) { | |
1902 t--; | |
1903 } | |
1904 url_buf = g_strndup(c, t - c); | |
1905 tmpurlbuf = gaim_unescape_html(url_buf); | |
1906 g_string_append_printf(ret, | |
1907 "<A HREF=\"ftp://%s\">%s</A>", tmpurlbuf, | |
1908 url_buf); | |
1909 g_free(url_buf); | |
1910 g_free(tmpurlbuf); | |
1911 c = t; | |
1912 break; | |
1913 } | |
1914 if (!t) | |
1915 break; | |
1916 t++; | |
1917 } | |
1918 } | |
1919 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) { | |
1920 t = c; | |
1921 while (1) { | |
1922 if (badchar(*t) || badentity(t)) { | |
1923 if (*(t - 1) == '.') | |
1924 t--; | |
1925 url_buf = g_strndup(c, t - c); | |
1926 tmpurlbuf = gaim_unescape_html(url_buf); | |
1927 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", | |
1928 tmpurlbuf, url_buf); | |
1929 g_free(url_buf); | |
1930 g_free(tmpurlbuf); | |
1931 c = t; | |
1932 break; | |
1933 } | |
1934 if (!t) | |
1935 break; | |
1936 t++; | |
1937 | |
1938 } | |
1939 } else if (c != text && (*c == '@')) { | |
1940 int flag; | |
1941 GString *gurl_buf = NULL; | |
1942 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; | |
1943 | |
1944 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1))) | |
1945 flag = 0; | |
1946 else { | |
1947 flag = 1; | |
1948 gurl_buf = g_string_new(""); | |
1949 } | |
1950 | |
1951 t = c; | |
1952 while (flag) { | |
1953 /* iterate backwards grabbing the local part of an email address */ | |
1954 g = g_utf8_get_char(t); | |
1955 if (badchar(*t) || (g >= 127) || (*t == '(') || | |
1956 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "<", 4) || | |
1957 !g_ascii_strncasecmp(t - 3, ">", 4))) || | |
1958 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, """, 6)))))) { | |
1959 /* local part will already be part of ret, strip it out */ | |
1960 ret = g_string_truncate(ret, ret->len - (c - t)); | |
1961 ret = g_string_append_unichar(ret, g); | |
1962 break; | |
1963 } else { | |
1964 g_string_prepend_unichar(gurl_buf, g); | |
1965 t = g_utf8_find_prev_char(text, t); | |
1966 if (t < text) { | |
1967 ret = g_string_assign(ret, ""); | |
1968 break; | |
1969 } | |
1970 } | |
1971 } | |
1972 | |
1973 t = g_utf8_find_next_char(c, NULL); | |
1974 | |
1975 while (flag) { | |
1976 /* iterate forwards grabbing the domain part of an email address */ | |
1977 g = g_utf8_get_char(t); | |
1978 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) { | |
1979 char *d; | |
1980 | |
1981 url_buf = g_string_free(gurl_buf, FALSE); | |
1982 | |
1983 /* strip off trailing periods */ | |
1984 if (strlen(url_buf) > 0) { | |
1985 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) | |
1986 *d = '\0'; | |
1987 } | |
1988 | |
1989 tmpurlbuf = gaim_unescape_html(url_buf); | |
1990 if (gaim_email_is_valid(tmpurlbuf)) { | |
1991 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>", | |
1992 tmpurlbuf, url_buf); | |
1993 } else { | |
1994 g_string_append(ret, url_buf); | |
1995 } | |
1996 g_free(url_buf); | |
1997 g_free(tmpurlbuf); | |
1998 c = t; | |
1999 | |
2000 break; | |
2001 } else { | |
2002 g_string_append_unichar(gurl_buf, g); | |
2003 t = g_utf8_find_next_char(t, NULL); | |
2004 } | |
2005 } | |
2006 } | |
2007 | |
2008 if(*c == ')' && !inside_html) { | |
2009 inside_paren--; | |
2010 ret = g_string_append_c(ret, *c); | |
2011 c++; | |
2012 } | |
2013 | |
2014 if (*c == 0) | |
2015 break; | |
2016 | |
2017 ret = g_string_append_c(ret, *c); | |
2018 c++; | |
2019 | |
2020 } | |
2021 return g_string_free(ret, FALSE); | |
2022 } | |
2023 | |
2024 char * | |
2025 gaim_unescape_html(const char *html) { | |
2026 if (html != NULL) { | |
2027 const char *c = html; | |
2028 GString *ret = g_string_new(""); | |
2029 while (*c) { | |
2030 int len; | |
2031 const char *ent; | |
2032 | |
2033 if ((ent = detect_entity(c, &len)) != NULL) { | |
2034 ret = g_string_append(ret, ent); | |
2035 c += len; | |
2036 } else if (!strncmp(c, "<br>", 4)) { | |
2037 ret = g_string_append_c(ret, '\n'); | |
2038 c += 4; | |
2039 } else { | |
2040 ret = g_string_append_c(ret, *c); | |
2041 c++; | |
2042 } | |
2043 } | |
2044 return g_string_free(ret, FALSE); | |
2045 } | |
2046 | |
2047 return NULL; | |
2048 } | |
2049 | |
2050 char * | |
2051 gaim_markup_slice(const char *str, guint x, guint y) | |
2052 { | |
2053 GString *ret; | |
2054 GQueue *q; | |
2055 guint z = 0; | |
2056 gboolean appended = FALSE; | |
2057 gunichar c; | |
2058 char *tag; | |
2059 | |
2060 g_return_val_if_fail(x <= y, NULL); | |
2061 | |
2062 if (x == y) | |
2063 return g_strdup(""); | |
2064 | |
2065 ret = g_string_new(""); | |
2066 q = g_queue_new(); | |
2067 | |
2068 while (*str && (z < y)) { | |
2069 c = g_utf8_get_char(str); | |
2070 | |
2071 if (c == '<') { | |
2072 char *end = strchr(str, '>'); | |
2073 | |
2074 if (!end) { | |
2075 g_string_free(ret, TRUE); | |
2076 while ((tag = g_queue_pop_head(q))) | |
2077 g_free(tag); | |
2078 g_queue_free(q); | |
2079 return NULL; | |
2080 } | |
2081 | |
2082 if (!g_ascii_strncasecmp(str, "<img ", 5)) { | |
2083 z += strlen("[Image]"); | |
2084 } else if (!g_ascii_strncasecmp(str, "<br", 3)) { | |
2085 z += 1; | |
2086 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) { | |
2087 z += strlen("\n---\n"); | |
2088 } else if (!g_ascii_strncasecmp(str, "</", 2)) { | |
2089 /* pop stack */ | |
2090 char *tmp; | |
2091 | |
2092 tmp = g_queue_pop_head(q); | |
2093 g_free(tmp); | |
2094 /* z += 0; */ | |
2095 } else { | |
2096 /* push it unto the stack */ | |
2097 char *tmp; | |
2098 | |
2099 tmp = g_strndup(str, end - str + 1); | |
2100 g_queue_push_head(q, tmp); | |
2101 /* z += 0; */ | |
2102 } | |
2103 | |
2104 if (z >= x) { | |
2105 g_string_append_len(ret, str, end - str + 1); | |
2106 } | |
2107 | |
2108 str = end; | |
2109 } else if (c == '&') { | |
2110 char *end = strchr(str, ';'); | |
2111 if (!end) { | |
2112 g_string_free(ret, TRUE); | |
2113 while ((tag = g_queue_pop_head(q))) | |
2114 g_free(tag); | |
2115 g_queue_free(q); | |
2116 | |
2117 return NULL; | |
2118 } | |
2119 | |
2120 if (z >= x) | |
2121 g_string_append_len(ret, str, end - str + 1); | |
2122 | |
2123 z++; | |
2124 str = end; | |
2125 } else { | |
2126 if (z == x && z > 0 && !appended) { | |
2127 GList *l = q->tail; | |
2128 | |
2129 while (l) { | |
2130 tag = l->data; | |
2131 g_string_append(ret, tag); | |
2132 l = l->prev; | |
2133 } | |
2134 appended = TRUE; | |
2135 } | |
2136 | |
2137 if (z >= x) | |
2138 g_string_append_unichar(ret, c); | |
2139 z++; | |
2140 } | |
2141 | |
2142 str = g_utf8_next_char(str); | |
2143 } | |
2144 | |
2145 while ((tag = g_queue_pop_head(q))) { | |
2146 char *name; | |
2147 | |
2148 name = gaim_markup_get_tag_name(tag); | |
2149 g_string_append_printf(ret, "</%s>", name); | |
2150 g_free(name); | |
2151 g_free(tag); | |
2152 } | |
2153 | |
2154 g_queue_free(q); | |
2155 return g_string_free(ret, FALSE); | |
2156 } | |
2157 | |
2158 char * | |
2159 gaim_markup_get_tag_name(const char *tag) | |
2160 { | |
2161 int i; | |
2162 g_return_val_if_fail(tag != NULL, NULL); | |
2163 g_return_val_if_fail(*tag == '<', NULL); | |
2164 | |
2165 for (i = 1; tag[i]; i++) | |
2166 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/') | |
2167 break; | |
2168 | |
2169 return g_strndup(tag+1, i-1); | |
2170 } | |
2171 | |
2172 /************************************************************************** | |
2173 * Path/Filename Functions | |
2174 **************************************************************************/ | |
2175 const char * | |
2176 gaim_home_dir(void) | |
2177 { | |
2178 #ifndef _WIN32 | |
2179 return g_get_home_dir(); | |
2180 #else | |
2181 return wgaim_data_dir(); | |
2182 #endif | |
2183 } | |
2184 | |
2185 /* returns a string of the form ~/.gaim, where ~ is replaced by the user's home | |
2186 * dir. Note that there is no trailing slash after .gaim. */ | |
2187 const char * | |
2188 gaim_user_dir(void) | |
2189 { | |
2190 if (custom_home_dir != NULL && strlen(custom_home_dir) > 0) { | |
2191 strcpy ((char*) &home_dir, (char*) &custom_home_dir); | |
2192 } else { | |
2193 const gchar *hd = gaim_home_dir(); | |
2194 | |
2195 if (hd) { | |
2196 g_strlcpy((char*) &home_dir, hd, sizeof(home_dir)); | |
2197 g_strlcat((char*) &home_dir, G_DIR_SEPARATOR_S ".gaim", | |
2198 sizeof(home_dir)); | |
2199 } | |
2200 } | |
2201 | |
2202 return home_dir; | |
2203 } | |
2204 | |
2205 void gaim_util_set_user_dir(const char *dir) | |
2206 { | |
2207 if (dir != NULL && strlen(dir) > 0) { | |
2208 g_strlcpy((char*) &custom_home_dir, dir, | |
2209 sizeof(custom_home_dir)); | |
2210 } | |
2211 } | |
2212 | |
2213 int gaim_build_dir (const char *path, int mode) | |
2214 { | |
2215 #if GLIB_CHECK_VERSION(2,8,0) | |
2216 return g_mkdir_with_parents(path, mode); | |
2217 #else | |
2218 char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' }; | |
2219 int cur, len; | |
2220 | |
2221 g_return_val_if_fail(path != NULL, -1); | |
2222 | |
2223 dir = g_new0(char, strlen(path) + 1); | |
2224 components = g_strsplit(path, delim, -1); | |
2225 len = 0; | |
2226 for (cur = 0; components[cur] != NULL; cur++) { | |
2227 /* If you don't know what you're doing on both | |
2228 * win32 and *NIX, stay the hell away from this code */ | |
2229 if(cur > 1) | |
2230 dir[len++] = G_DIR_SEPARATOR; | |
2231 strcpy(dir + len, components[cur]); | |
2232 len += strlen(components[cur]); | |
2233 if(cur == 0) | |
2234 dir[len++] = G_DIR_SEPARATOR; | |
2235 | |
2236 if(g_file_test(dir, G_FILE_TEST_IS_DIR)) { | |
2237 continue; | |
2238 #ifdef _WIN32 | |
2239 /* allow us to create subdirs on UNC paths | |
2240 * (\\machinename\path\to\blah) | |
2241 * g_file_test() doesn't work on "\\machinename" */ | |
2242 } else if (cur == 2 && dir[0] == '\\' && dir[1] == '\\' | |
2243 && components[cur + 1] != NULL) { | |
2244 continue; | |
2245 #endif | |
2246 } else if(g_file_test(dir, G_FILE_TEST_EXISTS)) { | |
2247 gaim_debug_warning("build_dir", "bad path: %s\n", path); | |
2248 g_strfreev(components); | |
2249 g_free(dir); | |
2250 return -1; | |
2251 } | |
2252 | |
2253 if (g_mkdir(dir, mode) < 0) { | |
2254 gaim_debug_warning("build_dir", "mkdir: %s\n", strerror(errno)); | |
2255 g_strfreev(components); | |
2256 g_free(dir); | |
2257 return -1; | |
2258 } | |
2259 } | |
2260 | |
2261 g_strfreev(components); | |
2262 g_free(dir); | |
2263 return 0; | |
2264 #endif | |
2265 } | |
2266 | |
2267 /* | |
2268 * This function is long and beautiful, like my--um, yeah. Anyway, | |
2269 * it includes lots of error checking so as we don't overwrite | |
2270 * people's settings if there is a problem writing the new values. | |
2271 */ | |
2272 gboolean | |
2273 gaim_util_write_data_to_file(const char *filename, const char *data, size_t size) | |
2274 { | |
2275 const char *user_dir = gaim_user_dir(); | |
2276 gchar *filename_temp, *filename_full; | |
2277 FILE *file; | |
2278 size_t real_size, byteswritten; | |
2279 struct stat st; | |
2280 | |
2281 g_return_val_if_fail(user_dir != NULL, FALSE); | |
2282 | |
2283 gaim_debug_info("util", "Writing file %s to directory %s\n", | |
2284 filename, user_dir); | |
2285 | |
2286 /* Ensure the user directory exists */ | |
2287 if (!g_file_test(user_dir, G_FILE_TEST_IS_DIR)) | |
2288 { | |
2289 if (g_mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) | |
2290 { | |
2291 gaim_debug_error("util", "Error creating directory %s: %s\n", | |
2292 user_dir, strerror(errno)); | |
2293 return FALSE; | |
2294 } | |
2295 } | |
2296 | |
2297 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", user_dir, filename); | |
2298 filename_temp = g_strdup_printf("%s.save", filename_full); | |
2299 | |
2300 /* Remove an old temporary file, if one exists */ | |
2301 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS)) | |
2302 { | |
2303 if (g_unlink(filename_temp) == -1) | |
2304 { | |
2305 gaim_debug_error("util", "Error removing old file %s: %s\n", | |
2306 filename_temp, strerror(errno)); | |
2307 } | |
2308 } | |
2309 | |
2310 /* Open file */ | |
2311 file = g_fopen(filename_temp, "wb"); | |
2312 if (file == NULL) | |
2313 { | |
2314 gaim_debug_error("util", "Error opening file %s for writing: %s\n", | |
2315 filename_temp, strerror(errno)); | |
2316 g_free(filename_full); | |
2317 g_free(filename_temp); | |
2318 return FALSE; | |
2319 } | |
2320 | |
2321 /* Write to file */ | |
2322 real_size = (size == -1) ? strlen(data) : size; | |
2323 byteswritten = fwrite(data, 1, real_size, file); | |
2324 | |
2325 /* Close file */ | |
2326 if (fclose(file) != 0) | |
2327 { | |
2328 gaim_debug_error("util", "Error closing file %s: %s\n", | |
2329 filename_temp, strerror(errno)); | |
2330 g_free(filename_full); | |
2331 g_free(filename_temp); | |
2332 return FALSE; | |
2333 } | |
2334 | |
2335 /* Ensure the file is the correct size */ | |
2336 if (byteswritten != real_size) | |
2337 { | |
2338 gaim_debug_error("util", "Error writing to file %s: Wrote %" G_GSIZE_FORMAT " bytes " | |
2339 "but should have written %" G_GSIZE_FORMAT "; is your disk full?\n", | |
2340 filename_temp, byteswritten, real_size); | |
2341 g_free(filename_full); | |
2342 g_free(filename_temp); | |
2343 return FALSE; | |
2344 } | |
2345 /* Use stat to be absolutely sure. */ | |
2346 if ((g_stat(filename_temp, &st) == -1) || (st.st_size != real_size)) | |
2347 { | |
2348 gaim_debug_error("util", "Error writing data to file %s: " | |
2349 "Incomplete file written; is your disk full?\n", | |
2350 filename_temp); | |
2351 g_free(filename_full); | |
2352 g_free(filename_temp); | |
2353 return FALSE; | |
2354 } | |
2355 | |
2356 #ifndef _WIN32 | |
2357 /* Set file permissions */ | |
2358 if (chmod(filename_temp, S_IRUSR | S_IWUSR) == -1) | |
2359 { | |
2360 gaim_debug_error("util", "Error setting permissions of file %s: %s\n", | |
2361 filename_temp, strerror(errno)); | |
2362 } | |
2363 #endif | |
2364 | |
2365 /* Rename to the REAL name */ | |
2366 if (g_rename(filename_temp, filename_full) == -1) | |
2367 { | |
2368 gaim_debug_error("util", "Error renaming %s to %s: %s\n", | |
2369 filename_temp, filename_full, strerror(errno)); | |
2370 } | |
2371 | |
2372 g_free(filename_full); | |
2373 g_free(filename_temp); | |
2374 | |
2375 return TRUE; | |
2376 } | |
2377 | |
2378 xmlnode * | |
2379 gaim_util_read_xml_from_file(const char *filename, const char *description) | |
2380 { | |
2381 const char *user_dir = gaim_user_dir(); | |
2382 gchar *filename_full; | |
14878 | 2383 GError *error = NULL; |
14192 | 2384 gchar *contents = NULL; |
2385 gsize length; | |
2386 xmlnode *node = NULL; | |
2387 | |
2388 g_return_val_if_fail(user_dir != NULL, NULL); | |
2389 | |
2390 gaim_debug_info("util", "Reading file %s from directory %s\n", | |
2391 filename, user_dir); | |
2392 | |
2393 filename_full = g_build_filename(user_dir, filename, NULL); | |
2394 | |
2395 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
2396 { | |
2397 gaim_debug_info("util", "File %s does not exist (this is not " | |
2398 "necessarily an error)\n", filename_full); | |
2399 g_free(filename_full); | |
2400 return NULL; | |
2401 } | |
2402 | |
2403 if (!g_file_get_contents(filename_full, &contents, &length, &error)) | |
2404 { | |
2405 gaim_debug_error("util", "Error reading file %s: %s\n", | |
2406 filename_full, error->message); | |
2407 g_error_free(error); | |
2408 } | |
2409 | |
2410 if ((contents != NULL) && (length > 0)) | |
2411 { | |
2412 node = xmlnode_from_str(contents, length); | |
2413 | |
2414 /* If we were unable to parse the file then save its contents to a backup file */ | |
2415 if (node == NULL) | |
2416 { | |
2417 gchar *filename_temp; | |
2418 | |
2419 filename_temp = g_strdup_printf("%s~", filename); | |
14586
089e65ea9813
[gaim-migrate @ 17310]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14403
diff
changeset
|
2420 gaim_debug_error("util", "Error parsing file %s. Renaming old " |
14192 | 2421 "file to %s\n", filename_full, filename_temp); |
2422 gaim_util_write_data_to_file(filename_temp, contents, length); | |
2423 g_free(filename_temp); | |
2424 } | |
2425 | |
2426 g_free(contents); | |
2427 } | |
2428 | |
2429 /* If we could not parse the file then show the user an error message */ | |
2430 if (node == NULL) | |
2431 { | |
2432 gchar *title, *msg; | |
2433 title = g_strdup_printf(_("Error Reading %s"), filename); | |
2434 msg = g_strdup_printf(_("An error was encountered reading your " | |
2435 "%s. They have not been loaded, and the old file " | |
2436 "has been renamed to %s~."), description, filename_full); | |
2437 gaim_notify_error(NULL, NULL, title, msg); | |
2438 g_free(title); | |
2439 g_free(msg); | |
2440 } | |
2441 | |
2442 g_free(filename_full); | |
2443 | |
2444 return node; | |
2445 } | |
2446 | |
2447 /* | |
2448 * Like mkstemp() but returns a file pointer, uses a pre-set template, | |
2449 * uses the semantics of tempnam() for the directory to use and allocates | |
2450 * the space for the filepath. | |
2451 * | |
2452 * Caller is responsible for closing the file and removing it when done, | |
2453 * as well as freeing the space pointed-to by "path" with g_free(). | |
2454 * | |
2455 * Returns NULL on failure and cleans up after itself if so. | |
2456 */ | |
2457 static const char *gaim_mkstemp_templ = {"gaimXXXXXX"}; | |
2458 | |
2459 FILE * | |
2460 gaim_mkstemp(char **fpath, gboolean binary) | |
2461 { | |
2462 const gchar *tmpdir; | |
2463 #ifndef _WIN32 | |
2464 int fd; | |
2465 #endif | |
2466 FILE *fp = NULL; | |
2467 | |
2468 g_return_val_if_fail(fpath != NULL, NULL); | |
2469 | |
2470 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) { | |
2471 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, gaim_mkstemp_templ)) != NULL) { | |
2472 #ifdef _WIN32 | |
2473 char* result = _mktemp( *fpath ); | |
2474 if( result == NULL ) | |
2475 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", | |
2476 "Problem creating the template\n"); | |
2477 else | |
2478 { | |
2479 if( (fp = g_fopen( result, binary?"wb+":"w+")) == NULL ) { | |
2480 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", | |
2481 "Couldn't fopen() %s\n", result); | |
2482 } | |
2483 } | |
2484 #else | |
2485 if((fd = mkstemp(*fpath)) == -1) { | |
2486 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", | |
2487 "Couldn't make \"%s\", error: %d\n", | |
2488 *fpath, errno); | |
2489 } else { | |
2490 if((fp = fdopen(fd, "r+")) == NULL) { | |
2491 close(fd); | |
2492 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", | |
2493 "Couldn't fdopen(), error: %d\n", errno); | |
2494 } | |
2495 } | |
2496 #endif | |
2497 if(!fp) { | |
2498 g_free(*fpath); | |
2499 *fpath = NULL; | |
2500 } | |
2501 } | |
2502 } else { | |
2503 gaim_debug(GAIM_DEBUG_ERROR, "gaim_mkstemp", | |
2504 "g_get_tmp_dir() failed!\n"); | |
2505 } | |
2506 | |
2507 return fp; | |
2508 } | |
2509 | |
2510 gboolean | |
2511 gaim_program_is_valid(const char *program) | |
2512 { | |
2513 GError *error = NULL; | |
2514 char **argv; | |
2515 gchar *progname; | |
2516 gboolean is_valid = FALSE; | |
2517 | |
2518 g_return_val_if_fail(program != NULL, FALSE); | |
2519 g_return_val_if_fail(*program != '\0', FALSE); | |
2520 | |
2521 if (!g_shell_parse_argv(program, NULL, &argv, &error)) { | |
2522 gaim_debug(GAIM_DEBUG_ERROR, "program_is_valid", | |
2523 "Could not parse program '%s': %s\n", | |
2524 program, error->message); | |
2525 g_error_free(error); | |
2526 return FALSE; | |
2527 } | |
2528 | |
2529 if (argv == NULL) { | |
2530 return FALSE; | |
2531 } | |
2532 | |
2533 progname = g_find_program_in_path(argv[0]); | |
2534 is_valid = (progname != NULL); | |
2535 | |
2536 g_strfreev(argv); | |
2537 g_free(progname); | |
2538 | |
2539 return is_valid; | |
2540 } | |
2541 | |
2542 | |
2543 gboolean | |
2544 gaim_running_gnome(void) | |
2545 { | |
2546 #ifndef _WIN32 | |
2547 gchar *tmp = g_find_program_in_path("gnome-open"); | |
2548 | |
2549 if (tmp == NULL) | |
2550 return FALSE; | |
2551 g_free(tmp); | |
2552 | |
2553 return (g_getenv("GNOME_DESKTOP_SESSION_ID") != NULL); | |
2554 #else | |
2555 return FALSE; | |
2556 #endif | |
2557 } | |
2558 | |
2559 gboolean | |
2560 gaim_running_kde(void) | |
2561 { | |
2562 #ifndef _WIN32 | |
2563 gchar *tmp = g_find_program_in_path("kfmclient"); | |
2564 const char *session; | |
2565 | |
2566 if (tmp == NULL) | |
2567 return FALSE; | |
2568 g_free(tmp); | |
2569 | |
2570 session = g_getenv("KDE_FULL_SESSION"); | |
2571 if (session != NULL && !strcmp(session, "true")) | |
2572 return TRUE; | |
2573 | |
2574 /* If you run Gaim from Konsole under !KDE, this will provide a | |
2575 * a false positive. Since we do the GNOME checks first, this is | |
2576 * only a problem if you're running something !(KDE || GNOME) and | |
2577 * you run Gaim from Konsole. This really shouldn't be a problem. */ | |
2578 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL); | |
2579 #else | |
2580 return FALSE; | |
2581 #endif | |
2582 } | |
2583 | |
14955
e1446f88650c
[gaim-migrate @ 17733]
Luke Schierer <lschiere@pidgin.im>
parents:
14878
diff
changeset
|
2584 gboolean |
14646
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2585 gaim_running_osx(void) |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2586 { |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2587 #if defined(__APPLE__) |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2588 return TRUE; |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2589 #else |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2590 return FALSE; |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2591 #endif |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2592 } |
e0a93e6fa98b
[gaim-migrate @ 17392]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14586
diff
changeset
|
2593 |
14192 | 2594 char * |
2595 gaim_fd_get_ip(int fd) | |
2596 { | |
2597 struct sockaddr addr; | |
2598 socklen_t namelen = sizeof(addr); | |
2599 | |
2600 g_return_val_if_fail(fd != 0, NULL); | |
2601 | |
2602 if (getsockname(fd, &addr, &namelen)) | |
2603 return NULL; | |
2604 | |
2605 return g_strdup(inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr)); | |
2606 } | |
2607 | |
2608 | |
2609 /************************************************************************** | |
2610 * String Functions | |
2611 **************************************************************************/ | |
2612 const char * | |
2613 gaim_normalize(const GaimAccount *account, const char *str) | |
2614 { | |
2615 const char *ret = NULL; | |
2616 | |
2617 if (account != NULL) | |
2618 { | |
2619 GaimPlugin *prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
2620 | |
2621 if (prpl != NULL) | |
2622 { | |
2623 GaimPluginProtocolInfo *prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
2624 | |
2625 if(prpl_info && prpl_info->normalize) | |
2626 ret = prpl_info->normalize(account, str); | |
2627 } | |
2628 } | |
2629 | |
2630 if (ret == NULL) | |
2631 { | |
2632 static char buf[BUF_LEN]; | |
2633 char *tmp; | |
2634 | |
2635 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT); | |
2636 g_snprintf(buf, sizeof(buf), "%s", tmp); | |
2637 g_free(tmp); | |
2638 | |
2639 ret = buf; | |
2640 } | |
2641 | |
2642 return ret; | |
2643 } | |
2644 | |
2645 /* | |
2646 * You probably don't want to call this directly, it is | |
2647 * mainly for use as a PRPL callback function. See the | |
2648 * comments in util.h. | |
2649 */ | |
2650 const char * | |
2651 gaim_normalize_nocase(const GaimAccount *account, const char *str) | |
2652 { | |
2653 static char buf[BUF_LEN]; | |
2654 char *tmp1, *tmp2; | |
2655 | |
2656 g_return_val_if_fail(str != NULL, NULL); | |
2657 | |
2658 tmp1 = g_utf8_strdown(str, -1); | |
2659 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT); | |
2660 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : ""); | |
2661 g_free(tmp2); | |
2662 g_free(tmp1); | |
2663 | |
2664 return buf; | |
2665 } | |
2666 | |
2667 gchar * | |
2668 gaim_strdup_withhtml(const gchar *src) | |
2669 { | |
2670 gulong destsize, i, j; | |
2671 gchar *dest; | |
2672 | |
2673 g_return_val_if_fail(src != NULL, NULL); | |
2674 | |
2675 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */ | |
2676 destsize = 1; | |
2677 for (i = 0; src[i] != '\0'; i++) | |
2678 { | |
2679 if (src[i] == '\n') | |
2680 destsize += 4; | |
2681 else if (src[i] != '\r') | |
2682 destsize++; | |
2683 } | |
2684 | |
2685 dest = g_malloc(destsize); | |
2686 | |
2687 /* Copy stuff, ignoring \r's, because they are dumb */ | |
2688 for (i = 0, j = 0; src[i] != '\0'; i++) { | |
2689 if (src[i] == '\n') { | |
2690 strcpy(&dest[j], "<BR>"); | |
2691 j += 4; | |
2692 } else if (src[i] != '\r') | |
2693 dest[j++] = src[i]; | |
2694 } | |
2695 | |
2696 dest[destsize-1] = '\0'; | |
2697 | |
2698 return dest; | |
2699 } | |
2700 | |
2701 gboolean | |
2702 gaim_str_has_prefix(const char *s, const char *p) | |
2703 { | |
2704 #if GLIB_CHECK_VERSION(2,2,0) | |
2705 return g_str_has_prefix(s, p); | |
2706 #else | |
2707 g_return_val_if_fail(s != NULL, FALSE); | |
2708 g_return_val_if_fail(p != NULL, FALSE); | |
2709 | |
2710 return (!strncmp(s, p, strlen(p))); | |
2711 #endif | |
2712 } | |
2713 | |
2714 gboolean | |
2715 gaim_str_has_suffix(const char *s, const char *x) | |
2716 { | |
2717 #if GLIB_CHECK_VERSION(2,2,0) | |
2718 return g_str_has_suffix(s, x); | |
2719 #else | |
2720 int off; | |
2721 | |
2722 g_return_val_if_fail(s != NULL, FALSE); | |
2723 g_return_val_if_fail(x != NULL, FALSE); | |
2724 | |
2725 off = strlen(s) - strlen(x); | |
2726 return (off >= 0 && !strcmp(s + off, x)); | |
2727 #endif | |
2728 } | |
2729 | |
2730 char * | |
2731 gaim_str_add_cr(const char *text) | |
2732 { | |
2733 char *ret = NULL; | |
2734 int count = 0, j; | |
2735 guint i; | |
2736 | |
2737 g_return_val_if_fail(text != NULL, NULL); | |
2738 | |
2739 if (text[0] == '\n') | |
2740 count++; | |
2741 for (i = 1; i < strlen(text); i++) | |
2742 if (text[i] == '\n' && text[i - 1] != '\r') | |
2743 count++; | |
2744 | |
2745 if (count == 0) | |
2746 return g_strdup(text); | |
2747 | |
2748 ret = g_malloc0(strlen(text) + count + 1); | |
2749 | |
2750 i = 0; j = 0; | |
2751 if (text[i] == '\n') | |
2752 ret[j++] = '\r'; | |
2753 ret[j++] = text[i++]; | |
2754 for (; i < strlen(text); i++) { | |
2755 if (text[i] == '\n' && text[i - 1] != '\r') | |
2756 ret[j++] = '\r'; | |
2757 ret[j++] = text[i]; | |
2758 } | |
2759 | |
2760 gaim_debug_misc("gaim_str_add_cr", "got: %s, leaving with %s\n", | |
2761 text, ret); | |
2762 | |
2763 return ret; | |
2764 } | |
2765 | |
2766 void | |
2767 gaim_str_strip_char(char *text, char thechar) | |
2768 { | |
2769 int i, j; | |
2770 | |
2771 g_return_if_fail(text != NULL); | |
2772 | |
2773 for (i = 0, j = 0; text[i]; i++) | |
2774 if (text[i] != thechar) | |
2775 text[j++] = text[i]; | |
2776 | |
2777 text[j++] = '\0'; | |
2778 } | |
2779 | |
2780 void | |
2781 gaim_util_chrreplace(char *string, char delimiter, | |
2782 char replacement) | |
2783 { | |
2784 int i = 0; | |
2785 | |
2786 g_return_if_fail(string != NULL); | |
2787 | |
2788 while (string[i] != '\0') | |
2789 { | |
2790 if (string[i] == delimiter) | |
2791 string[i] = replacement; | |
2792 i++; | |
2793 } | |
2794 } | |
2795 | |
2796 gchar * | |
2797 gaim_strreplace(const char *string, const char *delimiter, | |
2798 const char *replacement) | |
2799 { | |
2800 gchar **split; | |
2801 gchar *ret; | |
2802 | |
2803 g_return_val_if_fail(string != NULL, NULL); | |
2804 g_return_val_if_fail(delimiter != NULL, NULL); | |
2805 g_return_val_if_fail(replacement != NULL, NULL); | |
2806 | |
2807 split = g_strsplit(string, delimiter, 0); | |
2808 ret = g_strjoinv(replacement, split); | |
2809 g_strfreev(split); | |
2810 | |
2811 return ret; | |
2812 } | |
2813 | |
2814 gchar * | |
2815 gaim_strcasereplace(const char *string, const char *delimiter, | |
2816 const char *replacement) | |
2817 { | |
2818 gchar *ret; | |
2819 int length_del, length_rep, i, j; | |
2820 | |
2821 g_return_val_if_fail(string != NULL, NULL); | |
2822 g_return_val_if_fail(delimiter != NULL, NULL); | |
2823 g_return_val_if_fail(replacement != NULL, NULL); | |
2824 | |
2825 length_del = strlen(delimiter); | |
2826 length_rep = strlen(replacement); | |
2827 | |
2828 /* Count how many times the delimiter appears */ | |
2829 i = 0; /* position in the source string */ | |
2830 j = 0; /* number of occurrences of "delimiter" */ | |
2831 while (string[i] != '\0') { | |
2832 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
2833 i += length_del; | |
2834 j += length_rep; | |
2835 } else { | |
2836 i++; | |
2837 j++; | |
2838 } | |
2839 } | |
2840 | |
2841 ret = g_malloc(j+1); | |
2842 | |
2843 i = 0; /* position in the source string */ | |
2844 j = 0; /* position in the destination string */ | |
2845 while (string[i] != '\0') { | |
2846 if (!strncasecmp(&string[i], delimiter, length_del)) { | |
2847 strncpy(&ret[j], replacement, length_rep); | |
2848 i += length_del; | |
2849 j += length_rep; | |
2850 } else { | |
2851 ret[j] = string[i]; | |
2852 i++; | |
2853 j++; | |
2854 } | |
2855 } | |
2856 | |
2857 ret[j] = '\0'; | |
2858 | |
2859 return ret; | |
2860 } | |
2861 | |
2862 const char * | |
2863 gaim_strcasestr(const char *haystack, const char *needle) | |
2864 { | |
2865 size_t hlen, nlen; | |
2866 const char *tmp, *ret; | |
2867 | |
2868 g_return_val_if_fail(haystack != NULL, NULL); | |
2869 g_return_val_if_fail(needle != NULL, NULL); | |
2870 | |
2871 hlen = strlen(haystack); | |
2872 nlen = strlen(needle); | |
2873 tmp = haystack, | |
2874 ret = NULL; | |
2875 | |
2876 g_return_val_if_fail(hlen > 0, NULL); | |
2877 g_return_val_if_fail(nlen > 0, NULL); | |
2878 | |
2879 while (*tmp && !ret) { | |
2880 if (!g_ascii_strncasecmp(needle, tmp, nlen)) | |
2881 ret = tmp; | |
2882 else | |
2883 tmp++; | |
2884 } | |
2885 | |
2886 return ret; | |
2887 } | |
2888 | |
2889 char * | |
2890 gaim_str_size_to_units(size_t size) | |
2891 { | |
2892 static const char *size_str[4] = { "bytes", "KB", "MB", "GB" }; | |
2893 float size_mag; | |
2894 int size_index = 0; | |
2895 | |
2896 if (size == -1) { | |
2897 return g_strdup(_("Calculating...")); | |
2898 } | |
2899 else if (size == 0) { | |
2900 return g_strdup(_("Unknown.")); | |
2901 } | |
2902 else { | |
2903 size_mag = (float)size; | |
2904 | |
2905 while ((size_index < 3) && (size_mag > 1024)) { | |
2906 size_mag /= 1024; | |
2907 size_index++; | |
2908 } | |
2909 | |
2910 if (size_index == 0) { | |
2911 return g_strdup_printf("%" G_GSIZE_FORMAT " %s", size, size_str[size_index]); | |
2912 } else { | |
2913 return g_strdup_printf("%.2f %s", size_mag, size_str[size_index]); | |
2914 } | |
2915 } | |
2916 } | |
2917 | |
2918 char * | |
2919 gaim_str_seconds_to_string(guint secs) | |
2920 { | |
2921 char *ret = NULL; | |
2922 guint days, hrs, mins; | |
2923 | |
2924 if (secs < 60) | |
2925 { | |
2926 return g_strdup_printf(ngettext("%d second", "%d seconds", secs), secs); | |
2927 } | |
2928 | |
2929 days = secs / (60 * 60 * 24); | |
2930 secs = secs % (60 * 60 * 24); | |
2931 hrs = secs / (60 * 60); | |
2932 secs = secs % (60 * 60); | |
2933 mins = secs / 60; | |
2934 secs = secs % 60; | |
2935 | |
2936 if (days > 0) | |
2937 { | |
2938 ret = g_strdup_printf(ngettext("%d day", "%d days", days), days); | |
2939 } | |
2940 | |
2941 if (hrs > 0) | |
2942 { | |
2943 if (ret != NULL) | |
2944 { | |
2945 char *tmp = g_strdup_printf( | |
2946 ngettext("%s, %d hour", "%s, %d hours", hrs), | |
2947 ret, hrs); | |
2948 g_free(ret); | |
2949 ret = tmp; | |
2950 } | |
2951 else | |
2952 ret = g_strdup_printf(ngettext("%d hour", "%d hours", hrs), hrs); | |
2953 } | |
2954 | |
2955 if (mins > 0) | |
2956 { | |
2957 if (ret != NULL) | |
2958 { | |
2959 char *tmp = g_strdup_printf( | |
2960 ngettext("%s, %d minute", "%s, %d minutes", mins), | |
2961 ret, mins); | |
2962 g_free(ret); | |
2963 ret = tmp; | |
2964 } | |
2965 else | |
2966 ret = g_strdup_printf(ngettext("%d minute", "%d minutes", mins), mins); | |
2967 } | |
2968 | |
2969 return ret; | |
2970 } | |
2971 | |
2972 | |
2973 char * | |
2974 gaim_str_binary_to_ascii(const unsigned char *binary, guint len) | |
2975 { | |
2976 GString *ret; | |
2977 guint i; | |
2978 | |
2979 g_return_val_if_fail(len > 0, NULL); | |
2980 | |
2981 ret = g_string_sized_new(len); | |
2982 | |
2983 for (i = 0; i < len; i++) | |
2984 if (binary[i] < 32 || binary[i] > 126) | |
2985 g_string_append_printf(ret, "\\x%02hhx", binary[i]); | |
2986 else if (binary[i] == '\\') | |
2987 g_string_append(ret, "\\\\"); | |
2988 else | |
2989 g_string_append_c(ret, binary[i]); | |
2990 | |
2991 return g_string_free(ret, FALSE); | |
2992 } | |
2993 | |
2994 /************************************************************************** | |
2995 * URI/URL Functions | |
2996 **************************************************************************/ | |
2997 gboolean | |
2998 gaim_url_parse(const char *url, char **ret_host, int *ret_port, | |
2999 char **ret_path, char **ret_user, char **ret_passwd) | |
3000 { | |
3001 char scan_info[255]; | |
3002 char port_str[6]; | |
3003 int f; | |
3004 const char *at, *slash; | |
3005 const char *turl; | |
3006 char host[256], path[256], user[256], passwd[256]; | |
3007 int port = 0; | |
3008 /* hyphen at end includes it in control set */ | |
3009 static char addr_ctrl[] = "A-Za-z0-9.-"; | |
3010 static char port_ctrl[] = "0-9"; | |
3011 static char page_ctrl[] = "A-Za-z0-9.~_/:*!@&%%?=+^-"; | |
3012 static char user_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; | |
3013 static char passwd_ctrl[] = "A-Za-z0-9.~_/*!&%%?=+^-"; | |
3014 | |
3015 g_return_val_if_fail(url != NULL, FALSE); | |
3016 | |
3017 if ((turl = strstr(url, "http://")) != NULL || | |
3018 (turl = strstr(url, "HTTP://")) != NULL) | |
3019 { | |
3020 turl += 7; | |
3021 url = turl; | |
3022 } | |
3023 | |
3024 /* parse out authentication information if supplied */ | |
3025 /* Only care about @ char BEFORE the first / */ | |
3026 at = strchr(url, '@'); | |
3027 slash = strchr(url, '/'); | |
3028 if ((at != NULL) && | |
3029 (((slash != NULL) && (strlen(at) > strlen(slash))) || | |
3030 (slash == NULL))) { | |
3031 g_snprintf(scan_info, sizeof(scan_info), | |
3032 "%%255[%s]:%%255[%s]^@", user_ctrl, passwd_ctrl); | |
3033 f = sscanf(url, scan_info, user, passwd); | |
3034 | |
3035 if (f ==1 ) { | |
3036 /* No passwd, possibly just username supplied */ | |
3037 g_snprintf(scan_info, sizeof(scan_info), | |
3038 "%%255[%s]^@", user_ctrl); | |
3039 f = sscanf(url, scan_info, user); | |
3040 *passwd = '\0'; | |
3041 } | |
3042 | |
3043 url = at+1; /* move pointer after the @ char */ | |
3044 } else { | |
3045 *user = '\0'; | |
3046 *passwd = '\0'; | |
3047 } | |
3048 | |
3049 g_snprintf(scan_info, sizeof(scan_info), | |
3050 "%%255[%s]:%%5[%s]/%%255[%s]", addr_ctrl, port_ctrl, page_ctrl); | |
3051 | |
3052 f = sscanf(url, scan_info, host, port_str, path); | |
3053 | |
3054 if (f == 1) | |
3055 { | |
3056 g_snprintf(scan_info, sizeof(scan_info), | |
3057 "%%255[%s]/%%255[%s]", | |
3058 addr_ctrl, page_ctrl); | |
3059 f = sscanf(url, scan_info, host, path); | |
3060 g_snprintf(port_str, sizeof(port_str), "80"); | |
3061 } | |
3062 | |
3063 if (f == 1) | |
3064 *path = '\0'; | |
3065 | |
3066 sscanf(port_str, "%d", &port); | |
3067 | |
3068 if (ret_host != NULL) *ret_host = g_strdup(host); | |
3069 if (ret_port != NULL) *ret_port = port; | |
3070 if (ret_path != NULL) *ret_path = g_strdup(path); | |
3071 if (ret_user != NULL) *ret_user = g_strdup(user); | |
3072 if (ret_passwd != NULL) *ret_passwd = g_strdup(passwd); | |
3073 | |
3074 return TRUE; | |
3075 } | |
3076 | |
14354 | 3077 /** |
3078 * The arguments to this function are similar to printf. | |
3079 */ | |
14192 | 3080 static void |
14354 | 3081 gaim_util_fetch_url_error(GaimUtilFetchUrlData *gfud, const char *format, ...) |
14192 | 3082 { |
14354 | 3083 gchar *error_message; |
3084 va_list args; | |
3085 | |
3086 va_start(args, format); | |
3087 error_message = g_strdup_vprintf(format, args); | |
3088 va_end(args); | |
3089 | |
3090 gfud->callback(gfud, gfud->user_data, NULL, 0, error_message); | |
3091 g_free(error_message); | |
3092 gaim_util_fetch_url_cancel(gfud); | |
14192 | 3093 } |
3094 | |
14403 | 3095 static void url_fetch_connect_cb(gpointer url_data, gint source, const gchar *error_message); |
3096 | |
14192 | 3097 static gboolean |
3098 parse_redirect(const char *data, size_t data_len, gint sock, | |
14354 | 3099 GaimUtilFetchUrlData *gfud) |
14192 | 3100 { |
3101 gchar *s; | |
3102 | |
3103 if ((s = g_strstr_len(data, data_len, "Location: ")) != NULL) | |
3104 { | |
3105 gchar *new_url, *temp_url, *end; | |
3106 gboolean full; | |
3107 int len; | |
3108 | |
3109 s += strlen("Location: "); | |
3110 end = strchr(s, '\r'); | |
3111 | |
3112 /* Just in case :) */ | |
3113 if (end == NULL) | |
3114 end = strchr(s, '\n'); | |
3115 | |
3116 if (end == NULL) | |
3117 return FALSE; | |
3118 | |
3119 len = end - s; | |
3120 | |
3121 new_url = g_malloc(len + 1); | |
3122 strncpy(new_url, s, len); | |
3123 new_url[len] = '\0'; | |
3124 | |
3125 full = gfud->full; | |
3126 | |
3127 if (*new_url == '/' || g_strstr_len(new_url, len, "://") == NULL) | |
3128 { | |
3129 temp_url = new_url; | |
3130 | |
3131 new_url = g_strdup_printf("%s:%d%s", gfud->website.address, | |
3132 gfud->website.port, temp_url); | |
3133 | |
3134 g_free(temp_url); | |
3135 | |
3136 full = FALSE; | |
3137 } | |
3138 | |
14354 | 3139 gaim_debug_info("util", "Redirecting to %s\n", new_url); |
14192 | 3140 |
14403 | 3141 /* |
3142 * Try again, with this new location. This code is somewhat | |
3143 * ugly, but we need to reuse the gfud because whoever called | |
3144 * us is holding a reference to it. | |
3145 */ | |
3146 g_free(gfud->url); | |
3147 gfud->url = new_url; | |
3148 gfud->full = full; | |
3149 g_free(gfud->request); | |
3150 gfud->request = NULL; | |
3151 | |
3152 g_free(gfud->website.user); | |
3153 g_free(gfud->website.passwd); | |
3154 g_free(gfud->website.address); | |
3155 g_free(gfud->website.page); | |
3156 gaim_url_parse(new_url, &gfud->website.address, &gfud->website.port, | |
3157 &gfud->website.page, &gfud->website.user, &gfud->website.passwd); | |
3158 | |
14837 | 3159 gfud->connect_data = gaim_proxy_connect(NULL, NULL, |
14403 | 3160 gfud->website.address, gfud->website.port, |
3161 url_fetch_connect_cb, gfud); | |
3162 | |
3163 if (gfud->connect_data == NULL) | |
3164 { | |
3165 gaim_util_fetch_url_error(gfud, _("Unable to connect to %s"), | |
3166 gfud->website.address); | |
3167 } | |
14192 | 3168 |
3169 return TRUE; | |
3170 } | |
3171 | |
3172 return FALSE; | |
3173 } | |
3174 | |
3175 static size_t | |
3176 parse_content_len(const char *data, size_t data_len) | |
3177 { | |
3178 size_t content_len = 0; | |
3179 const char *p = NULL; | |
3180 | |
3181 /* This is still technically wrong, since headers are case-insensitive | |
3182 * [RFC 2616, section 4.2], though this ought to catch the normal case. | |
3183 * Note: data is _not_ nul-terminated. | |
3184 */ | |
3185 if(data_len > 16) { | |
3186 p = (strncmp(data, "Content-Length: ", 16) == 0) ? data : NULL; | |
3187 if(!p) | |
3188 p = (strncmp(data, "CONTENT-LENGTH: ", 16) == 0) | |
3189 ? data : NULL; | |
3190 if(!p) { | |
3191 p = g_strstr_len(data, data_len, "\nContent-Length: "); | |
3192 if (p) | |
3193 p++; | |
3194 } | |
3195 if(!p) { | |
3196 p = g_strstr_len(data, data_len, "\nCONTENT-LENGTH: "); | |
3197 if (p) | |
3198 p++; | |
3199 } | |
3200 | |
3201 if(p) | |
3202 p += 16; | |
3203 } | |
3204 | |
3205 /* If we can find a Content-Length header at all, try to sscanf it. | |
3206 * Response headers should end with at least \r\n, so sscanf is safe, | |
3207 * if we make sure that there is indeed a \n in our header. | |
3208 */ | |
3209 if (p && g_strstr_len(p, data_len - (p - data), "\n")) { | |
3210 sscanf(p, "%" G_GSIZE_FORMAT, &content_len); | |
14354 | 3211 gaim_debug_misc("util", "parsed %u\n", content_len); |
14192 | 3212 } |
3213 | |
3214 return content_len; | |
3215 } | |
3216 | |
3217 | |
3218 static void | |
3219 url_fetch_recv_cb(gpointer url_data, gint source, GaimInputCondition cond) | |
3220 { | |
14354 | 3221 GaimUtilFetchUrlData *gfud = url_data; |
14192 | 3222 int len; |
3223 char buf[4096]; | |
3224 char *data_cursor; | |
3225 gboolean got_eof = FALSE; | |
3226 | |
3227 while((len = read(source, buf, sizeof(buf))) > 0) { | |
14354 | 3228 /* If we've filled up our buffer, make it bigger */ |
14192 | 3229 if((gfud->len + len) >= gfud->data_len) { |
3230 while((gfud->len + len) >= gfud->data_len) | |
3231 gfud->data_len += sizeof(buf); | |
3232 | |
3233 gfud->webdata = g_realloc(gfud->webdata, gfud->data_len); | |
3234 } | |
3235 | |
3236 data_cursor = gfud->webdata + gfud->len; | |
3237 | |
3238 gfud->len += len; | |
3239 | |
3240 memcpy(data_cursor, buf, len); | |
3241 | |
3242 gfud->webdata[gfud->len] = '\0'; | |
3243 | |
3244 if(!gfud->got_headers) { | |
3245 char *tmp; | |
3246 | |
14354 | 3247 /* See if we've reached the end of the headers yet */ |
14192 | 3248 if((tmp = strstr(gfud->webdata, "\r\n\r\n"))) { |
3249 char * new_data; | |
3250 guint header_len = (tmp + 4 - gfud->webdata); | |
3251 size_t content_len; | |
3252 | |
14354 | 3253 gaim_debug_misc("util", "Response headers: '%.*s'\n", |
14192 | 3254 header_len, gfud->webdata); |
3255 | |
3256 /* See if we can find a redirect. */ | |
3257 if(parse_redirect(gfud->webdata, header_len, source, gfud)) | |
3258 return; | |
3259 | |
3260 gfud->got_headers = TRUE; | |
3261 | |
3262 /* No redirect. See if we can find a content length. */ | |
3263 content_len = parse_content_len(gfud->webdata, header_len); | |
3264 | |
3265 if(content_len == 0) { | |
3266 /* We'll stick with an initial 8192 */ | |
3267 content_len = 8192; | |
3268 } else { | |
3269 gfud->has_explicit_data_len = TRUE; | |
3270 } | |
3271 | |
3272 | |
3273 /* If we're returning the headers too, we don't need to clean them out */ | |
3274 if(gfud->include_headers) { | |
3275 gfud->data_len = content_len + header_len; | |
3276 gfud->webdata = g_realloc(gfud->webdata, gfud->data_len); | |
3277 } else { | |
3278 size_t body_len = 0; | |
3279 | |
3280 if(gfud->len > (header_len + 1)) | |
3281 body_len = (gfud->len - header_len); | |
3282 | |
3283 content_len = MAX(content_len, body_len); | |
3284 | |
3285 new_data = g_try_malloc(content_len); | |
3286 if(new_data == NULL) { | |
14354 | 3287 gaim_debug_error("util", |
3288 "Failed to allocate %u bytes: %s\n", | |
3289 content_len, strerror(errno)); | |
3290 gaim_util_fetch_url_error(gfud, | |
3291 _("Unable to allocate enough memory to hold " | |
3292 "the contents from %s. The web server may " | |
3293 "be trying something malicious."), | |
3294 gfud->website.address); | |
14192 | 3295 |
3296 return; | |
3297 } | |
3298 | |
3299 /* We may have read part of the body when reading the headers, don't lose it */ | |
3300 if(body_len > 0) { | |
3301 tmp += 4; | |
3302 memcpy(new_data, tmp, body_len); | |
3303 } | |
3304 | |
3305 /* Out with the old... */ | |
3306 g_free(gfud->webdata); | |
3307 | |
3308 /* In with the new. */ | |
3309 gfud->len = body_len; | |
3310 gfud->data_len = content_len; | |
3311 gfud->webdata = new_data; | |
3312 } | |
3313 } | |
3314 } | |
3315 | |
3316 if(gfud->has_explicit_data_len && gfud->len >= gfud->data_len) { | |
3317 got_eof = TRUE; | |
3318 break; | |
3319 } | |
3320 } | |
3321 | |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3322 if(len < 0) { |
14192 | 3323 if(errno == EAGAIN) { |
3324 return; | |
3325 } else { | |
14354 | 3326 gaim_util_fetch_url_error(gfud, _("Error reading from %s: %s"), |
3327 gfud->website.address, strerror(errno)); | |
14192 | 3328 return; |
3329 } | |
3330 } | |
3331 | |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3332 if((len == 0) || got_eof) { |
14192 | 3333 gfud->webdata = g_realloc(gfud->webdata, gfud->len + 1); |
3334 gfud->webdata[gfud->len] = '\0'; | |
3335 | |
14354 | 3336 gfud->callback(gfud, gfud->user_data, gfud->webdata, gfud->len, NULL); |
3337 gaim_util_fetch_url_cancel(gfud); | |
14192 | 3338 } |
3339 } | |
3340 | |
3341 static void | |
3342 url_fetch_send_cb(gpointer data, gint source, GaimInputCondition cond) | |
3343 { | |
14354 | 3344 GaimUtilFetchUrlData *gfud; |
14192 | 3345 int len, total_len; |
3346 | |
3347 gfud = data; | |
3348 | |
3349 total_len = strlen(gfud->request); | |
3350 | |
14354 | 3351 len = write(gfud->fd, gfud->request + gfud->request_written, |
14192 | 3352 total_len - gfud->request_written); |
3353 | |
14354 | 3354 if (len < 0 && errno == EAGAIN) |
14192 | 3355 return; |
14354 | 3356 else if (len < 0) { |
3357 gaim_util_fetch_url_error(gfud, _("Error writing to %s: %s"), | |
3358 gfud->website.address, strerror(errno)); | |
14192 | 3359 return; |
3360 } | |
3361 gfud->request_written += len; | |
3362 | |
14354 | 3363 if (gfud->request_written != total_len) |
14192 | 3364 return; |
3365 | |
14354 | 3366 /* We're done writing our request, now start reading the response */ |
14192 | 3367 gaim_input_remove(gfud->inpa); |
14354 | 3368 gfud->inpa = gaim_input_add(gfud->fd, GAIM_INPUT_READ, url_fetch_recv_cb, |
14192 | 3369 gfud); |
3370 } | |
3371 | |
3372 static void | |
3373 url_fetch_connect_cb(gpointer url_data, gint source, const gchar *error_message) | |
3374 { | |
14354 | 3375 GaimUtilFetchUrlData *gfud; |
14192 | 3376 |
3377 gfud = url_data; | |
14354 | 3378 gfud->connect_data = NULL; |
14192 | 3379 |
3380 if (source == -1) | |
3381 { | |
14354 | 3382 gaim_util_fetch_url_error(gfud, _("Unable to connect to %s: %s"), |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3383 (gfud->website.address ? gfud->website.address : ""), error_message); |
14192 | 3384 return; |
3385 } | |
3386 | |
14354 | 3387 gfud->fd = source; |
3388 | |
14192 | 3389 if (!gfud->request) |
3390 { | |
3391 if (gfud->user_agent) { | |
3392 /* Host header is not forbidden in HTTP/1.0 requests, and HTTP/1.1 | |
3393 * clients must know how to handle the "chunked" transfer encoding. | |
3394 * Gaim doesn't know how to handle "chunked", so should always send | |
3395 * the Host header regardless, to get around some observed problems | |
3396 */ | |
3397 gfud->request = g_strdup_printf( | |
3398 "GET %s%s HTTP/%s\r\n" | |
3399 "Connection: close\r\n" | |
3400 "User-Agent: %s\r\n" | |
3401 "Accept: */*\r\n" | |
3402 "Host: %s\r\n\r\n", | |
3403 (gfud->full ? "" : "/"), | |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3404 (gfud->full ? (gfud->url ? gfud->url : "") : (gfud->website.page ? gfud->website.page : "")), |
14192 | 3405 (gfud->http11 ? "1.1" : "1.0"), |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3406 (gfud->user_agent ? gfud->user_agent : ""), |
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3407 (gfud->website.address ? gfud->website.address : "")); |
14192 | 3408 } else { |
3409 gfud->request = g_strdup_printf( | |
3410 "GET %s%s HTTP/%s\r\n" | |
3411 "Connection: close\r\n" | |
3412 "Accept: */*\r\n" | |
3413 "Host: %s\r\n\r\n", | |
3414 (gfud->full ? "" : "/"), | |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3415 (gfud->full ? (gfud->url ? gfud->url : "") : (gfud->website.page ? gfud->website.page : "")), |
14192 | 3416 (gfud->http11 ? "1.1" : "1.0"), |
14960
0c80979077cf
[gaim-migrate @ 17738]
Evan Schoenberg <evan.s@dreskin.net>
parents:
14955
diff
changeset
|
3417 (gfud->website.address ? gfud->website.address : "")); |
14192 | 3418 } |
3419 } | |
3420 | |
14354 | 3421 gaim_debug_misc("util", "Request: '%s'\n", gfud->request); |
14192 | 3422 |
3423 gfud->inpa = gaim_input_add(source, GAIM_INPUT_WRITE, | |
3424 url_fetch_send_cb, gfud); | |
3425 url_fetch_send_cb(gfud, source, GAIM_INPUT_WRITE); | |
3426 } | |
3427 | |
14354 | 3428 GaimUtilFetchUrlData * |
3429 gaim_util_fetch_url_request(const char *url, gboolean full, | |
14192 | 3430 const char *user_agent, gboolean http11, |
3431 const char *request, gboolean include_headers, | |
14354 | 3432 GaimUtilFetchUrlCallback callback, void *user_data) |
14192 | 3433 { |
14354 | 3434 GaimUtilFetchUrlData *gfud; |
3435 | |
3436 g_return_val_if_fail(url != NULL, NULL); | |
3437 g_return_val_if_fail(callback != NULL, NULL); | |
3438 | |
3439 gaim_debug_info("util", | |
14192 | 3440 "requested to fetch (%s), full=%d, user_agent=(%s), http11=%d\n", |
3441 url, full, user_agent?user_agent:"(null)", http11); | |
3442 | |
14354 | 3443 gfud = g_new0(GaimUtilFetchUrlData, 1); |
3444 | |
3445 gfud->callback = callback; | |
14192 | 3446 gfud->user_data = user_data; |
3447 gfud->url = g_strdup(url); | |
3448 gfud->user_agent = g_strdup(user_agent); | |
3449 gfud->http11 = http11; | |
3450 gfud->full = full; | |
3451 gfud->request = g_strdup(request); | |
3452 gfud->include_headers = include_headers; | |
3453 | |
3454 gaim_url_parse(url, &gfud->website.address, &gfud->website.port, | |
3455 &gfud->website.page, &gfud->website.user, &gfud->website.passwd); | |
3456 | |
14837 | 3457 gfud->connect_data = gaim_proxy_connect(NULL, NULL, |
14354 | 3458 gfud->website.address, gfud->website.port, |
3459 url_fetch_connect_cb, gfud); | |
3460 | |
3461 if (gfud->connect_data == NULL) | |
14192 | 3462 { |
14354 | 3463 gaim_util_fetch_url_error(gfud, _("Unable to connect to %s"), |
3464 gfud->website.address); | |
3465 return NULL; | |
14192 | 3466 } |
14354 | 3467 |
3468 return gfud; | |
3469 } | |
3470 | |
3471 void | |
3472 gaim_util_fetch_url_cancel(GaimUtilFetchUrlData *gfud) | |
3473 { | |
3474 if (gfud->connect_data != NULL) | |
3475 gaim_proxy_connect_cancel(gfud->connect_data); | |
3476 | |
3477 if (gfud->inpa > 0) | |
3478 gaim_input_remove(gfud->inpa); | |
3479 | |
3480 if (gfud->fd >= 0) | |
3481 close(gfud->fd); | |
3482 | |
3483 g_free(gfud->website.user); | |
3484 g_free(gfud->website.passwd); | |
3485 g_free(gfud->website.address); | |
3486 g_free(gfud->website.page); | |
3487 g_free(gfud->url); | |
3488 g_free(gfud->user_agent); | |
3489 g_free(gfud->request); | |
3490 g_free(gfud->webdata); | |
3491 | |
3492 g_free(gfud); | |
14192 | 3493 } |
3494 | |
3495 const char * | |
3496 gaim_url_decode(const char *str) | |
3497 { | |
3498 static char buf[BUF_LEN]; | |
3499 guint i, j = 0; | |
3500 char *bum; | |
3501 char hex[3]; | |
3502 | |
3503 g_return_val_if_fail(str != NULL, NULL); | |
3504 | |
3505 /* | |
3506 * XXX - This check could be removed and buf could be made | |
3507 * dynamically allocated, but this is easier. | |
3508 */ | |
3509 if (strlen(str) >= BUF_LEN) | |
3510 return NULL; | |
3511 | |
3512 for (i = 0; i < strlen(str); i++) { | |
3513 | |
3514 if (str[i] != '%') | |
3515 buf[j++] = str[i]; | |
3516 else { | |
3517 strncpy(hex, str + ++i, 2); | |
3518 hex[2] = '\0'; | |
3519 | |
3520 /* i is pointing to the start of the number */ | |
3521 i++; | |
3522 | |
3523 /* | |
3524 * Now it's at the end and at the start of the for loop | |
3525 * will be at the next character. | |
3526 */ | |
3527 buf[j++] = strtol(hex, NULL, 16); | |
3528 } | |
3529 } | |
3530 | |
3531 buf[j] = '\0'; | |
3532 | |
3533 if (!g_utf8_validate(buf, -1, (const char **)&bum)) | |
3534 *bum = '\0'; | |
3535 | |
3536 return buf; | |
3537 } | |
3538 | |
3539 const char * | |
3540 gaim_url_encode(const char *str) | |
3541 { | |
3542 const char *iter; | |
3543 static char buf[BUF_LEN]; | |
3544 char utf_char[6]; | |
3545 guint i, j = 0; | |
3546 | |
3547 g_return_val_if_fail(str != NULL, NULL); | |
3548 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); | |
3549 | |
3550 iter = str; | |
3551 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { | |
3552 gunichar c = g_utf8_get_char(iter); | |
3553 /* If the character is an ASCII character and is alphanumeric | |
3554 * no need to escape */ | |
3555 if (c < 128 && isalnum(c)) { | |
3556 buf[j++] = c; | |
3557 } else { | |
3558 int bytes = g_unichar_to_utf8(c, utf_char); | |
3559 for (i = 0; i < bytes; i++) { | |
3560 if (j > (BUF_LEN - 4)) | |
3561 break; | |
3562 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); | |
3563 j += 3; | |
3564 } | |
3565 } | |
3566 } | |
3567 | |
3568 buf[j] = '\0'; | |
3569 | |
3570 return buf; | |
3571 } | |
3572 | |
3573 /* Originally lifted from | |
3574 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html | |
3575 * ... and slightly modified to be a bit more rfc822 compliant | |
3576 * ... and modified a bit more to make domain checking rfc1035 compliant | |
3577 * with the exception permitted in rfc1101 for domains to start with digit | |
3578 * but not completely checking to avoid conflicts with IP addresses | |
3579 */ | |
3580 gboolean | |
3581 gaim_email_is_valid(const char *address) | |
3582 { | |
3583 const char *c, *domain; | |
3584 static char *rfc822_specials = "()<>@,;:\\\"[]"; | |
3585 | |
3586 /* first we validate the name portion (name@domain) (rfc822)*/ | |
3587 for (c = address; *c; c++) { | |
3588 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) { | |
3589 while (*++c) { | |
3590 if (*c == '\\') { | |
3591 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue; | |
3592 else return FALSE; | |
3593 } | |
3594 if (*c == '\"') break; | |
3595 if (*c < ' ' || *c >= 127) return FALSE; | |
3596 } | |
3597 if (!*c++) return FALSE; | |
3598 if (*c == '@') break; | |
3599 if (*c != '.') return FALSE; | |
3600 continue; | |
3601 } | |
3602 if (*c == '@') break; | |
3603 if (*c <= ' ' || *c >= 127) return FALSE; | |
3604 if (strchr(rfc822_specials, *c)) return FALSE; | |
3605 } | |
3606 /* strictly we should return false if (*(c - 1) == '.') too, but I think | |
3607 * we should permit user.@domain type addresses - they do work :) */ | |
3608 if (c == address) return FALSE; | |
3609 | |
3610 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */ | |
3611 if (!*(domain = ++c)) return FALSE; | |
3612 do { | |
3613 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-')) | |
3614 return FALSE; | |
3615 if (*c == '-' && *(c - 1) == '.') return FALSE; | |
3616 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') || | |
3617 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE; | |
3618 } while (*++c); | |
3619 | |
3620 if (*(c - 1) == '-') return FALSE; | |
3621 | |
3622 return ((c - domain) > 3 ? TRUE : FALSE); | |
3623 } | |
3624 | |
3625 /* Stolen from gnome_uri_list_extract_uris */ | |
3626 GList * | |
3627 gaim_uri_list_extract_uris(const gchar *uri_list) | |
3628 { | |
3629 const gchar *p, *q; | |
3630 gchar *retval; | |
3631 GList *result = NULL; | |
3632 | |
3633 g_return_val_if_fail (uri_list != NULL, NULL); | |
3634 | |
3635 p = uri_list; | |
3636 | |
3637 /* We don't actually try to validate the URI according to RFC | |
3638 * 2396, or even check for allowed characters - we just ignore | |
3639 * comments and trim whitespace off the ends. We also | |
3640 * allow LF delimination as well as the specified CRLF. | |
3641 */ | |
3642 while (p) { | |
3643 if (*p != '#') { | |
3644 while (isspace(*p)) | |
3645 p++; | |
3646 | |
3647 q = p; | |
3648 while (*q && (*q != '\n') && (*q != '\r')) | |
3649 q++; | |
3650 | |
3651 if (q > p) { | |
3652 q--; | |
3653 while (q > p && isspace(*q)) | |
3654 q--; | |
3655 | |
3656 retval = (gchar*)g_malloc (q - p + 2); | |
3657 strncpy (retval, p, q - p + 1); | |
3658 retval[q - p + 1] = '\0'; | |
3659 | |
3660 result = g_list_prepend (result, retval); | |
3661 } | |
3662 } | |
3663 p = strchr (p, '\n'); | |
3664 if (p) | |
3665 p++; | |
3666 } | |
3667 | |
3668 return g_list_reverse (result); | |
3669 } | |
3670 | |
3671 | |
3672 /* Stolen from gnome_uri_list_extract_filenames */ | |
3673 GList * | |
3674 gaim_uri_list_extract_filenames(const gchar *uri_list) | |
3675 { | |
3676 GList *tmp_list, *node, *result; | |
3677 | |
3678 g_return_val_if_fail (uri_list != NULL, NULL); | |
3679 | |
3680 result = gaim_uri_list_extract_uris(uri_list); | |
3681 | |
3682 tmp_list = result; | |
3683 while (tmp_list) { | |
3684 gchar *s = (gchar*)tmp_list->data; | |
3685 | |
3686 node = tmp_list; | |
3687 tmp_list = tmp_list->next; | |
3688 | |
3689 if (!strncmp (s, "file:", 5)) { | |
3690 node->data = g_filename_from_uri (s, NULL, NULL); | |
3691 /* not sure if this fallback is useful at all */ | |
3692 if (!node->data) node->data = g_strdup (s+5); | |
3693 } else { | |
3694 result = g_list_remove_link(result, node); | |
3695 g_list_free_1 (node); | |
3696 } | |
3697 g_free (s); | |
3698 } | |
3699 return result; | |
3700 } | |
3701 | |
3702 /************************************************************************** | |
3703 * UTF8 String Functions | |
3704 **************************************************************************/ | |
3705 gchar * | |
3706 gaim_utf8_try_convert(const char *str) | |
3707 { | |
3708 gsize converted; | |
3709 gchar *utf8; | |
3710 | |
3711 g_return_val_if_fail(str != NULL, NULL); | |
3712 | |
3713 if (g_utf8_validate(str, -1, NULL)) { | |
3714 return g_strdup(str); | |
3715 } | |
3716 | |
3717 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL); | |
3718 if (utf8 != NULL) | |
3719 return utf8; | |
3720 | |
3721 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL); | |
3722 if ((utf8 != NULL) && (converted == strlen(str))) | |
3723 return utf8; | |
3724 | |
3725 g_free(utf8); | |
3726 | |
3727 return NULL; | |
3728 } | |
3729 | |
3730 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \ | |
3731 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf) | |
3732 gchar * | |
3733 gaim_utf8_salvage(const char *str) | |
3734 { | |
3735 GString *workstr; | |
3736 const char *end; | |
3737 | |
3738 g_return_val_if_fail(str != NULL, NULL); | |
3739 | |
3740 workstr = g_string_sized_new(strlen(str)); | |
3741 | |
3742 do { | |
3743 g_utf8_validate(str, -1, &end); | |
3744 workstr = g_string_append_len(workstr, str, end - str); | |
3745 str = end; | |
3746 if (*str == '\0') | |
3747 break; | |
3748 do { | |
3749 workstr = g_string_append_c(workstr, '?'); | |
3750 str++; | |
3751 } while (!utf8_first(*str)); | |
3752 } while (*str != '\0'); | |
3753 | |
3754 return g_string_free(workstr, FALSE); | |
3755 } | |
3756 | |
3757 | |
3758 char * | |
3759 gaim_utf8_ncr_encode(const char *str) | |
3760 { | |
3761 GString *out; | |
3762 | |
3763 g_return_val_if_fail(str != NULL, NULL); | |
3764 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); | |
3765 | |
3766 out = g_string_new(""); | |
3767 | |
3768 for(; *str; str = g_utf8_next_char(str)) { | |
3769 gunichar wc = g_utf8_get_char(str); | |
3770 | |
3771 /* super simple check. hopefully not too wrong. */ | |
3772 if(wc >= 0x80) { | |
3773 g_string_append_printf(out, "&#%u;", (guint32) wc); | |
3774 } else { | |
3775 g_string_append_unichar(out, wc); | |
3776 } | |
3777 } | |
3778 | |
3779 return g_string_free(out, FALSE); | |
3780 } | |
3781 | |
3782 | |
3783 char * | |
3784 gaim_utf8_ncr_decode(const char *str) | |
3785 { | |
3786 GString *out; | |
3787 char *buf, *b; | |
3788 | |
3789 g_return_val_if_fail(str != NULL, NULL); | |
3790 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); | |
3791 | |
3792 buf = (char *) str; | |
3793 out = g_string_new(""); | |
3794 | |
3795 while( (b = strstr(buf, "&#")) ) { | |
3796 gunichar wc; | |
3797 int base = 0; | |
3798 | |
3799 /* append everything leading up to the &# */ | |
3800 g_string_append_len(out, buf, b-buf); | |
3801 | |
3802 b += 2; /* skip past the &# */ | |
3803 | |
3804 /* strtoul will treat 0x prefix as hex, but not just x */ | |
3805 if(*b == 'x' || *b == 'X') { | |
3806 base = 16; | |
3807 b++; | |
3808 } | |
3809 | |
3810 /* advances buf to the end of the ncr segment */ | |
3811 wc = (gunichar) strtoul(b, &buf, base); | |
3812 | |
3813 /* this mimics the previous impl of ncr_decode */ | |
3814 if(*buf == ';') { | |
3815 g_string_append_unichar(out, wc); | |
3816 buf++; | |
3817 } | |
3818 } | |
3819 | |
3820 /* append whatever's left */ | |
3821 g_string_append(out, buf); | |
3822 | |
3823 return g_string_free(out, FALSE); | |
3824 } | |
3825 | |
3826 | |
3827 int | |
3828 gaim_utf8_strcasecmp(const char *a, const char *b) | |
3829 { | |
3830 char *a_norm = NULL; | |
3831 char *b_norm = NULL; | |
3832 int ret = -1; | |
3833 | |
3834 if(!a && b) | |
3835 return -1; | |
3836 else if(!b && a) | |
3837 return 1; | |
3838 else if(!a && !b) | |
3839 return 0; | |
3840 | |
3841 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL)) | |
3842 { | |
3843 gaim_debug_error("gaim_utf8_strcasecmp", | |
3844 "One or both parameters are invalid UTF8\n"); | |
3845 return ret; | |
3846 } | |
3847 | |
3848 a_norm = g_utf8_casefold(a, -1); | |
3849 b_norm = g_utf8_casefold(b, -1); | |
3850 ret = g_utf8_collate(a_norm, b_norm); | |
3851 g_free(a_norm); | |
3852 g_free(b_norm); | |
3853 | |
3854 return ret; | |
3855 } | |
3856 | |
3857 /* previously conversation::find_nick() */ | |
3858 gboolean | |
3859 gaim_utf8_has_word(const char *haystack, const char *needle) | |
3860 { | |
3861 char *hay, *pin, *p; | |
3862 int n; | |
3863 gboolean ret = FALSE; | |
3864 | |
3865 hay = g_utf8_strdown(haystack, -1); | |
3866 | |
3867 pin = g_utf8_strdown(needle, -1); | |
3868 n = strlen(pin); | |
3869 | |
3870 if ((p = strstr(hay, pin)) != NULL) { | |
3871 if ((p == hay || !isalnum(*(p - 1))) && !isalnum(*(p + n))) { | |
3872 ret = TRUE; | |
3873 } | |
3874 } | |
3875 | |
3876 g_free(pin); | |
3877 g_free(hay); | |
3878 | |
3879 return ret; | |
3880 } | |
3881 | |
3882 void | |
3883 gaim_print_utf8_to_console(FILE *filestream, char *message) | |
3884 { | |
3885 gchar *message_conv; | |
3886 GError *error = NULL; | |
3887 | |
3888 /* Try to convert 'message' to user's locale */ | |
3889 message_conv = g_locale_from_utf8(message, -1, NULL, NULL, &error); | |
3890 if (message_conv != NULL) { | |
3891 fputs(message_conv, filestream); | |
3892 g_free(message_conv); | |
3893 } | |
3894 else | |
3895 { | |
3896 /* use 'message' as a fallback */ | |
3897 g_warning("%s\n", error->message); | |
3898 g_error_free(error); | |
3899 fputs(message, filestream); | |
3900 } | |
3901 } | |
3902 | |
3903 gboolean gaim_message_meify(char *message, size_t len) | |
3904 { | |
3905 char *c; | |
3906 gboolean inside_html = FALSE; | |
3907 | |
3908 g_return_val_if_fail(message != NULL, FALSE); | |
3909 | |
3910 if(len == -1) | |
3911 len = strlen(message); | |
3912 | |
3913 for (c = message; *c; c++, len--) { | |
3914 if(inside_html) { | |
3915 if(*c == '>') | |
3916 inside_html = FALSE; | |
3917 } else { | |
3918 if(*c == '<') | |
3919 inside_html = TRUE; | |
3920 else | |
3921 break; | |
3922 } | |
3923 } | |
3924 | |
3925 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) { | |
3926 memmove(c, c+4, len-3); | |
3927 return TRUE; | |
3928 } | |
3929 | |
3930 return FALSE; | |
3931 } | |
3932 | |
3933 char *gaim_text_strip_mnemonic(const char *in) | |
3934 { | |
3935 char *out; | |
3936 char *a; | |
3937 char *a0; | |
3938 const char *b; | |
3939 | |
3940 g_return_val_if_fail(in != NULL, NULL); | |
3941 | |
3942 out = g_malloc(strlen(in)+1); | |
3943 a = out; | |
3944 b = in; | |
3945 | |
3946 a0 = a; /* The last non-space char seen so far, or the first char */ | |
3947 | |
3948 while(*b) { | |
3949 if(*b == '_') { | |
3950 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') { | |
3951 /* Detected CJK style shortcut (Bug 875311) */ | |
3952 a = a0; /* undo the left parenthesis */ | |
3953 b += 3; /* and skip the whole mess */ | |
3954 } else if(*(b+1) == '_') { | |
3955 *(a++) = '_'; | |
3956 b += 2; | |
3957 a0 = a; | |
3958 } else { | |
3959 b++; | |
3960 } | |
3961 /* We don't want to corrupt the middle of UTF-8 characters */ | |
3962 } else if (!(*b & 0x80)) { /* other 1-byte char */ | |
3963 if (*b != ' ') | |
3964 a0 = a; | |
3965 *(a++) = *(b++); | |
3966 } else { | |
3967 /* Multibyte utf8 char, don't look for _ inside these */ | |
3968 int n = 0; | |
3969 int i; | |
3970 if ((*b & 0xe0) == 0xc0) { | |
3971 n = 2; | |
3972 } else if ((*b & 0xf0) == 0xe0) { | |
3973 n = 3; | |
3974 } else if ((*b & 0xf8) == 0xf0) { | |
3975 n = 4; | |
3976 } else if ((*b & 0xfc) == 0xf8) { | |
3977 n = 5; | |
3978 } else if ((*b & 0xfe) == 0xfc) { | |
3979 n = 6; | |
3980 } else { /* Illegal utf8 */ | |
3981 n = 1; | |
3982 } | |
3983 a0 = a; /* unless we want to delete CJK spaces too */ | |
3984 for (i = 0; i < n && *b; i += 1) { | |
3985 *(a++) = *(b++); | |
3986 } | |
3987 } | |
3988 } | |
3989 *a = '\0'; | |
3990 | |
3991 return out; | |
3992 } | |
3993 | |
3994 const char* gaim_unescape_filename(const char *escaped) { | |
3995 return gaim_url_decode(escaped); | |
3996 } | |
3997 | |
3998 | |
3999 /* this is almost identical to gaim_url_encode (hence gaim_url_decode | |
4000 * being used above), but we want to keep certain characters unescaped | |
4001 * for compat reasons */ | |
4002 const char * | |
4003 gaim_escape_filename(const char *str) | |
4004 { | |
4005 const char *iter; | |
4006 static char buf[BUF_LEN]; | |
4007 char utf_char[6]; | |
4008 guint i, j = 0; | |
4009 | |
4010 g_return_val_if_fail(str != NULL, NULL); | |
4011 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); | |
4012 | |
4013 iter = str; | |
4014 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { | |
4015 gunichar c = g_utf8_get_char(iter); | |
4016 /* If the character is an ASCII character and is alphanumeric, | |
4017 * or one of the specified values, no need to escape */ | |
4018 if (c < 128 && (isalnum(c) || c == '@' || c == '-' || | |
4019 c == '_' || c == '.' || c == '#')) { | |
4020 buf[j++] = c; | |
4021 } else { | |
4022 int bytes = g_unichar_to_utf8(c, utf_char); | |
4023 for (i = 0; i < bytes; i++) { | |
4024 if (j > (BUF_LEN - 4)) | |
4025 break; | |
4026 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); | |
4027 j += 3; | |
4028 } | |
4029 } | |
4030 } | |
4031 | |
4032 buf[j] = '\0'; | |
4033 | |
4034 return buf; | |
4035 } | |
4036 |