]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_showfile.cpp
Improve UserManager::QuitUser() and related code
[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 class CommandShowFile : public Command
23 {
24         enum Method
25         {
26                 SF_MSG,
27                 SF_NOTICE,
28                 SF_NUMERIC
29         };
30
31         std::string introtext;
32         std::string endtext;
33         unsigned int intronumeric;
34         unsigned int textnumeric;
35         unsigned int endnumeric;
36         file_cache contents;
37         Method method;
38
39  public:
40         CommandShowFile(Module* parent, const std::string& cmdname)
41                 : Command(parent, cmdname)
42         {
43         }
44
45         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
46         {
47                 const std::string& sn = ServerInstance->Config->ServerName;
48                 if (method == SF_NUMERIC)
49                 {
50                         if (!introtext.empty())
51                                 user->SendText(":%s %03d %s :%s %s", sn.c_str(), intronumeric, user->nick.c_str(), sn.c_str(), introtext.c_str());
52
53                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
54                                 user->SendText(":%s %03d %s :- %s", sn.c_str(), textnumeric, user->nick.c_str(), i->c_str());
55
56                         user->SendText(":%s %03d %s :%s", sn.c_str(), endnumeric, user->nick.c_str(), endtext.c_str());
57                 }
58                 else
59                 {
60                         const char* msgcmd = (method == SF_MSG ? "PRIVMSG" : "NOTICE");
61                         std::string header = InspIRCd::Format(":%s %s %s :", sn.c_str(), msgcmd, user->nick.c_str());
62                         for (file_cache::const_iterator i = contents.begin(); i != contents.end(); ++i)
63                                 user->SendText(header + *i);
64                 }
65                 return CMD_SUCCESS;
66         }
67
68         void UpdateSettings(ConfigTag* tag, const std::vector<std::string>& filecontents)
69         {
70                 introtext = tag->getString("introtext", "Showing " + name);
71                 endtext = tag->getString("endtext", "End of " + name);
72                 intronumeric = tag->getInt("intronumeric", RPL_RULESTART, 0, 999);
73                 textnumeric = tag->getInt("numeric", RPL_RULES, 0, 999);
74                 endnumeric = tag->getInt("endnumeric", RPL_RULESEND, 0, 999);
75                 std::string smethod = tag->getString("method");
76
77                 method = SF_NUMERIC;
78                 if (smethod == "msg")
79                         method = SF_MSG;
80                 else if (smethod == "notice")
81                         method = SF_NOTICE;
82
83                 contents = filecontents;
84                 if (tag->getBool("colors"))
85                         InspIRCd::ProcessColors(contents);
86         }
87 };
88
89 class ModuleShowFile : public Module
90 {
91         std::vector<CommandShowFile*> cmds;
92
93         void ReadTag(ConfigTag* tag, std::vector<CommandShowFile*>& newcmds)
94         {
95                 std::string cmdname = tag->getString("name");
96                 if (cmdname.empty())
97                         throw ModuleException("Empty value for 'name'");
98
99                 std::transform(cmdname.begin(), cmdname.end(), cmdname.begin(), ::toupper);
100
101                 const std::string file = tag->getString("file", cmdname);
102                 if (file.empty())
103                         throw ModuleException("Empty value for 'file'");
104                 FileReader reader(file);
105
106                 CommandShowFile* sfcmd;
107                 Command* handler = ServerInstance->Parser->GetHandler(cmdname);
108                 if (handler)
109                 {
110                         // Command exists, check if it is ours
111                         if (handler->creator != this)
112                                 throw ModuleException("Command " + cmdname + " already exists");
113
114                         // This is our command, make sure we don't have the same entry twice
115                         sfcmd = static_cast<CommandShowFile*>(handler);
116                         if (std::find(newcmds.begin(), newcmds.end(), sfcmd) != newcmds.end())
117                                 throw ModuleException("Command " + cmdname + " is already used in a <showfile> tag");
118                 }
119                 else
120                 {
121                         // Command doesn't exist, create it
122                         sfcmd = new CommandShowFile(this, cmdname);
123                         ServerInstance->Modules->AddService(*sfcmd);
124                 }
125
126                 sfcmd->UpdateSettings(tag, reader.GetVector());
127                 newcmds.push_back(sfcmd);
128         }
129
130         static void DelAll(const std::vector<CommandShowFile*>& list)
131         {
132                 for (std::vector<CommandShowFile*>::const_iterator i = list.begin(); i != list.end(); ++i)
133                         delete *i;
134         }
135
136  public:
137         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
138         {
139                 std::vector<CommandShowFile*> newcmds;
140
141                 ConfigTagList tags = ServerInstance->Config->ConfTags("showfile");
142                 for (ConfigIter i = tags.first; i != tags.second; ++i)
143                 {
144                         ConfigTag* tag = i->second;
145                         try
146                         {
147                                 ReadTag(tag, newcmds);
148                         }
149                         catch (CoreException& ex)
150                         {
151                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error: " + ex.GetReason() + " at " + tag->getTagLocation());
152                         }
153                 }
154
155                 // Remove all commands that were removed from the config
156                 std::vector<CommandShowFile*> removed(cmds.size());
157                 std::sort(newcmds.begin(), newcmds.end());
158                 std::set_difference(cmds.begin(), cmds.end(), newcmds.begin(), newcmds.end(), removed.begin());
159
160                 DelAll(removed);
161                 cmds.swap(newcmds);
162         }
163
164         ~ModuleShowFile()
165         {
166                 DelAll(cmds);
167         }
168
169         Version GetVersion() CXX11_OVERRIDE
170         {
171                 return Version("Provides support for showing text files to users", VF_VENDOR);
172         }
173 };
174
175 MODULE_INIT(ModuleShowFile)