]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Merge branch 'stg-2.409'
[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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "stg/servconf.h"
23
24 #include "netunit.h"
25
26 #include "parsers/simple.h"
27 #include "parsers/get_container.h"
28
29 #include "parsers/server_info.h"
30
31 #include "parsers/get_admin.h"
32 #include "parsers/chg_admin.h"
33
34 #include "parsers/get_tariff.h"
35 #include "parsers/chg_tariff.h"
36
37 #include "parsers/auth_by.h"
38 #include "parsers/get_user.h"
39 #include "parsers/chg_user.h"
40
41 #include "parsers/get_service.h"
42 #include "parsers/chg_service.h"
43
44 #include "parsers/get_corp.h"
45 #include "parsers/chg_corp.h"
46
47 #include "parsers/base.h"
48
49 #include "stg/common.h"
50
51 #include <cstdio>
52 #include <cstring>
53 #include <clocale>
54
55 #include <expat.h>
56 #include <langinfo.h>
57
58 using namespace STG;
59
60 class SERVCONF::IMPL
61 {
62 public:
63     IMPL(const std::string & server, uint16_t port,
64          const std::string & login, const std::string & password);
65     IMPL(const std::string & server, uint16_t port,
66          const std::string & localAddress, uint16_t localPort,
67          const std::string & login, const std::string & password);
68     ~IMPL() { XML_ParserFree(parser); }
69
70     const std::string & GetStrError() const;
71     static void Start(void * data, const char * el, const char ** attr);
72     static void End(void * data, const char * el);
73
74     int RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data);
75
76     template <class P, typename C>
77     int Exec(const std::string & request, C callback, void * data)
78     {
79         P cp(callback, data, encoding);
80         return ExecImpl(request, cp);
81     }
82
83     template <class P, typename C>
84     int Exec(const std::string & tag, const std::string & request, C callback, void * data)
85     {
86         P cp(tag, callback, data, encoding);
87         return ExecImpl(request, cp);
88     }
89
90     const std::string & Encoding() const { return encoding; }
91
92 private:
93     NETTRANSACT nt;
94
95     std::string encoding;
96     std::string errorMsg;
97     XML_Parser parser;
98
99     static bool ParserRecv(const std::string & chunk, bool final, void * data);
100     static bool SimpleRecv(const std::string & chunk, bool final, void * data);
101     int ExecImpl(const std::string & request, PARSER & cp);
102 };
103
104 bool SERVCONF::IMPL::ParserRecv(const std::string & chunk, bool final, void * data)
105 {
106 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
107
108 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
109     {
110     strprintf(&sc->errorMsg, "XML parse error at line %d, %d: %s. Is final: %d",
111               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
112               static_cast<int>(XML_GetCurrentColumnNumber(sc->parser)),
113               XML_ErrorString(XML_GetErrorCode(sc->parser)), (int)final);
114     return false;
115     }
116
117 return true;
118 }
119
120 bool SERVCONF::IMPL::SimpleRecv(const std::string & chunk, bool /*final*/, void * data)
121 {
122 *static_cast<std::string *>(data) += chunk;
123 return true;
124 }
125
126 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
127                    const std::string & login, const std::string & password)
128     : pImpl(new IMPL(server, port, login, password))
129 {
130 }
131
132 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
133                    const std::string & localAddress, uint16_t localPort,
134                    const std::string & login, const std::string & password)
135     : pImpl(new IMPL(server, port, localAddress, localPort, login, password))
136 {
137 }
138
139 SERVCONF::~SERVCONF()
140 {
141 delete pImpl;
142 }
143
144 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
145 {
146 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
147 }
148
149 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
150 {
151 return pImpl->RawXML(request, f, data);
152 }
153
154 // -- Admins --
155
156 int SERVCONF::GetAdmins(GET_CONTAINER::CALLBACK<GET_ADMIN::INFO>::TYPE f, void * data)
157 {
158 return pImpl->Exec<GET_CONTAINER::PARSER<GET_ADMIN::PARSER> >("admins", "<GetAdmins/>", f, data);
159 }
160
161 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
162 {
163 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
164 }
165
166 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
167 {
168 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf, pImpl->Encoding()) + "/>", f, data);
169 }
170
171 int SERVCONF::AddAdmin(const std::string & login,
172                        const ADMIN_CONF_RES & conf,
173                        SIMPLE::CALLBACK f, void * data)
174 {
175 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
176 if (res != st_ok)
177     return res;
178 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf, pImpl->Encoding()) + "/>", f, data);
179 }
180
181 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
182 {
183 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
184 }
185
186 // -- Tariffs --
187
188 int SERVCONF::GetTariffs(GET_CONTAINER::CALLBACK<GET_TARIFF::INFO>::TYPE f, void * data)
189 {
190 return pImpl->Exec<GET_CONTAINER::PARSER<GET_TARIFF::PARSER> >("tariffs", "<GetTariffs/>", f, data);
191 }
192
193 int SERVCONF::GetTariff(const std::string & name, GET_TARIFF::CALLBACK f, void * data)
194 {
195 return pImpl->Exec<GET_TARIFF::PARSER>("<GetTariff name=\"" + name + "\"/>", f, data);
196 }
197
198 int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data)
199 {
200 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + tariffData.tariffConf.name.data() + "\">" + CHG_TARIFF::Serialize(tariffData, pImpl->Encoding()) + "</SetTariff>", f, data);
201 }
202
203 int SERVCONF::AddTariff(const std::string & name,
204                        const TARIFF_DATA_RES & tariffData,
205                        SIMPLE::CALLBACK f, void * data)
206 {
207 int res = pImpl->Exec<SIMPLE::PARSER>("AddTariff", "<AddTariff name=\"" + name + "\"/>", f, data);
208 if (res != st_ok)
209     return res;
210 return pImpl->Exec<SIMPLE::PARSER>("SetTariff", "<SetTariff name=\"" + name + "\">" + CHG_TARIFF::Serialize(tariffData, pImpl->Encoding()) + "</SetTariff>", f, data);
211 }
212
213 int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data)
214 {
215 return pImpl->Exec<SIMPLE::PARSER>("DelTariff", "<DelTariff name=\"" + name + "\"/>", f, data);
216 }
217
218 // -- Users --
219
220 int SERVCONF::GetUsers(GET_CONTAINER::CALLBACK<GET_USER::INFO>::TYPE f, void * data)
221 {
222 return pImpl->Exec<GET_CONTAINER::PARSER<GET_USER::PARSER> >("users", "<GetUsers/>", f, data);
223 }
224
225 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
226 {
227 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
228 }
229
230 int SERVCONF::ChgUser(const std::string & login,
231                       const USER_CONF_RES & conf,
232                       const USER_STAT_RES & stat,
233                       SIMPLE::CALLBACK f, void * data)
234 {
235 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat, pImpl->Encoding()) + "</SetUser>", f, data);
236 }
237
238 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
239 {
240 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
241 }
242
243 int SERVCONF::AddUser(const std::string & login,
244                       const USER_CONF_RES & conf,
245                       const USER_STAT_RES & stat,
246                       SIMPLE::CALLBACK f, void * data)
247 {
248 int res = pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
249 if (res != st_ok)
250     return res;
251 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat, pImpl->Encoding()) + "</SetUser>", f, data);
252 }
253
254 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
255 {
256 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
257 }
258
259 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
260 {
261 return pImpl->Exec<SIMPLE::PARSER>("SendMessageResult", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
262 }
263
264 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
265 {
266 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
267 }
268
269 // -- Services --
270
271 int SERVCONF::GetServices(GET_CONTAINER::CALLBACK<GET_SERVICE::INFO>::TYPE f, void * data)
272 {
273 return pImpl->Exec<GET_CONTAINER::PARSER<GET_SERVICE::PARSER> >("services", "<GetServices/>", f, data);
274 }
275
276 int SERVCONF::GetService(const std::string & name, GET_SERVICE::CALLBACK f, void * data)
277 {
278 return pImpl->Exec<GET_SERVICE::PARSER>("<GetService name=\"" + name + "\"/>", f, data);
279 }
280
281 int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
282 {
283 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService " + CHG_SERVICE::Serialize(conf, pImpl->Encoding()) + "/>", f, data);
284 }
285
286 int SERVCONF::AddService(const std::string & name,
287                          const SERVICE_CONF_RES & conf,
288                          SIMPLE::CALLBACK f, void * data)
289 {
290 int res = pImpl->Exec<SIMPLE::PARSER>("AddService", "<AddService name=\"" + name + "\"/>", f, data);
291 if (res != st_ok)
292     return res;
293 return pImpl->Exec<SIMPLE::PARSER>("SetService", "<SetService " + CHG_SERVICE::Serialize(conf, pImpl->Encoding()) + "/>", f, data);
294 }
295
296 int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data)
297 {
298 return pImpl->Exec<SIMPLE::PARSER>("DelService", "<DelService name=\"" + name + "\"/>", f, data);
299 }
300
301 // -- Corporations --
302
303 int SERVCONF::GetCorporations(GET_CONTAINER::CALLBACK<GET_CORP::INFO>::TYPE f, void * data)
304 {
305 return pImpl->Exec<GET_CONTAINER::PARSER<GET_CORP::PARSER> >("corporations", "<GetCorporations/>", f, data);
306 }
307
308 int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data)
309 {
310 return pImpl->Exec<GET_CORP::PARSER>("<GetCorp name=\"" + name + "\"/>", f, data);
311 }
312
313 int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
314 {
315 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + conf.name.data() + "\">" + CHG_CORP::Serialize(conf, pImpl->Encoding()) + "</SetCorp>", f, data);
316 }
317
318 int SERVCONF::AddCorp(const std::string & name,
319                       const CORP_CONF_RES & conf,
320                       SIMPLE::CALLBACK f, void * data)
321 {
322 int res = pImpl->Exec<SIMPLE::PARSER>("AddCorp", "<AddCorp name=\"" + name + "\"/>", f, data);
323 if (res != st_ok)
324     return res;
325 return pImpl->Exec<SIMPLE::PARSER>("SetCorp", "<SetCorp name=\"" + name + "\">" + CHG_CORP::Serialize(conf, pImpl->Encoding()) + "</SetCorp>", f, data);
326 }
327
328 int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data)
329 {
330 return pImpl->Exec<SIMPLE::PARSER>("DelCorp", "<DelCorp name=\"" + name + "\"/>", f, data);
331 }
332
333 const std::string & SERVCONF::GetStrError() const
334 {
335 return pImpl->GetStrError();
336 }
337
338 //-----------------------------------------------------------------------------
339 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
340                      const std::string & login, const std::string & password)
341     : nt(server, port, login, password)
342 {
343 setlocale(LC_ALL, "");
344 setlocale(LC_NUMERIC, "C");
345 encoding = nl_langinfo(CODESET);
346 parser = XML_ParserCreate(NULL);
347 }
348 //-----------------------------------------------------------------------------
349 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
350                      const std::string & localAddress, uint16_t localPort,
351                      const std::string & login, const std::string & password)
352     : nt(server, port, localAddress, localPort, login, password)
353 {
354 setlocale(LC_ALL, "");
355 setlocale(LC_NUMERIC, "C");
356 encoding = nl_langinfo(CODESET);
357 parser = XML_ParserCreate(NULL);
358 }
359 //-----------------------------------------------------------------------------
360 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
361 {
362 PARSER * currParser = static_cast<PARSER *>(data);
363 currParser->ParseStart(el, attr);
364 }
365 //-----------------------------------------------------------------------------
366 void SERVCONF::IMPL::End(void * data, const char * el)
367 {
368 PARSER * currParser = static_cast<PARSER *>(data);
369 currParser->ParseEnd(el);
370 }
371 //-----------------------------------------------------------------------------
372 const std::string & SERVCONF::IMPL::GetStrError() const
373 {
374 return errorMsg;
375 }
376 //-----------------------------------------------------------------------------
377 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
378 {
379 XML_ParserReset(parser, NULL);
380 XML_SetElementHandler(parser, Start, End);
381 XML_SetUserData(parser, &cp);
382
383 int ret = 0;
384 if ((ret = nt.Connect()) != st_ok)
385     {
386     errorMsg = nt.GetError();
387     cp.Failure(errorMsg);
388     return ret;
389     }
390 if ((ret = nt.Transact(request, ParserRecv, this)) != st_ok)
391     {
392     errorMsg = nt.GetError();
393     cp.Failure(errorMsg);
394     return ret;
395     }
396
397 nt.Disconnect();
398 return st_ok;
399 }
400
401 int SERVCONF::IMPL::RawXML(const std::string & request, RAW_XML::CALLBACK callback, void * data)
402 {
403 int ret = 0;
404 if ((ret = nt.Connect()) != st_ok)
405     {
406     errorMsg = nt.GetError();
407     callback(false, errorMsg, "", data);
408     return ret;
409     }
410 std::string response;
411 if ((ret = nt.Transact(request, SimpleRecv, &response)) != st_ok)
412     {
413     errorMsg = nt.GetError();
414     callback(false, errorMsg, "", data);
415     return ret;
416     }
417
418 nt.Disconnect();
419 callback(true, "", response, data);
420 return st_ok;
421 }