summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2014-06-26 19:30:28 -0400
committerAdam <Adam@anope.org>2014-06-26 19:30:28 -0400
commitd492923610d9c9146b2a2b63de38deab2cfd4ca7 (patch)
tree5a3663883bd8247e10eeae4f334c4c6a36ccef96
parentee3289029d61da12013e0f5a3e4faf2d07543a0e (diff)
Change NumberList to take std::function's and use lambda closures
instead of having to create full classes in order to process number lists.
-rw-r--r--include/lists.h31
-rw-r--r--modules/commands/bs_badwords.cpp89
-rw-r--r--modules/commands/cs_access.cpp96
-rw-r--r--modules/commands/cs_akick.cpp69
-rw-r--r--modules/commands/cs_xop.cpp81
-rw-r--r--modules/commands/ms_del.cpp38
-rw-r--r--modules/commands/ms_list.cpp21
-rw-r--r--modules/commands/ms_read.cpp37
-rw-r--r--modules/commands/os_akill.cpp93
-rw-r--r--modules/commands/os_session.cpp103
-rw-r--r--modules/commands/os_sxline.cpp95
-rw-r--r--src/misc.cpp45
12 files changed, 263 insertions, 535 deletions
diff --git a/include/lists.h b/include/lists.h
index c40d97e7d..2ca681453 100644
--- a/include/lists.h
+++ b/include/lists.h
@@ -24,39 +24,16 @@
*/
class CoreExport NumberList
{
- private:
- bool is_valid;
-
- std::set<unsigned> numbers;
+ std::function<void(void)> endf;
- bool desc;
public:
/** Processes a numbered list
* @param list The list
- * @param descending True to make HandleNumber get called with numbers in descending order
- */
- NumberList(const Anope::string &list, bool descending);
-
- /** Destructor, does nothing
- */
- virtual ~NumberList();
-
- /** Should be called after the constructors are done running. This calls the callbacks.
- */
- void Process();
-
- /** Called with a number from the list
- * @param number The number
+ * @param descending True to call the number handler callback with the numbers in descending order
*/
- virtual void HandleNumber(unsigned number);
+ NumberList(const Anope::string &list, bool descending, std::function<void(unsigned int)> nf, std::function<void(void)> ef);
- /** Called when there is an error with the numbered list
- * Return false to immediatly stop processing the list and return
- * This is all done before we start calling HandleNumber, so no numbers will have been processed yet
- * @param list The list
- * @return false to stop processing
- */
- virtual bool InvalidRange(const Anope::string &list);
+ ~NumberList();
};
/** This class handles formatting LIST/VIEW replies.
diff --git a/modules/commands/bs_badwords.cpp b/modules/commands/bs_badwords.cpp
index a29209e31..38748724e 100644
--- a/modules/commands/bs_badwords.cpp
+++ b/modules/commands/bs_badwords.cpp
@@ -145,43 +145,6 @@ Serializable* BadWordImpl::Unserialize(Serializable *obj, Serialize::Data &data)
return bw;
}
-class BadwordsDelCallback : public NumberList
-{
- CommandSource &source;
- ChanServ::Channel *ci;
- BadWords *bw;
- Command *c;
- unsigned deleted;
- bool override;
- public:
- BadwordsDelCallback(CommandSource &_source, ChanServ::Channel *_ci, Command *_c, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), c(_c), deleted(0), override(false)
- {
- if (!source.AccessFor(ci).HasPriv("BADWORDS") && source.HasPriv("botserv/administration"))
- this->override = true;
- bw = ci->Require<BadWords>("badwords");
- }
-
- ~BadwordsDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on the bad word list of \002{0}\002."), ci->name);
- else if (deleted == 1)
- source.Reply(_("Deleted \0021\002 entry from bad word list of \002{0}\002."), ci->name);
- else
- source.Reply(_("Deleted \002{0}\002 entries from the bad word list of \002{1}\002."), deleted, ci->name);
- }
-
- void HandleNumber(unsigned Number) override
- {
- if (!bw || !Number || Number > bw->GetBadWordCount())
- return;
-
- Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "DEL " << bw->GetBadWord(Number - 1)->word;
- ++deleted;
- bw->EraseBadWord(Number - 1);
- }
-};
-
class CommandBSBadwords : public Command
{
private:
@@ -202,30 +165,20 @@ class CommandBSBadwords : public Command
if (!word.empty() && word.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class BadwordsListCallback : public NumberList
- {
- ListFormatter &list;
- BadWords *bw;
- public:
- BadwordsListCallback(ListFormatter &_list, BadWords *_bw, const Anope::string &numlist) : NumberList(numlist, false), list(_list), bw(_bw)
+ NumberList(word, false,
+ [&](unsigned int num)
{
- }
-
- void HandleNumber(unsigned Number) override
- {
- if (!Number || Number > bw->GetBadWordCount())
+ if (!num || num > bw->GetBadWordCount())
return;
- const BadWord *b = bw->GetBadWord(Number - 1);
+ const BadWord *b = bw->GetBadWord(num - 1);
ListFormatter::ListEntry entry;
- entry["Number"] = stringify(Number);
+ entry["Number"] = stringify(num);
entry["Word"] = b->word;
entry["Type"] = b->type == BW_SINGLE ? "(SINGLE)" : (b->type == BW_START ? "(START)" : (b->type == BW_END ? "(END)" : ""));
- this->list.AddEntry(entry);
- }
- }
- nl_list(list, bw, word);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ [](){});
}
else
{
@@ -320,11 +273,32 @@ class CommandBSBadwords : public Command
return;
}
+ bool override = !source.AccessFor(ci).HasPriv("BADWORDS");
+
/* Special case: is it a number/list? Only do search if it isn't. */
if (!word.empty() && isdigit(word[0]) && word.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- BadwordsDelCallback list(source, ci, this, word);
- list.Process();
+ unsigned int deleted = 0;
+
+ NumberList(word, true,
+ [&](unsigned int num)
+ {
+ if (!num || num > badwords->GetBadWordCount())
+ return;
+
+ Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "DEL " << badwords->GetBadWord(num - 1)->word;
+ ++deleted;
+ badwords->EraseBadWord(num - 1);
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on the bad word list of \002{0}\002."), ci->name);
+ else if (deleted == 1)
+ source.Reply(_("Deleted \0021\002 entry from bad word list of \002{0}\002."), ci->name);
+ else
+ source.Reply(_("Deleted \002{0}\002 entries from the bad word list of \002{1}\002."), deleted, ci->name);
+ });
}
else
{
@@ -345,7 +319,6 @@ class CommandBSBadwords : public Command
return;
}
- bool override = !source.AccessFor(ci).HasPriv("BADWORDS");
Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "DEL " << badword->word;
source.Reply(_("\002{0}\002 deleted from \002{1}\002 bad words list."), badword->word, ci->name);
diff --git a/modules/commands/cs_access.cpp b/modules/commands/cs_access.cpp
index 5f38dc09a..d154b3f21 100644
--- a/modules/commands/cs_access.cpp
+++ b/modules/commands/cs_access.cpp
@@ -249,70 +249,55 @@ class CommandCSAccess : public Command
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class AccessDelCallback : public NumberList
- {
- CommandSource &source;
- ChanServ::Channel *ci;
- Command *c;
- unsigned deleted;
- Anope::string Nicks;
- bool denied;
- bool override;
- Anope::string mask;
- public:
- AccessDelCallback(CommandSource &_source, ChanServ::Channel *_ci, Command *_c, const Anope::string &numlist) : NumberList(numlist, true), source(_source), ci(_ci), c(_c), deleted(0), denied(false), override(false), mask(numlist)
- {
- if (!source.AccessFor(ci).HasPriv("ACCESS_CHANGE") && source.HasPriv("chanserv/access/modify"))
- this->override = true;
- }
+ bool override = !source.AccessFor(ci).HasPriv("ACCESS_CHANGE") && source.HasPriv("chanserv/access/modify");
+ Anope::string nicks;
+ bool denied = false;
+ unsigned int deleted = 0;
- ~AccessDelCallback()
+ NumberList(mask, true,
+ [&](unsigned int num)
{
- if (denied && !deleted)
- source.Reply(_("Access denied. You do not have enough privileges on \002{0}\002 to remove any access entries matching \002{1}\002."));
- else if (!deleted)
- source.Reply(_("There are no entries matching \002{0}\002 on the access list of \002{1}\002."), mask, ci->name);
- else
- {
- Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << Nicks;
-
- if (deleted == 1)
- source.Reply(_("Deleted \0021\002 entry from the access list of \002{0}\002."), ci->name);
- else
- source.Reply(_("Deleted \002{0}\002 entries from the access list of \002{1}\002."), deleted, ci->name);
- }
- }
-
- void HandleNumber(unsigned Number) override
- {
- if (!Number || Number > ci->GetAccessCount())
+ if (!num || num > ci->GetAccessCount())
return;
- ChanServ::ChanAccess *access = ci->GetAccess(Number - 1);
+ ChanServ::ChanAccess *access = ci->GetAccess(num - 1);
ChanServ::AccessGroup ag = source.AccessFor(ci);
const ChanServ::ChanAccess *u_highest = ag.Highest();
- if ((!u_highest || *u_highest <= *access) && !ag.founder && !this->override && access->GetAccount() != source.nc)
+ if ((!u_highest || *u_highest <= *access) && !ag.founder && !override && access->GetAccount() != source.nc)
{
denied = true;
return;
}
++deleted;
- if (!Nicks.empty())
- Nicks += ", " + access->Mask();
+ if (!nicks.empty())
+ nicks += ", " + access->Mask();
else
- Nicks = access->Mask();
+ nicks = access->Mask();
- ci->EraseAccess(Number - 1);
+ ci->EraseAccess(num - 1);
Event::OnAccessDel(&Event::AccessDel::OnAccessDel, ci, source, access);
delete access;
- }
- }
- delcallback(source, ci, this, mask);
- delcallback.Process();
+ },
+ [&]()
+ {
+ if (denied && !deleted)
+ source.Reply(_("Access denied. You do not have enough privileges on \002{0}\002 to remove any access entries matching \002{1}\002."));
+ else if (!deleted)
+ source.Reply(_("There are no entries matching \002{0}\002 on the access list of \002{1}\002."), mask, ci->name);
+ else
+ {
+ Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << nicks;
+
+ if (deleted == 1)
+ source.Reply(_("Deleted \0021\002 entry from the access list of \002{0}\002."), ci->name);
+ else
+ source.Reply(_("Deleted \002{0}\002 entries from the access list of \002{1}\002."), deleted, ci->name);
+ }
+ });
}
else
{
@@ -356,17 +341,8 @@ class CommandCSAccess : public Command
if (!nick.empty() && nick.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class AccessListCallback : public NumberList
- {
- ListFormatter &list;
- ChanServ::Channel *ci;
-
- public:
- AccessListCallback(ListFormatter &_list, ChanServ::Channel *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci)
- {
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(nick, false,
+ [&](unsigned int number)
{
if (!number || number > ci->GetAccessCount())
return;
@@ -395,11 +371,9 @@ class CommandCSAccess : public Command
entry["Mask"] = access->Mask();
entry["By"] = access->creator;
entry["Last seen"] = timebuf;
- this->list.AddEntry(entry);
- }
- }
- nl_list(list, ci, nick);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ [&](){});
}
else
{
diff --git a/modules/commands/cs_akick.cpp b/modules/commands/cs_akick.cpp
index 9f69810fc..147842af8 100644
--- a/modules/commands/cs_akick.cpp
+++ b/modules/commands/cs_akick.cpp
@@ -248,7 +248,7 @@ class CommandCSAKick : public Command
void DoDel(CommandSource &source, ChanServ::Channel *ci, const std::vector<Anope::string> &params)
{
const Anope::string &mask = params[2];
- unsigned i, end;
+ bool override = !source.AccessFor(ci).HasPriv("AKICK");
if (!ci->GetAkickCount())
{
@@ -259,52 +259,39 @@ class CommandCSAKick : public Command
/* Special case: is it a number/list? Only do search if it isn't. */
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class AkickDelCallback : public NumberList
- {
- CommandSource &source;
- ChanServ::Channel *ci;
- CommandCSAKick *c;
- unsigned deleted;
- ChanServ::AccessGroup ag;
- public:
- AkickDelCallback(CommandSource &_source, ChanServ::Channel *_ci, CommandCSAKick *_c, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), c(_c), deleted(0), ag(source.AccessFor(ci))
- {
- }
+ unsigned int deleted = 0;
- ~AkickDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
- else if (deleted == 1)
- source.Reply(_("Deleted 1 entry from %s autokick list."), ci->name.c_str());
- else
- source.Reply(_("Deleted %d entries from %s autokick list."), deleted, ci->name.c_str());
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(mask, true,
+ [&](unsigned int number)
{
if (!number || number > ci->GetAkickCount())
return;
const AutoKick *ak = ci->GetAkick(number - 1);
- c->akickevents(&Event::Akick::OnAkickDel, source, ci, ak);
+ this->akickevents(&Event::Akick::OnAkickDel, source, ci, ak);
- bool override = !ag.HasPriv("AKICK");
- Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << (ak->nc ? ak->nc->display : ak->mask);
+ Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << (ak->nc ? ak->nc->display : ak->mask);
++deleted;
ci->EraseAkick(number - 1);
- }
- }
- delcallback(source, ci, this, mask);
- delcallback.Process();
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
+ else if (deleted == 1)
+ source.Reply(_("Deleted 1 entry from %s autokick list."), ci->name.c_str());
+ else
+ source.Reply(_("Deleted %d entries from %s autokick list."), deleted, ci->name.c_str());
+ });
}
else
{
const NickServ::Nick *na = NickServ::FindNick(mask);
const NickServ::Account *nc = na ? *na->nc : NULL;
+ unsigned int i, end;
for (i = 0, end = ci->GetAkickCount(); i < end; ++i)
{
const AutoKick *ak = ci->GetAkick(i);
@@ -319,7 +306,6 @@ class CommandCSAKick : public Command
return;
}
- bool override = !source.AccessFor(ci).HasPriv("AKICK");
Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << mask;
this->akickevents(&Event::Akick::OnAkickDel, source, ci, ci->GetAkick(i));
@@ -336,17 +322,8 @@ class CommandCSAKick : public Command
if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class AkickListCallback : public NumberList
- {
- ListFormatter &list;
- ChanServ::Channel *ci;
-
- public:
- AkickListCallback(ListFormatter &_list, ChanServ::Channel *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci)
- {
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(mask, false,
+ [&](unsigned int number)
{
if (!number || number > ci->GetAkickCount())
return;
@@ -373,11 +350,9 @@ class CommandCSAKick : public Command
entry["Created"] = timebuf;
entry["Last used"] = lastused;
entry["Reason"] = ak->reason;
- this->list.AddEntry(entry);
- }
- }
- nl_list(list, ci, mask);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ []{});
}
else
{
diff --git a/modules/commands/cs_xop.cpp b/modules/commands/cs_xop.cpp
index 44af355ec..13c0e7410 100644
--- a/modules/commands/cs_xop.cpp
+++ b/modules/commands/cs_xop.cpp
@@ -281,42 +281,18 @@ class CommandCSXOP : public Command
/* Special case: is it a number/list? Only do search if it isn't. */
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class XOPDelCallback : public NumberList
- {
- CommandSource &source;
- ChanServ::Channel *ci;
- Command *c;
- unsigned deleted;
- Anope::string nicks;
- bool override;
- public:
- XOPDelCallback(CommandSource &_source, ChanServ::Channel *_ci, Command *_c, bool _override, const Anope::string &numlist) : NumberList(numlist, true), source(_source), ci(_ci), c(_c), deleted(0), override(_override)
- {
- }
-
- ~XOPDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on %s %s list."), ci->name.c_str(), source.command.c_str());
- else
- {
- Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << nicks;
+ unsigned int deleted = 0;
+ Anope::string nicks;
- if (deleted == 1)
- source.Reply(_("Deleted one entry from %s %s list."), ci->name.c_str(), source.command.c_str());
- else
- source.Reply(_("Deleted %d entries from %s %s list."), deleted, ci->name.c_str(), source.command.c_str());
- }
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(mask, true,
+ [&](unsigned int number)
{
if (!number || number > ci->GetAccessCount())
return;
ChanServ::ChanAccess *caccess = ci->GetAccess(number - 1);
- if (caccess->provider->name != "access/xop" || this->source.command.upper() != caccess->AccessSerialize())
+ if (caccess->provider->name != "access/xop" || source.command.upper() != caccess->AccessSerialize())
return;
++deleted;
@@ -328,10 +304,21 @@ class CommandCSXOP : public Command
ci->EraseAccess(number - 1);
Event::OnAccessDel(&Event::AccessDel::OnAccessDel, ci, source, caccess);
delete caccess;
- }
- }
- delcallback(source, ci, this, override, mask);
- delcallback.Process();
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on %s %s list."), ci->name.c_str(), source.command.c_str());
+ else
+ {
+ Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << nicks;
+
+ if (deleted == 1)
+ source.Reply(_("Deleted one entry from %s %s list."), ci->name.c_str(), source.command.c_str());
+ else
+ source.Reply(_("Deleted %d entries from %s %s list."), deleted, ci->name.c_str(), source.command.c_str());
+ }
+ });
}
else
{
@@ -384,33 +371,23 @@ class CommandCSXOP : public Command
if (!nick.empty() && nick.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class XOPListCallback : public NumberList
- {
- ListFormatter &list;
- ChanServ::Channel *ci;
- CommandSource &source;
- public:
- XOPListCallback(ListFormatter &_list, ChanServ::Channel *_ci, const Anope::string &numlist, CommandSource &src) : NumberList(numlist, false), list(_list), ci(_ci), source(src)
- {
- }
-
- void HandleNumber(unsigned Number) override
+ NumberList(nick, false,
+ [&](unsigned int number)
{
- if (!Number || Number > ci->GetAccessCount())
+ if (!number || number > ci->GetAccessCount())
return;
- const ChanServ::ChanAccess *a = ci->GetAccess(Number - 1);
+ const ChanServ::ChanAccess *a = ci->GetAccess(number - 1);
- if (a->provider->name != "access/xop" || this->source.command.upper() != a->AccessSerialize())
+ if (a->provider->name != "access/xop" || source.command.upper() != a->AccessSerialize())
return;
ListFormatter::ListEntry entry;
- entry["Number"] = stringify(Number);
+ entry["Number"] = stringify(number);
entry["Mask"] = a->Mask();
- this->list.AddEntry(entry);
- }
- } nl_list(list, ci, nick, source);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ []{});
}
else
{
diff --git a/modules/commands/ms_del.cpp b/modules/commands/ms_del.cpp
index 17722d340..bc612c6c1 100644
--- a/modules/commands/ms_del.cpp
+++ b/modules/commands/ms_del.cpp
@@ -12,29 +12,6 @@
#include "module.h"
#include "modules/memoserv.h"
-class MemoDelCallback : public NumberList
-{
- CommandSource &source;
- ChanServ::Channel *ci;
- MemoServ::MemoInfo *mi;
- public:
- MemoDelCallback(CommandSource &_source, ChanServ::Channel *_ci, MemoServ::MemoInfo *_mi, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), mi(_mi)
- {
- }
-
- void HandleNumber(unsigned number) override
- {
- if (!number || number > mi->memos->size())
- return;
-
- if (MemoServ::Event::OnMemoDel)
- MemoServ::Event::OnMemoDel(&MemoServ::Event::MemoDel::OnMemoDel, ci ? ci->name : source.nc->display, mi, mi->GetMemo(number - 1));
-
- mi->Del(number - 1);
- source.Reply(_("Memo \002{0}\002 has been deleted."), number);
- }
-};
-
class CommandMSDel : public Command
{
public:
@@ -96,8 +73,19 @@ class CommandMSDel : public Command
if (isdigit(numstr[0]))
{
- MemoDelCallback list(source, ci, mi, numstr);
- list.Process();
+ NumberList(numstr, true,
+ [&](unsigned int number)
+ {
+ if (!number || number > mi->memos->size())
+ return;
+
+ if (MemoServ::Event::OnMemoDel)
+ MemoServ::Event::OnMemoDel(&MemoServ::Event::MemoDel::OnMemoDel, ci ? ci->name : source.nc->display, mi, mi->GetMemo(number - 1));
+
+ mi->Del(number - 1);
+ source.Reply(_("Memo \002{0}\002 has been deleted."), number);
+ },
+ [](){});
}
else if (numstr.equals_ci("LAST"))
{
diff --git a/modules/commands/ms_list.cpp b/modules/commands/ms_list.cpp
index 543e132e3..2cdb9de52 100644
--- a/modules/commands/ms_list.cpp
+++ b/modules/commands/ms_list.cpp
@@ -71,17 +71,8 @@ class CommandMSList : public Command
if (!param.empty() && isdigit(param[0]))
{
- class MemoListCallback : public NumberList
- {
- ListFormatter &list;
- CommandSource &source;
- const MemoServ::MemoInfo *mi;
- public:
- MemoListCallback(ListFormatter &_list, CommandSource &_source, const MemoServ::MemoInfo *_mi, const Anope::string &numlist) : NumberList(numlist, false), list(_list), source(_source), mi(_mi)
- {
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(param, false,
+ [&](unsigned int number)
{
if (!number || number > mi->memos->size())
return;
@@ -92,11 +83,9 @@ class CommandMSList : public Command
entry["Number"] = (m->unread ? "* " : " ") + stringify(number);
entry["Sender"] = m->sender;
entry["Date/Time"] = Anope::strftime(m->time, source.GetAccount());
- this->list.AddEntry(entry);
- }
- }
- mlc(list, source, mi, param);
- mlc.Process();
+ list.AddEntry(entry);
+ },
+ []{});
}
else
{
diff --git a/modules/commands/ms_read.cpp b/modules/commands/ms_read.cpp
index d7e5a6ed1..33ba8175a 100644
--- a/modules/commands/ms_read.cpp
+++ b/modules/commands/ms_read.cpp
@@ -45,25 +45,8 @@ static void rsend_notify(CommandSource &source, MemoServ::MemoInfo *mi, MemoServ
m->receipt = false;
}
-class MemoListCallback : public NumberList
+class CommandMSRead : public Command
{
- CommandSource &source;
- MemoServ::MemoInfo *mi;
- const ChanServ::Channel *ci;
-
- public:
- MemoListCallback(CommandSource &_source, MemoServ::MemoInfo *_mi, const ChanServ::Channel *_ci, const Anope::string &numlist) : NumberList(numlist, false), source(_source), mi(_mi), ci(_ci)
- {
- }
-
- void HandleNumber(unsigned number) override
- {
- if (!number || number > mi->memos->size())
- return;
-
- MemoListCallback::DoRead(source, mi, ci, number - 1);
- }
-
static void DoRead(CommandSource &source, MemoServ::MemoInfo *mi, const ChanServ::Channel *ci, unsigned index)
{
MemoServ::Memo *m = mi->GetMemo(index);
@@ -92,10 +75,7 @@ class MemoListCallback : public NumberList
if (m->receipt)
rsend_notify(source, mi, m, ci ? ci->name : source.GetNick());
}
-};
-class CommandMSRead : public Command
-{
public:
CommandMSRead(Module *creator) : Command(creator, "memoserv/read", 1, 2)
{
@@ -156,7 +136,7 @@ class CommandMSRead : public Command
for (i = 0, end = mi->memos->size(); i < end; ++i)
if (mi->GetMemo(i)->unread)
{
- MemoListCallback::DoRead(source, mi, ci, i);
+ DoRead(source, mi, ci, i);
++readcount;
}
if (!readcount)
@@ -170,12 +150,19 @@ class CommandMSRead : public Command
else if (numstr.equals_ci("LAST"))
{
for (i = 0, end = mi->memos->size() - 1; i < end; ++i);
- MemoListCallback::DoRead(source, mi, ci, i);
+ DoRead(source, mi, ci, i);
}
else /* number[s] */
{
- MemoListCallback list(source, mi, ci, numstr);
- list.Process();
+ NumberList(numstr, false,
+ [&](unsigned int number)
+ {
+ if (!number || number > mi->memos->size())
+ return;
+
+ DoRead(source, mi, ci, number - 1);
+ },
+ []{});
}
}
diff --git a/modules/commands/os_akill.cpp b/modules/commands/os_akill.cpp
index 5dad0ece9..25024bbe1 100644
--- a/modules/commands/os_akill.cpp
+++ b/modules/commands/os_akill.cpp
@@ -13,48 +13,6 @@
static ServiceReference<XLineManager> akills("XLineManager", "xlinemanager/sgline");
-class AkillDelCallback : public NumberList
-{
- CommandSource &source;
- unsigned deleted;
- Command *cmd;
- public:
- AkillDelCallback(CommandSource &_source, const Anope::string &numlist, Command *c) : NumberList(numlist, true), source(_source), deleted(0), cmd(c)
- {
- }
-
- ~AkillDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on the akill list."));
- else if (deleted == 1)
- source.Reply(_("Deleted \0021\002 entry from the akill list."));
- else
- source.Reply(_("Deleted \002{0}\002 entries from the akill list."), deleted);
- }
-
- void HandleNumber(unsigned number) override
- {
- if (!number)
- return;
-
- XLine *x = akills->GetEntry(number - 1);
-
- if (!x)
- return;
-
- Log(LOG_ADMIN, source, cmd) << "to remove " << x->mask << " from the list";
-
- ++deleted;
- DoDel(source, x);
- }
-
- static void DoDel(CommandSource &source, XLine *x)
- {
- akills->DelXLine(x);
- }
-};
-
class CommandOSAKill : public Command
{
private:
@@ -217,8 +175,30 @@ class CommandOSAKill : public Command
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- AkillDelCallback list(source, mask, this);
- list.Process();
+ unsigned int deleted = 0;
+
+ NumberList(mask, true,
+ [&](unsigned int number)
+ {
+ XLine *x = akills->GetEntry(number - 1);
+
+ if (!x)
+ return;
+
+ Log(LOG_ADMIN, source, this) << "to remove " << x->mask << " from the list";
+
+ ++deleted;
+ akills->DelXLine(x);
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on the akill list."));
+ else if (deleted == 1)
+ source.Reply(_("Deleted \0021\002 entry from the akill list."));
+ else
+ source.Reply(_("Deleted \002{0}\002 entries from the akill list."), deleted);
+ });
}
else
{
@@ -236,7 +216,7 @@ class CommandOSAKill : public Command
Log(LOG_ADMIN, source, this) << "to remove " << x->mask << " from the list";
source.Reply(_("\002{0}\002 deleted from the akill list."), x->mask);
- AkillDelCallback::DoDel(source, x);
+ akills->DelXLine(x);
}
while ((x = akills->HasEntry(mask)));
@@ -252,20 +232,9 @@ class CommandOSAKill : public Command
if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class ListCallback : public NumberList
- {
- CommandSource &source;
- ListFormatter &list;
- public:
- ListCallback(CommandSource &_source, ListFormatter &_list, const Anope::string &numstr) : NumberList(numstr, false), source(_source), list(_list)
+ NumberList(mask, false,
+ [&](unsigned int number)
{
- }
-
- void HandleNumber(unsigned number) override
- {
- if (!number)
- return;
-
const XLine *x = akills->GetEntry(number - 1);
if (!x)
@@ -278,11 +247,9 @@ class CommandOSAKill : public Command
entry["Created"] = Anope::strftime(x->created, NULL, true);
entry["Expires"] = Anope::Expires(x->expires, source.nc);
entry["Reason"] = x->reason;
- this->list.AddEntry(entry);
- }
- }
- nl_list(source, list, mask);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ [&]{});
}
else
{
diff --git a/modules/commands/os_session.cpp b/modules/commands/os_session.cpp
index 48f9fa033..fecef51d9 100644
--- a/modules/commands/os_session.cpp
+++ b/modules/commands/os_session.cpp
@@ -130,48 +130,6 @@ class MySessionService : public SessionService
}
};
-class ExceptionDelCallback : public NumberList
-{
- protected:
- CommandSource &source;
- unsigned deleted;
- Command *cmd;
- public:
- ExceptionDelCallback(CommandSource &_source, const Anope::string &numlist, Command *c) : NumberList(numlist, true), source(_source), deleted(0), cmd(c)
- {
- }
-
- ~ExceptionDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on session-limit exception list."));
- else if (deleted == 1)
- source.Reply(_("Deleted \0021\002 entry from session-limit exception list."));
- else
- source.Reply(_("Deleted \002{0}\002 entries from session-limit exception list."), deleted);
- }
-
- virtual void HandleNumber(unsigned number) override
- {
- if (!number || number > session_service->GetExceptions().size())
- return;
-
- Log(LOG_ADMIN, source, cmd) << "to remove the session limit exception for " << session_service->GetExceptions()[number - 1]->mask;
-
- ++deleted;
- DoDel(source, number - 1);
- }
-
- static void DoDel(CommandSource &source, unsigned index)
- {
- Exception *e = session_service->GetExceptions()[index];
- (*events)(&Event::Exception::OnExceptionDel, source, e);
-
- session_service->DelException(e);
- delete e;
- }
-};
-
class CommandOSSession : public Command
{
private:
@@ -279,6 +237,15 @@ class CommandOSSession : public Command
class CommandOSException : public Command
{
+ static void DoDel(CommandSource &source, unsigned index)
+ {
+ Exception *e = session_service->GetExceptions()[index];
+ (*events)(&Event::Exception::OnExceptionDel, source, e);
+
+ session_service->DelException(e);
+ delete e;
+ }
+
private:
void DoAdd(CommandSource &source, const std::vector<Anope::string> &params)
{
@@ -390,8 +357,28 @@ class CommandOSException : public Command
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- ExceptionDelCallback list(source, mask, this);
- list.Process();
+ unsigned int deleted = 0;
+
+ NumberList(mask, true,
+ [&](unsigned int number)
+ {
+ if (!number || number > session_service->GetExceptions().size())
+ return;
+
+ Log(LOG_ADMIN, source, this) << "to remove the session limit exception for " << session_service->GetExceptions()[number - 1]->mask;
+
+ ++deleted;
+ DoDel(source, number - 1);
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on session-limit exception list."));
+ else if (deleted == 1)
+ source.Reply(_("Deleted \0021\002 entry from session-limit exception list."));
+ else
+ source.Reply(_("Deleted \002{0}\002 entries from session-limit exception list."), deleted);
+ });
}
else
{
@@ -400,7 +387,7 @@ class CommandOSException : public Command
if (mask.equals_ci(session_service->GetExceptions()[i]->mask))
{
Log(LOG_ADMIN, source, this) << "to remove the session limit exception for " << mask;
- ExceptionDelCallback::DoDel(source, i);
+ DoDel(source, i);
source.Reply(_("\002{0}\002 deleted from session-limit exception list."), mask);
break;
}
@@ -460,35 +447,25 @@ class CommandOSException : public Command
if (!mask.empty() && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class ExceptionListCallback : public NumberList
- {
- CommandSource &source;
- ListFormatter &list;
- public:
- ExceptionListCallback(CommandSource &_source, ListFormatter &_list, const Anope::string &numlist) : NumberList(numlist, false), source(_source), list(_list)
+ NumberList(mask, false,
+ [&](unsigned int number)
{
- }
-
- void HandleNumber(unsigned Number) override
- {
- if (!Number || Number > session_service->GetExceptions().size())
+ if (!number || number > session_service->GetExceptions().size())
return;
- Exception *e = session_service->GetExceptions()[Number - 1];
+ Exception *e = session_service->GetExceptions()[number - 1];
ListFormatter::ListEntry entry;
- entry["Number"] = stringify(Number);
+ entry["Number"] = stringify(number);
entry["Mask"] = e->mask;
entry["By"] = e->who;
entry["Created"] = Anope::strftime(e->time, NULL, true);
entry["Expires"] = Anope::Expires(e->expires, source.GetAccount());
entry["Limit"] = stringify(e->limit);
entry["Reason"] = e->reason;
- this->list.AddEntry(entry);
- }
- }
- nl_list(source, list, mask);
- nl_list.Process();
+ list.AddEntry(entry);
+ },
+ []{});
}
else
{
diff --git a/modules/commands/os_sxline.cpp b/modules/commands/os_sxline.cpp
index e1c637a2e..b14820229 100644
--- a/modules/commands/os_sxline.cpp
+++ b/modules/commands/os_sxline.cpp
@@ -11,49 +11,6 @@
#include "module.h"
-class SXLineDelCallback : public NumberList
-{
- XLineManager *xlm;
- Command *command;
- CommandSource &source;
- unsigned deleted;
- public:
- SXLineDelCallback(XLineManager *x, Command *c, CommandSource &_source, const Anope::string &numlist) : NumberList(numlist, true), xlm(x), command(c), source(_source), deleted(0)
- {
- }
-
- ~SXLineDelCallback()
- {
- if (!deleted)
- source.Reply(_("No matching entries on the {0} list."), source.command);
- else if (deleted == 1)
- source.Reply(_("Deleted \0021\002 entry from the {0} list."), source.command);
- else
- source.Reply(_("Deleted \002{0}\002 entries from the {1} list."), deleted, source.command);
- }
-
- void HandleNumber(unsigned number) override
- {
- if (!number)
- return;
-
- XLine *x = this->xlm->GetEntry(number - 1);
-
- if (!x)
- return;
-
- Log(LOG_ADMIN, source, command) << "to remove " << x->mask << " from the list";
-
- ++deleted;
- DoDel(this->xlm, source, x);
- }
-
- static void DoDel(XLineManager *xlm, CommandSource &source, XLine *x)
- {
- xlm->DelXLine(x);
- }
-};
-
class CommandOSSXLineBase : public Command
{
private:
@@ -80,8 +37,30 @@ class CommandOSSXLineBase : public Command
if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- SXLineDelCallback list(this->xlm(), this, source, mask);
- list.Process();
+ unsigned int deleted = 0;
+
+ NumberList(mask, true,
+ [&](unsigned int number)
+ {
+ XLine *x = this->xlm()->GetEntry(number - 1);
+
+ if (!x)
+ return;
+
+ Log(LOG_ADMIN, source, this) << "to remove " << x->mask << " from the list";
+
+ ++deleted;
+ this->xlm()->DelXLine(x);
+ },
+ [&]()
+ {
+ if (!deleted)
+ source.Reply(_("No matching entries on the {0} list."), source.command);
+ else if (deleted == 1)
+ source.Reply(_("Deleted \0021\002 entry from the {0} list."), source.command);
+ else
+ source.Reply(_("Deleted \002{0}\002 entries from the {1} list."), deleted, source.command);
+ });
}
else
{
@@ -95,7 +74,7 @@ class CommandOSSXLineBase : public Command
Event::OnDelXLine(&Event::DelXLine::OnDelXLine, source, x, this->xlm());
- SXLineDelCallback::DoDel(this->xlm(), source, x);
+ this->xlm()->DelXLine(x);
source.Reply(_("\002{0}\002 deleted from the {1} list."), mask, source.command);
Log(LOG_ADMIN, source, this) << "to remove " << mask << " from the list";
}
@@ -116,22 +95,10 @@ class CommandOSSXLineBase : public Command
if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
{
- class SXLineListCallback : public NumberList
- {
- XLineManager *xlm;
- CommandSource &source;
- ListFormatter &list;
- public:
- SXLineListCallback(XLineManager *x, CommandSource &_source, ListFormatter &_list, const Anope::string &numlist) : NumberList(numlist, false), xlm(x), source(_source), list(_list)
- {
- }
-
- void HandleNumber(unsigned number) override
+ NumberList(mask, false,
+ [&](unsigned int number)
{
- if (!number)
- return;
-
- const XLine *x = this->xlm->GetEntry(number - 1);
+ const XLine *x = this->xlm()->GetEntry(number - 1);
if (!x)
return;
@@ -144,10 +111,8 @@ class CommandOSSXLineBase : public Command
entry["Expires"] = Anope::Expires(x->expires, source.nc);
entry["Reason"] = x->reason;
list.AddEntry(entry);
- }
- }
- sl_list(this->xlm(), source, list, mask);
- sl_list.Process();
+ },
+ []{});
}
else
{
diff --git a/src/misc.cpp b/src/misc.cpp
index b6891d669..c347b76a4 100644
--- a/src/misc.cpp
+++ b/src/misc.cpp
@@ -28,11 +28,13 @@
#include <netdb.h>
#endif
-NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(true), desc(descending)
+NumberList::NumberList(const Anope::string &list, bool descending, std::function<void(unsigned int)> nf, std::function<void(void)> ef) : endf(ef)
{
Anope::string error;
commasepstream sep(list);
Anope::string token;
+ bool is_valid = true;
+ std::set<unsigned> numbers;
sep.GetToken(token);
if (token.empty())
@@ -56,11 +58,8 @@ NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(tr
if (!error.empty())
{
- if (!this->InvalidRange(list))
- {
- is_valid = false;
- return;
- }
+ is_valid = false;
+ return;
}
}
else
@@ -81,44 +80,24 @@ NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(tr
if (!error.empty() || !error2.empty())
{
- if (!this->InvalidRange(list))
- {
- is_valid = false;
- return;
- }
+ is_valid = false;
+ return;
}
}
} while (sep.GetToken(token));
-}
-
-NumberList::~NumberList()
-{
-}
-void NumberList::Process()
-{
if (!is_valid)
return;
- if (this->desc)
- {
- for (std::set<unsigned>::reverse_iterator it = numbers.rbegin(), it_end = numbers.rend(); it != it_end; ++it)
- this->HandleNumber(*it);
- }
+ if (descending)
+ std::for_each(numbers.rbegin(), numbers.rend(), nf);
else
- {
- for (std::set<unsigned>::iterator it = numbers.begin(), it_end = numbers.end(); it != it_end; ++it)
- this->HandleNumber(*it);
- }
+ std::for_each(numbers.begin(), numbers.end(), nf);
}
-void NumberList::HandleNumber(unsigned)
-{
-}
-
-bool NumberList::InvalidRange(const Anope::string &)
+NumberList::~NumberList()
{
- return true;
+ endf();
}
ListFormatter::ListFormatter(NickServ::Account *acc) : nc(acc)