Skip to content

Commit

Permalink
Make CUser::m_sUserName constant
Browse files Browse the repository at this point in the history
Changing the user name for a CUser instance is a really, really bad idea. There
are lots of paths that depend on the user name and only few of them are fixed up
when the user name changes.

This fixes a problem where admin's "CloneUser from to" caused problems with
modules, because all modules where loaded under the old user name and thus they
read/write NV data from the wrong directory in ~/.znc/users.

Thanks to un1matr1x for reporting this.

Signed-off-by: Uli Schlachter <psychon@znc.in>
  • Loading branch information
psychon committed Feb 24, 2011
1 parent 0b8f9ee commit d44e590
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
28 changes: 11 additions & 17 deletions User.cpp
Expand Up @@ -48,10 +48,15 @@ class CUserTimer : public CCron {
CUser* m_pUser; CUser* m_pUser;
}; };


CUser::CUser(const CString& sUserName) { CUser::CUser(const CString& sUserName)
: m_sUserName(sUserName), m_sCleanUserName(MakeCleanUserName(sUserName))
{
// set paths that depend on the user name:
m_sUserPath = CZNC::Get().GetUserPath() + "/" + m_sUserName;
m_sDLPath = m_sUserPath + "/downloads";

m_pIRCSock = NULL; m_pIRCSock = NULL;
m_fTimezoneOffset = 0; m_fTimezoneOffset = 0;
SetUserName(sUserName);
m_sNick = m_sCleanUserName; m_sNick = m_sCleanUserName;
m_sIdent = m_sCleanUserName; m_sIdent = m_sCleanUserName;
m_sRealName = sUserName; m_sRealName = sUserName;
Expand Down Expand Up @@ -343,13 +348,11 @@ bool CUser::Clone(const CUser& User, CString& sErrorRet, bool bCloneChans) {
return false; return false;
} }


// user names can only specified for the constructor, changing it later
// on breaks too much stuff (e.g. lots of paths depend on the user name)
if (GetUserName() != User.GetUserName()) { if (GetUserName() != User.GetUserName()) {
if (CZNC::Get().FindUser(User.GetUserName())) { DEBUG("Ignoring username in CUser::Clone(), old username [" << GetUserName()
sErrorRet = "New username already exists"; << "]; New username [" << User.GetUserName() << "]");
return false;
}

SetUserName(User.GetUserName());
} }


if (!User.GetPass().empty()) { if (!User.GetPass().empty()) {
Expand Down Expand Up @@ -1205,15 +1208,6 @@ CString CUser::MakeCleanUserName(const CString& sUserName) {
} }


// Setters // Setters
void CUser::SetUserName(const CString& s) {
m_sCleanUserName = CUser::MakeCleanUserName(s);
m_sUserName = s;

// set paths that depend on the user name:
m_sUserPath = CZNC::Get().GetUserPath() + "/" + m_sUserName;
m_sDLPath = GetUserPath() + "/downloads";
}

bool CUser::IsChan(const CString& sChan) const { bool CUser::IsChan(const CString& sChan) const {
if (sChan.empty()) if (sChan.empty())
return false; // There is no way this is a chan return false; // There is no way this is a chan
Expand Down
5 changes: 2 additions & 3 deletions User.h
Expand Up @@ -136,7 +136,6 @@ class CUser {
void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; } void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }


// Setters // Setters
void SetUserName(const CString& s);
void SetNick(const CString& s); void SetNick(const CString& s);
void SetAltNick(const CString& s); void SetAltNick(const CString& s);
void SetIdent(const CString& s); void SetIdent(const CString& s);
Expand Down Expand Up @@ -230,8 +229,8 @@ class CUser {
void JoinChans(set<CChan*>& sChans); void JoinChans(set<CChan*>& sChans);


protected: protected:
CString m_sUserName; const CString m_sUserName;
CString m_sCleanUserName; const CString m_sCleanUserName;
CString m_sNick; CString m_sNick;
CString m_sAltNick; CString m_sAltNick;
CString m_sIdent; CString m_sIdent;
Expand Down
3 changes: 1 addition & 2 deletions modules/admin.cpp
Expand Up @@ -563,14 +563,13 @@ class CAdminMod : public CModule {
return; return;
} }


CUser* pNewUser = new CUser(sOldUsername); CUser* pNewUser = new CUser(sNewUsername);
CString sError; CString sError;
if (!pNewUser->Clone(*pOldUser, sError)) { if (!pNewUser->Clone(*pOldUser, sError)) {
delete pNewUser; delete pNewUser;
PutModule("Error: Cloning failed! [" + sError + "]"); PutModule("Error: Cloning failed! [" + sError + "]");
return; return;
} }
pNewUser->SetUserName(sNewUsername);
pNewUser->SetIRCConnectEnabled(false); pNewUser->SetIRCConnectEnabled(false);


if (!CZNC::Get().AddUser(pNewUser, sError)) { if (!CZNC::Get().AddUser(pNewUser, sError)) {
Expand Down

0 comments on commit d44e590

Please sign in to comment.