2007-12-06 [colin] 3.1.0cvs62
[claws.git] / src / common / utils.c
index 73eaf2118207ed5b6291935fed3160038643bef4..a93cb49dcd053b5d53ef184669dfa5773d7e30cd 100644 (file)
 
 #ifdef MAEMO
 #include <libosso.h>
-#include <osso-browser-interface.h>
+#ifdef CHINOOK
+# include <tablet-browser-interface.h>
+#else
+# include <osso-browser-interface.h>
+#endif
 #endif
 
 #include "utils.h"
@@ -1037,7 +1041,7 @@ void subst_char(gchar *str, gchar orig, gchar subst)
        }
 }
 
-static void subst_chars(gchar *str, gchar *orig, gchar subst)
+void subst_chars(gchar *str, gchar *orig, gchar subst)
 {
        register gchar *p = str;
 
@@ -1226,36 +1230,6 @@ static gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
        return NULL;
 }
 
-static gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
-{
-       const gchar *p;
-       gchar quote_chr = '"';
-       gint in_brace;
-       gboolean in_quote = FALSE;
-
-       p = str;
-
-       if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
-               p++;
-               in_brace = 1;
-               while (*p) {
-                       if (*p == op && !in_quote)
-                               in_brace++;
-                       else if (*p == cl && !in_quote)
-                               in_brace--;
-                       else if (*p == quote_chr)
-                               in_quote ^= TRUE;
-
-                       if (in_brace == 0)
-                               return (gchar *)p;
-
-                       p++;
-               }
-       }
-
-       return NULL;
-}
-
 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
                            gint max_tokens)
 {
@@ -1530,8 +1504,15 @@ void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
        decode_uri_with_plus(decoded_uri, encoded_uri, TRUE);
 }
 
+static gchar *decode_uri_gdup(const gchar *encoded_uri)
+{
+    gchar *buffer = g_malloc(strlen(encoded_uri)+1);
+    decode_uri(buffer, encoded_uri);
+    return buffer;
+}
+
 gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
-                    gchar **subject, gchar **body, gchar **attach)
+                    gchar **subject, gchar **body, gchar ***attach)
 {
        gchar *tmp_mailto;
        gchar *p;
@@ -1539,6 +1520,8 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
                                          "/etc/passwd",
                                          "/etc/shadow",
                                          NULL };
+       gint num_attach = 0;
+       gchar **my_att = NULL;
 
        Xstrdup_a(tmp_mailto, mailto, return -1);
 
@@ -1552,7 +1535,10 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
        }
 
        if (to && !*to)
-               *to = g_strdup(tmp_mailto);
+               *to = decode_uri_gdup(tmp_mailto);
+
+       my_att = g_malloc(sizeof(char *));
+       my_att[0] = NULL;
 
        while (p) {
                gchar *field, *value;
@@ -1574,33 +1560,54 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
 
                if (*value == '\0') continue;
 
-               if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) {
-                       *cc = g_strdup(value);
-               } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) {
-                       *bcc = g_strdup(value);
+               if (cc && !g_ascii_strcasecmp(field, "cc")) {
+                       if (!*cc) {
+                               *cc = decode_uri_gdup(value);
+                       } else {
+                               gchar *tmp = decode_uri_gdup(value);
+                               gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
+                               g_free(*cc);
+                               *cc = new_cc;
+                       }
+               } else if (bcc && !g_ascii_strcasecmp(field, "bcc")) {
+                       if (!*bcc) {
+                               *bcc = decode_uri_gdup(value);
+                       } else {
+                               gchar *tmp = decode_uri_gdup(value);
+                               gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
+                               g_free(*bcc);
+                               *bcc = new_bcc;
+                       }
                } else if (subject && !*subject &&
                           !g_ascii_strcasecmp(field, "subject")) {
-                       *subject = g_malloc(strlen(value) + 1);
-                       decode_uri(*subject, value);
+                       *subject = decode_uri_gdup(value);
                } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
-                       *body = g_malloc(strlen(value) + 1);
-                       decode_uri(*body, value);
-               } else if (attach && !*attach && !g_ascii_strcasecmp(field, "attach")) {
+                       *body = decode_uri_gdup(value);
+               } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
                        int i = 0;
-                       *attach = g_malloc(strlen(value) + 1);
-                       decode_uri(*attach, value);
+                       gchar *tmp = decode_uri_gdup(value);
                        for (; forbidden_uris[i]; i++) {
-                               if (strstr(*attach, forbidden_uris[i])) {
+                               if (strstr(tmp, forbidden_uris[i])) {
                                        g_print("Refusing to attach '%s', potential private data leak\n",
-                                                       *attach);
-                                       g_free(*attach);
-                                       *attach = NULL;
+                                                       tmp);
+                                       g_free(tmp);
+                                       tmp = NULL;
                                        break;
                                }
                        }
+                       if (tmp) {
+                               /* attach is correct */
+                               num_attach++;
+                               printf("realloc my_att %d\n", (num_attach+1));
+                               my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
+                               my_att[num_attach-1] = tmp;
+                               my_att[num_attach] = NULL;
+                       }
                }
        }
 
+       if (attach)
+               *attach = my_att;
        return 0;
 }
 
@@ -3016,26 +3023,7 @@ gint str_write_to_file(const gchar *str, const gchar *file)
        return 0;
 }
 
-gchar *file_read_to_str(const gchar *file)
-{
-       FILE *fp;
-       gchar *str;
-
-       g_return_val_if_fail(file != NULL, NULL);
-
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return NULL;
-       }
-
-       str = file_read_stream_to_str(fp);
-
-       fclose(fp);
-
-       return str;
-}
-
-gchar *file_read_stream_to_str(FILE *fp)
+static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
 {
        GByteArray *array;
        guchar buf[BUFSIZ];
@@ -3063,7 +3051,7 @@ gchar *file_read_stream_to_str(FILE *fp)
        str = (gchar *)array->data;
        g_byte_array_free(array, FALSE);
 
-       if (!g_utf8_validate(str, -1, NULL)) {
+       if (recode && !g_utf8_validate(str, -1, NULL)) {
                const gchar *src_codeset, *dest_codeset;
                gchar *tmp = NULL;
                src_codeset = conv_get_locale_charset_str();
@@ -3076,6 +3064,42 @@ gchar *file_read_stream_to_str(FILE *fp)
        return str;
 }
 
+static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
+{
+       FILE *fp;
+       gchar *str;
+
+       g_return_val_if_fail(file != NULL, NULL);
+
+       if ((fp = g_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return NULL;
+       }
+
+       str = file_read_stream_to_str_full(fp, recode);
+
+       fclose(fp);
+
+       return str;
+}
+
+gchar *file_read_to_str(const gchar *file)
+{
+       return file_read_to_str_full(file, TRUE);
+}
+gchar *file_read_stream_to_str(FILE *fp)
+{
+       return file_read_stream_to_str_full(fp, TRUE);
+}
+
+gchar *file_read_to_str_no_recode(const gchar *file)
+{
+       return file_read_to_str_full(file, FALSE);
+}
+gchar *file_read_stream_to_str_no_recode(FILE *fp)
+{
+       return file_read_stream_to_str_full(fp, FALSE);
+}
 
 char *fgets_crlf(char *buf, int size, FILE *stream)
 {
@@ -4050,7 +4074,7 @@ static GHashTable *create_domain_tab(void)
            "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl",
            "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de",
            "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er",
-           "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd",
+           "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd",
            "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
            "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr",
            "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir",