src/commands/Cmd_accounts.cpp

00001 #include <string>
00002 #include "Cmd_accounts.h"
00003 #include "sha1.h"
00004 #include "../Logger.h"
00005 #include "../cppsqlite3.h"
00006 
00007 Cmd_accounts::Cmd_accounts(std::string& params, Server* server)
00008     : ICommandServer(params, server)
00009 {
00010         myAccDb.open("accounts.db");
00011 }
00012 
00013 Cmd_accounts::~Cmd_accounts()
00014 {
00015     myAccDb.close();
00016 }
00017 
00018 void Cmd_accounts::help(){
00019     std::cout << "Account manager" << std::endl << "===============" << std::endl ;
00020     std::cout << "accounts => Show registered accounts." << std::endl;
00021     std::cout << "accounts help => Show this help." << std::endl;
00022     std::cout << "accounts add => Adds a new account to the database." << std::endl;
00023     std::cout << "accounts attach <account_id> => Creates a new basic character and attaches it to the specified account (for testing purposes)." << std::endl;
00024 }
00025 
00026 void Cmd_accounts::execute(){
00027     std::string param1 = getParam(1);
00028 
00029     if (param1 == "")
00030         showAccounts();
00031     else if (param1 == "help")
00032         help();
00033     else if (param1 == "add")
00034         addAccount();
00035     else if (param1 == "attach")
00036         attachChar();
00037     else
00038         std::cout << "You passed a wrong parameter to accounts command, run \"accounts help\" for further info." << std::endl;
00039 
00040 }
00041 
00042 void Cmd_accounts::showAccounts(Uint32 recordsPerPage){
00043     CppSQLite3Query q = myAccDb.execQuery("select * from registered_users order by id;");
00044 
00045     while (!q.eof()){
00046         std::cout << std::setw(3) << q.fieldValue(0) << " | ";
00047         std::cout << std::setw(10) << q.fieldValue(1) << " | ";
00048         std::cout << std::setw(10) << q.fieldValue(3) << " | ";
00049         std::cout << std::setw(10) << q.fieldValue(4) << " | ";
00050         std::cout << std::setw(3) << q.fieldValue(6) << " | ";
00051         std::cout << std::setw(20) <<q.fieldValue(5) << " | " << std::endl;
00052         q.nextRow();
00053     }
00054 }
00055 
00056 void Cmd_accounts::addAccount(const std::string& username,
00057                                 const std::string& password,
00058                                 const std::string& name,
00059                                 const std::string& surname,
00060                                 const std::string& email)
00061 {
00062     std::string username_, password_, name_, surname_, email_;
00063     SHA1 pwdSHA1;
00064         Uint32 shaData[5];
00065 
00066         Logger* logger = Logger::getInstance();
00067 
00068     if (username == "" || password  == "" || name == "" || surname == "" || email == ""){
00069         std::cout << "ACCOUNTS CREATOR\n";
00070         std::cout << "================\n";
00071         std::cout << "Use this program to create simple user data into sqlite3 db.\n";
00072         std::cout << "User name : ";
00073         std::getline(std::cin, username_);
00074         std::cout << "Password: ";
00075         std::getline(std::cin, password_);
00076         std::cout << "Real name: ";
00077         std::getline(std::cin, name_);
00078         std::cout << "Real surname: ";
00079         std::getline(std::cin, surname_);
00080         std::cout << "Email address: ";
00081         std::getline(std::cin, email_);
00082     }
00083     else{
00084         username_ = username;
00085         password_= password;
00086         name_ = name;
00087         surname_ = surname;
00088         email_ = email;
00089     }
00090 
00091     std::string temp = username_+password_;
00092         pwdSHA1.Reset();
00093         for (unsigned int i = 0; i < temp.size(); i++){
00094                 pwdSHA1.Input(temp[i]);
00095         }
00096 
00097         if (!pwdSHA1.Result(shaData)){
00098                 std::cerr<<"Sha could not compute message digest for login informations." << std::endl;
00099                 logger->log("Error during SHA1 computation in addAccount() method.", LOGMODE_ERROR);
00100         }
00101         else{ // We have a valid SHA1 hash.
00102         char hash[40];
00103         for (int i = 1; i < 6; i++){ // Transforms the hash in a readable format.
00104             sprintf(hash + (8*(i-1)), "%08x", shaData[i-1]);
00105         }
00106 
00107         Sint32 nRows;
00108         #ifdef TESTPHASE
00109         nRows = myAccDb.execDML("CREATE TABLE IF NOT EXISTS registered_users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, index_hash TEXT, surname TEXT, name TEXT, email TEXT, pc_id INTEGER DEFAULT 0);");
00110         nRows = myAccDb.execDML("CREATE UNIQUE INDEX IF NOT EXISTS sha1_index ON registered_users (index_hash);");
00111         #endif
00112         std::string query = "insert into registered_users(username, index_hash, surname, name, email) values ( \""+username_+"\", \""+hash+"\", \""+name_+"\", \""+surname_+"\", \""+email_+"\");";
00113         nRows = myAccDb.execDML(query.data());
00114 
00115         std::cout << "OK account for username \"" << username_ << "\" created." << std::endl;
00116         }
00117 
00118 }
00119 
00120 void Cmd_accounts::attachChar(){
00121     std::string charname, xPos, yPos, accNumber;
00122 
00123     std::cout << "Account ID :";
00124     std::getline(std::cin, accNumber);
00125     std::cout << "Initial X position :";
00126     std::getline(std::cin, xPos);   // Not really good...
00127     std::cout << "Initial Y position :";
00128     std::getline(std::cin, yPos);              //This neither
00129     std::cout << "Character name :";
00130     std::getline(std::cin, charname);
00131 
00132     Sint32 nRows;
00133     #ifdef TESTPHASE
00134     nRows = myAccDb.execDML("CREATE TABLE IF NOT EXISTS characters (id INTEGER PRIMARY KEY AUTOINCREMENT, account_id INTEGER, name TEXT UNIQUE, x_position INTEGER, y_position INTEGER);");
00135     #endif
00136     std::string query = "insert into characters(account_id, name, x_position, y_position) VALUES ( \""+accNumber+"\", \""+charname+"\", \""+xPos+"\", \""+yPos+"\");";
00137     nRows = myAccDb.execDML(query.data());
00138 
00139     if (nRows){
00140         query = "select id from characters WHERE name = \""+charname+"\";";
00141         CppSQLite3Query q = myAccDb.execQuery(query.data());
00142         if(!q.eof()){
00143             nRows = 0;
00144             std::ostringstream oss;
00145             oss << q.fieldValue(0);
00146             query = "UPDATE registered_users SET pc_id = \""+oss.str()+"\" WHERE id = \""+accNumber+"\";";
00147             nRows = myAccDb.execDML(query.data());
00148             if (nRows == 0){ //no tables updated, something went wrong, delete the char and print a warning.
00149                 query = "DELETE FROM characters WHERE id = \""+oss.str()+"\";";
00150                 nRows = myAccDb.execDML(query.data());
00151                 Logger::getInstance()->log("Failed to insert a new character due to an invalid account id.", LOGMODE_DB);
00152             }
00153         }
00154 
00155     }
00156 
00157 
00158 }

Generated on Mon Dec 3 04:30:12 2007 for lo-testserver by  doxygen 1.3.9.1