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"
25 #include "parsers/simple.h"
27 #include "parsers/server_info.h"
29 #include "parsers/get_admins.h"
30 #include "parsers/get_admin.h"
31 #include "parsers/chg_admin.h"
33 #include "parsers/auth_by.h"
34 #include "parsers/get_users.h"
35 #include "parsers/get_user.h"
36 #include "parsers/chg_user.h"
38 #include "parsers/base.h"
40 #include "stg/common.h"
52 IMPL(const std::string & server, uint16_t port,
53 const std::string & login, const std::string & password);
55 const std::string & GetStrError() const;
56 static void Start(void * data, const char * el, const char ** attr);
57 static void End(void * data, const char * el);
59 template <class P, typename C>
60 int Exec(const std::string & request, C callback, void * data)
63 return ExecImpl(request, cp);
66 template <class P, typename C>
67 int Exec(const std::string & tag, const std::string & request, C callback, void * data)
69 P cp(tag, callback, data);
70 return ExecImpl(request, cp);
79 static bool AnsRecv(void * data, const std::string & chunk, bool final);
80 int ExecImpl(const std::string & request, PARSER & cp);
83 bool SERVCONF::IMPL::AnsRecv(void * data, const std::string & chunk, bool final)
85 SERVCONF::IMPL * sc = static_cast<SERVCONF::IMPL *>(data);
87 if (XML_Parse(sc->parser, chunk.c_str(), chunk.length(), final) == XML_STATUS_ERROR)
89 strprintf(&sc->errorMsg, "XML parse error at line %d: %s",
90 static_cast<int>(XML_GetCurrentLineNumber(sc->parser)),
91 XML_ErrorString(XML_GetErrorCode(sc->parser)));
92 printf("%s\n", sc->errorMsg.c_str());
99 SERVCONF::SERVCONF(const std::string & server, uint16_t port,
100 const std::string & login, const std::string & password)
101 : pImpl(new IMPL(server, port, login, password))
105 SERVCONF::~SERVCONF()
110 int SERVCONF::ServerInfo(SERVER_INFO::CALLBACK f, void * data)
112 return pImpl->Exec<SERVER_INFO::PARSER>("<GetServerInfo/>", f, data);
117 int SERVCONF::GetAdmins(GET_ADMINS::CALLBACK f, void * data)
119 return pImpl->Exec<GET_ADMINS::PARSER>("<GetAdmins/>", f, data);
122 int SERVCONF::GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data)
124 return pImpl->Exec<GET_ADMIN::PARSER>("<GetAdmin login=\"" + login + "\"/>", f, data);
127 int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data)
129 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
132 int SERVCONF::AddAdmin(const std::string & login,
133 const ADMIN_CONF_RES & conf,
134 SIMPLE::CALLBACK f, void * data)
136 int res = pImpl->Exec<SIMPLE::PARSER>("AddAdmin", "<AddAdmin login=\"" + login + "\"/>", f, data);
139 return pImpl->Exec<SIMPLE::PARSER>("ChgAdmin", "<ChgAdmin" + CHG_ADMIN::Serialize(conf) + "/>", f, data);
142 int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data)
144 return pImpl->Exec<SIMPLE::PARSER>("DelAdmin", "<DelAdmin login=\"" + login + "\"/>", f, data);
149 int SERVCONF::GetUsers(GET_USERS::CALLBACK f, void * data)
151 return pImpl->Exec<GET_USERS::PARSER>("<GetUsers/>", f, data);
154 int SERVCONF::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data)
156 return pImpl->Exec<GET_USER::PARSER>("<GetUser login=\"" + login + "\"/>", f, data);
159 int SERVCONF::ChgUser(const std::string & login,
160 const USER_CONF_RES & conf,
161 const USER_STAT_RES & stat,
162 SIMPLE::CALLBACK f, void * data)
164 return pImpl->Exec<CHG_USER::PARSER>("<SetUser><Login value=\"" + login + "\"/>" + CHG_USER::Serialize(conf, stat) + "</SetUser>", f, data);
167 int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
169 return pImpl->Exec<SIMPLE::PARSER>("DelUser", "<DelUser login=\"" + login + "\"/>", f, data);
172 int SERVCONF::AddUser(const std::string & login, SIMPLE::CALLBACK f, void * data)
174 return pImpl->Exec<SIMPLE::PARSER>("AddUser", "<AddUser><Login value=\"" + login + "\"/></AddUser>", f, data);
177 int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data)
179 return pImpl->Exec<AUTH_BY::PARSER>("<GetUserAuthBy login=\"" + login + "\"/>", f, data);
182 int SERVCONF::SendMessage(const std::string & login, const std::string & text, SIMPLE::CALLBACK f, void * data)
184 return pImpl->Exec<SIMPLE::PARSER>("SendMessage", "<Message login=\"" + login + "\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"" + Encode12str(text) + "\"/>", f, data);
187 int SERVCONF::CheckUser(const std::string & login, const std::string & password, SIMPLE::CALLBACK f, void * data)
189 return pImpl->Exec<SIMPLE::PARSER>("CheckUser", "<CheckUser login=\"" + login + "\" password=\"" + password + "\"/>", f, data);
192 const std::string & SERVCONF::GetStrError() const
194 return pImpl->GetStrError();
197 //-----------------------------------------------------------------------------
198 SERVCONF::IMPL::IMPL(const std::string & server, uint16_t port,
199 const std::string & login, const std::string & password)
200 : nt( server, port, login, password )
202 parser = XML_ParserCreate(NULL);
203 nt.SetRxCallback(this, AnsRecv);
205 //-----------------------------------------------------------------------------
206 void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr)
208 PARSER * currParser = static_cast<PARSER *>(data);
209 currParser->ParseStart(el, attr);
211 //-----------------------------------------------------------------------------
212 void SERVCONF::IMPL::End(void * data, const char * el)
214 PARSER * currParser = static_cast<PARSER *>(data);
215 currParser->ParseEnd(el);
217 //-----------------------------------------------------------------------------
218 const std::string & SERVCONF::IMPL::GetStrError() const
222 //-----------------------------------------------------------------------------
223 int SERVCONF::IMPL::ExecImpl(const std::string & request, PARSER & cp)
225 XML_ParserReset(parser, NULL);
226 XML_SetElementHandler(parser, Start, End);
227 XML_SetUserData(parser, &cp);
230 if ((ret = nt.Connect()) != st_ok)
232 errorMsg = nt.GetError();
235 if ((ret = nt.Transact(request.c_str())) != st_ok)
237 errorMsg = nt.GetError();
240 if ((ret = nt.Disconnect()) != st_ok)
242 errorMsg = nt.GetError();