2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013-2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class CommandShowFile : public Command
41 std::string introtext;
43 unsigned int intronumeric;
44 unsigned int textnumeric;
45 unsigned int endnumeric;
50 CommandShowFile(Module* parent, const std::string& cmdname)
51 : Command(parent, cmdname)
55 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
57 if (method == SF_NUMERIC)
59 if (!introtext.empty() && intronumeric)
60 user->WriteRemoteNumeric(intronumeric, introtext);
62 for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
63 user->WriteRemoteNumeric(textnumeric, InspIRCd::Format(" %s", i->c_str()));
65 if (!endtext.empty() && endnumeric)
66 user->WriteRemoteNumeric(endnumeric, endtext.c_str());
68 else if (IS_LOCAL(user))
70 LocalUser* const localuser = IS_LOCAL(user);
71 for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
73 const std::string& line = *i;
74 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, localuser, line, ((method == SF_MSG) ? MSG_PRIVMSG : MSG_NOTICE));
75 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
81 void UpdateSettings(ConfigTag* tag, const std::vector<std::string>& filecontents)
83 introtext = tag->getString("introtext", "Showing " + name);
84 endtext = tag->getString("endtext", "End of " + name);
85 intronumeric = tag->getUInt("intronumeric", RPL_RULESTART, 0, 999);
86 textnumeric = tag->getUInt("numeric", RPL_RULES, 0, 999);
87 endnumeric = tag->getUInt("endnumeric", RPL_RULESEND, 0, 999);
88 std::string smethod = tag->getString("method");
93 else if (smethod == "notice")
96 contents = filecontents;
97 InspIRCd::ProcessColors(contents);
101 class ModuleShowFile : public Module
103 std::vector<CommandShowFile*> cmds;
105 void ReadTag(ConfigTag* tag, std::vector<CommandShowFile*>& newcmds)
107 std::string cmdname = tag->getString("name");
109 throw ModuleException("Empty value for 'name'");
111 std::transform(cmdname.begin(), cmdname.end(), cmdname.begin(), ::toupper);
113 const std::string file = tag->getString("file", cmdname);
115 throw ModuleException("Empty value for 'file'");
116 FileReader reader(file);
118 CommandShowFile* sfcmd;
119 Command* handler = ServerInstance->Parser.GetHandler(cmdname);
122 // Command exists, check if it is ours
123 if (handler->creator != this)
124 throw ModuleException("Command " + cmdname + " already exists");
126 // This is our command, make sure we don't have the same entry twice
127 sfcmd = static_cast<CommandShowFile*>(handler);
128 if (stdalgo::isin(newcmds, sfcmd))
129 throw ModuleException("Command " + cmdname + " is already used in a <showfile> tag");
133 // Command doesn't exist, create it
134 sfcmd = new CommandShowFile(this, cmdname);
135 ServerInstance->Modules->AddService(*sfcmd);
138 sfcmd->UpdateSettings(tag, reader.GetVector());
139 newcmds.push_back(sfcmd);
143 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
145 std::vector<CommandShowFile*> newcmds;
147 ConfigTagList tags = ServerInstance->Config->ConfTags("showfile");
148 for (ConfigIter i = tags.first; i != tags.second; ++i)
150 ConfigTag* tag = i->second;
153 ReadTag(tag, newcmds);
155 catch (CoreException& ex)
157 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error: " + ex.GetReason() + " at " + tag->getTagLocation());
161 // Remove all commands that were removed from the config
162 std::vector<CommandShowFile*> removed(cmds.size());
163 std::sort(newcmds.begin(), newcmds.end());
164 std::set_difference(cmds.begin(), cmds.end(), newcmds.begin(), newcmds.end(), removed.begin());
166 stdalgo::delete_all(removed);
172 stdalgo::delete_all(cmds);
175 Version GetVersion() CXX11_OVERRIDE
177 return Version("Provides support for showing text files to users", VF_VENDOR);
181 MODULE_INIT(ModuleShowFile)