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 ResultCallback(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 RecvAuthByData(bool result, const std::string & reason,
305 const PARSER_AUTH_BY::INFO & list, void * d)
307 ResultData * data = static_cast<ResultData *>(d);
308 data->result = result;
309 data->reason = reason;
314 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
315 std::cout << *it << "\n";
317 std::cout << std::endl;
319 //-----------------------------------------------------------------------------
320 struct StringReqParams
323 RESETABLE<std::string> reqParam;
324 const std::string * value;
326 //-----------------------------------------------------------------------------
327 void GetUserCallback(bool result, const std::string& reason, const PARSER_GET_USER::INFO & info, void * d)
329 GetUserData * data = static_cast<GetUserData *>(d);
330 data->result = false;
331 data->reason = reason;
336 if (info.login == "")
338 data->result = false;
339 data->reason = "Invalid login.";
343 if (!data->request.cash.res_empty())
344 cout << "cash = " << info.cash << endl;
346 if (!data->request.credit.res_empty())
347 cout << "credit = " << info.credit << endl;
349 if (!data->request.creditExpire.res_empty())
352 struct tm brokenTime;
353 time_t tt = info.creditExpire;
355 brokenTime.tm_wday = 0;
356 brokenTime.tm_yday = 0;
357 brokenTime.tm_isdst = 0;
358 brokenTime.tm_hour = 0;
359 brokenTime.tm_min = 0;
360 brokenTime.tm_sec = 0;
362 gmtime_r(&tt, &brokenTime);
364 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
366 cout << "creditExpire = " << buf << endl;
369 if (!data->request.down.res_empty())
370 cout << "down = " << info.down << endl;
372 if (!data->request.passive.res_empty())
373 cout << "passive = " << info.passive << endl;
375 if (!data->request.disableDetailStat.res_empty())
376 cout << "disableDetailStat = " << info.disableDetailStat << endl;
378 if (!data->request.alwaysOnline.res_empty())
379 cout << "alwaysOnline = " << info.alwaysOnline << endl;
381 if (!data->request.prepaidTraff.res_empty())
382 cout << "prepaidTraff = " << info.prepaidTraff << endl;
384 for (int i = 0; i < DIR_NUM; i++)
386 if (!data->request.sessionUpload[i].res_empty())
387 cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
388 if (!data->request.sessionDownload[i].res_empty())
389 cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
392 for (int i = 0; i < DIR_NUM; i++)
394 if (!data->request.monthUpload[i].res_empty())
395 cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
396 if (!data->request.monthDownload[i].res_empty())
397 cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
400 for (int i = 0; i < USERDATA_NUM; i++)
402 if (!data->request.userData[i].res_empty())
405 ConvertFromKOI8(info.userData[i], &str);
406 cout << "user data " << i << " = " << str << endl;
410 StringReqParams strReqParams[] =
412 {"note", data->request.note, &info.note},
413 {"name", data->request.name, &info.name},
414 {"address", data->request.address, &info.address},
415 {"email", data->request.email, &info.email},
416 {"phone", data->request.phone, &info.phone},
417 {"group", data->request.group, &info.group},
418 {"tariff", data->request.tariff, &info.tariff},
419 {"password", data->request.usrPasswd, &info.password},
420 {"ip", data->request.ips, &info.ips} // IP-address of user
422 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
424 if (!strReqParams[i].reqParam.res_empty())
427 ConvertFromKOI8(*strReqParams[i].value, &str);
428 cout << strReqParams[i].name << " = " << str << endl;
433 //-----------------------------------------------------------------------------
434 bool ProcessSetUser(const std::string & server,
436 const std::string & login,
437 const std::string & password,
438 const std::string & str)
440 SERVCONF sc(server, port, login, password);
443 int res = sc.ChgUser(str.c_str(), ResultCallback, &data);
445 if (res == st_ok && data.result)
453 printf("%s\n", sc.GetStrError().c_str());
455 printf("%s\n", data.reason.c_str());
458 //-----------------------------------------------------------------------------
459 bool ProcessSendMessage(const std::string & server, uint16_t port,
460 const std::string & login, const std::string & password,
461 const std::string & requestString)
463 SERVCONF sc(server, port, login, password);
466 int res = sc.SendMessage(requestString.c_str(), ResultCallback, &data);
468 if (res == st_ok && data.result)
476 printf("%s\n", sc.GetStrError().c_str());
478 printf("%s\n", data.reason.c_str());
481 //-----------------------------------------------------------------------------
482 bool ProcessGetUser(const std::string &server,
484 const std::string &admLogin,
485 const std::string &admPasswd,
486 const std::string &login,
489 SERVCONF sc(server, port, admLogin, admPasswd);
491 GetUserData data(request, false);
492 bool res = (sc.GetUser(login.c_str(), GetUserCallback, &data) == st_ok);
494 if (res && data.result)
502 printf("%s\n", sc.GetStrError().c_str());
504 printf("%s\n", data.reason.c_str());
507 //-----------------------------------------------------------------------------
508 bool ProcessAuthBy(const std::string &server,
510 const std::string &admLogin,
511 const std::string &admPasswd,
512 const std::string &login)
514 SERVCONF sc(server, port, admLogin, admPasswd);
517 bool res = (sc.AuthBy(login.c_str(), RecvAuthByData, &data) == st_ok);
519 if (res && data.result)
527 printf("%s\n", sc.GetStrError().c_str());
529 printf("%s\n", data.reason.c_str());
532 //-----------------------------------------------------------------------------