in the UI replace 'POP3' with 'POP' and 'IMAP4' with 'IMAP'
[claws.git] / src / etpan / imap-thread.c
index be7dca5f8949217e0c70860980747b995b51ab2e..f12f5de2aea8791050ebb1b08bbd125d7c038531 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2012 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2016 DINH Viet Hoa and the Claws Mail 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
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -59,26 +58,6 @@ static chash * session_hash = NULL;
 static guint thread_manager_signal = 0;
 static GIOChannel * io_channel = NULL;
 
-static void delete_imap(Folder *folder, mailimap *imap)
-{
-       chashdatum key;
-
-       key.data = &folder;
-       key.len = sizeof(folder);
-       chash_delete(session_hash, &key, NULL);
-       
-       key.data = &imap;
-       key.len = sizeof(imap);
-       chash_delete(courier_workaround_hash, &key, NULL);
-       if (imap && imap->imap_stream) {
-               /* we don't want libetpan to logout */
-               mailstream_close(imap->imap_stream);
-               imap->imap_stream = NULL;
-       }
-       debug_print("removing mailimap %p\n", imap);
-       mailimap_free(imap);    
-}
-
 static gboolean thread_manager_event(GIOChannel * source,
     GIOCondition condition,
     gpointer data)
@@ -107,7 +86,7 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
                return;
        }
        buf = malloc(size+1);
@@ -128,7 +107,7 @@ static void imap_logger_cmd(int direction, const char * str, size_t size)
        lines = g_strsplit(buf, "\n", -1);
 
        while (lines[i] && *lines[i]) {
-               log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+               log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                i++;
        }
        g_strfreev(lines);
@@ -142,7 +121,7 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 128 && !direction) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
                return;
        }
        
@@ -164,11 +143,11 @@ static void imap_logger_fetch(int direction, const char * str, size_t size)
 
        if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
                while (lines[i] && *lines[i]) {
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [data - %zd bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -181,7 +160,7 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [UID data - %zd bytes]\n", direction?'>':'<', size);
                return;
        }
        buf = malloc(size+1);
@@ -203,11 +182,11 @@ static void imap_logger_uid(int direction, const char * str, size_t size)
        while (lines[i] && *lines[i]) {
                int llen = strlen(lines[i]);
                if (llen < 64)
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                else {
                        gchar tmp[64];
                        strncpy2(tmp, lines[i], 63);
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
+                       log_print(LOG_PROTOCOL, "IMAP%c %s[... - %d bytes more]\n", direction?'>':'<', tmp,
                                  llen-64);
                }
                i++;
@@ -223,10 +202,10 @@ static void imap_logger_append(int direction, const char * str, size_t size)
        int i = 0;
 
        if (size > 8192) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
                return;
        } else if (direction == 0 && size > 64) {
-               log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
                return;
        } 
        buf = malloc(size+1);
@@ -247,11 +226,11 @@ static void imap_logger_append(int direction, const char * str, size_t size)
 
        if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
                while (lines[i] && *lines[i]) {
-                       log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
+                       log_print(LOG_PROTOCOL, "IMAP%c %s\n", direction?'>':'<', lines[i]);
                        i++;
                }
        } else {
-               log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+               log_print(LOG_PROTOCOL, "IMAP%c [data - %zd bytes]\n", direction?'>':'<', size);
        }
        g_strfreev(lines);
        free(buf);
@@ -353,7 +332,7 @@ void imap_done(Folder * folder)
        
        chash_delete(imap_hash, &key, NULL);
        
-       debug_print("remove thread");
+       debug_print("remove thread\n");
 }
 
 static struct etpan_thread * get_thread(Folder * folder)
@@ -361,13 +340,17 @@ static struct etpan_thread * get_thread(Folder * folder)
        struct etpan_thread * thread;
        chashdatum key;
        chashdatum value;
-       
+       int r;
+
        key.data = &folder;
        key.len = sizeof(folder);
