/***************************************************************************** * PokerTH - The open source texas holdem engine * * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * * * * You should have received a copy of the GNU Affero General Public License * * along with this program. If not, see . * * * * * * Additional permission under GNU AGPL version 3 section 7 * * * * If you modify this program, or any covered work, by linking or * * combining it with the OpenSSL project's OpenSSL library (or a * * modified version of that library), containing parts covered by the * * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH * * (Felix Hammer, Florian Thauer, Lothar May) grant you additional * * permission to convey the resulting work. * * Corresponding Source for a non-source form of such a combination * * shall include the source code for the parts of OpenSSL used as well * * as that of the covered work. * *****************************************************************************/ #include "cleanerconfig.h" #include #define MODUS 0711 #ifdef _WIN32 #include #include #endif #include #include #include #include #include #include #include using namespace std; CleanerConfig::CleanerConfig() { // !!!! Revisionsnummer der Configdefaults !!!!! configRev = 10; // Pfad und Dateinamen setzen #ifdef _WIN32 const char *appDataPath = getenv("AppData"); if (appDataPath && appDataPath[0] != 0) { configFileName = appDataPath; } else { const int MaxPathSize = 1024; char curDir[MaxPathSize + 1]; curDir[0] = 0; _getcwd(curDir, MaxPathSize); curDir[MaxPathSize] = 0; configFileName = curDir; // Testen ob das Verzeichnis beschreibbar ist ofstream tmpFile; const char *tmpFileName = "pokerth_test.tmp"; tmpFile.open((configFileName + "\\" + tmpFileName).c_str()); if (tmpFile) { // Erfolgreich, Verzeichnis beschreibbar. // Datei wieder loeschen. tmpFile.close(); remove((configFileName + "\\" + tmpFileName).c_str()); } else { // Fehlgeschlagen, Verzeichnis nicht beschreibbar curDir[0] = 0; GetTempPathA(MaxPathSize, curDir); curDir[MaxPathSize] = 0; configFileName = curDir; } } //define app-dir configFileName += "\\pokerth\\"; ////define log-dir // logDir = configFileName; // logDir += "log-files\\"; //create directories on first start of app mkdir(configFileName.c_str()); // mkdir(logDir.c_str()); #else //define app-dir const char *homePath = getenv("HOME"); if(homePath) { configFileName = homePath; configFileName += "/.pokerth/"; ////define log-dir // logDir = configFileName; // logDir += "log-files/"; //create directories on first start of app mkdir(configFileName.c_str(), MODUS) ; // mkdir(logDir.c_str(), MODUS); } #endif ostringstream tempIntToString; tempIntToString << configRev; configList.push_back(ConfigInfo("ConfigRevision", CONFIG_TYPE_INT, tempIntToString.str())); configList.push_back(ConfigInfo("Language", CONFIG_TYPE_STRING, getDefaultLanguage())); configList.push_back(ConfigInfo("HostAddress", CONFIG_TYPE_STRING, "0.0.0.0")); configList.push_back(ConfigInfo("DefaultListenPort", CONFIG_TYPE_STRING, "4327")); configList.push_back(ConfigInfo("ClientAuthString", CONFIG_TYPE_STRING, "")); configList.push_back(ConfigInfo("ServerAuthString", CONFIG_TYPE_STRING, "")); configList.push_back(ConfigInfo("WarnLevelToKick", CONFIG_TYPE_INT, "2")); configList.push_back(ConfigInfo("TextFloodLevelToTrigger", CONFIG_TYPE_INT, "3")); configList.push_back(ConfigInfo("CapsFloodCapsNumberToTrigger", CONFIG_TYPE_INT, "10")); configList.push_back(ConfigInfo("LetterRepeatingNumberToTrigger", CONFIG_TYPE_INT, "10")); configList.push_back(ConfigInfo("KickNumberToBan", CONFIG_TYPE_INT, "2")); configList.push_back(ConfigInfo("SecondsToForgetAboutKick", CONFIG_TYPE_INT, "1800")); list badWordsList; badWordsList.push_back("arsch"); badWordsList.push_back("asshole"); badWordsList.push_back("bastard"); badWordsList.push_back("bitch"); badWordsList.push_back("cunt"); badWordsList.push_back(" dick "); badWordsList.push_back("drecksau"); badWordsList.push_back(" fag "); badWordsList.push_back("fagget"); badWordsList.push_back("fotze"); badWordsList.push_back("fuker"); badWordsList.push_back("fuck"); badWordsList.push_back(" fuk "); badWordsList.push_back("gay"); badWordsList.push_back("horny"); badWordsList.push_back("hure"); badWordsList.push_back("idiot"); badWordsList.push_back("mistgeburt"); badWordsList.push_back("missgeburt"); badWordsList.push_back("motherfucker"); badWordsList.push_back("nazi"); badWordsList.push_back("nigga"); badWordsList.push_back("nigger"); badWordsList.push_back("nutte"); badWordsList.push_back("ommak"); badWordsList.push_back("penis"); badWordsList.push_back("pussy"); badWordsList.push_back("schlampe"); badWordsList.push_back("schwanz"); badWordsList.push_back("sex"); badWordsList.push_back("shit"); badWordsList.push_back("sieg heil"); badWordsList.push_back("slut"); badWordsList.push_back("suck"); badWordsList.push_back("whore"); configList.push_back(ConfigInfo("BadWordsList", CONFIG_TYPE_STRING_LIST, "BadWords", badWordsList)); list badWordsExceptionList; badWordsExceptionList.push_back("idiots end"); badWordsExceptionList.push_back("all-in-idiot"); badWordsExceptionList.push_back("all-in idiot"); badWordsExceptionList.push_back("allin idiot"); badWordsExceptionList.push_back("all in idiot"); configList.push_back(ConfigInfo("BadWordsException", CONFIG_TYPE_STRING_LIST, "BadWordsException", badWordsExceptionList)); list urlStringsList; urlStringsList.push_back("http://"); urlStringsList.push_back(".com"); urlStringsList.push_back(".net"); urlStringsList.push_back(".org"); urlStringsList.push_back(".de"); configList.push_back(ConfigInfo("UrlStringsList", CONFIG_TYPE_STRING_LIST, "UrlStrings", urlStringsList)); list urlExceptionStringsList; urlExceptionStringsList.push_back("http://www.esl."); urlExceptionStringsList.push_back("http://www.pokerth.net"); urlExceptionStringsList.push_back("pokerth.net"); configList.push_back(ConfigInfo("UrlExceptionStringsList", CONFIG_TYPE_STRING_LIST, "UrlExceptionStrings", urlExceptionStringsList)); //fill tempList firstTime configBufferList = configList; configFileName += "cleanerconfig.xml"; //Prüfen ob Configfile existiert --> sonst anlegen TiXmlDocument doc(configFileName); if(!doc.LoadFile()) { myConfigState = NONEXISTING; updateConfig(myConfigState); } else { //Check if config revision is ok. Otherwise --> update() int tempRevision = 0; TiXmlHandle docHandle( &doc ); TiXmlElement* confRevision = docHandle.FirstChild( "PokerTHCleaner" ).FirstChild( "Configuration" ).FirstChild( "ConfigRevision" ).ToElement(); if ( confRevision ) { confRevision->QueryIntAttribute("value", &tempRevision ); } if (tempRevision < configRev ) { /*löschen()*/ myConfigState = OLD; updateConfig(myConfigState) ; } } fillBuffer(); } CleanerConfig::~CleanerConfig() { } void CleanerConfig::fillBuffer() { string tempString1(""); string tempString2(""); TiXmlDocument doc(configFileName); if(doc.LoadFile()) { TiXmlHandle docHandle( &doc ); for (size_t i=0; iAttribute("value"); if (tmpStr1) tempString1 = tmpStr1; configBufferList[i].defaultValue = tempString1; const char *tmpStr2 = conf->Attribute("type"); if (tmpStr2) { tempString2 = tmpStr2; if(tempString2 == "list") { list tempStringList2; TiXmlElement* confList = docHandle.FirstChild( "PokerTHCleaner" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).FirstChild().ToElement(); for( ; confList; confList=confList->NextSiblingElement()) { tempStringList2.push_back(confList->Attribute("value")); } configBufferList[i].defaultListValue = tempStringList2; } } } else { qDebug("Could not find the root element in the config file!"); } } } } void CleanerConfig::writeBuffer() const { TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", ""); doc.LinkEndChild( decl ); TiXmlElement * root = new TiXmlElement( "PokerTHCleaner" ); doc.LinkEndChild( root ); TiXmlElement * config; config = new TiXmlElement( "Configuration" ); root->LinkEndChild( config ); size_t i; for (i=0; iLinkEndChild( tmpElement ); tmpElement->SetAttribute("value", configBufferList[i].defaultValue); if(configBufferList[i].type == CONFIG_TYPE_INT_LIST || configBufferList[i].type == CONFIG_TYPE_STRING_LIST) { tmpElement->SetAttribute("type", "list"); list tempList = configBufferList[i].defaultListValue; list::iterator it; for(it = tempList.begin(); it != tempList.end(); ++it) { TiXmlElement *tmpSubElement = new TiXmlElement(configBufferList[i].defaultValue); tmpElement->LinkEndChild( tmpSubElement ); tmpSubElement->SetAttribute("value", *it); } } } doc.SaveFile( configFileName ); } void CleanerConfig::updateConfig(ConfigState myConfigState) { size_t i; if(myConfigState == NONEXISTING) { //Create a new ConfigFile! TiXmlDocument doc; TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", ""); doc.LinkEndChild( decl ); TiXmlElement * root = new TiXmlElement( "PokerTHCleaner" ); doc.LinkEndChild( root ); TiXmlElement * config; config = new TiXmlElement( "Configuration" ); root->LinkEndChild( config ); for (i=0; iLinkEndChild( tmpElement ); tmpElement->SetAttribute("value", stringToUtf8(configList[i].defaultValue)); if(configList[i].type == CONFIG_TYPE_INT_LIST || configList[i].type == CONFIG_TYPE_STRING_LIST) { tmpElement->SetAttribute("type", "list"); list tempList = configList[i].defaultListValue; list::iterator it; for(it = tempList.begin(); it != tempList.end(); ++it) { TiXmlElement *tmpSubElement = new TiXmlElement(configList[i].defaultValue); tmpElement->LinkEndChild( tmpSubElement ); tmpSubElement->SetAttribute("value", *it); } } } doc.SaveFile( configFileName ); } if(myConfigState == OLD) { TiXmlDocument oldDoc(configFileName); //load the old one if(oldDoc.LoadFile()) { string tempString1(""); string tempString2(""); TiXmlDocument newDoc; //Create the new one TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", ""); newDoc.LinkEndChild( decl ); TiXmlElement * root = new TiXmlElement( "PokerTHCleaner" ); newDoc.LinkEndChild( root ); TiXmlElement * config; config = new TiXmlElement( "Configuration" ); root->LinkEndChild( config ); //change configRev and AppDataPath TiXmlElement * confElement0 = new TiXmlElement( "ConfigRevision" ); config->LinkEndChild( confElement0 ); confElement0->SetAttribute("value", configRev); TiXmlHandle oldDocHandle( &oldDoc ); for (i=0; i take over the saved values if(configList[i].name != "ConfigRevision") { // dont update ConfigRevision because it was already set ^ TiXmlElement *tmpElement = new TiXmlElement(configList[i].name); config->LinkEndChild( tmpElement ); const char *tmpStr1 = oldConf->Attribute("value"); if (tmpStr1) tempString1 = tmpStr1; tmpElement->SetAttribute("value", tempString1); //for lists copy elements const char *tmpStr2 = oldConf->Attribute("type"); if (tmpStr2) { tempString2 = tmpStr2; if(tempString2 == "list") { list tempStringList2; TiXmlElement* oldConfList = oldDocHandle.FirstChild( "PokerTHCleaner" ).FirstChild( "Configuration" ).FirstChild( configList[i].name ).FirstChild().ToElement(); for( ; oldConfList; oldConfList=oldConfList->NextSiblingElement()) { tempStringList2.push_back(oldConfList->Attribute("value")); } tmpElement->SetAttribute("type", "list"); list tempList = tempStringList2; list::iterator it; for(it = tempList.begin(); it != tempList.end(); ++it) { TiXmlElement *tmpSubElement = new TiXmlElement(tempString1); tmpElement->LinkEndChild( tmpSubElement ); tmpSubElement->SetAttribute("value", *it); } } } } } else { // if element is not there --> set it with defaultValue TiXmlElement *tmpElement = new TiXmlElement(configList[i].name); config->LinkEndChild( tmpElement ); tmpElement->SetAttribute("value", stringToUtf8(configList[i].defaultValue)); if(configList[i].type == CONFIG_TYPE_INT_LIST || configList[i].type == CONFIG_TYPE_STRING_LIST) { tmpElement->SetAttribute("type", "list"); list tempList = configList[i].defaultListValue; list::iterator it; for(it = tempList.begin(); it != tempList.end(); ++it) { TiXmlElement *tmpSubElement = new TiXmlElement(configList[i].defaultValue); tmpElement->LinkEndChild( tmpSubElement ); tmpSubElement->SetAttribute("value", *it); } } } } newDoc.SaveFile( configFileName ); } else { qDebug("Cannot update config file: Unable to load configuration."); } } } string CleanerConfig::readConfigString(string varName) const { size_t i; string tempString(""); for (i=0; i> tempInt; return tempInt; } list CleanerConfig::readConfigStringList(string varName) const { size_t i; list tempStringList; for (i=0; i CleanerConfig::readConfigIntList(string varName) const { size_t i; list tempStringList; list tempIntList; for (i=0; i::iterator it; for(it = tempStringList.begin(); it != tempStringList.end(); ++it) { isst.str(*it); isst >> tempInt; tempIntList.push_back(tempInt); isst.str(""); isst.clear(); } return tempIntList; } void CleanerConfig::writeConfigInt(string varName, int varCont) { size_t i; ostringstream intToString; for (i=0; i varCont) { size_t i; ostringstream intToString; list stringList; for (i=0; i::iterator it; for(it = varCont.begin(); it != varCont.end(); ++it) { intToString << (*it); stringList.push_back(intToString.str()); intToString.str(""); intToString.clear(); } configBufferList[i].defaultListValue = stringList; } } } void CleanerConfig::writeConfigStringList(string varName, list varCont) { size_t i; for (i=0; i