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 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::GetUsers()
166 char request[] = "<GetUsers/>";
169 currParser = &parserGetUsers;
170 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
173 nt.SetRxCallback(this, AnsRecv);
175 if ((ret = nt.Connect()) != st_ok)
177 errorMsg = nt.GetError();
180 if ((ret = nt.Transact(request)) != st_ok)
182 errorMsg = nt.GetError();
185 if ((ret = nt.Disconnect()) != st_ok)
187 errorMsg = nt.GetError();
193 //-----------------------------------------------------------------------------
194 int SERVCONF::SendMessage(const char * login, const char * message, int prio)
198 Encode12(msg, message, strlen(message));
199 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
202 currParser = &parserSendMessage;
203 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
206 nt.SetRxCallback(this, AnsRecv);
208 if ((ret = nt.Connect()) != st_ok)
210 errorMsg = nt.GetError();
213 if ((ret = nt.Transact(request)) != st_ok)
215 errorMsg = nt.GetError();
218 if ((ret = nt.Disconnect()) != st_ok)
220 errorMsg = nt.GetError();
226 //-----------------------------------------------------------------------------
227 int SERVCONF::GetServerInfo()
229 char request[] = "<GetServerInfo/>";
232 currParser = &parserServerInfo;
233 ((PARSER_GET_SERVER_INFO*)currParser)->SetServerInfoRecvCb(RecvServerInfoDataCb, getServerInfoDataCb);
236 nt.SetRxCallback(this, AnsRecv);
238 if ((ret = nt.Connect()) != st_ok)
240 errorMsg = nt.GetError();
243 if ((ret = nt.Transact(request)) != st_ok)
245 errorMsg = nt.GetError();
248 if ((ret = nt.Disconnect()) != st_ok)
250 errorMsg = nt.GetError();
256 //-----------------------------------------------------------------------------
257 int SERVCONF::ChgUser(const char * request)
261 currParser = &parserChgUser;
262 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
265 nt.SetRxCallback(this, AnsRecv);
267 if ((ret = nt.Connect()) != st_ok)
269 errorMsg = nt.GetError();
272 if ((ret = nt.Transact(request)) != st_ok)
274 errorMsg = nt.GetError();
277 if ((ret = nt.Disconnect()) != st_ok)
279 errorMsg = nt.GetError();
285 //-----------------------------------------------------------------------------
286 // TODO: remove this shit!
287 //-----------------------------------------------------------------------------
288 int SERVCONF::MsgUser(const char * request)
292 currParser = &parserSendMessage;
293 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
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 int SERVCONF::CheckUser(const char * login, const char * password)
320 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
323 currParser = &parserCheckUser;
324 ((PARSER_CHECK_USER*)currParser)->SetCheckUserRecvCb(RecvCheckUserCb, checkUserDataCb);
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::Start(const char *el, const char **attr)
350 currParser->ParseStart(el, attr);
353 //-----------------------------------------------------------------------------
354 void SERVCONF::End(const char *el)
356 currParser->ParseEnd(el);
358 //-----------------------------------------------------------------------------
359 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
362 getUsersDataDataCb = data;
364 //-----------------------------------------------------------------------------
365 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
367 RecvGetUserDataCb = f; //GET_USER
368 getUserDataDataCb = data;
370 //-----------------------------------------------------------------------------
371 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
373 RecvServerInfoDataCb = f;
374 getServerInfoDataCb = data;
376 //-----------------------------------------------------------------------------
377 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
380 chgUserDataCb = data;
382 //-----------------------------------------------------------------------------
383 void SERVCONF::SetCheckUserCb(RecvCheckUserCb_t f, void * data)
386 checkUserDataCb = data;
388 //-----------------------------------------------------------------------------
389 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
391 RecvSendMessageCb = f;
392 sendMessageDataCb = data;
394 //-----------------------------------------------------------------------------
395 const std::string & SERVCONF::GetStrError() const
399 //-----------------------------------------------------------------------------
400 int SERVCONF::GetError()
406 //-----------------------------------------------------------------------------