2006-04-20 [colin] 2.1.1cvs20
[claws.git] / src / imap.c
index c428a0b9d3ca8e755fe3d28251e74320fc35bd20..3dc6b35cca08480fd0aff224160213b32d5859d5 100644 (file)
@@ -30,6 +30,7 @@
 #include "imap.h"
 #include "imap_gtk.h"
 #include "inc.h"
+#include "xml.h"
 
 #ifdef HAVE_LIBETPAN
 
@@ -168,6 +169,9 @@ struct _IMAPFolderItem
        GHashTable *flags_unset_table;
 };
 
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
+
 static void imap_folder_init           (Folder         *folder,
                                         const gchar    *name,
                                         const gchar    *path);
@@ -436,6 +440,10 @@ FolderClass *imap_get_class(void)
                imap_class.close = imap_close;
                imap_class.get_num_list = imap_get_num_list;
                imap_class.scan_required = imap_scan_required;
+               imap_class.set_xml = folder_set_xml;
+               imap_class.get_xml = folder_get_xml;
+               imap_class.item_set_xml = imap_item_set_xml;
+               imap_class.item_get_xml = imap_item_get_xml;
 
                /* Message functions */
                imap_class.get_msginfo = imap_get_msginfo;
@@ -522,7 +530,6 @@ static gboolean imap_reset_uid_lists_func(GNode *node, gpointer data)
        IMAPFolderItem *item = (IMAPFolderItem *)node->data;
        
        item->lastuid = 0;
-       item->uid_next = 0;
        g_slist_free(item->uid_list);
        item->uid_list = NULL;
        
@@ -584,6 +591,9 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
        imap_get_capabilities(session);
 
        switch(type) {
+       case IMAP_AUTH_ANON:
+               ok = imap_cmd_login(session, user, pass, "ANONYMOUS");
+               break;
        case IMAP_AUTH_CRAM_MD5:
                ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
                break;
@@ -592,8 +602,10 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                break;
        default:
                debug_print("capabilities:\n"
+                               "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
                                "\t LOGIN %d\n", 
+                       imap_has_capability(session, "ANONYMOUS"),
                        imap_has_capability(session, "CRAM-MD5"),
                        imap_has_capability(session, "LOGIN"));
                if (imap_has_capability(session, "CRAM-MD5"))
@@ -676,12 +688,16 @@ static IMAPSession *imap_session_get(Folder *folder)
 {
        RemoteFolder *rfolder = REMOTE_FOLDER(folder);
        IMAPSession *session = NULL;
+       static time_t last_failure = 0;
 
        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 && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return NULL;
        }
 
@@ -694,10 +710,14 @@ static IMAPSession *imap_session_get(Folder *folder)
                } */
        } else {
                imap_reset_uid_lists(folder);
+               if (time(NULL) - last_failure <= 2)
+                       return NULL;
                session = imap_session_new(folder, folder->account);
        }
-       if(session == NULL)
+       if(session == NULL) {
+               last_failure = time(NULL);
                return NULL;
+       }
 
        /* Make sure session is authenticated */
        if (!IMAP_SESSION(session)->authenticated)
@@ -706,6 +726,7 @@ static IMAPSession *imap_session_get(Folder *folder)
        if (!IMAP_SESSION(session)->authenticated) {
                session_destroy(SESSION(session));
                rfolder->session = NULL;
+               last_failure = time(NULL);
                return NULL;
        }
 
