summaryrefslogtreecommitdiff
path: root/src/imapparser.cc
blob: fc2e071e86847e14dd057b38bd3cc10d2f4523a5 (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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/** --------------------------------------------------------------------
 *  @file imapparser.cc
 *  @brief Implementation of the common items for parsing IMAP input
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 *  -----------------------------------------------------------------  **/
#include "imapparser.h"

#include "convert.h"

#include <exception>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include <stdio.h>

using namespace ::std;
using namespace Binc;

//------------------------------------------------------------------------
Request::Request(void) : extra(0), flags(), statuses(), bset(), searchkey(), fatt()
{
  uidmode = false;
}

Request::~Request(void)
{
  if (extra != 0) delete extra;
}

//------------------------------------------------------------------------
void Request::setUidMode(void)
{
  uidmode = true;
}

//------------------------------------------------------------------------
bool Request::getUidMode(void) const
{
  return uidmode;
}

//------------------------------------------------------------------------
void Request::setTag(string &t_in)
{
  tag = t_in;
}

//------------------------------------------------------------------------
const string &Request::getTag(void) const
{
  return tag;
}

//------------------------------------------------------------------------
void Request::setMode(const string &m_in)
{
  mode = m_in;
}

//------------------------------------------------------------------------
const string &Request::getMode(void) const
{
  return mode;
}

//------------------------------------------------------------------------
void Request::setName(const string &s_in)
{
  name = s_in;
}

//------------------------------------------------------------------------
const string &Request::getName(void) const
{
  return name;
}

//------------------------------------------------------------------------
void Request::setAuthType(const string &s_in)
{
  authtype = s_in;
}

//------------------------------------------------------------------------
const string &Request::getAuthType(void) const
{
  return authtype;
}

//------------------------------------------------------------------------
void Request::setLiteral(const string &s_in)
{
  literal = s_in;
}

//------------------------------------------------------------------------
const string &Request::getLiteral(void) const
{
  return literal;
}

//------------------------------------------------------------------------
void Request::setDate(const string &s_in)
{
  date = s_in;
}

//------------------------------------------------------------------------
const string &Request::getDate(void) const
{
  return date;
}

//------------------------------------------------------------------------
void Request::setCharSet(const string &s_in)
{
  charset = s_in;
  uppercase(charset);
}

//------------------------------------------------------------------------
const string &Request::getCharSet(void) const
{
  return charset;
}

//------------------------------------------------------------------------
void Request::setUserID(const string &s_in)
{
  userid = s_in;
}

//------------------------------------------------------------------------
const string &Request::getUserID(void) const
{
  return userid;
}

//------------------------------------------------------------------------
void Request::setPassword(const string &s_in)
{
  password = s_in;
}

//------------------------------------------------------------------------
const string &Request::getPassword(void) const
{
  return password;
}

//------------------------------------------------------------------------
void Request::setMailbox(const string &s_in)
{
  mailbox = s_in;
}

//------------------------------------------------------------------------
const string &Request::getMailbox(void) const
{
  return mailbox;
}

//------------------------------------------------------------------------
void Request::setListMailbox(const string &s_in)
{
  listmailbox = s_in;
}

//------------------------------------------------------------------------
const string &Request::getListMailbox(void) const
{
  return listmailbox;
}

//------------------------------------------------------------------------
void Request::setContextInfo(const string &s_in)
{
  contextInfo = s_in;
}

//------------------------------------------------------------------------
const string &Request::getContextInfo(void) const
{
  return contextInfo;
}

//------------------------------------------------------------------------
void Request::setNewMailbox(const string &s_in)
{
  newmailbox = s_in;
}

//------------------------------------------------------------------------
const string &Request::getNewMailbox(void) const
{
  return newmailbox;
}

//------------------------------------------------------------------------
SequenceSet &Request::getSet(void)
{
  return bset;
}

//------------------------------------------------------------------------
vector<string> &Request::getStatuses(void)
{
  return statuses;
}

//------------------------------------------------------------------------
vector<string> &Request::getFlags(void)
{
  return flags;
}

//------------------------------------------------------------------------
SequenceSet::SequenceSet(void) : limited(true), nullSet(false) {}

//------------------------------------------------------------------------
SequenceSet::SequenceSet(const SequenceSet &copy)
  : limited(copy.limited)
  , nullSet(copy.nullSet)
  , internal(copy.internal)
{}

//------------------------------------------------------------------------
SequenceSet &SequenceSet::operator=(const SequenceSet &copy)
{
  limited = copy.limited;
  nullSet = copy.nullSet;
  internal = copy.internal;

  return *this;
}

//------------------------------------------------------------------------
SequenceSet::~SequenceSet(void) {}

//------------------------------------------------------------------------
SequenceSet &SequenceSet::null(void)
{
  static SequenceSet nil;
  nil.nullSet = true;
  return nil;
}

//------------------------------------------------------------------------
bool SequenceSet::isNull(void) const
{
  return nullSet;
}

//------------------------------------------------------------------------
SequenceSet &SequenceSet::all(void)
{
  static bool initialized = false;
  static SequenceSet all;

  if (!initialized) {
    all.addRange(1, (unsigned int)-1);
    initialized = true;
  }

  return all;
}

//------------------------------------------------------------------------
SequenceSet::Range::Range(unsigned int a, unsigned int b)
{
  if (a > b) {
    from = b;
    to = a;
  } else {
    from = a;
    to = b;
  }
}

//------------------------------------------------------------------------
void SequenceSet::addRange(unsigned int a, unsigned int b)
{
  if (a == (unsigned int)-1 || b == (unsigned int)-1) limited = false;
  internal.push_back(Range(a, b));
}

//------------------------------------------------------------------------
void SequenceSet::addNumber(unsigned int a)
{
  if (a == (unsigned int)-1) limited = false;
  internal.push_back(Range(a, a));
}

//------------------------------------------------------------------------
bool SequenceSet::isInSet(unsigned int n) const
{
  unsigned int maxvalue = 0;
  for (vector<Range>::const_iterator i = internal.begin(); i != internal.end(); ++i) {
    const Range &r = *i;
    if (r.from > maxvalue)
      maxvalue = r.from;
    else if (r.to > maxvalue)
      maxvalue = r.to;

    if (n >= (*i).from && n <= (*i).to) {
      return true;
    }
  }

  return (n > maxvalue && !limited);
}

//------------------------------------------------------------------------
BincImapParserFetchAtt::BincImapParserFetchAtt(const std::string &typeName) : type(typeName)
{
  offsetstart = 0;
  offsetlength = (unsigned int)-1;
  hassection = false;
}

//------------------------------------------------------------------------
string BincImapParserFetchAtt::toString(void)
{
  string tmp;
  if (type == "BODY.PEEK")
    tmp = "BODY";
  else
    tmp = type;

  if (type == "BODY" || type == "BODY.PEEK") {
    if (hassection) {
      tmp += "[";
      tmp += section;
      if (sectiontext != "") {
        if (section != "") tmp += ".";
        tmp += sectiontext;

        if (headerlist.size() > 0) {
          tmp += " (";
          for (vector<string>::iterator i = headerlist.begin(); i != headerlist.end(); ++i) {
            if (i != headerlist.begin()) tmp += " ";
            tmp += Binc::toImapString(*i);
          }
          tmp += ")";
        }
      }
      tmp += "]";

      if (offsetstart == 0 && offsetlength == (unsigned int)-1)
        tmp += " ";
      else
        tmp += "<" + Binc::toString(offsetstart) + "> ";
    }
  }

  return tmp;
}

//------------------------------------------------------------------------
BincImapParserSearchKey::BincImapParserSearchKey(void)
{
  type = 0;
  number = 0;
}

//------------------------------------------------------------------------
const SequenceSet &BincImapParserSearchKey::getSet(void) const
{
  return bset;
}