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 char *cache_dir
= NULL
;
45 static char *config_dir
= NULL
;
46 static char *data_dir
= NULL
;
48 static JsonNode
*escape_js_node
= NULL
;
49 static JsonGenerator
*escape_js_gen
= NULL
;
52 move_to_xdg_base_dir(const char *purple_xdg_dir
, char *subdir
)
55 gboolean xdg_dir_exists
;
57 xdg_dir_exists
= g_file_test(purple_xdg_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
58 if (!xdg_dir_exists
) {
61 mkdir_res
= g_mkdir_with_parents(purple_xdg_dir
, (S_IRUSR
| S_IWUSR
| S_IXUSR
));
62 if (mkdir_res
== -1) {
63 purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n",
64 purple_xdg_dir
, g_strerror(errno
));
69 xdg_dir
= g_build_filename(purple_xdg_dir
, subdir
, NULL
);
70 xdg_dir_exists
= g_file_test(xdg_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
71 if (!xdg_dir_exists
) {
73 gboolean old_dir_exists
;
75 old_dir
= g_build_filename(purple_user_dir(), subdir
, NULL
);
76 old_dir_exists
= g_file_test(old_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
79 g_rename(old_dir
, xdg_dir
);
92 /* If legacy directory for libpurple exists, move it to location following
93 * xdg base dir spec. https://developer.pidgin.im/ticket/10029
96 migrate_to_xdg_base_dirs(void)
98 const char *legacy_purple_dir
;
101 legacy_purple_dir
= purple_user_dir();
102 dir_exists
= g_file_test(legacy_purple_dir
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
);
104 move_to_xdg_base_dir(purple_data_dir(), "certificates");
105 move_to_xdg_base_dir(purple_cache_dir(), "icons");
106 move_to_xdg_base_dir(purple_data_dir(), "logs");
113 purple_menu_action_new(const char *label
, PurpleCallback callback
, gpointer data
,
116 PurpleMenuAction
*act
= g_new0(PurpleMenuAction
, 1);
117 act
->label
= g_strdup(label
);
118 act
->callback
= callback
;
120 act
->children
= children
;
125 purple_menu_action_free(PurpleMenuAction
*act
)
127 g_return_if_fail(act
!= NULL
);
129 g_free(act
->stock_icon
);
134 char * purple_menu_action_get_label(const PurpleMenuAction
*act
)
136 g_return_val_if_fail(act
!= NULL
, NULL
);
141 PurpleCallback
purple_menu_action_get_callback(const PurpleMenuAction
*act
)
143 g_return_val_if_fail(act
!= NULL
, NULL
);
145 return act
->callback
;
148 gpointer
purple_menu_action_get_data(const PurpleMenuAction
*act
)
150 g_return_val_if_fail(act
!= NULL
, NULL
);
155 GList
* purple_menu_action_get_children(const PurpleMenuAction
*act
)
157 g_return_val_if_fail(act
!= NULL
, NULL
);
159 return act
->children
;
162 void purple_menu_action_set_label(PurpleMenuAction
*act
, char *label
)
164 g_return_if_fail(act
!= NULL
);
169 void purple_menu_action_set_callback(PurpleMenuAction
*act
, PurpleCallback callback
)
171 g_return_if_fail(act
!= NULL
);
173 act
->callback
= callback
;
176 void purple_menu_action_set_data(PurpleMenuAction
*act
, gpointer data
)
178 g_return_if_fail(act
!= NULL
);
183 void purple_menu_action_set_children(PurpleMenuAction
*act
, GList
*children
)
185 g_return_if_fail(act
!= NULL
);
187 act
->children
= children
;
190 void purple_menu_action_set_stock_icon(PurpleMenuAction
*act
,
193 g_return_if_fail(act
!= NULL
);
195 g_free(act
->stock_icon
);
196 act
->stock_icon
= g_strdup(stock
);
200 purple_menu_action_get_stock_icon(PurpleMenuAction
*act
)
202 return act
->stock_icon
;
206 purple_util_init(void)
208 escape_js_node
= json_node_new(JSON_NODE_VALUE
);
209 escape_js_gen
= json_generator_new();
210 json_node_set_boolean(escape_js_node
, FALSE
);
212 if (custom_user_dir
== NULL
) {
213 migrate_to_xdg_base_dirs();
218 purple_util_uninit(void)
220 /* Free these so we don't have leaks at shutdown. */
222 g_free(custom_user_dir
);
223 custom_user_dir
= NULL
;
237 json_node_free(escape_js_node
);
238 escape_js_node
= NULL
;
240 g_object_unref(escape_js_gen
);
241 escape_js_gen
= NULL
;
244 /**************************************************************************
246 **************************************************************************/
248 purple_base16_encode(const guchar
*data
, gsize len
)
253 g_return_val_if_fail(data
!= NULL
, NULL
);
254 g_return_val_if_fail(len
> 0, NULL
);
256 ascii
= g_malloc(len
* 2 + 1);
258 for (i
= 0; i
< len
; i
++)
259 g_snprintf(&ascii
[i
* 2], 3, "%02x", data
[i
] & 0xFF);
265 purple_base16_decode(const char *str
, gsize
*ret_len
)
267 gsize len
, i
, accumulator
= 0;
270 g_return_val_if_fail(str
!= NULL
, NULL
);
274 g_return_val_if_fail(*str
, 0);
275 g_return_val_if_fail(len
% 2 == 0, 0);
277 data
= g_malloc(len
/ 2);
279 for (i
= 0; i
< len
; i
++)
287 accumulator
|= str
[i
] - 48;
290 switch(tolower(str
[i
]))
292 case 'a': accumulator
|= 10; break;
293 case 'b': accumulator
|= 11; break;
294 case 'c': accumulator
|= 12; break;
295 case 'd': accumulator
|= 13; break;
296 case 'e': accumulator
|= 14; break;
297 case 'f': accumulator
|= 15; break;
302 data
[(i
- 1) / 2] = accumulator
;
312 purple_base16_encode_chunked(const guchar
*data
, gsize len
)
317 g_return_val_if_fail(data
!= NULL
, NULL
);
318 g_return_val_if_fail(len
> 0, NULL
);
320 /* For each byte of input, we need 2 bytes for the hex representation
321 * and 1 for the colon.
322 * The final colon will be replaced by a terminating NULL
324 ascii
= g_malloc(len
* 3 + 1);
326 for (i
= 0; i
< len
; i
++)
327 g_snprintf(&ascii
[i
* 3], 4, "%02x:", data
[i
] & 0xFF);
329 /* Replace the final colon with NULL */
330 ascii
[len
* 3 - 1] = 0;
336 /**************************************************************************
338 **************************************************************************/
339 static const char xdigits
[] =
343 purple_base64_encode(const guchar
*data
, gsize len
)
345 return g_base64_encode(data
, len
);
349 purple_base64_decode(const char *str
, gsize
*ret_len
)
352 * We want to allow ret_len to be NULL for backward compatibility,
353 * but g_base64_decode() requires a valid length variable. So if
354 * ret_len is NULL then pass in a dummy variable.
357 return g_base64_decode(str
, ret_len
!= NULL
? ret_len
: &unused
);
360 /**************************************************************************
361 * Quoted Printable Functions (see RFC 2045).
362 **************************************************************************/
364 purple_quotedp_decode(const char *str
, gsize
*ret_len
)
369 n
= new = g_malloc(strlen (str
) + 1);
370 end
= str
+ strlen(str
);
372 for (p
= str
; p
< end
; p
++, n
++) {
374 if (p
[1] == '\r' && p
[2] == '\n') { /* 5.1 #5 */
377 } else if (p
[1] == '\n') { /* fuzzy case for 5.1 #5 */
380 } else if (p
[1] && p
[2]) {
381 char *nibble1
= strchr(xdigits
, tolower(p
[1]));
382 char *nibble2
= strchr(xdigits
, tolower(p
[2]));
383 if (nibble1
&& nibble2
) { /* 5.1 #1 */
384 *n
= ((nibble1
- xdigits
) << 4) | (nibble2
- xdigits
);
386 } else { /* This should never happen */
389 } else { /* This should never happen */
404 /* Resize to take less space */
405 /* new = realloc(new, n - new); */
407 return (guchar
*)new;
410 /**************************************************************************
412 **************************************************************************/
414 purple_mime_decode_field(const char *str
)
417 * This is wing's version, partially based on revo/shx's version
418 * See RFC2047 [which apparently obsoletes RFC1342]
421 state_start
, state_equal1
, state_question1
,
422 state_charset
, state_question2
,
423 state_encoding
, state_question3
,
424 state_encoded_text
, state_question4
, state_equal2
= state_start
425 } encoded_word_state_t
;
426 encoded_word_state_t state
= state_start
;
427 const char *cur
, *mark
;
428 const char *charset0
= NULL
, *encoding0
= NULL
, *encoded_text0
= NULL
;
431 /* token can be any CHAR (supposedly ISO8859-1/ISO2022), not just ASCII */
432 #define token_char_p(c) \
433 (c != ' ' && !iscntrl(c) && !strchr("()<>@,;:\"/[]?.=", c))
435 /* But encoded-text must be ASCII; alas, isascii() may not exist */
436 #define encoded_text_char_p(c) \
437 ((c & 0x80) == 0 && c != '?' && c != ' ' && isgraph(c))
439 g_return_val_if_fail(str
!= NULL
, NULL
);
441 new = g_string_new(NULL
);
443 /* Here we will be looking for encoded words and if they seem to be
444 * valid then decode them.
445 * They are of this form: =?charset?encoding?text?=
448 for (cur
= str
, mark
= NULL
; *cur
; cur
+= 1) {
452 state
= state_question1
;
454 g_string_append_len(new, mark
, cur
- mark
+ 1);
458 case state_question1
:
459 if (token_char_p(*cur
)) {
461 state
= state_charset
;
462 } else { /* This should never happen */
463 g_string_append_len(new, mark
, cur
- mark
+ 1);
469 state
= state_question2
;
470 } else if (!token_char_p(*cur
)) { /* This should never happen */
471 g_string_append_len(new, mark
, cur
- mark
+ 1);
475 case state_question2
:
476 if (token_char_p(*cur
)) {
478 state
= state_encoding
;
479 } else { /* This should never happen */
480 g_string_append_len(new, mark
, cur
- mark
+ 1);
486 state
= state_question3
;
487 } else if (!token_char_p(*cur
)) { /* This should never happen */
488 g_string_append_len(new, mark
, cur
- mark
+ 1);
492 case state_question3
:
493 if (encoded_text_char_p(*cur
)) {
495 state
= state_encoded_text
;
496 } else if (*cur
== '?') { /* empty string */
498 state
= state_question4
;
499 } else { /* This should never happen */
500 g_string_append_len(new, mark
, cur
- mark
+ 1);
504 case state_encoded_text
:
506 state
= state_question4
;
507 } else if (!encoded_text_char_p(*cur
)) {
508 g_string_append_len(new, mark
, cur
- mark
+ 1);
512 case state_question4
:
513 if (*cur
== '=') { /* Got the whole encoded-word */
514 char *charset
= g_strndup(charset0
, encoding0
- charset0
- 1);
515 char *encoding
= g_strndup(encoding0
, encoded_text0
- encoding0
- 1);
516 char *encoded_text
= g_strndup(encoded_text0
, cur
- encoded_text0
- 1);
517 guchar
*decoded
= NULL
;
519 if (g_ascii_strcasecmp(encoding
, "Q") == 0)
520 decoded
= purple_quotedp_decode(encoded_text
, &dec_len
);
521 else if (g_ascii_strcasecmp(encoding
, "B") == 0)
522 decoded
= purple_base64_decode(encoded_text
, &dec_len
);
527 char *converted
= g_convert((const gchar
*)decoded
, dec_len
, "utf-8", charset
, NULL
, &len
, NULL
);
530 g_string_append_len(new, converted
, len
);
537 g_free(encoded_text
);
538 state
= state_equal2
; /* Restart the FSM */
539 } else { /* This should never happen */
540 g_string_append_len(new, mark
, cur
- mark
+ 1);
547 state
= state_equal1
;
549 /* Some unencoded text. */
550 g_string_append_c(new, *cur
);
556 if (state
!= state_start
)
557 g_string_append_len(new, mark
, cur
- mark
+ 1);
559 return g_string_free(new, FALSE
);;
563 /**************************************************************************
564 * Date/Time Functions
565 **************************************************************************/
567 const char *purple_get_tzoff_str(const struct tm
*tm
, gboolean iso
)
573 struct tm new_tm
= *tm
;
577 if (new_tm
.tm_isdst
< 0)
578 g_return_val_if_reached("");
581 if ((off
= wpurple_get_tz_offset()) == -1)
583 #elif defined(HAVE_TM_GMTOFF)
584 off
= new_tm
.tm_gmtoff
;
585 #elif defined(HAVE_TIMEZONE)
589 purple_debug_warning("util",
590 "there is no possibility to obtain tz offset");
594 min
= (off
/ 60) % 60;
595 hrs
= ((off
/ 60) - min
) / 60;
601 /* please leave the colons...they're optional for iso, but jabber
603 if(g_snprintf(buf
, sizeof(buf
), "%+03d:%02d", hrs
, ABS(min
)) > 6)
604 g_return_val_if_reached("");
607 if (g_snprintf(buf
, sizeof(buf
), "%+03d%02d", hrs
, ABS(min
)) > 5)
608 g_return_val_if_reached("");
614 /* Windows doesn't HAVE_STRFTIME_Z_FORMAT, but this seems clearer. -- rlaager */
615 #if !defined(HAVE_STRFTIME_Z_FORMAT) || defined(_WIN32)
616 static size_t purple_internal_strftime(char *s
, size_t max
, const char *format
, const struct tm
*tm
)
622 /* Yes, this is checked in purple_utf8_strftime(),
623 * but better safe than sorry. -- rlaager */
624 g_return_val_if_fail(format
!= NULL
, 0);
626 /* This is fairly efficient, and it only gets
627 * executed on Windows or if the underlying
628 * system doesn't support the %z format string,
629 * for strftime() so I think it's good enough.
631 for (c
= start
= format
; *c
; c
++)
638 #ifndef HAVE_STRFTIME_Z_FORMAT
641 char *tmp
= g_strdup_printf("%s%.*s%s",
643 (int)(c
- start
- 1),
645 purple_get_tzoff_str(tm
, FALSE
));
654 char *tmp
= g_strdup_printf("%s%.*s%s",
656 (int)(c
- start
- 1),
658 wpurple_get_timezone_abbreviation(tm
));
672 char *tmp
= g_strconcat(fmt
, start
, NULL
);
677 ret
= strftime(s
, max
, fmt
, tm
);
683 return strftime(s
, max
, format
, tm
);
685 #else /* HAVE_STRFTIME_Z_FORMAT && !_WIN32 */
686 #define purple_internal_strftime strftime
690 purple_utf8_strftime(const char *format
, const struct tm
*tm
)
692 static char buf
[128];
698 g_return_val_if_fail(format
!= NULL
, NULL
);
702 time_t now
= time(NULL
);
703 tm
= localtime(&now
);
706 locale
= g_locale_from_utf8(format
, -1, NULL
, NULL
, &err
);
709 purple_debug_error("util", "Format conversion failed in purple_utf8_strftime(): %s\n", err
->message
);
712 locale
= g_strdup(format
);
715 /* A return value of 0 is either an error (in
716 * which case, the contents of the buffer are
717 * undefined) or the empty string (in which
718 * case, no harm is done here). */
719 if ((len
= purple_internal_strftime(buf
, sizeof(buf
), locale
, tm
)) == 0)
727 utf8
= g_locale_to_utf8(buf
, len
, NULL
, NULL
, &err
);
730 purple_debug_error("util", "Result conversion failed in purple_utf8_strftime(): %s\n", err
->message
);
735 g_strlcpy(buf
, utf8
, sizeof(buf
));
743 purple_date_format_short(const struct tm
*tm
)
745 return purple_utf8_strftime("%x", tm
);
749 purple_date_format_long(const struct tm
*tm
)
752 * This string determines how some dates are displayed. The default
753 * string "%x %X" shows the date then the time. Translators can
754 * change this to "%X %x" if they want the time to be shown first,
755 * followed by the date.
757 return purple_utf8_strftime(_("%x %X"), tm
);
761 purple_date_format_full(const struct tm
*tm
)
763 return purple_utf8_strftime("%c", tm
);
767 purple_time_format(const struct tm
*tm
)
769 return purple_utf8_strftime("%X", tm
);
773 purple_time_build(int year
, int month
, int day
, int hour
, int min
, int sec
)
777 tm
.tm_year
= year
- 1900;
778 tm
.tm_mon
= month
- 1;
782 tm
.tm_sec
= sec
>= 0 ? sec
: time(NULL
) % 60;
787 /* originally taken from GLib trunk 1-6-11 */
788 /* originally licensed as LGPL 2+ */
790 mktime_utc(struct tm
*tm
)
795 static const gint days_before
[] =
797 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
802 if (tm
->tm_mon
< 0 || tm
->tm_mon
> 11)
805 retval
= (tm
->tm_year
- 70) * 365;
806 retval
+= (tm
->tm_year
- 68) / 4;
807 retval
+= days_before
[tm
->tm_mon
] + tm
->tm_mday
- 1;
809 if (tm
->tm_year
% 4 == 0 && tm
->tm_mon
< 2)
812 retval
= ((((retval
* 24) + tm
->tm_hour
) * 60) + tm
->tm_min
) * 60 + tm
->tm_sec
;
814 retval
= timegm (tm
);
815 #endif /* !HAVE_TIMEGM */
821 purple_str_to_time(const char *timestamp
, gboolean utc
,
822 struct tm
*tm
, long *tz_off
, const char **rest
)
827 long tzoff
= PURPLE_NO_TZ_OFF
;
829 gboolean mktime_with_utc
= FALSE
;
834 g_return_val_if_fail(timestamp
!= NULL
, 0);
836 memset(&t
, 0, sizeof(struct tm
));
840 /* Strip leading whitespace */
841 while (g_ascii_isspace(*str
))
845 if (rest
!= NULL
&& *str
!= '\0')
851 if (!g_ascii_isdigit(*str
) && *str
!= '-' && *str
!= '+') {
852 if (rest
!= NULL
&& *str
!= '\0')
859 if (sscanf(str
, "%04d", &year
) && year
>= 1900) {
862 if (*str
== '-' || *str
== '/')
865 t
.tm_year
= year
- 1900;
869 if (!sscanf(str
, "%02d", &t
.tm_mon
)) {
870 if (rest
!= NULL
&& *str
!= '\0')
879 if (*str
== '-' || *str
== '/')
883 if (!sscanf(str
, "%02d", &t
.tm_mday
)) {
884 if (rest
!= NULL
&& *str
!= '\0')
892 /* Grab the year off the end if there's still stuff */
893 if (*str
== '/' || *str
== '-') {
894 /* But make sure we don't read the year twice */
896 if (rest
!= NULL
&& *str
!= '\0')
904 if (!sscanf(str
, "%04d", &t
.tm_year
)) {
905 if (rest
!= NULL
&& *str
!= '\0')
912 } else if (*str
== 'T' || *str
== '.') {
915 /* Continue grabbing the hours/minutes/seconds */
916 if ((sscanf(str
, "%02d:%02d:%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
918 (sscanf(str
, "%02d%02d%02d", &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
) == 3 &&
921 gint sign
, tzhrs
, tzmins
;
924 /* Cut off those pesky micro-seconds */
927 } while (*str
>= '0' && *str
<= '9');
930 sign
= (*str
== '+') ? 1 : -1;
932 /* Process the timezone */
933 if (*str
== '+' || *str
== '-') {
936 if (((sscanf(str
, "%02d:%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 5)) ||
937 (sscanf(str
, "%02d%02d", &tzhrs
, &tzmins
) == 2 && (str
+= 4))))
939 mktime_with_utc
= TRUE
;
940 tzoff
= tzhrs
* 60 * 60 + tzmins
* 60;
943 } else if (*str
== 'Z') {
944 /* 'Z' = Zulu = UTC */
946 mktime_with_utc
= TRUE
;
950 if (!mktime_with_utc
)
952 /* No timezone specified. */
955 mktime_with_utc
= TRUE
;
965 if (rest
!= NULL
&& *str
!= '\0') {
966 /* Strip trailing whitespace */
967 while (g_ascii_isspace(*str
))
975 retval
= mktime_utc(&t
);
982 if (tzoff
!= PURPLE_NO_TZ_OFF
)
992 purple_uts35_to_str(const char *format
, size_t len
, struct tm
*tm
)
998 time_t now
= time(NULL
);
999 tm
= localtime(&now
);
1002 string
= g_string_sized_new(len
);
1006 while ((i
+ count
) < len
&& format
[i
] == format
[i
+count
])
1009 switch (format
[i
]) {
1010 /* Era Designator */
1014 } else if (count
== 4) {
1016 } else if (count
>= 5) {
1026 /* Two-digits only */
1027 g_string_append(string
, purple_utf8_strftime("%y", tm
));
1030 char *tmp
= g_strdup_printf("%%0%dY", count
);
1031 g_string_append(string
, purple_utf8_strftime(tmp
, tm
));
1036 /* Year (in "Week of Year" based calendars) */
1039 /* Two-digits only */
1049 /* Cyclic Year Name */
1053 } else if (count
== 4) {
1055 } else if (count
>= 5) {
1066 } else if (count
== 3) {
1068 } else if (count
>= 4) {
1074 /* Stand-alone Quarter */
1078 } else if (count
== 3) {
1080 } else if (count
>= 4) {
1090 g_string_append(string
, purple_utf8_strftime("%m", tm
));
1091 } else if (count
== 3) {
1093 g_string_append(string
, purple_utf8_strftime("%b", tm
));
1094 } else if (count
== 4) {
1096 g_string_append(string
, purple_utf8_strftime("%B", tm
));
1097 } else if (count
>= 5) {
1098 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
1103 /* Stand-alone Month */
1107 g_string_append(string
, purple_utf8_strftime("%m", tm
));
1108 } else if (count
== 3) {
1110 g_string_append(string
, purple_utf8_strftime("%b", tm
));
1111 } else if (count
== 4) {
1113 g_string_append(string
, purple_utf8_strftime("%B", tm
));
1114 } else if (count
>= 5) {
1115 g_string_append_len(string
, purple_utf8_strftime("%b", tm
), 1);
1127 g_string_append(string
, purple_utf8_strftime("%W", tm
));
1128 count
= MIN(count
, 2);
1139 g_string_append(string
, purple_utf8_strftime("%d", tm
));
1140 count
= MIN(count
, 2);
1145 g_string_append(string
, purple_utf8_strftime("%j", tm
));
1146 count
= MIN(count
, 3);
1149 /* Day of Year in Month */
1154 /* Modified Julian Day */
1163 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1164 } else if (count
== 4) {
1166 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1167 } else if (count
>= 5) {
1169 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1174 /* Local Day of Week */
1178 g_string_append(string
, purple_utf8_strftime("%u", tm
));
1179 } else if (count
== 3) {
1181 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1182 } else if (count
== 4) {
1184 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1185 } else if (count
>= 5) {
1187 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1192 /* Stand-alone Local Day of Week */
1196 g_string_append(string
, purple_utf8_strftime("%u", tm
));
1198 } else if (count
== 3) {
1200 g_string_append(string
, purple_utf8_strftime("%a", tm
));
1201 } else if (count
== 4) {
1203 g_string_append(string
, purple_utf8_strftime("%A", tm
));
1204 } else if (count
>= 5) {
1206 g_string_append_len(string
, purple_utf8_strftime("%a", tm
), 1);
1214 g_string_append(string
, purple_utf8_strftime("%p", tm
));
1222 g_string_append(string
, purple_utf8_strftime("%I", tm
));
1223 } else if (count
>= 2) {
1225 g_string_append(string
, purple_utf8_strftime("%I", tm
));
1234 g_string_append(string
, purple_utf8_strftime("%H", tm
));
1235 } else if (count
>= 2) {
1237 g_string_append(string
, purple_utf8_strftime("%H", tm
));
1246 } else if (count
>= 2) {
1256 } else if (count
>= 2) {
1262 /* Hour (hHkK by locale) */
1269 g_string_append(string
, purple_utf8_strftime("%M", tm
));
1270 count
= MIN(count
, 2);
1276 g_string_append(string
, purple_utf8_strftime("%S", tm
));
1277 count
= MIN(count
, 2);
1280 /* Fractional Sub-second */
1289 /* Time Zone (specific non-location format) */
1293 } else if (count
>= 4) {
1303 g_string_append(string
, purple_utf8_strftime("%z", tm
));
1304 } else if (count
== 4) {
1306 } else if (count
>= 5) {
1308 g_string_append(string
, purple_utf8_strftime("%z", tm
));
1313 /* Time Zone (generic non-location format) */
1317 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1319 } else if (count
>= 4) {
1321 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1331 } else if (count
>= 4) {
1332 /* Generic Location Format) */
1333 g_string_append(string
, purple_utf8_strftime("%Z", tm
));
1340 g_string_append_len(string
, format
+ i
, count
);
1347 return g_string_free(string
, FALSE
);
1350 /**************************************************************************
1352 **************************************************************************/
1355 * This function is stolen from glib's gmarkup.c and modified to not
1356 * replace ' with '
1358 static void append_escaped_text(GString
*str
,
1359 const gchar
*text
, gssize length
)
1366 end
= text
+ length
;
1371 next
= g_utf8_next_char (p
);
1376 g_string_append (str
, "&");
1380 g_string_append (str
, "<");
1384 g_string_append (str
, ">");
1388 g_string_append (str
, """);
1392 c
= g_utf8_get_char (p
);
1393 if ((0x1 <= c
&& c
<= 0x8) ||
1394 (0xb <= c
&& c
<= 0xc) ||
1395 (0xe <= c
&& c
<= 0x1f) ||
1396 (0x7f <= c
&& c
<= 0x84) ||
1397 (0x86 <= c
&& c
<= 0x9f))
1398 g_string_append_printf (str
, "&#x%x;", c
);
1400 g_string_append_len (str
, p
, next
- p
);
1408 /* This function is stolen from glib's gmarkup.c */
1409 gchar
*purple_markup_escape_text(const gchar
*text
, gssize length
)
1413 g_return_val_if_fail(text
!= NULL
, NULL
);
1416 length
= strlen(text
);
1418 /* prealloc at least as long as original text */
1419 str
= g_string_sized_new(length
);
1420 append_escaped_text(str
, text
, length
);
1422 return g_string_free(str
, FALSE
);
1426 purple_markup_unescape_entity(const char *text
, int *length
)
1432 if (!text
|| *text
!= '&')
1435 #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1)))
1437 if(IS_ENTITY("&"))
1439 else if(IS_ENTITY("<"))
1441 else if(IS_ENTITY(">"))
1443 else if(IS_ENTITY(" "))
1445 else if(IS_ENTITY("©"))
1446 pln
= "\302\251"; /* or use g_unichar_to_utf8(0xa9); */
1447 else if(IS_ENTITY("""))
1449 else if(IS_ENTITY("®"))
1450 pln
= "\302\256"; /* or use g_unichar_to_utf8(0xae); */
1451 else if(IS_ENTITY("'"))
1453 else if(*(text
+1) == '#' &&
1454 (sscanf(text
, "&#%u%1[;]", £
, temp
) == 2 ||
1455 sscanf(text
, "&#x%x%1[;]", £
, temp
) == 2) &&
1458 int buflen
= g_unichar_to_utf8((gunichar
)pound
, buf
);
1462 len
= (*(text
+2) == 'x' ? 3 : 2);
1463 while(isxdigit((gint
) text
[len
])) len
++;
1464 if(text
[len
] == ';') len
++;
1475 purple_markup_get_css_property(const gchar
*style
,
1478 const gchar
*css_str
= style
;
1479 const gchar
*css_value_start
;
1480 const gchar
*css_value_end
;
1484 g_return_val_if_fail(opt
!= NULL
, NULL
);
1489 /* find the CSS property */
1492 /* skip whitespace characters */
1493 while (*css_str
&& g_ascii_isspace(*css_str
))
1495 if (!g_ascii_isalpha(*css_str
))
1497 if (g_ascii_strncasecmp(css_str
, opt
, strlen(opt
)))
1499 /* go to next css property positioned after the next ';' */
1500 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1510 /* find the CSS value position in the string */
1511 css_str
+= strlen(opt
);
1512 while (*css_str
&& g_ascii_isspace(*css_str
))
1514 if (*css_str
!= ':')
1517 while (*css_str
&& g_ascii_isspace(*css_str
))
1519 if (*css_str
== '\0' || *css_str
== '"' || *css_str
== ';')
1522 /* mark the CSS value */
1523 css_value_start
= css_str
;
1524 while (*css_str
&& *css_str
!= '"' && *css_str
!= ';')
1526 css_value_end
= css_str
- 1;
1528 /* Removes trailing whitespace */
1529 while (css_value_end
> css_value_start
&& g_ascii_isspace(*css_value_end
))
1532 tmp
= g_strndup(css_value_start
, css_value_end
- css_value_start
+ 1);
1533 ret
= purple_unescape_html(tmp
);
1539 gboolean
purple_markup_is_rtl(const char *html
)
1542 const gchar
*start
, *end
;
1543 gboolean res
= FALSE
;
1545 if (purple_markup_find_tag("span", html
, &start
, &end
, &attributes
))
1547 /* tmp is a member of attributes and is free with g_datalist_clear call */
1548 const char *tmp
= g_datalist_get_data(&attributes
, "dir");
1549 if (tmp
&& !g_ascii_strcasecmp(tmp
, "RTL"))
1553 tmp
= g_datalist_get_data(&attributes
, "style");
1556 char *tmp2
= purple_markup_get_css_property(tmp
, "direction");
1557 if (tmp2
&& !g_ascii_strcasecmp(tmp2
, "RTL"))
1563 g_datalist_clear(&attributes
);
1569 purple_markup_find_tag(const char *needle
, const char *haystack
,
1570 const char **start
, const char **end
, GData
**attributes
)
1573 const char *cur
= haystack
;
1575 gboolean found
= FALSE
;
1576 gboolean in_tag
= FALSE
;
1577 gboolean in_attr
= FALSE
;
1578 const char *in_quotes
= NULL
;
1581 g_return_val_if_fail( needle
!= NULL
, FALSE
);
1582 g_return_val_if_fail( *needle
!= '\0', FALSE
);
1583 g_return_val_if_fail( haystack
!= NULL
, FALSE
);
1584 g_return_val_if_fail( start
!= NULL
, FALSE
);
1585 g_return_val_if_fail( end
!= NULL
, FALSE
);
1586 g_return_val_if_fail(attributes
!= NULL
, FALSE
);
1588 needlelen
= strlen(needle
);
1589 g_datalist_init(&attribs
);
1591 while (*cur
&& !found
) {
1594 const char *close
= cur
;
1596 while (*close
&& *close
!= *in_quotes
)
1599 /* if we got the close quote, store the value and carry on from *
1600 * after it. if we ran to the end of the string, point to the NULL *
1601 * and we're outta here */
1603 /* only store a value if we have an attribute name */
1605 size_t len
= close
- cur
;
1606 char *val
= g_strndup(cur
, len
);
1608 g_datalist_set_data_full(&attribs
, name
, val
, g_free
);
1618 } else if (in_attr
) {
1619 const char *close
= cur
;
1621 while (*close
&& *close
!= '>' && *close
!= '"' &&
1622 *close
!= '\'' && *close
!= ' ' && *close
!= '=')
1625 /* if we got the equals, store the name of the attribute. if we got
1626 * the quote, save the attribute and go straight to quote mode.
1627 * otherwise the tag closed or we reached the end of the string,
1628 * so we can get outta here */
1636 size_t len
= close
- cur
;
1638 /* don't store a blank attribute name */
1641 name
= g_ascii_strdown(cur
, len
);
1659 /* swallow extra spaces inside tag */
1660 while (*cur
&& *cur
== ' ') cur
++;
1677 /* if we hit a < followed by the name of our tag... */
1678 if (*cur
== '<' && !g_ascii_strncasecmp(cur
+ 1, needle
, needlelen
)) {
1680 cur
= cur
+ needlelen
+ 1;
1682 /* if we're pointing at a space or a >, we found the right tag. if *
1683 * we're not, we've found a longer tag, so we need to skip to the *
1684 * >, but not being distracted by >s inside quotes. */
1685 if (*cur
== ' ' || *cur
== '>') {
1688 while (*cur
&& *cur
!= '"' && *cur
!= '\'' && *cur
!= '>') {
1691 while (*cur
&& *cur
!= '"')
1693 } else if (*cur
== '\'') {
1695 while (*cur
&& *cur
!= '\'')
1708 /* clean up any attribute name from a premature termination */
1712 *attributes
= attribs
;
1723 purple_markup_extract_info_field(const char *str
, int len
, PurpleNotifyUserInfo
*user_info
,
1724 const char *start_token
, int skip
,
1725 const char *end_token
, char check_value
,
1726 const char *no_value_token
,
1727 const char *display_name
, gboolean is_link
,
1728 const char *link_prefix
,
1729 PurpleInfoFieldFormatCallback format_cb
)
1733 g_return_val_if_fail(str
!= NULL
, FALSE
);
1734 g_return_val_if_fail(user_info
!= NULL
, FALSE
);
1735 g_return_val_if_fail(start_token
!= NULL
, FALSE
);
1736 g_return_val_if_fail(end_token
!= NULL
, FALSE
);
1737 g_return_val_if_fail(display_name
!= NULL
, FALSE
);
1739 p
= strstr(str
, start_token
);
1744 p
+= strlen(start_token
) + skip
;
1749 if (check_value
!= '\0' && *p
== check_value
)
1752 q
= strstr(p
, end_token
);
1754 /* Trim leading blanks */
1755 while (*p
!= '\n' && g_ascii_isspace(*p
)) {
1759 /* Trim trailing blanks */
1760 while (q
> p
&& g_ascii_isspace(*(q
- 1))) {
1764 /* Don't bother with null strings */
1768 if (q
!= NULL
&& (!no_value_token
||
1769 (no_value_token
&& strncmp(p
, no_value_token
,
1770 strlen(no_value_token
)))))
1772 GString
*dest
= g_string_new("");
1776 g_string_append(dest
, "<a href=\"");
1779 g_string_append(dest
, link_prefix
);
1781 if (format_cb
!= NULL
)
1783 char *reformatted
= format_cb(p
, q
- p
);
1784 g_string_append(dest
, reformatted
);
1785 g_free(reformatted
);
1788 g_string_append_len(dest
, p
, q
- p
);
1789 g_string_append(dest
, "\">");
1792 g_string_append(dest
, link_prefix
);
1794 g_string_append_len(dest
, p
, q
- p
);
1795 g_string_append(dest
, "</a>");
1799 if (format_cb
!= NULL
)
1801 char *reformatted
= format_cb(p
, q
- p
);
1802 g_string_append(dest
, reformatted
);
1803 g_free(reformatted
);
1806 g_string_append_len(dest
, p
, q
- p
);
1809 purple_notify_user_info_add_pair_html(user_info
, display_name
, dest
->str
);
1810 g_string_free(dest
, TRUE
);
1818 struct purple_parse_tag
{
1824 /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method
1825 recommended in the GCC docs). It contains 'continue's that should
1826 affect the while-loop in purple_markup_html_to_xhtml and doing the
1827 above would break that.
1828 Also, remember to put braces in constructs that require them for
1829 multiple statements when using this macro. */
1830 #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \
1831 const char *o = c + strlen("<" x); \
1832 const char *p = NULL, *q = NULL, *r = NULL; \
1833 /* o = iterating over full tag \
1834 * p = > (end of tag) \
1835 * q = start of quoted bit \
1836 * r = < inside tag \
1838 GString *innards = g_string_new(""); \
1840 if(!q && (*o == '\"' || *o == '\'') ) { \
1843 if(*o == *q) { /* end of quoted bit */ \
1844 char *unescaped = g_strndup(q+1, o-q-1); \
1845 char *escaped = g_markup_escape_text(unescaped, -1); \
1846 g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \
1847 g_free(unescaped); \
1850 } else if(*c == '\\') { \
1853 } else if(*o == '<') { \
1855 } else if(*o == '>') { \
1859 innards = g_string_append_c(innards, *o); \
1863 if(p && !r) { /* got an end of tag and no other < earlier */\
1864 if(*(p-1) != '/') { \
1865 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1868 tags = g_list_prepend(tags, pt); \
1871 xhtml = g_string_append(xhtml, "<" y); \
1872 xhtml = g_string_append(xhtml, innards->str); \
1873 xhtml = g_string_append_c(xhtml, '>'); \
1876 } else { /* got end of tag with earlier < *or* didn't get anything */ \
1878 xhtml = g_string_append(xhtml, "<"); \
1880 plain = g_string_append_c(plain, '<'); \
1883 g_string_free(innards, TRUE); \
1886 if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \
1887 (*(c+strlen("<" x)) == '>' || \
1888 !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \
1890 xhtml = g_string_append(xhtml, "<" y); \
1891 c += strlen("<" x); \
1893 struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \
1896 tags = g_list_prepend(tags, pt); \
1898 xhtml = g_string_append_c(xhtml, '>'); \
1901 xhtml = g_string_append(xhtml, "/>");\
1903 c = strchr(c, '>') + 1; \
1906 /* Don't forget to check the note above for ALLOW_TAG_ALT. */
1907 #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x)
1909 purple_markup_html_to_xhtml(const char *html
, char **xhtml_out
,
1912 GString
*xhtml
= NULL
;
1913 GString
*plain
= NULL
;
1914 GString
*url
= NULL
;
1915 GString
*cdata
= NULL
;
1916 GList
*tags
= NULL
, *tag
;
1917 const char *c
= html
;
1920 #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \
1925 #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>')))
1927 g_return_if_fail(xhtml_out
!= NULL
|| plain_out
!= NULL
);
1930 xhtml
= g_string_new("");
1932 plain
= g_string_new("");
1936 if(*(c
+1) == '/') { /* closing tag */
1939 struct purple_parse_tag
*pt
= tag
->data
;
1940 if(!g_ascii_strncasecmp((c
+2), pt
->src_tag
, strlen(pt
->src_tag
)) && *(c
+strlen(pt
->src_tag
)+2) == '>') {
1941 c
+= strlen(pt
->src_tag
) + 3;
1948 struct purple_parse_tag
*pt
= tags
->data
;
1949 if(xhtml
&& !pt
->ignore
)
1950 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
1951 if(plain
&& purple_strequal(pt
->src_tag
, "a")) {
1952 /* if this is a link, we have to add the url to the plaintext, too */
1954 (!g_string_equal(cdata
, url
) && (g_ascii_strncasecmp(url
->str
, "mailto:", 7) != 0 ||
1955 g_utf8_collate(url
->str
+ 7, cdata
->str
) != 0)))
1956 g_string_append_printf(plain
, " <%s>", g_strstrip(purple_unescape_html(url
->str
)));
1958 g_string_free(cdata
, TRUE
);
1965 tags
= g_list_remove(tags
, pt
);
1969 tags
= g_list_remove(tags
, tag
->data
);
1971 /* a closing tag we weren't expecting...
1972 * we'll let it slide, if it's really a tag...if it's
1973 * just a </ we'll escape it properly */
1974 const char *end
= c
+2;
1975 while(*end
&& g_ascii_isalpha(*end
))
1981 xhtml
= g_string_append(xhtml
, "<");
1983 plain
= g_string_append_c(plain
, '<');
1987 } else { /* opening tag */
1988 ALLOW_TAG("blockquote");
1998 /* we only allow html to start the message */
2002 ALLOW_TAG_ALT("i", "em");
2003 ALLOW_TAG_ALT("italic", "em");
2013 /* we skip <HR> because it's not legal in XHTML-IM. However,
2014 * we still want to send something sensible, so we put a
2015 * linebreak in its place. <BR> also needs special handling
2016 * because putting a </BR> to close it would just be dumb. */
2017 if((!g_ascii_strncasecmp(c
, "<br", 3)
2018 || !g_ascii_strncasecmp(c
, "<hr", 3))
2019 && (*(c
+3) == '>' ||
2020 !g_ascii_strncasecmp(c
+3, "/>", 2) ||
2021 !g_ascii_strncasecmp(c
+3, " />", 3))) {
2022 c
= strchr(c
, '>') + 1;
2024 xhtml
= g_string_append(xhtml
, "<br/>");
2025 if(plain
&& *c
!= '\n')
2026 plain
= g_string_append_c(plain
, '\n');
2029 if(!g_ascii_strncasecmp(c
, "<b>", 3) || !g_ascii_strncasecmp(c
, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c
, "<strong>", strlen("<strong>"))) {
2030 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2033 else if (*(c
+2) == 'o')
2034 pt
->src_tag
= "bold";
2036 pt
->src_tag
= "strong";
2037 pt
->dest_tag
= "span";
2038 tags
= g_list_prepend(tags
, pt
);
2039 c
= strchr(c
, '>') + 1;
2041 xhtml
= g_string_append(xhtml
, "<span style='font-weight: bold;'>");
2044 if(!g_ascii_strncasecmp(c
, "<u>", 3) || !g_ascii_strncasecmp(c
, "<underline>", strlen("<underline>"))) {
2045 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2046 pt
->src_tag
= *(c
+2) == '>' ? "u" : "underline";
2047 pt
->dest_tag
= "span";
2048 tags
= g_list_prepend(tags
, pt
);
2049 c
= strchr(c
, '>') + 1;
2051 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: underline;'>");
2054 if(!g_ascii_strncasecmp(c
, "<s>", 3) || !g_ascii_strncasecmp(c
, "<strike>", strlen("<strike>"))) {
2055 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2056 pt
->src_tag
= *(c
+2) == '>' ? "s" : "strike";
2057 pt
->dest_tag
= "span";
2058 tags
= g_list_prepend(tags
, pt
);
2059 c
= strchr(c
, '>') + 1;
2061 xhtml
= g_string_append(xhtml
, "<span style='text-decoration: line-through;'>");
2064 if(!g_ascii_strncasecmp(c
, "<sub>", 5)) {
2065 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2066 pt
->src_tag
= "sub";
2067 pt
->dest_tag
= "span";
2068 tags
= g_list_prepend(tags
, pt
);
2069 c
= strchr(c
, '>') + 1;
2071 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:sub;'>");
2074 if(!g_ascii_strncasecmp(c
, "<sup>", 5)) {
2075 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2076 pt
->src_tag
= "sup";
2077 pt
->dest_tag
= "span";
2078 tags
= g_list_prepend(tags
, pt
);
2079 c
= strchr(c
, '>') + 1;
2081 xhtml
= g_string_append(xhtml
, "<span style='vertical-align:super;'>");
2084 if (!g_ascii_strncasecmp(c
, "<img", 4) && (*(c
+4) == '>' || *(c
+4) == ' ')) {
2085 const char *p
= c
+ 4;
2086 GString
*src
= NULL
, *alt
= NULL
;
2087 #define ESCAPE(from, to) \
2088 CHECK_QUOTE(from); \
2089 while (VALID_CHAR(from)) { \
2091 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2092 to = g_string_append(to, "&"); \
2093 else if (*from == '\'') \
2094 to = g_string_append(to, "'"); \
2096 to = g_string_append_c(to, *from); \
2100 while (*p
&& *p
!= '>') {
2101 if (!g_ascii_strncasecmp(p
, "src=", 4)) {
2102 const char *q
= p
+ 4;
2104 g_string_free(src
, TRUE
);
2105 src
= g_string_new("");
2108 } else if (!g_ascii_strncasecmp(p
, "alt=", 4)) {
2109 const char *q
= p
+ 4;
2111 g_string_free(alt
, TRUE
);
2112 alt
= g_string_new("");
2120 if ((c
= strchr(p
, '>')) != NULL
)
2124 /* src and alt are required! */
2126 g_string_append_printf(xhtml
, "<img src='%s' alt='%s' />", g_strstrip(src
->str
), alt
? alt
->str
: "");
2129 plain
= g_string_append(plain
, purple_unescape_html(alt
->str
));
2131 xhtml
= g_string_append(xhtml
, alt
->str
);
2132 g_string_free(alt
, TRUE
);
2134 g_string_free(src
, TRUE
);
2137 if (!g_ascii_strncasecmp(c
, "<a", 2) && (*(c
+2) == '>' || *(c
+2) == ' ')) {
2138 const char *p
= c
+ 2;
2139 struct purple_parse_tag
*pt
;
2140 while (*p
&& *p
!= '>') {
2141 if (!g_ascii_strncasecmp(p
, "href=", 5)) {
2142 const char *q
= p
+ 5;
2144 g_string_free(url
, TRUE
);
2145 url
= g_string_new("");
2147 g_string_free(cdata
, TRUE
);
2148 cdata
= g_string_new("");
2150 while (VALID_CHAR(q
)) {
2152 if ((*q
== '&') && (purple_markup_unescape_entity(q
, &len
) == NULL
))
2153 url
= g_string_append(url
, "&");
2155 url
= g_string_append(url
, """);
2157 url
= g_string_append_c(url
, *q
);
2165 if ((c
= strchr(p
, '>')) != NULL
)
2169 pt
= g_new0(struct purple_parse_tag
, 1);
2172 tags
= g_list_prepend(tags
, pt
);
2174 g_string_append_printf(xhtml
, "<a href=\"%s\">", url
? g_strstrip(url
->str
) : "");
2177 #define ESCAPE(from, to) \
2178 CHECK_QUOTE(from); \
2179 while (VALID_CHAR(from)) { \
2181 if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \
2182 to = g_string_append(to, "&"); \
2183 else if (*from == '\'') \
2184 to = g_string_append_c(to, '\"'); \
2186 to = g_string_append_c(to, *from); \
2189 if(!g_ascii_strncasecmp(c
, "<font", 5) && (*(c
+5) == '>' || *(c
+5) == ' ')) {
2190 const char *p
= c
+ 5;
2191 GString
*style
= g_string_new("");
2192 struct purple_parse_tag
*pt
;
2193 while (*p
&& *p
!= '>') {
2194 if (!g_ascii_strncasecmp(p
, "back=", 5)) {
2195 const char *q
= p
+ 5;
2196 GString
*color
= g_string_new("");
2198 g_string_append_printf(style
, "background: %s; ", color
->str
);
2199 g_string_free(color
, TRUE
);
2201 } else if (!g_ascii_strncasecmp(p
, "color=", 6)) {
2202 const char *q
= p
+ 6;
2203 GString
*color
= g_string_new("");
2205 g_string_append_printf(style
, "color: %s; ", color
->str
);
2206 g_string_free(color
, TRUE
);
2208 } else if (!g_ascii_strncasecmp(p
, "face=", 5)) {
2209 const char *q
= p
+ 5;
2210 GString
*face
= g_string_new("");
2212 g_string_append_printf(style
, "font-family: %s; ", g_strstrip(face
->str
));
2213 g_string_free(face
, TRUE
);
2215 } else if (!g_ascii_strncasecmp(p
, "size=", 5)) {
2216 const char *q
= p
+ 5;
2218 const char *size
= "medium";
2245 g_string_append_printf(style
, "font-size: %s; ", size
);
2251 if ((c
= strchr(p
, '>')) != NULL
)
2255 pt
= g_new0(struct purple_parse_tag
, 1);
2256 pt
->src_tag
= "font";
2257 pt
->dest_tag
= "span";
2258 tags
= g_list_prepend(tags
, pt
);
2259 if(style
->len
&& xhtml
)
2260 g_string_append_printf(xhtml
, "<span style='%s'>", g_strstrip(style
->str
));
2263 g_string_free(style
, TRUE
);
2267 if (!g_ascii_strncasecmp(c
, "<body ", 6)) {
2268 const char *p
= c
+ 6;
2269 gboolean did_something
= FALSE
;
2270 while (*p
&& *p
!= '>') {
2271 if (!g_ascii_strncasecmp(p
, "bgcolor=", 8)) {
2272 const char *q
= p
+ 8;
2273 struct purple_parse_tag
*pt
= g_new0(struct purple_parse_tag
, 1);
2274 GString
*color
= g_string_new("");
2276 while (VALID_CHAR(q
)) {
2277 color
= g_string_append_c(color
, *q
);
2281 g_string_append_printf(xhtml
, "<span style='background: %s;'>", g_strstrip(color
->str
));
2282 g_string_free(color
, TRUE
);
2283 if ((c
= strchr(p
, '>')) != NULL
)
2287 pt
->src_tag
= "body";
2288 pt
->dest_tag
= "span";
2289 tags
= g_list_prepend(tags
, pt
);
2290 did_something
= TRUE
;
2295 if (did_something
) continue;
2297 /* this has to come after the special case for bgcolor */
2299 if(!g_ascii_strncasecmp(c
, "<!--", strlen("<!--"))) {
2300 char *p
= strstr(c
+ strlen("<!--"), "-->");
2303 xhtml
= g_string_append(xhtml
, "<!--");
2304 c
+= strlen("<!--");
2310 xhtml
= g_string_append(xhtml
, "<");
2312 plain
= g_string_append_c(plain
, '<');
2315 } else if(*c
== '&') {
2320 if ((pln
= purple_markup_unescape_entity(c
, &len
)) == NULL
) {
2322 g_snprintf(buf
, sizeof(buf
), "%c", *c
);
2326 xhtml
= g_string_append_len(xhtml
, c
, len
);
2328 plain
= g_string_append(plain
, pln
);
2330 cdata
= g_string_append_len(cdata
, c
, len
);
2334 xhtml
= g_string_append_c(xhtml
, *c
);
2336 plain
= g_string_append_c(plain
, *c
);
2338 cdata
= g_string_append_c(cdata
, *c
);
2343 for (tag
= tags
; tag
; tag
= tag
->next
) {
2344 struct purple_parse_tag
*pt
= tag
->data
;
2346 g_string_append_printf(xhtml
, "</%s>", pt
->dest_tag
);
2351 *xhtml_out
= g_string_free(xhtml
, FALSE
);
2353 *plain_out
= g_string_free(plain
, FALSE
);
2355 g_string_free(url
, TRUE
);
2357 g_string_free(cdata
, TRUE
);
2362 /* The following are probably reasonable changes:
2363 * - \n should be converted to a normal space
2364 * - in addition to <br>, <p> and <div> etc. should also be converted into \n
2365 * - We want to turn </td>#whitespace<td> sequences into a single tab
2366 * - We want to turn <td> into a single tab (for msn profile "parsing")
2367 * - We want to turn </tr>#whitespace<tr> sequences into a single \n
2368 * - <script>...</script> and <style>...</style> should be completely removed
2372 purple_markup_strip_html(const char *str
)
2374 int i
, j
, k
, entlen
;
2375 gboolean visible
= TRUE
;
2376 gboolean closing_td_p
= FALSE
;
2378 const gchar
*cdata_close_tag
= NULL
, *ent
;
2385 str2
= g_strdup(str
);
2387 for (i
= 0, j
= 0; str2
[i
]; i
++)
2391 if (cdata_close_tag
)
2393 /* Note: Don't even assume any other tag is a tag in CDATA */
2394 if (g_ascii_strncasecmp(str2
+ i
, cdata_close_tag
,
2395 strlen(cdata_close_tag
)) == 0)
2397 i
+= strlen(cdata_close_tag
) - 1;
2398 cdata_close_tag
= NULL
;
2402 else if (g_ascii_strncasecmp(str2
+ i
, "<td", 3) == 0 && closing_td_p
)
2407 else if (g_ascii_strncasecmp(str2
+ i
, "</td>", 5) == 0)
2409 closing_td_p
= TRUE
;
2414 closing_td_p
= FALSE
;
2420 if(g_ascii_isspace(str2
[k
]))
2424 /* Scan until we end the tag either implicitly (closed start
2425 * tag) or explicitly, using a sloppy method (i.e., < or >
2426 * inside quoted attributes will screw us up)
2428 while (str2
[k
] && str2
[k
] != '<' && str2
[k
] != '>')
2433 /* If we've got an <a> tag with an href, save the address
2434 * to print later. */
2435 if (g_ascii_strncasecmp(str2
+ i
, "<a", 2) == 0 &&
2436 g_ascii_isspace(str2
[i
+2]))
2438 int st
; /* start of href, inclusive [ */
2439 int end
; /* end of href, exclusive ) */
2441 /* Find start of href */
2442 for (st
= i
+ 3; st
< k
; st
++)
2444 if (g_ascii_strncasecmp(str2
+st
, "href=", 5) == 0)
2447 if (str2
[st
] == '"' || str2
[st
] == '\'')
2455 /* find end of address */
2456 for (end
= st
; end
< k
&& str2
[end
] != delim
; end
++)
2458 /* All the work is done in the loop construct above. */
2461 /* If there's an address, save it. If there was
2462 * already one saved, kill it. */
2467 tmp
= g_strndup(str2
+ st
, end
- st
);
2468 href
= purple_unescape_html(tmp
);
2474 /* Replace </a> with an ascii representation of the
2475 * address the link was pointing to. */
2476 else if (href
!= NULL
&& g_ascii_strncasecmp(str2
+ i
, "</a>", 4) == 0)
2478 size_t hrlen
= strlen(href
);
2480 /* Only insert the href if it's different from the CDATA. */
2481 if ((hrlen
!= (gsize
)(j
- href_st
) ||
2482 strncmp(str2
+ href_st
, href
, hrlen
)) &&
2483 (hrlen
!= (gsize
)(j
- href_st
+ 7) || /* 7 == strlen("http://") */
2484 strncmp(str2
+ href_st
, href
+ 7, hrlen
- 7)))
2488 g_memmove(str2
+ j
, href
, hrlen
);
2496 /* Check for tags which should be mapped to newline (but ignore some of
2497 * the tags at the beginning of the text) */
2498 else if ((j
&& (g_ascii_strncasecmp(str2
+ i
, "<p>", 3) == 0
2499 || g_ascii_strncasecmp(str2
+ i
, "<tr", 3) == 0
2500 || g_ascii_strncasecmp(str2
+ i
, "<hr", 3) == 0
2501 || g_ascii_strncasecmp(str2
+ i
, "<li", 3) == 0
2502 || g_ascii_strncasecmp(str2
+ i
, "<div", 4) == 0))
2503 || g_ascii_strncasecmp(str2
+ i
, "<br", 3) == 0
2504 || g_ascii_strncasecmp(str2
+ i
, "</table>", 8) == 0)
2508 /* Check for tags which begin CDATA and need to be closed */
2509 #if 0 /* FIXME.. option is end tag optional, we can't handle this right now */
2510 else if (g_ascii_strncasecmp(str2
+ i
, "<option", 7) == 0)
2512 /* FIXME: We should not do this if the OPTION is SELECT'd */
2513 cdata_close_tag
= "</option>";
2516 else if (g_ascii_strncasecmp(str2
+ i
, "<script", 7) == 0)
2518 cdata_close_tag
= "</script>";
2520 else if (g_ascii_strncasecmp(str2
+ i
, "<style", 6) == 0)
2522 cdata_close_tag
= "</style>";
2524 /* Update the index and continue checking after the tag */
2525 i
= (str2
[k
] == '<' || str2
[k
] == '\0')? k
- 1: k
;
2529 else if (cdata_close_tag
)
2533 else if (!g_ascii_isspace(str2
[i
]))
2538 if (str2
[i
] == '&' && (ent
= purple_markup_unescape_entity(str2
+ i
, &entlen
)) != NULL
)
2547 str2
[j
++] = g_ascii_isspace(str2
[i
])? ' ': str2
[i
];
2576 badentity(const char *c
)
2578 if (!g_ascii_strncasecmp(c
, "<", 4) ||
2579 !g_ascii_strncasecmp(c
, ">", 4) ||
2580 !g_ascii_strncasecmp(c
, """, 6)) {
2587 process_link(GString
*ret
,
2588 const char *start
, const char *c
,
2590 const char *urlprefix
,
2593 char *url_buf
, *tmpurlbuf
;
2597 if (!badchar(*t
) && !badentity(t
))
2600 if (t
- c
== matchlen
)
2603 if (*t
== ',' && *(t
+ 1) != ' ') {
2607 if (t
> start
&& *(t
- 1) == '.')
2609 if (t
> start
&& *(t
- 1) == ')' && inside_paren
> 0)
2612 url_buf
= g_strndup(c
, t
- c
);
2613 tmpurlbuf
= purple_unescape_html(url_buf
);
2614 g_string_append_printf(ret
, "<A HREF=\"%s%s\">%s</A>",
2616 tmpurlbuf
, url_buf
);
2626 purple_markup_linkify(const char *text
)
2628 const char *c
, *t
, *q
= NULL
;
2629 char *tmpurlbuf
, *url_buf
;
2631 gboolean inside_html
= FALSE
;
2632 int inside_paren
= 0;
2638 ret
= g_string_new("");
2643 if(*c
== '(' && !inside_html
) {
2645 ret
= g_string_append_c(ret
, *c
);
2651 inside_html
= FALSE
;
2652 } else if(!q
&& (*c
== '\"' || *c
== '\'')) {
2658 } else if(*c
== '<') {
2660 if (!g_ascii_strncasecmp(c
, "<A", 2)) {
2662 if (!g_ascii_strncasecmp(c
, "/A>", 3)) {
2663 inside_html
= FALSE
;
2666 ret
= g_string_append_c(ret
, *c
);
2672 } else if (!g_ascii_strncasecmp(c
, "http://", 7)) {
2673 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2674 } else if (!g_ascii_strncasecmp(c
, "https://", 8)) {
2675 c
= process_link(ret
, text
, c
, 8, "", inside_paren
);
2676 } else if (!g_ascii_strncasecmp(c
, "ftp://", 6)) {
2677 c
= process_link(ret
, text
, c
, 6, "", inside_paren
);
2678 } else if (!g_ascii_strncasecmp(c
, "sftp://", 7)) {
2679 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2680 } else if (!g_ascii_strncasecmp(c
, "file://", 7)) {
2681 c
= process_link(ret
, text
, c
, 7, "", inside_paren
);
2682 } else if (!g_ascii_strncasecmp(c
, "www.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2683 c
= process_link(ret
, text
, c
, 4, "http://", inside_paren
);
2684 } else if (!g_ascii_strncasecmp(c
, "ftp.", 4) && c
[4] != '.' && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2685 c
= process_link(ret
, text
, c
, 4, "ftp://", inside_paren
);
2686 } else if (!g_ascii_strncasecmp(c
, "xmpp:", 5) && (c
== text
|| badchar(c
[-1]) || badentity(c
-1))) {
2687 c
= process_link(ret
, text
, c
, 5, "", inside_paren
);
2688 } else if (!g_ascii_strncasecmp(c
, "mailto:", 7)) {
2691 if (badchar(*t
) || badentity(t
)) {
2696 if (t
> text
&& *(t
- 1) == '.')
2698 if ((d
= strstr(c
+ 7, "?")) != NULL
&& d
< t
)
2699 url_buf
= g_strndup(c
+ 7, d
- c
- 7);
2701 url_buf
= g_strndup(c
+ 7, t
- c
- 7);
2702 if (!purple_email_is_valid(url_buf
)) {
2707 url_buf
= g_strndup(c
, t
- c
);
2708 tmpurlbuf
= purple_unescape_html(url_buf
);
2709 g_string_append_printf(ret
, "<A HREF=\"%s\">%s</A>",
2710 tmpurlbuf
, url_buf
);
2718 } else if (c
!= text
&& (*c
== '@')) {
2720 GString
*gurl_buf
= NULL
;
2721 const char illegal_chars
[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0";
2723 if (strchr(illegal_chars
,*(c
- 1)) || strchr(illegal_chars
, *(c
+ 1)))
2727 gurl_buf
= g_string_new("");
2732 /* iterate backwards grabbing the local part of an email address */
2733 g
= g_utf8_get_char(t
);
2734 if (badchar(*t
) || (g
>= 127) || (*t
== '(') ||
2735 ((*t
== ';') && ((t
> (text
+2) && (!g_ascii_strncasecmp(t
- 3, "<", 4) ||
2736 !g_ascii_strncasecmp(t
- 3, ">", 4))) ||
2737 (t
> (text
+4) && (!g_ascii_strncasecmp(t
- 5, """, 6)))))) {
2738 /* local part will already be part of ret, strip it out */
2739 ret
= g_string_truncate(ret
, ret
->len
- (c
- t
));
2740 ret
= g_string_append_unichar(ret
, g
);
2743 g_string_prepend_unichar(gurl_buf
, g
);
2744 t
= g_utf8_find_prev_char(text
, t
);
2746 ret
= g_string_assign(ret
, "");
2752 t
= g_utf8_find_next_char(c
, NULL
);
2755 /* iterate forwards grabbing the domain part of an email address */
2756 g
= g_utf8_get_char(t
);
2757 if (badchar(*t
) || (g
>= 127) || (*t
== ')') || badentity(t
)) {
2760 url_buf
= g_string_free(gurl_buf
, FALSE
);
2762 /* strip off trailing periods */
2764 for (d
= url_buf
+ strlen(url_buf
) - 1; *d
== '.'; d
--, t
--)
2768 tmpurlbuf
= purple_unescape_html(url_buf
);
2769 if (purple_email_is_valid(tmpurlbuf
)) {
2770 g_string_append_printf(ret
, "<A HREF=\"mailto:%s\">%s</A>",
2771 tmpurlbuf
, url_buf
);
2773 g_string_append(ret
, url_buf
);
2781 g_string_append_unichar(gurl_buf
, g
);
2782 t
= g_utf8_find_next_char(t
, NULL
);
2787 if(*c
== ')' && !inside_html
) {
2789 ret
= g_string_append_c(ret
, *c
);
2796 ret
= g_string_append_c(ret
, *c
);
2800 return g_string_free(ret
, FALSE
);
2803 char *purple_unescape_text(const char *in
)
2811 ret
= g_string_new("");
2816 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2817 g_string_append(ret
, ent
);
2820 g_string_append_c(ret
, *c
);
2825 return g_string_free(ret
, FALSE
);
2828 char *purple_unescape_html(const char *html
)
2831 const char *c
= html
;
2836 ret
= g_string_new("");
2841 if ((ent
= purple_markup_unescape_entity(c
, &len
)) != NULL
) {
2842 g_string_append(ret
, ent
);
2844 } else if (!strncmp(c
, "<br>", 4)) {
2845 g_string_append_c(ret
, '\n');
2848 g_string_append_c(ret
, *c
);
2853 return g_string_free(ret
, FALSE
);
2857 purple_markup_slice(const char *str
, guint x
, guint y
)
2862 gboolean appended
= FALSE
;
2866 g_return_val_if_fail(str
!= NULL
, NULL
);
2867 g_return_val_if_fail(x
<= y
, NULL
);
2870 return g_strdup("");
2872 ret
= g_string_new("");
2875 while (*str
&& (z
< y
)) {
2876 c
= g_utf8_get_char(str
);
2879 char *end
= strchr(str
, '>');
2882 g_string_free(ret
, TRUE
);
2883 while ((tag
= g_queue_pop_head(q
)))
2889 if (!g_ascii_strncasecmp(str
, "<img ", 5)) {
2890 z
+= strlen("[Image]");
2891 } else if (!g_ascii_strncasecmp(str
, "<br", 3)) {
2893 } else if (!g_ascii_strncasecmp(str
, "<hr>", 4)) {
2894 z
+= strlen("\n---\n");
2895 } else if (!g_ascii_strncasecmp(str
, "</", 2)) {
2899 tmp
= g_queue_pop_head(q
);
2903 /* push it unto the stack */
2906 tmp
= g_strndup(str
, end
- str
+ 1);
2907 g_queue_push_head(q
, tmp
);
2912 g_string_append_len(ret
, str
, end
- str
+ 1);
2916 } else if (c
== '&') {
2917 char *end
= strchr(str
, ';');
2919 g_string_free(ret
, TRUE
);
2920 while ((tag
= g_queue_pop_head(q
)))
2928 g_string_append_len(ret
, str
, end
- str
+ 1);
2933 if (z
== x
&& z
> 0 && !appended
) {
2938 g_string_append(ret
, tag
);
2945 g_string_append_unichar(ret
, c
);
2949 str
= g_utf8_next_char(str
);
2952 while ((tag
= g_queue_pop_head(q
))) {
2955 name
= purple_markup_get_tag_name(tag
);
2956 g_string_append_printf(ret
, "</%s>", name
);
2962 return g_string_free(ret
, FALSE
);
2966 purple_markup_get_tag_name(const char *tag
)
2969 g_return_val_if_fail(tag
!= NULL
, NULL
);
2970 g_return_val_if_fail(*tag
== '<', NULL
);
2972 for (i
= 1; tag
[i
]; i
++)
2973 if (tag
[i
] == '>' || tag
[i
] == ' ' || tag
[i
] == '/')
2976 return g_strndup(tag
+1, i
-1);
2979 /**************************************************************************
2980 * Path/Filename Functions
2981 **************************************************************************/
2983 purple_home_dir(void)
2986 return g_get_home_dir();
2988 return wpurple_home_dir();
2992 /* Returns the argument passed to -c IFF it was present, or ~/.purple. */
2994 purple_user_dir(void)
2996 if (custom_user_dir
!= NULL
)
2997 return custom_user_dir
;
2999 user_dir
= g_build_filename(purple_home_dir(), ".purple", NULL
);
3005 purple_cache_dir(void)
3007 if (custom_user_dir
!= NULL
) {
3008 return custom_user_dir
;
3012 cache_dir
= g_build_filename(g_get_user_cache_dir(), "purple", NULL
);
3019 purple_config_dir(void)
3021 if (custom_user_dir
!= NULL
) {
3022 return custom_user_dir
;
3026 config_dir
= g_build_filename(g_get_user_config_dir(), "purple", NULL
);
3033 purple_data_dir(void)
3035 if (custom_user_dir
!= NULL
) {
3036 return custom_user_dir
;
3040 data_dir
= g_build_filename(g_get_user_data_dir(), "purple", NULL
);
3046 void purple_util_set_user_dir(const char *dir
)
3048 g_free(custom_user_dir
);
3050 if (dir
!= NULL
&& *dir
)
3051 custom_user_dir
= g_strdup(dir
);
3053 custom_user_dir
= NULL
;
3056 int purple_build_dir (const char *path
, int mode
)
3058 return g_mkdir_with_parents(path
, mode
);
3062 purple_util_write_data_to_file_common(const char *dir
, const char *filename
, const char *data
, gssize size
)
3064 gchar
*filename_full
;
3065 gboolean ret
= FALSE
;
3067 g_return_val_if_fail(dir
!= NULL
, FALSE
);
3069 purple_debug_misc("util", "Writing file %s to directory %s",
3072 /* Ensure the user directory exists */
3073 if (!g_file_test(dir
, G_FILE_TEST_IS_DIR
))
3075 if (g_mkdir(dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
) == -1)
3077 purple_debug_error("util", "Error creating directory %s: %s\n",
3078 dir
, g_strerror(errno
));
3083 filename_full
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", dir
, filename
);
3085 ret
= purple_util_write_data_to_file_absolute(filename_full
, data
, size
);
3087 g_free(filename_full
);
3092 * This function is long and beautiful, like my--um, yeah. Anyway,
3093 * it includes lots of error checking so as we don't overwrite
3094 * people's settings if there is a problem writing the new values.
3097 purple_util_write_data_to_file(const char *filename
, const char *data
, gssize size
)
3099 const char *user_dir
= purple_user_dir();
3100 gboolean ret
= purple_util_write_data_to_file_common(user_dir
, filename
, data
, size
);
3106 purple_util_write_data_to_cache_file(const char *filename
, const char *data
, gssize size
)
3108 const char *cache_dir
= purple_cache_dir();
3109 gboolean ret
= purple_util_write_data_to_file_common(cache_dir
, filename
, data
, size
);
3115 purple_util_write_data_to_config_file(const char *filename
, const char *data
, gssize size
)
3117 const char *config_dir
= purple_cache_dir();
3118 gboolean ret
= purple_util_write_data_to_file_common(config_dir
, filename
, data
, size
);
3124 purple_util_write_data_to_data_file(const char *filename
, const char *data
, gssize size
)
3126 const char *data_dir
= purple_cache_dir();
3127 gboolean ret
= purple_util_write_data_to_file_common(data_dir
, filename
, data
, size
);
3133 purple_util_write_data_to_file_absolute(const char *filename_full
, const char *data
, gssize size
)
3135 gchar
*filename_temp
;
3137 gsize real_size
, byteswritten
;
3143 purple_debug_misc("util", "Writing file %s",
3146 g_return_val_if_fail((size
>= -1), FALSE
);
3148 filename_temp
= g_strdup_printf("%s.save", filename_full
);
3150 /* Remove an old temporary file, if one exists */
3151 if (g_file_test(filename_temp
, G_FILE_TEST_EXISTS
))
3153 if (g_unlink(filename_temp
) == -1)
3155 purple_debug_error("util", "Error removing old file "
3157 filename_temp
, g_strerror(errno
));
3162 file
= g_fopen(filename_temp
, "wb");
3165 purple_debug_error("util", "Error opening file %s for "
3167 filename_temp
, g_strerror(errno
));
3168 g_free(filename_temp
);
3173 real_size
= (size
== -1) ? strlen(data
) : (size_t) size
;
3174 byteswritten
= fwrite(data
, 1, real_size
, file
);
3178 /* Set file permissions */
3179 if (fchmod(fileno(file
), S_IRUSR
| S_IWUSR
) == -1) {
3180 purple_debug_error("util", "Error setting permissions of "
3181 "file %s: %s\n", filename_temp
, g_strerror(errno
));
3185 /* Apparently XFS (and possibly other filesystems) do not
3186 * guarantee that file data is flushed before file metadata,
3187 * so this procedure is insufficient without some flushage. */
3188 if (fflush(file
) < 0) {
3189 purple_debug_error("util", "Error flushing %s: %s\n",
3190 filename_temp
, g_strerror(errno
));
3191 g_free(filename_temp
);
3195 if (fsync(fileno(file
)) < 0) {
3196 purple_debug_error("util", "Error syncing file contents for %s: %s\n",
3197 filename_temp
, g_strerror(errno
));
3198 g_free(filename_temp
);
3205 if (fclose(file
) != 0)
3207 purple_debug_error("util", "Error closing file %s: %s\n",
3208 filename_temp
, g_strerror(errno
));
3209 g_free(filename_temp
);
3214 /* This is the same effect (we hope) as the HAVE_FILENO block
3215 * above, but for systems without fileno(). */
3216 if ((fd
= open(filename_temp
, O_RDWR
)) < 0) {
3217 purple_debug_error("util", "Error opening file %s for flush: %s\n",
3218 filename_temp
, g_strerror(errno
));
3219 g_free(filename_temp
);
3225 if (fchmod(fd
, S_IRUSR
| S_IWUSR
) == -1) {
3226 purple_debug_error("util", "Error setting permissions of "
3227 "file %s: %s\n", filename_temp
, g_strerror(errno
));
3231 if (fsync(fd
) < 0) {
3232 purple_debug_error("util", "Error syncing %s: %s\n",
3233 filename_temp
, g_strerror(errno
));
3234 g_free(filename_temp
);
3238 if (close(fd
) < 0) {
3239 purple_debug_error("util", "Error closing %s after sync: %s\n",
3240 filename_temp
, g_strerror(errno
));
3241 g_free(filename_temp
);
3246 /* Ensure the file is the correct size */
3247 if (byteswritten
!= real_size
)
3249 purple_debug_error("util", "Error writing to file %s: Wrote %"
3250 G_GSIZE_FORMAT
" bytes "
3251 "but should have written %" G_GSIZE_FORMAT
3252 "; is your disk full?\n",
3253 filename_temp
, byteswritten
, real_size
);
3254 g_free(filename_temp
);
3257 #ifndef __COVERITY__
3258 /* Use stat to be absolutely sure.
3259 * It causes TOCTOU coverity warning (against g_rename below),
3260 * but it's not a threat for us.
3262 if ((g_stat(filename_temp
, &st
) == -1) || ((gsize
)st
.st_size
!= real_size
)) {
3263 purple_debug_error("util", "Error writing data to file %s: "
3264 "couldn't g_stat file", filename_temp
);
3265 g_free(filename_temp
);
3268 #endif /* __COVERITY__ */
3270 /* Rename to the REAL name */
3271 if (g_rename(filename_temp
, filename_full
) == -1)
3273 purple_debug_error("util", "Error renaming %s to %s: %s\n",
3274 filename_temp
, filename_full
,
3278 g_free(filename_temp
);
3284 purple_util_read_xml_from_file(const char *filename
, const char *description
)
3286 return purple_xmlnode_from_file(purple_user_dir(), filename
, description
, "util");
3290 * Like mkstemp() but returns a file pointer, uses a pre-set template,
3291 * uses the semantics of tempnam() for the directory to use and allocates
3292 * the space for the filepath.
3294 * Caller is responsible for closing the file and removing it when done,
3295 * as well as freeing the space pointed-to by "path" with g_free().
3297 * Returns NULL on failure and cleans up after itself if so.
3299 static const char *purple_mkstemp_templ
= {"purpleXXXXXX"};
3302 purple_mkstemp(char **fpath
, gboolean binary
)
3304 const gchar
*tmpdir
;
3308 g_return_val_if_fail(fpath
!= NULL
, NULL
);
3310 if((tmpdir
= (gchar
*)g_get_tmp_dir()) != NULL
) {
3311 if((*fpath
= g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", tmpdir
, purple_mkstemp_templ
)) != NULL
) {
3312 fd
= g_mkstemp(*fpath
);
3314 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3315 "Couldn't make \"%s\", error: %d\n",
3318 if((fp
= fdopen(fd
, "r+")) == NULL
) {
3320 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3321 "Couldn't fdopen(), error: %d\n", errno
);
3331 purple_debug(PURPLE_DEBUG_ERROR
, "purple_mkstemp",
3332 "g_get_tmp_dir() failed!\n");
3339 purple_program_is_valid(const char *program
)
3341 GError
*error
= NULL
;
3344 gboolean is_valid
= FALSE
;
3346 g_return_val_if_fail(program
!= NULL
, FALSE
);
3347 g_return_val_if_fail(*program
!= '\0', FALSE
);
3349 if (!g_shell_parse_argv(program
, NULL
, &argv
, &error
)) {
3350 purple_debug(PURPLE_DEBUG_ERROR
, "program_is_valid",
3351 "Could not parse program '%s': %s\n",
3352 program
, error
->message
);
3353 g_error_free(error
);
3361 progname
= g_find_program_in_path(argv
[0]);
3362 is_valid
= (progname
!= NULL
);
3364 if(purple_debug_is_verbose())
3365 purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program
,
3366 is_valid
? "Valid" : "Invalid");
3376 purple_running_gnome(void)
3379 gchar
*tmp
= g_find_program_in_path("gvfs-open");
3382 tmp
= g_find_program_in_path("gnome-open");
3391 tmp
= (gchar
*)g_getenv("GNOME_DESKTOP_SESSION_ID");
3393 return ((tmp
!= NULL
) && (*tmp
!= '\0'));
3400 purple_running_kde(void)
3403 gchar
*tmp
= g_find_program_in_path("kfmclient");
3404 const char *session
;
3410 session
= g_getenv("KDE_FULL_SESSION");
3411 if (purple_strequal(session
, "true"))
3414 /* If you run Purple from Konsole under !KDE, this will provide a
3415 * a false positive. Since we do the GNOME checks first, this is
3416 * only a problem if you're running something !(KDE || GNOME) and
3417 * you run Purple from Konsole. This really shouldn't be a problem. */
3418 return ((g_getenv("KDEDIR") != NULL
) || g_getenv("KDEDIRS") != NULL
);
3425 purple_running_osx(void)
3427 #if defined(__APPLE__)
3434 typedef union purple_sockaddr
{
3436 struct sockaddr_in sa_in
;
3437 #if defined(AF_INET6)
3438 struct sockaddr_in6 sa_in6
;
3440 struct sockaddr_storage sa_stor
;
3444 purple_fd_get_ip(int fd
)
3446 PurpleSockaddr addr
;
3447 socklen_t namelen
= sizeof(addr
);
3450 g_return_val_if_fail(fd
!= 0, NULL
);
3452 if (getsockname(fd
, &(addr
.sa
), &namelen
))
3455 family
= addr
.sa
.sa_family
;
3457 if (family
== AF_INET
) {
3458 return g_strdup(inet_ntoa(addr
.sa_in
.sin_addr
));
3460 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
3461 else if (family
== AF_INET6
) {
3462 char host
[INET6_ADDRSTRLEN
];
3465 tmp
= inet_ntop(family
, &(addr
.sa_in6
.sin6_addr
), host
, sizeof(host
));
3466 return g_strdup(tmp
);
3474 purple_socket_get_family(int fd
)
3476 PurpleSockaddr addr
;
3477 socklen_t len
= sizeof(addr
);
3479 g_return_val_if_fail(fd
>= 0, -1);
3481 if (getsockname(fd
, &(addr
.sa
), &len
))
3484 return addr
.sa
.sa_family
;
3488 purple_socket_speaks_ipv4(int fd
)
3492 g_return_val_if_fail(fd
>= 0, FALSE
);
3494 family
= purple_socket_get_family(fd
);
3499 #if defined(IPV6_V6ONLY)
3503 socklen_t len
= sizeof(val
);
3505 if (getsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &val
, &len
) != 0)
3515 /**************************************************************************
3517 **************************************************************************/
3519 purple_strequal(const gchar
*left
, const gchar
*right
)
3521 return (g_strcmp0(left
, right
) == 0);
3525 purple_normalize(const PurpleAccount
*account
, const char *str
)
3527 const char *ret
= NULL
;
3528 static char buf
[BUF_LEN
];
3530 /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */
3531 g_return_val_if_fail(str
!= NULL
, "");
3533 if (account
!= NULL
)
3535 PurpleProtocol
*protocol
=
3536 purple_protocols_find(purple_account_get_protocol_id(account
));
3538 if (protocol
!= NULL
)
3539 ret
= purple_protocol_client_iface_normalize(protocol
, account
, str
);
3546 tmp
= g_utf8_normalize(str
, -1, G_NORMALIZE_DEFAULT
);
3547 g_snprintf(buf
, sizeof(buf
), "%s", tmp
);
3557 * You probably don't want to call this directly, it is
3558 * mainly for use as a protocol callback function. See the
3559 * comments in util.h.
3562 purple_normalize_nocase(const PurpleAccount
*account
, const char *str
)
3564 static char buf
[BUF_LEN
];
3567 g_return_val_if_fail(str
!= NULL
, NULL
);
3569 tmp1
= g_utf8_strdown(str
, -1);
3570 tmp2
= g_utf8_normalize(tmp1
, -1, G_NORMALIZE_DEFAULT
);
3571 g_snprintf(buf
, sizeof(buf
), "%s", tmp2
? tmp2
: "");
3579 purple_validate(const PurpleProtocol
*protocol
, const char *str
)
3581 const char *normalized
;
3583 g_return_val_if_fail(protocol
!= NULL
, FALSE
);
3584 g_return_val_if_fail(str
!= NULL
, FALSE
);
3589 if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol
, CLIENT_IFACE
, normalize
))
3592 normalized
= purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol
),
3595 return (NULL
!= normalized
);
3599 purple_strdup_withhtml(const gchar
*src
)
3601 gulong destsize
, i
, j
;
3604 g_return_val_if_fail(src
!= NULL
, NULL
);
3606 /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */
3608 for (i
= 0; src
[i
] != '\0'; i
++)
3612 else if (src
[i
] != '\r')
3616 dest
= g_malloc(destsize
);
3618 /* Copy stuff, ignoring \r's, because they are dumb */
3619 for (i
= 0, j
= 0; src
[i
] != '\0'; i
++) {
3620 if (src
[i
] == '\n') {
3621 strcpy(&dest
[j
], "<BR>");
3623 } else if (src
[i
] != '\r')
3627 dest
[destsize
-1] = '\0';
3633 purple_str_has_prefix(const char *s
, const char *p
)
3635 return g_str_has_prefix(s
, p
);
3639 purple_str_has_caseprefix(const gchar
*s
, const gchar
*p
)
3641 g_return_val_if_fail(s
, FALSE
);
3642 g_return_val_if_fail(p
, FALSE
);
3644 return (g_ascii_strncasecmp(s
, p
, strlen(p
)) == 0);
3648 purple_str_has_suffix(const char *s
, const char *x
)
3650 return g_str_has_suffix(s
, x
);
3654 purple_str_add_cr(const char *text
)
3660 g_return_val_if_fail(text
!= NULL
, NULL
);
3662 if (text
[0] == '\n')
3664 for (i
= 1; i
< strlen(text
); i
++)
3665 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3669 return g_strdup(text
);
3671 ret
= g_malloc0(strlen(text
) + count
+ 1);
3674 if (text
[i
] == '\n')
3676 ret
[j
++] = text
[i
++];
3677 for (; i
< strlen(text
); i
++) {
3678 if (text
[i
] == '\n' && text
[i
- 1] != '\r')
3687 purple_str_strip_char(char *text
, char thechar
)
3691 g_return_if_fail(text
!= NULL
);
3693 for (i
= 0, j
= 0; text
[i
]; i
++)
3694 if (text
[i
] != thechar
)
3695 text
[j
++] = text
[i
];
3701 purple_util_chrreplace(char *string
, char delimiter
,
3706 g_return_if_fail(string
!= NULL
);
3708 while (string
[i
] != '\0')
3710 if (string
[i
] == delimiter
)
3711 string
[i
] = replacement
;
3717 purple_strreplace(const char *string
, const char *delimiter
,
3718 const char *replacement
)
3723 g_return_val_if_fail(string
!= NULL
, NULL
);
3724 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3725 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3727 split
= g_strsplit(string
, delimiter
, 0);
3728 ret
= g_strjoinv(replacement
, split
);
3735 purple_strcasereplace(const char *string
, const char *delimiter
,
3736 const char *replacement
)
3739 int length_del
, length_rep
, i
, j
;
3741 g_return_val_if_fail(string
!= NULL
, NULL
);
3742 g_return_val_if_fail(delimiter
!= NULL
, NULL
);
3743 g_return_val_if_fail(replacement
!= NULL
, NULL
);
3745 length_del
= strlen(delimiter
);
3746 length_rep
= strlen(replacement
);
3748 /* Count how many times the delimiter appears */
3749 i
= 0; /* position in the source string */
3750 j
= 0; /* number of occurrences of "delimiter" */
3751 while (string
[i
] != '\0') {
3752 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3761 ret
= g_malloc(j
+1);
3763 i
= 0; /* position in the source string */
3764 j
= 0; /* position in the destination string */
3765 while (string
[i
] != '\0') {
3766 if (!g_ascii_strncasecmp(&string
[i
], delimiter
, length_del
)) {
3767 strncpy(&ret
[j
], replacement
, length_rep
);
3782 /** TODO: Expose this when we can add API */
3784 purple_strcasestr_len(const char *haystack
, gssize hlen
, const char *needle
, gssize nlen
)
3786 const char *tmp
, *ret
;
3788 g_return_val_if_fail(haystack
!= NULL
, NULL
);
3789 g_return_val_if_fail(needle
!= NULL
, NULL
);
3792 hlen
= strlen(haystack
);
3794 nlen
= strlen(needle
);
3798 g_return_val_if_fail(hlen
> 0, NULL
);
3799 g_return_val_if_fail(nlen
> 0, NULL
);
3801 while (*tmp
&& !ret
&& (hlen
- (tmp
- haystack
)) >= nlen
) {
3802 if (!g_ascii_strncasecmp(needle
, tmp
, nlen
))
3812 purple_strcasestr(const char *haystack
, const char *needle
)
3814 return purple_strcasestr_len(haystack
, -1, needle
, -1);
3818 purple_str_size_to_units(goffset size
)
3820 static const char * const size_str
[] = { "bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
3822 gsize size_index
= 0;
3825 return g_strdup(_("Calculating..."));
3827 else if (size
== 0) {
3828 return g_strdup(_("Unknown."));
3831 size_mag
= (float)size
;
3833 while ((size_index
< G_N_ELEMENTS(size_str
) - 1) && (size_mag
> 1024)) {
3838 if (size_index
== 0) {
3839 return g_strdup_printf("%" G_GOFFSET_FORMAT
" %s", size
, _(size_str
[size_index
]));
3841 return g_strdup_printf("%.2f %s", size_mag
, _(size_str
[size_index
]));
3847 purple_str_seconds_to_string(guint secs
)
3850 guint days
, hrs
, mins
;
3854 return g_strdup_printf(dngettext(PACKAGE
, "%d second", "%d seconds", secs
), secs
);
3857 days
= secs
/ (60 * 60 * 24);
3858 secs
= secs
% (60 * 60 * 24);
3859 hrs
= secs
/ (60 * 60);
3860 secs
= secs
% (60 * 60);
3862 /* secs = secs % 60; */
3866 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d day", "%d days", days
), days
);
3873 char *tmp
= g_strdup_printf(
3874 dngettext(PACKAGE
, "%s, %d hour", "%s, %d hours", hrs
),
3880 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d hour", "%d hours", hrs
), hrs
);
3887 char *tmp
= g_strdup_printf(
3888 dngettext(PACKAGE
, "%s, %d minute", "%s, %d minutes", mins
),
3894 ret
= g_strdup_printf(dngettext(PACKAGE
, "%d minute", "%d minutes", mins
), mins
);
3902 purple_str_binary_to_ascii(const unsigned char *binary
, guint len
)
3907 g_return_val_if_fail(len
> 0, NULL
);
3909 ret
= g_string_sized_new(len
);
3911 for (i
= 0; i
< len
; i
++)
3912 if (binary
[i
] < 32 || binary
[i
] > 126)
3913 g_string_append_printf(ret
, "\\x%02x", binary
[i
] & 0xFF);
3914 else if (binary
[i
] == '\\')
3915 g_string_append(ret
, "\\\\");
3917 g_string_append_c(ret
, binary
[i
]);
3919 return g_string_free(ret
, FALSE
);
3923 purple_utf16_size(const gunichar2
*str
)
3925 /* UTF16 cannot contain two consequent NUL bytes starting at even
3926 * position - see Unicode standards Chapter 3.9 D91 or RFC2781
3932 g_return_val_if_fail(str
!= NULL
, 0);
3936 return i
* sizeof(gunichar2
);
3940 purple_str_wipe(gchar
*str
)
3944 memset(str
, 0, strlen(str
));
3949 purple_utf16_wipe(gunichar2
*str
)
3953 memset(str
, 0, purple_utf16_size(str
));
3957 /**************************************************************************
3959 **************************************************************************/
3961 void purple_got_protocol_handler_uri(const char *uri
)
3965 const char *tmp
, *param_string
;
3967 GHashTable
*params
= NULL
;
3969 if (!(tmp
= strchr(uri
, ':')) || tmp
== uri
) {
3970 purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n");
3974 len
= MIN(sizeof(proto
) - 1, (gsize
)(tmp
- uri
));
3976 strncpy(proto
, uri
, len
);
3981 if (g_str_equal(proto
, "xmpp"))
3986 purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp
, proto
, delimiter
);
3988 if ((param_string
= strchr(tmp
, '?'))) {
3989 const char *keyend
= NULL
, *pairstart
;
3990 char *key
, *value
= NULL
;
3992 cmd
= g_strndup(tmp
, (param_string
- tmp
));
3995 params
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
3996 pairstart
= tmp
= param_string
;
3998 while (*tmp
|| *pairstart
) {
3999 if (*tmp
== delimiter
|| !(*tmp
)) {
4000 /* If there is no explicit value */
4001 if (keyend
== NULL
) {
4004 /* without these brackets, clang won't
4005 * recognize tmp as a non-NULL
4008 if (keyend
&& keyend
!= pairstart
) {
4010 key
= g_strndup(pairstart
, (keyend
- pairstart
));
4011 /* If there is an explicit value */
4012 if (keyend
!= tmp
&& keyend
!= (tmp
- 1))
4013 value
= g_strndup(keyend
+ 1, (tmp
- keyend
- 1));
4014 for (p
= key
; *p
; ++p
)
4015 *p
= g_ascii_tolower(*p
);
4016 g_hash_table_insert(params
, key
, value
);
4018 keyend
= value
= NULL
;
4019 pairstart
= (*tmp
) ? tmp
+ 1 : tmp
;
4020 } else if (*tmp
== '=')
4027 cmd
= g_strdup(tmp
);
4029 purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto
, cmd
, params
);
4033 g_hash_table_destroy(params
);
4037 purple_url_decode(const char *str
)
4039 static char buf
[BUF_LEN
];
4044 g_return_val_if_fail(str
!= NULL
, NULL
);
4047 * XXX - This check could be removed and buf could be made
4048 * dynamically allocated, but this is easier.
4050 if (strlen(str
) >= BUF_LEN
)
4053 for (i
= 0; i
< strlen(str
); i
++) {
4058 strncpy(hex
, str
+ ++i
, 2);
4061 /* i is pointing to the start of the number */
4065 * Now it's at the end and at the start of the for loop
4066 * will be at the next character.
4068 buf
[j
++] = strtol(hex
, NULL
, 16);
4074 if (!g_utf8_validate(buf
, -1, (const char **)&bum
))
4081 purple_url_encode(const char *str
)
4084 static char buf
[BUF_LEN
];
4088 g_return_val_if_fail(str
!= NULL
, NULL
);
4089 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4092 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
4093 gunichar c
= g_utf8_get_char(iter
);
4094 /* If the character is an ASCII character and is alphanumeric
4095 * no need to escape */
4096 if (c
< 128 && (isalnum(c
) || c
== '-' || c
== '.' || c
== '_' || c
== '~')) {
4099 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4100 for (i
= 0; (int)i
< bytes
; i
++) {
4101 if (j
> (BUF_LEN
- 4))
4103 if (i
>= sizeof(utf_char
)) {
4104 g_warn_if_reached();
4107 sprintf(buf
+ j
, "%%%02X", utf_char
[i
] & 0xff);
4118 /* Originally lifted from
4119 * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html
4120 * ... and slightly modified to be a bit more rfc822 compliant
4121 * ... and modified a bit more to make domain checking rfc1035 compliant
4122 * with the exception permitted in rfc1101 for domains to start with digit
4123 * but not completely checking to avoid conflicts with IP addresses
4126 purple_email_is_valid(const char *address
)
4128 const char *c
, *domain
;
4129 static char *rfc822_specials
= "()<>@,;:\\\"[]";
4131 g_return_val_if_fail(address
!= NULL
, FALSE
);
4133 if (*address
== '.') return FALSE
;
4135 /* first we validate the name portion (name@domain) (rfc822)*/
4136 for (c
= address
; *c
; c
++) {
4137 if (*c
== '\"' && (c
== address
|| *(c
- 1) == '.' || *(c
- 1) == '\"')) {
4140 if (*c
++ && *c
< 127 && *c
!= '\n' && *c
!= '\r') continue;
4143 if (*c
== '\"') break;
4144 if (*c
< ' ' || *c
>= 127) return FALSE
;
4146 if (!*c
++) return FALSE
;
4147 if (*c
== '@') break;
4148 if (*c
!= '.') return FALSE
;
4151 if (*c
== '@') break;
4152 if (*c
<= ' ' || *c
>= 127) return FALSE
;
4153 if (strchr(rfc822_specials
, *c
)) return FALSE
;
4156 /* It's obviously not an email address if we didn't find an '@' above */
4157 if (*c
== '\0') return FALSE
;
4159 /* strictly we should return false if (*(c - 1) == '.') too, but I think
4160 * we should permit user.@domain type addresses - they do work :) */
4161 if (c
== address
) return FALSE
;
4163 /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */
4164 if (!*(domain
= ++c
)) return FALSE
;
4166 if (*c
== '.' && (c
== domain
|| *(c
- 1) == '.' || *(c
- 1) == '-'))
4168 if (*c
== '-' && (*(c
- 1) == '.' || *(c
- 1) == '@')) return FALSE
;
4169 if ((*c
< '0' && *c
!= '-' && *c
!= '.') || (*c
> '9' && *c
< 'A') ||
4170 (*c
> 'Z' && *c
< 'a') || (*c
> 'z')) return FALSE
;
4173 if (*(c
- 1) == '-') return FALSE
;
4175 return ((c
- domain
) > 3 ? TRUE
: FALSE
);
4179 purple_ipv4_address_is_valid(const char *ip
)
4181 int c
, o1
, o2
, o3
, o4
;
4184 g_return_val_if_fail(ip
!= NULL
, FALSE
);
4186 c
= sscanf(ip
, "%d.%d.%d.%d%c", &o1
, &o2
, &o3
, &o4
, &end
);
4187 if (c
!= 4 || o1
< 0 || o1
> 255 || o2
< 0 || o2
> 255 || o3
< 0 || o3
> 255 || o4
< 0 || o4
> 255)
4193 purple_ipv6_address_is_valid(const gchar
*ip
)
4196 gboolean double_colon
= FALSE
;
4200 g_return_val_if_fail(ip
!= NULL
, FALSE
);
4205 for (c
= ip
; *c
; ++c
) {
4206 if ((*c
>= '0' && *c
<= '9') ||
4207 (*c
>= 'a' && *c
<= 'f') ||
4208 (*c
>= 'A' && *c
<= 'F')) {
4210 /* Only four hex digits per chunk */
4213 } else if (*c
== ':') {
4214 /* The start of a new chunk */
4217 if (*(c
+ 1) == ':') {
4219 * '::' indicates a consecutive series of chunks full
4220 * of zeroes. There can be only one of these per address.
4224 double_colon
= TRUE
;
4231 * Either we saw a '::' and there were fewer than 8 chunks -or-
4232 * we didn't see a '::' and saw exactly 8 chunks.
4234 return (double_colon
&& chunks
< 8) || (!double_colon
&& chunks
== 8);
4238 purple_ip_address_is_valid(const char *ip
)
4240 return (purple_ipv4_address_is_valid(ip
) || purple_ipv6_address_is_valid(ip
));
4243 /* Stolen from gnome_uri_list_extract_uris */
4245 purple_uri_list_extract_uris(const gchar
*uri_list
)
4249 GList
*result
= NULL
;
4251 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
4255 /* We don't actually try to validate the URI according to RFC
4256 * 2396, or even check for allowed characters - we just ignore
4257 * comments and trim whitespace off the ends. We also
4258 * allow LF delimination as well as the specified CRLF.
4266 while (*q
&& (*q
!= '\n') && (*q
!= '\r'))
4271 while (q
> p
&& isspace(*q
))
4274 retval
= (gchar
*)g_malloc (q
- p
+ 2);
4275 strncpy (retval
, p
, q
- p
+ 1);
4276 retval
[q
- p
+ 1] = '\0';
4278 result
= g_list_prepend (result
, retval
);
4281 p
= strchr (p
, '\n');
4286 return g_list_reverse (result
);
4290 /* Stolen from gnome_uri_list_extract_filenames */
4292 purple_uri_list_extract_filenames(const gchar
*uri_list
)
4294 GList
*tmp_list
, *node
, *result
;
4296 g_return_val_if_fail (uri_list
!= NULL
, NULL
);
4298 result
= purple_uri_list_extract_uris(uri_list
);
4302 gchar
*s
= (gchar
*)tmp_list
->data
;
4305 tmp_list
= tmp_list
->next
;
4307 if (!strncmp (s
, "file:", 5)) {
4308 node
->data
= g_filename_from_uri (s
, NULL
, NULL
);
4309 /* not sure if this fallback is useful at all */
4310 if (!node
->data
) node
->data
= g_strdup (s
+5);
4312 result
= g_list_delete_link(result
, node
);
4319 /**************************************************************************
4320 * UTF8 String Functions
4321 **************************************************************************/
4323 purple_utf8_try_convert(const char *str
)
4328 g_return_val_if_fail(str
!= NULL
, NULL
);
4330 if (g_utf8_validate(str
, -1, NULL
)) {
4331 return g_strdup(str
);
4334 utf8
= g_locale_to_utf8(str
, -1, &converted
, NULL
, NULL
);
4338 utf8
= g_convert(str
, -1, "UTF-8", "ISO-8859-15", &converted
, NULL
, NULL
);
4339 if ((utf8
!= NULL
) && (converted
== strlen(str
)))
4347 #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
4348 || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0)
4350 purple_utf8_salvage(const char *str
)
4355 g_return_val_if_fail(str
!= NULL
, NULL
);
4357 workstr
= g_string_sized_new(strlen(str
));
4360 (void)g_utf8_validate(str
, -1, &end
);
4361 workstr
= g_string_append_len(workstr
, str
, end
- str
);
4366 workstr
= g_string_append_c(workstr
, '?');
4368 } while (!utf8_first(*str
));
4369 } while (*str
!= '\0');
4371 return g_string_free(workstr
, FALSE
);
4375 purple_utf8_strip_unprintables(const gchar
*str
)
4377 gchar
*workstr
, *iter
;
4381 /* Act like g_strdup */
4384 if (!g_utf8_validate(str
, -1, &bad
)) {
4385 purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; "
4386 "first bad character was %02x (%c)\n",
4388 g_return_val_if_reached(NULL
);
4391 workstr
= iter
= g_new(gchar
, strlen(str
) + 1);
4393 gunichar ch
= g_utf8_get_char(str
);
4394 gchar
*next
= g_utf8_next_char(str
);
4396 * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
4397 * [#x10000-#x10FFFF]
4399 if ((ch
== '\t' || ch
== '\n' || ch
== '\r') ||
4400 (ch
>= 0x20 && ch
<= 0xD7FF) ||
4401 (ch
>= 0xE000 && ch
<= 0xFFFD) ||
4402 (ch
>= 0x10000 && ch
<= 0x10FFFF)) {
4403 memcpy(iter
, str
, next
- str
);
4404 iter
+= (next
- str
);
4410 /* nul-terminate the new string */
4417 * This function is copied from g_strerror() but changed to use
4421 purple_gai_strerror(gint errnum
)
4423 static GPrivate msg_private
= G_PRIVATE_INIT(g_free
);
4425 int saved_errno
= errno
;
4427 const char *msg_locale
;
4429 msg_locale
= gai_strerror(errnum
);
4430 if (g_get_charset(NULL
))
4432 /* This string is already UTF-8--great! */
4433 errno
= saved_errno
;
4438 gchar
*msg_utf8
= g_locale_to_utf8(msg_locale
, -1, NULL
, NULL
, NULL
);
4441 /* Stick in the quark table so that we can return a static result */
4442 GQuark msg_quark
= g_quark_from_string(msg_utf8
);
4445 msg_utf8
= (gchar
*)g_quark_to_string(msg_quark
);
4446 errno
= saved_errno
;
4451 msg
= g_private_get(&msg_private
);
4455 msg
= g_new(gchar
, 64);
4456 g_private_set(&msg_private
, msg
);
4459 sprintf(msg
, "unknown error (%d)", errnum
);
4461 errno
= saved_errno
;
4466 purple_utf8_ncr_encode(const char *str
)
4470 g_return_val_if_fail(str
!= NULL
, NULL
);
4471 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4473 out
= g_string_new("");
4475 for(; *str
; str
= g_utf8_next_char(str
)) {
4476 gunichar wc
= g_utf8_get_char(str
);
4478 /* super simple check. hopefully not too wrong. */
4480 g_string_append_printf(out
, "&#%u;", (guint32
) wc
);
4482 g_string_append_unichar(out
, wc
);
4486 return g_string_free(out
, FALSE
);
4491 purple_utf8_ncr_decode(const char *str
)
4496 g_return_val_if_fail(str
!= NULL
, NULL
);
4497 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4500 out
= g_string_new("");
4502 while( (b
= strstr(buf
, "&#")) ) {
4506 /* append everything leading up to the &# */
4507 g_string_append_len(out
, buf
, b
-buf
);
4509 b
+= 2; /* skip past the &# */
4511 /* strtoul will treat 0x prefix as hex, but not just x */
4512 if(*b
== 'x' || *b
== 'X') {
4517 /* advances buf to the end of the ncr segment */
4518 wc
= (gunichar
) strtoul(b
, &buf
, base
);
4520 /* this mimics the previous impl of ncr_decode */
4522 g_string_append_unichar(out
, wc
);
4527 /* append whatever's left */
4528 g_string_append(out
, buf
);
4530 return g_string_free(out
, FALSE
);
4535 purple_utf8_strcasecmp(const char *a
, const char *b
)
4537 char *a_norm
= NULL
;
4538 char *b_norm
= NULL
;
4548 if(!g_utf8_validate(a
, -1, NULL
) || !g_utf8_validate(b
, -1, NULL
))
4550 purple_debug_error("purple_utf8_strcasecmp",
4551 "One or both parameters are invalid UTF8\n");
4555 a_norm
= g_utf8_casefold(a
, -1);
4556 b_norm
= g_utf8_casefold(b
, -1);
4557 ret
= g_utf8_collate(a_norm
, b_norm
);
4564 /* previously conversation::find_nick() */
4566 purple_utf8_has_word(const char *haystack
, const char *needle
)
4568 char *hay
, *pin
, *p
;
4569 const char *start
, *prev_char
;
4570 gunichar before
, after
;
4572 gboolean ret
= FALSE
;
4574 start
= hay
= g_utf8_strdown(haystack
, -1);
4576 pin
= g_utf8_strdown(needle
, -1);
4579 while ((p
= strstr(start
, pin
)) != NULL
) {
4580 prev_char
= g_utf8_find_prev_char(hay
, p
);
4583 before
= g_utf8_get_char(prev_char
);
4585 after
= g_utf8_get_char_validated(p
+ n
, - 1);
4588 /* The character before is a reasonable guess for a word boundary
4589 ("!g_unichar_isalnum()" is not a valid way to determine word
4590 boundaries, but it is the only reasonable thing to do here),
4591 and isn't the '&' from a "&" or some such entity*/
4592 (before
!= (gunichar
)-2 && !g_unichar_isalnum(before
) && *(p
- 1) != '&'))
4593 && after
!= (gunichar
)-2 && !g_unichar_isalnum(after
)) {
4607 purple_print_utf8_to_console(FILE *filestream
, char *message
)
4609 gchar
*message_conv
;
4610 GError
*error
= NULL
;
4612 /* Try to convert 'message' to user's locale */
4613 message_conv
= g_locale_from_utf8(message
, -1, NULL
, NULL
, &error
);
4614 if (message_conv
!= NULL
) {
4615 fputs(message_conv
, filestream
);
4616 g_free(message_conv
);
4620 /* use 'message' as a fallback */
4621 g_warning("%s\n", error
->message
);
4622 g_error_free(error
);
4623 fputs(message
, filestream
);
4627 gboolean
purple_message_meify(char *message
, gssize len
)
4630 gboolean inside_html
= FALSE
;
4632 g_return_val_if_fail(message
!= NULL
, FALSE
);
4635 len
= strlen(message
);
4637 for (c
= message
; *c
; c
++, len
--) {
4640 inside_html
= FALSE
;
4649 if(*c
&& !g_ascii_strncasecmp(c
, "/me ", 4)) {
4650 memmove(c
, c
+4, len
-3);
4657 char *purple_text_strip_mnemonic(const char *in
)
4664 g_return_val_if_fail(in
!= NULL
, NULL
);
4666 out
= g_malloc(strlen(in
)+1);
4670 a0
= a
; /* The last non-space char seen so far, or the first char */
4674 if(a
> out
&& b
> in
&& *(b
-1) == '(' && *(b
+1) && !(*(b
+1) & 0x80) && *(b
+2) == ')') {
4675 /* Detected CJK style shortcut (Bug 875311) */
4676 a
= a0
; /* undo the left parenthesis */
4677 b
+= 3; /* and skip the whole mess */
4678 } else if(*(b
+1) == '_') {
4685 /* We don't want to corrupt the middle of UTF-8 characters */
4686 } else if (!(*b
& 0x80)) { /* other 1-byte char */
4691 /* Multibyte utf8 char, don't look for _ inside these */
4694 if ((*b
& 0xe0) == 0xc0) {
4696 } else if ((*b
& 0xf0) == 0xe0) {
4698 } else if ((*b
& 0xf8) == 0xf0) {
4700 } else if ((*b
& 0xfc) == 0xf8) {
4702 } else if ((*b
& 0xfe) == 0xfc) {
4704 } else { /* Illegal utf8 */
4707 a0
= a
; /* unless we want to delete CJK spaces too */
4708 for (i
= 0; i
< n
&& *b
; i
+= 1) {
4718 const char* purple_unescape_filename(const char *escaped
) {
4719 return purple_url_decode(escaped
);
4723 /* this is almost identical to purple_url_encode (hence purple_url_decode
4724 * being used above), but we want to keep certain characters unescaped
4725 * for compat reasons */
4727 purple_escape_filename(const char *str
)
4730 static char buf
[BUF_LEN
];
4734 g_return_val_if_fail(str
!= NULL
, NULL
);
4735 g_return_val_if_fail(g_utf8_validate(str
, -1, NULL
), NULL
);
4738 for (; *iter
&& j
< (BUF_LEN
- 1) ; iter
= g_utf8_next_char(iter
)) {
4739 gunichar c
= g_utf8_get_char(iter
);
4740 /* If the character is an ASCII character and is alphanumeric,
4741 * or one of the specified values, no need to escape */
4742 if (c
< 128 && (g_ascii_isalnum(c
) || c
== '@' || c
== '-' ||
4743 c
== '_' || c
== '.' || c
== '#')) {
4746 int bytes
= g_unichar_to_utf8(c
, utf_char
);
4747 for (i
= 0; (int)i
< bytes
; i
++) {
4748 if (j
> (BUF_LEN
- 4))
4750 if (i
>= sizeof(utf_char
)) {
4751 g_warn_if_reached();
4754 sprintf(buf
+ j
, "%%%02x", utf_char
[i
] & 0xff);
4760 /* File/Directory names in windows cannot end in periods/spaces.
4761 * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
4763 while (j
> 0 && (buf
[j
- 1] == '.' || buf
[j
- 1] == ' '))
4771 gchar
* purple_escape_js(const gchar
*str
)
4775 json_node_set_string(escape_js_node
, str
);
4776 json_generator_set_root(escape_js_gen
, escape_js_node
);
4777 escaped
= json_generator_to_data(escape_js_gen
, NULL
);
4778 json_node_set_boolean(escape_js_node
, FALSE
);
4783 void purple_restore_default_signal_handlers(void)
4786 signal(SIGHUP
, SIG_DFL
); /* 1: terminal line hangup */
4787 signal(SIGINT
, SIG_DFL
); /* 2: interrupt program */
4788 signal(SIGQUIT
, SIG_DFL
); /* 3: quit program */
4789 signal(SIGILL
, SIG_DFL
); /* 4: illegal instruction (not reset when caught) */
4790 signal(SIGTRAP
, SIG_DFL
); /* 5: trace trap (not reset when caught) */
4791 signal(SIGABRT
, SIG_DFL
); /* 6: abort program */
4794 signal(SIGPOLL
, SIG_DFL
); /* 7: pollable event (POSIX) */
4795 #endif /* SIGPOLL */
4798 signal(SIGEMT
, SIG_DFL
); /* 7: EMT instruction (Non-POSIX) */
4801 signal(SIGFPE
, SIG_DFL
); /* 8: floating point exception */
4802 signal(SIGBUS
, SIG_DFL
); /* 10: bus error */
4803 signal(SIGSEGV
, SIG_DFL
); /* 11: segmentation violation */
4804 signal(SIGSYS
, SIG_DFL
); /* 12: bad argument to system call */
4805 signal(SIGPIPE
, SIG_DFL
); /* 13: write on a pipe with no reader */
4806 signal(SIGALRM
, SIG_DFL
); /* 14: real-time timer expired */
4807 signal(SIGTERM
, SIG_DFL
); /* 15: software termination signal */
4808 signal(SIGCHLD
, SIG_DFL
); /* 20: child status has changed */
4809 signal(SIGXCPU
, SIG_DFL
); /* 24: exceeded CPU time limit */
4810 signal(SIGXFSZ
, SIG_DFL
); /* 25: exceeded file size limit */
4811 #endif /* !_WIN32 */
4815 set_status_with_attrs(PurpleStatus
*status
, ...)
4818 va_start(args
, status
);
4819 purple_status_set_active_with_attrs(status
, TRUE
, args
);
4823 void purple_util_set_current_song(const char *title
, const char *artist
, const char *album
)
4825 GList
*list
= purple_accounts_get_all();
4826 for (; list
; list
= list
->next
) {
4827 PurplePresence
*presence
;
4829 PurpleAccount
*account
= list
->data
;
4830 if (!purple_account_get_enabled(account
, purple_core_get_ui()))
4833 presence
= purple_account_get_presence(account
);
4834 tune
= purple_presence_get_status(presence
, "tune");
4838 set_status_with_attrs(tune
,
4839 PURPLE_TUNE_TITLE
, title
,
4840 PURPLE_TUNE_ARTIST
, artist
,
4841 PURPLE_TUNE_ALBUM
, album
,
4844 purple_status_set_active(tune
, FALSE
);
4849 char * purple_util_format_song_info(const char *title
, const char *artist
, const char *album
, gpointer unused
)
4854 if (!title
|| !*title
)
4857 esc
= g_markup_escape_text(title
, -1);
4858 string
= g_string_new("");
4859 g_string_append_printf(string
, "%s", esc
);
4862 if (artist
&& *artist
) {
4863 esc
= g_markup_escape_text(artist
, -1);
4864 g_string_append_printf(string
, _(" - %s"), esc
);
4868 if (album
&& *album
) {
4869 esc
= g_markup_escape_text(album
, -1);
4870 g_string_append_printf(string
, _(" (%s)"), esc
);
4874 return g_string_free(string
, FALSE
);
4878 purple_get_host_name(void)
4880 return g_get_host_name();
4884 purple_uuid_random(void)
4888 tmp
= g_random_int();
4889 a
= 0x4000 | (tmp
& 0xFFF); /* 0x4000 to 0x4FFF */
4891 b
= ((1 << 3) << 12) | (tmp
& 0x3FFF); /* 0x8000 to 0xBFFF */
4893 tmp
= g_random_int();
4895 return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x",
4900 (tmp
>> 16) & 0xFFFF, g_random_int());
4903 void purple_callback_set_zero(gpointer data
)
4905 gpointer
*ptr
= data
;
4907 g_return_if_fail(ptr
!= NULL
);
4913 purple_value_new(GType type
)
4917 g_return_val_if_fail(type
!= G_TYPE_NONE
, NULL
);
4919 ret
= g_new0(GValue
, 1);
4920 g_value_init(ret
, type
);
4926 purple_value_dup(GValue
*value
)
4930 g_return_val_if_fail(value
!= NULL
, NULL
);
4932 ret
= g_new0(GValue
, 1);
4933 g_value_init(ret
, G_VALUE_TYPE(value
));
4934 g_value_copy(value
, ret
);
4940 purple_value_free(GValue
*value
)
4942 g_return_if_fail(value
!= NULL
);
4944 g_value_unset(value
);
4948 gchar
*purple_http_digest_calculate_session_key(
4949 const gchar
*algorithm
,
4950 const gchar
*username
,
4952 const gchar
*password
,
4954 const gchar
*client_nonce
)
4957 gchar hash
[33]; /* We only support MD5. */
4960 g_return_val_if_fail(username
!= NULL
, NULL
);
4961 g_return_val_if_fail(realm
!= NULL
, NULL
);
4962 g_return_val_if_fail(password
!= NULL
, NULL
);
4963 g_return_val_if_fail(nonce
!= NULL
, NULL
);
4965 /* Check for a supported algorithm. */
4966 g_return_val_if_fail(algorithm
== NULL
||
4967 *algorithm
== '\0' ||
4968 g_ascii_strcasecmp(algorithm
, "MD5") ||
4969 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
4971 hasher
= purple_md5_hash_new();
4972 g_return_val_if_fail(hash
!= NULL
, NULL
);
4974 purple_hash_append(hasher
, (guchar
*)username
, strlen(username
));
4975 purple_hash_append(hasher
, (guchar
*)":", 1);
4976 purple_hash_append(hasher
, (guchar
*)realm
, strlen(realm
));
4977 purple_hash_append(hasher
, (guchar
*)":", 1);
4978 purple_hash_append(hasher
, (guchar
*)password
, strlen(password
));
4980 if (algorithm
!= NULL
&& !g_ascii_strcasecmp(algorithm
, "MD5-sess"))
4984 if (client_nonce
== NULL
)
4986 g_object_unref(hasher
);
4987 purple_debug_error("hash", "Required client_nonce missing for MD5-sess digest calculation.\n");
4991 purple_hash_digest(hasher
, digest
, sizeof(digest
));
4993 purple_hash_reset(hasher
);
4994 purple_hash_append(hasher
, digest
, sizeof(digest
));
4995 purple_hash_append(hasher
, (guchar
*)":", 1);
4996 purple_hash_append(hasher
, (guchar
*)nonce
, strlen(nonce
));
4997 purple_hash_append(hasher
, (guchar
*)":", 1);
4998 purple_hash_append(hasher
, (guchar
*)client_nonce
, strlen(client_nonce
));
5001 digest_ok
= purple_hash_digest_to_str(hasher
, hash
, sizeof(hash
));
5002 g_object_unref(hasher
);
5004 g_return_val_if_fail(digest_ok
, NULL
);
5006 return g_strdup(hash
);
5009 gchar
*purple_http_digest_calculate_response(
5010 const gchar
*algorithm
,
5011 const gchar
*method
,
5012 const gchar
*digest_uri
,
5014 const gchar
*entity
,
5016 const gchar
*nonce_count
,
5017 const gchar
*client_nonce
,
5018 const gchar
*session_key
)
5021 static gchar hash2
[33]; /* We only support MD5. */
5024 g_return_val_if_fail(method
!= NULL
, NULL
);
5025 g_return_val_if_fail(digest_uri
!= NULL
, NULL
);
5026 g_return_val_if_fail(nonce
!= NULL
, NULL
);
5027 g_return_val_if_fail(session_key
!= NULL
, NULL
);
5029 /* Check for a supported algorithm. */
5030 g_return_val_if_fail(algorithm
== NULL
||
5031 *algorithm
== '\0' ||
5032 g_ascii_strcasecmp(algorithm
, "MD5") ||
5033 g_ascii_strcasecmp(algorithm
, "MD5-sess"), NULL
);
5035 /* Check for a supported "quality of protection". */
5036 g_return_val_if_fail(qop
== NULL
||
5038 g_ascii_strcasecmp(qop
, "auth") ||
5039 g_ascii_strcasecmp(qop
, "auth-int"), NULL
);
5041 hash
= purple_md5_hash_new();
5042 g_return_val_if_fail(hash
!= NULL
, NULL
);
5044 purple_hash_append(hash
, (guchar
*)method
, strlen(method
));
5045 purple_hash_append(hash
, (guchar
*)":", 1);
5046 purple_hash_append(hash
, (guchar
*)digest_uri
, strlen(digest_uri
));
5048 if (qop
!= NULL
&& !g_ascii_strcasecmp(qop
, "auth-int"))
5051 gchar entity_hash
[33];
5055 g_object_unref(hash
);
5056 purple_debug_error("hash", "Required entity missing for auth-int digest calculation.\n");
5060 hash2
= purple_md5_hash_new();
5061 purple_hash_append(hash2
, (guchar
*)entity
, strlen(entity
));
5062 digest_ok
= purple_hash_digest_to_str(hash2
, entity_hash
, sizeof(entity_hash
));
5063 g_object_unref(hash2
);
5066 g_object_unref(hash
);
5067 g_return_val_if_reached(NULL
);
5070 purple_hash_append(hash
, (guchar
*)":", 1);
5071 purple_hash_append(hash
, (guchar
*)entity_hash
, strlen(entity_hash
));
5074 digest_ok
= purple_hash_digest_to_str(hash
, hash2
, sizeof(hash2
));
5075 purple_hash_reset(hash
);
5078 g_object_unref(hash
);
5079 g_return_val_if_reached(NULL
);
5082 purple_hash_append(hash
, (guchar
*)session_key
, strlen(session_key
));
5083 purple_hash_append(hash
, (guchar
*)":", 1);
5084 purple_hash_append(hash
, (guchar
*)nonce
, strlen(nonce
));
5085 purple_hash_append(hash
, (guchar
*)":", 1);
5087 if (qop
!= NULL
&& *qop
!= '\0')
5089 if (nonce_count
== NULL
)
5091 g_object_unref(hash
);
5092 purple_debug_error("hash", "Required nonce_count missing for digest calculation.\n");
5096 if (client_nonce
== NULL
)
5098 g_object_unref(hash
);
5099 purple_debug_error("hash", "Required client_nonce missing for digest calculation.\n");
5103 purple_hash_append(hash
, (guchar
*)nonce_count
, strlen(nonce_count
));
5104 purple_hash_append(hash
, (guchar
*)":", 1);
5105 purple_hash_append(hash
, (guchar
*)client_nonce
, strlen(client_nonce
));
5106 purple_hash_append(hash
, (guchar
*)":", 1);
5108 purple_hash_append(hash
, (guchar
*)qop
, strlen(qop
));
5110 purple_hash_append(hash
, (guchar
*)":", 1);
5113 purple_hash_append(hash
, (guchar
*)hash2
, strlen(hash2
));
5114 digest_ok
= purple_hash_digest_to_str(hash
, hash2
, sizeof(hash2
));
5115 g_object_unref(hash
);
5117 g_return_val_if_fail(digest_ok
, NULL
);
5119 return g_strdup(hash2
);
5123 _purple_fstat(int fd
, GStatBuf
*st
)
5127 g_return_val_if_fail(st
!= NULL
, -1);
5130 ret
= _fstat(fd
, st
);
5132 ret
= fstat(fd
, st
);
5140 /* Temporarily removed - re-add this when you need ini file support. */
5142 #define PURPLE_KEY_FILE_DEFAULT_MAX_SIZE 102400
5143 #define PURPLE_KEY_FILE_HARD_LIMIT 10485760
5146 purple_key_file_load_from_ini(GKeyFile
*key_file
, const gchar
*file
,
5149 const gchar
*header
= "[default]\n\n";
5150 int header_len
= strlen(header
);
5153 gsize file_size
, buff_size
;
5155 GError
*error
= NULL
;
5157 g_return_val_if_fail(key_file
!= NULL
, FALSE
);
5158 g_return_val_if_fail(file
!= NULL
, FALSE
);
5159 g_return_val_if_fail(max_size
< PURPLE_KEY_FILE_HARD_LIMIT
, FALSE
);
5162 max_size
= PURPLE_KEY_FILE_DEFAULT_MAX_SIZE
;
5164 fd
= g_open(file
, O_RDONLY
, S_IREAD
);
5166 purple_debug_error("util", "Failed to read ini file %s", file
);
5170 if (_purple_fstat(fd
, &st
) != 0) {
5171 purple_debug_error("util", "Failed to fstat ini file %s", file
);
5175 file_size
= (st
.st_size
> max_size
) ? max_size
: st
.st_size
;
5177 buff_size
= file_size
+ header_len
;
5178 buff
= g_new(gchar
, buff_size
);
5179 memcpy(buff
, header
, header_len
);
5180 if (read(fd
, buff
+ header_len
, file_size
) != (gssize
)file_size
) {
5181 purple_debug_error("util",
5182 "Failed to read whole ini file %s", file
);
5189 g_key_file_load_from_data(key_file
, buff
, buff_size
,
5190 G_KEY_FILE_NONE
, &error
);
5195 purple_debug_error("util", "Failed parsing ini file %s: %s",
5196 file
, error
->message
);