mail[Wesnoth-commits] r24774 - in /branches/mp_registration/src/server: server.cpp user_handler.cpp user_handler.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by baufo on March 18, 2008 - 09:30:
Author: baufo
Date: Tue Mar 18 09:30:49 2008
New Revision: 24774

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24774&view=rev
Log:
Make user_handler functions throw errors rather than returning false.

Modified:
    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/server/server.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/mp_registration/src/server/server.cpp?rev=24774&r1=24773&r2=24774&view=diff
==============================================================================
--- branches/mp_registration/src/server/server.cpp (original)
+++ branches/mp_registration/src/server/server.cpp Tue Mar 18 09:30:49 2008
@@ -1039,17 +1039,20 @@
             return;
            }
            //! @todo Check if provided values are sane
-           if(user_handler_->add_user(pl->second.name(), 
(*data.child("register"))["mail"].to_string(),
-                (*data.child("register"))["password"].to_string())) {
+           try {
+               (user_handler_->add_user(pl->second.name(), 
(*data.child("register"))["mail"].to_string(),
+                (*data.child("register"))["password"].to_string()));
+
             std::stringstream msg;
-            msg << "Your username was registered." <<
+            msg << "Your username has been registered." <<
                                        //Warn that providing an email 
address might be a good idea
                                        
((*data.child("register"))["mail"].empty() ?
                                        " It is recommended that you provide 
an email address for password recovery." : "");
             lobby_.send_server_message(msg.str().c_str(), sock);
-        } else {
-            //! @todo Describe the error in detail
-            lobby_.send_server_message("There was an error registering your 
username", sock);
+
+        } catch (user_handler::error e) {
+            lobby_.send_server_message(("There was and error registering 
your username. The error message was: "
+            + e.message).c_str(), sock);
         }
         return;
        }
@@ -1070,14 +1073,21 @@
            const simple_wml::node& update = *(data.child("update_details"));
 
            //! @todo Check if provided values are sane
-           if(!(update["mail"].to_string().empty())) {
-               user_handler_->set_mail(pl->second.name(), 
update["mail"].to_string());
+           try {
+            if(!(update["mail"].to_string().empty())) {
+                user_handler_->set_mail(pl->second.name(), 
update["mail"].to_string());
+            }
+            if(!(update["password"].to_string().empty())) {
+                user_handler_->set_password(pl->second.name(), 
update["password"].to_string());
+            }
+
+        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: "
+            + e.message).c_str(), sock);
            }
-           if(!(update["password"].to_string().empty())) {
-               user_handler_->set_password(pl->second.name(), 
update["password"].to_string());
-           }
-
-        lobby_.send_server_message("Your user details have been updated.", 
sock);
+
         return;
        }
 

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=24774&r1=24773&r2=24774&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.cpp (original)
+++ branches/mp_registration/src/server/user_handler.cpp Tue Mar 18 09:30:49 
2008
@@ -100,8 +100,6 @@
 
     std::cout << m.response() << "\n";
 
-    //Do _all_ smtp servers put the result code in front of
-    //their response messages?
     if(m.response().substr(0,3) != "250") {
         return false;
     }
@@ -113,7 +111,7 @@
     //! @todo Write this function :)
 }
 
