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