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