-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.h
213 lines (163 loc) · 4.72 KB
/
mask.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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
struct Mask : std::string
{
enum Form { INVALID, CANONICAL, EXTENDED };
enum Type { NICK, USER, HOST, ACCT, JOIN, GECOS, FULL, SSL };
auto num(const char &c) const { return std::count(begin(),end(),c); }
bool has(const char &c) const { return find(c) != std::string::npos; }
// Canonical mask related
auto get_nick() const { return substr(0,find('!')); }
auto get_user() const { return substr(find('!')+1,find('@') - find('!') - 1); }
auto get_host() const { return substr(find('@')+1,npos); }
bool has_ident() const { return !has('~'); }
bool has_all_wild() const { return std::string(*this) == "*!*@*"; }
bool has_wild(const Type &t) const;
bool is_canonical() const;
// Extended mask utils
bool has_negation() const { return has('~'); }
bool has_mask() const { return has(':'); }
auto get_mask() const { return has_mask()? substr(find(':')+1,npos) : ""; }
auto &get_exttype() const { return has_negation()? at(2) : at(1); }
bool has_wild_mask() const { return get_mask().empty() || get_mask() == "*"; }
bool is_extended() const { return size() > 1 && at(0) == '$'; }
Mask(void) = default;
Mask(const char *const &s): std::string(s) {}
Mask(const std::string &s): std::string(s) {}
Mask(std::string &&s): std::string(std::move(s)) {}
Mask(Mask &&) = default;
Mask(const Mask &) = default;
Mask &operator=(Mask &&) = default;
Mask &operator=(const Mask &) = default;
};
inline
bool Mask::is_canonical()
const
{
return has('!') &&
has('@') &&
num('!') == 1 &&
num('@') == 1 &&
find('!') < find('@') &&
!get_nick().empty() &&
!get_user().empty() &&
!get_host().empty();
}
inline
bool Mask::has_wild(const Type &t)
const
{
switch(t)
{
case NICK: return get_nick() == "*";
case USER: return get_user() == "*";
case HOST: return get_host() == "*";
default: return false;
}
}
inline
Mask::Form form(const Mask &mask)
{
return mask.is_canonical()? Mask::CANONICAL:
mask.is_extended()? Mask::EXTENDED:
Mask::INVALID;
}
inline
bool operator==(const Mask &a, const Mask::Form &f)
{
return form(a) == f;
}
inline
bool operator!=(const Mask &a, const Mask::Form &f)
{
return form(a) != f;
}
inline
bool operator==(const Mask &a, const Mask &b)
{
auto ap(0U), bp(0U);
while(ap < a.size() && bp < b.size())
{
const auto ca(tolower(a.at(ap))), cb(tolower(b.at(bp)));
const auto globa(ca == '*'), globb(cb == '*');
const auto wilda(ca == '?'), wildb(cb == '?');
if(!globa && !globb && !wilda && !wildb && ca != cb)
return false;
if((globa && ap+1 >= a.size()) || (globb && bp+1 >= b.size()))
break;
if(globa && cb == tolower(a.at(ap+1)))
ap += 2;
if(globb && ca == tolower(b.at(bp+1)))
bp += 2;
if(globa && globb)
++ap, ++bp;
if(!globa)
++ap;
if(!globb)
++bp;
}
if(ap < a.size() && !b.empty() && b.back() == '*')
return true;
if(bp < b.size() && !a.empty() && a.back() == '*')
return true;
return std::equal(a.begin()+ap,a.end(),b.begin()+bp,b.end());
}
inline
bool operator!=(const Mask &a, const Mask &b)
{
return !(a == b);
}
inline
bool operator<=(const Mask &a, const Mask &b)
{
return a == b || tolower(a) <= tolower(b);
}
inline
bool operator>=(const Mask &a, const Mask &b)
{
return a == b || tolower(a) >= tolower(b);
}
inline
bool operator<(const Mask &a, const Mask &b)
{
return a != b && tolower(a) < tolower(b);
}
inline
bool operator>(const Mask &a, const Mask &b)
{
return a != b && tolower(a) > tolower(b);
}
inline
bool operator==(const Mask &a, const std::string &b)
{
return tolower(a) == tolower(b);
}
inline
bool operator!=(const Mask &a, const std::string &b)
{
return tolower(a) != tolower(b);
}
inline
bool operator<=(const Mask &a, const std::string &b)
{
return tolower(a) <= tolower(b);
}
inline
bool operator>=(const Mask &a, const std::string &b)
{
return tolower(a) >= tolower(b);
}
inline
bool operator<(const Mask &a, const std::string &b)
{
return tolower(a) < tolower(b);
}
inline
bool operator>(const Mask &a, const std::string &b)
{
return tolower(a) > tolower(b);
}