void * data;
bool * result;
};
-//-----------------------------------------------------------------------------
-struct AuthByCbData
-{
- void * data;
- bool * result;
-};
//---------------------------------------------------------------------------
struct HelpParams
{
*result = true;
}
//-----------------------------------------------------------------------------
-void RecvAuthByData(const std::vector<std::string> & list, void * d)
+void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
{
-AuthByCbData * abcbd;
-abcbd = (AuthByCbData *)d;
-
-bool * result = abcbd->result;
-
-REQUEST * req = (REQUEST *)abcbd->data;
-
for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
cout << *it << "\n";
cout << endl;
-
-*result = true;
}
//-----------------------------------------------------------------------------
int ProcessSetUser(const std::string &server,
{
SERVCONF sc;
-bool result = false;
-
-sc.SetServer(server.c_str()); // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
-sc.SetPort(port); // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
-sc.SetAdmLogin(admLogin.c_str()); // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉÎ É ÐÁÒÏÌØ ÁÄÍÉÎÁ
+sc.SetServer(server.c_str());
+sc.SetPort(port);
+sc.SetAdmLogin(admLogin.c_str());
sc.SetAdmPassword(admPasswd.c_str());
-// TODO Good variable name :)
-AuthByCbData abcbd;
-
-abcbd.data = data;
-abcbd.result = &result;
-
-sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
-sc.GetUserAuthBy(login.c_str());
+sc.SetAuthByCallback(RecvAuthByData, NULL);
+sc.AuthBy(login.c_str());
-if (result)
- {
- printf("Ok\n");
- return 0;
- }
-else
+if (sc.GetError())
{
printf("Error\n");
return -1;
}
+printf("Ok\n");
return 0;
}
//-----------------------------------------------------------------------------
-lstgcrypto
LIBS = -lexpat
-SRCS = netunit.cpp \
+SRCS = netunit.cpp \
parser.cpp \
+ parser_auth_by.cpp \
servconf.cpp
INCS = servconf.h \
netunit.h
LIB_INCS = -I ../common.lib/include \
- -I ../crypto.lib/include
+ -I ../crypto.lib/include
include ../Makefile.in
--- /dev/null
+#ifndef __STG_STGLIBS_SRVCONF_PARSER_H__
+#define __STG_STGLIBS_SRVCONF_PARSER_H__
+
+class PARSER
+{
+public:
+ virtual ~PARSER() {}
+ virtual int ParseStart(const char *el, const char **attr) = 0;
+ virtual void ParseEnd(const char *el) = 0;
+};
+
+#endif
--- /dev/null
+#ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__
+#define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__
+
+#include "stg/parser.h"
+
+#include <vector>
+#include <string>
+
+class PARSER_AUTH_BY: public PARSER
+{
+public:
+ typedef std::vector<std::string> INFO;
+ typedef void (* CALLBACK)(const INFO & info, void * data);
+
+ PARSER_AUTH_BY();
+ int ParseStart(const char *el, const char **attr);
+ void ParseEnd(const char *el);
+ void SetCallback(CALLBACK f, void * data);
+private:
+ CALLBACK callback;
+ void * data;
+ int depth;
+ INFO info;
+};
+
+#endif
#ifndef SERVCONF_H
#define SERVCONF_H
-#include <expat.h>
+#include "netunit.h"
+
+#include "stg/parser_auth_by.h"
+#include "stg/os_int.h"
+#include "stg/const.h"
#include <list>
#include <vector>
#include <string>
-#include "stg/os_int.h"
-#include "stg/const.h"
-#include "netunit.h"
+#include <expat.h>
void Start(void *data, const char *el, const char **attr);
void End(void *data, const char *el);
typedef int(*RecvChgUserCb_t)(const char * asnwer, void * data);
typedef int(*RecvCheckUserCb_t)(const char * answer, void * data);
typedef int(*RecvSendMessageCb_t)(const char * answer, void * data);
-typedef void(*RecvAuthByDataCb_t)(const std::vector<std::string> & list, void * data);
//-----------------------------------------------------------------------------
struct ADMINDATA
{
char login[ADM_LOGIN_LEN];
};
//-----------------------------------------------------------------------------
-class PARSER
-{
-public:
- PARSER() {}
- virtual ~PARSER() {}
- virtual int ParseStart(const char *el, const char **attr) = 0;
- virtual void ParseEnd(const char *el) = 0;
-};
-//-----------------------------------------------------------------------------
class PARSER_CHG_USER: public PARSER
{
public:
bool error;
};
//-----------------------------------------------------------------------------
-class PARSER_AUTH_BY: public PARSER
-{
-public:
- PARSER_AUTH_BY();
- int ParseStart(const char *el, const char **attr);
- void ParseEnd(const char *el);
- void ParseServerInfo(const char *el, const char **attr);
- bool GetError();
- void SetRecvCb(RecvAuthByDataCb_t, void * data);
-private:
- RecvAuthByDataCb_t RecvAuthByDataCb;
- void * authByDataCb;
- int depth;
- bool error;
- std::vector<std::string> list;
-};
-//-----------------------------------------------------------------------------
class SERVCONF
{
public:
void SetAdmPassword(const char * password);
void SetUserDataRecvCb(RecvUserDataCb_t, void * data);
- void SetGetUserAuthByRecvCb(RecvAuthByDataCb_t, void * data);
+ void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data);
void SetServerInfoRecvCb(RecvServerInfoDataCb_t, void * data);
void SetChgUserCb(RecvChgUserCb_t, void * data);
void SetCheckUserCb(RecvCheckUserCb_t, void * data);
int GetUsers();
int GetUser(const char * login);
int ChgUser(const char * request);
- int GetUserAuthBy(const char * login);
+ int AuthBy(const char * login);
// TODO: Remove this shit!
int MsgUser(const char * request);
int SendMessage(const char * login, const char * message, int prio);
RecvUserDataCb_t RecvUserDataCb;
RecvUserDataCb_t RecvGetUserDataCb;
- RecvAuthByDataCb_t RecvAuthByCb;
+ PARSER_AUTH_BY::CALLBACK authByCallback;
RecvServerInfoDataCb_t RecvServerInfoDataCb;
RecvChgUserCb_t RecvChgUserCb;
RecvCheckUserCb_t RecvCheckUserCb;
RecvSendMessageCb_t RecvSendMessageCb;
void * getUserDataDataCb;
- void * getUserAuthByDataCb;
+ void * authByData;
void * getUsersDataDataCb;
void * getServerInfoDataCb;
void * chgUserDataCb;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-PARSER_AUTH_BY::PARSER_AUTH_BY()
- : RecvAuthByDataCb(NULL),
- authByDataCb(NULL),
- depth(0),
- error(false),
- list()
-{
-}
-//-----------------------------------------------------------------------------
-int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr)
-{
-depth++;
-if (depth == 1)
- {
- if (strcasecmp(el, "AuthorizedBy") != 0)
- {
- list.erase(list.begin(), list.end());
- //printf("%s\n", el);
- }
- }
-else
- {
- if (depth == 2)
- {
- if (strcasecmp(el, "Auth") == 0)
- {
- if (attr && attr[0] && attr[1])
- list.push_back(attr[1]);
- return 0;
- }
- }
- }
-return 0;
-}
-//-----------------------------------------------------------------------------
-void PARSER_AUTH_BY::ParseEnd(const char *)
-{
-depth--;
-if (depth == 0)
- {
- RecvAuthByDataCb(list, authByDataCb);
- }
-}
-//-----------------------------------------------------------------------------
-void PARSER_AUTH_BY::SetRecvCb(RecvAuthByDataCb_t f, void * data)
-{
-RecvAuthByDataCb = f;
-authByDataCb = data;
-}
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
PARSER_CHG_USER::PARSER_CHG_USER()
: RecvChgUserCb(NULL),
chgUserCbData(NULL),
--- /dev/null
+#include "stg/parser_auth_by.h"
+
+#include <strings.h> // strcasecmp
+
+PARSER_AUTH_BY::PARSER_AUTH_BY()
+ : callback(NULL),
+ data(NULL),
+ depth(0)
+{
+}
+//-----------------------------------------------------------------------------
+int PARSER_AUTH_BY::ParseStart(const char *el, const char **attr)
+{
+depth++;
+if (depth == 1)
+ {
+ if (strcasecmp(el, "AuthorizedBy") != 0)
+ info.clear();
+ }
+else
+ {
+ if (depth == 2)
+ {
+ if (strcasecmp(el, "Auth") == 0)
+ {
+ if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0)
+ info.push_back(attr[1]);
+ return 0;
+ }
+ }
+ }
+return 0;
+}
+//-----------------------------------------------------------------------------
+void PARSER_AUTH_BY::ParseEnd(const char * /*el*/)
+{
+depth--;
+if (depth == 0)
+ callback(info, data);
+}
+//-----------------------------------------------------------------------------
+void PARSER_AUTH_BY::SetCallback(CALLBACK f, void * data)
+{
+callback = f;
+data = data;
+}
error(0),
RecvUserDataCb(NULL),
RecvGetUserDataCb(NULL),
+ authByCallback(NULL),
RecvServerInfoDataCb(NULL),
RecvChgUserCb(NULL),
RecvCheckUserCb(NULL),
RecvSendMessageCb(NULL),
getUserDataDataCb(NULL),
- getUserAuthByDataCb(NULL),
+ authByData(NULL),
getUsersDataDataCb(NULL),
getServerInfoDataCb(NULL),
chgUserDataCb(NULL),
return st_ok;
}
//-----------------------------------------------------------------------------
-int SERVCONF::GetUserAuthBy(const char * l)
+int SERVCONF::AuthBy(const char * l)
{
char request[255];
snprintf(request, 255, "<GetUserAuthBy login=\"%s\"/>", l);
-int ret;
currParser = &parserAuthBy;
-((PARSER_AUTH_BY*)currParser)->SetRecvCb(RecvAuthByCb, getUserAuthByDataCb);
+((PARSER_AUTH_BY*)currParser)->SetCallback(authByCallback, authByData);
nt.Reset();
nt.SetRxCallback(this, AnsRecv);
+int ret;
if ((ret = nt.Connect()) != st_ok)
{
errorMsg = nt.GetError();
getUserDataDataCb = data;
}
//-----------------------------------------------------------------------------
-void SERVCONF::SetGetUserAuthByRecvCb(RecvAuthByDataCb_t f, void * data)
+void SERVCONF::SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data)
{
-RecvAuthByCb = f;
-getUserAuthByDataCb = data;
+authByCallback = f;
+authByData = data;
}
//-----------------------------------------------------------------------------
void SERVCONF::SetServerInfoRecvCb(RecvServerInfoDataCb_t f, void * data)