-bool user_handler::add_user(const std::string& name,
+void user_handler::add_user(const std::string& name,
         const std::string& mail, const std::string& password) {
 
     //! @todo Check if provided values are sane
@@ -122,7 +120,7 @@
 
     //Check if this user already exists
     if(user_exists(name)) {
-        return false;
+        throw error("Could not add new user. A user with the name \"" + name 
+ "\" already exists.");
     }
 
     //! @todo I guess we should only allow every email address to be 
registered only once
@@ -137,7 +135,7 @@
 
     //Don't send a confirmation mail if we don't have an email
     if(mail.empty()) {
-        return true;
+        return;
     }
 
     std::stringstream msg;
@@ -146,41 +144,40 @@
             "Your password: " << user["password"];
 
     send_mail(user["mail"].c_str(), "Wesnoth Multiplayer Server 
Registration", msg.str().c_str());
-    return true;
-}
-
-bool user_handler::password_reminder(const std::string& name) {
-    if(!user_exists(name)) {
-        return false;
+}
+
+void user_handler::password_reminder(const std::string& name) {
+    if(!user_exists(name)) {
+        throw error("Could not send password reminder. No user with the name 
\"" + name + "\" exists.");
     }
 
     config& user = *(users_->child(name));
 
     if(user["mail"].empty()) {
-        return false;
+        throw error("Could not send password reminder. The email address of 
the user \"" + name + "\" is empty");
     }
 
     std::stringstream msg;
     msg << "Your username: " << name << "\n" <<
             "Your password: " << user["password"];
 
-    //If sending succeeds return true, otherwise false
-    return send_mail(user["mail"].c_str(), "Wesnoth Multiplayer Server 
Password Reminder", msg.str().c_str());
-
-}
-
-bool user_handler::remove_user(const std::string& name) {
+    //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");
+    }
+
+}
+
+void user_handler::remove_user(const std::string& name) {
     //Return if the user does not exist
     if(!user_exists(name)) {
-        return false;
+        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
     //! the whole config everytime something changes
     save_config();
-
-    return true;
 }
 
 bool user_handler::login(const std::string& name, const std::string& 
password) {
@@ -193,12 +190,13 @@
     return (user["password"] == password);
 }
 
-bool user_handler::set_user_attribute(const std::string& name,
+void user_handler::set_user_attribute(const std::string& name,
         const std::string& attribute, const std::string& value) {
 
     //Return if the user does not exist
     if(!user_exists(name)) {
-        return false;
+        throw error("Could not set attribute \"" + attribute  + "\" for user 
\"" + name +
+        "\". No user with the name with this name exists.");
     }
 
     config& user = *(users_->child(name));
@@ -207,8 +205,6 @@
     //! @todo To save performance it we should of course not save
     //! the whole config everytime something changes
     save_config();
-
-    return true;
 }
 
 bool user_handler::user_exists(const std::string& name) {

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=24774&r1=24773&r2=24774&view=diff
==============================================================================
--- branches/mp_registration/src/server/user_handler.hpp (original)
+++ branches/mp_registration/src/server/user_handler.hpp Tue Mar 18 09:30:49 
2008
@@ -31,28 +31,36 @@
 
         //! Adds a user.
         //! Returns false if adding fails (e.g. because a user with the same 
name already exists).
-        bool add_user(const std::string& name, const std::string& mail, 
const std::string& password);
+        void add_user(const std::string& name, const std::string& mail, 
const std::string& password);
         //! Removes a user-
         //! Returns false if the user did not exist;
-        bool remove_user(const std::string& name);
+        void remove_user(const std::string& name);
 
-        bool password_reminder(const std::string& name);
+        void password_reminder(const std::string& name);
 
-        bool set_mail(const std::string& user, const std::string& mail) {
-            return set_user_attribute(user, "mail", mail);
+        void set_mail(const std::string& user, const std::string& mail) {
+            set_user_attribute(user, "mail", mail);
         }
-        bool set_password(const std::string& user, const std::string& 
password) {
-            return set_user_attribute(user, "password", password);
+
+        void set_password(const std::string& user, const std::string& 
password) {
+            set_user_attribute(user, "password", password);
         }
 
         bool login(const std::string& name, const std::string& password);
         bool user_exists(const std::string& name);
 
+
+        struct error {
+            error(const std::string& msg) : message(msg) {}
+            std::string message;
+        };
+
     private:
         //! Sends an e-mail to a specified address. Requires access to an 
SMTP server.
+        //! Returns false if the mail could not be sent.
         bool send_mail(const char* to_address, const char* subject, const 
char* message);
 
-        bool set_user_attribute(const std::string& name,
+        void set_user_attribute(const std::string& name,
                 const std::string& attribute, const std::string& value);
 
         std::string users_file_;




Related Messages


Powered by MHonArc, Updated Tue Mar 18 11:02:05 2008