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