]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showfile.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_showfile.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22
23 enum
24 {
25         // From UnrealIRCd.
26         RPL_RULES = 232,
27         RPL_RULESTART = 308,
28         RPL_RULESEND = 309,
29         ERR_NORULES = 434
30 };
31
32 class CommandShowFile : public Command
33 {
34         enum Method
35         {
36                 SF_MSG,
37                 SF_NOTICE,
38                 SF_NUMERIC
39         };
40
41         std::string introtext;
42         std::string endtext;
43         unsigned int intronumeric;
44         unsigned int textnumeric;
45         unsigned int endnumeric;
46         file_cache contents;
47         Method method;
48
49  public:
50         CommandShowFile(Module* parent, const std::string& cmdname)
51                 : Command(parent, cmdname)
52         {
53         }
54
55         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
56         {
57                 if (method == SF_NUMERIC)
58                 {
59                         if (!introtext.empty() && intronumeric)
60                                 user->WriteRemoteNumeric(intronumeric, introtext);
61
62                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
63                                 user->WriteRemoteNumeric(textnumeric, InspIRCd::Format(" %s", i->c_str()));
64
65                         if (!endtext.empty() && endnumeric)
66                                 user->WriteRemoteNumeric(endnumeric, endtext.c_str());
67                 }
68                 else if (IS_LOCAL(user))
69                 {
70                         LocalUser* const localuser = IS_LOCAL(user);
71                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
72                         {
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);
76                         }
77                 }
78                 return CMD_SUCCESS;
79         }
80
81         void UpdateSettings(ConfigTag* tag, const std::vector<std::string>& filecontents)
82         {
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");
89
90                 method = SF_NUMERIC;
91                 if (smethod == "msg")
92                         method = SF_MSG;
93                 else if (smethod == "notice")
94                         method = SF_NOTICE;
95
96                 contents = filecontents;
97                 InspIRCd::ProcessColors(contents);
98         }
99 };
100
101 class ModuleShowFile : public Module
102 {
103         std::vector<CommandShowFile*> cmds;
104
105         void ReadTag(ConfigTag* tag, std::vector<CommandShowFile*>& newcmds)
106         {
107                 std::string cmdname = tag->getString("name");
108                 if (cmdname.empty())
109                         throw ModuleException("Empty value for 'name'");
110
111                 std::transform(cmdname.begin(), cmdname.end(), cmdname.begin(), ::toupper);
112
113                 const std::string file = tag->getString("file", cmdname);
114                 if (file.empty())
115                         throw ModuleException("Empty value for 'file'");
116                 FileReader reader(file);
117
118                 CommandShowFile* sfcmd;
119                 Command* handler = ServerInstance->Parser.GetHandler(cmdname);
120                 if (handler)
121                 {
122                         // Command exists, check if it is ours
123                         if (handler->creator != this)
124                                 throw ModuleException("Command " + cmdname + " already exists");
125
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");
130                 }
131                 else
132                 {
133                         // Command doesn't exist, create it
134                         sfcmd = new CommandShowFile(this, cmdname);
135                         ServerInstance->Modules->AddService(*sfcmd);
136                 }
137
138                 sfcmd->UpdateSettings(tag, reader.GetVector());
139                 newcmds.push_back(sfcmd);
140         }
141
142  public:
143         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
144         {
145                 std::vector<CommandShowFile*> newcmds;
146
147                 ConfigTagList tags = ServerInstance->Config->ConfTags("showfile");
148                 for (ConfigIter i = tags.first; i != tags.second; ++i)
149                 {
150                         ConfigTag* tag = i->second;
151                         try
152                         {
153                                 ReadTag(tag, newcmds);
154                         }
155                         catch (CoreException& ex)
156                         {
157                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error: " + ex.GetReason() + " at " + tag->getTagLocation());
158                         }
159                 }
160
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());
165
166                 stdalgo::delete_all(removed);
167                 cmds.swap(newcmds);
168         }
169
170         ~ModuleShowFile()
171         {
172                 stdalgo::delete_all(cmds);
173         }
174
175         Version GetVersion() CXX11_OVERRIDE
176         {
177                 return Version("Adds support for showing the contents of files to users when they execute a command.", VF_VENDOR);
178         }
179 };
180
181 MODULE_INIT(ModuleShowFile)