]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
9c6c30b5c4c02f94150dd71031e4b0b0a468a639
[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
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/common.h"
10
11 #include <iostream>
12 #include <string>
13 #include <map>
14
15 namespace
16 {
17
18 std::string Indent(size_t level, bool dash = false)
19 {
20 if (level == 0)
21     return "";
22 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
23 }
24
25 void PrintUser(const STG::GET_USER::INFO & info, size_t level = 0)
26 {
27 std::cout << Indent(level, true) << "login: " << info.login << "\n"
28           << Indent(level)       << "password: " << info.password << "\n"
29           << Indent(level)       << "cash: " << info.cash << "\n"
30           << Indent(level)       << "credit: " << info.credit << "\n"
31           << Indent(level)       << "credit expire: " << TimeToString(info.creditExpire) << "\n"
32           << Indent(level)       << "last cash add: " << info.lastCashAdd << "\n"
33           << Indent(level)       << "last cash add time: " << TimeToString(info.lastCashAddTime) << "\n"
34           << Indent(level)       << "prepaid traffic: " << info.prepaidTraff << "\n"
35           << Indent(level)       << "disabled: " << (info.disabled ? "t" : "f") << "\n"
36           << Indent(level)       << "passive: " << (info.passive ? "t" : "f") << "\n"
37           << Indent(level)       << "disabled detail stat: " << (info.disableDetailStat ? "t" : "f") << "\n"
38           << Indent(level)       << "connected: " << (info.connected ? "t" : "f") << "\n"
39           << Indent(level)       << "always on-line: " << (info.alwaysOnline ? "t" : "f") << "\n"
40           << Indent(level)       << "IP: " << inet_ntostring(info.ip) << "\n"
41           << Indent(level)       << "IPs: " << info.ips << "\n"
42           << Indent(level)       << "tariff: " << info.tariff << "\n"
43           << Indent(level)       << "group: " << info.group << "\n"
44           << Indent(level)       << "note: " << info.note << "\n"
45           << Indent(level)       << "email: " << info.email << "\n"
46           << Indent(level)       << "name: " << info.name << "\n"
47           << Indent(level)       << "address: " << info.address << "\n"
48           << Indent(level)       << "phone: " << info.phone << "\n"
49           << Indent(level)       << "last ping time: " << TimeToString(info.pingTime) << "\n"
50           << Indent(level)       << "last activity time: " << TimeToString(info.lastActivityTime) << "\n"
51           << Indent(level)       << "traffic:\n";
52 for (size_t i = 0; i < DIR_NUM; ++i)
53     {
54     std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
55               << Indent(level + 1)       << "session upload: " << info.stat.su[i] << "\n"
56               << Indent(level + 1)       << "session download: " << info.stat.sd[i] << "\n"
57               << Indent(level + 1)       << "month upload: " << info.stat.mu[i] << "\n"
58               << Indent(level + 1)       << "month download: " << info.stat.md[i] << "\n";
59     }
60 std::cout << Indent(level)       << "user data:\n";
61 for (size_t i = 0; i < USERDATA_NUM; ++i)
62     std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
63 if (!info.authBy.empty())
64     {
65     std::cout << Indent(level) << "auth by:\n";
66     for (size_t i = 0; i < info.authBy.size(); ++i)
67         std::cout << Indent(level + 1, true) << info.authBy[i] << "\n";
68     }
69 }
70
71 std::vector<SGCONF::API_ACTION::PARAM> GetUserParams()
72 {
73 std::vector<SGCONF::API_ACTION::PARAM> params;
74 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
75 params.push_back(SGCONF::API_ACTION::PARAM("cash", "<cash>", "\t\tuser's cash"));
76 params.push_back(SGCONF::API_ACTION::PARAM("credit", "<amount>", "\tuser's credit"));
77 params.push_back(SGCONF::API_ACTION::PARAM("credit-expire", "<date>", "\tcredit expiration"));
78 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
79 params.push_back(SGCONF::API_ACTION::PARAM("disabled", "<flag>", "\tdisable user (y|n)"));
80 params.push_back(SGCONF::API_ACTION::PARAM("passive", "<flag>", "\tmake user passive (y|n)"));
81 params.push_back(SGCONF::API_ACTION::PARAM("disable-detail-stat", "<flag>", "disable detail stat (y|n)"));
82 params.push_back(SGCONF::API_ACTION::PARAM("always-online", "<flag>", "\tmake user always online (y|n)"));
83 params.push_back(SGCONF::API_ACTION::PARAM("ips", "<ips>", "\t\tcoma-separated list of ips"));
84 params.push_back(SGCONF::API_ACTION::PARAM("tariff", "<tariff name>", "\tcurrent tariff"));
85 params.push_back(SGCONF::API_ACTION::PARAM("next-tariff", "<tariff name>", "tariff starting from the next month"));
86 params.push_back(SGCONF::API_ACTION::PARAM("group", "<group>", "\t\tuser's group"));
87 params.push_back(SGCONF::API_ACTION::PARAM("note", "<note>", "\t\tuser's note"));
88 params.push_back(SGCONF::API_ACTION::PARAM("email", "<email>", "\t\tuser's email"));
89 params.push_back(SGCONF::API_ACTION::PARAM("name", "<real name>", "\tuser's real name"));
90 params.push_back(SGCONF::API_ACTION::PARAM("address", "<address>", "\tuser's postal address"));
91 params.push_back(SGCONF::API_ACTION::PARAM("phone", "<phone>", "\t\tuser's phone number"));
92 params.push_back(SGCONF::API_ACTION::PARAM("session-traffic", "<up/dn, ...>", "coma-separated session upload and download"));
93 params.push_back(SGCONF::API_ACTION::PARAM("month-traffic", "<up/dn, ...>", "coma-separated month upload and download"));
94 params.push_back(SGCONF::API_ACTION::PARAM("user-data", "<value, ...>", "coma-separated user data values"));
95 return params;
96 }
97
98 std::vector<SGCONF::API_ACTION::PARAM> GetCheckParams()
99 {
100 std::vector<SGCONF::API_ACTION::PARAM> params;
101 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
102 return params;
103 }
104
105 std::vector<SGCONF::API_ACTION::PARAM> GetMessageParams()
106 {
107 std::vector<SGCONF::API_ACTION::PARAM> params;
108 params.push_back(SGCONF::API_ACTION::PARAM("logins", "<login, ...>", "\tlist of logins to send a message"));
109 params.push_back(SGCONF::API_ACTION::PARAM("text", "<text>", "\t\tmessage text"));
110 return params;
111 }
112
113 void SimpleCallback(bool result,
114                     const std::string & reason,
115                     void * /*data*/)
116 {
117 if (!result)
118     {
119     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
120     return;
121     }
122 std::cout << "Success.\n";
123 }
124
125 void GetUsersCallback(bool result,
126                       const std::string & reason,
127                       const std::vector<STG::GET_USER::INFO> & info,
128                       void * /*data*/)
129 {
130 if (!result)
131     {
132     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
133     return;
134     }
135 std::cout << "Users:\n";
136 for (size_t i = 0; i < info.size(); ++i)
137     PrintUser(info[i], 1);
138 }
139
140 void GetUserCallback(bool result,
141                      const std::string & reason,
142                      const STG::GET_USER::INFO & info,
143                      void * /*data*/)
144 {
145 if (!result)
146     {
147     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
148     return;
149     }
150 PrintUser(info);
151 }
152
153 bool GetUsersFunction(const SGCONF::CONFIG & config,
154                       const std::string & /*arg*/,
155                       const std::map<std::string, std::string> & /*options*/)
156 {
157 STG::SERVCONF proto(config.server.data(),
158                     config.port.data(),
159                     config.userName.data(),
160                     config.userPass.data());
161 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
162 }
163
164 bool GetUserFunction(const SGCONF::CONFIG & config,
165                      const std::string & arg,
166                      const std::map<std::string, std::string> & /*options*/)
167 {
168 STG::SERVCONF proto(config.server.data(),
169                     config.port.data(),
170                     config.userName.data(),
171                     config.userPass.data());
172 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
173 }
174
175 bool DelUserFunction(const SGCONF::CONFIG & config,
176                      const std::string & arg,
177                      const std::map<std::string, std::string> & /*options*/)
178 {
179 STG::SERVCONF proto(config.server.data(),
180                     config.port.data(),
181                     config.userName.data(),
182                     config.userPass.data());
183 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
184 }
185
186 bool AddUserFunction(const SGCONF::CONFIG & config,
187                      const std::string & arg,
188                      const std::map<std::string, std::string> & /*options*/)
189 {
190 // TODO
191 std::cerr << "Unimplemented.\n";
192 return false;
193 }
194
195 bool ChgUserFunction(const SGCONF::CONFIG & config,
196                      const std::string & arg,
197                      const std::map<std::string, std::string> & options)
198 {
199 // TODO
200 std::cerr << "Unimplemented.\n";
201 return false;
202 }
203
204 bool CheckUserFunction(const SGCONF::CONFIG & config,
205                        const std::string & arg,
206                        const std::map<std::string, std::string> & options)
207 {
208 // TODO
209 std::cerr << "Unimplemented.\n";
210 return false;
211 }
212
213 bool SendMessageFunction(const SGCONF::CONFIG & config,
214                          const std::string & arg,
215                          const std::map<std::string, std::string> & options)
216 {
217 // TODO
218 std::cerr << "Unimplemented.\n";
219 return false;
220 }
221
222 } // namespace anonymous
223
224 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
225 {
226 std::vector<API_ACTION::PARAM> params(GetUserParams());
227 blocks.Add("User management options")
228       .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
229       .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", GetUserFunction), "get user")
230       .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", params, AddUserFunction), "add user")
231       .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "del user")
232       .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", params, ChgUserFunction), "change user")
233       .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", GetCheckParams(), CheckUserFunction), "check user existance and credentials")
234       .Add("send-message", SGCONF::MakeAPIAction(commands, GetMessageParams(), SendMessageFunction), "send message");
235 }