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"
48 const int usageConf = 0;
49 const int usageInfo = 1;
51 const int TO_KOI8 = 0;
52 const int FROM_KOI8 = 1;
53 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
62 GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
67 //---------------------------------------------------------------------------
70 std::string setActionName;
71 std::string getActionName;
72 std::string valueName;
73 std::string valueParam;
75 //---------------------------------------------------------------------------
76 void Usage(int usageType)
78 printf("Sgconf version: %s\n\n", VERSION_SG);
81 if (usageType == usageConf)
82 strcpy(action, "set");
84 strcpy(action, "get");
86 printf("To add or to set cash use:\n");
87 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
88 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
89 printf("To get cash use:\n");
90 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
94 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
95 {"set credit", "get credit", "-r", "<credit>"},
96 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
97 {"set password", "get password", "-o", "<new_password>"},
98 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
99 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
100 {"set name", "get name", "-A", "<name>"},
101 {"set note", "get note", "-N", "<note>"},
102 {"set street address", "get street address", "-D", "<address>"},
103 {"set email", "get email", "-L", "<email>"},
104 {"set phone", "get phone", "-P", "<phone>"},
105 {"set group", "get group", "-G", "<group>"},
106 {"set/unset down", "get down", "-d", "<0/1>"},
107 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
108 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
109 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
112 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
114 printf("To %s use:\n", hp[i].setActionName.c_str());
115 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
116 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
117 printf("To %s use:\n", hp[i].getActionName.c_str());
118 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
119 hp[i].valueName.c_str());
122 printf("To set user\'s upload traffic value use:\n");
123 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
124 printf("To get user\'s upload traffic value use:\n");
125 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
127 printf("To set user\'s download traffic value use:\n");
128 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
129 printf("To get user\'s download traffic value use:\n");
130 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
132 printf("To set userdata<0...9> use:\n");
133 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
134 printf("To get userdata<0...9> use:\n");
135 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
137 printf("To get user's authorizers list use:\n");
138 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
140 printf("To send message use:\n");
141 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
143 printf("To create user use:\n");
144 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
146 printf("To delete user use:\n");
147 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
149 //---------------------------------------------------------------------------
154 //---------------------------------------------------------------------------
159 //---------------------------------------------------------------------------
160 int CheckLogin(const char * login)
162 for (int i = 0; i < (int)strlen(login); i++)
164 if (!(( login[i] >= 'a' && login[i] <= 'z')
165 || (login[i] >= 'A' && login[i] <= 'Z')
166 || (login[i] >= '0' && login[i] <= '9')
176 //-----------------------------------------------------------------------------
177 short int ParseServerPort(const char * p)
180 if (str2x(p, port) != 0)
182 printf("Incorrect server port %s\n", p);
183 exit(NETWORK_ERR_CODE);
187 //-----------------------------------------------------------------------------
188 char * ParseAdminLogin(char * adm)
192 printf("Incorrect admin login %s\n", adm);
193 exit(PARAMETER_PARSING_ERR_CODE);
197 //-----------------------------------------------------------------------------
198 char * ParsePassword(char * pass)
200 if (strlen(pass) >= ADM_PASSWD_LEN)
202 printf("Password too big %s\n", pass);
203 exit(PARAMETER_PARSING_ERR_CODE);
208 //-----------------------------------------------------------------------------
209 char * ParseUser(char * usr)
213 printf("Incorrect user login %s\n", usr);
214 exit(PARAMETER_PARSING_ERR_CODE);
218 //-----------------------------------------------------------------------------
219 void ConvertKOI8(const std::string & src, std::string * dst, int encType)
222 char * ob = new char[src.size() * 2 + 1];
223 char * ib = new char[src.size() + 1];
225 strcpy(ib, src.c_str());
230 setlocale(LC_ALL, "");
235 if (encType == TO_KOI8)
237 strcpy(charsetF, nl_langinfo(CODESET));
238 strcpy(charsetT, "koi8-ru");
242 strcpy(charsetT, nl_langinfo(CODESET));
243 strcpy(charsetF, "koi8-ru");
248 size_t insize = strlen(ib);
249 size_t outsize = insize * 2 + 1;
253 cd = iconv_open(charsetT, charsetF);
254 if (cd == (iconv_t) -1)
257 printf("error iconv_open\n");
260 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
265 exit(ICONV_ERR_CODE);
268 #if defined(FREE_BSD) || defined(FREE_BSD5)
269 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
271 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
273 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
274 //printf("ib=%s ob=%s\n", ib, ob);
275 //printf("nconv=%d outsize=%d\n", nconv, outsize);
276 if (nconv == (size_t) -1)
280 printf("iconv error\n");
281 exit(ICONV_ERR_CODE);
293 //-----------------------------------------------------------------------------
294 void ConvertFromKOI8(const std::string & src, std::string * dst)
296 ConvertKOI8(src, dst, FROM_KOI8);
298 //-----------------------------------------------------------------------------
299 void ResultCallback(bool result, const std::string & reason, void * d)
301 ResultData * data = static_cast<ResultData *>(d);
302 data->result = result;
303 data->reason = reason;
305 //-----------------------------------------------------------------------------
306 void RecvAuthByData(bool result, const std::string & reason,
307 const AUTH_BY::INFO & list, void * d)
309 ResultData * data = static_cast<ResultData *>(d);
310 data->result = result;
311 data->reason = reason;
316 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
317 std::cout << *it << "\n";
319 std::cout << std::endl;
321 //-----------------------------------------------------------------------------
322 struct StringReqParams
325 RESETABLE<std::string> reqParam;
326 const std::string * value;
328 //-----------------------------------------------------------------------------
329 void GetUserCallback(bool result, const std::string& reason, const GET_USER::INFO & info, void * d)
331 GetUserData * data = static_cast<GetUserData *>(d);
332 data->result = false;
333 data->reason = reason;
338 if (info.login == "")
340 data->result = false;
341 data->reason = "Invalid login.";
345 if (!data->request.cash.res_empty())
346 cout << "cash = " << info.cash << endl;
348 if (!data->request.credit.res_empty())
349 cout << "credit = " << info.credit << endl;
351 if (!data->request.creditExpire.res_empty())
354 struct tm brokenTime;
355 time_t tt = info.creditExpire;
357 brokenTime.tm_wday = 0;
358 brokenTime.tm_yday = 0;
359 brokenTime.tm_isdst = 0;
360 brokenTime.tm_hour = 0;
361 brokenTime.tm_min = 0;
362 brokenTime.tm_sec = 0;
364 gmtime_r(&tt, &brokenTime);
366 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
368 cout << "creditExpire = " << buf << endl;
371 if (!data->request.down.res_empty())
372 cout << "down = " << info.down << endl;
374 if (!data->request.passive.res_empty())
375 cout << "passive = " << info.passive << endl;
377 if (!data->request.disableDetailStat.res_empty())
378 cout << "disableDetailStat = " << info.disableDetailStat << endl;
380 if (!data->request.alwaysOnline.res_empty())
381 cout << "alwaysOnline = " << info.alwaysOnline << endl;
383 if (!data->request.prepaidTraff.res_empty())
384 cout << "prepaidTraff = " << info.prepaidTraff << endl;
386 for (int i = 0; i < DIR_NUM; i++)
388 if (!data->request.sessionUpload[i].res_empty())
389 cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
390 if (!data->request.sessionDownload[i].res_empty())
391 cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
394 for (int i = 0; i < DIR_NUM; i++)
396 if (!data->request.monthUpload[i].res_empty())
397 cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
398 if (!data->request.monthDownload[i].res_empty())
399 cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
402 for (int i = 0; i < USERDATA_NUM; i++)
404 if (!data->request.userData[i].res_empty())
407 ConvertFromKOI8(info.userData[i], &str);
408 cout << "user data " << i << " = " << str << endl;
412 StringReqParams strReqParams[] =
414 {"note", data->request.note, &info.note},
415 {"name", data->request.name, &info.name},
416 {"address", data->request.address, &info.address},
417 {"email", data->request.email, &info.email},
418 {"phone", data->request.phone, &info.phone},
419 {"group", data->request.group, &info.group},
420 {"tariff", data->request.tariff, &info.tariff},
421 {"password", data->request.usrPasswd, &info.password},
422 {"ip", data->request.ips, &info.ips} // IP-address of user
424 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
426 if (!strReqParams[i].reqParam.res_empty())
429 ConvertFromKOI8(*strReqParams[i].value, &str);
430 cout << strReqParams[i].name << " = " << str << endl;
435 //-----------------------------------------------------------------------------
436 bool ProcessSetUser(const std::string & server,
438 const std::string & login,
439 const std::string & password,
440 const std::string & str)
442 SERVCONF sc(server, port, login, password);
445 int res = sc.ChgUser(str.c_str(), ResultCallback, &data);
447 if (res == st_ok && data.result)
455 printf("%s\n", sc.GetStrError().c_str());
457 printf("%s\n", data.reason.c_str());
460 //-----------------------------------------------------------------------------
461 bool ProcessSendMessage(const std::string & server, uint16_t port,
462 const std::string & login, const std::string & password,
463 const std::string & requestString)
465 SERVCONF sc(server, port, login, password);
468 int res = sc.SendMessage(requestString.c_str(), ResultCallback, &data);
470 if (res == st_ok && data.result)
478 printf("%s\n", sc.GetStrError().c_str());
480 printf("%s\n", data.reason.c_str());
483 //-----------------------------------------------------------------------------
484 bool ProcessGetUser(const std::string &server,
486 const std::string &admLogin,
487 const std::string &admPasswd,
488 const std::string &login,
491 SERVCONF sc(server, port, admLogin, admPasswd);
493 GetUserData data(request, false);
494 bool res = (sc.GetUser(login.c_str(), GetUserCallback, &data) == st_ok);
496 if (res && data.result)
504 printf("%s\n", sc.GetStrError().c_str());
506 printf("%s\n", data.reason.c_str());
509 //-----------------------------------------------------------------------------
510 bool ProcessAuthBy(const std::string &server,
512 const std::string &admLogin,
513 const std::string &admPasswd,
514 const std::string &login)
516 SERVCONF sc(server, port, admLogin, admPasswd);
519 bool res = (sc.AuthBy(login.c_str(), RecvAuthByData, &data) == st_ok);
521 if (res && data.result)
529 printf("%s\n", sc.GetStrError().c_str());
531 printf("%s\n", data.reason.c_str());
534 //-----------------------------------------------------------------------------