From 250a7d6e7a4c8ffd644931b7b3964289a8f99151 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Wed, 17 Sep 2014 09:36:24 +0300 Subject: [PATCH] Split sgconfig parsers into seaparate files. --- .../plugins/configuration/sgconfig/parser.cpp | 1108 +---------------- .../plugins/configuration/sgconfig/parser.h | 235 +--- .../configuration/sgconfig/parser_admin.cpp | 206 --- .../configuration/sgconfig/parser_admins.cpp | 170 +++ .../configuration/sgconfig/parser_admins.h | 98 ++ .../configuration/sgconfig/parser_auth_by.cpp | 69 +- .../configuration/sgconfig/parser_auth_by.h | 51 +- .../configuration/sgconfig/parser_message.cpp | 138 ++ .../configuration/sgconfig/parser_message.h | 62 + .../sgconfig/parser_server_info.cpp | 57 + .../sgconfig/parser_server_info.h | 57 + .../configuration/sgconfig/parser_tariff.cpp | 483 ------- .../configuration/sgconfig/parser_tariffs.cpp | 308 +++++ .../configuration/sgconfig/parser_tariffs.h | 100 ++ .../sgconfig/parser_user_info.cpp | 52 +- .../configuration/sgconfig/parser_user_info.h | 29 +- .../configuration/sgconfig/parser_users.cpp | 814 ++++++++++++ .../configuration/sgconfig/parser_users.h | 144 +++ 18 files changed, 2089 insertions(+), 2092 deletions(-) delete mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_admins.h create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_message.h create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h delete mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp create mode 100644 projects/stargazer/plugins/configuration/sgconfig/parser_users.h diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp index 45ef82c8..52c0ea9b 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp @@ -32,1122 +32,24 @@ #include #include // sprintf -#include - -#define UNAME_LEN (256) - -namespace -{ - -std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0) -{ -std::string answer; - -if (loginInStart) - answer += ""; -else - answer += ""; - -answer += ""; - -if (user.GetProperty().password.ModificationTime() > lastTime) - { - if (showPass) - answer += ""; - else - answer += ""; - } - -if (user.GetProperty().cash.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().freeMb.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().credit.ModificationTime() > lastTime) - answer += ""; - -if (user.GetProperty().nextTariff.Get() != "") - { - if (user.GetProperty().tariffName.ModificationTime() > lastTime || - user.GetProperty().nextTariff.ModificationTime() > lastTime) - answer += ""; - } -else - { - if (user.GetProperty().tariffName.ModificationTime() > lastTime) - answer += ""; - } - -if (user.GetProperty().note.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().phone.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().address.ModificationTime() > lastTime) - answer += "
"; -if (user.GetProperty().email.ModificationTime() > lastTime) - answer += ""; - -std::vector *> userdata; -userdata.push_back(user.GetProperty().userdata0.GetPointer()); -userdata.push_back(user.GetProperty().userdata1.GetPointer()); -userdata.push_back(user.GetProperty().userdata2.GetPointer()); -userdata.push_back(user.GetProperty().userdata3.GetPointer()); -userdata.push_back(user.GetProperty().userdata4.GetPointer()); -userdata.push_back(user.GetProperty().userdata5.GetPointer()); -userdata.push_back(user.GetProperty().userdata6.GetPointer()); -userdata.push_back(user.GetProperty().userdata7.GetPointer()); -userdata.push_back(user.GetProperty().userdata8.GetPointer()); -userdata.push_back(user.GetProperty().userdata9.GetPointer()); - -for (size_t i = 0; i < userdata.size(); i++) - if (userdata[i]->ModificationTime() > lastTime) - answer += "Get()) + "\" />"; - -if (user.GetProperty().realName.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().group.ModificationTime() > lastTime) - answer += ""; -if (user.GetConnectedModificationTime() > lastTime) - answer += std::string(""; -if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime) - answer += std::string(""; -if (user.GetCurrIPModificationTime() > lastTime) - answer += ""; -if (user.GetPingTime() > lastTime) - answer += ""; -if (user.GetProperty().ips.ModificationTime() > lastTime) - answer += ""; - -answer += " lastTime) - for (size_t j = 0; j < DIR_NUM; j++) - answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\""; -if (user.GetProperty().down.ModificationTime() > lastTime) - for (size_t j = 0; j < DIR_NUM; j++) - answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\""; -if (user.GetSessionUploadModificationTime() > lastTime) - for (size_t j = 0; j < DIR_NUM; j++) - answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\""; -if (user.GetSessionDownloadModificationTime() > lastTime) - for (size_t j = 0; j < DIR_NUM; j++) - answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\""; -answer += "/>"; - -if (user.GetProperty().disabled.ModificationTime() > lastTime) - answer += std::string(""; -if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime) - answer += std::string(""; -if (user.GetProperty().passive.ModificationTime() > lastTime) - answer += std::string(""; -if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime) - answer += ""; -if (user.GetProperty().creditExpire.ModificationTime() > lastTime) - answer += ""; - -if (lastTime == 0) - { - answer += ""; - std::vector list(user.GetAuthorizers()); - for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) - answer += ""; - answer += ""; - } - -answer += ""; - -return answer; -} - -} // namespace anonymous - -//----------------------------------------------------------------------------- -// GET SERVER INFO //----------------------------------------------------------------------------- -int PARSER_GET_SERVER_INFO::ParseStart(void *, const char *el, const char **) -{ -answer.clear(); -if (strcasecmp(el, "GetServerInfo") == 0) - { - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_GET_SERVER_INFO::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "GetServerInfo") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_GET_SERVER_INFO::CreateAnswer() -{ -char un[UNAME_LEN]; -struct utsname utsn; - -uname(&utsn); -un[0] = 0; - -strcat(un, utsn.sysname); -strcat(un, " "); -strcat(un, utsn.release); -strcat(un, " "); -strcat(un, utsn.machine); -strcat(un, " "); -strcat(un, utsn.nodename); - -answer.clear(); -answer += ""; -answer += std::string(""; -answer += "Count()) + "\"/>"; -answer += ""; -answer += "Count()) + "\"/>"; -answer += std::string(""; -answer += ""; -answer += "GetDayFee()) + "\"/>"; - -for (size_t i = 0; i< DIR_NUM; i++) - answer += "GetDirName(i)) + "\"/>"; - -answer += ""; -} -//----------------------------------------------------------------------------- -// GET USER -//----------------------------------------------------------------------------- -int PARSER_GET_USER::ParseStart(void *, const char *el, const char **attr) -{ -if (strcasecmp(el, "GetUser") == 0) - { - if (attr[0] && attr[1]) - login = attr[1]; - else - { - //login.clear(); - login.erase(login.begin(), login.end()); - return -1; - } - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_GET_USER::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "GetUser") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_GET_USER::CreateAnswer() -{ -USER_PTR u; - -answer.clear(); - -if (users->FindByName(login, &u)) - { - answer = ""; - return; - } - -answer = UserToXML(*u, false, currAdmin->GetPriv()->userConf || currAdmin->GetPriv()->userPasswd); -} -//----------------------------------------------------------------------------- -// GET USERS -//----------------------------------------------------------------------------- -int PARSER_GET_USERS::ParseStart(void *, const char *el, const char ** attr) -{ -/*if (attr && *attr && *(attr+1)) - { - printfd(__FILE__, "attr=%s %s\n", *attr, *(attr+1)); - } -else - { - printfd(__FILE__, "attr = NULL\n"); - }*/ - -lastUpdateFound = false; -if (strcasecmp(el, "GetUsers") == 0) - { - while (attr && *attr && *(attr+1)) - { - if (strcasecmp(*attr, "LastUpdate") == 0) - { - if (str2x(*(attr+1), lastUserUpdateTime) == 0) - { - //printfd(__FILE__, "lastUserUpdateTime=%d\n", lastUserUpdateTime); - lastUpdateFound = true; - } - else - { - //printfd(__FILE__, "NO lastUserUpdateTime\n"); - } - } - ++attr; - } - - return 0; - } -return -1; -} +// BASE PARSER //----------------------------------------------------------------------------- -int PARSER_GET_USERS::ParseEnd(void *, const char *el) +int BASE_PARSER::Start(void *, const char *el, const char **) { -if (strcasecmp(el, "GetUsers") == 0) - { - CreateAnswer(); +if (strcasecmp(el, tag.c_str()) == 0) return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::CreateAnswer() -{ -answer.clear(); - -int h = users->OpenSearch(); -if (!h) - { - printfd(__FILE__, "users->OpenSearch() error\n"); - users->CloseSearch(h); - return; - } - -if (lastUpdateFound) - answer += ""; -else - answer += ""; - -USER_PTR u; - -while (users->SearchNext(h, &u) == 0) - answer += UserToXML(*u, true, currAdmin->GetPriv()->userConf || currAdmin->GetPriv()->userPasswd, lastUserUpdateTime); - -users->CloseSearch(h); - -answer += ""; -} -//----------------------------------------------------------------------------- -// ADD USER -//----------------------------------------------------------------------------- -int PARSER_ADD_USER::ParseStart(void *, const char *el, const char **attr) -{ -depth++; - -if (depth == 1) - { - if (strcasecmp(el, "AddUser") == 0) - { - return 0; - } - } -else - { - if (strcasecmp(el, "login") == 0) - { - login = attr[1]; - return 0; - } - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_ADD_USER::ParseEnd(void *, const char *el) -{ -if (depth == 1) - { - if (strcasecmp(el, "AddUser") == 0) - { - CreateAnswer(); - depth--; - return 0; - } - } - -depth--; -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_ADD_USER::Reset() -{ -BASE_PARSER::Reset(); -depth = 0; -} -//----------------------------------------------------------------------------- -void PARSER_ADD_USER::CreateAnswer() -{ -if (CheckUserData() == 0) - answer = ""; -else - answer = ""; -} -//----------------------------------------------------------------------------- -int PARSER_ADD_USER::CheckUserData() -{ -USER_PTR u; -if (users->FindByName(login, &u)) - { - return users->Add(login, currAdmin); - } -return -1; -} -//----------------------------------------------------------------------------- -// PARSER CHG USER -//----------------------------------------------------------------------------- -PARSER_CHG_USER::PARSER_CHG_USER() - : BASE_PARSER(), - usr(NULL), - ucr(NULL), - upr(NULL), - downr(NULL), - cashMsg(), - login(), - cashMustBeAdded(false), - res(0) -{ -Reset(); -} -//----------------------------------------------------------------------------- -PARSER_CHG_USER::~PARSER_CHG_USER() -{ -delete usr; -delete ucr; -delete[] upr; -delete[] downr; -} -//----------------------------------------------------------------------------- -void PARSER_CHG_USER::Reset() -{ -depth = 0; -delete usr; - -delete ucr; - -delete[] upr; - -delete[] downr; - -usr = new USER_STAT_RES; -ucr = new USER_CONF_RES; - -upr = new RESETABLE[DIR_NUM]; -downr = new RESETABLE[DIR_NUM]; -} -//----------------------------------------------------------------------------- -std::string PARSER_CHG_USER::EncChar2String(const char * strEnc) -{ -std::string str; -Decode21str(str, strEnc); -return str; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_USER::ParseStart(void *, const char *el, const char **attr) -{ -depth++; - -if (depth == 1) - { - if (strcasecmp(el, "SetUser") == 0) - { - return 0; - } - } -else - { - //printfd(__FILE__, "el=%s\n", el); - if (strcasecmp(el, "login") == 0) - { - login = attr[1]; - return 0; - } - - if (strcasecmp(el, "ip") == 0) - { - try - { - ucr->ips = StrToIPS(attr[1]); - } - catch (...) - { - printfd(__FILE__, "StrToIPS Error!\n"); - } - } - - if (strcasecmp(el, "password") == 0) - { - ucr->password = attr[1]; - return 0; - } - - if (strcasecmp(el, "address") == 0) - { - ucr->address = EncChar2String(attr[1]); - return 0; - } - if (strcasecmp(el, "aonline") == 0) - { - ucr->alwaysOnline = (*(attr[1]) != '0'); - return 0; - } - - if (strcasecmp(el, "cash") == 0) - { - if (attr[2] && (strcasecmp(attr[2], "msg") == 0)) - { - cashMsg = EncChar2String(attr[3]); - } - - double cash; - if (strtodouble2(attr[1], cash) == 0) - usr->cash = cash; - - if (strcasecmp(attr[0], "set") == 0) - cashMustBeAdded = false; - - if (strcasecmp(attr[0], "add") == 0) - cashMustBeAdded = true; - - return 0; - } - - if (strcasecmp(el, "CreditExpire") == 0) - { - long int creditExpire = 0; - if (str2x(attr[1], creditExpire) == 0) - ucr->creditExpire = (time_t)creditExpire; - - return 0; - } - - if (strcasecmp(el, "credit") == 0) - { - double credit; - if (strtodouble2(attr[1], credit) == 0) - ucr->credit = credit; - return 0; - } - - if (strcasecmp(el, "freemb") == 0) - { - double freeMb; - if (strtodouble2(attr[1], freeMb) == 0) - usr->freeMb = freeMb; - return 0; - } - - if (strcasecmp(el, "down") == 0) - { - int down = 0; - if (str2x(attr[1], down) == 0) - ucr->disabled = down; - return 0; - } - - if (strcasecmp(el, "DisableDetailStat") == 0) - { - int disabledDetailStat = 0; - if (str2x(attr[1], disabledDetailStat) == 0) - ucr->disabledDetailStat = disabledDetailStat; - return 0; - } - - if (strcasecmp(el, "email") == 0) - { - ucr->email = EncChar2String(attr[1]); - return 0; - } - - for (int i = 0; i < USERDATA_NUM; i++) - { - char name[15]; - sprintf(name, "userdata%d", i); - if (strcasecmp(el, name) == 0) - { - ucr->userdata[i] = EncChar2String(attr[1]); - return 0; - } - } - - if (strcasecmp(el, "group") == 0) - { - ucr->group = EncChar2String(attr[1]); - return 0; - } - - if (strcasecmp(el, "note") == 0) - { - ucr->note = EncChar2String(attr[1]); - return 0; - } - - if (strcasecmp(el, "passive") == 0) - { - int passive = 0; - if (str2x(attr[1], passive) == 0) - ucr->passive = passive; - return 0; - } - - if (strcasecmp(el, "phone") == 0) - { - ucr->phone = EncChar2String(attr[1]); - return 0; - } - - if (strcasecmp(el, "Name") == 0) - { - ucr->realName = EncChar2String(attr[1]); - return 0; - } - - if (strcasecmp(el, "traff") == 0) - { - int j = 0; - DIR_TRAFF dtu; - DIR_TRAFF dtd; - uint64_t t = 0; - while (attr[j]) - { - int dir = attr[j][2] - '0'; - - if (strncasecmp(attr[j], "md", 2) == 0) - { - str2x(attr[j+1], t); - dtd[dir] = t; - downr[dir] = t; - } - if (strncasecmp(attr[j], "mu", 2) == 0) - { - str2x(attr[j+1], t); - dtu[dir] = t; - upr[dir] = t; - } - j+=2; - } - return 0; - } - - if (strcasecmp(el, "tariff") == 0) - { - if (strcasecmp(attr[0], "now") == 0) - ucr->tariffName = attr[1]; - - if (strcasecmp(attr[0], "delayed") == 0) - ucr->nextTariff = attr[1]; - - return 0; - } - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_USER::ParseEnd(void *, const char *el) -{ -if (depth == 1) - { - if (strcasecmp(el, "SetUser") == 0) - { - AplayChanges(); - CreateAnswer(); - depth--; - return 0; - } - } - -depth--; -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_CHG_USER::CreateAnswer() -{ -switch (res) - { - case 0: - answer = ""; - break; - case -1: - answer = ""; - break; - case -2: - answer = ""; - break; - default: - answer = ""; - break; - } - -} -//----------------------------------------------------------------------------- -int PARSER_CHG_USER::AplayChanges() -{ -printfd(__FILE__, "PARSER_CHG_USER::AplayChanges()\n"); -USER_PTR u; - -res = 0; -if (users->FindByName(login, &u)) - { - res = -1; - return -1; - } - -bool check = false; -bool alwaysOnline = u->GetProperty().alwaysOnline; -if (!ucr->alwaysOnline.empty()) - { - check = true; - alwaysOnline = ucr->alwaysOnline.const_data(); - } -bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP(); -if (!ucr->ips.empty()) - { - check = true; - onlyOneIP = ucr->ips.const_data().OnlyOneIP(); - } - -if (check && alwaysOnline && !onlyOneIP) - { - printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n"); - GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin->GetLogStr().c_str()); - res = -1; - return -1; - } - -for (size_t i = 0; i < ucr->ips.const_data().Count(); ++i) - { - CONST_USER_PTR user; - uint32_t ip = ucr->ips.const_data().operator[](i).ip; - if (users->IsIPInUse(ip, login, &user)) - { - printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str()); - GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin->GetLogStr().c_str(), inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str()); - res = -1; - return -1; - } - } - -if (!ucr->ips.empty()) - if (!u->GetProperty().ips.Set(ucr->ips.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->alwaysOnline.empty()) - if (!u->GetProperty().alwaysOnline.Set(ucr->alwaysOnline.const_data(), - currAdmin, login, store)) - res = -1; - -if (!ucr->address.empty()) - if (!u->GetProperty().address.Set(ucr->address.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->creditExpire.empty()) - if (!u->GetProperty().creditExpire.Set(ucr->creditExpire.const_data(), - currAdmin, login, store)) - res = -1; - -if (!ucr->credit.empty()) - if (!u->GetProperty().credit.Set(ucr->credit.const_data(), currAdmin, login, store)) - res = -1; - -if (!usr->freeMb.empty()) - if (!u->GetProperty().freeMb.Set(usr->freeMb.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->disabled.empty()) - if (!u->GetProperty().disabled.Set(ucr->disabled.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->disabledDetailStat.empty()) - if (!u->GetProperty().disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->email.empty()) - if (!u->GetProperty().email.Set(ucr->email.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->group.empty()) - if (!u->GetProperty().group.Set(ucr->group.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->note.empty()) - if (!u->GetProperty().note.Set(ucr->note.const_data(), currAdmin, login, store)) - res = -1; - -std::vector *> userdata; -userdata.push_back(u->GetProperty().userdata0.GetPointer()); -userdata.push_back(u->GetProperty().userdata1.GetPointer()); -userdata.push_back(u->GetProperty().userdata2.GetPointer()); -userdata.push_back(u->GetProperty().userdata3.GetPointer()); -userdata.push_back(u->GetProperty().userdata4.GetPointer()); -userdata.push_back(u->GetProperty().userdata5.GetPointer()); -userdata.push_back(u->GetProperty().userdata6.GetPointer()); -userdata.push_back(u->GetProperty().userdata7.GetPointer()); -userdata.push_back(u->GetProperty().userdata8.GetPointer()); -userdata.push_back(u->GetProperty().userdata9.GetPointer()); - -for (int i = 0; i < (int)userdata.size(); i++) - { - if (!ucr->userdata[i].empty()) - { - if(!userdata[i]->Set(ucr->userdata[i].const_data(), currAdmin, login, store)) - res = -1; - } - } - -if (!ucr->passive.empty()) - if (!u->GetProperty().passive.Set(ucr->passive.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->password.empty()) - if (!u->GetProperty().password.Set(ucr->password.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->phone.empty()) - if (!u->GetProperty().phone.Set(ucr->phone.const_data(), currAdmin, login, store)) - res = -1; - -if (!ucr->realName.empty()) - if (!u->GetProperty().realName.Set(ucr->realName.const_data(), currAdmin, login, store)) - res = -1; - - -if (!usr->cash.empty()) - { - //if (*currAdmin->GetPriv()->userCash) - { - if (cashMustBeAdded) - { - if (!u->GetProperty().cash.Set(usr->cash.const_data() + u->GetProperty().cash, - currAdmin, - login, - store, - cashMsg)) - res = -1; - } - else - { - if (!u->GetProperty().cash.Set(usr->cash.const_data(), currAdmin, login, store, cashMsg)) - res = -1; - } - } - } - - -if (!ucr->tariffName.empty()) - { - if (tariffs->FindByName(ucr->tariffName.const_data())) - { - if (!u->GetProperty().tariffName.Set(ucr->tariffName.const_data(), currAdmin, login, store)) - res = -1; - u->ResetNextTariff(); - } - else - { - //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str()); - res = -1; - } - } - -if (!ucr->nextTariff.empty()) - { - if (tariffs->FindByName(ucr->nextTariff.const_data())) - { - if (!u->GetProperty().nextTariff.Set(ucr->nextTariff.const_data(), currAdmin, login, store)) - res = -1; - } - else - { - //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str()); - res = -1; - } - } - -DIR_TRAFF up = u->GetProperty().up; -DIR_TRAFF down = u->GetProperty().down; -int upCount = 0; -int downCount = 0; -for (int i = 0; i < DIR_NUM; i++) - { - if (!upr[i].empty()) - { - up[i] = upr[i].data(); - upCount++; - } - if (!downr[i].empty()) - { - down[i] = downr[i].data(); - downCount++; - } - } - -if (upCount) - if (!u->GetProperty().up.Set(up, currAdmin, login, store)) - res = -1; - -if (downCount) - if (!u->GetProperty().down.Set(down, currAdmin, login, store)) - res = -1; - -u->WriteConf(); -u->WriteStat(); - -return 0; -} -//----------------------------------------------------------------------------- -// SEND MESSAGE -//----------------------------------------------------------------------------- -int PARSER_SEND_MESSAGE::ParseStart(void *, const char *el, const char **attr) -{ -if (strcasecmp(el, "Message") == 0) - { - for (int i = 0; i < 14; i++) - { - if (attr[i] == NULL) - { - result = res_params_error; - CreateAnswer(); - printfd(__FILE__, "To few parameters\n"); - return 0; - } - } - - for (int i = 0; i < 14; i+=2) - { - if (strcasecmp(attr[i], "login") == 0) - { - ParseLogins(attr[i+1]); - /*if (users->FindByName(login, &u)) - { - result = res_unknown; - break; - }*/ - } - - if (strcasecmp(attr[i], "MsgVer") == 0) - { - str2x(attr[i+1], msg.header.ver); - if (msg.header.ver != 1) - result = res_params_error; - } - - if (strcasecmp(attr[i], "MsgType") == 0) - { - str2x(attr[i+1], msg.header.type); - if (msg.header.type != 1) - result = res_params_error; - } - - if (strcasecmp(attr[i], "Repeat") == 0) - { - str2x(attr[i+1], msg.header.repeat); - if (msg.header.repeat < 0) - result = res_params_error; - } - - if (strcasecmp(attr[i], "RepeatPeriod") == 0) - { - str2x(attr[i+1], msg.header.repeatPeriod); - } - - if (strcasecmp(attr[i], "ShowTime") == 0) - { - str2x(attr[i+1], msg.header.showTime); - } - - if (strcasecmp(attr[i], "Text") == 0) - { - Decode21str(msg.text, attr[i+1]); - result = res_ok; - } - } - return 0; - } return -1; } //----------------------------------------------------------------------------- -int PARSER_SEND_MESSAGE::ParseEnd(void *, const char *el) +int BASE_PARSER::End(void *, const char *el) { -//MSG msg; -if (strcasecmp(el, "Message") == 0) +if (strcasecmp(el, tag.c_str()) == 0) { - result = res_unknown; - for (unsigned i = 0; i < logins.size(); i++) - { - if (users->FindByName(logins[i], &u)) - { - printfd(__FILE__, "User not found. %s\n", logins[i].c_str()); - continue; - } - msg.header.creationTime = static_cast(stgTime); - u->AddMessage(&msg); - result = res_ok; - } - /*if (result == res_ok) - { - if (strcmp(login, "*") == 0) - { - msg.text = text; - msg.prio = pri; - printfd(__FILE__, "SendMsg text: %s\n", text); - users->GetAllUsers(SendMessageAllUsers, &msg); - } - else - { - u->AddMessage(pri, text); - } - }*/ CreateAnswer(); return 0; } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_SEND_MESSAGE::ParseLogins(const char * login) -{ -char * p; -char * l = new char[strlen(login) + 1]; -strcpy(l, login); -p = strtok(l, ":"); -logins.clear(); -while(p) - { - logins.push_back(p); - p = strtok(NULL, ":"); - } -delete[] l; -return 0; -} -//----------------------------------------------------------------------------- -void PARSER_SEND_MESSAGE::CreateAnswer() -{ -switch (result) - { - case res_ok: - answer = ""; - break; - case res_params_error: - printfd(__FILE__, "res_params_error\n"); - answer = ""; - break; - case res_unknown: - printfd(__FILE__, "res_unknown\n"); - answer = ""; - break; - default: - printfd(__FILE__, "res_default\n"); - } - -} -//----------------------------------------------------------------------------- -// DEL USER -//----------------------------------------------------------------------------- -int PARSER_DEL_USER::ParseStart(void *, const char *el, const char **attr) -{ -res = 0; -if (strcasecmp(el, "DelUser") == 0) - { - if (attr[0] == NULL || attr[1] == NULL) - { - //CreateAnswer("Parameters error!"); - CreateAnswer(); - return 0; - } - - if (users->FindByName(attr[1], &u)) - { - res = 1; - CreateAnswer(); - return 0; - } - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_DEL_USER::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "DelUser") == 0) - { - if (!res) - users->Del(u->GetLogin(), currAdmin); - - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_DEL_USER::CreateAnswer() -{ -if (res) - answer = ""; -else - answer = ""; -} -//----------------------------------------------------------------------------- -// CHECK USER -// -//----------------------------------------------------------------------------- -int PARSER_CHECK_USER::ParseStart(void *, const char *el, const char **attr) -{ -if (strcasecmp(el, "CheckUser") == 0) - { - if (attr[0] == NULL || attr[1] == NULL - || attr[2] == NULL || attr[3] == NULL) - { - CreateAnswer("Invalid parameters."); - printfd(__FILE__, "PARSER_CHECK_USER - attr err\n"); - return 0; - } - - USER_PTR user; - if (users->FindByName(attr[1], &user)) - { - CreateAnswer("User not found."); - printfd(__FILE__, "PARSER_CHECK_USER - login err\n"); - return 0; - } - - if (strcmp(user->GetProperty().password.Get().c_str(), attr[3])) - { - CreateAnswer("Wrong password."); - printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n"); - return 0; - } - - CreateAnswer(NULL); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_CHECK_USER::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "CheckUser") == 0) - { - return 0; - } return -1; } -//----------------------------------------------------------------------------- -void PARSER_CHECK_USER::CreateAnswer(const char * error) -{ -if (error) - answer = std::string(""; -else - answer = ""; -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.h b/projects/stargazer/plugins/configuration/sgconfig/parser.h index 81bd454b..2e68f8e9 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.h @@ -42,19 +42,21 @@ class USER_CONF_RES; //----------------------------------------------------------------------------- class BASE_PARSER { public: - BASE_PARSER() + BASE_PARSER(const ADMIN & admin, const std::string & t) : strError(), admins(NULL), users(NULL), tariffs(NULL), store(NULL), settings(NULL), - currAdmin(NULL), - depth(0) + currAdmin(admin), + depth(0), + tag(t) {} virtual ~BASE_PARSER() {} - virtual int ParseStart(void *data, const char *el, const char **attr) = 0; - virtual int ParseEnd(void *data, const char *el) = 0; + virtual int Start(void *data, const char *el, const char **attr); + virtual int End(void *data, const char *el); + virtual void Reset() { answer.clear(); depth = 0; } void SetUsers(USERS * u) { users = u; } @@ -63,9 +65,11 @@ public: void SetStore(STORE * s) { store = s; } void SetStgSettings(const SETTINGS * s) { settings = s; } - void SetCurrAdmin(ADMIN & cua) { currAdmin = &cua; } const std::string & GetStrError() const { return strError; } const std::string & GetAnswer() const { return answer; } + const std::string & GetTag() const { return tag; } + std::string GetOpenTag() const { return "<" + tag + ">"; } + std::string GetCloseTag() const { return ""; } protected: BASE_PARSER(const BASE_PARSER & rvalue); @@ -77,227 +81,14 @@ protected: TARIFFS * tariffs; STORE * store; const SETTINGS * settings; - ADMIN * currAdmin; + const ADMIN & currAdmin; int depth; std::string answer; -}; -//----------------------------------------------------------------------------- -class PARSER_GET_ADMINS: public BASE_PARSER { -public: - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_ADD_ADMIN: public BASE_PARSER { -public: - PARSER_ADD_ADMIN() : BASE_PARSER(), adminToAdd() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string adminToAdd; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_DEL_ADMIN: public BASE_PARSER { -public: - PARSER_DEL_ADMIN() : BASE_PARSER(), adminToDel() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string adminToDel; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_CHG_ADMIN: public BASE_PARSER { -public: - PARSER_CHG_ADMIN() : BASE_PARSER(), login(), password(), privAsString() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - RESETABLE login; - RESETABLE password; - RESETABLE privAsString; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_GET_SERVER_INFO: public BASE_PARSER { -public: - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_GET_USER: public BASE_PARSER { -public: - PARSER_GET_USER() : BASE_PARSER(), login() {} - ~PARSER_GET_USER() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string login; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_GET_USERS: public BASE_PARSER { -public: - PARSER_GET_USERS() : BASE_PARSER(), lastUserUpdateTime(0), lastUpdateFound(false) {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - time_t lastUserUpdateTime; - bool lastUpdateFound; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_GET_TARIFFS: public BASE_PARSER { -public: - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_ADD_TARIFF: public BASE_PARSER { -public: - PARSER_ADD_TARIFF() : BASE_PARSER(), tariffToAdd() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string tariffToAdd; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_DEL_TARIFF: public BASE_PARSER { -public: - PARSER_DEL_TARIFF() : BASE_PARSER(), tariffToDel() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string tariffToDel; - - void CreateAnswer(); -}; -//----------------------------------------------------------------------------- -class PARSER_CHG_TARIFF: public BASE_PARSER { -public: - PARSER_CHG_TARIFF() : BASE_PARSER(), td() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - TARIFF_DATA_RES td; - - int ParseSlashedIntParams(int paramsNum, const std::string & s, int * params); - int ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params); - int CheckTariffData(); - int AplayChanges(); - void CreateAnswer(); -}; -//-----------------------------------------------------------------------------/ -class PARSER_ADD_USER: public BASE_PARSER { -public: - PARSER_ADD_USER() : BASE_PARSER(), login() {} - ~PARSER_ADD_USER() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string login; - - int CheckUserData(); - void CreateAnswer(); - void Reset(); -}; -//----------------------------------------------------------------------------- -class PARSER_CHG_USER: public BASE_PARSER { -public: - PARSER_CHG_USER(); - ~PARSER_CHG_USER(); - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - USER_STAT_RES * usr; - USER_CONF_RES * ucr; - RESETABLE * upr; - RESETABLE * downr; - std::string cashMsg; - std::string login; - bool cashMustBeAdded; - int res; - - PARSER_CHG_USER(const PARSER_CHG_USER & rvalue); - PARSER_CHG_USER & operator=(const PARSER_CHG_USER & rvalue); - - std::string EncChar2String(const char *); - int AplayChanges(); - void CreateAnswer(); - void Reset(); -}; -//----------------------------------------------------------------------------- -class PARSER_DEL_USER: public BASE_PARSER { -public: - PARSER_DEL_USER() : BASE_PARSER(), res(0), u(NULL) {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); + std::string tag; private: - int res; - USER * u; - - PARSER_DEL_USER(const PARSER_DEL_USER & rvalue); - PARSER_DEL_USER & operator=(const PARSER_DEL_USER & rvalue); - - void CreateAnswer(); + virtual void CreateAnswer() = 0; }; //----------------------------------------------------------------------------- -class PARSER_CHECK_USER: public BASE_PARSER { -public: - PARSER_CHECK_USER() : BASE_PARSER() {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - void CreateAnswer(const char * error); -}; -//----------------------------------------------------------------------------- -class PARSER_SEND_MESSAGE: public BASE_PARSER { -public: - PARSER_SEND_MESSAGE() : BASE_PARSER(), logins(), result(0), msg(), u(NULL) {} - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - enum {res_ok, res_params_error, res_unknown}; - std::vector logins; - int result; - STG_MSG msg; - USER * u; - - PARSER_SEND_MESSAGE(const PARSER_SEND_MESSAGE & rvalue); - PARSER_SEND_MESSAGE & operator=(const PARSER_SEND_MESSAGE & rvalue); - - int ParseLogins(const char * logins); - void CreateAnswer(); -}; //----------------------------------------------------------------------------- #endif //PARSER_H diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp deleted file mode 100644 index fef2efba..00000000 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp +++ /dev/null @@ -1,206 +0,0 @@ -#include "parser.h" - -#include "stg/admins.h" -#include "stg/common.h" - -#include // strcasecmp - -//----------------------------------------------------------------------------- -// GET ADMINS -//----------------------------------------------------------------------------- -int PARSER_GET_ADMINS::ParseStart(void *, const char *el, const char **) -{ -if (strcasecmp(el, "GetAdmins") == 0) - { - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_GET_ADMINS::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "GetAdmins") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_GET_ADMINS::CreateAnswer() -{ -const PRIV * priv = currAdmin->GetPriv(); -if (!priv->adminChg) - { - answer = ""; - return; - } - -answer.clear(); - -answer += ""; -ADMIN_CONF ac; -int h = admins->OpenSearch(); - -while (admins->SearchNext(h, &ac) == 0) - { - unsigned int p = (ac.priv.userStat << 0) + - (ac.priv.userConf << 2) + - (ac.priv.userCash << 4) + - (ac.priv.userPasswd << 6) + - (ac.priv.userAddDel << 8) + - (ac.priv.adminChg << 10) + - (ac.priv.tariffChg << 12); - answer += ""; - } -admins->CloseSearch(h); -answer += ""; -} -//----------------------------------------------------------------------------- - -//----------------------------------------------------------------------------- -// DEL ADMIN -//----------------------------------------------------------------------------- -int PARSER_DEL_ADMIN::ParseStart(void *, const char *el, const char **attr) -{ -strError = ""; -if (strcasecmp(el, "DelAdmin") == 0) - { - adminToDel = attr[1]; - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_DEL_ADMIN::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "DelAdmin") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_DEL_ADMIN::CreateAnswer() -{ -if (admins->Del(adminToDel, currAdmin) == 0) - answer = ""; -else - answer = "GetStrError() + "\"/>"; -} -//----------------------------------------------------------------------------- -// ADD ADMIN -//----------------------------------------------------------------------------- -int PARSER_ADD_ADMIN::ParseStart(void *, const char *el, const char **attr) -{ -if (strcasecmp(el, "AddAdmin") == 0) - { - adminToAdd = attr[1]; - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_ADD_ADMIN::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "AddAdmin") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_ADD_ADMIN::CreateAnswer() -{ -if (admins->Add(adminToAdd, currAdmin) == 0) - answer = ""; -else - answer = "GetStrError() + "\"/>"; -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// CHG ADMIN -//----------------------------------------------------------------------------- -int PARSER_CHG_ADMIN::ParseStart(void *, const char *el, const char **attr) -{ -if (strcasecmp(el, "ChgAdmin") == 0) - { - for (int i = 0; i < 6; i+=2) - { - printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]); - if (attr[i] == NULL) - break; - - if (strcasecmp(attr[i], "Login") == 0) - { - login = attr[i + 1]; - continue; - } - - if (strcasecmp(attr[i], "Priv") == 0) - { - privAsString = attr[i + 1]; - continue; - } - - if (strcasecmp(attr[i], "Password") == 0) - { - password = attr[i + 1]; - continue; - } - } - - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_ADMIN::ParseEnd(void *, const char *el) -{ -if (strcasecmp(el, "ChgAdmin") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_CHG_ADMIN::CreateAnswer() -{ -if (!login.empty()) - { - ADMIN * origAdmin = NULL; - - if (admins->Find(login.data(), &origAdmin)) - { - answer = ""; - return; - } - - ADMIN_CONF conf(origAdmin->GetConf()); - - if (!password.empty()) - conf.password = password.data(); - - if (!privAsString.empty()) - { - int p = 0; - if (str2x(privAsString.data().c_str(), p) < 0) - { - answer = ""; - return; - } - - conf.priv.FromInt(p); - } - - if (admins->Change(conf, currAdmin) != 0) - answer = "GetStrError() + "\"/>"; - else - answer = ""; - } -else - answer = ""; -} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp new file mode 100644 index 00000000..c75dc8a7 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp @@ -0,0 +1,170 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_admins.h" + +#include "stg/admins.h" +#include "stg/common.h" + +#include // strcasecmp + +using STG::PARSER::GET_ADMINS; +using STG::PARSER::ADD_ADMIN; +using STG::PARSER::DEL_ADMIN; +using STG::PARSER::CHG_ADMIN; + +void GET_ADMINS::CreateAnswer() +{ + const PRIV * priv = currAdmin.GetPriv(); + if (!priv->adminChg) + { + answer = ""; + return; + } + + answer.clear(); + + answer += GetOpenTag(); + ADMIN_CONF ac; + int h = m_admins.OpenSearch(); + + while (m_admins.SearchNext(h, &ac) == 0) + { + unsigned int p = (ac.priv.userStat << 0) + + (ac.priv.userConf << 2) + + (ac.priv.userCash << 4) + + (ac.priv.userPasswd << 6) + + (ac.priv.userAddDel << 8) + + (ac.priv.adminChg << 10) + + (ac.priv.tariffChg << 12); + answer += ""; + } + m_admins.CloseSearch(h); + answer += GetCloseTag(); +} + +int DEL_ADMIN::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, tag.c_str()) == 0) + { + admin = attr[1]; + return 0; + } + return -1; +} + +void DEL_ADMIN::CreateAnswer() +{ + if (m_admins.Del(admin, &currAdmin) == 0) + answer = "<" + tag + " Result=\"Ok\"/>"; + else + answer = "<" + tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>"; +} + +int ADD_ADMIN::Start(void *, const char *el, const char **attr) +{ + if (strcasecmp(el, tag.c_str()) == 0) + { + admin = attr[1]; + return 0; + } + return -1; +} + +void ADD_ADMIN::CreateAnswer() +{ + if (m_admins.Add(admin, &currAdmin) == 0) + answer = "<" + tag + " Result=\"Ok\"/>"; + else + answer = "<" + tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>"; +} + +int CHG_ADMIN::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, tag.c_str()) == 0) + { + for (size_t i = 0; i < 6; i += 2) + { + printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]); + if (attr[i] == NULL) + break; + + if (strcasecmp(attr[i], "Login") == 0) + { + login = attr[i + 1]; + continue; + } + + if (strcasecmp(attr[i], "Priv") == 0) + { + privAsString = attr[i + 1]; + continue; + } + + if (strcasecmp(attr[i], "Password") == 0) + { + password = attr[i + 1]; + continue; + } + } + + return 0; + } + return -1; +} + +void CHG_ADMIN::CreateAnswer() +{ + if (!login.empty()) + { + ADMIN * origAdmin = NULL; + + if (m_admins.Find(login, &origAdmin)) + { + answer = "<" + tag + " Result = \"Admin '" + login + "' is not found.\"/>"; + return; + } + + ADMIN_CONF conf(origAdmin->GetConf()); + + if (!password.empty()) + conf.password = password.data(); + + if (!privAsString.empty()) + { + int p = 0; + if (str2x(privAsString.data().c_str(), p) < 0) + { + answer = "<" + tag + " Result = \"Incorrect parameter Priv.\"/>"; + return; + } + + conf.priv.FromInt(p); + } + + if (m_admins.Change(conf, &currAdmin) != 0) + answer = "<" + tag + " Result = \"" + m_admins.GetStrError() + "\"/>"; + else + answer = "<" + tag + " Result = \"Ok\"/>"; + } + else + answer = "<" + tag + " Result = \"Incorrect parameter login.\"/>"; +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h new file mode 100644 index 00000000..63ee6278 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.h @@ -0,0 +1,98 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_ADMINS_H__ +#define __STG_SGCONFIG_PARSER_ADMINS_H__ + +#include "parser.h" + +#include "stg/resetable.h" + +#include + +class ADMINS; +class ADMIN; + +namespace STG +{ +namespace PARSER +{ + +class GET_ADMINS: public BASE_PARSER +{ + public: + GET_ADMINS(const ADMIN & admin, const ADMINS & admins) + : BASE_PARSER(admin, "GetAdmins"), m_admins(admins) {} + + private: + const ADMINS & m_admins; + + void CreateAnswer(); +}; + +class ADD_ADMIN: public BASE_PARSER +{ + public: + ADD_ADMIN(const ADMIN & admin, ADMINS & admins) + : BASE_PARSER(admin, "AddAdmin"), m_admins(admins) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string admin; + ADMINS & m_admins; + + void CreateAnswer(); +}; + +class DEL_ADMIN: public BASE_PARSER +{ + public: + DEL_ADMIN(const ADMIN & admin, ADMINS & admins) + : BASE_PARSER(admin, "DelAdmin"), m_admins(admins) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string admin; + ADMINS & m_admins; + + void CreateAnswer(); +}; + +class CHG_ADMIN: public BASE_PARSER +{ + public: + CHG_ADMIN(const ADMIN & admin, ADMINS & admins) + : BASE_PARSER(admin, "ChgAdmin"), m_admins(admins) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string login; + RESETABLE password; + RESETABLE privAsString; + ADMINS & m_admins; + + void CreateAnswer(); +}; + +} +} + +#endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.cpp index 21765e11..e16745c5 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.cpp @@ -1,47 +1,54 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + #include "parser_auth_by.h" #include "stg/users.h" #include "stg/user.h" -int PARSER_AUTH_BY::ParseStart(void * /*data*/, const char *el, const char **attr) +using STG::PARSER::AUTH_BY; + +int AUTH_BY::Start(void * /*data*/, const char *el, const char **attr) { -if (strcasecmp(el, "GetUserAuthBy") == 0) - { - if (attr[0] && attr[1]) - login = attr[1]; - else - { - login.erase(login.begin(), login.end()); + if (strcasecmp(el, tag.c_str()) != 0) return -1; - } - return 0; - } -return -1; -} -int PARSER_AUTH_BY::ParseEnd(void * /*data*/, const char *el) -{ -if (strcasecmp(el, "GetUserAuthBy") == 0) - { - CreateAnswer(); + if (!attr[1]) + return -1; + + m_login = attr[1]; return 0; - } -return -1; } -void PARSER_AUTH_BY::CreateAnswer() +void AUTH_BY::CreateAnswer() { -USER_PTR u; -if (users->FindByName(login, &u)) + CONST_USER_PTR u; + if (m_users.FindByName(m_login, &u)) { - answer = ""; - return; + answer = ""; + return; } -answer.clear(); -answer += ""; -std::vector list(u->GetAuthorizers()); -for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) - answer += ""; -answer += ""; + answer = ""; + std::vector list(u->GetAuthorizers()); + for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + answer += ""; + answer += ""; } diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h index c34586c9..c103147e 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h @@ -1,18 +1,53 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + #ifndef __STG_PARSER_AUTH_BY_H__ #define __STG_PARSER_AUTH_BY_H__ +#include "parser.h" + #include -#include "parser.h" +class ADMIN; +class USERS; + +namespace STG +{ +namespace PARSER +{ -class PARSER_AUTH_BY : public BASE_PARSER { -public: - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - void CreateAnswer(); +class AUTH_BY : public BASE_PARSER +{ + public: + AUTH_BY(const ADMIN & admin, const USERS & users) + : BASE_PARSER(admin, "GetUserAuthBy"), m_users(users) {} + int Start(void * data, const char * el, const char ** attr); -private: - std::string login; + private: + const USERS & m_users; + std::string m_login; + + void CreateAnswer(); }; +} +} + #endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp new file mode 100644 index 00000000..18767320 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp @@ -0,0 +1,138 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_message.h" + +#include "stg/users.h" +#include "stg/common.h" + +extern volatile time_t stgTime; // So sad... + +using STG::PARSER::SEND_MESSAGE; + +int SEND_MESSAGE::Start(void *, const char *el, const char **attr) +{ + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + for (size_t i = 0; i < 14; i++) + if (attr[i] == NULL) + { + m_result = res_params_error; + CreateAnswer(); + printfd(__FILE__, "To few parameters\n"); + return 0; + } + + for (size_t i = 0; i < 14; i += 2) + { + if (strcasecmp(attr[i], "login") == 0) + ParseLogins(attr[i + 1]); + + if (strcasecmp(attr[i], "MsgVer") == 0) + { + str2x(attr[i + 1], m_msg.header.ver); + if (m_msg.header.ver != 1) + m_result = res_params_error; + } + + if (strcasecmp(attr[i], "MsgType") == 0) + { + str2x(attr[i + 1], m_msg.header.type); + if (m_msg.header.type != 1) + m_result = res_params_error; + } + + if (strcasecmp(attr[i], "Repeat") == 0) + { + str2x(attr[i + 1], m_msg.header.repeat); + if (m_msg.header.repeat < 0) + m_result = res_params_error; + } + + if (strcasecmp(attr[i], "RepeatPeriod") == 0) + str2x(attr[i + 1], m_msg.header.repeatPeriod); + + if (strcasecmp(attr[i], "ShowTime") == 0) + str2x(attr[i + 1], m_msg.header.showTime); + + if (strcasecmp(attr[i], "Text") == 0) + { + Decode21str(m_msg.text, attr[i + 1]); + m_result = res_ok; + } + } + return 0; +} + +int SEND_MESSAGE::End(void *, const char *el) +{ + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + m_result = res_unknown; + for (unsigned i = 0; i < m_logins.size(); i++) + { + if (m_users.FindByName(m_logins[i], &m_user)) + { + printfd(__FILE__, "User not found. %s\n", m_logins[i].c_str()); + continue; + } + m_msg.header.creationTime = static_cast(stgTime); + m_user->AddMessage(&m_msg); + m_result = res_ok; + } + CreateAnswer(); + return 0; +} + +int SEND_MESSAGE::ParseLogins(const char * login) +{ + char * p; + char * l = new char[strlen(login) + 1]; + strcpy(l, login); + p = strtok(l, ":"); + m_logins.clear(); + while (p) + { + m_logins.push_back(p); + p = strtok(NULL, ":"); + } + + delete[] l; + return 0; +} + +void SEND_MESSAGE::CreateAnswer() +{ + switch (m_result) + { + case res_ok: + answer = ""; + break; + case res_params_error: + answer = ""; + break; + case res_unknown: + answer = ""; + break; + } +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_message.h b/projects/stargazer/plugins/configuration/sgconfig/parser_message.h new file mode 100644 index 00000000..e7c12f4f --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_message.h @@ -0,0 +1,62 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_USERS_H__ +#define __STG_SGCONFIG_PARSER_USERS_H__ + +#include "parser.h" + +#include "stg/message.h" + +#include +#include + +class USERS; +class USER; + +namespace STG +{ +namespace PARSER +{ + +class SEND_MESSAGE: public BASE_PARSER +{ + public: + SEND_MESSAGE(const ADMIN & admin, USERS & users) + : BASE_PARSER(admin, "Message"), m_users(users), m_result(res_ok), m_user(NULL) {} + int Start(void *data, const char *el, const char **attr); + int End(void *data, const char *el); + + private: + USERS & m_users; + std::vector m_logins; + enum { res_ok, res_params_error, res_unknown } m_result; + STG_MSG m_msg; + USER * m_user; + + int ParseLogins(const char * logins); + void CreateAnswer(); +}; + +} +} + +#endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp new file mode 100644 index 00000000..1287bf51 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp @@ -0,0 +1,57 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_server_info.h" + +#include "stg/common.h" +#include "stg/version.h" +#include "stg/const.h" + +#include +#include + +#include + +using STG::PARSER::GET_SERVER_INFO; + +void GET_SERVER_INFO::CreateAnswer() +{ + struct utsname utsn; + uname(&utsn); + + std::string name = std::string(utsn.sysname) + " " + + utsn.release + " " + + utsn.machine + " " + + utsn.nodename; + + answer = GetOpenTag() + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + for (size_t i = 0; i< DIR_NUM; i++) + answer += ""; + + answer += GetCloseTag(); +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h new file mode 100644 index 00000000..2aabf473 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h @@ -0,0 +1,57 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_SERVER_INFO_H__ +#define __STG_SGCONFIG_PARSER_SERVER_INFO_H__ + +#include "parser.h" + +class ADMIN; + +namespace STG +{ +namespace PARSER +{ + +class GET_SERVER_INFO: public BASE_PARSER { + public: + GET_SERVER_INFO(const ADMIN & admin, + const SETTINGS & settings, + const USERS & users, + const TARIFFS & tariffs) + : BASE_PARSER(admin, "GetServerInfo"), + m_settings(settings), + m_users(users), + m_tariffs(tariffs) + {} + + private: + const SETTINGS & m_settings; + const USERS & m_users; + const TARIFFS & m_tariffs; + + void CreateAnswer(); +}; + +} +} + +#endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp deleted file mode 100644 index e083f852..00000000 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp +++ /dev/null @@ -1,483 +0,0 @@ -#include "parser.h" - -#include "stg/tariffs.h" -#include "stg/users.h" -#include "stg/common.h" - -#include // snprintf -#include - -const int pt_mega = 1024 * 1024; -//----------------------------------------------------------------------------- -// GET TARIFFS -//----------------------------------------------------------------------------- -int PARSER_GET_TARIFFS::ParseStart(void *, const char * el, const char **) -{ -if (strcasecmp(el, "GetTariffs") == 0) - { - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_GET_TARIFFS::ParseEnd(void *, const char * el) -{ -if (strcasecmp(el, "GetTariffs") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_GET_TARIFFS::CreateAnswer() -{ -answer = ""; - -std::list dataList; -tariffs->GetTariffsData(&dataList); -std::list::const_iterator it = dataList.begin(); -for (; it != dataList.end(); ++it) - { - answer += "tariffConf.name + "\">"; - - for (size_t i = 0; i < DIR_NUM; i++) - answer += "dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" + - x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>"; - - answer += "dirPrice[i].priceDayA * pt_mega); - } - answer += "\"/>"; - - answer += "dirPrice[i].priceDayB * pt_mega); - } - answer += "\"/>"; - - answer += "dirPrice[i].priceNightA * pt_mega); - } - answer += "\"/>"; - - answer += "dirPrice[i].priceNightB * pt_mega); - } - answer += "\"/>"; - - answer += "dirPrice[i].threshold); - } - answer += "\"/>"; - - answer += "dirPrice[i].singlePrice ? "1" : "0"); - } - answer += "\"/>"; - - answer += "dirPrice[i].noDiscount ? "1" : "0"); - } - answer += "\"/>"; - - answer += "tariffConf.fee) + "\"/>"; - - answer += "tariffConf.passiveCost) + "\"/>"; - - answer += "tariffConf.free) + "\"/>"; - - switch (it->tariffConf.traffType) - { - case TRAFF_UP: - answer += ""; - break; - case TRAFF_DOWN: - answer += ""; - break; - case TRAFF_UP_DOWN: - answer += ""; - break; - case TRAFF_MAX: - answer += ""; - break; - } - - answer += "tariffConf.period) + "\"/>"; - - answer += ""; - } -answer += ""; -} -//----------------------------------------------------------------------------- -// ADD TARIFF -//----------------------------------------------------------------------------- -int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr) -{ -if (strcasecmp(el, "AddTariff") == 0) - { - if (attr[1]) - { - tariffToAdd = attr[1]; - } - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el) -{ -if (strcasecmp(el, "AddTariff") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_ADD_TARIFF::CreateAnswer() -{ -if (tariffs->Add(tariffToAdd, currAdmin) == 0) - answer = ""; -else - answer = "GetStrError() + "\"/>"; -} -//----------------------------------------------------------------------------- -// DEL TARIFF -//----------------------------------------------------------------------------- -int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr) -{ -strError = ""; -if (strcasecmp(el, "DelTariff") == 0) - { - tariffToDel = attr[1]; - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el) -{ -if (strcasecmp(el, "DelTariff") == 0) - { - CreateAnswer(); - return 0; - } -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_DEL_TARIFF::CreateAnswer() -{ -if (users->TariffInUse(tariffToDel)) - answer = ""; -else if (tariffs->Del(tariffToDel, currAdmin) == 0) - answer = ""; -else - answer = "GetStrError() + "\"/>"; -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -// CHG TARIFF -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params) -{ -char * str = new char[s.size() + 1]; -char * p; -strcpy(str, s.c_str()); -p = strtok(str, "/"); - -for (int i = 0; i < paramsNum; i++) - { - if (p == NULL) - { - delete[] str; - return -1; - } - - if (str2x(p, params[i]) != 0) - { - delete[] str; - return -1; - } - - p = strtok(NULL, "/"); - } - -delete[] str; -return 0; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params) -{ -char * str = new char[s.size() + 1]; -char * p; -strcpy(str, s.c_str()); -p = strtok(str, "/"); - -for (int i = 0; i < paramsNum; i++) - { - if (p == NULL) - { - delete[] str; - return -1; - } - - if (strtodouble2(p, params[i]) != 0) - { - delete[] str; - return -1; - } - - p = strtok(NULL, "/"); - } - -delete[] str; -return 0; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr) -{ -double price[DIR_NUM]; -int t[DIR_NUM]; -depth++; - -if (depth == 1) - { - if (strcasecmp(el, "SetTariff") == 0) - { - td.tariffConf.name = attr[1]; - return 0; - } - } -else - { - std::string s; - - if (strcasecmp(el, "PriceDayA") == 0) - { - s = attr[1]; - if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].priceDayA = price[j] / pt_mega; - return 0; - } - - if (strcasecmp(el, "PriceDayB") == 0) - { - s = attr[1]; - if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].priceDayB = price[j] / pt_mega; - return 0; - } - - - if (strcasecmp(el, "PriceNightA") == 0) - { - s = attr[1]; - if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].priceNightA = price[j] / pt_mega; - return 0; - } - - if (strcasecmp(el, "PriceNightB") == 0) - { - s = attr[1]; - if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].priceNightB = price[j] / pt_mega; - return 0; - } - - if (strcasecmp(el, "Threshold") == 0) - { - s = attr[1]; - if (ParseSlashedIntParams(DIR_NUM, s, t) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].threshold = t[j]; - return 0; - } - - if (strcasecmp(el, "SinglePrice") == 0) - { - s = attr[1]; - if (ParseSlashedIntParams(DIR_NUM, s, t) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].singlePrice = t[j]; - return 0; - } - - if (strcasecmp(el, "NoDiscount") == 0) - { - s = attr[1]; - if (ParseSlashedIntParams(DIR_NUM, s, t) == 0) - for (int j = 0; j < DIR_NUM; j++) - td.dirPrice[j].noDiscount = t[j]; - return 0; - } - - for (int j = 0; j < DIR_NUM; j++) - { - char st[50]; - snprintf(st, 50, "Time%d", j); - if (strcasecmp(el, st) == 0) - { - int h1 = 0; - int m1 = 0; - int h2 = 0; - int m2 = 0; - if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0) - { - td.dirPrice[j].hDay = h1; - td.dirPrice[j].mDay = m1; - td.dirPrice[j].hNight = h2; - td.dirPrice[j].mNight = m2; - } - return 0; - } - } - - if (strcasecmp(el, "Fee") == 0) - { - double fee; - if (strtodouble2(attr[1], fee) == 0) - td.tariffConf.fee = fee; - return 0; - } - - if (strcasecmp(el, "PassiveCost") == 0) - { - double pc; - if (strtodouble2(attr[1], pc) == 0) - td.tariffConf.passiveCost = pc; - return 0; - } - if (strcasecmp(el, "Free") == 0) - { - double free; - if (strtodouble2(attr[1], free) == 0) - td.tariffConf.free = free; - return 0; - } - - if (strcasecmp(el, "TraffType") == 0) - { - if (strcasecmp(attr[1], "up") == 0) - { - td.tariffConf.traffType = TRAFF_UP; - return 0; - } - - if (strcasecmp(attr[1], "down") == 0) - { - td.tariffConf.traffType = TRAFF_DOWN; - return 0; - } - if (strcasecmp(attr[1], "up+down") == 0) - { - td.tariffConf.traffType = TRAFF_UP_DOWN; - return 0; - } - if (strcasecmp(attr[1], "max") == 0) - { - td.tariffConf.traffType = TRAFF_MAX; - return 0; - } - return 0; - } - - if (strcasecmp(el, "Period") == 0) - { - td.tariffConf.period = TARIFF::StringToPeriod(attr[1]); - return 0; - } - } -return -1; -} -//----------------------------------------------------------------------------- -int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el) -{ -if (depth == 1) - { - if (strcasecmp(el, "SetTariff") == 0) - { - CreateAnswer(); - depth--; - return 0; - } - } - -depth--; -return -1; -} -//----------------------------------------------------------------------------- -void PARSER_CHG_TARIFF::CreateAnswer() -{ -if (!td.tariffConf.name.data().empty()) - { - TARIFF_DATA tariffData = td.GetData(); - if (tariffs->Chg(tariffData, currAdmin) == 0) - { - answer = ""; - return; - } - else - { - answer = "GetStrError() + "\"/>"; - return; - } - } -answer = ""; -} -//----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp new file mode 100644 index 00000000..8d54b1d5 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp @@ -0,0 +1,308 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_tariffs.h" + +#include "stg/tariffs.h" +#include "stg/users.h" +#include "stg/common.h" +#include "stg/resetable.h" + +#include // snprintf +#include + +using STG::PARSER::GET_TARIFFS; +using STG::PARSER::ADD_TARIFF; +using STG::PARSER::DEL_TARIFF; +using STG::PARSER::CHG_TARIFF; + +namespace +{ + +const double pt_mega = 1024 * 1024; + +template +std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier) +{ + std::string res; + for (size_t i = 0; i < size; ++i) + { + if (!res.empty()) + res += "/"; + res += x2str((array[i].*field) * multiplier); + } + return res; +} + +template +bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE C::* field, F divisor) +{ + size_t index = 0; + std::string::size_type from = 0; + std::string::size_type pos = 0; + while (index < size && (pos = source.find('/', from)) != std::string::npos) + { + if (str2x(source.substr(from, pos - from), (array[index].*field).data())) + return false; + (array[index].*field).data() /= divisor; + from = pos + 1; + ++index; + } + if (str2x(source.substr(from), (array[index].*field).data())) + return false; + (array[index].*field).data() /= divisor; + return true; +} + +} + +void GET_TARIFFS::CreateAnswer() +{ + answer = GetOpenTag(); + + std::list dataList; + m_tariffs.GetTariffsData(&dataList); + std::list::const_iterator it = dataList.begin(); + for (; it != dataList.end(); ++it) + { + answer += "tariffConf.name + "\">"; + + for (size_t i = 0; i < DIR_NUM; i++) + answer += "dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" + + x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>"; + + answer += "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" + + "dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" + + "tariffConf.fee) + "\"/>" + + "tariffConf.passiveCost) + "\"/>" + + "tariffConf.free) + "\"/>" + + "tariffConf.traffType) + "\"/>" + + "tariffConf.period) + "\"/>" + + ""; + } + + answer += GetCloseTag(); +} + +int ADD_TARIFF::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + if (attr[1] == NULL) + return -1; + + tariff = attr[1]; + return 0; +} + +void ADD_TARIFF::CreateAnswer() +{ + if (m_tariffs.Add(tariff, &currAdmin) == 0) + answer = "<" + tag + " Result=\"Ok\"/>"; + else + answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; +} + +int DEL_TARIFF::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + if (attr[1] == NULL) + return -1; + + tariff = attr[1]; + return 0; +} + +void DEL_TARIFF::CreateAnswer() +{ + if (m_users.TariffInUse(tariff)) + answer = "<" + tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>"; + else if (m_tariffs.Del(tariff, &currAdmin) == 0) + answer = "<" + tag + " Result=\"Ok\"/>"; + else + answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; +} + +int CHG_TARIFF::Start(void *, const char * el, const char ** attr) +{ + depth++; + + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) == 0) + { + td.tariffConf.name = attr[1]; + return 0; + } + } + else + { + if (strcasecmp(el, "PriceDayA") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "PriceDayB") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "PriceNightA") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "PriceNightB") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "Threshold") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "SinglePrice") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1)) + return -1; // TODO: log it + else + return 0; + } + + if (strcasecmp(el, "NoDiscount") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1)) + return -1; // TODO: log it + else + return 0; + } + + for (int j = 0; j < DIR_NUM; j++) + { + char st[50]; + snprintf(st, 50, "Time%d", j); + if (strcasecmp(el, st) == 0) + { + int h1 = 0; + int m1 = 0; + int h2 = 0; + int m2 = 0; + if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0) + { + td.dirPrice[j].hDay = h1; + td.dirPrice[j].mDay = m1; + td.dirPrice[j].hNight = h2; + td.dirPrice[j].mNight = m2; + } + return 0; + } + } + + if (strcasecmp(el, "Fee") == 0) + { + double fee; + if (strtodouble2(attr[1], fee) == 0) + td.tariffConf.fee = fee; + return 0; + } + + if (strcasecmp(el, "PassiveCost") == 0) + { + double pc; + if (strtodouble2(attr[1], pc) == 0) + td.tariffConf.passiveCost = pc; + return 0; + } + + if (strcasecmp(el, "Free") == 0) + { + double free; + if (strtodouble2(attr[1], free) == 0) + td.tariffConf.free = free; + return 0; + } + + if (strcasecmp(el, "TraffType") == 0) + { + td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]); + return 0; + } + + if (strcasecmp(el, "Period") == 0) + { + td.tariffConf.period = TARIFF::StringToPeriod(attr[1]); + return 0; + } + } + return -1; +} + +int CHG_TARIFF::End(void *, const char * el) +{ + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + CreateAnswer(); + } + + --depth; + return 0; +} + +void CHG_TARIFF::CreateAnswer() +{ + if (!td.tariffConf.name.data().empty()) + { + TARIFF_DATA tariffData = td.GetData(); + if (m_tariffs.Chg(tariffData, &currAdmin) == 0) + answer = "<" + tag + " Result=\"ok\"/>"; + else + answer = "<" + tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>"; + } + else + answer = "<" + tag + " Result=\"Change tariff error!\"/>"; +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h new file mode 100644 index 00000000..c96163fe --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h @@ -0,0 +1,100 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_TARIFFS_H__ +#define __STG_SGCONFIG_PARSER_TARIFFS_H__ + +#include "parser.h" + +#include "stg/tariff_conf.h" + +#include + +class TARIFFS; +class USERS; +class ADMIN; + +namespace STG +{ +namespace PARSER +{ + +class GET_TARIFFS: public BASE_PARSER +{ + public: + GET_TARIFFS(const ADMIN & admin, const TARIFFS & tariffs) + : BASE_PARSER(admin, "GetTariffs"), m_tariffs(tariffs) {} + + private: + const TARIFFS & m_tariffs; + + void CreateAnswer(); +}; + +class ADD_TARIFF: public BASE_PARSER +{ + public: + ADD_TARIFF(const ADMIN & admin, TARIFFS & tariffs) + : BASE_PARSER(admin, "AddTariff"), m_tariffs(tariffs) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string tariff; + TARIFFS & m_tariffs; + + void CreateAnswer(); +}; + +class DEL_TARIFF: public BASE_PARSER +{ + public: + DEL_TARIFF(const ADMIN & admin, const USERS & users, TARIFFS & tariffs) + : BASE_PARSER(admin, "DelTariff"), m_users(users), m_tariffs(tariffs) {} + int Start(void * data, const char * el, const char ** attr); + + private: + std::string tariff; + const USERS & m_users; + TARIFFS & m_tariffs; + + void CreateAnswer(); +}; + +class CHG_TARIFF: public BASE_PARSER +{ + public: + CHG_TARIFF(const ADMIN & admin, TARIFFS & tariffs) + : BASE_PARSER(admin, "SetTariff"), m_tariffs(tariffs) {} + int Start(void * data, const char * el, const char ** attr); + int End(void * data, const char * el); + + private: + TARIFF_DATA_RES td; + TARIFFS & m_tariffs; + + int CheckTariffData(); + void CreateAnswer(); +}; + +} +} + +#endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp index de28eaba..5a101570 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp @@ -26,43 +26,35 @@ #include // strcasecmp -int PARSER_USER_INFO::ParseStart(void * /*data*/, const char *el, const char **attr) -{ -login.clear(); -if (strcasecmp(el, "GetUserInfo") != 0) - return -1; - -if (!attr[0] || !attr[1] || strcasecmp(attr[0], "login") != 0) - return -1; +using STG::PARSER::USER_INFO; -login = attr[1]; -return 0; -} - -int PARSER_USER_INFO::ParseEnd(void * /*data*/, const char *el) +int USER_INFO::Start(void * /*data*/, const char *el, const char **attr) { -if (strcasecmp(el, "GetUserInfo") != 0) - return -1; + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + if (!attr[1]) + return -1; -CreateAnswer(); -return 0; + m_login = attr[1]; + return 0; } -void PARSER_USER_INFO::CreateAnswer() +void USER_INFO::CreateAnswer() { -CONST_USER_PTR u; -if (users->FindByName(login, &u)) + CONST_USER_PTR u; + if (m_users.FindByName(m_login, &u)) { - answer = ""; - return; + answer = ""; + return; } -answer = "GetAuthorizedModificationTime()) + "\"" + - " lastDisconnectTime=\"" + x2str(u->GetConnectedModificationTime()) + "\"" + - " connected=\"" + (u->GetConnected() ? "true" : "false") + "\"" + - " lastDisconnectReason=\"" + u->GetLastDisconnectReason() + "\">"; -std::vector list(u->GetAuthorizers()); -for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) - answer += ""; -answer += ""; + answer = "GetAuthorizedModificationTime()) + "\"" + + " lastDisconnectTime=\"" + x2str(u->GetConnectedModificationTime()) + "\"" + + " connected=\"" + (u->GetConnected() ? "true" : "false") + "\"" + + " lastDisconnectReason=\"" + u->GetLastDisconnectReason() + "\">"; + std::vector list(u->GetAuthorizers()); + for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + answer += ""; + answer += ""; } diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h b/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h index ccd1d94e..284b352c 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h @@ -25,15 +25,26 @@ #include -class PARSER_USER_INFO : public BASE_PARSER { -public: - int ParseStart(void *data, const char *el, const char **attr); - int ParseEnd(void *data, const char *el); - -private: - std::string login; - - void CreateAnswer(); +namespace STG +{ +namespace PARSER +{ + +class USER_INFO : public BASE_PARSER +{ + public: + USER_INFO(const ADMIN & admin, const USERS & users) + : BASE_PARSER(admin, "GetUserInfo"), m_users(users) {} + int Start(void * data, const char * el, const char ** attr); + + private: + const USERS & m_users; + std::string m_login; + + void CreateAnswer(); }; +} +} + #endif diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp new file mode 100644 index 00000000..16054a9c --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp @@ -0,0 +1,814 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_users.h" + +#include "stg/users.h" +#include "stg/tariffs.h" +#include "stg/user_property.h" +#include "stg/user_conf.h" +#include "stg/user_stat.h" +#include "stg/common.h" + +#include + +using STG::PARSER::GET_USERS; +using STG::PARSER::GET_USER; +using STG::PARSER::ADD_USER; +using STG::PARSER::DEL_USER; +using STG::PARSER::CHG_USER; +using STG::PARSER::CHECK_USER; + +namespace +{ + +std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0) +{ + std::string answer; + + if (loginInStart) + answer += ""; + else + answer += ""; + + answer += ""; + + if (user.GetProperty().password.ModificationTime() > lastTime) + { + if (showPass) + answer += ""; + else + answer += ""; + } + + if (user.GetProperty().cash.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().freeMb.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().credit.ModificationTime() > lastTime) + answer += ""; + + if (user.GetProperty().nextTariff.Get() != "") + { + if (user.GetProperty().tariffName.ModificationTime() > lastTime || + user.GetProperty().nextTariff.ModificationTime() > lastTime) + answer += ""; + } + else + { + if (user.GetProperty().tariffName.ModificationTime() > lastTime) + answer += ""; + } + + if (user.GetProperty().note.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().phone.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().address.ModificationTime() > lastTime) + answer += "
"; + if (user.GetProperty().email.ModificationTime() > lastTime) + answer += ""; + + std::vector *> userdata; + userdata.push_back(user.GetProperty().userdata0.GetPointer()); + userdata.push_back(user.GetProperty().userdata1.GetPointer()); + userdata.push_back(user.GetProperty().userdata2.GetPointer()); + userdata.push_back(user.GetProperty().userdata3.GetPointer()); + userdata.push_back(user.GetProperty().userdata4.GetPointer()); + userdata.push_back(user.GetProperty().userdata5.GetPointer()); + userdata.push_back(user.GetProperty().userdata6.GetPointer()); + userdata.push_back(user.GetProperty().userdata7.GetPointer()); + userdata.push_back(user.GetProperty().userdata8.GetPointer()); + userdata.push_back(user.GetProperty().userdata9.GetPointer()); + + for (size_t i = 0; i < userdata.size(); i++) + if (userdata[i]->ModificationTime() > lastTime) + answer += "Get()) + "\" />"; + + if (user.GetProperty().realName.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().group.ModificationTime() > lastTime) + answer += ""; + if (user.GetConnectedModificationTime() > lastTime) + answer += std::string(""; + if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime) + answer += std::string(""; + if (user.GetCurrIPModificationTime() > lastTime) + answer += ""; + if (user.GetPingTime() > lastTime) + answer += ""; + if (user.GetProperty().ips.ModificationTime() > lastTime) + answer += ""; + + answer += " lastTime) + for (size_t j = 0; j < DIR_NUM; j++) + answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\""; + if (user.GetProperty().down.ModificationTime() > lastTime) + for (size_t j = 0; j < DIR_NUM; j++) + answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\""; + if (user.GetSessionUploadModificationTime() > lastTime) + for (size_t j = 0; j < DIR_NUM; j++) + answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\""; + if (user.GetSessionDownloadModificationTime() > lastTime) + for (size_t j = 0; j < DIR_NUM; j++) + answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\""; + answer += "/>"; + + if (user.GetProperty().disabled.ModificationTime() > lastTime) + answer += std::string(""; + if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime) + answer += std::string(""; + if (user.GetProperty().passive.ModificationTime() > lastTime) + answer += std::string(""; + if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime) + answer += ""; + if (user.GetProperty().creditExpire.ModificationTime() > lastTime) + answer += ""; + + if (lastTime == 0) + { + answer += ""; + std::vector list(user.GetAuthorizers()); + for (std::vector::const_iterator it = list.begin(); it != list.end(); ++it) + answer += ""; + answer += ""; + } + + answer += ""; + + return answer; +} + +} // namespace anonymous + +int GET_USER::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + if (attr[1] == NULL) + return -1; + + login = attr[1]; + return 0; +} + +void GET_USER::CreateAnswer() +{ + CONST_USER_PTR u; + + if (m_users.FindByName(login, &u)) + answer = "<" + tag + " result=\"error\" reason=\"User not found.\"/>"; + else + answer = UserToXML(*u, false, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd); +} + +int GET_USERS::Start(void *, const char * el, const char ** attr) +{ + lastUpdateFound = false; + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + while (attr && *attr && *(attr + 1)) + { + if (strcasecmp(*attr, "LastUpdate") == 0) + { + if (str2x(*(attr + 1), lastUserUpdateTime) == 0) + lastUpdateFound = true; + } + ++attr; + } + + return 0; +} + +void GET_USERS::CreateAnswer() +{ + int h = m_users.OpenSearch(); + if (!h) + { + printfd(__FILE__, "m_users.OpenSearch() error\n"); + return; + } + + if (lastUpdateFound) + answer = "<" + tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">"; + else + answer = GetOpenTag(); + + USER_PTR u; + + while (m_users.SearchNext(h, &u) == 0) + answer += UserToXML(*u, true, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd, lastUserUpdateTime); + + m_users.CloseSearch(h); + + answer += GetCloseTag(); +} + +int ADD_USER::Start(void *, const char * el, const char ** attr) +{ + depth++; + + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) == 0) + return 0; + } + else + { + if (strcasecmp(el, "login") == 0) + { + login = attr[1]; + return 0; + } + } + return -1; +} + +int ADD_USER::End(void *, const char *el) +{ + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) == 0) + return -1; + CreateAnswer(); + } + + --depth; + return 0; +} + +void ADD_USER::CreateAnswer() +{ + if (CheckUserData() == 0) + answer = "<" + tag + " result=\"ok\"/>"; + else + answer = "<" + tag + " result=\"error\" reason=\"Access denied\"/>"; +} + +int ADD_USER::CheckUserData() +{ + USER_PTR u; + if (m_users.FindByName(login, &u)) + return m_users.Add(login, &currAdmin); + + return -1; +} + +CHG_USER::CHG_USER(const ADMIN & admin, USERS & users, const TARIFFS & tariffs) + : BASE_PARSER(admin, "SetUser"), + m_users(users), + m_tariffs(tariffs), + usr(new USER_STAT_RES), + ucr(new USER_CONF_RES), + upr(new RESETABLE[DIR_NUM]), + downr(new RESETABLE[DIR_NUM]), + cashMustBeAdded(false), + res(0) +{ +} + +CHG_USER::~CHG_USER() +{ + delete usr; + delete ucr; + delete[] upr; + delete[] downr; +} + +int CHG_USER::Start(void *, const char * el, const char ** attr) +{ + depth++; + + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) == 0) + return 0; + } + else + { + if (strcasecmp(el, "login") == 0) + { + login = attr[1]; + return 0; + } + + if (strcasecmp(el, "ip") == 0) + { + try + { + ucr->ips = StrToIPS(attr[1]); + } + catch (...) + { + printfd(__FILE__, "StrToIPS Error!\n"); + } + } + + if (strcasecmp(el, "password") == 0) + { + ucr->password = attr[1]; + return 0; + } + + if (strcasecmp(el, "address") == 0) + { + ucr->address = Decode21str(attr[1]); + return 0; + } + + if (strcasecmp(el, "aonline") == 0) + { + ucr->alwaysOnline = (*(attr[1]) != '0'); + return 0; + } + + if (strcasecmp(el, "cash") == 0) + { + if (attr[2] && (strcasecmp(attr[2], "msg") == 0)) + cashMsg = Decode21str(attr[3]); + + double cash; + if (strtodouble2(attr[1], cash) == 0) + usr->cash = cash; + + if (strcasecmp(attr[0], "set") == 0) + cashMustBeAdded = false; + + if (strcasecmp(attr[0], "add") == 0) + cashMustBeAdded = true; + + return 0; + } + + if (strcasecmp(el, "CreditExpire") == 0) + { + long int creditExpire = 0; + if (str2x(attr[1], creditExpire) == 0) + ucr->creditExpire = (time_t)creditExpire; + + return 0; + } + + if (strcasecmp(el, "credit") == 0) + { + double credit; + if (strtodouble2(attr[1], credit) == 0) + ucr->credit = credit; + return 0; + } + + if (strcasecmp(el, "freemb") == 0) + { + double freeMb; + if (strtodouble2(attr[1], freeMb) == 0) + usr->freeMb = freeMb; + return 0; + } + + if (strcasecmp(el, "down") == 0) + { + int down = 0; + if (str2x(attr[1], down) == 0) + ucr->disabled = down; + return 0; + } + + if (strcasecmp(el, "DisableDetailStat") == 0) + { + int disabledDetailStat = 0; + if (str2x(attr[1], disabledDetailStat) == 0) + ucr->disabledDetailStat = disabledDetailStat; + return 0; + } + + if (strcasecmp(el, "email") == 0) + { + ucr->email = Decode21str(attr[1]); + return 0; + } + + for (int i = 0; i < USERDATA_NUM; i++) + { + char name[15]; + sprintf(name, "userdata%d", i); + if (strcasecmp(el, name) == 0) + { + ucr->userdata[i] = Decode21str(attr[1]); + return 0; + } + } + + if (strcasecmp(el, "group") == 0) + { + ucr->group = Decode21str(attr[1]); + return 0; + } + + if (strcasecmp(el, "note") == 0) + { + ucr->note = Decode21str(attr[1]); + return 0; + } + + if (strcasecmp(el, "passive") == 0) + { + int passive = 0; + if (str2x(attr[1], passive) == 0) + ucr->passive = passive; + return 0; + } + + if (strcasecmp(el, "phone") == 0) + { + ucr->phone = Decode21str(attr[1]); + return 0; + } + + if (strcasecmp(el, "Name") == 0) + { + ucr->realName = Decode21str(attr[1]); + return 0; + } + + if (strcasecmp(el, "traff") == 0) + { + int j = 0; + while (attr[j]) + { + int dir = attr[j][2] - '0'; + + if (strncasecmp(attr[j], "md", 2) == 0) + { + uint64_t t = 0; + str2x(attr[j + 1], t); + downr[dir] = t; + } + if (strncasecmp(attr[j], "mu", 2) == 0) + { + uint64_t t = 0; + str2x(attr[j + 1], t); + upr[dir] = t; + } + j += 2; + } + return 0; + } + + if (strcasecmp(el, "tariff") == 0) + { + if (strcasecmp(attr[0], "now") == 0) + ucr->tariffName = attr[1]; + + if (strcasecmp(attr[0], "delayed") == 0) + ucr->nextTariff = attr[1]; + + return 0; + } + } + return -1; +} + +int CHG_USER::End(void *, const char *el) +{ + if (depth == 1) + { + if (strcasecmp(el, tag.c_str()) != 0) + return -1; + + ApplyChanges(); + CreateAnswer(); + } + + depth--; + return 0; +} + +void CHG_USER::CreateAnswer() +{ + if (res == 0) + answer = "<" + tag + " result=\"ok\"/>"; + else + answer = "<" + tag + " result=\"error\"/>"; +} + +int CHG_USER::ApplyChanges() +{ + printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n"); + USER_PTR u; + + res = 0; + if (m_users.FindByName(login, &u)) + { + res = -1; + return -1; + } + + bool check = false; + bool alwaysOnline = u->GetProperty().alwaysOnline; + if (!ucr->alwaysOnline.empty()) + { + check = true; + alwaysOnline = ucr->alwaysOnline.const_data(); + } + bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP(); + if (!ucr->ips.empty()) + { + check = true; + onlyOneIP = ucr->ips.const_data().OnlyOneIP(); + } + + if (check && alwaysOnline && !onlyOneIP) + { + printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n"); + GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin.GetLogStr().c_str()); + res = -1; + return -1; + } + + for (size_t i = 0; i < ucr->ips.const_data().Count(); ++i) + { + CONST_USER_PTR user; + uint32_t ip = ucr->ips.const_data().operator[](i).ip; + if (m_users.IsIPInUse(ip, login, &user)) + { + printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str()); + GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str()); + res = -1; + return -1; + } + } + + if (!ucr->ips.empty()) + if (!u->GetProperty().ips.Set(ucr->ips.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->alwaysOnline.empty()) + if (!u->GetProperty().alwaysOnline.Set(ucr->alwaysOnline.const_data(), + &currAdmin, login, store)) + res = -1; + + if (!ucr->address.empty()) + if (!u->GetProperty().address.Set(ucr->address.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->creditExpire.empty()) + if (!u->GetProperty().creditExpire.Set(ucr->creditExpire.const_data(), + &currAdmin, login, store)) + res = -1; + + if (!ucr->credit.empty()) + if (!u->GetProperty().credit.Set(ucr->credit.const_data(), &currAdmin, login, store)) + res = -1; + + if (!usr->freeMb.empty()) + if (!u->GetProperty().freeMb.Set(usr->freeMb.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->disabled.empty()) + if (!u->GetProperty().disabled.Set(ucr->disabled.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->disabledDetailStat.empty()) + if (!u->GetProperty().disabledDetailStat.Set(ucr->disabledDetailStat.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->email.empty()) + if (!u->GetProperty().email.Set(ucr->email.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->group.empty()) + if (!u->GetProperty().group.Set(ucr->group.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->note.empty()) + if (!u->GetProperty().note.Set(ucr->note.const_data(), &currAdmin, login, store)) + res = -1; + + std::vector *> userdata; + userdata.push_back(u->GetProperty().userdata0.GetPointer()); + userdata.push_back(u->GetProperty().userdata1.GetPointer()); + userdata.push_back(u->GetProperty().userdata2.GetPointer()); + userdata.push_back(u->GetProperty().userdata3.GetPointer()); + userdata.push_back(u->GetProperty().userdata4.GetPointer()); + userdata.push_back(u->GetProperty().userdata5.GetPointer()); + userdata.push_back(u->GetProperty().userdata6.GetPointer()); + userdata.push_back(u->GetProperty().userdata7.GetPointer()); + userdata.push_back(u->GetProperty().userdata8.GetPointer()); + userdata.push_back(u->GetProperty().userdata9.GetPointer()); + + for (int i = 0; i < (int)userdata.size(); i++) + if (!ucr->userdata[i].empty()) + if(!userdata[i]->Set(ucr->userdata[i].const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->passive.empty()) + if (!u->GetProperty().passive.Set(ucr->passive.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->password.empty()) + if (!u->GetProperty().password.Set(ucr->password.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->phone.empty()) + if (!u->GetProperty().phone.Set(ucr->phone.const_data(), &currAdmin, login, store)) + res = -1; + + if (!ucr->realName.empty()) + if (!u->GetProperty().realName.Set(ucr->realName.const_data(), &currAdmin, login, store)) + res = -1; + + + if (!usr->cash.empty()) + if (cashMustBeAdded) + { + if (!u->GetProperty().cash.Set(usr->cash.const_data() + u->GetProperty().cash, + &currAdmin, + login, + store, + cashMsg)) + res = -1; + else + if (!u->GetProperty().cash.Set(usr->cash.const_data(), &currAdmin, login, store, cashMsg)) + res = -1; + } + + + if (!ucr->tariffName.empty()) + { + if (m_tariffs.FindByName(ucr->tariffName.const_data())) + { + if (!u->GetProperty().tariffName.Set(ucr->tariffName.const_data(), &currAdmin, login, store)) + res = -1; + u->ResetNextTariff(); + } + else + { + //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str()); + res = -1; + } + } + + if (!ucr->nextTariff.empty()) + { + if (m_tariffs.FindByName(ucr->nextTariff.const_data())) + { + if (!u->GetProperty().nextTariff.Set(ucr->nextTariff.const_data(), &currAdmin, login, store)) + res = -1; + } + else + { + //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str()); + res = -1; + } + } + + DIR_TRAFF up = u->GetProperty().up; + DIR_TRAFF down = u->GetProperty().down; + int upCount = 0; + int downCount = 0; + for (int i = 0; i < DIR_NUM; i++) + { + if (!upr[i].empty()) + { + up[i] = upr[i].data(); + upCount++; + } + if (!downr[i].empty()) + { + down[i] = downr[i].data(); + downCount++; + } + } + + if (upCount) + if (!u->GetProperty().up.Set(up, &currAdmin, login, store)) + res = -1; + + if (downCount) + if (!u->GetProperty().down.Set(down, &currAdmin, login, store)) + res = -1; + + u->WriteConf(); + u->WriteStat(); + + return 0; +} + +int DEL_USER::Start(void *, const char *el, const char **attr) +{ + res = 0; + if (strcasecmp(el, tag.c_str()) == 0) + { + if (attr[0] == NULL || attr[1] == NULL) + { + //CreateAnswer("Parameters error!"); + CreateAnswer(); + return 0; + } + + if (m_users.FindByName(attr[1], &u)) + { + res = 1; + CreateAnswer(); + return 0; + } + CreateAnswer(); + return 0; + } + return -1; +} + +int DEL_USER::End(void *, const char *el) +{ + if (strcasecmp(el, tag.c_str()) == 0) + { + if (!res) + m_users.Del(u->GetLogin(), &currAdmin); + + return 0; + } + return -1; +} + +void DEL_USER::CreateAnswer() +{ + if (res) + answer = "<" + tag + " value=\"error\" reason=\"User not found\"/>"; + else + answer = "<" + tag + " value=\"ok\"/>"; +} + +int CHECK_USER::Start(void *, const char *el, const char **attr) +{ + if (strcasecmp(el, tag.c_str()) == 0) + { + if (attr[0] == NULL || attr[1] == NULL || + attr[2] == NULL || attr[3] == NULL) + { + CreateAnswer("Invalid parameters."); + printfd(__FILE__, "PARSER_CHECK_USER - attr err\n"); + return 0; + } + + CONST_USER_PTR user; + if (m_users.FindByName(attr[1], &user)) + { + CreateAnswer("User not found."); + printfd(__FILE__, "PARSER_CHECK_USER - login err\n"); + return 0; + } + + if (strcmp(user->GetProperty().password.Get().c_str(), attr[3])) + { + CreateAnswer("Wrong password."); + printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n"); + return 0; + } + + CreateAnswer(NULL); + return 0; + } + return -1; +} + +int CHECK_USER::End(void *, const char *el) +{ + if (strcasecmp(el, tag.c_str()) == 0) + return 0; + return -1; +} + +void CHECK_USER::CreateAnswer(const char * error) +{ + if (error) + answer = "<" + tag + " value=\"Err\" reason=\"" + error + "\"/>"; + else + answer = "<" + tag + " value=\"Ok\"/>"; +} diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_users.h b/projects/stargazer/plugins/configuration/sgconfig/parser_users.h new file mode 100644 index 00000000..da4583b1 --- /dev/null +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_users.h @@ -0,0 +1,144 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONFIG_PARSER_USERS_H__ +#define __STG_SGCONFIG_PARSER_USERS_H__ + +#include "parser.h" + +#include "stg/resetable.h" + +#include + +class USERS; +class USER; +class TARIFFS; +class ADMIN; + +namespace STG +{ +namespace PARSER +{ + +class GET_USERS: public BASE_PARSER +{ + public: + GET_USERS(const ADMIN & admin, USERS & users) + : BASE_PARSER(admin, "GetUsers"), m_users(users), + lastUserUpdateTime(0), lastUpdateFound(false) {} + int Start(void * data, const char * el, const char ** attr); + + private: + USERS & m_users; + time_t lastUserUpdateTime; + bool lastUpdateFound; + + void CreateAnswer(); +}; + +class GET_USER: public BASE_PARSER +{ + public: + GET_USER(const ADMIN & admin, const USERS & users) + : BASE_PARSER(admin, "GetUser"), m_users(users) {} + int Start(void * data, const char * el, const char ** attr); + + private: + const USERS & m_users; + std::string login; + + void CreateAnswer(); +}; + +class ADD_USER: public BASE_PARSER +{ + public: + ADD_USER(const ADMIN & admin, USERS & users) + : BASE_PARSER(admin, "AddUser"), m_users(users) {} + int Start(void * data, const char * el, const char ** attr); + int End(void * data, const char * el); + + private: + USERS & m_users; + std::string login; + + int CheckUserData(); + void CreateAnswer(); +}; + +class CHG_USER: public BASE_PARSER +{ + public: + CHG_USER(const ADMIN & admin, USERS & users, const TARIFFS & tariffs); + ~CHG_USER(); + int Start(void * data, const char * el, const char ** attr); + int End(void * data, const char * el); + + private: + USERS & m_users; + const TARIFFS & m_tariffs; + USER_STAT_RES * usr; + USER_CONF_RES * ucr; + RESETABLE * upr; + RESETABLE * downr; + std::string cashMsg; + std::string login; + bool cashMustBeAdded; + int res; + + int ApplyChanges(); + void CreateAnswer(); +}; + +class DEL_USER: public BASE_PARSER +{ + public: + DEL_USER(const ADMIN & admin, USERS & users) + : BASE_PARSER(admin, "DelUser"), m_users(users), res(0), u(NULL) {} + int Start(void * data, const char * el, const char ** attr); + int End(void * data, const char * el); + + private: + USERS & m_users; + int res; + USER * u; + + void CreateAnswer(); +}; + +class CHECK_USER: public BASE_PARSER +{ + public: + CHECK_USER(const ADMIN & admin, const USERS & users) + : BASE_PARSER(admin, "CheckUser"), m_users(users) {} + int Start(void * data, const char * el, const char ** attr); + int End(void * data, const char * el); + + private: + const USERS & m_users; + + void CreateAnswer(const char * error); +}; + +} +} + +#endif -- 2.44.2