]> git.stg.codes - stg.git/blob - projects/sgconf/common_sg.cpp
9c1b7283705bdf3cbc1652811e847c463b55cab9
[stg.git] / projects / sgconf / common_sg.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Author: faust $
23  $Revision: 1.12 $
24  $Date: 2009/06/08 10:02:28 $
25  */
26
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <locale.h>
33 #include <langinfo.h>
34 #include <iostream>
35 #include <iconv.h>
36
37 #include "stg/common.h"
38 #include "sg_error_codes.h"
39 #include "common_sg.h"
40 #include "version_sg.h"
41
42 using namespace std;
43
44 const int usageConf = 0;
45 const int usageInfo = 1;
46
47 const int TO_KOI8 = 0;
48 const int FROM_KOI8 = 1;
49 //-----------------------------------------------------------------------------
50 struct GetUserCbData
51 {
52     void * data;
53     bool * result;
54 };
55 //-----------------------------------------------------------------------------
56 struct AuthByCbData
57 {
58     void * data;
59     bool * result;
60 };
61 //---------------------------------------------------------------------------
62 struct HelpParams
63 {
64     string setActionName;
65     string getActionName;
66     string valueName;
67     string valueParam;
68 };
69 //---------------------------------------------------------------------------
70 void Usage(int usageType)
71 {
72 printf("Sgconf version: %s\n\n", VERSION_SG);
73
74 char action[4];
75 if (usageType == usageConf)
76     strcpy(action, "set");
77 else
78     strcpy(action, "get");
79
80 printf("To add or to set cash use:\n");
81 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
82 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
83 printf("To get cash use:\n");
84 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
85
86 HelpParams hp[] =
87 {
88     {"set tariff",              "get tariff",           "-t",   "<tariff:now|delayed>"},
89     {"set credit",              "get credit",           "-r",   "<credit>"},
90     {"set credit expire",       "get credit expire",    "-E",   "<credit_expire_date>"},
91     {"set password",            "get password",         "-o",   "<new_password>"},
92     {"set prepaid traffic",     "get prepaid traffic",  "-e",   "<prepaid>"},
93     {"set IP-addresses",        "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
94     {"set name",                "get name",             "-A",   "<name>"},
95     {"set note",                "get note",             "-N",   "<note>"},
96     {"set street address",      "get street address",   "-D",   "<address>"},
97     {"set email",               "get email",            "-L",   "<email>"},
98     {"set phone",               "get phone",            "-P",   "<phone>"},
99     {"set group",               "get group",            "-G",   "<group>"},
100     {"set/unset down",          "get down",             "-d",   "<0/1>"},
101     {"set/unset \'passive\'",   "get \'passive\'",      "-i",   "<0/1>"},
102     {"set/unset \'disableDetailStat\'",   "get \'disableDetailStat\'",      "--disable-stat",   "<0/1>"},
103     {"set/unset \'alwaysOnline\'",   "get \'alwaysOnline\'",      "--always-online",   "<0/1>"},
104 };
105
106 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
107     {
108     printf("To %s use:\n", hp[i].setActionName.c_str());
109     printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
110            hp[i].valueName.c_str(), hp[i].valueParam.c_str());
111     printf("To %s use:\n", hp[i].getActionName.c_str());
112     printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
113            hp[i].valueName.c_str());
114     }
115
116 printf("To set user\'s upload traffic value use:\n");
117 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
118 printf("To get user\'s upload traffic value use:\n");
119 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
120
121 printf("To set user\'s download traffic value use:\n");
122 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
123 printf("To get user\'s download traffic value use:\n");
124 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
125
126 printf("To set userdata<0...9> use:\n");
127 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
128 printf("To get userdata<0...9> use:\n");
129 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
130
131 printf("To get user's authorizers list use:\n");
132 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
133
134 printf("To send message use:\n");
135 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
136
137 printf("To create user use:\n");
138 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
139
140 printf("To delete user use:\n");
141 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
142 }
143 //---------------------------------------------------------------------------
144 void UsageConf()
145 {
146 Usage(usageConf);
147 }
148 //---------------------------------------------------------------------------
149 void UsageInfo()
150 {
151 Usage(usageInfo);
152 }
153 //---------------------------------------------------------------------------
154 int CheckLogin(const char * login)
155 {
156 for (int i = 0; i < (int)strlen(login); i++)
157     {
158     if (!(( login[i] >= 'a' && login[i] <= 'z')
159         || (login[i] >= 'A' && login[i] <= 'Z')
160         || (login[i] >= '0' && login[i] <= '9')
161         ||  login[i] == '.'
162         ||  login[i] == '_'
163         ||  login[i] == '-'))
164         {
165         return 1;
166         }
167     }
168 return 0;
169 }
170 //-----------------------------------------------------------------------------
171 short int ParseServerPort(const char * p)
172 {
173 int port;
174 if (str2x(p, port) != 0)
175     {
176     printf("Incorresct server port %s\n", p);
177     exit(NETWORK_ERR_CODE);
178     }
179 return (short)port;
180 }
181 //-----------------------------------------------------------------------------
182 char * ParseAdminLogin(char * adm)
183 {
184 if (CheckLogin(adm))
185     {
186     printf("Incorresct admin login %s\n", adm);
187     exit(PARAMETER_PARSING_ERR_CODE);
188     }
189 return adm;
190 }
191 //-----------------------------------------------------------------------------
192 char * ParsePassword(char * pass)
193 {
194 if (strlen(pass) >= ADM_PASSWD_LEN)
195     {
196     printf("Password too big %s\n", pass);
197     exit(PARAMETER_PARSING_ERR_CODE);
198     }
199
200 return pass;
201 }
202 //-----------------------------------------------------------------------------
203 char * ParseUser(char * usr)
204 {
205 if (CheckLogin(usr))
206     {
207     printf("Incorresct user login %s\n", usr);
208     exit(PARAMETER_PARSING_ERR_CODE);
209     }
210 return usr;
211 }
212 //-----------------------------------------------------------------------------
213 void ConvertKOI8(const string & src, string * dst, int encType)
214 {
215 iconv_t cd;
216 char * ob = new char[src.size() * 2 + 1];
217 char * ib = new char[src.size() + 1];
218
219 strcpy(ib, src.c_str());
220
221 char * outbuf = ob;
222 char * inbuf = ib;
223
224 setlocale(LC_ALL, "");
225
226 char charsetF[100];
227 char charsetT[100];
228
229 if (encType == TO_KOI8)
230     {
231     strcpy(charsetF, nl_langinfo(CODESET));
232     strcpy(charsetT, "koi8-ru");
233     }
234 else
235     {
236     strcpy(charsetT, nl_langinfo(CODESET));
237     strcpy(charsetF, "koi8-ru");
238     }
239
240 size_t nconv = 1;
241
242 size_t insize = strlen(ib);
243 size_t outsize = insize * 2 + 1;
244
245 insize = src.size();
246
247 cd = iconv_open(charsetT, charsetF);
248 if (cd == (iconv_t) -1)
249     {
250     if (errno != EINVAL)
251         printf("error iconv_open\n");
252     else
253         {
254         printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
255         *dst = src;
256         return;
257         }
258
259     exit(ICONV_ERR_CODE);
260     }
261
262 #if defined(CONST_ICONV)
263 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
264 #else
265 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
266 #endif
267 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
268 //printf("ib=%s ob=%s\n", ib, ob);
269 //printf("nconv=%d outsize=%d\n", nconv, outsize);
270 if (nconv == (size_t) -1)
271     {
272     if (errno != EINVAL)
273         {
274         printf("iconv error\n");
275         exit(ICONV_ERR_CODE);
276         }
277     }
278
279 *outbuf = L'\0';
280
281 iconv_close(cd);
282 *dst = ob;
283
284 delete[] ob;
285 delete[] ib;
286 }
287 //-----------------------------------------------------------------------------
288 void ConvertFromKOI8(const string & src, string * dst)
289 {
290 ConvertKOI8(src, dst, FROM_KOI8);
291 }
292 //-----------------------------------------------------------------------------
293 void ConvertToKOI8(const string & src, string * dst)
294 {
295 ConvertKOI8(src, dst, TO_KOI8);
296 }
297 //-----------------------------------------------------------------------------
298 int RecvSetUserAnswer(const char * ans, void * d)
299 {
300 GetUserCbData * gucbd;
301 gucbd = (GetUserCbData *)d;
302
303 bool * result = gucbd->result;
304
305 //REQUEST * req = (REQUEST *)gucbd->data;
306
307 //printf("ans=%s\n", ans);
308 if (strcasecmp("Ok", ans) == 0)
309     *result = true;
310 else
311     *result = false;
312
313 return 0;
314 }
315 //-----------------------------------------------------------------------------
316 struct StringReqParams
317 {
318     string name;
319     RESETABLE<string> reqParam;
320     string * value;
321 };
322 //-----------------------------------------------------------------------------
323 void RecvUserData(USERDATA * ud, void * d)
324 {
325 GetUserCbData * gucbd;
326 gucbd = (GetUserCbData *)d;
327
328 bool * result = gucbd->result;
329
330 REQUEST * req = (REQUEST *)gucbd->data;
331
332 if (ud->login == "")
333     {
334     *result = false;
335     return;
336     }
337
338 if (!req->cash.empty())
339     cout << "cash=" << ud->cash << endl;
340
341 if (!req->credit.empty())
342     cout << "credit=" << ud->credit << endl;
343
344 if (!req->creditExpire.empty())
345     {
346     char buf[32];
347     struct tm brokenTime;
348     time_t tt = ud->creditExpire;
349
350     brokenTime.tm_wday = 0;
351     brokenTime.tm_yday = 0;
352     brokenTime.tm_isdst = 0;
353     brokenTime.tm_hour = 0;
354     brokenTime.tm_min = 0;
355     brokenTime.tm_sec = 0;
356
357     gmtime_r(&tt, &brokenTime);
358
359     strftime(buf, 32, "%Y-%m-%d", &brokenTime);
360
361     cout << "creditExpire=" << buf << endl;
362     }
363
364 if (!req->down.empty())
365     cout << "down=" << ud->down << endl;
366
367 if (!req->passive.empty())
368     cout << "passive=" << ud->passive << endl;
369
370 if (!req->disableDetailStat.empty())
371     cout << "disableDetailStat=" << ud->disableDetailStat << endl;
372
373 if (!req->alwaysOnline.empty())
374     cout << "alwaysOnline=" << ud->alwaysOnline << endl;
375
376 if (!req->prepaidTraff.empty())
377     cout << "prepaidTraff=" << ud->prepaidTraff << endl;
378
379 for (int i = 0; i < DIR_NUM; i++)
380     {
381     if (!req->u[i].empty())
382         cout << "u" << i << "=" << ud->stat.mu[i] << endl;
383     if (!req->d[i].empty())
384         cout << "d" << i << "=" << ud->stat.md[i] << endl;
385     }
386
387 for (int i = 0; i < USERDATA_NUM; i++)
388     {
389     if (!req->ud[i].empty())
390         {
391         string str;
392         ConvertFromKOI8(ud->userData[i], &str);
393         cout << "userdata" << i << "=" << str << endl;
394         }
395     }
396
397 StringReqParams strReqParams[] =
398 {
399     {"note",     req->note,        &ud->note},
400     {"name",     req->name,        &ud->name},
401     {"address",  req->address,     &ud->address},
402     {"email",    req->email,       &ud->email},
403     {"phone",    req->phone,       &ud->phone},
404     {"group",    req->group,       &ud->group},
405     {"tariff",   req->tariff,      &ud->tariff},
406     {"password", req->usrPasswd,   &ud->password},
407     {"ip",       req->ips,         &ud->ips}    // IP-address of user
408 };
409 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
410     {
411     if (!strReqParams[i].reqParam.empty())
412         {
413         string str;
414         ConvertFromKOI8(*strReqParams[i].value, &str);
415         cout << strReqParams[i].name << "=" << str << endl;
416         }
417     }
418 *result = true;
419 }
420 //-----------------------------------------------------------------------------
421 void RecvAuthByData(const std::vector<std::string> & list, void * d)
422 {
423 AuthByCbData * abcbd;
424 abcbd = (AuthByCbData *)d;
425
426 bool * result = abcbd->result;
427
428 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
429     cout << *it << "\n";
430 cout << endl;
431
432 *result = true;
433 }
434 //-----------------------------------------------------------------------------
435 int ProcessSetUser(const std::string &server,
436                    int port,
437                    const std::string &admLogin,
438                    const std::string &admPasswd,
439                    const std::string &str,
440                    void * data,
441                    bool isMessage)
442 {
443 SERVCONF sc;
444
445 bool result = false;
446
447
448 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
449 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
450 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
451 sc.SetAdmPassword(admPasswd.c_str());
452
453 // TODO Good variable name :)
454 GetUserCbData gucbd;
455
456 gucbd.data = data;
457 gucbd.result = &result;
458
459 if (isMessage)
460     {
461     sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
462     sc.MsgUser(str.c_str());
463     }
464 else
465     {
466     sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
467     sc.ChgUser(str.c_str());
468     }
469
470 if (result)
471     {
472     printf("Ok\n");
473     return 0;
474     }
475 else
476     {
477     printf("Error\n");
478     return -1;
479     }
480
481 return 0;
482 }
483 //-----------------------------------------------------------------------------
484 int ProcessGetUser(const std::string &server,
485                    int port,
486                    const std::string &admLogin,
487                    const std::string &admPasswd,
488                    const std::string &login,
489                    void * data)
490 {
491 SERVCONF sc;
492
493 bool result = false;
494
495 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
496 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
497 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
498 sc.SetAdmPassword(admPasswd.c_str());
499
500 // TODO Good variable name :)
501 GetUserCbData gucbd;
502
503 gucbd.data = data;
504 gucbd.result = &result;
505
506 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
507 sc.GetUser(login.c_str());
508
509 if (result)
510     {
511     printf("Ok\n");
512     return 0;
513     }
514 else
515     {
516     printf("Error\n");
517     return -1;
518     }
519
520 return 0;
521 }
522 //-----------------------------------------------------------------------------
523 int ProcessAuthBy(const std::string &server,
524                   int port,
525                   const std::string &admLogin,
526                   const std::string &admPasswd,
527                   const std::string &login,
528                   void * data)
529 {
530 SERVCONF sc;
531
532 bool result = false;
533
534 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
535 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
536 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
537 sc.SetAdmPassword(admPasswd.c_str());
538
539 // TODO Good variable name :)
540 AuthByCbData abcbd;
541
542 abcbd.data = data;
543 abcbd.result = &result;
544
545 sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
546 sc.GetUserAuthBy(login.c_str());
547
548 if (result)
549     {
550     printf("Ok\n");
551     return 0;
552     }
553 else
554     {
555     printf("Error\n");
556     return -1;
557     }
558
559 return 0;
560 }
561 //-----------------------------------------------------------------------------