The Battle for Wesnoth  1.17.0-dev
ban.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
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;
49  static compare_fn active_;
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 {
125  ban_set bans_;
126  deleted_ban_list deleted_bans_;
127  ban_time_queue time_queue_;
128  default_ban_times ban_times_;
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 }
std::string reason_
Definition: ban.hpp:66
std::string get_reason() const
Definition: ban.hpp:89
std::list< banned_ptr > deleted_ban_list
Definition: ban.hpp:53
default_ban_times ban_times_
Definition: ban.hpp:128
std::time_t start_time_
Definition: ban.hpp:65
bool is_digit(const char &c) const
Definition: ban.hpp:133
#define a
unsigned int get_int_ip() const
Definition: ban.hpp:110
std::time_t get_end_time() const
Definition: ban.hpp:81
deleted_ban_list deleted_bans_
Definition: ban.hpp:126
bool match_group(const std::string &group) const
Definition: ban.hpp:103
std::string group_
Definition: ban.hpp:68
static compare_fn active_
Definition: ban.hpp:49
#define b
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:56
std::shared_ptr< banned > banned_ptr
Definition: ban.hpp:34
std::string get_who_banned() const
Definition: ban.hpp:97
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
Definition: parser.cpp:764
std::string nick_
Definition: ban.hpp:69
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:46
std::time_t end_time_
Definition: ban.hpp:64
We want to move the lowest value to the top.
Definition: ban.hpp:37
ip_mask parse_ip(const std::string &ip)
Definition: ban.cpp:116
std::size_t to_digit(const char &c) const
Definition: ban.hpp:138
unsigned int mask() const
Definition: ban.hpp:113
ban_time_queue time_queue_
Definition: ban.hpp:127
std::string get_group() const
Definition: ban.hpp:94
std::string who_banned_
Definition: ban.hpp:67
std::ostream & operator<<(std::ostream &o, const banned &n)
Definition: ban.cpp:36
std::string get_nick() const
Definition: ban.hpp:100
std::string ban_help_
Definition: ban.hpp:129
std::set< banned_ptr, banned_compare_subnet > ban_set
Definition: ban.hpp:52
static const std::string who_banned_default_
Definition: ban.hpp:70
void expire_bans()
Definition: ban.hpp:145
unsigned int ip_
Definition: ban.hpp:61
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:28
std::string get_ip() const
Definition: ban.hpp:92
std::priority_queue< banned_ptr, std::vector< banned_ptr >, banned_compare > ban_time_queue
Definition: ban.hpp:54
std::string filename_
Definition: ban.hpp:130
error(const std::string &message)
Definition: ban.hpp:119
std::map< std::string, std::size_t > default_ban_times
Definition: ban.hpp:55
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
mock_char c
const std::string & get_ban_help() const
Definition: ban.hpp:174
static map_location::DIRECTION n
bool operator>(const version_info &l, const version_info &r)
Greater-than operator for version_info.
std::string ip_text_
Definition: ban.hpp:63
unsigned int mask_
Definition: ban.hpp:62