2005-07-01 [colin] 1.9.12cvs8
[claws.git] / src / common / utils.c
index 6f7d1ffbb91a520506463328a13001304d815718..ffe51f67c750f59fcaf7e6f1f3d940ff5de9c578 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto & The Sylpheed-Claws Team
  *
  * 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
@@ -24,6 +24,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 #include <sys/wait.h>
 #include <dirent.h>
 #include <time.h>
+#include <regex.h>
+#include <sys/utsname.h>
 
-#include "intl.h"
 #include "utils.h"
 #include "socket.h"
+#include "../codeconv.h"
 
 #define BUFFSIZE       8192
 
@@ -109,6 +112,25 @@ void hash_free_value_mem(GHashTable *table)
        g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
 }
 
+gint str_case_equal(gconstpointer v, gconstpointer v2)
+{
+       return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
+}
+
+guint str_case_hash(gconstpointer key)
+{
+       const gchar *p = key;
+       guint h = *p;
+
+       if (h) {
+               h = tolower(h);
+               for (p += 1; *p != '\0'; p++)
+                       h = (h << 5) - h + tolower(*p);
+       }
+
+       return h;
+}
+
 void ptr_array_free_strings(GPtrArray *array)
 {
        gint i;
@@ -122,9 +144,29 @@ void ptr_array_free_strings(GPtrArray *array)
        }
 }
 
+gboolean str_find(const gchar *haystack, const gchar *needle)
+{
+       return strstr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_case_find(const gchar *haystack, const gchar *needle)
+{
+       return strcasestr(haystack, needle) != NULL ? TRUE : FALSE;
+}
+
+gboolean str_find_equal(const gchar *haystack, const gchar *needle)
+{
+       return strcmp(haystack, needle) == 0;
+}
+
+gboolean str_case_find_equal(const gchar *haystack, const gchar *needle)
+{
+       return strcasecmp(haystack, needle) == 0;
+}
+
 gint to_number(const gchar *nstr)
 {
-       register const gchar *p;
+       register const guchar *p;
 
        if (*nstr == '\0') return -1;
 
@@ -155,13 +197,13 @@ gchar *to_human_readable(off_t size)
        static gchar str[10];
 
        if (size < 1024)
-               g_snprintf(str, sizeof(str), "%dB", (gint)size);
+               g_snprintf(str, sizeof(str), _("%dB"), (gint)size);
        else if (size >> 10 < 1024)
-               g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
+               g_snprintf(str, sizeof(str), _("%.1fKB"), (gfloat)size / (1 << 10));
        else if (size >> 20 < 1024)
-               g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
+               g_snprintf(str, sizeof(str), _("%.2fMB"), (gfloat)size / (1 << 20));
        else
-               g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
+               g_snprintf(str, sizeof(str), _("%.2fGB"), (gfloat)size / (1 << 30));
 
        return str;
 }
@@ -256,7 +298,7 @@ gchar *strcasestr(const gchar *haystack, const gchar *needle)
                return NULL;
 
        while (haystack_len >= needle_len) {
-               if (!strncasecmp(haystack, needle, needle_len))
+               if (!g_ascii_strncasecmp(haystack, needle, needle_len))
                        return (gchar *)haystack;
                else {
                        haystack++;
@@ -267,23 +309,40 @@ gchar *strcasestr(const gchar *haystack, const gchar *needle)
        return NULL;
 }
 
+gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
+                  gconstpointer needle, size_t needlelen)
+{
+       const gchar *haystack_ = (const gchar *)haystack;
+       const gchar *needle_ = (const gchar *)needle;
+       const gchar *haystack_cur = (const gchar *)haystack;
+
+       if (needlelen == 1)
+               return memchr(haystack_, *needle_, haystacklen);
+
+       while ((haystack_cur = memchr(haystack_cur, *needle_, haystacklen))
+              != NULL) {
+               if (haystacklen - (haystack_cur - haystack_) < needlelen)
+                       break;
+               if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
+                       return (gpointer)haystack_cur;
+               else
+                       haystack_cur++;
+       }
+
+       return NULL;
+}
+
 /* Copy no more than N characters of SRC to DEST, with NULL terminating.  */
 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
 {
-       register gchar c;
-       gchar *s = dest;
+       register const gchar *s = src;
+       register gchar *d = dest;
 
-       do {
-               if (--n == 0) {
-                       *dest = '\0';
-                       return s;
-               }
-               c = *src++;
-               *dest++ = c;
-       } while (c != '\0');
+       while (--n && *s)
+               *d++ = *s++;
+       *d = '\0';
 
-       /* don't do zero fill */
-       return s;
+       return dest;
 }
 
 #if !HAVE_ISWALNUM
@@ -469,53 +528,52 @@ wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
        return NULL;
 }
 
-/* Examine if next block is non-ASCII string */
-gboolean is_next_nonascii(const wchar_t *s)
+gint get_mbs_len(const gchar *s)
 {
-       const wchar_t *wp;
+       const gchar *p = s;
+       gint mb_len;
+       gint len = 0;
 
-       /* skip head space */
-       for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
-               ;
-       for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
-               if (*wp > 127)
-                       return TRUE;
+       if (!p)
+               return -1;
+
+       while (*p != '\0') {
+               mb_len = g_utf8_skip[*(guchar *)p];
+               if (mb_len == 0)
+                       break;
+               else
+                       len++;
+
+               p += mb_len;
        }
 
-       return FALSE;
+       return len;
 }
 
-/* Examine if next block is multi-byte string */
-gboolean is_next_mbs(const wchar_t *s)
+/* Examine if next block is non-ASCII string */
+gboolean is_next_nonascii(const guchar *s)
 {
-       gint mbl;
-       const wchar_t *wp;
-       gchar tmp[MB_LEN_MAX];
+       const guchar *p;
 
        /* skip head space */
-       for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
+       for (p = s; *p != '\0' && isspace(*p); p++)
                ;
-       for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
-               mbl = wctomb(tmp, *wp);
-               if (mbl > 1)
+       for (; *p != '\0' && !isspace(*p); p++) {
+               if (*p > 127 || *p < 32)
                        return TRUE;
        }
 
        return FALSE;
 }
 
-wchar_t *find_wspace(const wchar_t *s)
+gint get_next_word_len(const guchar *s)
 {
-       const wchar_t *wp;
+       gint len = 0;
 
-       for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
+       for (; *s != '\0' && !isspace(*s); s++, len++)
                ;
-       for (; *wp != (wchar_t)0; wp++) {
-               if (iswspace(*wp))
-                       return (wchar_t *)wp;
-       }
 
-       return NULL;
+       return len;
 }
 
 /* compare subjects */
@@ -529,15 +587,30 @@ gint subject_compare(const gchar *s1, const gchar *s2)
        Xstrdup_a(str1, s1, return -1);
        Xstrdup_a(str2, s2, return -1);
 
-       trim_subject(str1);
-       trim_subject(str2);
+       trim_subject_for_compare(str1);
+       trim_subject_for_compare(str2);
 
        if (!*str1 || !*str2) return -1;
 
        return strcmp(str1, str2);
 }
 
-void trim_subject(gchar *str)
+gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
+{
+       gchar *str1, *str2;
+
+       if (!s1 || !s2) return -1;
+
+       Xstrdup_a(str1, s1, return -1);
+       Xstrdup_a(str2, s2, return -1);
+
+       trim_subject_for_sort(str1);
+       trim_subject_for_sort(str2);
+
+       return g_utf8_collate(str1, str2);
+}
+
+void trim_subject_for_compare(gchar *str)
 {
        gchar *srcp;
 
@@ -545,23 +618,44 @@ void trim_subject(gchar *str)
        eliminate_parenthesis(str, '(', ')');
        g_strstrip(str);
 
-       while (!strncasecmp(str, "Re:", 3)) {
-               srcp = str + 3;
-               while (isspace(*srcp)) srcp++;
+       srcp = str + subject_get_prefix_length(str);
+       if (srcp != str)
                memmove(str, srcp, strlen(srcp) + 1);
-       }
 }
 
