From 4b1e7143c35d2798097040cf283f463b86135f5a Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Thu, 22 Sep 2011 17:02:52 +0000 Subject: [PATCH] Set default TrimPrefix to ":" Use TrimPrefix instead of if prefix then chomp, saving quite a lot of duplicated code --- Client.cpp | 44 +++++++------------------------------ IRCSock.cpp | 63 +++++++++++++---------------------------------------- ZNCString.h | 4 ++-- 3 files changed, 25 insertions(+), 86 deletions(-) diff --git a/Client.cpp b/Client.cpp index 16a32954..33c8ca6c 100644 --- a/Client.cpp +++ b/Client.cpp @@ -94,10 +94,7 @@ void CClient::ReadLine(const CString& sData) { if (sCommand.Equals("PASS")) { m_bGotPass = true; - CString sAuthLine = sLine.Token(1, true); - if (sAuthLine.Left(1) == ":") { - sAuthLine.LeftChomp(); - } + CString sAuthLine = sLine.Token(1, true).TrimPrefix_n(); // [user[/network]:]password if (sAuthLine.find(":") == CString::npos) { @@ -116,10 +113,7 @@ void CClient::ReadLine(const CString& sData) { AuthUser(); return; // Don't forward this msg. ZNC has already registered us. } else if (sCommand.Equals("NICK")) { - CString sNick = sLine.Token(1); - if (sNick.Left(1) == ":") { - sNick.LeftChomp(); - } + CString sNick = sLine.Token(1).TrimPrefix_n(); m_sNick = sNick; m_bGotNick = true; @@ -213,11 +207,7 @@ void CClient::ReadLine(const CString& sData) { return; // If the server understands it, we already enabled namesx / uhnames } else if (sCommand.Equals("NOTICE")) { CString sTarget = sLine.Token(1); - CString sMsg = sLine.Token(2, true); - - if (sMsg.Left(1) == ":") { - sMsg.LeftChomp(); - } + CString sMsg = sLine.Token(2, true).TrimPrefix_n(); if (sTarget.TrimPrefix(m_pUser->GetStatusPrefix())) { if (!sTarget.Equals("status")) { @@ -271,11 +261,7 @@ void CClient::ReadLine(const CString& sData) { } } else if (sCommand.Equals("PRIVMSG")) { CString sTarget = sLine.Token(1); - CString sMsg = sLine.Token(2, true); - - if (sMsg.Left(1) == ":") { - sMsg.LeftChomp(); - } + CString sMsg = sLine.Token(2, true).TrimPrefix_n(); if (sMsg.WildCmp("\001*\001")) { CString sCTCP = sMsg; @@ -395,13 +381,9 @@ void CClient::ReadLine(const CString& sData) { PutStatusNotice("Detached from [" + sChan + "]"); return; } else if (sCommand.Equals("JOIN")) { - CString sChans = sLine.Token(1); + CString sChans = sLine.Token(1).TrimPrefix_n(); CString sKey = sLine.Token(2); - if (sChans.Left(1) == ":") { - sChans.LeftChomp(); - } - VCString vChans; sChans.Split(",", vChans, false); sChans.clear(); @@ -431,16 +413,8 @@ void CClient::ReadLine(const CString& sData) { sLine += " " + sKey; } } else if (sCommand.Equals("PART")) { - CString sChan = sLine.Token(1); - CString sMessage = sLine.Token(2, true); - - if (sChan.Left(1) == ":") { - // I hate those broken clients, I hate them so much, I really hate them... - sChan.LeftChomp(); - } - if (sMessage.Left(1) == ":") { - sMessage.LeftChomp(); - } + CString sChan = sLine.Token(1).TrimPrefix_n(); + CString sMessage = sLine.Token(2, true).TrimPrefix_n(); NETWORKMODULECALL(OnUserPart(sChan, sMessage), m_pUser, m_pNetwork, this, return); @@ -459,11 +433,9 @@ void CClient::ReadLine(const CString& sData) { } } else if (sCommand.Equals("TOPIC")) { CString sChan = sLine.Token(1); - CString sTopic = sLine.Token(2, true); + CString sTopic = sLine.Token(2, true).TrimPrefix_n(); if (!sTopic.empty()) { - if (sTopic.Left(1) == ":") - sTopic.LeftChomp(); NETWORKMODULECALL(OnUserTopic(sChan, sTopic), m_pUser, m_pNetwork, this, return); sLine = "TOPIC " + sChan + " :" + sTopic; } else { diff --git a/IRCSock.cpp b/IRCSock.cpp index 414c1d2e..e3f458a8 100644 --- a/IRCSock.cpp +++ b/IRCSock.cpp @@ -105,11 +105,7 @@ void CIRCSock::ReadLine(const CString& sData) { } else if (sLine.Equals("ERROR ", false, 6)) { //ERROR :Closing Link: nick[24.24.24.24] (Excess Flood) CString sError(sLine.substr(6)); - - if (sError.Left(1) == ":") { - sError.LeftChomp(); - } - + sError.TrimPrefix(); m_pNetwork->PutStatus("Error from Server [" + sError + "]"); return; } @@ -117,7 +113,7 @@ void CIRCSock::ReadLine(const CString& sData) { CString sCmd = sLine.Token(1); if ((sCmd.length() == 3) && (isdigit(sCmd[0])) && (isdigit(sCmd[1])) && (isdigit(sCmd[2]))) { - CString sServer = sLine.Token(0); sServer.LeftChomp(); + CString sServer = sLine.Token(0).LeftChomp_n(); unsigned int uRaw = sCmd.ToUInt(); CString sNick = sLine.Token(2); CString sRest = sLine.Token(3, true); @@ -166,7 +162,7 @@ void CIRCSock::ReadLine(const CString& sData) { case 10: { // :irc.server.com 010 nick : CString sHost = sRest.Token(0); CString sPort = sRest.Token(1); - CString sInfo = sRest.Token(2, true).TrimPrefix_n(":"); + CString sInfo = sRest.Token(2, true).TrimPrefix_n(); m_pNetwork->PutStatus("Server [" + m_pNetwork->GetCurrentServer()->GetString(false) + "] redirects us to [" + sHost + ":" + sPort + "] with reason [" + sInfo + "]"); m_pNetwork->PutStatus("Perhaps you want to add it as a new server."); @@ -278,11 +274,7 @@ void CIRCSock::ReadLine(const CString& sData) { // If we don't know that channel, some client might have // requested a /names for it and we really should forward this. if (pChan) { - CString sNicks = sRest.Token(2, true); - if (sNicks.Left(1) == ":") { - sNicks.LeftChomp(); - } - + CString sNicks = sRest.Token(2, true).TrimPrefix_n(); pChan->AddNicks(sNicks); } @@ -363,18 +355,14 @@ void CIRCSock::ReadLine(const CString& sData) { } } } else { - CNick Nick(sLine.Token(0).LeftChomp_n()); + CNick Nick(sLine.Token(0).TrimPrefix_n()); sCmd = sLine.Token(1); CString sRest = sLine.Token(2, true); if (sCmd.Equals("NICK")) { - CString sNewNick = sRest; + CString sNewNick = sRest.TrimPrefix_n(); bool bIsVisible = false; - if (sNewNick.Left(1) == ":") { - sNewNick.LeftChomp(); - } - vector vFoundChans; const vector& vChans = m_pNetwork->GetChans(); @@ -403,13 +391,9 @@ void CIRCSock::ReadLine(const CString& sData) { return; } } else if (sCmd.Equals("QUIT")) { - CString sMessage = sRest; + CString sMessage = sRest.TrimPrefix_n(); bool bIsVisible = false; - if (sMessage.Left(1) == ":") { - sMessage.LeftChomp(); - } - // :nick!ident@host.com QUIT :message if (Nick.GetNick().Equals(GetNick())) { @@ -441,11 +425,7 @@ void CIRCSock::ReadLine(const CString& sData) { return; } } else if (sCmd.Equals("JOIN")) { - CString sChan = sRest.Token(0); - if (sChan.Left(1) == ":") { - sChan.LeftChomp(); - } - + CString sChan = sRest.Token(0).TrimPrefix_n(); CChan* pChan; // Todo: use nick compare function @@ -470,11 +450,8 @@ void CIRCSock::ReadLine(const CString& sData) { } } } else if (sCmd.Equals("PART")) { - CString sChan = sRest.Token(0); - if (sChan.Left(1) == ":") { - sChan.LeftChomp(); - } - CString sMsg = sRest.Token(1, true).TrimPrefix_n(":"); + CString sChan = sRest.Token(0).TrimPrefix_n(); + CString sMsg = sRest.Token(1, true).TrimPrefix_n(); CChan* pChan = m_pNetwork->FindChan(sChan); bool bDetached = false; @@ -621,11 +598,7 @@ void CIRCSock::ReadLine(const CString& sData) { } else if (sCmd.Equals("PRIVMSG")) { // :nick!ident@host.com PRIVMSG #chan :Message CString sTarget = sRest.Token(0); - CString sMsg = sRest.Token(1, true); - - if (sMsg.Left(1) == ":") { - sMsg.LeftChomp(); - } + CString sMsg = sRest.Token(1, true).TrimPrefix_n(); if (sMsg.WildCmp("\001*\001")) { sMsg.LeftChomp(); @@ -659,11 +632,7 @@ void CIRCSock::ReadLine(const CString& sData) { } } else if (sCmd.Equals("WALLOPS")) { // :blub!dummy@rox-8DBEFE92 WALLOPS :this is a test - CString sMsg = sRest.Token(0, true); - - if (sMsg.Left(1) == ":") { - sMsg.LeftChomp(); - } + CString sMsg = sRest.Token(0, true).TrimPrefix_n(); if (!m_pNetwork->IsUserAttached()) { m_pNetwork->AddQueryBuffer(":" + Nick.GetNickMask() + " WALLOPS ", ":" + m_pNetwork->GetUser()->AddTimestamp(sMsg), false); @@ -689,9 +658,9 @@ void CIRCSock::ReadLine(const CString& sData) { // of this reply. CString sArgs; if (sRest.Token(2) == "*") { - sArgs = sRest.Token(3, true).TrimPrefix_n(":"); + sArgs = sRest.Token(3, true).TrimPrefix_n(); } else { - sArgs = sRest.Token(2, true).TrimPrefix_n(":"); + sArgs = sRest.Token(2, true).TrimPrefix_n(); } if (sSubCmd == "LS") { @@ -1074,9 +1043,7 @@ void CIRCSock::ForwardRaw353(const CString& sLine) const { } void CIRCSock::ForwardRaw353(const CString& sLine, CClient* pClient) const { - CString sNicks = sLine.Token(5, true); - if (sNicks.Left(1) == ":") - sNicks.LeftChomp(); + CString sNicks = sLine.Token(5, true).TrimPrefix_n(); if ((!m_bNamesx || pClient->HasNamesx()) && (!m_bUHNames || pClient->HasUHNames())) { // Client and server have both the same UHNames and Namesx stuff enabled diff --git a/ZNCString.h b/ZNCString.h index 266a7bf3..936022b1 100644 --- a/ZNCString.h +++ b/ZNCString.h @@ -413,7 +413,7 @@ public: * @param sPrefix The prefix that should be removed. * @return True if this string was modified. */ - bool TrimPrefix(const CString& sPrefix); + bool TrimPrefix(const CString& sPrefix = ":"); /** Trim a given suffix. * @param sSuffix The suffix that should be removed. * @return True if this string was modified. @@ -423,7 +423,7 @@ public: * @param sPrefix The prefix that should be removed. * @return A copy of this string without the prefix. */ - CString TrimPrefix_n(const CString& sPrefix) const; + CString TrimPrefix_n(const CString& sPrefix = ":") const; /** Trim a given suffix. * @param sSuffix The suffix that should be removed. * @return A copy of this string without the prefix.