[evolution-kolab/ek-wip-porting-imapx: 107/109] CamelIMAPXExtdServer: adapted to upstream changes



commit 1cc5a1fa90fc2c97585594c9a2afa3e109df63b7
Author: Christian Hilberg <hilberg kernelconcepts de>
Date:   Thu Feb 2 19:12:54 2012 +0100

    CamelIMAPXExtdServer: adapted to upstream changes
    
    * adapted to make use of CamelIMAPXJob
      and CamelIMAPXCommand (new classes
      from upstream which have been extracted
      from CamelIMAQPXServer)
    * pulled in upstream fixes for deadlock issues
    * updated for unified upstream CamelIMAPXServer
      function param naming scheme

 .../providers/imapx/camel-imapx-extd-server.c      |  962 ++++++++++----------
 1 files changed, 466 insertions(+), 496 deletions(-)
---
diff --git a/src/camel/providers/imapx/camel-imapx-extd-server.c b/src/camel/providers/imapx/camel-imapx-extd-server.c
index fd74423..3a6c661 100644
--- a/src/camel/providers/imapx/camel-imapx-extd-server.c
+++ b/src/camel/providers/imapx/camel-imapx-extd-server.c
@@ -38,9 +38,11 @@
 #include <glib/gstdio.h>
 #include <glib/gi18n-lib.h>
 
+#include "camel-imapx-command.h"
+#include "camel-imapx-job.h"
 #include "camel-imapx-settings.h"
-#include "camel-imapx-utils.h"
 #include "camel-imapx-summary.h"
+#include "camel-imapx-utils.h"
 
 #include "camel-imapx-extd-folder.h"
 #include "camel-imapx-extd-store.h"
@@ -114,91 +116,85 @@ camel_imapx_extd_server_finalize (GObject *object)
 /*----------------------------------------------------------------------------*/
 /* internal statics */
 
