/*
* Ignore module for ZNC (Docker/Pi Version)
*/
#include <znc/Modules.h>
#include <znc/User.h>
class CIgnoreMod : public CModule {
public:
MODCONSTRUCTOR(CIgnoreMod) {
AddHelpCommand();
AddCommand("Add", static_cast<CModCommand::ModCmdFunc>(&CIgnoreMod::OnAddCommand), "<HostMask>", "Ignore a user");
AddCommand("Del", static_cast<CModCommand::ModCmdFunc>(&CIgnoreMod::OnDelCommand), "<HostMask>", "Unignore a user");
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CIgnoreMod::OnListCommand), "", "List ignored users");
AddCommand("Clear", static_cast<CModCommand::ModCmdFunc>(&CIgnoreMod::OnClearCommand), "", "Clear all ignored users");
}
void OnAddCommand(const CString& sLine) {
CString sHost = sLine.Token(1);
if (sHost.empty()) {
PutModule("Usage: Add <HostMask>");
return;
}
if (m_ssIgnores.find(sHost) != m_ssIgnores.end()) {
PutModule("I am already ignoring [" + sHost + "]");
return;
}
m_ssIgnores.insert(sHost);
PutModule("Added [" + sHost + "] to the ignore list");
SaveRegistry();
}
void OnDelCommand(const CString& sLine) {
CString sHost = sLine.Token(1);
if (sHost.empty()) {
PutModule("Usage: Del <HostMask>");
return;
}
if (m_ssIgnores.erase(sHost)) {
PutModule("Removed [" + sHost + "] from the ignore list");
SaveRegistry();
} else {
PutModule("I am not ignoring [" + sHost + "]");
}
}
void OnListCommand(const CString& sLine) {
if (m_ssIgnores.empty()) {
PutModule("I am not ignoring anyone");
return;
}
CTable Table;
Table.AddColumn("HostMask");
for (const CString& sHost : m_ssIgnores) {
Table.AddRow();
Table.SetCell("HostMask", sHost);
}
PutModule(Table);
}
void OnClearCommand(const CString& sLine) {
m_ssIgnores.clear();
PutModule("Ignore list cleared");
SaveRegistry();
}
EModRet OnPrivMsg(CNick& Nick, CString& sMessage) override {
return IsIgnored(Nick.GetHostMask()) ? HALT : CONTINUE;
}
EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) override {
return IsIgnored(Nick.GetHostMask()) ? HALT : CONTINUE;
}
bool IsIgnored(const CString& sHostMask) {
for (const CString& sIgnore : m_ssIgnores) {
if (sHostMask.WildCmp(sIgnore)) {
return true;
}
}
return false;
}
bool OnLoad(const CString& sArgs, CString& sMessage) override {
for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
m_ssIgnores.insert(it->first);
}
return true;
}
void SaveRegistry() {
ClearNV();
for (const CString& sIgnore : m_ssIgnores) {
SetNV(sIgnore, "");
}
}
private:
std::set<CString> m_ssIgnores;
};
template <> void TModInfo<CIgnoreMod>(CModInfo& Info) {
Info.SetWikiPage("ignore");
}
NETWORKMODULEDEFS(CIgnoreMod, "Ignore users by hostmask")