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 "ciphers/md5hash.h"
22 #include "conversation.h"
25 #include "glibcompat.h"
31 #include <json-glib/json-glib.h>
33 struct _PurpleMenuAction
36 PurpleCallback callback
;
42 static char *custom_user_dir
= NULL
;
43 static char *user_dir
= NULL
;
44 static gchar
*cache_dir
= NULL
;
45 static gchar
*config_dir
= NULL
;
46 static gchar
*data_dir
= NULL
;
48 static JsonNode
*escape_js_node
= NULL
;
49 static JsonGenerator
*escape_js_gen
= NULL
;
51 /* If legacy directory for libpurple exists, move it to location following
52 * xdg base dir spec. https://developer.pidgin.im/ticket/10029
55 migrate_to_xdg_base_dirs(void)
57 const char *legacy_purple_dir
;
60 legacy_purple_dir
= purple_user_dir();
61 dir_exists
= g_file_test(legacy_purple_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
63 purple_move_to_xdg_base_dir(purple_data_dir(), "certificates");
64 purple_move_to_xdg_base_dir(purple_cache_dir(), "icons");
65 purple_move_to_xdg_base_dir(purple_data_dir(), "logs");
66 purple_move_to_xdg_base_dir(purple_data_dir(), "pounces.xml");
73 purple_menu_action_new(const char *label
, PurpleCallback callback
, gpointer data
,
76 PurpleMenuAction
*act
= g_new0(PurpleMenuAction
, 1);
77 act
->label
= g_strdup(label
);
78 act
->callback
= callback
;
80 act
->children
= children
;
85 purple_menu_action_free(PurpleMenuAction
*act
)
87 g_return_if_fail(act
!= NULL
);
89 g_free(act
->stock_icon
);
94 char * purple_menu_action_get_label(const PurpleMenuAction
*act
)
96 g_return_val_if_fail(act
!= NULL
, NULL
);
101 PurpleCallback
purple_menu_action_get_callback(const PurpleMenuAction
*act
)
103 g_return_val_if_fail(act
!= NULL
, NULL
);
105 return act
->callback
;
108 gpointer
purple_menu_action_get_data(const PurpleMenuAction
*act
)
110 g_return_val_if_fail(act
!= NULL
, NULL
);
115 GList
* purple_menu_action_get_children(const PurpleMenuAction
*act
)
117 g_return_val_if_fail(act
!= NULL
, NULL
);
119 return act
->children
;
122 void purple_menu_action_set_label(PurpleMenuAction
*act
, char *label
)
124 g_return_if_fail(act
!= NULL
);
129 void purple_menu_action_set_callback(PurpleMenuAction
*act
, PurpleCallback callback
)
131 g_return_if_fail(act
!= NULL
);
133 act
->callback
= callback
;
136 void purple_menu_action_set_data(PurpleMenuAction
*act
, gpointer data
)
138 g_return_if_fail(act
!= NULL
);
143 void purple_menu_action_set_children(PurpleMenuAction
*act
, GList
*children
)
145 g_return_if_fail(act
!= NULL
);
147 act
->children
= children
;
150 void purple_menu_action_set_stock_icon(PurpleMenuAction
*act
,
153 g_return_if_fail(act
!= NULL
);
155 g_free(act
->stock_icon
);
156 act
->stock_icon
= g_strdup(stock
);
160 purple_menu_action_get_stock_icon(PurpleMenuAction
*act
)
162 return act
->stock_icon
;
166 purple_util_init(void)
168 escape_js_node
= json_node_new(JSON_NODE_VALUE
);
169 escape_js_gen
= json_generator_new();
170 json_node_set_boolean(escape_js_node
, FALSE
);
172 migrate_to_xdg_base_dirs();
176 purple_util_uninit(void)
178 /* Free these so we don't have leaks at shutdown. */
180 g_free(custom_user_dir
);
181 custom_user_dir
= NULL
;
195 json_node_free(escape_js_node
);
196 escape_js_node
= NULL
;
198 g_object_unref(escape_js_gen
);
199 escape_js_gen
= NULL
;
202 /**************************************************************************
204 **************************************************************************/
206 purple_base16_encode(const guchar
*data
, gsize len
)
211 g_return_val_if_fail(data
!= NULL
, NULL
);
212 g_return_val_if_fail(len
> 0, NULL
);
214 ascii
= g_malloc(len
* 2 + 1);
216 for (i
= 0; i
< len
; i
++)
217 g_snprintf(&ascii
[i
* 2], 3, "%02x", data
[i
] & 0xFF);
223 purple_base16_decode(const char *str
, gsize
*ret_len
)
225 gsize len
, i
, accumulator
= 0;
228 g_return_val_if_fail(str
!= NULL
, NULL
);
232 g_return_val_if_fail(*str
, 0);
233 g_return_val_if_fail(len
% 2 == 0, 0);
235 data
= g_malloc(len
/ 2);
237 for (i
= 0; i
< len
; i
++)
245 accumulator
|= str
[i
] - 48;
248 switch(tolower(str
[i
]))
250 case 'a': accumulator
|= 10; break;
251 case 'b': accumulator
|= 11; break;
252 case 'c': accumulator
|= 12; break;
253 case 'd': accumulator
|= 13; break;
254 case 'e': accumulator
|= 14; break;
255 case 'f': accumulator
|= 15; break;
260 data
[(i
- 1) / 2] = accumulator
;
270 purple_base16_encode_chunked(const guchar
*data
, gsize len
)
275 g_return_val_if_fail(data
!= NULL
, NULL
);
276 g_return_val_if_fail(len
> 0, NULL
);
278 /* For each byte of input, we need 2 bytes for the hex representation
279 * and 1 for the colon.
280 * The final colon will be replaced by a terminating NULL
282 ascii
= g_malloc(len
* 3 + 1);
284 for (i
= 0; i
< len
; i
++)
285 g_snprintf(&ascii
[i
* 3], 4, "%02x:", data
[i
] & 0xFF);
287 /* Replace the final colon with NULL */
288 ascii
[len
* 3 - 1] = 0;
294 /**************************************************************************
296 **************************************************************************/
297 static const char xdigits
[] =
301 purple_base64_encode(const guchar
*data
, gsize len
)
303 return g_base64_encode(data
, len
);
307 purple_base64_decode(const char *str
, gsize
*ret_len
)
310 * We want to allow ret_len to be NULL for backward compatibility,
311 * but g_base64_decode() requires a valid length variable. So if
312 * ret_len is NULL then pass in a dummy variable.
315 return g_base64_decode(str
, ret_len
!= NULL
? ret_len
: &unused
);
318 /**************************************************************************
319 * Quoted Printable Functions (see RFC 2045).
320 **************************************************************************/
322 purple_quotedp_decode(const char *str
, gsize
*ret_len
)
327 n
= new = g_malloc(strlen (str
) + 1);
328 end
= str
+ strlen(str
);
330 for (p
= str
; p
< end
; p
++, n
++) {
332 if (p
[1] == '\r' && p
[2] == '\n') { /* 5.1 #5 */
335 } else if (p
[1] == '\n') { /* fuzzy case for 5.1 #5 */
338 } else if (p
[1] && p
[2]) {
339 char *nibble1
= strchr(xdigits
, tolower(p
[1]));
340 char *nibble2
= strchr(xdigits
, tolower(p
[2]));
341 if (nibble1
&& nibble2
) { /* 5.1 #1 */
342 *n
= ((nibble1
- xdigits
) << 4) | (nibble2
- xdigits
);
344 } else { /* This should never happen */
347 } else { /* This should never happen */
362 /* Resize to take less space */
363 /* new = realloc(new, n - new); */
365 return (guchar
*)new;
368 /**************************************************************************
370 **************************************************************************/
372 purple_mime_decode_field(const char *str
)
375 * This is wing's version, partially based on revo/shx's version
376 * See RFC2047 [which apparently obsoletes RFC1342]
379 state_start
, state_equal1
, state_question1
,
380 state_charset
, state_question2
,
381 state_encoding
, state_question3
,
382 state_encoded_text
, state_question4
, state_equal2
= state_start
383 } encoded_word_state_t
;
384 encoded_word_state_t state
= state_start
;
385 const char *cur
, *mark
;
386 const char *charset0
= NULL
, *encoding0
= NULL
, *encoded_text0
= NULL
;
389 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
390 #define token_char_p(c) \
391 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
393 /* But encoded-text must be ASCII; alas, isascii() may not exist */
394 #define encoded_text_char_p(c) \
395 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
397 g_return_val_if_fail(str
!= NULL
, NULL
);
399 new = g_string_new(NULL
);
401 /* Here we will be looking for encoded words and if they seem to be
402 * valid then decode them.
403 * They are of this form: =?charset?encoding?text?=
406 for (cur
= str
, mark
= NULL
; *cur
; cur
+= 1) {
410 state
= state_question1
;
412 g_string_append_len(new, mark
, cur
- mark
+ 1);
416 case state_question1
:
417 if (token_char_p(*cur
)) {
419 state
= state_charset
;
420 } else { /* This should never happen */
421 g_string_append_len(new, mark
, cur
- mark
+ 1);
427 state
= state_question2
;
428 } else if (!token_char_p(*cur
)) { /* This should never happen */
429 g_string_append_len(new, mark
, cur
- mark
+ 1);
433 case state_question2
:
434 if (token_char_p(*cur
)) {
436 state
= state_encoding
;
437 } else { /* This should never happen */
438 g_string_append_len(new, mark
, cur
- mark
+ 1);
444 state
= state_question3
;
445 } else if (!token_char_p(*cur
)) { /* This should never happen */
446 g_string_append_len(new, mark
, cur
- mark
+ 1);
450 case state_question3
:
451 if (encoded_text_char_p(*cur
)) {
453 state
= state_encoded_text
;
454 } else if (*cur
== '?') { /* empty string */
456 state
= state_question4
;
457 } else { /* This should never happen */
458 g_string_append_len(new, mark
, cur
- mark
+ 1);
462 case state_encoded_text
:
464 state
= state_question4
;
465 } else if (!encoded_text_char_p(*cur
)) {
466 g_string_append_len(new, mark
, cur
- mark
+ 1);
470 case state_question4
:
471 if (*cur
== '=') { /* Got the whole encoded-word */
472 char *charset
= g_strndup(charset0
, encoding0
- charset0
- 1);
473 char *encoding
= g_strndup(encoding0
, encoded_text0
- encoding0
- 1);
474 char *encoded_text
= g_strndup(encoded_text0
, cur
- encoded_text0
- 1);
475 guchar
*decoded
= NULL
;
477 if (g_ascii_strcasecmp(encoding
, "Q") == 0)
478 decoded
= purple_quotedp_decode(encoded_text
, &dec_len
);
479 else if (g_ascii_strcasecmp(encoding
, "B") == 0)
480 decoded
= purple_base64_decode(encoded_text
, &dec_len
);
485 char *converted
= g_convert((const gchar
*)decoded
, dec_len
, "utf-8", charset
, NULL
, &len
, NULL
);
488 g_string_append_len(new, converted
, len
);
495 g_free(encoded_text
);
496 state
= state_equal2
; /* Restart the FSM */
497 } else { /* This should never happen */
498 g_string_append_len(new, mark
, cur
- mark
+ 1);
505 state
= state_equal1
;
507 /* Some unencoded text. */
508 g_string_append_c(new, *cur
);
514 if (state
!= state_start
)
515 g_string_append_len(new, mark
, cur
- mark
+ 1);
517 return g_string_free(new, FALSE
);;
521 /**************************************************************************
522 * Date/Time Functions
523 **************************************************************************/
525 const char *purple_get_tzoff_str(const struct tm
*tm
, gboolean iso
)
531 struct tm new_tm
= *tm
;
535 if (new_tm
.tm_isdst
< 0)
536 g_return_val_if_reached("");
539 if ((off
= wpurple_get_tz_offset()) == -1)
541 #elif defined(HAVE_TM_GMTOFF)
542 off
= new_tm
.tm_gmtoff
;
543 #elif defined(HAVE_TIMEZONE)
547 purple_debug_warning("util",
548 "there is no possibility to obtain tz offset");
552 min
= (off
/ 60) % 60;
553 hrs
= ((off
/ 60) - min
) / 60;
559 /* please leave the colons...they're optional for iso, but jabber
561 if(g_snprintf(buf
, sizeof(buf
), "%+03d:%02d", hrs
, ABS(min
)) > 6)
562 g_return_val_if_reached("");
565 if (g_snprintf(buf
, sizeof(buf
), "%+03d%02d", hrs
, ABS(min
)) > 5)
566 g_return_val_if_reached("");
572 /* Windows doesn't HAVE_STRFTIME_Z_FORMAT, but this seems clearer. -- rlaager */
573 #if !defined(HAVE_STRFTIME_Z_FORMAT) || defined(_WIN32)
574 static size_t purple_internal_strftime(char *s
, size_t max
, const char *format
, const struct tm
*tm
)
580 /* Yes, this is checked in purple_utf8_strftime(),
581 * but better safe than sorry. -- rlaager */
582 g_return_val_if_fail(format
!= NULL
, 0);
584 /* This is fairly efficient, and it only gets
585 * executed on Windows or if the underlying
586 * system doesn't support the %z format string,
587 * for strftime() so I think it's good enough.
589 for (c
= start
= format
; *c
; c
++)
596 #ifndef HAVE_STRFTIME_Z_FORMAT
599 char *tmp
= g_strdup_printf("%s%.*s%s",
601 (int)(c
- start
- 1),
603 purple_get_tzoff_str(tm
, FALSE
));
612 char *tmp
= g_strdup_printf("%s%.*s%s",
614 (int)(c
- start
- 1),
616 wpurple_get_timezone_abbreviation(tm
));
630 char *tmp
= g_strconcat(fmt
, start
, NULL
);
635 ret
= strftime(s
, max
, fmt
, tm
);
641 return strftime(s
, max
, format
, tm
);
643 #else /* HAVE_STRFTIME_Z_FORMAT && !_WIN32 */
644 #define purple_internal_strftime strftime
648 purple_utf8_strftime(const char *format
, const struct tm
*tm
)
650 static char buf
[128];
656 g_return_val_if_fail(format
!= NULL
, NULL
);
660 time_t now
= time(NULL
);
661 tm
= localtime(&now
);
664 locale
= g_locale_from_utf8(format
, -1, NULL
, NULL
, &err
);
667 purple_debug_error("util", "Format conversion failed in purple_utf8_strftime(): %s\n", err
->message
);
670 locale
= g_strdup(format
);
673 /* A return value of 0 is either an error (in
674 * which case, the contents of the buffer are
675 * undefined) or the empty string (in which
676 * case, no harm is done here). */
677 if ((len
= purple_internal_strftime(buf
, sizeof(buf
), locale
, tm
)) == 0)
685 utf8
= g_locale_to_utf8(buf
, len
, NULL
, NULL
, &err
);
688 purple_debug_error("util", "Result conversion failed in purple_utf8_strftime(): %s\n", err
->message
);
693 g_strlcpy(buf
, utf8
, sizeof(buf
));
701 purple_date_format_short(const struct tm
*tm
)
703 return purple_utf8_strftime("%x", tm
);
707 purple_date_format_long(const struct tm
*tm
)
710 * This string determines how some dates are displayed. The default
711 * string "%x %X" shows the date then the time. Translators can
712 * change this to "%X %x" if they want the time to be shown first,
713 * followed by the date.
715 return purple_utf8_strftime(_("%x %X"), tm
);
719 purple_date_format_full(const struct tm
*tm
)
721 return purple_utf8_strftime("%c", tm
);
725 purple_time_format(const struct tm
*tm
)
727 return purple_utf8_strftime("%X", tm
);
731 purple_time_build(int year
, int month
, int day
, int hour
, int min
, int sec
)
735 tm
.tm_year
= year
- 1900;
736 tm
.tm_mon
= month
- 1;
740 tm
.tm_sec
= sec
>= 0 ? sec
: time(NULL
) % 60;
745 /* originally taken from GLib trunk 1-6-11 */
746 /* originally licensed as LGPL 2+ */
748 mktime_utc(struct tm
*tm
)
753 static const gint days_before
[] =
755 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
760 if (tm
->tm_mon
< 0 || tm
->tm_mon
> 11)
763 retval
= (tm
->tm_year
- 70) * 365;
764 retval
+= (tm
->tm_year
- 68) / 4;
765 retval
+= days_before
[tm
->tm_mon
] + tm
->tm_mday
- 1;
767 if (tm
->tm_year
% 4 == 0 && tm
->tm_mon
< 2)
770 retval
= ((((retval
* 24) + tm
->tm_hour
) * 60) + tm
->tm_min
) * 60 + tm
->tm_sec
;
772 retval
= timegm (tm
);
773 #endif /* !HAVE_TIMEGM */
779 purple_str_to_time(const char *timestamp
, gboolean utc
,
780 struct tm
*tm
, long *tz_off
, const char **rest
)
785 long tzoff
= PURPLE_NO_TZ_OFF
;
787 gboolean mktime_with_utc
= FALSE
;
792 g_return_val_if_fail(timestamp
!= NULL
, 0);
794 memset(&t
, 0, sizeof(struct tm
));
798 /* Strip leading whitespace */
799 while (g_ascii_isspace(*str
))
803 if (rest
!= NULL
&& *str
!= '\0')
809 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
810 if (rest
!= NULL
&& *str
!= '\0')
817 if (sscanf(str
, "%04d", &year
) && year
>= 1900) {
820 if (*str
== '-' || *str
== '/')
823 t
.tm_year
= year
- 1900;
827 if (!sscanf(str
, "%02d", &t
.tm_mon
)) {
828 if (rest
!= NULL
&& *str
!= '\0')
837 if (*str
== '-' || *str
== '/')
841 if (!sscanf(str
, "%02d", &t
.tm_mday
)) {
842 if (rest
!= NULL
&& *str
!= '\0')
850 /* Grab the year off the end if there's still stuff */
851 if (*str
== '/' || *str
== '-') {
852 /* But make sure we don't read the year twice */
854 if (rest
!= NULL
&& *str
!= '\0')
862 if (!sscanf(str
, "%04d", &t
.tm_year
)) {
863 if (rest
!= NULL
&& *str
!= '\0')
870 } else if (*str
== 'T' || *str
== '.') {
873 /* Continue grabbing the hours/minutes/seconds */
874 if ((sscanf(str
, "%02d:%02d:%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
876 (sscanf(str
, "%02d%02d%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
879 gint sign
, tzhrs
, tzmins
;
882 /* Cut off those pesky micro-seconds */
885 } while (*str
>= '0' && *str
<= '9');
888 sign
= (*str
== '+') ? 1 : -1;
890 /* Process the timezone */
891 if (*str
== '+' || *str
== '-') {
894 if (((sscanf(str
, "%02d:%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 5)) ||
895 (sscanf(str
, "%02d%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 4))))
897 mktime_with_utc
= TRUE
;
898 tzoff
= tzhrs
* 60 * 60 + tzmins
* 60;
901 } else if (*str
== 'Z') {
902 /* 'Z' = Zulu = UTC */
904 mktime_with_utc
= TRUE
;
908 if (!mktime_with_utc
)
910 /* No timezone specified. */
913 mktime_with_utc
= TRUE
;
923 if (rest
!= NULL
&& *str
!= '\0') {
924 /* Strip trailing whitespace */
925 while (g_ascii_isspace(*str
))
933 retval
= mktime_utc(&t
);
940 if (tzoff
!= PURPLE_NO_TZ_OFF
)
950 purple_uts35_to_str(const char *format
, size_t len
, struct tm
*tm
)
956 time_t now
= time(NULL
);
957 tm
= localtime(&now
);
960 string
= g_string_sized_new(len
);
964 while ((i
+ count
) < len
&& format
[i
] == format
[i
+count
])
972 } else if (count
== 4) {
974 } else if (count
>= 5) {
984 /* Two-digits only */
985 g_string_append(string
, purple_utf8_strftime("%y", tm
));
988 char *tmp
= g_strdup_printf("%%0%dY", count
);
989 g_string_append(string
, purple_utf8_strftime(tmp
, tm
));
994 /* Year (in "Week of Year" based calendars) */
997 /* Two-digits only */
1007 /* Cyclic Year Name */
1011 } else if (count
== 4) {
1013 } else if (count
>= 5) {
1024 } else if (count
== 3) {
1026 } else if (count
>= 4) {
1032 /* Stand-alone Quarter */
1036 } else if (count
== 3) {
1038 } else if (count
>= 4) {
1048 g_string_append(string
, purple_utf8_strftime("%m", tm
));
1049 } else if (count
== 3) {
1051 g_string_append(string
, purple_utf8_strftime("%b", tm
));
1052 } else if (count
== 4) {
1054 g_string_append(string
, purple_utf8_strftime("%B", tm
));
1055 } else if (count
>= 5) {
1056 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
1061 /* Stand-alone Month */
1065 g_string_append(string
, purple_utf8_strftime("%m", tm
));
1066 } else if (count
== 3) {
1068 g_string_append(string
, purple_utf8_strftime("%b", tm
));
1069 } else if (count
== 4) {
1071 g_string_append(string
, purple_utf8_strftime("%B", tm
));
1072 } else if (count
>= 5) {
1073 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
1085 g_string_append(string
, purple_utf8_strftime("%W", tm
));
1086 count
= MIN(count
, 2);
1097 g_string_append(string
, purple_utf8_strftime("%d", tm
));
1098 count
= MIN(count
, 2);
1103 g_string_append(string
, purple_utf8_strftime("%j", tm
));
1104 count
= MIN(count
, 3);
1107 /* Day of Year in Month */
1112 /* Modified Julian Day */
1121 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1122 } else if (count
== 4) {
1124 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1125 } else if (count
>= 5) {
1127 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1132 /* Local Day of Week */
1136 g_string_append(string
, purple_utf8_strftime("%u", tm
));
1137 } else if (count
== 3) {
1139 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1140 } else if (count
== 4) {
1142 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1143 } else if (count
>= 5) {
1145 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1150 /* Stand-alone Local Day of Week */
1154 g_string_append(string
, purple_utf8_strftime("%u", tm
));
1156 } else if (count
== 3) {
1158 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1159 } else if (count
== 4) {
1161 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1162 } else if (count
>= 5) {
1164 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1172 g_string_append(string
, purple_utf8_strftime("%p", tm
));
1180 g_string_append(string
, purple_utf8_strftime("%I", tm
));
1181 } else if (count
>= 2) {
1183 g_string_append(string
, purple_utf8_strftime("%I", tm
));
1192 g_string_append(string
, purple_utf8_strftime("%H", tm
));
1193 } else if (count
>= 2) {
1195 g_string_append(string
, purple_utf8_strftime("%H", tm
));
1204 } else if (count
>= 2) {
1214 } else if (count
>= 2) {
1220 /* Hour (hHkK by locale) */
1227 g_string_append(string
, purple_utf8_strftime("%M", tm
));
1228 count
= MIN(count
, 2);
1234 g_string_append(string
, purple_utf8_strftime("%S", tm
));
1235 count
= MIN(count
, 2);
1238 /* Fractional Sub-second */
1247 /* Time Zone (specific non-location format) */
1251 } else if (count
>= 4) {
1261 g_string_append(string
, purple_utf8_strftime("%z", tm
));
1262 } else if (count
== 4) {
1264 } else if (count
>= 5) {
1266 g_string_append(string
, purple_utf8_strftime("%z", tm
));
1271 /* Time Zone (generic non-location format) */
1275 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1277 } else if (count
>= 4) {
1279 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1289 } else if (count
>= 4) {
1290 /* Generic Location Format) */
1291 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1298 g_string_append_len(string
, format
+ i
, count
);
1305 return g_string_free(string
, FALSE
);
1308 /**************************************************************************
1310 **************************************************************************/
1313 * This function is stolen from glib's gmarkup.c and modified to not
1314 * replace ' with '
1316 static void append_escaped_text(GString
*str
,
1317 const gchar
*text
, gssize length
)
1324 end
= text
+ length
;
1329 next
= g_utf8_next_char (p
);
1334 g_string_append (str
, "&");
1338 g_string_append (str
, "<");
1342 g_string_append (str
, ">");
1346 g_string_append (str
, """);
1350 c
= g_utf8_get_char (p
);
1351 if ((0x1 <= c
&& c
<= 0x8) ||
1352 (0xb <= c
&& c
<= 0xc) ||
1353 (0xe <= c
&& c
<= 0x1f) ||
1354 (0x7f <= c
&& c
<= 0x84) ||
1355 (0x86 <= c
&& c
<= 0x9f))
1356 g_string_append_printf (str
, "&#x%x;", c
);
1358 g_string_append_len (str
, p
, next
- p
);
1366 /* This function is stolen from glib's gmarkup.c */
1367 gchar
*purple_markup_escape_text(const gchar
*text
, gssize length
)
1371 g_return_val_if_fail(text
!= NULL
, NULL
);
1374 length
= strlen(text
);
1376 /* prealloc at least as long as original text */
1377 str
= g_string_sized_new(length
);
1378 append_escaped_text(str
, text
, length
);
1380 return g_string_free(str
, FALSE
);
1384 purple_markup_unescape_entity(const char *text
, int *length
)
1390 if (!text
|| *text
!= '&')
1393 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1395 if(IS_ENTITY("&"))
1397 else if(IS_ENTITY("<"))
1399 else if(IS_ENTITY(">"))
1401 else if(IS_ENTITY(" "))
1403 else if(IS_ENTITY("©"))
1404 pln
= "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1405 else if(IS_ENTITY("""))
1407 else if(IS_ENTITY("®"))
1408 pln
= "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1409 else if(IS_ENTITY("'"))
1411 else if(*(text
+1) == '#' &&
1412 (sscanf(text
, "&#%u%1[;]", £
, temp
) == 2 ||
1413 sscanf(text
, "&#x%x%1[;]", £
, temp
) == 2) &&
1416 int buflen
= g_unichar_to_utf8((gunichar
)pound
, buf
);
1420 len
= (*(text
+2) == 'x' ? 3 : 2);
1421 while(isxdigit((gint
) text
[len
])) len
++;
1422 if(text
[len
] == ';') len
++;
1433 purple_markup_get_css_property(const gchar
*style
,
1436 const gchar
*css_str
= style
;
1437 const gchar
*css_value_start
;
1438 const gchar
*css_value_end
;
1442 g_return_val_if_fail(opt
!= NULL
, NULL
);
1447 /* find the CSS property */
1450 /* skip whitespace characters */
1451 while (*css_str
&& g_ascii_isspace(*css_str
))
1453 if (!g_ascii_isalpha(*css_str
))
1455 if (g_ascii_strncasecmp(css_str
, opt
, strlen(opt
)))
1457 /* go to next css property positioned after the next ';' */
1458 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1468 /* find the CSS value position in the string */
1469 css_str
+= strlen(opt
);
1470 while (*css_str
&& g_ascii_isspace(*css_str
))
1472 if (*css_str
!= ':')
1475 while (*css_str
&& g_ascii_isspace(*css_str
))
1477 if (*css_str
== '\0' || *css_str
== '"' || *css_str
== ';')
1480 /* mark the CSS value */
1481 css_value_start
= css_str
;
1482 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1484 css_value_end
= css_str
- 1;
1486 /* Removes trailing whitespace */
1487 while (css_value_end
> css_value_start
&& g_ascii_isspace(*css_value_end
))
1490 tmp
= g_strndup(css_value_start
, css_value_end
- css_value_start
+ 1);
1491 ret
= purple_unescape_html(tmp
);
1497 gboolean
purple_markup_is_rtl(const char *html
)
1500 const gchar
*start
, *end
;
1501 gboolean res
= FALSE
;
1503 if (purple_markup_find_tag("span", html
, &start
, &end
, &attributes
))
1505 /* tmp is a member of attributes and is free with g_datalist_clear call */
1506 const char *tmp
= g_datalist_get_data(&attributes
, "dir");
1507 if (tmp
&& !g_ascii_strcasecmp(tmp
, "RTL"))
1511 tmp
= g_datalist_get_data(&attributes
, "style");
1514 char *tmp2
= purple_markup_get_css_property(tmp
, "direction");
1515 if (tmp2
&& !g_ascii_strcasecmp(tmp2
, "RTL"))
1521 g_datalist_clear(&attributes
);
1527 purple_markup_find_tag(const char *needle
, const char *haystack
,
1528 const char **start
, const char **end
, GData
**attributes
)
1531 const char *cur
= haystack
;
1533 gboolean found
= FALSE
;
1534 gboolean in_tag
= FALSE
;
1535 gboolean in_attr
= FALSE
;
1536 const char *in_quotes
= NULL
;
1539 g_return_val_if_fail( needle
!= NULL
, FALSE
);
1540 g_return_val_if_fail( *needle
!= '\0', FALSE
);
1541 g_return_val_if_fail( haystack
!= NULL
, FALSE
);
1542 g_return_val_if_fail( start
!= NULL
, FALSE
);
1543 g_return_val_if_fail( end
!= NULL
, FALSE
);
1544 g_return_val_if_fail(attributes
!= NULL
, FALSE
);
1546 needlelen
= strlen(needle
);
1547 g_datalist_init(&attribs
);
1549 while (*cur
&& !found
) {
1552 const char *close
= cur
;
1554 while (*close
&& *close
!= *in_quotes
)
1557 /* if we got the close quote, store the value and carry on from *
1558 * after it. if we ran to the end of the string, point to the NULL *
1559 * and we're outta here */
1561 /* only store a value if we have an attribute name */
1563 size_t len
= close
- cur
;
1564 char *val
= g_strndup(cur
, len
);
1566 g_datalist_set_data_full(&attribs
, name
, val
, g_free
);
1576 } else if (in_attr
) {
1577 const char *close
= cur
;
1579 while (*close
&& *close
!= '>' && *close
!= '"' &&
1580 *close
!= '\'' && *close
!= ' ' && *close
!= '=')
1583 /* if we got the equals, store the name of the attribute. if we got
1584 * the quote, save the attribute and go straight to quote mode.
1585 * otherwise the tag closed or we reached the end of the string,
1586 * so we can get outta here */
1594 size_t len
= close
- cur
;
1596 /* don't store a blank attribute name */
1599 name
= g_ascii_strdown(cur
, len
);
1617 /* swallow extra spaces inside tag */
1618 while (*cur
&& *cur
== ' ') cur
++;
1635 /* if we hit a < followed by the name of our tag... */
1636 if (*cur
== '<' && !g_ascii_strncasecmp(cur
+ 1, needle
, needlelen
)) {
1638 cur
= cur
+ needlelen
+ 1;
1640 /* if we're pointing at a space or a >, we found the right tag. if *
1641 * we're not, we've found a longer tag, so we need to skip to the *
1642 * >, but not being distracted by >s inside quotes. */
1643 if (*cur
== ' ' || *cur
== '>') {
1646 while (*cur
&& *cur
!= '"' && *cur
!= '\'' && *cur
!= '>') {
1649 while (*cur
&& *cur
!= '"')
1651 } else if (*cur
== '\'') {
1653 while (*cur
&& *cur
!= '\'')
1666 /* clean up any attribute name from a premature termination */
1670 *attributes
= attribs
;
1681 purple_markup_extract_info_field(const char *str
, int len
, PurpleNotifyUserInfo
*user_info
,
1682 const char *start_token
, int skip
,
1683 const char *end_token
, char check_value
,
1684 const char *no_value_token
,
1685 const char *display_name
, gboolean is_link
,
1686 const char *link_prefix
,
1687 PurpleInfoFieldFormatCallback format_cb
)
1691 g_return_val_if_fail(str
!= NULL
, FALSE
);
1692 g_return_val_if_fail(user_info
!= NULL
, FALSE
);
1693 g_return_val_if_fail(start_token
!= NULL
, FALSE
);
1694 g_return_val_if_fail(end_token
!= NULL
, FALSE
);
1695 g_return_val_if_fail(display_name
!= NULL
, FALSE
);
1697 p
= strstr(str
, start_token
);
1702 p
+= strlen(start_token
) + skip
;
1707 if (check_value
!= '\0' && *p
== check_value
)
1710 q
= strstr(p
, end_token
);
1712 /* Trim leading blanks */
1713 while (*p
!= '\n' && g_ascii_isspace(*p
)) {
1717 /* Trim trailing blanks */
1718 while (q
> p
&& g_ascii_isspace(*(q
- 1))) {
1722 /* Don't bother with null strings */
1726 if (q
!= NULL
&& (!no_value_token
||
1727 (no_value_token
&& strncmp(p
, no_value_token
,
1728 strlen(no_value_token
)))))
1730 GString
*dest
= g_string_new("");
1734 g_string_append(dest
, "<a href=\"");
1737 g_string_append(dest
, link_prefix
);
1739 if (format_cb
!= NULL
)
1741 char *reformatted
= format_cb(p
, q
- p
);
1742 g_string_append(dest
, reformatted
);
1743 g_free(reformatted
);
1746 g_string_append_len(dest
, p
, q
- p
);
1747 g_string_append(dest
, "\">");
1750 g_string_append(dest
, link_prefix
);
1752 g_string_append_len(dest
, p
, q
- p
);
1753 g_string_append(dest
, "</a>");
1757 if (format_cb
!= NULL
)
1759 char *reformatted
= format_cb(p
, q
- p
);
1760 g_string_append(dest
, reformatted
);
1761 g_free(reformatted
);
1764 g_string_append_len(dest
, p
, q
- p
);
1767 purple_notify_user_info_add_pair_html(user_info
, display_name
, dest
->str
);
1768 g_string_free(dest
, TRUE
);
1776 struct purple_parse_tag
{
1782 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1783 recommended in the GCC docs). It contains 'continue's that should
1784 affect the while-loop in purple_markup_html_to_xhtml and doing the
1785 above would break that.
1786 Also, remember to put braces in constructs that require them for
1787 multiple statements when using this macro. */
1788 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1789 const char *o = c + strlen("<" x); \
1790 const char *p = NULL, *q = NULL, *r = NULL; \
1791 /* o = iterating over full tag \
1792 * p = > (end of tag) \
1793 * q = start of quoted bit \
1794 * r = < inside tag \
1796 GString *innards = g_string_new(""); \
1798 if(!q && (*o == '\"' || *o == '\'') ) { \
1801 if(*o == *q) { /* end of quoted bit */ \
1802 char *unescaped = g_strndup(q+1, o-q-1); \
1803 char *escaped = g_markup_escape_text(unescaped, -1); \
1804 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1805 g_free(unescaped); \
1808 } else if(*c == '\\') { \
1811 } else if(*o == '<') { \
1813 } else if(*o == '>') { \
1817 innards = g_string_append_c(innards, *o); \
1821 if(p && !r) { /* got an end of tag and no other < earlier */\
1822 if(*(p-1) != '/') { \
1823 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1826 tags = g_list_prepend(tags, pt); \
1829 xhtml = g_string_append(xhtml, "<" y); \
1830 xhtml = g_string_append(xhtml, innards->str); \
1831 xhtml = g_string_append_c(xhtml, '>'); \
1834 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1836 xhtml = g_string_append(xhtml, "<"); \
1838 plain = g_string_append_c(plain, '<'); \
1841 g_string_free(innards, TRUE); \
1844 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1845 (*(c+strlen("<" x)) == '>' || \
1846 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1848 xhtml = g_string_append(xhtml, "<" y); \
1849 c += strlen("<" x); \
1851 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1854 tags = g_list_prepend(tags, pt); \
1856 xhtml = g_string_append_c(xhtml, '>'); \
1859 xhtml = g_string_append(xhtml, "/>");\
1861 c = strchr(c, '>') + 1; \
1864 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1865 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1867 purple_markup_html_to_xhtml(const char *html
, char **xhtml_out
,
1870 GString
*xhtml
= NULL
;
1871 GString
*plain
= NULL
;
1872 GString
*url
= NULL
;
1873 GString
*cdata
= NULL
;
1874 GList
*tags
= NULL
, *tag
;
1875 const char *c
= html
;
1878 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1883 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1885 g_return_if_fail(xhtml_out
!= NULL
|| plain_out
!= NULL
);
1888 xhtml
= g_string_new("");
1890 plain
= g_string_new("");
1894 if(*(c
+1) == '/') { /* closing tag */
1897 struct purple_parse_tag
*pt
= tag
->data
;
1898 if(!g_ascii_strncasecmp((c
+2), pt
->src_tag
, strlen(pt
->src_tag
)) && *(c
+strlen(pt
->src_tag
)+2) == '>') {
1899 c
+= strlen(pt
->src_tag
) + 3;
1906 struct purple_parse_tag
*pt
= tags
->data
;
1907 if(xhtml
&& !pt
->ignore
)
1908 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
1909 if(plain
&& purple_strequal(pt
->src_tag
, "a")) {
1910 /* if this is a link, we have to add the url to the plaintext, too */
1912 (!g_string_equal(cdata
, url
) && (g_ascii_strncasecmp(url
->str
, "mailto:", 7) != 0 ||
1913 g_utf8_collate(url
->str
+ 7, cdata
->str
) != 0)))
1914 g_string_append_printf(plain
, " <%s>", g_strstrip(purple_unescape_html(url
->str
)));
1916 g_string_free(cdata
, TRUE
);
1923 tags
= g_list_remove(tags
, pt
);
1927 tags
= g_list_remove(tags
, tag
->data
);
1929 /* a closing tag we weren't expecting...
1930 * we'll let it slide, if it's really a tag...if it's
1931 * just a </ we'll escape it properly */
1932 const char *end
= c
+2;
1933 while(*end
&& g_ascii_isalpha(*end
))
1939 xhtml
= g_string_append(xhtml
, "<");
1941 plain
= g_string_append_c(plain
, '<');
1945 } else { /* opening tag */
1946 ALLOW_TAG("blockquote");
1956 /* we only allow html to start the message */
1960 ALLOW_TAG_ALT("i", "em");
1961 ALLOW_TAG_ALT("italic", "em");
1971 /* we skip <HR> because it's not legal in XHTML-IM. However,
1972 * we still want to send something sensible, so we put a
1973 * linebreak in its place. <BR> also needs special handling
1974 * because putting a </BR> to close it would just be dumb. */
1975 if((!g_ascii_strncasecmp(c
, "<br", 3)
1976 || !g_ascii_strncasecmp(c
, "<hr", 3))
1977 && (*(c
+3) == '>' ||
1978 !g_ascii_strncasecmp(c
+3, "/>", 2) ||
1979 !g_ascii_strncasecmp(c
+3, " />", 3))) {
1980 c
= strchr(c
, '>') + 1;
1982 xhtml
= g_string_append(xhtml
, "<br/>");
1983 if(plain
&& *c
!= '\n')
1984 plain
= g_string_append_c(plain
, '\n');
1987 if(!g_ascii_strncasecmp(c
, "<b>", 3) || !g_ascii_strncasecmp(c
, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c
, "<strong>", strlen("<strong>"))) {
1988 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
1991 else if (*(c
+2) == 'o')
1992 pt
->src_tag
= "bold";
1994 pt
->src_tag
= "strong";
1995 pt
->dest_tag
= "span";
1996 tags
= g_list_prepend(tags
, pt
);
1997 c
= strchr(c
, '>') + 1;
1999 xhtml
= g_string_append(xhtml
, "<span style='font-weight: bold;'>");
2002 if(!g_ascii_strncasecmp(c
, "<u>", 3) || !g_ascii_strncasecmp(c
, "<underline>", strlen("<underline>"))) {
2003 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2004 pt
->src_tag
= *(c
+2) == '>' ? "u" : "underline";
2005 pt
->dest_tag
= "span";
2006 tags
= g_list_prepend(tags
, pt
);
2007 c
= strchr(c
, '>') + 1;
2009 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: underline;'>");
2012 if(!g_ascii_strncasecmp(c
, "<s>", 3) || !g_ascii_strncasecmp(c
, "<strike>", strlen("<strike>"))) {
2013 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2014 pt
->src_tag
= *(c
+2) == '>' ? "s" : "strike";
2015 pt
->dest_tag
= "span";
2016 tags
= g_list_prepend(tags
, pt
);
2017 c
= strchr(c
, '>') + 1;
2019 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: line-through;'>");
2022 if(!g_ascii_strncasecmp(c
, "<sub>", 5)) {
2023 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2024 pt
->src_tag
= "sub";
2025 pt
->dest_tag
= "span";
2026 tags
= g_list_prepend(tags
, pt
);
2027 c
= strchr(c
, '>') + 1;
2029 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:sub;'>");
2032 if(!g_ascii_strncasecmp(c
, "<sup>", 5)) {
2033 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2034 pt
->src_tag
= "sup";
2035 pt
->dest_tag
= "span";
2036 tags
= g_list_prepend(tags
, pt
);
2037 c
= strchr(c
, '>') + 1;
2039 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:super;'>");
2042 if (!g_ascii_strncasecmp(c
, "<img", 4) && (*(c
+4) == '>' || *(c
+4) == ' ')) {
2043 const char *p
= c
+ 4;
2044 GString
*src
= NULL
, *alt
= NULL
;
2045 #define ESCAPE(from, to) \
2046 CHECK_QUOTE(from); \
2047 while (VALID_CHAR(from)) { \
2049 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2050 to = g_string_append(to, "&"); \
2051 else if (*from == '\'') \
2052 to = g_string_append(to, "'"); \
2054 to = g_string_append_c(to, *from); \
2058 while (*p
&& *p
!= '>') {
2059 if (!g_ascii_strncasecmp(p
, "src=", 4)) {
2060 const char *q
= p
+ 4;
2062 g_string_free(src
, TRUE
);
2063 src
= g_string_new("");
2066 } else if (!g_ascii_strncasecmp(p
, "alt=", 4)) {
2067 const char *q
= p
+ 4;
2069 g_string_free(alt
, TRUE
);
2070 alt
= g_string_new("");
2078 if ((c
= strchr(p
, '>')) != NULL
)
2082 /* src and alt are required! */
2084 g_string_append_printf(xhtml
, "<img src='%s' alt='%s' />", g_strstrip(src
->str
), alt
? alt
->str
: "");
2087 plain
= g_string_append(plain
, purple_unescape_html(alt
->str
));
2089 xhtml
= g_string_append(xhtml
, alt
->str
);
2090 g_string_free(alt
, TRUE
);
2092 g_string_free(src
, TRUE
);
2095 if (!g_ascii_strncasecmp(c
, "<a", 2) && (*(c
+2) == '>' || *(c
+2) == ' ')) {
2096 const char *p
= c
+ 2;
2097 struct purple_parse_tag
*pt
;
2098 while (*p
&& *p
!= '>') {
2099 if (!g_ascii_strncasecmp(p
, "href=", 5)) {
2100 const char *q
= p
+ 5;
2102 g_string_free(url
, TRUE
);
2103 url
= g_string_new("");
2105 g_string_free(cdata
, TRUE
);
2106 cdata
= g_string_new("");
2108 while (VALID_CHAR(q
)) {
2110 if ((*q
== '&') && (purple_markup_unescape_entity(q
, &len
) == NULL
))
2111 url
= g_string_append(url
, "&");
2113 url
= g_string_append(url
, """);
2115 url
= g_string_append_c(url
, *q
);
2123 if ((c
= strchr(p
, '>')) != NULL
)
2127 pt
= g_new0(struct purple_parse_tag
, 1);
2130 tags
= g_list_prepend(tags
, pt
);
2132 g_string_append_printf(xhtml
, "<a href=\"%s\">", url
? g_strstrip(url
->str
) : "");
2135 #define ESCAPE(from, to) \
2136 CHECK_QUOTE(from); \
2137 while (VALID_CHAR(from)) { \
2139 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2140 to = g_string_append(to, "&"); \
2141 else if (*from == '\'') \
2142 to = g_string_append_c(to, '\"'); \
2144 to = g_string_append_c(to, *from); \
2147 if(!g_ascii_strncasecmp(c
, "<font", 5) && (*(c
+5) == '>' || *(c
+5) == ' ')) {
2148 const char *p
= c
+ 5;
2149 GString
*style
= g_string_new("");
2150 struct purple_parse_tag
*pt
;
2151 while (*p
&& *p
!= '>') {
2152 if (!g_ascii_strncasecmp(p
, "back=", 5)) {
2153 const char *q
= p
+ 5;
2154 GString
*color
= g_string_new("");
2156 g_string_append_printf(style
, "background: %s; ", color
->str
);
2157 g_string_free(color
, TRUE
);
2159 } else if (!g_ascii_strncasecmp(p
, "color=", 6)) {
2160 const char *q
= p
+ 6;
2161 GString
*color
= g_string_new("");
2163 g_string_append_printf(style
, "color: %s; ", color
->str
);
2164 g_string_free(color
, TRUE
);
2166 } else if (!g_ascii_strncasecmp(p
, "face=", 5)) {
2167 const char *q
= p
+ 5;
2168 GString
*face
= g_string_new("");
2170 g_string_append_printf(style
, "font-family: %s; ", g_strstrip(face
->str
));
2171 g_string_free(face
, TRUE
);
2173 } else if (!g_ascii_strncasecmp(p
, "size=", 5)) {
2174 const char *q
= p
+ 5;
2176 const char *size
= "medium";
2203 g_string_append_printf(style
, "font-size: %s; ", size
);
2209 if ((c
= strchr(p
, '>')) != NULL
)
2213 pt
= g_new0(struct purple_parse_tag
, 1);
2214 pt
->src_tag
= "font";
2215 pt
->dest_tag
= "span";
2216 tags
= g_list_prepend(tags
, pt
);
2217 if(style
->len
&& xhtml
)
2218 g_string_append_printf(xhtml
, "<span style='%s'>", g_strstrip(style
->str
));
2221 g_string_free(style
, TRUE
);
2225 if (!g_ascii_strncasecmp(c
, "<body ", 6)) {
2226 const char *p
= c
+ 6;
2227 gboolean did_something
= FALSE
;
2228 while (*p
&& *p
!= '>') {
2229 if (!g_ascii_strncasecmp(p
, "bgcolor=", 8)) {
2230 const char *q
= p
+ 8;
2231 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2232 GString
*color
= g_string_new("");
2234 while (VALID_CHAR(q
)) {
2235 color
= g_string_append_c(color
, *q
);
2239 g_string_append_printf(xhtml
, "<span style='background: %s;'>", g_strstrip(color
->str
));
2240 g_string_free(color
, TRUE
);
2241 if ((c
= strchr(p
, '>')) != NULL
)
2245 pt
->src_tag
= "body";
2246 pt
->dest_tag
= "span";
2247 tags
= g_list_prepend(tags
, pt
);
2248 did_something
= TRUE
;
2253 if (did_something
) continue;
2255 /* this has to come after the special case for bgcolor */
2257 if(!g_ascii_strncasecmp(c
, "<!--", strlen("<!--"))) {
2258 char *p
= strstr(c
+ strlen("<!--"), "-->");
2261 xhtml
= g_string_append(xhtml
, "<!--");
2262 c
+= strlen("<!--");
2268 xhtml
= g_string_append(xhtml
, "<");
2270 plain
= g_string_append_c(plain
, '<');
2273 } else if(*c
== '&') {
2278 if ((pln
= purple_markup_unescape_entity(c
, &len
)) == NULL
) {
2280 g_snprintf(buf
, sizeof(buf
), "%c", *c
);
2284 xhtml
= g_string_append_len(xhtml
, c
, len
);
2286 plain
= g_string_append(plain
, pln
);
2288 cdata
= g_string_append_len(cdata
, c
, len
);
2292 xhtml
= g_string_append_c(xhtml
, *c
);
2294 plain
= g_string_append_c(plain
, *c
);
2296 cdata
= g_string_append_c(cdata
, *c
);
2301 for (tag
= tags
; tag
; tag
= tag
->next
) {
2302 struct purple_parse_tag
*pt
= tag
->data
;
2304 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
2309 *xhtml_out
= g_string_free(xhtml
, FALSE
);
2311 *plain_out
= g_string_free(plain
, FALSE
);
2313 g_string_free(url
, TRUE
);
2315 g_string_free(cdata
, TRUE
);
2320 /* The following are probably reasonable changes:
2321 * - \n should be converted to a normal space
2322 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
2323 * - We want to turn </td>#whitespace<td> sequences into a single tab
2324 * - We want to turn <td> into a single tab (for msn profile "parsing")
2325 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
2326 * - <script>...</script> and <style>...</style> should be completely removed
2330 purple_markup_strip_html(const char *str
)
2332 int i
, j
, k
, entlen
;
2333 gboolean visible
= TRUE
;
2334 gboolean closing_td_p
= FALSE
;
2336 const gchar
*cdata_close_tag
= NULL
, *ent
;
2343 str2
= g_strdup(str
);
2345 for (i
= 0, j
= 0; str2
[i
]; i
++)
2349 if (cdata_close_tag
)
2351 /* Note: Don't even assume any other tag is a tag in CDATA */
2352 if (g_ascii_strncasecmp(str2
+ i
, cdata_close_tag
,
2353 strlen(cdata_close_tag
)) == 0)
2355 i
+= strlen(cdata_close_tag
) - 1;
2356 cdata_close_tag
= NULL
;
2360 else if (g_ascii_strncasecmp(str2
+ i
, "<td", 3) == 0 && closing_td_p
)
2365 else if (g_ascii_strncasecmp(str2
+ i
, "</td>", 5) == 0)
2367 closing_td_p
= TRUE
;
2372 closing_td_p
= FALSE
;
2378 if(g_ascii_isspace(str2
[k
]))
2382 /* Scan until we end the tag either implicitly (closed start
2383 * tag) or explicitly, using a sloppy method (i.e., < or >
2384 * inside quoted attributes will screw us up)
2386 while (str2
[k
] && str2
[k
] != '<' && str2
[k
] != '>')
2391 /* If we've got an <a> tag with an href, save the address
2392 * to print later. */
2393 if (g_ascii_strncasecmp(str2
+ i
, "<a", 2) == 0 &&
2394 g_ascii_isspace(str2
[i
+2]))
2396 int st
; /* start of href, inclusive [ */
2397 int end
; /* end of href, exclusive ) */
2399 /* Find start of href */
2400 for (st
= i
+ 3; st
< k
; st
++)
2402 if (g_ascii_strncasecmp(str2
+st
, "href=", 5) == 0)
2405 if (str2
[st
] == '"' || str2
[st
] == '\'')
2413 /* find end of address */
2414 for (end
= st
; end
< k
&& str2
[end
] != delim
; end
++)
2416 /* All the work is done in the loop construct above. */
2419 /* If there's an address, save it. If there was
2420 * already one saved, kill it. */
2425 tmp
= g_strndup(str2
+ st
, end
- st
);
2426 href
= purple_unescape_html(tmp
);
2432 /* Replace </a> with an ascii representation of the
2433 * address the link was pointing to. */
2434 else if (href
!= NULL
&& g_ascii_strncasecmp(str2
+ i
, "</a>", 4) == 0)
2436 size_t hrlen
= strlen(href
);
2438 /* Only insert the href if it's different from the CDATA. */
2439 if ((hrlen
!= (gsize
)(j
- href_st
) ||
2440 strncmp(str2
+ href_st
, href
, hrlen
)) &&
2441 (hrlen
!= (gsize
)(j
- href_st
+ 7) || /* 7 == strlen("http://") */
2442 strncmp(str2
+ href_st
, href
+ 7, hrlen
- 7)))
2446 g_memmove(str2
+ j
, href
, hrlen
);
2454 /* Check for tags which should be mapped to newline (but ignore some of
2455 * the tags at the beginning of the text) */
2456 else if ((j
&& (g_ascii_strncasecmp(str2
+ i
, "<p>", 3) == 0
2457 || g_ascii_strncasecmp(str2
+ i
, "<tr", 3) == 0
2458 || g_ascii_strncasecmp(str2
+ i
, "<hr", 3) == 0
2459 || g_ascii_strncasecmp(str2
+ i
, "<li", 3) == 0
2460 || g_ascii_strncasecmp(str2
+ i
, "<div", 4) == 0))
2461 || g_ascii_strncasecmp(str2
+ i
, "<br", 3) == 0
2462 || g_ascii_strncasecmp(str2
+ i
, "</table>", 8) == 0)
2466 /* Check for tags which begin CDATA and need to be closed */
2467 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */
2468 else if (g_ascii_strncasecmp(str2
+ i
, "<option", 7) == 0)
2470 /* FIXME: We should not do this if the OPTION is SELECT'd */
2471 cdata_close_tag
= "</option>";
2474 else if (g_ascii_strncasecmp(str2
+ i
, "<script", 7) == 0)
2476 cdata_close_tag
= "</script>";
2478 else if (g_ascii_strncasecmp(str2
+ i
, "<style", 6) == 0)
2480 cdata_close_tag
= "</style>";
2482 /* Update the index and continue checking after the tag */
2483 i
= (str2
[k
] == '<' || str2
[k
] == '\0')? k
- 1: k
;
2487 else if (cdata_close_tag
)
2491 else if (!g_ascii_isspace(str2
[i
]))
2496 if (str2
[i
] == '&' && (ent
= purple_markup_unescape_entity(str2
+ i
, &entlen
)) != NULL
)
2505 str2
[j
++] = g_ascii_isspace(str2
[i
])? ' ': str2
[i
];
2534 badentity(const char *c
)
2536 if (!g_ascii_strncasecmp(c
, "<", 4) ||
2537 !g_ascii_strncasecmp(c
, ">", 4) ||
2538 !g_ascii_strncasecmp(c
, """, 6)) {
2545 process_link(GString
*ret
,
2546 const char *start
, const char *c
,
2548 const char *urlprefix
,
2551 char *url_buf
, *tmpurlbuf
;
2555 if (!badchar(*t
) && !badentity(t
))
2558 if (t
- c
== matchlen
)
2561 if (*t
== ',' && *(t
+ 1) != ' ') {
2565 if (t
> start
&& *(t
- 1) == '.')
2567 if (t
> start
&& *(t
- 1) == ')' && inside_paren
> 0)
2570 url_buf
= g_strndup(c
, t
- c
);
2571 tmpurlbuf
= purple_unescape_html(url_buf
);
2572 g_string_append_printf(ret
, "<A HREF=\"%s%s\">%s</A>",
2574 tmpurlbuf
, url_buf
);
2584 purple_markup_linkify(const char *text
)
2586 const char *c
, *t
, *q
= NULL
;
2587 char *tmpurlbuf
, *url_buf
;
2589 gboolean inside_html
= FALSE
;
2590 int inside_paren
= 0;
2596 ret
= g_string_new("");
2601 if(*c
== '(' && !inside_html
) {
2603 ret
= g_string_append_c(ret
, *c
);
2609 inside_html
= FALSE
;
2610 } else if(!q
&& (*c
== '\"' || *c
== '\'')) {
2616 } else if(*c
== '<') {
2618 if (!g_ascii_strncasecmp(c
, "<A", 2)) {
2620 if (!g_ascii_strncasecmp(c
, "/A>", 3)) {
2621 inside_html
= FALSE
;
2624 ret
= g_string_append_c(ret
, *c
);
2630 } else if (!g_ascii_strncasecmp(c
, "http://", 7)) {
2631 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2632 } else if (!g_ascii_strncasecmp(c
, "https://", 8)) {
2633 c
= process_link(ret
, text
, c
, 8, "", inside_paren
);
2634 } else if (!g_ascii_strncasecmp(c
, "ftp://", 6)) {
2635 c
= process_link(ret
, text
, c
, 6, "", inside_paren
);
2636 } else if (!g_ascii_strncasecmp(c
, "sftp://", 7)) {
2637 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2638 } else if (!g_ascii_strncasecmp(c
, "file://", 7)) {
2639 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2640 } else if (!g_ascii_strncasecmp(c
, "www.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2641 c
= process_link(ret
, text
, c
, 4, "http://", inside_paren
);
2642 } else if (!g_ascii_strncasecmp(c
, "ftp.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2643 c
= process_link(ret
, text
, c
, 4, "ftp://", inside_paren
);
2644 } else if (!g_ascii_strncasecmp(c
, "xmpp:", 5) && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2645 c
= process_link(ret
, text
, c
, 5, "", inside_paren
);
2646 } else if (!g_ascii_strncasecmp(c
, "mailto:", 7)) {
2649 if (badchar(*t
) || badentity(t
)) {
2654 if (t
> text
&& *(t
- 1) == '.')
2656 if ((d
= strstr(c
+ 7, "?")) != NULL
&& d
< t
)
2657 url_buf
= g_strndup(c
+ 7, d
- c
- 7);
2659 url_buf
= g_strndup(c
+ 7, t
- c
- 7);
2660 if (!purple_email_is_valid(url_buf
)) {
2665 url_buf
= g_strndup(c
, t
- c
);
2666 tmpurlbuf
= purple_unescape_html(url_buf
);
2667 g_string_append_printf(ret
, "<A HREF=\"%s\">%s</A>",
2668 tmpurlbuf
, url_buf
);
2676 } else if (c
!= text
&& (*c
== '@')) {
2678 GString
*gurl_buf
= NULL
;
2679 const char illegal_chars
[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2681 if (strchr(illegal_chars
,*(c
- 1)) || strchr(illegal_chars
, *(c
+ 1)))
2685 gurl_buf
= g_string_new("");
2690 /* iterate backwards grabbing the local part of an email address */
2691 g
= g_utf8_get_char(t
);
2692 if (badchar(*t
) || (g
>= 127) || (*t
== '(') ||
2693 ((*t
== ';') && ((t
> (text
+2) && (!g_ascii_strncasecmp(t
- 3, "<", 4) ||
2694 !g_ascii_strncasecmp(t
- 3, ">", 4))) ||
2695 (t
> (text
+4) && (!g_ascii_strncasecmp(t
- 5, """, 6)))))) {
2696 /* local part will already be part of ret, strip it out */
2697 ret
= g_string_truncate(ret
, ret
->len
- (c
- t
));
2698 ret
= g_string_append_unichar(ret
, g
);
2701 g_string_prepend_unichar(gurl_buf
, g
);
2702 t
= g_utf8_find_prev_char(text
, t
);
2704 ret
= g_string_assign(ret
, "");
2710 t
= g_utf8_find_next_char(c
, NULL
);
2713 /* iterate forwards grabbing the domain part of an email address */
2714 g
= g_utf8_get_char(t
);
2715 if (badchar(*t
) || (g
>= 127) || (*t
== ')') || badentity(t
)) {
2718 url_buf
= g_string_free(gurl_buf
, FALSE
);
2720 /* strip off trailing periods */
2722 for (d
= url_buf
+ strlen(url_buf
) - 1; *d
== '.'; d
--, t
--)
2726 tmpurlbuf
= purple_unescape_html(url_buf
);
2727 if (purple_email_is_valid(tmpurlbuf
)) {
2728 g_string_append_printf(ret
, "<A HREF=\"mailto:%s\">%s</A>",
2729 tmpurlbuf
, url_buf
);
2731 g_string_append(ret
, url_buf
);
2739 g_string_append_unichar(gurl_buf
, g
);
2740 t
= g_utf8_find_next_char(t
, NULL
);
2745 if(*c
== ')' && !inside_html
) {
2747 ret
= g_string_append_c(ret
, *c
);
2754 ret
= g_string_append_c(ret
, *c
);
2758 return g_string_free(ret
, FALSE
);
2761 char *purple_unescape_text(const char *in
)
2769 ret
= g_string_new("");
2774 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2775 g_string_append(ret
, ent
);
2778 g_string_append_c(ret
, *c
);
2783 return g_string_free(ret
, FALSE
);
2786 char *purple_unescape_html(const char *html
)
2789 const char *c
= html
;
2794 ret
= g_string_new("");
2799 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2800 g_string_append(ret
, ent
);
2802 } else if (!strncmp(c
, "<br>", 4)) {
2803 g_string_append_c(ret
, '\n');
2806 g_string_append_c(ret
, *c
);
2811 return g_string_free(ret
, FALSE
);
2815 purple_markup_slice(const char *str
, guint x
, guint y
)
2820 gboolean appended
= FALSE
;
2824 g_return_val_if_fail(str
!= NULL
, NULL
);
2825 g_return_val_if_fail(x
<= y
, NULL
);
2828 return g_strdup("");
2830 ret
= g_string_new("");
2833 while (*str
&& (z
< y
)) {
2834 c
= g_utf8_get_char(str
);
2837 char *end
= strchr(str
, '>');
2840 g_string_free(ret
, TRUE
);
2841 while ((tag
= g_queue_pop_head(q
)))
2847 if (!g_ascii_strncasecmp(str
, "<img ", 5)) {
2848 z
+= strlen("[Image]");
2849 } else if (!g_ascii_strncasecmp(str
, "<br", 3)) {
2851 } else if (!g_ascii_strncasecmp(str
, "<hr>", 4)) {
2852 z
+= strlen("\n---\n");
2853 } else if (!g_ascii_strncasecmp(str
, "</", 2)) {
2857 tmp
= g_queue_pop_head(q
);
2861 /* push it unto the stack */
2864 tmp
= g_strndup(str
, end
- str
+ 1);
2865 g_queue_push_head(q
, tmp
);
2870 g_string_append_len(ret
, str
, end
- str
+ 1);
2874 } else if (c
== '&') {
2875 char *end
= strchr(str
, ';');
2877 g_string_free(ret
, TRUE
);
2878 while ((tag
= g_queue_pop_head(q
)))
2886 g_string_append_len(ret
, str
, end
- str
+ 1);
2891 if (z
== x
&& z
> 0 && !appended
) {
2896 g_string_append(ret
, tag
);
2903 g_string_append_unichar(ret
, c
);
2907 str
= g_utf8_next_char(str
);
2910 while ((tag
= g_queue_pop_head(q
))) {
2913 name
= purple_markup_get_tag_name(tag
);
2914 g_string_append_printf(ret
, "</%s>", name
);
2920 return g_string_free(ret
, FALSE
);
2924 purple_markup_get_tag_name(const char *tag
)
2927 g_return_val_if_fail(tag
!= NULL
, NULL
);
2928 g_return_val_if_fail(*tag
== '<', NULL
);
2930 for (i
= 1; tag
[i
]; i
++)
2931 if (tag
[i
] == '>' || tag
[i
] == ' ' || tag
[i
] == '/')
2934 return g_strndup(tag
+1, i
-1);
2937 /**************************************************************************
2938 * Path/Filename Functions
2939 **************************************************************************/
2941 purple_home_dir(void)
2944 return g_get_home_dir();
2946 return wpurple_home_dir();
2950 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2952 purple_user_dir(void)
2954 if (custom_user_dir
!= NULL
)
2955 return custom_user_dir
;
2957 user_dir
= g_build_filename(purple_home_dir(), ".purple", NULL
);
2963 purple_cache_dir(void)
2966 if (!custom_user_dir
) {
2967 cache_dir
= g_build_filename(g_get_user_cache_dir(), "purple", NULL
);
2969 cache_dir
= g_build_filename(custom_user_dir
, "cache", NULL
);
2977 purple_config_dir(void)
2980 if (!custom_user_dir
) {
2981 config_dir
= g_build_filename(g_get_user_config_dir(), "purple", NULL
);
2983 config_dir
= g_build_filename(custom_user_dir
, "config", NULL
);
2991 purple_data_dir(void)
2994 if (!custom_user_dir
) {
2995 data_dir
= g_build_filename(g_get_user_data_dir(), "purple", NULL
);
2997 data_dir
= g_build_filename(custom_user_dir
, "data", NULL
);
3005 purple_move_to_xdg_base_dir(const char *purple_xdg_dir
, char *path
)
3008 gboolean xdg_path_exists
;
3010 /* Check if destination dir exists, otherwise create it */
3011 xdg_path_exists
= g_file_test(purple_xdg_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
3012 if (!xdg_path_exists
) {
3015 mkdir_res
= purple_build_dir(purple_xdg_dir
, S_IRWXU
);
3016 if (mkdir_res
== -1) {
3017 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
3018 purple_xdg_dir
, g_strerror(errno
));
3023 xdg_path
= g_build_filename(purple_xdg_dir
, path
, NULL
);
3024 xdg_path_exists
= g_file_test(xdg_path
, G_FILE_TEST_EXISTS
);
3025 if (!xdg_path_exists
) {
3027 gboolean old_path_exists
;
3029 old_path
= g_build_filename(purple_user_dir(), path
, NULL
);
3030 old_path_exists
= g_file_test(old_path
, G_FILE_TEST_EXISTS
);
3031 if (old_path_exists
) {
3032 g_rename(old_path
, xdg_path
);
3045 void purple_util_set_user_dir(const char *dir
)
3047 g_free(custom_user_dir
);
3049 if (dir
!= NULL
&& *dir
)
3050 custom_user_dir
= g_strdup(dir
);
3052 custom_user_dir
= NULL
;
3055 int purple_build_dir(const char *path
, int mode
)
3057 return g_mkdir_with_parents(path
, mode
);
3061 purple_util_write_data_to_file_common(const char *dir
, const char *filename
, const char *data
, gssize size
)
3063 gchar
*filename_full
;
3064 gboolean ret
= FALSE
;
3066 g_return_val_if_fail(dir
!= NULL
, FALSE
);
3068 purple_debug_misc("util", "Writing file %s to directory %s",
3071 /* Ensure the directory exists */
3072 if (!g_file_test(dir
, G_FILE_TEST_IS_DIR
))
3074 if (g_mkdir(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
) == -1)
3076 purple_debug_error("util", "Error creating directory %s: %s\n",
3077 dir
, g_strerror(errno
));
3082 filename_full
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", dir
, filename
);
3084 ret
= purple_util_write_data_to_file_absolute(filename_full
, data
, size
);
3086 g_free(filename_full
);
3091 purple_util_write_data_to_file(const char *filename
, const char *data
, gssize size
)
3093 const char *user_dir
= purple_user_dir();
3094 gboolean ret
= purple_util_write_data_to_file_common(user_dir
, filename
, data
, size
);
3100 purple_util_write_data_to_cache_file(const char *filename
, const char *data
, gssize size
)
3102 const char *cache_dir
= purple_cache_dir();
3103 gboolean ret
= purple_util_write_data_to_file_common(cache_dir
, filename
, data
, size
);
3109 purple_util_write_data_to_config_file(const char *filename
, const char *data
, gssize size
)
3111 const char *config_dir
= purple_cache_dir();
3112 gboolean ret
= purple_util_write_data_to_file_common(config_dir
, filename
, data
, size
);
3118 purple_util_write_data_to_data_file(const char *filename
, const char *data
, gssize size
)
3120 const char *data_dir
= purple_cache_dir();
3121 gboolean ret
= purple_util_write_data_to_file_common(data_dir
, filename
, data
, size
);
3127 * This function is long and beautiful, like my--um, yeah. Anyway,
3128 * it includes lots of error checking so as we don't overwrite
3129 * people's settings if there is a problem writing the new values.
3132 purple_util_write_data_to_file_absolute(const char *filename_full
, const char *data
, gssize size
)
3134 gchar
*filename_temp
;
3136 gsize real_size
, byteswritten
;
3142 purple_debug_misc("util", "Writing file %s",
3145 g_return_val_if_fail((size
>= -1), FALSE
);
3147 filename_temp
= g_strdup_printf("%s.save", filename_full
);
3149 /* Remove an old temporary file, if one exists */
3150 if (g_file_test(filename_temp
, G_FILE_TEST_EXISTS
))
3152 if (g_unlink(filename_temp
) == -1)
3154 purple_debug_error("util", "Error removing old file "
3156 filename_temp
, g_strerror(errno
));
3161 file
= g_fopen(filename_temp
, "wb");
3164 purple_debug_error("util", "Error opening file %s for "
3166 filename_temp
, g_strerror(errno
));
3167 g_free(filename_temp
);
3172 real_size
= (size
== -1) ? strlen(data
) : (size_t) size
;
3173 byteswritten
= fwrite(data
, 1, real_size
, file
);
3177 /* Set file permissions */
3178 if (fchmod(fileno(file
), S_IRUSR
| S_IWUSR
) == -1) {
3179 purple_debug_error("util", "Error setting permissions of "
3180 "file %s: %s\n", filename_temp
, g_strerror(errno
));
3184 /* Apparently XFS (and possibly other filesystems) do not
3185 * guarantee that file data is flushed before file metadata,
3186 * so this procedure is insufficient without some flushage. */
3187 if (fflush(file
) < 0) {
3188 purple_debug_error("util", "Error flushing %s: %s\n",
3189 filename_temp
, g_strerror(errno
));
3190 g_free(filename_temp
);
3194 if (fsync(fileno(file
)) < 0) {
3195 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
3196 filename_temp
, g_strerror(errno
));
3197 g_free(filename_temp
);
3204 if (fclose(file
) != 0)
3206 purple_debug_error("util", "Error closing file %s: %s\n",
3207 filename_temp
, g_strerror(errno
));
3208 g_free(filename_temp
);
3213 /* This is the same effect (we hope) as the HAVE_FILENO block
3214 * above, but for systems without fileno(). */
3215 if ((fd
= open(filename_temp
, O_RDWR
)) < 0) {
3216 purple_debug_error("util", "Error opening file %s for flush: %s\n",
3217 filename_temp
, g_strerror(errno
));
3218 g_free(filename_temp
);
3224 if (fchmod(fd
, S_IRUSR
| S_IWUSR
) == -1) {
3225 purple_debug_error("util", "Error setting permissions of "
3226 "file %s: %s\n", filename_temp
, g_strerror(errno
));
3230 if (fsync(fd
) < 0) {
3231 purple_debug_error("util", "Error syncing %s: %s\n",
3232 filename_temp
, g_strerror(errno
));
3233 g_free(filename_temp
);
3237 if (close(fd
) < 0) {
3238 purple_debug_error("util", "Error closing %s after sync: %s\n",
3239 filename_temp
, g_strerror(errno
));
3240 g_free(filename_temp
);
3245 /* Ensure the file is the correct size */
3246 if (byteswritten
!= real_size
)
3248 purple_debug_error("util", "Error writing to file %s: Wrote %"
3249 G_GSIZE_FORMAT
" bytes "
3250 "but should have written %" G_GSIZE_FORMAT
3251 "; is your disk full?\n",
3252 filename_temp
, byteswritten
, real_size
);
3253 g_free(filename_temp
);
3256 #ifndef __COVERITY__
3257 /* Use stat to be absolutely sure.
3258 * It causes TOCTOU coverity warning (against g_rename below),
3259 * but it's not a threat for us.
3261 if ((g_stat(filename_temp
, &st
) == -1) || ((gsize
)st
.st_size
!= real_size
)) {
3262 purple_debug_error("util", "Error writing data to file %s: "
3263 "couldn't g_stat file", filename_temp
);
3264 g_free(filename_temp
);
3267 #endif /* __COVERITY__ */
3269 /* Rename to the REAL name */
3270 if (g_rename(filename_temp
, filename_full
) == -1)
3272 purple_debug_error("util", "Error renaming %s to %s: %s\n",
3273 filename_temp
, filename_full
,
3277 g_free(filename_temp
);
3283 purple_util_read_xml_from_file(const char *filename
, const char *description
)
3285 return purple_xmlnode_from_file(purple_user_dir(), filename
, description
, "util");
3289 purple_util_read_xml_from_cache_file(const char *filename
, const char *description
)
3291 return purple_xmlnode_from_file(purple_cache_dir(), filename
, description
, "util");
3295 purple_util_read_xml_from_config_file(const char *filename
, const char *description
)
3297 return purple_xmlnode_from_file(purple_config_dir(), filename
, description
, "util");
3301 purple_util_read_xml_from_data_file(const char *filename
, const char *description
)
3303 return purple_xmlnode_from_file(purple_data_dir(), filename
, description
, "util");
3307 * Like mkstemp() but returns a file pointer, uses a pre-set template,
3308 * uses the semantics of tempnam() for the directory to use and allocates
3309 * the space for the filepath.
3311 * Caller is responsible for closing the file and removing it when done,
3312 * as well as freeing the space pointed-to by "path" with g_free().
3314 * Returns NULL on failure and cleans up after itself if so.
3316 static const char *purple_mkstemp_templ
= {"purpleXXXXXX"};
3319 purple_mkstemp(char **fpath
, gboolean binary
)
3321 const gchar
*tmpdir
;
3325 g_return_val_if_fail(fpath
!= NULL
, NULL
);
3327 if((tmpdir
= (gchar
*)g_get_tmp_dir()) != NULL
) {
3328 if((*fpath
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", tmpdir
, purple_mkstemp_templ
)) != NULL
) {
3329 fd
= g_mkstemp(*fpath
);
3331 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3332 "Couldn't make \"%s\", error: %d\n",
3335 if((fp
= fdopen(fd
, "r+")) == NULL
) {
3337 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3338 "Couldn't fdopen(), error: %d\n", errno
);
3348 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3349 "g_get_tmp_dir() failed!\n");
3356 purple_program_is_valid(const char *program
)
3358 GError
*error
= NULL
;
3361 gboolean is_valid
= FALSE
;
3363 g_return_val_if_fail(program
!= NULL
, FALSE
);
3364 g_return_val_if_fail(*program
!= '\0', FALSE
);
3366 if (!g_shell_parse_argv(program
, NULL
, &argv
, &error
)) {
3367 purple_debug(PURPLE_DEBUG_ERROR
, "program_is_valid",
3368 "Could not parse program '%s': %s\n",
3369 program
, error
->message
);
3370 g_error_free(error
);
3378 progname
= g_find_program_in_path(argv
[0]);
3379 is_valid
= (progname
!= NULL
);
3381 if(purple_debug_is_verbose())
3382 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program
,
3383 is_valid
? "Valid" : "Invalid");
3393 purple_running_gnome(void)
3396 gchar
*tmp
= g_find_program_in_path("gvfs-open");
3399 tmp
= g_find_program_in_path("gnome-open");
3408 tmp
= (gchar
*)g_getenv("GNOME_DESKTOP_SESSION_ID");
3410 return ((tmp
!= NULL
) && (*tmp
!= '\0'));
3417 purple_running_kde(void)
3420 gchar
*tmp
= g_find_program_in_path("kfmclient");
3421 const char *session
;
3427 session
= g_getenv("KDE_FULL_SESSION");
3428 if (purple_strequal(session
, "true"))
3431 /* If you run Purple from Konsole under !KDE, this will provide a
3432 * a false positive. Since we do the GNOME checks first, this is
3433 * only a problem if you're running something !(KDE || GNOME) and
3434 * you run Purple from Konsole. This really shouldn't be a problem. */
3435 return ((g_getenv("KDEDIR") != NULL
) || g_getenv("KDEDIRS") != NULL
);
3442 purple_running_osx(void)
3444 #if defined(__APPLE__)
3451 typedef union purple_sockaddr
{
3453 struct sockaddr_in sa_in
;
3454 #if defined(AF_INET6)
3455 struct sockaddr_in6 sa_in6
;
3457 struct sockaddr_storage sa_stor
;
3461 purple_fd_get_ip(int fd
)
3463 PurpleSockaddr addr
;
3464 socklen_t namelen
= sizeof(addr
);
3467 g_return_val_if_fail(fd
!= 0, NULL
);
3469 if (getsockname(fd
, &(addr
.sa
), &namelen
))
3472 family
= addr
.sa
.sa_family
;
3474 if (family
== AF_INET
) {
3475 return g_strdup(inet_ntoa(addr
.sa_in
.sin_addr
));
3477 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
3478 else if (family
== AF_INET6
) {
3479 char host
[INET6_ADDRSTRLEN
];
3482 tmp
= inet_ntop(family
, &(addr
.sa_in6
.sin6_addr
), host
, sizeof(host
));
3483 return g_strdup(tmp
);
3491 purple_socket_get_family(int fd
)
3493 PurpleSockaddr addr
;
3494 socklen_t len
= sizeof(addr
);
3496 g_return_val_if_fail(fd
>= 0, -1);
3498 if (getsockname(fd
, &(addr
.sa
), &len
))
3501 return addr
.sa
.sa_family
;
3505 purple_socket_speaks_ipv4(int fd
)
3509 g_return_val_if_fail(fd
>= 0, FALSE
);
3511 family
= purple_socket_get_family(fd
);
3516 #if defined(IPV6_V6ONLY)
3520 socklen_t len
= sizeof(val
);
3522 if (getsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, &len
) != 0)
3532 /**************************************************************************
3534 **************************************************************************/
3536 purple_strequal(const gchar
*left
, const gchar
*right
)
3538 return (g_strcmp0(left
, right
) == 0);
3542 purple_normalize(const PurpleAccount
*account
, const char *str
)
3544 const char *ret
= NULL
;
3545 static char buf
[BUF_LEN
];
3547 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3548 g_return_val_if_fail(str
!= NULL
, "");
3550 if (account
!= NULL
)
3552 PurpleProtocol
*protocol
=
3553 purple_protocols_find(purple_account_get_protocol_id(account
));
3555 if (protocol
!= NULL
)
3556 ret
= purple_protocol_client_iface_normalize(protocol
, account
, str
);
3563 tmp
= g_utf8_normalize(str
, -1, G_NORMALIZE_DEFAULT
);
3564 g_snprintf(buf
, sizeof(buf
), "%s", tmp
);
3574 * You probably don't want to call this directly, it is
3575 * mainly for use as a protocol callback function. See the
3576 * comments in util.h.
3579 purple_normalize_nocase(const PurpleAccount
*account
, const char *str
)
3581 static char buf
[BUF_LEN
];
3584 g_return_val_if_fail(str
!= NULL
, NULL
);
3586 tmp1
= g_utf8_strdown(str
, -1);
3587 tmp2
= g_utf8_normalize(tmp1
, -1, G_NORMALIZE_DEFAULT
);
3588 g_snprintf(buf
, sizeof(buf
), "%s", tmp2
? tmp2
: "");
3596 purple_validate(const PurpleProtocol
*protocol
, const char *str
)
3598 const char *normalized
;
3600 g_return_val_if_fail(protocol
!= NULL
, FALSE
);
3601 g_return_val_if_fail(str
!= NULL
, FALSE
);
3606 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol
, CLIENT_IFACE
, normalize
))
3609 normalized
= purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol
),
3612 return (NULL
!= normalized
);
3616 purple_strdup_withhtml(const gchar
*src
)
3618 gulong destsize
, i
, j
;
3621 g_return_val_if_fail(src
!= NULL
, NULL
);
3623 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3625 for (i
= 0; src
[i
] != '\0'; i
++)
3629 else if (src
[i
] != '\r')
3633 dest
= g_malloc(destsize
);
3635 /* Copy stuff, ignoring \r's, because they are dumb */
3636 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
3637 if (src
[i
] == '\n') {
3638 strcpy(&dest
[j
], "<BR>");
3640 } else if (src
[i
] != '\r')
3644 dest
[destsize
-1] = '\0';
3650 purple_str_has_prefix(const char *s
, const char *p
)
3652 return g_str_has_prefix(s
, p
);
3656 purple_str_has_caseprefix(const gchar
*s
, const gchar
*p
)
3658 g_return_val_if_fail(s
, FALSE
);
3659 g_return_val_if_fail(p
, FALSE
);
3661 return (g_ascii_strncasecmp(s
, p
, strlen(p
)) == 0);
3665 purple_str_has_suffix(const char *s
, const char *x
)
3667 return g_str_has_suffix(s
, x
);
3671 purple_str_add_cr(const char *text
)
3677 g_return_val_if_fail(text
!= NULL
, NULL
);
3679 if (text
[0] == '\n')
3681 for (i
= 1; i
< strlen(text
); i
++)
3682 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3686 return g_strdup(text
);
3688 ret
= g_malloc0(strlen(text
) + count
+ 1);
3691 if (text
[i
] == '\n')
3693 ret
[j
++] = text
[i
++];
3694 for (; i
< strlen(text
); i
++) {
3695 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3704 purple_str_strip_char(char *text
, char thechar
)
3708 g_return_if_fail(text
!= NULL
);
3710 for (i
= 0, j
= 0; text
[i
]; i
++)
3711 if (text
[i
] != thechar
)
3712 text
[j
++] = text
[i
];
3718 purple_util_chrreplace(char *string
, char delimiter
,
3723 g_return_if_fail(string
!= NULL
);
3725 while (string
[i
] != '\0')
3727 if (string
[i
] == delimiter
)
3728 string
[i
] = replacement
;
3734 purple_strreplace(const char *string
, const char *delimiter
,
3735 const char *replacement
)
3740 g_return_val_if_fail(string
!= NULL
, NULL
);
3741 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3742 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3744 split
= g_strsplit(string
, delimiter
, 0);
3745 ret
= g_strjoinv(replacement
, split
);
3752 purple_strcasereplace(const char *string
, const char *delimiter
,
3753 const char *replacement
)
3756 int length_del
, length_rep
, i
, j
;
3758 g_return_val_if_fail(string
!= NULL
, NULL
);
3759 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3760 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3762 length_del
= strlen(delimiter
);
3763 length_rep
= strlen(replacement
);
3765 /* Count how many times the delimiter appears */
3766 i
= 0; /* position in the source string */
3767 j
= 0; /* number of occurrences of "delimiter" */
3768 while (string
[i
] != '\0') {
3769 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3778 ret
= g_malloc(j
+1);
3780 i
= 0; /* position in the source string */
3781 j
= 0; /* position in the destination string */
3782 while (string
[i
] != '\0') {
3783 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3784 strncpy(&ret
[j
], replacement
, length_rep
);
3799 /** TODO: Expose this when we can add API */
3801 purple_strcasestr_len(const char *haystack
, gssize hlen
, const char *needle
, gssize nlen
)
3803 const char *tmp
, *ret
;
3805 g_return_val_if_fail(haystack
!= NULL
, NULL
);
3806 g_return_val_if_fail(needle
!= NULL
, NULL
);
3809 hlen
= strlen(haystack
);
3811 nlen
= strlen(needle
);
3815 g_return_val_if_fail(hlen
> 0, NULL
);
3816 g_return_val_if_fail(nlen
> 0, NULL
);
3818 while (*tmp
&& !ret
&& (hlen
- (tmp
- haystack
)) >= nlen
) {
3819 if (!g_ascii_strncasecmp(needle
, tmp
, nlen
))
3829 purple_strcasestr(const char *haystack
, const char *needle
)
3831 return purple_strcasestr_len(haystack
, -1, needle
, -1);
3835 purple_str_size_to_units(goffset size
)
3837 static const char * const size_str
[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3839 gsize size_index
= 0;
3842 return g_strdup(_("Calculating..."));
3844 else if (size
== 0) {
3845 return g_strdup(_("Unknown."));
3848 size_mag
= (float)size
;
3850 while ((size_index
< G_N_ELEMENTS(size_str
) - 1) && (size_mag
> 1024)) {
3855 if (size_index
== 0) {
3856 return g_strdup_printf("%" G_GOFFSET_FORMAT
" %s", size
, _(size_str
[size_index
]));
3858 return g_strdup_printf("%.2f %s", size_mag
, _(size_str
[size_index
]));
3864 purple_str_seconds_to_string(guint secs
)
3867 guint days
, hrs
, mins
;
3871 return g_strdup_printf(dngettext(PACKAGE
, "%d second", "%d seconds", secs
), secs
);
3874 days
= secs
/ (60 * 60 * 24);
3875 secs
= secs
% (60 * 60 * 24);
3876 hrs
= secs
/ (60 * 60);
3877 secs
= secs
% (60 * 60);
3879 /* secs = secs % 60; */
3883 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d day", "%d days", days
), days
);
3890 char *tmp
= g_strdup_printf(
3891 dngettext(PACKAGE
, "%s, %d hour", "%s, %d hours", hrs
),
3897 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d hour", "%d hours", hrs
), hrs
);
3904 char *tmp
= g_strdup_printf(
3905 dngettext(PACKAGE
, "%s, %d minute", "%s, %d minutes", mins
),
3911 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d minute", "%d minutes", mins
), mins
);
3919 purple_str_binary_to_ascii(const unsigned char *binary
, guint len
)
3924 g_return_val_if_fail(len
> 0, NULL
);
3926 ret
= g_string_sized_new(len
);
3928 for (i
= 0; i
< len
; i
++)
3929 if (binary
[i
] < 32 || binary
[i
] > 126)
3930 g_string_append_printf(ret
, "\\x%02x", binary
[i
] & 0xFF);
3931 else if (binary
[i
] == '\\')
3932 g_string_append(ret
, "\\\\");
3934 g_string_append_c(ret
, binary
[i
]);
3936 return g_string_free(ret
, FALSE
);
3940 purple_utf16_size(const gunichar2
*str
)
3942 /* UTF16 cannot contain two consequent NUL bytes starting at even
3943 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3949 g_return_val_if_fail(str
!= NULL
, 0);
3953 return i
* sizeof(gunichar2
);
3957 purple_str_wipe(gchar
*str
)
3961 memset(str
, 0, strlen(str
));
3966 purple_utf16_wipe(gunichar2
*str
)
3970 memset(str
, 0, purple_utf16_size(str
));
3974 /**************************************************************************
3976 **************************************************************************/
3978 void purple_got_protocol_handler_uri(const char *uri
)
3982 const char *tmp
, *param_string
;
3984 GHashTable
*params
= NULL
;
3986 if (!(tmp
= strchr(uri
, ':')) || tmp
== uri
) {
3987 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3991 len
= MIN(sizeof(proto
) - 1, (gsize
)(tmp
- uri
));
3993 strncpy(proto
, uri
, len
);
3998 if (g_str_equal(proto
, "xmpp"))
4003 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp
, proto
, delimiter
);
4005 if ((param_string
= strchr(tmp
, '?'))) {
4006 const char *keyend
= NULL
, *pairstart
;
4007 char *key
, *value
= NULL
;
4009 cmd
= g_strndup(tmp
, (param_string
- tmp
));
4012 params
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
4013 pairstart
= tmp
= param_string
;
4015 while (*tmp
|| *pairstart
) {
4016 if (*tmp
== delimiter
|| !(*tmp
)) {
4017 /* If there is no explicit value */
4018 if (keyend
== NULL
) {
4021 /* without these brackets, clang won't
4022 * recognize tmp as a non-NULL
4025 if (keyend
&& keyend
!= pairstart
) {
4027 key
= g_strndup(pairstart
, (keyend
- pairstart
));
4028 /* If there is an explicit value */
4029 if (keyend
!= tmp
&& keyend
!= (tmp
- 1))
4030 value
= g_strndup(keyend
+ 1, (tmp
- keyend
- 1));
4031 for (p
= key
; *p
; ++p
)
4032 *p
= g_ascii_tolower(*p
);
4033 g_hash_table_insert(params
, key
, value
);
4035 keyend
= value
= NULL
;
4036 pairstart
= (*tmp
) ? tmp
+ 1 : tmp
;
4037 } else if (*tmp
== '=')
4044 cmd
= g_strdup(tmp
);
4046 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto
, cmd
, params
);
4050 g_hash_table_destroy(params
);
4054 purple_url_decode(const char *str
)
4056 static char buf
[BUF_LEN
];
4061 g_return_val_if_fail(str
!= NULL
, NULL
);
4064 * XXX - This check could be removed and buf could be made
4065 * dynamically allocated, but this is easier.
4067 if (strlen(str
) >= BUF_LEN
)
4070 for (i
= 0; i
< strlen(str
); i
++) {
4075 strncpy(hex
, str
+ ++i
, 2);
4078 /* i is pointing to the start of the number */
4082 * Now it's at the end and at the start of the for loop
4083 * will be at the next character.
4085 buf
[j
++] = strtol(hex
, NULL
, 16);
4091 if (!g_utf8_validate(buf
, -1, (const char **)&bum
))
4098 purple_url_encode(const char *str
)
4101 static char buf
[BUF_LEN
];
4105 g_return_val_if_fail(str
!= NULL
, NULL
);
4106 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4109 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
4110 gunichar c
= g_utf8_get_char(iter
);
4111 /* If the character is an ASCII character and is alphanumeric
4112 * no need to escape */
4113 if (c
< 128 && (isalnum(c
) || c
== '-' || c
== '.' || c
== '_' || c
== '~')) {
4116 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4117 for (i
= 0; (int)i
< bytes
; i
++) {
4118 if (j
> (BUF_LEN
- 4))
4120 if (i
>= sizeof(utf_char
)) {
4121 g_warn_if_reached();
4124 sprintf(buf
+ j
, "%%%02X", utf_char
[i
] & 0xff);
4135 /* Originally lifted from
4136 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
4137 * ... and slightly modified to be a bit more rfc822 compliant
4138 * ... and modified a bit more to make domain checking rfc1035 compliant
4139 * with the exception permitted in rfc1101 for domains to start with digit
4140 * but not completely checking to avoid conflicts with IP addresses
4143 purple_email_is_valid(const char *address
)
4145 const char *c
, *domain
;
4146 static char *rfc822_specials
= "()<>@,;:\\\"[]";
4148 g_return_val_if_fail(address
!= NULL
, FALSE
);
4150 if (*address
== '.') return FALSE
;
4152 /* first we validate the name portion (name@domain) (rfc822)*/
4153 for (c
= address
; *c
; c
++) {
4154 if (*c
== '\"' && (c
== address
|| *(c
- 1) == '.' || *(c
- 1) == '\"')) {
4157 if (*c
++ && *c
< 127 && *c
!= '\n' && *c
!= '\r') continue;
4160 if (*c
== '\"') break;
4161 if (*c
< ' ' || *c
>= 127) return FALSE
;
4163 if (!*c
++) return FALSE
;
4164 if (*c
== '@') break;
4165 if (*c
!= '.') return FALSE
;
4168 if (*c
== '@') break;
4169 if (*c
<= ' ' || *c
>= 127) return FALSE
;
4170 if (strchr(rfc822_specials
, *c
)) return FALSE
;
4173 /* It's obviously not an email address if we didn't find an '@' above */
4174 if (*c
== '\0') return FALSE
;
4176 /* strictly we should return false if (*(c - 1) == '.') too, but I think
4177 * we should permit user.@domain type addresses - they do work :) */
4178 if (c
== address
) return FALSE
;
4180 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
4181 if (!*(domain
= ++c
)) return FALSE
;
4183 if (*c
== '.' && (c
== domain
|| *(c
- 1) == '.' || *(c
- 1) == '-'))
4185 if (*c
== '-' && (*(c
- 1) == '.' || *(c
- 1) == '@')) return FALSE
;
4186 if ((*c
< '0' && *c
!= '-' && *c
!= '.') || (*c
> '9' && *c
< 'A') ||
4187 (*c
> 'Z' && *c
< 'a') || (*c
> 'z')) return FALSE
;
4190 if (*(c
- 1) == '-') return FALSE
;
4192 return ((c
- domain
) > 3 ? TRUE
: FALSE
);
4196 purple_ipv4_address_is_valid(const char *ip
)
4198 int c
, o1
, o2
, o3
, o4
;
4201 g_return_val_if_fail(ip
!= NULL
, FALSE
);
4203 c
= sscanf(ip
, "%d.%d.%d.%d%c", &o1
, &o2
, &o3
, &o4
, &end
);
4204 if (c
!= 4 || o1
< 0 || o1
> 255 || o2
< 0 || o2
> 255 || o3
< 0 || o3
> 255 || o4
< 0 || o4
> 255)
4210 purple_ipv6_address_is_valid(const gchar
*ip
)
4213 gboolean double_colon
= FALSE
;
4217 g_return_val_if_fail(ip
!= NULL
, FALSE
);
4222 for (c
= ip
; *c
; ++c
) {
4223 if ((*c
>= '0' && *c
<= '9') ||
4224 (*c
>= 'a' && *c
<= 'f') ||
4225 (*c
>= 'A' && *c
<= 'F')) {
4227 /* Only four hex digits per chunk */
4230 } else if (*c
== ':') {
4231 /* The start of a new chunk */
4234 if (*(c
+ 1) == ':') {
4236 * '::' indicates a consecutive series of chunks full
4237 * of zeroes. There can be only one of these per address.
4241 double_colon
= TRUE
;
4248 * Either we saw a '::' and there were fewer than 8 chunks -or-
4249 * we didn't see a '::' and saw exactly 8 chunks.
4251 return (double_colon
&& chunks
< 8) || (!double_colon
&& chunks
== 8);
4255 purple_ip_address_is_valid(const char *ip
)
4257 return (purple_ipv4_address_is_valid(ip
) || purple_ipv6_address_is_valid(ip
));
4260 /* Stolen from gnome_uri_list_extract_uris */
4262 purple_uri_list_extract_uris(const gchar
*uri_list
)
4266 GList
*result
= NULL
;
4268 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
4272 /* We don't actually try to validate the URI according to RFC
4273 * 2396, or even check for allowed characters - we just ignore
4274 * comments and trim whitespace off the ends. We also
4275 * allow LF delimination as well as the specified CRLF.
4283 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
4288 while (q
> p
&& isspace(*q
))
4291 retval
= (gchar
*)g_malloc (q
- p
+ 2);
4292 strncpy (retval
, p
, q
- p
+ 1);
4293 retval
[q
- p
+ 1] = '\0';
4295 result
= g_list_prepend (result
, retval
);
4298 p
= strchr (p
, '\n');
4303 return g_list_reverse (result
);
4307 /* Stolen from gnome_uri_list_extract_filenames */
4309 purple_uri_list_extract_filenames(const gchar
*uri_list
)
4311 GList
*tmp_list
, *node
, *result
;
4313 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
4315 result
= purple_uri_list_extract_uris(uri_list
);
4319 gchar
*s
= (gchar
*)tmp_list
->data
;
4322 tmp_list
= tmp_list
->next
;
4324 if (!strncmp (s
, "file:", 5)) {
4325 node
->data
= g_filename_from_uri (s
, NULL
, NULL
);
4326 /* not sure if this fallback is useful at all */
4327 if (!node
->data
) node
->data
= g_strdup (s
+5);
4329 result
= g_list_delete_link(result
, node
);
4336 /**************************************************************************
4337 * UTF8 String Functions
4338 **************************************************************************/
4340 purple_utf8_try_convert(const char *str
)
4345 g_return_val_if_fail(str
!= NULL
, NULL
);
4347 if (g_utf8_validate(str
, -1, NULL
)) {
4348 return g_strdup(str
);
4351 utf8
= g_locale_to_utf8(str
, -1, &converted
, NULL
, NULL
);
4355 utf8
= g_convert(str
, -1, "UTF-8", "ISO-8859-15", &converted
, NULL
, NULL
);
4356 if ((utf8
!= NULL
) && (converted
== strlen(str
)))
4364 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
4365 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
4367 purple_utf8_salvage(const char *str
)
4372 g_return_val_if_fail(str
!= NULL
, NULL
);
4374 workstr
= g_string_sized_new(strlen(str
));
4377 (void)g_utf8_validate(str
, -1, &end
);
4378 workstr
= g_string_append_len(workstr
, str
, end
- str
);
4383 workstr
= g_string_append_c(workstr
, '?');
4385 } while (!utf8_first(*str
));
4386 } while (*str
!= '\0');
4388 return g_string_free(workstr
, FALSE
);
4392 purple_utf8_strip_unprintables(const gchar
*str
)
4394 gchar
*workstr
, *iter
;
4398 /* Act like g_strdup */
4401 if (!g_utf8_validate(str
, -1, &bad
)) {
4402 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
4403 "first bad character was %02x (%c)\n",
4405 g_return_val_if_reached(NULL
);
4408 workstr
= iter
= g_new(gchar
, strlen(str
) + 1);
4410 gunichar ch
= g_utf8_get_char(str
);
4411 gchar
*next
= g_utf8_next_char(str
);
4413 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
4414 * [#x10000-#x10FFFF]
4416 if ((ch
== '\t' || ch
== '\n' || ch
== '\r') ||
4417 (ch
>= 0x20 && ch
<= 0xD7FF) ||
4418 (ch
>= 0xE000 && ch
<= 0xFFFD) ||
4419 (ch
>= 0x10000 && ch
<= 0x10FFFF)) {
4420 memcpy(iter
, str
, next
- str
);
4421 iter
+= (next
- str
);
4427 /* nul-terminate the new string */
4434 * This function is copied from g_strerror() but changed to use
4438 purple_gai_strerror(gint errnum
)
4440 static GPrivate msg_private
= G_PRIVATE_INIT(g_free
);
4442 int saved_errno
= errno
;
4444 const char *msg_locale
;
4446 msg_locale
= gai_strerror(errnum
);
4447 if (g_get_charset(NULL
))
4449 /* This string is already UTF-8--great! */
4450 errno
= saved_errno
;
4455 gchar
*msg_utf8
= g_locale_to_utf8(msg_locale
, -1, NULL
, NULL
, NULL
);
4458 /* Stick in the quark table so that we can return a static result */
4459 GQuark msg_quark
= g_quark_from_string(msg_utf8
);
4462 msg_utf8
= (gchar
*)g_quark_to_string(msg_quark
);
4463 errno
= saved_errno
;
4468 msg
= g_private_get(&msg_private
);
4472 msg
= g_new(gchar
, 64);
4473 g_private_set(&msg_private
, msg
);
4476 sprintf(msg
, "unknown error (%d)", errnum
);
4478 errno
= saved_errno
;
4483 purple_utf8_ncr_encode(const char *str
)
4487 g_return_val_if_fail(str
!= NULL
, NULL
);
4488 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4490 out
= g_string_new("");
4492 for(; *str
; str
= g_utf8_next_char(str
)) {
4493 gunichar wc
= g_utf8_get_char(str
);
4495 /* super simple check. hopefully not too wrong. */
4497 g_string_append_printf(out
, "&#%u;", (guint32
) wc
);
4499 g_string_append_unichar(out
, wc
);
4503 return g_string_free(out
, FALSE
);
4508 purple_utf8_ncr_decode(const char *str
)
4513 g_return_val_if_fail(str
!= NULL
, NULL
);
4514 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4517 out
= g_string_new("");
4519 while( (b
= strstr(buf
, "&#")) ) {
4523 /* append everything leading up to the &# */
4524 g_string_append_len(out
, buf
, b
-buf
);
4526 b
+= 2; /* skip past the &# */
4528 /* strtoul will treat 0x prefix as hex, but not just x */
4529 if(*b
== 'x' || *b
== 'X') {
4534 /* advances buf to the end of the ncr segment */
4535 wc
= (gunichar
) strtoul(b
, &buf
, base
);
4537 /* this mimics the previous impl of ncr_decode */
4539 g_string_append_unichar(out
, wc
);
4544 /* append whatever's left */
4545 g_string_append(out
, buf
);
4547 return g_string_free(out
, FALSE
);
4552 purple_utf8_strcasecmp(const char *a
, const char *b
)
4554 char *a_norm
= NULL
;
4555 char *b_norm
= NULL
;
4565 if(!g_utf8_validate(a
, -1, NULL
) || !g_utf8_validate(b
, -1, NULL
))
4567 purple_debug_error("purple_utf8_strcasecmp",
4568 "One or both parameters are invalid UTF8\n");
4572 a_norm
= g_utf8_casefold(a
, -1);
4573 b_norm
= g_utf8_casefold(b
, -1);
4574 ret
= g_utf8_collate(a_norm
, b_norm
);
4581 /* previously conversation::find_nick() */
4583 purple_utf8_has_word(const char *haystack
, const char *needle
)
4585 char *hay
, *pin
, *p
;
4586 const char *start
, *prev_char
;
4587 gunichar before
, after
;
4589 gboolean ret
= FALSE
;
4591 start
= hay
= g_utf8_strdown(haystack
, -1);
4593 pin
= g_utf8_strdown(needle
, -1);
4596 while ((p
= strstr(start
, pin
)) != NULL
) {
4597 prev_char
= g_utf8_find_prev_char(hay
, p
);
4600 before
= g_utf8_get_char(prev_char
);
4602 after
= g_utf8_get_char_validated(p
+ n
, - 1);
4605 /* The character before is a reasonable guess for a word boundary
4606 ("!g_unichar_isalnum()" is not a valid way to determine word
4607 boundaries, but it is the only reasonable thing to do here),
4608 and isn't the '&' from a "&" or some such entity*/
4609 (before
!= (gunichar
)-2 && !g_unichar_isalnum(before
) && *(p
- 1) != '&'))
4610 && after
!= (gunichar
)-2 && !g_unichar_isalnum(after
)) {
4624 purple_print_utf8_to_console(FILE *filestream
, char *message
)
4626 gchar
*message_conv
;
4627 GError
*error
= NULL
;
4629 /* Try to convert 'message' to user's locale */
4630 message_conv
= g_locale_from_utf8(message
, -1, NULL
, NULL
, &error
);
4631 if (message_conv
!= NULL
) {
4632 fputs(message_conv
, filestream
);
4633 g_free(message_conv
);
4637 /* use 'message' as a fallback */
4638 g_warning("%s\n", error
->message
);
4639 g_error_free(error
);
4640 fputs(message
, filestream
);
4644 gboolean
purple_message_meify(char *message
, gssize len
)
4647 gboolean inside_html
= FALSE
;
4649 g_return_val_if_fail(message
!= NULL
, FALSE
);
4652 len
= strlen(message
);
4654 for (c
= message
; *c
; c
++, len
--) {
4657 inside_html
= FALSE
;
4666 if(*c
&& !g_ascii_strncasecmp(c
, "/me ", 4)) {
4667 memmove(c
, c
+4, len
-3);
4674 char *purple_text_strip_mnemonic(const char *in
)
4681 g_return_val_if_fail(in
!= NULL
, NULL
);
4683 out
= g_malloc(strlen(in
)+1);
4687 a0
= a
; /* The last non-space char seen so far, or the first char */
4691 if(a
> out
&& b
> in
&& *(b
-1) == '(' && *(b
+1) && !(*(b
+1) & 0x80) && *(b
+2) == ')') {
4692 /* Detected CJK style shortcut (Bug 875311) */
4693 a
= a0
; /* undo the left parenthesis */
4694 b
+= 3; /* and skip the whole mess */
4695 } else if(*(b
+1) == '_') {
4702 /* We don't want to corrupt the middle of UTF-8 characters */
4703 } else if (!(*b
& 0x80)) { /* other 1-byte char */
4708 /* Multibyte utf8 char, don't look for _ inside these */
4711 if ((*b
& 0xe0) == 0xc0) {
4713 } else if ((*b
& 0xf0) == 0xe0) {
4715 } else if ((*b
& 0xf8) == 0xf0) {
4717 } else if ((*b
& 0xfc) == 0xf8) {
4719 } else if ((*b
& 0xfe) == 0xfc) {
4721 } else { /* Illegal utf8 */
4724 a0
= a
; /* unless we want to delete CJK spaces too */
4725 for (i
= 0; i
< n
&& *b
; i
+= 1) {
4735 const char* purple_unescape_filename(const char *escaped
) {
4736 return purple_url_decode(escaped
);
4740 /* this is almost identical to purple_url_encode (hence purple_url_decode
4741 * being used above), but we want to keep certain characters unescaped
4742 * for compat reasons */
4744 purple_escape_filename(const char *str
)
4747 static char buf
[BUF_LEN
];
4751 g_return_val_if_fail(str
!= NULL
, NULL
);
4752 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4755 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
4756 gunichar c
= g_utf8_get_char(iter
);
4757 /* If the character is an ASCII character and is alphanumeric,
4758 * or one of the specified values, no need to escape */
4759 if (c
< 128 && (g_ascii_isalnum(c
) || c
== '@' || c
== '-' ||
4760 c
== '_' || c
== '.' || c
== '#')) {
4763 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4764 for (i
= 0; (int)i
< bytes
; i
++) {
4765 if (j
> (BUF_LEN
- 4))
4767 if (i
>= sizeof(utf_char
)) {
4768 g_warn_if_reached();
4771 sprintf(buf
+ j
, "%%%02x", utf_char
[i
] & 0xff);
4777 /* File/Directory names in windows cannot end in periods/spaces.
4778 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4780 while (j
> 0 && (buf
[j
- 1] == '.' || buf
[j
- 1] == ' '))
4788 gchar
* purple_escape_js(const gchar
*str
)
4792 json_node_set_string(escape_js_node
, str
);
4793 json_generator_set_root(escape_js_gen
, escape_js_node
);
4794 escaped
= json_generator_to_data(escape_js_gen
, NULL
);
4795 json_node_set_boolean(escape_js_node
, FALSE
);
4800 void purple_restore_default_signal_handlers(void)
4803 signal(SIGHUP
, SIG_DFL
); /* 1: terminal line hangup */
4804 signal(SIGINT
, SIG_DFL
); /* 2: interrupt program */
4805 signal(SIGQUIT
, SIG_DFL
); /* 3: quit program */
4806 signal(SIGILL
, SIG_DFL
); /* 4: illegal instruction (not reset when caught) */
4807 signal(SIGTRAP
, SIG_DFL
); /* 5: trace trap (not reset when caught) */
4808 signal(SIGABRT
, SIG_DFL
); /* 6: abort program */
4811 signal(SIGPOLL
, SIG_DFL
); /* 7: pollable event (POSIX) */
4812 #endif /* SIGPOLL */
4815 signal(SIGEMT
, SIG_DFL
); /* 7: EMT instruction (Non-POSIX) */
4818 signal(SIGFPE
, SIG_DFL
); /* 8: floating point exception */
4819 signal(SIGBUS
, SIG_DFL
); /* 10: bus error */
4820 signal(SIGSEGV
, SIG_DFL
); /* 11: segmentation violation */
4821 signal(SIGSYS
, SIG_DFL
); /* 12: bad argument to system call */
4822 signal(SIGPIPE
, SIG_DFL
); /* 13: write on a pipe with no reader */
4823 signal(SIGALRM
, SIG_DFL
); /* 14: real-time timer expired */
4824 signal(SIGTERM
, SIG_DFL
); /* 15: software termination signal */
4825 signal(SIGCHLD
, SIG_DFL
); /* 20: child status has changed */
4826 signal(SIGXCPU
, SIG_DFL
); /* 24: exceeded CPU time limit */
4827 signal(SIGXFSZ
, SIG_DFL
); /* 25: exceeded file size limit */
4828 #endif /* !_WIN32 */
4832 set_status_with_attrs(PurpleStatus
*status
, ...)
4835 va_start(args
, status
);
4836 purple_status_set_active_with_attrs(status
, TRUE
, args
);
4840 void purple_util_set_current_song(const char *title
, const char *artist
, const char *album
)
4842 GList
*list
= purple_accounts_get_all();
4843 for (; list
; list
= list
->next
) {
4844 PurplePresence
*presence
;
4846 PurpleAccount
*account
= list
->data
;
4847 if (!purple_account_get_enabled(account
, purple_core_get_ui()))
4850 presence
= purple_account_get_presence(account
);
4851 tune
= purple_presence_get_status(presence
, "tune");
4855 set_status_with_attrs(tune
,
4856 PURPLE_TUNE_TITLE
, title
,
4857 PURPLE_TUNE_ARTIST
, artist
,
4858 PURPLE_TUNE_ALBUM
, album
,
4861 purple_status_set_active(tune
, FALSE
);
4866 char * purple_util_format_song_info(const char *title
, const char *artist
, const char *album
, gpointer unused
)
4871 if (!title
|| !*title
)
4874 esc
= g_markup_escape_text(title
, -1);
4875 string
= g_string_new("");
4876 g_string_append_printf(string
, "%s", esc
);
4879 if (artist
&& *artist
) {
4880 esc
= g_markup_escape_text(artist
, -1);
4881 g_string_append_printf(string
, _(" - %s"), esc
);
4885 if (album
&& *album
) {
4886 esc
= g_markup_escape_text(album
, -1);
4887 g_string_append_printf(string
, _(" (%s)"), esc
);
4891 return g_string_free(string
, FALSE
);
4895 purple_get_host_name(void)
4897 return g_get_host_name();
4901 purple_uuid_random(void)
4905 tmp
= g_random_int();
4906 a
= 0x4000 | (tmp
& 0xFFF); /* 0x4000 to 0x4FFF */
4908 b
= ((1 << 3) << 12) | (tmp
& 0x3FFF); /* 0x8000 to 0xBFFF */
4910 tmp
= g_random_int();
4912 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4917 (tmp
>> 16) & 0xFFFF, g_random_int());
4920 void purple_callback_set_zero(gpointer data
)
4922 gpointer
*ptr
= data
;
4924 g_return_if_fail(ptr
!= NULL
);
4930 purple_value_new(GType type
)
4934 g_return_val_if_fail(type
!= G_TYPE_NONE
, NULL
);
4936 ret
= g_new0(GValue
, 1);
4937 g_value_init(ret
, type
);
4943 purple_value_dup(GValue
*value
)
4947 g_return_val_if_fail(value
!= NULL
, NULL
);
4949 ret
= g_new0(GValue
, 1);
4950 g_value_init(ret
, G_VALUE_TYPE(value
));
4951 g_value_copy(value
, ret
);
4957 purple_value_free(GValue
*value
)
4959 g_return_if_fail(value
!= NULL
);
4961 g_value_unset(value
);
4965 gchar
*purple_http_digest_calculate_session_key(
4966 const gchar
*algorithm
,
4967 const gchar
*username
,
4969 const gchar
*password
,
4971 const gchar
*client_nonce
)
4974 gchar hash
[33]; /* We only support MD5. */
4977 g_return_val_if_fail(username
!= NULL
, NULL
);
4978 g_return_val_if_fail(realm
!= NULL
, NULL
);
4979 g_return_val_if_fail(password
!= NULL
, NULL
);
4980 g_return_val_if_fail(nonce
!= NULL
, NULL
);
4982 /* Check for a supported algorithm. */
4983 g_return_val_if_fail(algorithm
== NULL
||
4984 *algorithm
== '\0' ||
4985 g_ascii_strcasecmp(algorithm
, "MD5") ||
4986 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
4988 hasher
= purple_md5_hash_new();
4989 g_return_val_if_fail(hash
!= NULL
, NULL
);
4991 purple_hash_append(hasher
, (guchar
*)username
, strlen(username
));
4992 purple_hash_append(hasher
, (guchar
*)":", 1);
4993 purple_hash_append(hasher
, (guchar
*)realm
, strlen(realm
));
4994 purple_hash_append(hasher
, (guchar
*)":", 1);
4995 purple_hash_append(hasher
, (guchar
*)password
, strlen(password
));
4997 if (algorithm
!= NULL
&& !g_ascii_strcasecmp(algorithm
, "MD5-sess"))
5001 if (client_nonce
== NULL
)
5003 g_object_unref(hasher
);
5004 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
5008 purple_hash_digest(hasher
, digest
, sizeof(digest
));
5010 purple_hash_reset(hasher
);
5011 purple_hash_append(hasher
, digest
, sizeof(digest
));
5012 purple_hash_append(hasher
, (guchar
*)":", 1);
5013 purple_hash_append(hasher
, (guchar
*)nonce
, strlen(nonce
));
5014 purple_hash_append(hasher
, (guchar
*)":", 1);
5015 purple_hash_append(hasher
, (guchar
*)client_nonce
, strlen(client_nonce
));
5018 digest_ok
= purple_hash_digest_to_str(hasher
, hash
, sizeof(hash
));
5019 g_object_unref(hasher
);
5021 g_return_val_if_fail(digest_ok
, NULL
);
5023 return g_strdup(hash
);
5026 gchar
*purple_http_digest_calculate_response(
5027 const gchar
*algorithm
,
5028 const gchar
*method
,
5029 const gchar
*digest_uri
,
5031 const gchar
*entity
,
5033 const gchar
*nonce_count
,
5034 const gchar
*client_nonce
,
5035 const gchar
*session_key
)
5038 static gchar hash2
[33]; /* We only support MD5. */
5041 g_return_val_if_fail(method
!= NULL
, NULL
);
5042 g_return_val_if_fail(digest_uri
!= NULL
, NULL
);
5043 g_return_val_if_fail(nonce
!= NULL
, NULL
);
5044 g_return_val_if_fail(session_key
!= NULL
, NULL
);
5046 /* Check for a supported algorithm. */
5047 g_return_val_if_fail(algorithm
== NULL
||
5048 *algorithm
== '\0' ||
5049 g_ascii_strcasecmp(algorithm
, "MD5") ||
5050 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
5052 /* Check for a supported "quality of protection". */
5053 g_return_val_if_fail(qop
== NULL
||
5055 g_ascii_strcasecmp(qop
, "auth") ||
5056 g_ascii_strcasecmp(qop
, "auth-int"), NULL
);
5058 hash
= purple_md5_hash_new();
5059 g_return_val_if_fail(hash
!= NULL
, NULL
);
5061 purple_hash_append(hash
, (guchar
*)method
, strlen(method
));
5062 purple_hash_append(hash
, (guchar
*)":", 1);
5063 purple_hash_append(hash
, (guchar
*)digest_uri
, strlen(digest_uri
));
5065 if (qop
!= NULL
&& !g_ascii_strcasecmp(qop
, "auth-int"))
5068 gchar entity_hash
[33];
5072 g_object_unref(hash
);
5073 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
5077 hash2
= purple_md5_hash_new();
5078 purple_hash_append(hash2
, (guchar
*)entity
, strlen(entity
));
5079 digest_ok
= purple_hash_digest_to_str(hash2
, entity_hash
, sizeof(entity_hash
));
5080 g_object_unref(hash2
);
5083 g_object_unref(hash
);
5084 g_return_val_if_reached(NULL
);
5087 purple_hash_append(hash
, (guchar
*)":", 1);
5088 purple_hash_append(hash
, (guchar
*)entity_hash
, strlen(entity_hash
));
5091 digest_ok
= purple_hash_digest_to_str(hash
, hash2
, sizeof(hash2
));
5092 purple_hash_reset(hash
);
5095 g_object_unref(hash
);
5096 g_return_val_if_reached(NULL
);
5099 purple_hash_append(hash
, (guchar
*)session_key
, strlen(session_key
));
5100 purple_hash_append(hash
, (guchar
*)":", 1);
5101 purple_hash_append(hash
, (guchar
*)nonce
, strlen(nonce
));
5102 purple_hash_append(hash
, (guchar
*)":", 1);
5104 if (qop
!= NULL
&& *qop
!= '\0')
5106 if (nonce_count
== NULL
)
5108 g_object_unref(hash
);
5109 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
5113 if (client_nonce
== NULL
)
5115 g_object_unref(hash
);
5116 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
5120 purple_hash_append(hash
, (guchar
*)nonce_count
, strlen(nonce_count
));
5121 purple_hash_append(hash
, (guchar
*)":", 1);
5122 purple_hash_append(hash
, (guchar
*)client_nonce
, strlen(client_nonce
));
5123 purple_hash_append(hash
, (guchar
*)":", 1);
5125 purple_hash_append(hash
, (guchar
*)qop
, strlen(qop
));
5127 purple_hash_append(hash
, (guchar
*)":", 1);
5130 purple_hash_append(hash
, (guchar
*)hash2
, strlen(hash2
));
5131 digest_ok
= purple_hash_digest_to_str(hash
, hash2
, sizeof(hash2
));
5132 g_object_unref(hash
);
5134 g_return_val_if_fail(digest_ok
, NULL
);
5136 return g_strdup(hash2
);
5140 _purple_fstat(int fd
, GStatBuf
*st
)
5144 g_return_val_if_fail(st
!= NULL
, -1);
5147 ret
= _fstat(fd
, st
);
5149 ret
= fstat(fd
, st
);
5157 /* Temporarily removed - re-add this when you need ini file support. */
5159 #define PURPLE_KEY_FILE_DEFAULT_MAX_SIZE 102400
5160 #define PURPLE_KEY_FILE_HARD_LIMIT 10485760
5163 purple_key_file_load_from_ini(GKeyFile
*key_file
, const gchar
*file
,
5166 const gchar
*header
= "[default]\n\n";
5167 int header_len
= strlen(header
);
5170 gsize file_size
, buff_size
;
5172 GError
*error
= NULL
;
5174 g_return_val_if_fail(key_file
!= NULL
, FALSE
);
5175 g_return_val_if_fail(file
!= NULL
, FALSE
);
5176 g_return_val_if_fail(max_size
< PURPLE_KEY_FILE_HARD_LIMIT
, FALSE
);
5179 max_size
= PURPLE_KEY_FILE_DEFAULT_MAX_SIZE
;
5181 fd
= g_open(file
, O_RDONLY
, S_IREAD
);
5183 purple_debug_error("util", "Failed to read ini file %s", file
);
5187 if (_purple_fstat(fd
, &st
) != 0) {
5188 purple_debug_error("util", "Failed to fstat ini file %s", file
);
5192 file_size
= (st
.st_size
> max_size
) ? max_size
: st
.st_size
;
5194 buff_size
= file_size
+ header_len
;
5195 buff
= g_new(gchar
, buff_size
);
5196 memcpy(buff
, header
, header_len
);
5197 if (read(fd
, buff
+ header_len
, file_size
) != (gssize
)file_size
) {
5198 purple_debug_error("util",
5199 "Failed to read whole ini file %s", file
);
5206 g_key_file_load_from_data(key_file
, buff
, buff_size
,
5207 G_KEY_FILE_NONE
, &error
);
5212 purple_debug_error("util", "Failed parsing ini file %s: %s",
5213 file
, error
->message
);