merge imap_t into imap_store_t - there is really no point in having them

separated.
This commit is contained in:
Oswald Buddenhagen 2006-03-20 15:01:48 +00:00
parent d7126dca5e
commit 31fc41a32c

View File

@ -107,7 +107,10 @@ typedef struct {
struct imap_cmd; struct imap_cmd;
#define max_in_progress 50 /* make this configurable? */ #define max_in_progress 50 /* make this configurable? */
typedef struct imap { typedef struct imap_store {
store_t gen;
const char *prefix;
unsigned /*currentnc:1,*/ trashnc:1;
int uidnext; /* from SELECT responses */ int uidnext; /* from SELECT responses */
list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */ list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
string_list_t *boxes; /* LIST results */ string_list_t *boxes; /* LIST results */
@ -120,13 +123,6 @@ typedef struct imap {
SSL_CTX *SSLContext; SSL_CTX *SSLContext;
#endif #endif
buffer_t buf; /* this is BIG, so put it last */ buffer_t buf; /* this is BIG, so put it last */
} imap_t;
typedef struct imap_store {
store_t gen;
imap_t *imap;
const char *prefix;
unsigned /*currentnc:1,*/ trashnc:1;
} imap_store_t; } imap_store_t;
struct imap_cmd_cb { struct imap_cmd_cb {
@ -146,7 +142,7 @@ struct imap_cmd {
int tag; int tag;
}; };
#define CAP(cap) (imap->caps & (1 << (cap))) #define CAP(cap) (ctx->caps & (1 << (cap)))
enum CAPABILITY { enum CAPABILITY {
NOLOGIN = 0, NOLOGIN = 0,
@ -238,7 +234,6 @@ verify_cert( SSL *ssl )
static int static int
init_ssl_ctx( imap_store_t *ctx ) init_ssl_ctx( imap_store_t *ctx )
{ {
imap_t *imap = ctx->imap;
imap_store_conf_t *conf = (imap_store_conf_t *)ctx->gen.conf; imap_store_conf_t *conf = (imap_store_conf_t *)ctx->gen.conf;
imap_server_conf_t *srvc = conf->server; imap_server_conf_t *srvc = conf->server;
SSL_METHOD *method; SSL_METHOD *method;
@ -248,12 +243,12 @@ init_ssl_ctx( imap_store_t *ctx )
method = TLSv1_client_method(); method = TLSv1_client_method();
else else
method = SSLv23_client_method(); method = SSLv23_client_method();
imap->SSLContext = SSL_CTX_new( method ); ctx->SSLContext = SSL_CTX_new( method );
if (!srvc->cert_file) { if (!srvc->cert_file) {
error( "Error, CertificateFile not defined\n" ); error( "Error, CertificateFile not defined\n" );
return -1; return -1;
} else if (!SSL_CTX_load_verify_locations( imap->SSLContext, srvc->cert_file, NULL )) { } else if (!SSL_CTX_load_verify_locations( ctx->SSLContext, srvc->cert_file, NULL )) {
error( "Error while loading certificate file '%s': %s\n", error( "Error while loading certificate file '%s': %s\n",
srvc->cert_file, ERR_error_string( ERR_get_error(), 0 ) ); srvc->cert_file, ERR_error_string( ERR_get_error(), 0 ) );
return -1; return -1;
@ -266,10 +261,10 @@ init_ssl_ctx( imap_store_t *ctx )
if (!srvc->use_tlsv1) if (!srvc->use_tlsv1)
options |= SSL_OP_NO_TLSv1; options |= SSL_OP_NO_TLSv1;
SSL_CTX_set_options( imap->SSLContext, options ); SSL_CTX_set_options( ctx->SSLContext, options );
/* we check the result of the verification after SSL_connect() */ /* we check the result of the verification after SSL_connect() */
SSL_CTX_set_verify( imap->SSLContext, SSL_VERIFY_NONE, 0 ); SSL_CTX_set_verify( ctx->SSLContext, SSL_VERIFY_NONE, 0 );
return 0; return 0;
} }
#endif /* HAVE_LIBSSL */ #endif /* HAVE_LIBSSL */
@ -410,35 +405,34 @@ static struct imap_cmd *
v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb, v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
const char *fmt, va_list ap ) const char *fmt, va_list ap )
{ {
imap_t *imap = ctx->imap;
struct imap_cmd *cmd; struct imap_cmd *cmd;
int n, bufl; int n, bufl;
char buf[1024]; char buf[1024];
cmd = nfmalloc( sizeof(struct imap_cmd) ); cmd = nfmalloc( sizeof(struct imap_cmd) );
nfvasprintf( &cmd->cmd, fmt, ap ); nfvasprintf( &cmd->cmd, fmt, ap );
cmd->tag = ++imap->nexttag; cmd->tag = ++ctx->nexttag;
if (cb) if (cb)
cmd->cb = *cb; cmd->cb = *cb;
else else
memset( &cmd->cb, 0, sizeof(cmd->cb) ); memset( &cmd->cb, 0, sizeof(cmd->cb) );
while (imap->literal_pending) while (ctx->literal_pending)
get_cmd_result( ctx, 0 ); get_cmd_result( ctx, 0 );
bufl = nfsnprintf( buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ? bufl = nfsnprintf( buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
"%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n", "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
cmd->tag, cmd->cmd, cmd->cb.dlen ); cmd->tag, cmd->cmd, cmd->cb.dlen );
if (DFlags & VERBOSE) { if (DFlags & VERBOSE) {
if (imap->num_in_progress) if (ctx->num_in_progress)
printf( "(%d in progress) ", imap->num_in_progress ); printf( "(%d in progress) ", ctx->num_in_progress );
if (memcmp( cmd->cmd, "LOGIN", 5 )) if (memcmp( cmd->cmd, "LOGIN", 5 ))
printf( ">>> %s", buf ); printf( ">>> %s", buf );
else else
printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag ); printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
} }
if (socket_write( &imap->buf.sock, buf, bufl ) != bufl) { if (socket_write( &ctx->buf.sock, buf, bufl ) != bufl) {
free( cmd->cmd ); free( cmd->cmd );
free( cmd ); free( cmd );
if (cb && cb->data) if (cb && cb->data)
@ -447,10 +441,10 @@ v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
} }
if (cmd->cb.data) { if (cmd->cb.data) {
if (CAP(LITERALPLUS)) { if (CAP(LITERALPLUS)) {
n = socket_write( &imap->buf.sock, cmd->cb.data, cmd->cb.dlen ); n = socket_write( &ctx->buf.sock, cmd->cb.data, cmd->cb.dlen );
free( cmd->cb.data ); free( cmd->cb.data );
if (n != cmd->cb.dlen || if (n != cmd->cb.dlen ||
(n = socket_write( &imap->buf.sock, "\r\n", 2 )) != 2) (n = socket_write( &ctx->buf.sock, "\r\n", 2 )) != 2)
{ {
free( cmd->cmd ); free( cmd->cmd );
free( cmd ); free( cmd );
@ -458,13 +452,13 @@ v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
} }
cmd->cb.data = 0; cmd->cb.data = 0;
} else } else
imap->literal_pending = 1; ctx->literal_pending = 1;
} else if (cmd->cb.cont) } else if (cmd->cb.cont)
imap->literal_pending = 1; ctx->literal_pending = 1;
cmd->next = 0; cmd->next = 0;
*imap->in_progress_append = cmd; *ctx->in_progress_append = cmd;
imap->in_progress_append = &cmd->next; ctx->in_progress_append = &cmd->next;
imap->num_in_progress++; ctx->num_in_progress++;
return cmd; return cmd;
} }
@ -483,15 +477,14 @@ issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ...
static struct imap_cmd * static struct imap_cmd *
issue_imap_cmd_w( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... ) issue_imap_cmd_w( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
{ {
imap_t *imap = ctx->imap;
struct imap_cmd *ret; struct imap_cmd *ret;
va_list ap; va_list ap;
va_start( ap, fmt ); va_start( ap, fmt );
ret = v_issue_imap_cmd( ctx, cb, fmt, ap ); ret = v_issue_imap_cmd( ctx, cb, fmt, ap );
va_end( ap ); va_end( ap );
while (imap->num_in_progress > max_in_progress || while (ctx->num_in_progress > max_in_progress ||
socket_pending( &imap->buf.sock )) socket_pending( &ctx->buf.sock ))
get_cmd_result( ctx, 0 ); get_cmd_result( ctx, 0 );
return ret; return ret;
} }
@ -551,10 +544,10 @@ imap_exec_m( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
/* /*
static void static void
drain_imap_replies( imap_t *imap ) drain_imap_replies( imap_store_t *ctx )
{ {
while (imap->num_in_progress) while (ctx->num_in_progress)
get_cmd_result( imap, 0 ); get_cmd_result( ctx, 0 );
} }
*/ */
@ -586,7 +579,7 @@ free_list( list_t *list )
} }
static int static int
parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level ) parse_imap_list_l( imap_store_t *ctx, char **sp, list_t **curp, int level )
{ {
list_t *cur; list_t *cur;
char *s = *sp, *p; char *s = *sp, *p;
@ -606,9 +599,9 @@ parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )
/* sublist */ /* sublist */
s++; s++;
cur->val = LIST; cur->val = LIST;
if (parse_imap_list_l( imap, &s, &cur->child, level + 1 )) if (parse_imap_list_l( ctx, &s, &cur->child, level + 1 ))
goto bail; goto bail;
} else if (imap && *s == '{') { } else if (ctx && *s == '{') {
/* literal */ /* literal */
bytes = cur->len = strtol( s + 1, &s, 10 ); bytes = cur->len = strtol( s + 1, &s, 10 );
if (*s != '}') if (*s != '}')
@ -617,28 +610,28 @@ parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )
s = cur->val = nfmalloc( cur->len ); s = cur->val = nfmalloc( cur->len );
/* dump whats left over in the input buffer */ /* dump whats left over in the input buffer */
n = imap->buf.bytes - imap->buf.offset; n = ctx->buf.bytes - ctx->buf.offset;
if (n > bytes) if (n > bytes)
/* the entire message fit in the buffer */ /* the entire message fit in the buffer */
n = bytes; n = bytes;
memcpy( s, imap->buf.buf + imap->buf.offset, n ); memcpy( s, ctx->buf.buf + ctx->buf.offset, n );
s += n; s += n;
bytes -= n; bytes -= n;
/* mark that we used part of the buffer */ /* mark that we used part of the buffer */
imap->buf.offset += n; ctx->buf.offset += n;
/* now read the rest of the message */ /* now read the rest of the message */
while (bytes > 0) { while (bytes > 0) {
if ((n = socket_read (&imap->buf.sock, s, bytes)) <= 0) if ((n = socket_read (&ctx->buf.sock, s, bytes)) <= 0)
goto bail; goto bail;
s += n; s += n;
bytes -= n; bytes -= n;
} }
if (buffer_gets( &imap->buf, &s )) if (buffer_gets( &ctx->buf, &s ))
goto bail; goto bail;
} else if (*s == '"') { } else if (*s == '"') {
/* quoted string */ /* quoted string */
@ -683,11 +676,11 @@ parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )
} }
static list_t * static list_t *
parse_imap_list( imap_t *imap, char **sp ) parse_imap_list( imap_store_t *ctx, char **sp )
{ {
list_t *head; list_t *head;
if (!parse_imap_list_l( imap, sp, &head, 0 )) if (!parse_imap_list_l( ctx, sp, &head, 0 ))
return head; return head;
free_list( head ); free_list( head );
return NULL; return NULL;
@ -700,7 +693,7 @@ parse_list( char **sp )
} }
static int static int
parse_fetch( imap_t *imap, char *cmd ) /* move this down */ parse_fetch( imap_store_t *ctx, char *cmd ) /* move this down */
{ {
list_t *tmp, *list, *flags; list_t *tmp, *list, *flags;
char *body = 0; char *body = 0;
@ -710,7 +703,7 @@ parse_fetch( imap_t *imap, char *cmd ) /* move this down */
int uid = 0, mask = 0, status = 0, size = 0; int uid = 0, mask = 0, status = 0, size = 0;
unsigned i; unsigned i;
list = parse_imap_list( imap, &cmd ); list = parse_imap_list( ctx, &cmd );
if (!is_list( list )) { if (!is_list( list )) {
error( "IMAP error: bogus FETCH response\n" ); error( "IMAP error: bogus FETCH response\n" );
@ -769,7 +762,7 @@ parse_fetch( imap_t *imap, char *cmd ) /* move this down */
} }
if (body) { if (body) {
for (cmdp = imap->in_progress; cmdp; cmdp = cmdp->next) for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
if (cmdp->cb.uid == uid) if (cmdp->cb.uid == uid)
goto gotuid; goto gotuid;
error( "IMAP error: unexpected FETCH response (UID %d)\n", uid ); error( "IMAP error: unexpected FETCH response (UID %d)\n", uid );
@ -784,8 +777,8 @@ parse_fetch( imap_t *imap, char *cmd ) /* move this down */
} else if (uid) { /* ignore async flag updates for now */ } else if (uid) { /* ignore async flag updates for now */
/* XXX this will need sorting for out-of-order (multiple queries) */ /* XXX this will need sorting for out-of-order (multiple queries) */
cur = nfcalloc( sizeof(*cur) ); cur = nfcalloc( sizeof(*cur) );
*imap->msgapp = &cur->gen; *ctx->msgapp = &cur->gen;
imap->msgapp = &cur->gen.next; ctx->msgapp = &cur->gen.next;
cur->gen.next = 0; cur->gen.next = 0;
cur->gen.uid = uid; cur->gen.uid = uid;
cur->gen.flags = mask; cur->gen.flags = mask;
@ -798,23 +791,22 @@ parse_fetch( imap_t *imap, char *cmd ) /* move this down */
} }
static void static void
parse_capability( imap_t *imap, char *cmd ) parse_capability( imap_store_t *ctx, char *cmd )
{ {
char *arg; char *arg;
unsigned i; unsigned i;
imap->caps = 0x80000000; ctx->caps = 0x80000000;
while ((arg = next_arg( &cmd ))) while ((arg = next_arg( &cmd )))
for (i = 0; i < as(cap_list); i++) for (i = 0; i < as(cap_list); i++)
if (!strcmp( cap_list[i], arg )) if (!strcmp( cap_list[i], arg ))
imap->caps |= 1 << i; ctx->caps |= 1 << i;
imap->rcaps = imap->caps; ctx->rcaps = ctx->caps;
} }
static int static int
parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s ) parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s )
{ {
imap_t *imap = ctx->imap;
char *arg, *p; char *arg, *p;
if (*s != '[') if (*s != '[')
@ -832,12 +824,12 @@ parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s )
return RESP_BAD; return RESP_BAD;
} }
} else if (!strcmp( "UIDNEXT", arg )) { } else if (!strcmp( "UIDNEXT", arg )) {
if (!(arg = next_arg( &s )) || !(imap->uidnext = atoi( arg ))) { if (!(arg = next_arg( &s )) || !(ctx->uidnext = atoi( arg ))) {
error( "IMAP error: malformed NEXTUID status\n" ); error( "IMAP error: malformed NEXTUID status\n" );
return RESP_BAD; return RESP_BAD;
} }
} else if (!strcmp( "CAPABILITY", arg )) { } else if (!strcmp( "CAPABILITY", arg )) {
parse_capability( imap, s ); parse_capability( ctx, s );
} else if (!strcmp( "ALERT", arg )) { } else if (!strcmp( "ALERT", arg )) {
/* RFC2060 says that these messages MUST be displayed /* RFC2060 says that these messages MUST be displayed
* to the user * to the user
@ -856,7 +848,7 @@ parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s )
} }
static void static void
parse_search( imap_t *imap, char *cmd ) parse_search( imap_store_t *ctx, char *cmd )
{ {
char *arg; char *arg;
struct imap_cmd *cmdp; struct imap_cmd *cmdp;
@ -876,7 +868,7 @@ parse_search( imap_t *imap, char *cmd )
* to come in-order, as there are no other means to identify which * to come in-order, as there are no other means to identify which
* SEARCH response belongs to which request. * SEARCH response belongs to which request.
*/ */
for (cmdp = imap->in_progress; cmdp; cmdp = cmdp->next) for (cmdp = ctx->in_progress; cmdp; cmdp = cmdp->next)
if (cmdp->cb.uid == -1) { if (cmdp->cb.uid == -1) {
*(int *)cmdp->cb.ctx = uid; *(int *)cmdp->cb.ctx = uid;
return; return;
@ -887,7 +879,6 @@ parse_search( imap_t *imap, char *cmd )
static void static void
parse_list_rsp( imap_store_t *ctx, char *cmd ) parse_list_rsp( imap_store_t *ctx, char *cmd )
{ {
imap_t *imap = ctx->imap;
char *arg; char *arg;
list_t *list, *lp; list_t *list, *lp;
int l; int l;
@ -908,19 +899,18 @@ parse_list_rsp( imap_store_t *ctx, char *cmd )
arg += l; arg += l;
if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */ if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround broken servers */
return; return;
add_string_list( &imap->boxes, arg ); add_string_list( &ctx->boxes, arg );
} }
static int static int
get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd ) get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
{ {
imap_t *imap = ctx->imap;
struct imap_cmd *cmdp, **pcmdp, *ncmdp; struct imap_cmd *cmdp, **pcmdp, *ncmdp;
char *cmd, *arg, *arg1, *p; char *cmd, *arg, *arg1, *p;
int n, resp, resp2, tag; int n, resp, resp2, tag;
for (;;) { for (;;) {
if (buffer_gets( &imap->buf, &cmd )) if (buffer_gets( &ctx->buf, &cmd ))
return RESP_BAD; return RESP_BAD;
arg = next_arg( &cmd ); arg = next_arg( &cmd );
@ -932,42 +922,42 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
} }
if (!strcmp( "NAMESPACE", arg )) { if (!strcmp( "NAMESPACE", arg )) {
imap->ns_personal = parse_list( &cmd ); ctx->ns_personal = parse_list( &cmd );
imap->ns_other = parse_list( &cmd ); ctx->ns_other = parse_list( &cmd );
imap->ns_shared = parse_list( &cmd ); ctx->ns_shared = parse_list( &cmd );
} else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) || } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
!strcmp( "NO", arg ) || !strcmp( "BYE", arg )) { !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
if ((resp = parse_response_code( ctx, 0, cmd )) != RESP_OK) if ((resp = parse_response_code( ctx, 0, cmd )) != RESP_OK)
return resp; return resp;
} else if (!strcmp( "CAPABILITY", arg )) } else if (!strcmp( "CAPABILITY", arg ))
parse_capability( imap, cmd ); parse_capability( ctx, cmd );
else if (!strcmp( "LIST", arg )) else if (!strcmp( "LIST", arg ))
parse_list_rsp( ctx, cmd ); parse_list_rsp( ctx, cmd );
else if (!strcmp( "SEARCH", arg )) else if (!strcmp( "SEARCH", arg ))
parse_search( imap, cmd ); parse_search( ctx, cmd );
else if ((arg1 = next_arg( &cmd ))) { else if ((arg1 = next_arg( &cmd ))) {
if (!strcmp( "EXISTS", arg1 )) if (!strcmp( "EXISTS", arg1 ))
ctx->gen.count = atoi( arg ); ctx->gen.count = atoi( arg );
else if (!strcmp( "RECENT", arg1 )) else if (!strcmp( "RECENT", arg1 ))
ctx->gen.recent = atoi( arg ); ctx->gen.recent = atoi( arg );
else if(!strcmp ( "FETCH", arg1 )) { else if(!strcmp ( "FETCH", arg1 )) {
if (parse_fetch( imap, cmd )) if (parse_fetch( ctx, cmd ))
return RESP_BAD; return RESP_BAD;
} }
} else { } else {
error( "IMAP error: unable to parse untagged response\n" ); error( "IMAP error: unable to parse untagged response\n" );
return RESP_BAD; return RESP_BAD;
} }
} else if (!imap->in_progress) { } else if (!ctx->in_progress) {
error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" ); error( "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
return RESP_BAD; return RESP_BAD;
} else if (*arg == '+') { } else if (*arg == '+') {
/* This can happen only with the last command underway, as /* This can happen only with the last command underway, as
it enforces a round-trip. */ it enforces a round-trip. */
cmdp = (struct imap_cmd *)((char *)imap->in_progress_append - cmdp = (struct imap_cmd *)((char *)ctx->in_progress_append -
offsetof(struct imap_cmd, next)); offsetof(struct imap_cmd, next));
if (cmdp->cb.data) { if (cmdp->cb.data) {
n = socket_write( &imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen ); n = socket_write( &ctx->buf.sock, cmdp->cb.data, cmdp->cb.dlen );
free( cmdp->cb.data ); free( cmdp->cb.data );
cmdp->cb.data = 0; cmdp->cb.data = 0;
if (n != (int)cmdp->cb.dlen) if (n != (int)cmdp->cb.dlen)
@ -979,25 +969,25 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
error( "IMAP error: unexpected command continuation request\n" ); error( "IMAP error: unexpected command continuation request\n" );
return RESP_BAD; return RESP_BAD;
} }
if (socket_write( &imap->buf.sock, "\r\n", 2 ) != 2) if (socket_write( &ctx->buf.sock, "\r\n", 2 ) != 2)
return RESP_BAD; return RESP_BAD;
if (!cmdp->cb.cont) if (!cmdp->cb.cont)
imap->literal_pending = 0; ctx->literal_pending = 0;
if (!tcmd) if (!tcmd)
return DRV_OK; return DRV_OK;
} else { } else {
tag = atoi( arg ); tag = atoi( arg );
for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next) for (pcmdp = &ctx->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
if (cmdp->tag == tag) if (cmdp->tag == tag)
goto gottag; goto gottag;
error( "IMAP error: unexpected tag %s\n", arg ); error( "IMAP error: unexpected tag %s\n", arg );
return RESP_BAD; return RESP_BAD;
gottag: gottag:
if (!(*pcmdp = cmdp->next)) if (!(*pcmdp = cmdp->next))
imap->in_progress_append = pcmdp; ctx->in_progress_append = pcmdp;
imap->num_in_progress--; ctx->num_in_progress--;
if (cmdp->cb.cont || cmdp->cb.data) if (cmdp->cb.cont || cmdp->cb.data)
imap->literal_pending = 0; ctx->literal_pending = 0;
arg = next_arg( &cmd ); arg = next_arg( &cmd );
if (!strcmp( "OK", arg )) if (!strcmp( "OK", arg ))
resp = DRV_OK; resp = DRV_OK;
@ -1048,29 +1038,22 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
} }
static void static void
imap_close_server( imap_store_t *ictx ) imap_close_store( store_t *gctx )
{ {
imap_t *imap = ictx->imap; imap_store_t *ctx = (imap_store_t *)gctx;
if (imap->buf.sock.fd != -1) { free_generic_messages( gctx->msgs );
imap_exec( ictx, 0, "LOGOUT" ); if (ctx->buf.sock.fd != -1) {
close( imap->buf.sock.fd ); imap_exec( ctx, 0, "LOGOUT" );
close( ctx->buf.sock.fd );
} }
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
if (imap->SSLContext) if (ctx->SSLContext)
SSL_CTX_free( imap->SSLContext ); SSL_CTX_free( ctx->SSLContext );
#endif #endif
free_list( imap->ns_personal ); free_list( ctx->ns_personal );
free_list( imap->ns_other ); free_list( ctx->ns_other );
free_list( imap->ns_shared ); free_list( ctx->ns_shared );
free( imap );
}
static void
imap_close_store( store_t *ctx )
{
imap_close_server( (imap_store_t *)ctx );
free_generic_messages( ctx->msgs );
free( ctx ); free( ctx );
} }
@ -1078,7 +1061,6 @@ imap_close_store( store_t *ctx )
static int static int
start_tls( imap_store_t *ctx ) start_tls( imap_store_t *ctx )
{ {
imap_t *imap = ctx->imap;
int ret; int ret;
static int ssl_inited; static int ssl_inited;
@ -1091,18 +1073,18 @@ start_tls( imap_store_t *ctx )
if (init_ssl_ctx( ctx )) if (init_ssl_ctx( ctx ))
return 1; return 1;
imap->buf.sock.ssl = SSL_new( imap->SSLContext ); ctx->buf.sock.ssl = SSL_new( ctx->SSLContext );
SSL_set_fd( imap->buf.sock.ssl, imap->buf.sock.fd ); SSL_set_fd( ctx->buf.sock.ssl, ctx->buf.sock.fd );
if ((ret = SSL_connect( imap->buf.sock.ssl )) <= 0) { if ((ret = SSL_connect( ctx->buf.sock.ssl )) <= 0) {
socket_perror( "connect", &imap->buf.sock, ret ); socket_perror( "connect", &ctx->buf.sock, ret );
return 1; return 1;
} }
/* verify the server certificate */ /* verify the server certificate */
if (verify_cert( imap->buf.sock.ssl )) if (verify_cert( ctx->buf.sock.ssl ))
return 1; return 1;
imap->buf.sock.use_ssl = 1; ctx->buf.sock.use_ssl = 1;
info( "Connection is now encrypted\n" ); info( "Connection is now encrypted\n" );
return 0; return 0;
} }
@ -1165,7 +1147,6 @@ cram( const char *challenge, const char *user, const char *pass )
static int static int
do_cram_auth (imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt) do_cram_auth (imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt)
{ {
imap_t *imap = ctx->imap;
imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server; imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
char *resp; char *resp;
int n, l; int n, l;
@ -1175,7 +1156,7 @@ do_cram_auth (imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt)
if (DFlags & VERBOSE) if (DFlags & VERBOSE)
printf( ">+> %s\n", resp ); printf( ">+> %s\n", resp );
l = strlen( resp ); l = strlen( resp );
n = socket_write( &imap->buf.sock, resp, l ); n = socket_write( &ctx->buf.sock, resp, l );
free( resp ); free( resp );
if (n != l) if (n != l)
return -1; return -1;
@ -1190,7 +1171,6 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
imap_store_conf_t *cfg = (imap_store_conf_t *)conf; imap_store_conf_t *cfg = (imap_store_conf_t *)conf;
imap_server_conf_t *srvc = cfg->server; imap_server_conf_t *srvc = cfg->server;
imap_store_t *ctx = (imap_store_t *)oldctx; imap_store_t *ctx = (imap_store_t *)oldctx;
imap_t *imap;
char *arg, *rsp; char *arg, *rsp;
struct hostent *he; struct hostent *he;
struct sockaddr_in addr; struct sockaddr_in addr;
@ -1202,17 +1182,15 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
if (ctx) { if (ctx) {
if (((imap_store_conf_t *)(ctx->gen.conf))->server == cfg->server) { if (((imap_store_conf_t *)(ctx->gen.conf))->server == cfg->server) {
ctx->gen.conf = conf; ctx->gen.conf = conf;
imap = ctx->imap;
goto final; goto final;
} }
imap_close_server( ctx ); imap_close_store( &ctx->gen );
} else }
ctx = nfcalloc( sizeof(*ctx) );
ctx = nfcalloc( sizeof(*ctx) );
ctx->gen.conf = conf; ctx->gen.conf = conf;
ctx->imap = imap = nfcalloc( sizeof(*imap) ); ctx->buf.sock.fd = -1;
imap->buf.sock.fd = -1; ctx->in_progress_append = &ctx->in_progress;
imap->in_progress_append = &imap->in_progress;
/* open connection to IMAP server */ /* open connection to IMAP server */
#if HAVE_LIBSSL #if HAVE_LIBSSL
@ -1238,7 +1216,7 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
close (a[0]); close (a[0]);
imap->buf.sock.fd = a[1]; ctx->buf.sock.fd = a[1];
info( "ok\n" ); info( "ok\n" );
} else { } else {
@ -1266,7 +1244,7 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
} }
info( "ok\n" ); info( "ok\n" );
imap->buf.sock.fd = s; ctx->buf.sock.fd = s;
#if HAVE_LIBSSL #if HAVE_LIBSSL
if (srvc->use_imaps) { if (srvc->use_imaps) {
@ -1278,7 +1256,7 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
} }
/* read the greeting string */ /* read the greeting string */
if (buffer_gets( &imap->buf, &rsp )) { if (buffer_gets( &ctx->buf, &rsp )) {
error( "IMAP error: no greeting response\n" ); error( "IMAP error: no greeting response\n" );
goto bail; goto bail;
} }
@ -1295,7 +1273,7 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
goto bail; goto bail;
} }
parse_response_code( ctx, 0, rsp ); parse_response_code( ctx, 0, rsp );
if (!imap->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK) if (!ctx->caps && imap_exec( ctx, 0, "CAPABILITY" ) != RESP_OK)
goto bail; goto bail;
if (!preauth) { if (!preauth) {
@ -1383,13 +1361,13 @@ imap_open_store( store_conf_t *conf, store_t *oldctx )
if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK) if (imap_exec( ctx, 0, "NAMESPACE" ) != RESP_OK)
goto bail; goto bail;
/* XXX for now assume personal namespace */ /* XXX for now assume personal namespace */
if (is_list( imap->ns_personal ) && if (is_list( ctx->ns_personal ) &&
is_list( imap->ns_personal->child ) && is_list( ctx->ns_personal->child ) &&
is_atom( imap->ns_personal->child->child )) is_atom( ctx->ns_personal->child->child ))
ctx->prefix = imap->ns_personal->child->child->val; ctx->prefix = ctx->ns_personal->child->child->val;
} }
ctx->trashnc = 1; ctx->trashnc = 1;
return (store_t *)ctx; return &ctx->gen;
bail: bail:
imap_close_store( &ctx->gen ); imap_close_store( &ctx->gen );
@ -1413,7 +1391,6 @@ static int
imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs ) imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs )
{ {
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
imap_t *imap = ctx->imap;
const char *prefix; const char *prefix;
int ret, i, j, bl; int ret, i, j, bl;
struct imap_cmd_cb cb; struct imap_cmd_cb cb;
@ -1421,10 +1398,10 @@ imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs )
if (!strcmp( gctx->name, "INBOX" )) { if (!strcmp( gctx->name, "INBOX" )) {
// imap->currentnc = 0; // ctx->currentnc = 0;
prefix = ""; prefix = "";
} else { } else {
// imap->currentnc = 1; /* could use LIST results for that */ // ctx->currentnc = 1; /* could use LIST results for that */
prefix = ctx->prefix; prefix = ctx->prefix;
} }
@ -1435,7 +1412,7 @@ imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs )
goto bail; goto bail;
if (gctx->count) { if (gctx->count) {
imap->msgapp = &gctx->msgs; ctx->msgapp = &gctx->msgs;
sort_ints( excs, nexcs ); sort_ints( excs, nexcs );
for (i = 0; i < nexcs; ) { for (i = 0; i < nexcs; ) {
for (bl = 0; i < nexcs && bl < 960; i++) { for (bl = 0; i < nexcs && bl < 960; i++) {
@ -1453,7 +1430,7 @@ imap_select( store_t *gctx, int minuid, int maxuid, int *excs, int nexcs )
goto bail; goto bail;
} }
if (maxuid == INT_MAX) if (maxuid == INT_MAX)
maxuid = imap->uidnext ? imap->uidnext - 1 : 1000000000; maxuid = ctx->uidnext ? ctx->uidnext - 1 : 1000000000;
if (maxuid >= minuid && if (maxuid >= minuid &&
(ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid, (ret = imap_exec_b( ctx, 0, "UID FETCH %d:%d (UID%s%s)", minuid, maxuid,
(gctx->opts & OPEN_FLAGS) ? " FLAGS" : "", (gctx->opts & OPEN_FLAGS) ? " FLAGS" : "",
@ -1549,7 +1526,6 @@ static int
imap_store_msg( store_t *gctx, msg_data_t *data, int *uid ) imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
{ {
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
imap_t *imap = ctx->imap;
struct imap_cmd_cb cb; struct imap_cmd_cb cb;
const char *prefix, *box; const char *prefix, *box;
int ret, d; int ret, d;
@ -1570,18 +1546,18 @@ imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
prefix = ctx->prefix; prefix = ctx->prefix;
cb.create = 1; cb.create = 1;
if (ctx->trashnc) if (ctx->trashnc)
imap->caps = imap->rcaps & ~(1 << LITERALPLUS); ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);
} else { } else {
box = gctx->name; box = gctx->name;
prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix; prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
cb.create = (gctx->opts & OPEN_CREATE) != 0; cb.create = (gctx->opts & OPEN_CREATE) != 0;
/*if (ctx->currentnc) /*if (ctx->currentnc)
imap->caps = imap->rcaps & ~(1 << LITERALPLUS);*/ ctx->caps = ctx->rcaps & ~(1 << LITERALPLUS);*/
*uid = -2; *uid = -2;
} }
cb.ctx = uid; cb.ctx = uid;
ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr ); ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr );
imap->caps = imap->rcaps; ctx->caps = ctx->rcaps;
if (ret != DRV_OK) if (ret != DRV_OK)
return ret; return ret;
if (!uid) if (!uid)
@ -1614,13 +1590,12 @@ static int
imap_list( store_t *gctx, string_list_t **retb ) imap_list( store_t *gctx, string_list_t **retb )
{ {
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
imap_t *imap = ctx->imap;
int ret; int ret;
imap->boxes = 0; ctx->boxes = 0;
if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) != DRV_OK) if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) != DRV_OK)
return ret; return ret;
*retb = imap->boxes; *retb = ctx->boxes;
return DRV_OK; return DRV_OK;
} }