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) << "free mb: " << info.stat.freeMb << "\n"
50 << Indent(level) << "last ping time: " << TimeToString(info.pingTime) << "\n"
51 << Indent(level) << "last activity time: " << TimeToString(info.lastActivityTime) << "\n"
52 << Indent(level) << "traffic:\n";
53 for (size_t i = 0; i < DIR_NUM; ++i)
55 std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
56 << Indent(level + 1) << "session upload: " << info.stat.su[i] << "\n"
57 << Indent(level + 1) << "session download: " << info.stat.sd[i] << "\n"
58 << Indent(level + 1) << "month upload: " << info.stat.mu[i] << "\n"
59 << Indent(level + 1) << "month download: " << info.stat.md[i] << "\n";
61 std::cout << Indent(level) << "user data:\n";
62 for (size_t i = 0; i < USERDATA_NUM; ++i)
63 std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
64 if (!info.authBy.empty())
66 std::cout << Indent(level) << "auth by:\n";
67 for (size_t i = 0; i < info.authBy.size(); ++i)
68 std::cout << Indent(level + 1, true) << info.authBy[i] << "\n";
72 void SimpleCallback(bool result,
73 const std::string & reason,
78 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
81 std::cout << "Success.\n";
84 void GetUsersCallback(bool result,
85 const std::string & reason,
86 const std::vector<STG::GET_USER::INFO> & info,
91 std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
94 std::cout << "Users:\n";
95 for (size_t i = 0; i < info.size(); ++i)
96 PrintUser(info[i], 1);
99 void GetUserCallback(bool result,
100 const std::string & reason,
101 const STG::GET_USER::INFO & info,
106 std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
112 bool GetUsersFunction(const SGCONF::CONFIG & config,
113 const std::string & /*arg*/,
114 const std::map<std::string, std::string> & /*options*/)
116 STG::SERVCONF proto(config.server.data(),
118 config.userName.data(),
119 config.userPass.data());
120 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
123 bool GetUserFunction(const SGCONF::CONFIG & config,
124 const std::string & arg,
125 const std::map<std::string, std::string> & /*options*/)
127 STG::SERVCONF proto(config.server.data(),
129 config.userName.data(),
130 config.userPass.data());
131 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
134 bool DelUserFunction(const SGCONF::CONFIG & config,
135 const std::string & arg,
136 const std::map<std::string, std::string> & /*options*/)
138 STG::SERVCONF proto(config.server.data(),
140 config.userName.data(),
141 config.userPass.data());
142 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
145 bool AddUserFunction(const SGCONF::CONFIG & config,
146 const std::string & arg,
147 const std::map<std::string, std::string> & /*options*/)
150 std::cerr << "Unimplemented.\n";
154 bool ChgUserFunction(const SGCONF::CONFIG & config,
155 const std::string & arg,
156 const std::map<std::string, std::string> & options)
159 std::cerr << "Unimplemented.\n";
163 bool CheckUserFunction(const SGCONF::CONFIG & config,
164 const std::string & arg,
165 const std::map<std::string, std::string> & options)
168 std::cerr << "Unimplemented.\n";
172 bool SendMessageFunction(const SGCONF::CONFIG & config,
173 const std::string & arg,
174 const std::map<std::string, std::string> & options)
177 std::cerr << "Unimplemented.\n";
181 } // namespace anonymous
183 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
185 blocks.Add("User management options")
186 .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list")
187 .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", true, GetUserFunction), "get user")
188 .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, AddUserFunction), "add user")
189 .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, DelUserFunction), "del user")
190 .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, ChgUserFunction), "change user")
191 .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, CheckUserFunction), "check user existance and credentials")
192 .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", true, SendMessageFunction), "send message");