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 password", "get password", "-o", "<new_password>"},
85 {"set prepaid traffic", "get prepaid traffic", "-e", "<prepaid>"},
86 {"set IP-addresses", "get IP-addresses", "-I", "<*|ip_addr[,ip_addr...]>"},
87 {"set name", "get name", "-A", "<name>"},
88 {"set note", "get note", "-N", "<note>"},
89 {"set street address", "get street address", "-D", "<address>"},
90 {"set email", "get email", "-L", "<email>"},
91 {"set phone", "get phone", "-P", "<phone>"},
92 {"set group", "get group", "-G", "<group>"},
93 {"set/unset down", "get down", "-d", "<0/1>"},
94 {"set/unset \'passive\'", "get \'passive\'", "-i", "<0/1>"},
95 {"set/unset \'disableDetailStat\'", "get \'disableDetailStat\'", "--disable-stat", "<0/1>"},
96 {"set/unset \'alwaysOnline\'", "get \'alwaysOnline\'", "--always-online", "<0/1>"},
99 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
101 printf("To %s use:\n", hp[i].setActionName.c_str());
102 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
103 hp[i].valueName.c_str(), hp[i].valueParam.c_str());
104 printf("To %s use:\n", hp[i].getActionName.c_str());
105 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
106 hp[i].valueName.c_str());
109 printf("To set user\'s upload traffic value use:\n");
110 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
111 printf("To get user\'s upload traffic value use:\n");
112 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
114 printf("To set user\'s download traffic value use:\n");
115 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
116 printf("To get user\'s download traffic value use:\n");
117 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
119 printf("To set userdata<0...9> use:\n");
120 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
121 printf("To get userdata<0...9> use:\n");
122 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
124 printf("To send message use:\n");
125 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
127 printf("To create user use:\n");
128 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
130 printf("To delete user use:\n");
131 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
133 //---------------------------------------------------------------------------
138 //---------------------------------------------------------------------------
143 //---------------------------------------------------------------------------
144 int CheckLogin(const char * login)
146 for (int i = 0; i < (int)strlen(login); i++)
148 if (!(( login[i] >= 'a' && login[i] <= 'z')
149 || (login[i] >= 'A' && login[i] <= 'Z')
150 || (login[i] >= '0' && login[i] <= '9')
159 //-----------------------------------------------------------------------------
160 short int ParseServerPort(const char * p)
163 if (str2x(p, port) != 0)
165 printf("Incorresct server port %s\n", p);
166 exit(NETWORK_ERR_CODE);
170 //-----------------------------------------------------------------------------
171 char * ParseAdminLogin(char * adm)
175 printf("Incorresct admin login %s\n", adm);
176 exit(PARAMETER_PARSING_ERR_CODE);
180 //-----------------------------------------------------------------------------
181 char * ParsePassword(char * pass)
183 if (strlen(pass) >= ADM_PASSWD_LEN)
185 printf("Password too big %s\n", pass);
186 exit(PARAMETER_PARSING_ERR_CODE);
191 //-----------------------------------------------------------------------------
192 char * ParseUser(char * usr)
196 printf("Incorresct user login %s\n", usr);
197 exit(PARAMETER_PARSING_ERR_CODE);
201 //-----------------------------------------------------------------------------
202 void ConvertKOI8(const string & src, string * dst, int encType)
205 char * ob = new char[src.size() * 2 + 1];
206 char * ib = new char[src.size() + 1];
208 strcpy(ib, src.c_str());
213 setlocale(LC_ALL, "");
218 if (encType == TO_KOI8)
220 strcpy(charsetF, nl_langinfo(CODESET));
221 strcpy(charsetT, "koi8-ru");
225 strcpy(charsetT, nl_langinfo(CODESET));
226 strcpy(charsetF, "koi8-ru");
231 size_t insize = strlen(ib);
232 size_t outsize = insize * 2 + 1;
236 cd = iconv_open(charsetT, charsetF);
237 if (cd == (iconv_t) -1)
240 printf("error iconv_open\n");
243 printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
248 exit(ICONV_ERR_CODE);
251 #if defined(FREE_BSD) || defined(FREE_BSD5)
252 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
254 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
256 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
257 //printf("ib=%s ob=%s\n", ib, ob);
258 //printf("nconv=%d outsize=%d\n", nconv, outsize);
259 if (nconv == (size_t) -1)
263 printf("iconv error\n");
264 exit(ICONV_ERR_CODE);
276 //-----------------------------------------------------------------------------
277 void ConvertFromKOI8(const string & src, string * dst)
279 ConvertKOI8(src, dst, FROM_KOI8);
281 //-----------------------------------------------------------------------------
282 void ConvertToKOI8(const string & src, string * dst)
284 ConvertKOI8(src, dst, TO_KOI8);
286 //-----------------------------------------------------------------------------
287 int RecvSetUserAnswer(const char * ans, void * d)
289 GetUserCbData * gucbd;
290 gucbd = (GetUserCbData *)d;
292 bool * result = gucbd->result;
294 //REQUEST * req = (REQUEST *)gucbd->data;
296 //printf("ans=%s\n", ans);
297 if (strcasecmp("Ok", ans) == 0)
304 //-----------------------------------------------------------------------------
305 struct StringReqParams
308 RESETABLE<string> reqParam;
311 //-----------------------------------------------------------------------------
312 void RecvUserData(USERDATA * ud, void * d)
314 GetUserCbData * gucbd;
315 gucbd = (GetUserCbData *)d;
317 bool * result = gucbd->result;
319 REQUEST * req = (REQUEST *)gucbd->data;
327 if (!req->cash.res_empty())
328 cout << "cash=" << ud->cash << endl;
330 if (!req->credit.res_empty())
331 cout << "credit=" << ud->credit << endl;
333 if (!req->down.res_empty())
334 cout << "down=" << ud->down << endl;
336 if (!req->passive.res_empty())
337 cout << "passive=" << ud->passive << endl;
339 if (!req->disableDetailStat.res_empty())
340 cout << "disableDetailStat=" << ud->disableDetailStat << endl;
342 if (!req->alwaysOnline.res_empty())
343 cout << "alwaysOnline=" << ud->alwaysOnline << endl;
345 if (!req->prepaidTraff.res_empty())
346 cout << "prepaidTraff=" << ud->prepaidTraff << endl;
348 for (int i = 0; i < DIR_NUM; i++)
350 if (!req->u[i].res_empty())
351 cout << "u" << i << "=" << ud->stat.mu[i] << endl;
352 if (!req->d[i].res_empty())
353 cout << "d" << i << "=" << ud->stat.md[i] << endl;
356 for (int i = 0; i < USERDATA_NUM; i++)
358 if (!req->ud[i].res_empty())
361 ConvertFromKOI8(ud->userData[i], &str);
362 cout << "userdata" << i << "=" << str << endl;
366 StringReqParams strReqParams[] =
368 {"note", req->note, &ud->note},
369 {"name", req->name, &ud->name},
370 {"address", req->address, &ud->address},
371 {"email", req->email, &ud->email},
372 {"phone", req->phone, &ud->phone},
373 {"group", req->group, &ud->group},
374 {"tariff", req->tariff, &ud->tariff},
375 {"password", req->usrPasswd, &ud->password},
376 {"ip", req->ips, &ud->ips} // IP-address of user
378 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
380 if (!strReqParams[i].reqParam.res_empty())
383 ConvertFromKOI8(*strReqParams[i].value, &str);
384 cout << strReqParams[i].name << "=" << str << endl;
389 //-----------------------------------------------------------------------------
390 int ProcessSetUser(const std::string &server,
392 const std::string &admLogin,
393 const std::string &admPasswd,
394 const std::string &str,
403 sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
404 sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
405 sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
406 sc.SetAdmPassword(admPasswd.c_str());
408 // TODO Good variable name :)
412 gucbd.result = &result;
416 sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
417 sc.MsgUser(str.c_str());
421 sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
422 sc.ChgUser(str.c_str());
438 //-----------------------------------------------------------------------------
439 int ProcessGetUser(const std::string &server,
441 const std::string &admLogin,
442 const std::string &admPasswd,
443 const std::string &login,
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;
461 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
462 sc.GetUser(login.c_str());
477 //-----------------------------------------------------------------------------