commit b817c3afd10f73ebfff251b1dd1bbc455aae9031
Author: Oswald Buddenhagen <o...@kde.org>
Date:   Sun Mar 13 12:06:49 2011 +0100

    move greeting response handling into get_cmd_result()
    
    the primary purpose of this is getting rid of the "free-standing"
    buffer_gets() call.

 src/drv_imap.c |   54 +++++++++++++++++++++++++++--------------------
 1 files changed, 31 insertions(+), 23 deletions(-)

diff --git a/src/drv_imap.c b/src/drv_imap.c
index f1fea71..a79aef0 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -131,7 +131,7 @@ typedef struct imap_store {
 #endif
 
        /* Used during sequential operations like connect */
-       int preauth;
+       enum { GreetingPending = 0, GreetingBad, GreetingOk, GreetingPreauth } 
greeting;
        union {
                void (*imap_open)( store_t *srv, void *aux );
        } callbacks;
@@ -1093,6 +1093,7 @@ struct imap_cmd_trycreate {
        struct imap_cmd *orig_cmd;
 };
 
+static void imap_open_store_greeted( imap_store_t * );
 static void get_cmd_result_p2( imap_store_t *, struct imap_cmd *, int );
 
 static void
@@ -1100,8 +1101,9 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
 {
        struct imap_cmd *cmdp, **pcmdp;
        char *cmd, *arg, *arg1, *p;
-       int n, resp, resp2, tag;
+       int n, resp, resp2, tag, greeted;
 
+       greeted = ctx->greeting;
        while (!buffer_gets( &ctx->buf, &cmd )) {
                arg = next_arg( &cmd );
                if (*arg == '*') {
@@ -1115,8 +1117,14 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd 
)
                                ctx->ns_personal = parse_list( &cmd );
                                ctx->ns_other = parse_list( &cmd );
                                ctx->ns_shared = parse_list( &cmd );
-                       } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg 
) ||
-                                  !strcmp( "NO", arg ) || !strcmp( "BYE", arg 
)) {
+                       } else if (ctx->greeting == GreetingPending && !strcmp( 
"PREAUTH", arg )) {
+                               ctx->greeting = GreetingPreauth;
+                               parse_response_code( ctx, 0, cmd );
+                       } else if (!strcmp( "OK", arg )) {
+                               ctx->greeting = GreetingOk;
+                               parse_response_code( ctx, 0, cmd );
+                       } else if (!strcmp( "BAD", arg ) || !strcmp( "NO", arg 
) || !strcmp( "BYE", arg )) {
+                               ctx->greeting = GreetingBad;
                                parse_response_code( ctx, 0, cmd );
                        } else if (!strcmp( "CAPABILITY", arg ))
                                parse_capability( ctx, cmd );
@@ -1137,6 +1145,11 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd 
)
                                error( "IMAP error: unrecognized untagged 
response '%s'\n", arg );
                                break; /* this may mean anything, so prefer not 
to spam the log */
                        }
+                       if (greeted == GreetingPending) {
+                               ctx->ref_count++;
+                               imap_open_store_greeted( ctx );
+                               goto finalize;
+                       }
                } else if (!ctx->in_progress) {
                        error( "IMAP error: unexpected reply: %s %s\n", arg, 
cmd ? cmd : "" );
                        break; /* this may mean anything, so prefer not to spam 
the log */
@@ -1219,6 +1232,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
                        free( cmdp->param.data );
                        free( cmdp->cmd );
                        free( cmdp );
+                 finalize:
                        if (ctx->store_canceled)
                                tcmd = 0;
                        deref_store( ctx );
@@ -1456,7 +1470,6 @@ imap_open_store( store_conf_t *conf,
        imap_server_conf_t *srvc = cfg->server;
        imap_store_t *ctx;
        store_t **ctxp;
-       char *arg, *rsp;
        struct hostent *he;
        struct sockaddr_in addr;
        int s, a[2];
@@ -1553,30 +1566,25 @@ imap_open_store( store_conf_t *conf,
                }
        }
 #endif
+       return;
 
-       /* read the greeting string */
-       if (buffer_gets( &ctx->buf, &rsp ))
-               goto bail;
-       arg = next_arg( &rsp );
-       if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
-               error( "IMAP error: invalid greeting response\n" );
-               goto bail;
-       }
-       if (!strcmp( "PREAUTH", arg ))
-               ctx->preauth = 1;
-       else if (strcmp( "OK", arg ) != 0) {
+  bail:
+       imap_open_store_bail( ctx );
+}
+
+static void
+imap_open_store_greeted( imap_store_t *ctx )
+{
+       if (ctx->greeting == GreetingBad) {
                error( "IMAP error: unknown greeting response\n" );
-               goto bail;
+               imap_open_store_bail( ctx );
+               return;
        }
-       parse_response_code( ctx, 0, rsp );
+
        if (!ctx->caps)
                imap_exec( ctx, 0, imap_open_store_p2, "CAPABILITY" );
        else
                imap_open_store_authenticate( ctx );
-       return;
-
-  bail:
-       imap_open_store_bail( ctx );
 }
 
 static void
@@ -1591,7 +1599,7 @@ imap_open_store_p2( imap_store_t *ctx, struct imap_cmd 
*cmd ATTR_UNUSED, int res
 static void
 imap_open_store_authenticate( imap_store_t *ctx )
 {
-       if (!ctx->preauth) {
+       if (ctx->greeting != GreetingPreauth) {
 #if HAVE_LIBSSL
                imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf;
                imap_server_conf_t *srvc = cfg->server;

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to