-       
-       chash_get(imap_hash, &key, &value);
+
+       r = chash_get(imap_hash, &key, &value);
+       if (r < 0)
+               return NULL;
+
        thread = value.data;
-       
+
        return thread;
 }
 
@@ -405,45 +388,54 @@ static void generic_cb(int cancelled, void * result, void * callback_data)
        debug_print("generic_cb\n");
        if (op->imap && op->imap->imap_response_info &&
            op->imap->imap_response_info->rsp_alert) {
-               log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n", 
+               log_error(LOG_PROTOCOL, "IMAP< Alert: %s\n",
                        op->imap->imap_response_info->rsp_alert);
                g_timeout_add(10, cb_show_error, NULL);
        } 
        op->finished = 1;
 }
 
-static void threaded_run(Folder * folder, void * param, void * result,
-                        void (* func)(struct etpan_thread_op * ))
+/* Please do *not* blindly use imap pointers after this function returns,
+ * someone may have deleted it while this function was waiting for completion.
+ * Check return value to see if imap is still valid.
+ * Run get_imap(folder) again to get a fresh and valid pointer.
+ */
+static int threaded_run(Folder * folder, void * param, void * result,
+                       void (* func)(struct etpan_thread_op * ))
 {
        struct etpan_thread_op * op;
        struct etpan_thread * thread;
+       struct mailimap * imap = get_imap(folder);
        
        imap_folder_ref(folder);
 
        op = etpan_thread_op_new();
        
-       op->imap = get_imap(folder);
+       op->imap = imap;
        op->param = param;
        op->result = result;
        
-       op->cancellable = 0;
        op->run = func;
        op->callback = generic_cb;
        op->callback_data = op;
-       op->cleanup = NULL;
-       
-       op->finished = 0;
-       
+
        thread = get_thread(folder);
        etpan_thread_op_schedule(thread, op);
        
        while (!op->finished) {
                gtk_main_iteration();
        }
-       
+
        etpan_thread_op_free(op);
 
        imap_folder_unref(folder);
+
+       if (imap != get_imap(folder)) {
+               g_warning("returning from operation on a stale imap %p", imap);
+               return 1;
+       }
+
+       return 0;
 }
 
 
@@ -467,6 +459,55 @@ struct connect_result {
        }                                                       \
 }
 
+
+static void delete_imap_run(struct etpan_thread_op * op)
+{
+       mailimap * imap = op->imap;
+
+       /* we don't want libetpan to logout */
+       if (imap->imap_stream) {
+               mailstream_close(imap->imap_stream);
+               imap->imap_stream = NULL;
+       }
+
+       mailimap_free(imap);
+}
+
+static void threaded_delete_imap(Folder *folder, mailimap *imap)
+{
+       struct etpan_thread_op * op;
+
+       /* No need to wait for completion, threaded_run() won't work here. */
+       op = etpan_thread_op_new();
+       op->imap = imap;
+       op->run = delete_imap_run;
+       op->cleanup = etpan_thread_op_free;
+
+       etpan_thread_op_schedule(get_thread(folder), op);
+
+       debug_print("threaded delete imap posted\n");
+}
+
+static void delete_imap(Folder *folder, mailimap *imap)
+{
+       chashdatum key;
+
+       key.data = &folder;
+       key.len = sizeof(folder);
+       chash_delete(session_hash, &key, NULL);
+
+       if (!imap)
+               return;
+       key.data = &imap;
+       key.len = sizeof(imap);
+       chash_delete(courier_workaround_hash, &key, NULL);
+       /* We can't just free imap here as there may be ops on it pending
+        * in the thread. Posting freeing as an op will synchronize against
+        * existing jobs and as imap is already removed from session_hash
+        * we are sure no new ops can be posted. */
+       threaded_delete_imap(folder, imap);
+}
+
 static void connect_run(struct etpan_thread_op * op)
 {
        int r;
@@ -519,7 +560,7 @@ int imap_threaded_connect(Folder * folder, const char * server, int port)
 
        return result.error;
 }
