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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
24 $Date: 2009/06/08 10:02:28 $
37 #include "stg/common.h"
38 #include "sg_error_codes.h"
39 #include "common_sg.h"
40 #include "version_sg.h"
44 const int usageConf = 0;
45 const int usageInfo = 1;
47 const int TO_KOI8 = 0;
48 const int FROM_KOI8 = 1;
49 //-----------------------------------------------------------------------------
55 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
64 void Usage(int usageType)
66 printf("Sgconf version: %s\n\n", VERSION_SG);
69 if (usageType == usageConf)
70 strcpy(action, "set");
72 strcpy(action, "get");
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");
82 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
83 {"set credit", "get credit", "-r", "<credit>"},
84 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
85 {"set password", "get password", "-o", "<new_password>"},
86 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
87 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
88 {"set name", "get name", "-A", "<name>"},
89 {"set note", "get note", "-N", "<note>"},
90 {"set street address", "get street address", "-D", "<address>"},
91 {"set email", "get email", "-L", "<email>"},
92 {"set phone", "get phone", "-P", "<phone>"},
93 {"set group", "get group", "-G", "<group>"},
94 {"set/unset down", "get down", "-d", "<0/1>"},
95 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
96 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
97 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
100 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
102 printf("To %s use:\n", hp[i].setActionName.c_str());
103 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
104 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
105 printf("To %s use:\n", hp[i].getActionName.c_str());
106 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
107 hp[i].valueName.c_str());
110 printf("To set user\'s upload traffic value use:\n");
111 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
112 printf("To get user\'s upload traffic value use:\n");
113 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
115 printf("To set user\'s download traffic value use:\n");
116 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
117 printf("To get user\'s download traffic value use:\n");
118 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
120 printf("To set userdata<0...9> use:\n");
121 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
122 printf("To get userdata<0...9> use:\n");
123 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
125 printf("To get user's authorizers list use:\n");
126 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
128 printf("To send message use:\n");
129 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
131 printf("To create user use:\n");
132 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
134 printf("To delete user use:\n");
135 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
137 //---------------------------------------------------------------------------
142 //---------------------------------------------------------------------------
147 //---------------------------------------------------------------------------
148 int CheckLogin(const char * login)
150 for (int i = 0; i < (int)strlen(login); i++)
152 if (!(( login[i] >= 'a' && login[i] <= 'z')
153 || (login[i] >= 'A' && login[i] <= 'Z')
154 || (login[i] >= '0' && login[i] <= '9')
164 //-----------------------------------------------------------------------------
165 short int ParseServerPort(const char * p)
168 if (str2x(p, port) != 0)
170 printf("Incorresct server port %s\n", p);
171 exit(NETWORK_ERR_CODE);
175 //-----------------------------------------------------------------------------
176 char * ParseAdminLogin(char * adm)
180 printf("Incorresct admin login %s\n", adm);
181 exit(PARAMETER_PARSING_ERR_CODE);
185 //-----------------------------------------------------------------------------
186 char * ParsePassword(char * pass)
188 if (strlen(pass) >= ADM_PASSWD_LEN)
190 printf("Password too big %s\n", pass);
191 exit(PARAMETER_PARSING_ERR_CODE);
196 //-----------------------------------------------------------------------------
197 char * ParseUser(char * usr)
201 printf("Incorresct user login %s\n", usr);
202 exit(PARAMETER_PARSING_ERR_CODE);
206 //-----------------------------------------------------------------------------
207 void ConvertKOI8(const string & src, string * dst, int encType)
210 char * ob = new char[src.size() * 2 + 1];
211 char * ib = new char[src.size() + 1];
213 strcpy(ib, src.c_str());
218 setlocale(LC_ALL, "");
223 if (encType == TO_KOI8)
225 strcpy(charsetF, nl_langinfo(CODESET));
226 strcpy(charsetT, "koi8-ru");
230 strcpy(charsetT, nl_langinfo(CODESET));
231 strcpy(charsetF, "koi8-ru");
236 size_t insize = strlen(ib);
237 size_t outsize = insize * 2 + 1;
241 cd = iconv_open(charsetT, charsetF);
242 if (cd == (iconv_t) -1)
245 printf("error iconv_open\n");
248 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
253 exit(ICONV_ERR_CODE);
256 #if defined(FREE_BSD) || defined(FREE_BSD5)
257 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
259 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
261 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
262 //printf("ib=%s ob=%s\n", ib, ob);
263 //printf("nconv=%d outsize=%d\n", nconv, outsize);
264 if (nconv == (size_t) -1)
268 printf("iconv error\n");
269 exit(ICONV_ERR_CODE);
281 //-----------------------------------------------------------------------------
282 void ConvertFromKOI8(const string & src, string * dst)
284 ConvertKOI8(src, dst, FROM_KOI8);
286 //-----------------------------------------------------------------------------
287 void ConvertToKOI8(const string & src, string * dst)
289 ConvertKOI8(src, dst, TO_KOI8);
291 //-----------------------------------------------------------------------------
292 int RecvSetUserAnswer(const char * ans, void * d)
294 GetUserCbData * gucbd;
295 gucbd = (GetUserCbData *)d;
297 bool * result = gucbd->result;
299 //REQUEST * req = (REQUEST *)gucbd->data;
301 //printf("ans=%s\n", ans);
302 if (strcasecmp("Ok", ans) == 0)
309 //-----------------------------------------------------------------------------
310 struct StringReqParams
313 RESETABLE<string> reqParam;
316 //-----------------------------------------------------------------------------
317 void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
319 GetUserData * data = static_cast<GetUserData *>(d);
321 REQUEST * req = (REQUEST *)gucbd->data;
323 if (info.login == "")
325 data->result = false;
329 if (!data->request.cash.res_empty())
330 cout << "cash=" << info.cash << endl;
332 if (!data->requst.credit.res_empty())
333 cout << "credit=" << info.credit << endl;
335 if (!data->requst.creditExpire.res_empty())
338 struct tm brokenTime;
339 time_t tt = info.creditExpire;
341 brokenTime.tm_wday = 0;
342 brokenTime.tm_yday = 0;
343 brokenTime.tm_isdst = 0;
344 brokenTime.tm_hour = 0;
345 brokenTime.tm_min = 0;
346 brokenTime.tm_sec = 0;
348 gmtime_r(&tt, &brokenTime);
350 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
352 cout << "creditExpire=" << buf << endl;
355 if (!data->requst.down.res_empty())
356 cout << "down=" << info.down << endl;
358 if (!data->requst.passive.res_empty())
359 cout << "passive=" << info.passive << endl;
361 if (!data->requst.disableDetailStat.res_empty())
362 cout << "disableDetailStat=" << info.disableDetailStat << endl;
364 if (!data->requst.alwaysOnline.res_empty())
365 cout << "alwaysOnline=" << info.alwaysOnline << endl;
367 if (!data->requst.prepaidTraff.res_empty())
368 cout << "prepaidTraff=" << info.prepaidTraff << endl;
370 for (int i = 0; i < DIR_NUM; i++)
372 if (!data->requst.up[i].res_empty())
373 cout << "session upload for dir" << i << "=" << info.stat.su[i] << endl;
374 if (!data->requst.down[i].res_empty())
375 cout << "session download for dir" << i << "=" << info.stat.sd[i] << endl;
378 for (int i = 0; i < DIR_NUM; i++)
380 if (!data->requst.monthUp[i].res_empty())
381 cout << "month upload for dir" << i << "=" << info.stat.mu[i] << endl;
382 if (!data->requst.monthDown[i].res_empty())
383 cout << "month download for dir" << i << "=" << info.stat.md[i] << endl;
386 for (int i = 0; i < USERDATA_NUM; i++)
388 if (!data->requst.ud[i].res_empty())
391 ConvertFromKOI8(info.userData[i], &str);
392 cout << "userdata" << i << "=" << str << endl;
396 StringReqParams strReqParams[] =
398 {"note", data->requst.note, &info.note},
399 {"name", data->requst.name, &info.name},
400 {"address", data->requst.address, &info.address},
401 {"email", data->requst.email, &info.email},
402 {"phone", data->requst.phone, &info.phone},
403 {"group", data->requst.group, &info.group},
404 {"tariff", data->requst.tariff, &info.tariff},
405 {"password", data->requst.usrPasswd, &info.password},
406 {"ip", data->requst.ips, &info.ips} // IP-address of user
408 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
410 if (!strReqParams[i].reqParam.res_empty())
413 ConvertFromKOI8(*strReqParams[i].value, &str);
414 cout << strReqParams[i].name << "=" << str << endl;
419 //-----------------------------------------------------------------------------
420 void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
422 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
426 //-----------------------------------------------------------------------------
427 int ProcessSetUser(const std::string &server,
429 const std::string &admLogin,
430 const std::string &admPasswd,
431 const std::string &str,
440 sc.SetServer(server.c_str());
442 sc.SetAdmLogin(admLogin.c_str());
443 sc.SetAdmPassword(admPasswd.c_str());
445 // TODO Good variable name :)
449 gucbd.result = &result;
453 sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
454 sc.MsgUser(str.c_str());
458 sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
459 sc.ChgUser(str.c_str());
475 //-----------------------------------------------------------------------------
476 int ProcessGetUser(const std::string &server,
478 const std::string &admLogin,
479 const std::string &admPasswd,
480 const std::string &login,
487 sc.SetServer(server.c_str());
489 sc.SetAdmLogin(admLogin.c_str());
490 sc.SetAdmPassword(admPasswd.c_str());
492 // TODO Good variable name :)
495 gucbd.data = &request;
496 gucbd.result = &result;
498 sc.SetGetUserCallback(GetUserCallback, &gucbd);
499 sc.GetUser(login.c_str());
514 //-----------------------------------------------------------------------------
515 int ProcessAuthBy(const std::string &server,
517 const std::string &admLogin,
518 const std::string &admPasswd,
519 const std::string &login,
524 sc.SetServer(server.c_str());
526 sc.SetAdmLogin(admLogin.c_str());
527 sc.SetAdmPassword(admPasswd.c_str());
529 sc.SetAuthByCallback(RecvAuthByData, NULL);
530 sc.AuthBy(login.c_str());
541 //-----------------------------------------------------------------------------