3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/user_conf.h"
11 #include "stg/user_stat.h"
12 #include "stg/user_ips.h"
13 #include "stg/common.h"
23 std::string Indent(size_t level, bool dash = false)
27 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
30 void PrintUser(const STG::GetUser::Info & info, size_t level = 0)
32 std::cout << Indent(level, true) << "login: " << info.login << "\n"
33 << Indent(level) << "password: " << info.password << "\n"
34 << Indent(level) << "cash: " << info.cash << "\n"
35 << Indent(level) << "credit: " << info.credit << "\n"
36 << Indent(level) << "credit expire: " << TimeToString(info.creditExpire) << "\n"
37 << Indent(level) << "last cash add: " << info.lastCashAdd << "\n"
38 << Indent(level) << "last cash add time: " << TimeToString(info.lastCashAddTime) << "\n"
39 << Indent(level) << "prepaid traffic: " << info.prepaidTraff << "\n"
40 << Indent(level) << "disabled: " << (info.disabled ? "t" : "f") << "\n"
41 << Indent(level) << "passive: " << (info.passive ? "t" : "f") << "\n"
42 << Indent(level) << "disabled detail stat: " << (info.disableDetailStat ? "t" : "f") << "\n"
43 << Indent(level) << "connected: " << (info.connected ? "t" : "f") << "\n"
44 << Indent(level) << "always on-line: " << (info.alwaysOnline ? "t" : "f") << "\n"
45 << Indent(level) << "IP: " << inet_ntostring(info.ip) << "\n"
46 << Indent(level) << "IPs: " << info.ips << "\n"
47 << Indent(level) << "tariff: " << info.tariff << "\n"
48 << Indent(level) << "group: " << info.group << "\n"
49 << Indent(level) << "note: " << info.note << "\n"
50 << Indent(level) << "email: " << info.email << "\n"
51 << Indent(level) << "name: " << info.name << "\n"
52 << Indent(level) << "address: " << info.address << "\n"
53 << Indent(level) << "phone: " << info.phone << "\n"
54 << Indent(level) << "corporation: " << info.corp << "\n"
55 << Indent(level) << "last ping time: " << TimeToString(info.pingTime) << "\n"
56 << Indent(level) << "last activity time: " << TimeToString(info.lastActivityTime) << "\n"
57 << Indent(level) << "traffic:\n";
58 for (size_t i = 0; i < DIR_NUM; ++i)
60 std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
61 << Indent(level + 1) << "session upload: " << info.stat.su[i] << "\n"
62 << Indent(level + 1) << "session download: " << info.stat.sd[i] << "\n"
63 << Indent(level + 1) << "month upload: " << info.stat.mu[i] << "\n"
64 << Indent(level + 1) << "month download: " << info.stat.md[i] << "\n";
66 std::cout << Indent(level) << "user data:\n";
67 for (size_t i = 0; i < USERDATA_NUM; ++i)
68 std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
69 if (!info.services.empty())
71 std::cout << Indent(level) << "services:\n";
72 for (size_t i = 0; i < info.services.size(); ++i)
73 std::cout << Indent(level + 1, true) << info.services[i] << "\n";
75 if (!info.authBy.empty())
77 std::cout << Indent(level) << "auth by:\n";
78 for (size_t i = 0; i < info.authBy.size(); ++i)
79 std::cout << Indent(level + 1, true) << info.authBy[i] << "\n";
83 std::vector<SGCONF::API_ACTION::PARAM> GetUserParams()
85 std::vector<SGCONF::API_ACTION::PARAM> params;
86 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
87 params.push_back(SGCONF::API_ACTION::PARAM("cash-add", "<cash[:message]>", "cash to add (with optional comment)"));
88 params.push_back(SGCONF::API_ACTION::PARAM("cash-set", "<cash[:message]>", "cash to set (with optional comment)"));
89 params.push_back(SGCONF::API_ACTION::PARAM("credit", "<amount>", "\tuser's credit"));
90 params.push_back(SGCONF::API_ACTION::PARAM("credit-expire", "<date>", "\tcredit expiration"));
91 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
92 params.push_back(SGCONF::API_ACTION::PARAM("disabled", "<flag>", "\tdisable user (y|n)"));
93 params.push_back(SGCONF::API_ACTION::PARAM("passive", "<flag>", "\tmake user passive (y|n)"));
94 params.push_back(SGCONF::API_ACTION::PARAM("disable-detail-stat", "<flag>", "disable detail stat (y|n)"));
95 params.push_back(SGCONF::API_ACTION::PARAM("always-online", "<flag>", "\tmake user always online (y|n)"));
96 params.push_back(SGCONF::API_ACTION::PARAM("ips", "<ips>", "\t\tcoma-separated list of ips"));
97 params.push_back(SGCONF::API_ACTION::PARAM("tariff", "<tariff name>", "\tcurrent tariff"));
98 params.push_back(SGCONF::API_ACTION::PARAM("next-tariff", "<tariff name>", "tariff starting from the next month"));
99 params.push_back(SGCONF::API_ACTION::PARAM("group", "<group>", "\t\tuser's group"));
100 params.push_back(SGCONF::API_ACTION::PARAM("note", "<note>", "\t\tuser's note"));
101 params.push_back(SGCONF::API_ACTION::PARAM("email", "<email>", "\t\tuser's email"));
102 params.push_back(SGCONF::API_ACTION::PARAM("name", "<real name>", "\tuser's real name"));
103 params.push_back(SGCONF::API_ACTION::PARAM("address", "<address>", "\tuser's postal address"));
104 params.push_back(SGCONF::API_ACTION::PARAM("phone", "<phone>", "\t\tuser's phone number"));
105 params.push_back(SGCONF::API_ACTION::PARAM("corp", "<corp name>", "\tcorporation name"));
106 params.push_back(SGCONF::API_ACTION::PARAM("session-traffic", "<up/dn, ...>", "coma-separated session upload and download"));
107 params.push_back(SGCONF::API_ACTION::PARAM("month-traffic", "<up/dn, ...>", "coma-separated month upload and download"));
108 params.push_back(SGCONF::API_ACTION::PARAM("user-data", "<value, ...>", "coma-separated user data values"));
112 std::vector<SGCONF::API_ACTION::PARAM> GetCheckParams()
114 std::vector<SGCONF::API_ACTION::PARAM> params;
115 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
119 std::vector<SGCONF::API_ACTION::PARAM> GetMessageParams()
121 std::vector<SGCONF::API_ACTION::PARAM> params;
122 params.push_back(SGCONF::API_ACTION::PARAM("logins", "<login, ...>", "\tlist of logins to send a message"));
123 params.push_back(SGCONF::API_ACTION::PARAM("text", "<text>", "\t\tmessage text"));
127 void ConvBool(const std::string & value, STG::Optional<int> & res)
129 res = !value.empty() && value[0] == 'y';
132 void Splice(std::vector<STG::Optional<std::string> > & lhs, const std::vector<STG::Optional<std::string> > & rhs)
134 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
135 lhs[i].splice(rhs[i]);
138 STG::Optional<std::string> ConvString(const std::string & value)
140 return STG::Optional<std::string>(value);
143 void ConvStringList(std::string value, std::vector<STG::Optional<std::string> > & res)
145 Splice(res, Split<std::vector<STG::Optional<std::string> > >(value, ',', ConvString));
148 void ConvServices(std::string value, STG::Optional<std::vector<std::string> > & res)
150 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
151 res = Split<std::vector<std::string> >(value, ',');
154 void ConvCreditExpire(const std::string & value, STG::Optional<time_t> & res)
156 struct tm brokenTime;
157 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
158 throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
159 res = stg_timegm(&brokenTime);
162 void ConvIPs(const std::string & value, STG::Optional<STG::UserIPs> & res)
164 res = STG::UserIPs::parse(value);
173 TRAFF ConvTraff(const std::string & value)
176 size_t slashPos = value.find_first_of('/');
177 if (slashPos == std::string::npos)
178 throw SGCONF::ACTION::ERROR("Traffic record should be in format 'upload/download'. Got: '" + value + "'");
180 if (str2x(value.substr(0, slashPos), res.up) < 0)
181 throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(0, slashPos) + "'");
182 if (str2x(value.substr(slashPos + 1, value.length() - slashPos), res.down) < 0)
183 throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
187 void ConvSessionTraff(std::string value, STG::UserStatOpt & res)
189 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
190 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
191 if (traff.size() != DIR_NUM)
192 throw SGCONF::ACTION::ERROR("There should be prcisely " + std::to_string(DIR_NUM) + " records of session traffic.");
193 for (size_t i = 0; i < DIR_NUM; ++i)
195 res.sessionUp[i] = traff[i].up;
196 res.sessionDown[i] = traff[i].down;
200 void ConvMonthTraff(std::string value, STG::UserStatOpt & res)
202 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
203 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
204 if (traff.size() != DIR_NUM)
205 throw SGCONF::ACTION::ERROR("There should be prcisely " + std::to_string(DIR_NUM) + " records of month traffic.");
206 for (size_t i = 0; i < DIR_NUM; ++i)
208 res.monthUp[i] = traff[i].up;
209 res.monthDown[i] = traff[i].down;
213 void ConvCashInfo(const std::string & value, STG::Optional<STG::CashInfo> & res)
216 size_t pos = value.find_first_of(':');
217 if (pos == std::string::npos)
219 if (str2x(value, info.first) < 0)
220 throw SGCONF::ACTION::ERROR("Cash should be a double value. Got: '" + value + "'");
224 if (str2x(value.substr(0, pos), info.first) < 0)
225 throw SGCONF::ACTION::ERROR("Cash should be a double value. Got: '" + value + "'");
226 info.second = value.substr(pos + 1);
231 void SimpleCallback(bool result,
232 const std::string & reason,
237 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
240 std::cout << "Success.\n";
243 void GetUsersCallback(bool result,
244 const std::string & reason,
245 const std::vector<STG::GetUser::Info> & info,
250 std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
253 std::cout << "Users:\n";
254 for (size_t i = 0; i < info.size(); ++i)
255 PrintUser(info[i], 1);
258 void GetUserCallback(bool result,
259 const std::string & reason,
260 const STG::GetUser::Info & info,
265 std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
271 void AuthByCallback(bool result,
272 const std::string & reason,
273 const std::vector<std::string> & info,
278 std::cerr << "Failed to get authorizer list. Reason: '" << reason << "'." << std::endl;
281 std::cout << "Authorized by:\n";
282 for (size_t i = 0; i < info.size(); ++i)
283 std::cout << Indent(1, true) << info[i] << "\n";
286 bool GetUsersFunction(const SGCONF::CONFIG & config,
287 const std::string & /*arg*/,
288 const std::map<std::string, std::string> & /*options*/)
290 STG::ServConf proto(config.server.data(),
292 config.localAddress.data(),
293 config.localPort.data(),
294 config.userName.data(),
295 config.userPass.data());
296 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
299 bool GetUserFunction(const SGCONF::CONFIG & config,
300 const std::string & arg,
301 const std::map<std::string, std::string> & /*options*/)
303 STG::ServConf proto(config.server.data(),
305 config.localAddress.data(),
306 config.localPort.data(),
307 config.userName.data(),
308 config.userPass.data());
309 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
312 bool DelUserFunction(const SGCONF::CONFIG & config,
313 const std::string & arg,
314 const std::map<std::string, std::string> & /*options*/)
316 STG::ServConf proto(config.server.data(),
318 config.localAddress.data(),
319 config.localPort.data(),
320 config.userName.data(),
321 config.userPass.data());
322 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
325 bool AddUserFunction(const SGCONF::CONFIG & config,
326 const std::string & arg,
327 const std::map<std::string, std::string> & options)
329 STG::UserConfOpt conf;
330 SGCONF::MaybeSet(options, "password", conf.password);
331 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
332 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
333 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
334 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
335 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
336 SGCONF::MaybeSet(options, "address", conf.address);
337 SGCONF::MaybeSet(options, "phone", conf.phone);
338 SGCONF::MaybeSet(options, "email", conf.email);
339 SGCONF::MaybeSet(options, "note", conf.note);
340 SGCONF::MaybeSet(options, "name", conf.realName);
341 SGCONF::MaybeSet(options, "corp", conf.corp);
342 SGCONF::MaybeSet(options, "services", conf.services, ConvServices);
343 SGCONF::MaybeSet(options, "group", conf.group);
344 SGCONF::MaybeSet(options, "credit", conf.credit);
345 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
346 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
347 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
348 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
349 STG::UserStatOpt stat;
350 SGCONF::MaybeSet(options, "cash-set", stat.cashSet, ConvCashInfo);
351 SGCONF::MaybeSet(options, "free", stat.freeMb);
352 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
353 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
354 STG::ServConf proto(config.server.data(),
356 config.localAddress.data(),
357 config.localPort.data(),
358 config.userName.data(),
359 config.userPass.data());
360 return proto.AddUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
363 bool ChgUserFunction(const SGCONF::CONFIG & config,
364 const std::string & arg,
365 const std::map<std::string, std::string> & options)
367 STG::UserConfOpt conf;
368 SGCONF::MaybeSet(options, "password", conf.password);
369 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
370 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
371 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
372 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
373 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
374 SGCONF::MaybeSet(options, "address", conf.address);
375 SGCONF::MaybeSet(options, "phone", conf.phone);
376 SGCONF::MaybeSet(options, "email", conf.email);
377 SGCONF::MaybeSet(options, "note", conf.note);
378 SGCONF::MaybeSet(options, "name", conf.realName);
379 SGCONF::MaybeSet(options, "corp", conf.corp);
380 SGCONF::MaybeSet(options, "services", conf.services, ConvServices);
381 SGCONF::MaybeSet(options, "group", conf.group);
382 SGCONF::MaybeSet(options, "credit", conf.credit);
383 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
384 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
385 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
386 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
387 STG::UserStatOpt stat;
388 SGCONF::MaybeSet(options, "cash-add", stat.cashAdd, ConvCashInfo);
389 SGCONF::MaybeSet(options, "cash-set", stat.cashSet, ConvCashInfo);
390 SGCONF::MaybeSet(options, "free", stat.freeMb);
391 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
392 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
393 STG::ServConf proto(config.server.data(),
395 config.localAddress.data(),
396 config.localPort.data(),
397 config.userName.data(),
398 config.userPass.data());
399 return proto.ChgUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
402 bool CheckUserFunction(const SGCONF::CONFIG & config,
403 const std::string & arg,
404 const std::map<std::string, std::string> & options)
406 std::map<std::string, std::string>::const_iterator it(options.find("password"));
407 if (it == options.end())
408 throw SGCONF::ACTION::ERROR("Password is not specified.");
409 STG::ServConf proto(config.server.data(),
411 config.localAddress.data(),
412 config.localPort.data(),
413 config.userName.data(),
414 config.userPass.data());
415 return proto.CheckUser(arg, it->second, SimpleCallback, NULL) == STG::st_ok;
418 bool SendMessageFunction(const SGCONF::CONFIG & config,
419 const std::string & /*arg*/,
420 const std::map<std::string, std::string> & options)
422 std::map<std::string, std::string>::const_iterator it(options.find("logins"));
423 if (it == options.end())
424 throw SGCONF::ACTION::ERROR("Logins are not specified.");
425 std::string logins = it->second;
426 for (size_t i = 0; i < logins.length(); ++i)
427 if (logins[i] == ',')
429 it = options.find("text");
430 if (it == options.end())
431 throw SGCONF::ACTION::ERROR("Message text is not specified.");
432 std::string text = it->second;
433 STG::ServConf proto(config.server.data(),
435 config.localAddress.data(),
436 config.localPort.data(),
437 config.userName.data(),
438 config.userPass.data());
439 return proto.SendMessage(logins, text, SimpleCallback, NULL) == STG::st_ok;
442 bool AuthByFunction(const SGCONF::CONFIG & config,
443 const std::string & arg,
444 const std::map<std::string, std::string> & /*options*/)
446 STG::ServConf proto(config.server.data(),
448 config.localAddress.data(),
449 config.localPort.data(),
450 config.userName.data(),
451 config.userPass.data());
452 return proto.AuthBy(arg, AuthByCallback, NULL) == STG::st_ok;
455 } // namespace anonymous
457 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
459 std::vector<API_ACTION::PARAM> params(GetUserParams());
460 blocks.Add("User management options")
461 .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
462 .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", GetUserFunction), "get user")
463 .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", params, AddUserFunction), "add user")
464 .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "delete user")
465 .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", params, ChgUserFunction), "change user")
466 .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", GetCheckParams(), CheckUserFunction), "check user existance and credentials")
467 .Add("send-message", SGCONF::MakeAPIAction(commands, GetMessageParams(), SendMessageFunction), "send message")
468 .Add("auth-by", SGCONF::MakeAPIAction(commands, "<login>", AuthByFunction), "a list of authorizers user authorized by");