-
+#ifdef USE_GNUTLS
 static void connect_ssl_run(struct etpan_thread_op * op)
 {
        int r;
@@ -570,7 +611,8 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
                accept_if_valid = folder->account->ssl_certs_auto_accept;
 
        refresh_resolvers();
-       threaded_run(folder, &param, &result, connect_ssl_run);
+       if (threaded_run(folder, &param, &result, connect_ssl_run))
+               return MAILIMAP_ERROR_INVAL;
 
        if ((result.error == MAILIMAP_NO_ERROR_AUTHENTICATED ||
             result.error == MAILIMAP_NO_ERROR_NON_AUTHENTICATED) && !etpan_skip_ssl_cert_check) {
@@ -582,7 +624,7 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
 
        return result.error;
 }
-
+#endif
 struct capa_param {
        mailimap * imap;
 };
@@ -611,7 +653,7 @@ static void capability_run(struct etpan_thread_op * op)
 }
 
 
-struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *ok)
+int imap_threaded_capability(Folder *folder, struct mailimap_capability_data ** caps)
 {
        struct capa_param param;
        struct capa_result result;
@@ -625,10 +667,10 @@ struct mailimap_capability_data * imap_threaded_capability(Folder *folder, int *
        
        debug_print("capa %d\n", result.error);
        
-       if (ok)
-               *ok = result.error;
+       if (result.error == MAILIMAP_NO_ERROR)
+               *caps = result.caps;
 
-       return result.caps;
+       return result.error;
        
 }
        
@@ -670,13 +712,11 @@ void imap_threaded_disconnect(Folder * folder)
        
        param.imap = imap;
        
-       threaded_run(folder, &param, &result, disconnect_run);
-       
-       if (imap == get_imap(folder)) {
+       if (threaded_run(folder, &param, &result, disconnect_run)) {
+               debug_print("imap already deleted %p\n", imap);
+       } else {
                debug_print("deleting old imap %p\n", imap);
                delete_imap(folder, imap);
-       } else {
-               debug_print("imap already deleted %p\n", imap);
        }
        
        debug_print("disconnect ok\n");
@@ -853,7 +893,7 @@ static void login_run(struct etpan_thread_op * op)
        old_debug = mailstream_debug;
        mailstream_debug = 0;
 #endif
-       if (!strcmp(param->type, "LOGIN"))
+       if (!strcmp(param->type, "plaintext"))
                r = mailimap_login(param->imap,
                           param->login, param->password);
        else if (!strcmp(param->type, "GSSAPI"))
@@ -861,7 +901,16 @@ static void login_run(struct etpan_thread_op * op)
                        param->type, param->server, NULL, NULL,
                        param->login, param->login,
                        param->password, NULL);
-       else 
+       else if (!strcmp(param->type, "SCRAM-SHA-1"))
+               /* 7th argument has to be NULL here, to stop libetpan sending the
+                * a= attribute in its initial SCRAM-SHA-1 message to server. At least
+                * Dovecot 2.2 doesn't seem to like that, and will not authenticate
+                * succesfully. */
+               r = mailimap_authenticate(param->imap,
+                       param->type, NULL, NULL, NULL,
+                       NULL, param->login,
+                       param->password, NULL);
+       else
                r = mailimap_authenticate(param->imap,
                        param->type, NULL, NULL, NULL,
                        param->login, param->login,
@@ -885,6 +934,9 @@ int imap_threaded_login(Folder * folder,
        
        debug_print("imap login - begin\n");
        
+       if (!folder)
+               return MAILIMAP_ERROR_INVAL;
+
        param.imap = get_imap(folder);
        param.login = login;
        param.password = password;
@@ -1021,8 +1073,9 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
        imap = get_imap(folder);
        param.imap = imap;
 
-       threaded_run(folder, &param, &result, noop_run);
-       
+       if (threaded_run(folder, &param, &result, noop_run))
+               return MAILIMAP_ERROR_INVAL;
+
        if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
                * p_exists = imap->imap_selection_info->sel_exists;
                * p_recent = imap->imap_selection_info->sel_recent;
@@ -1049,7 +1102,7 @@ int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
        return result.error;
 }
 
-
+#ifdef USE_GNUTLS
 struct starttls_result {
        int error;
 };
@@ -1068,7 +1121,7 @@ static void starttls_run(struct etpan_thread_op * op)
        r = mailimap_starttls(param->imap);
        
        result->error = r;
-       debug_print("imap starttls run - end %i\n", r);
+       debug_print("imap STARTTLS run - end %i\n", r);
        
        if (r == 0) {
                mailimap *imap = param->imap;
@@ -1079,14 +1132,14 @@ static void starttls_run(struct etpan_thread_op * op)
                plain_low = mailstream_get_low(imap->imap_stream);
                fd = mailstream_low_get_fd(plain_low);
                if (fd == -1) {
-                       debug_print("imap starttls run - can't get fd\n");
+                       debug_print("imap STARTTLS run - can't get fd\n");
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
 
                tls_low = mailstream_low_tls_open_with_callback(fd, etpan_connect_ssl_context_cb, param->account);
                if (tls_low == NULL) {
-                       debug_print("imap starttls run - can't tls_open\n");
+                       debug_print("imap STARTTLS run - can't tls_open\n");
                        result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
@@ -1101,7 +1154,7 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        struct starttls_result result;
        gboolean accept_if_valid = FALSE;
 
-       debug_print("imap starttls - begin\n");
+       debug_print("imap STARTTLS - begin\n");
 
        param.imap = get_imap(folder);
        param.server = host;
@@ -1111,9 +1164,10 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        if (folder->account)
                accept_if_valid = folder->account->ssl_certs_auto_accept;
 
-       threaded_run(folder, &param, &result, starttls_run);
+       if (threaded_run(folder, &param, &result, starttls_run))
+               return MAILIMAP_ERROR_INVAL;
 
-       debug_print("imap starttls - end\n");
+       debug_print("imap STARTTLS - end\n");
 
        if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
                if (etpan_certificate_check(param.imap->imap_stream, host, port,
@@ -1122,8 +1176,7 @@ int imap_threaded_starttls(Folder * folder, const gchar *host, int port)
        }       
        return result.error;
 }
-
-
+#endif
 
 struct create_param {
        mailimap * imap;
@@ -1306,8 +1359,9 @@ int imap_threaded_select(Folder * folder, const char * mb,
        param.imap = imap;
        param.mb = mb;
        
-       threaded_run(folder, &param, &result, select_run);
-       
+       if (threaded_run(folder, &param, &result, select_run))
+               return MAILIMAP_ERROR_INVAL;
+
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
@@ -1457,8 +1511,9 @@ int imap_threaded_examine(Folder * folder, const char * mb,
        param.imap = imap;
        param.mb = mb;
        
-       threaded_run(folder, &param, &result, examine_run);
-       
+       if (threaded_run(folder, &param, &result, examine_run))
+               return MAILIMAP_ERROR_INVAL;
+
        if (result.error != MAILIMAP_NO_ERROR)
                return result.error;
        
@@ -1499,15 +1554,24 @@ static struct mailimap_set_item *sc_mailimap_set_item_copy(struct mailimap_set_i
 static struct mailimap_set *sc_mailimap_set_copy(struct mailimap_set *orig)
 {
        clist *list = orig ? orig->set_list : NULL;
-       clist *newlist = clist_new();
+       clist *newlist;
        clistiter *cur;
-       
+
        if (!orig)
                return NULL;
-       for (cur = clist_begin(list); cur; cur = clist_next(cur))
-               clist_append(newlist, 
+
+       newlist = clist_new();
+       if (!newlist)
+               return NULL;
+
+       for (cur = clist_begin(list); cur; cur = clist_next(cur)) {
+               if (clist_append(newlist,
                        sc_mailimap_set_item_copy(
-                       (struct mailimap_set_item *)clist_content(cur)));
+                       (struct mailimap_set_item *)clist_content(cur))) != 0) {
+                       clist_free(newlist);
+                       return NULL;
+               }
+       }
        return mailimap_set_new(newlist);
 }
 
@@ -1932,7 +1996,7 @@ static void fetch_uid_run(struct etpan_thread_op * op)
 
        fetch_result = NULL;
        mailstream_logger = imap_logger_noop;
-       log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
+       log_print(LOG_PROTOCOL, "IMAP- [fetching UIDs...]\n");
 
        r = imap_get_messages_list(param->imap, param->first_index,
                                   &fetch_result);
@@ -2189,7 +2253,7 @@ int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
        param.first_index = first_index;
        
        mailstream_logger = imap_logger_noop;
-       log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
+       log_print(LOG_PROTOCOL, "IMAP- [fetching flags...]\n");
 
        threaded_run(folder, &param, &result, fetch_uid_flags_run);
 
@@ -2695,11 +2759,11 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
                                  carray ** p_env_list)
 {
        clistiter * cur;
-       carray * env_list;
 
-       env_list = carray_new(16);
-  
        if (fetch_result) {
+               carray * env_list;
+               env_list = carray_new(16);
+
                for(cur = clist_begin(fetch_result) ; cur != NULL ;
                    cur = clist_next(cur)) {
                        struct mailimap_msg_att * msg_att;
@@ -2709,10 +2773,12 @@ imap_fetch_result_to_envelop_list(clist * fetch_result,
                        msg_att = clist_content(cur);
 
                        env_info = fetch_to_env_info(msg_att, &tags);
-                       if (!env_info)
+                       if (!env_info
+                        || carray_add(env_list, env_info, NULL) != 0
+                        || carray_add(env_list, tags, NULL) != 0) {
+                               carray_free(env_list);
                                return MAILIMAP_ERROR_MEMORY;
-                       carray_add(env_list, env_info, NULL);
-                       carray_add(env_list, tags, NULL);
+                       }
                }
                * p_env_list = env_list;
        } else {
@@ -2736,19 +2802,23 @@ static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
                };
 
        hdrlist = clist_new();
+       if (!hdrlist)
+               return MAIL_ERROR_MEMORY;
        i = 0;
        while (headers[i] != NULL) {
                header = strdup(headers[i]);
-               if (header == NULL || clist_append(hdrlist, header) != 0)
+               if (header == NULL || clist_append(hdrlist, header) != 0) {
+                       clist_free(hdrlist);
                        return MAIL_ERROR_MEMORY;
+               }
                ++i;
        }
-  
+
        imap_hdrlist = mailimap_header_list_new(hdrlist);
        section = mailimap_section_new_header_fields(imap_hdrlist);
        fetch_att = mailimap_fetch_att_new_body_peek_section(section);
        mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
-  
+
        return MAIL_NO_ERROR;
 }
 
@@ -2799,7 +2869,12 @@ imap_get_envelopes_list(mailimap * imap, struct mailimap_set * set,
                r = imap_add_envelope_fetch_att(fetch_type);
        else
                r = imap_add_header_fetch_att(fetch_type);
-       
+
+       if (r != MAILIMAP_NO_ERROR) {
+               debug_print("add fetch attr: %d\n", r);
+               return r;
+       }
+
        mailstream_logger = imap_logger_fetch;
        
        r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
@@ -2885,8 +2960,9 @@ int imap_threaded_fetch_env(Folder * folder, struct mailimap_set * set,
        param.imap = imap;
        param.set = set;
        
-       threaded_run(folder, &param, &result, fetch_env_run);
-       
+       if (threaded_run(folder, &param, &result, fetch_env_run))
+               return MAILIMAP_ERROR_INVAL;
+
        if (result.error != MAILIMAP_NO_ERROR) {
                chashdatum key;
                chashdatum value;