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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/**
* @file operator-append.cc
* @brief Implementation of the APPEND command.
* @author Andreas Aardal Hanssen
* @date 2002-2005
*/
#include "depot.h"
#include "iodevice.h"
#include "iofactory.h"
#include "mailbox.h"
#include "operators.h"
#include "pendingupdates.h"
#include "recursivedescent.h"
#include "session.h"
#include <algorithm>
#include <string>
#include <fcntl.h>
using namespace Binc;
using std::string;
AppendOperator::AppendOperator(void) {}
AppendOperator::~AppendOperator(void) {}
const string AppendOperator::getName(void) const
{
return "APPEND";
}
int AppendOperator::getState(void) const
{
return Session::AUTHENTICATED | Session::SELECTED;
}
Operator::ProcessResult AppendOperator::process(Depot &depot, Request &command)
{
Session &session = Session::getInstance();
const string &srcmailbox = command.getMailbox();
const string &canonmailbox = toCanonMailbox(srcmailbox);
Mailbox *mailbox = nullptr;
if ((mailbox = depot.get(canonmailbox)) == nullptr) {
session.setResponseCode("TRYCREATE");
session.setLastError("invalid destination mailbox " + toImapString(srcmailbox));
return NO;
}
// mask all passed flags together
unsigned int newflags = (unsigned int)Message::F_NONE;
std::vector<string>::const_iterator f_i = command.flags.begin();
while (f_i != command.flags.end()) {
if (*f_i == "\\Deleted") newflags |= Message::F_DELETED;
if (*f_i == "\\Answered") newflags |= Message::F_ANSWERED;
if (*f_i == "\\Seen") newflags |= Message::F_SEEN;
if (*f_i == "\\Draft") newflags |= Message::F_DRAFT;
if (*f_i == "\\Flagged") newflags |= Message::F_FLAGGED;
++f_i;
}
int mday, year, hour, minute, second;
char month[4];
struct tm mytm;
if (command.getDate() != "") {
sscanf(command.getDate().c_str(),
"%2i-%3s-%4i %2i:%2i:%2i",
&mday,
month,
&year,
&hour,
&minute,
&second);
month[3] = '\0';
string monthstr = month;
lowercase(monthstr);
mytm.tm_sec = second;
mytm.tm_min = minute;
mytm.tm_hour = hour;
mytm.tm_year = year - 1900;
mytm.tm_mday = mday;
if (monthstr == "jan")
mytm.tm_mon = 0;
else if (monthstr == "feb")
mytm.tm_mon = 1;
else if (monthstr == "mar")
mytm.tm_mon = 2;
else if (monthstr == "apr")
mytm.tm_mon = 3;
else if (monthstr == "may")
mytm.tm_mon = 4;
else if (monthstr == "jun")
mytm.tm_mon = 5;
else if (monthstr == "jul")
mytm.tm_mon = 6;
else if (monthstr == "aug")
mytm.tm_mon = 7;
else if (monthstr == "sep")
mytm.tm_mon = 8;
else if (monthstr == "oct")
mytm.tm_mon = 9;
else if (monthstr == "nov")
mytm.tm_mon = 10;
else if (monthstr == "dec")
mytm.tm_mon = 11;
mytm.tm_isdst = -1;
}
// Read number of characters in literal. Literal is required here.
char c;
if (!bincClient.readChar(&c)) return ABORT;
if (c != '{') {
session.setLastError("expected literal");
return BAD;
}
string nr;
bool literalPlus = false;
while (1) {
if (!bincClient.readChar(&c)) {
session.setLastError("unexcepted EOF");
return BAD;
}
if (c == '}') break;
// Support LITERAL+
if (c == '+' && !literalPlus) {
literalPlus = true;
continue;
}
if (!isdigit(c)) {
session.setLastError("unexcepted non-digit character");
return BAD;
}
if (literalPlus) {
session.setLastError("expected '}'");
return BAD;
}
nr += c;
}
int nchars = atoi(nr.c_str());
if (nchars < 0) {
session.setLastError("expected positive size of appended message");
return BAD;
}
if (!bincClient.readChar(&c)) return ABORT;
if (c != '\r') {
session.setLastError("expected CR");
return BAD;
}
if (!bincClient.readChar(&c)) return ABORT;
if (c != '\n') {
session.setLastError("expected LF");
return BAD;
}
time_t newtime = (command.getDate() != "") ? mktime(&mytm) : time(nullptr);
if (newtime == -1) newtime = time(nullptr);
Message *dest = mailbox->createMessage(depot.mailboxToFilename(canonmailbox), newtime);
if (!dest) {
session.setLastError(mailbox->getLastError());
return NO;
}
if (!literalPlus) {
bincClient << "+ go ahead with " << nchars << " characters" << std::endl;
bincClient.flush();
}
bincClient.clearFlags(IODevice::HasInputLimit);
while (nchars > 0) {
// Read in chunks of 8192, followed by an optional chunk at the
// end which is < 8192 bytes.
string s;
int bytesToRead = nchars > 8192 ? 8192 : nchars;
if (!bincClient.readStr(&s, bytesToRead)) {
mailbox->rollBackNewMessages();
session.setLastError(bincClient.getLastErrorString());
return NO;
}
// Write the chunk to the message.
if (!dest->appendChunk(s)) {
mailbox->rollBackNewMessages();
session.setLastError(dest->getLastError());
return NO;
}
// Update the message count.
nchars -= s.size();
}
// Read the trailing CRLF after the message data.
if (!bincClient.readChar(&c)) return ABORT;
if (c != '\r') {
mailbox->rollBackNewMessages();
session.setLastError("expected CR");
return BAD;
}
if (!bincClient.readChar(&c)) return ABORT;
if (c != '\n') {
mailbox->rollBackNewMessages();
session.setLastError("expected LF");
return BAD;
}
// Commit the message.
dest->close();
dest->setStdFlag(newflags);
dest->setInternalDate(mktime(&mytm));
if (!mailbox->commitNewMessages(depot.mailboxToFilename(canonmailbox))) {
session.setLastError("failed to commit after successful APPEND: "
+ mailbox->getLastError());
return NO;
}
if (mailbox == depot.getSelected()) {
pendingUpdates(mailbox,
PendingUpdates::EXISTS | PendingUpdates::RECENT | PendingUpdates::FLAGS,
true,
false,
true);
}
return OK;
}
Operator::ParseResult AppendOperator::parse(Request &c_in) const
{
Session &session = Session::getInstance();
Operator::ParseResult res;
if (c_in.getUidMode()) return REJECT;
if ((res = expectSPACE()) != ACCEPT) {
session.setLastError("Expected SPACE after APPEND");
return res;
}
string mailbox;
if ((res = expectMailbox(mailbox)) != ACCEPT) {
session.setLastError("Expected mailbox after APPEND SPACE");
return res;
}
c_in.setMailbox(mailbox);
if ((res = expectSPACE()) != ACCEPT) {
session.setLastError("Expected SPACE after APPEND SPACE mailbox");
return res;
}
if ((res = expectThisString("(")) == ACCEPT) {
if ((res = expectFlag(c_in.getFlags())) == ACCEPT) {
while (1) {
if ((res = expectSPACE()) != ACCEPT) break;
if ((res = expectFlag(c_in.getFlags())) != ACCEPT) {
session.setLastError("expected a flag after the '('");
return res;
}
}
}
if ((res = expectThisString(")")) != ACCEPT) {
session.setLastError("expected a ')'");
return res;
}
if ((res = expectSPACE()) != ACCEPT) {
session.setLastError("expected a SPACE after the flag list");
return res;
}
}
string date;
if ((res = expectDateTime(date)) == ACCEPT) {
if ((res = expectSPACE()) != ACCEPT) {
session.setLastError("expected a SPACE after date_time");
return res;
}
}
c_in.setDate(date);
c_in.setName("APPEND");
return ACCEPT;
}
|