From fdfb7497dea6cc94a44133ef19e953ced167e855 Mon Sep 17 00:00:00 2001
From: Maxim Mamontov <faust@gts.dp.ua>
Date: Fri, 4 Mar 2011 18:23:44 +0200
Subject: [PATCH] Add creditExpire setting/getting via sgconf

---
 projects/sgconf/common_sg.cpp  | 23 +++++++++++++++++++++--
 projects/sgconf/main.cpp       | 34 ++++++++++++++++++++++++++++++++--
 projects/sgconf/request.h      |  1 +
 stglibs/srvconf.lib/parser.cpp | 16 ++++++++++++++++
 stglibs/srvconf.lib/servconf.h |  1 +
 5 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/projects/sgconf/common_sg.cpp b/projects/sgconf/common_sg.cpp
index 09fcfd77..1fc0685a 100644
--- a/projects/sgconf/common_sg.cpp
+++ b/projects/sgconf/common_sg.cpp
@@ -81,6 +81,7 @@ HelpParams hp[] =
 {
     {"set tariff",              "get tariff",           "-t",   "<tariff:now|delayed>"},
     {"set credit",              "get credit",           "-r",   "<credit>"},
+    {"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...]>"},
@@ -330,6 +331,26 @@ if (!req->cash.res_empty())
 if (!req->credit.res_empty())
     cout << "credit=" << ud->credit << endl;
 
+if (!req->creditExpire.res_empty())
+    {
+    char buf[32];
+    struct tm brokenTime;
+    time_t tt = ud->creditExpire;
+
+    brokenTime.tm_wday = 0;
+    brokenTime.tm_yday = 0;
+    brokenTime.tm_isdst = 0;
+    brokenTime.tm_hour = 0;
+    brokenTime.tm_min = 0;
+    brokenTime.tm_sec = 0;
+
+    gmtime_r(&tt, &brokenTime);
+
+    strftime(buf, 32, "%Y-%m-%d", &brokenTime);
+
+    cout << "creditExpire=" << buf << endl;
+    }
+
 if (!req->down.res_empty())
     cout << "down=" << ud->down << endl;
 
@@ -475,5 +496,3 @@ else
 return 0;
 }
 //-----------------------------------------------------------------------------
-
-
diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp
index ce8af73f..ec7dadc7 100644
--- a/projects/sgconf/main.cpp
+++ b/projects/sgconf/main.cpp
@@ -312,6 +312,22 @@ delete[] s;
 return ss;
 }
 //-----------------------------------------------------------------------------
+time_t ParseCreditExpire(const char * str)
+{
+struct tm brokenTime;
+
+brokenTime.tm_wday = 0;
+brokenTime.tm_yday = 0;
+brokenTime.tm_isdst = 0;
+brokenTime.tm_hour = 0;
+brokenTime.tm_min = 0;
+brokenTime.tm_sec = 0;
+
+stg_strptime(str, "%Y-%m-%d", &brokenTime);
+
+return stg_timegm(&brokenTime);
+}
+//-----------------------------------------------------------------------------
 void ParseAnyString(const char * c, string * msg, const char * enc)
 {
 iconv_t cd;
@@ -419,6 +435,12 @@ if (!req->credit.res_empty())
     strcat(r, str);
     }
 
+if (!req->creditExpire.res_empty())
+    {
+    sprintf(str, "<creditExpire value=\"%ld\"/>\n", req->creditExpire.const_data());
+    strcat(r, str);
+    }
+
 if (!req->prepaidTraff.res_empty())
     {
     sprintf(str, "<FreeMb value=\"%f\"/>\n", req->prepaidTraff.const_data());
@@ -681,7 +703,7 @@ REQUEST req;
 RESETABLE<string>   t1;
 int missedOptionArg = false;
 
-const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISO";
+const char * short_options_get = "s:p:a:w:u:crtmodieNADLPGISOE";
 int option_index = -1;
 
 while (1)
@@ -726,6 +748,10 @@ while (1)
             req.credit = 1;
             break;
 
+        case 'E': //credit expire
+            req.creditExpire = 1;
+            break;
+
         case 'd': //down
             req.down = 1;
             break;
@@ -860,7 +886,7 @@ REQUEST req;
 
 RESETABLE<string>   t1;
 
-const char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:I:S:O:";
+const char * short_options_set = "s:p:a:w:u:c:r:t:m:o:d:i:e:v:nlN:A:D:L:P:G:I:S:O:E:";
 
 int missedOptionArg = false;
 
@@ -912,6 +938,10 @@ while (1)
             req.credit = ParseCredit(optarg);
             break;
 
+        case 'E': //credit expire
+            req.creditExpire = ParseCreditExpire(optarg);
+            break;
+
         case 'd': //down
             req.down = ParseDownPassive(optarg);
             break;
diff --git a/projects/sgconf/request.h b/projects/sgconf/request.h
index 2f2408ca..6878f656 100644
--- a/projects/sgconf/request.h
+++ b/projects/sgconf/request.h
@@ -77,6 +77,7 @@ bool                deleteUser;
 
 RESETABLE<string>   usrMsg;
 RESETABLE<double>   credit;
+RESETABLE<time_t>   creditExpire;
 RESETABLE<string>   usrPasswd;
 RESETABLE<bool>     down;
 RESETABLE<bool>     passive;
diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp
index 139c91bc..89b2457c 100644
--- a/stglibs/srvconf.lib/parser.cpp
+++ b/stglibs/srvconf.lib/parser.cpp
@@ -165,6 +165,14 @@ if (strcasecmp(el, "credit") == 0)
         }
     }
 
+if (strcasecmp(el, "creditExpire") == 0)
+    {
+    if (str2x(attr[1], user.creditExpire) < 0)
+        {
+        return;
+        }
+    }
+
 /*if (strcasecmp(el, "freemb") == 0)
     {
     if (strtodouble2(attr[1], user.freeMb) < 0)
@@ -518,6 +526,14 @@ if (strcasecmp(el, "currip") == 0)
     user.ip = inet_addr(attr[1]);
     }
 
+if (strcasecmp(el, "creditExpire") == 0)
+    {
+    if (str2x(attr[1], user.creditExpire) < 0)
+        {
+        return;
+        }
+    }
+
 for (int i = 0; i < USERDATA_NUM; i++)
     {
     string num;
diff --git a/stglibs/srvconf.lib/servconf.h b/stglibs/srvconf.lib/servconf.h
index 9edbe6dd..29151d62 100644
--- a/stglibs/srvconf.lib/servconf.h
+++ b/stglibs/srvconf.lib/servconf.h
@@ -72,6 +72,7 @@ struct USERDATA
     std::string     password;
     double          cash;
     double          credit;
+    time_t          creditExpire;
     double          lastCash;
     double          prepaidTraff;
     int             down;
-- 
2.44.2