From 0104d3c5dc6f3dfc385a15dcaf3f2208fb092a39 Mon Sep 17 00:00:00 2001 From: psychon Date: Wed, 9 Sep 2009 10:15:36 +0000 Subject: [PATCH] Improve the AltNick handling Thanks to ViciousPotato for reporting that AltNicks were truncated to 9 characters. We assumed the server's maximum nick length to be 9 by default which made the code truncate the AltNick to 9 characters. Now we save the nick we sent last to the IRC server. If the server reports a "nick is already in use" with a nick shorter than we sent it, we assume this that the server truncated our nick to the allowed length and use this length for our retries (appending different characters to the default nick). Not all irc servers truncate the nick this way if it's too long, on those that don't this patch shouldn't cause any behavior change. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1620 726aef4b-f618-498e-8847-2d620e286838 --- IRCSock.cpp | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/IRCSock.cpp b/IRCSock.cpp index 69f456b8..cd18b30d 100644 --- a/IRCSock.cpp +++ b/IRCSock.cpp @@ -816,6 +816,9 @@ void CIRCSock::Connected() { PutIRC("NICK " + sNick); PutIRC("USER " + sIdent + " \"" + sIdent + "\" \"" + sIdent + "\" :" + sRealName); + + // SendAltNick() needs this + m_Nick.SetNick(sNick); } void CIRCSock::Disconnected() { @@ -1002,24 +1005,34 @@ void CIRCSock::ForwardRaw353(const CString& sLine) const { } void CIRCSock::SendAltNick(const CString& sBadNick) { - const unsigned int uMax = GetMaxNickLen(); - const CString& sConfNick = m_pUser->GetNick().Left(uMax); - const CString& sAltNick = m_pUser->GetAltNick().Left(uMax); + const CString& sLastNick = m_Nick.GetNick(); - if (sBadNick.Equals(sConfNick)) { + // We don't know the maximum allowed nick length yet, but we know which + // nick we sent last. If sBadNick is shorter than that, we assume the + // server truncated our nick. + if (sBadNick.length() < sLastNick.length()) + m_uMaxNickLen = sBadNick.length(); + + unsigned int uMax = m_uMaxNickLen; + + const CString& sConfNick = m_pUser->GetNick(); + const CString& sAltNick = m_pUser->GetAltNick(); + CString sNewNick; + + if (sLastNick.Equals(sConfNick)) { if ((!sAltNick.empty()) && (!sConfNick.Equals(sAltNick))) { - PutIRC("NICK " + sAltNick); + sNewNick = sAltNick; } else { - PutIRC("NICK " + sConfNick.Left(uMax -1) + "-"); + sNewNick = sConfNick.Left(uMax - 1) + "-"; } - } else if (sBadNick.Equals(sAltNick)) { - PutIRC("NICK " + sConfNick.Left(uMax -1) + "-"); - } else if (sBadNick.Equals(CString(sConfNick.Left(uMax -1) + "-"))) { - PutIRC("NICK " + sConfNick.Left(uMax -1) + "|"); - } else if (sBadNick.Equals(CString(sConfNick.Left(uMax -1) + "|"))) { - PutIRC("NICK " + sConfNick.Left(uMax -1) + "^"); - } else if (sBadNick.Equals(CString(sConfNick.Left(uMax -1) + "^"))) { - PutIRC("NICK " + sConfNick.Left(uMax -1) + "a"); + } else if (sLastNick.Equals(sAltNick)) { + sNewNick = sConfNick.Left(uMax -1) + "-"; + } else if (sLastNick.Equals(CString(sConfNick.Left(uMax -1) + "-"))) { + sNewNick = sConfNick.Left(uMax -1) + "|"; + } else if (sLastNick.Equals(CString(sConfNick.Left(uMax -1) + "|"))) { + sNewNick = sConfNick.Left(uMax -1) + "^"; + } else if (sLastNick.Equals(CString(sConfNick.Left(uMax -1) + "^"))) { + sNewNick = sConfNick.Left(uMax -1) + "a"; } else { char cLetter = 0; if (sBadNick.empty()) { @@ -1036,9 +1049,10 @@ void CIRCSock::SendAltNick(const CString& sBadNick) { return; } - CString sSend = "NICK " + sConfNick.Left(uMax -1) + ++cLetter; - PutIRC(sSend); + sNewNick = sConfNick.Left(uMax -1) + ++cLetter; } + PutIRC("NICK " + sNewNick); + m_Nick.SetNick(sNewNick); } unsigned char CIRCSock::GetPermFromMode(unsigned char uMode) const {