2005-07-01 [colin] 1.9.12cvs8
[claws.git] / src / common / utils.c
index 8ca8fa06e62b6ae3f26fae4a70c96ba44225031c..ffe51f67c750f59fcaf7e6f1f3d940ff5de9c578 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * 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>
@@ -42,8 +43,8 @@
 #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"
@@ -113,7 +114,7 @@ void hash_free_value_mem(GHashTable *table)
 
 gint str_case_equal(gconstpointer v, gconstpointer v2)
 {
-       return strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
+       return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
 }
 
 guint str_case_hash(gconstpointer key)
@@ -143,6 +144,26 @@ 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 guchar *p;
@@ -277,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++;
@@ -288,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
@@ -500,11 +538,9 @@ gint get_mbs_len(const gchar *s)
                return -1;
 
        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 -1;
                else
                        len++;
 
@@ -571,12 +607,12 @@ gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
        trim_subject_for_sort(str1);
        trim_subject_for_sort(str2);
 
-       return strcasecmp(str1, str2);
+       return g_utf8_collate(str1, str2);
 }
 
 void trim_subject_for_compare(gchar *str)
 {
-       guchar *srcp;
+       gchar *srcp;
 
        eliminate_parenthesis(str, '[', ']');
        eliminate_parenthesis(str, '(', ')');
@@ -589,7 +625,7 @@ void trim_subject_for_compare(gchar *str)
 
 void trim_subject_for_sort(gchar *str)
 {
-       guchar *srcp;
+       gchar *srcp;
 
        g_strstrip(str);
 
@@ -600,34 +636,38 @@ void trim_subject_for_sort(gchar *str)
 
 void trim_subject(gchar *str)
 {
-       register guchar *srcp, *destp;
+       register guchar *srcp;
        gchar op, cl;
        gint in_brace;
+       
+       g_strstrip(str);
 
-       destp = str + subject_get_prefix_length(str);
+       srcp = str + subject_get_prefix_length(str);
 
-       if (*destp == '[') {
+       if (*srcp == '[') {
                op = '[';
                cl = ']';
-       } else if (*destp == '(') {
+       } else if (*srcp == '(') {
                op = '(';
                cl = ')';
        } else
-               return;
+               op = 0;
 
-       srcp = destp + 1;
-       in_brace = 1;
-       while (*srcp) {
-               if (*srcp == op)
-                       in_brace++;
-               else if (*srcp == cl)
-                       in_brace--;
-               srcp++;
-               if (in_brace == 0)
-                       break;
+       if (op) {
+               ++srcp;
+               in_brace = 1;
+               while (*srcp) {
+                       if (*srcp == op)
+                               in_brace++;
+                       else if (*srcp == cl)
+                               in_brace--;
+                       srcp++;
+                       if (in_brace == 0)
+                               break;
+               }
        }
        while (isspace(*srcp)) srcp++;
-       memmove(destp, srcp, strlen(srcp) + 1);
+       memmove(str, srcp, strlen(srcp) + 1);
 }
 
 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
@@ -881,7 +921,7 @@ GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
        return address_list_append_real(addr_list, str, FALSE);
 }
 
-GSList *references_list_append(GSList *msgid_list, const gchar *str)
+GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
 {
        const gchar *strp;
 
@@ -901,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);
 
@@ -911,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;
@@ -1037,13 +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, "|&;()<>'!{}[]",'_');
+       subst_chars(str, " \"'|&;()<>'!{}[]",'_');
 }
 
 gboolean is_header_line(const gchar *str)
@@ -1378,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;
        }
 
@@ -1401,7 +1456,6 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
        GList *result = NULL;
        const gchar *p, *q;
        gchar *escaped_utf8uri;
-       gchar *file;
 
        p = uri_list;
 
@@ -1425,7 +1479,6 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
                                        strncpy(escaped_utf8uri, p, q - p + 1);
                                        escaped_utf8uri[q - p + 1] = '\0';
                                        decode_uri(file, escaped_utf8uri);
