[geary/mjog/dovecot-envelope-mailbox-quirk: 5/6] Geary.Imap.ServerResponse: Add quirks property




commit a209051232ccbdcea2887e249ff91c7df32d6112
Author: Michael Gratton <mike vee net>
Date:   Mon Aug 17 13:05:31 2020 +1000

    Geary.Imap.ServerResponse: Add quirks property
    
    Add a quirks object as a property so code that parses the response has
    access to it. Simplify constructing server responses slightly and pass
    the IMAP connection's quirks when doing so.

 .../imap/response/imap-continuation-response.vala  | 14 ++++----
 src/engine/imap/response/imap-server-data.vala     |  9 ++---
 src/engine/imap/response/imap-server-response.vala | 41 +++++++---------------
 src/engine/imap/response/imap-status-response.vala | 14 ++++----
 .../imap/transport/imap-client-connection.vala     | 24 ++++++-------
 .../response/imap-namespace-response-test.vala     |  2 +-
 6 files changed, 45 insertions(+), 59 deletions(-)
---
diff --git a/src/engine/imap/response/imap-continuation-response.vala 
b/src/engine/imap/response/imap-continuation-response.vala
index 8fc0a7cc7..d15fe45fe 100644
--- a/src/engine/imap/response/imap-continuation-response.vala
+++ b/src/engine/imap/response/imap-continuation-response.vala
@@ -15,8 +15,10 @@
  */
 
 public class Geary.Imap.ContinuationResponse : ServerResponse {
-    private ContinuationResponse() {
-        base (Tag.get_continuation());
+
+
+    private ContinuationResponse(Quirks quirks) {
+        base(Tag.get_continuation(), quirks);
     }
 
     /**
@@ -25,9 +27,9 @@ public class Geary.Imap.ContinuationResponse : ServerResponse {
      * The supplied root is "stripped" of its children.  This may happen even if an exception is
      * thrown.  It's recommended to use {@link is_continuation_response} prior to this call.
      */
-    public ContinuationResponse.migrate(RootParameters root) throws ImapError {
-        base.migrate(root);
-
+    public ContinuationResponse.migrate(RootParameters root, Quirks quirks)
+        throws ImapError {
+        base.migrate(root, quirks);
         if (!tag.is_continuation())
             throw new ImapError.INVALID("Tag %s is not a continuation", tag.to_string());
     }
@@ -37,8 +39,6 @@ public class Geary.Imap.ContinuationResponse : ServerResponse {
      */
     public static bool is_continuation_response(RootParameters root) {
         Tag? tag = root.get_tag();
-
         return tag != null ? tag.is_continuation() : false;
     }
 }
-
diff --git a/src/engine/imap/response/imap-server-data.vala b/src/engine/imap/response/imap-server-data.vala
index 29aee4b57..f9e0edc70 100644
--- a/src/engine/imap/response/imap-server-data.vala
+++ b/src/engine/imap/response/imap-server-data.vala
@@ -13,8 +13,8 @@
 public class Geary.Imap.ServerData : ServerResponse {
     public ServerDataType server_data_type { get; private set; }
 
-    private ServerData(Tag tag, ServerDataType server_data_type) {
-        base (tag);
+    private ServerData(Tag tag, ServerDataType server_data_type, Quirks quirks) {
+        base(tag, quirks);
 
         this.server_data_type = server_data_type;
     }
@@ -25,8 +25,9 @@ public class Geary.Imap.ServerData : ServerResponse {
      * The supplied root is "stripped" of its children.  This may happen even if an exception is
      * thrown.  It's recommended to use {@link is_server_data} prior to this call.
      */
-    public ServerData.migrate(RootParameters root) throws ImapError {
-        base.migrate(root);
+    public ServerData.migrate(RootParameters root, Quirks quirks)
+        throws ImapError {
+        base.migrate(root, quirks);
 
         server_data_type = ServerDataType.from_response(this);
     }
diff --git a/src/engine/imap/response/imap-server-response.vala 
b/src/engine/imap/response/imap-server-response.vala
index 7d2ace515..5f33568f7 100644
--- a/src/engine/imap/response/imap-server-response.vala
+++ b/src/engine/imap/response/imap-server-response.vala
@@ -14,10 +14,15 @@
  */
 
 public abstract class Geary.Imap.ServerResponse : RootParameters {
+
+
     public Tag tag { get; private set; }
+    public Quirks quirks { get; private set; }
 
-    protected ServerResponse(Tag tag) {
+
+    protected ServerResponse(Tag tag, Quirks quirks) {
         this.tag = tag;
+        this.quirks = quirks;
     }
 
     /**
@@ -25,36 +30,16 @@ public abstract class Geary.Imap.ServerResponse : RootParameters {
      *
      * The supplied root is "stripped" of its children.
      */
-    protected ServerResponse.migrate(RootParameters root) throws ImapError {
+    protected ServerResponse.migrate(RootParameters root,
+                                     Quirks quirks)
+        throws ImapError {
         base.migrate(root);
+        this.quirks = quirks;
 
-        if (!has_tag())
+        if (!has_tag()) {
             throw new ImapError.INVALID("Server response does not have a tag token: %s", to_string());
-
-        tag = get_tag();
+        }
+        this.tag = get_tag();
     }
 
-    /**
-     * Migrate the contents of RootParameters into a new, properly-typed ServerResponse.
-     *
-     * The returned ServerResponse may be a {@link ContinuationResponse}, {@link ServerData},
-     * or a generic {@link StatusResponse}.
-     *
-     * The RootParameters will be migrated and stripped clean upon exit.
-     *
-     * @throws ImapError.PARSE_ERROR if not a known form of ServerResponse.
-     */
-    public static ServerResponse migrate_from_server(RootParameters root) throws ImapError {
-        if (ContinuationResponse.is_continuation_response(root))
-            return new ContinuationResponse.migrate(root);
-
-        if (StatusResponse.is_status_response(root))
-            return new StatusResponse.migrate(root);
-
-        if (ServerData.is_server_data(root))
-            return new ServerData.migrate(root);
-
-        throw new ImapError.PARSE_ERROR("Unknown server response: %s", root.to_string());
-    }
 }
-
diff --git a/src/engine/imap/response/imap-status-response.vala 
b/src/engine/imap/response/imap-status-response.vala
index 2b5c7336d..25e2a5458 100644
--- a/src/engine/imap/response/imap-status-response.vala
+++ b/src/engine/imap/response/imap-status-response.vala
@@ -11,8 +11,6 @@
  * StatusResponses may be tagged or untagged, depending on their nature.
  *
  * See [[http://tools.ietf.org/html/rfc3501#section-7.1]] for more information.
- *
- * @see ServerResponse.migrate_from_server
  */
 
 public class Geary.Imap.StatusResponse : ServerResponse {
@@ -34,8 +32,11 @@ public class Geary.Imap.StatusResponse : ServerResponse {
      */
     public ResponseCode? response_code { get; private set; }
 
-    private StatusResponse(Tag tag, Status status, ResponseCode? response_code) {
-        base (tag);
+    private StatusResponse(Tag tag,
+                           Status status,
+                           ResponseCode? response_code,
+                           Quirks quirks) {
+        base(tag, quirks);
 
         this.status = status;
         this.response_code = response_code;
@@ -48,8 +49,9 @@ public class Geary.Imap.StatusResponse : ServerResponse {
      * The supplied root is "stripped" of its children.  This may happen even if an exception is
      * thrown.  It's recommended to use {@link is_status_response} prior to this call.
      */
-    public StatusResponse.migrate(RootParameters root) throws ImapError {
-        base.migrate(root);
+    public StatusResponse.migrate(RootParameters root, Quirks quirks)
+        throws ImapError {
+            base.migrate(root, quirks);
 
         status = Status.from_parameter(get_as_string(1));
         response_code = get_if_list(2) as ResponseCode;
diff --git a/src/engine/imap/transport/imap-client-connection.vala 
b/src/engine/imap/transport/imap-client-connection.vala
index 756d7bb7f..927ac3bdc 100644
--- a/src/engine/imap/transport/imap-client-connection.vala
+++ b/src/engine/imap/transport/imap-client-connection.vala
@@ -467,26 +467,24 @@ public class Geary.Imap.ClientConnection : BaseObject, Logging.Source {
 
     private void on_parameters_ready(RootParameters root) {
         try {
-            ServerResponse response = ServerResponse.migrate_from_server(root);
-            GLib.Type type = response.get_type();
-            if (type == typeof(StatusResponse)) {
-                on_status_response((StatusResponse) response);
-            } else if (type == typeof(ServerData)) {
-                on_server_data((ServerData) response);
-            } else if (type == typeof(ContinuationResponse)) {
-                on_continuation_response((ContinuationResponse) response);
+            // Important! The order of these tests matters.
+            if (ContinuationResponse.is_continuation_response(root)) {
+                on_continuation_response(
+                    new ContinuationResponse.migrate(root, this.quirks)
+                );
+            } else if (StatusResponse.is_status_response(root)) {
+                on_status_response(new StatusResponse.migrate(root, this.quirks));
+            } else if (ServerData.is_server_data(root)) {
+                on_server_data(new ServerData.migrate(root, this.quirks));
             } else {
-                warning(
-                    "Unknown ServerResponse of type %s received: %s:",
-                    response.get_type().name(),
-                    response.to_string()
+                throw new ImapError.PARSE_ERROR(
+                    "Unknown server response: %s", root.to_string()
                 );
             }
         } catch (ImapError err) {
             received_bad_response(root, err);
         }
 
-
         if (this.pending_queue.is_empty && this.sent_queue.is_empty) {
             // There's nothing remaining to send, and every sent
             // command has been dealt with, so ready an IDLE command.
diff --git a/test/engine/imap/response/imap-namespace-response-test.vala 
b/test/engine/imap/response/imap-namespace-response-test.vala
index d0ea77615..2d8d18f59 100644
--- a/test/engine/imap/response/imap-namespace-response-test.vala
+++ b/test/engine/imap/response/imap-namespace-response-test.vala
@@ -126,7 +126,7 @@ class Geary.Imap.NamespaceResponseTest : TestCase {
         else
             root.add(shared);
 
-        return new ServerData.migrate(root);
+        return new ServerData.migrate(root, new Quirks());
     }
 
     private ListParameter newNamespace(string prefix, string? delim) {


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