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