3 #include "api_action.h"
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/common.h"
18 std::string Indent(size_t level, bool dash = false)
22 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
25 void PrintUser(const STG::GET_USER::INFO & info, size_t level = 0)
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)
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";
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())
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";
71 std::vector<SGCONF::API_ACTION::PARAM> GetUserParams()
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"));
98 std::vector<SGCONF::API_ACTION::PARAM> GetCheckParams()
100 std::vector<SGCONF::API_ACTION::PARAM> params;
101 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "\tuser's password"));
105 std::vector<SGCONF::API_ACTION::PARAM> GetMessageParams()
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"));
113 void SimpleCallback(bool result,
114 const std::string & reason,
119 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
122 std::cout << "Success.\n";
125 void GetUsersCallback(bool result,
126 const std::string & reason,
127 const std::vector<STG::GET_USER::INFO> & info,
132 std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
135 std::cout << "Users:\n";
136 for (size_t i = 0; i < info.size(); ++i)
137 PrintUser(info[i], 1);
140 void GetUserCallback(bool result,
141 const std::string & reason,
142 const STG::GET_USER::INFO & info,
147 std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
153 bool GetUsersFunction(const SGCONF::CONFIG & config,
154 const std::string & /*arg*/,
155 const std::map<std::string, std::string> & /*options*/)
157 STG::SERVCONF proto(config.server.data(),
159 config.userName.data(),
160 config.userPass.data());
161 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
164 bool GetUserFunction(const SGCONF::CONFIG & config,
165 const std::string & arg,
166 const std::map<std::string, std::string> & /*options*/)
168 STG::SERVCONF proto(config.server.data(),
170 config.userName.data(),
171 config.userPass.data());
172 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
175 bool DelUserFunction(const SGCONF::CONFIG & config,
176 const std::string & arg,
177 const std::map<std::string, std::string> & /*options*/)
179 STG::SERVCONF proto(config.server.data(),
181 config.userName.data(),
182 config.userPass.data());
183 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
186 bool AddUserFunction(const SGCONF::CONFIG & config,
187 const std::string & arg,
188 const std::map<std::string, std::string> & /*options*/)
191 std::cerr << "Unimplemented.\n";
195 bool ChgUserFunction(const SGCONF::CONFIG & config,
196 const std::string & arg,
197 const std::map<std::string, std::string> & options)
200 std::cerr << "Unimplemented.\n";
204 bool CheckUserFunction(const SGCONF::CONFIG & config,
205 const std::string & arg,
206 const std::map<std::string, std::string> & options)
209 std::cerr << "Unimplemented.\n";
213 bool SendMessageFunction(const SGCONF::CONFIG & config,
214 const std::string & arg,
215 const std::map<std::string, std::string> & options)
218 std::cerr << "Unimplemented.\n";
222 } // namespace anonymous
224 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
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");