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>
48 #include "statusbar.h"
49 #include "logwindow.h"
53 extern gboolean debug_mode;
55 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data);
57 void list_free_strings(GList *list)
59 list = g_list_first(list);
61 while (list != NULL) {
67 void slist_free_strings(GSList *list)
69 while (list != NULL) {
75 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
80 void hash_free_strings(GHashTable *table)
82 g_hash_table_foreach(table, hash_free_strings_func, NULL);
85 static void hash_free_value_mem_func(gpointer key, gpointer value,
91 void hash_free_value_mem(GHashTable *table)
93 g_hash_table_foreach(table, hash_free_value_mem_func, NULL);
96 void ptr_array_free_strings(GPtrArray *array)
101 g_return_if_fail(array != NULL);
103 for (i = 0; i < array->len; i++) {
104 str = g_ptr_array_index(array, i);
109 gint to_number(const gchar *nstr)
111 register const gchar *p;
113 if (*nstr == '\0') return -1;
115 for (p = nstr; *p != '\0'; p++)
116 if (!isdigit(*p)) return -1;
121 /* convert integer into string,
122 nstr must be not lower than 11 characters length */
123 gchar *itos_buf(gchar *nstr, gint n)
125 g_snprintf(nstr, 11, "%d", n);
129 /* convert integer into string */
132 static gchar nstr[11];
134 return itos_buf(nstr, n);
137 gchar *to_human_readable(off_t size)
139 static gchar str[10];
142 g_snprintf(str, sizeof(str), "%dB", (gint)size);
143 else if (size >> 10 < 1024)
144 g_snprintf(str, sizeof(str), "%.1fKB", (gfloat)size / (1 << 10));
145 else if (size >> 20 < 1024)
146 g_snprintf(str, sizeof(str), "%.2fMB", (gfloat)size / (1 << 20));
148 g_snprintf(str, sizeof(str), "%.2fGB", (gfloat)size / (1 << 30));
153 /* strcmp with NULL-checking */
154 gint strcmp2(const gchar *s1, const gchar *s2)
156 if (s1 == NULL || s2 == NULL)
159 return strcmp(s1, s2);
161 /* strstr with NULL-checking */
162 gchar *strstr2(const gchar *s1, const gchar *s2)
164 if (s1 == NULL || s2 == NULL)
167 return strstr(s1, s2);
170 gint path_cmp(const gchar *s1, const gchar *s2)
174 if (s1 == NULL || s2 == NULL) return -1;
175 if (*s1 == '\0' || *s2 == '\0') return -1;
180 if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
181 if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
183 return strncmp(s1, s2, MAX(len1, len2));
186 /* remove trailing return code */
187 gchar *strretchomp(gchar *str)
191 if (!*str) return str;
193 for (s = str + strlen(str) - 1;
194 s >= str && (*s == '\n' || *s == '\r');
201 /* remove trailing character */
202 gchar *strtailchomp(gchar *str, gchar tail_char)
206 if (!*str) return str;
207 if (tail_char == '\0') return str;
209 for (s = str + strlen(str) - 1; s >= str && *s == tail_char; s--)
215 /* remove CR (carriage return) */
216 gchar *strcrchomp(gchar *str)
220 if (!*str) return str;
222 s = str + strlen(str) - 1;
223 if (*s == '\n' && s > str && *(s - 1) == '\r') {
231 /* Similar to `strstr' but this function ignores the case of both strings. */
232 gchar *strcasestr(const gchar *haystack, const gchar *needle)
234 register size_t haystack_len, needle_len;
236 haystack_len = strlen(haystack);
237 needle_len = strlen(needle);
239 if (haystack_len < needle_len || needle_len == 0)
242 while (haystack_len >= needle_len) {
243 if (!strncasecmp(haystack, needle, needle_len))
244 return (gchar *)haystack;
254 /* Copy no more than N characters of SRC to DEST, with NULL terminating. */
255 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
269 /* don't do zero fill */
274 int iswalnum(wint_t wc)
276 return isalnum((int)wc);
281 int iswspace(wint_t wc)
283 return isspace((int)wc);
288 wint_t towlower(wint_t wc)
290 if (wc >= L'A' && wc <= L'Z')
291 return wc + L'a' - L'A';
298 size_t wcslen(const wchar_t *s)
310 /* Copy SRC to DEST. */
311 wchar_t *wcscpy(wchar_t *dest, const wchar_t *src)
319 } while (c != L'\0');
326 /* Copy no more than N wide-characters of SRC to DEST. */
327 wchar_t *wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
337 } while (c != L'\0');
348 /* Duplicate S, returning an identical malloc'd string. */
349 wchar_t *wcsdup(const wchar_t *s)
354 new_str = g_new(wchar_t, wcslen(s) + 1);
362 /* Duplicate no more than N wide-characters of S,
363 returning an identical malloc'd string. */
364 wchar_t *wcsndup(const wchar_t *s, size_t n)
369 new_str = g_new(wchar_t, n + 1);
370 wcsncpy(new_str, s, n);
371 new_str[n] = (wchar_t)0;
378 wchar_t *strdup_mbstowcs(const gchar *s)
383 new_str = g_new(wchar_t, strlen(s) + 1);
384 if (mbstowcs(new_str, s, strlen(s) + 1) < 0) {
388 new_str = g_realloc(new_str,
389 sizeof(wchar_t) * (wcslen(new_str) + 1));
396 gchar *strdup_wcstombs(const wchar_t *s)
402 len = wcslen(s) * MB_CUR_MAX + 1;
403 new_str = g_new(gchar, len);
404 if (wcstombs(new_str, s, len) < 0) {
408 new_str = g_realloc(new_str, strlen(new_str) + 1);
415 /* Compare S1 and S2, ignoring case. */
416 gint wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
422 c1 = towlower(*s1++);
423 c2 = towlower(*s2++);
426 else if (c1 == 0 && c2 == 0)
433 /* Find the first occurrence of NEEDLE in HAYSTACK, ignoring case. */
434 wchar_t *wcscasestr(const wchar_t *haystack, const wchar_t *needle)
436 register size_t haystack_len, needle_len;
438 haystack_len = wcslen(haystack);
439 needle_len = wcslen(needle);
441 if (haystack_len < needle_len || needle_len == 0)
444 while (haystack_len >= needle_len) {
445 if (!wcsncasecmp(haystack, needle, needle_len))
446 return (wchar_t *)haystack;
456 /* Examine if next block is non-ASCII string */
457 gboolean is_next_nonascii(const wchar_t *s)
461 /* skip head space */
462 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
464 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
472 /* Examine if next block is multi-byte string */
473 gboolean is_next_mbs(const wchar_t *s)
477 gchar tmp[MB_LEN_MAX];
479 /* skip head space */
480 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
482 for (; *wp != (wchar_t)0 && !iswspace(*wp); wp++) {
483 mbl = wctomb(tmp, *wp);
491 wchar_t *find_wspace(const wchar_t *s)
495 for (wp = s; *wp != (wchar_t)0 && iswspace(*wp); wp++)
497 for (; *wp != (wchar_t)0; wp++) {
499 return (wchar_t *)wp;
505 /* compare subjects */
506 gint subject_compare(const gchar *s1, const gchar *s2)
510 if (!s1 || !s2) return -1;
511 if (!*s1 || !*s2) return -1;
513 Xstrdup_a(str1, s1, return -1);
514 Xstrdup_a(str2, s2, return -1);
519 if (!*str1 || !*str2) return -1;
521 return strcmp(str1, str2);
524 void trim_subject(gchar *str)
528 eliminate_parenthesis(str, '[', ']');
529 eliminate_parenthesis(str, '(', ')');
532 while (!strncasecmp(str, "Re:", 3)) {
534 while (isspace(*srcp)) srcp++;
535 memmove(str, srcp, strlen(srcp) + 1);
539 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
541 register gchar *srcp, *destp;
546 while ((destp = strchr(destp, op))) {
552 else if (*srcp == cl)
558 while (isspace(*srcp)) srcp++;
559 memmove(destp, srcp, strlen(srcp) + 1);
563 void extract_parenthesis(gchar *str, gchar op, gchar cl)
565 register gchar *srcp, *destp;
570 while ((srcp = strchr(destp, op))) {
573 memmove(destp, srcp + 1, strlen(srcp));
578 else if (*destp == cl)
590 void extract_one_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
593 register gchar *srcp, *destp;
595 gboolean in_quote = FALSE;
599 if ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
600 memmove(destp, srcp + 1, strlen(srcp));
603 if (*destp == op && !in_quote)
605 else if (*destp == cl && !in_quote)
607 else if (*destp == quote_chr)
619 void extract_parenthesis_with_skip_quote(gchar *str, gchar quote_chr,
622 register gchar *srcp, *destp;
624 gboolean in_quote = FALSE;
628 while ((srcp = strchr_with_skip_quote(destp, quote_chr, op))) {
631 memmove(destp, srcp + 1, strlen(srcp));
634 if (*destp == op && !in_quote)
636 else if (*destp == cl && !in_quote)
638 else if (*destp == quote_chr)
650 void eliminate_quote(gchar *str, gchar quote_chr)
652 register gchar *srcp, *destp;
656 while ((destp = strchr(destp, quote_chr))) {
657 if ((srcp = strchr(destp + 1, quote_chr))) {
659 while (isspace(*srcp)) srcp++;
660 memmove(destp, srcp, strlen(srcp) + 1);
668 void extract_quote(gchar *str, gchar quote_chr)
672 if ((str = strchr(str, quote_chr))) {
674 while ((p = strchr(p + 1, quote_chr)) && (p[-1] == '\\')) {
675 memmove(p - 1, p, strlen(p) + 1);
680 memmove(str, str + 1, p - str);
685 void eliminate_address_comment(gchar *str)
687 register gchar *srcp, *destp;
692 while ((destp = strchr(destp, '"'))) {
693 if ((srcp = strchr(destp + 1, '"'))) {
698 while (isspace(*srcp)) srcp++;
699 memmove(destp, srcp, strlen(srcp) + 1);
709 while ((destp = strchr_with_skip_quote(destp, '"', '('))) {
715 else if (*srcp == ')')
721 while (isspace(*srcp)) srcp++;
722 memmove(destp, srcp, strlen(srcp) + 1);
726 gchar *strchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
728 gboolean in_quote = FALSE;
731 if (*str == c && !in_quote)
733 if (*str == quote_chr)
741 gchar *strrchr_with_skip_quote(const gchar *str, gint quote_chr, gint c)
743 gboolean in_quote = FALSE;
746 p = str + strlen(str) - 1;
748 if (*p == c && !in_quote)
758 void extract_address(gchar *str)
760 eliminate_address_comment(str);
761 if (strchr_with_skip_quote(str, '"', '<'))
762 extract_parenthesis_with_skip_quote(str, '"', '<', '>');
766 GSList *address_list_append(GSList *addr_list, const gchar *str)
771 if (!str) return addr_list;
773 Xstrdup_a(work, str, return addr_list);
775 eliminate_address_comment(work);
778 while (workp && *workp) {
781 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
787 if (strchr_with_skip_quote(workp, '"', '<'))
788 extract_parenthesis_with_skip_quote
789 (workp, '"', '<', '>');
793 addr_list = g_slist_append(addr_list, g_strdup(workp));
801 GSList *references_list_append(GSList *msgid_list, const gchar *str)
805 if (!str) return msgid_list;
808 while (strp && *strp) {
809 const gchar *start, *end;
812 if ((start = strchr(strp, '<')) != NULL) {
813 end = strchr(start + 1, '>');
818 msgid = g_strndup(start + 1, end - start - 1);
821 msgid_list = g_slist_append(msgid_list, msgid);
831 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
836 if (!str) return group_list;
838 Xstrdup_a(work, str, return group_list);
842 while (workp && *workp) {
845 if ((p = strchr_with_skip_quote(workp, '"', ','))) {
853 group_list = g_slist_append(group_list,
862 GList *add_history(GList *list, const gchar *str)
866 g_return_val_if_fail(str != NULL, list);
868 old = g_list_find_custom(list, (gpointer)str, (GCompareFunc)strcmp2);
871 list = g_list_remove(list, old->data);
872 } else if (g_list_length(list) >= MAX_HISTORY_SIZE) {
875 last = g_list_last(list);
878 g_list_remove(list, last->data);
882 list = g_list_prepend(list, g_strdup(str));
887 void remove_return(gchar *str)
889 register gchar *p = str;
892 if (*p == '\n' || *p == '\r')
893 memmove(p, p + 1, strlen(p));
899 void remove_space(gchar *str)
901 register gchar *p = str;
906 while (isspace(*(p + spc)))
909 memmove(p, p + spc, strlen(p + spc) + 1);
915 void unfold_line(gchar *str)
917 register gchar *p = str;
921 if (*p == '\n' || *p == '\r') {
924 while (isspace(*(p + spc)))
927 memmove(p, p + spc, strlen(p + spc) + 1);
933 void subst_char(gchar *str, gchar orig, gchar subst)
935 register gchar *p = str;
944 void subst_chars(gchar *str, gchar *orig, gchar subst)
946 register gchar *p = str;
949 if (strchr(orig, *p) != NULL)
955 void subst_for_filename(gchar *str)
957 subst_chars(str, " \t\r\n\"/\\", '_');
960 gboolean is_header_line(const gchar *str)
962 if (str[0] == ':') return FALSE;
964 while (*str != '\0' && *str != ' ') {
973 gboolean is_ascii_str(const guchar *str)
975 while (*str != '\0') {
976 if (*str != '\t' && *str != ' ' &&
977 *str != '\r' && *str != '\n' &&
978 (*str < 32 || *str >= 127))
986 gint get_quote_level(const gchar *str, const gchar *quote_chars)
988 const gchar *first_pos;
989 const gchar *last_pos;
990 const gchar *p = str;
991 gint quote_level = -1;
993 /* speed up line processing by only searching to the last '>' */
994 if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
995 /* skip a line if it contains a '<' before the initial '>' */
996 if (memchr(str, '<', first_pos - str) != NULL)
998 last_pos = line_has_quote_char_last(first_pos, quote_chars);
1002 while (p <= last_pos) {
1003 while (p < last_pos) {
1010 if (strchr(quote_chars, *p))
1012 else if (*p != '-' && !isspace(*p) && p <= last_pos) {
1013 /* any characters are allowed except '-' and space */
1015 && !strchr(quote_chars, *p)
1019 if (strchr(quote_chars, *p))
1031 const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
1033 gchar * position = NULL;
1034 gchar * tmp_pos = NULL;
1037 if (quote_chars == NULL)
1040 for (i = 0; i < strlen(quote_chars); i++) {
1041 tmp_pos = strchr (str, quote_chars[i]);
1043 || (tmp_pos != NULL && position >= tmp_pos) )
1049 const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
1051 gchar * position = NULL;
1052 gchar * tmp_pos = NULL;
1055 if (quote_chars == NULL)
1058 for (i = 0; i < strlen(quote_chars); i++) {
1059 tmp_pos = strrchr (str, quote_chars[i]);
1061 || (tmp_pos != NULL && position <= tmp_pos) )
1067 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
1069 register guint haystack_len, needle_len;
1070 gboolean in_squote = FALSE, in_dquote = FALSE;
1072 haystack_len = strlen(haystack);
1073 needle_len = strlen(needle);
1075 if (haystack_len < needle_len || needle_len == 0)
1078 while (haystack_len >= needle_len) {
1079 if (!in_squote && !in_dquote &&
1080 !strncmp(haystack, needle, needle_len))
1081 return (gchar *)haystack;
1083 /* 'foo"bar"' -> foo"bar"
1084 "foo'bar'" -> foo'bar' */
1085 if (*haystack == '\'') {
1088 else if (!in_dquote)
1090 } else if (*haystack == '\"') {
1093 else if (!in_squote)
1104 gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
1107 gchar quote_chr = '"';
1109 gboolean in_quote = FALSE;
1113 if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
1117 if (*p == op && !in_quote)
1119 else if (*p == cl && !in_quote)
1121 else if (*p == quote_chr)
1134 gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
1137 GSList *string_list = NULL, *slist;
1139 const gchar *s_op, *s_cl;
1142 g_return_val_if_fail(str != NULL, NULL);
1145 max_tokens = G_MAXINT;
1147 s_op = strchr_with_skip_quote(str, '"', op);
1148 if (!s_op) return NULL;
1150 s_cl = strchr_parenthesis_close(str, op, cl);
1158 new_string = g_new(gchar, len + 1);
1159 strncpy(new_string, str, len);
1160 new_string[len] = 0;
1161 string_list = g_slist_prepend(string_list, new_string);
1165 while (*str && isspace(*str)) str++;
1167 string_list = g_slist_prepend(string_list,
1170 s_op = strchr_with_skip_quote(str, '"', op);
1171 if (!--max_tokens || !s_op) break;
1175 s_cl = strchr_parenthesis_close(str, op, cl);
1176 } while (--max_tokens && s_cl);
1179 str_array = g_new(gchar*, n);
1183 str_array[i--] = NULL;
1184 for (slist = string_list; slist; slist = slist->next)
1185 str_array[i--] = slist->data;
1187 g_slist_free(string_list);
1192 gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
1195 GSList *string_list = NULL, *slist;
1196 gchar **str_array, *s, *new_str;
1197 guint i, n = 1, len;
1199 g_return_val_if_fail(str != NULL, NULL);
1200 g_return_val_if_fail(delim != NULL, NULL);
1203 max_tokens = G_MAXINT;
1205 s = strstr_with_skip_quote(str, delim);
1207 guint delimiter_len = strlen(delim);
1211 new_str = g_strndup(str, len);
1213 if (new_str[0] == '\'' || new_str[0] == '\"') {
1214 if (new_str[len - 1] == new_str[0]) {
1215 new_str[len - 1] = '\0';
1216 memmove(new_str, new_str + 1, len - 1);
1219 string_list = g_slist_prepend(string_list, new_str);
1221 str = s + delimiter_len;
1222 s = strstr_with_skip_quote(str, delim);
1223 } while (--max_tokens && s);
1227 new_str = g_strdup(str);
1228 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);
1239 str_array = g_new(gchar*, n);
1243 str_array[i--] = NULL;
1244 for (slist = string_list; slist; slist = slist->next)
1245 str_array[i--] = slist->data;
1247 g_slist_free(string_list);
1252 gchar *get_abbrev_newsgroup_name(const gchar *group)
1254 gchar *abbrev_group;
1256 const gchar *p = group;
1258 abbrev_group = ap = g_malloc(strlen(group) + 1);
1263 if (strchr(p, '.')) {
1265 while (*p != '.') p++;
1268 return abbrev_group;
1273 return abbrev_group;
1276 gchar *trim_string(const gchar *str, gint len)
1278 const gchar *p = str;
1283 if (!str) return NULL;
1284 if (strlen(str) <= len)
1285 return g_strdup(str);
1287 while (*p != '\0') {
1288 mb_len = mblen(p, MB_LEN_MAX);
1291 else if (mb_len < 0)
1292 return g_strdup(str);
1293 else if (new_len + mb_len > len)
1300 Xstrndup_a(new_str, str, new_len, return g_strdup(str));
1301 return g_strconcat(new_str, "...", NULL);
1304 GList *uri_list_extract_filenames(const gchar *uri_list)
1306 GList *result = NULL;
1314 while (isspace(*p)) p++;
1315 if (!strncmp(p, "file:", 5)) {
1318 while (*q && *q != '\n' && *q != '\r') q++;
1322 while (q > p && isspace(*q)) q--;
1323 file = g_malloc(q - p + 2);
1324 strncpy(file, p, q - p + 1);
1325 file[q - p + 1] = '\0';
1326 result = g_list_append(result,file);
1330 p = strchr(p, '\n');
1337 #define HEX_TO_INT(val, hex) \
1341 if ('0' <= c && c <= '9') { \
1343 } else if ('a' <= c && c <= 'f') { \
1344 val = c - 'a' + 10; \
1345 } else if ('A' <= c && c <= 'F') { \
1346 val = c - 'A' + 10; \
1352 gint scan_mailto_url(const gchar *mailto, gchar **to, gchar **cc, gchar **bcc,
1353 gchar **subject, gchar **body)
1358 Xstrdup_a(tmp_mailto, mailto, return -1);
1360 if (!strncmp(tmp_mailto, "mailto:", 7))
1363 p = strchr(tmp_mailto, '?');
1370 *to = g_strdup(tmp_mailto);
1373 gchar *field, *value;
1390 if (*value == '\0') continue;
1392 if (cc && !*cc && !g_strcasecmp(field, "cc")) {
1393 *cc = g_strdup(value);
1394 } else if (bcc && !*bcc && !g_strcasecmp(field, "bcc")) {
1395 *bcc = g_strdup(value);
1396 } else if (subject && !*subject &&
1397 !g_strcasecmp(field, "subject")) {
1398 *subject = g_malloc(strlen(value) + 1);
1399 decode_uri(*subject, value);
1400 } else if (body && !*body && !g_strcasecmp(field, "body")) {
1401 *body = g_malloc(strlen(value) + 1);
1402 decode_uri(*body, value);
1410 * We need this wrapper around g_get_home_dir(), so that
1411 * we can fix some Windoze things here. Should be done in glibc of course
1412 * but as long as we are not able to do our own extensions to glibc, we do
1415 gchar *get_home_dir(void)
1417 #if HAVE_DOSISH_SYSTEM
1418 static gchar *home_dir;
1421 home_dir = read_w32_registry_string(NULL,
1422 "Software\\Sylpheed", "HomeDir" );
1423 if (!home_dir || !*home_dir) {
1424 if (getenv ("HOMEDRIVE") && getenv("HOMEPATH")) {
1425 const char *s = g_get_home_dir();
1427 home_dir = g_strdup (s);
1429 if (!home_dir || !*home_dir)
1430 home_dir = g_strdup ("c:\\sylpheed");
1432 debug_print("initialized home_dir to `%s'\n", home_dir);
1435 #else /* standard glib */
1436 return g_get_home_dir();
1440 gchar *get_rc_dir(void)
1442 static gchar *rc_dir = NULL;
1445 rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1451 gchar *get_news_cache_dir(void)
1453 static gchar *news_cache_dir = NULL;
1455 if (!news_cache_dir)
1456 news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1457 NEWS_CACHE_DIR, NULL);
1459 return news_cache_dir;
1462 gchar *get_imap_cache_dir(void)
1464 static gchar *imap_cache_dir = NULL;
1466 if (!imap_cache_dir)
1467 imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1468 IMAP_CACHE_DIR, NULL);
1470 return imap_cache_dir;
1473 gchar *get_mbox_cache_dir(void)
1475 static gchar *mbox_cache_dir = NULL;
1477 if (!mbox_cache_dir)
1478 mbox_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1479 MBOX_CACHE_DIR, NULL);
1481 return mbox_cache_dir;
1484 gchar *get_mime_tmp_dir(void)
1486 static gchar *mime_tmp_dir = NULL;
1489 mime_tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1490 MIME_TMP_DIR, NULL);
1492 return mime_tmp_dir;
1495 gchar *get_template_dir(void)
1497 static gchar *template_dir = NULL;
1500 template_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1501 TEMPLATE_DIR, NULL);
1503 return template_dir;
1506 gchar *get_header_cache_dir(void)
1508 static gchar *header_dir = NULL;
1511 header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1512 HEADER_CACHE_DIR, NULL);
1517 gchar *get_tmp_dir(void)
1519 static gchar *tmp_dir = NULL;
1522 tmp_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1528 gchar *get_tmp_file(void)
1531 static guint32 id = 0;
1533 tmp_file = g_strdup_printf("%s%ctmpfile.%08x",
1534 get_tmp_dir(), G_DIR_SEPARATOR, id++);
1539 gchar *get_domain_name(void)
1541 static gchar *domain_name = NULL;
1544 gchar buf[128] = "";
1547 if (gethostname(buf, sizeof(buf)) < 0) {
1548 perror("gethostname");
1549 domain_name = "unknown";
1551 buf[sizeof(buf) - 1] = '\0';
1552 if ((hp = my_gethostbyname(buf)) == NULL) {
1553 perror("gethostbyname");
1554 domain_name = g_strdup(buf);
1556 domain_name = g_strdup(hp->h_name);
1560 debug_print("domain name = %s\n", domain_name);
1566 off_t get_file_size(const gchar *file)
1570 if (stat(file, &s) < 0) {
1571 FILE_OP_ERROR(file, "stat");
1578 off_t get_file_size_as_crlf(const gchar *file)
1582 gchar buf[BUFFSIZE];
1584 if ((fp = fopen(file, "rb")) == NULL) {
1585 FILE_OP_ERROR(file, "fopen");
1589 while (fgets(buf, sizeof(buf), fp) != NULL) {
1591 size += strlen(buf) + 2;
1595 FILE_OP_ERROR(file, "fgets");
1604 off_t get_left_file_size(FILE *fp)
1610 if ((pos = ftell(fp)) < 0) {
1614 if (fseek(fp, 0L, SEEK_END) < 0) {
1618 if ((end = ftell(fp)) < 0) {
1623 if (fseek(fp, pos, SEEK_SET) < 0) {
1631 gboolean file_exist(const gchar *file, gboolean allow_fifo)
1638 if (stat(file, &s) < 0) {
1639 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1643 if (S_ISREG(s.st_mode) || (allow_fifo && S_ISFIFO(s.st_mode)))
1649 gboolean is_dir_exist(const gchar *dir)
1656 if (stat(dir, &s) < 0) {
1657 if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
1661 if (S_ISDIR(s.st_mode))
1667 gboolean is_file_entry_exist(const gchar *file)
1674 if (stat(file, &s) < 0) {
1675 if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
1682 gint change_dir(const gchar *dir)
1684 gchar *prevdir = NULL;
1687 prevdir = g_get_current_dir();
1689 if (chdir(dir) < 0) {
1690 FILE_OP_ERROR(dir, "chdir");
1691 if (debug_mode) g_free(prevdir);
1693 } else if (debug_mode) {
1696 cwd = g_get_current_dir();
1697 if (strcmp(prevdir, cwd) != 0)
1698 g_print("current dir: %s\n", cwd);
1706 gint make_dir(const gchar *dir)
1708 if (mkdir(dir, S_IRWXU) < 0) {
1709 FILE_OP_ERROR(dir, "mkdir");
1712 if (chmod(dir, S_IRWXU) < 0)
1713 FILE_OP_ERROR(dir, "chmod");
1718 gint make_dir_hier(const gchar *dir)
1723 for (p = dir; (p = strchr(p, G_DIR_SEPARATOR)) != NULL; p++) {
1724 parent_dir = g_strndup(dir, p - dir);
1725 if (*parent_dir != '\0') {
1726 if (!is_dir_exist(parent_dir)) {
1727 if (make_dir(parent_dir) < 0) {
1736 if (!is_dir_exist(dir)) {
1737 if (make_dir(dir) < 0)
1744 gint remove_all_files(const gchar *dir)
1750 prev_dir = g_get_current_dir();
1752 if (chdir(dir) < 0) {
1753 FILE_OP_ERROR(dir, "chdir");
1758 if ((dp = opendir(".")) == NULL) {
1759 FILE_OP_ERROR(dir, "opendir");
1764 while ((d = readdir(dp)) != NULL) {
1765 if (!strcmp(d->d_name, ".") ||
1766 !strcmp(d->d_name, ".."))
1769 if (unlink(d->d_name) < 0)
1770 FILE_OP_ERROR(d->d_name, "unlink");
1775 if (chdir(prev_dir) < 0) {
1776 FILE_OP_ERROR(prev_dir, "chdir");
1786 gint remove_numbered_files(const gchar *dir, guint first, guint last)
1793 prev_dir = g_get_current_dir();
1795 if (chdir(dir) < 0) {
1796 FILE_OP_ERROR(dir, "chdir");
1801 if ((dp = opendir(".")) == NULL) {
1802 FILE_OP_ERROR(dir, "opendir");
1807 while ((d = readdir(dp)) != NULL) {
1808 fileno = to_number(d->d_name);
1809 if (fileno >= 0 && first <= fileno && fileno <= last) {
1810 if (is_dir_exist(d->d_name))
1812 if (unlink(d->d_name) < 0)
1813 FILE_OP_ERROR(d->d_name, "unlink");
1819 if (chdir(prev_dir) < 0) {
1820 FILE_OP_ERROR(prev_dir, "chdir");
1830 gint remove_numbered_files_not_in_list(const gchar *dir, GSList *numberlist)
1837 prev_dir = g_get_current_dir();
1839 if (chdir(dir) < 0) {
1840 FILE_OP_ERROR(dir, "chdir");
1845 if ((dp = opendir(".")) == NULL) {
1846 FILE_OP_ERROR(dir, "opendir");
1851 while ((d = readdir(dp)) != NULL) {
1852 fileno = to_number(d->d_name);
1853 if (fileno >= 0 && (g_slist_find(numberlist, GINT_TO_POINTER(fileno)) == NULL)) {
1854 debug_print("removing unwanted file %d from %s\n", fileno, dir);
1855 if (is_dir_exist(d->d_name))
1857 if (unlink(d->d_name) < 0)
1858 FILE_OP_ERROR(d->d_name, "unlink");
1864 if (chdir(prev_dir) < 0) {
1865 FILE_OP_ERROR(prev_dir, "chdir");
1875 gint remove_all_numbered_files(const gchar *dir)
1877 return remove_numbered_files(dir, 0, UINT_MAX);
1880 gint remove_expired_files(const gchar *dir, guint hours)
1887 time_t mtime, now, expire_time;
1889 prev_dir = g_get_current_dir();
1891 if (chdir(dir) < 0) {
1892 FILE_OP_ERROR(dir, "chdir");
1897 if ((dp = opendir(".")) == NULL) {
1898 FILE_OP_ERROR(dir, "opendir");
1904 expire_time = hours * 60 * 60;
1906 while ((d = readdir(dp)) != NULL) {
1907 fileno = to_number(d->d_name);
1909 if (stat(d->d_name, &s) < 0) {
1910 FILE_OP_ERROR(d->d_name, "stat");
1913 if (S_ISDIR(s.st_mode))
1915 mtime = MAX(s.st_mtime, s.st_atime);
1916 if (now - mtime > expire_time) {
1917 if (unlink(d->d_name) < 0)
1918 FILE_OP_ERROR(d->d_name, "unlink");
1925 if (chdir(prev_dir) < 0) {
1926 FILE_OP_ERROR(prev_dir, "chdir");
1936 gint remove_dir_recursive(const gchar *dir)
1943 /* g_print("dir = %s\n", dir); */
1945 if (stat(dir, &s) < 0) {
1946 FILE_OP_ERROR(dir, "stat");
1947 if (ENOENT == errno) return 0;
1951 if (!S_ISDIR(s.st_mode)) {
1952 if (unlink(dir) < 0) {
1953 FILE_OP_ERROR(dir, "unlink");
1960 prev_dir = g_get_current_dir();
1961 /* g_print("prev_dir = %s\n", prev_dir); */
1963 if (!path_cmp(prev_dir, dir)) {
1965 if (chdir("..") < 0) {
1966 FILE_OP_ERROR(dir, "chdir");
1969 prev_dir = g_get_current_dir();
1972 if (chdir(dir) < 0) {
1973 FILE_OP_ERROR(dir, "chdir");
1978 if ((dp = opendir(".")) == NULL) {
1979 FILE_OP_ERROR(dir, "opendir");
1985 /* remove all files in the directory */
1986 while ((d = readdir(dp)) != NULL) {
1987 if (!strcmp(d->d_name, ".") ||
1988 !strcmp(d->d_name, ".."))
1991 if (stat(d->d_name, &s) < 0) {
1992 FILE_OP_ERROR(d->d_name, "stat");
1996 /* g_print("removing %s\n", d->d_name); */
1998 if (S_ISDIR(s.st_mode)) {
1999 if (remove_dir_recursive(d->d_name) < 0) {
2000 g_warning("can't remove directory\n");
2004 if (unlink(d->d_name) < 0)
2005 FILE_OP_ERROR(d->d_name, "unlink");
2011 if (chdir(prev_dir) < 0) {
2012 FILE_OP_ERROR(prev_dir, "chdir");
2019 if (rmdir(dir) < 0) {
2020 FILE_OP_ERROR(dir, "rmdir");
2028 /* this seems to be slower than the stdio version... */
2029 gint copy_file(const gchar *src, const gchar *dest)
2031 gint src_fd, dest_fd;
2035 gchar *dest_bak = NULL;
2037 if ((src_fd = open(src, O_RDONLY)) < 0) {
2038 FILE_OP_ERROR(src, "open");
2042 if (is_file_exist(dest)) {
2043 dest_bak = g_strconcat(dest, ".bak", NULL);
2044 if (rename(dest, dest_bak) < 0) {
2045 FILE_OP_ERROR(dest, "rename");
2052 if ((dest_fd = open(dest, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
2053 FILE_OP_ERROR(dest, "open");
2056 if (rename(dest_bak, dest) < 0)
2057 FILE_OP_ERROR(dest_bak, "rename");
2063 while ((n_read = read(src_fd, buf, sizeof(buf))) > 0) {
2068 n_write = write(dest_fd, bufp, len);
2070 g_warning(_("writing to %s failed.\n"), dest);
2075 if (rename(dest_bak, dest) < 0)
2076 FILE_OP_ERROR(dest_bak, "rename");
2089 if (n_read < 0 || get_file_size(src) != get_file_size(dest)) {
2090 g_warning(_("File copy from %s to %s failed.\n"), src, dest);
2093 if (rename(dest_bak, dest) < 0)
2094 FILE_OP_ERROR(dest_bak, "rename");
2107 * Append src file body to the tail of dest file.
2108 * Now keep_backup has no effects.
2110 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2112 FILE *src_fp, *dest_fp;
2116 gboolean err = FALSE;
2118 if ((src_fp = fopen(src, "rb")) == NULL) {
2119 FILE_OP_ERROR(src, "fopen");
2123 if ((dest_fp = fopen(dest, "ab")) == NULL) {
2124 FILE_OP_ERROR(dest, "fopen");
2129 if (change_file_mode_rw(dest_fp, dest) < 0) {
2130 FILE_OP_ERROR(dest, "chmod");
2131 g_warning(_("can't change file mode\n"));
2134 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2135 if (n_read < sizeof(buf) && ferror(src_fp))
2137 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2138 g_warning(_("writing to %s failed.\n"), dest);
2146 if (ferror(src_fp)) {
2147 FILE_OP_ERROR(src, "fread");
2151 if (fclose(dest_fp) == EOF) {
2152 FILE_OP_ERROR(dest, "fclose");
2164 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
2166 FILE *src_fp, *dest_fp;
2169 gchar *dest_bak = NULL;
2170 gboolean err = FALSE;
2172 if ((src_fp = fopen(src, "rb")) == NULL) {
2173 FILE_OP_ERROR(src, "fopen");
2176 if (is_file_exist(dest)) {
2177 dest_bak = g_strconcat(dest, ".bak", NULL);
2178 if (rename(dest, dest_bak) < 0) {
2179 FILE_OP_ERROR(dest, "rename");
2186 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2187 FILE_OP_ERROR(dest, "fopen");
2190 if (rename(dest_bak, dest) < 0)
2191 FILE_OP_ERROR(dest_bak, "rename");
2197 if (change_file_mode_rw(dest_fp, dest) < 0) {
2198 FILE_OP_ERROR(dest, "chmod");
2199 g_warning(_("can't change file mode\n"));
2202 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
2203 if (n_read < sizeof(buf) && ferror(src_fp))
2205 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2206 g_warning(_("writing to %s failed.\n"), dest);
2211 if (rename(dest_bak, dest) < 0)
2212 FILE_OP_ERROR(dest_bak, "rename");
2219 if (ferror(src_fp)) {
2220 FILE_OP_ERROR(src, "fread");
2224 if (fclose(dest_fp) == EOF) {
2225 FILE_OP_ERROR(dest, "fclose");
2232 if (rename(dest_bak, dest) < 0)
2233 FILE_OP_ERROR(dest_bak, "rename");
2239 if (keep_backup == FALSE && dest_bak)
2247 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
2249 if (overwrite == FALSE && is_file_exist(dest)) {
2250 g_warning("move_file(): file %s already exists.", dest);
2254 if (rename(src, dest) == 0) return 0;
2256 if (EXDEV != errno) {
2257 FILE_OP_ERROR(src, "rename");
2261 if (copy_file(src, dest, FALSE) < 0) return -1;
2268 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
2272 gint bytes_left, to_read;
2274 gboolean err = FALSE;
2276 if (fseek(fp, offset, SEEK_SET) < 0) {
2281 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2282 FILE_OP_ERROR(dest, "fopen");
2286 if (change_file_mode_rw(dest_fp, dest) < 0) {
2287 FILE_OP_ERROR(dest, "chmod");
2288 g_warning("can't change file mode\n");
2291 bytes_left = length;
2292 to_read = MIN(bytes_left, sizeof(buf));
2294 while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
2295 if (n_read < to_read && ferror(fp))
2297 if (fwrite(buf, n_read, 1, dest_fp) < 1) {
2298 g_warning(_("writing to %s failed.\n"), dest);
2303 bytes_left -= n_read;
2304 if (bytes_left == 0)
2306 to_read = MIN(bytes_left, sizeof(buf));
2313 if (fclose(dest_fp) == EOF) {
2314 FILE_OP_ERROR(dest, "fclose");
2326 /* convert line endings into CRLF. If the last line doesn't end with
2327 * linebreak, add it.
2329 gint canonicalize_file(const gchar *src, const gchar *dest)
2331 FILE *src_fp, *dest_fp;
2332 gchar buf[BUFFSIZE];
2334 gboolean err = FALSE;
2335 gboolean last_linebreak = FALSE;
2337 if ((src_fp = fopen(src, "rb")) == NULL) {
2338 FILE_OP_ERROR(src, "fopen");
2342 if ((dest_fp = fopen(dest, "wb")) == NULL) {
2343 FILE_OP_ERROR(dest, "fopen");
2348 if (change_file_mode_rw(dest_fp, dest) < 0) {
2349 FILE_OP_ERROR(dest, "chmod");
2350 g_warning("can't change file mode\n");
2353 while (fgets(buf, sizeof(buf), src_fp) != NULL) {
2357 if (len == 0) break;
2358 last_linebreak = FALSE;
2360 if (buf[len - 1] != '\n') {
2361 last_linebreak = TRUE;
2362 r = fputs(buf, dest_fp);
2363 } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
2364 r = fputs(buf, dest_fp);
2367 r = fwrite(buf, len - 1, 1, dest_fp);
2372 r = fputs("\r\n", dest_fp);
2376 g_warning("writing to %s failed.\n", dest);
2384 if (last_linebreak == TRUE) {
2385 if (fputs("\r\n", dest_fp) == EOF)
2389 if (ferror(src_fp)) {
2390 FILE_OP_ERROR(src, "fread");
2394 if (fclose(dest_fp) == EOF) {
2395 FILE_OP_ERROR(dest, "fclose");
2407 gint canonicalize_file_replace(const gchar *file)
2411 tmp_file = get_tmp_file();
2413 if (canonicalize_file(file, tmp_file) < 0) {
2418 if (move_file(tmp_file, file, TRUE) < 0) {
2419 g_warning("can't replace %s .\n", file);
2429 gint change_file_mode_rw(FILE *fp, const gchar *file)
2432 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
2434 return chmod(file, S_IRUSR|S_IWUSR);
2438 FILE *my_tmpfile(void)
2441 const gchar suffix[] = ".XXXXXX";
2442 const gchar *tmpdir;
2444 const gchar *progname;
2450 tmpdir = get_tmp_dir();
2451 tmplen = strlen(tmpdir);
2452 progname = g_get_prgname();
2453 proglen = strlen(progname);
2454 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
2457 memcpy(fname, tmpdir, tmplen);
2458 fname[tmplen] = G_DIR_SEPARATOR;
2459 memcpy(fname + tmplen + 1, progname, proglen);
2460 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
2462 fd = mkstemp(fname);
2468 fp = fdopen(fd, "w+b");
2473 #endif /* HAVE_MKSTEMP */
2478 FILE *str_open_as_stream(const gchar *str)
2483 g_return_val_if_fail(str != NULL, NULL);
2487 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
2492 if (len == 0) return fp;
2494 if (fwrite(str, len, 1, fp) != 1) {
2495 FILE_OP_ERROR("str_open_as_stream", "fwrite");
2504 gint str_write_to_file(const gchar *str, const gchar *file)
2509 g_return_val_if_fail(str != NULL, -1);
2510 g_return_val_if_fail(file != NULL, -1);
2512 if ((fp = fopen(file, "wb")) == NULL) {
2513 FILE_OP_ERROR(file, "fopen");
2523 if (fwrite(str, len, 1, fp) != 1) {
2524 FILE_OP_ERROR(file, "fwrite");
2530 if (fclose(fp) == EOF) {
2531 FILE_OP_ERROR(file, "fclose");
2539 gchar *file_read_to_str(const gchar *file)
2547 g_return_val_if_fail(file != NULL, NULL);
2549 if ((fp = fopen(file, "rb")) == NULL) {
2550 FILE_OP_ERROR(file, "fopen");
2554 array = g_byte_array_new();
2556 while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
2557 if (n_read < sizeof(buf) && ferror(fp))
2559 g_byte_array_append(array, buf, n_read);
2563 FILE_OP_ERROR(file, "fread");
2565 g_byte_array_free(array, TRUE);
2572 g_byte_array_append(array, buf, 1);
2573 str = (gchar *)array->data;
2574 g_byte_array_free(array, FALSE);
2579 gint execute_async(gchar *const argv[])
2583 if ((pid = fork()) < 0) {
2588 if (pid == 0) { /* child process */
2591 if ((gch_pid = fork()) < 0) {
2596 if (gch_pid == 0) { /* grandchild process */
2597 execvp(argv[0], argv);
2606 waitpid(pid, NULL, 0);
2611 gint execute_sync(gchar *const argv[])
2615 if ((pid = fork()) < 0) {
2620 if (pid == 0) { /* child process */
2621 execvp(argv[0], argv);
2627 waitpid(pid, NULL, 0);
2632 gint execute_command_line(const gchar *cmdline, gboolean async)
2637 argv = strsplit_with_quote(cmdline, " ", 0);
2640 ret = execute_async(argv);
2642 ret = execute_sync(argv);
2648 static gint is_unchanged_uri_char(char c)
2660 void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
2666 for(i = 0; i < strlen(uri) ; i++) {
2667 if (is_unchanged_uri_char(uri[i])) {
2668 if (k + 2 >= bufsize)
2670 encoded_uri[k++] = uri[i];
2673 char * hexa = "0123456789ABCDEF";
2675 if (k + 4 >= bufsize)
2677 encoded_uri[k++] = '%';
2678 encoded_uri[k++] = hexa[uri[i] / 16];
2679 encoded_uri[k++] = hexa[uri[i] % 16];
2685 /* Converts two-digit hexadecimal to decimal. Used for unescaping escaped
2688 static gint axtoi(const gchar *hexstr)
2690 gint hi, lo, result;
2693 if ('0' <= hi && hi <= '9') {
2696 if ('a' <= hi && hi <= 'f') {
2699 if ('A' <= hi && hi <= 'F') {
2704 if ('0' <= lo && lo <= '9') {
2707 if ('a' <= lo && lo <= 'f') {
2710 if ('A' <= lo && lo <= 'F') {
2713 result = lo + (16 * hi);
2718 /* Decodes URL-Encoded strings (i.e. strings in which spaces are replaced by
2719 * plusses, and escape characters are used)
2722 void decode_uri(gchar *decoded_uri, const gchar *encoded_uri)
2724 const gchar *encoded;
2727 encoded = encoded_uri;
2728 decoded = decoded_uri;
2731 if (*encoded == '%') {
2733 if (isxdigit(encoded[0])
2734 && isxdigit(encoded[1])) {
2735 *decoded = (gchar) axtoi(encoded);
2740 else if (*encoded == '+') {
2746 *decoded = *encoded;
2756 gint open_uri(const gchar *uri, const gchar *cmdline)
2758 static gchar *default_cmdline = "netscape -remote openURL(%s,raise)";
2759 gchar buf[BUFFSIZE];
2761 gchar encoded_uri[BUFFSIZE];
2763 g_return_val_if_fail(uri != NULL, -1);
2765 /* an option to choose whether to use encode_uri or not ? */
2766 encode_uri(encoded_uri, BUFFSIZE, uri);
2769 (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
2770 !strchr(p + 2, '%'))
2771 g_snprintf(buf, sizeof(buf), cmdline, encoded_uri);
2774 g_warning(_("Open URI command line is invalid: `%s'"),
2776 g_snprintf(buf, sizeof(buf), default_cmdline, encoded_uri);
2779 execute_command_line(buf, TRUE);
2784 time_t remote_tzoffset_sec(const gchar *zone)
2786 static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
2792 time_t remoteoffset;
2794 strncpy(zone3, zone, 3);
2798 if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
2799 (c == '+' || c == '-')) {
2800 remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
2802 remoteoffset = -remoteoffset;
2803 } else if (!strncmp(zone, "UT" , 2) ||
2804 !strncmp(zone, "GMT", 2)) {
2806 } else if (strlen(zone3) == 3 &&
2807 (p = strstr(ustzstr, zone3)) != NULL &&
2808 (p - ustzstr) % 3 == 0) {
2809 iustz = ((gint)(p - ustzstr) / 3 + 1) / 2 - 8;
2810 remoteoffset = iustz * 3600;
2811 } else if (strlen(zone3) == 1) {
2813 case 'Z': remoteoffset = 0; break;
2814 case 'A': remoteoffset = -1; break;
2815 case 'B': remoteoffset = -2; break;
2816 case 'C': remoteoffset = -3; break;
2817 case 'D': remoteoffset = -4; break;
2818 case 'E': remoteoffset = -5; break;
2819 case 'F': remoteoffset = -6; break;
2820 case 'G': remoteoffset = -7; break;
2821 case 'H': remoteoffset = -8; break;
2822 case 'I': remoteoffset = -9; break;
2823 case 'K': remoteoffset = -10; break; /* J is not used */
2824 case 'L': remoteoffset = -11; break;
2825 case 'M': remoteoffset = -12; break;
2826 case 'N': remoteoffset = 1; break;
2827 case 'O': remoteoffset = 2; break;
2828 case 'P': remoteoffset = 3; break;
2829 case 'Q': remoteoffset = 4; break;
2830 case 'R': remoteoffset = 5; break;
2831 case 'S': remoteoffset = 6; break;
2832 case 'T': remoteoffset = 7; break;
2833 case 'U': remoteoffset = 8; break;
2834 case 'V': remoteoffset = 9; break;
2835 case 'W': remoteoffset = 10; break;
2836 case 'X': remoteoffset = 11; break;
2837 case 'Y': remoteoffset = 12; break;
2838 default: remoteoffset = 0; break;
2840 remoteoffset = remoteoffset * 3600;
2843 return remoteoffset;
2846 time_t tzoffset_sec(time_t *now)
2852 lt = localtime(now);
2854 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2856 if (lt->tm_year < gmt.tm_year)
2858 else if (lt->tm_year > gmt.tm_year)
2860 else if (lt->tm_yday < gmt.tm_yday)
2862 else if (lt->tm_yday > gmt.tm_yday)
2865 if (off >= 24 * 60) /* should be impossible */
2866 off = 23 * 60 + 59; /* if not, insert silly value */
2867 if (off <= -24 * 60)
2868 off = -(23 * 60 + 59);
2877 /* calculate timezone offset */
2878 gchar *tzoffset(time_t *now)
2880 static gchar offset_string[6];
2886 lt = localtime(now);
2888 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
2890 if (lt->tm_year < gmt.tm_year)
2892 else if (lt->tm_year > gmt.tm_year)
2894 else if (lt->tm_yday < gmt.tm_yday)
2896 else if (lt->tm_yday > gmt.tm_yday)
2904 if (off >= 24 * 60) /* should be impossible */
2905 off = 23 * 60 + 59; /* if not, insert silly value */
2907 sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
2909 return offset_string;
2912 void get_rfc822_date(gchar *buf, gint len)
2916 gchar day[4], mon[4];
2917 gint dd, hh, mm, ss, yyyy;
2922 sscanf(asctime(lt), "%3s %3s %d %d:%d:%d %d\n",
2923 day, mon, &dd, &hh, &mm, &ss, &yyyy);
2924 g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
2925 day, dd, mon, yyyy, hh, mm, ss, tzoffset(&t));
2928 static FILE *log_fp = NULL;
2930 void set_log_file(const gchar *filename)
2933 log_fp = fopen(filename, "wb");
2935 FILE_OP_ERROR(filename, "fopen");
2938 void close_log_file(void)
2946 static guint log_verbosity_count = 0;
2948 void log_verbosity_set(gboolean verbose)
2951 log_verbosity_count++;
2952 else if (log_verbosity_count > 0)
2953 log_verbosity_count--;
2956 void debug_print_real(const gchar *format, ...)
2959 gchar buf[BUFFSIZE];
2961 if (!debug_mode) return;
2963 va_start(args, format);
2964 g_vsnprintf(buf, sizeof(buf), format, args);
2972 void log_print(const gchar *format, ...)
2975 gchar buf[BUFFSIZE + TIME_LEN];
2979 strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
2981 va_start(args, format);
2982 g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
2985 if (debug_mode) fputs(buf, stdout);
2986 log_window_append(buf, LOG_NORMAL);
2991 if (log_verbosity_count)
2992 statusbar_puts_all(buf + TIME_LEN);
2995 void log_message(const gchar *format, ...)
2998 gchar buf[BUFFSIZE + TIME_LEN];
3002 strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
3004 va_start(args, format);
3005 g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
3008 if (debug_mode) g_message("%s", buf + TIME_LEN);
3009 log_window_append(buf + TIME_LEN, LOG_MSG);
3011 fwrite(buf, TIME_LEN, 1, log_fp);
3012 fputs("* message: ", log_fp);
3013 fputs(buf + TIME_LEN, log_fp);
3016 statusbar_puts_all(buf + TIME_LEN);
3019 void log_warning(const gchar *format, ...)
3022 gchar buf[BUFFSIZE + TIME_LEN];
3026 strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
3028 va_start(args, format);
3029 g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
3032 g_warning("%s", buf);
3033 log_window_append(buf + TIME_LEN, LOG_WARN);
3035 fwrite(buf, TIME_LEN, 1, log_fp);
3036 fputs("** warning: ", log_fp);
3037 fputs(buf + TIME_LEN, log_fp);
3042 void log_error(const gchar *format, ...)
3045 gchar buf[BUFFSIZE + TIME_LEN];
3049 strftime(buf, TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
3051 va_start(args, format);
3052 g_vsnprintf(buf + TIME_LEN, BUFFSIZE, format, args);
3055 g_warning("%s", buf);
3056 log_window_append(buf + TIME_LEN, LOG_ERROR);
3058 fwrite(buf, TIME_LEN, 1, log_fp);
3059 fputs("*** error: ", log_fp);
3060 fputs(buf + TIME_LEN, log_fp);
3066 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
3068 if (subject == NULL)
3071 if (g_strncasecmp(subject, "Re: ", 4) == 0)
3072 return g_hash_table_lookup(subject_table, subject + 4);
3074 return g_hash_table_lookup(subject_table, subject);
3077 void subject_table_insert(GHashTable *subject_table, gchar * subject,
3080 if (subject == NULL)
3084 if (g_strcasecmp(subject, "Re:") == 0)
3086 if (g_strcasecmp(subject, "Re: ") == 0)
3089 if (g_strncasecmp(subject, "Re: ", 4) == 0)
3090 g_hash_table_insert(subject_table, subject + 4, data);
3092 g_hash_table_insert(subject_table, subject, data);
3095 void subject_table_remove(GHashTable *subject_table, gchar * subject)
3097 if (subject == NULL)
3100 if (g_strncasecmp(subject, "Re: ", 4) == 0)
3101 g_hash_table_remove(subject_table, subject + 4);
3103 g_hash_table_remove(subject_table, subject);
3106 gboolean subject_is_reply(const gchar *subject)
3108 /* XXX: just simply here so someone can handle really
3109 * advanced Re: detection like "Re[4]", "ANTW:" or
3110 * Re: Re: Re: Re: Re: Re: Re: Re:" stuff. */
3111 if (subject == NULL) return FALSE;
3112 else return 0 == g_strncasecmp(subject, "Re: ", 4);
3115 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
3119 *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
3120 fd = mkstemp(*filename);
3122 return fdopen(fd, "w+");