0.9.10claws5
[claws.git] / src / imap.c
index b12f4b4100c36884a2c5a6de99c2ebb0da883583..def44b3c364c376ff93f8ed21b0e8de33ad1acea 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2004 Hiroyuki Yamamoto
  *
  * 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
 #include "md5.h"
 #include "base64.h"
 #include "utils.h"
+#include "prefs_common.h"
 #include "inputdialog.h"
 #include "log.h"
 #include "remotefolder.h"
+#include "alertpanel.h"
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -433,55 +435,56 @@ static gboolean imap_scan_required                (Folder         *folder,
 static void imap_change_flags                  (Folder         *folder,
                                                 FolderItem     *item,
                                                 MsgInfo        *msginfo,
-                                                MsgPermFlags   newflags);
+                                                MsgPermFlags    newflags);
+static gint imap_get_flags                     (Folder         *folder,
+                                                FolderItem     *item,
+                                                MsgInfoList    *msglist,
+                                                GRelation      *msgflags);
 static gchar *imap_folder_get_path             (Folder         *folder);
 static gchar *imap_item_get_path               (Folder         *folder,
                                                 FolderItem     *item);
 
-static FolderClass imap_class =
-{
-       F_IMAP,
-       "imap",
-       "IMAP4",
-
-       /* Folder functions */
-       imap_folder_new,
-       imap_folder_destroy,
-       imap_scan_tree,
-       imap_create_tree,
-
-       /* FolderItem functions */
-       imap_folder_item_new,
-       imap_folder_item_destroy,
-       NULL,
-       NULL,
-       imap_item_get_path,
-       imap_create_folder,
-       imap_rename_folder,
-       imap_remove_folder,
-       imap_close,
-       imap_get_num_list,
-       NULL,
-       NULL,
-       NULL,
-       imap_scan_required,
-
-       /* Message functions */
-       imap_get_msginfo,
-       imap_get_msginfos,
-       imap_fetch_msg,
-       imap_add_msg,
-       imap_add_msgs,
-       imap_copy_msg,
-       imap_copy_msgs,
-       imap_remove_msg,
-       imap_remove_all_msg,
-       imap_is_msg_changed,
-       imap_change_flags,
-};
+static FolderClass imap_class;
 
 FolderClass *imap_get_class(void)
 {
+       if (imap_class.idstr == NULL) {
+               imap_class.type = F_IMAP;
+               imap_class.idstr = "imap";
+               imap_class.uistr = "IMAP4";
+
+               /* Folder functions */
+               imap_class.new_folder = imap_folder_new;
+               imap_class.destroy_folder = imap_folder_destroy;
+               imap_class.scan_tree = imap_scan_tree;
+               imap_class.create_tree = imap_create_tree;
+
+               /* FolderItem functions */
+               imap_class.item_new = imap_folder_item_new;
+               imap_class.item_destroy = imap_folder_item_destroy;
+               imap_class.item_get_path = imap_item_get_path;
+               imap_class.create_folder = imap_create_folder;
+               imap_class.rename_folder = imap_rename_folder;
+               imap_class.remove_folder = imap_remove_folder;
+               imap_class.close = imap_close;
+               imap_class.get_num_list = imap_get_num_list;
+               imap_class.scan_required = imap_scan_required;
+
+               /* Message functions */
+               imap_class.get_msginfo = imap_get_msginfo;
+               imap_class.get_msginfos = imap_get_msginfos;
+               imap_class.fetch_msg = imap_fetch_msg;
+               imap_class.add_msg = imap_add_msg;
+               imap_class.add_msgs = imap_add_msgs;
+               imap_class.copy_msg = imap_copy_msg;
+               imap_class.copy_msgs = imap_copy_msgs;
+               imap_class.remove_msg = imap_remove_msg;
+               imap_class.remove_all_msg = imap_remove_all_msg;
+               imap_class.is_msg_changed = imap_is_msg_changed;
+               imap_class.change_flags = imap_change_flags;
+               imap_class.get_flags = imap_get_flags;
+       }
+       
        return &imap_class;
 }
 
