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