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 void SERVCONF::SetServer(const char * server)
115 nt.SetServer(server);
117 //-----------------------------------------------------------------------------
118 void SERVCONF::SetPort(uint16_t port)
120 nt.SetServerPort(port);
122 //-----------------------------------------------------------------------------
123 void SERVCONF::SetAdmLogin(const char * login)
127 //-----------------------------------------------------------------------------
128 void SERVCONF::SetAdmPassword(const char * password)
130 nt.SetPassword(password);
132 //-----------------------------------------------------------------------------
133 int SERVCONF::GetUser(const char * l)
136 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
139 currParser = &parserGetUser;
140 ((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb);
143 nt.SetRxCallback(this, AnsRecv);
145 if ((ret = nt.Connect()) != st_ok)
147 errorMsg = nt.GetError();
150 if ((ret = nt.Transact(request)) != st_ok)
152 errorMsg = nt.GetError();
155 if ((ret = nt.Disconnect()) != st_ok)
157 errorMsg = nt.GetError();
163 //-----------------------------------------------------------------------------
164 int SERVCONF::GetUserAuthBy(const char * l)
167 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
170 currParser = &parserAuthBy;
171 ((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb);
174 nt.SetRxCallback(this, AnsRecv);
176 if ((ret = nt.Connect()) != st_ok)
178 errorMsg = nt.GetError();
181 if ((ret = nt.Transact(request)) != st_ok)
183 errorMsg = nt.GetError();
186 if ((ret = nt.Disconnect()) != st_ok)
188 errorMsg = nt.GetError();
194 //-----------------------------------------------------------------------------
195 int SERVCONF::GetUsers()
197 char request[] = "<GetUsers/>";
200 currParser = &parserGetUsers;
201 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
204 nt.SetRxCallback(this, AnsRecv);
206 if ((ret = nt.Connect()) != st_ok)
208 errorMsg = nt.GetError();
211 if ((ret = nt.Transact(request)) != st_ok)
213 errorMsg = nt.GetError();
216 if ((ret = nt.Disconnect()) != st_ok)
218 errorMsg = nt.GetError();
224 //-----------------------------------------------------------------------------
225 int SERVCONF::SendMessage(const char * login, const char * message, int prio)
229 Encode12(msg, message, strlen(message));
230 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
233 currParser = &parserSendMessage;
234 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
237 nt.SetRxCallback(this, AnsRecv);
239 if ((ret = nt.Connect()) != st_ok)
241 errorMsg = nt.GetError();
244 if ((ret = nt.Transact(request)) != st_ok)
246 errorMsg = nt.GetError();
249 if ((ret = nt.Disconnect()) != st_ok)
251 errorMsg = nt.GetError();
257 //-----------------------------------------------------------------------------
258 int SERVCONF::GetServerInfo()
260 char request[] = "<GetServerInfo/>";
263 currParser = &parserServerInfo;
264 ((PARSER_GET_SERVER_INFO*)currParser)->SetServerInfoRecvCb(RecvServerInfoDataCb, getServerInfoDataCb);
267 nt.SetRxCallback(this, AnsRecv);
269 if ((ret = nt.Connect()) != st_ok)
271 errorMsg = nt.GetError();
274 if ((ret = nt.Transact(request)) != st_ok)
276 errorMsg = nt.GetError();
279 if ((ret = nt.Disconnect()) != st_ok)
281 errorMsg = nt.GetError();
287 //-----------------------------------------------------------------------------
288 int SERVCONF::ChgUser(const char * request)
292 currParser = &parserChgUser;
293 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
296 nt.SetRxCallback(this, AnsRecv);
298 if ((ret = nt.Connect()) != st_ok)
300 errorMsg = nt.GetError();
303 if ((ret = nt.Transact(request)) != st_ok)
305 errorMsg = nt.GetError();
308 if ((ret = nt.Disconnect()) != st_ok)
310 errorMsg = nt.GetError();
316 //-----------------------------------------------------------------------------
317 // TODO: remove this shit!
318 //-----------------------------------------------------------------------------
319 int SERVCONF::MsgUser(const char * request)
323 currParser = &parserSendMessage;
324 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
327 nt.SetRxCallback(this, AnsRecv);
329 if ((ret = nt.Connect()) != st_ok)
331 errorMsg = nt.GetError();
334 if ((ret = nt.Transact(request)) != st_ok)
336 errorMsg = nt.GetError();
339 if ((ret = nt.Disconnect()) != st_ok)
341 errorMsg = nt.GetError();
347 //-----------------------------------------------------------------------------
348 int SERVCONF::CheckUser(const char * login, const char * password)
351 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
354 currParser = &parserCheckUser;
355 ((PARSER_CHECK_USER*)currParser)->SetCheckUserRecvCb(RecvCheckUserCb, checkUserDataCb);
358 nt.SetRxCallback(this, AnsRecv);
360 if ((ret = nt.Connect()) != st_ok)
362 errorMsg = nt.GetError();
365 if ((ret = nt.Transact(request)) != st_ok)
367 errorMsg = nt.GetError();
370 if ((ret = nt.Disconnect()) != st_ok)
372 errorMsg = nt.GetError();
378 //-----------------------------------------------------------------------------
379 int SERVCONF::Start(const char *el, const char **attr)
381 currParser->ParseStart(el, attr);
384 //-----------------------------------------------------------------------------
385 void SERVCONF::End(const char *el)
387 currParser->ParseEnd(el);
389 //-----------------------------------------------------------------------------
390 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
393 getUsersDataDataCb = data;
395 //-----------------------------------------------------------------------------
396 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
398 RecvGetUserDataCb = f; //GET_USER
399 getUserDataDataCb = data;
401 //-----------------------------------------------------------------------------
402 void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data)
405 getUserAuthByDataCb = data;
407 //-----------------------------------------------------------------------------
408 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
410 RecvServerInfoDataCb = f;
411 getServerInfoDataCb = data;
413 //-----------------------------------------------------------------------------
414 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
417 chgUserDataCb = data;
419 //-----------------------------------------------------------------------------
420 void SERVCONF::SetCheckUserCb(RecvCheckUserCb_t f, void * data)
423 checkUserDataCb = data;
425 //-----------------------------------------------------------------------------
426 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
428 RecvSendMessageCb = f;
429 sendMessageDataCb = data;
431 //-----------------------------------------------------------------------------
432 const std::string & SERVCONF::GetStrError() const
436 //-----------------------------------------------------------------------------
437 int SERVCONF::GetError()
443 //-----------------------------------------------------------------------------