From ab34760613214a898ef8614abd10eb3cdb6c50fd Mon Sep 17 00:00:00 2001 From: Cizzle Date: Wed, 15 Mar 2017 19:28:00 +0100 Subject: [PATCH] crypt: Add Get/SetNickPrefix commands (#1382) Hide the internal keyword from ListKeys --- modules/crypt.cpp | 63 ++++++++++++++++++++++++++++----------- test/integration/main.cpp | 6 ++-- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/modules/crypt.cpp b/modules/crypt.cpp index 5145b878..7aa0ffba 100644 --- a/modules/crypt.cpp +++ b/modules/crypt.cpp @@ -183,7 +183,8 @@ class CCryptMod : public CModule { if (it != EndNV()) { size_t sp = sStatusPrefix.size(); size_t np = it->second.size(); - if (sStatusPrefix.CaseCmp(it->second, std::min(sp, np)) != 0) + int min = std::min(sp, np); + if (min == 0 || sStatusPrefix.CaseCmp(it->second, min) != 0) return it->second; } return sStatusPrefix.StartsWith("*") ? "." : "*"; @@ -206,6 +207,12 @@ class CCryptMod : public CModule { AddCommand("KeyX", static_cast( &CCryptMod::OnKeyXCommand), "", "Start a DH1080 key exchange with nick"); + AddCommand("GetNickPrefix", static_cast( + &CCryptMod::OnGetNickPrefixCommand), + "", "Get the nick prefix"); + AddCommand("SetNickPrefix", static_cast( + &CCryptMod::OnSetNickPrefixCommand), + "[Prefix]", "Set the nick prefix, with no argument it's disabled."); } ~CCryptMod() override { @@ -501,29 +508,49 @@ class CCryptMod : public CModule { } } - void OnListKeysCommand(const CString& sCommand) { - if (BeginNV() == EndNV()) { - PutModule("You have no encryption keys set."); - } else { - CTable Table; - Table.AddColumn("Target"); - Table.AddColumn("Key"); + void OnGetNickPrefixCommand(const CString& sCommand) { + CString sPrefix = NickPrefix(); + PutModule("Nick Prefix" + (sPrefix.empty() ? " disabled." : (": " + sPrefix))); + } - for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) { + void OnSetNickPrefixCommand(const CString& sCommand) { + CString sPrefix = sCommand.Token(1); + + if (sPrefix.StartsWith(":")) { + PutModule("You cannot use :, even followed by other symbols, as Nick Prefix."); + } else { + CString sStatusPrefix = GetUser()->GetStatusPrefix(); + size_t sp = sStatusPrefix.size(); + size_t np = sPrefix.size(); + int min = std::min(sp, np); + if (min > 0 && sStatusPrefix.CaseCmp(sPrefix, min) == 0) + PutModule("Overlap with Status Prefix (" + sStatusPrefix + "), this Nick Prefix will not be used!"); + else { + SetNV(NICK_PREFIX_KEY, sPrefix); + if (sPrefix.empty()) + PutModule("Disabling Nick Prefix."); + else + PutModule("Setting Nick Prefix to " + sPrefix); + } + } + } + + void OnListKeysCommand(const CString& sCommand) { + CTable Table; + Table.AddColumn("Target"); + Table.AddColumn("Key"); + + for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) { + if (!it->first.Equals(NICK_PREFIX_KEY)) { Table.AddRow(); Table.SetCell("Target", it->first); Table.SetCell("Key", it->second); } - - MCString::iterator it = FindNV(NICK_PREFIX_KEY); - if (it == EndNV()) { - Table.AddRow(); - Table.SetCell("Target", NICK_PREFIX_KEY); - Table.SetCell("Key", NickPrefix()); - } - - PutModule(Table); } + if (Table.empty()) + PutModule("You have no encryption keys set."); + else + PutModule(Table); } CString MakeIvec() { diff --git a/test/integration/main.cpp b/test/integration/main.cpp index 61d84588..130dd145 100644 --- a/test/integration/main.cpp +++ b/test/integration/main.cpp @@ -2078,13 +2078,13 @@ TEST_F(ZNCTest, ModuleCrypt) { client1.Write("PRIVMSG *crypt :listkeys"); QByteArray key1(""); - client1.ReadUntilAndGet("| nick2 | ", key1); + client1.ReadUntilAndGet("| nick2 | ", key1); Z; client2.Write("PRIVMSG *crypt :listkeys"); QByteArray key2(""); - client2.ReadUntilAndGet("| user | ", key2); + client2.ReadUntilAndGet("| user | ", key2); Z; - ASSERT_EQ(key1.mid(18), key2.mid(18)); + ASSERT_EQ(key1.mid(11), key2.mid(11)); client1.Write("PRIVMSG .nick2 :Hello"); QByteArray secretmsg; ircd1.ReadUntilAndGet("PRIVMSG nick2 :+OK ", secretmsg);