-#warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
                     /*
                     * g_filename_from_uri() rejects escaped/locale encoded uri
                     * string which come from Nautilus.
@@ -1435,7 +1488,7 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
                                                        = conv_codeset_strdup(
                                                                file + 5,
                                                                CS_UTF_8,
-                                                               conv_get_current_charset_str());
+                                                               conv_get_locale_charset_str());
                                        if (!locale_file)
                                                locale_file = g_strdup(file + 5);
                                        result = g_list_append(result, locale_file);
@@ -1449,21 +1502,6 @@ 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
  */
@@ -1498,24 +1536,39 @@ static gint axtoi(const gchar *hexstr)
 
 gboolean is_uri_string(const gchar *str)
 {
-       return (g_strncasecmp(str, "http://", 7) == 0 ||
-               g_strncasecmp(str, "https://", 8) == 0 ||
-               g_strncasecmp(str, "ftp://", 6) == 0 ||
-               g_strncasecmp(str, "www.", 4) == 0);
+       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_strncasecmp(uri, "http://", 7) == 0)
+       if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
                return (gchar *)(uri + 7);
-       else if (g_strncasecmp(uri, "https://", 8) == 0)
+       else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
                return (gchar *)(uri + 8);
-       else if (g_strncasecmp(uri, "ftp://", 6) == 0)
+       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)
  */
@@ -1586,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);
                }
@@ -1738,17 +1791,16 @@ 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);
                        }
@@ -2013,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();
 
@@ -2030,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)
@@ -2057,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();
 
@@ -2074,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)
@@ -2108,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();
@@ -2129,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;
@@ -2354,7 +2406,7 @@ gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
        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) {
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
@@ -2422,7 +2474,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
        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) {
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
@@ -2485,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));
 
        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;
@@ -2528,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;
        }
@@ -2617,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)
@@ -2782,7 +2842,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
        /* output header part */
        while (fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
-               if (!g_strncasecmp(buf, "Bcc:", 4)) {
+               if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
                        gint next;
 
                        for (;;) {
@@ -2856,7 +2916,7 @@ gchar *generate_mime_boundary(const gchar *prefix)
         * doesn't do any harm.
         */
        for (i = 0; i < sizeof(buf_uniq) - 1; i++)
-               buf_uniq[i] = tbl[(random() ^ pid) % (sizeof(tbl) - 1)];
+               buf_uniq[i] = tbl[(rand() ^ pid) % (sizeof(tbl) - 1)];
        buf_uniq[i] = '\0';
 
        get_rfc822_date(buf_date, sizeof(buf_date));
@@ -2864,7 +2924,7 @@ gchar *generate_mime_boundary(const gchar *prefix)
        subst_char(buf_date, ',', '_');
        subst_char(buf_date, ':', '_');
 
-       return g_strdup_printf("%s=_%s_%s", prefix ? prefix : "Multipart",
+       return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
                               buf_date, buf_uniq);
 }
 
@@ -2892,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());
@@ -2943,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;
@@ -2972,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);
@@ -3038,7 +3100,7 @@ gchar *file_read_stream_to_str(FILE *fp)
        if (!g_utf8_validate(str, -1, NULL)) {
                const gchar *src_codeset, *dest_codeset;
                gchar *tmp = NULL;
-               src_codeset = conv_get_current_charset_str();
+               src_codeset = conv_get_locale_charset_str();
                dest_codeset = CS_UTF_8;
                tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
                g_free(str);
@@ -3130,39 +3192,18 @@ gint execute_command_line(const gchar *cmdline, gboolean async)
 
 gchar *get_command_output(const gchar *cmdline)
 {
-       gchar buf[BUFFSIZE];
-       FILE *fp;
-       GString *str;
-       gchar *ret;
+       gchar *child_stdout;
+       gint status;
 
        g_return_val_if_fail(cmdline != NULL, NULL);
 
-       if ((fp = popen(cmdline, "r")) == NULL) {
-               FILE_OP_ERROR(cmdline, "popen");
+       if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status,
+                                     NULL) == FALSE) {
+               g_warning("Can't execute command: %s\n", cmdline);
                return NULL;
        }
 
-       str = g_string_new("");
-
-       while (fgets(buf, sizeof(buf), fp) != NULL)
-               g_string_append(str, buf);
-
-       pclose(fp);
-
-       ret = str->str;
-       g_string_free(str, FALSE);
-
-       if (!g_utf8_validate(ret, -1, NULL)) {
-               const gchar *src_codeset, *dest_codeset;
-               gchar *tmp = NULL;
-               src_codeset = conv_get_current_charset_str();
-               dest_codeset = CS_UTF_8;
-               tmp = conv_codeset_strdup(ret, src_codeset, dest_codeset);
-               g_free(ret);
-               ret = tmp;
-       }
-       
-       return ret;
+       return child_stdout;
 }
 
 static gint is_unchanged_uri_char(char c)
