]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showfile.cpp
Various improvements to UNIX socket support.
[user/henk/code/inspircd.git] / src / modules / m_showfile.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21
22 enum
23 {
24         // From UnrealIRCd.
25         RPL_RULES = 232,
26         RPL_RULESTART = 308,
27         RPL_RULESEND = 309,
28         ERR_NORULES = 434
29 };
30
31 class CommandShowFile : public Command
32 {
33         enum Method
34         {
35                 SF_MSG,
36                 SF_NOTICE,
37                 SF_NUMERIC
38         };
39
40         std::string introtext;
41         std::string endtext;
42         unsigned int intronumeric;
43         unsigned int textnumeric;
44         unsigned int endnumeric;
45         file_cache contents;
46         Method method;
47
48  public:
49         CommandShowFile(Module* parent, const std::string& cmdname)
50                 : Command(parent, cmdname)
51         {
52         }
53
54         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
55         {
56                 if (method == SF_NUMERIC)
57                 {
58                         if (!introtext.empty() && intronumeric)
59                                 user->WriteRemoteNumeric(intronumeric, introtext);
60
61                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
62                                 user->WriteRemoteNumeric(textnumeric, InspIRCd::Format("- %s", i->c_str()));
63
64                         if (!endtext.empty() && endnumeric)
65                                 user->WriteRemoteNumeric(endnumeric, endtext.c_str());
66                 }
67                 else if (IS_LOCAL(user))
68                 {
69                         LocalUser* const localuser = IS_LOCAL(user);
70                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
71                         {
72                                 const std::string& line = *i;
73                                 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, localuser, line, ((method == SF_MSG) ? MSG_PRIVMSG : MSG_NOTICE));
74                                 localuser->Send(ServerInstance->GetRFCEvents().privmsg, msg);
75                         }
76                 }
77                 return CMD_SUCCESS;
78         }
79
80         void UpdateSettings(ConfigTag* tag, const std::vector<std::string>& filecontents)
81         {
82                 introtext = tag->getString("introtext", "Showing " + name);
83                 endtext = tag->getString("endtext", "End of " + name);
84                 intronumeric = tag->getUInt("intronumeric", RPL_RULESTART, 0, 999);
85                 textnumeric = tag->getUInt("numeric", RPL_RULES, 0, 999);
86                 endnumeric = tag->getUInt("endnumeric", RPL_RULESEND, 0, 999);
87                 std::string smethod = tag->getString("method");
88
89                 method = SF_NUMERIC;
90                 if (smethod == "msg")
91                         method = SF_MSG;
92                 else if (smethod == "notice")
93                         method = SF_NOTICE;
94
95                 contents = filecontents;
96                 InspIRCd::ProcessColors(contents);
97         }
98 };
99
100 class ModuleShowFile : public Module
101 {
102         std::vector<CommandShowFile*> cmds;
103
104         void ReadTag(ConfigTag* tag, std::vector<CommandShowFile*>& newcmds)
105         {
106                 std::string cmdname = tag->getString("name");
107                 if (cmdname.empty())
108                         throw ModuleException("Empty value for 'name'");
109
110                 std::transform(cmdname.begin(), cmdname.end(), cmdname.begin(), ::toupper);
111
112                 const std::string file = tag->getString("file", cmdname);
113                 if (file.empty())
114                         throw ModuleException("Empty value for 'file'");
115                 FileReader reader(file);
116
117                 CommandShowFile* sfcmd;
118                 Command* handler = ServerInstance->Parser.GetHandler(cmdname);
119                 if (handler)
120                 {
121                         // Command exists, check if it is ours
122                         if (handler->creator != this)
123                                 throw ModuleException("Command " + cmdname + " already exists");
124
125                         // This is our command, make sure we don't have the same entry twice
126                         sfcmd = static_cast<CommandShowFile*>(handler);
127                         if (stdalgo::isin(newcmds, sfcmd))
128                                 throw ModuleException("Command " + cmdname + " is already used in a <showfile> tag");
129                 }
130                 else
131                 {
132                         // Command doesn't exist, create it
133                         sfcmd = new CommandShowFile(this, cmdname);
134                         ServerInstance->Modules->AddService(*sfcmd);
135                 }
136
137                 sfcmd->UpdateSettings(tag, reader.GetVector());
138                 newcmds.push_back(sfcmd);
139         }
140
141  public:
142         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
143         {
144                 std::vector<CommandShowFile*> newcmds;
145
146                 ConfigTagList tags = ServerInstance->Config->ConfTags("showfile");
147                 for (ConfigIter i = tags.first; i != tags.second; ++i)
148                 {
149                         ConfigTag* tag = i->second;
150                         try
151                         {
152                                 ReadTag(tag, newcmds);
153                         }
154                         catch (CoreException& ex)
155                         {
156                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error: " + ex.GetReason() + " at " + tag->getTagLocation());
157                         }
158                 }
159
160                 // Remove all commands that were removed from the config
161                 std::vector<CommandShowFile*> removed(cmds.size());
162                 std::sort(newcmds.begin(), newcmds.end());
163                 std::set_difference(cmds.begin(), cmds.end(), newcmds.begin(), newcmds.end(), removed.begin());
164
165                 stdalgo::delete_all(removed);
166                 cmds.swap(newcmds);
167         }
168
169         ~ModuleShowFile()
170         {
171                 stdalgo::delete_all(cmds);
172         }
173
174         Version GetVersion() CXX11_OVERRIDE
175         {
176                 return Version("Provides support for showing text files to users", VF_VENDOR);
177         }
178 };
179
180 MODULE_INIT(ModuleShowFile)