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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/08/04 00:40:38 $
30 #include "stg/common.h"
31 #include "stg/servconf.h"
35 //-----------------------------------------------------------------------------
36 int AnsRecv(void * data, list<string> * list1)
40 char ans[ENC_MSG_LEN + 1];
45 XML_ParserReset(sc->parser, NULL);
46 XML_SetElementHandler(sc->parser, Start, End);
47 XML_SetUserData(sc->parser, data);
50 list<string>::iterator node;
51 node = list1->begin();
53 if (node == list1->end())
58 while (node != list1->end())
60 strncpy(ans, node->c_str(), ENC_MSG_LEN);
62 //printf("---> %s\n", ans);
65 if (XML_Parse(sc->parser, ans, len, done) == XML_STATUS_ERROR)
67 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
68 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
69 XML_ErrorString(XML_GetErrorCode(sc->parser)));
70 return st_xml_parse_error;
78 //-----------------------------------------------------------------------------
79 void Start(void *data, const char *el, const char **attr)
85 //-----------------------------------------------------------------------------
86 void End(void *data, const char *el)
92 //-----------------------------------------------------------------------------
98 RecvGetUserDataCb(NULL),
99 RecvServerInfoDataCb(NULL),
101 RecvCheckUserCb(NULL),
102 RecvSendMessageCb(NULL),
103 getUserDataDataCb(NULL),
104 getUserAuthByDataCb(NULL),
105 getUsersDataDataCb(NULL),
106 getServerInfoDataCb(NULL),
108 checkUserDataCb(NULL),
109 sendMessageDataCb(NULL)
111 parser = XML_ParserCreate(NULL);
113 //-----------------------------------------------------------------------------
114 void SERVCONF::SetServer(const char * server)
116 nt.SetServer(server);
118 //-----------------------------------------------------------------------------
119 void SERVCONF::SetPort(uint16_t port)
121 nt.SetServerPort(port);
123 //-----------------------------------------------------------------------------
124 void SERVCONF::SetAdmLogin(const char * login)
128 //-----------------------------------------------------------------------------
129 void SERVCONF::SetAdmPassword(const char * password)
131 nt.SetPassword(password);
133 //-----------------------------------------------------------------------------
134 int SERVCONF::GetUser(const char * l)
137 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
140 currParser = &parserGetUser;
141 ((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb);
144 nt.SetRxCallback(this, AnsRecv);
146 if ((ret = nt.Connect()) != st_ok)
148 errorMsg = nt.GetError();
151 if ((ret = nt.Transact(request)) != st_ok)
153 errorMsg = nt.GetError();
156 if ((ret = nt.Disconnect()) != st_ok)
158 errorMsg = nt.GetError();
164 //-----------------------------------------------------------------------------
165 int SERVCONF::GetUserAuthBy(const char * l)
168 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
171 currParser = &parserAuthBy;
172 ((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb);
175 nt.SetRxCallback(this, AnsRecv);
177 if ((ret = nt.Connect()) != st_ok)
179 errorMsg = nt.GetError();
182 if ((ret = nt.Transact(request)) != st_ok)
184 errorMsg = nt.GetError();
187 if ((ret = nt.Disconnect()) != st_ok)
189 errorMsg = nt.GetError();
195 //-----------------------------------------------------------------------------
196 int SERVCONF::GetUsers()
198 char request[] = "<GetUsers/>";
201 currParser = &parserGetUsers;
202 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
205 nt.SetRxCallback(this, AnsRecv);
207 if ((ret = nt.Connect()) != st_ok)
209 errorMsg = nt.GetError();
212 if ((ret = nt.Transact(request)) != st_ok)
214 errorMsg = nt.GetError();
217 if ((ret = nt.Disconnect()) != st_ok)
219 errorMsg = nt.GetError();
225 //-----------------------------------------------------------------------------
226 int SERVCONF::SendMessage(const char * login, const char * message, int prio)
230 Encode12(msg, message, strlen(message));
231 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
234 currParser = &parserSendMessage;
235 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
238 nt.SetRxCallback(this, AnsRecv);
240 if ((ret = nt.Connect()) != st_ok)
242 errorMsg = nt.GetError();
245 if ((ret = nt.Transact(request)) != st_ok)
247 errorMsg = nt.GetError();
250 if ((ret = nt.Disconnect()) != st_ok)
252 errorMsg = nt.GetError();
258 //-----------------------------------------------------------------------------
259 int SERVCONF::GetServerInfo()
261 char request[] = "<GetServerInfo/>";
264 currParser = &parserServerInfo;
265 ((PARSER_GET_SERVER_INFO*)currParser)->SetServerInfoRecvCb(RecvServerInfoDataCb, getServerInfoDataCb);
268 nt.SetRxCallback(this, AnsRecv);
270 if ((ret = nt.Connect()) != st_ok)
272 errorMsg = nt.GetError();
275 if ((ret = nt.Transact(request)) != st_ok)
277 errorMsg = nt.GetError();
280 if ((ret = nt.Disconnect()) != st_ok)
282 errorMsg = nt.GetError();
288 //-----------------------------------------------------------------------------
289 int SERVCONF::ChgUser(const char * request)
293 currParser = &parserChgUser;
294 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
297 nt.SetRxCallback(this, AnsRecv);
299 if ((ret = nt.Connect()) != st_ok)
301 errorMsg = nt.GetError();
304 if ((ret = nt.Transact(request)) != st_ok)
306 errorMsg = nt.GetError();
309 if ((ret = nt.Disconnect()) != st_ok)
311 errorMsg = nt.GetError();
317 //-----------------------------------------------------------------------------
318 // TODO: remove this shit!
319 //-----------------------------------------------------------------------------
320 int SERVCONF::MsgUser(const char * request)
324 currParser = &parserSendMessage;
325 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
328 nt.SetRxCallback(this, AnsRecv);
330 if ((ret = nt.Connect()) != st_ok)
332 errorMsg = nt.GetError();
335 if ((ret = nt.Transact(request)) != st_ok)
337 errorMsg = nt.GetError();
340 if ((ret = nt.Disconnect()) != st_ok)
342 errorMsg = nt.GetError();
348 //-----------------------------------------------------------------------------
349 int SERVCONF::CheckUser(const char * login, const char * password)
352 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
355 currParser = &parserCheckUser;
356 ((PARSER_CHECK_USER*)currParser)->SetCheckUserRecvCb(RecvCheckUserCb, checkUserDataCb);
359 nt.SetRxCallback(this, AnsRecv);
361 if ((ret = nt.Connect()) != st_ok)
363 errorMsg = nt.GetError();
366 if ((ret = nt.Transact(request)) != st_ok)
368 errorMsg = nt.GetError();
371 if ((ret = nt.Disconnect()) != st_ok)
373 errorMsg = nt.GetError();
379 //-----------------------------------------------------------------------------
380 int SERVCONF::Start(const char *el, const char **attr)
382 currParser->ParseStart(el, attr);
385 //-----------------------------------------------------------------------------
386 void SERVCONF::End(const char *el)
388 currParser->ParseEnd(el);
390 //-----------------------------------------------------------------------------
391 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
394 getUsersDataDataCb = data;
396 //-----------------------------------------------------------------------------
397 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
399 RecvGetUserDataCb = f; //GET_USER
400 getUserDataDataCb = data;
402 //-----------------------------------------------------------------------------
403 void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data)
406 getUserAuthByDataCb = data;
408 //-----------------------------------------------------------------------------
409 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
411 RecvServerInfoDataCb = f;
412 getServerInfoDataCb = data;
414 //-----------------------------------------------------------------------------
415 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
418 chgUserDataCb = data;
420 //-----------------------------------------------------------------------------
421 void SERVCONF::SetCheckUserCb(RecvCheckUserCb_t f, void * data)
424 checkUserDataCb = data;
426 //-----------------------------------------------------------------------------
427 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
429 RecvSendMessageCb = f;
430 sendMessageDataCb = data;
432 //-----------------------------------------------------------------------------
433 const std::string & SERVCONF::GetStrError() const
437 //-----------------------------------------------------------------------------
438 int SERVCONF::GetError()
444 //-----------------------------------------------------------------------------