InspIRCd  4.0
xline.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Matt Schatz <[email protected]>
5  * Copyright (C) 2012-2013, 2017-2018, 2020 Sadie Powell <[email protected]>
6  * Copyright (C) 2012, 2019 Robby <[email protected]>
7  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
8  * Copyright (C) 2007 Dennis Friis <[email protected]>
9  * Copyright (C) 2006-2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2004, 2006-2008, 2010 Craig Edwards <[email protected]>
11  *
12  * This file is part of InspIRCd. InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #pragma once
27 
35 class CoreExport XLine : public Cullable
36 {
37  protected:
38 
44  void DefaultApply(User* u, const std::string &line, bool bancache);
45 
46  public:
47 
55  XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
56  : set_time(s_time)
57  , duration(d)
58  , source(src)
59  , reason(re)
60  , type(t)
61  {
62  expiry = set_time + duration;
63  }
64 
67  virtual ~XLine() = default;
68 
72  virtual void SetCreateTime(time_t created)
73  {
74  set_time = created;
75  expiry = created + duration;
76  }
77 
83  virtual bool Matches(User *u) = 0;
84 
91  virtual bool Matches(const std::string &str) = 0;
92 
97  virtual void Apply(User* u);
98 
102  virtual void Unset() { }
103 
108  virtual void DisplayExpiry();
109 
117  virtual const std::string& Displayable() = 0;
118 
121  virtual void OnAdd() { }
122 
125  time_t set_time;
126 
129  unsigned long duration;
130 
133  std::string source;
134 
137  std::string reason;
138 
141  time_t expiry;
142 
146  const std::string type;
147 
148  // Whether this XLine was loaded from the server config.
149  bool from_config = false;
150 
151  virtual bool IsBurstable();
152 };
153 
156 class CoreExport KLine : public XLine
157 {
158  public:
159 
168  KLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
169  : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
170  {
171  matchtext = this->identmask;
172  matchtext.append("@").append(this->hostmask);
173  }
174 
175  bool Matches(User* u) override;
176 
177  bool Matches(const std::string& str) override;
178 
179  void Apply(User* u) override;
180 
181  const std::string& Displayable() override;
182 
183  bool IsBurstable() override;
184 
187  std::string identmask;
190  std::string hostmask;
191 
192  std::string matchtext;
193 };
194 
197 class CoreExport GLine : public XLine
198 {
199  public:
208  GLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
209  : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
210  {
211  matchtext = this->identmask;
212  matchtext.append("@").append(this->hostmask);
213  }
214 
215  bool Matches(User* u) override;
216 
217  bool Matches(const std::string& str) override;
218 
219  void Apply(User* u) override;
220 
221  const std::string& Displayable() override;
222 
225  std::string identmask;
228  std::string hostmask;
229 
230  std::string matchtext;
231 };
232 
235 class CoreExport ELine : public XLine
236 {
237  public:
246  ELine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
247  : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
248  {
249  matchtext = this->identmask;
250  matchtext.append("@").append(this->hostmask);
251  }
252 
253  bool Matches(User* u) override;
254 
255  bool Matches(const std::string& str) override;
256 
257  void Unset() override;
258 
259  void OnAdd() override;
260 
261  const std::string& Displayable() override;
262 
265  std::string identmask;
268  std::string hostmask;
269 
270  std::string matchtext;
271 };
272 
275 class CoreExport ZLine : public XLine
276 {
277  public:
285  ZLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ip)
286  : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
287  {
288  }
289 
290  bool Matches(User* u) override;
291 
292  bool Matches(const std::string& str) override;
293 
294  void Apply(User* u) override;
295 
296  const std::string& Displayable() override;
297 
300  std::string ipaddr;
301 };
302 
305 class CoreExport QLine : public XLine
306 {
307  public:
315  QLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nickname)
316  : XLine(s_time, d, src, re, "Q"), nick(nickname)
317  {
318  }
319 
320  bool Matches(User* u) override;
321 
322  bool Matches(const std::string& str) override;
323 
324  void Apply(User* u) override;
325 
326  const std::string& Displayable() override;
327 
330  std::string nick;
331 };
332 
340 class CoreExport XLineFactory
341 {
342  protected:
343 
344  std::string type;
345 
346  public:
347 
351  XLineFactory(const std::string &t) : type(t) { }
352 
356  virtual const std::string& GetType() { return type; }
357 
366  virtual XLine* Generate(time_t set_time, unsigned long duration, const std::string& source, const std::string& reason, const std::string& xline_specific_mask) = 0;
367 
368  virtual bool AutoApplyToUserList(XLine* x) { return true; }
369 
372  virtual ~XLineFactory() = default;
373 };
374 
379 class CoreExport XLineManager
380 {
381  protected:
384  std::vector<XLine *> pending_lines;
385 
388  XLineFactMap line_factory;
389 
395  XLineContainer lookup_lines;
396 
397  public:
398 
401  XLineManager();
402 
405  ~XLineManager();
406 
410  IdentHostPair IdentSplit(const std::string &ident_and_host);
411 
414  void CheckELines();
415 
422  XLineLookup* GetAll(const std::string &type);
423 
426  void DelAll(const std::string &type);
427 
431  std::vector<std::string> GetAllTypes();
432 
438  bool AddLine(XLine* line, User* user);
439 
448  bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
449 
460  bool RegisterFactory(XLineFactory* xlf);
461 
466  bool UnregisterFactory(XLineFactory* xlf);
467 
472  XLineFactory* GetFactory(const std::string &type);
473 
479  XLine* MatchesLine(const std::string &type, User* user);
480 
486  XLine* MatchesLine(const std::string &type, const std::string &pattern);
487 
493  void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
494 
499  void ApplyLines();
500 
506  bool InvokeStats(const std::string& type, Stats::Context& context);
507 
509  void ExpireRemovedConfigLines(const std::string& type, const insp::flat_set<std::string>& configlines);
510 };
Definition: cull.h:30
Definition: xline.h:236
ELine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &ident, const std::string &host)
Definition: xline.h:246
std::string hostmask
Definition: xline.h:268
std::string identmask
Definition: xline.h:265
Definition: xline.h:198
std::string identmask
Definition: xline.h:225
std::string hostmask
Definition: xline.h:228
GLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &ident, const std::string &host)
Definition: xline.h:208
Definition: xline.h:157
std::string hostmask
Definition: xline.h:190
std::string identmask
Definition: xline.h:187
KLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &ident, const std::string &host)
Definition: xline.h:168
Definition: xline.h:306
QLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &nickname)
Definition: xline.h:315
std::string nick
Definition: xline.h:330
Definition: users.h:238
Definition: xline.h:341
virtual ~XLineFactory()=default
virtual XLine * Generate(time_t set_time, unsigned long duration, const std::string &source, const std::string &reason, const std::string &xline_specific_mask)=0
XLineFactory(const std::string &t)
Definition: xline.h:351
virtual const std::string & GetType()
Definition: xline.h:356
Definition: xline.h:36
std::string reason
Definition: xline.h:137
virtual ~XLine()=default
virtual void Unset()
Definition: xline.h:102
virtual void SetCreateTime(time_t created)
Definition: xline.h:72
time_t set_time
Definition: xline.h:125
virtual void OnAdd()
Definition: xline.h:121
unsigned long duration
Definition: xline.h:129
const std::string type
Definition: xline.h:146
time_t expiry
Definition: xline.h:141
virtual bool Matches(const std::string &str)=0
XLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &t)
Definition: xline.h:55
std::string source
Definition: xline.h:133
virtual const std::string & Displayable()=0
Definition: xline.h:380
XLineContainer lookup_lines
Definition: xline.h:395
XLineFactMap line_factory
Definition: xline.h:388
std::vector< XLine * > pending_lines
Definition: xline.h:384
Definition: xline.h:276
std::string ipaddr
Definition: xline.h:300
ZLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &ip)
Definition: xline.h:285
Definition: flat_map.h:208