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>
21 #include "stg/servconf.h"
23 #include "stg/common.h"
28 //-----------------------------------------------------------------------------
29 int AnsRecv(void * data, std::list<std::string> * list1)
31 SERVCONF * sc = static_cast<SERVCONF *>(data);
33 XML_ParserReset(sc->parser, NULL);
34 XML_SetElementHandler(sc->parser, Start, End);
35 XML_SetUserData(sc->parser, data);
37 char ans[ENC_MSG_LEN + 1];
41 std::list<std::string>::iterator node;
42 node = list1->begin();
44 while (node != list1->end())
46 strncpy(ans, node->c_str(), ENC_MSG_LEN);
50 if (XML_Parse(sc->parser, ans, len, done) == XML_STATUS_ERROR)
52 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
53 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
54 XML_ErrorString(XML_GetErrorCode(sc->parser)));
55 printf("%s\n", sc->errorMsg.c_str());
56 return st_xml_parse_error;
63 //-----------------------------------------------------------------------------
64 void Start(void *data, const char *el, const char **attr)
66 SERVCONF * sc = static_cast<SERVCONF *>(data);
69 //-----------------------------------------------------------------------------
70 void End(void * data, const char * el)
72 SERVCONF * sc = static_cast<SERVCONF *>(data);
75 //-----------------------------------------------------------------------------
79 parser = XML_ParserCreate(NULL);
80 nt.SetRxCallback(this, AnsRecv);
82 //-----------------------------------------------------------------------------
83 void SERVCONF::SetServer(const char * server)
87 //-----------------------------------------------------------------------------
88 void SERVCONF::SetPort(uint16_t port)
90 nt.SetServerPort(port);
92 //-----------------------------------------------------------------------------
93 void SERVCONF::SetAdmLogin(const char * login)
97 //-----------------------------------------------------------------------------
98 void SERVCONF::SetAdmPassword(const char * password)
100 nt.SetPassword(password);
102 //-----------------------------------------------------------------------------
103 int SERVCONF::GetUser(const char * l)
106 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
108 currParser = &parserGetUser;
110 return Exec(request);
112 //-----------------------------------------------------------------------------
113 int SERVCONF::AuthBy(const char * l)
116 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
118 currParser = &parserAuthBy;
120 return Exec(request);
122 //-----------------------------------------------------------------------------
123 int SERVCONF::GetUsers()
125 char request[] = "<GetUsers/>";
127 currParser = &parserGetUsers;
129 return Exec(request);
131 //-----------------------------------------------------------------------------
132 int SERVCONF::ServerInfo()
134 char request[] = "<GetServerInfo/>";
136 currParser = &parserServerInfo;
138 return Exec(request);
140 //-----------------------------------------------------------------------------
141 int SERVCONF::ChgUser(const char * request)
143 currParser = &parserChgUser;
145 return Exec(request);
147 //-----------------------------------------------------------------------------
148 int SERVCONF::SendMessage(const char * request)
150 currParser = &parserSendMessage;
152 return Exec(request);
154 //-----------------------------------------------------------------------------
155 int SERVCONF::CheckUser(const char * login, const char * password)
158 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
160 currParser = &parserCheckUser;
162 return Exec(request);
164 //-----------------------------------------------------------------------------
165 int SERVCONF::Start(const char * el, const char ** attr)
167 currParser->ParseStart(el, attr);
170 //-----------------------------------------------------------------------------
171 void SERVCONF::End(const char * el)
173 currParser->ParseEnd(el);
175 //-----------------------------------------------------------------------------
176 void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
178 parserGetUsers.SetCallback(f, data);
180 //-----------------------------------------------------------------------------
181 void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
183 parserGetUser.SetCallback(f, data);
185 //-----------------------------------------------------------------------------
186 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
188 parserAuthBy.SetCallback(f, data);
190 //-----------------------------------------------------------------------------
191 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
193 parserServerInfo.SetCallback(f, data);
195 //-----------------------------------------------------------------------------
196 void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
198 parserChgUser.SetCallback(f, data);
200 //-----------------------------------------------------------------------------
201 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
203 parserCheckUser.SetCallback(f, data);
205 //-----------------------------------------------------------------------------
206 void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data)
208 parserSendMessage.SetCallback(f, data);
210 //-----------------------------------------------------------------------------
211 const std::string & SERVCONF::GetStrError() const
215 //-----------------------------------------------------------------------------
216 int SERVCONF::Exec(const char * request)
221 if ((ret = nt.Connect()) != st_ok)
223 errorMsg = nt.GetError();
226 if ((ret = nt.Transact(request)) != st_ok)
228 errorMsg = nt.GetError();
231 if ((ret = nt.Disconnect()) != st_ok)
233 errorMsg = nt.GetError();