// Anope IRC Services // // Copyright (C) 2003-2026 Anope Contributors // // Anope is free software. You can use, modify, and/or distribute it under the // terms of version 2 of the GNU General Public License. See docs/LICENSE.txt // for the complete terms of this license and docs/AUTHORS.txt for a list of // contributors. // // Based on the original code of Epona by Lara // Based on the original code of Services by Andy Church // // SPDX-License-Identifier: GPL-2.0-only #include "module.h" class CommandBSSetGreet final : public Command { public: CommandBSSetGreet(Module *creator, const Anope::string &sname = "botserv/set/greet") : Command(creator, sname, 2, 2) { this->SetDesc(_("Enable greet messages")); this->SetSyntax(_("\037channel\037 {\037ON|OFF\037}")); } void Execute(CommandSource &source, const std::vector ¶ms) override { ChannelInfo *ci = ChannelInfo::Find(params[0]); const Anope::string &value = params[1]; if (ci == NULL) { source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str()); return; } if (!source.HasPriv("botserv/administration") && !source.AccessFor(ci).HasPriv("SET")) { source.Reply(ACCESS_DENIED); return; } if (Anope::ReadOnly) { source.Reply(READ_ONLY_MODE); return; } if (value.equals_ci("ON")) { bool override = !source.AccessFor(ci).HasPriv("SET"); Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to enable greets"; ci->Extend("BS_GREET"); source.Reply(_("Greet mode is now \002on\002 on channel %s."), ci->name.c_str()); } else if (value.equals_ci("OFF")) { bool override = !source.AccessFor(ci).HasPriv("SET"); Log(override ? LOG_OVERRIDE : LOG_COMMAND, source, this, ci) << "to disable greets"; ci->Shrink("BS_GREET"); source.Reply(_("Greet mode is now \002off\002 on channel %s."), ci->name.c_str()); } else this->OnSyntaxError(source, source.command); } bool OnHelp(CommandSource &source, const Anope::string &) override { this->SendSyntax(source); source.Reply(" "); source.Reply(_( "Enables or disables \002greet\002 mode on a channel. " "When it is enabled, the bot will display greet " "messages of users joining the channel, provided " "they have enough access to the channel." )); return true; } }; class CommandNSSetGreet : public Command { public: CommandNSSetGreet(Module *creator, const Anope::string &sname = "nickserv/set/greet", size_t min = 0) : Command(creator, sname, min, min + 1) { this->SetDesc(_("Associate a greet message with your nickname")); this->SetSyntax(_("[\037message\037]")); } void Run(CommandSource &source, const Anope::string &user, const Anope::string ¶m) { if (Anope::ReadOnly) { source.Reply(READ_ONLY_MODE); return; } const NickAlias *na = NickAlias::Find(user); if (!na) { source.Reply(NICK_X_NOT_REGISTERED, user.c_str()); return; } NickCore *nc = na->nc; EventReturn MOD_RESULT; FOREACH_RESULT(OnSetNickOption, MOD_RESULT, (source, this, nc, param)); if (MOD_RESULT == EVENT_STOP) return; if (!param.empty()) { Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to change the greet of " << nc->display; nc->Extend("greet", param); source.Reply(_("Greet message for \002%s\002 changed to \002%s\002."), nc->display.c_str(), param.c_str()); } else { Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to unset the greet of " << nc->display; nc->Shrink("greet"); source.Reply(_("Greet message for \002%s\002 unset."), nc->display.c_str()); } } void Execute(CommandSource &source, const std::vector ¶ms) override { this->Run(source, source.nc->display, params.size() > 0 ? params[0] : ""); } bool OnHelp(CommandSource &source, const Anope::string &) override { this->SendSyntax(source); source.Reply(" "); source.Reply(_( "Changes your greet message. This message will be displayed when joining a channel " "that has GREET option enabled, provided that you have the necessary access on it." )); ExampleWrapper() .AddEntry(_("I come in peace"), _( "Changes your greet message to \035I come in peace\035." )) .AddEntry("", _( "Removes your greet message." )) .SendTo(source); return true; } }; class CommandNSSASetGreet final : public CommandNSSetGreet { public: CommandNSSASetGreet(Module *creator) : CommandNSSetGreet(creator, "nickserv/saset/greet", 1) { this->ClearSyntax(); this->SetSyntax(_("\037nickname\037 [\037message\037]")); } void Execute(CommandSource &source, const std::vector ¶ms) override { this->Run(source, params[0], params.size() > 1 ? params[1] : ""); } bool OnHelp(CommandSource &source, const Anope::string &) override { this->SendSyntax(source); source.Reply(" "); source.Reply(_( "Changes the greet message of the specified nickname. This message will be displayed " "when joining a channel that has GREET option enabled, provided that the user has the " "necessary access on it." )); ExampleWrapper() .AddEntry(_("alien I come in peace"), _( "Changes the greet message of \035alien\035 to \035I come in peace\035." )) .AddEntry("zebra", _( "Removes the greet message of \035zebra\035." )) .SendTo(source); return true; } }; class Greet final : public Module { /* channel setting for whether or not greet should be shown */ SerializableExtensibleItem bs_greet; /* user greets */ SerializableExtensibleItem ns_greet; CommandBSSetGreet commandbssetgreet; CommandNSSetGreet commandnssetgreet; CommandNSSASetGreet commandnssasetgreet; public: Greet(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), bs_greet(this, "BS_GREET"), ns_greet(this, "greet"), commandbssetgreet(this), commandnssetgreet(this), commandnssasetgreet(this) { } void OnJoinChannel(User *user, Channel *c) override { /* Only display the greet if the main uplink we're connected * to has synced, or we'll get greet-floods when the net * recovers from a netsplit. -GD */ if (!c->ci || !c->ci->bi || !user->server->IsSynced() || !user->IsIdentified()) return; Anope::string *greet = ns_greet.Get(user->Account()); if (bs_greet.HasExt(c->ci) && greet != NULL && !greet->empty() && c->FindUser(c->ci->bi) && c->ci->AccessFor(user).HasPriv("GREET")) { const auto message = Anope::Format("[%s] %s", user->Account()->display.c_str(), greet->c_str()); IRCD->SendPrivmsg(*c->ci->bi, c->name, message); c->ci->bi->lastmsg = Anope::CurTime; } } void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool show_hidden) override { Anope::string *greet = ns_greet.Get(na->nc); if (greet != NULL) info[_("Greet")] = *greet; } void OnBotInfo(CommandSource &source, BotInfo *bi, ChannelInfo *ci, InfoFormatter &info) override { if (bs_greet.HasExt(ci)) info.AddOption(_("Greet")); } }; MODULE_INIT(Greet)