]> git.stg.codes - stg.git/blobdiff - projects/sgconf/common_sg.cpp
Ticket 37. '../stglibs/common.lib/strptime.cpp' added in the
[stg.git] / projects / sgconf / common_sg.cpp
index a54826dfb7dad95bb8ecb476c2714bdf118e027b..9c1b7283705bdf3cbc1652811e847c463b55cab9 100644 (file)
  */
 
 
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <locale.h>
+#include <langinfo.h>
+#include <iostream>
+#include <iconv.h>
+
+#include "stg/common.h"
 #include "sg_error_codes.h"
 #include "common_sg.h"
 #include "version_sg.h"
 
-#include "stg/common.h"
-
-#include <iostream>
-#include <vector>
-
-#include <cstdio>
-#include <cstring>
-#include <cstdlib>
-#include <cerrno>
-#include <clocale>
-
-#include <langinfo.h>
-#include <iconv.h>
+using namespace std;
 
 const int usageConf = 0;
 const int usageInfo = 1;
@@ -49,26 +47,24 @@ const int usageInfo = 1;
 const int TO_KOI8 = 0;
 const int FROM_KOI8 = 1;
 //-----------------------------------------------------------------------------
-struct ResultData
+struct GetUserCbData
 {
-    bool result;
-    std::string reason;
+    void * data;
+    bool * result;
 };
 //-----------------------------------------------------------------------------
-struct GetUserData
+struct AuthByCbData
 {
-    GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
-    REQUEST & request;
-    bool result;
-    std::string reason;
+    void * data;
+    bool * result;
 };
 //---------------------------------------------------------------------------
 struct HelpParams
 {
-    std::string setActionName;
-    std::string getActionName;
-    std::string valueName;
-    std::string valueParam;
+    string setActionName;
+    string getActionName;
+    string valueName;
+    string valueParam;
 };
 //---------------------------------------------------------------------------
 void Usage(int usageType)
@@ -94,7 +90,7 @@ HelpParams hp[] =
     {"set credit expire",       "get credit expire",    "-E",   "<credit_expire_date>"},
     {"set password",            "get password",         "-o",   "<new_password>"},
     {"set prepaid traffic",     "get prepaid traffic",  "-e",   "<prepaid>"},
