mirror of
https://github.com/znc/znc.git
synced 2026-08-07 17:33:34 +02:00
finished building out module/user based registry system (round 1)
git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@299 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
+50
@@ -38,12 +38,54 @@ CModule::CModule(void* pDLL, CUser* pUser, const CString& sModName) {
|
||||
m_pManager = pUser->GetManager();
|
||||
m_pUser = pUser;
|
||||
m_sModName = sModName;
|
||||
LoadRegistry();
|
||||
}
|
||||
|
||||
CModule::~CModule() {
|
||||
while (m_vTimers.size()) {
|
||||
RemTimer(m_vTimers[0]->GetName());
|
||||
}
|
||||
SaveRegistry();
|
||||
}
|
||||
|
||||
bool CModule::LoadRegistry()
|
||||
{
|
||||
CString sRegistryDir = m_pUser->GetDataPath() + "/" + m_sModName;
|
||||
CUtils::MakeDir( sRegistryDir );
|
||||
return( ( m_mssRegistry.ReadFromDisk( sRegistryDir + "/" + m_pUser->GetUserName() + "-registry.txt", 0600 ) == MCString::MCS_SUCCESS ) );
|
||||
}
|
||||
|
||||
bool CModule::SaveRegistry()
|
||||
{
|
||||
CString sRegistryDir = m_pUser->GetDataPath() + "/" + m_sModName;
|
||||
CUtils::MakeDir( sRegistryDir );
|
||||
return( ( m_mssRegistry.WriteToDisk( sRegistryDir + "/" + m_pUser->GetUserName() + "-registry.txt", 0600 ) == MCString::MCS_SUCCESS ) );
|
||||
}
|
||||
|
||||
bool CModule::SetNV( const CString & sName, const CString & sValue, bool bWriteToDisk )
|
||||
{
|
||||
m_mssRegistry[sName] = sValue;
|
||||
if ( bWriteToDisk )
|
||||
return( SaveRegistry() );
|
||||
|
||||
return( true );
|
||||
}
|
||||
|
||||
CString CModule::GetNV( const CString & sName )
|
||||
{
|
||||
return( m_mssRegistry[sName] );
|
||||
}
|
||||
|
||||
bool CModule::DelNV( const CString & sName, bool bWriteToDisk )
|
||||
{
|
||||
MCString::iterator it = m_mssRegistry.find( sName );
|
||||
if ( it != m_mssRegistry.end() )
|
||||
m_mssRegistry.erase( it );
|
||||
|
||||
if ( bWriteToDisk )
|
||||
return( SaveRegistry() );
|
||||
|
||||
return( true );
|
||||
}
|
||||
|
||||
bool CModule::AddTimer(CTimer* pTimer) {
|
||||
@@ -57,6 +99,14 @@ bool CModule::AddTimer(CTimer* pTimer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CModule::AddTimer(FPTimer_t pFBCallback, const CString& sLabel, u_int uInterval,
|
||||
u_int uCycles, const CString& sDescription )
|
||||
{
|
||||
CFPTimer *pTimer = new CFPTimer( this, uInterval, uCycles, sLabel, sDescription );
|
||||
pTimer->SetFPCallback( pFBCallback );
|
||||
return( AddTimer( pTimer ) );
|
||||
}
|
||||
|
||||
bool CModule::RemTimer(const CString& sLabel) {
|
||||
for (unsigned int a = 0; a < m_vTimers.size(); a++) {
|
||||
CTimer* pTimer = m_vTimers[a];
|
||||
|
||||
@@ -16,6 +16,7 @@ class CNick;
|
||||
class CChan;
|
||||
class Csock;
|
||||
class CModule;
|
||||
class CFPTimer;
|
||||
template<class T> class TSocketManager;
|
||||
// !Forward Declarations
|
||||
|
||||
@@ -40,6 +41,30 @@ protected:
|
||||
CString m_sDescription;
|
||||
};
|
||||
|
||||
typedef void (*FPTimer_t)(CModule *, CFPTimer *);
|
||||
|
||||
class CFPTimer : public CTimer {
|
||||
public:
|
||||
CFPTimer(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription)
|
||||
: CTimer( pModule, uInterval, uCycles, sLabel, sDescription ) {
|
||||
m_pFBCallback = NULL;
|
||||
}
|
||||
|
||||
virtual ~CFPTimer() {}
|
||||
|
||||
void SetFPCallback( FPTimer_t p ) { m_pFBCallback = p; }
|
||||
|
||||
protected:
|
||||
virtual void RunJob() {
|
||||
if ( m_pFBCallback )
|
||||
m_pFBCallback( m_pModule, this );
|
||||
}
|
||||
|
||||
private:
|
||||
FPTimer_t m_pFBCallback;
|
||||
};
|
||||
|
||||
|
||||
class CModInfo {
|
||||
public:
|
||||
|
||||
@@ -129,11 +154,18 @@ public:
|
||||
|
||||
// Timer stuff
|
||||
bool AddTimer(CTimer* pTimer);
|
||||
bool AddTimer(FPTimer_t pFBCallback, const CString& sLabel, u_int uInterval, u_int uCycles = 0, const CString& sDescription = "");
|
||||
bool RemTimer(const CString& sLabel);
|
||||
bool UnlinkTimer(CTimer* pTimer);
|
||||
CTimer* FindTimer(const CString& sLabel);
|
||||
virtual void ListTimers();
|
||||
// !Timer stuff
|
||||
|
||||
bool LoadRegistry();
|
||||
bool SaveRegistry();
|
||||
bool SetNV( const CString & sName, const CString & sValue, bool bWriteToDisk = true );
|
||||
CString GetNV( const CString & sName );
|
||||
bool DelNV( const CString & sName, bool bWriteToDisk = true );
|
||||
|
||||
protected:
|
||||
vector<CTimer*> m_vTimers;
|
||||
@@ -141,6 +173,7 @@ protected:
|
||||
TSocketManager<Csock>* m_pManager;
|
||||
CUser* m_pUser;
|
||||
CString m_sModName;
|
||||
MCString m_mssRegistry; //!< way to save name/value pairs. Note there is no encryption involved in this
|
||||
};
|
||||
|
||||
class CModules : public vector<CModule*> {
|
||||
|
||||
@@ -257,6 +257,7 @@ int MCString::WriteToDisk( const CString & sPath, mode_t iMode )
|
||||
|
||||
int MCString::ReadFromDisk( const CString & sPath, mode_t iMode )
|
||||
{
|
||||
clear();
|
||||
CFile cFile( sPath );
|
||||
if ( !cFile.Open( O_RDONLY|O_CREAT, iMode ) )
|
||||
return( MCS_EOPEN );
|
||||
|
||||
Reference in New Issue
Block a user