From d1ee94f02cb84a43a95b0411368e1effca6bd877 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Sun, 13 Mar 2011 12:06:49 +0100 Subject: [PATCH] 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 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/drv_imap.c b/src/drv_imap.c index da5b34e..a1a2145 100644 --- a/src/drv_imap.c +++ b/src/drv_imap.c @@ -129,7 +129,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; @@ -1098,6 +1098,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 int @@ -1105,8 +1106,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; for (;;) { if (buffer_gets( &ctx->buf, &cmd )) break; @@ -1123,8 +1125,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 ); @@ -1145,6 +1153,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) { + imap_ref( ctx ); + imap_open_store_greeted( ctx ); + return imap_deref( ctx ) ? RESP_CANCEL : RESP_OK; + } } 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 */ @@ -1482,7 +1495,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]; @@ -1579,30 +1591,26 @@ imap_open_store( store_conf_t *conf, } } #endif + get_cmd_result( ctx, 0 ); + 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 @@ -1617,7 +1625,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) { #ifdef HAVE_LIBSSL imap_store_conf_t *cfg = (imap_store_conf_t *)ctx->gen.conf; imap_server_conf_t *srvc = cfg->server;