const std::string &admLogin,
const std::string &admPasswd,
const std::string &str,
- void * data,
bool isMessage)
{
SERVCONF sc;
-bool result = false;
-
sc.SetServer(server.c_str());
sc.SetPort(port);
sc.SetAdmLogin(admLogin.c_str());
REQUEST request;
GetUserData cbdata(request, false);
+int res = 0;
if (isMessage)
{
sc.SetSendMessageCb(RecvSetUserAnswer, &cbdata);
- sc.MsgUser(str.c_str());
+ res = sc.MsgUser(str.c_str());
}
else
{
- sc.SetChgUserCb(RecvSetUserAnswer, &cbdata);
- sc.ChgUser(str.c_str());
+ sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata);
+ res = sc.ChgUser(str.c_str());
}
-if (result)
+if (res && cbdata.result)
{
printf("Ok\n");
return 0;
GetUserData data(request, false);
sc.SetGetUserCallback(GetUserCallback, &data);
-sc.GetUser(login.c_str());
+bool res = (sc.GetUser(login.c_str()) == st_ok);
-if (data.result)
+if (res && data.result)
{
printf("Ok\n");
return 0;
int port,
const std::string &admLogin,
const std::string &admPasswd,
- const std::string &login,
- REQUEST & request)
+ const std::string &login)
{
SERVCONF sc;
sc.SetAdmPassword(admPasswd.c_str());
sc.SetAuthByCallback(RecvAuthByData, NULL);
-sc.AuthBy(login.c_str());
+bool res = (sc.AuthBy(login.c_str()) == st_ok);
-if (sc.GetError())
+if (!res)
{
printf("Error\n");
return -1;
int port,
const std::string &admLogin,
const std::string &admPasswd,
- const std::string &login,
- REQUEST & request);
+ const std::string &login);
int ProcessSetUser(const std::string &server,
int port,
const std::string &admLogin,
const std::string &admPasswd,
const std::string &str,
- void * data,
bool isMessage = false);
#endif
}
if (req.authBy)
- return ProcessAuthBy(req.server, req.port, req.admLogin, req.admPasswd, req.login, req);
+ return ProcessAuthBy(req.server, req.port, req.admLogin, req.admPasswd, req.login);
else
return ProcessGetUser(req.server, req.port, req.admLogin, req.admPasswd, req.login, req);
}
memset(rstr, 0, rLen);
CreateRequestSet(&req, rstr);
-return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, NULL, isMessage);
+return ProcessSetUser(req.server, req.port, req.admLogin, req.admPasswd, rstr, isMessage);
}
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
#define MAXKEYBYTES 56 /* 448 bits */
#ifdef __cplusplus
+#include <cstddef> // size_t
extern "C" {
+#else
+#include <stddef.h> // size_t
#endif
typedef struct {
parser_check_user.cpp \
parser_get_user.cpp \
parser_get_users.cpp \
+ parser_chg_user.cpp \
servconf.cpp
INCS = servconf.h \
#ifndef NetUnitH
#define NetUnitH
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
+#include "stg/blowfish.h"
#include <list>
#include <string>
-#include "stg/blowfish.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
#define STG_HEADER "SG04"
#define OK_HEADER "OKHD"
class PARSER_CHG_USER: public PARSER
{
public:
+ typedef int (* CALLBACK)(const char * asnwer, void * data);
+
PARSER_CHG_USER();
- int ParseStart(const char *el, const char **attr);
- void ParseEnd(const char *el);
- void ParseAnswer(const char *el, const char **attr);
- void SetChgUserRecvCb(RecvChgUserCb_t, void * data);
+ int ParseStart(const char * el, const char ** attr);
+ void ParseEnd(const char * el);
+ void SetCallback(CALLBACK f, void * data);
private:
- RecvChgUserCb_t RecvChgUserCb;
- void * chgUserCbData;
+ CALLBACK callback;
+ void * data;
int depth;
- bool error;
+
+ void ParseAnswer(const char * el, const char ** attr);
};
#endif
#ifndef __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__
#define __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__
-#include "/parser.h"
+#include "parser.h"
#include "stg/const.h"
#include "stg/parser_check_user.h"
#include "stg/parser_get_user.h"
#include "stg/parser_get_users.h"
+#include "stg/parser_chg_user.h"
#include "stg/os_int.h"
#include "stg/const.h"
#include <expat.h>
-void Start(void *data, const char *el, const char **attr);
-void End(void *data, const char *el);
+void Start(void * data, const char * el, const char ** attr);
+void End(void * data, const char * el);
#define MAX_ERR_STR_LEN (64)
#define IP_STRING_LEN (255)
//-----------------------------------------------------------------------------
-typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data);
-typedef int(*RecvSendMessageCb_t)(const char * answer, void * data);
+typedef int (* RecvSendMessageCb_t)(const char * answer, void * data);
//-----------------------------------------------------------------------------
struct ADMINDATA
{
{
public:
PARSER_SEND_MESSAGE();
- int ParseStart(const char *el, const char **attr);
- void ParseEnd(const char *el);
- void ParseAnswer(const char *el, const char **attr);
+ int ParseStart(const char * el, const char ** attr);
+ void ParseEnd(const char * el);
+ void ParseAnswer(const char * el, const char ** attr);
void SetSendMessageRecvCb(RecvSendMessageCb_t, void * data);
private:
RecvSendMessageCb_t RecvSendMessageCb;
void * sendMessageCbData;
int depth;
- bool error;
};
//-----------------------------------------------------------------------------
class SERVCONF
void SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data);
void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data);
void SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data);
- void SetChgUserCb(RecvChgUserCb_t, void * data);
+ void SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data);
void SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data);
void SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data);
void SetSendMessageCb(RecvSendMessageCb_t, void * data);
int CheckUser(const char * login, const char * password);
const std::string & GetStrError() const;
- int GetError();
- int Start(const char *el, const char **attr);
- void End(const char *el);
+ int Start(const char * el, const char ** attr);
+ void End(const char * el);
private:
PARSER * currParser;
PARSER_SEND_MESSAGE parserSendMessage;
NETTRANSACT nt;
- int parseDepth;
std::string errorMsg;
- int error;
XML_Parser parser;
- PARSER_GET_USERS::CALLBACK getUsersCallback;
- PARSER_GET_USER::CALLBACK getUserCallback;
- PARSER_AUTH_BY::CALLBACK authByCallback;
- PARSER_SERVER_INFO::CALLBACK serverInfoCallback;
- RecvChgUserCb_t RecvChgUserCb;
- PARSER_CHECK_USER::CALLBACK checkUserCallback;
RecvSendMessageCb_t RecvSendMessageCb;
- void * getUsersData;
- void * getUserData;
- void * authByData;
- void * serverInfoData;
- void * chgUserDataCb;
- void * checkUserData;
void * sendMessageDataCb;
+ int Exec(const char * request);
+
friend int AnsRecv(void * data, std::list<std::string> * list);
};
//-----------------------------------------------------------------------------
using namespace std;
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-PARSER_CHG_USER::PARSER_CHG_USER()
- : RecvChgUserCb(NULL),
- chgUserCbData(NULL),
- depth(0),
- error(false)
-{
-}
-//-----------------------------------------------------------------------------
-int PARSER_CHG_USER::ParseStart(const char *el, const char **attr)
-{
-depth++;
-if (depth == 1)
- {
- if (strcasecmp(el, "SetUser") == 0)
- {
- ParseAnswer(el, attr);
- }
- else if (strcasecmp(el, "DelUser") == 0)
- {
- ParseAnswer(el, attr);
- }
- else if (strcasecmp(el, "AddUser") == 0)
- {
- ParseAnswer(el, attr);
- }
- }
-return 0;
-}
-//-----------------------------------------------------------------------------
-void PARSER_CHG_USER::ParseEnd(const char *)
-{
-depth--;
-}
-//-----------------------------------------------------------------------------
-void PARSER_CHG_USER::ParseAnswer(const char *, const char **attr)
-{
-if (RecvChgUserCb)
- {
- RecvChgUserCb(attr[1], chgUserCbData);
- }
-}
-//-----------------------------------------------------------------------------
-void PARSER_CHG_USER::SetChgUserRecvCb(RecvChgUserCb_t f, void * data)
-{
-RecvChgUserCb = f;
-chgUserCbData = data;
-}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
PARSER_SEND_MESSAGE::PARSER_SEND_MESSAGE()
: RecvSendMessageCb(NULL),
sendMessageCbData(NULL),
- depth(0),
- error(false)
+ depth(0)
{
}
//-----------------------------------------------------------------------------
* Author : Maxim Mamontov <faust@stargazer.dp.ua>
*/
+#include "stg/parser_chg_user.h"
+
+#include <cstddef>
+
+#include <strings.h>
+
+PARSER_CHG_USER::PARSER_CHG_USER()
+ : callback(NULL),
+ data(NULL),
+ depth(0)
+{
+}
+//-----------------------------------------------------------------------------
+int PARSER_CHG_USER::ParseStart(const char *el, const char **attr)
+{
+depth++;
+if (depth == 1)
+ {
+ if (strcasecmp(el, "SetUser") == 0)
+ ParseAnswer(el, attr);
+ else if (strcasecmp(el, "DelUser") == 0)
+ ParseAnswer(el, attr);
+ else if (strcasecmp(el, "AddUser") == 0)
+ ParseAnswer(el, attr);
+ }
+return 0;
+}
+//-----------------------------------------------------------------------------
+void PARSER_CHG_USER::ParseEnd(const char *)
+{
+depth--;
+}
+//-----------------------------------------------------------------------------
+void PARSER_CHG_USER::ParseAnswer(const char * /*el*/, const char ** attr)
+{
+if (attr && attr[0] && attr[1])
+ if (callback)
+ callback(attr[1], data);
+}
+//-----------------------------------------------------------------------------
+void PARSER_CHG_USER::SetCallback(CALLBACK f, void * d)
+{
+callback = f;
+data = d;
+}
$Author: faust $
*/
-#include <cstdio>
-#include <cstring>
+#include "stg/servconf.h"
#include "stg/common.h"
-#include "stg/servconf.h"
+
+#include <cstdio>
+#include <cstring>
using namespace std;
//-----------------------------------------------------------------------------
SERVCONF::SERVCONF()
: currParser(NULL),
- parseDepth(0),
- error(0),
- getUsersCallback(NULL),
- getUserCallback(NULL),
- authByCallback(NULL),
- serverInfoCallback(NULL),
- RecvChgUserCb(NULL),
- checkUserCallback(NULL),
RecvSendMessageCb(NULL),
- getUsersData(NULL),
- getUserData(NULL),
- authByData(NULL),
- serverInfoData(NULL),
- chgUserDataCb(NULL),
- checkUserData(NULL),
sendMessageDataCb(NULL)
{
parser = XML_ParserCreate(NULL);
+nt.SetRxCallback(this, AnsRecv);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetServer(const char * server)
{
char request[255];
snprintf(request, 255, "<GetUser login=\"%s\"/>", l);
-int ret;
currParser = &parserGetUser;
-((PARSER_GET_USER*)currParser)->SetCallback(getUserCallback, getUserData);
-
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::AuthBy(const char * l)
snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
currParser = &parserAuthBy;
-((PARSER_AUTH_BY*)currParser)->SetCallback(authByCallback, authByData);
-
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-int ret;
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::GetUsers()
{
char request[] = "<GetUsers/>";
-int ret;
currParser = &parserGetUsers;
-((PARSER_GET_USERS*)currParser)->SetCallback(getUsersCallback, getUsersData);
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::SendMessage(const char * login, const char * message, int prio)
char msg[500];
Encode12(msg, message, strlen(message));
snprintf(request, 1000, "<Message login=\"%s\" priority=\"%d\" text=\"%s\"/>", login, prio, msg);
-int ret;
currParser = &parserSendMessage;
parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::ServerInfo()
{
char request[] = "<GetServerInfo/>";
-int ret;
currParser = &parserServerInfo;
-((PARSER_SERVER_INFO*)currParser)->SetCallback(serverInfoCallback, serverInfoData);
-
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::ChgUser(const char * request)
{
-int ret;
-
currParser = &parserChgUser;
-((PARSER_CHG_USER*)currParser)->SetChgUserRecvCb(RecvChgUserCb, chgUserDataCb);
-
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
// TODO: remove this shit!
//-----------------------------------------------------------------------------
int SERVCONF::MsgUser(const char * request)
{
-int ret;
-
currParser = &parserSendMessage;
parserSendMessage.SetSendMessageRecvCb(RecvSendMessageCb, sendMessageDataCb);
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::CheckUser(const char * login, const char * password)
{
char request[255];
snprintf(request, 255, "<CheckUser login=\"%s\" password=\"%s\"/>", login, password);
-int ret;
currParser = &parserCheckUser;
-((PARSER_CHECK_USER*)currParser)->SetCallback(checkUserCallback, checkUserData);
-
-nt.Reset();
-nt.SetRxCallback(this, AnsRecv);
-if ((ret = nt.Connect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Transact(request)) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-if ((ret = nt.Disconnect()) != st_ok)
- {
- errorMsg = nt.GetError();
- return ret;
- }
-
-return st_ok;
+return Exec(request);
}
//-----------------------------------------------------------------------------
int SERVCONF::Start(const char *el, const char **attr)
//-----------------------------------------------------------------------------
void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data)
{
-getUsersCallback = f;
-getUsersData = data;
+parserGetUsers.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)
{
-getUserCallback = f;
-getUserData = data;
+parserGetUser.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
{
-authByCallback = f;
-authByData = data;
+parserAuthBy.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data)
{
-serverInfoCallback = f;
-serverInfoData = data;
+parserServerInfo.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
-void SERVCONF::SetChgUserCb(RecvChgUserCb_t f, void * data)
+void SERVCONF::SetChgUserCallback(PARSER_CHG_USER::CALLBACK f, void * data)
{
-RecvChgUserCb = f;
-chgUserDataCb = data;
+parserChgUser.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetCheckUserCallback(PARSER_CHECK_USER::CALLBACK f, void * data)
{
-checkUserCallback = f;
-checkUserData = data;
+parserCheckUser.SetCallback(f, data);
}
//-----------------------------------------------------------------------------
void SERVCONF::SetSendMessageCb(RecvSendMessageCb_t f, void * data)
return errorMsg;
}
//-----------------------------------------------------------------------------
-int SERVCONF::GetError()
+int SERVCONF::Exec(const char * request)
{
-int e = error;
-error = 0;
-return e;
+nt.Reset();
+
+int ret = 0;
+if ((ret = nt.Connect()) != st_ok)
+ {
+ errorMsg = nt.GetError();
+ return ret;
+ }
+if ((ret = nt.Transact(request)) != st_ok)
+ {
+ errorMsg = nt.GetError();
+ return ret;
+ }
+if ((ret = nt.Disconnect()) != st_ok)
+ {
+ errorMsg = nt.GetError();
+ return ret;
+ }
+
+return st_ok;
}
-//-----------------------------------------------------------------------------