1 |
// Windows XP FAQ Poll |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// Poller.cpp |
6 |
|
7 |
#include "Poller.h" |
8 |
|
9 |
Poller::Poller() |
10 |
{ |
11 |
string mailbox = session->list("\"\" \"" + account.getMailbox() + "\""); |
12 |
|
13 |
if (mailbox.find(account.getMailbox()) == string::npos) |
14 |
{ |
15 |
cerr << program << ": Mailbox does not exist: " << account.getMailbox() |
16 |
<< "\n"; |
17 |
|
18 |
exit(1); |
19 |
} |
20 |
} |
21 |
|
22 |
Poller::~Poller() |
23 |
{ |
24 |
if (!nodelete) |
25 |
{ |
26 |
session->expunge(); |
27 |
} |
28 |
} |
29 |
|
30 |
void Poller::poll(bool nodelete, const string& file) |
31 |
{ |
32 |
this->nodelete = nodelete; |
33 |
this->file = file; |
34 |
|
35 |
load(); |
36 |
|
37 |
ballots(); |
38 |
approvals(); |
39 |
|
40 |
save(); |
41 |
} |
42 |
|
43 |
void Poller::load() |
44 |
{ |
45 |
ifstream fin(file.c_str()); |
46 |
if (!fin.is_open()) |
47 |
{ |
48 |
fin.clear(); |
49 |
|
50 |
ofstream fout(file.c_str()); |
51 |
|
52 |
fout << "_find_\n" |
53 |
<< "reply=0\n" |
54 |
<< "news=0\n" |
55 |
<< "sig=0\n" |
56 |
<< "search=0\n" |
57 |
<< "link=0\n" |
58 |
<< "browse=0\n" |
59 |
<< "other=0\n" |
60 |
<< "_help_\n" |
61 |
<< "solved=0\n" |
62 |
<< "note=0\n" |
63 |
<< "link=0\n" |
64 |
<< "news=0\n" |
65 |
<< "lazy=0\n" |
66 |
<< "_improve_\n" |
67 |
<< "nothing=0\n" |
68 |
<< "links=0\n" |
69 |
<< "suggest=0\n"; |
70 |
|
71 |
fout.close(); |
72 |
|
73 |
fin.open(file.c_str()); |
74 |
} |
75 |
|
76 |
while (fin.good()) |
77 |
{ |
78 |
enum { FIND, HELP, IMPROVE } type; |
79 |
string vote; |
80 |
unsigned count; |
81 |
string text; |
82 |
|
83 |
fin.peek() != '_' ? getline(fin, vote, '=') : getline(fin, vote); |
84 |
if (debug) cerr << "vote = " << vote << "\n"; |
85 |
|
86 |
if (vote == "_find_") |
87 |
{ |
88 |
type = FIND; |
89 |
continue; |
90 |
} |
91 |
else if (vote == "_help_") |
92 |
{ |
93 |
type = HELP; |
94 |
continue; |
95 |
} |
96 |
else if (vote == "_improve_") |
97 |
{ |
98 |
type = IMPROVE; |
99 |
continue; |
100 |
} |
101 |
else if (vote == "approved" || vote == "approve") |
102 |
{ |
103 |
getline(fin, text); |
104 |
} |
105 |
else |
106 |
{ |
107 |
fin >> count; |
108 |
fin.get(); |
109 |
} |
110 |
|
111 |
fin.peek(); |
112 |
|
113 |
switch (type) |
114 |
{ |
115 |
case FIND: |
116 |
if (vote == "reply") |
117 |
{ |
118 |
find.reply = count; |
119 |
} |
120 |
else if (vote == "news") |
121 |
{ |
122 |
find.news = count; |
123 |
} |
124 |
else if (vote == "sig") |
125 |
{ |
126 |
find.sig = count; |
127 |
} |
128 |
else if (vote == "search") |
129 |
{ |
130 |
find.search = count; |
131 |
} |
132 |
else if (vote == "link") |
133 |
{ |
134 |
find.link = count; |
135 |
} |
136 |
else if (vote == "browse") |
137 |
{ |
138 |
find.browse = count; |
139 |
} |
140 |
else if (vote == "other") |
141 |
{ |
142 |
find.other = count; |
143 |
} |
144 |
else if (vote == "approved") |
145 |
{ |
146 |
find.approved.push_back(text); |
147 |
} |
148 |
else if (vote == "approve") |
149 |
{ |
150 |
find.approve.push_back(text); |
151 |
} |
152 |
break; |
153 |
case HELP: |
154 |
if (vote == "solved") |
155 |
{ |
156 |
help.solved = count; |
157 |
} |
158 |
else if (vote == "note") |
159 |
{ |
160 |
help.note = count; |
161 |
} |
162 |
else if (vote == "link") |
163 |
{ |
164 |
help.link = count; |
165 |
} |
166 |
else if (vote == "news") |
167 |
{ |
168 |
help.news = count; |
169 |
} |
170 |
else if (vote == "lazy") |
171 |
{ |
172 |
help.lazy = count; |
173 |
} |
174 |
break; |
175 |
case IMPROVE: |
176 |
if (vote == "nothing") |
177 |
{ |
178 |
improve.nothing = count; |
179 |
} |
180 |
else if (vote == "links") |
181 |
{ |
182 |
improve.links = count; |
183 |
} |
184 |
else if (vote == "suggest") |
185 |
{ |
186 |
improve.suggest = count; |
187 |
} |
188 |
else if (vote == "approved") |
189 |
{ |
190 |
improve.approved.push_back(text); |
191 |
} |
192 |
else if (vote == "approve") |
193 |
{ |
194 |
improve.approve.push_back(text); |
195 |
} |
196 |
break; |
197 |
} |
198 |
} |
199 |
|
200 |
fin.close(); |
201 |
} |
202 |
|
203 |
void Poller::save() |
204 |
{ |
205 |
unsigned index; |
206 |
ofstream fout(file.c_str()); |
207 |
|
208 |
fout << "_find_\n" |
209 |
<< "reply=" << find.reply << "\n" |
210 |
<< "news=" << find.news << "\n" |
211 |
<< "sig=" << find.sig << "\n" |
212 |
<< "search=" << find.search << "\n" |
213 |
<< "link=" << find.link << "\n" |
214 |
<< "browse=" << find.browse << "\n" |
215 |
<< "other=" << find.other << "\n"; |
216 |
|
217 |
for (index = 0; index < find.approved.size(); index++) |
218 |
{ |
219 |
fout << "approved=" << find.approved[index] << "\n"; |
220 |
} |
221 |
|
222 |
for (index = 0; index < find.approve.size(); index++) |
223 |
{ |
224 |
fout << "approve=" << find.approve[index] << "\n"; |
225 |
} |
226 |
|
227 |
fout << "_help_\n" |
228 |
<< "solved=" << help.solved << "\n" |
229 |
<< "note=" << help.note << "\n" |
230 |
<< "link=" << help.link << "\n" |
231 |
<< "news=" << help.news << "\n" |
232 |
<< "lazy=" << help.lazy << "\n" |
233 |
<< "_improve_\n" |
234 |
<< "nothing=" << improve.nothing << "\n" |
235 |
<< "links=" << improve.links << "\n" |
236 |
<< "suggest=" << improve.suggest << "\n"; |
237 |
|
238 |
for (index = 0; index < improve.approved.size(); index++) |
239 |
{ |
240 |
fout << "approved=" << improve.approved[index] << "\n"; |
241 |
} |
242 |
|
243 |
for (index = 0; index < improve.approve.size(); index++) |
244 |
{ |
245 |
fout << "approve=" << improve.approve[index] << "\n"; |
246 |
} |
247 |
|
248 |
fout.close(); |
249 |
} |
250 |
|
251 |
void Poller::ballots() |
252 |
{ |
253 |
session->select('\"' + account.getMailbox() + '\"'); |
254 |
|
255 |
stringstream search; |
256 |
search << session->search(string("ALL HEADER X-Mailer \"WinXPFAQPoll 1.0 ") |
257 |
+ "(Perl)\" SUBJECT \"Windows XP FAQ | Poll Ballot\" UNDELETED FROM \"" |
258 |
+ "Windows XP FAQ | Poll\""); |
259 |
|
260 |
search.ignore(9); |
261 |
search.peek(); |
262 |
|
263 |
while (search.good()) |
264 |
{ |
265 |
unsigned message; |
266 |
search >> message; |
267 |
|
268 |
if (debug) cerr << "message = " << message << "\n"; |
269 |
messages.push(message); |
270 |
|
271 |
search.get(); |
272 |
search.peek(); |
273 |
} |
274 |
|
275 |
if (!messages.empty()) |
276 |
{ |
277 |
unsigned message = messages.front(); |
278 |
messages.pop(); |
279 |
|
280 |
ballot(message); |
281 |
} |
282 |
} |
283 |
|
284 |
void Poller::ballot(unsigned message) |
285 |
{ |
286 |
cout << "Checking message: " << message << "..." << flush; |
287 |
|
288 |
ostringstream number; |
289 |
number << message; |
290 |
|
291 |
stringstream buffer; |
292 |
|
293 |
buffer << session->fetch(number.str() + " BODY.PEEK[2]"); |
294 |
|
295 |
if (session->successful()) |
296 |
{ |
297 |
buffer.ignore(string::npos, '\n'); |
298 |
|
299 |
bool approved = session->fetch(number.str() + " FLAGS").find("\\Flagge" |
300 |
+ string("d")) != string::npos; |
301 |
|
302 |
while (buffer.peek() != '\n' && buffer.good()) |
303 |
{ |
304 |
enum { FIND, HELP, IMPROVE } type; |
305 |
string vote; |
306 |
|
307 |
getline(buffer, vote); |
308 |
if (debug) cerr << "vote = " << vote << "\n"; |
309 |
|
310 |
if (vote == "_find_") |
311 |
{ |
312 |
type = FIND; |
313 |
continue; |
314 |
} |
315 |
else if (vote == "_help_") |
316 |
{ |
317 |
type = HELP; |
318 |
continue; |
319 |
} |
320 |
else if (vote == "_improve_") |
321 |
{ |
322 |
type = IMPROVE; |
323 |
continue; |
324 |
} |
325 |
|
326 |
buffer.peek(); |
327 |
|
328 |
switch (type) |
329 |
{ |
330 |
case FIND: |
331 |
if (vote == "reply") |
332 |
{ |
333 |
find.reply++; |
334 |
} |
335 |
else if (vote == "news") |
336 |
{ |
337 |
find.news++; |
338 |
} |
339 |
else if (vote == "sig") |
340 |
{ |
341 |
find.sig++; |
342 |
} |
343 |
else if (vote == "search") |
344 |
{ |
345 |
find.search++; |
346 |
} |
347 |
else if (vote == "link") |
348 |
{ |
349 |
find.link++; |
350 |
} |
351 |
else if (vote == "browse") |
352 |
{ |
353 |
find.browse++; |
354 |
} |
355 |
else if (vote == "other") |
356 |
{ |
357 |
find.other++; |
358 |
|
359 |
string text; |
360 |
getline(buffer, text); |
361 |
|
362 |
if (approved) |
363 |
{ |
364 |
find.approved.push_back(text); |
365 |
} |
366 |
else |
367 |
{ |
368 |
find.approve.push_back(text); |
369 |
|
370 |
submit("find", text); |
371 |
} |
372 |
} |
373 |
break; |
374 |
case HELP: |
375 |
if (vote == "solved") |
376 |
{ |
377 |
help.solved++; |
378 |
} |
379 |
else if (vote == "note") |
380 |
{ |
381 |
help.note++; |
382 |
} |
383 |
else if (vote == "link") |
384 |
{ |
385 |
help.link++; |
386 |
} |
387 |
else if (vote == "news") |
388 |
{ |
389 |
help.news++; |
390 |
} |
391 |
else if (vote == "lazy") |
392 |
{ |
393 |
help.lazy++; |
394 |
} |
395 |
break; |
396 |
case IMPROVE: |
397 |
if (vote == "nothing") |
398 |
{ |
399 |
improve.nothing++; |
400 |
} |
401 |
else if (vote == "links") |
402 |
{ |
403 |
improve.links++; |
404 |
} |
405 |
else if (vote == "suggest") |
406 |
{ |
407 |
improve.suggest++; |
408 |
|
409 |
string text; |
410 |
getline(buffer, text); |
411 |
|
412 |
if (approved) |
413 |
{ |
414 |
improve.approved.push_back(text); |
415 |
} |
416 |
else |
417 |
{ |
418 |
improve.approve.push_back(text); |
419 |
|
420 |
submit("improve", text); |
421 |
} |
422 |
} |
423 |
break; |
424 |
} |
425 |
} |
426 |
|
427 |
session->store(number.str() + " +FLAGS (\\Deleted)"); |
428 |
|
429 |
cout << "done.\n"; |
430 |
} |
431 |
else |
432 |
{ |
433 |
cout << "cancelled.\n"; |
434 |
} |
435 |
|
436 |
if (!messages.empty()) |
437 |
{ |
438 |
unsigned message = messages.front(); |
439 |
messages.pop(); |
440 |
|
441 |
ballot(message); |
442 |
} |
443 |
} |
444 |
|
445 |
void Poller::submit(const string& type, const string& text) |
446 |
{ |
447 |
ostringstream message; |
448 |
|
449 |
message << "From: \"Windows XP FAQ | Poll\" <" << account.getEmail() |
450 |
<< ">\n" |
451 |
<< "To: \"" << account.getName() << "\" <" << account.getEmail() |
452 |
<< ">\n" |
453 |
<< "Subject: Windows XP FAQ | Poll Submit\n" |
454 |
<< "Content-Type: text/plain charset=\"us-ascii\"\n" |
455 |
<< "Content-Transfer-Encoding: 7bit\n" |
456 |
<< "X-Mailer: WinXPFAQPoll 1.0\n" |
457 |
<< "X-WinXPFAQPoll-Submit-Type: " << type << "\n" |
458 |
<< "X-WinXPFAQPoll-Submit-Text: " << text << "\n\n"; |
459 |
|
460 |
if (type == "find") |
461 |
{ |
462 |
message << "How did you find this page?\n" |
463 |
<< " I arrived here by entirely different means:\n"; |
464 |
} |
465 |
else if (type == "improve") |
466 |
{ |
467 |
message << "How could I improve this site?\n" |
468 |
<< " I have my own completely insane suggestion:\n"; |
469 |
} |
470 |
|
471 |
message << " " << text << "\n\n" |
472 |
<< "Flag this message to approve the voter\'s possibly objectionable\n" |
473 |
<< "input or delete it to disapprove.\n\n" |
474 |
<< "This message will be marked deleted when it is processed.\n"; |
475 |
|
476 |
ostringstream length; |
477 |
length << (message.str().length() + 16); |
478 |
|
479 |
session->append('\"' + account.getMailbox() + "\" {" + length.str() + '}', |
480 |
message.str()); |
481 |
|
482 |
session->noop(); |
483 |
} |
484 |
|
485 |
void Poller::approvals() |
486 |
{ |
487 |
// |
488 |
} |
489 |
|
490 |
void Poller::approval(unsigned message) |
491 |
{ |
492 |
// |
493 |
} |