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 $
28 #include "sg_error_codes.h"
29 #include "common_sg.h"
30 #include "version_sg.h"
32 #include "stg/common.h"
46 const int usageConf = 0;
47 const int usageInfo = 1;
49 const int TO_KOI8 = 0;
50 const int FROM_KOI8 = 1;
51 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
60 GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
65 //---------------------------------------------------------------------------
68 std::string setActionName;
69 std::string getActionName;
70 std::string valueName;
71 std::string valueParam;
73 //---------------------------------------------------------------------------
74 void Usage(int usageType)
76 printf("Sgconf version: %s\n\n", VERSION_SG);
79 if (usageType == usageConf)
80 strcpy(action, "set");
82 strcpy(action, "get");
84 printf("To add or to set cash use:\n");
85 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
86 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
87 printf("To get cash use:\n");
88 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
92 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
93 {"set credit", "get credit", "-r", "<credit>"},
94 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
95 {"set password", "get password", "-o", "<new_password>"},
96 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
97 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
98 {"set name", "get name", "-A", "<name>"},
99 {"set note", "get note", "-N", "<note>"},
100 {"set street address", "get street address", "-D", "<address>"},
101 {"set email", "get email", "-L", "<email>"},
102 {"set phone", "get phone", "-P", "<phone>"},
103 {"set group", "get group", "-G", "<group>"},
104 {"set/unset down", "get down", "-d", "<0/1>"},
105 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
106 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
107 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
110 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
112 printf("To %s use:\n", hp[i].setActionName.c_str());
113 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
114 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
115 printf("To %s use:\n", hp[i].getActionName.c_str());
116 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
117 hp[i].valueName.c_str());
120 printf("To set user\'s upload traffic value use:\n");
121 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
122 printf("To get user\'s upload traffic value use:\n");
123 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
125 printf("To set user\'s download traffic value use:\n");
126 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
127 printf("To get user\'s download traffic value use:\n");
128 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
130 printf("To set userdata<0...9> use:\n");
131 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
132 printf("To get userdata<0...9> use:\n");
133 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
135 printf("To get user's authorizers list use:\n");
136 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
138 printf("To send message use:\n");
139 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
141 printf("To create user use:\n");
142 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
144 printf("To delete user use:\n");
145 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
147 //---------------------------------------------------------------------------
152 //---------------------------------------------------------------------------
157 //---------------------------------------------------------------------------
158 int CheckLogin(const char * login)
160 for (int i = 0; i < (int)strlen(login); i++)
162 if (!(( login[i] >= 'a' && login[i] <= 'z')
163 || (login[i] >= 'A' && login[i] <= 'Z')
164 || (login[i] >= '0' && login[i] <= '9')
174 //-----------------------------------------------------------------------------
175 short int ParseServerPort(const char * p)
178 if (str2x(p, port) != 0)
180 printf("Incorrect server port %s\n", p);
181 exit(NETWORK_ERR_CODE);
185 //-----------------------------------------------------------------------------
186 char * ParseAdminLogin(char * adm)
190 printf("Incorrect admin login %s\n", adm);
191 exit(PARAMETER_PARSING_ERR_CODE);
195 //-----------------------------------------------------------------------------
196 char * ParsePassword(char * pass)
198 if (strlen(pass) >= ADM_PASSWD_LEN)
200 printf("Password too big %s\n", pass);
201 exit(PARAMETER_PARSING_ERR_CODE);
206 //-----------------------------------------------------------------------------
207 char * ParseUser(char * usr)
211 printf("Incorrect user login %s\n", usr);
212 exit(PARAMETER_PARSING_ERR_CODE);
216 //-----------------------------------------------------------------------------
217 void ConvertKOI8(const std::string & src, std::string * dst, int encType)
220 char * ob = new char[src.size() * 2 + 1];
221 char * ib = new char[src.size() + 1];
223 strcpy(ib, src.c_str());
228 setlocale(LC_ALL, "");
233 if (encType == TO_KOI8)
235 strcpy(charsetF, nl_langinfo(CODESET));
236 strcpy(charsetT, "koi8-ru");
240 strcpy(charsetT, nl_langinfo(CODESET));
241 strcpy(charsetF, "koi8-ru");
246 size_t insize = strlen(ib);
247 size_t outsize = insize * 2 + 1;
251 cd = iconv_open(charsetT, charsetF);
252 if (cd == (iconv_t) -1)
255 printf("error iconv_open\n");
258 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
263 exit(ICONV_ERR_CODE);
266 #if defined(FREE_BSD) || defined(FREE_BSD5)
267 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
269 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
271 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
272 //printf("ib=%s ob=%s\n", ib, ob);
273 //printf("nconv=%d outsize=%d\n", nconv, outsize);
274 if (nconv == (size_t) -1)
278 printf("iconv error\n");
279 exit(ICONV_ERR_CODE);
291 //-----------------------------------------------------------------------------
292 void ConvertFromKOI8(const std::string & src, std::string * dst)
294 ConvertKOI8(src, dst, FROM_KOI8);
296 //-----------------------------------------------------------------------------
297 void SendMessageCallback(bool result, const std::string & reason, void * d)
299 ResultData * data = static_cast<ResultData *>(d);
300 data->result = result;
301 data->reason = reason;
303 //-----------------------------------------------------------------------------
304 void RecvSetUserAnswer(bool result, const std::string & reason, void * d)
306 ResultData * data = static_cast<ResultData *>(d);
307 data->result = result;
308 data->reason = reason;
310 //-----------------------------------------------------------------------------
311 void RecvAuthByData(bool result, const std::string & reason,
312 const PARSER_AUTH_BY::INFO & list, void * d)
314 ResultData * data = static_cast<ResultData *>(d);
315 data->result = result;
316 data->reason = reason;
321 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
322 std::cout << *it << "\n";
324 std::cout << std::endl;
326 //-----------------------------------------------------------------------------
327 struct StringReqParams
330 RESETABLE<std::string> reqParam;
331 const std::string * value;
333 //-----------------------------------------------------------------------------
334 void GetUserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * d)
336 GetUserData * data = static_cast<GetUserData *>(d);
337 data->result = false;
338 data->reason = reason;
343 if (info.login == "")
345 data->result = false;
346 data->reason = "Invalid login.";
350 if (!data->request.cash.res_empty())
351 cout << "cash = " << info.cash << endl;
353 if (!data->request.credit.res_empty())
354 cout << "credit = " << info.credit << endl;
356 if (!data->request.creditExpire.res_empty())
359 struct tm brokenTime;
360 time_t tt = info.creditExpire;
362 brokenTime.tm_wday = 0;
363 brokenTime.tm_yday = 0;
364 brokenTime.tm_isdst = 0;
365 brokenTime.tm_hour = 0;
366 brokenTime.tm_min = 0;
367 brokenTime.tm_sec = 0;
369 gmtime_r(&tt, &brokenTime);
371 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
373 cout << "creditExpire = " << buf << endl;
376 if (!data->request.down.res_empty())
377 cout << "down = " << info.down << endl;
379 if (!data->request.passive.res_empty())
380 cout << "passive = " << info.passive << endl;
382 if (!data->request.disableDetailStat.res_empty())
383 cout << "disableDetailStat = " << info.disableDetailStat << endl;
385 if (!data->request.alwaysOnline.res_empty())
386 cout << "alwaysOnline = " << info.alwaysOnline << endl;
388 if (!data->request.prepaidTraff.res_empty())
389 cout << "prepaidTraff = " << info.prepaidTraff << endl;
391 for (int i = 0; i < DIR_NUM; i++)
393 if (!data->request.sessionUpload[i].res_empty())
394 cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
395 if (!data->request.sessionDownload[i].res_empty())
396 cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
399 for (int i = 0; i < DIR_NUM; i++)
401 if (!data->request.monthUpload[i].res_empty())
402 cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
403 if (!data->request.monthDownload[i].res_empty())
404 cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
407 for (int i = 0; i < USERDATA_NUM; i++)
409 if (!data->request.userData[i].res_empty())
412 ConvertFromKOI8(info.userData[i], &str);
413 cout << "user data " << i << " = " << str << endl;
417 StringReqParams strReqParams[] =
419 {"note", data->request.note, &info.note},
420 {"name", data->request.name, &info.name},
421 {"address", data->request.address, &info.address},
422 {"email", data->request.email, &info.email},
423 {"phone", data->request.phone, &info.phone},
424 {"group", data->request.group, &info.group},
425 {"tariff", data->request.tariff, &info.tariff},
426 {"password", data->request.usrPasswd, &info.password},
427 {"ip", data->request.ips, &info.ips} // IP-address of user
429 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
431 if (!strReqParams[i].reqParam.res_empty())
434 ConvertFromKOI8(*strReqParams[i].value, &str);
435 cout << strReqParams[i].name << " = " << str << endl;
440 //-----------------------------------------------------------------------------
441 bool ProcessSetUser(const std::string & server,
443 const std::string & login,
444 const std::string & password,
445 const std::string & str)
447 SERVCONF sc(server, port, login, password);
450 sc.SetChgUserCallback(RecvSetUserAnswer, &data);
451 int res = sc.ChgUser(str.c_str());
453 if (res == st_ok && data.result)
461 printf("%s\n", sc.GetStrError().c_str());
463 printf("%s\n", data.reason.c_str());
466 //-----------------------------------------------------------------------------
467 bool ProcessSendMessage(const std::string & server, uint16_t port,
468 const std::string & login, const std::string & password,
469 const std::string & requestString)
471 SERVCONF sc(server, port, login, password);
474 sc.SetSendMessageCallback(SendMessageCallback, &data);
475 int res = sc.SendMessage(requestString.c_str());
477 if (res == st_ok && data.result)
485 printf("%s\n", sc.GetStrError().c_str());
487 printf("%s\n", data.reason.c_str());
490 //-----------------------------------------------------------------------------
491 bool ProcessGetUser(const std::string &server,
493 const std::string &admLogin,
494 const std::string &admPasswd,
495 const std::string &login,
498 SERVCONF sc(server, port, admLogin, admPasswd);
500 GetUserData data(request, false);
502 sc.SetGetUserCallback(GetUserCallback, &data);
503 bool res = (sc.GetUser(login.c_str()) == st_ok);
505 if (res && data.result)
513 printf("%s\n", sc.GetStrError().c_str());
515 printf("%s\n", data.reason.c_str());
518 //-----------------------------------------------------------------------------
519 bool ProcessAuthBy(const std::string &server,
521 const std::string &admLogin,
522 const std::string &admPasswd,
523 const std::string &login)
525 SERVCONF sc(server, port, admLogin, admPasswd);
528 sc.SetAuthByCallback(RecvAuthByData, &data);
529 bool res = (sc.AuthBy(login.c_str()) == st_ok);
531 if (res && data.result)
539 printf("%s\n", sc.GetStrError().c_str());
541 printf("%s\n", data.reason.c_str());
544 //-----------------------------------------------------------------------------