]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
Verbose test reports on failures.
[stg.git] / projects / sgconf / users.cpp
1 #include "users.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "makeproto.h"
6 #include "config.h"
7 #include "utils.h"
8
9 #include "stg/servconf.h"
10 #include "stg/servconf_types.h"
11 #include "stg/user_conf.h"
12 #include "stg/user_stat.h"
13 #include "stg/user_ips.h"
14 #include "stg/common.h"
15 #include "stg/splice.h"
16
17 #include <iostream>
18 #include <algorithm>
19 #include <string>
20 #include <map>
21 #include <optional>
22
23 namespace
24 {
25
26 std::string Indent(size_t level, bool dash = false)
27 {
28 if (level == 0)
29     return "";
30 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
31 }
32
33 void PrintUser(const STG::GetUser::Info & info, size_t level = 0)
34 {
35 std::cout << Indent(level, true) << "login: " << info.login << "\n"
36           << Indent(level)       << "password: " << info.password << "\n"
37           << Indent(level)       << "cash: " << info.cash << "\n"
38           << Indent(level)       << "credit: " << info.credit << "\n"
39           << Indent(level)       << "credit expire: " << TimeToString(info.creditExpire) << "\n"
40           << Indent(level)       << "last cash add: " << info.lastCashAdd << "\n"
41           << Indent(level)       << "last cash add time: " << TimeToString(info.lastCashAddTime) << "\n"
42           << Indent(level)       << "prepaid traffic: " << info.prepaidTraff << "\n"
43           << Indent(level)       << "disabled: " << (info.disabled ? "t" : "f") << "\n"
44           << Indent(level)       << "passive: " << (info.passive ? "t" : "f") << "\n"
45           << Indent(level)       << "disabled detail stat: " << (info.disableDetailStat ? "t" : "f") << "\n"
46           << Indent(level)       << "connected: " << (info.connected ? "t" : "f") << "\n"
47           << Indent(level)       << "always on-line: " << (info.alwaysOnline ? "t" : "f") << "\n"
48           << Indent(level)       << "IP: " << inet_ntostring(info.ip) << "\n"
49           << Indent(level)       << "IPs: " << info.ips << "\n"
50           << Indent(level)       << "tariff: " << info.tariff << "\n"
51           << Indent(level)       << "group: " << info.group << "\n"
52           << Indent(level)       << "note: " << info.note << "\n"
53           << Indent(level)       << "email: " << info.email << "\n"
54           << Indent(level)       << "name: " << info.name << "\n"
55           << Indent(level)       << "address: " << info.address << "\n"
56           << Indent(level)       << "phone: " << info.phone << "\n"
57           << Indent(level)       << "corporation: " << info.corp << "\n"
58           << Indent(level)       << "last ping time: " << TimeToString(info.pingTime) << "\n"
59           << Indent(level)       << "last activity time: " << TimeToString(info.lastActivityTime) << "\n"
60           << Indent(level)       << "traffic:\n";
61 for (size_t i = 0; i < DIR_NUM; ++i)
62     {
63     std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
64               << Indent(level + 1)       << "session upload: " << info.stat.su[i] << "\n"
65               << Indent(level + 1)       << "session download: " << info.stat.sd[i] << "\n"
66               << Indent(level + 1)       << "month upload: " << info.stat.mu[i] << "\n"
67               << Indent(level + 1)       << "month download: " << info.stat.md[i] << "\n";
68     }
69 std::cout << Indent(level)       << "user data:\n";
70 for (size_t i = 0; i < USERDATA_NUM; ++i)
71     std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
72 if (!info.services.empty())
73     {
74     std::cout << Indent(level) << "services:\n";
75     for (size_t i = 0; i < info.services.size(); ++i)
76         std::cout << Indent(level + 1, true) << info.services[i] << "\n";
77     }
78 if (!info.authBy.empty())
79     {
80     std::cout << Indent(level) << "auth by:\n";
81     for (size_t i = 0; i < info.authBy.size(); ++i)
82         std::cout << Indent(level + 1, true) << info.authBy[i] << "\n";
83     }
84 }
85
86 std::vector<SGCONF::API_ACTION::PARAM> GetUserParams()
87 {
88 std::vector<SGCONF::API_ACTION::PARAM> params;
89 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
90 params.push_back(SGCONF::API_ACTION::PARAM("cash-add", "<cash[:message]>", "cash to add (with optional comment)"));
91 params.push_back(SGCONF::API_ACTION::PARAM("cash-set", "<cash[:message]>", "cash to set (with optional comment)"));
92 params.push_back(SGCONF::API_ACTION::PARAM("credit", "<amount>", "\tuser's credit"));
93 params.push_back(SGCONF::API_ACTION::PARAM("credit-expire", "<date>", "\tcredit expiration"));
94 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
95 params.push_back(SGCONF::API_ACTION::PARAM("disabled", "<flag>", "\tdisable user (y|n)"));
96 params.push_back(SGCONF::API_ACTION::PARAM("passive", "<flag>", "\tmake user passive (y|n)"));
97 params.push_back(SGCONF::API_ACTION::PARAM("disable-detail-stat", "<flag>", "disable detail stat (y|n)"));
98 params.push_back(SGCONF::API_ACTION::PARAM("always-online", "<flag>", "\tmake user always online (y|n)"));
99 params.push_back(SGCONF::API_ACTION::PARAM("ips", "<ips>", "\t\tcoma-separated list of ips"));
100 params.push_back(SGCONF::API_ACTION::PARAM("tariff", "<tariff name>", "\tcurrent tariff"));
101 params.push_back(SGCONF::API_ACTION::PARAM("next-tariff", "<tariff name>", "tariff starting from the next month"));
102 params.push_back(SGCONF::API_ACTION::PARAM("group", "<group>", "\t\tuser's group"));
103 params.push_back(SGCONF::API_ACTION::PARAM("note", "<note>", "\t\tuser's note"));
104 params.push_back(SGCONF::API_ACTION::PARAM("email", "<email>", "\t\tuser's email"));
105 params.push_back(SGCONF::API_ACTION::PARAM("name", "<real name>", "\tuser's real name"));
106 params.push_back(SGCONF::API_ACTION::PARAM("address", "<address>", "\tuser's postal address"));
107 params.push_back(SGCONF::API_ACTION::PARAM("phone", "<phone>", "\t\tuser's phone number"));
108 params.push_back(SGCONF::API_ACTION::PARAM("corp", "<corp name>", "\tcorporation name"));
109 params.push_back(SGCONF::API_ACTION::PARAM("session-traffic", "<up/dn, ...>", "coma-separated session upload and download"));
110 params.push_back(SGCONF::API_ACTION::PARAM("month-traffic", "<up/dn, ...>", "coma-separated month upload and download"));
111 params.push_back(SGCONF::API_ACTION::PARAM("user-data", "<value, ...>", "coma-separated user data values"));
112 return params;
113 }
114
115 std::vector<SGCONF::API_ACTION::PARAM> GetCheckParams()
116 {
117 std::vector<SGCONF::API_ACTION::PARAM> params;
118 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
119 return params;
120 }
121
122 std::vector<SGCONF::API_ACTION::PARAM> GetMessageParams()
123 {
124 std::vector<SGCONF::API_ACTION::PARAM> params;
125 params.push_back(SGCONF::API_ACTION::PARAM("logins", "<login, ...>", "\tlist of logins to send a message"));
126 params.push_back(SGCONF::API_ACTION::PARAM("text", "<text>", "\t\tmessage text"));
127 return params;
128 }
129
130 void ConvBool(const std::string & value, std::optional<int> & res)
131 {
132 res = !value.empty() && value[0] == 'y';
133 }
134
135 void Splice(std::vector<std::optional<std::string> > & lhs, const std::vector<std::optional<std::string> > & rhs)
136 {
137 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
138     STG::splice(lhs[i], rhs[i]);
139 }
140
141 std::optional<std::string> ConvString(const std::string & value)
142 {
143 return std::optional<std::string>(value);
144 }
145
146 void ConvStringList(std::string value, std::vector<std::optional<std::string> > & res)
147 {
148 Splice(res, Split<std::vector<std::optional<std::string> > >(value, ',', ConvString));
149 }
150
151 void ConvServices(std::string value, std::optional<std::vector<std::string> > & res)
152 {
153 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
154 res = Split<std::vector<std::string> >(value, ',');
155 }
156
157 void ConvCreditExpire(const std::string & value, std::optional<time_t> & res)
158 {
159 struct tm brokenTime;
160 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
161     throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
162 res = stg_timegm(&brokenTime);
163 }
164
165 void ConvIPs(const std::string & value, std::optional<STG::UserIPs> & res)
166 {
167 res = STG::UserIPs::parse(value);
168 }
169
170 struct TRAFF
171 {
172     uint64_t up;
173     uint64_t down;
174 };
175
176 TRAFF ConvTraff(const std::string & value)
177 {
178 TRAFF res;
179 size_t slashPos = value.find_first_of('/');
180 if (slashPos == std::string::npos)
181     throw SGCONF::ACTION::ERROR("Traffic record should be in format 'upload/download'. Got: '" + value + "'");
182
183 if (str2x(value.substr(0, slashPos), res.up) < 0)
184     throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(0, slashPos) + "'");
185 if (str2x(value.substr(slashPos + 1, value.length() - slashPos), res.down) < 0)
186     throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
187 return res;
188 }
189
190 void ConvSessionTraff(std::string value, STG::UserStatOpt & res)
191 {
192 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
193 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
194 if (traff.size() != DIR_NUM)
195     throw SGCONF::ACTION::ERROR("There should be prcisely " + std::to_string(DIR_NUM) + " records of session traffic.");
196 for (size_t i = 0; i < DIR_NUM; ++i)
197     {
198     res.sessionUp[i] = traff[i].up;
199     res.sessionDown[i] = traff[i].down;
200     }
201 }
202
203 void ConvMonthTraff(std::string value, STG::UserStatOpt & res)
204 {
205 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
206 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
207 if (traff.size() != DIR_NUM)
208     throw SGCONF::ACTION::ERROR("There should be prcisely " + std::to_string(DIR_NUM) + " records of month traffic.");
209 for (size_t i = 0; i < DIR_NUM; ++i)
210     {
211     res.monthUp[i] = traff[i].up;
212     res.monthDown[i] = traff[i].down;
213     }
214 }
215
216 void ConvCashInfo(const std::string & value, std::optional<STG::CashInfo> & res)
217 {
218 STG::CashInfo info;
219 size_t pos = value.find_first_of(':');
220 if (pos == std::string::npos)
221     {
222     if (str2x(value, info.first) < 0)
223         throw SGCONF::ACTION::ERROR("Cash should be a double value. Got: '" + value + "'");
224     }
225 else
226     {
227     if (str2x(value.substr(0, pos), info.first) < 0)
228         throw SGCONF::ACTION::ERROR("Cash should be a double value. Got: '" + value + "'");
229     info.second = value.substr(pos + 1);
230     }
231 res = info;
232 }
233
234 void SimpleCallback(bool result,
235                     const std::string & reason,
236                     void * /*data*/)
237 {
238 if (!result)
239     {
240     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
241     return;
242     }
243 std::cout << "Success.\n";
244 }
245
246 void GetUsersCallback(bool result,
247                       const std::string & reason,
248                       const std::vector<STG::GetUser::Info> & info,
249                       void * /*data*/)
250 {
251 if (!result)
252     {
253     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
254     return;
255     }
256 std::cout << "Users:\n";
257 for (size_t i = 0; i < info.size(); ++i)
258     PrintUser(info[i], 1);
259 }
260
261 void GetUserCallback(bool result,
262                      const std::string & reason,
263                      const STG::GetUser::Info & info,
264                      void * /*data*/)
265 {
266 if (!result)
267     {
268     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
269     return;
270     }
271 PrintUser(info);
272 }
273
274 void AuthByCallback(bool result,
275                     const std::string & reason,
276                     const std::vector<std::string> & info,
277                     void * /*data*/)
278 {
279 if (!result)
280     {
281     std::cerr << "Failed to get authorizer list. Reason: '" << reason << "'." << std::endl;
282     return;
283     }
284 std::cout << "Authorized by:\n";
285 for (size_t i = 0; i < info.size(); ++i)
286     std::cout << Indent(1, true) << info[i] << "\n";
287 }
288
289 bool GetUsersFunction(const SGCONF::CONFIG & config,
290                       const std::string & /*arg*/,
291                       const std::map<std::string, std::string> & /*options*/)
292 {
293 return makeProto(config).GetUsers(GetUsersCallback, NULL) == STG::st_ok;
294 }
295
296 bool GetUserFunction(const SGCONF::CONFIG & config,
297                      const std::string & arg,
298                      const std::map<std::string, std::string> & /*options*/)
299 {
300 return makeProto(config).GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
301 }
302
303 bool DelUserFunction(const SGCONF::CONFIG & config,
304                      const std::string & arg,
305                      const std::map<std::string, std::string> & /*options*/)
306 {
307 return makeProto(config).DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
308 }
309
310 bool AddUserFunction(const SGCONF::CONFIG & config,
311                      const std::string & arg,
312                      const std::map<std::string, std::string> & options)
313 {
314 STG::UserConfOpt conf;
315 SGCONF::MaybeSet(options, "password", conf.password);
316 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
317 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
318 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
319 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
320 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
321 SGCONF::MaybeSet(options, "address", conf.address);
322 SGCONF::MaybeSet(options, "phone", conf.phone);
323 SGCONF::MaybeSet(options, "email", conf.email);
324 SGCONF::MaybeSet(options, "note", conf.note);
325 SGCONF::MaybeSet(options, "name", conf.realName);
326 SGCONF::MaybeSet(options, "corp", conf.corp);
327 SGCONF::MaybeSet(options, "services", conf.services, ConvServices);
328 SGCONF::MaybeSet(options, "group", conf.group);
329 SGCONF::MaybeSet(options, "credit", conf.credit);
330 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
331 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
332 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
333 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
334 STG::UserStatOpt stat;
335 SGCONF::MaybeSet(options, "cash-set", stat.cashSet, ConvCashInfo);
336 SGCONF::MaybeSet(options, "free", stat.freeMb);
337 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
338 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
339 return makeProto(config).AddUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
340 }
341
342 bool ChgUserFunction(const SGCONF::CONFIG & config,
343                      const std::string & arg,
344                      const std::map<std::string, std::string> & options)
345 {
346 STG::UserConfOpt conf;
347 SGCONF::MaybeSet(options, "password", conf.password);
348 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
349 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
350 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
351 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
352 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
353 SGCONF::MaybeSet(options, "address", conf.address);
354 SGCONF::MaybeSet(options, "phone", conf.phone);
355 SGCONF::MaybeSet(options, "email", conf.email);
356 SGCONF::MaybeSet(options, "note", conf.note);
357 SGCONF::MaybeSet(options, "name", conf.realName);
358 SGCONF::MaybeSet(options, "corp", conf.corp);
359 SGCONF::MaybeSet(options, "services", conf.services, ConvServices);
360 SGCONF::MaybeSet(options, "group", conf.group);
361 SGCONF::MaybeSet(options, "credit", conf.credit);
362 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
363 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
364 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
365 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
366 STG::UserStatOpt stat;
367 SGCONF::MaybeSet(options, "cash-add", stat.cashAdd, ConvCashInfo);
368 SGCONF::MaybeSet(options, "cash-set", stat.cashSet, ConvCashInfo);
369 SGCONF::MaybeSet(options, "free", stat.freeMb);
370 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
371 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
372 return makeProto(config).ChgUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
373 }
374
375 bool CheckUserFunction(const SGCONF::CONFIG & config,
376                        const std::string & arg,
377                        const std::map<std::string, std::string> & options)
378 {
379 std::map<std::string, std::string>::const_iterator it(options.find("password"));
380 if (it == options.end())
381     throw SGCONF::ACTION::ERROR("Password is not specified.");
382 return makeProto(config).CheckUser(arg, it->second, SimpleCallback, NULL) == STG::st_ok;
383 }
384
385 bool SendMessageFunction(const SGCONF::CONFIG & config,
386                          const std::string & /*arg*/,
387                          const std::map<std::string, std::string> & options)
388 {
389 std::map<std::string, std::string>::const_iterator it(options.find("logins"));
390 if (it == options.end())
391     throw SGCONF::ACTION::ERROR("Logins are not specified.");
392 std::string logins = it->second;
393 for (size_t i = 0; i < logins.length(); ++i)
394     if (logins[i] == ',')
395         logins[i] = ':';
396 it = options.find("text");
397 if (it == options.end())
398     throw SGCONF::ACTION::ERROR("Message text is not specified.");
399 std::string text = it->second;
400 return makeProto(config).SendMessage(logins, text, SimpleCallback, NULL) == STG::st_ok;
401 }
402
403 bool AuthByFunction(const SGCONF::CONFIG & config,
404                     const std::string & arg,
405                     const std::map<std::string, std::string> & /*options*/)
406 {
407 return makeProto(config).AuthBy(arg, AuthByCallback, NULL) == STG::st_ok;
408 }
409
410 } // namespace anonymous
411
412 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
413 {
414 std::vector<API_ACTION::PARAM> params(GetUserParams());
415 blocks.Add("User management options")
416       .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
417       .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", GetUserFunction), "get user")
418       .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", params, AddUserFunction), "add user")
419       .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "delete user")
420       .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", params, ChgUserFunction), "change user")
421       .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", GetCheckParams(), CheckUserFunction), "check user existance and credentials")
422       .Add("send-message", SGCONF::MakeAPIAction(commands, GetMessageParams(), SendMessageFunction), "send message")
423       .Add("auth-by", SGCONF::MakeAPIAction(commands, "<login>", AuthByFunction), "a list of authorizers user authorized by");
424 }