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 //-----------------------------------------------------------------------------
97 RecvGetUserDataCb(NULL),
98 RecvServerInfoDataCb(NULL),
100 RecvCheckUserCb(NULL),
101 RecvSendMessageCb(NULL),
102 getUserDataDataCb(NULL),
103 getUserAuthByDataCb(NULL),
104 getUsersDataDataCb(NULL),
105 getServerInfoDataCb(NULL),
107 checkUserDataCb(NULL),
108 sendMessageDataCb(NULL)
110 parser = XML_ParserCreate(NULL);
112 //-----------------------------------------------------------------------------
113 SERVCONF::~SERVCONF()
115 XML_ParserFree(parser);
117 //-----------------------------------------------------------------------------
118 void SERVCONF::SetServer(const char * server)
120 nt.SetServer(server);
122 //-----------------------------------------------------------------------------
123 void SERVCONF::SetPort(uint16_t port)
125 nt.SetServerPort(port);
127 //-----------------------------------------------------------------------------
128 void SERVCONF::SetAdmLogin(const char * login)
132 //-----------------------------------------------------------------------------
133 void SERVCONF::SetAdmPassword(const char * password)
135 nt.SetPassword(password);
137 //-----------------------------------------------------------------------------
138 int SERVCONF::GetUser(const char * l)
141 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
144 currParser = &parserGetUser;
145 ((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb);
148 nt.SetRxCallback(this, AnsRecv);
150 if ((ret = nt.Connect()) != st_ok)
152 errorMsg = nt.GetError();
155 if ((ret = nt.Transact(request)) != st_ok)
157 errorMsg = nt.GetError();
160 if ((ret = nt.Disconnect()) != st_ok)
162 errorMsg = nt.GetError();
168 //-----------------------------------------------------------------------------
169 int SERVCONF::GetUserAuthBy(const char * l)
172 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
175 currParser = &parserAuthBy;
176 ((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb);
179 nt.SetRxCallback(this, AnsRecv);
181 if ((ret = nt.Connect()) != st_ok)
183 errorMsg = nt.GetError();
186 if ((ret = nt.Transact(request)) != st_ok)
188 errorMsg = nt.GetError();
191 if ((ret = nt.Disconnect()) != st_ok)
193 errorMsg = nt.GetError();
199 //-----------------------------------------------------------------------------
200 int SERVCONF::GetUsers()
202 char request[] = "<GetUsers/>";
205 currParser = &parserGetUsers;
206 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
209 nt.SetRxCallback(this, AnsRecv);
211 if ((ret = nt.Connect()) != st_ok)
213 errorMsg = nt.GetError();
216 if ((ret = nt.Transact(request)) != st_ok)
218 errorMsg = nt.GetError();
221 if ((ret = nt.Disconnect()) != st_ok)
223 errorMsg = nt.GetError();
229 //-----------------------------------------------------------------------------
230 int SERVCONF::SendMessage(const char * login, const char * message, int prio)
234 Encode12(msg, message, strlen(message));
235 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
238 currParser = &parserSendMessage;
239 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
242 nt.SetRxCallback(this, AnsRecv);
244 if ((ret = nt.Connect()) != st_ok)
246 errorMsg = nt.GetError();
249 if ((ret = nt.Transact(request)) != st_ok)
251 errorMsg = nt.GetError();
254 if ((ret = nt.Disconnect()) != st_ok)
256 errorMsg = nt.GetError();
262 //-----------------------------------------------------------------------------
263 int SERVCONF::GetServerInfo()
265 char request[] = "<GetServerInfo/>";
268 currParser = &parserServerInfo;
269 ((PARSER_GET_SERVER_INFO*)currParser)->SetServerInfoRecvCb(RecvServerInfoDataCb, getServerInfoDataCb);
272 nt.SetRxCallback(this, AnsRecv);
274 if ((ret = nt.Connect()) != st_ok)
276 errorMsg = nt.GetError();
279 if ((ret = nt.Transact(request)) != st_ok)
281 errorMsg = nt.GetError();
284 if ((ret = nt.Disconnect()) != st_ok)
286 errorMsg = nt.GetError();
292 //-----------------------------------------------------------------------------
293 int SERVCONF::ChgUser(const char * request)
297 currParser = &parserChgUser;
298 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
301 nt.SetRxCallback(this, AnsRecv);
303 if ((ret = nt.Connect()) != st_ok)
305 errorMsg = nt.GetError();
308 if ((ret = nt.Transact(request)) != st_ok)
310 errorMsg = nt.GetError();
313 if ((ret = nt.Disconnect()) != st_ok)
315 errorMsg = nt.GetError();
321 //-----------------------------------------------------------------------------
322 // TODO: remove this shit!
323 //-----------------------------------------------------------------------------
324 int SERVCONF::MsgUser(const char * request)
328 currParser = &parserSendMessage;
329 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
332 nt.SetRxCallback(this, AnsRecv);
334 if ((ret = nt.Connect()) != st_ok)
336 errorMsg = nt.GetError();
339 if ((ret = nt.Transact(request)) != st_ok)
341 errorMsg = nt.GetError();
344 if ((ret = nt.Disconnect()) != st_ok)
346 errorMsg = nt.GetError();
352 //-----------------------------------------------------------------------------
353 int SERVCONF::CheckUser(const char * login, const char * password)
356 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
359 currParser = &parserCheckUser;
360 ((PARSER_CHECK_USER*)currParser)->SetCheckUserRecvCb(RecvCheckUserCb, checkUserDataCb);
363 nt.SetRxCallback(this, AnsRecv);
365 if ((ret = nt.Connect()) != st_ok)
367 errorMsg = nt.GetError();
370 if ((ret = nt.Transact(request)) != st_ok)
372 errorMsg = nt.GetError();
375 if ((ret = nt.Disconnect()) != st_ok)
377 errorMsg = nt.GetError();
383 //-----------------------------------------------------------------------------
384 int SERVCONF::Start(const char *el, const char **attr)
386 currParser->ParseStart(el, attr);
389 //-----------------------------------------------------------------------------
390 void SERVCONF::End(const char *el)
392 currParser->ParseEnd(el);
394 //-----------------------------------------------------------------------------
395 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
398 getUsersDataDataCb = data;
400 //-----------------------------------------------------------------------------
401 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
403 RecvGetUserDataCb = f; //GET_USER
404 getUserDataDataCb = data;
406 //-----------------------------------------------------------------------------
407 void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data)
410 getUserAuthByDataCb = data;
412 //-----------------------------------------------------------------------------
413 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
415 RecvServerInfoDataCb = f;
416 getServerInfoDataCb = data;
418 //-----------------------------------------------------------------------------
419 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
422 chgUserDataCb = data;
424 //-----------------------------------------------------------------------------
425 void SERVCONF::SetCheckUserCb(RecvCheckUserCb_t f, void * data)
428 checkUserDataCb = data;
430 //-----------------------------------------------------------------------------
431 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
433 RecvSendMessageCb = f;
434 sendMessageDataCb = data;
436 //-----------------------------------------------------------------------------
437 const std::string & SERVCONF::GetStrError() const
441 //-----------------------------------------------------------------------------
442 int SERVCONF::GetError()
448 //-----------------------------------------------------------------------------