]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
3958869c0eb6ab3ed80ac2c40f3d7d7e86d442e1
[stg.git] / projects / sgconf / users.cpp
1 #include "users.h"
2
3 #include "config.h"
4
5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/common.h"
8
9 #include <iostream>
10
11 namespace
12 {
13
14 std::string Indent(size_t level, bool dash = false)
15 {
16 if (level == 0)
17     return "";
18 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
19 }
20
21 void PrintUser(const STG::GET_USER::INFO & info, size_t level = 0)
22 {
23 std::cout << Indent(level, true) << "login: " << info.login << "\n"
24           << Indent(level)       << "password: " << info.password << "\n"
25           << Indent(level)       << "cash: " << info.cash << "\n"
26           << Indent(level)       << "credit: " << info.credit << "\n"
27           << Indent(level)       << "credit expire: " << TimeToString(info.creditExpire) << "\n"
28           << Indent(level)       << "last cash add: " << info.lastCashAdd << "\n"
29           << Indent(level)       << "last cash add time: " << TimeToString(info.lastCashAddTime) << "\n"
30           << Indent(level)       << "prepaid traffic: " << info.prepaidTraff << "\n"
31           << Indent(level)       << "disabled: " << (info.disabled ? "t" : "f") << "\n"
32           << Indent(level)       << "passive: " << (info.passive ? "t" : "f") << "\n"
33           << Indent(level)       << "disabled detail stat: " << (info.disableDetailStat ? "t" : "f") << "\n"
34           << Indent(level)       << "connected: " << (info.connected ? "t" : "f") << "\n"
35           << Indent(level)       << "always on-line: " << (info.alwaysOnline ? "t" : "f") << "\n"
36           << Indent(level)       << "IP: " << inet_ntostring(info.ip) << "\n"
37           << Indent(level)       << "IPs: " << info.ips << "\n"
38           << Indent(level)       << "tariff: " << info.tariff << "\n"
39           << Indent(level)       << "group: " << info.group << "\n"
40           << Indent(level)       << "note: " << info.note << "\n"
41           << Indent(level)       << "email: " << info.email << "\n"
42           << Indent(level)       << "name: " << info.name << "\n"
43           << Indent(level)       << "address: " << info.address << "\n"
44           << Indent(level)       << "phone: " << info.phone << "\n"
45           << Indent(level)       << "free mb: " << info.stat.freeMb << "\n"
46           << Indent(level)       << "last ping time: " << TimeToString(info.pingTime) << "\n"
47           << Indent(level)       << "last activity time: " << TimeToString(info.lastActivityTime) << "\n"
48           << Indent(level)       << "traffic:\n";
49 for (size_t i = 0; i < DIR_NUM; ++i)
50     {
51     std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
52               << Indent(level + 1)       << "session upload: " << info.stat.su[i] << "\n"
53               << Indent(level + 1)       << "session download: " << info.stat.sd[i] << "\n"
54               << Indent(level + 1)       << "month upload: " << info.stat.mu[i] << "\n"
55               << Indent(level + 1)       << "month download: " << info.stat.md[i] << "\n";
56     }
57 std::cout << Indent(level)       << "user data:\n";
58 for (size_t i = 0; i < USERDATA_NUM; ++i)
59     std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
60 if (!info.authBy.empty())
61     {
62     std::cout << Indent(level) << "auth by:\n";
63     for (size_t i = 0; i < info.authBy.size(); ++i)
64         std::cout << Indent(level + 1, true) << info.authBy[i] << "\n";
65     }
66 }
67
68 void SimpleCallback(bool result,
69                     const std::string & reason,
70                     void * /*data*/)
71 {
72 if (!result)
73     {
74     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
75     return;
76     }
77 std::cout << "Success.\n";
78 }
79
80 void GetUsersCallback(bool result,
81                       const std::string & reason,
82                       const std::vector<STG::GET_USER::INFO> & info,
83                       void * /*data*/)
84 {
85 if (!result)
86     {
87     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
88     return;
89     }
90 std::cout << "Users:\n";
91 for (size_t i = 0; i < info.size(); ++i)
92     PrintUser(info[i], 1);
93 }
94
95 void GetUserCallback(bool result,
96                      const std::string & reason,
97                      const STG::GET_USER::INFO & info,
98                      void * /*data*/)
99 {
100 if (!result)
101     {
102     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
103     return;
104     }
105 PrintUser(info);
106 }
107
108 } // namespace anonymous
109
110 bool SGCONF::GetUsersFunction(const SGCONF::CONFIG & config,
111                                 const std::string & /*arg*/,
112                                 const std::map<std::string, std::string> & /*options*/)
113 {
114 STG::SERVCONF proto(config.server.data(),
115                     config.port.data(),
116                     config.userName.data(),
117                     config.userPass.data());
118 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
119 }
120
121 bool SGCONF::GetUserFunction(const SGCONF::CONFIG & config,
122                                const std::string & arg,
123                                const std::map<std::string, std::string> & /*options*/)
124 {
125 STG::SERVCONF proto(config.server.data(),
126                     config.port.data(),
127                     config.userName.data(),
128                     config.userPass.data());
129 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
130 }
131
132 bool SGCONF::DelUserFunction(const SGCONF::CONFIG & config,
133                                const std::string & arg,
134                                const std::map<std::string, std::string> & /*options*/)
135 {
136 STG::SERVCONF proto(config.server.data(),
137                     config.port.data(),
138                     config.userName.data(),
139                     config.userPass.data());
140 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
141 }
142
143 bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config,
144                              const std::string & arg,
145                              const std::map<std::string, std::string> & /*options*/)
146 {
147 // TODO
148 std::cerr << "Unimplemented.\n";
149 return false;
150 }
151
152 bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config,
153                              const std::string & arg,
154                              const std::map<std::string, std::string> & options)
155 {
156 // TODO
157 std::cerr << "Unimplemented.\n";
158 return false;
159 }
160
161 bool SGCONF::CheckUserFunction(const SGCONF::CONFIG & config,
162                                const std::string & arg,
163                                const std::map<std::string, std::string> & options)
164 {
165 // TODO
166 std::cerr << "Unimplemented.\n";
167 return false;
168 }
169
170 bool SGCONF::SendMessageFunction(const SGCONF::CONFIG & config,
171                                  const std::string & arg,
172                                  const std::map<std::string, std::string> & options)
173 {
174 // TODO
175 std::cerr << "Unimplemented.\n";
176 return false;
177 }