]> git.stg.codes - stg.git/blob - projects/sgconf/common_sg.cpp
Виправлено роботу з перекодуванням рядків
[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 "common_sg.h"
38 #include "version_sg.h"
39 #include "common.h"
40 #include "sg_error_codes.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 HelpParams
57 {
58     string setActionName;
59     string getActionName;
60     string valueName;
61     string valueParam;
62 };
63 //---------------------------------------------------------------------------
64 void Usage(int usageType)
65 {
66 printf("Sgconf version: %s\n\n", VERSION_SG);
67
68 char action[4];
69 if (usageType == usageConf)
70     strcpy(action, "set");
71 else
72     strcpy(action, "get");
73
74 printf("To add or to set cash use:\n");
75 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
76 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
77 printf("To get cash use:\n");
78 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
79
80 HelpParams hp[] =
81 {
82     {"set tariff",              "get tariff",           "-t",   "<tariff:now|delayed>"},
83     {"set credit",              "get credit",           "-r",   "<credit>"},
84     {"set password",            "get password",         "-o",   "<new_password>"},
85     {"set prepaid traffic",     "get prepaid traffic",  "-e",   "<prepaid>"},
86     {"set IP-addresses",        "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
87     {"set name",                "get name",             "-A",   "<name>"},
88     {"set note",                "get note",             "-N",   "<note>"},
89     {"set street address",      "get street address",   "-D",   "<address>"},
90     {"set email",               "get email",            "-L",   "<email>"},
91     {"set phone",               "get phone",            "-P",   "<phone>"},
92     {"set group",               "get group",            "-G",   "<group>"},
93     {"set/unset down",          "get down",             "-d",   "<0/1>"},
94     {"set/unset \'passive\'",   "get \'passive\'",      "-i",   "<0/1>"},
95     {"set/unset \'disableDetailStat\'",   "get \'disableDetailStat\'",      "--disable-stat",   "<0/1>"},
96     {"set/unset \'alwaysOnline\'",   "get \'alwaysOnline\'",      "--always-online",   "<0/1>"},
97 };
98
99 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
100     {
101     printf("To %s use:\n", hp[i].setActionName.c_str());
102     printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
103            hp[i].valueName.c_str(), hp[i].valueParam.c_str());
104     printf("To %s use:\n", hp[i].getActionName.c_str());
105     printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
106            hp[i].valueName.c_str());
107     }
108
109 printf("To set user\'s upload traffic value use:\n");
110 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
111 printf("To get user\'s upload traffic value use:\n");
112 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
113
114 printf("To set user\'s download traffic value use:\n");
115 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
116 printf("To get user\'s download traffic value use:\n");
117 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
118
119 printf("To set userdata<0...9> use:\n");
120 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
121 printf("To get userdata<0...9> use:\n");
122 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
123
124 printf("To send message use:\n");
125 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
126
127 printf("To create user use:\n");
128 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
129
130 printf("To delete user use:\n");
131 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
132 }
133 //---------------------------------------------------------------------------
134 void UsageConf()
135 {
136 Usage(usageConf);
137 }
138 //---------------------------------------------------------------------------
139 void UsageInfo()
140 {
141 Usage(usageInfo);
142 }
143 //---------------------------------------------------------------------------
144 int CheckLogin(const char * login)
145 {
146 for (int i = 0; i < (int)strlen(login); i++)
147     {
148     if (!(( login[i] >= 'a' && login[i] <= 'z')
149         || (login[i] >= 'A' && login[i] <= 'Z')
150         || (login[i] >= '0' && login[i] <= '9')
151         ||  login[i] == '_'
152         ||  login[i] == '-'))
153         {
154         return 1;
155         }
156     }
157 return 0;
158 }
159 //-----------------------------------------------------------------------------
160 short int ParseServerPort(const char * p)
161 {
162 int port;
163 if (str2x(p, port) != 0)
164     {
165     printf("Incorresct server port %s\n", p);
166     exit(NETWORK_ERR_CODE);
167     }
168 return (short)port;
169 }
170 //-----------------------------------------------------------------------------
171 char * ParseAdminLogin(char * adm)
172 {
173 if (CheckLogin(adm))
174     {
175     printf("Incorresct admin login %s\n", adm);
176     exit(PARAMETER_PARSING_ERR_CODE);
177     }
178 return adm;
179 }
180 //-----------------------------------------------------------------------------
181 char * ParsePassword(char * pass)
182 {
183 if (strlen(pass) >= ADM_PASSWD_LEN)
184     {
185     printf("Password too big %s\n", pass);
186     exit(PARAMETER_PARSING_ERR_CODE);
187     }
188
189 return pass;
190 }
191 //-----------------------------------------------------------------------------
192 char * ParseUser(char * usr)
193 {
194 if (CheckLogin(usr))
195     {
196     printf("Incorresct user login %s\n", usr);
197     exit(PARAMETER_PARSING_ERR_CODE);
198     }
199 return usr;
200 }
201 //-----------------------------------------------------------------------------
202 void ConvertKOI8(const string & src, string * dst, int encType)
203 {
204 iconv_t cd;
205 char * ob = new char[src.size() * 2 + 1];
206 char * ib = new char[src.size() + 1];
207
208 strcpy(ib, src.c_str());
209
210 char * outbuf = ob;
211 char * inbuf = ib;
212
213 setlocale(LC_ALL, "");
214
215 char charsetF[100];
216 char charsetT[100];
217
218 if (encType == TO_KOI8)
219     {
220     strcpy(charsetF, nl_langinfo(CODESET));
221     strcpy(charsetT, "koi8-ru");
222     }
223 else
224     {
225     strcpy(charsetT, nl_langinfo(CODESET));
226     strcpy(charsetF, "koi8-ru");
227     }
228
229 size_t nconv = 1;
230
231 size_t insize = strlen(ib);
232 size_t outsize = insize * 2 + 1;
233
234 insize = src.size();
235
236 cd = iconv_open(charsetT, charsetF);
237 if (cd == (iconv_t) -1)
238     {
239     if (errno != EINVAL)
240         printf("error iconv_open\n");
241     else
242         {
243         printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
244         *dst = src;
245         return;
246         }
247
248     exit(ICONV_ERR_CODE);
249     }
250
251 #if defined(FREE_BSD) || defined(FREE_BSD5)
252 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
253 #else
254 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
255 #endif
256 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
257 //printf("ib=%s ob=%s\n", ib, ob);
258 //printf("nconv=%d outsize=%d\n", nconv, outsize);
259 if (nconv == (size_t) -1)
260     {
261     if (errno != EINVAL)
262         {
263         printf("iconv error\n");
264         exit(ICONV_ERR_CODE);
265         }
266     }
267
268 *outbuf = L'\0';
269
270 iconv_close(cd);
271 *dst = ob;
272
273 delete[] ob;
274 delete[] ib;
275 }
276 //-----------------------------------------------------------------------------
277 void ConvertFromKOI8(const string & src, string * dst)
278 {
279 ConvertKOI8(src, dst, FROM_KOI8);
280 }
281 //-----------------------------------------------------------------------------
282 void ConvertToKOI8(const string & src, string * dst)
283 {
284 ConvertKOI8(src, dst, TO_KOI8);
285 }
286 //-----------------------------------------------------------------------------
287 int RecvSetUserAnswer(const char * ans, void * d)
288 {
289 GetUserCbData * gucbd;
290 gucbd = (GetUserCbData *)d;
291
292 bool * result = gucbd->result;
293
294 //REQUEST * req = (REQUEST *)gucbd->data;
295
296 //printf("ans=%s\n", ans);
297 if (strcasecmp("Ok", ans) == 0)
298     *result = true;
299 else
300     *result = false;
301
302 return 0;
303 }
304 //-----------------------------------------------------------------------------
305 struct StringReqParams
306 {
307     string name;
308     RESETABLE<string> reqParam;
309     string * value;
310 };
311 //-----------------------------------------------------------------------------
312 void RecvUserData(USERDATA * ud, void * d)
313 {
314 GetUserCbData * gucbd;
315 gucbd = (GetUserCbData *)d;
316
317 bool * result = gucbd->result;
318
319 REQUEST * req = (REQUEST *)gucbd->data;
320
321 if (ud->login == "")
322     {
323     *result = false;
324     return;
325     }
326
327 if (!req->cash.res_empty())
328     cout << "cash=" << ud->cash << endl;
329
330 if (!req->credit.res_empty())
331     cout << "credit=" << ud->credit << endl;
332
333 if (!req->down.res_empty())
334     cout << "down=" << ud->down << endl;
335
336 if (!req->passive.res_empty())
337     cout << "passive=" << ud->passive << endl;
338
339 if (!req->disableDetailStat.res_empty())
340     cout << "disableDetailStat=" << ud->disableDetailStat << endl;
341
342 if (!req->alwaysOnline.res_empty())
343     cout << "alwaysOnline=" << ud->alwaysOnline << endl;
344
345 if (!req->prepaidTraff.res_empty())
346     cout << "prepaidTraff=" << ud->prepaidTraff << endl;
347
348 for (int i = 0; i < DIR_NUM; i++)
349     {
350     if (!req->u[i].res_empty())
351         cout << "u" << i << "=" << ud->stat.mu[i] << endl;
352     if (!req->d[i].res_empty())
353         cout << "d" << i << "=" << ud->stat.md[i] << endl;
354     }
355
356 for (int i = 0; i < USERDATA_NUM; i++)
357     {
358     if (!req->ud[i].res_empty())
359         {
360         string str;
361         ConvertFromKOI8(ud->userData[i], &str);
362         cout << "userdata" << i << "=" << str << endl;
363         }
364     }
365
366 StringReqParams strReqParams[] =
367 {
368     {"note",     req->note,        &ud->note},
369     {"name",     req->name,        &ud->name},
370     {"address",  req->address,     &ud->address},
371     {"email",    req->email,       &ud->email},
372     {"phone",    req->phone,       &ud->phone},
373     {"group",    req->group,       &ud->group},
374     {"tariff",   req->tariff,      &ud->tariff},
375     {"password", req->usrPasswd,   &ud->password},
376     {"ip",       req->ips,         &ud->ips}    // IP-address of user
377 };
378 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
379     {
380     if (!strReqParams[i].reqParam.res_empty())
381         {
382         string str;
383         ConvertFromKOI8(*strReqParams[i].value, &str);
384         cout << strReqParams[i].name << "=" << str << endl;
385         }
386     }
387 *result = true;
388 }
389 //-----------------------------------------------------------------------------
390 int ProcessSetUser(const std::string &server,
391                    int port,
392                    const std::string &admLogin,
393                    const std::string &admPasswd,
394                    const std::string &str,
395                    void * data,
396                    bool isMessage)
397 {
398 SERVCONF sc;
399
400 bool result = false;
401
402
403 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
404 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
405 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
406 sc.SetAdmPassword(admPasswd.c_str());
407
408 // TODO Good variable name :)
409 GetUserCbData gucbd;
410
411 gucbd.data = data;
412 gucbd.result = &result;
413
414 if (isMessage)
415     {
416     sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
417     sc.MsgUser(str.c_str());
418     }
419 else
420     {
421     sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
422     sc.ChgUser(str.c_str());
423     }
424
425 if (result)
426     {
427     printf("Ok\n");
428     return 0;
429     }
430 else
431     {
432     printf("Error\n");
433     return -1;
434     }
435
436 return 0;
437 }
438 //-----------------------------------------------------------------------------
439 int ProcessGetUser(const std::string &server,
440                    int port,
441                    const std::string &admLogin,
442                    const std::string &admPasswd,
443                    const std::string &login,
444                    void * data)
445 {
446 SERVCONF sc;
447
448 bool result = false;
449
450 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
451 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
452 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
453 sc.SetAdmPassword(admPasswd.c_str());
454
455 // TODO Good variable name :)
456 GetUserCbData gucbd;
457
458 gucbd.data = data;
459 gucbd.result = &result;
460
461 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
462 sc.GetUser(login.c_str());
463
464 if (result)
465     {
466     printf("Ok\n");
467     return 0;
468     }
469 else
470     {
471     printf("Error\n");
472     return -1;
473     }
474
475 return 0;
476 }
477 //-----------------------------------------------------------------------------
478
479