The Battle for Wesnoth  1.15.2+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 <set>
20 #include <map>
21 #include <list>
22 #include <queue>
23 #include <ctime>
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. */
36  struct banned_compare {
37  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
38  };
39 
41  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
42  private:
43  bool less(const banned_ptr& a, const banned_ptr& b) const;
44  typedef bool (banned_compare_subnet::*compare_fn)(const banned_ptr& a, const banned_ptr& b) const;
45  static compare_fn active_;
46  };
47 
48  typedef std::set<banned_ptr,banned_compare_subnet > ban_set;
49  typedef std::list<banned_ptr> deleted_ban_list;
50  typedef std::priority_queue<banned_ptr,std::vector<banned_ptr>, banned_compare> ban_time_queue;
51  typedef std::map<std::string, std::size_t> default_ban_times;
52  typedef std::pair<unsigned int, unsigned int> ip_mask;
53 
54  ip_mask parse_ip(const std::string&);
55 
56  class banned {
57  unsigned int ip_;
58  unsigned int mask_;
59  std::string ip_text_;
60  std::time_t end_time_;
61  std::time_t start_time_;
62  std::string reason_;
63  std::string who_banned_;
64  std::string group_;
65  std::string nick_;
66  static const std::string who_banned_default_;
67 
68  banned(const std::string& ip);
69 
70  public:
71  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="");
72  banned(const config&);
73 
74  void read(const config&);
75  void write(config&) const;
76 
77  std::time_t get_end_time() const
78  { return end_time_; }
79 
80  std::string get_human_end_time() const;
81  std::string get_human_start_time() const;
82  std::string get_human_time_span() const;
83  static std::string get_human_time(const std::time_t&);
84 
85  std::string get_reason() const
86  { return reason_; }
87 
88  std::string get_ip() const
89  { return ip_text_; }
90  std::string get_group() const
91  { return group_; }
92 
93  std::string get_who_banned() const
94  { return who_banned_; }
95 
96  std::string get_nick() const
97  { return nick_; }
98 
99  bool match_group(const std::string& group) const
100  { return group_ == group; }
101 
102  bool match_ip(const ip_mask& ip) const;
103  bool match_ipmask(const ip_mask& ip) const;
104 
105  unsigned int get_mask_ip(unsigned int) const;
106  unsigned int get_int_ip() const
107  { return ip_; }
108 
109  unsigned int mask() const
110  { return mask_; }
111 
112  static banned_ptr create_dummy(const std::string& ip);
113 
114  bool operator>(const banned& b) const;
115 
116  struct error : public ::game::error {
117  error(const std::string& message) : ::game::error(message) {}
118  };
119  };
120 
122  {
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  { return c >= '0' && c <= '9'; }
134  std::size_t to_digit(const char& c) const
135  { return c - '0'; }
136 
137  void init_ban_help();
138  void check_ban_times(std::time_t time_now);
139  inline void expire_bans() {
140  check_ban_times(std::time(nullptr));
141  }
142  public:
143  ban_manager();
144  ~ban_manager();
145 
146  void read();
147  void write();
148 
149  /**
150  * Parses the given duration and adds it to *time except if the
151  * duration is '0' or 'permanent' in which case *time will be set to '0'.
152  * @returns false if an invalid time modifier is encountered.
153  * *time is undefined in that case.
154  */
155  bool parse_time(const std::string& duration, std::time_t* time) const;
156 
157  std::string ban(const std::string&, const std::time_t&, const std::string&, const std::string&, const std::string&, const std::string& = "");
158  void unban(std::ostringstream& os, const std::string& ip, bool immediate_write=true);
159  void unban_group(std::ostringstream& os, const std::string& group);
160 
161 
162  void list_deleted_bans(std::ostringstream& out, const std::string& mask = "*") const;
163  void list_bans(std::ostringstream& out, const std::string& mask = "*");
164 
165  std::string is_ip_banned(const std::string& ip);
166 
167  const std::string& get_ban_help() const
168  { return ban_help_; }
169 
170  void load_config(const config&);
171 
172  };
173 }
std::string reason_
Definition: ban.hpp:62
std::string get_reason() const
Definition: ban.hpp:85
std::list< banned_ptr > deleted_ban_list
Definition: ban.hpp:49
default_ban_times ban_times_
Definition: ban.hpp:127
std::time_t start_time_
Definition: ban.hpp:61
bool is_digit(const char &c) const
Definition: ban.hpp:132
#define a
unsigned int get_int_ip() const
Definition: ban.hpp:106
std::time_t get_end_time() const
Definition: ban.hpp:77
deleted_ban_list deleted_bans_
Definition: ban.hpp:125
bool match_group(const std::string &group) const
Definition: ban.hpp:99
std::set< banned_ptr, banned_compare_subnet > ban_set
Definition: ban.hpp:48
std::string group_
Definition: ban.hpp:64
static compare_fn active_
Definition: ban.hpp:45
#define b
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:52
std::shared_ptr< banned > banned_ptr
Definition: ban.hpp:33
std::string get_who_banned() const
Definition: ban.hpp:93
void write(std::ostream &out, const configr_of &cfg, unsigned int level)
Definition: parser.cpp:762
std::string nick_
Definition: ban.hpp:65
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:625
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:46
std::time_t end_time_
Definition: ban.hpp:60
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:122
std::size_t to_digit(const char &c) const
Definition: ban.hpp:134
unsigned int mask() const
Definition: ban.hpp:109
ban_time_queue time_queue_
Definition: ban.hpp:126
std::string get_group() const
Definition: ban.hpp:90
std::string who_banned_
Definition: ban.hpp:63
std::ostream & operator<<(std::ostream &o, const banned &n)
Definition: ban.cpp:36
std::string get_nick() const
Definition: ban.hpp:96
std::string ban_help_
Definition: ban.hpp:128
static const std::string who_banned_default_
Definition: ban.hpp:66
std::priority_queue< banned_ptr, std::vector< banned_ptr >, banned_compare > ban_time_queue
Definition: ban.hpp:50
void expire_bans()
Definition: ban.hpp:139
unsigned int ip_
Definition: ban.hpp:57
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string get_ip() const
Definition: ban.hpp:88
Definition: ban.cpp:28
std::string filename_
Definition: ban.hpp:129
error(const std::string &message)
Definition: ban.hpp:117
std::map< std::string, std::size_t > default_ban_times
Definition: ban.hpp:51
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
const std::string & get_ban_help() const
Definition: ban.hpp:167
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:59
unsigned int mask_
Definition: ban.hpp:58