The Battle for Wesnoth  1.19.5+dev
ban.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Pauli Nieminen <paniemin@cc.hut.fi>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "exceptions.hpp"
19 #include "utils/optional_fwd.hpp"
20 
21 #include <list>
22 #include <map>
23 #include <queue>
24 #include <set>
25 
26 class config;
27 
28 namespace wesnothd
29 {
30 class banned;
31 
32 std::ostream& operator<<(std::ostream& o, const banned& n);
33 
34 typedef std::shared_ptr<banned> banned_ptr;
35 
36 /** We want to move the lowest value to the top. */
38 {
39  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
40 };
41 
43 {
44  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
45 
46 private:
47  bool less(const banned_ptr& a, const banned_ptr& b) const;
48  typedef bool (banned_compare_subnet::*compare_fn)(const banned_ptr& a, const banned_ptr& b) const;
50 };
51 
52 typedef std::set<banned_ptr, banned_compare_subnet> ban_set;
53 typedef std::list<banned_ptr> deleted_ban_list;
54 typedef std::priority_queue<banned_ptr, std::vector<banned_ptr>, banned_compare> ban_time_queue;
55 typedef std::map<std::string, std::chrono::seconds> default_ban_times;
56 typedef std::pair<unsigned int, unsigned int> ip_mask;
57 
58 ip_mask parse_ip(const std::string&);
59 
60 class banned {
61  unsigned int ip_;
62  unsigned int mask_;
63  std::string ip_text_;
64  utils::optional<std::chrono::system_clock::time_point> end_time_;
65  utils::optional<std::chrono::system_clock::time_point> start_time_;
66  std::string reason_;
67  std::string who_banned_;
68  std::string group_;
69  std::string nick_;
70  static const std::string who_banned_default_;
71 
72 public:
73  banned(const std::string& ip,
74  const utils::optional<std::chrono::system_clock::time_point>& end_time,
75  const std::string& reason,
76  const std::string& who_banned = who_banned_default_,
77  const std::string& group = "",
78  const std::string& nick = "");
79 
80  banned(const config&);
81 
82  banned(const std::string& ip);
83 
84  void read(const config&);
85  void write(config&) const;
86 
87  const auto& get_end_time() const
88  { return end_time_; }
89 
90  std::string get_human_end_time() const;
91  std::string get_human_start_time() const;
92  std::string get_human_time_span() const;
93 
94  std::string get_reason() const
95  { return reason_; }
96 
97  std::string get_ip() const
98  { return ip_text_; }
99  std::string get_group() const
100  { return group_; }
101 
102  std::string get_who_banned() const
103  { return who_banned_; }
104 
105  std::string get_nick() const
106  { return nick_; }
107 
108  bool match_group(const std::string& group) const
109  { return group_ == group; }
110 
111  bool match_ip(const ip_mask& ip) const;
112  bool match_ipmask(const ip_mask& ip) const;
113 
114  unsigned int get_mask_ip(unsigned int) const;
115  unsigned int get_int_ip() const
116  { return ip_; }
117 
118  unsigned int mask() const
119  { return mask_; }
120 
121  bool operator>(const banned& b) const;
122 
123  struct error : public ::game::error {
124  error(const std::string& message) : ::game::error(message) {}
125  };
126 };
127 
129 {
134  std::string ban_help_;
135  std::string filename_;
136  bool dirty_;
137 
138  bool is_digit(const char& c) const
139  {
140  return c >= '0' && c <= '9';
141  }
142 
143  std::size_t to_digit(const char& c) const
144  {
145  return c - '0';
146  }
147 
148  void init_ban_help();
149  void check_ban_times(const std::chrono::system_clock::time_point& time_now);
150  inline void expire_bans()
151  {
152  check_ban_times(std::chrono::system_clock::now());
153  }
154 
155 public:
156  ban_manager();
157  ~ban_manager();
158 
159  void read();
160  void write();
161 
162  /**
163  * Parses the given duration and adds it to *time except if the
164  * duration is '0' or 'permanent' in which case *time will be set to '0'.
165  * @returns false if an invalid time modifier is encountered.
166  * *time is undefined in that case.
167  */
168  std::pair<bool, utils::optional<std::chrono::system_clock::time_point>> parse_time(
169  const std::string& duration, std::chrono::system_clock::time_point start_time) const;
170 
171  std::string ban(const std::string& ip,
172  const utils::optional<std::chrono::system_clock::time_point>& end_time,
173  const std::string& reason,
174  const std::string& who_banned,
175  const std::string& group,
176  const std::string& nick = "");
177 
178  void unban(std::ostringstream& os, const std::string& ip, bool immediate_write=true);
179  void unban_group(std::ostringstream& os, const std::string& group);
180 
181  void list_deleted_bans(std::ostringstream& out, const std::string& mask = "*") const;
182  void list_bans(std::ostringstream& out, const std::string& mask = "*");
183 
184  std::string is_ip_banned(const std::string& ip);
185 
186  const std::string& get_ban_help() const
187  { return ban_help_; }
188 
189  void load_config(const config&);
190 };
191 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
std::string ban(const std::string &ip, const utils::optional< std::chrono::system_clock::time_point > &end_time, const std::string &reason, const std::string &who_banned, const std::string &group, const std::string &nick="")
Definition: ban.cpp:490
void expire_bans()
Definition: ban.hpp:150
void list_bans(std::ostringstream &out, const std::string &mask="*")
Definition: ban.cpp:618
void unban(std::ostringstream &os, const std::string &ip, bool immediate_write=true)
Definition: ban.cpp:527
std::pair< bool, utils::optional< std::chrono::system_clock::time_point > > parse_time(const std::string &duration, std::chrono::system_clock::time_point start_time) const
Parses the given duration and adds it to *time except if the duration is '0' or 'permanent' in which ...
Definition: ban.cpp:332
void check_ban_times(const std::chrono::system_clock::time_point &time_now)
Definition: ban.cpp:566
default_ban_times ban_times_
Definition: ban.hpp:133
std::string filename_
Definition: ban.hpp:135
void unban_group(std::ostringstream &os, const std::string &group)
Definition: ban.cpp:554
bool is_digit(const char &c) const
Definition: ban.hpp:138
std::size_t to_digit(const char &c) const
Definition: ban.hpp:143
ban_time_queue time_queue_
Definition: ban.hpp:132
std::string ban_help_
Definition: ban.hpp:134
const std::string & get_ban_help() const
Definition: ban.hpp:186
std::string is_ip_banned(const std::string &ip)
Definition: ban.cpp:658
deleted_ban_list deleted_bans_
Definition: ban.hpp:131
void list_deleted_bans(std::ostringstream &out, const std::string &mask="*") const
Definition: ban.cpp:595
void load_config(const config &)
Definition: ban.cpp:698
void init_ban_help()
Definition: ban.cpp:674
utils::optional< std::chrono::system_clock::time_point > start_time_
Definition: ban.hpp:65
std::string group_
Definition: ban.hpp:68
bool match_ipmask(const ip_mask &ip) const
Definition: ban.cpp:264
std::string reason_
Definition: ban.hpp:66
std::string get_human_start_time() const
Definition: ban.cpp:219
unsigned int get_mask_ip(unsigned int) const
Definition: ban.cpp:253
std::string get_group() const
Definition: ban.hpp:99
unsigned int get_int_ip() const
Definition: ban.hpp:115
unsigned int mask() const
Definition: ban.hpp:118
void read(const config &)
Definition: ban.cpp:163
static const std::string who_banned_default_
Definition: ban.hpp:70
unsigned int ip_
Definition: ban.hpp:61
std::string get_reason() const
Definition: ban.hpp:94
std::string get_nick() const
Definition: ban.hpp:105
unsigned int mask_
Definition: ban.hpp:62
std::string nick_
Definition: ban.hpp:69
bool operator>(const banned &b) const
Definition: ban.cpp:247
void write(config &) const
Definition: ban.cpp:195
std::string get_who_banned() const
Definition: ban.hpp:102
banned(const std::string &ip, const utils::optional< std::chrono::system_clock::time_point > &end_time, const std::string &reason, const std::string &who_banned=who_banned_default_, const std::string &group="", const std::string &nick="")
Definition: ban.cpp:83
bool match_ip(const ip_mask &ip) const
Definition: ban.cpp:258
std::string get_human_end_time() const
Definition: ban.cpp:228
bool match_group(const std::string &group) const
Definition: ban.hpp:108
std::string get_human_time_span() const
Definition: ban.cpp:237
std::string get_ip() const
Definition: ban.hpp:97
std::string who_banned_
Definition: ban.hpp:67
utils::optional< std::chrono::system_clock::time_point > end_time_
Definition: ban.hpp:64
const auto & get_end_time() const
Definition: ban.hpp:87
std::string ip_text_
Definition: ban.hpp:63
std::ostream & operator<<(std::ostream &o, const banned &n)
Definition: ban.cpp:38
std::set< banned_ptr, banned_compare_subnet > ban_set
Definition: ban.hpp:52
std::map< std::string, std::chrono::seconds > default_ban_times
Definition: ban.hpp:55
std::priority_queue< banned_ptr, std::vector< banned_ptr >, banned_compare > ban_time_queue
Definition: ban.hpp:54
std::list< banned_ptr > deleted_ban_list
Definition: ban.hpp:53
std::shared_ptr< banned > banned_ptr
Definition: ban.hpp:34
ip_mask parse_ip(const std::string &ip)
Definition: ban.cpp:118
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:56
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
error(const std::string &message)
Definition: ban.hpp:124
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:55
static compare_fn active_
Definition: ban.hpp:49
bool(banned_compare_subnet::* compare_fn)(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.hpp:48
bool less(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:60
We want to move the lowest value to the top.
Definition: ban.hpp:38
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:48
mock_char c
static map_location::direction n
#define b