22 |
|
void Poller::poll(bool nodelete, const string& file) |
23 |
|
{ |
24 |
|
this->nodelete = nodelete; |
25 |
+ |
this->file = file; |
26 |
+ |
|
27 |
+ |
load(); |
28 |
+ |
|
29 |
+ |
ballots(); |
30 |
+ |
approvals(); |
31 |
+ |
|
32 |
+ |
save(); |
33 |
+ |
} |
34 |
+ |
|
35 |
+ |
void Poller::load() |
36 |
+ |
{ |
37 |
+ |
ifstream fin(file.c_str()); |
38 |
+ |
if (!fin.is_open()) |
39 |
+ |
{ |
40 |
+ |
ofstream fout(file.c_str()); |
41 |
+ |
|
42 |
+ |
fout << "_find_\n" |
43 |
+ |
<< "reply=0\n" |
44 |
+ |
<< "news=0\n" |
45 |
+ |
<< "sig=0\n" |
46 |
+ |
<< "search=0\n" |
47 |
+ |
<< "link=0\n" |
48 |
+ |
<< "browse=0\n" |
49 |
+ |
<< "other=0\n" |
50 |
+ |
<< "_help_\n" |
51 |
+ |
<< "solved=0\n" |
52 |
+ |
<< "note=0\n" |
53 |
+ |
<< "link=0\n" |
54 |
+ |
<< "news=0\n" |
55 |
+ |
<< "lazy=0\n" |
56 |
+ |
<< "_improve_\n" |
57 |
+ |
<< "nothing=0\n" |
58 |
+ |
<< "links=0\n" |
59 |
+ |
<< "suggest=0\n"; |
60 |
+ |
|
61 |
+ |
fout.close(); |
62 |
+ |
|
63 |
+ |
fin.open(file.c_str()); |
64 |
+ |
} |
65 |
+ |
|
66 |
+ |
do |
67 |
+ |
{ |
68 |
+ |
enum { FIND, HELP, IMPROVE } type; |
69 |
+ |
string vote; |
70 |
+ |
unsigned count; |
71 |
+ |
string text; |
72 |
+ |
|
73 |
+ |
getline(fin, vote, '='); |
74 |
+ |
if (debug) cerr << "vote = " << vote << "\n"; |
75 |
+ |
|
76 |
+ |
if (vote == "_find_") |
77 |
+ |
{ |
78 |
+ |
type = FIND; |
79 |
+ |
continue; |
80 |
+ |
} |
81 |
+ |
else if (vote == "_help_") |
82 |
+ |
{ |
83 |
+ |
type = HELP; |
84 |
+ |
continue; |
85 |
+ |
} |
86 |
+ |
else if (vote == "_improve_") |
87 |
+ |
{ |
88 |
+ |
type = IMPROVE; |
89 |
+ |
continue; |
90 |
+ |
} |
91 |
+ |
else if (vote == "approved" || vote == "approve") |
92 |
+ |
{ |
93 |
+ |
getline(fin, text); |
94 |
+ |
} |
95 |
+ |
else |
96 |
+ |
{ |
97 |
+ |
fin >> count; |
98 |
+ |
fin.get(); |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
switch (type) |
102 |
+ |
{ |
103 |
+ |
case FIND: |
104 |
+ |
if (vote == "reply") |
105 |
+ |
{ |
106 |
+ |
find.reply = count; |
107 |
+ |
} |
108 |
+ |
else if (vote == "news") |
109 |
+ |
{ |
110 |
+ |
find.news = count; |
111 |
+ |
} |
112 |
+ |
else if (vote == "sig") |
113 |
+ |
{ |
114 |
+ |
find.sig = count; |
115 |
+ |
} |
116 |
+ |
else if (vote == "search") |
117 |
+ |
{ |
118 |
+ |
find.search = count; |
119 |
+ |
} |
120 |
+ |
else if (vote == "link") |
121 |
+ |
{ |
122 |
+ |
find.link = count; |
123 |
+ |
} |
124 |
+ |
else if (vote == "browse") |
125 |
+ |
{ |
126 |
+ |
find.browse = count; |
127 |
+ |
} |
128 |
+ |
else if (vote == "other") |
129 |
+ |
{ |
130 |
+ |
find.other = count; |
131 |
+ |
} |
132 |
+ |
else if (vote == "approved") |
133 |
+ |
{ |
134 |
+ |
find.approved.push_back(text); |
135 |
+ |
} |
136 |
+ |
else if (vote == "approve") |
137 |
+ |
{ |
138 |
+ |
find.approve.push_back(text); |
139 |
+ |
} |
140 |
+ |
break; |
141 |
+ |
case HELP: |
142 |
+ |
if (vote == "solved") |
143 |
+ |
{ |
144 |
+ |
help.solved = count; |
145 |
+ |
} |
146 |
+ |
else if (vote == "note") |
147 |
+ |
{ |
148 |
+ |
help.note = count; |
149 |
+ |
} |
150 |
+ |
else if (vote == "link") |
151 |
+ |
{ |
152 |
+ |
help.link = count; |
153 |
+ |
} |
154 |
+ |
else if (vote == "news") |
155 |
+ |
{ |
156 |
+ |
help.news = count; |
157 |
+ |
} |
158 |
+ |
else if (vote == "lazy") |
159 |
+ |
{ |
160 |
+ |
help.lazy = count; |
161 |
+ |
} |
162 |
+ |
break; |
163 |
+ |
case IMPROVE: |
164 |
+ |
if (vote == "nothing") |
165 |
+ |
{ |
166 |
+ |
improve.nothing = count; |
167 |
+ |
} |
168 |
+ |
else if (vote == "links") |
169 |
+ |
{ |
170 |
+ |
improve.links = count; |
171 |
+ |
} |
172 |
+ |
else if (vote == "suggest") |
173 |
+ |
{ |
174 |
+ |
improve.suggest = count; |
175 |
+ |
} |
176 |
+ |
else if (vote == "approved") |
177 |
+ |
{ |
178 |
+ |
improve.approved.push_back(text); |
179 |
+ |
} |
180 |
+ |
else if (vote == "approve") |
181 |
+ |
{ |
182 |
+ |
improve.approve.push_back(text); |
183 |
+ |
} |
184 |
+ |
break; |
185 |
+ |
} |
186 |
+ |
} |
187 |
+ |
while (fin.good()); |
188 |
+ |
|
189 |
+ |
fin.close(); |
190 |
+ |
} |
191 |
+ |
|
192 |
+ |
void Poller::save() |
193 |
+ |
{ |
194 |
+ |
unsigned index; |
195 |
+ |
ofstream fout(file.c_str()); |
196 |
+ |
|
197 |
+ |
fout << "_find_\n" |
198 |
+ |
<< "reply=" << find.reply << "\n" |
199 |
+ |
<< "news=" << find.news << "\n" |
200 |
+ |
<< "sig=" << find.sig << "\n" |
201 |
+ |
<< "search=" << find.search << "\n" |
202 |
+ |
<< "link=" << find.link << "\n" |
203 |
+ |
<< "browse=" << find.browse << "\n" |
204 |
+ |
<< "other=" << find.other << "\n"; |
205 |
+ |
|
206 |
+ |
for (index = 0; index < find.approved.size(); index++) |
207 |
+ |
{ |
208 |
+ |
fout << "approved=" << find.approved[index] << "\n"; |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
for (index = 0; index < find.approve.size(); index++) |
212 |
+ |
{ |
213 |
+ |
fout << "approve=" << find.approve[index] << "\n"; |
214 |
+ |
} |
215 |
+ |
|
216 |
+ |
fout << "_help_\n" |
217 |
+ |
<< "solved=" << help.solved << "\n" |
218 |
+ |
<< "note=" << help.note << "\n" |
219 |
+ |
<< "link=" << help.link << "\n" |
220 |
+ |
<< "news=" << help.news << "\n" |
221 |
+ |
<< "lazy=" << help.lazy << "\n" |
222 |
+ |
<< "_improve_\n" |
223 |
+ |
<< "nothing=" << improve.nothing << "\n" |
224 |
+ |
<< "links=" << improve.links << "\n" |
225 |
+ |
<< "suggest=" << improve.suggest << "\n"; |
226 |
+ |
|
227 |
+ |
for (index = 0; index < improve.approved.size(); index++) |
228 |
+ |
{ |
229 |
+ |
fout << "approved=" << improve.approved[index] << "\n"; |
230 |
+ |
} |
231 |
+ |
|
232 |
+ |
for (index = 0; index < improve.approve.size(); index++) |
233 |
+ |
{ |
234 |
+ |
fout << "approve=" << improve.approve[index] << "\n"; |
235 |
+ |
} |
236 |
+ |
|
237 |
+ |
fout.close(); |
238 |
+ |
} |
239 |
+ |
|
240 |
+ |
void Poller::ballots() |
241 |
+ |
{ |
242 |
+ |
session->select('\"' + account.getMailbox() + '\"'); |
243 |
+ |
|
244 |
+ |
stringstream search; |
245 |
+ |
search << session->search(string("ALL HEADER X-Mailer \"WinXPFAQPoll 1.0 ") |
246 |
+ |
+ "(Perl)\" SUBJECT \"Windows XP FAQ | Poll Ballot\" UNDELETED FROM \"" |
247 |
+ |
+ "Windows XP FAQ | Poll\" HEADER \"X-WinXPFAQPoll-Post-Host\" * HEADE" |
248 |
+ |
+ "R \"X-WinXPFAQPoll-Post-Agent\" *"); |
249 |
+ |
|
250 |
+ |
string junk; |
251 |
+ |
|
252 |
+ |
getline(search, junk, ' '); |
253 |
+ |
getline(search, junk, ' '); |
254 |
+ |
|
255 |
+ |
while (search.good()) |
256 |
+ |
{ |
257 |
+ |
unsigned message; |
258 |
+ |
search >> message; |
259 |
+ |
search.get(); |
260 |
+ |
|
261 |
+ |
if (debug) cerr << "message = " << message << "\n"; |
262 |
+ |
messages.push(message); |
263 |
+ |
} |
264 |
+ |
|
265 |
+ |
if (!messages.empty()) |
266 |
+ |
{ |
267 |
+ |
unsigned message = messages.front(); |
268 |
+ |
messages.pop(); |
269 |
+ |
|
270 |
+ |
ballot(message); |
271 |
+ |
} |
272 |
+ |
|
273 |
+ |
if (!nodelete) |
274 |
+ |
{ |
275 |
+ |
session->expunge(); |
276 |
+ |
} |
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
void Poller::ballot(unsigned message) |
280 |
+ |
{ |
281 |
+ |
cout << "Checking message: " << message << "..." << flush; |
282 |
+ |
|
283 |
+ |
char number[32 + 1]; |
284 |
+ |
sprintf(number, "%u", message); |
285 |
+ |
|
286 |
+ |
stringstream buffer; |
287 |
+ |
|
288 |
+ |
buffer << session->fetch(string(number) + " BODY.PEEK[2]"); |
289 |
+ |
|
290 |
+ |
if (session->successful()) |
291 |
+ |
{ |
292 |
+ |
bool approved = session->fetch(string(number) + " FLAGS").find("\\Flag" |
293 |
+ |
+ string("ged")) != string::npos; |
294 |
+ |
|
295 |
+ |
do |
296 |
+ |
{ |
297 |
+ |
enum { FIND, HELP, IMPROVE } type; |
298 |
+ |
string vote; |
299 |
+ |
|
300 |
+ |
getline(buffer, vote); |
301 |
+ |
if (debug) cerr << "vote = " << vote << "\n"; |
302 |
+ |
|
303 |
+ |
if (vote == "_find_") |
304 |
+ |
{ |
305 |
+ |
type = FIND; |
306 |
+ |
continue; |
307 |
+ |
} |
308 |
+ |
else if (vote == "_help_") |
309 |
+ |
{ |
310 |
+ |
type = HELP; |
311 |
+ |
continue; |
312 |
+ |
} |
313 |
+ |
else if (vote == "_improve_") |
314 |
+ |
{ |
315 |
+ |
type = IMPROVE; |
316 |
+ |
continue; |
317 |
+ |
} |
318 |
+ |
|
319 |
+ |
switch (type) |
320 |
+ |
{ |
321 |
+ |
case FIND: |
322 |
+ |
if (vote == "reply") |
323 |
+ |
{ |
324 |
+ |
find.reply++; |
325 |
+ |
} |
326 |
+ |
else if (vote == "news") |
327 |
+ |
{ |
328 |
+ |
find.news++; |
329 |
+ |
} |
330 |
+ |
else if (vote == "sig") |
331 |
+ |
{ |
332 |
+ |
find.sig++; |
333 |
+ |
} |
334 |
+ |
else if (vote == "search") |
335 |
+ |
{ |
336 |
+ |
find.search++; |
337 |
+ |
} |
338 |
+ |
else if (vote == "link") |
339 |
+ |
{ |
340 |
+ |
find.link++; |
341 |
+ |
} |
342 |
+ |
else if (vote == "browse") |
343 |
+ |
{ |
344 |
+ |
find.browse++; |
345 |
+ |
} |
346 |
+ |
else if (vote == "other") |
347 |
+ |
{ |
348 |
+ |
find.other++; |
349 |
+ |
|
350 |
+ |
// |
351 |
+ |
} |
352 |
+ |
break; |
353 |
+ |
case HELP: |
354 |
+ |
if (vote == "solved") |
355 |
+ |
{ |
356 |
+ |
help.solved++; |
357 |
+ |
} |
358 |
+ |
else if (vote == "note") |
359 |
+ |
{ |
360 |
+ |
help.note++; |
361 |
+ |
} |
362 |
+ |
else if (vote == "link") |
363 |
+ |
{ |
364 |
+ |
help.link++; |
365 |
+ |
} |
366 |
+ |
else if (vote == "news") |
367 |
+ |
{ |
368 |
+ |
help.news++; |
369 |
+ |
} |
370 |
+ |
else if (vote == "lazy") |
371 |
+ |
{ |
372 |
+ |
help.lazy++; |
373 |
+ |
} |
374 |
+ |
break; |
375 |
+ |
case IMPROVE: |
376 |
+ |
if (vote == "nothing") |
377 |
+ |
{ |
378 |
+ |
improve.nothing++; |
379 |
+ |
} |
380 |
+ |
else if (vote == "links") |
381 |
+ |
{ |
382 |
+ |
improve.links++; |
383 |
+ |
} |
384 |
+ |
else if (vote == "suggest") |
385 |
+ |
{ |
386 |
+ |
improve.suggest++; |
387 |
+ |
|
388 |
+ |
// |
389 |
+ |
} |
390 |
+ |
break; |
391 |
+ |
} |
392 |
+ |
} |
393 |
+ |
while (buffer.good()); |
394 |
+ |
|
395 |
+ |
cout << "done.\n"; |
396 |
+ |
} |
397 |
+ |
else |
398 |
+ |
{ |
399 |
+ |
cout << "cancelled.\n"; |
400 |
+ |
} |
401 |
+ |
|
402 |
+ |
if (!messages.empty()) |
403 |
+ |
{ |
404 |
+ |
unsigned message = messages.front(); |
405 |
+ |
messages.pop(); |
406 |
+ |
|
407 |
+ |
ballot(message); |
408 |
+ |
} |
409 |
+ |
} |
410 |
+ |
|
411 |
+ |
void Poller::approvals() |
412 |
+ |
{ |
413 |
+ |
// |
414 |
+ |
|
415 |
+ |
if (!nodelete) |
416 |
+ |
{ |
417 |
+ |
session->expunge(); |
418 |
+ |
} |
419 |
|
} |