]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
m_dnsbl: Use the blacklist name rather than the domain.
[user/henk/code/inspircd.git] / src / channels.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006, 2008 Oliver Lupton <oliverlupton@gmail.com>
7  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8  *   Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
9  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "listmode.h"
28
29 namespace
30 {
31         ChanModeReference ban(NULL, "ban");
32 }
33
34 Channel::Channel(const std::string &cname, time_t ts)
35         : name(cname), age(ts), topicset(0)
36 {
37         if (!ServerInstance->chanlist.insert(std::make_pair(cname, this)).second)
38                 throw CoreException("Cannot create duplicate channel " + cname);
39 }
40
41 void Channel::SetMode(ModeHandler* mh, bool on)
42 {
43         modes[mh->GetId()] = on;
44 }
45
46 void Channel::SetTopic(User* u, const std::string& ntopic, time_t topicts, const std::string* setter)
47 {
48         // Send a TOPIC message to the channel only if the new topic text differs
49         if (this->topic != ntopic)
50         {
51                 this->topic = ntopic;
52                 ClientProtocol::Messages::Topic topicmsg(u, this, this->topic);
53                 Write(ServerInstance->GetRFCEvents().topic, topicmsg);
54         }
55
56         // Always update setter and set time
57         if (!setter)
58                 setter = ServerInstance->Config->FullHostInTopic ? &u->GetFullHost() : &u->nick;
59         this->setby.assign(*setter, 0, ServerInstance->Config->Limits.GetMaxMask());
60         this->topicset = topicts;
61
62         FOREACH_MOD(OnPostTopicChange, (u, this, this->topic));
63 }
64
65 Membership* Channel::AddUser(User* user)
66 {
67         std::pair<MemberMap::iterator, bool> ret = userlist.insert(std::make_pair(user, insp::aligned_storage<Membership>()));
68         if (!ret.second)
69                 return NULL;
70
71         Membership* memb = new(ret.first->second) Membership(user, this);
72         return memb;
73 }
74
75 void Channel::DelUser(User* user)
76 {
77         MemberMap::iterator it = userlist.find(user);
78         if (it != userlist.end())
79                 DelUser(it);
80 }
81
82 void Channel::CheckDestroy()
83 {
84         if (!userlist.empty())
85                 return;
86
87         ModResult res;
88         FIRST_MOD_RESULT(OnChannelPreDelete, res, (this));
89         if (res == MOD_RES_DENY)
90                 return;
91
92         // If the channel isn't in chanlist then it is already in the cull list, don't add it again
93         chan_hash::iterator iter = ServerInstance->chanlist.find(this->name);
94         if ((iter == ServerInstance->chanlist.end()) || (iter->second != this))
95                 return;
96
97         FOREACH_MOD(OnChannelDelete, (this));
98         ServerInstance->chanlist.erase(iter);
99         ServerInstance->GlobalCulls.AddItem(this);
100 }
101
102 void Channel::DelUser(const MemberMap::iterator& membiter)
103 {
104         Membership* memb = membiter->second;
105         memb->cull();
106         memb->~Membership();
107         userlist.erase(membiter);
108
109         // If this channel became empty then it should be removed
110         CheckDestroy();
111 }
112
113 Membership* Channel::GetUser(User* user)
114 {
115         MemberMap::iterator i = userlist.find(user);
116         if (i == userlist.end())
117                 return NULL;
118         return i->second;
119 }
120
121 void Channel::SetDefaultModes()
122 {
123         ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "SetDefaultModes %s",
124                 ServerInstance->Config->DefaultModes.c_str());
125         irc::spacesepstream list(ServerInstance->Config->DefaultModes);
126         std::string modeseq;
127         std::string parameter;
128
129         list.GetToken(modeseq);
130
131         for (std::string::iterator n = modeseq.begin(); n != modeseq.end(); ++n)
132         {
133                 ModeHandler* mode = ServerInstance->Modes->FindMode(*n, MODETYPE_CHANNEL);
134                 if (mode)
135                 {
136                         if (mode->IsPrefixMode())
137                                 continue;
138
139                         if (mode->NeedsParam(true))
140                         {
141                                 list.GetToken(parameter);
142                                 // If the parameter begins with a ':' then it's invalid
143                                 if (parameter.c_str()[0] == ':')
144                                         continue;
145                         }
146                         else
147                                 parameter.clear();
148
149                         if ((mode->NeedsParam(true)) && (parameter.empty()))
150                                 continue;
151
152                         mode->OnModeChange(ServerInstance->FakeClient, ServerInstance->FakeClient, this, parameter, true);
153                 }
154         }
155 }
156
157 /*
158  * add a channel to a user, creating the record for it if needed and linking
159  * it to the user record
160  */
161 Channel* Channel::JoinUser(LocalUser* user, std::string cname, bool override, const std::string& key)
162 {
163         if (user->registered != REG_ALL)
164         {
165                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join unregistered user " + user->uuid + " to channel " + cname);
166                 return NULL;
167         }
168
169         /*
170          * We don't restrict the number of channels that remote users or users that are override-joining may be in.
171          * We restrict local users to <connect:maxchans> channels.
172          * We restrict local operators to <oper:maxchans> channels.
173          * This is a lot more logical than how it was formerly. -- w00t
174          */
175         if (!override)
176         {
177                 unsigned int maxchans = user->GetClass()->maxchans;
178                 if (user->IsOper())
179                 {
180                         unsigned int opermaxchans = ConvToInt(user->oper->getConfig("maxchans"));
181                         // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
182                         if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
183                                 opermaxchans = ServerInstance->Config->OperMaxChans;
184                         if (opermaxchans)
185                                 maxchans = opermaxchans;
186                 }
187                 if (user->chans.size() >= maxchans)
188                 {
189                         user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
190                         return NULL;
191                 }
192         }
193
194         // Crop channel name if it's too long
195         if (cname.length() > ServerInstance->Config->Limits.ChanMax)
196                 cname.resize(ServerInstance->Config->Limits.ChanMax);
197
198         Channel* chan = ServerInstance->FindChan(cname);
199         bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
200         std::string privs; // Prefix mode(letter)s to give to the joining user
201
202         if (!chan)
203         {
204                 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
205
206                 if (override == false)
207                 {
208                         // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
209                         ModResult MOD_RESULT;
210                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
211                         if (MOD_RESULT == MOD_RES_DENY)
212                                 return NULL; // A module wasn't happy with the join, abort
213                 }
214
215                 chan = new Channel(cname, ServerInstance->Time());
216                 // Set the default modes on the channel (<options:defaultmodes>)
217                 chan->SetDefaultModes();
218         }
219         else
220         {
221                 /* Already on the channel */
222                 if (chan->HasUser(user))
223                         return NULL;
224
225                 if (override == false)
226                 {
227                         ModResult MOD_RESULT;
228                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
229
230                         // A module explicitly denied the join and (hopefully) generated a message
231                         // describing the situation, so we may stop here without sending anything
232                         if (MOD_RESULT == MOD_RES_DENY)
233                                 return NULL;
234
235                         // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
236                         // most of the time) then proceed to check channel bans.
237                         //
238                         // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
239                         // then this entire section is skipped
240                         if (MOD_RESULT == MOD_RES_PASSTHRU)
241                         {
242                                 if (chan->IsBanned(user))
243                                 {
244                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You're banned)");
245                                         return NULL;
246                                 }
247                         }
248                 }
249         }
250
251         // We figured that this join is allowed and also created the
252         // channel if it didn't exist before, now do the actual join
253         chan->ForceJoin(user, &privs, false, created_by_local);
254         return chan;
255 }
256
257 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
258 {
259         if (IS_SERVER(user))
260         {
261                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
262                 return NULL;
263         }
264
265         Membership* memb = this->AddUser(user);
266         if (!memb)
267                 return NULL; // Already on the channel
268
269         user->chans.push_front(memb);
270
271         if (privs)
272         {
273                 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
274                 // remote user and his own server set the modes), then set them internally now
275                 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
276                 {
277                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
278                         if (mh)
279                         {
280                                 std::string nick = user->nick;
281                                 // Set the mode on the user
282                                 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
283                         }
284                 }
285         }
286
287         // Tell modules about this join, they have the chance now to populate except_list with users we won't send the JOIN (and possibly MODE) to
288         CUList except_list;
289         FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
290
291         ClientProtocol::Events::Join joinevent(memb);
292         this->Write(joinevent, 0, except_list);
293
294         FOREACH_MOD(OnPostJoin, (memb));
295         return memb;
296 }
297
298 bool Channel::IsBanned(User* user)
299 {
300         ModResult result;
301         FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
302
303         if (result != MOD_RES_PASSTHRU)
304                 return (result == MOD_RES_DENY);
305
306         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
307         if (!banlm)
308                 return false;
309
310         const ListModeBase::ModeList* bans = banlm->GetList(this);
311         if (bans)
312         {
313                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
314                 {
315                         if (CheckBan(user, it->mask))
316                                 return true;
317                 }
318         }
319         return false;
320 }
321
322 bool Channel::CheckBan(User* user, const std::string& mask)
323 {
324         ModResult result;
325         FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
326         if (result != MOD_RES_PASSTHRU)
327                 return (result == MOD_RES_DENY);
328
329         // extbans were handled above, if this is one it obviously didn't match
330         if ((mask.length() <= 2) || (mask[1] == ':'))
331                 return false;
332
333         std::string::size_type at = mask.find('@');
334         if (at == std::string::npos)
335                 return false;
336
337         const std::string nickIdent = user->nick + "!" + user->ident;
338         std::string prefix(mask, 0, at);
339         if (InspIRCd::Match(nickIdent, prefix, NULL))
340         {
341                 std::string suffix(mask, at + 1);
342                 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
343                         InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
344                         InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
345                         return true;
346         }
347         return false;
348 }
349
350 ModResult Channel::GetExtBanStatus(User *user, char type)
351 {
352         ModResult rv;
353         FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
354         if (rv != MOD_RES_PASSTHRU)
355                 return rv;
356
357         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
358         if (!banlm)
359                 return MOD_RES_PASSTHRU;
360
361         const ListModeBase::ModeList* bans = banlm->GetList(this);
362         if (bans)
363         {
364                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
365                 {
366                         if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
367                                 continue;
368
369                         if (CheckBan(user, it->mask.substr(2)))
370                                 return MOD_RES_DENY;
371                 }
372         }
373         return MOD_RES_PASSTHRU;
374 }
375
376 /* Channel::PartUser
377  * Remove a channel from a users record, remove the reference to the Membership object
378  * from the channel and destroy it.
379  */
380 bool Channel::PartUser(User* user, std::string& reason)
381 {
382         MemberMap::iterator membiter = userlist.find(user);
383
384         if (membiter == userlist.end())
385                 return false;
386
387         Membership* memb = membiter->second;
388         CUList except_list;
389         FOREACH_MOD(OnUserPart, (memb, reason, except_list));
390
391         ClientProtocol::Messages::Part partmsg(memb, reason);
392         Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
393
394         // Remove this channel from the user's chanlist
395         user->chans.erase(memb);
396         // Remove the Membership from this channel's userlist and destroy it
397         this->DelUser(membiter);
398
399         return true;
400 }
401
402 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
403 {
404         Membership* memb = victimiter->second;
405         CUList except_list;
406         FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
407
408         ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
409         Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
410
411         memb->user->chans.erase(memb);
412         this->DelUser(victimiter);
413 }
414
415 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
416 {
417         unsigned int minrank = 0;
418         if (status)
419         {
420                 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
421                 if (mh)
422                         minrank = mh->GetPrefixRank();
423         }
424         for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
425         {
426                 LocalUser* user = IS_LOCAL(i->first);
427                 if ((user) && (!except_list.count(user)))
428                 {
429                         /* User doesn't have the status we're after */
430                         if (minrank && i->second->getRank() < minrank)
431                                 continue;
432
433                         user->Send(protoev);
434                 }
435         }
436 }
437
438 const char* Channel::ChanModes(bool showkey)
439 {
440         static std::string scratch;
441         std::string sparam;
442
443         scratch.clear();
444
445         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
446         for(int n = 0; n < 64; n++)
447         {
448                 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
449                 if (mh && IsModeSet(mh))
450                 {
451                         scratch.push_back(n + 65);
452
453                         ParamModeBase* pm = mh->IsParameterMode();
454                         if (!pm)
455                                 continue;
456
457                         if (n == 'k' - 65 && !showkey)
458                         {
459                                 sparam += " <key>";
460                         }
461                         else
462                         {
463                                 sparam += ' ';
464                                 pm->GetParameter(this, sparam);
465                         }
466                 }
467         }
468
469         scratch += sparam;
470         return scratch.c_str();
471 }
472
473 void Channel::WriteNotice(const std::string& text)
474 {
475         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE);
476         Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
477 }
478
479 /* returns the status character for a given user on a channel, e.g. @ for op,
480  * % for halfop etc. If the user has several modes set, the highest mode
481  * the user has must be returned.
482  */
483 char Membership::GetPrefixChar() const
484 {
485         char pf = 0;
486         unsigned int bestrank = 0;
487
488         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
489         {
490                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
491                 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
492                 {
493                         bestrank = mh->GetPrefixRank();
494                         pf = mh->GetPrefix();
495                 }
496         }
497         return pf;
498 }
499
500 unsigned int Membership::getRank()
501 {
502         char mchar = modes.c_str()[0];
503         unsigned int rv = 0;
504         if (mchar)
505         {
506                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
507                 if (mh)
508                         rv = mh->GetPrefixRank();
509         }
510         return rv;
511 }
512
513 std::string Membership::GetAllPrefixChars() const
514 {
515         std::string ret;
516         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
517         {
518                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
519                 if (mh && mh->GetPrefix())
520                         ret.push_back(mh->GetPrefix());
521         }
522
523         return ret;
524 }
525
526 unsigned int Channel::GetPrefixValue(User* user)
527 {
528         MemberMap::iterator m = userlist.find(user);
529         if (m == userlist.end())
530                 return 0;
531         return m->second->getRank();
532 }
533
534 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
535 {
536         char prefix = delta_mh->GetModeChar();
537         for (unsigned int i = 0; i < modes.length(); i++)
538         {
539                 char mchar = modes[i];
540                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
541                 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
542                 {
543                         modes = modes.substr(0,i) +
544                                 (adding ? std::string(1, prefix) : "") +
545                                 modes.substr(mchar == prefix ? i+1 : i);
546                         return adding != (mchar == prefix);
547                 }
548         }
549         if (adding)
550                 modes.push_back(prefix);
551         return adding;
552 }
553
554
555 void Membership::WriteNotice(const std::string& text) const
556 {
557         LocalUser* const localuser = IS_LOCAL(user);
558         if (!localuser)
559                 return;
560
561         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
562         localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
563 }