]> git.stg.codes - stg.git/commitdiff
Split sgconfig parsers into seaparate files.
authorMaxim Mamontov <faust.madf@gmail.com>
Wed, 17 Sep 2014 06:36:24 +0000 (09:36 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Wed, 17 Sep 2014 06:36:24 +0000 (09:36 +0300)
18 files changed:
projects/stargazer/plugins/configuration/sgconfig/parser.cpp
projects/stargazer/plugins/configuration/sgconfig/parser.h
projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp [deleted file]
projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_admins.h [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.cpp
projects/stargazer/plugins/configuration/sgconfig/parser_auth_by.h
projects/stargazer/plugins/configuration/sgconfig/parser_message.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_message.h [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_server_info.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_server_info.h [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp [deleted file]
projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.h [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_user_info.cpp
projects/stargazer/plugins/configuration/sgconfig/parser_user_info.h
projects/stargazer/plugins/configuration/sgconfig/parser_users.cpp [new file with mode: 0644]
projects/stargazer/plugins/configuration/sgconfig/parser_users.h [new file with mode: 0644]

index 45ef82c8a4ee566d1605458d102b228181a729d0..52c0ea9ba114018e4dae5d594d0835761bd0a6c4 100644 (file)
 #include <cstring>
 #include <cstdio> // sprintf
 
-#include <sys/utsname.h>
-
-#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 += "<User result=\"ok\">";
-else
-    answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
-
-answer += "<Login value=\"" + user.GetLogin() + "\"/>";
-
-if (user.GetProperty().password.ModificationTime() > lastTime)
-    {
-    if (showPass)
-        answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
-    else
-        answer += "<Password value=\"++++++\"/>";
-    }
-
-if (user.GetProperty().cash.ModificationTime() > lastTime)
-    answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
-if (user.GetProperty().freeMb.ModificationTime() > lastTime)
-    answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
-if (user.GetProperty().credit.ModificationTime() > lastTime)
-    answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
-
-if (user.GetProperty().nextTariff.Get() != "")
-    {
-    if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
-        user.GetProperty().nextTariff.ModificationTime() > lastTime)
-        answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
-    }
-else
-    {
-    if (user.GetProperty().tariffName.ModificationTime() > lastTime)
-        answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
-    }
-
-if (user.GetProperty().note.ModificationTime() > lastTime)
-    answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
-if (user.GetProperty().phone.ModificationTime() > lastTime)
-    answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
-if (user.GetProperty().address.ModificationTime() > lastTime)
-    answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
-if (user.GetProperty().email.ModificationTime() > lastTime)
-    answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
-
-std::vector<const USER_PROPERTY_LOGGED<std::string> *> 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 += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
-
-if (user.GetProperty().realName.ModificationTime() > lastTime)
-    answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
-if (user.GetProperty().group.ModificationTime() > lastTime)
-    answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
-if (user.GetConnectedModificationTime() > lastTime)
-    answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
-if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
-    answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
-if (user.GetCurrIPModificationTime() > lastTime)
-    answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
-if (user.GetPingTime() > lastTime)
-    answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
-if (user.GetProperty().ips.ModificationTime() > lastTime)
-    answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
-
-answer += "<Traff";
-const DIR_TRAFF & upload(user.GetProperty().down.Get());
-const DIR_TRAFF & download(user.GetProperty().up.Get());
-if (user.GetProperty().up.ModificationTime() > 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("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
-if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
-    answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
-if (user.GetProperty().passive.ModificationTime() > lastTime)
-    answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
-if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
-    answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
-if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
-    answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
-if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
-    answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
-if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
-    answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
-
-if (lastTime == 0)
-    {
-    answer += "<AuthorizedBy>";
-    std::vector<std::string> list(user.GetAuthorizers());
-    for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
-        answer += "<Auth name=\"" + *it + "\"/>";
-    answer += "</AuthorizedBy>";
-    }
-
-answer += "</User>";
-
-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 += "<ServerInfo>";
-answer += std::string("<version value=\"") + SERVER_VERSION + "\"/>";
-answer += "<tariff_num value=\"" + x2str(tariffs->Count()) + "\"/>";
-answer += "<tariff value=\"2\"/>";
-answer += "<user_num value=\"" + x2str(users->Count()) + "\"/>";
-answer += std::string("<uname value=\"") + un + "\"/>";
-answer += "<dir_num value=\"" + x2str(DIR_NUM) + "\"/>";
-answer += "<day_fee value=\"" + x2str(settings->GetDayFee()) + "\"/>";
-
-for (size_t i = 0; i< DIR_NUM; i++)
-    answer += "<dir_name_" + x2str(i) + " value=\"" + Encode12str(settings->GetDirName(i)) + "\"/>";
-
-answer += "</ServerInfo>";
-}
-//-----------------------------------------------------------------------------
-//  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 = "<User result=\"error\" reason=\"User not found.\"/>";
-    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 += "<Users LastUpdate=\"" + x2str(time(NULL)) + "\">";
-else
-    answer += "<Users>";
-
-USER_PTR u;
-
-while (users->SearchNext(h, &u) == 0)
-    answer += UserToXML(*u, true, currAdmin->GetPriv()->userConf || currAdmin->GetPriv()->userPasswd, lastUserUpdateTime);
-
-users->CloseSearch(h);
-
-answer += "</Users>";
-}
-//-----------------------------------------------------------------------------
-//  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 = "<AddUser result=\"ok\"/>";
-else
-    answer = "<AddUser result=\"error\" reason=\"Access denied\"/>";
-}
-//-----------------------------------------------------------------------------
-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<uint64_t>[DIR_NUM];
-downr = new RESETABLE<uint64_t>[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 = "<SetUser result=\"ok\"/>";
-        break;
-    case -1:
-        answer = "<SetUser result=\"error\"/>";
-        break;
-    case -2:
-        answer = "<SetUser result=\"error\"/>";
-        break;
-    default:
-        answer = "<SetUser result=\"error\"/>";
-        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<USER_PROPERTY_LOGGED<std::string> *> 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<unsigned int>(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 = "<SendMessageResult value=\"ok\"/>";
-        break;
-    case res_params_error:
-        printfd(__FILE__, "res_params_error\n");
-        answer = "<SendMessageResult value=\"Parameters error.\"/>";
-        break;
-    case res_unknown:
-        printfd(__FILE__, "res_unknown\n");
-        answer = "<SendMessageResult value=\"Unknown user.\"/>";
-        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 = "<DelUser value=\"error\" reason=\"User not found\"/>";
-else
-    answer = "<DelUser value=\"ok\"/>";
-}
-//-----------------------------------------------------------------------------
-//  CHECK USER
-// <checkuser login="vasya" password=\"123456\"/>
-//-----------------------------------------------------------------------------
-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("<CheckUser value=\"Err\" reason=\"") + error + "\"/>";
-else
-    answer = "<CheckUser value=\"Ok\"/>";
-}
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
index 81bd454b92067814713060dd58293d6fb2393e9c..2e68f8e960cd5a6f977fab9bfe1faa2ea7d82893 100644 (file)
@@ -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 "</" + tag + ">"; }
 
 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<std::string> login;
-    RESETABLE<std::string> password;
-    RESETABLE<std::string> 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<uint64_t> * upr;
-    RESETABLE<uint64_t> * 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<std::string> 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 (file)
index fef2efb..0000000
+++ /dev/null
@@ -1,206 +0,0 @@
-#include "parser.h"
-
-#include "stg/admins.h"
-#include "stg/common.h"
-
-#include <strings.h> // 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 = "<Error Result=\"Error. Access denied.\"/>";
-    return;
-    }
-
-answer.clear();
-
-answer += "<Admins>";
-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 += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
-    }
-admins->CloseSearch(h);
-answer += "</Admins>";
-}
-//-----------------------------------------------------------------------------
-
-//-----------------------------------------------------------------------------
-//  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 = "<DelAdmin Result=\"Ok\"/>";
-else
-    answer = "<DelAdmin Result=\"Error. " + admins->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 = "<AddAdmin Result=\"Ok\"/>";
-else
-    answer = "<AddAdmin Result=\"Error. " + admins->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 = "<ChgAdmin Result = \"Admin '" + login.data() + "' 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 = "<ChgAdmin Result = \"Incorrect parameter Priv.\"/>";
-            return;
-            }
-
-        conf.priv.FromInt(p);
-        }
-
-    if (admins->Change(conf, currAdmin) != 0)
-        answer = "<ChgAdmin Result = \"" + admins->GetStrError() + "\"/>";
-    else
-        answer = "<ChgAdmin Result = \"Ok\"/>";
-    }
-else
-    answer = "<ChgAdmin Result = \"Incorrect parameter login.\"/>";
-}
diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp
new file mode 100644 (file)
index 0000000..c75dc8a
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#include "parser_admins.h"
+
+#include "stg/admins.h"
+#include "stg/common.h"
+
+#include <strings.h> // 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 = "<Error Result=\"Error. Access denied.\"/>";
+        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 += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
+    }
+    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 (file)
index 0000000..63ee627
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_ADMINS_H__
+#define __STG_SGCONFIG_PARSER_ADMINS_H__
+
+#include "parser.h"
+
+#include "stg/resetable.h"
+
+#include <string>
+
+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<std::string> password;
+        RESETABLE<std::string> privAsString;
+        ADMINS & m_admins;
+
+        void CreateAnswer();
+};
+
+}
+}
+
+#endif
index 21765e11c1a9b18dcf10181797145bd69c8e2eee..e16745c58097b4a83c81429bd70e6a0ed2b1b880 100644 (file)
@@ -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 <faust@stargazer.dp.ua>
+ */
+
 #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 = "<AuthorizedBy result=\"error\" reason=\"User not found.\"/>";