@@ -854,13 +875,15 @@ static void imap_session_authenticate(IMAPSession *session,
        g_return_if_fail(account->userid != NULL);
 
        pass = account->passwd;
-       if (!pass) {
+       if (!pass && account->imap_auth_type != IMAP_AUTH_ANON) {
                gchar *tmp_pass;
                tmp_pass = input_dialog_query_password(account->recv_server, account->userid);
                if (!tmp_pass)
-                       tmp_pass = g_strdup(""); /* allow empty password */
+                       return;
                Xstrdup_a(pass, tmp_pass, {g_free(tmp_pass); return;});
                g_free(tmp_pass);
+       } else if (account->imap_auth_type == IMAP_AUTH_ANON) {
+               pass = "";
        }
        statusbar_print_all(_("Connecting to IMAP4 server %s...\n"),
                                account->recv_server);
@@ -932,7 +955,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                make_dir_hier(path);
        filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(uid), NULL);
        g_free(path);
-
+       debug_print("trying to fetch cached %s\n", filename);
        if (is_file_exist(filename)) {
                /* see whether the local file represents the whole message
                 * or not. As the IMAP server reports size with \r chars,
@@ -951,6 +974,11 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
                        procmsg_msginfo_free(msginfo);
                        file_strip_crs(filename);
                        return filename;
+               } else if (!cached) {
+                       debug_print("message not cached, considering file complete\n");
+                       procmsg_msginfo_free(msginfo);
+                       file_strip_crs(filename);
+                       return filename;
                } else {
                        procmsg_msginfo_free(cached);
                        procmsg_msginfo_free(msginfo);
@@ -1098,6 +1126,24 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                        statusbar_progress_all(0,0,0);
                        statusbar_pop_all();
                        return -1;
+               } else {
+                       debug_print("appended new message as %d\n", new_uid);
+                       /* put the local file in the imapcache, so that we don't
+                        * have to fetch it back later. */
+                       if (new_uid > 0) {
+                               gchar *cache_path = folder_item_get_path(dest);
+                               if (!is_dir_exist(cache_path))
+                                       make_dir_hier(cache_path);
+                               if (is_dir_exist(cache_path)) {
+                                       gchar *cache_file = g_strconcat(
+                                               cache_path, G_DIR_SEPARATOR_S, 
+                                               itos(new_uid), NULL);
+                                       copy_file(real_file, cache_file, TRUE);
+                                       debug_print("copied to cache: %s\n", cache_file);
+                                       g_free(cache_file);
+                               }
+                               g_free(cache_path);
+                       }
                }
 
                if (relation != NULL)
@@ -2033,6 +2079,8 @@ static void *imap_get_uncached_messages_thread(void *data)
                if (r != MAILIMAP_NO_ERROR)
                        continue;
                
+               session_set_access_time(SESSION(session));
+
                count = 0;
                for(i = 0 ; i < carray_count(env_list) ; i ++) {
                        struct imap_fetch_env_info * info;
@@ -2116,7 +2164,10 @@ static GSList *imap_get_uncached_messages(IMAPSession *session,
                data->numlist = newlist;
                data->cur += count;
                
-               if (prefs_common.work_offline && !inc_offline_should_override()) {
+               if (prefs_common.work_offline && 
+                   !inc_offline_should_override(
+                       _("Sylpheed-Claws needs network access in order "
+                         "to access the IMAP server."))) {
                        g_free(data);
                        return NULL;
                }
@@ -2413,50 +2464,6 @@ static void imap_free_capabilities(IMAPSession *session)
 
 /* low-level IMAP4rev1 commands */
 
-#if 0
-static gint imap_cmd_authenticate(IMAPSession *session, const gchar *user,
-                                 const gchar *pass, IMAPAuthType type)
-{
-       gchar *auth_type;
-       gint ok;
-       gchar *buf = NULL;
-       gchar *challenge;
-       gint challenge_len;
-       gchar hexdigest[33];
-       gchar *response;
-       gchar *response64;
-
-       auth_type = "CRAM-MD5";
-
-       imap_gen_send(session, "AUTHENTICATE %s", auth_type);
-       ok = imap_gen_recv(session, &buf);
-       if (ok != IMAP_SUCCESS || buf[0] != '+' || buf[1] != ' ') {
-               g_free(buf);
-               return IMAP_ERROR;
-       }
-
-       challenge = g_malloc(strlen(buf + 2) + 1);
-       challenge_len = base64_decode(challenge, buf + 2, -1);
-       challenge[challenge_len] = '\0';
-       g_free(buf);
-
-       md5_hex_hmac(hexdigest, challenge, challenge_len, pass, strlen(pass));
-       g_free(challenge);
-
-       response = g_strdup_printf("%s %s", user, hexdigest);
-       response64 = g_malloc((strlen(response) + 3) * 2 + 1);
-       base64_encode(response64, response, strlen(response));
-       g_free(response);
-
-       sock_puts(SESSION(session)->sock, response64);
-       ok = imap_cmd_ok(session, NULL);
-       if (ok != IMAP_SUCCESS)
-               log_warning(_("IMAP4 authentication failed.\n"));
-
-       return ok;
-}
-#endif
-
 static gint imap_cmd_login(IMAPSession *session,
                           const gchar *user, const gchar *pass,
                           const gchar *type)
@@ -2507,7 +2514,8 @@ static gint imap_cmd_starttls(IMAPSession *session)
 {
        int r;
        
-       r = imap_threaded_starttls(session->folder);
+       r = imap_threaded_starttls(session->folder, 
+               SESSION(session)->server, SESSION(session)->port);
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("starttls err %d\n", r);
                return IMAP_ERROR;
@@ -2634,7 +2642,10 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        data->headers = headers;
        data->body = body;
 
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -2657,10 +2668,8 @@ static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
 
        flag_list = imap_flag_to_lep(flags);
        r = imap_threaded_append(session->folder, destfolder,
-                        file, flag_list);
+                        file, flag_list, new_uid);
        mailimap_flag_list_free(flag_list);
-       if (new_uid != NULL)
-               *new_uid = 0;
 
        if (r != MAILIMAP_NO_ERROR) {
                debug_print("append err %d\n", r);
@@ -2717,7 +2726,10 @@ static gint imap_cmd_expunge(IMAPSession *session)
 {
        int r;
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                return -1;
        }
 
@@ -3052,7 +3064,10 @@ static gint get_list_of_uids(IMAPSession *session, Folder *folder, IMAPFolderIte
        data->item = item;
        data->msgnum_list = msgnum_list;
        data->session = session;
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
@@ -3083,8 +3098,15 @@ gint imap_get_num_list(Folder *folder, FolderItem *_item, GSList **msgnum_list,
        session = imap_session_get(folder);
        g_return_val_if_fail(session != NULL, -1);
        lock_session();
-       statusbar_print_all("Scanning %s...\n", FOLDER_ITEM(item)->path 
-                               ? FOLDER_ITEM(item)->path:"");
+
+       if (FOLDER_ITEM(item)->path) 
+               statusbar_print_all(_("Scanning folder %s%c%s ..."),
+                                     FOLDER_ITEM(item)->folder->name, 
+                                     G_DIR_SEPARATOR,
+                                     FOLDER_ITEM(item)->path);
+       else
+               statusbar_print_all(_("Scanning folder %s ..."),
+                                     FOLDER_ITEM(item)->folder->name);
 
        selected_folder = (session->mbox != NULL) &&
                          (!strcmp(session->mbox, item->item.path));
@@ -3290,6 +3312,7 @@ GSList *imap_get_msginfos(Folder *folder, FolderItem *item,
                                                }
                                                g_free(file);
                                        }
+                                       session_set_access_time(SESSION(session));
                                }
 
                                if (elem == NULL)
@@ -3376,7 +3399,8 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                item->item.last_num = uid_next - 1;
                debug_print("uidnext %d, item->uid_next %d, exists %d, item->item.total_msgs %d\n", 
                        uid_next, item->uid_next, exists, item->item.total_msgs);
-               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)) {
+               if ((uid_next != item->uid_next) || (exists != item->item.total_msgs)
+                   || unseen != item->item.unread_msgs || uid_val != item->item.mtime) {
                        unlock_session();
                        return TRUE;
                }
@@ -3670,37 +3694,39 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                        unseen = g_slist_concat(unseen, uidlist);
                }
                
-               r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
-                                        imapset, &lep_uidlist);
-               if (r == MAILIMAP_NO_ERROR) {
-                       GSList * uidlist;
-                       
-                       uidlist = imap_uid_list_from_lep(lep_uidlist);
-                       mailimap_search_result_free(lep_uidlist);
-                       
-                       answered = g_slist_concat(answered, uidlist);
-               }
-
                r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_FLAGGED,
                                         imapset, &lep_uidlist);
                if (r == MAILIMAP_NO_ERROR) {
                        GSList * uidlist;
-                       
+
                        uidlist = imap_uid_list_from_lep(lep_uidlist);
                        mailimap_search_result_free(lep_uidlist);
-                       
+
                        flagged = g_slist_concat(flagged, uidlist);
                }
-               
-               r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
-                                        imapset, &lep_uidlist);
-               if (r == MAILIMAP_NO_ERROR) {
-                       GSList * uidlist;
-                       
-                       uidlist = imap_uid_list_from_lep(lep_uidlist);
-                       mailimap_search_result_free(lep_uidlist);
-                       
-                       deleted = g_slist_concat(deleted, uidlist);
+
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_ANSWERED,
+                                                imapset, &lep_uidlist);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList * uidlist;
+
+                               uidlist = imap_uid_list_from_lep(lep_uidlist);
+                               mailimap_search_result_free(lep_uidlist);
+
+                               answered = g_slist_concat(answered, uidlist);
+                       }
+
+                       r = imap_threaded_search(folder, IMAP_SEARCH_TYPE_DELETED,
+                                                imapset, &lep_uidlist);
+                       if (r == MAILIMAP_NO_ERROR) {
+                               GSList * uidlist;
+
+                               uidlist = imap_uid_list_from_lep(lep_uidlist);
+                               mailimap_search_result_free(lep_uidlist);
+
+                               deleted = g_slist_concat(deleted, uidlist);
+                       }
                }
        }
 