@@ -614,6 +617,9 @@ static IMAPSession *imap_session_get(Folder *folder)
        g_return_val_if_fail(folder != NULL, NULL);
        g_return_val_if_fail(FOLDER_CLASS(folder) == &imap_class, NULL);
        g_return_val_if_fail(folder->account != NULL, NULL);
+       
+       if (prefs_common.work_offline)
+               return NULL;
 
        /* Make sure we have a session */
        if (rfolder->session != NULL) {
@@ -1014,6 +1020,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                g_tuples_destroy(tuples);
        }
 
+       g_relation_destroy(uid_mapping);
        imap_seq_set_free(seq_list);
 
        g_free(destdir);
@@ -1234,6 +1241,12 @@ static gint imap_scan_tree(Folder *folder)
                        log_warning(_("root folder %s does not exist\n"), real_path);
                        g_ptr_array_free(argbuf, TRUE);
                        g_free(real_path);
+
+                       if (!folder->node) {
+                               item = folder_item_new(folder, folder->name, NULL);
+                               item->folder = folder;
+                               folder->node = item->node = g_node_new(item);
+                       }
                        return -1;
                }
                g_ptr_array_free(argbuf, TRUE);
@@ -1355,7 +1368,7 @@ static gint imap_scan_tree_recursive(IMAPSession *session, FolderItem *item)
                if (!strcmp(new_item->path, "INBOX")) {
                        new_item->stype = F_INBOX;
                        folder->inbox = new_item;
-               } else if (!item->parent || item->stype == F_INBOX) {
+               } else if (!folder_item_parent(item) || item->stype == F_INBOX) {
                        gchar *base;
 
                        base = g_basename(new_item->path);
@@ -1606,7 +1619,7 @@ static FolderItem *imap_create_folder(Folder *folder, FolderItem *parent,
        session = imap_session_get(folder);
        if (!session) return NULL;
 
-       if (!parent->parent && strcmp(name, "INBOX") == 0)
+       if (!folder_item_parent(parent) && strcmp(name, "INBOX") == 0)
                dirpath = g_strdup(name);
        else if (parent->path)
                dirpath = g_strconcat(parent->path, "/", name, NULL);
@@ -1704,6 +1717,12 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
        g_return_val_if_fail(item->path != NULL, -1);
        g_return_val_if_fail(name != NULL, -1);
 
+       if (strchr(name, imap_get_path_separator(folder, item->path)) != NULL) {
+               g_warning(_("New folder name must not contain the namespace "
+                           "path seperator"));
+               return -1;
+       }
+
        session = imap_session_get(folder);
        if (!session) return -1;
 
@@ -1967,7 +1986,7 @@ static SockInfo *imap_init_sock(SockInfo *sock)
 
 static GList *imap_parse_namespace_str(gchar *str)
 {
-       gchar *p = str;
+       guchar *p = str;
        gchar *name;
        gchar *separator;
        IMAPNameSpace *namespace;
@@ -2159,7 +2178,7 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
        g_return_val_if_fail(str != NULL, cur_pos);
 
        /* read the next line if the current response buffer is empty */
-       while (isspace(*cur_pos)) cur_pos++;
+       while (isspace(*(guchar *)cur_pos)) cur_pos++;
        while (*cur_pos == '\0') {
                if ((nextline = sock_getline(sock)) == NULL)
                        return cur_pos;
@@ -2170,7 +2189,7 @@ static gchar *imap_parse_atom(SockInfo *sock, gchar *src,
                debug_print("IMAP4< %s\n", nextline);
                g_free(nextline);
 
-               while (isspace(*cur_pos)) cur_pos++;
+               while (isspace(*(guchar *)cur_pos)) cur_pos++;
        }
 
        if (!strncmp(cur_pos, "NIL", 3)) {
@@ -2225,7 +2244,7 @@ static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
 
        g_return_val_if_fail(str != NULL, cur_pos);
 
-       while (isspace(*cur_pos)) cur_pos++;
+       while (isspace(*(guchar *)cur_pos)) cur_pos++;
 
        g_return_val_if_fail(*cur_pos == '{', cur_pos);
 
@@ -2252,7 +2271,7 @@ static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
        *headers = g_strndup(cur_pos, len);
        cur_pos += len;
 
-       while (isspace(*cur_pos)) cur_pos++;
+       while (isspace(*(guchar *)cur_pos)) cur_pos++;
        while (*cur_pos == '\0') {
                if ((nextline = sock_getline(sock)) == NULL)
                        return cur_pos;
@@ -2262,7 +2281,7 @@ static gchar *imap_get_header(SockInfo *sock, gchar *cur_pos, gchar **headers,
                debug_print("IMAP4< %s\n", nextline);
                g_free(nextline);
 
-               while (isspace(*cur_pos)) cur_pos++;
+               while (isspace(*(guchar *)cur_pos)) cur_pos++;
        }
 
        return cur_pos;
@@ -3479,7 +3498,7 @@ static gchar *imap_locale_to_modified_utf7(const gchar *from)
                        norm_utf7_len -= 2;
                        from_tmp++;
                        from_len--;
-               } else if (IS_PRINT(*from_tmp)) {
+               } else if (IS_PRINT(*(guchar *)from_tmp)) {
                        /* printable ascii char */
                        *norm_utf7_p = *from_tmp;
                        norm_utf7_p++;
@@ -3491,7 +3510,7 @@ static gchar *imap_locale_to_modified_utf7(const gchar *from)
 
                        /* unprintable char: convert to UTF-7 */
                        p = from_tmp;
-                       while (!IS_PRINT(*p) && conv_len < from_len) {
+                       while (!IS_PRINT(*(guchar *)p) && conv_len < from_len) {
                                mb_len = mblen(p, MB_LEN_MAX);
                                if (mb_len <= 0) {
                                        g_warning("wrong multibyte sequence\n");
@@ -4047,3 +4066,147 @@ void imap_change_flags(Folder *folder, FolderItem *item, MsgInfo *msginfo, MsgPe
 
        return;
 }
+
+static gint compare_msginfo(gconstpointer a, gconstpointer b)
+{
+       return ((MsgInfo *)a)->msgnum - ((MsgInfo *)b)->msgnum;
+}
+
+static guint gslist_find_next_num(MsgNumberList **list, guint num)
+{
+       GSList *elem;
+
+       g_return_val_if_fail(list != NULL, -1);
+
+       for (elem = *list; elem != NULL; elem = g_slist_next(elem))
+               if (GPOINTER_TO_INT(elem->data) >= num)
+                       break;
+       *list = elem;
+       return elem != NULL ? GPOINTER_TO_INT(elem->data) : (gint)-1;
+}
+
+/*
+ * NEW and DELETED flags are not syncronized
+ * - The NEW/RECENT flags in IMAP folders can not really be directly
+ *   modified by Sylpheed
+ * - The DELETE/DELETED flag in IMAP and Sylpheed don't have the same
+ *   meaning, in IMAP it always removes the messages from the FolderItem
+ *   in Sylpheed it can mean to move the message to trash
+ */
+static gint imap_get_flags(Folder *folder, FolderItem *item,
+                           MsgInfoList *msginfo_list, GRelation *msgflags)
+{
+       IMAPSession *session;
+       GSList *sorted_list;
+       /*
+       GSList *new = NULL, *p_new;
+       */
+       GSList *unseen = NULL, *answered = NULL, *flagged = NULL, *deleted = NULL;
+       GSList *p_unseen, *p_answered, *p_flagged, *p_deleted;
+       GSList *elem;
+       GSList *seq_list, *cur;
+       gboolean reverse_seen = FALSE;
+       GString *cmd_buf;
+       gint ok;
+       gint exists_cnt, recent_cnt, unseen_cnt, uid_next;
+       guint32 uidvalidity;
+
+       g_return_val_if_fail(folder != NULL, -1);
+       g_return_val_if_fail(item != NULL, -1);
+       g_return_val_if_fail(msginfo_list != NULL, -1);
+
+       session = imap_session_get(folder);
+       g_return_val_if_fail(session != NULL, -1);
+
+       ok = imap_select(session, IMAP_FOLDER(folder), item->path,
+                       NULL, NULL, NULL, NULL);
+       if (ok != IMAP_SUCCESS)
+               return -1;
+
+       ok = imap_status(session, IMAP_FOLDER(folder), item->path,
+                        &exists_cnt, &recent_cnt, &uid_next, &uidvalidity, &unseen_cnt);
+
+       if (unseen_cnt > exists_cnt / 2)
+               reverse_seen = TRUE;
+
+       cmd_buf = g_string_new(NULL);
+
+       sorted_list = g_slist_sort(g_slist_copy(msginfo_list), compare_msginfo);
+
+       seq_list = imap_get_seq_set_from_msglist(msginfo_list);
+
+       for (cur = seq_list; cur != NULL; cur = g_slist_next(cur)) {
+               IMAPSet imapset = cur->data;
+/*
+               g_string_sprintf(cmd_buf, "RECENT UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_new);
+               new = g_slist_concat(new, p_new);
+*/
+               g_string_sprintf(cmd_buf, "%sSEEN UID %s", reverse_seen ? "" : "UN", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_unseen);
+               unseen = g_slist_concat(unseen, p_unseen);
+
+               g_string_sprintf(cmd_buf, "ANSWERED UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_answered);
+               answered = g_slist_concat(answered, p_answered);
+
+               g_string_sprintf(cmd_buf, "FLAGGED UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_flagged);
+               flagged = g_slist_concat(flagged, p_flagged);
+/*
+               g_string_sprintf(cmd_buf, "DELETED UID %s", imapset);
+               imap_cmd_search(session, cmd_buf->str, &p_deleted);
+               deleted = g_slist_concat(deleted, p_deleted);
+*/
+       }
+
+/*
+       p_new = new;
+*/
+       p_unseen = unseen;
+       p_answered = answered;
+       p_flagged = flagged;
+       p_deleted = deleted;
+       
+       for (elem = sorted_list; elem != NULL; elem = g_slist_next(elem)) {
+               MsgInfo *msginfo;
+               MsgPermFlags flags;
+               
+               msginfo = (MsgInfo *) elem->data;
+               flags = msginfo->flags.perm_flags;
+               flags &= ~((reverse_seen ? 0 : MSG_UNREAD) | MSG_REPLIED | MSG_MARKED);
+               if (reverse_seen)
+                       flags |= MSG_UNREAD;
+               /*
+               if (gslist_find_next_num(&p_new, msginfo->msgnum) == msginfo->msgnum)
+                       flags |= MSG_NEW;
+               */
+               if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
+                       if (!reverse_seen) {
+                               flags |= MSG_UNREAD;
+                       } else {
+                               flags &= ~(MSG_UNREAD | MSG_NEW);
+                       }
+               }
+               if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
+                       flags |= MSG_REPLIED;
+               if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
+                       flags |= MSG_MARKED;
+               /*
+               if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
+                       MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
+                */
+               g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
+       }
+
+       imap_seq_set_free(seq_list);
+       g_slist_free(deleted);
+       g_slist_free(flagged);
+       g_slist_free(answered);
+       g_slist_free(unseen);
+       /* new not freed in original patch ??? */
+       g_slist_free(sorted_list);
+       g_string_free(cmd_buf, TRUE);
+
+       return 0;
+}