]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/core_channel.cpp
Set the minimum length to 1 for most config items with a default.
[user/henk/code/inspircd.git] / src / coremods / core_channel / core_channel.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6  *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
7  *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2014-2015, 2018 Attila Molnar <attilamolnar@hush.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25 #include "core_channel.h"
26 #include "invite.h"
27 #include "listmode.h"
28
29 namespace
30 {
31 /** Hook that sends a MODE after a JOIN if the user in the JOIN has some modes prefix set.
32  * This happens e.g. when modules such as operprefix explicitly set prefix modes on the joining
33  * user, or when a member with prefix modes does a host cycle.
34  */
35 class JoinHook : public ClientProtocol::EventHook
36 {
37         ClientProtocol::Messages::Mode modemsg;
38         Modes::ChangeList modechangelist;
39         const User* joininguser;
40
41  public:
42         /** If true, MODE changes after JOIN will be sourced from the user, rather than the server
43          */
44         bool modefromuser;
45
46         JoinHook(Module* mod)
47                 : ClientProtocol::EventHook(mod, "JOIN")
48         {
49         }
50
51         void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE
52         {
53                 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
54                 const Membership& memb = *join.GetMember();
55
56                 modechangelist.clear();
57                 for (std::string::const_iterator i = memb.modes.begin(); i != memb.modes.end(); ++i)
58                 {
59                         PrefixMode* const pm = ServerInstance->Modes.FindPrefixMode(*i);
60                         if (!pm)
61                                 continue; // Shouldn't happen
62                         modechangelist.push_add(pm, memb.user->nick);
63                 }
64
65                 if (modechangelist.empty())
66                 {
67                         // Member got no modes on join
68                         joininguser = NULL;
69                         return;
70                 }
71
72                 joininguser = memb.user;
73
74                 // Prepare a mode protocol event that we can append to the message list in OnPreEventSend()
75                 modemsg.SetParams(memb.chan, NULL, modechangelist);
76                 if (modefromuser)
77                         modemsg.SetSource(join);
78                 else
79                         modemsg.SetSourceUser(ServerInstance->FakeClient);
80         }
81
82         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
83         {
84                 // If joininguser is NULL then they didn't get any modes on join, skip.
85                 // Also don't show their own modes to them, they get that in the NAMES list not via MODE.
86                 if ((joininguser) && (user != joininguser))
87                         messagelist.push_back(&modemsg);
88                 return MOD_RES_PASSTHRU;
89         }
90 };
91
92 }
93
94 class CoreModChannel : public Module, public CheckExemption::EventListener
95 {
96         Invite::APIImpl invapi;
97         CommandInvite cmdinvite;
98         CommandJoin cmdjoin;
99         CommandKick cmdkick;
100         CommandNames cmdnames;
101         CommandTopic cmdtopic;
102         Events::ModuleEventProvider evprov;
103         JoinHook joinhook;
104
105         ModeChannelBan banmode;
106         SimpleChannelModeHandler inviteonlymode;
107         ModeChannelKey keymode;
108         ModeChannelLimit limitmode;
109         SimpleChannelModeHandler moderatedmode;
110         SimpleChannelModeHandler noextmsgmode;
111         ModeChannelOp opmode;
112         SimpleChannelModeHandler privatemode;
113         SimpleChannelModeHandler secretmode;
114         SimpleChannelModeHandler topiclockmode;
115         ModeChannelVoice voicemode;
116
117         insp::flat_map<std::string, char> exemptions;
118
119         ModResult IsInvited(User* user, Channel* chan)
120         {
121                 LocalUser* localuser = IS_LOCAL(user);
122                 if ((localuser) && (invapi.IsInvited(localuser, chan)))
123                         return MOD_RES_ALLOW;
124                 return MOD_RES_PASSTHRU;
125         }
126
127  public:
128         CoreModChannel()
129                 : CheckExemption::EventListener(this, UINT_MAX)
130                 , invapi(this)
131                 , cmdinvite(this, invapi)
132                 , cmdjoin(this)
133                 , cmdkick(this)
134                 , cmdnames(this)
135                 , cmdtopic(this)
136                 , evprov(this, "event/channel")
137                 , joinhook(this)
138                 , banmode(this)
139                 , inviteonlymode(this, "inviteonly", 'i')
140                 , keymode(this)
141                 , limitmode(this)
142                 , moderatedmode(this, "moderated", 'm')
143                 , noextmsgmode(this, "noextmsg", 'n')
144                 , opmode(this)
145                 , privatemode(this, "private", 'p')
146                 , secretmode(this, "secret", 's')
147                 , topiclockmode(this, "topiclock", 't')
148                 , voicemode(this)
149         {
150         }
151
152         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
153         {
154                 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
155
156                 std::string current;
157                 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
158                 insp::flat_map<std::string, char> exempts;
159                 while (defaultstream.GetToken(current))
160                 {
161                         std::string::size_type pos = current.find(':');
162                         if (pos == std::string::npos || (pos + 2) > current.size())
163                                 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
164
165                         const std::string restriction = current.substr(0, pos);
166                         const char prefix = current[pos + 1];
167
168                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
169                         exempts[restriction] = prefix;
170                 }
171
172                 ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
173                 const std::string announceinvites = securitytag->getString("announceinvites", "dynamic", 1);
174                 Invite::AnnounceState newannouncestate;
175                 if (stdalgo::string::equalsci(announceinvites, "none"))
176                         newannouncestate = Invite::ANNOUNCE_NONE;
177                 else if (stdalgo::string::equalsci(announceinvites, "all"))
178                         newannouncestate = Invite::ANNOUNCE_ALL;
179                 else if (stdalgo::string::equalsci(announceinvites, "ops"))
180                         newannouncestate = Invite::ANNOUNCE_OPS;
181                 else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
182                         newannouncestate = Invite::ANNOUNCE_DYNAMIC;
183                 else
184                         throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());
185
186                 // Config is valid, apply it
187
188                 // Validates and applies <maxlist> tags, so do it first
189                 banmode.DoRehash();
190
191                 exemptions.swap(exempts);
192                 // In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour
193                 // and will be removed in the next major release.
194                 limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;;
195                 cmdinvite.announceinvites = newannouncestate;
196                 joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");
197
198                 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
199                 if (optionstag->getBool("invitebypassmodes", true))
200                         ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
201                 else
202                 {
203                         for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
204                                 ServerInstance->Modules.Detach(events[i], this);
205                 }
206         }
207
208         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
209         {
210                 tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
211
212                 insp::flat_map<int, std::string> limits;
213                 std::string vlist;
214                 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
215                 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
216                 {
217                         ListModeBase* lm = *iter;
218
219                         const unsigned int limit = lm->GetLowerLimit();
220                         limits[limit].push_back(lm->GetModeChar());
221
222                         if (lm->HasVariableLength())
223                                 vlist.push_back(lm->GetModeChar());
224                 }
225
226                 std::string& buffer = tokens["MAXLIST"];
227                 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
228                 {
229                         if (!buffer.empty())
230                                 buffer.push_back(',');
231
232                         std::string modes(iter->second);
233                         std::sort(modes.begin(), modes.end());
234
235                         buffer.append(modes);
236                         buffer.push_back(':');
237                         buffer.append(ConvToStr(iter->first));
238                 }
239
240                 if (!vlist.empty())
241                 {
242                         tokens["VBANLIST"]; // deprecated
243                         tokens["VLIST"] = vlist;
244                 }
245         }
246
247         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
248         {
249                 if (!chan)
250                         return MOD_RES_PASSTHRU;
251
252                 // Check whether the channel key is correct.
253                 const std::string ckey = chan->GetModeParameter(&keymode);
254                 if (!ckey.empty())
255                 {
256                         ModResult MOD_RESULT;
257                         FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
258                         if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
259                         {
260                                 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
261                                 user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (incorrect channel key)");
262                                 return MOD_RES_DENY;
263                         }
264                 }
265
266                 // Check whether the invite only mode is set.
267                 if (chan->IsModeSet(inviteonlymode))
268                 {
269                         ModResult MOD_RESULT;
270                         FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
271                         if (MOD_RESULT != MOD_RES_ALLOW)
272                         {
273                                 user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (invite only)");
274                                 return MOD_RES_DENY;
275                         }
276                 }
277
278                 // Check whether the limit would be exceeded by this user joining.
279                 if (chan->IsModeSet(limitmode))
280                 {
281                         ModResult MOD_RESULT;
282                         FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
283                         if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
284                         {
285                                 user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (channel is full)");
286                                 return MOD_RES_DENY;
287                         }
288                 }
289
290                 // Everything looks okay.
291                 return MOD_RES_PASSTHRU;
292         }
293
294         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
295         {
296                 Channel* const chan = memb->chan;
297                 LocalUser* const localuser = IS_LOCAL(memb->user);
298                 if (localuser)
299                 {
300                         // Remove existing invite, if any
301                         invapi.Remove(localuser, chan);
302
303                         if (chan->topic.length())
304                                 Topic::ShowTopic(localuser, chan);
305
306                         // Show all members of the channel, including invisible (+i) users
307                         cmdnames.SendNames(localuser, chan, true);
308                 }
309         }
310
311         ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
312         {
313                 // Hook only runs when being invited bypasses +bkl
314                 return IsInvited(user, chan);
315         }
316
317         ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
318         {
319                 // Hook only runs when being invited bypasses +bkl
320                 return IsInvited(user, chan);
321         }
322
323         ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
324         {
325                 // Hook only runs when being invited bypasses +bkl
326                 return IsInvited(user, chan);
327         }
328
329         ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
330         {
331                 // Hook always runs
332                 return IsInvited(user, chan);
333         }
334
335         void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
336         {
337                 invapi.RemoveAll(user);
338         }
339
340         void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
341         {
342                 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
343                 invapi.RemoveAll(chan);
344         }
345
346         ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
347         {
348                 if (!exemptions.count(restriction))
349                         return MOD_RES_PASSTHRU;
350
351                 unsigned int mypfx = chan->GetPrefixValue(user);
352                 char minmode = exemptions[restriction];
353
354                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
355                 if (mh && mypfx >= mh->GetPrefixRank())
356                         return MOD_RES_ALLOW;
357                 if (mh || minmode == '*')
358                         return MOD_RES_DENY;
359                 return MOD_RES_PASSTHRU;
360         }
361
362         void Prioritize() CXX11_OVERRIDE
363         {
364                 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
365                 ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST);
366         }
367
368         Version GetVersion() CXX11_OVERRIDE
369         {
370                 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
371         }
372 };
373
374 MODULE_INIT(CoreModChannel)