From 9ba719688161499f01c168b1aed84a563bcb5953 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 15 Sep 2012 10:11:31 -0400 Subject: [PATCH] Make CommandSource use references, sometimes we hold them for awhile --- include/base.h | 7 +++++++ include/commands.h | 21 +++++++++++---------- include/logger.h | 2 +- modules/commands/cs_access.cpp | 2 +- modules/commands/hs_group.cpp | 2 +- modules/commands/hs_request.cpp | 2 +- modules/commands/ms_rsend.cpp | 2 +- modules/commands/ns_access.cpp | 4 ++-- modules/commands/ns_drop.cpp | 2 +- modules/commands/ns_ghost.cpp | 2 +- modules/commands/ns_group.cpp | 4 ++-- modules/commands/ns_info.cpp | 2 +- modules/commands/ns_recover.cpp | 2 +- modules/commands/ns_register.cpp | 4 ++-- modules/commands/ns_release.cpp | 4 ++-- modules/commands/ns_update.cpp | 2 +- modules/extra/m_ldap_authentication.cpp | 17 ++++++++--------- src/command.cpp | 18 ++++++++++++------ src/logger.cpp | 2 +- 19 files changed, 57 insertions(+), 44 deletions(-) diff --git a/include/base.h b/include/base.h index 9ea0dc630..c09b97400 100644 --- a/include/base.h +++ b/include/base.h @@ -88,6 +88,13 @@ class dynamic_reference : public dynamic_reference_base return NULL; } + inline T* operator*() + { + if (operator bool()) + return this->ref; + return NULL; + } + inline void operator=(T *newref) { if (operator bool()) diff --git a/include/commands.h b/include/commands.h index c3a412624..45b122247 100644 --- a/include/commands.h +++ b/include/commands.h @@ -44,18 +44,18 @@ class CoreExport CommandSource /* The nick executing the command */ Anope::string nick; /* User executing the command, may be NULL */ - User *u; + dynamic_reference u; public: /* The account executing the command */ - NickCore *nc; + dynamic_reference nc; /* Where the reply should go */ CommandReply *reply; /* Channel the command was executed on (fantasy) */ - Channel *c; + dynamic_reference c; /* The service this command is on */ - BotInfo *owner; + dynamic_reference owner; /* The service the reply should come from, *not* necessarily the service the command is on */ - BotInfo *service; + dynamic_reference service; /* The actual name of the command being executed */ Anope::string command; /* The permission of the command being executed */ @@ -64,17 +64,18 @@ class CoreExport CommandSource CommandSource(const Anope::string &n, User *user, NickCore *core, CommandReply *reply); const Anope::string &GetNick() const; - User *GetUser() const; - AccessGroup AccessFor(ChannelInfo *ci) const; - bool IsFounder(ChannelInfo *ci) const; + User *GetUser(); + NickCore *GetAccount(); + AccessGroup AccessFor(ChannelInfo *ci); + bool IsFounder(ChannelInfo *ci); void Reply(const char *message, ...); void Reply(const Anope::string &message); bool HasCommand(const Anope::string &cmd); bool HasPriv(const Anope::string &cmd); - bool IsServicesOper() const; - bool IsOper() const; + bool IsServicesOper(); + bool IsOper(); }; /** Every services command is a class, inheriting from Command. diff --git a/include/logger.h b/include/logger.h index eea86f890..9b3658e94 100644 --- a/include/logger.h +++ b/include/logger.h @@ -65,7 +65,7 @@ class CoreExport Log Log(LogType type = LOG_NORMAL, const Anope::string &category = "", const BotInfo *bi = NULL); /* LOG_COMMAND/OVERRIDE/ADMIN */ - Log(LogType type, const CommandSource &source, Command *c, const ChannelInfo *ci = NULL); + Log(LogType type, CommandSource &source, Command *c, const ChannelInfo *ci = NULL); /* LOG_CHANNEL */ Log(const User *u, Channel *c, const Anope::string &category = ""); diff --git a/modules/commands/cs_access.cpp b/modules/commands/cs_access.cpp index c482e3c66..df724db1e 100644 --- a/modules/commands/cs_access.cpp +++ b/modules/commands/cs_access.cpp @@ -469,7 +469,7 @@ class CommandCSAccess : public Command else if (is_del) { const NickAlias *na = findnick(nick); - if (na && na->nc == source.nc) + if (na && na->nc == source.GetAccount()) has_access = true; } diff --git a/modules/commands/hs_group.cpp b/modules/commands/hs_group.cpp index f0d030c70..22538ed1a 100644 --- a/modules/commands/hs_group.cpp +++ b/modules/commands/hs_group.cpp @@ -38,7 +38,7 @@ class CommandHSGroup : public Command void Execute(CommandSource &source, const std::vector ¶ms) anope_override { NickAlias *na = findnick(source.GetNick()); - if (na && source.nc == na->nc && na->HasVhost()) + if (na && source.GetAccount() == na->nc && na->HasVhost()) { this->Sync(na); if (!na->GetVhostIdent().empty()) diff --git a/modules/commands/hs_request.cpp b/modules/commands/hs_request.cpp index 1b08968ae..f4b710b7f 100644 --- a/modules/commands/hs_request.cpp +++ b/modules/commands/hs_request.cpp @@ -89,7 +89,7 @@ class CommandHSRequest : public Command { User *u = source.GetUser(); NickAlias *na = findnick(source.GetNick()); - if (!na || na->nc != source.nc) + if (!na || na->nc != source.GetAccount()) { source.Reply(ACCESS_DENIED); return; diff --git a/modules/commands/ms_rsend.cpp b/modules/commands/ms_rsend.cpp index 6dc79bb44..9b6545f7f 100644 --- a/modules/commands/ms_rsend.cpp +++ b/modules/commands/ms_rsend.cpp @@ -34,7 +34,7 @@ class CommandMSRSend : public Command const NickAlias *na = NULL; /* prevent user from rsend to themselves */ - if ((na = findnick(nick)) && na->nc == source.nc) + if ((na = findnick(nick)) && na->nc == source.GetAccount()) { source.Reply(_("You can not request a receipt when sending a memo to yourself.")); return; diff --git a/modules/commands/ns_access.cpp b/modules/commands/ns_access.cpp index 73748a401..915b70cb3 100644 --- a/modules/commands/ns_access.cpp +++ b/modules/commands/ns_access.cpp @@ -114,12 +114,12 @@ class CommandNSAccess : public Command source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); return; } - else if (na->nc != source.nc && !source.HasPriv("nickserv/access")) + else if (na->nc != source.GetAccount() && !source.HasPriv("nickserv/access")) { source.Reply(ACCESS_DENIED); return; } - else if (Config->NSSecureAdmins && source.nc != na->nc && na->nc->IsServicesOper() && !cmd.equals_ci("LIST")) + else if (Config->NSSecureAdmins && source.GetAccount() != na->nc && na->nc->IsServicesOper() && !cmd.equals_ci("LIST")) { source.Reply(_("You may view but not modify the access list of other services operators.")); return; diff --git a/modules/commands/ns_drop.cpp b/modules/commands/ns_drop.cpp index 9f6de6f5a..d5ac7cef8 100644 --- a/modules/commands/ns_drop.cpp +++ b/modules/commands/ns_drop.cpp @@ -38,7 +38,7 @@ class CommandNSDrop : public Command return; } - bool is_mine = source.nc == na->nc; + bool is_mine = source.GetAccount() == na->nc; Anope::string my_nick; if (is_mine && nick.empty()) my_nick = na->nick; diff --git a/modules/commands/ns_ghost.cpp b/modules/commands/ns_ghost.cpp index 074c133b2..3362cf408 100644 --- a/modules/commands/ns_ghost.cpp +++ b/modules/commands/ns_ghost.cpp @@ -44,7 +44,7 @@ class CommandNSGhost : public Command else { bool ok = false; - if (source.nc == na->nc) + if (source.GetAccount() == na->nc) ok = true; else if (!na->nc->HasFlag(NI_SECURE) && source.GetUser() && is_on_access(source.GetUser(), na->nc)) ok = true; diff --git a/modules/commands/ns_group.cpp b/modules/commands/ns_group.cpp index ed1a9eb75..3df4b3460 100644 --- a/modules/commands/ns_group.cpp +++ b/modules/commands/ns_group.cpp @@ -266,7 +266,7 @@ class CommandNSGList : public Command source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); return; } - else if (na->nc != source.nc && !source.IsServicesOper()) + else if (na->nc != source.GetAccount() && !source.IsServicesOper()) { source.Reply(ACCESS_DENIED, Config->NickServ.c_str()); return; @@ -275,7 +275,7 @@ class CommandNSGList : public Command nc = na->nc; } else - nc = source.nc; + nc = source.GetAccount(); ListFormatter list; list.addColumn("Nick").addColumn("Expires"); diff --git a/modules/commands/ns_info.cpp b/modules/commands/ns_info.cpp index 94126e93b..b3b734a39 100644 --- a/modules/commands/ns_info.cpp +++ b/modules/commands/ns_info.cpp @@ -57,7 +57,7 @@ class CommandNSInfo : public Command if (u2 && u2->Account() == na->nc) nick_online = true; - if (has_auspex || na->nc == source.nc) + if (has_auspex || na->nc == source.GetAccount()) show_hidden = true; source.Reply(_("%s is %s"), na->nick.c_str(), na->last_realname.c_str()); diff --git a/modules/commands/ns_recover.cpp b/modules/commands/ns_recover.cpp index b74703a48..7c5b6cbeb 100644 --- a/modules/commands/ns_recover.cpp +++ b/modules/commands/ns_recover.cpp @@ -82,7 +82,7 @@ class CommandNSRecover : public Command else { bool ok = false; - if (source.nc == na->nc) + if (source.GetAccount() == na->nc) ok = true; else if (!na->nc->HasFlag(NI_SECURE) && source.GetUser() && is_on_access(source.GetUser(), na->nc)) ok = true; diff --git a/modules/commands/ns_register.cpp b/modules/commands/ns_register.cpp index 953fd2d89..2665b2381 100644 --- a/modules/commands/ns_register.cpp +++ b/modules/commands/ns_register.cpp @@ -58,7 +58,7 @@ class CommandNSConfirm : public Command { ircdproto->SendLogin(source.GetUser()); const NickAlias *na = findnick(source.GetNick()); - if (!Config->NoNicknameOwnership && na != NULL && na->nc == source.nc && na->nc->HasFlag(NI_UNCONFIRMED) == false) + if (!Config->NoNicknameOwnership && na != NULL && na->nc == source.GetAccount() && na->nc->HasFlag(NI_UNCONFIRMED) == false) source.GetUser()->SetMode(findbot(Config->NickServ), UMODE_REGISTERED); } } @@ -294,7 +294,7 @@ class CommandNSResend : public Command if (na == NULL) source.Reply(NICK_NOT_REGISTERED); - else if (na->nc != source.nc || source.nc->HasFlag(NI_UNCONFIRMED) == false) + else if (na->nc != source.GetAccount() || source.nc->HasFlag(NI_UNCONFIRMED) == false) source.Reply(_("Your account is already confirmed.")); else { diff --git a/modules/commands/ns_release.cpp b/modules/commands/ns_release.cpp index 44c89fbf7..9e12685aa 100644 --- a/modules/commands/ns_release.cpp +++ b/modules/commands/ns_release.cpp @@ -58,10 +58,10 @@ class CommandNSRelease : public Command } else { - bool override = source.nc != na->nc && source.HasPriv("nickserv/release"); + bool override = source.GetAccount() != na->nc && source.HasPriv("nickserv/release"); bool ok = override; - if (source.nc == na->nc) + if (source.GetAccount() == na->nc) ok = true; else if (source.GetUser() && !na->nc->HasFlag(NI_SECURE) && is_on_access(source.GetUser(), na->nc)) ok = true; diff --git a/modules/commands/ns_update.cpp b/modules/commands/ns_update.cpp index b4affe547..5ca8da26d 100644 --- a/modules/commands/ns_update.cpp +++ b/modules/commands/ns_update.cpp @@ -30,7 +30,7 @@ class CommandNSUpdate : public Command NickAlias *na = findnick(u->nick); - if (na && na->nc == source.nc) + if (na && na->nc == source.GetAccount()) { na->last_realname = u->realname; na->last_seen = Anope::CurTime; diff --git a/modules/extra/m_ldap_authentication.cpp b/modules/extra/m_ldap_authentication.cpp index 0f0593c34..7fcd67e5d 100644 --- a/modules/extra/m_ldap_authentication.cpp +++ b/modules/extra/m_ldap_authentication.cpp @@ -10,7 +10,6 @@ static Anope::string username_attribute; struct IdentifyInfo { - dynamic_reference user; dynamic_reference command; CommandSource source; std::vector params; @@ -20,8 +19,8 @@ struct IdentifyInfo service_reference lprov; bool admin_bind; - IdentifyInfo(User *u, Command *c, CommandSource &s, const std::vector &pa, const Anope::string &a, const Anope::string &p, service_reference &lp) : - user(u), command(c), source(s), params(pa), account(a), pass(p), lprov(lp), admin_bind(true) { } + IdentifyInfo(Command *c, CommandSource &s, const std::vector &pa, const Anope::string &a, const Anope::string &p, service_reference &lp) : + command(c), source(s), params(pa), account(a), pass(p), lprov(lp), admin_bind(true) { } }; class IdentifyInterface : public LDAPInterface @@ -47,7 +46,7 @@ class IdentifyInterface : public LDAPInterface IdentifyInfo *ii = it->second; this->requests.erase(it); - if (!ii->user || !ii->command || !ii->lprov) + if (!ii->source.GetUser() || !ii->command || !ii->lprov) { delete this; return; @@ -75,7 +74,7 @@ class IdentifyInterface : public LDAPInterface } else { - User *u = ii->user; + User *u = ii->source.GetUser(); Command *c = ii->command; u->Extend("m_ldap_authentication_error", NULL); @@ -98,7 +97,7 @@ class IdentifyInterface : public LDAPInterface } else { - User *u = ii->user; + User *u = ii->source.GetUser(); Command *c = ii->command; u->Extend("m_ldap_authentication_authenticated", NULL); @@ -137,13 +136,13 @@ class IdentifyInterface : public LDAPInterface IdentifyInfo *ii = it->second; this->requests.erase(it); - if (!ii->user || !ii->command) + if (!ii->source.GetUser() || !ii->command) { delete ii; return; } - User *u = ii->user; + User *u = ii->source.GetUser(); Command *c = ii->command; u->Extend("m_ldap_authentication_error", NULL); @@ -289,7 +288,7 @@ class NSIdentifyLDAP : public Module return EVENT_CONTINUE; } - IdentifyInfo *ii = new IdentifyInfo(u, c, *source, params, account, password, this->ldap); + IdentifyInfo *ii = new IdentifyInfo(c, *source, params, account, password, this->ldap); try { LDAPQuery id = this->ldap->BindAsAdmin(&this->iinterface); diff --git a/src/command.cpp b/src/command.cpp index 21901bd34..9f1b26fa4 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -15,6 +15,7 @@ #include "opertype.h" #include "access.h" #include "regchannel.h" +#include "channels.h" CommandSource::CommandSource(const Anope::string &n, User *user, NickCore *core, CommandReply *r) : nick(n), u(user), nc(core), reply(r), c(NULL), owner(NULL), service(NULL) @@ -26,12 +27,17 @@ const Anope::string &CommandSource::GetNick() const return this->nick; } -User *CommandSource::GetUser() const +User *CommandSource::GetUser() { return this->u; } -AccessGroup CommandSource::AccessFor(ChannelInfo *ci) const +NickCore *CommandSource::GetAccount() +{ + return this->nc; +} + +AccessGroup CommandSource::AccessFor(ChannelInfo *ci) { if (this->u) return ci->AccessFor(this->u); @@ -41,12 +47,12 @@ AccessGroup CommandSource::AccessFor(ChannelInfo *ci) const return AccessGroup(); } -bool CommandSource::IsFounder(ChannelInfo *ci) const +bool CommandSource::IsFounder(ChannelInfo *ci) { if (this->u) return ::IsFounder(this->u, ci); else if (this->nc) - return this->nc == ci->GetFounder(); + return *this->nc == ci->GetFounder(); return false; } @@ -68,7 +74,7 @@ bool CommandSource::HasPriv(const Anope::string &cmd) return false; } -bool CommandSource::IsServicesOper() const +bool CommandSource::IsServicesOper() { if (this->u) return this->u->IsServicesOper(); @@ -77,7 +83,7 @@ bool CommandSource::IsServicesOper() const return false; } -bool CommandSource::IsOper() const +bool CommandSource::IsOper() { if (this->u) return this->u->HasMode(UMODE_OPER); diff --git a/src/logger.cpp b/src/logger.cpp index f14664cdb..6547e3e6b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -85,7 +85,7 @@ Log::Log(LogType type, const Anope::string &category, const BotInfo *b) : bi(b), this->Sources.push_back(bi->nick); } -Log::Log(LogType type, const CommandSource &source, Command *_c, const ChannelInfo *_ci) : nick(source.GetNick()), u(source.GetUser()), nc(source.nc), c(_c), chan(NULL), ci(_ci), s(NULL), Type(type) +Log::Log(LogType type, CommandSource &source, Command *_c, const ChannelInfo *_ci) : nick(source.GetNick()), u(source.GetUser()), nc(source.nc), c(_c), chan(NULL), ci(_ci), s(NULL), Type(type) { if (!c) throw CoreException("Invalid pointers passed to Log::Log");