aaae7b22b00bcb170a4b8d8df755bd89e50348d0
[claws.git] / src / common / utils.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The code of the g_utf8_substring function below is owned by
19  * Matthias Clasen <matthiasc@src.gnome.org>/<mclasen@redhat.com>
20  * and is got from GLIB 2.30: https://git.gnome.org/browse/glib/commit/
21  *  ?h=glib-2-30&id=9eb65dd3ed5e1a9638595cbe10699c7606376511
22  *
23  * GLib 2.30 is licensed under GPL v2 or later and:
24  * Copyright (C) 1999 Tom Tromey
25  * Copyright (C) 2000 Red Hat, Inc.
26  *
27  * https://git.gnome.org/browse/glib/tree/glib/gutf8.c
28  *  ?h=glib-2-30&id=9eb65dd3ed5e1a9638595cbe10699c7606376511
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #  include "config.h"
33 #include "claws-features.h"
34 #endif
35
36 #include "defs.h"
37
38 #include <glib.h>
39 #include <gio/gio.h>
40
41 #include <glib/gi18n.h>
42
43 #ifdef USE_PTHREAD
44 #include <pthread.h>
45 #endif
46
47 #include <stdio.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <sys/param.h>
52 #ifndef G_OS_WIN32
53 #include <sys/socket.h>
54 #endif
55
56 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
57 #  include <wchar.h>
58 #  include <wctype.h>
59 #endif
60 #include <stdlib.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63 #include <stdarg.h>
64 #include <sys/types.h>
65 #if HAVE_SYS_WAIT_H
66 #  include <sys/wait.h>
67 #endif
68 #include <dirent.h>
69 #include <time.h>
70 #include <regex.h>
71
72 #ifdef G_OS_UNIX
73 #include <sys/utsname.h>
74 #endif
75
76 #include <fcntl.h>
77
78 #ifdef G_OS_WIN32
79 #  include <direct.h>
80 #  include <io.h>
81 #  include <w32lib.h>
82 #endif
83
84 #include "utils.h"
85 #include "socket.h"
86 #include "codeconv.h"
87 #include "tlds.h"
88 #include "timing.h"
89 #include "claws_io.h"
90
91 #define BUFFSIZE        8192
92
93 static gboolean debug_mode = FALSE;
94
95 GSList *slist_copy_deep(GSList *list, GCopyFunc func)
96 {
97 #if GLIB_CHECK_VERSION(2, 34, 0)
98         return g_slist_copy_deep(list, func, NULL);
99 #else
100         GSList *res = g_slist_copy(list);
101         GSList *walk = res;
102         while (walk) {
103                 walk->data = func(walk->data, NULL);
104                 walk = walk->next;
105         }
106         return res;
107 #endif
108 }
109
110 void list_free_strings_full(GList *list)
111 {
112         g_list_free_full(list, (GDestroyNotify)g_free);
113 }
114
115 void slist_free_strings_full(GSList *list)
116 {
117         g_slist_free_full(list, (GDestroyNotify)g_free);
118 }
119
120 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
121 {
122         g_free(key);
123 }
124
125 void hash_free_strings(GHashTable *table)
126 {
127         g_hash_table_foreach(table, hash_free_strings_func, NULL);
128 }
129
130 gint str_case_equal(gconstpointer v, gconstpointer v2)
131 {
132         return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
133 }
134
135 guint str_case_hash(gconstpointer key)
136 {
137         const gchar *p = key;
138         guint h = *p;
139
140         if (h) {
141                 h = g_ascii_tolower(h);
142                 for (p += 1; *p != '\0'; p++)
143                         h = (h << 5) - h + g_ascii_tolower(*p);
144         }
145
146         return h;
147 }
148
149 gint to_number(const gchar *nstr)
150 {
151         register const gchar *p;
152
153         if (*nstr == '\0') return -1;
154
155         for (p = nstr; *p != '\0'; p++)
156                 if (!g_ascii_isdigit(*p)) return -1;
157
158         return atoi(nstr);
159 }
160
161 /* convert integer into string,
162    nstr must be not lower than 11 characters length */
163 gchar *itos_buf(gchar *nstr, gint n)
164 {
165         g_snprintf(nstr, 11, "%d", n);
166         return nstr;
167 }
168
169 /* convert integer into string */
170 gchar *itos(gint n)
171 {
172         static gchar nstr[11];
173
174         return itos_buf(nstr, n);
175 }
176
177 #define divide(num,divisor,i,d)         \
178 {                                       \
179         i = num >> divisor;             \
180         d = num & ((1<<divisor)-1);     \
181         d = (d*100) >> divisor;         \
182 }
183
184
185 /*!
186  * \brief Convert a given size in bytes in a human-readable string
187  *
188  * \param size  The size expressed in bytes to convert in string
189  * \return      The string that respresents the size in an human-readable way
190  */
191 gchar *to_human_readable(goffset size)
192 {
193         static gchar str[14];
194         static gchar *b_format = NULL, *kb_format = NULL, 
195                      *mb_format = NULL, *gb_format = NULL;
196         register int t = 0, r = 0;
197         if (b_format == NULL) {
198                 b_format  = _("%dB");
199                 kb_format = _("%d.%02dKB");
200                 mb_format = _("%d.%02dMB");
201                 gb_format = _("%.2fGB");
202         }
203         
204         if (size < (goffset)1024) {
205                 g_snprintf(str, sizeof(str), b_format, (gint)size);
206                 return str;
207         } else if (size >> 10 < (goffset)1024) {
208                 divide(size, 10, t, r);
209                 g_snprintf(str, sizeof(str), kb_format, t, r);
210                 return str;
211         } else if (size >> 20 < (goffset)1024) {
212                 divide(size, 20, t, r);
213                 g_snprintf(str, sizeof(str), mb_format, t, r);
214                 return str;
215         } else {
216                 g_snprintf(str, sizeof(str), gb_format, (gfloat)(size >> 30));
217                 return str;
218         }
219 }
220
221 /* strcmp with NULL-checking */
222 gint strcmp2(const gchar *s1, const gchar *s2)
223 {
224         if (s1 == NULL || s2 == NULL)
225                 return -1;
226         else
227                 return strcmp(s1, s2);
228 }
229 /* strstr with NULL-checking */
230 gchar *strstr2(const gchar *s1, const gchar *s2)
231 {
232         if (s1 == NULL || s2 == NULL)
233                 return NULL;
234         else
235                 return strstr(s1, s2);
236 }
237 /* compare paths */
238 gint path_cmp(const gchar *s1, const gchar *s2)
239 {
240         gint len1, len2;
241         int rc;
242 #ifdef G_OS_WIN32
243         gchar *s1buf, *s2buf;
244 #endif
245
246         if (s1 == NULL || s2 == NULL) return -1;
247         if (*s1 == '\0' || *s2 == '\0') return -1;
248
249 #ifdef G_OS_WIN32
250         s1buf = g_strdup (s1);
251         s2buf = g_strdup (s2);
252         subst_char (s1buf, '/', G_DIR_SEPARATOR);
253         subst_char (s2buf, '/', G_DIR_SEPARATOR);
254         s1 = s1buf;
255         s2 = s2buf;
256 #endif /* !G_OS_WIN32 */
257
258         len1 = strlen(s1);
259         len2 = strlen(s2);
260
261         if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
262         if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
263
264         rc = strncmp(s1, s2, MAX(len1, len2));
265 #ifdef G_OS_WIN32
266         g_free (s1buf);
267         g_free (s2buf);
268 #endif /* !G_OS_WIN32 */
269         return rc;
270 }
271
272 /* remove trailing return code */
273 gchar *strretchomp(gchar *str)
274 {
275         register gchar *s;
276
277         if (!*str) return str;
278
279         for (s = str + strlen(str) - 1;
280              s >= str && (*s == '\n' || *s == '\r');
281              s--)
282                 *s = '\0';
283
284         return str;
285 }
286
287 /* remove trailing character */
288 gchar *strtailchomp(gchar *str, gchar tail_char)
289 {
290         register gchar *s;
291
292         if (!*str) return str;
293         if (tail_char == '\0') return str;
294
295         for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
296                 *s = '\0';
297
298         return str;
299 }
300
301 /* remove CR (carriage return) */
302 gchar *strcrchomp(gchar *str)
303 {
304         register gchar *s;
305
306         if (!*str) return str;
307
308         s = str + strlen(str) - 1;
309         if (*s == '\n' && s > str && *(s - 1) == '\r') {
310                 *(s - 1) = '\n';
311                 *s = '\0';
312         }
313
314         return str;
315 }
316
317 gint file_strip_crs(const gchar *file)
318 {
319         FILE *fp = NULL, *outfp = NULL;
320         gchar buf[4096];
321         gchar *out = get_tmp_file();
322         if (file == NULL)
323                 goto freeout;
324
325         fp = claws_fopen(file, "rb");
326         if (!fp)
327                 goto freeout;
328
329         outfp = claws_fopen(out, "wb");
330         if (!outfp) {
331                 claws_fclose(fp);
332                 goto freeout;
333         }
334
335         while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
336                 strcrchomp(buf);
337                 if (claws_fputs(buf, outfp) == EOF) {
338                         claws_fclose(fp);
339                         claws_fclose(outfp);
340                         goto unlinkout;
341                 }
342         }
343
344         claws_fclose(fp);
345         if (claws_safe_fclose(outfp) == EOF) {
346                 goto unlinkout;
347         }
348         
349         if (move_file(out, file, TRUE) < 0)
350                 goto unlinkout;
351         
352         g_free(out);
353         return 0;
354 unlinkout:
355         claws_unlink(out);
356 freeout:
357         g_free(out);
358         return -1;
359 }
360
361 /* Similar to `strstr' but this function ignores the case of both strings.  */
362 gchar *strcasestr(const gchar *haystack, const gchar *needle)
363 {
364         size_t haystack_len = strlen(haystack);
365
366         return strncasestr(haystack, haystack_len, needle);
367 }
368
369 gchar *strncasestr(const gchar *haystack, gint haystack_len, const gchar *needle)
370 {
371         register size_t needle_len;
372
373         needle_len   = strlen(needle);
374
375         if (haystack_len < needle_len || needle_len == 0)
376                 return NULL;
377
378         while (haystack_len >= needle_len) {
379                 if (!g_ascii_strncasecmp(haystack, needle, needle_len))
380                         return (gchar *)haystack;
381                 else {
382                         haystack++;
383                         haystack_len--;
384                 }
385         }
386
387         return NULL;
388 }
389
390 gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
391                    gconstpointer needle, size_t needlelen)
392 {
393         const gchar *haystack_ = (const gchar *)haystack;
394         const gchar *needle_ = (const gchar *)needle;
395         const gchar *haystack_cur = (const gchar *)haystack;
396         size_t haystack_left = haystacklen;
397
398         if (needlelen == 1)
399                 return memchr(haystack_, *needle_, haystacklen);
400
401         while ((haystack_cur = memchr(haystack_cur, *needle_, haystack_left))
402                != NULL) {
403                 if (haystacklen - (haystack_cur - haystack_) < needlelen)
404                         break;
405                 if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
406                         return (gpointer)haystack_cur;
407                 else{
408                         haystack_cur++;
409                         haystack_left = haystacklen - (haystack_cur - haystack_);
410                 }
411         }
412
413         return NULL;
414 }
415
416 /* Copy no more than N characters of SRC to DEST, with NULL terminating.  */
417 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
418 {
419         register const gchar *s = src;
420         register gchar *d = dest;
421
422         while (--n && *s)
423                 *d++ = *s++;
424         *d = '\0';
425
426         return dest;
427 }
428
429
430 /* Examine if next block is non-ASCII string */
431 gboolean is_next_nonascii(const gchar *s)
432 {
433         const gchar *p;
434
435         /* skip head space */
436         for (p = s; *p != '\0' && g_ascii_isspace(*p); p++)
437                 ;
438         for (; *p != '\0' && !g_ascii_isspace(*p); p++) {
439                 if (*(guchar *)p > 127 || *(guchar *)p < 32)
440                         return TRUE;
441         }
442
443         return FALSE;
444 }
445
446 gint get_next_word_len(const gchar *s)
447 {
448         gint len = 0;
449
450         for (; *s != '\0' && !g_ascii_isspace(*s); s++, len++)
451                 ;
452
453         return len;
454 }
455
456 static void trim_subject_for_compare(gchar *str)
457 {
458         gchar *srcp;
459
460         eliminate_parenthesis(str, '[', ']');
461         eliminate_parenthesis(str, '(', ')');
462         g_strstrip(str);
463
464         srcp = str + subject_get_prefix_length(str);
465         if (srcp != str)
466                 memmove(str, srcp, strlen(srcp) + 1);
467 }
468
469 static void trim_subject_for_sort(gchar *str)
470 {
471         gchar *srcp;
472
473         g_strstrip(str);
474
475         srcp = str + subject_get_prefix_length(str);
476         if (srcp != str)
477                 memmove(str, srcp, strlen(srcp) + 1);
478 }
479
480 /* compare subjects */
481 gint subject_compare(const gchar *s1, const gchar *s2)
482 {
483         gchar *str1, *str2;
484
485         if (!s1 || !s2) return -1;
486         if (!*s1 || !*s2) return -1;
487
488         Xstrdup_a(str1, s1, return -1);
489         Xstrdup_a(str2, s2, return -1);
490
491         trim_subject_for_compare(str1);
492         trim_subject_for_compare(str2);
493
494         if (!*str1 || !*str2) return -1;
495
496         return strcmp(str1, str2);
497 }
498
499 gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
500 {
501         gchar *str1, *str2;
502
503         if (!s1 || !s2) return -1;
504
505         Xstrdup_a(str1, s1, return -1);
506         Xstrdup_a(str2, s2, return -1);
507
508         trim_subject_for_sort(str1);
509         trim_subject_for_sort(str2);
510
511         return g_utf8_collate(str1, str2);
512 }
513
514 void trim_subject(gchar *str)
515 {
516         register gchar *srcp;
517         gchar op, cl;
518         gint in_brace;
519
520         g_strstrip(str);
521
522         srcp = str + subject_get_prefix_length(str);
523
524         if (*srcp == '[') {
525                 op = '[';
526                 cl = ']';
527         } else if (*srcp == '(') {
528                 op = '(';
529                 cl = ')';
530         } else
531                 op = 0;
532
533         if (op) {
534                 ++srcp;
535                 in_brace = 1;
536                 while (*srcp) {
537                         if (*srcp == op)
538                                 in_brace++;
539                         else if (*srcp == cl)
540                                 in_brace--;
541                         srcp++;
542                         if (in_brace == 0)
543                                 break;
544                 }
545         }
546         while (g_ascii_isspace(*srcp)) srcp++;
547         memmove(str, srcp, strlen(srcp) + 1);
548 }
549
550 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
551 {
552         register gchar *srcp, *destp;
553         gint in_brace;
554
555         destp = str;
556
557         while ((destp = strchr(destp, op))) {
558                 in_brace = 1;
559                 srcp = destp + 1;
560                 while (*srcp) {
561                         if (*srcp == op)
562                                 in_brace++;
563                         else if (*srcp == cl)
564                                 in_brace--;
565                         srcp++;
566                         if (in_brace == 0)
567                                 break;
568                 }
569                 while (g_ascii_isspace(*srcp)) srcp++;
570                 memmove(destp, srcp, strlen(srcp) + 1);
571         }
572 }
573
574 void extract_parenthesis(gchar *str, gchar op, gchar cl)
575 {
576         register gchar *srcp, *destp;
577         gint in_brace;
578
579         destp = str;
580
581         while ((srcp = strchr(destp, op))) {
582                 if (destp > str)
583                         *destp++ = ' ';
584                 memmove(destp, srcp + 1, strlen(srcp));
585                 in_brace = 1;
586                 while(*destp) {
587                         if (*destp == op)
588                                 in_brace++;
589                         else if (*destp == cl)
590                                 in_brace--;
591
592                         if (in_brace == 0)
593                                 break;
594
595                         destp++;
596                 }
597         }
598         *destp = '\0';
599 }
600
601 static void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
602                                          gchar op, gchar cl)
603 {
604         register gchar *srcp, *destp;
605         gint in_brace;
606         gboolean in_quote = FALSE;
607
608         destp = str;
609
610         while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
611                 if (destp > str)
612                         *destp++ = ' ';
613                 memmove(destp, srcp + 1, strlen(srcp));
614                 in_brace = 1;
615                 while(*destp) {
616                         if (*destp == op && !in_quote)
617                                 in_brace++;
618                         else if (*destp == cl && !in_quote)
619                                 in_brace--;
620                         else if (*destp == quote_chr)
621                                 in_quote ^= TRUE;
622
623                         if (in_brace == 0)
624                                 break;
625
626                         destp++;
627                 }
628         }
629         *destp = '\0';
630 }
631
632 void extract_quote(gchar *str, gchar quote_chr)
633 {
634         register gchar *p;
635
636         if ((str = strchr(str, quote_chr))) {
637                 p = str;
638                 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
639                         memmove(p - 1, p, strlen(p) + 1);
640                         p--;
641                 }
642                 if(p) {
643                         *p = '\0';
644                         memmove(str, str + 1, p - str);
645                 }
646         }
647 }
648
649 /* Returns a newly allocated string with all quote_chr not at the beginning
650    or the end of str escaped with '\' or the given str if not required. */
651 gchar *escape_internal_quotes(gchar *str, gchar quote_chr)
652 {
653         register gchar *p, *q;
654         gchar *qstr;
655         int k = 0, l = 0;
656
657         if (str == NULL || *str == '\0')
658                 return str;
659
660         /* search for unescaped quote_chr */
661         p = str;
662         if (*p == quote_chr)
663                 ++p, ++l;
664         while (*p) {
665                 if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
666                         ++k;
667                 ++p, ++l;
668         }
669         if (!k) /* nothing to escape */
670                 return str;
671
672         /* unescaped quote_chr found */
673         qstr = g_malloc(l + k + 1);
674         p = str;
675         q = qstr;
676         if (*p == quote_chr) {
677                 *q = quote_chr;
678                 ++p, ++q;
679         }
680         while (*p) {
681                 if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
682                         *q++ = '\\';
683                 *q++ = *p++;
684         }
685         *q = '\0';
686
687         return qstr;
688 }
689
690 void eliminate_address_comment(gchar *str)
691 {
692         register gchar *srcp, *destp;
693         gint in_brace;
694
695         destp = str;
696
697         while ((destp = strchr(destp, '"'))) {
698                 if ((srcp = strchr(destp + 1, '"'))) {
699                         srcp++;
700                         if (*srcp == '@') {
701                                 destp = srcp + 1;
702                         } else {
703                                 while (g_ascii_isspace(*srcp)) srcp++;
704                                 memmove(destp, srcp, strlen(srcp) + 1);
705                         }
706                 } else {
707                         *destp = '\0';
708                         break;
709                 }
710         }
711
712         destp = str;
713
714         while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
715                 in_brace = 1;
716                 srcp = destp + 1;
717                 while (*srcp) {
718                         if (*srcp == '(')
719                                 in_brace++;
720                         else if (*srcp == ')')
721                                 in_brace--;
722                         srcp++;
723                         if (in_brace == 0)
724                                 break;
725                 }
726                 while (g_ascii_isspace(*srcp)) srcp++;
727                 memmove(destp, srcp, strlen(srcp) + 1);
728         }
729 }
730
731 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
732 {
733         gboolean in_quote = FALSE;
734
735         while (*str) {
736                 if (*str == c && !in_quote)
737                         return (gchar *)str;
738                 if (*str == quote_chr)
739                         in_quote ^= TRUE;
740                 str++;
741         }
742
743         return NULL;
744 }
745
746 void extract_address(gchar *str)
747 {
748         cm_return_if_fail(str != NULL);
749         eliminate_address_comment(str);
750         if (strchr_with_skip_quote(str, '"', '<'))
751                 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
752         g_strstrip(str);
753 }
754
755 void extract_list_id_str(gchar *str)
756 {
757         if (strchr_with_skip_quote(str, '"', '<'))
758                 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
759         g_strstrip(str);
760 }
761
762 static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
763 {
764         gchar *work;
765         gchar *workp;
766
767         if (!str) return addr_list;
768
769         Xstrdup_a(work, str, return addr_list);
770
771         if (removecomments)
772                 eliminate_address_comment(work);
773         workp = work;
774
775         while (workp && *workp) {
776                 gchar *p, *next;
777
778                 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
779                         *p = '\0';
780                         next = p + 1;
781                 } else
782                         next = NULL;
783
784                 if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
785                         extract_parenthesis_with_skip_quote
786                                 (workp, '"', '<', '>');
787
788                 g_strstrip(workp);
789                 if (*workp)
790                         addr_list = g_slist_append(addr_list, g_strdup(workp));
791
792                 workp = next;
793         }
794
795         return addr_list;
796 }
797
798 GSList *address_list_append(GSList *addr_list, const gchar *str)
799 {
800         return address_list_append_real(addr_list, str, TRUE);
801 }
802
803 GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
804 {
805         return address_list_append_real(addr_list, str, FALSE);
806 }
807
808 GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
809 {
810         const gchar *strp;
811
812         if (!str) return msgid_list;
813         strp = str;
814
815         while (strp && *strp) {
816                 const gchar *start, *end;
817                 gchar *msgid;
818
819                 if ((start = strchr(strp, '<')) != NULL) {
820                         end = strchr(start + 1, '>');
821                         if (!end) break;
822                 } else
823                         break;
824
825                 msgid = g_strndup(start + 1, end - start - 1);
826                 g_strstrip(msgid);
827                 if (*msgid)
828                         msgid_list = g_slist_prepend(msgid_list, msgid);
829                 else
830                         g_free(msgid);
831
832                 strp = end + 1;
833         }
834
835         return msgid_list;
836 }
837
838 GSList *references_list_append(GSList *msgid_list, const gchar *str)
839 {
840         GSList *list;
841
842         list = references_list_prepend(NULL, str);
843         list = g_slist_reverse(list);
844         msgid_list = g_slist_concat(msgid_list, list);
845
846         return msgid_list;
847 }
848
849 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
850 {
851         gchar *work;
852         gchar *workp;
853
854         if (!str) return group_list;
855
856         Xstrdup_a(work, str, return group_list);
857
858         workp = work;
859
860         while (workp && *workp) {
861                 gchar *p, *next;
862
863                 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
864                         *p = '\0';
865                         next = p + 1;
866                 } else
867                         next = NULL;
868
869                 g_strstrip(workp);
870                 if (*workp)
871                         group_list = g_slist_append(group_list,
872                                                     g_strdup(workp));
873
874                 workp = next;
875         }
876
877         return group_list;
878 }
879
880 GList *add_history(GList *list, const gchar *str)
881 {
882         GList *old;
883         gchar *oldstr;
884
885         cm_return_val_if_fail(str != NULL, list);
886
887         old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
888         if (old) {
889                 oldstr = old->data;
890                 list = g_list_remove(list, old->data);
891                 g_free(oldstr);
892         } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
893                 GList *last;
894
895                 last = g_list_last(list);
896                 if (last) {
897                         oldstr = last->data;
898                         list = g_list_remove(list, last->data);
899                         g_free(oldstr);
900                 }
901         }
902
903         list = g_list_prepend(list, g_strdup(str));
904
905         return list;
906 }
907
908 void remove_return(gchar *str)
909 {
910         register gchar *p = str;
911
912         while (*p) {
913                 if (*p == '\n' || *p == '\r')
914                         memmove(p, p + 1, strlen(p));
915                 else
916                         p++;
917         }
918 }
919
920 void remove_space(gchar *str)
921 {
922         register gchar *p = str;
923         register gint spc;
924
925         while (*p) {
926                 spc = 0;
927                 while (g_ascii_isspace(*(p + spc)))
928                         spc++;
929                 if (spc)
930                         memmove(p, p + spc, strlen(p + spc) + 1);
931                 else
932                         p++;
933         }
934 }
935
936 void unfold_line(gchar *str)
937 {
938         register gchar *ch;
939         register gunichar c;
940         register gint len;
941
942         ch = str; /* iterator for source string */
943
944         while (*ch != 0) {
945                 c = g_utf8_get_char_validated(ch, -1);
946
947                 if (c == (gunichar)-1 || c == (gunichar)-2) {
948                         /* non-unicode byte, move past it */
949                         ch++;
950                         continue;
951                 }
952
953                 len = g_unichar_to_utf8(c, NULL);
954
955                 if (!g_unichar_isdefined(c) || !g_unichar_isprint(c) ||
956                                 g_unichar_isspace(c)) {
957                         /* replace anything bad or whitespacey with a single space */
958                         *ch = ' ';
959                         ch++;
960                         if (len > 1) {
961                                 /* move rest of the string forwards, since we just replaced
962                                  * a multi-byte sequence with one byte */
963                                 memmove(ch, ch + len-1, strlen(ch + len-1) + 1);
964                         }
965                 } else {
966                         /* A valid unicode character, copy it. */
967                         ch += len;
968                 }
969         }
970 }
971
972 void subst_char(gchar *str, gchar orig, gchar subst)
973 {
974         register gchar *p = str;
975
976         while (*p) {
977                 if (*p == orig)
978                         *p = subst;
979                 p++;
980         }
981 }
982
983 void subst_chars(gchar *str, gchar *orig, gchar subst)
984 {
985         register gchar *p = str;
986
987         while (*p) {
988                 if (strchr(orig, *p) != NULL)
989                         *p = subst;
990                 p++;
991         }
992 }
993
994 void subst_for_filename(gchar *str)
995 {
996         if (!str)
997                 return;
998 #ifdef G_OS_WIN32
999         subst_chars(str, "\t\r\n\\/*?:", '_');
1000 #else
1001         subst_chars(str, "\t\r\n\\/*", '_');
1002 #endif
1003 }
1004
1005 void subst_for_shellsafe_filename(gchar *str)
1006 {
1007         if (!str)
1008                 return;
1009         subst_for_filename(str);
1010         subst_chars(str, " \"'|&;()<>'!{}[]",'_');
1011 }
1012
1013 gboolean is_ascii_str(const gchar *str)
1014 {
1015         const guchar *p = (const guchar *)str;
1016
1017         while (*p != '\0') {
1018                 if (*p != '\t' && *p != ' ' &&
1019                     *p != '\r' && *p != '\n' &&
1020                     (*p < 32 || *p >= 127))
1021                         return FALSE;
1022                 p++;
1023         }
1024
1025         return TRUE;
1026 }
1027
1028 static const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
1029 {
1030         gchar * position = NULL;
1031         gchar * tmp_pos = NULL;
1032         int i;
1033
1034         if (str == NULL || quote_chars == NULL)
1035                 return NULL;
1036
1037         for (i = 0; i < strlen(quote_chars); i++) {
1038                 tmp_pos = strrchr (str, quote_chars[i]);
1039                 if(position == NULL
1040                                 || (tmp_pos != NULL && position <= tmp_pos) )
1041                         position = tmp_pos;
1042         }
1043         return position;
1044 }
1045
1046 gint get_quote_level(const gchar *str, const gchar *quote_chars)
1047 {
1048         const gchar *first_pos;
1049         const gchar *last_pos;
1050         const gchar *p = str;
1051         gint quote_level = -1;
1052
1053         /* speed up line processing by only searching to the last '>' */
1054         if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
1055                 /* skip a line if it contains a '<' before the initial '>' */
1056                 if (memchr(str, '<', first_pos - str) != NULL)
1057                         return -1;
1058                 last_pos = line_has_quote_char_last(first_pos, quote_chars);
1059         } else
1060                 return -1;
1061
1062         while (p <= last_pos) {
1063                 while (p < last_pos) {
1064                         if (g_ascii_isspace(*p))
1065                                 p++;
1066                         else
1067                                 break;
1068                 }
1069
1070                 if (strchr(quote_chars, *p))
1071                         quote_level++;
1072                 else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) {
1073                         /* any characters are allowed except '-','<' and space */
1074                         while (*p != '-' && *p != '<'
1075                                && !strchr(quote_chars, *p)
1076                                && !g_ascii_isspace(*p)
1077                                && p < last_pos)
1078                                 p++;
1079                         if (strchr(quote_chars, *p))
1080                                 quote_level++;
1081                         else
1082                                 break;
1083                 }
1084
1085                 p++;
1086         }
1087
1088         return quote_level;
1089 }
1090
1091 gint check_line_length(const gchar *str, gint max_chars, gint *line)
1092 {
1093         const gchar *p = str, *q;
1094         gint cur_line = 0, len;
1095
1096         while ((q = strchr(p, '\n')) != NULL) {
1097                 len = q - p + 1;
1098                 if (len > max_chars) {
1099                         if (line)
1100                                 *line = cur_line;
1101                         return -1;
1102                 }
1103                 p = q + 1;
1104                 ++cur_line;
1105         }
1106
1107         len = strlen(p);
1108         if (len > max_chars) {
1109                 if (line)
1110                         *line = cur_line;
1111                 return -1;
1112         }
1113
1114         return 0;
1115 }
1116
1117 const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
1118 {
1119         gchar * position = NULL;
1120         gchar * tmp_pos = NULL;
1121         int i;
1122
1123         if (str == NULL || quote_chars == NULL)
1124                 return NULL;
1125
1126         for (i = 0; i < strlen(quote_chars); i++) {
1127                 tmp_pos = strchr (str, quote_chars[i]);
1128                 if(position == NULL
1129                                 || (tmp_pos != NULL && position >= tmp_pos) )
1130                         position = tmp_pos;
1131         }
1132         return position;
1133 }
1134
1135 static gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1136 {
1137         register guint haystack_len, needle_len;
1138         gboolean in_squote = FALSE, in_dquote = FALSE;
1139
1140         haystack_len = strlen(haystack);
1141         needle_len   = strlen(needle);
1142
1143         if (haystack_len < needle_len || needle_len == 0)
1144                 return NULL;
1145
1146         while (haystack_len >= needle_len) {
1147                 if (!in_squote && !in_dquote &&
1148                     !strncmp(haystack, needle, needle_len))
1149                         return (gchar *)haystack;
1150
1151                 /* 'foo"bar"' -> foo"bar"
1152                    "foo'bar'" -> foo'bar' */
1153                 if (*haystack == '\'') {
1154                         if (in_squote)
1155                                 in_squote = FALSE;
1156                         else if (!in_dquote)
1157                                 in_squote = TRUE;
1158                 } else if (*haystack == '\"') {
1159                         if (in_dquote)
1160                                 in_dquote = FALSE;
1161                         else if (!in_squote)
1162                                 in_dquote = TRUE;
1163                 } else if (*haystack == '\\') {
1164                         haystack++;
1165                         haystack_len--;
1166                 }
1167
1168                 haystack++;
1169                 haystack_len--;
1170         }
1171
1172         return NULL;
1173 }
1174
1175 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1176                             gint max_tokens)
1177 {
1178         GSList *string_list = NULL, *slist;
1179         gchar **str_array, *s, *new_str;
1180         guint i, n = 1, len;
1181
1182         cm_return_val_if_fail(str != NULL, NULL);
1183         cm_return_val_if_fail(delim != NULL, NULL);
1184
1185         if (max_tokens < 1)
1186                 max_tokens = G_MAXINT;
1187
1188         s = strstr_with_skip_quote(str, delim);
1189         if (s) {
1190                 guint delimiter_len = strlen(delim);
1191
1192                 do {
1193                         len = s - str;
1194                         new_str = g_strndup(str, len);
1195
1196                         if (new_str[0] == '\'' || new_str[0] == '\"') {
1197                                 if (new_str[len - 1] == new_str[0]) {
1198                                         new_str[len - 1] = '\0';
1199                                         memmove(new_str, new_str + 1, len - 1);
1200                                 }
1201                         }
1202                         string_list = g_slist_prepend(string_list, new_str);
1203                         n++;
1204                         str = s + delimiter_len;
1205                         s = strstr_with_skip_quote(str, delim);
1206                 } while (--max_tokens && s);
1207         }
1208
1209         if (*str) {
1210                 new_str = g_strdup(str);
1211                 if (new_str[0] == '\'' || new_str[0] == '\"') {
1212                         len = strlen(str);
1213                         if (new_str[len - 1] == new_str[0]) {
1214                                 new_str[len - 1] = '\0';
1215                                 memmove(new_str, new_str + 1, len - 1);
1216                         }
1217                 }
1218                 string_list = g_slist_prepend(string_list, new_str);
1219                 n++;
1220         }
1221
1222         str_array = g_new(gchar*, n);
1223
1224         i = n - 1;
1225
1226         str_array[i--] = NULL;
1227         for (slist = string_list; slist; slist = slist->next)
1228                 str_array[i--] = slist->data;
1229
1230         g_slist_free(string_list);
1231
1232         return str_array;
1233 }
1234
1235 gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
1236 {
1237         gchar *abbrev_group;
1238         gchar *ap;
1239         const gchar *p = group;
1240         const gchar *last;
1241
1242         cm_return_val_if_fail(group != NULL, NULL);
1243
1244         last = group + strlen(group);
1245         abbrev_group = ap = g_malloc(strlen(group) + 1);
1246
1247         while (*p) {
1248                 while (*p == '.')
1249                         *ap++ = *p++;
1250                 if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
1251                         *ap++ = *p++;
1252                         while (*p != '.') p++;
1253                 } else {
1254                         strcpy(ap, p);
1255                         return abbrev_group;
1256                 }
1257         }
1258
1259         *ap = '\0';
1260         return abbrev_group;
1261 }
1262
1263 gchar *trim_string(const gchar *str, gint len)
1264 {
1265         const gchar *p = str;
1266         gint mb_len;
1267         gchar *new_str;
1268         gint new_len = 0;
1269
1270         if (!str) return NULL;
1271         if (strlen(str) <= len)
1272                 return g_strdup(str);
1273         if (g_utf8_validate(str, -1, NULL) == FALSE)
1274                 return g_strdup(str);
1275
1276         while (*p != '\0') {
1277                 mb_len = g_utf8_skip[*(guchar *)p];
1278                 if (mb_len == 0)
1279                         break;
1280                 else if (new_len + mb_len > len)
1281                         break;
1282
1283                 new_len += mb_len;
1284                 p += mb_len;
1285         }
1286
1287         Xstrndup_a(new_str, str, new_len, return g_strdup(str));
1288         return g_strconcat(new_str, "...", NULL);
1289 }
1290
1291 GList *uri_list_extract_filenames(const gchar *uri_list)
1292 {
1293         GList *result = NULL;
1294         const gchar *p, *q;
1295         gchar *escaped_utf8uri;
1296
1297         p = uri_list;
1298
1299         while (p) {
1300                 if (*p != '#') {
1301                         while (g_ascii_isspace(*p)) p++;
1302                         if (!strncmp(p, "file:", 5)) {
1303                                 q = p;
1304                                 q += 5;
1305                                 while (*q && *q != '\n' && *q != '\r') q++;
1306
1307                                 if (q > p) {
1308                                         gchar *file, *locale_file = NULL;
1309                                         q--;
1310                                         while (q > p && g_ascii_isspace(*q))
1311                                                 q--;
1312                                         Xalloca(escaped_utf8uri, q - p + 2,
1313                                                 return result);
1314                                         Xalloca(file, q - p + 2,
1315                                                 return result);
1316                                         *file = '\0';
1317                                         strncpy(escaped_utf8uri, p, q - p + 1);
1318                                         escaped_utf8uri[q - p + 1] = '\0';
1319                                         decode_uri_with_plus(file, escaped_utf8uri, FALSE);
1320                     /*
1321                      * g_filename_from_uri() rejects escaped/locale encoded uri
1322                      * string which come from Nautilus.
1323                      */
1324 #ifndef G_OS_WIN32
1325                                         if (g_utf8_validate(file, -1, NULL))
1326                                                 locale_file
1327                                                         = conv_codeset_strdup(
1328                                                                 file + 5,
1329                                                                 CS_UTF_8,
1330                                                                 conv_get_locale_charset_str());
1331                                         if (!locale_file)
1332                                                 locale_file = g_strdup(file + 5);
1333 #else
1334                                         locale_file = g_filename_from_uri(escaped_utf8uri, NULL, NULL);
1335 #endif
1336                                         result = g_list_append(result, locale_file);
1337                                 }
1338                         }
1339                 }
1340                 p = strchr(p, '\n');
1341                 if (p) p++;
1342         }
1343
1344         return result;
1345 }
1346
1347 /* Converts two-digit hexadecimal to decimal.  Used for unescaping escaped
1348  * characters
1349  */
1350 static gint axtoi(const gchar *hexstr)
1351 {
1352         gint hi, lo, result;
1353
1354         hi = hexstr[0];
1355         if ('0' <= hi && hi <= '9') {
1356                 hi -= '0';
1357         } else
1358                 if ('a' <= hi && hi <= 'f') {
1359                         hi -= ('a' - 10);
1360                 } else
1361                         if ('A' <= hi && hi <= 'F') {
1362                                 hi -= ('A' - 10);
1363                         }
1364
1365         lo = hexstr[1];
1366         if ('0' <= lo && lo <= '9') {
1367                 lo -= '0';
1368         } else
1369                 if ('a' <= lo && lo <= 'f') {
1370                         lo -= ('a'-10);
1371                 } else
1372                         if ('A' <= lo && lo <= 'F') {
1373                                 lo -= ('A' - 10);
1374                         }
1375         result = lo + (16 * hi);
1376         return result;
1377 }
1378
1379 gboolean is_uri_string(const gchar *str)
1380 {
1381         while (str && *str && g_ascii_isspace(*str))
1382                 str++;
1383         return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
1384                 g_ascii_strncasecmp(str, "https://", 8) == 0 ||
1385                 g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
1386                 g_ascii_strncasecmp(str, "www.", 4) == 0);
1387 }
1388
1389 gchar *get_uri_path(const gchar *uri)
1390 {
1391         while (uri && *uri && g_ascii_isspace(*uri))
1392                 uri++;
1393         if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
1394                 return (gchar *)(uri + 7);
1395         else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
1396                 return (gchar *)(uri + 8);
1397         else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
1398                 return (gchar *)(uri + 6);
1399         else
1400                 return (gchar *)uri;
1401 }
1402
1403 gint get_uri_len(const gchar *str)
1404 {
1405         const gchar *p;
1406
1407         if (is_uri_string(str)) {
1408                 for (p = str; *p != '\0'; p++) {
1409                         if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p))
1410                                 break;
1411                 }
1412                 return p - str;
1413         }
1414
1415         return 0;
1416 }
1417
1418 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
1419  * plusses, and escape characters are used)
1420  */
1421 void decode_uri_with_plus(gchar *decoded_uri, const gchar *encoded_uri, gboolean with_plus)
1422 {
1423         gchar *dec = decoded_uri;
1424         const gchar *enc = encoded_uri;
1425
1426         while (*enc) {
1427                 if (*enc == '%') {
1428                         enc++;
1429                         if (isxdigit((guchar)enc[0]) &&
1430                             isxdigit((guchar)enc[1])) {
1431                                 *dec = axtoi(enc);
1432                                 dec++;
1433                                 enc += 2;
1434                         }
1435                 } else {
1436                         if (with_plus && *enc == '+')
1437                                 *dec = ' ';
1438                         else
1439                                 *dec = *enc;
1440                         dec++;
1441                         enc++;
1442                 }
1443         }
1444
1445         *dec = '\0';
1446 }
1447
1448 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
1449 {
1450         decode_uri_with_plus(decoded_uri, encoded_uri, TRUE);
1451 }
1452
1453 static gchar *decode_uri_gdup(const gchar *encoded_uri)
1454 {
1455     gchar *buffer = g_malloc(strlen(encoded_uri)+1);
1456     decode_uri_with_plus(buffer, encoded_uri, FALSE);
1457     return buffer;
1458 }
1459
1460 gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc, gchar **bcc,
1461                      gchar **subject, gchar **body, gchar ***attach, gchar **inreplyto)
1462 {
1463         gchar *tmp_mailto;
1464         gchar *p;
1465         const gchar *forbidden_uris[] = { ".gnupg/",
1466                                           "/etc/passwd",
1467                                           "/etc/shadow",
1468                                           ".ssh/",
1469                                           "../",
1470                                           NULL };
1471         gint num_attach = 0;
1472         gchar **my_att = NULL;
1473
1474         Xstrdup_a(tmp_mailto, mailto, return -1);
1475
1476         if (!strncmp(tmp_mailto, "mailto:", 7))
1477                 tmp_mailto += 7;
1478
1479         p = strchr(tmp_mailto, '?');
1480         if (p) {
1481                 *p = '\0';
1482                 p++;
1483         }
1484
1485         if (to && !*to)
1486                 *to = decode_uri_gdup(tmp_mailto);
1487
1488         my_att = g_malloc(sizeof(char *));
1489         my_att[0] = NULL;
1490
1491         while (p) {
1492                 gchar *field, *value;
1493
1494                 field = p;
1495
1496                 p = strchr(p, '=');
1497                 if (!p) break;
1498                 *p = '\0';
1499                 p++;
1500
1501                 value = p;
1502
1503                 p = strchr(p, '&');
1504                 if (p) {
1505                         *p = '\0';
1506                         p++;
1507                 }
1508
1509                 if (*value == '\0') continue;
1510
1511                 if (from && !g_ascii_strcasecmp(field, "from")) {
1512                         if (!*from) {
1513                                 *from = decode_uri_gdup(value);
1514                         } else {
1515                                 gchar *tmp = decode_uri_gdup(value);
1516                                 gchar *new_from = g_strdup_printf("%s, %s", *from, tmp);
1517                                 g_free(*from);
1518                                 *from = new_from;
1519                         }
1520                 } else if (cc && !g_ascii_strcasecmp(field, "cc")) {
1521                         if (!*cc) {
1522                                 *cc = decode_uri_gdup(value);
1523                         } else {
1524                                 gchar *tmp = decode_uri_gdup(value);
1525                                 gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
1526                                 g_free(*cc);
1527                                 *cc = new_cc;
1528                         }
1529                 } else if (bcc && !g_ascii_strcasecmp(field, "bcc")) {
1530                         if (!*bcc) {
1531                                 *bcc = decode_uri_gdup(value);
1532                         } else {
1533                                 gchar *tmp = decode_uri_gdup(value);
1534                                 gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
1535                                 g_free(*bcc);
1536                                 *bcc = new_bcc;
1537                         }
1538                 } else if (subject && !*subject &&
1539                            !g_ascii_strcasecmp(field, "subject")) {
1540                         *subject = decode_uri_gdup(value);
1541                 } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
1542                         *body = decode_uri_gdup(value);
1543                 } else if (body && !*body && !g_ascii_strcasecmp(field, "insert")) {
1544                         gchar *tmp = decode_uri_gdup(value);
1545                         if (!g_file_get_contents(tmp, body, NULL, NULL)) {
1546                                 g_warning("couldn't set insert file '%s' in body", value);
1547                         }
1548                         g_free(tmp);
1549                 } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
1550                         int i = 0;
1551                         gchar *tmp = decode_uri_gdup(value);
1552                         for (; forbidden_uris[i]; i++) {
1553                                 if (strstr(tmp, forbidden_uris[i])) {
1554                                         g_print("Refusing to attach '%s', potential private data leak\n",
1555                                                         tmp);
1556                                         g_free(tmp);
1557                                         break;
1558                                 }
1559                         }
1560                         if (tmp) {
1561                                 /* attach is correct */
1562                                 num_attach++;
1563                                 my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
1564                                 my_att[num_attach-1] = tmp;
1565                                 my_att[num_attach] = NULL;
1566                         }
1567                 } else if (inreplyto && !*inreplyto &&
1568                            !g_ascii_strcasecmp(field, "in-reply-to")) {
1569                         *inreplyto = decode_uri_gdup(value);
1570                 }
1571         }
1572
1573         if (attach)
1574                 *attach = my_att;
1575         return 0;
1576 }
1577
1578
1579 #ifdef G_OS_WIN32
1580 #include <windows.h>
1581 #ifndef CSIDL_APPDATA
1582 #define CSIDL_APPDATA 0x001a
1583 #endif
1584 #ifndef CSIDL_LOCAL_APPDATA
1585 #define CSIDL_LOCAL_APPDATA 0x001c
1586 #endif
1587 #ifndef CSIDL_FLAG_CREATE
1588 #define CSIDL_FLAG_CREATE 0x8000
1589 #endif
1590 #define DIM(v)               (sizeof(v)/sizeof((v)[0]))
1591
1592 #define RTLD_LAZY 0
1593 const char *
1594 w32_strerror (int w32_errno)
1595 {
1596   static char strerr[256];
1597   int ec = (int)GetLastError ();
1598
1599   if (w32_errno == 0)
1600     w32_errno = ec;
1601   FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, w32_errno,
1602                  MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1603                  strerr, DIM (strerr)-1, NULL);
1604   return strerr;
1605 }
1606
1607 static __inline__ void *
1608 dlopen (const char * name, int flag)
1609 {
1610   void * hd = LoadLibrary (name);
1611   return hd;
1612 }
1613
1614 static __inline__ void *
1615 dlsym (void * hd, const char * sym)
1616 {
1617   if (hd && sym)
1618     {
1619       void * fnc = GetProcAddress (hd, sym);
1620       if (!fnc)
1621         return NULL;
1622       return fnc;
1623     }
1624   return NULL;
1625 }
1626
1627
1628 static __inline__ const char *
1629 dlerror (void)
1630 {
1631   return w32_strerror (0);
1632 }
1633
1634
1635 static __inline__ int
1636 dlclose (void * hd)
1637 {
1638   if (hd)
1639     {
1640       FreeLibrary (hd);
1641       return 0;
1642     }
1643   return -1;
1644 }
1645
1646 static HRESULT
1647 w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
1648 {
1649   static int initialized;
1650   static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
1651
1652   if (!initialized)
1653     {
1654       static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
1655       void *handle;
1656       int i;
1657
1658       initialized = 1;
1659
1660       for (i=0, handle = NULL; !handle && dllnames[i]; i++)
1661         {
1662           handle = dlopen (dllnames[i], RTLD_LAZY);
1663           if (handle)
1664             {
1665               func = dlsym (handle, "SHGetFolderPathW");
1666               if (!func)
1667                 {
1668                   dlclose (handle);
1669                   handle = NULL;
1670                 }
1671             }
1672         }
1673     }
1674
1675   if (func)
1676     return func (a,b,c,d,e);
1677   else
1678     return -1;
1679 }
1680
1681 /* Returns a static string with the directroy from which the module
1682    has been loaded.  Returns an empty string on error. */
1683 static char *w32_get_module_dir(void)
1684 {
1685         static char *moddir;
1686
1687         if (!moddir) {
1688                 char name[MAX_PATH+10];
1689                 char *p;
1690
1691                 if ( !GetModuleFileNameA (0, name, sizeof (name)-10) )
1692                         *name = 0;
1693                 else {
1694                         p = strrchr (name, '\\');
1695                         if (p)
1696                                 *p = 0;
1697                         else
1698                                 *name = 0;
1699                 }
1700                 moddir = g_strdup (name);
1701         }
1702         return moddir;
1703 }
1704 #endif /* G_OS_WIN32 */
1705
1706 /* Return a static string with the locale dir. */
1707 const gchar *get_locale_dir(void)
1708 {
1709         static gchar *loc_dir;
1710
1711 #ifdef G_OS_WIN32
1712         if (!loc_dir)
1713                 loc_dir = g_strconcat(w32_get_module_dir(), G_DIR_SEPARATOR_S,
1714                                       "\\share\\locale", NULL);
1715 #endif
1716         if (!loc_dir)
1717                 loc_dir = LOCALEDIR;
1718         
1719         return loc_dir;
1720 }
1721
1722
1723 const gchar *get_home_dir(void)
1724 {
1725 #ifdef G_OS_WIN32
1726         static char home_dir_utf16[MAX_PATH] = "";
1727         static gchar *home_dir_utf8 = NULL;
1728         if (home_dir_utf16[0] == '\0') {
1729                 if (w32_shgetfolderpath
1730                             (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
1731                              NULL, 0, home_dir_utf16) < 0)
1732                                 strcpy (home_dir_utf16, "C:\\Claws Mail");
1733                 home_dir_utf8 = g_utf16_to_utf8 ((const gunichar2 *)home_dir_utf16, -1, NULL, NULL, NULL);
1734         }
1735         return home_dir_utf8;
1736 #else
1737         static const gchar *homeenv = NULL;
1738
1739         if (homeenv)
1740                 return homeenv;
1741
1742         if (!homeenv && g_getenv("HOME") != NULL)
1743                 homeenv = g_strdup(g_getenv("HOME"));
1744         if (!homeenv)
1745                 homeenv = g_get_home_dir();
1746
1747         return homeenv;
1748 #endif
1749 }
1750
1751 static gchar *claws_rc_dir = NULL;
1752 static gboolean rc_dir_alt = FALSE;
1753 const gchar *get_rc_dir(void)
1754 {
1755
1756         if (!claws_rc_dir) {
1757                 claws_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1758                                      RC_DIR, NULL);
1759                 debug_print("using default rc_dir %s\n", claws_rc_dir);
1760         }
1761         return claws_rc_dir;
1762 }
1763
1764 void set_rc_dir(const gchar *dir)
1765 {
1766         gchar *canonical_dir;
1767         if (claws_rc_dir != NULL) {
1768                 g_print("Error: rc_dir already set\n");
1769         } else {
1770                 int err = cm_canonicalize_filename(dir, &canonical_dir);
1771                 int len;
1772
1773                 if (err) {
1774                         g_print("Error looking for %s: %d(%s)\n",
1775                                 dir, -err, g_strerror(-err));
1776                         exit(0);
1777                 }
1778                 rc_dir_alt = TRUE;
1779
1780                 claws_rc_dir = canonical_dir;
1781                 
1782                 len = strlen(claws_rc_dir);
1783                 if (claws_rc_dir[len - 1] == G_DIR_SEPARATOR)
1784                         claws_rc_dir[len - 1] = '\0';
1785                 
1786                 debug_print("set rc_dir to %s\n", claws_rc_dir);
1787                 if (!is_dir_exist(claws_rc_dir)) {
1788                         if (make_dir_hier(claws_rc_dir) != 0) {
1789                                 g_print("Error: can't create %s\n",
1790                                 claws_rc_dir);
1791                                 exit(0);
1792                         }
1793                 }
1794         }
1795 }
1796
1797 gboolean rc_dir_is_alt(void) {
1798         return rc_dir_alt;
1799 }
1800
1801 const gchar *get_mail_base_dir(void)
1802 {
1803         return get_home_dir();
1804 }
1805
1806 const gchar *get_news_cache_dir(void)
1807 {
1808         static gchar *news_cache_dir = NULL;
1809         if (!news_cache_dir)
1810                 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1811                                              NEWS_CACHE_DIR, NULL);
1812
1813         return news_cache_dir;
1814 }
1815
1816 const gchar *get_imap_cache_dir(void)
1817 {
1818         static gchar *imap_cache_dir = NULL;
1819
1820         if (!imap_cache_dir)
1821                 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1822                                              IMAP_CACHE_DIR, NULL);
1823
1824         return imap_cache_dir;
1825 }
1826
1827 const gchar *get_mime_tmp_dir(void)
1828 {
1829         static gchar *mime_tmp_dir = NULL;
1830
1831         if (!mime_tmp_dir)
1832                 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1833                                            MIME_TMP_DIR, NULL);
1834
1835         return mime_tmp_dir;
1836 }
1837
1838 const gchar *get_template_dir(void)
1839 {
1840         static gchar *template_dir = NULL;
1841
1842         if (!template_dir)
1843                 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1844                                            TEMPLATE_DIR, NULL);
1845
1846         return template_dir;
1847 }
1848
1849 #ifdef G_OS_WIN32
1850 const gchar *w32_get_cert_file(void)
1851 {
1852         const gchar *cert_file = NULL;
1853         if (!cert_file)
1854                 cert_file = g_strconcat(w32_get_module_dir(),
1855                                  "\\share\\claws-mail\\",
1856                                 "ca-certificates.crt",
1857                                 NULL);  
1858         return cert_file;
1859 }
1860 #endif
1861
1862 /* Return the filepath of the claws-mail.desktop file */
1863 const gchar *get_desktop_file(void)
1864 {
1865 #ifdef DESKTOPFILEPATH
1866   return DESKTOPFILEPATH;
1867 #else
1868   return NULL;
1869 #endif
1870 }
1871
1872 /* Return the default directory for Plugins. */
1873 const gchar *get_plugin_dir(void)
1874 {
1875 #ifdef G_OS_WIN32
1876         static gchar *plugin_dir = NULL;
1877
1878         if (!plugin_dir)
1879                 plugin_dir = g_strconcat(w32_get_module_dir(),
1880                                          "\\lib\\claws-mail\\plugins\\",
1881                                          NULL);
1882         return plugin_dir;
1883 #else
1884         if (is_dir_exist(PLUGINDIR))
1885                 return PLUGINDIR;
1886         else {
1887                 static gchar *plugin_dir = NULL;
1888                 if (!plugin_dir)
1889                         plugin_dir = g_strconcat(get_rc_dir(), 
1890                                 G_DIR_SEPARATOR_S, "plugins", 
1891                                 G_DIR_SEPARATOR_S, NULL);
1892                 return plugin_dir;                      
1893         }
1894 #endif
1895 }
1896
1897
1898 #ifdef G_OS_WIN32
1899 /* Return the default directory for Themes. */
1900 const gchar *w32_get_themes_dir(void)
1901 {
1902         static gchar *themes_dir = NULL;
1903
1904         if (!themes_dir)
1905                 themes_dir = g_strconcat(w32_get_module_dir(),
1906                                          "\\share\\claws-mail\\themes",
1907                                          NULL);
1908         return themes_dir;
1909 }
1910 #endif
1911
1912 const gchar *get_tmp_dir(void)
1913 {
1914         static gchar *tmp_dir = NULL;
1915
1916         if (!tmp_dir)
1917                 tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1918                                       TMP_DIR, NULL);
1919
1920         return tmp_dir;
1921 }
1922
1923 gchar *get_tmp_file(void)
1924 {
1925         gchar *tmp_file;
1926         static guint32 id = 0;
1927
1928         tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
1929                                    get_tmp_dir(), G_DIR_SEPARATOR, id++);
1930
1931         return tmp_file;
1932 }
1933
1934 const gchar *get_domain_name(void)
1935 {
1936 #ifdef G_OS_UNIX
1937         static gchar *domain_name = NULL;
1938         struct addrinfo hints, *res;
1939         char hostname[256];
1940         int s;
1941
1942         if (!domain_name) {
1943                 if (gethostname(hostname, sizeof(hostname)) != 0) {
1944                         perror("gethostname");
1945                         domain_name = "localhost";
1946                 } else {
1947                         memset(&hints, 0, sizeof(struct addrinfo));
1948                         hints.ai_family = AF_UNSPEC;
1949                         hints.ai_socktype = 0;
1950                         hints.ai_flags = AI_CANONNAME;
1951                         hints.ai_protocol = 0;
1952
1953                         s = getaddrinfo(hostname, NULL, &hints, &res);
1954                         if (s != 0) {
1955                                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
1956                                 domain_name = g_strdup(hostname);
1957                         } else {
1958                                 domain_name = g_strdup(res->ai_canonname);
1959                                 freeaddrinfo(res);
1960                         }
1961                 }
1962                 debug_print("domain name = %s\n", domain_name);
1963         }
1964
1965         return domain_name;
1966 #else
1967         return "localhost";
1968 #endif
1969 }
1970
1971 off_t get_file_size(const gchar *file)
1972 {
1973 #ifdef G_OS_WIN32
1974         GFile *f;
1975         GFileInfo *fi;
1976         GError *error = NULL;
1977         goffset size;
1978
1979         f = g_file_new_for_path(file);
1980         fi = g_file_query_info(f, "standard::size",
1981                         G_FILE_QUERY_INFO_NONE, NULL, &error);
1982         if (error != NULL) {
1983                 debug_print("get_file_size error: %s\n", error->message);
1984                 g_error_free(error);
1985                 g_object_unref(f);
1986                 return -1;
1987         }
1988         size = g_file_info_get_size(fi);
1989         g_object_unref(fi);
1990         g_object_unref(f);
1991         return size;
1992
1993 #else
1994         GStatBuf s;
1995
1996         if (g_stat(file, &s) < 0) {
1997                 FILE_OP_ERROR(file, "stat");
1998                 return -1;
1999         }
2000
2001         return s.st_size;
2002 #endif
2003 }
2004
2005 time_t get_file_mtime(const gchar *file)
2006 {
2007         GStatBuf s;
2008
2009         if (g_stat(file, &s) < 0) {
2010                 FILE_OP_ERROR(file, "stat");
2011                 return -1;
2012         }
2013
2014         return s.st_mtime;
2015 }
2016
2017 gboolean file_exist(const gchar *file, gboolean allow_fifo)
2018 {
2019         GStatBuf s;
2020
2021         if (file == NULL)
2022                 return FALSE;
2023
2024         if (g_stat(file, &s) < 0) {
2025                 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
2026                 return FALSE;
2027         }
2028
2029         if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
2030                 return TRUE;
2031
2032         return FALSE;
2033 }
2034
2035
2036 /* Test on whether FILE is a relative file name. This is
2037  * straightforward for Unix but more complex for Windows. */
2038 gboolean is_relative_filename(const gchar *file)
2039 {
2040         if (!file)
2041                 return TRUE;
2042 #ifdef G_OS_WIN32
2043         if ( *file == '\\' && file[1] == '\\' && strchr (file+2, '\\') )
2044                 return FALSE; /* Prefixed with a hostname - this can't
2045                                * be a relative name. */
2046
2047         if ( ((*file >= 'a' && *file <= 'z')
2048               || (*file >= 'A' && *file <= 'Z'))
2049              && file[1] == ':')
2050                 file += 2;  /* Skip drive letter. */
2051
2052         return !(*file == '\\' || *file == '/');
2053 #else
2054         return !(*file == G_DIR_SEPARATOR);
2055 #endif
2056 }
2057
2058
2059 gboolean is_dir_exist(const gchar *dir)
2060 {
2061         if (dir == NULL)
2062                 return FALSE;
2063
2064         return g_file_test(dir, G_FILE_TEST_IS_DIR);
2065 }
2066
2067 gboolean is_file_entry_exist(const gchar *file)
2068 {
2069         if (file == NULL)
2070                 return FALSE;
2071
2072         return g_file_test(file, G_FILE_TEST_EXISTS);
2073 }
2074
2075 gboolean dirent_is_regular_file(struct dirent *d)
2076 {
2077 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
2078         if (d->d_type == DT_REG)
2079                 return TRUE;
2080         else if (d->d_type != DT_UNKNOWN)
2081                 return FALSE;
2082 #endif
2083
2084         return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
2085 }
2086
2087 gint change_dir(const gchar *dir)
2088 {
2089         gchar *prevdir = NULL;
2090
2091         if (debug_mode)
2092                 prevdir = g_get_current_dir();
2093
2094         if (g_chdir(dir) < 0) {
2095                 FILE_OP_ERROR(dir, "chdir");
2096                 if (debug_mode) g_free(prevdir);
2097                 return -1;
2098         } else if (debug_mode) {
2099                 gchar *cwd;
2100
2101                 cwd = g_get_current_dir();
2102                 if (strcmp(prevdir, cwd) != 0)
2103                         g_print("current dir: %s\n", cwd);
2104                 g_free(cwd);
2105                 g_free(prevdir);
2106         }
2107
2108         return 0;
2109 }
2110
2111 gint make_dir(const gchar *dir)
2112 {
2113         if (g_mkdir(dir, S_IRWXU) < 0) {
2114                 FILE_OP_ERROR(dir, "mkdir");
2115                 return -1;
2116         }
2117         if (g_chmod(dir, S_IRWXU) < 0)
2118                 FILE_OP_ERROR(dir, "chmod");
2119
2120         return 0;
2121 }
2122
2123 gint make_dir_hier(const gchar *dir)
2124 {
2125         gchar *parent_dir;
2126         const gchar *p;
2127
2128         for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
2129                 parent_dir = g_strndup(dir, p - dir);
2130                 if (*parent_dir != '\0') {
2131                         if (!is_dir_exist(parent_dir)) {
2132                                 if (make_dir(parent_dir) < 0) {
2133                                         g_free(parent_dir);
2134                                         return -1;
2135                                 }
2136                         }
2137                 }
2138                 g_free(parent_dir);
2139         }
2140
2141         if (!is_dir_exist(dir)) {
2142                 if (make_dir(dir) < 0)
2143                         return -1;
2144         }
2145
2146         return 0;
2147 }
2148
2149 gint remove_all_files(const gchar *dir)
2150 {
2151         GDir *dp;
2152         const gchar *file_name;
2153         gchar *tmp;
2154
2155         if ((dp = g_dir_open(dir, 0, NULL)) == NULL) {
2156                 g_warning("failed to open directory: %s", dir);
2157                 return -1;
2158         }
2159
2160         while ((file_name = g_dir_read_name(dp)) != NULL) {
2161                 tmp = g_strconcat(dir, G_DIR_SEPARATOR_S, file_name, NULL);
2162                 if (claws_unlink(tmp) < 0)
2163                         FILE_OP_ERROR(tmp, "unlink");
2164                 g_free(tmp);
2165         }
2166
2167         g_dir_close(dp);
2168
2169         return 0;
2170 }
2171
2172 gint remove_numbered_files(const gchar *dir, guint first, guint last)
2173 {
2174         GDir *dp;
2175         const gchar *dir_name;
2176         gchar *prev_dir;
2177         gint file_no;
2178
2179         if (first == last) {
2180                 /* Skip all the dir reading part. */
2181                 gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
2182                 if (is_dir_exist(filename)) {
2183                         /* a numbered directory with this name exists,
2184                          * remove the dot-file instead */
2185                         g_free(filename);
2186                         filename = g_strdup_printf("%s%s.%u", dir, G_DIR_SEPARATOR_S, first);
2187                 }
2188                 if (claws_unlink(filename) < 0) {
2189                         FILE_OP_ERROR(filename, "unlink");
2190                         g_free(filename);
2191                         return -1;
2192                 }
2193                 g_free(filename);
2194                 return 0;
2195         }
2196
2197         prev_dir = g_get_current_dir();
2198
2199         if (g_chdir(dir) < 0) {
2200                 FILE_OP_ERROR(dir, "chdir");
2201                 g_free(prev_dir);
2202                 return -1;
2203         }
2204
2205         if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
2206                 g_warning("failed to open directory: %s", dir);
2207                 g_free(prev_dir);
2208                 return -1;
2209         }
2210
2211         while ((dir_name = g_dir_read_name(dp)) != NULL) {
2212                 file_no = to_number(dir_name);
2213                 if (file_no > 0 && first <= file_no && file_no <= last) {
2214                         if (is_dir_exist(dir_name)) {
2215                                 gchar *dot_file = g_strdup_printf(".%s", dir_name);
2216                                 if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
2217                                         FILE_OP_ERROR(dot_file, "unlink");
2218                                 }
2219                                 g_free(dot_file);
2220                                 continue;
2221                         }
2222                         if (claws_unlink(dir_name) < 0)
2223                                 FILE_OP_ERROR(dir_name, "unlink");
2224                 }
2225         }
2226
2227         g_dir_close(dp);
2228
2229         if (g_chdir(prev_dir) < 0) {
2230                 FILE_OP_ERROR(prev_dir, "chdir");
2231                 g_free(prev_dir);
2232                 return -1;
2233         }
2234
2235         g_free(prev_dir);
2236
2237         return 0;
2238 }
2239
2240 gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
2241 {
2242         GDir *dp;
2243         const gchar *dir_name;
2244         gchar *prev_dir;
2245         gint file_no;
2246         GHashTable *wanted_files;
2247         GSList *cur;
2248         GError *error = NULL;
2249
2250         if (numberlist == NULL)
2251             return 0;
2252
2253         prev_dir = g_get_current_dir();
2254
2255         if (g_chdir(dir) < 0) {
2256                 FILE_OP_ERROR(dir, "chdir");
2257                 g_free(prev_dir);
2258                 return -1;
2259         }
2260
2261         if ((dp = g_dir_open(".", 0, &error)) == NULL) {
2262                 g_message("Couldn't open current directory: %s (%d).\n",
2263                                 error->message, error->code);
2264                 g_error_free(error);
2265                 g_free(prev_dir);
2266                 return -1;
2267         }
2268
2269         wanted_files = g_hash_table_new(g_direct_hash, g_direct_equal);
2270         for (cur = numberlist; cur != NULL; cur = cur->next) {
2271                 /* numberlist->data is expected to be GINT_TO_POINTER */
2272                 g_hash_table_insert(wanted_files, cur->data, GINT_TO_POINTER(1));
2273         }
2274
2275         while ((dir_name = g_dir_read_name(dp)) != NULL) {
2276                 file_no = to_number(dir_name);
2277                 if (is_dir_exist(dir_name))
2278                         continue;
2279                 if (file_no > 0 && g_hash_table_lookup(wanted_files, GINT_TO_POINTER(file_no)) == NULL) {
2280                         debug_print("removing unwanted file %d from %s\n", file_no, dir);
2281                         if (is_dir_exist(dir_name)) {
2282                                 gchar *dot_file = g_strdup_printf(".%s", dir_name);
2283                                 if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
2284                                         FILE_OP_ERROR(dot_file, "unlink");
2285                                 }
2286                                 g_free(dot_file);
2287                                 continue;
2288                         }
2289                         if (claws_unlink(dir_name) < 0)
2290                                 FILE_OP_ERROR(dir_name, "unlink");
2291                 }
2292         }
2293
2294         g_dir_close(dp);
2295         g_hash_table_destroy(wanted_files);
2296
2297         if (g_chdir(prev_dir) < 0) {
2298                 FILE_OP_ERROR(prev_dir, "chdir");
2299                 g_free(prev_dir);
2300                 return -1;
2301         }
2302
2303         g_free(prev_dir);
2304
2305         return 0;
2306 }
2307
2308 gint remove_all_numbered_files(const gchar *dir)
2309 {
2310         return remove_numbered_files(dir, 0, UINT_MAX);
2311 }
2312
2313 gint remove_dir_recursive(const gchar *dir)
2314 {
2315         GStatBuf s;
2316         GDir *dp;
2317         const gchar *dir_name;
2318         gchar *prev_dir;
2319
2320         if (g_stat(dir, &s) < 0) {
2321                 FILE_OP_ERROR(dir, "stat");
2322                 if (ENOENT == errno) return 0;
2323                 return -(errno);
2324         }
2325
2326         if (!S_ISDIR(s.st_mode)) {
2327                 if (claws_unlink(dir) < 0) {
2328                         FILE_OP_ERROR(dir, "unlink");
2329                         return -(errno);
2330                 }
2331
2332                 return 0;
2333         }
2334
2335         prev_dir = g_get_current_dir();
2336         /* g_print("prev_dir = %s\n", prev_dir); */
2337
2338         if (!path_cmp(prev_dir, dir)) {
2339                 g_free(prev_dir);
2340                 if (g_chdir("..") < 0) {
2341                         FILE_OP_ERROR(dir, "chdir");
2342                         return -(errno);
2343                 }
2344                 prev_dir = g_get_current_dir();
2345         }
2346
2347         if (g_chdir(dir) < 0) {
2348                 FILE_OP_ERROR(dir, "chdir");
2349                 g_free(prev_dir);
2350                 return -(errno);
2351         }
2352
2353         if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
2354                 g_warning("failed to open directory: %s", dir);
2355                 g_chdir(prev_dir);
2356                 g_free(prev_dir);
2357                 return -(errno);
2358         }
2359
2360         /* remove all files in the directory */
2361         while ((dir_name = g_dir_read_name(dp)) != NULL) {
2362                 /* g_print("removing %s\n", dir_name); */
2363
2364                 if (is_dir_exist(dir_name)) {
2365                         gint ret;
2366
2367                         if ((ret = remove_dir_recursive(dir_name)) < 0) {
2368                                 g_warning("can't remove directory: %s", dir_name);
2369                                 return ret;
2370                         }
2371                 } else {
2372                         if (claws_unlink(dir_name) < 0)
2373                                 FILE_OP_ERROR(dir_name, "unlink");
2374                 }
2375         }
2376
2377         g_dir_close(dp);
2378
2379         if (g_chdir(prev_dir) < 0) {
2380                 FILE_OP_ERROR(prev_dir, "chdir");
2381                 g_free(prev_dir);
2382                 return -(errno);
2383         }
2384
2385         g_free(prev_dir);
2386
2387         if (g_rmdir(dir) < 0) {
2388                 FILE_OP_ERROR(dir, "rmdir");
2389                 return -(errno);
2390         }
2391
2392         return 0;
2393 }
2394
2395 gint rename_force(const gchar *oldpath, const gchar *newpath)
2396 {
2397 #ifndef G_OS_UNIX
2398         if (!is_file_entry_exist(oldpath)) {
2399                 errno = ENOENT;
2400                 return -1;
2401         }
2402         if (is_file_exist(newpath)) {
2403                 if (claws_unlink(newpath) < 0)
2404                         FILE_OP_ERROR(newpath, "unlink");
2405         }
2406 #endif
2407         return g_rename(oldpath, newpath);
2408 }
2409
2410 /*
2411  * Append src file body to the tail of dest file.
2412  * Now keep_backup has no effects.
2413  */
2414 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2415 {
2416         FILE *src_fp, *dest_fp;
2417         gint n_read;
2418         gchar buf[BUFSIZ];
2419
2420         gboolean err = FALSE;
2421
2422         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
2423                 FILE_OP_ERROR(src, "claws_fopen");
2424                 return -1;
2425         }
2426
2427         if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
2428                 FILE_OP_ERROR(dest, "claws_fopen");
2429                 claws_fclose(src_fp);
2430                 return -1;
2431         }
2432
2433         if (change_file_mode_rw(dest_fp, dest) < 0) {
2434                 FILE_OP_ERROR(dest, "chmod");
2435                 g_warning("can't change file mode: %s", dest);
2436         }
2437
2438         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2439                 if (n_read < sizeof(buf) && claws_ferror(src_fp))
2440                         break;
2441                 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
2442                         g_warning("writing to %s failed.", dest);
2443                         claws_fclose(dest_fp);
2444                         claws_fclose(src_fp);
2445                         claws_unlink(dest);
2446                         return -1;
2447                 }
2448         }
2449
2450         if (claws_ferror(src_fp)) {
2451                 FILE_OP_ERROR(src, "claws_fread");
2452                 err = TRUE;
2453         }
2454         claws_fclose(src_fp);
2455         if (claws_fclose(dest_fp) == EOF) {
2456                 FILE_OP_ERROR(dest, "claws_fclose");
2457                 err = TRUE;
2458         }
2459
2460         if (err) {
2461                 claws_unlink(dest);
2462                 return -1;
2463         }
2464
2465         return 0;
2466 }
2467
2468 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2469 {
2470         FILE *src_fp, *dest_fp;
2471         gint n_read;
2472         gchar buf[BUFSIZ];
2473         gchar *dest_bak = NULL;
2474         gboolean err = FALSE;
2475
2476         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
2477                 FILE_OP_ERROR(src, "claws_fopen");
2478                 return -1;
2479         }
2480         if (is_file_exist(dest)) {
2481                 dest_bak = g_strconcat(dest, ".bak", NULL);
2482                 if (rename_force(dest, dest_bak) < 0) {
2483                         FILE_OP_ERROR(dest, "rename");
2484                         claws_fclose(src_fp);
2485                         g_free(dest_bak);
2486                         return -1;
2487                 }
2488         }
2489
2490         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
2491                 FILE_OP_ERROR(dest, "claws_fopen");
2492                 claws_fclose(src_fp);
2493                 if (dest_bak) {
2494                         if (rename_force(dest_bak, dest) < 0)
2495                                 FILE_OP_ERROR(dest_bak, "rename");
2496                         g_free(dest_bak);
2497                 }
2498                 return -1;
2499         }
2500
2501         if (change_file_mode_rw(dest_fp, dest) < 0) {
2502                 FILE_OP_ERROR(dest, "chmod");
2503                 g_warning("can't change file mode: %s", dest);
2504         }
2505
2506         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2507                 if (n_read < sizeof(buf) && claws_ferror(src_fp))
2508                         break;
2509                 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
2510                         g_warning("writing to %s failed.", dest);
2511                         claws_fclose(dest_fp);
2512                         claws_fclose(src_fp);
2513                         claws_unlink(dest);
2514                         if (dest_bak) {
2515                                 if (rename_force(dest_bak, dest) < 0)
2516                                         FILE_OP_ERROR(dest_bak, "rename");
2517                                 g_free(dest_bak);
2518                         }
2519                         return -1;
2520                 }
2521         }
2522
2523         if (claws_ferror(src_fp)) {
2524                 FILE_OP_ERROR(src, "claws_fread");
2525                 err = TRUE;
2526         }
2527         claws_fclose(src_fp);
2528         if (claws_safe_fclose(dest_fp) == EOF) {
2529                 FILE_OP_ERROR(dest, "claws_fclose");
2530                 err = TRUE;
2531         }
2532
2533         if (err) {
2534                 claws_unlink(dest);
2535                 if (dest_bak) {
2536                         if (rename_force(dest_bak, dest) < 0)
2537                                 FILE_OP_ERROR(dest_bak, "rename");
2538                         g_free(dest_bak);
2539                 }
2540                 return -1;
2541         }
2542
2543         if (keep_backup == FALSE && dest_bak)
2544                 claws_unlink(dest_bak);
2545
2546         g_free(dest_bak);
2547
2548         return 0;
2549 }
2550
2551 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
2552 {
2553         if (overwrite == FALSE && is_file_exist(dest)) {
2554                 g_warning("move_file(): file %s already exists.", dest);
2555                 return -1;
2556         }
2557
2558         if (rename_force(src, dest) == 0) return 0;
2559
2560         if (EXDEV != errno) {
2561                 FILE_OP_ERROR(src, "rename");
2562                 return -1;
2563         }
2564
2565         if (copy_file(src, dest, FALSE) < 0) return -1;
2566
2567         claws_unlink(src);
2568
2569         return 0;
2570 }
2571
2572 gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
2573 {
2574         gint n_read;
2575         gint bytes_left, to_read;
2576         gchar buf[BUFSIZ];
2577
2578         if (fseek(fp, offset, SEEK_SET) < 0) {
2579                 perror("fseek");
2580                 return -1;
2581         }
2582
2583         bytes_left = length;
2584         to_read = MIN(bytes_left, sizeof(buf));
2585
2586         while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
2587                 if (n_read < to_read && claws_ferror(fp))
2588                         break;
2589                 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
2590                         return -1;
2591                 }
2592                 bytes_left -= n_read;
2593                 if (bytes_left == 0)
2594                         break;
2595                 to_read = MIN(bytes_left, sizeof(buf));
2596         }
2597
2598         if (claws_ferror(fp)) {
2599                 perror("claws_fread");
2600                 return -1;
2601         }
2602
2603         return 0;
2604 }
2605
2606 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
2607 {
2608         FILE *dest_fp;
2609         gboolean err = FALSE;
2610
2611         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
2612                 FILE_OP_ERROR(dest, "claws_fopen");
2613                 return -1;
2614         }
2615
2616         if (change_file_mode_rw(dest_fp, dest) < 0) {
2617                 FILE_OP_ERROR(dest, "chmod");
2618                 g_warning("can't change file mode: %s", dest);
2619         }
2620
2621         if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
2622                 err = TRUE;
2623
2624         if (claws_safe_fclose(dest_fp) == EOF) {
2625                 FILE_OP_ERROR(dest, "claws_fclose");
2626                 err = TRUE;
2627         }
2628
2629         if (err) {
2630                 g_warning("writing to %s failed.", dest);
2631                 claws_unlink(dest);
2632                 return -1;
2633         }
2634
2635         return 0;
2636 }
2637
2638 /* convert line endings into CRLF. If the last line doesn't end with
2639  * linebreak, add it.
2640  */
2641 gchar *canonicalize_str(const gchar *str)
2642 {
2643         const gchar *p;
2644         guint new_len = 0;
2645         gchar *out, *outp;
2646
2647         for (p = str; *p != '\0'; ++p) {
2648                 if (*p != '\r') {
2649                         ++new_len;
2650                         if (*p == '\n')
2651                                 ++new_len;
2652                 }
2653         }
2654         if (p == str || *(p - 1) != '\n')
2655                 new_len += 2;
2656
2657         out = outp = g_malloc(new_len + 1);
2658         for (p = str; *p != '\0'; ++p) {
2659                 if (*p != '\r') {
2660                         if (*p == '\n')
2661                                 *outp++ = '\r';
2662                         *outp++ = *p;
2663                 }
2664         }
2665         if (p == str || *(p - 1) != '\n') {
2666                 *outp++ = '\r';
2667                 *outp++ = '\n';
2668         }
2669         *outp = '\0';
2670
2671         return out;
2672 }
2673
2674 gint canonicalize_file(const gchar *src, const gchar *dest)
2675 {
2676         FILE *src_fp, *dest_fp;
2677         gchar buf[BUFFSIZE];
2678         gint len;
2679         gboolean err = FALSE;
2680         gboolean last_linebreak = FALSE;
2681
2682         if (src == NULL || dest == NULL)
2683                 return -1;
2684
2685         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
2686                 FILE_OP_ERROR(src, "claws_fopen");
2687                 return -1;
2688         }
2689
2690         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
2691                 FILE_OP_ERROR(dest, "claws_fopen");
2692                 claws_fclose(src_fp);
2693                 return -1;
2694         }
2695
2696         if (change_file_mode_rw(dest_fp, dest) < 0) {
2697                 FILE_OP_ERROR(dest, "chmod");
2698                 g_warning("can't change file mode: %s", dest);
2699         }
2700
2701         while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
2702                 gint r = 0;
2703
2704                 len = strlen(buf);
2705                 if (len == 0) break;
2706                 last_linebreak = FALSE;
2707
2708                 if (buf[len - 1] != '\n') {
2709                         last_linebreak = TRUE;
2710                         r = claws_fputs(buf, dest_fp);
2711                 } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
2712                         r = claws_fputs(buf, dest_fp);
2713                 } else {
2714                         if (len > 1) {
2715                                 r = claws_fwrite(buf, 1, len - 1, dest_fp);
2716                                 if (r != (len -1))
2717                                         r = EOF;
2718                         }
2719                         if (r != EOF)
2720                                 r = claws_fputs("\r\n", dest_fp);
2721                 }
2722
2723                 if (r == EOF) {
2724                         g_warning("writing to %s failed.", dest);
2725                         claws_fclose(dest_fp);
2726                         claws_fclose(src_fp);
2727                         claws_unlink(dest);
2728                         return -1;
2729                 }
2730         }
2731
2732         if (last_linebreak == TRUE) {
2733                 if (claws_fputs("\r\n", dest_fp) == EOF)
2734                         err = TRUE;
2735         }
2736
2737         if (claws_ferror(src_fp)) {
2738                 FILE_OP_ERROR(src, "claws_fgets");
2739                 err = TRUE;
2740         }
2741         claws_fclose(src_fp);
2742         if (claws_safe_fclose(dest_fp) == EOF) {
2743                 FILE_OP_ERROR(dest, "claws_fclose");
2744                 err = TRUE;
2745         }
2746
2747         if (err) {
2748                 claws_unlink(dest);
2749                 return -1;
2750         }
2751
2752         return 0;
2753 }
2754
2755 gint canonicalize_file_replace(const gchar *file)
2756 {
2757         gchar *tmp_file;
2758
2759         tmp_file = get_tmp_file();
2760
2761         if (canonicalize_file(file, tmp_file) < 0) {
2762                 g_free(tmp_file);
2763                 return -1;
2764         }
2765
2766         if (move_file(tmp_file, file, TRUE) < 0) {
2767                 g_warning("can't replace file: %s", file);
2768                 claws_unlink(tmp_file);
2769                 g_free(tmp_file);
2770                 return -1;
2771         }
2772
2773         g_free(tmp_file);
2774         return 0;
2775 }
2776
2777 gchar *normalize_newlines(const gchar *str)
2778 {
2779         const gchar *p;
2780         gchar *out, *outp;
2781
2782         out = outp = g_malloc(strlen(str) + 1);
2783         for (p = str; *p != '\0'; ++p) {
2784                 if (*p == '\r') {
2785                         if (*(p + 1) != '\n')
2786                                 *outp++ = '\n';
2787                 } else
2788                         *outp++ = *p;
2789         }
2790
2791         *outp = '\0';
2792
2793         return out;
2794 }
2795
2796 gchar *get_outgoing_rfc2822_str(FILE *fp)
2797 {
2798         gchar buf[BUFFSIZE];
2799         GString *str;
2800         gchar *ret;
2801
2802         str = g_string_new(NULL);
2803
2804         /* output header part */
2805         while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
2806                 strretchomp(buf);
2807                 if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
2808                         gint next;
2809
2810                         for (;;) {
2811                                 next = fgetc(fp);
2812                                 if (next == EOF)
2813                                         break;
2814                                 else if (next != ' ' && next != '\t') {
2815                                         ungetc(next, fp);
2816                                         break;
2817                                 }
2818                                 if (claws_fgets(buf, sizeof(buf), fp) == NULL)
2819                                         break;
2820                         }
2821                 } else {
2822                         g_string_append(str, buf);
2823                         g_string_append(str, "\r\n");
2824                         if (buf[0] == '\0')
2825                                 break;
2826                 }
2827         }
2828
2829         /* output body part */
2830         while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
2831                 strretchomp(buf);
2832                 if (buf[0] == '.')
2833                         g_string_append_c(str, '.');
2834                 g_string_append(str, buf);
2835                 g_string_append(str, "\r\n");
2836         }
2837
2838         ret = str->str;
2839         g_string_free(str, FALSE);
2840
2841         return ret;
2842 }
2843
2844 /*
2845  * Create a new boundary in a way that it is very unlikely that this
2846  * will occur in the following text.  It would be easy to ensure
2847  * uniqueness if everything is either quoted-printable or base64
2848  * encoded (note that conversion is allowed), but because MIME bodies
2849  * may be nested, it may happen that the same boundary has already
2850  * been used.
2851  *
2852  *   boundary := 0*69<bchars> bcharsnospace
2853  *   bchars := bcharsnospace / " "
2854  *   bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
2855  *                  "+" / "_" / "," / "-" / "." /
2856  *                  "/" / ":" / "=" / "?"
2857  *
2858  * some special characters removed because of buggy MTAs
2859  */
2860
2861 gchar *generate_mime_boundary(const gchar *prefix)
2862 {
2863         static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2864                              "abcdefghijklmnopqrstuvwxyz"
2865                              "1234567890+_./=";
2866         gchar buf_uniq[24];
2867         gint i;
2868
2869         for (i = 0; i < sizeof(buf_uniq) - 1; i++)
2870                 buf_uniq[i] = tbl[g_random_int_range(0, sizeof(tbl) - 1)];
2871         buf_uniq[i] = '\0';
2872
2873         return g_strdup_printf("%s_/%s", prefix ? prefix : "MP",
2874                                buf_uniq);
2875 }
2876
2877 gint change_file_mode_rw(FILE *fp, const gchar *file)
2878 {
2879 #if HAVE_FCHMOD
2880         return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
2881 #else
2882         return g_chmod(file, S_IRUSR|S_IWUSR);
2883 #endif
2884 }
2885
2886 FILE *my_tmpfile(void)
2887 {
2888         const gchar suffix[] = ".XXXXXX";
2889         const gchar *tmpdir;
2890         guint tmplen;
2891         const gchar *progname;
2892         guint proglen;
2893         gchar *fname;
2894         gint fd;
2895         FILE *fp;
2896 #ifndef G_OS_WIN32
2897         gchar buf[2]="\0";
2898 #endif
2899
2900         tmpdir = get_tmp_dir();
2901         tmplen = strlen(tmpdir);
2902         progname = g_get_prgname();
2903         if (progname == NULL)
2904                 progname = "claws-mail";
2905         proglen = strlen(progname);
2906         Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
2907                 return tmpfile());
2908
2909         memcpy(fname, tmpdir, tmplen);
2910         fname[tmplen] = G_DIR_SEPARATOR;
2911         memcpy(fname + tmplen + 1, progname, proglen);
2912         memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
2913
2914         fd = g_mkstemp(fname);
2915         if (fd < 0)
2916                 return tmpfile();
2917
2918 #ifndef G_OS_WIN32
2919         claws_unlink(fname);
2920         
2921         /* verify that we can write in the file after unlinking */
2922         if (write(fd, buf, 1) < 0) {
2923                 close(fd);
2924                 return tmpfile();
2925         }
2926         
2927 #endif
2928
2929         fp = claws_fdopen(fd, "w+b");
2930         if (!fp)
2931                 close(fd);
2932         else {
2933                 rewind(fp);
2934                 return fp;
2935         }
2936
2937         return tmpfile();
2938 }
2939
2940 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
2941 {
2942         int fd;
2943         *filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
2944         fd = g_mkstemp(*filename);
2945         if (fd < 0)
2946                 return NULL;
2947         return claws_fdopen(fd, "w+");
2948 }
2949
2950 FILE *str_open_as_stream(const gchar *str)
2951 {
2952         FILE *fp;
2953         size_t len;
2954
2955         cm_return_val_if_fail(str != NULL, NULL);
2956
2957         fp = my_tmpfile();
2958         if (!fp) {
2959                 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
2960                 return NULL;
2961         }
2962
2963         len = strlen(str);
2964         if (len == 0) return fp;
2965
2966         if (claws_fwrite(str, 1, len, fp) != len) {
2967                 FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
2968                 claws_fclose(fp);
2969                 return NULL;
2970         }
2971
2972         rewind(fp);
2973         return fp;
2974 }
2975
2976 gint str_write_to_file(const gchar *str, const gchar *file)
2977 {
2978         FILE *fp;
2979         size_t len;
2980
2981         cm_return_val_if_fail(str != NULL, -1);
2982         cm_return_val_if_fail(file != NULL, -1);
2983
2984         if ((fp = claws_fopen(file, "wb")) == NULL) {
2985                 FILE_OP_ERROR(file, "claws_fopen");
2986                 return -1;
2987         }
2988
2989         len = strlen(str);
2990         if (len == 0) {
2991                 claws_fclose(fp);
2992                 return 0;
2993         }
2994
2995         if (claws_fwrite(str, 1, len, fp) != len) {
2996                 FILE_OP_ERROR(file, "claws_fwrite");
2997                 claws_fclose(fp);
2998                 claws_unlink(file);
2999                 return -1;
3000         }
3001
3002         if (claws_safe_fclose(fp) == EOF) {
3003                 FILE_OP_ERROR(file, "claws_fclose");
3004                 claws_unlink(file);
3005                 return -1;
3006         }
3007
3008         return 0;
3009 }
3010
3011 static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
3012 {
3013         GByteArray *array;
3014         guchar buf[BUFSIZ];
3015         gint n_read;
3016         gchar *str;
3017
3018         cm_return_val_if_fail(fp != NULL, NULL);
3019
3020         array = g_byte_array_new();
3021
3022         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
3023                 if (n_read < sizeof(buf) && claws_ferror(fp))
3024                         break;
3025                 g_byte_array_append(array, buf, n_read);
3026         }
3027
3028         if (claws_ferror(fp)) {
3029                 FILE_OP_ERROR("file stream", "claws_fread");
3030                 g_byte_array_free(array, TRUE);
3031                 return NULL;
3032         }
3033
3034         buf[0] = '\0';
3035         g_byte_array_append(array, buf, 1);
3036         str = (gchar *)array->data;
3037         g_byte_array_free(array, FALSE);
3038
3039         if (recode && !g_utf8_validate(str, -1, NULL)) {
3040                 const gchar *src_codeset, *dest_codeset;
3041                 gchar *tmp = NULL;
3042                 src_codeset = conv_get_locale_charset_str();
3043                 dest_codeset = CS_UTF_8;
3044                 tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
3045                 g_free(str);
3046                 str = tmp;
3047         }
3048
3049         return str;
3050 }
3051
3052 static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
3053 {
3054         FILE *fp;
3055         gchar *str;
3056         GStatBuf s;
3057 #ifndef G_OS_WIN32
3058         gint fd, err;
3059         struct timeval timeout = {1, 0};
3060         fd_set fds;
3061         int fflags = 0;
3062 #endif
3063
3064         cm_return_val_if_fail(file != NULL, NULL);
3065
3066         if (g_stat(file, &s) != 0) {
3067                 FILE_OP_ERROR(file, "stat");
3068                 return NULL;
3069         }
3070         if (S_ISDIR(s.st_mode)) {
3071                 g_warning("%s: is a directory", file);
3072                 return NULL;
3073         }
3074
3075 #ifdef G_OS_WIN32
3076         fp = claws_fopen (file, "rb");
3077         if (fp == NULL) {
3078                 FILE_OP_ERROR(file, "open");
3079                 return NULL;
3080         }
3081 #else     
3082         /* test whether the file is readable without blocking */
3083         fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
3084         if (fd == -1) {
3085                 FILE_OP_ERROR(file, "open");
3086                 return NULL;
3087         }
3088
3089         FD_ZERO(&fds);
3090         FD_SET(fd, &fds);
3091
3092         /* allow for one second */
3093         err = select(fd+1, &fds, NULL, NULL, &timeout);
3094         if (err <= 0 || !FD_ISSET(fd, &fds)) {
3095                 if (err < 0) {
3096                         FILE_OP_ERROR(file, "select");
3097                 } else {
3098                         g_warning("%s: doesn't seem readable", file);
3099                 }
3100                 close(fd);
3101                 return NULL;
3102         }
3103         
3104         /* Now clear O_NONBLOCK */
3105         if ((fflags = fcntl(fd, F_GETFL)) < 0) {
3106                 FILE_OP_ERROR(file, "fcntl (F_GETFL)");
3107                 close(fd);
3108                 return NULL;
3109         }
3110         if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
3111                 FILE_OP_ERROR(file, "fcntl (F_SETFL)");
3112                 close(fd);
3113                 return NULL;
3114         }
3115         
3116         /* get the FILE pointer */
3117         fp = claws_fdopen(fd, "rb");
3118
3119         if (fp == NULL) {
3120                 FILE_OP_ERROR(file, "claws_fdopen");
3121                 close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
3122                 return NULL;
3123         }
3124 #endif
3125
3126         str = file_read_stream_to_str_full(fp, recode);
3127
3128         claws_fclose(fp);
3129
3130         return str;
3131 }
3132
3133 gchar *file_read_to_str(const gchar *file)
3134 {
3135         return file_read_to_str_full(file, TRUE);
3136 }
3137 gchar *file_read_stream_to_str(FILE *fp)
3138 {
3139         return file_read_stream_to_str_full(fp, TRUE);
3140 }
3141
3142 gchar *file_read_to_str_no_recode(const gchar *file)
3143 {
3144         return file_read_to_str_full(file, FALSE);
3145 }
3146 gchar *file_read_stream_to_str_no_recode(FILE *fp)
3147 {
3148         return file_read_stream_to_str_full(fp, FALSE);
3149 }
3150
3151 char *fgets_crlf(char *buf, int size, FILE *stream)
3152 {
3153         gboolean is_cr = FALSE;
3154         gboolean last_was_cr = FALSE;
3155         int c = 0;
3156         char *cs;
3157
3158         cs = buf;
3159         while (--size > 0 && (c = getc(stream)) != EOF)
3160         {
3161                 *cs++ = c;
3162                 is_cr = (c == '\r');
3163                 if (c == '\n') {
3164                         break;
3165                 }
3166                 if (last_was_cr) {
3167                         *(--cs) = '\n';
3168                         cs++;
3169                         ungetc(c, stream);
3170                         break;
3171                 }
3172                 last_was_cr = is_cr;
3173         }
3174         if (c == EOF && cs == buf)
3175                 return NULL;
3176
3177         *cs = '\0';
3178
3179         return buf;     
3180 }
3181
3182 static gint execute_async(gchar *const argv[], const gchar *working_directory)
3183 {
3184         cm_return_val_if_fail(argv != NULL && argv[0] != NULL, -1);
3185
3186         if (g_spawn_async(working_directory, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH,
3187                           NULL, NULL, NULL, FALSE) == FALSE) {
3188                 g_warning("couldn't execute command: %s", argv[0]);
3189                 return -1;
3190         }
3191
3192         return 0;
3193 }
3194
3195 static gint execute_sync(gchar *const argv[], const gchar *working_directory)
3196 {
3197         gint status;
3198
3199         cm_return_val_if_fail(argv != NULL && argv[0] != NULL, -1);
3200
3201 #ifdef G_OS_UNIX
3202         if (g_spawn_sync(working_directory, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH,
3203                          NULL, NULL, NULL, NULL, &status, NULL) == FALSE) {
3204                 g_warning("couldn't execute command: %s", argv[0]);
3205                 return -1;
3206         }
3207
3208         if (WIFEXITED(status))
3209                 return WEXITSTATUS(status);
3210         else
3211                 return -1;
3212 #else
3213         if (g_spawn_sync(working_directory, (gchar **)argv, NULL,
3214                                 G_SPAWN_SEARCH_PATH|
3215                                 G_SPAWN_CHILD_INHERITS_STDIN|
3216                                 G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
3217                          NULL, NULL, NULL, NULL, &status, NULL) == FALSE) {
3218                 g_warning("couldn't execute command: %s", argv[0]);
3219                 return -1;
3220         }
3221
3222         return status;
3223 #endif
3224 }
3225
3226 gint execute_command_line(const gchar *cmdline, gboolean async,
3227                 const gchar *working_directory)
3228 {
3229         gchar **argv;
3230         gint ret;
3231
3232         debug_print("execute_command_line(): executing: %s\n", cmdline?cmdline:"(null)");
3233
3234         argv = strsplit_with_quote(cmdline, " ", 0);
3235
3236         if (async)
3237                 ret = execute_async(argv, working_directory);
3238         else
3239                 ret = execute_sync(argv, working_directory);
3240
3241         g_strfreev(argv);
3242
3243         return ret;
3244 }
3245
3246 gchar *get_command_output(const gchar *cmdline)
3247 {
3248         gchar *child_stdout;
3249         gint status;
3250
3251         cm_return_val_if_fail(cmdline != NULL, NULL);
3252
3253         debug_print("get_command_output(): executing: %s\n", cmdline);
3254
3255         if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status,
3256                                       NULL) == FALSE) {
3257                 g_warning("couldn't execute command: %s", cmdline);
3258                 return NULL;
3259         }
3260
3261         return child_stdout;
3262 }
3263
3264 static gint is_unchanged_uri_char(char c)
3265 {
3266         switch (c) {
3267                 case '(':
3268                 case ')':
3269                         return 0;
3270                 default:
3271                         return 1;
3272         }
3273 }
3274
3275 static void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
3276 {
3277         int i;
3278         int k;
3279
3280         k = 0;
3281         for(i = 0; i < strlen(uri) ; i++) {
3282                 if (is_unchanged_uri_char(uri[i])) {
3283                         if (k + 2 >= bufsize)
3284                                 break;
3285                         encoded_uri[k++] = uri[i];
3286                 }
3287                 else {
3288                         char * hexa = "0123456789ABCDEF";
3289
3290                         if (k + 4 >= bufsize)
3291                                 break;
3292                         encoded_uri[k++] = '%';
3293                         encoded_uri[k++] = hexa[uri[i] / 16];
3294                         encoded_uri[k++] = hexa[uri[i] % 16];
3295                 }
3296         }
3297         encoded_uri[k] = 0;
3298 }
3299
3300 gint open_uri(const gchar *uri, const gchar *cmdline)
3301 {
3302
3303 #ifndef G_OS_WIN32
3304         gchar buf[BUFFSIZE];
3305         gchar *p;
3306         gchar encoded_uri[BUFFSIZE];
3307         cm_return_val_if_fail(uri != NULL, -1);
3308
3309         /* an option to choose whether to use encode_uri or not ? */
3310         encode_uri(encoded_uri, BUFFSIZE, uri);
3311
3312         if (cmdline &&
3313             (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
3314             !strchr(p + 2, '%'))
3315                 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
3316         else {
3317                 if (cmdline)
3318                         g_warning("Open URI command-line is invalid "
3319                                   "(there must be only one '%%s'): %s",
3320                                   cmdline);
3321                 g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
3322         }
3323
3324         execute_command_line(buf, TRUE, NULL);
3325 #else
3326         ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOW);
3327 #endif
3328         return 0;
3329 }
3330
3331 gint open_txt_editor(const gchar *filepath, const gchar *cmdline)
3332 {
3333         gchar buf[BUFFSIZE];
3334         gchar *p;
3335
3336         cm_return_val_if_fail(filepath != NULL, -1);
3337
3338         if (cmdline &&
3339             (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
3340             !strchr(p + 2, '%'))
3341                 g_snprintf(buf, sizeof(buf), cmdline, filepath);
3342         else {
3343                 if (cmdline)
3344                         g_warning("Open Text Editor command-line is invalid "
3345                                   "(there must be only one '%%s'): %s",
3346                                   cmdline);
3347                 g_snprintf(buf, sizeof(buf), DEFAULT_EDITOR_CMD, filepath);
3348         }
3349
3350         execute_command_line(buf, TRUE, NULL);
3351
3352         return 0;
3353 }
3354
3355 time_t remote_tzoffset_sec(const gchar *zone)
3356 {
3357         static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
3358         gchar zone3[4];
3359         gchar *p;
3360         gchar c;
3361         gint iustz;
3362         gint offset;
3363         time_t remoteoffset;
3364
3365         strncpy(zone3, zone, 3);
3366         zone3[3] = '\0';
3367         remoteoffset = 0;
3368
3369         if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
3370             (c == '+' || c == '-')) {
3371                 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
3372                 if (c == '-')
3373                         remoteoffset = -remoteoffset;
3374         } else if (!strncmp(zone, "UT" , 2) ||
3375                    !strncmp(zone, "GMT", 3)) {
3376                 remoteoffset = 0;
3377         } else if (strlen(zone3) == 3) {
3378                 for (p = ustzstr; *p != '\0'; p += 3) {
3379                         if (!g_ascii_strncasecmp(p, zone3, 3)) {
3380                                 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
3381                                 remoteoffset = iustz * 3600;
3382                                 break;
3383                         }
3384                 }
3385                 if (*p == '\0')
3386                         return -1;
3387         } else if (strlen(zone3) == 1) {
3388                 switch (zone[0]) {
3389                 case 'Z': remoteoffset =   0; break;
3390                 case 'A': remoteoffset =  -1; break;
3391                 case 'B': remoteoffset =  -2; break;
3392                 case 'C': remoteoffset =  -3; break;
3393                 case 'D': remoteoffset =  -4; break;
3394                 case 'E': remoteoffset =  -5; break;
3395                 case 'F': remoteoffset =  -6; break;
3396                 case 'G': remoteoffset =  -7; break;
3397                 case 'H': remoteoffset =  -8; break;
3398                 case 'I': remoteoffset =  -9; break;
3399                 case 'K': remoteoffset = -10; break; /* J is not used */
3400                 case 'L': remoteoffset = -11; break;
3401                 case 'M': remoteoffset = -12; break;
3402                 case 'N': remoteoffset =   1; break;
3403                 case 'O': remoteoffset =   2; break;
3404                 case 'P': remoteoffset =   3; break;
3405                 case 'Q': remoteoffset =   4; break;
3406                 case 'R': remoteoffset =   5; break;
3407                 case 'S': remoteoffset =   6; break;
3408                 case 'T': remoteoffset =   7; break;
3409                 case 'U': remoteoffset =   8; break;
3410                 case 'V': remoteoffset =   9; break;
3411                 case 'W': remoteoffset =  10; break;
3412                 case 'X': remoteoffset =  11; break;
3413                 case 'Y': remoteoffset =  12; break;
3414                 default:  remoteoffset =   0; break;
3415                 }
3416                 remoteoffset = remoteoffset * 3600;
3417         } else
3418                 return -1;
3419
3420         return remoteoffset;
3421 }
3422
3423 time_t tzoffset_sec(time_t *now)
3424 {
3425         struct tm gmt, *lt;
3426         gint off;
3427         struct tm buf1, buf2;
3428 #ifdef G_OS_WIN32
3429         if (now && *now < 0)
3430                 return 0;
3431 #endif  
3432         gmt = *gmtime_r(now, &buf1);
3433         lt = localtime_r(now, &buf2);
3434
3435         off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
3436
3437         if (lt->tm_year < gmt.tm_year)
3438                 off -= 24 * 60;
3439         else if (lt->tm_year > gmt.tm_year)
3440                 off += 24 * 60;
3441         else if (lt->tm_yday < gmt.tm_yday)
3442                 off -= 24 * 60;
3443         else if (lt->tm_yday > gmt.tm_yday)
3444                 off += 24 * 60;
3445
3446         if (off >= 24 * 60)             /* should be impossible */
3447                 off = 23 * 60 + 59;     /* if not, insert silly value */
3448         if (off <= -24 * 60)
3449                 off = -(23 * 60 + 59);
3450
3451         return off * 60;
3452 }
3453
3454 /* calculate timezone offset */
3455 gchar *tzoffset(time_t *now)
3456 {
3457         static gchar offset_string[6];
3458         struct tm gmt, *lt;
3459         gint off;
3460         gchar sign = '+';
3461         struct tm buf1, buf2;
3462 #ifdef G_OS_WIN32
3463         if (now && *now < 0)
3464                 return 0;
3465 #endif
3466         gmt = *gmtime_r(now, &buf1);
3467         lt = localtime_r(now, &buf2);
3468
3469         off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
3470
3471         if (lt->tm_year < gmt.tm_year)
3472                 off -= 24 * 60;
3473         else if (lt->tm_year > gmt.tm_year)
3474                 off += 24 * 60;
3475         else if (lt->tm_yday < gmt.tm_yday)
3476                 off -= 24 * 60;
3477         else if (lt->tm_yday > gmt.tm_yday)
3478                 off += 24 * 60;
3479
3480         if (off < 0) {
3481                 sign = '-';
3482                 off = -off;
3483         }
3484
3485         if (off >= 24 * 60)             /* should be impossible */
3486                 off = 23 * 60 + 59;     /* if not, insert silly value */
3487
3488         sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
3489
3490         return offset_string;
3491 }
3492
3493 static void _get_rfc822_date(gchar *buf, gint len, gboolean hidetz)
3494 {
3495         struct tm *lt;
3496         time_t t;
3497         gchar day[4], mon[4];
3498         gint dd, hh, mm, ss, yyyy;
3499         struct tm buf1;
3500         gchar buf2[RFC822_DATE_BUFFSIZE];
3501
3502         t = time(NULL);
3503         if (hidetz)
3504                 lt = gmtime_r(&t, &buf1);
3505         else
3506                 lt = localtime_r(&t, &buf1);
3507
3508         if (sscanf(asctime_r(lt, buf2), "%3s %3s %d %d:%d:%d %d\n",
3509                day, mon, &dd, &hh, &mm, &ss, &yyyy) != 7)
3510                 g_warning("failed reading date/time");
3511
3512         g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
3513                    day, dd, mon, yyyy, hh, mm, ss, (hidetz? "-0000": tzoffset(&t)));
3514 }
3515
3516 void get_rfc822_date(gchar *buf, gint len)
3517 {
3518         _get_rfc822_date(buf, len, FALSE);
3519 }
3520
3521 void get_rfc822_date_hide_tz(gchar *buf, gint len)
3522 {
3523         _get_rfc822_date(buf, len, TRUE);
3524 }
3525
3526 void debug_set_mode(gboolean mode)
3527 {
3528         debug_mode = mode;
3529 }
3530
3531 gboolean debug_get_mode(void)
3532 {
3533         return debug_mode;
3534 }
3535
3536 void debug_print_real(const gchar *format, ...)
3537 {
3538         va_list args;
3539         gchar buf[BUFFSIZE];
3540
3541         if (!debug_mode) return;
3542
3543         va_start(args, format);
3544         g_vsnprintf(buf, sizeof(buf), format, args);
3545         va_end(args);
3546
3547         g_print("%s", buf);
3548 }
3549
3550
3551 const char * debug_srcname(const char *file)
3552 {
3553         const char *s = strrchr (file, '/');
3554         return s? s+1:file;
3555 }
3556
3557
3558 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
3559 {
3560         if (subject == NULL)
3561                 subject = "";
3562         else
3563                 subject += subject_get_prefix_length(subject);
3564
3565         return g_hash_table_lookup(subject_table, subject);
3566 }
3567
3568 void subject_table_insert(GHashTable *subject_table, gchar * subject,
3569                           void * data)
3570 {
3571         if (subject == NULL || *subject == 0)
3572                 return;
3573         subject += subject_get_prefix_length(subject);
3574         g_hash_table_insert(subject_table, subject, data);
3575 }
3576
3577 void subject_table_remove(GHashTable *subject_table, gchar * subject)
3578 {
3579         if (subject == NULL)
3580                 return;
3581
3582         subject += subject_get_prefix_length(subject);
3583         g_hash_table_remove(subject_table, subject);
3584 }
3585
3586 static regex_t u_regex;
3587 static gboolean u_init_;
3588
3589 void utils_free_regex(void)
3590 {
3591         if (u_init_) {
3592                 regfree(&u_regex);
3593                 u_init_ = FALSE;
3594         }
3595 }
3596
3597 /*!
3598  *\brief        Check if a string is prefixed with known (combinations)
3599  *              of prefixes. The function assumes that each prefix
3600  *              is terminated by zero or exactly _one_ space.
3601  *
3602  *\param        str String to check for a prefixes
3603  *
3604  *\return       int Number of chars in the prefix that should be skipped
3605  *              for a "clean" subject line. If no prefix was found, 0
3606  *              is returned.
3607  */
3608 int subject_get_prefix_length(const gchar *subject)
3609 {
3610         /*!< Array with allowable reply prefixes regexps. */
3611         static const gchar * const prefixes[] = {
3612                 "Re\\:",                        /* "Re:" */
3613                 "Re\\[[1-9][0-9]*\\]\\:",       /* "Re[XXX]:" (non-conforming news mail clients) */
3614                 "Antw\\:",                      /* "Antw:" (Dutch / German Outlook) */
3615                 "Aw\\:",                        /* "Aw:"   (German) */
3616                 "Antwort\\:",                   /* "Antwort:" (German Lotus Notes) */
3617                 "Res\\:",                       /* "Res:" (Spanish/Brazilian Outlook) */
3618                 "Fw\\:",                        /* "Fw:" Forward */
3619                 "Fwd\\:",                       /* "Fwd:" Forward */
3620                 "Enc\\:",                       /* "Enc:" Forward (Brazilian Outlook) */
3621                 "Odp\\:",                       /* "Odp:" Re (Polish Outlook) */
3622                 "Rif\\:",                       /* "Rif:" (Italian Outlook) */
3623                 "Sv\\:",                        /* "Sv" (Norwegian) */
3624                 "Vs\\:",                        /* "Vs" (Norwegian) */
3625                 "Ad\\:",                        /* "Ad" (Norwegian) */
3626                 "\347\255\224\345\244\215\\:",  /* "Re" (Chinese, UTF-8) */
3627                 "R\303\251f\\. \\:",            /* "R�f. :" (French Lotus Notes) */
3628                 "Re \\:",                       /* "Re :" (French Yahoo Mail) */
3629                 /* add more */
3630         };
3631         const int PREFIXES = sizeof prefixes / sizeof prefixes[0];
3632         int n;
3633         regmatch_t pos;
3634
3635         if (!subject) return 0;
3636         if (!*subject) return 0;
3637
3638         if (!u_init_) {
3639                 GString *s = g_string_new("");
3640
3641                 for (n = 0; n < PREFIXES; n++)
3642                         /* Terminate each prefix regexpression by a
3643                          * "\ ?" (zero or ONE space), and OR them */
3644                         g_string_append_printf(s, "(%s\\ ?)%s",
3645                                           prefixes[n],
3646                                           n < PREFIXES - 1 ?
3647                                           "|" : "");
3648
3649                 g_string_prepend(s, "(");
3650                 g_string_append(s, ")+");       /* match at least once */
3651                 g_string_prepend(s, "^\\ *");   /* from beginning of line */
3652
3653
3654                 /* We now have something like "^\ *((PREFIX1\ ?)|(PREFIX2\ ?))+"
3655                  * TODO: Should this be       "^\ *(((PREFIX1)|(PREFIX2))\ ?)+" ??? */
3656                 if (regcomp(&u_regex, s->str, REG_EXTENDED | REG_ICASE)) {
3657                         debug_print("Error compiling regexp %s\n", s->str);
3658                         g_string_free(s, TRUE);
3659                         return 0;
3660                 } else {
3661                         u_init_ = TRUE;
3662                         g_string_free(s, TRUE);
3663                 }
3664         }
3665
3666         if (!regexec(&u_regex, subject, 1, &pos, 0) && pos.rm_so != -1)
3667                 return pos.rm_eo;
3668         else
3669                 return 0;
3670 }
3671
3672 static guint g_stricase_hash(gconstpointer gptr)
3673 {
3674         guint hash_result = 0;
3675         const char *str;
3676
3677         for (str = gptr; str && *str; str++) {
3678                 hash_result += toupper(*str);
3679         }
3680
3681         return hash_result;
3682 }
3683
3684 static gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
3685 {
3686         const char *str1 = gptr1;
3687         const char *str2 = gptr2;
3688
3689         return !strcasecmp(str1, str2);
3690 }
3691
3692 gint g_int_compare(gconstpointer a, gconstpointer b)
3693 {
3694         return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
3695 }
3696
3697 /*
3698    quote_cmd_argument()
3699
3700    return a quoted string safely usable in argument of a command.
3701
3702    code is extracted and adapted from etPan! project -- DINH V. Ho�.
3703 */
3704
3705 gint quote_cmd_argument(gchar * result, guint size,
3706                         const gchar * path)
3707 {
3708         const gchar * p;
3709         gchar * result_p;
3710         guint remaining;
3711
3712         result_p = result;
3713         remaining = size;
3714
3715         for(p = path ; * p != '\0' ; p ++) {
3716
3717                 if (isalnum((guchar)*p) || (* p == '/')) {
3718                         if (remaining > 0) {
3719                                 * result_p = * p;
3720                                 result_p ++;
3721                                 remaining --;
3722                         }
3723                         else {
3724                                 result[size - 1] = '\0';
3725                                 return -1;
3726                         }
3727                 }
3728                 else {
3729                         if (remaining >= 2) {
3730                                 * result_p = '\\';
3731                                 result_p ++;
3732                                 * result_p = * p;
3733                                 result_p ++;
3734                                 remaining -= 2;
3735                         }
3736                         else {
3737                                 result[size - 1] = '\0';
3738                                 return -1;
3739                         }
3740                 }
3741         }
3742         if (remaining > 0) {
3743                 * result_p = '\0';
3744         }
3745         else {
3746                 result[size - 1] = '\0';
3747                 return -1;
3748         }
3749
3750         return 0;
3751 }
3752
3753 typedef struct
3754 {
3755         GNode           *parent;
3756         GNodeMapFunc     func;
3757         gpointer         data;
3758 } GNodeMapData;
3759
3760 static void g_node_map_recursive(GNode *node, gpointer data)
3761 {
3762         GNodeMapData *mapdata = (GNodeMapData *) data;
3763         GNode *newnode;
3764         GNodeMapData newmapdata;
3765         gpointer newdata;
3766
3767         newdata = mapdata->func(node->data, mapdata->data);
3768         if (newdata != NULL) {
3769                 newnode = g_node_new(newdata);
3770                 g_node_append(mapdata->parent, newnode);
3771
3772                 newmapdata.parent = newnode;
3773                 newmapdata.func = mapdata->func;
3774                 newmapdata.data = mapdata->data;
3775
3776                 g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &newmapdata);
3777         }
3778 }
3779
3780 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
3781 {
3782         GNode *root;
3783         GNodeMapData mapdata;
3784
3785         cm_return_val_if_fail(node != NULL, NULL);
3786         cm_return_val_if_fail(func != NULL, NULL);
3787
3788         root = g_node_new(func(node->data, data));
3789
3790         mapdata.parent = root;
3791         mapdata.func = func;
3792         mapdata.data = data;
3793
3794         g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &mapdata);
3795
3796         return root;
3797 }
3798
3799 #define HEX_TO_INT(val, hex)                    \
3800 {                                               \
3801         gchar c = hex;                          \
3802                                                 \
3803         if ('0' <= c && c <= '9') {             \
3804                 val = c - '0';                  \
3805         } else if ('a' <= c && c <= 'f') {      \
3806                 val = c - 'a' + 10;             \
3807         } else if ('A' <= c && c <= 'F') {      \
3808                 val = c - 'A' + 10;             \
3809         } else {                                \
3810                 val = -1;                       \
3811         }                                       \
3812 }
3813
3814 gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
3815 {
3816         gint hi, lo;
3817
3818         HEX_TO_INT(hi, c1);
3819         HEX_TO_INT(lo, c2);
3820
3821         if (hi == -1 || lo == -1)
3822                 return FALSE;
3823
3824         *out = (hi << 4) + lo;
3825         return TRUE;
3826 }
3827
3828 #define INT_TO_HEX(hex, val)            \
3829 {                                       \
3830         if ((val) < 10)                 \
3831                 hex = '0' + (val);      \
3832         else                            \
3833                 hex = 'A' + (val) - 10; \
3834 }
3835
3836 void get_hex_str(gchar *out, guchar ch)
3837 {
3838         gchar hex;
3839
3840         INT_TO_HEX(hex, ch >> 4);
3841         *out++ = hex;
3842         INT_TO_HEX(hex, ch & 0x0f);
3843         *out   = hex;
3844 }
3845
3846 #undef REF_DEBUG
3847 #ifndef REF_DEBUG
3848 #define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
3849 #else
3850 #define G_PRINT_REF g_print
3851 #endif
3852
3853 /*!
3854  *\brief        Register ref counted pointer. It is based on GBoxed, so should
3855  *              work with anything that uses the GType system. The semantics
3856  *              are similar to a C++ auto pointer, with the exception that
3857  *              C doesn't have automatic closure (calling destructors) when
3858  *              exiting a block scope.
3859  *              Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
3860  *              function directly.
3861  *
3862  *\return       GType A GType type.
3863  */
3864 GType g_auto_pointer_register(void)
3865 {
3866         static GType auto_pointer_type;
3867         if (!auto_pointer_type)
3868                 auto_pointer_type =
3869                         g_boxed_type_register_static
3870                                 ("G_TYPE_AUTO_POINTER",
3871                                  (GBoxedCopyFunc) g_auto_pointer_copy,
3872                                  (GBoxedFreeFunc) g_auto_pointer_free);
3873         return auto_pointer_type;
3874 }
3875
3876 /*!
3877  *\brief        Structure with g_new() allocated pointer guarded by the
3878  *              auto pointer
3879  */
3880 typedef struct AutoPointerRef {
3881         void          (*free) (gpointer);
3882         gpointer        pointer;
3883         glong           cnt;
3884 } AutoPointerRef;
3885
3886 /*!
3887  *\brief        The auto pointer opaque structure that references the
3888  *              pointer guard block.
3889  */
3890 typedef struct AutoPointer {
3891         AutoPointerRef *ref;
3892         gpointer        ptr; /*!< access to protected pointer */
3893 } AutoPointer;
3894
3895 /*!
3896  *\brief        Creates an auto pointer for a g_new()ed pointer. Example:
3897  *
3898  *\code
3899  *
3900  *              ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
3901  *              ... when assigning, copying and freeing storage elements
3902  *
3903  *              gtk_list_store_new(N_S_COLUMNS,
3904  *                                 G_TYPE_AUTO_POINTER,
3905  *                                 -1);
3906  *
3907  *
3908  *              Template *precious_data = g_new0(Template, 1);
3909  *              g_pointer protect = g_auto_pointer_new(precious_data);
3910  *
3911  *              gtk_list_store_set(container, &iter,
3912  *                                 S_DATA, protect,
3913  *                                 -1);
3914  *
3915  *              ... the gtk_list_store has copied the pointer and
3916  *              ... incremented its reference count, we should free
3917  *              ... the auto pointer (in C++ a destructor would do
3918  *              ... this for us when leaving block scope)
3919  *
3920  *              g_auto_pointer_free(protect);
3921  *
3922  *              ... gtk_list_store_set() now manages the data. When
3923  *              ... *explicitly* requesting a pointer from the list
3924  *              ... store, don't forget you get a copy that should be
3925  *              ... freed with g_auto_pointer_free() eventually.
3926  *
3927  *\endcode
3928  *
3929  *\param        pointer Pointer to be guarded.
3930  *
3931  *\return       GAuto * Pointer that should be used in containers with
3932  *              GType support.
3933  */
3934 GAuto *g_auto_pointer_new(gpointer p)
3935 {
3936         AutoPointerRef *ref;
3937         AutoPointer    *ptr;
3938
3939         if (p == NULL)
3940                 return NULL;
3941
3942         ref = g_new0(AutoPointerRef, 1);
3943         ptr = g_new0(AutoPointer, 1);
3944
3945         ref->pointer = p;
3946         ref->free = g_free;
3947         ref->cnt = 1;
3948
3949         ptr->ref = ref;
3950         ptr->ptr = p;
3951
3952 #ifdef REF_DEBUG
3953         G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
3954 #endif
3955         return ptr;
3956 }
3957
3958 /*!
3959  *\brief        Allocate an autopointer using the passed \a free function to
3960  *              free the guarded pointer
3961  */
3962 GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
3963 {
3964         AutoPointer *aptr;
3965
3966         if (p == NULL)
3967                 return NULL;
3968
3969         aptr = g_auto_pointer_new(p);
3970         aptr->ref->free = free_;
3971         return aptr;
3972 }
3973
3974 gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
3975 {
3976         if (auto_ptr == NULL)
3977                 return NULL;
3978         return ((AutoPointer *) auto_ptr)->ptr;
3979 }
3980
3981 /*!
3982  *\brief        Copies an auto pointer by. It's mostly not necessary
3983  *              to call this function directly, unless you copy/assign
3984  *              the guarded pointer.
3985  *
3986  *\param        auto_ptr Auto pointer returned by previous call to
3987  *              g_auto_pointer_new_XXX()
3988  *
3989  *\return       gpointer An auto pointer
3990  */
3991 GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
3992 {
3993         AutoPointer     *ptr;
3994         AutoPointerRef  *ref;
3995         AutoPointer     *newp;
3996
3997         if (auto_ptr == NULL)
3998                 return NULL;
3999
4000         ptr = auto_ptr;
4001         ref = ptr->ref;
4002         newp = g_new0(AutoPointer, 1);
4003
4004         newp->ref = ref;
4005         newp->ptr = ref->pointer;
4006         ++(ref->cnt);
4007
4008 #ifdef REF_DEBUG
4009         G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
4010 #endif
4011         return newp;
4012 }
4013
4014 /*!
4015  *\brief        Free an auto pointer
4016  */
4017 void g_auto_pointer_free(GAuto *auto_ptr)
4018 {
4019         AutoPointer     *ptr;
4020         AutoPointerRef  *ref;
4021
4022         if (auto_ptr == NULL)
4023                 return;
4024
4025         ptr = auto_ptr;
4026         ref = ptr->ref;
4027
4028         if (--(ref->cnt) == 0) {
4029 #ifdef REF_DEBUG
4030                 G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
4031 #endif
4032                 ref->free(ref->pointer);
4033                 g_free(ref);
4034         }
4035 #ifdef REF_DEBUG
4036         else
4037                 G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
4038 #endif
4039         g_free(ptr);
4040 }
4041
4042 void replace_returns(gchar *str)
4043 {
4044         if (!str)
4045                 return;
4046
4047         while (strstr(str, "\n")) {
4048                 *strstr(str, "\n") = ' ';
4049         }
4050         while (strstr(str, "\r")) {
4051                 *strstr(str, "\r") = ' ';
4052         }
4053 }
4054
4055 /* get_uri_part() - retrieves a URI starting from scanpos.
4056                     Returns TRUE if successful */
4057 gboolean get_uri_part(const gchar *start, const gchar *scanpos,
4058                              const gchar **bp, const gchar **ep, gboolean hdr)
4059 {
4060         const gchar *ep_;
4061         gint parenthese_cnt = 0;
4062
4063         cm_return_val_if_fail(start != NULL, FALSE);
4064         cm_return_val_if_fail(scanpos != NULL, FALSE);
4065         cm_return_val_if_fail(bp != NULL, FALSE);
4066         cm_return_val_if_fail(ep != NULL, FALSE);
4067
4068         *bp = scanpos;
4069
4070         /* find end point of URI */
4071         for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
4072                 if (!g_ascii_isgraph(*(const guchar *)ep_) ||
4073                     !IS_ASCII(*(const guchar *)ep_) ||
4074                     strchr("[]{}<>\"", *ep_)) {
4075                         break;
4076                 } else if (strchr("(", *ep_)) {
4077                         parenthese_cnt++;
4078                 } else if (strchr(")", *ep_)) {
4079                         if (parenthese_cnt > 0)
4080                                 parenthese_cnt--;
4081                         else
4082                                 break;
4083                 }
4084         }
4085
4086         /* no punctuation at end of string */
4087
4088         /* FIXME: this stripping of trailing punctuations may bite with other URIs.
4089          * should pass some URI type to this function and decide on that whether
4090          * to perform punctuation stripping */
4091
4092 #define IS_REAL_PUNCT(ch)       (g_ascii_ispunct(ch) && !strchr("/?=-_~)", ch))
4093
4094         for (; ep_ - 1 > scanpos + 1 &&
4095                IS_REAL_PUNCT(*(ep_ - 1));
4096              ep_--)
4097                 ;
4098
4099 #undef IS_REAL_PUNCT
4100
4101         *ep = ep_;
4102
4103         return TRUE;
4104 }
4105
4106 gchar *make_uri_string(const gchar *bp, const gchar *ep)
4107 {
4108         while (bp && *bp && g_ascii_isspace(*bp))
4109                 bp++;
4110         return g_strndup(bp, ep - bp);
4111 }
4112
4113 /* valid mail address characters */
4114 #define IS_RFC822_CHAR(ch) \
4115         (IS_ASCII(ch) && \
4116          (ch) > 32   && \
4117          (ch) != 127 && \
4118          !g_ascii_isspace(ch) && \
4119          !strchr("(),;<>\"", (ch)))
4120
4121 /* alphabet and number within 7bit ASCII */
4122 #define IS_ASCII_ALNUM(ch)      (IS_ASCII(ch) && g_ascii_isalnum(ch))
4123 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
4124
4125 static GHashTable *create_domain_tab(void)
4126 {
4127         gint n;
4128         GHashTable *htab = g_hash_table_new(g_stricase_hash, g_stricase_equal);
4129
4130         cm_return_val_if_fail(htab, NULL);
4131         for (n = 0; n < sizeof toplvl_domains / sizeof toplvl_domains[0]; n++)
4132                 g_hash_table_insert(htab, (gpointer) toplvl_domains[n], (gpointer) toplvl_domains[n]);
4133         return htab;
4134 }
4135
4136 static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
4137 {
4138         const gint MAX_LVL_DOM_NAME_LEN = 6;
4139         gchar buf[MAX_LVL_DOM_NAME_LEN + 1];
4140         const gchar *m = buf + MAX_LVL_DOM_NAME_LEN + 1;
4141         register gchar *p;
4142
4143         if (last - first > MAX_LVL_DOM_NAME_LEN || first > last)
4144                 return FALSE;
4145
4146         for (p = buf; p < m &&  first < last; *p++ = *first++)
4147                 ;
4148         *p = 0;
4149
4150         return g_hash_table_lookup(tab, buf) != NULL;
4151 }
4152
4153 /* get_email_part() - retrieves an email address. Returns TRUE if successful */
4154 gboolean get_email_part(const gchar *start, const gchar *scanpos,
4155                                const gchar **bp, const gchar **ep, gboolean hdr)
4156 {
4157         /* more complex than the uri part because we need to scan back and forward starting from
4158          * the scan position. */
4159         gboolean result = FALSE;
4160         const gchar *bp_ = NULL;
4161         const gchar *ep_ = NULL;
4162         static GHashTable *dom_tab;
4163         const gchar *last_dot = NULL;
4164         const gchar *prelast_dot = NULL;
4165         const gchar *last_tld_char = NULL;
4166
4167         /* the informative part of the email address (describing the name
4168          * of the email address owner) may contain quoted parts. the
4169          * closure stack stores the last encountered quotes. */
4170         gchar closure_stack[128];
4171         gchar *ptr = closure_stack;
4172
4173         cm_return_val_if_fail(start != NULL, FALSE);
4174         cm_return_val_if_fail(scanpos != NULL, FALSE);
4175         cm_return_val_if_fail(bp != NULL, FALSE);
4176         cm_return_val_if_fail(ep != NULL, FALSE);
4177
4178         if (hdr) {
4179                 const gchar *start_quote = NULL;
4180                 const gchar *end_quote = NULL;
4181 search_again:
4182                 /* go to the real start */
4183                 if (start[0] == ',')
4184                         start++;
4185                 if (start[0] == ';')
4186                         start++;
4187                 while (start[0] == '\n' || start[0] == '\r')
4188                         start++;
4189                 while (start[0] == ' ' || start[0] == '\t')
4190                         start++;
4191
4192                 *bp = start;
4193                 
4194                 /* check if there are quotes (to skip , in them) */
4195                 if (*start == '"') {
4196                         start_quote = start;
4197                         start++;
4198                         end_quote = strstr(start, "\"");
4199                 } else {
4200                         start_quote = NULL;
4201                         end_quote = NULL;
4202                 }
4203                 
4204                 /* skip anything between quotes */
4205                 if (start_quote && end_quote) {
4206                         start = end_quote;
4207                         
4208                 } 
4209
4210                 /* find end (either , or ; or end of line) */
4211                 if (strstr(start, ",") && strstr(start, ";"))
4212                         *ep = strstr(start,",") < strstr(start, ";")
4213                                 ? strstr(start, ",") : strstr(start, ";");
4214                 else if (strstr(start, ","))
4215                         *ep = strstr(start, ",");
4216                 else if (strstr(start, ";"))
4217                         *ep = strstr(start, ";");
4218                 else
4219                         *ep = start+strlen(start);
4220
4221                 /* go back to real start */
4222                 if (start_quote && end_quote) {
4223                         start = start_quote;
4224                 }
4225
4226                 /* check there's still an @ in that, or search
4227                  * further if possible */
4228                 if (strstr(start, "@") && strstr(start, "@") < *ep)
4229                         return TRUE;
4230                 else if (*ep < start+strlen(start)) {
4231                         start = *ep;
4232                         goto search_again;
4233                 } else if (start_quote && strstr(start, "\"") && strstr(start, "\"") < *ep) {
4234                         *bp = start_quote;
4235                         return TRUE;
4236                 } else
4237                         return FALSE;
4238         }
4239
4240         if (!dom_tab)
4241                 dom_tab = create_domain_tab();
4242         cm_return_val_if_fail(dom_tab, FALSE);
4243
4244         /* scan start of address */
4245         for (bp_ = scanpos - 1;
4246              bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
4247                 ;
4248
4249         /* TODO: should start with an alnum? */
4250         bp_++;
4251         for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
4252                 ;
4253
4254         if (bp_ != scanpos) {
4255                 /* scan end of address */
4256                 for (ep_ = scanpos + 1;
4257                      *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
4258                         if (*ep_ == '.') {
4259                                 prelast_dot = last_dot;
4260                                 last_dot = ep_;
4261                                 if (*(last_dot + 1) == '.') {
4262                                         if (prelast_dot == NULL)
4263                                                 return FALSE;
4264                                         last_dot = prelast_dot;
4265                                         break;
4266                                 }
4267                         }
4268
4269                 /* TODO: really should terminate with an alnum? */
4270                 for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
4271                      --ep_)
4272                         ;
4273                 ep_++;
4274
4275                 if (last_dot == NULL)
4276                         return FALSE;
4277                 if (last_dot >= ep_)
4278                         last_dot = prelast_dot;
4279                 if (last_dot == NULL || (scanpos + 1 >= last_dot))
4280                         return FALSE;
4281                 last_dot++;
4282
4283                 for (last_tld_char = last_dot; last_tld_char < ep_; last_tld_char++)
4284                         if (*last_tld_char == '?')
4285                                 break;
4286
4287                 if (is_toplvl_domain(dom_tab, last_dot, last_tld_char))
4288                         result = TRUE;
4289
4290                 *ep = ep_;
4291                 *bp = bp_;
4292         }
4293
4294         if (!result) return FALSE;
4295
4296         if (*ep_ && bp_ != start && *(bp_ - 1) == '"' && *(ep_) == '"'
4297         && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
4298         && IS_RFC822_CHAR(*(ep_ + 3))) {
4299                 /* this informative part with an @ in it is
4300                  * followed by the email address */
4301                 ep_ += 3;
4302
4303                 /* go to matching '>' (or next non-rfc822 char, like \n) */
4304                 for (; *ep_ != '>' && *ep_ != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
4305                         ;
4306
4307                 /* include the bracket */
4308                 if (*ep_ == '>') ep_++;
4309
4310                 /* include the leading quote */
4311                 bp_--;
4312
4313                 *ep = ep_;
4314                 *bp = bp_;
4315                 return TRUE;
4316         }
4317
4318         /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
4319         if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
4320                 return FALSE;
4321
4322         /* see if this is <bracketed>; in this case we also scan for the informative part. */
4323         if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
4324                 return TRUE;
4325
4326 #define FULL_STACK()    ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
4327 #define IN_STACK()      (ptr > closure_stack)
4328 /* has underrun check */
4329 #define POP_STACK()     if(IN_STACK()) --ptr
4330 /* has overrun check */
4331 #define PUSH_STACK(c)   if(!FULL_STACK()) *ptr++ = (c); else return TRUE
4332 /* has underrun check */
4333 #define PEEK_STACK()    (IN_STACK() ? *(ptr - 1) : 0)
4334
4335         ep_++;
4336
4337         /* scan for the informative part. */
4338         for (bp_ -= 2; bp_ >= start; bp_--) {
4339                 /* if closure on the stack keep scanning */
4340                 if (PEEK_STACK() == *bp_) {
4341                         POP_STACK();
4342                         continue;
4343                 }
4344                 if (!IN_STACK() && (*bp_ == '\'' || *bp_ == '"')) {
4345                         PUSH_STACK(*bp_);
4346                         continue;
4347                 }
4348
4349                 /* if nothing in the closure stack, do the special conditions
4350                  * the following if..else expression simply checks whether
4351                  * a token is acceptable. if not acceptable, the clause
4352                  * should terminate the loop with a 'break' */
4353                 if (!PEEK_STACK()) {
4354                         if (*bp_ == '-'
4355                         && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
4356                         && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
4357                                 /* hyphens are allowed, but only in
4358                                    between alnums */
4359                         } else if (strchr(" \"'", *bp_)) {
4360                                 /* but anything not being a punctiation
4361                                    is ok */
4362                         } else {
4363                                 break; /* anything else is rejected */
4364                         }
4365                 }
4366         }
4367
4368         bp_++;
4369
4370         /* scan forward (should start with an alnum) */
4371         for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
4372                 ;
4373 #undef PEEK_STACK
4374 #undef PUSH_STACK
4375 #undef POP_STACK
4376 #undef IN_STACK
4377 #undef FULL_STACK
4378
4379
4380         *bp = bp_;
4381         *ep = ep_;
4382
4383         return result;
4384 }
4385
4386 #undef IS_QUOTE
4387 #undef IS_ASCII_ALNUM
4388 #undef IS_RFC822_CHAR
4389
4390 gchar *make_email_string(const gchar *bp, const gchar *ep)
4391 {
4392         /* returns a mailto: URI; mailto: is also used to detect the
4393          * uri type later on in the button_pressed signal handler */
4394         gchar *tmp;
4395         gchar *result;
4396         gchar *colon, *at;
4397
4398         tmp = g_strndup(bp, ep - bp);
4399
4400         /* If there is a colon in the username part of the address,
4401          * we're dealing with an URI for some other protocol - do
4402          * not prefix with mailto: in such case. */
4403         colon = strchr(tmp, ':');
4404         at = strchr(tmp, '@');
4405         if (colon != NULL && at != NULL && colon < at) {
4406                 result = tmp;
4407         } else {
4408                 result = g_strconcat("mailto:", tmp, NULL);
4409                 g_free(tmp);
4410         }
4411
4412         return result;
4413 }
4414
4415 gchar *make_http_string(const gchar *bp, const gchar *ep)
4416 {
4417         /* returns an http: URI; */
4418         gchar *tmp;
4419         gchar *result;
4420
4421         while (bp && *bp && g_ascii_isspace(*bp))
4422                 bp++;
4423         tmp = g_strndup(bp, ep - bp);
4424         result = g_strconcat("http://", tmp, NULL);
4425         g_free(tmp);
4426
4427         return result;
4428 }
4429
4430 static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type, const gchar *file_to_open)
4431 {
4432         FILE *fp = claws_fopen(path, "rb");
4433         gchar buf[BUFFSIZE];
4434         gchar *result = NULL;
4435         if (!fp)
4436                 return NULL;
4437         while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
4438                 gchar **parts = g_strsplit(buf, ";", 3);
4439                 gchar *trimmed = parts[0];
4440                 while (trimmed[0] == ' ' || trimmed[0] == '\t')
4441                         trimmed++;
4442                 while (trimmed[strlen(trimmed)-1] == ' ' || trimmed[strlen(trimmed)-1] == '\t')
4443                         trimmed[strlen(trimmed)-1] = '\0';
4444
4445                 if (!strcmp(trimmed, type)) {
4446                         gboolean needsterminal = FALSE;
4447                         if (parts[2] && strstr(parts[2], "needsterminal")) {
4448                                 needsterminal = TRUE;
4449                         }
4450                         if (parts[2] && strstr(parts[2], "test=")) {
4451                                 gchar *orig_testcmd = g_strdup(strstr(parts[2], "test=")+5);
4452                                 gchar *testcmd = orig_testcmd;
4453                                 if (strstr(testcmd,";"))
4454                                         *(strstr(testcmd,";")) = '\0';
4455                                 while (testcmd[0] == ' ' || testcmd[0] == '\t')
4456                                         testcmd++;
4457                                 while (testcmd[strlen(testcmd)-1] == '\n')
4458                                         testcmd[strlen(testcmd)-1] = '\0';
4459                                 while (testcmd[strlen(testcmd)-1] == '\r')
4460                                         testcmd[strlen(testcmd)-1] = '\0';
4461                                 while (testcmd[strlen(testcmd)-1] == ' ' || testcmd[strlen(testcmd)-1] == '\t')
4462                                         testcmd[strlen(testcmd)-1] = '\0';
4463                                         
4464                                 if (strstr(testcmd, "%s")) {
4465                                         gchar *tmp = g_strdup_printf(testcmd, file_to_open);
4466                                         gint res = system(tmp);
4467                                         g_free(tmp);
4468                                         g_free(orig_testcmd);
4469                                         
4470                                         if (res != 0) {
4471                                                 g_strfreev(parts);
4472                                                 continue;
4473                                         }
4474                                 } else {
4475                                         gint res = system(testcmd);
4476                                         g_free(orig_testcmd);
4477                                         
4478                                         if (res != 0) {
4479                                                 g_strfreev(parts);
4480                                                 continue;
4481                                         }
4482                                 }
4483                         }
4484                         
4485                         trimmed = parts[1];
4486                         while (trimmed[0] == ' ' || trimmed[0] == '\t')
4487                                 trimmed++;
4488                         while (trimmed[strlen(trimmed)-1] == '\n')
4489                                 trimmed[strlen(trimmed)-1] = '\0';
4490                         while (trimmed[strlen(trimmed)-1] == '\r')
4491                                 trimmed[strlen(trimmed)-1] = '\0';
4492                         while (trimmed[strlen(trimmed)-1] == ' ' || trimmed[strlen(trimmed)-1] == '\t')
4493                                 trimmed[strlen(trimmed)-1] = '\0';
4494                         result = g_strdup(trimmed);
4495                         g_strfreev(parts);
4496                         claws_fclose(fp);
4497                         if (needsterminal) {
4498                                 gchar *tmp = g_strdup_printf("xterm -e %s", result);
4499                                 g_free(result);
4500                                 result = tmp;
4501                         }
4502                         return result;
4503                 }
4504                 g_strfreev(parts);
4505         }
4506         claws_fclose(fp);
4507         return NULL;
4508 }
4509 gchar *mailcap_get_command_for_type(const gchar *type, const gchar *file_to_open)
4510 {
4511         gchar *result = NULL;
4512         gchar *path = NULL;
4513         if (type == NULL)
4514                 return NULL;
4515         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
4516         result = mailcap_get_command_in_file(path, type, file_to_open);
4517         g_free(path);
4518         if (result)
4519                 return result;
4520         result = mailcap_get_command_in_file("/etc/mailcap", type, file_to_open);
4521         return result;
4522 }
4523
4524 void mailcap_update_default(const gchar *type, const gchar *command)
4525 {
4526         gchar *path = NULL, *outpath = NULL;
4527         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
4528         outpath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap.new", NULL);
4529         FILE *fp = claws_fopen(path, "rb");
4530         FILE *outfp = NULL;
4531         gchar buf[BUFFSIZE];
4532         gboolean err = FALSE;
4533
4534         if (!fp) {
4535                 fp = claws_fopen(path, "a");
4536                 if (!fp) {
4537                         g_warning("failed to create file %s", path);
4538                         g_free(path);
4539                         g_free(outpath);
4540                         return;
4541                 }
4542                 fp = g_freopen(path, "rb", fp);
4543                 if (!fp) {
4544                         g_warning("failed to reopen file %s", path);
4545                         g_free(path);
4546                         g_free(outpath);
4547                         return;
4548                 }
4549         }
4550
4551         outfp = claws_fopen(outpath, "wb");
4552         if (!outfp) {
4553                 g_warning("failed to create file %s", outpath);
4554                 g_free(path);
4555                 g_free(outpath);
4556                 claws_fclose(fp);
4557                 return;
4558         }
4559         while (fp && claws_fgets(buf, sizeof (buf), fp) != NULL) {
4560                 gchar **parts = g_strsplit(buf, ";", 3);
4561                 gchar *trimmed = parts[0];
4562                 while (trimmed[0] == ' ')
4563                         trimmed++;
4564                 while (trimmed[strlen(trimmed)-1] == ' ')
4565                         trimmed[strlen(trimmed)-1] = '\0';
4566
4567                 if (!strcmp(trimmed, type)) {
4568                         g_strfreev(parts);
4569                         continue;
4570                 }
4571                 else {
4572                         if(claws_fputs(buf, outfp) == EOF) {
4573                                 err = TRUE;
4574                                 break;
4575                         }
4576                 }
4577                 g_strfreev(parts);
4578         }
4579         if (fprintf(outfp, "%s; %s\n", type, command) < 0)
4580                 err = TRUE;
4581
4582         if (fp)
4583                 claws_fclose(fp);
4584
4585         if (claws_safe_fclose(outfp) == EOF)
4586                 err = TRUE;
4587                 
4588         if (!err)
4589                 g_rename(outpath, path);
4590
4591         g_free(path);
4592         g_free(outpath);
4593 }
4594
4595 gint copy_dir(const gchar *src, const gchar *dst)
4596 {
4597         GDir *dir;
4598         const gchar *name;
4599
4600         if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
4601                 g_warning("failed to open directory: %s", src);
4602                 return -1;
4603         }
4604
4605         if (make_dir(dst) < 0)
4606                 return -1;
4607
4608         while ((name = g_dir_read_name(dir)) != NULL) {
4609                 gchar *old_file, *new_file;
4610                 old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
4611                 new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
4612                 debug_print("copying: %s -> %s\n", old_file, new_file);
4613                 if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
4614                         gint r = copy_file(old_file, new_file, TRUE);
4615                         if (r < 0) {
4616                                 g_dir_close(dir);
4617                                 return r;
4618                         }
4619                 }
4620 #ifndef G_OS_WIN32
4621                 /* Windows has no symlinks.  Or well, Vista seems to
4622                    have something like this but the semantics might be
4623                    different.  Thus we don't use it under Windows. */
4624                  else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
4625                         GError *error = NULL;
4626                         gint r = 0;
4627                         gchar *target = g_file_read_link(old_file, &error);
4628                         if (target)
4629                                 r = symlink(target, new_file);
4630                         g_free(target);
4631                         if (r < 0) {
4632                                 g_dir_close(dir);
4633                                 return r;
4634                         }
4635                  }
4636 #endif /*G_OS_WIN32*/
4637                 else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
4638                         gint r = copy_dir(old_file, new_file);
4639                         if (r < 0) {
4640                                 g_dir_close(dir);
4641                                 return r;
4642                         }
4643                 }
4644         }
4645         g_dir_close(dir);
4646         return 0;
4647 }
4648
4649 /* crude test to see if a file is an email. */
4650 gboolean file_is_email (const gchar *filename)
4651 {
4652         FILE *fp = NULL;
4653         gchar buffer[2048];
4654         gint i = 0;
4655         gint score = 0;
4656         if (filename == NULL)
4657                 return FALSE;
4658         if ((fp = claws_fopen(filename, "rb")) == NULL)
4659                 return FALSE;
4660         while (i < 60 && score < 3
4661                && claws_fgets(buffer, sizeof (buffer), fp) != NULL) {
4662                 if (!strncmp(buffer, "From:", strlen("From:")))
4663                         score++;
4664                 else if (!strncmp(buffer, "Date:", strlen("Date:")))
4665                         score++;
4666                 else if (!strncmp(buffer, "Message-ID:", strlen("Message-ID:")))
4667                         score++;
4668                 else if (!strncmp(buffer, "Subject:", strlen("Subject:")))
4669                         score++;
4670                 i++;
4671         }
4672         claws_fclose(fp);
4673         return (score >= 3);
4674 }
4675
4676 gboolean sc_g_list_bigger(GList *list, gint max)
4677 {
4678         GList *cur = list;
4679         int i = 0;
4680         while (cur && i <= max+1) {
4681                 i++;
4682                 cur = cur->next;
4683         }
4684         return (i > max);
4685 }
4686
4687 gboolean sc_g_slist_bigger(GSList *list, gint max)
4688 {
4689         GSList *cur = list;
4690         int i = 0;
4691         while (cur && i <= max+1) {
4692                 i++;
4693                 cur = cur->next;
4694         }
4695         return (i > max);
4696 }
4697
4698 const gchar *daynames[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4699 const gchar *monthnames[] = {NULL, NULL, NULL, NULL, NULL, NULL, 
4700                              NULL, NULL, NULL, NULL, NULL, NULL};
4701 const gchar *s_daynames[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4702 const gchar *s_monthnames[] = {NULL, NULL, NULL, NULL, NULL, NULL, 
4703                              NULL, NULL, NULL, NULL, NULL, NULL};
4704
4705 gint daynames_len[] =     {0,0,0,0,0,0,0};
4706 gint monthnames_len[] =   {0,0,0,0,0,0,
4707                                  0,0,0,0,0,0};
4708 gint s_daynames_len[] =   {0,0,0,0,0,0,0};
4709 gint s_monthnames_len[] = {0,0,0,0,0,0,
4710                                  0,0,0,0,0,0};
4711 const gchar *s_am_up = NULL;
4712 const gchar *s_pm_up = NULL;
4713 const gchar *s_am_low = NULL;
4714 const gchar *s_pm_low = NULL;
4715
4716 gint s_am_up_len = 0;
4717 gint s_pm_up_len = 0;
4718 gint s_am_low_len = 0;
4719 gint s_pm_low_len = 0;
4720
4721 static gboolean time_names_init_done = FALSE;
4722
4723 static void init_time_names(void)
4724 {
4725         int i = 0;
4726
4727         daynames[0] = C_("Complete day name for use by strftime", "Sunday");
4728         daynames[1] = C_("Complete day name for use by strftime", "Monday");
4729         daynames[2] = C_("Complete day name for use by strftime", "Tuesday");
4730         daynames[3] = C_("Complete day name for use by strftime", "Wednesday");
4731         daynames[4] = C_("Complete day name for use by strftime", "Thursday");
4732         daynames[5] = C_("Complete day name for use by strftime", "Friday");
4733         daynames[6] = C_("Complete day name for use by strftime", "Saturday");
4734
4735         monthnames[0] = C_("Complete month name for use by strftime", "January");
4736         monthnames[1] = C_("Complete month name for use by strftime", "February");
4737         monthnames[2] = C_("Complete month name for use by strftime", "March");
4738         monthnames[3] = C_("Complete month name for use by strftime", "April");
4739         monthnames[4] = C_("Complete month name for use by strftime", "May");
4740         monthnames[5] = C_("Complete month name for use by strftime", "June");
4741         monthnames[6] = C_("Complete month name for use by strftime", "July");
4742         monthnames[7] = C_("Complete month name for use by strftime", "August");
4743         monthnames[8] = C_("Complete month name for use by strftime", "September");
4744         monthnames[9] = C_("Complete month name for use by strftime", "October");
4745         monthnames[10] = C_("Complete month name for use by strftime", "November");
4746         monthnames[11] = C_("Complete month name for use by strftime", "December");
4747
4748         s_daynames[0] = C_("Abbr. day name for use by strftime", "Sun");
4749         s_daynames[1] = C_("Abbr. day name for use by strftime", "Mon");
4750         s_daynames[2] = C_("Abbr. day name for use by strftime", "Tue");
4751         s_daynames[3] = C_("Abbr. day name for use by strftime", "Wed");
4752         s_daynames[4] = C_("Abbr. day name for use by strftime", "Thu");
4753         s_daynames[5] = C_("Abbr. day name for use by strftime", "Fri");
4754         s_daynames[6] = C_("Abbr. day name for use by strftime", "Sat");
4755         
4756         s_monthnames[0] = C_("Abbr. month name for use by strftime", "Jan");
4757         s_monthnames[1] = C_("Abbr. month name for use by strftime", "Feb");
4758         s_monthnames[2] = C_("Abbr. month name for use by strftime", "Mar");
4759         s_monthnames[3] = C_("Abbr. month name for use by strftime", "Apr");
4760         s_monthnames[4] = C_("Abbr. month name for use by strftime", "May");
4761         s_monthnames[5] = C_("Abbr. month name for use by strftime", "Jun");
4762         s_monthnames[6] = C_("Abbr. month name for use by strftime", "Jul");
4763         s_monthnames[7] = C_("Abbr. month name for use by strftime", "Aug");
4764         s_monthnames[8] = C_("Abbr. month name for use by strftime", "Sep");
4765         s_monthnames[9] = C_("Abbr. month name for use by strftime", "Oct");
4766         s_monthnames[10] = C_("Abbr. month name for use by strftime", "Nov");
4767         s_monthnames[11] = C_("Abbr. month name for use by strftime", "Dec");
4768
4769         for (i = 0; i < 7; i++) {
4770                 daynames_len[i] = strlen(daynames[i]);
4771                 s_daynames_len[i] = strlen(s_daynames[i]);
4772         }
4773         for (i = 0; i < 12; i++) {
4774                 monthnames_len[i] = strlen(monthnames[i]);
4775                 s_monthnames_len[i] = strlen(s_monthnames[i]);
4776         }
4777
4778         s_am_up = C_("For use by strftime (morning)", "AM");
4779         s_pm_up = C_("For use by strftime (afternoon)", "PM");
4780         s_am_low = C_("For use by strftime (morning, lowercase)", "am");
4781         s_pm_low = C_("For use by strftime (afternoon, lowercase)", "pm");
4782         
4783         s_am_up_len = strlen(s_am_up);
4784         s_pm_up_len = strlen(s_pm_up);
4785         s_am_low_len = strlen(s_am_low);
4786         s_pm_low_len = strlen(s_pm_low);
4787
4788         time_names_init_done = TRUE;
4789 }
4790
4791 #define CHECK_SIZE() {                  \
4792         total_done += len;              \
4793         if (total_done >= buflen) {     \
4794                 buf[buflen-1] = '\0';   \
4795                 return 0;               \
4796         }                               \
4797 }
4798
4799 size_t fast_strftime(gchar *buf, gint buflen, const gchar *format, struct tm *lt)
4800 {
4801         gchar *curpos = buf;
4802         gint total_done = 0;
4803         gchar subbuf[64], subfmt[64];
4804         static time_t last_tzset = (time_t)0;
4805         
4806         if (!time_names_init_done)
4807                 init_time_names();
4808         
4809         if (format == NULL || lt == NULL)
4810                 return 0;
4811                 
4812         if (last_tzset != time(NULL)) {
4813                 tzset();
4814                 last_tzset = time(NULL);
4815         }
4816         while(*format) {
4817                 if (*format == '%') {
4818                         gint len = 0, tmp = 0;
4819                         format++;
4820                         switch(*format) {
4821                         case '%':
4822                                 len = 1; CHECK_SIZE();
4823                                 *curpos = '%';
4824                                 break;
4825                         case 'a':
4826                                 len = s_daynames_len[lt->tm_wday]; CHECK_SIZE();
4827                                 strncpy2(curpos, s_daynames[lt->tm_wday], buflen - total_done);
4828                                 break;
4829                         case 'A':
4830                                 len = daynames_len[lt->tm_wday]; CHECK_SIZE();
4831                                 strncpy2(curpos, daynames[lt->tm_wday], buflen - total_done);
4832                                 break;
4833                         case 'b':
4834                         case 'h':
4835                                 len = s_monthnames_len[lt->tm_mon]; CHECK_SIZE();
4836                                 strncpy2(curpos, s_monthnames[lt->tm_mon], buflen - total_done);
4837                                 break;
4838                         case 'B':
4839                                 len = monthnames_len[lt->tm_mon]; CHECK_SIZE();
4840                                 strncpy2(curpos, monthnames[lt->tm_mon], buflen - total_done);
4841                                 break;
4842                         case 'c':
4843                                 strftime(subbuf, 64, "%c", lt);
4844                                 len = strlen(subbuf); CHECK_SIZE();
4845                                 strncpy2(curpos, subbuf, buflen - total_done);
4846                                 break;
4847                         case 'C':
4848                                 total_done += 2; CHECK_SIZE();
4849                                 tmp = (lt->tm_year + 1900)/100;
4850                                 *curpos++ = '0'+(tmp / 10);
4851                                 *curpos++ = '0'+(tmp % 10);
4852                                 break;
4853                         case 'd':
4854                                 total_done += 2; CHECK_SIZE();
4855                                 *curpos++ = '0'+(lt->tm_mday / 10);
4856                                 *curpos++ = '0'+(lt->tm_mday % 10);
4857                                 break;
4858                         case 'D':
4859                                 total_done += 8; CHECK_SIZE();
4860                                 *curpos++ = '0'+((lt->tm_mon+1) / 10);
4861                                 *curpos++ = '0'+((lt->tm_mon+1) % 10);
4862                                 *curpos++ = '/';
4863                                 *curpos++ = '0'+(lt->tm_mday / 10);
4864                                 *curpos++ = '0'+(lt->tm_mday % 10);
4865                                 *curpos++ = '/';
4866                                 tmp = lt->tm_year%100;
4867                                 *curpos++ = '0'+(tmp / 10);
4868                                 *curpos++ = '0'+(tmp % 10);
4869                                 break;
4870                         case 'e':
4871                                 len = 2; CHECK_SIZE();
4872                                 snprintf(curpos, buflen - total_done, "%2d", lt->tm_mday);
4873                                 break;
4874                         case 'F':
4875                                 len = 10; CHECK_SIZE();
4876                                 snprintf(curpos, buflen - total_done, "%4d-%02d-%02d", 
4877                                         lt->tm_year + 1900, lt->tm_mon +1, lt->tm_mday);
4878                                 break;
4879                         case 'H':
4880                                 total_done += 2; CHECK_SIZE();
4881                                 *curpos++ = '0'+(lt->tm_hour / 10);
4882                                 *curpos++ = '0'+(lt->tm_hour % 10);
4883                                 break;
4884                         case 'I':
4885                                 total_done += 2; CHECK_SIZE();
4886                                 tmp = lt->tm_hour;
4887                                 if (tmp > 12)
4888                                         tmp -= 12;
4889                                 else if (tmp == 0)
4890                                         tmp = 12;
4891                                 *curpos++ = '0'+(tmp / 10);
4892                                 *curpos++ = '0'+(tmp % 10);
4893                                 break;
4894                         case 'j':
4895                                 len = 3; CHECK_SIZE();
4896                                 snprintf(curpos, buflen - total_done, "%03d", lt->tm_yday+1);
4897                                 break;
4898                         case 'k':
4899                                 len = 2; CHECK_SIZE();
4900                                 snprintf(curpos, buflen - total_done, "%2d", lt->tm_hour);
4901                                 break;
4902                         case 'l':
4903                                 len = 2; CHECK_SIZE();
4904                                 tmp = lt->tm_hour;
4905                                 if (tmp > 12)
4906                                         tmp -= 12;
4907                                 else if (tmp == 0)
4908                                         tmp = 12;
4909                                 snprintf(curpos, buflen - total_done, "%2d", tmp);
4910                                 break;
4911                         case 'm':
4912                                 total_done += 2; CHECK_SIZE();
4913                                 tmp = lt->tm_mon + 1;
4914                                 *curpos++ = '0'+(tmp / 10);
4915                                 *curpos++ = '0'+(tmp % 10);
4916                                 break;
4917                         case 'M':
4918                                 total_done += 2; CHECK_SIZE();
4919                                 *curpos++ = '0'+(lt->tm_min / 10);
4920                                 *curpos++ = '0'+(lt->tm_min % 10);
4921                                 break;
4922                         case 'n':
4923                                 len = 1; CHECK_SIZE();
4924                                 *curpos = '\n';
4925                                 break;
4926                         case 'p':
4927                                 if (lt->tm_hour >= 12) {
4928                                         len = s_pm_up_len; CHECK_SIZE();
4929                                         snprintf(curpos, buflen-total_done, "%s", s_pm_up);
4930                                 } else {
4931                                         len = s_am_up_len; CHECK_SIZE();
4932                                         snprintf(curpos, buflen-total_done, "%s", s_am_up);
4933                                 }
4934                                 break;
4935                         case 'P':
4936                                 if (lt->tm_hour >= 12) {
4937                                         len = s_pm_low_len; CHECK_SIZE();
4938                                         snprintf(curpos, buflen-total_done, "%s", s_pm_low);
4939                                 } else {
4940                                         len = s_am_low_len; CHECK_SIZE();
4941                                         snprintf(curpos, buflen-total_done, "%s", s_am_low);
4942                                 }
4943                                 break;
4944                         case 'r':
4945                                 strftime(subbuf, 64, "%r", lt);
4946                                 len = strlen(subbuf); CHECK_SIZE();
4947                                 strncpy2(curpos, subbuf, buflen - total_done);
4948                                 break;
4949                         case 'R':
4950                                 total_done += 5; CHECK_SIZE();
4951                                 *curpos++ = '0'+(lt->tm_hour / 10);
4952                                 *curpos++ = '0'+(lt->tm_hour % 10);
4953                                 *curpos++ = ':';
4954                                 *curpos++ = '0'+(lt->tm_min / 10);
4955                                 *curpos++ = '0'+(lt->tm_min % 10);
4956                                 break;
4957                         case 's':
4958                                 snprintf(subbuf, 64, "%lld", (long long)mktime(lt));
4959                                 len = strlen(subbuf); CHECK_SIZE();
4960                                 strncpy2(curpos, subbuf, buflen - total_done);
4961                                 break;
4962                         case 'S':
4963                                 total_done += 2; CHECK_SIZE();
4964                                 *curpos++ = '0'+(lt->tm_sec / 10);
4965                                 *curpos++ = '0'+(lt->tm_sec % 10);
4966                                 break;
4967                         case 't':
4968                                 len = 1; CHECK_SIZE();
4969                                 *curpos = '\t';
4970                                 break;
4971                         case 'T':
4972                                 total_done += 8; CHECK_SIZE();
4973                                 *curpos++ = '0'+(lt->tm_hour / 10);
4974                                 *curpos++ = '0'+(lt->tm_hour % 10);
4975                                 *curpos++ = ':';
4976                                 *curpos++ = '0'+(lt->tm_min / 10);
4977                                 *curpos++ = '0'+(lt->tm_min % 10);
4978                                 *curpos++ = ':';
4979                                 *curpos++ = '0'+(lt->tm_sec / 10);
4980                                 *curpos++ = '0'+(lt->tm_sec % 10);
4981                                 break;
4982                         case 'u':
4983                                 len = 1; CHECK_SIZE();
4984                                 snprintf(curpos, buflen - total_done, "%d", lt->tm_wday == 0 ? 7: lt->tm_wday);
4985                                 break;
4986                         case 'w':
4987                                 len = 1; CHECK_SIZE();
4988                                 snprintf(curpos, buflen - total_done, "%d", lt->tm_wday);
4989                                 break;
4990                         case 'x':
4991                                 strftime(subbuf, 64, "%x", lt);
4992                                 len = strlen(subbuf); CHECK_SIZE();
4993                                 strncpy2(curpos, subbuf, buflen - total_done);
4994                                 break;
4995                         case 'X':
4996                                 strftime(subbuf, 64, "%X", lt);
4997                                 len = strlen(subbuf); CHECK_SIZE();
4998                                 strncpy2(curpos, subbuf, buflen - total_done);
4999                                 break;
5000                         case 'y':
5001                                 total_done += 2; CHECK_SIZE();
5002                                 tmp = lt->tm_year%100;
5003                                 *curpos++ = '0'+(tmp / 10);
5004                                 *curpos++ = '0'+(tmp % 10);
5005                                 break;
5006                         case 'Y':
5007                                 len = 4; CHECK_SIZE();
5008                                 snprintf(curpos, buflen - total_done, "%4d", lt->tm_year + 1900);
5009                                 break;
5010                         case 'G':
5011                         case 'g':
5012                         case 'U':
5013                         case 'V':
5014                         case 'W':
5015                         case 'z':
5016                         case 'Z':
5017                         case '+':
5018                                 /* let these complicated ones be done with the libc */
5019                                 snprintf(subfmt, 64, "%%%c", *format);
5020                                 strftime(subbuf, 64, subfmt, lt);
5021                                 len = strlen(subbuf); CHECK_SIZE();
5022                                 strncpy2(curpos, subbuf, buflen - total_done);
5023                                 break;
5024                         case 'E':
5025                         case 'O':
5026                                 /* let these complicated modifiers be done with the libc */
5027                                 snprintf(subfmt, 64, "%%%c%c", *format, *(format+1));
5028                                 strftime(subbuf, 64, subfmt, lt);
5029                                 len = strlen(subbuf); CHECK_SIZE();
5030                                 strncpy2(curpos, subbuf, buflen - total_done);
5031                                 format++;
5032                                 break;
5033                         default:
5034                                 g_warning("format error (%c)", *format);
5035                                 *curpos = '\0';
5036                                 return total_done;
5037                         }
5038                         curpos += len;
5039                         format++;
5040                 } else {
5041                         int len = 1; CHECK_SIZE();
5042                         *curpos++ = *format++; 
5043                 }
5044         }
5045         *curpos = '\0';
5046         return total_done;
5047 }
5048
5049 gboolean prefs_common_get_use_shred(void);
5050
5051
5052 #ifdef G_OS_WIN32
5053 #define WEXITSTATUS(x) (x)
5054 #endif
5055
5056 int claws_unlink(const gchar *filename) 
5057 {
5058         GStatBuf s;
5059         static int found_shred = -1;
5060         static const gchar *args[4];
5061
5062         if (filename == NULL)
5063                 return 0;
5064
5065         if (prefs_common_get_use_shred()) {
5066                 if (found_shred == -1) {
5067                         /* init */
5068                         args[0] = g_find_program_in_path("shred");
5069                         debug_print("found shred: %s\n", args[0]);
5070                         found_shred = (args[0] != NULL) ? 1:0;
5071                         args[1] = "-f";
5072                         args[3] = NULL;
5073                 }
5074                 if (found_shred == 1) {
5075                         if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
5076                                 if (s.st_nlink == 1) {
5077                                         gint status=0;
5078                                         args[2] = filename;
5079                                         g_spawn_sync(NULL, (gchar **)args, NULL, 0,
5080                                          NULL, NULL, NULL, NULL, &status, NULL);
5081                                         debug_print("%s %s exited with status %d\n",
5082                                                 args[0], filename, WEXITSTATUS(status));
5083                                         if (truncate(filename, 0) < 0)
5084                                                 g_warning("couln't truncate: %s", filename);
5085                                 }
5086                         }
5087                 }
5088         }
5089         return g_unlink(filename);
5090 }
5091
5092 GMutex *cm_mutex_new(void) {
5093 #if GLIB_CHECK_VERSION(2,32,0)
5094         GMutex *m = g_new0(GMutex, 1);
5095         g_mutex_init(m);
5096         return m;
5097 #else
5098         return g_mutex_new();
5099 #endif
5100 }
5101
5102 void cm_mutex_free(GMutex *mutex) {
5103 #if GLIB_CHECK_VERSION(2,32,0)
5104         g_mutex_clear(mutex);
5105         g_free(mutex);
5106 #else
5107         g_mutex_free(mutex);
5108 #endif
5109 }
5110
5111 static gchar *canonical_list_to_file(GSList *list)
5112 {
5113         GString *result = g_string_new(NULL);
5114         GSList *pathlist = g_slist_reverse(g_slist_copy(list));
5115         GSList *cur;
5116         gchar *str;
5117
5118 #ifndef G_OS_WIN32
5119         result = g_string_append(result, G_DIR_SEPARATOR_S);
5120 #else
5121         if (pathlist->data) {
5122                 const gchar *root = (gchar *)pathlist->data;
5123                 if (root[0] != '\0' && g_ascii_isalpha(root[0]) &&
5124                     root[1] == ':') {
5125                         /* drive - don't prepend dir separator */
5126                 } else {
5127                         result = g_string_append(result, G_DIR_SEPARATOR_S);
5128                 }
5129         }
5130 #endif
5131
5132         for (cur = pathlist; cur; cur = cur->next) {
5133                 result = g_string_append(result, (gchar *)cur->data);
5134                 if (cur->next)
5135                         result = g_string_append(result, G_DIR_SEPARATOR_S);
5136         }
5137         g_slist_free(pathlist);
5138
5139         str = result->str;
5140         g_string_free(result, FALSE);
5141
5142         return str;
5143 }
5144
5145 static GSList *cm_split_path(const gchar *filename, int depth)
5146 {
5147         gchar **path_parts;
5148         GSList *canonical_parts = NULL;
5149         GStatBuf st;
5150         int i;
5151 #ifndef G_OS_WIN32
5152         gboolean follow_symlinks = TRUE;
5153 #endif
5154
5155         if (depth > 32) {
5156 #ifndef G_OS_WIN32
5157                 errno = ELOOP;
5158 #else
5159                 errno = EINVAL; /* can't happen, no symlink handling */
5160 #endif
5161                 return NULL;
5162         }
5163
5164         if (!g_path_is_absolute(filename)) {
5165                 errno =EINVAL;
5166                 return NULL;
5167         }
5168
5169         path_parts = g_strsplit(filename, G_DIR_SEPARATOR_S, -1);
5170
5171         for (i = 0; path_parts[i] != NULL; i++) {
5172                 if (!strcmp(path_parts[i], ""))
5173                         continue;
5174                 if (!strcmp(path_parts[i], "."))
5175                         continue;
5176                 else if (!strcmp(path_parts[i], "..")) {
5177                         if (i == 0) {
5178                                 errno =ENOTDIR;
5179                                 return NULL;
5180                         }
5181                         else /* Remove the last inserted element */
5182                                 canonical_parts = 
5183                                         g_slist_delete_link(canonical_parts,
5184                                                             canonical_parts);
5185                 } else {
5186                         gchar *tmp_path;
5187
5188                         canonical_parts = g_slist_prepend(canonical_parts,
5189                                                 g_strdup(path_parts[i]));
5190
5191                         tmp_path = canonical_list_to_file(canonical_parts);
5192
5193                         if(g_stat(tmp_path, &st) < 0) {
5194                                 if (errno == ENOENT) {
5195                                         errno = 0;
5196 #ifndef G_OS_WIN32
5197                                         follow_symlinks = FALSE;
5198 #endif
5199                                 }
5200                                 if (errno != 0) {
5201                                         g_free(tmp_path);
5202                                         slist_free_strings_full(canonical_parts);
5203                                         g_strfreev(path_parts);
5204
5205                                         return NULL;
5206                                 }
5207                         }
5208 #ifndef G_OS_WIN32
5209                         if (follow_symlinks && g_file_test(tmp_path, G_FILE_TEST_IS_SYMLINK)) {
5210                                 GError *error = NULL;
5211                                 gchar *target = g_file_read_link(tmp_path, &error);
5212
5213                                 if (!g_path_is_absolute(target)) {
5214                                         /* remove the last inserted element */
5215                                         canonical_parts = 
5216                                                 g_slist_delete_link(canonical_parts,
5217                                                             canonical_parts);
5218                                         /* add the target */
5219                                         canonical_parts = g_slist_prepend(canonical_parts,
5220                                                 g_strdup(target));
5221                                         g_free(target);
5222
5223                                         /* and get the new target */
5224                                         target = canonical_list_to_file(canonical_parts);
5225                                 }
5226
5227                                 /* restart from absolute target */
5228                                 slist_free_strings_full(canonical_parts);
5229                                 canonical_parts = NULL;
5230                                 if (!error)
5231                                         canonical_parts = cm_split_path(target, depth + 1);
5232                                 else
5233                                         g_error_free(error);
5234                                 if (canonical_parts == NULL) {
5235                                         g_free(tmp_path);
5236                                         g_strfreev(path_parts);
5237                                         return NULL;
5238                                 }
5239                                 g_free(target);
5240                         }
5241 #endif
5242                         g_free(tmp_path);
5243                 }
5244         }
5245         g_strfreev(path_parts);
5246         return canonical_parts;
5247 }
5248
5249 /*
5250  * Canonicalize a filename, resolving symlinks along the way.
5251  * Returns a negative errno in case of error.
5252  */
5253 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
5254         GSList *canonical_parts;
5255         gboolean is_absolute;
5256
5257         if (filename == NULL)
5258                 return -EINVAL;
5259         if (canonical_name == NULL)
5260                 return -EINVAL;
5261         *canonical_name = NULL;
5262
5263         is_absolute = g_path_is_absolute(filename);
5264         if (!is_absolute) {
5265                 /* Always work on absolute filenames. */
5266                 gchar *cur = g_get_current_dir();
5267                 gchar *absolute_filename = g_strconcat(cur, G_DIR_SEPARATOR_S,
5268                                                        filename, NULL);
5269                 
5270                 canonical_parts = cm_split_path(absolute_filename, 0);
5271                 g_free(absolute_filename);
5272                 g_free(cur);
5273         } else
5274                 canonical_parts = cm_split_path(filename, 0);
5275
5276         if (canonical_parts == NULL)
5277                 return -errno;
5278
5279         *canonical_name = canonical_list_to_file(canonical_parts);
5280         slist_free_strings_full(canonical_parts);
5281         return 0;
5282 }
5283
5284 /* Returns a decoded base64 string, guaranteed to be null-terminated. */
5285 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len)
5286 {
5287         gchar *tmp = g_base64_decode(text, out_len);
5288         gchar *out = g_strndup(tmp, *out_len);
5289
5290         g_free(tmp);
5291
5292         if (strlen(out) != *out_len) {
5293                 g_warning ("strlen(out) %zd != *out_len %" G_GSIZE_FORMAT, strlen(out), *out_len);
5294         }
5295
5296         return out;
5297 }
5298
5299 #if !GLIB_CHECK_VERSION(2, 30, 0)
5300 /**
5301  * g_utf8_substring:
5302  * @str: a UTF-8 encoded string
5303  * @start_pos: a character offset within @str
5304  * @end_pos: another character offset within @str
5305  *
5306  * Copies a substring out of a UTF-8 encoded string.
5307  * The substring will contain @end_pos - @start_pos
5308  * characters.
5309  *
5310  * Returns: a newly allocated copy of the requested
5311  *     substring. Free with g_free() when no longer needed.
5312  *
5313  * Since: GLIB 2.30
5314  */
5315 gchar *
5316 g_utf8_substring (const gchar *str,
5317                                   glong            start_pos,
5318                                   glong            end_pos)
5319 {
5320   gchar *start, *end, *out;
5321
5322   start = g_utf8_offset_to_pointer (str, start_pos);
5323   end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
5324
5325   out = g_malloc (end - start + 1);
5326   memcpy (out, start, end - start);
5327   out[end - start] = 0;
5328
5329   return out;
5330 }
5331 #endif
5332
5333 /* Attempts to read count bytes from a PRNG into memory area starting at buf.
5334  * It is up to the caller to make sure there is at least count bytes
5335  * available at buf. */
5336 gboolean
5337 get_random_bytes(void *buf, size_t count)
5338 {
5339         /* Open our prng source. */
5340 #if defined G_OS_WIN32
5341         HCRYPTPROV rnd;
5342
5343         if (!CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, 0) &&
5344                         !CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
5345                 debug_print("Could not acquire a CSP handle.\n");
5346                 return FALSE;
5347         }
5348 #else
5349         int rnd;
5350         ssize_t ret;
5351
5352         rnd = open("/dev/urandom", O_RDONLY);
5353         if (rnd == -1) {
5354                 FILE_OP_ERROR("/dev/urandom", "open");
5355                 debug_print("Could not open /dev/urandom.\n");
5356                 return FALSE;
5357         }
5358 #endif
5359
5360         /* Read data from the source into buf. */
5361 #if defined G_OS_WIN32
5362         if (!CryptGenRandom(rnd, count, buf)) {
5363                 debug_print("Could not read %zd random bytes.\n", count);
5364                 CryptReleaseContext(rnd, 0);
5365                 return FALSE;
5366         }
5367 #else
5368         ret = read(rnd, buf, count);
5369         if (ret != count) {
5370                 FILE_OP_ERROR("/dev/urandom", "read");
5371                 debug_print("Could not read enough data from /dev/urandom, read only %ld of %lu bytes.\n", ret, count);
5372                 close(rnd);
5373                 return FALSE;
5374         }
5375 #endif
5376
5377         /* Close the prng source. */
5378 #if defined G_OS_WIN32
5379         CryptReleaseContext(rnd, 0);
5380 #else
5381         close(rnd);
5382 #endif
5383
5384         return TRUE;
5385 }