summaryrefslogtreecommitdiff
path: root/src/operator-copy.cc
blob: 06204b204988f6c205a979819ca7fefb40141f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 *  @file operator-copy.cc
 *  @brief Implementation of the COPY command.
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 */

#include "convert.h"
#include "depot.h"
#include "iodevice.h"
#include "iofactory.h"
#include "maildir.h"
#include "operators.h"
#include "recursivedescent.h"
#include "session.h"

#include <string>

using namespace Binc;
using std::string;

CopyOperator::CopyOperator() {}

CopyOperator::~CopyOperator() {}

const string CopyOperator::getName() const
{
  return "COPY";
}

int CopyOperator::getState() const
{
  return Session::SELECTED;
}

Operator::ProcessResult CopyOperator::process(Depot &depot, Request &command)
{
  Session &session = Session::getInstance();

  // Get the current mailbox
  Mailbox *srcMailbox = depot.getSelected();

  // Get the destination mailbox
  string dmailbox = command.getMailbox();
  Mailbox *destMailbox = depot.get(toCanonMailbox(dmailbox));
  if (destMailbox == nullptr) {
    session.setResponseCode("TRYCREATE");
    session.setLastError("invalid mailbox " + toImapString(dmailbox));
    return NO;
  }

  unsigned int mode = Mailbox::SKIP_EXPUNGED;
  mode |= command.getUidMode() ? Mailbox::UID_MODE : Mailbox::SQNR_MODE;

  // Copy each message in the sequence set to the destination mailbox.
  bool success = true;
  Mailbox::iterator i = srcMailbox->begin(command.bset, mode);
  for (; success && i != srcMailbox->end(); ++i) {
    Message &source = *i;

    if (srcMailbox->fastCopy(source,
                             *destMailbox,
                             depot.mailboxToFilename(toCanonMailbox(dmailbox))))
      continue;

    // Have the destination mailbox create a message for us.
    Message *dest = destMailbox->createMessage(
        depot.mailboxToFilename(toCanonMailbox(dmailbox)),
        source.getInternalDate());
    if (!dest) {
      session.setLastError(destMailbox->getLastError());
      success = false;
      break;
    }

    // Set the flags and internal date.
    dest->setStdFlag(source.getStdFlags());
    dest->setInternalDate(source.getInternalDate());

    // Copy chunks from the source message over to the destination
    // message.
    string chunk;
    do {
      int readSize = source.readChunk(chunk);

      if (readSize == 0) {
        break;
      } else if (readSize == -1) {
        bincWarning << "when reading from message " << i.getSqnr() << "/" << source.getUID()
                    << " in \"" << srcMailbox->getName() << "\": " << source.getLastError()
                    << std::endl;
        success = false;
      } else if (!dest->appendChunk(chunk)) {
        bincWarning << "when writing to \"" << dmailbox << "\": " << dest->getLastError()
                    << std::endl;
        success = false;
      }
    } while (success);

    dest->close();
  }

  if (!success && !destMailbox->rollBackNewMessages()) {
    session.setLastError("Failed to rollback after unsuccessful copy: "
                         + destMailbox->getLastError());
    return NO;
  }

  if (success) {
    if (!destMailbox->commitNewMessages(depot.mailboxToFilename(toCanonMailbox(dmailbox)))) {
      session.setLastError("Failed to commit after successful copy: "
                           + destMailbox->getLastError());
      return NO;
    }
  }

  if (!success) {
    session.setLastError("The transaction was unrolled. Please "
                         "contant your system administrator for "
                         "more information.");
  }

  return success ? OK : NO;
}

Operator::ParseResult CopyOperator::parse(Request &c_in) const
{
  Session &session = Session::getInstance();

  Operator::ParseResult res;
  if ((res = expectSPACE()) != ACCEPT) {
    session.setLastError("Expected SPACE after COPY");
    return res;
  }

  if ((res = expectSet(c_in.getSet())) != ACCEPT) {
    session.setLastError("Expected sequence set after COPY SPACE");
    return res;
  }

  if ((res = expectSPACE()) != ACCEPT) {
    session.setLastError("Expected SPACE after COPY SPACE set");
    return res;
  }

  string mailbox;
  if ((res = expectMailbox(mailbox)) != ACCEPT) {
    session.setLastError("Expected mailbox after COPY SPACE set SPACE");
    return res;
  }

  if ((res = expectCRLF()) != ACCEPT) {
    session.setLastError("Expected CRLF after COPY SPACE set SPACE mailbox");
    return res;
  }

  c_in.setMailbox(mailbox);
  c_in.setName("COPY");

  return ACCEPT;
}