Standardize all protocol header guard macros.
[pidgin-git.git] / libpurple / util.c
blob6a3377210fffce66eec5ee5926dfbd22de7d6550
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 "conversation.h"
22 #include "core.h"
23 #include "debug.h"
24 #include "glibcompat.h"
25 #include "notify.h"
26 #include "protocol.h"
27 #include "prefs.h"
28 #include "util.h"
30 #include <json-glib/json-glib.h>
32 static char *custom_user_dir = NULL;
33 static char *user_dir = NULL;
34 static gchar *cache_dir = NULL;
35 static gchar *config_dir = NULL;
36 static gchar *data_dir = NULL;
38 static JsonNode *escape_js_node = NULL;
39 static JsonGenerator *escape_js_gen = NULL;
41 void
42 purple_util_init(void)
44 escape_js_node = json_node_new(JSON_NODE_VALUE);
45 escape_js_gen = json_generator_new();
46 json_node_set_boolean(escape_js_node, FALSE);
49 void
50 purple_util_uninit(void)
52 /* Free these so we don't have leaks at shutdown. */
54 g_free(custom_user_dir);
55 custom_user_dir = NULL;
57 g_free(user_dir);
58 user_dir = NULL;
60 g_free(cache_dir);
61 cache_dir = NULL;
63 g_free(config_dir);
64 config_dir = NULL;
66 g_free(data_dir);
67 data_dir = NULL;
69 json_node_free(escape_js_node);
70 escape_js_node = NULL;
72 g_object_unref(escape_js_gen);
73 escape_js_gen = NULL;
76 /**************************************************************************
77 * Base16 Functions
78 **************************************************************************/
79 gchar *
80 purple_base16_encode(const guchar *data, gsize len)
82 gsize i;
83 gchar *ascii = NULL;
85 g_return_val_if_fail(data != NULL, NULL);
86 g_return_val_if_fail(len > 0, NULL);
88 ascii = g_malloc(len * 2 + 1);
90 for (i = 0; i < len; i++)
91 g_snprintf(&ascii[i * 2], 3, "%02x", data[i] & 0xFF);
93 return ascii;
96 guchar *
97 purple_base16_decode(const char *str, gsize *ret_len)
99 gsize len, i, accumulator = 0;
100 guchar *data;
102 g_return_val_if_fail(str != NULL, NULL);
104 len = strlen(str);
106 g_return_val_if_fail(*str, 0);
107 g_return_val_if_fail(len % 2 == 0, 0);
109 data = g_malloc(len / 2);
111 for (i = 0; i < len; i++)
113 if ((i % 2) == 0)
114 accumulator = 0;
115 else
116 accumulator <<= 4;
118 if (isdigit(str[i]))
119 accumulator |= str[i] - 48;
120 else
122 switch(tolower(str[i]))
124 case 'a': accumulator |= 10; break;
125 case 'b': accumulator |= 11; break;
126 case 'c': accumulator |= 12; break;
127 case 'd': accumulator |= 13; break;
128 case 'e': accumulator |= 14; break;
129 case 'f': accumulator |= 15; break;
133 if (i % 2)
134 data[(i - 1) / 2] = accumulator;
137 if (ret_len != NULL)
138 *ret_len = len / 2;
140 return data;
143 gchar *
144 purple_base16_encode_chunked(const guchar *data, gsize len)
146 gsize i;
147 gchar *ascii = NULL;
149 g_return_val_if_fail(data != NULL, NULL);
150 g_return_val_if_fail(len > 0, NULL);
152 /* For each byte of input, we need 2 bytes for the hex representation
153 * and 1 for the colon.
154 * The final colon will be replaced by a terminating NULL
156 ascii = g_malloc(len * 3 + 1);
158 for (i = 0; i < len; i++)
159 g_snprintf(&ascii[i * 3], 4, "%02x:", data[i] & 0xFF);
161 /* Replace the final colon with NULL */
162 ascii[len * 3 - 1] = 0;
164 return ascii;
167 /**************************************************************************
168 * Date/Time Functions
169 **************************************************************************/
171 const char *
172 purple_utf8_strftime(const char *format, const struct tm *tm)
174 static char buf[128];
175 GDateTime *dt;
176 char *utf8;
178 g_return_val_if_fail(format != NULL, NULL);
180 if (tm == NULL)
182 dt = g_date_time_new_now_local();
183 } else {
184 dt = g_date_time_new_local(tm->tm_year + 1900, tm->tm_mon + 1,
185 tm->tm_mday, tm->tm_hour,
186 tm->tm_min, tm->tm_sec);
189 utf8 = g_date_time_format(dt, format);
190 g_date_time_unref(dt);
192 if (utf8 == NULL) {
193 purple_debug_error("util",
194 "purple_utf8_strftime(): Formatting failed\n");
195 return "";
198 g_strlcpy(buf, utf8, sizeof(buf));
199 g_free(utf8);
200 return buf;
203 const char *
204 purple_date_format_short(const struct tm *tm)
206 return purple_utf8_strftime("%x", tm);
209 const char *
210 purple_date_format_long(const struct tm *tm)
213 * This string determines how some dates are displayed. The default
214 * string "%x %X" shows the date then the time. Translators can
215 * change this to "%X %x" if they want the time to be shown first,
216 * followed by the date.
218 return purple_utf8_strftime(_("%x %X"), tm);
221 const char *
222 purple_date_format_full(const struct tm *tm)
224 return purple_utf8_strftime("%c", tm);
227 const char *
228 purple_time_format(const struct tm *tm)
230 return purple_utf8_strftime("%X", tm);
233 time_t
234 purple_time_build(int year, int month, int day, int hour, int min, int sec)
236 struct tm tm;
238 tm.tm_year = year - 1900;
239 tm.tm_mon = month - 1;
240 tm.tm_mday = day;
241 tm.tm_hour = hour;
242 tm.tm_min = min;
243 tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60;
245 return mktime(&tm);
248 /* originally taken from GLib trunk 1-6-11 */
249 /* originally licensed as LGPL 2+ */
250 static time_t
251 mktime_utc(struct tm *tm)
253 time_t retval;
255 #ifndef HAVE_TIMEGM
256 static const gint days_before[] =
258 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
260 #endif
262 #ifndef HAVE_TIMEGM
263 if (tm->tm_mon < 0 || tm->tm_mon > 11)
264 return (time_t) -1;
266 retval = (tm->tm_year - 70) * 365;
267 retval += (tm->tm_year - 68) / 4;
268 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
270 if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
271 retval -= 1;
273 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
274 #else
275 retval = timegm (tm);
276 #endif /* !HAVE_TIMEGM */
278 return retval;
281 time_t
282 purple_str_to_time(const char *timestamp, gboolean utc,
283 struct tm *tm, long *tz_off, const char **rest)
285 struct tm t;
286 const gchar *str;
287 gint year = 0;
288 long tzoff = PURPLE_NO_TZ_OFF;
289 time_t retval;
290 gboolean mktime_with_utc = FALSE;
292 if (rest != NULL)
293 *rest = NULL;
295 g_return_val_if_fail(timestamp != NULL, 0);
297 memset(&t, 0, sizeof(struct tm));
299 str = timestamp;
301 /* Strip leading whitespace */
302 while (g_ascii_isspace(*str))
303 str++;
305 if (*str == '\0') {
306 if (rest != NULL && *str != '\0')
307 *rest = str;
309 return 0;
312 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
313 if (rest != NULL && *str != '\0')
314 *rest = str;
316 return 0;
319 /* 4 digit year */
320 if (sscanf(str, "%04d", &year) && year >= 1900) {
321 str += 4;
323 if (*str == '-' || *str == '/')
324 str++;
326 t.tm_year = year - 1900;
329 /* 2 digit month */
330 if (!sscanf(str, "%02d", &t.tm_mon)) {
331 if (rest != NULL && *str != '\0')
332 *rest = str;
334 return 0;
337 str += 2;
338 t.tm_mon -= 1;
340 if (*str == '-' || *str == '/')
341 str++;
343 /* 2 digit day */
344 if (!sscanf(str, "%02d", &t.tm_mday)) {
345 if (rest != NULL && *str != '\0')
346 *rest = str;
348 return 0;
351 str += 2;
353 /* Grab the year off the end if there's still stuff */
354 if (*str == '/' || *str == '-') {
355 /* But make sure we don't read the year twice */
356 if (year >= 1900) {
357 if (rest != NULL && *str != '\0')
358 *rest = str;
360 return 0;
363 str++;
365 if (!sscanf(str, "%04d", &t.tm_year)) {
366 if (rest != NULL && *str != '\0')
367 *rest = str;
369 return 0;
372 t.tm_year -= 1900;
373 } else if (*str == 'T' || *str == '.') {
374 str++;
376 /* Continue grabbing the hours/minutes/seconds */
377 if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
378 (str += 8)) ||
379 (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
380 (str += 6)))
382 gint sign, tzhrs, tzmins;
384 if (*str == '.') {
385 /* Cut off those pesky micro-seconds */
386 do {
387 str++;
388 } while (*str >= '0' && *str <= '9');
391 sign = (*str == '+') ? 1 : -1;
393 /* Process the timezone */
394 if (*str == '+' || *str == '-') {
395 str++;
397 if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) ||
398 (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4))))
400 mktime_with_utc = TRUE;
401 tzoff = tzhrs * 60 * 60 + tzmins * 60;
402 tzoff *= sign;
404 } else if (*str == 'Z') {
405 /* 'Z' = Zulu = UTC */
406 str++;
407 mktime_with_utc = TRUE;
408 tzoff = 0;
411 if (!mktime_with_utc)
413 /* No timezone specified. */
415 if (utc) {
416 mktime_with_utc = TRUE;
417 tzoff = 0;
418 } else {
419 /* Local Time */
420 t.tm_isdst = -1;
426 if (rest != NULL && *str != '\0') {
427 /* Strip trailing whitespace */
428 while (g_ascii_isspace(*str))
429 str++;
431 if (*str != '\0')
432 *rest = str;
435 if (mktime_with_utc)
436 retval = mktime_utc(&t);
437 else
438 retval = mktime(&t);
440 if (tm != NULL)
441 *tm = t;
443 if (tzoff != PURPLE_NO_TZ_OFF)
444 retval -= tzoff;
446 if (tz_off != NULL)
447 *tz_off = tzoff;
449 return retval;
452 GDateTime *
453 purple_str_to_date_time(const char *timestamp, gboolean utc)
455 const gchar *str;
456 gint year = 0;
457 gint month = 0;
458 gint day = 0;
459 gint hour = 0;
460 gint minute = 0;
461 gint seconds = 0;
462 gint microseconds = 0;
463 int chars = 0;
464 GTimeZone *tz = NULL;
465 GDateTime *retval;
467 g_return_val_if_fail(timestamp != NULL, NULL);
469 str = timestamp;
471 /* Strip leading whitespace */
472 while (g_ascii_isspace(*str))
473 str++;
475 if (*str == '\0') {
476 return NULL;
479 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
480 return NULL;
483 /* 4 digit year */
484 if (sscanf(str, "%04d", &year) && year > 0) {
485 str += 4;
487 if (*str == '-' || *str == '/')
488 str++;
491 /* 2 digit month */
492 if (!sscanf(str, "%02d", &month)) {
493 return NULL;
496 str += 2;
498 if (*str == '-' || *str == '/')
499 str++;
501 /* 2 digit day */
502 if (!sscanf(str, "%02d", &day)) {
503 return NULL;
506 str += 2;
508 /* Grab the year off the end if there's still stuff */
509 if (*str == '/' || *str == '-') {
510 /* But make sure we don't read the year twice */
511 if (year > 0) {
512 return NULL;
515 str++;
517 if (!sscanf(str, "%04d", &year)) {
518 return NULL;
520 } else if (*str == 'T' || *str == '.') {
521 str++;
523 /* Continue grabbing the hours/minutes/seconds */
524 if ((sscanf(str, "%02d:%02d:%02d", &hour, &minute, &seconds) == 3 &&
525 (str += 8)) ||
526 (sscanf(str, "%02d%02d%02d", &hour, &minute, &seconds) == 3 &&
527 (str += 6)))
529 if (*str == '.') {
530 str++;
531 if (sscanf(str, "%d%n", &microseconds, &chars) == 1) {
532 str += chars;
536 if (*str) {
537 const gchar *end = str;
538 if (*end == '+' || *end == '-') {
539 end++;
542 while (isdigit(*end) || *end == ':') {
543 end++;
546 if (str != end) {
547 /* Trim anything trailing a purely numeric time zone. */
548 gchar *tzstr = g_strndup(str, end - str);
549 tz = g_time_zone_new(tzstr);
550 g_free(tzstr);
551 } else {
552 /* Just try whatever is there. */
553 tz = g_time_zone_new(str);
559 if (!tz) {
560 /* No timezone specified. */
561 if (utc) {
562 tz = g_time_zone_new_utc();
563 } else {
564 tz = g_time_zone_new_local();
568 retval = g_date_time_new(tz, year, month, day, hour, minute,
569 seconds + microseconds * pow(10, -chars));
570 g_time_zone_unref(tz);
572 return retval;
575 char *
576 purple_uts35_to_str(const char *format, size_t len, struct tm *tm)
578 GString *string;
579 guint i, count;
581 if (tm == NULL) {
582 time_t now = time(NULL);
583 tm = localtime(&now);
586 string = g_string_sized_new(len);
587 i = 0;
588 while (i < len) {
589 count = 1;
590 while ((i + count) < len && format[i] == format[i+count])
591 count++;
593 switch (format[i]) {
594 /* Era Designator */
595 case 'G':
596 if (count <= 3) {
597 /* Abbreviated */
598 } else if (count == 4) {
599 /* Full */
600 } else if (count >= 5) {
601 /* Narrow */
602 count = 5;
604 break;
607 /* Year */
608 case 'y':
609 if (count == 2) {
610 /* Two-digits only */
611 g_string_append(string, purple_utf8_strftime("%y", tm));
612 } else {
613 /* Zero-padding */
614 g_string_append_printf(string, "%0*d",
615 count,
616 tm->tm_year + 1900);
618 break;
620 /* Year (in "Week of Year" based calendars) */
621 case 'Y':
622 if (count == 2) {
623 /* Two-digits only */
624 } else {
625 /* Zero-padding */
627 break;
629 /* Extended Year */
630 case 'u':
631 break;
633 /* Cyclic Year Name */
634 case 'U':
635 if (count <= 3) {
636 /* Abbreviated */
637 } else if (count == 4) {
638 /* Full */
639 } else if (count >= 5) {
640 /* Narrow */
641 count = 5;
643 break;
646 /* Quarter */
647 case 'Q':
648 if (count <= 2) {
649 /* Numerical */
650 } else if (count == 3) {
651 /* Abbreviation */
652 } else if (count >= 4) {
653 /* Full */
654 count = 4;
656 break;
658 /* Stand-alone Quarter */
659 case 'q':
660 if (count <= 2) {
661 /* Numerical */
662 } else if (count == 3) {
663 /* Abbreviation */
664 } else if (count >= 4) {
665 /* Full */
666 count = 4;
668 break;
670 /* Month */
671 case 'M':
672 if (count <= 2) {
673 /* Numerical */
674 g_string_append(string, purple_utf8_strftime("%m", tm));
675 } else if (count == 3) {
676 /* Abbreviation */
677 g_string_append(string, purple_utf8_strftime("%b", tm));
678 } else if (count == 4) {
679 /* Full */
680 g_string_append(string, purple_utf8_strftime("%B", tm));
681 } else if (count >= 5) {
682 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
683 count = 5;
685 break;
687 /* Stand-alone Month */
688 case 'L':
689 if (count <= 2) {
690 /* Numerical */
691 g_string_append(string, purple_utf8_strftime("%m", tm));
692 } else if (count == 3) {
693 /* Abbreviation */
694 g_string_append(string, purple_utf8_strftime("%b", tm));
695 } else if (count == 4) {
696 /* Full */
697 g_string_append(string, purple_utf8_strftime("%B", tm));
698 } else if (count >= 5) {
699 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
700 count = 5;
702 break;
704 /* Ignored */
705 case 'l':
706 break;
709 /* Week of Year */
710 case 'w':
711 g_string_append(string, purple_utf8_strftime("%W", tm));
712 count = MIN(count, 2);
713 break;
715 /* Week of Month */
716 case 'W':
717 count = 1;
718 break;
721 /* Day of Month */
722 case 'd':
723 g_string_append(string, purple_utf8_strftime("%d", tm));
724 count = MIN(count, 2);
725 break;
727 /* Day of Year */
728 case 'D':
729 g_string_append(string, purple_utf8_strftime("%j", tm));
730 count = MIN(count, 3);
731 break;
733 /* Day of Year in Month */
734 case 'F':
735 count = 1;
736 break;
738 /* Modified Julian Day */
739 case 'g':
740 break;
743 /* Day of Week */
744 case 'E':
745 if (count <= 3) {
746 /* Short */
747 g_string_append(string, purple_utf8_strftime("%a", tm));
748 } else if (count == 4) {
749 /* Full */
750 g_string_append(string, purple_utf8_strftime("%A", tm));
751 } else if (count >= 5) {
752 /* Narrow */
753 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
754 count = 5;
756 break;
758 /* Local Day of Week */
759 case 'e':
760 if (count <= 2) {
761 /* Numeric */
762 g_string_append(string, purple_utf8_strftime("%u", tm));
763 } else if (count == 3) {
764 /* Short */
765 g_string_append(string, purple_utf8_strftime("%a", tm));
766 } else if (count == 4) {
767 /* Full */
768 g_string_append(string, purple_utf8_strftime("%A", tm));
769 } else if (count >= 5) {
770 /* Narrow */
771 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
772 count = 5;
774 break;
776 /* Stand-alone Local Day of Week */
777 case 'c':
778 if (count <= 2) {
779 /* Numeric */
780 g_string_append(string, purple_utf8_strftime("%u", tm));
781 count = 1;
782 } else if (count == 3) {
783 /* Short */
784 g_string_append(string, purple_utf8_strftime("%a", tm));
785 } else if (count == 4) {
786 /* Full */
787 g_string_append(string, purple_utf8_strftime("%A", tm));
788 } else if (count >= 5) {
789 /* Narrow */
790 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
791 count = 5;
793 break;
796 /* AM/PM */
797 case 'a':
798 g_string_append(string, purple_utf8_strftime("%p", tm));
799 break;
802 /* Hour (1-12) */
803 case 'h':
804 if (count == 1) {
805 /* No padding */
806 g_string_append(string, purple_utf8_strftime("%I", tm));
807 } else if (count >= 2) {
808 /* Zero-padded */
809 g_string_append(string, purple_utf8_strftime("%I", tm));
810 count = 2;
812 break;
814 /* Hour (0-23) */
815 case 'H':
816 if (count == 1) {
817 /* No padding */
818 g_string_append(string, purple_utf8_strftime("%H", tm));
819 } else if (count >= 2) {
820 /* Zero-padded */
821 g_string_append(string, purple_utf8_strftime("%H", tm));
822 count = 2;
824 break;
826 /* Hour (0-11) */
827 case 'K':
828 if (count == 1) {
829 /* No padding */
830 } else if (count >= 2) {
831 /* Zero-padded */
832 count = 2;
834 break;
836 /* Hour (1-24) */
837 case 'k':
838 if (count == 1) {
839 /* No padding */
840 } else if (count >= 2) {
841 /* Zero-padded */
842 count = 2;
844 break;
846 /* Hour (hHkK by locale) */
847 case 'j':
848 break;
851 /* Minute */
852 case 'm':
853 g_string_append(string, purple_utf8_strftime("%M", tm));
854 count = MIN(count, 2);
855 break;
858 /* Second */
859 case 's':
860 g_string_append(string, purple_utf8_strftime("%S", tm));
861 count = MIN(count, 2);
862 break;
864 /* Fractional Sub-second */
865 case 'S':
866 break;
868 /* Millisecond */
869 case 'A':
870 break;
873 /* Time Zone (specific non-location format) */
874 case 'z':
875 if (count <= 3) {
876 /* Short */
877 } else if (count >= 4) {
878 /* Full */
879 count = 4;
881 break;
883 /* Time Zone */
884 case 'Z':
885 if (count <= 3) {
886 /* RFC822 */
887 g_string_append(string, purple_utf8_strftime("%z", tm));
888 } else if (count == 4) {
889 /* Localized GMT */
890 } else if (count >= 5) {
891 /* ISO8601 */
892 g_string_append(string, purple_utf8_strftime("%z", tm));
893 count = 5;
895 break;
897 /* Time Zone (generic non-location format) */
898 case 'v':
899 if (count <= 3) {
900 /* Short */
901 g_string_append(string, purple_utf8_strftime("%Z", tm));
902 count = 1;
903 } else if (count >= 4) {
904 /* Long */
905 g_string_append(string, purple_utf8_strftime("%Z", tm));
906 count = 4;
908 break;
910 /* Time Zone */
911 case 'V':
912 if (count <= 3) {
913 /* Same as z */
914 count = 1;
915 } else if (count >= 4) {
916 /* Generic Location Format) */
917 g_string_append(string, purple_utf8_strftime("%Z", tm));
918 count = 4;
920 break;
923 default:
924 g_string_append_len(string, format + i, count);
925 break;
928 i += count;
931 return g_string_free(string, FALSE);
935 /**************************************************************************/
936 /* GLib Event Loop Functions */
937 /**************************************************************************/
939 void purple_timeout_reset(GSource *source, gint64 seconds_from_now)
941 g_source_set_ready_time(source, g_get_monotonic_time() + (seconds_from_now * G_USEC_PER_SEC));
945 /**************************************************************************
946 * Markup Functions
947 **************************************************************************/
950 * This function is stolen from glib's gmarkup.c and modified to not
951 * replace ' with &apos;
953 static void append_escaped_text(GString *str,
954 const gchar *text, gssize length)
956 const gchar *p;
957 const gchar *end;
958 gunichar c;
960 p = text;
961 end = text + length;
963 while (p != end)
965 const gchar *next;
966 next = g_utf8_next_char (p);
968 switch (*p)
970 case '&':
971 g_string_append (str, "&amp;");
972 break;
974 case '<':
975 g_string_append (str, "&lt;");
976 break;
978 case '>':
979 g_string_append (str, "&gt;");
980 break;
982 case '"':
983 g_string_append (str, "&quot;");
984 break;
986 default:
987 c = g_utf8_get_char (p);
988 if ((0x1 <= c && c <= 0x8) ||
989 (0xb <= c && c <= 0xc) ||
990 (0xe <= c && c <= 0x1f) ||
991 (0x7f <= c && c <= 0x84) ||
992 (0x86 <= c && c <= 0x9f))
993 g_string_append_printf (str, "&#x%x;", c);
994 else
995 g_string_append_len (str, p, next - p);
996 break;
999 p = next;
1003 /* This function is stolen from glib's gmarkup.c */
1004 gchar *purple_markup_escape_text(const gchar *text, gssize length)
1006 GString *str;
1008 g_return_val_if_fail(text != NULL, NULL);
1010 if (length < 0)
1011 length = strlen(text);
1013 /* prealloc at least as long as original text */
1014 str = g_string_sized_new(length);
1015 append_escaped_text(str, text, length);
1017 return g_string_free(str, FALSE);
1020 const char *
1021 purple_markup_unescape_entity(const char *text, int *length)
1023 const char *pln;
1024 int len;
1026 if (!text || *text != '&')
1027 return NULL;
1029 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1031 if(IS_ENTITY("&amp;"))
1032 pln = "&";
1033 else if(IS_ENTITY("&lt;"))
1034 pln = "<";
1035 else if(IS_ENTITY("&gt;"))
1036 pln = ">";
1037 else if(IS_ENTITY("&nbsp;"))
1038 pln = " ";
1039 else if(IS_ENTITY("&copy;"))
1040 pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1041 else if(IS_ENTITY("&quot;"))
1042 pln = "\"";
1043 else if(IS_ENTITY("&reg;"))
1044 pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1045 else if(IS_ENTITY("&apos;"))
1046 pln = "\'";
1047 else if(text[1] == '#' && (g_ascii_isxdigit(text[2]) || text[2] == 'x')) {
1048 static char buf[7];
1049 const char *start = text + 2;
1050 char *end;
1051 guint64 pound;
1052 int base = 10;
1053 int buflen;
1055 if (*start == 'x') {
1056 base = 16;
1057 start++;
1060 pound = g_ascii_strtoull(start, &end, base);
1061 if (pound == 0 || pound > INT_MAX || *end != ';') {
1062 return NULL;
1065 len = (end - text) + 1;
1067 buflen = g_unichar_to_utf8((gunichar)pound, buf);
1068 buf[buflen] = '\0';
1069 pln = buf;
1071 else
1072 return NULL;
1074 if (length)
1075 *length = len;
1076 return pln;
1079 char *
1080 purple_markup_get_css_property(const gchar *style,
1081 const gchar *opt)
1083 const gchar *css_str = style;
1084 const gchar *css_value_start;
1085 const gchar *css_value_end;
1086 gchar *tmp;
1087 gchar *ret;
1089 g_return_val_if_fail(opt != NULL, NULL);
1091 if (!css_str)
1092 return NULL;
1094 /* find the CSS property */
1095 while (1)
1097 /* skip whitespace characters */
1098 while (*css_str && g_ascii_isspace(*css_str))
1099 css_str++;
1100 if (!g_ascii_isalpha(*css_str))
1101 return NULL;
1102 if (g_ascii_strncasecmp(css_str, opt, strlen(opt)))
1104 /* go to next css property positioned after the next ';' */
1105 while (*css_str && *css_str != '"' && *css_str != ';')
1106 css_str++;
1107 if(*css_str != ';')
1108 return NULL;
1109 css_str++;
1111 else
1112 break;
1115 /* find the CSS value position in the string */
1116 css_str += strlen(opt);
1117 while (*css_str && g_ascii_isspace(*css_str))
1118 css_str++;
1119 if (*css_str != ':')
1120 return NULL;
1121 css_str++;
1122 while (*css_str && g_ascii_isspace(*css_str))
1123 css_str++;
1124 if (*css_str == '\0' || *css_str == '"' || *css_str == ';')
1125 return NULL;
1127 /* mark the CSS value */
1128 css_value_start = css_str;
1129 while (*css_str && *css_str != '"' && *css_str != ';')
1130 css_str++;
1131 css_value_end = css_str - 1;
1133 /* Removes trailing whitespace */
1134 while (css_value_end > css_value_start && g_ascii_isspace(*css_value_end))
1135 css_value_end--;
1137 tmp = g_strndup(css_value_start, css_value_end - css_value_start + 1);
1138 ret = purple_unescape_html(tmp);
1139 g_free(tmp);
1141 return ret;
1144 gboolean purple_markup_is_rtl(const char *html)
1146 GData *attributes;
1147 const gchar *start, *end;
1148 gboolean res = FALSE;
1150 if (purple_markup_find_tag("span", html, &start, &end, &attributes))
1152 /* tmp is a member of attributes and is free with g_datalist_clear call */
1153 const char *tmp = g_datalist_get_data(&attributes, "dir");
1154 if (tmp && !g_ascii_strcasecmp(tmp, "RTL"))
1155 res = TRUE;
1156 if (!res)
1158 tmp = g_datalist_get_data(&attributes, "style");
1159 if (tmp)
1161 char *tmp2 = purple_markup_get_css_property(tmp, "direction");
1162 if (tmp2 && !g_ascii_strcasecmp(tmp2, "RTL"))
1163 res = TRUE;
1164 g_free(tmp2);
1168 g_datalist_clear(&attributes);
1170 return res;
1173 gboolean
1174 purple_markup_find_tag(const char *needle, const char *haystack,
1175 const char **start, const char **end, GData **attributes)
1177 GData *attribs;
1178 const char *cur = haystack;
1179 char *name = NULL;
1180 gboolean found = FALSE;
1181 gboolean in_tag = FALSE;
1182 gboolean in_attr = FALSE;
1183 const char *in_quotes = NULL;
1184 size_t needlelen;
1186 g_return_val_if_fail( needle != NULL, FALSE);
1187 g_return_val_if_fail( *needle != '\0', FALSE);
1188 g_return_val_if_fail( haystack != NULL, FALSE);
1189 g_return_val_if_fail( start != NULL, FALSE);
1190 g_return_val_if_fail( end != NULL, FALSE);
1191 g_return_val_if_fail(attributes != NULL, FALSE);
1193 needlelen = strlen(needle);
1194 g_datalist_init(&attribs);
1196 while (*cur && !found) {
1197 if (in_tag) {
1198 if (in_quotes) {
1199 const char *close = cur;
1201 while (*close && *close != *in_quotes)
1202 close++;
1204 /* if we got the close quote, store the value and carry on from *
1205 * after it. if we ran to the end of the string, point to the NULL *
1206 * and we're outta here */
1207 if (*close) {
1208 /* only store a value if we have an attribute name */
1209 if (name) {
1210 size_t len = close - cur;
1211 char *val = g_strndup(cur, len);
1213 g_datalist_set_data_full(&attribs, name, val, g_free);
1214 g_free(name);
1215 name = NULL;
1218 in_quotes = NULL;
1219 cur = close + 1;
1220 } else {
1221 cur = close;
1223 } else if (in_attr) {
1224 const char *close = cur;
1226 while (*close && *close != '>' && *close != '"' &&
1227 *close != '\'' && *close != ' ' && *close != '=')
1228 close++;
1230 /* if we got the equals, store the name of the attribute. if we got
1231 * the quote, save the attribute and go straight to quote mode.
1232 * otherwise the tag closed or we reached the end of the string,
1233 * so we can get outta here */
1234 switch (*close) {
1235 case '"':
1236 case '\'':
1237 in_quotes = close;
1238 /* fall through */
1239 case '=':
1241 size_t len = close - cur;
1243 /* don't store a blank attribute name */
1244 if (len) {
1245 g_free(name);
1246 name = g_ascii_strdown(cur, len);
1249 in_attr = FALSE;
1250 cur = close + 1;
1252 break;
1253 case ' ':
1254 case '>':
1255 in_attr = FALSE;
1256 /* fall through */
1257 default:
1258 cur = close;
1259 break;
1261 } else {
1262 switch (*cur) {
1263 case ' ':
1264 /* swallow extra spaces inside tag */
1265 while (*cur && *cur == ' ') cur++;
1266 in_attr = TRUE;
1267 break;
1268 case '>':
1269 found = TRUE;
1270 *end = cur;
1271 break;
1272 case '"':
1273 case '\'':
1274 in_quotes = cur;
1275 /* fall through */
1276 default:
1277 cur++;
1278 break;
1281 } else {
1282 /* if we hit a < followed by the name of our tag... */
1283 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) {
1284 *start = cur;
1285 cur = cur + needlelen + 1;
1287 /* if we're pointing at a space or a >, we found the right tag. if *
1288 * we're not, we've found a longer tag, so we need to skip to the *
1289 * >, but not being distracted by >s inside quotes. */
1290 if (*cur == ' ' || *cur == '>') {
1291 in_tag = TRUE;
1292 } else {
1293 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') {
1294 if (*cur == '"') {
1295 cur++;
1296 while (*cur && *cur != '"')
1297 cur++;
1298 } else if (*cur == '\'') {
1299 cur++;
1300 while (*cur && *cur != '\'')
1301 cur++;
1302 } else {
1303 cur++;
1307 } else {
1308 cur++;
1313 /* clean up any attribute name from a premature termination */
1314 g_free(name);
1316 if (found) {
1317 *attributes = attribs;
1318 } else {
1319 *start = NULL;
1320 *end = NULL;
1321 *attributes = NULL;
1324 return found;
1327 gboolean
1328 purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
1329 const char *start_token, int skip,
1330 const char *end_token, char check_value,
1331 const char *no_value_token,
1332 const char *display_name, gboolean is_link,
1333 const char *link_prefix,
1334 PurpleInfoFieldFormatCallback format_cb)
1336 const char *p, *q;
1338 g_return_val_if_fail(str != NULL, FALSE);
1339 g_return_val_if_fail(user_info != NULL, FALSE);
1340 g_return_val_if_fail(start_token != NULL, FALSE);
1341 g_return_val_if_fail(end_token != NULL, FALSE);
1342 g_return_val_if_fail(display_name != NULL, FALSE);
1344 p = strstr(str, start_token);
1346 if (p == NULL)
1347 return FALSE;
1349 p += strlen(start_token) + skip;
1351 if (p >= str + len)
1352 return FALSE;
1354 if (check_value != '\0' && *p == check_value)
1355 return FALSE;
1357 q = strstr(p, end_token);
1359 /* Trim leading blanks */
1360 while (*p != '\n' && g_ascii_isspace(*p)) {
1361 p += 1;
1364 /* Trim trailing blanks */
1365 while (q > p && g_ascii_isspace(*(q - 1))) {
1366 q -= 1;
1369 /* Don't bother with null strings */
1370 if (p == q)
1371 return FALSE;
1373 if (q != NULL && (!no_value_token ||
1374 (no_value_token && strncmp(p, no_value_token,
1375 strlen(no_value_token)))))
1377 GString *dest = g_string_new("");
1379 if (is_link)
1381 g_string_append(dest, "<a href=\"");
1383 if (link_prefix)
1384 g_string_append(dest, link_prefix);
1386 if (format_cb != NULL)
1388 char *reformatted = format_cb(p, q - p);
1389 g_string_append(dest, reformatted);
1390 g_free(reformatted);
1392 else
1393 g_string_append_len(dest, p, q - p);
1394 g_string_append(dest, "\">");
1396 if (link_prefix)
1397 g_string_append(dest, link_prefix);
1399 g_string_append_len(dest, p, q - p);
1400 g_string_append(dest, "</a>");
1402 else
1404 if (format_cb != NULL)
1406 char *reformatted = format_cb(p, q - p);
1407 g_string_append(dest, reformatted);
1408 g_free(reformatted);
1410 else
1411 g_string_append_len(dest, p, q - p);
1414 purple_notify_user_info_add_pair_html(user_info, display_name, dest->str);
1415 g_string_free(dest, TRUE);
1417 return TRUE;
1420 return FALSE;
1423 struct purple_parse_tag {
1424 char *src_tag;
1425 char *dest_tag;
1426 gboolean ignore;
1429 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1430 recommended in the GCC docs). It contains 'continue's that should
1431 affect the while-loop in purple_markup_html_to_xhtml and doing the
1432 above would break that.
1433 Also, remember to put braces in constructs that require them for
1434 multiple statements when using this macro. */
1435 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1436 const char *o = c + strlen("<" x); \
1437 const char *p = NULL, *q = NULL, *r = NULL; \
1438 /* o = iterating over full tag \
1439 * p = > (end of tag) \
1440 * q = start of quoted bit \
1441 * r = < inside tag \
1442 */ \
1443 GString *innards = g_string_new(""); \
1444 while(o && *o) { \
1445 if(!q && (*o == '\"' || *o == '\'') ) { \
1446 q = o; \
1447 } else if(q) { \
1448 if(*o == *q) { /* end of quoted bit */ \
1449 char *unescaped = g_strndup(q+1, o-q-1); \
1450 char *escaped = g_markup_escape_text(unescaped, -1); \
1451 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1452 g_free(unescaped); \
1453 g_free(escaped); \
1454 q = NULL; \
1455 } else if(*c == '\\') { \
1456 o++; \
1458 } else if(*o == '<') { \
1459 r = o; \
1460 } else if(*o == '>') { \
1461 p = o; \
1462 break; \
1463 } else { \
1464 innards = g_string_append_c(innards, *o); \
1466 o++; \
1468 if(p && !r) { /* got an end of tag and no other < earlier */\
1469 if(*(p-1) != '/') { \
1470 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1471 pt->src_tag = x; \
1472 pt->dest_tag = y; \
1473 tags = g_list_prepend(tags, pt); \
1475 if(xhtml) { \
1476 xhtml = g_string_append(xhtml, "<" y); \
1477 xhtml = g_string_append(xhtml, innards->str); \
1478 xhtml = g_string_append_c(xhtml, '>'); \
1480 c = p + 1; \
1481 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1482 if(xhtml) \
1483 xhtml = g_string_append(xhtml, "&lt;"); \
1484 if(plain) \
1485 plain = g_string_append_c(plain, '<'); \
1486 c++; \
1488 g_string_free(innards, TRUE); \
1489 continue; \
1491 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1492 (*(c+strlen("<" x)) == '>' || \
1493 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1494 if(xhtml) \
1495 xhtml = g_string_append(xhtml, "<" y); \
1496 c += strlen("<" x); \
1497 if(*c != '/') { \
1498 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1499 pt->src_tag = x; \
1500 pt->dest_tag = y; \
1501 tags = g_list_prepend(tags, pt); \
1502 if(xhtml) \
1503 xhtml = g_string_append_c(xhtml, '>'); \
1504 } else { \
1505 if(xhtml) \
1506 xhtml = g_string_append(xhtml, "/>");\
1508 c = strchr(c, '>') + 1; \
1509 continue; \
1511 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1512 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1513 void
1514 purple_markup_html_to_xhtml(const char *html, char **xhtml_out,
1515 char **plain_out)
1517 GString *xhtml = NULL;
1518 GString *plain = NULL;
1519 GString *url = NULL;
1520 GString *cdata = NULL;
1521 GList *tags = NULL, *tag;
1522 const char *c = html;
1523 char quote = '\0';
1525 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1526 quote = *(ptr++); \
1527 else \
1528 quote = '\0';
1530 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1532 g_return_if_fail(xhtml_out != NULL || plain_out != NULL);
1534 if(xhtml_out)
1535 xhtml = g_string_new("");
1536 if(plain_out)
1537 plain = g_string_new("");
1539 while(c && *c) {
1540 if(*c == '<') {
1541 if(*(c+1) == '/') { /* closing tag */
1542 tag = tags;
1543 while(tag) {
1544 struct purple_parse_tag *pt = tag->data;
1545 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') {
1546 c += strlen(pt->src_tag) + 3;
1547 break;
1549 tag = tag->next;
1551 if(tag) {
1552 while(tags) {
1553 struct purple_parse_tag *pt = tags->data;
1554 if(xhtml && !pt->ignore)
1555 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1556 if(plain && purple_strequal(pt->src_tag, "a")) {
1557 /* if this is a link, we have to add the url to the plaintext, too */
1558 if (cdata && url &&
1559 (!g_string_equal(cdata, url) && (g_ascii_strncasecmp(url->str, "mailto:", 7) != 0 ||
1560 g_utf8_collate(url->str + 7, cdata->str) != 0)))
1561 g_string_append_printf(plain, " <%s>", g_strstrip(purple_unescape_html(url->str)));
1562 if (cdata) {
1563 g_string_free(cdata, TRUE);
1564 cdata = NULL;
1568 if(tags == tag)
1569 break;
1570 tags = g_list_remove(tags, pt);
1571 g_free(pt);
1573 g_free(tag->data);
1574 tags = g_list_delete_link(tags, tag);
1575 } else {
1576 /* a closing tag we weren't expecting...
1577 * we'll let it slide, if it's really a tag...if it's
1578 * just a </ we'll escape it properly */
1579 const char *end = c+2;
1580 while(*end && g_ascii_isalpha(*end))
1581 end++;
1582 if(*end == '>') {
1583 c = end+1;
1584 } else {
1585 if(xhtml)
1586 xhtml = g_string_append(xhtml, "&lt;");
1587 if(plain)
1588 plain = g_string_append_c(plain, '<');
1589 c++;
1592 } else { /* opening tag */
1593 ALLOW_TAG("blockquote");
1594 ALLOW_TAG("cite");
1595 ALLOW_TAG("div");
1596 ALLOW_TAG("em");
1597 ALLOW_TAG("h1");
1598 ALLOW_TAG("h2");
1599 ALLOW_TAG("h3");
1600 ALLOW_TAG("h4");
1601 ALLOW_TAG("h5");
1602 ALLOW_TAG("h6");
1603 /* we only allow html to start the message */
1604 if(c == html) {
1605 ALLOW_TAG("html");
1607 ALLOW_TAG_ALT("i", "em");
1608 ALLOW_TAG_ALT("italic", "em");
1609 ALLOW_TAG("li");
1610 ALLOW_TAG("ol");
1611 ALLOW_TAG("p");
1612 ALLOW_TAG("pre");
1613 ALLOW_TAG("q");
1614 ALLOW_TAG("span");
1615 ALLOW_TAG("ul");
1618 /* we skip <HR> because it's not legal in XHTML-IM. However,
1619 * we still want to send something sensible, so we put a
1620 * linebreak in its place. <BR> also needs special handling
1621 * because putting a </BR> to close it would just be dumb. */
1622 if((!g_ascii_strncasecmp(c, "<br", 3)
1623 || !g_ascii_strncasecmp(c, "<hr", 3))
1624 && (*(c+3) == '>' ||
1625 !g_ascii_strncasecmp(c+3, "/>", 2) ||
1626 !g_ascii_strncasecmp(c+3, " />", 3))) {
1627 c = strchr(c, '>') + 1;
1628 if(xhtml)
1629 xhtml = g_string_append(xhtml, "<br/>");
1630 if(plain && *c != '\n')
1631 plain = g_string_append_c(plain, '\n');
1632 continue;
1634 if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c, "<strong>", strlen("<strong>"))) {
1635 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1636 if (*(c+2) == '>')
1637 pt->src_tag = "b";
1638 else if (*(c+2) == 'o')
1639 pt->src_tag = "bold";
1640 else
1641 pt->src_tag = "strong";
1642 pt->dest_tag = "span";
1643 tags = g_list_prepend(tags, pt);
1644 c = strchr(c, '>') + 1;
1645 if(xhtml)
1646 xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>");
1647 continue;
1649 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) {
1650 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1651 pt->src_tag = *(c+2) == '>' ? "u" : "underline";
1652 pt->dest_tag = "span";
1653 tags = g_list_prepend(tags, pt);
1654 c = strchr(c, '>') + 1;
1655 if (xhtml)
1656 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>");
1657 continue;
1659 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) {
1660 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1661 pt->src_tag = *(c+2) == '>' ? "s" : "strike";
1662 pt->dest_tag = "span";
1663 tags = g_list_prepend(tags, pt);
1664 c = strchr(c, '>') + 1;
1665 if(xhtml)
1666 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>");
1667 continue;
1669 if(!g_ascii_strncasecmp(c, "<sub>", 5)) {
1670 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1671 pt->src_tag = "sub";
1672 pt->dest_tag = "span";
1673 tags = g_list_prepend(tags, pt);
1674 c = strchr(c, '>') + 1;
1675 if(xhtml)
1676 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>");
1677 continue;
1679 if(!g_ascii_strncasecmp(c, "<sup>", 5)) {
1680 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1681 pt->src_tag = "sup";
1682 pt->dest_tag = "span";
1683 tags = g_list_prepend(tags, pt);
1684 c = strchr(c, '>') + 1;
1685 if(xhtml)
1686 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
1687 continue;
1689 if (!g_ascii_strncasecmp(c, "<img", 4) && (*(c+4) == '>' || *(c+4) == ' ')) {
1690 const char *p = c + 4;
1691 GString *src = NULL, *alt = NULL;
1692 #define ESCAPE(from, to) \
1693 CHECK_QUOTE(from); \
1694 while (VALID_CHAR(from)) { \
1695 int len; \
1696 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1697 to = g_string_append(to, "&amp;"); \
1698 else if (*from == '\'') \
1699 to = g_string_append(to, "&apos;"); \
1700 else \
1701 to = g_string_append_c(to, *from); \
1702 from++; \
1705 while (*p && *p != '>') {
1706 if (!g_ascii_strncasecmp(p, "src=", 4)) {
1707 const char *q = p + 4;
1708 if (src)
1709 g_string_free(src, TRUE);
1710 src = g_string_new("");
1711 ESCAPE(q, src);
1712 p = q;
1713 } else if (!g_ascii_strncasecmp(p, "alt=", 4)) {
1714 const char *q = p + 4;
1715 if (alt)
1716 g_string_free(alt, TRUE);
1717 alt = g_string_new("");
1718 ESCAPE(q, alt);
1719 p = q;
1720 } else {
1721 p++;
1724 #undef ESCAPE
1725 if ((c = strchr(p, '>')) != NULL)
1726 c++;
1727 else
1728 c = p;
1729 /* src and alt are required! */
1730 if(src && xhtml)
1731 g_string_append_printf(xhtml, "<img src='%s' alt='%s' />", g_strstrip(src->str), alt ? alt->str : "");
1732 if(alt) {
1733 if(plain)
1734 plain = g_string_append(plain, purple_unescape_html(alt->str));
1735 if(!src && xhtml)
1736 xhtml = g_string_append(xhtml, alt->str);
1737 g_string_free(alt, TRUE);
1739 g_string_free(src, TRUE);
1740 continue;
1742 if (!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
1743 const char *p = c + 2;
1744 struct purple_parse_tag *pt;
1745 while (*p && *p != '>') {
1746 if (!g_ascii_strncasecmp(p, "href=", 5)) {
1747 const char *q = p + 5;
1748 if (url)
1749 g_string_free(url, TRUE);
1750 url = g_string_new("");
1751 if (cdata)
1752 g_string_free(cdata, TRUE);
1753 cdata = g_string_new("");
1754 CHECK_QUOTE(q);
1755 while (VALID_CHAR(q)) {
1756 int len;
1757 if ((*q == '&') && (purple_markup_unescape_entity(q, &len) == NULL))
1758 url = g_string_append(url, "&amp;");
1759 else if (*q == '"')
1760 url = g_string_append(url, "&quot;");
1761 else
1762 url = g_string_append_c(url, *q);
1763 q++;
1765 p = q;
1766 } else {
1767 p++;
1770 if ((c = strchr(p, '>')) != NULL)
1771 c++;
1772 else
1773 c = p;
1774 pt = g_new0(struct purple_parse_tag, 1);
1775 pt->src_tag = "a";
1776 pt->dest_tag = "a";
1777 tags = g_list_prepend(tags, pt);
1778 if(xhtml)
1779 g_string_append_printf(xhtml, "<a href=\"%s\">", url ? g_strstrip(url->str) : "");
1780 continue;
1782 #define ESCAPE(from, to) \
1783 CHECK_QUOTE(from); \
1784 while (VALID_CHAR(from)) { \
1785 int len; \
1786 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1787 to = g_string_append(to, "&amp;"); \
1788 else if (*from == '\'') \
1789 to = g_string_append_c(to, '\"'); \
1790 else \
1791 to = g_string_append_c(to, *from); \
1792 from++; \
1794 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
1795 const char *p = c + 5;
1796 GString *style = g_string_new("");
1797 struct purple_parse_tag *pt;
1798 while (*p && *p != '>') {
1799 if (!g_ascii_strncasecmp(p, "back=", 5)) {
1800 const char *q = p + 5;
1801 GString *color = g_string_new("");
1802 ESCAPE(q, color);
1803 g_string_append_printf(style, "background: %s; ", color->str);
1804 g_string_free(color, TRUE);
1805 p = q;
1806 } else if (!g_ascii_strncasecmp(p, "color=", 6)) {
1807 const char *q = p + 6;
1808 GString *color = g_string_new("");
1809 ESCAPE(q, color);
1810 g_string_append_printf(style, "color: %s; ", color->str);
1811 g_string_free(color, TRUE);
1812 p = q;
1813 } else if (!g_ascii_strncasecmp(p, "face=", 5)) {
1814 const char *q = p + 5;
1815 GString *face = g_string_new("");
1816 ESCAPE(q, face);
1817 g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str));
1818 g_string_free(face, TRUE);
1819 p = q;
1820 } else if (!g_ascii_strncasecmp(p, "size=", 5)) {
1821 const char *q = p + 5;
1822 int sz;
1823 const char *size = "medium";
1824 CHECK_QUOTE(q);
1825 sz = atoi(q);
1826 switch (sz)
1828 case 1:
1829 size = "xx-small";
1830 break;
1831 case 2:
1832 size = "small";
1833 break;
1834 case 3:
1835 size = "medium";
1836 break;
1837 case 4:
1838 size = "large";
1839 break;
1840 case 5:
1841 size = "x-large";
1842 break;
1843 case 6:
1844 case 7:
1845 size = "xx-large";
1846 break;
1847 default:
1848 break;
1850 g_string_append_printf(style, "font-size: %s; ", size);
1851 p = q;
1852 } else {
1853 p++;
1856 if ((c = strchr(p, '>')) != NULL)
1857 c++;
1858 else
1859 c = p;
1860 pt = g_new0(struct purple_parse_tag, 1);
1861 pt->src_tag = "font";
1862 pt->dest_tag = "span";
1863 tags = g_list_prepend(tags, pt);
1864 if(style->len && xhtml)
1865 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
1866 else
1867 pt->ignore = TRUE;
1868 g_string_free(style, TRUE);
1869 continue;
1871 #undef ESCAPE
1872 if (!g_ascii_strncasecmp(c, "<body ", 6)) {
1873 const char *p = c + 6;
1874 gboolean did_something = FALSE;
1875 while (*p && *p != '>') {
1876 if (!g_ascii_strncasecmp(p, "bgcolor=", 8)) {
1877 const char *q = p + 8;
1878 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1879 GString *color = g_string_new("");
1880 CHECK_QUOTE(q);
1881 while (VALID_CHAR(q)) {
1882 color = g_string_append_c(color, *q);
1883 q++;
1885 if (xhtml)
1886 g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str));
1887 g_string_free(color, TRUE);
1888 if ((c = strchr(p, '>')) != NULL)
1889 c++;
1890 else
1891 c = p;
1892 pt->src_tag = "body";
1893 pt->dest_tag = "span";
1894 tags = g_list_prepend(tags, pt);
1895 did_something = TRUE;
1896 break;
1898 p++;
1900 if (did_something) continue;
1902 /* this has to come after the special case for bgcolor */
1903 ALLOW_TAG("body");
1904 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) {
1905 char *p = strstr(c + strlen("<!--"), "-->");
1906 if(p) {
1907 if(xhtml)
1908 xhtml = g_string_append(xhtml, "<!--");
1909 c += strlen("<!--");
1910 continue;
1914 if(xhtml)
1915 xhtml = g_string_append(xhtml, "&lt;");
1916 if(plain)
1917 plain = g_string_append_c(plain, '<');
1918 c++;
1920 } else if(*c == '&') {
1921 char buf[7];
1922 const char *pln;
1923 int len;
1925 if ((pln = purple_markup_unescape_entity(c, &len)) == NULL) {
1926 len = 1;
1927 g_snprintf(buf, sizeof(buf), "%c", *c);
1928 pln = buf;
1930 if(xhtml)
1931 xhtml = g_string_append_len(xhtml, c, len);
1932 if(plain)
1933 plain = g_string_append(plain, pln);
1934 if(cdata)
1935 cdata = g_string_append_len(cdata, c, len);
1936 c += len;
1937 } else {
1938 if(xhtml)
1939 xhtml = g_string_append_c(xhtml, *c);
1940 if(plain)
1941 plain = g_string_append_c(plain, *c);
1942 if(cdata)
1943 cdata = g_string_append_c(cdata, *c);
1944 c++;
1947 if(xhtml) {
1948 for (tag = tags; tag ; tag = tag->next) {
1949 struct purple_parse_tag *pt = tag->data;
1950 if(!pt->ignore)
1951 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1954 g_list_free(tags);
1955 if(xhtml_out)
1956 *xhtml_out = g_string_free(xhtml, FALSE);
1957 if(plain_out)
1958 *plain_out = g_string_free(plain, FALSE);
1959 if(url)
1960 g_string_free(url, TRUE);
1961 if (cdata)
1962 g_string_free(cdata, TRUE);
1963 #undef CHECK_QUOTE
1964 #undef VALID_CHAR
1967 /* The following are probably reasonable changes:
1968 * - \n should be converted to a normal space
1969 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
1970 * - We want to turn </td>#whitespace<td> sequences into a single tab
1971 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
1972 * - <script>...</script> and <style>...</style> should be completely removed
1975 char *
1976 purple_markup_strip_html(const char *str)
1978 int i, j, k, entlen;
1979 gboolean visible = TRUE;
1980 gboolean closing_td_p = FALSE;
1981 gchar *str2;
1982 const gchar *cdata_close_tag = NULL, *ent;
1983 gchar *href = NULL;
1984 int href_st = 0;
1986 if(!str)
1987 return NULL;
1989 str2 = g_strdup(str);
1991 for (i = 0, j = 0; str2[i]; i++)
1993 if (str2[i] == '<')
1995 if (cdata_close_tag)
1997 /* Note: Don't even assume any other tag is a tag in CDATA */
1998 if (g_ascii_strncasecmp(str2 + i, cdata_close_tag,
1999 strlen(cdata_close_tag)) == 0)
2001 i += strlen(cdata_close_tag) - 1;
2002 cdata_close_tag = NULL;
2004 continue;
2006 else if (g_ascii_strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p)
2008 str2[j++] = '\t';
2009 visible = TRUE;
2011 else if (g_ascii_strncasecmp(str2 + i, "</td>", 5) == 0)
2013 closing_td_p = TRUE;
2014 visible = FALSE;
2016 else
2018 closing_td_p = FALSE;
2019 visible = TRUE;
2022 k = i + 1;
2024 if(g_ascii_isspace(str2[k]))
2025 visible = TRUE;
2026 else if (str2[k])
2028 /* Scan until we end the tag either implicitly (closed start
2029 * tag) or explicitly, using a sloppy method (i.e., < or >
2030 * inside quoted attributes will screw us up)
2032 while (str2[k] && str2[k] != '<' && str2[k] != '>')
2034 k++;
2037 /* If we've got an <a> tag with an href, save the address
2038 * to print later. */
2039 if (g_ascii_strncasecmp(str2 + i, "<a", 2) == 0 &&
2040 g_ascii_isspace(str2[i+2]))
2042 int st; /* start of href, inclusive [ */
2043 int end; /* end of href, exclusive ) */
2044 char delim = ' ';
2045 /* Find start of href */
2046 for (st = i + 3; st < k; st++)
2048 if (g_ascii_strncasecmp(str2+st, "href=", 5) == 0)
2050 st += 5;
2051 if (str2[st] == '"' || str2[st] == '\'')
2053 delim = str2[st];
2054 st++;
2056 break;
2059 /* find end of address */
2060 for (end = st; end < k && str2[end] != delim; end++)
2062 /* All the work is done in the loop construct above. */
2065 /* If there's an address, save it. If there was
2066 * already one saved, kill it. */
2067 if (st < k)
2069 char *tmp;
2070 g_free(href);
2071 tmp = g_strndup(str2 + st, end - st);
2072 href = purple_unescape_html(tmp);
2073 g_free(tmp);
2074 href_st = j;
2078 /* Replace </a> with an ascii representation of the
2079 * address the link was pointing to. */
2080 else if (href != NULL && g_ascii_strncasecmp(str2 + i, "</a>", 4) == 0)
2082 size_t hrlen = strlen(href);
2084 /* Only insert the href if it's different from the CDATA. */
2085 if ((hrlen != (gsize)(j - href_st) ||
2086 strncmp(str2 + href_st, href, hrlen)) &&
2087 (hrlen != (gsize)(j - href_st + 7) || /* 7 == strlen("http://") */
2088 strncmp(str2 + href_st, href + 7, hrlen - 7)))
2090 str2[j++] = ' ';
2091 str2[j++] = '(';
2092 memmove(str2 + j, href, hrlen);
2093 j += hrlen;
2094 str2[j++] = ')';
2095 g_free(href);
2096 href = NULL;
2100 /* Check for tags which should be mapped to newline (but ignore some of
2101 * the tags at the beginning of the text) */
2102 else if ((j && (g_ascii_strncasecmp(str2 + i, "<p>", 3) == 0
2103 || g_ascii_strncasecmp(str2 + i, "<tr", 3) == 0
2104 || g_ascii_strncasecmp(str2 + i, "<hr", 3) == 0
2105 || g_ascii_strncasecmp(str2 + i, "<li", 3) == 0
2106 || g_ascii_strncasecmp(str2 + i, "<div", 4) == 0))
2107 || g_ascii_strncasecmp(str2 + i, "<br", 3) == 0
2108 || g_ascii_strncasecmp(str2 + i, "</table>", 8) == 0)
2110 str2[j++] = '\n';
2112 /* Check for tags which begin CDATA and need to be closed */
2113 else if (g_ascii_strncasecmp(str2 + i, "<script", 7) == 0)
2115 cdata_close_tag = "</script>";
2117 else if (g_ascii_strncasecmp(str2 + i, "<style", 6) == 0)
2119 cdata_close_tag = "</style>";
2121 /* Update the index and continue checking after the tag */
2122 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k;
2123 continue;
2126 else if (cdata_close_tag)
2128 continue;
2130 else if (!g_ascii_isspace(str2[i]))
2132 visible = TRUE;
2135 if (str2[i] == '&' && (ent = purple_markup_unescape_entity(str2 + i, &entlen)) != NULL)
2137 while (*ent)
2138 str2[j++] = *ent++;
2139 i += entlen - 1;
2140 continue;
2143 if (visible)
2144 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i];
2147 g_free(href);
2149 str2[j] = '\0';
2151 return str2;
2154 static gboolean
2155 badchar(char c)
2157 switch (c) {
2158 case ' ':
2159 case ',':
2160 case '\0':
2161 case '\n':
2162 case '\r':
2163 case '<':
2164 case '>':
2165 case '"':
2166 return TRUE;
2167 default:
2168 return FALSE;
2172 static gboolean
2173 badentity(const char *c)
2175 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
2176 !g_ascii_strncasecmp(c, "&gt;", 4) ||
2177 !g_ascii_strncasecmp(c, "&quot;", 6)) {
2178 return TRUE;
2180 return FALSE;
2183 static const char *
2184 process_link(GString *ret,
2185 const char *start, const char *c,
2186 int matchlen,
2187 const char *urlprefix,
2188 int inside_paren)
2190 char *url_buf, *tmpurlbuf;
2191 const char *t;
2193 for (t = c;; t++) {
2194 if (!badchar(*t) && !badentity(t))
2195 continue;
2197 if (t - c == matchlen)
2198 break;
2200 if (*t == ',' && *(t + 1) != ' ') {
2201 continue;
2204 if (t > start && *(t - 1) == '.')
2205 t--;
2206 if (t > start && *(t - 1) == ')' && inside_paren > 0)
2207 t--;
2209 url_buf = g_strndup(c, t - c);
2210 tmpurlbuf = purple_unescape_html(url_buf);
2211 g_string_append_printf(ret, "<A HREF=\"%s%s\">%s</A>",
2212 urlprefix,
2213 tmpurlbuf, url_buf);
2214 g_free(tmpurlbuf);
2215 g_free(url_buf);
2216 return t;
2219 return c;
2222 char *
2223 purple_markup_linkify(const char *text)
2225 const char *c, *t, *q = NULL;
2226 char *tmpurlbuf, *url_buf;
2227 gunichar g;
2228 gboolean inside_html = FALSE;
2229 int inside_paren = 0;
2230 GString *ret;
2232 if (text == NULL)
2233 return NULL;
2235 ret = g_string_new("");
2237 c = text;
2238 while (*c) {
2240 if(*c == '(' && !inside_html) {
2241 inside_paren++;
2242 ret = g_string_append_c(ret, *c);
2243 c++;
2246 if(inside_html) {
2247 if(*c == '>') {
2248 inside_html = FALSE;
2249 } else if(!q && (*c == '\"' || *c == '\'')) {
2250 q = c;
2251 } else if(q) {
2252 if(*c == *q)
2253 q = NULL;
2255 } else if(*c == '<') {
2256 inside_html = TRUE;
2257 if (!g_ascii_strncasecmp(c, "<A", 2)) {
2258 while (1) {
2259 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
2260 inside_html = FALSE;
2261 break;
2263 ret = g_string_append_c(ret, *c);
2264 c++;
2265 if (!(*c))
2266 break;
2269 } else if (!g_ascii_strncasecmp(c, "http://", 7)) {
2270 c = process_link(ret, text, c, 7, "", inside_paren);
2271 } else if (!g_ascii_strncasecmp(c, "https://", 8)) {
2272 c = process_link(ret, text, c, 8, "", inside_paren);
2273 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) {
2274 c = process_link(ret, text, c, 6, "", inside_paren);
2275 } else if (!g_ascii_strncasecmp(c, "sftp://", 7)) {
2276 c = process_link(ret, text, c, 7, "", inside_paren);
2277 } else if (!g_ascii_strncasecmp(c, "file://", 7)) {
2278 c = process_link(ret, text, c, 7, "", inside_paren);
2279 } else if (!g_ascii_strncasecmp(c, "www.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2280 c = process_link(ret, text, c, 4, "http://", inside_paren);
2281 } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2282 c = process_link(ret, text, c, 4, "ftp://", inside_paren);
2283 } else if (!g_ascii_strncasecmp(c, "xmpp:", 5) && (c == text || badchar(c[-1]) || badentity(c-1))) {
2284 c = process_link(ret, text, c, 5, "", inside_paren);
2285 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) {
2286 t = c;
2287 while (1) {
2288 if (badchar(*t) || badentity(t)) {
2289 char *d;
2290 if (t - c == 7) {
2291 break;
2293 if (t > text && *(t - 1) == '.')
2294 t--;
2295 if ((d = strstr(c + 7, "?")) != NULL && d < t)
2296 url_buf = g_strndup(c + 7, d - c - 7);
2297 else
2298 url_buf = g_strndup(c + 7, t - c - 7);
2299 if (!purple_email_is_valid(url_buf)) {
2300 g_free(url_buf);
2301 break;
2303 g_free(url_buf);
2304 url_buf = g_strndup(c, t - c);
2305 tmpurlbuf = purple_unescape_html(url_buf);
2306 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
2307 tmpurlbuf, url_buf);
2308 g_free(url_buf);
2309 g_free(tmpurlbuf);
2310 c = t;
2311 break;
2313 t++;
2315 } else if (c != text && (*c == '@')) {
2316 int flag;
2317 GString *gurl_buf = NULL;
2318 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2320 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1)))
2321 flag = 0;
2322 else {
2323 flag = 1;
2324 gurl_buf = g_string_new("");
2327 t = c;
2328 while (flag) {
2329 /* iterate backwards grabbing the local part of an email address */
2330 g = g_utf8_get_char(t);
2331 if (badchar(*t) || (g >= 127) || (*t == '(') ||
2332 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "&lt;", 4) ||
2333 !g_ascii_strncasecmp(t - 3, "&gt;", 4))) ||
2334 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, "&quot;", 6)))))) {
2335 /* local part will already be part of ret, strip it out */
2336 ret = g_string_truncate(ret, ret->len - (c - t));
2337 ret = g_string_append_unichar(ret, g);
2338 break;
2339 } else {
2340 g_string_prepend_unichar(gurl_buf, g);
2341 t = g_utf8_find_prev_char(text, t);
2342 if (t < text) {
2343 ret = g_string_assign(ret, "");
2344 break;
2349 t = g_utf8_find_next_char(c, NULL);
2351 while (flag) {
2352 /* iterate forwards grabbing the domain part of an email address */
2353 g = g_utf8_get_char(t);
2354 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) {
2355 char *d;
2357 url_buf = g_string_free(gurl_buf, FALSE);
2359 /* strip off trailing periods */
2360 if (*url_buf) {
2361 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
2362 *d = '\0';
2365 tmpurlbuf = purple_unescape_html(url_buf);
2366 if (purple_email_is_valid(tmpurlbuf)) {
2367 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
2368 tmpurlbuf, url_buf);
2369 } else {
2370 g_string_append(ret, url_buf);
2372 g_free(url_buf);
2373 g_free(tmpurlbuf);
2374 c = t;
2376 break;
2377 } else {
2378 g_string_append_unichar(gurl_buf, g);
2379 t = g_utf8_find_next_char(t, NULL);
2384 if(*c == ')' && !inside_html) {
2385 inside_paren--;
2386 ret = g_string_append_c(ret, *c);
2387 c++;
2390 if (*c == 0)
2391 break;
2393 ret = g_string_append_c(ret, *c);
2394 c++;
2397 return g_string_free(ret, FALSE);
2400 char *purple_unescape_text(const char *in)
2402 GString *ret;
2403 const char *c = in;
2405 if (in == NULL)
2406 return NULL;
2408 ret = g_string_new("");
2409 while (*c) {
2410 int len;
2411 const char *ent;
2413 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2414 g_string_append(ret, ent);
2415 c += len;
2416 } else {
2417 g_string_append_c(ret, *c);
2418 c++;
2422 return g_string_free(ret, FALSE);
2425 char *purple_unescape_html(const char *html)
2427 GString *ret;
2428 const char *c = html;
2430 if (html == NULL)
2431 return NULL;
2433 ret = g_string_new("");
2434 while (*c) {
2435 int len;
2436 const char *ent;
2438 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2439 g_string_append(ret, ent);
2440 c += len;
2441 } else if (!strncmp(c, "<br>", 4)) {
2442 g_string_append_c(ret, '\n');
2443 c += 4;
2444 } else {
2445 g_string_append_c(ret, *c);
2446 c++;
2450 return g_string_free(ret, FALSE);
2453 char *
2454 purple_markup_slice(const char *str, guint x, guint y)
2456 GString *ret;
2457 GQueue *q;
2458 guint z = 0;
2459 gboolean appended = FALSE;
2460 gunichar c;
2461 char *tag;
2463 g_return_val_if_fail(str != NULL, NULL);
2464 g_return_val_if_fail(x <= y, NULL);
2466 if (x == y)
2467 return g_strdup("");
2469 ret = g_string_new("");
2470 q = g_queue_new();
2472 while (*str && (z < y)) {
2473 c = g_utf8_get_char(str);
2475 if (c == '<') {
2476 char *end = strchr(str, '>');
2478 if (!end) {
2479 g_string_free(ret, TRUE);
2480 while ((tag = g_queue_pop_head(q)))
2481 g_free(tag);
2482 g_queue_free(q);
2483 return NULL;
2486 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
2487 z += strlen("[Image]");
2488 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
2489 z += 1;
2490 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
2491 z += strlen("\n---\n");
2492 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
2493 /* pop stack */
2494 char *tmp;
2496 tmp = g_queue_pop_head(q);
2497 g_free(tmp);
2498 /* z += 0; */
2499 } else {
2500 /* push it unto the stack */
2501 char *tmp;
2503 tmp = g_strndup(str, end - str + 1);
2504 g_queue_push_head(q, tmp);
2505 /* z += 0; */
2508 if (z >= x) {
2509 g_string_append_len(ret, str, end - str + 1);
2512 str = end;
2513 } else if (c == '&') {
2514 char *end = strchr(str, ';');
2515 if (!end) {
2516 g_string_free(ret, TRUE);
2517 while ((tag = g_queue_pop_head(q)))
2518 g_free(tag);
2519 g_queue_free(q);
2521 return NULL;
2524 if (z >= x)
2525 g_string_append_len(ret, str, end - str + 1);
2527 z++;
2528 str = end;
2529 } else {
2530 if (z == x && z > 0 && !appended) {
2531 GList *l = q->tail;
2533 while (l) {
2534 tag = l->data;
2535 g_string_append(ret, tag);
2536 l = l->prev;
2538 appended = TRUE;
2541 if (z >= x)
2542 g_string_append_unichar(ret, c);
2543 z++;
2546 str = g_utf8_next_char(str);
2549 while ((tag = g_queue_pop_head(q))) {
2550 char *name;
2552 name = purple_markup_get_tag_name(tag);
2553 g_string_append_printf(ret, "</%s>", name);
2554 g_free(name);
2555 g_free(tag);
2558 g_queue_free(q);
2559 return g_string_free(ret, FALSE);
2562 char *
2563 purple_markup_get_tag_name(const char *tag)
2565 int i;
2566 g_return_val_if_fail(tag != NULL, NULL);
2567 g_return_val_if_fail(*tag == '<', NULL);
2569 for (i = 1; tag[i]; i++)
2570 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
2571 break;
2573 return g_strndup(tag+1, i-1);
2576 /**************************************************************************
2577 * Path/Filename Functions
2578 **************************************************************************/
2579 const char *
2580 purple_home_dir(void)
2582 #ifndef _WIN32
2583 return g_get_home_dir();
2584 #else
2585 return wpurple_home_dir();
2586 #endif
2589 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2590 const char *
2591 purple_user_dir(void)
2593 if (custom_user_dir != NULL)
2594 return custom_user_dir;
2595 else if (!user_dir)
2596 user_dir = g_build_filename(purple_home_dir(), ".purple", NULL);
2598 return user_dir;
2601 static const gchar *
2602 purple_xdg_dir(gchar **xdg_dir, const gchar *xdg_base_dir, const gchar *xdg_type)
2604 if (!*xdg_dir) {
2605 if (!custom_user_dir) {
2606 *xdg_dir = g_build_filename(xdg_base_dir, "purple", NULL);
2607 } else {
2608 *xdg_dir = g_build_filename(custom_user_dir, xdg_type, NULL);
2612 return *xdg_dir;
2615 const gchar *
2616 purple_cache_dir(void)
2618 return purple_xdg_dir(&cache_dir, g_get_user_cache_dir(), "cache");
2621 const gchar *
2622 purple_config_dir(void)
2624 return purple_xdg_dir(&config_dir, g_get_user_config_dir(), "config");
2627 const gchar *
2628 purple_data_dir(void)
2630 return purple_xdg_dir(&data_dir, g_get_user_data_dir(), "data");
2633 gboolean
2634 purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path)
2636 gint mkdir_res;
2637 gchar *xdg_path;
2638 gboolean xdg_path_exists;
2640 /* Create destination directory */
2641 mkdir_res = purple_build_dir(purple_xdg_dir, S_IRWXU);
2642 if (mkdir_res == -1) {
2643 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
2644 purple_xdg_dir, g_strerror(errno));
2645 return FALSE;
2648 xdg_path = g_build_filename(purple_xdg_dir, path, NULL);
2649 xdg_path_exists = g_file_test(xdg_path, G_FILE_TEST_EXISTS);
2650 if (!xdg_path_exists) {
2651 gchar *old_path;
2652 gboolean old_path_exists;
2654 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2655 old_path = g_build_filename(purple_user_dir(), path, NULL);
2656 G_GNUC_END_IGNORE_DEPRECATIONS
2657 old_path_exists = g_file_test(old_path, G_FILE_TEST_EXISTS);
2658 if (old_path_exists) {
2659 int rename_res;
2661 rename_res = g_rename(old_path, xdg_path);
2662 if (rename_res == -1) {
2663 purple_debug_error("util", "Error renaming %s to %s; failed migration\n",
2664 old_path, xdg_path);
2665 g_free(old_path);
2666 g_free(xdg_path);
2668 return FALSE;
2672 g_free(old_path);
2675 g_free(xdg_path);
2677 return TRUE;
2680 void purple_util_set_user_dir(const char *dir)
2682 g_free(custom_user_dir);
2684 if (dir != NULL && *dir)
2685 custom_user_dir = g_strdup(dir);
2686 else
2687 custom_user_dir = NULL;
2690 int purple_build_dir(const char *path, int mode)
2692 return g_mkdir_with_parents(path, mode);
2695 static gboolean
2696 purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size)
2698 gchar *filename_full;
2699 gboolean ret = FALSE;
2701 g_return_val_if_fail(dir != NULL, FALSE);
2703 purple_debug_misc("util", "Writing file %s to directory %s",
2704 filename, dir);
2706 /* Ensure the directory exists */
2707 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
2709 if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
2711 purple_debug_error("util", "Error creating directory %s: %s\n",
2712 dir, g_strerror(errno));
2713 return FALSE;
2717 filename_full = g_build_filename(dir, filename, NULL);
2719 ret = purple_util_write_data_to_file_absolute(filename_full, data, size);
2721 g_free(filename_full);
2722 return ret;
2725 gboolean
2726 purple_util_write_data_to_file(const char *filename, const char *data, gssize size)
2728 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2729 const char *user_dir = purple_user_dir();
2730 G_GNUC_END_IGNORE_DEPRECATIONS
2731 gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size);
2733 return ret;
2736 gboolean
2737 purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size)
2739 const char *cache_dir = purple_cache_dir();
2740 gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size);
2742 return ret;
2745 gboolean
2746 purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size)
2748 const char *config_dir = purple_config_dir();
2749 gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size);
2751 return ret;
2754 gboolean
2755 purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size)
2757 const char *data_dir = purple_data_dir();
2758 gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size);
2760 return ret;
2763 gboolean
2764 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size)
2766 GFile *file;
2767 GError *err = NULL;
2769 g_return_val_if_fail(size >= -1, FALSE);
2771 if (size == -1) {
2772 size = strlen(data);
2775 file = g_file_new_for_path(filename_full);
2777 if (!g_file_replace_contents(file, data, size, NULL, FALSE,
2778 G_FILE_CREATE_PRIVATE, NULL, NULL, &err)) {
2779 purple_debug_error("util", "Error writing file: %s: %s\n",
2780 filename_full, err->message);
2781 g_clear_error(&err);
2782 g_object_unref(file);
2783 return FALSE;
2786 g_object_unref(file);
2787 return TRUE;
2790 PurpleXmlNode *
2791 purple_util_read_xml_from_file(const char *filename, const char *description)
2793 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2794 return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util");
2795 G_GNUC_END_IGNORE_DEPRECATIONS
2798 PurpleXmlNode *
2799 purple_util_read_xml_from_cache_file(const char *filename, const char *description)
2801 return purple_xmlnode_from_file(purple_cache_dir(), filename, description, "util");
2804 PurpleXmlNode *
2805 purple_util_read_xml_from_config_file(const char *filename, const char *description)
2807 return purple_xmlnode_from_file(purple_config_dir(), filename, description, "util");
2810 PurpleXmlNode *
2811 purple_util_read_xml_from_data_file(const char *filename, const char *description)
2813 return purple_xmlnode_from_file(purple_data_dir(), filename, description, "util");
2817 * Like mkstemp() but returns a file pointer, uses a pre-set template,
2818 * uses the semantics of tempnam() for the directory to use and allocates
2819 * the space for the filepath.
2821 * Caller is responsible for closing the file and removing it when done,
2822 * as well as freeing the space pointed-to by "path" with g_free().
2824 * Returns NULL on failure and cleans up after itself if so.
2826 static const char *purple_mkstemp_templ = {"purpleXXXXXX"};
2828 FILE *
2829 purple_mkstemp(char **fpath, gboolean binary)
2831 const gchar *tmpdir;
2832 int fd;
2833 FILE *fp = NULL;
2835 g_return_val_if_fail(fpath != NULL, NULL);
2837 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
2838 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) {
2839 fd = g_mkstemp(*fpath);
2840 if(fd == -1) {
2841 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2842 "Couldn't make \"%s\", error: %d\n",
2843 *fpath, errno);
2844 } else {
2845 if((fp = fdopen(fd, "r+")) == NULL) {
2846 close(fd);
2847 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2848 "Couldn't fdopen(), error: %d\n", errno);
2852 if(!fp) {
2853 g_free(*fpath);
2854 *fpath = NULL;
2857 } else {
2858 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2859 "g_get_tmp_dir() failed!\n");
2862 return fp;
2865 gboolean
2866 purple_program_is_valid(const char *program)
2868 GError *error = NULL;
2869 char **argv;
2870 gchar *progname;
2871 gboolean is_valid = FALSE;
2873 g_return_val_if_fail(program != NULL, FALSE);
2874 g_return_val_if_fail(*program != '\0', FALSE);
2876 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
2877 purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid",
2878 "Could not parse program '%s': %s\n",
2879 program, error->message);
2880 g_error_free(error);
2881 return FALSE;
2884 if (argv == NULL) {
2885 return FALSE;
2888 progname = g_find_program_in_path(argv[0]);
2889 is_valid = (progname != NULL);
2891 if(purple_debug_is_verbose())
2892 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program,
2893 is_valid ? "Valid" : "Invalid");
2895 g_strfreev(argv);
2896 g_free(progname);
2898 return is_valid;
2902 gboolean
2903 purple_running_gnome(void)
2905 #ifndef _WIN32
2906 gchar *tmp = g_find_program_in_path("gvfs-open");
2908 if (tmp == NULL) {
2909 tmp = g_find_program_in_path("gnome-open");
2911 if (tmp == NULL) {
2912 return FALSE;
2916 g_free(tmp);
2918 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
2920 return ((tmp != NULL) && (*tmp != '\0'));
2921 #else
2922 return FALSE;
2923 #endif
2926 gboolean
2927 purple_running_kde(void)
2929 #ifndef _WIN32
2930 gchar *tmp = g_find_program_in_path("kfmclient");
2931 const char *session;
2933 if (tmp == NULL)
2934 return FALSE;
2935 g_free(tmp);
2937 session = g_getenv("KDE_FULL_SESSION");
2938 if (purple_strequal(session, "true"))
2939 return TRUE;
2941 /* If you run Purple from Konsole under !KDE, this will provide a
2942 * a false positive. Since we do the GNOME checks first, this is
2943 * only a problem if you're running something !(KDE || GNOME) and
2944 * you run Purple from Konsole. This really shouldn't be a problem. */
2945 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
2946 #else
2947 return FALSE;
2948 #endif
2951 gboolean
2952 purple_running_osx(void)
2954 #if defined(__APPLE__)
2955 return TRUE;
2956 #else
2957 return FALSE;
2958 #endif
2961 typedef union purple_sockaddr {
2962 struct sockaddr sa;
2963 struct sockaddr_in sa_in;
2964 #if defined(AF_INET6)
2965 struct sockaddr_in6 sa_in6;
2966 #endif
2967 struct sockaddr_storage sa_stor;
2968 } PurpleSockaddr;
2970 char *
2971 purple_fd_get_ip(int fd)
2973 PurpleSockaddr addr;
2974 socklen_t namelen = sizeof(addr);
2975 int family;
2977 g_return_val_if_fail(fd != 0, NULL);
2979 if (getsockname(fd, &(addr.sa), &namelen))
2980 return NULL;
2982 family = addr.sa.sa_family;
2984 if (family == AF_INET) {
2985 return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
2987 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
2988 else if (family == AF_INET6) {
2989 char host[INET6_ADDRSTRLEN];
2990 const char *tmp;
2992 tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
2993 return g_strdup(tmp);
2995 #endif
2997 return NULL;
3001 purple_socket_get_family(int fd)
3003 PurpleSockaddr addr;
3004 socklen_t len = sizeof(addr);
3006 g_return_val_if_fail(fd >= 0, -1);
3008 if (getsockname(fd, &(addr.sa), &len))
3009 return -1;
3011 return addr.sa.sa_family;
3014 gboolean
3015 purple_socket_speaks_ipv4(int fd)
3017 int family;
3019 g_return_val_if_fail(fd >= 0, FALSE);
3021 family = purple_socket_get_family(fd);
3023 switch (family) {
3024 case AF_INET:
3025 return TRUE;
3026 #if defined(IPV6_V6ONLY)
3027 case AF_INET6:
3029 int val = 0;
3030 socklen_t len = sizeof(val);
3032 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0)
3033 return FALSE;
3034 return !val;
3036 #endif
3037 default:
3038 return FALSE;
3042 /**************************************************************************
3043 * String Functions
3044 **************************************************************************/
3045 const char *
3046 purple_normalize(PurpleAccount *account, const char *str)
3048 const char *ret = NULL;
3049 static char buf[BUF_LEN];
3051 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3052 g_return_val_if_fail(str != NULL, "");
3054 if (account != NULL)
3056 PurpleProtocol *protocol =
3057 purple_protocols_find(purple_account_get_protocol_id(account));
3059 if (protocol != NULL)
3060 ret = purple_protocol_client_iface_normalize(protocol, account, str);
3063 if (ret == NULL)
3065 char *tmp;
3067 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
3068 g_snprintf(buf, sizeof(buf), "%s", tmp);
3069 g_free(tmp);
3071 ret = buf;
3074 return ret;
3078 * You probably don't want to call this directly, it is
3079 * mainly for use as a protocol callback function. See the
3080 * comments in util.h.
3082 const char *
3083 purple_normalize_nocase(const PurpleAccount *account, const char *str)
3085 static char buf[BUF_LEN];
3086 char *tmp1, *tmp2;
3088 g_return_val_if_fail(str != NULL, NULL);
3090 tmp1 = g_utf8_strdown(str, -1);
3091 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
3092 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : "");
3093 g_free(tmp2);
3094 g_free(tmp1);
3096 return buf;
3099 gboolean
3100 purple_validate(const PurpleProtocol *protocol, const char *str)
3102 const char *normalized;
3104 g_return_val_if_fail(protocol != NULL, FALSE);
3105 g_return_val_if_fail(str != NULL, FALSE);
3107 if (str[0] == '\0')
3108 return FALSE;
3110 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, normalize))
3111 return TRUE;
3113 normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol),
3114 NULL, str);
3116 return (NULL != normalized);
3119 gchar *
3120 purple_strdup_withhtml(const gchar *src)
3122 gulong destsize, i, j;
3123 gchar *dest;
3125 g_return_val_if_fail(src != NULL, NULL);
3127 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3128 destsize = 1;
3129 for (i = 0; src[i] != '\0'; i++)
3131 if (src[i] == '\n')
3132 destsize += 4;
3133 else if (src[i] != '\r')
3134 destsize++;
3137 dest = g_malloc(destsize);
3139 /* Copy stuff, ignoring \r's, because they are dumb */
3140 for (i = 0, j = 0; src[i] != '\0'; i++) {
3141 if (src[i] == '\n') {
3142 strcpy(&dest[j], "<BR>");
3143 j += 4;
3144 } else if (src[i] != '\r')
3145 dest[j++] = src[i];
3148 dest[destsize-1] = '\0';
3150 return dest;
3153 gboolean
3154 purple_str_has_prefix(const char *s, const char *p)
3156 return g_str_has_prefix(s, p);
3159 gboolean
3160 purple_str_has_caseprefix(const gchar *s, const gchar *p)
3162 g_return_val_if_fail(s, FALSE);
3163 g_return_val_if_fail(p, FALSE);
3165 return (g_ascii_strncasecmp(s, p, strlen(p)) == 0);
3168 gboolean
3169 purple_str_has_suffix(const char *s, const char *x)
3171 return g_str_has_suffix(s, x);
3174 char *
3175 purple_str_add_cr(const char *text)
3177 char *ret = NULL;
3178 int count = 0, j;
3179 guint i;
3181 g_return_val_if_fail(text != NULL, NULL);
3183 if (text[0] == '\n')
3184 count++;
3185 for (i = 1; i < strlen(text); i++)
3186 if (text[i] == '\n' && text[i - 1] != '\r')
3187 count++;
3189 if (count == 0)
3190 return g_strdup(text);
3192 ret = g_malloc0(strlen(text) + count + 1);
3194 i = 0; j = 0;
3195 if (text[i] == '\n')
3196 ret[j++] = '\r';
3197 ret[j++] = text[i++];
3198 for (; i < strlen(text); i++) {
3199 if (text[i] == '\n' && text[i - 1] != '\r')
3200 ret[j++] = '\r';
3201 ret[j++] = text[i];
3204 return ret;
3207 void
3208 purple_str_strip_char(char *text, char thechar)
3210 int i, j;
3212 g_return_if_fail(text != NULL);
3214 for (i = 0, j = 0; text[i]; i++)
3215 if (text[i] != thechar)
3216 text[j++] = text[i];
3218 text[j] = '\0';
3221 void
3222 purple_util_chrreplace(char *string, char delimiter,
3223 char replacement)
3225 int i = 0;
3227 g_return_if_fail(string != NULL);
3229 while (string[i] != '\0')
3231 if (string[i] == delimiter)
3232 string[i] = replacement;
3233 i++;
3237 gchar *
3238 purple_strreplace(const char *string, const char *delimiter,
3239 const char *replacement)
3241 gchar **split;
3242 gchar *ret;
3244 g_return_val_if_fail(string != NULL, NULL);
3245 g_return_val_if_fail(delimiter != NULL, NULL);
3246 g_return_val_if_fail(replacement != NULL, NULL);
3248 split = g_strsplit(string, delimiter, 0);
3249 ret = g_strjoinv(replacement, split);
3250 g_strfreev(split);
3252 return ret;
3255 gchar *
3256 purple_strcasereplace(const char *string, const char *delimiter,
3257 const char *replacement)
3259 gchar *ret;
3260 int length_del, length_rep, i, j;
3262 g_return_val_if_fail(string != NULL, NULL);
3263 g_return_val_if_fail(delimiter != NULL, NULL);
3264 g_return_val_if_fail(replacement != NULL, NULL);
3266 length_del = strlen(delimiter);
3267 length_rep = strlen(replacement);
3269 /* Count how many times the delimiter appears */
3270 i = 0; /* position in the source string */
3271 j = 0; /* number of occurrences of "delimiter" */
3272 while (string[i] != '\0') {
3273 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3274 i += length_del;
3275 j += length_rep;
3276 } else {
3277 i++;
3278 j++;
3282 ret = g_malloc(j+1);
3284 i = 0; /* position in the source string */
3285 j = 0; /* position in the destination string */
3286 while (string[i] != '\0') {
3287 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3288 strncpy(&ret[j], replacement, length_rep);
3289 i += length_del;
3290 j += length_rep;
3291 } else {
3292 ret[j] = string[i];
3293 i++;
3294 j++;
3298 ret[j] = '\0';
3300 return ret;
3303 /** TODO: Expose this when we can add API */
3304 static const char *
3305 purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen)
3307 const char *tmp, *ret;
3309 g_return_val_if_fail(haystack != NULL, NULL);
3310 g_return_val_if_fail(needle != NULL, NULL);
3312 if (hlen == -1)
3313 hlen = strlen(haystack);
3314 if (nlen == -1)
3315 nlen = strlen(needle);
3316 tmp = haystack,
3317 ret = NULL;
3319 g_return_val_if_fail(hlen > 0, NULL);
3320 g_return_val_if_fail(nlen > 0, NULL);
3322 while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) {
3323 if (!g_ascii_strncasecmp(needle, tmp, nlen))
3324 ret = tmp;
3325 else
3326 tmp++;
3329 return ret;
3332 const char *
3333 purple_strcasestr(const char *haystack, const char *needle)
3335 return purple_strcasestr_len(haystack, -1, needle, -1);
3338 char *
3339 purple_str_size_to_units(goffset size)
3341 static const char * const size_str[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3342 float size_mag;
3343 gsize size_index = 0;
3345 if (size == -1) {
3346 return g_strdup(_("Calculating..."));
3348 else if (size == 0) {
3349 return g_strdup(_("Unknown."));
3351 else {
3352 size_mag = (float)size;
3354 while ((size_index < G_N_ELEMENTS(size_str) - 1) && (size_mag > 1024)) {
3355 size_mag /= 1024;
3356 size_index++;
3359 if (size_index == 0) {
3360 return g_strdup_printf("%" G_GOFFSET_FORMAT " %s", size, _(size_str[size_index]));
3361 } else {
3362 return g_strdup_printf("%.2f %s", size_mag, _(size_str[size_index]));
3367 char *
3368 purple_str_seconds_to_string(guint secs)
3370 char *ret = NULL;
3371 guint days, hrs, mins;
3373 if (secs < 60)
3375 return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs);
3378 days = secs / (60 * 60 * 24);
3379 secs = secs % (60 * 60 * 24);
3380 hrs = secs / (60 * 60);
3381 secs = secs % (60 * 60);
3382 mins = secs / 60;
3383 /* secs = secs % 60; */
3385 if (days > 0)
3387 ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days);
3390 if (hrs > 0)
3392 if (ret != NULL)
3394 char *tmp = g_strdup_printf(
3395 dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs),
3396 ret, hrs);
3397 g_free(ret);
3398 ret = tmp;
3400 else
3401 ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs);
3404 if (mins > 0)
3406 if (ret != NULL)
3408 char *tmp = g_strdup_printf(
3409 dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins),
3410 ret, mins);
3411 g_free(ret);
3412 ret = tmp;
3414 else
3415 ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins);
3418 return ret;
3422 char *
3423 purple_str_binary_to_ascii(const unsigned char *binary, guint len)
3425 GString *ret;
3426 guint i;
3428 g_return_val_if_fail(len > 0, NULL);
3430 ret = g_string_sized_new(len);
3432 for (i = 0; i < len; i++)
3433 if (binary[i] < 32 || binary[i] > 126)
3434 g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF);
3435 else if (binary[i] == '\\')
3436 g_string_append(ret, "\\\\");
3437 else
3438 g_string_append_c(ret, binary[i]);
3440 return g_string_free(ret, FALSE);
3443 size_t
3444 purple_utf16_size(const gunichar2 *str)
3446 /* UTF16 cannot contain two consequent NUL bytes starting at even
3447 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3448 * Chapter 2.
3451 size_t i = 0;
3453 g_return_val_if_fail(str != NULL, 0);
3455 while (str[i++]);
3457 return i * sizeof(gunichar2);
3460 void
3461 purple_str_wipe(gchar *str)
3463 if (str == NULL)
3464 return;
3465 memset(str, 0, strlen(str));
3466 g_free(str);
3469 void
3470 purple_utf16_wipe(gunichar2 *str)
3472 if (str == NULL)
3473 return;
3474 memset(str, 0, purple_utf16_size(str));
3475 g_free(str);
3478 /**************************************************************************
3479 * URI/URL Functions
3480 **************************************************************************/
3482 void purple_got_protocol_handler_uri(const char *uri)
3484 char proto[11];
3485 char delimiter;
3486 const char *tmp, *param_string;
3487 char *cmd;
3488 GHashTable *params = NULL;
3489 gsize len;
3490 if (!(tmp = strchr(uri, ':')) || tmp == uri) {
3491 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3492 return;
3495 len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri));
3497 strncpy(proto, uri, len);
3498 proto[len] = '\0';
3500 tmp++;
3502 if (purple_strequal(proto, "xmpp"))
3503 delimiter = ';';
3504 else
3505 delimiter = '&';
3507 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter);
3509 if ((param_string = strchr(tmp, '?'))) {
3510 const char *keyend = NULL, *pairstart;
3511 char *key, *value = NULL;
3513 cmd = g_strndup(tmp, (param_string - tmp));
3514 param_string++;
3516 params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
3517 pairstart = tmp = param_string;
3519 while (*tmp || *pairstart) {
3520 if (*tmp == delimiter || !(*tmp)) {
3521 /* If there is no explicit value */
3522 if (keyend == NULL) {
3523 keyend = tmp;
3525 /* without these brackets, clang won't
3526 * recognize tmp as a non-NULL
3529 if (keyend && keyend != pairstart) {
3530 char *p;
3531 key = g_strndup(pairstart, (keyend - pairstart));
3532 /* If there is an explicit value */
3533 if (keyend != tmp && keyend != (tmp - 1))
3534 value = g_strndup(keyend + 1, (tmp - keyend - 1));
3535 for (p = key; *p; ++p)
3536 *p = g_ascii_tolower(*p);
3537 g_hash_table_insert(params, key, value);
3539 keyend = value = NULL;
3540 pairstart = (*tmp) ? tmp + 1 : tmp;
3541 } else if (*tmp == '=')
3542 keyend = tmp;
3544 if (*tmp)
3545 tmp++;
3547 } else
3548 cmd = g_strdup(tmp);
3550 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params);
3552 g_free(cmd);
3553 if (params)
3554 g_hash_table_destroy(params);
3557 const char *
3558 purple_url_decode(const char *str)
3560 static char buf[BUF_LEN];
3561 guint i, j = 0;
3562 char *bum;
3563 char hex[3];
3565 g_return_val_if_fail(str != NULL, NULL);
3568 * XXX - This check could be removed and buf could be made
3569 * dynamically allocated, but this is easier.
3571 if (strlen(str) >= BUF_LEN)
3572 return NULL;
3574 for (i = 0; i < strlen(str); i++) {
3576 if (str[i] != '%')
3577 buf[j++] = str[i];
3578 else {
3579 strncpy(hex, str + ++i, 2);
3580 hex[2] = '\0';
3582 /* i is pointing to the start of the number */
3583 i++;
3586 * Now it's at the end and at the start of the for loop
3587 * will be at the next character.
3589 buf[j++] = strtol(hex, NULL, 16);
3593 buf[j] = '\0';
3595 if (!g_utf8_validate(buf, -1, (const char **)&bum))
3596 *bum = '\0';
3598 return buf;
3601 const char *
3602 purple_url_encode(const char *str)
3604 const char *iter;
3605 static char buf[BUF_LEN];
3606 char utf_char[6];
3607 guint i, j = 0;
3609 g_return_val_if_fail(str != NULL, NULL);
3610 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
3612 iter = str;
3613 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
3614 gunichar c = g_utf8_get_char(iter);
3615 /* If the character is an ASCII character and is alphanumeric
3616 * no need to escape */
3617 if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
3618 buf[j++] = c;
3619 } else {
3620 int bytes = g_unichar_to_utf8(c, utf_char);
3621 for (i = 0; (int)i < bytes; i++) {
3622 if (j > (BUF_LEN - 4))
3623 break;
3624 if (i >= sizeof(utf_char)) {
3625 g_warn_if_reached();
3626 break;
3628 sprintf(buf + j, "%%%02X", utf_char[i] & 0xff);
3629 j += 3;
3634 buf[j] = '\0';
3636 return buf;
3639 /* Originally lifted from
3640 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
3641 * ... and slightly modified to be a bit more rfc822 compliant
3642 * ... and modified a bit more to make domain checking rfc1035 compliant
3643 * with the exception permitted in rfc1101 for domains to start with digit
3644 * but not completely checking to avoid conflicts with IP addresses
3646 gboolean
3647 purple_email_is_valid(const char *address)
3649 const char *c, *domain;
3650 static char *rfc822_specials = "()<>@,;:\\\"[]";
3652 g_return_val_if_fail(address != NULL, FALSE);
3654 if (*address == '.') return FALSE;
3656 /* first we validate the name portion (name@domain) (rfc822)*/
3657 for (c = address; *c; c++) {
3658 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
3659 while (*++c) {
3660 if (*c == '\\') {
3661 if (*c++ && *c < 127 && *c > 0 && *c != '\n' && *c != '\r') continue;
3662 else return FALSE;
3664 if (*c == '\"') break;
3665 if (*c < ' ' || *c >= 127) return FALSE;
3667 if (!*c++) return FALSE;
3668 if (*c == '@') break;
3669 if (*c != '.') return FALSE;
3670 continue;
3672 if (*c == '@') break;
3673 if (*c <= ' ' || *c >= 127) return FALSE;
3674 if (strchr(rfc822_specials, *c)) return FALSE;
3677 /* It's obviously not an email address if we didn't find an '@' above */
3678 if (*c == '\0') return FALSE;
3680 /* strictly we should return false if (*(c - 1) == '.') too, but I think
3681 * we should permit user.@domain type addresses - they do work :) */
3682 if (c == address) return FALSE;
3684 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
3685 if (!*(domain = ++c)) return FALSE;
3686 do {
3687 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
3688 return FALSE;
3689 if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE;
3690 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
3691 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
3692 } while (*++c);
3694 if (*(c - 1) == '-') return FALSE;
3696 return ((c - domain) > 3 ? TRUE : FALSE);
3699 gboolean
3700 purple_ipv4_address_is_valid(const char *ip)
3702 int c, o1, o2, o3, o4;
3703 char end;
3705 g_return_val_if_fail(ip != NULL, FALSE);
3707 c = sscanf(ip, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, &end);
3708 if (c != 4 || o1 < 0 || o1 > 255 || o2 < 0 || o2 > 255 || o3 < 0 || o3 > 255 || o4 < 0 || o4 > 255)
3709 return FALSE;
3710 return TRUE;
3713 gboolean
3714 purple_ipv6_address_is_valid(const gchar *ip)
3716 const gchar *c;
3717 gboolean double_colon = FALSE;
3718 gint chunks = 1;
3719 gint in = 0;
3721 g_return_val_if_fail(ip != NULL, FALSE);
3723 if (*ip == '\0')
3724 return FALSE;
3726 for (c = ip; *c; ++c) {
3727 if ((*c >= '0' && *c <= '9') ||
3728 (*c >= 'a' && *c <= 'f') ||
3729 (*c >= 'A' && *c <= 'F')) {
3730 if (++in > 4)
3731 /* Only four hex digits per chunk */
3732 return FALSE;
3733 continue;
3734 } else if (*c == ':') {
3735 /* The start of a new chunk */
3736 ++chunks;
3737 in = 0;
3738 if (*(c + 1) == ':') {
3740 * '::' indicates a consecutive series of chunks full
3741 * of zeroes. There can be only one of these per address.
3743 if (double_colon)
3744 return FALSE;
3745 double_colon = TRUE;
3747 } else
3748 return FALSE;
3752 * Either we saw a '::' and there were fewer than 8 chunks -or-
3753 * we didn't see a '::' and saw exactly 8 chunks.
3755 return (double_colon && chunks < 8) || (!double_colon && chunks == 8);
3758 gboolean
3759 purple_ip_address_is_valid(const char *ip)
3761 return (purple_ipv4_address_is_valid(ip) || purple_ipv6_address_is_valid(ip));
3764 /* Stolen from gnome_uri_list_extract_uris */
3765 GList *
3766 purple_uri_list_extract_uris(const gchar *uri_list)
3768 const gchar *p, *q;
3769 gchar *retval;
3770 GList *result = NULL;
3772 g_return_val_if_fail (uri_list != NULL, NULL);
3774 p = uri_list;
3776 /* We don't actually try to validate the URI according to RFC
3777 * 2396, or even check for allowed characters - we just ignore
3778 * comments and trim whitespace off the ends. We also
3779 * allow LF delimination as well as the specified CRLF.
3781 while (p) {
3782 if (*p != '#') {
3783 while (isspace(*p))
3784 p++;
3786 q = p;
3787 while (*q && (*q != '\n') && (*q != '\r'))
3788 q++;
3790 if (q > p) {
3791 q--;
3792 while (q > p && isspace(*q))
3793 q--;
3795 retval = (gchar*)g_malloc (q - p + 2);
3796 strncpy (retval, p, q - p + 1);
3797 retval[q - p + 1] = '\0';
3799 result = g_list_prepend (result, retval);
3802 p = strchr (p, '\n');
3803 if (p)
3804 p++;
3807 return g_list_reverse (result);
3811 /* Stolen from gnome_uri_list_extract_filenames */
3812 GList *
3813 purple_uri_list_extract_filenames(const gchar *uri_list)
3815 GList *tmp_list, *node, *result;
3817 g_return_val_if_fail (uri_list != NULL, NULL);
3819 result = purple_uri_list_extract_uris(uri_list);
3821 tmp_list = result;
3822 while (tmp_list) {
3823 gchar *s = (gchar*)tmp_list->data;
3825 node = tmp_list;
3826 tmp_list = tmp_list->next;
3828 if (!strncmp (s, "file:", 5)) {
3829 node->data = g_filename_from_uri (s, NULL, NULL);
3830 /* not sure if this fallback is useful at all */
3831 if (!node->data) node->data = g_strdup (s+5);
3832 } else {
3833 result = g_list_delete_link(result, node);
3835 g_free (s);
3837 return result;
3840 char *
3841 purple_uri_escape_for_open(const char *unescaped)
3843 /* Replace some special characters like $ with their percent-encoded value.
3844 * This shouldn't be necessary because we shell-escape the entire arg before
3845 * exec'ing the browser, however, we had a report that a URL containing
3846 * $(xterm) was causing xterm to start on his system. This is obviously a
3847 * bug on his system, but it's pretty easy for us to protect against it. */
3848 return g_uri_escape_string(unescaped, "[]:;/%#,+?=&@", FALSE);
3851 /**************************************************************************
3852 * UTF8 String Functions
3853 **************************************************************************/
3854 gchar *
3855 purple_utf8_try_convert(const char *str)
3857 gsize converted;
3858 gchar *utf8;
3860 g_return_val_if_fail(str != NULL, NULL);
3862 if (g_utf8_validate(str, -1, NULL)) {
3863 return g_strdup(str);
3866 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
3867 if (utf8 != NULL)
3868 return utf8;
3870 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
3871 if ((utf8 != NULL) && (converted == strlen(str)))
3872 return utf8;
3874 g_free(utf8);
3876 return NULL;
3879 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
3880 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
3881 gchar *
3882 purple_utf8_salvage(const char *str)
3884 GString *workstr;
3885 const char *end;
3887 g_return_val_if_fail(str != NULL, NULL);
3889 workstr = g_string_sized_new(strlen(str));
3891 do {
3892 (void)g_utf8_validate(str, -1, &end);
3893 workstr = g_string_append_len(workstr, str, end - str);
3894 str = end;
3895 if (*str == '\0')
3896 break;
3897 do {
3898 workstr = g_string_append_c(workstr, '?');
3899 str++;
3900 } while (!utf8_first(*str));
3901 } while (*str != '\0');
3903 return g_string_free(workstr, FALSE);
3906 gchar *
3907 purple_utf8_strip_unprintables(const gchar *str)
3909 gchar *workstr, *iter;
3910 const gchar *bad;
3912 if (str == NULL)
3913 /* Act like g_strdup */
3914 return NULL;
3916 if (!g_utf8_validate(str, -1, &bad)) {
3917 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
3918 "first bad character was %02x (%c)\n",
3919 str, *bad, *bad);
3920 g_return_val_if_reached(NULL);
3923 workstr = iter = g_new(gchar, strlen(str) + 1);
3924 while (*str) {
3925 gunichar ch = g_utf8_get_char(str);
3926 gchar *next = g_utf8_next_char(str);
3928 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
3929 * [#x10000-#x10FFFF]
3931 if ((ch == '\t' || ch == '\n' || ch == '\r') ||
3932 (ch >= 0x20 && ch <= 0xD7FF) ||
3933 (ch >= 0xE000 && ch <= 0xFFFD) ||
3934 (ch >= 0x10000 && ch <= 0x10FFFF)) {
3935 memcpy(iter, str, next - str);
3936 iter += (next - str);
3939 str = next;
3942 /* nul-terminate the new string */
3943 *iter = '\0';
3945 return workstr;
3949 * This function is copied from g_strerror() but changed to use
3950 * gai_strerror().
3952 const gchar *
3953 purple_gai_strerror(gint errnum)
3955 static GPrivate msg_private = G_PRIVATE_INIT(g_free);
3956 char *msg;
3957 int saved_errno = errno;
3959 const char *msg_locale;
3961 msg_locale = gai_strerror(errnum);
3962 if (g_get_charset(NULL))
3964 /* This string is already UTF-8--great! */
3965 errno = saved_errno;
3966 return msg_locale;
3968 else
3970 gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
3971 if (msg_utf8)
3973 /* Stick in the quark table so that we can return a static result */
3974 GQuark msg_quark = g_quark_from_string(msg_utf8);
3975 g_free(msg_utf8);
3977 msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
3978 errno = saved_errno;
3979 return msg_utf8;
3983 msg = g_private_get(&msg_private);
3985 if (!msg)
3987 msg = g_new(gchar, 64);
3988 g_private_set(&msg_private, msg);
3991 sprintf(msg, "unknown error (%d)", errnum);
3993 errno = saved_errno;
3994 return msg;
3997 char *
3998 purple_utf8_ncr_encode(const char *str)
4000 GString *out;
4002 g_return_val_if_fail(str != NULL, NULL);
4003 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4005 out = g_string_new("");
4007 for(; *str; str = g_utf8_next_char(str)) {
4008 gunichar wc = g_utf8_get_char(str);
4010 /* super simple check. hopefully not too wrong. */
4011 if(wc >= 0x80) {
4012 g_string_append_printf(out, "&#%u;", (guint32) wc);
4013 } else {
4014 g_string_append_unichar(out, wc);
4018 return g_string_free(out, FALSE);
4022 char *
4023 purple_utf8_ncr_decode(const char *str)
4025 GString *out;
4026 char *buf, *b;
4028 g_return_val_if_fail(str != NULL, NULL);
4029 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4031 buf = (char *) str;
4032 out = g_string_new("");
4034 while( (b = strstr(buf, "&#")) ) {
4035 gunichar wc;
4036 int base = 0;
4038 /* append everything leading up to the &# */
4039 g_string_append_len(out, buf, b-buf);
4041 b += 2; /* skip past the &# */
4043 /* strtoul will treat 0x prefix as hex, but not just x */
4044 if(*b == 'x' || *b == 'X') {
4045 base = 16;
4046 b++;
4049 /* advances buf to the end of the ncr segment */
4050 wc = (gunichar) strtoul(b, &buf, base);
4052 /* this mimics the previous impl of ncr_decode */
4053 if(*buf == ';') {
4054 g_string_append_unichar(out, wc);
4055 buf++;
4059 /* append whatever's left */
4060 g_string_append(out, buf);
4062 return g_string_free(out, FALSE);
4067 purple_utf8_strcasecmp(const char *a, const char *b)
4069 char *a_norm = NULL;
4070 char *b_norm = NULL;
4071 int ret = -1;
4073 if(!a && b)
4074 return -1;
4075 else if(!b && a)
4076 return 1;
4077 else if(!a && !b)
4078 return 0;
4080 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
4082 purple_debug_error("purple_utf8_strcasecmp",
4083 "One or both parameters are invalid UTF8\n");
4084 return ret;
4087 a_norm = g_utf8_casefold(a, -1);
4088 b_norm = g_utf8_casefold(b, -1);
4089 ret = g_utf8_collate(a_norm, b_norm);
4090 g_free(a_norm);
4091 g_free(b_norm);
4093 return ret;
4096 /* previously conversation::find_nick() */
4097 gboolean
4098 purple_utf8_has_word(const char *haystack, const char *needle)
4100 char *hay, *pin, *p;
4101 const char *start, *prev_char;
4102 gunichar before, after;
4103 int n;
4104 gboolean ret = FALSE;
4106 start = hay = g_utf8_strdown(haystack, -1);
4108 pin = g_utf8_strdown(needle, -1);
4109 n = strlen(pin);
4111 while ((p = strstr(start, pin)) != NULL) {
4112 prev_char = g_utf8_find_prev_char(hay, p);
4113 before = -2;
4114 if (prev_char) {
4115 before = g_utf8_get_char(prev_char);
4117 after = g_utf8_get_char_validated(p + n, - 1);
4119 if ((p == hay ||
4120 /* The character before is a reasonable guess for a word boundary
4121 ("!g_unichar_isalnum()" is not a valid way to determine word
4122 boundaries, but it is the only reasonable thing to do here),
4123 and isn't the '&' from a "&amp;" or some such entity*/
4124 (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&'))
4125 && after != (gunichar)-2 && !g_unichar_isalnum(after)) {
4126 ret = TRUE;
4127 break;
4129 start = p + 1;
4132 g_free(pin);
4133 g_free(hay);
4135 return ret;
4138 gboolean purple_message_meify(char *message, gssize len)
4140 char *c;
4141 gboolean inside_html = FALSE;
4143 g_return_val_if_fail(message != NULL, FALSE);
4145 if(len == -1)
4146 len = strlen(message);
4148 for (c = message; *c; c++, len--) {
4149 if(inside_html) {
4150 if(*c == '>')
4151 inside_html = FALSE;
4152 } else {
4153 if(*c == '<')
4154 inside_html = TRUE;
4155 else
4156 break;
4160 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
4161 memmove(c, c+4, len-3);
4162 return TRUE;
4165 return FALSE;
4168 char *purple_text_strip_mnemonic(const char *in)
4170 char *out;
4171 char *a;
4172 char *a0;
4173 const char *b;
4175 g_return_val_if_fail(in != NULL, NULL);
4177 out = g_malloc(strlen(in)+1);
4178 a = out;
4179 b = in;
4181 a0 = a; /* The last non-space char seen so far, or the first char */
4183 while(*b) {
4184 if(*b == '_') {
4185 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
4186 /* Detected CJK style shortcut (Bug 875311) */
4187 a = a0; /* undo the left parenthesis */
4188 b += 3; /* and skip the whole mess */
4189 } else if(*(b+1) == '_') {
4190 *(a++) = '_';
4191 b += 2;
4192 a0 = a;
4193 } else {
4194 b++;
4196 /* We don't want to corrupt the middle of UTF-8 characters */
4197 } else if (!(*b & 0x80)) { /* other 1-byte char */
4198 if (*b != ' ')
4199 a0 = a;
4200 *(a++) = *(b++);
4201 } else {
4202 /* Multibyte utf8 char, don't look for _ inside these */
4203 int n = 0;
4204 int i;
4205 if ((*b & 0xe0) == 0xc0) {
4206 n = 2;
4207 } else if ((*b & 0xf0) == 0xe0) {
4208 n = 3;
4209 } else if ((*b & 0xf8) == 0xf0) {
4210 n = 4;
4211 } else if ((*b & 0xfc) == 0xf8) {
4212 n = 5;
4213 } else if ((*b & 0xfe) == 0xfc) {
4214 n = 6;
4215 } else { /* Illegal utf8 */
4216 n = 1;
4218 a0 = a; /* unless we want to delete CJK spaces too */
4219 for (i = 0; i < n && *b; i += 1) {
4220 *(a++) = *(b++);
4224 *a = '\0';
4226 return out;
4229 const char* purple_unescape_filename(const char *escaped) {
4230 return purple_url_decode(escaped);
4234 /* this is almost identical to purple_url_encode (hence purple_url_decode
4235 * being used above), but we want to keep certain characters unescaped
4236 * for compat reasons */
4237 const char *
4238 purple_escape_filename(const char *str)
4240 const char *iter;
4241 static char buf[BUF_LEN];
4242 char utf_char[6];
4243 guint i, j = 0;
4245 g_return_val_if_fail(str != NULL, NULL);
4246 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4248 iter = str;
4249 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4250 gunichar c = g_utf8_get_char(iter);
4251 /* If the character is an ASCII character and is alphanumeric,
4252 * or one of the specified values, no need to escape */
4253 if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' ||
4254 c == '_' || c == '.' || c == '#')) {
4255 buf[j++] = c;
4256 } else {
4257 int bytes = g_unichar_to_utf8(c, utf_char);
4258 for (i = 0; (int)i < bytes; i++) {
4259 if (j > (BUF_LEN - 4))
4260 break;
4261 if (i >= sizeof(utf_char)) {
4262 g_warn_if_reached();
4263 break;
4265 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
4266 j += 3;
4270 #ifdef _WIN32
4271 /* File/Directory names in windows cannot end in periods/spaces.
4272 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4274 while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' '))
4275 j--;
4276 #endif
4277 buf[j] = '\0';
4279 return buf;
4282 gchar * purple_escape_js(const gchar *str)
4284 gchar *escaped;
4286 json_node_set_string(escape_js_node, str);
4287 json_generator_set_root(escape_js_gen, escape_js_node);
4288 escaped = json_generator_to_data(escape_js_gen, NULL);
4289 json_node_set_boolean(escape_js_node, FALSE);
4291 return escaped;
4294 void purple_restore_default_signal_handlers(void)
4296 #ifndef _WIN32
4297 signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */
4298 signal(SIGINT, SIG_DFL); /* 2: interrupt program */
4299 signal(SIGQUIT, SIG_DFL); /* 3: quit program */
4300 signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */
4301 signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */
4302 signal(SIGABRT, SIG_DFL); /* 6: abort program */
4304 #ifdef SIGPOLL
4305 signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */
4306 #endif /* SIGPOLL */
4308 #ifdef SIGEMT
4309 signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */
4310 #endif /* SIGEMT */
4312 signal(SIGFPE, SIG_DFL); /* 8: floating point exception */
4313 signal(SIGBUS, SIG_DFL); /* 10: bus error */
4314 signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */
4315 signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */
4316 signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */
4317 signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */
4318 signal(SIGTERM, SIG_DFL); /* 15: software termination signal */
4319 signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */
4320 signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */
4321 signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */
4322 #endif /* !_WIN32 */
4325 static void
4326 set_status_with_attrs(PurpleStatus *status, ...)
4328 va_list args;
4329 va_start(args, status);
4330 purple_status_set_active_with_attrs(status, TRUE, args);
4331 va_end(args);
4334 void purple_util_set_current_song(const char *title, const char *artist, const char *album)
4336 GList *list = purple_accounts_get_all();
4337 for (; list; list = list->next) {
4338 PurplePresence *presence;
4339 PurpleStatus *tune;
4340 PurpleAccount *account = list->data;
4341 if (!purple_account_get_enabled(account, purple_core_get_ui()))
4342 continue;
4344 presence = purple_account_get_presence(account);
4345 tune = purple_presence_get_status(presence, "tune");
4346 if (!tune)
4347 continue;
4348 if (title) {
4349 set_status_with_attrs(tune,
4350 PURPLE_TUNE_TITLE, title,
4351 PURPLE_TUNE_ARTIST, artist,
4352 PURPLE_TUNE_ALBUM, album,
4353 NULL);
4354 } else {
4355 purple_status_set_active(tune, FALSE);
4360 char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused)
4362 GString *string;
4363 char *esc;
4365 if (!title || !*title)
4366 return NULL;
4368 esc = g_markup_escape_text(title, -1);
4369 string = g_string_new("");
4370 g_string_append_printf(string, "%s", esc);
4371 g_free(esc);
4373 if (artist && *artist) {
4374 esc = g_markup_escape_text(artist, -1);
4375 g_string_append_printf(string, _(" - %s"), esc);
4376 g_free(esc);
4379 if (album && *album) {
4380 esc = g_markup_escape_text(album, -1);
4381 g_string_append_printf(string, _(" (%s)"), esc);
4382 g_free(esc);
4385 return g_string_free(string, FALSE);
4388 const gchar *
4389 purple_get_host_name(void)
4391 return g_get_host_name();
4394 gchar *
4395 purple_uuid_random(void)
4397 guint32 tmp, a, b;
4399 tmp = g_random_int();
4400 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */
4401 tmp >>= 12;
4402 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */
4404 tmp = g_random_int();
4406 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4407 g_random_int(),
4408 tmp & 0xFFFF,
4411 (tmp >> 16) & 0xFFFF, g_random_int());
4414 void purple_callback_set_zero(gpointer data)
4416 gpointer *ptr = data;
4418 g_return_if_fail(ptr != NULL);
4420 *ptr = NULL;
4423 GValue *
4424 purple_value_new(GType type)
4426 GValue *ret;
4428 g_return_val_if_fail(type != G_TYPE_NONE, NULL);
4430 ret = g_new0(GValue, 1);
4431 g_value_init(ret, type);
4433 return ret;
4436 GValue *
4437 purple_value_dup(GValue *value)
4439 GValue *ret;
4441 g_return_val_if_fail(value != NULL, NULL);
4443 ret = g_new0(GValue, 1);
4444 g_value_init(ret, G_VALUE_TYPE(value));
4445 g_value_copy(value, ret);
4447 return ret;
4450 void
4451 purple_value_free(GValue *value)
4453 g_return_if_fail(value != NULL);
4455 g_value_unset(value);
4456 g_free(value);
4459 gchar *purple_http_digest_calculate_session_key(
4460 const gchar *algorithm,
4461 const gchar *username,
4462 const gchar *realm,
4463 const gchar *password,
4464 const gchar *nonce,
4465 const gchar *client_nonce)
4467 GChecksum *hasher;
4468 gchar *hash;
4470 g_return_val_if_fail(username != NULL, NULL);
4471 g_return_val_if_fail(realm != NULL, NULL);
4472 g_return_val_if_fail(password != NULL, NULL);
4473 g_return_val_if_fail(nonce != NULL, NULL);
4475 /* Check for a supported algorithm. */
4476 g_return_val_if_fail(algorithm == NULL ||
4477 *algorithm == '\0' ||
4478 g_ascii_strcasecmp(algorithm, "MD5") ||
4479 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
4481 hasher = g_checksum_new(G_CHECKSUM_MD5);
4482 g_return_val_if_fail(hasher != NULL, NULL);
4484 g_checksum_update(hasher, (guchar *)username, -1);
4485 g_checksum_update(hasher, (guchar *)":", -1);
4486 g_checksum_update(hasher, (guchar *)realm, -1);
4487 g_checksum_update(hasher, (guchar *)":", -1);
4488 g_checksum_update(hasher, (guchar *)password, -1);
4490 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
4492 guchar digest[16];
4493 gsize digest_len = 16;
4495 if (client_nonce == NULL)
4497 g_object_unref(hasher);
4498 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
4499 return NULL;
4502 g_checksum_get_digest(hasher, digest, &digest_len);
4504 g_checksum_reset(hasher);
4505 g_checksum_update(hasher, digest, sizeof(digest));
4506 g_checksum_update(hasher, (guchar *)":", -1);
4507 g_checksum_update(hasher, (guchar *)nonce, -1);
4508 g_checksum_update(hasher, (guchar *)":", -1);
4509 g_checksum_update(hasher, (guchar *)client_nonce, -1);
4512 hash = g_strdup(g_checksum_get_string(hasher));
4513 g_checksum_free(hasher);
4515 return hash;
4518 gchar *purple_http_digest_calculate_response(
4519 const gchar *algorithm,
4520 const gchar *method,
4521 const gchar *digest_uri,
4522 const gchar *qop,
4523 const gchar *entity,
4524 const gchar *nonce,
4525 const gchar *nonce_count,
4526 const gchar *client_nonce,
4527 const gchar *session_key)
4529 GChecksum *hash;
4530 gchar *hash2;
4532 g_return_val_if_fail(method != NULL, NULL);
4533 g_return_val_if_fail(digest_uri != NULL, NULL);
4534 g_return_val_if_fail(nonce != NULL, NULL);
4535 g_return_val_if_fail(session_key != NULL, NULL);
4537 /* Check for a supported algorithm. */
4538 g_return_val_if_fail(algorithm == NULL ||
4539 *algorithm == '\0' ||
4540 g_ascii_strcasecmp(algorithm, "MD5") ||
4541 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
4543 /* Check for a supported "quality of protection". */
4544 g_return_val_if_fail(qop == NULL ||
4545 *qop == '\0' ||
4546 g_ascii_strcasecmp(qop, "auth") ||
4547 g_ascii_strcasecmp(qop, "auth-int"), NULL);
4549 hash = g_checksum_new(G_CHECKSUM_MD5);
4550 g_return_val_if_fail(hash != NULL, NULL);
4552 g_checksum_update(hash, (guchar *)method, -1);
4553 g_checksum_update(hash, (guchar *)":", -1);
4554 g_checksum_update(hash, (guchar *)digest_uri, -1);
4556 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
4558 gchar *entity_hash;
4560 if (entity == NULL)
4562 g_checksum_free(hash);
4563 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
4564 return NULL;
4567 entity_hash = g_compute_checksum_for_string(G_CHECKSUM_MD5,
4568 entity, -1);
4570 if (entity_hash == NULL) {
4571 g_checksum_free(hash);
4572 g_return_val_if_reached(NULL);
4575 g_checksum_update(hash, (guchar *)":", -1);
4576 g_checksum_update(hash, (guchar *)entity_hash, -1);
4577 g_free(entity_hash);
4580 hash2 = g_strdup(g_checksum_get_string(hash));
4581 g_checksum_reset(hash);
4583 if (hash2 == NULL) {
4584 g_checksum_free(hash);
4585 g_return_val_if_reached(NULL);
4588 g_checksum_update(hash, (guchar *)session_key, -1);
4589 g_checksum_update(hash, (guchar *)":", -1);
4590 g_checksum_update(hash, (guchar *)nonce, -1);
4591 g_checksum_update(hash, (guchar *)":", -1);
4593 if (qop != NULL && *qop != '\0')
4595 if (nonce_count == NULL)
4597 g_checksum_free(hash);
4598 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
4599 return NULL;
4602 if (client_nonce == NULL)
4604 g_checksum_free(hash);
4605 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
4606 return NULL;
4609 g_checksum_update(hash, (guchar *)nonce_count, -1);
4610 g_checksum_update(hash, (guchar *)":", -1);
4611 g_checksum_update(hash, (guchar *)client_nonce, -1);
4612 g_checksum_update(hash, (guchar *)":", -1);
4614 g_checksum_update(hash, (guchar *)qop, -1);
4616 g_checksum_update(hash, (guchar *)":", -1);
4619 g_checksum_update(hash, (guchar *)hash2, -1);
4620 g_free(hash2);
4622 hash2 = g_strdup(g_checksum_get_string(hash));
4623 g_checksum_free(hash);
4625 return hash2;
4629 _purple_fstat(int fd, GStatBuf *st)
4631 int ret;
4633 g_return_val_if_fail(st != NULL, -1);
4635 #ifdef _WIN32
4636 ret = _fstat(fd, st);
4637 #else
4638 ret = fstat(fd, st);
4639 #endif
4641 return ret;