]> git.stg.codes - stg.git/blob - projects/sgconf/users.cpp
61d367366ca1680aa0d8f0ab838026d0feb4b6ec
[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 }
61
62 void SimpleCallback(bool result,
63                     const std::string & reason,
64                     void * /*data*/)
65 {
66 if (!result)
67     {
68     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
69     return;
70     }
71 std::cout << "Success.\n";
72 }
73
74 void GetUsersCallback(bool result,
75                       const std::string & reason,
76                       const std::vector<STG::GET_USER::INFO> & info,
77                       void * /*data*/)
78 {
79 if (!result)
80     {
81     std::cerr << "Failed to get user list. Reason: '" << reason << "'." << std::endl;
82     return;
83     }
84 std::cout << "Users:\n";
85 for (size_t i = 0; i < info.size(); ++i)
86     PrintUser(info[i], 1);
87 }
88
89 void GetUserCallback(bool result,
90                      const std::string & reason,
91                      const STG::GET_USER::INFO & info,
92                      void * /*data*/)
93 {
94 if (!result)
95     {
96     std::cerr << "Failed to get user. Reason: '" << reason << "'." << std::endl;
97     return;
98     }
99 PrintUser(info);
100 }
101
102 } // namespace anonymous
103
104 bool SGCONF::GetUsersFunction(const SGCONF::CONFIG & config,
105                                 const std::string & /*arg*/,
106                                 const std::map<std::string, std::string> & /*options*/)
107 {
108 STG::SERVCONF proto(config.server.data(),
109                     config.port.data(),
110                     config.userName.data(),
111                     config.userPass.data());
112 return proto.GetUsers(GetUsersCallback, NULL) == STG::st_ok;
113 }
114
115 bool SGCONF::GetUserFunction(const SGCONF::CONFIG & config,
116                                const std::string & arg,
117                                const std::map<std::string, std::string> & /*options*/)
118 {
119 STG::SERVCONF proto(config.server.data(),
120                     config.port.data(),
121                     config.userName.data(),
122                     config.userPass.data());
123 return proto.GetUser(arg, GetUserCallback, NULL) == STG::st_ok;
124 }
125
126 bool SGCONF::DelUserFunction(const SGCONF::CONFIG & config,
127                                const std::string & arg,
128                                const std::map<std::string, std::string> & /*options*/)
129 {
130 STG::SERVCONF proto(config.server.data(),
131                     config.port.data(),
132                     config.userName.data(),
133                     config.userPass.data());
134 return proto.DelUser(arg, SimpleCallback, NULL) == STG::st_ok;
135 }
136
137 bool SGCONF::AddUserFunction(const SGCONF::CONFIG & config,
138                                const std::string & arg,
139                                const std::map<std::string, std::string> & /*options*/)
140 {
141 // TODO
142 std::cerr << "Unimplemented.\n";
143 return false;
144 }
145
146 bool SGCONF::ChgUserFunction(const SGCONF::CONFIG & config,
147                                const std::string & arg,
148                                const std::map<std::string, std::string> & options)
149 {
150 // TODO
151 std::cerr << "Unimplemented.\n";
152 return false;
153 }