~ubuntu-dev/ubuntu/lucid/mutt/lucid-201002110857

« back to all changes in this revision

Viewing changes to imap/message.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-06-07 17:30:03 UTC
  • mto: (16.2.1 experimental) (2.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090607173003-rg37ui3h2bbv7wl0
Tags: upstream-1.5.19
ImportĀ upstreamĀ versionĀ 1.5.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3
 
 * Copyright (C) 1999-2007 Brendan Cully <brendan@kublai.com>
 
3
 * Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>
4
4
 * 
5
5
 *     This program is free software; you can redistribute it and/or modify
6
6
 *     it under the terms of the GNU General Public License as published by
76
76
 
77
77
#if USE_HCACHE
78
78
  unsigned int *uid_validity = NULL;
79
 
  unsigned int *uidnext = NULL;
 
79
  unsigned int *puidnext = NULL;
 
80
  unsigned int uidnext = 0;
80
81
  int evalhc = 0;
81
82
#endif /* USE_HCACHE */
82
83
 
124
125
  if (idata->hcache && !msgbegin)
125
126
  {
126
127
    uid_validity = mutt_hcache_fetch_raw (idata->hcache, "/UIDVALIDITY", imap_hcache_keylen);
127
 
    uidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
128
 
    if (uid_validity && uidnext && *uid_validity == idata->uid_validity
129
 
        && *uidnext > 0)
 
128
    puidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
 
129
    if (puidnext)
 
130
    {
 
131
      uidnext = *puidnext;
 
132
      FREE (&puidnext);
 
133
    }
 
134
    if (uid_validity && uidnext && *uid_validity == idata->uid_validity)
130
135
      evalhc = 1;
131
 
    else
132
 
      FREE (&uidnext);
133
136
    FREE (&uid_validity);
134
137
  }
135
138
  if (evalhc)
138
141
                        M_PROGRESS_MSG, ReadInc, msgend + 1);
139
142
 
140
143
    snprintf (buf, sizeof (buf),
141
 
      "UID FETCH 1:%u (UID FLAGS)", *uidnext - 1);
142
 
    FREE (&uidnext);
 
144
      "UID FETCH 1:%u (UID FLAGS)", uidnext - 1);
143
145
  
144
146
    imap_cmd_start (idata, buf);
145
147
  
146
 
    for (msgno = msgbegin; msgno <= msgend ; msgno++)
 
148
    rc = IMAP_CMD_CONTINUE;
 
149
    for (msgno = msgbegin; rc == IMAP_CMD_CONTINUE; msgno++)
147
150
    {
148
151
      mutt_progress_update (&progress, msgno + 1, -1);
149
152
  
173
176
          break;
174
177
        }
175
178
 
 
179
        if (!h.data->uid)
 
180
        {
 
181
          dprint (2, (debugfile, "imap_read_headers: skipping hcache FETCH "
 
182
                      "response for unknown message number %d\n", h.sid));
 
183
          mfhrc = -1;
 
184
          continue;
 
185
        }
 
186
        
176
187
        idx = h.sid - 1;
177
188
        ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
178
189
        if (ctx->hdrs[idx])
258
269
      else if (mfhrc < 0)
259
270
        break;
260
271
 
 
272
      if (!ftello (fp))
 
273
      {
 
274
        dprint (2, (debugfile, "msg_fetch_header: ignoring fetch response with no body\n"));
 
275
        mfhrc = -1;
 
276
        continue;
 
277
      }
 
278
 
261
279
      /* make sure we don't get remnants from older larger message headers */
262
280
      fputs ("\n\n", fp);
263
281
 
264
282
      idx = h.sid - 1;
 
283
      if (idx > msgend)
 
284
      {
 
285
        dprint (1, (debugfile, "imap_read_headers: skipping FETCH response for "
 
286
                    "unknown message number %d\n", h.sid));
 
287
        mfhrc = -1;
 
288
        continue;
 
289
      }
 
290
 
265
291
      ctx->hdrs[idx] = mutt_new_header ();
266
292
 
267
293
      ctx->hdrs[idx]->index = h.sid - 1;
574
600
    return -1;
575
601
 
576
602
  imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
 
603
  if (!*mailbox)
 
604
    strfcpy (mailbox, "INBOX", sizeof (mailbox));
577
605
  
578
606
  if ((fp = fopen (msg->path, "r")) == NULL)
579
607
  {
687
715
{
688
716
  IMAP_DATA* idata;
689
717
  BUFFER cmd, sync_cmd;
690
 
  char uid[11];
691
718
  char mbox[LONG_STRING];
692
719
  char mmbox[LONG_STRING];
 
720
  char prompt[LONG_STRING];
693
721
  int rc;
694
722
  int n;
695
723
  IMAP_MBOX mx;
696
724
  int err_continue = M_NO;
 
725
  int triedcreate = 0;
697
726
 
698
727
  idata = (IMAP_DATA*) ctx->data;
699
728
 
718
747
  }
719
748
  
720
749
  imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
721
 
 
722
 
  memset (&sync_cmd, 0, sizeof (sync_cmd));
723
 
  memset (&cmd, 0, sizeof (cmd));
724
 
  mutt_buffer_addstr (&cmd, "UID COPY ");
725
 
 
726
 
  /* Null HEADER* means copy tagged messages */
727
 
  if (!h)
728
 
  {
729
 
    /* if any messages have attachments to delete, fall through to FETCH
730
 
     * and APPEND. TODO: Copy what we can with COPY, fall through for the
731
 
     * remainder. */
732
 
    for (n = 0; n < ctx->msgcount; n++)
733
 
    {
734
 
      if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
735
 
      {
736
 
        dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
737
 
        return 1;
738
 
      }
739
 
 
740
 
      if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
741
 
          ctx->hdrs[n]->changed)
742
 
      {
743
 
        rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
744
 
        if (rc < 0)
745
 
        {
746
 
          dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
747
 
          goto fail;
748
 
        }
749
 
      }
750
 
    }
751
 
 
752
 
    rc = imap_make_msg_set (idata, &cmd, M_TAG, 0, 0);
753
 
    if (!rc)
754
 
    {
755
 
      dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
756
 
      goto fail;
757
 
    }
758
 
    mutt_message (_("Copying %d messages to %s..."), rc, mbox);
759
 
  }