-    return;
+        answer = "<AuthorizedBy result=\"error\" reason=\"User not found.\"/>";
+        return;
     }
 
-answer.clear();
-answer += "<AuthorizedBy result=\"ok\">";
-std::vector<std::string> list(u->GetAuthorizers());
-for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
-    answer += "<Auth name=\"" + *it + "\"/>";
-answer += "</AuthorizedBy>";
+    answer = "<AuthorizedBy result=\"ok\">";
+    std::vector<std::string> list(u->GetAuthorizers());
+    for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
+        answer += "<Auth name=\"" + *it + "\"/>";
+    answer += "</AuthorizedBy>";
 }
index c34586c9fa7cd03c422b90b0eeef127fd28d6e22..c103147e4816a44a92aca397f8b6b3d23e4dc0e7 100644 (file)
@@ -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 <faust@stargazer.dp.ua>
+ */
+
 #ifndef __STG_PARSER_AUTH_BY_H__
 #define __STG_PARSER_AUTH_BY_H__
 
+#include "parser.h"
+
 #include <string>
 
-#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 (file)
index 0000000..1876732
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#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<unsigned int>(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 = "<SendMessageResult value=\"ok\"/>";
+            break;
+        case res_params_error:
+            answer = "<SendMessageResult value=\"Parameters error.\"/>";
+            break;
+        case res_unknown:
+            answer = "<SendMessageResult value=\"Unknown user.\"/>";
+            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 (file)
index 0000000..e7c12f4
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_USERS_H__
+#define __STG_SGCONFIG_PARSER_USERS_H__
+
+#include "parser.h"
+
+#include "stg/message.h"
+
+#include <vector>
+#include <string>
+
+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<std::string> 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 (file)
index 0000000..1287bf5
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#include "parser_server_info.h"
+
+#include "stg/common.h"
+#include "stg/version.h"
+#include "stg/const.h"
+
+#include <string>
+#include <cstring>
+
+#include <sys/utsname.h>
+
+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() + "<version value=\"" + SERVER_VERSION + "\"/>" +
+             "<tariff_num value=\"" + x2str(tariffs.Count()) + "\"/>" +
+             "<tariff value=\"2\"/>" +
+             "<user_num value=\"" + x2str(users.Count()) + "\"/>" +
+             "<uname value=\"" + name + "\"/>" +
+             "<dir_num value=\"" + x2str(DIR_NUM) + "\"/>" +
+             "<day_fee value=\"" + x2str(settings.GetDayFee()) + "\"/>";
+
+    for (size_t i = 0; i< DIR_NUM; i++)
+        answer += "<dir_name_" + x2str(i) + " value=\"" + Encode12str(settings.GetDirName(i)) + "\"/>";
+
+    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 (file)
index 0000000..2aabf47
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#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 (file)
index e083f85..0000000
+++ /dev/null
@@ -1,483 +0,0 @@
-#include "parser.h"
-
-#include "stg/tariffs.h"
-#include "stg/users.h"
-#include "stg/common.h"
-
-#include <cstdio> // snprintf
-#include <cstring>
-
-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 = "<Tariffs>";
-
-std::list<TARIFF_DATA> dataList;
-tariffs->GetTariffsData(&dataList);
-std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
-for (; it != dataList.end(); ++it)
-    {
-    answer += "<tariff name=\"" + it->tariffConf.name + "\">";
-
-    for (size_t i = 0; i < DIR_NUM; i++)
-        answer += "<Time" + x2str(i) + " value=\"" +
-            x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
-            x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
-
-    answer += "<PriceDayA value=\"";
-    bool first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += x2str(it->dirPrice[i].priceDayA * pt_mega);
-        }
-    answer += "\"/>";
-
-    answer += "<PriceDayB value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += x2str(it->dirPrice[i].priceDayB * pt_mega);
-        }
-    answer += "\"/>";
-
-    answer += "<PriceNightA value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += x2str(it->dirPrice[i].priceNightA * pt_mega);
-        }
-    answer += "\"/>";
-
-    answer += "<PriceNightB value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += x2str(it->dirPrice[i].priceNightB * pt_mega);
-        }
-    answer += "\"/>";
-
-    answer += "<Threshold value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += x2str(it->dirPrice[i].threshold);
-        }
-    answer += "\"/>";
-
-    answer += "<SinglePrice value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += (it->dirPrice[i].singlePrice ? "1" : "0");
-        }
-    answer += "\"/>";
-
-    answer += "<NoDiscount value=\"";
-    first = true;
-    for (size_t i = 0; i < DIR_NUM; i++)
-        {
-        if (first)
-            first = false;
-        else
-            answer += "/";
-        answer += (it->dirPrice[i].noDiscount ? "1" : "0");
-        }
-    answer += "\"/>";
-
-    answer += "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>";
-
-    answer += "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>";
-
-    answer += "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>";
-
-    switch (it->tariffConf.traffType)
-        {
-        case TRAFF_UP:
-            answer += "<TraffType value=\"up\"/>";
-            break;
-        case TRAFF_DOWN:
-            answer += "<TraffType value=\"down\"/>";
-            break;
-        case TRAFF_UP_DOWN:
-            answer += "<TraffType value=\"up+down\"/>";
-            break;
-        case TRAFF_MAX:
-            answer += "<TraffType value=\"max\"/>";
-            break;
-        }
-
-    answer += "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>";
-
-    answer += "</tariff>";
-    }
-answer += "</Tariffs>";
-}
-//-----------------------------------------------------------------------------
-//  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 = "<AddTariff Result=\"Ok\"/>";
-else
-    answer = "<AddTariff Result=\"Error. " + tariffs->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 = "<DelTariff Result=\"Error. Tariff \'" + tariffToDel + "\' cannot be deleted. Tariff in use.\"/>";
-else if (tariffs->Del(tariffToDel, currAdmin) == 0)
-    answer = "<DelTariff Result=\"Ok\"/>";
-else
-    answer = "<DelTariff Result=\"Error. " + tariffs->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 = "<SetTariff Result=\"ok\"/>";
-        return;
-        }
-    else
-        {
-        answer = "<SetTariff Result=\"Change tariff error! " + tariffs->GetStrError() + "\"/>";
-        return;
-        }
-    }
-answer = "<SetTariff Result=\"Change tariff error!\"/>";
-}
-//-----------------------------------------------------------------------------
diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp
new file mode 100644 (file)
index 0000000..8d54b1d
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#include "parser_tariffs.h"
+
+#include "stg/tariffs.h"
+#include "stg/users.h"
+#include "stg/common.h"
+#include "stg/resetable.h"
+
+#include <cstdio> // snprintf
+#include <cstring>
+
+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 <typename A, typename C, typename F>
+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 <typename A, typename C, typename F>
+bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> 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<TARIFF_DATA> dataList;
+    m_tariffs.GetTariffsData(&dataList);
+    std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
+    for (; it != dataList.end(); ++it)
+        {
+        answer += "<tariff name=\"" + it->tariffConf.name + "\">";
+
+        for (size_t i = 0; i < DIR_NUM; i++)
+            answer += "<Time" + x2str(i) + " value=\"" +
+                x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
+                x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
+
+        answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
+                  "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
+                  "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
+                  "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
+                  "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
+                  "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
+                  "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
+                  "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
+                  "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
+                  "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
+                  "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
+                  "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
+                  "</tariff>";
+        }
+
+    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 (file)
index 0000000..c96163f
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_TARIFFS_H__
+#define __STG_SGCONFIG_PARSER_TARIFFS_H__
+
+#include "parser.h"
+
+#include "stg/tariff_conf.h"
+
+#include <string>
+
+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
index de28eaba925b1b15ef8054a5d1bc0b170a001666..5a101570ec2b888e67b1d5bacd90250a6beadcb9 100644 (file)
 
 #include <strings.h> // 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 = "<UserInfo result=\"error\"/>";