-void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
+void trim_subject_for_sort(gchar *str)
 {
-       register gchar *srcp, *destp;
+       gchar *srcp;
+
+       g_strstrip(str);
+
+       srcp = str + subject_get_prefix_length(str);
+       if (srcp != str)        
+               memmove(str, srcp, strlen(srcp) + 1);
+}
+
+void trim_subject(gchar *str)
+{
+       register guchar *srcp;
+       gchar op, cl;
        gint in_brace;
+       
+       g_strstrip(str);
 
-       srcp = destp = str;
+       srcp = str + subject_get_prefix_length(str);
 
-       while ((destp = strchr(destp, op))) {
+       if (*srcp == '[') {
+               op = '[';
+               cl = ']';
+       } else if (*srcp == '(') {
+               op = '(';
+               cl = ')';
+       } else
+               op = 0;
+
+       if (op) {
+               ++srcp;
                in_brace = 1;
-               srcp = destp + 1;
                while (*srcp) {
                        if (*srcp == op)
                                in_brace++;
@@ -571,57 +665,52 @@ void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
                        if (in_brace == 0)
                                break;
                }
-               while (isspace(*srcp)) srcp++;
-               memmove(destp, srcp, strlen(srcp) + 1);
        }
+       while (isspace(*srcp)) srcp++;
+       memmove(str, srcp, strlen(srcp) + 1);
 }
 
-void extract_parenthesis(gchar *str, gchar op, gchar cl)
+void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
 {
-       register gchar *srcp, *destp;
+       register guchar *srcp, *destp;
        gint in_brace;
 
        srcp = destp = str;
 
-       while ((srcp = strchr(destp, op))) {
-               if (destp > str)
-                       *destp++ = ' ';
-               memmove(destp, srcp + 1, strlen(srcp));
+       while ((destp = strchr(destp, op))) {
                in_brace = 1;
-               while(*destp) {
-                       if (*destp == op)
+               srcp = destp + 1;
+               while (*srcp) {
+                       if (*srcp == op)
                                in_brace++;
-                       else if (*destp == cl)
+                       else if (*srcp == cl)
                                in_brace--;
-
+                       srcp++;
                        if (in_brace == 0)
                                break;
-
-                       destp++;
                }
+               while (isspace(*srcp)) srcp++;
+               memmove(destp, srcp, strlen(srcp) + 1);
        }
-       *destp = '\0';
 }
 
-void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
-                                            gchar op, gchar cl)
+void extract_parenthesis(gchar *str, gchar op, gchar cl)
 {
        register gchar *srcp, *destp;
        gint in_brace;
-       gboolean in_quote = FALSE;
 
        srcp = destp = str;
 
-       if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
+       while ((srcp = strchr(destp, op))) {
+               if (destp > str)
+                       *destp++ = ' ';
                memmove(destp, srcp + 1, strlen(srcp));
                in_brace = 1;
                while(*destp) {
-                       if (*destp == op && !in_quote)
+                       if (*destp == op)
                                in_brace++;
-                       else if (*destp == cl && !in_quote)
+                       else if (*destp == cl)
                                in_brace--;
-                       else if (*destp == quote_chr)
-                               in_quote ^= TRUE;
 
                        if (in_brace == 0)
                                break;
@@ -665,7 +754,7 @@ void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
 
 void eliminate_quote(gchar *str, gchar quote_chr)
 {
-       register gchar *srcp, *destp;
+       register guchar *srcp, *destp;
 
        srcp = destp = str;
 
@@ -700,7 +789,7 @@ void extract_quote(gchar *str, gchar quote_chr)
 
 void eliminate_address_comment(gchar *str)
 {
-       register gchar *srcp, *destp;
+       register guchar *srcp, *destp;
        gint in_brace;
 
        srcp = destp = str;
@@ -779,7 +868,14 @@ void extract_address(gchar *str)
        g_strstrip(str);
 }
 
-GSList *address_list_append(GSList *addr_list, const gchar *str)
+void extract_list_id_str(gchar *str)
+{
+       if (strchr_with_skip_quote(str, '"', '<'))
+               extract_parenthesis_with_skip_quote(str, '"', '<', '>');
+       g_strstrip(str);
+}
+
+static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
 {
        gchar *work;
        gchar *workp;
@@ -788,7 +884,8 @@ GSList *address_list_append(GSList *addr_list, const gchar *str)
 
        Xstrdup_a(work, str, return addr_list);
 
-       eliminate_address_comment(work);
+       if (removecomments)
+               eliminate_address_comment(work);
        workp = work;
 
        while (workp && *workp) {
@@ -800,7 +897,7 @@ GSList *address_list_append(GSList *addr_list, const gchar *str)
                } else
                        next = NULL;
 
-               if (strchr_with_skip_quote(workp, '"', '<'))
+               if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
                        extract_parenthesis_with_skip_quote
                                (workp, '"', '<', '>');
 
@@ -814,7 +911,17 @@ GSList *address_list_append(GSList *addr_list, const gchar *str)
        return addr_list;
 }
 
-GSList *references_list_append(GSList *msgid_list, const gchar *str)
+GSList *address_list_append(GSList *addr_list, const gchar *str)
+{
+       return address_list_append_real(addr_list, str, TRUE);
+}
+
+GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
+{
+       return address_list_append_real(addr_list, str, FALSE);
+}
+
+GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
 {
        const gchar *strp;
 
@@ -834,7 +941,7 @@ GSList *references_list_append(GSList *msgid_list, const gchar *str)
                msgid = g_strndup(start + 1, end - start - 1);
                g_strstrip(msgid);
                if (*msgid)
-                       msgid_list = g_slist_append(msgid_list, msgid);
+                       msgid_list = g_slist_prepend(msgid_list, msgid);
                else
                        g_free(msgid);
 
@@ -844,6 +951,17 @@ GSList *references_list_append(GSList *msgid_list, const gchar *str)
        return msgid_list;
 }
 
+GSList *references_list_append(GSList *msgid_list, const gchar *str)
+{
+       GSList *list;
+
+       list = references_list_prepend(NULL, str);
+       list = g_slist_reverse(list);
+       msgid_list = g_slist_concat(msgid_list, list);
+
+       return msgid_list;
+}
+
 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
 {
        gchar *work;
@@ -914,7 +1032,7 @@ void remove_return(gchar *str)
 
 void remove_space(gchar *str)
 {
-       register gchar *p = str;
+       register guchar *p = str;
        register gint spc;
 
        while (*p) {
@@ -930,7 +1048,7 @@ void remove_space(gchar *str)
 
 void unfold_line(gchar *str)
 {
-       register gchar *p = str;
+       register guchar *p = str;
        register gint spc;
 
        while (*p) {
@@ -970,7 +1088,17 @@ void subst_chars(gchar *str, gchar *orig, gchar subst)
 
 void subst_for_filename(gchar *str)
 {
-       subst_chars(str, " \t\r\n\"/\\", '_');
+       if (!str)
+               return;
+       subst_chars(str, "\t\r\n\\/*", '_');
+}
+
+void subst_for_shellsafe_filename(gchar *str)
+{
+       if (!str)
+               return;
+       subst_for_filename(str);
+       subst_chars(str, " \"'|&;()<>'!{}[]",'_');
 }
 
 gboolean is_header_line(const gchar *str)
@@ -988,6 +1116,8 @@ gboolean is_header_line(const gchar *str)
 
 gboolean is_ascii_str(const guchar *str)
 {
+       g_return_val_if_fail(str, FALSE);
+
        while (*str != '\0') {
                if (*str != '\t' && *str != ' ' &&
                    *str != '\r' && *str != '\n' &&
@@ -1001,15 +1131,15 @@ gboolean is_ascii_str(const guchar *str)
 
 gint get_quote_level(const gchar *str, const gchar *quote_chars)
 {
-       const gchar *first_pos;
-       const gchar *last_pos;
-       const gchar *p = str;
+       const guchar *first_pos;
+       const guchar *last_pos;
+       const guchar *p = str;
        gint quote_level = -1;
 
        /* speed up line processing by only searching to the last '>' */
        if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
                /* skip a line if it contains a '<' before the initial '>' */
-               if (memchr(str, '<', first_pos - str) != NULL)
+               if (memchr(str, '<', first_pos - (const guchar *)str) != NULL)
                        return -1;
                last_pos = line_has_quote_char_last(first_pos, quote_chars);
        } else
@@ -1178,7 +1308,7 @@ gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
                        n++;
                        str = s_cl + 1;
 
-                       while (*str && isspace(*str)) str++;
+                       while (*str && isspace(*(guchar *)str)) str++;
                        if (*str != op) {
                                string_list = g_slist_prepend(string_list,
                                                              g_strdup(""));
@@ -1270,22 +1400,23 @@ gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
        gchar *abbrev_group;
        gchar *ap;
        const gchar *p = group;
-       gint  count = 0;
+       const gchar *last;
 
+       g_return_val_if_fail(group != NULL, NULL);
+
+       last = group + strlen(group);
        abbrev_group = ap = g_malloc(strlen(group) + 1);
 
        while (*p) {
                while (*p == '.')
                        *ap++ = *p++;
-
-               if ((strlen( p) + count) > len && strchr(p, '.')) {
+               if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
                        *ap++ = *p++;
                        while (*p != '.') p++;
                } else {
-                       strcpy( ap, p);
+                       strcpy(ap, p);
                        return abbrev_group;
                }
-               count = count + 2;
        }
 
        *ap = '\0';
@@ -1302,17 +1433,17 @@ gchar *trim_string(const gchar *str, gint len)
        if (!str) return NULL;
        if (strlen(str) <= len)
                return g_strdup(str);
+       if (g_utf8_validate(str, -1, NULL) == FALSE)
+               return g_strdup(str);
 
        while (*p != '\0') {
-               mb_len = mblen(p, MB_LEN_MAX);
+               mb_len = g_utf8_skip[*(guchar *)p];
                if (mb_len == 0)
                        break;
-               else if (mb_len < 0)
-                       return g_strdup(str);
                else if (new_len + mb_len > len)
                        break;
-               else
-                       new_len += mb_len;
+
+               new_len += mb_len;
                p += mb_len;
        }
 
@@ -1324,7 +1455,7 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
 {
        GList *result = NULL;
        const gchar *p, *q;
-       gchar *file;
+       gchar *escaped_utf8uri;
 
        p = uri_list;
 
@@ -1332,17 +1463,35 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
                if (*p != '#') {
                        while (isspace(*p)) p++;
                        if (!strncmp(p, "file:", 5)) {
-                               p += 5;
                                q = p;
+                               q += 5;
                                while (*q && *q != '\n' && *q != '\r') q++;
 
                                if (q > p) {
+                                       gchar *file, *locale_file = NULL;
                                        q--;
                                        while (q > p && isspace(*q)) q--;
-                                       file = g_malloc(q - p + 2);
-                                       strncpy(file, p, q - p + 1);
-                                       file[q - p + 1] = '\0';
-                                       result = g_list_append(result,file);
+                                       Xalloca(escaped_utf8uri, q - p + 2,
+                                               return result);
+                                       Xalloca(file, q - p + 2,
+                                               return result);
+                                       *file = '\0';
+                                       strncpy(escaped_utf8uri, p, q - p + 1);
+                                       escaped_utf8uri[q - p + 1] = '\0';
+                                       decode_uri(file, escaped_utf8uri);
+                    /*
+                    * g_filename_from_uri() rejects escaped/locale encoded uri
+                    * string which come from Nautilus.
+                    */
+                                       if (g_utf8_validate(file, -1, NULL))
+                                               locale_file
+                                                       = conv_codeset_strdup(
+                                                               file + 5,
+                                                               CS_UTF_8,
+                                                               conv_get_locale_charset_str());
+                                       if (!locale_file)
+                                               locale_file = g_strdup(file + 5);
+                                       result = g_list_append(result, locale_file);
                                }
                        }
                }
@@ -1353,19 +1502,101 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
        return result;
 }
 
-#define HEX_TO_INT(val, hex) \
-{ \
-       gchar c = hex; \
- \
-       if ('0' <= c && c <= '9') { \
-               val = c - '0'; \
-       } else if ('a' <= c && c <= 'f') { \
-               val = c - 'a' + 10; \
-       } else if ('A' <= c && c <= 'F') { \
-               val = c - 'A' + 10; \
-       } else { \
-               val = 0; \
-       } \
+/* Converts two-digit hexadecimal to decimal.  Used for unescaping escaped 
+ * characters
+ */
+static gint axtoi(const gchar *hexstr)
+{
+       gint hi, lo, result;
+       
+       hi = hexstr[0];
+       if ('0' <= hi && hi <= '9') {
+               hi -= '0';
+       } else
+               if ('a' <= hi && hi <= 'f') {
+                       hi -= ('a' - 10);
+               } else
+                       if ('A' <= hi && hi <= 'F') {
+                               hi -= ('A' - 10);
+                       }
+
+       lo = hexstr[1];
+       if ('0' <= lo && lo <= '9') {
+               lo -= '0';
+       } else
+               if ('a' <= lo && lo <= 'f') {
+                       lo -= ('a'-10);
+               } else
+                       if ('A' <= lo && lo <= 'F') {
+                               lo -= ('A' - 10);
+                       }
+       result = lo + (16 * hi);
+       return result;
+}
+
+gboolean is_uri_string(const gchar *str)
+{
+       return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
+               g_ascii_strncasecmp(str, "https://", 8) == 0 ||
+               g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
+               g_ascii_strncasecmp(str, "www.", 4) == 0);
+}
+
+gchar *get_uri_path(const gchar *uri)
+{
+       if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
+               return (gchar *)(uri + 7);
+       else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
+               return (gchar *)(uri + 8);
+       else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
+               return (gchar *)(uri + 6);
+       else
+               return (gchar *)uri;
+}
+
+gint get_uri_len(const gchar *str)
+{
+       const gchar *p;
+
+       if (is_uri_string(str)) {
+               for (p = str; *p != '\0'; p++) {
+                       if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p))
+                               break;
+               }
+               return p - str;
+       }
+
+       return 0;
+}
+
+/* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
+ * plusses, and escape characters are used)
+ */
+void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
+{
+       gchar *dec = decoded_uri;
+       const gchar *enc = encoded_uri;
+
+       while (*enc) {
+               if (*enc == '%') {
+                       enc++;
+                       if (isxdigit((guchar)enc[0]) &&
+                           isxdigit((guchar)enc[1])) {
+                               *dec = axtoi(enc);
+                               dec++;
+                               enc += 2;
+                       }
+               } else {
+                       if (*enc == '+')
+                               *dec = ' ';
+                       else
+                               *dec = *enc;
+                       dec++;
+                       enc++;
+               }
+       }
+
+       *dec = '\0';
 }
 
 gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
@@ -1408,15 +1639,15 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
 
                if (*value == '\0') continue;
 
-               if (cc && !*cc && !g_strcasecmp(field, "cc")) {
+               if (cc && !*cc && !g_ascii_strcasecmp(field, "cc")) {
                        *cc = g_strdup(value);
-               } else if (bcc && !*bcc && !g_strcasecmp(field, "bcc")) {
+               } else if (bcc && !*bcc && !g_ascii_strcasecmp(field, "bcc")) {
                        *bcc = g_strdup(value);
                } else if (subject && !*subject &&
-                          !g_strcasecmp(field, "subject")) {
+                          !g_ascii_strcasecmp(field, "subject")) {
                        *subject = g_malloc(strlen(value) + 1);
                        decode_uri(*subject, value);
-               } else if (body && !*body && !g_strcasecmp(field, "body")) {
+               } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
                        *body = g_malloc(strlen(value) + 1);
                        decode_uri(*body, value);
                }
@@ -1431,7 +1662,7 @@ gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
  * but as long as we are not able to do our own extensions to glibc, we do
  * it here.
  */
-gchar *get_home_dir(void)
+const gchar *get_home_dir(void)
 {
 #if HAVE_DOSISH_SYSTEM
     static gchar *home_dir;
@@ -1456,7 +1687,7 @@ gchar *get_home_dir(void)
 #endif
 }
 
-gchar *get_rc_dir(void)
+const gchar *get_rc_dir(void)
 {
        static gchar *rc_dir = NULL;
 
@@ -1467,7 +1698,7 @@ gchar *get_rc_dir(void)
        return rc_dir;
 }
 
-gchar *get_news_cache_dir(void)
+const gchar *get_news_cache_dir(void)
 {
        static gchar *news_cache_dir = NULL;
 
@@ -1478,7 +1709,7 @@ gchar *get_news_cache_dir(void)
        return news_cache_dir;
 }
 
-gchar *get_imap_cache_dir(void)
+const gchar *get_imap_cache_dir(void)
 {
        static gchar *imap_cache_dir = NULL;
 
@@ -1489,7 +1720,7 @@ gchar *get_imap_cache_dir(void)
        return imap_cache_dir;
 }
 
-gchar *get_mbox_cache_dir(void)
+const gchar *get_mbox_cache_dir(void)
 {
        static gchar *mbox_cache_dir = NULL;
 
@@ -1500,7 +1731,7 @@ gchar *get_mbox_cache_dir(void)
        return mbox_cache_dir;
 }
 
-gchar *get_mime_tmp_dir(void)
+const gchar *get_mime_tmp_dir(void)
 {
        static gchar *mime_tmp_dir = NULL;
 
@@ -1511,7 +1742,7 @@ gchar *get_mime_tmp_dir(void)
        return mime_tmp_dir;
 }
 
-gchar *get_template_dir(void)
+const gchar *get_template_dir(void)
 {
        static gchar *template_dir = NULL;
 
@@ -1522,7 +1753,7 @@ gchar *get_template_dir(void)
        return template_dir;
 }
 
-gchar *get_header_cache_dir(void)
+const gchar *get_header_cache_dir(void)
 {
        static gchar *header_dir = NULL;
 
@@ -1533,7 +1764,7 @@ gchar *get_header_cache_dir(void)
        return header_dir;
 }
 
-gchar *get_tmp_dir(void)
+const gchar *get_tmp_dir(void)
 {
        static gchar *tmp_dir = NULL;
 
@@ -1555,22 +1786,21 @@ gchar *get_tmp_file(void)
        return tmp_file;
 }
 
-gchar *get_domain_name(void)
+const gchar *get_domain_name(void)
 {
        static gchar *domain_name = NULL;
 
        if (!domain_name) {
-               gchar buf[128] = "";
                struct hostent *hp;
+               struct utsname uts;
 
-               if (gethostname(buf, sizeof(buf)) < 0) {
+               if (uname(&uts) < 0) {
                        perror("gethostname");
                        domain_name = "unknown";
                } else {
-                       buf[sizeof(buf) - 1] = '\0';
-                       if ((hp = my_gethostbyname(buf)) == NULL) {
+                       if ((hp = my_gethostbyname(uts.nodename)) == NULL) {
                                perror("gethostbyname");
-                               domain_name = g_strdup(buf);
+                               domain_name = g_strdup(uts.nodename);
                        } else {
                                domain_name = g_strdup(hp->h_name);
                        }
@@ -1698,6 +1928,34 @@ gboolean is_file_entry_exist(const gchar *file)
        return TRUE;
 }
 
+gboolean dirent_is_regular_file(struct dirent *d)
+{
+       struct stat s;
+
+#ifdef HAVE_DIRENT_D_TYPE
+       if (d->d_type == DT_REG)
+               return TRUE;
+       else if (d->d_type != DT_UNKNOWN)
+               return FALSE;
+#endif
+
+       return (stat(d->d_name, &s) == 0 && S_ISREG(s.st_mode));
+}
+
+gboolean dirent_is_directory(struct dirent *d)
+{
+       struct stat s;
+
+#ifdef HAVE_DIRENT_D_TYPE
+       if (d->d_type == DT_DIR)
+               return TRUE;
+       else if (d->d_type != DT_UNKNOWN)
+               return FALSE;
+#endif
+
+       return (stat(d->d_name, &s) == 0 && S_ISDIR(s.st_mode));
+}
+
 gint change_dir(const gchar *dir)
 {
        gchar *prevdir = NULL;
@@ -1807,7 +2065,7 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        DIR *dp;
        struct dirent *d;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
 
        prev_dir = g_get_current_dir();
 
@@ -1824,8 +2082,8 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
        }
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0 && first <= fileno && fileno <= last) {
+               file_no = to_number(d->d_name);
+               if (file_no > 0 && first <= file_no && file_no <= last) {
                        if (is_dir_exist(d->d_name))
                                continue;
                        if (unlink(d->d_name) < 0)
@@ -1851,7 +2109,7 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
        DIR *dp;
        struct dirent *d;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
 
        prev_dir = g_get_current_dir();
 
@@ -1868,9 +2126,9 @@ gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
        }
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
-                       debug_print("removing unwanted file %d from %s\n", fileno, dir);
+               file_no = to_number(d->d_name);
+               if (file_no > 0 && (g_slist_find(numberlist, GINT_TO_POINTER(file_no)) == NULL)) {
+                       debug_print("removing unwanted file %d from %s\n", file_no, dir);
                        if (is_dir_exist(d->d_name))
                                continue;
                        if (unlink(d->d_name) < 0)
@@ -1902,7 +2160,7 @@ gint remove_expired_files(const gchar *dir, guint hours)
        struct dirent *d;
        struct stat s;
        gchar *prev_dir;
-       gint fileno;
+       gint file_no;
        time_t mtime, now, expire_time;
 
        prev_dir = g_get_current_dir();
@@ -1923,8 +2181,8 @@ gint remove_expired_files(const gchar *dir, guint hours)
        expire_time = hours * 60 * 60;
 
        while ((d = readdir(dp)) != NULL) {
-               fileno = to_number(d->d_name);
-               if (fileno >= 0) {
+               file_no = to_number(d->d_name);
+               if (file_no > 0) {
                        if (stat(d->d_name, &s) < 0) {
                                FILE_OP_ERROR(d->d_name, "stat");
                                continue;
@@ -2007,14 +2265,9 @@ gint remove_dir_recursive(const gchar *dir)
                    !strcmp(d->d_name, ".."))
                        continue;
 
-               if (stat(d->d_name, &s) < 0) {
-                       FILE_OP_ERROR(d->d_name, "stat");
-                       continue;
-               }
-
                /* g_print("removing %s\n", d->d_name); */
 
-               if (S_ISDIR(s.st_mode)) {
+               if (dirent_is_directory(d)) {
                        if (remove_dir_recursive(d->d_name) < 0) {
                                g_warning("can't remove directory\n");
                                return -1;
@@ -2086,7 +2339,7 @@ gint copy_file(const gchar *src, const gchar *dest)
                while (len > 0) {
                        n_write = write(dest_fd, bufp, len);
                        if (n_write <= 0) {
-                               g_warning(_("writing to %s failed.\n"), dest);
+                               g_warning("writing to %s failed.\n", dest);
                                close(dest_fd);
                                close(src_fd);
                                unlink(dest);
@@ -2106,7 +2359,7 @@ gint copy_file(const gchar *src, const gchar *dest)
        close(dest_fd);
 
        if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
-               g_warning(_("File copy from %s to %s failed.\n"), src, dest);
+               g_warning("File copy from %s to %s failed.\n", src, dest);
                unlink(dest);
                if (dest_bak) {
                        if (rename(dest_bak, dest) < 0)
@@ -2147,14 +2400,14 @@ gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 
        if (change_file_mode_rw(dest_fp, dest) < 0) {
                FILE_OP_ERROR(dest, "chmod");
-               g_warning(_("can't change file mode\n"));
+               g_warning("can't change file mode\n");
        }
 
        while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
                if (n_read < sizeof(buf) && ferror(src_fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
-                       g_warning(_("writing to %s failed.\n"), dest);
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+                       g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
                        unlink(dest);
@@ -2215,14 +2468,14 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
 
        if (change_file_mode_rw(dest_fp, dest) < 0) {
                FILE_OP_ERROR(dest, "chmod");
-               g_warning(_("can't change file mode\n"));
+               g_warning("can't change file mode\n");
        }
 
        while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
                if (n_read < sizeof(buf) && ferror(src_fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
-                       g_warning(_("writing to %s failed.\n"), dest);
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+                       g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
                        unlink(dest);
@@ -2284,39 +2537,24 @@ gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
        return 0;
 }
 
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
 {
-       FILE *dest_fp;
        gint n_read;
        gint bytes_left, to_read;
        gchar buf[BUFSIZ];
-       gboolean err = FALSE;
 
        if (fseek(fp, offset, SEEK_SET) < 0) {
                perror("fseek");
                return -1;
        }
 
-       if ((dest_fp = fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "fopen");
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode\n");
-       }
-
-       bytes_left = length;
-       to_read = MIN(bytes_left, sizeof(buf));
+       bytes_left = length;
+       to_read = MIN(bytes_left, sizeof(buf));
 
        while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
                if (n_read < to_read && ferror(fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
-                       g_warning(_("writing to %s failed.\n"), dest);
-                       fclose(dest_fp);
-                       unlink(dest);
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        return -1;
                }
                bytes_left -= n_read;
@@ -2327,14 +2565,37 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 
        if (ferror(fp)) {
                perror("fread");
-               err = TRUE;
+               return -1;
        }
-       if (fclose(dest_fp) == EOF) {
+
+       return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+       FILE *dest_fp;
+       gboolean err = FALSE;
+
+       if ((dest_fp = fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "fopen");
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode\n");
+       }
+
+       if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
+               err = TRUE;
+
+       if (!err && fclose(dest_fp) == EOF) {
                FILE_OP_ERROR(dest, "fclose");
                err = TRUE;
        }
 
        if (err) {
+               g_warning("writing to %s failed.\n", dest);
                unlink(dest);
                return -1;
        }
@@ -2345,6 +2606,39 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
 /* convert line endings into CRLF. If the last line doesn't end with
  * linebreak, add it.
  */
+gchar *canonicalize_str(const gchar *str)
+{
+       const gchar *p;
+       guint new_len = 0;
+       gchar *out, *outp;
+
+       for (p = str; *p != '\0'; ++p) {
+               if (*p != '\r') {
+                       ++new_len;
+                       if (*p == '\n')
+                               ++new_len;
+               }
+       }
+       if (p == str || *(p - 1) != '\n')
+               new_len += 2;
+
+       out = outp = g_malloc(new_len + 1);
+       for (p = str; *p != '\0'; ++p) {
+               if (*p != '\r') {
+                       if (*p == '\n')
+                               *outp++ = '\r';
+                       *outp++ = *p;
+               }
+       }
+       if (p == str || *(p - 1) != '\n') {
+               *outp++ = '\r';
+               *outp++ = '\n';
+       }
+       *outp = '\0';
+
+       return out;
+}
+
 gint canonicalize_file(const gchar *src, const gchar *dest)
 {
        FILE *src_fp, *dest_fp;
@@ -2383,8 +2677,8 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
                        r = fputs(buf, dest_fp);
                } else {
                        if (len > 1) {
-                               r = fwrite(buf, len - 1, 1, dest_fp);
-                               if (r != 1)
+                               r = fwrite(buf, 1, len - 1, dest_fp);
+                               if (r != (len -1))
                                        r = EOF;
                        }
                        if (r != EOF)
@@ -2406,7 +2700,7 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
        }
 
        if (ferror(src_fp)) {
-               FILE_OP_ERROR(src, "fread");
+               FILE_OP_ERROR(src, "fgets");
                err = TRUE;
        }
        fclose(src_fp);
@@ -2445,6 +2739,195 @@ gint canonicalize_file_replace(const gchar *file)
        return 0;
 }
 
+gint uncanonicalize_file(const gchar *src, const gchar *dest)
+{
+       FILE *src_fp, *dest_fp;
+       gchar buf[BUFFSIZE];
+       gboolean err = FALSE;
+
+       if ((src_fp = fopen(src, "rb")) == NULL) {
+               FILE_OP_ERROR(src, "fopen");
+               return -1;
+       }
+
+       if ((dest_fp = fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "fopen");
+               fclose(src_fp);
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode\n");
+       }
+
+       while (fgets(buf, sizeof(buf), src_fp) != NULL) {
+               strcrchomp(buf);
+               if (fputs(buf, dest_fp) == EOF) {
+                       g_warning("writing to %s failed.\n", dest);
+                       fclose(dest_fp);
+                       fclose(src_fp);
+                       unlink(dest);
+                       return -1;
+               }
+       }
+
+       if (ferror(src_fp)) {
+               FILE_OP_ERROR(src, "fgets");
+               err = TRUE;
+       }
+       fclose(src_fp);
+       if (fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               unlink(dest);
+               return -1;
+       }
+
+       return 0;
+}
+
+gint uncanonicalize_file_replace(const gchar *file)
+{
+       gchar *tmp_file;
+
+       tmp_file = get_tmp_file();
+
+       if (uncanonicalize_file(file, tmp_file) < 0) {
+               g_free(tmp_file);
+               return -1;
+       }
+
+       if (move_file(tmp_file, file, TRUE) < 0) {
+               g_warning("can't replace %s .\n", file);
+               unlink(tmp_file);
+               g_free(tmp_file);
+               return -1;
+       }
+
+       g_free(tmp_file);
+       return 0;
+}
+
+gchar *normalize_newlines(const gchar *str)
+{
+       const gchar *p = str;
+       gchar *out, *outp;
+
+       out = outp = g_malloc(strlen(str) + 1);
+       for (p = str; *p != '\0'; ++p) {
+               if (*p == '\r') {
+                       if (*(p + 1) != '\n')
+                               *outp++ = '\n';
+               } else
+                       *outp++ = *p;
+       }
+
+       *outp = '\0';
+
+       return out;
+}
+
+gchar *get_outgoing_rfc2822_str(FILE *fp)
+{
+       gchar buf[BUFFSIZE];
+       GString *str;
+       gchar *ret;
+
+       str = g_string_new(NULL);
+
+       /* output header part */
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               strretchomp(buf);
+               if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
+                       gint next;
+
+                       for (;;) {
+                               next = fgetc(fp);
+                               if (next == EOF)
+                                       break;
+                               else if (next != ' ' && next != '\t') {
+                                       ungetc(next, fp);
+                                       break;
+                               }
+                               if (fgets(buf, sizeof(buf), fp) == NULL)
+                                       break;
+                       }
+               } else {
+                       g_string_append(str, buf);
+                       g_string_append(str, "\r\n");
+                       if (buf[0] == '\0')
+                               break;
+               }
+       }
+
+       /* output body part */
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               strretchomp(buf);
+               if (buf[0] == '.')
+                       g_string_append_c(str, '.');
+               g_string_append(str, buf);
+               g_string_append(str, "\r\n");
+       }
+
+       ret = str->str;
+       g_string_free(str, FALSE);
+
+       return ret;
+}
+
+/*
+ * Create a new boundary in a way that it is very unlikely that this
+ * will occur in the following text.  It would be easy to ensure
+ * uniqueness if everything is either quoted-printable or base64
+ * encoded (note that conversion is allowed), but because MIME bodies
+ * may be nested, it may happen that the same boundary has already
+ * been used. We avoid scanning the message for conflicts and hope the
+ * best.
+ *
+ *   boundary := 0*69<bchars> bcharsnospace
+ *   bchars := bcharsnospace / " "
+ *   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
+ *                    "+" / "_" / "," / "-" / "." /
+ *                    "/" / ":" / "=" / "?"
+ *
+ * some special characters removed because of buggy MTAs
+ */
+
+gchar *generate_mime_boundary(const gchar *prefix)
+{
+       static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                            "abcdefghijklmnopqrstuvwxyz"
+                            "1234567890+_./=";
+       gchar buf_uniq[17];
+       gchar buf_date[64];
+       gint i;
+       gint pid;
+
+       pid = getpid();
+
+       /* We make the boundary depend on the pid, so that all running
+        * processes generate different values even when they have been
+        * started within the same second and srandom(time(NULL)) has been
+        * used.  I can't see whether this is really an advantage but it
+        * doesn't do any harm.
+        */
+       for (i = 0; i < sizeof(buf_uniq) - 1; i++)
+               buf_uniq[i] = tbl[(rand() ^ pid) % (sizeof(tbl) - 1)];
+       buf_uniq[i] = '\0';
+
+       get_rfc822_date(buf_date, sizeof(buf_date));
+       subst_char(buf_date, ' ', '_');
+       subst_char(buf_date, ',', '_');
+       subst_char(buf_date, ':', '_');
+
+       return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
+                              buf_date, buf_uniq);
+}
+
 gint change_file_mode_rw(FILE *fp, const gchar *file)
 {
 #if HAVE_FCHMOD
@@ -2469,6 +2952,8 @@ FILE *my_tmpfile(void)
        tmpdir = get_tmp_dir();
        tmplen = strlen(tmpdir);
        progname = g_get_prgname();
+       if (progname == NULL)
+               progname = "sylpheed-claws";
        proglen = strlen(progname);
        Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
                return tmpfile());
@@ -2494,6 +2979,16 @@ FILE *my_tmpfile(void)
        return tmpfile();
 }
 
+FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
+{
+       int fd;
+       
+       *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
+       fd = mkstemp(*filename);
+
+       return fdopen(fd, "w+");
+}
+
 FILE *str_open_as_stream(const gchar *str)
 {
        FILE *fp;
@@ -2510,7 +3005,7 @@ FILE *str_open_as_stream(const gchar *str)
        len = strlen(str);
        if (len == 0) return fp;
 
-       if (fwrite(str, len, 1, fp) != 1) {
+       if (fwrite(str, 1, len, fp) != len) {
                FILE_OP_ERROR("str_open_as_stream", "fwrite");
                fclose(fp);
                return NULL;
@@ -2539,7 +3034,7 @@ gint str_write_to_file(const gchar *str, const gchar *file)
                return 0;
        }
 
-       if (fwrite(str, len, 1, fp) != 1) {
+       if (fwrite(str, 1, len, fp) != len) {
                FILE_OP_ERROR(file, "fwrite");
                fclose(fp);
                unlink(file);
@@ -2557,10 +3052,7 @@ gint str_write_to_file(const gchar *str, const gchar *file)
 
 gchar *file_read_to_str(const gchar *file)
 {
-       GByteArray *array;
        FILE *fp;
-       gchar buf[BUFSIZ];
-       gint n_read;
        gchar *str;
 
        g_return_val_if_fail(file != NULL, NULL);
@@ -2570,6 +3062,22 @@ gchar *file_read_to_str(const gchar *file)
                return NULL;
        }
 
+       str = file_read_stream_to_str(fp);
+
+       fclose(fp);
+
+       return str;
+}
+
+gchar *file_read_stream_to_str(FILE *fp)
+{
+       GByteArray *array;
+       gchar buf[BUFSIZ];
+       gint n_read;
+       gchar *str;
+
+       g_return_val_if_fail(fp != NULL, NULL);
+
        array = g_byte_array_new();
 
        while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
@@ -2579,25 +3087,33 @@ gchar *file_read_to_str(const gchar *file)
        }
 
        if (ferror(fp)) {
-               FILE_OP_ERROR(file, "fread");
-               fclose(fp);
+               FILE_OP_ERROR("file stream", "fread");
                g_byte_array_free(array, TRUE);
                return NULL;
        }
 
-       fclose(fp);
-
        buf[0] = '\0';
        g_byte_array_append(array, buf, 1);
        str = (gchar *)array->data;
        g_byte_array_free(array, FALSE);
 
+       if (!g_utf8_validate(str, -1, NULL)) {
+               const gchar *src_codeset, *dest_codeset;
+               gchar *tmp = NULL;
+               src_codeset = conv_get_locale_charset_str();
+               dest_codeset = CS_UTF_8;
+               tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
+               g_free(str);
+               str = tmp;
+       }
+
        return str;
 }
 
 gint execute_async(gchar *const argv[])
 {
        pid_t pid;
+       gint status;
 
        if ((pid = fork()) < 0) {
                perror("fork");
@@ -2622,14 +3138,18 @@ gint execute_async(gchar *const argv[])
                _exit(0);
        }
 
-       waitpid(pid, NULL, 0);
+       waitpid(pid, &status, 0);
 
-       return 0;
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       else
+               return -1;
 }
 
 gint execute_sync(gchar *const argv[])
 {
        pid_t pid;
+       gint status;
 
        if ((pid = fork()) < 0) {
                perror("fork");
@@ -2643,9 +3163,12 @@ gint execute_sync(gchar *const argv[])
                _exit(1);
        }
 
-       waitpid(pid, NULL, 0);
+       waitpid(pid, &status, 0);
 
-       return 0;
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       else
+               return -1;
 }
 
 gint execute_command_line(const gchar *cmdline, gboolean async)
@@ -2653,17 +3176,36 @@ gint execute_command_line(const gchar *cmdline, gboolean async)
        gchar **argv;
        gint ret;
 
+       debug_print("executing: %s\n", cmdline);
+
        argv = strsplit_with_quote(cmdline, " ", 0);
 
        if (async)
                ret = execute_async(argv);
        else
                ret = execute_sync(argv);
+
        g_strfreev(argv);
 
        return ret;
 }
 
+gchar *get_command_output(const gchar *cmdline)
+{
+       gchar *child_stdout;
+       gint status;
+
+       g_return_val_if_fail(cmdline != NULL, NULL);
+
+       if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status,
+                                     NULL) == FALSE) {
+               g_warning("Can't execute command: %s\n", cmdline);
+               return NULL;
+       }
+
+       return child_stdout;
+}
+
 static gint is_unchanged_uri_char(char c)
 {
        switch (c) {
@@ -2701,80 +3243,8 @@ void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
        encoded_uri[k] = 0;
 }
 
-/* Converts two-digit hexadecimal to decimal.  Used for unescaping escaped 
- * characters
- */
-static gint axtoi(const gchar *hexstr)
-{
-       gint hi, lo, result;
-       
-       hi = hexstr[0];
-       if ('0' <= hi && hi <= '9') {
-               hi -= '0';
-       } else
-               if ('a' <= hi && hi <= 'f') {
-                       hi -= ('a' - 10);
-               } else
-                       if ('A' <= hi && hi <= 'F') {
-                               hi -= ('A' - 10);
-                       }
-
-       lo = hexstr[1];
-       if ('0' <= lo && lo <= '9') {
-               lo -= '0';
-       } else
-               if ('a' <= lo && lo <= 'f') {
-                       lo -= ('a'-10);
-               } else
-                       if ('A' <= lo && lo <= 'F') {
-                               lo -= ('A' - 10);
-                       }
-       result = lo + (16 * hi);
-       return result;
-}
-
-
-/* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
- * plusses, and escape characters are used)
- */
-
-void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
-{
-       const gchar *encoded;
-       gchar *decoded;
-
-       encoded = encoded_uri;
-       decoded = decoded_uri;
-
-       while (*encoded) {
-               if (*encoded == '%') {
-                       encoded++;
-                       if (isxdigit(encoded[0])
-                           && isxdigit(encoded[1])) {
-                               *decoded = (gchar) axtoi(encoded);
-                               decoded++;
-                               encoded += 2;
-                       }
-               }
-               else if (*encoded == '+') {
-                       *decoded = ' ';
-                       decoded++;
-                       encoded++;
-               }
-               else {
-                       *decoded = *encoded;
-                       decoded++;
-                       encoded++;
-               }
-       }
-
-       *decoded = '\0';
-}
-
-
 gint open_uri(const gchar *uri, const gchar *cmdline)
 {
-       static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
        gchar buf[BUFFSIZE];
        gchar *p;
        gchar encoded_uri[BUFFSIZE];
@@ -2790,11 +3260,12 @@ gint open_uri(const gchar *uri, const gchar *cmdline)
                g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
        else {
                if (cmdline)
-                       g_warning(_("Open URI command line is invalid: `%s'"),
+                       g_warning("Open URI command line is invalid "
+                                 "(there must be only one '%%s'): %s",
                                  cmdline);
-               g_snprintf(buf, sizeof(buf), default_cmdline, encoded_uri);
+               g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
        }
-       
+
        execute_command_line(buf, TRUE);
 
        return 0;
@@ -2822,11 +3293,16 @@ time_t remote_tzoffset_sec(const gchar *zone)
        } else if (!strncmp(zone, "UT" , 2) ||
                   !strncmp(zone, "GMT", 2)) {
                remoteoffset = 0;
-       } else if (strlen(zone3) == 3 &&
-                  (p = strstr(ustzstr, zone3)) != NULL &&
-                  (p - ustzstr) % 3 == 0) {
-               iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
-               remoteoffset = iustz * 3600;
+       } else if (strlen(zone3) == 3) {
+               for (p = ustzstr; *p != '\0'; p += 3) {
+                       if (!g_ascii_strncasecmp(p, zone3, 3)) {
+                               iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
+                               remoteoffset = iustz * 3600;
+                               break;
+                       }
+               }
+               if (*p == '\0')
+                       return -1;
        } else if (strlen(zone3) == 1) {
                switch (zone[0]) {
                case 'Z': remoteoffset =   0; break;
@@ -2857,7 +3333,8 @@ time_t remote_tzoffset_sec(const gchar *zone)
                default:  remoteoffset =   0; break;
                }
                remoteoffset = remoteoffset * 3600;
-       }
+       } else
+               return -1;
 
        return remoteoffset;
 }
@@ -2885,10 +3362,6 @@ time_t tzoffset_sec(time_t *now)
                off = 23 * 60 + 59;     /* if not, insert silly value */
        if (off <= -24 * 60)
                off = -(23 * 60 + 59);
-       if (off > 12 * 60)
-               off -= 24 * 60;
-       if (off < -12 * 60)
-               off += 24 * 60;
 
        return off * 60;
 }
@@ -2949,7 +3422,7 @@ void debug_set_mode(gboolean mode)
        debug_mode = mode;
 }
 
-gboolean debug_get_mode()
+gboolean debug_get_mode(void)
 {
        return debug_mode;
 }
@@ -2965,36 +3438,26 @@ void debug_print_real(const gchar *format, ...)
        g_vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
 
-       fputs(buf, stdout);
+       g_print("%s", buf);
 }
 
 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
 {
        if (subject == NULL)
                subject = "";
-
-       if (g_strncasecmp(subject, "Re: ", 4) == 0)
-               return g_hash_table_lookup(subject_table, subject + 4);
        else
-               return g_hash_table_lookup(subject_table, subject);
+               subject += subject_get_prefix_length(subject);
+
+       return g_hash_table_lookup(subject_table, subject);
 }
 
 void subject_table_insert(GHashTable *subject_table, gchar * subject,
                          void * data)
 {
-       if (subject == NULL)
+       if (subject == NULL || *subject == 0)
                return;
-       if (* subject == 0)
-               return;
-       if (g_strcasecmp(subject, "Re:") == 0)
-               return;
-       if (g_strcasecmp(subject, "Re: ") == 0)
-               return;
-
-       if (g_strncasecmp(subject, "Re: ", 4) == 0)
-               g_hash_table_insert(subject_table, subject + 4, data);
-       else
-               g_hash_table_insert(subject_table, subject, data);
+       subject += subject_get_prefix_length(subject);
+       g_hash_table_insert(subject_table, subject, data);
 }
 
 void subject_table_remove(GHashTable *subject_table, gchar * subject)
@@ -3002,230 +3465,481 @@ void subject_table_remove(GHashTable *subject_table, gchar * subject)
        if (subject == NULL)
                return;
 
-       if (g_strncasecmp(subject, "Re: ", 4) == 0)
-               g_hash_table_remove(subject_table, subject + 4);
+       subject += subject_get_prefix_length(subject);  
+       g_hash_table_remove(subject_table, subject);
+}
+
+/*!
+ *\brief       Check if a string is prefixed with known (combinations) 
+ *             of prefixes. The function assumes that each prefix 
+ *             is terminated by zero or exactly _one_ space.
+ *
+ *\param       str String to check for a prefixes
+ *
+ *\return      int Number of chars in the prefix that should be skipped 
+ *             for a "clean" subject line. If no prefix was found, 0
+ *             is returned.
+ */            
+int subject_get_prefix_length(const gchar *subject)
+{
+       /*!< Array with allowable reply prefixes regexps. */
+       static const gchar * const prefixes[] = {
+               "Re\\:",                        /* "Re:" */
+               "Re\\[[1-9][0-9]*\\]\\:",       /* "Re[XXX]:" (non-conforming news mail clients) */
+               "Antw\\:",                      /* "Antw:" (Dutch / German Outlook) */
+               "Aw\\:",                        /* "Aw:"   (German) */
+               "Antwort\\:",                   /* "Antwort:" (German Lotus Notes) */
+               "Res\\:",                       /* "Res:" (Brazilian Outlook) */
+               "Fw\\:",                        /* "Fw:" Forward */
+               "Enc\\:",                       /* "Enc:" Forward (Brazilian Outlook) */
+               "Odp\\:",                       /* "Odp:" Re (Polish Outlook) */
+               "Rif\\:"                        /* "Rif:" (Italian Outlook) */
+               /* add more */
+       };
+       const int PREFIXES = sizeof prefixes / sizeof prefixes[0];
+       int n;
+       regmatch_t pos;
+       static regex_t regex;
+       static gboolean init_;
+
+       if (!subject) return 0;
+       if (!*subject) return 0;
+
+       if (!init_) {
+               GString *s = g_string_new("");
+               
+               for (n = 0; n < PREFIXES; n++)
+                       /* Terminate each prefix regexpression by a
+                        * "\ ?" (zero or ONE space), and OR them */
+                       g_string_append_printf(s, "(%s\\ ?)%s",
+                                         prefixes[n],
+                                         n < PREFIXES - 1 ? 
+                                         "|" : "");
+               
+               g_string_prepend(s, "(");
+               g_string_append(s, ")+");       /* match at least once */
+               g_string_prepend(s, "^\\ *");   /* from beginning of line */
+               
+
+               /* We now have something like "^\ *((PREFIX1\ ?)|(PREFIX2\ ?))+" 
+                * TODO: Should this be       "^\ *(((PREFIX1)|(PREFIX2))\ ?)+" ??? */
+               if (regcomp(&regex, s->str, REG_EXTENDED | REG_ICASE)) { 
+                       debug_print("Error compiling regexp %s\n", s->str);
+                       g_string_free(s, TRUE);
+                       return 0;
+               } else {
+                       init_ = TRUE;
+                       g_string_free(s, TRUE);
+               }
+       }
+       
+       if (!regexec(&regex, subject, 1, &pos, 0) && pos.rm_so != -1)
+               return pos.rm_eo;
        else
-               g_hash_table_remove(subject_table, subject);
+               return 0;
 }
 
-gboolean subject_is_reply(const gchar *subject)
+guint g_stricase_hash(gconstpointer gptr)
 {
-       /* XXX: just simply here so someone can handle really
-        * advanced Re: detection like "Re[4]", "ANTW:" or
-        * Re: Re: Re: Re: Re: Re: Re: Re:" stuff. */
-       if (subject == NULL) return FALSE;
-       else return 0 == g_strncasecmp(subject, "Re: ", 4);
+       guint hash_result = 0;
+       const char *str;
+
+       for (str = gptr; str && *str; str++) {
+               if (isupper((guchar)*str)) hash_result += (*str + ' ');
+               else hash_result += *str;
+       }
+
+       return hash_result;
 }
 
-FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
+gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
 {
-       int fd;
-       
-       *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
-       fd = mkstemp(*filename);
+       const char *str1 = gptr1;
+       const char *str2 = gptr2;
 
-       return fdopen(fd, "w+");
+       return !g_utf8_collate(str1, str2);
 }
 
-/* allow Mutt-like patterns in quick search */
-gchar *expand_search_string(const gchar *search_string)
-{
-       int i, len, new_len = 0;
-       gchar term_char, save_char;
-       gchar *cmd_start, *cmd_end;
-       gchar *new_str = NULL;
-       gchar *copy_str;
-       gboolean casesens, dontmatch;
-       /* list of allowed pattern abbreviations */
-       struct {
-               gchar           *abbreviated;   /* abbreviation */
-               gchar           *command;       /* actual matcher command */ 
-               gint            numparams;      /* number of params for cmd */
-               gboolean        qualifier;      /* do we append regexpcase */
-               gboolean        quotes;         /* do we need quotes */
-       }
-       cmds[] = {
-               { "a",  "all",                          0,      FALSE,  FALSE },
-               { "ag", "age_greater",                  1,      FALSE,  FALSE },
-               { "al", "age_lower",                    1,      FALSE,  FALSE },
-               { "b",  "body_part",                    1,      TRUE,   TRUE  },
-               { "B",  "message",                      1,      TRUE,   TRUE  },
-               { "c",  "cc",                           1,      TRUE,   TRUE  },
-               { "C",  "to_or_cc",                     1,      TRUE,   TRUE  },
-               { "D",  "deleted",                      0,      FALSE,  FALSE },
-               { "e",  "header \"Sender\"",            1,      TRUE,   TRUE  },
-               { "E",  "execute",                      1,      FALSE,  TRUE  },
-               { "f",  "from",                         1,      TRUE,   TRUE  },
-               { "F",  "forwarded",                    0,      FALSE,  FALSE },
-               { "h",  "headers_part",                 1,      TRUE,   TRUE  },
-               { "i",  "header \"Message-Id\"",        1,      TRUE,   TRUE  },
-               { "I",  "inreplyto",                    1,      TRUE,   TRUE  },
-               { "n",  "newsgroups",                   1,      TRUE,   TRUE  },
-               { "N",  "new",                          0,      FALSE,  FALSE },
-               { "O",  "~new",                         0,      FALSE,  FALSE },
-               { "r",  "replied",                      0,      FALSE,  FALSE },
-               { "R",  "~unread",                      0,      FALSE,  FALSE },
-               { "s",  "subject",                      1,      TRUE,   TRUE  },
-               { "se", "score_equal",                  1,      FALSE,  FALSE },
-               { "sg", "score_greater",                1,      FALSE,  FALSE },
-               { "sl", "score_lower",                  1,      FALSE,  FALSE },
-               { "Se", "size_equal",                   1,      FALSE,  FALSE },
-               { "Sg", "size_greater",                 1,      FALSE,  FALSE },
-               { "Ss", "size_smaller",                 1,      FALSE,  FALSE },
-               { "t",  "to",                           1,      TRUE,   TRUE  },
-               { "T",  "marked",                       0,      FALSE,  FALSE },
-               { "U",  "unread",                       0,      FALSE,  FALSE },
-               { "x",  "header \"References\"",        1,      TRUE,   TRUE  },
-               { "y",  "header \"X-Label\"",           1,      TRUE,   TRUE  },
-               { "&",  "&",                            0,      FALSE,  FALSE },
-               { "|",  "|",                            0,      FALSE,  FALSE },
-               { NULL, NULL,                           0,      FALSE,  FALSE }
-       };
-
-       if (search_string == NULL)
-               return NULL;
+gint g_int_compare(gconstpointer a, gconstpointer b)
+{
+       return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
+}
 
-       copy_str = g_strdup(search_string);
-
-       /* if it's a full command don't process it so users
-          can still do something like from regexpcase "foo" */
-       for (i = 0; cmds[i].command; i++) {
-               gchar *tmp_search_string = search_string;
-               cmd_start = cmds[i].command;
-               /* allow logical NOT */
-               if (*tmp_search_string == '~')
-                       tmp_search_string++;
-               if (!strncmp(tmp_search_string, cmd_start, strlen(cmd_start)))
-                       break;
-       }
-       if (cmds[i].command)
-               return copy_str;
+gchar *generate_msgid(gchar *buf, gint len)
+{
+       struct tm *lt;
+       time_t t;
+       gchar *addr;
 
-       cmd_start = cmd_end = copy_str;
-       while (cmd_end && *cmd_end) {
-               /* skip all white spaces */
-               while (*cmd_end && isspace(*cmd_end))
-                       cmd_end++;
+       t = time(NULL);
+       lt = localtime(&t);
 
-               /* extract a command */
-               while (*cmd_end && !isspace(*cmd_end))
-                       cmd_end++;
+       addr = g_strconcat("@", get_domain_name(), NULL);
 
-               /* save character */
-               save_char = *cmd_end;
-               *cmd_end = '\0';
+       g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x%s",
+                  lt->tm_year + 1900, lt->tm_mon + 1,
+                  lt->tm_mday, lt->tm_hour,
+                  lt->tm_min, lt->tm_sec,
+                  (guint) rand(), addr);
 
-               dontmatch = FALSE;
-               casesens = FALSE;
+       g_free(addr);
+       return buf;
+}
 
-               /* ~ and ! mean logical NOT */
-               if (*cmd_start == '~' || *cmd_start == '!')
-               {
-                       dontmatch = TRUE;
-                       cmd_start++;
+/*
+   quote_cmd_argument()
+   
+   return a quoted string safely usable in argument of a command.
+   
+   code is extracted and adapted from etPan! project -- DINH V. HoĆ .
+*/
+
+gint quote_cmd_argument(gchar * result, guint size,
+                       const gchar * path)
+{
+       const gchar * p;
+       gchar * result_p;
+       guint remaining;
+
+       result_p = result;
+       remaining = size;
+
+       for(p = path ; * p != '\0' ; p ++) {
+
+               if (isalnum((guchar)*p) || (* p == '/')) {
+                       if (remaining > 0) {
+                               * result_p = * p;
+                               result_p ++; 
+                               remaining --;
+                       }
+                       else {
+                               result[size - 1] = '\0';
+                               return -1;
+                       }
                }
-               /* % means case sensitive match */
-               if (*cmd_start == '%')
-               {
-                       casesens = TRUE;
-                       cmd_start++;
+               else { 
+                       if (remaining >= 2) {
+                               * result_p = '\\';
+                               result_p ++; 
+                               * result_p = * p;
+                               result_p ++; 
+                               remaining -= 2;
+                       }
+                       else {
+                               result[size - 1] = '\0';
+                               return -1;
+                       }
                }
+       }
+       if (remaining > 0) {
+               * result_p = '\0';
+       }
+       else {
+               result[size - 1] = '\0';
+               return -1;
+       }
+  
+       return 0;
+}
 
-               /* find matching abbreviation */
-               for (i = 0; cmds[i].command; i++) {
-                       if (!strcmp(cmd_start, cmds[i].abbreviated)) {
-                               /* restore character */
-                               *cmd_end = save_char;
-                               len = strlen(cmds[i].command) + 1;
-                               if (dontmatch)
-                                       len++;
-                               if (casesens)
-                                       len++;
-
-                               /* copy command */
-                               if (new_str) {
-                                       new_len += 1;
-                                       new_str = g_realloc(new_str, new_len);
-                                       strcat(new_str, " ");
-                               }
-                               new_len += (len + 1);
-                               new_str = g_realloc(new_str, new_len);
-                               if (new_len == len + 1)
-                                       *new_str = '\0';
-                               if (dontmatch)
-                                       strcat(new_str, "~");
-                               strcat(new_str, cmds[i].command);
-                               strcat(new_str, " ");
-
-                               /* stop if no params required */
-                               if (cmds[i].numparams == 0)
-                                       break;
+typedef struct 
+{
+       GNode           *parent;
+       GNodeMapFunc     func;
+       gpointer         data;
+} GNodeMapData;
 
-                               /* extract a parameter, allow quotes */
-                               cmd_end++;
-                               cmd_start = cmd_end;
-                               if (*cmd_start == '"') {
-                                       term_char = '"';
-                                       cmd_end++;
-                               }
-                               else
-                                       term_char = ' ';
+static void g_node_map_recursive(GNode *node, gpointer data)
+{
+       GNodeMapData *mapdata = (GNodeMapData *) data;
+       GNode *newnode;
+       GNodeMapData newmapdata;
+       gpointer newdata;
 
-                               /* extract actual parameter */
-                               while ((*cmd_end) && (*cmd_end != term_char))
-                                       cmd_end++;
+       newdata = mapdata->func(node->data, mapdata->data);
+       if (newdata != NULL) {
+               newnode = g_node_new(newdata);
+               g_node_append(mapdata->parent, newnode);
 
-                               if (*cmd_end && (*cmd_end != term_char))
-                                       break;
+               newmapdata.parent = newnode;
+               newmapdata.func = mapdata->func;
+               newmapdata.data = mapdata->data;
 
-                               if (*cmd_end == '"')
-                                       cmd_end++;
+               g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &newmapdata);
+       }
+}
 
-                               save_char = *cmd_end;
-                               *cmd_end = '\0';
+GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
+{
+       GNode *root;
+       GNodeMapData mapdata;
 
-                               new_len += strlen(cmd_start);
+       g_return_val_if_fail(node != NULL, NULL);
+       g_return_val_if_fail(func != NULL, NULL);
 
-                               /* do we need to add regexpcase ? */
-                               if (cmds[i].qualifier)
-                                       new_len += 10; /* "regexpcase " */
+       root = g_node_new(func(node->data, data));
 
-                               if (term_char != '"')
-                                       new_len += 2;
-                               new_str = g_realloc(new_str, new_len);
+       mapdata.parent = root;
+       mapdata.func = func;
+       mapdata.data = data;
 
-                               if (cmds[i].qualifier) {
-                                       if (casesens)
-                                               strcat(new_str, "regexp ");
-                                       else
-                                               strcat(new_str, "regexpcase ");
-                               }
+       g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &mapdata);
 
-                               /* do we need to add quotes ? */
-                               if (cmds[i].quotes && term_char != '"')
-                                       strcat(new_str, "\"");
+       return root;
+}
 
-                               /* copy actual parameter */
-                               strcat(new_str, cmd_start);
+#define HEX_TO_INT(val, hex)                   \
+{                                              \
+       gchar c = hex;                          \
+                                               \
+       if ('0' <= c && c <= '9') {             \
+               val = c - '0';                  \
+       } else if ('a' <= c && c <= 'f') {      \
+               val = c - 'a' + 10;             \
+       } else if ('A' <= c && c <= 'F') {      \
+               val = c - 'A' + 10;             \
+       } else {                                \
+               val = -1;                       \
+       }                                       \
+}
 
-                               /* do we need to add quotes ? */
-                               if (cmds[i].quotes && term_char != '"')
-                                       strcat(new_str, "\"");
+gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
+{
+       gint hi, lo;
 
-                               /* restore original character */
-                               *cmd_end = save_char;
+       HEX_TO_INT(hi, c1);
+       HEX_TO_INT(lo, c2);
 
-                               break;
-                       }
-               }
+       if (hi == -1 || lo == -1)
+               return FALSE;
 
-               if (*cmd_end) {
-                       cmd_end++;
-                       cmd_start = cmd_end;
-               }
-       }
+       *out = (hi << 4) + lo;
+       return TRUE;
+}
 
-       g_free(copy_str);
-       return new_str;
+#define INT_TO_HEX(hex, val)           \
+{                                      \
+       if ((val) < 10)                 \
+               hex = '0' + (val);      \
+       else                            \
+               hex = 'A' + (val) - 10; \
+}
+
+void get_hex_str(gchar *out, guchar ch)
+{
+       gchar hex;
+
+       INT_TO_HEX(hex, ch >> 4);
+       *out++ = hex;
+       INT_TO_HEX(hex, ch & 0x0f);
+       *out++ = hex;
+}
+
+#undef REF_DEBUG
+#ifndef REF_DEBUG
+#define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
+#else
+#define G_PRINT_REF g_print
+#endif
+
+/*!
+ *\brief       Register ref counted pointer. It is based on GBoxed, so should
+ *             work with anything that uses the GType system. The semantics
+ *             are similar to a C++ auto pointer, with the exception that
+ *             C doesn't have automatic closure (calling destructors) when 
+ *             exiting a block scope.
+ *             Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
+ *             function directly.
+ *
+ *\return      GType A GType type.
+ */
+GType g_auto_pointer_register(void)
+{
+       static GType auto_pointer_type;
+       if (!auto_pointer_type)
+               auto_pointer_type =
+                       g_boxed_type_register_static
+                               ("G_TYPE_AUTO_POINTER",
+                                (GBoxedCopyFunc) g_auto_pointer_copy,
+                                (GBoxedFreeFunc) g_auto_pointer_free);
+       return auto_pointer_type;                                                    
 }
 
+/*!
+ *\brief       Structure with g_new() allocated pointer guarded by the
+ *             auto pointer
+ */
+typedef struct AutoPointerRef {
+       void          (*free) (gpointer);
+       gpointer        pointer;
+       glong           cnt;
+} AutoPointerRef;
+
+/*!
+ *\brief       The auto pointer opaque structure that references the
+ *             pointer guard block.
+ */
+typedef struct AutoPointer {
+       AutoPointerRef *ref;
+       gpointer        ptr; /*!< access to protected pointer */
+} AutoPointer;
+
+/*!
+ *\brief       Creates an auto pointer for a g_new()ed pointer. Example:
+ *
+ *\code        
+ *
+ *             ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
+ *             ... when assigning, copying and freeing storage elements
+ *
+ *             gtk_list_store_new(N_S_COLUMNS, 
+ *                                G_TYPE_AUTO_POINTER,
+ *                                -1);
+ *
+ *
+ *             Template *precious_data = g_new0(Template, 1);
+ *             g_pointer protect = g_auto_pointer_new(precious_data);
+ *
+ *             gtk_list_store_set(container, &iter,
+ *                                S_DATA, protect,
+ *                                -1);
+ *
+ *             ... the gtk_list_store has copied the pointer and 
+ *             ... incremented its reference count, we should free
+ *             ... the auto pointer (in C++ a destructor would do
+ *             ... this for us when leaving block scope)
+ * 
+ *             g_auto_pointer_free(protect);
+ *
+ *             ... gtk_list_store_set() now manages the data. When
+ *             ... *explicitly* requesting a pointer from the list 
+ *             ... store, don't forget you get a copy that should be 
+ *             ... freed with g_auto_pointer_free() eventually.
+ *
+ *\endcode
+ *
+ *\param       pointer Pointer to be guarded.
+ *
+ *\return      GAuto * Pointer that should be used in containers with
+ *             GType support.
+ */
+GAuto *g_auto_pointer_new(gpointer p)
+{
+       AutoPointerRef *ref;
+       AutoPointer    *ptr;
+       
+       if (p == NULL) 
+               return NULL;
+
+       ref = g_new0(AutoPointerRef, 1);
+       ptr = g_new0(AutoPointer, 1);
+
+       ref->pointer = p;
+       ref->free = g_free;
+       ref->cnt = 1;
+
+       ptr->ref = ref;
+       ptr->ptr = p;
+
+#ifdef REF_DEBUG
+       G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
+#endif
+       return ptr;
+}
+
+/*!
+ *\brief       Allocate an autopointer using the passed \a free function to
+ *             free the guarded pointer
+ */
+GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
+{
+       AutoPointer *aptr;
+       
+       if (p == NULL)
+               return NULL;
+
+       aptr = g_auto_pointer_new(p);
+       aptr->ref->free = free_;
+       return aptr; 
+}
+
+gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
+{
+       if (auto_ptr == NULL) 
+               return NULL;
+       return ((AutoPointer *) auto_ptr)->ptr; 
+}
+
+/*!
+ *\brief       Copies an auto pointer by. It's mostly not necessary
+ *             to call this function directly, unless you copy/assign
+ *             the guarded pointer.
+ *
+ *\param       auto_ptr Auto pointer returned by previous call to 
+ *             g_auto_pointer_new_XXX()
+ *
+ *\return      gpointer An auto pointer
+ */
+GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
+{
+       AutoPointer     *ptr;
+       AutoPointerRef  *ref;
+       AutoPointer     *newp;
+
+       if (auto_ptr == NULL) 
+               return NULL;
+
+       ptr = auto_ptr;
+       ref = ptr->ref;
+       newp = g_new0(AutoPointer, 1);
+
+       newp->ref = ref;
+       newp->ptr = ref->pointer;
+       ++(ref->cnt);
+       
+#ifdef REF_DEBUG
+       G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+#endif
+       return newp;
+}
+
+/*!
+ *\brief       Free an auto pointer
+ */
+void g_auto_pointer_free(GAuto *auto_ptr)
+{
+       AutoPointer     *ptr;
+       AutoPointerRef  *ref;
+       
+       if (auto_ptr == NULL)
+               return;
+
+       ptr = auto_ptr;
+       ref = ptr->ref;
+
+       if (--(ref->cnt) == 0) {
+#ifdef REF_DEBUG
+               G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+#endif
+               ref->free(ref->pointer);
+               g_free(ref);
+       } 
+#ifdef REF_DEBUG
+       else
+               G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+#endif
+       g_free(ptr);            
+}
+
+void replace_returns(gchar *str)
+{
+       if (!str)
+               return;
+
+       while (strstr(str, "\n")) {
+               *strstr(str, "\n") = ' ';
+       }
+       while (strstr(str, "\r")) {
+               *strstr(str, "\r") = ' ';
+       }
+}