server/ban.hpp

Go to the documentation of this file.
00001 /* $Id: ban.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Pauli Nieminen <paniemin@cc.hut.fi>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef SERVER_GAME_HPP_INCLUDED
00017 #define SERVER_GAME_HPP_INCLUDED
00018 #include "exceptions.hpp"
00019 
00020 #include <set>
00021 #include <map>
00022 #include <list>
00023 #include <queue>
00024 #include <ctime>
00025 
00026 #include <boost/shared_ptr.hpp>
00027 
00028 class config;
00029 
00030 namespace wesnothd {
00031 
00032     class banned;
00033 
00034     std::ostream& operator<<(std::ostream& o, const banned& n);
00035 
00036     typedef boost::shared_ptr<banned> banned_ptr;
00037 
00038     /** We want to move the lowest value to the top. */
00039     struct banned_compare {
00040         bool operator()(const banned_ptr& a, const banned_ptr& b) const;
00041     };
00042 
00043     struct banned_compare_subnet {
00044         bool operator()(const banned_ptr& a, const banned_ptr& b) const;
00045         private:
00046         bool less(const banned_ptr& a, const banned_ptr& b) const;
00047         typedef bool (banned_compare_subnet::*compare_fn)(const banned_ptr& a, const banned_ptr& b) const;
00048         static compare_fn active_;
00049     };
00050 
00051     typedef std::set<banned_ptr,banned_compare_subnet > ban_set;
00052     typedef std::list<banned_ptr> deleted_ban_list;
00053     typedef std::priority_queue<banned_ptr,std::vector<banned_ptr>, banned_compare> ban_time_queue;
00054     typedef std::map<std::string, size_t> default_ban_times;
00055     typedef std::pair<unsigned int, unsigned int> ip_mask;
00056 
00057     ip_mask parse_ip(const std::string&);
00058 
00059     class banned {
00060         unsigned int ip_;
00061         unsigned int mask_;
00062         std::string ip_text_;
00063         time_t end_time_;
00064         time_t start_time_;
00065         std::string reason_;
00066         std::string who_banned_;
00067         std::string group_;
00068         std::string nick_;
00069         static const std::string who_banned_default_;
00070 
00071         banned(const std::string& ip);
00072 
00073     public:
00074         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="");
00075         banned(const config&);
00076 
00077         void read(const config&);
00078         void write(config&) const;
00079 
00080         time_t get_end_time() const
00081         { return end_time_; }
00082 
00083         std::string get_human_end_time() const;
00084         std::string get_human_start_time() const;
00085         static std::string get_human_time(const time_t&);
00086 
00087         std::string get_reason() const
00088         { return reason_; }
00089 
00090         std::string get_ip() const
00091         { return ip_text_; }
00092         std::string get_group() const
00093         { return group_; }
00094 
00095         std::string get_who_banned() const
00096         { return who_banned_; }
00097 
00098         std::string get_nick() const
00099         { return nick_; }
00100 
00101         bool match_group(const std::string& group) const
00102         { return group_ == group; }
00103 
00104         bool match_ip(const ip_mask& ip) const;
00105         bool match_ipmask(const ip_mask& ip) const;
00106 
00107         unsigned int get_mask_ip(unsigned int) const;
00108         unsigned int get_int_ip() const
00109         { return ip_; }
00110 
00111         unsigned int mask() const
00112         { return mask_; }
00113 
00114         static banned_ptr create_dummy(const std::string& ip);
00115 
00116         bool operator>(const banned& b) const;
00117 
00118         struct error : public game::error {
00119             error(const std::string& message) : game::error(message) {}
00120         };
00121     };
00122 
00123     class ban_manager
00124     {
00125 
00126         ban_set bans_;
00127         deleted_ban_list deleted_bans_;
00128         ban_time_queue time_queue_;
00129         default_ban_times ban_times_;
00130         std::string ban_help_;
00131         std::string filename_;
00132         bool dirty_;
00133 
00134         bool is_digit(const char& c) const
00135         { return c >= '0' && c <= '9'; }
00136         size_t to_digit(const char& c) const
00137         { return c - '0'; }
00138 
00139         void init_ban_help();
00140     public:
00141         ban_manager();
00142         ~ban_manager();
00143 
00144         void read();
00145         void write();
00146 
00147         /**
00148          * Parses the given duration and adds it to *time except if the
00149          * duration is '0' or 'permanent' in which case *time will be set to '0'.
00150          * @returns false if an invalid time modifier is encountered.
00151          * *time is undefined in that case.
00152          */
00153         bool parse_time(const std::string& duration, time_t* time) const;
00154 
00155         std::string ban(const std::string&, const time_t&, const std::string&, const std::string&, const std::string&, const std::string& = "");
00156         void unban(std::ostringstream& os, const std::string& ip);
00157         void unban_group(std::ostringstream& os, const std::string& group);
00158 
00159 
00160         void check_ban_times(time_t time_now);
00161 
00162         void list_deleted_bans(std::ostringstream& out, const std::string& mask = "*") const;
00163         void list_bans(std::ostringstream& out, const std::string& mask = "*") const;
00164 
00165         std::string is_ip_banned(const std::string& ip) const;
00166 
00167         const std::string& get_ban_help() const
00168         { return ban_help_; }
00169 
00170         void load_config(const config&);
00171 
00172     };
00173 }
00174 
00175 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:55 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs