managesieve: handle lines starting with "OK"
authorCharles Lehner <cel@celehner.com>
Thu, 16 Jul 2015 21:58:47 +0000 (17:58 -0400)
committerCharles Lehner <cel@celehner.com>
Thu, 16 Jul 2015 22:22:05 +0000 (18:22 -0400)
Count octets instead of waiting for OK

src/plugins/managesieve/managesieve.c
src/plugins/managesieve/managesieve.h

index 4c67904..a6d7bbb 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <glib.h>
 #include <glib/gi18n.h>
+#include <ctype.h>
 
 #include "claws.h"
 #include "account.h"
@@ -439,22 +440,22 @@ static void unquote_inplace(gchar *str)
 
 static void parse_response(gchar *msg, SieveResult *result)
 {
+       gchar *end;
+
        /* response status */
-       gchar *end = strchr(msg, ' ');
-       if (end)
-               *end++ = '\0';
-       result->success = strcmp(msg, "OK") == 0;
-       result->has_status = TRUE;
-       if (!end) {
-               result->code = SIEVE_CODE_NONE;
-               result->description = NULL;
-               result->has_octets = FALSE;
-               result->octets = 0;
-               return;
+       if (isalpha(msg[0])) {
+               end = strchr(msg, ' ');
+               if (end) {
+                       *end++ = '\0';
+                       while (*end == ' ')
+                               end++;
+               }
+               result->success = strcmp(msg, "OK") == 0;
+               result->has_status = TRUE;
+               msg = end;
+       } else {
+               result->has_status = FALSE;
        }
-       while (*end == ' ')
-               end++;
-       msg = end;
 
        /* response code */
        if (msg[0] == '(' && (end = strchr(msg, ')'))) {
@@ -673,17 +674,23 @@ static gint sieve_session_recv_msg(Session *session, const gchar *msg)
                } else {
                        parse_response((gchar *)msg, &result);
                        sieve_session->state = SIEVE_GETSCRIPT_DATA;
+                       /* account for newline */
+                       sieve_session->octets_remaining = result.octets + 1;
                }
                ret = SE_OK;
                break;
        case SIEVE_GETSCRIPT_DATA:
-               if (response_is_ok(msg)) {
+               if (sieve_session->octets_remaining > 0) {
+                       sieve_session->current_cmd->cb(sieve_session,
+                                       (gchar *)msg,
+                                       sieve_session->current_cmd->data);
+                       sieve_session->octets_remaining -= strlen(msg) + 1;
+               } else if (response_is_ok(msg)) {
                        sieve_session->state = SIEVE_READY;
                        sieve_session->current_cmd->cb(sieve_session, NULL,
                                        sieve_session->current_cmd->data);
                } else {
-                       sieve_session->current_cmd->cb(sieve_session, (gchar *)msg,
-                                       sieve_session->current_cmd->data);
+                       log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
                }
                ret = SE_OK;
                break;
index b775f90..c150d4a 100644 (file)
@@ -114,6 +114,7 @@ struct SieveSession
        GSList *send_queue;
        SieveErrorValue error;
        SieveCommand *current_cmd;
+       guint octets_remaining;
 
        gboolean use_auth;
        SieveAuthType avail_auth_type;