Author: baufo
Date: Thu Mar 20 19:43:01 2008
New Revision: 24905

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24905&view=rev
Log:
Adding a clean up routine

Modified:
    branches/mp_registration/src/menu_events.cpp
    branches/mp_registration/src/server/server.cpp
    branches/mp_registration/src/server/user_handler.cpp
    branches/mp_registration/src/server/user_handler.hpp

Modified: branches/mp_registration/src/menu_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/menu_events.cpp?rev=24905&r1=24904&r2=24905&view=diff
==============================================================================
--- branches/mp_registration/src/menu_events.cpp (original)
+++ branches/mp_registration/src/menu_events.cpp Thu Mar 20 19:43:01 2008
@@ -1887,6 +1887,9 @@
                    }
                    out = "registering with password *** and " + (arg2.empty() 
? "no email address" : "email address " + arg2);
 
+               } else if (cmd == "drop") {
+                   nickserv.add_child("drop");
+                   out = "dropping your username";
                } else if (cmd == "info") {
                    nickserv.add_child("info")["name"] = arg1;
                    out = "requesting information for user " + arg1;
@@ -1956,7 +1959,7 @@
                                || cmd == "mute" || cmd == "muteall" || cmd == 
"ping")
                {
                        send_command(cmd, (argc > 1) ? arg1 + " " + arg2 : 
arg1);
-               } else if (cmd == "nickserv" && argc > 1) {
+               } else if (cmd == "nickserv" && (argc > 1 || (argc > 0 && arg1 
== "drop"))) {
                    send_nickserv_command(arg1, arg2);
         } else if ((cmd == "m" || cmd == "msg" || cmd == "whisper") && argc > 
1) {
                        config cwhisper,data;

Modified: branches/mp_registration/src/server/server.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/server.cpp?rev=24905&r1=24904&r2=24905&view=diff
==============================================================================
--- branches/mp_registration/src/server/server.cpp (original)
+++ branches/mp_registration/src/server/server.cpp Thu Mar 20 19:43:01 2008
@@ -222,7 +222,9 @@
 
        time_t last_ping_;
        time_t last_stats_;
+       time_t last_clean_;
        void dump_stats(const time_t& now);
+       void clean_user_handler(const time_t& now);
 
        void process_data(const network::connection sock,
                          simple_wml::document& data);
@@ -264,7 +266,8 @@
        join_lobby_response_("[join_lobby]\n[/join_lobby]\n", 
simple_wml::INIT_COMPRESSED),
        games_and_users_list_("[gamelist]\n[/gamelist]\n", 
simple_wml::INIT_STATIC),
        last_ping_(time(NULL)),
-       last_stats_(last_ping_)
+       last_stats_(last_ping_),
+       last_clean_(last_ping_)
 {
        load_config();
 
@@ -413,6 +416,14 @@
                << "\tlobby_users = " << lobby_.nobservers() << "\n";
 }
 
+void server::clean_user_handler(const time_t& now) {
+       if(!user_handler_) {
+           return;
+       }
+       last_clean_ = now;
+       user_handler_->clean_up();
+}
+
 void server::run() {
        for (int loop = 0;; ++loop) {
                SDL_Delay(20);
@@ -432,6 +443,8 @@
                        if ((loop%100) == 0 && last_ping_ + 10 <= now) {
                                // Make sure we log stats every 5 minutes
                                if (last_stats_ + 5*60 <= now) dump_stats(now);
+                               // Clean user_handler_ once a day
+                               if (last_clean_ + 60*60*24 <= now) 
clean_user_handler(now);
                                // send a 'ping' to all players to detect ghosts
                                config ping;
                                ping["ping"] = lexical_cast<std::string>(now);
@@ -831,11 +844,13 @@
                return;
        }
 
+       //Check if this server allows nick registration at all
+    if(!user_handler_) {
+        lobby_.send_server_message("This server does not allow to register on 
it.", sock);
+        return;
+    }
+
        if(data.child("register")) {
-           if(!user_handler_) {
-            lobby_.send_server_message("This server does not allow to register 
on it.", sock);
-            return;
-           }
            try {
                (user_handler_->add_user(pl->second.name(), 
(*data.child("register"))["mail"].to_string(),
                 (*data.child("register"))["password"].to_string()));
@@ -865,13 +880,8 @@
 
     //A user requested to update his password or mail
        if(data.child("set")) {
-           if(!user_handler_) {
-            lobby_.send_server_message("This server does not allow to register 
on it.", sock);
-            return;
-           }
-
            if(!(user_handler_->user_exists(pl->second.name()))) {
-            lobby_.send_server_message("You are not registered. Please 
register first",
+            lobby_.send_server_message("You are not registered. Please 
register first.",
                     sock);
             return;
            }
@@ -889,7 +899,7 @@
                 user_handler_->set_realname(pl->second.name(), 
set["realname"].to_string());
             }
 
-        lobby_.send_server_message("Your user details have been updated.", 
sock);
+            lobby_.send_server_message("Your user details have been updated.", 
sock);
 
            } catch (user_handler::error e) {
             lobby_.send_server_message(("There was and error updating your 
details. The error message was: "
@@ -899,12 +909,8 @@
         return;
        }
 
+    //A user requested information about another user
        if(data.child("info")) {
-           if(!user_handler_) {
-            lobby_.send_server_message("This server does not allow to register 
on it. "
-            "No user info available." , sock);
-            return;
-           }
            try {
                std::string res = 
user_handler_->user_info((*data.child("info"))["name"].to_string());
                lobby_.send_server_message(res.c_str(), sock);
@@ -914,6 +920,32 @@
             + e.message).c_str(), sock);
            }
 
+       }
+
+       //A user requested to delete his nick
+       if(data.child("drop")) {
+           if(!(user_handler_->user_exists(pl->second.name()))) {
+            lobby_.send_server_message("You are not registered.",
+                    sock);
+            return;
+           }
+
+        try {
+            user_handler_->remove_user(pl->second.name());
+            lobby_.send_server_message("Your username has been dropped.", 
sock);
+
+            //Mark the player as not registered and send the other clients
+            //an update to dislpay this change
+            pl->second.mark_registered(false);
+
+            simple_wml::document diff;
+            make_change_diff(games_and_users_list_.root(), NULL,
+                                "user", pl->second.config_address(), diff);
+            lobby_.send_data(diff);
+           } catch (user_handler::error e) {
+            lobby_.send_server_message(("There was and error dropping your 
username. The error message was: "
+            + e.message).c_str(), sock);
+           }
        }
 
 }

Modified: branches/mp_registration/src/server/user_handler.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/user_handler.cpp?rev=24905&r1=24904&r2=24905&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.cpp (original)
+++ branches/mp_registration/src/server/user_handler.cpp Thu Mar 20 19:43:01 
2008
@@ -1,13 +1,15 @@
 #include "user_handler.hpp"
 
-#include <algorithm>
+#include "../log.hpp"
+#include "../filesystem.hpp"
+#include "../serialization/parser.hpp"
+#include "../serialization/preprocessor.hpp"
+#include "../serialization/string_utils.hpp"
+
 #include <cassert>
-#include <cerrno>
 #include <cstdlib>
 #include <ctime>
 #include <iostream>
-#include <map>
-#include <set>
 #include <sstream>
 #include <vector>
 
@@ -17,6 +19,7 @@
         cfg_(read_config())
 {
     load_config();
+    clean_up();
 }
 
 user_handler::~user_handler() {
@@ -48,24 +51,44 @@
 
     //Load configuration and initialize with default values if we don't find 
values
 
+        if(cfg_["username_expiration_limit"].empty()) {
+            unsigned short default_time = 60;
+            username_expiration_limit_ = default_time;
+            cfg_["username_expiration_limit"] = 
lexical_cast_default<std::string>(default_time);
+        } else {
+            try {
+                username_expiration_limit_ = lexical_cast_default<unsigned 
short>(cfg_["username_expiration_limit"]);
+            } catch (bad_lexical_cast) {
+                std::cerr << "Bad lexical cast reading 
'username_expiration_limit', using default value.\n";
+                unsigned short default_time = 60;
+                username_expiration_limit_ = default_time;
+                cfg_["username_expiration_limit"] = 
lexical_cast_default<std::string>(default_time);
+            }
+        }
+
     #ifndef NO_MAIL
 
-    cfg_["from_address"] = cfg_["from_address"].empty() ? "[EMAIL PROTECTED]" 
: cfg_["from_address"];
-    cfg_["mail_server"] = cfg_["mail_server"].empty() ? "127.0.0.1" : 
cfg_["mail_server"];
-
-    //"mail_user" and "mail_password" may stay empty
-
-    if(cfg_["mail_port"].empty()) {
-        mail_port_ = jwsmtp::mailer::SMTP_PORT;
-        unsigned short default_port = jwsmtp::mailer::SMTP_PORT;
-        cfg_["mail_port"] = lexical_cast_default<std::string>(default_port);
-    } else {
-        try {
-            mail_port_ = lexical_cast_default<unsigned 
short>(cfg_["mail_port"]);
-        } catch (bad_lexical_cast) {
-            std::cerr << "Bad lexical cast reading the 'mail_port', using 
default port.\n";
+    if(mail()) {
+        config& mail = *(cfg_.child("mail"));
+
+        mail["from_address"] = mail["from_address"].empty() ? "[EMAIL 
PROTECTED]" : mail["from_address"];
+        mail["mail_server"] = mail["mail_server"].empty() ? "127.0.0.1" : 
mail["mail_server"];
+
+        //"mail_user" and "mail_password" may stay empty
+
+        if(mail["mail_port"].empty()) {
             unsigned short default_port = jwsmtp::mailer::SMTP_PORT;
-            cfg_["mail_port"] = 
lexical_cast_default<std::string>(default_port);
+            mail_port_ = default_port;
+            mail["mail_port"] = 
lexical_cast_default<std::string>(default_port);
+        } else {
+            try {
+                mail_port_ = lexical_cast_default<unsigned 
short>(mail["mail_port"]);
+            } catch (bad_lexical_cast) {
+                std::cerr << "Bad lexical cast reading the 'mail_port', using 
default port.\n";
+                unsigned short default_port = jwsmtp::mailer::SMTP_PORT;
+                mail_port_ = default_port;
+                mail["mail_port"] = 
lexical_cast_default<std::string>(default_port);
+            }
         }
     }
 
@@ -96,11 +119,18 @@
 
     #ifndef NO_MAIL
 
-    jwsmtp::mailer m(to_address, cfg_["from_address"].c_str(), subject, 
message,
-            cfg_["mail_server"].c_str(), mail_port_, false);
-    if(!(cfg_["mail_user"].empty())) {
-        m.username(cfg_["mail_user"]);
-        m.password( cfg_["mail_password"]);
+    //if we cannot send emails at all we of course also cannot send this one
+    if(!mail()) {
+        return false;
+    }
+
+    config& mail = *(cfg_.child("mail"));
+
+    jwsmtp::mailer m(to_address, mail["from_address"].c_str(), subject, 
message,
+            mail["mail_server"].c_str(), mail_port_, false);
+    if(!(mail["mail_user"].empty())) {
+        m.username(mail["mail_user"]);
+        m.password( mail["mail_password"]);
     }
     //! @todo Sending the mail in a new thread
     //! (as suggested on http://johnwiggins.net/jwsmtp/)
@@ -124,7 +154,35 @@
 }
 
 void user_handler::clean_up() {
-    //! @todo Write this function :)
+    std::cout << "User handler clean up...\n";
+    remove_dead_users();
+    std::cout << "Clean up finished\n";
+}
+
+void user_handler::remove_dead_users() {
+    //username_expiration_limit_ set to 0 means
+    //no expiration limit
+    if(!username_expiration_limit_) {
+        return;
+    }
+
+    time_t now = time(NULL);
+    //A minute has 60 seconds, an hour 60 minutes and
+    //a day 24 hours.
+    //Thus a day has 60 * 60 * 24 = 86400 seconds
+    time_t limit = username_expiration_limit_ * 86400;
+
+    const config::child_map& user_childs = users_->all_children();
+    for(config::child_map::const_iterator i = user_childs.begin(); i != 
user_childs.end(); ++i) {
+        const config& user = *(users_->child(i->first));
+        time_t last_login = lexical_cast_default<time_t>(user["last_login"]);
+        if((now - last_login) > limit) {
+            std::cout << "User '" << i->first << "' exceeds expiration date: ";
+            remove_user(i->first);
+        }
+    }
+
+    save_config();
 }
 
 void user_handler::add_user(const std::string& name,
@@ -146,7 +204,6 @@
     if(!mail.empty()) {
         const config::child_map& user_childs = users_->all_children();
         for(config::child_map::const_iterator i = user_childs.begin(); i != 
user_childs.end(); ++i) {
-            std::cout << (*(users_->child(i->first)))["mail"] << std::endl;
             if((*(users_->child(i->first)))["mail"] == mail) {
                 throw error("Could not add new user. The email address '" + 
mail + "' is already in use.");
             }
@@ -196,7 +253,7 @@
     config& user = *(users_->child(name));
 
     if(user["mail"].empty()) {
-        throw error("Could not send password reminder. The email address of 
the user '" + name + "' is empty");
+        throw error("Could not send password reminder. The email address of 
the user '" + name + "' is empty.");
     }
 
     std::stringstream msg;
@@ -205,7 +262,7 @@
 
     //If sending does not return true warn that no message was sent.
     if(!(send_mail(user["mail"].c_str(), "Wesnoth Multiplayer Server Password 
Reminder", msg.str().c_str()))) {
-        throw error("Could not send password reminder. There was an error 
sending the reminder email");
+        throw error("Could not send password reminder. There was an error 
sending the reminder email.");
     }
     return;
 
@@ -236,6 +293,7 @@
     if(!user_exists(name)) {
         throw error("Could not remove user. No user with the name '" + name + 
"' exists.");
     }
+
     users_->remove_child(name, 0);
 
     //! @todo To save performance it we should of course not save
@@ -274,6 +332,17 @@
 
 bool user_handler::user_exists(const std::string& name) {
     return ((users_->child(name)));
+}
+
+bool user_handler::mail() {
+
+    //We should not even get to call this when compiling with NO_MAIL
+    //but some doulbe safety can never hurt :)
+    #ifdef NO_MAIL
+    return false;
+    #endif
+
+    return (cfg_.child("mail"));
 }
 
 void user_handler::set_mail(const std::string& user, const std::string& mail) {

Modified: branches/mp_registration/src/server/user_handler.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/user_handler.hpp?rev=24905&r1=24904&r2=24905&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.hpp (original)
+++ branches/mp_registration/src/server/user_handler.hpp Thu Mar 20 19:43:01 
2008
@@ -4,11 +4,6 @@
 #include "../global.hpp"
 
 #include "../config.hpp"
-#include "../log.hpp"
-#include "../filesystem.hpp"
-#include "../serialization/parser.hpp"
-#include "../serialization/preprocessor.hpp"
-#include "../serialization/string_utils.hpp"
 
 #ifndef NO_MAIL
 #include <jwsmtp/jwsmtp.h>
@@ -23,14 +18,16 @@
         void load_config();
         void save_config();
 
-        //! Remove users that registered but did never log in, etc.
         void clean_up();
+
+        //! Removes users that have not logged in for a certain amount of time
+        void remove_dead_users();
 
         //! Adds a user.
         //! Returns false if adding fails (e.g. because a user with the same 
name already exists).
         void add_user(const std::string& name, const std::string& mail, const 
std::string& password);
 
-        //! Removes a user-
+        //! Removes a user.
         //! Returns false if the user does not exist
         void remove_user(const std::string& name);
 
@@ -53,6 +50,9 @@
         //! Returns a string containing info like the last login of this user
         std::string user_info(const std::string& name);
 
+        //! Returns true if we can send emails
+        bool mail();
+
 
         struct error {
             error(const std::string& msg) : message(msg) {}
@@ -72,6 +72,7 @@
 
         std::string users_file_;
         unsigned short mail_port_;
+        unsigned short username_expiration_limit_;
 
         config cfg_;
         config* users_;


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to