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 //-----------------------------------------------------------------------------
52 GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
56 //---------------------------------------------------------------------------
64 //---------------------------------------------------------------------------
65 void Usage(int usageType)
67 printf("Sgconf version: %s\n\n", VERSION_SG);
70 if (usageType == usageConf)
71 strcpy(action, "set");
73 strcpy(action, "get");
75 printf("To add or to set cash use:\n");
76 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
77 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
78 printf("To get cash use:\n");
79 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
83 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
84 {"set credit", "get credit", "-r", "<credit>"},
85 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
86 {"set password", "get password", "-o", "<new_password>"},
87 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
88 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
89 {"set name", "get name", "-A", "<name>"},
90 {"set note", "get note", "-N", "<note>"},
91 {"set street address", "get street address", "-D", "<address>"},
92 {"set email", "get email", "-L", "<email>"},
93 {"set phone", "get phone", "-P", "<phone>"},
94 {"set group", "get group", "-G", "<group>"},
95 {"set/unset down", "get down", "-d", "<0/1>"},
96 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
97 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
98 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
101 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
103 printf("To %s use:\n", hp[i].setActionName.c_str());
104 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
105 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
106 printf("To %s use:\n", hp[i].getActionName.c_str());
107 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
108 hp[i].valueName.c_str());
111 printf("To set user\'s upload traffic value use:\n");
112 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
113 printf("To get user\'s upload traffic value use:\n");
114 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
116 printf("To set user\'s download traffic value use:\n");
117 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
118 printf("To get user\'s download traffic value use:\n");
119 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
121 printf("To set userdata<0...9> use:\n");
122 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
123 printf("To get userdata<0...9> use:\n");
124 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
126 printf("To get user's authorizers list use:\n");
127 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
129 printf("To send message use:\n");
130 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
132 printf("To create user use:\n");
133 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
135 printf("To delete user use:\n");
136 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
138 //---------------------------------------------------------------------------
143 //---------------------------------------------------------------------------
148 //---------------------------------------------------------------------------
149 int CheckLogin(const char * login)
151 for (int i = 0; i < (int)strlen(login); i++)
153 if (!(( login[i] >= 'a' && login[i] <= 'z')
154 || (login[i] >= 'A' && login[i] <= 'Z')
155 || (login[i] >= '0' && login[i] <= '9')
165 //-----------------------------------------------------------------------------
166 short int ParseServerPort(const char * p)
169 if (str2x(p, port) != 0)
171 printf("Incorresct server port %s\n", p);
172 exit(NETWORK_ERR_CODE);
176 //-----------------------------------------------------------------------------
177 char * ParseAdminLogin(char * adm)
181 printf("Incorresct admin login %s\n", adm);
182 exit(PARAMETER_PARSING_ERR_CODE);
186 //-----------------------------------------------------------------------------
187 char * ParsePassword(char * pass)
189 if (strlen(pass) >= ADM_PASSWD_LEN)
191 printf("Password too big %s\n", pass);
192 exit(PARAMETER_PARSING_ERR_CODE);
197 //-----------------------------------------------------------------------------
198 char * ParseUser(char * usr)
202 printf("Incorresct user login %s\n", usr);
203 exit(PARAMETER_PARSING_ERR_CODE);
207 //-----------------------------------------------------------------------------
208 void ConvertKOI8(const string & src, string * dst, int encType)
211 char * ob = new char[src.size() * 2 + 1];
212 char * ib = new char[src.size() + 1];
214 strcpy(ib, src.c_str());
219 setlocale(LC_ALL, "");
224 if (encType == TO_KOI8)
226 strcpy(charsetF, nl_langinfo(CODESET));
227 strcpy(charsetT, "koi8-ru");
231 strcpy(charsetT, nl_langinfo(CODESET));
232 strcpy(charsetF, "koi8-ru");
237 size_t insize = strlen(ib);
238 size_t outsize = insize * 2 + 1;
242 cd = iconv_open(charsetT, charsetF);
243 if (cd == (iconv_t) -1)
246 printf("error iconv_open\n");
249 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
254 exit(ICONV_ERR_CODE);
257 #if defined(FREE_BSD) || defined(FREE_BSD5)
258 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
260 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
262 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
263 //printf("ib=%s ob=%s\n", ib, ob);
264 //printf("nconv=%d outsize=%d\n", nconv, outsize);
265 if (nconv == (size_t) -1)
269 printf("iconv error\n");
270 exit(ICONV_ERR_CODE);
282 //-----------------------------------------------------------------------------
283 void ConvertFromKOI8(const string & src, string * dst)
285 ConvertKOI8(src, dst, FROM_KOI8);
287 //-----------------------------------------------------------------------------
288 void ConvertToKOI8(const string & src, string * dst)
290 ConvertKOI8(src, dst, TO_KOI8);
292 //-----------------------------------------------------------------------------
293 int RecvSetUserAnswer(const char * ans, void * d)
295 GetUserData * data = static_cast<GetUserData *>(d);
297 data->result = (strcasecmp("Ok", ans) == 0);
301 //-----------------------------------------------------------------------------
302 struct StringReqParams
305 RESETABLE<string> reqParam;
306 const string * value;
308 //-----------------------------------------------------------------------------
309 void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
311 GetUserData * data = static_cast<GetUserData *>(d);
313 if (info.login == "")
315 data->result = false;
319 if (!data->request.cash.res_empty())
320 cout << "cash = " << info.cash << endl;
322 if (!data->request.credit.res_empty())
323 cout << "credit = " << info.credit << endl;
325 if (!data->request.creditExpire.res_empty())
328 struct tm brokenTime;
329 time_t tt = info.creditExpire;
331 brokenTime.tm_wday = 0;
332 brokenTime.tm_yday = 0;
333 brokenTime.tm_isdst = 0;
334 brokenTime.tm_hour = 0;
335 brokenTime.tm_min = 0;
336 brokenTime.tm_sec = 0;
338 gmtime_r(&tt, &brokenTime);
340 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
342 cout << "creditExpire = " << buf << endl;
345 if (!data->request.down.res_empty())
346 cout << "down = " << info.down << endl;
348 if (!data->request.passive.res_empty())
349 cout << "passive = " << info.passive << endl;
351 if (!data->request.disableDetailStat.res_empty())
352 cout << "disableDetailStat = " << info.disableDetailStat << endl;
354 if (!data->request.alwaysOnline.res_empty())
355 cout << "alwaysOnline = " << info.alwaysOnline << endl;
357 if (!data->request.prepaidTraff.res_empty())
358 cout << "prepaidTraff = " << info.prepaidTraff << endl;
360 for (int i = 0; i < DIR_NUM; i++)
362 if (!data->request.sessionUpload[i].res_empty())
363 cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
364 if (!data->request.sessionDownload[i].res_empty())
365 cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
368 for (int i = 0; i < DIR_NUM; i++)
370 if (!data->request.monthUpload[i].res_empty())
371 cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
372 if (!data->request.monthDownload[i].res_empty())
373 cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
376 for (int i = 0; i < USERDATA_NUM; i++)
378 if (!data->request.userData[i].res_empty())
381 ConvertFromKOI8(info.userData[i], &str);
382 cout << "user data " << i << " = " << str << endl;
386 StringReqParams strReqParams[] =
388 {"note", data->request.note, &info.note},
389 {"name", data->request.name, &info.name},
390 {"address", data->request.address, &info.address},
391 {"email", data->request.email, &info.email},
392 {"phone", data->request.phone, &info.phone},
393 {"group", data->request.group, &info.group},
394 {"tariff", data->request.tariff, &info.tariff},
395 {"password", data->request.usrPasswd, &info.password},
396 {"ip", data->request.ips, &info.ips} // IP-address of user
398 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
400 if (!strReqParams[i].reqParam.res_empty())
403 ConvertFromKOI8(*strReqParams[i].value, &str);
404 cout << strReqParams[i].name << " = " << str << endl;
409 //-----------------------------------------------------------------------------
410 void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
412 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
416 //-----------------------------------------------------------------------------
417 int ProcessSetUser(const std::string &server,
419 const std::string &admLogin,
420 const std::string &admPasswd,
421 const std::string &str,
426 sc.SetServer(server.c_str());
428 sc.SetAdmLogin(admLogin.c_str());
429 sc.SetAdmPassword(admPasswd.c_str());
432 GetUserData cbdata(request, false);
437 sc.SetSendMessageCb(RecvSetUserAnswer, &cbdata);
438 res = sc.MsgUser(str.c_str());
442 sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata);
443 res = sc.ChgUser(str.c_str());
446 if (res && cbdata.result)
459 //-----------------------------------------------------------------------------
460 int ProcessGetUser(const std::string &server,
462 const std::string &admLogin,
463 const std::string &admPasswd,
464 const std::string &login,
469 sc.SetServer(server.c_str());
471 sc.SetAdmLogin(admLogin.c_str());
472 sc.SetAdmPassword(admPasswd.c_str());
474 // TODO Good variable name :)
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 //-----------------------------------------------------------------------------