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),
100 serverInfoCallback(NULL),
102 checkUserCallback(NULL),
103 RecvSendMessageCb(NULL),
104 getUserDataDataCb(NULL),
106 getUsersDataDataCb(NULL),
107 serverInfoData(NULL),
110 sendMessageDataCb(NULL)
112 parser = XML_ParserCreate(NULL);
114 //-----------------------------------------------------------------------------
115 void SERVCONF::SetServer(const char * server)
117 nt.SetServer(server);
119 //-----------------------------------------------------------------------------
120 void SERVCONF::SetPort(uint16_t port)
122 nt.SetServerPort(port);
124 //-----------------------------------------------------------------------------
125 void SERVCONF::SetAdmLogin(const char * login)
129 //-----------------------------------------------------------------------------
130 void SERVCONF::SetAdmPassword(const char * password)
132 nt.SetPassword(password);
134 //-----------------------------------------------------------------------------
135 int SERVCONF::GetUser(const char * l)
138 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
141 currParser = &parserGetUser;
142 ((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb);
145 nt.SetRxCallback(this, AnsRecv);
147 if ((ret = nt.Connect()) != st_ok)
149 errorMsg = nt.GetError();
152 if ((ret = nt.Transact(request)) != st_ok)
154 errorMsg = nt.GetError();
157 if ((ret = nt.Disconnect()) != st_ok)
159 errorMsg = nt.GetError();
165 //-----------------------------------------------------------------------------
166 int SERVCONF::AuthBy(const char * l)
169 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
171 currParser = &parserAuthBy;
172 ((PARSER_AUTH_BY*)currParser)->SetCallback(authByCallback, authByData);
175 nt.SetRxCallback(this, AnsRecv);
178 if ((ret = nt.Connect()) != st_ok)
180 errorMsg = nt.GetError();
183 if ((ret = nt.Transact(request)) != st_ok)
185 errorMsg = nt.GetError();
188 if ((ret = nt.Disconnect()) != st_ok)
190 errorMsg = nt.GetError();
196 //-----------------------------------------------------------------------------
197 int SERVCONF::GetUsers()
199 char request[] = "<GetUsers/>";
202 currParser = &parserGetUsers;
203 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
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::SendMessage(const char * login, const char * message, int prio)
231 Encode12(msg, message, strlen(message));
232 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
235 currParser = &parserSendMessage;
236 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
239 nt.SetRxCallback(this, AnsRecv);
241 if ((ret = nt.Connect()) != st_ok)
243 errorMsg = nt.GetError();
246 if ((ret = nt.Transact(request)) != st_ok)
248 errorMsg = nt.GetError();
251 if ((ret = nt.Disconnect()) != st_ok)
253 errorMsg = nt.GetError();
259 //-----------------------------------------------------------------------------
260 int SERVCONF::ServerInfo()
262 char request[] = "<GetServerInfo/>";
265 currParser = &parserServerInfo;
266 ((PARSER_SERVER_INFO*)currParser)->SetCallback(serverInfoCallback, serverInfoData);
269 nt.SetRxCallback(this, AnsRecv);
271 if ((ret = nt.Connect()) != st_ok)
273 errorMsg = nt.GetError();
276 if ((ret = nt.Transact(request)) != st_ok)
278 errorMsg = nt.GetError();
281 if ((ret = nt.Disconnect()) != st_ok)
283 errorMsg = nt.GetError();
289 //-----------------------------------------------------------------------------
290 int SERVCONF::ChgUser(const char * request)
294 currParser = &parserChgUser;
295 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
298 nt.SetRxCallback(this, AnsRecv);
300 if ((ret = nt.Connect()) != st_ok)
302 errorMsg = nt.GetError();
305 if ((ret = nt.Transact(request)) != st_ok)
307 errorMsg = nt.GetError();
310 if ((ret = nt.Disconnect()) != st_ok)
312 errorMsg = nt.GetError();
318 //-----------------------------------------------------------------------------
319 // TODO: remove this shit!
320 //-----------------------------------------------------------------------------
321 int SERVCONF::MsgUser(const char * request)
325 currParser = &parserSendMessage;
326 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
329 nt.SetRxCallback(this, AnsRecv);
331 if ((ret = nt.Connect()) != st_ok)
333 errorMsg = nt.GetError();
336 if ((ret = nt.Transact(request)) != st_ok)
338 errorMsg = nt.GetError();
341 if ((ret = nt.Disconnect()) != st_ok)
343 errorMsg = nt.GetError();
349 //-----------------------------------------------------------------------------
350 int SERVCONF::CheckUser(const char * login, const char * password)
353 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
356 currParser = &parserCheckUser;
357 ((PARSER_CHECK_USER*)currParser)->SetCallback(checkUserCallback, checkUserData);
360 nt.SetRxCallback(this, AnsRecv);
362 if ((ret = nt.Connect()) != st_ok)
364 errorMsg = nt.GetError();
367 if ((ret = nt.Transact(request)) != st_ok)
369 errorMsg = nt.GetError();
372 if ((ret = nt.Disconnect()) != st_ok)
374 errorMsg = nt.GetError();
380 //-----------------------------------------------------------------------------
381 int SERVCONF::Start(const char *el, const char **attr)
383 currParser->ParseStart(el, attr);
386 //-----------------------------------------------------------------------------
387 void SERVCONF::End(const char *el)
389 currParser->ParseEnd(el);
391 //-----------------------------------------------------------------------------
392 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
395 getUsersDataDataCb = data;
397 //-----------------------------------------------------------------------------
398 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
400 RecvGetUserDataCb = f; //GET_USER
401 getUserDataDataCb = data;
403 //-----------------------------------------------------------------------------
404 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
409 //-----------------------------------------------------------------------------
410 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
412 serverInfoCallback = f;
413 serverInfoData = data;
415 //-----------------------------------------------------------------------------
416 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
419 chgUserDataCb = data;
421 //-----------------------------------------------------------------------------
422 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
424 checkUserCallback = f;
425 checkUserData = data;
427 //-----------------------------------------------------------------------------
428 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
430 RecvSendMessageCb = f;
431 sendMessageDataCb = data;
433 //-----------------------------------------------------------------------------
434 const std::string & SERVCONF::GetStrError() const
438 //-----------------------------------------------------------------------------
439 int SERVCONF::GetError()
445 //-----------------------------------------------------------------------------