]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Added RawXML method and related stuff.
[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/simple.h"
26
27 #include "parsers/server_info.h"
28
29 #include "parsers/get_admins.h"
30 #include "parsers/get_admin.h"
31 #include "parsers/chg_admin.h"
32
33 #include "parsers/auth_by.h"
34 #include "parsers/get_users.h"
35 #include "parsers/get_user.h"
36 #include "parsers/chg_user.h"
37
38 #include "parsers/base.h"
39
40 #include "stg/common.h"
41
42 #include <cstdio>
43 #include <cstring>
44
45 #include <expat.h>
46
47 using namespace STG;
48
49 class SERVCONF::IMPL
50 {
51 public:
52     IMPL(const std::string & server, uint16_t port,
53          const std::string & login, const std::string & password);
54
55     const std::string & GetStrError() const;
56     static void Start(void * data, const char * el, const char ** attr);
57     static void End(void * data, const char * el);
58
59     int RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data);
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     template <class P, typename C>
69     int Exec(const std::string & tag, const std::string & request, C callback, void * data)
70     {
71         P cp(tag, callback, data);
72         return ExecImpl(request, cp);
73     }
74
75 private:
76     NETTRANSACT nt;
77
78     std::string errorMsg;
79     XML_Parser parser;
80
81     static bool ParserRecv(void * data, const std::string & chunk, bool final);
82     static bool SimpleRecv(void * data, const std::string & chunk, bool final);
83     int ExecImpl(const std::string & request, PARSER & cp);
84 };
85
86 bool SERVCONF::IMPL::ParserRecv(void * data, const std::string & chunk, bool final)
87 {
88 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
89
90 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
91     {
92     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
93               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
94               XML_ErrorString(XML_GetErrorCode(sc->parser)));
95     printf("%s\n", sc->errorMsg.c_str());
96     return false;
97     }
98
99 return true;
100 }
101
102 bool SERVCONF::IMPL::SimpleRecv(void * data, const std::string & chunk, bool final)
103 {
104 *static_cast<std::string *>(data) += chunk;
105 return true;
106 }
107
108 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
109                    const std::string & login, const std::string & password)
110     : pImpl(new IMPL(server, port, login, password))
111 {
112 }
113
114 SERVCONF::~SERVCONF()
115 {
116 delete pImpl;
117 }
118
119 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
120 {
121 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
122 }
123
124 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
125 {
126 }
127
128 // -- Admins --
129
130 int SERVCONF::GetAdmins(GET_ADMINS::CALLBACK f, void * data)
131 {
132 return pImpl->Exec<GET_ADMINS::PARSER>("<GetAdmins/>", f, data);
133 }
134
135 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
136 {
137 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
138 }
139
140 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
141 {
142 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
143 }
144
145 int SERVCONF::AddAdmin(const std::string & login,
146                        const ADMIN_CONF_RES & conf,
147                        SIMPLE::CALLBACK f, void * data)
148 {
149 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
150 if (res != st_ok)
151     return res;
152 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
153 }
154
155 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
156 {
157 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
158 }
159
160 // -- Users --
161
162 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
163 {
164 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
165 }
166
167 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
168 {
169 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
170 }
171
172 int SERVCONF::ChgUser(const std::string & login,
173                       const USER_CONF_RES & conf,
174                       const USER_STAT_RES & stat,
175                       SIMPLE::CALLBACK f, void * data)
176 {
177 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
178 }
179
180 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
181 {
182 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
183 }
184
185 int SERVCONF::AddUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
186 {
187 return pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
188 }
189
190 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
191 {
192 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
193 }
194
195 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
196 {
197 return pImpl->Exec<SIMPLE::PARSER>("SendMessage", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
198 }
199
200 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
201 {
202 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
203 }
204
205 const std::string & SERVCONF::GetStrError() const
206 {
207 return pImpl->GetStrError();
208 }
209
210 //-----------------------------------------------------------------------------
211 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
212                      const std::string & login, const std::string & password)
213     : nt( server, port, login, password )
214 {
215 parser = XML_ParserCreate(NULL);
216 nt.SetRxCallback(this, ParserRecv);
217 }
218 //-----------------------------------------------------------------------------
219 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
220 {
221 PARSER * currParser = static_cast<PARSER *>(data);
222 currParser->ParseStart(el, attr);
223 }
224 //-----------------------------------------------------------------------------
225 void SERVCONF::IMPL::End(void * data, const char * el)
226 {
227 PARSER * currParser = static_cast<PARSER *>(data);
228 currParser->ParseEnd(el);
229 }
230 //-----------------------------------------------------------------------------
231 const std::string & SERVCONF::IMPL::GetStrError() const
232 {
233 return errorMsg;
234 }
235 //-----------------------------------------------------------------------------
236 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
237 {
238 XML_ParserReset(parser, NULL);
239 XML_SetElementHandler(parser, Start, End);
240 XML_SetUserData(parser, &cp);
241
242 int ret = 0;
243 if ((ret = nt.Connect()) != st_ok)
244     {
245     errorMsg = nt.GetError();
246     return ret;
247     }
248 if ((ret = nt.Transact(request.c_str())) != st_ok)
249     {
250     errorMsg = nt.GetError();
251     return ret;
252     }
253 if ((ret = nt.Disconnect()) != st_ok)
254     {
255     errorMsg = nt.GetError();
256     return ret;
257     }
258
259 return st_ok;
260 }
261
262 int SERVCONF::RawXML(const std::string & request, RAW_XML::CALLBACK f, void * data)
263 {
264 std::string response;
265 nt.SetRxCallback(&response, SimpleRecv);
266 int ret = 0;
267 if ((ret = nt.Connect()) != st_ok)
268     {
269     nt.SetRxCallback(this, ParserRecv);
270     errorMsg = nt.GetError();
271     f(false, errorMsg, "", data);
272     return ret;
273     }
274 if ((ret = nt.Transact(request.c_str())) != st_ok)
275     {
276     nt.SetRxCallback(this, ParserRecv);
277     errorMsg = nt.GetError();
278     f(false, errorMsg, "", data);
279     return ret;
280     }
281 if ((ret = nt.Disconnect()) != st_ok)
282     {
283     nt.SetRxCallback(this, ParserRecv);
284     errorMsg = nt.GetError();
285     f(false, errorMsg, "", data);
286     return ret;
287     }
288 nt.SetRxCallback(this, ParserRecv);
289 f(true, "", response, data);
290 return st_ok;
291 }