-static gint
-extd_server_untagged (CamelIMAPXServer *self,
+static gboolean
+extd_server_untagged (CamelIMAPXExtdServer *self,
                       GCancellable *cancellable,
-                      GError **err)
+                      GError **error)
 {
-	/* restructured and modified dupe of imapx_untagged */
+	/* modified dupe of imapx_untagged */
 
-	CamelIMAPXServer *imap = NULL;
+	CamelIMAPXServer *is = NULL;
 	CamelService *service = NULL;
 	CamelSettings *settings = NULL;
 	CamelSortType fetch_order;
-	guint id = 0, len = 0;
+	guint id, len;
 	guchar *token = NULL, *p = NULL, c;
-	gint tok = 0;
+	gint tok;
 	gboolean lsub = FALSE;
 	struct _status_info *sinfo = NULL;
 
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	/* cancellable may be NULL */
-	g_return_val_if_fail (err == NULL || *err == NULL, -1);
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-	imap = CAMEL_IMAPX_SERVER (self);
-	service = CAMEL_SERVICE (imap->store);
+	is = CAMEL_IMAPX_SERVER (self);
+	service = CAMEL_SERVICE (is->store);
 	settings = camel_service_get_settings (service);
 
-	fetch_order = \
-		camel_imapx_settings_get_fetch_order (CAMEL_IMAPX_SETTINGS (settings));
+	fetch_order = camel_imapx_settings_get_fetch_order (
+		CAMEL_IMAPX_SETTINGS (settings));
 
-	e(imap->tagprefix, "got untagged response\n");
+	e(is->tagprefix, "got untagged response\n");
 	id = 0;
-	tok = camel_imapx_stream_token (imap->stream, &token, &len, cancellable, err);
+	tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 	if (tok < 0)
-		return -1;
+		return FALSE;
 
 	if (tok == IMAPX_TOK_INT) {
 		id = strtoul ((gchar *) token, NULL, 10);
-		tok = camel_imapx_stream_token (imap->stream, &token, &len, cancellable, err);
+		tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 		if (tok < 0)
-			return -1;
+			return FALSE;
 	}
 
 	if (tok == '\n') {
-		g_set_error (err,
-		             CAMEL_IMAPX_ERROR,
-		             1,
-		             "truncated server response");
-		return -1;
+		g_set_error (
+			error, CAMEL_IMAPX_ERROR, 1,
+			"truncated server response");
+		return FALSE;
 	}
 
-	e(imap->tagprefix, "Have token '%s' id %d\n", token, id);
+	e(is->tagprefix, "Have token '%s' id %d\n", token, id);
 	p = token;
 	while ((c = *p))
 		*p++ = toupper((gchar) c);
 
 	switch (imapx_tokenise ((const gchar *) token, len)) {
 	case IMAPX_CAPABILITY:
-		if (imap->cinfo)
-			imapx_free_capability (imap->cinfo);
-		imap->cinfo = imapx_parse_capability (imap->stream, cancellable, err);
-		if (imap->cinfo == NULL)
-			return -1;
-		c(imap->tagprefix, "got capability flags %08x\n", imap->cinfo->capa);
-		return 0;
+		if (is->cinfo)
+			imapx_free_capability (is->cinfo);
+		is->cinfo = imapx_parse_capability (is->stream, cancellable, error);
+		if (is->cinfo == NULL)
+			return FALSE;
+		c(is->tagprefix, "got capability flags %08x\n", is->cinfo->capa);
+		return TRUE;
 	case IMAPX_EXPUNGE: {
 		guint32 expunge = id;
-		struct _CamelIMAPXJob *job = \
-			camel_imapx_server_match_active_job (imap,
-			                                     IMAPX_JOB_EXPUNGE,
-			                                     NULL);
+		CamelIMAPXJob *job = camel_imapx_server_match_active_job (is, IMAPX_JOB_EXPUNGE, NULL);
 
 		/* If there is a job running, let it handle the deletion */
 		if (job)
 			break;
 
-		c(imap->tagprefix, "expunged: %d\n", id);
-		if (imap->select_folder) {
+		c(is->tagprefix, "expunged: %d\n", id);
+		if (is->select_folder) {
 			gchar *uid = NULL;
 
-			uid = camel_imapx_server_get_uid_from_index (imap,
-			                                             imap->select_folder->summary,
-			                                             expunge - 1);
+			uid = camel_imapx_server_get_uid_from_index (is, is->select_folder->summary, expunge - 1);
 			if (!uid)
 				break;
 
-			camel_imapx_server_expunge_uid_from_summary (imap, uid, TRUE);
+			camel_imapx_server_expunge_uid_from_summary (is, uid, TRUE);
 		}
 
 		break;
@@ -211,27 +207,27 @@ extd_server_untagged (CamelIMAPXServer *self,
 		guchar *token;
 		gint tok;
 
-		tok = camel_imapx_stream_token (imap->stream, &token, &len, cancellable, err);
+		tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 		if (tok < 0)
-			return -1;
+			return FALSE;
 		if (tok == '(') {
 			unsolicited = FALSE;
 			while (tok != ')') {
 				/* We expect this to be 'EARLIER' */
-				tok = camel_imapx_stream_token (imap->stream, &token, &len, cancellable, err);
+				tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 				if (tok < 0)
-					return -1;
+					return FALSE;
 			}
 		} else
-			camel_imapx_stream_ungettoken (imap->stream, tok, token, len);
+			camel_imapx_stream_ungettoken (is->stream, tok, token, len);
 
-		uids = imapx_parse_uids (imap->stream, cancellable, err);
+		uids = imapx_parse_uids (is->stream, cancellable, error);
 		if (uids == NULL)
-			return -1;
+			return FALSE;
 		for (i = 0; i < uids->len; i++) {
 			gchar *uid = g_strdup_printf("%u", GPOINTER_TO_UINT(g_ptr_array_index (uids, i)));
-			c(imap->tagprefix, "vanished: %s\n", uid);
-			camel_imapx_server_expunge_uid_from_summary (imap, uid, unsolicited);
+			c(is->tagprefix, "vanished: %s\n", uid);
+			camel_imapx_server_expunge_uid_from_summary (is, uid, unsolicited);
 		}
 		g_ptr_array_free (uids, FALSE);
 		break;
@@ -239,9 +235,9 @@ extd_server_untagged (CamelIMAPXServer *self,
 	case IMAPX_NAMESPACE: {
 		CamelIMAPXNamespaceList *nsl = NULL;
 
-		nsl = imapx_parse_namespace_list (imap->stream, cancellable, err);
+		nsl = imapx_parse_namespace_list (is->stream, cancellable, error);
 		if (nsl != NULL) {
-			CamelIMAPXStore *imapx_store = (CamelIMAPXStore *) imap->store;
+			CamelIMAPXStore *imapx_store = (CamelIMAPXStore *) is->store;
 			CamelIMAPXStoreNamespace *ns;
 
 			imapx_store->summary->namespaces = nsl;
@@ -253,128 +249,127 @@ extd_server_untagged (CamelIMAPXServer *self,
 				imapx_store->dir_sep = ns->sep;
 		}
 
-		return 0;
+		return TRUE;
 	}
 	case IMAPX_EXISTS:
-		c(imap->tagprefix, "exists: %d\n", id);
-		imap->exists = id;
+		c(is->tagprefix, "exists: %d\n", id);
+		is->exists = id;
 
-		if (imap->select_folder)
-			((CamelIMAPXFolder *) imap->select_folder)->exists_on_server = id;
+		if (is->select_folder)
+			((CamelIMAPXFolder *) is->select_folder)->exists_on_server = id;
 
-		if (camel_imapx_server_idle_supported (imap) &&
-		    camel_imapx_server_in_idle (imap)) {
-			if (camel_folder_summary_count (imap->select_folder->summary) < id)
-				camel_imapx_server_stop_idle (imap, err);
+		if (camel_imapx_server_idle_supported (is) && camel_imapx_server_in_idle (is)) {
+			if (camel_folder_summary_count (is->select_folder->summary) < id)
+				camel_imapx_server_stop_idle (is, error);
 		}
 
 		break;
 	case IMAPX_FLAGS: {
 		guint32 flags;
 
-		imapx_parse_flags (imap->stream, &flags, NULL, cancellable, err);
+		imapx_parse_flags (is->stream, &flags, NULL, cancellable, error);
 
-		c(imap->tagprefix, "flags: %08x\n", flags);
+		c(is->tagprefix, "flags: %08x\n", flags);
 		break;
 	}
 	case IMAPX_FETCH: {
 		struct _fetch_info *finfo;
 
-		finfo = imapx_parse_fetch (imap->stream, cancellable, err);
+		finfo = imapx_parse_fetch (is->stream, cancellable, error);
 		if (finfo == NULL) {
 			imapx_free_fetch (finfo);
-			return -1;
+			return FALSE;
 		}
 
 		if ((finfo->got & (FETCH_BODY | FETCH_UID)) == (FETCH_BODY | FETCH_UID)) {
-			struct _CamelIMAPXJob *job = \
-				camel_imapx_server_match_active_job (imap,
-				                                     IMAPX_JOB_GET_MESSAGE,
-				                                     finfo->uid);
+			CamelIMAPXJob *job = camel_imapx_server_match_active_job (is, IMAPX_JOB_GET_MESSAGE, finfo->uid);
+			GetMessageData *data;
+
+			data = camel_imapx_job_get_data (job);
+			g_return_val_if_fail (data != NULL, FALSE);
 
 			/* This must've been a get-message request, fill out the body stream,
 			 * in the right spot */
 
 			if (job && job->error == NULL) {
-				if (job->u.get_message.use_multi_fetch) {
-					job->u.get_message.body_offset = finfo->offset;
-					g_seekable_seek (G_SEEKABLE (job->u.get_message.stream), finfo->offset, G_SEEK_SET, NULL, NULL);
+				if (data->use_multi_fetch) {
+					data->body_offset = finfo->offset;
+					g_seekable_seek (G_SEEKABLE (data->stream), finfo->offset, G_SEEK_SET, NULL, NULL);
 				}
 
-				job->u.get_message.body_len = camel_stream_write_to_stream (finfo->body, job->u.get_message.stream, job->cancellable, &job->error);
-				if (job->u.get_message.body_len == -1)
+				data->body_len = camel_stream_write_to_stream (finfo->body, data->stream, job->cancellable, &job->error);
+				if (data->body_len == -1)
 					g_prefix_error (
-					                &job->error,
-					                _("Error writing to cache stream: "));
+						&job->error,
+						_("Error writing to cache stream: "));
 			}
 		}
 
 		if ((finfo->got & FETCH_FLAGS) && !(finfo->got & FETCH_HEADER)) {
-			struct _CamelIMAPXJob *job = \
-				camel_imapx_server_match_active_job (imap,
-				                                     IMAPX_JOB_FETCH_NEW_MESSAGES | IMAPX_JOB_REFRESH_INFO,
-				                                     NULL);
+			CamelIMAPXJob *job = camel_imapx_server_match_active_job (is, IMAPX_JOB_FETCH_NEW_MESSAGES | IMAPX_JOB_REFRESH_INFO, NULL);
 			/* This is either a refresh_info job, check to see if it is and update
 			 * if so, otherwise it must've been an unsolicited response, so update
 			 * the summary to match */
 
 			if (job && (finfo->got & FETCH_UID)) {
+				RefreshInfoData *data;
 				struct _refresh_info r;
 
+				data = camel_imapx_job_get_data (job);
+				g_return_val_if_fail (data != NULL, FALSE);
+
 				r.uid = finfo->uid;
 				finfo->uid = NULL;
 				r.server_flags = finfo->flags;
 				r.server_user_flags = finfo->user_flags;
 				finfo->user_flags = NULL;
 				r.exists = FALSE;
-				g_array_append_val (job->u.refresh_info.infos, r);
-			} else if (imap->select_folder) {
+				g_array_append_val (data->infos, r);
+			} else if (is->select_folder) {
 				CamelFolder *folder;
 				CamelMessageInfo *mi = NULL;
 				gboolean changed = FALSE;
 				gchar *uid = NULL;
 
-				g_object_ref (imap->select_folder);
-				folder = imap->select_folder;
+				g_object_ref (is->select_folder);
+				folder = is->select_folder;
 
-				c(imap->tagprefix, "flag changed: %d\n", id);
+				c(is->tagprefix, "flag changed: %d\n", id);
 
 				if (finfo->got & FETCH_UID) {
 					uid = finfo->uid;
 					finfo->uid = NULL;
 				} else {
-					uid = camel_imapx_server_get_uid_from_index (imap, folder->summary, id - 1);
+					uid = camel_imapx_server_get_uid_from_index (is, folder->summary, id - 1);
 				}
 
 				if (uid) {
 					mi = camel_folder_summary_get (folder->summary, uid);
 					if (mi) {
-						/* It's unsolicited _unless_ imap->select_pending (i.e. during
+						/* It's unsolicited _unless_ is->select_pending (i.e. during
 						 * a QRESYNC SELECT */
-						changed = imapx_update_message_info_flags (mi, finfo->flags, finfo->user_flags, imap->permanentflags, folder, !imap->select_pending);
+						changed = imapx_update_message_info_flags (mi, finfo->flags, finfo->user_flags, is->permanentflags, folder, !is->select_pending);
 					} else {
 						/* This (UID + FLAGS for previously unknown message) might
 						 * happen during a SELECT (QRESYNC). We should use it. */
-						c(imap->tagprefix, "flags changed for unknown uid %s\n.", uid);
+						c(is->tagprefix, "flags changed for unknown uid %s\n.", uid);
 					}
 					finfo->user_flags = NULL;
 				}
 
 				if (changed) {
-					if (imap->changes == NULL)
-						imap->changes = camel_folder_change_info_new ();
+					if (is->changes == NULL)
+						is->changes = camel_folder_change_info_new ();
 
-					camel_folder_change_info_change_uid (imap->changes, uid);
+					camel_folder_change_info_change_uid (is->changes, uid);
 					g_free (uid);
 				}
 
-				if (camel_imapx_server_idle_supported (imap) &&
-				    changed &&
-				    camel_imapx_server_in_idle (imap)) {
-					camel_folder_summary_save_to_db (imap->select_folder->summary, NULL);
-					imapx_update_store_summary (imap->select_folder);
-					camel_folder_changed (imap->select_folder, imap->changes);
-					camel_folder_change_info_clear (imap->changes);
+				if (camel_imapx_server_idle_supported (is) && changed && camel_imapx_server_in_idle (is)) {
+					camel_folder_summary_save_to_db (is->select_folder->summary, NULL);
+					imapx_update_store_summary (is->select_folder);
+					camel_folder_changed (is->select_folder, is->changes);
+					camel_folder_change_info_clear (is->changes);
 				}
 
 				if (mi)
@@ -384,10 +379,7 @@ extd_server_untagged (CamelIMAPXServer *self,
 		}
 
 		if ((finfo->got & (FETCH_HEADER | FETCH_UID)) == (FETCH_HEADER | FETCH_UID)) {
-			struct _CamelIMAPXJob *job = \
-				camel_imapx_server_match_active_job (imap,
-				                                     IMAPX_JOB_FETCH_NEW_MESSAGES | IMAPX_JOB_REFRESH_INFO,
-				                                     NULL);
+			CamelIMAPXJob *job = camel_imapx_server_match_active_job (is, IMAPX_JOB_FETCH_NEW_MESSAGES | IMAPX_JOB_REFRESH_INFO, NULL);
 
 			/* This must be a refresh info job as well, but it has asked for
 			 * new messages to be added to the index */
@@ -411,37 +403,41 @@ extd_server_untagged (CamelIMAPXServer *self,
 
 					mi->uid = camel_pstring_strdup (finfo->uid);
 
-					if (!(finfo->got & FETCH_FLAGS))
-						{
-							struct _refresh_info *r = NULL;
-							GArray *infos = job->u.refresh_info.infos;
-							gint min = job->u.refresh_info.last_index;
-							gint max = job->u.refresh_info.index, mid;
-							gboolean found = FALSE;
+					if (!(finfo->got & FETCH_FLAGS)) {
+						RefreshInfoData *data;
+						struct _refresh_info *r = NULL;
+						gint min, max, mid;
+						gboolean found = FALSE;
 
-							/* array is sorted, so use a binary search */
-							do {
-								gint cmp = 0;
+						data = camel_imapx_job_get_data (job);
+						g_return_val_if_fail (data != NULL, FALSE);
 
-								mid = (min + max) / 2;
-								r = &g_array_index (infos, struct _refresh_info, mid);
-								cmp = camel_imapx_server_refresh_info_uid_cmp (imap, finfo->uid, r->uid, fetch_order == CAMEL_SORT_ASCENDING);
+						min = data->last_index;
+						max = data->index;
 
-								if (cmp > 0)
-									min = mid + 1;
-								else if (cmp < 0)
-									max = mid - 1;
-								else
-									found = TRUE;
+						/* array is sorted, so use a binary search */
+						do {
+							gint cmp = 0;
 
-							} while (!found && min <= max);
+							mid = (min + max) / 2;
+							r = &g_array_index (data->infos, struct _refresh_info, mid);
+							cmp = camel_imapx_server_refresh_info_uid_cmp (is, finfo->uid, r->uid, fetch_order == CAMEL_SORT_ASCENDING);
 
-							if (!found)
-								g_assert_not_reached ();
+							if (cmp > 0)
+								min = mid + 1;
+							else if (cmp < 0)
+								max = mid - 1;
+							else
+								found = TRUE;
 
-							server_flags = r->server_flags;
-							server_user_flags = r->server_user_flags;
-						} else {
+						} while (!found && min <= max);
+
+						if (!found)
+							g_assert_not_reached ();
+
+						server_flags = r->server_flags;
+						server_user_flags = r->server_user_flags;
+					} else {
 						server_flags = finfo->flags;
 						server_user_flags = finfo->user_flags;
 						/* free user_flags ? */
@@ -459,10 +455,10 @@ extd_server_untagged (CamelIMAPXServer *self,
 						guint64 uidl = strtoull (mi->uid, NULL, 10);
 
 						if (uidl >= ifolder->uidnext_on_server) {
-							c(imap->tagprefix, "Updating unread count for new message %s\n", mi->uid);
+							c(is->tagprefix, "Updating unread count for new message %s\n", mi->uid);
 							((CamelIMAPXFolder *) job->folder)->unread_on_server++;
 						} else {
-							c(imap->tagprefix, "Not updating unread count for new message %s\n", mi->uid);
+							c(is->tagprefix, "Not updating unread count for new message %s\n", mi->uid);
 						}
 					}
 
@@ -470,15 +466,19 @@ extd_server_untagged (CamelIMAPXServer *self,
 					binfo->size = finfo->size;
 
 					if (!camel_folder_summary_check_uid (job->folder->summary, mi->uid)) {
+						RefreshInfoData *data;
 						CamelIMAPXFolder *ifolder = (CamelIMAPXFolder *) job->folder;
 						gint cnt;
 
+						data = camel_imapx_job_get_data (job);
+						g_return_val_if_fail (data != NULL, FALSE);
+
 						camel_folder_summary_add (job->folder->summary, mi);
 						imapx_set_message_info_flags_for_new_message (mi, server_flags, server_user_flags, job->folder);
-						camel_folder_change_info_add_uid (job->u.refresh_info.changes, mi->uid);
+						camel_folder_change_info_add_uid (data->changes, mi->uid);
 
 						if (!g_hash_table_lookup (ifolder->ignore_recent, mi->uid)) {
-							camel_folder_change_info_recent_uid (job->u.refresh_info.changes, mi->uid);
+							camel_folder_change_info_recent_uid (data->changes, mi->uid);
 							g_hash_table_remove (ifolder->ignore_recent, mi->uid);
 						}
 
@@ -499,29 +499,31 @@ extd_server_untagged (CamelIMAPXServer *self,
 	case IMAPX_LSUB:
 		lsub = TRUE;
 	case IMAPX_LIST: {
-		struct _list_info *linfo = imapx_parse_list (imap->stream, cancellable, err);
-		struct _CamelIMAPXJob *job;
+		struct _list_info *linfo = imapx_parse_list (is->stream, cancellable, error);
+		CamelIMAPXJob *job;
+		ListData *data;
 
 		if (!linfo)
 			break;
 
-		job = camel_imapx_server_match_active_job (imap,
-		                                           IMAPX_JOB_LIST,
-		                                           linfo->name);
+		job = camel_imapx_server_match_active_job (is, IMAPX_JOB_LIST, linfo->name);
+
+		data = camel_imapx_job_get_data (job);
+		g_return_val_if_fail (data != NULL, FALSE);
 
 		// TODO: we want to make sure the names match?
 
-		if (job->u.list.flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED) {
-			c(imap->tagprefix, "lsub: '%s' (%c)\n", linfo->name, linfo->separator);
+		if (data->flags & CAMEL_STORE_FOLDER_INFO_SUBSCRIBED) {
+			c(is->tagprefix, "lsub: '%s' (%c)\n", linfo->name, linfo->separator);
 
 		} else {
-			c(imap->tagprefix, "list: '%s' (%c)\n", linfo->name, linfo->separator);
+			c(is->tagprefix, "list: '%s' (%c)\n", linfo->name, linfo->separator);
 		}
 
-		if (job && g_hash_table_lookup (job->u.list.folders, linfo->name) == NULL) {
+		if (job && g_hash_table_lookup (data->folders, linfo->name) == NULL) {
 			if (lsub)
 				linfo->flags |= CAMEL_FOLDER_SUBSCRIBED;
-			g_hash_table_insert (job->u.list.folders, linfo->name, linfo);
+			g_hash_table_insert (data->folders, linfo->name, linfo);
 		} else {
 			g_warning("got list response but no current listing job happening?\n");
 			imapx_free_list (linfo);
@@ -529,13 +531,13 @@ extd_server_untagged (CamelIMAPXServer *self,
 		break;
 	}
 	case IMAPX_RECENT:
-		c(imap->tagprefix, "recent: %d\n", id);
-		imap->recent = id;
+		c(is->tagprefix, "recent: %d\n", id);
+		is->recent = id;
 		break;
 	case IMAPX_STATUS: {
-		struct _state_info *sinfo = imapx_parse_status_info (imap->stream, cancellable, err);
+		struct _state_info *sinfo = imapx_parse_status_info (is->stream, cancellable, error);
 		if (sinfo) {
-			CamelIMAPXStoreSummary *s = ((CamelIMAPXStore *) imap->store)->summary;
+			CamelIMAPXStoreSummary *s = ((CamelIMAPXStore *) is->store)->summary;
 			CamelIMAPXStoreNamespace *ns;
 			CamelIMAPXFolder *ifolder = NULL;;
 
@@ -544,9 +546,9 @@ extd_server_untagged (CamelIMAPXServer *self,
 				gchar *path_name;
 
 				path_name = camel_imapx_store_summary_full_to_path (s, sinfo->name, ns->sep);
-				c(imap->tagprefix, "Got folder path '%s' for full '%s'\n", path_name, sinfo->name);
+				c(is->tagprefix, "Got folder path '%s' for full '%s'\n", path_name, sinfo->name);
 				if (path_name) {
-					ifolder = (gpointer) camel_store_get_folder_sync (imap->store, path_name, 0, cancellable, err);
+					ifolder = (gpointer) camel_store_get_folder_sync (is->store, path_name, 0, cancellable, error);
 					g_free (path_name);
 				}
 			}
@@ -559,9 +561,9 @@ extd_server_untagged (CamelIMAPXServer *self,
 				ifolder->uidnext_on_server = sinfo->uidnext;
 				ifolder->uidvalidity_on_server = sinfo->uidvalidity;
 				if (sinfo->uidvalidity && sinfo->uidvalidity != ((CamelIMAPXSummary *) cfolder->summary)->validity)
-					camel_imapx_server_invalidate_local_cache (imap, ifolder, sinfo->uidvalidity);
+					camel_imapx_server_invalidate_local_cache (is, ifolder, sinfo->uidvalidity);
 			} else {
-				c(imap->tagprefix, "Received STATUS for unknown folder '%s'\n", sinfo->name);
+				c(is->tagprefix, "Received STATUS for unknown folder '%s'\n", sinfo->name);
 			}
 
 			g_free (sinfo->name);
@@ -572,181 +574,194 @@ extd_server_untagged (CamelIMAPXServer *self,
 	case IMAPX_BYE: {
 		guchar *token;
 
-		if (camel_imapx_stream_text (imap->stream, &token, cancellable, NULL)) {
-			c(imap->tagprefix, "BYE: %s\n", token);
-			g_set_error (err, CAMEL_IMAPX_ERROR, 1,
-			             "IMAP server said BYE: %s", token);
+		if (camel_imapx_stream_text (is->stream, &token, cancellable, NULL)) {
+			c(is->tagprefix, "BYE: %s\n", token);
+			g_set_error (
+				error, CAMEL_IMAPX_ERROR, 1,
+				"IMAP server said BYE: %s", token);
 		}
-		imap->state = IMAPX_SHUTDOWN;
-		return -1;
+		is->state = IMAPX_SHUTDOWN;
+		return FALSE;
 	}
 	case IMAPX_PREAUTH:
-		c(imap->tagprefix, "preauthenticated\n");
-		if (imap->state < IMAPX_AUTHENTICATED)
-			imap->state = IMAPX_AUTHENTICATED;
+		c(is->tagprefix, "preauthenticated\n");
+		if (is->state < IMAPX_AUTHENTICATED)
+			is->state = IMAPX_AUTHENTICATED;
 		/* fall through... */
 	case IMAPX_OK: case IMAPX_NO: case IMAPX_BAD:
 		/* TODO: validate which ones of these can happen as unsolicited responses */
 		/* TODO: handle bye/preauth differently */
-		camel_imapx_stream_ungettoken (imap->stream, tok, token, len);
-		sinfo = imapx_parse_status (imap->stream, cancellable, err);
+		camel_imapx_stream_ungettoken (is->stream, tok, token, len);
+		sinfo = imapx_parse_status (is->stream, cancellable, error);
 		if (sinfo == NULL)
-			return -1;
+			return FALSE;
 		switch (sinfo->condition) {
 		case IMAPX_CLOSED:
-			c(imap->tagprefix, "previously selected folder is now closed\n");
-			if (imap->select_pending && !imap->select_folder) {
-				imap->select_folder = imap->select_pending;
+			c(is->tagprefix, "previously selected folder is now closed\n");
+			if (is->select_pending && !is->select_folder) {
+				is->select_folder = is->select_pending;
 			}
 			break;
 		case IMAPX_READ_WRITE:
-			imap->mode = IMAPX_MODE_READ | IMAPX_MODE_WRITE;
-			c(imap->tagprefix, "folder is read-write\n");
+			is->mode = IMAPX_MODE_READ | IMAPX_MODE_WRITE;
+			c(is->tagprefix, "folder is read-write\n");
 			break;
 		case IMAPX_READ_ONLY:
-			imap->mode = IMAPX_MODE_READ;
-			c(imap->tagprefix, "folder is read-only\n");
+			is->mode = IMAPX_MODE_READ;
+			c(is->tagprefix, "folder is read-only\n");
 			break;
 		case IMAPX_UIDVALIDITY:
-			imap->uidvalidity = sinfo->u.uidvalidity;
+			is->uidvalidity = sinfo->u.uidvalidity;
 			break;
 		case IMAPX_UNSEEN:
-			imap->unseen = sinfo->u.unseen;
+			is->unseen = sinfo->u.unseen;
 			break;
 		case IMAPX_HIGHESTMODSEQ:
-			imap->highestmodseq = sinfo->u.highestmodseq;
+			is->highestmodseq = sinfo->u.highestmodseq;
 			break;
 		case IMAPX_PERMANENTFLAGS:
-			imap->permanentflags = sinfo->u.permanentflags;
+			is->permanentflags = sinfo->u.permanentflags;
 			break;
 		case IMAPX_UIDNEXT:
-			imap->uidnext = sinfo->u.uidnext;
+			is->uidnext = sinfo->u.uidnext;
 			break;
 		case IMAPX_ALERT:
-			c(imap->tagprefix, "ALERT!: %s\n", sinfo->text);
+			c(is->tagprefix, "ALERT!: %s\n", sinfo->text);
 			break;
 		case IMAPX_PARSE:
-			c(imap->tagprefix, "PARSE: %s\n", sinfo->text);
+			c(is->tagprefix, "PARSE: %s\n", sinfo->text);
 			break;
 		case IMAPX_CAPABILITY:
 			if (sinfo->u.cinfo) {
-				struct _capability_info *cinfo = imap->cinfo;
-				imap->cinfo = sinfo->u.cinfo;
+				struct _capability_info *cinfo = is->cinfo;
+				is->cinfo = sinfo->u.cinfo;
 				sinfo->u.cinfo = NULL;
 				if (cinfo)
 					imapx_free_capability (cinfo);
-				c(imap->tagprefix, "got capability flags %08x\n", imap->cinfo->capa);
+				c(is->tagprefix, "got capability flags %08x\n", is->cinfo->capa);
 			}
 			break;
 		default:
 			break;
 		}
 		imapx_free_status (sinfo);
-		return 0;
+		return TRUE;
 	default:
 		/* unknown response, just ignore it */
-		c(imap->tagprefix, "unknown token: %s\n", token);
+		c(is->tagprefix, "unknown token: %s\n", token);
 	}
 
-	return camel_imapx_stream_skip (imap->stream, cancellable, err);
+	return (camel_imapx_stream_skip (is->stream, cancellable, error) == 0);
 }
 
-static void
-extd_server_step (CamelIMAPXServer *self,
+static gboolean
+extd_server_step (CamelIMAPXExtdServer *self,
                   GCancellable *cancellable,
-                  GError **err)
+                  GError **error)
 {
 	/* modified dupe of imapx_step() */
+
+	CamelIMAPXServer *is = NULL;
 	guint len = 0;
 	guchar *token = NULL;
-	gint tok = 0;
+	gint tok = -1;
 
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	/* cancellable may be NULL */
-	g_return_if_fail (err == NULL || *err == NULL);
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+	is = CAMEL_IMAPX_SERVER (self);
 
 	// poll ?  wait for other stuff? loop?
-	tok = camel_imapx_stream_token (self->stream, &token, &len, cancellable, err);
+	tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 	if (tok < 0)
-		return;
+		return FALSE;
 
 	if (tok == '*')
-		/* modified */
-		extd_server_untagged (self, cancellable, err);
+		return extd_server_untagged (self, cancellable, error);
 	else if (tok == IMAPX_TOK_TOKEN)
-		camel_imapx_server_completion (self, token, len, cancellable, err);
+		return camel_imapx_server_completion (is, token, len, cancellable, error);
 	else if (tok == '+')
-		camel_imapx_server_continuation (self, FALSE, cancellable, err);
-	else
-		g_set_error (err,
-		             CAMEL_IMAPX_ERROR,
-		             1,
-		             "unexpected server response:");
+		return camel_imapx_server_continuation (is, FALSE, cancellable, error);
+
+	g_set_error (
+		error, CAMEL_IMAPX_ERROR, 1,
+		"unexpected server response:");
+
+	return FALSE;
 }
 
 /* Used to run 1 command synchronously,
  * use for capa, login, and namespaces only. */
 static gboolean
-extd_server_command_run (CamelIMAPXServer *self,
-                         CamelIMAPXCommand *ic)
+extd_server_command_run (CamelIMAPXExtdServer *self,
+                         CamelIMAPXCommand *ic,
+                         GCancellable *cancellable,
+                         GError **error)
 {
 	/* modified dupe of imapx_command_run() */
 
+	CamelIMAPXServer *is = NULL;
+	gboolean success = TRUE;
+
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	g_assert (ic != NULL);
+	/* cancellable may be NULL */
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-	camel_imapx_server_command_close (self, ic);
+	is = CAMEL_IMAPX_SERVER (self);
 
-	QUEUE_LOCK (self);
-	camel_imapx_server_command_start (self, ic);
-	QUEUE_UNLOCK (self);
+	camel_imapx_command_close (ic);
 
-	while (ic->status == NULL && ic->error == NULL) {
-		/* modified */
-		extd_server_step (self, ic->cancellable, &ic->error);
-	}
+	QUEUE_LOCK (is);
+	camel_imapx_server_command_start (is, ic, cancellable, error);
+	QUEUE_UNLOCK (is);
+
+	while (success && ic->status == NULL)
+		success = extd_server_step (self, cancellable, error);
 
-	if (self->literal == ic)
-		self->literal = NULL;
+	if (is->literal == ic)
+		is->literal = NULL;
 
-	QUEUE_LOCK (self);
-	camel_dlist_remove ((CamelDListNode *) ic);
-	QUEUE_UNLOCK (self);
+	QUEUE_LOCK (is);
+	camel_imapx_command_queue_remove (is->active, ic);
+	QUEUE_UNLOCK (is);
 
-	return (ic->error == NULL);
+	return success;
 }
 
 static gboolean
-extd_server_connect_to_server (CamelIMAPXServer *self,
+extd_server_connect_to_server (CamelIMAPXExtdServer *self,
                                GCancellable *cancellable,
-                               GError **err)
+                               GError **error)
 {
 	/* modified dupe of imapx_connect_to_server() */
 
+	CamelIMAPXServer *is = NULL;
 	CamelNetworkSettings *network_settings = NULL;
 	CamelNetworkSecurityMethod method;
-	CamelStream *tcp_stream = NULL;
+	CamelStream * tcp_stream = NULL;
 	CamelSockOptData sockopt;
 	CamelSettings *settings = NULL;
 	CamelService *service = NULL;
-	guint len = 0;
+	guint len;
 	guchar *token = NULL;
-	gint tok = 0;
-	struct _CamelIMAPXCommand *ic = NULL;
+	gint tok;
+	CamelIMAPXCommand *ic = NULL;
 	gboolean success = TRUE;
 	gchar *host = NULL;
-	GError *tmp_err = NULL;
-
-	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
-	/* cancellable may be NULL */
-	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
+	GError *local_error = NULL;
 
 #ifndef G_OS_WIN32
-	gboolean use_shell_command = FALSE;
+	gboolean use_shell_command;
 	gchar *shell_command = NULL;
 #endif
+	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
+	/* cancellable may be NULL */
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-	service = CAMEL_SERVICE (self->store);
+	is = CAMEL_IMAPX_SERVER (self);
+	service = CAMEL_SERVICE (is->store);
 	settings = camel_service_get_settings (service);
 
 	network_settings = CAMEL_NETWORK_SETTINGS (settings);
@@ -754,18 +769,18 @@ extd_server_connect_to_server (CamelIMAPXServer *self,
 	method = camel_network_settings_get_security_method (network_settings);
 
 #ifndef G_OS_WIN32
-	use_shell_command = \
-		camel_imapx_settings_get_use_shell_command (CAMEL_IMAPX_SETTINGS (settings));
+	use_shell_command = camel_imapx_settings_get_use_shell_command (
+		CAMEL_IMAPX_SETTINGS (settings));
 
 	if (use_shell_command)
-		shell_command = \
-			camel_imapx_settings_dup_shell_command (CAMEL_IMAPX_SETTINGS (settings));
+		shell_command = camel_imapx_settings_dup_shell_command (
+			CAMEL_IMAPX_SETTINGS (settings));
 
 	if (shell_command != NULL) {
 		gboolean success;
 
-		success = camel_imapx_server_connect_to_server_process (self,
-		                                                        shell_command, &tmp_err);
+		success = camel_imapx_server_connect_to_server_process (
+			is, shell_command, &local_error);
 
 		g_free (shell_command);
 
@@ -776,168 +791,157 @@ extd_server_connect_to_server (CamelIMAPXServer *self,
 	}
 #endif
 
-	tcp_stream = camel_network_service_connect_sync ( \
-	                                                 CAMEL_NETWORK_SERVICE (self->store), cancellable, err);
+	tcp_stream = camel_network_service_connect_sync (
+		CAMEL_NETWORK_SERVICE (is->store), cancellable, error);
 
 	if (tcp_stream == NULL) {
 		success = FALSE;
 		goto exit;
 	}
 
-	self->stream = CAMEL_IMAPX_STREAM (camel_imapx_stream_new (tcp_stream));
+	is->stream = (CamelIMAPXStream *) camel_imapx_stream_new (tcp_stream);
 	g_object_unref (tcp_stream);
 
 	/* Disable Nagle - we send a lot of small requests which nagle slows down */
 	sockopt.option = CAMEL_SOCKOPT_NODELAY;
 	sockopt.value.no_delay = TRUE;
-	camel_tcp_stream_setsockopt (CAMEL_TCP_STREAM (tcp_stream), &sockopt);
+	camel_tcp_stream_setsockopt ((CamelTcpStream *) tcp_stream, &sockopt);
 
 	/* Set keepalive - needed for some hosts/router configurations, we're idle a lot */
 	sockopt.option = CAMEL_SOCKOPT_KEEPALIVE;
 	sockopt.value.keep_alive = TRUE;
-	camel_tcp_stream_setsockopt (CAMEL_TCP_STREAM (tcp_stream), &sockopt);
+	camel_tcp_stream_setsockopt ((CamelTcpStream *) tcp_stream, &sockopt);
 
  connected:
-	self->stream->tagprefix = self->tagprefix;
+	is->stream->tagprefix = is->tagprefix;
 	while (1) {
 		// poll ?  wait for other stuff? loop?
-		if (camel_application_is_exiting || self->parser_quit) {
-			g_set_error (err,
-			             G_IO_ERROR,
-			             G_IO_ERROR_CANCELLED,
-			             "Connection to server cancelled\n");
+		if (camel_application_is_exiting || is->parser_quit) {
+			g_set_error (
+				error, G_IO_ERROR,
+				G_IO_ERROR_CANCELLED,
+				"Connection to server cancelled\n");
 			success = FALSE;
 			goto exit;
 		}
 
-		tok = camel_imapx_stream_token (self->stream, &token, &len, cancellable, err);
+		tok = camel_imapx_stream_token (is->stream, &token, &len, cancellable, error);
 		if (tok < 0) {
 			success = FALSE;
 			goto exit;
 		}
 
 		if (tok == '*') {
-			/* modified */
-			extd_server_untagged (self, cancellable, err);
+			extd_server_untagged (self, cancellable, error);
 			break;
 		}
-		camel_imapx_stream_ungettoken (self->stream, tok, token, len);
-		if (camel_imapx_stream_text (self->stream, &token, cancellable, err)) {
+		camel_imapx_stream_ungettoken (is->stream, tok, token, len);
+		if (camel_imapx_stream_text (is->stream, &token, cancellable, error)) {
 			success = FALSE;
 			goto exit;
 		}
-		e(self->tagprefix, "Got unexpected line before greeting:  '%s'\n", token);
+		e(is->tagprefix, "Got unexpected line before greeting:  '%s'\n", token);
 		g_free (token);
 	}
 
-	if (!self->cinfo) {
-		ic = camel_imapx_server_command_new (self,
-		                                     "CAPABILITY", NULL,
-		                                     cancellable, "CAPABILITY");
-		/* modified */
-		extd_server_command_run (self, ic);
-
-		if (ic->error != NULL || ic->status->result != IMAPX_OK) {
-			if (ic->error == NULL)
-				g_set_error (err,
-				             CAMEL_ERROR,
-				             CAMEL_ERROR_GENERIC,
-				             "%s", ic->status->text);
-			else {
-				g_propagate_error (err, ic->error);
-				ic->error = NULL;
-			}
-
-			camel_imapx_server_command_unref (self, ic);
+	if (!is->cinfo) {
+		ic = camel_imapx_command_new (
+			is, "CAPABILITY", NULL, "CAPABILITY");
+		if (!extd_server_command_run (self, ic, cancellable, error)) {
+			camel_imapx_command_unref (ic);
 			success = FALSE;
+			goto exit;
+		}
 
+		/* Server reported error. */
+		if (ic->status->result != IMAPX_OK) {
+			g_set_error (
+				error, CAMEL_ERROR,
+				CAMEL_ERROR_GENERIC,
+				"%s", ic->status->text);
+			camel_imapx_command_unref (ic);
+			success = FALSE;
 			goto exit;
 		}
-		camel_imapx_server_command_unref (self, ic);
+
+		camel_imapx_command_unref (ic);
 	}
 
 	if (method == CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT) {
 
-		if (!(self->cinfo->capa & IMAPX_CAPABILITY_STARTTLS)) {
-			g_set_error (&tmp_err,
-			             CAMEL_ERROR,
-			             CAMEL_ERROR_GENERIC,
-			             _("Failed to connect to IMAP server %s in secure mode: %s"),
-			             host, _("STARTTLS not supported"));
+		if (!(is->cinfo->capa & IMAPX_CAPABILITY_STARTTLS)) {
+			g_set_error (
+				&local_error, CAMEL_ERROR,
+				CAMEL_ERROR_GENERIC,
+				_("Failed to connect to IMAP server %s in secure mode: %s"),
+				host, _("STARTTLS not supported"));
 			goto exit;
 		}
 
-		ic = camel_imapx_server_command_new (self,
-		                                     "STARTTLS", NULL,
-		                                     cancellable, "STARTTLS");
-		/* modified */
-		extd_server_command_run (self, ic);
-
-		if (ic->error != NULL || ic->status->result != IMAPX_OK) {
-			if (ic->error == NULL)
-				g_set_error (&tmp_err,
-				             CAMEL_ERROR,
-				             CAMEL_ERROR_GENERIC,
-				             "%s", ic->status->text);
-			else {
-				g_propagate_error (&tmp_err, ic->error);
-				ic->error = NULL;
-			}
+		ic = camel_imapx_command_new (
+			is, "STARTTLS", NULL, "STARTTLS");
+		if (!extd_server_command_run (self, ic, cancellable, error)) {
+			camel_imapx_command_unref (ic);
+			success = FALSE;
+			goto exit;
+		}
 
-			camel_imapx_server_command_unref (self, ic);
+		/* Server reported error. */
+		if (ic->status->result != IMAPX_OK) {
+			g_set_error (
+				error, CAMEL_ERROR,
+				CAMEL_ERROR_GENERIC,
+				"%s", ic->status->text);
+			camel_imapx_command_unref (ic);
+			success = FALSE;
 			goto exit;
 		}
 
 		/* See if we got new capabilities in the STARTTLS response */
-		imapx_free_capability (self->cinfo);
-		self->cinfo = NULL;
+		imapx_free_capability (is->cinfo);
+		is->cinfo = NULL;
 		if (ic->status->condition == IMAPX_CAPABILITY) {
-			self->cinfo = ic->status->u.cinfo;
+			is->cinfo = ic->status->u.cinfo;
 			ic->status->u.cinfo = NULL;
-			c(self->tagprefix, "got capability flags %08x\n", self->cinfo->capa);
+			c(is->tagprefix, "got capability flags %08x\n", is->cinfo->capa);
 		}
 
-		camel_imapx_server_command_unref (self, ic);
+		camel_imapx_command_unref (ic);
 
-		if (camel_tcp_stream_ssl_enable_ssl (CAMEL_TCP_STREAM_SSL (tcp_stream),
-		                                     cancellable, &tmp_err) == -1) {
-			g_prefix_error (&tmp_err,
-			                _("Failed to connect to IMAP server %s in secure mode: "),
-			                host);
+		if (camel_tcp_stream_ssl_enable_ssl (
+			CAMEL_TCP_STREAM_SSL (tcp_stream),
+			cancellable, &local_error) == -1) {
+			g_prefix_error (
+				&local_error,
+				_("Failed to connect to IMAP server %s in secure mode: "),
+				host);
 			goto exit;
 		}
 		/* Get new capabilities if they weren't already given */
-		if (!self->cinfo) {
-			ic = camel_imapx_server_command_new (self,
-			                                     "CAPABILITY", NULL,
-			                                     cancellable, "CAPABILITY");
-			/* modified */
-			if (!extd_server_command_run (self, ic)) {
-				g_propagate_error (&tmp_err, ic->error);
-				ic->error = NULL;
-				camel_imapx_server_command_unref (self, ic);
+		if (!is->cinfo) {
+			ic = camel_imapx_command_new (
+				is, "CAPABILITY", NULL, "CAPABILITY");
+			if (!extd_server_command_run (self, ic, cancellable, error)) {
+				camel_imapx_command_unref (ic);
+				success = FALSE;
 				goto exit;
 			}
 
-			camel_imapx_server_command_unref (self, ic);
+			camel_imapx_command_unref (ic);
 		}
 	}
 
- exit:
-	if (tmp_err != NULL) {
-		e(self->tagprefix, "Unable to connect %d %s \n",
-		  tmp_err->code, tmp_err->message);
-		g_propagate_error (err, tmp_err);
-		tmp_err = NULL;
-		g_object_unref (self->stream);
-		self->stream = NULL;
-
-		if (self->cinfo) {
-			imapx_free_capability (self->cinfo);
-			self->cinfo = NULL;
+exit:
+	if (!success) {
+		if (is->stream != NULL) {
+			g_object_unref (is->stream);
+			is->stream = NULL;
 		}
 
-		success = FALSE;
+		if (is->cinfo != NULL) {
+			imapx_free_capability (is->cinfo);
+			is->cinfo = NULL;
+		}
 	}
 
 	g_free (host);
@@ -946,12 +950,13 @@ extd_server_connect_to_server (CamelIMAPXServer *self,
 }
 
 static gboolean
-extd_server_reconnect (CamelIMAPXServer *self,
+extd_server_reconnect (CamelIMAPXExtdServer *self,
                        GCancellable *cancellable,
-                       GError **err)
+                       GError **error)
 {
 	/* modified dupe of imapx_reconnect() */
 
+	CamelIMAPXServer *is = NULL;
 	CamelIMAPXCommand *ic = NULL;
 	CamelService *service = NULL;
 	CamelSession *session = NULL;
@@ -962,92 +967,82 @@ extd_server_reconnect (CamelIMAPXServer *self,
 
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	/* cancellable may be NULL */
-	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-	service = CAMEL_SERVICE (self->store);
+	is = CAMEL_IMAPX_SERVER (self);
+	service = CAMEL_SERVICE (is->store);
 	session = camel_service_get_session (service);
 	settings = camel_service_get_settings (service);
 
 	mechanism = camel_network_settings_dup_auth_mechanism (
-	                                                       CAMEL_NETWORK_SETTINGS (settings));
+		CAMEL_NETWORK_SETTINGS (settings));
 
 	use_idle = camel_imapx_settings_get_use_idle (
-	                                              CAMEL_IMAPX_SETTINGS (settings));
+		CAMEL_IMAPX_SETTINGS (settings));
 
 	use_qresync = camel_imapx_settings_get_use_qresync (
-	                                                    CAMEL_IMAPX_SETTINGS (settings));
+		CAMEL_IMAPX_SETTINGS (settings));
 
-	/* modified */
-	if (!extd_server_connect_to_server (self, cancellable, err))
+	if (!extd_server_connect_to_server (self, cancellable, error))
 		goto exception;
 
-	if (self->state == IMAPX_AUTHENTICATED)
+	if (is->state == IMAPX_AUTHENTICATED)
 		goto preauthed;
 
-	if (!camel_session_authenticate_sync (session, service, mechanism, cancellable, err))
+	if (!camel_session_authenticate_sync (
+		session, service, mechanism, cancellable, error))
 		goto exception;
 
 	/* After login we re-capa unless the server already told us */
-	if (!self->cinfo) {
-		ic = camel_imapx_server_command_new (self,
-		                                     "CAPABILITY", NULL,
-		                                     cancellable, "CAPABILITY");
-		/* modified */
-		if (!extd_server_command_run (self, ic)) {
-			g_propagate_error (err, ic->error);
-			ic->error = NULL;
-			camel_imapx_server_command_unref (self, ic);
+	if (!is->cinfo) {
+		ic = camel_imapx_command_new (
+			is, "CAPABILITY", NULL, "CAPABILITY");
+		if (!extd_server_command_run (self, ic, cancellable, error)) {
+			camel_imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_server_command_unref (self, ic);
+		camel_imapx_command_unref (ic);
 	}
 
-	self->state = IMAPX_AUTHENTICATED;
+	is->state = IMAPX_AUTHENTICATED;
 
  preauthed:
-	self->use_idle = use_idle;
+	is->use_idle = use_idle;
 
-	if (camel_imapx_server_idle_supported (self))
-		camel_imapx_server_init_idle (self);
+	if (camel_imapx_server_idle_supported (is))
+		camel_imapx_server_init_idle (is);
 
 	/* Fetch namespaces */
-	if (self->cinfo->capa & IMAPX_CAPABILITY_NAMESPACE) {
-		ic = camel_imapx_server_command_new (self,
-		                                     "NAMESPACE", NULL,
-		                                     cancellable, "NAMESPACE");
-		/* modified */
-		if (!extd_server_command_run (self, ic)) {
-			g_propagate_error (err, ic->error);
-			ic->error = NULL;
-			camel_imapx_server_command_unref (self, ic);
+	if (is->cinfo->capa & IMAPX_CAPABILITY_NAMESPACE) {
+		ic = camel_imapx_command_new (
+			is, "NAMESPACE", NULL, "NAMESPACE");
+		if (!extd_server_command_run (self, ic, cancellable, error)) {
+			camel_imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_server_command_unref (self, ic);
+		camel_imapx_command_unref (ic);
 	}
 
-	if (use_qresync && self->cinfo->capa & IMAPX_CAPABILITY_QRESYNC) {
-		ic = camel_imapx_server_command_new (self,
-		                                     "ENABLE", NULL, cancellable,
-		                                     "ENABLE CONDSTORE QRESYNC");
-		if (!extd_server_command_run (self, ic)) {
-			g_propagate_error (err, ic->error);
-			ic->error = NULL;
-			camel_imapx_server_command_unref (self, ic);
+	if (use_qresync && is->cinfo->capa & IMAPX_CAPABILITY_QRESYNC) {
+		ic = camel_imapx_command_new (
+			is, "ENABLE", NULL, "ENABLE CONDSTORE QRESYNC");
+		if (!extd_server_command_run (self, ic, cancellable, error)) {
+			camel_imapx_command_unref (ic);
 			goto exception;
 		}
 
-		camel_imapx_server_command_unref (self, ic);
+		camel_imapx_command_unref (ic);
 
-		self->use_qresync = TRUE;
+		is->use_qresync = TRUE;
 	} else
-		self->use_qresync = FALSE;
+		is->use_qresync = FALSE;
 
-	if (CAMEL_IMAPX_STORE (self->store)->summary->namespaces == NULL) {
+	if (((CamelIMAPXStore *) is->store)->summary->namespaces == NULL) {
 		CamelIMAPXNamespaceList *nsl = NULL;
 		CamelIMAPXStoreNamespace *ns = NULL;
-		CamelIMAPXStore *imapx_store = CAMEL_IMAPX_STORE (self->store);
+		CamelIMAPXStore *imapx_store = (CamelIMAPXStore *) is->store;
 
 		/* set a default namespace */
 		nsl = g_malloc0 (sizeof (CamelIMAPXNamespaceList));
@@ -1062,19 +1057,19 @@ extd_server_reconnect (CamelIMAPXServer *self,
 		imapx_store->dir_sep = ns->sep;
 	}
 
-	self->state = IMAPX_INITIALISED;
+	is->state = IMAPX_INITIALISED;
 
 	g_free (mechanism);
 
 	return TRUE;
 
- exception:
+exception:
 
-	camel_imapx_server_disconnect (self);
+	camel_imapx_server_disconnect (is);
 
-	if (self->cinfo) {
-		imapx_free_capability (self->cinfo);
-		self->cinfo = NULL;
+	if (is->cinfo) {
+		imapx_free_capability (is->cinfo);
+		is->cinfo = NULL;
 	}
 
 	g_free (mechanism);
@@ -1083,29 +1078,23 @@ extd_server_reconnect (CamelIMAPXServer *self,
 }
 
 static void
-extd_server_parse_contents (CamelIMAPXServer *self,
+extd_server_parse_contents (CamelIMAPXExtdServer *self,
                             GCancellable *cancellable,
-                            GError **err)
+                            GError **error)
 {
 	/* modified dupe of parse_contents() */
 
-	gint buffered = 0;
-	GError *tmp_err = NULL;
+	CamelIMAPXServer *is = NULL;
 
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	/* cancellable may be NULL */
-	g_return_if_fail (err == NULL || *err == NULL);
-
-	do {
-		/* modified */
-		extd_server_step (self, cancellable, &tmp_err);
-
-		buffered = camel_imapx_stream_buffered (self->stream);
+	g_return_if_fail (error == NULL || *error == NULL);
 
-	} while (buffered && tmp_err == NULL);
+	is = CAMEL_IMAPX_SERVER (self);
 
-	if (tmp_err != NULL)
-		g_propagate_error (err, tmp_err);
+	while (extd_server_step (self, cancellable, error))
+		if (camel_imapx_stream_buffered (is->stream) == 0)
+			break;
 }
 
 /*
@@ -1120,26 +1109,25 @@ extd_server_parser_thread (gpointer d)
 {
 	/* modified dupe of imapx_parser_thread() */
 
-	CamelIMAPXServer *self = CAMEL_IMAPX_SERVER (d);
+	CamelIMAPXExtdServer *self = CAMEL_IMAPX_EXTD_SERVER (d);
+	CamelIMAPXServer *is = CAMEL_IMAPX_SERVER (self);
 	GCancellable *cancellable;
-	GError *tmp_err = NULL;
-
-	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
+	GError *local_error = NULL;
 
-	QUEUE_LOCK (self);
+	QUEUE_LOCK (is);
 	cancellable = camel_operation_new ();
-	self->cancellable = g_object_ref (cancellable);
-	QUEUE_UNLOCK (self);
+	is->cancellable = g_object_ref (cancellable);
+	QUEUE_UNLOCK (is);
 
-	while (tmp_err == NULL && self->stream) {
+	while (local_error == NULL && is->stream) {
 		g_cancellable_reset (cancellable);
 
 #ifndef G_OS_WIN32
-		if (self->is_process_stream)	{
+		if (is->is_process_stream)	{
 			GPollFD fds[2] = { {0, 0, 0}, {0, 0, 0} };
 			gint res;
 
-			fds[0].fd = ((CamelStreamProcess *) self->stream->source)->sockfd;
+			fds[0].fd = ((CamelStreamProcess *) is->stream->source)->sockfd;
 			fds[0].events = G_IO_IN;
 			fds[1].fd = g_cancellable_get_fd (cancellable);
 			fds[1].events = G_IO_IN;
@@ -1149,60 +1137,57 @@ extd_server_parser_thread (gpointer d)
 			else if (res == 0)
 				/* timed out */;
 			else if (fds[0].revents & G_IO_IN)
-				/* modified */
-				extd_server_parse_contents (self, cancellable, &tmp_err);
+				extd_server_parse_contents (self, cancellable, &local_error);
 			g_cancellable_release_fd (cancellable);
 		} else
 #endif
-			{
-				/* modified */
-				extd_server_parse_contents (self, cancellable, &tmp_err);
-			}
+		{
+			extd_server_parse_contents (self, cancellable, &local_error);
+		}
 
-		if (self->parser_quit)
+		if (is->parser_quit)
 			g_cancellable_cancel (cancellable);
 
 		if (g_cancellable_is_cancelled (cancellable)) {
 			gint is_empty;
 
-			QUEUE_LOCK (self);
-			is_empty = camel_dlist_empty (&self->active);
-			QUEUE_UNLOCK (self);
+			QUEUE_LOCK (is);
+			is_empty = camel_imapx_command_queue_is_empty (is->active);
+			QUEUE_UNLOCK (is);
 
-			if (is_empty ||
-			    (camel_imapx_server_idle_supported (self) && camel_imapx_server_in_idle (self))) {
+			if (is_empty || (camel_imapx_server_idle_supported (is) && camel_imapx_server_in_idle (is))) {
 				g_cancellable_reset (cancellable);
-				g_clear_error (&tmp_err);
+				g_clear_error (&local_error);
 			} else {
 				/* Cancelled error should be set. */
-				g_warn_if_fail (tmp_err != NULL);
+				g_warn_if_fail (local_error != NULL);
 			}
 		}
 
 		/* Jump out of the loop if an error occurred. */
-		if (tmp_err != NULL)
+		if (local_error != NULL)
 			break;
 	}
 
-	QUEUE_LOCK (self);
-	self->state = IMAPX_SHUTDOWN;
-	QUEUE_UNLOCK (self);
+	QUEUE_LOCK (is);
+	is->state = IMAPX_SHUTDOWN;
+	QUEUE_UNLOCK (is);
 
-	camel_imapx_server_cancel_all_jobs (self, tmp_err);
+	camel_imapx_server_cancel_all_jobs (is, local_error);
 
-	g_clear_error (&tmp_err);
+	g_clear_error (&local_error);
 
-	QUEUE_LOCK (self);
-	if (self->cancellable != NULL) {
-		g_object_unref (self->cancellable);
-		self->cancellable = NULL;
+	QUEUE_LOCK (is);
+	if (is->cancellable != NULL) {
+		g_object_unref (is->cancellable);
+		is->cancellable = NULL;
 	}
 	g_object_unref (cancellable);
-	QUEUE_UNLOCK (self);
+	QUEUE_UNLOCK (is);
 
-	self->parser_quit = FALSE;
+	is->parser_quit = FALSE;
 
-	g_signal_emit (self, signals[SHUTDOWN], 0);
+	g_signal_emit (is, signals[SHUTDOWN], 0);
 
 	return NULL;
 }
@@ -1217,17 +1202,14 @@ imapx_extd_server_connect (CamelIMAPXServer *self,
 {
 	/* modified dupe of camel_imapx_server_connect() */
 
-	gboolean success = FALSE;
+	gboolean success;
 
 	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
 	/* cancellable may be NULL */
 	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
 
 	if (self->state == IMAPX_SHUTDOWN) {
-		g_set_error (err,
-		             CAMEL_SERVICE_ERROR,
-		             CAMEL_SERVICE_ERROR_UNAVAILABLE,
-		             "Shutting down");
+		g_set_error (err, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE, "Shutting down");
 		return FALSE;
 	}
 
@@ -1235,20 +1217,13 @@ imapx_extd_server_connect (CamelIMAPXServer *self,
 		return TRUE;
 
 	g_static_rec_mutex_lock (&self->ostream_lock);
-	/* modified */
-	success = extd_server_reconnect (self, cancellable, err);
+	success = extd_server_reconnect (CAMEL_IMAPX_EXTD_SERVER (self), cancellable, err);
 	g_static_rec_mutex_unlock (&self->ostream_lock);
 
 	if (!success)
 		return FALSE;
 
-	/* modified */
-	self->parser_thread = \
-		g_thread_create ((GThreadFunc) \
-		                 extd_server_parser_thread,
-		                 self,
-		                 TRUE,
-		                 NULL);
+	self->parser_thread = g_thread_create ((GThreadFunc) extd_server_parser_thread, self, TRUE, NULL);
 	return TRUE;
 }
 
@@ -1261,7 +1236,7 @@ imapx_extd_server_connect_to_server (CamelIMAPXServer *self,
 	/* cancellable may be NULL */
 	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
 
-	return extd_server_connect_to_server (self,
+	return extd_server_connect_to_server (CAMEL_IMAPX_EXTD_SERVER (self),
 	                                      cancellable,
 	                                      err);
 }
@@ -1270,10 +1245,11 @@ static CamelAuthenticationResult
 imapx_extd_server_authenticate (CamelIMAPXServer *self,
                                 const gchar *mechanism,
                                 GCancellable *cancellable,
-                                GError **err)
+                                GError **error)
 {
 	/* modified dupe of camel_imapx_server_authenticate() */
 
+	CamelIMAPXServer *is = NULL;
 	CamelNetworkSettings *network_settings = NULL;
 	CamelSettings *settings = NULL;
 	CamelAuthenticationResult result = CAMEL_AUTHENTICATION_REJECTED;
@@ -1283,12 +1259,15 @@ imapx_extd_server_authenticate (CamelIMAPXServer *self,
 	gchar *host = NULL;
 	gchar *user = NULL;
 
-	g_assert (CAMEL_IS_IMAPX_EXTD_SERVER (self));
-	g_assert (mechanism != NULL);
+	g_return_val_if_fail (
+		CAMEL_IS_IMAPX_EXTD_SERVER (self),
+		CAMEL_AUTHENTICATION_REJECTED);
+	/* mechanism may be NULL */
 	/* cancellable may be NULL */
-	g_return_val_if_fail (err == NULL || *err == NULL, CAMEL_AUTHENTICATION_REJECTED);
+	g_return_val_if_fail (error == NULL || *error == NULL, CAMEL_AUTHENTICATION_REJECTED);
 
-	service = CAMEL_SERVICE (self->store);
+	is = self;
+	service = CAMEL_SERVICE (is->store);
 	settings = camel_service_get_settings (service);
 
 	network_settings = CAMEL_NETWORK_SETTINGS (settings);
@@ -1296,94 +1275,85 @@ imapx_extd_server_authenticate (CamelIMAPXServer *self,
 	user = camel_network_settings_dup_user (network_settings);
 
 	if (mechanism != NULL) {
-		if (!g_hash_table_lookup (self->cinfo->auth_types, mechanism)) {
-			g_set_error (err,
-			             CAMEL_SERVICE_ERROR,
-			             CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
-			             _("IMAP server %s does not support %s "
-			               "authentication"), host, mechanism);
+		if (!g_hash_table_lookup (is->cinfo->auth_types, mechanism)) {
+			g_set_error (
+				error, CAMEL_SERVICE_ERROR,
+				CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
+				_("IMAP server %s does not support %s "
+				  "authentication"), host, mechanism);
 			result = CAMEL_AUTHENTICATION_ERROR;
 			goto exit;
 		}
 
 		sasl = camel_sasl_new ("imap", mechanism, service);
 		if (sasl != NULL) {
-			g_set_error (err,
-			             CAMEL_SERVICE_ERROR,
-			             CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
-			             _("No support for %s authentication"),
-			             mechanism);
+			g_set_error (
+				error, CAMEL_SERVICE_ERROR,
+				CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
+				_("No support for %s authentication"),
+				mechanism);
 			result = CAMEL_AUTHENTICATION_ERROR;
 			goto exit;
 		}
 	}
 
 	if (sasl != NULL) {
-		ic = camel_imapx_server_command_new (self,
-		                                     "AUTHENTICATE", NULL, cancellable,
-		                                     "AUTHENTICATE %A", sasl);
+		ic = camel_imapx_command_new (
+			is, "AUTHENTICATE", NULL, "AUTHENTICATE %A", sasl);
 	} else {
-		const gchar *password = NULL;
+		const gchar *password;
 
 		password = camel_service_get_password (service);
 
 		if (user == NULL) {
-			g_set_error_literal (err,
-			                     CAMEL_SERVICE_ERROR,
-			                     CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
-			                     _("Cannot authenticate without a username"));
+			g_set_error_literal (
+				error, CAMEL_SERVICE_ERROR,
+				CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
+				_("Cannot authenticate without a username"));
 			result = CAMEL_AUTHENTICATION_ERROR;
 			goto exit;
 		}
 
 		if (password == NULL) {
-			g_set_error_literal (err,
-			                     CAMEL_SERVICE_ERROR,
-			                     CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
-			                     _("Authentication password not available"));
+			g_set_error_literal (
+				error, CAMEL_SERVICE_ERROR,
+				CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
+				_("Authentication password not available"));
 			result = CAMEL_AUTHENTICATION_ERROR;
 			goto exit;
 		}
 
-		ic = camel_imapx_server_command_new (self,
-		                                     "LOGIN", NULL, cancellable,
-		                                     "LOGIN %s %s", user, password);
+		ic = camel_imapx_command_new (
+			is, "LOGIN", NULL, "LOGIN %s %s", user, password);
 	}
 
-	/* modified */
-	extd_server_command_run (self, ic);
-
-	if (ic->error == NULL) {
-		if (ic->status->result == IMAPX_OK)
-			result = CAMEL_AUTHENTICATION_ACCEPTED;
-		else
-			result = CAMEL_AUTHENTICATION_REJECTED;
-	} else {
-		g_propagate_error (err, ic->error);
-		ic->error = NULL;
+	if (!extd_server_command_run (CAMEL_IMAPX_EXTD_SERVER (self), ic, cancellable, error))
 		result = CAMEL_AUTHENTICATION_ERROR;
-	}
+	else if (ic->status->result == IMAPX_OK)
+		result = CAMEL_AUTHENTICATION_ACCEPTED;
+	else
+		result = CAMEL_AUTHENTICATION_REJECTED;
 
 	/* Forget old capabilities after login. */
 	if (result == CAMEL_AUTHENTICATION_ACCEPTED) {
-		if (self->cinfo) {
-			imapx_free_capability (self->cinfo);
-			self->cinfo = NULL;
+		if (is->cinfo) {
+			imapx_free_capability (is->cinfo);
+			is->cinfo = NULL;
 		}
 
 		if (ic->status->condition == IMAPX_CAPABILITY) {
-			self->cinfo = ic->status->u.cinfo;
+			is->cinfo = ic->status->u.cinfo;
 			ic->status->u.cinfo = NULL;
-			c(self->tagprefix, "got capability flags %08x\n", self->cinfo->capa);
+			c(is->tagprefix, "got capability flags %08x\n", is->cinfo->capa);
 		}
 	}
 
-	camel_imapx_server_command_unref (self, ic);
+	camel_imapx_command_unref (ic);
 
 	if (sasl != NULL)
 		g_object_unref (sasl);
 
- exit:
+exit:
 	g_free (host);
 	g_free (user);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]