Add a fast way to search for Message-ID (at least)
[claws.git] / src / imap.c
index 32ecd3861fef894c022260a3ea82826f425c72f1..76d1dbd8561adc456719610879433e7457bb60e0 100644 (file)
@@ -835,12 +835,12 @@ static int imap_get_capabilities(IMAPSession *session)
 {
        struct mailimap_capability_data *capabilities = NULL;
        clistiter *cur;
-       int result = -1;
+       int result;
 
        if (session->capability != NULL)
                return MAILIMAP_NO_ERROR;
 
-       capabilities = imap_threaded_capability(session->folder, &result);
+       result = imap_threaded_capability(session->folder, &capabilities);
 
        if (result != MAILIMAP_NO_ERROR) {
                return result;
@@ -898,6 +898,9 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
        case IMAP_AUTH_DIGEST_MD5:
                ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
                break;
+       case IMAP_AUTH_SCRAM_SHA1:
+               ok = imap_cmd_login(session, user, pass, "SCRAM-SHA-1");
+               break;
        case IMAP_AUTH_LOGIN:
                ok = imap_cmd_login(session, user, pass, "LOGIN");
                break;
@@ -909,17 +912,21 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                                "\t ANONYMOUS %d\n"
                                "\t CRAM-MD5 %d\n"
                                "\t DIGEST-MD5 %d\n"
+                               "\t SCRAM-SHA-1 %d\n"
                                "\t LOGIN %d\n"
                                "\t GSSAPI %d\n", 
                        imap_has_capability(session, "ANONYMOUS"),
                        imap_has_capability(session, "CRAM-MD5"),
                        imap_has_capability(session, "DIGEST-MD5"),
+                       imap_has_capability(session, "SCRAM-SHA-1"),
                        imap_has_capability(session, "LOGIN"),
                        imap_has_capability(session, "GSSAPI"));
                if (imap_has_capability(session, "CRAM-MD5"))
                        ok = imap_cmd_login(session, user, pass, "CRAM-MD5");
                if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "DIGEST-MD5"))
                        ok = imap_cmd_login(session, user, pass, "DIGEST-MD5");
+               if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "SCRAM-SHA-1"))
+                       ok = imap_cmd_login(session, user, pass, "SCRAM-SHA-1");
                if (ok == MAILIMAP_ERROR_LOGIN && imap_has_capability(session, "GSSAPI"))
                        ok = imap_cmd_login(session, user, pass, "GSSAPI");
                if (ok == MAILIMAP_ERROR_LOGIN) /* we always try LOGIN before giving up */
@@ -941,6 +948,12 @@ static gint imap_auth(IMAPSession *session, const gchar *user, const gchar *pass
                                     "DIGEST-MD5 SASL plugin is installed.");
                } 
 
