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