-    {"set IP-addresses",        "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
+    {"set IP-addresses",       "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
     {"set name",                "get name",             "-A",   "<name>"},
     {"set note",                "get note",             "-N",   "<note>"},
     {"set street address",      "get street address",   "-D",   "<address>"},
@@ -177,7 +173,7 @@ short int ParseServerPort(const char * p)
 int port;
 if (str2x(p, port) != 0)
     {
-    printf("Incorrect server port %s\n", p);
+    printf("Incorresct server port %s\n", p);
     exit(NETWORK_ERR_CODE);
     }
 return (short)port;
@@ -187,7 +183,7 @@ char * ParseAdminLogin(char * adm)
 {
 if (CheckLogin(adm))
     {
-    printf("Incorrect admin login %s\n", adm);
+    printf("Incorresct admin login %s\n", adm);
     exit(PARAMETER_PARSING_ERR_CODE);
     }
 return adm;
@@ -208,13 +204,13 @@ char * ParseUser(char * usr)
 {
 if (CheckLogin(usr))
     {
-    printf("Incorrect user login %s\n", usr);
+    printf("Incorresct user login %s\n", usr);
     exit(PARAMETER_PARSING_ERR_CODE);
     }
 return usr;
 }
 //-----------------------------------------------------------------------------
-void ConvertKOI8(const std::string & src, std::string * dst, int encType)
+void ConvertKOI8(const string & src, string * dst, int encType)
 {
 iconv_t cd;
 char * ob = new char[src.size() * 2 + 1];
@@ -263,7 +259,7 @@ if (cd == (iconv_t) -1)
     exit(ICONV_ERR_CODE);
     }
 
-#if defined(FREE_BSD) || defined(FREE_BSD5)
+#if defined(CONST_ICONV)
 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
 #else
 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
@@ -289,75 +285,67 @@ delete[] ob;
 delete[] ib;
 }
 //-----------------------------------------------------------------------------
-void ConvertFromKOI8(const std::string & src, std::string * dst)
+void ConvertFromKOI8(const string & src, string * dst)
 {
 ConvertKOI8(src, dst, FROM_KOI8);
 }
 //-----------------------------------------------------------------------------
-void SendMessageCallback(bool result, const std::string & reason, void * d)
-{
-ResultData * data = static_cast<ResultData *>(d);
-data->result = result;
-data->reason = reason;
-}
-//-----------------------------------------------------------------------------
-void RecvSetUserAnswer(bool result, const std::string & reason, void * d)
+void ConvertToKOI8(const string & src, string * dst)
 {
-ResultData * data = static_cast<ResultData *>(d);
-data->result = result;
-data->reason = reason;
+ConvertKOI8(src, dst, TO_KOI8);
 }
 //-----------------------------------------------------------------------------
-void RecvAuthByData(bool result, const std::string & reason,
-                    const PARSER_AUTH_BY::INFO & list, void * d)
+int RecvSetUserAnswer(const char * ans, void * d)
 {
-ResultData * data = static_cast<ResultData *>(d);
-data->result = result;
-data->reason = reason;
+GetUserCbData * gucbd;
+gucbd = (GetUserCbData *)d;
 
-if (!result)
-    return;
+bool * result = gucbd->result;
 
-for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
-    std::cout << *it << "\n";
+//REQUEST * req = (REQUEST *)gucbd->data;
+
+//printf("ans=%s\n", ans);
+if (strcasecmp("Ok", ans) == 0)
+    *result = true;
+else
+    *result = false;
 
-std::cout << std::endl;
+return 0;
 }
 //-----------------------------------------------------------------------------
 struct StringReqParams
 {
-    std::string name;
-    RESETABLE<std::string> reqParam;
-    const std::string * value;
+    string name;
+    RESETABLE<string> reqParam;
+    string * value;
 };
 //-----------------------------------------------------------------------------
-void GetUserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * d)
+void RecvUserData(USERDATA * ud, void * d)
 {
-GetUserData * data = static_cast<GetUserData *>(d);
-data->result = false;
-data->reason = reason;
+GetUserCbData * gucbd;
+gucbd = (GetUserCbData *)d;
 
-if (!result)
-    return;
+bool * result = gucbd->result;
+
+REQUEST * req = (REQUEST *)gucbd->data;
 
-if (info.login == "")
+if (ud->login == "")
     {
-    data->result = false;
-    data->reason = "Invalid login.";
+    *result = false;
     return;
     }
 
-if (!data->request.cash.res_empty())
-    cout << "cash = " << info.cash << endl;
+if (!req->cash.empty())
+    cout << "cash=" << ud->cash << endl;
 
-if (!data->request.credit.res_empty())
-    cout << "credit = " << info.credit << endl;
+if (!req->credit.empty())
+    cout << "credit=" << ud->credit << endl;
 
-if (!data->request.creditExpire.res_empty())
+if (!req->creditExpire.empty())
     {
     char buf[32];
     struct tm brokenTime;
-    time_t tt = info.creditExpire;
+    time_t tt = ud->creditExpire;
 
     brokenTime.tm_wday = 0;
     brokenTime.tm_yday = 0;
@@ -370,195 +358,204 @@ if (!data->request.creditExpire.res_empty())
 
     strftime(buf, 32, "%Y-%m-%d", &brokenTime);
 
-    cout << "creditExpire = " << buf << endl;
+    cout << "creditExpire=" << buf << endl;
     }
 
-if (!data->request.down.res_empty())
-    cout << "down = " << info.down << endl;
+if (!req->down.empty())
+    cout << "down=" << ud->down << endl;
 
-if (!data->request.passive.res_empty())
-    cout << "passive = " << info.passive << endl;
+if (!req->passive.empty())
+    cout << "passive=" << ud->passive << endl;
 
-if (!data->request.disableDetailStat.res_empty())
-    cout << "disableDetailStat = " << info.disableDetailStat << endl;
+if (!req->disableDetailStat.empty())
+    cout << "disableDetailStat=" << ud->disableDetailStat << endl;
 
-if (!data->request.alwaysOnline.res_empty())
-    cout << "alwaysOnline = " << info.alwaysOnline << endl;
+if (!req->alwaysOnline.empty())
+    cout << "alwaysOnline=" << ud->alwaysOnline << endl;
 
-if (!data->request.prepaidTraff.res_empty())
-    cout << "prepaidTraff = " << info.prepaidTraff << endl;
+if (!req->prepaidTraff.empty())
+    cout << "prepaidTraff=" << ud->prepaidTraff << endl;
 
 for (int i = 0; i < DIR_NUM; i++)
     {
-    if (!data->request.sessionUpload[i].res_empty())
-        cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
-    if (!data->request.sessionDownload[i].res_empty())
-        cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
-    }
-
-for (int i = 0; i < DIR_NUM; i++)
-    {
-    if (!data->request.monthUpload[i].res_empty())
-        cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
-    if (!data->request.monthDownload[i].res_empty())
-        cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
+    if (!req->u[i].empty())
+        cout << "u" << i << "=" << ud->stat.mu[i] << endl;
+    if (!req->d[i].empty())
+        cout << "d" << i << "=" << ud->stat.md[i] << endl;
     }
 
 for (int i = 0; i < USERDATA_NUM; i++)
     {
-    if (!data->request.userData[i].res_empty())
+    if (!req->ud[i].empty())
         {
-        std::string str;
-        ConvertFromKOI8(info.userData[i], &str);
-        cout << "user data " << i << " = " << str << endl;
+        string str;
+        ConvertFromKOI8(ud->userData[i], &str);
+        cout << "userdata" << i << "=" << str << endl;
         }
     }
 
 StringReqParams strReqParams[] =
 {
-    {"note",     data->request.note,        &info.note},
-    {"name",     data->request.name,        &info.name},
-    {"address",  data->request.address,     &info.address},
-    {"email",    data->request.email,       &info.email},
-    {"phone",    data->request.phone,       &info.phone},
-    {"group",    data->request.group,       &info.group},
-    {"tariff",   data->request.tariff,      &info.tariff},
-    {"password", data->request.usrPasswd,   &info.password},
-    {"ip",       data->request.ips,         &info.ips} // IP-address of user
+    {"note",     req->note,        &ud->note},
+    {"name",     req->name,        &ud->name},
+    {"address",  req->address,     &ud->address},
+    {"email",    req->email,       &ud->email},
+    {"phone",    req->phone,       &ud->phone},
+    {"group",    req->group,       &ud->group},
+    {"tariff",   req->tariff,      &ud->tariff},
+    {"password", req->usrPasswd,   &ud->password},
+    {"ip",      req->ips,         &ud->ips}    // IP-address of user
 };
 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
     {
-    if (!strReqParams[i].reqParam.res_empty())
+    if (!strReqParams[i].reqParam.empty())
         {
         string str;
         ConvertFromKOI8(*strReqParams[i].value, &str);
-        cout << strReqParams[i].name << " = " << str << endl;
+        cout << strReqParams[i].name << "=" << str << endl;
         }
     }
-data->result = true;
+*result = true;
 }
 //-----------------------------------------------------------------------------
-bool ProcessSetUser(const std::string & server,
-                    int port,
-                    const std::string & admLogin,
-                    const std::string & admPasswd,
-                    const std::string & str)
+void RecvAuthByData(const std::vector<std::string> & list, void * d)
 {
-SERVCONF sc;
+AuthByCbData * abcbd;
+abcbd = (AuthByCbData *)d;
 
-sc.SetServer(server.c_str());
-sc.SetPort(port);
-sc.SetAdmLogin(admLogin.c_str());
-sc.SetAdmPassword(admPasswd.c_str());
-
-ResultData data;
-sc.SetChgUserCallback(RecvSetUserAnswer, &data);
-int res = sc.ChgUser(str.c_str());
+bool * result = abcbd->result;
 
-if (res == st_ok && data.result)
-    {
-    printf("Ok\n");
-    return false;
-    }
+for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
+    cout << *it << "\n";
+cout << endl;
 
-printf("Error\n");
-if (res != st_ok)
-    printf("%s\n", sc.GetStrError().c_str());
-else
-    printf("%s\n", data.reason.c_str());
-return true;
+*result = true;
 }
 //-----------------------------------------------------------------------------
-bool ProcessSendMessage(const std::string & server, uint16_t port,
-                        const std::string & login, const std::string & password,
-                        const std::string & requestString)
+int ProcessSetUser(const std::string &server,
+                   int port,
+                   const std::string &admLogin,
+                   const std::string &admPasswd,
+                   const std::string &str,
+                   void * data,
+                   bool isMessage)
 {
 SERVCONF sc;
 
-sc.SetServer(server.c_str());
-sc.SetPort(port);
-sc.SetAdmLogin(login.c_str());
-sc.SetAdmPassword(password.c_str());
+bool result = false;
+
+
+sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
+sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
+sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
+sc.SetAdmPassword(admPasswd.c_str());
+
+// TODO Good variable name :)
+GetUserCbData gucbd;
 
-ResultData data;
-sc.SetSendMessageCallback(SendMessageCallback, &data);
-int res = sc.SendMessage(requestString.c_str());
+gucbd.data = data;
+gucbd.result = &result;
 
-if (res == st_ok && data.result)
+if (isMessage)
     {
-    printf("Ok\n");
-    return true;
+    sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
+    sc.MsgUser(str.c_str());
+    }
+else
+    {
+    sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
+    sc.ChgUser(str.c_str());
     }
 
-printf("Error\n");
-if (res != st_ok)
-    printf("%s\n", sc.GetStrError().c_str());
+if (result)
+    {
+    printf("Ok\n");
+    return 0;
+    }
 else
-    printf("%s\n", data.reason.c_str());
-return false;
+    {
+    printf("Error\n");
+    return -1;
+    }
+
+return 0;
 }
 //-----------------------------------------------------------------------------
-bool ProcessGetUser(const std::string &server,
-                    int port,
-                    const std::string &admLogin,
-                    const std::string &admPasswd,
-                    const std::string &login,
-                    REQUEST & request)
+int ProcessGetUser(const std::string &server,
+                   int port,
+                   const std::string &admLogin,
+                   const std::string &admPasswd,
+                   const std::string &login,
+                   void * data)
 {
 SERVCONF sc;
 
-sc.SetServer(server.c_str());
-sc.SetPort(port);
-sc.SetAdmLogin(admLogin.c_str());
+bool result = false;
+
+sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
+sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
+sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
 sc.SetAdmPassword(admPasswd.c_str());
 
-GetUserData data(request, false);
+// TODO Good variable name :)
+GetUserCbData gucbd;
+
+gucbd.data = data;
+gucbd.result = &result;
 
-sc.SetGetUserCallback(GetUserCallback, &data);
-bool res = (sc.GetUser(login.c_str()) == st_ok);
+sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
+sc.GetUser(login.c_str());
 
-if (res && data.result)
+if (result)
     {
     printf("Ok\n");
-    return true;
+    return 0;
     }
-
-printf("Error\n");
-if (!res)
-    printf("%s\n", sc.GetStrError().c_str());
 else
-    printf("%s\n", data.reason.c_str());
-return false;
+    {
+    printf("Error\n");
+    return -1;
+    }
+
+return 0;
 }
 //-----------------------------------------------------------------------------
-bool ProcessAuthBy(const std::string &server,
-                   int port,
-                   const std::string &admLogin,
-                   const std::string &admPasswd,
-                   const std::string &login)
+int ProcessAuthBy(const std::string &server,
+                  int port,
+                  const std::string &admLogin,
+                  const std::string &admPasswd,
+                  const std::string &login,
+                  void * data)
 {
 SERVCONF sc;
 
-sc.SetServer(server.c_str());
-sc.SetPort(port);
-sc.SetAdmLogin(admLogin.c_str());
+bool result = false;
+
+sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
+sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
+sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
 sc.SetAdmPassword(admPasswd.c_str());
 
-ResultData data;
-sc.SetAuthByCallback(RecvAuthByData, &data);
-bool res = (sc.AuthBy(login.c_str()) == st_ok);
+// TODO Good variable name :)
+AuthByCbData abcbd;
+
+abcbd.data = data;
+abcbd.result = &result;
 
-if (res && data.result)
+sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
+sc.GetUserAuthBy(login.c_str());
+
+if (result)
     {
     printf("Ok\n");
-    return true;
+    return 0;
     }
-
-printf("Error\n");
-if (!res)
-    printf("%s\n", sc.GetStrError().c_str());
 else
-    printf("%s\n", data.reason.c_str());
-return false;
+    {
+    printf("Error\n");
+    return -1;
+    }
+
+return 0;
 }
 //-----------------------------------------------------------------------------