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