760
 
  else
761
 
  {
762
 
    mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
763
 
    snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
764
 
    mutt_buffer_addstr (&cmd, uid);
765
 
 
766
 
    if (h->active && h->changed)
767
 
    {
768
 
      rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
769
 
      if (rc < 0)
770
 
      {
771
 
        dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
772
 
        goto fail;
773
 
      }
774
 
    }
775
 
  }
776
 
 
777
 
  /* let's get it on */
778
 
  mutt_buffer_addstr (&cmd, " ");
 
750
  if (!*mbox)
 
751
    strfcpy (mbox, "INBOX", sizeof (mbox));
779
752
  imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
780
 
  mutt_buffer_addstr (&cmd, mmbox);
781
753
 
782
 
  rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
783
 
  if (rc == -2)
 
754
  /* loop in case of TRYCREATE */
 
755
  do
784
756
  {
785
 
    /* bail out if command failed for reasons other than nonexistent target */
786
 
    if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
787
 
    {
788
 
      imap_error ("imap_copy_messages", idata->buf);
789
 
      goto fail;
790
 
    }
791
 
    dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
792
 
    snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
793
 
    if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
794
 
    {
795
 
      mutt_clear_error ();
796
 
      goto fail;
797
 
    }
798
 
    if (imap_create_mailbox (idata, mbox) < 0)
799
 
      goto fail;
800
 
 
801
 
    /* try again */
802
 
    rc = imap_exec (idata, cmd.data, 0);
 
757
    memset (&sync_cmd, 0, sizeof (sync_cmd));
 
758
    memset (&cmd, 0, sizeof (cmd));
 
759
 
 
760
    /* Null HEADER* means copy tagged messages */
 
761
    if (!h)
 
762
    {
 
763
      /* if any messages have attachments to delete, fall through to FETCH
 
764
       * and APPEND. TODO: Copy what we can with COPY, fall through for the
 
765
       * remainder. */
 
766
      for (n = 0; n < ctx->msgcount; n++)
 
767
      {
 
768
        if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
 
769
        {
 
770
          dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
 
771
          return 1;
 
772
        }
 
773
 
 
774
        if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
 
775
            ctx->hdrs[n]->changed)
 
776
        {
 
777
          rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
 
778
          if (rc < 0)
 
779
          {
 
780
            dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
 
781
            goto fail;
 
782
          }
 
783
        }
 
784
      }
 
785
 
 
786
      rc = imap_exec_msgset (idata, "UID COPY", mmbox, M_TAG, 0, 0);
 
787
      if (!rc)
 
788
      {
 
789
        dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
 
790
        goto fail;
 
791
      }
 
792
      else if (rc < 0)
 
793
      {
 
794
        dprint (1, (debugfile, "could not queue copy\n"));
 
795
        goto fail;
 
796
      }
 
797
      else
 
798
        mutt_message (_("Copying %d messages to %s..."), rc, mbox);
 
799
    }
 
800
    else
 
801
    {
 
802
      mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
 
803
      mutt_buffer_printf (&cmd, "UID COPY %u %s", HEADER_DATA (h)->uid, mmbox);
 
804
 
 
805
      if (h->active && h->changed)
 
806
      {
 
807
        rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
 
808
        if (rc < 0)
 
809
        {
 
810
          dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
 
811
          goto fail;
 
812
        }
 
813
      }    
 
814
      if ((rc = imap_exec (idata, cmd.data, IMAP_CMD_QUEUE)) < 0)
 
815
      {
 
816
        dprint (1, (debugfile, "could not queue copy\n"));
 
817
        goto fail;
 
818
      }
 
819
    }
 
820
 
 
821
    /* let's get it on */
 
822
    rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
 
823
    if (rc == -2)
 
824
    {
 
825
      if (triedcreate)
 
826
      {
 
827
        dprint (1, (debugfile, "Already tried to create mailbox %s\n", mbox));
 
828
        break;
 
829
      }
 
830
      /* bail out if command failed for reasons other than nonexistent target */
 
831
      if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
 
832
        break;
 
833
      dprint (3, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
 
834
      snprintf (prompt, sizeof (prompt), _("Create %s?"), mbox);
 
835
      if (option (OPTCONFIRMCREATE) && mutt_yesorno (prompt, 1) < 1)
 
836
      {
 
837
        mutt_clear_error ();
 
838
        break;
 
839
      }
 
840
      if (imap_create_mailbox (idata, mbox) < 0)
 
841
        break;
 
842
      triedcreate = 1;
 
843
    }
803
844
  }
 
845
  while (rc == -2);
 
846
 
804
847
  if (rc != 0)
805
848
  {
806
849
    imap_error ("imap_copy_messages", idata->buf);
1041
1084
 
1042
1085
  /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
1043
1086
   *   read header lines and call it again. Silly. */
1044
 
  if ((rc = msg_parse_fetch (h, buf) != -2) || !fp)
 
1087
  if ((rc = msg_parse_fetch (h, buf)) != -2 || !fp)
1045
1088
    return rc;
1046
1089
  
1047
1090
  if (imap_get_literal_count (buf, &bytes) == 0)