]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
0fb68544e1584961a5aad457729481e91eaf51fe
[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.lastCash << "\n"
29           << Indent(level)       << "prepaid traffic: " << info.prepaidTraff << "\n"
30           << Indent(level)       << "disabled: " << (info.disabled ? "t" : "f") << "\n"
31           << Indent(level)       << "passive: " << (info.passive ? "t" : "f") << "\n"
32           << Indent(level)       << "disabled detail stat: " << (info.disableDetailStat ? "t" : "f") << "\n"
33           << Indent(level)       << "connected: " << (info.connected ? "t" : "f") << "\n"
34           << Indent(level)       << "always on-line: " << (info.alwaysOnline ? "t" : "f") << "\n"
35           << Indent(level)       << "IP: " << inet_ntostring(info.ip) << "\n"
36           << Indent(level)       << "IPs: " << info.ips << "\n"
37           << Indent(level)       << "tariff: " << info.tariff << "\n"
38           << Indent(level)       << "group: " << info.group << "\n"
39           << Indent(level)       << "note: " << info.note << "\n"
40           << Indent(level)       << "email: " << info.email << "\n"
41           << Indent(level)       << "name: " << info.name << "\n"
42           << Indent(level)       << "address: " << info.address << "\n"
43           << Indent(level)       << "phone: " << info.phone << "\n"
44           << Indent(level)       << "free mb: " << info.stat.freeMb << "\n"
45           << Indent(level)       << "traffic:\n";
46 for (size_t i = 0; i < DIR_NUM; ++i)
47     {
48     std::cout << Indent(level + 1, true) << "dir: " << i << "\n"
49               << Indent(level + 1)       << "session upload: " << info.stat.su[i] << "\n"
50               << Indent(level + 1)       << "session download: " << info.stat.sd[i] << "\n"
51               << Indent(level + 1)       << "month upload: " << info.stat.mu[i] << "\n"
52               << Indent(level + 1)       << "month download: " << info.stat.md[i] << "\n";
53     }
54 std::cout << Indent(level)       << "user data:\n";
55 for (size_t i = 0; i < USERDATA_NUM; ++i)
56     std::cout << Indent(level + 1, true) << "user data " << i << ": " << info.userData[i] << "\n";
57 }
58
59 void SimpleCallback(bool result,
60                     const std::string & reason,
61                     void * /*data*/)
62 {
63 if (!result)
64     {
65     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
66     return;
67     }
68 std::cout << "Success.\n";
69 }
70
71 void GetUsersCallback(bool result,
72                       const std::string & reason,
73                       const std::vector<STG::GET_USER::INFO> & info,
74                       void * /*data*/)
75 {
76 if (!result)
77     {
78     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
79     return;
80     }
81 std::cout << "Users:\n";
82 for (size_t i = 0; i < info.size(); ++i)
83     PrintUser(info[i], 1);
84 }
85
86 void GetUserCallback(bool result,
87                      const std::string & reason,
88                      const STG::GET_USER::INFO & info,
89                      void * /*data*/)
90 {
91 if (!result)
92     {
93     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
94     return;
95     }
96 PrintUser(info);
97 }
98
99 } // namespace anonymous
100
101 bool SGCONF::GetUsersFunction(const SGCONF::CONFIG & config,
102                                 const std::string & /*arg*/,
103                                 const std::map<std::string, std::string> & /*options*/)
104 {
105 STG::SERVCONF proto(config.server.data(),
106                     config.port.data(),
107                     config.userName.data(),
108                     config.userPass.data());
109 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
110 }
111
112 bool SGCONF::GetUserFunction(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.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
121 }
122
123 bool SGCONF::DelUserFunction(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.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
132 }
133
134 bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config,
135                                const std::string & arg,
136                                const std::map<std::string, std::string> & /*options*/)
137 {
138 // TODO
139 std::cerr << "Unimplemented.\n";
140 return false;
141 }
142
143 bool SGCONF::ChgUserFunction(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 }