-    return;
+        answer = "<UserInfo result=\"error\"/>";
+        return;
     }
 
-answer = "<UserInfo lastAuthTime=\"" + x2str(u->GetAuthorizedModificationTime()) + "\"" +
-         " lastDisconnectTime=\"" + x2str(u->GetConnectedModificationTime()) + "\"" +
-         " connected=\"" + (u->GetConnected() ? "true" : "false") + "\"" +
-         " lastDisconnectReason=\"" + u->GetLastDisconnectReason() + "\">";
-std::vector<std::string> list(u->GetAuthorizers());
-for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
-    answer += "<Auth name=\"" + *it + "\"/>";
-answer += "</UserInfo>";
+    answer = "<UserInfo lastAuthTime=\"" + x2str(u->GetAuthorizedModificationTime()) + "\"" +
+             " lastDisconnectTime=\"" + x2str(u->GetConnectedModificationTime()) + "\"" +
+             " connected=\"" + (u->GetConnected() ? "true" : "false") + "\"" +
+             " lastDisconnectReason=\"" + u->GetLastDisconnectReason() + "\">";
+    std::vector<std::string> list(u->GetAuthorizers());
+    for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
+        answer += "<Auth name=\"" + *it + "\"/>";
+    answer += "</UserInfo>";
 }
