Code cleanup

This commit is contained in:
Pierre Pronchery 2012-12-17 16:24:56 +01:00
parent 92d435dd9b
commit b5fd806d45

View File

@ -460,6 +460,11 @@ static int _imap4_lookup(IMAP4 * imap4, char const * hostname, uint16_t port,
/* imap4_parse */ /* imap4_parse */
static int _parse_context(IMAP4 * imap4, char const * answer); static int _parse_context(IMAP4 * imap4, char const * answer);
static int _context_fetch(IMAP4 * imap4, char const * answer); static int _context_fetch(IMAP4 * imap4, char const * answer);
static int _context_fetch_body(IMAP4 * imap4, char const * answer);
static int _context_fetch_command(IMAP4 * imap4, char const * answer);
static int _context_fetch_flags(IMAP4 * imap4, char const * answer);
static int _context_fetch_headers(IMAP4 * imap4, char const * answer);
static int _context_fetch_id(IMAP4 * imap4, char const * answer);
static int _context_init(IMAP4 * imap4); static int _context_init(IMAP4 * imap4);
static int _context_list(IMAP4 * imap4, char const * answer); static int _context_list(IMAP4 * imap4, char const * answer);
static int _context_login(IMAP4 * imap4, char const * answer); static int _context_login(IMAP4 * imap4, char const * answer);
@ -558,13 +563,7 @@ static int _parse_context(IMAP4 * imap4, char const * answer)
static int _context_fetch(IMAP4 * imap4, char const * answer) static int _context_fetch(IMAP4 * imap4, char const * answer)
{ {
AccountPluginHelper * helper = imap4->helper;
IMAP4Command * cmd = &imap4->queue[0]; IMAP4Command * cmd = &imap4->queue[0];
AccountFolder * folder = cmd->data.fetch.folder;
AccountMessage * message = cmd->data.fetch.message;
unsigned int id = cmd->data.fetch.id;
char * p;
size_t i;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%p, \"%s\")\n", __func__, (void *)folder, fprintf(stderr, "DEBUG: %s(%p, \"%s\")\n", __func__, (void *)folder,
@ -578,24 +577,57 @@ static int _context_fetch(IMAP4 * imap4, char const * answer)
switch(cmd->data.fetch.status) switch(cmd->data.fetch.status)
{ {
case I4FS_ID: case I4FS_ID:
id = strtol(answer, &p, 10); return _context_fetch_id(imap4, answer);
if(answer[0] == '\0' || *p != ' ')
return 0;
cmd->data.fetch.id = id;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s() id=%u\n", __func__, id);
#endif
answer = p;
if(strncmp(answer, " FETCH ", 7) != 0)
return -1;
/* skip spaces */
for(i = 7; answer[i] == ' '; i++);
if(answer[i++] != '(')
return 0;
/* fallback */
answer = &answer[i];
cmd->data.fetch.status = I4FS_COMMAND;
case I4FS_COMMAND: case I4FS_COMMAND:
return _context_fetch_command(imap4, answer);
case I4FS_FLAGS:
return _context_fetch_flags(imap4, answer);
case I4FS_BODY:
return _context_fetch_body(imap4, answer);
case I4FS_HEADERS:
return _context_fetch_headers(imap4, answer);
}
return -1;
}
static int _context_fetch_body(IMAP4 * imap4, char const * answer)
{
AccountPluginHelper * helper = imap4->helper;
IMAP4Command * cmd = &imap4->queue[0];
AccountMessage * message = cmd->data.fetch.message;
size_t i;
/* check the size */
if(cmd->data.fetch.size == 0)
{
/* XXX may not be the case (flags...) */
if(strcmp(answer, ")") == 0)
{
cmd->data.fetch.status = I4FS_ID;
return 0;
}
else
{
cmd->data.fetch.status = I4FS_COMMAND;
return _context_fetch(imap4, answer);
}
}
if((i = strlen(answer) + 2) <= cmd->data.fetch.size)
cmd->data.fetch.size -= i;
helper->message_set_body(message->message, answer, strlen(answer), 1);
helper->message_set_body(message->message, "\r\n", 2, 1);
return 0;
}
static int _context_fetch_command(IMAP4 * imap4, char const * answer)
{
IMAP4Command * cmd = &imap4->queue[0];
AccountFolder * folder = cmd->data.fetch.folder;
AccountMessage * message = cmd->data.fetch.message;
unsigned int id = cmd->data.fetch.id;
char * p;
size_t i;
/* skip spaces */ /* skip spaces */
for(i = 0; answer[i] == ' '; i++); for(i = 0; answer[i] == ' '; i++);
if(strncmp(&answer[i], "FLAGS ", 6) == 0) if(strncmp(&answer[i], "FLAGS ", 6) == 0)
@ -612,17 +644,21 @@ static int _context_fetch(IMAP4 * imap4, char const * answer)
if(answer[i] != '{') if(answer[i] != '{')
return -1; return -1;
cmd->data.fetch.size = strtoul(&answer[++i], &p, 10); cmd->data.fetch.size = strtoul(&answer[++i], &p, 10);
if(answer[i] == '\0' || *p != '}' if(answer[i] == '\0' || *p != '}' || cmd->data.fetch.size == 0)
|| cmd->data.fetch.size == 0)
return -1; return -1;
if((message = _imap4_folder_get_message(imap4, folder, if((message = _imap4_folder_get_message(imap4, folder, id)) != NULL)
id)) != NULL)
{ {
cmd->data.fetch.status = I4FS_HEADERS; cmd->data.fetch.status = I4FS_HEADERS;
cmd->data.fetch.message = message; cmd->data.fetch.message = message;
} }
return (message != NULL) ? 0 : -1; return (message != NULL) ? 0 : -1;
case I4FS_FLAGS: }
static int _context_fetch_flags(IMAP4 * imap4, char const * answer)
{
IMAP4Command * cmd = &imap4->queue[0];
size_t i;
/* skip spaces */ /* skip spaces */
for(i = 0; answer[i] == ' '; i++); for(i = 0; answer[i] == ' '; i++);
if(strncmp(&answer[i], "FLAGS ", 6) != 0) if(strncmp(&answer[i], "FLAGS ", 6) != 0)
@ -648,30 +684,15 @@ static int _context_fetch(IMAP4 * imap4, char const * answer)
} }
cmd->data.fetch.status = I4FS_COMMAND; cmd->data.fetch.status = I4FS_COMMAND;
return _context_fetch(imap4, &answer[i]); return _context_fetch(imap4, &answer[i]);
case I4FS_BODY: }
/* check the size */
if(cmd->data.fetch.size == 0) static int _context_fetch_headers(IMAP4 * imap4, char const * answer)
{ {
/* XXX may not be the case (flags...) */ AccountPluginHelper * helper = imap4->helper;
if(strcmp(answer, ")") == 0) IMAP4Command * cmd = &imap4->queue[0];
{ AccountMessage * message = cmd->data.fetch.message;
cmd->data.fetch.status = I4FS_ID; size_t i;
return 0;
}
else
{
cmd->data.fetch.status = I4FS_COMMAND;
return _context_fetch(imap4, answer);
}
}
if((i = strlen(answer) + 2) <= cmd->data.fetch.size)
cmd->data.fetch.size -= i;
helper->message_set_body(message->message, answer,
strlen(answer), 1);
helper->message_set_body(message->message, "\r\n", 2,
1);
return 0;
case I4FS_HEADERS:
/* check the size */ /* check the size */
if((i = strlen(answer) + 2) <= cmd->data.fetch.size) if((i = strlen(answer) + 2) <= cmd->data.fetch.size)
cmd->data.fetch.size -= i; cmd->data.fetch.size -= i;
@ -679,19 +700,41 @@ static int _context_fetch(IMAP4 * imap4, char const * answer)
{ {
/* beginning of the body */ /* beginning of the body */
cmd->data.fetch.status = I4FS_BODY; cmd->data.fetch.status = I4FS_BODY;
helper->message_set_body(message->message, NULL, helper->message_set_body(message->message, NULL, 0, 0);
0, 0);
} }
/* XXX check this before parsing anything */ /* XXX check this before parsing anything */
else if(cmd->data.fetch.size == 0 else if(cmd->data.fetch.size == 0 || i > cmd->data.fetch.size)
|| i > cmd->data.fetch.size)
return 0; return 0;
else else
helper->message_set_header(message->message, helper->message_set_header(message->message, answer);
answer);
return 0; return 0;
} }
static int _context_fetch_id(IMAP4 * imap4, char const * answer)
{
IMAP4Command * cmd = &imap4->queue[0];
unsigned int id = cmd->data.fetch.id;
char * p;
size_t i;
id = strtol(answer, &p, 10);
if(answer[0] == '\0' || *p != ' ')
return 0;
cmd->data.fetch.id = id;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s() id=%u\n", __func__, id);
#endif
answer = p;
if(strncmp(answer, " FETCH ", 7) != 0)
return -1; return -1;
/* skip spaces */
for(i = 7; answer[i] == ' '; i++);
if(answer[i++] != '(')
return 0;
/* fallback */
answer = &answer[i];
cmd->data.fetch.status = I4FS_COMMAND;
return _context_fetch_command(imap4, answer);
} }
static int _context_init(IMAP4 * imap4) static int _context_init(IMAP4 * imap4)