@@ -3254,7 +3295,7 @@ time_t remote_tzoffset_sec(const gchar *zone)
                remoteoffset = 0;
        } else if (strlen(zone3) == 3) {
                for (p = ustzstr; *p != '\0'; p += 3) {
-                       if (!strncasecmp(p, zone3, 3)) {
+                       if (!g_ascii_strncasecmp(p, zone3, 3)) {
                                iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
                                remoteoffset = iustz * 3600;
                                break;
@@ -3397,7 +3438,7 @@ 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)
@@ -3470,7 +3511,7 @@ int subject_get_prefix_length(const gchar *subject)
                for (n = 0; n < PREFIXES; n++)
                        /* Terminate each prefix regexpression by a
                         * "\ ?" (zero or ONE space), and OR them */
-                       g_string_sprintfa(s, "(%s\\ ?)%s",
+                       g_string_append_printf(s, "(%s\\ ?)%s",
                                          prefixes[n],
                                          n < PREFIXES - 1 ? 
                                          "|" : "");
@@ -3516,7 +3557,7 @@ gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
        const char *str1 = gptr1;
        const char *str2 = gptr2;
 
-       return !strcasecmp(str1, str2);
+       return !g_utf8_collate(str1, str2);
 }
 
 gint g_int_compare(gconstpointer a, gconstpointer b)
@@ -3524,9 +3565,8 @@ gint g_int_compare(gconstpointer a, gconstpointer b)
        return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
 }
 
-gchar *generate_msgid(const gchar *address, gchar *buf, gint len)
+gchar *generate_msgid(gchar *buf, gint len)
 {
-       /* steal from compose.c::compose_generate_msgid() */
        struct tm *lt;
        time_t t;
        gchar *addr;
@@ -3534,26 +3574,18 @@ gchar *generate_msgid(const gchar *address, gchar *buf, gint len)
        t = time(NULL);
        lt = localtime(&t);
 
-       if (address && *address) {
-               if (strchr(address, '@'))
-                       addr = g_strdup(address);
-               else
-                       addr = g_strconcat(address, "@", get_domain_name(), NULL);
-       } else
-               addr = g_strconcat(g_get_user_name(), "@", get_domain_name(),
-                                  NULL);
+       addr = g_strconcat("@", get_domain_name(), NULL);
 
-       g_snprintf(buf, len, "%04d%02d%02d%02d%02d%02d.%08x.%s",
+       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)random(), addr);
+                  (guint) rand(), addr);
 
        g_free(addr);
        return buf;
 }
 
-
 /*
    quote_cmd_argument()
    
@@ -3655,3 +3687,259 @@ GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
 
        return root;
 }
+
+#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;                       \
+       }                                       \
+}
+
+gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
+{
+       gint hi, lo;
+
+       HEX_TO_INT(hi, c1);
+       HEX_TO_INT(lo, c2);
+
+       if (hi == -1 || lo == -1)
+               return FALSE;
+
+       *out = (hi << 4) + lo;
+       return TRUE;
+}
+
+#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") = ' ';
+       }
+}