index ccd1d94e4fabc3dced308863a0b50fd397819fad..284b352c732e6b816b0cda77f09bad2ab372ceef 100644 (file)
 
 #include <string>
 
-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 (file)
index 0000000..16054a9
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#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 <cstdio>
+
+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 += "<User result=\"ok\">";
+    else
+        answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
+
+    answer += "<Login value=\"" + user.GetLogin() + "\"/>";
+
+    if (user.GetProperty().password.ModificationTime() > lastTime)
+    {
+        if (showPass)
+            answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
+        else
+            answer += "<Password value=\"++++++\"/>";
+    }
+
+    if (user.GetProperty().cash.ModificationTime() > lastTime)
+        answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
+    if (user.GetProperty().freeMb.ModificationTime() > lastTime)
+        answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
+    if (user.GetProperty().credit.ModificationTime() > lastTime)
+        answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
+
+    if (user.GetProperty().nextTariff.Get() != "")
+    {
+        if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
+            user.GetProperty().nextTariff.ModificationTime() > lastTime)
+            answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
+    }
+    else
+    {
+        if (user.GetProperty().tariffName.ModificationTime() > lastTime)
+            answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
+    }
+
+    if (user.GetProperty().note.ModificationTime() > lastTime)
+        answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
+    if (user.GetProperty().phone.ModificationTime() > lastTime)
+        answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
+    if (user.GetProperty().address.ModificationTime() > lastTime)
+        answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
+    if (user.GetProperty().email.ModificationTime() > lastTime)
+        answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
+
+    std::vector<const USER_PROPERTY_LOGGED<std::string> *> 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 += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
+
+    if (user.GetProperty().realName.ModificationTime() > lastTime)
+        answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
+    if (user.GetProperty().group.ModificationTime() > lastTime)
+        answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
+    if (user.GetConnectedModificationTime() > lastTime)
+        answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
+    if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
+        answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
+    if (user.GetCurrIPModificationTime() > lastTime)
+        answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
+    if (user.GetPingTime() > lastTime)
+        answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
+    if (user.GetProperty().ips.ModificationTime() > lastTime)
+        answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
+
+    answer += "<Traff";
+    const DIR_TRAFF & upload(user.GetProperty().down.Get());
+    const DIR_TRAFF & download(user.GetProperty().up.Get());
+    if (user.GetProperty().up.ModificationTime() > 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("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
+    if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
+        answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
+    if (user.GetProperty().passive.ModificationTime() > lastTime)
+        answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
+    if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
+        answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
+    if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
+        answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
+    if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
+        answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
+    if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
+        answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
+
+    if (lastTime == 0)
+    {
+        answer += "<AuthorizedBy>";
+        std::vector<std::string> list(user.GetAuthorizers());
+        for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
+            answer += "<Auth name=\"" + *it + "\"/>";
+        answer += "</AuthorizedBy>";
+    }
+
+    answer += "</User>";
+
+    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<uint64_t>[DIR_NUM]),
+      downr(new RESETABLE<uint64_t>[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<USER_PROPERTY_LOGGED<std::string> *> 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 (file)
index 0000000..da4583b
--- /dev/null
@@ -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 <stg34@stargazer.dp.ua>
+ *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_USERS_H__
+#define __STG_SGCONFIG_PARSER_USERS_H__
+
+#include "parser.h"
+
+#include "stg/resetable.h"
+
+#include <string>
+
+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<uint64_t> * upr;
+        RESETABLE<uint64_t> * 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