]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Simplified parser interfaces.
[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 #include "parsers/auth_by.h"
25 #include "parsers/server_info.h"
26 #include "parsers/check_user.h"
27 #include "parsers/get_users.h"
28 #include "parsers/get_user.h"
29 #include "parsers/chg_user.h"
30 #include "parsers/send_message.h"
31 #include "parsers/base.h"
32
33 #include "stg/common.h"
34
35 #include <cstdio>
36 #include <cstring>
37
38 #include <expat.h>
39
40 using namespace STG;
41
42 class SERVCONF::IMPL
43 {
44 public:
45     IMPL(const std::string & server, uint16_t port,
46          const std::string & login, const std::string & password);
47
48     int ServerInfo(SERVER_INFO::CALLBACK f, void * data);
49
50     int GetAdmins(GET_ADMINS::CALLBACK f, void * data);
51     int GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data);
52     int ChgAdmin(const std::string & login, const ADMIN_CONF_RES & conf, CHG_ADMIN::CALLBACK f, void * data);
53     int AddAdmin(const std::string & login, const ADMIN_CONF & conf, GET_ADMIN::CALLBACK f, void * data);
54     int DelAdmin(const std::string & login, DEL_ADMIN::CALLBACK f, void * data);
55
56     int GetUsers(GET_USERS::CALLBACK f, void * data);
57     int GetUser(const std::string & login, GET_USER::CALLBACK f, void * data);
58     int ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data);
59     int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data);
60     int SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data);
61     int CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data);
62
63     const std::string & GetStrError() const;
64     static void Start(void * data, const char * el, const char ** attr);
65     static void End(void * data, const char * el);
66
67 private:
68     NETTRANSACT nt;
69
70     std::string errorMsg;
71     XML_Parser parser;
72
73     int Exec(const std::string & request, PARSER & cp);
74
75     static bool AnsRecv(void * data, const std::string & chunk, bool final);
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::GetUsers(GET_USERS::CALLBACK f, void * data)
106 {
107     return pImpl->GetUsers( f, data );
108 }
109
110 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
111 {
112     return pImpl->GetUser(login, f, data);
113 }
114
115 int SERVCONF::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
116 {
117     return pImpl->ChgUser(request, f, data);
118 }
119
120 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
121 {
122     return pImpl->AuthBy(login, f, data);
123 }
124
125 int SERVCONF::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
126 {
127     return pImpl->SendMessage(request, f, data);
128 }
129
130 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
131 {
132     return pImpl->ServerInfo(f, data);
133 }
134
135 int SERVCONF::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
136 {
137     return pImpl->CheckUser(login, password, f, data);
138 }
139
140 const std::string & SERVCONF::GetStrError() const
141 {
142     return pImpl->GetStrError();
143 }
144
145 //-----------------------------------------------------------------------------
146 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
147                      const std::string & login, const std::string & password)
148     : nt( server, port, login, password )
149 {
150 parser = XML_ParserCreate(NULL);
151 nt.SetRxCallback(this, AnsRecv);
152 }
153 //-----------------------------------------------------------------------------
154 int SERVCONF::IMPL::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
155 {
156 GET_USER::PARSER parser(f, data);
157 return Exec("<GetUser login=\"" + login + "\"/>", parser);
158 }
159 //-----------------------------------------------------------------------------
160 int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
161 {
162 AUTH_BY::PARSER parser(f, data);
163 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parser);
164 }
165 //-----------------------------------------------------------------------------
166 int SERVCONF::IMPL::GetUsers(GET_USERS::CALLBACK f, void * data)
167 {
168 GET_USERS::PARSER parser(f, data);
169 return Exec("<GetUsers/>", parser);
170 }
171 //-----------------------------------------------------------------------------
172 int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
173 {
174 SERVER_INFO::PARSER parser(f, data);
175 return Exec("<GetServerInfo/>", parser);
176 }
177 //-----------------------------------------------------------------------------
178 int SERVCONF::IMPL::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
179 {
180 CHG_USER::PARSER parser(f, data);
181 return Exec(request, parser);
182 }
183 //-----------------------------------------------------------------------------
184 int SERVCONF::IMPL::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
185 {
186 SEND_MESSAGE::PARSER parser(f, data);
187 return Exec(request, parser);
188 }
189 //-----------------------------------------------------------------------------
190 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
191 {
192 CHECK_USER::PARSER parser(f, data);
193 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parser);
194 }
195 //-----------------------------------------------------------------------------
196 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
197 {
198 PARSER * currParser = static_cast<PARSER *>(data);
199 currParser->ParseStart(el, attr);
200 }
201 //-----------------------------------------------------------------------------
202 void SERVCONF::IMPL::End(void * data, const char * el)
203 {
204 PARSER * currParser = static_cast<PARSER *>(data);
205 currParser->ParseEnd(el);
206 }
207 //-----------------------------------------------------------------------------
208 const std::string & SERVCONF::IMPL::GetStrError() const
209 {
210 return errorMsg;
211 }
212 //-----------------------------------------------------------------------------
213 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
214 {
215 XML_ParserReset(parser, NULL);
216 XML_SetElementHandler(parser, Start, End);
217 XML_SetUserData(parser, &cp);
218
219 int ret = 0;
220 if ((ret = nt.Connect()) != st_ok)
221     {
222     errorMsg = nt.GetError();
223     return ret;
224     }
225 if ((ret = nt.Transact(request.c_str())) != st_ok)
226     {
227     errorMsg = nt.GetError();
228     return ret;
229     }
230 if ((ret = nt.Disconnect()) != st_ok)
231     {
232     errorMsg = nt.GetError();
233     return ret;
234     }
235
236 return st_ok;
237 }