/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed-Claws Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "defs.h"
#include <glib.h>
+#ifdef ENABLE_NLS
#include <glib/gi18n.h>
+#else
+#define _(a) (a)
+#define N_(a) (a)
+#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <regex.h>
+
+#ifdef G_OS_UNIX
#include <sys/utsname.h>
+#endif
#ifdef G_OS_WIN32
# include <direct.h>
# include <io.h>
+# include <fcntl.h>
#endif
#include "utils.h"
#define BUFFSIZE 8192
static gboolean debug_mode = FALSE;
+#ifdef G_OS_WIN32
+static GSList *tempfiles=NULL;
+#endif
#if !GLIB_CHECK_VERSION(2, 7, 0) && !defined(G_OS_UNIX)
}
#endif /* GLIB_CHECK_VERSION && G_OS_UNIX */
+
+#ifdef G_OS_WIN32
+gint mkstemp_name(const gchar *template, gchar **name_used)
+{
+ static gulong count=0; /* W32-_mktemp only supports up to 27
+ tempfiles... */
+ int tmpfd;
+
+ *name_used = g_strdup_printf("%s.%ld",_mktemp(template),count++);
+ tmpfd = open (*name_used, (O_CREAT | O_RDWR | O_BINARY
+ | S_IREAD | S_IWRITE));
+
+ tempfiles=g_slist_append(tempfiles, g_strdup(*name_used));
+ if (tmpfd<0) {
+ perror(g_strdup_printf("cant create %s",*name_used));
+ return -1;
+ }
+ else
+ return tmpfd;
+}
+#endif /* G_OS_WIN32 */
+
+#ifdef G_OS_WIN32
+gint mkstemp(const gchar *template)
+{
+ gchar *dummyname;
+ gint res = mkstemp_name(template, &dummyname);
+ g_free(dummyname);
+ return res;
+}
+#endif /* G_OS_WIN32 */
+
void list_free_strings(GList *list)
{
list = g_list_first(list);
gint path_cmp(const gchar *s1, const gchar *s2)
{
gint len1, len2;
+ int rc;
+#ifdef G_OS_WIN32
+ gchar *s1buf, *s2buf;
+#endif
if (s1 == NULL || s2 == NULL) return -1;
if (*s1 == '\0' || *s2 == '\0') return -1;
+#ifdef G_OS_WIN32
+ s1buf = g_strdup (s1);
+ s2buf = g_strdup (s2);
+ subst_char (s1buf, '/', G_DIR_SEPARATOR);
+ subst_char (s2buf, '/', G_DIR_SEPARATOR);
+ s1 = s1buf;
+ s2 = s2buf;
+#endif /* !G_OS_WIN32 */
+
len1 = strlen(s1);
len2 = strlen(s2);
if (s1[len1 - 1] == G_DIR_SEPARATOR) len1--;
if (s2[len2 - 1] == G_DIR_SEPARATOR) len2--;
- return strncmp(s1, s2, MAX(len1, len2));
+ rc = strncmp(s1, s2, MAX(len1, len2));
+#ifdef G_OS_WIN32
+ g_free (s1buf);
+ g_free (s2buf);
+#endif /* !G_OS_WIN32 */
+ return rc;
}
/* remove trailing return code */
last = g_list_last(list);
if (last) {
g_free(last->data);
- g_list_remove(list, last->data);
+ list = g_list_remove(list, last->data);
}
}
{
if (!str)
return;
+#ifdef G_OS_WIN32
+ subst_chars(str, "\t\r\n\\/*:", '_');
+#else
subst_chars(str, "\t\r\n\\/*", '_');
+#endif
}
void subst_for_shellsafe_filename(gchar *str)
return 0;
}
+\f
+#ifdef G_OS_WIN32
+#include <windows.h>
+#ifndef CSIDL_APPDATA
+#define CSIDL_APPDATA 0x001a
+#endif
+#ifndef CSIDL_LOCAL_APPDATA
+#define CSIDL_LOCAL_APPDATA 0x001c
+#endif
+#ifndef CSIDL_FLAG_CREATE
+#define CSIDL_FLAG_CREATE 0x8000
+#endif
+#define DIM(v) (sizeof(v)/sizeof((v)[0]))
+
+#define RTLD_LAZY 0
+const char *
+w32_strerror (int w32_errno)
+{
+ static char strerr[256];
+ int ec = (int)GetLastError ();
+
+ if (w32_errno == 0)
+ w32_errno = ec;
+ FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, w32_errno,
+ MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
+ strerr, DIM (strerr)-1, NULL);
+ return strerr;
+}
+
+static __inline__ void *
+dlopen (const char * name, int flag)
+{
+ void * hd = LoadLibrary (name);
+ return hd;
+}
+
+static __inline__ void *
+dlsym (void * hd, const char * sym)
+{
+ if (hd && sym)
+ {
+ void * fnc = GetProcAddress (hd, sym);
+ if (!fnc)
+ return NULL;
+ return fnc;
+ }
+ return NULL;
+}
+
+
+static __inline__ const char *
+dlerror (void)
+{
+ return w32_strerror (0);
+}
+
+
+static __inline__ int
+dlclose (void * hd)
+{
+ if (hd)
+ {
+ FreeLibrary (hd);
+ return 0;
+ }
+ return -1;
+}
+
+static HRESULT
+w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
+{
+ static int initialized;
+ static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
+
+ if (!initialized)
+ {
+ static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
+ void *handle;
+ int i;
+
+ initialized = 1;
+
+ for (i=0, handle = NULL; !handle && dllnames[i]; i++)
+ {
+ handle = dlopen (dllnames[i], RTLD_LAZY);
+ if (handle)
+ {
+ func = dlsym (handle, "SHGetFolderPathA");
+ if (!func)
+ {
+ dlclose (handle);
+ handle = NULL;
+ }
+ }
+ }
+ }
+
+ if (func)
+ return func (a,b,c,d,e);
+ else
+ return -1;
+}
+#endif
+
const gchar *get_home_dir(void)
{
#ifdef G_OS_WIN32
- static const gchar *home_dir = NULL;
+ static char home_dir[MAX_PATH] = "";
- if (!home_dir) {
- home_dir = g_get_home_dir();
- if (!home_dir)
- home_dir = "C:\\Sylpheed";
+ if (home_dir[0] == '\0')
+ {
+ if (w32_shgetfolderpath
+ (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
+ NULL, 0, home_dir) < 0)
+ strcpy (home_dir, "C:\\Sylpheed");
}
-
return home_dir;
#else
return g_get_home_dir();
static gchar *rc_dir = NULL;
if (!rc_dir)
-#ifdef G_OS_WIN32
- rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
- "Application Data", G_DIR_SEPARATOR_S,
- RC_DIR, NULL);
-#else
rc_dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
RC_DIR, NULL);
-#endif
return rc_dir;
}
return header_dir;
}
+/* Return the default directory for Plugins. */
+const gchar *get_plugin_dir(void)
+{
+#ifdef G_OS_WIN32
+ static gchar *plugin_dir = NULL;
+
+ if (!plugin_dir)
+ plugin_dir = g_strconcat(sylpheed_get_startup_dir(),
+ "\\lib\\sylpheed-claws\\plugins\\",
+ NULL);
+ return plugin_dir;
+#else
+ return PLUGINDIR;
+#endif
+}
+
const gchar *get_tmp_dir(void)
{
static gchar *tmp_dir = NULL;
return FALSE;
}
+
+/* Test on whether FILE is a relative file name. This is
+ * straightforward for Unix but more complex for Windows. */
+gboolean is_relative_filename(const gchar *file)
+{
+ if (!file)
+ return TRUE;
+#ifdef G_OS_WIN32
+ if ( *file == '\\' && file[1] == '\\' && strchr (file+2, '\\') )
+ return FALSE; /* Prefixed with a hostname - this can't
+ * be a relative name. */
+
+ if ( ((*file >= 'a' && *file <= 'z')
+ || (*file >= 'A' && *file <= 'Z'))
+ && file[1] == ':')
+ file += 2; /* Skip drive letter. */
+
+ return !(*file == '\\' || *file == '/');
+#else
+ return !(*file == G_DIR_SEPARATOR);
+#endif
+}
+
+
gboolean is_dir_exist(const gchar *dir)
{
if (dir == NULL)
* uniqueness if everything is either quoted-printable or base64
* encoded (note that conversion is allowed), but because MIME bodies
* may be nested, it may happen that the same boundary has already
- * been used. We avoid scanning the message for conflicts and hope the
- * best.
+ * been used.
*
* boundary := 0*69<bchars> bcharsnospace
* bchars := bcharsnospace / " "
static gchar tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"1234567890+_./=";
- gchar buf_uniq[17];
- gchar buf_date[64];
+ gchar buf_uniq[24];
gint i;
for (i = 0; i < sizeof(buf_uniq) - 1; i++)
buf_uniq[i] = tbl[g_random_int_range(0, sizeof(tbl) - 1)];
buf_uniq[i] = '\0';
- get_rfc822_date(buf_date, sizeof(buf_date));
- subst_char(buf_date, ' ', '_');
- subst_char(buf_date, ',', '_');
- subst_char(buf_date, ':', '_');
-
- return g_strdup_printf("%s_%s_%s", prefix ? prefix : "Multipart",
- buf_date, buf_uniq);
+ return g_strdup_printf("%s_%s", prefix ? prefix : "MP",
+ buf_uniq);
}
gint change_file_mode_rw(FILE *fp, const gchar *file)
FILE *my_tmpfile(void)
{
-#if HAVE_MKSTEMP
+#if HAVE_MKSTEMP || defined(G_OS_WIN32)
const gchar suffix[] = ".XXXXXX";
const gchar *tmpdir;
guint tmplen;
if (fd < 0)
return tmpfile();
+#ifndef G_OS_WIN32
g_unlink(fname);
+#endif
fp = fdopen(fd, "w+b");
if (!fp)
close(fd);
else
return fp;
-#endif /* HAVE_MKSTEMP */
+#endif /* HAVE_MKSTEMP || G_OS_WIN32 */
return tmpfile();
}
FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
{
int fd;
-
+#ifdef G_OS_WIN32
+ char *template = g_strdup_printf ("%s%csylpheed.XXXXXX",
+ dir, G_DIR_SEPARATOR);
+ fd = mkstemp_name(template, filename);
+ g_free(template);
+#else
*filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR);
fd = mkstemp(*filename);
-
+#endif
return fdopen(fd, "w+");
}
return 0;
}
+gint open_txt_editor(const gchar *filepath, const gchar *cmdline)
+{
+ gchar buf[BUFFSIZE];
+ gchar *p;
+
+ g_return_val_if_fail(filepath != NULL, -1);
+
+ if (cmdline &&
+ (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
+ !strchr(p + 2, '%'))
+ g_snprintf(buf, sizeof(buf), cmdline, filepath);
+ else {
+ if (cmdline)
+ g_warning("Open Text Editor command line is invalid "
+ "(there must be only one '%%s'): %s",
+ cmdline);
+ g_snprintf(buf, sizeof(buf), DEFAULT_EDITOR_CMD, filepath);
+ }
+
+ execute_command_line(buf, TRUE);
+
+ return 0;
+}
+
time_t remote_tzoffset_sec(const gchar *zone)
{
static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
g_print("%s", buf);
}
+
+const char * debug_srcname(const char *file)
+{
+ const char *s = strrchr (file, '/');
+ return s? s+1:file;
+}
+
+
void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
{
if (subject == NULL)
/* get_uri_part() - retrieves a URI starting from scanpos.
Returns TRUE if succesful */
gboolean get_uri_part(const gchar *start, const gchar *scanpos,
- const gchar **bp, const gchar **ep)
+ const gchar **bp, const gchar **ep, gboolean hdr)
{
const gchar *ep_;
/* find end point of URI */
for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
- if (!isgraph(*(const guchar *)ep_) ||
+ if (!g_ascii_isgraph(*(const guchar *)ep_) ||
!IS_ASCII(*(const guchar *)ep_) ||
strchr("[]{}()<>\"", *ep_))
break;
* should pass some URI type to this function and decide on that whether
* to perform punctuation stripping */
-#define IS_REAL_PUNCT(ch) (ispunct(ch) && ((ch) != '/'))
+#define IS_REAL_PUNCT(ch) (g_ascii_ispunct(ch) && !strchr("/?=", ch))
for (; ep_ - 1 > scanpos + 1 &&
- IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
+ IS_REAL_PUNCT(*(ep_ - 1));
ep_--)
;
/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
gboolean get_email_part(const gchar *start, const gchar *scanpos,
- const gchar **bp, const gchar **ep)
+ const gchar **bp, const gchar **ep, gboolean hdr)
{
/* more complex than the uri part because we need to scan back and forward starting from
* the scan position. */
g_return_val_if_fail(bp != NULL, FALSE);
g_return_val_if_fail(ep != NULL, FALSE);
+ if (hdr) {
+search_again:
+ /* go to the real start */
+ if (start[0] == ',')
+ start++;
+ if (start[0] == ';')
+ start++;
+ while (start[0] == ' ' || start[0] == '\t')
+ start++;
+
+ *bp = start;
+
+ /* find end (either , or ; or end of line) */
+ if (strstr(start, ",") && strstr(start, ";"))
+ *ep = strstr(start,",") < strstr(start, ";")
+ ? strstr(start, ",") : strstr(start, ";");
+ else if (strstr(start, ","))
+ *ep = strstr(start, ",");
+ else if (strstr(start, ";"))
+ *ep = strstr(start, ";");
+ else
+ *ep = start+strlen(start);
+
+ /* check there's still an @ in that, or search
+ * further if possible */
+ if (strstr(start, "@") && strstr(start, "@") < *ep)
+ return TRUE;
+ else if (*ep < start+strlen(start)) {
+ start = *ep;
+ goto search_again;
+ } else
+ return FALSE;
+ }
+
if (!dom_tab)
dom_tab = create_domain_tab();
g_return_val_if_fail(dom_tab, FALSE);
&& (((bp_ + 1) < ep_) && isalnum(*(bp_ + 1)))) {
/* hyphens are allowed, but only in
between alnums */
- } else if (!strchr(",;:=?./+<>!&\r\n\t", *bp_)) {
+ } else if (strchr(" \"'", *bp_)) {
/* but anything not being a punctiation
is ok */
} else {
bp_++;
+ /* scan forward (should start with an alnum) */
+ for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
+ ;
#undef PEEK_STACK
#undef PUSH_STACK
#undef POP_STACK
#undef IN_STACK
#undef FULL_STACK
- /* scan forward (should start with an alnum) */
- for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
- ;
- *ep = ep_;
*bp = bp_;
+ *ep = ep_;
return result;
}
result = mailcap_get_command_in_file("/etc/mailcap", type);
return result;
}
+
+gint copy_dir(const gchar *src, const gchar *dst)
+{
+ GDir *dir;
+ const gchar *name;
+
+ if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
+ g_warning("failed to open directory: %s\n", src);
+ return -1;
+ }
+
+ if (make_dir(dst) < 0)
+ return -1;
+
+ while ((name = g_dir_read_name(dir)) != NULL) {
+ gchar *old_file, *new_file;
+ old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
+ new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
+ debug_print("copying: %s -> %s\n", old_file, new_file);
+ if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
+ gint r = copy_file(old_file, new_file, TRUE);
+ if (r < 0)
+ return r;
+ } else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
+ gint r = copy_dir(old_file, new_file);
+ if (r < 0)
+ return r;
+ }
+ }
+ return 0;
+}
+
+/* crude test to see if a file is an email. */
+gboolean file_is_email (const gchar *filename)
+{
+ FILE *fp = NULL;
+ gchar buffer[2048];
+ gint i = 0;
+ gint score = 0;
+ if (filename == NULL)
+ return FALSE;
+ if ((fp = g_fopen(filename, "rb")) == NULL)
+ return FALSE;
+ while (i < 60 && score < 4
+ && fgets(buffer, sizeof (buffer), fp) > 0) {
+ if (!strncmp(buffer, "Return-Path:", strlen("Return-Path:")))
+ score++;
+ if (!strncmp(buffer, "From:", strlen("From:")))
+ score++;
+ if (!strncmp(buffer, "To:", strlen("To:")))
+ score++;
+ if (!strncmp(buffer, "Subject:", strlen("Subject:")))
+ score++;
+ i++;
+ }
+ fclose(fp);
+ return (score >= 4);
+}