From 70aafdee8f882e1dac5a0e1a7417e242b1733472 Mon Sep 17 00:00:00 2001 From: prozacx Date: Sun, 17 Sep 2006 22:57:28 +0000 Subject: [PATCH] Moved awaynick functionality into modules/awaynick.cpp module git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@775 726aef4b-f618-498e-8847-2d620e286838 --- IRCSock.cpp | 6 +- Timers.h | 62 -------- User.cpp | 66 ++------ User.h | 20 +-- modules/awaynick.cpp | 150 ++++++++++++++++++ modules/webadmin.cpp | 2 - .../webadmin/skins/default.de/UserPage.tmpl | 9 +- modules/webadmin/skins/default/UserPage.tmpl | 9 +- .../webadmin/skins/graphiX.de/UserPage.tmpl | 10 -- modules/webadmin/skins/graphiX/UserPage.tmpl | 10 -- modules/webadmin/skins/ice.de/UserPage.tmpl | 9 +- modules/webadmin/skins/ice/UserPage.tmpl | 9 +- znc.cpp | 2 +- 13 files changed, 188 insertions(+), 176 deletions(-) create mode 100644 modules/awaynick.cpp diff --git a/IRCSock.cpp b/IRCSock.cpp index 6f4da39d95..880e23aa62 100644 --- a/IRCSock.cpp +++ b/IRCSock.cpp @@ -91,7 +91,6 @@ void CIRCSock::ReadLine(const CString& sData) { PutIRC("WHO " + sNick); SetNick(sNick); - m_pUser->StartAwayNickTimer(); MODULECALL(OnIRCConnected(), m_pUser, NULL, ); @@ -165,7 +164,7 @@ void CIRCSock::ReadLine(const CString& sData) { CString sBadNick = sRest.Token(0); CString sConfNick = m_pUser->GetNick().Left(uMax); - if (sNick == "*" || sNick.CaseCmp(CNick::Concat(sConfNick, m_pUser->GetAwaySuffix(), GetMaxNickLen())) == 0) { + if (sNick == "*") { CString sAltNick = m_pUser->GetAltNick().Left(uMax); if (sBadNick.CaseCmp(sConfNick) == 0) { @@ -597,9 +596,8 @@ void CIRCSock::ReadLine(const CString& sData) { void CIRCSock::KeepNick(bool bForce) { const CString& sConfNick = m_pUser->GetNick(); - CString sAwayNick = CNick::Concat(sConfNick, m_pUser->GetAwaySuffix(), GetMaxNickLen()); - if (m_bAuthed && m_bKeepNick && (!IsOrigNickPending() || bForce) && m_pUser->GetKeepNick() && GetNick().CaseCmp(sConfNick) != 0 && GetNick().CaseCmp(sAwayNick) != 0) { + if (m_bAuthed && m_bKeepNick && (!IsOrigNickPending() || bForce) && m_pUser->GetKeepNick() && GetNick().CaseCmp(sConfNick) != 0) { PutIRC("NICK " + sConfNick); SetOrigNickPending(true); } diff --git a/Timers.h b/Timers.h index 02e65fec38..ae1aa6351c 100644 --- a/Timers.h +++ b/Timers.h @@ -82,66 +82,4 @@ class CJoinTimer : public CCron { CUser* m_pUser; }; -class CBackNickTimer : public CCron { -public: - CBackNickTimer(CUser* pUser) : CCron() { - m_pUser = pUser; - Start(3); - } - virtual ~CBackNickTimer() {} - -private: -protected: - virtual void RunJob() { - if (m_pUser->IsUserAttached() && m_pUser->IsIRCConnected()) { - CIRCSock* pIRCSock = m_pUser->GetIRCSock(); - - if (pIRCSock) { - CString sConfNick = m_pUser->GetNick(); - - if (pIRCSock->GetNick().CaseCmp(CNick::Concat(sConfNick, m_pUser->GetAwaySuffix(), pIRCSock->GetMaxNickLen())) == 0) { - pIRCSock->PutIRC("NICK " + sConfNick); - } - } - } - - m_pUser->DelBackNickTimer(); - CZNC::Get().GetManager().DelCronByAddr(this); - } - - CUser* m_pUser; -}; - -class CAwayNickTimer : public CCron { -public: - - CAwayNickTimer(CUser* pUser) : CCron() { - m_pUser = pUser; - Start(30); - } - virtual ~CAwayNickTimer() {} - -private: -protected: - virtual void RunJob() { - if (!m_pUser->IsUserAttached() && m_pUser->IsIRCConnected()) { - CIRCSock* pIRCSock = m_pUser->GetIRCSock(); - - if (pIRCSock) { - const CString& sSuffix = m_pUser->GetAwaySuffix(); - - if (!sSuffix.empty()) { - CString sAwayNick = CNick::Concat(m_pUser->GetNick(), sSuffix, pIRCSock->GetMaxNickLen()); - pIRCSock->PutIRC("NICK " + sAwayNick); - } - } - } - - m_pUser->DelAwayNickTimer(); - CZNC::Get().GetManager().DelCronByAddr(this); - } - - CUser* m_pUser; -}; - #endif // !_TIMERS_H diff --git a/User.cpp b/User.cpp index 93dc230f7b..94337416fc 100644 --- a/User.cpp +++ b/User.cpp @@ -37,8 +37,6 @@ CUser::CUser(const CString& sUserName) { m_bKeepBuffer = false; m_bAutoCycle = true; m_bBeingDeleted = false; - m_pBackNickTimer = NULL; - m_pAwayNickTimer = NULL; m_pKeepNickTimer = new CKeepNickTimer(this); m_pJoinTimer = new CJoinTimer(this); m_pMiscTimer = new CMiscTimer(this); @@ -64,8 +62,6 @@ CUser::~CUser() { DelModules(); #endif - CZNC::Get().GetManager().DelCronByAddr(m_pBackNickTimer); - CZNC::Get().GetManager().DelCronByAddr(m_pAwayNickTimer); CZNC::Get().GetManager().DelCronByAddr(m_pKeepNickTimer); CZNC::Get().GetManager().DelCronByAddr(m_pJoinTimer); CZNC::Get().GetManager().DelCronByAddr(m_pMiscTimer); @@ -110,6 +106,23 @@ void CUser::IRCDisconnected() { } } +CString CUser::ExpandString(const CString& sStr) const { + CString sRet; + return ExpandString(sStr, sRet); +} + +CString& CUser::ExpandString(const CString& sStr, CString& sRet) const { + sRet = sStr; + sRet.Replace("%user%", GetUserName()); + sRet.Replace("%nick%", GetUserName()); + sRet.Replace("%altnick%", GetAltNick()); + sRet.Replace("%ident%", GetIdent()); + sRet.Replace("%realname%", GetRealName()); + sRet.Replace("%vhost%", GetVHost()); + + return sRet; +} + void CUser::BounceAllClients() { for (unsigned int a = 0; a < m_vClients.size(); a++) { m_vClients[a]->BouncedOff(); @@ -125,7 +138,6 @@ void CUser::UserConnected(CClient* pClient) { PutStatus("Another client authenticated as your user, use the 'ListClients' command to see all clients"); m_vClients.push_back(pClient); - StartBackNickTimer(); if (m_RawBuffer.IsEmpty()) { pClient->PutClient(":irc.znc.com 001 " + pClient->GetNick() + " :- Welcome to ZNC -"); @@ -163,34 +175,6 @@ void CUser::UserConnected(CClient* pClient) { } } -void CUser::StartAwayNickTimer() { - if (!m_pAwayNickTimer) { - m_pAwayNickTimer = new CAwayNickTimer(this); - CZNC::Get().GetManager().AddCron(m_pAwayNickTimer); - } -} - -void CUser::StartBackNickTimer() { - CIRCSock* pIRCSock = GetIRCSock(); - - if (pIRCSock) { - CString sConfNick = GetNick(); - - if (pIRCSock->GetNick().CaseCmp(CNick::Concat(sConfNick, GetAwaySuffix(), pIRCSock->GetMaxNickLen())) == 0) { - m_pBackNickTimer = new CBackNickTimer(this); - CZNC::Get().GetManager().AddCron(m_pBackNickTimer); - } - } -} - -void CUser::DelAwayNickTimer() { - m_pAwayNickTimer = NULL; -} - -void CUser::DelBackNickTimer() { - m_pBackNickTimer = NULL; -} - void CUser::UserDisconnected(CClient* pClient) { for (unsigned int a = 0; a < m_vClients.size(); a++) { if (m_vClients[a] == pClient) { @@ -198,10 +182,6 @@ void CUser::UserDisconnected(CClient* pClient) { break; } } - - if (!IsUserAttached()) { - StartAwayNickTimer(); - } } bool CUser::Clone(const CUser& User, CString& sErrorRet) { @@ -229,7 +209,6 @@ bool CUser::Clone(const CUser& User, CString& sErrorRet) { SetAltNick(User.GetAltNick(false)); SetIdent(User.GetIdent(false)); SetRealName(User.GetRealName()); - SetAwaySuffix(User.GetAwaySuffix()); SetStatusPrefix(User.GetStatusPrefix()); SetVHost(User.GetVHost()); SetQuitMsg(User.GetQuitMsg()); @@ -368,14 +347,6 @@ bool CUser::Clone(const CUser& User, CString& sErrorRet) { SetAdmin(User.IsAdmin()); // !Flags - if (!IsUserAttached()) { - if (GetAwaySuffix().empty()) { - StartBackNickTimer(); - } else { - StartAwayNickTimer(); - } - } - return true; } @@ -506,7 +477,6 @@ bool CUser::WriteConfig(CFile& File) { PrintLine(File, "RealName", GetRealName()); PrintLine(File, "VHost", GetVHost()); PrintLine(File, "QuitMsg", GetQuitMsg()); - PrintLine(File, "AwaySuffix", GetAwaySuffix()); PrintLine(File, "StatusPrefix", GetStatusPrefix()); PrintLine(File, "ChanModes", GetDefaultChanModes()); PrintLine(File, "Buffer", CString(GetBufferCount())); @@ -928,7 +898,6 @@ void CUser::SetUserName(const CString& s) { void CUser::SetNick(const CString& s) { m_sNick = s; } void CUser::SetAltNick(const CString& s) { m_sAltNick = s; } -void CUser::SetAwaySuffix(const CString& s) { m_sAwaySuffix = s; } void CUser::SetIdent(const CString& s) { m_sIdent = s; } void CUser::SetRealName(const CString& s) { m_sRealName = s; } void CUser::SetVHost(const CString& s) { m_sVHost = s; } @@ -980,7 +949,6 @@ const CString& CUser::GetNick(bool bAllowDefault) const { return (bAllowDefault const CString& CUser::GetAltNick(bool bAllowDefault) const { return (bAllowDefault && m_sAltNick.empty()) ? GetCleanUserName() : m_sAltNick; } const CString& CUser::GetIdent(bool bAllowDefault) const { return (bAllowDefault && m_sIdent.empty()) ? GetCleanUserName() : m_sIdent; } const CString& CUser::GetRealName() const { return m_sRealName.empty() ? m_sUserName : m_sRealName; } -const CString& CUser::GetAwaySuffix() const { return m_sAwaySuffix; } const CString& CUser::GetVHost() const { return m_sVHost; } const CString& CUser::GetPass() const { return m_sPass; } bool CUser::IsPassHashed() const { return m_bPassHashed; } diff --git a/User.h b/User.h index 555c985387..ee7649d226 100644 --- a/User.h +++ b/User.h @@ -22,8 +22,6 @@ class CChan; class CServer; class CIRCSock; class CClient; -class CBackNickTimer; -class CAwayNickTimer; class CKeepNickTimer; class CJoinTimer; class CMiscTimer; @@ -92,15 +90,14 @@ class CUser { void IRCConnected(CIRCSock* pIRCSock); void IRCDisconnected(); + CString ExpandString(const CString& sStr) const; + CString& ExpandString(const CString& sStr, CString& sRet) const; + bool SendFile(const CString& sRemoteNick, const CString& sFileName, const CString& sModuleName = ""); bool GetFile(const CString& sRemoteNick, const CString& sRemoteIP, unsigned short uRemotePort, const CString& sFileName, unsigned long uFileSize, const CString& sModuleName = ""); bool ResumeFile(const CString& sRemoteNick, unsigned short uPort, unsigned long uFileSize); CString GetCurNick(); bool Clone(const CUser& User, CString& sErrorRet); - void StartAwayNickTimer(); - void StartBackNickTimer(); - void DelAwayNickTimer(); - void DelBackNickTimer(); void BounceAllClients(); // Setters @@ -109,7 +106,6 @@ class CUser { void SetAltNick(const CString& s); void SetIdent(const CString& s); void SetRealName(const CString& s); - void SetAwaySuffix(const CString& s); void SetVHost(const CString& s); void SetPass(const CString& s, bool bHashed); void SetBounceDCCs(bool b); @@ -140,7 +136,6 @@ class CUser { const CString& GetAltNick(bool bAllowDefault = true) const; const CString& GetIdent(bool bAllowDefault = true) const; const CString& GetRealName() const; - const CString& GetAwaySuffix() const; const CString& GetVHost() const; const CString& GetPass() const; bool IsPassHashed() const; @@ -178,7 +173,6 @@ class CUser { CString m_sCleanUserName; CString m_sNick; CString m_sAltNick; - CString m_sAwaySuffix; CString m_sIdent; CString m_sRealName; CString m_sVHost; @@ -196,9 +190,9 @@ class CUser { CString m_sDLPath; // !Paths - CBuffer m_RawBuffer; - CBuffer m_MotdBuffer; - CBuffer m_QueryBuffer; + CBuffer m_RawBuffer; + CBuffer m_MotdBuffer; + CBuffer m_QueryBuffer; bool m_bIRCConnected; bool m_bMultiClients; bool m_bBounceDCCs; @@ -211,8 +205,6 @@ class CUser { bool m_bAutoCycle; bool m_bBeingDeleted; - CBackNickTimer* m_pBackNickTimer; - CAwayNickTimer* m_pAwayNickTimer; CKeepNickTimer* m_pKeepNickTimer; CJoinTimer* m_pJoinTimer; CMiscTimer* m_pMiscTimer; diff --git a/modules/awaynick.cpp b/modules/awaynick.cpp new file mode 100644 index 0000000000..4a07b7af46 --- /dev/null +++ b/modules/awaynick.cpp @@ -0,0 +1,150 @@ +//! @author prozac@rottenboy.com + +// @todo handle raw 433 (nick in use) +#include "main.h" +#include "User.h" +#include "Nick.h" +#include "Modules.h" +#include "Chan.h" +#include "IRCSock.h" + +class CAwayNickMod; + +class CAwayNickTimer : public CTimer { +public: + CAwayNickTimer(CAwayNickMod& Module); + +private: + virtual void RunJob(); + +private: + CAwayNickMod& m_Module; +}; + +class CBackNickTimer : public CTimer { +public: + CBackNickTimer(CModule& Module) + : CTimer(&Module, 3, 1, "BackNickTimer", "Set your nick back when you reattach"), + m_Module(Module) {} + +private: + virtual void RunJob() { + CUser* pUser = m_Module.GetUser(); + + if (pUser->IsUserAttached() && pUser->IsIRCConnected()) { + CString sConfNick = pUser->GetNick(); + m_Module.PutIRC("NICK " + sConfNick); + } + } + +private: + CModule& m_Module; +}; + +class CAwayNickMod : public CModule { +public: + MODCONSTRUCTOR(CAwayNickMod) {} + + virtual bool OnLoad(const CString& sArgs) { + m_sFormat = sArgs; + + if (m_sFormat.empty()) { + m_sFormat = "zz_%nick%"; + } + + return true; + } + + virtual ~CAwayNickMod() { + } + + void StartAwayNickTimer() { + RemTimer("AwayNickTimer"); + AddTimer(new CAwayNickTimer(*this)); + } + + void StartBackNickTimer() { + CIRCSock* pIRCSock = m_pUser->GetIRCSock(); + + if (pIRCSock) { + CString sConfNick = m_pUser->GetNick(); + + if (pIRCSock->GetNick().CaseCmp(GetAwayNick().Left(pIRCSock->GetNick().length())) == 0) { + RemTimer("BackNickTimer"); + AddTimer(new CBackNickTimer(*this)); + } + } + } + + virtual void OnIRCConnected() { + if (m_pUser && !m_pUser->IsUserAttached()) { + StartAwayNickTimer(); + } + } + + virtual void OnIRCDisconnected() { + RemTimer("AwayNickTimer"); + RemTimer("BackNickTimer"); + } + + virtual void OnUserAttached() { + StartBackNickTimer(); + } + + virtual void OnUserDetached() { + if (!m_pUser->IsUserAttached()) { + StartAwayNickTimer(); + } + } + + virtual void OnModCommand(const CString& sCommand) { + if (strcasecmp(sCommand.c_str(), "TIMERS") == 0) { + ListTimers(); + } else if (sCommand.Token(0).CaseCmp("SET") == 0) { + CString sFormat(sCommand.Token(1)); + + if (!sFormat.empty()) { + m_sFormat = sFormat; + } + + if (m_pUser) { + CString sExpanded = GetAwayNick(); + CString sMsg = "AwayNick is set to [" + m_sFormat + "]"; + + if (m_sFormat != sExpanded) { + sMsg += " (" + sExpanded + ")"; + } + + PutModule(sMsg); + } + } + } + + CString GetAwayNick() { + unsigned int uLen = 9; + CIRCSock* pIRCSock = m_pUser->GetIRCSock(); + + if (pIRCSock) { + pIRCSock->GetMaxNickLen(); + } + + return m_pUser->ExpandString(m_sFormat).Left(uLen); + } + +private: + CString m_sFormat; +}; + +CAwayNickTimer::CAwayNickTimer(CAwayNickMod& Module) + : CTimer(&Module, 30, 1, "AwayNickTimer", "Set your nick while you're detached"), + m_Module(Module) {} + +void CAwayNickTimer::RunJob() { + CUser* pUser = m_Module.GetUser(); + + if (!pUser->IsUserAttached() && pUser->IsIRCConnected()) { + m_Module.PutIRC("NICK " + m_Module.GetAwayNick()); + } +} + +MODULEDEFS(CAwayNickMod, "Change your nick while you are away") diff --git a/modules/webadmin.cpp b/modules/webadmin.cpp index f959abd077..435ac59ab7 100644 --- a/modules/webadmin.cpp +++ b/modules/webadmin.cpp @@ -782,7 +782,6 @@ bool CWebAdminSock::UserPage(CString& sPageRet, CUser* pUser) { m_Template["Username"] = pUser->GetUserName(); m_Template["Nick"] = pUser->GetNick(); m_Template["AltNick"] = pUser->GetAltNick(); - m_Template["AwaySuffix"] = pUser->GetAwaySuffix(); m_Template["StatusPrefix"] = pUser->GetStatusPrefix(); m_Template["Ident"] = pUser->GetIdent(); m_Template["RealName"] = pUser->GetRealName(); @@ -1058,7 +1057,6 @@ CUser* CWebAdminSock::GetNewUser(CString& sPageRet, CUser* pUser) { sArg = GetParam("nick"); if (!sArg.empty()) { pNewUser->SetNick(sArg); } sArg = GetParam("altnick"); if (!sArg.empty()) { pNewUser->SetAltNick(sArg); } - sArg = GetParam("awaysuffix"); if (!sArg.empty()) { pNewUser->SetAwaySuffix(sArg); } sArg = GetParam("statusprefix"); if (!sArg.empty()) { pNewUser->SetStatusPrefix(sArg); } sArg = GetParam("ident"); if (!sArg.empty()) { pNewUser->SetIdent(sArg); } sArg = GetParam("realname"); if (!sArg.empty()) { pNewUser->SetRealName(sArg); } diff --git a/modules/webadmin/skins/default.de/UserPage.tmpl b/modules/webadmin/skins/default.de/UserPage.tmpl index 622bfadb30..bae91d79ca 100644 --- a/modules/webadmin/skins/default.de/UserPage.tmpl +++ b/modules/webadmin/skins/default.de/UserPage.tmpl @@ -51,19 +51,16 @@
Alt. Nickname:
+
-
AwaySuffix:
-
+
Ident:
+
StatusPrefix:
-
-
Ident:
-
-
Realer/Voller Name:
diff --git a/modules/webadmin/skins/default/UserPage.tmpl b/modules/webadmin/skins/default/UserPage.tmpl index 4f41a6dc67..63a25756b3 100644 --- a/modules/webadmin/skins/default/UserPage.tmpl +++ b/modules/webadmin/skins/default/UserPage.tmpl @@ -51,19 +51,16 @@
AltNick:
+
-
AwaySuffix:
-
+
Ident:
+
StatusPrefix:
-
-
Ident:
-
-
RealName:
diff --git a/modules/webadmin/skins/graphiX.de/UserPage.tmpl b/modules/webadmin/skins/graphiX.de/UserPage.tmpl index 05f15c079a..f4e9f22b7f 100644 --- a/modules/webadmin/skins/graphiX.de/UserPage.tmpl +++ b/modules/webadmin/skins/graphiX.de/UserPage.tmpl @@ -85,16 +85,6 @@ - - - Away Suffix: - - - - - - - Status Prefix: diff --git a/modules/webadmin/skins/graphiX/UserPage.tmpl b/modules/webadmin/skins/graphiX/UserPage.tmpl index ac83675c2f..cf5db46b20 100755 --- a/modules/webadmin/skins/graphiX/UserPage.tmpl +++ b/modules/webadmin/skins/graphiX/UserPage.tmpl @@ -85,16 +85,6 @@ - - - Away Suffix: - - - - - - - Status Prefix: diff --git a/modules/webadmin/skins/ice.de/UserPage.tmpl b/modules/webadmin/skins/ice.de/UserPage.tmpl index 0d0f801838..ffa06df8a0 100644 --- a/modules/webadmin/skins/ice.de/UserPage.tmpl +++ b/modules/webadmin/skins/ice.de/UserPage.tmpl @@ -51,19 +51,16 @@
Alt. Nickname:
+
-
AwaySuffix:
-
+
Ident:
+
StatusPrefix:
-
-
Ident:
-
-
Realer/Voller Name:
diff --git a/modules/webadmin/skins/ice/UserPage.tmpl b/modules/webadmin/skins/ice/UserPage.tmpl index 6a4243c845..31f68552d4 100644 --- a/modules/webadmin/skins/ice/UserPage.tmpl +++ b/modules/webadmin/skins/ice/UserPage.tmpl @@ -51,19 +51,16 @@
AltNick:
+
-
AwaySuffix:
-
+
Ident:
+
StatusPrefix:
-
-
Ident:
-
-
RealName:
diff --git a/znc.cpp b/znc.cpp index c403e2905e..0e9f1c9e91 100644 --- a/znc.cpp +++ b/znc.cpp @@ -901,7 +901,7 @@ bool CZNC::ParseConfig(const CString& sConfig) { pUser->SetAltNick(sValue); continue; } else if (sName.CaseCmp("AwaySuffix") == 0) { - pUser->SetAwaySuffix(sValue); + CUtils::PrintMessage("WARNING: AwaySuffix has been depricated, instead try -> LoadModule = awaynick %nick%_" + sValue); continue; } else if (sName.CaseCmp("Pass") == 0) { if (sValue.Right(1) == "-") {