]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/gameserv.cpp
Working on doing the identify/register scheme. Right now, there are problems with...
[irc/gameservirc.git] / gameserv / gameserv.cpp
index 2d50be58f905cf2f797b5ba08d959a3e54656b5c..5d49ef5d8dc916166140f31b640e873a64d5abf1 100644 (file)
@@ -4,11 +4,12 @@
 #include "extern.h"
 #include <cctype>
 #include <fstream.h>
+#include <crypt.h>
 
 List<aClient> players;
 Monster monsters[5][12];
 
-// Database functions For load/save
+// Database functions
 int save_gs_dbase();
 int load_gs_dbase();
 
@@ -19,14 +20,26 @@ int stricmp(const char *s1, const char *s2);
 int strnicmp(const char *s1, const char *s2, size_t len);
 // String Functions
 
-// GameServ Booleans
-bool is_playing(char *u);
-bool has_started(char *u);
-bool is_fighting(char *u);
-bool isnt_fighting(char *u);
-bool player_fight(char *u);
-bool master_fight(char *u);
-// GameServ Booleans
+
+/********** Password functions **********/
+
+bool passcmp(char *encrypted, char *plaintext); // Compares an encrypted pass with a plain text one
+
+bool check_password(char *name, char *plaintext); // Finds a password for the given name, and checks it with passcmp against the plaintext password given.
+/********** Password functions **********/
+
+
+/********** GameServ Booleans **********/
+
+bool is_playing(char *u); // True if the given nickname in the clients list is playing.
+bool has_started(char *u); // True if the given nickname in the clients list has started playing.
+bool is_fighting(char *u); // True if the given nick in the clients list is fighting anything.
+bool isnt_fighting(char *u); // True if the given nick isn't fighting. Same as !is_fighting(u).
+bool player_fight(char *u); // True if the player is fighting another player.
+bool master_fight(char *u); // True if the player is fighting their master.
+
+/********** GameServ Booleans **********/
 
 
 void display_monster(char *u);
@@ -357,6 +370,13 @@ void do_register(char *u)
     aClient *user;
     password = strtok(NULL, " ");
 
+    static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
+    static char salt[3];
+    
+    salt[0] = saltChars[rand() % strlen(saltChars)];
+    salt[1] = saltChars[rand() % strlen(saltChars)];
+    salt[3] = '\0';
+
     if (!password)
     {
        notice(s_GameServ, u, "SYNTAX: /msg %S REGISTER PASSWORD");
@@ -368,6 +388,7 @@ void do_register(char *u)
            user->stats = new Player(user);
            user->stats->started = 1;
            user->stats->user = user; // Set the backwards pointer
+           strcpy(user->stats->password, crypt(password, salt));
            players.insertAtBack(user);
        }
        else
@@ -1545,7 +1566,8 @@ int save_gs_dbase()
        outfile << it->name << ' ' << it->level << ' ' << it->exp << ' ' << it->gold << ' ' << it->bank << ' '
                << it->hp << ' ' << it->maxhp << ' ' << it->strength << ' ' << it->defense << ' '
                << it->armor << ' ' << it->weapon << ' ' << (it->alive ? "alive" : "dead") << ' '
-               << it->forest_fights << ' ' << it->player_fights << endl;
+               << it->forest_fights << ' ' << it->player_fights <<  ' ' 
+               << it->password << endl;
        ptr = ptr->Next();
     }
 outfile.close();
@@ -1556,7 +1578,7 @@ int load_gs_dbase()
     ifstream infile;
     aClient *temp;
     Player *p;
-    char *alive, *tempname, *buf;
+    char *alive, *tempname, *buf, *password;
     buf = new char[1023];
 
     infile.open(playerdata);
@@ -1589,12 +1611,39 @@ int load_gs_dbase()
        p->alive = (stricmp(alive, "ALIVE") == 0 ? true : false);
        p->forest_fights = stringtoint(strtok(NULL, " "));
        p->player_fights = stringtoint(strtok(NULL, " "));
+       password = strtok(NULL, " ");
+       strcpy(p->password, password);
        temp->setNick("NULL");
 
-       printf("%s %d %ld %ld %ld %d %d %d %d %d %d %s %d %d\n", p->name, p->level, p->exp, p->gold, p->bank, p->hp, p->maxhp, p->strength, p->defense, p->armor, p->weapon, alive, p->forest_fights, p->player_fights);
+       printf("%s %d %ld %ld %ld %d %d %d %d %d %d %s %d %d %s\n", p->name, p->level, 
+       p->exp, p->gold, p->bank, p->hp, p->maxhp, p->strength, p->defense, p->armor, p->weapon, 
+       alive, p->forest_fights, p->player_fights, p->password);
+
        players.insertAtBack(temp);
        delete temp;
     }
 delete buf;
 }
 
+bool passcmp(char *encrypted, char *plaintext)
+{
+    char salt[3];
+    salt[0] = encrypted[0];
+    salt[1] = encrypted[1];
+    salt[3] = '\0';
+    return (strcmp(encrypted, crypt(plaintext, salt)) == 0);
+}
+
+bool check_password(char *name, char *plaintext)
+{
+    ListNode<aClient> *tempPtr = clients.First();
+
+    while (tempPtr)
+    {
+       if (stricmp(name, tempPtr->getData()->getNick()) == 0)
+           return passcmp(tempPtr->getData()->stats->password, plaintext);
+
+       tempPtr = tempPtr->Next();
+    }
+    return false;
+}