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