The Battle for Wesnoth  1.19.8+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  /** Returns the seconds remaining until than ban expires, or nullopt if permanent. */
91  utils::optional<std::chrono::seconds> get_remaining_ban_time() const;
92 
93  std::string get_human_end_time() const;
94  std::string get_human_start_time() const;
95 
96  std::string get_reason() const
97  { return reason_; }
98 
99  std::string get_ip() const
100  { return ip_text_; }
101  std::string get_group() const
102  { return group_; }
103 
104  std::string get_who_banned() const
105  { return who_banned_; }
106 
107  std::string get_nick() const
108  { return nick_; }
109 
110  bool match_group(const std::string& group) const
111  { return group_ == group; }
112 
113  bool match_ip(const ip_mask& ip) const;
114  bool match_ipmask(const ip_mask& ip) const;
115 
116  unsigned int get_mask_ip(unsigned int) const;
117  unsigned int get_int_ip() const
118  { return ip_; }
119 
120  unsigned int mask() const
121  { return mask_; }
122 
123  bool operator>(const banned& b) const;
124 
125  struct error : public ::game::error {
126  error(const std::string& message) : ::game::error(message) {}
127  };
128 };
129 
131 {
136  std::string ban_help_;
137  std::string filename_;
138  bool dirty_;
139 
140  bool is_digit(const char& c) const
141  {
142  return c >= '0' && c <= '9';
143  }
144 
145  std::size_t to_digit(const char& c) const
146  {
147  return c - '0';
148  }
149 
150  void init_ban_help();
151  void check_ban_times(const std::chrono::system_clock::time_point& time_now);
152  inline void expire_bans()
153  {
154  check_ban_times(std::chrono::system_clock::now());
155  }
156 
157 public:
158  ban_manager();
159  ~ban_manager();
160 
161  void read();
162  void write();
163 
164  /**
165  * Parses the given duration and adds it to *time except if the
166  * duration is '0' or 'permanent' in which case *time will be set to '0'.
167  * @returns false if an invalid time modifier is encountered.
168  * *time is undefined in that case.
169  */
170  std::pair<bool, utils::optional<std::chrono::system_clock::time_point>> parse_time(
171  const std::string& duration, std::chrono::system_clock::time_point start_time) const;
172 
173  std::string ban(const std::string& ip,
174  const utils::optional<std::chrono::system_clock::time_point>& end_time,
175  const std::string& reason,
176  const std::string& who_banned,
177  const std::string& group,
178  const std::string& nick = "");
179 
180  void unban(std::ostringstream& os, const std::string& ip, bool immediate_write=true);
181  void unban_group(std::ostringstream& os, const std::string& group);
182 
183  void list_deleted_bans(std::ostringstream& out, const std::string& mask = "*") const;
184  void list_bans(std::ostringstream& out, const std::string& mask = "*");
185 
186  banned_ptr get_ban_info(const std::string& ip);
187 
188  const std::string& get_ban_help() const
189  { return ban_help_; }
190 
191  void load_config(const config&);
192 };
193 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
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:152
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:135
banned_ptr get_ban_info(const std::string &ip)
Definition: ban.cpp:658
std::string filename_
Definition: ban.hpp:137
void unban_group(std::ostringstream &os, const std::string &group)
Definition: ban.cpp:554
bool is_digit(const char &c) const
Definition: ban.hpp:140
std::size_t to_digit(const char &c) const
Definition: ban.hpp:145
ban_time_queue time_queue_
Definition: ban.hpp:134
std::string ban_help_
Definition: ban.hpp:136
const std::string & get_ban_help() const
Definition: ban.hpp:188
deleted_ban_list deleted_bans_
Definition: ban.hpp:133
void list_deleted_bans(std::ostringstream &out, const std::string &mask="*") const
Definition: ban.cpp:595
void load_config(const config &)
Definition: ban.cpp:696
void init_ban_help()
Definition: ban.cpp:672
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
utils::optional< std::chrono::seconds > get_remaining_ban_time() const
Returns the seconds remaining until than ban expires, or nullopt if permanent.
Definition: ban.cpp:219
std::string get_human_start_time() const
Definition: ban.cpp:229
unsigned int get_mask_ip(unsigned int) const
Definition: ban.cpp:253
std::string get_group() const
Definition: ban.hpp:101
unsigned int get_int_ip() const
Definition: ban.hpp:117
unsigned int mask() const
Definition: ban.hpp:120
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:96
std::string get_nick() const
Definition: ban.hpp:107
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:104
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:238
bool match_group(const std::string &group) const
Definition: ban.hpp:110
std::string get_ip() const
Definition: ban.hpp:99
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:126
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