The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ban.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Pauli Nieminen <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef SERVER_GAME_HPP_INCLUDED
16 #define SERVER_GAME_HPP_INCLUDED
17 #include "exceptions.hpp"
18 
19 #include <set>
20 #include <map>
21 #include <list>
22 #include <queue>
23 #include <ctime>
24 
25 #include <boost/shared_ptr.hpp>
26 
27 class config;
28 
29 namespace wesnothd {
30 
31  class banned;
32 
33  std::ostream& operator<<(std::ostream& o, const banned& n);
34 
36 
37  /** We want to move the lowest value to the top. */
38  struct banned_compare {
39  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
40  };
41 
43  bool operator()(const banned_ptr& a, const banned_ptr& b) const;
44  private:
45  bool less(const banned_ptr& a, const banned_ptr& b) const;
46  typedef bool (banned_compare_subnet::*compare_fn)(const banned_ptr& a, const banned_ptr& b) const;
48  };
49 
50  typedef std::set<banned_ptr,banned_compare_subnet > ban_set;
51  typedef std::list<banned_ptr> deleted_ban_list;
52  typedef std::priority_queue<banned_ptr,std::vector<banned_ptr>, banned_compare> ban_time_queue;
53  typedef std::map<std::string, size_t> default_ban_times;
54  typedef std::pair<unsigned int, unsigned int> ip_mask;
55 
56  ip_mask parse_ip(const std::string&);
57 
58  class banned {
59  unsigned int ip_;
60  unsigned int mask_;
62  time_t end_time_;
63  time_t start_time_;
69 
70  banned(const std::string& ip);
71 
72  public:
73  banned(const std::string& ip, const 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  void read(const config&);
77  void write(config&) const;
78 
79  time_t get_end_time() const
80  { return end_time_; }
81 
85  static std::string get_human_time(const time_t&);
86 
88  { return reason_; }
89 
91  { return ip_text_; }
93  { return group_; }
94 
96  { return who_banned_; }
97 
99  { return nick_; }
100 
101  bool match_group(const std::string& group) const
102  { return group_ == group; }
103 
104  bool match_ip(const ip_mask& ip) const;
105  bool match_ipmask(const ip_mask& ip) const;
106 
107  unsigned int get_mask_ip(unsigned int) const;
108  unsigned int get_int_ip() const
109  { return ip_; }
110 
111  unsigned int mask() const
112  { return mask_; }
113 
114  static banned_ptr create_dummy(const std::string& ip);
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 
126  ban_set bans_;
127  deleted_ban_list deleted_bans_;
128  ban_time_queue time_queue_;
129  default_ban_times ban_times_;
132  bool dirty_;
133 
134  bool is_digit(const char& c) const
135  { return c >= '0' && c <= '9'; }
136  size_t to_digit(const char& c) const
137  { return c - '0'; }
138 
139  void init_ban_help();
140  public:
141  ban_manager();
142  ~ban_manager();
143 
144  void read();
145  void write();
146 
147  /**
148  * Parses the given duration and adds it to *time except if the
149  * duration is '0' or 'permanent' in which case *time will be set to '0'.
150  * @returns false if an invalid time modifier is encountered.
151  * *time is undefined in that case.
152  */
153  bool parse_time(const std::string& duration, time_t* time) const;
154 
155  std::string ban(const std::string&, const time_t&, const std::string&, const std::string&, const std::string&, const std::string& = "");
156  void unban(std::ostringstream& os, const std::string& ip);
157  void unban_group(std::ostringstream& os, const std::string& group);
158 
159 
160  void check_ban_times(time_t time_now);
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 = "*") const;
164 
165  std::string is_ip_banned(const std::string& ip) const;
166 
167  const std::string& get_ban_help() const
168  { return ban_help_; }
169 
170  void load_config(const config&);
171 
172  };
173 }
174 
175 #endif
size_t to_digit(const char &c) const
Definition: ban.hpp:136
std::string reason_
Definition: ban.hpp:64
std::list< banned_ptr > deleted_ban_list
Definition: ban.hpp:51
unsigned int get_mask_ip(unsigned int) const
Definition: ban.cpp:247
default_ban_times ban_times_
Definition: ban.hpp:129
void list_deleted_bans(std::ostringstream &out, const std::string &mask="*") const
Definition: ban.cpp:584
void load_config(const config &)
Definition: ban.cpp:696
bool match_group(const std::string &group) const
Definition: ban.hpp:101
std::string ban(const std::string &, const time_t &, const std::string &, const std::string &, const std::string &, const std::string &="")
Definition: ban.cpp:490
const GLfloat * c
Definition: glew.h:12741
std::string get_human_time_span() const
Definition: ban.cpp:233
unsigned int mask() const
Definition: ban.hpp:111
unsigned int get_int_ip() const
Definition: ban.hpp:108
deleted_ban_list deleted_bans_
Definition: ban.hpp:127
bool(banned_compare_subnet::* compare_fn)(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.hpp:46
std::string get_human_start_time() const
Definition: ban.cpp:217
static std::string get_human_time(const time_t &)
std::set< banned_ptr, banned_compare_subnet > ban_set
Definition: ban.hpp:50
std::string group_
Definition: ban.hpp:66
bool match_ip(const ip_mask &ip) const
Definition: ban.cpp:252
static compare_fn active_
Definition: ban.hpp:47
std::string is_ip_banned(const std::string &ip) const
Definition: ban.cpp:655
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:54
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void read(const config &)
Definition: ban.cpp:166
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:53
time_t get_end_time() const
Definition: ban.hpp:79
bool parse_time(const std::string &duration, 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:326
std::string nick_
Definition: ban.hpp:67
void unban_group(std::ostringstream &os, const std::string &group)
Definition: ban.cpp:549
boost::shared_ptr< banned > banned_ptr
Definition: ban.hpp:35
bool less(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:58
std::string get_group() const
Definition: ban.hpp:92
std::string get_human_end_time() const
Definition: ban.cpp:224
We want to move the lowest value to the top.
Definition: ban.hpp:38
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
ip_mask parse_ip(const std::string &ip)
Definition: ban.cpp:122
GLenum GLint GLuint mask
Definition: glew.h:1813
banned(const std::string &ip)
Definition: ban.cpp:71
bool match_ipmask(const ip_mask &ip) const
Definition: ban.cpp:257
ban_time_queue time_queue_
Definition: ban.hpp:128
static banned_ptr create_dummy(const std::string &ip)
Definition: ban.cpp:65
std::string get_nick() const
Definition: ban.hpp:98
std::string who_banned_
Definition: ban.hpp:65
void unban(std::ostringstream &os, const std::string &ip)
Definition: ban.cpp:524
std::ostream & operator<<(std::ostream &o, const banned &n)
Definition: ban.cpp:36
std::string ban_help_
Definition: ban.hpp:130
bool operator>(const banned &b) const
Definition: ban.cpp:242
static const std::string who_banned_default_
Definition: ban.hpp:68
void list_bans(std::ostringstream &out, const std::string &mask="*") const
Definition: ban.cpp:612
std::priority_queue< banned_ptr, std::vector< banned_ptr >, banned_compare > ban_time_queue
Definition: ban.hpp:52
unsigned int ip_
Definition: ban.hpp:59
const std::string & get_ban_help() const
Definition: ban.hpp:167
GLclampd n
Definition: glew.h:5903
void init_ban_help()
Definition: ban.cpp:669
void check_ban_times(time_t time_now)
Definition: ban.cpp:560
GLboolean GLuint group
Definition: glew.h:2589
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
Definition: ban.cpp:28
std::string get_who_banned() const
Definition: ban.hpp:95
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
time_t end_time_
Definition: ban.hpp:62
std::string filename_
Definition: ban.hpp:131
error(const std::string &message)
Definition: ban.hpp:119
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
time_t start_time_
Definition: ban.hpp:63
std::map< std::string, size_t > default_ban_times
Definition: ban.hpp:53
std::string ip_text_
Definition: ban.hpp:61
void write(config &) const
Definition: ban.cpp:189
std::string get_reason() const
Definition: ban.hpp:87
unsigned int mask_
Definition: ban.hpp:60
bool operator()(const banned_ptr &a, const banned_ptr &b) const
Definition: ban.cpp:46
bool is_digit(const char &c) const
Definition: ban.hpp:134
std::string get_ip() const
Definition: ban.hpp:90