ban.hpp

Go to the documentation of this file.
00001 /* $Id: ban.hpp 26653 2008-05-16 16:08:26Z suokko $ */
00002 /*
00003    Copyright (C) 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #ifndef SERVER_GAME_HPP_INCLUDED
00016 #define SERVER_GAME_HPP_INCLUDED
00017 #include <map>
00018 #include <queue>
00019 #include <ctime>
00020 
00021 class config;
00022 
00023 namespace wesnothd {
00024 
00025     class banned;
00026 
00027     //! We want to move the lowest value to the top
00028     struct banned_compare {
00029         bool operator()(const banned* a, const banned* b) const;
00030     };
00031 
00032     typedef std::map<std::string, banned*> ban_map;
00033     typedef std::priority_queue<banned*,std::vector<banned*>, banned_compare> ban_time_queue;
00034     typedef std::map<std::string, size_t> default_ban_times;
00035 
00036 
00037     class banned {
00038         std::string ip_;
00039         time_t end_time_;
00040         std::string reason_;
00041         bool deleted_;
00042 
00043         banned() {}
00044 
00045     public:
00046         banned(const std::string& ip, const time_t end_time, const std::string& reason);
00047         banned(const config&);
00048 
00049         void read(const config&);
00050         void write(config&) const;
00051 
00052         time_t get_end_time() const
00053         { return end_time_; }
00054 
00055         std::string get_human_end_time() const;
00056 
00057         std::string get_reason() const
00058         { return reason_; }
00059 
00060         std::string get_ip() const 
00061         { return ip_; }
00062 
00063         void remove_ban()
00064         { deleted_ = true; }
00065 
00066         bool is_deleted() const
00067         { return deleted_; }
00068 
00069         //! Notice that comparision is done wrong way to make the smallest value in top of heap
00070         bool operator>(const banned& b) const;
00071     };
00072 
00073     class ban_manager
00074     {
00075 
00076         ban_map bans_;
00077         ban_time_queue time_queue_;
00078         default_ban_times ban_times_;
00079         std::string ban_help_;
00080 
00081         bool is_number(const char& c) const
00082         { return c >= '0' && c <= '9'; }
00083         size_t to_number(const char& c) const
00084         { return c - '0'; }
00085 
00086         void init_ban_help();
00087     public:
00088         ban_manager();
00089         ~ban_manager();
00090         
00091         void read(const config&);
00092         void write(config&) const;
00093 
00094         time_t parse_time(std::string time_in) const;
00095 
00096         void ban(const std::string&, const time_t&, const std::string&);
00097         void unban(std::ostringstream& os, const std::string& ip);
00098 
00099         void check_ban_times(time_t time_now);
00100 
00101         void list_bans(std::ostringstream& out) const;
00102 
00103         bool is_ip_banned(std::string ip) const;
00104         
00105         const std::string& get_ban_help() const
00106         { return ban_help_; }   
00107 
00108         void set_default_ban_times(const config&);
00109 
00110     };
00111 }
00112 
00113 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs