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 //-----------------------------------------------------------------------------
54 GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
58 //---------------------------------------------------------------------------
61 std::string setActionName;
62 std::string getActionName;
63 std::string valueName;
64 std::string valueParam;
66 //---------------------------------------------------------------------------
67 void Usage(int usageType)
69 printf("Sgconf version: %s\n\n", VERSION_SG);
72 if (usageType == usageConf)
73 strcpy(action, "set");
75 strcpy(action, "get");
77 printf("To add or to set cash use:\n");
78 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
79 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
80 printf("To get cash use:\n");
81 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
85 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
86 {"set credit", "get credit", "-r", "<credit>"},
87 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
88 {"set password", "get password", "-o", "<new_password>"},
89 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
90 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
91 {"set name", "get name", "-A", "<name>"},
92 {"set note", "get note", "-N", "<note>"},
93 {"set street address", "get street address", "-D", "<address>"},
94 {"set email", "get email", "-L", "<email>"},
95 {"set phone", "get phone", "-P", "<phone>"},
96 {"set group", "get group", "-G", "<group>"},
97 {"set/unset down", "get down", "-d", "<0/1>"},
98 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
99 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
100 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
103 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
105 printf("To %s use:\n", hp[i].setActionName.c_str());
106 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
107 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
108 printf("To %s use:\n", hp[i].getActionName.c_str());
109 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
110 hp[i].valueName.c_str());
113 printf("To set user\'s upload traffic value use:\n");
114 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
115 printf("To get user\'s upload traffic value use:\n");
116 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
118 printf("To set user\'s download traffic value use:\n");
119 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
120 printf("To get user\'s download traffic value use:\n");
121 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
123 printf("To set userdata<0...9> use:\n");
124 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
125 printf("To get userdata<0...9> use:\n");
126 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
128 printf("To get user's authorizers list use:\n");
129 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
131 printf("To send message use:\n");
132 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
134 printf("To create user use:\n");
135 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
137 printf("To delete user use:\n");
138 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
140 //---------------------------------------------------------------------------
145 //---------------------------------------------------------------------------
150 //---------------------------------------------------------------------------
151 int CheckLogin(const char * login)
153 for (int i = 0; i < (int)strlen(login); i++)
155 if (!(( login[i] >= 'a' && login[i] <= 'z')
156 || (login[i] >= 'A' && login[i] <= 'Z')
157 || (login[i] >= '0' && login[i] <= '9')
167 //-----------------------------------------------------------------------------
168 short int ParseServerPort(const char * p)
171 if (str2x(p, port) != 0)
173 printf("Incorrect server port %s\n", p);
174 exit(NETWORK_ERR_CODE);
178 //-----------------------------------------------------------------------------
179 char * ParseAdminLogin(char * adm)
183 printf("Incorrect admin login %s\n", adm);
184 exit(PARAMETER_PARSING_ERR_CODE);
188 //-----------------------------------------------------------------------------
189 char * ParsePassword(char * pass)
191 if (strlen(pass) >= ADM_PASSWD_LEN)
193 printf("Password too big %s\n", pass);
194 exit(PARAMETER_PARSING_ERR_CODE);
199 //-----------------------------------------------------------------------------
200 char * ParseUser(char * usr)
204 printf("Incorrect user login %s\n", usr);
205 exit(PARAMETER_PARSING_ERR_CODE);
209 //-----------------------------------------------------------------------------
210 void ConvertKOI8(const std::string & src, std::string * dst, int encType)
213 char * ob = new char[src.size() * 2 + 1];
214 char * ib = new char[src.size() + 1];
216 strcpy(ib, src.c_str());
221 setlocale(LC_ALL, "");
226 if (encType == TO_KOI8)
228 strcpy(charsetF, nl_langinfo(CODESET));
229 strcpy(charsetT, "koi8-ru");
233 strcpy(charsetT, nl_langinfo(CODESET));
234 strcpy(charsetF, "koi8-ru");
239 size_t insize = strlen(ib);
240 size_t outsize = insize * 2 + 1;
244 cd = iconv_open(charsetT, charsetF);
245 if (cd == (iconv_t) -1)
248 printf("error iconv_open\n");
251 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
256 exit(ICONV_ERR_CODE);
259 #if defined(FREE_BSD) || defined(FREE_BSD5)
260 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
262 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
264 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
265 //printf("ib=%s ob=%s\n", ib, ob);
266 //printf("nconv=%d outsize=%d\n", nconv, outsize);
267 if (nconv == (size_t) -1)
271 printf("iconv error\n");
272 exit(ICONV_ERR_CODE);
284 //-----------------------------------------------------------------------------
285 void ConvertFromKOI8(const std::string & src, std::string * dst)
287 ConvertKOI8(src, dst, FROM_KOI8);
289 //-----------------------------------------------------------------------------
290 int RecvSetUserAnswer(const char * ans, void * d)
292 GetUserData * data = static_cast<GetUserData *>(d);
294 std::cout << ans << std::endl;
296 data->result = (strcasecmp("Ok", ans) == 0);
300 //-----------------------------------------------------------------------------
301 struct StringReqParams
304 RESETABLE<std::string> reqParam;
305 const std::string * value;
307 //-----------------------------------------------------------------------------
308 void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
310 GetUserData * data = static_cast<GetUserData *>(d);
312 if (info.login == "")
314 data->result = false;
318 if (!data->request.cash.res_empty())
319 cout << "cash = " << info.cash << endl;
321 if (!data->request.credit.res_empty())
322 cout << "credit = " << info.credit << endl;
324 if (!data->request.creditExpire.res_empty())
327 struct tm brokenTime;
328 time_t tt = info.creditExpire;
330 brokenTime.tm_wday = 0;
331 brokenTime.tm_yday = 0;
332 brokenTime.tm_isdst = 0;
333 brokenTime.tm_hour = 0;
334 brokenTime.tm_min = 0;
335 brokenTime.tm_sec = 0;
337 gmtime_r(&tt, &brokenTime);
339 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
341 cout << "creditExpire = " << buf << endl;
344 if (!data->request.down.res_empty())
345 cout << "down = " << info.down << endl;
347 if (!data->request.passive.res_empty())
348 cout << "passive = " << info.passive << endl;
350 if (!data->request.disableDetailStat.res_empty())
351 cout << "disableDetailStat = " << info.disableDetailStat << endl;
353 if (!data->request.alwaysOnline.res_empty())
354 cout << "alwaysOnline = " << info.alwaysOnline << endl;
356 if (!data->request.prepaidTraff.res_empty())
357 cout << "prepaidTraff = " << info.prepaidTraff << endl;
359 for (int i = 0; i < DIR_NUM; i++)
361 if (!data->request.sessionUpload[i].res_empty())
362 cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
363 if (!data->request.sessionDownload[i].res_empty())
364 cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
367 for (int i = 0; i < DIR_NUM; i++)
369 if (!data->request.monthUpload[i].res_empty())
370 cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
371 if (!data->request.monthDownload[i].res_empty())
372 cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
375 for (int i = 0; i < USERDATA_NUM; i++)
377 if (!data->request.userData[i].res_empty())
380 ConvertFromKOI8(info.userData[i], &str);
381 cout << "user data " << i << " = " << str << endl;
385 StringReqParams strReqParams[] =
387 {"note", data->request.note, &info.note},
388 {"name", data->request.name, &info.name},
389 {"address", data->request.address, &info.address},
390 {"email", data->request.email, &info.email},
391 {"phone", data->request.phone, &info.phone},
392 {"group", data->request.group, &info.group},
393 {"tariff", data->request.tariff, &info.tariff},
394 {"password", data->request.usrPasswd, &info.password},
395 {"ip", data->request.ips, &info.ips} // IP-address of user
397 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
399 if (!strReqParams[i].reqParam.res_empty())
402 ConvertFromKOI8(*strReqParams[i].value, &str);
403 cout << strReqParams[i].name << " = " << str << endl;
408 //-----------------------------------------------------------------------------
409 void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
411 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
415 //-----------------------------------------------------------------------------
416 int ProcessSetUser(const std::string &server,
418 const std::string &admLogin,
419 const std::string &admPasswd,
420 const std::string &str,
425 sc.SetServer(server.c_str());
427 sc.SetAdmLogin(admLogin.c_str());
428 sc.SetAdmPassword(admPasswd.c_str());
431 GetUserData cbdata(request, false);
436 sc.SetSendMessageCallback(RecvSetUserAnswer, &cbdata);
437 res = sc.SendMessage(str.c_str());
441 sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata);
442 res = sc.ChgUser(str.c_str());
445 if (res == st_ok && cbdata.result)
454 printf("%s\n", sc.GetStrError().c_str());
460 //-----------------------------------------------------------------------------
461 int ProcessGetUser(const std::string &server,
463 const std::string &admLogin,
464 const std::string &admPasswd,
465 const std::string &login,
470 sc.SetServer(server.c_str());
472 sc.SetAdmLogin(admLogin.c_str());
473 sc.SetAdmPassword(admPasswd.c_str());
475 GetUserData data(request, false);
477 sc.SetGetUserCallback(GetUserCallback, &data);
478 bool res = (sc.GetUser(login.c_str()) == st_ok);
480 if (res && data.result)
493 //-----------------------------------------------------------------------------
494 int ProcessAuthBy(const std::string &server,
496 const std::string &admLogin,
497 const std::string &admPasswd,
498 const std::string &login)
502 sc.SetServer(server.c_str());
504 sc.SetAdmLogin(admLogin.c_str());
505 sc.SetAdmPassword(admPasswd.c_str());
507 sc.SetAuthByCallback(RecvAuthByData, NULL);
508 bool res = (sc.AuthBy(login.c_str()) == st_ok);
519 //-----------------------------------------------------------------------------