S_New4/accountManager.cpp
2019-07-08 20:17:11 +02:00

137 lines
4.4 KiB
C++

#include "accountManager.h"
AccountManager::AccountManager(std::string pathToFile, std::string pathToAccountNumberFile)
: pathToAccountNumberFile(pathToAccountNumberFile)
{
std::ifstream ifs(pathToFile);
if(!ifs.is_open()) {
if(writeDefault(pathToFile) != 0)
exit(12);
ifs.open(pathToFile);
if(!ifs.is_open()) {
std::cout << "Konnte Account File nicht öffnen" << std::endl;
exit(13);
}
}
std::string line;
while (std::getline(ifs, line)) {
if(line.length() > 0 && line[0] == '#')
continue;
Account account;
size_t Delimeter = line.find("/");
if(Delimeter == std::string::npos) {
account.Email = line;
account.Password = line;
} else {
account.Email = std::string(line).erase(Delimeter, line.length() - Delimeter);
account.Password = line.erase(0, Delimeter + 1);
}
if(account.Email == "" || account.Password == "")
continue;
else
accounts.push_back(account);
}
ifs.close();
}
Account AccountManager::getNextAccount()
{
if(accounts.size() == 0) {
std::cout << "Keine Accounts vorhanden." << std::endl;
exit(36);
}
size_t accountNumber = getLastAccountNumber();
accountNumber++;
if( accountNumber >= accounts.size() )
accountNumber=0;
if(setLastAccountNumber(accountNumber) != 0)
exit(45);
return accounts.at(accountNumber);
}
int AccountManager::writeDefault(std::string path)
{
std::ofstream ofs(path);
if(!ofs.is_open()) {
perror((std::string("Konnte Account Datei nicht öffnen: ") + path).c_str());
return -1;
}
std::cout << "Erstelle Datei mit Accounts unter: " << path << "..." <<std::endl;
ofs << "# <- Am Anfang der Zeile deaktiviert diese." << std::endl;
ofs << "#Email/Passwort - Falls diese gleich sind, geht auch nur Email oder Passwort" << std::endl;
ofs << "EntzueckendPackenderSkorpion@oida.icu" << std::endl;
ofs << "ExzellentIdealerSchakal@muell.icu" << std::endl;
ofs << "BrillantVitalerSalamander@spam.care" << std::endl;
ofs << "BezauberndRiesigerSchakal@oida.icu" << std::endl;
ofs << "DynamischWarmerBaer@magspam.net" << std::endl;
ofs << "EmotionalLeidenschaftlicherBaer@oida.icu" << std::endl;
ofs << "BewundernswertWarmerSchwan@oida.icu" << std::endl;
ofs << "HuebschUnfassbarerAal@papierkorb.me" << std::endl;
ofs << "FroehlichWundervollerBiber@muellmail.com" << std::endl;
ofs << "BefreiendWilderDelfin@spam.care" << std::endl;
ofs << "FreundlichStilvollerTiger@oida.icu" << std::endl;
ofs << "EchtKoeniglicherStorch@existiert.net" << std::endl;
ofs << "ErhellendSeltenerSeeloewe@ultra.fyi" << std::endl;
ofs << "BefluegeltPrallerSchwan@spam.care" << std::endl;
ofs << "FreundlichZuverlaessigerSchwan@spam.care" << std::endl;
ofs << "HervorragendWundervollerReiher@oida.icu" << std::endl;
ofs << "AnspruchsvollStilvollerUhu@magspam.net" << std::endl;
ofs.close();
return 0;
}
size_t AccountManager::getLastAccountNumber()
{
std::ifstream fStream;
fStream.open(pathToAccountNumberFile);
if(!fStream.is_open()) {
std::ofstream ofs;
ofs.open(pathToAccountNumberFile);
ofs << "0" << std::endl;
ofs.close();
fStream.open(pathToAccountNumberFile);
if(!fStream.is_open()) {
perror("Konnte Account Number Datei nicht erstellen");
exit(34);
}
}
std::string content( (std::istreambuf_iterator<char>(fStream) ), (std::istreambuf_iterator<char>() ) );
return static_cast<size_t>( atoi(content.c_str()) );
}
int AccountManager::setLastAccountNumber(size_t number)
{
std::ofstream ofs;
ofs.open(pathToAccountNumberFile, std::ios::trunc);
if(!ofs.is_open()) {
std::cout << "Account Number Datei ist nicht geöffnet." << std::endl;
return 110;
}
//fStream.clear();
ofs << number << std::endl;
return 0;
}
bool AccountManager::isDirExist(const std::string& path)
{
struct stat info;
if (stat(path.c_str(), &info) != 0) {
return false;
}
return (info.st_mode & S_IFDIR) != 0;
}
bool AccountManager::createDir(std::string path, std::string atLinux)
{
return system(std::string("mkdir " + atLinux + "'" + path +"'").c_str()); // -p if is linux
}