@@ -3717,7 +3743,11 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                msginfo = (MsgInfo *) elem->data;
                flags = msginfo->flags.perm_flags;
                wasnew = (flags & MSG_NEW);
-               flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW) | MSG_REPLIED | MSG_MARKED);
+               } else {
+                       flags &= ~((reverse_seen ? 0 : MSG_UNREAD | MSG_NEW | MSG_MARKED));
+               }
                if (reverse_seen)
                        flags |= MSG_UNREAD | (wasnew ? MSG_NEW : 0);
                if (gslist_find_next_num(&p_unseen, msginfo->msgnum) == msginfo->msgnum) {
@@ -3727,18 +3757,22 @@ static /*gint*/ void *imap_get_flags_thread(void *data)
                                flags &= ~(MSG_UNREAD | MSG_NEW);
                        }
                }
-               if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
-                       flags |= MSG_REPLIED;
-               else
-                       flags &= ~MSG_REPLIED;
+               
                if (gslist_find_next_num(&p_flagged, msginfo->msgnum) == msginfo->msgnum)
                        flags |= MSG_MARKED;
                else
                        flags &= ~MSG_MARKED;
-               if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
-                       flags |= MSG_DELETED;
-               else
-                       flags &= ~MSG_DELETED;
+
+               if (item->opened || item->processing_pending || item == folder->inbox) {
+                       if (gslist_find_next_num(&p_answered, msginfo->msgnum) == msginfo->msgnum)
+                               flags |= MSG_REPLIED;
+                       else
+                               flags &= ~MSG_REPLIED;
+                       if (gslist_find_next_num(&p_deleted, msginfo->msgnum) == msginfo->msgnum)
+                               flags |= MSG_DELETED;
+                       else
+                               flags &= ~MSG_DELETED;
+               }
                g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
        }
 
