ServerAuthHandlerPrefered.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <server/ServerAuthHandlerPrefered.h>
00022 #include <server/ScorchedServer.h>
00023 #include <common/OptionsScorched.h>
00024 #include <common/Defines.h>
00025 #include <common/Logger.h>
00026 #include <XML/XMLFile.h>
00027 
00028 ServerAuthHandlerPrefered::ServerAuthHandlerPrefered() : lastReadTime_(0)
00029 {
00030 }
00031 
00032 ServerAuthHandlerPrefered::~ServerAuthHandlerPrefered()
00033 {
00034 }
00035 
00036 void ServerAuthHandlerPrefered::createAuthentication(ComsConnectAuthMessage &authMessage)
00037 {
00038 }
00039 
00040 bool ServerAuthHandlerPrefered::authenticateUser(ComsConnectAuthMessage &authMessage, 
00041                 std::string &message)
00042 {
00043         if (!getUserById(authMessage.getUniqueId()))
00044         {
00045                 message = 
00046                         "This server is running a prefered player only game.\n"
00047                         "Your supplied unique id is not in the prefered player list.\n";
00048                 return false;
00049         }
00050 
00051         return true;
00052 }
00053 
00054 bool ServerAuthHandlerPrefered::authenticateUserName(const char *uniqueId, 
00055         const LangString &playername)
00056 {
00057         std::string strName = LangStringUtil::convertFromLang(playername);
00058         UserEntry *userEntry = getUserByName(strName.c_str());
00059         if (!userEntry) return true;
00060         if (0 == strcmp(userEntry->uniqueid.c_str(), uniqueId)) return true;
00061         return false;
00062 }
00063 
00064 void ServerAuthHandlerPrefered::banUser(const char *uniqueId)
00065 {
00066 }
00067 
00068 ServerAuthHandlerPrefered::UserEntry *ServerAuthHandlerPrefered::getUserByName(const char *name)
00069 {
00070         load();
00071 
00072         std::list<UserEntry>::iterator itor;
00073         for (itor = entries_.begin();
00074                 itor != entries_.end();
00075                 itor++)
00076         {
00077                 UserEntry &entry = *itor;
00078                 if (0 == strcmp(entry.name.c_str(), name))
00079                 {
00080                         return &entry;
00081                 }
00082         }
00083         return 0;
00084 }
00085 
00086 ServerAuthHandlerPrefered::UserEntry *ServerAuthHandlerPrefered::getUserById(const char *uniqueId)
00087 {
00088         load();
00089 
00090         std::list<UserEntry>::iterator itor;
00091         for (itor = entries_.begin();
00092                 itor != entries_.end();
00093                 itor++)
00094         {
00095                 UserEntry &entry = *itor;
00096                 if (0 == strcmp(entry.uniqueid.c_str(), uniqueId))
00097                 {
00098                         return &entry;
00099                 }
00100         }
00101         return 0;
00102 }
00103 
00104 bool ServerAuthHandlerPrefered::load()
00105 {
00106         std::string filename = 
00107                 S3D::getSettingsFile(S3D::formatStringBuffer("preferedplayers-%i.xml", 
00108                         ScorchedServer::instance()->getOptionsGame().getPortNo()));
00109         if (!::S3D::fileExists(filename)) return true;
00110 
00111         time_t fileTime = S3D::fileModTime(filename);
00112         if (fileTime == lastReadTime_) return true;
00113 
00114         XMLFile file;
00115         if (!file.readFile(filename))
00116         {
00117                 Logger::log(S3D::formatStringBuffer("Failed to parse user file \"%s\"\n%s", 
00118                         filename.c_str(), file.getParserError()));
00119                 return false;
00120         }
00121 
00122         Logger::log(S3D::formatStringBuffer("Refreshing user list %s", filename.c_str()));
00123         lastReadTime_ = (unsigned int) fileTime;
00124         entries_.clear();
00125         if (!file.getRootNode()) return true; // Empty File
00126 
00127         std::list<XMLNode *>::iterator childrenItor;
00128         std::list<XMLNode *> &children = file.getRootNode()->getChildren();
00129         for (childrenItor = children.begin();
00130                  childrenItor != children.end();
00131                 childrenItor++)
00132         {
00133                 XMLNode *currentNode = (*childrenItor);
00134 
00135                 UserEntry entry;
00136                 if (!currentNode->getNamedChild("name", entry.name)) return false;
00137                 if (!currentNode->getNamedChild("uniqueid", entry.uniqueid)) return false;
00138                 if (!currentNode->failChildren()) return false;
00139                 entries_.push_back(entry);
00140         }
00141         return true;
00142 }

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3