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
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
21 #include "conversation.h"
24 #include "glibcompat.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
;
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
);
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
;
69 json_node_free(escape_js_node
);
70 escape_js_node
= NULL
;
72 g_object_unref(escape_js_gen
);
76 /**************************************************************************
78 **************************************************************************/
80 purple_base16_encode(const guchar
*data
, gsize len
)
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);
97 purple_base16_decode(const char *str
, gsize
*ret_len
)
99 gsize len
, i
, accumulator
= 0;
102 g_return_val_if_fail(str
!= NULL
, NULL
);
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
++)
119 accumulator
|= str
[i
] - 48;
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;
134 data
[(i
- 1) / 2] = accumulator
;
144 purple_base16_encode_chunked(const guchar
*data
, gsize len
)
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;
167 /**************************************************************************
168 * Date/Time Functions
169 **************************************************************************/
172 purple_utf8_strftime(const char *format
, const struct tm
*tm
)
174 static char buf
[128];
178 g_return_val_if_fail(format
!= NULL
, NULL
);
182 dt
= g_date_time_new_now_local();
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
);
193 purple_debug_error("util",
194 "purple_utf8_strftime(): Formatting failed\n");
198 g_strlcpy(buf
, utf8
, sizeof(buf
));
204 purple_date_format_short(const struct tm
*tm
)
206 return purple_utf8_strftime("%x", tm
);
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
);
222 purple_date_format_full(const struct tm
*tm
)
224 return purple_utf8_strftime("%c", tm
);
228 purple_time_format(const struct tm
*tm
)
230 return purple_utf8_strftime("%X", tm
);
234 purple_time_build(int year
, int month
, int day
, int hour
, int min
, int sec
)
238 tm
.tm_year
= year
- 1900;
239 tm
.tm_mon
= month
- 1;
243 tm
.tm_sec
= sec
>= 0 ? sec
: time(NULL
) % 60;
248 /* originally taken from GLib trunk 1-6-11 */
249 /* originally licensed as LGPL 2+ */
251 mktime_utc(struct tm
*tm
)
256 static const gint days_before
[] =
258 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
263 if (tm
->tm_mon
< 0 || tm
->tm_mon
> 11)
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)
273 retval
= ((((retval
* 24) + tm
->tm_hour
) * 60) + tm
->tm_min
) * 60 + tm
->tm_sec
;
275 retval
= timegm (tm
);
276 #endif /* !HAVE_TIMEGM */
282 purple_str_to_time(const char *timestamp
, gboolean utc
,
283 struct tm
*tm
, long *tz_off
, const char **rest
)
288 long tzoff
= PURPLE_NO_TZ_OFF
;
290 gboolean mktime_with_utc
= FALSE
;
295 g_return_val_if_fail(timestamp
!= NULL
, 0);
297 memset(&t
, 0, sizeof(struct tm
));
301 /* Strip leading whitespace */
302 while (g_ascii_isspace(*str
))
313 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
314 if (rest
!= NULL
&& *str
!= '\0')
321 if (sscanf(str
, "%04d", &year
) && year
>= 1900) {
324 if (*str
== '-' || *str
== '/')
327 t
.tm_year
= year
- 1900;
331 if (!sscanf(str
, "%02d", &t
.tm_mon
)) {
332 if (rest
!= NULL
&& *str
!= '\0')
341 if (*str
== '-' || *str
== '/')
345 if (!sscanf(str
, "%02d", &t
.tm_mday
)) {
346 if (rest
!= NULL
&& *str
!= '\0')
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 */
358 if (rest
!= NULL
&& *str
!= '\0')
366 if (!sscanf(str
, "%04d", &t
.tm_year
)) {
367 if (rest
!= NULL
&& *str
!= '\0')
374 } else if (*str
== 'T' || *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 &&
380 (sscanf(str
, "%02d%02d%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
383 gint sign
, tzhrs
, tzmins
;
386 /* Cut off those pesky micro-seconds */
389 } while (*str
>= '0' && *str
<= '9');
392 sign
= (*str
== '+') ? 1 : -1;
394 /* Process the timezone */
395 if (*str
== '+' || *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;
405 } else if (*str
== 'Z') {
406 /* 'Z' = Zulu = UTC */
408 mktime_with_utc
= TRUE
;
412 if (!mktime_with_utc
)
414 /* No timezone specified. */
417 mktime_with_utc
= TRUE
;
427 if (rest
!= NULL
&& *str
!= '\0') {
428 /* Strip trailing whitespace */
429 while (g_ascii_isspace(*str
))
437 retval
= mktime_utc(&t
);
444 if (tzoff
!= PURPLE_NO_TZ_OFF
)
454 purple_str_to_date_time(const char *timestamp
, gboolean utc
)
463 gint microseconds
= 0;
465 GTimeZone
*tz
= NULL
;
468 g_return_val_if_fail(timestamp
!= NULL
, NULL
);
472 /* Strip leading whitespace */
473 while (g_ascii_isspace(*str
))
480 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
485 if (sscanf(str
, "%04d", &year
) && year
> 0) {
488 if (*str
== '-' || *str
== '/')
493 if (!sscanf(str
, "%02d", &month
)) {
499 if (*str
== '-' || *str
== '/')
503 if (!sscanf(str
, "%02d", &day
)) {
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 */
518 if (!sscanf(str
, "%04d", &year
)) {
521 } else if (*str
== 'T' || *str
== '.') {
524 /* Continue grabbing the hours/minutes/seconds */
525 if ((sscanf(str
, "%02d:%02d:%02d", &hour
, &minute
, &seconds
) == 3 &&
527 (sscanf(str
, "%02d%02d%02d", &hour
, &minute
, &seconds
) == 3 &&
532 if (sscanf(str
, "%d%n", µseconds
, &chars
) == 1) {
538 const gchar
*end
= str
;
539 if (*end
== '+' || *end
== '-') {
543 while (isdigit(*end
) || *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
);
553 /* Just try whatever is there. */
554 tz
= g_time_zone_new(str
);
561 /* No timezone specified. */
563 tz
= g_time_zone_new_utc();
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
);
577 purple_uts35_to_str(const char *format
, size_t len
, struct tm
*tm
)
583 time_t now
= time(NULL
);
584 tm
= localtime(&now
);
587 string
= g_string_sized_new(len
);
591 while ((i
+ count
) < len
&& format
[i
] == format
[i
+count
])
599 } else if (count
== 4) {
601 } else if (count
>= 5) {
611 /* Two-digits only */
612 g_string_append(string
, purple_utf8_strftime("%y", tm
));
615 g_string_append_printf(string
, "%0*d",
621 /* Year (in "Week of Year" based calendars) */
624 /* Two-digits only */
634 /* Cyclic Year Name */
638 } else if (count
== 4) {
640 } else if (count
>= 5) {
649 /* Stand-alone Quarter */
653 } else if (count
== 3) {
655 } else if (count
>= 4) {
663 /* Stand-alone Month */
667 g_string_append(string
, purple_utf8_strftime("%m", tm
));
668 } else if (count
== 3) {
670 g_string_append(string
, purple_utf8_strftime("%b", tm
));
671 } else if (count
== 4) {
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);
687 g_string_append(string
, purple_utf8_strftime("%W", tm
));
688 count
= MIN(count
, 2);
699 g_string_append(string
, purple_utf8_strftime("%d", tm
));
700 count
= MIN(count
, 2);
705 g_string_append(string
, purple_utf8_strftime("%j", tm
));
706 count
= MIN(count
, 3);
709 /* Day of Year in Month */
714 /* Modified Julian Day */
723 g_string_append(string
, purple_utf8_strftime("%a", tm
));
724 } else if (count
== 4) {
726 g_string_append(string
, purple_utf8_strftime("%A", tm
));
727 } else if (count
>= 5) {
729 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
734 /* Local Day of Week */
738 g_string_append(string
, purple_utf8_strftime("%u", tm
));
739 } else if (count
== 3) {
741 g_string_append(string
, purple_utf8_strftime("%a", tm
));
742 } else if (count
== 4) {
744 g_string_append(string
, purple_utf8_strftime("%A", tm
));
745 } else if (count
>= 5) {
747 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
752 /* Stand-alone Local Day of Week */
756 g_string_append(string
, purple_utf8_strftime("%u", tm
));
758 } else if (count
== 3) {
760 g_string_append(string
, purple_utf8_strftime("%a", tm
));
761 } else if (count
== 4) {
763 g_string_append(string
, purple_utf8_strftime("%A", tm
));
764 } else if (count
>= 5) {
766 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
774 g_string_append(string
, purple_utf8_strftime("%p", tm
));
782 g_string_append(string
, purple_utf8_strftime("%I", tm
));
783 } else if (count
>= 2) {
785 g_string_append(string
, purple_utf8_strftime("%I", tm
));
794 g_string_append(string
, purple_utf8_strftime("%H", tm
));
795 } else if (count
>= 2) {
797 g_string_append(string
, purple_utf8_strftime("%H", tm
));
808 } else if (count
>= 2) {
814 /* Hour (hHkK by locale) */
821 g_string_append(string
, purple_utf8_strftime("%M", tm
));
822 count
= MIN(count
, 2);
828 g_string_append(string
, purple_utf8_strftime("%S", tm
));
829 count
= MIN(count
, 2);
832 /* Fractional Sub-second */
841 /* Time Zone (specific non-location format) */
845 } else if (count
>= 4) {
855 g_string_append(string
, purple_utf8_strftime("%z", tm
));
856 } else if (count
== 4) {
858 } else if (count
>= 5) {
860 g_string_append(string
, purple_utf8_strftime("%z", tm
));
865 /* Time Zone (generic non-location format) */
869 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
871 } else if (count
>= 4) {
873 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
883 } else if (count
>= 4) {
884 /* Generic Location Format) */
885 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
892 g_string_append_len(string
, format
+ i
, count
);
899 return g_string_free(string
, FALSE
);
903 /**************************************************************************
905 **************************************************************************/
908 * This function is stolen from glib's gmarkup.c and modified to not
909 * replace ' with '
911 static void append_escaped_text(GString
*str
,
912 const gchar
*text
, gssize length
)
924 next
= g_utf8_next_char (p
);
929 g_string_append (str
, "&");
933 g_string_append (str
, "<");
937 g_string_append (str
, ">");
941 g_string_append (str
, """);
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
);
953 g_string_append_len (str
, p
, next
- p
);
961 /* This function is stolen from glib's gmarkup.c */
962 gchar
*purple_markup_escape_text(const gchar
*text
, gssize length
)
966 g_return_val_if_fail(text
!= NULL
, NULL
);
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
);
979 purple_markup_unescape_entity(const char *text
, int *length
)
984 if (!text
|| *text
!= '&')
987 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
989 if(IS_ENTITY("&"))
991 else if(IS_ENTITY("<"))
993 else if(IS_ENTITY(">"))
995 else if(IS_ENTITY(" "))
997 else if(IS_ENTITY("©"))
998 pln
= "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
999 else if(IS_ENTITY("""))
1001 else if(IS_ENTITY("®"))
1002 pln
= "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1003 else if(IS_ENTITY("'"))
1005 else if(text
[1] == '#' && (g_ascii_isxdigit(text
[2]) || text
[2] == 'x')) {
1007 const char *start
= text
+ 2;
1013 if (*start
== 'x') {
1018 pound
= g_ascii_strtoull(start
, &end
, base
);
1019 if (pound
== 0 || pound
> INT_MAX
|| *end
!= ';') {
1023 len
= (end
- text
) + 1;
1025 buflen
= g_unichar_to_utf8((gunichar
)pound
, buf
);
1038 purple_markup_get_css_property(const gchar
*style
,
1041 const gchar
*css_str
= style
;
1042 const gchar
*css_value_start
;
1043 const gchar
*css_value_end
;
1047 g_return_val_if_fail(opt
!= NULL
, NULL
);
1052 /* find the CSS property */
1055 /* skip whitespace characters */
1056 while (*css_str
&& g_ascii_isspace(*css_str
))
1058 if (!g_ascii_isalpha(*css_str
))
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
!= ';')
1073 /* find the CSS value position in the string */
1074 css_str
+= strlen(opt
);
1075 while (*css_str
&& g_ascii_isspace(*css_str
))
1077 if (*css_str
!= ':')
1080 while (*css_str
&& g_ascii_isspace(*css_str
))
1082 if (*css_str
== '\0' || *css_str
== '"' || *css_str
== ';')
1085 /* mark the CSS value */
1086 css_value_start
= css_str
;
1087 while (*css_str
&& *css_str
!= '"' && *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
))
1095 tmp
= g_strndup(css_value_start
, css_value_end
- css_value_start
+ 1);
1096 ret
= purple_unescape_html(tmp
);
1102 gboolean
purple_markup_is_rtl(const char *html
)
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"))
1116 tmp
= g_datalist_get_data(&attributes
, "style");
1119 char *tmp2
= purple_markup_get_css_property(tmp
, "direction");
1120 if (tmp2
&& !g_ascii_strcasecmp(tmp2
, "RTL"))
1126 g_datalist_clear(&attributes
);
1132 purple_markup_find_tag(const char *needle
, const char *haystack
,
1133 const char **start
, const char **end
, GData
**attributes
)
1136 const char *cur
= haystack
;
1138 gboolean found
= FALSE
;
1139 gboolean in_tag
= FALSE
;
1140 gboolean in_attr
= FALSE
;
1141 const char *in_quotes
= NULL
;
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
) {
1157 const char *close
= cur
;
1159 while (*close
&& *close
!= *in_quotes
)
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 */
1166 /* only store a value if we have an attribute 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
);
1181 } else if (in_attr
) {
1182 const char *close
= cur
;
1184 while (*close
&& *close
!= '>' && *close
!= '"' &&
1185 *close
!= '\'' && *close
!= ' ' && *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 */
1199 size_t len
= close
- cur
;
1201 /* don't store a blank attribute name */
1204 name
= g_ascii_strdown(cur
, len
);
1222 /* swallow extra spaces inside tag */
1223 while (*cur
&& *cur
== ' ') cur
++;
1240 /* if we hit a < followed by the name of our tag... */
1241 if (*cur
== '<' && !g_ascii_strncasecmp(cur
+ 1, needle
, needlelen
)) {
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
== '>') {
1251 while (*cur
&& *cur
!= '"' && *cur
!= '\'' && *cur
!= '>') {
1254 while (*cur
&& *cur
!= '"')
1256 } else if (*cur
== '\'') {
1258 while (*cur
&& *cur
!= '\'')
1271 /* clean up any attribute name from a premature termination */
1275 *attributes
= attribs
;
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
)
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
);
1307 p
+= strlen(start_token
) + skip
;
1312 if (check_value
!= '\0' && *p
== check_value
)
1315 q
= strstr(p
, end_token
);
1317 /* Trim leading blanks */
1318 while (*p
!= '\n' && g_ascii_isspace(*p
)) {
1322 /* Trim trailing blanks */
1323 while (q
> p
&& g_ascii_isspace(*(q
- 1))) {
1327 /* Don't bother with null strings */
1331 if (q
!= NULL
&& (!no_value_token
||
1332 strncmp(p
, no_value_token
, strlen(no_value_token
)))) {
1333 GString
*dest
= g_string_new("");
1337 g_string_append(dest
, "<a href=\"");
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
);
1349 g_string_append_len(dest
, p
, q
- p
);
1350 g_string_append(dest
, "\">");
1353 g_string_append(dest
, link_prefix
);
1355 g_string_append_len(dest
, p
, q
- p
);
1356 g_string_append(dest
, "</a>");
1360 if (format_cb
!= NULL
)
1362 char *reformatted
= format_cb(p
, q
- p
);
1363 g_string_append(dest
, reformatted
);
1364 g_free(reformatted
);
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
);
1379 struct purple_parse_tag
{
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 \
1399 GString *innards = g_string_new(""); \
1401 if(!q && (*o == '\"' || *o == '\'') ) { \
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); \
1411 } else if(*c == '\\') { \
1414 } else if(*o == '<') { \
1416 } else if(*o == '>') { \
1420 innards = g_string_append_c(innards, *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); \
1429 tags = g_list_prepend(tags, pt); \
1432 xhtml = g_string_append(xhtml, "<" y); \
1433 xhtml = g_string_append(xhtml, innards->str); \
1434 xhtml = g_string_append_c(xhtml, '>'); \
1437 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1439 xhtml = g_string_append(xhtml, "<"); \
1441 plain = g_string_append_c(plain, '<'); \
1444 g_string_free(innards, TRUE); \
1447 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1448 (*(c+strlen("<" x)) == '>' || \
1449 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1451 xhtml = g_string_append(xhtml, "<" y); \
1452 c += strlen("<" x); \
1454 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1457 tags = g_list_prepend(tags, pt); \
1459 xhtml = g_string_append_c(xhtml, '>'); \
1462 xhtml = g_string_append(xhtml, "/>");\
1464 c = strchr(c, '>') + 1; \
1467 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1468 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1470 purple_markup_html_to_xhtml(const char *html
, char **xhtml_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
;
1481 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1486 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1488 g_return_if_fail(xhtml_out
!= NULL
|| plain_out
!= NULL
);
1491 xhtml
= g_string_new("");
1493 plain
= g_string_new("");
1497 if(*(c
+1) == '/') { /* closing 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;
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 */
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
)));
1519 g_string_free(cdata
, TRUE
);
1526 tags
= g_list_remove(tags
, pt
);
1530 tags
= g_list_delete_link(tags
, tag
);
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
))
1542 xhtml
= g_string_append(xhtml
, "<");
1544 plain
= g_string_append_c(plain
, '<');
1548 } else { /* opening tag */
1549 ALLOW_TAG("blockquote");
1559 /* we only allow html to start the message */
1563 ALLOW_TAG_ALT("i", "em");
1564 ALLOW_TAG_ALT("italic", "em");
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;
1585 xhtml
= g_string_append(xhtml
, "<br/>");
1586 if(plain
&& *c
!= '\n')
1587 plain
= g_string_append_c(plain
, '\n');
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);
1594 else if (*(c
+2) == 'o')
1595 pt
->src_tag
= "bold";
1597 pt
->src_tag
= "strong";
1598 pt
->dest_tag
= "span";
1599 tags
= g_list_prepend(tags
, pt
);
1600 c
= strchr(c
, '>') + 1;
1602 xhtml
= g_string_append(xhtml
, "<span style='font-weight: bold;'>");
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;
1612 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: underline;'>");
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;
1622 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: line-through;'>");
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;
1632 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:sub;'>");
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;
1642 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:super;'>");
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)) { \
1652 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1653 to = g_string_append(to, "&"); \
1654 else if (*from == '\'') \
1655 to = g_string_append(to, "'"); \
1657 to = g_string_append_c(to, *from); \
1661 while (*p
&& *p
!= '>') {
1662 if (!g_ascii_strncasecmp(p
, "src=", 4)) {
1663 const char *q
= p
+ 4;
1665 g_string_free(src
, TRUE
);
1666 src
= g_string_new("");
1669 } else if (!g_ascii_strncasecmp(p
, "alt=", 4)) {
1670 const char *q
= p
+ 4;
1672 g_string_free(alt
, TRUE
);
1673 alt
= g_string_new("");
1681 if ((c
= strchr(p
, '>')) != NULL
)
1685 /* src and alt are required! */
1687 g_string_append_printf(xhtml
, "<img src='%s' alt='%s' />", g_strstrip(src
->str
), alt
? alt
->str
: "");
1690 plain
= g_string_append(plain
, purple_unescape_html(alt
->str
));
1692 xhtml
= g_string_append(xhtml
, alt
->str
);
1693 g_string_free(alt
, TRUE
);
1695 g_string_free(src
, TRUE
);
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;
1705 g_string_free(url
, TRUE
);
1706 url
= g_string_new("");
1708 g_string_free(cdata
, TRUE
);
1709 cdata
= g_string_new("");
1711 while (VALID_CHAR(q
)) {
1713 if ((*q
== '&') && (purple_markup_unescape_entity(q
, &len
) == NULL
))
1714 url
= g_string_append(url
, "&");
1716 url
= g_string_append(url
, """);
1718 url
= g_string_append_c(url
, *q
);
1726 if ((c
= strchr(p
, '>')) != NULL
)
1730 pt
= g_new0(struct purple_parse_tag
, 1);
1733 tags
= g_list_prepend(tags
, pt
);
1735 g_string_append_printf(xhtml
, "<a href=\"%s\">", url
? g_strstrip(url
->str
) : "");
1738 #define ESCAPE(from, to) \
1739 CHECK_QUOTE(from); \
1740 while (VALID_CHAR(from)) { \
1742 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1743 to = g_string_append(to, "&"); \
1744 else if (*from == '\'') \
1745 to = g_string_append_c(to, '\"'); \
1747 to = g_string_append_c(to, *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("");
1759 g_string_append_printf(style
, "background: %s; ", color
->str
);
1760 g_string_free(color
, TRUE
);
1762 } else if (!g_ascii_strncasecmp(p
, "color=", 6)) {
1763 const char *q
= p
+ 6;
1764 GString
*color
= g_string_new("");
1766 g_string_append_printf(style
, "color: %s; ", color
->str
);
1767 g_string_free(color
, TRUE
);
1769 } else if (!g_ascii_strncasecmp(p
, "face=", 5)) {
1770 const char *q
= p
+ 5;
1771 GString
*face
= g_string_new("");
1773 g_string_append_printf(style
, "font-family: %s; ", g_strstrip(face
->str
));
1774 g_string_free(face
, TRUE
);
1776 } else if (!g_ascii_strncasecmp(p
, "size=", 5)) {
1777 const char *q
= p
+ 5;
1779 const char *size
= "medium";
1806 g_string_append_printf(style
, "font-size: %s; ", size
);
1812 if ((c
= strchr(p
, '>')) != NULL
)
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
));
1824 g_string_free(style
, TRUE
);
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("");
1837 while (VALID_CHAR(q
)) {
1838 color
= g_string_append_c(color
, *q
);
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
)
1848 pt
->src_tag
= "body";
1849 pt
->dest_tag
= "span";
1850 tags
= g_list_prepend(tags
, pt
);
1851 did_something
= TRUE
;
1856 if (did_something
) continue;
1858 /* this has to come after the special case for bgcolor */
1860 if(!g_ascii_strncasecmp(c
, "<!--", strlen("<!--"))) {
1861 char *p
= strstr(c
+ strlen("<!--"), "-->");
1864 xhtml
= g_string_append(xhtml
, "<!--");
1865 c
+= strlen("<!--");
1871 xhtml
= g_string_append(xhtml
, "<");
1873 plain
= g_string_append_c(plain
, '<');
1876 } else if(*c
== '&') {
1881 if ((pln
= purple_markup_unescape_entity(c
, &len
)) == NULL
) {
1883 g_snprintf(buf
, sizeof(buf
), "%c", *c
);
1887 xhtml
= g_string_append_len(xhtml
, c
, len
);
1889 plain
= g_string_append(plain
, pln
);
1891 cdata
= g_string_append_len(cdata
, c
, len
);
1895 xhtml
= g_string_append_c(xhtml
, *c
);
1897 plain
= g_string_append_c(plain
, *c
);
1899 cdata
= g_string_append_c(cdata
, *c
);
1904 for (tag
= tags
; tag
; tag
= tag
->next
) {
1905 struct purple_parse_tag
*pt
= tag
->data
;
1907 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
1912 *xhtml_out
= g_string_free(xhtml
, FALSE
);
1914 *plain_out
= g_string_free(plain
, FALSE
);
1916 g_string_free(url
, TRUE
);
1918 g_string_free(cdata
, TRUE
);
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
1932 purple_markup_strip_html(const char *str
)
1934 int i
, j
, k
, entlen
;
1935 gboolean visible
= TRUE
;
1936 gboolean closing_td_p
= FALSE
;
1938 const gchar
*cdata_close_tag
= NULL
, *ent
;
1945 str2
= g_strdup(str
);
1947 for (i
= 0, j
= 0; str2
[i
]; 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
;
1962 else if (g_ascii_strncasecmp(str2
+ i
, "<td", 3) == 0 && closing_td_p
)
1967 else if (g_ascii_strncasecmp(str2
+ i
, "</td>", 5) == 0)
1969 closing_td_p
= TRUE
;
1974 closing_td_p
= FALSE
;
1980 if(g_ascii_isspace(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
] != '>')
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 ) */
2001 /* Find start of href */
2002 for (st
= i
+ 3; st
< k
; st
++)
2004 if (g_ascii_strncasecmp(str2
+st
, "href=", 5) == 0)
2007 if (str2
[st
] == '"' || str2
[st
] == '\'')
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. */
2027 tmp
= g_strndup(str2
+ st
, end
- st
);
2028 href
= purple_unescape_html(tmp
);
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)))
2048 memmove(str2
+ j
, href
, hrlen
);
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)
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
;
2082 else if (cdata_close_tag
)
2086 else if (!g_ascii_isspace(str2
[i
]))
2091 if (str2
[i
] == '&' && (ent
= purple_markup_unescape_entity(str2
+ i
, &entlen
)) != NULL
)
2100 str2
[j
++] = g_ascii_isspace(str2
[i
])? ' ': str2
[i
];
2129 badentity(const char *c
)
2131 if (!g_ascii_strncasecmp(c
, "<", 4) ||
2132 !g_ascii_strncasecmp(c
, ">", 4) ||
2133 !g_ascii_strncasecmp(c
, """, 6)) {
2140 process_link(GString
*ret
,
2141 const char *start
, const char *c
,
2143 const char *urlprefix
,
2146 char *url_buf
, *tmpurlbuf
;
2150 if (!badchar(*t
) && !badentity(t
))
2153 if (t
- c
== matchlen
)
2156 if (*t
== ',' && *(t
+ 1) != ' ') {
2160 if (t
> start
&& *(t
- 1) == '.')
2162 if (t
> start
&& *(t
- 1) == ')' && inside_paren
> 0)
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>",
2169 tmpurlbuf
, url_buf
);
2179 purple_markup_linkify(const char *text
)
2181 const char *c
, *t
, *q
= NULL
;
2182 char *tmpurlbuf
, *url_buf
;
2184 gboolean inside_html
= FALSE
;
2185 int inside_paren
= 0;
2191 ret
= g_string_new("");
2196 if(*c
== '(' && !inside_html
) {
2198 ret
= g_string_append_c(ret
, *c
);
2204 inside_html
= FALSE
;
2205 } else if(!q
&& (*c
== '\"' || *c
== '\'')) {
2211 } else if(*c
== '<') {
2213 if (!g_ascii_strncasecmp(c
, "<A", 2)) {
2215 if (!g_ascii_strncasecmp(c
, "/A>", 3)) {
2216 inside_html
= FALSE
;
2219 ret
= g_string_append_c(ret
, *c
);
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)) {
2244 if (badchar(*t
) || badentity(t
)) {
2249 if (t
> text
&& *(t
- 1) == '.')
2251 if ((d
= strstr(c
+ 7, "?")) != NULL
&& d
< t
)
2252 url_buf
= g_strndup(c
+ 7, d
- c
- 7);
2254 url_buf
= g_strndup(c
+ 7, t
- c
- 7);
2255 if (!purple_email_is_valid(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
);
2271 } else if (c
!= text
&& (*c
== '@')) {
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)))
2280 gurl_buf
= g_string_new("");
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, "<", 4) ||
2289 !g_ascii_strncasecmp(t
- 3, ">", 4))) ||
2290 (t
> (text
+4) && (!g_ascii_strncasecmp(t
- 5, """, 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
);
2296 g_string_prepend_unichar(gurl_buf
, g
);
2297 t
= g_utf8_find_prev_char(text
, t
);
2299 ret
= g_string_assign(ret
, "");
2305 t
= g_utf8_find_next_char(c
, NULL
);
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
)) {
2313 url_buf
= g_string_free(gurl_buf
, FALSE
);
2316 /* strip off trailing periods */
2318 for (d
= url_buf
+ strlen(url_buf
) - 1; *d
== '.'; d
--, t
--)
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
);
2327 g_string_append(ret
, url_buf
);
2335 g_string_append_unichar(gurl_buf
, g
);
2336 t
= g_utf8_find_next_char(t
, NULL
);
2341 g_string_free(gurl_buf
, TRUE
);
2345 if(*c
== ')' && !inside_html
) {
2347 ret
= g_string_append_c(ret
, *c
);
2354 ret
= g_string_append_c(ret
, *c
);
2358 return g_string_free(ret
, FALSE
);
2361 char *purple_unescape_text(const char *in
)
2369 ret
= g_string_new("");
2374 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2375 g_string_append(ret
, ent
);
2378 g_string_append_c(ret
, *c
);
2383 return g_string_free(ret
, FALSE
);
2386 char *purple_unescape_html(const char *html
)
2389 const char *c
= html
;
2394 ret
= g_string_new("");
2399 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2400 g_string_append(ret
, ent
);
2402 } else if (!strncmp(c
, "<br>", 4)) {
2403 g_string_append_c(ret
, '\n');
2406 g_string_append_c(ret
, *c
);
2411 return g_string_free(ret
, FALSE
);
2415 purple_markup_slice(const char *str
, guint x
, guint y
)
2420 gboolean appended
= FALSE
;
2424 g_return_val_if_fail(str
!= NULL
, NULL
);
2425 g_return_val_if_fail(x
<= y
, NULL
);
2428 return g_strdup("");
2430 ret
= g_string_new("");
2433 while (*str
&& (z
< y
)) {
2434 c
= g_utf8_get_char(str
);
2437 char *end
= strchr(str
, '>');
2440 g_string_free(ret
, TRUE
);
2441 while ((tag
= g_queue_pop_head(q
)))
2447 if (!g_ascii_strncasecmp(str
, "<img ", 5)) {
2448 z
+= strlen("[Image]");
2449 } else if (!g_ascii_strncasecmp(str
, "<br", 3)) {
2451 } else if (!g_ascii_strncasecmp(str
, "<hr>", 4)) {
2452 z
+= strlen("\n---\n");
2453 } else if (!g_ascii_strncasecmp(str
, "</", 2)) {
2457 tmp
= g_queue_pop_head(q
);
2461 /* push it unto the stack */
2464 tmp
= g_strndup(str
, end
- str
+ 1);
2465 g_queue_push_head(q
, tmp
);
2470 g_string_append_len(ret
, str
, end
- str
+ 1);
2474 } else if (c
== '&') {
2475 char *end
= strchr(str
, ';');
2477 g_string_free(ret
, TRUE
);
2478 while ((tag
= g_queue_pop_head(q
)))
2486 g_string_append_len(ret
, str
, end
- str
+ 1);
2491 if (z
== x
&& z
> 0 && !appended
) {
2496 g_string_append(ret
, tag
);
2503 g_string_append_unichar(ret
, c
);
2507 str
= g_utf8_next_char(str
);
2510 while ((tag
= g_queue_pop_head(q
))) {
2513 name
= purple_markup_get_tag_name(tag
);
2514 g_string_append_printf(ret
, "</%s>", name
);
2520 return g_string_free(ret
, FALSE
);
2524 purple_markup_get_tag_name(const char *tag
)
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
] == '/')
2534 return g_strndup(tag
+1, i
-1);
2537 /**************************************************************************
2538 * Path/Filename Functions
2539 **************************************************************************/
2541 purple_home_dir(void)
2544 return g_get_home_dir();
2546 return wpurple_home_dir();
2550 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2552 purple_user_dir(void)
2554 if (custom_user_dir
!= NULL
)
2555 return custom_user_dir
;
2557 user_dir
= g_build_filename(purple_home_dir(), ".purple", NULL
);
2562 static const gchar
*
2563 purple_xdg_dir(gchar
**xdg_dir
, const gchar
*xdg_base_dir
, const gchar
*xdg_type
)
2566 if (!custom_user_dir
) {
2567 *xdg_dir
= g_build_filename(xdg_base_dir
, "purple", NULL
);
2569 *xdg_dir
= g_build_filename(custom_user_dir
, xdg_type
, NULL
);
2577 purple_cache_dir(void)
2579 return purple_xdg_dir(&cache_dir
, g_get_user_cache_dir(), "cache");
2583 purple_config_dir(void)
2585 return purple_xdg_dir(&config_dir
, g_get_user_config_dir(), "config");
2589 purple_data_dir(void)
2591 return purple_xdg_dir(&data_dir
, g_get_user_data_dir(), "data");
2595 purple_move_to_xdg_base_dir(const char *purple_xdg_dir
, char *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
));
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
) {
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
) {
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
);
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
);
2648 custom_user_dir
= NULL
;
2651 int purple_build_dir(const char *path
, int mode
)
2653 return g_mkdir_with_parents(path
, mode
);
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",
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
));
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
);
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
);
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
);
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
);
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
);
2725 purple_util_write_data_to_file_absolute(const char *filename_full
, const char *data
, gssize size
)
2730 g_return_val_if_fail(size
>= -1, FALSE
);
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
);
2747 g_object_unref(file
);
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
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");
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");
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"};
2790 purple_mkstemp(char **fpath
, gboolean binary
)
2792 const gchar
*tmpdir
;
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
);
2802 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2803 "Couldn't make \"%s\", error: %d\n",
2806 if((fp
= fdopen(fd
, "r+")) == NULL
) {
2808 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2809 "Couldn't fdopen(), error: %d\n", errno
);
2819 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2820 "g_get_tmp_dir() failed!\n");
2827 purple_program_is_valid(const char *program
)
2829 GError
*error
= NULL
;
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
);
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");
2864 purple_running_gnome(void)
2867 gchar
*tmp
= g_find_program_in_path("gvfs-open");
2870 tmp
= g_find_program_in_path("gnome-open");
2879 tmp
= (gchar
*)g_getenv("GNOME_DESKTOP_SESSION_ID");
2881 return ((tmp
!= NULL
) && (*tmp
!= '\0'));
2888 purple_running_kde(void)
2891 gchar
*tmp
= g_find_program_in_path("kfmclient");
2892 const char *session
;
2898 session
= g_getenv("KDE_FULL_SESSION");
2899 if (purple_strequal(session
, "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
);
2913 purple_running_osx(void)
2915 #if defined(__APPLE__)
2922 typedef union purple_sockaddr
{
2924 struct sockaddr_in sa_in
;
2925 #if defined(AF_INET6)
2926 struct sockaddr_in6 sa_in6
;
2928 struct sockaddr_storage sa_stor
;
2932 purple_fd_get_ip(int fd
)
2934 PurpleSockaddr addr
;
2935 socklen_t namelen
= sizeof(addr
);
2938 g_return_val_if_fail(fd
!= 0, NULL
);
2940 if (getsockname(fd
, &(addr
.sa
), &namelen
))
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
];
2953 tmp
= inet_ntop(family
, &(addr
.sa_in6
.sin6_addr
), host
, sizeof(host
));
2954 return g_strdup(tmp
);
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
))
2972 return addr
.sa
.sa_family
;
2976 purple_socket_speaks_ipv4(int fd
)
2980 g_return_val_if_fail(fd
>= 0, FALSE
);
2982 family
= purple_socket_get_family(fd
);
2987 #if defined(IPV6_V6ONLY)
2991 socklen_t len
= sizeof(val
);
2993 if (getsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, &len
) != 0)
3003 /**************************************************************************
3005 **************************************************************************/
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
);
3028 tmp
= g_utf8_normalize(str
, -1, G_NORMALIZE_DEFAULT
);
3029 g_snprintf(buf
, sizeof(buf
), "%s", tmp
);
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.
3044 purple_normalize_nocase(const PurpleAccount
*account
, const char *str
)
3046 static char buf
[BUF_LEN
];
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
: "");
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
);
3071 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol
, CLIENT
, normalize
))
3074 normalized
= purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol
),
3077 return (NULL
!= normalized
);
3081 purple_strdup_withhtml(const gchar
*src
)
3083 gulong destsize
, i
, j
;
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 */
3090 for (i
= 0; src
[i
] != '\0'; i
++)
3094 else if (src
[i
] != '\r')
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>");
3105 } else if (src
[i
] != '\r')
3109 dest
[destsize
-1] = '\0';
3115 purple_str_has_prefix(const char *s
, const char *p
)
3117 return g_str_has_prefix(s
, p
);
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);
3130 purple_str_has_suffix(const char *s
, const char *x
)
3132 return g_str_has_suffix(s
, x
);
3136 purple_str_add_cr(const char *text
)
3142 g_return_val_if_fail(text
!= NULL
, NULL
);
3144 if (text
[0] == '\n')
3146 for (i
= 1; i
< strlen(text
); i
++)
3147 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3151 return g_strdup(text
);
3153 ret
= g_malloc0(strlen(text
) + count
+ 1);
3156 if (text
[i
] == '\n')
3158 ret
[j
++] = text
[i
++];
3159 for (; i
< strlen(text
); i
++) {
3160 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3169 purple_str_strip_char(char *text
, char thechar
)
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
];
3183 purple_util_chrreplace(char *string
, char delimiter
,
3188 g_return_if_fail(string
!= NULL
);
3190 while (string
[i
] != '\0')
3192 if (string
[i
] == delimiter
)
3193 string
[i
] = replacement
;
3199 purple_strreplace(const char *string
, const char *delimiter
,
3200 const char *replacement
)
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
);
3217 purple_strcasereplace(const char *string
, const char *delimiter
,
3218 const char *replacement
)
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
)) {
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
);
3264 /** TODO: Expose this when we can add API */
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
);
3274 hlen
= strlen(haystack
);
3276 nlen
= strlen(needle
);
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
))
3294 purple_strcasestr(const char *haystack
, const char *needle
)
3296 return purple_strcasestr_len(haystack
, -1, needle
, -1);
3300 purple_str_seconds_to_string(guint secs
)
3303 guint days
, hrs
, mins
;
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);
3315 /* secs = secs % 60; */
3319 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d day", "%d days", days
), days
);
3326 char *tmp
= g_strdup_printf(
3327 dngettext(PACKAGE
, "%s, %d hour", "%s, %d hours", hrs
),
3333 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d hour", "%d hours", hrs
), hrs
);
3340 char *tmp
= g_strdup_printf(
3341 dngettext(PACKAGE
, "%s, %d minute", "%s, %d minutes", mins
),
3347 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d minute", "%d minutes", mins
), mins
);
3355 purple_str_binary_to_ascii(const unsigned char *binary
, guint len
)
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
, "\\\\");
3370 g_string_append_c(ret
, binary
[i
]);
3372 return g_string_free(ret
, FALSE
);
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
3385 g_return_val_if_fail(str
!= NULL
, 0);
3389 return i
* sizeof(gunichar2
);
3393 purple_str_wipe(gchar
*str
)
3397 memset(str
, 0, strlen(str
));
3402 purple_utf16_wipe(gunichar2
*str
)
3406 memset(str
, 0, purple_utf16_size(str
));
3410 /**************************************************************************
3412 **************************************************************************/
3414 void purple_got_protocol_handler_uri(const char *uri
)
3418 const char *tmp
, *param_string
;
3420 GHashTable
*params
= NULL
;
3422 if (!(tmp
= strchr(uri
, ':')) || tmp
== uri
) {
3423 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3427 len
= MIN(sizeof(proto
) - 1, (gsize
)(tmp
- uri
));
3429 strncpy(proto
, uri
, len
);
3434 if (purple_strequal(proto
, "xmpp"))
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
));
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
) {
3457 /* without these brackets, clang won't
3458 * recognize tmp as a non-NULL
3461 if (keyend
&& keyend
!= pairstart
) {
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
== '=')
3480 cmd
= g_strdup(tmp
);
3482 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto
, cmd
, params
);
3486 g_hash_table_destroy(params
);
3490 purple_url_decode(const char *str
)
3492 static char buf
[BUF_LEN
];
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
)
3506 for (i
= 0; i
< strlen(str
); i
++) {
3511 strncpy(hex
, str
+ ++i
, 2);
3514 /* i is pointing to the start of the number */
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);
3527 if (!g_utf8_validate(buf
, -1, (const char **)&bum
))
3534 purple_url_encode(const char *str
)
3537 static char buf
[BUF_LEN
];
3541 g_return_val_if_fail(str
!= NULL
, NULL
);
3542 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
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
== '~')) {
3552 int bytes
= g_unichar_to_utf8(c
, utf_char
);
3553 for (i
= 0; (int)i
< bytes
; i
++) {
3554 if (j
> (BUF_LEN
- 4))
3556 if (i
>= sizeof(utf_char
)) {
3557 g_warn_if_reached();
3560 sprintf(buf
+ j
, "%%%02X", utf_char
[i
] & 0xff);
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
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) == '\"')) {
3593 if (*c
++ && *c
< 127 && *c
> 0 && *c
!= '\n' && *c
!= '\r') continue;
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
;
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
;
3619 if (*c
== '.' && (c
== domain
|| *(c
- 1) == '.' || *(c
- 1) == '-'))
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
;
3626 if (*(c
- 1) == '-') return FALSE
;
3628 return ((c
- domain
) > 3 ? TRUE
: FALSE
);
3631 /* Stolen from gnome_uri_list_extract_uris */
3633 purple_uri_list_extract_uris(const gchar
*uri_list
)
3637 GList
*result
= NULL
;
3639 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
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.
3654 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
3659 while (q
> p
&& isspace(*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');
3674 return g_list_reverse (result
);
3678 /* Stolen from gnome_uri_list_extract_filenames */
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
);
3690 gchar
*s
= (gchar
*)tmp_list
->data
;
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);
3700 result
= g_list_delete_link(result
, node
);
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 **************************************************************************/
3722 purple_utf8_try_convert(const char *str
)
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
);
3737 utf8
= g_convert(str
, -1, "UTF-8", "ISO-8859-15", &converted
, NULL
, NULL
);
3738 if ((utf8
!= NULL
) && (converted
== strlen(str
)))
3746 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
3747 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
3749 purple_utf8_salvage(const char *str
)
3754 g_return_val_if_fail(str
!= NULL
, NULL
);
3756 workstr
= g_string_sized_new(strlen(str
));
3759 (void)g_utf8_validate(str
, -1, &end
);
3760 workstr
= g_string_append_len(workstr
, str
, end
- str
);
3765 workstr
= g_string_append_c(workstr
, '?');
3767 } while (!utf8_first(*str
));
3768 } while (*str
!= '\0');
3770 return g_string_free(workstr
, FALSE
);
3774 purple_utf8_strip_unprintables(const gchar
*str
)
3776 gchar
*workstr
, *iter
;
3780 /* Act like g_strdup */
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",
3787 g_return_val_if_reached(NULL
);
3790 workstr
= iter
= g_new(gchar
, strlen(str
) + 1);
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
);
3809 /* nul-terminate the new string */
3816 * This function is copied from g_strerror() but changed to use
3820 purple_gai_strerror(gint errnum
)
3822 static GPrivate msg_private
= G_PRIVATE_INIT(g_free
);
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
;
3837 gchar
*msg_utf8
= g_locale_to_utf8(msg_locale
, -1, NULL
, NULL
, NULL
);
3840 /* Stick in the quark table so that we can return a static result */
3841 GQuark msg_quark
= g_quark_from_string(msg_utf8
);
3844 msg_utf8
= (gchar
*)g_quark_to_string(msg_quark
);
3845 errno
= saved_errno
;
3850 msg
= g_private_get(&msg_private
);
3854 msg
= g_new(gchar
, 64);
3855 g_private_set(&msg_private
, msg
);
3858 sprintf(msg
, "unknown error (%d)", errnum
);
3860 errno
= saved_errno
;
3865 purple_utf8_ncr_encode(const char *str
)
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. */
3879 g_string_append_printf(out
, "&#%u;", (guint32
) wc
);
3881 g_string_append_unichar(out
, wc
);
3885 return g_string_free(out
, FALSE
);
3890 purple_utf8_ncr_decode(const char *str
)
3895 g_return_val_if_fail(str
!= NULL
, NULL
);
3896 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
3899 out
= g_string_new("");
3901 while( (b
= strstr(buf
, "&#")) ) {
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') {
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 */
3921 g_string_append_unichar(out
, wc
);
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
;
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");
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
);
3963 /* previously conversation::find_nick() */
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
;
3971 gboolean ret
= FALSE
;
3973 start
= hay
= g_utf8_strdown(haystack
, -1);
3975 pin
= g_utf8_strdown(needle
, -1);
3978 while ((p
= strstr(start
, pin
)) != NULL
) {
3979 prev_char
= g_utf8_find_prev_char(hay
, p
);
3982 before
= g_utf8_get_char(prev_char
);
3984 after
= g_utf8_get_char_validated(p
+ n
, - 1);
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 "&" or some such entity*/
3991 (before
!= (gunichar
)-2 && !g_unichar_isalnum(before
) && *(p
- 1) != '&'))
3992 && after
!= (gunichar
)-2 && !g_unichar_isalnum(after
)) {
4005 gboolean
purple_message_meify(char *message
, gssize len
)
4008 gboolean inside_html
= FALSE
;
4010 g_return_val_if_fail(message
!= NULL
, FALSE
);
4013 len
= strlen(message
);
4015 for (c
= message
; *c
; c
++, len
--) {
4018 inside_html
= FALSE
;
4027 if(*c
&& !g_ascii_strncasecmp(c
, "/me ", 4)) {
4028 memmove(c
, c
+4, len
-3);
4035 char *purple_text_strip_mnemonic(const char *in
)
4042 g_return_val_if_fail(in
!= NULL
, NULL
);
4044 out
= g_malloc(strlen(in
)+1);
4048 a0
= a
; /* The last non-space char seen so far, or the first char */
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) == '_') {
4063 /* We don't want to corrupt the middle of UTF-8 characters */
4064 } else if (!(*b
& 0x80)) { /* other 1-byte char */
4069 /* Multibyte utf8 char, don't look for _ inside these */
4072 if ((*b
& 0xe0) == 0xc0) {
4074 } else if ((*b
& 0xf0) == 0xe0) {
4076 } else if ((*b
& 0xf8) == 0xf0) {
4078 } else if ((*b
& 0xfc) == 0xf8) {
4080 } else if ((*b
& 0xfe) == 0xfc) {
4082 } else { /* Illegal utf8 */
4085 a0
= a
; /* unless we want to delete CJK spaces too */
4086 for (i
= 0; i
< n
&& *b
; i
+= 1) {
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 */
4105 purple_escape_filename(const char *str
)
4108 static char buf
[BUF_LEN
];
4112 g_return_val_if_fail(str
!= NULL
, NULL
);
4113 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
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
== '#')) {
4124 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4125 for (i
= 0; (int)i
< bytes
; i
++) {
4126 if (j
> (BUF_LEN
- 4))
4128 if (i
>= sizeof(utf_char
)) {
4129 g_warn_if_reached();
4132 sprintf(buf
+ j
, "%%%02x", utf_char
[i
] & 0xff);
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] == ' '))
4149 gchar
* purple_escape_js(const gchar
*str
)
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
);
4161 void purple_restore_default_signal_handlers(void)
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 */
4172 signal(SIGPOLL
, SIG_DFL
); /* 7: pollable event (POSIX) */
4173 #endif /* SIGPOLL */
4176 signal(SIGEMT
, SIG_DFL
); /* 7: EMT instruction (Non-POSIX) */
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 */
4193 set_status_with_attrs(PurpleStatus
*status
, ...)
4196 va_start(args
, status
);
4197 purple_status_set_active_with_attrs(status
, TRUE
, 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
;
4207 PurpleAccount
*account
= list
->data
;
4208 if (!purple_account_get_enabled(account
, purple_core_get_ui()))
4211 presence
= purple_account_get_presence(account
);
4212 tune
= purple_presence_get_status(presence
, "tune");
4216 set_status_with_attrs(tune
,
4217 PURPLE_TUNE_TITLE
, title
,
4218 PURPLE_TUNE_ARTIST
, artist
,
4219 PURPLE_TUNE_ALBUM
, album
,
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
)
4232 if (!title
|| !*title
)
4235 esc
= g_markup_escape_text(title
, -1);
4236 string
= g_string_new("");
4237 g_string_append_printf(string
, "%s", esc
);
4240 if (artist
&& *artist
) {
4241 esc
= g_markup_escape_text(artist
, -1);
4242 g_string_append_printf(string
, _(" - %s"), esc
);
4246 if (album
&& *album
) {
4247 esc
= g_markup_escape_text(album
, -1);
4248 g_string_append_printf(string
, _(" (%s)"), esc
);
4252 return g_string_free(string
, FALSE
);
4256 purple_get_host_name(void)
4258 return g_get_host_name();
4262 purple_uuid_random(void)
4266 tmp
= g_random_int();
4267 a
= 0x4000 | (tmp
& 0xFFF); /* 0x4000 to 0x4FFF */
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",
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
);
4291 purple_value_new(GType type
)
4295 g_return_val_if_fail(type
!= G_TYPE_NONE
, NULL
);
4297 ret
= g_new0(GValue
, 1);
4298 g_value_init(ret
, type
);
4304 purple_value_dup(GValue
*value
)
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
);
4318 purple_value_free(GValue
*value
)
4320 g_return_if_fail(value
!= NULL
);
4322 g_value_unset(value
);
4327 _purple_fstat(int fd
, GStatBuf
*st
)
4331 g_return_val_if_fail(st
!= NULL
, -1);
4334 ret
= _fstat(fd
, st
);
4336 ret
= fstat(fd
, st
);