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\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)
217 setlocale(LC_ALL, "");
222 if (encType == TO_KOI8)
224 strcpy(charsetF, nl_langinfo(CODESET));
225 strcpy(charsetT, "koi8-ru");
229 strcpy(charsetT, nl_langinfo(CODESET));
230 strcpy(charsetF, "koi8-ru");
235 cd = iconv_open(charsetT, charsetF);
236 if (cd == (iconv_t) -1)
239 printf("error iconv_open\n");
242 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
247 exit(ICONV_ERR_CODE);
250 char * ob = new char[src.size() * 2 + 1];
251 char * ib = new char[src.size() + 1];
253 strcpy(ib, src.c_str());
258 size_t insize = strlen(ib);
259 size_t outsize = insize * 2 + 1;
263 #if defined(CONST_ICONV)
264 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
266 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
268 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
269 //printf("ib=%s ob=%s\n", ib, ob);
270 //printf("nconv=%d outsize=%d\n", nconv, outsize);
271 if (nconv == (size_t) -1)
275 printf("iconv error\n");
276 exit(ICONV_ERR_CODE);
288 //-----------------------------------------------------------------------------
289 void ConvertFromKOI8(const string & src, string * dst)
291 ConvertKOI8(src, dst, FROM_KOI8);
293 //-----------------------------------------------------------------------------
294 void ConvertToKOI8(const string & src, string * dst)
296 ConvertKOI8(src, dst, TO_KOI8);
298 //-----------------------------------------------------------------------------
299 int RecvSetUserAnswer(const char * ans, void * d)
301 GetUserCbData * gucbd;
302 gucbd = (GetUserCbData *)d;
304 bool * result = gucbd->result;
306 //REQUEST * req = (REQUEST *)gucbd->data;
308 //printf("ans=%s\n", ans);
309 if (strcasecmp("Ok", ans) == 0)
316 //-----------------------------------------------------------------------------
317 struct StringReqParams
320 RESETABLE<string> reqParam;
323 //-----------------------------------------------------------------------------
324 void RecvUserData(USERDATA * ud, void * d)
326 GetUserCbData * gucbd;
327 gucbd = (GetUserCbData *)d;
329 bool * result = gucbd->result;
331 REQUEST * req = (REQUEST *)gucbd->data;
339 if (!req->cash.empty())
340 cout << "cash=" << ud->cash << endl;
342 if (!req->credit.empty())
343 cout << "credit=" << ud->credit << endl;
345 if (!req->creditExpire.empty())
348 struct tm brokenTime;
349 time_t tt = ud->creditExpire;
351 brokenTime.tm_wday = 0;
352 brokenTime.tm_yday = 0;
353 brokenTime.tm_isdst = 0;
354 brokenTime.tm_hour = 0;
355 brokenTime.tm_min = 0;
356 brokenTime.tm_sec = 0;
358 gmtime_r(&tt, &brokenTime);
360 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
362 cout << "creditExpire=" << buf << endl;
365 if (!req->down.empty())
366 cout << "down=" << ud->down << endl;
368 if (!req->passive.empty())
369 cout << "passive=" << ud->passive << endl;
371 if (!req->disableDetailStat.empty())
372 cout << "disableDetailStat=" << ud->disableDetailStat << endl;
374 if (!req->alwaysOnline.empty())
375 cout << "alwaysOnline=" << ud->alwaysOnline << endl;
377 if (!req->prepaidTraff.empty())
378 cout << "prepaidTraff=" << ud->prepaidTraff << endl;
380 for (int i = 0; i < DIR_NUM; i++)
382 if (!req->u[i].empty())
383 cout << "u" << i << "=" << ud->stat.mu[i] << endl;
384 if (!req->d[i].empty())
385 cout << "d" << i << "=" << ud->stat.md[i] << endl;
388 for (int i = 0; i < USERDATA_NUM; i++)
390 if (!req->ud[i].empty())
393 ConvertFromKOI8(ud->userData[i], &str);
394 cout << "userdata" << i << "=" << str << endl;
398 StringReqParams strReqParams[] =
400 {"note", req->note, &ud->note},
401 {"name", req->name, &ud->name},
402 {"address", req->address, &ud->address},
403 {"email", req->email, &ud->email},
404 {"phone", req->phone, &ud->phone},
405 {"group", req->group, &ud->group},
406 {"tariff", req->tariff, &ud->tariff},
407 {"password", req->usrPasswd, &ud->password},
408 {"ip", req->ips, &ud->ips} // IP-address of user
410 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
412 if (!strReqParams[i].reqParam.empty())
415 ConvertFromKOI8(*strReqParams[i].value, &str);
416 cout << strReqParams[i].name << "=" << str << endl;
421 //-----------------------------------------------------------------------------
422 void RecvAuthByData(const std::vector<std::string> & list, void * d)
424 AuthByCbData * abcbd;
425 abcbd = (AuthByCbData *)d;
427 bool * result = abcbd->result;
429 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
435 //-----------------------------------------------------------------------------
436 int ProcessSetUser(const std::string &server,
438 const std::string &admLogin,
439 const std::string &admPasswd,
440 const std::string &str,
449 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
450 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
451 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
452 sc.SetAdmPassword(admPasswd.c_str());
454 // TODO Good variable name :)
458 gucbd.result = &result;
462 sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
463 sc.MsgUser(str.c_str());
467 sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
468 sc.ChgUser(str.c_str());
484 //-----------------------------------------------------------------------------
485 int ProcessGetUser(const std::string &server,
487 const std::string &admLogin,
488 const std::string &admPasswd,
489 const std::string &login,
496 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
497 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
498 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
499 sc.SetAdmPassword(admPasswd.c_str());
501 // TODO Good variable name :)
505 gucbd.result = &result;
507 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
508 sc.GetUser(login.c_str());
523 //-----------------------------------------------------------------------------
524 int ProcessAuthBy(const std::string &server,
526 const std::string &admLogin,
527 const std::string &admPasswd,
528 const std::string &login,
535 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
536 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
537 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
538 sc.SetAdmPassword(admPasswd.c_str());
540 // TODO Good variable name :)
544 abcbd.result = &result;
546 sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
547 sc.GetUserAuthBy(login.c_str());
562 //-----------------------------------------------------------------------------