Add purple_util_write_data_to_*_file declarations
[pidgin-git.git] / libpurple / util.c
blobc358899bb3624c679987e2c22d8fcd5e95206d7b
1 /* Purple is the legal property of its developers, whose names are too numerous
2 * to list here. Please refer to the COPYRIGHT file distributed with this
3 * source distribution.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 #include "internal.h"
21 #include "ciphers/md5hash.h"
22 #include "conversation.h"
23 #include "core.h"
24 #include "debug.h"
25 #include "glibcompat.h"
26 #include "notify.h"
27 #include "protocol.h"
28 #include "prefs.h"
29 #include "util.h"
31 #include <json-glib/json-glib.h>
33 struct _PurpleMenuAction
35 char *label;
36 PurpleCallback callback;
37 gpointer data;
38 GList *children;
39 gchar *stock_icon;
42 static char *custom_user_dir = NULL;
43 static char *user_dir = NULL;
44 static char *cache_dir = NULL;
45 static char *config_dir = NULL;
46 static char *data_dir = NULL;
48 static JsonNode *escape_js_node = NULL;
49 static JsonGenerator *escape_js_gen = NULL;
51 static void
52 move_to_xdg_base_dir(const char *purple_xdg_dir, char *subdir)
54 char *xdg_dir;
55 gboolean xdg_dir_exists;
57 xdg_dir_exists = g_file_test(purple_xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
58 if (!xdg_dir_exists) {
59 gint mkdir_res;
61 mkdir_res = g_mkdir_with_parents(purple_xdg_dir, (S_IRUSR | S_IWUSR | S_IXUSR));
62 if (mkdir_res == -1) {
63 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
64 purple_xdg_dir, g_strerror(errno));
65 return;
69 xdg_dir = g_build_filename(purple_xdg_dir, subdir, NULL);
70 xdg_dir_exists = g_file_test(xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
71 if (!xdg_dir_exists) {
72 char *old_dir;
73 gboolean old_dir_exists;
75 old_dir = g_build_filename(purple_user_dir(), subdir, NULL);
76 old_dir_exists = g_file_test(old_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
78 if (old_dir_exists) {
79 g_rename(old_dir, xdg_dir);
82 g_free(old_dir);
83 old_dir = NULL;
86 g_free(xdg_dir);
87 xdg_dir = NULL;
89 return;
92 /* If legacy directory for libpurple exists, move it to location following
93 * xdg base dir spec. https://developer.pidgin.im/ticket/10029
95 static void
96 migrate_to_xdg_base_dirs(void)
98 const char *legacy_purple_dir;
99 gboolean dir_exists;
101 legacy_purple_dir = purple_user_dir();
102 dir_exists = g_file_test(legacy_purple_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
103 if (dir_exists) {
104 move_to_xdg_base_dir(purple_data_dir(), "certificates");
105 move_to_xdg_base_dir(purple_cache_dir(), "icons");
106 move_to_xdg_base_dir(purple_data_dir(), "logs");
109 return;
112 PurpleMenuAction *
113 purple_menu_action_new(const char *label, PurpleCallback callback, gpointer data,
114 GList *children)
116 PurpleMenuAction *act = g_new0(PurpleMenuAction, 1);
117 act->label = g_strdup(label);
118 act->callback = callback;
119 act->data = data;
120 act->children = children;
121 return act;
124 void
125 purple_menu_action_free(PurpleMenuAction *act)
127 g_return_if_fail(act != NULL);
129 g_free(act->stock_icon);
130 g_free(act->label);
131 g_free(act);
134 char * purple_menu_action_get_label(const PurpleMenuAction *act)
136 g_return_val_if_fail(act != NULL, NULL);
138 return act->label;
141 PurpleCallback purple_menu_action_get_callback(const PurpleMenuAction *act)
143 g_return_val_if_fail(act != NULL, NULL);
145 return act->callback;
148 gpointer purple_menu_action_get_data(const PurpleMenuAction *act)
150 g_return_val_if_fail(act != NULL, NULL);
152 return act->data;
155 GList* purple_menu_action_get_children(const PurpleMenuAction *act)
157 g_return_val_if_fail(act != NULL, NULL);
159 return act->children;
162 void purple_menu_action_set_label(PurpleMenuAction *act, char *label)
164 g_return_if_fail(act != NULL);
166 act-> label = label;
169 void purple_menu_action_set_callback(PurpleMenuAction *act, PurpleCallback callback)
171 g_return_if_fail(act != NULL);
173 act->callback = callback;
176 void purple_menu_action_set_data(PurpleMenuAction *act, gpointer data)
178 g_return_if_fail(act != NULL);
180 act->data = data;
183 void purple_menu_action_set_children(PurpleMenuAction *act, GList *children)
185 g_return_if_fail(act != NULL);
187 act->children = children;
190 void purple_menu_action_set_stock_icon(PurpleMenuAction *act,
191 const gchar *stock)
193 g_return_if_fail(act != NULL);
195 g_free(act->stock_icon);
196 act->stock_icon = g_strdup(stock);
199 const gchar *
200 purple_menu_action_get_stock_icon(PurpleMenuAction *act)
202 return act->stock_icon;
205 void
206 purple_util_init(void)
208 escape_js_node = json_node_new(JSON_NODE_VALUE);
209 escape_js_gen = json_generator_new();
210 json_node_set_boolean(escape_js_node, FALSE);
212 if (custom_user_dir == NULL) {
213 migrate_to_xdg_base_dirs();
217 void
218 purple_util_uninit(void)
220 /* Free these so we don't have leaks at shutdown. */
222 g_free(custom_user_dir);
223 custom_user_dir = NULL;
225 g_free(user_dir);
226 user_dir = NULL;
228 g_free(cache_dir);
229 cache_dir = NULL;
231 g_free(config_dir);
232 config_dir = NULL;
234 g_free(data_dir);
235 data_dir = NULL;
237 json_node_free(escape_js_node);
238 escape_js_node = NULL;
240 g_object_unref(escape_js_gen);
241 escape_js_gen = NULL;
244 /**************************************************************************
245 * Base16 Functions
246 **************************************************************************/
247 gchar *
248 purple_base16_encode(const guchar *data, gsize len)
250 gsize i;
251 gchar *ascii = NULL;
253 g_return_val_if_fail(data != NULL, NULL);
254 g_return_val_if_fail(len > 0, NULL);
256 ascii = g_malloc(len * 2 + 1);
258 for (i = 0; i < len; i++)
259 g_snprintf(&ascii[i * 2], 3, "%02x", data[i] & 0xFF);
261 return ascii;
264 guchar *
265 purple_base16_decode(const char *str, gsize *ret_len)
267 gsize len, i, accumulator = 0;
268 guchar *data;
270 g_return_val_if_fail(str != NULL, NULL);
272 len = strlen(str);
274 g_return_val_if_fail(*str, 0);
275 g_return_val_if_fail(len % 2 == 0, 0);
277 data = g_malloc(len / 2);
279 for (i = 0; i < len; i++)
281 if ((i % 2) == 0)
282 accumulator = 0;
283 else
284 accumulator <<= 4;
286 if (isdigit(str[i]))
287 accumulator |= str[i] - 48;
288 else
290 switch(tolower(str[i]))
292 case 'a': accumulator |= 10; break;
293 case 'b': accumulator |= 11; break;
294 case 'c': accumulator |= 12; break;
295 case 'd': accumulator |= 13; break;
296 case 'e': accumulator |= 14; break;
297 case 'f': accumulator |= 15; break;
301 if (i % 2)
302 data[(i - 1) / 2] = accumulator;
305 if (ret_len != NULL)
306 *ret_len = len / 2;
308 return data;
311 gchar *
312 purple_base16_encode_chunked(const guchar *data, gsize len)
314 gsize i;
315 gchar *ascii = NULL;
317 g_return_val_if_fail(data != NULL, NULL);
318 g_return_val_if_fail(len > 0, NULL);
320 /* For each byte of input, we need 2 bytes for the hex representation
321 * and 1 for the colon.
322 * The final colon will be replaced by a terminating NULL
324 ascii = g_malloc(len * 3 + 1);
326 for (i = 0; i < len; i++)
327 g_snprintf(&ascii[i * 3], 4, "%02x:", data[i] & 0xFF);
329 /* Replace the final colon with NULL */
330 ascii[len * 3 - 1] = 0;
332 return ascii;
336 /**************************************************************************
337 * Base64 Functions
338 **************************************************************************/
339 static const char xdigits[] =
340 "0123456789abcdef";
342 gchar *
343 purple_base64_encode(const guchar *data, gsize len)
345 return g_base64_encode(data, len);
348 guchar *
349 purple_base64_decode(const char *str, gsize *ret_len)
352 * We want to allow ret_len to be NULL for backward compatibility,
353 * but g_base64_decode() requires a valid length variable. So if
354 * ret_len is NULL then pass in a dummy variable.
356 gsize unused;
357 return g_base64_decode(str, ret_len != NULL ? ret_len : &unused);
360 /**************************************************************************
361 * Quoted Printable Functions (see RFC 2045).
362 **************************************************************************/
363 guchar *
364 purple_quotedp_decode(const char *str, gsize *ret_len)
366 char *n, *new;
367 const char *end, *p;
369 n = new = g_malloc(strlen (str) + 1);
370 end = str + strlen(str);
372 for (p = str; p < end; p++, n++) {
373 if (*p == '=') {
374 if (p[1] == '\r' && p[2] == '\n') { /* 5.1 #5 */
375 n -= 1;
376 p += 2;
377 } else if (p[1] == '\n') { /* fuzzy case for 5.1 #5 */
378 n -= 1;
379 p += 1;
380 } else if (p[1] && p[2]) {
381 char *nibble1 = strchr(xdigits, tolower(p[1]));
382 char *nibble2 = strchr(xdigits, tolower(p[2]));
383 if (nibble1 && nibble2) { /* 5.1 #1 */
384 *n = ((nibble1 - xdigits) << 4) | (nibble2 - xdigits);
385 p += 2;
386 } else { /* This should never happen */
387 *n = *p;
389 } else { /* This should never happen */
390 *n = *p;
393 else if (*p == '_')
394 *n = ' ';
395 else
396 *n = *p;
399 *n = '\0';
401 if (ret_len != NULL)
402 *ret_len = n - new;
404 /* Resize to take less space */
405 /* new = realloc(new, n - new); */
407 return (guchar *)new;
410 /**************************************************************************
411 * MIME Functions
412 **************************************************************************/
413 char *
414 purple_mime_decode_field(const char *str)
417 * This is wing's version, partially based on revo/shx's version
418 * See RFC2047 [which apparently obsoletes RFC1342]
420 typedef enum {
421 state_start, state_equal1, state_question1,
422 state_charset, state_question2,
423 state_encoding, state_question3,
424 state_encoded_text, state_question4, state_equal2 = state_start
425 } encoded_word_state_t;
426 encoded_word_state_t state = state_start;
427 const char *cur, *mark;
428 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL;
429 GString *new;
431 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
432 #define token_char_p(c) \
433 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
435 /* But encoded-text must be ASCII; alas, isascii() may not exist */
436 #define encoded_text_char_p(c) \
437 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
439 g_return_val_if_fail(str != NULL, NULL);
441 new = g_string_new(NULL);
443 /* Here we will be looking for encoded words and if they seem to be
444 * valid then decode them.
445 * They are of this form: =?charset?encoding?text?=
448 for (cur = str, mark = NULL; *cur; cur += 1) {
449 switch (state) {
450 case state_equal1:
451 if (*cur == '?') {
452 state = state_question1;
453 } else {
454 g_string_append_len(new, mark, cur - mark + 1);
455 state = state_start;
457 break;
458 case state_question1:
459 if (token_char_p(*cur)) {
460 charset0 = cur;
461 state = state_charset;
462 } else { /* This should never happen */
463 g_string_append_len(new, mark, cur - mark + 1);
464 state = state_start;
466 break;
467 case state_charset:
468 if (*cur == '?') {
469 state = state_question2;
470 } else if (!token_char_p(*cur)) { /* This should never happen */
471 g_string_append_len(new, mark, cur - mark + 1);
472 state = state_start;
474 break;
475 case state_question2:
476 if (token_char_p(*cur)) {
477 encoding0 = cur;
478 state = state_encoding;
479 } else { /* This should never happen */
480 g_string_append_len(new, mark, cur - mark + 1);
481 state = state_start;
483 break;
484 case state_encoding:
485 if (*cur == '?') {
486 state = state_question3;
487 } else if (!token_char_p(*cur)) { /* This should never happen */
488 g_string_append_len(new, mark, cur - mark + 1);
489 state = state_start;
491 break;
492 case state_question3:
493 if (encoded_text_char_p(*cur)) {
494 encoded_text0 = cur;
495 state = state_encoded_text;
496 } else if (*cur == '?') { /* empty string */
497 encoded_text0 = cur;
498 state = state_question4;
499 } else { /* This should never happen */
500 g_string_append_len(new, mark, cur - mark + 1);
501 state = state_start;
503 break;
504 case state_encoded_text:
505 if (*cur == '?') {
506 state = state_question4;
507 } else if (!encoded_text_char_p(*cur)) {
508 g_string_append_len(new, mark, cur - mark + 1);
509 state = state_start;
511 break;
512 case state_question4:
513 if (*cur == '=') { /* Got the whole encoded-word */
514 char *charset = g_strndup(charset0, encoding0 - charset0 - 1);
515 char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1);
516 char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1);
517 guchar *decoded = NULL;
518 gsize dec_len;
519 if (g_ascii_strcasecmp(encoding, "Q") == 0)
520 decoded = purple_quotedp_decode(encoded_text, &dec_len);
521 else if (g_ascii_strcasecmp(encoding, "B") == 0)
522 decoded = purple_base64_decode(encoded_text, &dec_len);
523 else
524 decoded = NULL;
525 if (decoded) {
526 gsize len;
527 char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL);
529 if (converted) {
530 g_string_append_len(new, converted, len);
531 g_free(converted);
533 g_free(decoded);
535 g_free(charset);
536 g_free(encoding);
537 g_free(encoded_text);
538 state = state_equal2; /* Restart the FSM */
539 } else { /* This should never happen */
540 g_string_append_len(new, mark, cur - mark + 1);
541 state = state_start;
543 break;
544 default:
545 if (*cur == '=') {
546 mark = cur;
547 state = state_equal1;
548 } else {
549 /* Some unencoded text. */
550 g_string_append_c(new, *cur);
552 break;
553 } /* switch */
554 } /* for */
556 if (state != state_start)
557 g_string_append_len(new, mark, cur - mark + 1);
559 return g_string_free(new, FALSE);;
563 /**************************************************************************
564 * Date/Time Functions
565 **************************************************************************/
567 const char *purple_get_tzoff_str(const struct tm *tm, gboolean iso)
569 static char buf[7];
570 long off;
571 gint8 min;
572 gint8 hrs;
573 struct tm new_tm = *tm;
575 mktime(&new_tm);
577 if (new_tm.tm_isdst < 0)
578 g_return_val_if_reached("");
580 #ifdef _WIN32
581 if ((off = wpurple_get_tz_offset()) == -1)
582 return "";
583 #elif defined(HAVE_TM_GMTOFF)
584 off = new_tm.tm_gmtoff;
585 #elif defined(HAVE_TIMEZONE)
586 tzset();
587 off = -1 * timezone;
588 #else
589 purple_debug_warning("util",
590 "there is no possibility to obtain tz offset");
591 return "";
592 #endif
594 min = (off / 60) % 60;
595 hrs = ((off / 60) - min) / 60;
597 if(iso) {
598 if (0 == off) {
599 strcpy(buf, "Z");
600 } else {
601 /* please leave the colons...they're optional for iso, but jabber
602 * wants them */
603 if(g_snprintf(buf, sizeof(buf), "%+03d:%02d", hrs, ABS(min)) > 6)
604 g_return_val_if_reached("");
606 } else {
607 if (g_snprintf(buf, sizeof(buf), "%+03d%02d", hrs, ABS(min)) > 5)
608 g_return_val_if_reached("");
611 return buf;
614 /* Windows doesn't HAVE_STRFTIME_Z_FORMAT, but this seems clearer. -- rlaager */
615 #if !defined(HAVE_STRFTIME_Z_FORMAT) || defined(_WIN32)
616 static size_t purple_internal_strftime(char *s, size_t max, const char *format, const struct tm *tm)
618 const char *start;
619 const char *c;
620 char *fmt = NULL;
622 /* Yes, this is checked in purple_utf8_strftime(),
623 * but better safe than sorry. -- rlaager */
624 g_return_val_if_fail(format != NULL, 0);
626 /* This is fairly efficient, and it only gets
627 * executed on Windows or if the underlying
628 * system doesn't support the %z format string,
629 * for strftime() so I think it's good enough.
630 * -- rlaager */
631 for (c = start = format; *c ; c++)
633 if (*c != '%')
634 continue;
636 c++;
638 #ifndef HAVE_STRFTIME_Z_FORMAT
639 if (*c == 'z')
641 char *tmp = g_strdup_printf("%s%.*s%s",
642 fmt ? fmt : "",
643 (int)(c - start - 1),
644 start,
645 purple_get_tzoff_str(tm, FALSE));
646 g_free(fmt);
647 fmt = tmp;
648 start = c + 1;
650 #endif
651 #ifdef _WIN32
652 if (*c == 'Z')
654 char *tmp = g_strdup_printf("%s%.*s%s",
655 fmt ? fmt : "",
656 (int)(c - start - 1),
657 start,
658 wpurple_get_timezone_abbreviation(tm));
659 g_free(fmt);
660 fmt = tmp;
661 start = c + 1;
663 #endif
666 if (fmt != NULL)
668 size_t ret;
670 if (*start)
672 char *tmp = g_strconcat(fmt, start, NULL);
673 g_free(fmt);
674 fmt = tmp;
677 ret = strftime(s, max, fmt, tm);
678 g_free(fmt);
680 return ret;
683 return strftime(s, max, format, tm);
685 #else /* HAVE_STRFTIME_Z_FORMAT && !_WIN32 */
686 #define purple_internal_strftime strftime
687 #endif
689 const char *
690 purple_utf8_strftime(const char *format, const struct tm *tm)
692 static char buf[128];
693 char *locale;
694 GError *err = NULL;
695 int len;
696 char *utf8;
698 g_return_val_if_fail(format != NULL, NULL);
700 if (tm == NULL)
702 time_t now = time(NULL);
703 tm = localtime(&now);
706 locale = g_locale_from_utf8(format, -1, NULL, NULL, &err);
707 if (err != NULL)
709 purple_debug_error("util", "Format conversion failed in purple_utf8_strftime(): %s\n", err->message);
710 g_error_free(err);
711 err = NULL;
712 locale = g_strdup(format);
715 /* A return value of 0 is either an error (in
716 * which case, the contents of the buffer are
717 * undefined) or the empty string (in which
718 * case, no harm is done here). */
719 if ((len = purple_internal_strftime(buf, sizeof(buf), locale, tm)) == 0)
721 g_free(locale);
722 return "";
725 g_free(locale);
727 utf8 = g_locale_to_utf8(buf, len, NULL, NULL, &err);
728 if (err != NULL)
730 purple_debug_error("util", "Result conversion failed in purple_utf8_strftime(): %s\n", err->message);
731 g_error_free(err);
733 else
735 g_strlcpy(buf, utf8, sizeof(buf));
736 g_free(utf8);
739 return buf;
742 const char *
743 purple_date_format_short(const struct tm *tm)
745 return purple_utf8_strftime("%x", tm);
748 const char *
749 purple_date_format_long(const struct tm *tm)
752 * This string determines how some dates are displayed. The default
753 * string "%x %X" shows the date then the time. Translators can
754 * change this to "%X %x" if they want the time to be shown first,
755 * followed by the date.
757 return purple_utf8_strftime(_("%x %X"), tm);
760 const char *
761 purple_date_format_full(const struct tm *tm)
763 return purple_utf8_strftime("%c", tm);
766 const char *
767 purple_time_format(const struct tm *tm)
769 return purple_utf8_strftime("%X", tm);
772 time_t
773 purple_time_build(int year, int month, int day, int hour, int min, int sec)
775 struct tm tm;
777 tm.tm_year = year - 1900;
778 tm.tm_mon = month - 1;
779 tm.tm_mday = day;
780 tm.tm_hour = hour;
781 tm.tm_min = min;
782 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60;
784 return mktime(&tm);
787 /* originally taken from GLib trunk 1-6-11 */
788 /* originally licensed as LGPL 2+ */
789 static time_t
790 mktime_utc(struct tm *tm)
792 time_t retval;
794 #ifndef HAVE_TIMEGM
795 static const gint days_before[] =
797 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
799 #endif
801 #ifndef HAVE_TIMEGM
802 if (tm->tm_mon < 0 || tm->tm_mon > 11)
803 return (time_t) -1;
805 retval = (tm->tm_year - 70) * 365;
806 retval += (tm->tm_year - 68) / 4;
807 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
809 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
810 retval -= 1;
812 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
813 #else
814 retval = timegm (tm);
815 #endif /* !HAVE_TIMEGM */
817 return retval;
820 time_t
821 purple_str_to_time(const char *timestamp, gboolean utc,
822 struct tm *tm, long *tz_off, const char **rest)
824 struct tm t;
825 const gchar *str;
826 gint year = 0;
827 long tzoff = PURPLE_NO_TZ_OFF;
828 time_t retval;
829 gboolean mktime_with_utc = FALSE;
831 if (rest != NULL)
832 *rest = NULL;
834 g_return_val_if_fail(timestamp != NULL, 0);
836 memset(&t, 0, sizeof(struct tm));
838 str = timestamp;
840 /* Strip leading whitespace */
841 while (g_ascii_isspace(*str))
842 str++;
844 if (*str == '\0') {
845 if (rest != NULL && *str != '\0')
846 *rest = str;
848 return 0;
851 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
852 if (rest != NULL && *str != '\0')
853 *rest = str;
855 return 0;
858 /* 4 digit year */
859 if (sscanf(str, "%04d", &year) && year >= 1900) {
860 str += 4;
862 if (*str == '-' || *str == '/')
863 str++;
865 t.tm_year = year - 1900;
868 /* 2 digit month */
869 if (!sscanf(str, "%02d", &t.tm_mon)) {
870 if (rest != NULL && *str != '\0')
871 *rest = str;
873 return 0;
876 str += 2;
877 t.tm_mon -= 1;
879 if (*str == '-' || *str == '/')
880 str++;
882 /* 2 digit day */
883 if (!sscanf(str, "%02d", &t.tm_mday)) {
884 if (rest != NULL && *str != '\0')
885 *rest = str;
887 return 0;
890 str += 2;
892 /* Grab the year off the end if there's still stuff */
893 if (*str == '/' || *str == '-') {
894 /* But make sure we don't read the year twice */
895 if (year >= 1900) {
896 if (rest != NULL && *str != '\0')
897 *rest = str;
899 return 0;
902 str++;
904 if (!sscanf(str, "%04d", &t.tm_year)) {
905 if (rest != NULL && *str != '\0')
906 *rest = str;
908 return 0;
911 t.tm_year -= 1900;
912 } else if (*str == 'T' || *str == '.') {
913 str++;
915 /* Continue grabbing the hours/minutes/seconds */
916 if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
917 (str += 8)) ||
918 (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
919 (str += 6)))
921 gint sign, tzhrs, tzmins;
923 if (*str == '.') {
924 /* Cut off those pesky micro-seconds */
925 do {
926 str++;
927 } while (*str >= '0' && *str <= '9');
930 sign = (*str == '+') ? 1 : -1;
932 /* Process the timezone */
933 if (*str == '+' || *str == '-') {
934 str++;
936 if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) ||
937 (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4))))
939 mktime_with_utc = TRUE;
940 tzoff = tzhrs * 60 * 60 + tzmins * 60;
941 tzoff *= sign;
943 } else if (*str == 'Z') {
944 /* 'Z' = Zulu = UTC */
945 str++;
946 mktime_with_utc = TRUE;
947 tzoff = 0;
950 if (!mktime_with_utc)
952 /* No timezone specified. */
954 if (utc) {
955 mktime_with_utc = TRUE;
956 tzoff = 0;
957 } else {
958 /* Local Time */
959 t.tm_isdst = -1;
965 if (rest != NULL && *str != '\0') {
966 /* Strip trailing whitespace */
967 while (g_ascii_isspace(*str))
968 str++;
970 if (*str != '\0')
971 *rest = str;
974 if (mktime_with_utc)
975 retval = mktime_utc(&t);
976 else
977 retval = mktime(&t);
979 if (tm != NULL)
980 *tm = t;
982 if (tzoff != PURPLE_NO_TZ_OFF)
983 retval -= tzoff;
985 if (tz_off != NULL)
986 *tz_off = tzoff;
988 return retval;
991 char *
992 purple_uts35_to_str(const char *format, size_t len, struct tm *tm)
994 GString *string;
995 guint i, count;
997 if (tm == NULL) {
998 time_t now = time(NULL);
999 tm = localtime(&now);
1002 string = g_string_sized_new(len);
1003 i = 0;
1004 while (i < len) {
1005 count = 1;
1006 while ((i + count) < len && format[i] == format[i+count])
1007 count++;
1009 switch (format[i]) {
1010 /* Era Designator */
1011 case 'G':
1012 if (count <= 3) {
1013 /* Abbreviated */
1014 } else if (count == 4) {
1015 /* Full */
1016 } else if (count >= 5) {
1017 /* Narrow */
1018 count = 5;
1020 break;
1023 /* Year */
1024 case 'y':
1025 if (count == 2) {
1026 /* Two-digits only */
1027 g_string_append(string, purple_utf8_strftime("%y", tm));
1028 } else {
1029 /* Zero-padding */
1030 char *tmp = g_strdup_printf("%%0%dY", count);
1031 g_string_append(string, purple_utf8_strftime(tmp, tm));
1032 g_free(tmp);
1034 break;
1036 /* Year (in "Week of Year" based calendars) */
1037 case 'Y':
1038 if (count == 2) {
1039 /* Two-digits only */
1040 } else {
1041 /* Zero-padding */
1043 break;
1045 /* Extended Year */
1046 case 'u':
1047 break;
1049 /* Cyclic Year Name */
1050 case 'U':
1051 if (count <= 3) {
1052 /* Abbreviated */
1053 } else if (count == 4) {
1054 /* Full */
1055 } else if (count >= 5) {
1056 /* Narrow */
1057 count = 5;
1059 break;
1062 /* Quarter */
1063 case 'Q':
1064 if (count <= 2) {
1065 /* Numerical */
1066 } else if (count == 3) {
1067 /* Abbreviation */
1068 } else if (count >= 4) {
1069 /* Full */
1070 count = 4;
1072 break;
1074 /* Stand-alone Quarter */
1075 case 'q':
1076 if (count <= 2) {
1077 /* Numerical */
1078 } else if (count == 3) {
1079 /* Abbreviation */
1080 } else if (count >= 4) {
1081 /* Full */
1082 count = 4;
1084 break;
1086 /* Month */
1087 case 'M':
1088 if (count <= 2) {
1089 /* Numerical */
1090 g_string_append(string, purple_utf8_strftime("%m", tm));
1091 } else if (count == 3) {
1092 /* Abbreviation */
1093 g_string_append(string, purple_utf8_strftime("%b", tm));
1094 } else if (count == 4) {
1095 /* Full */
1096 g_string_append(string, purple_utf8_strftime("%B", tm));
1097 } else if (count >= 5) {
1098 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
1099 count = 5;
1101 break;
1103 /* Stand-alone Month */
1104 case 'L':
1105 if (count <= 2) {
1106 /* Numerical */
1107 g_string_append(string, purple_utf8_strftime("%m", tm));
1108 } else if (count == 3) {
1109 /* Abbreviation */
1110 g_string_append(string, purple_utf8_strftime("%b", tm));
1111 } else if (count == 4) {
1112 /* Full */
1113 g_string_append(string, purple_utf8_strftime("%B", tm));
1114 } else if (count >= 5) {
1115 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
1116 count = 5;
1118 break;
1120 /* Ignored */
1121 case 'l':
1122 break;
1125 /* Week of Year */
1126 case 'w':
1127 g_string_append(string, purple_utf8_strftime("%W", tm));
1128 count = MIN(count, 2);
1129 break;
1131 /* Week of Month */
1132 case 'W':
1133 count = 1;
1134 break;
1137 /* Day of Month */
1138 case 'd':
1139 g_string_append(string, purple_utf8_strftime("%d", tm));
1140 count = MIN(count, 2);
1141 break;
1143 /* Day of Year */
1144 case 'D':
1145 g_string_append(string, purple_utf8_strftime("%j", tm));
1146 count = MIN(count, 3);
1147 break;
1149 /* Day of Year in Month */
1150 case 'F':
1151 count = 1;
1152 break;
1154 /* Modified Julian Day */
1155 case 'g':
1156 break;
1159 /* Day of Week */
1160 case 'E':
1161 if (count <= 3) {
1162 /* Short */
1163 g_string_append(string, purple_utf8_strftime("%a", tm));
1164 } else if (count == 4) {
1165 /* Full */
1166 g_string_append(string, purple_utf8_strftime("%A", tm));
1167 } else if (count >= 5) {
1168 /* Narrow */
1169 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1170 count = 5;
1172 break;
1174 /* Local Day of Week */
1175 case 'e':
1176 if (count <= 2) {
1177 /* Numeric */
1178 g_string_append(string, purple_utf8_strftime("%u", tm));
1179 } else if (count == 3) {
1180 /* Short */
1181 g_string_append(string, purple_utf8_strftime("%a", tm));
1182 } else if (count == 4) {
1183 /* Full */
1184 g_string_append(string, purple_utf8_strftime("%A", tm));
1185 } else if (count >= 5) {
1186 /* Narrow */
1187 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1188 count = 5;
1190 break;
1192 /* Stand-alone Local Day of Week */
1193 case 'c':
1194 if (count <= 2) {
1195 /* Numeric */
1196 g_string_append(string, purple_utf8_strftime("%u", tm));
1197 count = 1;
1198 } else if (count == 3) {
1199 /* Short */
1200 g_string_append(string, purple_utf8_strftime("%a", tm));
1201 } else if (count == 4) {
1202 /* Full */
1203 g_string_append(string, purple_utf8_strftime("%A", tm));
1204 } else if (count >= 5) {
1205 /* Narrow */
1206 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1207 count = 5;
1209 break;
1212 /* AM/PM */
1213 case 'a':
1214 g_string_append(string, purple_utf8_strftime("%p", tm));
1215 break;
1218 /* Hour (1-12) */
1219 case 'h':
1220 if (count == 1) {
1221 /* No padding */
1222 g_string_append(string, purple_utf8_strftime("%I", tm));
1223 } else if (count >= 2) {
1224 /* Zero-padded */
1225 g_string_append(string, purple_utf8_strftime("%I", tm));
1226 count = 2;
1228 break;
1230 /* Hour (0-23) */
1231 case 'H':
1232 if (count == 1) {
1233 /* No padding */
1234 g_string_append(string, purple_utf8_strftime("%H", tm));
1235 } else if (count >= 2) {
1236 /* Zero-padded */
1237 g_string_append(string, purple_utf8_strftime("%H", tm));
1238 count = 2;
1240 break;
1242 /* Hour (0-11) */
1243 case 'K':
1244 if (count == 1) {
1245 /* No padding */
1246 } else if (count >= 2) {
1247 /* Zero-padded */
1248 count = 2;
1250 break;
1252 /* Hour (1-24) */
1253 case 'k':
1254 if (count == 1) {
1255 /* No padding */
1256 } else if (count >= 2) {
1257 /* Zero-padded */
1258 count = 2;
1260 break;
1262 /* Hour (hHkK by locale) */
1263 case 'j':
1264 break;
1267 /* Minute */
1268 case 'm':
1269 g_string_append(string, purple_utf8_strftime("%M", tm));
1270 count = MIN(count, 2);
1271 break;
1274 /* Second */
1275 case 's':
1276 g_string_append(string, purple_utf8_strftime("%S", tm));
1277 count = MIN(count, 2);
1278 break;
1280 /* Fractional Sub-second */
1281 case 'S':
1282 break;
1284 /* Millisecond */
1285 case 'A':
1286 break;
1289 /* Time Zone (specific non-location format) */
1290 case 'z':
1291 if (count <= 3) {
1292 /* Short */
1293 } else if (count >= 4) {
1294 /* Full */
1295 count = 4;
1297 break;
1299 /* Time Zone */
1300 case 'Z':
1301 if (count <= 3) {
1302 /* RFC822 */
1303 g_string_append(string, purple_utf8_strftime("%z", tm));
1304 } else if (count == 4) {
1305 /* Localized GMT */
1306 } else if (count >= 5) {
1307 /* ISO8601 */
1308 g_string_append(string, purple_utf8_strftime("%z", tm));
1309 count = 5;
1311 break;
1313 /* Time Zone (generic non-location format) */
1314 case 'v':
1315 if (count <= 3) {
1316 /* Short */
1317 g_string_append(string, purple_utf8_strftime("%Z", tm));
1318 count = 1;
1319 } else if (count >= 4) {
1320 /* Long */
1321 g_string_append(string, purple_utf8_strftime("%Z", tm));
1322 count = 4;
1324 break;
1326 /* Time Zone */
1327 case 'V':
1328 if (count <= 3) {
1329 /* Same as z */
1330 count = 1;
1331 } else if (count >= 4) {
1332 /* Generic Location Format) */
1333 g_string_append(string, purple_utf8_strftime("%Z", tm));
1334 count = 4;
1336 break;
1339 default:
1340 g_string_append_len(string, format + i, count);
1341 break;
1344 i += count;
1347 return g_string_free(string, FALSE);
1350 /**************************************************************************
1351 * Markup Functions
1352 **************************************************************************/
1355 * This function is stolen from glib's gmarkup.c and modified to not
1356 * replace ' with &apos;
1358 static void append_escaped_text(GString *str,
1359 const gchar *text, gssize length)
1361 const gchar *p;
1362 const gchar *end;
1363 gunichar c;
1365 p = text;
1366 end = text + length;
1368 while (p != end)
1370 const gchar *next;
1371 next = g_utf8_next_char (p);
1373 switch (*p)
1375 case '&':
1376 g_string_append (str, "&amp;");
1377 break;
1379 case '<':
1380 g_string_append (str, "&lt;");
1381 break;
1383 case '>':
1384 g_string_append (str, "&gt;");
1385 break;
1387 case '"':
1388 g_string_append (str, "&quot;");
1389 break;
1391 default:
1392 c = g_utf8_get_char (p);
1393 if ((0x1 <= c && c <= 0x8) ||
1394 (0xb <= c && c <= 0xc) ||
1395 (0xe <= c && c <= 0x1f) ||
1396 (0x7f <= c && c <= 0x84) ||
1397 (0x86 <= c && c <= 0x9f))
1398 g_string_append_printf (str, "&#x%x;", c);
1399 else
1400 g_string_append_len (str, p, next - p);
1401 break;
1404 p = next;
1408 /* This function is stolen from glib's gmarkup.c */
1409 gchar *purple_markup_escape_text(const gchar *text, gssize length)
1411 GString *str;
1413 g_return_val_if_fail(text != NULL, NULL);
1415 if (length < 0)
1416 length = strlen(text);
1418 /* prealloc at least as long as original text */
1419 str = g_string_sized_new(length);
1420 append_escaped_text(str, text, length);
1422 return g_string_free(str, FALSE);
1425 const char *
1426 purple_markup_unescape_entity(const char *text, int *length)
1428 const char *pln;
1429 int len, pound;
1430 char temp[2];
1432 if (!text || *text != '&')
1433 return NULL;
1435 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1437 if(IS_ENTITY("&amp;"))
1438 pln = "&";
1439 else if(IS_ENTITY("&lt;"))
1440 pln = "<";
1441 else if(IS_ENTITY("&gt;"))
1442 pln = ">";
1443 else if(IS_ENTITY("&nbsp;"))
1444 pln = " ";
1445 else if(IS_ENTITY("&copy;"))
1446 pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1447 else if(IS_ENTITY("&quot;"))
1448 pln = "\"";
1449 else if(IS_ENTITY("&reg;"))
1450 pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1451 else if(IS_ENTITY("&apos;"))
1452 pln = "\'";
1453 else if(*(text+1) == '#' &&
1454 (sscanf(text, "&#%u%1[;]", &pound, temp) == 2 ||
1455 sscanf(text, "&#x%x%1[;]", &pound, temp) == 2) &&
1456 pound != 0) {
1457 static char buf[7];
1458 int buflen = g_unichar_to_utf8((gunichar)pound, buf);
1459 buf[buflen] = '\0';
1460 pln = buf;
1462 len = (*(text+2) == 'x' ? 3 : 2);
1463 while(isxdigit((gint) text[len])) len++;
1464 if(text[len] == ';') len++;
1466 else
1467 return NULL;
1469 if (length)
1470 *length = len;
1471 return pln;
1474 char *
1475 purple_markup_get_css_property(const gchar *style,
1476 const gchar *opt)
1478 const gchar *css_str = style;
1479 const gchar *css_value_start;
1480 const gchar *css_value_end;
1481 gchar *tmp;
1482 gchar *ret;
1484 g_return_val_if_fail(opt != NULL, NULL);
1486 if (!css_str)
1487 return NULL;
1489 /* find the CSS property */
1490 while (1)
1492 /* skip whitespace characters */
1493 while (*css_str && g_ascii_isspace(*css_str))
1494 css_str++;
1495 if (!g_ascii_isalpha(*css_str))
1496 return NULL;
1497 if (g_ascii_strncasecmp(css_str, opt, strlen(opt)))
1499 /* go to next css property positioned after the next ';' */
1500 while (*css_str && *css_str != '"' && *css_str != ';')
1501 css_str++;
1502 if(*css_str != ';')
1503 return NULL;
1504 css_str++;
1506 else
1507 break;
1510 /* find the CSS value position in the string */
1511 css_str += strlen(opt);
1512 while (*css_str && g_ascii_isspace(*css_str))
1513 css_str++;
1514 if (*css_str != ':')
1515 return NULL;
1516 css_str++;
1517 while (*css_str && g_ascii_isspace(*css_str))
1518 css_str++;
1519 if (*css_str == '\0' || *css_str == '"' || *css_str == ';')
1520 return NULL;
1522 /* mark the CSS value */
1523 css_value_start = css_str;
1524 while (*css_str && *css_str != '"' && *css_str != ';')
1525 css_str++;
1526 css_value_end = css_str - 1;
1528 /* Removes trailing whitespace */
1529 while (css_value_end > css_value_start && g_ascii_isspace(*css_value_end))
1530 css_value_end--;
1532 tmp = g_strndup(css_value_start, css_value_end - css_value_start + 1);
1533 ret = purple_unescape_html(tmp);
1534 g_free(tmp);
1536 return ret;
1539 gboolean purple_markup_is_rtl(const char *html)
1541 GData *attributes;
1542 const gchar *start, *end;
1543 gboolean res = FALSE;
1545 if (purple_markup_find_tag("span", html, &start, &end, &attributes))
1547 /* tmp is a member of attributes and is free with g_datalist_clear call */
1548 const char *tmp = g_datalist_get_data(&attributes, "dir");
1549 if (tmp && !g_ascii_strcasecmp(tmp, "RTL"))
1550 res = TRUE;
1551 if (!res)
1553 tmp = g_datalist_get_data(&attributes, "style");
1554 if (tmp)
1556 char *tmp2 = purple_markup_get_css_property(tmp, "direction");
1557 if (tmp2 && !g_ascii_strcasecmp(tmp2, "RTL"))
1558 res = TRUE;
1559 g_free(tmp2);
1563 g_datalist_clear(&attributes);
1565 return res;
1568 gboolean
1569 purple_markup_find_tag(const char *needle, const char *haystack,
1570 const char **start, const char **end, GData **attributes)
1572 GData *attribs;
1573 const char *cur = haystack;
1574 char *name = NULL;
1575 gboolean found = FALSE;
1576 gboolean in_tag = FALSE;
1577 gboolean in_attr = FALSE;
1578 const char *in_quotes = NULL;
1579 size_t needlelen;
1581 g_return_val_if_fail( needle != NULL, FALSE);
1582 g_return_val_if_fail( *needle != '\0', FALSE);
1583 g_return_val_if_fail( haystack != NULL, FALSE);
1584 g_return_val_if_fail( start != NULL, FALSE);
1585 g_return_val_if_fail( end != NULL, FALSE);
1586 g_return_val_if_fail(attributes != NULL, FALSE);
1588 needlelen = strlen(needle);
1589 g_datalist_init(&attribs);
1591 while (*cur && !found) {
1592 if (in_tag) {
1593 if (in_quotes) {
1594 const char *close = cur;
1596 while (*close && *close != *in_quotes)
1597 close++;
1599 /* if we got the close quote, store the value and carry on from *
1600 * after it. if we ran to the end of the string, point to the NULL *
1601 * and we're outta here */
1602 if (*close) {
1603 /* only store a value if we have an attribute name */
1604 if (name) {
1605 size_t len = close - cur;
1606 char *val = g_strndup(cur, len);
1608 g_datalist_set_data_full(&attribs, name, val, g_free);
1609 g_free(name);
1610 name = NULL;
1613 in_quotes = NULL;
1614 cur = close + 1;
1615 } else {
1616 cur = close;
1618 } else if (in_attr) {
1619 const char *close = cur;
1621 while (*close && *close != '>' && *close != '"' &&
1622 *close != '\'' && *close != ' ' && *close != '=')
1623 close++;
1625 /* if we got the equals, store the name of the attribute. if we got
1626 * the quote, save the attribute and go straight to quote mode.
1627 * otherwise the tag closed or we reached the end of the string,
1628 * so we can get outta here */
1629 switch (*close) {
1630 case '"':
1631 case '\'':
1632 in_quotes = close;
1633 /* fall through */
1634 case '=':
1636 size_t len = close - cur;
1638 /* don't store a blank attribute name */
1639 if (len) {
1640 g_free(name);
1641 name = g_ascii_strdown(cur, len);
1644 in_attr = FALSE;
1645 cur = close + 1;
1647 break;
1648 case ' ':
1649 case '>':
1650 in_attr = FALSE;
1651 /* fall through */
1652 default:
1653 cur = close;
1654 break;
1656 } else {
1657 switch (*cur) {
1658 case ' ':
1659 /* swallow extra spaces inside tag */
1660 while (*cur && *cur == ' ') cur++;
1661 in_attr = TRUE;
1662 break;
1663 case '>':
1664 found = TRUE;
1665 *end = cur;
1666 break;
1667 case '"':
1668 case '\'':
1669 in_quotes = cur;
1670 /* fall through */
1671 default:
1672 cur++;
1673 break;
1676 } else {
1677 /* if we hit a < followed by the name of our tag... */
1678 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) {
1679 *start = cur;
1680 cur = cur + needlelen + 1;
1682 /* if we're pointing at a space or a >, we found the right tag. if *
1683 * we're not, we've found a longer tag, so we need to skip to the *
1684 * >, but not being distracted by >s inside quotes. */
1685 if (*cur == ' ' || *cur == '>') {
1686 in_tag = TRUE;
1687 } else {
1688 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') {
1689 if (*cur == '"') {
1690 cur++;
1691 while (*cur && *cur != '"')
1692 cur++;
1693 } else if (*cur == '\'') {
1694 cur++;
1695 while (*cur && *cur != '\'')
1696 cur++;
1697 } else {
1698 cur++;
1702 } else {
1703 cur++;
1708 /* clean up any attribute name from a premature termination */
1709 g_free(name);
1711 if (found) {
1712 *attributes = attribs;
1713 } else {
1714 *start = NULL;
1715 *end = NULL;
1716 *attributes = NULL;
1719 return found;
1722 gboolean
1723 purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
1724 const char *start_token, int skip,
1725 const char *end_token, char check_value,
1726 const char *no_value_token,
1727 const char *display_name, gboolean is_link,
1728 const char *link_prefix,
1729 PurpleInfoFieldFormatCallback format_cb)
1731 const char *p, *q;
1733 g_return_val_if_fail(str != NULL, FALSE);
1734 g_return_val_if_fail(user_info != NULL, FALSE);
1735 g_return_val_if_fail(start_token != NULL, FALSE);
1736 g_return_val_if_fail(end_token != NULL, FALSE);
1737 g_return_val_if_fail(display_name != NULL, FALSE);
1739 p = strstr(str, start_token);
1741 if (p == NULL)
1742 return FALSE;
1744 p += strlen(start_token) + skip;
1746 if (p >= str + len)
1747 return FALSE;
1749 if (check_value != '\0' && *p == check_value)
1750 return FALSE;
1752 q = strstr(p, end_token);
1754 /* Trim leading blanks */
1755 while (*p != '\n' && g_ascii_isspace(*p)) {
1756 p += 1;
1759 /* Trim trailing blanks */
1760 while (q > p && g_ascii_isspace(*(q - 1))) {
1761 q -= 1;
1764 /* Don't bother with null strings */
1765 if (p == q)
1766 return FALSE;
1768 if (q != NULL && (!no_value_token ||
1769 (no_value_token && strncmp(p, no_value_token,
1770 strlen(no_value_token)))))
1772 GString *dest = g_string_new("");
1774 if (is_link)
1776 g_string_append(dest, "<a href=\"");
1778 if (link_prefix)
1779 g_string_append(dest, link_prefix);
1781 if (format_cb != NULL)
1783 char *reformatted = format_cb(p, q - p);
1784 g_string_append(dest, reformatted);
1785 g_free(reformatted);
1787 else
1788 g_string_append_len(dest, p, q - p);
1789 g_string_append(dest, "\">");
1791 if (link_prefix)
1792 g_string_append(dest, link_prefix);
1794 g_string_append_len(dest, p, q - p);
1795 g_string_append(dest, "</a>");
1797 else
1799 if (format_cb != NULL)
1801 char *reformatted = format_cb(p, q - p);
1802 g_string_append(dest, reformatted);
1803 g_free(reformatted);
1805 else
1806 g_string_append_len(dest, p, q - p);
1809 purple_notify_user_info_add_pair_html(user_info, display_name, dest->str);
1810 g_string_free(dest, TRUE);
1812 return TRUE;
1815 return FALSE;
1818 struct purple_parse_tag {
1819 char *src_tag;
1820 char *dest_tag;
1821 gboolean ignore;
1824 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1825 recommended in the GCC docs). It contains 'continue's that should
1826 affect the while-loop in purple_markup_html_to_xhtml and doing the
1827 above would break that.
1828 Also, remember to put braces in constructs that require them for
1829 multiple statements when using this macro. */
1830 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1831 const char *o = c + strlen("<" x); \
1832 const char *p = NULL, *q = NULL, *r = NULL; \
1833 /* o = iterating over full tag \
1834 * p = > (end of tag) \
1835 * q = start of quoted bit \
1836 * r = < inside tag \
1837 */ \
1838 GString *innards = g_string_new(""); \
1839 while(o && *o) { \
1840 if(!q && (*o == '\"' || *o == '\'') ) { \
1841 q = o; \
1842 } else if(q) { \
1843 if(*o == *q) { /* end of quoted bit */ \
1844 char *unescaped = g_strndup(q+1, o-q-1); \
1845 char *escaped = g_markup_escape_text(unescaped, -1); \
1846 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1847 g_free(unescaped); \
1848 g_free(escaped); \
1849 q = NULL; \
1850 } else if(*c == '\\') { \
1851 o++; \
1853 } else if(*o == '<') { \
1854 r = o; \
1855 } else if(*o == '>') { \
1856 p = o; \
1857 break; \
1858 } else { \
1859 innards = g_string_append_c(innards, *o); \
1861 o++; \
1863 if(p && !r) { /* got an end of tag and no other < earlier */\
1864 if(*(p-1) != '/') { \
1865 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1866 pt->src_tag = x; \
1867 pt->dest_tag = y; \
1868 tags = g_list_prepend(tags, pt); \
1870 if(xhtml) { \
1871 xhtml = g_string_append(xhtml, "<" y); \
1872 xhtml = g_string_append(xhtml, innards->str); \
1873 xhtml = g_string_append_c(xhtml, '>'); \
1875 c = p + 1; \
1876 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1877 if(xhtml) \
1878 xhtml = g_string_append(xhtml, "&lt;"); \
1879 if(plain) \
1880 plain = g_string_append_c(plain, '<'); \
1881 c++; \
1883 g_string_free(innards, TRUE); \
1884 continue; \
1886 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1887 (*(c+strlen("<" x)) == '>' || \
1888 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1889 if(xhtml) \
1890 xhtml = g_string_append(xhtml, "<" y); \
1891 c += strlen("<" x); \
1892 if(*c != '/') { \
1893 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1894 pt->src_tag = x; \
1895 pt->dest_tag = y; \
1896 tags = g_list_prepend(tags, pt); \
1897 if(xhtml) \
1898 xhtml = g_string_append_c(xhtml, '>'); \
1899 } else { \
1900 if(xhtml) \
1901 xhtml = g_string_append(xhtml, "/>");\
1903 c = strchr(c, '>') + 1; \
1904 continue; \
1906 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1907 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1908 void
1909 purple_markup_html_to_xhtml(const char *html, char **xhtml_out,
1910 char **plain_out)
1912 GString *xhtml = NULL;
1913 GString *plain = NULL;
1914 GString *url = NULL;
1915 GString *cdata = NULL;
1916 GList *tags = NULL, *tag;
1917 const char *c = html;
1918 char quote = '\0';
1920 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1921 quote = *(ptr++); \
1922 else \
1923 quote = '\0';
1925 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1927 g_return_if_fail(xhtml_out != NULL || plain_out != NULL);
1929 if(xhtml_out)
1930 xhtml = g_string_new("");
1931 if(plain_out)
1932 plain = g_string_new("");
1934 while(c && *c) {
1935 if(*c == '<') {
1936 if(*(c+1) == '/') { /* closing tag */
1937 tag = tags;
1938 while(tag) {
1939 struct purple_parse_tag *pt = tag->data;
1940 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') {
1941 c += strlen(pt->src_tag) + 3;
1942 break;
1944 tag = tag->next;
1946 if(tag) {
1947 while(tags) {
1948 struct purple_parse_tag *pt = tags->data;
1949 if(xhtml && !pt->ignore)
1950 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1951 if(plain && purple_strequal(pt->src_tag, "a")) {
1952 /* if this is a link, we have to add the url to the plaintext, too */
1953 if (cdata && url &&
1954 (!g_string_equal(cdata, url) && (g_ascii_strncasecmp(url->str, "mailto:", 7) != 0 ||
1955 g_utf8_collate(url->str + 7, cdata->str) != 0)))
1956 g_string_append_printf(plain, " <%s>", g_strstrip(purple_unescape_html(url->str)));
1957 if (cdata) {
1958 g_string_free(cdata, TRUE);
1959 cdata = NULL;
1963 if(tags == tag)
1964 break;
1965 tags = g_list_remove(tags, pt);
1966 g_free(pt);
1968 g_free(tag->data);
1969 tags = g_list_remove(tags, tag->data);
1970 } else {
1971 /* a closing tag we weren't expecting...
1972 * we'll let it slide, if it's really a tag...if it's
1973 * just a </ we'll escape it properly */
1974 const char *end = c+2;
1975 while(*end && g_ascii_isalpha(*end))
1976 end++;
1977 if(*end == '>') {
1978 c = end+1;
1979 } else {
1980 if(xhtml)
1981 xhtml = g_string_append(xhtml, "&lt;");
1982 if(plain)
1983 plain = g_string_append_c(plain, '<');
1984 c++;
1987 } else { /* opening tag */
1988 ALLOW_TAG("blockquote");
1989 ALLOW_TAG("cite");
1990 ALLOW_TAG("div");
1991 ALLOW_TAG("em");
1992 ALLOW_TAG("h1");
1993 ALLOW_TAG("h2");
1994 ALLOW_TAG("h3");
1995 ALLOW_TAG("h4");
1996 ALLOW_TAG("h5");
1997 ALLOW_TAG("h6");
1998 /* we only allow html to start the message */
1999 if(c == html) {
2000 ALLOW_TAG("html");
2002 ALLOW_TAG_ALT("i", "em");
2003 ALLOW_TAG_ALT("italic", "em");
2004 ALLOW_TAG("li");
2005 ALLOW_TAG("ol");
2006 ALLOW_TAG("p");
2007 ALLOW_TAG("pre");
2008 ALLOW_TAG("q");
2009 ALLOW_TAG("span");
2010 ALLOW_TAG("ul");
2013 /* we skip <HR> because it's not legal in XHTML-IM. However,
2014 * we still want to send something sensible, so we put a
2015 * linebreak in its place. <BR> also needs special handling
2016 * because putting a </BR> to close it would just be dumb. */
2017 if((!g_ascii_strncasecmp(c, "<br", 3)
2018 || !g_ascii_strncasecmp(c, "<hr", 3))
2019 && (*(c+3) == '>' ||
2020 !g_ascii_strncasecmp(c+3, "/>", 2) ||
2021 !g_ascii_strncasecmp(c+3, " />", 3))) {
2022 c = strchr(c, '>') + 1;
2023 if(xhtml)
2024 xhtml = g_string_append(xhtml, "<br/>");
2025 if(plain && *c != '\n')
2026 plain = g_string_append_c(plain, '\n');
2027 continue;
2029 if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c, "<strong>", strlen("<strong>"))) {
2030 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2031 if (*(c+2) == '>')
2032 pt->src_tag = "b";
2033 else if (*(c+2) == 'o')
2034 pt->src_tag = "bold";
2035 else
2036 pt->src_tag = "strong";
2037 pt->dest_tag = "span";
2038 tags = g_list_prepend(tags, pt);
2039 c = strchr(c, '>') + 1;
2040 if(xhtml)
2041 xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>");
2042 continue;
2044 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) {
2045 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2046 pt->src_tag = *(c+2) == '>' ? "u" : "underline";
2047 pt->dest_tag = "span";
2048 tags = g_list_prepend(tags, pt);
2049 c = strchr(c, '>') + 1;
2050 if (xhtml)
2051 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>");
2052 continue;
2054 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) {
2055 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2056 pt->src_tag = *(c+2) == '>' ? "s" : "strike";
2057 pt->dest_tag = "span";
2058 tags = g_list_prepend(tags, pt);
2059 c = strchr(c, '>') + 1;
2060 if(xhtml)
2061 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>");
2062 continue;
2064 if(!g_ascii_strncasecmp(c, "<sub>", 5)) {
2065 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2066 pt->src_tag = "sub";
2067 pt->dest_tag = "span";
2068 tags = g_list_prepend(tags, pt);
2069 c = strchr(c, '>') + 1;
2070 if(xhtml)
2071 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>");
2072 continue;
2074 if(!g_ascii_strncasecmp(c, "<sup>", 5)) {
2075 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2076 pt->src_tag = "sup";
2077 pt->dest_tag = "span";
2078 tags = g_list_prepend(tags, pt);
2079 c = strchr(c, '>') + 1;
2080 if(xhtml)
2081 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
2082 continue;
2084 if (!g_ascii_strncasecmp(c, "<img", 4) && (*(c+4) == '>' || *(c+4) == ' ')) {
2085 const char *p = c + 4;
2086 GString *src = NULL, *alt = NULL;
2087 #define ESCAPE(from, to) \
2088 CHECK_QUOTE(from); \
2089 while (VALID_CHAR(from)) { \
2090 int len; \
2091 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2092 to = g_string_append(to, "&amp;"); \
2093 else if (*from == '\'') \
2094 to = g_string_append(to, "&apos;"); \
2095 else \
2096 to = g_string_append_c(to, *from); \
2097 from++; \
2100 while (*p && *p != '>') {
2101 if (!g_ascii_strncasecmp(p, "src=", 4)) {
2102 const char *q = p + 4;
2103 if (src)
2104 g_string_free(src, TRUE);
2105 src = g_string_new("");
2106 ESCAPE(q, src);
2107 p = q;
2108 } else if (!g_ascii_strncasecmp(p, "alt=", 4)) {
2109 const char *q = p + 4;
2110 if (alt)
2111 g_string_free(alt, TRUE);
2112 alt = g_string_new("");
2113 ESCAPE(q, alt);
2114 p = q;
2115 } else {
2116 p++;
2119 #undef ESCAPE
2120 if ((c = strchr(p, '>')) != NULL)
2121 c++;
2122 else
2123 c = p;
2124 /* src and alt are required! */
2125 if(src && xhtml)
2126 g_string_append_printf(xhtml, "<img src='%s' alt='%s' />", g_strstrip(src->str), alt ? alt->str : "");
2127 if(alt) {
2128 if(plain)
2129 plain = g_string_append(plain, purple_unescape_html(alt->str));
2130 if(!src && xhtml)
2131 xhtml = g_string_append(xhtml, alt->str);
2132 g_string_free(alt, TRUE);
2134 g_string_free(src, TRUE);
2135 continue;
2137 if (!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
2138 const char *p = c + 2;
2139 struct purple_parse_tag *pt;
2140 while (*p && *p != '>') {
2141 if (!g_ascii_strncasecmp(p, "href=", 5)) {
2142 const char *q = p + 5;
2143 if (url)
2144 g_string_free(url, TRUE);
2145 url = g_string_new("");
2146 if (cdata)
2147 g_string_free(cdata, TRUE);
2148 cdata = g_string_new("");
2149 CHECK_QUOTE(q);
2150 while (VALID_CHAR(q)) {
2151 int len;
2152 if ((*q == '&') && (purple_markup_unescape_entity(q, &len) == NULL))
2153 url = g_string_append(url, "&amp;");
2154 else if (*q == '"')
2155 url = g_string_append(url, "&quot;");
2156 else
2157 url = g_string_append_c(url, *q);
2158 q++;
2160 p = q;
2161 } else {
2162 p++;
2165 if ((c = strchr(p, '>')) != NULL)
2166 c++;
2167 else
2168 c = p;
2169 pt = g_new0(struct purple_parse_tag, 1);
2170 pt->src_tag = "a";
2171 pt->dest_tag = "a";
2172 tags = g_list_prepend(tags, pt);
2173 if(xhtml)
2174 g_string_append_printf(xhtml, "<a href=\"%s\">", url ? g_strstrip(url->str) : "");
2175 continue;
2177 #define ESCAPE(from, to) \
2178 CHECK_QUOTE(from); \
2179 while (VALID_CHAR(from)) { \
2180 int len; \
2181 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2182 to = g_string_append(to, "&amp;"); \
2183 else if (*from == '\'') \
2184 to = g_string_append_c(to, '\"'); \
2185 else \
2186 to = g_string_append_c(to, *from); \
2187 from++; \
2189 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
2190 const char *p = c + 5;
2191 GString *style = g_string_new("");
2192 struct purple_parse_tag *pt;
2193 while (*p && *p != '>') {
2194 if (!g_ascii_strncasecmp(p, "back=", 5)) {
2195 const char *q = p + 5;
2196 GString *color = g_string_new("");
2197 ESCAPE(q, color);
2198 g_string_append_printf(style, "background: %s; ", color->str);
2199 g_string_free(color, TRUE);
2200 p = q;
2201 } else if (!g_ascii_strncasecmp(p, "color=", 6)) {
2202 const char *q = p + 6;
2203 GString *color = g_string_new("");
2204 ESCAPE(q, color);
2205 g_string_append_printf(style, "color: %s; ", color->str);
2206 g_string_free(color, TRUE);
2207 p = q;
2208 } else if (!g_ascii_strncasecmp(p, "face=", 5)) {
2209 const char *q = p + 5;
2210 GString *face = g_string_new("");
2211 ESCAPE(q, face);
2212 g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str));
2213 g_string_free(face, TRUE);
2214 p = q;
2215 } else if (!g_ascii_strncasecmp(p, "size=", 5)) {
2216 const char *q = p + 5;
2217 int sz;
2218 const char *size = "medium";
2219 CHECK_QUOTE(q);
2220 sz = atoi(q);
2221 switch (sz)
2223 case 1:
2224 size = "xx-small";
2225 break;
2226 case 2:
2227 size = "small";
2228 break;
2229 case 3:
2230 size = "medium";
2231 break;
2232 case 4:
2233 size = "large";
2234 break;
2235 case 5:
2236 size = "x-large";
2237 break;
2238 case 6:
2239 case 7:
2240 size = "xx-large";
2241 break;
2242 default:
2243 break;
2245 g_string_append_printf(style, "font-size: %s; ", size);
2246 p = q;
2247 } else {
2248 p++;
2251 if ((c = strchr(p, '>')) != NULL)
2252 c++;
2253 else
2254 c = p;
2255 pt = g_new0(struct purple_parse_tag, 1);
2256 pt->src_tag = "font";
2257 pt->dest_tag = "span";
2258 tags = g_list_prepend(tags, pt);
2259 if(style->len && xhtml)
2260 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
2261 else
2262 pt->ignore = TRUE;
2263 g_string_free(style, TRUE);
2264 continue;
2266 #undef ESCAPE
2267 if (!g_ascii_strncasecmp(c, "<body ", 6)) {
2268 const char *p = c + 6;
2269 gboolean did_something = FALSE;
2270 while (*p && *p != '>') {
2271 if (!g_ascii_strncasecmp(p, "bgcolor=", 8)) {
2272 const char *q = p + 8;
2273 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2274 GString *color = g_string_new("");
2275 CHECK_QUOTE(q);
2276 while (VALID_CHAR(q)) {
2277 color = g_string_append_c(color, *q);
2278 q++;
2280 if (xhtml)
2281 g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str));
2282 g_string_free(color, TRUE);
2283 if ((c = strchr(p, '>')) != NULL)
2284 c++;
2285 else
2286 c = p;
2287 pt->src_tag = "body";
2288 pt->dest_tag = "span";
2289 tags = g_list_prepend(tags, pt);
2290 did_something = TRUE;
2291 break;
2293 p++;
2295 if (did_something) continue;
2297 /* this has to come after the special case for bgcolor */
2298 ALLOW_TAG("body");
2299 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) {
2300 char *p = strstr(c + strlen("<!--"), "-->");
2301 if(p) {
2302 if(xhtml)
2303 xhtml = g_string_append(xhtml, "<!--");
2304 c += strlen("<!--");
2305 continue;
2309 if(xhtml)
2310 xhtml = g_string_append(xhtml, "&lt;");
2311 if(plain)
2312 plain = g_string_append_c(plain, '<');
2313 c++;
2315 } else if(*c == '&') {
2316 char buf[7];
2317 const char *pln;
2318 int len;
2320 if ((pln = purple_markup_unescape_entity(c, &len)) == NULL) {
2321 len = 1;
2322 g_snprintf(buf, sizeof(buf), "%c", *c);
2323 pln = buf;
2325 if(xhtml)
2326 xhtml = g_string_append_len(xhtml, c, len);
2327 if(plain)
2328 plain = g_string_append(plain, pln);
2329 if(cdata)
2330 cdata = g_string_append_len(cdata, c, len);
2331 c += len;
2332 } else {
2333 if(xhtml)
2334 xhtml = g_string_append_c(xhtml, *c);
2335 if(plain)
2336 plain = g_string_append_c(plain, *c);
2337 if(cdata)
2338 cdata = g_string_append_c(cdata, *c);
2339 c++;
2342 if(xhtml) {
2343 for (tag = tags; tag ; tag = tag->next) {
2344 struct purple_parse_tag *pt = tag->data;
2345 if(!pt->ignore)
2346 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
2349 g_list_free(tags);
2350 if(xhtml_out)
2351 *xhtml_out = g_string_free(xhtml, FALSE);
2352 if(plain_out)
2353 *plain_out = g_string_free(plain, FALSE);
2354 if(url)
2355 g_string_free(url, TRUE);
2356 if (cdata)
2357 g_string_free(cdata, TRUE);
2358 #undef CHECK_QUOTE
2359 #undef VALID_CHAR
2362 /* The following are probably reasonable changes:
2363 * - \n should be converted to a normal space
2364 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
2365 * - We want to turn </td>#whitespace<td> sequences into a single tab
2366 * - We want to turn <td> into a single tab (for msn profile "parsing")
2367 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
2368 * - <script>...</script> and <style>...</style> should be completely removed
2371 char *
2372 purple_markup_strip_html(const char *str)
2374 int i, j, k, entlen;
2375 gboolean visible = TRUE;
2376 gboolean closing_td_p = FALSE;
2377 gchar *str2;
2378 const gchar *cdata_close_tag = NULL, *ent;
2379 gchar *href = NULL;
2380 int href_st = 0;
2382 if(!str)
2383 return NULL;
2385 str2 = g_strdup(str);
2387 for (i = 0, j = 0; str2[i]; i++)
2389 if (str2[i] == '<')
2391 if (cdata_close_tag)
2393 /* Note: Don't even assume any other tag is a tag in CDATA */
2394 if (g_ascii_strncasecmp(str2 + i, cdata_close_tag,
2395 strlen(cdata_close_tag)) == 0)
2397 i += strlen(cdata_close_tag) - 1;
2398 cdata_close_tag = NULL;
2400 continue;
2402 else if (g_ascii_strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p)
2404 str2[j++] = '\t';
2405 visible = TRUE;
2407 else if (g_ascii_strncasecmp(str2 + i, "</td>", 5) == 0)
2409 closing_td_p = TRUE;
2410 visible = FALSE;
2412 else
2414 closing_td_p = FALSE;
2415 visible = TRUE;
2418 k = i + 1;
2420 if(g_ascii_isspace(str2[k]))
2421 visible = TRUE;
2422 else if (str2[k])
2424 /* Scan until we end the tag either implicitly (closed start
2425 * tag) or explicitly, using a sloppy method (i.e., < or >
2426 * inside quoted attributes will screw us up)
2428 while (str2[k] && str2[k] != '<' && str2[k] != '>')
2430 k++;
2433 /* If we've got an <a> tag with an href, save the address
2434 * to print later. */
2435 if (g_ascii_strncasecmp(str2 + i, "<a", 2) == 0 &&
2436 g_ascii_isspace(str2[i+2]))
2438 int st; /* start of href, inclusive [ */
2439 int end; /* end of href, exclusive ) */
2440 char delim = ' ';
2441 /* Find start of href */
2442 for (st = i + 3; st < k; st++)
2444 if (g_ascii_strncasecmp(str2+st, "href=", 5) == 0)
2446 st += 5;
2447 if (str2[st] == '"' || str2[st] == '\'')
2449 delim = str2[st];
2450 st++;
2452 break;
2455 /* find end of address */
2456 for (end = st; end < k && str2[end] != delim; end++)
2458 /* All the work is done in the loop construct above. */
2461 /* If there's an address, save it. If there was
2462 * already one saved, kill it. */
2463 if (st < k)
2465 char *tmp;
2466 g_free(href);
2467 tmp = g_strndup(str2 + st, end - st);
2468 href = purple_unescape_html(tmp);
2469 g_free(tmp);
2470 href_st = j;
2474 /* Replace </a> with an ascii representation of the
2475 * address the link was pointing to. */
2476 else if (href != NULL && g_ascii_strncasecmp(str2 + i, "</a>", 4) == 0)
2478 size_t hrlen = strlen(href);
2480 /* Only insert the href if it's different from the CDATA. */
2481 if ((hrlen != (gsize)(j - href_st) ||
2482 strncmp(str2 + href_st, href, hrlen)) &&
2483 (hrlen != (gsize)(j - href_st + 7) || /* 7 == strlen("http://") */
2484 strncmp(str2 + href_st, href + 7, hrlen - 7)))
2486 str2[j++] = ' ';
2487 str2[j++] = '(';
2488 g_memmove(str2 + j, href, hrlen);
2489 j += hrlen;
2490 str2[j++] = ')';
2491 g_free(href);
2492 href = NULL;
2496 /* Check for tags which should be mapped to newline (but ignore some of
2497 * the tags at the beginning of the text) */
2498 else if ((j && (g_ascii_strncasecmp(str2 + i, "<p>", 3) == 0
2499 || g_ascii_strncasecmp(str2 + i, "<tr", 3) == 0
2500 || g_ascii_strncasecmp(str2 + i, "<hr", 3) == 0
2501 || g_ascii_strncasecmp(str2 + i, "<li", 3) == 0
2502 || g_ascii_strncasecmp(str2 + i, "<div", 4) == 0))
2503 || g_ascii_strncasecmp(str2 + i, "<br", 3) == 0
2504 || g_ascii_strncasecmp(str2 + i, "</table>", 8) == 0)
2506 str2[j++] = '\n';
2508 /* Check for tags which begin CDATA and need to be closed */
2509 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */
2510 else if (g_ascii_strncasecmp(str2 + i, "<option", 7) == 0)
2512 /* FIXME: We should not do this if the OPTION is SELECT'd */
2513 cdata_close_tag = "</option>";
2515 #endif
2516 else if (g_ascii_strncasecmp(str2 + i, "<script", 7) == 0)
2518 cdata_close_tag = "</script>";
2520 else if (g_ascii_strncasecmp(str2 + i, "<style", 6) == 0)
2522 cdata_close_tag = "</style>";
2524 /* Update the index and continue checking after the tag */
2525 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k;
2526 continue;
2529 else if (cdata_close_tag)
2531 continue;
2533 else if (!g_ascii_isspace(str2[i]))
2535 visible = TRUE;
2538 if (str2[i] == '&' && (ent = purple_markup_unescape_entity(str2 + i, &entlen)) != NULL)
2540 while (*ent)
2541 str2[j++] = *ent++;
2542 i += entlen - 1;
2543 continue;
2546 if (visible)
2547 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i];
2550 g_free(href);
2552 str2[j] = '\0';
2554 return str2;
2557 static gboolean
2558 badchar(char c)
2560 switch (c) {
2561 case ' ':
2562 case ',':
2563 case '\0':
2564 case '\n':
2565 case '\r':
2566 case '<':
2567 case '>':
2568 case '"':
2569 return TRUE;
2570 default:
2571 return FALSE;
2575 static gboolean
2576 badentity(const char *c)
2578 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
2579 !g_ascii_strncasecmp(c, "&gt;", 4) ||
2580 !g_ascii_strncasecmp(c, "&quot;", 6)) {
2581 return TRUE;
2583 return FALSE;
2586 static const char *
2587 process_link(GString *ret,
2588 const char *start, const char *c,
2589 int matchlen,
2590 const char *urlprefix,
2591 int inside_paren)
2593 char *url_buf, *tmpurlbuf;
2594 const char *t;
2596 for (t = c;; t++) {
2597 if (!badchar(*t) && !badentity(t))
2598 continue;
2600 if (t - c == matchlen)
2601 break;
2603 if (*t == ',' && *(t + 1) != ' ') {
2604 continue;
2607 if (t > start && *(t - 1) == '.')
2608 t--;
2609 if (t > start && *(t - 1) == ')' && inside_paren > 0)
2610 t--;
2612 url_buf = g_strndup(c, t - c);
2613 tmpurlbuf = purple_unescape_html(url_buf);
2614 g_string_append_printf(ret, "<A HREF=\"%s%s\">%s</A>",
2615 urlprefix,
2616 tmpurlbuf, url_buf);
2617 g_free(tmpurlbuf);
2618 g_free(url_buf);
2619 return t;
2622 return c;
2625 char *
2626 purple_markup_linkify(const char *text)
2628 const char *c, *t, *q = NULL;
2629 char *tmpurlbuf, *url_buf;
2630 gunichar g;
2631 gboolean inside_html = FALSE;
2632 int inside_paren = 0;
2633 GString *ret;
2635 if (text == NULL)
2636 return NULL;
2638 ret = g_string_new("");
2640 c = text;
2641 while (*c) {
2643 if(*c == '(' && !inside_html) {
2644 inside_paren++;
2645 ret = g_string_append_c(ret, *c);
2646 c++;
2649 if(inside_html) {
2650 if(*c == '>') {
2651 inside_html = FALSE;
2652 } else if(!q && (*c == '\"' || *c == '\'')) {
2653 q = c;
2654 } else if(q) {
2655 if(*c == *q)
2656 q = NULL;
2658 } else if(*c == '<') {
2659 inside_html = TRUE;
2660 if (!g_ascii_strncasecmp(c, "<A", 2)) {
2661 while (1) {
2662 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
2663 inside_html = FALSE;
2664 break;
2666 ret = g_string_append_c(ret, *c);
2667 c++;
2668 if (!(*c))
2669 break;
2672 } else if (!g_ascii_strncasecmp(c, "http://", 7)) {
2673 c = process_link(ret, text, c, 7, "", inside_paren);
2674 } else if (!g_ascii_strncasecmp(c, "https://", 8)) {
2675 c = process_link(ret, text, c, 8, "", inside_paren);
2676 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) {
2677 c = process_link(ret, text, c, 6, "", inside_paren);
2678 } else if (!g_ascii_strncasecmp(c, "sftp://", 7)) {
2679 c = process_link(ret, text, c, 7, "", inside_paren);
2680 } else if (!g_ascii_strncasecmp(c, "file://", 7)) {
2681 c = process_link(ret, text, c, 7, "", inside_paren);
2682 } else if (!g_ascii_strncasecmp(c, "www.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2683 c = process_link(ret, text, c, 4, "http://", inside_paren);
2684 } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2685 c = process_link(ret, text, c, 4, "ftp://", inside_paren);
2686 } else if (!g_ascii_strncasecmp(c, "xmpp:", 5) && (c == text || badchar(c[-1]) || badentity(c-1))) {
2687 c = process_link(ret, text, c, 5, "", inside_paren);
2688 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) {
2689 t = c;
2690 while (1) {
2691 if (badchar(*t) || badentity(t)) {
2692 char *d;
2693 if (t - c == 7) {
2694 break;
2696 if (t > text && *(t - 1) == '.')
2697 t--;
2698 if ((d = strstr(c + 7, "?")) != NULL && d < t)
2699 url_buf = g_strndup(c + 7, d - c - 7);
2700 else
2701 url_buf = g_strndup(c + 7, t - c - 7);
2702 if (!purple_email_is_valid(url_buf)) {
2703 g_free(url_buf);
2704 break;
2706 g_free(url_buf);
2707 url_buf = g_strndup(c, t - c);
2708 tmpurlbuf = purple_unescape_html(url_buf);
2709 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
2710 tmpurlbuf, url_buf);
2711 g_free(url_buf);
2712 g_free(tmpurlbuf);
2713 c = t;
2714 break;
2716 t++;
2718 } else if (c != text && (*c == '@')) {
2719 int flag;
2720 GString *gurl_buf = NULL;
2721 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2723 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1)))
2724 flag = 0;
2725 else {
2726 flag = 1;
2727 gurl_buf = g_string_new("");
2730 t = c;
2731 while (flag) {
2732 /* iterate backwards grabbing the local part of an email address */
2733 g = g_utf8_get_char(t);
2734 if (badchar(*t) || (g >= 127) || (*t == '(') ||
2735 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "&lt;", 4) ||
2736 !g_ascii_strncasecmp(t - 3, "&gt;", 4))) ||
2737 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, "&quot;", 6)))))) {
2738 /* local part will already be part of ret, strip it out */
2739 ret = g_string_truncate(ret, ret->len - (c - t));
2740 ret = g_string_append_unichar(ret, g);
2741 break;
2742 } else {
2743 g_string_prepend_unichar(gurl_buf, g);
2744 t = g_utf8_find_prev_char(text, t);
2745 if (t < text) {
2746 ret = g_string_assign(ret, "");
2747 break;
2752 t = g_utf8_find_next_char(c, NULL);
2754 while (flag) {
2755 /* iterate forwards grabbing the domain part of an email address */
2756 g = g_utf8_get_char(t);
2757 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) {
2758 char *d;
2760 url_buf = g_string_free(gurl_buf, FALSE);
2762 /* strip off trailing periods */
2763 if (*url_buf) {
2764 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
2765 *d = '\0';
2768 tmpurlbuf = purple_unescape_html(url_buf);
2769 if (purple_email_is_valid(tmpurlbuf)) {
2770 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
2771 tmpurlbuf, url_buf);
2772 } else {
2773 g_string_append(ret, url_buf);
2775 g_free(url_buf);
2776 g_free(tmpurlbuf);
2777 c = t;
2779 break;
2780 } else {
2781 g_string_append_unichar(gurl_buf, g);
2782 t = g_utf8_find_next_char(t, NULL);
2787 if(*c == ')' && !inside_html) {
2788 inside_paren--;
2789 ret = g_string_append_c(ret, *c);
2790 c++;
2793 if (*c == 0)
2794 break;
2796 ret = g_string_append_c(ret, *c);
2797 c++;
2800 return g_string_free(ret, FALSE);
2803 char *purple_unescape_text(const char *in)
2805 GString *ret;
2806 const char *c = in;
2808 if (in == NULL)
2809 return NULL;
2811 ret = g_string_new("");
2812 while (*c) {
2813 int len;
2814 const char *ent;
2816 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2817 g_string_append(ret, ent);
2818 c += len;
2819 } else {
2820 g_string_append_c(ret, *c);
2821 c++;
2825 return g_string_free(ret, FALSE);
2828 char *purple_unescape_html(const char *html)
2830 GString *ret;
2831 const char *c = html;
2833 if (html == NULL)
2834 return NULL;
2836 ret = g_string_new("");
2837 while (*c) {
2838 int len;
2839 const char *ent;
2841 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2842 g_string_append(ret, ent);
2843 c += len;
2844 } else if (!strncmp(c, "<br>", 4)) {
2845 g_string_append_c(ret, '\n');
2846 c += 4;
2847 } else {
2848 g_string_append_c(ret, *c);
2849 c++;
2853 return g_string_free(ret, FALSE);
2856 char *
2857 purple_markup_slice(const char *str, guint x, guint y)
2859 GString *ret;
2860 GQueue *q;
2861 guint z = 0;
2862 gboolean appended = FALSE;
2863 gunichar c;
2864 char *tag;
2866 g_return_val_if_fail(str != NULL, NULL);
2867 g_return_val_if_fail(x <= y, NULL);
2869 if (x == y)
2870 return g_strdup("");
2872 ret = g_string_new("");
2873 q = g_queue_new();
2875 while (*str && (z < y)) {
2876 c = g_utf8_get_char(str);
2878 if (c == '<') {
2879 char *end = strchr(str, '>');
2881 if (!end) {
2882 g_string_free(ret, TRUE);
2883 while ((tag = g_queue_pop_head(q)))
2884 g_free(tag);
2885 g_queue_free(q);
2886 return NULL;
2889 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
2890 z += strlen("[Image]");
2891 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
2892 z += 1;
2893 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
2894 z += strlen("\n---\n");
2895 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
2896 /* pop stack */
2897 char *tmp;
2899 tmp = g_queue_pop_head(q);
2900 g_free(tmp);
2901 /* z += 0; */
2902 } else {
2903 /* push it unto the stack */
2904 char *tmp;
2906 tmp = g_strndup(str, end - str + 1);
2907 g_queue_push_head(q, tmp);
2908 /* z += 0; */
2911 if (z >= x) {
2912 g_string_append_len(ret, str, end - str + 1);
2915 str = end;
2916 } else if (c == '&') {
2917 char *end = strchr(str, ';');
2918 if (!end) {
2919 g_string_free(ret, TRUE);
2920 while ((tag = g_queue_pop_head(q)))
2921 g_free(tag);
2922 g_queue_free(q);
2924 return NULL;
2927 if (z >= x)
2928 g_string_append_len(ret, str, end - str + 1);
2930 z++;
2931 str = end;
2932 } else {
2933 if (z == x && z > 0 && !appended) {
2934 GList *l = q->tail;
2936 while (l) {
2937 tag = l->data;
2938 g_string_append(ret, tag);
2939 l = l->prev;
2941 appended = TRUE;
2944 if (z >= x)
2945 g_string_append_unichar(ret, c);
2946 z++;
2949 str = g_utf8_next_char(str);
2952 while ((tag = g_queue_pop_head(q))) {
2953 char *name;
2955 name = purple_markup_get_tag_name(tag);
2956 g_string_append_printf(ret, "</%s>", name);
2957 g_free(name);
2958 g_free(tag);
2961 g_queue_free(q);
2962 return g_string_free(ret, FALSE);
2965 char *
2966 purple_markup_get_tag_name(const char *tag)
2968 int i;
2969 g_return_val_if_fail(tag != NULL, NULL);
2970 g_return_val_if_fail(*tag == '<', NULL);
2972 for (i = 1; tag[i]; i++)
2973 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
2974 break;
2976 return g_strndup(tag+1, i-1);
2979 /**************************************************************************
2980 * Path/Filename Functions
2981 **************************************************************************/
2982 const char *
2983 purple_home_dir(void)
2985 #ifndef _WIN32
2986 return g_get_home_dir();
2987 #else
2988 return wpurple_home_dir();
2989 #endif
2992 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2993 const char *
2994 purple_user_dir(void)
2996 if (custom_user_dir != NULL)
2997 return custom_user_dir;
2998 else if (!user_dir)
2999 user_dir = g_build_filename(purple_home_dir(), ".purple", NULL);
3001 return user_dir;
3004 const char *
3005 purple_cache_dir(void)
3007 if (custom_user_dir != NULL) {
3008 return custom_user_dir;
3011 if (!cache_dir) {
3012 cache_dir = g_build_filename(g_get_user_cache_dir(), "purple", NULL);
3015 return cache_dir;
3018 const char *
3019 purple_config_dir(void)
3021 if (custom_user_dir != NULL) {
3022 return custom_user_dir;
3025 if (!config_dir) {
3026 config_dir = g_build_filename(g_get_user_config_dir(), "purple", NULL);
3029 return config_dir;
3032 const char *
3033 purple_data_dir(void)
3035 if (custom_user_dir != NULL) {
3036 return custom_user_dir;
3039 if (!data_dir) {
3040 data_dir = g_build_filename(g_get_user_data_dir(), "purple", NULL);
3043 return data_dir;
3046 void purple_util_set_user_dir(const char *dir)
3048 g_free(custom_user_dir);
3050 if (dir != NULL && *dir)
3051 custom_user_dir = g_strdup(dir);
3052 else
3053 custom_user_dir = NULL;
3056 int purple_build_dir (const char *path, int mode)
3058 return g_mkdir_with_parents(path, mode);
3061 static gboolean
3062 purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size)
3064 gchar *filename_full;
3065 gboolean ret = FALSE;
3067 g_return_val_if_fail(dir != NULL, FALSE);
3069 purple_debug_misc("util", "Writing file %s to directory %s",
3070 filename, dir);
3072 /* Ensure the user directory exists */
3073 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
3075 if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
3077 purple_debug_error("util", "Error creating directory %s: %s\n",
3078 dir, g_strerror(errno));
3079 return FALSE;
3083 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dir, filename);
3085 ret = purple_util_write_data_to_file_absolute(filename_full, data, size);
3087 g_free(filename_full);
3088 return ret;
3092 * This function is long and beautiful, like my--um, yeah. Anyway,
3093 * it includes lots of error checking so as we don't overwrite
3094 * people's settings if there is a problem writing the new values.
3096 gboolean
3097 purple_util_write_data_to_file(const char *filename, const char *data, gssize size)
3099 const char *user_dir = purple_user_dir();
3100 gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size);
3102 return ret;
3105 gboolean
3106 purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size)
3108 const char *cache_dir = purple_cache_dir();
3109 gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size);
3111 return ret;
3114 gboolean
3115 purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size)
3117 const char *config_dir = purple_cache_dir();
3118 gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size);
3120 return ret;
3123 gboolean
3124 purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size)
3126 const char *data_dir = purple_cache_dir();
3127 gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size);
3129 return ret;
3132 gboolean
3133 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size)
3135 gchar *filename_temp;
3136 FILE *file;
3137 gsize real_size, byteswritten;
3138 GStatBuf st;
3139 #ifndef HAVE_FILENO
3140 int fd;
3141 #endif
3143 purple_debug_misc("util", "Writing file %s",
3144 filename_full);
3146 g_return_val_if_fail((size >= -1), FALSE);
3148 filename_temp = g_strdup_printf("%s.save", filename_full);
3150 /* Remove an old temporary file, if one exists */
3151 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS))
3153 if (g_unlink(filename_temp) == -1)
3155 purple_debug_error("util", "Error removing old file "
3156 "%s: %s\n",
3157 filename_temp, g_strerror(errno));
3161 /* Open file */
3162 file = g_fopen(filename_temp, "wb");
3163 if (file == NULL)
3165 purple_debug_error("util", "Error opening file %s for "
3166 "writing: %s\n",
3167 filename_temp, g_strerror(errno));
3168 g_free(filename_temp);
3169 return FALSE;
3172 /* Write to file */
3173 real_size = (size == -1) ? strlen(data) : (size_t) size;
3174 byteswritten = fwrite(data, 1, real_size, file);
3176 #ifdef HAVE_FILENO
3177 #ifndef _WIN32
3178 /* Set file permissions */
3179 if (fchmod(fileno(file), S_IRUSR | S_IWUSR) == -1) {
3180 purple_debug_error("util", "Error setting permissions of "
3181 "file %s: %s\n", filename_temp, g_strerror(errno));
3183 #endif
3185 /* Apparently XFS (and possibly other filesystems) do not
3186 * guarantee that file data is flushed before file metadata,
3187 * so this procedure is insufficient without some flushage. */
3188 if (fflush(file) < 0) {
3189 purple_debug_error("util", "Error flushing %s: %s\n",
3190 filename_temp, g_strerror(errno));
3191 g_free(filename_temp);
3192 fclose(file);
3193 return FALSE;
3195 if (fsync(fileno(file)) < 0) {
3196 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
3197 filename_temp, g_strerror(errno));
3198 g_free(filename_temp);
3199 fclose(file);
3200 return FALSE;
3202 #endif
3204 /* Close file */
3205 if (fclose(file) != 0)
3207 purple_debug_error("util", "Error closing file %s: %s\n",
3208 filename_temp, g_strerror(errno));
3209 g_free(filename_temp);
3210 return FALSE;
3213 #ifndef HAVE_FILENO
3214 /* This is the same effect (we hope) as the HAVE_FILENO block
3215 * above, but for systems without fileno(). */
3216 if ((fd = open(filename_temp, O_RDWR)) < 0) {
3217 purple_debug_error("util", "Error opening file %s for flush: %s\n",
3218 filename_temp, g_strerror(errno));
3219 g_free(filename_temp);
3220 return FALSE;
3223 #ifndef _WIN32
3224 /* copy-pasta! */
3225 if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) {
3226 purple_debug_error("util", "Error setting permissions of "
3227 "file %s: %s\n", filename_temp, g_strerror(errno));
3229 #endif
3231 if (fsync(fd) < 0) {
3232 purple_debug_error("util", "Error syncing %s: %s\n",
3233 filename_temp, g_strerror(errno));
3234 g_free(filename_temp);
3235 close(fd);
3236 return FALSE;
3238 if (close(fd) < 0) {
3239 purple_debug_error("util", "Error closing %s after sync: %s\n",
3240 filename_temp, g_strerror(errno));
3241 g_free(filename_temp);
3242 return FALSE;
3244 #endif
3246 /* Ensure the file is the correct size */
3247 if (byteswritten != real_size)
3249 purple_debug_error("util", "Error writing to file %s: Wrote %"
3250 G_GSIZE_FORMAT " bytes "
3251 "but should have written %" G_GSIZE_FORMAT
3252 "; is your disk full?\n",
3253 filename_temp, byteswritten, real_size);
3254 g_free(filename_temp);
3255 return FALSE;
3257 #ifndef __COVERITY__
3258 /* Use stat to be absolutely sure.
3259 * It causes TOCTOU coverity warning (against g_rename below),
3260 * but it's not a threat for us.
3262 if ((g_stat(filename_temp, &st) == -1) || ((gsize)st.st_size != real_size)) {
3263 purple_debug_error("util", "Error writing data to file %s: "
3264 "couldn't g_stat file", filename_temp);
3265 g_free(filename_temp);
3266 return FALSE;
3268 #endif /* __COVERITY__ */
3270 /* Rename to the REAL name */
3271 if (g_rename(filename_temp, filename_full) == -1)
3273 purple_debug_error("util", "Error renaming %s to %s: %s\n",
3274 filename_temp, filename_full,
3275 g_strerror(errno));
3278 g_free(filename_temp);
3280 return TRUE;
3283 PurpleXmlNode *
3284 purple_util_read_xml_from_file(const char *filename, const char *description)
3286 return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util");
3290 * Like mkstemp() but returns a file pointer, uses a pre-set template,
3291 * uses the semantics of tempnam() for the directory to use and allocates
3292 * the space for the filepath.
3294 * Caller is responsible for closing the file and removing it when done,
3295 * as well as freeing the space pointed-to by "path" with g_free().
3297 * Returns NULL on failure and cleans up after itself if so.
3299 static const char *purple_mkstemp_templ = {"purpleXXXXXX"};
3301 FILE *
3302 purple_mkstemp(char **fpath, gboolean binary)
3304 const gchar *tmpdir;
3305 int fd;
3306 FILE *fp = NULL;
3308 g_return_val_if_fail(fpath != NULL, NULL);
3310 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
3311 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) {
3312 fd = g_mkstemp(*fpath);
3313 if(fd == -1) {
3314 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3315 "Couldn't make \"%s\", error: %d\n",
3316 *fpath, errno);
3317 } else {
3318 if((fp = fdopen(fd, "r+")) == NULL) {
3319 close(fd);
3320 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3321 "Couldn't fdopen(), error: %d\n", errno);
3325 if(!fp) {
3326 g_free(*fpath);
3327 *fpath = NULL;
3330 } else {
3331 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3332 "g_get_tmp_dir() failed!\n");
3335 return fp;
3338 gboolean
3339 purple_program_is_valid(const char *program)
3341 GError *error = NULL;
3342 char **argv;
3343 gchar *progname;
3344 gboolean is_valid = FALSE;
3346 g_return_val_if_fail(program != NULL, FALSE);
3347 g_return_val_if_fail(*program != '\0', FALSE);
3349 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
3350 purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid",
3351 "Could not parse program '%s': %s\n",
3352 program, error->message);
3353 g_error_free(error);
3354 return FALSE;
3357 if (argv == NULL) {
3358 return FALSE;
3361 progname = g_find_program_in_path(argv[0]);
3362 is_valid = (progname != NULL);
3364 if(purple_debug_is_verbose())
3365 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program,
3366 is_valid ? "Valid" : "Invalid");
3368 g_strfreev(argv);
3369 g_free(progname);
3371 return is_valid;
3375 gboolean
3376 purple_running_gnome(void)
3378 #ifndef _WIN32
3379 gchar *tmp = g_find_program_in_path("gvfs-open");
3381 if (tmp == NULL) {
3382 tmp = g_find_program_in_path("gnome-open");
3384 if (tmp == NULL) {
3385 return FALSE;
3389 g_free(tmp);
3391 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
3393 return ((tmp != NULL) && (*tmp != '\0'));
3394 #else
3395 return FALSE;
3396 #endif
3399 gboolean
3400 purple_running_kde(void)
3402 #ifndef _WIN32
3403 gchar *tmp = g_find_program_in_path("kfmclient");
3404 const char *session;
3406 if (tmp == NULL)
3407 return FALSE;
3408 g_free(tmp);
3410 session = g_getenv("KDE_FULL_SESSION");
3411 if (purple_strequal(session, "true"))
3412 return TRUE;
3414 /* If you run Purple from Konsole under !KDE, this will provide a
3415 * a false positive. Since we do the GNOME checks first, this is
3416 * only a problem if you're running something !(KDE || GNOME) and
3417 * you run Purple from Konsole. This really shouldn't be a problem. */
3418 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
3419 #else
3420 return FALSE;
3421 #endif
3424 gboolean
3425 purple_running_osx(void)
3427 #if defined(__APPLE__)
3428 return TRUE;
3429 #else
3430 return FALSE;
3431 #endif
3434 typedef union purple_sockaddr {
3435 struct sockaddr sa;
3436 struct sockaddr_in sa_in;
3437 #if defined(AF_INET6)
3438 struct sockaddr_in6 sa_in6;
3439 #endif
3440 struct sockaddr_storage sa_stor;
3441 } PurpleSockaddr;
3443 char *
3444 purple_fd_get_ip(int fd)
3446 PurpleSockaddr addr;
3447 socklen_t namelen = sizeof(addr);
3448 int family;
3450 g_return_val_if_fail(fd != 0, NULL);
3452 if (getsockname(fd, &(addr.sa), &namelen))
3453 return NULL;
3455 family = addr.sa.sa_family;
3457 if (family == AF_INET) {
3458 return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
3460 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
3461 else if (family == AF_INET6) {
3462 char host[INET6_ADDRSTRLEN];
3463 const char *tmp;
3465 tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
3466 return g_strdup(tmp);
3468 #endif
3470 return NULL;
3474 purple_socket_get_family(int fd)
3476 PurpleSockaddr addr;
3477 socklen_t len = sizeof(addr);
3479 g_return_val_if_fail(fd >= 0, -1);
3481 if (getsockname(fd, &(addr.sa), &len))
3482 return -1;
3484 return addr.sa.sa_family;
3487 gboolean
3488 purple_socket_speaks_ipv4(int fd)
3490 int family;
3492 g_return_val_if_fail(fd >= 0, FALSE);
3494 family = purple_socket_get_family(fd);
3496 switch (family) {
3497 case AF_INET:
3498 return TRUE;
3499 #if defined(IPV6_V6ONLY)
3500 case AF_INET6:
3502 int val = 0;
3503 socklen_t len = sizeof(val);
3505 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0)
3506 return FALSE;
3507 return !val;
3509 #endif
3510 default:
3511 return FALSE;
3515 /**************************************************************************
3516 * String Functions
3517 **************************************************************************/
3518 gboolean
3519 purple_strequal(const gchar *left, const gchar *right)
3521 return (g_strcmp0(left, right) == 0);
3524 const char *
3525 purple_normalize(const PurpleAccount *account, const char *str)
3527 const char *ret = NULL;
3528 static char buf[BUF_LEN];
3530 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3531 g_return_val_if_fail(str != NULL, "");
3533 if (account != NULL)
3535 PurpleProtocol *protocol =
3536 purple_protocols_find(purple_account_get_protocol_id(account));
3538 if (protocol != NULL)
3539 ret = purple_protocol_client_iface_normalize(protocol, account, str);
3542 if (ret == NULL)
3544 char *tmp;
3546 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
3547 g_snprintf(buf, sizeof(buf), "%s", tmp);
3548 g_free(tmp);
3550 ret = buf;
3553 return ret;
3557 * You probably don't want to call this directly, it is
3558 * mainly for use as a protocol callback function. See the
3559 * comments in util.h.
3561 const char *
3562 purple_normalize_nocase(const PurpleAccount *account, const char *str)
3564 static char buf[BUF_LEN];
3565 char *tmp1, *tmp2;
3567 g_return_val_if_fail(str != NULL, NULL);
3569 tmp1 = g_utf8_strdown(str, -1);
3570 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
3571 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : "");
3572 g_free(tmp2);
3573 g_free(tmp1);
3575 return buf;
3578 gboolean
3579 purple_validate(const PurpleProtocol *protocol, const char *str)
3581 const char *normalized;
3583 g_return_val_if_fail(protocol != NULL, FALSE);
3584 g_return_val_if_fail(str != NULL, FALSE);
3586 if (str[0] == '\0')
3587 return FALSE;
3589 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT_IFACE, normalize))
3590 return TRUE;
3592 normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol),
3593 NULL, str);
3595 return (NULL != normalized);
3598 gchar *
3599 purple_strdup_withhtml(const gchar *src)
3601 gulong destsize, i, j;
3602 gchar *dest;
3604 g_return_val_if_fail(src != NULL, NULL);
3606 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3607 destsize = 1;
3608 for (i = 0; src[i] != '\0'; i++)
3610 if (src[i] == '\n')
3611 destsize += 4;
3612 else if (src[i] != '\r')
3613 destsize++;
3616 dest = g_malloc(destsize);
3618 /* Copy stuff, ignoring \r's, because they are dumb */
3619 for (i = 0, j = 0; src[i] != '\0'; i++) {
3620 if (src[i] == '\n') {
3621 strcpy(&dest[j], "<BR>");
3622 j += 4;
3623 } else if (src[i] != '\r')
3624 dest[j++] = src[i];
3627 dest[destsize-1] = '\0';
3629 return dest;
3632 gboolean
3633 purple_str_has_prefix(const char *s, const char *p)
3635 return g_str_has_prefix(s, p);
3638 gboolean
3639 purple_str_has_caseprefix(const gchar *s, const gchar *p)
3641 g_return_val_if_fail(s, FALSE);
3642 g_return_val_if_fail(p, FALSE);
3644 return (g_ascii_strncasecmp(s, p, strlen(p)) == 0);
3647 gboolean
3648 purple_str_has_suffix(const char *s, const char *x)
3650 return g_str_has_suffix(s, x);
3653 char *
3654 purple_str_add_cr(const char *text)
3656 char *ret = NULL;
3657 int count = 0, j;
3658 guint i;
3660 g_return_val_if_fail(text != NULL, NULL);
3662 if (text[0] == '\n')
3663 count++;
3664 for (i = 1; i < strlen(text); i++)
3665 if (text[i] == '\n' && text[i - 1] != '\r')
3666 count++;
3668 if (count == 0)
3669 return g_strdup(text);
3671 ret = g_malloc0(strlen(text) + count + 1);
3673 i = 0; j = 0;
3674 if (text[i] == '\n')
3675 ret[j++] = '\r';
3676 ret[j++] = text[i++];
3677 for (; i < strlen(text); i++) {
3678 if (text[i] == '\n' && text[i - 1] != '\r')
3679 ret[j++] = '\r';
3680 ret[j++] = text[i];
3683 return ret;
3686 void
3687 purple_str_strip_char(char *text, char thechar)
3689 int i, j;
3691 g_return_if_fail(text != NULL);
3693 for (i = 0, j = 0; text[i]; i++)
3694 if (text[i] != thechar)
3695 text[j++] = text[i];
3697 text[j] = '\0';
3700 void
3701 purple_util_chrreplace(char *string, char delimiter,
3702 char replacement)
3704 int i = 0;
3706 g_return_if_fail(string != NULL);
3708 while (string[i] != '\0')
3710 if (string[i] == delimiter)
3711 string[i] = replacement;
3712 i++;
3716 gchar *
3717 purple_strreplace(const char *string, const char *delimiter,
3718 const char *replacement)
3720 gchar **split;
3721 gchar *ret;
3723 g_return_val_if_fail(string != NULL, NULL);
3724 g_return_val_if_fail(delimiter != NULL, NULL);
3725 g_return_val_if_fail(replacement != NULL, NULL);
3727 split = g_strsplit(string, delimiter, 0);
3728 ret = g_strjoinv(replacement, split);
3729 g_strfreev(split);
3731 return ret;
3734 gchar *
3735 purple_strcasereplace(const char *string, const char *delimiter,
3736 const char *replacement)
3738 gchar *ret;
3739 int length_del, length_rep, i, j;
3741 g_return_val_if_fail(string != NULL, NULL);
3742 g_return_val_if_fail(delimiter != NULL, NULL);
3743 g_return_val_if_fail(replacement != NULL, NULL);
3745 length_del = strlen(delimiter);
3746 length_rep = strlen(replacement);
3748 /* Count how many times the delimiter appears */
3749 i = 0; /* position in the source string */
3750 j = 0; /* number of occurrences of "delimiter" */
3751 while (string[i] != '\0') {
3752 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3753 i += length_del;
3754 j += length_rep;
3755 } else {
3756 i++;
3757 j++;
3761 ret = g_malloc(j+1);
3763 i = 0; /* position in the source string */
3764 j = 0; /* position in the destination string */
3765 while (string[i] != '\0') {
3766 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3767 strncpy(&ret[j], replacement, length_rep);
3768 i += length_del;
3769 j += length_rep;
3770 } else {
3771 ret[j] = string[i];
3772 i++;
3773 j++;
3777 ret[j] = '\0';
3779 return ret;
3782 /** TODO: Expose this when we can add API */
3783 static const char *
3784 purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen)
3786 const char *tmp, *ret;
3788 g_return_val_if_fail(haystack != NULL, NULL);
3789 g_return_val_if_fail(needle != NULL, NULL);
3791 if (hlen == -1)
3792 hlen = strlen(haystack);
3793 if (nlen == -1)
3794 nlen = strlen(needle);
3795 tmp = haystack,
3796 ret = NULL;
3798 g_return_val_if_fail(hlen > 0, NULL);
3799 g_return_val_if_fail(nlen > 0, NULL);
3801 while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) {
3802 if (!g_ascii_strncasecmp(needle, tmp, nlen))
3803 ret = tmp;
3804 else
3805 tmp++;
3808 return ret;
3811 const char *
3812 purple_strcasestr(const char *haystack, const char *needle)
3814 return purple_strcasestr_len(haystack, -1, needle, -1);
3817 char *
3818 purple_str_size_to_units(goffset size)
3820 static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3821 float size_mag;
3822 gsize size_index = 0;
3824 if (size == -1) {
3825 return g_strdup(_("Calculating..."));
3827 else if (size == 0) {
3828 return g_strdup(_("Unknown."));
3830 else {
3831 size_mag = (float)size;
3833 while ((size_index < G_N_ELEMENTS(size_str) - 1) && (size_mag > 1024)) {
3834 size_mag /= 1024;
3835 size_index++;
3838 if (size_index == 0) {
3839 return g_strdup_printf("%" G_GOFFSET_FORMAT " %s", size, _(size_str[size_index]));
3840 } else {
3841 return g_strdup_printf("%.2f %s", size_mag, _(size_str[size_index]));
3846 char *
3847 purple_str_seconds_to_string(guint secs)
3849 char *ret = NULL;
3850 guint days, hrs, mins;
3852 if (secs < 60)
3854 return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs);
3857 days = secs / (60 * 60 * 24);
3858 secs = secs % (60 * 60 * 24);
3859 hrs = secs / (60 * 60);
3860 secs = secs % (60 * 60);
3861 mins = secs / 60;
3862 /* secs = secs % 60; */
3864 if (days > 0)
3866 ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days);
3869 if (hrs > 0)
3871 if (ret != NULL)
3873 char *tmp = g_strdup_printf(
3874 dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs),
3875 ret, hrs);
3876 g_free(ret);
3877 ret = tmp;
3879 else
3880 ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs);
3883 if (mins > 0)
3885 if (ret != NULL)
3887 char *tmp = g_strdup_printf(
3888 dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins),
3889 ret, mins);
3890 g_free(ret);
3891 ret = tmp;
3893 else
3894 ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins);
3897 return ret;
3901 char *
3902 purple_str_binary_to_ascii(const unsigned char *binary, guint len)
3904 GString *ret;
3905 guint i;
3907 g_return_val_if_fail(len > 0, NULL);
3909 ret = g_string_sized_new(len);
3911 for (i = 0; i < len; i++)
3912 if (binary[i] < 32 || binary[i] > 126)
3913 g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF);
3914 else if (binary[i] == '\\')
3915 g_string_append(ret, "\\\\");
3916 else
3917 g_string_append_c(ret, binary[i]);
3919 return g_string_free(ret, FALSE);
3922 size_t
3923 purple_utf16_size(const gunichar2 *str)
3925 /* UTF16 cannot contain two consequent NUL bytes starting at even
3926 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3927 * Chapter 2.
3930 size_t i = 0;
3932 g_return_val_if_fail(str != NULL, 0);
3934 while (str[i++]);
3936 return i * sizeof(gunichar2);
3939 void
3940 purple_str_wipe(gchar *str)
3942 if (str == NULL)
3943 return;
3944 memset(str, 0, strlen(str));
3945 g_free(str);
3948 void
3949 purple_utf16_wipe(gunichar2 *str)
3951 if (str == NULL)
3952 return;
3953 memset(str, 0, purple_utf16_size(str));
3954 g_free(str);
3957 /**************************************************************************
3958 * URI/URL Functions
3959 **************************************************************************/
3961 void purple_got_protocol_handler_uri(const char *uri)
3963 char proto[11];
3964 char delimiter;
3965 const char *tmp, *param_string;
3966 char *cmd;
3967 GHashTable *params = NULL;
3968 gsize len;
3969 if (!(tmp = strchr(uri, ':')) || tmp == uri) {
3970 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3971 return;
3974 len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri));
3976 strncpy(proto, uri, len);
3977 proto[len] = '\0';
3979 tmp++;
3981 if (g_str_equal(proto, "xmpp"))
3982 delimiter = ';';
3983 else
3984 delimiter = '&';
3986 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter);
3988 if ((param_string = strchr(tmp, '?'))) {
3989 const char *keyend = NULL, *pairstart;
3990 char *key, *value = NULL;
3992 cmd = g_strndup(tmp, (param_string - tmp));
3993 param_string++;
3995 params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
3996 pairstart = tmp = param_string;
3998 while (*tmp || *pairstart) {
3999 if (*tmp == delimiter || !(*tmp)) {
4000 /* If there is no explicit value */
4001 if (keyend == NULL) {
4002 keyend = tmp;
4004 /* without these brackets, clang won't
4005 * recognize tmp as a non-NULL
4008 if (keyend && keyend != pairstart) {
4009 char *p;
4010 key = g_strndup(pairstart, (keyend - pairstart));
4011 /* If there is an explicit value */
4012 if (keyend != tmp && keyend != (tmp - 1))
4013 value = g_strndup(keyend + 1, (tmp - keyend - 1));
4014 for (p = key; *p; ++p)
4015 *p = g_ascii_tolower(*p);
4016 g_hash_table_insert(params, key, value);
4018 keyend = value = NULL;
4019 pairstart = (*tmp) ? tmp + 1 : tmp;
4020 } else if (*tmp == '=')
4021 keyend = tmp;
4023 if (*tmp)
4024 tmp++;
4026 } else
4027 cmd = g_strdup(tmp);
4029 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params);
4031 g_free(cmd);
4032 if (params)
4033 g_hash_table_destroy(params);
4036 const char *
4037 purple_url_decode(const char *str)
4039 static char buf[BUF_LEN];
4040 guint i, j = 0;
4041 char *bum;
4042 char hex[3];
4044 g_return_val_if_fail(str != NULL, NULL);
4047 * XXX - This check could be removed and buf could be made
4048 * dynamically allocated, but this is easier.
4050 if (strlen(str) >= BUF_LEN)
4051 return NULL;
4053 for (i = 0; i < strlen(str); i++) {
4055 if (str[i] != '%')
4056 buf[j++] = str[i];
4057 else {
4058 strncpy(hex, str + ++i, 2);
4059 hex[2] = '\0';
4061 /* i is pointing to the start of the number */
4062 i++;
4065 * Now it's at the end and at the start of the for loop
4066 * will be at the next character.
4068 buf[j++] = strtol(hex, NULL, 16);
4072 buf[j] = '\0';
4074 if (!g_utf8_validate(buf, -1, (const char **)&bum))
4075 *bum = '\0';
4077 return buf;
4080 const char *
4081 purple_url_encode(const char *str)
4083 const char *iter;
4084 static char buf[BUF_LEN];
4085 char utf_char[6];
4086 guint i, j = 0;
4088 g_return_val_if_fail(str != NULL, NULL);
4089 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4091 iter = str;
4092 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4093 gunichar c = g_utf8_get_char(iter);
4094 /* If the character is an ASCII character and is alphanumeric
4095 * no need to escape */
4096 if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
4097 buf[j++] = c;
4098 } else {
4099 int bytes = g_unichar_to_utf8(c, utf_char);
4100 for (i = 0; (int)i < bytes; i++) {
4101 if (j > (BUF_LEN - 4))
4102 break;
4103 if (i >= sizeof(utf_char)) {
4104 g_warn_if_reached();
4105 break;
4107 sprintf(buf + j, "%%%02X", utf_char[i] & 0xff);
4108 j += 3;
4113 buf[j] = '\0';
4115 return buf;
4118 /* Originally lifted from
4119 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
4120 * ... and slightly modified to be a bit more rfc822 compliant
4121 * ... and modified a bit more to make domain checking rfc1035 compliant
4122 * with the exception permitted in rfc1101 for domains to start with digit
4123 * but not completely checking to avoid conflicts with IP addresses
4125 gboolean
4126 purple_email_is_valid(const char *address)
4128 const char *c, *domain;
4129 static char *rfc822_specials = "()<>@,;:\\\"[]";
4131 g_return_val_if_fail(address != NULL, FALSE);
4133 if (*address == '.') return FALSE;
4135 /* first we validate the name portion (name@domain) (rfc822)*/
4136 for (c = address; *c; c++) {
4137 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
4138 while (*++c) {
4139 if (*c == '\\') {
4140 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue;
4141 else return FALSE;
4143 if (*c == '\"') break;
4144 if (*c < ' ' || *c >= 127) return FALSE;
4146 if (!*c++) return FALSE;
4147 if (*c == '@') break;
4148 if (*c != '.') return FALSE;
4149 continue;
4151 if (*c == '@') break;
4152 if (*c <= ' ' || *c >= 127) return FALSE;
4153 if (strchr(rfc822_specials, *c)) return FALSE;
4156 /* It's obviously not an email address if we didn't find an '@' above */
4157 if (*c == '\0') return FALSE;
4159 /* strictly we should return false if (*(c - 1) == '.') too, but I think
4160 * we should permit user.@domain type addresses - they do work :) */
4161 if (c == address) return FALSE;
4163 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
4164 if (!*(domain = ++c)) return FALSE;
4165 do {
4166 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
4167 return FALSE;
4168 if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE;
4169 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
4170 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
4171 } while (*++c);
4173 if (*(c - 1) == '-') return FALSE;
4175 return ((c - domain) > 3 ? TRUE : FALSE);
4178 gboolean
4179 purple_ipv4_address_is_valid(const char *ip)
4181 int c, o1, o2, o3, o4;
4182 char end;
4184 g_return_val_if_fail(ip != NULL, FALSE);
4186 c = sscanf(ip, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, &end);
4187 if (c != 4 || o1 < 0 || o1 > 255 || o2 < 0 || o2 > 255 || o3 < 0 || o3 > 255 || o4 < 0 || o4 > 255)
4188 return FALSE;
4189 return TRUE;
4192 gboolean
4193 purple_ipv6_address_is_valid(const gchar *ip)
4195 const gchar *c;
4196 gboolean double_colon = FALSE;
4197 gint chunks = 1;
4198 gint in = 0;
4200 g_return_val_if_fail(ip != NULL, FALSE);
4202 if (*ip == '\0')
4203 return FALSE;
4205 for (c = ip; *c; ++c) {
4206 if ((*c >= '0' && *c <= '9') ||
4207 (*c >= 'a' && *c <= 'f') ||
4208 (*c >= 'A' && *c <= 'F')) {
4209 if (++in > 4)
4210 /* Only four hex digits per chunk */
4211 return FALSE;
4212 continue;
4213 } else if (*c == ':') {
4214 /* The start of a new chunk */
4215 ++chunks;
4216 in = 0;
4217 if (*(c + 1) == ':') {
4219 * '::' indicates a consecutive series of chunks full
4220 * of zeroes. There can be only one of these per address.
4222 if (double_colon)
4223 return FALSE;
4224 double_colon = TRUE;
4226 } else
4227 return FALSE;
4231 * Either we saw a '::' and there were fewer than 8 chunks -or-
4232 * we didn't see a '::' and saw exactly 8 chunks.
4234 return (double_colon && chunks < 8) || (!double_colon && chunks == 8);
4237 gboolean
4238 purple_ip_address_is_valid(const char *ip)
4240 return (purple_ipv4_address_is_valid(ip) || purple_ipv6_address_is_valid(ip));
4243 /* Stolen from gnome_uri_list_extract_uris */
4244 GList *
4245 purple_uri_list_extract_uris(const gchar *uri_list)
4247 const gchar *p, *q;
4248 gchar *retval;
4249 GList *result = NULL;
4251 g_return_val_if_fail (uri_list != NULL, NULL);
4253 p = uri_list;
4255 /* We don't actually try to validate the URI according to RFC
4256 * 2396, or even check for allowed characters - we just ignore
4257 * comments and trim whitespace off the ends. We also
4258 * allow LF delimination as well as the specified CRLF.
4260 while (p) {
4261 if (*p != '#') {
4262 while (isspace(*p))
4263 p++;
4265 q = p;
4266 while (*q && (*q != '\n') && (*q != '\r'))
4267 q++;
4269 if (q > p) {
4270 q--;
4271 while (q > p && isspace(*q))
4272 q--;
4274 retval = (gchar*)g_malloc (q - p + 2);
4275 strncpy (retval, p, q - p + 1);
4276 retval[q - p + 1] = '\0';
4278 result = g_list_prepend (result, retval);
4281 p = strchr (p, '\n');
4282 if (p)
4283 p++;
4286 return g_list_reverse (result);
4290 /* Stolen from gnome_uri_list_extract_filenames */
4291 GList *
4292 purple_uri_list_extract_filenames(const gchar *uri_list)
4294 GList *tmp_list, *node, *result;
4296 g_return_val_if_fail (uri_list != NULL, NULL);
4298 result = purple_uri_list_extract_uris(uri_list);
4300 tmp_list = result;
4301 while (tmp_list) {
4302 gchar *s = (gchar*)tmp_list->data;
4304 node = tmp_list;
4305 tmp_list = tmp_list->next;
4307 if (!strncmp (s, "file:", 5)) {
4308 node->data = g_filename_from_uri (s, NULL, NULL);
4309 /* not sure if this fallback is useful at all */
4310 if (!node->data) node->data = g_strdup (s+5);
4311 } else {
4312 result = g_list_delete_link(result, node);
4314 g_free (s);
4316 return result;
4319 /**************************************************************************
4320 * UTF8 String Functions
4321 **************************************************************************/
4322 gchar *
4323 purple_utf8_try_convert(const char *str)
4325 gsize converted;
4326 gchar *utf8;
4328 g_return_val_if_fail(str != NULL, NULL);
4330 if (g_utf8_validate(str, -1, NULL)) {
4331 return g_strdup(str);
4334 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
4335 if (utf8 != NULL)
4336 return utf8;
4338 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
4339 if ((utf8 != NULL) && (converted == strlen(str)))
4340 return utf8;
4342 g_free(utf8);
4344 return NULL;
4347 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
4348 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
4349 gchar *
4350 purple_utf8_salvage(const char *str)
4352 GString *workstr;
4353 const char *end;
4355 g_return_val_if_fail(str != NULL, NULL);
4357 workstr = g_string_sized_new(strlen(str));
4359 do {
4360 (void)g_utf8_validate(str, -1, &end);
4361 workstr = g_string_append_len(workstr, str, end - str);
4362 str = end;
4363 if (*str == '\0')
4364 break;
4365 do {
4366 workstr = g_string_append_c(workstr, '?');
4367 str++;
4368 } while (!utf8_first(*str));
4369 } while (*str != '\0');
4371 return g_string_free(workstr, FALSE);
4374 gchar *
4375 purple_utf8_strip_unprintables(const gchar *str)
4377 gchar *workstr, *iter;
4378 const gchar *bad;
4380 if (str == NULL)
4381 /* Act like g_strdup */
4382 return NULL;
4384 if (!g_utf8_validate(str, -1, &bad)) {
4385 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
4386 "first bad character was %02x (%c)\n",
4387 str, *bad, *bad);
4388 g_return_val_if_reached(NULL);
4391 workstr = iter = g_new(gchar, strlen(str) + 1);
4392 while (*str) {
4393 gunichar ch = g_utf8_get_char(str);
4394 gchar *next = g_utf8_next_char(str);
4396 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
4397 * [#x10000-#x10FFFF]
4399 if ((ch == '\t' || ch == '\n' || ch == '\r') ||
4400 (ch >= 0x20 && ch <= 0xD7FF) ||
4401 (ch >= 0xE000 && ch <= 0xFFFD) ||
4402 (ch >= 0x10000 && ch <= 0x10FFFF)) {
4403 memcpy(iter, str, next - str);
4404 iter += (next - str);
4407 str = next;
4410 /* nul-terminate the new string */
4411 *iter = '\0';
4413 return workstr;
4417 * This function is copied from g_strerror() but changed to use
4418 * gai_strerror().
4420 const gchar *
4421 purple_gai_strerror(gint errnum)
4423 static GPrivate msg_private = G_PRIVATE_INIT(g_free);
4424 char *msg;
4425 int saved_errno = errno;
4427 const char *msg_locale;
4429 msg_locale = gai_strerror(errnum);
4430 if (g_get_charset(NULL))
4432 /* This string is already UTF-8--great! */
4433 errno = saved_errno;
4434 return msg_locale;
4436 else
4438 gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
4439 if (msg_utf8)
4441 /* Stick in the quark table so that we can return a static result */
4442 GQuark msg_quark = g_quark_from_string(msg_utf8);
4443 g_free(msg_utf8);
4445 msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
4446 errno = saved_errno;
4447 return msg_utf8;
4451 msg = g_private_get(&msg_private);
4453 if (!msg)
4455 msg = g_new(gchar, 64);
4456 g_private_set(&msg_private, msg);
4459 sprintf(msg, "unknown error (%d)", errnum);
4461 errno = saved_errno;
4462 return msg;
4465 char *
4466 purple_utf8_ncr_encode(const char *str)
4468 GString *out;
4470 g_return_val_if_fail(str != NULL, NULL);
4471 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4473 out = g_string_new("");
4475 for(; *str; str = g_utf8_next_char(str)) {
4476 gunichar wc = g_utf8_get_char(str);
4478 /* super simple check. hopefully not too wrong. */
4479 if(wc >= 0x80) {
4480 g_string_append_printf(out, "&#%u;", (guint32) wc);
4481 } else {
4482 g_string_append_unichar(out, wc);
4486 return g_string_free(out, FALSE);
4490 char *
4491 purple_utf8_ncr_decode(const char *str)
4493 GString *out;
4494 char *buf, *b;
4496 g_return_val_if_fail(str != NULL, NULL);
4497 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4499 buf = (char *) str;
4500 out = g_string_new("");
4502 while( (b = strstr(buf, "&#")) ) {
4503 gunichar wc;
4504 int base = 0;
4506 /* append everything leading up to the &# */
4507 g_string_append_len(out, buf, b-buf);
4509 b += 2; /* skip past the &# */
4511 /* strtoul will treat 0x prefix as hex, but not just x */
4512 if(*b == 'x' || *b == 'X') {
4513 base = 16;
4514 b++;
4517 /* advances buf to the end of the ncr segment */
4518 wc = (gunichar) strtoul(b, &buf, base);
4520 /* this mimics the previous impl of ncr_decode */
4521 if(*buf == ';') {
4522 g_string_append_unichar(out, wc);
4523 buf++;
4527 /* append whatever's left */
4528 g_string_append(out, buf);
4530 return g_string_free(out, FALSE);
4535 purple_utf8_strcasecmp(const char *a, const char *b)
4537 char *a_norm = NULL;
4538 char *b_norm = NULL;
4539 int ret = -1;
4541 if(!a && b)
4542 return -1;
4543 else if(!b && a)
4544 return 1;
4545 else if(!a && !b)
4546 return 0;
4548 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
4550 purple_debug_error("purple_utf8_strcasecmp",
4551 "One or both parameters are invalid UTF8\n");
4552 return ret;
4555 a_norm = g_utf8_casefold(a, -1);
4556 b_norm = g_utf8_casefold(b, -1);
4557 ret = g_utf8_collate(a_norm, b_norm);
4558 g_free(a_norm);
4559 g_free(b_norm);
4561 return ret;
4564 /* previously conversation::find_nick() */
4565 gboolean
4566 purple_utf8_has_word(const char *haystack, const char *needle)
4568 char *hay, *pin, *p;
4569 const char *start, *prev_char;
4570 gunichar before, after;
4571 int n;
4572 gboolean ret = FALSE;
4574 start = hay = g_utf8_strdown(haystack, -1);
4576 pin = g_utf8_strdown(needle, -1);
4577 n = strlen(pin);
4579 while ((p = strstr(start, pin)) != NULL) {
4580 prev_char = g_utf8_find_prev_char(hay, p);
4581 before = -2;
4582 if (prev_char) {
4583 before = g_utf8_get_char(prev_char);
4585 after = g_utf8_get_char_validated(p + n, - 1);
4587 if ((p == hay ||
4588 /* The character before is a reasonable guess for a word boundary
4589 ("!g_unichar_isalnum()" is not a valid way to determine word
4590 boundaries, but it is the only reasonable thing to do here),
4591 and isn't the '&' from a "&amp;" or some such entity*/
4592 (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&'))
4593 && after != (gunichar)-2 && !g_unichar_isalnum(after)) {
4594 ret = TRUE;
4595 break;
4597 start = p + 1;
4600 g_free(pin);
4601 g_free(hay);
4603 return ret;
4606 void
4607 purple_print_utf8_to_console(FILE *filestream, char *message)
4609 gchar *message_conv;
4610 GError *error = NULL;
4612 /* Try to convert 'message' to user's locale */
4613 message_conv = g_locale_from_utf8(message, -1, NULL, NULL, &error);
4614 if (message_conv != NULL) {
4615 fputs(message_conv, filestream);
4616 g_free(message_conv);
4618 else
4620 /* use 'message' as a fallback */
4621 g_warning("%s\n", error->message);
4622 g_error_free(error);
4623 fputs(message, filestream);
4627 gboolean purple_message_meify(char *message, gssize len)
4629 char *c;
4630 gboolean inside_html = FALSE;
4632 g_return_val_if_fail(message != NULL, FALSE);
4634 if(len == -1)
4635 len = strlen(message);
4637 for (c = message; *c; c++, len--) {
4638 if(inside_html) {
4639 if(*c == '>')
4640 inside_html = FALSE;
4641 } else {
4642 if(*c == '<')
4643 inside_html = TRUE;
4644 else
4645 break;
4649 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
4650 memmove(c, c+4, len-3);
4651 return TRUE;
4654 return FALSE;
4657 char *purple_text_strip_mnemonic(const char *in)
4659 char *out;
4660 char *a;
4661 char *a0;
4662 const char *b;
4664 g_return_val_if_fail(in != NULL, NULL);
4666 out = g_malloc(strlen(in)+1);
4667 a = out;
4668 b = in;
4670 a0 = a; /* The last non-space char seen so far, or the first char */
4672 while(*b) {
4673 if(*b == '_') {
4674 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
4675 /* Detected CJK style shortcut (Bug 875311) */
4676 a = a0; /* undo the left parenthesis */
4677 b += 3; /* and skip the whole mess */
4678 } else if(*(b+1) == '_') {
4679 *(a++) = '_';
4680 b += 2;
4681 a0 = a;
4682 } else {
4683 b++;
4685 /* We don't want to corrupt the middle of UTF-8 characters */
4686 } else if (!(*b & 0x80)) { /* other 1-byte char */
4687 if (*b != ' ')
4688 a0 = a;
4689 *(a++) = *(b++);
4690 } else {
4691 /* Multibyte utf8 char, don't look for _ inside these */
4692 int n = 0;
4693 int i;
4694 if ((*b & 0xe0) == 0xc0) {
4695 n = 2;
4696 } else if ((*b & 0xf0) == 0xe0) {
4697 n = 3;
4698 } else if ((*b & 0xf8) == 0xf0) {
4699 n = 4;
4700 } else if ((*b & 0xfc) == 0xf8) {
4701 n = 5;
4702 } else if ((*b & 0xfe) == 0xfc) {
4703 n = 6;
4704 } else { /* Illegal utf8 */
4705 n = 1;
4707 a0 = a; /* unless we want to delete CJK spaces too */
4708 for (i = 0; i < n && *b; i += 1) {
4709 *(a++) = *(b++);
4713 *a = '\0';
4715 return out;
4718 const char* purple_unescape_filename(const char *escaped) {
4719 return purple_url_decode(escaped);
4723 /* this is almost identical to purple_url_encode (hence purple_url_decode
4724 * being used above), but we want to keep certain characters unescaped
4725 * for compat reasons */
4726 const char *
4727 purple_escape_filename(const char *str)
4729 const char *iter;
4730 static char buf[BUF_LEN];
4731 char utf_char[6];
4732 guint i, j = 0;
4734 g_return_val_if_fail(str != NULL, NULL);
4735 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4737 iter = str;
4738 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4739 gunichar c = g_utf8_get_char(iter);
4740 /* If the character is an ASCII character and is alphanumeric,
4741 * or one of the specified values, no need to escape */
4742 if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' ||
4743 c == '_' || c == '.' || c == '#')) {
4744 buf[j++] = c;
4745 } else {
4746 int bytes = g_unichar_to_utf8(c, utf_char);
4747 for (i = 0; (int)i < bytes; i++) {
4748 if (j > (BUF_LEN - 4))
4749 break;
4750 if (i >= sizeof(utf_char)) {
4751 g_warn_if_reached();
4752 break;
4754 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
4755 j += 3;
4759 #ifdef _WIN32
4760 /* File/Directory names in windows cannot end in periods/spaces.
4761 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4763 while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' '))
4764 j--;
4765 #endif
4766 buf[j] = '\0';
4768 return buf;
4771 gchar * purple_escape_js(const gchar *str)
4773 gchar *escaped;
4775 json_node_set_string(escape_js_node, str);
4776 json_generator_set_root(escape_js_gen, escape_js_node);
4777 escaped = json_generator_to_data(escape_js_gen, NULL);
4778 json_node_set_boolean(escape_js_node, FALSE);
4780 return escaped;
4783 void purple_restore_default_signal_handlers(void)
4785 #ifndef _WIN32
4786 signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */
4787 signal(SIGINT, SIG_DFL); /* 2: interrupt program */
4788 signal(SIGQUIT, SIG_DFL); /* 3: quit program */
4789 signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */
4790 signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */
4791 signal(SIGABRT, SIG_DFL); /* 6: abort program */
4793 #ifdef SIGPOLL
4794 signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */
4795 #endif /* SIGPOLL */
4797 #ifdef SIGEMT
4798 signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */
4799 #endif /* SIGEMT */
4801 signal(SIGFPE, SIG_DFL); /* 8: floating point exception */
4802 signal(SIGBUS, SIG_DFL); /* 10: bus error */
4803 signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */
4804 signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */
4805 signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */
4806 signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */
4807 signal(SIGTERM, SIG_DFL); /* 15: software termination signal */
4808 signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */
4809 signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */
4810 signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */
4811 #endif /* !_WIN32 */
4814 static void
4815 set_status_with_attrs(PurpleStatus *status, ...)
4817 va_list args;
4818 va_start(args, status);
4819 purple_status_set_active_with_attrs(status, TRUE, args);
4820 va_end(args);
4823 void purple_util_set_current_song(const char *title, const char *artist, const char *album)
4825 GList *list = purple_accounts_get_all();
4826 for (; list; list = list->next) {
4827 PurplePresence *presence;
4828 PurpleStatus *tune;
4829 PurpleAccount *account = list->data;
4830 if (!purple_account_get_enabled(account, purple_core_get_ui()))
4831 continue;
4833 presence = purple_account_get_presence(account);
4834 tune = purple_presence_get_status(presence, "tune");
4835 if (!tune)
4836 continue;
4837 if (title) {
4838 set_status_with_attrs(tune,
4839 PURPLE_TUNE_TITLE, title,
4840 PURPLE_TUNE_ARTIST, artist,
4841 PURPLE_TUNE_ALBUM, album,
4842 NULL);
4843 } else {
4844 purple_status_set_active(tune, FALSE);
4849 char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused)
4851 GString *string;
4852 char *esc;
4854 if (!title || !*title)
4855 return NULL;
4857 esc = g_markup_escape_text(title, -1);
4858 string = g_string_new("");
4859 g_string_append_printf(string, "%s", esc);
4860 g_free(esc);
4862 if (artist && *artist) {
4863 esc = g_markup_escape_text(artist, -1);
4864 g_string_append_printf(string, _(" - %s"), esc);
4865 g_free(esc);
4868 if (album && *album) {
4869 esc = g_markup_escape_text(album, -1);
4870 g_string_append_printf(string, _(" (%s)"), esc);
4871 g_free(esc);
4874 return g_string_free(string, FALSE);
4877 const gchar *
4878 purple_get_host_name(void)
4880 return g_get_host_name();
4883 gchar *
4884 purple_uuid_random(void)
4886 guint32 tmp, a, b;
4888 tmp = g_random_int();
4889 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */
4890 tmp >>= 12;
4891 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */
4893 tmp = g_random_int();
4895 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4896 g_random_int(),
4897 tmp & 0xFFFF,
4900 (tmp >> 16) & 0xFFFF, g_random_int());
4903 void purple_callback_set_zero(gpointer data)
4905 gpointer *ptr = data;
4907 g_return_if_fail(ptr != NULL);
4909 *ptr = NULL;
4912 GValue *
4913 purple_value_new(GType type)
4915 GValue *ret;
4917 g_return_val_if_fail(type != G_TYPE_NONE, NULL);
4919 ret = g_new0(GValue, 1);
4920 g_value_init(ret, type);
4922 return ret;
4925 GValue *
4926 purple_value_dup(GValue *value)
4928 GValue *ret;
4930 g_return_val_if_fail(value != NULL, NULL);
4932 ret = g_new0(GValue, 1);
4933 g_value_init(ret, G_VALUE_TYPE(value));
4934 g_value_copy(value, ret);
4936 return ret;
4939 void
4940 purple_value_free(GValue *value)
4942 g_return_if_fail(value != NULL);
4944 g_value_unset(value);
4945 g_free(value);
4948 gchar *purple_http_digest_calculate_session_key(
4949 const gchar *algorithm,
4950 const gchar *username,
4951 const gchar *realm,
4952 const gchar *password,
4953 const gchar *nonce,
4954 const gchar *client_nonce)
4956 PurpleHash *hasher;
4957 gchar hash[33]; /* We only support MD5. */
4958 gboolean digest_ok;
4960 g_return_val_if_fail(username != NULL, NULL);
4961 g_return_val_if_fail(realm != NULL, NULL);
4962 g_return_val_if_fail(password != NULL, NULL);
4963 g_return_val_if_fail(nonce != NULL, NULL);
4965 /* Check for a supported algorithm. */
4966 g_return_val_if_fail(algorithm == NULL ||
4967 *algorithm == '\0' ||
4968 g_ascii_strcasecmp(algorithm, "MD5") ||
4969 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
4971 hasher = purple_md5_hash_new();
4972 g_return_val_if_fail(hash != NULL, NULL);
4974 purple_hash_append(hasher, (guchar *)username, strlen(username));
4975 purple_hash_append(hasher, (guchar *)":", 1);
4976 purple_hash_append(hasher, (guchar *)realm, strlen(realm));
4977 purple_hash_append(hasher, (guchar *)":", 1);
4978 purple_hash_append(hasher, (guchar *)password, strlen(password));
4980 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
4982 guchar digest[16];
4984 if (client_nonce == NULL)
4986 g_object_unref(hasher);
4987 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
4988 return NULL;
4991 purple_hash_digest(hasher, digest, sizeof(digest));
4993 purple_hash_reset(hasher);
4994 purple_hash_append(hasher, digest, sizeof(digest));
4995 purple_hash_append(hasher, (guchar *)":", 1);
4996 purple_hash_append(hasher, (guchar *)nonce, strlen(nonce));
4997 purple_hash_append(hasher, (guchar *)":", 1);
4998 purple_hash_append(hasher, (guchar *)client_nonce, strlen(client_nonce));
5001 digest_ok = purple_hash_digest_to_str(hasher, hash, sizeof(hash));
5002 g_object_unref(hasher);
5004 g_return_val_if_fail(digest_ok, NULL);
5006 return g_strdup(hash);
5009 gchar *purple_http_digest_calculate_response(
5010 const gchar *algorithm,
5011 const gchar *method,
5012 const gchar *digest_uri,
5013 const gchar *qop,
5014 const gchar *entity,
5015 const gchar *nonce,
5016 const gchar *nonce_count,
5017 const gchar *client_nonce,
5018 const gchar *session_key)
5020 PurpleHash *hash;
5021 static gchar hash2[33]; /* We only support MD5. */
5022 gboolean digest_ok;
5024 g_return_val_if_fail(method != NULL, NULL);
5025 g_return_val_if_fail(digest_uri != NULL, NULL);
5026 g_return_val_if_fail(nonce != NULL, NULL);
5027 g_return_val_if_fail(session_key != NULL, NULL);
5029 /* Check for a supported algorithm. */
5030 g_return_val_if_fail(algorithm == NULL ||
5031 *algorithm == '\0' ||
5032 g_ascii_strcasecmp(algorithm, "MD5") ||
5033 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
5035 /* Check for a supported "quality of protection". */
5036 g_return_val_if_fail(qop == NULL ||
5037 *qop == '\0' ||
5038 g_ascii_strcasecmp(qop, "auth") ||
5039 g_ascii_strcasecmp(qop, "auth-int"), NULL);
5041 hash = purple_md5_hash_new();
5042 g_return_val_if_fail(hash != NULL, NULL);
5044 purple_hash_append(hash, (guchar *)method, strlen(method));
5045 purple_hash_append(hash, (guchar *)":", 1);
5046 purple_hash_append(hash, (guchar *)digest_uri, strlen(digest_uri));
5048 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
5050 PurpleHash *hash2;
5051 gchar entity_hash[33];
5053 if (entity == NULL)
5055 g_object_unref(hash);
5056 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
5057 return NULL;
5060 hash2 = purple_md5_hash_new();
5061 purple_hash_append(hash2, (guchar *)entity, strlen(entity));
5062 digest_ok = purple_hash_digest_to_str(hash2, entity_hash, sizeof(entity_hash));
5063 g_object_unref(hash2);
5065 if (!digest_ok) {
5066 g_object_unref(hash);
5067 g_return_val_if_reached(NULL);
5070 purple_hash_append(hash, (guchar *)":", 1);
5071 purple_hash_append(hash, (guchar *)entity_hash, strlen(entity_hash));
5074 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5075 purple_hash_reset(hash);
5077 if (!digest_ok) {
5078 g_object_unref(hash);
5079 g_return_val_if_reached(NULL);
5082 purple_hash_append(hash, (guchar *)session_key, strlen(session_key));
5083 purple_hash_append(hash, (guchar *)":", 1);
5084 purple_hash_append(hash, (guchar *)nonce, strlen(nonce));
5085 purple_hash_append(hash, (guchar *)":", 1);
5087 if (qop != NULL && *qop != '\0')
5089 if (nonce_count == NULL)
5091 g_object_unref(hash);
5092 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
5093 return NULL;
5096 if (client_nonce == NULL)
5098 g_object_unref(hash);
5099 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
5100 return NULL;
5103 purple_hash_append(hash, (guchar *)nonce_count, strlen(nonce_count));
5104 purple_hash_append(hash, (guchar *)":", 1);
5105 purple_hash_append(hash, (guchar *)client_nonce, strlen(client_nonce));
5106 purple_hash_append(hash, (guchar *)":", 1);
5108 purple_hash_append(hash, (guchar *)qop, strlen(qop));
5110 purple_hash_append(hash, (guchar *)":", 1);
5113 purple_hash_append(hash, (guchar *)hash2, strlen(hash2));
5114 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5115 g_object_unref(hash);
5117 g_return_val_if_fail(digest_ok, NULL);
5119 return g_strdup(hash2);
5123 _purple_fstat(int fd, GStatBuf *st)
5125 int ret;
5127 g_return_val_if_fail(st != NULL, -1);
5129 #ifdef _WIN32
5130 ret = _fstat(fd, st);
5131 #else
5132 ret = fstat(fd, st);
5133 #endif
5135 return ret;
5138 #if 0
5140 /* Temporarily removed - re-add this when you need ini file support. */
5142 #define PURPLE_KEY_FILE_DEFAULT_MAX_SIZE 102400
5143 #define PURPLE_KEY_FILE_HARD_LIMIT 10485760
5145 gboolean
5146 purple_key_file_load_from_ini(GKeyFile *key_file, const gchar *file,
5147 gsize max_size)
5149 const gchar *header = "[default]\n\n";
5150 int header_len = strlen(header);
5151 int fd;
5152 GStatBuf st;
5153 gsize file_size, buff_size;
5154 gchar *buff;
5155 GError *error = NULL;
5157 g_return_val_if_fail(key_file != NULL, FALSE);
5158 g_return_val_if_fail(file != NULL, FALSE);
5159 g_return_val_if_fail(max_size < PURPLE_KEY_FILE_HARD_LIMIT, FALSE);
5161 if (max_size == 0)
5162 max_size = PURPLE_KEY_FILE_DEFAULT_MAX_SIZE;
5164 fd = g_open(file, O_RDONLY, S_IREAD);
5165 if (fd == -1) {
5166 purple_debug_error("util", "Failed to read ini file %s", file);
5167 return FALSE;
5170 if (_purple_fstat(fd, &st) != 0) {
5171 purple_debug_error("util", "Failed to fstat ini file %s", file);
5172 return FALSE;
5175 file_size = (st.st_size > max_size) ? max_size : st.st_size;
5177 buff_size = file_size + header_len;
5178 buff = g_new(gchar, buff_size);
5179 memcpy(buff, header, header_len);
5180 if (read(fd, buff + header_len, file_size) != (gssize)file_size) {
5181 purple_debug_error("util",
5182 "Failed to read whole ini file %s", file);
5183 g_close(fd, NULL);
5184 free(buff);
5185 return FALSE;
5187 g_close(fd, NULL);
5189 g_key_file_load_from_data(key_file, buff, buff_size,
5190 G_KEY_FILE_NONE, &error);
5192 free(buff);
5194 if (error) {
5195 purple_debug_error("util", "Failed parsing ini file %s: %s",
5196 file, error->message);
5197 return FALSE;
5200 return TRUE;
5202 #endif