-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan_lists.h
308 lines (239 loc) · 6.6 KB
/
chan_lists.h
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
template<class Value> using List = std::set<Value>;
template<class R, class List> using Closure = std::function<R (const typename List::value_type &)>;
template<class List> void for_each(const List &list, const Mask &match, const Closure<void,List> &func);
template<class List> void for_each(const List &list, const Mask &match, const Closure<bool,List> &func);
template<class List> Deltas compose(const List &list, const User &user, const Delta &delta);
template<class List> size_t count(const List &list, const Mask &match);
template<class List> size_t count(const List &list, const User &user);
template<class List> bool exists(const List &list, const Mask &match);
template<class List> bool exists(const List &list, const User &user);
struct Lists
{
List<Ban> bans;
List<Quiet> quiets;
List<Except> excepts;
List<Invite> invites;
List<AKick> akicks;
List<Flags> flags;
// Convenience access for the flags list only
const Flags &get_flag(const Mask &m) const;
const Flags &get_flag(const User &u) const;
bool has_flag(const Mask &m) const;
bool has_flag(const User &u) const;
bool has_flag(const Mask &m, const char &flag) const;
bool has_flag(const User &u, const char &flag) const;
// Finds existence of user in appropriate list by letters
bool has_mode(const User &u, const char &list) const;
bool has_mode(const User &u, const Mode &lists = {"bqeI"}) const;
// Mutators
bool set_mode(const Delta &delta);
void delta_flag(const Mask &m, const std::string &delta);
friend std::ostream &operator<<(std::ostream &s, const Lists &lists);
};
inline
void Lists::delta_flag(const Mask &mask,
const std::string &delta)
{
if(mask.empty())
throw Assertive("Lists::delta_flag on empty Mask");
auto it(flags.find(Flags{mask}));
if(it == flags.end())
it = flags.emplace(mask).first;
auto &f(const_cast<Flags &>(*it));
f.delta(delta);
if(!f)
flags.erase(it);
else
f.update(time(NULL));
}
inline
bool Lists::set_mode(const Delta &d)
{
using std::get;
switch(char(d))
{
case 'b': return bool(d)? bans.emplace(get<d.MASK>(d)).second:
bans.erase(get<d.MASK>(d));
case 'q': return bool(d)? quiets.emplace(get<d.MASK>(d)).second:
quiets.erase(get<d.MASK>(d));
case 'e': return bool(d)? excepts.emplace(get<d.MASK>(d)).second:
excepts.erase(get<d.MASK>(d));
case 'I': return bool(d)? invites.emplace(get<d.MASK>(d)).second:
invites.erase(get<d.MASK>(d));
default: return false;
}
}
inline
bool Lists::has_mode(const User &user,
const Mode &lists)
const
{
for(const auto &list : lists)
if(has_mode(user,list))
return true;
return false;
}
inline
bool Lists::has_mode(const User &user,
const char &list)
const
{
switch(list)
{
case 'b': return exists(bans,user);
case 'q': return exists(quiets,user);
case 'e': return exists(excepts,user);
case 'I': return exists(invites,user);
default: return false;
}
}
inline
bool Lists::has_flag(const User &u,
const char &flag)
const
{
return u.is_logged_in()? has_flag(u.get_acct(),flag) : false;
}
inline
bool Lists::has_flag(const Mask &m,
const char &flag)
const
{
const auto it(flags.find(Flags{m}));
return it != flags.end()? it->get_flags().has(flag) : false;
}
inline
bool Lists::has_flag(const User &u)
const
{
return u.is_logged_in()? has_flag(u.get_acct()) : false;
}
inline
bool Lists::has_flag(const Mask &m)
const
{
return flags.count(Flags{m});
}
inline
const Flags &Lists::get_flag(const User &u)
const
{
return get_flag(u.get_acct());
}
inline
const Flags &Lists::get_flag(const Mask &m)
const
{
const auto it(flags.find(Flags{m}));
if(it == flags.end())
throw Exception("No flags matching this user.");
return *it;
}
inline
std::ostream &operator<<(std::ostream &s, const Lists &l)
{
s << "bans: \t" << l.bans.size() << std::endl;
for(const auto &b : l.bans)
s << "\t+b " << b << std::endl;
s << "quiets: \t" << l.quiets.size() << std::endl;
for(const auto &q : l.quiets)
s << "\t+q " << q << std::endl;
s << "excepts: \t" << l.excepts.size() << std::endl;
for(const auto &e : l.excepts)
s << "\t+e " << e << std::endl;
s << "invites: \t" << l.invites.size() << std::endl;
for(const auto &i : l.invites)
s << "\t+I " << i << std::endl;
s << "flags: \t" << l.flags.size() << std::endl;
for(const auto &f : l.flags)
s << "\t"<< f << std::endl;
s << "akicks: \t" << l.akicks.size() << std::endl;
for(const auto &a : l.akicks)
s << "\t"<< a << std::endl;
return s;
}
template<class List>
bool exists(const List &list,
const User &user)
{
if(exists(list,user.mask(Mask::NICK)))
return true;
if(exists(list,user.mask(Mask::HOST)))
return true;
if(user.is_logged_in() && exists(list,user.mask(Mask::ACCT)))
return true;
return false;
}
template<class List>
bool exists(const List &list,
const Mask &match)
{
return std::any_of(std::begin(list),std::end(list),[&match]
(const auto &element)
{
return Mask(element) == match;
});
}
template<class List>
size_t count(const List &list,
const User &user)
{
size_t ret(0);
ret += count(list,user.mask(Mask::NICK));
ret += count(list,user.mask(Mask::HOST));
if(user.is_logged_in())
ret += count(list,user.mask(Mask::ACCT));
return ret;
}
template<class List>
size_t count(const List &list,
const Mask &match)
{
return std::count_if(std::begin(list),std::end(list),[&match]
(const auto &element)
{
return Mask(element) == match;
});
}
template<class List>
Deltas compose(const List &list,
const User &user,
const Delta &delta)
{
Deltas ret;
const auto lambda([&ret,&delta]
(const auto &element)
{
ret.emplace_back(string(delta),Mask(element));
});
if(user.is_logged_in())
for_each(list,user.mask(Mask::ACCT),lambda);
for_each(list,user.mask(Mask::NICK),lambda);
for_each(list,user.mask(Mask::HOST),lambda);
return ret;
}
template<class List>
void for_each(const List &list,
const Mask &match,
const Closure<void,List> &func)
{
for(const auto &elem : list)
if(elem == match)
func(elem);
}
template<class List>
void for_each(const List &list,
const Mask &match,
const Closure<bool,List> &func)
{
for(const auto &elem : list)
if(elem == match)
if(!func(elem))
return;
}