Replace functions which called once with their bodies
[pidgin-git.git] / libpurple / util.c
blob51d0c4dcac11aef12bcce4c4d04a2237c7409e88
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) {
307 *rest = str;
310 return 0;
313 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
314 if (rest != NULL && *str != '\0')
315 *rest = str;
317 return 0;
320 /* 4 digit year */
321 if (sscanf(str, "%04d", &year) && year >= 1900) {
322 str += 4;
324 if (*str == '-' || *str == '/')
325 str++;
327 t.tm_year = year - 1900;
330 /* 2 digit month */
331 if (!sscanf(str, "%02d", &t.tm_mon)) {
332 if (rest != NULL && *str != '\0')
333 *rest = str;
335 return 0;
338 str += 2;
339 t.tm_mon -= 1;
341 if (*str == '-' || *str == '/')
342 str++;
344 /* 2 digit day */
345 if (!sscanf(str, "%02d", &t.tm_mday)) {
346 if (rest != NULL && *str != '\0')
347 *rest = str;
349 return 0;
352 str += 2;
354 /* Grab the year off the end if there's still stuff */
355 if (*str == '/' || *str == '-') {
356 /* But make sure we don't read the year twice */
357 if (year >= 1900) {
358 if (rest != NULL && *str != '\0')
359 *rest = str;
361 return 0;
364 str++;
366 if (!sscanf(str, "%04d", &t.tm_year)) {
367 if (rest != NULL && *str != '\0')
368 *rest = str;
370 return 0;
373 t.tm_year -= 1900;
374 } else if (*str == 'T' || *str == '.') {
375 str++;
377 /* Continue grabbing the hours/minutes/seconds */
378 if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
379 (str += 8)) ||
380 (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 &&
381 (str += 6)))
383 gint sign, tzhrs, tzmins;
385 if (*str == '.') {
386 /* Cut off those pesky micro-seconds */
387 do {
388 str++;
389 } while (*str >= '0' && *str <= '9');
392 sign = (*str == '+') ? 1 : -1;
394 /* Process the timezone */
395 if (*str == '+' || *str == '-') {
396 str++;
398 if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) ||
399 (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4))))
401 mktime_with_utc = TRUE;
402 tzoff = tzhrs * 60 * 60 + tzmins * 60;
403 tzoff *= sign;
405 } else if (*str == 'Z') {
406 /* 'Z' = Zulu = UTC */
407 str++;
408 mktime_with_utc = TRUE;
409 tzoff = 0;
412 if (!mktime_with_utc)
414 /* No timezone specified. */
416 if (utc) {
417 mktime_with_utc = TRUE;
418 tzoff = 0;
419 } else {
420 /* Local Time */
421 t.tm_isdst = -1;
427 if (rest != NULL && *str != '\0') {
428 /* Strip trailing whitespace */
429 while (g_ascii_isspace(*str))
430 str++;
432 if (*str != '\0')
433 *rest = str;
436 if (mktime_with_utc)
437 retval = mktime_utc(&t);
438 else
439 retval = mktime(&t);
441 if (tm != NULL)
442 *tm = t;
444 if (tzoff != PURPLE_NO_TZ_OFF)
445 retval -= tzoff;
447 if (tz_off != NULL)
448 *tz_off = tzoff;
450 return retval;
453 GDateTime *
454 purple_str_to_date_time(const char *timestamp, gboolean utc)
456 const gchar *str;
457 gint year = 0;
458 gint month = 0;
459 gint day = 0;
460 gint hour = 0;
461 gint minute = 0;
462 gint seconds = 0;
463 gint microseconds = 0;
464 int chars = 0;
465 GTimeZone *tz = NULL;
466 GDateTime *retval;
468 g_return_val_if_fail(timestamp != NULL, NULL);
470 str = timestamp;
472 /* Strip leading whitespace */
473 while (g_ascii_isspace(*str))
474 str++;
476 if (*str == '\0') {
477 return NULL;
480 if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') {
481 return NULL;
484 /* 4 digit year */
485 if (sscanf(str, "%04d", &year) && year > 0) {
486 str += 4;
488 if (*str == '-' || *str == '/')
489 str++;
492 /* 2 digit month */
493 if (!sscanf(str, "%02d", &month)) {
494 return NULL;
497 str += 2;
499 if (*str == '-' || *str == '/')
500 str++;
502 /* 2 digit day */
503 if (!sscanf(str, "%02d", &day)) {
504 return NULL;
507 str += 2;
509 /* Grab the year off the end if there's still stuff */
510 if (*str == '/' || *str == '-') {
511 /* But make sure we don't read the year twice */
512 if (year > 0) {
513 return NULL;
516 str++;
518 if (!sscanf(str, "%04d", &year)) {
519 return NULL;
521 } else if (*str == 'T' || *str == '.') {
522 str++;
524 /* Continue grabbing the hours/minutes/seconds */
525 if ((sscanf(str, "%02d:%02d:%02d", &hour, &minute, &seconds) == 3 &&
526 (str += 8)) ||
527 (sscanf(str, "%02d%02d%02d", &hour, &minute, &seconds) == 3 &&
528 (str += 6)))
530 if (*str == '.') {
531 str++;
532 if (sscanf(str, "%d%n", &microseconds, &chars) == 1) {
533 str += chars;
537 if (*str) {
538 const gchar *end = str;
539 if (*end == '+' || *end == '-') {
540 end++;
543 while (isdigit(*end) || *end == ':') {
544 end++;
547 if (str != end) {
548 /* Trim anything trailing a purely numeric time zone. */
549 gchar *tzstr = g_strndup(str, end - str);
550 tz = g_time_zone_new(tzstr);
551 g_free(tzstr);
552 } else {
553 /* Just try whatever is there. */
554 tz = g_time_zone_new(str);
560 if (!tz) {
561 /* No timezone specified. */
562 if (utc) {
563 tz = g_time_zone_new_utc();
564 } else {
565 tz = g_time_zone_new_local();
569 retval = g_date_time_new(tz, year, month, day, hour, minute,
570 seconds + microseconds * pow(10, -chars));
571 g_time_zone_unref(tz);
573 return retval;
576 char *
577 purple_uts35_to_str(const char *format, size_t len, struct tm *tm)
579 GString *string;
580 guint i, count;
582 if (tm == NULL) {
583 time_t now = time(NULL);
584 tm = localtime(&now);
587 string = g_string_sized_new(len);
588 i = 0;
589 while (i < len) {
590 count = 1;
591 while ((i + count) < len && format[i] == format[i+count])
592 count++;
594 switch (format[i]) {
595 /* Era Designator */
596 case 'G':
597 if (count <= 3) {
598 /* Abbreviated */
599 } else if (count == 4) {
600 /* Full */
601 } else if (count >= 5) {
602 /* Narrow */
603 count = 5;
605 break;
608 /* Year */
609 case 'y':
610 if (count == 2) {
611 /* Two-digits only */
612 g_string_append(string, purple_utf8_strftime("%y", tm));
613 } else {
614 /* Zero-padding */
615 g_string_append_printf(string, "%0*d",
616 count,
617 tm->tm_year + 1900);
619 break;
621 /* Year (in "Week of Year" based calendars) */
622 case 'Y':
623 if (count == 2) {
624 /* Two-digits only */
625 } else {
626 /* Zero-padding */
628 break;
630 /* Extended Year */
631 case 'u':
632 break;
634 /* Cyclic Year Name */
635 case 'U':
636 if (count <= 3) {
637 /* Abbreviated */
638 } else if (count == 4) {
639 /* Full */
640 } else if (count >= 5) {
641 /* Narrow */
642 count = 5;
644 break;
647 /* Quarter */
648 case 'Q':
649 /* Stand-alone Quarter */
650 case 'q':
651 if (count <= 2) {
652 /* Numerical */
653 } else if (count == 3) {
654 /* Abbreviation */
655 } else if (count >= 4) {
656 /* Full */
657 count = 4;
659 break;
661 /* Month */
662 case 'M':
663 /* Stand-alone Month */
664 case 'L':
665 if (count <= 2) {
666 /* Numerical */
667 g_string_append(string, purple_utf8_strftime("%m", tm));
668 } else if (count == 3) {
669 /* Abbreviation */
670 g_string_append(string, purple_utf8_strftime("%b", tm));
671 } else if (count == 4) {
672 /* Full */
673 g_string_append(string, purple_utf8_strftime("%B", tm));
674 } else if (count >= 5) {
675 g_string_append_len(string, purple_utf8_strftime("%b", tm), 1);
676 count = 5;
678 break;
680 /* Ignored */
681 case 'l':
682 break;
685 /* Week of Year */
686 case 'w':
687 g_string_append(string, purple_utf8_strftime("%W", tm));
688 count = MIN(count, 2);
689 break;
691 /* Week of Month */
692 case 'W':
693 count = 1;
694 break;
697 /* Day of Month */
698 case 'd':
699 g_string_append(string, purple_utf8_strftime("%d", tm));
700 count = MIN(count, 2);
701 break;
703 /* Day of Year */
704 case 'D':
705 g_string_append(string, purple_utf8_strftime("%j", tm));
706 count = MIN(count, 3);
707 break;
709 /* Day of Year in Month */
710 case 'F':
711 count = 1;
712 break;
714 /* Modified Julian Day */
715 case 'g':
716 break;
719 /* Day of Week */
720 case 'E':
721 if (count <= 3) {
722 /* Short */
723 g_string_append(string, purple_utf8_strftime("%a", tm));
724 } else if (count == 4) {
725 /* Full */
726 g_string_append(string, purple_utf8_strftime("%A", tm));
727 } else if (count >= 5) {
728 /* Narrow */
729 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
730 count = 5;
732 break;
734 /* Local Day of Week */
735 case 'e':
736 if (count <= 2) {
737 /* Numeric */
738 g_string_append(string, purple_utf8_strftime("%u", tm));
739 } else if (count == 3) {
740 /* Short */
741 g_string_append(string, purple_utf8_strftime("%a", tm));
742 } else if (count == 4) {
743 /* Full */
744 g_string_append(string, purple_utf8_strftime("%A", tm));
745 } else if (count >= 5) {
746 /* Narrow */
747 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
748 count = 5;
750 break;
752 /* Stand-alone Local Day of Week */
753 case 'c':
754 if (count <= 2) {
755 /* Numeric */
756 g_string_append(string, purple_utf8_strftime("%u", tm));
757 count = 1;
758 } else if (count == 3) {
759 /* Short */
760 g_string_append(string, purple_utf8_strftime("%a", tm));
761 } else if (count == 4) {
762 /* Full */
763 g_string_append(string, purple_utf8_strftime("%A", tm));
764 } else if (count >= 5) {
765 /* Narrow */
766 g_string_append_len(string, purple_utf8_strftime("%a", tm), 1);
767 count = 5;
769 break;
772 /* AM/PM */
773 case 'a':
774 g_string_append(string, purple_utf8_strftime("%p", tm));
775 break;
778 /* Hour (1-12) */
779 case 'h':
780 if (count == 1) {
781 /* No padding */
782 g_string_append(string, purple_utf8_strftime("%I", tm));
783 } else if (count >= 2) {
784 /* Zero-padded */
785 g_string_append(string, purple_utf8_strftime("%I", tm));
786 count = 2;
788 break;
790 /* Hour (0-23) */
791 case 'H':
792 if (count == 1) {
793 /* No padding */
794 g_string_append(string, purple_utf8_strftime("%H", tm));
795 } else if (count >= 2) {
796 /* Zero-padded */
797 g_string_append(string, purple_utf8_strftime("%H", tm));
798 count = 2;
800 break;
802 /* Hour (0-11) */
803 case 'K':
804 /* Hour (1-24) */
805 case 'k':
806 if (count == 1) {
807 /* No padding */
808 } else if (count >= 2) {
809 /* Zero-padded */
810 count = 2;
812 break;
814 /* Hour (hHkK by locale) */
815 case 'j':
816 break;
819 /* Minute */
820 case 'm':
821 g_string_append(string, purple_utf8_strftime("%M", tm));
822 count = MIN(count, 2);
823 break;
826 /* Second */
827 case 's':
828 g_string_append(string, purple_utf8_strftime("%S", tm));
829 count = MIN(count, 2);
830 break;
832 /* Fractional Sub-second */
833 case 'S':
834 break;
836 /* Millisecond */
837 case 'A':
838 break;
841 /* Time Zone (specific non-location format) */
842 case 'z':
843 if (count <= 3) {
844 /* Short */
845 } else if (count >= 4) {
846 /* Full */
847 count = 4;
849 break;
851 /* Time Zone */
852 case 'Z':
853 if (count <= 3) {
854 /* RFC822 */
855 g_string_append(string, purple_utf8_strftime("%z", tm));
856 } else if (count == 4) {
857 /* Localized GMT */
858 } else if (count >= 5) {
859 /* ISO8601 */
860 g_string_append(string, purple_utf8_strftime("%z", tm));
861 count = 5;
863 break;
865 /* Time Zone (generic non-location format) */
866 case 'v':
867 if (count <= 3) {
868 /* Short */
869 g_string_append(string, purple_utf8_strftime("%Z", tm));
870 count = 1;
871 } else if (count >= 4) {
872 /* Long */
873 g_string_append(string, purple_utf8_strftime("%Z", tm));
874 count = 4;
876 break;
878 /* Time Zone */
879 case 'V':
880 if (count <= 3) {
881 /* Same as z */
882 count = 1;
883 } else if (count >= 4) {
884 /* Generic Location Format) */
885 g_string_append(string, purple_utf8_strftime("%Z", tm));
886 count = 4;
888 break;
891 default:
892 g_string_append_len(string, format + i, count);
893 break;
896 i += count;
899 return g_string_free(string, FALSE);
903 /**************************************************************************
904 * Markup Functions
905 **************************************************************************/
908 * This function is stolen from glib's gmarkup.c and modified to not
909 * replace ' with &apos;
911 static void append_escaped_text(GString *str,
912 const gchar *text, gssize length)
914 const gchar *p;
915 const gchar *end;
916 gunichar c;
918 p = text;
919 end = text + length;
921 while (p != end)
923 const gchar *next;
924 next = g_utf8_next_char (p);
926 switch (*p)
928 case '&':
929 g_string_append (str, "&amp;");
930 break;
932 case '<':
933 g_string_append (str, "&lt;");
934 break;
936 case '>':
937 g_string_append (str, "&gt;");
938 break;
940 case '"':
941 g_string_append (str, "&quot;");
942 break;
944 default:
945 c = g_utf8_get_char (p);
946 if ((0x1 <= c && c <= 0x8) ||
947 (0xb <= c && c <= 0xc) ||
948 (0xe <= c && c <= 0x1f) ||
949 (0x7f <= c && c <= 0x84) ||
950 (0x86 <= c && c <= 0x9f))
951 g_string_append_printf (str, "&#x%x;", c);
952 else
953 g_string_append_len (str, p, next - p);
954 break;
957 p = next;
961 /* This function is stolen from glib's gmarkup.c */
962 gchar *purple_markup_escape_text(const gchar *text, gssize length)
964 GString *str;
966 g_return_val_if_fail(text != NULL, NULL);
968 if (length < 0)
969 length = strlen(text);
971 /* prealloc at least as long as original text */
972 str = g_string_sized_new(length);
973 append_escaped_text(str, text, length);
975 return g_string_free(str, FALSE);
978 const char *
979 purple_markup_unescape_entity(const char *text, int *length)
981 const char *pln;
982 int len;
984 if (!text || *text != '&')
985 return NULL;
987 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
989 if(IS_ENTITY("&amp;"))
990 pln = "&";
991 else if(IS_ENTITY("&lt;"))
992 pln = "<";
993 else if(IS_ENTITY("&gt;"))
994 pln = ">";
995 else if(IS_ENTITY("&nbsp;"))
996 pln = " ";
997 else if(IS_ENTITY("&copy;"))
998 pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
999 else if(IS_ENTITY("&quot;"))
1000 pln = "\"";
1001 else if(IS_ENTITY("&reg;"))
1002 pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1003 else if(IS_ENTITY("&apos;"))
1004 pln = "\'";
1005 else if(text[1] == '#' && (g_ascii_isxdigit(text[2]) || text[2] == 'x')) {
1006 static char buf[7];
1007 const char *start = text + 2;
1008 char *end;
1009 guint64 pound;
1010 int base = 10;
1011 int buflen;
1013 if (*start == 'x') {
1014 base = 16;
1015 start++;
1018 pound = g_ascii_strtoull(start, &end, base);
1019 if (pound == 0 || pound > INT_MAX || *end != ';') {
1020 return NULL;
1023 len = (end - text) + 1;
1025 buflen = g_unichar_to_utf8((gunichar)pound, buf);
1026 buf[buflen] = '\0';
1027 pln = buf;
1029 else
1030 return NULL;
1032 if (length)
1033 *length = len;
1034 return pln;
1037 char *
1038 purple_markup_get_css_property(const gchar *style,
1039 const gchar *opt)
1041 const gchar *css_str = style;
1042 const gchar *css_value_start;
1043 const gchar *css_value_end;
1044 gchar *tmp;
1045 gchar *ret;
1047 g_return_val_if_fail(opt != NULL, NULL);
1049 if (!css_str)
1050 return NULL;
1052 /* find the CSS property */
1053 while (1)
1055 /* skip whitespace characters */
1056 while (*css_str && g_ascii_isspace(*css_str))
1057 css_str++;
1058 if (!g_ascii_isalpha(*css_str))
1059 return NULL;
1060 if (g_ascii_strncasecmp(css_str, opt, strlen(opt)))
1062 /* go to next css property positioned after the next ';' */
1063 while (*css_str && *css_str != '"' && *css_str != ';')
1064 css_str++;
1065 if(*css_str != ';')
1066 return NULL;
1067 css_str++;
1069 else
1070 break;
1073 /* find the CSS value position in the string */
1074 css_str += strlen(opt);
1075 while (*css_str && g_ascii_isspace(*css_str))
1076 css_str++;
1077 if (*css_str != ':')
1078 return NULL;
1079 css_str++;
1080 while (*css_str && g_ascii_isspace(*css_str))
1081 css_str++;
1082 if (*css_str == '\0' || *css_str == '"' || *css_str == ';')
1083 return NULL;
1085 /* mark the CSS value */
1086 css_value_start = css_str;
1087 while (*css_str && *css_str != '"' && *css_str != ';')
1088 css_str++;
1089 css_value_end = css_str - 1;
1091 /* Removes trailing whitespace */
1092 while (css_value_end > css_value_start && g_ascii_isspace(*css_value_end))
1093 css_value_end--;
1095 tmp = g_strndup(css_value_start, css_value_end - css_value_start + 1);
1096 ret = purple_unescape_html(tmp);
1097 g_free(tmp);
1099 return ret;
1102 gboolean purple_markup_is_rtl(const char *html)
1104 GData *attributes;
1105 const gchar *start, *end;
1106 gboolean res = FALSE;
1108 if (purple_markup_find_tag("span", html, &start, &end, &attributes))
1110 /* tmp is a member of attributes and is free with g_datalist_clear call */
1111 const char *tmp = g_datalist_get_data(&attributes, "dir");
1112 if (tmp && !g_ascii_strcasecmp(tmp, "RTL"))
1113 res = TRUE;
1114 if (!res)
1116 tmp = g_datalist_get_data(&attributes, "style");
1117 if (tmp)
1119 char *tmp2 = purple_markup_get_css_property(tmp, "direction");
1120 if (tmp2 && !g_ascii_strcasecmp(tmp2, "RTL"))
1121 res = TRUE;
1122 g_free(tmp2);
1126 g_datalist_clear(&attributes);
1128 return res;
1131 gboolean
1132 purple_markup_find_tag(const char *needle, const char *haystack,
1133 const char **start, const char **end, GData **attributes)
1135 GData *attribs;
1136 const char *cur = haystack;
1137 char *name = NULL;
1138 gboolean found = FALSE;
1139 gboolean in_tag = FALSE;
1140 gboolean in_attr = FALSE;
1141 const char *in_quotes = NULL;
1142 size_t needlelen;
1144 g_return_val_if_fail( needle != NULL, FALSE);
1145 g_return_val_if_fail( *needle != '\0', FALSE);
1146 g_return_val_if_fail( haystack != NULL, FALSE);
1147 g_return_val_if_fail( start != NULL, FALSE);
1148 g_return_val_if_fail( end != NULL, FALSE);
1149 g_return_val_if_fail(attributes != NULL, FALSE);
1151 needlelen = strlen(needle);
1152 g_datalist_init(&attribs);
1154 while (*cur && !found) {
1155 if (in_tag) {
1156 if (in_quotes) {
1157 const char *close = cur;
1159 while (*close && *close != *in_quotes)
1160 close++;
1162 /* if we got the close quote, store the value and carry on from *
1163 * after it. if we ran to the end of the string, point to the NULL *
1164 * and we're outta here */
1165 if (*close) {
1166 /* only store a value if we have an attribute name */
1167 if (name) {
1168 size_t len = close - cur;
1169 char *val = g_strndup(cur, len);
1171 g_datalist_set_data_full(&attribs, name, val, g_free);
1172 g_free(name);
1173 name = NULL;
1176 in_quotes = NULL;
1177 cur = close + 1;
1178 } else {
1179 cur = close;
1181 } else if (in_attr) {
1182 const char *close = cur;
1184 while (*close && *close != '>' && *close != '"' &&
1185 *close != '\'' && *close != ' ' && *close != '=')
1186 close++;
1188 /* if we got the equals, store the name of the attribute. if we got
1189 * the quote, save the attribute and go straight to quote mode.
1190 * otherwise the tag closed or we reached the end of the string,
1191 * so we can get outta here */
1192 switch (*close) {
1193 case '"':
1194 case '\'':
1195 in_quotes = close;
1196 /* fall through */
1197 case '=':
1199 size_t len = close - cur;
1201 /* don't store a blank attribute name */
1202 if (len) {
1203 g_free(name);
1204 name = g_ascii_strdown(cur, len);
1207 in_attr = FALSE;
1208 cur = close + 1;
1210 break;
1211 case ' ':
1212 case '>':
1213 in_attr = FALSE;
1214 /* fall through */
1215 default:
1216 cur = close;
1217 break;
1219 } else {
1220 switch (*cur) {
1221 case ' ':
1222 /* swallow extra spaces inside tag */
1223 while (*cur && *cur == ' ') cur++;
1224 in_attr = TRUE;
1225 break;
1226 case '>':
1227 found = TRUE;
1228 *end = cur;
1229 break;
1230 case '"':
1231 case '\'':
1232 in_quotes = cur;
1233 /* fall through */
1234 default:
1235 cur++;
1236 break;
1239 } else {
1240 /* if we hit a < followed by the name of our tag... */
1241 if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) {
1242 *start = cur;
1243 cur = cur + needlelen + 1;
1245 /* if we're pointing at a space or a >, we found the right tag. if *
1246 * we're not, we've found a longer tag, so we need to skip to the *
1247 * >, but not being distracted by >s inside quotes. */
1248 if (*cur == ' ' || *cur == '>') {
1249 in_tag = TRUE;
1250 } else {
1251 while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') {
1252 if (*cur == '"') {
1253 cur++;
1254 while (*cur && *cur != '"')
1255 cur++;
1256 } else if (*cur == '\'') {
1257 cur++;
1258 while (*cur && *cur != '\'')
1259 cur++;
1260 } else {
1261 cur++;
1265 } else {
1266 cur++;
1271 /* clean up any attribute name from a premature termination */
1272 g_free(name);
1274 if (found) {
1275 *attributes = attribs;
1276 } else {
1277 *start = NULL;
1278 *end = NULL;
1279 *attributes = NULL;
1282 return found;
1285 gboolean
1286 purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
1287 const char *start_token, int skip,
1288 const char *end_token, char check_value,
1289 const char *no_value_token,
1290 const char *display_name, gboolean is_link,
1291 const char *link_prefix,
1292 PurpleInfoFieldFormatCallback format_cb)
1294 const char *p, *q;
1296 g_return_val_if_fail(str != NULL, FALSE);
1297 g_return_val_if_fail(user_info != NULL, FALSE);
1298 g_return_val_if_fail(start_token != NULL, FALSE);
1299 g_return_val_if_fail(end_token != NULL, FALSE);
1300 g_return_val_if_fail(display_name != NULL, FALSE);
1302 p = strstr(str, start_token);
1304 if (p == NULL)
1305 return FALSE;
1307 p += strlen(start_token) + skip;
1309 if (p >= str + len)
1310 return FALSE;
1312 if (check_value != '\0' && *p == check_value)
1313 return FALSE;
1315 q = strstr(p, end_token);
1317 /* Trim leading blanks */
1318 while (*p != '\n' && g_ascii_isspace(*p)) {
1319 p += 1;
1322 /* Trim trailing blanks */
1323 while (q > p && g_ascii_isspace(*(q - 1))) {
1324 q -= 1;
1327 /* Don't bother with null strings */
1328 if (p == q)
1329 return FALSE;
1331 if (q != NULL && (!no_value_token ||
1332 strncmp(p, no_value_token, strlen(no_value_token)))) {
1333 GString *dest = g_string_new("");
1335 if (is_link)
1337 g_string_append(dest, "<a href=\"");
1339 if (link_prefix)
1340 g_string_append(dest, link_prefix);
1342 if (format_cb != NULL)
1344 char *reformatted = format_cb(p, q - p);
1345 g_string_append(dest, reformatted);
1346 g_free(reformatted);
1348 else
1349 g_string_append_len(dest, p, q - p);
1350 g_string_append(dest, "\">");
1352 if (link_prefix)
1353 g_string_append(dest, link_prefix);
1355 g_string_append_len(dest, p, q - p);
1356 g_string_append(dest, "</a>");
1358 else
1360 if (format_cb != NULL)
1362 char *reformatted = format_cb(p, q - p);
1363 g_string_append(dest, reformatted);
1364 g_free(reformatted);
1366 else
1367 g_string_append_len(dest, p, q - p);
1370 purple_notify_user_info_add_pair_html(user_info, display_name, dest->str);
1371 g_string_free(dest, TRUE);
1373 return TRUE;
1376 return FALSE;
1379 struct purple_parse_tag {
1380 char *src_tag;
1381 char *dest_tag;
1382 gboolean ignore;
1385 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1386 recommended in the GCC docs). It contains 'continue's that should
1387 affect the while-loop in purple_markup_html_to_xhtml and doing the
1388 above would break that.
1389 Also, remember to put braces in constructs that require them for
1390 multiple statements when using this macro. */
1391 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1392 const char *o = c + strlen("<" x); \
1393 const char *p = NULL, *q = NULL, *r = NULL; \
1394 /* o = iterating over full tag \
1395 * p = > (end of tag) \
1396 * q = start of quoted bit \
1397 * r = < inside tag \
1398 */ \
1399 GString *innards = g_string_new(""); \
1400 while(o && *o) { \
1401 if(!q && (*o == '\"' || *o == '\'') ) { \
1402 q = o; \
1403 } else if(q) { \
1404 if(*o == *q) { /* end of quoted bit */ \
1405 char *unescaped = g_strndup(q+1, o-q-1); \
1406 char *escaped = g_markup_escape_text(unescaped, -1); \
1407 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1408 g_free(unescaped); \
1409 g_free(escaped); \
1410 q = NULL; \
1411 } else if(*c == '\\') { \
1412 o++; \
1414 } else if(*o == '<') { \
1415 r = o; \
1416 } else if(*o == '>') { \
1417 p = o; \
1418 break; \
1419 } else { \
1420 innards = g_string_append_c(innards, *o); \
1422 o++; \
1424 if(p && !r) { /* got an end of tag and no other < earlier */\
1425 if(*(p-1) != '/') { \
1426 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1427 pt->src_tag = x; \
1428 pt->dest_tag = y; \
1429 tags = g_list_prepend(tags, pt); \
1431 if(xhtml) { \
1432 xhtml = g_string_append(xhtml, "<" y); \
1433 xhtml = g_string_append(xhtml, innards->str); \
1434 xhtml = g_string_append_c(xhtml, '>'); \
1436 c = p + 1; \
1437 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1438 if(xhtml) \
1439 xhtml = g_string_append(xhtml, "&lt;"); \
1440 if(plain) \
1441 plain = g_string_append_c(plain, '<'); \
1442 c++; \
1444 g_string_free(innards, TRUE); \
1445 continue; \
1447 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1448 (*(c+strlen("<" x)) == '>' || \
1449 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1450 if(xhtml) \
1451 xhtml = g_string_append(xhtml, "<" y); \
1452 c += strlen("<" x); \
1453 if(*c != '/') { \
1454 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1455 pt->src_tag = x; \
1456 pt->dest_tag = y; \
1457 tags = g_list_prepend(tags, pt); \
1458 if(xhtml) \
1459 xhtml = g_string_append_c(xhtml, '>'); \
1460 } else { \
1461 if(xhtml) \
1462 xhtml = g_string_append(xhtml, "/>");\
1464 c = strchr(c, '>') + 1; \
1465 continue; \
1467 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1468 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1469 void
1470 purple_markup_html_to_xhtml(const char *html, char **xhtml_out,
1471 char **plain_out)
1473 GString *xhtml = NULL;
1474 GString *plain = NULL;
1475 GString *url = NULL;
1476 GString *cdata = NULL;
1477 GList *tags = NULL, *tag;
1478 const char *c = html;
1479 char quote = '\0';
1481 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1482 quote = *(ptr++); \
1483 else \
1484 quote = '\0';
1486 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1488 g_return_if_fail(xhtml_out != NULL || plain_out != NULL);
1490 if(xhtml_out)
1491 xhtml = g_string_new("");
1492 if(plain_out)
1493 plain = g_string_new("");
1495 while(c && *c) {
1496 if(*c == '<') {
1497 if(*(c+1) == '/') { /* closing tag */
1498 tag = tags;
1499 while(tag) {
1500 struct purple_parse_tag *pt = tag->data;
1501 if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') {
1502 c += strlen(pt->src_tag) + 3;
1503 break;
1505 tag = tag->next;
1507 if(tag) {
1508 while(tags) {
1509 struct purple_parse_tag *pt = tags->data;
1510 if(xhtml && !pt->ignore)
1511 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1512 if(plain && purple_strequal(pt->src_tag, "a")) {
1513 /* if this is a link, we have to add the url to the plaintext, too */
1514 if (cdata && url &&
1515 (!g_string_equal(cdata, url) && (g_ascii_strncasecmp(url->str, "mailto:", 7) != 0 ||
1516 g_utf8_collate(url->str + 7, cdata->str) != 0)))
1517 g_string_append_printf(plain, " <%s>", g_strstrip(purple_unescape_html(url->str)));
1518 if (cdata) {
1519 g_string_free(cdata, TRUE);
1520 cdata = NULL;
1524 if(tags == tag)
1525 break;
1526 tags = g_list_remove(tags, pt);
1527 g_free(pt);
1529 g_free(tag->data);
1530 tags = g_list_delete_link(tags, tag);
1531 } else {
1532 /* a closing tag we weren't expecting...
1533 * we'll let it slide, if it's really a tag...if it's
1534 * just a </ we'll escape it properly */
1535 const char *end = c+2;
1536 while(*end && g_ascii_isalpha(*end))
1537 end++;
1538 if(*end == '>') {
1539 c = end+1;
1540 } else {
1541 if(xhtml)
1542 xhtml = g_string_append(xhtml, "&lt;");
1543 if(plain)
1544 plain = g_string_append_c(plain, '<');
1545 c++;
1548 } else { /* opening tag */
1549 ALLOW_TAG("blockquote");
1550 ALLOW_TAG("cite");
1551 ALLOW_TAG("div");
1552 ALLOW_TAG("em");
1553 ALLOW_TAG("h1");
1554 ALLOW_TAG("h2");
1555 ALLOW_TAG("h3");
1556 ALLOW_TAG("h4");
1557 ALLOW_TAG("h5");
1558 ALLOW_TAG("h6");
1559 /* we only allow html to start the message */
1560 if(c == html) {
1561 ALLOW_TAG("html");
1563 ALLOW_TAG_ALT("i", "em");
1564 ALLOW_TAG_ALT("italic", "em");
1565 ALLOW_TAG("li");
1566 ALLOW_TAG("ol");
1567 ALLOW_TAG("p");
1568 ALLOW_TAG("pre");
1569 ALLOW_TAG("q");
1570 ALLOW_TAG("span");
1571 ALLOW_TAG("ul");
1574 /* we skip <HR> because it's not legal in XHTML-IM. However,
1575 * we still want to send something sensible, so we put a
1576 * linebreak in its place. <BR> also needs special handling
1577 * because putting a </BR> to close it would just be dumb. */
1578 if((!g_ascii_strncasecmp(c, "<br", 3)
1579 || !g_ascii_strncasecmp(c, "<hr", 3))
1580 && (*(c+3) == '>' ||
1581 !g_ascii_strncasecmp(c+3, "/>", 2) ||
1582 !g_ascii_strncasecmp(c+3, " />", 3))) {
1583 c = strchr(c, '>') + 1;
1584 if(xhtml)
1585 xhtml = g_string_append(xhtml, "<br/>");
1586 if(plain && *c != '\n')
1587 plain = g_string_append_c(plain, '\n');
1588 continue;
1590 if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c, "<strong>", strlen("<strong>"))) {
1591 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1592 if (*(c+2) == '>')
1593 pt->src_tag = "b";
1594 else if (*(c+2) == 'o')
1595 pt->src_tag = "bold";
1596 else
1597 pt->src_tag = "strong";
1598 pt->dest_tag = "span";
1599 tags = g_list_prepend(tags, pt);
1600 c = strchr(c, '>') + 1;
1601 if(xhtml)
1602 xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>");
1603 continue;
1605 if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) {
1606 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1607 pt->src_tag = *(c+2) == '>' ? "u" : "underline";
1608 pt->dest_tag = "span";
1609 tags = g_list_prepend(tags, pt);
1610 c = strchr(c, '>') + 1;
1611 if (xhtml)
1612 xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>");
1613 continue;
1615 if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) {
1616 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1617 pt->src_tag = *(c+2) == '>' ? "s" : "strike";
1618 pt->dest_tag = "span";
1619 tags = g_list_prepend(tags, pt);
1620 c = strchr(c, '>') + 1;
1621 if(xhtml)
1622 xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>");
1623 continue;
1625 if(!g_ascii_strncasecmp(c, "<sub>", 5)) {
1626 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1627 pt->src_tag = "sub";
1628 pt->dest_tag = "span";
1629 tags = g_list_prepend(tags, pt);
1630 c = strchr(c, '>') + 1;
1631 if(xhtml)
1632 xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>");
1633 continue;
1635 if(!g_ascii_strncasecmp(c, "<sup>", 5)) {
1636 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1637 pt->src_tag = "sup";
1638 pt->dest_tag = "span";
1639 tags = g_list_prepend(tags, pt);
1640 c = strchr(c, '>') + 1;
1641 if(xhtml)
1642 xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>");
1643 continue;
1645 if (!g_ascii_strncasecmp(c, "<img", 4) && (*(c+4) == '>' || *(c+4) == ' ')) {
1646 const char *p = c + 4;
1647 GString *src = NULL, *alt = NULL;
1648 #define ESCAPE(from, to) \
1649 CHECK_QUOTE(from); \
1650 while (VALID_CHAR(from)) { \
1651 int len; \
1652 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1653 to = g_string_append(to, "&amp;"); \
1654 else if (*from == '\'') \
1655 to = g_string_append(to, "&apos;"); \
1656 else \
1657 to = g_string_append_c(to, *from); \
1658 from++; \
1661 while (*p && *p != '>') {
1662 if (!g_ascii_strncasecmp(p, "src=", 4)) {
1663 const char *q = p + 4;
1664 if (src)
1665 g_string_free(src, TRUE);
1666 src = g_string_new("");
1667 ESCAPE(q, src);
1668 p = q;
1669 } else if (!g_ascii_strncasecmp(p, "alt=", 4)) {
1670 const char *q = p + 4;
1671 if (alt)
1672 g_string_free(alt, TRUE);
1673 alt = g_string_new("");
1674 ESCAPE(q, alt);
1675 p = q;
1676 } else {
1677 p++;
1680 #undef ESCAPE
1681 if ((c = strchr(p, '>')) != NULL)
1682 c++;
1683 else
1684 c = p;
1685 /* src and alt are required! */
1686 if(src && xhtml)
1687 g_string_append_printf(xhtml, "<img src='%s' alt='%s' />", g_strstrip(src->str), alt ? alt->str : "");
1688 if(alt) {
1689 if(plain)
1690 plain = g_string_append(plain, purple_unescape_html(alt->str));
1691 if(!src && xhtml)
1692 xhtml = g_string_append(xhtml, alt->str);
1693 g_string_free(alt, TRUE);
1695 g_string_free(src, TRUE);
1696 continue;
1698 if (!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) {
1699 const char *p = c + 2;
1700 struct purple_parse_tag *pt;
1701 while (*p && *p != '>') {
1702 if (!g_ascii_strncasecmp(p, "href=", 5)) {
1703 const char *q = p + 5;
1704 if (url)
1705 g_string_free(url, TRUE);
1706 url = g_string_new("");
1707 if (cdata)
1708 g_string_free(cdata, TRUE);
1709 cdata = g_string_new("");
1710 CHECK_QUOTE(q);
1711 while (VALID_CHAR(q)) {
1712 int len;
1713 if ((*q == '&') && (purple_markup_unescape_entity(q, &len) == NULL))
1714 url = g_string_append(url, "&amp;");
1715 else if (*q == '"')
1716 url = g_string_append(url, "&quot;");
1717 else
1718 url = g_string_append_c(url, *q);
1719 q++;
1721 p = q;
1722 } else {
1723 p++;
1726 if ((c = strchr(p, '>')) != NULL)
1727 c++;
1728 else
1729 c = p;
1730 pt = g_new0(struct purple_parse_tag, 1);
1731 pt->src_tag = "a";
1732 pt->dest_tag = "a";
1733 tags = g_list_prepend(tags, pt);
1734 if(xhtml)
1735 g_string_append_printf(xhtml, "<a href=\"%s\">", url ? g_strstrip(url->str) : "");
1736 continue;
1738 #define ESCAPE(from, to) \
1739 CHECK_QUOTE(from); \
1740 while (VALID_CHAR(from)) { \
1741 int len; \
1742 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1743 to = g_string_append(to, "&amp;"); \
1744 else if (*from == '\'') \
1745 to = g_string_append_c(to, '\"'); \
1746 else \
1747 to = g_string_append_c(to, *from); \
1748 from++; \
1750 if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) {
1751 const char *p = c + 5;
1752 GString *style = g_string_new("");
1753 struct purple_parse_tag *pt;
1754 while (*p && *p != '>') {
1755 if (!g_ascii_strncasecmp(p, "back=", 5)) {
1756 const char *q = p + 5;
1757 GString *color = g_string_new("");
1758 ESCAPE(q, color);
1759 g_string_append_printf(style, "background: %s; ", color->str);
1760 g_string_free(color, TRUE);
1761 p = q;
1762 } else if (!g_ascii_strncasecmp(p, "color=", 6)) {
1763 const char *q = p + 6;
1764 GString *color = g_string_new("");
1765 ESCAPE(q, color);
1766 g_string_append_printf(style, "color: %s; ", color->str);
1767 g_string_free(color, TRUE);
1768 p = q;
1769 } else if (!g_ascii_strncasecmp(p, "face=", 5)) {
1770 const char *q = p + 5;
1771 GString *face = g_string_new("");
1772 ESCAPE(q, face);
1773 g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str));
1774 g_string_free(face, TRUE);
1775 p = q;
1776 } else if (!g_ascii_strncasecmp(p, "size=", 5)) {
1777 const char *q = p + 5;
1778 int sz;
1779 const char *size = "medium";
1780 CHECK_QUOTE(q);
1781 sz = atoi(q);
1782 switch (sz)
1784 case 1:
1785 size = "xx-small";
1786 break;
1787 case 2:
1788 size = "small";
1789 break;
1790 case 3:
1791 size = "medium";
1792 break;
1793 case 4:
1794 size = "large";
1795 break;
1796 case 5:
1797 size = "x-large";
1798 break;
1799 case 6:
1800 case 7:
1801 size = "xx-large";
1802 break;
1803 default:
1804 break;
1806 g_string_append_printf(style, "font-size: %s; ", size);
1807 p = q;
1808 } else {
1809 p++;
1812 if ((c = strchr(p, '>')) != NULL)
1813 c++;
1814 else
1815 c = p;
1816 pt = g_new0(struct purple_parse_tag, 1);
1817 pt->src_tag = "font";
1818 pt->dest_tag = "span";
1819 tags = g_list_prepend(tags, pt);
1820 if(style->len && xhtml)
1821 g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str));
1822 else
1823 pt->ignore = TRUE;
1824 g_string_free(style, TRUE);
1825 continue;
1827 #undef ESCAPE
1828 if (!g_ascii_strncasecmp(c, "<body ", 6)) {
1829 const char *p = c + 6;
1830 gboolean did_something = FALSE;
1831 while (*p && *p != '>') {
1832 if (!g_ascii_strncasecmp(p, "bgcolor=", 8)) {
1833 const char *q = p + 8;
1834 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1);
1835 GString *color = g_string_new("");
1836 CHECK_QUOTE(q);
1837 while (VALID_CHAR(q)) {
1838 color = g_string_append_c(color, *q);
1839 q++;
1841 if (xhtml)
1842 g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str));
1843 g_string_free(color, TRUE);
1844 if ((c = strchr(p, '>')) != NULL)
1845 c++;
1846 else
1847 c = p;
1848 pt->src_tag = "body";
1849 pt->dest_tag = "span";
1850 tags = g_list_prepend(tags, pt);
1851 did_something = TRUE;
1852 break;
1854 p++;
1856 if (did_something) continue;
1858 /* this has to come after the special case for bgcolor */
1859 ALLOW_TAG("body");
1860 if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) {
1861 char *p = strstr(c + strlen("<!--"), "-->");
1862 if(p) {
1863 if(xhtml)
1864 xhtml = g_string_append(xhtml, "<!--");
1865 c += strlen("<!--");
1866 continue;
1870 if(xhtml)
1871 xhtml = g_string_append(xhtml, "&lt;");
1872 if(plain)
1873 plain = g_string_append_c(plain, '<');
1874 c++;
1876 } else if(*c == '&') {
1877 char buf[7];
1878 const char *pln;
1879 int len;
1881 if ((pln = purple_markup_unescape_entity(c, &len)) == NULL) {
1882 len = 1;
1883 g_snprintf(buf, sizeof(buf), "%c", *c);
1884 pln = buf;
1886 if(xhtml)
1887 xhtml = g_string_append_len(xhtml, c, len);
1888 if(plain)
1889 plain = g_string_append(plain, pln);
1890 if(cdata)
1891 cdata = g_string_append_len(cdata, c, len);
1892 c += len;
1893 } else {
1894 if(xhtml)
1895 xhtml = g_string_append_c(xhtml, *c);
1896 if(plain)
1897 plain = g_string_append_c(plain, *c);
1898 if(cdata)
1899 cdata = g_string_append_c(cdata, *c);
1900 c++;
1903 if(xhtml) {
1904 for (tag = tags; tag ; tag = tag->next) {
1905 struct purple_parse_tag *pt = tag->data;
1906 if(!pt->ignore)
1907 g_string_append_printf(xhtml, "</%s>", pt->dest_tag);
1910 g_list_free(tags);
1911 if(xhtml_out)
1912 *xhtml_out = g_string_free(xhtml, FALSE);
1913 if(plain_out)
1914 *plain_out = g_string_free(plain, FALSE);
1915 if(url)
1916 g_string_free(url, TRUE);
1917 if (cdata)
1918 g_string_free(cdata, TRUE);
1919 #undef CHECK_QUOTE
1920 #undef VALID_CHAR
1923 /* The following are probably reasonable changes:
1924 * - \n should be converted to a normal space
1925 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
1926 * - We want to turn </td>#whitespace<td> sequences into a single tab
1927 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
1928 * - <script>...</script> and <style>...</style> should be completely removed
1931 char *
1932 purple_markup_strip_html(const char *str)
1934 int i, j, k, entlen;
1935 gboolean visible = TRUE;
1936 gboolean closing_td_p = FALSE;
1937 gchar *str2;
1938 const gchar *cdata_close_tag = NULL, *ent;
1939 gchar *href = NULL;
1940 int href_st = 0;
1942 if(!str)
1943 return NULL;
1945 str2 = g_strdup(str);
1947 for (i = 0, j = 0; str2[i]; i++)
1949 if (str2[i] == '<')
1951 if (cdata_close_tag)
1953 /* Note: Don't even assume any other tag is a tag in CDATA */
1954 if (g_ascii_strncasecmp(str2 + i, cdata_close_tag,
1955 strlen(cdata_close_tag)) == 0)
1957 i += strlen(cdata_close_tag) - 1;
1958 cdata_close_tag = NULL;
1960 continue;
1962 else if (g_ascii_strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p)
1964 str2[j++] = '\t';
1965 visible = TRUE;
1967 else if (g_ascii_strncasecmp(str2 + i, "</td>", 5) == 0)
1969 closing_td_p = TRUE;
1970 visible = FALSE;
1972 else
1974 closing_td_p = FALSE;
1975 visible = TRUE;
1978 k = i + 1;
1980 if(g_ascii_isspace(str2[k]))
1981 visible = TRUE;
1982 else if (str2[k])
1984 /* Scan until we end the tag either implicitly (closed start
1985 * tag) or explicitly, using a sloppy method (i.e., < or >
1986 * inside quoted attributes will screw us up)
1988 while (str2[k] && str2[k] != '<' && str2[k] != '>')
1990 k++;
1993 /* If we've got an <a> tag with an href, save the address
1994 * to print later. */
1995 if (g_ascii_strncasecmp(str2 + i, "<a", 2) == 0 &&
1996 g_ascii_isspace(str2[i+2]))
1998 int st; /* start of href, inclusive [ */
1999 int end; /* end of href, exclusive ) */
2000 char delim = ' ';
2001 /* Find start of href */
2002 for (st = i + 3; st < k; st++)
2004 if (g_ascii_strncasecmp(str2+st, "href=", 5) == 0)
2006 st += 5;
2007 if (str2[st] == '"' || str2[st] == '\'')
2009 delim = str2[st];
2010 st++;
2012 break;
2015 /* find end of address */
2016 for (end = st; end < k && str2[end] != delim; end++)
2018 /* All the work is done in the loop construct above. */
2021 /* If there's an address, save it. If there was
2022 * already one saved, kill it. */
2023 if (st < k)
2025 char *tmp;
2026 g_free(href);
2027 tmp = g_strndup(str2 + st, end - st);
2028 href = purple_unescape_html(tmp);
2029 g_free(tmp);
2030 href_st = j;
2034 /* Replace </a> with an ascii representation of the
2035 * address the link was pointing to. */
2036 else if (href != NULL && g_ascii_strncasecmp(str2 + i, "</a>", 4) == 0)
2038 size_t hrlen = strlen(href);
2040 /* Only insert the href if it's different from the CDATA. */
2041 if ((hrlen != (gsize)(j - href_st) ||
2042 strncmp(str2 + href_st, href, hrlen)) &&
2043 (hrlen != (gsize)(j - href_st + 7) || /* 7 == strlen("http://") */
2044 strncmp(str2 + href_st, href + 7, hrlen - 7)))
2046 str2[j++] = ' ';
2047 str2[j++] = '(';
2048 memmove(str2 + j, href, hrlen);
2049 j += hrlen;
2050 str2[j++] = ')';
2051 g_free(href);
2052 href = NULL;
2056 /* Check for tags which should be mapped to newline (but ignore some of
2057 * the tags at the beginning of the text) */
2058 else if ((j && (g_ascii_strncasecmp(str2 + i, "<p>", 3) == 0
2059 || g_ascii_strncasecmp(str2 + i, "<tr", 3) == 0
2060 || g_ascii_strncasecmp(str2 + i, "<hr", 3) == 0
2061 || g_ascii_strncasecmp(str2 + i, "<li", 3) == 0
2062 || g_ascii_strncasecmp(str2 + i, "<div", 4) == 0))
2063 || g_ascii_strncasecmp(str2 + i, "<br", 3) == 0
2064 || g_ascii_strncasecmp(str2 + i, "</table>", 8) == 0)
2066 str2[j++] = '\n';
2068 /* Check for tags which begin CDATA and need to be closed */
2069 else if (g_ascii_strncasecmp(str2 + i, "<script", 7) == 0)
2071 cdata_close_tag = "</script>";
2073 else if (g_ascii_strncasecmp(str2 + i, "<style", 6) == 0)
2075 cdata_close_tag = "</style>";
2077 /* Update the index and continue checking after the tag */
2078 i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k;
2079 continue;
2082 else if (cdata_close_tag)
2084 continue;
2086 else if (!g_ascii_isspace(str2[i]))
2088 visible = TRUE;
2091 if (str2[i] == '&' && (ent = purple_markup_unescape_entity(str2 + i, &entlen)) != NULL)
2093 while (*ent)
2094 str2[j++] = *ent++;
2095 i += entlen - 1;
2096 continue;
2099 if (visible)
2100 str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i];
2103 g_free(href);
2105 str2[j] = '\0';
2107 return str2;
2110 static gboolean
2111 badchar(char c)
2113 switch (c) {
2114 case ' ':
2115 case ',':
2116 case '\0':
2117 case '\n':
2118 case '\r':
2119 case '<':
2120 case '>':
2121 case '"':
2122 return TRUE;
2123 default:
2124 return FALSE;
2128 static gboolean
2129 badentity(const char *c)
2131 if (!g_ascii_strncasecmp(c, "&lt;", 4) ||
2132 !g_ascii_strncasecmp(c, "&gt;", 4) ||
2133 !g_ascii_strncasecmp(c, "&quot;", 6)) {
2134 return TRUE;
2136 return FALSE;
2139 static const char *
2140 process_link(GString *ret,
2141 const char *start, const char *c,
2142 int matchlen,
2143 const char *urlprefix,
2144 int inside_paren)
2146 char *url_buf, *tmpurlbuf;
2147 const char *t;
2149 for (t = c;; t++) {
2150 if (!badchar(*t) && !badentity(t))
2151 continue;
2153 if (t - c == matchlen)
2154 break;
2156 if (*t == ',' && *(t + 1) != ' ') {
2157 continue;
2160 if (t > start && *(t - 1) == '.')
2161 t--;
2162 if (t > start && *(t - 1) == ')' && inside_paren > 0)
2163 t--;
2165 url_buf = g_strndup(c, t - c);
2166 tmpurlbuf = purple_unescape_html(url_buf);
2167 g_string_append_printf(ret, "<A HREF=\"%s%s\">%s</A>",
2168 urlprefix,
2169 tmpurlbuf, url_buf);
2170 g_free(tmpurlbuf);
2171 g_free(url_buf);
2172 return t;
2175 return c;
2178 char *
2179 purple_markup_linkify(const char *text)
2181 const char *c, *t, *q = NULL;
2182 char *tmpurlbuf, *url_buf;
2183 gunichar g;
2184 gboolean inside_html = FALSE;
2185 int inside_paren = 0;
2186 GString *ret;
2188 if (text == NULL)
2189 return NULL;
2191 ret = g_string_new("");
2193 c = text;
2194 while (*c) {
2196 if(*c == '(' && !inside_html) {
2197 inside_paren++;
2198 ret = g_string_append_c(ret, *c);
2199 c++;
2202 if(inside_html) {
2203 if(*c == '>') {
2204 inside_html = FALSE;
2205 } else if(!q && (*c == '\"' || *c == '\'')) {
2206 q = c;
2207 } else if(q) {
2208 if(*c == *q)
2209 q = NULL;
2211 } else if(*c == '<') {
2212 inside_html = TRUE;
2213 if (!g_ascii_strncasecmp(c, "<A", 2)) {
2214 while (1) {
2215 if (!g_ascii_strncasecmp(c, "/A>", 3)) {
2216 inside_html = FALSE;
2217 break;
2219 ret = g_string_append_c(ret, *c);
2220 c++;
2221 if (!(*c))
2222 break;
2225 } else if (!g_ascii_strncasecmp(c, "http://", 7)) {
2226 c = process_link(ret, text, c, 7, "", inside_paren);
2227 } else if (!g_ascii_strncasecmp(c, "https://", 8)) {
2228 c = process_link(ret, text, c, 8, "", inside_paren);
2229 } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) {
2230 c = process_link(ret, text, c, 6, "", inside_paren);
2231 } else if (!g_ascii_strncasecmp(c, "sftp://", 7)) {
2232 c = process_link(ret, text, c, 7, "", inside_paren);
2233 } else if (!g_ascii_strncasecmp(c, "file://", 7)) {
2234 c = process_link(ret, text, c, 7, "", inside_paren);
2235 } else if (!g_ascii_strncasecmp(c, "www.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2236 c = process_link(ret, text, c, 4, "http://", inside_paren);
2237 } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) {
2238 c = process_link(ret, text, c, 4, "ftp://", inside_paren);
2239 } else if (!g_ascii_strncasecmp(c, "xmpp:", 5) && (c == text || badchar(c[-1]) || badentity(c-1))) {
2240 c = process_link(ret, text, c, 5, "", inside_paren);
2241 } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) {
2242 t = c;
2243 while (1) {
2244 if (badchar(*t) || badentity(t)) {
2245 char *d;
2246 if (t - c == 7) {
2247 break;
2249 if (t > text && *(t - 1) == '.')
2250 t--;
2251 if ((d = strstr(c + 7, "?")) != NULL && d < t)
2252 url_buf = g_strndup(c + 7, d - c - 7);
2253 else
2254 url_buf = g_strndup(c + 7, t - c - 7);
2255 if (!purple_email_is_valid(url_buf)) {
2256 g_free(url_buf);
2257 break;
2259 g_free(url_buf);
2260 url_buf = g_strndup(c, t - c);
2261 tmpurlbuf = purple_unescape_html(url_buf);
2262 g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>",
2263 tmpurlbuf, url_buf);
2264 g_free(url_buf);
2265 g_free(tmpurlbuf);
2266 c = t;
2267 break;
2269 t++;
2271 } else if (c != text && (*c == '@')) {
2272 int flag;
2273 GString *gurl_buf = NULL;
2274 const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2276 if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1)))
2277 flag = 0;
2278 else {
2279 flag = 1;
2280 gurl_buf = g_string_new("");
2283 t = c;
2284 while (flag) {
2285 /* iterate backwards grabbing the local part of an email address */
2286 g = g_utf8_get_char(t);
2287 if (badchar(*t) || (g >= 127) || (*t == '(') ||
2288 ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "&lt;", 4) ||
2289 !g_ascii_strncasecmp(t - 3, "&gt;", 4))) ||
2290 (t > (text+4) && (!g_ascii_strncasecmp(t - 5, "&quot;", 6)))))) {
2291 /* local part will already be part of ret, strip it out */
2292 ret = g_string_truncate(ret, ret->len - (c - t));
2293 ret = g_string_append_unichar(ret, g);
2294 break;
2295 } else {
2296 g_string_prepend_unichar(gurl_buf, g);
2297 t = g_utf8_find_prev_char(text, t);
2298 if (t < text) {
2299 ret = g_string_assign(ret, "");
2300 break;
2305 t = g_utf8_find_next_char(c, NULL);
2307 while (flag) {
2308 /* iterate forwards grabbing the domain part of an email address */
2309 g = g_utf8_get_char(t);
2310 if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) {
2311 char *d;
2313 url_buf = g_string_free(gurl_buf, FALSE);
2314 gurl_buf = NULL;
2316 /* strip off trailing periods */
2317 if (*url_buf) {
2318 for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--)
2319 *d = '\0';
2322 tmpurlbuf = purple_unescape_html(url_buf);
2323 if (purple_email_is_valid(tmpurlbuf)) {
2324 g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
2325 tmpurlbuf, url_buf);
2326 } else {
2327 g_string_append(ret, url_buf);
2329 g_free(url_buf);
2330 g_free(tmpurlbuf);
2331 c = t;
2333 break;
2334 } else {
2335 g_string_append_unichar(gurl_buf, g);
2336 t = g_utf8_find_next_char(t, NULL);
2340 if (gurl_buf) {
2341 g_string_free(gurl_buf, TRUE);
2345 if(*c == ')' && !inside_html) {
2346 inside_paren--;
2347 ret = g_string_append_c(ret, *c);
2348 c++;
2351 if (*c == 0)
2352 break;
2354 ret = g_string_append_c(ret, *c);
2355 c++;
2358 return g_string_free(ret, FALSE);
2361 char *purple_unescape_text(const char *in)
2363 GString *ret;
2364 const char *c = in;
2366 if (in == NULL)
2367 return NULL;
2369 ret = g_string_new("");
2370 while (*c) {
2371 int len;
2372 const char *ent;
2374 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2375 g_string_append(ret, ent);
2376 c += len;
2377 } else {
2378 g_string_append_c(ret, *c);
2379 c++;
2383 return g_string_free(ret, FALSE);
2386 char *purple_unescape_html(const char *html)
2388 GString *ret;
2389 const char *c = html;
2391 if (html == NULL)
2392 return NULL;
2394 ret = g_string_new("");
2395 while (*c) {
2396 int len;
2397 const char *ent;
2399 if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) {
2400 g_string_append(ret, ent);
2401 c += len;
2402 } else if (!strncmp(c, "<br>", 4)) {
2403 g_string_append_c(ret, '\n');
2404 c += 4;
2405 } else {
2406 g_string_append_c(ret, *c);
2407 c++;
2411 return g_string_free(ret, FALSE);
2414 char *
2415 purple_markup_slice(const char *str, guint x, guint y)
2417 GString *ret;
2418 GQueue *q;
2419 guint z = 0;
2420 gboolean appended = FALSE;
2421 gunichar c;
2422 char *tag;
2424 g_return_val_if_fail(str != NULL, NULL);
2425 g_return_val_if_fail(x <= y, NULL);
2427 if (x == y)
2428 return g_strdup("");
2430 ret = g_string_new("");
2431 q = g_queue_new();
2433 while (*str && (z < y)) {
2434 c = g_utf8_get_char(str);
2436 if (c == '<') {
2437 char *end = strchr(str, '>');
2439 if (!end) {
2440 g_string_free(ret, TRUE);
2441 while ((tag = g_queue_pop_head(q)))
2442 g_free(tag);
2443 g_queue_free(q);
2444 return NULL;
2447 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
2448 z += strlen("[Image]");
2449 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
2450 z += 1;
2451 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
2452 z += strlen("\n---\n");
2453 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
2454 /* pop stack */
2455 char *tmp;
2457 tmp = g_queue_pop_head(q);
2458 g_free(tmp);
2459 /* z += 0; */
2460 } else {
2461 /* push it unto the stack */
2462 char *tmp;
2464 tmp = g_strndup(str, end - str + 1);
2465 g_queue_push_head(q, tmp);
2466 /* z += 0; */
2469 if (z >= x) {
2470 g_string_append_len(ret, str, end - str + 1);
2473 str = end;
2474 } else if (c == '&') {
2475 char *end = strchr(str, ';');
2476 if (!end) {
2477 g_string_free(ret, TRUE);
2478 while ((tag = g_queue_pop_head(q)))
2479 g_free(tag);
2480 g_queue_free(q);
2482 return NULL;
2485 if (z >= x)
2486 g_string_append_len(ret, str, end - str + 1);
2488 z++;
2489 str = end;
2490 } else {
2491 if (z == x && z > 0 && !appended) {
2492 GList *l = q->tail;
2494 while (l) {
2495 tag = l->data;
2496 g_string_append(ret, tag);
2497 l = l->prev;
2499 appended = TRUE;
2502 if (z >= x)
2503 g_string_append_unichar(ret, c);
2504 z++;
2507 str = g_utf8_next_char(str);
2510 while ((tag = g_queue_pop_head(q))) {
2511 char *name;
2513 name = purple_markup_get_tag_name(tag);
2514 g_string_append_printf(ret, "</%s>", name);
2515 g_free(name);
2516 g_free(tag);
2519 g_queue_free(q);
2520 return g_string_free(ret, FALSE);
2523 char *
2524 purple_markup_get_tag_name(const char *tag)
2526 int i;
2527 g_return_val_if_fail(tag != NULL, NULL);
2528 g_return_val_if_fail(*tag == '<', NULL);
2530 for (i = 1; tag[i]; i++)
2531 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
2532 break;
2534 return g_strndup(tag+1, i-1);
2537 /**************************************************************************
2538 * Path/Filename Functions
2539 **************************************************************************/
2540 const char *
2541 purple_home_dir(void)
2543 #ifndef _WIN32
2544 return g_get_home_dir();
2545 #else
2546 return wpurple_home_dir();
2547 #endif
2550 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2551 const char *
2552 purple_user_dir(void)
2554 if (custom_user_dir != NULL)
2555 return custom_user_dir;
2556 else if (!user_dir)
2557 user_dir = g_build_filename(purple_home_dir(), ".purple", NULL);
2559 return user_dir;
2562 static const gchar *
2563 purple_xdg_dir(gchar **xdg_dir, const gchar *xdg_base_dir, const gchar *xdg_type)
2565 if (!*xdg_dir) {
2566 if (!custom_user_dir) {
2567 *xdg_dir = g_build_filename(xdg_base_dir, "purple", NULL);
2568 } else {
2569 *xdg_dir = g_build_filename(custom_user_dir, xdg_type, NULL);
2573 return *xdg_dir;
2576 const gchar *
2577 purple_cache_dir(void)
2579 return purple_xdg_dir(&cache_dir, g_get_user_cache_dir(), "cache");
2582 const gchar *
2583 purple_config_dir(void)
2585 return purple_xdg_dir(&config_dir, g_get_user_config_dir(), "config");
2588 const gchar *
2589 purple_data_dir(void)
2591 return purple_xdg_dir(&data_dir, g_get_user_data_dir(), "data");
2594 gboolean
2595 purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path)
2597 gint mkdir_res;
2598 gchar *xdg_path;
2599 gboolean xdg_path_exists;
2601 /* Create destination directory */
2602 mkdir_res = purple_build_dir(purple_xdg_dir, S_IRWXU);
2603 if (mkdir_res == -1) {
2604 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
2605 purple_xdg_dir, g_strerror(errno));
2606 return FALSE;
2609 xdg_path = g_build_filename(purple_xdg_dir, path, NULL);
2610 xdg_path_exists = g_file_test(xdg_path, G_FILE_TEST_EXISTS);
2611 if (!xdg_path_exists) {
2612 gchar *old_path;
2613 gboolean old_path_exists;
2615 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2616 old_path = g_build_filename(purple_user_dir(), path, NULL);
2617 G_GNUC_END_IGNORE_DEPRECATIONS
2618 old_path_exists = g_file_test(old_path, G_FILE_TEST_EXISTS);
2619 if (old_path_exists) {
2620 int rename_res;
2622 rename_res = g_rename(old_path, xdg_path);
2623 if (rename_res == -1) {
2624 purple_debug_error("util", "Error renaming %s to %s; failed migration\n",
2625 old_path, xdg_path);
2626 g_free(old_path);
2627 g_free(xdg_path);
2629 return FALSE;
2633 g_free(old_path);
2636 g_free(xdg_path);
2638 return TRUE;
2641 void purple_util_set_user_dir(const char *dir)
2643 g_free(custom_user_dir);
2645 if (dir != NULL && *dir)
2646 custom_user_dir = g_strdup(dir);
2647 else
2648 custom_user_dir = NULL;
2651 int purple_build_dir(const char *path, int mode)
2653 return g_mkdir_with_parents(path, mode);
2656 static gboolean
2657 purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size)
2659 gchar *filename_full;
2660 gboolean ret = FALSE;
2662 g_return_val_if_fail(dir != NULL, FALSE);
2664 purple_debug_misc("util", "Writing file %s to directory %s",
2665 filename, dir);
2667 /* Ensure the directory exists */
2668 if (!g_file_test(dir, G_FILE_TEST_IS_DIR))
2670 if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
2672 purple_debug_error("util", "Error creating directory %s: %s\n",
2673 dir, g_strerror(errno));
2674 return FALSE;
2678 filename_full = g_build_filename(dir, filename, NULL);
2680 ret = purple_util_write_data_to_file_absolute(filename_full, data, size);
2682 g_free(filename_full);
2683 return ret;
2686 gboolean
2687 purple_util_write_data_to_file(const char *filename, const char *data, gssize size)
2689 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2690 const char *user_dir = purple_user_dir();
2691 G_GNUC_END_IGNORE_DEPRECATIONS
2692 gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size);
2694 return ret;
2697 gboolean
2698 purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size)
2700 const char *cache_dir = purple_cache_dir();
2701 gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size);
2703 return ret;
2706 gboolean
2707 purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size)
2709 const char *config_dir = purple_config_dir();
2710 gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size);
2712 return ret;
2715 gboolean
2716 purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size)
2718 const char *data_dir = purple_data_dir();
2719 gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size);
2721 return ret;
2724 gboolean
2725 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size)
2727 GFile *file;
2728 GError *err = NULL;
2730 g_return_val_if_fail(size >= -1, FALSE);
2732 if (size == -1) {
2733 size = strlen(data);
2736 file = g_file_new_for_path(filename_full);
2738 if (!g_file_replace_contents(file, data, size, NULL, FALSE,
2739 G_FILE_CREATE_PRIVATE, NULL, NULL, &err)) {
2740 purple_debug_error("util", "Error writing file: %s: %s\n",
2741 filename_full, err->message);
2742 g_clear_error(&err);
2743 g_object_unref(file);
2744 return FALSE;
2747 g_object_unref(file);
2748 return TRUE;
2751 PurpleXmlNode *
2752 purple_util_read_xml_from_file(const char *filename, const char *description)
2754 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2755 return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util");
2756 G_GNUC_END_IGNORE_DEPRECATIONS
2759 PurpleXmlNode *
2760 purple_util_read_xml_from_cache_file(const char *filename, const char *description)
2762 return purple_xmlnode_from_file(purple_cache_dir(), filename, description, "util");
2765 PurpleXmlNode *
2766 purple_util_read_xml_from_config_file(const char *filename, const char *description)
2768 return purple_xmlnode_from_file(purple_config_dir(), filename, description, "util");
2771 PurpleXmlNode *
2772 purple_util_read_xml_from_data_file(const char *filename, const char *description)
2774 return purple_xmlnode_from_file(purple_data_dir(), filename, description, "util");
2778 * Like mkstemp() but returns a file pointer, uses a pre-set template,
2779 * uses the semantics of tempnam() for the directory to use and allocates
2780 * the space for the filepath.
2782 * Caller is responsible for closing the file and removing it when done,
2783 * as well as freeing the space pointed-to by "path" with g_free().
2785 * Returns NULL on failure and cleans up after itself if so.
2787 static const char *purple_mkstemp_templ = {"purpleXXXXXX"};
2789 FILE *
2790 purple_mkstemp(char **fpath, gboolean binary)
2792 const gchar *tmpdir;
2793 int fd;
2794 FILE *fp = NULL;
2796 g_return_val_if_fail(fpath != NULL, NULL);
2798 if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) {
2799 if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) {
2800 fd = g_mkstemp(*fpath);
2801 if(fd == -1) {
2802 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2803 "Couldn't make \"%s\", error: %d\n",
2804 *fpath, errno);
2805 } else {
2806 if((fp = fdopen(fd, "r+")) == NULL) {
2807 close(fd);
2808 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2809 "Couldn't fdopen(), error: %d\n", errno);
2813 if(!fp) {
2814 g_free(*fpath);
2815 *fpath = NULL;
2818 } else {
2819 purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp",
2820 "g_get_tmp_dir() failed!\n");
2823 return fp;
2826 gboolean
2827 purple_program_is_valid(const char *program)
2829 GError *error = NULL;
2830 char **argv;
2831 gchar *progname;
2832 gboolean is_valid = FALSE;
2834 g_return_val_if_fail(program != NULL, FALSE);
2835 g_return_val_if_fail(*program != '\0', FALSE);
2837 if (!g_shell_parse_argv(program, NULL, &argv, &error)) {
2838 purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid",
2839 "Could not parse program '%s': %s\n",
2840 program, error->message);
2841 g_error_free(error);
2842 return FALSE;
2845 if (argv == NULL) {
2846 return FALSE;
2849 progname = g_find_program_in_path(argv[0]);
2850 is_valid = (progname != NULL);
2852 if(purple_debug_is_verbose())
2853 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program,
2854 is_valid ? "Valid" : "Invalid");
2856 g_strfreev(argv);
2857 g_free(progname);
2859 return is_valid;
2863 gboolean
2864 purple_running_gnome(void)
2866 #ifndef _WIN32
2867 gchar *tmp = g_find_program_in_path("gvfs-open");
2869 if (tmp == NULL) {
2870 tmp = g_find_program_in_path("gnome-open");
2872 if (tmp == NULL) {
2873 return FALSE;
2877 g_free(tmp);
2879 tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID");
2881 return ((tmp != NULL) && (*tmp != '\0'));
2882 #else
2883 return FALSE;
2884 #endif
2887 gboolean
2888 purple_running_kde(void)
2890 #ifndef _WIN32
2891 gchar *tmp = g_find_program_in_path("kfmclient");
2892 const char *session;
2894 if (tmp == NULL)
2895 return FALSE;
2896 g_free(tmp);
2898 session = g_getenv("KDE_FULL_SESSION");
2899 if (purple_strequal(session, "true"))
2900 return TRUE;
2902 /* If you run Purple from Konsole under !KDE, this will provide a
2903 * a false positive. Since we do the GNOME checks first, this is
2904 * only a problem if you're running something !(KDE || GNOME) and
2905 * you run Purple from Konsole. This really shouldn't be a problem. */
2906 return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL);
2907 #else
2908 return FALSE;
2909 #endif
2912 gboolean
2913 purple_running_osx(void)
2915 #if defined(__APPLE__)
2916 return TRUE;
2917 #else
2918 return FALSE;
2919 #endif
2922 typedef union purple_sockaddr {
2923 struct sockaddr sa;
2924 struct sockaddr_in sa_in;
2925 #if defined(AF_INET6)
2926 struct sockaddr_in6 sa_in6;
2927 #endif
2928 struct sockaddr_storage sa_stor;
2929 } PurpleSockaddr;
2931 char *
2932 purple_fd_get_ip(int fd)
2934 PurpleSockaddr addr;
2935 socklen_t namelen = sizeof(addr);
2936 int family;
2938 g_return_val_if_fail(fd != 0, NULL);
2940 if (getsockname(fd, &(addr.sa), &namelen))
2941 return NULL;
2943 family = addr.sa.sa_family;
2945 if (family == AF_INET) {
2946 return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
2948 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
2949 else if (family == AF_INET6) {
2950 char host[INET6_ADDRSTRLEN];
2951 const char *tmp;
2953 tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
2954 return g_strdup(tmp);
2956 #endif
2958 return NULL;
2962 purple_socket_get_family(int fd)
2964 PurpleSockaddr addr;
2965 socklen_t len = sizeof(addr);
2967 g_return_val_if_fail(fd >= 0, -1);
2969 if (getsockname(fd, &(addr.sa), &len))
2970 return -1;
2972 return addr.sa.sa_family;
2975 gboolean
2976 purple_socket_speaks_ipv4(int fd)
2978 int family;
2980 g_return_val_if_fail(fd >= 0, FALSE);
2982 family = purple_socket_get_family(fd);
2984 switch (family) {
2985 case AF_INET:
2986 return TRUE;
2987 #if defined(IPV6_V6ONLY)
2988 case AF_INET6:
2990 int val = 0;
2991 socklen_t len = sizeof(val);
2993 if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0)
2994 return FALSE;
2995 return !val;
2997 #endif
2998 default:
2999 return FALSE;
3003 /**************************************************************************
3004 * String Functions
3005 **************************************************************************/
3006 const char *
3007 purple_normalize(PurpleAccount *account, const char *str)
3009 const char *ret = NULL;
3010 static char buf[BUF_LEN];
3012 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3013 g_return_val_if_fail(str != NULL, "");
3015 if (account != NULL)
3017 PurpleProtocol *protocol =
3018 purple_protocols_find(purple_account_get_protocol_id(account));
3020 if (protocol != NULL)
3021 ret = purple_protocol_client_iface_normalize(protocol, account, str);
3024 if (ret == NULL)
3026 char *tmp;
3028 tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT);
3029 g_snprintf(buf, sizeof(buf), "%s", tmp);
3030 g_free(tmp);
3032 ret = buf;
3035 return ret;
3039 * You probably don't want to call this directly, it is
3040 * mainly for use as a protocol callback function. See the
3041 * comments in util.h.
3043 const char *
3044 purple_normalize_nocase(const PurpleAccount *account, const char *str)
3046 static char buf[BUF_LEN];
3047 char *tmp1, *tmp2;
3049 g_return_val_if_fail(str != NULL, NULL);
3051 tmp1 = g_utf8_strdown(str, -1);
3052 tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT);
3053 g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : "");
3054 g_free(tmp2);
3055 g_free(tmp1);
3057 return buf;
3060 gboolean
3061 purple_validate(const PurpleProtocol *protocol, const char *str)
3063 const char *normalized;
3065 g_return_val_if_fail(protocol != NULL, FALSE);
3066 g_return_val_if_fail(str != NULL, FALSE);
3068 if (str[0] == '\0')
3069 return FALSE;
3071 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, normalize))
3072 return TRUE;
3074 normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol),
3075 NULL, str);
3077 return (NULL != normalized);
3080 gchar *
3081 purple_strdup_withhtml(const gchar *src)
3083 gulong destsize, i, j;
3084 gchar *dest;
3086 g_return_val_if_fail(src != NULL, NULL);
3088 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3089 destsize = 1;
3090 for (i = 0; src[i] != '\0'; i++)
3092 if (src[i] == '\n')
3093 destsize += 4;
3094 else if (src[i] != '\r')
3095 destsize++;
3098 dest = g_malloc(destsize);
3100 /* Copy stuff, ignoring \r's, because they are dumb */
3101 for (i = 0, j = 0; src[i] != '\0'; i++) {
3102 if (src[i] == '\n') {
3103 strcpy(&dest[j], "<BR>");
3104 j += 4;
3105 } else if (src[i] != '\r')
3106 dest[j++] = src[i];
3109 dest[destsize-1] = '\0';
3111 return dest;
3114 gboolean
3115 purple_str_has_prefix(const char *s, const char *p)
3117 return g_str_has_prefix(s, p);
3120 gboolean
3121 purple_str_has_caseprefix(const gchar *s, const gchar *p)
3123 g_return_val_if_fail(s, FALSE);
3124 g_return_val_if_fail(p, FALSE);
3126 return (g_ascii_strncasecmp(s, p, strlen(p)) == 0);
3129 gboolean
3130 purple_str_has_suffix(const char *s, const char *x)
3132 return g_str_has_suffix(s, x);
3135 char *
3136 purple_str_add_cr(const char *text)
3138 char *ret = NULL;
3139 int count = 0, j;
3140 guint i;
3142 g_return_val_if_fail(text != NULL, NULL);
3144 if (text[0] == '\n')
3145 count++;
3146 for (i = 1; i < strlen(text); i++)
3147 if (text[i] == '\n' && text[i - 1] != '\r')
3148 count++;
3150 if (count == 0)
3151 return g_strdup(text);
3153 ret = g_malloc0(strlen(text) + count + 1);
3155 i = 0; j = 0;
3156 if (text[i] == '\n')
3157 ret[j++] = '\r';
3158 ret[j++] = text[i++];
3159 for (; i < strlen(text); i++) {
3160 if (text[i] == '\n' && text[i - 1] != '\r')
3161 ret[j++] = '\r';
3162 ret[j++] = text[i];
3165 return ret;
3168 void
3169 purple_str_strip_char(char *text, char thechar)
3171 int i, j;
3173 g_return_if_fail(text != NULL);
3175 for (i = 0, j = 0; text[i]; i++)
3176 if (text[i] != thechar)
3177 text[j++] = text[i];
3179 text[j] = '\0';
3182 void
3183 purple_util_chrreplace(char *string, char delimiter,
3184 char replacement)
3186 int i = 0;
3188 g_return_if_fail(string != NULL);
3190 while (string[i] != '\0')
3192 if (string[i] == delimiter)
3193 string[i] = replacement;
3194 i++;
3198 gchar *
3199 purple_strreplace(const char *string, const char *delimiter,
3200 const char *replacement)
3202 gchar **split;
3203 gchar *ret;
3205 g_return_val_if_fail(string != NULL, NULL);
3206 g_return_val_if_fail(delimiter != NULL, NULL);
3207 g_return_val_if_fail(replacement != NULL, NULL);
3209 split = g_strsplit(string, delimiter, 0);
3210 ret = g_strjoinv(replacement, split);
3211 g_strfreev(split);
3213 return ret;
3216 gchar *
3217 purple_strcasereplace(const char *string, const char *delimiter,
3218 const char *replacement)
3220 gchar *ret;
3221 int length_del, length_rep, i, j;
3223 g_return_val_if_fail(string != NULL, NULL);
3224 g_return_val_if_fail(delimiter != NULL, NULL);
3225 g_return_val_if_fail(replacement != NULL, NULL);
3227 length_del = strlen(delimiter);
3228 length_rep = strlen(replacement);
3230 /* Count how many times the delimiter appears */
3231 i = 0; /* position in the source string */
3232 j = 0; /* number of occurrences of "delimiter" */
3233 while (string[i] != '\0') {
3234 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3235 i += length_del;
3236 j += length_rep;
3237 } else {
3238 i++;
3239 j++;
3243 ret = g_malloc(j+1);
3245 i = 0; /* position in the source string */
3246 j = 0; /* position in the destination string */
3247 while (string[i] != '\0') {
3248 if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) {
3249 strncpy(&ret[j], replacement, length_rep);
3250 i += length_del;
3251 j += length_rep;
3252 } else {
3253 ret[j] = string[i];
3254 i++;
3255 j++;
3259 ret[j] = '\0';
3261 return ret;
3264 /** TODO: Expose this when we can add API */
3265 static const char *
3266 purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen)
3268 const char *tmp, *ret;
3270 g_return_val_if_fail(haystack != NULL, NULL);
3271 g_return_val_if_fail(needle != NULL, NULL);
3273 if (hlen == -1)
3274 hlen = strlen(haystack);
3275 if (nlen == -1)
3276 nlen = strlen(needle);
3277 tmp = haystack,
3278 ret = NULL;
3280 g_return_val_if_fail(hlen > 0, NULL);
3281 g_return_val_if_fail(nlen > 0, NULL);
3283 while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) {
3284 if (!g_ascii_strncasecmp(needle, tmp, nlen))
3285 ret = tmp;
3286 else
3287 tmp++;
3290 return ret;
3293 const char *
3294 purple_strcasestr(const char *haystack, const char *needle)
3296 return purple_strcasestr_len(haystack, -1, needle, -1);
3299 char *
3300 purple_str_seconds_to_string(guint secs)
3302 char *ret = NULL;
3303 guint days, hrs, mins;
3305 if (secs < 60)
3307 return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs);
3310 days = secs / (60 * 60 * 24);
3311 secs = secs % (60 * 60 * 24);
3312 hrs = secs / (60 * 60);
3313 secs = secs % (60 * 60);
3314 mins = secs / 60;
3315 /* secs = secs % 60; */
3317 if (days > 0)
3319 ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days);
3322 if (hrs > 0)
3324 if (ret != NULL)
3326 char *tmp = g_strdup_printf(
3327 dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs),
3328 ret, hrs);
3329 g_free(ret);
3330 ret = tmp;
3332 else
3333 ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs);
3336 if (mins > 0)
3338 if (ret != NULL)
3340 char *tmp = g_strdup_printf(
3341 dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins),
3342 ret, mins);
3343 g_free(ret);
3344 ret = tmp;
3346 else
3347 ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins);
3350 return ret;
3354 char *
3355 purple_str_binary_to_ascii(const unsigned char *binary, guint len)
3357 GString *ret;
3358 guint i;
3360 g_return_val_if_fail(len > 0, NULL);
3362 ret = g_string_sized_new(len);
3364 for (i = 0; i < len; i++)
3365 if (binary[i] < 32 || binary[i] > 126)
3366 g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF);
3367 else if (binary[i] == '\\')
3368 g_string_append(ret, "\\\\");
3369 else
3370 g_string_append_c(ret, binary[i]);
3372 return g_string_free(ret, FALSE);
3375 size_t
3376 purple_utf16_size(const gunichar2 *str)
3378 /* UTF16 cannot contain two consequent NUL bytes starting at even
3379 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3380 * Chapter 2.
3383 size_t i = 0;
3385 g_return_val_if_fail(str != NULL, 0);
3387 while (str[i++]);
3389 return i * sizeof(gunichar2);
3392 void
3393 purple_str_wipe(gchar *str)
3395 if (str == NULL)
3396 return;
3397 memset(str, 0, strlen(str));
3398 g_free(str);
3401 void
3402 purple_utf16_wipe(gunichar2 *str)
3404 if (str == NULL)
3405 return;
3406 memset(str, 0, purple_utf16_size(str));
3407 g_free(str);
3410 /**************************************************************************
3411 * URI/URL Functions
3412 **************************************************************************/
3414 void purple_got_protocol_handler_uri(const char *uri)
3416 char proto[11];
3417 char delimiter;
3418 const char *tmp, *param_string;
3419 char *cmd;
3420 GHashTable *params = NULL;
3421 gsize len;
3422 if (!(tmp = strchr(uri, ':')) || tmp == uri) {
3423 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3424 return;
3427 len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri));
3429 strncpy(proto, uri, len);
3430 proto[len] = '\0';
3432 tmp++;
3434 if (purple_strequal(proto, "xmpp"))
3435 delimiter = ';';
3436 else
3437 delimiter = '&';
3439 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter);
3441 if ((param_string = strchr(tmp, '?'))) {
3442 const char *keyend = NULL, *pairstart;
3443 char *key, *value = NULL;
3445 cmd = g_strndup(tmp, (param_string - tmp));
3446 param_string++;
3448 params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
3449 pairstart = tmp = param_string;
3451 while (*tmp || *pairstart) {
3452 if (*tmp == delimiter || !(*tmp)) {
3453 /* If there is no explicit value */
3454 if (keyend == NULL) {
3455 keyend = tmp;
3457 /* without these brackets, clang won't
3458 * recognize tmp as a non-NULL
3461 if (keyend && keyend != pairstart) {
3462 char *p;
3463 key = g_strndup(pairstart, (keyend - pairstart));
3464 /* If there is an explicit value */
3465 if (keyend != tmp && keyend != (tmp - 1))
3466 value = g_strndup(keyend + 1, (tmp - keyend - 1));
3467 for (p = key; *p; ++p)
3468 *p = g_ascii_tolower(*p);
3469 g_hash_table_insert(params, key, value);
3471 keyend = value = NULL;
3472 pairstart = (*tmp) ? tmp + 1 : tmp;
3473 } else if (*tmp == '=')
3474 keyend = tmp;
3476 if (*tmp)
3477 tmp++;
3479 } else
3480 cmd = g_strdup(tmp);
3482 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params);
3484 g_free(cmd);
3485 if (params)
3486 g_hash_table_destroy(params);
3489 const char *
3490 purple_url_decode(const char *str)
3492 static char buf[BUF_LEN];
3493 guint i, j = 0;
3494 char *bum;
3495 char hex[3];
3497 g_return_val_if_fail(str != NULL, NULL);
3500 * XXX - This check could be removed and buf could be made
3501 * dynamically allocated, but this is easier.
3503 if (strlen(str) >= BUF_LEN)
3504 return NULL;
3506 for (i = 0; i < strlen(str); i++) {
3508 if (str[i] != '%')
3509 buf[j++] = str[i];
3510 else {
3511 strncpy(hex, str + ++i, 2);
3512 hex[2] = '\0';
3514 /* i is pointing to the start of the number */
3515 i++;
3518 * Now it's at the end and at the start of the for loop
3519 * will be at the next character.
3521 buf[j++] = strtol(hex, NULL, 16);
3525 buf[j] = '\0';
3527 if (!g_utf8_validate(buf, -1, (const char **)&bum))
3528 *bum = '\0';
3530 return buf;
3533 const char *
3534 purple_url_encode(const char *str)
3536 const char *iter;
3537 static char buf[BUF_LEN];
3538 char utf_char[6];
3539 guint i, j = 0;
3541 g_return_val_if_fail(str != NULL, NULL);
3542 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
3544 iter = str;
3545 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
3546 gunichar c = g_utf8_get_char(iter);
3547 /* If the character is an ASCII character and is alphanumeric
3548 * no need to escape */
3549 if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
3550 buf[j++] = c;
3551 } else {
3552 int bytes = g_unichar_to_utf8(c, utf_char);
3553 for (i = 0; (int)i < bytes; i++) {
3554 if (j > (BUF_LEN - 4))
3555 break;
3556 if (i >= sizeof(utf_char)) {
3557 g_warn_if_reached();
3558 break;
3560 sprintf(buf + j, "%%%02X", utf_char[i] & 0xff);
3561 j += 3;
3566 buf[j] = '\0';
3568 return buf;
3571 /* Originally lifted from
3572 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
3573 * ... and slightly modified to be a bit more rfc822 compliant
3574 * ... and modified a bit more to make domain checking rfc1035 compliant
3575 * with the exception permitted in rfc1101 for domains to start with digit
3576 * but not completely checking to avoid conflicts with IP addresses
3578 gboolean
3579 purple_email_is_valid(const char *address)
3581 const char *c, *domain;
3582 static char *rfc822_specials = "()<>@,;:\\\"[]";
3584 g_return_val_if_fail(address != NULL, FALSE);
3586 if (*address == '.') return FALSE;
3588 /* first we validate the name portion (name@domain) (rfc822)*/
3589 for (c = address; *c; c++) {
3590 if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
3591 while (*++c) {
3592 if (*c == '\\') {
3593 if (*c++ && *c < 127 && *c > 0 && *c != '\n' && *c != '\r') continue;
3594 else return FALSE;
3596 if (*c == '\"') break;
3597 if (*c < ' ' || *c >= 127) return FALSE;
3599 if (!*c++) return FALSE;
3600 if (*c == '@') break;
3601 if (*c != '.') return FALSE;
3602 continue;
3604 if (*c == '@') break;
3605 if (*c <= ' ' || *c >= 127) return FALSE;
3606 if (strchr(rfc822_specials, *c)) return FALSE;
3609 /* It's obviously not an email address if we didn't find an '@' above */
3610 if (*c == '\0') return FALSE;
3612 /* strictly we should return false if (*(c - 1) == '.') too, but I think
3613 * we should permit user.@domain type addresses - they do work :) */
3614 if (c == address) return FALSE;
3616 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
3617 if (!*(domain = ++c)) return FALSE;
3618 do {
3619 if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-'))
3620 return FALSE;
3621 if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE;
3622 if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') ||
3623 (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE;
3624 } while (*++c);
3626 if (*(c - 1) == '-') return FALSE;
3628 return ((c - domain) > 3 ? TRUE : FALSE);
3631 /* Stolen from gnome_uri_list_extract_uris */
3632 GList *
3633 purple_uri_list_extract_uris(const gchar *uri_list)
3635 const gchar *p, *q;
3636 gchar *retval;
3637 GList *result = NULL;
3639 g_return_val_if_fail (uri_list != NULL, NULL);
3641 p = uri_list;
3643 /* We don't actually try to validate the URI according to RFC
3644 * 2396, or even check for allowed characters - we just ignore
3645 * comments and trim whitespace off the ends. We also
3646 * allow LF delimination as well as the specified CRLF.
3648 while (p) {
3649 if (*p != '#') {
3650 while (isspace(*p))
3651 p++;
3653 q = p;
3654 while (*q && (*q != '\n') && (*q != '\r'))
3655 q++;
3657 if (q > p) {
3658 q--;
3659 while (q > p && isspace(*q))
3660 q--;
3662 retval = (gchar*)g_malloc (q - p + 2);
3663 strncpy (retval, p, q - p + 1);
3664 retval[q - p + 1] = '\0';
3666 result = g_list_prepend (result, retval);
3669 p = strchr (p, '\n');
3670 if (p)
3671 p++;
3674 return g_list_reverse (result);
3678 /* Stolen from gnome_uri_list_extract_filenames */
3679 GList *
3680 purple_uri_list_extract_filenames(const gchar *uri_list)
3682 GList *tmp_list, *node, *result;
3684 g_return_val_if_fail (uri_list != NULL, NULL);
3686 result = purple_uri_list_extract_uris(uri_list);
3688 tmp_list = result;
3689 while (tmp_list) {
3690 gchar *s = (gchar*)tmp_list->data;
3692 node = tmp_list;
3693 tmp_list = tmp_list->next;
3695 if (!strncmp (s, "file:", 5)) {
3696 node->data = g_filename_from_uri (s, NULL, NULL);
3697 /* not sure if this fallback is useful at all */
3698 if (!node->data) node->data = g_strdup (s+5);
3699 } else {
3700 result = g_list_delete_link(result, node);
3702 g_free (s);
3704 return result;
3707 char *
3708 purple_uri_escape_for_open(const char *unescaped)
3710 /* Replace some special characters like $ with their percent-encoded value.
3711 * This shouldn't be necessary because we shell-escape the entire arg before
3712 * exec'ing the browser, however, we had a report that a URL containing
3713 * $(xterm) was causing xterm to start on his system. This is obviously a
3714 * bug on his system, but it's pretty easy for us to protect against it. */
3715 return g_uri_escape_string(unescaped, "[]:;/%#,+?=&@", FALSE);
3718 /**************************************************************************
3719 * UTF8 String Functions
3720 **************************************************************************/
3721 gchar *
3722 purple_utf8_try_convert(const char *str)
3724 gsize converted;
3725 gchar *utf8;
3727 g_return_val_if_fail(str != NULL, NULL);
3729 if (g_utf8_validate(str, -1, NULL)) {
3730 return g_strdup(str);
3733 utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL);
3734 if (utf8 != NULL)
3735 return utf8;
3737 utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL);
3738 if ((utf8 != NULL) && (converted == strlen(str)))
3739 return utf8;
3741 g_free(utf8);
3743 return NULL;
3746 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
3747 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
3748 gchar *
3749 purple_utf8_salvage(const char *str)
3751 GString *workstr;
3752 const char *end;
3754 g_return_val_if_fail(str != NULL, NULL);
3756 workstr = g_string_sized_new(strlen(str));
3758 do {
3759 (void)g_utf8_validate(str, -1, &end);
3760 workstr = g_string_append_len(workstr, str, end - str);
3761 str = end;
3762 if (*str == '\0')
3763 break;
3764 do {
3765 workstr = g_string_append_c(workstr, '?');
3766 str++;
3767 } while (!utf8_first(*str));
3768 } while (*str != '\0');
3770 return g_string_free(workstr, FALSE);
3773 gchar *
3774 purple_utf8_strip_unprintables(const gchar *str)
3776 gchar *workstr, *iter;
3777 const gchar *bad;
3779 if (str == NULL)
3780 /* Act like g_strdup */
3781 return NULL;
3783 if (!g_utf8_validate(str, -1, &bad)) {
3784 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
3785 "first bad character was %02x (%c)\n",
3786 str, *bad, *bad);
3787 g_return_val_if_reached(NULL);
3790 workstr = iter = g_new(gchar, strlen(str) + 1);
3791 while (*str) {
3792 gunichar ch = g_utf8_get_char(str);
3793 gchar *next = g_utf8_next_char(str);
3795 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
3796 * [#x10000-#x10FFFF]
3798 if ((ch == '\t' || ch == '\n' || ch == '\r') ||
3799 (ch >= 0x20 && ch <= 0xD7FF) ||
3800 (ch >= 0xE000 && ch <= 0xFFFD) ||
3801 (ch >= 0x10000 && ch <= 0x10FFFF)) {
3802 memcpy(iter, str, next - str);
3803 iter += (next - str);
3806 str = next;
3809 /* nul-terminate the new string */
3810 *iter = '\0';
3812 return workstr;
3816 * This function is copied from g_strerror() but changed to use
3817 * gai_strerror().
3819 const gchar *
3820 purple_gai_strerror(gint errnum)
3822 static GPrivate msg_private = G_PRIVATE_INIT(g_free);
3823 char *msg;
3824 int saved_errno = errno;
3826 const char *msg_locale;
3828 msg_locale = gai_strerror(errnum);
3829 if (g_get_charset(NULL))
3831 /* This string is already UTF-8--great! */
3832 errno = saved_errno;
3833 return msg_locale;
3835 else
3837 gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
3838 if (msg_utf8)
3840 /* Stick in the quark table so that we can return a static result */
3841 GQuark msg_quark = g_quark_from_string(msg_utf8);
3842 g_free(msg_utf8);
3844 msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
3845 errno = saved_errno;
3846 return msg_utf8;
3850 msg = g_private_get(&msg_private);
3852 if (!msg)
3854 msg = g_new(gchar, 64);
3855 g_private_set(&msg_private, msg);
3858 sprintf(msg, "unknown error (%d)", errnum);
3860 errno = saved_errno;
3861 return msg;
3864 char *
3865 purple_utf8_ncr_encode(const char *str)
3867 GString *out;
3869 g_return_val_if_fail(str != NULL, NULL);
3870 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
3872 out = g_string_new("");
3874 for(; *str; str = g_utf8_next_char(str)) {
3875 gunichar wc = g_utf8_get_char(str);
3877 /* super simple check. hopefully not too wrong. */
3878 if(wc >= 0x80) {
3879 g_string_append_printf(out, "&#%u;", (guint32) wc);
3880 } else {
3881 g_string_append_unichar(out, wc);
3885 return g_string_free(out, FALSE);
3889 char *
3890 purple_utf8_ncr_decode(const char *str)
3892 GString *out;
3893 char *buf, *b;
3895 g_return_val_if_fail(str != NULL, NULL);
3896 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
3898 buf = (char *) str;
3899 out = g_string_new("");
3901 while( (b = strstr(buf, "&#")) ) {
3902 gunichar wc;
3903 int base = 0;
3905 /* append everything leading up to the &# */
3906 g_string_append_len(out, buf, b-buf);
3908 b += 2; /* skip past the &# */
3910 /* strtoul will treat 0x prefix as hex, but not just x */
3911 if(*b == 'x' || *b == 'X') {
3912 base = 16;
3913 b++;
3916 /* advances buf to the end of the ncr segment */
3917 wc = (gunichar) strtoul(b, &buf, base);
3919 /* this mimics the previous impl of ncr_decode */
3920 if(*buf == ';') {
3921 g_string_append_unichar(out, wc);
3922 buf++;
3926 /* append whatever's left */
3927 g_string_append(out, buf);
3929 return g_string_free(out, FALSE);
3934 purple_utf8_strcasecmp(const char *a, const char *b)
3936 char *a_norm = NULL;
3937 char *b_norm = NULL;
3938 int ret = -1;
3940 if(!a && b)
3941 return -1;
3942 else if(!b && a)
3943 return 1;
3944 else if(!a && !b)
3945 return 0;
3947 if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL))
3949 purple_debug_error("purple_utf8_strcasecmp",
3950 "One or both parameters are invalid UTF8\n");
3951 return ret;
3954 a_norm = g_utf8_casefold(a, -1);
3955 b_norm = g_utf8_casefold(b, -1);
3956 ret = g_utf8_collate(a_norm, b_norm);
3957 g_free(a_norm);
3958 g_free(b_norm);
3960 return ret;
3963 /* previously conversation::find_nick() */
3964 gboolean
3965 purple_utf8_has_word(const char *haystack, const char *needle)
3967 char *hay, *pin, *p;
3968 const char *start, *prev_char;
3969 gunichar before, after;
3970 int n;
3971 gboolean ret = FALSE;
3973 start = hay = g_utf8_strdown(haystack, -1);
3975 pin = g_utf8_strdown(needle, -1);
3976 n = strlen(pin);
3978 while ((p = strstr(start, pin)) != NULL) {
3979 prev_char = g_utf8_find_prev_char(hay, p);
3980 before = -2;
3981 if (prev_char) {
3982 before = g_utf8_get_char(prev_char);
3984 after = g_utf8_get_char_validated(p + n, - 1);
3986 if ((p == hay ||
3987 /* The character before is a reasonable guess for a word boundary
3988 ("!g_unichar_isalnum()" is not a valid way to determine word
3989 boundaries, but it is the only reasonable thing to do here),
3990 and isn't the '&' from a "&amp;" or some such entity*/
3991 (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&'))
3992 && after != (gunichar)-2 && !g_unichar_isalnum(after)) {
3993 ret = TRUE;
3994 break;
3996 start = p + 1;
3999 g_free(pin);
4000 g_free(hay);
4002 return ret;
4005 gboolean purple_message_meify(char *message, gssize len)
4007 char *c;
4008 gboolean inside_html = FALSE;
4010 g_return_val_if_fail(message != NULL, FALSE);
4012 if(len == -1)
4013 len = strlen(message);
4015 for (c = message; *c; c++, len--) {
4016 if(inside_html) {
4017 if(*c == '>')
4018 inside_html = FALSE;
4019 } else {
4020 if(*c == '<')
4021 inside_html = TRUE;
4022 else
4023 break;
4027 if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) {
4028 memmove(c, c+4, len-3);
4029 return TRUE;
4032 return FALSE;
4035 char *purple_text_strip_mnemonic(const char *in)
4037 char *out;
4038 char *a;
4039 char *a0;
4040 const char *b;
4042 g_return_val_if_fail(in != NULL, NULL);
4044 out = g_malloc(strlen(in)+1);
4045 a = out;
4046 b = in;
4048 a0 = a; /* The last non-space char seen so far, or the first char */
4050 while(*b) {
4051 if(*b == '_') {
4052 if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') {
4053 /* Detected CJK style shortcut (Bug 875311) */
4054 a = a0; /* undo the left parenthesis */
4055 b += 3; /* and skip the whole mess */
4056 } else if(*(b+1) == '_') {
4057 *(a++) = '_';
4058 b += 2;
4059 a0 = a;
4060 } else {
4061 b++;
4063 /* We don't want to corrupt the middle of UTF-8 characters */
4064 } else if (!(*b & 0x80)) { /* other 1-byte char */
4065 if (*b != ' ')
4066 a0 = a;
4067 *(a++) = *(b++);
4068 } else {
4069 /* Multibyte utf8 char, don't look for _ inside these */
4070 int n = 0;
4071 int i;
4072 if ((*b & 0xe0) == 0xc0) {
4073 n = 2;
4074 } else if ((*b & 0xf0) == 0xe0) {
4075 n = 3;
4076 } else if ((*b & 0xf8) == 0xf0) {
4077 n = 4;
4078 } else if ((*b & 0xfc) == 0xf8) {
4079 n = 5;
4080 } else if ((*b & 0xfe) == 0xfc) {
4081 n = 6;
4082 } else { /* Illegal utf8 */
4083 n = 1;
4085 a0 = a; /* unless we want to delete CJK spaces too */
4086 for (i = 0; i < n && *b; i += 1) {
4087 *(a++) = *(b++);
4091 *a = '\0';
4093 return out;
4096 const char* purple_unescape_filename(const char *escaped) {
4097 return purple_url_decode(escaped);
4101 /* this is almost identical to purple_url_encode (hence purple_url_decode
4102 * being used above), but we want to keep certain characters unescaped
4103 * for compat reasons */
4104 const char *
4105 purple_escape_filename(const char *str)
4107 const char *iter;
4108 static char buf[BUF_LEN];
4109 char utf_char[6];
4110 guint i, j = 0;
4112 g_return_val_if_fail(str != NULL, NULL);
4113 g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
4115 iter = str;
4116 for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) {
4117 gunichar c = g_utf8_get_char(iter);
4118 /* If the character is an ASCII character and is alphanumeric,
4119 * or one of the specified values, no need to escape */
4120 if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' ||
4121 c == '_' || c == '.' || c == '#')) {
4122 buf[j++] = c;
4123 } else {
4124 int bytes = g_unichar_to_utf8(c, utf_char);
4125 for (i = 0; (int)i < bytes; i++) {
4126 if (j > (BUF_LEN - 4))
4127 break;
4128 if (i >= sizeof(utf_char)) {
4129 g_warn_if_reached();
4130 break;
4132 sprintf(buf + j, "%%%02x", utf_char[i] & 0xff);
4133 j += 3;
4137 #ifdef _WIN32
4138 /* File/Directory names in windows cannot end in periods/spaces.
4139 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4141 while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' '))
4142 j--;
4143 #endif
4144 buf[j] = '\0';
4146 return buf;
4149 gchar * purple_escape_js(const gchar *str)
4151 gchar *escaped;
4153 json_node_set_string(escape_js_node, str);
4154 json_generator_set_root(escape_js_gen, escape_js_node);
4155 escaped = json_generator_to_data(escape_js_gen, NULL);
4156 json_node_set_boolean(escape_js_node, FALSE);
4158 return escaped;
4161 void purple_restore_default_signal_handlers(void)
4163 #ifndef _WIN32
4164 signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */
4165 signal(SIGINT, SIG_DFL); /* 2: interrupt program */
4166 signal(SIGQUIT, SIG_DFL); /* 3: quit program */
4167 signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */
4168 signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */
4169 signal(SIGABRT, SIG_DFL); /* 6: abort program */
4171 #ifdef SIGPOLL
4172 signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */
4173 #endif /* SIGPOLL */
4175 #ifdef SIGEMT
4176 signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */
4177 #endif /* SIGEMT */
4179 signal(SIGFPE, SIG_DFL); /* 8: floating point exception */
4180 signal(SIGBUS, SIG_DFL); /* 10: bus error */
4181 signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */
4182 signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */
4183 signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */
4184 signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */
4185 signal(SIGTERM, SIG_DFL); /* 15: software termination signal */
4186 signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */
4187 signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */
4188 signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */
4189 #endif /* !_WIN32 */
4192 static void
4193 set_status_with_attrs(PurpleStatus *status, ...)
4195 va_list args;
4196 va_start(args, status);
4197 purple_status_set_active_with_attrs(status, TRUE, args);
4198 va_end(args);
4201 void purple_util_set_current_song(const char *title, const char *artist, const char *album)
4203 GList *list = purple_accounts_get_all();
4204 for (; list; list = list->next) {
4205 PurplePresence *presence;
4206 PurpleStatus *tune;
4207 PurpleAccount *account = list->data;
4208 if (!purple_account_get_enabled(account, purple_core_get_ui()))
4209 continue;
4211 presence = purple_account_get_presence(account);
4212 tune = purple_presence_get_status(presence, "tune");
4213 if (!tune)
4214 continue;
4215 if (title) {
4216 set_status_with_attrs(tune,
4217 PURPLE_TUNE_TITLE, title,
4218 PURPLE_TUNE_ARTIST, artist,
4219 PURPLE_TUNE_ALBUM, album,
4220 NULL);
4221 } else {
4222 purple_status_set_active(tune, FALSE);
4227 char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused)
4229 GString *string;
4230 char *esc;
4232 if (!title || !*title)
4233 return NULL;
4235 esc = g_markup_escape_text(title, -1);
4236 string = g_string_new("");
4237 g_string_append_printf(string, "%s", esc);
4238 g_free(esc);
4240 if (artist && *artist) {
4241 esc = g_markup_escape_text(artist, -1);
4242 g_string_append_printf(string, _(" - %s"), esc);
4243 g_free(esc);
4246 if (album && *album) {
4247 esc = g_markup_escape_text(album, -1);
4248 g_string_append_printf(string, _(" (%s)"), esc);
4249 g_free(esc);
4252 return g_string_free(string, FALSE);
4255 const gchar *
4256 purple_get_host_name(void)
4258 return g_get_host_name();
4261 gchar *
4262 purple_uuid_random(void)
4264 guint32 tmp, a, b;
4266 tmp = g_random_int();
4267 a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */
4268 tmp >>= 12;
4269 b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */
4271 tmp = g_random_int();
4273 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4274 g_random_int(),
4275 tmp & 0xFFFF,
4278 (tmp >> 16) & 0xFFFF, g_random_int());
4281 void purple_callback_set_zero(gpointer data)
4283 gpointer *ptr = data;
4285 g_return_if_fail(ptr != NULL);
4287 *ptr = NULL;
4290 GValue *
4291 purple_value_new(GType type)
4293 GValue *ret;
4295 g_return_val_if_fail(type != G_TYPE_NONE, NULL);
4297 ret = g_new0(GValue, 1);
4298 g_value_init(ret, type);
4300 return ret;
4303 GValue *
4304 purple_value_dup(GValue *value)
4306 GValue *ret;
4308 g_return_val_if_fail(value != NULL, NULL);
4310 ret = g_new0(GValue, 1);
4311 g_value_init(ret, G_VALUE_TYPE(value));
4312 g_value_copy(value, ret);
4314 return ret;
4317 void
4318 purple_value_free(GValue *value)
4320 g_return_if_fail(value != NULL);
4322 g_value_unset(value);
4323 g_free(value);
4327 _purple_fstat(int fd, GStatBuf *st)
4329 int ret;
4331 g_return_val_if_fail(st != NULL, -1);
4333 #ifdef _WIN32
4334 ret = _fstat(fd, st);
4335 #else
4336 ret = fstat(fd, st);
4337 #endif
4339 return ret;