Use common function for getting XDG specific directory
[pidgin-git.git] / libpurple / util.c
blob6f21e9e790a7751f7536b47b2cab792bdc96eccb
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 gchar *cache_dir = NULL;
45 static gchar *config_dir = NULL;
46 static gchar *data_dir = NULL;
48 static JsonNode *escape_js_node = NULL;
49 static JsonGenerator *escape_js_gen = NULL;
51 /* If legacy directory for libpurple exists, move it to location following
52 * xdg base dir spec. https://developer.pidgin.im/ticket/10029
54 static void
55 migrate_to_xdg_base_dirs(void)
57 const char *legacy_purple_dir;
58 gboolean dir_exists;
60 legacy_purple_dir = purple_user_dir();
61 dir_exists = g_file_test(legacy_purple_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
62 if (dir_exists) {
63 purple_move_to_xdg_base_dir(purple_data_dir(), "certificates");
64 purple_move_to_xdg_base_dir(purple_cache_dir(), "icons");
65 purple_move_to_xdg_base_dir(purple_data_dir(), "logs");
66 purple_move_to_xdg_base_dir(purple_data_dir(), "pounces.xml");
69 return;
72 PurpleMenuAction *
73 purple_menu_action_new(const char *label, PurpleCallback callback, gpointer data,
74 GList *children)
76 PurpleMenuAction *act = g_new0(PurpleMenuAction, 1);
77 act->label = g_strdup(label);
78 act->callback = callback;
79 act->data = data;
80 act->children = children;
81 return act;
84 void
85 purple_menu_action_free(PurpleMenuAction *act)
87 g_return_if_fail(act != NULL);
89 g_free(act->stock_icon);
90 g_free(act->label);
91 g_free(act);
94 char * purple_menu_action_get_label(const PurpleMenuAction *act)
96 g_return_val_if_fail(act != NULL, NULL);
98 return act->label;
101 PurpleCallback purple_menu_action_get_callback(const PurpleMenuAction *act)
103 g_return_val_if_fail(act != NULL, NULL);
105 return act->callback;
108 gpointer purple_menu_action_get_data(const PurpleMenuAction *act)
110 g_return_val_if_fail(act != NULL, NULL);
112 return act->data;
115 GList* purple_menu_action_get_children(const PurpleMenuAction *act)
117 g_return_val_if_fail(act != NULL, NULL);
119 return act->children;
122 void purple_menu_action_set_label(PurpleMenuAction *act, char *label)
124 g_return_if_fail(act != NULL);
126 act-> label = label;
129 void purple_menu_action_set_callback(PurpleMenuAction *act, PurpleCallback callback)
131 g_return_if_fail(act != NULL);
133 act->callback = callback;
136 void purple_menu_action_set_data(PurpleMenuAction *act, gpointer data)
138 g_return_if_fail(act != NULL);
140 act->data = data;
143 void purple_menu_action_set_children(PurpleMenuAction *act, GList *children)
145 g_return_if_fail(act != NULL);
147 act->children = children;
150 void purple_menu_action_set_stock_icon(PurpleMenuAction *act,
151 const gchar *stock)
153 g_return_if_fail(act != NULL);
155 g_free(act->stock_icon);
156 act->stock_icon = g_strdup(stock);
159 const gchar *
160 purple_menu_action_get_stock_icon(PurpleMenuAction *act)
162 return act->stock_icon;
165 void
166 purple_util_init(void)
168 escape_js_node = json_node_new(JSON_NODE_VALUE);
169 escape_js_gen = json_generator_new();
170 json_node_set_boolean(escape_js_node, FALSE);
172 migrate_to_xdg_base_dirs();
175 void
176 purple_util_uninit(void)
178 /* Free these so we don't have leaks at shutdown. */
180 g_free(custom_user_dir);
181 custom_user_dir = NULL;
183 g_free(user_dir);
184 user_dir = NULL;
186 g_free(cache_dir);
187 cache_dir = NULL;
189 g_free(config_dir);
190 config_dir = NULL;
192 g_free(data_dir);
193 data_dir = NULL;
195 json_node_free(escape_js_node);
196 escape_js_node = NULL;
198 g_object_unref(escape_js_gen);
199 escape_js_gen = NULL;
202 /**************************************************************************
203 * Base16 Functions
204 **************************************************************************/
205 gchar *
206 purple_base16_encode(const guchar *data, gsize len)
208 gsize i;
209 gchar *ascii = NULL;
211 g_return_val_if_fail(data != NULL, NULL);
212 g_return_val_if_fail(len > 0, NULL);
214 ascii = g_malloc(len * 2 + 1);
216 for (i = 0; i < len; i++)
217 g_snprintf(&ascii[i * 2], 3, "%02x", data[i] & 0xFF);
219 return ascii;
222 guchar *
223 purple_base16_decode(const char *str, gsize *ret_len)
225 gsize len, i, accumulator = 0;
226 guchar *data;
228 g_return_val_if_fail(str != NULL, NULL);
230 len = strlen(str);
232 g_return_val_if_fail(*str, 0);
233 g_return_val_if_fail(len % 2 == 0, 0);
235 data = g_malloc(len / 2);
237 for (i = 0; i < len; i++)
239 if ((i % 2) == 0)
240 accumulator = 0;
241 else
242 accumulator <<= 4;
244 if (isdigit(str[i]))
245 accumulator |= str[i] - 48;
246 else
248 switch(tolower(str[i]))
250 case 'a': accumulator |= 10; break;
251 case 'b': accumulator |= 11; break;
252 case 'c': accumulator |= 12; break;
253 case 'd': accumulator |= 13; break;
254 case 'e': accumulator |= 14; break;
255 case 'f': accumulator |= 15; break;
259 if (i % 2)
260 data[(i - 1) / 2] = accumulator;
263 if (ret_len != NULL)
264 *ret_len = len / 2;
266 return data;
269 gchar *
270 purple_base16_encode_chunked(const guchar *data, gsize len)
272 gsize i;
273 gchar *ascii = NULL;
275 g_return_val_if_fail(data != NULL, NULL);
276 g_return_val_if_fail(len > 0, NULL);
278 /* For each byte of input, we need 2 bytes for the hex representation
279 * and 1 for the colon.
280 * The final colon will be replaced by a terminating NULL
282 ascii = g_malloc(len * 3 + 1);
284 for (i = 0; i < len; i++)
285 g_snprintf(&ascii[i * 3], 4, "%02x:", data[i] & 0xFF);
287 /* Replace the final colon with NULL */
288 ascii[len * 3 - 1] = 0;
290 return ascii;
294 /**************************************************************************
295 * Base64 Functions
296 **************************************************************************/
297 static const char xdigits[] =
298 "0123456789abcdef";
300 gchar *
301 purple_base64_encode(const guchar *data, gsize len)
303 return g_base64_encode(data, len);
306 guchar *
307 purple_base64_decode(const char *str, gsize *ret_len)
310 * We want to allow ret_len to be NULL for backward compatibility,
311 * but g_base64_decode() requires a valid length variable. So if
312 * ret_len is NULL then pass in a dummy variable.
314 gsize unused;
315 return g_base64_decode(str, ret_len != NULL ? ret_len : &unused);
318 /**************************************************************************
319 * Quoted Printable Functions (see RFC 2045).
320 **************************************************************************/
321 guchar *
322 purple_quotedp_decode(const char *str, gsize *ret_len)
324 char *n, *new;
325 const char *end, *p;
327 n = new = g_malloc(strlen (str) + 1);
328 end = str + strlen(str);
330 for (p = str; p < end; p++, n++) {
331 if (*p == '=') {
332 if (p[1] == '\r' && p[2] == '\n') { /* 5.1 #5 */
333 n -= 1;
334 p += 2;
335 } else if (p[1] == '\n') { /* fuzzy case for 5.1 #5 */
336 n -= 1;
337 p += 1;
338 } else if (p[1] && p[2]) {
339 char *nibble1 = strchr(xdigits, tolower(p[1]));
340 char *nibble2 = strchr(xdigits, tolower(p[2]));
341 if (nibble1 && nibble2) { /* 5.1 #1 */
342 *n = ((nibble1 - xdigits) << 4) | (nibble2 - xdigits);
343 p += 2;
344 } else { /* This should never happen */
345 *n = *p;
347 } else { /* This should never happen */
348 *n = *p;
351 else if (*p == '_')
352 *n = ' ';
353 else
354 *n = *p;
357 *n = '\0';
359 if (ret_len != NULL)
360 *ret_len = n - new;
362 /* Resize to take less space */
363 /* new = realloc(new, n - new); */
365 return (guchar *)new;
368 /**************************************************************************
369 * MIME Functions
370 **************************************************************************/
371 char *
372 purple_mime_decode_field(const char *str)
375 * This is wing's version, partially based on revo/shx's version
376 * See RFC2047 [which apparently obsoletes RFC1342]
378 typedef enum {
379 state_start, state_equal1, state_question1,
380 state_charset, state_question2,
381 state_encoding, state_question3,
382 state_encoded_text, state_question4, state_equal2 = state_start
383 } encoded_word_state_t;
384 encoded_word_state_t state = state_start;
385 const char *cur, *mark;
386 const char *charset0 = NULL, *encoding0 = NULL, *encoded_text0 = NULL;
387 GString *new;
389 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
390 #define token_char_p(c) \
391 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
393 /* But encoded-text must be ASCII; alas, isascii() may not exist */
394 #define encoded_text_char_p(c) \
395 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
397 g_return_val_if_fail(str != NULL, NULL);
399 new = g_string_new(NULL);
401 /* Here we will be looking for encoded words and if they seem to be
402 * valid then decode them.
403 * They are of this form: =?charset?encoding?text?=
406 for (cur = str, mark = NULL; *cur; cur += 1) {
407 switch (state) {
408 case state_equal1:
409 if (*cur == '?') {
410 state = state_question1;
411 } else {
412 g_string_append_len(new, mark, cur - mark + 1);
413 state = state_start;
415 break;
416 case state_question1:
417 if (token_char_p(*cur)) {
418 charset0 = cur;
419 state = state_charset;
420 } else { /* This should never happen */
421 g_string_append_len(new, mark, cur - mark + 1);
422 state = state_start;
424 break;
425 case state_charset:
426 if (*cur == '?') {
427 state = state_question2;
428 } else if (!token_char_p(*cur)) { /* This should never happen */
429 g_string_append_len(new, mark, cur - mark + 1);
430 state = state_start;
432 break;
433 case state_question2:
434 if (token_char_p(*cur)) {
435 encoding0 = cur;
436 state = state_encoding;
437 } else { /* This should never happen */
438 g_string_append_len(new, mark, cur - mark + 1);
439 state = state_start;
441 break;
442 case state_encoding:
443 if (*cur == '?') {
444 state = state_question3;
445 } else if (!token_char_p(*cur)) { /* This should never happen */
446 g_string_append_len(new, mark, cur - mark + 1);
447 state = state_start;
449 break;
450 case state_question3:
451 if (encoded_text_char_p(*cur)) {
452 encoded_text0 = cur;
453 state = state_encoded_text;
454 } else if (*cur == '?') { /* empty string */
455 encoded_text0 = cur;
456 state = state_question4;
457 } else { /* This should never happen */
458 g_string_append_len(new, mark, cur - mark + 1);
459 state = state_start;
461 break;
462 case state_encoded_text:
463 if (*cur == '?') {
464 state = state_question4;
465 } else if (!encoded_text_char_p(*cur)) {
466 g_string_append_len(new, mark, cur - mark + 1);
467 state = state_start;
469 break;
470 case state_question4:
471 if (*cur == '=') { /* Got the whole encoded-word */
472 char *charset = g_strndup(charset0, encoding0 - charset0 - 1);
473 char *encoding = g_strndup(encoding0, encoded_text0 - encoding0 - 1);
474 char *encoded_text = g_strndup(encoded_text0, cur - encoded_text0 - 1);
475 guchar *decoded = NULL;
476 gsize dec_len;
477 if (g_ascii_strcasecmp(encoding, "Q") == 0)
478 decoded = purple_quotedp_decode(encoded_text, &dec_len);
479 else if (g_ascii_strcasecmp(encoding, "B") == 0)
480 decoded = purple_base64_decode(encoded_text, &dec_len);
481 else
482 decoded = NULL;
483 if (decoded) {
484 gsize len;
485 char *converted = g_convert((const gchar *)decoded, dec_len, "utf-8", charset, NULL, &len, NULL);
487 if (converted) {
488 g_string_append_len(new, converted, len);
489 g_free(converted);
491 g_free(decoded);
493 g_free(charset);
494 g_free(encoding);
495 g_free(encoded_text);
496 state = state_equal2; /* Restart the FSM */
497 } else { /* This should never happen */
498 g_string_append_len(new, mark, cur - mark + 1);
499 state = state_start;
501 break;
502 default:
503 if (*cur == '=') {
504 mark = cur;
505 state = state_equal1;
506 } else {
507 /* Some unencoded text. */
508 g_string_append_c(new, *cur);
510 break;
511 } /* switch */
512 } /* for */
514 if (state != state_start)
515 g_string_append_len(new, mark, cur - mark + 1);
517 return g_string_free(new, FALSE);;
521 /**************************************************************************
522 * Date/Time Functions
523 **************************************************************************/
525 const char *purple_get_tzoff_str(const struct tm *tm, gboolean iso)
527 static char buf[7];
528 long off;
529 gint8 min;
530 gint8 hrs;
531 struct tm new_tm = *tm;
533 mktime(&new_tm);
535 if (new_tm.tm_isdst < 0)
536 g_return_val_if_reached("");
538 #ifdef _WIN32
539 if ((off = wpurple_get_tz_offset()) == -1)
540 return "";
541 #elif defined(HAVE_TM_GMTOFF)
542 off = new_tm.tm_gmtoff;
543 #elif defined(HAVE_TIMEZONE)
544 tzset();
545 off = -1 * timezone;
546 #else
547 purple_debug_warning("util",
548 "there is no possibility to obtain tz offset");
549 return "";
550 #endif
552 min = (off / 60) % 60;
553 hrs = ((off / 60) - min) / 60;
555 if(iso) {
556 if (0 == off) {
557 strcpy(buf, "Z");
558 } else {
559 /* please leave the colons...they're optional for iso, but jabber
560 * wants them */
561 if(g_snprintf(buf, sizeof(buf), "%+03d:%02d", hrs, ABS(min)) > 6)
562 g_return_val_if_reached("");
564 } else {
565 if (g_snprintf(buf, sizeof(buf), "%+03d%02d", hrs, ABS(min)) > 5)
566 g_return_val_if_reached("");
569 return buf;
572 /* Windows doesn't HAVE_STRFTIME_Z_FORMAT, but this seems clearer. -- rlaager */
573 #if !defined(HAVE_STRFTIME_Z_FORMAT) || defined(_WIN32)
574 static size_t purple_internal_strftime(char *s, size_t max, const char *format, const struct tm *tm)
576 const char *start;
577 const char *c;
578 char *fmt = NULL;
580 /* Yes, this is checked in purple_utf8_strftime(),
581 * but better safe than sorry. -- rlaager */
582 g_return_val_if_fail(format != NULL, 0);
584 /* This is fairly efficient, and it only gets
585 * executed on Windows or if the underlying
586 * system doesn't support the %z format string,
587 * for strftime() so I think it's good enough.
588 * -- rlaager */
589 for (c = start = format; *c ; c++)
591 if (*c != '%')
592 continue;
594 c++;
596 #ifndef HAVE_STRFTIME_Z_FORMAT
597 if (*c == 'z')
599 char *tmp = g_strdup_printf("%s%.*s%s",
600 fmt ? fmt : "",
601 (int)(c - start - 1),
602 start,
603 purple_get_tzoff_str(tm, FALSE));
604 g_free(fmt);
605 fmt = tmp;
606 start = c + 1;
608 #endif
609 #ifdef _WIN32
610 if (*c == 'Z')
612 char *tmp = g_strdup_printf("%s%.*s%s",
613 fmt ? fmt : "",
614 (int)(c - start - 1),
615 start,
616 wpurple_get_timezone_abbreviation(tm));
617 g_free(fmt);
618 fmt = tmp;
619 start = c + 1;
621 #endif
624 if (fmt != NULL)
626 size_t ret;
628 if (*start)
630 char *tmp = g_strconcat(fmt, start, NULL);
631 g_free(fmt);
632 fmt = tmp;
635 ret = strftime(s, max, fmt, tm);
636 g_free(fmt);
638 return ret;
641 return strftime(s, max, format, tm);
643 #else /* HAVE_STRFTIME_Z_FORMAT && !_WIN32 */
644 #define purple_internal_strftime strftime
645 #endif
647 const char *
648 purple_utf8_strftime(const char *format, const struct tm *tm)
650 static char buf[128];
651 char *locale;
652 GError *err = NULL;
653 int len;
654 char *utf8;
656 g_return_val_if_fail(format != NULL, NULL);
658 if (tm == NULL)
660 time_t now = time(NULL);
661 tm = localtime(&now);
664 locale = g_locale_from_utf8(format, -1, NULL, NULL, &err);
665 if (err != NULL)
667 purple_debug_error("util", "Format conversion failed in purple_utf8_strftime(): %s\n", err->message);
668 g_error_free(err);
669 err = NULL;
670 locale = g_strdup(format);
673 /* A return value of 0 is either an error (in
674 * which case, the contents of the buffer are
675 * undefined) or the empty string (in which
676 * case, no harm is done here). */
677 if ((len = purple_internal_strftime(buf, sizeof(buf), locale, tm)) == 0)
679 g_free(locale);
680 return "";
683 g_free(locale);
685 utf8 = g_locale_to_utf8(buf, len, NULL, NULL, &err);
686 if (err != NULL)
688 purple_debug_error("util", "Result conversion failed in purple_utf8_strftime(): %s\n", err->message);
689 g_error_free(err);
691 else
693 g_strlcpy(buf, utf8, sizeof(buf));
694 g_free(utf8);
697 return buf;
700 const char *
701 purple_date_format_short(const struct tm *tm)
703 return purple_utf8_strftime("%x", tm);
706 const char *
707 purple_date_format_long(const struct tm *tm)
710 * This string determines how some dates are displayed. The default
711 * string "%x %X" shows the date then the time. Translators can
712 * change this to "%X %x" if they want the time to be shown first,
713 * followed by the date.
715 return purple_utf8_strftime(_("%x %X"), tm);
718 const char *
719 purple_date_format_full(const struct tm *tm)
721 return purple_utf8_strftime("%c", tm);
724 const char *
725 purple_time_format(const struct tm *tm)
727 return purple_utf8_strftime("%X", tm);
730 time_t
731 purple_time_build(int year, int month, int day, int hour, int min, int sec)
733 struct tm tm;
735 tm.tm_year = year - 1900;
736 tm.tm_mon = month - 1;
737 tm.tm_mday = day;
738 tm.tm_hour = hour;
739 tm.tm_min = min;
740 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60;
742 return mktime(&tm);
745 /* originally taken from GLib trunk 1-6-11 */
746 /* originally licensed as LGPL 2+ */
747 static time_t
748 mktime_utc(struct tm *tm)
750 time_t retval;
752 #ifndef HAVE_TIMEGM
753 static const gint days_before[] =
755 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
757 #endif
759 #ifndef HAVE_TIMEGM
760 if (tm->tm_mon < 0 || tm->tm_mon > 11)
761 return (time_t) -1;
763 retval = (tm->tm_year - 70) * 365;
764 retval += (tm->tm_year - 68) / 4;
765 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
767 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
768 retval -= 1;
770 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
771 #else
772 retval = timegm (tm);
773 #endif /* !HAVE_TIMEGM */
775 return retval;
778 time_t
779 purple_str_to_time(const char *timestamp, gboolean utc,
780 struct tm *tm, long *tz_off, const char **rest)
782 struct tm t;
783 const gchar *str;
784 gint year = 0;
785 long tzoff = PURPLE_NO_TZ_OFF;
786 time_t retval;
787 gboolean mktime_with_utc = FALSE;
789 if (rest != NULL)
790 *rest = NULL;
792 g_return_val_if_fail(timestamp != NULL, 0);
794 memset(&t, 0, sizeof(struct tm));
796 str = timestamp;
798 /* Strip leading whitespace */
799 while (g_ascii_isspace(*str))
800 str++;
802 if (*str == '\0') {
803 if (rest != NULL && *str != '\0')
804 *rest = str;
806 return 0;
809 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
810 if (rest != NULL && *str != '\0')
811 *rest = str;
813 return 0;
816 /* 4 digit year */
817 if (sscanf(str, "%04d", &year) && year >= 1900) {
818 str += 4;
820 if (*str == '-' || *str == '/')
821 str++;
823 t.tm_year = year - 1900;
826 /* 2 digit month */
827 if (!sscanf(str, "%02d", &t.tm_mon)) {
828 if (rest != NULL && *str != '\0')
829 *rest = str;
831 return 0;
834 str += 2;
835 t.tm_mon -= 1;
837 if (*str == '-' || *str == '/')
838 str++;
840 /* 2 digit day */
841 if (!sscanf(str, "%02d", &t.tm_mday)) {
842 if (rest != NULL && *str != '\0')
843 *rest = str;
845 return 0;
848 str += 2;
850 /* Grab the year off the end if there's still stuff */
851 if (*str == '/' || *str == '-') {
852 /* But make sure we don't read the year twice */
853 if (year >= 1900) {
854 if (rest != NULL && *str != '\0')
855 *rest = str;
857 return 0;
860 str++;
862 if (!sscanf(str, "%04d", &t.tm_year)) {
863 if (rest != NULL && *str != '\0')
864 *rest = str;
866 return 0;
869 t.tm_year -= 1900;
870 } else if (*str == 'T' || *str == '.') {
871 str++;
873 /* Continue grabbing the hours/minutes/seconds */
874 if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
875 (str += 8)) ||
876 (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
877 (str += 6)))
879 gint sign, tzhrs, tzmins;
881 if (*str == '.') {
882 /* Cut off those pesky micro-seconds */
883 do {
884 str++;
885 } while (*str >= '0' && *str <= '9');
888 sign = (*str == '+') ? 1 : -1;
890 /* Process the timezone */
891 if (*str == '+' || *str == '-') {
892 str++;
894 if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) ||
895 (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4))))
897 mktime_with_utc = TRUE;
898 tzoff = tzhrs * 60 * 60 + tzmins * 60;
899 tzoff *= sign;
901 } else if (*str == 'Z') {
902 /* 'Z' = Zulu = UTC */
903 str++;
904 mktime_with_utc = TRUE;
905 tzoff = 0;
908 if (!mktime_with_utc)
910 /* No timezone specified. */
912 if (utc) {
913 mktime_with_utc = TRUE;
914 tzoff = 0;
915 } else {
916 /* Local Time */
917 t.tm_isdst = -1;
923 if (rest != NULL && *str != '\0') {
924 /* Strip trailing whitespace */
925 while (g_ascii_isspace(*str))
926 str++;
928 if (*str != '\0')
929 *rest = str;
932 if (mktime_with_utc)
933 retval = mktime_utc(&t);
934 else
935 retval = mktime(&t);
937 if (tm != NULL)
938 *tm = t;
940 if (tzoff != PURPLE_NO_TZ_OFF)
941 retval -= tzoff;
943 if (tz_off != NULL)
944 *tz_off = tzoff;
946 return retval;
949 char *
950 purple_uts35_to_str(const char *format, size_t len, struct tm *tm)
952 GString *string;
953 guint i, count;
955 if (tm == NULL) {
956 time_t now = time(NULL);
957 tm = localtime(&now);
960 string = g_string_sized_new(len);
961 i = 0;
962 while (i < len) {
963 count = 1;
964 while ((i + count) < len && format[i] == format[i+count])
965 count++;
967 switch (format[i]) {
968 /* Era Designator */
969 case 'G':
970 if (count <= 3) {
971 /* Abbreviated */
972 } else if (count == 4) {
973 /* Full */
974 } else if (count >= 5) {
975 /* Narrow */
976 count = 5;
978 break;
981 /* Year */
982 case 'y':
983 if (count == 2) {
984 /* Two-digits only */
985 g_string_append(string, purple_utf8_strftime("%y", tm));
986 } else {
987 /* Zero-padding */
988 char *tmp = g_strdup_printf("%%0%dY", count);
989 g_string_append(string, purple_utf8_strftime(tmp, tm));
990 g_free(tmp);
992 break;
994 /* Year (in "Week of Year" based calendars) */
995 case 'Y':
996 if (count == 2) {
997 /* Two-digits only */
998 } else {
999 /* Zero-padding */
1001 break;
1003 /* Extended Year */
1004 case 'u':
1005 break;
1007 /* Cyclic Year Name */
1008 case 'U':
1009 if (count <= 3) {
1010 /* Abbreviated */
1011 } else if (count == 4) {
1012 /* Full */
1013 } else if (count >= 5) {
1014 /* Narrow */
1015 count = 5;
1017 break;
1020 /* Quarter */
1021 case 'Q':
1022 if (count <= 2) {
1023 /* Numerical */
1024 } else if (count == 3) {
1025 /* Abbreviation */
1026 } else if (count >= 4) {
1027 /* Full */
1028 count = 4;
1030 break;
1032 /* Stand-alone Quarter */
1033 case 'q':
1034 if (count <= 2) {
1035 /* Numerical */
1036 } else if (count == 3) {
1037 /* Abbreviation */
1038 } else if (count >= 4) {
1039 /* Full */
1040 count = 4;
1042 break;
1044 /* Month */
1045 case 'M':
1046 if (count <= 2) {
1047 /* Numerical */
1048 g_string_append(string, purple_utf8_strftime("%m", tm));
1049 } else if (count == 3) {
1050 /* Abbreviation */
1051 g_string_append(string, purple_utf8_strftime("%b", tm));
1052 } else if (count == 4) {
1053 /* Full */
1054 g_string_append(string, purple_utf8_strftime("%B", tm));
1055 } else if (count >= 5) {
1056 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
1057 count = 5;
1059 break;
1061 /* Stand-alone Month */
1062 case 'L':
1063 if (count <= 2) {
1064 /* Numerical */
1065 g_string_append(string, purple_utf8_strftime("%m", tm));
1066 } else if (count == 3) {
1067 /* Abbreviation */
1068 g_string_append(string, purple_utf8_strftime("%b", tm));
1069 } else if (count == 4) {
1070 /* Full */
1071 g_string_append(string, purple_utf8_strftime("%B", tm));
1072 } else if (count >= 5) {
1073 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
1074 count = 5;
1076 break;
1078 /* Ignored */
1079 case 'l':
1080 break;
1083 /* Week of Year */
1084 case 'w':
1085 g_string_append(string, purple_utf8_strftime("%W", tm));
1086 count = MIN(count, 2);
1087 break;
1089 /* Week of Month */
1090 case 'W':
1091 count = 1;
1092 break;
1095 /* Day of Month */
1096 case 'd':
1097 g_string_append(string, purple_utf8_strftime("%d", tm));
1098 count = MIN(count, 2);
1099 break;
1101 /* Day of Year */
1102 case 'D':
1103 g_string_append(string, purple_utf8_strftime("%j", tm));
1104 count = MIN(count, 3);
1105 break;
1107 /* Day of Year in Month */
1108 case 'F':
1109 count = 1;
1110 break;
1112 /* Modified Julian Day */
1113 case 'g':
1114 break;
1117 /* Day of Week */
1118 case 'E':
1119 if (count <= 3) {
1120 /* Short */
1121 g_string_append(string, purple_utf8_strftime("%a", tm));
1122 } else if (count == 4) {
1123 /* Full */
1124 g_string_append(string, purple_utf8_strftime("%A", tm));
1125 } else if (count >= 5) {
1126 /* Narrow */
1127 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1128 count = 5;
1130 break;
1132 /* Local Day of Week */
1133 case 'e':
1134 if (count <= 2) {
1135 /* Numeric */
1136 g_string_append(string, purple_utf8_strftime("%u", tm));
1137 } else if (count == 3) {
1138 /* Short */
1139 g_string_append(string, purple_utf8_strftime("%a", tm));
1140 } else if (count == 4) {
1141 /* Full */
1142 g_string_append(string, purple_utf8_strftime("%A", tm));
1143 } else if (count >= 5) {
1144 /* Narrow */
1145 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1146 count = 5;
1148 break;
1150 /* Stand-alone Local Day of Week */
1151 case 'c':
1152 if (count <= 2) {
1153 /* Numeric */
1154 g_string_append(string, purple_utf8_strftime("%u", tm));
1155 count = 1;
1156 } else if (count == 3) {
1157 /* Short */
1158 g_string_append(string, purple_utf8_strftime("%a", tm));
1159 } else if (count == 4) {
1160 /* Full */
1161 g_string_append(string, purple_utf8_strftime("%A", tm));
1162 } else if (count >= 5) {
1163 /* Narrow */
1164 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
1165 count = 5;
1167 break;
1170 /* AM/PM */
1171 case 'a':
1172 g_string_append(string, purple_utf8_strftime("%p", tm));
1173 break;
1176 /* Hour (1-12) */
1177 case 'h':
1178 if (count == 1) {
1179 /* No padding */
1180 g_string_append(string, purple_utf8_strftime("%I", tm));
1181 } else if (count >= 2) {
1182 /* Zero-padded */
1183 g_string_append(string, purple_utf8_strftime("%I", tm));
1184 count = 2;
1186 break;
1188 /* Hour (0-23) */
1189 case 'H':
1190 if (count == 1) {
1191 /* No padding */
1192 g_string_append(string, purple_utf8_strftime("%H", tm));
1193 } else if (count >= 2) {
1194 /* Zero-padded */
1195 g_string_append(string, purple_utf8_strftime("%H", tm));
1196 count = 2;
1198 break;
1200 /* Hour (0-11) */
1201 case 'K':
1202 if (count == 1) {
1203 /* No padding */
1204 } else if (count >= 2) {
1205 /* Zero-padded */
1206 count = 2;
1208 break;
1210 /* Hour (1-24) */
1211 case 'k':
1212 if (count == 1) {
1213 /* No padding */
1214 } else if (count >= 2) {
1215 /* Zero-padded */
1216 count = 2;
1218 break;
1220 /* Hour (hHkK by locale) */
1221 case 'j':
1222 break;
1225 /* Minute */
1226 case 'm':
1227 g_string_append(string, purple_utf8_strftime("%M", tm));
1228 count = MIN(count, 2);
1229 break;
1232 /* Second */
1233 case 's':
1234 g_string_append(string, purple_utf8_strftime("%S", tm));
1235 count = MIN(count, 2);
1236 break;
1238 /* Fractional Sub-second */
1239 case 'S':
1240 break;
1242 /* Millisecond */
1243 case 'A':
1244 break;
1247 /* Time Zone (specific non-location format) */
1248 case 'z':
1249 if (count <= 3) {
1250 /* Short */
1251 } else if (count >= 4) {
1252 /* Full */
1253 count = 4;
1255 break;
1257 /* Time Zone */
1258 case 'Z':
1259 if (count <= 3) {
1260 /* RFC822 */
1261 g_string_append(string, purple_utf8_strftime("%z", tm));
1262 } else if (count == 4) {
1263 /* Localized GMT */
1264 } else if (count >= 5) {
1265 /* ISO8601 */
1266 g_string_append(string, purple_utf8_strftime("%z", tm));
1267 count = 5;
1269 break;
1271 /* Time Zone (generic non-location format) */
1272 case 'v':
1273 if (count <= 3) {
1274 /* Short */
1275 g_string_append(string, purple_utf8_strftime("%Z", tm));
1276 count = 1;
1277 } else if (count >= 4) {
1278 /* Long */
1279 g_string_append(string, purple_utf8_strftime("%Z", tm));
1280 count = 4;
1282 break;
1284 /* Time Zone */
1285 case 'V':
1286 if (count <= 3) {
1287 /* Same as z */
1288 count = 1;
1289 } else if (count >= 4) {
1290 /* Generic Location Format) */
1291 g_string_append(string, purple_utf8_strftime("%Z", tm));
1292 count = 4;
1294 break;
1297 default:
1298 g_string_append_len(string, format + i, count);
1299 break;
1302 i += count;
1305 return g_string_free(string, FALSE);
1308 /**************************************************************************
1309 * Markup Functions
1310 **************************************************************************/
1313 * This function is stolen from glib's gmarkup.c and modified to not
1314 * replace ' with &apos;
1316 static void append_escaped_text(GString *str,
1317 const gchar *text, gssize length)
1319 const gchar *p;
1320 const gchar *end;
1321 gunichar c;
1323 p = text;
1324 end = text + length;
1326 while (p != end)
1328 const gchar *next;
1329 next = g_utf8_next_char (p);
1331 switch (*p)
1333 case '&':
1334 g_string_append (str, "&amp;");
1335 break;
1337 case '<':
1338 g_string_append (str, "&lt;");
1339 break;
1341 case '>':
1342 g_string_append (str, "&gt;");
1343 break;
1345 case '"':
1346 g_string_append (str, "&quot;");
1347 break;
1349 default:
1350 c = g_utf8_get_char (p);
1351 if ((0x1 <= c && c <= 0x8) ||
1352 (0xb <= c && c <= 0xc) ||
1353 (0xe <= c && c <= 0x1f) ||
1354 (0x7f <= c && c <= 0x84) ||
1355 (0x86 <= c && c <= 0x9f))
1356 g_string_append_printf (str, "&#x%x;", c);
1357 else
1358 g_string_append_len (str, p, next - p);
1359 break;
1362 p = next;
1366 /* This function is stolen from glib's gmarkup.c */
1367 gchar *purple_markup_escape_text(const gchar *text, gssize length)
1369 GString *str;
1371 g_return_val_if_fail(text != NULL, NULL);
1373 if (length < 0)
1374 length = strlen(text);
1376 /* prealloc at least as long as original text */
1377 str = g_string_sized_new(length);
1378 append_escaped_text(str, text, length);
1380 return g_string_free(str, FALSE);
1383 const char *
1384 purple_markup_unescape_entity(const char *text, int *length)
1386 const char *pln;
1387 int len, pound;
1388 char temp[2];
1390 if (!text || *text != '&')
1391 return NULL;
1393 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1395 if(IS_ENTITY("&amp;"))
1396 pln = "&";
1397 else if(IS_ENTITY("&lt;"))
1398 pln = "<";
1399 else if(IS_ENTITY("&gt;"))
1400 pln = ">";
1401 else if(IS_ENTITY("&nbsp;"))
1402 pln = " ";
1403 else if(IS_ENTITY("&copy;"))
1404 pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1405 else if(IS_ENTITY("&quot;"))
1406 pln = "\"";
1407 else if(IS_ENTITY("&reg;"))
1408 pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1409 else if(IS_ENTITY("&apos;"))
1410 pln = "\'";
1411 else if(*(text+1) == '#' &&
1412 (sscanf(text, "&#%u%1[;]", &pound, temp) == 2 ||
1413 sscanf(text, "&#x%x%1[;]", &pound, temp) == 2) &&
1414 pound != 0) {
1415 static char buf[7];
1416 int buflen = g_unichar_to_utf8((gunichar)pound, buf);
1417 buf[buflen] = '\0';
1418 pln = buf;
1420 len = (*(text+2) == 'x' ? 3 : 2);
1421 while(isxdigit((gint) text[len])) len++;
1422 if(text[len] == ';') len++;
1424 else
1425 return NULL;
1427 if (length)
1428 *length = len;
1429 return pln;
1432 char *
1433 purple_markup_get_css_property(const gchar *style,
1434 const gchar *opt)
1436 const gchar *css_str = style;
1437 const gchar *css_value_start;
1438 const gchar *css_value_end;
1439 gchar *tmp;
1440 gchar *ret;
1442 g_return_val_if_fail(opt != NULL, NULL);
1444 if (!css_str)
1445 return NULL;
1447 /* find the CSS property */
1448 while (1)
1450 /* skip whitespace characters */
1451 while (*css_str && g_ascii_isspace(*css_str))
1452 css_str++;
1453 if (!g_ascii_isalpha(*css_str))
1454 return NULL;
1455 if (g_ascii_strncasecmp(css_str, opt, strlen(opt)))
1457 /* go to next css property positioned after the next ';' */
1458 while (*css_str && *css_str != '"' && *css_str != ';')
1459 css_str++;
1460 if(*css_str != ';')
1461 return NULL;
1462 css_str++;
1464 else
1465 break;
1468 /* find the CSS value position in the string */
1469 css_str += strlen(opt);
1470 while (*css_str && g_ascii_isspace(*css_str))
1471 css_str++;
1472 if (*css_str != ':')
1473 return NULL;
1474 css_str++;
1475 while (*css_str && g_ascii_isspace(*css_str))
1476 css_str++;
1477 if (*css_str == '\0' || *css_str == '"' || *css_str == ';')
1478 return NULL;
1480 /* mark the CSS value */
1481 css_value_start = css_str;
1482 while (*css_str && *css_str != '"' && *css_str != ';')
1483 css_str++;
1484 css_value_end = css_str - 1;
1486 /* Removes trailing whitespace */
1487 while (css_value_end > css_value_start && g_ascii_isspace(*css_value_end))
1488 css_value_end--;
1490 tmp = g_strndup(css_value_start, css_value_end - css_value_start + 1);
1491 ret = purple_unescape_html(tmp);
1492 g_free(tmp);
1494 return ret;
1497 gboolean purple_markup_is_rtl(const char *html)
1499 GData *attributes;
1500 const gchar *start, *end;
1501 gboolean res = FALSE;
1503 if (purple_markup_find_tag("span", html, &start, &end, &attributes))
1505 /* tmp is a member of attributes and is free with g_datalist_clear call */
1506 const char *tmp = g_datalist_get_data(&attributes, "dir");
1507 if (tmp && !g_ascii_strcasecmp(tmp, "RTL"))
1508 res = TRUE;
1509 if (!res)
1511 tmp = g_datalist_get_data(&attributes, "style");
1512 if (tmp)
1514 char *tmp2 = purple_markup_get_css_property(tmp, "direction");
1515 if (tmp2 && !g_ascii_strcasecmp(tmp2, "RTL"))
1516 res = TRUE;
1517 g_free(tmp2);
1521 g_datalist_clear(&attributes);
1523 return res;
1526 gboolean
1527 purple_markup_find_tag(const char *needle, const char *haystack,
1528 const char **start, const char **end, GData **attributes)
1530 GData *attribs;
1531 const char *cur = haystack;
1532 char *name = NULL;
1533 gboolean found = FALSE;
1534 gboolean in_tag = FALSE;
1535 gboolean in_attr = FALSE;
1536 const char *in_quotes = NULL;
1537 size_t needlelen;
1539 g_return_val_if_fail( needle != NULL, FALSE);
1540 g_return_val_if_fail( *needle != '\0', FALSE);
1541 g_return_val_if_fail( haystack != NULL, FALSE);
1542 g_return_val_if_fail( start != NULL, FALSE);
1543 g_return_val_if_fail( end != NULL, FALSE);
1544 g_return_val_if_fail(attributes != NULL, FALSE);
1546 needlelen = strlen(needle);
1547 g_datalist_init(&attribs);
1549 while (*cur && !found) {
1550 if (in_tag) {
1551 if (in_quotes) {
1552 const char *close = cur;
1554 while (*close && *close != *in_quotes)
1555 close++;
1557 /* if we got the close quote, store the value and carry on from *
1558 * after it. if we ran to the end of the string, point to the NULL *
1559 * and we're outta here */
1560 if (*close) {
1561 /* only store a value if we have an attribute name */
1562 if (name) {
1563 size_t len = close - cur;
1564 char *val = g_strndup(cur, len);
1566 g_datalist_set_data_full(&attribs, name, val, g_free);
1567 g_free(name);
1568 name = NULL;
1571 in_quotes = NULL;
1572 cur = close + 1;
1573 } else {
1574 cur = close;
1576 } else if (in_attr) {
1577 const char *close = cur;
1579 while (*close && *close != '>' && *close != '"' &&
1580 *close != '\'' && *close != ' ' && *close != '=')
1581 close++;
1583 /* if we got the equals, store the name of the attribute. if we got
1584 * the quote, save the attribute and go straight to quote mode.
1585 * otherwise the tag closed or we reached the end of the string,
1586 * so we can get outta here */
1587 switch (*close) {
1588 case '"':
1589 case '\'':
1590 in_quotes = close;
1591 /* fall through */
1592 case '=':
1594 size_t len = close - cur;
1596 /* don't store a blank attribute name */
1597 if (len) {
1598 g_free(name);
1599 name = g_ascii_strdown(cur, len);
1602 in_attr = FALSE;
1603 cur = close + 1;
1605 break;
1606 case ' ':
1607 case '>':
1608 in_attr = FALSE;
1609 /* fall through */
1610 default:
1611 cur = close;
1612 break;
1614 } else {
1615 switch (*cur) {
1616 case ' ':
1617 /* swallow extra spaces inside tag */
1618 while (*cur && *cur == ' ') cur++;
1619 in_attr = TRUE;
1620 break;
1621 case '>':
1622 found = TRUE;
1623 *end = cur;
1624 break;
1625 case '"':
1626 case '\'':
1627 in_quotes = cur;
1628 /* fall through */
1629 default:
1630 cur++;
1631 break;
1634 } else {
1635 /* if we hit a < followed by the name of our tag... */
1636 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) {
1637 *start = cur;
1638 cur = cur + needlelen + 1;
1640 /* if we're pointing at a space or a >, we found the right tag. if *
1641 * we're not, we've found a longer tag, so we need to skip to the *
1642 * >, but not being distracted by >s inside quotes. */
1643 if (*cur == ' ' || *cur == '>') {
1644 in_tag = TRUE;
1645 } else {
1646 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') {
1647 if (*cur == '"') {
1648 cur++;
1649 while (*cur && *cur != '"')
1650 cur++;
1651 } else if (*cur == '\'') {
1652 cur++;
1653 while (*cur && *cur != '\'')
1654 cur++;
1655 } else {
1656 cur++;
1660 } else {
1661 cur++;
1666 /* clean up any attribute name from a premature termination */
1667 g_free(name);
1669 if (found) {
1670 *attributes = attribs;
1671 } else {
1672 *start = NULL;
1673 *end = NULL;
1674 *attributes = NULL;
1677 return found;
1680 gboolean
1681 purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
1682 const char *start_token, int skip,
1683 const char *end_token, char check_value,
1684 const char *no_value_token,
1685 const char *display_name, gboolean is_link,
1686 const char *link_prefix,
1687 PurpleInfoFieldFormatCallback format_cb)
1689 const char *p, *q;
1691 g_return_val_if_fail(str != NULL, FALSE);
1692 g_return_val_if_fail(user_info != NULL, FALSE);
1693 g_return_val_if_fail(start_token != NULL, FALSE);
1694 g_return_val_if_fail(end_token != NULL, FALSE);
1695 g_return_val_if_fail(display_name != NULL, FALSE);
1697 p = strstr(str, start_token);
1699 if (p == NULL)
1700 return FALSE;
1702 p += strlen(start_token) + skip;
1704 if (p >= str + len)
1705 return FALSE;
1707 if (check_value != '\0' && *p == check_value)
1708 return FALSE;
1710 q = strstr(p, end_token);
1712 /* Trim leading blanks */
1713 while (*p != '\n' && g_ascii_isspace(*p)) {
1714 p += 1;
1717 /* Trim trailing blanks */
1718 while (q > p && g_ascii_isspace(*(q - 1))) {
1719 q -= 1;
1722 /* Don't bother with null strings */
1723 if (p == q)
1724 return FALSE;
1726 if (q != NULL && (!no_value_token ||
1727 (no_value_token && strncmp(p, no_value_token,
1728 strlen(no_value_token)))))
1730 GString *dest = g_string_new("");
1732 if (is_link)
1734 g_string_append(dest, "<a href=\"");
1736 if (link_prefix)
1737 g_string_append(dest, link_prefix);
1739 if (format_cb != NULL)
1741 char *reformatted = format_cb(p, q - p);
1742 g_string_append(dest, reformatted);
1743 g_free(reformatted);
1745 else
1746 g_string_append_len(dest, p, q - p);
1747 g_string_append(dest, "\">");
1749 if (link_prefix)
1750 g_string_append(dest, link_prefix);
1752 g_string_append_len(dest, p, q - p);
1753 g_string_append(dest, "</a>");
1755 else
1757 if (format_cb != NULL)
1759 char *reformatted = format_cb(p, q - p);
1760 g_string_append(dest, reformatted);
1761 g_free(reformatted);
1763 else
1764 g_string_append_len(dest, p, q - p);
1767 purple_notify_user_info_add_pair_html(user_info, display_name, dest->str);
1768 g_string_free(dest, TRUE);
1770 return TRUE;
1773 return FALSE;
1776 struct purple_parse_tag {
1777 char *src_tag;
1778 char *dest_tag;
1779 gboolean ignore;
1782 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1783 recommended in the GCC docs). It contains 'continue's that should
1784 affect the while-loop in purple_markup_html_to_xhtml and doing the
1785 above would break that.
1786 Also, remember to put braces in constructs that require them for
1787 multiple statements when using this macro. */
1788 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1789 const char *o = c + strlen("<" x); \
1790 const char *p = NULL, *q = NULL, *r = NULL; \
1791 /* o = iterating over full tag \
1792 * p = > (end of tag) \
1793 * q = start of quoted bit \
1794 * r = < inside tag \
1795 */ \
1796 GString *innards = g_string_new(""); \
1797 while(o && *o) { \
1798 if(!q && (*o == '\"' || *o == '\'') ) { \
1799 q = o; \
1800 } else if(q) { \
1801 if(*o == *q) { /* end of quoted bit */ \
1802 char *unescaped = g_strndup(q+1, o-q-1); \
1803 char *escaped = g_markup_escape_text(unescaped, -1); \
1804 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1805 g_free(unescaped); \
1806 g_free(escaped); \
1807 q = NULL; \
1808 } else if(*c == '\\') { \
1809 o++; \
1811 } else if(*o == '<') { \
1812 r = o; \
1813 } else if(*o == '>') { \
1814 p = o; \
1815 break; \
1816 } else { \
1817 innards = g_string_append_c(innards, *o); \
1819 o++; \
1821 if(p && !r) { /* got an end of tag and no other < earlier */\
1822 if(*(p-1) != '/') { \
1823 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1824 pt->src_tag = x; \
1825 pt->dest_tag = y; \
1826 tags = g_list_prepend(tags, pt); \
1828 if(xhtml) { \
1829 xhtml = g_string_append(xhtml, "<" y); \
1830 xhtml = g_string_append(xhtml, innards->str); \
1831 xhtml = g_string_append_c(xhtml, '>'); \
1833 c = p + 1; \
1834 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1835 if(xhtml) \
1836 xhtml = g_string_append(xhtml, "&lt;"); \
1837 if(plain) \
1838 plain = g_string_append_c(plain, '<'); \
1839 c++; \
1841 g_string_free(innards, TRUE); \
1842 continue; \
1844 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1845 (*(c+strlen("<" x)) == '>' || \
1846 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1847 if(xhtml) \
1848 xhtml = g_string_append(xhtml, "<" y); \
1849 c += strlen("<" x); \
1850 if(*c != '/') { \
1851 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1852 pt->src_tag = x; \
1853 pt->dest_tag = y; \
1854 tags = g_list_prepend(tags, pt); \
1855 if(xhtml) \
1856 xhtml = g_string_append_c(xhtml, '>'); \
1857 } else { \
1858 if(xhtml) \
1859 xhtml = g_string_append(xhtml, "/>");\
1861 c = strchr(c, '>') + 1; \
1862 continue; \
1864 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1865 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1866 void
1867 purple_markup_html_to_xhtml(const char *html, char **xhtml_out,
1868 char **plain_out)
1870 GString *xhtml = NULL;
1871 GString *plain = NULL;
1872 GString *url = NULL;
1873 GString *cdata = NULL;
1874 GList *tags = NULL, *tag;
1875 const char *c = html;
1876 char quote = '\0';
1878 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1879 quote = *(ptr++); \
1880 else \
1881 quote = '\0';
1883 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1885 g_return_if_fail(xhtml_out != NULL || plain_out != NULL);
1887 if(xhtml_out)
1888 xhtml = g_string_new("");
1889 if(plain_out)
1890 plain = g_string_new("");
1892 while(c && *c) {
1893 if(*c == '<') {
1894 if(*(c+1) == '/') { /* closing tag */
1895 tag = tags;
1896 while(tag) {
1897 struct purple_parse_tag *pt = tag->data;
1898 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') {
1899 c += strlen(pt->src_tag) + 3;
1900 break;
1902 tag = tag->next;
1904 if(tag) {
1905 while(tags) {
1906 struct purple_parse_tag *pt = tags->data;
1907 if(xhtml && !pt->ignore)
1908 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1909 if(plain && purple_strequal(pt->src_tag, "a")) {
1910 /* if this is a link, we have to add the url to the plaintext, too */
1911 if (cdata && url &&
1912 (!g_string_equal(cdata, url) && (g_ascii_strncasecmp(url->str, "mailto:", 7) != 0 ||
1913 g_utf8_collate(url->str + 7, cdata->str) != 0)))
1914 g_string_append_printf(plain, " <%s>", g_strstrip(purple_unescape_html(url->str)));
1915 if (cdata) {
1916 g_string_free(cdata, TRUE);
1917 cdata = NULL;
1921 if(tags == tag)
1922 break;
1923 tags = g_list_remove(tags, pt);
1924 g_free(pt);
1926 g_free(tag->data);
1927 tags = g_list_remove(tags, tag->data);
1928 } else {
1929 /* a closing tag we weren't expecting...
1930 * we'll let it slide, if it's really a tag...if it's
1931 * just a </ we'll escape it properly */
1932 const char *end = c+2;
1933 while(*end && g_ascii_isalpha(*end))
1934 end++;
1935 if(*end == '>') {
1936 c = end+1;
1937 } else {
1938 if(xhtml)
1939 xhtml = g_string_append(xhtml, "&lt;");
1940 if(plain)
1941 plain = g_string_append_c(plain, '<');
1942 c++;
1945 } else { /* opening tag */
1946 ALLOW_TAG("blockquote");
1947 ALLOW_TAG("cite");
1948 ALLOW_TAG("div");
1949 ALLOW_TAG("em");
1950 ALLOW_TAG("h1");
1951 ALLOW_TAG("h2");
1952 ALLOW_TAG("h3");
1953 ALLOW_TAG("h4");
1954 ALLOW_TAG("h5");
1955 ALLOW_TAG("h6");
1956 /* we only allow html to start the message */
1957 if(c == html) {
1958 ALLOW_TAG("html");
1960 ALLOW_TAG_ALT("i", "em");
1961 ALLOW_TAG_ALT("italic", "em");
1962 ALLOW_TAG("li");
1963 ALLOW_TAG("ol");
1964 ALLOW_TAG("p");
1965 ALLOW_TAG("pre");
1966 ALLOW_TAG("q");
1967 ALLOW_TAG("span");
1968 ALLOW_TAG("ul");
1971 /* we skip <HR> because it's not legal in XHTML-IM. However,
1972 * we still want to send something sensible, so we put a
1973 * linebreak in its place. <BR> also needs special handling
1974 * because putting a </BR> to close it would just be dumb. */
1975 if((!g_ascii_strncasecmp(c, "<br", 3)
1976 || !g_ascii_strncasecmp(c, "<hr", 3))
1977 && (*(c+3) == '>' ||
1978 !g_ascii_strncasecmp(c+3, "/>", 2) ||
1979 !g_ascii_strncasecmp(c+3, " />", 3))) {
1980 c = strchr(c, '>') + 1;
1981 if(xhtml)
1982 xhtml = g_string_append(xhtml, "<br/>");
1983 if(plain && *c != '\n')
1984 plain = g_string_append_c(plain, '\n');
1985 continue;
1987 if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c, "<strong>", strlen("<strong>"))) {
1988 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1989 if (*(c+2) == '>')
1990 pt->src_tag = "b";
1991 else if (*(c+2) == 'o')
1992 pt->src_tag = "bold";
1993 else
1994 pt->src_tag = "strong";
1995 pt->dest_tag = "span";
1996 tags = g_list_prepend(tags, pt);
1997 c = strchr(c, '>') + 1;
1998 if(xhtml)
1999 xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>");
2000 continue;
2002 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) {
2003 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2004 pt->src_tag = *(c+2) == '>' ? "u" : "underline";
2005 pt->dest_tag = "span";
2006 tags = g_list_prepend(tags, pt);
2007 c = strchr(c, '>') + 1;
2008 if (xhtml)
2009 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>");
2010 continue;
2012 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) {
2013 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2014 pt->src_tag = *(c+2) == '>' ? "s" : "strike";
2015 pt->dest_tag = "span";
2016 tags = g_list_prepend(tags, pt);
2017 c = strchr(c, '>') + 1;
2018 if(xhtml)
2019 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>");
2020 continue;
2022 if(!g_ascii_strncasecmp(c, "<sub>", 5)) {
2023 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2024 pt->src_tag = "sub";
2025 pt->dest_tag = "span";
2026 tags = g_list_prepend(tags, pt);
2027 c = strchr(c, '>') + 1;
2028 if(xhtml)
2029 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>");
2030 continue;
2032 if(!g_ascii_strncasecmp(c, "<sup>", 5)) {
2033 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2034 pt->src_tag = "sup";
2035 pt->dest_tag = "span";
2036 tags = g_list_prepend(tags, pt);
2037 c = strchr(c, '>') + 1;
2038 if(xhtml)
2039 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
2040 continue;
2042 if (!g_ascii_strncasecmp(c, "<img", 4) && (*(c+4) == '>' || *(c+4) == ' ')) {
2043 const char *p = c + 4;
2044 GString *src = NULL, *alt = NULL;
2045 #define ESCAPE(from, to) \
2046 CHECK_QUOTE(from); \
2047 while (VALID_CHAR(from)) { \
2048 int len; \
2049 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2050 to = g_string_append(to, "&amp;"); \
2051 else if (*from == '\'') \
2052 to = g_string_append(to, "&apos;"); \
2053 else \
2054 to = g_string_append_c(to, *from); \
2055 from++; \
2058 while (*p && *p != '>') {
2059 if (!g_ascii_strncasecmp(p, "src=", 4)) {
2060 const char *q = p + 4;
2061 if (src)
2062 g_string_free(src, TRUE);
2063 src = g_string_new("");
2064 ESCAPE(q, src);
2065 p = q;
2066 } else if (!g_ascii_strncasecmp(p, "alt=", 4)) {
2067 const char *q = p + 4;
2068 if (alt)
2069 g_string_free(alt, TRUE);
2070 alt = g_string_new("");
2071 ESCAPE(q, alt);
2072 p = q;
2073 } else {
2074 p++;
2077 #undef ESCAPE
2078 if ((c = strchr(p, '>')) != NULL)
2079 c++;
2080 else
2081 c = p;
2082 /* src and alt are required! */
2083 if(src && xhtml)
2084 g_string_append_printf(xhtml, "<img src='%s' alt='%s' />", g_strstrip(src->str), alt ? alt->str : "");
2085 if(alt) {
2086 if(plain)
2087 plain = g_string_append(plain, purple_unescape_html(alt->str));
2088 if(!src && xhtml)
2089 xhtml = g_string_append(xhtml, alt->str);
2090 g_string_free(alt, TRUE);
2092 g_string_free(src, TRUE);
2093 continue;
2095 if (!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
2096 const char *p = c + 2;
2097 struct purple_parse_tag *pt;
2098 while (*p && *p != '>') {
2099 if (!g_ascii_strncasecmp(p, "href=", 5)) {
2100 const char *q = p + 5;
2101 if (url)
2102 g_string_free(url, TRUE);
2103 url = g_string_new("");
2104 if (cdata)
2105 g_string_free(cdata, TRUE);
2106 cdata = g_string_new("");
2107 CHECK_QUOTE(q);
2108 while (VALID_CHAR(q)) {
2109 int len;
2110 if ((*q == '&') && (purple_markup_unescape_entity(q, &len) == NULL))
2111 url = g_string_append(url, "&amp;");
2112 else if (*q == '"')
2113 url = g_string_append(url, "&quot;");
2114 else
2115 url = g_string_append_c(url, *q);
2116 q++;
2118 p = q;
2119 } else {
2120 p++;
2123 if ((c = strchr(p, '>')) != NULL)
2124 c++;
2125 else
2126 c = p;
2127 pt = g_new0(struct purple_parse_tag, 1);
2128 pt->src_tag = "a";
2129 pt->dest_tag = "a";
2130 tags = g_list_prepend(tags, pt);
2131 if(xhtml)
2132 g_string_append_printf(xhtml, "<a href=\"%s\">", url ? g_strstrip(url->str) : "");
2133 continue;
2135 #define ESCAPE(from, to) \
2136 CHECK_QUOTE(from); \
2137 while (VALID_CHAR(from)) { \
2138 int len; \
2139 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2140 to = g_string_append(to, "&amp;"); \
2141 else if (*from == '\'') \
2142 to = g_string_append_c(to, '\"'); \
2143 else \
2144 to = g_string_append_c(to, *from); \
2145 from++; \
2147 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
2148 const char *p = c + 5;
2149 GString *style = g_string_new("");
2150 struct purple_parse_tag *pt;
2151 while (*p && *p != '>') {
2152 if (!g_ascii_strncasecmp(p, "back=", 5)) {
2153 const char *q = p + 5;
2154 GString *color = g_string_new("");
2155 ESCAPE(q, color);
2156 g_string_append_printf(style, "background: %s; ", color->str);
2157 g_string_free(color, TRUE);
2158 p = q;
2159 } else if (!g_ascii_strncasecmp(p, "color=", 6)) {
2160 const char *q = p + 6;
2161 GString *color = g_string_new("");
2162 ESCAPE(q, color);
2163 g_string_append_printf(style, "color: %s; ", color->str);
2164 g_string_free(color, TRUE);
2165 p = q;
2166 } else if (!g_ascii_strncasecmp(p, "face=", 5)) {
2167 const char *q = p + 5;
2168 GString *face = g_string_new("");
2169 ESCAPE(q, face);
2170 g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str));
2171 g_string_free(face, TRUE);
2172 p = q;
2173 } else if (!g_ascii_strncasecmp(p, "size=", 5)) {
2174 const char *q = p + 5;
2175 int sz;
2176 const char *size = "medium";
2177 CHECK_QUOTE(q);
2178 sz = atoi(q);
2179 switch (sz)
2181 case 1:
2182 size = "xx-small";
2183 break;
2184 case 2:
2185 size = "small";
2186 break;
2187 case 3:
2188 size = "medium";
2189 break;
2190 case 4:
2191 size = "large";
2192 break;
2193 case 5:
2194 size = "x-large";
2195 break;
2196 case 6:
2197 case 7:
2198 size = "xx-large";
2199 break;
2200 default:
2201 break;
2203 g_string_append_printf(style, "font-size: %s; ", size);
2204 p = q;
2205 } else {
2206 p++;
2209 if ((c = strchr(p, '>')) != NULL)
2210 c++;
2211 else
2212 c = p;
2213 pt = g_new0(struct purple_parse_tag, 1);
2214 pt->src_tag = "font";
2215 pt->dest_tag = "span";
2216 tags = g_list_prepend(tags, pt);
2217 if(style->len && xhtml)
2218 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
2219 else
2220 pt->ignore = TRUE;
2221 g_string_free(style, TRUE);
2222 continue;
2224 #undef ESCAPE
2225 if (!g_ascii_strncasecmp(c, "<body ", 6)) {
2226 const char *p = c + 6;
2227 gboolean did_something = FALSE;
2228 while (*p && *p != '>') {
2229 if (!g_ascii_strncasecmp(p, "bgcolor=", 8)) {
2230 const char *q = p + 8;
2231 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
2232 GString *color = g_string_new("");
2233 CHECK_QUOTE(q);
2234 while (VALID_CHAR(q)) {
2235 color = g_string_append_c(color, *q);
2236 q++;
2238 if (xhtml)
2239 g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str));
2240 g_string_free(color, TRUE);
2241 if ((c = strchr(p, '>')) != NULL)
2242 c++;
2243 else
2244 c = p;
2245 pt->src_tag = "body";
2246 pt->dest_tag = "span";
2247 tags = g_list_prepend(tags, pt);
2248 did_something = TRUE;
2249 break;
2251 p++;
2253 if (did_something) continue;
2255 /* this has to come after the special case for bgcolor */
2256 ALLOW_TAG("body");
2257 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) {
2258 char *p = strstr(c + strlen("<!--"), "-->");
2259 if(p) {
2260 if(xhtml)
2261 xhtml = g_string_append(xhtml, "<!--");
2262 c += strlen("<!--");
2263 continue;
2267 if(xhtml)
2268 xhtml = g_string_append(xhtml, "&lt;");
2269 if(plain)
2270 plain = g_string_append_c(plain, '<');
2271 c++;
2273 } else if(*c == '&') {
2274 char buf[7];
2275 const char *pln;
2276 int len;
2278 if ((pln = purple_markup_unescape_entity(c, &len)) == NULL) {
2279 len = 1;
2280 g_snprintf(buf, sizeof(buf), "%c", *c);
2281 pln = buf;
2283 if(xhtml)
2284 xhtml = g_string_append_len(xhtml, c, len);
2285 if(plain)
2286 plain = g_string_append(plain, pln);
2287 if(cdata)
2288 cdata = g_string_append_len(cdata, c, len);
2289 c += len;
2290 } else {
2291 if(xhtml)
2292 xhtml = g_string_append_c(xhtml, *c);
2293 if(plain)
2294 plain = g_string_append_c(plain, *c);
2295 if(cdata)
2296 cdata = g_string_append_c(cdata, *c);
2297 c++;
2300 if(xhtml) {
2301 for (tag = tags; tag ; tag = tag->next) {
2302 struct purple_parse_tag *pt = tag->data;
2303 if(!pt->ignore)
2304 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
2307 g_list_free(tags);
2308 if(xhtml_out)
2309 *xhtml_out = g_string_free(xhtml, FALSE);
2310 if(plain_out)
2311 *plain_out = g_string_free(plain, FALSE);
2312 if(url)
2313 g_string_free(url, TRUE);
2314 if (cdata)
2315 g_string_free(cdata, TRUE);
2316 #undef CHECK_QUOTE
2317 #undef VALID_CHAR
2320 /* The following are probably reasonable changes:
2321 * - \n should be converted to a normal space
2322 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
2323 * - We want to turn </td>#whitespace<td> sequences into a single tab
2324 * - We want to turn <td> into a single tab (for msn profile "parsing")
2325 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
2326 * - <script>...</script> and <style>...</style> should be completely removed
2329 char *
2330 purple_markup_strip_html(const char *str)
2332 int i, j, k, entlen;
2333 gboolean visible = TRUE;
2334 gboolean closing_td_p = FALSE;
2335 gchar *str2;
2336 const gchar *cdata_close_tag = NULL, *ent;
2337 gchar *href = NULL;
2338 int href_st = 0;
2340 if(!str)
2341 return NULL;
2343 str2 = g_strdup(str);
2345 for (i = 0, j = 0; str2[i]; i++)
2347 if (str2[i] == '<')
2349 if (cdata_close_tag)
2351 /* Note: Don't even assume any other tag is a tag in CDATA */
2352 if (g_ascii_strncasecmp(str2 + i, cdata_close_tag,
2353 strlen(cdata_close_tag)) == 0)
2355 i += strlen(cdata_close_tag) - 1;
2356 cdata_close_tag = NULL;
2358 continue;
2360 else if (g_ascii_strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p)
2362 str2[j++] = '\t';
2363 visible = TRUE;
2365 else if (g_ascii_strncasecmp(str2 + i, "</td>", 5) == 0)
2367 closing_td_p = TRUE;
2368 visible = FALSE;
2370 else
2372 closing_td_p = FALSE;
2373 visible = TRUE;
2376 k = i + 1;
2378 if(g_ascii_isspace(str2[k]))
2379 visible = TRUE;
2380 else if (str2[k])
2382 /* Scan until we end the tag either implicitly (closed start
2383 * tag) or explicitly, using a sloppy method (i.e., < or >
2384 * inside quoted attributes will screw us up)
2386 while (str2[k] && str2[k] != '<' && str2[k] != '>')
2388 k++;
2391 /* If we've got an <a> tag with an href, save the address
2392 * to print later. */
2393 if (g_ascii_strncasecmp(str2 + i, "<a", 2) == 0 &&
2394 g_ascii_isspace(str2[i+2]))
2396 int st; /* start of href, inclusive [ */
2397 int end; /* end of href, exclusive ) */
2398 char delim = ' ';
2399 /* Find start of href */
2400 for (st = i + 3; st < k; st++)
2402 if (g_ascii_strncasecmp(str2+st, "href=", 5) == 0)
2404 st += 5;
2405 if (str2[st] == '"' || str2[st] == '\'')
2407 delim = str2[st];
2408 st++;
2410 break;
2413 /* find end of address */
2414 for (end = st; end < k && str2[end] != delim; end++)
2416 /* All the work is done in the loop construct above. */
2419 /* If there's an address, save it. If there was
2420 * already one saved, kill it. */
2421 if (st < k)
2423 char *tmp;
2424 g_free(href);
2425 tmp = g_strndup(str2 + st, end - st);
2426 href = purple_unescape_html(tmp);
2427 g_free(tmp);
2428 href_st = j;
2432 /* Replace </a> with an ascii representation of the
2433 * address the link was pointing to. */
2434 else if (href != NULL && g_ascii_strncasecmp(str2 + i, "</a>", 4) == 0)
2436 size_t hrlen = strlen(href);
2438 /* Only insert the href if it's different from the CDATA. */
2439 if ((hrlen != (gsize)(j - href_st) ||
2440 strncmp(str2 + href_st, href, hrlen)) &&
2441 (hrlen != (gsize)(j - href_st + 7) || /* 7 == strlen("http://") */
2442 strncmp(str2 + href_st, href + 7, hrlen - 7)))
2444 str2[j++] = ' ';
2445 str2[j++] = '(';
2446 g_memmove(str2 + j, href, hrlen);
2447 j += hrlen;
2448 str2[j++] = ')';
2449 g_free(href);
2450 href = NULL;
2454 /* Check for tags which should be mapped to newline (but ignore some of
2455 * the tags at the beginning of the text) */
2456 else if ((j && (g_ascii_strncasecmp(str2 + i, "<p>", 3) == 0
2457 || g_ascii_strncasecmp(str2 + i, "<tr", 3) == 0
2458 || g_ascii_strncasecmp(str2 + i, "<hr", 3) == 0
2459 || g_ascii_strncasecmp(str2 + i, "<li", 3) == 0
2460 || g_ascii_strncasecmp(str2 + i, "<div", 4) == 0))
2461 || g_ascii_strncasecmp(str2 + i, "<br", 3) == 0
2462 || g_ascii_strncasecmp(str2 + i, "</table>", 8) == 0)
2464 str2[j++] = '\n';
2466 /* Check for tags which begin CDATA and need to be closed */
2467 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */
2468 else if (g_ascii_strncasecmp(str2 + i, "<option", 7) == 0)
2470 /* FIXME: We should not do this if the OPTION is SELECT'd */
2471 cdata_close_tag = "</option>";
2473 #endif
2474 else if (g_ascii_strncasecmp(str2 + i, "<script", 7) == 0)
2476 cdata_close_tag = "</script>";
2478 else if (g_ascii_strncasecmp(str2 + i, "<style", 6) == 0)
2480 cdata_close_tag = "</style>";
2482 /* Update the index and continue checking after the tag */
2483 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k;
2484 continue;
2487 else if (cdata_close_tag)
2489 continue;
2491 else if (!g_ascii_isspace(str2[i]))
2493 visible = TRUE;
2496 if (str2[i] == '&' && (ent = purple_markup_unescape_entity(str2 + i, &entlen)) != NULL)
2498 while (*ent)
2499 str2[j++] = *ent++;
2500 i += entlen - 1;
2501 continue;
2504 if (visible)
2505 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i];
2508 g_free(href);
2510 str2[j] = '\0';
2512 return str2;
2515 static gboolean
2516 badchar(char c)
2518 switch (c) {
2519 case ' ':
2520 case ',':
2521 case '\0':
2522 case '\n':
2523 case '\r':
2524 case '<':
2525 case '>':
2526 case '"':
2527 return TRUE;
2528 default:
2529 return FALSE;
2533 static gboolean
2534 badentity(const char *c)
2536 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
2537 !g_ascii_strncasecmp(c, "&gt;", 4) ||
2538 !g_ascii_strncasecmp(c, "&quot;", 6)) {
2539 return TRUE;
2541 return FALSE;
2544 static const char *
2545 process_link(GString *ret,
2546 const char *start, const char *c,
2547 int matchlen,
2548 const char *urlprefix,
2549 int inside_paren)
2551 char *url_buf, *tmpurlbuf;
2552 const char *t;
2554 for (t = c;; t++) {
2555 if (!badchar(*t) && !badentity(t))
2556 continue;
2558 if (t - c == matchlen)
2559 break;
2561 if (*t == ',' && *(t + 1) != ' ') {
2562 continue;
2565 if (t > start && *(t - 1) == '.')
2566 t--;
2567 if (t > start && *(t - 1) == ')' && inside_paren > 0)
2568 t--;
2570 url_buf = g_strndup(c, t - c);
2571 tmpurlbuf = purple_unescape_html(url_buf);
2572 g_string_append_printf(ret, "<A HREF=\"%s%s\">%s</A>",
2573 urlprefix,
2574 tmpurlbuf, url_buf);
2575 g_free(tmpurlbuf);
2576 g_free(url_buf);
2577 return t;
2580 return c;
2583 char *
2584 purple_markup_linkify(const char *text)
2586 const char *c, *t, *q = NULL;
2587 char *tmpurlbuf, *url_buf;
2588 gunichar g;
2589 gboolean inside_html = FALSE;
2590 int inside_paren = 0;
2591 GString *ret;
2593 if (text == NULL)
2594 return NULL;
2596 ret = g_string_new("");
2598 c = text;
2599 while (*c) {
2601 if(*c == '(' && !inside_html) {
2602 inside_paren++;
2603 ret = g_string_append_c(ret, *c);
2604 c++;
2607 if(inside_html) {
2608 if(*c == '>') {
2609 inside_html = FALSE;
2610 } else if(!q && (*c == '\"' || *c == '\'')) {
2611 q = c;
2612 } else if(q) {
2613 if(*c == *q)
2614 q = NULL;
2616 } else if(*c == '<') {
2617 inside_html = TRUE;
2618 if (!g_ascii_strncasecmp(c, "<A", 2)) {
2619 while (1) {
2620 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
2621 inside_html = FALSE;
2622 break;
2624 ret = g_string_append_c(ret, *c);
2625 c++;
2626 if (!(*c))
2627 break;
2630 } else if (!g_ascii_strncasecmp(c, "http://", 7)) {
2631 c = process_link(ret, text, c, 7, "", inside_paren);
2632 } else if (!g_ascii_strncasecmp(c, "https://", 8)) {
2633 c = process_link(ret, text, c, 8, "", inside_paren);
2634 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) {
2635 c = process_link(ret, text, c, 6, "", inside_paren);
2636 } else if (!g_ascii_strncasecmp(c, "sftp://", 7)) {
2637 c = process_link(ret, text, c, 7, "", inside_paren);
2638 } else if (!g_ascii_strncasecmp(c, "file://", 7)) {
2639 c = process_link(ret, text, c, 7, "", inside_paren);
2640 } else if (!g_ascii_strncasecmp(c, "www.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2641 c = process_link(ret, text, c, 4, "http://", inside_paren);
2642 } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2643 c = process_link(ret, text, c, 4, "ftp://", inside_paren);
2644 } else if (!g_ascii_strncasecmp(c, "xmpp:", 5) && (c == text || badchar(c[-1]) || badentity(c-1))) {
2645 c = process_link(ret, text, c, 5, "", inside_paren);
2646 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) {
2647 t = c;
2648 while (1) {
2649 if (badchar(*t) || badentity(t)) {
2650 char *d;
2651 if (t - c == 7) {
2652 break;
2654 if (t > text && *(t - 1) == '.')
2655 t--;
2656 if ((d = strstr(c + 7, "?")) != NULL && d < t)
2657 url_buf = g_strndup(c + 7, d - c - 7);
2658 else
2659 url_buf = g_strndup(c + 7, t - c - 7);
2660 if (!purple_email_is_valid(url_buf)) {
2661 g_free(url_buf);
2662 break;
2664 g_free(url_buf);
2665 url_buf = g_strndup(c, t - c);
2666 tmpurlbuf = purple_unescape_html(url_buf);
2667 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
2668 tmpurlbuf, url_buf);
2669 g_free(url_buf);
2670 g_free(tmpurlbuf);
2671 c = t;
2672 break;
2674 t++;
2676 } else if (c != text && (*c == '@')) {
2677 int flag;
2678 GString *gurl_buf = NULL;
2679 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2681 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1)))
2682 flag = 0;
2683 else {
2684 flag = 1;
2685 gurl_buf = g_string_new("");
2688 t = c;
2689 while (flag) {
2690 /* iterate backwards grabbing the local part of an email address */
2691 g = g_utf8_get_char(t);
2692 if (badchar(*t) || (g >= 127) || (*t == '(') ||
2693 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "&lt;", 4) ||
2694 !g_ascii_strncasecmp(t - 3, "&gt;", 4))) ||
2695 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, "&quot;", 6)))))) {
2696 /* local part will already be part of ret, strip it out */
2697 ret = g_string_truncate(ret, ret->len - (c - t));
2698 ret = g_string_append_unichar(ret, g);
2699 break;
2700 } else {
2701 g_string_prepend_unichar(gurl_buf, g);
2702 t = g_utf8_find_prev_char(text, t);
2703 if (t < text) {
2704 ret = g_string_assign(ret, "");
2705 break;
2710 t = g_utf8_find_next_char(c, NULL);
2712 while (flag) {
2713 /* iterate forwards grabbing the domain part of an email address */
2714 g = g_utf8_get_char(t);
2715 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) {
2716 char *d;
2718 url_buf = g_string_free(gurl_buf, FALSE);
2720 /* strip off trailing periods */
2721 if (*url_buf) {
2722 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
2723 *d = '\0';
2726 tmpurlbuf = purple_unescape_html(url_buf);
2727 if (purple_email_is_valid(tmpurlbuf)) {
2728 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
2729 tmpurlbuf, url_buf);
2730 } else {
2731 g_string_append(ret, url_buf);
2733 g_free(url_buf);
2734 g_free(tmpurlbuf);
2735 c = t;
2737 break;
2738 } else {
2739 g_string_append_unichar(gurl_buf, g);
2740 t = g_utf8_find_next_char(t, NULL);
2745 if(*c == ')' && !inside_html) {
2746 inside_paren--;
2747 ret = g_string_append_c(ret, *c);
2748 c++;
2751 if (*c == 0)
2752 break;
2754 ret = g_string_append_c(ret, *c);
2755 c++;
2758 return g_string_free(ret, FALSE);
2761 char *purple_unescape_text(const char *in)
2763 GString *ret;
2764 const char *c = in;
2766 if (in == NULL)
2767 return NULL;
2769 ret = g_string_new("");
2770 while (*c) {
2771 int len;
2772 const char *ent;
2774 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2775 g_string_append(ret, ent);
2776 c += len;
2777 } else {
2778 g_string_append_c(ret, *c);
2779 c++;
2783 return g_string_free(ret, FALSE);
2786 char *purple_unescape_html(const char *html)
2788 GString *ret;
2789 const char *c = html;
2791 if (html == NULL)
2792 return NULL;
2794 ret = g_string_new("");
2795 while (*c) {
2796 int len;
2797 const char *ent;
2799 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2800 g_string_append(ret, ent);
2801 c += len;
2802 } else if (!strncmp(c, "<br>", 4)) {
2803 g_string_append_c(ret, '\n');
2804 c += 4;
2805 } else {
2806 g_string_append_c(ret, *c);
2807 c++;
2811 return g_string_free(ret, FALSE);
2814 char *
2815 purple_markup_slice(const char *str, guint x, guint y)
2817 GString *ret;
2818 GQueue *q;
2819 guint z = 0;
2820 gboolean appended = FALSE;
2821 gunichar c;
2822 char *tag;
2824 g_return_val_if_fail(str != NULL, NULL);
2825 g_return_val_if_fail(x <= y, NULL);
2827 if (x == y)
2828 return g_strdup("");
2830 ret = g_string_new("");
2831 q = g_queue_new();
2833 while (*str && (z < y)) {
2834 c = g_utf8_get_char(str);
2836 if (c == '<') {
2837 char *end = strchr(str, '>');
2839 if (!end) {
2840 g_string_free(ret, TRUE);
2841 while ((tag = g_queue_pop_head(q)))
2842 g_free(tag);
2843 g_queue_free(q);
2844 return NULL;
2847 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
2848 z += strlen("[Image]");
2849 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
2850 z += 1;
2851 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
2852 z += strlen("\n---\n");
2853 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
2854 /* pop stack */
2855 char *tmp;
2857 tmp = g_queue_pop_head(q);
2858 g_free(tmp);
2859 /* z += 0; */
2860 } else {
2861 /* push it unto the stack */
2862 char *tmp;
2864 tmp = g_strndup(str, end - str + 1);
2865 g_queue_push_head(q, tmp);
2866 /* z += 0; */
2869 if (z >= x) {
2870 g_string_append_len(ret, str, end - str + 1);
2873 str = end;
2874 } else if (c == '&') {
2875 char *end = strchr(str, ';');
2876 if (!end) {
2877 g_string_free(ret, TRUE);
2878 while ((tag = g_queue_pop_head(q)))
2879 g_free(tag);
2880 g_queue_free(q);
2882 return NULL;
2885 if (z >= x)
2886 g_string_append_len(ret, str, end - str + 1);
2888 z++;
2889 str = end;
2890 } else {
2891 if (z == x && z > 0 && !appended) {
2892 GList *l = q->tail;
2894 while (l) {
2895 tag = l->data;
2896 g_string_append(ret, tag);
2897 l = l->prev;
2899 appended = TRUE;
2902 if (z >= x)
2903 g_string_append_unichar(ret, c);
2904 z++;
2907 str = g_utf8_next_char(str);
2910 while ((tag = g_queue_pop_head(q))) {
2911 char *name;
2913 name = purple_markup_get_tag_name(tag);
2914 g_string_append_printf(ret, "</%s>", name);
2915 g_free(name);
2916 g_free(tag);
2919 g_queue_free(q);
2920 return g_string_free(ret, FALSE);
2923 char *
2924 purple_markup_get_tag_name(const char *tag)
2926 int i;
2927 g_return_val_if_fail(tag != NULL, NULL);
2928 g_return_val_if_fail(*tag == '<', NULL);
2930 for (i = 1; tag[i]; i++)
2931 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
2932 break;
2934 return g_strndup(tag+1, i-1);
2937 /**************************************************************************
2938 * Path/Filename Functions
2939 **************************************************************************/
2940 const char *
2941 purple_home_dir(void)
2943 #ifndef _WIN32
2944 return g_get_home_dir();
2945 #else
2946 return wpurple_home_dir();
2947 #endif
2950 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2951 const char *
2952 purple_user_dir(void)
2954 if (custom_user_dir != NULL)
2955 return custom_user_dir;
2956 else if (!user_dir)
2957 user_dir = g_build_filename(purple_home_dir(), ".purple", NULL);
2959 return user_dir;
2962 static const gchar *
2963 purple_xdg_dir(gchar **xdg_dir, const gchar *xdg_base_dir, const gchar *xdg_type)
2965 if (!*xdg_dir) {
2966 if (!custom_user_dir) {
2967 *xdg_dir = g_build_filename(xdg_base_dir, "purple", NULL);
2968 } else {
2969 *xdg_dir = g_build_filename(custom_user_dir, xdg_type, NULL);
2973 return *xdg_dir;
2976 const gchar *
2977 purple_cache_dir(void)
2979 return purple_xdg_dir(&cache_dir, g_get_user_cache_dir(), "cache");
2982 const gchar *
2983 purple_config_dir(void)
2985 return purple_xdg_dir(&config_dir, g_get_user_config_dir(), "config");
2988 const gchar *
2989 purple_data_dir(void)
2991 return purple_xdg_dir(&data_dir, g_get_user_data_dir(), "data");
2994 void
2995 purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path)
2997 char *xdg_path;
2998 gboolean xdg_path_exists;
3000 /* Check if destination dir exists, otherwise create it */
3001 xdg_path_exists = g_file_test(purple_xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
3002 if (!xdg_path_exists) {
3003 gint mkdir_res;
3005 mkdir_res = purple_build_dir(purple_xdg_dir, S_IRWXU);
3006 if (mkdir_res == -1) {
3007 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
3008 purple_xdg_dir, g_strerror(errno));
3009 return;
3013 xdg_path = g_build_filename(purple_xdg_dir, path, NULL);
3014 xdg_path_exists = g_file_test(xdg_path, G_FILE_TEST_EXISTS);
3015 if (!xdg_path_exists) {
3016 char *old_path;
3017 gboolean old_path_exists;
3019 old_path = g_build_filename(purple_user_dir(), path, NULL);
3020 old_path_exists = g_file_test(old_path, G_FILE_TEST_EXISTS);
3021 if (old_path_exists) {
3022 g_rename(old_path, xdg_path);
3025 g_free(old_path);
3026 old_path = NULL;
3029 g_free(xdg_path);
3030 xdg_path = NULL;
3032 return;
3035 void purple_util_set_user_dir(const char *dir)
3037 g_free(custom_user_dir);
3039 if (dir != NULL && *dir)
3040 custom_user_dir = g_strdup(dir);
3041 else
3042 custom_user_dir = NULL;
3045 int purple_build_dir(const char *path, int mode)
3047 return g_mkdir_with_parents(path, mode);
3050 static gboolean
3051 purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size)
3053 gchar *filename_full;
3054 gboolean ret = FALSE;
3056 g_return_val_if_fail(dir != NULL, FALSE);
3058 purple_debug_misc("util", "Writing file %s to directory %s",
3059 filename, dir);
3061 /* Ensure the directory exists */
3062 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
3064 if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
3066 purple_debug_error("util", "Error creating directory %s: %s\n",
3067 dir, g_strerror(errno));
3068 return FALSE;
3072 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dir, filename);
3074 ret = purple_util_write_data_to_file_absolute(filename_full, data, size);
3076 g_free(filename_full);
3077 return ret;
3080 gboolean
3081 purple_util_write_data_to_file(const char *filename, const char *data, gssize size)
3083 const char *user_dir = purple_user_dir();
3084 gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size);
3086 return ret;
3089 gboolean
3090 purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size)
3092 const char *cache_dir = purple_cache_dir();
3093 gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size);
3095 return ret;
3098 gboolean
3099 purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size)
3101 const char *config_dir = purple_config_dir();
3102 gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size);
3104 return ret;
3107 gboolean
3108 purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size)
3110 const char *data_dir = purple_data_dir();
3111 gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size);
3113 return ret;
3117 * This function is long and beautiful, like my--um, yeah. Anyway,
3118 * it includes lots of error checking so as we don't overwrite
3119 * people's settings if there is a problem writing the new values.
3121 gboolean
3122 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size)
3124 gchar *filename_temp;
3125 FILE *file;
3126 gsize real_size, byteswritten;
3127 GStatBuf st;
3128 #ifndef HAVE_FILENO
3129 int fd;
3130 #endif
3132 purple_debug_misc("util", "Writing file %s",
3133 filename_full);
3135 g_return_val_if_fail((size >= -1), FALSE);
3137 filename_temp = g_strdup_printf("%s.save", filename_full);
3139 /* Remove an old temporary file, if one exists */
3140 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS))
3142 if (g_unlink(filename_temp) == -1)
3144 purple_debug_error("util", "Error removing old file "
3145 "%s: %s\n",
3146 filename_temp, g_strerror(errno));
3150 /* Open file */
3151 file = g_fopen(filename_temp, "wb");
3152 if (file == NULL)
3154 purple_debug_error("util", "Error opening file %s for "
3155 "writing: %s\n",
3156 filename_temp, g_strerror(errno));
3157 g_free(filename_temp);
3158 return FALSE;
3161 /* Write to file */
3162 real_size = (size == -1) ? strlen(data) : (size_t) size;
3163 byteswritten = fwrite(data, 1, real_size, file);
3165 #ifdef HAVE_FILENO
3166 #ifndef _WIN32
3167 /* Set file permissions */
3168 if (fchmod(fileno(file), S_IRUSR | S_IWUSR) == -1) {
3169 purple_debug_error("util", "Error setting permissions of "
3170 "file %s: %s\n", filename_temp, g_strerror(errno));
3172 #endif
3174 /* Apparently XFS (and possibly other filesystems) do not
3175 * guarantee that file data is flushed before file metadata,
3176 * so this procedure is insufficient without some flushage. */
3177 if (fflush(file) < 0) {
3178 purple_debug_error("util", "Error flushing %s: %s\n",
3179 filename_temp, g_strerror(errno));
3180 g_free(filename_temp);
3181 fclose(file);
3182 return FALSE;
3184 if (fsync(fileno(file)) < 0) {
3185 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
3186 filename_temp, g_strerror(errno));
3187 g_free(filename_temp);
3188 fclose(file);
3189 return FALSE;
3191 #endif
3193 /* Close file */
3194 if (fclose(file) != 0)
3196 purple_debug_error("util", "Error closing file %s: %s\n",
3197 filename_temp, g_strerror(errno));
3198 g_free(filename_temp);
3199 return FALSE;
3202 #ifndef HAVE_FILENO
3203 /* This is the same effect (we hope) as the HAVE_FILENO block
3204 * above, but for systems without fileno(). */
3205 if ((fd = open(filename_temp, O_RDWR)) < 0) {
3206 purple_debug_error("util", "Error opening file %s for flush: %s\n",
3207 filename_temp, g_strerror(errno));
3208 g_free(filename_temp);
3209 return FALSE;
3212 #ifndef _WIN32
3213 /* copy-pasta! */
3214 if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) {
3215 purple_debug_error("util", "Error setting permissions of "
3216 "file %s: %s\n", filename_temp, g_strerror(errno));
3218 #endif
3220 if (fsync(fd) < 0) {
3221 purple_debug_error("util", "Error syncing %s: %s\n",
3222 filename_temp, g_strerror(errno));
3223 g_free(filename_temp);
3224 close(fd);
3225 return FALSE;
3227 if (close(fd) < 0) {
3228 purple_debug_error("util", "Error closing %s after sync: %s\n",
3229 filename_temp, g_strerror(errno));
3230 g_free(filename_temp);
3231 return FALSE;
3233 #endif
3235 /* Ensure the file is the correct size */
3236 if (byteswritten != real_size)
3238 purple_debug_error("util", "Error writing to file %s: Wrote %"
3239 G_GSIZE_FORMAT " bytes "
3240 "but should have written %" G_GSIZE_FORMAT
3241 "; is your disk full?\n",
3242 filename_temp, byteswritten, real_size);
3243 g_free(filename_temp);
3244 return FALSE;
3246 #ifndef __COVERITY__
3247 /* Use stat to be absolutely sure.
3248 * It causes TOCTOU coverity warning (against g_rename below),
3249 * but it's not a threat for us.
3251 if ((g_stat(filename_temp, &st) == -1) || ((gsize)st.st_size != real_size)) {
3252 purple_debug_error("util", "Error writing data to file %s: "
3253 "couldn't g_stat file", filename_temp);
3254 g_free(filename_temp);
3255 return FALSE;
3257 #endif /* __COVERITY__ */
3259 /* Rename to the REAL name */
3260 if (g_rename(filename_temp, filename_full) == -1)
3262 purple_debug_error("util", "Error renaming %s to %s: %s\n",
3263 filename_temp, filename_full,
3264 g_strerror(errno));
3267 g_free(filename_temp);
3269 return TRUE;
3272 PurpleXmlNode *
3273 purple_util_read_xml_from_file(const char *filename, const char *description)
3275 return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util");
3278 PurpleXmlNode *
3279 purple_util_read_xml_from_cache_file(const char *filename, const char *description)
3281 return purple_xmlnode_from_file(purple_cache_dir(), filename, description, "util");
3284 PurpleXmlNode *
3285 purple_util_read_xml_from_config_file(const char *filename, const char *description)
3287 return purple_xmlnode_from_file(purple_config_dir(), filename, description, "util");
3290 PurpleXmlNode *
3291 purple_util_read_xml_from_data_file(const char *filename, const char *description)
3293 return purple_xmlnode_from_file(purple_data_dir(), filename, description, "util");
3297 * Like mkstemp() but returns a file pointer, uses a pre-set template,
3298 * uses the semantics of tempnam() for the directory to use and allocates
3299 * the space for the filepath.
3301 * Caller is responsible for closing the file and removing it when done,
3302 * as well as freeing the space pointed-to by "path" with g_free().
3304 * Returns NULL on failure and cleans up after itself if so.
3306 static const char *purple_mkstemp_templ = {"purpleXXXXXX"};
3308 FILE *
3309 purple_mkstemp(char **fpath, gboolean binary)
3311 const gchar *tmpdir;
3312 int fd;
3313 FILE *fp = NULL;
3315 g_return_val_if_fail(fpath != NULL, NULL);
3317 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
3318 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) {
3319 fd = g_mkstemp(*fpath);
3320 if(fd == -1) {
3321 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3322 "Couldn't make \"%s\", error: %d\n",
3323 *fpath, errno);
3324 } else {
3325 if((fp = fdopen(fd, "r+")) == NULL) {
3326 close(fd);
3327 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3328 "Couldn't fdopen(), error: %d\n", errno);
3332 if(!fp) {
3333 g_free(*fpath);
3334 *fpath = NULL;
3337 } else {
3338 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3339 "g_get_tmp_dir() failed!\n");
3342 return fp;
3345 gboolean
3346 purple_program_is_valid(const char *program)
3348 GError *error = NULL;
3349 char **argv;
3350 gchar *progname;
3351 gboolean is_valid = FALSE;
3353 g_return_val_if_fail(program != NULL, FALSE);
3354 g_return_val_if_fail(*program != '\0', FALSE);
3356 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
3357 purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid",
3358 "Could not parse program '%s': %s\n",
3359 program, error->message);
3360 g_error_free(error);
3361 return FALSE;
3364 if (argv == NULL) {
3365 return FALSE;
3368 progname = g_find_program_in_path(argv[0]);
3369 is_valid = (progname != NULL);
3371 if(purple_debug_is_verbose())
3372 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program,
3373 is_valid ? "Valid" : "Invalid");
3375 g_strfreev(argv);
3376 g_free(progname);
3378 return is_valid;
3382 gboolean
3383 purple_running_gnome(void)
3385 #ifndef _WIN32
3386 gchar *tmp = g_find_program_in_path("gvfs-open");
3388 if (tmp == NULL) {
3389 tmp = g_find_program_in_path("gnome-open");
3391 if (tmp == NULL) {
3392 return FALSE;
3396 g_free(tmp);
3398 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
3400 return ((tmp != NULL) && (*tmp != '\0'));
3401 #else
3402 return FALSE;
3403 #endif
3406 gboolean
3407 purple_running_kde(void)
3409 #ifndef _WIN32
3410 gchar *tmp = g_find_program_in_path("kfmclient");
3411 const char *session;
3413 if (tmp == NULL)
3414 return FALSE;
3415 g_free(tmp);
3417 session = g_getenv("KDE_FULL_SESSION");
3418 if (purple_strequal(session, "true"))
3419 return TRUE;
3421 /* If you run Purple from Konsole under !KDE, this will provide a
3422 * a false positive. Since we do the GNOME checks first, this is
3423 * only a problem if you're running something !(KDE || GNOME) and
3424 * you run Purple from Konsole. This really shouldn't be a problem. */
3425 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
3426 #else
3427 return FALSE;
3428 #endif
3431 gboolean
3432 purple_running_osx(void)
3434 #if defined(__APPLE__)
3435 return TRUE;
3436 #else
3437 return FALSE;
3438 #endif
3441 typedef union purple_sockaddr {
3442 struct sockaddr sa;
3443 struct sockaddr_in sa_in;
3444 #if defined(AF_INET6)
3445 struct sockaddr_in6 sa_in6;
3446 #endif
3447 struct sockaddr_storage sa_stor;
3448 } PurpleSockaddr;
3450 char *
3451 purple_fd_get_ip(int fd)
3453 PurpleSockaddr addr;
3454 socklen_t namelen = sizeof(addr);
3455 int family;
3457 g_return_val_if_fail(fd != 0, NULL);
3459 if (getsockname(fd, &(addr.sa), &namelen))
3460 return NULL;
3462 family = addr.sa.sa_family;
3464 if (family == AF_INET) {
3465 return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
3467 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
3468 else if (family == AF_INET6) {
3469 char host[INET6_ADDRSTRLEN];
3470 const char *tmp;
3472 tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
3473 return g_strdup(tmp);
3475 #endif
3477 return NULL;
3481 purple_socket_get_family(int fd)
3483 PurpleSockaddr addr;
3484 socklen_t len = sizeof(addr);
3486 g_return_val_if_fail(fd >= 0, -1);
3488 if (getsockname(fd, &(addr.sa), &len))
3489 return -1;
3491 return addr.sa.sa_family;
3494 gboolean
3495 purple_socket_speaks_ipv4(int fd)
3497 int family;
3499 g_return_val_if_fail(fd >= 0, FALSE);
3501 family = purple_socket_get_family(fd);
3503 switch (family) {
3504 case AF_INET:
3505 return TRUE;
3506 #if defined(IPV6_V6ONLY)
3507 case AF_INET6:
3509 int val = 0;
3510 socklen_t len = sizeof(val);
3512 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0)
3513 return FALSE;
3514 return !val;
3516 #endif
3517 default:
3518 return FALSE;
3522 /**************************************************************************
3523 * String Functions
3524 **************************************************************************/
3525 gboolean
3526 purple_strequal(const gchar *left, const gchar *right)
3528 return (g_strcmp0(left, right) == 0);
3531 const char *
3532 purple_normalize(const PurpleAccount *account, const char *str)
3534 const char *ret = NULL;
3535 static char buf[BUF_LEN];
3537 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3538 g_return_val_if_fail(str != NULL, "");
3540 if (account != NULL)
3542 PurpleProtocol *protocol =
3543 purple_protocols_find(purple_account_get_protocol_id(account));
3545 if (protocol != NULL)
3546 ret = purple_protocol_client_iface_normalize(protocol, account, str);
3549 if (ret == NULL)
3551 char *tmp;
3553 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
3554 g_snprintf(buf, sizeof(buf), "%s", tmp);
3555 g_free(tmp);
3557 ret = buf;
3560 return ret;
3564 * You probably don't want to call this directly, it is
3565 * mainly for use as a protocol callback function. See the
3566 * comments in util.h.
3568 const char *
3569 purple_normalize_nocase(const PurpleAccount *account, const char *str)
3571 static char buf[BUF_LEN];
3572 char *tmp1, *tmp2;
3574 g_return_val_if_fail(str != NULL, NULL);
3576 tmp1 = g_utf8_strdown(str, -1);
3577 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
3578 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : "");
3579 g_free(tmp2);
3580 g_free(tmp1);
3582 return buf;
3585 gboolean
3586 purple_validate(const PurpleProtocol *protocol, const char *str)
3588 const char *normalized;
3590 g_return_val_if_fail(protocol != NULL, FALSE);
3591 g_return_val_if_fail(str != NULL, FALSE);
3593 if (str[0] == '\0')
3594 return FALSE;
3596 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT_IFACE, normalize))
3597 return TRUE;
3599 normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol),
3600 NULL, str);
3602 return (NULL != normalized);
3605 gchar *
3606 purple_strdup_withhtml(const gchar *src)
3608 gulong destsize, i, j;
3609 gchar *dest;
3611 g_return_val_if_fail(src != NULL, NULL);
3613 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3614 destsize = 1;
3615 for (i = 0; src[i] != '\0'; i++)
3617 if (src[i] == '\n')
3618 destsize += 4;
3619 else if (src[i] != '\r')
3620 destsize++;
3623 dest = g_malloc(destsize);
3625 /* Copy stuff, ignoring \r's, because they are dumb */
3626 for (i = 0, j = 0; src[i] != '\0'; i++) {
3627 if (src[i] == '\n') {
3628 strcpy(&dest[j], "<BR>");
3629 j += 4;
3630 } else if (src[i] != '\r')
3631 dest[j++] = src[i];
3634 dest[destsize-1] = '\0';
3636 return dest;
3639 gboolean
3640 purple_str_has_prefix(const char *s, const char *p)
3642 return g_str_has_prefix(s, p);
3645 gboolean
3646 purple_str_has_caseprefix(const gchar *s, const gchar *p)
3648 g_return_val_if_fail(s, FALSE);
3649 g_return_val_if_fail(p, FALSE);
3651 return (g_ascii_strncasecmp(s, p, strlen(p)) == 0);
3654 gboolean
3655 purple_str_has_suffix(const char *s, const char *x)
3657 return g_str_has_suffix(s, x);
3660 char *
3661 purple_str_add_cr(const char *text)
3663 char *ret = NULL;
3664 int count = 0, j;
3665 guint i;
3667 g_return_val_if_fail(text != NULL, NULL);
3669 if (text[0] == '\n')
3670 count++;
3671 for (i = 1; i < strlen(text); i++)
3672 if (text[i] == '\n' && text[i - 1] != '\r')
3673 count++;
3675 if (count == 0)
3676 return g_strdup(text);
3678 ret = g_malloc0(strlen(text) + count + 1);
3680 i = 0; j = 0;
3681 if (text[i] == '\n')
3682 ret[j++] = '\r';
3683 ret[j++] = text[i++];
3684 for (; i < strlen(text); i++) {
3685 if (text[i] == '\n' && text[i - 1] != '\r')
3686 ret[j++] = '\r';
3687 ret[j++] = text[i];
3690 return ret;
3693 void
3694 purple_str_strip_char(char *text, char thechar)
3696 int i, j;
3698 g_return_if_fail(text != NULL);
3700 for (i = 0, j = 0; text[i]; i++)
3701 if (text[i] != thechar)
3702 text[j++] = text[i];
3704 text[j] = '\0';
3707 void
3708 purple_util_chrreplace(char *string, char delimiter,
3709 char replacement)
3711 int i = 0;
3713 g_return_if_fail(string != NULL);
3715 while (string[i] != '\0')
3717 if (string[i] == delimiter)
3718 string[i] = replacement;
3719 i++;
3723 gchar *
3724 purple_strreplace(const char *string, const char *delimiter,
3725 const char *replacement)
3727 gchar **split;
3728 gchar *ret;
3730 g_return_val_if_fail(string != NULL, NULL);
3731 g_return_val_if_fail(delimiter != NULL, NULL);
3732 g_return_val_if_fail(replacement != NULL, NULL);
3734 split = g_strsplit(string, delimiter, 0);
3735 ret = g_strjoinv(replacement, split);
3736 g_strfreev(split);
3738 return ret;
3741 gchar *
3742 purple_strcasereplace(const char *string, const char *delimiter,
3743 const char *replacement)
3745 gchar *ret;
3746 int length_del, length_rep, i, j;
3748 g_return_val_if_fail(string != NULL, NULL);
3749 g_return_val_if_fail(delimiter != NULL, NULL);
3750 g_return_val_if_fail(replacement != NULL, NULL);
3752 length_del = strlen(delimiter);
3753 length_rep = strlen(replacement);
3755 /* Count how many times the delimiter appears */
3756 i = 0; /* position in the source string */
3757 j = 0; /* number of occurrences of "delimiter" */
3758 while (string[i] != '\0') {
3759 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3760 i += length_del;
3761 j += length_rep;
3762 } else {
3763 i++;
3764 j++;
3768 ret = g_malloc(j+1);
3770 i = 0; /* position in the source string */
3771 j = 0; /* position in the destination string */
3772 while (string[i] != '\0') {
3773 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3774 strncpy(&ret[j], replacement, length_rep);
3775 i += length_del;
3776 j += length_rep;
3777 } else {
3778 ret[j] = string[i];
3779 i++;
3780 j++;
3784 ret[j] = '\0';
3786 return ret;
3789 /** TODO: Expose this when we can add API */
3790 static const char *
3791 purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen)
3793 const char *tmp, *ret;
3795 g_return_val_if_fail(haystack != NULL, NULL);
3796 g_return_val_if_fail(needle != NULL, NULL);
3798 if (hlen == -1)
3799 hlen = strlen(haystack);
3800 if (nlen == -1)
3801 nlen = strlen(needle);
3802 tmp = haystack,
3803 ret = NULL;
3805 g_return_val_if_fail(hlen > 0, NULL);
3806 g_return_val_if_fail(nlen > 0, NULL);
3808 while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) {
3809 if (!g_ascii_strncasecmp(needle, tmp, nlen))
3810 ret = tmp;
3811 else
3812 tmp++;
3815 return ret;
3818 const char *
3819 purple_strcasestr(const char *haystack, const char *needle)
3821 return purple_strcasestr_len(haystack, -1, needle, -1);
3824 char *
3825 purple_str_size_to_units(goffset size)
3827 static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3828 float size_mag;
3829 gsize size_index = 0;
3831 if (size == -1) {
3832 return g_strdup(_("Calculating..."));
3834 else if (size == 0) {
3835 return g_strdup(_("Unknown."));
3837 else {
3838 size_mag = (float)size;
3840 while ((size_index < G_N_ELEMENTS(size_str) - 1) && (size_mag > 1024)) {
3841 size_mag /= 1024;
3842 size_index++;
3845 if (size_index == 0) {
3846 return g_strdup_printf("%" G_GOFFSET_FORMAT " %s", size, _(size_str[size_index]));
3847 } else {
3848 return g_strdup_printf("%.2f %s", size_mag, _(size_str[size_index]));
3853 char *
3854 purple_str_seconds_to_string(guint secs)
3856 char *ret = NULL;
3857 guint days, hrs, mins;
3859 if (secs < 60)
3861 return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs);
3864 days = secs / (60 * 60 * 24);
3865 secs = secs % (60 * 60 * 24);
3866 hrs = secs / (60 * 60);
3867 secs = secs % (60 * 60);
3868 mins = secs / 60;
3869 /* secs = secs % 60; */
3871 if (days > 0)
3873 ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days);
3876 if (hrs > 0)
3878 if (ret != NULL)
3880 char *tmp = g_strdup_printf(
3881 dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs),
3882 ret, hrs);
3883 g_free(ret);
3884 ret = tmp;
3886 else
3887 ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs);
3890 if (mins > 0)
3892 if (ret != NULL)
3894 char *tmp = g_strdup_printf(
3895 dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins),
3896 ret, mins);
3897 g_free(ret);
3898 ret = tmp;
3900 else
3901 ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins);
3904 return ret;
3908 char *
3909 purple_str_binary_to_ascii(const unsigned char *binary, guint len)
3911 GString *ret;
3912 guint i;
3914 g_return_val_if_fail(len > 0, NULL);
3916 ret = g_string_sized_new(len);
3918 for (i = 0; i < len; i++)
3919 if (binary[i] < 32 || binary[i] > 126)
3920 g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF);
3921 else if (binary[i] == '\\')
3922 g_string_append(ret, "\\\\");
3923 else
3924 g_string_append_c(ret, binary[i]);
3926 return g_string_free(ret, FALSE);
3929 size_t
3930 purple_utf16_size(const gunichar2 *str)
3932 /* UTF16 cannot contain two consequent NUL bytes starting at even
3933 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3934 * Chapter 2.
3937 size_t i = 0;
3939 g_return_val_if_fail(str != NULL, 0);
3941 while (str[i++]);
3943 return i * sizeof(gunichar2);
3946 void
3947 purple_str_wipe(gchar *str)
3949 if (str == NULL)
3950 return;
3951 memset(str, 0, strlen(str));
3952 g_free(str);
3955 void
3956 purple_utf16_wipe(gunichar2 *str)
3958 if (str == NULL)
3959 return;
3960 memset(str, 0, purple_utf16_size(str));
3961 g_free(str);
3964 /**************************************************************************
3965 * URI/URL Functions
3966 **************************************************************************/
3968 void purple_got_protocol_handler_uri(const char *uri)
3970 char proto[11];
3971 char delimiter;
3972 const char *tmp, *param_string;
3973 char *cmd;
3974 GHashTable *params = NULL;
3975 gsize len;
3976 if (!(tmp = strchr(uri, ':')) || tmp == uri) {
3977 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3978 return;
3981 len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri));
3983 strncpy(proto, uri, len);
3984 proto[len] = '\0';
3986 tmp++;
3988 if (g_str_equal(proto, "xmpp"))
3989 delimiter = ';';
3990 else
3991 delimiter = '&';
3993 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter);
3995 if ((param_string = strchr(tmp, '?'))) {
3996 const char *keyend = NULL, *pairstart;
3997 char *key, *value = NULL;
3999 cmd = g_strndup(tmp, (param_string - tmp));
4000 param_string++;
4002 params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
4003 pairstart = tmp = param_string;
4005 while (*tmp || *pairstart) {
4006 if (*tmp == delimiter || !(*tmp)) {
4007 /* If there is no explicit value */
4008 if (keyend == NULL) {
4009 keyend = tmp;
4011 /* without these brackets, clang won't
4012 * recognize tmp as a non-NULL
4015 if (keyend && keyend != pairstart) {
4016 char *p;
4017 key = g_strndup(pairstart, (keyend - pairstart));
4018 /* If there is an explicit value */
4019 if (keyend != tmp && keyend != (tmp - 1))
4020 value = g_strndup(keyend + 1, (tmp - keyend - 1));
4021 for (p = key; *p; ++p)
4022 *p = g_ascii_tolower(*p);
4023 g_hash_table_insert(params, key, value);
4025 keyend = value = NULL;
4026 pairstart = (*tmp) ? tmp + 1 : tmp;
4027 } else if (*tmp == '=')
4028 keyend = tmp;
4030 if (*tmp)
4031 tmp++;
4033 } else
4034 cmd = g_strdup(tmp);
4036 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params);
4038 g_free(cmd);
4039 if (params)
4040 g_hash_table_destroy(params);
4043 const char *
4044 purple_url_decode(const char *str)
4046 static char buf[BUF_LEN];
4047 guint i, j = 0;
4048 char *bum;
4049 char hex[3];
4051 g_return_val_if_fail(str != NULL, NULL);
4054 * XXX - This check could be removed and buf could be made
4055 * dynamically allocated, but this is easier.
4057 if (strlen(str) >= BUF_LEN)
4058 return NULL;
4060 for (i = 0; i < strlen(str); i++) {
4062 if (str[i] != '%')
4063 buf[j++] = str[i];
4064 else {
4065 strncpy(hex, str + ++i, 2);
4066 hex[2] = '\0';
4068 /* i is pointing to the start of the number */
4069 i++;
4072 * Now it's at the end and at the start of the for loop
4073 * will be at the next character.
4075 buf[j++] = strtol(hex, NULL, 16);
4079 buf[j] = '\0';
4081 if (!g_utf8_validate(buf, -1, (const char **)&bum))
4082 *bum = '\0';
4084 return buf;
4087 const char *
4088 purple_url_encode(const char *str)
4090 const char *iter;
4091 static char buf[BUF_LEN];
4092 char utf_char[6];
4093 guint i, j = 0;
4095 g_return_val_if_fail(str != NULL, NULL);
4096 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4098 iter = str;
4099 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4100 gunichar c = g_utf8_get_char(iter);
4101 /* If the character is an ASCII character and is alphanumeric
4102 * no need to escape */
4103 if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
4104 buf[j++] = c;
4105 } else {
4106 int bytes = g_unichar_to_utf8(c, utf_char);
4107 for (i = 0; (int)i < bytes; i++) {
4108 if (j > (BUF_LEN - 4))
4109 break;
4110 if (i >= sizeof(utf_char)) {
4111 g_warn_if_reached();
4112 break;
4114 sprintf(buf + j, "%%%02X", utf_char[i] & 0xff);
4115 j += 3;
4120 buf[j] = '\0';
4122 return buf;
4125 /* Originally lifted from
4126 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
4127 * ... and slightly modified to be a bit more rfc822 compliant
4128 * ... and modified a bit more to make domain checking rfc1035 compliant
4129 * with the exception permitted in rfc1101 for domains to start with digit
4130 * but not completely checking to avoid conflicts with IP addresses
4132 gboolean
4133 purple_email_is_valid(const char *address)
4135 const char *c, *domain;
4136 static char *rfc822_specials = "()<>@,;:\\\"[]";
4138 g_return_val_if_fail(address != NULL, FALSE);
4140 if (*address == '.') return FALSE;
4142 /* first we validate the name portion (name@domain) (rfc822)*/
4143 for (c = address; *c; c++) {
4144 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
4145 while (*++c) {
4146 if (*c == '\\') {
4147 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue;
4148 else return FALSE;
4150 if (*c == '\"') break;
4151 if (*c < ' ' || *c >= 127) return FALSE;
4153 if (!*c++) return FALSE;
4154 if (*c == '@') break;
4155 if (*c != '.') return FALSE;
4156 continue;
4158 if (*c == '@') break;
4159 if (*c <= ' ' || *c >= 127) return FALSE;
4160 if (strchr(rfc822_specials, *c)) return FALSE;
4163 /* It's obviously not an email address if we didn't find an '@' above */
4164 if (*c == '\0') return FALSE;
4166 /* strictly we should return false if (*(c - 1) == '.') too, but I think
4167 * we should permit user.@domain type addresses - they do work :) */
4168 if (c == address) return FALSE;
4170 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
4171 if (!*(domain = ++c)) return FALSE;
4172 do {
4173 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
4174 return FALSE;
4175 if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE;
4176 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
4177 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
4178 } while (*++c);
4180 if (*(c - 1) == '-') return FALSE;
4182 return ((c - domain) > 3 ? TRUE : FALSE);
4185 gboolean
4186 purple_ipv4_address_is_valid(const char *ip)
4188 int c, o1, o2, o3, o4;
4189 char end;
4191 g_return_val_if_fail(ip != NULL, FALSE);
4193 c = sscanf(ip, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, &end);
4194 if (c != 4 || o1 < 0 || o1 > 255 || o2 < 0 || o2 > 255 || o3 < 0 || o3 > 255 || o4 < 0 || o4 > 255)
4195 return FALSE;
4196 return TRUE;
4199 gboolean
4200 purple_ipv6_address_is_valid(const gchar *ip)
4202 const gchar *c;
4203 gboolean double_colon = FALSE;
4204 gint chunks = 1;
4205 gint in = 0;
4207 g_return_val_if_fail(ip != NULL, FALSE);
4209 if (*ip == '\0')
4210 return FALSE;
4212 for (c = ip; *c; ++c) {
4213 if ((*c >= '0' && *c <= '9') ||
4214 (*c >= 'a' && *c <= 'f') ||
4215 (*c >= 'A' && *c <= 'F')) {
4216 if (++in > 4)
4217 /* Only four hex digits per chunk */
4218 return FALSE;
4219 continue;
4220 } else if (*c == ':') {
4221 /* The start of a new chunk */
4222 ++chunks;
4223 in = 0;
4224 if (*(c + 1) == ':') {
4226 * '::' indicates a consecutive series of chunks full
4227 * of zeroes. There can be only one of these per address.
4229 if (double_colon)
4230 return FALSE;
4231 double_colon = TRUE;
4233 } else
4234 return FALSE;
4238 * Either we saw a '::' and there were fewer than 8 chunks -or-
4239 * we didn't see a '::' and saw exactly 8 chunks.
4241 return (double_colon && chunks < 8) || (!double_colon && chunks == 8);
4244 gboolean
4245 purple_ip_address_is_valid(const char *ip)
4247 return (purple_ipv4_address_is_valid(ip) || purple_ipv6_address_is_valid(ip));
4250 /* Stolen from gnome_uri_list_extract_uris */
4251 GList *
4252 purple_uri_list_extract_uris(const gchar *uri_list)
4254 const gchar *p, *q;
4255 gchar *retval;
4256 GList *result = NULL;
4258 g_return_val_if_fail (uri_list != NULL, NULL);
4260 p = uri_list;
4262 /* We don't actually try to validate the URI according to RFC
4263 * 2396, or even check for allowed characters - we just ignore
4264 * comments and trim whitespace off the ends. We also
4265 * allow LF delimination as well as the specified CRLF.
4267 while (p) {
4268 if (*p != '#') {
4269 while (isspace(*p))
4270 p++;
4272 q = p;
4273 while (*q && (*q != '\n') && (*q != '\r'))
4274 q++;
4276 if (q > p) {
4277 q--;
4278 while (q > p && isspace(*q))
4279 q--;
4281 retval = (gchar*)g_malloc (q - p + 2);
4282 strncpy (retval, p, q - p + 1);
4283 retval[q - p + 1] = '\0';
4285 result = g_list_prepend (result, retval);
4288 p = strchr (p, '\n');
4289 if (p)
4290 p++;
4293 return g_list_reverse (result);
4297 /* Stolen from gnome_uri_list_extract_filenames */
4298 GList *
4299 purple_uri_list_extract_filenames(const gchar *uri_list)
4301 GList *tmp_list, *node, *result;
4303 g_return_val_if_fail (uri_list != NULL, NULL);
4305 result = purple_uri_list_extract_uris(uri_list);
4307 tmp_list = result;
4308 while (tmp_list) {
4309 gchar *s = (gchar*)tmp_list->data;
4311 node = tmp_list;
4312 tmp_list = tmp_list->next;
4314 if (!strncmp (s, "file:", 5)) {
4315 node->data = g_filename_from_uri (s, NULL, NULL);
4316 /* not sure if this fallback is useful at all */
4317 if (!node->data) node->data = g_strdup (s+5);
4318 } else {
4319 result = g_list_delete_link(result, node);
4321 g_free (s);
4323 return result;
4326 /**************************************************************************
4327 * UTF8 String Functions
4328 **************************************************************************/
4329 gchar *
4330 purple_utf8_try_convert(const char *str)
4332 gsize converted;
4333 gchar *utf8;
4335 g_return_val_if_fail(str != NULL, NULL);
4337 if (g_utf8_validate(str, -1, NULL)) {
4338 return g_strdup(str);
4341 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
4342 if (utf8 != NULL)
4343 return utf8;
4345 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
4346 if ((utf8 != NULL) && (converted == strlen(str)))
4347 return utf8;
4349 g_free(utf8);
4351 return NULL;
4354 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
4355 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
4356 gchar *
4357 purple_utf8_salvage(const char *str)
4359 GString *workstr;
4360 const char *end;
4362 g_return_val_if_fail(str != NULL, NULL);
4364 workstr = g_string_sized_new(strlen(str));
4366 do {
4367 (void)g_utf8_validate(str, -1, &end);
4368 workstr = g_string_append_len(workstr, str, end - str);
4369 str = end;
4370 if (*str == '\0')
4371 break;
4372 do {
4373 workstr = g_string_append_c(workstr, '?');
4374 str++;
4375 } while (!utf8_first(*str));
4376 } while (*str != '\0');
4378 return g_string_free(workstr, FALSE);
4381 gchar *
4382 purple_utf8_strip_unprintables(const gchar *str)
4384 gchar *workstr, *iter;
4385 const gchar *bad;
4387 if (str == NULL)
4388 /* Act like g_strdup */
4389 return NULL;
4391 if (!g_utf8_validate(str, -1, &bad)) {
4392 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
4393 "first bad character was %02x (%c)\n",
4394 str, *bad, *bad);
4395 g_return_val_if_reached(NULL);
4398 workstr = iter = g_new(gchar, strlen(str) + 1);
4399 while (*str) {
4400 gunichar ch = g_utf8_get_char(str);
4401 gchar *next = g_utf8_next_char(str);
4403 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
4404 * [#x10000-#x10FFFF]
4406 if ((ch == '\t' || ch == '\n' || ch == '\r') ||
4407 (ch >= 0x20 && ch <= 0xD7FF) ||
4408 (ch >= 0xE000 && ch <= 0xFFFD) ||
4409 (ch >= 0x10000 && ch <= 0x10FFFF)) {
4410 memcpy(iter, str, next - str);
4411 iter += (next - str);
4414 str = next;
4417 /* nul-terminate the new string */
4418 *iter = '\0';
4420 return workstr;
4424 * This function is copied from g_strerror() but changed to use
4425 * gai_strerror().
4427 const gchar *
4428 purple_gai_strerror(gint errnum)
4430 static GPrivate msg_private = G_PRIVATE_INIT(g_free);
4431 char *msg;
4432 int saved_errno = errno;
4434 const char *msg_locale;
4436 msg_locale = gai_strerror(errnum);
4437 if (g_get_charset(NULL))
4439 /* This string is already UTF-8--great! */
4440 errno = saved_errno;
4441 return msg_locale;
4443 else
4445 gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
4446 if (msg_utf8)
4448 /* Stick in the quark table so that we can return a static result */
4449 GQuark msg_quark = g_quark_from_string(msg_utf8);
4450 g_free(msg_utf8);
4452 msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
4453 errno = saved_errno;
4454 return msg_utf8;
4458 msg = g_private_get(&msg_private);
4460 if (!msg)
4462 msg = g_new(gchar, 64);
4463 g_private_set(&msg_private, msg);
4466 sprintf(msg, "unknown error (%d)", errnum);
4468 errno = saved_errno;
4469 return msg;
4472 char *
4473 purple_utf8_ncr_encode(const char *str)
4475 GString *out;
4477 g_return_val_if_fail(str != NULL, NULL);
4478 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4480 out = g_string_new("");
4482 for(; *str; str = g_utf8_next_char(str)) {
4483 gunichar wc = g_utf8_get_char(str);
4485 /* super simple check. hopefully not too wrong. */
4486 if(wc >= 0x80) {
4487 g_string_append_printf(out, "&#%u;", (guint32) wc);
4488 } else {
4489 g_string_append_unichar(out, wc);
4493 return g_string_free(out, FALSE);
4497 char *
4498 purple_utf8_ncr_decode(const char *str)
4500 GString *out;
4501 char *buf, *b;
4503 g_return_val_if_fail(str != NULL, NULL);
4504 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4506 buf = (char *) str;
4507 out = g_string_new("");
4509 while( (b = strstr(buf, "&#")) ) {
4510 gunichar wc;
4511 int base = 0;
4513 /* append everything leading up to the &# */
4514 g_string_append_len(out, buf, b-buf);
4516 b += 2; /* skip past the &# */
4518 /* strtoul will treat 0x prefix as hex, but not just x */
4519 if(*b == 'x' || *b == 'X') {
4520 base = 16;
4521 b++;
4524 /* advances buf to the end of the ncr segment */
4525 wc = (gunichar) strtoul(b, &buf, base);
4527 /* this mimics the previous impl of ncr_decode */
4528 if(*buf == ';') {
4529 g_string_append_unichar(out, wc);
4530 buf++;
4534 /* append whatever's left */
4535 g_string_append(out, buf);
4537 return g_string_free(out, FALSE);
4542 purple_utf8_strcasecmp(const char *a, const char *b)
4544 char *a_norm = NULL;
4545 char *b_norm = NULL;
4546 int ret = -1;
4548 if(!a && b)
4549 return -1;
4550 else if(!b && a)
4551 return 1;
4552 else if(!a && !b)
4553 return 0;
4555 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
4557 purple_debug_error("purple_utf8_strcasecmp",
4558 "One or both parameters are invalid UTF8\n");
4559 return ret;
4562 a_norm = g_utf8_casefold(a, -1);
4563 b_norm = g_utf8_casefold(b, -1);
4564 ret = g_utf8_collate(a_norm, b_norm);
4565 g_free(a_norm);
4566 g_free(b_norm);
4568 return ret;
4571 /* previously conversation::find_nick() */
4572 gboolean
4573 purple_utf8_has_word(const char *haystack, const char *needle)
4575 char *hay, *pin, *p;
4576 const char *start, *prev_char;
4577 gunichar before, after;
4578 int n;
4579 gboolean ret = FALSE;
4581 start = hay = g_utf8_strdown(haystack, -1);
4583 pin = g_utf8_strdown(needle, -1);
4584 n = strlen(pin);
4586 while ((p = strstr(start, pin)) != NULL) {
4587 prev_char = g_utf8_find_prev_char(hay, p);
4588 before = -2;
4589 if (prev_char) {
4590 before = g_utf8_get_char(prev_char);
4592 after = g_utf8_get_char_validated(p + n, - 1);
4594 if ((p == hay ||
4595 /* The character before is a reasonable guess for a word boundary
4596 ("!g_unichar_isalnum()" is not a valid way to determine word
4597 boundaries, but it is the only reasonable thing to do here),
4598 and isn't the '&' from a "&amp;" or some such entity*/
4599 (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&'))
4600 && after != (gunichar)-2 && !g_unichar_isalnum(after)) {
4601 ret = TRUE;
4602 break;
4604 start = p + 1;
4607 g_free(pin);
4608 g_free(hay);
4610 return ret;
4613 void
4614 purple_print_utf8_to_console(FILE *filestream, char *message)
4616 gchar *message_conv;
4617 GError *error = NULL;
4619 /* Try to convert 'message' to user's locale */
4620 message_conv = g_locale_from_utf8(message, -1, NULL, NULL, &error);
4621 if (message_conv != NULL) {
4622 fputs(message_conv, filestream);
4623 g_free(message_conv);
4625 else
4627 /* use 'message' as a fallback */
4628 g_warning("%s\n", error->message);
4629 g_error_free(error);
4630 fputs(message, filestream);
4634 gboolean purple_message_meify(char *message, gssize len)
4636 char *c;
4637 gboolean inside_html = FALSE;
4639 g_return_val_if_fail(message != NULL, FALSE);
4641 if(len == -1)
4642 len = strlen(message);
4644 for (c = message; *c; c++, len--) {
4645 if(inside_html) {
4646 if(*c == '>')
4647 inside_html = FALSE;
4648 } else {
4649 if(*c == '<')
4650 inside_html = TRUE;
4651 else
4652 break;
4656 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
4657 memmove(c, c+4, len-3);
4658 return TRUE;
4661 return FALSE;
4664 char *purple_text_strip_mnemonic(const char *in)
4666 char *out;
4667 char *a;
4668 char *a0;
4669 const char *b;
4671 g_return_val_if_fail(in != NULL, NULL);
4673 out = g_malloc(strlen(in)+1);
4674 a = out;
4675 b = in;
4677 a0 = a; /* The last non-space char seen so far, or the first char */
4679 while(*b) {
4680 if(*b == '_') {
4681 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
4682 /* Detected CJK style shortcut (Bug 875311) */
4683 a = a0; /* undo the left parenthesis */
4684 b += 3; /* and skip the whole mess */
4685 } else if(*(b+1) == '_') {
4686 *(a++) = '_';
4687 b += 2;
4688 a0 = a;
4689 } else {
4690 b++;
4692 /* We don't want to corrupt the middle of UTF-8 characters */
4693 } else if (!(*b & 0x80)) { /* other 1-byte char */
4694 if (*b != ' ')
4695 a0 = a;
4696 *(a++) = *(b++);
4697 } else {
4698 /* Multibyte utf8 char, don't look for _ inside these */
4699 int n = 0;
4700 int i;
4701 if ((*b & 0xe0) == 0xc0) {
4702 n = 2;
4703 } else if ((*b & 0xf0) == 0xe0) {
4704 n = 3;
4705 } else if ((*b & 0xf8) == 0xf0) {
4706 n = 4;
4707 } else if ((*b & 0xfc) == 0xf8) {
4708 n = 5;
4709 } else if ((*b & 0xfe) == 0xfc) {
4710 n = 6;
4711 } else { /* Illegal utf8 */
4712 n = 1;
4714 a0 = a; /* unless we want to delete CJK spaces too */
4715 for (i = 0; i < n && *b; i += 1) {
4716 *(a++) = *(b++);
4720 *a = '\0';
4722 return out;
4725 const char* purple_unescape_filename(const char *escaped) {
4726 return purple_url_decode(escaped);
4730 /* this is almost identical to purple_url_encode (hence purple_url_decode
4731 * being used above), but we want to keep certain characters unescaped
4732 * for compat reasons */
4733 const char *
4734 purple_escape_filename(const char *str)
4736 const char *iter;
4737 static char buf[BUF_LEN];
4738 char utf_char[6];
4739 guint i, j = 0;
4741 g_return_val_if_fail(str != NULL, NULL);
4742 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4744 iter = str;
4745 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4746 gunichar c = g_utf8_get_char(iter);
4747 /* If the character is an ASCII character and is alphanumeric,
4748 * or one of the specified values, no need to escape */
4749 if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' ||
4750 c == '_' || c == '.' || c == '#')) {
4751 buf[j++] = c;
4752 } else {
4753 int bytes = g_unichar_to_utf8(c, utf_char);
4754 for (i = 0; (int)i < bytes; i++) {
4755 if (j > (BUF_LEN - 4))
4756 break;
4757 if (i >= sizeof(utf_char)) {
4758 g_warn_if_reached();
4759 break;
4761 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
4762 j += 3;
4766 #ifdef _WIN32
4767 /* File/Directory names in windows cannot end in periods/spaces.
4768 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4770 while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' '))
4771 j--;
4772 #endif
4773 buf[j] = '\0';
4775 return buf;
4778 gchar * purple_escape_js(const gchar *str)
4780 gchar *escaped;
4782 json_node_set_string(escape_js_node, str);
4783 json_generator_set_root(escape_js_gen, escape_js_node);
4784 escaped = json_generator_to_data(escape_js_gen, NULL);
4785 json_node_set_boolean(escape_js_node, FALSE);
4787 return escaped;
4790 void purple_restore_default_signal_handlers(void)
4792 #ifndef _WIN32
4793 signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */
4794 signal(SIGINT, SIG_DFL); /* 2: interrupt program */
4795 signal(SIGQUIT, SIG_DFL); /* 3: quit program */
4796 signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */
4797 signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */
4798 signal(SIGABRT, SIG_DFL); /* 6: abort program */
4800 #ifdef SIGPOLL
4801 signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */
4802 #endif /* SIGPOLL */
4804 #ifdef SIGEMT
4805 signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */
4806 #endif /* SIGEMT */
4808 signal(SIGFPE, SIG_DFL); /* 8: floating point exception */
4809 signal(SIGBUS, SIG_DFL); /* 10: bus error */
4810 signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */
4811 signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */
4812 signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */
4813 signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */
4814 signal(SIGTERM, SIG_DFL); /* 15: software termination signal */
4815 signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */
4816 signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */
4817 signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */
4818 #endif /* !_WIN32 */
4821 static void
4822 set_status_with_attrs(PurpleStatus *status, ...)
4824 va_list args;
4825 va_start(args, status);
4826 purple_status_set_active_with_attrs(status, TRUE, args);
4827 va_end(args);
4830 void purple_util_set_current_song(const char *title, const char *artist, const char *album)
4832 GList *list = purple_accounts_get_all();
4833 for (; list; list = list->next) {
4834 PurplePresence *presence;
4835 PurpleStatus *tune;
4836 PurpleAccount *account = list->data;
4837 if (!purple_account_get_enabled(account, purple_core_get_ui()))
4838 continue;
4840 presence = purple_account_get_presence(account);
4841 tune = purple_presence_get_status(presence, "tune");
4842 if (!tune)
4843 continue;
4844 if (title) {
4845 set_status_with_attrs(tune,
4846 PURPLE_TUNE_TITLE, title,
4847 PURPLE_TUNE_ARTIST, artist,
4848 PURPLE_TUNE_ALBUM, album,
4849 NULL);
4850 } else {
4851 purple_status_set_active(tune, FALSE);
4856 char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused)
4858 GString *string;
4859 char *esc;
4861 if (!title || !*title)
4862 return NULL;
4864 esc = g_markup_escape_text(title, -1);
4865 string = g_string_new("");
4866 g_string_append_printf(string, "%s", esc);
4867 g_free(esc);
4869 if (artist && *artist) {
4870 esc = g_markup_escape_text(artist, -1);
4871 g_string_append_printf(string, _(" - %s"), esc);
4872 g_free(esc);
4875 if (album && *album) {
4876 esc = g_markup_escape_text(album, -1);
4877 g_string_append_printf(string, _(" (%s)"), esc);
4878 g_free(esc);
4881 return g_string_free(string, FALSE);
4884 const gchar *
4885 purple_get_host_name(void)
4887 return g_get_host_name();
4890 gchar *
4891 purple_uuid_random(void)
4893 guint32 tmp, a, b;
4895 tmp = g_random_int();
4896 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */
4897 tmp >>= 12;
4898 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */
4900 tmp = g_random_int();
4902 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4903 g_random_int(),
4904 tmp & 0xFFFF,
4907 (tmp >> 16) & 0xFFFF, g_random_int());
4910 void purple_callback_set_zero(gpointer data)
4912 gpointer *ptr = data;
4914 g_return_if_fail(ptr != NULL);
4916 *ptr = NULL;
4919 GValue *
4920 purple_value_new(GType type)
4922 GValue *ret;
4924 g_return_val_if_fail(type != G_TYPE_NONE, NULL);
4926 ret = g_new0(GValue, 1);
4927 g_value_init(ret, type);
4929 return ret;
4932 GValue *
4933 purple_value_dup(GValue *value)
4935 GValue *ret;
4937 g_return_val_if_fail(value != NULL, NULL);
4939 ret = g_new0(GValue, 1);
4940 g_value_init(ret, G_VALUE_TYPE(value));
4941 g_value_copy(value, ret);
4943 return ret;
4946 void
4947 purple_value_free(GValue *value)
4949 g_return_if_fail(value != NULL);
4951 g_value_unset(value);
4952 g_free(value);
4955 gchar *purple_http_digest_calculate_session_key(
4956 const gchar *algorithm,
4957 const gchar *username,
4958 const gchar *realm,
4959 const gchar *password,
4960 const gchar *nonce,
4961 const gchar *client_nonce)
4963 PurpleHash *hasher;
4964 gchar hash[33]; /* We only support MD5. */
4965 gboolean digest_ok;
4967 g_return_val_if_fail(username != NULL, NULL);
4968 g_return_val_if_fail(realm != NULL, NULL);
4969 g_return_val_if_fail(password != NULL, NULL);
4970 g_return_val_if_fail(nonce != NULL, NULL);
4972 /* Check for a supported algorithm. */
4973 g_return_val_if_fail(algorithm == NULL ||
4974 *algorithm == '\0' ||
4975 g_ascii_strcasecmp(algorithm, "MD5") ||
4976 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
4978 hasher = purple_md5_hash_new();
4979 g_return_val_if_fail(hash != NULL, NULL);
4981 purple_hash_append(hasher, (guchar *)username, strlen(username));
4982 purple_hash_append(hasher, (guchar *)":", 1);
4983 purple_hash_append(hasher, (guchar *)realm, strlen(realm));
4984 purple_hash_append(hasher, (guchar *)":", 1);
4985 purple_hash_append(hasher, (guchar *)password, strlen(password));
4987 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
4989 guchar digest[16];
4991 if (client_nonce == NULL)
4993 g_object_unref(hasher);
4994 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
4995 return NULL;
4998 purple_hash_digest(hasher, digest, sizeof(digest));
5000 purple_hash_reset(hasher);
5001 purple_hash_append(hasher, digest, sizeof(digest));
5002 purple_hash_append(hasher, (guchar *)":", 1);
5003 purple_hash_append(hasher, (guchar *)nonce, strlen(nonce));
5004 purple_hash_append(hasher, (guchar *)":", 1);
5005 purple_hash_append(hasher, (guchar *)client_nonce, strlen(client_nonce));
5008 digest_ok = purple_hash_digest_to_str(hasher, hash, sizeof(hash));
5009 g_object_unref(hasher);
5011 g_return_val_if_fail(digest_ok, NULL);
5013 return g_strdup(hash);
5016 gchar *purple_http_digest_calculate_response(
5017 const gchar *algorithm,
5018 const gchar *method,
5019 const gchar *digest_uri,
5020 const gchar *qop,
5021 const gchar *entity,
5022 const gchar *nonce,
5023 const gchar *nonce_count,
5024 const gchar *client_nonce,
5025 const gchar *session_key)
5027 PurpleHash *hash;
5028 static gchar hash2[33]; /* We only support MD5. */
5029 gboolean digest_ok;
5031 g_return_val_if_fail(method != NULL, NULL);
5032 g_return_val_if_fail(digest_uri != NULL, NULL);
5033 g_return_val_if_fail(nonce != NULL, NULL);
5034 g_return_val_if_fail(session_key != NULL, NULL);
5036 /* Check for a supported algorithm. */
5037 g_return_val_if_fail(algorithm == NULL ||
5038 *algorithm == '\0' ||
5039 g_ascii_strcasecmp(algorithm, "MD5") ||
5040 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
5042 /* Check for a supported "quality of protection". */
5043 g_return_val_if_fail(qop == NULL ||
5044 *qop == '\0' ||
5045 g_ascii_strcasecmp(qop, "auth") ||
5046 g_ascii_strcasecmp(qop, "auth-int"), NULL);
5048 hash = purple_md5_hash_new();
5049 g_return_val_if_fail(hash != NULL, NULL);
5051 purple_hash_append(hash, (guchar *)method, strlen(method));
5052 purple_hash_append(hash, (guchar *)":", 1);
5053 purple_hash_append(hash, (guchar *)digest_uri, strlen(digest_uri));
5055 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
5057 PurpleHash *hash2;
5058 gchar entity_hash[33];
5060 if (entity == NULL)
5062 g_object_unref(hash);
5063 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
5064 return NULL;
5067 hash2 = purple_md5_hash_new();
5068 purple_hash_append(hash2, (guchar *)entity, strlen(entity));
5069 digest_ok = purple_hash_digest_to_str(hash2, entity_hash, sizeof(entity_hash));
5070 g_object_unref(hash2);
5072 if (!digest_ok) {
5073 g_object_unref(hash);
5074 g_return_val_if_reached(NULL);
5077 purple_hash_append(hash, (guchar *)":", 1);
5078 purple_hash_append(hash, (guchar *)entity_hash, strlen(entity_hash));
5081 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5082 purple_hash_reset(hash);
5084 if (!digest_ok) {
5085 g_object_unref(hash);
5086 g_return_val_if_reached(NULL);
5089 purple_hash_append(hash, (guchar *)session_key, strlen(session_key));
5090 purple_hash_append(hash, (guchar *)":", 1);
5091 purple_hash_append(hash, (guchar *)nonce, strlen(nonce));
5092 purple_hash_append(hash, (guchar *)":", 1);
5094 if (qop != NULL && *qop != '\0')
5096 if (nonce_count == NULL)
5098 g_object_unref(hash);
5099 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
5100 return NULL;
5103 if (client_nonce == NULL)
5105 g_object_unref(hash);
5106 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
5107 return NULL;
5110 purple_hash_append(hash, (guchar *)nonce_count, strlen(nonce_count));
5111 purple_hash_append(hash, (guchar *)":", 1);
5112 purple_hash_append(hash, (guchar *)client_nonce, strlen(client_nonce));
5113 purple_hash_append(hash, (guchar *)":", 1);
5115 purple_hash_append(hash, (guchar *)qop, strlen(qop));
5117 purple_hash_append(hash, (guchar *)":", 1);
5120 purple_hash_append(hash, (guchar *)hash2, strlen(hash2));
5121 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5122 g_object_unref(hash);
5124 g_return_val_if_fail(digest_ok, NULL);
5126 return g_strdup(hash2);
5130 _purple_fstat(int fd, GStatBuf *st)
5132 int ret;
5134 g_return_val_if_fail(st != NULL, -1);
5136 #ifdef _WIN32
5137 ret = _fstat(fd, st);
5138 #else
5139 ret = fstat(fd, st);
5140 #endif
5142 return ret;
5145 #if 0
5147 /* Temporarily removed - re-add this when you need ini file support. */
5149 #define PURPLE_KEY_FILE_DEFAULT_MAX_SIZE 102400
5150 #define PURPLE_KEY_FILE_HARD_LIMIT 10485760
5152 gboolean
5153 purple_key_file_load_from_ini(GKeyFile *key_file, const gchar *file,
5154 gsize max_size)
5156 const gchar *header = "[default]\n\n";
5157 int header_len = strlen(header);
5158 int fd;
5159 GStatBuf st;
5160 gsize file_size, buff_size;
5161 gchar *buff;
5162 GError *error = NULL;
5164 g_return_val_if_fail(key_file != NULL, FALSE);
5165 g_return_val_if_fail(file != NULL, FALSE);
5166 g_return_val_if_fail(max_size < PURPLE_KEY_FILE_HARD_LIMIT, FALSE);
5168 if (max_size == 0)
5169 max_size = PURPLE_KEY_FILE_DEFAULT_MAX_SIZE;
5171 fd = g_open(file, O_RDONLY, S_IREAD);
5172 if (fd == -1) {
5173 purple_debug_error("util", "Failed to read ini file %s", file);
5174 return FALSE;
5177 if (_purple_fstat(fd, &st) != 0) {
5178 purple_debug_error("util", "Failed to fstat ini file %s", file);
5179 return FALSE;
5182 file_size = (st.st_size > max_size) ? max_size : st.st_size;
5184 buff_size = file_size + header_len;
5185 buff = g_new(gchar, buff_size);
5186 memcpy(buff, header, header_len);
5187 if (read(fd, buff + header_len, file_size) != (gssize)file_size) {
5188 purple_debug_error("util",
5189 "Failed to read whole ini file %s", file);
5190 g_close(fd, NULL);
5191 free(buff);
5192 return FALSE;
5194 g_close(fd, NULL);
5196 g_key_file_load_from_data(key_file, buff, buff_size,
5197 G_KEY_FILE_NONE, &error);
5199 free(buff);
5201 if (error) {
5202 purple_debug_error("util", "Failed parsing ini file %s: %s",
5203 file, error->message);
5204 return FALSE;
5207 return TRUE;
5209 #endif