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 $
34 //-----------------------------------------------------------------------------
35 int AnsRecv(void * data, list<string> * list1)
39 char ans[ENC_MSG_LEN + 1];
44 XML_ParserReset(sc->parser, NULL);
45 XML_SetElementHandler(sc->parser, Start, End);
46 XML_SetUserData(sc->parser, data);
49 list<string>::iterator node;
50 node = list1->begin();
52 if (node == list1->end())
57 while (node != list1->end())
59 strncpy(ans, node->c_str(), ENC_MSG_LEN);
61 //printf("---> %s\n", ans);
64 if (XML_Parse(sc->parser, ans, len, done) == XML_STATUS_ERROR)
66 snprintf(sc->errorMsg, MAX_ERR_STR_LEN, "XML parse error at line %d: %s",
67 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
68 XML_ErrorString(XML_GetErrorCode(sc->parser)));
69 printf(sc->errorMsg, "XML parse error at line %d: %s",
70 XML_GetCurrentLineNumber(sc->parser),
71 XML_ErrorString(XML_GetErrorCode(sc->parser)));
72 return st_xml_parse_error;
80 //-----------------------------------------------------------------------------
81 void Start(void *data, const char *el, const char **attr)
87 //-----------------------------------------------------------------------------
88 void End(void *data, const char *el)
94 //-----------------------------------------------------------------------------
97 parser = XML_ParserCreate(NULL);
100 //-----------------------------------------------------------------------------
101 void SERVCONF::SetServer(const char * server)
103 nt.SetServer(server);
105 //-----------------------------------------------------------------------------
106 void SERVCONF::SetPort(uint16_t port)
108 nt.SetServerPort(port);
110 //-----------------------------------------------------------------------------
111 void SERVCONF::SetAdmLogin(const char * login)
115 //-----------------------------------------------------------------------------
116 void SERVCONF::SetAdmPassword(const char * password)
118 nt.SetPassword(password);
120 //-----------------------------------------------------------------------------
121 int SERVCONF::GetUser(const char * l)
124 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
127 currParser = &parserGetUser;
128 ((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb);
131 nt.SetRxCallback(this, AnsRecv);
133 if ((ret = nt.Connect()) != st_ok)
135 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
138 if ((ret = nt.Transact(request)) != st_ok)
140 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
143 if ((ret = nt.Disconnect()) != st_ok)
145 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
151 //-----------------------------------------------------------------------------
152 int SERVCONF::GetUsers()
154 char request[] = "<GetUsers/>";
157 currParser = &parserGetUsers;
158 ((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb);
161 nt.SetRxCallback(this, AnsRecv);
163 if ((ret = nt.Connect()) != st_ok)
165 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
168 if ((ret = nt.Transact(request)) != st_ok)
170 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
173 if ((ret = nt.Disconnect()) != st_ok)
175 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
181 //-----------------------------------------------------------------------------
182 int SERVCONF::SendMessage(const char * login, const char * message, int prio)
186 Encode12(msg, message, strlen(message));
187 snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
190 currParser = &parserSendMessage;
191 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
194 nt.SetRxCallback(this, AnsRecv);
196 if ((ret = nt.Connect()) != st_ok)
198 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
201 if ((ret = nt.Transact(request)) != st_ok)
203 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
206 if ((ret = nt.Disconnect()) != st_ok)
208 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
214 //-----------------------------------------------------------------------------
215 int SERVCONF::GetServerInfo()
217 char request[] = "<GetServerInfo/>";
220 currParser = &parserServerInfo;
221 ((PARSER_GET_SERVER_INFO*)currParser)->SetServerInfoRecvCb(RecvServerInfoDataCb, getServerInfoDataCb);
224 nt.SetRxCallback(this, AnsRecv);
226 if ((ret = nt.Connect()) != st_ok)
228 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
231 if ((ret = nt.Transact(request)) != st_ok)
233 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
236 if ((ret = nt.Disconnect()) != st_ok)
238 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
244 //-----------------------------------------------------------------------------
245 int SERVCONF::ChgUser(const char * request)
249 currParser = &parserChgUser;
250 ((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
253 nt.SetRxCallback(this, AnsRecv);
255 if ((ret = nt.Connect()) != st_ok)
257 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
258 printfd(__FILE__, "Error on connect: '%s'\n", errorMsg);
261 if ((ret = nt.Transact(request)) != st_ok)
263 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
264 printfd(__FILE__, "Error on transact: '%s'\n", errorMsg);
267 if ((ret = nt.Disconnect()) != st_ok)
269 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
270 printfd(__FILE__, "Error on disconnect: '%s'\n", errorMsg);
276 //-----------------------------------------------------------------------------
277 // TODO: remove this shit!
278 //-----------------------------------------------------------------------------
279 int SERVCONF::MsgUser(const char * request)
283 currParser = &parserSendMessage;
284 parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
287 nt.SetRxCallback(this, AnsRecv);
289 if ((ret = nt.Connect()) != st_ok)
291 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
294 if ((ret = nt.Transact(request)) != st_ok)
296 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
299 if ((ret = nt.Disconnect()) != st_ok)
301 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
307 //-----------------------------------------------------------------------------
308 int SERVCONF::CheckUser(const char * login, const char * password)
311 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
314 currParser = &parserCheckUser;
315 ((PARSER_CHECK_USER*)currParser)->SetCheckUserRecvCb(RecvCheckUserCb, checkUserDataCb);
318 nt.SetRxCallback(this, AnsRecv);
320 if ((ret = nt.Connect()) != st_ok)
322 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
325 if ((ret = nt.Transact(request)) != st_ok)
327 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
330 if ((ret = nt.Disconnect()) != st_ok)
332 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
338 //-----------------------------------------------------------------------------
339 int SERVCONF::Start(const char *el, const char **attr)
341 currParser->ParseStart(el, attr);
344 //-----------------------------------------------------------------------------
345 void SERVCONF::End(const char *el)
347 currParser->ParseEnd(el);
349 //-----------------------------------------------------------------------------
350 void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data)
353 getUsersDataDataCb = data;
355 //-----------------------------------------------------------------------------
356 void SERVCONF::SetGetUserDataRecvCb(RecvUserDataCb_t f, void * data)
358 RecvGetUserDataCb = f; //GET_USER
359 getUserDataDataCb = data;
361 //-----------------------------------------------------------------------------
362 void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)
364 RecvServerInfoDataCb = f;
365 getServerInfoDataCb = data;
367 //-----------------------------------------------------------------------------
368 void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
371 chgUserDataCb = data;
373 //-----------------------------------------------------------------------------
374 void SERVCONF::SetCheckUserCb(RecvCheckUserCb_t f, void * data)
377 checkUserDataCb = data;
379 //-----------------------------------------------------------------------------
380 void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
382 RecvSendMessageCb = f;
383 sendMessageDataCb = data;
385 //-----------------------------------------------------------------------------
386 char * SERVCONF::GetStrError()
390 //-----------------------------------------------------------------------------
391 int SERVCONF::GetError()
397 //-----------------------------------------------------------------------------