2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
40 #include <sys/types.h>
51 static gboolean debug_mode = FALSE;
53 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data);
55 void list_free_strings(GList *list)
57 list = g_list_first(list);
59 while (list != NULL) {
65 void slist_free_strings(GSList *list)
67 while (list != NULL) {
73 GSList *slist_concat_unique (GSList *first, GSList *second)
81 } else if (second == NULL)
84 for (tmp = second; tmp != NULL; tmp = g_slist_next(tmp)) {
85 if (g_slist_find(ret, tmp->data) == NULL)
86 ret = g_slist_prepend(ret, tmp->data);
91 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
96 void hash_free_strings(GHashTable *table)
98 g_hash_table_foreach(table, hash_free_strings_func, NULL);
101 static void hash_free_value_mem_func(gpointer key, gpointer value,
107 void hash_free_value_mem(GHashTable *table)
109 g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
112 void ptr_array_free_strings(GPtrArray *array)
117 g_return_if_fail(array != NULL);
119 for (i = 0; i < array->len; i++) {
120 str = g_ptr_array_index(array, i);
125 gint to_number(const gchar *nstr)
127 register const gchar *p;
129 if (*nstr == '\0') return -1;
131 for (p = nstr; *p != '\0'; p++)
132 if (!isdigit(*p)) return -1;
137 /* convert integer into string,
138 nstr must be not lower than 11 characters length */
139 gchar *itos_buf(gchar *nstr, gint n)
141 g_snprintf(nstr, 11, "%d", n);
145 /* convert integer into string */
148 static gchar nstr[11];
150 return itos_buf(nstr, n);
153 gchar *to_human_readable(off_t size)
155 static gchar str[10];
158 g_snprintf(str, sizeof(str), "%dB", (gint)size);
159 else if (size >> 10 < 1024)
160 g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
161 else if (size >> 20 < 1024)
162 g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
164 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
169 /* strcmp with NULL-checking */
170 gint strcmp2(const gchar *s1, const gchar *s2)
172 if (s1 == NULL || s2 == NULL)
175 return strcmp(s1, s2);
177 /* strstr with NULL-checking */
178 gchar *strstr2(const gchar *s1, const gchar *s2)
180 if (s1 == NULL || s2 == NULL)
183 return strstr(s1, s2);
186 gint path_cmp(const gchar *s1, const gchar *s2)
190 if (s1 == NULL || s2 == NULL) return -1;
191 if (*s1 == '\0' || *s2 == '\0') return -1;
196 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
197 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
199 return strncmp(s1, s2, MAX(len1, len2));
202 /* remove trailing return code */
203 gchar *strretchomp(gchar *str)
207 if (!*str) return str;
209 for (s = str + strlen(str) - 1;
210 s >= str && (*s == '\n' || *s == '\r');
217 /* remove trailing character */
218 gchar *strtailchomp(gchar *str, gchar tail_char)
222 if (!*str) return str;
223 if (tail_char == '\0') return str;
225 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
231 /* remove CR (carriage return) */
232 gchar *strcrchomp(gchar *str)
236 if (!*str) return str;
238 s = str + strlen(str) - 1;
239 if (*s == '\n' && s > str && *(s - 1) == '\r') {
247 /* Similar to `strstr' but this function ignores the case of both strings. */
248 gchar *strcasestr(const gchar *haystack, const gchar *needle)
250 register size_t haystack_len, needle_len;
252 haystack_len = strlen(haystack);
253 needle_len = strlen(needle);
255 if (haystack_len < needle_len || needle_len == 0)
258 while (haystack_len >= needle_len) {
259 if (!strncasecmp(haystack, needle, needle_len))
260 return (gchar *)haystack;
270 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
271 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
285 /* don't do zero fill */
290 int iswalnum(wint_t wc)
292 return isalnum((int)wc);
297 int iswspace(wint_t wc)
299 return isspace((int)wc);
304 wint_t towlower(wint_t wc)
306 if (wc >= L'A' && wc <= L'Z')
307 return wc + L'a' - L'A';
314 size_t wcslen(const wchar_t *s)
326 /* Copy SRC to DEST. */
327 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
335 } while (c != L'\0');
342 /* Copy no more than N wide-characters of SRC to DEST. */
343 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
353 } while (c != L'\0');
364 /* Duplicate S, returning an identical malloc'd string. */
365 wchar_t *wcsdup(const wchar_t *s)
370 new_str = g_new(wchar_t, wcslen(s) + 1);
378 /* Duplicate no more than N wide-characters of S,
379 returning an identical malloc'd string. */
380 wchar_t *wcsndup(const wchar_t *s, size_t n)
385 new_str = g_new(wchar_t, n + 1);
386 wcsncpy(new_str, s, n);
387 new_str[n] = (wchar_t)0;
394 wchar_t *strdup_mbstowcs(const gchar *s)
399 new_str = g_new(wchar_t, strlen(s) + 1);
400 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
404 new_str = g_realloc(new_str,
405 sizeof(wchar_t) * (wcslen(new_str) + 1));
412 gchar *strdup_wcstombs(const wchar_t *s)
418 len = wcslen(s) * MB_CUR_MAX + 1;
419 new_str = g_new(gchar, len);
420 if (wcstombs(new_str, s, len) < 0) {
424 new_str = g_realloc(new_str, strlen(new_str) + 1);
431 /* Compare S1 and S2, ignoring case. */
432 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
438 c1 = towlower(*s1++);
439 c2 = towlower(*s2++);
442 else if (c1 == 0 && c2 == 0)
449 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
450 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
452 register size_t haystack_len, needle_len;
454 haystack_len = wcslen(haystack);
455 needle_len = wcslen(needle);
457 if (haystack_len < needle_len || needle_len == 0)
460 while (haystack_len >= needle_len) {
461 if (!wcsncasecmp(haystack, needle, needle_len))
462 return (wchar_t *)haystack;
472 /* Examine if next block is non-ASCII string */
473 gboolean is_next_nonascii(const wchar_t *s)
477 /* skip head space */
478 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
480 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
488 /* Examine if next block is multi-byte string */
489 gboolean is_next_mbs(const wchar_t *s)
493 gchar tmp[MB_LEN_MAX];
495 /* skip head space */
496 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
498 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
499 mbl = wctomb(tmp, *wp);
507 wchar_t *find_wspace(const wchar_t *s)
511 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
513 for (; *wp != (wchar_t)0; wp++) {
515 return (wchar_t *)wp;
521 /* compare subjects */
522 gint subject_compare(const gchar *s1, const gchar *s2)
526 if (!s1 || !s2) return -1;
527 if (!*s1 || !*s2) return -1;
529 Xstrdup_a(str1, s1, return -1);
530 Xstrdup_a(str2, s2, return -1);
535 if (!*str1 || !*str2) return -1;
537 return strcmp(str1, str2);
540 void trim_subject(gchar *str)
544 eliminate_parenthesis(str, '[', ']');
545 eliminate_parenthesis(str, '(', ')');
548 while (!strncasecmp(str, "Re:", 3)) {
550 while (isspace(*srcp)) srcp++;
551 memmove(str, srcp, strlen(srcp) + 1);
555 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
557 register gchar *srcp, *destp;
562 while ((destp = strchr(destp, op))) {
568 else if (*srcp == cl)
574 while (isspace(*srcp)) srcp++;
575 memmove(destp, srcp, strlen(srcp) + 1);
579 void extract_parenthesis(gchar *str, gchar op, gchar cl)
581 register gchar *srcp, *destp;
586 while ((srcp = strchr(destp, op))) {
589 memmove(destp, srcp + 1, strlen(srcp));
594 else if (*destp == cl)
606 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
609 register gchar *srcp, *destp;
611 gboolean in_quote = FALSE;
615 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
616 memmove(destp, srcp + 1, strlen(srcp));
619 if (*destp == op && !in_quote)
621 else if (*destp == cl && !in_quote)
623 else if (*destp == quote_chr)
635 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
638 register gchar *srcp, *destp;
640 gboolean in_quote = FALSE;
644 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
647 memmove(destp, srcp + 1, strlen(srcp));
650 if (*destp == op && !in_quote)
652 else if (*destp == cl && !in_quote)
654 else if (*destp == quote_chr)
666 void eliminate_quote(gchar *str, gchar quote_chr)
668 register gchar *srcp, *destp;
672 while ((destp = strchr(destp, quote_chr))) {
673 if ((srcp = strchr(destp + 1, quote_chr))) {
675 while (isspace(*srcp)) srcp++;
676 memmove(destp, srcp, strlen(srcp) + 1);
684 void extract_quote(gchar *str, gchar quote_chr)
688 if ((str = strchr(str, quote_chr))) {
690 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
691 memmove(p - 1, p, strlen(p) + 1);
696 memmove(str, str + 1, p - str);
701 void eliminate_address_comment(gchar *str)
703 register gchar *srcp, *destp;
708 while ((destp = strchr(destp, '"'))) {
709 if ((srcp = strchr(destp + 1, '"'))) {
714 while (isspace(*srcp)) srcp++;
715 memmove(destp, srcp, strlen(srcp) + 1);
725 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
731 else if (*srcp == ')')
737 while (isspace(*srcp)) srcp++;
738 memmove(destp, srcp, strlen(srcp) + 1);
742 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
744 gboolean in_quote = FALSE;
747 if (*str == c && !in_quote)
749 if (*str == quote_chr)
757 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
759 gboolean in_quote = FALSE;
762 p = str + strlen(str) - 1;
764 if (*p == c && !in_quote)
774 void extract_address(gchar *str)
776 eliminate_address_comment(str);
777 if (strchr_with_skip_quote(str, '"', '<'))
778 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
782 GSList *address_list_append(GSList *addr_list, const gchar *str)
787 if (!str) return addr_list;
789 Xstrdup_a(work, str, return addr_list);
791 eliminate_address_comment(work);
794 while (workp && *workp) {
797 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
803 if (strchr_with_skip_quote(workp, '"', '<'))
804 extract_parenthesis_with_skip_quote
805 (workp, '"', '<', '>');
809 addr_list = g_slist_append(addr_list, g_strdup(workp));
817 GSList *references_list_append(GSList *msgid_list, const gchar *str)
821 if (!str) return msgid_list;
824 while (strp && *strp) {
825 const gchar *start, *end;
828 if ((start = strchr(strp, '<')) != NULL) {
829 end = strchr(start + 1, '>');
834 msgid = g_strndup(start + 1, end - start - 1);
837 msgid_list = g_slist_append(msgid_list, msgid);
847 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
852 if (!str) return group_list;
854 Xstrdup_a(work, str, return group_list);
858 while (workp && *workp) {
861 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
869 group_list = g_slist_append(group_list,
878 GList *add_history(GList *list, const gchar *str)
882 g_return_val_if_fail(str != NULL, list);
884 old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
887 list = g_list_remove(list, old->data);
888 } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
891 last = g_list_last(list);
894 g_list_remove(list, last->data);
898 list = g_list_prepend(list, g_strdup(str));
903 void remove_return(gchar *str)
905 register gchar *p = str;
908 if (*p == '\n' || *p == '\r')
909 memmove(p, p + 1, strlen(p));
915 void remove_space(gchar *str)
917 register gchar *p = str;
922 while (isspace(*(p + spc)))
925 memmove(p, p + spc, strlen(p + spc) + 1);
931 void unfold_line(gchar *str)
933 register gchar *p = str;
937 if (*p == '\n' || *p == '\r') {
940 while (isspace(*(p + spc)))
943 memmove(p, p + spc, strlen(p + spc) + 1);
949 void subst_char(gchar *str, gchar orig, gchar subst)
951 register gchar *p = str;
960 void subst_chars(gchar *str, gchar *orig, gchar subst)
962 register gchar *p = str;
965 if (strchr(orig, *p) != NULL)
971 void subst_for_filename(gchar *str)
973 subst_chars(str, " \t\r\n\"/\\", '_');
976 gboolean is_header_line(const gchar *str)
978 if (str[0] == ':') return FALSE;
980 while (*str != '\0' && *str != ' ') {
989 gboolean is_ascii_str(const guchar *str)
991 while (*str != '\0') {
992 if (*str != '\t' && *str != ' ' &&
993 *str != '\r' && *str != '\n' &&
994 (*str < 32 || *str >= 127))
1002 gint get_quote_level(const gchar *str, const gchar *quote_chars)
1004 const gchar *first_pos;
1005 const gchar *last_pos;
1006 const gchar *p = str;
1007 gint quote_level = -1;
1009 /* speed up line processing by only searching to the last '>' */
1010 if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
1011 /* skip a line if it contains a '<' before the initial '>' */
1012 if (memchr(str, '<', first_pos - str) != NULL)
1014 last_pos = line_has_quote_char_last(first_pos, quote_chars);
1018 while (p <= last_pos) {
1019 while (p < last_pos) {
1026 if (strchr(quote_chars, *p))
1028 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
1029 /* any characters are allowed except '-' and space */
1031 && !strchr(quote_chars, *p)
1035 if (strchr(quote_chars, *p))
1047 const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
1049 gchar * position = NULL;
1050 gchar * tmp_pos = NULL;
1053 if (quote_chars == NULL)
1056 for (i = 0; i < strlen(quote_chars); i++) {
1057 tmp_pos = strchr (str, quote_chars[i]);
1059 || (tmp_pos != NULL && position >= tmp_pos) )
1065 const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
1067 gchar * position = NULL;
1068 gchar * tmp_pos = NULL;
1071 if (quote_chars == NULL)
1074 for (i = 0; i < strlen(quote_chars); i++) {
1075 tmp_pos = strrchr (str, quote_chars[i]);
1077 || (tmp_pos != NULL && position <= tmp_pos) )
1083 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1085 register guint haystack_len, needle_len;
1086 gboolean in_squote = FALSE, in_dquote = FALSE;
1088 haystack_len = strlen(haystack);
1089 needle_len = strlen(needle);
1091 if (haystack_len < needle_len || needle_len == 0)
1094 while (haystack_len >= needle_len) {
1095 if (!in_squote && !in_dquote &&
1096 !strncmp(haystack, needle, needle_len))
1097 return (gchar *)haystack;
1099 /* 'foo"bar"' -> foo"bar"
1100 "foo'bar'" -> foo'bar' */
1101 if (*haystack == '\'') {
1104 else if (!in_dquote)
1106 } else if (*haystack == '\"') {
1109 else if (!in_squote)
1120 gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
1123 gchar quote_chr = '"';
1125 gboolean in_quote = FALSE;
1129 if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
1133 if (*p == op && !in_quote)
1135 else if (*p == cl && !in_quote)
1137 else if (*p == quote_chr)
1150 gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
1153 GSList *string_list = NULL, *slist;
1155 const gchar *s_op, *s_cl;
1158 g_return_val_if_fail(str != NULL, NULL);
1161 max_tokens = G_MAXINT;
1163 s_op = strchr_with_skip_quote(str, '"', op);
1164 if (!s_op) return NULL;
1166 s_cl = strchr_parenthesis_close(str, op, cl);
1174 new_string = g_new(gchar, len + 1);
1175 strncpy(new_string, str, len);
1176 new_string[len] = 0;
1177 string_list = g_slist_prepend(string_list, new_string);
1181 while (*str && isspace(*str)) str++;
1183 string_list = g_slist_prepend(string_list,
1186 s_op = strchr_with_skip_quote(str, '"', op);
1187 if (!--max_tokens || !s_op) break;
1191 s_cl = strchr_parenthesis_close(str, op, cl);
1192 } while (--max_tokens && s_cl);
1195 str_array = g_new(gchar*, n);
1199 str_array[i--] = NULL;
1200 for (slist = string_list; slist; slist = slist->next)
1201 str_array[i--] = slist->data;
1203 g_slist_free(string_list);
1208 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1211 GSList *string_list = NULL, *slist;
1212 gchar **str_array, *s, *new_str;
1213 guint i, n = 1, len;
1215 g_return_val_if_fail(str != NULL, NULL);
1216 g_return_val_if_fail(delim != NULL, NULL);
1219 max_tokens = G_MAXINT;
1221 s = strstr_with_skip_quote(str, delim);
1223 guint delimiter_len = strlen(delim);
1227 new_str = g_strndup(str, len);
1229 if (new_str[0] == '\'' || new_str[0] == '\"') {
1230 if (new_str[len - 1] == new_str[0]) {
1231 new_str[len - 1] = '\0';
1232 memmove(new_str, new_str + 1, len - 1);
1235 string_list = g_slist_prepend(string_list, new_str);
1237 str = s + delimiter_len;
1238 s = strstr_with_skip_quote(str, delim);
1239 } while (--max_tokens && s);
1243 new_str = g_strdup(str);
1244 if (new_str[0] == '\'' || new_str[0] == '\"') {
1246 if (new_str[len - 1] == new_str[0]) {
1247 new_str[len - 1] = '\0';
1248 memmove(new_str, new_str + 1, len - 1);
1251 string_list = g_slist_prepend(string_list, new_str);
1255 str_array = g_new(gchar*, n);
1259 str_array[i--] = NULL;
1260 for (slist = string_list; slist; slist = slist->next)
1261 str_array[i--] = slist->data;
1263 g_slist_free(string_list);
1268 gchar *get_abbrev_newsgroup_name(const gchar *group, gint len)
1270 gchar *abbrev_group;
1272 const gchar *p = group;
1275 abbrev_group = ap = g_malloc(strlen(group) + 1);
1281 if ((strlen( p) + count) > len && strchr(p, '.')) {
1283 while (*p != '.') p++;
1286 return abbrev_group;
1292 return abbrev_group;
1295 gchar *trim_string(const gchar *str, gint len)
1297 const gchar *p = str;
1302 if (!str) return NULL;
1303 if (strlen(str) <= len)
1304 return g_strdup(str);
1306 while (*p != '\0') {
1307 mb_len = mblen(p, MB_LEN_MAX);
1310 else if (mb_len < 0)
1311 return g_strdup(str);
1312 else if (new_len + mb_len > len)
1319 Xstrndup_a(new_str, str, new_len, return g_strdup(str));
1320 return g_strconcat(new_str, "...", NULL);
1323 GList *uri_list_extract_filenames(const gchar *uri_list)
1325 GList *result = NULL;
1333 while (isspace(*p)) p++;
1334 if (!strncmp(p, "file:", 5)) {
1337 while (*q && *q != '\n' && *q != '\r') q++;
1341 while (q > p && isspace(*q)) q--;
1342 file = g_malloc(q - p + 2);
1343 strncpy(file, p, q - p + 1);
1344 file[q - p + 1] = '\0';
1345 result = g_list_append(result,file);
1349 p = strchr(p, '\n');
1356 #define HEX_TO_INT(val, hex) \
1360 if ('0' <= c && c <= '9') { \
1362 } else if ('a' <= c && c <= 'f') { \
1363 val = c - 'a' + 10; \
1364 } else if ('A' <= c && c <= 'F') { \
1365 val = c - 'A' + 10; \
1371 gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
1372 gchar **subject, gchar **body)
1377 Xstrdup_a(tmp_mailto, mailto, return -1);
1379 if (!strncmp(tmp_mailto, "mailto:", 7))
1382 p = strchr(tmp_mailto, '?');
1389 *to = g_strdup(tmp_mailto);
1392 gchar *field, *value;
1409 if (*value == '\0') continue;
1411 if (cc && !*cc && !g_strcasecmp(field, "cc")) {
1412 *cc = g_strdup(value);
1413 } else if (bcc && !*bcc && !g_strcasecmp(field, "bcc")) {
1414 *bcc = g_strdup(value);
1415 } else if (subject && !*subject &&
1416 !g_strcasecmp(field, "subject")) {
1417 *subject = g_malloc(strlen(value) + 1);
1418 decode_uri(*subject, value);
1419 } else if (body && !*body && !g_strcasecmp(field, "body")) {
1420 *body = g_malloc(strlen(value) + 1);
1421 decode_uri(*body, value);
1429 * We need this wrapper around g_get_home_dir(), so that
1430 * we can fix some Windoze things here. Should be done in glibc of course
1431 * but as long as we are not able to do our own extensions to glibc, we do
1434 gchar *get_home_dir(void)
1436 #if HAVE_DOSISH_SYSTEM
1437 static gchar *home_dir;
1440 home_dir = read_w32_registry_string(NULL,
1441 "Software\\Sylpheed", "HomeDir" );
1442 if (!home_dir || !*home_dir) {
1443 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1444 const char *s = g_get_home_dir();
1446 home_dir = g_strdup (s);
1448 if (!home_dir || !*home_dir)
1449 home_dir = g_strdup ("c:\\sylpheed");
1451 debug_print("initialized home_dir to `%s'\n", home_dir);
1454 #else /* standard glib */
1455 return g_get_home_dir();
1459 gchar *get_rc_dir(void)
1461 static gchar *rc_dir = NULL;
1464 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1470 gchar *get_news_cache_dir(void)
1472 static gchar *news_cache_dir = NULL;
1474 if (!news_cache_dir)
1475 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1476 NEWS_CACHE_DIR, NULL);
1478 return news_cache_dir;
1481 gchar *get_imap_cache_dir(void)
1483 static gchar *imap_cache_dir = NULL;
1485 if (!imap_cache_dir)
1486 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1487 IMAP_CACHE_DIR, NULL);
1489 return imap_cache_dir;
1492 gchar *get_mbox_cache_dir(void)
1494 static gchar *mbox_cache_dir = NULL;
1496 if (!mbox_cache_dir)
1497 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1498 MBOX_CACHE_DIR, NULL);
1500 return mbox_cache_dir;
1503 gchar *get_mime_tmp_dir(void)
1505 static gchar *mime_tmp_dir = NULL;
1508 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1509 MIME_TMP_DIR, NULL);
1511 return mime_tmp_dir;
1514 gchar *get_template_dir(void)
1516 static gchar *template_dir = NULL;
1519 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1520 TEMPLATE_DIR, NULL);
1522 return template_dir;
1525 gchar *get_header_cache_dir(void)
1527 static gchar *header_dir = NULL;
1530 header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1531 HEADER_CACHE_DIR, NULL);
1536 gchar *get_tmp_dir(void)
1538 static gchar *tmp_dir = NULL;
1541 tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1547 gchar *get_tmp_file(void)
1550 static guint32 id = 0;
1552 tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
1553 get_tmp_dir(), G_DIR_SEPARATOR, id++);
1558 gchar *get_domain_name(void)
1560 static gchar *domain_name = NULL;
1563 gchar buf[128] = "";
1566 if (gethostname(buf, sizeof(buf)) < 0) {
1567 perror("gethostname");
1568 domain_name = "unknown";
1570 buf[sizeof(buf) - 1] = '\0';
1571 if ((hp = my_gethostbyname(buf)) == NULL) {
1572 perror("gethostbyname");
1573 domain_name = g_strdup(buf);
1575 domain_name = g_strdup(hp->h_name);
1579 debug_print("domain name = %s\n", domain_name);
1585 off_t get_file_size(const gchar *file)
1589 if (stat(file, &s) < 0) {
1590 FILE_OP_ERROR(file, "stat");
1597 off_t get_file_size_as_crlf(const gchar *file)
1601 gchar buf[BUFFSIZE];
1603 if ((fp = fopen(file, "rb")) == NULL) {
1604 FILE_OP_ERROR(file, "fopen");
1608 while (fgets(buf, sizeof(buf), fp) != NULL) {
1610 size += strlen(buf) + 2;
1614 FILE_OP_ERROR(file, "fgets");
1623 off_t get_left_file_size(FILE *fp)
1629 if ((pos = ftell(fp)) < 0) {
1633 if (fseek(fp, 0L, SEEK_END) < 0) {
1637 if ((end = ftell(fp)) < 0) {
1642 if (fseek(fp, pos, SEEK_SET) < 0) {
1650 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1657 if (stat(file, &s) < 0) {
1658 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1662 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1668 gboolean is_dir_exist(const gchar *dir)
1675 if (stat(dir, &s) < 0) {
1676 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1680 if (S_ISDIR(s.st_mode))
1686 gboolean is_file_entry_exist(const gchar *file)
1693 if (stat(file, &s) < 0) {
1694 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1701 gint change_dir(const gchar *dir)
1703 gchar *prevdir = NULL;
1706 prevdir = g_get_current_dir();
1708 if (chdir(dir) < 0) {
1709 FILE_OP_ERROR(dir, "chdir");
1710 if (debug_mode) g_free(prevdir);
1712 } else if (debug_mode) {
1715 cwd = g_get_current_dir();
1716 if (strcmp(prevdir, cwd) != 0)
1717 g_print("current dir: %s\n", cwd);
1725 gint make_dir(const gchar *dir)
1727 if (mkdir(dir, S_IRWXU) < 0) {
1728 FILE_OP_ERROR(dir, "mkdir");
1731 if (chmod(dir, S_IRWXU) < 0)
1732 FILE_OP_ERROR(dir, "chmod");
1737 gint make_dir_hier(const gchar *dir)
1742 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1743 parent_dir = g_strndup(dir, p - dir);
1744 if (*parent_dir != '\0') {
1745 if (!is_dir_exist(parent_dir)) {
1746 if (make_dir(parent_dir) < 0) {
1755 if (!is_dir_exist(dir)) {
1756 if (make_dir(dir) < 0)
1763 gint remove_all_files(const gchar *dir)
1769 prev_dir = g_get_current_dir();
1771 if (chdir(dir) < 0) {
1772 FILE_OP_ERROR(dir, "chdir");
1777 if ((dp = opendir(".")) == NULL) {
1778 FILE_OP_ERROR(dir, "opendir");
1783 while ((d = readdir(dp)) != NULL) {
1784 if (!strcmp(d->d_name, ".") ||
1785 !strcmp(d->d_name, ".."))
1788 if (unlink(d->d_name) < 0)
1789 FILE_OP_ERROR(d->d_name, "unlink");
1794 if (chdir(prev_dir) < 0) {
1795 FILE_OP_ERROR(prev_dir, "chdir");
1805 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1812 prev_dir = g_get_current_dir();
1814 if (chdir(dir) < 0) {
1815 FILE_OP_ERROR(dir, "chdir");
1820 if ((dp = opendir(".")) == NULL) {
1821 FILE_OP_ERROR(dir, "opendir");
1826 while ((d = readdir(dp)) != NULL) {
1827 fileno = to_number(d->d_name);
1828 if (fileno >= 0 && first <= fileno && fileno <= last) {
1829 if (is_dir_exist(d->d_name))
1831 if (unlink(d->d_name) < 0)
1832 FILE_OP_ERROR(d->d_name, "unlink");
1838 if (chdir(prev_dir) < 0) {
1839 FILE_OP_ERROR(prev_dir, "chdir");
1849 gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
1856 prev_dir = g_get_current_dir();
1858 if (chdir(dir) < 0) {
1859 FILE_OP_ERROR(dir, "chdir");
1864 if ((dp = opendir(".")) == NULL) {
1865 FILE_OP_ERROR(dir, "opendir");
1870 while ((d = readdir(dp)) != NULL) {
1871 fileno = to_number(d->d_name);
1872 if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
1873 debug_print("removing unwanted file %d from %s\n", fileno, dir);
1874 if (is_dir_exist(d->d_name))
1876 if (unlink(d->d_name) < 0)
1877 FILE_OP_ERROR(d->d_name, "unlink");
1883 if (chdir(prev_dir) < 0) {
1884 FILE_OP_ERROR(prev_dir, "chdir");
1894 gint remove_all_numbered_files(const gchar *dir)
1896 return remove_numbered_files(dir, 0, UINT_MAX);
1899 gint remove_expired_files(const gchar *dir, guint hours)
1906 time_t mtime, now, expire_time;
1908 prev_dir = g_get_current_dir();
1910 if (chdir(dir) < 0) {
1911 FILE_OP_ERROR(dir, "chdir");
1916 if ((dp = opendir(".")) == NULL) {
1917 FILE_OP_ERROR(dir, "opendir");
1923 expire_time = hours * 60 * 60;
1925 while ((d = readdir(dp)) != NULL) {
1926 fileno = to_number(d->d_name);
1928 if (stat(d->d_name, &s) < 0) {
1929 FILE_OP_ERROR(d->d_name, "stat");
1932 if (S_ISDIR(s.st_mode))
1934 mtime = MAX(s.st_mtime, s.st_atime);
1935 if (now - mtime > expire_time) {
1936 if (unlink(d->d_name) < 0)
1937 FILE_OP_ERROR(d->d_name, "unlink");
1944 if (chdir(prev_dir) < 0) {
1945 FILE_OP_ERROR(prev_dir, "chdir");
1955 gint remove_dir_recursive(const gchar *dir)
1962 /* g_print("dir = %s\n", dir); */
1964 if (stat(dir, &s) < 0) {
1965 FILE_OP_ERROR(dir, "stat");
1966 if (ENOENT == errno) return 0;
1970 if (!S_ISDIR(s.st_mode)) {
1971 if (unlink(dir) < 0) {
1972 FILE_OP_ERROR(dir, "unlink");
1979 prev_dir = g_get_current_dir();
1980 /* g_print("prev_dir = %s\n", prev_dir); */
1982 if (!path_cmp(prev_dir, dir)) {
1984 if (chdir("..") < 0) {
1985 FILE_OP_ERROR(dir, "chdir");
1988 prev_dir = g_get_current_dir();
1991 if (chdir(dir) < 0) {
1992 FILE_OP_ERROR(dir, "chdir");
1997 if ((dp = opendir(".")) == NULL) {
1998 FILE_OP_ERROR(dir, "opendir");
2004 /* remove all files in the directory */
2005 while ((d = readdir(dp)) != NULL) {
2006 if (!strcmp(d->d_name, ".") ||
2007 !strcmp(d->d_name, ".."))
2010 if (stat(d->d_name, &s) < 0) {
2011 FILE_OP_ERROR(d->d_name, "stat");
2015 /* g_print("removing %s\n", d->d_name); */
2017 if (S_ISDIR(s.st_mode)) {
2018 if (remove_dir_recursive(d->d_name) < 0) {
2019 g_warning("can't remove directory\n");
2023 if (unlink(d->d_name) < 0)
2024 FILE_OP_ERROR(d->d_name, "unlink");
2030 if (chdir(prev_dir) < 0) {
2031 FILE_OP_ERROR(prev_dir, "chdir");
2038 if (rmdir(dir) < 0) {
2039 FILE_OP_ERROR(dir, "rmdir");
2047 /* this seems to be slower than the stdio version... */
2048 gint copy_file(const gchar *src, const gchar *dest)
2050 gint src_fd, dest_fd;
2054 gchar *dest_bak = NULL;
2056 if ((src_fd = open(src, O_RDONLY)) < 0) {
2057 FILE_OP_ERROR(src, "open");
2061 if (is_file_exist(dest)) {
2062 dest_bak = g_strconcat(dest, ".bak", NULL);
2063 if (rename(dest, dest_bak) < 0) {
2064 FILE_OP_ERROR(dest, "rename");
2071 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
2072 FILE_OP_ERROR(dest, "open");
2075 if (rename(dest_bak, dest) < 0)
2076 FILE_OP_ERROR(dest_bak, "rename");
2082 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
2087 n_write = write(dest_fd, bufp, len);
2089 g_warning(_("writing to %s failed.\n"), dest);
2094 if (rename(dest_bak, dest) < 0)
2095 FILE_OP_ERROR(dest_bak, "rename");
2108 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
2109 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
2112 if (rename(dest_bak, dest) < 0)
2113 FILE_OP_ERROR(dest_bak, "rename");
2126 * Append src file body to the tail of dest file.
2127 * Now keep_backup has no effects.
2129 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2131 FILE *src_fp, *dest_fp;
2135 gboolean err = FALSE;
2137 if ((src_fp = fopen(src, "rb")) == NULL) {
2138 FILE_OP_ERROR(src, "fopen");
2142 if ((dest_fp = fopen(dest, "ab")) == NULL) {
2143 FILE_OP_ERROR(dest, "fopen");
2148 if (change_file_mode_rw(dest_fp, dest) < 0) {
2149 FILE_OP_ERROR(dest, "chmod");
2150 g_warning(_("can't change file mode\n"));
2153 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2154 if (n_read < sizeof(buf) && ferror(src_fp))
2156 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2157 g_warning(_("writing to %s failed.\n"), dest);
2165 if (ferror(src_fp)) {
2166 FILE_OP_ERROR(src, "fread");
2170 if (fclose(dest_fp) == EOF) {
2171 FILE_OP_ERROR(dest, "fclose");
2183 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2185 FILE *src_fp, *dest_fp;
2188 gchar *dest_bak = NULL;
2189 gboolean err = FALSE;
2191 if ((src_fp = fopen(src, "rb")) == NULL) {
2192 FILE_OP_ERROR(src, "fopen");
2195 if (is_file_exist(dest)) {
2196 dest_bak = g_strconcat(dest, ".bak", NULL);
2197 if (rename(dest, dest_bak) < 0) {
2198 FILE_OP_ERROR(dest, "rename");
2205 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2206 FILE_OP_ERROR(dest, "fopen");
2209 if (rename(dest_bak, dest) < 0)
2210 FILE_OP_ERROR(dest_bak, "rename");
2216 if (change_file_mode_rw(dest_fp, dest) < 0) {
2217 FILE_OP_ERROR(dest, "chmod");
2218 g_warning(_("can't change file mode\n"));
2221 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2222 if (n_read < sizeof(buf) && ferror(src_fp))
2224 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2225 g_warning(_("writing to %s failed.\n"), dest);
2230 if (rename(dest_bak, dest) < 0)
2231 FILE_OP_ERROR(dest_bak, "rename");
2238 if (ferror(src_fp)) {
2239 FILE_OP_ERROR(src, "fread");
2243 if (fclose(dest_fp) == EOF) {
2244 FILE_OP_ERROR(dest, "fclose");
2251 if (rename(dest_bak, dest) < 0)
2252 FILE_OP_ERROR(dest_bak, "rename");
2258 if (keep_backup == FALSE && dest_bak)
2266 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
2268 if (overwrite == FALSE && is_file_exist(dest)) {
2269 g_warning("move_file(): file %s already exists.", dest);
2273 if (rename(src, dest) == 0) return 0;
2275 if (EXDEV != errno) {
2276 FILE_OP_ERROR(src, "rename");
2280 if (copy_file(src, dest, FALSE) < 0) return -1;
2287 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
2291 gint bytes_left, to_read;
2293 gboolean err = FALSE;
2295 if (fseek(fp, offset, SEEK_SET) < 0) {
2300 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2301 FILE_OP_ERROR(dest, "fopen");
2305 if (change_file_mode_rw(dest_fp, dest) < 0) {
2306 FILE_OP_ERROR(dest, "chmod");
2307 g_warning("can't change file mode\n");
2310 bytes_left = length;
2311 to_read = MIN(bytes_left, sizeof(buf));
2313 while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
2314 if (n_read < to_read && ferror(fp))
2316 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2317 g_warning(_("writing to %s failed.\n"), dest);
2322 bytes_left -= n_read;
2323 if (bytes_left == 0)
2325 to_read = MIN(bytes_left, sizeof(buf));
2332 if (fclose(dest_fp) == EOF) {
2333 FILE_OP_ERROR(dest, "fclose");
2345 /* convert line endings into CRLF. If the last line doesn't end with
2346 * linebreak, add it.
2348 gint canonicalize_file(const gchar *src, const gchar *dest)
2350 FILE *src_fp, *dest_fp;
2351 gchar buf[BUFFSIZE];
2353 gboolean err = FALSE;
2354 gboolean last_linebreak = FALSE;
2356 if ((src_fp = fopen(src, "rb")) == NULL) {
2357 FILE_OP_ERROR(src, "fopen");
2361 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2362 FILE_OP_ERROR(dest, "fopen");
2367 if (change_file_mode_rw(dest_fp, dest) < 0) {
2368 FILE_OP_ERROR(dest, "chmod");
2369 g_warning("can't change file mode\n");
2372 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2376 if (len == 0) break;
2377 last_linebreak = FALSE;
2379 if (buf[len - 1] != '\n') {
2380 last_linebreak = TRUE;
2381 r = fputs(buf, dest_fp);
2382 } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
2383 r = fputs(buf, dest_fp);
2386 r = fwrite(buf, len - 1, 1, dest_fp);
2391 r = fputs("\r\n", dest_fp);
2395 g_warning("writing to %s failed.\n", dest);
2403 if (last_linebreak == TRUE) {
2404 if (fputs("\r\n", dest_fp) == EOF)
2408 if (ferror(src_fp)) {
2409 FILE_OP_ERROR(src, "fread");
2413 if (fclose(dest_fp) == EOF) {
2414 FILE_OP_ERROR(dest, "fclose");
2426 gint canonicalize_file_replace(const gchar *file)
2430 tmp_file = get_tmp_file();
2432 if (canonicalize_file(file, tmp_file) < 0) {
2437 if (move_file(tmp_file, file, TRUE) < 0) {
2438 g_warning("can't replace %s .\n", file);
2448 gint change_file_mode_rw(FILE *fp, const gchar *file)
2451 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
2453 return chmod(file, S_IRUSR|S_IWUSR);
2457 FILE *my_tmpfile(void)
2460 const gchar suffix[] = ".XXXXXX";
2461 const gchar *tmpdir;
2463 const gchar *progname;
2469 tmpdir = get_tmp_dir();
2470 tmplen = strlen(tmpdir);
2471 progname = g_get_prgname();
2472 proglen = strlen(progname);
2473 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
2476 memcpy(fname, tmpdir, tmplen);
2477 fname[tmplen] = G_DIR_SEPARATOR;
2478 memcpy(fname + tmplen + 1, progname, proglen);
2479 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
2481 fd = mkstemp(fname);
2487 fp = fdopen(fd, "w+b");
2492 #endif /* HAVE_MKSTEMP */
2497 FILE *str_open_as_stream(const gchar *str)
2502 g_return_val_if_fail(str != NULL, NULL);
2506 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
2511 if (len == 0) return fp;
2513 if (fwrite(str, len, 1, fp) != 1) {
2514 FILE_OP_ERROR("str_open_as_stream", "fwrite");
2523 gint str_write_to_file(const gchar *str, const gchar *file)
2528 g_return_val_if_fail(str != NULL, -1);
2529 g_return_val_if_fail(file != NULL, -1);
2531 if ((fp = fopen(file, "wb")) == NULL) {
2532 FILE_OP_ERROR(file, "fopen");
2542 if (fwrite(str, len, 1, fp) != 1) {
2543 FILE_OP_ERROR(file, "fwrite");
2549 if (fclose(fp) == EOF) {
2550 FILE_OP_ERROR(file, "fclose");
2558 gchar *file_read_to_str(const gchar *file)
2566 g_return_val_if_fail(file != NULL, NULL);
2568 if ((fp = fopen(file, "rb")) == NULL) {
2569 FILE_OP_ERROR(file, "fopen");
2573 array = g_byte_array_new();
2575 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
2576 if (n_read < sizeof(buf) && ferror(fp))
2578 g_byte_array_append(array, buf, n_read);
2582 FILE_OP_ERROR(file, "fread");
2584 g_byte_array_free(array, TRUE);
2591 g_byte_array_append(array, buf, 1);
2592 str = (gchar *)array->data;
2593 g_byte_array_free(array, FALSE);
2598 gint execute_async(gchar *const argv[])
2602 if ((pid = fork()) < 0) {
2607 if (pid == 0) { /* child process */
2610 if ((gch_pid = fork()) < 0) {
2615 if (gch_pid == 0) { /* grandchild process */
2616 execvp(argv[0], argv);
2625 waitpid(pid, NULL, 0);
2630 gint execute_sync(gchar *const argv[])
2634 if ((pid = fork()) < 0) {
2639 if (pid == 0) { /* child process */
2640 execvp(argv[0], argv);
2646 waitpid(pid, NULL, 0);
2651 gint execute_command_line(const gchar *cmdline, gboolean async)
2656 argv = strsplit_with_quote(cmdline, " ", 0);
2659 ret = execute_async(argv);
2661 ret = execute_sync(argv);
2667 static gint is_unchanged_uri_char(char c)
2679 void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
2685 for(i = 0; i < strlen(uri) ; i++) {
2686 if (is_unchanged_uri_char(uri[i])) {
2687 if (k + 2 >= bufsize)
2689 encoded_uri[k++] = uri[i];
2692 char * hexa = "0123456789ABCDEF";
2694 if (k + 4 >= bufsize)
2696 encoded_uri[k++] = '%';
2697 encoded_uri[k++] = hexa[uri[i] / 16];
2698 encoded_uri[k++] = hexa[uri[i] % 16];
2704 /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
2707 static gint axtoi(const gchar *hexstr)
2709 gint hi, lo, result;
2712 if ('0' <= hi && hi <= '9') {
2715 if ('a' <= hi && hi <= 'f') {
2718 if ('A' <= hi && hi <= 'F') {
2723 if ('0' <= lo && lo <= '9') {
2726 if ('a' <= lo && lo <= 'f') {
2729 if ('A' <= lo && lo <= 'F') {
2732 result = lo + (16 * hi);
2737 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
2738 * plusses, and escape characters are used)
2741 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
2743 const gchar *encoded;
2746 encoded = encoded_uri;
2747 decoded = decoded_uri;
2750 if (*encoded == '%') {
2752 if (isxdigit(encoded[0])
2753 && isxdigit(encoded[1])) {
2754 *decoded = (gchar) axtoi(encoded);
2759 else if (*encoded == '+') {
2765 *decoded = *encoded;
2775 gint open_uri(const gchar *uri, const gchar *cmdline)
2777 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
2778 gchar buf[BUFFSIZE];
2780 gchar encoded_uri[BUFFSIZE];
2782 g_return_val_if_fail(uri != NULL, -1);
2784 /* an option to choose whether to use encode_uri or not ? */
2785 encode_uri(encoded_uri, BUFFSIZE, uri);
2788 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
2789 !strchr(p + 2, '%'))
2790 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
2793 g_warning(_("Open URI command line is invalid: `%s'"),
2795 g_snprintf(buf, sizeof(buf), default_cmdline, encoded_uri);
2798 execute_command_line(buf, TRUE);
2803 time_t remote_tzoffset_sec(const gchar *zone)
2805 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
2811 time_t remoteoffset;
2813 strncpy(zone3, zone, 3);
2817 if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
2818 (c == '+' || c == '-')) {
2819 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
2821 remoteoffset = -remoteoffset;
2822 } else if (!strncmp(zone, "UT" , 2) ||
2823 !strncmp(zone, "GMT", 2)) {
2825 } else if (strlen(zone3) == 3 &&
2826 (p = strstr(ustzstr, zone3)) != NULL &&
2827 (p - ustzstr) % 3 == 0) {
2828 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
2829 remoteoffset = iustz * 3600;
2830 } else if (strlen(zone3) == 1) {
2832 case 'Z': remoteoffset = 0; break;
2833 case 'A': remoteoffset = -1; break;
2834 case 'B': remoteoffset = -2; break;
2835 case 'C': remoteoffset = -3; break;
2836 case 'D': remoteoffset = -4; break;
2837 case 'E': remoteoffset = -5; break;
2838 case 'F': remoteoffset = -6; break;
2839 case 'G': remoteoffset = -7; break;
2840 case 'H': remoteoffset = -8; break;
2841 case 'I': remoteoffset = -9; break;
2842 case 'K': remoteoffset = -10; break; /* J is not used */
2843 case 'L': remoteoffset = -11; break;
2844 case 'M': remoteoffset = -12; break;
2845 case 'N': remoteoffset = 1; break;
2846 case 'O': remoteoffset = 2; break;
2847 case 'P': remoteoffset = 3; break;
2848 case 'Q': remoteoffset = 4; break;
2849 case 'R': remoteoffset = 5; break;
2850 case 'S': remoteoffset = 6; break;
2851 case 'T': remoteoffset = 7; break;
2852 case 'U': remoteoffset = 8; break;
2853 case 'V': remoteoffset = 9; break;
2854 case 'W': remoteoffset = 10; break;
2855 case 'X': remoteoffset = 11; break;
2856 case 'Y': remoteoffset = 12; break;
2857 default: remoteoffset = 0; break;
2859 remoteoffset = remoteoffset * 3600;
2862 return remoteoffset;
2865 time_t tzoffset_sec(time_t *now)
2871 lt = localtime(now);
2873 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2875 if (lt->tm_year < gmt.tm_year)
2877 else if (lt->tm_year > gmt.tm_year)
2879 else if (lt->tm_yday < gmt.tm_yday)
2881 else if (lt->tm_yday > gmt.tm_yday)
2884 if (off >= 24 * 60) /* should be impossible */
2885 off = 23 * 60 + 59; /* if not, insert silly value */
2886 if (off <= -24 * 60)
2887 off = -(23 * 60 + 59);
2896 /* calculate timezone offset */
2897 gchar *tzoffset(time_t *now)
2899 static gchar offset_string[6];
2905 lt = localtime(now);
2907 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2909 if (lt->tm_year < gmt.tm_year)
2911 else if (lt->tm_year > gmt.tm_year)
2913 else if (lt->tm_yday < gmt.tm_yday)
2915 else if (lt->tm_yday > gmt.tm_yday)
2923 if (off >= 24 * 60) /* should be impossible */
2924 off = 23 * 60 + 59; /* if not, insert silly value */
2926 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
2928 return offset_string;
2931 void get_rfc822_date(gchar *buf, gint len)
2935 gchar day[4], mon[4];
2936 gint dd, hh, mm, ss, yyyy;
2941 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
2942 day, mon, &dd, &hh, &mm, &ss, &yyyy);
2943 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2944 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
2947 void debug_set_mode(gboolean mode)
2952 gboolean debug_get_mode()
2957 void debug_print_real(const gchar *format, ...)
2960 gchar buf[BUFFSIZE];
2962 if (!debug_mode) return;
2964 va_start(args, format);
2965 g_vsnprintf(buf, sizeof(buf), format, args);
2971 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
2973 if (subject == NULL)
2976 if (g_strncasecmp(subject, "Re: ", 4) == 0)
2977 return g_hash_table_lookup(subject_table, subject + 4);
2979 return g_hash_table_lookup(subject_table, subject);
2982 void subject_table_insert(GHashTable *subject_table, gchar * subject,
2985 if (subject == NULL)
2989 if (g_strcasecmp(subject, "Re:") == 0)
2991 if (g_strcasecmp(subject, "Re: ") == 0)
2994 if (g_strncasecmp(subject, "Re: ", 4) == 0)
2995 g_hash_table_insert(subject_table, subject + 4, data);
2997 g_hash_table_insert(subject_table, subject, data);
3000 void subject_table_remove(GHashTable *subject_table, gchar * subject)
3002 if (subject == NULL)
3005 if (g_strncasecmp(subject, "Re: ", 4) == 0)
3006 g_hash_table_remove(subject_table, subject + 4);
3008 g_hash_table_remove(subject_table, subject);
3011 gboolean subject_is_reply(const gchar *subject)
3013 /* XXX: just simply here so someone can handle really
3014 * advanced Re: detection like "Re[4]", "ANTW:" or
3015 * Re: Re: Re: Re: Re: Re: Re: Re:" stuff. */
3016 if (subject == NULL) return FALSE;
3017 else return 0 == g_strncasecmp(subject, "Re: ", 4);
3020 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
3024 *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
3025 fd = mkstemp(*filename);
3027 return fdopen(fd, "w+");
3030 /* allow Mutt-like patterns in quick search */
3031 gchar *expand_search_string(const gchar *search_string)
3033 int i, len, new_len = 0;
3034 gchar term_char, save_char;
3035 gchar *cmd_start, *cmd_end;
3036 gchar *new_str = NULL;
3038 gboolean casesens, dontmatch;
3039 /* list of allowed pattern abbreviations */
3041 gchar *abbreviated; /* abbreviation */
3042 gchar *command; /* actual matcher command */
3043 gint numparams; /* number of params for cmd */
3044 gboolean qualifier; /* do we append regexpcase */
3045 gboolean quotes; /* do we need quotes */
3048 { "a", "all", 0, FALSE, FALSE },
3049 { "ag", "age_greater", 1, FALSE, FALSE },
3050 { "al", "age_lower", 1, FALSE, FALSE },
3051 { "b", "body_part", 1, TRUE, TRUE },
3052 { "B", "message", 1, TRUE, TRUE },
3053 { "c", "cc", 1, TRUE, TRUE },
3054 { "C", "to_or_cc", 1, TRUE, TRUE },
3055 { "D", "deleted", 0, FALSE, FALSE },
3056 { "e", "header \"Sender\"", 1, TRUE, TRUE },
3057 { "E", "execute", 1, FALSE, TRUE },
3058 { "f", "from", 1, TRUE, TRUE },
3059 { "F", "forwarded", 0, FALSE, FALSE },
3060 { "h", "headers_part", 1, TRUE, TRUE },
3061 { "i", "header \"Message-Id\"", 1, TRUE, TRUE },
3062 { "I", "inreplyto", 1, TRUE, TRUE },
3063 { "n", "newsgroups", 1, TRUE, TRUE },
3064 { "N", "new", 0, FALSE, FALSE },
3065 { "O", "~new", 0, FALSE, FALSE },
3066 { "r", "replied", 0, FALSE, FALSE },
3067 { "R", "~unread", 0, FALSE, FALSE },
3068 { "s", "subject", 1, TRUE, TRUE },
3069 { "se", "score_equal", 1, FALSE, FALSE },
3070 { "sg", "score_greater", 1, FALSE, FALSE },
3071 { "sl", "score_lower", 1, FALSE, FALSE },
3072 { "Se", "size_equal", 1, FALSE, FALSE },
3073 { "Sg", "size_greater", 1, FALSE, FALSE },
3074 { "Ss", "size_smaller", 1, FALSE, FALSE },
3075 { "t", "to", 1, TRUE, TRUE },
3076 { "T", "marked", 0, FALSE, FALSE },
3077 { "U", "unread", 0, FALSE, FALSE },
3078 { "x", "header \"References\"", 1, TRUE, TRUE },
3079 { "y", "header \"X-Label\"", 1, TRUE, TRUE },
3080 { "&", "&", 0, FALSE, FALSE },
3081 { "|", "|", 0, FALSE, FALSE },
3082 { NULL, NULL, 0, FALSE, FALSE }
3085 if (search_string == NULL)
3088 copy_str = g_strdup(search_string);
3090 /* if it's a full command don't process it so users
3091 can still do something like from regexpcase "foo" */
3092 for (i = 0; cmds[i].command; i++) {
3093 gchar *tmp_search_string = search_string;
3094 cmd_start = cmds[i].command;
3095 /* allow logical NOT */
3096 if (*tmp_search_string == '~')
3097 tmp_search_string++;
3098 if (!strncmp(tmp_search_string, cmd_start, strlen(cmd_start)))
3101 if (cmds[i].command)
3104 cmd_start = cmd_end = copy_str;
3105 while (cmd_end && *cmd_end) {
3106 /* skip all white spaces */
3107 while (*cmd_end && isspace(*cmd_end))
3110 /* extract a command */
3111 while (*cmd_end && !isspace(*cmd_end))
3114 /* save character */
3115 save_char = *cmd_end;
3121 /* ~ and ! mean logical NOT */
3122 if (*cmd_start == '~' || *cmd_start == '!')
3127 /* % means case sensitive match */
3128 if (*cmd_start == '%')
3134 /* find matching abbreviation */
3135 for (i = 0; cmds[i].command; i++) {
3136 if (!strcmp(cmd_start, cmds[i].abbreviated)) {
3137 /* restore character */
3138 *cmd_end = save_char;
3139 len = strlen(cmds[i].command) + 1;
3148 new_str = g_realloc(new_str, new_len);
3149 strcat(new_str, " ");
3151 new_len += (len + 1);
3152 new_str = g_realloc(new_str, new_len);
3153 if (new_len == len + 1)
3156 strcat(new_str, "~");
3157 strcat(new_str, cmds[i].command);
3158 strcat(new_str, " ");
3160 /* stop if no params required */
3161 if (cmds[i].numparams == 0)
3164 /* extract a parameter, allow quotes */
3166 cmd_start = cmd_end;
3167 if (*cmd_start == '"') {
3174 /* extract actual parameter */
3175 while ((*cmd_end) && (*cmd_end != term_char))
3178 if (*cmd_end && (*cmd_end != term_char))
3181 if (*cmd_end == '"')
3184 save_char = *cmd_end;
3187 new_len += strlen(cmd_start);
3189 /* do we need to add regexpcase ? */
3190 if (cmds[i].qualifier)
3191 new_len += 10; /* "regexpcase " */
3193 if (term_char != '"')
3195 new_str = g_realloc(new_str, new_len);
3197 if (cmds[i].qualifier) {
3199 strcat(new_str, "regexp ");
3201 strcat(new_str, "regexpcase ");
3204 /* do we need to add quotes ? */
3205 if (cmds[i].quotes && term_char != '"')
3206 strcat(new_str, "\"");
3208 /* copy actual parameter */
3209 strcat(new_str, cmd_start);
3211 /* do we need to add quotes ? */
3212 if (cmds[i].quotes && term_char != '"')
3213 strcat(new_str, "\"");
3215 /* restore original character */
3216 *cmd_end = save_char;
3224 cmd_start = cmd_end;