Rewrite Buffer to store a format string.

Will use this to allow more parameters to be inserted at the time the
buffers are flushed to the client.
This commit is contained in:
Stéphan Kochen
2011-10-16 17:31:43 +02:00
parent 2fdf54d43d
commit 948ae2510c
8 changed files with 70 additions and 75 deletions
+18 -23
View File
@@ -8,19 +8,14 @@
#include <znc/Buffer.h>
CBufLine::CBufLine(const CString& sPre, const CString& sPost, bool bIncNick=true) {
m_sPre = sPre;
m_sPost = sPost;
m_bIncNick = bIncNick;
CBufLine::CBufLine(const CString& sFormat) {
m_sFormat = sFormat;
}
CBufLine::~CBufLine() {}
void CBufLine::GetLine(const CString& sTarget, CString& sRet) const {
if (m_bIncNick)
sRet = m_sPre + sTarget + m_sPost;
else
sRet = m_sPre + m_sPost;
void CBufLine::GetLine(CString& sRet, const MCString& msParams) const {
sRet = CString::NamedFormat(m_sFormat, msParams);
}
CBuffer::CBuffer(unsigned int uLineCount) {
@@ -29,7 +24,7 @@ CBuffer::CBuffer(unsigned int uLineCount) {
CBuffer::~CBuffer() {}
int CBuffer::AddLine(const CString& sPre, const CString& sPost, bool bIncNick) {
int CBuffer::AddLine(const CString& sFormat) {
if (!m_uLineCount) {
return 0;
}
@@ -38,48 +33,48 @@ int CBuffer::AddLine(const CString& sPre, const CString& sPost, bool bIncNick) {
erase(begin());
}
push_back(CBufLine(sPre, sPost, bIncNick));
push_back(CBufLine(sFormat));
return size();
}
int CBuffer::UpdateLine(const CString& sPre, const CString& sPost, bool bIncNick) {
int CBuffer::UpdateLine(const CString& sMatch, const CString& sFormat) {
for (iterator it = begin(); it != end(); ++it) {
if (it->GetPre() == sPre) {
it->SetPost(sPost);
it->SetIncNick(bIncNick);
if (it->GetFormat().compare(0, sMatch.length(), sMatch) == 0) {
it->SetFormat(sFormat);
return size();
}
}
return AddLine(sPre, sPost, bIncNick);
return AddLine(sFormat);
}
int CBuffer::UpdateExactLine(const CString& sPre, const CString& sPost, bool bIncNick) {
int CBuffer::UpdateExactLine(const CString& sFormat) {
for (iterator it = begin(); it != end(); ++it) {
if (it->GetPre() == sPre && it->GetPost() == sPost)
if (it->GetFormat() == sFormat) {
return size();
}
}
return AddLine(sPre, sPost, bIncNick);
return AddLine(sFormat);
}
bool CBuffer::GetLine(const CString& sTarget, CString& sRet, unsigned int uIdx) const {
bool CBuffer::GetLine(unsigned int uIdx, CString& sRet, const MCString& msParams) const {
if (uIdx >= size()) {
return false;
}
(*this)[uIdx].GetLine(sTarget, sRet);
(*this)[uIdx].GetLine(sRet, msParams);
return true;
}
bool CBuffer::GetNextLine(const CString& sTarget, CString& sRet) {
bool CBuffer::GetNextLine(CString& sRet, const MCString& msParams) {
sRet = "";
if (!size()) {
return false;
}
begin()->GetLine(sTarget, sRet);
begin()->GetLine(sRet, msParams);
erase(begin());
return true;
}
+12 -12
View File
@@ -344,13 +344,16 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
m_vClients.push_back(pClient);
unsigned int uIdx;
CString sLine;
MCString msParams;
msParams["target"] = GetIRCNick().GetNick();
if (m_RawBuffer.IsEmpty()) {
pClient->PutClient(":irc.znc.in 001 " + pClient->GetNick() + " :- Welcome to ZNC -");
} else {
unsigned int uIdx = 0;
CString sLine;
while (m_RawBuffer.GetLine(GetIRCNick().GetNick(), sLine, uIdx++)) {
uIdx = 0;
while (m_RawBuffer.GetLine(uIdx++, sLine, msParams)) {
pClient->PutClient(sLine);
}
@@ -359,10 +362,8 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
}
// Send the cached MOTD
unsigned int uIdx = 0;
CString sLine;
while (m_MotdBuffer.GetLine(GetIRCNick().GetNick(), sLine, uIdx++)) {
uIdx = 0;
while (m_MotdBuffer.GetLine(uIdx++, sLine, msParams)) {
pClient->PutClient(sLine);
}
@@ -391,10 +392,9 @@ void CIRCNetwork::ClientConnected(CClient *pClient) {
}
}
CString sBufLine;
while (m_QueryBuffer.GetNextLine(GetIRCNick().GetNick(), sBufLine)) {
NETWORKMODULECALL(OnPrivBufferPlayLine(*pClient, sBufLine), m_pUser, this, NULL, continue);
pClient->PutClient(sBufLine);
while (m_QueryBuffer.GetNextLine(sLine, msParams)) {
NETWORKMODULECALL(OnPrivBufferPlayLine(*pClient, sLine), m_pUser, this, NULL, continue);
pClient->PutClient(sLine);
}
// Tell them why they won't connect
+10 -8
View File
@@ -118,6 +118,7 @@ void CIRCSock::ReadLine(const CString& sData) {
unsigned int uRaw = sCmd.ToUInt();
CString sNick = sLine.Token(2);
CString sRest = sLine.Token(3, true);
CString sTmp;
switch (uRaw) {
case 1: { // :irc.server.com 001 nick :Welcome to the Internet Relay Network nick
@@ -152,13 +153,13 @@ void CIRCSock::ReadLine(const CString& sData) {
IRCSOCKMODULECALL(OnIRCConnected(), NOTHING);
m_pNetwork->ClearRawBuffer();
m_pNetwork->AddRawBuffer(":" + sServer + " " + sCmd + " ", " " + sRest);
m_pNetwork->AddRawBuffer(":" + _NAMEDFMT(sServer) + " " + sCmd + " {target} " + _NAMEDFMT(sRest));
break;
}
case 5:
ParseISupport(sRest);
m_pNetwork->UpdateExactRawBuffer(":" + sServer + " " + sCmd + " ", " " + sRest);
m_pNetwork->UpdateExactRawBuffer(":" + _NAMEDFMT(sServer) + " " + sCmd + " {target} " + _NAMEDFMT(sRest));
break;
case 10: { // :irc.server.com 010 nick <hostname> <port> :<info>
CString sHost = sRest.Token(0);
@@ -180,7 +181,8 @@ void CIRCSock::ReadLine(const CString& sData) {
case 255: // client count
case 265: // local users
case 266: // global users
m_pNetwork->UpdateRawBuffer(":" + sServer + " " + sCmd + " ", " " + sRest);
sTmp = ":" + _NAMEDFMT(sServer) + " " + sCmd;
m_pNetwork->UpdateRawBuffer(sTmp, sTmp + " {target} " + _NAMEDFMT(sRest));
break;
case 305:
m_pNetwork->SetIRCAway(false);
@@ -331,7 +333,7 @@ void CIRCSock::ReadLine(const CString& sData) {
m_pNetwork->ClearMotdBuffer();
case 372: // motd
case 376: // end motd
m_pNetwork->AddMotdBuffer(":" + sServer + " " + sCmd + " ", " " + sRest);
m_pNetwork->AddMotdBuffer(":" + _NAMEDFMT(sServer) + " " + sCmd + " {target} " + _NAMEDFMT(sRest));
break;
case 437:
// :irc.server.net 437 * badnick :Nick/channel is temporarily unavailable
@@ -663,7 +665,7 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sMsg = sRest.Token(0, true).TrimPrefix_n();
if (!m_pNetwork->IsUserOnline()) {
m_pNetwork->AddQueryBuffer(":" + Nick.GetNickMask() + " WALLOPS ", ":" + m_pNetwork->GetUser()->AddTimestamp(sMsg), false);
m_pNetwork->AddQueryBuffer(":" + _NAMEDFMT(Nick.GetNickMask()) + " WALLOPS :" + _NAMEDFMT(m_pNetwork->GetUser()->AddTimestamp(sMsg)));
}
} else if (sCmd.Equals("CAP")) {
// CAPs are supported only before authorization.
@@ -770,7 +772,7 @@ bool CIRCSock::OnPrivCTCP(CNick& Nick, CString& sMessage) {
if (!m_pNetwork->IsUserOnline()) {
// If the user is detached, add to the buffer
m_pNetwork->AddQueryBuffer(":" + Nick.GetNickMask() + " PRIVMSG ", " :\001ACTION " + m_pNetwork->GetUser()->AddTimestamp(sMessage) + "\001");
m_pNetwork->AddQueryBuffer(":" + _NAMEDFMT(Nick.GetNickMask()) + " PRIVMSG {target} :\001ACTION " + _NAMEDFMT(m_pNetwork->GetUser()->AddTimestamp(sMessage)) + "\001");
}
sMessage = "ACTION " + sMessage;
@@ -825,7 +827,7 @@ bool CIRCSock::OnPrivNotice(CNick& Nick, CString& sMessage) {
if (!m_pNetwork->IsUserOnline()) {
// If the user is detached, add to the buffer
m_pNetwork->AddQueryBuffer(":" + Nick.GetNickMask() + " NOTICE ", " :" + m_pNetwork->GetUser()->AddTimestamp(sMessage));
m_pNetwork->AddQueryBuffer(":" + _NAMEDFMT(Nick.GetNickMask()) + " NOTICE {target} :" + _NAMEDFMT(m_pNetwork->GetUser()->AddTimestamp(sMessage)));
}
return false;
@@ -836,7 +838,7 @@ bool CIRCSock::OnPrivMsg(CNick& Nick, CString& sMessage) {
if (!m_pNetwork->IsUserOnline()) {
// If the user is detached, add to the buffer
m_pNetwork->AddQueryBuffer(":" + Nick.GetNickMask() + " PRIVMSG ", " :" + m_pNetwork->GetUser()->AddTimestamp(sMessage));
m_pNetwork->AddQueryBuffer(":" + _NAMEDFMT(Nick.GetNickMask()) + " PRIVMSG {target} :" + _NAMEDFMT(m_pNetwork->GetUser()->AddTimestamp(sMessage)));
}
return false;
+2
View File
@@ -1095,6 +1095,8 @@ bool CString::RightChomp(unsigned int uLen) {
}
//////////////// MCString ////////////////
const MCString MCString::EmptyMap;
MCString::status_t MCString::WriteToDisk(const CString& sPath, mode_t iMode) const {
CFile cFile(sPath);