diff options
| author | Adam <Adam@drink-coca-cola.info> | 2010-05-17 20:33:28 -0400 |
|---|---|---|
| committer | Adam <Adam@anope.org> | 2010-06-18 21:03:41 -0400 |
| commit | 17ab4104005cc5098d9152e6c90d54977222dd06 (patch) | |
| tree | d8ffffedcf5537292a4f19faeb5f1e7796969132 /src/core/cs_akick.c | |
| parent | 2a22d9c2fdf6978858055f1143af0ec40e417424 (diff) | |
Removed process_numlist and having to mark everything as "in use" and constantly checking it. Replaced with a better system.
Diffstat (limited to 'src/core/cs_akick.c')
| -rw-r--r-- | src/core/cs_akick.c | 269 |
1 files changed, 139 insertions, 130 deletions
diff --git a/src/core/cs_akick.c b/src/core/cs_akick.c index abd00be6a..42b79c362 100644 --- a/src/core/cs_akick.c +++ b/src/core/cs_akick.c @@ -45,103 +45,125 @@ static void split_usermask(const char *mask, const char **nick, const char **use delete [] mask2; } -int akick_del_callback(User * u, int num, va_list args) +class AkickListCallback : public NumberList { - ChannelInfo *ci = va_arg(args, ChannelInfo *); - int *last = va_arg(args, int *); + protected: + User *u; + ChannelInfo *ci; + bool SentHeader; + public: + AkickListCallback(User *_u, ChannelInfo *_ci, const std::string &numlist) : NumberList(numlist), u(_u), ci(_ci), SentHeader(false) + { + } + + ~AkickListCallback() + { + if (!SentHeader) + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); + } - *last = num; + virtual void HandleNumber(unsigned Number) + { + if (Number > ci->GetAkickCount()) + return; - if (num < 1 || num > ci->GetAkickCount()) - return 0; + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); + } - ci->GetAkick(num - 1)->InUse = false; - return 1; -} + DoList(u, ci, Number - 1, ci->GetAkick(Number - 1)); + } + static void DoList(User *u, ChannelInfo *ci, unsigned index, AutoKick *akick) + { + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_FORMAT, index + 1, + ((akick->HasFlag(AK_ISNICK)) ? akick->nc->display : akick->mask.c_str()), + (!akick->reason.empty() ? akick->reason.c_str() : getstring(u, NO_REASON))); + } +}; -int akick_list(User * u, int index, ChannelInfo * ci, int *sent_header) +class AkickViewCallback : public AkickListCallback { - AutoKick *akick = ci->GetAkick(index); - - if (!akick->InUse) - return 0; - if (!*sent_header) { - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); - *sent_header = 1; + public: + AkickViewCallback(User *_u, ChannelInfo *_ci, const std::string &numlist) : AkickListCallback(u, ci, numlist) + { } - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_FORMAT, index + 1, - ((akick->HasFlag(AK_ISNICK)) ? akick->nc-> - display : akick->mask.c_str()), - (!akick->reason.empty() ? akick-> - reason.c_str() : getstring(u, NO_REASON))); - return 1; -} + void HandleNumber(unsigned Number) + { + if (Number > ci->GetAkickCount()) + return; -int akick_list_callback(User * u, int num, va_list args) -{ - ChannelInfo *ci = va_arg(args, ChannelInfo *); - int *sent_header = va_arg(args, int *); - if (num < 1 || num > ci->GetAkickCount()) - return 0; - return akick_list(u, num - 1, ci, sent_header); -} + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); + } -int akick_view(User * u, int index, ChannelInfo * ci, int *sent_header) -{ - AutoKick *akick = ci->GetAkick(index); - char timebuf[64]; - struct tm tm; - - if (!akick->InUse) - return 0; - if (!*sent_header) { - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); - *sent_header = 1; + DoList(u, ci, Number - 1, ci->GetAkick(Number - 1)); } - if (akick->addtime) { - tm = *localtime(&akick->addtime); - strftime_lang(timebuf, sizeof(timebuf), u, - STRFTIME_SHORT_DATE_FORMAT, &tm); - } else { - snprintf(timebuf, sizeof(timebuf), "%s", getstring(u, UNKNOWN)); + static void DoList(User *u, ChannelInfo *ci, unsigned index, AutoKick *akick) + { + char timebuf[64]; + struct tm tm; + + memset(&timebuf, 0, sizeof(timebuf)); + + if (akick->addtime) + { + tm = *localtime(&akick->addtime); + strftime_lang(timebuf, sizeof(timebuf), u, STRFTIME_SHORT_DATE_FORMAT, &tm); + } + else + snprintf(timebuf, sizeof(timebuf), "%s", getstring(u, UNKNOWN)); + + notice_lang(Config.s_ChanServ, u, (akick->HasFlag(AK_STUCK) ? CHAN_AKICK_VIEW_FORMAT_STUCK : CHAN_AKICK_VIEW_FORMAT), index + 1, + ((akick->HasFlag(AK_ISNICK)) ? akick->nc->display : akick->mask.c_str()), + !akick->creator.empty() ? akick->creator.c_str() : getstring(u, UNKNOWN), timebuf, + (!akick->reason.empty() ? akick->reason.c_str() : getstring(u, NO_REASON))); + + if (akick->last_used) + { + char last_used[64]; + tm = *localtime(&akick->last_used); + strftime_lang(last_used, sizeof(last_used), u, STRFTIME_SHORT_DATE_FORMAT, &tm); + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LAST_USED, last_used); + } } +}; - notice_lang(Config.s_ChanServ, u, (akick->HasFlag(AK_STUCK) ? CHAN_AKICK_VIEW_FORMAT_STUCK : CHAN_AKICK_VIEW_FORMAT), index + 1, - ((akick->HasFlag(AK_ISNICK)) ? akick->nc->display : akick->mask.c_str()), - !akick->creator.empty() ? akick->creator.c_str() : getstring(u, UNKNOWN), timebuf, - (!akick->reason.empty() ? akick->reason.c_str() : getstring(u, NO_REASON))); - if (akick->last_used) +class AkickDelCallback : public NumberList +{ + User *u; + ChannelInfo *ci; + unsigned Deleted; + public: + AkickDelCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list), u(_u), ci(_ci), Deleted(0) { - char last_used[64]; - tm = *localtime(&akick->last_used); - strftime_lang(last_used, sizeof(last_used), u, STRFTIME_SHORT_DATE_FORMAT, &tm); - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LAST_USED, last_used); } - return 1; -} -int akick_view_callback(User * u, int num, va_list args) -{ - ChannelInfo *ci = va_arg(args, ChannelInfo *); - int *sent_header = va_arg(args, int *); - if (num < 1 || num > ci->GetAkickCount()) - return 0; - return akick_view(u, num - 1, ci, sent_header); -} + ~AkickDelCallback() + { + if (!Deleted) + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); + else if (Deleted == 1) + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_DELETED_ONE, ci->name.c_str()); + else + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_DELETED_SEVERAL, Deleted, ci->name.c_str()); + } -int get_access_nc(NickCore *nc, ChannelInfo *ci) -{ - ChanAccess *access; - if (!ci || !nc) - return 0; + void HandleNumber(unsigned Number) + { + if (Number > ci->GetAkickCount()) + return; - if ((access = ci->GetAccess(nc))) - return access->level; - return 0; -} + ++Deleted; + ci->EraseAkick(Number - 1); + } +}; class CommandCSAKick : public Command { @@ -188,7 +210,7 @@ class CommandCSAKick : public Command * or whether the mask matches a user with higher/equal access - Viper */ if ((ci->HasFlag(CI_PEACE)) && nc) { - if ((nc == ci->founder) || (get_access_nc(nc, ci) >= get_access(u, ci))) + if ((nc == ci->founder) || (get_access_level(ci, nc) >= get_access(u, ci))) { notice_lang(Config.s_ChanServ, u, ACCESS_DENIED); return; @@ -221,7 +243,7 @@ class CommandCSAKick : public Command if (na2->HasFlag(NS_FORBIDDEN)) continue; - if (na2->nc && ((na2->nc == ci->founder) || (get_access_nc(na2->nc, ci) >= get_access(u, ci)))) + if (na2->nc && ((na2->nc == ci->founder) || (get_access_level(ci, na2->nc) >= get_access(u, ci)))) { char buf[BUFSIZE]; @@ -238,8 +260,6 @@ class CommandCSAKick : public Command for (unsigned j = 0; j < ci->GetAkickCount(); ++j) { akick = ci->GetAkick(j); - if (!akick->InUse) - continue; if ((akick->HasFlag(AK_ISNICK)) ? akick->nc == nc : akick->mask == mask) { notice_lang(Config.s_ChanServ, u, CHAN_AKICK_ALREADY_EXISTS, (akick->HasFlag(AK_ISNICK)) ? akick->nc->display : akick->mask.c_str(), ci->name.c_str()); @@ -259,6 +279,8 @@ class CommandCSAKick : public Command else akick = ci->AddAkick(u->nick, mask.c_str(), !reason.empty() ? reason.c_str() : ""); + FOREACH_MOD(I_OnAkickAdd, OnAkickAdd(u, ci, akick)); + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_ADDED, mask.c_str(), ci->name.c_str()); this->DoEnforce(u, ci, params); @@ -285,7 +307,7 @@ class CommandCSAKick : public Command { akick = ci->GetAkick(i); - if (!akick->InUse || akick->HasFlag(AK_ISNICK)) + if (akick->HasFlag(AK_ISNICK)) continue; if (akick->mask == mask) break; @@ -325,7 +347,7 @@ class CommandCSAKick : public Command { akick = ci->GetAkick(i); - if (!akick->InUse || akick->HasFlag(AK_ISNICK)) + if (akick->HasFlag(AK_ISNICK)) continue; if (akick->mask == mask) break; @@ -355,25 +377,7 @@ class CommandCSAKick : public Command /* Special case: is it a number/list? Only do search if it isn't. */ if (isdigit(*mask.c_str()) && strspn(mask.c_str(), "1234567890,-") == strlen(mask.c_str())) - { - int last = -1, deleted, count; - - deleted = process_numlist(mask.c_str(), &count, akick_del_callback, u, ci, &last); - - if (!deleted) - { - if (count == 1) - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_SUCH_ENTRY, last, ci->name.c_str()); - else - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); - } - else if (deleted == 1) - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_DELETED_ONE, ci->name.c_str()); - else - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_DELETED_SEVERAL, deleted, ci->name.c_str()); - if (deleted) - ci->CleanAkick(); - } + (new AkickDelCallback(u, ci, mask.c_str()))->Process(); else { NickAlias *na = findnick(mask.c_str()); @@ -383,8 +387,6 @@ class CommandCSAKick : public Command { akick = ci->GetAkick(i); - if (!akick->InUse) - continue; if (((akick->HasFlag(AK_ISNICK)) && akick->nc == nc) || (!(akick->HasFlag(AK_ISNICK)) && akick->mask == mask)) @@ -397,7 +399,7 @@ class CommandCSAKick : public Command return; } - ci->EraseAkick(akick); + ci->EraseAkick(i); notice_lang(Config.s_ChanServ, u, CHAN_AKICK_DELETED, mask.c_str(), ci->name.c_str()); } @@ -405,10 +407,7 @@ class CommandCSAKick : public Command void DoList(User *u, ChannelInfo *ci, const std::vector<ci::string> ¶ms) { - int sent_header = 0; ci::string mask = params.size() > 2 ? params[2] : ""; - unsigned i; - AutoKick *akick; if (!ci->GetAkickCount()) { @@ -417,15 +416,15 @@ class CommandCSAKick : public Command } if (!mask.empty() && isdigit(*mask.c_str()) && strspn(mask.c_str(), "1234567890,-") == strlen(mask.c_str())) - process_numlist(mask.c_str(), NULL, akick_list_callback, u, ci, &sent_header); + (new AkickListCallback(u, ci, mask.c_str()))->Process(); else { - for (i = 0; i < ci->GetAkickCount(); ++i) + bool SentHeader = false; + + for (unsigned i = 0; i < ci->GetAkickCount(); ++i) { - akick = ci->GetAkick(i); + AutoKick *akick = ci->GetAkick(i); - if (!akick->InUse) - continue; if (!mask.empty()) { if (!(akick->HasFlag(AK_ISNICK)) && !Anope::Match(akick->mask.c_str(), mask.c_str(), false)) @@ -433,21 +432,24 @@ class CommandCSAKick : public Command if ((akick->HasFlag(AK_ISNICK)) && !Anope::Match(akick->nc->display, mask.c_str(), false)) continue; } - akick_list(u, i, ci, &sent_header); + + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); + } + + AkickListCallback::DoList(u, ci, i, akick); } + + if (!SentHeader) + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); } - - if (!sent_header) - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); - } void DoView(User *u, ChannelInfo *ci, const std::vector<ci::string> ¶ms) { - int sent_header = 0; ci::string mask = params.size() > 2 ? params[2] : ""; - AutoKick *akick; - unsigned i; if (!ci->GetAkickCount()) { @@ -456,15 +458,15 @@ class CommandCSAKick : public Command } if (!mask.empty() && isdigit(*mask.c_str()) && strspn(mask.c_str(), "1234567890,-") == strlen(mask.c_str())) - process_numlist(mask.c_str(), NULL, akick_view_callback, u, ci, &sent_header); + (new AkickViewCallback(u, ci, mask.c_str()))->Process(); else { - for (i = 0; i < ci->GetAkickCount(); ++i) + bool SentHeader = false; + + for (unsigned i = 0; i < ci->GetAkickCount(); ++i) { - akick = ci->GetAkick(i); + AutoKick *akick = ci->GetAkick(i); - if (!akick->InUse) - continue; if (!mask.empty()) { if (!(akick->HasFlag(AK_ISNICK)) && !Anope::Match(akick->mask.c_str(), mask.c_str(), false)) @@ -472,12 +474,19 @@ class CommandCSAKick : public Command if ((akick->HasFlag(AK_ISNICK)) && !Anope::Match(akick->nc->display, mask.c_str(), false)) continue; } - akick_view(u, i, ci, &sent_header); + + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_LIST_HEADER, ci->name.c_str()); + } + + AkickViewCallback::DoList(u, ci, i, akick); } - } - if (!sent_header) - notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); + if (!SentHeader) + notice_lang(Config.s_ChanServ, u, CHAN_AKICK_NO_MATCH, ci->name.c_str()); + } } void DoEnforce(User *u, ChannelInfo *ci, const std::vector<ci::string> ¶ms) |
