Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
maildir-readcache.cc
Go to the documentation of this file.
1
7#include <algorithm>
8#include "maildir.h"
9#include "convert.h"
10#include "globals.h"
11
12using namespace ::std;
13using namespace Binc;
14
15//------------------------------------------------------------------------
17{
18 index.clearUids();
19
20 const string cachefilename = path + "/bincimap-cache";
21 FILE *fp = fopen(cachefilename.c_str(), "r");
22 if (!fp) {
23 uidvalidity = time(0);
24 uidnext = 1;
25 messages.clear();
26 index.clear();
27 newMessages.clear();
28 return NoCache;
29 }
30
31 char inputBuffer[512];
32 if (!fgets(inputBuffer, sizeof(inputBuffer), fp)) {
33 fclose(fp);
34 uidvalidity = time(0);
35 uidnext = 1;
36 messages.clear();
37 index.clear();
38 newMessages.clear();
39 return NoCache;
40 }
41
42 // terminate the buffer
43 inputBuffer[sizeof(inputBuffer) - 1] = '\0';
44
45 char cacheFileVersionBuffer[512];
46 unsigned int readUidValidity;
47 unsigned int readUidNext;
48
49 if (sscanf(inputBuffer, "%s %u %u", cacheFileVersionBuffer, &readUidValidity, &readUidNext) != 3
50 || strcmp(cacheFileVersionBuffer, BINC_CACHE) != 0) {
51 // bump cache
52 fclose(fp);
53 uidvalidity = time(0);
54 uidnext = 1;
55 messages.clear();
56 index.clear();
57 newMessages.clear();
58 return NoCache;
59 }
60
61 uidnext = readUidNext;
62 uidvalidity = readUidValidity;
63
64 unsigned int readUID;
65 unsigned int readSize;
66 unsigned int readInternalDate;
67 char readUnique[512];
68
69 while (fgets(inputBuffer, sizeof(inputBuffer), fp)) {
70 inputBuffer[sizeof(inputBuffer) - 1] = '\0';
71 if (sscanf(inputBuffer, "%u %u %u %s", &readUID,
72 &readInternalDate, &readSize, readUnique) != 4) {
73 // error in input
74 fclose(fp);
75 uidvalidity = time(0);
76 uidnext = 1;
77 messages.clear();
78 index.clear();
79 newMessages.clear();
80 return NoCache;
81 }
82
83 vector<string> customFlags;
84
85 char *flagStart = inputBuffer;
86 for (int i = 0; flagStart != 0 && *flagStart != '\0' && i < 4; ++i) {
87 flagStart = strchr(flagStart, ' ');
88
89 // skip consecutive white space
90 while (flagStart && *flagStart == ' ')
91 ++flagStart;
92 }
93
94 // get flags
95 while (flagStart) {
96 char *lastOffset = flagStart;
97 flagStart = strchr(flagStart, ' ');
98 if (flagStart) {
99 *flagStart = '\0';
100 ++flagStart;
101 }
102
103 customFlags.push_back(lastOffset);
104
105 // skip consecutive white space
106 while (flagStart && *flagStart != '\0' && *flagStart == ' ')
107 ++flagStart;
108 }
109
110 MaildirMessage m(*this);
111 m.setUnique(readUnique);
112 m.setInternalDate(readInternalDate);
113
114 if (index.find(readUnique) == 0) {
115 for (vector<string>::const_iterator it = customFlags.begin();
116 it != customFlags.end(); ++it) {
117 string tmpFlag = *it;
118 trim(tmpFlag, " \n");
119 m.setCustomFlag(tmpFlag);
120 }
121
122 m.setUID(readUID);
124 m.setSize(readSize);
125 add(m);
126 } else {
127 // Remember to insert the uid of the message again - we reset this
128 // at the top of this function.
129 index.insert(readUnique, readUID);
130 MaildirMessage &existingMessage = messages.find(readUID)->second;
131
132 vector<string> oldCustomFlags = existingMessage.getCustomFlags();
133 sort(oldCustomFlags.begin(), oldCustomFlags.end());
134 sort(customFlags.begin(), customFlags.end());
135 if (oldCustomFlags != customFlags) {
136 existingMessage.resetCustomFlags();
137 for (vector<string>::const_iterator it = customFlags.begin();
138 it != customFlags.end(); ++it) {
139 string tmpFlag = *it;
140 trim(tmpFlag, " \n");
141 existingMessage.setCustomFlag(tmpFlag);
142 }
143 }
144 }
145 }
146
147 fclose(fp);
148
149 return Ok;
150}
151
void add(MaildirMessage &m)
Definition: maildir.cc:770
ReadCacheResult readCache(void)
void insert(const std::string &unique, unsigned int uid, const std::string &fileName="")
Definition: maildir.cc:786
void clearUids(void)
Definition: maildir.cc:826
MaildirIndexItem * find(const std::string &unique)
Definition: maildir.cc:810
void clear(void)
Definition: maildir.cc:820
The MaildirMessage class provides an interface for IMAP messages.
void setUnique(const std::string &s_in)
void setInternalDate(time_t internaldate)
std::vector< std::string > getCustomFlags(void) const
void setUID(unsigned int uid)
void setCustomFlag(const std::string &flag)
void setSize(unsigned int size)
void setInternalFlag(unsigned char flags)
Declaration of miscellaneous convertion functions.
Global constants.
#define BINC_CACHE
Definition: globals.h:12
Declaration of the Maildir class.
Definition: bincimapd.cc:9
void trim(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:137
std::stack< int > inputBuffer