diff options
Diffstat (limited to 'src/channels.cpp')
-rw-r--r-- | src/channels.cpp | 206 |
1 files changed, 95 insertions, 111 deletions
diff --git a/src/channels.cpp b/src/channels.cpp index 3ea8da375..48e27f323 100644 --- a/src/channels.cpp +++ b/src/channels.cpp @@ -1,65 +1,63 @@ -/* Channel-handling routines. +/* + * Anope IRC Services * - * (C) 2003-2017 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2003-2017 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ #include "services.h" #include "channels.h" -#include "regchannel.h" #include "logger.h" #include "modules.h" #include "users.h" -#include "bots.h" #include "servers.h" #include "protocol.h" #include "users.h" #include "config.h" -#include "access.h" #include "sockets.h" #include "language.h" #include "uplink.h" +#include "event.h" +#include "modules/chanserv.h" channel_map ChannelList; std::vector<Channel *> Channel::deleting; Channel::Channel(const Anope::string &nname, time_t ts) + : logger(this) { if (nname.empty()) throw CoreException("A channel without a name ?"); this->name = nname; - this->creation_time = ts; - this->syncing = this->botchannel = false; - this->server_modetime = this->chanserv_modetime = 0; - this->server_modecount = this->chanserv_modecount = this->bouncy_modes = this->topic_ts = this->topic_time = 0; - - this->ci = ChannelInfo::Find(this->name); - if (this->ci) - this->ci->c = this; if (Me && Me->IsSynced()) - Log(NULL, this, "create"); + logger.Category("create").Log("Channel {0} was created", this->GetName()); - FOREACH_MOD(OnChannelCreate, (this)); + EventManager::Get()->Dispatch(&Event::ChannelCreate::OnChannelCreate, this); } Channel::~Channel() { - UnsetExtensibles(); - - FOREACH_MOD(OnChannelDelete, (this)); + EventManager::Get()->Dispatch(&Event::ChannelDelete::OnChannelDelete, this); ModeManager::StackerDel(this); if (Me && Me->IsSynced()) - Log(NULL, this, "destroy"); + logger.Category("destroy").Log("Channel {0} was destroyed", this->GetName()); if (this->ci) this->ci->c = NULL; @@ -67,6 +65,11 @@ Channel::~Channel() ChannelList.erase(this->name); } +const Anope::string &Channel::GetName() const +{ + return name; +} + void Channel::Reset() { this->modes.clear(); @@ -100,7 +103,7 @@ void Channel::Reset() void Channel::Sync() { syncing = false; - FOREACH_MOD(OnChannelSync, (this)); + EventManager::Get()->Dispatch(&Event::ChannelSync::OnChannelSync, this); CheckModes(); } @@ -112,13 +115,13 @@ void Channel::CheckModes() /* Check for mode bouncing */ if (this->chanserv_modetime == Anope::CurTime && this->server_modetime == Anope::CurTime && this->server_modecount >= 3 && this->chanserv_modecount >= 3) { - Log() << "Warning: unable to set modes on channel " << this->name << ". Are your servers' U:lines configured correctly?"; + Anope::Logger.Log("Warning: unable to set modes on channel {0}. Are your servers' U:lines configured correctly?", this->GetName()); this->bouncy_modes = 1; return; } Reference<Channel> ref = this; - FOREACH_MOD(OnCheckModes, (ref)); + EventManager::Get()->Dispatch(&Event::CheckModes::OnCheckModes, ref); } bool Channel::CheckDelete() @@ -134,7 +137,7 @@ bool Channel::CheckDelete() return false; EventReturn MOD_RESULT; - FOREACH_RESULT(OnCheckDelete, MOD_RESULT, (this)); + MOD_RESULT = EventManager::Get()->Dispatch(&Event::CheckDelete::OnCheckDelete, this); return MOD_RESULT != EVENT_STOP && this->users.empty(); } @@ -142,7 +145,7 @@ bool Channel::CheckDelete() ChanUserContainer* Channel::JoinUser(User *user, const ChannelStatus *status) { if (user->server && user->server->IsSynced()) - Log(user, this, "join"); + logger.Category("join").Log(_("{0} joined {1}"), user->GetMask(), this->GetName()); ChanUserContainer *cuc = new ChanUserContainer(user, this); user->chans[this] = cuc; @@ -156,16 +159,16 @@ ChanUserContainer* Channel::JoinUser(User *user, const ChannelStatus *status) void Channel::DeleteUser(User *user) { if (user->server && user->server->IsSynced() && !user->Quitting()) - Log(user, this, "leave"); + logger.Category("leave").Log(_("{0} left {1}"), user->GetMask(), this->GetName()); - FOREACH_MOD(OnLeaveChannel, (user, this)); + EventManager::Get()->Dispatch(&Event::LeaveChannel::OnLeaveChannel, user, this); ChanUserContainer *cu = user->FindChannel(this); if (!this->users.erase(user)) - Log(LOG_DEBUG) << "Channel::DeleteUser() tried to delete nonexistent user " << user->nick << " from channel " << this->name; + logger.Debug("Channel::DeleteUser() tried to delete non-existent user {0} from channel {1}", user->GetMask(), this->GetName()); if (!user->chans.erase(this)) - Log(LOG_DEBUG) << "Channel::DeleteUser() tried to delete nonexistent channel " << this->name << " from " << user->nick << "'s channel list"; + logger.Debug("Channel::DeleteUser() tried to delete non-existent channel {0} from channel list of user {1}", this->GetName(), user->GetMask()); delete cu; QueueForDeletion(); @@ -257,7 +260,7 @@ std::vector<Anope::string> Channel::GetModeList(const Anope::string &mname) return r; } -void Channel::SetModeInternal(MessageSource &setter, ChannelMode *ocm, const Anope::string &oparam, bool enforce_mlock) +void Channel::SetModeInternal(const MessageSource &setter, ChannelMode *ocm, const Anope::string &oparam, bool enforce_mlock) { if (!ocm) return; @@ -272,7 +275,7 @@ void Channel::SetModeInternal(MessageSource &setter, ChannelMode *ocm, const Ano { if (param.empty()) { - Log() << "Channel::SetModeInternal() mode " << cm->mchar << " with no parameter for channel " << this->name; + Anope::Logger.Log("Channel::SetModeInternal() mode {} with no parameter for channel {}", cm->mchar, this->GetName()); return; } @@ -280,18 +283,18 @@ void Channel::SetModeInternal(MessageSource &setter, ChannelMode *ocm, const Ano if (!u) { - Log(LOG_DEBUG) << "MODE " << this->name << " +" << cm->mchar << " for nonexistent user " << param; + Anope::Logger.Debug("Mode +{0} for non-existent user {1} on channel {2}", cm->mchar, param, this->GetName()); return; } - Log(LOG_DEBUG) << "Setting +" << cm->mchar << " on " << this->name << " for " << u->nick; + Anope::Logger.Debug("Setting +{0} on {1} for {2}", cm->mchar, this->GetName(), u->GetMask()); /* Set the status on the user */ ChanUserContainer *cc = u->FindChannel(this); if (cc) cc->status.AddMode(cm->mchar); - FOREACH_RESULT(OnChannelModeSet, MOD_RESULT, (this, setter, cm, param)); + MOD_RESULT = EventManager::Get()->Dispatch(&Event::ChannelModeSet::OnChannelModeSet, this, setter, cm, param); /* Enforce secureops, etc */ if (enforce_mlock && MOD_RESULT != EVENT_STOP) @@ -308,17 +311,11 @@ void Channel::SetModeInternal(MessageSource &setter, ChannelMode *ocm, const Ano if (param.empty() && cm->type != MODE_REGULAR) { - Log() << "Channel::SetModeInternal() mode " << cm->mchar << " for " << this->name << " with no paramater, but is a param mode"; + Anope::Logger.Log("Channel::SetModeInternal() mode {0} for {1} with no paramater, but is a param mode", cm->mchar, this->name); return; } - if (cm->type == MODE_LIST) - { - ChannelModeList *cml = anope_dynamic_static_cast<ChannelModeList *>(cm); - cml->OnAdd(this, param); - } - - FOREACH_RESULT(OnChannelModeSet, MOD_RESULT, (this, setter, cm, param)); + MOD_RESULT = EventManager::Get()->Dispatch(&Event::ChannelModeSet::OnChannelModeSet, this, setter, cm, param); /* Check if we should enforce mlock */ if (!enforce_mlock || MOD_RESULT == EVENT_STOP) @@ -327,7 +324,7 @@ void Channel::SetModeInternal(MessageSource &setter, ChannelMode *ocm, const Ano this->CheckModes(); } -void Channel::RemoveModeInternal(MessageSource &setter, ChannelMode *ocm, const Anope::string &oparam, bool enforce_mlock) +void Channel::RemoveModeInternal(const MessageSource &setter, ChannelMode *ocm, const Anope::string &oparam, bool enforce_mlock) { if (!ocm) return; @@ -342,27 +339,26 @@ void Channel::RemoveModeInternal(MessageSource &setter, ChannelMode *ocm, const { if (param.empty()) { - Log() << "Channel::RemoveModeInternal() mode " << cm->mchar << " with no parameter for channel " << this->name; + Anope::Logger.Log("Channel::RemoveModeInternal() mode {0} with no parameter for channel {1}", cm->mchar, this->GetName()); return; } - BotInfo *bi = BotInfo::Find(param); - User *u = bi ? bi : User::Find(param); + User *u = User::Find(param); if (!u) { - Log(LOG_DEBUG) << "Channel::RemoveModeInternal() MODE " << this->name << "-" << cm->mchar << " for nonexistent user " << param; + Anope::Logger.Debug("Mode -{0} for non-existent user {1} on channel {2}", cm->mchar, param, this->GetName()); return; } - Log(LOG_DEBUG) << "Setting -" << cm->mchar << " on " << this->name << " for " << u->nick; + Anope::Logger.Debug("Setting -{0} on {1} for {2}", cm->mchar, this->GetName(), u->GetMask()); /* Remove the status on the user */ ChanUserContainer *cc = u->FindChannel(this); if (cc) cc->status.DelMode(cm->mchar); - FOREACH_RESULT(OnChannelModeUnset, MOD_RESULT, (this, setter, cm, param)); + MOD_RESULT = EventManager::Get()->Dispatch(&Event::ChannelModeUnset::OnChannelModeUnset, this, setter, cm, param); if (enforce_mlock && MOD_RESULT != EVENT_STOP) this->SetCorrectModes(u, false); @@ -382,13 +378,7 @@ void Channel::RemoveModeInternal(MessageSource &setter, ChannelMode *ocm, const else this->modes.erase(cm->name); - if (cm->type == MODE_LIST) - { - ChannelModeList *cml = anope_dynamic_static_cast<ChannelModeList *>(cm); - cml->OnDel(this, param); - } - - FOREACH_RESULT(OnChannelModeUnset, MOD_RESULT, (this, setter, cm, param)); + MOD_RESULT = EventManager::Get()->Dispatch(&Event::ChannelModeUnset::OnChannelModeUnset, this, setter, cm, param); if (cm->name == "PERM") { @@ -406,7 +396,7 @@ void Channel::RemoveModeInternal(MessageSource &setter, ChannelMode *ocm, const this->CheckModes(); } -void Channel::SetMode(BotInfo *bi, ChannelMode *cm, const Anope::string ¶m, bool enforce_mlock) +void Channel::SetMode(User *bi, ChannelMode *cm, const Anope::string ¶m, bool enforce_mlock) { Anope::string wparam = param; if (!cm) @@ -455,16 +445,15 @@ void Channel::SetMode(BotInfo *bi, ChannelMode *cm, const Anope::string ¶m, ChannelMode *wcm = cm->Wrap(wparam); ModeManager::StackerAdd(bi, this, wcm, true, wparam); - MessageSource ms(bi); - SetModeInternal(ms, wcm, wparam, enforce_mlock); + SetModeInternal(bi, wcm, wparam, enforce_mlock); } -void Channel::SetMode(BotInfo *bi, const Anope::string &mname, const Anope::string ¶m, bool enforce_mlock) +void Channel::SetMode(User *bi, const Anope::string &mname, const Anope::string ¶m, bool enforce_mlock) { SetMode(bi, ModeManager::FindChannelModeByName(mname), param, enforce_mlock); } -void Channel::RemoveMode(BotInfo *bi, ChannelMode *cm, const Anope::string ¶m, bool enforce_mlock) +void Channel::RemoveMode(User *bi, ChannelMode *cm, const Anope::string ¶m, bool enforce_mlock) { if (!cm) return; @@ -509,11 +498,10 @@ void Channel::RemoveMode(BotInfo *bi, ChannelMode *cm, const Anope::string ¶ ChannelMode *wcm = cm->Wrap(wparam); ModeManager::StackerAdd(bi, this, wcm, false, wparam); - MessageSource ms(bi); - RemoveModeInternal(ms, wcm, wparam, enforce_mlock); + RemoveModeInternal(bi, wcm, wparam, enforce_mlock); } -void Channel::RemoveMode(BotInfo *bi, const Anope::string &mname, const Anope::string ¶m, bool enforce_mlock) +void Channel::RemoveMode(User *bi, const Anope::string &mname, const Anope::string ¶m, bool enforce_mlock) { RemoveMode(bi, ModeManager::FindChannelModeByName(mname), param, enforce_mlock); } @@ -533,7 +521,7 @@ bool Channel::GetParam(const Anope::string &mname, Anope::string &target) const return false; } -void Channel::SetModes(BotInfo *bi, bool enforce_mlock, const char *cmodes, ...) +void Channel::SetModes(User *bi, bool enforce_mlock, const char *cmodes, ...) { char buf[BUFSIZE] = ""; va_list args; @@ -606,12 +594,12 @@ void Channel::SetModesInternal(MessageSource &source, const Anope::string &mode, ; else if (ts > this->creation_time) { - Log(LOG_DEBUG) << "Dropping mode " << mode << " on " << this->name << ", " << ts << " > " << this->creation_time; + Anope::Logger.Debug("Dropping mode {0} on {1}, TS {2] > {3}", mode, this->GetName(), ts, this->creation_time); return; } else if (ts < this->creation_time) { - Log(LOG_DEBUG) << "Changing TS of " << this->name << " from " << this->creation_time << " to " << ts; + Anope::Logger.Debug("Changing TS of {0} from {1} to {2}", this->GetName(), this->creation_time, ts); this->creation_time = ts; this->Reset(); } @@ -649,7 +637,7 @@ void Channel::SetModesInternal(MessageSource &source, const Anope::string &mode, cm = ModeManager::FindChannelModeByChar(m[i]); if (!cm) { - Log(LOG_DEBUG) << "Channel::SetModeInternal: Unknown mode char " << m[i]; + Anope::Logger.Debug("Channel::SetModeInternal: Unknown mode char {0}", m[i]); continue; } modestring += cm->mchar; @@ -692,7 +680,9 @@ void Channel::SetModesInternal(MessageSource &source, const Anope::string &mode, this->RemoveModeInternal(source, cm, token, enforce_mlock); } else - Log() << "warning: Channel::SetModesInternal() received more modes requiring params than params, modes: " << mode; + { + Anope::Logger.Log("warning: Channel::SetModesInternal() received more modes requiring params than params, modes: {0}", mode); + } } if (!this_reference) @@ -710,9 +700,9 @@ void Channel::SetModesInternal(MessageSource &source, const Anope::string &mode, } if (setter) - Log(setter, this, "mode") << modestring << paramstring; + logger.Category("mode").Log("{0} {1}", modestring, paramstring); else - Log(LOG_DEBUG) << source.GetName() << " is setting " << this->name << " to " << modestring << paramstring; + logger.Debug("{0} is setting {1] to {2}{3}", source.GetName(), this->GetName(), modestring, paramstring); if (enforce_mlock) this->CheckModes(); @@ -734,58 +724,52 @@ bool Channel::MatchesList(User *u, const Anope::string &mode) return false; } -void Channel::KickInternal(const MessageSource &source, const Anope::string &nick, const Anope::string &reason) +bool Channel::KickInternal(const MessageSource &source, const Anope::string &nick, const Anope::string &reason) { User *sender = source.GetUser(); User *target = User::Find(nick); + if (!target) { - Log(LOG_DEBUG) << "Channel::KickInternal got a nonexistent user " << nick << " on " << this->name << ": " << reason; - return; + Anope::Logger.Debug("Channel::KickInternal got a nonexistent user {0} on {1}: {2}", nick, this->GetName(), reason); + return false; } if (sender) - Log(sender, this, "kick") << "kicked " << target->nick << " (" << reason << ")"; + logger.User(sender).Category("kick").Log(_("{0} kicked {1} from {2} ({3})"), sender->GetMask(), target->GetMask(), this->GetName(), reason); else - Log(target, this, "kick") << "was kicked by " << source.GetName() << " (" << reason << ")"; + logger.Category("kick").Log(_("{0} kicked {1} from {2} ({3})"), source.GetName(), target->GetMask(), this->GetName(), reason); Anope::string chname = this->name; ChanUserContainer *cu = target->FindChannel(this); if (cu == NULL) { - Log(LOG_DEBUG) << "Channel::KickInternal got kick for user " << target->nick << " from " << source.GetSource() << " who isn't on channel " << this->name; - return; + Anope::Logger.Debug("Kick for user {0} who is not in channel {1}", target->GetMask(), this->GetName()); + return false; } ChannelStatus status = cu->status; - FOREACH_MOD(OnPreUserKicked, (source, cu, reason)); + EventReturn MOD_RESULT = EventManager::Get()->Dispatch(&Event::PreUserKicked::OnPreUserKicked, source, cu, reason); + if ((sender && sender->server == Me) || source.GetServer() == Me) + if (MOD_RESULT == EVENT_STOP) + return false; + this->DeleteUser(target); - FOREACH_MOD(OnUserKicked, (source, target, this->name, status, reason)); + EventManager::Get()->Dispatch(&Event::UserKicked::OnUserKicked, source, target, this->name, status, reason); + return true; } -bool Channel::Kick(BotInfo *bi, User *u, const char *reason, ...) +bool Channel::Kick(User *source, User *u, const Anope::string &reason) { - va_list args; - char buf[BUFSIZE] = ""; - va_start(args, reason); - vsnprintf(buf, BUFSIZE - 1, reason, args); - va_end(args); - /* Do not kick protected clients or Ulines */ if (u->IsProtected()) return false; - if (bi == NULL) - bi = this->ci->WhoSends(); - - EventReturn MOD_RESULT; - FOREACH_RESULT(OnBotKick, MOD_RESULT, (bi, this, u, buf)); - if (MOD_RESULT == EVENT_STOP) + if (!this->KickInternal(source, u->nick, reason)) return false; - IRCD->SendKick(bi, this, u, "%s", buf); - this->KickInternal(bi, u->nick, buf); + IRCD->SendKick(source, this, u, reason); return true; } @@ -796,9 +780,9 @@ void Channel::ChangeTopicInternal(User *u, const Anope::string &user, const Anop this->topic_ts = ts; this->topic_time = Anope::CurTime; - Log(LOG_DEBUG) << "Topic of " << this->name << " changed by " << this->topic_setter << " to " << newtopic; + Anope::Logger.Debug("Topic of {0} changed by {1} to {2}", this->GetName(), this->topic_setter, newtopic); - FOREACH_MOD(OnTopicUpdated, (u, this, user, this->topic)); + EventManager::Get()->Dispatch(&Event::TopicUpdated::OnTopicUpdated, u, this, user, this->topic); } void Channel::ChangeTopic(const Anope::string &user, const Anope::string &newtopic, time_t ts) @@ -807,12 +791,12 @@ void Channel::ChangeTopic(const Anope::string &user, const Anope::string &newtop this->topic_setter = user; this->topic_ts = ts; - IRCD->SendTopic(this->ci->WhoSends(), this); + IRCD->Send<messages::Topic>(this->ci ? this->ci->WhoSends() : Config->GetClient("ChanServ"), this, newtopic, ts, user); /* Now that the topic is set update the time set. This is *after* we set it so the protocol modules are able to tell the old last set time */ this->topic_time = Anope::CurTime; - FOREACH_MOD(OnTopicUpdated, (NULL, this, user, this->topic)); + EventManager::Get()->Dispatch(&Event::TopicUpdated::OnTopicUpdated, nullptr, this, user, this->topic); } void Channel::SetCorrectModes(User *user, bool give_modes) @@ -823,14 +807,14 @@ void Channel::SetCorrectModes(User *user, bool give_modes) if (!this->ci) return; - Log(LOG_DEBUG) << "Setting correct user modes for " << user->nick << " on " << this->name << " (" << (give_modes ? "" : "not ") << "giving modes)"; + Anope::Logger.Debug("Setting correct user modes for {0} on {1} ({2}giving modes)", user->nick, this->name, give_modes ? "" : "not "); - AccessGroup u_access = ci->AccessFor(user); + ChanServ::AccessGroup u_access = ci->AccessFor(user); /* Initially only take modes if the channel is being created by a non netmerge */ bool take_modes = this->syncing && user->server->IsSynced(); - FOREACH_MOD(OnSetCorrectModes, (user, this, u_access, give_modes, take_modes)); + EventManager::Get()->Dispatch(&Event::SetCorrectModes::OnSetCorrectModes, user, this, u_access, give_modes, take_modes); /* Never take modes from ulines */ if (user->server->IsULined()) @@ -857,7 +841,7 @@ void Channel::SetCorrectModes(User *user, bool give_modes) } } /* modes that have no privileges assigned shouldn't be removed (like operprefix, ojoin) */ - else if (take_modes && !has_priv && ci->GetLevel(cm->name + "ME") != ACCESS_INVALID && !u_access.HasPriv(cm->name + "ME")) + else if (take_modes && !has_priv && ci->GetLevel(cm->name + "ME") != ChanServ::ACCESS_INVALID && !u_access.HasPriv(cm->name + "ME")) { /* Only remove modes if they are > voice */ if (cm->name == "VOICE") @@ -901,20 +885,19 @@ bool Channel::CheckKick(User *user) Anope::string mask, reason; - EventReturn MOD_RESULT; - FOREACH_RESULT(OnCheckKick, MOD_RESULT, (user, this, mask, reason)); + EventReturn MOD_RESULT = EventManager::Get()->Dispatch(&Event::CheckKick::OnCheckKick, user, this, mask, reason); if (MOD_RESULT != EVENT_STOP) return false; if (mask.empty()) mask = this->ci->GetIdealBan(user); if (reason.empty()) - reason = Language::Translate(user->Account(), CHAN_NOT_ALLOWED_TO_JOIN); + reason = Language::Translate(user->Account(), _("You are not permitted to be on this channel.")); - Log(LOG_DEBUG) << "Autokicking " << user->nick << " (" << mask << ") from " << this->name; + Anope::Logger.Debug("Autokicking {0} ({1}) from {2}", user->nick, mask, this->name); this->SetMode(NULL, "BAN", mask); - this->Kick(NULL, user, "%s", reason.c_str()); + this->Kick(NULL, user, reason); return true; } @@ -954,3 +937,4 @@ void Channel::DeleteChannels() } deleting.clear(); } + |