summaryrefslogtreecommitdiff
path: root/src/pendingupdates.cc
blob: 402a75bd2a66c052e4ecdbaa04a81edab6d16a4e (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
/**
 *  @file pendingupdates.cc
 *  @brief  <--->
 *  @author Andreas Aardal Hanssen
 *  @date 2002-2005
 */

#include "pendingupdates.h"

#include "iodevice.h"
#include "iofactory.h"
#include "mailbox.h"
#include "message.h"
#include "session.h"

#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace Binc;

PendingUpdates::PendingUpdates(void) : expunges(), flagupdates()
{
  recent = 0;
  exists = 0;

  newrecent = false;
  newexists = false;
}

PendingUpdates::~PendingUpdates(void) {}

void PendingUpdates::addExpunged(unsigned int uid)
{
  expunges.push_back(uid);
}

void PendingUpdates::addFlagUpdates(unsigned int sqnr,
                                    unsigned int uid,
                                    unsigned int flags,
                                    const vector<string> &cflags)
{
  flagupdates[sqnr] = flags;
  sqnrtouid[sqnr] = uid;
  sqnrtocflags[sqnr] = cflags;
}

void PendingUpdates::setExists(unsigned int n)
{
  exists = n;
  newexists = true;
}

void PendingUpdates::setRecent(unsigned int n)
{
  recent = n;
  newrecent = true;
}

unsigned int PendingUpdates::getExists(void) const
{
  return exists;
}

unsigned int PendingUpdates::getRecent(void) const
{
  return recent;
}

bool PendingUpdates::newExists(void) const
{
  return newexists;
}

bool PendingUpdates::newRecent(void) const
{
  return newrecent;
}

PendingUpdates::expunged_const_iterator::expunged_const_iterator(void) {}

PendingUpdates::expunged_const_iterator::expunged_const_iterator(
    vector<unsigned int>::iterator i)
  : internal(i)
{}

unsigned int PendingUpdates::expunged_const_iterator::operator*(void) const
{
  return *internal;
}

void PendingUpdates::expunged_const_iterator::operator++(void)
{
  ++internal;
}

bool PendingUpdates::expunged_const_iterator::operator==(expunged_const_iterator i) const
{
  return internal == i.internal;
}

bool PendingUpdates::expunged_const_iterator::operator!=(expunged_const_iterator i) const
{
  return internal != i.internal;
}

PendingUpdates::expunged_const_iterator PendingUpdates::beginExpunged(void)
{
  return expunged_const_iterator(expunges.begin());
}

PendingUpdates::expunged_const_iterator PendingUpdates::endExpunged(void)
{
  return expunged_const_iterator(expunges.end());
}

PendingUpdates::flagupdates_const_iterator::flagupdates_const_iterator(void) {}

PendingUpdates::flagupdates_const_iterator::flagupdates_const_iterator(
    map<unsigned int, unsigned int>::iterator i,
    map<unsigned int, vector<string>> *j,
    map<unsigned int, unsigned int> *sqnrmap)
  : internal(i)
  , sqnrtocflags(j)
{
  sqnrtouid = sqnrmap;
}

void PendingUpdates::flagupdates_const_iterator::operator++(void)
{
  ++internal;
}

bool PendingUpdates::flagupdates_const_iterator::operator!=(flagupdates_const_iterator i) const
{
  return internal != i.internal;
}

PendingUpdates::flagupdates_const_iterator PendingUpdates::beginFlagUpdates(void)
{
  return flagupdates_const_iterator(flagupdates.begin(), &sqnrtocflags, &sqnrtouid);
}

PendingUpdates::flagupdates_const_iterator PendingUpdates::endFlagUpdates(void)
{
  return flagupdates_const_iterator(flagupdates.end(), &sqnrtocflags, &sqnrtouid);
}

unsigned int PendingUpdates::flagupdates_const_iterator::first(void) const
{
  return internal->first;
}

unsigned int PendingUpdates::flagupdates_const_iterator::second(void) const
{
  return internal->second;
}

vector<string> PendingUpdates::flagupdates_const_iterator::getCustomFlags(void) const
{
  return (*sqnrtocflags)[internal->first];
}

unsigned int PendingUpdates::flagupdates_const_iterator::getUID(void) const
{
  return (*sqnrtouid)[internal->first];
}

bool Binc::pendingUpdates(Mailbox *mailbox,
                          int type,
                          bool rescan,
                          bool showAll,
                          bool forceScan,
                          bool uidfetchflags)
{
  Session &session = Session::getInstance();

  if (mailbox == nullptr) return true;

  PendingUpdates p;
  if (!mailbox->getUpdates(rescan, type, p, forceScan)) {
    session.setLastError(mailbox->getLastError());
    return false;
  }

  if (type & PendingUpdates::EXPUNGE) {
    PendingUpdates::expunged_const_iterator i = p.beginExpunged();
    PendingUpdates::expunged_const_iterator e = p.endExpunged();

    while (i != e) {
      bincClient << "* " << *i << " EXPUNGE" << endl;
      ++i;
    }
  }

  if (((type & PendingUpdates::EXISTS) && p.newExists()) || showAll)
    bincClient << "* " << p.getExists() << " EXISTS" << endl;

  if (((type & PendingUpdates::RECENT) && p.newRecent()) || showAll)
    bincClient << "* " << p.getRecent() << " RECENT" << endl;

  if (type & PendingUpdates::FLAGS) {
    PendingUpdates::flagupdates_const_iterator i = p.beginFlagUpdates();
    PendingUpdates::flagupdates_const_iterator e = p.endFlagUpdates();

    while (i != e) {
      int flags = i.second();

      vector<string> flagv;
      if (flags & Message::F_SEEN) flagv.push_back("\\Seen");
      if (flags & Message::F_ANSWERED) flagv.push_back("\\Answered");
      if (flags & Message::F_DELETED) flagv.push_back("\\Deleted");
      if (flags & Message::F_DRAFT) flagv.push_back("\\Draft");
      if (flags & Message::F_RECENT) flagv.push_back("\\Recent");
      if (flags & Message::F_FLAGGED) flagv.push_back("\\Flagged");

      bincClient << "* " << i.first() << " FETCH (FLAGS (";
      for (vector<string>::const_iterator k = flagv.begin(); k != flagv.end(); ++k) {
        if (k != flagv.begin()) bincClient << " ";
        bincClient << *k;
      }

      vector<string> customFlags = i.getCustomFlags();
      for (auto it = customFlags.cbegin(); it != customFlags.cend(); ++it) {
        if (flagv.size() > 0 || it != customFlags.cbegin()) bincClient << " ";
        bincClient << *it;
      }

      bincClient << ")";
      if (uidfetchflags) bincClient << " UID " << i.getUID();
      bincClient << ")" << endl;
      ++i;
    }
  }

  return true;
}