]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
Added suboptions to tariffs and admins.
[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)       << "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)
54     {
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";
60     }
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())
65     {
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";
69     }
70 }
71
72 void SimpleCallback(bool result,
73                     const std::string & reason,
74                     void * /*data*/)
75 {
76 if (!result)
77     {
78     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
79     return;
80     }
81 std::cout << "Success.\n";
82 }
83
84 void GetUsersCallback(bool result,
85                       const std::string & reason,
86                       const std::vector<STG::GET_USER::INFO> & info,
87                       void * /*data*/)
88 {
89 if (!result)
90     {
91     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
92     return;
93     }
94 std::cout << "Users:\n";
95 for (size_t i = 0; i < info.size(); ++i)
96     PrintUser(info[i], 1);
97 }
98
99 void GetUserCallback(bool result,
100                      const std::string & reason,
101                      const STG::GET_USER::INFO & info,
102                      void * /*data*/)
103 {
104 if (!result)
105     {
106     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
107     return;
108     }
109 PrintUser(info);
110 }
111
112 bool GetUsersFunction(const SGCONF::CONFIG & config,
113                       const std::string & /*arg*/,
114                       const std::map<std::string, std::string> & /*options*/)
115 {
116 STG::SERVCONF proto(config.server.data(),
117                     config.port.data(),
118                     config.userName.data(),
119                     config.userPass.data());
120 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
121 }
122
123 bool GetUserFunction(const SGCONF::CONFIG & config,
124                      const std::string & arg,
125                      const std::map<std::string, std::string> & /*options*/)
126 {
127 STG::SERVCONF proto(config.server.data(),
128                     config.port.data(),
129                     config.userName.data(),
130                     config.userPass.data());
131 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
132 }
133
134 bool DelUserFunction(const SGCONF::CONFIG & config,
135                      const std::string & arg,
136                      const std::map<std::string, std::string> & /*options*/)
137 {
138 STG::SERVCONF proto(config.server.data(),
139                     config.port.data(),
140                     config.userName.data(),
141                     config.userPass.data());
142 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
143 }
144
145 bool AddUserFunction(const SGCONF::CONFIG & config,
146                      const std::string & arg,
147                      const std::map<std::string, std::string> & /*options*/)
148 {
149 // TODO
150 std::cerr << "Unimplemented.\n";
151 return false;
152 }
153
154 bool ChgUserFunction(const SGCONF::CONFIG & config,
155                      const std::string & arg,
156                      const std::map<std::string, std::string> & options)
157 {
158 // TODO
159 std::cerr << "Unimplemented.\n";
160 return false;
161 }
162
163 bool CheckUserFunction(const SGCONF::CONFIG & config,
164                        const std::string & arg,
165                        const std::map<std::string, std::string> & options)
166 {
167 // TODO
168 std::cerr << "Unimplemented.\n";
169 return false;
170 }
171
172 bool SendMessageFunction(const SGCONF::CONFIG & config,
173                          const std::string & arg,
174                          const std::map<std::string, std::string> & options)
175 {
176 // TODO
177 std::cerr << "Unimplemented.\n";
178 return false;
179 }
180
181 } // namespace anonymous
182
183 void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
184 {
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>", GetUserFunction), "get user")
188       .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", AddUserFunction), "add user")
189       .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "del user")
190       .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", ChgUserFunction), "change user")
191       .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", CheckUserFunction), "check user existance and credentials")
192       .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", SendMessageFunction), "send message");
193 }