InspIRCd  3.0
xline.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Matt Schatz <[email protected]>
5  * Copyright (C) 2013, 2016 Attila Molnar <[email protected]>
6  * Copyright (C) 2012-2013, 2017-2018 Sadie Powell <[email protected]>
7  * Copyright (C) 2012, 2018-2019 Robby <[email protected]>
8  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
9  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2007 Dennis Friis <[email protected]>
11  * Copyright (C) 2006-2008, 2010 Craig Edwards <[email protected]>
12  *
13  * This file is part of InspIRCd. InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
36 class CoreExport XLine : public classbase
37 {
38  protected:
39 
45  void DefaultApply(User* u, const std::string &line, bool bancache);
46 
47  public:
48 
56  XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
57  : set_time(s_time)
58  , duration(d)
59  , source(src)
60  , reason(re)
61  , type(t)
62  , from_config(false)
63  {
64  expiry = set_time + duration;
65  }
66 
69  virtual ~XLine()
70  {
71  }
72 
76  virtual void SetCreateTime(time_t created)
77  {
78  set_time = created;
79  expiry = created + duration;
80  }
81 
87  virtual bool Matches(User *u) = 0;
88 
95  virtual bool Matches(const std::string &str) = 0;
96 
101  virtual void Apply(User* u);
102 
106  virtual void Unset() { }
107 
112  virtual void DisplayExpiry();
113 
121  virtual const std::string& Displayable() = 0;
122 
125  virtual void OnAdd() { }
126 
129  time_t set_time;
130 
133  unsigned long duration;
134 
137  std::string source;
138 
141  std::string reason;
142 
145  time_t expiry;
146 
150  const std::string type;
151 
152  // Whether this XLine was loaded from the server config.
153  bool from_config;
154 
155  virtual bool IsBurstable();
156 };
157 
160 class CoreExport KLine : public XLine
161 {
162  public:
163 
172  KLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
173  : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
174  {
175  matchtext = this->identmask;
176  matchtext.append("@").append(this->hostmask);
177  }
178 
182  {
183  }
184 
185  bool Matches(User* u) CXX11_OVERRIDE;
186 
187  bool Matches(const std::string& str) CXX11_OVERRIDE;
188 
189  void Apply(User* u) CXX11_OVERRIDE;
190 
191  const std::string& Displayable() CXX11_OVERRIDE;
192 
193  bool IsBurstable() CXX11_OVERRIDE;
194 
197  std::string identmask;
200  std::string hostmask;
201 
202  std::string matchtext;
203 };
204 
207 class CoreExport GLine : public XLine
208 {
209  public:
218  GLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
219  : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
220  {
221  matchtext = this->identmask;
222  matchtext.append("@").append(this->hostmask);
223  }
224 
228  {
229  }
230 
231  bool Matches(User* u) CXX11_OVERRIDE;
232 
233  bool Matches(const std::string& str) CXX11_OVERRIDE;
234 
235  void Apply(User* u) CXX11_OVERRIDE;
236 
237  const std::string& Displayable() CXX11_OVERRIDE;
238 
241  std::string identmask;
244  std::string hostmask;
245 
246  std::string matchtext;
247 };
248 
251 class CoreExport ELine : public XLine
252 {
253  public:
262  ELine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
263  : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
264  {
265  matchtext = this->identmask;
266  matchtext.append("@").append(this->hostmask);
267  }
268 
269  ~ELine()
270  {
271  }
272 
273  bool Matches(User* u) CXX11_OVERRIDE;
274 
275  bool Matches(const std::string& str) CXX11_OVERRIDE;
276 
277  void Unset() CXX11_OVERRIDE;
278 
279  void OnAdd() CXX11_OVERRIDE;
280 
281  const std::string& Displayable() CXX11_OVERRIDE;
282 
285  std::string identmask;
288  std::string hostmask;
289 
290  std::string matchtext;
291 };
292 
295 class CoreExport ZLine : public XLine
296 {
297  public:
305  ZLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ip)
306  : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
307  {
308  }
309 
313  {
314  }
315 
316  bool Matches(User* u) CXX11_OVERRIDE;
317 
318  bool Matches(const std::string& str) CXX11_OVERRIDE;
319 
320  void Apply(User* u) CXX11_OVERRIDE;
321 
322  const std::string& Displayable() CXX11_OVERRIDE;
323 
326  std::string ipaddr;
327 };
328 
331 class CoreExport QLine : public XLine
332 {
333  public:
341  QLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nickname)
342  : XLine(s_time, d, src, re, "Q"), nick(nickname)
343  {
344  }
345 
349  {
350  }
351  bool Matches(User* u) CXX11_OVERRIDE;
352 
353  bool Matches(const std::string& str) CXX11_OVERRIDE;
354 
355  void Apply(User* u) CXX11_OVERRIDE;
356 
357  const std::string& Displayable() CXX11_OVERRIDE;
358 
361  std::string nick;
362 };
363 
371 class CoreExport XLineFactory
372 {
373  protected:
374 
375  std::string type;
376 
377  public:
378 
382  XLineFactory(const std::string &t) : type(t) { }
383 
387  virtual const std::string& GetType() { return type; }
388 
397  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;
398 
399  virtual bool AutoApplyToUserList(XLine* x) { return true; }
400 
403  virtual ~XLineFactory() { }
404 };
405 
410 class CoreExport XLineManager
411 {
412  protected:
415  std::vector<XLine *> pending_lines;
416 
419  XLineFactMap line_factory;
420 
426  XLineContainer lookup_lines;
427 
428  public:
429 
432  XLineManager();
433 
436  ~XLineManager();
437 
441  IdentHostPair IdentSplit(const std::string &ident_and_host);
442 
445  void CheckELines();
446 
453  XLineLookup* GetAll(const std::string &type);
454 
457  void DelAll(const std::string &type);
458 
462  std::vector<std::string> GetAllTypes();
463 
469  bool AddLine(XLine* line, User* user);
470 
479  bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
480 
491  bool RegisterFactory(XLineFactory* xlf);
492 
497  bool UnregisterFactory(XLineFactory* xlf);
498 
503  XLineFactory* GetFactory(const std::string &type);
504 
510  XLine* MatchesLine(const std::string &type, User* user);
511 
517  XLine* MatchesLine(const std::string &type, const std::string &pattern);
518 
524  void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
525 
530  void ApplyLines();
531 
539  void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats);
540 
542  void ExpireRemovedConfigLines(const std::string& type, const insp::flat_set<std::string>& configlines);
543 };
XLine::Apply
virtual void Apply(User *u)
Definition: xline.cpp:550
ZLine::~ZLine
~ZLine()
Definition: xline.h:312
GLine
Definition: xline.h:207
XLineManager::lookup_lines
XLineContainer lookup_lines
Definition: xline.h:426
insp::flat_set
Definition: flat_map.h:205
XLineFactory::XLineFactory
XLineFactory(const std::string &t)
Definition: xline.h:382
ELine::ELine
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:262
ELine
Definition: xline.h:251
XLine::expiry
time_t expiry
Definition: xline.h:145
XLine::~XLine
virtual ~XLine()
Definition: xline.h:69
XLineFactory::~XLineFactory
virtual ~XLineFactory()
Definition: xline.h:403
XLineManager::line_factory
XLineFactMap line_factory
Definition: xline.h:419
XLine::Matches
virtual bool Matches(User *u)=0
Definition: xline.cpp:144
KLine::KLine
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:172
QLine::QLine
QLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &nickname)
Definition: xline.h:341
XLine::source
std::string source
Definition: xline.h:137
XLine::set_time
time_t set_time
Definition: xline.h:129
KLine::~KLine
~KLine()
Definition: xline.h:181
XLineFactory::GetType
virtual const std::string & GetType()
Definition: xline.h:387
XLine::XLine
XLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &t)
Definition: xline.h:56
classbase
Definition: base.h:46
KLine
Definition: xline.h:160
XLine::duration
unsigned long duration
Definition: xline.h:133
XLine
Definition: xline.h:36
XLine::type
const std::string type
Definition: xline.h:150
ZLine::ZLine
ZLine(time_t s_time, unsigned long d, const std::string &src, const std::string &re, const std::string &ip)
Definition: xline.h:305
XLine::SetCreateTime
virtual void SetCreateTime(time_t created)
Definition: xline.h:76
QLine::~QLine
~QLine()
Definition: xline.h:348
XLineManager::pending_lines
std::vector< XLine * > pending_lines
Definition: xline.h:415
XLine::OnAdd
virtual void OnAdd()
Definition: xline.h:125
XLineFactory
Definition: xline.h:371
XLine::reason
std::string reason
Definition: xline.h:141
QLine
Definition: xline.h:331
User
Definition: users.h:233
GLine::~GLine
~GLine()
Definition: xline.h:227
GLine::GLine
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:218
XLine::Displayable
virtual const std::string & Displayable()=0
XLineManager
Definition: xline.h:410
XLine::Unset
virtual void Unset()
Definition: xline.h:106
ZLine
Definition: xline.h:295