The Battle for Wesnoth  1.19.0-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 
20 #include <ctime>
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::size_t> 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  std::time_t end_time_;
65  std::time_t 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, const std::time_t end_time, const std::string& reason, const std::string& who_banned=who_banned_default_, const std::string& group="", const std::string& nick="");
74  banned(const config&);
75 
76  banned(const std::string& ip);
77 
78  void read(const config&);
79  void write(config&) const;
80 
81  std::time_t get_end_time() const
82  { return end_time_; }
83 
84  std::string get_human_end_time() const;
85  std::string get_human_start_time() const;
86  std::string get_human_time_span() const;
87  static std::string get_human_time(const std::time_t&);
88 
89  std::string get_reason() const
90  { return reason_; }
91 
92  std::string get_ip() const
93  { return ip_text_; }
94  std::string get_group() const
95  { return group_; }
96 
97  std::string get_who_banned() const
98  { return who_banned_; }
99 
100  std::string get_nick() const
101  { return nick_; }
102 
103  bool match_group(const std::string& group) const
104  { return group_ == group; }
105 
106  bool match_ip(const ip_mask& ip) const;
107  bool match_ipmask(const ip_mask& ip) const;
108 
109  unsigned int get_mask_ip(unsigned int) const;
110  unsigned int get_int_ip() const
111  { return ip_; }
112 
113  unsigned int mask() const
114  { return mask_; }
115 
116  bool operator>(const banned& b) const;
117 
118  struct error : public ::game::error {
119  error(const std::string& message) : ::game::error(message) {}
120  };
121 };
122 
124 {
129  std::string ban_help_;
130  std::string filename_;
131  bool dirty_;
132 
133  bool is_digit(const char& c) const
134  {
135  return c >= '0' && c <= '9';
136  }
137 
138  std::size_t to_digit(const char& c) const
139  {
140  return c - '0';
141  }
142 
143  void init_ban_help();
144  void check_ban_times(std::time_t time_now);
145  inline void expire_bans()
146  {
147  check_ban_times(std::time(nullptr));
148  }
149 
150 public:
151  ban_manager();
152  ~ban_manager();
153 
154  void read();
155  void write();
156 
157  /**
158  * Parses the given duration and adds it to *time except if the
159  * duration is '0' or 'permanent' in which case *time will be set to '0'.
160  * @returns false if an invalid time modifier is encountered.
161  * *time is undefined in that case.
162  */
163  bool parse_time(const std::string& duration, std::time_t* time) const;
164 
165  std::string ban(const std::string&, const std::time_t&, const std::string&, const std::string&, const std::string&, const std::string& = "");
166  void unban(std::ostringstream& os, const std::string& ip, bool immediate_write=true);
167  void unban_group(std::ostringstream& os, const std::string& group);
168 
169  void list_deleted_bans(std::ostringstream& out, const std::string& mask = "*") const;
170  void list_bans(std::ostringstream& out, const std::string& mask = "*");
171 
172  std::string is_ip_banned(const std::string& ip);
173 
174  const std::string& get_ban_help() const
175  { return ban_help_; }
176 
177  void load_config(const config&);
178 };
179 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::string ban(const std::string &, const std::time_t &, const std::string &, const std::string &, const std::string &, const std::string &="")
Definition: ban.cpp:495
void expire_bans()
Definition: ban.hpp:145
bool parse_time(const std::string &duration, std::time_t *time) const
Parses the given duration and adds it to *time except if the duration is '0' or 'permanent' in which ...
Definition: ban.cpp:333
void list_bans(std::ostringstream &out, const std::string &mask="*")
Definition: ban.cpp:617
void unban(std::ostringstream &os, const std::string &ip, bool immediate_write=true)
Definition: ban.cpp:532
default_ban_times ban_times_
Definition: ban.hpp:128
std::string filename_
Definition: ban.hpp:130
void unban_group(std::ostringstream &os, const std::string &group)
Definition: ban.cpp:559
bool is_digit(const char &c) const
Definition: ban.hpp:133
std::size_t to_digit(const char &c) const
Definition: ban.hpp:138
ban_time_queue time_queue_
Definition: ban.hpp:127
std::string ban_help_
Definition: ban.hpp:129
const std::string & get_ban_help() const
Definition: ban.hpp:174
void check_ban_times(std::time_t time_now)
Definition: ban.cpp:571
std::string is_ip_banned(const std::string &ip)
Definition: ban.cpp:657
deleted_ban_list deleted_bans_
Definition: ban.hpp:126
void list_deleted_bans(std::ostringstream &out, const std::string &mask="*") const
Definition: ban.cpp:594
void load_config(const config &)
Definition: ban.cpp:697
void init_ban_help()
Definition: ban.cpp:673
banned(const std::string &ip, const std::time_t 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:82
std::string group_
Definition: ban.hpp:68
static std::string get_human_time(const std::time_t &)
bool match_ipmask(const ip_mask &ip) const
Definition: ban.cpp:265
std::string reason_
Definition: ban.hpp:66
std::time_t get_end_time() const
Definition: ban.hpp:81
std::string get_human_start_time() const
Definition: ban.cpp:222
unsigned int get_mask_ip(unsigned int) const
Definition: ban.cpp:254
std::string get_group() const
Definition: ban.hpp:94
unsigned int get_int_ip() const
Definition: ban.hpp:110
unsigned int mask() const
Definition: ban.hpp:113
void read(const config &)
Definition: ban.cpp:162
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:89
std::string get_nick() const
Definition: ban.hpp:100
unsigned int mask_
Definition: ban.hpp:62
std::string nick_
Definition: ban.hpp:69
bool operator>(const banned &b) const
Definition: ban.cpp:249
void write(config &) const
Definition: ban.cpp:194
std::string get_who_banned() const
Definition: ban.hpp:97
std::time_t end_time_
Definition: ban.hpp:64
bool match_ip(const ip_mask &ip) const
Definition: ban.cpp:259
std::string get_human_end_time() const
Definition: ban.cpp:231
bool match_group(const std::string &group) const
Definition: ban.hpp:103
std::string get_human_time_span() const
Definition: ban.cpp:240
std::string get_ip() const
Definition: ban.hpp:92
std::string who_banned_
Definition: ban.hpp:67
std::time_t start_time_
Definition: ban.hpp:65
std::string ip_text_
Definition: ban.hpp:63
std::ostream & operator<<(std::ostream &o, const banned &n)
Definition: ban.cpp:37
std::set< banned_ptr, banned_compare_subnet > ban_set
Definition: ban.hpp:52
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:117
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:56
std::map< std::string, std::size_t > default_ban_times
Definition: ban.hpp:55
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:119
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:54
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:59
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:47
mock_char c
static map_location::DIRECTION n
#define a
#define b