/*
* Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
- * Copyright (C) 1999-2007 Brendan Cully <brendan@kublai.com>
+ * Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#if USE_HCACHE
unsigned int *uid_validity = NULL;
- unsigned int *uidnext = NULL;
+ unsigned int *puidnext = NULL;
+ unsigned int uidnext = 0;
int evalhc = 0;
#endif /* USE_HCACHE */
if (idata->hcache && !msgbegin)
{
uid_validity = mutt_hcache_fetch_raw (idata->hcache, "/UIDVALIDITY", imap_hcache_keylen);
- uidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
- if (uid_validity && uidnext && *uid_validity == idata->uid_validity
- && *uidnext > 0)
+ puidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
+ if (puidnext)
+ {
+ uidnext = *puidnext;
+ FREE (&puidnext);
+ }
+ if (uid_validity && uidnext && *uid_validity == idata->uid_validity)
evalhc = 1;
- else
- FREE (&uidnext);
FREE (&uid_validity);
}
if (evalhc)
M_PROGRESS_MSG, ReadInc, msgend + 1);
snprintf (buf, sizeof (buf),
- "UID FETCH 1:%u (UID FLAGS)", *uidnext - 1);
- FREE (&uidnext);
+ "UID FETCH 1:%u (UID FLAGS)", uidnext - 1);
imap_cmd_start (idata, buf);
- for (msgno = msgbegin; msgno <= msgend ; msgno++)
+ rc = IMAP_CMD_CONTINUE;
+ for (msgno = msgbegin; rc == IMAP_CMD_CONTINUE; msgno++)
{
mutt_progress_update (&progress, msgno + 1, -1);
break;
}
+ if (!h.data->uid)
+ {
+ dprint (2, (debugfile, "imap_read_headers: skipping hcache FETCH "
+ "response for unknown message number %d\n", h.sid));
+ mfhrc = -1;
+ continue;
+ }
+
idx = h.sid - 1;
ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
if (ctx->hdrs[idx])
else if (mfhrc < 0)
break;
+ if (!ftello (fp))
+ {
+ dprint (2, (debugfile, "msg_fetch_header: ignoring fetch response with no body\n"));
+ mfhrc = -1;
+ continue;
+ }
+
/* make sure we don't get remnants from older larger message headers */
fputs ("\n\n", fp);
idx = h.sid - 1;
+ if (idx > msgend)
+ {
+ dprint (1, (debugfile, "imap_read_headers: skipping FETCH response for "
+ "unknown message number %d\n", h.sid));
+ mfhrc = -1;
+ continue;
+ }
+
ctx->hdrs[idx] = mutt_new_header ();
ctx->hdrs[idx]->index = h.sid - 1;
return -1;
imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
+ if (!*mailbox)
+ strfcpy (mailbox, "INBOX", sizeof (mailbox));
if ((fp = fopen (msg->path, "r")) == NULL)
{
{
IMAP_DATA* idata;
BUFFER cmd, sync_cmd;
- char uid[11];
char mbox[LONG_STRING];
char mmbox[LONG_STRING];
+ char prompt[LONG_STRING];
int rc;
int n;
IMAP_MBOX mx;
int err_continue = M_NO;
+ int triedcreate = 0;
idata = (IMAP_DATA*) ctx->data;
}
imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
+ if (!*mbox)
+ strfcpy (mbox, "INBOX", sizeof (mbox));
+ imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
- memset (&sync_cmd, 0, sizeof (sync_cmd));
- memset (&cmd, 0, sizeof (cmd));
- mutt_buffer_addstr (&cmd, "UID COPY ");
-
- /* Null HEADER* means copy tagged messages */
- if (!h)
+ /* loop in case of TRYCREATE */
+ do
{
- /* if any messages have attachments to delete, fall through to FETCH
- * and APPEND. TODO: Copy what we can with COPY, fall through for the
- * remainder. */
- for (n = 0; n < ctx->msgcount; n++)
+ memset (&sync_cmd, 0, sizeof (sync_cmd));
+ memset (&cmd, 0, sizeof (cmd));
+
+ /* Null HEADER* means copy tagged messages */
+ if (!h)
{
- if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
+ /* if any messages have attachments to delete, fall through to FETCH
+ * and APPEND. TODO: Copy what we can with COPY, fall through for the
+ * remainder. */
+ for (n = 0; n < ctx->msgcount; n++)
{
- dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
- return 1;
+ if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
+ {
+ dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
+ return 1;
+ }
+
+ if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
+ ctx->hdrs[n]->changed)
+ {
+ rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
+ if (rc < 0)
+ {
+ dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
+ goto fail;
+ }
+ }
}
- if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
- ctx->hdrs[n]->changed)
+ rc = imap_exec_msgset (idata, "UID COPY", mmbox, M_TAG, 0, 0);
+ if (!rc)
{
- rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
- if (rc < 0)
- {
- dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
- goto fail;
- }
+ dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
+ goto fail;
+ }
+ else if (rc < 0)
+ {
+ dprint (1, (debugfile, "could not queue copy\n"));
+ goto fail;
}
+ else
+ mutt_message (_("Copying %d messages to %s..."), rc, mbox);
}
-
- rc = imap_make_msg_set (idata, &cmd, M_TAG, 0, 0);
- if (!rc)
+ else
{
- dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
- goto fail;
- }
- mutt_message (_("Copying %d messages to %s..."), rc, mbox);
- }
- else
- {
- mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
- snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
- mutt_buffer_addstr (&cmd, uid);
+ mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
+ mutt_buffer_printf (&cmd, "UID COPY %u %s", HEADER_DATA (h)->uid, mmbox);
- if (h->active && h->changed)
- {
- rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
- if (rc < 0)
+ if (h->active && h->changed)
{
- dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
- goto fail;
+ rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
+ if (rc < 0)
+ {
+ dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
+ goto fail;
+ }
+ }
+ if ((rc = imap_exec (idata, cmd.data, IMAP_CMD_QUEUE)) < 0)
+ {
+ dprint (1, (debugfile, "could not queue copy\n"));
+ goto fail;
}
}
- }
-
- /* let's get it on */
- mutt_buffer_addstr (&cmd, " ");
- imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
- mutt_buffer_addstr (&cmd, mmbox);
- rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
- if (rc == -2)
- {
- /* bail out if command failed for reasons other than nonexistent target */
- if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
- {
- imap_error ("imap_copy_messages", idata->buf);
- goto fail;
- }
- dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
- snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
- if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
+ /* let's get it on */
+ rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
+ if (rc == -2)
{
- mutt_clear_error ();
- goto fail;
+ if (triedcreate)
+ {
+ dprint (1, (debugfile, "Already tried to create mailbox %s\n", mbox));
+ break;
+ }
+ /* bail out if command failed for reasons other than nonexistent target */
+ if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
+ break;
+ dprint (3, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
+ snprintf (prompt, sizeof (prompt), _("Create %s?"), mbox);
+ if (option (OPTCONFIRMCREATE) && mutt_yesorno (prompt, 1) < 1)
+ {
+ mutt_clear_error ();
+ break;
+ }
+ if (imap_create_mailbox (idata, mbox) < 0)
+ break;
+ triedcreate = 1;
}
- if (imap_create_mailbox (idata, mbox) < 0)
- goto fail;
-
- /* try again */
- rc = imap_exec (idata, cmd.data, 0);
}
+ while (rc == -2);
+
if (rc != 0)
{
imap_error ("imap_copy_messages", idata->buf);
/* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
* read header lines and call it again. Silly. */
- if ((rc = msg_parse_fetch (h, buf) != -2) || !fp)
+ if ((rc = msg_parse_fetch (h, buf)) != -2 || !fp)
return rc;
if (imap_get_literal_count (buf, &bytes) == 0)