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"
31 //-----------------------------------------------------------------------------
32 void Start(void *data, const char *el, const char **attr)
34 SERVCONF * sc = static_cast<SERVCONF *>(data);
37 //-----------------------------------------------------------------------------
38 void End(void * data, const char * el)
40 SERVCONF * sc = static_cast<SERVCONF *>(data);
44 } // namespace anonymous
46 int AnsRecv(void * data, std::list<std::string> * list1)
48 SERVCONF * sc = static_cast<SERVCONF *>(data);
50 XML_ParserReset(sc->parser, NULL);
51 XML_SetElementHandler(sc->parser, Start, End);
52 XML_SetUserData(sc->parser, data);
54 char ans[ENC_MSG_LEN + 1];
58 std::list<std::string>::iterator node;
59 node = list1->begin();
61 while (node != list1->end())
63 strncpy(ans, node->c_str(), ENC_MSG_LEN);
67 if (XML_Parse(sc->parser, ans, len, done) == XML_STATUS_ERROR)
69 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
70 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
71 XML_ErrorString(XML_GetErrorCode(sc->parser)));
72 printf("%s\n", sc->errorMsg.c_str());
73 return st_xml_parse_error;
81 //-----------------------------------------------------------------------------
82 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
83 const std::string & login, const std::string & password)
85 nt( server, port, login, password )
87 parser = XML_ParserCreate(NULL);
88 nt.SetRxCallback(this, AnsRecv);
90 //-----------------------------------------------------------------------------
91 int SERVCONF::GetUser(const char * l)
94 snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
96 currParser = &parserGetUser;
100 //-----------------------------------------------------------------------------
101 int SERVCONF::AuthBy(const char * l)
104 snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
106 currParser = &parserAuthBy;
108 return Exec(request);
110 //-----------------------------------------------------------------------------
111 int SERVCONF::GetUsers()
113 char request[] = "<GetUsers/>";
115 currParser = &parserGetUsers;
117 return Exec(request);
119 //-----------------------------------------------------------------------------
120 int SERVCONF::ServerInfo()
122 char request[] = "<GetServerInfo/>";
124 currParser = &parserServerInfo;
126 return Exec(request);
128 //-----------------------------------------------------------------------------
129 int SERVCONF::ChgUser(const char * request)
131 currParser = &parserChgUser;
133 return Exec(request);
135 //-----------------------------------------------------------------------------
136 int SERVCONF::SendMessage(const char * request)
138 currParser = &parserSendMessage;
140 return Exec(request);
142 //-----------------------------------------------------------------------------
143 int SERVCONF::CheckUser(const char * login, const char * password)
146 snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
148 currParser = &parserCheckUser;
150 return Exec(request);
152 //-----------------------------------------------------------------------------
153 int SERVCONF::Start(const char * el, const char ** attr)
155 currParser->ParseStart(el, attr);
158 //-----------------------------------------------------------------------------
159 void SERVCONF::End(const char * el)
161 currParser->ParseEnd(el);
163 //-----------------------------------------------------------------------------
164 void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
166 parserGetUsers.SetCallback(f, data);
168 //-----------------------------------------------------------------------------
169 void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
171 parserGetUser.SetCallback(f, data);
173 //-----------------------------------------------------------------------------
174 void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
176 parserAuthBy.SetCallback(f, data);
178 //-----------------------------------------------------------------------------
179 void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
181 parserServerInfo.SetCallback(f, data);
183 //-----------------------------------------------------------------------------
184 void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
186 parserChgUser.SetCallback(f, data);
188 //-----------------------------------------------------------------------------
189 void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
191 parserCheckUser.SetCallback(f, data);
193 //-----------------------------------------------------------------------------
194 void SERVCONF::SetSendMessageCallback(PARSER_SEND_MESSAGE::CALLBACK f, void * data)
196 parserSendMessage.SetCallback(f, data);
198 //-----------------------------------------------------------------------------
199 const std::string & SERVCONF::GetStrError() const
203 //-----------------------------------------------------------------------------
204 int SERVCONF::Exec(const char * request)
209 if ((ret = nt.Connect()) != st_ok)
211 errorMsg = nt.GetError();
214 if ((ret = nt.Transact(request)) != st_ok)
216 errorMsg = nt.GetError();
219 if ((ret = nt.Disconnect()) != st_ok)
221 errorMsg = nt.GetError();