View Single Post
  #1  
Old 08-11-2010, 11:24 PM
pfyon's Avatar
pfyon
Discordant
 
Join Date: Mar 2009
Location: Ottawa
Posts: 495
Default EQEmuLoginServer - Creates account upon login

This might be useful for small servers for just friends. When someone logs in and the username does not already exist in the DB, it will create a new user with username/password given (and email address nobody@localhost).

Tested on Ubuntu 10.04 with rev 1620. The revision numbers in the diff are because I was comitting against my own svn.

Code:
Index: Client.cpp
===================================================================
--- Client.cpp  (revision 60)
+++ Client.cpp  (working copy)
@@ -225,11 +225,29 @@
        _HeapDeleteCharBuffer(e_buffer);
 #endif

-       bool result;
+       bool result = false;
        if(server.db->GetLoginDataFromAccountName(e_user, d_pass_hash, d_account_id) == false)
        {
                log->Log(log_client_error, "Error logging in, user %s does not exist in the database.", e_user.c_str());
-               result = false;
+               if(server.db->CreateLoginAccount(e_user.c_str(), e_hash.c_str()))
+               {
+                       //Made the account properly
+                       if(server.db->GetLoginDataFromAccountName(e_user, d_pass_hash, d_account_id) == false)
+                       {
+                               //Somehow did not authenticate properly
+                               log->Log(log_client_error, "Error logging in, user %s does not exist in the database.", e_user.c_str());
+                               result = false;
+                       } else
+                       {
+                               if(d_pass_hash.compare(e_hash) == 0)
+                               {
+                                       result = true;
+                               } else
+                               {
+                                       result = false;
+                               }
+                       }
+               }
        }
        else
        {
Code:
Index: DatabaseMySQL.h
===================================================================
--- DatabaseMySQL.h     (revision 56)
+++ DatabaseMySQL.h     (working copy)
@@ -88,6 +88,11 @@
         * Creates new world registration for unregistered servers and returns new id
         */
        virtual bool CreateWorldRegistration(string long_name, string short_name, unsigned int &id);
+
+       /**
+        * Creates a new login account in the player accounts table
+        */
+       virtual bool CreateLoginAccount(string name, string password);
 protected:
        string user, pass, host, port, name;
        MYSQL *db;
Code:
Index: DatabaseMySQL.cpp
===================================================================
--- DatabaseMySQL.cpp   (revision 56)
+++ DatabaseMySQL.cpp   (working copy)
@@ -97,6 +97,36 @@
        return false;
 }

+bool DatabaseMySQL::CreateLoginAccount(string name, string password)
+{
+       if(!db)
+       {
+               return false;
+       }
+
+       MYSQL_RES *res;
+       MYSQL_ROW row;
+       char escaped_name[101];
+       char escaped_password[101];
+       unsigned long length;
+       length = mysql_real_escape_string(db, escaped_name, name.substr(0, 50).c_str(), name.substr(0, 100).length());
+       escaped_name[length+1] = 0;
+       length = mysql_real_escape_string(db, escaped_password, password.substr(0, 50).c_str(), password.substr(0, 100).length());
+       escaped_password[length+1] = 0;
+
+       stringstream query(stringstream::in | stringstream::out);
+       query << "INSERT INTO " << server.options.GetAccountTable() << " (AccountName, AccountPassword, AccountCreateDate, AccountEmail)";
+       query << "VALUES ('" << escaped_name << "', '" << escaped_password << "', now(), 'nobody@localhost')";
+
+       if(mysql_query(db, query.str().c_str()) != 0)
+       {
+               log->Log(log_database, "Error creating login account: %s", query.str().c_str());
+               return false;
+       }
+
+       return true;
+}
+
 bool DatabaseMySQL::GetWorldRegistration(string long_name, string short_name, unsigned int &id, string &desc, unsigned int &list_id,
                unsigned int &trusted, string &list_desc, string &account, string &password)
 {
Code:
Index: Database.h
===================================================================
--- Database.h  (revision 56)
+++ Database.h  (working copy)
@@ -73,6 +73,11 @@
         * Creates new world registration for unregistered servers and returns new id
         */
        virtual bool CreateWorldRegistration(string long_name, string short_name, unsigned int &id) { return false; }
+
+       /**
+        * Creates a new login account in the player accounts table
+        */
+       virtual bool CreateLoginAccount(string name, string password) { return false;}
 protected:
        string user, pass, host, port, name;
 };
Reply With Quote