/* Purple is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA #include "conversation.h" #include <json-glib/json-glib.h> static char *custom_user_dir = NULL; static char *user_dir = NULL; static gchar *cache_dir = NULL; static gchar *config_dir = NULL; static gchar *data_dir = NULL; static JsonNode *escape_js_node = NULL; static JsonGenerator *escape_js_gen = NULL; escape_js_node = json_node_new(JSON_NODE_VALUE); escape_js_gen = json_generator_new(); json_node_set_boolean(escape_js_node, FALSE); /* Free these so we don't have leaks at shutdown. */ json_node_free(escape_js_node); g_object_unref(escape_js_gen); /************************************************************************** **************************************************************************/ purple_base16_encode(const guchar *data, gsize len) g_return_val_if_fail(data != NULL, NULL); g_return_val_if_fail(len > 0, NULL); ascii = g_malloc(len * 2 + 1); for (i = 0; i < len; i++) g_snprintf(&ascii[i * 2], 3, "%02x", data[i] & 0xFF); purple_base16_decode(const char *str, gsize *ret_len) gsize len, i, accumulator = 0; g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(*str, 0); g_return_val_if_fail(len % 2 == 0, 0); data = g_malloc(len / 2); for (i = 0; i < len; i++) accumulator |= str[i] - 48; case 'a': accumulator |= 10; break; case 'b': accumulator |= 11; break; case 'c': accumulator |= 12; break; case 'd': accumulator |= 13; break; case 'e': accumulator |= 14; break; case 'f': accumulator |= 15; break; data[(i - 1) / 2] = accumulator; purple_base16_encode_chunked(const guchar *data, gsize len) g_return_val_if_fail(data != NULL, NULL); g_return_val_if_fail(len > 0, NULL); /* For each byte of input, we need 2 bytes for the hex representation * The final colon will be replaced by a terminating NULL ascii = g_malloc(len * 3 + 1); for (i = 0; i < len; i++) g_snprintf(&ascii[i * 3], 4, "%02x:", data[i] & 0xFF); /* Replace the final colon with NULL */ /************************************************************************** **************************************************************************/ purple_utf8_strftime(const char *format, const struct tm *tm) g_return_val_if_fail(format != NULL, NULL); dt = g_date_time_new_now_local(); dt = g_date_time_new_local(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, utf8 = g_date_time_format(dt, format); purple_debug_error("util", "purple_utf8_strftime(): Formatting failed\n"); g_strlcpy(buf, utf8, sizeof(buf)); purple_date_format_short(const struct tm *tm) return purple_utf8_strftime("%x", tm); purple_date_format_long(const struct tm *tm) * This string determines how some dates are displayed. The default * string "%x %X" shows the date then the time. Translators can * change this to "%X %x" if they want the time to be shown first, return purple_utf8_strftime(_("%x %X"), tm); purple_date_format_full(const struct tm *tm) return purple_utf8_strftime("%c", tm); purple_time_format(const struct tm *tm) return purple_utf8_strftime("%X", tm); purple_time_build(int year, int month, int day, int hour, int min, int sec) tm.tm_year = year - 1900; tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; /* originally taken from GLib trunk 1-6-11 */ /* originally licensed as LGPL 2+ */ mktime_utc(struct tm *tm) static const gint days_before[] = 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 if (tm->tm_mon < 0 || tm->tm_mon > 11) retval = (tm->tm_year - 70) * 365; retval += (tm->tm_year - 68) / 4; retval += days_before[tm->tm_mon] + tm->tm_mday - 1; if (tm->tm_year % 4 == 0 && tm->tm_mon < 2) retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec; #endif /* !HAVE_TIMEGM */ purple_str_to_time(const char *timestamp, gboolean utc, struct tm *tm, long *tz_off, const char **rest) long tzoff = PURPLE_NO_TZ_OFF; gboolean mktime_with_utc = FALSE; g_return_val_if_fail(timestamp != NULL, 0); memset(&t, 0, sizeof(struct tm)); /* Strip leading whitespace */ while (g_ascii_isspace(*str)) if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') { if (rest != NULL && *str != '\0') if (sscanf(str, "%04d", &year) && year >= 1900) { if (*str == '-' || *str == '/') if (!sscanf(str, "%02d", &t.tm_mon)) { if (rest != NULL && *str != '\0') if (*str == '-' || *str == '/') if (!sscanf(str, "%02d", &t.tm_mday)) { if (rest != NULL && *str != '\0') /* Grab the year off the end if there's still stuff */ if (*str == '/' || *str == '-') { /* But make sure we don't read the year twice */ if (rest != NULL && *str != '\0') if (!sscanf(str, "%04d", &t.tm_year)) { if (rest != NULL && *str != '\0') } else if (*str == 'T' || *str == '.') { /* Continue grabbing the hours/minutes/seconds */ if ((sscanf(str, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && (sscanf(str, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 && gint sign, tzhrs, tzmins; /* Cut off those pesky micro-seconds */ } while (*str >= '0' && *str <= '9'); sign = (*str == '+') ? 1 : -1; /* Process the timezone */ if (*str == '+' || *str == '-') { if (((sscanf(str, "%02d:%02d", &tzhrs, &tzmins) == 2 && (str += 5)) || (sscanf(str, "%02d%02d", &tzhrs, &tzmins) == 2 && (str += 4)))) tzoff = tzhrs * 60 * 60 + tzmins * 60; } else if (*str == 'Z') { /* No timezone specified. */ if (rest != NULL && *str != '\0') { /* Strip trailing whitespace */ while (g_ascii_isspace(*str)) if (tzoff != PURPLE_NO_TZ_OFF) purple_str_to_date_time(const char *timestamp, gboolean utc) g_return_val_if_fail(timestamp != NULL, NULL); /* Strip leading whitespace */ while (g_ascii_isspace(*str)) if (!g_ascii_isdigit(*str) && *str != '-' && *str != '+') { if (sscanf(str, "%04d", &year) && year > 0) { if (*str == '-' || *str == '/') if (!sscanf(str, "%02d", &month)) { if (*str == '-' || *str == '/') if (!sscanf(str, "%02d", &day)) { /* Grab the year off the end if there's still stuff */ if (*str == '/' || *str == '-') { /* But make sure we don't read the year twice */ if (!sscanf(str, "%04d", &year)) { } else if (*str == 'T' || *str == '.') { /* Continue grabbing the hours/minutes/seconds */ if ((sscanf(str, "%02d:%02d:%02d", &hour, &minute, &seconds) == 3 && (sscanf(str, "%02d%02d%02d", &hour, &minute, &seconds) == 3 && if (sscanf(str, "%d%n", µseconds, &chars) == 1) { if (*end == '+' || *end == '-') { while (isdigit(*end) || *end == ':') { /* Trim anything trailing a purely numeric time zone. */ gchar *tzstr = g_strndup(str, end - str); tz = g_time_zone_new(tzstr); /* Just try whatever is there. */ tz = g_time_zone_new(str); /* No timezone specified. */ tz = g_time_zone_new_utc(); tz = g_time_zone_new_local(); retval = g_date_time_new(tz, year, month, day, hour, minute, seconds + microseconds * pow(10, -chars)); purple_uts35_to_str(const char *format, size_t len, struct tm *tm) string = g_string_sized_new(len); while ((i + count) < len && format[i] == format[i+count]) g_string_append(string, purple_utf8_strftime("%y", tm)); g_string_append_printf(string, "%0*d", /* Year (in "Week of Year" based calendars) */ /* Stand-alone Quarter */ g_string_append(string, purple_utf8_strftime("%m", tm)); g_string_append(string, purple_utf8_strftime("%b", tm)); g_string_append(string, purple_utf8_strftime("%B", tm)); g_string_append_len(string, purple_utf8_strftime("%b", tm), 1); g_string_append(string, purple_utf8_strftime("%W", tm)); g_string_append(string, purple_utf8_strftime("%d", tm)); g_string_append(string, purple_utf8_strftime("%j", tm)); /* Day of Year in Month */ /* Modified Julian Day */ g_string_append(string, purple_utf8_strftime("%a", tm)); g_string_append(string, purple_utf8_strftime("%A", tm)); g_string_append_len(string, purple_utf8_strftime("%a", tm), 1); g_string_append(string, purple_utf8_strftime("%u", tm)); g_string_append(string, purple_utf8_strftime("%a", tm)); g_string_append(string, purple_utf8_strftime("%A", tm)); g_string_append_len(string, purple_utf8_strftime("%a", tm), 1); /* Stand-alone Local Day of Week */ g_string_append(string, purple_utf8_strftime("%u", tm)); g_string_append(string, purple_utf8_strftime("%a", tm)); g_string_append(string, purple_utf8_strftime("%A", tm)); g_string_append_len(string, purple_utf8_strftime("%a", tm), 1); g_string_append(string, purple_utf8_strftime("%p", tm)); g_string_append(string, purple_utf8_strftime("%I", tm)); g_string_append(string, purple_utf8_strftime("%I", tm)); g_string_append(string, purple_utf8_strftime("%H", tm)); g_string_append(string, purple_utf8_strftime("%H", tm)); /* Hour (hHkK by locale) */ g_string_append(string, purple_utf8_strftime("%M", tm)); g_string_append(string, purple_utf8_strftime("%S", tm)); /* Fractional Sub-second */ /* Time Zone (specific non-location format) */ g_string_append(string, purple_utf8_strftime("%z", tm)); g_string_append(string, purple_utf8_strftime("%z", tm)); /* Time Zone (generic non-location format) */ g_string_append(string, purple_utf8_strftime("%Z", tm)); g_string_append(string, purple_utf8_strftime("%Z", tm)); /* Generic Location Format) */ g_string_append(string, purple_utf8_strftime("%Z", tm)); g_string_append_len(string, format + i, count); return g_string_free(string, FALSE); /************************************************************************** **************************************************************************/ * This function is stolen from glib's gmarkup.c and modified to not static void append_escaped_text(GString *str, const gchar *text, gssize length) next = g_utf8_next_char (p); g_string_append (str, "&"); g_string_append (str, "<"); g_string_append (str, ">"); g_string_append (str, """); if ((0x1 <= c && c <= 0x8) || (0xb <= c && c <= 0xc) || (0xe <= c && c <= 0x1f) || (0x7f <= c && c <= 0x84) || (0x86 <= c && c <= 0x9f)) g_string_append_printf (str, "&#x%x;", c); g_string_append_len (str, p, next - p); /* This function is stolen from glib's gmarkup.c */ gchar *purple_markup_escape_text(const gchar *text, gssize length) g_return_val_if_fail(text != NULL, NULL); /* prealloc at least as long as original text */ str = g_string_sized_new(length); append_escaped_text(str, text, length); return g_string_free(str, FALSE); purple_markup_unescape_entity(const char *text, int *length) if (!text || *text != '&') #define IS_ENTITY(s) (!g_ascii_strncasecmp(text, s, (len = sizeof(s) - 1))) else if(IS_ENTITY("<")) else if(IS_ENTITY(">")) else if(IS_ENTITY(" ")) else if(IS_ENTITY("©")) pln = "\302\251"; /* or use g_unichar_to_utf8(0xa9); */ else if(IS_ENTITY(""")) else if(IS_ENTITY("®")) pln = "\302\256"; /* or use g_unichar_to_utf8(0xae); */ else if(IS_ENTITY("'")) else if(text[1] == '#' && (g_ascii_isxdigit(text[2]) || text[2] == 'x')) { const char *start = text + 2; pound = g_ascii_strtoull(start, &end, base); if (pound == 0 || pound > INT_MAX || *end != ';') { buflen = g_unichar_to_utf8((gunichar)pound, buf); purple_markup_get_css_property(const gchar *style, const gchar *css_str = style; const gchar *css_value_start; const gchar *css_value_end; g_return_val_if_fail(opt != NULL, NULL); /* find the CSS property */ /* skip whitespace characters */ while (*css_str && g_ascii_isspace(*css_str)) if (!g_ascii_isalpha(*css_str)) if (g_ascii_strncasecmp(css_str, opt, strlen(opt))) /* go to next css property positioned after the next ';' */ while (*css_str && *css_str != '"' && *css_str != ';') /* find the CSS value position in the string */ while (*css_str && g_ascii_isspace(*css_str)) while (*css_str && g_ascii_isspace(*css_str)) if (*css_str == '\0' || *css_str == '"' || *css_str == ';') css_value_start = css_str; while (*css_str && *css_str != '"' && *css_str != ';') css_value_end = css_str - 1; /* Removes trailing whitespace */ while (css_value_end > css_value_start && g_ascii_isspace(*css_value_end)) tmp = g_strndup(css_value_start, css_value_end - css_value_start + 1); ret = purple_unescape_html(tmp); gboolean purple_markup_is_rtl(const char *html) const gchar *start, *end; if (purple_markup_find_tag("span", html, &start, &end, &attributes)) /* tmp is a member of attributes and is free with g_datalist_clear call */ const char *tmp = g_datalist_get_data(&attributes, "dir"); if (tmp && !g_ascii_strcasecmp(tmp, "RTL")) tmp = g_datalist_get_data(&attributes, "style"); char *tmp2 = purple_markup_get_css_property(tmp, "direction"); if (tmp2 && !g_ascii_strcasecmp(tmp2, "RTL")) g_datalist_clear(&attributes); purple_markup_find_tag(const char *needle, const char *haystack, const char **start, const char **end, GData **attributes) const char *cur = haystack; gboolean in_attr = FALSE; const char *in_quotes = NULL; g_return_val_if_fail( needle != NULL, FALSE); g_return_val_if_fail( *needle != '\0', FALSE); g_return_val_if_fail( haystack != NULL, FALSE); g_return_val_if_fail( start != NULL, FALSE); g_return_val_if_fail( end != NULL, FALSE); g_return_val_if_fail(attributes != NULL, FALSE); needlelen = strlen(needle); g_datalist_init(&attribs); while (*close && *close != *in_quotes) /* if we got the close quote, store the value and carry on from * * after it. if we ran to the end of the string, point to the NULL * * and we're outta here */ /* only store a value if we have an attribute name */ size_t len = close - cur; char *val = g_strndup(cur, len); g_datalist_set_data_full(&attribs, name, val, g_free); while (*close && *close != '>' && *close != '"' && *close != '\'' && *close != ' ' && *close != '=') /* if we got the equals, store the name of the attribute. if we got * the quote, save the attribute and go straight to quote mode. * otherwise the tag closed or we reached the end of the string, * so we can get outta here */ size_t len = close - cur; /* don't store a blank attribute name */ name = g_ascii_strdown(cur, len); /* swallow extra spaces inside tag */ while (*cur && *cur == ' ') cur++; /* if we hit a < followed by the name of our tag... */ if (*cur == '<' && !g_ascii_strncasecmp(cur + 1, needle, needlelen)) { cur = cur + needlelen + 1; /* if we're pointing at a space or a >, we found the right tag. if * * we're not, we've found a longer tag, so we need to skip to the * * >, but not being distracted by >s inside quotes. */ if (*cur == ' ' || *cur == '>') { while (*cur && *cur != '"' && *cur != '\'' && *cur != '>') { while (*cur && *cur != '"') } else if (*cur == '\'') { while (*cur && *cur != '\'') /* clean up any attribute name from a premature termination */ purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info, const char *start_token, int skip, const char *end_token, char check_value, const char *no_value_token, const char *display_name, gboolean is_link, PurpleInfoFieldFormatCallback format_cb) g_return_val_if_fail(str != NULL, FALSE); g_return_val_if_fail(user_info != NULL, FALSE); g_return_val_if_fail(start_token != NULL, FALSE); g_return_val_if_fail(end_token != NULL, FALSE); g_return_val_if_fail(display_name != NULL, FALSE); p = strstr(str, start_token); p += strlen(start_token) + skip; if (check_value != '\0' && *p == check_value) q = strstr(p, end_token); /* Trim leading blanks */ while (*p != '\n' && g_ascii_isspace(*p)) { /* Trim trailing blanks */ while (q > p && g_ascii_isspace(*(q - 1))) { /* Don't bother with null strings */ if (q != NULL && (!no_value_token || strncmp(p, no_value_token, strlen(no_value_token)))) { GString *dest = g_string_new(""); g_string_append(dest, "<a href=\""); g_string_append(dest, link_prefix); char *reformatted = format_cb(p, q - p); g_string_append(dest, reformatted); g_string_append_len(dest, p, q - p); g_string_append(dest, "\">"); g_string_append(dest, link_prefix); g_string_append_len(dest, p, q - p); g_string_append(dest, "</a>"); char *reformatted = format_cb(p, q - p); g_string_append(dest, reformatted); g_string_append_len(dest, p, q - p); purple_notify_user_info_add_pair_html(user_info, display_name, dest->str); g_string_free(dest, TRUE); struct purple_parse_tag { /* NOTE: Do not put `do {} while(0)` around this macro (as this is the method recommended in the GCC docs). It contains 'continue's that should affect the while-loop in purple_markup_html_to_xhtml and doing the Also, remember to put braces in constructs that require them for multiple statements when using this macro. */ #define ALLOW_TAG_ALT(x, y) if(!g_ascii_strncasecmp(c, "<" x " ", strlen("<" x " "))) { \ const char *o = c + strlen("<" x); \ const char *p = NULL, *q = NULL, *r = NULL; \ /* o = iterating over full tag \ * q = start of quoted bit \ GString *innards = g_string_new(""); \ if(!q && (*o == '\"' || *o == '\'') ) { \ if(*o == *q) { /* end of quoted bit */ \ char *unescaped = g_strndup(q+1, o-q-1); \ char *escaped = g_markup_escape_text(unescaped, -1); \ g_string_append_printf(innards, "%c%s%c", *q, escaped, *q); \ } else if(*c == '\\') { \ innards = g_string_append_c(innards, *o); \ if(p && !r) { /* got an end of tag and no other < earlier */\ struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \ tags = g_list_prepend(tags, pt); \ xhtml = g_string_append(xhtml, "<" y); \ xhtml = g_string_append(xhtml, innards->str); \ xhtml = g_string_append_c(xhtml, '>'); \ } else { /* got end of tag with earlier < *or* didn't get anything */ \ xhtml = g_string_append(xhtml, "<"); \ plain = g_string_append_c(plain, '<'); \ g_string_free(innards, TRUE); \ if(!g_ascii_strncasecmp(c, "<" x, strlen("<" x)) && \ (*(c+strlen("<" x)) == '>' || \ !g_ascii_strncasecmp(c+strlen("<" x), "/>", 2))) { \ xhtml = g_string_append(xhtml, "<" y); \ struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); \ tags = g_list_prepend(tags, pt); \ xhtml = g_string_append_c(xhtml, '>'); \ xhtml = g_string_append(xhtml, "/>");\ c = strchr(c, '>') + 1; \ /* Don't forget to check the note above for ALLOW_TAG_ALT. */ #define ALLOW_TAG(x) ALLOW_TAG_ALT(x, x) purple_markup_html_to_xhtml(const char *html, char **xhtml_out, GList *tags = NULL, *tag; #define CHECK_QUOTE(ptr) if (*(ptr) == '\'' || *(ptr) == '\"') \ #define VALID_CHAR(ptr) (*(ptr) && *(ptr) != quote && (quote || (*(ptr) != ' ' && *(ptr) != '>'))) g_return_if_fail(xhtml_out != NULL || plain_out != NULL); xhtml = g_string_new(""); plain = g_string_new(""); if(*(c+1) == '/') { /* closing tag */ struct purple_parse_tag *pt = tag->data; if(!g_ascii_strncasecmp((c+2), pt->src_tag, strlen(pt->src_tag)) && *(c+strlen(pt->src_tag)+2) == '>') { c += strlen(pt->src_tag) + 3; struct purple_parse_tag *pt = tags->data; g_string_append_printf(xhtml, "</%s>", pt->dest_tag); if(plain && purple_strequal(pt->src_tag, "a")) { /* if this is a link, we have to add the url to the plaintext, too */ (!g_string_equal(cdata, url) && (g_ascii_strncasecmp(url->str, "mailto:", 7) != 0 || g_utf8_collate(url->str + 7, cdata->str) != 0))) g_string_append_printf(plain, " <%s>", g_strstrip(purple_unescape_html(url->str))); g_string_free(cdata, TRUE); tags = g_list_delete_link(tags, tags); tags = g_list_delete_link(tags, tag); /* a closing tag we weren't expecting... * we'll let it slide, if it's really a tag...if it's * just a </ we'll escape it properly */ while(*end && g_ascii_isalpha(*end)) xhtml = g_string_append(xhtml, "<"); plain = g_string_append_c(plain, '<'); } else { /* opening tag */ /* we only allow html to start the message */ ALLOW_TAG_ALT("i", "em"); ALLOW_TAG_ALT("italic", "em"); /* we skip <HR> because it's not legal in XHTML-IM. However, * we still want to send something sensible, so we put a * linebreak in its place. <BR> also needs special handling * because putting a </BR> to close it would just be dumb. */ if((!g_ascii_strncasecmp(c, "<br", 3) || !g_ascii_strncasecmp(c, "<hr", 3)) !g_ascii_strncasecmp(c+3, "/>", 2) || !g_ascii_strncasecmp(c+3, " />", 3))) { xhtml = g_string_append(xhtml, "<br/>"); plain = g_string_append_c(plain, '\n'); if(!g_ascii_strncasecmp(c, "<b>", 3) || !g_ascii_strncasecmp(c, "<bold>", strlen("<bold>")) || !g_ascii_strncasecmp(c, "<strong>", strlen("<strong>"))) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); tags = g_list_prepend(tags, pt); xhtml = g_string_append(xhtml, "<span style='font-weight: bold;'>"); if(!g_ascii_strncasecmp(c, "<u>", 3) || !g_ascii_strncasecmp(c, "<underline>", strlen("<underline>"))) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); pt->src_tag = *(c+2) == '>' ? "u" : "underline"; tags = g_list_prepend(tags, pt); xhtml = g_string_append(xhtml, "<span style='text-decoration: underline;'>"); if(!g_ascii_strncasecmp(c, "<s>", 3) || !g_ascii_strncasecmp(c, "<strike>", strlen("<strike>"))) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); pt->src_tag = *(c+2) == '>' ? "s" : "strike"; tags = g_list_prepend(tags, pt); xhtml = g_string_append(xhtml, "<span style='text-decoration: line-through;'>"); if(!g_ascii_strncasecmp(c, "<sub>", 5)) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); tags = g_list_prepend(tags, pt); xhtml = g_string_append(xhtml, "<span style='vertical-align:sub;'>"); if(!g_ascii_strncasecmp(c, "<sup>", 5)) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); tags = g_list_prepend(tags, pt); xhtml = g_string_append(xhtml, "<span style='vertical-align:super;'>"); if (!g_ascii_strncasecmp(c, "<img", 4) && (*(c+4) == '>' || *(c+4) == ' ')) { GString *src = NULL, *alt = NULL; #define ESCAPE(from, to) \ while (VALID_CHAR(from)) { \ if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \ to = g_string_append(to, "&"); \ else if (*from == '\'') \ to = g_string_append(to, "'"); \ to = g_string_append_c(to, *from); \ while (*p && *p != '>') { if (!g_ascii_strncasecmp(p, "src=", 4)) { g_string_free(src, TRUE); } else if (!g_ascii_strncasecmp(p, "alt=", 4)) { g_string_free(alt, TRUE); if ((c = strchr(p, '>')) != NULL) /* src and alt are required! */ g_string_append_printf(xhtml, "<img src='%s' alt='%s' />", g_strstrip(src->str), alt ? alt->str : ""); plain = g_string_append(plain, purple_unescape_html(alt->str)); xhtml = g_string_append(xhtml, alt->str); g_string_free(alt, TRUE); g_string_free(src, TRUE); if (!g_ascii_strncasecmp(c, "<a", 2) && (*(c+2) == '>' || *(c+2) == ' ')) { struct purple_parse_tag *pt; while (*p && *p != '>') { if (!g_ascii_strncasecmp(p, "href=", 5)) { g_string_free(url, TRUE); g_string_free(cdata, TRUE); cdata = g_string_new(""); if ((*q == '&') && (purple_markup_unescape_entity(q, &len) == NULL)) url = g_string_append(url, "&"); url = g_string_append(url, """); url = g_string_append_c(url, *q); if ((c = strchr(p, '>')) != NULL) pt = g_new0(struct purple_parse_tag, 1); tags = g_list_prepend(tags, pt); g_string_append_printf(xhtml, "<a href=\"%s\">", url ? g_strstrip(url->str) : ""); #define ESCAPE(from, to) \ while (VALID_CHAR(from)) { \ if ((*from == '&') && (purple_markup_unescape_entity(from, &len) == NULL)) \ to = g_string_append(to, "&"); \ else if (*from == '\'') \ to = g_string_append_c(to, '\"'); \ to = g_string_append_c(to, *from); \ if(!g_ascii_strncasecmp(c, "<font", 5) && (*(c+5) == '>' || *(c+5) == ' ')) { GString *style = g_string_new(""); struct purple_parse_tag *pt; while (*p && *p != '>') { if (!g_ascii_strncasecmp(p, "back=", 5)) { GString *color = g_string_new(""); g_string_append_printf(style, "background: %s; ", color->str); g_string_free(color, TRUE); } else if (!g_ascii_strncasecmp(p, "color=", 6)) { GString *color = g_string_new(""); g_string_append_printf(style, "color: %s; ", color->str); g_string_free(color, TRUE); } else if (!g_ascii_strncasecmp(p, "face=", 5)) { GString *face = g_string_new(""); g_string_append_printf(style, "font-family: %s; ", g_strstrip(face->str)); g_string_free(face, TRUE); } else if (!g_ascii_strncasecmp(p, "size=", 5)) { const char *size = "medium"; g_string_append_printf(style, "font-size: %s; ", size); if ((c = strchr(p, '>')) != NULL) pt = g_new0(struct purple_parse_tag, 1); tags = g_list_prepend(tags, pt); g_string_append_printf(xhtml, "<span style='%s'>", g_strstrip(style->str)); g_string_free(style, TRUE); if (!g_ascii_strncasecmp(c, "<body ", 6)) { gboolean did_something = FALSE; while (*p && *p != '>') { if (!g_ascii_strncasecmp(p, "bgcolor=", 8)) { struct purple_parse_tag *pt = g_new0(struct purple_parse_tag, 1); GString *color = g_string_new(""); color = g_string_append_c(color, *q); g_string_append_printf(xhtml, "<span style='background: %s;'>", g_strstrip(color->str)); g_string_free(color, TRUE); if ((c = strchr(p, '>')) != NULL) tags = g_list_prepend(tags, pt); if (did_something) continue; /* this has to come after the special case for bgcolor */ if(!g_ascii_strncasecmp(c, "<!--", strlen("<!--"))) { char *p = strstr(c + strlen("<!--"), "-->"); xhtml = g_string_append(xhtml, "<!--"); xhtml = g_string_append(xhtml, "<"); plain = g_string_append_c(plain, '<'); if ((pln = purple_markup_unescape_entity(c, &len)) == NULL) { g_snprintf(buf, sizeof(buf), "%c", *c); xhtml = g_string_append_len(xhtml, c, len); plain = g_string_append(plain, pln); cdata = g_string_append_len(cdata, c, len); xhtml = g_string_append_c(xhtml, *c); plain = g_string_append_c(plain, *c); cdata = g_string_append_c(cdata, *c); for (tag = tags; tag ; tag = tag->next) { struct purple_parse_tag *pt = tag->data; g_string_append_printf(xhtml, "</%s>", pt->dest_tag); *xhtml_out = g_string_free(xhtml, FALSE); *plain_out = g_string_free(plain, FALSE); g_string_free(url, TRUE); g_string_free(cdata, TRUE); /* The following are probably reasonable changes: * - \n should be converted to a normal space * - in addition to <br>, <p> and <div> etc. should also be converted into \n * - We want to turn </td>#whitespace<td> sequences into a single tab * - We want to turn </tr>#whitespace<tr> sequences into a single \n * - <script>...</script> and <style>...</style> should be completely removed purple_markup_strip_html(const char *str) gboolean closing_td_p = FALSE; const gchar *cdata_close_tag = NULL, *ent; for (i = 0, j = 0; str2[i]; i++) /* Note: Don't even assume any other tag is a tag in CDATA */ if (g_ascii_strncasecmp(str2 + i, cdata_close_tag, strlen(cdata_close_tag)) == 0) i += strlen(cdata_close_tag) - 1; else if (g_ascii_strncasecmp(str2 + i, "<td", 3) == 0 && closing_td_p) else if (g_ascii_strncasecmp(str2 + i, "</td>", 5) == 0) if(g_ascii_isspace(str2[k])) /* Scan until we end the tag either implicitly (closed start * tag) or explicitly, using a sloppy method (i.e., < or > * inside quoted attributes will screw us up) while (str2[k] && str2[k] != '<' && str2[k] != '>') /* If we've got an <a> tag with an href, save the address if (g_ascii_strncasecmp(str2 + i, "<a", 2) == 0 && g_ascii_isspace(str2[i+2])) int st; /* start of href, inclusive [ */ int end; /* end of href, exclusive ) */ for (st = i + 3; st < k; st++) if (g_ascii_strncasecmp(str2+st, "href=", 5) == 0) if (str2[st] == '"' || str2[st] == '\'') /* find end of address */ for (end = st; end < k && str2[end] != delim; end++) /* All the work is done in the loop construct above. */ /* If there's an address, save it. If there was * already one saved, kill it. */ tmp = g_strndup(str2 + st, end - st); href = purple_unescape_html(tmp); /* Replace </a> with an ascii representation of the * address the link was pointing to. */ else if (href != NULL && g_ascii_strncasecmp(str2 + i, "</a>", 4) == 0) size_t hrlen = strlen(href); /* Only insert the href if it's different from the CDATA. */ if ((hrlen != (gsize)(j - href_st) || strncmp(str2 + href_st, href, hrlen)) && (hrlen != (gsize)(j - href_st + 7) || /* 7 == strlen("http://") */ strncmp(str2 + href_st, href + 7, hrlen - 7))) memmove(str2 + j, href, hrlen); /* Check for tags which should be mapped to newline (but ignore some of * the tags at the beginning of the text) */ else if ((j && (g_ascii_strncasecmp(str2 + i, "<p>", 3) == 0 || g_ascii_strncasecmp(str2 + i, "<tr", 3) == 0 || g_ascii_strncasecmp(str2 + i, "<hr", 3) == 0 || g_ascii_strncasecmp(str2 + i, "<li", 3) == 0 || g_ascii_strncasecmp(str2 + i, "<div", 4) == 0)) || g_ascii_strncasecmp(str2 + i, "<br", 3) == 0 || g_ascii_strncasecmp(str2 + i, "</table>", 8) == 0) /* Check for tags which begin CDATA and need to be closed */ else if (g_ascii_strncasecmp(str2 + i, "<script", 7) == 0) cdata_close_tag = "</script>"; else if (g_ascii_strncasecmp(str2 + i, "<style", 6) == 0) cdata_close_tag = "</style>"; /* Update the index and continue checking after the tag */ i = (str2[k] == '<' || str2[k] == '\0')? k - 1: k; else if (cdata_close_tag) else if (!g_ascii_isspace(str2[i])) if (str2[i] == '&' && (ent = purple_markup_unescape_entity(str2 + i, &entlen)) != NULL) str2[j++] = g_ascii_isspace(str2[i])? ' ': str2[i]; if (!g_ascii_strncasecmp(c, "<", 4) || !g_ascii_strncasecmp(c, ">", 4) || !g_ascii_strncasecmp(c, """, 6)) { process_link(GString *ret, const char *start, const char *c, char *url_buf, *tmpurlbuf; if (!badchar(*t) && !badentity(t)) if (*t == ',' && *(t + 1) != ' ') { if (t > start && *(t - 1) == '.') if (t > start && *(t - 1) == ')' && inside_paren > 0) url_buf = g_strndup(c, t - c); tmpurlbuf = purple_unescape_html(url_buf); g_string_append_printf(ret, "<A HREF=\"%s%s\">%s</A>", purple_markup_linkify(const char *text) const char *c, *t, *q = NULL; char *tmpurlbuf, *url_buf; gboolean inside_html = FALSE; if(*c == '(' && !inside_html) { ret = g_string_append_c(ret, *c); } else if(!q && (*c == '\"' || *c == '\'')) { if (!g_ascii_strncasecmp(c, "<A", 2)) { if (!g_ascii_strncasecmp(c, "/A>", 3)) { ret = g_string_append_c(ret, *c); } else if (!g_ascii_strncasecmp(c, "http://", 7)) { c = process_link(ret, text, c, 7, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "https://", 8)) { c = process_link(ret, text, c, 8, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "ftp://", 6)) { c = process_link(ret, text, c, 6, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "sftp://", 7)) { c = process_link(ret, text, c, 7, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "file://", 7)) { c = process_link(ret, text, c, 7, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "www.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) { c = process_link(ret, text, c, 4, "http://", inside_paren); } else if (!g_ascii_strncasecmp(c, "ftp.", 4) && c[4] != '.' && (c == text || badchar(c[-1]) || badentity(c-1))) { c = process_link(ret, text, c, 4, "ftp://", inside_paren); } else if (!g_ascii_strncasecmp(c, "xmpp:", 5) && (c == text || badchar(c[-1]) || badentity(c-1))) { c = process_link(ret, text, c, 5, "", inside_paren); } else if (!g_ascii_strncasecmp(c, "mailto:", 7)) { if (badchar(*t) || badentity(t)) { if (t > text && *(t - 1) == '.') if ((d = strstr(c + 7, "?")) != NULL && d < t) url_buf = g_strndup(c + 7, d - c - 7); url_buf = g_strndup(c + 7, t - c - 7); if (!purple_email_is_valid(url_buf)) { url_buf = g_strndup(c, t - c); tmpurlbuf = purple_unescape_html(url_buf); g_string_append_printf(ret, "<A HREF=\"%s\">%s</A>", } else if (c != text && (*c == '@')) { GString *gurl_buf = NULL; const char illegal_chars[] = "!@#$%^&*()[]{}/|\\<>\":;\r\n \0"; if (strchr(illegal_chars,*(c - 1)) || strchr(illegal_chars, *(c + 1))) gurl_buf = g_string_new(""); /* iterate backwards grabbing the local part of an email address */ if (badchar(*t) || (g >= 127) || (*t == '(') || ((*t == ';') && ((t > (text+2) && (!g_ascii_strncasecmp(t - 3, "<", 4) || !g_ascii_strncasecmp(t - 3, ">", 4))) || (t > (text+4) && (!g_ascii_strncasecmp(t - 5, """, 6)))))) { /* local part will already be part of ret, strip it out */ ret = g_string_truncate(ret, ret->len - (c - t)); ret = g_string_append_unichar(ret, g); g_string_prepend_unichar(gurl_buf, g); t = g_utf8_find_prev_char(text, t); ret = g_string_assign(ret, ""); t = g_utf8_find_next_char(c, NULL); /* iterate forwards grabbing the domain part of an email address */ if (badchar(*t) || (g >= 127) || (*t == ')') || badentity(t)) { url_buf = g_string_free(gurl_buf, FALSE); /* strip off trailing periods */ for (d = url_buf + strlen(url_buf) - 1; *d == '.'; d--, t--) tmpurlbuf = purple_unescape_html(url_buf); if (purple_email_is_valid(tmpurlbuf)) { g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>", g_string_append(ret, url_buf); g_string_append_unichar(gurl_buf, g); t = g_utf8_find_next_char(t, NULL); g_string_free(gurl_buf, TRUE); if(*c == ')' && !inside_html) { ret = g_string_append_c(ret, *c); ret = g_string_append_c(ret, *c); return g_string_free(ret, FALSE); char *purple_unescape_text(const char *in) if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) { g_string_append(ret, ent); g_string_append_c(ret, *c); return g_string_free(ret, FALSE); char *purple_unescape_html(const char *html) if ((ent = purple_markup_unescape_entity(c, &len)) != NULL) { g_string_append(ret, ent); } else if (!strncmp(c, "<br>", 4)) { g_string_append_c(ret, '\n'); g_string_append_c(ret, *c); return g_string_free(ret, FALSE); purple_markup_slice(const char *str, guint x, guint y) gboolean appended = FALSE; g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(x <= y, NULL); while (*str && (z < y)) { c = g_utf8_get_char(str); char *end = strchr(str, '>'); g_string_free(ret, TRUE); while ((tag = g_queue_pop_head(q))) if (!g_ascii_strncasecmp(str, "<img ", 5)) { } else if (!g_ascii_strncasecmp(str, "<br", 3)) { } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) { } else if (!g_ascii_strncasecmp(str, "</", 2)) { tmp = g_queue_pop_head(q); /* push it unto the stack */ tmp = g_strndup(str, end - str + 1); g_queue_push_head(q, tmp); g_string_append_len(ret, str, end - str + 1); char *end = strchr(str, ';'); g_string_free(ret, TRUE); while ((tag = g_queue_pop_head(q))) g_string_append_len(ret, str, end - str + 1); if (z == x && z > 0 && !appended) { g_string_append(ret, tag); g_string_append_unichar(ret, c); str = g_utf8_next_char(str); while ((tag = g_queue_pop_head(q))) { name = purple_markup_get_tag_name(tag); g_string_append_printf(ret, "</%s>", name); return g_string_free(ret, FALSE); purple_markup_get_tag_name(const char *tag) g_return_val_if_fail(tag != NULL, NULL); g_return_val_if_fail(*tag == '<', NULL); if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/') return g_strndup(tag+1, i-1); /************************************************************************** * Path/Filename Functions **************************************************************************/ return wpurple_home_dir(); /* Returns the argument passed to -c IFF it was present, or ~/.purple. */ if (custom_user_dir != NULL) user_dir = g_build_filename(purple_home_dir(), ".purple", NULL); purple_xdg_dir(gchar **xdg_dir, const gchar *xdg_base_dir, const gchar *xdg_type) *xdg_dir = g_build_filename(xdg_base_dir, "purple", NULL); *xdg_dir = g_build_filename(custom_user_dir, xdg_type, NULL); return purple_xdg_dir(&cache_dir, g_get_user_cache_dir(), "cache"); return purple_xdg_dir(&config_dir, g_get_user_config_dir(), "config"); return purple_xdg_dir(&data_dir, g_get_user_data_dir(), "data"); purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path) gboolean xdg_path_exists; /* Create destination directory */ mkdir_res = purple_build_dir(purple_xdg_dir, S_IRWXU); purple_debug_error("util", "Error creating xdg directory %s: %s; failed migration\n", purple_xdg_dir, g_strerror(errno)); xdg_path = g_build_filename(purple_xdg_dir, path, NULL); xdg_path_exists = g_file_test(xdg_path, G_FILE_TEST_EXISTS); gboolean old_path_exists; G_GNUC_BEGIN_IGNORE_DEPRECATIONS old_path = g_build_filename(purple_user_dir(), path, NULL); G_GNUC_END_IGNORE_DEPRECATIONS old_path_exists = g_file_test(old_path, G_FILE_TEST_EXISTS); rename_res = g_rename(old_path, xdg_path); purple_debug_error("util", "Error renaming %s to %s; failed migration\n", void purple_util_set_user_dir(const char *dir) custom_user_dir = g_strdup(dir); int purple_build_dir(const char *path, int mode) return g_mkdir_with_parents(path, mode); purple_util_write_data_to_file_common(const char *dir, const char *filename, const char *data, gssize size) g_return_val_if_fail(dir != NULL, FALSE); purple_debug_misc("util", "Writing file %s to directory %s", /* Ensure the directory exists */ if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) if (g_mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) == -1) purple_debug_error("util", "Error creating directory %s: %s\n", filename_full = g_build_filename(dir, filename, NULL); ret = purple_util_write_data_to_file_absolute(filename_full, data, size); purple_util_write_data_to_file(const char *filename, const char *data, gssize size) G_GNUC_BEGIN_IGNORE_DEPRECATIONS const char *user_dir = purple_user_dir(); G_GNUC_END_IGNORE_DEPRECATIONS gboolean ret = purple_util_write_data_to_file_common(user_dir, filename, data, size); purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size) const char *cache_dir = purple_cache_dir(); gboolean ret = purple_util_write_data_to_file_common(cache_dir, filename, data, size); purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size) const char *config_dir = purple_config_dir(); gboolean ret = purple_util_write_data_to_file_common(config_dir, filename, data, size); purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size) const char *data_dir = purple_data_dir(); gboolean ret = purple_util_write_data_to_file_common(data_dir, filename, data, size); purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size) g_return_val_if_fail(size >= -1, FALSE); file = g_file_new_for_path(filename_full); if (!g_file_replace_contents(file, data, size, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, NULL, &err)) { purple_debug_error("util", "Error writing file: %s: %s\n", filename_full, err->message); purple_util_read_xml_from_file(const char *filename, const char *description) G_GNUC_BEGIN_IGNORE_DEPRECATIONS return purple_xmlnode_from_file(purple_user_dir(), filename, description, "util"); G_GNUC_END_IGNORE_DEPRECATIONS purple_util_read_xml_from_cache_file(const char *filename, const char *description) return purple_xmlnode_from_file(purple_cache_dir(), filename, description, "util"); purple_util_read_xml_from_config_file(const char *filename, const char *description) return purple_xmlnode_from_file(purple_config_dir(), filename, description, "util"); purple_util_read_xml_from_data_file(const char *filename, const char *description) return purple_xmlnode_from_file(purple_data_dir(), filename, description, "util"); * Like mkstemp() but returns a file pointer, uses a pre-set template, * uses the semantics of tempnam() for the directory to use and allocates * the space for the filepath. * Caller is responsible for closing the file and removing it when done, * as well as freeing the space pointed-to by "path" with g_free(). * Returns NULL on failure and cleans up after itself if so. static const char *purple_mkstemp_templ = {"purpleXXXXXX"}; purple_mkstemp(char **fpath, gboolean binary) g_return_val_if_fail(fpath != NULL, NULL); if((tmpdir = (gchar*)g_get_tmp_dir()) != NULL) { if((*fpath = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", tmpdir, purple_mkstemp_templ)) != NULL) { purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp", "Couldn't make \"%s\", error: %d\n", if((fp = fdopen(fd, "r+")) == NULL) { purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp", "Couldn't fdopen(), error: %d\n", errno); purple_debug(PURPLE_DEBUG_ERROR, "purple_mkstemp", "g_get_tmp_dir() failed!\n"); purple_program_is_valid(const char *program) gboolean is_valid = FALSE; g_return_val_if_fail(program != NULL, FALSE); g_return_val_if_fail(*program != '\0', FALSE); if (!g_shell_parse_argv(program, NULL, &argv, &error)) { purple_debug(PURPLE_DEBUG_ERROR, "program_is_valid", "Could not parse program '%s': %s\n", program, error->message); progname = g_find_program_in_path(argv[0]); is_valid = (progname != NULL); if(purple_debug_is_verbose()) purple_debug_info("program_is_valid", "Tested program %s. %s.\n", program, is_valid ? "Valid" : "Invalid"); purple_running_gnome(void) gchar *tmp = g_find_program_in_path("gvfs-open"); tmp = g_find_program_in_path("gnome-open"); tmp = (gchar *)g_getenv("GNOME_DESKTOP_SESSION_ID"); return ((tmp != NULL) && (*tmp != '\0')); gchar *tmp = g_find_program_in_path("kfmclient"); session = g_getenv("KDE_FULL_SESSION"); if (purple_strequal(session, "true")) /* If you run Purple from Konsole under !KDE, this will provide a * a false positive. Since we do the GNOME checks first, this is * only a problem if you're running something !(KDE || GNOME) and * you run Purple from Konsole. This really shouldn't be a problem. */ return ((g_getenv("KDEDIR") != NULL) || g_getenv("KDEDIRS") != NULL); typedef union purple_sockaddr { struct sockaddr_in sa_in; struct sockaddr_in6 sa_in6; struct sockaddr_storage sa_stor; socklen_t namelen = sizeof(addr); g_return_val_if_fail(fd != 0, NULL); if (getsockname(fd, &(addr.sa), &namelen)) family = addr.sa.sa_family; return g_strdup(inet_ntoa(addr.sa_in.sin_addr)); #if defined(AF_INET6) && defined(HAVE_INET_NTOP) else if (family == AF_INET6) { char host[INET6_ADDRSTRLEN]; tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host)); purple_socket_get_family(int fd) socklen_t len = sizeof(addr); g_return_val_if_fail(fd >= 0, -1); if (getsockname(fd, &(addr.sa), &len)) return addr.sa.sa_family; purple_socket_speaks_ipv4(int fd) g_return_val_if_fail(fd >= 0, FALSE); family = purple_socket_get_family(fd); socklen_t len = sizeof(val); if (getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &len) != 0) /************************************************************************** **************************************************************************/ purple_normalize(PurpleAccount *account, const char *str) static char buf[BUF_LEN]; /* This should prevent a crash if purple_normalize gets called with NULL str, see #10115 */ g_return_val_if_fail(str != NULL, ""); PurpleProtocol *protocol = purple_protocols_find(purple_account_get_protocol_id(account)); ret = purple_protocol_client_iface_normalize(protocol, account, str); tmp = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT); g_snprintf(buf, sizeof(buf), "%s", tmp); * You probably don't want to call this directly, it is * mainly for use as a protocol callback function. See the purple_normalize_nocase(const PurpleAccount *account, const char *str) static char buf[BUF_LEN]; g_return_val_if_fail(str != NULL, NULL); tmp1 = g_utf8_strdown(str, -1); tmp2 = g_utf8_normalize(tmp1, -1, G_NORMALIZE_DEFAULT); g_snprintf(buf, sizeof(buf), "%s", tmp2 ? tmp2 : ""); purple_validate(const PurpleProtocol *protocol, const char *str) g_return_val_if_fail(protocol != NULL, FALSE); g_return_val_if_fail(str != NULL, FALSE); if (!PURPLE_PROTOCOL_IMPLEMENTS(protocol, CLIENT, normalize)) normalized = purple_protocol_client_iface_normalize(PURPLE_PROTOCOL(protocol), return (NULL != normalized); purple_strdup_withhtml(const gchar *src) g_return_val_if_fail(src != NULL, NULL); /* New length is (length of src) + (number of \n's * 3) - (number of \r's) + 1 */ for (i = 0; src[i] != '\0'; i++) dest = g_malloc(destsize); /* Copy stuff, ignoring \r's, because they are dumb */ for (i = 0, j = 0; src[i] != '\0'; i++) { strcpy(&dest[j], "<BR>"); } else if (src[i] != '\r') purple_str_has_prefix(const char *s, const char *p) return g_str_has_prefix(s, p); purple_str_has_caseprefix(const gchar *s, const gchar *p) g_return_val_if_fail(s, FALSE); g_return_val_if_fail(p, FALSE); return (g_ascii_strncasecmp(s, p, strlen(p)) == 0); purple_str_has_suffix(const char *s, const char *x) return g_str_has_suffix(s, x); purple_str_add_cr(const char *text) g_return_val_if_fail(text != NULL, NULL); for (i = 1; i < strlen(text); i++) if (text[i] == '\n' && text[i - 1] != '\r') ret = g_malloc0(strlen(text) + count + 1); for (; i < strlen(text); i++) { if (text[i] == '\n' && text[i - 1] != '\r') purple_str_strip_char(char *text, char thechar) g_return_if_fail(text != NULL); for (i = 0, j = 0; text[i]; i++) purple_util_chrreplace(char *string, char delimiter, g_return_if_fail(string != NULL); while (string[i] != '\0') if (string[i] == delimiter) purple_strreplace(const char *string, const char *delimiter, g_return_val_if_fail(string != NULL, NULL); g_return_val_if_fail(delimiter != NULL, NULL); g_return_val_if_fail(replacement != NULL, NULL); split = g_strsplit(string, delimiter, 0); ret = g_strjoinv(replacement, split); purple_strcasereplace(const char *string, const char *delimiter, int length_del, length_rep, i, j; g_return_val_if_fail(string != NULL, NULL); g_return_val_if_fail(delimiter != NULL, NULL); g_return_val_if_fail(replacement != NULL, NULL); length_del = strlen(delimiter); length_rep = strlen(replacement); /* Count how many times the delimiter appears */ i = 0; /* position in the source string */ j = 0; /* number of occurrences of "delimiter" */ while (string[i] != '\0') { if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) { i = 0; /* position in the source string */ j = 0; /* position in the destination string */ while (string[i] != '\0') { if (!g_ascii_strncasecmp(&string[i], delimiter, length_del)) { strncpy(&ret[j], replacement, length_rep); /** TODO: Expose this when we can add API */ purple_strcasestr_len(const char *haystack, gssize hlen, const char *needle, gssize nlen) g_return_val_if_fail(haystack != NULL, NULL); g_return_val_if_fail(needle != NULL, NULL); g_return_val_if_fail(hlen > 0, NULL); g_return_val_if_fail(nlen > 0, NULL); while (*tmp && !ret && (hlen - (tmp - haystack)) >= nlen) { if (!g_ascii_strncasecmp(needle, tmp, nlen)) purple_strcasestr(const char *haystack, const char *needle) return purple_strcasestr_len(haystack, -1, needle, -1); purple_str_seconds_to_string(guint secs) return g_strdup_printf(dngettext(PACKAGE, "%d second", "%d seconds", secs), secs); days = secs / (60 * 60 * 24); secs = secs % (60 * 60 * 24); ret = g_strdup_printf(dngettext(PACKAGE, "%d day", "%d days", days), days); char *tmp = g_strdup_printf( dngettext(PACKAGE, "%s, %d hour", "%s, %d hours", hrs), ret = g_strdup_printf(dngettext(PACKAGE, "%d hour", "%d hours", hrs), hrs); char *tmp = g_strdup_printf( dngettext(PACKAGE, "%s, %d minute", "%s, %d minutes", mins), ret = g_strdup_printf(dngettext(PACKAGE, "%d minute", "%d minutes", mins), mins); purple_str_binary_to_ascii(const unsigned char *binary, guint len) g_return_val_if_fail(len > 0, NULL); ret = g_string_sized_new(len); for (i = 0; i < len; i++) if (binary[i] < 32 || binary[i] > 126) g_string_append_printf(ret, "\\x%02x", binary[i] & 0xFF); else if (binary[i] == '\\') g_string_append(ret, "\\\\"); g_string_append_c(ret, binary[i]); return g_string_free(ret, FALSE); purple_utf16_size(const gunichar2 *str) /* UTF16 cannot contain two consequent NUL bytes starting at even * position - see Unicode standards Chapter 3.9 D91 or RFC2781 g_return_val_if_fail(str != NULL, 0); return i * sizeof(gunichar2); purple_str_wipe(gchar *str) memset(str, 0, strlen(str)); purple_utf16_wipe(gunichar2 *str) memset(str, 0, purple_utf16_size(str)); /************************************************************************** **************************************************************************/ void purple_got_protocol_handler_uri(const char *uri) const char *tmp, *param_string; GHashTable *params = NULL; if (!(tmp = strchr(uri, ':')) || tmp == uri) { purple_debug_error("util", "Malformed protocol handler message - missing protocol.\n"); len = MIN(sizeof(proto) - 1, (gsize)(tmp - uri)); strncpy(proto, uri, len); if (purple_strequal(proto, "xmpp")) purple_debug_info("util", "Processing message '%s' for protocol '%s' using delimiter '%c'.\n", tmp, proto, delimiter); if ((param_string = strchr(tmp, '?'))) { const char *keyend = NULL, *pairstart; char *key, *value = NULL; cmd = g_strndup(tmp, (param_string - tmp)); params = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); pairstart = tmp = param_string; while (*tmp || *pairstart) { if (*tmp == delimiter || !(*tmp)) { /* If there is no explicit value */ /* without these brackets, clang won't * recognize tmp as a non-NULL if (keyend && keyend != pairstart) { key = g_strndup(pairstart, (keyend - pairstart)); /* If there is an explicit value */ if (keyend != tmp && keyend != (tmp - 1)) value = g_strndup(keyend + 1, (tmp - keyend - 1)); *p = g_ascii_tolower(*p); g_hash_table_insert(params, key, value); pairstart = (*tmp) ? tmp + 1 : tmp; purple_signal_emit_return_1(purple_get_core(), "uri-handler", proto, cmd, params); g_hash_table_destroy(params); purple_url_decode(const char *str) static char buf[BUF_LEN]; g_return_val_if_fail(str != NULL, NULL); * XXX - This check could be removed and buf could be made * dynamically allocated, but this is easier. if (strlen(str) >= BUF_LEN) for (i = 0; i < strlen(str); i++) { strncpy(hex, str + ++i, 2); /* i is pointing to the start of the number */ * Now it's at the end and at the start of the for loop * will be at the next character. buf[j++] = strtol(hex, NULL, 16); if (!g_utf8_validate(buf, -1, (const char **)&bum)) purple_url_encode(const char *str) static char buf[BUF_LEN]; g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { gunichar c = g_utf8_get_char(iter); /* If the character is an ASCII character and is alphanumeric if (c < 128 && (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) { int bytes = g_unichar_to_utf8(c, utf_char); for (i = 0; (int)i < bytes; i++) { if (i >= sizeof(utf_char)) { sprintf(buf + j, "%%%02X", utf_char[i] & 0xff); /* Originally lifted from * http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html * ... and slightly modified to be a bit more rfc822 compliant * ... and modified a bit more to make domain checking rfc1035 compliant * with the exception permitted in rfc1101 for domains to start with digit * but not completely checking to avoid conflicts with IP addresses purple_email_is_valid(const char *address) static char *rfc822_specials = "()<>@,;:\\\"[]"; g_return_val_if_fail(address != NULL, FALSE); if (*address == '.') return FALSE; /* first we validate the name portion (name@domain) (rfc822)*/ for (c = address; *c; c++) { if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) { if (*c++ && *c < 127 && *c > 0 && *c != '\n' && *c != '\r') continue; if (*c < ' ' || *c >= 127) return FALSE; if (*c != '.') return FALSE; if (*c <= ' ' || *c >= 127) return FALSE; if (strchr(rfc822_specials, *c)) return FALSE; /* It's obviously not an email address if we didn't find an '@' above */ if (*c == '\0') return FALSE; /* strictly we should return false if (*(c - 1) == '.') too, but I think * we should permit user.@domain type addresses - they do work :) */ if (c == address) return FALSE; /* next we validate the domain portion (name@domain) (rfc1035 & rfc1011) */ if (!*(domain = ++c)) return FALSE; if (*c == '.' && (c == domain || *(c - 1) == '.' || *(c - 1) == '-')) if (*c == '-' && (*(c - 1) == '.' || *(c - 1) == '@')) return FALSE; if ((*c < '0' && *c != '-' && *c != '.') || (*c > '9' && *c < 'A') || (*c > 'Z' && *c < 'a') || (*c > 'z')) return FALSE; if (*(c - 1) == '-') return FALSE; return ((c - domain) > 3 ? TRUE : FALSE); /* Stolen from gnome_uri_list_extract_uris */ purple_uri_list_extract_uris(const gchar *uri_list) g_return_val_if_fail (uri_list != NULL, NULL); /* We don't actually try to validate the URI according to RFC * 2396, or even check for allowed characters - we just ignore * comments and trim whitespace off the ends. We also * allow LF delimination as well as the specified CRLF. while (*q && (*q != '\n') && (*q != '\r')) while (q > p && isspace(*q)) retval = (gchar*)g_malloc (q - p + 2); strncpy (retval, p, q - p + 1); retval[q - p + 1] = '\0'; result = g_list_prepend (result, retval); return g_list_reverse (result); /* Stolen from gnome_uri_list_extract_filenames */ purple_uri_list_extract_filenames(const gchar *uri_list) GList *tmp_list, *node, *result; g_return_val_if_fail (uri_list != NULL, NULL); result = purple_uri_list_extract_uris(uri_list); gchar *s = (gchar*)tmp_list->data; tmp_list = tmp_list->next; if (!strncmp (s, "file:", 5)) { node->data = g_filename_from_uri (s, NULL, NULL); /* not sure if this fallback is useful at all */ if (!node->data) node->data = g_strdup (s+5); result = g_list_delete_link(result, node); purple_uri_escape_for_open(const char *unescaped) /* Replace some special characters like $ with their percent-encoded value. * This shouldn't be necessary because we shell-escape the entire arg before * exec'ing the browser, however, we had a report that a URL containing * $(xterm) was causing xterm to start on his system. This is obviously a * bug on his system, but it's pretty easy for us to protect against it. */ return g_uri_escape_string(unescaped, "[]:;/%#,+?=&@", FALSE); /************************************************************************** **************************************************************************/ purple_utf8_try_convert(const char *str) g_return_val_if_fail(str != NULL, NULL); if (g_utf8_validate(str, -1, NULL)) { utf8 = g_locale_to_utf8(str, -1, &converted, NULL, NULL); utf8 = g_convert(str, -1, "UTF-8", "ISO-8859-15", &converted, NULL, NULL); if ((utf8 != NULL) && (converted == strlen(str))) #define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \ || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf0) purple_utf8_salvage(const char *str) g_return_val_if_fail(str != NULL, NULL); workstr = g_string_sized_new(strlen(str)); (void)g_utf8_validate(str, -1, &end); workstr = g_string_append_len(workstr, str, end - str); workstr = g_string_append_c(workstr, '?'); } while (!utf8_first(*str)); return g_string_free(workstr, FALSE); purple_utf8_strip_unprintables(const gchar *str) if (!g_utf8_validate(str, -1, &bad)) { purple_debug_error("util", "purple_utf8_strip_unprintables(%s) failed; " "first bad character was %02x (%c)\n", g_return_val_if_reached(NULL); workstr = iter = g_new(gchar, strlen(str) + 1); gunichar ch = g_utf8_get_char(str); gchar *next = g_utf8_next_char(str); * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | if ((ch == '\t' || ch == '\n' || ch == '\r') || (ch >= 0x20 && ch <= 0xD7FF) || (ch >= 0xE000 && ch <= 0xFFFD) || (ch >= 0x10000 && ch <= 0x10FFFF)) { memcpy(iter, str, next - str); /* nul-terminate the new string */ * This function is copied from g_strerror() but changed to use purple_gai_strerror(gint errnum) static GPrivate msg_private = G_PRIVATE_INIT(g_free); msg_locale = gai_strerror(errnum); /* This string is already UTF-8--great! */ gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL); /* Stick in the quark table so that we can return a static result */ GQuark msg_quark = g_quark_from_string(msg_utf8); msg_utf8 = (gchar *)g_quark_to_string(msg_quark); msg = g_private_get(&msg_private); g_private_set(&msg_private, msg); sprintf(msg, "unknown error (%d)", errnum); purple_utf8_ncr_encode(const char *str) g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); for(; *str; str = g_utf8_next_char(str)) { gunichar wc = g_utf8_get_char(str); /* super simple check. hopefully not too wrong. */ g_string_append_printf(out, "&#%u;", (guint32) wc); g_string_append_unichar(out, wc); return g_string_free(out, FALSE); purple_utf8_ncr_decode(const char *str) g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); while( (b = strstr(buf, "&#")) ) { /* append everything leading up to the &# */ g_string_append_len(out, buf, b-buf); b += 2; /* skip past the &# */ /* strtoul will treat 0x prefix as hex, but not just x */ if(*b == 'x' || *b == 'X') { /* advances buf to the end of the ncr segment */ wc = (gunichar) strtoul(b, &buf, base); /* this mimics the previous impl of ncr_decode */ g_string_append_unichar(out, wc); /* append whatever's left */ g_string_append(out, buf); return g_string_free(out, FALSE); purple_utf8_strcasecmp(const char *a, const char *b) if(!g_utf8_validate(a, -1, NULL) || !g_utf8_validate(b, -1, NULL)) purple_debug_error("purple_utf8_strcasecmp", "One or both parameters are invalid UTF8\n"); a_norm = g_utf8_casefold(a, -1); b_norm = g_utf8_casefold(b, -1); ret = g_utf8_collate(a_norm, b_norm); /* previously conversation::find_nick() */ purple_utf8_has_word(const char *haystack, const char *needle) const char *start, *prev_char; start = hay = g_utf8_strdown(haystack, -1); pin = g_utf8_strdown(needle, -1); while ((p = strstr(start, pin)) != NULL) { prev_char = g_utf8_find_prev_char(hay, p); before = g_utf8_get_char(prev_char); after = g_utf8_get_char_validated(p + n, - 1); /* The character before is a reasonable guess for a word boundary ("!g_unichar_isalnum()" is not a valid way to determine word boundaries, but it is the only reasonable thing to do here), and isn't the '&' from a "&" or some such entity*/ (before != (gunichar)-2 && !g_unichar_isalnum(before) && *(p - 1) != '&')) && after != (gunichar)-2 && !g_unichar_isalnum(after)) { gboolean purple_message_meify(char *message, gssize len) gboolean inside_html = FALSE; g_return_val_if_fail(message != NULL, FALSE); for (c = message; *c; c++, len--) { if(*c && !g_ascii_strncasecmp(c, "/me ", 4)) { char *purple_text_strip_mnemonic(const char *in) g_return_val_if_fail(in != NULL, NULL); out = g_malloc(strlen(in)+1); a0 = a; /* The last non-space char seen so far, or the first char */ if(a > out && b > in && *(b-1) == '(' && *(b+1) && !(*(b+1) & 0x80) && *(b+2) == ')') { /* Detected CJK style shortcut (Bug 875311) */ a = a0; /* undo the left parenthesis */ b += 3; /* and skip the whole mess */ } else if(*(b+1) == '_') { /* We don't want to corrupt the middle of UTF-8 characters */ } else if (!(*b & 0x80)) { /* other 1-byte char */ /* Multibyte utf8 char, don't look for _ inside these */ if ((*b & 0xe0) == 0xc0) { } else if ((*b & 0xf0) == 0xe0) { } else if ((*b & 0xf8) == 0xf0) { } else if ((*b & 0xfc) == 0xf8) { } else if ((*b & 0xfe) == 0xfc) { } else { /* Illegal utf8 */ a0 = a; /* unless we want to delete CJK spaces too */ for (i = 0; i < n && *b; i += 1) { const char* purple_unescape_filename(const char *escaped) { return purple_url_decode(escaped); /* this is almost identical to purple_url_encode (hence purple_url_decode * being used above), but we want to keep certain characters unescaped purple_escape_filename(const char *str) static char buf[BUF_LEN]; g_return_val_if_fail(str != NULL, NULL); g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL); for (; *iter && j < (BUF_LEN - 1) ; iter = g_utf8_next_char(iter)) { gunichar c = g_utf8_get_char(iter); /* If the character is an ASCII character and is alphanumeric, * or one of the specified values, no need to escape */ if (c < 128 && (g_ascii_isalnum(c) || c == '@' || c == '-' || c == '_' || c == '.' || c == '#')) { int bytes = g_unichar_to_utf8(c, utf_char); for (i = 0; (int)i < bytes; i++) { if (i >= sizeof(utf_char)) { sprintf(buf + j, "%%%02x", utf_char[i] & 0xff); /* File/Directory names in windows cannot end in periods/spaces. * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx while (j > 0 && (buf[j - 1] == '.' || buf[j - 1] == ' ')) gchar * purple_escape_js(const gchar *str) json_node_set_string(escape_js_node, str); json_generator_set_root(escape_js_gen, escape_js_node); escaped = json_generator_to_data(escape_js_gen, NULL); json_node_set_boolean(escape_js_node, FALSE); void purple_restore_default_signal_handlers(void) signal(SIGHUP, SIG_DFL); /* 1: terminal line hangup */ signal(SIGINT, SIG_DFL); /* 2: interrupt program */ signal(SIGQUIT, SIG_DFL); /* 3: quit program */ signal(SIGILL, SIG_DFL); /* 4: illegal instruction (not reset when caught) */ signal(SIGTRAP, SIG_DFL); /* 5: trace trap (not reset when caught) */ signal(SIGABRT, SIG_DFL); /* 6: abort program */ signal(SIGPOLL, SIG_DFL); /* 7: pollable event (POSIX) */ signal(SIGEMT, SIG_DFL); /* 7: EMT instruction (Non-POSIX) */ signal(SIGFPE, SIG_DFL); /* 8: floating point exception */ signal(SIGBUS, SIG_DFL); /* 10: bus error */ signal(SIGSEGV, SIG_DFL); /* 11: segmentation violation */ signal(SIGSYS, SIG_DFL); /* 12: bad argument to system call */ signal(SIGPIPE, SIG_DFL); /* 13: write on a pipe with no reader */ signal(SIGALRM, SIG_DFL); /* 14: real-time timer expired */ signal(SIGTERM, SIG_DFL); /* 15: software termination signal */ signal(SIGCHLD, SIG_DFL); /* 20: child status has changed */ signal(SIGXCPU, SIG_DFL); /* 24: exceeded CPU time limit */ signal(SIGXFSZ, SIG_DFL); /* 25: exceeded file size limit */ set_status_with_attrs(PurpleStatus *status, ...) purple_status_set_active_with_attrs(status, TRUE, args); void purple_util_set_current_song(const char *title, const char *artist, const char *album) GList *list = purple_accounts_get_all(); for (; list; list = list->next) { PurplePresence *presence; PurpleAccount *account = list->data; if (!purple_account_get_enabled(account, purple_core_get_ui())) presence = purple_account_get_presence(account); tune = purple_presence_get_status(presence, "tune"); set_status_with_attrs(tune, PURPLE_TUNE_TITLE, title, PURPLE_TUNE_ARTIST, artist, PURPLE_TUNE_ALBUM, album, purple_status_set_active(tune, FALSE); char * purple_util_format_song_info(const char *title, const char *artist, const char *album, gpointer unused) esc = g_markup_escape_text(title, -1); string = g_string_new(""); g_string_append_printf(string, "%s", esc); esc = g_markup_escape_text(artist, -1); g_string_append_printf(string, _(" - %s"), esc); esc = g_markup_escape_text(album, -1); g_string_append_printf(string, _(" (%s)"), esc); return g_string_free(string, FALSE); purple_get_host_name(void) return g_get_host_name(); a = 0x4000 | (tmp & 0xFFF); /* 0x4000 to 0x4FFF */ b = ((1 << 3) << 12) | (tmp & 0x3FFF); /* 0x8000 to 0xBFFF */ return g_strdup_printf("%08x-%04x-%04x-%04x-%04x%08x", (tmp >> 16) & 0xFFFF, g_random_int()); void purple_callback_set_zero(gpointer data) g_return_if_fail(ptr != NULL); purple_value_new(GType type) g_return_val_if_fail(type != G_TYPE_NONE, NULL); purple_value_dup(GValue *value) g_return_val_if_fail(value != NULL, NULL); g_value_init(ret, G_VALUE_TYPE(value)); g_value_copy(value, ret); purple_value_free(GValue *value) g_return_if_fail(value != NULL); _purple_fstat(int fd, GStatBuf *st) g_return_val_if_fail(st != NULL, -1);