]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_info/core_info.cpp
Hide the server name/desc better when <options:hideserver> is set.
[user/henk/code/inspircd.git] / src / coremods / core_info / core_info.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018, 2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6  *   Copyright (C) 2014, 2016, 2018 Attila Molnar <attilamolnar@hush.com>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "core_info.h"
24
25 enum
26 {
27         // From RFC 2812.
28         RPL_WELCOME = 1,
29         RPL_YOURHOST = 2,
30         RPL_CREATED = 3,
31         RPL_MYINFO = 4
32 };
33
34 RouteDescriptor ServerTargetCommand::GetRouting(User* user, const Params& parameters)
35 {
36         // Parameter must be a server name, not a nickname or uuid
37         if ((!parameters.empty()) && (parameters[0].find('.') != std::string::npos))
38                 return ROUTE_UNICAST(parameters[0]);
39         return ROUTE_LOCALONLY;
40 }
41
42 class CoreModInfo : public Module
43 {
44         CommandAdmin cmdadmin;
45         CommandCommands cmdcommands;
46         CommandInfo cmdinfo;
47         CommandModules cmdmodules;
48         CommandMotd cmdmotd;
49         CommandServList cmdservlist;
50         CommandTime cmdtime;
51         CommandVersion cmdversion;
52         Numeric::Numeric numeric004;
53
54         /** Returns a list of user or channel mode characters.
55          * Used for constructing the parts of the mode list in the 004 numeric.
56          * @param mt Controls whether to list user modes or channel modes
57          * @param needparam Return modes only if they require a parameter to be set
58          * @return The available mode letters that satisfy the given conditions
59         */
60         static std::string CreateModeList(ModeType mt, bool needparam = false)
61         {
62                 std::string modestr;
63                 for (unsigned char mode = 'A'; mode <= 'z'; mode++)
64                 {
65                         ModeHandler* mh = ServerInstance->Modes.FindMode(mode, mt);
66                         if ((mh) && ((!needparam) || (mh->NeedsParam(true))))
67                                 modestr.push_back(mode);
68                 }
69                 return modestr;
70         }
71
72         void OnServiceChange(const ServiceProvider& prov)
73         {
74                 if (prov.service != SERVICE_MODE)
75                         return;
76
77                 std::vector<std::string>& params = numeric004.GetParams();
78                 params.erase(params.begin()+2, params.end());
79
80                 // Create lists of modes
81                 // 1. User modes
82                 // 2. Channel modes
83                 // 3. Channel modes that require a parameter when set
84                 numeric004.push(CreateModeList(MODETYPE_USER));
85                 numeric004.push(CreateModeList(MODETYPE_CHANNEL));
86                 numeric004.push(CreateModeList(MODETYPE_CHANNEL, true));
87         }
88  public:
89         CoreModInfo()
90                 : cmdadmin(this)
91                 , cmdcommands(this)
92                 , cmdinfo(this)
93                 , cmdmodules(this)
94                 , cmdmotd(this)
95                 , cmdservlist(this)
96                 , cmdtime(this)
97                 , cmdversion(this)
98                 , numeric004(RPL_MYINFO)
99         {
100                 numeric004.push(ServerInstance->Config->ServerName);
101                 numeric004.push(INSPIRCD_BRANCH);
102         }
103
104         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
105         {
106                 // Process the escape codes in the MOTDs.
107                 ConfigFileCache newmotds;
108                 for (ServerConfig::ClassVector::const_iterator iter = ServerInstance->Config->Classes.begin(); iter != ServerInstance->Config->Classes.end(); ++iter)
109                 {
110                         ConfigTag* tag = (*iter)->config;
111                         // Don't process the file if it has already been processed.
112                         const std::string motd = tag->getString("motd", "motd");
113                         if (newmotds.find(motd) != newmotds.end())
114                                 continue;
115
116                         // We can't process the file if it doesn't exist.
117                         ConfigFileCache::iterator file = ServerInstance->Config->Files.find(motd);
118                         if (file == ServerInstance->Config->Files.end())
119                                 continue;
120
121                         // Process escape codes.
122                         newmotds[file->first] = file->second;
123                         InspIRCd::ProcessColors(newmotds[file->first]);
124                 }
125
126                 cmdmotd.motds.swap(newmotds);
127
128                 ConfigTag* tag = ServerInstance->Config->ConfValue("admin");
129                 cmdadmin.AdminName = tag->getString("name");
130                 cmdadmin.AdminEmail = tag->getString("email", "null@example.com");
131                 cmdadmin.AdminNick = tag->getString("nick", "admin");
132         }
133
134         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
135         {
136                 user->WriteNumeric(RPL_WELCOME, InspIRCd::Format("Welcome to the %s IRC Network %s", ServerInstance->Config->Network.c_str(), user->GetFullRealHost().c_str()));
137                 user->WriteNumeric(RPL_YOURHOST, InspIRCd::Format("Your host is %s, running version %s", ServerInstance->Config->GetServerName().c_str(), INSPIRCD_BRANCH));
138                 user->WriteNumeric(RPL_CREATED, InspIRCd::TimeString(ServerInstance->startup_time, "This server was created %H:%M:%S %b %d %Y"));
139                 user->WriteNumeric(numeric004);
140
141                 ServerInstance->ISupport.SendTo(user);
142
143                 /* Trigger MOTD and LUSERS output, give modules a chance too */
144                 ModResult MOD_RESULT;
145                 std::string command("LUSERS");
146                 CommandBase::Params parameters;
147                 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, parameters, user, true));
148                 if (!MOD_RESULT)
149                         ServerInstance->Parser.CallHandler(command, parameters, user);
150
151                 MOD_RESULT = MOD_RES_PASSTHRU;
152                 command = "MOTD";
153                 FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (command, parameters, user, true));
154                 if (!MOD_RESULT)
155                         ServerInstance->Parser.CallHandler(command, parameters, user);
156
157                 if (ServerInstance->Config->RawLog)
158                 {
159                         ClientProtocol::Messages::Privmsg rawlogmsg(ServerInstance->FakeClient, user, "*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
160                         user->Send(ServerInstance->GetRFCEvents().privmsg, rawlogmsg);
161                 }
162         }
163
164         void OnServiceAdd(ServiceProvider& service) CXX11_OVERRIDE
165         {
166                 OnServiceChange(service);
167         }
168
169         void OnServiceDel(ServiceProvider& service) CXX11_OVERRIDE
170         {
171                 OnServiceChange(service);
172         }
173
174         void Prioritize() CXX11_OVERRIDE
175         {
176                 ServerInstance->Modules.SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
177         }
178
179         Version GetVersion() CXX11_OVERRIDE
180         {
181                 return Version("Provides the ADMIN, COMMANDS, INFO, MODULES, MOTD, TIME, SERVLIST, and VERSION commands", VF_VENDOR|VF_CORE);
182         }
183 };
184
185 MODULE_INIT(CoreModInfo)