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
|
/**
* @file maildir-readcache.cc
* @brief Implementation of the Maildir class.
* @author Andreas Aardal Hanssen
* @date 2002-2005
*/
#include "convert.h"
#include "globals.h"
#include "maildir.h"
#include <algorithm>
using namespace Binc;
using std::string;
using std::vector;
Maildir::ReadCacheResult Maildir::readCache(void)
{
index.clearUids();
const string cachefilename = path + "/bincimap-cache";
FILE *fp = fopen(cachefilename.c_str(), "r");
if (!fp) {
uidvalidity = time(nullptr);
uidnext = 1;
messages.clear();
index.clear();
newMessages.clear();
return NoCache;
}
char inputBuffer[512];
if (!fgets(inputBuffer, sizeof(inputBuffer), fp)) {
fclose(fp);
uidvalidity = time(nullptr);
uidnext = 1;
messages.clear();
index.clear();
newMessages.clear();
return NoCache;
}
// terminate the buffer
inputBuffer[sizeof(inputBuffer) - 1] = '\0';
char cacheFileVersionBuffer[512];
unsigned int readUidValidity;
unsigned int readUidNext;
if (sscanf(inputBuffer, "%s %u %u", cacheFileVersionBuffer, &readUidValidity, &readUidNext) != 3
|| strcmp(cacheFileVersionBuffer, BINC_CACHE) != 0)
{
// bump cache
fclose(fp);
uidvalidity = time(nullptr);
uidnext = 1;
messages.clear();
index.clear();
newMessages.clear();
return NoCache;
}
uidnext = readUidNext;
uidvalidity = readUidValidity;
unsigned int readUID;
unsigned int readSize;
unsigned int readInternalDate;
char readUnique[512];
while (fgets(inputBuffer, sizeof(inputBuffer), fp)) {
inputBuffer[sizeof(inputBuffer) - 1] = '\0';
if (sscanf(inputBuffer, "%u %u %u %s", &readUID, &readInternalDate, &readSize, readUnique) != 4) {
// error in input
fclose(fp);
uidvalidity = time(nullptr);
uidnext = 1;
messages.clear();
index.clear();
newMessages.clear();
return NoCache;
}
vector<string> customFlags;
char *flagStart = inputBuffer;
for (int i = 0; flagStart != nullptr && *flagStart != '\0' && i < 4; ++i) {
flagStart = strchr(flagStart, ' ');
// skip consecutive white space
while (flagStart && *flagStart == ' ')
++flagStart;
}
// get flags
while (flagStart) {
char *lastOffset = flagStart;
flagStart = strchr(flagStart, ' ');
if (flagStart) {
*flagStart = '\0';
++flagStart;
}
customFlags.push_back(lastOffset);
// skip consecutive white space
while (flagStart && *flagStart != '\0' && *flagStart == ' ')
++flagStart;
}
MaildirMessage m(*this);
m.setUnique(readUnique);
m.setInternalDate(readInternalDate);
if (index.find(readUnique) == nullptr) {
for (auto tmpFlag : customFlags) {
trim(tmpFlag, " \n");
m.setCustomFlag(tmpFlag);
}
m.setUID(readUID);
m.setInternalFlag(MaildirMessage::JustArrived);
m.setSize(readSize);
add(m);
} else {
// Remember to insert the uid of the message again - we reset this
// at the top of this function.
index.insert(readUnique, readUID);
MaildirMessage &existingMessage = messages.find(readUID)->second;
vector<string> oldCustomFlags = existingMessage.getCustomFlags();
sort(oldCustomFlags.begin(), oldCustomFlags.end());
sort(customFlags.begin(), customFlags.end());
if (oldCustomFlags != customFlags) {
existingMessage.resetCustomFlags();
for (auto tmpFlag : customFlags) {
trim(tmpFlag, " \n");
existingMessage.setCustomFlag(tmpFlag);
}
}
}
}
fclose(fp);
return Ok;
}
|