]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
Implemented add-user and chg-user.
[stg.git] / projects / sgconf / users.cpp
1 #include "users.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
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"
14
15 #include <iostream>
16 #include <algorithm>
17 #include <string>
18 #include <map>
19
20 namespace
21 {
22
23 std::string Indent(size_t level, bool dash = false)
24 {
25 if (level == 0)
26     return "";
27 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
28 }
29
30 void PrintUser(const STG::GET_USER::INFO & info, size_t level = 0)
31 {
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)
59     {
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";
65     }
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())
70     {
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";
74     }
75 if (!info.authBy.empty())
76     {
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";
80     }
81 }
82
83 std::vector<SGCONF::API_ACTION::PARAM> GetUserParams()
84 {
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", "<cash>", "\t\tuser's cash"));
88 params.push_back(SGCONF::API_ACTION::PARAM("credit", "<amount>", "\tuser's credit"));
89 params.push_back(SGCONF::API_ACTION::PARAM("credit-expire", "<date>", "\tcredit expiration"));
90 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
91 params.push_back(SGCONF::API_ACTION::PARAM("disabled", "<flag>", "\tdisable user (y|n)"));
92 params.push_back(SGCONF::API_ACTION::PARAM("passive", "<flag>", "\tmake user passive (y|n)"));
93 params.push_back(SGCONF::API_ACTION::PARAM("disable-detail-stat", "<flag>", "disable detail stat (y|n)"));
94 params.push_back(SGCONF::API_ACTION::PARAM("always-online", "<flag>", "\tmake user always online (y|n)"));
95 params.push_back(SGCONF::API_ACTION::PARAM("ips", "<ips>", "\t\tcoma-separated list of ips"));
96 params.push_back(SGCONF::API_ACTION::PARAM("tariff", "<tariff name>", "\tcurrent tariff"));
97 params.push_back(SGCONF::API_ACTION::PARAM("next-tariff", "<tariff name>", "tariff starting from the next month"));
98 params.push_back(SGCONF::API_ACTION::PARAM("group", "<group>", "\t\tuser's group"));
99 params.push_back(SGCONF::API_ACTION::PARAM("note", "<note>", "\t\tuser's note"));
100 params.push_back(SGCONF::API_ACTION::PARAM("email", "<email>", "\t\tuser's email"));
101 params.push_back(SGCONF::API_ACTION::PARAM("name", "<real name>", "\tuser's real name"));
102 params.push_back(SGCONF::API_ACTION::PARAM("address", "<address>", "\tuser's postal address"));
103 params.push_back(SGCONF::API_ACTION::PARAM("phone", "<phone>", "\t\tuser's phone number"));
104 params.push_back(SGCONF::API_ACTION::PARAM("corp", "<corp name>", "\t\tcorporation name"));
105 params.push_back(SGCONF::API_ACTION::PARAM("session-traffic", "<up/dn, ...>", "coma-separated session upload and download"));
106 params.push_back(SGCONF::API_ACTION::PARAM("month-traffic", "<up/dn, ...>", "coma-separated month upload and download"));
107 params.push_back(SGCONF::API_ACTION::PARAM("user-data", "<value, ...>", "coma-separated user data values"));
108 return params;
109 }
110
111 std::vector<SGCONF::API_ACTION::PARAM> GetCheckParams()
112 {
113 std::vector<SGCONF::API_ACTION::PARAM> params;
114 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
115 return params;
116 }
117
118 std::vector<SGCONF::API_ACTION::PARAM> GetMessageParams()
119 {
120 std::vector<SGCONF::API_ACTION::PARAM> params;
121 params.push_back(SGCONF::API_ACTION::PARAM("logins", "<login, ...>", "\tlist of logins to send a message"));
122 params.push_back(SGCONF::API_ACTION::PARAM("text", "<text>", "\t\tmessage text"));
123 return params;
124 }
125
126 void ConvBool(const std::string & value, RESETABLE<int> & res)
127 {
128 res = !value.empty() && value[0] == 'y';
129 }
130
131 void Splice(std::vector<RESETABLE<std::string> > & lhs, const std::vector<RESETABLE<std::string> > & rhs)
132 {
133 for (size_t i = 0; i < lhs.size(); ++i)
134     lhs[i].splice(rhs[i]);
135 }
136
137 RESETABLE<std::string> ConvString(const std::string & value)
138 {
139 return value;
140 }
141
142 void ConvStringList(std::string value, std::vector<RESETABLE<std::string> > & res)
143 {
144 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
145 Splice(res, Split<std::vector<RESETABLE<std::string> > >(value, ',', ConvString));
146 }
147
148 void ConvCreditExpire(const std::string & value, RESETABLE<time_t> & res)
149 {
150 struct tm brokenTime;
151 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
152     throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
153 res = stg_timegm(&brokenTime);
154 }
155
156 void ConvIPs(const std::string & value, RESETABLE<USER_IPS> & res)
157 {
158 res = StrToIPS(value);
159 }
160
161 struct TRAFF
162 {
163     uint64_t up;
164     uint64_t down;
165 };
166
167 TRAFF ConvTraff(const std::string & value)
168 {
169 TRAFF res;
170 size_t slashPos = value.find_first_of('/');
171 if (slashPos == std::string::npos)
172     throw SGCONF::ACTION::ERROR("Traffic record should be in format 'upload/download'. Got: '" + value + "'");
173
174 if (str2x(value.substr(0, slashPos), res.up) < 0)
175     throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(0, slashPos) + "'");
176 if (str2x(value.substr(slashPos + 1, value.length() - slashPos), res.down) < 0)
177     throw SGCONF::ACTION::ERROR("Traffic value should be an integer. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
178 return res;
179 }
180
181 void ConvSessionTraff(std::string value, USER_STAT_RES & res)
182 {
183 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
184 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
185 if (traff.size() != DIR_NUM)
186     throw SGCONF::ACTION::ERROR("There should be prcisely " + x2str(DIR_NUM) + " records of session traffic.");
187 for (size_t i = 0; i < DIR_NUM; ++i)
188     {
189     res.sessionUp[i] = traff[i].up;
190     res.sessionDown[i] = traff[i].down;
191     }
192 }
193
194 void ConvMonthTraff(std::string value, USER_STAT_RES & res)
195 {
196 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
197 std::vector<TRAFF> traff(Split<std::vector<TRAFF> >(value, ',', ConvTraff));
198 if (traff.size() != DIR_NUM)
199     throw SGCONF::ACTION::ERROR("There should be prcisely " + x2str(DIR_NUM) + " records of month traffic.");
200 for (size_t i = 0; i < DIR_NUM; ++i)
201     {
202     res.monthUp[i] = traff[i].up;
203     res.monthDown[i] = traff[i].down;
204     }
205 }
206
207 void SimpleCallback(bool result,
208                     const std::string & reason,
209                     void * /*data*/)
210 {
211 if (!result)
212     {
213     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
214     return;
215     }
216 std::cout << "Success.\n";
217 }
218
219 void GetUsersCallback(bool result,
220                       const std::string & reason,
221                       const std::vector<STG::GET_USER::INFO> & info,
222                       void * /*data*/)
223 {
224 if (!result)
225     {
226     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
227     return;
228     }
229 std::cout << "Users:\n";
230 for (size_t i = 0; i < info.size(); ++i)
231     PrintUser(info[i], 1);
232 }
233
234 void GetUserCallback(bool result,
235                      const std::string & reason,
236                      const STG::GET_USER::INFO & info,
237                      void * /*data*/)
238 {
239 if (!result)
240     {
241     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
242     return;
243     }
244 PrintUser(info);
245 }
246
247 bool GetUsersFunction(const SGCONF::CONFIG & config,
248                       const std::string & /*arg*/,
249                       const std::map<std::string, std::string> & /*options*/)
250 {
251 STG::SERVCONF proto(config.server.data(),
252                     config.port.data(),
253                     config.userName.data(),
254                     config.userPass.data());
255 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
256 }
257
258 bool GetUserFunction(const SGCONF::CONFIG & config,
259                      const std::string & arg,
260                      const std::map<std::string, std::string> & /*options*/)
261 {
262 STG::SERVCONF proto(config.server.data(),
263                     config.port.data(),
264                     config.userName.data(),
265                     config.userPass.data());
266 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
267 }
268
269 bool DelUserFunction(const SGCONF::CONFIG & config,
270                      const std::string & arg,
271                      const std::map<std::string, std::string> & /*options*/)
272 {
273 STG::SERVCONF proto(config.server.data(),
274                     config.port.data(),
275                     config.userName.data(),
276                     config.userPass.data());
277 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
278 }
279
280 bool AddUserFunction(const SGCONF::CONFIG & config,
281                      const std::string & arg,
282                      const std::map<std::string, std::string> & options)
283 {
284 USER_CONF_RES conf;
285 SGCONF::MaybeSet(options, "password", conf.password);
286 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
287 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
288 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
289 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
290 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
291 SGCONF::MaybeSet(options, "address", conf.address);
292 SGCONF::MaybeSet(options, "phone", conf.phone);
293 SGCONF::MaybeSet(options, "email", conf.email);
294 SGCONF::MaybeSet(options, "note", conf.note);
295 SGCONF::MaybeSet(options, "name", conf.realName);
296 SGCONF::MaybeSet(options, "corp", conf.corp);
297 SGCONF::MaybeSet(options, "services", conf.services, ConvStringList);
298 SGCONF::MaybeSet(options, "group", conf.group);
299 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
300 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
301 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
302 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
303 USER_STAT_RES stat;
304 SGCONF::MaybeSet(options, "cash", stat.cash);
305 SGCONF::MaybeSet(options, "free", stat.freeMb);
306 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
307 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
308 STG::SERVCONF proto(config.server.data(),
309                     config.port.data(),
310                     config.userName.data(),
311                     config.userPass.data());
312 return proto.AddUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
313 }
314
315 bool ChgUserFunction(const SGCONF::CONFIG & config,
316                      const std::string & arg,
317                      const std::map<std::string, std::string> & options)
318 {
319 USER_CONF_RES conf;
320 SGCONF::MaybeSet(options, "password", conf.password);
321 SGCONF::MaybeSet(options, "passive", conf.passive, ConvBool);
322 SGCONF::MaybeSet(options, "disabled", conf.disabled, ConvBool);
323 SGCONF::MaybeSet(options, "disable-detail-stat", conf.disabledDetailStat, ConvBool);
324 SGCONF::MaybeSet(options, "always-online", conf.alwaysOnline, ConvBool);
325 SGCONF::MaybeSet(options, "tariff", conf.tariffName);
326 SGCONF::MaybeSet(options, "address", conf.address);
327 SGCONF::MaybeSet(options, "phone", conf.phone);
328 SGCONF::MaybeSet(options, "email", conf.email);
329 SGCONF::MaybeSet(options, "note", conf.note);
330 SGCONF::MaybeSet(options, "name", conf.realName);
331 SGCONF::MaybeSet(options, "corp", conf.corp);
332 SGCONF::MaybeSet(options, "services", conf.services, ConvStringList);
333 SGCONF::MaybeSet(options, "group", conf.group);
334 SGCONF::MaybeSet(options, "next-tariff", conf.nextTariff);
335 SGCONF::MaybeSet(options, "user-data", conf.userdata, ConvStringList);
336 SGCONF::MaybeSet(options, "credit-expire", conf.creditExpire, ConvCreditExpire);
337 SGCONF::MaybeSet(options, "ips", conf.ips, ConvIPs);
338 USER_STAT_RES stat;
339 SGCONF::MaybeSet(options, "cash", stat.cash);
340 SGCONF::MaybeSet(options, "free", stat.freeMb);
341 SGCONF::MaybeSet(options, "session-traffic", stat, ConvSessionTraff);
342 SGCONF::MaybeSet(options, "month-traffic", stat, ConvMonthTraff);
343 STG::SERVCONF proto(config.server.data(),
344                     config.port.data(),
345                     config.userName.data(),
346                     config.userPass.data());
347 return proto.ChgUser(arg, conf, stat, SimpleCallback, NULL) == STG::st_ok;
348 }
349
350 bool CheckUserFunction(const SGCONF::CONFIG & config,
351                        const std::string & arg,
352                        const std::map<std::string, std::string> & options)
353 {
354 std::map<std::string, std::string>::const_iterator it(options.find("password"));
355 if (it == options.end())
356     throw SGCONF::ACTION::ERROR("Password is not specified.");
357 STG::SERVCONF proto(config.server.data(),
358                     config.port.data(),
359                     config.userName.data(),
360                     config.userPass.data());
361 return proto.CheckUser(arg, it->second, SimpleCallback, NULL) == STG::st_ok;
362 }
363
364 bool SendMessageFunction(const SGCONF::CONFIG & config,
365                          const std::string & /*arg*/,
366                          const std::map<std::string, std::string> & options)
367 {
368 std::map<std::string, std::string>::const_iterator it(options.find("logins"));
369 if (it == options.end())
370     throw SGCONF::ACTION::ERROR("Logins are not specified.");
371 std::string logins = it->second;
372 for (size_t i = 0; i < logins.length(); ++i)
373     if (logins[i] == ',')
374         logins[i] = ':';
375 it = options.find("text");
376 if (it == options.end())
377     throw SGCONF::ACTION::ERROR("Message text is not specified.");
378 std::string text = it->second;
379 STG::SERVCONF proto(config.server.data(),
380                     config.port.data(),
381                     config.userName.data(),
382                     config.userPass.data());
383 return proto.SendMessage(logins, text, SimpleCallback, NULL) == STG::st_ok;
384 }
385
386 } // namespace anonymous
387
388 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
389 {
390 std::vector<API_ACTION::PARAM> params(GetUserParams());
391 blocks.Add("User management options")
392       .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
393       .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", GetUserFunction), "get user")
394       .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", params, AddUserFunction), "add user")
395       .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "del user")
396       .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", params, ChgUserFunction), "change user")
397       .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", GetCheckParams(), CheckUserFunction), "check user existance and credentials")
398       .Add("send-message", SGCONF::MakeAPIAction(commands, GetMessageParams(), SendMessageFunction), "send message");
399 }