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
))
306 if (rest
!= NULL
&& *str
!= '\0')
312 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
313 if (rest
!= NULL
&& *str
!= '\0')
320 if (sscanf(str
, "%04d", &year
) && year
>= 1900) {
323 if (*str
== '-' || *str
== '/')
326 t
.tm_year
= year
- 1900;
330 if (!sscanf(str
, "%02d", &t
.tm_mon
)) {
331 if (rest
!= NULL
&& *str
!= '\0')
340 if (*str
== '-' || *str
== '/')
344 if (!sscanf(str
, "%02d", &t
.tm_mday
)) {
345 if (rest
!= NULL
&& *str
!= '\0')
353 /* Grab the year off the end if there's still stuff */
354 if (*str
== '/' || *str
== '-') {
355 /* But make sure we don't read the year twice */
357 if (rest
!= NULL
&& *str
!= '\0')
365 if (!sscanf(str
, "%04d", &t
.tm_year
)) {
366 if (rest
!= NULL
&& *str
!= '\0')
373 } else if (*str
== 'T' || *str
== '.') {
376 /* Continue grabbing the hours/minutes/seconds */
377 if ((sscanf(str
, "%02d:%02d:%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
379 (sscanf(str
, "%02d%02d%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
382 gint sign
, tzhrs
, tzmins
;
385 /* Cut off those pesky micro-seconds */
388 } while (*str
>= '0' && *str
<= '9');
391 sign
= (*str
== '+') ? 1 : -1;
393 /* Process the timezone */
394 if (*str
== '+' || *str
== '-') {
397 if (((sscanf(str
, "%02d:%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 5)) ||
398 (sscanf(str
, "%02d%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 4))))
400 mktime_with_utc
= TRUE
;
401 tzoff
= tzhrs
* 60 * 60 + tzmins
* 60;
404 } else if (*str
== 'Z') {
405 /* 'Z' = Zulu = UTC */
407 mktime_with_utc
= TRUE
;
411 if (!mktime_with_utc
)
413 /* No timezone specified. */
416 mktime_with_utc
= TRUE
;
426 if (rest
!= NULL
&& *str
!= '\0') {
427 /* Strip trailing whitespace */
428 while (g_ascii_isspace(*str
))
436 retval
= mktime_utc(&t
);
443 if (tzoff
!= PURPLE_NO_TZ_OFF
)
453 purple_str_to_date_time(const char *timestamp
, gboolean utc
)
462 gint microseconds
= 0;
464 GTimeZone
*tz
= NULL
;
467 g_return_val_if_fail(timestamp
!= NULL
, NULL
);
471 /* Strip leading whitespace */
472 while (g_ascii_isspace(*str
))
479 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
484 if (sscanf(str
, "%04d", &year
) && year
> 0) {
487 if (*str
== '-' || *str
== '/')
492 if (!sscanf(str
, "%02d", &month
)) {
498 if (*str
== '-' || *str
== '/')
502 if (!sscanf(str
, "%02d", &day
)) {
508 /* Grab the year off the end if there's still stuff */
509 if (*str
== '/' || *str
== '-') {
510 /* But make sure we don't read the year twice */
517 if (!sscanf(str
, "%04d", &year
)) {
520 } else if (*str
== 'T' || *str
== '.') {
523 /* Continue grabbing the hours/minutes/seconds */
524 if ((sscanf(str
, "%02d:%02d:%02d", &hour
, &minute
, &seconds
) == 3 &&
526 (sscanf(str
, "%02d%02d%02d", &hour
, &minute
, &seconds
) == 3 &&
531 if (sscanf(str
, "%d%n", µseconds
, &chars
) == 1) {
537 const gchar
*end
= str
;
538 if (*end
== '+' || *end
== '-') {
542 while (isdigit(*end
) || *end
== ':') {
547 /* Trim anything trailing a purely numeric time zone. */
548 gchar
*tzstr
= g_strndup(str
, end
- str
);
549 tz
= g_time_zone_new(tzstr
);
552 /* Just try whatever is there. */
553 tz
= g_time_zone_new(str
);
560 /* No timezone specified. */
562 tz
= g_time_zone_new_utc();
564 tz
= g_time_zone_new_local();
568 retval
= g_date_time_new(tz
, year
, month
, day
, hour
, minute
,
569 seconds
+ microseconds
* pow(10, -chars
));
570 g_time_zone_unref(tz
);
576 purple_uts35_to_str(const char *format
, size_t len
, struct tm
*tm
)
582 time_t now
= time(NULL
);
583 tm
= localtime(&now
);
586 string
= g_string_sized_new(len
);
590 while ((i
+ count
) < len
&& format
[i
] == format
[i
+count
])
598 } else if (count
== 4) {
600 } else if (count
>= 5) {
610 /* Two-digits only */
611 g_string_append(string
, purple_utf8_strftime("%y", tm
));
614 g_string_append_printf(string
, "%0*d",
620 /* Year (in "Week of Year" based calendars) */
623 /* Two-digits only */
633 /* Cyclic Year Name */
637 } else if (count
== 4) {
639 } else if (count
>= 5) {
650 } else if (count
== 3) {
652 } else if (count
>= 4) {
658 /* Stand-alone Quarter */
662 } else if (count
== 3) {
664 } else if (count
>= 4) {
674 g_string_append(string
, purple_utf8_strftime("%m", tm
));
675 } else if (count
== 3) {
677 g_string_append(string
, purple_utf8_strftime("%b", tm
));
678 } else if (count
== 4) {
680 g_string_append(string
, purple_utf8_strftime("%B", tm
));
681 } else if (count
>= 5) {
682 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
687 /* Stand-alone Month */
691 g_string_append(string
, purple_utf8_strftime("%m", tm
));
692 } else if (count
== 3) {
694 g_string_append(string
, purple_utf8_strftime("%b", tm
));
695 } else if (count
== 4) {
697 g_string_append(string
, purple_utf8_strftime("%B", tm
));
698 } else if (count
>= 5) {
699 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
711 g_string_append(string
, purple_utf8_strftime("%W", tm
));
712 count
= MIN(count
, 2);
723 g_string_append(string
, purple_utf8_strftime("%d", tm
));
724 count
= MIN(count
, 2);
729 g_string_append(string
, purple_utf8_strftime("%j", tm
));
730 count
= MIN(count
, 3);
733 /* Day of Year in Month */
738 /* Modified Julian Day */
747 g_string_append(string
, purple_utf8_strftime("%a", tm
));
748 } else if (count
== 4) {
750 g_string_append(string
, purple_utf8_strftime("%A", tm
));
751 } else if (count
>= 5) {
753 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
758 /* Local Day of Week */
762 g_string_append(string
, purple_utf8_strftime("%u", tm
));
763 } else if (count
== 3) {
765 g_string_append(string
, purple_utf8_strftime("%a", tm
));
766 } else if (count
== 4) {
768 g_string_append(string
, purple_utf8_strftime("%A", tm
));
769 } else if (count
>= 5) {
771 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
776 /* Stand-alone Local Day of Week */
780 g_string_append(string
, purple_utf8_strftime("%u", tm
));
782 } else if (count
== 3) {
784 g_string_append(string
, purple_utf8_strftime("%a", tm
));
785 } else if (count
== 4) {
787 g_string_append(string
, purple_utf8_strftime("%A", tm
));
788 } else if (count
>= 5) {
790 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
798 g_string_append(string
, purple_utf8_strftime("%p", tm
));
806 g_string_append(string
, purple_utf8_strftime("%I", tm
));
807 } else if (count
>= 2) {
809 g_string_append(string
, purple_utf8_strftime("%I", tm
));
818 g_string_append(string
, purple_utf8_strftime("%H", tm
));
819 } else if (count
>= 2) {
821 g_string_append(string
, purple_utf8_strftime("%H", tm
));
830 } else if (count
>= 2) {
840 } else if (count
>= 2) {
846 /* Hour (hHkK by locale) */
853 g_string_append(string
, purple_utf8_strftime("%M", tm
));
854 count
= MIN(count
, 2);
860 g_string_append(string
, purple_utf8_strftime("%S", tm
));
861 count
= MIN(count
, 2);
864 /* Fractional Sub-second */
873 /* Time Zone (specific non-location format) */
877 } else if (count
>= 4) {
887 g_string_append(string
, purple_utf8_strftime("%z", tm
));
888 } else if (count
== 4) {
890 } else if (count
>= 5) {
892 g_string_append(string
, purple_utf8_strftime("%z", tm
));
897 /* Time Zone (generic non-location format) */
901 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
903 } else if (count
>= 4) {
905 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
915 } else if (count
>= 4) {
916 /* Generic Location Format) */
917 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
924 g_string_append_len(string
, format
+ i
, count
);
931 return g_string_free(string
, FALSE
);
935 /**************************************************************************/
936 /* GLib Event Loop Functions */
937 /**************************************************************************/
939 void purple_timeout_reset(GSource
*source
, gint64 seconds_from_now
)
941 g_source_set_ready_time(source
, g_get_monotonic_time() + (seconds_from_now
* G_USEC_PER_SEC
));
945 /**************************************************************************
947 **************************************************************************/
950 * This function is stolen from glib's gmarkup.c and modified to not
951 * replace ' with '
953 static void append_escaped_text(GString
*str
,
954 const gchar
*text
, gssize length
)
966 next
= g_utf8_next_char (p
);
971 g_string_append (str
, "&");
975 g_string_append (str
, "<");
979 g_string_append (str
, ">");
983 g_string_append (str
, """);
987 c
= g_utf8_get_char (p
);
988 if ((0x1 <= c
&& c
<= 0x8) ||
989 (0xb <= c
&& c
<= 0xc) ||
990 (0xe <= c
&& c
<= 0x1f) ||
991 (0x7f <= c
&& c
<= 0x84) ||
992 (0x86 <= c
&& c
<= 0x9f))
993 g_string_append_printf (str
, "&#x%x;", c
);
995 g_string_append_len (str
, p
, next
- p
);
1003 /* This function is stolen from glib's gmarkup.c */
1004 gchar
*purple_markup_escape_text(const gchar
*text
, gssize length
)
1008 g_return_val_if_fail(text
!= NULL
, NULL
);
1011 length
= strlen(text
);
1013 /* prealloc at least as long as original text */
1014 str
= g_string_sized_new(length
);
1015 append_escaped_text(str
, text
, length
);
1017 return g_string_free(str
, FALSE
);
1021 purple_markup_unescape_entity(const char *text
, int *length
)
1026 if (!text
|| *text
!= '&')
1029 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1031 if(IS_ENTITY("&"))
1033 else if(IS_ENTITY("<"))
1035 else if(IS_ENTITY(">"))
1037 else if(IS_ENTITY(" "))
1039 else if(IS_ENTITY("©"))
1040 pln
= "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1041 else if(IS_ENTITY("""))
1043 else if(IS_ENTITY("®"))
1044 pln
= "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1045 else if(IS_ENTITY("'"))
1047 else if(text
[1] == '#' && (g_ascii_isxdigit(text
[2]) || text
[2] == 'x')) {
1049 const char *start
= text
+ 2;
1055 if (*start
== 'x') {
1060 pound
= g_ascii_strtoull(start
, &end
, base
);
1061 if (pound
== 0 || pound
> INT_MAX
|| *end
!= ';') {
1065 len
= (end
- text
) + 1;
1067 buflen
= g_unichar_to_utf8((gunichar
)pound
, buf
);
1080 purple_markup_get_css_property(const gchar
*style
,
1083 const gchar
*css_str
= style
;
1084 const gchar
*css_value_start
;
1085 const gchar
*css_value_end
;
1089 g_return_val_if_fail(opt
!= NULL
, NULL
);
1094 /* find the CSS property */
1097 /* skip whitespace characters */
1098 while (*css_str
&& g_ascii_isspace(*css_str
))
1100 if (!g_ascii_isalpha(*css_str
))
1102 if (g_ascii_strncasecmp(css_str
, opt
, strlen(opt
)))
1104 /* go to next css property positioned after the next ';' */
1105 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1115 /* find the CSS value position in the string */
1116 css_str
+= strlen(opt
);
1117 while (*css_str
&& g_ascii_isspace(*css_str
))
1119 if (*css_str
!= ':')
1122 while (*css_str
&& g_ascii_isspace(*css_str
))
1124 if (*css_str
== '\0' || *css_str
== '"' || *css_str
== ';')
1127 /* mark the CSS value */
1128 css_value_start
= css_str
;
1129 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1131 css_value_end
= css_str
- 1;
1133 /* Removes trailing whitespace */
1134 while (css_value_end
> css_value_start
&& g_ascii_isspace(*css_value_end
))
1137 tmp
= g_strndup(css_value_start
, css_value_end
- css_value_start
+ 1);
1138 ret
= purple_unescape_html(tmp
);
1144 gboolean
purple_markup_is_rtl(const char *html
)
1147 const gchar
*start
, *end
;
1148 gboolean res
= FALSE
;
1150 if (purple_markup_find_tag("span", html
, &start
, &end
, &attributes
))
1152 /* tmp is a member of attributes and is free with g_datalist_clear call */
1153 const char *tmp
= g_datalist_get_data(&attributes
, "dir");
1154 if (tmp
&& !g_ascii_strcasecmp(tmp
, "RTL"))
1158 tmp
= g_datalist_get_data(&attributes
, "style");
1161 char *tmp2
= purple_markup_get_css_property(tmp
, "direction");
1162 if (tmp2
&& !g_ascii_strcasecmp(tmp2
, "RTL"))
1168 g_datalist_clear(&attributes
);
1174 purple_markup_find_tag(const char *needle
, const char *haystack
,
1175 const char **start
, const char **end
, GData
**attributes
)
1178 const char *cur
= haystack
;
1180 gboolean found
= FALSE
;
1181 gboolean in_tag
= FALSE
;
1182 gboolean in_attr
= FALSE
;
1183 const char *in_quotes
= NULL
;
1186 g_return_val_if_fail( needle
!= NULL
, FALSE
);
1187 g_return_val_if_fail( *needle
!= '\0', FALSE
);
1188 g_return_val_if_fail( haystack
!= NULL
, FALSE
);
1189 g_return_val_if_fail( start
!= NULL
, FALSE
);
1190 g_return_val_if_fail( end
!= NULL
, FALSE
);
1191 g_return_val_if_fail(attributes
!= NULL
, FALSE
);
1193 needlelen
= strlen(needle
);
1194 g_datalist_init(&attribs
);
1196 while (*cur
&& !found
) {
1199 const char *close
= cur
;
1201 while (*close
&& *close
!= *in_quotes
)
1204 /* if we got the close quote, store the value and carry on from *
1205 * after it. if we ran to the end of the string, point to the NULL *
1206 * and we're outta here */
1208 /* only store a value if we have an attribute name */
1210 size_t len
= close
- cur
;
1211 char *val
= g_strndup(cur
, len
);
1213 g_datalist_set_data_full(&attribs
, name
, val
, g_free
);
1223 } else if (in_attr
) {
1224 const char *close
= cur
;
1226 while (*close
&& *close
!= '>' && *close
!= '"' &&
1227 *close
!= '\'' && *close
!= ' ' && *close
!= '=')
1230 /* if we got the equals, store the name of the attribute. if we got
1231 * the quote, save the attribute and go straight to quote mode.
1232 * otherwise the tag closed or we reached the end of the string,
1233 * so we can get outta here */
1241 size_t len
= close
- cur
;
1243 /* don't store a blank attribute name */
1246 name
= g_ascii_strdown(cur
, len
);
1264 /* swallow extra spaces inside tag */
1265 while (*cur
&& *cur
== ' ') cur
++;
1282 /* if we hit a < followed by the name of our tag... */
1283 if (*cur
== '<' && !g_ascii_strncasecmp(cur
+ 1, needle
, needlelen
)) {
1285 cur
= cur
+ needlelen
+ 1;
1287 /* if we're pointing at a space or a >, we found the right tag. if *
1288 * we're not, we've found a longer tag, so we need to skip to the *
1289 * >, but not being distracted by >s inside quotes. */
1290 if (*cur
== ' ' || *cur
== '>') {
1293 while (*cur
&& *cur
!= '"' && *cur
!= '\'' && *cur
!= '>') {
1296 while (*cur
&& *cur
!= '"')
1298 } else if (*cur
== '\'') {
1300 while (*cur
&& *cur
!= '\'')
1313 /* clean up any attribute name from a premature termination */
1317 *attributes
= attribs
;
1328 purple_markup_extract_info_field(const char *str
, int len
, PurpleNotifyUserInfo
*user_info
,
1329 const char *start_token
, int skip
,
1330 const char *end_token
, char check_value
,
1331 const char *no_value_token
,
1332 const char *display_name
, gboolean is_link
,
1333 const char *link_prefix
,
1334 PurpleInfoFieldFormatCallback format_cb
)
1338 g_return_val_if_fail(str
!= NULL
, FALSE
);
1339 g_return_val_if_fail(user_info
!= NULL
, FALSE
);
1340 g_return_val_if_fail(start_token
!= NULL
, FALSE
);
1341 g_return_val_if_fail(end_token
!= NULL
, FALSE
);
1342 g_return_val_if_fail(display_name
!= NULL
, FALSE
);
1344 p
= strstr(str
, start_token
);
1349 p
+= strlen(start_token
) + skip
;
1354 if (check_value
!= '\0' && *p
== check_value
)
1357 q
= strstr(p
, end_token
);
1359 /* Trim leading blanks */
1360 while (*p
!= '\n' && g_ascii_isspace(*p
)) {
1364 /* Trim trailing blanks */
1365 while (q
> p
&& g_ascii_isspace(*(q
- 1))) {
1369 /* Don't bother with null strings */
1373 if (q
!= NULL
&& (!no_value_token
||
1374 (no_value_token
&& strncmp(p
, no_value_token
,
1375 strlen(no_value_token
)))))
1377 GString
*dest
= g_string_new("");
1381 g_string_append(dest
, "<a href=\"");
1384 g_string_append(dest
, link_prefix
);
1386 if (format_cb
!= NULL
)
1388 char *reformatted
= format_cb(p
, q
- p
);
1389 g_string_append(dest
, reformatted
);
1390 g_free(reformatted
);
1393 g_string_append_len(dest
, p
, q
- p
);
1394 g_string_append(dest
, "\">");
1397 g_string_append(dest
, link_prefix
);
1399 g_string_append_len(dest
, p
, q
- p
);
1400 g_string_append(dest
, "</a>");
1404 if (format_cb
!= NULL
)
1406 char *reformatted
= format_cb(p
, q
- p
);
1407 g_string_append(dest
, reformatted
);
1408 g_free(reformatted
);
1411 g_string_append_len(dest
, p
, q
- p
);
1414 purple_notify_user_info_add_pair_html(user_info
, display_name
, dest
->str
);
1415 g_string_free(dest
, TRUE
);
1423 struct purple_parse_tag
{
1429 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1430 recommended in the GCC docs). It contains 'continue's that should
1431 affect the while-loop in purple_markup_html_to_xhtml and doing the
1432 above would break that.
1433 Also, remember to put braces in constructs that require them for
1434 multiple statements when using this macro. */
1435 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1436 const char *o = c + strlen("<" x); \
1437 const char *p = NULL, *q = NULL, *r = NULL; \
1438 /* o = iterating over full tag \
1439 * p = > (end of tag) \
1440 * q = start of quoted bit \
1441 * r = < inside tag \
1443 GString *innards = g_string_new(""); \
1445 if(!q && (*o == '\"' || *o == '\'') ) { \
1448 if(*o == *q) { /* end of quoted bit */ \
1449 char *unescaped = g_strndup(q+1, o-q-1); \
1450 char *escaped = g_markup_escape_text(unescaped, -1); \
1451 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1452 g_free(unescaped); \
1455 } else if(*c == '\\') { \
1458 } else if(*o == '<') { \
1460 } else if(*o == '>') { \
1464 innards = g_string_append_c(innards, *o); \
1468 if(p && !r) { /* got an end of tag and no other < earlier */\
1469 if(*(p-1) != '/') { \
1470 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1473 tags = g_list_prepend(tags, pt); \
1476 xhtml = g_string_append(xhtml, "<" y); \
1477 xhtml = g_string_append(xhtml, innards->str); \
1478 xhtml = g_string_append_c(xhtml, '>'); \
1481 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1483 xhtml = g_string_append(xhtml, "<"); \
1485 plain = g_string_append_c(plain, '<'); \
1488 g_string_free(innards, TRUE); \
1491 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1492 (*(c+strlen("<" x)) == '>' || \
1493 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1495 xhtml = g_string_append(xhtml, "<" y); \
1496 c += strlen("<" x); \
1498 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1501 tags = g_list_prepend(tags, pt); \
1503 xhtml = g_string_append_c(xhtml, '>'); \
1506 xhtml = g_string_append(xhtml, "/>");\
1508 c = strchr(c, '>') + 1; \
1511 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1512 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1514 purple_markup_html_to_xhtml(const char *html
, char **xhtml_out
,
1517 GString
*xhtml
= NULL
;
1518 GString
*plain
= NULL
;
1519 GString
*url
= NULL
;
1520 GString
*cdata
= NULL
;
1521 GList
*tags
= NULL
, *tag
;
1522 const char *c
= html
;
1525 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1530 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1532 g_return_if_fail(xhtml_out
!= NULL
|| plain_out
!= NULL
);
1535 xhtml
= g_string_new("");
1537 plain
= g_string_new("");
1541 if(*(c
+1) == '/') { /* closing tag */
1544 struct purple_parse_tag
*pt
= tag
->data
;
1545 if(!g_ascii_strncasecmp((c
+2), pt
->src_tag
, strlen(pt
->src_tag
)) && *(c
+strlen(pt
->src_tag
)+2) == '>') {
1546 c
+= strlen(pt
->src_tag
) + 3;
1553 struct purple_parse_tag
*pt
= tags
->data
;
1554 if(xhtml
&& !pt
->ignore
)
1555 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
1556 if(plain
&& purple_strequal(pt
->src_tag
, "a")) {
1557 /* if this is a link, we have to add the url to the plaintext, too */
1559 (!g_string_equal(cdata
, url
) && (g_ascii_strncasecmp(url
->str
, "mailto:", 7) != 0 ||
1560 g_utf8_collate(url
->str
+ 7, cdata
->str
) != 0)))
1561 g_string_append_printf(plain
, " <%s>", g_strstrip(purple_unescape_html(url
->str
)));
1563 g_string_free(cdata
, TRUE
);
1570 tags
= g_list_remove(tags
, pt
);
1574 tags
= g_list_delete_link(tags
, tag
);
1576 /* a closing tag we weren't expecting...
1577 * we'll let it slide, if it's really a tag...if it's
1578 * just a </ we'll escape it properly */
1579 const char *end
= c
+2;
1580 while(*end
&& g_ascii_isalpha(*end
))
1586 xhtml
= g_string_append(xhtml
, "<");
1588 plain
= g_string_append_c(plain
, '<');
1592 } else { /* opening tag */
1593 ALLOW_TAG("blockquote");
1603 /* we only allow html to start the message */
1607 ALLOW_TAG_ALT("i", "em");
1608 ALLOW_TAG_ALT("italic", "em");
1618 /* we skip <HR> because it's not legal in XHTML-IM. However,
1619 * we still want to send something sensible, so we put a
1620 * linebreak in its place. <BR> also needs special handling
1621 * because putting a </BR> to close it would just be dumb. */
1622 if((!g_ascii_strncasecmp(c
, "<br", 3)
1623 || !g_ascii_strncasecmp(c
, "<hr", 3))
1624 && (*(c
+3) == '>' ||
1625 !g_ascii_strncasecmp(c
+3, "/>", 2) ||
1626 !g_ascii_strncasecmp(c
+3, " />", 3))) {
1627 c
= strchr(c
, '>') + 1;
1629 xhtml
= g_string_append(xhtml
, "<br/>");
1630 if(plain
&& *c
!= '\n')
1631 plain
= g_string_append_c(plain
, '\n');
1634 if(!g_ascii_strncasecmp(c
, "<b>", 3) || !g_ascii_strncasecmp(c
, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c
, "<strong>", strlen("<strong>"))) {
1635 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1638 else if (*(c
+2) == 'o')
1639 pt
->src_tag
= "bold";
1641 pt
->src_tag
= "strong";
1642 pt
->dest_tag
= "span";
1643 tags
= g_list_prepend(tags
, pt
);
1644 c
= strchr(c
, '>') + 1;
1646 xhtml
= g_string_append(xhtml
, "<span style='font-weight: bold;'>");
1649 if(!g_ascii_strncasecmp(c
, "<u>", 3) || !g_ascii_strncasecmp(c
, "<underline>", strlen("<underline>"))) {
1650 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1651 pt
->src_tag
= *(c
+2) == '>' ? "u" : "underline";
1652 pt
->dest_tag
= "span";
1653 tags
= g_list_prepend(tags
, pt
);
1654 c
= strchr(c
, '>') + 1;
1656 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: underline;'>");
1659 if(!g_ascii_strncasecmp(c
, "<s>", 3) || !g_ascii_strncasecmp(c
, "<strike>", strlen("<strike>"))) {
1660 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1661 pt
->src_tag
= *(c
+2) == '>' ? "s" : "strike";
1662 pt
->dest_tag
= "span";
1663 tags
= g_list_prepend(tags
, pt
);
1664 c
= strchr(c
, '>') + 1;
1666 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: line-through;'>");
1669 if(!g_ascii_strncasecmp(c
, "<sub>", 5)) {
1670 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1671 pt
->src_tag
= "sub";
1672 pt
->dest_tag
= "span";
1673 tags
= g_list_prepend(tags
, pt
);
1674 c
= strchr(c
, '>') + 1;
1676 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:sub;'>");
1679 if(!g_ascii_strncasecmp(c
, "<sup>", 5)) {
1680 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1681 pt
->src_tag
= "sup";
1682 pt
->dest_tag
= "span";
1683 tags
= g_list_prepend(tags
, pt
);
1684 c
= strchr(c
, '>') + 1;
1686 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:super;'>");
1689 if (!g_ascii_strncasecmp(c
, "<img", 4) && (*(c
+4) == '>' || *(c
+4) == ' ')) {
1690 const char *p
= c
+ 4;
1691 GString
*src
= NULL
, *alt
= NULL
;
1692 #define ESCAPE(from, to) \
1693 CHECK_QUOTE(from); \
1694 while (VALID_CHAR(from)) { \
1696 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1697 to = g_string_append(to, "&"); \
1698 else if (*from == '\'') \
1699 to = g_string_append(to, "'"); \
1701 to = g_string_append_c(to, *from); \
1705 while (*p
&& *p
!= '>') {
1706 if (!g_ascii_strncasecmp(p
, "src=", 4)) {
1707 const char *q
= p
+ 4;
1709 g_string_free(src
, TRUE
);
1710 src
= g_string_new("");
1713 } else if (!g_ascii_strncasecmp(p
, "alt=", 4)) {
1714 const char *q
= p
+ 4;
1716 g_string_free(alt
, TRUE
);
1717 alt
= g_string_new("");
1725 if ((c
= strchr(p
, '>')) != NULL
)
1729 /* src and alt are required! */
1731 g_string_append_printf(xhtml
, "<img src='%s' alt='%s' />", g_strstrip(src
->str
), alt
? alt
->str
: "");
1734 plain
= g_string_append(plain
, purple_unescape_html(alt
->str
));
1736 xhtml
= g_string_append(xhtml
, alt
->str
);
1737 g_string_free(alt
, TRUE
);
1739 g_string_free(src
, TRUE
);
1742 if (!g_ascii_strncasecmp(c
, "<a", 2) && (*(c
+2) == '>' || *(c
+2) == ' ')) {
1743 const char *p
= c
+ 2;
1744 struct purple_parse_tag
*pt
;
1745 while (*p
&& *p
!= '>') {
1746 if (!g_ascii_strncasecmp(p
, "href=", 5)) {
1747 const char *q
= p
+ 5;
1749 g_string_free(url
, TRUE
);
1750 url
= g_string_new("");
1752 g_string_free(cdata
, TRUE
);
1753 cdata
= g_string_new("");
1755 while (VALID_CHAR(q
)) {
1757 if ((*q
== '&') && (purple_markup_unescape_entity(q
, &len
) == NULL
))
1758 url
= g_string_append(url
, "&");
1760 url
= g_string_append(url
, """);
1762 url
= g_string_append_c(url
, *q
);
1770 if ((c
= strchr(p
, '>')) != NULL
)
1774 pt
= g_new0(struct purple_parse_tag
, 1);
1777 tags
= g_list_prepend(tags
, pt
);
1779 g_string_append_printf(xhtml
, "<a href=\"%s\">", url
? g_strstrip(url
->str
) : "");
1782 #define ESCAPE(from, to) \
1783 CHECK_QUOTE(from); \
1784 while (VALID_CHAR(from)) { \
1786 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
1787 to = g_string_append(to, "&"); \
1788 else if (*from == '\'') \
1789 to = g_string_append_c(to, '\"'); \
1791 to = g_string_append_c(to, *from); \
1794 if(!g_ascii_strncasecmp(c
, "<font", 5) && (*(c
+5) == '>' || *(c
+5) == ' ')) {
1795 const char *p
= c
+ 5;
1796 GString
*style
= g_string_new("");
1797 struct purple_parse_tag
*pt
;
1798 while (*p
&& *p
!= '>') {
1799 if (!g_ascii_strncasecmp(p
, "back=", 5)) {
1800 const char *q
= p
+ 5;
1801 GString
*color
= g_string_new("");
1803 g_string_append_printf(style
, "background: %s; ", color
->str
);
1804 g_string_free(color
, TRUE
);
1806 } else if (!g_ascii_strncasecmp(p
, "color=", 6)) {
1807 const char *q
= p
+ 6;
1808 GString
*color
= g_string_new("");
1810 g_string_append_printf(style
, "color: %s; ", color
->str
);
1811 g_string_free(color
, TRUE
);
1813 } else if (!g_ascii_strncasecmp(p
, "face=", 5)) {
1814 const char *q
= p
+ 5;
1815 GString
*face
= g_string_new("");
1817 g_string_append_printf(style
, "font-family: %s; ", g_strstrip(face
->str
));
1818 g_string_free(face
, TRUE
);
1820 } else if (!g_ascii_strncasecmp(p
, "size=", 5)) {
1821 const char *q
= p
+ 5;
1823 const char *size
= "medium";
1850 g_string_append_printf(style
, "font-size: %s; ", size
);
1856 if ((c
= strchr(p
, '>')) != NULL
)
1860 pt
= g_new0(struct purple_parse_tag
, 1);
1861 pt
->src_tag
= "font";
1862 pt
->dest_tag
= "span";
1863 tags
= g_list_prepend(tags
, pt
);
1864 if(style
->len
&& xhtml
)
1865 g_string_append_printf(xhtml
, "<span style='%s'>", g_strstrip(style
->str
));
1868 g_string_free(style
, TRUE
);
1872 if (!g_ascii_strncasecmp(c
, "<body ", 6)) {
1873 const char *p
= c
+ 6;
1874 gboolean did_something
= FALSE
;
1875 while (*p
&& *p
!= '>') {
1876 if (!g_ascii_strncasecmp(p
, "bgcolor=", 8)) {
1877 const char *q
= p
+ 8;
1878 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1879 GString
*color
= g_string_new("");
1881 while (VALID_CHAR(q
)) {
1882 color
= g_string_append_c(color
, *q
);
1886 g_string_append_printf(xhtml
, "<span style='background: %s;'>", g_strstrip(color
->str
));
1887 g_string_free(color
, TRUE
);
1888 if ((c
= strchr(p
, '>')) != NULL
)
1892 pt
->src_tag
= "body";
1893 pt
->dest_tag
= "span";
1894 tags
= g_list_prepend(tags
, pt
);
1895 did_something
= TRUE
;
1900 if (did_something
) continue;
1902 /* this has to come after the special case for bgcolor */
1904 if(!g_ascii_strncasecmp(c
, "<!--", strlen("<!--"))) {
1905 char *p
= strstr(c
+ strlen("<!--"), "-->");
1908 xhtml
= g_string_append(xhtml
, "<!--");
1909 c
+= strlen("<!--");
1915 xhtml
= g_string_append(xhtml
, "<");
1917 plain
= g_string_append_c(plain
, '<');
1920 } else if(*c
== '&') {
1925 if ((pln
= purple_markup_unescape_entity(c
, &len
)) == NULL
) {
1927 g_snprintf(buf
, sizeof(buf
), "%c", *c
);
1931 xhtml
= g_string_append_len(xhtml
, c
, len
);
1933 plain
= g_string_append(plain
, pln
);
1935 cdata
= g_string_append_len(cdata
, c
, len
);
1939 xhtml
= g_string_append_c(xhtml
, *c
);
1941 plain
= g_string_append_c(plain
, *c
);
1943 cdata
= g_string_append_c(cdata
, *c
);
1948 for (tag
= tags
; tag
; tag
= tag
->next
) {
1949 struct purple_parse_tag
*pt
= tag
->data
;
1951 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
1956 *xhtml_out
= g_string_free(xhtml
, FALSE
);
1958 *plain_out
= g_string_free(plain
, FALSE
);
1960 g_string_free(url
, TRUE
);
1962 g_string_free(cdata
, TRUE
);
1967 /* The following are probably reasonable changes:
1968 * - \n should be converted to a normal space
1969 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
1970 * - We want to turn </td>#whitespace<td> sequences into a single tab
1971 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
1972 * - <script>...</script> and <style>...</style> should be completely removed
1976 purple_markup_strip_html(const char *str
)
1978 int i
, j
, k
, entlen
;
1979 gboolean visible
= TRUE
;
1980 gboolean closing_td_p
= FALSE
;
1982 const gchar
*cdata_close_tag
= NULL
, *ent
;
1989 str2
= g_strdup(str
);
1991 for (i
= 0, j
= 0; str2
[i
]; i
++)
1995 if (cdata_close_tag
)
1997 /* Note: Don't even assume any other tag is a tag in CDATA */
1998 if (g_ascii_strncasecmp(str2
+ i
, cdata_close_tag
,
1999 strlen(cdata_close_tag
)) == 0)
2001 i
+= strlen(cdata_close_tag
) - 1;
2002 cdata_close_tag
= NULL
;
2006 else if (g_ascii_strncasecmp(str2
+ i
, "<td", 3) == 0 && closing_td_p
)
2011 else if (g_ascii_strncasecmp(str2
+ i
, "</td>", 5) == 0)
2013 closing_td_p
= TRUE
;
2018 closing_td_p
= FALSE
;
2024 if(g_ascii_isspace(str2
[k
]))
2028 /* Scan until we end the tag either implicitly (closed start
2029 * tag) or explicitly, using a sloppy method (i.e., < or >
2030 * inside quoted attributes will screw us up)
2032 while (str2
[k
] && str2
[k
] != '<' && str2
[k
] != '>')
2037 /* If we've got an <a> tag with an href, save the address
2038 * to print later. */
2039 if (g_ascii_strncasecmp(str2
+ i
, "<a", 2) == 0 &&
2040 g_ascii_isspace(str2
[i
+2]))
2042 int st
; /* start of href, inclusive [ */
2043 int end
; /* end of href, exclusive ) */
2045 /* Find start of href */
2046 for (st
= i
+ 3; st
< k
; st
++)
2048 if (g_ascii_strncasecmp(str2
+st
, "href=", 5) == 0)
2051 if (str2
[st
] == '"' || str2
[st
] == '\'')
2059 /* find end of address */
2060 for (end
= st
; end
< k
&& str2
[end
] != delim
; end
++)
2062 /* All the work is done in the loop construct above. */
2065 /* If there's an address, save it. If there was
2066 * already one saved, kill it. */
2071 tmp
= g_strndup(str2
+ st
, end
- st
);
2072 href
= purple_unescape_html(tmp
);
2078 /* Replace </a> with an ascii representation of the
2079 * address the link was pointing to. */
2080 else if (href
!= NULL
&& g_ascii_strncasecmp(str2
+ i
, "</a>", 4) == 0)
2082 size_t hrlen
= strlen(href
);
2084 /* Only insert the href if it's different from the CDATA. */
2085 if ((hrlen
!= (gsize
)(j
- href_st
) ||
2086 strncmp(str2
+ href_st
, href
, hrlen
)) &&
2087 (hrlen
!= (gsize
)(j
- href_st
+ 7) || /* 7 == strlen("http://") */
2088 strncmp(str2
+ href_st
, href
+ 7, hrlen
- 7)))
2092 memmove(str2
+ j
, href
, hrlen
);
2100 /* Check for tags which should be mapped to newline (but ignore some of
2101 * the tags at the beginning of the text) */
2102 else if ((j
&& (g_ascii_strncasecmp(str2
+ i
, "<p>", 3) == 0
2103 || g_ascii_strncasecmp(str2
+ i
, "<tr", 3) == 0
2104 || g_ascii_strncasecmp(str2
+ i
, "<hr", 3) == 0
2105 || g_ascii_strncasecmp(str2
+ i
, "<li", 3) == 0
2106 || g_ascii_strncasecmp(str2
+ i
, "<div", 4) == 0))
2107 || g_ascii_strncasecmp(str2
+ i
, "<br", 3) == 0
2108 || g_ascii_strncasecmp(str2
+ i
, "</table>", 8) == 0)
2112 /* Check for tags which begin CDATA and need to be closed */
2113 else if (g_ascii_strncasecmp(str2
+ i
, "<script", 7) == 0)
2115 cdata_close_tag
= "</script>";
2117 else if (g_ascii_strncasecmp(str2
+ i
, "<style", 6) == 0)
2119 cdata_close_tag
= "</style>";
2121 /* Update the index and continue checking after the tag */
2122 i
= (str2
[k
] == '<' || str2
[k
] == '\0')? k
- 1: k
;
2126 else if (cdata_close_tag
)
2130 else if (!g_ascii_isspace(str2
[i
]))
2135 if (str2
[i
] == '&' && (ent
= purple_markup_unescape_entity(str2
+ i
, &entlen
)) != NULL
)
2144 str2
[j
++] = g_ascii_isspace(str2
[i
])? ' ': str2
[i
];
2173 badentity(const char *c
)
2175 if (!g_ascii_strncasecmp(c
, "<", 4) ||
2176 !g_ascii_strncasecmp(c
, ">", 4) ||
2177 !g_ascii_strncasecmp(c
, """, 6)) {
2184 process_link(GString
*ret
,
2185 const char *start
, const char *c
,
2187 const char *urlprefix
,
2190 char *url_buf
, *tmpurlbuf
;
2194 if (!badchar(*t
) && !badentity(t
))
2197 if (t
- c
== matchlen
)
2200 if (*t
== ',' && *(t
+ 1) != ' ') {
2204 if (t
> start
&& *(t
- 1) == '.')
2206 if (t
> start
&& *(t
- 1) == ')' && inside_paren
> 0)
2209 url_buf
= g_strndup(c
, t
- c
);
2210 tmpurlbuf
= purple_unescape_html(url_buf
);
2211 g_string_append_printf(ret
, "<A HREF=\"%s%s\">%s</A>",
2213 tmpurlbuf
, url_buf
);
2223 purple_markup_linkify(const char *text
)
2225 const char *c
, *t
, *q
= NULL
;
2226 char *tmpurlbuf
, *url_buf
;
2228 gboolean inside_html
= FALSE
;
2229 int inside_paren
= 0;
2235 ret
= g_string_new("");
2240 if(*c
== '(' && !inside_html
) {
2242 ret
= g_string_append_c(ret
, *c
);
2248 inside_html
= FALSE
;
2249 } else if(!q
&& (*c
== '\"' || *c
== '\'')) {
2255 } else if(*c
== '<') {
2257 if (!g_ascii_strncasecmp(c
, "<A", 2)) {
2259 if (!g_ascii_strncasecmp(c
, "/A>", 3)) {
2260 inside_html
= FALSE
;
2263 ret
= g_string_append_c(ret
, *c
);
2269 } else if (!g_ascii_strncasecmp(c
, "http://", 7)) {
2270 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2271 } else if (!g_ascii_strncasecmp(c
, "https://", 8)) {
2272 c
= process_link(ret
, text
, c
, 8, "", inside_paren
);
2273 } else if (!g_ascii_strncasecmp(c
, "ftp://", 6)) {
2274 c
= process_link(ret
, text
, c
, 6, "", inside_paren
);
2275 } else if (!g_ascii_strncasecmp(c
, "sftp://", 7)) {
2276 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2277 } else if (!g_ascii_strncasecmp(c
, "file://", 7)) {
2278 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2279 } else if (!g_ascii_strncasecmp(c
, "www.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2280 c
= process_link(ret
, text
, c
, 4, "http://", inside_paren
);
2281 } else if (!g_ascii_strncasecmp(c
, "ftp.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2282 c
= process_link(ret
, text
, c
, 4, "ftp://", inside_paren
);
2283 } else if (!g_ascii_strncasecmp(c
, "xmpp:", 5) && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2284 c
= process_link(ret
, text
, c
, 5, "", inside_paren
);
2285 } else if (!g_ascii_strncasecmp(c
, "mailto:", 7)) {
2288 if (badchar(*t
) || badentity(t
)) {
2293 if (t
> text
&& *(t
- 1) == '.')
2295 if ((d
= strstr(c
+ 7, "?")) != NULL
&& d
< t
)
2296 url_buf
= g_strndup(c
+ 7, d
- c
- 7);
2298 url_buf
= g_strndup(c
+ 7, t
- c
- 7);
2299 if (!purple_email_is_valid(url_buf
)) {
2304 url_buf
= g_strndup(c
, t
- c
);
2305 tmpurlbuf
= purple_unescape_html(url_buf
);
2306 g_string_append_printf(ret
, "<A HREF=\"%s\">%s</A>",
2307 tmpurlbuf
, url_buf
);
2315 } else if (c
!= text
&& (*c
== '@')) {
2317 GString
*gurl_buf
= NULL
;
2318 const char illegal_chars
[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2320 if (strchr(illegal_chars
,*(c
- 1)) || strchr(illegal_chars
, *(c
+ 1)))
2324 gurl_buf
= g_string_new("");
2329 /* iterate backwards grabbing the local part of an email address */
2330 g
= g_utf8_get_char(t
);
2331 if (badchar(*t
) || (g
>= 127) || (*t
== '(') ||
2332 ((*t
== ';') && ((t
> (text
+2) && (!g_ascii_strncasecmp(t
- 3, "<", 4) ||
2333 !g_ascii_strncasecmp(t
- 3, ">", 4))) ||
2334 (t
> (text
+4) && (!g_ascii_strncasecmp(t
- 5, """, 6)))))) {
2335 /* local part will already be part of ret, strip it out */
2336 ret
= g_string_truncate(ret
, ret
->len
- (c
- t
));
2337 ret
= g_string_append_unichar(ret
, g
);
2340 g_string_prepend_unichar(gurl_buf
, g
);
2341 t
= g_utf8_find_prev_char(text
, t
);
2343 ret
= g_string_assign(ret
, "");
2349 t
= g_utf8_find_next_char(c
, NULL
);
2352 /* iterate forwards grabbing the domain part of an email address */
2353 g
= g_utf8_get_char(t
);
2354 if (badchar(*t
) || (g
>= 127) || (*t
== ')') || badentity(t
)) {
2357 url_buf
= g_string_free(gurl_buf
, FALSE
);
2359 /* strip off trailing periods */
2361 for (d
= url_buf
+ strlen(url_buf
) - 1; *d
== '.'; d
--, t
--)
2365 tmpurlbuf
= purple_unescape_html(url_buf
);
2366 if (purple_email_is_valid(tmpurlbuf
)) {
2367 g_string_append_printf(ret
, "<A HREF=\"mailto:%s\">%s</A>",
2368 tmpurlbuf
, url_buf
);
2370 g_string_append(ret
, url_buf
);
2378 g_string_append_unichar(gurl_buf
, g
);
2379 t
= g_utf8_find_next_char(t
, NULL
);
2384 if(*c
== ')' && !inside_html
) {
2386 ret
= g_string_append_c(ret
, *c
);
2393 ret
= g_string_append_c(ret
, *c
);
2397 return g_string_free(ret
, FALSE
);
2400 char *purple_unescape_text(const char *in
)
2408 ret
= g_string_new("");
2413 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2414 g_string_append(ret
, ent
);
2417 g_string_append_c(ret
, *c
);
2422 return g_string_free(ret
, FALSE
);
2425 char *purple_unescape_html(const char *html
)
2428 const char *c
= html
;
2433 ret
= g_string_new("");
2438 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2439 g_string_append(ret
, ent
);
2441 } else if (!strncmp(c
, "<br>", 4)) {
2442 g_string_append_c(ret
, '\n');
2445 g_string_append_c(ret
, *c
);
2450 return g_string_free(ret
, FALSE
);
2454 purple_markup_slice(const char *str
, guint x
, guint y
)
2459 gboolean appended
= FALSE
;
2463 g_return_val_if_fail(str
!= NULL
, NULL
);
2464 g_return_val_if_fail(x
<= y
, NULL
);
2467 return g_strdup("");
2469 ret
= g_string_new("");
2472 while (*str
&& (z
< y
)) {
2473 c
= g_utf8_get_char(str
);
2476 char *end
= strchr(str
, '>');
2479 g_string_free(ret
, TRUE
);
2480 while ((tag
= g_queue_pop_head(q
)))
2486 if (!g_ascii_strncasecmp(str
, "<img ", 5)) {
2487 z
+= strlen("[Image]");
2488 } else if (!g_ascii_strncasecmp(str
, "<br", 3)) {
2490 } else if (!g_ascii_strncasecmp(str
, "<hr>", 4)) {
2491 z
+= strlen("\n---\n");
2492 } else if (!g_ascii_strncasecmp(str
, "</", 2)) {
2496 tmp
= g_queue_pop_head(q
);
2500 /* push it unto the stack */
2503 tmp
= g_strndup(str
, end
- str
+ 1);
2504 g_queue_push_head(q
, tmp
);
2509 g_string_append_len(ret
, str
, end
- str
+ 1);
2513 } else if (c
== '&') {
2514 char *end
= strchr(str
, ';');
2516 g_string_free(ret
, TRUE
);
2517 while ((tag
= g_queue_pop_head(q
)))
2525 g_string_append_len(ret
, str
, end
- str
+ 1);
2530 if (z
== x
&& z
> 0 && !appended
) {
2535 g_string_append(ret
, tag
);
2542 g_string_append_unichar(ret
, c
);
2546 str
= g_utf8_next_char(str
);
2549 while ((tag
= g_queue_pop_head(q
))) {
2552 name
= purple_markup_get_tag_name(tag
);
2553 g_string_append_printf(ret
, "</%s>", name
);
2559 return g_string_free(ret
, FALSE
);
2563 purple_markup_get_tag_name(const char *tag
)
2566 g_return_val_if_fail(tag
!= NULL
, NULL
);
2567 g_return_val_if_fail(*tag
== '<', NULL
);
2569 for (i
= 1; tag
[i
]; i
++)
2570 if (tag
[i
] == '>' || tag
[i
] == ' ' || tag
[i
] == '/')
2573 return g_strndup(tag
+1, i
-1);
2576 /**************************************************************************
2577 * Path/Filename Functions
2578 **************************************************************************/
2580 purple_home_dir(void)
2583 return g_get_home_dir();
2585 return wpurple_home_dir();
2589 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2591 purple_user_dir(void)
2593 if (custom_user_dir
!= NULL
)
2594 return custom_user_dir
;
2596 user_dir
= g_build_filename(purple_home_dir(), ".purple", NULL
);
2601 static const gchar
*
2602 purple_xdg_dir(gchar
**xdg_dir
, const gchar
*xdg_base_dir
, const gchar
*xdg_type
)
2605 if (!custom_user_dir
) {
2606 *xdg_dir
= g_build_filename(xdg_base_dir
, "purple", NULL
);
2608 *xdg_dir
= g_build_filename(custom_user_dir
, xdg_type
, NULL
);
2616 purple_cache_dir(void)
2618 return purple_xdg_dir(&cache_dir
, g_get_user_cache_dir(), "cache");
2622 purple_config_dir(void)
2624 return purple_xdg_dir(&config_dir
, g_get_user_config_dir(), "config");
2628 purple_data_dir(void)
2630 return purple_xdg_dir(&data_dir
, g_get_user_data_dir(), "data");
2634 purple_move_to_xdg_base_dir(const char *purple_xdg_dir
, char *path
)
2638 gboolean xdg_path_exists
;
2640 /* Create destination directory */
2641 mkdir_res
= purple_build_dir(purple_xdg_dir
, S_IRWXU
);
2642 if (mkdir_res
== -1) {
2643 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
2644 purple_xdg_dir
, g_strerror(errno
));
2648 xdg_path
= g_build_filename(purple_xdg_dir
, path
, NULL
);
2649 xdg_path_exists
= g_file_test(xdg_path
, G_FILE_TEST_EXISTS
);
2650 if (!xdg_path_exists
) {
2652 gboolean old_path_exists
;
2654 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2655 old_path
= g_build_filename(purple_user_dir(), path
, NULL
);
2656 G_GNUC_END_IGNORE_DEPRECATIONS
2657 old_path_exists
= g_file_test(old_path
, G_FILE_TEST_EXISTS
);
2658 if (old_path_exists
) {
2661 rename_res
= g_rename(old_path
, xdg_path
);
2662 if (rename_res
== -1) {
2663 purple_debug_error("util", "Error renaming %s to %s; failed migration\n",
2664 old_path
, xdg_path
);
2680 void purple_util_set_user_dir(const char *dir
)
2682 g_free(custom_user_dir
);
2684 if (dir
!= NULL
&& *dir
)
2685 custom_user_dir
= g_strdup(dir
);
2687 custom_user_dir
= NULL
;
2690 int purple_build_dir(const char *path
, int mode
)
2692 return g_mkdir_with_parents(path
, mode
);
2696 purple_util_write_data_to_file_common(const char *dir
, const char *filename
, const char *data
, gssize size
)
2698 gchar
*filename_full
;
2699 gboolean ret
= FALSE
;
2701 g_return_val_if_fail(dir
!= NULL
, FALSE
);
2703 purple_debug_misc("util", "Writing file %s to directory %s",
2706 /* Ensure the directory exists */
2707 if (!g_file_test(dir
, G_FILE_TEST_IS_DIR
))
2709 if (g_mkdir(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
) == -1)
2711 purple_debug_error("util", "Error creating directory %s: %s\n",
2712 dir
, g_strerror(errno
));
2717 filename_full
= g_build_filename(dir
, filename
, NULL
);
2719 ret
= purple_util_write_data_to_file_absolute(filename_full
, data
, size
);
2721 g_free(filename_full
);
2726 purple_util_write_data_to_file(const char *filename
, const char *data
, gssize size
)
2728 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2729 const char *user_dir
= purple_user_dir();
2730 G_GNUC_END_IGNORE_DEPRECATIONS
2731 gboolean ret
= purple_util_write_data_to_file_common(user_dir
, filename
, data
, size
);
2737 purple_util_write_data_to_cache_file(const char *filename
, const char *data
, gssize size
)
2739 const char *cache_dir
= purple_cache_dir();
2740 gboolean ret
= purple_util_write_data_to_file_common(cache_dir
, filename
, data
, size
);
2746 purple_util_write_data_to_config_file(const char *filename
, const char *data
, gssize size
)
2748 const char *config_dir
= purple_config_dir();
2749 gboolean ret
= purple_util_write_data_to_file_common(config_dir
, filename
, data
, size
);
2755 purple_util_write_data_to_data_file(const char *filename
, const char *data
, gssize size
)
2757 const char *data_dir
= purple_data_dir();
2758 gboolean ret
= purple_util_write_data_to_file_common(data_dir
, filename
, data
, size
);
2764 purple_util_write_data_to_file_absolute(const char *filename_full
, const char *data
, gssize size
)
2769 g_return_val_if_fail(size
>= -1, FALSE
);
2772 size
= strlen(data
);
2775 file
= g_file_new_for_path(filename_full
);
2777 if (!g_file_replace_contents(file
, data
, size
, NULL
, FALSE
,
2778 G_FILE_CREATE_PRIVATE
, NULL
, NULL
, &err
)) {
2779 purple_debug_error("util", "Error writing file: %s: %s\n",
2780 filename_full
, err
->message
);
2781 g_clear_error(&err
);
2782 g_object_unref(file
);
2786 g_object_unref(file
);
2791 purple_util_read_xml_from_file(const char *filename
, const char *description
)
2793 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2794 return purple_xmlnode_from_file(purple_user_dir(), filename
, description
, "util");
2795 G_GNUC_END_IGNORE_DEPRECATIONS
2799 purple_util_read_xml_from_cache_file(const char *filename
, const char *description
)
2801 return purple_xmlnode_from_file(purple_cache_dir(), filename
, description
, "util");
2805 purple_util_read_xml_from_config_file(const char *filename
, const char *description
)
2807 return purple_xmlnode_from_file(purple_config_dir(), filename
, description
, "util");
2811 purple_util_read_xml_from_data_file(const char *filename
, const char *description
)
2813 return purple_xmlnode_from_file(purple_data_dir(), filename
, description
, "util");
2817 * Like mkstemp() but returns a file pointer, uses a pre-set template,
2818 * uses the semantics of tempnam() for the directory to use and allocates
2819 * the space for the filepath.
2821 * Caller is responsible for closing the file and removing it when done,
2822 * as well as freeing the space pointed-to by "path" with g_free().
2824 * Returns NULL on failure and cleans up after itself if so.
2826 static const char *purple_mkstemp_templ
= {"purpleXXXXXX"};
2829 purple_mkstemp(char **fpath
, gboolean binary
)
2831 const gchar
*tmpdir
;
2835 g_return_val_if_fail(fpath
!= NULL
, NULL
);
2837 if((tmpdir
= (gchar
*)g_get_tmp_dir()) != NULL
) {
2838 if((*fpath
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", tmpdir
, purple_mkstemp_templ
)) != NULL
) {
2839 fd
= g_mkstemp(*fpath
);
2841 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2842 "Couldn't make \"%s\", error: %d\n",
2845 if((fp
= fdopen(fd
, "r+")) == NULL
) {
2847 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2848 "Couldn't fdopen(), error: %d\n", errno
);
2858 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
2859 "g_get_tmp_dir() failed!\n");
2866 purple_program_is_valid(const char *program
)
2868 GError
*error
= NULL
;
2871 gboolean is_valid
= FALSE
;
2873 g_return_val_if_fail(program
!= NULL
, FALSE
);
2874 g_return_val_if_fail(*program
!= '\0', FALSE
);
2876 if (!g_shell_parse_argv(program
, NULL
, &argv
, &error
)) {
2877 purple_debug(PURPLE_DEBUG_ERROR
, "program_is_valid",
2878 "Could not parse program '%s': %s\n",
2879 program
, error
->message
);
2880 g_error_free(error
);
2888 progname
= g_find_program_in_path(argv
[0]);
2889 is_valid
= (progname
!= NULL
);
2891 if(purple_debug_is_verbose())
2892 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program
,
2893 is_valid
? "Valid" : "Invalid");
2903 purple_running_gnome(void)
2906 gchar
*tmp
= g_find_program_in_path("gvfs-open");
2909 tmp
= g_find_program_in_path("gnome-open");
2918 tmp
= (gchar
*)g_getenv("GNOME_DESKTOP_SESSION_ID");
2920 return ((tmp
!= NULL
) && (*tmp
!= '\0'));
2927 purple_running_kde(void)
2930 gchar
*tmp
= g_find_program_in_path("kfmclient");
2931 const char *session
;
2937 session
= g_getenv("KDE_FULL_SESSION");
2938 if (purple_strequal(session
, "true"))
2941 /* If you run Purple from Konsole under !KDE, this will provide a
2942 * a false positive. Since we do the GNOME checks first, this is
2943 * only a problem if you're running something !(KDE || GNOME) and
2944 * you run Purple from Konsole. This really shouldn't be a problem. */
2945 return ((g_getenv("KDEDIR") != NULL
) || g_getenv("KDEDIRS") != NULL
);
2952 purple_running_osx(void)
2954 #if defined(__APPLE__)
2961 typedef union purple_sockaddr
{
2963 struct sockaddr_in sa_in
;
2964 #if defined(AF_INET6)
2965 struct sockaddr_in6 sa_in6
;
2967 struct sockaddr_storage sa_stor
;
2971 purple_fd_get_ip(int fd
)
2973 PurpleSockaddr addr
;
2974 socklen_t namelen
= sizeof(addr
);
2977 g_return_val_if_fail(fd
!= 0, NULL
);
2979 if (getsockname(fd
, &(addr
.sa
), &namelen
))
2982 family
= addr
.sa
.sa_family
;
2984 if (family
== AF_INET
) {
2985 return g_strdup(inet_ntoa(addr
.sa_in
.sin_addr
));
2987 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
2988 else if (family
== AF_INET6
) {
2989 char host
[INET6_ADDRSTRLEN
];
2992 tmp
= inet_ntop(family
, &(addr
.sa_in6
.sin6_addr
), host
, sizeof(host
));
2993 return g_strdup(tmp
);
3001 purple_socket_get_family(int fd
)
3003 PurpleSockaddr addr
;
3004 socklen_t len
= sizeof(addr
);
3006 g_return_val_if_fail(fd
>= 0, -1);
3008 if (getsockname(fd
, &(addr
.sa
), &len
))
3011 return addr
.sa
.sa_family
;
3015 purple_socket_speaks_ipv4(int fd
)
3019 g_return_val_if_fail(fd
>= 0, FALSE
);
3021 family
= purple_socket_get_family(fd
);
3026 #if defined(IPV6_V6ONLY)
3030 socklen_t len
= sizeof(val
);
3032 if (getsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, &len
) != 0)
3042 /**************************************************************************
3044 **************************************************************************/
3046 purple_normalize(PurpleAccount
*account
, const char *str
)
3048 const char *ret
= NULL
;
3049 static char buf
[BUF_LEN
];
3051 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3052 g_return_val_if_fail(str
!= NULL
, "");
3054 if (account
!= NULL
)
3056 PurpleProtocol
*protocol
=
3057 purple_protocols_find(purple_account_get_protocol_id(account
));
3059 if (protocol
!= NULL
)
3060 ret
= purple_protocol_client_iface_normalize(protocol
, account
, str
);
3067 tmp
= g_utf8_normalize(str
, -1, G_NORMALIZE_DEFAULT
);
3068 g_snprintf(buf
, sizeof(buf
), "%s", tmp
);
3078 * You probably don't want to call this directly, it is
3079 * mainly for use as a protocol callback function. See the
3080 * comments in util.h.
3083 purple_normalize_nocase(const PurpleAccount
*account
, const char *str
)
3085 static char buf
[BUF_LEN
];
3088 g_return_val_if_fail(str
!= NULL
, NULL
);
3090 tmp1
= g_utf8_strdown(str
, -1);
3091 tmp2
= g_utf8_normalize(tmp1
, -1, G_NORMALIZE_DEFAULT
);
3092 g_snprintf(buf
, sizeof(buf
), "%s", tmp2
? tmp2
: "");
3100 purple_validate(const PurpleProtocol
*protocol
, const char *str
)
3102 const char *normalized
;
3104 g_return_val_if_fail(protocol
!= NULL
, FALSE
);
3105 g_return_val_if_fail(str
!= NULL
, FALSE
);
3110 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol
, CLIENT
, normalize
))
3113 normalized
= purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol
),
3116 return (NULL
!= normalized
);
3120 purple_strdup_withhtml(const gchar
*src
)
3122 gulong destsize
, i
, j
;
3125 g_return_val_if_fail(src
!= NULL
, NULL
);
3127 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3129 for (i
= 0; src
[i
] != '\0'; i
++)
3133 else if (src
[i
] != '\r')
3137 dest
= g_malloc(destsize
);
3139 /* Copy stuff, ignoring \r's, because they are dumb */
3140 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
3141 if (src
[i
] == '\n') {
3142 strcpy(&dest
[j
], "<BR>");
3144 } else if (src
[i
] != '\r')
3148 dest
[destsize
-1] = '\0';
3154 purple_str_has_prefix(const char *s
, const char *p
)
3156 return g_str_has_prefix(s
, p
);
3160 purple_str_has_caseprefix(const gchar
*s
, const gchar
*p
)
3162 g_return_val_if_fail(s
, FALSE
);
3163 g_return_val_if_fail(p
, FALSE
);
3165 return (g_ascii_strncasecmp(s
, p
, strlen(p
)) == 0);
3169 purple_str_has_suffix(const char *s
, const char *x
)
3171 return g_str_has_suffix(s
, x
);
3175 purple_str_add_cr(const char *text
)
3181 g_return_val_if_fail(text
!= NULL
, NULL
);
3183 if (text
[0] == '\n')
3185 for (i
= 1; i
< strlen(text
); i
++)
3186 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3190 return g_strdup(text
);
3192 ret
= g_malloc0(strlen(text
) + count
+ 1);
3195 if (text
[i
] == '\n')
3197 ret
[j
++] = text
[i
++];
3198 for (; i
< strlen(text
); i
++) {
3199 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3208 purple_str_strip_char(char *text
, char thechar
)
3212 g_return_if_fail(text
!= NULL
);
3214 for (i
= 0, j
= 0; text
[i
]; i
++)
3215 if (text
[i
] != thechar
)
3216 text
[j
++] = text
[i
];
3222 purple_util_chrreplace(char *string
, char delimiter
,
3227 g_return_if_fail(string
!= NULL
);
3229 while (string
[i
] != '\0')
3231 if (string
[i
] == delimiter
)
3232 string
[i
] = replacement
;
3238 purple_strreplace(const char *string
, const char *delimiter
,
3239 const char *replacement
)
3244 g_return_val_if_fail(string
!= NULL
, NULL
);
3245 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3246 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3248 split
= g_strsplit(string
, delimiter
, 0);
3249 ret
= g_strjoinv(replacement
, split
);
3256 purple_strcasereplace(const char *string
, const char *delimiter
,
3257 const char *replacement
)
3260 int length_del
, length_rep
, i
, j
;
3262 g_return_val_if_fail(string
!= NULL
, NULL
);
3263 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3264 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3266 length_del
= strlen(delimiter
);
3267 length_rep
= strlen(replacement
);
3269 /* Count how many times the delimiter appears */
3270 i
= 0; /* position in the source string */
3271 j
= 0; /* number of occurrences of "delimiter" */
3272 while (string
[i
] != '\0') {
3273 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3282 ret
= g_malloc(j
+1);
3284 i
= 0; /* position in the source string */
3285 j
= 0; /* position in the destination string */
3286 while (string
[i
] != '\0') {
3287 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3288 strncpy(&ret
[j
], replacement
, length_rep
);
3303 /** TODO: Expose this when we can add API */
3305 purple_strcasestr_len(const char *haystack
, gssize hlen
, const char *needle
, gssize nlen
)
3307 const char *tmp
, *ret
;
3309 g_return_val_if_fail(haystack
!= NULL
, NULL
);
3310 g_return_val_if_fail(needle
!= NULL
, NULL
);
3313 hlen
= strlen(haystack
);
3315 nlen
= strlen(needle
);
3319 g_return_val_if_fail(hlen
> 0, NULL
);
3320 g_return_val_if_fail(nlen
> 0, NULL
);
3322 while (*tmp
&& !ret
&& (hlen
- (tmp
- haystack
)) >= nlen
) {
3323 if (!g_ascii_strncasecmp(needle
, tmp
, nlen
))
3333 purple_strcasestr(const char *haystack
, const char *needle
)
3335 return purple_strcasestr_len(haystack
, -1, needle
, -1);
3339 purple_str_size_to_units(goffset size
)
3341 static const char * const size_str
[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3343 gsize size_index
= 0;
3346 return g_strdup(_("Calculating..."));
3348 else if (size
== 0) {
3349 return g_strdup(_("Unknown."));
3352 size_mag
= (float)size
;
3354 while ((size_index
< G_N_ELEMENTS(size_str
) - 1) && (size_mag
> 1024)) {
3359 if (size_index
== 0) {
3360 return g_strdup_printf("%" G_GOFFSET_FORMAT
" %s", size
, _(size_str
[size_index
]));
3362 return g_strdup_printf("%.2f %s", size_mag
, _(size_str
[size_index
]));
3368 purple_str_seconds_to_string(guint secs
)
3371 guint days
, hrs
, mins
;
3375 return g_strdup_printf(dngettext(PACKAGE
, "%d second", "%d seconds", secs
), secs
);
3378 days
= secs
/ (60 * 60 * 24);
3379 secs
= secs
% (60 * 60 * 24);
3380 hrs
= secs
/ (60 * 60);
3381 secs
= secs
% (60 * 60);
3383 /* secs = secs % 60; */
3387 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d day", "%d days", days
), days
);
3394 char *tmp
= g_strdup_printf(
3395 dngettext(PACKAGE
, "%s, %d hour", "%s, %d hours", hrs
),
3401 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d hour", "%d hours", hrs
), hrs
);
3408 char *tmp
= g_strdup_printf(
3409 dngettext(PACKAGE
, "%s, %d minute", "%s, %d minutes", mins
),
3415 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d minute", "%d minutes", mins
), mins
);
3423 purple_str_binary_to_ascii(const unsigned char *binary
, guint len
)
3428 g_return_val_if_fail(len
> 0, NULL
);
3430 ret
= g_string_sized_new(len
);
3432 for (i
= 0; i
< len
; i
++)
3433 if (binary
[i
] < 32 || binary
[i
] > 126)
3434 g_string_append_printf(ret
, "\\x%02x", binary
[i
] & 0xFF);
3435 else if (binary
[i
] == '\\')
3436 g_string_append(ret
, "\\\\");
3438 g_string_append_c(ret
, binary
[i
]);
3440 return g_string_free(ret
, FALSE
);
3444 purple_utf16_size(const gunichar2
*str
)
3446 /* UTF16 cannot contain two consequent NUL bytes starting at even
3447 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3453 g_return_val_if_fail(str
!= NULL
, 0);
3457 return i
* sizeof(gunichar2
);
3461 purple_str_wipe(gchar
*str
)
3465 memset(str
, 0, strlen(str
));
3470 purple_utf16_wipe(gunichar2
*str
)
3474 memset(str
, 0, purple_utf16_size(str
));
3478 /**************************************************************************
3480 **************************************************************************/
3482 void purple_got_protocol_handler_uri(const char *uri
)
3486 const char *tmp
, *param_string
;
3488 GHashTable
*params
= NULL
;
3490 if (!(tmp
= strchr(uri
, ':')) || tmp
== uri
) {
3491 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3495 len
= MIN(sizeof(proto
) - 1, (gsize
)(tmp
- uri
));
3497 strncpy(proto
, uri
, len
);
3502 if (purple_strequal(proto
, "xmpp"))
3507 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp
, proto
, delimiter
);
3509 if ((param_string
= strchr(tmp
, '?'))) {
3510 const char *keyend
= NULL
, *pairstart
;
3511 char *key
, *value
= NULL
;
3513 cmd
= g_strndup(tmp
, (param_string
- tmp
));
3516 params
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
3517 pairstart
= tmp
= param_string
;
3519 while (*tmp
|| *pairstart
) {
3520 if (*tmp
== delimiter
|| !(*tmp
)) {
3521 /* If there is no explicit value */
3522 if (keyend
== NULL
) {
3525 /* without these brackets, clang won't
3526 * recognize tmp as a non-NULL
3529 if (keyend
&& keyend
!= pairstart
) {
3531 key
= g_strndup(pairstart
, (keyend
- pairstart
));
3532 /* If there is an explicit value */
3533 if (keyend
!= tmp
&& keyend
!= (tmp
- 1))
3534 value
= g_strndup(keyend
+ 1, (tmp
- keyend
- 1));
3535 for (p
= key
; *p
; ++p
)
3536 *p
= g_ascii_tolower(*p
);
3537 g_hash_table_insert(params
, key
, value
);
3539 keyend
= value
= NULL
;
3540 pairstart
= (*tmp
) ? tmp
+ 1 : tmp
;
3541 } else if (*tmp
== '=')
3548 cmd
= g_strdup(tmp
);
3550 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto
, cmd
, params
);
3554 g_hash_table_destroy(params
);
3558 purple_url_decode(const char *str
)
3560 static char buf
[BUF_LEN
];
3565 g_return_val_if_fail(str
!= NULL
, NULL
);
3568 * XXX - This check could be removed and buf could be made
3569 * dynamically allocated, but this is easier.
3571 if (strlen(str
) >= BUF_LEN
)
3574 for (i
= 0; i
< strlen(str
); i
++) {
3579 strncpy(hex
, str
+ ++i
, 2);
3582 /* i is pointing to the start of the number */
3586 * Now it's at the end and at the start of the for loop
3587 * will be at the next character.
3589 buf
[j
++] = strtol(hex
, NULL
, 16);
3595 if (!g_utf8_validate(buf
, -1, (const char **)&bum
))
3602 purple_url_encode(const char *str
)
3605 static char buf
[BUF_LEN
];
3609 g_return_val_if_fail(str
!= NULL
, NULL
);
3610 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
3613 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
3614 gunichar c
= g_utf8_get_char(iter
);
3615 /* If the character is an ASCII character and is alphanumeric
3616 * no need to escape */
3617 if (c
< 128 && (isalnum(c
) || c
== '-' || c
== '.' || c
== '_' || c
== '~')) {
3620 int bytes
= g_unichar_to_utf8(c
, utf_char
);
3621 for (i
= 0; (int)i
< bytes
; i
++) {
3622 if (j
> (BUF_LEN
- 4))
3624 if (i
>= sizeof(utf_char
)) {
3625 g_warn_if_reached();
3628 sprintf(buf
+ j
, "%%%02X", utf_char
[i
] & 0xff);
3639 /* Originally lifted from
3640 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
3641 * ... and slightly modified to be a bit more rfc822 compliant
3642 * ... and modified a bit more to make domain checking rfc1035 compliant
3643 * with the exception permitted in rfc1101 for domains to start with digit
3644 * but not completely checking to avoid conflicts with IP addresses
3647 purple_email_is_valid(const char *address
)
3649 const char *c
, *domain
;
3650 static char *rfc822_specials
= "()<>@,;:\\\"[]";
3652 g_return_val_if_fail(address
!= NULL
, FALSE
);
3654 if (*address
== '.') return FALSE
;
3656 /* first we validate the name portion (name@domain) (rfc822)*/
3657 for (c
= address
; *c
; c
++) {
3658 if (*c
== '\"' && (c
== address
|| *(c
- 1) == '.' || *(c
- 1) == '\"')) {
3661 if (*c
++ && *c
< 127 && *c
> 0 && *c
!= '\n' && *c
!= '\r') continue;
3664 if (*c
== '\"') break;
3665 if (*c
< ' ' || *c
>= 127) return FALSE
;
3667 if (!*c
++) return FALSE
;
3668 if (*c
== '@') break;
3669 if (*c
!= '.') return FALSE
;
3672 if (*c
== '@') break;
3673 if (*c
<= ' ' || *c
>= 127) return FALSE
;
3674 if (strchr(rfc822_specials
, *c
)) return FALSE
;
3677 /* It's obviously not an email address if we didn't find an '@' above */
3678 if (*c
== '\0') return FALSE
;
3680 /* strictly we should return false if (*(c - 1) == '.') too, but I think
3681 * we should permit user.@domain type addresses - they do work :) */
3682 if (c
== address
) return FALSE
;
3684 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
3685 if (!*(domain
= ++c
)) return FALSE
;
3687 if (*c
== '.' && (c
== domain
|| *(c
- 1) == '.' || *(c
- 1) == '-'))
3689 if (*c
== '-' && (*(c
- 1) == '.' || *(c
- 1) == '@')) return FALSE
;
3690 if ((*c
< '0' && *c
!= '-' && *c
!= '.') || (*c
> '9' && *c
< 'A') ||
3691 (*c
> 'Z' && *c
< 'a') || (*c
> 'z')) return FALSE
;
3694 if (*(c
- 1) == '-') return FALSE
;
3696 return ((c
- domain
) > 3 ? TRUE
: FALSE
);
3700 purple_ipv4_address_is_valid(const char *ip
)
3702 int c
, o1
, o2
, o3
, o4
;
3705 g_return_val_if_fail(ip
!= NULL
, FALSE
);
3707 c
= sscanf(ip
, "%d.%d.%d.%d%c", &o1
, &o2
, &o3
, &o4
, &end
);
3708 if (c
!= 4 || o1
< 0 || o1
> 255 || o2
< 0 || o2
> 255 || o3
< 0 || o3
> 255 || o4
< 0 || o4
> 255)
3714 purple_ipv6_address_is_valid(const gchar
*ip
)
3717 gboolean double_colon
= FALSE
;
3721 g_return_val_if_fail(ip
!= NULL
, FALSE
);
3726 for (c
= ip
; *c
; ++c
) {
3727 if ((*c
>= '0' && *c
<= '9') ||
3728 (*c
>= 'a' && *c
<= 'f') ||
3729 (*c
>= 'A' && *c
<= 'F')) {
3731 /* Only four hex digits per chunk */
3734 } else if (*c
== ':') {
3735 /* The start of a new chunk */
3738 if (*(c
+ 1) == ':') {
3740 * '::' indicates a consecutive series of chunks full
3741 * of zeroes. There can be only one of these per address.
3745 double_colon
= TRUE
;
3752 * Either we saw a '::' and there were fewer than 8 chunks -or-
3753 * we didn't see a '::' and saw exactly 8 chunks.
3755 return (double_colon
&& chunks
< 8) || (!double_colon
&& chunks
== 8);
3759 purple_ip_address_is_valid(const char *ip
)
3761 return (purple_ipv4_address_is_valid(ip
) || purple_ipv6_address_is_valid(ip
));
3764 /* Stolen from gnome_uri_list_extract_uris */
3766 purple_uri_list_extract_uris(const gchar
*uri_list
)
3770 GList
*result
= NULL
;
3772 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
3776 /* We don't actually try to validate the URI according to RFC
3777 * 2396, or even check for allowed characters - we just ignore
3778 * comments and trim whitespace off the ends. We also
3779 * allow LF delimination as well as the specified CRLF.
3787 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
3792 while (q
> p
&& isspace(*q
))
3795 retval
= (gchar
*)g_malloc (q
- p
+ 2);
3796 strncpy (retval
, p
, q
- p
+ 1);
3797 retval
[q
- p
+ 1] = '\0';
3799 result
= g_list_prepend (result
, retval
);
3802 p
= strchr (p
, '\n');
3807 return g_list_reverse (result
);
3811 /* Stolen from gnome_uri_list_extract_filenames */
3813 purple_uri_list_extract_filenames(const gchar
*uri_list
)
3815 GList
*tmp_list
, *node
, *result
;
3817 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
3819 result
= purple_uri_list_extract_uris(uri_list
);
3823 gchar
*s
= (gchar
*)tmp_list
->data
;
3826 tmp_list
= tmp_list
->next
;
3828 if (!strncmp (s
, "file:", 5)) {
3829 node
->data
= g_filename_from_uri (s
, NULL
, NULL
);
3830 /* not sure if this fallback is useful at all */
3831 if (!node
->data
) node
->data
= g_strdup (s
+5);
3833 result
= g_list_delete_link(result
, node
);
3841 purple_uri_escape_for_open(const char *unescaped
)
3843 /* Replace some special characters like $ with their percent-encoded value.
3844 * This shouldn't be necessary because we shell-escape the entire arg before
3845 * exec'ing the browser, however, we had a report that a URL containing
3846 * $(xterm) was causing xterm to start on his system. This is obviously a
3847 * bug on his system, but it's pretty easy for us to protect against it. */
3848 return g_uri_escape_string(unescaped
, "[]:;/%#,+?=&@", FALSE
);
3851 /**************************************************************************
3852 * UTF8 String Functions
3853 **************************************************************************/
3855 purple_utf8_try_convert(const char *str
)
3860 g_return_val_if_fail(str
!= NULL
, NULL
);
3862 if (g_utf8_validate(str
, -1, NULL
)) {
3863 return g_strdup(str
);
3866 utf8
= g_locale_to_utf8(str
, -1, &converted
, NULL
, NULL
);
3870 utf8
= g_convert(str
, -1, "UTF-8", "ISO-8859-15", &converted
, NULL
, NULL
);
3871 if ((utf8
!= NULL
) && (converted
== strlen(str
)))
3879 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
3880 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
3882 purple_utf8_salvage(const char *str
)
3887 g_return_val_if_fail(str
!= NULL
, NULL
);
3889 workstr
= g_string_sized_new(strlen(str
));
3892 (void)g_utf8_validate(str
, -1, &end
);
3893 workstr
= g_string_append_len(workstr
, str
, end
- str
);
3898 workstr
= g_string_append_c(workstr
, '?');
3900 } while (!utf8_first(*str
));
3901 } while (*str
!= '\0');
3903 return g_string_free(workstr
, FALSE
);
3907 purple_utf8_strip_unprintables(const gchar
*str
)
3909 gchar
*workstr
, *iter
;
3913 /* Act like g_strdup */
3916 if (!g_utf8_validate(str
, -1, &bad
)) {
3917 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
3918 "first bad character was %02x (%c)\n",
3920 g_return_val_if_reached(NULL
);
3923 workstr
= iter
= g_new(gchar
, strlen(str
) + 1);
3925 gunichar ch
= g_utf8_get_char(str
);
3926 gchar
*next
= g_utf8_next_char(str
);
3928 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
3929 * [#x10000-#x10FFFF]
3931 if ((ch
== '\t' || ch
== '\n' || ch
== '\r') ||
3932 (ch
>= 0x20 && ch
<= 0xD7FF) ||
3933 (ch
>= 0xE000 && ch
<= 0xFFFD) ||
3934 (ch
>= 0x10000 && ch
<= 0x10FFFF)) {
3935 memcpy(iter
, str
, next
- str
);
3936 iter
+= (next
- str
);
3942 /* nul-terminate the new string */
3949 * This function is copied from g_strerror() but changed to use
3953 purple_gai_strerror(gint errnum
)
3955 static GPrivate msg_private
= G_PRIVATE_INIT(g_free
);
3957 int saved_errno
= errno
;
3959 const char *msg_locale
;
3961 msg_locale
= gai_strerror(errnum
);
3962 if (g_get_charset(NULL
))
3964 /* This string is already UTF-8--great! */
3965 errno
= saved_errno
;
3970 gchar
*msg_utf8
= g_locale_to_utf8(msg_locale
, -1, NULL
, NULL
, NULL
);
3973 /* Stick in the quark table so that we can return a static result */
3974 GQuark msg_quark
= g_quark_from_string(msg_utf8
);
3977 msg_utf8
= (gchar
*)g_quark_to_string(msg_quark
);
3978 errno
= saved_errno
;
3983 msg
= g_private_get(&msg_private
);
3987 msg
= g_new(gchar
, 64);
3988 g_private_set(&msg_private
, msg
);
3991 sprintf(msg
, "unknown error (%d)", errnum
);
3993 errno
= saved_errno
;
3998 purple_utf8_ncr_encode(const char *str
)
4002 g_return_val_if_fail(str
!= NULL
, NULL
);
4003 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4005 out
= g_string_new("");
4007 for(; *str
; str
= g_utf8_next_char(str
)) {
4008 gunichar wc
= g_utf8_get_char(str
);
4010 /* super simple check. hopefully not too wrong. */
4012 g_string_append_printf(out
, "&#%u;", (guint32
) wc
);
4014 g_string_append_unichar(out
, wc
);
4018 return g_string_free(out
, FALSE
);
4023 purple_utf8_ncr_decode(const char *str
)
4028 g_return_val_if_fail(str
!= NULL
, NULL
);
4029 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4032 out
= g_string_new("");
4034 while( (b
= strstr(buf
, "&#")) ) {
4038 /* append everything leading up to the &# */
4039 g_string_append_len(out
, buf
, b
-buf
);
4041 b
+= 2; /* skip past the &# */
4043 /* strtoul will treat 0x prefix as hex, but not just x */
4044 if(*b
== 'x' || *b
== 'X') {
4049 /* advances buf to the end of the ncr segment */
4050 wc
= (gunichar
) strtoul(b
, &buf
, base
);
4052 /* this mimics the previous impl of ncr_decode */
4054 g_string_append_unichar(out
, wc
);
4059 /* append whatever's left */
4060 g_string_append(out
, buf
);
4062 return g_string_free(out
, FALSE
);
4067 purple_utf8_strcasecmp(const char *a
, const char *b
)
4069 char *a_norm
= NULL
;
4070 char *b_norm
= NULL
;
4080 if(!g_utf8_validate(a
, -1, NULL
) || !g_utf8_validate(b
, -1, NULL
))
4082 purple_debug_error("purple_utf8_strcasecmp",
4083 "One or both parameters are invalid UTF8\n");
4087 a_norm
= g_utf8_casefold(a
, -1);
4088 b_norm
= g_utf8_casefold(b
, -1);
4089 ret
= g_utf8_collate(a_norm
, b_norm
);
4096 /* previously conversation::find_nick() */
4098 purple_utf8_has_word(const char *haystack
, const char *needle
)
4100 char *hay
, *pin
, *p
;
4101 const char *start
, *prev_char
;
4102 gunichar before
, after
;
4104 gboolean ret
= FALSE
;
4106 start
= hay
= g_utf8_strdown(haystack
, -1);
4108 pin
= g_utf8_strdown(needle
, -1);
4111 while ((p
= strstr(start
, pin
)) != NULL
) {
4112 prev_char
= g_utf8_find_prev_char(hay
, p
);
4115 before
= g_utf8_get_char(prev_char
);
4117 after
= g_utf8_get_char_validated(p
+ n
, - 1);
4120 /* The character before is a reasonable guess for a word boundary
4121 ("!g_unichar_isalnum()" is not a valid way to determine word
4122 boundaries, but it is the only reasonable thing to do here),
4123 and isn't the '&' from a "&" or some such entity*/
4124 (before
!= (gunichar
)-2 && !g_unichar_isalnum(before
) && *(p
- 1) != '&'))
4125 && after
!= (gunichar
)-2 && !g_unichar_isalnum(after
)) {
4138 gboolean
purple_message_meify(char *message
, gssize len
)
4141 gboolean inside_html
= FALSE
;
4143 g_return_val_if_fail(message
!= NULL
, FALSE
);
4146 len
= strlen(message
);
4148 for (c
= message
; *c
; c
++, len
--) {
4151 inside_html
= FALSE
;
4160 if(*c
&& !g_ascii_strncasecmp(c
, "/me ", 4)) {
4161 memmove(c
, c
+4, len
-3);
4168 char *purple_text_strip_mnemonic(const char *in
)
4175 g_return_val_if_fail(in
!= NULL
, NULL
);
4177 out
= g_malloc(strlen(in
)+1);
4181 a0
= a
; /* The last non-space char seen so far, or the first char */
4185 if(a
> out
&& b
> in
&& *(b
-1) == '(' && *(b
+1) && !(*(b
+1) & 0x80) && *(b
+2) == ')') {
4186 /* Detected CJK style shortcut (Bug 875311) */
4187 a
= a0
; /* undo the left parenthesis */
4188 b
+= 3; /* and skip the whole mess */
4189 } else if(*(b
+1) == '_') {
4196 /* We don't want to corrupt the middle of UTF-8 characters */
4197 } else if (!(*b
& 0x80)) { /* other 1-byte char */
4202 /* Multibyte utf8 char, don't look for _ inside these */
4205 if ((*b
& 0xe0) == 0xc0) {
4207 } else if ((*b
& 0xf0) == 0xe0) {
4209 } else if ((*b
& 0xf8) == 0xf0) {
4211 } else if ((*b
& 0xfc) == 0xf8) {
4213 } else if ((*b
& 0xfe) == 0xfc) {
4215 } else { /* Illegal utf8 */
4218 a0
= a
; /* unless we want to delete CJK spaces too */
4219 for (i
= 0; i
< n
&& *b
; i
+= 1) {
4229 const char* purple_unescape_filename(const char *escaped
) {
4230 return purple_url_decode(escaped
);
4234 /* this is almost identical to purple_url_encode (hence purple_url_decode
4235 * being used above), but we want to keep certain characters unescaped
4236 * for compat reasons */
4238 purple_escape_filename(const char *str
)
4241 static char buf
[BUF_LEN
];
4245 g_return_val_if_fail(str
!= NULL
, NULL
);
4246 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4249 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
4250 gunichar c
= g_utf8_get_char(iter
);
4251 /* If the character is an ASCII character and is alphanumeric,
4252 * or one of the specified values, no need to escape */
4253 if (c
< 128 && (g_ascii_isalnum(c
) || c
== '@' || c
== '-' ||
4254 c
== '_' || c
== '.' || c
== '#')) {
4257 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4258 for (i
= 0; (int)i
< bytes
; i
++) {
4259 if (j
> (BUF_LEN
- 4))
4261 if (i
>= sizeof(utf_char
)) {
4262 g_warn_if_reached();
4265 sprintf(buf
+ j
, "%%%02x", utf_char
[i
] & 0xff);
4271 /* File/Directory names in windows cannot end in periods/spaces.
4272 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4274 while (j
> 0 && (buf
[j
- 1] == '.' || buf
[j
- 1] == ' '))
4282 gchar
* purple_escape_js(const gchar
*str
)
4286 json_node_set_string(escape_js_node
, str
);
4287 json_generator_set_root(escape_js_gen
, escape_js_node
);
4288 escaped
= json_generator_to_data(escape_js_gen
, NULL
);
4289 json_node_set_boolean(escape_js_node
, FALSE
);
4294 void purple_restore_default_signal_handlers(void)
4297 signal(SIGHUP
, SIG_DFL
); /* 1: terminal line hangup */
4298 signal(SIGINT
, SIG_DFL
); /* 2: interrupt program */
4299 signal(SIGQUIT
, SIG_DFL
); /* 3: quit program */
4300 signal(SIGILL
, SIG_DFL
); /* 4: illegal instruction (not reset when caught) */
4301 signal(SIGTRAP
, SIG_DFL
); /* 5: trace trap (not reset when caught) */
4302 signal(SIGABRT
, SIG_DFL
); /* 6: abort program */
4305 signal(SIGPOLL
, SIG_DFL
); /* 7: pollable event (POSIX) */
4306 #endif /* SIGPOLL */
4309 signal(SIGEMT
, SIG_DFL
); /* 7: EMT instruction (Non-POSIX) */
4312 signal(SIGFPE
, SIG_DFL
); /* 8: floating point exception */
4313 signal(SIGBUS
, SIG_DFL
); /* 10: bus error */
4314 signal(SIGSEGV
, SIG_DFL
); /* 11: segmentation violation */
4315 signal(SIGSYS
, SIG_DFL
); /* 12: bad argument to system call */
4316 signal(SIGPIPE
, SIG_DFL
); /* 13: write on a pipe with no reader */
4317 signal(SIGALRM
, SIG_DFL
); /* 14: real-time timer expired */
4318 signal(SIGTERM
, SIG_DFL
); /* 15: software termination signal */
4319 signal(SIGCHLD
, SIG_DFL
); /* 20: child status has changed */
4320 signal(SIGXCPU
, SIG_DFL
); /* 24: exceeded CPU time limit */
4321 signal(SIGXFSZ
, SIG_DFL
); /* 25: exceeded file size limit */
4322 #endif /* !_WIN32 */
4326 set_status_with_attrs(PurpleStatus
*status
, ...)
4329 va_start(args
, status
);
4330 purple_status_set_active_with_attrs(status
, TRUE
, args
);
4334 void purple_util_set_current_song(const char *title
, const char *artist
, const char *album
)
4336 GList
*list
= purple_accounts_get_all();
4337 for (; list
; list
= list
->next
) {
4338 PurplePresence
*presence
;
4340 PurpleAccount
*account
= list
->data
;
4341 if (!purple_account_get_enabled(account
, purple_core_get_ui()))
4344 presence
= purple_account_get_presence(account
);
4345 tune
= purple_presence_get_status(presence
, "tune");
4349 set_status_with_attrs(tune
,
4350 PURPLE_TUNE_TITLE
, title
,
4351 PURPLE_TUNE_ARTIST
, artist
,
4352 PURPLE_TUNE_ALBUM
, album
,
4355 purple_status_set_active(tune
, FALSE
);
4360 char * purple_util_format_song_info(const char *title
, const char *artist
, const char *album
, gpointer unused
)
4365 if (!title
|| !*title
)
4368 esc
= g_markup_escape_text(title
, -1);
4369 string
= g_string_new("");
4370 g_string_append_printf(string
, "%s", esc
);
4373 if (artist
&& *artist
) {
4374 esc
= g_markup_escape_text(artist
, -1);
4375 g_string_append_printf(string
, _(" - %s"), esc
);
4379 if (album
&& *album
) {
4380 esc
= g_markup_escape_text(album
, -1);
4381 g_string_append_printf(string
, _(" (%s)"), esc
);
4385 return g_string_free(string
, FALSE
);
4389 purple_get_host_name(void)
4391 return g_get_host_name();
4395 purple_uuid_random(void)
4399 tmp
= g_random_int();
4400 a
= 0x4000 | (tmp
& 0xFFF); /* 0x4000 to 0x4FFF */
4402 b
= ((1 << 3) << 12) | (tmp
& 0x3FFF); /* 0x8000 to 0xBFFF */
4404 tmp
= g_random_int();
4406 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4411 (tmp
>> 16) & 0xFFFF, g_random_int());
4414 void purple_callback_set_zero(gpointer data
)
4416 gpointer
*ptr
= data
;
4418 g_return_if_fail(ptr
!= NULL
);
4424 purple_value_new(GType type
)
4428 g_return_val_if_fail(type
!= G_TYPE_NONE
, NULL
);
4430 ret
= g_new0(GValue
, 1);
4431 g_value_init(ret
, type
);
4437 purple_value_dup(GValue
*value
)
4441 g_return_val_if_fail(value
!= NULL
, NULL
);
4443 ret
= g_new0(GValue
, 1);
4444 g_value_init(ret
, G_VALUE_TYPE(value
));
4445 g_value_copy(value
, ret
);
4451 purple_value_free(GValue
*value
)
4453 g_return_if_fail(value
!= NULL
);
4455 g_value_unset(value
);
4459 gchar
*purple_http_digest_calculate_session_key(
4460 const gchar
*algorithm
,
4461 const gchar
*username
,
4463 const gchar
*password
,
4465 const gchar
*client_nonce
)
4470 g_return_val_if_fail(username
!= NULL
, NULL
);
4471 g_return_val_if_fail(realm
!= NULL
, NULL
);
4472 g_return_val_if_fail(password
!= NULL
, NULL
);
4473 g_return_val_if_fail(nonce
!= NULL
, NULL
);
4475 /* Check for a supported algorithm. */
4476 g_return_val_if_fail(algorithm
== NULL
||
4477 *algorithm
== '\0' ||
4478 g_ascii_strcasecmp(algorithm
, "MD5") ||
4479 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
4481 hasher
= g_checksum_new(G_CHECKSUM_MD5
);
4482 g_return_val_if_fail(hasher
!= NULL
, NULL
);
4484 g_checksum_update(hasher
, (guchar
*)username
, -1);
4485 g_checksum_update(hasher
, (guchar
*)":", -1);
4486 g_checksum_update(hasher
, (guchar
*)realm
, -1);
4487 g_checksum_update(hasher
, (guchar
*)":", -1);
4488 g_checksum_update(hasher
, (guchar
*)password
, -1);
4490 if (algorithm
!= NULL
&& !g_ascii_strcasecmp(algorithm
, "MD5-sess"))
4493 gsize digest_len
= 16;
4495 if (client_nonce
== NULL
)
4497 g_object_unref(hasher
);
4498 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
4502 g_checksum_get_digest(hasher
, digest
, &digest_len
);
4504 g_checksum_reset(hasher
);
4505 g_checksum_update(hasher
, digest
, sizeof(digest
));
4506 g_checksum_update(hasher
, (guchar
*)":", -1);
4507 g_checksum_update(hasher
, (guchar
*)nonce
, -1);
4508 g_checksum_update(hasher
, (guchar
*)":", -1);
4509 g_checksum_update(hasher
, (guchar
*)client_nonce
, -1);
4512 hash
= g_strdup(g_checksum_get_string(hasher
));
4513 g_checksum_free(hasher
);
4518 gchar
*purple_http_digest_calculate_response(
4519 const gchar
*algorithm
,
4520 const gchar
*method
,
4521 const gchar
*digest_uri
,
4523 const gchar
*entity
,
4525 const gchar
*nonce_count
,
4526 const gchar
*client_nonce
,
4527 const gchar
*session_key
)
4532 g_return_val_if_fail(method
!= NULL
, NULL
);
4533 g_return_val_if_fail(digest_uri
!= NULL
, NULL
);
4534 g_return_val_if_fail(nonce
!= NULL
, NULL
);
4535 g_return_val_if_fail(session_key
!= NULL
, NULL
);
4537 /* Check for a supported algorithm. */
4538 g_return_val_if_fail(algorithm
== NULL
||
4539 *algorithm
== '\0' ||
4540 g_ascii_strcasecmp(algorithm
, "MD5") ||
4541 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
4543 /* Check for a supported "quality of protection". */
4544 g_return_val_if_fail(qop
== NULL
||
4546 g_ascii_strcasecmp(qop
, "auth") ||
4547 g_ascii_strcasecmp(qop
, "auth-int"), NULL
);
4549 hash
= g_checksum_new(G_CHECKSUM_MD5
);
4550 g_return_val_if_fail(hash
!= NULL
, NULL
);
4552 g_checksum_update(hash
, (guchar
*)method
, -1);
4553 g_checksum_update(hash
, (guchar
*)":", -1);
4554 g_checksum_update(hash
, (guchar
*)digest_uri
, -1);
4556 if (qop
!= NULL
&& !g_ascii_strcasecmp(qop
, "auth-int"))
4562 g_checksum_free(hash
);
4563 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
4567 entity_hash
= g_compute_checksum_for_string(G_CHECKSUM_MD5
,
4570 if (entity_hash
== NULL
) {
4571 g_checksum_free(hash
);
4572 g_return_val_if_reached(NULL
);
4575 g_checksum_update(hash
, (guchar
*)":", -1);
4576 g_checksum_update(hash
, (guchar
*)entity_hash
, -1);
4577 g_free(entity_hash
);
4580 hash2
= g_strdup(g_checksum_get_string(hash
));
4581 g_checksum_reset(hash
);
4583 if (hash2
== NULL
) {
4584 g_checksum_free(hash
);
4585 g_return_val_if_reached(NULL
);
4588 g_checksum_update(hash
, (guchar
*)session_key
, -1);
4589 g_checksum_update(hash
, (guchar
*)":", -1);
4590 g_checksum_update(hash
, (guchar
*)nonce
, -1);
4591 g_checksum_update(hash
, (guchar
*)":", -1);
4593 if (qop
!= NULL
&& *qop
!= '\0')
4595 if (nonce_count
== NULL
)
4597 g_checksum_free(hash
);
4598 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
4602 if (client_nonce
== NULL
)
4604 g_checksum_free(hash
);
4605 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
4609 g_checksum_update(hash
, (guchar
*)nonce_count
, -1);
4610 g_checksum_update(hash
, (guchar
*)":", -1);
4611 g_checksum_update(hash
, (guchar
*)client_nonce
, -1);
4612 g_checksum_update(hash
, (guchar
*)":", -1);
4614 g_checksum_update(hash
, (guchar
*)qop
, -1);
4616 g_checksum_update(hash
, (guchar
*)":", -1);
4619 g_checksum_update(hash
, (guchar
*)hash2
, -1);
4622 hash2
= g_strdup(g_checksum_get_string(hash
));
4623 g_checksum_free(hash
);
4629 _purple_fstat(int fd
, GStatBuf
*st
)
4633 g_return_val_if_fail(st
!= NULL
, -1);
4636 ret
= _fstat(fd
, st
);
4638 ret
= fstat(fd
, st
);