Fix some functions descriptions
[pidgin-git.git] / libpurple / util.c
blobec934ee926084f6dfaefd8855795f5b531556a87
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 const gchar *
2963 purple_cache_dir(void)
2965 if (!cache_dir) {
2966 if (!custom_user_dir) {
2967 cache_dir = g_build_filename(g_get_user_cache_dir(), "purple", NULL);
2968 } else {
2969 cache_dir = g_build_filename(custom_user_dir, "cache", NULL);
2973 return cache_dir;
2976 const gchar *
2977 purple_config_dir(void)
2979 if (!config_dir) {
2980 if (!custom_user_dir) {
2981 config_dir = g_build_filename(g_get_user_config_dir(), "purple", NULL);
2982 } else {
2983 config_dir = g_build_filename(custom_user_dir, "config", NULL);
2987 return config_dir;
2990 const gchar *
2991 purple_data_dir(void)
2993 if (!data_dir) {
2994 if (!custom_user_dir) {
2995 data_dir = g_build_filename(g_get_user_data_dir(), "purple", NULL);
2996 } else {
2997 data_dir = g_build_filename(custom_user_dir, "data", NULL);
3001 return data_dir;
3004 void
3005 purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path)
3007 char *xdg_path;
3008 gboolean xdg_path_exists;
3010 /* Check if destination dir exists, otherwise create it */
3011 xdg_path_exists = g_file_test(purple_xdg_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
3012 if (!xdg_path_exists) {
3013 gint mkdir_res;
3015 mkdir_res = purple_build_dir(purple_xdg_dir, S_IRWXU);
3016 if (mkdir_res == -1) {
3017 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
3018 purple_xdg_dir, g_strerror(errno));
3019 return;
3023 xdg_path = g_build_filename(purple_xdg_dir, path, NULL);
3024 xdg_path_exists = g_file_test(xdg_path, G_FILE_TEST_EXISTS);
3025 if (!xdg_path_exists) {
3026 char *old_path;
3027 gboolean old_path_exists;
3029 old_path = g_build_filename(purple_user_dir(), path, NULL);
3030 old_path_exists = g_file_test(old_path, G_FILE_TEST_EXISTS);
3031 if (old_path_exists) {
3032 g_rename(old_path, xdg_path);
3035 g_free(old_path);
3036 old_path = NULL;
3039 g_free(xdg_path);
3040 xdg_path = NULL;
3042 return;
3045 void purple_util_set_user_dir(const char *dir)
3047 g_free(custom_user_dir);
3049 if (dir != NULL && *dir)
3050 custom_user_dir = g_strdup(dir);
3051 else
3052 custom_user_dir = NULL;
3055 int purple_build_dir(const char *path, int mode)
3057 return g_mkdir_with_parents(path, mode);
3060 static gboolean
3061 purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size)
3063 gchar *filename_full;
3064 gboolean ret = FALSE;
3066 g_return_val_if_fail(dir != NULL, FALSE);
3068 purple_debug_misc("util", "Writing file %s to directory %s",
3069 filename, dir);
3071 /* Ensure the directory exists */
3072 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
3074 if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
3076 purple_debug_error("util", "Error creating directory %s: %s\n",
3077 dir, g_strerror(errno));
3078 return FALSE;
3082 filename_full = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dir, filename);
3084 ret = purple_util_write_data_to_file_absolute(filename_full, data, size);
3086 g_free(filename_full);
3087 return ret;
3090 gboolean
3091 purple_util_write_data_to_file(const char *filename, const char *data, gssize size)
3093 const char *user_dir = purple_user_dir();
3094 gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size);
3096 return ret;
3099 gboolean
3100 purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size)
3102 const char *cache_dir = purple_cache_dir();
3103 gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size);
3105 return ret;
3108 gboolean
3109 purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size)
3111 const char *config_dir = purple_cache_dir();
3112 gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size);
3114 return ret;
3117 gboolean
3118 purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size)
3120 const char *data_dir = purple_cache_dir();
3121 gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size);
3123 return ret;
3127 * This function is long and beautiful, like my--um, yeah. Anyway,
3128 * it includes lots of error checking so as we don't overwrite
3129 * people's settings if there is a problem writing the new values.
3131 gboolean
3132 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size)
3134 gchar *filename_temp;
3135 FILE *file;
3136 gsize real_size, byteswritten;
3137 GStatBuf st;
3138 #ifndef HAVE_FILENO
3139 int fd;
3140 #endif
3142 purple_debug_misc("util", "Writing file %s",
3143 filename_full);
3145 g_return_val_if_fail((size >= -1), FALSE);
3147 filename_temp = g_strdup_printf("%s.save", filename_full);
3149 /* Remove an old temporary file, if one exists */
3150 if (g_file_test(filename_temp, G_FILE_TEST_EXISTS))
3152 if (g_unlink(filename_temp) == -1)
3154 purple_debug_error("util", "Error removing old file "
3155 "%s: %s\n",
3156 filename_temp, g_strerror(errno));
3160 /* Open file */
3161 file = g_fopen(filename_temp, "wb");
3162 if (file == NULL)
3164 purple_debug_error("util", "Error opening file %s for "
3165 "writing: %s\n",
3166 filename_temp, g_strerror(errno));
3167 g_free(filename_temp);
3168 return FALSE;
3171 /* Write to file */
3172 real_size = (size == -1) ? strlen(data) : (size_t) size;
3173 byteswritten = fwrite(data, 1, real_size, file);
3175 #ifdef HAVE_FILENO
3176 #ifndef _WIN32
3177 /* Set file permissions */
3178 if (fchmod(fileno(file), S_IRUSR | S_IWUSR) == -1) {
3179 purple_debug_error("util", "Error setting permissions of "
3180 "file %s: %s\n", filename_temp, g_strerror(errno));
3182 #endif
3184 /* Apparently XFS (and possibly other filesystems) do not
3185 * guarantee that file data is flushed before file metadata,
3186 * so this procedure is insufficient without some flushage. */
3187 if (fflush(file) < 0) {
3188 purple_debug_error("util", "Error flushing %s: %s\n",
3189 filename_temp, g_strerror(errno));
3190 g_free(filename_temp);
3191 fclose(file);
3192 return FALSE;
3194 if (fsync(fileno(file)) < 0) {
3195 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
3196 filename_temp, g_strerror(errno));
3197 g_free(filename_temp);
3198 fclose(file);
3199 return FALSE;
3201 #endif
3203 /* Close file */
3204 if (fclose(file) != 0)
3206 purple_debug_error("util", "Error closing file %s: %s\n",
3207 filename_temp, g_strerror(errno));
3208 g_free(filename_temp);
3209 return FALSE;
3212 #ifndef HAVE_FILENO
3213 /* This is the same effect (we hope) as the HAVE_FILENO block
3214 * above, but for systems without fileno(). */
3215 if ((fd = open(filename_temp, O_RDWR)) < 0) {
3216 purple_debug_error("util", "Error opening file %s for flush: %s\n",
3217 filename_temp, g_strerror(errno));
3218 g_free(filename_temp);
3219 return FALSE;
3222 #ifndef _WIN32
3223 /* copy-pasta! */
3224 if (fchmod(fd, S_IRUSR | S_IWUSR) == -1) {
3225 purple_debug_error("util", "Error setting permissions of "
3226 "file %s: %s\n", filename_temp, g_strerror(errno));
3228 #endif
3230 if (fsync(fd) < 0) {
3231 purple_debug_error("util", "Error syncing %s: %s\n",
3232 filename_temp, g_strerror(errno));
3233 g_free(filename_temp);
3234 close(fd);
3235 return FALSE;
3237 if (close(fd) < 0) {
3238 purple_debug_error("util", "Error closing %s after sync: %s\n",
3239 filename_temp, g_strerror(errno));
3240 g_free(filename_temp);
3241 return FALSE;
3243 #endif
3245 /* Ensure the file is the correct size */
3246 if (byteswritten != real_size)
3248 purple_debug_error("util", "Error writing to file %s: Wrote %"
3249 G_GSIZE_FORMAT " bytes "
3250 "but should have written %" G_GSIZE_FORMAT
3251 "; is your disk full?\n",
3252 filename_temp, byteswritten, real_size);
3253 g_free(filename_temp);
3254 return FALSE;
3256 #ifndef __COVERITY__
3257 /* Use stat to be absolutely sure.
3258 * It causes TOCTOU coverity warning (against g_rename below),
3259 * but it's not a threat for us.
3261 if ((g_stat(filename_temp, &st) == -1) || ((gsize)st.st_size != real_size)) {
3262 purple_debug_error("util", "Error writing data to file %s: "
3263 "couldn't g_stat file", filename_temp);
3264 g_free(filename_temp);
3265 return FALSE;
3267 #endif /* __COVERITY__ */
3269 /* Rename to the REAL name */
3270 if (g_rename(filename_temp, filename_full) == -1)
3272 purple_debug_error("util", "Error renaming %s to %s: %s\n",
3273 filename_temp, filename_full,
3274 g_strerror(errno));
3277 g_free(filename_temp);
3279 return TRUE;
3282 PurpleXmlNode *
3283 purple_util_read_xml_from_file(const char *filename, const char *description)
3285 return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util");
3288 PurpleXmlNode *
3289 purple_util_read_xml_from_cache_file(const char *filename, const char *description)
3291 return purple_xmlnode_from_file(purple_cache_dir(), filename, description, "util");
3294 PurpleXmlNode *
3295 purple_util_read_xml_from_config_file(const char *filename, const char *description)
3297 return purple_xmlnode_from_file(purple_config_dir(), filename, description, "util");
3300 PurpleXmlNode *
3301 purple_util_read_xml_from_data_file(const char *filename, const char *description)
3303 return purple_xmlnode_from_file(purple_data_dir(), filename, description, "util");
3307 * Like mkstemp() but returns a file pointer, uses a pre-set template,
3308 * uses the semantics of tempnam() for the directory to use and allocates
3309 * the space for the filepath.
3311 * Caller is responsible for closing the file and removing it when done,
3312 * as well as freeing the space pointed-to by "path" with g_free().
3314 * Returns NULL on failure and cleans up after itself if so.
3316 static const char *purple_mkstemp_templ = {"purpleXXXXXX"};
3318 FILE *
3319 purple_mkstemp(char **fpath, gboolean binary)
3321 const gchar *tmpdir;
3322 int fd;
3323 FILE *fp = NULL;
3325 g_return_val_if_fail(fpath != NULL, NULL);
3327 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
3328 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) {
3329 fd = g_mkstemp(*fpath);
3330 if(fd == -1) {
3331 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3332 "Couldn't make \"%s\", error: %d\n",
3333 *fpath, errno);
3334 } else {
3335 if((fp = fdopen(fd, "r+")) == NULL) {
3336 close(fd);
3337 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3338 "Couldn't fdopen(), error: %d\n", errno);
3342 if(!fp) {
3343 g_free(*fpath);
3344 *fpath = NULL;
3347 } else {
3348 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
3349 "g_get_tmp_dir() failed!\n");
3352 return fp;
3355 gboolean
3356 purple_program_is_valid(const char *program)
3358 GError *error = NULL;
3359 char **argv;
3360 gchar *progname;
3361 gboolean is_valid = FALSE;
3363 g_return_val_if_fail(program != NULL, FALSE);
3364 g_return_val_if_fail(*program != '\0', FALSE);
3366 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
3367 purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid",
3368 "Could not parse program '%s': %s\n",
3369 program, error->message);
3370 g_error_free(error);
3371 return FALSE;
3374 if (argv == NULL) {
3375 return FALSE;
3378 progname = g_find_program_in_path(argv[0]);
3379 is_valid = (progname != NULL);
3381 if(purple_debug_is_verbose())
3382 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program,
3383 is_valid ? "Valid" : "Invalid");
3385 g_strfreev(argv);
3386 g_free(progname);
3388 return is_valid;
3392 gboolean
3393 purple_running_gnome(void)
3395 #ifndef _WIN32
3396 gchar *tmp = g_find_program_in_path("gvfs-open");
3398 if (tmp == NULL) {
3399 tmp = g_find_program_in_path("gnome-open");
3401 if (tmp == NULL) {
3402 return FALSE;
3406 g_free(tmp);
3408 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
3410 return ((tmp != NULL) && (*tmp != '\0'));
3411 #else
3412 return FALSE;
3413 #endif
3416 gboolean
3417 purple_running_kde(void)
3419 #ifndef _WIN32
3420 gchar *tmp = g_find_program_in_path("kfmclient");
3421 const char *session;
3423 if (tmp == NULL)
3424 return FALSE;
3425 g_free(tmp);
3427 session = g_getenv("KDE_FULL_SESSION");
3428 if (purple_strequal(session, "true"))
3429 return TRUE;
3431 /* If you run Purple from Konsole under !KDE, this will provide a
3432 * a false positive. Since we do the GNOME checks first, this is
3433 * only a problem if you're running something !(KDE || GNOME) and
3434 * you run Purple from Konsole. This really shouldn't be a problem. */
3435 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
3436 #else
3437 return FALSE;
3438 #endif
3441 gboolean
3442 purple_running_osx(void)
3444 #if defined(__APPLE__)
3445 return TRUE;
3446 #else
3447 return FALSE;
3448 #endif
3451 typedef union purple_sockaddr {
3452 struct sockaddr sa;
3453 struct sockaddr_in sa_in;
3454 #if defined(AF_INET6)
3455 struct sockaddr_in6 sa_in6;
3456 #endif
3457 struct sockaddr_storage sa_stor;
3458 } PurpleSockaddr;
3460 char *
3461 purple_fd_get_ip(int fd)
3463 PurpleSockaddr addr;
3464 socklen_t namelen = sizeof(addr);
3465 int family;
3467 g_return_val_if_fail(fd != 0, NULL);
3469 if (getsockname(fd, &(addr.sa), &namelen))
3470 return NULL;
3472 family = addr.sa.sa_family;
3474 if (family == AF_INET) {
3475 return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
3477 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
3478 else if (family == AF_INET6) {
3479 char host[INET6_ADDRSTRLEN];
3480 const char *tmp;
3482 tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
3483 return g_strdup(tmp);
3485 #endif
3487 return NULL;
3491 purple_socket_get_family(int fd)
3493 PurpleSockaddr addr;
3494 socklen_t len = sizeof(addr);
3496 g_return_val_if_fail(fd >= 0, -1);
3498 if (getsockname(fd, &(addr.sa), &len))
3499 return -1;
3501 return addr.sa.sa_family;
3504 gboolean
3505 purple_socket_speaks_ipv4(int fd)
3507 int family;
3509 g_return_val_if_fail(fd >= 0, FALSE);
3511 family = purple_socket_get_family(fd);
3513 switch (family) {
3514 case AF_INET:
3515 return TRUE;
3516 #if defined(IPV6_V6ONLY)
3517 case AF_INET6:
3519 int val = 0;
3520 socklen_t len = sizeof(val);
3522 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0)
3523 return FALSE;
3524 return !val;
3526 #endif
3527 default:
3528 return FALSE;
3532 /**************************************************************************
3533 * String Functions
3534 **************************************************************************/
3535 gboolean
3536 purple_strequal(const gchar *left, const gchar *right)
3538 return (g_strcmp0(left, right) == 0);
3541 const char *
3542 purple_normalize(const PurpleAccount *account, const char *str)
3544 const char *ret = NULL;
3545 static char buf[BUF_LEN];
3547 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3548 g_return_val_if_fail(str != NULL, "");
3550 if (account != NULL)
3552 PurpleProtocol *protocol =
3553 purple_protocols_find(purple_account_get_protocol_id(account));
3555 if (protocol != NULL)
3556 ret = purple_protocol_client_iface_normalize(protocol, account, str);
3559 if (ret == NULL)
3561 char *tmp;
3563 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
3564 g_snprintf(buf, sizeof(buf), "%s", tmp);
3565 g_free(tmp);
3567 ret = buf;
3570 return ret;
3574 * You probably don't want to call this directly, it is
3575 * mainly for use as a protocol callback function. See the
3576 * comments in util.h.
3578 const char *
3579 purple_normalize_nocase(const PurpleAccount *account, const char *str)
3581 static char buf[BUF_LEN];
3582 char *tmp1, *tmp2;
3584 g_return_val_if_fail(str != NULL, NULL);
3586 tmp1 = g_utf8_strdown(str, -1);
3587 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
3588 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : "");
3589 g_free(tmp2);
3590 g_free(tmp1);
3592 return buf;
3595 gboolean
3596 purple_validate(const PurpleProtocol *protocol, const char *str)
3598 const char *normalized;
3600 g_return_val_if_fail(protocol != NULL, FALSE);
3601 g_return_val_if_fail(str != NULL, FALSE);
3603 if (str[0] == '\0')
3604 return FALSE;
3606 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT_IFACE, normalize))
3607 return TRUE;
3609 normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol),
3610 NULL, str);
3612 return (NULL != normalized);
3615 gchar *
3616 purple_strdup_withhtml(const gchar *src)
3618 gulong destsize, i, j;
3619 gchar *dest;
3621 g_return_val_if_fail(src != NULL, NULL);
3623 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3624 destsize = 1;
3625 for (i = 0; src[i] != '\0'; i++)
3627 if (src[i] == '\n')
3628 destsize += 4;
3629 else if (src[i] != '\r')
3630 destsize++;
3633 dest = g_malloc(destsize);
3635 /* Copy stuff, ignoring \r's, because they are dumb */
3636 for (i = 0, j = 0; src[i] != '\0'; i++) {
3637 if (src[i] == '\n') {
3638 strcpy(&dest[j], "<BR>");
3639 j += 4;
3640 } else if (src[i] != '\r')
3641 dest[j++] = src[i];
3644 dest[destsize-1] = '\0';
3646 return dest;
3649 gboolean
3650 purple_str_has_prefix(const char *s, const char *p)
3652 return g_str_has_prefix(s, p);
3655 gboolean
3656 purple_str_has_caseprefix(const gchar *s, const gchar *p)
3658 g_return_val_if_fail(s, FALSE);
3659 g_return_val_if_fail(p, FALSE);
3661 return (g_ascii_strncasecmp(s, p, strlen(p)) == 0);
3664 gboolean
3665 purple_str_has_suffix(const char *s, const char *x)
3667 return g_str_has_suffix(s, x);
3670 char *
3671 purple_str_add_cr(const char *text)
3673 char *ret = NULL;
3674 int count = 0, j;
3675 guint i;
3677 g_return_val_if_fail(text != NULL, NULL);
3679 if (text[0] == '\n')
3680 count++;
3681 for (i = 1; i < strlen(text); i++)
3682 if (text[i] == '\n' && text[i - 1] != '\r')
3683 count++;
3685 if (count == 0)
3686 return g_strdup(text);
3688 ret = g_malloc0(strlen(text) + count + 1);
3690 i = 0; j = 0;
3691 if (text[i] == '\n')
3692 ret[j++] = '\r';
3693 ret[j++] = text[i++];
3694 for (; i < strlen(text); i++) {
3695 if (text[i] == '\n' && text[i - 1] != '\r')
3696 ret[j++] = '\r';
3697 ret[j++] = text[i];
3700 return ret;
3703 void
3704 purple_str_strip_char(char *text, char thechar)
3706 int i, j;
3708 g_return_if_fail(text != NULL);
3710 for (i = 0, j = 0; text[i]; i++)
3711 if (text[i] != thechar)
3712 text[j++] = text[i];
3714 text[j] = '\0';
3717 void
3718 purple_util_chrreplace(char *string, char delimiter,
3719 char replacement)
3721 int i = 0;
3723 g_return_if_fail(string != NULL);
3725 while (string[i] != '\0')
3727 if (string[i] == delimiter)
3728 string[i] = replacement;
3729 i++;
3733 gchar *
3734 purple_strreplace(const char *string, const char *delimiter,
3735 const char *replacement)
3737 gchar **split;
3738 gchar *ret;
3740 g_return_val_if_fail(string != NULL, NULL);
3741 g_return_val_if_fail(delimiter != NULL, NULL);
3742 g_return_val_if_fail(replacement != NULL, NULL);
3744 split = g_strsplit(string, delimiter, 0);
3745 ret = g_strjoinv(replacement, split);
3746 g_strfreev(split);
3748 return ret;
3751 gchar *
3752 purple_strcasereplace(const char *string, const char *delimiter,
3753 const char *replacement)
3755 gchar *ret;
3756 int length_del, length_rep, i, j;
3758 g_return_val_if_fail(string != NULL, NULL);
3759 g_return_val_if_fail(delimiter != NULL, NULL);
3760 g_return_val_if_fail(replacement != NULL, NULL);
3762 length_del = strlen(delimiter);
3763 length_rep = strlen(replacement);
3765 /* Count how many times the delimiter appears */
3766 i = 0; /* position in the source string */
3767 j = 0; /* number of occurrences of "delimiter" */
3768 while (string[i] != '\0') {
3769 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3770 i += length_del;
3771 j += length_rep;
3772 } else {
3773 i++;
3774 j++;
3778 ret = g_malloc(j+1);
3780 i = 0; /* position in the source string */
3781 j = 0; /* position in the destination string */
3782 while (string[i] != '\0') {
3783 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3784 strncpy(&ret[j], replacement, length_rep);
3785 i += length_del;
3786 j += length_rep;
3787 } else {
3788 ret[j] = string[i];
3789 i++;
3790 j++;
3794 ret[j] = '\0';
3796 return ret;
3799 /** TODO: Expose this when we can add API */
3800 static const char *
3801 purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen)
3803 const char *tmp, *ret;
3805 g_return_val_if_fail(haystack != NULL, NULL);
3806 g_return_val_if_fail(needle != NULL, NULL);
3808 if (hlen == -1)
3809 hlen = strlen(haystack);
3810 if (nlen == -1)
3811 nlen = strlen(needle);
3812 tmp = haystack,
3813 ret = NULL;
3815 g_return_val_if_fail(hlen > 0, NULL);
3816 g_return_val_if_fail(nlen > 0, NULL);
3818 while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) {
3819 if (!g_ascii_strncasecmp(needle, tmp, nlen))
3820 ret = tmp;
3821 else
3822 tmp++;
3825 return ret;
3828 const char *
3829 purple_strcasestr(const char *haystack, const char *needle)
3831 return purple_strcasestr_len(haystack, -1, needle, -1);
3834 char *
3835 purple_str_size_to_units(goffset size)
3837 static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3838 float size_mag;
3839 gsize size_index = 0;
3841 if (size == -1) {
3842 return g_strdup(_("Calculating..."));
3844 else if (size == 0) {
3845 return g_strdup(_("Unknown."));
3847 else {
3848 size_mag = (float)size;
3850 while ((size_index < G_N_ELEMENTS(size_str) - 1) && (size_mag > 1024)) {
3851 size_mag /= 1024;
3852 size_index++;
3855 if (size_index == 0) {
3856 return g_strdup_printf("%" G_GOFFSET_FORMAT " %s", size, _(size_str[size_index]));
3857 } else {
3858 return g_strdup_printf("%.2f %s", size_mag, _(size_str[size_index]));
3863 char *
3864 purple_str_seconds_to_string(guint secs)
3866 char *ret = NULL;
3867 guint days, hrs, mins;
3869 if (secs < 60)
3871 return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs);
3874 days = secs / (60 * 60 * 24);
3875 secs = secs % (60 * 60 * 24);
3876 hrs = secs / (60 * 60);
3877 secs = secs % (60 * 60);
3878 mins = secs / 60;
3879 /* secs = secs % 60; */
3881 if (days > 0)
3883 ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days);
3886 if (hrs > 0)
3888 if (ret != NULL)
3890 char *tmp = g_strdup_printf(
3891 dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs),
3892 ret, hrs);
3893 g_free(ret);
3894 ret = tmp;
3896 else
3897 ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs);
3900 if (mins > 0)
3902 if (ret != NULL)
3904 char *tmp = g_strdup_printf(
3905 dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins),
3906 ret, mins);
3907 g_free(ret);
3908 ret = tmp;
3910 else
3911 ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins);
3914 return ret;
3918 char *
3919 purple_str_binary_to_ascii(const unsigned char *binary, guint len)
3921 GString *ret;
3922 guint i;
3924 g_return_val_if_fail(len > 0, NULL);
3926 ret = g_string_sized_new(len);
3928 for (i = 0; i < len; i++)
3929 if (binary[i] < 32 || binary[i] > 126)
3930 g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF);
3931 else if (binary[i] == '\\')
3932 g_string_append(ret, "\\\\");
3933 else
3934 g_string_append_c(ret, binary[i]);
3936 return g_string_free(ret, FALSE);
3939 size_t
3940 purple_utf16_size(const gunichar2 *str)
3942 /* UTF16 cannot contain two consequent NUL bytes starting at even
3943 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3944 * Chapter 2.
3947 size_t i = 0;
3949 g_return_val_if_fail(str != NULL, 0);
3951 while (str[i++]);
3953 return i * sizeof(gunichar2);
3956 void
3957 purple_str_wipe(gchar *str)
3959 if (str == NULL)
3960 return;
3961 memset(str, 0, strlen(str));
3962 g_free(str);
3965 void
3966 purple_utf16_wipe(gunichar2 *str)
3968 if (str == NULL)
3969 return;
3970 memset(str, 0, purple_utf16_size(str));
3971 g_free(str);
3974 /**************************************************************************
3975 * URI/URL Functions
3976 **************************************************************************/
3978 void purple_got_protocol_handler_uri(const char *uri)
3980 char proto[11];
3981 char delimiter;
3982 const char *tmp, *param_string;
3983 char *cmd;
3984 GHashTable *params = NULL;
3985 gsize len;
3986 if (!(tmp = strchr(uri, ':')) || tmp == uri) {
3987 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3988 return;
3991 len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri));
3993 strncpy(proto, uri, len);
3994 proto[len] = '\0';
3996 tmp++;
3998 if (g_str_equal(proto, "xmpp"))
3999 delimiter = ';';
4000 else
4001 delimiter = '&';
4003 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter);
4005 if ((param_string = strchr(tmp, '?'))) {
4006 const char *keyend = NULL, *pairstart;
4007 char *key, *value = NULL;
4009 cmd = g_strndup(tmp, (param_string - tmp));
4010 param_string++;
4012 params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
4013 pairstart = tmp = param_string;
4015 while (*tmp || *pairstart) {
4016 if (*tmp == delimiter || !(*tmp)) {
4017 /* If there is no explicit value */
4018 if (keyend == NULL) {
4019 keyend = tmp;
4021 /* without these brackets, clang won't
4022 * recognize tmp as a non-NULL
4025 if (keyend && keyend != pairstart) {
4026 char *p;
4027 key = g_strndup(pairstart, (keyend - pairstart));
4028 /* If there is an explicit value */
4029 if (keyend != tmp && keyend != (tmp - 1))
4030 value = g_strndup(keyend + 1, (tmp - keyend - 1));
4031 for (p = key; *p; ++p)
4032 *p = g_ascii_tolower(*p);
4033 g_hash_table_insert(params, key, value);
4035 keyend = value = NULL;
4036 pairstart = (*tmp) ? tmp + 1 : tmp;
4037 } else if (*tmp == '=')
4038 keyend = tmp;
4040 if (*tmp)
4041 tmp++;
4043 } else
4044 cmd = g_strdup(tmp);
4046 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params);
4048 g_free(cmd);
4049 if (params)
4050 g_hash_table_destroy(params);
4053 const char *
4054 purple_url_decode(const char *str)
4056 static char buf[BUF_LEN];
4057 guint i, j = 0;
4058 char *bum;
4059 char hex[3];
4061 g_return_val_if_fail(str != NULL, NULL);
4064 * XXX - This check could be removed and buf could be made
4065 * dynamically allocated, but this is easier.
4067 if (strlen(str) >= BUF_LEN)
4068 return NULL;
4070 for (i = 0; i < strlen(str); i++) {
4072 if (str[i] != '%')
4073 buf[j++] = str[i];
4074 else {
4075 strncpy(hex, str + ++i, 2);
4076 hex[2] = '\0';
4078 /* i is pointing to the start of the number */
4079 i++;
4082 * Now it's at the end and at the start of the for loop
4083 * will be at the next character.
4085 buf[j++] = strtol(hex, NULL, 16);
4089 buf[j] = '\0';
4091 if (!g_utf8_validate(buf, -1, (const char **)&bum))
4092 *bum = '\0';
4094 return buf;
4097 const char *
4098 purple_url_encode(const char *str)
4100 const char *iter;
4101 static char buf[BUF_LEN];
4102 char utf_char[6];
4103 guint i, j = 0;
4105 g_return_val_if_fail(str != NULL, NULL);
4106 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4108 iter = str;
4109 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4110 gunichar c = g_utf8_get_char(iter);
4111 /* If the character is an ASCII character and is alphanumeric
4112 * no need to escape */
4113 if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
4114 buf[j++] = c;
4115 } else {
4116 int bytes = g_unichar_to_utf8(c, utf_char);
4117 for (i = 0; (int)i < bytes; i++) {
4118 if (j > (BUF_LEN - 4))
4119 break;
4120 if (i >= sizeof(utf_char)) {
4121 g_warn_if_reached();
4122 break;
4124 sprintf(buf + j, "%%%02X", utf_char[i] & 0xff);
4125 j += 3;
4130 buf[j] = '\0';
4132 return buf;
4135 /* Originally lifted from
4136 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
4137 * ... and slightly modified to be a bit more rfc822 compliant
4138 * ... and modified a bit more to make domain checking rfc1035 compliant
4139 * with the exception permitted in rfc1101 for domains to start with digit
4140 * but not completely checking to avoid conflicts with IP addresses
4142 gboolean
4143 purple_email_is_valid(const char *address)
4145 const char *c, *domain;
4146 static char *rfc822_specials = "()<>@,;:\\\"[]";
4148 g_return_val_if_fail(address != NULL, FALSE);
4150 if (*address == '.') return FALSE;
4152 /* first we validate the name portion (name@domain) (rfc822)*/
4153 for (c = address; *c; c++) {
4154 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
4155 while (*++c) {
4156 if (*c == '\\') {
4157 if (*c++ && *c < 127 && *c != '\n' && *c != '\r') continue;
4158 else return FALSE;
4160 if (*c == '\"') break;
4161 if (*c < ' ' || *c >= 127) return FALSE;
4163 if (!*c++) return FALSE;
4164 if (*c == '@') break;
4165 if (*c != '.') return FALSE;
4166 continue;
4168 if (*c == '@') break;
4169 if (*c <= ' ' || *c >= 127) return FALSE;
4170 if (strchr(rfc822_specials, *c)) return FALSE;
4173 /* It's obviously not an email address if we didn't find an '@' above */
4174 if (*c == '\0') return FALSE;
4176 /* strictly we should return false if (*(c - 1) == '.') too, but I think
4177 * we should permit user.@domain type addresses - they do work :) */
4178 if (c == address) return FALSE;
4180 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
4181 if (!*(domain = ++c)) return FALSE;
4182 do {
4183 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
4184 return FALSE;
4185 if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE;
4186 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
4187 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
4188 } while (*++c);
4190 if (*(c - 1) == '-') return FALSE;
4192 return ((c - domain) > 3 ? TRUE : FALSE);
4195 gboolean
4196 purple_ipv4_address_is_valid(const char *ip)
4198 int c, o1, o2, o3, o4;
4199 char end;
4201 g_return_val_if_fail(ip != NULL, FALSE);
4203 c = sscanf(ip, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, &end);
4204 if (c != 4 || o1 < 0 || o1 > 255 || o2 < 0 || o2 > 255 || o3 < 0 || o3 > 255 || o4 < 0 || o4 > 255)
4205 return FALSE;
4206 return TRUE;
4209 gboolean
4210 purple_ipv6_address_is_valid(const gchar *ip)
4212 const gchar *c;
4213 gboolean double_colon = FALSE;
4214 gint chunks = 1;
4215 gint in = 0;
4217 g_return_val_if_fail(ip != NULL, FALSE);
4219 if (*ip == '\0')
4220 return FALSE;
4222 for (c = ip; *c; ++c) {
4223 if ((*c >= '0' && *c <= '9') ||
4224 (*c >= 'a' && *c <= 'f') ||
4225 (*c >= 'A' && *c <= 'F')) {
4226 if (++in > 4)
4227 /* Only four hex digits per chunk */
4228 return FALSE;
4229 continue;
4230 } else if (*c == ':') {
4231 /* The start of a new chunk */
4232 ++chunks;
4233 in = 0;
4234 if (*(c + 1) == ':') {
4236 * '::' indicates a consecutive series of chunks full
4237 * of zeroes. There can be only one of these per address.
4239 if (double_colon)
4240 return FALSE;
4241 double_colon = TRUE;
4243 } else
4244 return FALSE;
4248 * Either we saw a '::' and there were fewer than 8 chunks -or-
4249 * we didn't see a '::' and saw exactly 8 chunks.
4251 return (double_colon && chunks < 8) || (!double_colon && chunks == 8);
4254 gboolean
4255 purple_ip_address_is_valid(const char *ip)
4257 return (purple_ipv4_address_is_valid(ip) || purple_ipv6_address_is_valid(ip));
4260 /* Stolen from gnome_uri_list_extract_uris */
4261 GList *
4262 purple_uri_list_extract_uris(const gchar *uri_list)
4264 const gchar *p, *q;
4265 gchar *retval;
4266 GList *result = NULL;
4268 g_return_val_if_fail (uri_list != NULL, NULL);
4270 p = uri_list;
4272 /* We don't actually try to validate the URI according to RFC
4273 * 2396, or even check for allowed characters - we just ignore
4274 * comments and trim whitespace off the ends. We also
4275 * allow LF delimination as well as the specified CRLF.
4277 while (p) {
4278 if (*p != '#') {
4279 while (isspace(*p))
4280 p++;
4282 q = p;
4283 while (*q && (*q != '\n') && (*q != '\r'))
4284 q++;
4286 if (q > p) {
4287 q--;
4288 while (q > p && isspace(*q))
4289 q--;
4291 retval = (gchar*)g_malloc (q - p + 2);
4292 strncpy (retval, p, q - p + 1);
4293 retval[q - p + 1] = '\0';
4295 result = g_list_prepend (result, retval);
4298 p = strchr (p, '\n');
4299 if (p)
4300 p++;
4303 return g_list_reverse (result);
4307 /* Stolen from gnome_uri_list_extract_filenames */
4308 GList *
4309 purple_uri_list_extract_filenames(const gchar *uri_list)
4311 GList *tmp_list, *node, *result;
4313 g_return_val_if_fail (uri_list != NULL, NULL);
4315 result = purple_uri_list_extract_uris(uri_list);
4317 tmp_list = result;
4318 while (tmp_list) {
4319 gchar *s = (gchar*)tmp_list->data;
4321 node = tmp_list;
4322 tmp_list = tmp_list->next;
4324 if (!strncmp (s, "file:", 5)) {
4325 node->data = g_filename_from_uri (s, NULL, NULL);
4326 /* not sure if this fallback is useful at all */
4327 if (!node->data) node->data = g_strdup (s+5);
4328 } else {
4329 result = g_list_delete_link(result, node);
4331 g_free (s);
4333 return result;
4336 /**************************************************************************
4337 * UTF8 String Functions
4338 **************************************************************************/
4339 gchar *
4340 purple_utf8_try_convert(const char *str)
4342 gsize converted;
4343 gchar *utf8;
4345 g_return_val_if_fail(str != NULL, NULL);
4347 if (g_utf8_validate(str, -1, NULL)) {
4348 return g_strdup(str);
4351 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
4352 if (utf8 != NULL)
4353 return utf8;
4355 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
4356 if ((utf8 != NULL) && (converted == strlen(str)))
4357 return utf8;
4359 g_free(utf8);
4361 return NULL;
4364 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
4365 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
4366 gchar *
4367 purple_utf8_salvage(const char *str)
4369 GString *workstr;
4370 const char *end;
4372 g_return_val_if_fail(str != NULL, NULL);
4374 workstr = g_string_sized_new(strlen(str));
4376 do {
4377 (void)g_utf8_validate(str, -1, &end);
4378 workstr = g_string_append_len(workstr, str, end - str);
4379 str = end;
4380 if (*str == '\0')
4381 break;
4382 do {
4383 workstr = g_string_append_c(workstr, '?');
4384 str++;
4385 } while (!utf8_first(*str));
4386 } while (*str != '\0');
4388 return g_string_free(workstr, FALSE);
4391 gchar *
4392 purple_utf8_strip_unprintables(const gchar *str)
4394 gchar *workstr, *iter;
4395 const gchar *bad;
4397 if (str == NULL)
4398 /* Act like g_strdup */
4399 return NULL;
4401 if (!g_utf8_validate(str, -1, &bad)) {
4402 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
4403 "first bad character was %02x (%c)\n",
4404 str, *bad, *bad);
4405 g_return_val_if_reached(NULL);
4408 workstr = iter = g_new(gchar, strlen(str) + 1);
4409 while (*str) {
4410 gunichar ch = g_utf8_get_char(str);
4411 gchar *next = g_utf8_next_char(str);
4413 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
4414 * [#x10000-#x10FFFF]
4416 if ((ch == '\t' || ch == '\n' || ch == '\r') ||
4417 (ch >= 0x20 && ch <= 0xD7FF) ||
4418 (ch >= 0xE000 && ch <= 0xFFFD) ||
4419 (ch >= 0x10000 && ch <= 0x10FFFF)) {
4420 memcpy(iter, str, next - str);
4421 iter += (next - str);
4424 str = next;
4427 /* nul-terminate the new string */
4428 *iter = '\0';
4430 return workstr;
4434 * This function is copied from g_strerror() but changed to use
4435 * gai_strerror().
4437 const gchar *
4438 purple_gai_strerror(gint errnum)
4440 static GPrivate msg_private = G_PRIVATE_INIT(g_free);
4441 char *msg;
4442 int saved_errno = errno;
4444 const char *msg_locale;
4446 msg_locale = gai_strerror(errnum);
4447 if (g_get_charset(NULL))
4449 /* This string is already UTF-8--great! */
4450 errno = saved_errno;
4451 return msg_locale;
4453 else
4455 gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
4456 if (msg_utf8)
4458 /* Stick in the quark table so that we can return a static result */
4459 GQuark msg_quark = g_quark_from_string(msg_utf8);
4460 g_free(msg_utf8);
4462 msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
4463 errno = saved_errno;
4464 return msg_utf8;
4468 msg = g_private_get(&msg_private);
4470 if (!msg)
4472 msg = g_new(gchar, 64);
4473 g_private_set(&msg_private, msg);
4476 sprintf(msg, "unknown error (%d)", errnum);
4478 errno = saved_errno;
4479 return msg;
4482 char *
4483 purple_utf8_ncr_encode(const char *str)
4485 GString *out;
4487 g_return_val_if_fail(str != NULL, NULL);
4488 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4490 out = g_string_new("");
4492 for(; *str; str = g_utf8_next_char(str)) {
4493 gunichar wc = g_utf8_get_char(str);
4495 /* super simple check. hopefully not too wrong. */
4496 if(wc >= 0x80) {
4497 g_string_append_printf(out, "&#%u;", (guint32) wc);
4498 } else {
4499 g_string_append_unichar(out, wc);
4503 return g_string_free(out, FALSE);
4507 char *
4508 purple_utf8_ncr_decode(const char *str)
4510 GString *out;
4511 char *buf, *b;
4513 g_return_val_if_fail(str != NULL, NULL);
4514 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4516 buf = (char *) str;
4517 out = g_string_new("");
4519 while( (b = strstr(buf, "&#")) ) {
4520 gunichar wc;
4521 int base = 0;
4523 /* append everything leading up to the &# */
4524 g_string_append_len(out, buf, b-buf);
4526 b += 2; /* skip past the &# */
4528 /* strtoul will treat 0x prefix as hex, but not just x */
4529 if(*b == 'x' || *b == 'X') {
4530 base = 16;
4531 b++;
4534 /* advances buf to the end of the ncr segment */
4535 wc = (gunichar) strtoul(b, &buf, base);
4537 /* this mimics the previous impl of ncr_decode */
4538 if(*buf == ';') {
4539 g_string_append_unichar(out, wc);
4540 buf++;
4544 /* append whatever's left */
4545 g_string_append(out, buf);
4547 return g_string_free(out, FALSE);
4552 purple_utf8_strcasecmp(const char *a, const char *b)
4554 char *a_norm = NULL;
4555 char *b_norm = NULL;
4556 int ret = -1;
4558 if(!a && b)
4559 return -1;
4560 else if(!b && a)
4561 return 1;
4562 else if(!a && !b)
4563 return 0;
4565 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
4567 purple_debug_error("purple_utf8_strcasecmp",
4568 "One or both parameters are invalid UTF8\n");
4569 return ret;
4572 a_norm = g_utf8_casefold(a, -1);
4573 b_norm = g_utf8_casefold(b, -1);
4574 ret = g_utf8_collate(a_norm, b_norm);
4575 g_free(a_norm);
4576 g_free(b_norm);
4578 return ret;
4581 /* previously conversation::find_nick() */
4582 gboolean
4583 purple_utf8_has_word(const char *haystack, const char *needle)
4585 char *hay, *pin, *p;
4586 const char *start, *prev_char;
4587 gunichar before, after;
4588 int n;
4589 gboolean ret = FALSE;
4591 start = hay = g_utf8_strdown(haystack, -1);
4593 pin = g_utf8_strdown(needle, -1);
4594 n = strlen(pin);
4596 while ((p = strstr(start, pin)) != NULL) {
4597 prev_char = g_utf8_find_prev_char(hay, p);
4598 before = -2;
4599 if (prev_char) {
4600 before = g_utf8_get_char(prev_char);
4602 after = g_utf8_get_char_validated(p + n, - 1);
4604 if ((p == hay ||
4605 /* The character before is a reasonable guess for a word boundary
4606 ("!g_unichar_isalnum()" is not a valid way to determine word
4607 boundaries, but it is the only reasonable thing to do here),
4608 and isn't the '&' from a "&amp;" or some such entity*/
4609 (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&'))
4610 && after != (gunichar)-2 && !g_unichar_isalnum(after)) {
4611 ret = TRUE;
4612 break;
4614 start = p + 1;
4617 g_free(pin);
4618 g_free(hay);
4620 return ret;
4623 void
4624 purple_print_utf8_to_console(FILE *filestream, char *message)
4626 gchar *message_conv;
4627 GError *error = NULL;
4629 /* Try to convert 'message' to user's locale */
4630 message_conv = g_locale_from_utf8(message, -1, NULL, NULL, &error);
4631 if (message_conv != NULL) {
4632 fputs(message_conv, filestream);
4633 g_free(message_conv);
4635 else
4637 /* use 'message' as a fallback */
4638 g_warning("%s\n", error->message);
4639 g_error_free(error);
4640 fputs(message, filestream);
4644 gboolean purple_message_meify(char *message, gssize len)
4646 char *c;
4647 gboolean inside_html = FALSE;
4649 g_return_val_if_fail(message != NULL, FALSE);
4651 if(len == -1)
4652 len = strlen(message);
4654 for (c = message; *c; c++, len--) {
4655 if(inside_html) {
4656 if(*c == '>')
4657 inside_html = FALSE;
4658 } else {
4659 if(*c == '<')
4660 inside_html = TRUE;
4661 else
4662 break;
4666 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
4667 memmove(c, c+4, len-3);
4668 return TRUE;
4671 return FALSE;
4674 char *purple_text_strip_mnemonic(const char *in)
4676 char *out;
4677 char *a;
4678 char *a0;
4679 const char *b;
4681 g_return_val_if_fail(in != NULL, NULL);
4683 out = g_malloc(strlen(in)+1);
4684 a = out;
4685 b = in;
4687 a0 = a; /* The last non-space char seen so far, or the first char */
4689 while(*b) {
4690 if(*b == '_') {
4691 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
4692 /* Detected CJK style shortcut (Bug 875311) */
4693 a = a0; /* undo the left parenthesis */
4694 b += 3; /* and skip the whole mess */
4695 } else if(*(b+1) == '_') {
4696 *(a++) = '_';
4697 b += 2;
4698 a0 = a;
4699 } else {
4700 b++;
4702 /* We don't want to corrupt the middle of UTF-8 characters */
4703 } else if (!(*b & 0x80)) { /* other 1-byte char */
4704 if (*b != ' ')
4705 a0 = a;
4706 *(a++) = *(b++);
4707 } else {
4708 /* Multibyte utf8 char, don't look for _ inside these */
4709 int n = 0;
4710 int i;
4711 if ((*b & 0xe0) == 0xc0) {
4712 n = 2;
4713 } else if ((*b & 0xf0) == 0xe0) {
4714 n = 3;
4715 } else if ((*b & 0xf8) == 0xf0) {
4716 n = 4;
4717 } else if ((*b & 0xfc) == 0xf8) {
4718 n = 5;
4719 } else if ((*b & 0xfe) == 0xfc) {
4720 n = 6;
4721 } else { /* Illegal utf8 */
4722 n = 1;
4724 a0 = a; /* unless we want to delete CJK spaces too */
4725 for (i = 0; i < n && *b; i += 1) {
4726 *(a++) = *(b++);
4730 *a = '\0';
4732 return out;
4735 const char* purple_unescape_filename(const char *escaped) {
4736 return purple_url_decode(escaped);
4740 /* this is almost identical to purple_url_encode (hence purple_url_decode
4741 * being used above), but we want to keep certain characters unescaped
4742 * for compat reasons */
4743 const char *
4744 purple_escape_filename(const char *str)
4746 const char *iter;
4747 static char buf[BUF_LEN];
4748 char utf_char[6];
4749 guint i, j = 0;
4751 g_return_val_if_fail(str != NULL, NULL);
4752 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4754 iter = str;
4755 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4756 gunichar c = g_utf8_get_char(iter);
4757 /* If the character is an ASCII character and is alphanumeric,
4758 * or one of the specified values, no need to escape */
4759 if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' ||
4760 c == '_' || c == '.' || c == '#')) {
4761 buf[j++] = c;
4762 } else {
4763 int bytes = g_unichar_to_utf8(c, utf_char);
4764 for (i = 0; (int)i < bytes; i++) {
4765 if (j > (BUF_LEN - 4))
4766 break;
4767 if (i >= sizeof(utf_char)) {
4768 g_warn_if_reached();
4769 break;
4771 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
4772 j += 3;
4776 #ifdef _WIN32
4777 /* File/Directory names in windows cannot end in periods/spaces.
4778 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4780 while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' '))
4781 j--;
4782 #endif
4783 buf[j] = '\0';
4785 return buf;
4788 gchar * purple_escape_js(const gchar *str)
4790 gchar *escaped;
4792 json_node_set_string(escape_js_node, str);
4793 json_generator_set_root(escape_js_gen, escape_js_node);
4794 escaped = json_generator_to_data(escape_js_gen, NULL);
4795 json_node_set_boolean(escape_js_node, FALSE);
4797 return escaped;
4800 void purple_restore_default_signal_handlers(void)
4802 #ifndef _WIN32
4803 signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */
4804 signal(SIGINT, SIG_DFL); /* 2: interrupt program */
4805 signal(SIGQUIT, SIG_DFL); /* 3: quit program */
4806 signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */
4807 signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */
4808 signal(SIGABRT, SIG_DFL); /* 6: abort program */
4810 #ifdef SIGPOLL
4811 signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */
4812 #endif /* SIGPOLL */
4814 #ifdef SIGEMT
4815 signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */
4816 #endif /* SIGEMT */
4818 signal(SIGFPE, SIG_DFL); /* 8: floating point exception */
4819 signal(SIGBUS, SIG_DFL); /* 10: bus error */
4820 signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */
4821 signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */
4822 signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */
4823 signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */
4824 signal(SIGTERM, SIG_DFL); /* 15: software termination signal */
4825 signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */
4826 signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */
4827 signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */
4828 #endif /* !_WIN32 */
4831 static void
4832 set_status_with_attrs(PurpleStatus *status, ...)
4834 va_list args;
4835 va_start(args, status);
4836 purple_status_set_active_with_attrs(status, TRUE, args);
4837 va_end(args);
4840 void purple_util_set_current_song(const char *title, const char *artist, const char *album)
4842 GList *list = purple_accounts_get_all();
4843 for (; list; list = list->next) {
4844 PurplePresence *presence;
4845 PurpleStatus *tune;
4846 PurpleAccount *account = list->data;
4847 if (!purple_account_get_enabled(account, purple_core_get_ui()))
4848 continue;
4850 presence = purple_account_get_presence(account);
4851 tune = purple_presence_get_status(presence, "tune");
4852 if (!tune)
4853 continue;
4854 if (title) {
4855 set_status_with_attrs(tune,
4856 PURPLE_TUNE_TITLE, title,
4857 PURPLE_TUNE_ARTIST, artist,
4858 PURPLE_TUNE_ALBUM, album,
4859 NULL);
4860 } else {
4861 purple_status_set_active(tune, FALSE);
4866 char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused)
4868 GString *string;
4869 char *esc;
4871 if (!title || !*title)
4872 return NULL;
4874 esc = g_markup_escape_text(title, -1);
4875 string = g_string_new("");
4876 g_string_append_printf(string, "%s", esc);
4877 g_free(esc);
4879 if (artist && *artist) {
4880 esc = g_markup_escape_text(artist, -1);
4881 g_string_append_printf(string, _(" - %s"), esc);
4882 g_free(esc);
4885 if (album && *album) {
4886 esc = g_markup_escape_text(album, -1);
4887 g_string_append_printf(string, _(" (%s)"), esc);
4888 g_free(esc);
4891 return g_string_free(string, FALSE);
4894 const gchar *
4895 purple_get_host_name(void)
4897 return g_get_host_name();
4900 gchar *
4901 purple_uuid_random(void)
4903 guint32 tmp, a, b;
4905 tmp = g_random_int();
4906 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */
4907 tmp >>= 12;
4908 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */
4910 tmp = g_random_int();
4912 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4913 g_random_int(),
4914 tmp & 0xFFFF,
4917 (tmp >> 16) & 0xFFFF, g_random_int());
4920 void purple_callback_set_zero(gpointer data)
4922 gpointer *ptr = data;
4924 g_return_if_fail(ptr != NULL);
4926 *ptr = NULL;
4929 GValue *
4930 purple_value_new(GType type)
4932 GValue *ret;
4934 g_return_val_if_fail(type != G_TYPE_NONE, NULL);
4936 ret = g_new0(GValue, 1);
4937 g_value_init(ret, type);
4939 return ret;
4942 GValue *
4943 purple_value_dup(GValue *value)
4945 GValue *ret;
4947 g_return_val_if_fail(value != NULL, NULL);
4949 ret = g_new0(GValue, 1);
4950 g_value_init(ret, G_VALUE_TYPE(value));
4951 g_value_copy(value, ret);
4953 return ret;
4956 void
4957 purple_value_free(GValue *value)
4959 g_return_if_fail(value != NULL);
4961 g_value_unset(value);
4962 g_free(value);
4965 gchar *purple_http_digest_calculate_session_key(
4966 const gchar *algorithm,
4967 const gchar *username,
4968 const gchar *realm,
4969 const gchar *password,
4970 const gchar *nonce,
4971 const gchar *client_nonce)
4973 PurpleHash *hasher;
4974 gchar hash[33]; /* We only support MD5. */
4975 gboolean digest_ok;
4977 g_return_val_if_fail(username != NULL, NULL);
4978 g_return_val_if_fail(realm != NULL, NULL);
4979 g_return_val_if_fail(password != NULL, NULL);
4980 g_return_val_if_fail(nonce != NULL, NULL);
4982 /* Check for a supported algorithm. */
4983 g_return_val_if_fail(algorithm == NULL ||
4984 *algorithm == '\0' ||
4985 g_ascii_strcasecmp(algorithm, "MD5") ||
4986 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
4988 hasher = purple_md5_hash_new();
4989 g_return_val_if_fail(hash != NULL, NULL);
4991 purple_hash_append(hasher, (guchar *)username, strlen(username));
4992 purple_hash_append(hasher, (guchar *)":", 1);
4993 purple_hash_append(hasher, (guchar *)realm, strlen(realm));
4994 purple_hash_append(hasher, (guchar *)":", 1);
4995 purple_hash_append(hasher, (guchar *)password, strlen(password));
4997 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
4999 guchar digest[16];
5001 if (client_nonce == NULL)
5003 g_object_unref(hasher);
5004 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
5005 return NULL;
5008 purple_hash_digest(hasher, digest, sizeof(digest));
5010 purple_hash_reset(hasher);
5011 purple_hash_append(hasher, digest, sizeof(digest));
5012 purple_hash_append(hasher, (guchar *)":", 1);
5013 purple_hash_append(hasher, (guchar *)nonce, strlen(nonce));
5014 purple_hash_append(hasher, (guchar *)":", 1);
5015 purple_hash_append(hasher, (guchar *)client_nonce, strlen(client_nonce));
5018 digest_ok = purple_hash_digest_to_str(hasher, hash, sizeof(hash));
5019 g_object_unref(hasher);
5021 g_return_val_if_fail(digest_ok, NULL);
5023 return g_strdup(hash);
5026 gchar *purple_http_digest_calculate_response(
5027 const gchar *algorithm,
5028 const gchar *method,
5029 const gchar *digest_uri,
5030 const gchar *qop,
5031 const gchar *entity,
5032 const gchar *nonce,
5033 const gchar *nonce_count,
5034 const gchar *client_nonce,
5035 const gchar *session_key)
5037 PurpleHash *hash;
5038 static gchar hash2[33]; /* We only support MD5. */
5039 gboolean digest_ok;
5041 g_return_val_if_fail(method != NULL, NULL);
5042 g_return_val_if_fail(digest_uri != NULL, NULL);
5043 g_return_val_if_fail(nonce != NULL, NULL);
5044 g_return_val_if_fail(session_key != NULL, NULL);
5046 /* Check for a supported algorithm. */
5047 g_return_val_if_fail(algorithm == NULL ||
5048 *algorithm == '\0' ||
5049 g_ascii_strcasecmp(algorithm, "MD5") ||
5050 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
5052 /* Check for a supported "quality of protection". */
5053 g_return_val_if_fail(qop == NULL ||
5054 *qop == '\0' ||
5055 g_ascii_strcasecmp(qop, "auth") ||
5056 g_ascii_strcasecmp(qop, "auth-int"), NULL);
5058 hash = purple_md5_hash_new();
5059 g_return_val_if_fail(hash != NULL, NULL);
5061 purple_hash_append(hash, (guchar *)method, strlen(method));
5062 purple_hash_append(hash, (guchar *)":", 1);
5063 purple_hash_append(hash, (guchar *)digest_uri, strlen(digest_uri));
5065 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
5067 PurpleHash *hash2;
5068 gchar entity_hash[33];
5070 if (entity == NULL)
5072 g_object_unref(hash);
5073 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
5074 return NULL;
5077 hash2 = purple_md5_hash_new();
5078 purple_hash_append(hash2, (guchar *)entity, strlen(entity));
5079 digest_ok = purple_hash_digest_to_str(hash2, entity_hash, sizeof(entity_hash));
5080 g_object_unref(hash2);
5082 if (!digest_ok) {
5083 g_object_unref(hash);
5084 g_return_val_if_reached(NULL);
5087 purple_hash_append(hash, (guchar *)":", 1);
5088 purple_hash_append(hash, (guchar *)entity_hash, strlen(entity_hash));
5091 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5092 purple_hash_reset(hash);
5094 if (!digest_ok) {
5095 g_object_unref(hash);
5096 g_return_val_if_reached(NULL);
5099 purple_hash_append(hash, (guchar *)session_key, strlen(session_key));
5100 purple_hash_append(hash, (guchar *)":", 1);
5101 purple_hash_append(hash, (guchar *)nonce, strlen(nonce));
5102 purple_hash_append(hash, (guchar *)":", 1);
5104 if (qop != NULL && *qop != '\0')
5106 if (nonce_count == NULL)
5108 g_object_unref(hash);
5109 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
5110 return NULL;
5113 if (client_nonce == NULL)
5115 g_object_unref(hash);
5116 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
5117 return NULL;
5120 purple_hash_append(hash, (guchar *)nonce_count, strlen(nonce_count));
5121 purple_hash_append(hash, (guchar *)":", 1);
5122 purple_hash_append(hash, (guchar *)client_nonce, strlen(client_nonce));
5123 purple_hash_append(hash, (guchar *)":", 1);
5125 purple_hash_append(hash, (guchar *)qop, strlen(qop));
5127 purple_hash_append(hash, (guchar *)":", 1);
5130 purple_hash_append(hash, (guchar *)hash2, strlen(hash2));
5131 digest_ok = purple_hash_digest_to_str(hash, hash2, sizeof(hash2));
5132 g_object_unref(hash);
5134 g_return_val_if_fail(digest_ok, NULL);
5136 return g_strdup(hash2);
5140 _purple_fstat(int fd, GStatBuf *st)
5142 int ret;
5144 g_return_val_if_fail(st != NULL, -1);
5146 #ifdef _WIN32
5147 ret = _fstat(fd, st);
5148 #else
5149 ret = fstat(fd, st);
5150 #endif
5152 return ret;
5155 #if 0
5157 /* Temporarily removed - re-add this when you need ini file support. */
5159 #define PURPLE_KEY_FILE_DEFAULT_MAX_SIZE 102400
5160 #define PURPLE_KEY_FILE_HARD_LIMIT 10485760
5162 gboolean
5163 purple_key_file_load_from_ini(GKeyFile *key_file, const gchar *file,
5164 gsize max_size)
5166 const gchar *header = "[default]\n\n";
5167 int header_len = strlen(header);
5168 int fd;
5169 GStatBuf st;
5170 gsize file_size, buff_size;
5171 gchar *buff;
5172 GError *error = NULL;
5174 g_return_val_if_fail(key_file != NULL, FALSE);
5175 g_return_val_if_fail(file != NULL, FALSE);
5176 g_return_val_if_fail(max_size < PURPLE_KEY_FILE_HARD_LIMIT, FALSE);
5178 if (max_size == 0)
5179 max_size = PURPLE_KEY_FILE_DEFAULT_MAX_SIZE;
5181 fd = g_open(file, O_RDONLY, S_IREAD);
5182 if (fd == -1) {
5183 purple_debug_error("util", "Failed to read ini file %s", file);
5184 return FALSE;
5187 if (_purple_fstat(fd, &st) != 0) {
5188 purple_debug_error("util", "Failed to fstat ini file %s", file);
5189 return FALSE;
5192 file_size = (st.st_size > max_size) ? max_size : st.st_size;
5194 buff_size = file_size + header_len;
5195 buff = g_new(gchar, buff_size);
5196 memcpy(buff, header, header_len);
5197 if (read(fd, buff + header_len, file_size) != (gssize)file_size) {
5198 purple_debug_error("util",
5199 "Failed to read whole ini file %s", file);
5200 g_close(fd, NULL);
5201 free(buff);
5202 return FALSE;
5204 g_close(fd, NULL);
5206 g_key_file_load_from_data(key_file, buff, buff_size,
5207 G_KEY_FILE_NONE, &error);
5209 free(buff);
5211 if (error) {
5212 purple_debug_error("util", "Failed parsing ini file %s: %s",
5213 file, error->message);
5214 return FALSE;
5217 return TRUE;
5219 #endif