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