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 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
61 //---------------------------------------------------------------------------
69 //---------------------------------------------------------------------------
70 void Usage(int usageType)
72 printf("Sgconf version: %s\n\n", VERSION_SG);
75 if (usageType == usageConf)
76 strcpy(action, "set");
78 strcpy(action, "get");
80 printf("To add or to set cash use:\n");
81 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
82 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
83 printf("To get cash use:\n");
84 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
88 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
89 {"set credit", "get credit", "-r", "<credit>"},
90 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
91 {"set password", "get password", "-o", "<new_password>"},
92 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
93 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
94 {"set name", "get name", "-A", "<name>"},
95 {"set note", "get note", "-N", "<note>"},
96 {"set street address", "get street address", "-D", "<address>"},
97 {"set email", "get email", "-L", "<email>"},
98 {"set phone", "get phone", "-P", "<phone>"},
99 {"set group", "get group", "-G", "<group>"},
100 {"set/unset down", "get down", "-d", "<0/1>"},
101 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
102 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
103 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
106 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
108 printf("To %s use:\n", hp[i].setActionName.c_str());
109 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
110 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
111 printf("To %s use:\n", hp[i].getActionName.c_str());
112 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
113 hp[i].valueName.c_str());
116 printf("To set user\'s upload traffic value use:\n");
117 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
118 printf("To get user\'s upload traffic value use:\n");
119 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
121 printf("To set user\'s download traffic value use:\n");
122 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
123 printf("To get user\'s download traffic value use:\n");
124 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
126 printf("To set userdata<0...9> use:\n");
127 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
128 printf("To get userdata<0...9> use:\n");
129 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
131 printf("To get user's authorizers list use:\n");
132 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n");
134 printf("To send message use:\n");
135 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
137 printf("To create user use:\n");
138 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
140 printf("To delete user use:\n");
141 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
143 //---------------------------------------------------------------------------
148 //---------------------------------------------------------------------------
153 //---------------------------------------------------------------------------
154 int CheckLogin(const char * login)
156 for (int i = 0; i < (int)strlen(login); i++)
158 if (!(( login[i] >= 'a' && login[i] <= 'z')
159 || (login[i] >= 'A' && login[i] <= 'Z')
160 || (login[i] >= '0' && login[i] <= '9')
170 //-----------------------------------------------------------------------------
171 short int ParseServerPort(const char * p)
174 if (str2x(p, port) != 0)
176 printf("Incorresct server port %s\n", p);
177 exit(NETWORK_ERR_CODE);
181 //-----------------------------------------------------------------------------
182 char * ParseAdminLogin(char * adm)
186 printf("Incorresct admin login %s\n", adm);
187 exit(PARAMETER_PARSING_ERR_CODE);
191 //-----------------------------------------------------------------------------
192 char * ParsePassword(char * pass)
194 if (strlen(pass) >= ADM_PASSWD_LEN)
196 printf("Password too big %s\n", pass);
197 exit(PARAMETER_PARSING_ERR_CODE);
202 //-----------------------------------------------------------------------------
203 char * ParseUser(char * usr)
207 printf("Incorresct user login %s\n", usr);
208 exit(PARAMETER_PARSING_ERR_CODE);
212 //-----------------------------------------------------------------------------
213 void ConvertKOI8(const string & src, string * dst, int encType)
216 char * ob = new char[src.size() * 2 + 1];
217 char * ib = new char[src.size() + 1];
219 strcpy(ib, src.c_str());
224 setlocale(LC_ALL, "");
229 if (encType == TO_KOI8)
231 strcpy(charsetF, nl_langinfo(CODESET));
232 strcpy(charsetT, "koi8-ru");
236 strcpy(charsetT, nl_langinfo(CODESET));
237 strcpy(charsetF, "koi8-ru");
242 size_t insize = strlen(ib);
243 size_t outsize = insize * 2 + 1;
247 cd = iconv_open(charsetT, charsetF);
248 if (cd == (iconv_t) -1)
251 printf("error iconv_open\n");
254 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
259 exit(ICONV_ERR_CODE);
262 #if defined(FREE_BSD) || defined(FREE_BSD5)
263 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
265 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
267 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
268 //printf("ib=%s ob=%s\n", ib, ob);
269 //printf("nconv=%d outsize=%d\n", nconv, outsize);
270 if (nconv == (size_t) -1)
274 printf("iconv error\n");
275 exit(ICONV_ERR_CODE);
287 //-----------------------------------------------------------------------------
288 void ConvertFromKOI8(const string & src, string * dst)
290 ConvertKOI8(src, dst, FROM_KOI8);
292 //-----------------------------------------------------------------------------
293 void ConvertToKOI8(const string & src, string * dst)
295 ConvertKOI8(src, dst, TO_KOI8);
297 //-----------------------------------------------------------------------------
298 int RecvSetUserAnswer(const char * ans, void * d)
300 GetUserCbData * gucbd;
301 gucbd = (GetUserCbData *)d;
303 bool * result = gucbd->result;
305 //REQUEST * req = (REQUEST *)gucbd->data;
307 //printf("ans=%s\n", ans);
308 if (strcasecmp("Ok", ans) == 0)
315 //-----------------------------------------------------------------------------
316 struct StringReqParams
319 RESETABLE<string> reqParam;
322 //-----------------------------------------------------------------------------
323 void RecvUserData(USERDATA * ud, void * d)
325 GetUserCbData * gucbd;
326 gucbd = (GetUserCbData *)d;
328 bool * result = gucbd->result;
330 REQUEST * req = (REQUEST *)gucbd->data;
338 if (!req->cash.res_empty())
339 cout << "cash=" << ud->cash << endl;
341 if (!req->credit.res_empty())
342 cout << "credit=" << ud->credit << endl;
344 if (!req->creditExpire.res_empty())
347 struct tm brokenTime;
348 time_t tt = ud->creditExpire;
350 brokenTime.tm_wday = 0;
351 brokenTime.tm_yday = 0;
352 brokenTime.tm_isdst = 0;
353 brokenTime.tm_hour = 0;
354 brokenTime.tm_min = 0;
355 brokenTime.tm_sec = 0;
357 gmtime_r(&tt, &brokenTime);
359 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
361 cout << "creditExpire=" << buf << endl;
364 if (!req->down.res_empty())
365 cout << "down=" << ud->down << endl;
367 if (!req->passive.res_empty())
368 cout << "passive=" << ud->passive << endl;
370 if (!req->disableDetailStat.res_empty())
371 cout << "disableDetailStat=" << ud->disableDetailStat << endl;
373 if (!req->alwaysOnline.res_empty())
374 cout << "alwaysOnline=" << ud->alwaysOnline << endl;
376 if (!req->prepaidTraff.res_empty())
377 cout << "prepaidTraff=" << ud->prepaidTraff << endl;
379 for (int i = 0; i < DIR_NUM; i++)
381 if (!req->u[i].res_empty())
382 cout << "u" << i << "=" << ud->stat.mu[i] << endl;
383 if (!req->d[i].res_empty())
384 cout << "d" << i << "=" << ud->stat.md[i] << endl;
387 for (int i = 0; i < USERDATA_NUM; i++)
389 if (!req->ud[i].res_empty())
392 ConvertFromKOI8(ud->userData[i], &str);
393 cout << "userdata" << i << "=" << str << endl;
397 StringReqParams strReqParams[] =
399 {"note", req->note, &ud->note},
400 {"name", req->name, &ud->name},
401 {"address", req->address, &ud->address},
402 {"email", req->email, &ud->email},
403 {"phone", req->phone, &ud->phone},
404 {"group", req->group, &ud->group},
405 {"tariff", req->tariff, &ud->tariff},
406 {"password", req->usrPasswd, &ud->password},
407 {"ip", req->ips, &ud->ips} // IP-address of user
409 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
411 if (!strReqParams[i].reqParam.res_empty())
414 ConvertFromKOI8(*strReqParams[i].value, &str);
415 cout << strReqParams[i].name << "=" << str << endl;
420 //-----------------------------------------------------------------------------
421 void RecvAuthByData(const std::vector<std::string> & list, void * d)
423 AuthByCbData * abcbd;
424 abcbd = (AuthByCbData *)d;
426 bool * result = abcbd->result;
428 REQUEST * req = (REQUEST *)abcbd->data;
430 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
436 //-----------------------------------------------------------------------------
437 int ProcessSetUser(const std::string &server,
439 const std::string &admLogin,
440 const std::string &admPasswd,
441 const std::string &str,
450 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
451 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
452 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
453 sc.SetAdmPassword(admPasswd.c_str());
455 // TODO Good variable name :)
459 gucbd.result = &result;
463 sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
464 sc.MsgUser(str.c_str());
468 sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
469 sc.ChgUser(str.c_str());
485 //-----------------------------------------------------------------------------
486 int ProcessGetUser(const std::string &server,
488 const std::string &admLogin,
489 const std::string &admPasswd,
490 const std::string &login,
497 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
498 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
499 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
500 sc.SetAdmPassword(admPasswd.c_str());
502 // TODO Good variable name :)
506 gucbd.result = &result;
508 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
509 sc.GetUser(login.c_str());
524 //-----------------------------------------------------------------------------
525 int ProcessAuthBy(const std::string &server,
527 const std::string &admLogin,
528 const std::string &admPasswd,
529 const std::string &login,
536 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
537 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
538 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
539 sc.SetAdmPassword(admPasswd.c_str());
541 // TODO Good variable name :)
545 abcbd.result = &result;
547 sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
548 sc.GetUserAuthBy(login.c_str());
563 //-----------------------------------------------------------------------------