forked from jevolk/SPQF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voting.cpp
773 lines (623 loc) · 17.9 KB
/
voting.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
// libircbot irc::bot::
#include "ircbot/bot.h"
using namespace irc::bot;
// SPQF
#include "log.h"
#include "vote.h"
#include "votes.h"
#include "vdb.h"
#include "praetor.h"
#include "voting.h"
Voting::Voting(Bot &bot,
Vdb &vdb,
Praetor &praetor):
bot(bot),
vdb(vdb),
praetor(praetor),
interrupted(false),
initialized(false),
poll_thread(&Voting::poll_worker,this),
remind_thread(&Voting::remind_worker,this),
eligible_thread(&Voting::eligible_worker,this)
{
}
Voting::~Voting() noexcept
{
interrupted.store(true,std::memory_order_release);
sem.notify_all();
eligible_thread.join();
remind_thread.join();
poll_thread.join();
}
void Voting::cancel(const id_t &id,
const Chan &chan,
const User &user)
{
Vote &vote(get(id));
cancel(vote,chan,user);
}
void Voting::cancel(Vote &vote,
const Chan &chan,
const User &user)
{
if(user.get_acct() != vote.get_user_acct())
throw Exception() << "You can't cancel a vote by " << vote.get_user_acct() << ".";
if(vote.total() > 1)
throw Exception("You can't cancel after someone else has voted.");
const auto &cfg(vote.get_cfg());
if(!cfg.get("cancel",true))
throw Exception("You can't cancel votes of this type.");
vote.cancel();
del(vote.get_id());
}
void Voting::valid_motion(const Vote &vote)
{
const auto &cfg(vote.get_cfg());
const auto &user(vote.get_user());
const auto &chan(vote.get_chan());
valid_limits(vote,chan,user);
if(!speaker(cfg,chan,user))
throw Exception("You are not able to create votes on this channel.");
if(!enfranchised(cfg,chan,user))
throw Exception("You are not yet enfranchised in this channel.");
if(!qualified(cfg,chan,user))
throw Exception("You have not been participating enough to start a vote.");
const auto now(time(nullptr));
if(cfg.has("limit.motion"))
{
const auto limit(secs_cast(cfg["limit.motion"]));
const Vdb::Terms query
{
Vdb::Term { "ended", ">=", lex_cast(now - limit) },
Vdb::Term { "issue", "==", vote.get_issue() },
Vdb::Term { "type", "==", vote.get_type() },
Vdb::Term { "chan", "==", chan.get_name() },
};
if(!vdb.query(query,1).empty())
throw Exception("This vote was made within the last ") << secs_cast(limit) << ". Try again later.";
}
const Adoc reasons(cfg.get_child("limit.reason",Adoc{}));
reasons.for_each([this,&chan,&vote,&now]
(const std::string &reason, const std::string &limit_str)
{
if(reason.size() > 64 || !isalpha(reason))
throw Exception("Malformed reason key given in limit.reason");
const auto limit(secs_cast(limit_str));
const Vdb::Terms query
{
Vdb::Term { "reason", "==", reason },
Vdb::Term { "ended", ">=", lex_cast(now - limit) },
Vdb::Term { "issue", "==", vote.get_issue() },
Vdb::Term { "type", "==", vote.get_type() },
Vdb::Term { "chan", "==", chan.get_name() },
};
if(!vdb.query(query,1).empty())
throw Exception("This vote failed with the reason '") << reason << "' within the last " << secs_cast(limit) << ". Try again later.";
});
}
void Voting::valid_limits(const Vote &vote,
const Chan &chan,
const User &user)
{
using limits = std::numeric_limits<uint8_t>;
if(user.is_myself() || chan.users.mode(user).has('o'))
return;
const auto &cfg(vote.get_cfg());
if(count(chan) > cfg.get<uint8_t>("limit.active",limits::max()))
throw Exception("Too many active votes for this channel.");
if(count(chan,user) > cfg.get<uint8_t>("limit.user",limits::max()))
throw Exception("Too many active votes started by you on this channel.");
valid_limits_type(vote,chan,user,cfg);
}
void Voting::valid_limits_type(const Vote &vote,
const Chan &chan,
const User &user,
const Adoc &cfg)
{
using limits = std::numeric_limits<uint8_t>;
if(vote.get_type() == "appeal")
return;
if(vote.get_type() == "trial")
return;
auto typcnt(0);
for_each(chan,user,[&typcnt,&vote]
(const Vote &active)
{
typcnt += active.get_type() == vote.get_type();
});
if(typcnt > cfg.get<uint8_t>("limit.type",limits::max()))
throw Exception("Too many active votes of this type started by you on this channel.");
}
void Voting::eligible_worker()
{
worker_wait_init();
while(!interrupted.load(std::memory_order_consume)) try
{
eligible_add();
worker_sleep(std::chrono::hours(12));
}
catch(const Internal &e)
{
std::cerr << "[Voting (eligible worker)]: \033[1;41m" << e << "\033[0m" << std::endl;
}
}
void Voting::eligible_add()
{
const std::lock_guard<Bot> lock(bot);
auto &chans(get_chans());
chans.for_each([this](Chan &chan)
{
eligible_add(chan);
});
}
void Voting::eligible_add(Chan &chan)
try
{
const auto civis(chan.get("config.vote.civis"));
const auto age(secs_cast(civis["eligible.age"]));
const auto lines(civis.get<uint>("eligible.lines",0));
const auto automat(civis.get<bool>("eligible.automatic",0));
const Adoc excludes(civis.get_child("eligible.exclude",Adoc{}));
const auto exclude(excludes.into<std::set<std::string>>());
if(!lines || !age || !automat)
return;
std::cout << "Finding eligible for channel " << chan.get_name() << std::endl;
std::map<std::string,uint> count;
std::map<std::string,std::string> accts;
const auto endtime(time(NULL) - age);
irc::log::for_each(chan.get_name(),[&count,&accts,&endtime]
(const irc::log::ClosureArgs &a)
{
if(a.time > endtime)
return true;
if(strncmp(a.type,"PRI",3) != 0) // PRIVMSG
return true;
if(strnlen(a.acct,16) == 0 || *a.acct == '*')
return true;
++count[a.acct];
const auto iit(accts.emplace(a.acct,a.nick));
if(iit.second)
return true;
auto &nick(iit.first->second);
if(nick != a.nick)
nick = a.nick;
return true;
});
for(const auto &p : count) try
{
const auto &acct(p.first);
if(exclude.count(acct))
continue;
const auto &linecnt(p.second);
if(linecnt < lines)
continue;
auto &users(get_users());
const auto &nick(accts.at(acct));
if(!users.has(nick))
continue;
User &user(users.get(nick));
if(!user.is_logged_in())
continue;
if(chan.lists.has_flag(user,'V'))
continue;
bool exists(false);
for_each([&exists,&acct](const Vote &vote)
{
if(vote.get_type() == "civis" && vote.get_issue() == acct)
exists = true;
});
if(exists)
continue;
const auto ids =
{
eligible_last_vote(chan,nick,
{
Vdb::Term { "type", "==", "quiet" },
Vdb::Term { "reason", "==", "" },
}),
eligible_last_vote(chan,nick,
{
Vdb::Term { "type", "==", "ban" },
Vdb::Term { "reason", "==", "" },
}),
eligible_last_vote(chan,acct,
{
Vdb::Term { "type", "==", "civis" },
Vdb::Term { "reason", "==", "plurality" },
}),
};
const auto &id(*std::max_element(std::begin(ids),std::end(ids)));
if(id)
{
const auto startfrom(lex_cast<time_t>(vdb.get_value(id,"ended")));
const irc::log::FilterAny filt([&acct,&startfrom]
(const irc::log::ClosureArgs &a)
{
if(a.time < startfrom)
return false;
if(strncmp(a.type,"PRI",3) != 0) // PRIVMSG
return false;
return strncmp(a.acct,acct.c_str(),16) == 0;
});
if(irc::log::count(chan.get_name(),filt) < lines)
continue;
}
const auto &sess(get_sess());
auto &myself(users.get(sess.get_nick()));
const auto &vote(motion<vote::Civis>(chan,myself,nick));
user << "You are now eligible to be a citizen of " << chan.get_name() << "! ";
user << " Remind people to vote for issue #" << vote.get_id() << "!";
user << user.flush;
}
catch(const std::exception &e)
{
std::cerr << "Error on: [" << p.first << " : " << p.second << "]: " << e.what() << std::endl;
}
std::cout << "Finished eligible for channel " << chan.get_name() << std::endl;
}
catch(const std::exception &e)
{
std::cerr << "Voting::eligible_add(" << chan.get_name() << "): " << e.what() << std::endl;
}
id_t Voting::eligible_last_vote(const Chan &chan,
const std::string &nick,
Vdb::Terms terms)
{
terms.emplace_front(Vdb::Term{"issue","==",nick});
terms.emplace_front(Vdb::Term{"chan","==",chan.get_name()});
const auto res(vdb.query(terms,1));
return !res.empty()? res.front() : 0;
}
void Voting::remind_worker()
{
worker_wait_init();
while(!interrupted.load(std::memory_order_consume)) try
{
remind_votes();
worker_sleep(std::chrono::hours(24));
}
catch(const Internal &e)
{
std::cerr << "[Voting (remind worker)]: \033[1;41m" << e << "\033[0m" << std::endl;
}
}
void Voting::remind_votes()
{
using namespace colors;
const std::unique_lock<Bot> lock(bot);
static const milliseconds delay(750);
//const FloodGuard guard(bot.sess,delay);
const auto &chans(get_chans());
for(auto it(votes.cbegin()); it != votes.cend(); ++it)
{
const auto &vote(*it->second);
if(!chans.has(vote.get_chan_name()))
continue;
const auto &cfg(vote.get_cfg());
if(!cfg.get("remind.enable",false))
continue;
auto &chan(vote.get_chan());
chan.users.for_each([&](User &user)
{
if(vote.voted(user))
return;
if(!enfranchised(cfg,chan,user))
return;
user << user.PRIVMSG << chan << user.get_nick() << ", I see you have not yet voted on issue "
<< vote << ", " << BOLD << vote.get_type() << OFF << ": " << UNDER2 << vote.get_issue() << OFF << ". "
<< "Your participation as a citizen of " << chan.get_name() << " is highly valued to maintain a fair and democratic community. "
<< "Please consider " << BOLD << FG::GREEN << "!v y " << vote.get_id() << OFF << " or " << BOLD << FG::RED << "!v n " << vote.get_id() << OFF
<< " before the issue closes in " << BOLD << secs_cast(vote.remaining()) << OFF << ". "
<< user.flush;
// TODO: fix guard
std::this_thread::sleep_for(delay);
});
}
}
void Voting::poll_worker()
{
poll_init();
initialized.store(true,std::memory_order_release);
sem.notify_all();
while(!interrupted.load(std::memory_order_consume)) try
{
poll_votes();
worker_sleep(std::chrono::seconds(2));
}
catch(const std::exception &e)
{
std::cerr << "[Voting (poll worker)]: \033[1;41m"
<< e.what() << "\033[0m"
<< std::endl;
}
}
void Voting::poll_init()
{
const std::lock_guard<Bot> lock(bot);
bot.set_tls_context();
std::cout << "[Voting]: Adding previously open votes."
<< " Reading " << vdb.count() << " votes..."
<< std::endl;
auto it(vdb.cbegin(stldb::SNAPSHOT));
const auto end(vdb.cend());
for(; it != end && !interrupted.load(std::memory_order_consume); ++it) try
{
const auto id(lex_cast<id_t>(it->first));
const Adoc doc(it->second);
const auto ended(secs_cast(doc["ended"]));
if(!ended)
{
std::cout << "Adding open vote #" << id << std::endl;
auto vote(vdb.get(id));
const auto iit(votes.emplace(id,std::move(vote)));
{
const auto &vote(*iit.first->second);
chanidx.emplace(vote.get_chan_name(),id);
useridx.emplace(vote.get_user_acct(),id);
}
}
}
catch(const std::exception &e)
{
const auto id(lex_cast<id_t>(it->first));
std::cerr << "[Voting]: Failed reading #" << id
<< ": \033[1;31m" << e.what()
<< "\033[0m" << std::endl;
}
}
void Voting::poll_votes()
{
const std::unique_lock<Bot> lock(bot);
const auto &chans(get_chans());
for(auto it(votes.begin()); it != votes.end();)
{
auto &vote(*it->second);
const bool finished
{
vote.get_ended() ||
vote.remaining() <= 0 ||
vote.interceded()
};
if(finished && chans.has(vote.get_chan_name()))
{
call_finish(vote);
praetor.add(std::move(del(it++)));
}
else ++it;
}
}
void Voting::call_finish(Vote &vote)
noexcept try
{
vote.finish();
}
catch(const std::exception &e)
{
std::stringstream err;
err << "An internal error occurred in Vote::finish(): " << e.what() << ".";
std::cerr << "[Voting]: \033[1;31m" << err.str() << "\033[0m" << std::endl;
const auto &chans(get_chans());
if(chans.has(vote.get_chan_name()))
{
Chan &chan(vote.get_chan());
chan << err.str() << chan.flush;
}
}
std::unique_ptr<Vote> Voting::del(const id_t &id)
{
const auto vit(votes.find(id));
if(vit == votes.end())
throw Exception("Could not delete any vote by this ID.");
return del(vit);
}
std::unique_ptr<Vote> Voting::del(const decltype(votes.begin()) &it)
{
const id_t &id(it->first);
auto vote(std::move(it->second));
const auto deindex([&id]
(auto &map, const auto &ent)
{
const auto pit(map.equal_range(ent));
const auto it(std::find_if(pit.first,pit.second,[&id]
(const auto &it)
{
const id_t &idx = it.second;
return idx == id;
}));
if(it != pit.second)
map.erase(it);
});
deindex(chanidx,vote->get_chan_name());
deindex(useridx,vote->get_user_acct());
votes.erase(it);
return std::move(vote);
}
template<class Duration>
void Voting::worker_sleep(Duration&& duration)
{
using std::cv_status;
using std::chrono::steady_clock;
const auto now(steady_clock::now());
const auto fut(now + duration);
std::unique_lock<decltype(mutex)> lock(mutex);
while(sem.wait_until(lock,fut) != cv_status::timeout && !interrupted.load(std::memory_order_consume));
}
void Voting::worker_wait_init()
{
{
std::unique_lock<decltype(mutex)> lock(mutex);
sem.wait(lock,[this]
{
return initialized.load(std::memory_order_consume) ||
interrupted.load(std::memory_order_consume);
});
}
{
const std::unique_lock<Bot> lock(bot);
bot.set_tls_context();
}
}
id_t Voting::get_next_id()
const
{
id_t i(1);
while(vdb.exists(i) || exists(i))
++i;
return i;
}
id_t Voting::duplicated(const Vote &vote)
const
{
const auto pit(chanidx.equal_range(vote.get_chan_name()));
for(auto it(pit.first); it != pit.second; ++it)
{
const auto id(it->second);
const auto vit(votes.find(id));
if(vit == votes.end())
continue;
const auto &existing(*vit->second);
if(vote.get_id() == existing.get_id())
continue;
if(vote.get_type() != existing.get_type())
continue;
if(!boost::iequals(vote.get_issue(),existing.get_issue()))
continue;
return existing.get_id();
};
return 0;
}
void Voting::for_each(const std::function<void (Vote &vote)> &closure)
{
for(auto &p : votes)
closure(*p.second);
}
void Voting::for_each(const User &user,
const std::function<void (Vote &vote)> &closure)
{
auto pit(useridx.equal_range(user.get_acct()));
for(; pit.first != pit.second; ++pit.first)
{
const auto &id(pit.first->second);
closure(get(id));
}
}
void Voting::for_each(const Chan &chan,
const std::function<void (Vote &vote)> &closure)
{
auto pit(chanidx.equal_range(chan.get_name()));
for(; pit.first != pit.second; ++pit.first)
{
const auto &id(pit.first->second);
closure(get(id));
}
}
void Voting::for_each(const Chan &chan,
const User &user,
const std::function<void (Vote &vote)> &closure)
{
const auto ids(get_ids(chan,user));
for(const auto &id : ids)
closure(get(id));
}
void Voting::for_each(const std::function<void (const Vote &vote)> &closure)
const
{
for(const auto &p : votes)
closure(*p.second);
}
void Voting::for_each(const User &user,
const std::function<void (const Vote &vote)> &closure)
const
{
auto pit(useridx.equal_range(user.get_acct()));
for(; pit.first != pit.second; ++pit.first)
{
const auto &id(pit.first->second);
closure(get(id));
}
}
void Voting::for_each(const Chan &chan,
const std::function<void (const Vote &vote)> &closure)
const
{
auto pit(chanidx.equal_range(chan.get_name()));
for(; pit.first != pit.second; ++pit.first)
{
const auto &id(pit.first->second);
closure(get(id));
}
}
void Voting::for_each(const Chan &chan,
const User &user,
const std::function<void (const Vote &vote)> &closure)
const
{
const auto ids(get_ids(chan,user));
for(const auto &id : ids)
closure(get(id));
}
Vote &Voting::get(const id_t &id)
try
{
return *votes.at(id);
}
catch(const std::out_of_range &e)
{
throw Exception("There is no active vote with that ID.");
}
const Vote &Voting::get(const id_t &id)
const try
{
return *votes.at(id);
}
catch(const std::out_of_range &e)
{
throw Exception("There is no active vote with that ID.");
}
const id_t &Voting::get_id(const User &user)
const
{
if(count(user) > 1)
throw Exception("There are multiple votes initiated by this user. Specify an ID#.");
const auto it(useridx.find(user.get_acct()));
if(it == useridx.end())
throw Exception("There are no active votes initiated by this user.");
return it->second;
}
const id_t &Voting::get_id(const Chan &chan)
const
{
if(count(chan) > 1)
throw Exception("There are multiple votes active in this channel. Specify an ID#.");
const auto it(chanidx.find(chan.get_name()));
if(it == chanidx.end())
throw Exception("There are no active votes in this channel.");
return it->second;
}
std::vector<id_t> Voting::get_ids(const Chan &chan,
const User &user)
const
{
const auto cpit(chanidx.equal_range(chan.get_name()));
if(cpit.first == cpit.second)
return {};
const auto upit(useridx.equal_range(user.get_acct()));
if(upit.first == upit.second)
return {};
static const auto second([](const auto &p) { return p.second; });
std::vector<id_t> cids(std::distance(cpit.first,cpit.second));
std::vector<id_t> uids(std::distance(upit.first,upit.second));
std::transform(cpit.first,cpit.second,cids.begin(),second);
std::transform(upit.first,upit.second,uids.begin(),second);
std::sort(cids.begin(),cids.end());
std::sort(uids.begin(),uids.end());
std::vector<id_t> ret(std::min(cids.size(),uids.size()));
std::set_intersection(cids.begin(),cids.end(),uids.begin(),uids.end(),ret.begin());
return ret;
}