]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Added admins manipulation interface.
[stg.git] / stglibs / srvconf.lib / servconf.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #include "stg/servconf.h"
22
23 #include "netunit.h"
24
25 #include "parsers/server_info.h"
26
27 #include "parsers/get_admins.h"
28 #include "parsers/get_admin.h"
29 #include "parsers/chg_admin.h"
30 #include "parsers/add_admin.h"
31 #include "parsers/del_admin.h"
32
33 #include "parsers/auth_by.h"
34 #include "parsers/check_user.h"
35 #include "parsers/get_users.h"
36 #include "parsers/get_user.h"
37 #include "parsers/chg_user.h"
38 #include "parsers/send_message.h"
39
40 #include "parsers/base.h"
41
42 #include "stg/common.h"
43
44 #include <cstdio>
45 #include <cstring>
46
47 #include <expat.h>
48
49 using namespace STG;
50
51 class SERVCONF::IMPL
52 {
53 public:
54     IMPL(const std::string & server, uint16_t port,
55          const std::string & login, const std::string & password);
56
57     const std::string & GetStrError() const;
58     static void Start(void * data, const char * el, const char ** attr);
59     static void End(void * data, const char * el);
60
61     template <class P, typename C>
62     int Exec(const std::string & request, C callback, void * data)
63     {
64         P cp(callback, data);
65         return ExecImpl(request, cp);
66     }
67
68 private:
69     NETTRANSACT nt;
70
71     std::string errorMsg;
72     XML_Parser parser;
73
74     static bool AnsRecv(void * data, const std::string & chunk, bool final);
75     int ExecImpl(const std::string & request, PARSER & cp);
76 };
77
78 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
79 {
80 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
81
82 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
83     {
84     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
85               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
86               XML_ErrorString(XML_GetErrorCode(sc->parser)));
87     printf("%s\n", sc->errorMsg.c_str());
88     return false;
89     }
90
91 return true;
92 }
93
94 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
95                    const std::string & login, const std::string & password)
96     : pImpl(new IMPL(server, port, login, password))
97 {
98 }
99
100 SERVCONF::~SERVCONF()
101 {
102 delete pImpl;
103 }
104
105 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
106 {
107 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
108 }
109
110 // -- Admins --
111
112 int SERVCONF::GetAdmins(GET_ADMINS::CALLBACK f, void * data)
113 {
114 return pImpl->Exec<GET_ADMINS::PARSER>("<GetAdmins/>", f, data);
115 }
116
117 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
118 {
119 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
120 }
121
122 int SERVCONF::ChgAdmin(const std::string & login, const ADMIN_CONF_RES & conf, CHG_ADMIN::CALLBACK f, void * data)
123 {
124 return pImpl->Exec<CHG_ADMIN::PARSER>("<ChgAdmin login=\"" + login + "\">" + CHG_ADMIN::Serialize(conf) + "</ChgAdmin>", f, data);
125 }
126
127 int SERVCONF::AddAdmin(const std::string & login, const ADMIN_CONF & conf, ADD_ADMIN::CALLBACK f, void * data)
128 {
129 int res = pImpl->Exec<ADD_ADMIN::PARSER>("<AddAdmin login=\"" + login + "\"/>", f, data);
130 if (res != st_ok)
131     return res;
132 return pImpl->Exec<CHG_ADMIN::PARSER>("<ChgAdmin login=\"" + login + "\">" + CHG_ADMIN::Serialize(conf) + "</ChgAdmin>", f, data);
133 }
134
135 int SERVCONF::DelAdmin(const std::string & login, DEL_ADMIN::CALLBACK f, void * data)
136 {
137 return pImpl->Exec<DEL_ADMIN::PARSER>("<DelAdmin login=\"" + login + "\"/>", f, data);
138 }
139
140 // -- Users --
141
142 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
143 {
144 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
145 }
146
147 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
148 {
149 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
150 }
151
152 int SERVCONF::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
153 {
154 return pImpl->Exec<CHG_USER::PARSER>(request, f, data);
155 }
156
157 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
158 {
159 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
160 }
161
162 int SERVCONF::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
163 {
164 return pImpl->Exec<SEND_MESSAGE::PARSER>(request, f, data);
165 }
166
167 int SERVCONF::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
168 {
169 return pImpl->Exec<CHECK_USER::PARSER>("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
170 }
171
172 const std::string & SERVCONF::GetStrError() const
173 {
174 return pImpl->GetStrError();
175 }
176
177 //-----------------------------------------------------------------------------
178 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
179                      const std::string & login, const std::string & password)
180     : nt( server, port, login, password )
181 {
182 parser = XML_ParserCreate(NULL);
183 nt.SetRxCallback(this, AnsRecv);
184 }
185 //-----------------------------------------------------------------------------
186 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
187 {
188 PARSER * currParser = static_cast<PARSER *>(data);
189 currParser->ParseStart(el, attr);
190 }
191 //-----------------------------------------------------------------------------
192 void SERVCONF::IMPL::End(void * data, const char * el)
193 {
194 PARSER * currParser = static_cast<PARSER *>(data);
195 currParser->ParseEnd(el);
196 }
197 //-----------------------------------------------------------------------------
198 const std::string & SERVCONF::IMPL::GetStrError() const
199 {
200 return errorMsg;
201 }
202 //-----------------------------------------------------------------------------
203 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
204 {
205 XML_ParserReset(parser, NULL);
206 XML_SetElementHandler(parser, Start, End);
207 XML_SetUserData(parser, &cp);
208
209 int ret = 0;
210 if ((ret = nt.Connect()) != st_ok)
211     {
212     errorMsg = nt.GetError();
213     return ret;
214     }
215 if ((ret = nt.Transact(request.c_str())) != st_ok)
216     {
217     errorMsg = nt.GetError();
218     return ret;
219     }
220 if ((ret = nt.Disconnect()) != st_ok)
221     {
222     errorMsg = nt.GetError();
223     return ret;
224     }
225
226 return st_ok;
227 }