fix bugs 4373, 'attach mailto URI double free' and 4374, ' insert mailto URI misses...
[claws.git] / src / common / utils.c
blob848f0ce8690c27bde1bd1f035c6241552da0fb6d
1 /*
2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2020 The Claws Mail Team and Hiroyuki Yamamoto
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.
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.
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/>.
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
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.
27 * https://git.gnome.org/browse/glib/tree/glib/gutf8.c
28 * ?h=glib-2-30&id=9eb65dd3ed5e1a9638595cbe10699c7606376511
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #include "claws-features.h"
34 #endif
36 #include "defs.h"
38 #include <glib.h>
39 #include <gio/gio.h>
41 #include <glib/gi18n.h>
43 #ifdef USE_PTHREAD
44 #include <pthread.h>
45 #endif
47 #include <stdio.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <sys/param.h>
52 #ifdef G_OS_WIN32
53 # include <ws2tcpip.h>
54 #else
55 # include <sys/socket.h>
56 #endif
58 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
59 # include <wchar.h>
60 # include <wctype.h>
61 #endif
62 #include <stdlib.h>
63 #include <sys/stat.h>
64 #include <unistd.h>
65 #include <stdarg.h>
66 #include <sys/types.h>
67 #if HAVE_SYS_WAIT_H
68 # include <sys/wait.h>
69 #endif
70 #include <dirent.h>
71 #include <time.h>
72 #include <regex.h>
74 #ifdef G_OS_UNIX
75 #include <sys/utsname.h>
76 #endif
78 #include <fcntl.h>
80 #ifdef G_OS_WIN32
81 # include <direct.h>
82 # include <io.h>
83 # include <w32lib.h>
84 #endif
86 #include "utils.h"
87 #include "socket.h"
88 #include "codeconv.h"
89 #include "tlds.h"
90 #include "timing.h"
91 #include "file-utils.h"
93 #define BUFFSIZE 8192
95 static gboolean debug_mode = FALSE;
97 GSList *slist_copy_deep(GSList *list, GCopyFunc func)
99 #if GLIB_CHECK_VERSION(2, 34, 0)
100 return g_slist_copy_deep(list, func, NULL);
101 #else
102 GSList *res = g_slist_copy(list);
103 GSList *walk = res;
104 while (walk) {
105 walk->data = func(walk->data, NULL);
106 walk = walk->next;
108 return res;
109 #endif
112 void list_free_strings_full(GList *list)
114 g_list_free_full(list, (GDestroyNotify)g_free);
117 void slist_free_strings_full(GSList *list)
119 g_slist_free_full(list, (GDestroyNotify)g_free);
122 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
124 g_free(key);
127 void hash_free_strings(GHashTable *table)
129 g_hash_table_foreach(table, hash_free_strings_func, NULL);
132 gint str_case_equal(gconstpointer v, gconstpointer v2)
134 return g_ascii_strcasecmp((const gchar *)v, (const gchar *)v2) == 0;
137 guint str_case_hash(gconstpointer key)
139 const gchar *p = key;
140 guint h = *p;
142 if (h) {
143 h = g_ascii_tolower(h);
144 for (p += 1; *p != '\0'; p++)
145 h = (h << 5) - h + g_ascii_tolower(*p);
148 return h;
151 gint to_number(const gchar *nstr)
153 register const gchar *p;
155 if (*nstr == '\0') return -1;
157 for (p = nstr; *p != '\0'; p++)
158 if (!g_ascii_isdigit(*p)) return -1;
160 return atoi(nstr);
163 /* convert integer into string,
164 nstr must be not lower than 11 characters length */
165 gchar *itos_buf(gchar *nstr, gint n)
167 g_snprintf(nstr, 11, "%d", n);
168 return nstr;
171 /* convert integer into string */
172 gchar *itos(gint n)
174 static gchar nstr[11];
176 return itos_buf(nstr, n);
179 #define divide(num,divisor,i,d) \
181 i = num >> divisor; \
182 d = num & ((1<<divisor)-1); \
183 d = (d*100) >> divisor; \
188 * \brief Convert a given size in bytes in a human-readable string
190 * \param size The size expressed in bytes to convert in string
191 * \return The string that respresents the size in an human-readable way
193 gchar *to_human_readable(goffset size)
195 static gchar str[14];
196 static gchar *b_format = NULL, *kb_format = NULL,
197 *mb_format = NULL, *gb_format = NULL;
198 register int t = 0, r = 0;
199 if (b_format == NULL) {
200 b_format = _("%dB");
201 kb_format = _("%d.%02dKB");
202 mb_format = _("%d.%02dMB");
203 gb_format = _("%.2fGB");
206 if (size < (goffset)1024) {
207 g_snprintf(str, sizeof(str), b_format, (gint)size);
208 return str;
209 } else if (size >> 10 < (goffset)1024) {
210 divide(size, 10, t, r);
211 g_snprintf(str, sizeof(str), kb_format, t, r);
212 return str;
213 } else if (size >> 20 < (goffset)1024) {
214 divide(size, 20, t, r);
215 g_snprintf(str, sizeof(str), mb_format, t, r);
216 return str;
217 } else {
218 g_snprintf(str, sizeof(str), gb_format, (gfloat)(size >> 30));
219 return str;
223 /* compare paths */
224 gint path_cmp(const gchar *s1, const gchar *s2)
226 gint len1, len2;
227 int rc;
228 #ifdef G_OS_WIN32
229 gchar *s1buf, *s2buf;
230 #endif
232 if (s1 == NULL || s2 == NULL) return -1;
233 if (*s1 == '\0' || *s2 == '\0') return -1;
235 #ifdef G_OS_WIN32
236 s1buf = g_strdup (s1);
237 s2buf = g_strdup (s2);
238 subst_char (s1buf, '/', G_DIR_SEPARATOR);
239 subst_char (s2buf, '/', G_DIR_SEPARATOR);
240 s1 = s1buf;
241 s2 = s2buf;
242 #endif /* !G_OS_WIN32 */
244 len1 = strlen(s1);
245 len2 = strlen(s2);
247 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
248 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
250 rc = strncmp(s1, s2, MAX(len1, len2));
251 #ifdef G_OS_WIN32
252 g_free (s1buf);
253 g_free (s2buf);
254 #endif /* !G_OS_WIN32 */
255 return rc;
258 /* remove trailing return code */
259 gchar *strretchomp(gchar *str)
261 register gchar *s;
263 if (!*str) return str;
265 for (s = str + strlen(str) - 1;
266 s >= str && (*s == '\n' || *s == '\r');
267 s--)
268 *s = '\0';
270 return str;
273 /* remove trailing character */
274 gchar *strtailchomp(gchar *str, gchar tail_char)
276 register gchar *s;
278 if (!*str) return str;
279 if (tail_char == '\0') return str;
281 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
282 *s = '\0';
284 return str;
287 /* remove CR (carriage return) */
288 gchar *strcrchomp(gchar *str)
290 register gchar *s;
292 if (!*str) return str;
294 s = str + strlen(str) - 1;
295 if (*s == '\n' && s > str && *(s - 1) == '\r') {
296 *(s - 1) = '\n';
297 *s = '\0';
300 return str;
303 #ifndef HAVE_STRCASESTR
304 /* Similar to `strstr' but this function ignores the case of both strings. */
305 gchar *strcasestr(const gchar *haystack, const gchar *needle)
307 size_t haystack_len = strlen(haystack);
309 return strncasestr(haystack, haystack_len, needle);
311 #endif /* HAVE_STRCASESTR */
313 gchar *strncasestr(const gchar *haystack, gint haystack_len, const gchar *needle)
315 register size_t needle_len;
317 needle_len = strlen(needle);
319 if (haystack_len < needle_len || needle_len == 0)
320 return NULL;
322 while (haystack_len >= needle_len) {
323 if (!g_ascii_strncasecmp(haystack, needle, needle_len))
324 return (gchar *)haystack;
325 else {
326 haystack++;
327 haystack_len--;
331 return NULL;
334 gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
335 gconstpointer needle, size_t needlelen)
337 const gchar *haystack_ = (const gchar *)haystack;
338 const gchar *needle_ = (const gchar *)needle;
339 const gchar *haystack_cur = (const gchar *)haystack;
340 size_t haystack_left = haystacklen;
342 if (needlelen == 1)
343 return memchr(haystack_, *needle_, haystacklen);
345 while ((haystack_cur = memchr(haystack_cur, *needle_, haystack_left))
346 != NULL) {
347 if (haystacklen - (haystack_cur - haystack_) < needlelen)
348 break;
349 if (memcmp(haystack_cur + 1, needle_ + 1, needlelen - 1) == 0)
350 return (gpointer)haystack_cur;
351 else{
352 haystack_cur++;
353 haystack_left = haystacklen - (haystack_cur - haystack_);
357 return NULL;
360 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
361 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
363 register const gchar *s = src;
364 register gchar *d = dest;
366 while (--n && *s)
367 *d++ = *s++;
368 *d = '\0';
370 return dest;
374 /* Examine if next block is non-ASCII string */
375 gboolean is_next_nonascii(const gchar *s)
377 const gchar *p;
379 /* skip head space */
380 for (p = s; *p != '\0' && g_ascii_isspace(*p); p++)
382 for (; *p != '\0' && !g_ascii_isspace(*p); p++) {
383 if (*(guchar *)p > 127 || *(guchar *)p < 32)
384 return TRUE;
387 return FALSE;
390 gint get_next_word_len(const gchar *s)
392 gint len = 0;
394 for (; *s != '\0' && !g_ascii_isspace(*s); s++, len++)
397 return len;
400 static void trim_subject_for_compare(gchar *str)
402 gchar *srcp;
404 eliminate_parenthesis(str, '[', ']');
405 eliminate_parenthesis(str, '(', ')');
406 g_strstrip(str);
408 srcp = str + subject_get_prefix_length(str);
409 if (srcp != str)
410 memmove(str, srcp, strlen(srcp) + 1);
413 static void trim_subject_for_sort(gchar *str)
415 gchar *srcp;
417 g_strstrip(str);
419 srcp = str + subject_get_prefix_length(str);
420 if (srcp != str)
421 memmove(str, srcp, strlen(srcp) + 1);
424 /* compare subjects */
425 gint subject_compare(const gchar *s1, const gchar *s2)
427 gchar *str1, *str2;
429 if (!s1 || !s2) return -1;
430 if (!*s1 || !*s2) return -1;
432 Xstrdup_a(str1, s1, return -1);
433 Xstrdup_a(str2, s2, return -1);
435 trim_subject_for_compare(str1);
436 trim_subject_for_compare(str2);
438 if (!*str1 || !*str2) return -1;
440 return strcmp(str1, str2);
443 gint subject_compare_for_sort(const gchar *s1, const gchar *s2)
445 gchar *str1, *str2;
447 if (!s1 || !s2) return -1;
449 Xstrdup_a(str1, s1, return -1);
450 Xstrdup_a(str2, s2, return -1);
452 trim_subject_for_sort(str1);
453 trim_subject_for_sort(str2);
455 return g_utf8_collate(str1, str2);
458 void trim_subject(gchar *str)
460 register gchar *srcp;
461 gchar op, cl;
462 gint in_brace;
464 g_strstrip(str);
466 srcp = str + subject_get_prefix_length(str);
468 if (*srcp == '[') {
469 op = '[';
470 cl = ']';
471 } else if (*srcp == '(') {
472 op = '(';
473 cl = ')';
474 } else
475 op = 0;
477 if (op) {
478 ++srcp;
479 in_brace = 1;
480 while (*srcp) {
481 if (*srcp == op)
482 in_brace++;
483 else if (*srcp == cl)
484 in_brace--;
485 srcp++;
486 if (in_brace == 0)
487 break;
490 while (g_ascii_isspace(*srcp)) srcp++;
491 memmove(str, srcp, strlen(srcp) + 1);
494 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
496 register gchar *srcp, *destp;
497 gint in_brace;
499 destp = str;
501 while ((destp = strchr(destp, op))) {
502 in_brace = 1;
503 srcp = destp + 1;
504 while (*srcp) {
505 if (*srcp == op)
506 in_brace++;
507 else if (*srcp == cl)
508 in_brace--;
509 srcp++;
510 if (in_brace == 0)
511 break;
513 while (g_ascii_isspace(*srcp)) srcp++;
514 memmove(destp, srcp, strlen(srcp) + 1);
518 void extract_parenthesis(gchar *str, gchar op, gchar cl)
520 register gchar *srcp, *destp;
521 gint in_brace;
523 destp = str;
525 while ((srcp = strchr(destp, op))) {
526 if (destp > str)
527 *destp++ = ' ';
528 memmove(destp, srcp + 1, strlen(srcp));
529 in_brace = 1;
530 while(*destp) {
531 if (*destp == op)
532 in_brace++;
533 else if (*destp == cl)
534 in_brace--;
536 if (in_brace == 0)
537 break;
539 destp++;
542 *destp = '\0';
545 static void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
546 gchar op, gchar cl)
548 register gchar *srcp, *destp;
549 gint in_brace;
550 gboolean in_quote = FALSE;
552 destp = str;
554 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
555 if (destp > str)
556 *destp++ = ' ';
557 memmove(destp, srcp + 1, strlen(srcp));
558 in_brace = 1;
559 while(*destp) {
560 if (*destp == op && !in_quote)
561 in_brace++;
562 else if (*destp == cl && !in_quote)
563 in_brace--;
564 else if (*destp == quote_chr)
565 in_quote ^= TRUE;
567 if (in_brace == 0)
568 break;
570 destp++;
573 *destp = '\0';
576 void extract_quote(gchar *str, gchar quote_chr)
578 register gchar *p;
580 if ((str = strchr(str, quote_chr))) {
581 p = str;
582 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
583 memmove(p - 1, p, strlen(p) + 1);
584 p--;
586 if(p) {
587 *p = '\0';
588 memmove(str, str + 1, p - str);
593 /* Returns a newly allocated string with all quote_chr not at the beginning
594 or the end of str escaped with '\' or the given str if not required. */
595 gchar *escape_internal_quotes(gchar *str, gchar quote_chr)
597 register gchar *p, *q;
598 gchar *qstr;
599 int k = 0, l = 0;
601 if (str == NULL || *str == '\0')
602 return str;
604 /* search for unescaped quote_chr */
605 p = str;
606 if (*p == quote_chr)
607 ++p, ++l;
608 while (*p) {
609 if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
610 ++k;
611 ++p, ++l;
613 if (!k) /* nothing to escape */
614 return str;
616 /* unescaped quote_chr found */
617 qstr = g_malloc(l + k + 1);
618 p = str;
619 q = qstr;
620 if (*p == quote_chr) {
621 *q = quote_chr;
622 ++p, ++q;
624 while (*p) {
625 if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
626 *q++ = '\\';
627 *q++ = *p++;
629 *q = '\0';
631 return qstr;
634 void eliminate_address_comment(gchar *str)
636 register gchar *srcp, *destp;
637 gint in_brace;
639 destp = str;
641 while ((destp = strchr(destp, '"'))) {
642 if ((srcp = strchr(destp + 1, '"'))) {
643 srcp++;
644 if (*srcp == '@') {
645 destp = srcp + 1;
646 } else {
647 while (g_ascii_isspace(*srcp)) srcp++;
648 memmove(destp, srcp, strlen(srcp) + 1);
650 } else {
651 *destp = '\0';
652 break;
656 destp = str;
658 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
659 in_brace = 1;
660 srcp = destp + 1;
661 while (*srcp) {
662 if (*srcp == '(')
663 in_brace++;
664 else if (*srcp == ')')
665 in_brace--;
666 srcp++;
667 if (in_brace == 0)
668 break;
670 while (g_ascii_isspace(*srcp)) srcp++;
671 memmove(destp, srcp, strlen(srcp) + 1);
675 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
677 gboolean in_quote = FALSE;
679 while (*str) {
680 if (*str == c && !in_quote)
681 return (gchar *)str;
682 if (*str == quote_chr)
683 in_quote ^= TRUE;
684 str++;
687 return NULL;
690 void extract_address(gchar *str)
692 cm_return_if_fail(str != NULL);
693 eliminate_address_comment(str);
694 if (strchr_with_skip_quote(str, '"', '<'))
695 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
696 g_strstrip(str);
699 void extract_list_id_str(gchar *str)
701 if (strchr_with_skip_quote(str, '"', '<'))
702 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
703 g_strstrip(str);
706 static GSList *address_list_append_real(GSList *addr_list, const gchar *str, gboolean removecomments)
708 gchar *work;
709 gchar *workp;
711 if (!str) return addr_list;
713 Xstrdup_a(work, str, return addr_list);
715 if (removecomments)
716 eliminate_address_comment(work);
717 workp = work;
719 while (workp && *workp) {
720 gchar *p, *next;
722 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
723 *p = '\0';
724 next = p + 1;
725 } else
726 next = NULL;
728 if (removecomments && strchr_with_skip_quote(workp, '"', '<'))
729 extract_parenthesis_with_skip_quote
730 (workp, '"', '<', '>');
732 g_strstrip(workp);
733 if (*workp)
734 addr_list = g_slist_append(addr_list, g_strdup(workp));
736 workp = next;
739 return addr_list;
742 GSList *address_list_append(GSList *addr_list, const gchar *str)
744 return address_list_append_real(addr_list, str, TRUE);
747 GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
749 return address_list_append_real(addr_list, str, FALSE);
752 GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
754 const gchar *strp;
756 if (!str) return msgid_list;
757 strp = str;
759 while (strp && *strp) {
760 const gchar *start, *end;
761 gchar *msgid;
763 if ((start = strchr(strp, '<')) != NULL) {
764 end = strchr(start + 1, '>');
765 if (!end) break;
766 } else
767 break;
769 msgid = g_strndup(start + 1, end - start - 1);
770 g_strstrip(msgid);
771 if (*msgid)
772 msgid_list = g_slist_prepend(msgid_list, msgid);
773 else
774 g_free(msgid);
776 strp = end + 1;
779 return msgid_list;
782 GSList *references_list_append(GSList *msgid_list, const gchar *str)
784 GSList *list;
786 list = references_list_prepend(NULL, str);
787 list = g_slist_reverse(list);
788 msgid_list = g_slist_concat(msgid_list, list);
790 return msgid_list;
793 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
795 gchar *work;
796 gchar *workp;
798 if (!str) return group_list;
800 Xstrdup_a(work, str, return group_list);
802 workp = work;
804 while (workp && *workp) {
805 gchar *p, *next;
807 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
808 *p = '\0';
809 next = p + 1;
810 } else
811 next = NULL;
813 g_strstrip(workp);
814 if (*workp)
815 group_list = g_slist_append(group_list,
816 g_strdup(workp));
818 workp = next;
821 return group_list;
824 GList *add_history(GList *list, const gchar *str)
826 GList *old;
827 gchar *oldstr;
829 cm_return_val_if_fail(str != NULL, list);
831 old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)g_strcmp0);
832 if (old) {
833 oldstr = old->data;
834 list = g_list_remove(list, old->data);
835 g_free(oldstr);
836 } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
837 GList *last;
839 last = g_list_last(list);
840 if (last) {
841 oldstr = last->data;
842 list = g_list_remove(list, last->data);
843 g_free(oldstr);
847 list = g_list_prepend(list, g_strdup(str));
849 return list;
852 void remove_return(gchar *str)
854 register gchar *p = str;
856 while (*p) {
857 if (*p == '\n' || *p == '\r')
858 memmove(p, p + 1, strlen(p));
859 else
860 p++;
864 void remove_space(gchar *str)
866 register gchar *p = str;
867 register gint spc;
869 while (*p) {
870 spc = 0;
871 while (g_ascii_isspace(*(p + spc)))
872 spc++;
873 if (spc)
874 memmove(p, p + spc, strlen(p + spc) + 1);
875 else
876 p++;
880 void unfold_line(gchar *str)
882 register gchar *ch;
883 register gunichar c;
884 register gint len;
886 ch = str; /* iterator for source string */
888 while (*ch != 0) {
889 c = g_utf8_get_char_validated(ch, -1);
891 if (c == (gunichar)-1 || c == (gunichar)-2) {
892 /* non-unicode byte, move past it */
893 ch++;
894 continue;
897 len = g_unichar_to_utf8(c, NULL);
899 if (!g_unichar_isdefined(c) || !g_unichar_isprint(c) ||
900 g_unichar_isspace(c)) {
901 /* replace anything bad or whitespacey with a single space */
902 *ch = ' ';
903 ch++;
904 if (len > 1) {
905 /* move rest of the string forwards, since we just replaced
906 * a multi-byte sequence with one byte */
907 memmove(ch, ch + len-1, strlen(ch + len-1) + 1);
909 } else {
910 /* A valid unicode character, copy it. */
911 ch += len;
916 void subst_char(gchar *str, gchar orig, gchar subst)
918 register gchar *p = str;
920 while (*p) {
921 if (*p == orig)
922 *p = subst;
923 p++;
927 void subst_chars(gchar *str, gchar *orig, gchar subst)
929 register gchar *p = str;
931 while (*p) {
932 if (strchr(orig, *p) != NULL)
933 *p = subst;
934 p++;
938 void subst_for_filename(gchar *str)
940 if (!str)
941 return;
942 #ifdef G_OS_WIN32
943 subst_chars(str, "\t\r\n\\/*?:", '_');
944 #else
945 subst_chars(str, "\t\r\n\\/*", '_');
946 #endif
949 void subst_for_shellsafe_filename(gchar *str)
951 if (!str)
952 return;
953 subst_for_filename(str);
954 subst_chars(str, " \"'|&;()<>'!{}[]",'_');
957 gboolean is_ascii_str(const gchar *str)
959 const guchar *p = (const guchar *)str;
961 while (*p != '\0') {
962 if (*p != '\t' && *p != ' ' &&
963 *p != '\r' && *p != '\n' &&
964 (*p < 32 || *p >= 127))
965 return FALSE;
966 p++;
969 return TRUE;
972 static const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
974 gchar * position = NULL;
975 gchar * tmp_pos = NULL;
976 int i;
978 if (str == NULL || quote_chars == NULL)
979 return NULL;
981 for (i = 0; i < strlen(quote_chars); i++) {
982 tmp_pos = strrchr (str, quote_chars[i]);
983 if(position == NULL
984 || (tmp_pos != NULL && position <= tmp_pos) )
985 position = tmp_pos;
987 return position;
990 gint get_quote_level(const gchar *str, const gchar *quote_chars)
992 const gchar *first_pos;
993 const gchar *last_pos;
994 const gchar *p = str;
995 gint quote_level = -1;
997 /* speed up line processing by only searching to the last '>' */
998 if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
999 /* skip a line if it contains a '<' before the initial '>' */
1000 if (memchr(str, '<', first_pos - str) != NULL)
1001 return -1;
1002 last_pos = line_has_quote_char_last(first_pos, quote_chars);
1003 } else
1004 return -1;
1006 while (p <= last_pos) {
1007 while (p < last_pos) {
1008 if (g_ascii_isspace(*p))
1009 p++;
1010 else
1011 break;
1014 if (strchr(quote_chars, *p))
1015 quote_level++;
1016 else if (*p != '-' && !g_ascii_isspace(*p) && p <= last_pos) {
1017 /* any characters are allowed except '-','<' and space */
1018 while (*p != '-' && *p != '<'
1019 && !strchr(quote_chars, *p)
1020 && !g_ascii_isspace(*p)
1021 && p < last_pos)
1022 p++;
1023 if (strchr(quote_chars, *p))
1024 quote_level++;
1025 else
1026 break;
1029 p++;
1032 return quote_level;
1035 gint check_line_length(const gchar *str, gint max_chars, gint *line)
1037 const gchar *p = str, *q;
1038 gint cur_line = 0, len;
1040 while ((q = strchr(p, '\n')) != NULL) {
1041 len = q - p + 1;
1042 if (len > max_chars) {
1043 if (line)
1044 *line = cur_line;
1045 return -1;
1047 p = q + 1;
1048 ++cur_line;
1051 len = strlen(p);
1052 if (len > max_chars) {
1053 if (line)
1054 *line = cur_line;
1055 return -1;
1058 return 0;
1061 const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
1063 gchar * position = NULL;
1064 gchar * tmp_pos = NULL;
1065 int i;
1067 if (str == NULL || quote_chars == NULL)
1068 return NULL;
1070 for (i = 0; i < strlen(quote_chars); i++) {
1071 tmp_pos = strchr (str, quote_chars[i]);
1072 if(position == NULL
1073 || (tmp_pos != NULL && position >= tmp_pos) )
1074 position = tmp_pos;
1076 return position;
1079 static gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1081 register guint haystack_len, needle_len;
1082 gboolean in_squote = FALSE, in_dquote = FALSE;
1084 haystack_len = strlen(haystack);
1085 needle_len = strlen(needle);
1087 if (haystack_len < needle_len || needle_len == 0)
1088 return NULL;
1090 while (haystack_len >= needle_len) {
1091 if (!in_squote && !in_dquote &&
1092 !strncmp(haystack, needle, needle_len))
1093 return (gchar *)haystack;
1095 /* 'foo"bar"' -> foo"bar"
1096 "foo'bar'" -> foo'bar' */
1097 if (*haystack == '\'') {
1098 if (in_squote)
1099 in_squote = FALSE;
1100 else if (!in_dquote)
1101 in_squote = TRUE;
1102 } else if (*haystack == '\"') {
1103 if (in_dquote)
1104 in_dquote = FALSE;
1105 else if (!in_squote)
1106 in_dquote = TRUE;
1107 } else if (*haystack == '\\') {
1108 haystack++;
1109 haystack_len--;
1112 haystack++;
1113 haystack_len--;
1116 return NULL;
1119 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1120 gint max_tokens)
1122 GSList *string_list = NULL, *slist;
1123 gchar **str_array, *s, *new_str;
1124 guint i, n = 1, len;
1126 cm_return_val_if_fail(str != NULL, NULL);
1127 cm_return_val_if_fail(delim != NULL, NULL);
1129 if (max_tokens < 1)
1130 max_tokens = G_MAXINT;
1132 s = strstr_with_skip_quote(str, delim);
1133 if (s) {
1134 guint delimiter_len = strlen(delim);
1136 do {
1137 len = s - str;
1138 new_str = g_strndup(str, len);
1140 if (new_str[0] == '\'' || new_str[0] == '\"') {
1141 if (new_str[len - 1] == new_str[0]) {
1142 new_str[len - 1] = '\0';
1143 memmove(new_str, new_str + 1, len - 1);
1146 string_list = g_slist_prepend(string_list, new_str);
1147 n++;
1148 str = s + delimiter_len;
1149 s = strstr_with_skip_quote(str, delim);
1150 } while (--max_tokens && s);
1153 if (*str) {
1154 new_str = g_strdup(str);
1155 if (new_str[0] == '\'' || new_str[0] == '\"') {
1156 len = strlen(str);
1157 if (new_str[len - 1] == new_str[0]) {
1158 new_str[len - 1] = '\0';
1159 memmove(new_str, new_str + 1, len - 1);
1162 string_list = g_slist_prepend(string_list, new_str);
1163 n++;
1166 str_array = g_new(gchar*, n);
1168 i = n - 1;
1170 str_array[i--] = NULL;
1171 for (slist = string_list; slist; slist = slist->next)
1172 str_array[i--] = slist->data;
1174 g_slist_free(string_list);
1176 return str_array;
1179 gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
1181 gchar *abbrev_group;
1182 gchar *ap;
1183 const gchar *p = group;
1184 const gchar *last;
1186 cm_return_val_if_fail(group != NULL, NULL);
1188 last = group + strlen(group);
1189 abbrev_group = ap = g_malloc(strlen(group) + 1);
1191 while (*p) {
1192 while (*p == '.')
1193 *ap++ = *p++;
1194 if ((ap - abbrev_group) + (last - p) > len && strchr(p, '.')) {
1195 *ap++ = *p++;
1196 while (*p != '.') p++;
1197 } else {
1198 strcpy(ap, p);
1199 return abbrev_group;
1203 *ap = '\0';
1204 return abbrev_group;
1207 gchar *trim_string(const gchar *str, gint len)
1209 const gchar *p = str;
1210 gint mb_len;
1211 gchar *new_str;
1212 gint new_len = 0;
1214 if (!str) return NULL;
1215 if (strlen(str) <= len)
1216 return g_strdup(str);
1217 if (g_utf8_validate(str, -1, NULL) == FALSE)
1218 return g_strdup(str);
1220 while (*p != '\0') {
1221 mb_len = g_utf8_skip[*(guchar *)p];
1222 if (mb_len == 0)
1223 break;
1224 else if (new_len + mb_len > len)
1225 break;
1227 new_len += mb_len;
1228 p += mb_len;
1231 Xstrndup_a(new_str, str, new_len, return g_strdup(str));
1232 return g_strconcat(new_str, "...", NULL);
1235 GList *uri_list_extract_filenames(const gchar *uri_list)
1237 GList *result = NULL;
1238 const gchar *p, *q;
1239 gchar *escaped_utf8uri;
1241 p = uri_list;
1243 while (p) {
1244 if (*p != '#') {
1245 while (g_ascii_isspace(*p)) p++;
1246 if (!strncmp(p, "file:", 5)) {
1247 q = p;
1248 q += 5;
1249 while (*q && *q != '\n' && *q != '\r') q++;
1251 if (q > p) {
1252 gchar *file, *locale_file = NULL;
1253 q--;
1254 while (q > p && g_ascii_isspace(*q))
1255 q--;
1256 Xalloca(escaped_utf8uri, q - p + 2,
1257 return result);
1258 Xalloca(file, q - p + 2,
1259 return result);
1260 *file = '\0';
1261 strncpy(escaped_utf8uri, p, q - p + 1);
1262 escaped_utf8uri[q - p + 1] = '\0';
1263 decode_uri_with_plus(file, escaped_utf8uri, FALSE);
1265 * g_filename_from_uri() rejects escaped/locale encoded uri
1266 * string which come from Nautilus.
1268 #ifndef G_OS_WIN32
1269 if (g_utf8_validate(file, -1, NULL))
1270 locale_file
1271 = conv_codeset_strdup(
1272 file + 5,
1273 CS_UTF_8,
1274 conv_get_locale_charset_str());
1275 if (!locale_file)
1276 locale_file = g_strdup(file + 5);
1277 #else
1278 locale_file = g_filename_from_uri(escaped_utf8uri, NULL, NULL);
1279 #endif
1280 result = g_list_append(result, locale_file);
1284 p = strchr(p, '\n');
1285 if (p) p++;
1288 return result;
1291 /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
1292 * characters
1294 static gint axtoi(const gchar *hexstr)
1296 gint hi, lo, result;
1298 hi = hexstr[0];
1299 if ('0' <= hi && hi <= '9') {
1300 hi -= '0';
1301 } else
1302 if ('a' <= hi && hi <= 'f') {
1303 hi -= ('a' - 10);
1304 } else
1305 if ('A' <= hi && hi <= 'F') {
1306 hi -= ('A' - 10);
1309 lo = hexstr[1];
1310 if ('0' <= lo && lo <= '9') {
1311 lo -= '0';
1312 } else
1313 if ('a' <= lo && lo <= 'f') {
1314 lo -= ('a'-10);
1315 } else
1316 if ('A' <= lo && lo <= 'F') {
1317 lo -= ('A' - 10);
1319 result = lo + (16 * hi);
1320 return result;
1323 gboolean is_uri_string(const gchar *str)
1325 while (str && *str && g_ascii_isspace(*str))
1326 str++;
1327 return (g_ascii_strncasecmp(str, "http://", 7) == 0 ||
1328 g_ascii_strncasecmp(str, "https://", 8) == 0 ||
1329 g_ascii_strncasecmp(str, "ftp://", 6) == 0 ||
1330 g_ascii_strncasecmp(str, "www.", 4) == 0);
1333 gchar *get_uri_path(const gchar *uri)
1335 while (uri && *uri && g_ascii_isspace(*uri))
1336 uri++;
1337 if (g_ascii_strncasecmp(uri, "http://", 7) == 0)
1338 return (gchar *)(uri + 7);
1339 else if (g_ascii_strncasecmp(uri, "https://", 8) == 0)
1340 return (gchar *)(uri + 8);
1341 else if (g_ascii_strncasecmp(uri, "ftp://", 6) == 0)
1342 return (gchar *)(uri + 6);
1343 else
1344 return (gchar *)uri;
1347 gint get_uri_len(const gchar *str)
1349 const gchar *p;
1351 if (is_uri_string(str)) {
1352 for (p = str; *p != '\0'; p++) {
1353 if (!g_ascii_isgraph(*p) || strchr("()<>\"", *p))
1354 break;
1356 return p - str;
1359 return 0;
1362 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
1363 * plusses, and escape characters are used)
1365 void decode_uri_with_plus(gchar *decoded_uri, const gchar *encoded_uri, gboolean with_plus)
1367 gchar *dec = decoded_uri;
1368 const gchar *enc = encoded_uri;
1370 while (*enc) {
1371 if (*enc == '%') {
1372 enc++;
1373 if (isxdigit((guchar)enc[0]) &&
1374 isxdigit((guchar)enc[1])) {
1375 *dec = axtoi(enc);
1376 dec++;
1377 enc += 2;
1379 } else {
1380 if (with_plus && *enc == '+')
1381 *dec = ' ';
1382 else
1383 *dec = *enc;
1384 dec++;
1385 enc++;
1389 *dec = '\0';
1392 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
1394 decode_uri_with_plus(decoded_uri, encoded_uri, TRUE);
1397 static gchar *decode_uri_gdup(const gchar *encoded_uri)
1399 gchar *buffer = g_malloc(strlen(encoded_uri)+1);
1400 decode_uri_with_plus(buffer, encoded_uri, FALSE);
1401 return buffer;
1404 gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc, gchar **bcc,
1405 gchar **subject, gchar **body, gchar ***attach, gchar **inreplyto)
1407 gchar *tmp_mailto;
1408 gchar *p;
1409 const gchar *forbidden_uris[] = { ".gnupg/",
1410 "/etc/passwd",
1411 "/etc/shadow",
1412 ".ssh/",
1413 "../",
1414 NULL };
1415 gint num_attach = 0;
1417 cm_return_val_if_fail(mailto != NULL, -1);
1419 Xstrdup_a(tmp_mailto, mailto, return -1);
1421 if (!strncmp(tmp_mailto, "mailto:", 7))
1422 tmp_mailto += 7;
1424 p = strchr(tmp_mailto, '?');
1425 if (p) {
1426 *p = '\0';
1427 p++;
1430 if (to && !*to)
1431 *to = decode_uri_gdup(tmp_mailto);
1433 while (p) {
1434 gchar *field, *value;
1436 field = p;
1438 p = strchr(p, '=');
1439 if (!p) break;
1440 *p = '\0';
1441 p++;
1443 value = p;
1445 p = strchr(p, '&');
1446 if (p) {
1447 *p = '\0';
1448 p++;
1451 if (*value == '\0') continue;
1453 if (from && !g_ascii_strcasecmp(field, "from")) {
1454 if (!*from) {
1455 *from = decode_uri_gdup(value);
1456 } else {
1457 gchar *tmp = decode_uri_gdup(value);
1458 gchar *new_from = g_strdup_printf("%s, %s", *from, tmp);
1459 g_free(tmp);
1460 g_free(*from);
1461 *from = new_from;
1463 } else if (cc && !g_ascii_strcasecmp(field, "cc")) {
1464 if (!*cc) {
1465 *cc = decode_uri_gdup(value);
1466 } else {
1467 gchar *tmp = decode_uri_gdup(value);
1468 gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
1469 g_free(tmp);
1470 g_free(*cc);
1471 *cc = new_cc;
1473 } else if (bcc && !g_ascii_strcasecmp(field, "bcc")) {
1474 if (!*bcc) {
1475 *bcc = decode_uri_gdup(value);
1476 } else {
1477 gchar *tmp = decode_uri_gdup(value);
1478 gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
1479 g_free(tmp);
1480 g_free(*bcc);
1481 *bcc = new_bcc;
1483 } else if (subject && !*subject &&
1484 !g_ascii_strcasecmp(field, "subject")) {
1485 *subject = decode_uri_gdup(value);
1486 } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
1487 *body = decode_uri_gdup(value);
1488 } else if (body && !*body && !g_ascii_strcasecmp(field, "insert")) {
1489 int i = 0;
1490 gchar *tmp = decode_uri_gdup(value);
1492 for (; forbidden_uris[i]; i++) {
1493 if (strstr(tmp, forbidden_uris[i])) {
1494 g_print("Refusing to insert '%s', potential private data leak\n",
1495 tmp);
1496 g_free(tmp);
1497 tmp = NULL;
1498 break;
1502 if (tmp) {
1503 if (!is_file_entry_regular(tmp)) {
1504 g_warning("Refusing to insert '%s', not a regular file\n", tmp);
1505 } else if (!g_file_get_contents(tmp, body, NULL, NULL)) {
1506 g_warning("couldn't set insert file '%s' in body", value);
1509 g_free(tmp);
1511 } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
1512 int i = 0;
1513 gchar *tmp = decode_uri_gdup(value);
1514 gchar **my_att = g_malloc(sizeof(char *));
1516 my_att[0] = NULL;
1518 for (; forbidden_uris[i]; i++) {
1519 if (strstr(tmp, forbidden_uris[i])) {
1520 g_print("Refusing to attach '%s', potential private data leak\n",
1521 tmp);
1522 g_free(tmp);
1523 g_free(my_att);
1524 tmp = NULL;
1525 break;
1528 if (tmp) {
1529 /* attach is correct */
1530 num_attach++;
1531 my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
1532 my_att[num_attach-1] = tmp;
1533 my_att[num_attach] = NULL;
1534 *attach = my_att;
1536 } else if (inreplyto && !*inreplyto &&
1537 !g_ascii_strcasecmp(field, "in-reply-to")) {
1538 *inreplyto = decode_uri_gdup(value);
1542 return 0;
1546 #ifdef G_OS_WIN32
1547 #include <windows.h>
1548 #ifndef CSIDL_APPDATA
1549 #define CSIDL_APPDATA 0x001a
1550 #endif
1551 #ifndef CSIDL_LOCAL_APPDATA
1552 #define CSIDL_LOCAL_APPDATA 0x001c
1553 #endif
1554 #ifndef CSIDL_FLAG_CREATE
1555 #define CSIDL_FLAG_CREATE 0x8000
1556 #endif
1557 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
1559 #define RTLD_LAZY 0
1560 const char *
1561 w32_strerror (int w32_errno)
1563 static char strerr[256];
1564 int ec = (int)GetLastError ();
1566 if (w32_errno == 0)
1567 w32_errno = ec;
1568 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, w32_errno,
1569 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
1570 strerr, DIM (strerr)-1, NULL);
1571 return strerr;
1574 static __inline__ void *
1575 dlopen (const char * name, int flag)
1577 void * hd = LoadLibrary (name);
1578 return hd;
1581 static __inline__ void *
1582 dlsym (void * hd, const char * sym)
1584 if (hd && sym)
1586 void * fnc = GetProcAddress (hd, sym);
1587 if (!fnc)
1588 return NULL;
1589 return fnc;
1591 return NULL;
1595 static __inline__ const char *
1596 dlerror (void)
1598 return w32_strerror (0);
1602 static __inline__ int
1603 dlclose (void * hd)
1605 if (hd)
1607 FreeLibrary (hd);
1608 return 0;
1610 return -1;
1613 static HRESULT
1614 w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
1616 static int initialized;
1617 static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
1619 if (!initialized)
1621 static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
1622 void *handle;
1623 int i;
1625 initialized = 1;
1627 for (i=0, handle = NULL; !handle && dllnames[i]; i++)
1629 handle = dlopen (dllnames[i], RTLD_LAZY);
1630 if (handle)
1632 func = dlsym (handle, "SHGetFolderPathW");
1633 if (!func)
1635 dlclose (handle);
1636 handle = NULL;
1642 if (func)
1643 return func (a,b,c,d,e);
1644 else
1645 return -1;
1648 /* Returns a static string with the directroy from which the module
1649 has been loaded. Returns an empty string on error. */
1650 static char *w32_get_module_dir(void)
1652 static char *moddir;
1654 if (!moddir) {
1655 char name[MAX_PATH+10];
1656 char *p;
1658 if ( !GetModuleFileNameA (0, name, sizeof (name)-10) )
1659 *name = 0;
1660 else {
1661 p = strrchr (name, '\\');
1662 if (p)
1663 *p = 0;
1664 else
1665 *name = 0;
1667 moddir = g_strdup (name);
1669 return moddir;
1671 #endif /* G_OS_WIN32 */
1673 /* Return a static string with the locale dir. */
1674 const gchar *get_locale_dir(void)
1676 static gchar *loc_dir;
1678 #ifdef G_OS_WIN32
1679 if (!loc_dir)
1680 loc_dir = g_strconcat(w32_get_module_dir(), G_DIR_SEPARATOR_S,
1681 "\\share\\locale", NULL);
1682 #endif
1683 if (!loc_dir)
1684 loc_dir = LOCALEDIR;
1686 return loc_dir;
1690 const gchar *get_home_dir(void)
1692 #ifdef G_OS_WIN32
1693 static char home_dir_utf16[MAX_PATH] = "";
1694 static gchar *home_dir_utf8 = NULL;
1695 if (home_dir_utf16[0] == '\0') {
1696 if (w32_shgetfolderpath
1697 (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
1698 NULL, 0, home_dir_utf16) < 0)
1699 strcpy (home_dir_utf16, "C:\\Claws Mail");
1700 home_dir_utf8 = g_utf16_to_utf8 ((const gunichar2 *)home_dir_utf16, -1, NULL, NULL, NULL);
1702 return home_dir_utf8;
1703 #else
1704 static const gchar *homeenv = NULL;
1706 if (homeenv)
1707 return homeenv;
1709 if (!homeenv && g_getenv("HOME") != NULL)
1710 homeenv = g_strdup(g_getenv("HOME"));
1711 if (!homeenv)
1712 homeenv = g_get_home_dir();
1714 return homeenv;
1715 #endif
1718 static gchar *claws_rc_dir = NULL;
1719 static gboolean rc_dir_alt = FALSE;
1720 const gchar *get_rc_dir(void)
1723 if (!claws_rc_dir) {
1724 claws_rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1725 RC_DIR, NULL);
1726 debug_print("using default rc_dir %s\n", claws_rc_dir);
1728 return claws_rc_dir;
1731 void set_rc_dir(const gchar *dir)
1733 gchar *canonical_dir;
1734 if (claws_rc_dir != NULL) {
1735 g_print("Error: rc_dir already set\n");
1736 } else {
1737 int err = cm_canonicalize_filename(dir, &canonical_dir);
1738 int len;
1740 if (err) {
1741 g_print("Error looking for %s: %d(%s)\n",
1742 dir, -err, g_strerror(-err));
1743 exit(0);
1745 rc_dir_alt = TRUE;
1747 claws_rc_dir = canonical_dir;
1749 len = strlen(claws_rc_dir);
1750 if (claws_rc_dir[len - 1] == G_DIR_SEPARATOR)
1751 claws_rc_dir[len - 1] = '\0';
1753 debug_print("set rc_dir to %s\n", claws_rc_dir);
1754 if (!is_dir_exist(claws_rc_dir)) {
1755 if (make_dir_hier(claws_rc_dir) != 0) {
1756 g_print("Error: can't create %s\n",
1757 claws_rc_dir);
1758 exit(0);
1764 gboolean rc_dir_is_alt(void) {
1765 return rc_dir_alt;
1768 const gchar *get_mail_base_dir(void)
1770 return get_home_dir();
1773 const gchar *get_news_cache_dir(void)
1775 static gchar *news_cache_dir = NULL;
1776 if (!news_cache_dir)
1777 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1778 NEWS_CACHE_DIR, NULL);
1780 return news_cache_dir;
1783 const gchar *get_imap_cache_dir(void)
1785 static gchar *imap_cache_dir = NULL;
1787 if (!imap_cache_dir)
1788 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1789 IMAP_CACHE_DIR, NULL);
1791 return imap_cache_dir;
1794 const gchar *get_mime_tmp_dir(void)
1796 static gchar *mime_tmp_dir = NULL;
1798 if (!mime_tmp_dir)
1799 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1800 MIME_TMP_DIR, NULL);
1802 return mime_tmp_dir;
1805 const gchar *get_template_dir(void)
1807 static gchar *template_dir = NULL;
1809 if (!template_dir)
1810 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1811 TEMPLATE_DIR, NULL);
1813 return template_dir;
1816 #ifdef G_OS_WIN32
1817 const gchar *w32_get_cert_file(void)
1819 const gchar *cert_file = NULL;
1820 if (!cert_file)
1821 cert_file = g_strconcat(w32_get_module_dir(),
1822 "\\share\\claws-mail\\",
1823 "ca-certificates.crt",
1824 NULL);
1825 return cert_file;
1827 #endif
1829 /* Return the filepath of the claws-mail.desktop file */
1830 const gchar *get_desktop_file(void)
1832 #ifdef DESKTOPFILEPATH
1833 return DESKTOPFILEPATH;
1834 #else
1835 return NULL;
1836 #endif
1839 /* Return the default directory for Plugins. */
1840 const gchar *get_plugin_dir(void)
1842 #ifdef G_OS_WIN32
1843 static gchar *plugin_dir = NULL;
1845 if (!plugin_dir)
1846 plugin_dir = g_strconcat(w32_get_module_dir(),
1847 "\\lib\\claws-mail\\plugins\\",
1848 NULL);
1849 return plugin_dir;
1850 #else
1851 if (is_dir_exist(PLUGINDIR))
1852 return PLUGINDIR;
1853 else {
1854 static gchar *plugin_dir = NULL;
1855 if (!plugin_dir)
1856 plugin_dir = g_strconcat(get_rc_dir(),
1857 G_DIR_SEPARATOR_S, "plugins",
1858 G_DIR_SEPARATOR_S, NULL);
1859 return plugin_dir;
1861 #endif
1865 #ifdef G_OS_WIN32
1866 /* Return the default directory for Themes. */
1867 const gchar *w32_get_themes_dir(void)
1869 static gchar *themes_dir = NULL;
1871 if (!themes_dir)
1872 themes_dir = g_strconcat(w32_get_module_dir(),
1873 "\\share\\claws-mail\\themes",
1874 NULL);
1875 return themes_dir;
1877 #endif
1879 const gchar *get_tmp_dir(void)
1881 static gchar *tmp_dir = NULL;
1883 if (!tmp_dir)
1884 tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1885 TMP_DIR, NULL);
1887 return tmp_dir;
1890 gchar *get_tmp_file(void)
1892 gchar *tmp_file;
1893 static guint32 id = 0;
1895 tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
1896 get_tmp_dir(), G_DIR_SEPARATOR, id++);
1898 return tmp_file;
1901 const gchar *get_domain_name(void)
1903 #ifdef G_OS_UNIX
1904 static gchar *domain_name = NULL;
1905 struct addrinfo hints, *res;
1906 char hostname[256];
1907 int s;
1909 if (!domain_name) {
1910 if (gethostname(hostname, sizeof(hostname)) != 0) {
1911 perror("gethostname");
1912 domain_name = "localhost";
1913 } else {
1914 memset(&hints, 0, sizeof(struct addrinfo));
1915 hints.ai_family = AF_UNSPEC;
1916 hints.ai_socktype = 0;
1917 hints.ai_flags = AI_CANONNAME;
1918 hints.ai_protocol = 0;
1920 s = getaddrinfo(hostname, NULL, &hints, &res);
1921 if (s != 0) {
1922 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
1923 domain_name = g_strdup(hostname);
1924 } else {
1925 domain_name = g_strdup(res->ai_canonname);
1926 freeaddrinfo(res);
1929 debug_print("domain name = %s\n", domain_name);
1932 return domain_name;
1933 #else
1934 return "localhost";
1935 #endif
1938 /* Tells whether the given host address string is a valid representation of a
1939 * numerical IP (v4 or, if supported, v6) address.
1941 gboolean is_numeric_host_address(const gchar *hostaddress)
1943 struct addrinfo hints, *res;
1944 int err;
1946 /* See what getaddrinfo makes of the string when told that it is a
1947 * numeric IP address representation. */
1948 memset(&hints, 0, sizeof(struct addrinfo));
1949 hints.ai_family = AF_UNSPEC;
1950 hints.ai_socktype = 0;
1951 hints.ai_flags = AI_NUMERICHOST;
1952 hints.ai_protocol = 0;
1954 err = getaddrinfo(hostaddress, NULL, &hints, &res);
1955 if (err == 0)
1956 freeaddrinfo(res);
1958 return (err == 0);
1961 off_t get_file_size(const gchar *file)
1963 #ifdef G_OS_WIN32
1964 GFile *f;
1965 GFileInfo *fi;
1966 GError *error = NULL;
1967 goffset size;
1969 f = g_file_new_for_path(file);
1970 fi = g_file_query_info(f, "standard::size",
1971 G_FILE_QUERY_INFO_NONE, NULL, &error);
1972 if (error != NULL) {
1973 debug_print("get_file_size error: %s\n", error->message);
1974 g_error_free(error);
1975 g_object_unref(f);
1976 return -1;
1978 size = g_file_info_get_size(fi);
1979 g_object_unref(fi);
1980 g_object_unref(f);
1981 return size;
1983 #else
1984 GStatBuf s;
1986 if (g_stat(file, &s) < 0) {
1987 FILE_OP_ERROR(file, "stat");
1988 return -1;
1991 return s.st_size;
1992 #endif
1995 time_t get_file_mtime(const gchar *file)
1997 GStatBuf s;
1999 if (g_stat(file, &s) < 0) {
2000 FILE_OP_ERROR(file, "stat");
2001 return -1;
2004 return s.st_mtime;
2007 gboolean file_exist(const gchar *file, gboolean allow_fifo)
2009 GStatBuf s;
2011 if (file == NULL)
2012 return FALSE;
2014 if (g_stat(file, &s) < 0) {
2015 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
2016 return FALSE;
2019 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
2020 return TRUE;
2022 return FALSE;
2026 /* Test on whether FILE is a relative file name. This is
2027 * straightforward for Unix but more complex for Windows. */
2028 gboolean is_relative_filename(const gchar *file)
2030 if (!file)
2031 return TRUE;
2032 #ifdef G_OS_WIN32
2033 if ( *file == '\\' && file[1] == '\\' && strchr (file+2, '\\') )
2034 return FALSE; /* Prefixed with a hostname - this can't
2035 * be a relative name. */
2037 if ( ((*file >= 'a' && *file <= 'z')
2038 || (*file >= 'A' && *file <= 'Z'))
2039 && file[1] == ':')
2040 file += 2; /* Skip drive letter. */
2042 return !(*file == '\\' || *file == '/');
2043 #else
2044 return !(*file == G_DIR_SEPARATOR);
2045 #endif
2049 gboolean is_dir_exist(const gchar *dir)
2051 if (dir == NULL)
2052 return FALSE;
2054 return g_file_test(dir, G_FILE_TEST_IS_DIR);
2057 gboolean is_file_entry_exist(const gchar *file)
2059 if (file == NULL)
2060 return FALSE;
2062 return g_file_test(file, G_FILE_TEST_EXISTS);
2065 gboolean is_file_entry_regular(const gchar *file)
2067 if (file == NULL)
2068 return FALSE;
2070 return g_file_test(file, G_FILE_TEST_IS_REGULAR);
2073 gboolean dirent_is_regular_file(struct dirent *d)
2075 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
2076 if (d->d_type == DT_REG)
2077 return TRUE;
2078 else if (d->d_type != DT_UNKNOWN)
2079 return FALSE;
2080 #endif
2082 return g_file_test(d->d_name, G_FILE_TEST_IS_REGULAR);
2085 gint change_dir(const gchar *dir)
2087 gchar *prevdir = NULL;
2089 if (debug_mode)
2090 prevdir = g_get_current_dir();
2092 if (g_chdir(dir) < 0) {
2093 FILE_OP_ERROR(dir, "chdir");
2094 if (debug_mode) g_free(prevdir);
2095 return -1;
2096 } else if (debug_mode) {
2097 gchar *cwd;
2099 cwd = g_get_current_dir();
2100 if (strcmp(prevdir, cwd) != 0)
2101 g_print("current dir: %s\n", cwd);
2102 g_free(cwd);
2103 g_free(prevdir);
2106 return 0;
2109 gint make_dir(const gchar *dir)
2111 if (g_mkdir(dir, S_IRWXU) < 0) {
2112 FILE_OP_ERROR(dir, "mkdir");
2113 return -1;
2115 if (g_chmod(dir, S_IRWXU) < 0)
2116 FILE_OP_ERROR(dir, "chmod");
2118 return 0;
2121 gint make_dir_hier(const gchar *dir)
2123 gchar *parent_dir;
2124 const gchar *p;
2126 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
2127 parent_dir = g_strndup(dir, p - dir);
2128 if (*parent_dir != '\0') {
2129 if (!is_dir_exist(parent_dir)) {
2130 if (make_dir(parent_dir) < 0) {
2131 g_free(parent_dir);
2132 return -1;
2136 g_free(parent_dir);
2139 if (!is_dir_exist(dir)) {
2140 if (make_dir(dir) < 0)
2141 return -1;
2144 return 0;
2147 gint remove_all_files(const gchar *dir)
2149 GDir *dp;
2150 const gchar *file_name;
2151 gchar *tmp;
2153 if ((dp = g_dir_open(dir, 0, NULL)) == NULL) {
2154 g_warning("failed to open directory: %s", dir);
2155 return -1;
2158 while ((file_name = g_dir_read_name(dp)) != NULL) {
2159 tmp = g_strconcat(dir, G_DIR_SEPARATOR_S, file_name, NULL);
2160 if (claws_unlink(tmp) < 0)
2161 FILE_OP_ERROR(tmp, "unlink");
2162 g_free(tmp);
2165 g_dir_close(dp);
2167 return 0;
2170 gint remove_numbered_files(const gchar *dir, guint first, guint last)
2172 GDir *dp;
2173 const gchar *dir_name;
2174 gchar *prev_dir;
2175 gint file_no;
2177 if (first == last) {
2178 /* Skip all the dir reading part. */
2179 gchar *filename = g_strdup_printf("%s%s%u", dir, G_DIR_SEPARATOR_S, first);
2180 if (is_dir_exist(filename)) {
2181 /* a numbered directory with this name exists,
2182 * remove the dot-file instead */
2183 g_free(filename);
2184 filename = g_strdup_printf("%s%s.%u", dir, G_DIR_SEPARATOR_S, first);
2186 if (claws_unlink(filename) < 0) {
2187 FILE_OP_ERROR(filename, "unlink");
2188 g_free(filename);
2189 return -1;
2191 g_free(filename);
2192 return 0;
2195 prev_dir = g_get_current_dir();
2197 if (g_chdir(dir) < 0) {
2198 FILE_OP_ERROR(dir, "chdir");
2199 g_free(prev_dir);
2200 return -1;
2203 if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
2204 g_warning("failed to open directory: %s", dir);
2205 g_free(prev_dir);
2206 return -1;
2209 while ((dir_name = g_dir_read_name(dp)) != NULL) {
2210 file_no = to_number(dir_name);
2211 if (file_no > 0 && first <= file_no && file_no <= last) {
2212 if (is_dir_exist(dir_name)) {
2213 gchar *dot_file = g_strdup_printf(".%s", dir_name);
2214 if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
2215 FILE_OP_ERROR(dot_file, "unlink");
2217 g_free(dot_file);
2218 continue;
2220 if (claws_unlink(dir_name) < 0)
2221 FILE_OP_ERROR(dir_name, "unlink");
2225 g_dir_close(dp);
2227 if (g_chdir(prev_dir) < 0) {
2228 FILE_OP_ERROR(prev_dir, "chdir");
2229 g_free(prev_dir);
2230 return -1;
2233 g_free(prev_dir);
2235 return 0;
2238 gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
2240 GDir *dp;
2241 const gchar *dir_name;
2242 gchar *prev_dir;
2243 gint file_no;
2244 GHashTable *wanted_files;
2245 GSList *cur;
2246 GError *error = NULL;
2248 if (numberlist == NULL)
2249 return 0;
2251 prev_dir = g_get_current_dir();
2253 if (g_chdir(dir) < 0) {
2254 FILE_OP_ERROR(dir, "chdir");
2255 g_free(prev_dir);
2256 return -1;
2259 if ((dp = g_dir_open(".", 0, &error)) == NULL) {
2260 g_message("Couldn't open current directory: %s (%d).\n",
2261 error->message, error->code);
2262 g_error_free(error);
2263 g_free(prev_dir);
2264 return -1;
2267 wanted_files = g_hash_table_new(g_direct_hash, g_direct_equal);
2268 for (cur = numberlist; cur != NULL; cur = cur->next) {
2269 /* numberlist->data is expected to be GINT_TO_POINTER */
2270 g_hash_table_insert(wanted_files, cur->data, GINT_TO_POINTER(1));
2273 while ((dir_name = g_dir_read_name(dp)) != NULL) {
2274 file_no = to_number(dir_name);
2275 if (is_dir_exist(dir_name))
2276 continue;
2277 if (file_no > 0 && g_hash_table_lookup(wanted_files, GINT_TO_POINTER(file_no)) == NULL) {
2278 debug_print("removing unwanted file %d from %s\n", file_no, dir);
2279 if (is_dir_exist(dir_name)) {
2280 gchar *dot_file = g_strdup_printf(".%s", dir_name);
2281 if (is_file_exist(dot_file) && claws_unlink(dot_file) < 0) {
2282 FILE_OP_ERROR(dot_file, "unlink");
2284 g_free(dot_file);
2285 continue;
2287 if (claws_unlink(dir_name) < 0)
2288 FILE_OP_ERROR(dir_name, "unlink");
2292 g_dir_close(dp);
2293 g_hash_table_destroy(wanted_files);
2295 if (g_chdir(prev_dir) < 0) {
2296 FILE_OP_ERROR(prev_dir, "chdir");
2297 g_free(prev_dir);
2298 return -1;
2301 g_free(prev_dir);
2303 return 0;
2306 gint remove_all_numbered_files(const gchar *dir)
2308 return remove_numbered_files(dir, 0, UINT_MAX);
2311 gint remove_dir_recursive(const gchar *dir)
2313 GStatBuf s;
2314 GDir *dp;
2315 const gchar *dir_name;
2316 gchar *prev_dir;
2318 if (g_stat(dir, &s) < 0) {
2319 FILE_OP_ERROR(dir, "stat");
2320 if (ENOENT == errno) return 0;
2321 return -(errno);
2324 if (!S_ISDIR(s.st_mode)) {
2325 if (claws_unlink(dir) < 0) {
2326 FILE_OP_ERROR(dir, "unlink");
2327 return -(errno);
2330 return 0;
2333 prev_dir = g_get_current_dir();
2334 /* g_print("prev_dir = %s\n", prev_dir); */
2336 if (!path_cmp(prev_dir, dir)) {
2337 g_free(prev_dir);
2338 if (g_chdir("..") < 0) {
2339 FILE_OP_ERROR(dir, "chdir");
2340 return -(errno);
2342 prev_dir = g_get_current_dir();
2345 if (g_chdir(dir) < 0) {
2346 FILE_OP_ERROR(dir, "chdir");
2347 g_free(prev_dir);
2348 return -(errno);
2351 if ((dp = g_dir_open(".", 0, NULL)) == NULL) {
2352 g_warning("failed to open directory: %s", dir);
2353 g_chdir(prev_dir);
2354 g_free(prev_dir);
2355 return -(errno);
2358 /* remove all files in the directory */
2359 while ((dir_name = g_dir_read_name(dp)) != NULL) {
2360 /* g_print("removing %s\n", dir_name); */
2362 if (is_dir_exist(dir_name)) {
2363 gint ret;
2365 if ((ret = remove_dir_recursive(dir_name)) < 0) {
2366 g_warning("can't remove directory: %s", dir_name);
2367 return ret;
2369 } else {
2370 if (claws_unlink(dir_name) < 0)
2371 FILE_OP_ERROR(dir_name, "unlink");
2375 g_dir_close(dp);
2377 if (g_chdir(prev_dir) < 0) {
2378 FILE_OP_ERROR(prev_dir, "chdir");
2379 g_free(prev_dir);
2380 return -(errno);
2383 g_free(prev_dir);
2385 if (g_rmdir(dir) < 0) {
2386 FILE_OP_ERROR(dir, "rmdir");
2387 return -(errno);
2390 return 0;
2393 /* convert line endings into CRLF. If the last line doesn't end with
2394 * linebreak, add it.
2396 gchar *canonicalize_str(const gchar *str)
2398 const gchar *p;
2399 guint new_len = 0;
2400 gchar *out, *outp;
2402 for (p = str; *p != '\0'; ++p) {
2403 if (*p != '\r') {
2404 ++new_len;
2405 if (*p == '\n')
2406 ++new_len;
2409 if (p == str || *(p - 1) != '\n')
2410 new_len += 2;
2412 out = outp = g_malloc(new_len + 1);
2413 for (p = str; *p != '\0'; ++p) {
2414 if (*p != '\r') {
2415 if (*p == '\n')
2416 *outp++ = '\r';
2417 *outp++ = *p;
2420 if (p == str || *(p - 1) != '\n') {
2421 *outp++ = '\r';
2422 *outp++ = '\n';
2424 *outp = '\0';
2426 return out;
2429 gchar *normalize_newlines(const gchar *str)
2431 const gchar *p;
2432 gchar *out, *outp;
2434 out = outp = g_malloc(strlen(str) + 1);
2435 for (p = str; *p != '\0'; ++p) {
2436 if (*p == '\r') {
2437 if (*(p + 1) != '\n')
2438 *outp++ = '\n';
2439 } else
2440 *outp++ = *p;
2443 *outp = '\0';
2445 return out;
2448 gchar *get_outgoing_rfc2822_str(FILE *fp)
2450 gchar buf[BUFFSIZE];
2451 GString *str;
2452 gchar *ret;
2454 str = g_string_new(NULL);
2456 /* output header part */
2457 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
2458 strretchomp(buf);
2459 if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
2460 gint next;
2462 for (;;) {
2463 next = fgetc(fp);
2464 if (next == EOF)
2465 break;
2466 else if (next != ' ' && next != '\t') {
2467 ungetc(next, fp);
2468 break;
2470 if (claws_fgets(buf, sizeof(buf), fp) == NULL)
2471 break;
2473 } else {
2474 g_string_append(str, buf);
2475 g_string_append(str, "\r\n");
2476 if (buf[0] == '\0')
2477 break;
2481 /* output body part */
2482 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
2483 strretchomp(buf);
2484 if (buf[0] == '.')
2485 g_string_append_c(str, '.');
2486 g_string_append(str, buf);
2487 g_string_append(str, "\r\n");
2490 ret = str->str;
2491 g_string_free(str, FALSE);
2493 return ret;
2497 * Create a new boundary in a way that it is very unlikely that this
2498 * will occur in the following text. It would be easy to ensure
2499 * uniqueness if everything is either quoted-printable or base64
2500 * encoded (note that conversion is allowed), but because MIME bodies
2501 * may be nested, it may happen that the same boundary has already
2502 * been used.
2504 * boundary := 0*69<bchars> bcharsnospace
2505 * bchars := bcharsnospace / " "
2506 * bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
2507 * "+" / "_" / "," / "-" / "." /
2508 * "/" / ":" / "=" / "?"
2510 * some special characters removed because of buggy MTAs
2513 gchar *generate_mime_boundary(const gchar *prefix)
2515 static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2516 "abcdefghijklmnopqrstuvwxyz"
2517 "1234567890+_./=";
2518 gchar buf_uniq[24];
2519 gint i;
2521 for (i = 0; i < sizeof(buf_uniq) - 1; i++)
2522 buf_uniq[i] = tbl[g_random_int_range(0, sizeof(tbl) - 1)];
2523 buf_uniq[i] = '\0';
2525 return g_strdup_printf("%s_/%s", prefix ? prefix : "MP",
2526 buf_uniq);
2529 char *fgets_crlf(char *buf, int size, FILE *stream)
2531 gboolean is_cr = FALSE;
2532 gboolean last_was_cr = FALSE;
2533 int c = 0;
2534 char *cs;
2536 cs = buf;
2537 while (--size > 0 && (c = getc(stream)) != EOF)
2539 *cs++ = c;
2540 is_cr = (c == '\r');
2541 if (c == '\n') {
2542 break;
2544 if (last_was_cr) {
2545 *(--cs) = '\n';
2546 cs++;
2547 ungetc(c, stream);
2548 break;
2550 last_was_cr = is_cr;
2552 if (c == EOF && cs == buf)
2553 return NULL;
2555 *cs = '\0';
2557 return buf;
2560 static gint execute_async(gchar *const argv[], const gchar *working_directory)
2562 cm_return_val_if_fail(argv != NULL && argv[0] != NULL, -1);
2564 if (g_spawn_async(working_directory, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH,
2565 NULL, NULL, NULL, FALSE) == FALSE) {
2566 g_warning("couldn't execute command: %s", argv[0]);
2567 return -1;
2570 return 0;
2573 static gint execute_sync(gchar *const argv[], const gchar *working_directory)
2575 gint status;
2577 cm_return_val_if_fail(argv != NULL && argv[0] != NULL, -1);
2579 #ifdef G_OS_UNIX
2580 if (g_spawn_sync(working_directory, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH,
2581 NULL, NULL, NULL, NULL, &status, NULL) == FALSE) {
2582 g_warning("couldn't execute command: %s", argv[0]);
2583 return -1;
2586 if (WIFEXITED(status))
2587 return WEXITSTATUS(status);
2588 else
2589 return -1;
2590 #else
2591 if (g_spawn_sync(working_directory, (gchar **)argv, NULL,
2592 G_SPAWN_SEARCH_PATH|
2593 G_SPAWN_CHILD_INHERITS_STDIN|
2594 G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
2595 NULL, NULL, NULL, NULL, &status, NULL) == FALSE) {
2596 g_warning("couldn't execute command: %s", argv[0]);
2597 return -1;
2600 return status;
2601 #endif
2604 gint execute_command_line(const gchar *cmdline, gboolean async,
2605 const gchar *working_directory)
2607 gchar **argv;
2608 gint ret;
2610 debug_print("execute_command_line(): executing: %s\n", cmdline?cmdline:"(null)");
2612 argv = strsplit_with_quote(cmdline, " ", 0);
2614 if (async)
2615 ret = execute_async(argv, working_directory);
2616 else
2617 ret = execute_sync(argv, working_directory);
2619 g_strfreev(argv);
2621 return ret;
2624 gchar *get_command_output(const gchar *cmdline)
2626 gchar *child_stdout;
2627 gint status;
2629 cm_return_val_if_fail(cmdline != NULL, NULL);
2631 debug_print("get_command_output(): executing: %s\n", cmdline);
2633 if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status,
2634 NULL) == FALSE) {
2635 g_warning("couldn't execute command: %s", cmdline);
2636 return NULL;
2639 return child_stdout;
2642 static gint is_unchanged_uri_char(char c)
2644 switch (c) {
2645 case '(':
2646 case ')':
2647 return 0;
2648 default:
2649 return 1;
2653 static void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
2655 int i;
2656 int k;
2658 k = 0;
2659 for(i = 0; i < strlen(uri) ; i++) {
2660 if (is_unchanged_uri_char(uri[i])) {
2661 if (k + 2 >= bufsize)
2662 break;
2663 encoded_uri[k++] = uri[i];
2665 else {
2666 char * hexa = "0123456789ABCDEF";
2668 if (k + 4 >= bufsize)
2669 break;
2670 encoded_uri[k++] = '%';
2671 encoded_uri[k++] = hexa[uri[i] / 16];
2672 encoded_uri[k++] = hexa[uri[i] % 16];
2675 encoded_uri[k] = 0;
2678 gint open_uri(const gchar *uri, const gchar *cmdline)
2681 #ifndef G_OS_WIN32
2682 gchar buf[BUFFSIZE];
2683 gchar *p;
2684 gchar encoded_uri[BUFFSIZE];
2685 cm_return_val_if_fail(uri != NULL, -1);
2687 /* an option to choose whether to use encode_uri or not ? */
2688 encode_uri(encoded_uri, BUFFSIZE, uri);
2690 if (cmdline &&
2691 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
2692 !strchr(p + 2, '%'))
2693 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
2694 else {
2695 if (cmdline)
2696 g_warning("Open URI command-line is invalid "
2697 "(there must be only one '%%s'): %s",
2698 cmdline);
2699 g_snprintf(buf, sizeof(buf), DEFAULT_BROWSER_CMD, encoded_uri);
2702 execute_command_line(buf, TRUE, NULL);
2703 #else
2704 ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOW);
2705 #endif
2706 return 0;
2709 gint open_txt_editor(const gchar *filepath, const gchar *cmdline)
2711 gchar buf[BUFFSIZE];
2712 gchar *p;
2714 cm_return_val_if_fail(filepath != NULL, -1);
2716 if (cmdline &&
2717 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
2718 !strchr(p + 2, '%'))
2719 g_snprintf(buf, sizeof(buf), cmdline, filepath);
2720 else {
2721 if (cmdline)
2722 g_warning("Open Text Editor command-line is invalid "
2723 "(there must be only one '%%s'): %s",
2724 cmdline);
2725 g_snprintf(buf, sizeof(buf), DEFAULT_EDITOR_CMD, filepath);
2728 execute_command_line(buf, TRUE, NULL);
2730 return 0;
2733 time_t remote_tzoffset_sec(const gchar *zone)
2735 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
2736 gchar zone3[4];
2737 gchar *p;
2738 gchar c;
2739 gint iustz;
2740 gint offset;
2741 time_t remoteoffset;
2743 strncpy(zone3, zone, 3);
2744 zone3[3] = '\0';
2745 remoteoffset = 0;
2747 if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
2748 (c == '+' || c == '-')) {
2749 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
2750 if (c == '-')
2751 remoteoffset = -remoteoffset;
2752 } else if (!strncmp(zone, "UT" , 2) ||
2753 !strncmp(zone, "GMT", 3)) {
2754 remoteoffset = 0;
2755 } else if (strlen(zone3) == 3) {
2756 for (p = ustzstr; *p != '\0'; p += 3) {
2757 if (!g_ascii_strncasecmp(p, zone3, 3)) {
2758 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
2759 remoteoffset = iustz * 3600;
2760 break;
2763 if (*p == '\0')
2764 return -1;
2765 } else if (strlen(zone3) == 1) {
2766 switch (zone[0]) {
2767 case 'Z': remoteoffset = 0; break;
2768 case 'A': remoteoffset = -1; break;
2769 case 'B': remoteoffset = -2; break;
2770 case 'C': remoteoffset = -3; break;
2771 case 'D': remoteoffset = -4; break;
2772 case 'E': remoteoffset = -5; break;
2773 case 'F': remoteoffset = -6; break;
2774 case 'G': remoteoffset = -7; break;
2775 case 'H': remoteoffset = -8; break;
2776 case 'I': remoteoffset = -9; break;
2777 case 'K': remoteoffset = -10; break; /* J is not used */
2778 case 'L': remoteoffset = -11; break;
2779 case 'M': remoteoffset = -12; break;
2780 case 'N': remoteoffset = 1; break;
2781 case 'O': remoteoffset = 2; break;
2782 case 'P': remoteoffset = 3; break;
2783 case 'Q': remoteoffset = 4; break;
2784 case 'R': remoteoffset = 5; break;
2785 case 'S': remoteoffset = 6; break;
2786 case 'T': remoteoffset = 7; break;
2787 case 'U': remoteoffset = 8; break;
2788 case 'V': remoteoffset = 9; break;
2789 case 'W': remoteoffset = 10; break;
2790 case 'X': remoteoffset = 11; break;
2791 case 'Y': remoteoffset = 12; break;
2792 default: remoteoffset = 0; break;
2794 remoteoffset = remoteoffset * 3600;
2795 } else
2796 return -1;
2798 return remoteoffset;
2801 time_t tzoffset_sec(time_t *now)
2803 struct tm gmt, *lt;
2804 gint off;
2805 struct tm buf1, buf2;
2806 #ifdef G_OS_WIN32
2807 if (now && *now < 0)
2808 return 0;
2809 #endif
2810 gmt = *gmtime_r(now, &buf1);
2811 lt = localtime_r(now, &buf2);
2813 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2815 if (lt->tm_year < gmt.tm_year)
2816 off -= 24 * 60;
2817 else if (lt->tm_year > gmt.tm_year)
2818 off += 24 * 60;
2819 else if (lt->tm_yday < gmt.tm_yday)
2820 off -= 24 * 60;
2821 else if (lt->tm_yday > gmt.tm_yday)
2822 off += 24 * 60;
2824 if (off >= 24 * 60) /* should be impossible */
2825 off = 23 * 60 + 59; /* if not, insert silly value */
2826 if (off <= -24 * 60)
2827 off = -(23 * 60 + 59);
2829 return off * 60;
2832 /* calculate timezone offset */
2833 gchar *tzoffset(time_t *now)
2835 static gchar offset_string[6];
2836 struct tm gmt, *lt;
2837 gint off;
2838 gchar sign = '+';
2839 struct tm buf1, buf2;
2840 #ifdef G_OS_WIN32
2841 if (now && *now < 0)
2842 return 0;
2843 #endif
2844 gmt = *gmtime_r(now, &buf1);
2845 lt = localtime_r(now, &buf2);
2847 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2849 if (lt->tm_year < gmt.tm_year)
2850 off -= 24 * 60;
2851 else if (lt->tm_year > gmt.tm_year)
2852 off += 24 * 60;
2853 else if (lt->tm_yday < gmt.tm_yday)
2854 off -= 24 * 60;
2855 else if (lt->tm_yday > gmt.tm_yday)
2856 off += 24 * 60;
2858 if (off < 0) {
2859 sign = '-';
2860 off = -off;
2863 if (off >= 24 * 60) /* should be impossible */
2864 off = 23 * 60 + 59; /* if not, insert silly value */
2866 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
2868 return offset_string;
2871 static void _get_rfc822_date(gchar *buf, gint len, gboolean hidetz)
2873 struct tm *lt;
2874 time_t t;
2875 gchar day[4], mon[4];
2876 gint dd, hh, mm, ss, yyyy;
2877 struct tm buf1;
2878 gchar buf2[RFC822_DATE_BUFFSIZE];
2880 t = time(NULL);
2881 if (hidetz)
2882 lt = gmtime_r(&t, &buf1);
2883 else
2884 lt = localtime_r(&t, &buf1);
2886 if (sscanf(asctime_r(lt, buf2), "%3s %3s %d %d:%d:%d %d\n",
2887 day, mon, &dd, &hh, &mm, &ss, &yyyy) != 7)
2888 g_warning("failed reading date/time");
2890 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2891 day, dd, mon, yyyy, hh, mm, ss, (hidetz? "-0000": tzoffset(&t)));
2894 void get_rfc822_date(gchar *buf, gint len)
2896 _get_rfc822_date(buf, len, FALSE);
2899 void get_rfc822_date_hide_tz(gchar *buf, gint len)
2901 _get_rfc822_date(buf, len, TRUE);
2904 void debug_set_mode(gboolean mode)
2906 debug_mode = mode;
2909 gboolean debug_get_mode(void)
2911 return debug_mode;
2914 void debug_print_real(const gchar *format, ...)
2916 va_list args;
2917 gchar buf[BUFFSIZE];
2919 if (!debug_mode) return;
2921 va_start(args, format);
2922 g_vsnprintf(buf, sizeof(buf), format, args);
2923 va_end(args);
2925 g_print("%s", buf);
2929 const char * debug_srcname(const char *file)
2931 const char *s = strrchr (file, '/');
2932 return s? s+1:file;
2936 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
2938 if (subject == NULL)
2939 subject = "";
2940 else
2941 subject += subject_get_prefix_length(subject);
2943 return g_hash_table_lookup(subject_table, subject);
2946 void subject_table_insert(GHashTable *subject_table, gchar * subject,
2947 void * data)
2949 if (subject == NULL || *subject == 0)
2950 return;
2951 subject += subject_get_prefix_length(subject);
2952 g_hash_table_insert(subject_table, subject, data);
2955 void subject_table_remove(GHashTable *subject_table, gchar * subject)
2957 if (subject == NULL)
2958 return;
2960 subject += subject_get_prefix_length(subject);
2961 g_hash_table_remove(subject_table, subject);
2964 static regex_t u_regex;
2965 static gboolean u_init_;
2967 void utils_free_regex(void)
2969 if (u_init_) {
2970 regfree(&u_regex);
2971 u_init_ = FALSE;
2976 *\brief Check if a string is prefixed with known (combinations)
2977 * of prefixes. The function assumes that each prefix
2978 * is terminated by zero or exactly _one_ space.
2980 *\param str String to check for a prefixes
2982 *\return int Number of chars in the prefix that should be skipped
2983 * for a "clean" subject line. If no prefix was found, 0
2984 * is returned.
2986 int subject_get_prefix_length(const gchar *subject)
2988 /*!< Array with allowable reply prefixes regexps. */
2989 static const gchar * const prefixes[] = {
2990 "Re\\:", /* "Re:" */
2991 "Re\\[[1-9][0-9]*\\]\\:", /* "Re[XXX]:" (non-conforming news mail clients) */
2992 "Antw\\:", /* "Antw:" (Dutch / German Outlook) */
2993 "Aw\\:", /* "Aw:" (German) */
2994 "Antwort\\:", /* "Antwort:" (German Lotus Notes) */
2995 "Res\\:", /* "Res:" (Spanish/Brazilian Outlook) */
2996 "Fw\\:", /* "Fw:" Forward */
2997 "Fwd\\:", /* "Fwd:" Forward */
2998 "Enc\\:", /* "Enc:" Forward (Brazilian Outlook) */
2999 "Odp\\:", /* "Odp:" Re (Polish Outlook) */
3000 "Rif\\:", /* "Rif:" (Italian Outlook) */
3001 "Sv\\:", /* "Sv" (Norwegian) */
3002 "Vs\\:", /* "Vs" (Norwegian) */
3003 "Ad\\:", /* "Ad" (Norwegian) */
3004 "\347\255\224\345\244\215\\:", /* "Re" (Chinese, UTF-8) */
3005 "R\303\251f\\. \\:", /* "R�f. :" (French Lotus Notes) */
3006 "Re \\:", /* "Re :" (French Yahoo Mail) */
3007 /* add more */
3009 const int PREFIXES = sizeof prefixes / sizeof prefixes[0];
3010 int n;
3011 regmatch_t pos;
3013 if (!subject) return 0;
3014 if (!*subject) return 0;
3016 if (!u_init_) {
3017 GString *s = g_string_new("");
3019 for (n = 0; n < PREFIXES; n++)
3020 /* Terminate each prefix regexpression by a
3021 * "\ ?" (zero or ONE space), and OR them */
3022 g_string_append_printf(s, "(%s\\ ?)%s",
3023 prefixes[n],
3024 n < PREFIXES - 1 ?
3025 "|" : "");
3027 g_string_prepend(s, "(");
3028 g_string_append(s, ")+"); /* match at least once */
3029 g_string_prepend(s, "^\\ *"); /* from beginning of line */
3032 /* We now have something like "^\ *((PREFIX1\ ?)|(PREFIX2\ ?))+"
3033 * TODO: Should this be "^\ *(((PREFIX1)|(PREFIX2))\ ?)+" ??? */
3034 if (regcomp(&u_regex, s->str, REG_EXTENDED | REG_ICASE)) {
3035 debug_print("Error compiling regexp %s\n", s->str);
3036 g_string_free(s, TRUE);
3037 return 0;
3038 } else {
3039 u_init_ = TRUE;
3040 g_string_free(s, TRUE);
3044 if (!regexec(&u_regex, subject, 1, &pos, 0) && pos.rm_so != -1)
3045 return pos.rm_eo;
3046 else
3047 return 0;
3050 static guint g_stricase_hash(gconstpointer gptr)
3052 guint hash_result = 0;
3053 const char *str;
3055 for (str = gptr; str && *str; str++) {
3056 hash_result += toupper(*str);
3059 return hash_result;
3062 static gint g_stricase_equal(gconstpointer gptr1, gconstpointer gptr2)
3064 const char *str1 = gptr1;
3065 const char *str2 = gptr2;
3067 return !strcasecmp(str1, str2);
3070 gint g_int_compare(gconstpointer a, gconstpointer b)
3072 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
3076 quote_cmd_argument()
3078 return a quoted string safely usable in argument of a command.
3080 code is extracted and adapted from etPan! project -- DINH V. Ho�.
3083 gint quote_cmd_argument(gchar * result, guint size,
3084 const gchar * path)
3086 const gchar * p;
3087 gchar * result_p;
3088 guint remaining;
3090 result_p = result;
3091 remaining = size;
3093 for(p = path ; * p != '\0' ; p ++) {
3095 if (isalnum((guchar)*p) || (* p == '/')) {
3096 if (remaining > 0) {
3097 * result_p = * p;
3098 result_p ++;
3099 remaining --;
3101 else {
3102 result[size - 1] = '\0';
3103 return -1;
3106 else {
3107 if (remaining >= 2) {
3108 * result_p = '\\';
3109 result_p ++;
3110 * result_p = * p;
3111 result_p ++;
3112 remaining -= 2;
3114 else {
3115 result[size - 1] = '\0';
3116 return -1;
3120 if (remaining > 0) {
3121 * result_p = '\0';
3123 else {
3124 result[size - 1] = '\0';
3125 return -1;
3128 return 0;
3131 typedef struct
3133 GNode *parent;
3134 GNodeMapFunc func;
3135 gpointer data;
3136 } GNodeMapData;
3138 static void g_node_map_recursive(GNode *node, gpointer data)
3140 GNodeMapData *mapdata = (GNodeMapData *) data;
3141 GNode *newnode;
3142 GNodeMapData newmapdata;
3143 gpointer newdata;
3145 newdata = mapdata->func(node->data, mapdata->data);
3146 if (newdata != NULL) {
3147 newnode = g_node_new(newdata);
3148 g_node_append(mapdata->parent, newnode);
3150 newmapdata.parent = newnode;
3151 newmapdata.func = mapdata->func;
3152 newmapdata.data = mapdata->data;
3154 g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &newmapdata);
3158 GNode *g_node_map(GNode *node, GNodeMapFunc func, gpointer data)
3160 GNode *root;
3161 GNodeMapData mapdata;
3163 cm_return_val_if_fail(node != NULL, NULL);
3164 cm_return_val_if_fail(func != NULL, NULL);
3166 root = g_node_new(func(node->data, data));
3168 mapdata.parent = root;
3169 mapdata.func = func;
3170 mapdata.data = data;
3172 g_node_children_foreach(node, G_TRAVERSE_ALL, g_node_map_recursive, &mapdata);
3174 return root;
3177 #define HEX_TO_INT(val, hex) \
3179 gchar c = hex; \
3181 if ('0' <= c && c <= '9') { \
3182 val = c - '0'; \
3183 } else if ('a' <= c && c <= 'f') { \
3184 val = c - 'a' + 10; \
3185 } else if ('A' <= c && c <= 'F') { \
3186 val = c - 'A' + 10; \
3187 } else { \
3188 val = -1; \
3192 gboolean get_hex_value(guchar *out, gchar c1, gchar c2)
3194 gint hi, lo;
3196 HEX_TO_INT(hi, c1);
3197 HEX_TO_INT(lo, c2);
3199 if (hi == -1 || lo == -1)
3200 return FALSE;
3202 *out = (hi << 4) + lo;
3203 return TRUE;
3206 #define INT_TO_HEX(hex, val) \
3208 if ((val) < 10) \
3209 hex = '0' + (val); \
3210 else \
3211 hex = 'A' + (val) - 10; \
3214 void get_hex_str(gchar *out, guchar ch)
3216 gchar hex;
3218 INT_TO_HEX(hex, ch >> 4);
3219 *out++ = hex;
3220 INT_TO_HEX(hex, ch & 0x0f);
3221 *out = hex;
3224 #undef REF_DEBUG
3225 #ifndef REF_DEBUG
3226 #define G_PRINT_REF 1 == 1 ? (void) 0 : (void)
3227 #else
3228 #define G_PRINT_REF g_print
3229 #endif
3232 *\brief Register ref counted pointer. It is based on GBoxed, so should
3233 * work with anything that uses the GType system. The semantics
3234 * are similar to a C++ auto pointer, with the exception that
3235 * C doesn't have automatic closure (calling destructors) when
3236 * exiting a block scope.
3237 * Use the \ref G_TYPE_AUTO_POINTER macro instead of calling this
3238 * function directly.
3240 *\return GType A GType type.
3242 GType g_auto_pointer_register(void)
3244 static GType auto_pointer_type;
3245 if (!auto_pointer_type)
3246 auto_pointer_type =
3247 g_boxed_type_register_static
3248 ("G_TYPE_AUTO_POINTER",
3249 (GBoxedCopyFunc) g_auto_pointer_copy,
3250 (GBoxedFreeFunc) g_auto_pointer_free);
3251 return auto_pointer_type;
3255 *\brief Structure with g_new() allocated pointer guarded by the
3256 * auto pointer
3258 typedef struct AutoPointerRef {
3259 void (*free) (gpointer);
3260 gpointer pointer;
3261 glong cnt;
3262 } AutoPointerRef;
3265 *\brief The auto pointer opaque structure that references the
3266 * pointer guard block.
3268 typedef struct AutoPointer {
3269 AutoPointerRef *ref;
3270 gpointer ptr; /*!< access to protected pointer */
3271 } AutoPointer;
3274 *\brief Creates an auto pointer for a g_new()ed pointer. Example:
3276 *\code
3278 * ... tell gtk_list_store it should use a G_TYPE_AUTO_POINTER
3279 * ... when assigning, copying and freeing storage elements
3281 * gtk_list_store_new(N_S_COLUMNS,
3282 * G_TYPE_AUTO_POINTER,
3283 * -1);
3286 * Template *precious_data = g_new0(Template, 1);
3287 * g_pointer protect = g_auto_pointer_new(precious_data);
3289 * gtk_list_store_set(container, &iter,
3290 * S_DATA, protect,
3291 * -1);
3293 * ... the gtk_list_store has copied the pointer and
3294 * ... incremented its reference count, we should free
3295 * ... the auto pointer (in C++ a destructor would do
3296 * ... this for us when leaving block scope)
3298 * g_auto_pointer_free(protect);
3300 * ... gtk_list_store_set() now manages the data. When
3301 * ... *explicitly* requesting a pointer from the list
3302 * ... store, don't forget you get a copy that should be
3303 * ... freed with g_auto_pointer_free() eventually.
3305 *\endcode
3307 *\param pointer Pointer to be guarded.
3309 *\return GAuto * Pointer that should be used in containers with
3310 * GType support.
3312 GAuto *g_auto_pointer_new(gpointer p)
3314 AutoPointerRef *ref;
3315 AutoPointer *ptr;
3317 if (p == NULL)
3318 return NULL;
3320 ref = g_new0(AutoPointerRef, 1);
3321 ptr = g_new0(AutoPointer, 1);
3323 ref->pointer = p;
3324 ref->free = g_free;
3325 ref->cnt = 1;
3327 ptr->ref = ref;
3328 ptr->ptr = p;
3330 #ifdef REF_DEBUG
3331 G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
3332 #endif
3333 return ptr;
3337 *\brief Allocate an autopointer using the passed \a free function to
3338 * free the guarded pointer
3340 GAuto *g_auto_pointer_new_with_free(gpointer p, GFreeFunc free_)
3342 AutoPointer *aptr;
3344 if (p == NULL)
3345 return NULL;
3347 aptr = g_auto_pointer_new(p);
3348 aptr->ref->free = free_;
3349 return aptr;
3352 gpointer g_auto_pointer_get_ptr(GAuto *auto_ptr)
3354 if (auto_ptr == NULL)
3355 return NULL;
3356 return ((AutoPointer *) auto_ptr)->ptr;
3360 *\brief Copies an auto pointer by. It's mostly not necessary
3361 * to call this function directly, unless you copy/assign
3362 * the guarded pointer.
3364 *\param auto_ptr Auto pointer returned by previous call to
3365 * g_auto_pointer_new_XXX()
3367 *\return gpointer An auto pointer
3369 GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
3371 AutoPointer *ptr;
3372 AutoPointerRef *ref;
3373 AutoPointer *newp;
3375 if (auto_ptr == NULL)
3376 return NULL;
3378 ptr = auto_ptr;
3379 ref = ptr->ref;
3380 newp = g_new0(AutoPointer, 1);
3382 newp->ref = ref;
3383 newp->ptr = ref->pointer;
3384 ++(ref->cnt);
3386 #ifdef REF_DEBUG
3387 G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3388 #endif
3389 return newp;
3393 *\brief Free an auto pointer
3395 void g_auto_pointer_free(GAuto *auto_ptr)
3397 AutoPointer *ptr;
3398 AutoPointerRef *ref;
3400 if (auto_ptr == NULL)
3401 return;
3403 ptr = auto_ptr;
3404 ref = ptr->ref;
3406 if (--(ref->cnt) == 0) {
3407 #ifdef REF_DEBUG
3408 G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3409 #endif
3410 ref->free(ref->pointer);
3411 g_free(ref);
3413 #ifdef REF_DEBUG
3414 else
3415 G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
3416 #endif
3417 g_free(ptr);
3420 void replace_returns(gchar *str)
3422 if (!str)
3423 return;
3425 while (strstr(str, "\n")) {
3426 *strstr(str, "\n") = ' ';
3428 while (strstr(str, "\r")) {
3429 *strstr(str, "\r") = ' ';
3433 /* get_uri_part() - retrieves a URI starting from scanpos.
3434 Returns TRUE if successful */
3435 gboolean get_uri_part(const gchar *start, const gchar *scanpos,
3436 const gchar **bp, const gchar **ep, gboolean hdr)
3438 const gchar *ep_;
3439 gint parenthese_cnt = 0;
3441 cm_return_val_if_fail(start != NULL, FALSE);
3442 cm_return_val_if_fail(scanpos != NULL, FALSE);
3443 cm_return_val_if_fail(bp != NULL, FALSE);
3444 cm_return_val_if_fail(ep != NULL, FALSE);
3446 *bp = scanpos;
3448 /* find end point of URI */
3449 for (ep_ = scanpos; *ep_ != '\0'; ep_ = g_utf8_next_char(ep_)) {
3450 gunichar u = g_utf8_get_char_validated(ep_, -1);
3451 if (!g_unichar_isgraph(u) ||
3452 u == (gunichar)-1 ||
3453 strchr("[]{}<>\"", *ep_)) {
3454 break;
3455 } else if (strchr("(", *ep_)) {
3456 parenthese_cnt++;
3457 } else if (strchr(")", *ep_)) {
3458 if (parenthese_cnt > 0)
3459 parenthese_cnt--;
3460 else
3461 break;
3465 /* no punctuation at end of string */
3467 /* FIXME: this stripping of trailing punctuations may bite with other URIs.
3468 * should pass some URI type to this function and decide on that whether
3469 * to perform punctuation stripping */
3471 #define IS_REAL_PUNCT(ch) (g_ascii_ispunct(ch) && !strchr("/?=-_~)", ch))
3473 for (; ep_ - 1 > scanpos + 1 &&
3474 IS_REAL_PUNCT(*(ep_ - 1));
3475 ep_--)
3478 #undef IS_REAL_PUNCT
3480 *ep = ep_;
3482 return TRUE;
3485 gchar *make_uri_string(const gchar *bp, const gchar *ep)
3487 while (bp && *bp && g_ascii_isspace(*bp))
3488 bp++;
3489 return g_strndup(bp, ep - bp);
3492 /* valid mail address characters */
3493 #define IS_RFC822_CHAR(ch) \
3494 (IS_ASCII(ch) && \
3495 (ch) > 32 && \
3496 (ch) != 127 && \
3497 !g_ascii_isspace(ch) && \
3498 !strchr("(),;<>\"", (ch)))
3500 /* alphabet and number within 7bit ASCII */
3501 #define IS_ASCII_ALNUM(ch) (IS_ASCII(ch) && g_ascii_isalnum(ch))
3502 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
3504 static GHashTable *create_domain_tab(void)
3506 gint n;
3507 GHashTable *htab = g_hash_table_new(g_stricase_hash, g_stricase_equal);
3509 cm_return_val_if_fail(htab, NULL);
3510 for (n = 0; n < sizeof toplvl_domains / sizeof toplvl_domains[0]; n++)
3511 g_hash_table_insert(htab, (gpointer) toplvl_domains[n], (gpointer) toplvl_domains[n]);
3512 return htab;
3515 static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
3517 gchar buf[BUFFSIZE + 1];
3518 const gchar *m = buf + BUFFSIZE + 1;
3519 register gchar *p;
3521 if (last - first > BUFFSIZE || first > last)
3522 return FALSE;
3524 for (p = buf; p < m && first < last; *p++ = *first++)
3526 *p = 0;
3528 return g_hash_table_lookup(tab, buf) != NULL;
3531 /* get_email_part() - retrieves an email address. Returns TRUE if successful */
3532 gboolean get_email_part(const gchar *start, const gchar *scanpos,
3533 const gchar **bp, const gchar **ep, gboolean hdr)
3535 /* more complex than the uri part because we need to scan back and forward starting from
3536 * the scan position. */
3537 gboolean result = FALSE;
3538 const gchar *bp_ = NULL;
3539 const gchar *ep_ = NULL;
3540 static GHashTable *dom_tab;
3541 const gchar *last_dot = NULL;
3542 const gchar *prelast_dot = NULL;
3543 const gchar *last_tld_char = NULL;
3545 /* the informative part of the email address (describing the name
3546 * of the email address owner) may contain quoted parts. the
3547 * closure stack stores the last encountered quotes. */
3548 gchar closure_stack[128];
3549 gchar *ptr = closure_stack;
3551 cm_return_val_if_fail(start != NULL, FALSE);
3552 cm_return_val_if_fail(scanpos != NULL, FALSE);
3553 cm_return_val_if_fail(bp != NULL, FALSE);
3554 cm_return_val_if_fail(ep != NULL, FALSE);
3556 if (hdr) {
3557 const gchar *start_quote = NULL;
3558 const gchar *end_quote = NULL;
3559 search_again:
3560 /* go to the real start */
3561 if (start[0] == ',')
3562 start++;
3563 if (start[0] == ';')
3564 start++;
3565 while (start[0] == '\n' || start[0] == '\r')
3566 start++;
3567 while (start[0] == ' ' || start[0] == '\t')
3568 start++;
3570 *bp = start;
3572 /* check if there are quotes (to skip , in them) */
3573 if (*start == '"') {
3574 start_quote = start;
3575 start++;
3576 end_quote = strstr(start, "\"");
3577 } else {
3578 start_quote = NULL;
3579 end_quote = NULL;
3582 /* skip anything between quotes */
3583 if (start_quote && end_quote) {
3584 start = end_quote;
3588 /* find end (either , or ; or end of line) */
3589 if (strstr(start, ",") && strstr(start, ";"))
3590 *ep = strstr(start,",") < strstr(start, ";")
3591 ? strstr(start, ",") : strstr(start, ";");
3592 else if (strstr(start, ","))
3593 *ep = strstr(start, ",");
3594 else if (strstr(start, ";"))
3595 *ep = strstr(start, ";");
3596 else
3597 *ep = start+strlen(start);
3599 /* go back to real start */
3600 if (start_quote && end_quote) {
3601 start = start_quote;
3604 /* check there's still an @ in that, or search
3605 * further if possible */
3606 if (strstr(start, "@") && strstr(start, "@") < *ep)
3607 return TRUE;
3608 else if (*ep < start+strlen(start)) {
3609 start = *ep;
3610 goto search_again;
3611 } else if (start_quote && strstr(start, "\"") && strstr(start, "\"") < *ep) {
3612 *bp = start_quote;
3613 return TRUE;
3614 } else
3615 return FALSE;
3618 if (!dom_tab)
3619 dom_tab = create_domain_tab();
3620 cm_return_val_if_fail(dom_tab, FALSE);
3622 /* scan start of address */
3623 for (bp_ = scanpos - 1;
3624 bp_ >= start && IS_RFC822_CHAR(*(const guchar *)bp_); bp_--)
3627 /* TODO: should start with an alnum? */
3628 bp_++;
3629 for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
3632 if (bp_ != scanpos) {
3633 /* scan end of address */
3634 for (ep_ = scanpos + 1;
3635 *ep_ && IS_RFC822_CHAR(*(const guchar *)ep_); ep_++)
3636 if (*ep_ == '.') {
3637 prelast_dot = last_dot;
3638 last_dot = ep_;
3639 if (*(last_dot + 1) == '.') {
3640 if (prelast_dot == NULL)
3641 return FALSE;
3642 last_dot = prelast_dot;
3643 break;
3647 /* TODO: really should terminate with an alnum? */
3648 for (; ep_ > scanpos && !IS_ASCII_ALNUM(*(const guchar *)ep_);
3649 --ep_)
3651 ep_++;
3653 if (last_dot == NULL)
3654 return FALSE;
3655 if (last_dot >= ep_)
3656 last_dot = prelast_dot;
3657 if (last_dot == NULL || (scanpos + 1 >= last_dot))
3658 return FALSE;
3659 last_dot++;
3661 for (last_tld_char = last_dot; last_tld_char < ep_; last_tld_char++)
3662 if (*last_tld_char == '?')
3663 break;
3665 if (is_toplvl_domain(dom_tab, last_dot, last_tld_char))
3666 result = TRUE;
3668 *ep = ep_;
3669 *bp = bp_;
3672 if (!result) return FALSE;
3674 if (*ep_ && bp_ != start && *(bp_ - 1) == '"' && *(ep_) == '"'
3675 && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
3676 && IS_RFC822_CHAR(*(ep_ + 3))) {
3677 /* this informative part with an @ in it is
3678 * followed by the email address */
3679 ep_ += 3;
3681 /* go to matching '>' (or next non-rfc822 char, like \n) */
3682 for (; *ep_ != '>' && *ep_ != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
3685 /* include the bracket */
3686 if (*ep_ == '>') ep_++;
3688 /* include the leading quote */
3689 bp_--;
3691 *ep = ep_;
3692 *bp = bp_;
3693 return TRUE;
3696 /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
3697 if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
3698 return FALSE;
3700 /* see if this is <bracketed>; in this case we also scan for the informative part. */
3701 if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
3702 return TRUE;
3704 #define FULL_STACK() ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
3705 #define IN_STACK() (ptr > closure_stack)
3706 /* has underrun check */
3707 #define POP_STACK() if(IN_STACK()) --ptr
3708 /* has overrun check */
3709 #define PUSH_STACK(c) if(!FULL_STACK()) *ptr++ = (c); else return TRUE
3710 /* has underrun check */
3711 #define PEEK_STACK() (IN_STACK() ? *(ptr - 1) : 0)
3713 ep_++;
3715 /* scan for the informative part. */
3716 for (bp_ -= 2; bp_ >= start; bp_--) {
3717 /* if closure on the stack keep scanning */
3718 if (PEEK_STACK() == *bp_) {
3719 POP_STACK();
3720 continue;
3722 if (!IN_STACK() && (*bp_ == '\'' || *bp_ == '"')) {
3723 PUSH_STACK(*bp_);
3724 continue;
3727 /* if nothing in the closure stack, do the special conditions
3728 * the following if..else expression simply checks whether
3729 * a token is acceptable. if not acceptable, the clause
3730 * should terminate the loop with a 'break' */
3731 if (!PEEK_STACK()) {
3732 if (*bp_ == '-'
3733 && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
3734 && (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1)))) {
3735 /* hyphens are allowed, but only in
3736 between alnums */
3737 } else if (strchr(" \"'", *bp_)) {
3738 /* but anything not being a punctiation
3739 is ok */
3740 } else {
3741 break; /* anything else is rejected */
3746 bp_++;
3748 /* scan forward (should start with an alnum) */
3749 for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
3751 #undef PEEK_STACK
3752 #undef PUSH_STACK
3753 #undef POP_STACK
3754 #undef IN_STACK
3755 #undef FULL_STACK
3758 *bp = bp_;
3759 *ep = ep_;
3761 return result;
3764 #undef IS_QUOTE
3765 #undef IS_ASCII_ALNUM
3766 #undef IS_RFC822_CHAR
3768 gchar *make_email_string(const gchar *bp, const gchar *ep)
3770 /* returns a mailto: URI; mailto: is also used to detect the
3771 * uri type later on in the button_pressed signal handler */
3772 gchar *tmp;
3773 gchar *result;
3774 gchar *colon, *at;
3776 tmp = g_strndup(bp, ep - bp);
3778 /* If there is a colon in the username part of the address,
3779 * we're dealing with an URI for some other protocol - do
3780 * not prefix with mailto: in such case. */
3781 colon = strchr(tmp, ':');
3782 at = strchr(tmp, '@');
3783 if (colon != NULL && at != NULL && colon < at) {
3784 result = tmp;
3785 } else {
3786 result = g_strconcat("mailto:", tmp, NULL);
3787 g_free(tmp);
3790 return result;
3793 gchar *make_http_string(const gchar *bp, const gchar *ep)
3795 /* returns an http: URI; */
3796 gchar *tmp;
3797 gchar *result;
3799 while (bp && *bp && g_ascii_isspace(*bp))
3800 bp++;
3801 tmp = g_strndup(bp, ep - bp);
3802 result = g_strconcat("http://", tmp, NULL);
3803 g_free(tmp);
3805 return result;
3808 static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type, const gchar *file_to_open)
3810 FILE *fp = claws_fopen(path, "rb");
3811 gchar buf[BUFFSIZE];
3812 gchar *result = NULL;
3813 if (!fp)
3814 return NULL;
3815 while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
3816 gchar **parts = g_strsplit(buf, ";", 3);
3817 gchar *trimmed = parts[0];
3818 while (trimmed[0] == ' ' || trimmed[0] == '\t')
3819 trimmed++;
3820 while (trimmed[strlen(trimmed)-1] == ' ' || trimmed[strlen(trimmed)-1] == '\t')
3821 trimmed[strlen(trimmed)-1] = '\0';
3823 if (!strcmp(trimmed, type)) {
3824 gboolean needsterminal = FALSE;
3825 if (parts[2] && strstr(parts[2], "needsterminal")) {
3826 needsterminal = TRUE;
3828 if (parts[2] && strstr(parts[2], "test=")) {
3829 gchar *orig_testcmd = g_strdup(strstr(parts[2], "test=")+5);
3830 gchar *testcmd = orig_testcmd;
3831 if (strstr(testcmd,";"))
3832 *(strstr(testcmd,";")) = '\0';
3833 while (testcmd[0] == ' ' || testcmd[0] == '\t')
3834 testcmd++;
3835 while (testcmd[strlen(testcmd)-1] == '\n')
3836 testcmd[strlen(testcmd)-1] = '\0';
3837 while (testcmd[strlen(testcmd)-1] == '\r')
3838 testcmd[strlen(testcmd)-1] = '\0';
3839 while (testcmd[strlen(testcmd)-1] == ' ' || testcmd[strlen(testcmd)-1] == '\t')
3840 testcmd[strlen(testcmd)-1] = '\0';
3842 if (strstr(testcmd, "%s")) {
3843 gchar *tmp = g_strdup_printf(testcmd, file_to_open);
3844 gint res = system(tmp);
3845 g_free(tmp);
3846 g_free(orig_testcmd);
3848 if (res != 0) {
3849 g_strfreev(parts);
3850 continue;
3852 } else {
3853 gint res = system(testcmd);
3854 g_free(orig_testcmd);
3856 if (res != 0) {
3857 g_strfreev(parts);
3858 continue;
3863 trimmed = parts[1];
3864 while (trimmed[0] == ' ' || trimmed[0] == '\t')
3865 trimmed++;
3866 while (trimmed[strlen(trimmed)-1] == '\n')
3867 trimmed[strlen(trimmed)-1] = '\0';
3868 while (trimmed[strlen(trimmed)-1] == '\r')
3869 trimmed[strlen(trimmed)-1] = '\0';
3870 while (trimmed[strlen(trimmed)-1] == ' ' || trimmed[strlen(trimmed)-1] == '\t')
3871 trimmed[strlen(trimmed)-1] = '\0';
3872 result = g_strdup(trimmed);
3873 g_strfreev(parts);
3874 claws_fclose(fp);
3875 if (needsterminal) {
3876 gchar *tmp = g_strdup_printf("xterm -e %s", result);
3877 g_free(result);
3878 result = tmp;
3880 return result;
3882 g_strfreev(parts);
3884 claws_fclose(fp);
3885 return NULL;
3887 gchar *mailcap_get_command_for_type(const gchar *type, const gchar *file_to_open)
3889 gchar *result = NULL;
3890 gchar *path = NULL;
3891 if (type == NULL)
3892 return NULL;
3893 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
3894 result = mailcap_get_command_in_file(path, type, file_to_open);
3895 g_free(path);
3896 if (result)
3897 return result;
3898 result = mailcap_get_command_in_file("/etc/mailcap", type, file_to_open);
3899 return result;
3902 void mailcap_update_default(const gchar *type, const gchar *command)
3904 gchar *path = NULL, *outpath = NULL;
3905 path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
3906 outpath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap.new", NULL);
3907 FILE *fp = claws_fopen(path, "rb");
3908 FILE *outfp = NULL;
3909 gchar buf[BUFFSIZE];
3910 gboolean err = FALSE;
3912 if (!fp) {
3913 fp = claws_fopen(path, "a");
3914 if (!fp) {
3915 g_warning("failed to create file %s", path);
3916 g_free(path);
3917 g_free(outpath);
3918 return;
3920 fp = g_freopen(path, "rb", fp);
3921 if (!fp) {
3922 g_warning("failed to reopen file %s", path);
3923 g_free(path);
3924 g_free(outpath);
3925 return;
3929 outfp = claws_fopen(outpath, "wb");
3930 if (!outfp) {
3931 g_warning("failed to create file %s", outpath);
3932 g_free(path);
3933 g_free(outpath);
3934 claws_fclose(fp);
3935 return;
3937 while (fp && claws_fgets(buf, sizeof (buf), fp) != NULL) {
3938 gchar **parts = g_strsplit(buf, ";", 3);
3939 gchar *trimmed = parts[0];
3940 while (trimmed[0] == ' ')
3941 trimmed++;
3942 while (trimmed[strlen(trimmed)-1] == ' ')
3943 trimmed[strlen(trimmed)-1] = '\0';
3945 if (!strcmp(trimmed, type)) {
3946 g_strfreev(parts);
3947 continue;
3949 else {
3950 if(claws_fputs(buf, outfp) == EOF) {
3951 err = TRUE;
3952 break;
3955 g_strfreev(parts);
3957 if (fprintf(outfp, "%s; %s\n", type, command) < 0)
3958 err = TRUE;
3960 if (fp)
3961 claws_fclose(fp);
3963 if (claws_safe_fclose(outfp) == EOF)
3964 err = TRUE;
3966 if (!err)
3967 g_rename(outpath, path);
3969 g_free(path);
3970 g_free(outpath);
3973 /* crude test to see if a file is an email. */
3974 gboolean file_is_email (const gchar *filename)
3976 FILE *fp = NULL;
3977 gchar buffer[2048];
3978 gint i = 0;
3979 gint score = 0;
3980 if (filename == NULL)
3981 return FALSE;
3982 if ((fp = claws_fopen(filename, "rb")) == NULL)
3983 return FALSE;
3984 while (i < 60 && score < 3
3985 && claws_fgets(buffer, sizeof (buffer), fp) != NULL) {
3986 if (!strncmp(buffer, "From:", strlen("From:")))
3987 score++;
3988 else if (!strncmp(buffer, "Date:", strlen("Date:")))
3989 score++;
3990 else if (!strncmp(buffer, "Message-ID:", strlen("Message-ID:")))
3991 score++;
3992 else if (!strncmp(buffer, "Subject:", strlen("Subject:")))
3993 score++;
3994 i++;
3996 claws_fclose(fp);
3997 return (score >= 3);
4000 gboolean sc_g_list_bigger(GList *list, gint max)
4002 GList *cur = list;
4003 int i = 0;
4004 while (cur && i <= max+1) {
4005 i++;
4006 cur = cur->next;
4008 return (i > max);
4011 gboolean sc_g_slist_bigger(GSList *list, gint max)
4013 GSList *cur = list;
4014 int i = 0;
4015 while (cur && i <= max+1) {
4016 i++;
4017 cur = cur->next;
4019 return (i > max);
4022 const gchar *daynames[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4023 const gchar *monthnames[] = {NULL, NULL, NULL, NULL, NULL, NULL,
4024 NULL, NULL, NULL, NULL, NULL, NULL};
4025 const gchar *s_daynames[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
4026 const gchar *s_monthnames[] = {NULL, NULL, NULL, NULL, NULL, NULL,
4027 NULL, NULL, NULL, NULL, NULL, NULL};
4029 gint daynames_len[] = {0,0,0,0,0,0,0};
4030 gint monthnames_len[] = {0,0,0,0,0,0,
4031 0,0,0,0,0,0};
4032 gint s_daynames_len[] = {0,0,0,0,0,0,0};
4033 gint s_monthnames_len[] = {0,0,0,0,0,0,
4034 0,0,0,0,0,0};
4035 const gchar *s_am_up = NULL;
4036 const gchar *s_pm_up = NULL;
4037 const gchar *s_am_low = NULL;
4038 const gchar *s_pm_low = NULL;
4040 gint s_am_up_len = 0;
4041 gint s_pm_up_len = 0;
4042 gint s_am_low_len = 0;
4043 gint s_pm_low_len = 0;
4045 static gboolean time_names_init_done = FALSE;
4047 static void init_time_names(void)
4049 int i = 0;
4051 daynames[0] = C_("Complete day name for use by strftime", "Sunday");
4052 daynames[1] = C_("Complete day name for use by strftime", "Monday");
4053 daynames[2] = C_("Complete day name for use by strftime", "Tuesday");
4054 daynames[3] = C_("Complete day name for use by strftime", "Wednesday");
4055 daynames[4] = C_("Complete day name for use by strftime", "Thursday");
4056 daynames[5] = C_("Complete day name for use by strftime", "Friday");
4057 daynames[6] = C_("Complete day name for use by strftime", "Saturday");
4059 monthnames[0] = C_("Complete month name for use by strftime", "January");
4060 monthnames[1] = C_("Complete month name for use by strftime", "February");
4061 monthnames[2] = C_("Complete month name for use by strftime", "March");
4062 monthnames[3] = C_("Complete month name for use by strftime", "April");
4063 monthnames[4] = C_("Complete month name for use by strftime", "May");
4064 monthnames[5] = C_("Complete month name for use by strftime", "June");
4065 monthnames[6] = C_("Complete month name for use by strftime", "July");
4066 monthnames[7] = C_("Complete month name for use by strftime", "August");
4067 monthnames[8] = C_("Complete month name for use by strftime", "September");
4068 monthnames[9] = C_("Complete month name for use by strftime", "October");
4069 monthnames[10] = C_("Complete month name for use by strftime", "November");
4070 monthnames[11] = C_("Complete month name for use by strftime", "December");
4072 s_daynames[0] = C_("Abbr. day name for use by strftime", "Sun");
4073 s_daynames[1] = C_("Abbr. day name for use by strftime", "Mon");
4074 s_daynames[2] = C_("Abbr. day name for use by strftime", "Tue");
4075 s_daynames[3] = C_("Abbr. day name for use by strftime", "Wed");
4076 s_daynames[4] = C_("Abbr. day name for use by strftime", "Thu");
4077 s_daynames[5] = C_("Abbr. day name for use by strftime", "Fri");
4078 s_daynames[6] = C_("Abbr. day name for use by strftime", "Sat");
4080 s_monthnames[0] = C_("Abbr. month name for use by strftime", "Jan");
4081 s_monthnames[1] = C_("Abbr. month name for use by strftime", "Feb");
4082 s_monthnames[2] = C_("Abbr. month name for use by strftime", "Mar");
4083 s_monthnames[3] = C_("Abbr. month name for use by strftime", "Apr");
4084 s_monthnames[4] = C_("Abbr. month name for use by strftime", "May");
4085 s_monthnames[5] = C_("Abbr. month name for use by strftime", "Jun");
4086 s_monthnames[6] = C_("Abbr. month name for use by strftime", "Jul");
4087 s_monthnames[7] = C_("Abbr. month name for use by strftime", "Aug");
4088 s_monthnames[8] = C_("Abbr. month name for use by strftime", "Sep");
4089 s_monthnames[9] = C_("Abbr. month name for use by strftime", "Oct");
4090 s_monthnames[10] = C_("Abbr. month name for use by strftime", "Nov");
4091 s_monthnames[11] = C_("Abbr. month name for use by strftime", "Dec");
4093 for (i = 0; i < 7; i++) {
4094 daynames_len[i] = strlen(daynames[i]);
4095 s_daynames_len[i] = strlen(s_daynames[i]);
4097 for (i = 0; i < 12; i++) {
4098 monthnames_len[i] = strlen(monthnames[i]);
4099 s_monthnames_len[i] = strlen(s_monthnames[i]);
4102 s_am_up = C_("For use by strftime (morning)", "AM");
4103 s_pm_up = C_("For use by strftime (afternoon)", "PM");
4104 s_am_low = C_("For use by strftime (morning, lowercase)", "am");
4105 s_pm_low = C_("For use by strftime (afternoon, lowercase)", "pm");
4107 s_am_up_len = strlen(s_am_up);
4108 s_pm_up_len = strlen(s_pm_up);
4109 s_am_low_len = strlen(s_am_low);
4110 s_pm_low_len = strlen(s_pm_low);
4112 time_names_init_done = TRUE;
4115 #define CHECK_SIZE() { \
4116 total_done += len; \
4117 if (total_done >= buflen) { \
4118 buf[buflen-1] = '\0'; \
4119 return 0; \
4123 size_t fast_strftime(gchar *buf, gint buflen, const gchar *format, struct tm *lt)
4125 gchar *curpos = buf;
4126 gint total_done = 0;
4127 gchar subbuf[64], subfmt[64];
4128 static time_t last_tzset = (time_t)0;
4130 if (!time_names_init_done)
4131 init_time_names();
4133 if (format == NULL || lt == NULL)
4134 return 0;
4136 if (last_tzset != time(NULL)) {
4137 tzset();
4138 last_tzset = time(NULL);
4140 while(*format) {
4141 if (*format == '%') {
4142 gint len = 0, tmp = 0;
4143 format++;
4144 switch(*format) {
4145 case '%':
4146 len = 1; CHECK_SIZE();
4147 *curpos = '%';
4148 break;
4149 case 'a':
4150 len = s_daynames_len[lt->tm_wday]; CHECK_SIZE();
4151 strncpy2(curpos, s_daynames[lt->tm_wday], buflen - total_done);
4152 break;
4153 case 'A':
4154 len = daynames_len[lt->tm_wday]; CHECK_SIZE();
4155 strncpy2(curpos, daynames[lt->tm_wday], buflen - total_done);
4156 break;
4157 case 'b':
4158 case 'h':
4159 len = s_monthnames_len[lt->tm_mon]; CHECK_SIZE();
4160 strncpy2(curpos, s_monthnames[lt->tm_mon], buflen - total_done);
4161 break;
4162 case 'B':
4163 len = monthnames_len[lt->tm_mon]; CHECK_SIZE();
4164 strncpy2(curpos, monthnames[lt->tm_mon], buflen - total_done);
4165 break;
4166 case 'c':
4167 strftime(subbuf, 64, "%c", lt);
4168 len = strlen(subbuf); CHECK_SIZE();
4169 strncpy2(curpos, subbuf, buflen - total_done);
4170 break;
4171 case 'C':
4172 total_done += 2; CHECK_SIZE();
4173 tmp = (lt->tm_year + 1900)/100;
4174 *curpos++ = '0'+(tmp / 10);
4175 *curpos++ = '0'+(tmp % 10);
4176 break;
4177 case 'd':
4178 total_done += 2; CHECK_SIZE();
4179 *curpos++ = '0'+(lt->tm_mday / 10);
4180 *curpos++ = '0'+(lt->tm_mday % 10);
4181 break;
4182 case 'D':
4183 total_done += 8; CHECK_SIZE();
4184 *curpos++ = '0'+((lt->tm_mon+1) / 10);
4185 *curpos++ = '0'+((lt->tm_mon+1) % 10);
4186 *curpos++ = '/';
4187 *curpos++ = '0'+(lt->tm_mday / 10);
4188 *curpos++ = '0'+(lt->tm_mday % 10);
4189 *curpos++ = '/';
4190 tmp = lt->tm_year%100;
4191 *curpos++ = '0'+(tmp / 10);
4192 *curpos++ = '0'+(tmp % 10);
4193 break;
4194 case 'e':
4195 len = 2; CHECK_SIZE();
4196 snprintf(curpos, buflen - total_done, "%2d", lt->tm_mday);
4197 break;
4198 case 'F':
4199 len = 10; CHECK_SIZE();
4200 snprintf(curpos, buflen - total_done, "%4d-%02d-%02d",
4201 lt->tm_year + 1900, lt->tm_mon +1, lt->tm_mday);
4202 break;
4203 case 'H':
4204 total_done += 2; CHECK_SIZE();
4205 *curpos++ = '0'+(lt->tm_hour / 10);
4206 *curpos++ = '0'+(lt->tm_hour % 10);
4207 break;
4208 case 'I':
4209 total_done += 2; CHECK_SIZE();
4210 tmp = lt->tm_hour;
4211 if (tmp > 12)
4212 tmp -= 12;
4213 else if (tmp == 0)
4214 tmp = 12;
4215 *curpos++ = '0'+(tmp / 10);
4216 *curpos++ = '0'+(tmp % 10);
4217 break;
4218 case 'j':
4219 len = 3; CHECK_SIZE();
4220 snprintf(curpos, buflen - total_done, "%03d", lt->tm_yday+1);
4221 break;
4222 case 'k':
4223 len = 2; CHECK_SIZE();
4224 snprintf(curpos, buflen - total_done, "%2d", lt->tm_hour);
4225 break;
4226 case 'l':
4227 len = 2; CHECK_SIZE();
4228 tmp = lt->tm_hour;
4229 if (tmp > 12)
4230 tmp -= 12;
4231 else if (tmp == 0)
4232 tmp = 12;
4233 snprintf(curpos, buflen - total_done, "%2d", tmp);
4234 break;
4235 case 'm':
4236 total_done += 2; CHECK_SIZE();
4237 tmp = lt->tm_mon + 1;
4238 *curpos++ = '0'+(tmp / 10);
4239 *curpos++ = '0'+(tmp % 10);
4240 break;
4241 case 'M':
4242 total_done += 2; CHECK_SIZE();
4243 *curpos++ = '0'+(lt->tm_min / 10);
4244 *curpos++ = '0'+(lt->tm_min % 10);
4245 break;
4246 case 'n':
4247 len = 1; CHECK_SIZE();
4248 *curpos = '\n';
4249 break;
4250 case 'p':
4251 if (lt->tm_hour >= 12) {
4252 len = s_pm_up_len; CHECK_SIZE();
4253 snprintf(curpos, buflen-total_done, "%s", s_pm_up);
4254 } else {
4255 len = s_am_up_len; CHECK_SIZE();
4256 snprintf(curpos, buflen-total_done, "%s", s_am_up);
4258 break;
4259 case 'P':
4260 if (lt->tm_hour >= 12) {
4261 len = s_pm_low_len; CHECK_SIZE();
4262 snprintf(curpos, buflen-total_done, "%s", s_pm_low);
4263 } else {
4264 len = s_am_low_len; CHECK_SIZE();
4265 snprintf(curpos, buflen-total_done, "%s", s_am_low);
4267 break;
4268 case 'r':
4269 #ifdef G_OS_WIN32
4270 strftime(subbuf, 64, "%I:%M:%S %p", lt);
4271 #else
4272 strftime(subbuf, 64, "%r", lt);
4273 #endif
4274 len = strlen(subbuf); CHECK_SIZE();
4275 strncpy2(curpos, subbuf, buflen - total_done);
4276 break;
4277 case 'R':
4278 total_done += 5; CHECK_SIZE();
4279 *curpos++ = '0'+(lt->tm_hour / 10);
4280 *curpos++ = '0'+(lt->tm_hour % 10);
4281 *curpos++ = ':';
4282 *curpos++ = '0'+(lt->tm_min / 10);
4283 *curpos++ = '0'+(lt->tm_min % 10);
4284 break;
4285 case 's':
4286 snprintf(subbuf, 64, "%lld", (long long)mktime(lt));
4287 len = strlen(subbuf); CHECK_SIZE();
4288 strncpy2(curpos, subbuf, buflen - total_done);
4289 break;
4290 case 'S':
4291 total_done += 2; CHECK_SIZE();
4292 *curpos++ = '0'+(lt->tm_sec / 10);
4293 *curpos++ = '0'+(lt->tm_sec % 10);
4294 break;
4295 case 't':
4296 len = 1; CHECK_SIZE();
4297 *curpos = '\t';
4298 break;
4299 case 'T':
4300 total_done += 8; CHECK_SIZE();
4301 *curpos++ = '0'+(lt->tm_hour / 10);
4302 *curpos++ = '0'+(lt->tm_hour % 10);
4303 *curpos++ = ':';
4304 *curpos++ = '0'+(lt->tm_min / 10);
4305 *curpos++ = '0'+(lt->tm_min % 10);
4306 *curpos++ = ':';
4307 *curpos++ = '0'+(lt->tm_sec / 10);
4308 *curpos++ = '0'+(lt->tm_sec % 10);
4309 break;
4310 case 'u':
4311 len = 1; CHECK_SIZE();
4312 snprintf(curpos, buflen - total_done, "%d", lt->tm_wday == 0 ? 7: lt->tm_wday);
4313 break;
4314 case 'w':
4315 len = 1; CHECK_SIZE();
4316 snprintf(curpos, buflen - total_done, "%d", lt->tm_wday);
4317 break;
4318 case 'x':
4319 strftime(subbuf, 64, "%x", lt);
4320 len = strlen(subbuf); CHECK_SIZE();
4321 strncpy2(curpos, subbuf, buflen - total_done);
4322 break;
4323 case 'X':
4324 strftime(subbuf, 64, "%X", lt);
4325 len = strlen(subbuf); CHECK_SIZE();
4326 strncpy2(curpos, subbuf, buflen - total_done);
4327 break;
4328 case 'y':
4329 total_done += 2; CHECK_SIZE();
4330 tmp = lt->tm_year%100;
4331 *curpos++ = '0'+(tmp / 10);
4332 *curpos++ = '0'+(tmp % 10);
4333 break;
4334 case 'Y':
4335 len = 4; CHECK_SIZE();
4336 snprintf(curpos, buflen - total_done, "%4d", lt->tm_year + 1900);
4337 break;
4338 case 'G':
4339 case 'g':
4340 case 'U':
4341 case 'V':
4342 case 'W':
4343 case 'z':
4344 case 'Z':
4345 case '+':
4346 /* let these complicated ones be done with the libc */
4347 snprintf(subfmt, 64, "%%%c", *format);
4348 strftime(subbuf, 64, subfmt, lt);
4349 len = strlen(subbuf); CHECK_SIZE();
4350 strncpy2(curpos, subbuf, buflen - total_done);
4351 break;
4352 case 'E':
4353 case 'O':
4354 /* let these complicated modifiers be done with the libc */
4355 snprintf(subfmt, 64, "%%%c%c", *format, *(format+1));
4356 strftime(subbuf, 64, subfmt, lt);
4357 len = strlen(subbuf); CHECK_SIZE();
4358 strncpy2(curpos, subbuf, buflen - total_done);
4359 format++;
4360 break;
4361 default:
4362 g_warning("format error (%c)", *format);
4363 *curpos = '\0';
4364 return total_done;
4366 curpos += len;
4367 format++;
4368 } else {
4369 int len = 1; CHECK_SIZE();
4370 *curpos++ = *format++;
4373 *curpos = '\0';
4374 return total_done;
4377 #ifdef G_OS_WIN32
4378 #define WEXITSTATUS(x) (x)
4379 #endif
4381 GMutex *cm_mutex_new(void) {
4382 #if GLIB_CHECK_VERSION(2,32,0)
4383 GMutex *m = g_new0(GMutex, 1);
4384 g_mutex_init(m);
4385 return m;
4386 #else
4387 return g_mutex_new();
4388 #endif
4391 void cm_mutex_free(GMutex *mutex) {
4392 #if GLIB_CHECK_VERSION(2,32,0)
4393 g_mutex_clear(mutex);
4394 g_free(mutex);
4395 #else
4396 g_mutex_free(mutex);
4397 #endif
4400 static gchar *canonical_list_to_file(GSList *list)
4402 GString *result = g_string_new(NULL);
4403 GSList *pathlist = g_slist_reverse(g_slist_copy(list));
4404 GSList *cur;
4405 gchar *str;
4407 #ifndef G_OS_WIN32
4408 result = g_string_append(result, G_DIR_SEPARATOR_S);
4409 #else
4410 if (pathlist->data) {
4411 const gchar *root = (gchar *)pathlist->data;
4412 if (root[0] != '\0' && g_ascii_isalpha(root[0]) &&
4413 root[1] == ':') {
4414 /* drive - don't prepend dir separator */
4415 } else {
4416 result = g_string_append(result, G_DIR_SEPARATOR_S);
4419 #endif
4421 for (cur = pathlist; cur; cur = cur->next) {
4422 result = g_string_append(result, (gchar *)cur->data);
4423 if (cur->next)
4424 result = g_string_append(result, G_DIR_SEPARATOR_S);
4426 g_slist_free(pathlist);
4428 str = result->str;
4429 g_string_free(result, FALSE);
4431 return str;
4434 static GSList *cm_split_path(const gchar *filename, int depth)
4436 gchar **path_parts;
4437 GSList *canonical_parts = NULL;
4438 GStatBuf st;
4439 int i;
4440 #ifndef G_OS_WIN32
4441 gboolean follow_symlinks = TRUE;
4442 #endif
4444 if (depth > 32) {
4445 #ifndef G_OS_WIN32
4446 errno = ELOOP;
4447 #else
4448 errno = EINVAL; /* can't happen, no symlink handling */
4449 #endif
4450 return NULL;
4453 if (!g_path_is_absolute(filename)) {
4454 errno =EINVAL;
4455 return NULL;
4458 path_parts = g_strsplit(filename, G_DIR_SEPARATOR_S, -1);
4460 for (i = 0; path_parts[i] != NULL; i++) {
4461 if (!strcmp(path_parts[i], ""))
4462 continue;
4463 if (!strcmp(path_parts[i], "."))
4464 continue;
4465 else if (!strcmp(path_parts[i], "..")) {
4466 if (i == 0) {
4467 errno =ENOTDIR;
4468 return NULL;
4470 else /* Remove the last inserted element */
4471 canonical_parts =
4472 g_slist_delete_link(canonical_parts,
4473 canonical_parts);
4474 } else {
4475 gchar *tmp_path;
4477 canonical_parts = g_slist_prepend(canonical_parts,
4478 g_strdup(path_parts[i]));
4480 tmp_path = canonical_list_to_file(canonical_parts);
4482 if(g_stat(tmp_path, &st) < 0) {
4483 if (errno == ENOENT) {
4484 errno = 0;
4485 #ifndef G_OS_WIN32
4486 follow_symlinks = FALSE;
4487 #endif
4489 if (errno != 0) {
4490 g_free(tmp_path);
4491 slist_free_strings_full(canonical_parts);
4492 g_strfreev(path_parts);
4494 return NULL;
4497 #ifndef G_OS_WIN32
4498 if (follow_symlinks && g_file_test(tmp_path, G_FILE_TEST_IS_SYMLINK)) {
4499 GError *error = NULL;
4500 gchar *target = g_file_read_link(tmp_path, &error);
4502 if (!g_path_is_absolute(target)) {
4503 /* remove the last inserted element */
4504 canonical_parts =
4505 g_slist_delete_link(canonical_parts,
4506 canonical_parts);
4507 /* add the target */
4508 canonical_parts = g_slist_prepend(canonical_parts,
4509 g_strdup(target));
4510 g_free(target);
4512 /* and get the new target */
4513 target = canonical_list_to_file(canonical_parts);
4516 /* restart from absolute target */
4517 slist_free_strings_full(canonical_parts);
4518 canonical_parts = NULL;
4519 if (!error)
4520 canonical_parts = cm_split_path(target, depth + 1);
4521 else
4522 g_error_free(error);
4523 if (canonical_parts == NULL) {
4524 g_free(tmp_path);
4525 g_strfreev(path_parts);
4526 return NULL;
4528 g_free(target);
4530 #endif
4531 g_free(tmp_path);
4534 g_strfreev(path_parts);
4535 return canonical_parts;
4539 * Canonicalize a filename, resolving symlinks along the way.
4540 * Returns a negative errno in case of error.
4542 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
4543 GSList *canonical_parts;
4544 gboolean is_absolute;
4546 if (filename == NULL)
4547 return -EINVAL;
4548 if (canonical_name == NULL)
4549 return -EINVAL;
4550 *canonical_name = NULL;
4552 is_absolute = g_path_is_absolute(filename);
4553 if (!is_absolute) {
4554 /* Always work on absolute filenames. */
4555 gchar *cur = g_get_current_dir();
4556 gchar *absolute_filename = g_strconcat(cur, G_DIR_SEPARATOR_S,
4557 filename, NULL);
4559 canonical_parts = cm_split_path(absolute_filename, 0);
4560 g_free(absolute_filename);
4561 g_free(cur);
4562 } else
4563 canonical_parts = cm_split_path(filename, 0);
4565 if (canonical_parts == NULL)
4566 return -errno;
4568 *canonical_name = canonical_list_to_file(canonical_parts);
4569 slist_free_strings_full(canonical_parts);
4570 return 0;
4573 /* Returns a decoded base64 string, guaranteed to be null-terminated. */
4574 guchar *g_base64_decode_zero(const gchar *text, gsize *out_len)
4576 gchar *tmp = g_base64_decode(text, out_len);
4577 gchar *out = g_strndup(tmp, *out_len);
4579 g_free(tmp);
4581 if (strlen(out) != *out_len) {
4582 g_warning ("strlen(out) %"G_GSIZE_FORMAT" != *out_len %"G_GSIZE_FORMAT, strlen(out), *out_len);
4585 return out;
4588 #if !GLIB_CHECK_VERSION(2, 30, 0)
4590 * g_utf8_substring:
4591 * @str: a UTF-8 encoded string
4592 * @start_pos: a character offset within @str
4593 * @end_pos: another character offset within @str
4595 * Copies a substring out of a UTF-8 encoded string.
4596 * The substring will contain @end_pos - @start_pos
4597 * characters.
4599 * Returns: a newly allocated copy of the requested
4600 * substring. Free with g_free() when no longer needed.
4602 * Since: GLIB 2.30
4604 gchar *
4605 g_utf8_substring (const gchar *str,
4606 glong start_pos,
4607 glong end_pos)
4609 gchar *start, *end, *out;
4611 start = g_utf8_offset_to_pointer (str, start_pos);
4612 end = g_utf8_offset_to_pointer (start, end_pos - start_pos);
4614 out = g_malloc (end - start + 1);
4615 memcpy (out, start, end - start);
4616 out[end - start] = 0;
4618 return out;
4620 #endif
4622 /* Attempts to read count bytes from a PRNG into memory area starting at buf.
4623 * It is up to the caller to make sure there is at least count bytes
4624 * available at buf. */
4625 gboolean
4626 get_random_bytes(void *buf, size_t count)
4628 /* Open our prng source. */
4629 #if defined G_OS_WIN32
4630 HCRYPTPROV rnd;
4632 if (!CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, 0) &&
4633 !CryptAcquireContext(&rnd, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
4634 debug_print("Could not acquire a CSP handle.\n");
4635 return FALSE;
4637 #else
4638 int rnd;
4639 ssize_t ret;
4641 rnd = open("/dev/urandom", O_RDONLY);
4642 if (rnd == -1) {
4643 FILE_OP_ERROR("/dev/urandom", "open");
4644 debug_print("Could not open /dev/urandom.\n");
4645 return FALSE;
4647 #endif
4649 /* Read data from the source into buf. */
4650 #if defined G_OS_WIN32
4651 if (!CryptGenRandom(rnd, count, buf)) {
4652 debug_print("Could not read %"G_GSIZE_FORMAT" random bytes.\n", count);
4653 CryptReleaseContext(rnd, 0);
4654 return FALSE;
4656 #else
4657 ret = read(rnd, buf, count);
4658 if (ret != count) {
4659 FILE_OP_ERROR("/dev/urandom", "read");
4660 debug_print("Could not read enough data from /dev/urandom, read only %ld of %lu bytes.\n", ret, count);
4661 close(rnd);
4662 return FALSE;
4664 #endif
4666 /* Close the prng source. */
4667 #if defined G_OS_WIN32
4668 CryptReleaseContext(rnd, 0);
4669 #else
4670 close(rnd);
4671 #endif
4673 return TRUE;
4676 /* returns FALSE if parsing failed, otherwise returns TRUE and sets *server, *port
4677 and eventually *fp from filename (if not NULL, they must be free'd by caller after
4678 user.
4679 filenames we expect: 'host.name.port.cert' or 'host.name.port.f:i:n:g:e:r:p:r:i:n:t.cert' */
4680 gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar **port, gchar **fp)
4682 const gchar *pos, *dotport_pos = NULL, *dotcert_pos = NULL, *dotfp_pos = NULL;
4684 g_return_val_if_fail(str != NULL, FALSE);
4686 pos = str + strlen(str) - 1;
4687 while ((pos > str) && !dotport_pos) {
4688 if (*pos == '.') {
4689 if (!dotcert_pos) {
4690 /* match the .cert suffix */
4691 if (strcmp(pos, ".cert") == 0) {
4692 dotcert_pos = pos;
4694 } else {
4695 if (!dotfp_pos) {
4696 /* match an eventual fingerprint */
4697 /* or the port number */
4698 if (strncmp(pos + 3, ":", 1) == 0) {
4699 dotfp_pos = pos;
4700 } else {
4701 dotport_pos = pos;
4703 } else {
4704 /* match the port number */
4705 dotport_pos = pos;
4709 pos--;
4711 if (!dotport_pos || !dotcert_pos) {
4712 g_warning("could not parse filename %s", str);
4713 return FALSE;
4716 if (server != NULL)
4717 *server = g_strndup(str, dotport_pos - str);
4718 if (dotfp_pos) {
4719 if (port != NULL)
4720 *port = g_strndup(dotport_pos + 1, dotfp_pos - dotport_pos - 1);
4721 if (fp != NULL)
4722 *fp = g_strndup(dotfp_pos + 1, dotcert_pos - dotfp_pos - 1);
4723 } else {
4724 if (port != NULL)
4725 *port = g_strndup(dotport_pos + 1, dotcert_pos - dotport_pos - 1);
4726 if (fp != NULL)
4727 *fp = NULL;
4730 debug_print("filename='%s' => server='%s' port='%s' fp='%s'\n",
4731 str,
4732 (server ? *server : "(n/a)"),
4733 (port ? *port : "(n/a)"),
4734 (fp ? *fp : "(n/a)"));
4736 if (!(server && *server) || !(port && *port))
4737 return FALSE;
4738 else
4739 return TRUE;
4742 #ifdef G_OS_WIN32
4743 gchar *win32_debug_log_path(void)
4745 return g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S,
4746 "claws-win32.log", NULL);
4748 #endif