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