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 "common_sg.h"
38 #include "version_sg.h"
40 #include "sg_error_codes.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 //---------------------------------------------------------------------------
63 //---------------------------------------------------------------------------
64 void Usage(int usageType)
66 printf("Sgconf version: %s\n\n", VERSION_SG);
69 if (usageType == usageConf)
70 strcpy(action, "set");
72 strcpy(action, "get");
74 printf("To add or to set cash use:\n");
75 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
76 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
77 printf("To get cash use:\n");
78 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
82 {"set tariff", "get tariff", "-t", "<tariff:now|delayed>"},
83 {"set credit", "get credit", "-r", "<credit>"},
84 {"set credit expire", "get credit expire", "-E", "<credit_expire_date>"},
85 {"set password", "get password", "-o", "<new_password>"},
86 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
87 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
88 {"set name", "get name", "-A", "<name>"},
89 {"set note", "get note", "-N", "<note>"},
90 {"set street address", "get street address", "-D", "<address>"},
91 {"set email", "get email", "-L", "<email>"},
92 {"set phone", "get phone", "-P", "<phone>"},
93 {"set group", "get group", "-G", "<group>"},
94 {"set/unset down", "get down", "-d", "<0/1>"},
95 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
96 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
97 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
100 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
102 printf("To %s use:\n", hp[i].setActionName.c_str());
103 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
104 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
105 printf("To %s use:\n", hp[i].getActionName.c_str());
106 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
107 hp[i].valueName.c_str());
110 printf("To set user\'s upload traffic value use:\n");
111 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
112 printf("To get user\'s upload traffic value use:\n");
113 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
115 printf("To set user\'s download traffic value use:\n");
116 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
117 printf("To get user\'s download traffic value use:\n");
118 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
120 printf("To set userdata<0...9> use:\n");
121 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
122 printf("To get userdata<0...9> use:\n");
123 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
125 printf("To send message use:\n");
126 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
128 printf("To create user use:\n");
129 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
131 printf("To delete user use:\n");
132 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
134 //---------------------------------------------------------------------------
139 //---------------------------------------------------------------------------
144 //---------------------------------------------------------------------------
145 int CheckLogin(const char * login)
147 for (int i = 0; i < (int)strlen(login); i++)
149 if (!(( login[i] >= 'a' && login[i] <= 'z')
150 || (login[i] >= 'A' && login[i] <= 'Z')
151 || (login[i] >= '0' && login[i] <= '9')
160 //-----------------------------------------------------------------------------
161 short int ParseServerPort(const char * p)
164 if (str2x(p, port) != 0)
166 printf("Incorresct server port %s\n", p);
167 exit(NETWORK_ERR_CODE);
171 //-----------------------------------------------------------------------------
172 char * ParseAdminLogin(char * adm)
176 printf("Incorresct admin login %s\n", adm);
177 exit(PARAMETER_PARSING_ERR_CODE);
181 //-----------------------------------------------------------------------------
182 char * ParsePassword(char * pass)
184 if (strlen(pass) >= ADM_PASSWD_LEN)
186 printf("Password too big %s\n", pass);
187 exit(PARAMETER_PARSING_ERR_CODE);
192 //-----------------------------------------------------------------------------
193 char * ParseUser(char * usr)
197 printf("Incorresct user login %s\n", usr);
198 exit(PARAMETER_PARSING_ERR_CODE);
202 //-----------------------------------------------------------------------------
203 void ConvertKOI8(const string & src, string * dst, int encType)
206 char * ob = new char[src.size() * 2 + 1];
207 char * ib = new char[src.size() + 1];
209 strcpy(ib, src.c_str());
214 setlocale(LC_ALL, "");
219 if (encType == TO_KOI8)
221 strcpy(charsetF, nl_langinfo(CODESET));
222 strcpy(charsetT, "koi8-ru");
226 strcpy(charsetT, nl_langinfo(CODESET));
227 strcpy(charsetF, "koi8-ru");
232 size_t insize = strlen(ib);
233 size_t outsize = insize * 2 + 1;
237 cd = iconv_open(charsetT, charsetF);
238 if (cd == (iconv_t) -1)
241 printf("error iconv_open\n");
244 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
249 exit(ICONV_ERR_CODE);
252 #if defined(FREE_BSD) || defined(FREE_BSD5)
253 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
255 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
257 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
258 //printf("ib=%s ob=%s\n", ib, ob);
259 //printf("nconv=%d outsize=%d\n", nconv, outsize);
260 if (nconv == (size_t) -1)
264 printf("iconv error\n");
265 exit(ICONV_ERR_CODE);
277 //-----------------------------------------------------------------------------
278 void ConvertFromKOI8(const string & src, string * dst)
280 ConvertKOI8(src, dst, FROM_KOI8);
282 //-----------------------------------------------------------------------------
283 void ConvertToKOI8(const string & src, string * dst)
285 ConvertKOI8(src, dst, TO_KOI8);
287 //-----------------------------------------------------------------------------
288 int RecvSetUserAnswer(const char * ans, void * d)
290 GetUserCbData * gucbd;
291 gucbd = (GetUserCbData *)d;
293 bool * result = gucbd->result;
295 //REQUEST * req = (REQUEST *)gucbd->data;
297 //printf("ans=%s\n", ans);
298 if (strcasecmp("Ok", ans) == 0)
305 //-----------------------------------------------------------------------------
306 struct StringReqParams
309 RESETABLE<string> reqParam;
312 //-----------------------------------------------------------------------------
313 void RecvUserData(USERDATA * ud, void * d)
315 GetUserCbData * gucbd;
316 gucbd = (GetUserCbData *)d;
318 bool * result = gucbd->result;
320 REQUEST * req = (REQUEST *)gucbd->data;
328 if (!req->cash.res_empty())
329 cout << "cash=" << ud->cash << endl;
331 if (!req->credit.res_empty())
332 cout << "credit=" << ud->credit << endl;
334 if (!req->creditExpire.res_empty())
337 struct tm brokenTime;
338 time_t tt = ud->creditExpire;
340 brokenTime.tm_wday = 0;
341 brokenTime.tm_yday = 0;
342 brokenTime.tm_isdst = 0;
343 brokenTime.tm_hour = 0;
344 brokenTime.tm_min = 0;
345 brokenTime.tm_sec = 0;
347 gmtime_r(&tt, &brokenTime);
349 strftime(buf, 32, "%Y-%m-%d", &brokenTime);
351 cout << "creditExpire=" << buf << endl;
354 if (!req->down.res_empty())
355 cout << "down=" << ud->down << endl;
357 if (!req->passive.res_empty())
358 cout << "passive=" << ud->passive << endl;
360 if (!req->disableDetailStat.res_empty())
361 cout << "disableDetailStat=" << ud->disableDetailStat << endl;
363 if (!req->alwaysOnline.res_empty())
364 cout << "alwaysOnline=" << ud->alwaysOnline << endl;
366 if (!req->prepaidTraff.res_empty())
367 cout << "prepaidTraff=" << ud->prepaidTraff << endl;
369 for (int i = 0; i < DIR_NUM; i++)
371 if (!req->u[i].res_empty())
372 cout << "u" << i << "=" << ud->stat.mu[i] << endl;
373 if (!req->d[i].res_empty())
374 cout << "d" << i << "=" << ud->stat.md[i] << endl;
377 for (int i = 0; i < USERDATA_NUM; i++)
379 if (!req->ud[i].res_empty())
382 ConvertFromKOI8(ud->userData[i], &str);
383 cout << "userdata" << i << "=" << str << endl;
387 StringReqParams strReqParams[] =
389 {"note", req->note, &ud->note},
390 {"name", req->name, &ud->name},
391 {"address", req->address, &ud->address},
392 {"email", req->email, &ud->email},
393 {"phone", req->phone, &ud->phone},
394 {"group", req->group, &ud->group},
395 {"tariff", req->tariff, &ud->tariff},
396 {"password", req->usrPasswd, &ud->password},
397 {"ip", req->ips, &ud->ips} // IP-address of user
399 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
401 if (!strReqParams[i].reqParam.res_empty())
404 ConvertFromKOI8(*strReqParams[i].value, &str);
405 cout << strReqParams[i].name << "=" << str << endl;
410 //-----------------------------------------------------------------------------
411 int ProcessSetUser(const std::string &server,
413 const std::string &admLogin,
414 const std::string &admPasswd,
415 const std::string &str,
424 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
425 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
426 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
427 sc.SetAdmPassword(admPasswd.c_str());
429 // TODO Good variable name :)
433 gucbd.result = &result;
437 sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
438 sc.MsgUser(str.c_str());
442 sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
443 sc.ChgUser(str.c_str());
459 //-----------------------------------------------------------------------------
460 int ProcessGetUser(const std::string &server,
462 const std::string &admLogin,
463 const std::string &admPasswd,
464 const std::string &login,
471 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
472 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
473 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
474 sc.SetAdmPassword(admPasswd.c_str());
476 // TODO Good variable name :)
480 gucbd.result = &result;
482 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
483 sc.GetUser(login.c_str());
498 //-----------------------------------------------------------------------------