]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/servconf.cpp
Fixes in paths.
[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 GetUsers(GET_USERS::CALLBACK f, void * data);
49     int GetUser(const std::string & login, GET_USER::CALLBACK f, void * data);
50     int ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data);
51     int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data);
52     int SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data);
53     int ServerInfo(SERVER_INFO::CALLBACK f, void * data);
54     int CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data);
55
56     const std::string & GetStrError() const;
57     static void Start(void * data, const char * el, const char ** attr);
58     static void End(void * data, const char * el);
59
60 private:
61     GET_USERS::PARSER parserGetUsers;
62     GET_USER::PARSER parserGetUser;
63     AUTH_BY::PARSER parserAuthBy;
64     SERVER_INFO::PARSER  parserServerInfo;
65     CHG_USER::PARSER parserChgUser;
66     CHECK_USER::PARSER parserCheckUser;
67     SEND_MESSAGE::PARSER parserSendMessage;
68
69     NETTRANSACT nt;
70
71     std::string errorMsg;
72     XML_Parser parser;
73
74     int Exec(const std::string & request, PARSER & cp);
75
76     static bool AnsRecv(void * data, const std::string & chunk, bool final);
77 };
78
79 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
80 {
81 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
82
83 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
84     {
85     strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
86               static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
87               XML_ErrorString(XML_GetErrorCode(sc->parser)));
88     printf("%s\n", sc->errorMsg.c_str());
89     return false;
90     }
91
92 return true;
93 }
94
95 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
96                    const std::string & login, const std::string & password)
97     : pImpl(new IMPL(server, port, login, password))
98 {
99 }
100
101 SERVCONF::~SERVCONF()
102 {
103     delete pImpl;
104 }
105
106 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
107 {
108     return pImpl->GetUsers( f, data );
109 }
110
111 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
112 {
113     return pImpl->GetUser(login, f, data);
114 }
115
116 int SERVCONF::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
117 {
118     return pImpl->ChgUser(request, f, data);
119 }
120
121 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
122 {
123     return pImpl->AuthBy(login, f, data);
124 }
125
126 int SERVCONF::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
127 {
128     return pImpl->SendMessage(request, f, data);
129 }
130
131 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
132 {
133     return pImpl->ServerInfo(f, data);
134 }
135
136 int SERVCONF::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
137 {
138     return pImpl->CheckUser(login, password, f, data);
139 }
140
141 const std::string & SERVCONF::GetStrError() const
142 {
143     return pImpl->GetStrError();
144 }
145
146 //-----------------------------------------------------------------------------
147 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
148                      const std::string & login, const std::string & password)
149     : nt( server, port, login, password )
150 {
151 parser = XML_ParserCreate(NULL);
152 nt.SetRxCallback(this, AnsRecv);
153 }
154 //-----------------------------------------------------------------------------
155 int SERVCONF::IMPL::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
156 {
157 parserGetUser.SetCallback(f, data);
158 return Exec("<GetUser login=\"" + login + "\"/>", parserGetUser);
159 }
160 //-----------------------------------------------------------------------------
161 int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
162 {
163 parserAuthBy.SetCallback(f, data);
164 return Exec("<GetUserAuthBy login=\"" + login + "\"/>", parserAuthBy);
165 }
166 //-----------------------------------------------------------------------------
167 int SERVCONF::IMPL::GetUsers(GET_USERS::CALLBACK f, void * data)
168 {
169 parserGetUsers.SetCallback(f, data);
170 return Exec("<GetUsers/>", parserGetUsers);
171 }
172 //-----------------------------------------------------------------------------
173 int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
174 {
175 parserServerInfo.SetCallback(f, data);
176 return Exec("<GetServerInfo/>", parserServerInfo);
177 }
178 //-----------------------------------------------------------------------------
179 int SERVCONF::IMPL::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data)
180 {
181 parserChgUser.SetCallback(f, data);
182 return Exec(request, parserChgUser);
183 }
184 //-----------------------------------------------------------------------------
185 int SERVCONF::IMPL::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data)
186 {
187 parserSendMessage.SetCallback(f, data);
188 return Exec(request, parserSendMessage);
189 }
190 //-----------------------------------------------------------------------------
191 int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data)
192 {
193 parserCheckUser.SetCallback(f, data);
194 return Exec("<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", parserCheckUser);
195 }
196 //-----------------------------------------------------------------------------
197 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
198 {
199 PARSER * currParser = static_cast<PARSER *>(data);
200 currParser->ParseStart(el, attr);
201 }
202 //-----------------------------------------------------------------------------
203 void SERVCONF::IMPL::End(void * data, const char * el)
204 {
205 PARSER * currParser = static_cast<PARSER *>(data);
206 currParser->ParseEnd(el);
207 }
208 //-----------------------------------------------------------------------------
209 const std::string & SERVCONF::IMPL::GetStrError() const
210 {
211 return errorMsg;
212 }
213 //-----------------------------------------------------------------------------
214 int SERVCONF::IMPL::Exec(const std::string & request, PARSER & cp)
215 {
216 XML_ParserReset(parser, NULL);
217 XML_SetElementHandler(parser, Start, End);
218 XML_SetUserData(parser, &cp);
219
220 int ret = 0;
221 if ((ret = nt.Connect()) != st_ok)
222     {
223     errorMsg = nt.GetError();
224     return ret;
225     }
226 if ((ret = nt.Transact(request.c_str())) != st_ok)
227     {
228     errorMsg = nt.GetError();
229     return ret;
230     }
231 if ((ret = nt.Disconnect()) != st_ok)
232     {
233     errorMsg = nt.GetError();
234     return ret;
235     }
236
237 return st_ok;
238 }