2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CommandShowFile : public Command
40 std::string introtext;
42 unsigned int intronumeric;
43 unsigned int textnumeric;
44 unsigned int endnumeric;
49 CommandShowFile(Module* parent, const std::string& cmdname)
50 : Command(parent, cmdname)
54 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
56 if (method == SF_NUMERIC)
58 if (!introtext.empty())
59 user->WriteRemoteNumeric(intronumeric, introtext);
61 for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
62 user->WriteRemoteNumeric(textnumeric, InspIRCd::Format("- %s", i->c_str()));
64 user->WriteRemoteNumeric(endnumeric, endtext.c_str());
66 else if (IS_LOCAL(user))
68 LocalUser* const localuser = IS_LOCAL(user);
69 for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
71 const std::string& line = *i;
72 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, localuser, line, ((method == SF_MSG) ? MSG_PRIVMSG : MSG_NOTICE));
73 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
79 void UpdateSettings(ConfigTag* tag, const std::vector<std::string>& filecontents)
81 introtext = tag->getString("introtext", "Showing " + name);
82 endtext = tag->getString("endtext", "End of " + name);
83 intronumeric = tag->getUInt("intronumeric", RPL_RULESTART, 0, 999);
84 textnumeric = tag->getUInt("numeric", RPL_RULES, 0, 999);
85 endnumeric = tag->getUInt("endnumeric", RPL_RULESEND, 0, 999);
86 std::string smethod = tag->getString("method");
91 else if (smethod == "notice")
94 contents = filecontents;
95 InspIRCd::ProcessColors(contents);
99 class ModuleShowFile : public Module
101 std::vector<CommandShowFile*> cmds;
103 void ReadTag(ConfigTag* tag, std::vector<CommandShowFile*>& newcmds)
105 std::string cmdname = tag->getString("name");
107 throw ModuleException("Empty value for 'name'");
109 std::transform(cmdname.begin(), cmdname.end(), cmdname.begin(), ::toupper);
111 const std::string file = tag->getString("file", cmdname);
113 throw ModuleException("Empty value for 'file'");
114 FileReader reader(file);
116 CommandShowFile* sfcmd;
117 Command* handler = ServerInstance->Parser.GetHandler(cmdname);
120 // Command exists, check if it is ours
121 if (handler->creator != this)
122 throw ModuleException("Command " + cmdname + " already exists");
124 // This is our command, make sure we don't have the same entry twice
125 sfcmd = static_cast<CommandShowFile*>(handler);
126 if (stdalgo::isin(newcmds, sfcmd))
127 throw ModuleException("Command " + cmdname + " is already used in a <showfile> tag");
131 // Command doesn't exist, create it
132 sfcmd = new CommandShowFile(this, cmdname);
133 ServerInstance->Modules->AddService(*sfcmd);
136 sfcmd->UpdateSettings(tag, reader.GetVector());
137 newcmds.push_back(sfcmd);
141 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
143 std::vector<CommandShowFile*> newcmds;
145 ConfigTagList tags = ServerInstance->Config->ConfTags("showfile");
146 for (ConfigIter i = tags.first; i != tags.second; ++i)
148 ConfigTag* tag = i->second;
151 ReadTag(tag, newcmds);
153 catch (CoreException& ex)
155 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error: " + ex.GetReason() + " at " + tag->getTagLocation());
159 // Remove all commands that were removed from the config
160 std::vector<CommandShowFile*> removed(cmds.size());
161 std::sort(newcmds.begin(), newcmds.end());
162 std::set_difference(cmds.begin(), cmds.end(), newcmds.begin(), newcmds.end(), removed.begin());
164 stdalgo::delete_all(removed);
170 stdalgo::delete_all(cmds);
173 Version GetVersion() CXX11_OVERRIDE
175 return Version("Provides support for showing text files to users", VF_VENDOR);
179 MODULE_INIT(ModuleShowFile)