@@ -3769,14 +3803,17 @@ static gint imap_get_flags(Folder *folder, FolderItem *item,
 
        GSList *tmp = NULL, *cur;
        
-       if (prefs_common.work_offline && !inc_offline_should_override()) {
+       if (prefs_common.work_offline && 
+           !inc_offline_should_override(
+               _("Sylpheed-Claws needs network access in order "
+                 "to access the IMAP server."))) {
                g_free(data);
                return -1;
        }
 
        tmp = folder_item_get_msg_list(item);
 
-       if (g_slist_length(tmp) == g_slist_length(msginfo_list))
+       if (g_slist_length(tmp) <= g_slist_length(msginfo_list))
                data->full_search = TRUE;
        
        for (cur = tmp; cur; cur = cur->next)
@@ -4193,6 +4230,9 @@ void imap_folder_unref(Folder *folder)
 
 static FolderClass imap_class;
 
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item);
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag);
+
 static Folder  *imap_folder_new        (const gchar    *name,
                                         const gchar    *path)
 {
@@ -4226,6 +4266,11 @@ FolderClass *imap_get_class(void)
                imap_class.create_tree = imap_create_tree;
                imap_class.create_folder = imap_create_folder;
                imap_class.rename_folder = imap_rename_folder;
+
+               imap_class.set_xml = folder_set_xml;
+               imap_class.get_xml = folder_get_xml;
+               imap_class.item_set_xml = imap_item_set_xml;
+               imap_class.item_get_xml = imap_item_get_xml;
                /* nothing implemented */
        }
 
@@ -4237,3 +4282,30 @@ void imap_synchronise(FolderItem *item)
 {
        imap_gtk_synchronise(item);
 }
+
+static void imap_item_set_xml(Folder *folder, FolderItem *item, XMLTag *tag)
+{
+       GList *cur;
+
+       folder_item_set_xml(folder, item, tag);
+       
+       for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
+               XMLAttr *attr = (XMLAttr *) cur->data;
+
+               if (!attr || !attr->name || !attr->value) continue;
+               if (!strcmp(attr->name, "uidnext"))
+                       IMAP_FOLDER_ITEM(item)->uid_next = atoi(attr->value);
+       }
+}
+
+static XMLTag *imap_item_get_xml(Folder *folder, FolderItem *item)
+{
+       XMLTag *tag;
+
+       tag = folder_item_get_xml(folder, item);
+
+       xml_tag_add_attr(tag, xml_attr_new_int("uidnext", 
+                       IMAP_FOLDER_ITEM(item)->uid_next));
+
+       return tag;
+}