cs_akick.cpp

Go to the documentation of this file.
00001 /* ChanServ core functions
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 /*************************************************************************/
00013 
00014 #include "module.h"
00015 
00016 class CommandCSAKick : public Command
00017 {
00018         void DoAdd(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
00019         {
00020                 Anope::string mask = params[2];
00021                 Anope::string reason = params.size() > 3 ? params[3] : "";
00022                 const NickAlias *na = NickAlias::Find(mask);
00023                 NickCore *nc = NULL;
00024                 const AutoKick *akick;
00025 
00026                 if (reason.length() > Config->CSReasonMax)
00027                         reason = reason.substr(0, Config->CSReasonMax);
00028 
00029                 if (IRCD->IsExtbanValid(mask))
00030                         ; /* If this is an extban don't try to complete the mask */
00031                 else if (IRCD->IsChannelValid(mask))
00032                 {
00033                         /* Also don't try to complete the mask if this is a channel */
00034 
00035                         if (mask.equals_ci(ci->name) && ci->HasExt("PEACE"))
00036                         {
00037                                 source.Reply(ACCESS_DENIED);
00038                                 return;
00039                         }
00040                 }
00041                 else if (!na)
00042                 {
00043                         /* If the mask contains a realname the reason must be prepended with a : */
00044                         if (mask.find('#') != Anope::string::npos)
00045                         {
00046                                 size_t r = reason.find(':');
00047                                 if (r != Anope::string::npos)
00048                                 {
00049                                         mask += " " + reason.substr(0, r);
00050                                         mask.trim();
00051                                         reason = reason.substr(r + 1);
00052                                         reason.trim();
00053                                 }
00054                                 else
00055                                 {
00056                                         mask = mask + " " + reason;
00057                                         reason.clear();
00058                                 }
00059                         }
00060 
00061                         Entry e("", mask);
00062                 
00063                         mask = (e.nick.empty() ? "*" : e.nick) + "!"
00064                                 + (e.user.empty() ? "*" : e.user) + "@"
00065                                 + (e.host.empty() ? "*" : e.host);
00066                         if (!e.real.empty())
00067                                 mask += "#" + e.real;
00068                 }
00069                 else
00070                         nc = na->nc;
00071 
00072                 /* Check excepts BEFORE we get this far */
00073                 if (ci->c)
00074                 {
00075                         std::pair<Channel::ModeList::iterator, Channel::ModeList::iterator> modes = ci->c->GetModeList("EXCEPT");
00076                         for (; modes.first != modes.second; ++modes.first)
00077                         {
00078                                 if (Anope::Match(modes.first->second, mask))
00079                                 {
00080                                         source.Reply(CHAN_EXCEPTED, mask.c_str(), ci->name.c_str());
00081                                         return;
00082                                 }
00083                         }
00084                 }
00085 
00086                 bool override = !source.AccessFor(ci).HasPriv("AKICK");
00087                 /* Opers overriding get to bypass PEACE */
00088                 if (override)
00089                         ;
00090                 /* These peace checks are only for masks */
00091                 else if (IRCD->IsChannelValid(mask))
00092                         ;
00093                 /* Check whether target nick has equal/higher access
00094                 * or whether the mask matches a user with higher/equal access - Viper */
00095                 else if (ci->HasExt("PEACE") && nc)
00096                 {
00097                         AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci);
00098                         if (nc == ci->GetFounder() || nc_access >= u_access)
00099                         {
00100                                 source.Reply(ACCESS_DENIED);
00101                                 return;
00102                         }
00103                 }
00104                 else if (ci->HasExt("PEACE"))
00105                 {
00106                         /* Match against all currently online users with equal or
00107                          * higher access. - Viper */
00108                         for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
00109                         {
00110                                 User *u2 = it->second;
00111 
00112                                 AccessGroup nc_access = ci->AccessFor(nc), u_access = source.AccessFor(ci);
00113                                 Entry entry_mask("", mask);
00114 
00115                                 if ((ci->AccessFor(u2).HasPriv("FOUNDER") || nc_access >= u_access) && entry_mask.Matches(u2))
00116                                 {
00117                                         source.Reply(ACCESS_DENIED);
00118                                         return;
00119                                 }
00120                         }
00121 
00122                         /* Match against the lastusermask of all nickalias's with equal
00123                          * or higher access. - Viper */
00124                         for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ++it)
00125                         {
00126                                 na = it->second;
00127 
00128                                 AccessGroup nc_access = ci->AccessFor(na->nc), u_access = source.AccessFor(ci);
00129                                 if (na->nc && (na->nc == ci->GetFounder() || nc_access >= u_access))
00130                                 {
00131                                         Anope::string buf = na->nick + "!" + na->last_usermask;
00132                                         if (Anope::Match(buf, mask))
00133                                         {
00134                                                 source.Reply(ACCESS_DENIED);
00135                                                 return;
00136                                         }
00137                                 }
00138                          }
00139                 }
00140 
00141                 for (unsigned j = 0, end = ci->GetAkickCount(); j < end; ++j)
00142                 {
00143                         akick = ci->GetAkick(j);
00144                         if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask))
00145                         {
00146                                 source.Reply(_("\002%s\002 already exists on %s autokick list."), akick->nc ? akick->nc->display.c_str() : akick->mask.c_str(), ci->name.c_str());
00147                                 return;
00148                         }
00149                 }
00150 
00151                 if (ci->GetAkickCount() >= Config->CSAutokickMax)
00152                 {
00153                         source.Reply(_("Sorry, you can only have %d autokick masks on a channel."), Config->CSAutokickMax);
00154                         return;
00155                 }
00156 
00157                 if (nc)
00158                         akick = ci->AddAkick(source.GetNick(), nc, reason);
00159                 else
00160                         akick = ci->AddAkick(source.GetNick(), mask, reason);
00161 
00162                 Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to add " << mask << (reason == "" ? "" : ": ") << reason;
00163 
00164                 FOREACH_MOD(I_OnAkickAdd, OnAkickAdd(source, ci, akick));
00165 
00166                 source.Reply(_("\002%s\002 added to %s autokick list."), mask.c_str(), ci->name.c_str());
00167 
00168                 this->DoEnforce(source, ci);
00169         }
00170 
00171         void DoDel(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
00172         {
00173                 const Anope::string &mask = params[2];
00174                 unsigned i, end;
00175 
00176                 if (!ci->GetAkickCount())
00177                 {
00178                         source.Reply(_("%s autokick list is empty."), ci->name.c_str());
00179                         return;
00180                 }
00181 
00182                 /* Special case: is it a number/list?  Only do search if it isn't. */
00183                 if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
00184                 {
00185                         class AkickDelCallback : public NumberList
00186                         {
00187                                 CommandSource &source;
00188                                 ChannelInfo *ci;
00189                                 Command *c;
00190                                 unsigned deleted;
00191                          public:
00192                                 AkickDelCallback(CommandSource &_source, ChannelInfo *_ci, Command *_c, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), c(_c), deleted(0)
00193                                 {
00194                                 }
00195 
00196                                 ~AkickDelCallback()
00197                                 {
00198                                         bool override = !source.AccessFor(ci).HasPriv("AKICK");
00199                                         Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, c, ci) << "to delete " << deleted << (deleted == 1 ? " entry" : " entries");
00200 
00201                                         if (!deleted)
00202                                                 source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
00203                                         else if (deleted == 1)
00204                                                 source.Reply(_("Deleted 1 entry from %s autokick list."), ci->name.c_str());
00205                                         else
00206                                                 source.Reply(_("Deleted %d entries from %s autokick list."), deleted, ci->name.c_str());
00207                                 }
00208 
00209                                 void HandleNumber(unsigned number) anope_override
00210                                 {
00211                                         if (!number || number > ci->GetAkickCount())
00212                                                 return;
00213 
00214                                         FOREACH_MOD(I_OnAkickDel, OnAkickDel(source, ci, ci->GetAkick(number - 1)));
00215 
00216                                         ++deleted;
00217                                         ci->EraseAkick(number - 1);
00218                                 }
00219                         }
00220                         delcallback(source, ci, this, mask);
00221                         delcallback.Process();
00222                 }
00223                 else
00224                 {
00225                         const NickAlias *na = NickAlias::Find(mask);
00226                         const NickCore *nc = na ? *na->nc : NULL;
00227 
00228                         for (i = 0, end = ci->GetAkickCount(); i < end; ++i)
00229                         {
00230                                 const AutoKick *akick = ci->GetAkick(i);
00231 
00232                                 if (akick->nc ? akick->nc == nc : mask.equals_ci(akick->mask))
00233                                         break;
00234                         }
00235 
00236                         if (i == ci->GetAkickCount())
00237                         {
00238                                 source.Reply(_("\002%s\002 not found on %s autokick list."), mask.c_str(), ci->name.c_str());
00239                                 return;
00240                         }
00241 
00242                         bool override = !source.AccessFor(ci).HasPriv("AKICK");
00243                         Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to delete " << mask;
00244 
00245                         FOREACH_MOD(I_OnAkickDel, OnAkickDel(source, ci, ci->GetAkick(i)));
00246 
00247                         ci->EraseAkick(i);
00248 
00249                         source.Reply(_("\002%s\002 deleted from %s autokick list."), mask.c_str(), ci->name.c_str());
00250                 }
00251         }
00252 
00253         void ProcessList(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params, ListFormatter &list)
00254         {
00255                 const Anope::string &mask = params.size() > 2 ? params[2] : "";
00256 
00257                 if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos)
00258                 {
00259                         class AkickListCallback : public NumberList
00260                         {
00261                                 ListFormatter &list;
00262                                 ChannelInfo *ci;
00263 
00264                          public:
00265                                 AkickListCallback(ListFormatter &_list, ChannelInfo *_ci, const Anope::string &numlist) : NumberList(numlist, false), list(_list), ci(_ci)
00266                                 {
00267                                 }
00268 
00269                                 void HandleNumber(unsigned number) anope_override
00270                                 {
00271                                         if (!number || number > ci->GetAkickCount())
00272                                                 return;
00273         
00274                                         const AutoKick *akick = ci->GetAkick(number - 1);
00275 
00276                                         Anope::string timebuf, lastused;
00277                                         if (akick->addtime)
00278                                                 timebuf = Anope::strftime(akick->addtime, NULL, false);
00279                                         else
00280                                                 timebuf = UNKNOWN;
00281                                         if (akick->last_used)
00282                                                 lastused = Anope::strftime(akick->last_used, NULL, false);
00283                                         else
00284                                                 lastused = UNKNOWN;
00285 
00286                                         ListFormatter::ListEntry entry;
00287                                         entry["Number"] = stringify(number);
00288                                         if (akick->nc)
00289                                                 entry["Mask"] = akick->nc->display;
00290                                         else
00291                                                 entry["Mask"] = akick->mask;
00292                                         entry["Creator"] = akick->creator;
00293                                         entry["Created"] = timebuf;
00294                                         entry["Last used"] = lastused;
00295                                         entry["Reason"] = akick->reason;
00296                                         this->list.AddEntry(entry);
00297                                 }
00298                         }
00299                         nl_list(list, ci, mask);
00300                         nl_list.Process();
00301                 }
00302                 else
00303                 {
00304                         for (unsigned i = 0, end = ci->GetAkickCount(); i < end; ++i)
00305                         {
00306                                 const AutoKick *akick = ci->GetAkick(i);
00307 
00308                                 if (!mask.empty())
00309                                 {
00310                                         if (!akick->nc && !Anope::Match(akick->mask, mask))
00311                                                 continue;
00312                                         if (akick->nc && !Anope::Match(akick->nc->display, mask))
00313                                                 continue;
00314                                 }
00315 
00316                                 Anope::string timebuf, lastused;
00317                                 if (akick->addtime)
00318                                         timebuf = Anope::strftime(akick->addtime);
00319                                 else
00320                                         timebuf = UNKNOWN;
00321                                 if (akick->last_used)
00322                                         lastused = Anope::strftime(akick->last_used);
00323                                 else
00324                                         lastused = UNKNOWN;
00325 
00326                                 ListFormatter::ListEntry entry;
00327                                 entry["Number"] = stringify(i + 1);
00328                                 if (akick->nc)
00329                                         entry["Mask"] = akick->nc->display;
00330                                 else
00331                                         entry["Mask"] = akick->mask;
00332                                 entry["Creator"] = akick->creator;
00333                                 entry["Created"] = timebuf;
00334                                 entry["Last used"] = lastused;
00335                                 entry["Reason"] = akick->reason;
00336                                 list.AddEntry(entry);
00337                         }
00338                 }
00339 
00340                 if (list.IsEmpty())
00341                         source.Reply(_("No matching entries on %s autokick list."), ci->name.c_str());
00342                 else
00343                 {
00344                         std::vector<Anope::string> replies;
00345                         list.Process(replies);
00346 
00347                         source.Reply(_("Autokick list for %s:"), ci->name.c_str());
00348         
00349                         for (unsigned i = 0; i < replies.size(); ++i)
00350                                 source.Reply(replies[i]);
00351 
00352                         source.Reply(_("End of autokick list"));
00353                 }
00354         }
00355 
00356         void DoList(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
00357         {
00358                 if (!ci->GetAkickCount())
00359                 {
00360                         source.Reply(_("%s autokick list is empty."), ci->name.c_str());
00361                         return;
00362                 }
00363 
00364                 ListFormatter list;
00365                 list.AddColumn("Number").AddColumn("Mask").AddColumn("Reason");
00366                 this->ProcessList(source, ci, params, list);
00367         }
00368 
00369         void DoView(CommandSource &source, ChannelInfo *ci, const std::vector<Anope::string> &params)
00370         {
00371                 if (!ci->GetAkickCount())
00372                 {
00373                         source.Reply(_("%s autokick list is empty."), ci->name.c_str());
00374                         return;
00375                 }
00376 
00377                 ListFormatter list;
00378                 list.AddColumn("Number").AddColumn("Mask").AddColumn("Creator").AddColumn("Created").AddColumn("Last used").AddColumn("Reason");
00379                 this->ProcessList(source, ci, params, list);
00380         }
00381 
00382         void DoEnforce(CommandSource &source, ChannelInfo *ci)
00383         {
00384                 Channel *c = ci->c;
00385                 int count = 0;
00386 
00387                 if (!c)
00388                 {
00389                         source.Reply(CHAN_X_NOT_IN_USE, ci->name.c_str());
00390                         return;
00391                 }
00392 
00393                 for (User::ChanUserList::iterator it = c->users.begin(), it_end = c->users.end(); it != it_end; )
00394                 {
00395                         ChanUserContainer *uc = *it++;
00396 
00397                         if (ci->CheckKick(uc->user))
00398                                 ++count;
00399                 }
00400 
00401                 bool override = !source.AccessFor(ci).HasPriv("AKICK");
00402                 Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "ENFORCE, affects " << count << " users";
00403 
00404                 source.Reply(_("AKICK ENFORCE for \002%s\002 complete; \002%d\002 users were affected."), ci->name.c_str(), count);
00405         }
00406 
00407         void DoClear(CommandSource &source, ChannelInfo *ci)
00408         {
00409                 bool override = !source.AccessFor(ci).HasPriv("AKICK");
00410                 Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to clear the akick list";
00411 
00412                 ci->ClearAkick();
00413                 source.Reply(_("Channel %s akick list has been cleared."), ci->name.c_str());
00414         }
00415 
00416  public:
00417         CommandCSAKick(Module *creator) : Command(creator, "chanserv/akick", 2, 4)
00418         {
00419                 this->SetDesc(_("Maintain the AutoKick list"));
00420                 this->SetSyntax(_("\037channel\037 ADD {\037nick\037 | \037mask\037} [\037reason\037]"));
00421                 this->SetSyntax(_("\037channel\037 DEL {\037nick\037 | \037mask\037 | \037entry-num\037 | \037list\037}"));
00422                 this->SetSyntax(_("\037channel\037 LIST [\037mask\037 | \037entry-num\037 | \037list\037]"));
00423                 this->SetSyntax(_("\037channel\037 VIEW [\037mask\037 | \037entry-num\037 | \037list\037]"));
00424                 this->SetSyntax(_("\037channel\037 ENFORCE"));
00425                 this->SetSyntax(_("\037channel\037 CLEAR"));
00426         }
00427 
00428         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00429         {
00430                 Anope::string chan = params[0];
00431                 Anope::string cmd = params[1];
00432                 Anope::string mask = params.size() > 2 ? params[2] : "";
00433 
00434                 ChannelInfo *ci = ChannelInfo::Find(params[0]);
00435                 if (ci == NULL)
00436                 {
00437                         source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
00438                         return;
00439                 }
00440 
00441                 if (mask.empty() && (cmd.equals_ci("ADD") || cmd.equals_ci("DEL")))
00442                         this->OnSyntaxError(source, cmd);
00443                 else if (!source.AccessFor(ci).HasPriv("AKICK") && !source.HasPriv("chanserv/access/modify"))
00444                         source.Reply(ACCESS_DENIED);
00445                 else if (!cmd.equals_ci("LIST") && !cmd.equals_ci("VIEW") && !cmd.equals_ci("ENFORCE") && Anope::ReadOnly)
00446                         source.Reply(_("Sorry, channel autokick list modification is temporarily disabled."));
00447                 else if (cmd.equals_ci("ADD"))
00448                         this->DoAdd(source, ci, params);
00449                 else if (cmd.equals_ci("DEL"))
00450                         this->DoDel(source, ci, params);
00451                 else if (cmd.equals_ci("LIST"))
00452                         this->DoList(source, ci, params);
00453                 else if (cmd.equals_ci("VIEW"))
00454                         this->DoView(source, ci, params);
00455                 else if (cmd.equals_ci("ENFORCE"))
00456                         this->DoEnforce(source, ci);
00457                 else if (cmd.equals_ci("CLEAR"))
00458                         this->DoClear(source, ci);
00459                 else
00460                         this->OnSyntaxError(source, "");
00461 
00462                 return;
00463         }
00464 
00465         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00466         {
00467                 this->SendSyntax(source);
00468                 source.Reply(" ");
00469                 source.Reply(_("Maintains the \002AutoKick list\002 for a channel.  If a user\n"
00470                                 "on the AutoKick list attempts to join the channel,\n"
00471                                 "%s will ban that user from the channel, then kick\n"
00472                                 "the user.\n"
00473                                 " \n"
00474                                 "The \002AKICK ADD\002 command adds the given nick or usermask\n"
00475                                 "to the AutoKick list.  If a \037reason\037 is given with\n"
00476                                 "the command, that reason will be used when the user is\n"
00477                                 "kicked; if not, the default reason is \"You have been\n"
00478                                 "banned from the channel\".\n"
00479                                 "When akicking a \037registered nick\037 the nickserv account\n"
00480                                 "will be added to the akick list instead of the mask.\n"
00481                                 "All users within that nickgroup will then be akicked.\n"),
00482                                 source.service->nick.c_str());
00483                 source.Reply(_(
00484                                 " \n"
00485                                 "The \002AKICK DEL\002 command removes the given nick or mask\n"
00486                                 "from the AutoKick list.  It does not, however, remove any\n"
00487                                 "bans placed by an AutoKick; those must be removed\n"
00488                                 "manually.\n"
00489                                 " \n"
00490                                 "The \002AKICK LIST\002 command displays the AutoKick list, or\n"
00491                                 "optionally only those AutoKick entries which match the\n"
00492                                 "given mask.\n"
00493                                 " \n"
00494                                 "The \002AKICK VIEW\002 command is a more verbose version of the\n"
00495                                 "\002AKICK LIST\002 command.\n"
00496                                 " \n"
00497                                 "The \002AKICK ENFORCE\002 command causes %s to enforce the\n"
00498                                 "current AKICK list by removing those users who match an\n"
00499                                 "AKICK mask.\n"
00500                                 " \n"
00501                                 "The \002AKICK CLEAR\002 command clears all entries of the\n"
00502                                 "akick list."), source.service->nick.c_str());
00503                 return true;
00504         }
00505 };
00506 
00507 class CSAKick : public Module
00508 {
00509         CommandCSAKick commandcsakick;
00510 
00511  public:
00512         CSAKick(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00513                 commandcsakick(this)
00514         {
00515                 this->SetAuthor("Anope");
00516 
00517                 Implementation i[] = { I_OnCheckKick };
00518                 ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
00519         }
00520 
00521         EventReturn OnCheckKick(User *u, ChannelInfo *ci, Anope::string &mask, Anope::string &reason) anope_override
00522         {
00523                 if (ci->c->MatchesList(u, "EXCEPT"))
00524                         return EVENT_CONTINUE;
00525 
00526                 for (unsigned j = 0, end = ci->GetAkickCount(); j < end; ++j)
00527                 {
00528                         AutoKick *autokick = ci->GetAkick(j);
00529                         bool kick = false;
00530 
00531                         if (autokick->nc)
00532                                 kick = autokick->nc == u->Account();
00533                         else if (IRCD->IsChannelValid(autokick->mask))
00534                         {
00535                                 Channel *c = Channel::Find(autokick->mask);
00536                                 kick = c != NULL && c->FindUser(u);
00537                         }
00538                         else
00539                                 kick = Entry("BAN", autokick->mask).Matches(u);
00540 
00541                         if (kick)
00542                         {
00543                                 Log(LOG_DEBUG_2) << u->nick << " matched akick " << (autokick->nc ? autokick->nc->display : autokick->mask);
00544                                 autokick->last_used = Anope::CurTime;
00545                                 if (!autokick->nc && autokick->mask.find('#') == Anope::string::npos)
00546                                         mask = autokick->mask;
00547                                 reason = autokick->reason.empty() ? Config->CSAutokickReason : autokick->reason;
00548                                 return EVENT_STOP;
00549                         }
00550                 }
00551 
00552                 return EVENT_CONTINUE;
00553         }
00554 };
00555 
00556 MODULE_INIT(CSAKick)