+               if (type == IMAP_AUTH_SCRAM_SHA1) {
+                       ext_info = _("\n\nSCRAM-SHA-1 logins only work if libetpan has been "
+                                    "compiled with SASL support and the "
+                                    "SCRAM SASL plugin is installed.");
+               }
+
                if (time(NULL) - last_login_err > 10) {
                        if (!prefs_common.no_recv_err_panel) {
                                alertpanel_error_log(_("Connection to %s failed: "
@@ -1564,7 +1577,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        ok = imap_select(session, IMAP_FOLDER(folder), item,
                         NULL, NULL, NULL, NULL, NULL, FALSE);
        if (ok != MAILIMAP_NO_ERROR) {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
                g_free(filename);
                return NULL;
        }
@@ -1575,7 +1588,7 @@ static gchar *imap_fetch_msg_full(Folder *folder, FolderItem *item, gint uid,
        ok = imap_cmd_fetch(session, (guint32)uid, filename, headers, body);
 
        if (ok != MAILIMAP_NO_ERROR) {
-               g_warning("can't fetch message %d\n", uid);
+               g_warning("can't fetch message %d", uid);
                g_free(filename);
                return NULL;
        }
@@ -1739,7 +1752,7 @@ static gint imap_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list,
                                     &new_uid);
 
                if (ok != MAILIMAP_NO_ERROR) {
-                       g_warning("can't append message %s\n", real_file);
+                       g_warning("can't append message %s", real_file);
                        g_free(real_file);
                        g_free(destdir);
                        statusbar_progress_all(0,0,0);
@@ -1859,7 +1872,7 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
        msginfo = (MsgInfo *)msglist->data;
        src = msginfo->folder;
        if (!same_dest_ok && src == dest) {
-               g_warning("the src folder is identical to the dest.\n");
+               g_warning("the src folder is identical to the dest.");
                return -1;
        }
 
@@ -1987,8 +2000,11 @@ static gint imap_do_copy_msgs(Folder *folder, FolderItem *dest,
                                if (!is_dir_exist(cache_path))
                                        make_dir_hier(cache_path);
                                if (is_file_exist(real_file) && is_dir_exist(cache_path)) {
-                                       copy_file(real_file, cache_file, TRUE);
-                                       debug_print("copied to cache: %s\n", cache_file);
+                                       if (copy_file(real_file, cache_file, TRUE) < 0)
+                                               debug_print("couldn't cache to %s: %s\n", cache_file,
+                                                           strerror(errno));
+                                       else
+                                               debug_print("copied to cache: %s\n", cache_file);
                                }
                                g_free(real_file);
                                g_free(cache_file);
@@ -2055,6 +2071,7 @@ static gboolean imap_matcher_type_is_local(gint matchertype)
        case MATCHCRITERIA_TO_OR_CC:
        case MATCHCRITERIA_SUBJECT:
        case MATCHCRITERIA_REFERENCES:
+       case MATCHCRITERIA_MESSAGEID:
        case MATCHCRITERIA_INREPLYTO:
        case MATCHCRITERIA_AGE_GREATER:
        case MATCHCRITERIA_AGE_LOWER:
@@ -2101,6 +2118,7 @@ static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
                case MATCHCRITERIA_NOT_MESSAGE: invert = TRUE; matchertype = MATCHCRITERIA_MESSAGE; break;
                case MATCHCRITERIA_NOT_BODY_PART: invert = TRUE; matchertype = MATCHCRITERIA_BODY_PART; break;
                case MATCHCRITERIA_NOT_TO_AND_NOT_CC: invert = TRUE; matchertype = MATCHCRITERIA_TO_OR_CC; break;
+               case MATCHCRITERIA_NOT_MESSAGEID: invert = TRUE; matchertype = MATCHCRITERIA_MESSAGEID; break;
                case MATCHCRITERIA_NOT_INREPLYTO: invert = TRUE; matchertype = MATCHCRITERIA_INREPLYTO; break;
                }
 
@@ -2127,6 +2145,10 @@ static IMAPSearchKey* search_make_key(MatcherProp* match, gboolean* is_all)
                        result = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
                        break;
 
+               case MATCHCRITERIA_MESSAGEID:
+                       result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "Message-ID", match->expr, 0);
+                       break;
+
                case MATCHCRITERIA_INREPLYTO:
                        result = imap_search_new(IMAP_SEARCH_CRITERIA_HEADER, "In-Reply-To", match->expr, 0);
                        break;
@@ -2974,12 +2996,12 @@ static FolderItem *imap_create_special_folder(Folder *folder,
        new_item = imap_create_folder(folder, item, name);
 
        if (!new_item) {
-               g_warning("Can't create '%s'\n", name);
+               g_warning("Can't create '%s'", name);
                if (!folder->inbox) return NULL;
 
                new_item = imap_create_folder(folder, folder->inbox, name);
                if (!new_item)
-                       g_warning("Can't create '%s' under INBOX\n", name);
+                       g_warning("Can't create '%s' under INBOX", name);
                else
                        new_item->stype = stype;
        } else
@@ -2995,13 +3017,25 @@ static gchar *imap_folder_get_path(Folder *folder)
        g_return_val_if_fail(folder != NULL, NULL);
         g_return_val_if_fail(folder->account != NULL, NULL);
 
+#ifdef G_OS_WIN32
+       gchar *sanitized_dirname = g_strdup(folder->account->recv_server);
+       g_strdelimit(sanitized_dirname, ":", ',');
+#endif
+
         folder_path = g_strconcat(get_imap_cache_dir(),
                                   G_DIR_SEPARATOR_S,
+#ifdef G_OS_WIN32
+                                                                                                                                       sanitized_dirname,
+#else
                                   folder->account->recv_server,
+#endif
                                   G_DIR_SEPARATOR_S,
                                   folder->account->userid,
                                   NULL);
 
+#ifdef G_OS_WIN32
+       g_free(sanitized_dirname);
+#endif
        return folder_path;
 }
 
@@ -3261,8 +3295,8 @@ static gint imap_rename_folder(Folder *folder, FolderItem *item,
 
        if (strchr(name, imap_get_path_separator(session, IMAP_FOLDER(folder), item->path, &ok)) != NULL ||
                is_fatal(ok)) {
-               g_warning(_("New folder name must not contain the namespace "
-                           "path separator"));
+               g_warning("New folder name must not contain the namespace "
+                           "path separator");
                return -1;
        }
 
@@ -3422,7 +3456,7 @@ static gint imap_remove_folder_real(Folder *folder, FolderItem *item)
        g_free(path);
        cache_dir = folder_item_get_path(item);
        if (is_dir_exist(cache_dir) && remove_dir_recursive(cache_dir) < 0)
-               g_warning("can't remove directory '%s'\n", cache_dir);
+               g_warning("can't remove directory '%s'", cache_dir);
        g_free(cache_dir);
        folder_item_remove(item);
        return 0;
@@ -4009,7 +4043,7 @@ static gint imap_status(IMAPSession *session, IMAPFolder *folder,
        mailimap_mailbox_data_status_free(data_status);
        
        if (got_values != mask) {
-               g_warning("status: incomplete values received (%d)\n", got_values);
+               g_warning("status: incomplete values received (%d)", got_values);
        }
        return MAILIMAP_NO_ERROR;
 }
@@ -4050,7 +4084,7 @@ static gint imap_cmd_login(IMAPSession *session,
 #else          
                        log_error(LOG_PROTOCOL, _("Connection to %s failed: "
                                        "server requires TLS, but Claws Mail "
-                                       "has been compiled without OpenSSL "
+                                       "has been compiled without TLS "
                                        "support.\n"),
                                        SESSION(session)->server);
                        return MAILIMAP_ERROR_LOGIN;
@@ -4425,7 +4459,7 @@ static gboolean imap_rename_folder_func(GNode *node, gpointer data)
        gint ok = MAILIMAP_NO_ERROR;
        oldpathlen = strlen(oldpath);
        if (strncmp(oldpath, item->path, oldpathlen) != 0) {
-               g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
+               g_warning("path doesn't match: %s, %s", oldpath, item->path);
                return TRUE;
        }
 
@@ -4814,9 +4848,9 @@ gboolean imap_scan_required(Folder *folder, FolderItem *_item)
                debug_print("exists %d, item->item.total_msgs %d\n"
                            "\tunseen %d, item->item.unread_msgs %d\n"
                            "\tuid_next %d, item->uid_next %d\n"
-                           "\tuid_val %d, item->item.mtime %d\n", 
-                           exists, item->item.total_msgs,unseen, item->item.unread_msgs,
-                           uid_next, item->uid_next,uid_val, item->item.mtime);
+                           "\tuid_val %d, item->item.mtime %ld\n",
+                           exists, item->item.total_msgs, unseen, item->item.unread_msgs,
+                           uid_next, item->uid_next, uid_val, (long)(item->item.mtime));
                if (exists != item->item.total_msgs
                    || unseen != item->item.unread_msgs 
                    || uid_next != item->uid_next
@@ -5412,7 +5446,7 @@ static gboolean process_flags(gpointer key, gpointer value, gpointer user_data)
                ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
                        data->msglist, flags_value, NULL, flags_set);
        } else {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
        }
 
        if (!is_fatal(ok))
@@ -5455,7 +5489,7 @@ static gboolean process_tags(gpointer key, gpointer value, gpointer user_data)
                ok = imap_set_message_flags(session, IMAP_FOLDER_ITEM(item),
                        data->msglist, 0, &list, tags_set);
        } else {
-               g_warning("can't select mailbox %s\n", item->path);
+               g_warning("can't select mailbox %s", item->path);
        }
 
        if (!is_fatal(ok))
@@ -5631,7 +5665,9 @@ static GSList * imap_list_from_lep(IMAPFolder * folder,
                        }
 
                        if (!all && dup_name[strlen(dup_name)-1] == '/') {
-                               dup_name[strlen(dup_name)-1] = '\0';
+                               g_free(base);
+                               free(dup_name);
+                               continue;
                        }
 
                        loc_name = imap_modified_utf7_to_utf8(base, FALSE);
@@ -5902,11 +5938,8 @@ void imap_disconnect_all(gboolean have_connectivity)
        GList *list;
        gboolean short_timeout;
 #ifdef HAVE_NETWORKMANAGER_SUPPORT
-       GError *error;
-#endif
+       GError *error = NULL;
 
-#ifdef HAVE_NETWORKMANAGER_SUPPORT
-       error = NULL;
        short_timeout = !networkmanager_is_online(&error);
        if(error) {
                short_timeout = TRUE;