10000 Implemented channel sorting + stable join order by sebageek · Pull Request #238 · znc/znc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implemented channel sorting + stable join order #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/znc/Chan.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ class CChan {
M_Except = 'e'
} EModes;

static const unsigned int m_uDefaultSortOrder;

CChan(const CString& sName, CIRCNetwork* pNetwork, bool bInConfig, CConfig *pConfig = NULL);
~CChan();

bool operator<(const CChan& other) const;

void Reset();
CConfig ToConfig();
void Clone(CChan& chan);
Expand Down Expand Up @@ -107,6 +111,7 @@ class CChan {
void SetDefaultModes(const CString& s) { m_sDefaultModes = s; }
void SetAutoClearChanBuffer(bool b);
void SetDetached(bool b = true) { m_bDetached = b; }
void SetSortOrder(unsigned int s) { m_uSortOrder = s; }
void SetInConfig(bool b) { m_bInConfig = b; }
void SetCreationDate(unsigned long u) { m_ulCreationDate = u; }
void Disable() { m_bDisabled = true; }
Expand All @@ -133,6 +138,7 @@ class CChan {
size_t GetNickCount() const { return m_msNicks.size(); }
bool AutoClearChanBuffer() const { return m_bAutoClearChanBuffer; }
bool IsDetached() const { return m_bDetached; }
unsigned int GetSortOrder() const { return m_uSortOrder; }
bool InConfig() const { return m_bInConfig; }
unsigned long GetCreationDate() const { return m_ulCreationDate; }
bool IsDisabled() const { return m_bDisabled; }
Expand All @@ -157,6 +163,7 @@ class CChan {
CString m_sDefaultModes;
std::map<CString,CNick> m_msNicks; // Todo: make this caseless (irc style)
CBuffer m_Buffer;
unsigned int m_uSortOrder;

bool m_bModeKnown;
std::map<unsigned char, CString> m_musModes;
Expand Down
2 changes: 2 additions & 0 deletions include/znc/IRCNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class CIRCNetwork {
const CString& GetChanPrefixes() const { return m_sChanPrefixes; };
void SetChanPrefixes(const CString& s) { m_sChanPrefixes = s; };
bool IsChan(const CString& sChan) const;
void SortChans();

const std::vector<CServer*>& GetServers() const;
bool HasServers() const { return !m_vServers.empty(); }
Expand Down Expand Up @@ -148,6 +149,7 @@ class CIRCNetwork {
CString& ExpandString(const CString& sStr, CString& sRet) const;
private:
bool JoinChan(CChan* pChan);
static bool CompareChanPtrsLesserThan(CChan* a, CChan* b);

protected:
CString m_sName;
Expand Down
11 changes: 11 additions & 0 deletions src/Chan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CChan::CChan(const CString& sName, CIRCNetwork* pNetwork, bool bInConfig, CConfi
m_bInConfig = bInConfig;
m_Nick.SetNetwork(m_pNetwork);
m_bDetached = false;
m_uSortOrder = m_uDefaultSortOrder;
m_bDisabled = false;
SetBufferCount(m_pNetwork->GetUser()->GetBufferCount(), true);
SetAutoClearChanBuffer(m_pNetwork->GetUser()->AutoClearChanBuffer());
Expand All @@ -52,13 +53,19 @@ CChan::CChan(const CString& sName, CIRCNetwork* pNetwork, bool bInConfig, CConfi
SetKey(sValue);
if (pConfig->FindStringEntry("modes", sValue))
SetDefaultModes(sValue);
if (pConfig->FindStringEntry("sortorder", sValue))
SetSortOrder(sValue.ToUInt());
}
}

CChan::~CChan() {
ClearNicks();
}

bool CChan::operator<(const CChan& other) const {
return GetSortOrder() < other.GetSortOrder();
}

void CChan::Reset() {
m_bIsOn = false;
m_bModeKnown = false;
Expand Down Expand Up @@ -86,6 +93,8 @@ CConfig CChan::ToConfig() {
config.AddKeyValuePair("Key", GetKey());
if (!GetDefaultModes().empty())
config.AddKeyValuePair("Modes", GetDefaultModes());
if (GetSortOrder() != m_uDefaultSortOrder)
config.AddKeyValuePair("SortOrder", CString(GetSortOrder()));

return config;
}
Expand Down Expand Up @@ -581,3 +590,5 @@ void CChan::Enable() {
ResetJoinTries();
m_bDisabled = false;
}

const unsigned int CChan::m_uDefaultSortOrder = 9999;
37 changes: 37 additions & 0 deletions src/ClientCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ void CClient::UserCommand(CString& sLine) {
Table.AddColumn("Conf");
Table.AddColumn("Buf");
Table.AddColumn("Modes");
Table.AddColumn("Order");
Table.AddColumn("Users");

for (unsigned int p = 0; p < sPerms.size(); p++) {
Expand All @@ -441,6 +442,7 @@ void CClient::UserCommand(CString& sLine) {
Table.SetCell("Conf", CString((pChan->InConfig()) ? "yes" : ""));
Table.SetCell("Buf", CString((pChan->AutoClearChanBuffer()) ? "*" : "") + CString(pChan->GetBufferCount()));
Table.SetCell("Modes", pChan->GetModeString());
Table.SetCell("Order", CString(pChan->GetSortOrder()));
Table.SetCell("Users", CString(pChan->GetNickCount()));

map<char, unsigned int> mPerms = pChan->GetPermCounts();
Expand Down Expand Up @@ -1329,6 +1331,36 @@ void CClient::UserCommand(CString& sLine) {
PutStatus("Setting BufferCount failed for [" + CString(uFail) + "] channels, "
"max buffer count is " + CString(CZNC::Get().GetMaxBufferSize()));
}
} else if (sCommand.Equals("SETSORTORDER")) {
if (!m_pNetwork) {
PutStatus("You must be connected with a network to use this command");
return;
}

CString sChan = sLine.Token(1);

if (sChan.empty()) {
PutStatus("Usage: SetSortOrder <#chan> [number]");
return;
}

unsigned int uSortOrder = sLine.Token(2).ToUInt();
if (uSortOrder == 0)
uSortOrder = CChan::m_uDefaultSortOrder;

const vector<CChan*>& vChans = m_pNetwork->GetChans();
vector<CChan*>::const_iterator it;
unsigned int uMatches = 0;
for (it = vChans.begin(); it != vChans.end(); ++it) {
if ((*it)->GetName().WildCmp(sChan)) {
uMatches++;
(*it)->SetSortOrder(uSortOrder);
}
}
m_pNetwork->SortChans();

PutStatus("SortOrder for [" + CString(uMatches) +
"] channels was set to [" + CString(uSortOrder) + "]");
} else if (m_pUser->IsAdmin() && sCommand.Equals("TRAFFIC")) {
CZNC::TrafficStatsPair Users, ZNC, Total;
CZNC::TrafficStatsMap traffic = CZNC::Get().GetTrafficStats(Users, ZNC, Total);
Expand Down Expand Up @@ -1586,6 +1618,11 @@ void CClient::HelpUser() {
Table.SetCell("Arguments", "<#chan> [linecount]");
Table.SetCell("Description", "Set the buffer count for a channel");

Table.AddRow();
Table.SetCell("Command", "SetSortOrder");
Table.SetCell("Arguments", "<#chan> [number]");
Table.SetCell("Description", "Set the channel's sort order number");
57A7
if (m_pUser->IsAdmin()) {
Table.AddRow();
Table.SetCell("Command", "AddBindHost");
Expand Down
15 changes: 15 additions & 0 deletions src/IRCNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <znc/Chan.h>
#include <znc/znc.h>

#include <algorithm>

using std::vector;
using std::set;

Expand Down Expand Up @@ -645,6 +647,8 @@ bool CIRCNetwork::AddChan(CChan* pChan) {
}

m_vChans.push_back(pChan);
SortChans();

return true;
}

Expand All @@ -655,6 +659,8 @@ bool CIRCNetwork::AddChan(const CString& sName, bool bInConfig) {

CChan* pChan = new CChan(sName, this, bInConfig);
m_vChans.push_back(pChan);
SortChans();

return true;
}

Expand Down Expand Up @@ -713,6 +719,10 @@ void CIRCNetwork::JoinChans() {
}
}

bool CIRCNetwork::CompareChanPtrsLesserThan(CChan* a, CChan* b) {
return *a < *b;
}

bool CIRCNetwork::JoinChan(CChan* pChan) {
if (m_pUser->JoinTries() != 0 && pChan->GetJoinTries() >= m_pUser->JoinTries()) {
PutStatus("The channel " + pChan->GetName() + " could not be joined, disabling it.");
Expand All @@ -736,6 +746,11 @@ bool CIRCNetwork::IsChan(const CString& sChan) const {
return GetChanPrefixes().find(sChan[0]) != CString::npos;
}

void CIRCNetwork::SortChans() {
// resort all channels
stable_sort(m_vChans.begin(), m_vChans.end(), CompareChanPtrsLesserThan);
}

// Server list

const vector<CServer*>& CIRCNetwork::GetServers() const { return m_vServers; }
Expand Down
0