]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/channels.cpp
Textual improvements and fixes such as typos, casing, etc. (#1612)
[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 (user->IsOper())
180                 {
181                         unsigned int opermaxchans = ConvToNum<unsigned int>(user->oper->getConfig("maxchans"));
182                         // If not set, use 2.0's <channels:opers>, if that's not set either, use limit from CC
183                         if (!opermaxchans && user->HasPrivPermission("channels/high-join-limit"))
184                                 opermaxchans = ServerInstance->Config->OperMaxChans;
185                         if (opermaxchans)
186                                 maxchans = opermaxchans;
187                 }
188                 if (user->chans.size() >= maxchans)
189                 {
190                         user->WriteNumeric(ERR_TOOMANYCHANNELS, cname, "You are on too many channels");
191                         return NULL;
192                 }
193         }
194
195         // Crop channel name if it's too long
196         if (cname.length() > ServerInstance->Config->Limits.ChanMax)
197                 cname.resize(ServerInstance->Config->Limits.ChanMax);
198
199         Channel* chan = ServerInstance->FindChan(cname);
200         bool created_by_local = (chan == NULL); // Flag that will be passed to modules in the OnUserJoin() hook later
201         std::string privs; // Prefix mode(letter)s to give to the joining user
202
203         if (!chan)
204         {
205                 privs = ServerInstance->Config->DefaultModes.substr(0, ServerInstance->Config->DefaultModes.find(' '));
206
207                 if (override == false)
208                 {
209                         // Ask the modules whether they're ok with the join, pass NULL as Channel* as the channel is yet to be created
210                         ModResult MOD_RESULT;
211                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, NULL, cname, privs, key));
212                         if (MOD_RESULT == MOD_RES_DENY)
213                                 return NULL; // A module wasn't happy with the join, abort
214                 }
215
216                 chan = new Channel(cname, ServerInstance->Time());
217                 // Set the default modes on the channel (<options:defaultmodes>)
218                 chan->SetDefaultModes();
219         }
220         else
221         {
222                 /* Already on the channel */
223                 if (chan->HasUser(user))
224                         return NULL;
225
226                 if (override == false)
227                 {
228                         ModResult MOD_RESULT;
229                         FIRST_MOD_RESULT(OnUserPreJoin, MOD_RESULT, (user, chan, cname, privs, key));
230
231                         // A module explicitly denied the join and (hopefully) generated a message
232                         // describing the situation, so we may stop here without sending anything
233                         if (MOD_RESULT == MOD_RES_DENY)
234                                 return NULL;
235
236                         // If no module returned MOD_RES_DENY or MOD_RES_ALLOW (which is the case
237                         // most of the time) then proceed to check channel bans.
238                         //
239                         // If a module explicitly allowed the join (by returning MOD_RES_ALLOW),
240                         // then this entire section is skipped
241                         if (MOD_RESULT == MOD_RES_PASSTHRU)
242                         {
243                                 if (chan->IsBanned(user))
244                                 {
245                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
246                                         return NULL;
247                                 }
248                         }
249                 }
250         }
251
252         // We figured that this join is allowed and also created the
253         // channel if it didn't exist before, now do the actual join
254         chan->ForceJoin(user, &privs, false, created_by_local);
255         return chan;
256 }
257
258 Membership* Channel::ForceJoin(User* user, const std::string* privs, bool bursting, bool created_by_local)
259 {
260         if (IS_SERVER(user))
261         {
262                 ServerInstance->Logs->Log("CHANNELS", LOG_DEBUG, "Attempted to join server user " + user->uuid + " to channel " + this->name);
263                 return NULL;
264         }
265
266         Membership* memb = this->AddUser(user);
267         if (!memb)
268                 return NULL; // Already on the channel
269
270         user->chans.push_front(memb);
271
272         if (privs)
273         {
274                 // If the user was granted prefix modes (in the OnUserPreJoin hook, or he's a
275                 // remote user and his own server set the modes), then set them internally now
276                 for (std::string::const_iterator i = privs->begin(); i != privs->end(); ++i)
277                 {
278                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
279                         if (mh)
280                         {
281                                 std::string nick = user->nick;
282                                 // Set the mode on the user
283                                 mh->OnModeChange(ServerInstance->FakeClient, NULL, this, nick, true);
284                         }
285                 }
286         }
287
288         // 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
289         CUList except_list;
290         FOREACH_MOD(OnUserJoin, (memb, bursting, created_by_local, except_list));
291
292         ClientProtocol::Events::Join joinevent(memb);
293         this->Write(joinevent, 0, except_list);
294
295         FOREACH_MOD(OnPostJoin, (memb));
296         return memb;
297 }
298
299 bool Channel::IsBanned(User* user)
300 {
301         ModResult result;
302         FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, this));
303
304         if (result != MOD_RES_PASSTHRU)
305                 return (result == MOD_RES_DENY);
306
307         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
308         if (!banlm)
309                 return false;
310
311         const ListModeBase::ModeList* bans = banlm->GetList(this);
312         if (bans)
313         {
314                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); it++)
315                 {
316                         if (CheckBan(user, it->mask))
317                                 return true;
318                 }
319         }
320         return false;
321 }
322
323 bool Channel::CheckBan(User* user, const std::string& mask)
324 {
325         ModResult result;
326         FIRST_MOD_RESULT(OnCheckBan, result, (user, this, mask));
327         if (result != MOD_RES_PASSTHRU)
328                 return (result == MOD_RES_DENY);
329
330         // extbans were handled above, if this is one it obviously didn't match
331         if ((mask.length() <= 2) || (mask[1] == ':'))
332                 return false;
333
334         std::string::size_type at = mask.find('@');
335         if (at == std::string::npos)
336                 return false;
337
338         const std::string nickIdent = user->nick + "!" + user->ident;
339         std::string prefix(mask, 0, at);
340         if (InspIRCd::Match(nickIdent, prefix, NULL))
341         {
342                 std::string suffix(mask, at + 1);
343                 if (InspIRCd::Match(user->GetRealHost(), suffix, NULL) ||
344                         InspIRCd::Match(user->GetDisplayedHost(), suffix, NULL) ||
345                         InspIRCd::MatchCIDR(user->GetIPString(), suffix, NULL))
346                         return true;
347         }
348         return false;
349 }
350
351 ModResult Channel::GetExtBanStatus(User *user, char type)
352 {
353         ModResult rv;
354         FIRST_MOD_RESULT(OnExtBanCheck, rv, (user, this, type));
355         if (rv != MOD_RES_PASSTHRU)
356                 return rv;
357
358         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
359         if (!banlm)
360                 return MOD_RES_PASSTHRU;
361
362         const ListModeBase::ModeList* bans = banlm->GetList(this);
363         if (bans)
364         {
365                 for (ListModeBase::ModeList::const_iterator it = bans->begin(); it != bans->end(); ++it)
366                 {
367                         if (it->mask.length() <= 2 || it->mask[0] != type || it->mask[1] != ':')
368                                 continue;
369
370                         if (CheckBan(user, it->mask.substr(2)))
371                                 return MOD_RES_DENY;
372                 }
373         }
374         return MOD_RES_PASSTHRU;
375 }
376
377 /* Channel::PartUser
378  * Remove a channel from a users record, remove the reference to the Membership object
379  * from the channel and destroy it.
380  */
381 bool Channel::PartUser(User* user, std::string& reason)
382 {
383         MemberMap::iterator membiter = userlist.find(user);
384
385         if (membiter == userlist.end())
386                 return false;
387
388         Membership* memb = membiter->second;
389         CUList except_list;
390         FOREACH_MOD(OnUserPart, (memb, reason, except_list));
391
392         ClientProtocol::Messages::Part partmsg(memb, reason);
393         Write(ServerInstance->GetRFCEvents().part, partmsg, 0, except_list);
394
395         // Remove this channel from the user's chanlist
396         user->chans.erase(memb);
397         // Remove the Membership from this channel's userlist and destroy it
398         this->DelUser(membiter);
399
400         return true;
401 }
402
403 void Channel::KickUser(User* src, const MemberMap::iterator& victimiter, const std::string& reason)
404 {
405         Membership* memb = victimiter->second;
406         CUList except_list;
407         FOREACH_MOD(OnUserKick, (src, memb, reason, except_list));
408
409         ClientProtocol::Messages::Kick kickmsg(src, memb, reason);
410         Write(ServerInstance->GetRFCEvents().kick, kickmsg, 0, except_list);
411
412         memb->user->chans.erase(memb);
413         this->DelUser(victimiter);
414 }
415
416 void Channel::Write(ClientProtocol::Event& protoev, char status, const CUList& except_list)
417 {
418         unsigned int minrank = 0;
419         if (status)
420         {
421                 PrefixMode* mh = ServerInstance->Modes->FindPrefix(status);
422                 if (mh)
423                         minrank = mh->GetPrefixRank();
424         }
425         for (MemberMap::iterator i = userlist.begin(); i != userlist.end(); i++)
426         {
427                 LocalUser* user = IS_LOCAL(i->first);
428                 if ((user) && (!except_list.count(user)))
429                 {
430                         /* User doesn't have the status we're after */
431                         if (minrank && i->second->getRank() < minrank)
432                                 continue;
433
434                         user->Send(protoev);
435                 }
436         }
437 }
438
439 const char* Channel::ChanModes(bool showsecret)
440 {
441         static std::string scratch;
442         std::string sparam;
443
444         scratch.clear();
445
446         /* This was still iterating up to 190, Channel::modes is only 64 elements -- Om */
447         for(int n = 0; n < 64; n++)
448         {
449                 ModeHandler* mh = ServerInstance->Modes->FindMode(n + 65, MODETYPE_CHANNEL);
450                 if (mh && IsModeSet(mh))
451                 {
452                         scratch.push_back(n + 65);
453
454                         ParamModeBase* pm = mh->IsParameterMode();
455                         if (!pm)
456                                 continue;
457
458                         if (pm->IsParameterSecret() && !showsecret)
459                         {
460                                 sparam += " <" + pm->name + ">";
461                         }
462                         else
463                         {
464                                 sparam += ' ';
465                                 pm->GetParameter(this, sparam);
466                         }
467                 }
468         }
469
470         scratch += sparam;
471         return scratch.c_str();
472 }
473
474 void Channel::WriteNotice(const std::string& text)
475 {
476         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this, text, MSG_NOTICE);
477         Write(ServerInstance->GetRFCEvents().privmsg, privmsg);
478 }
479
480 /* returns the status character for a given user on a channel, e.g. @ for op,
481  * % for halfop etc. If the user has several modes set, the highest mode
482  * the user has must be returned.
483  */
484 char Membership::GetPrefixChar() const
485 {
486         char pf = 0;
487         unsigned int bestrank = 0;
488
489         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
490         {
491                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
492                 if (mh && mh->GetPrefixRank() > bestrank && mh->GetPrefix())
493                 {
494                         bestrank = mh->GetPrefixRank();
495                         pf = mh->GetPrefix();
496                 }
497         }
498         return pf;
499 }
500
501 unsigned int Membership::getRank()
502 {
503         char mchar = modes.c_str()[0];
504         unsigned int rv = 0;
505         if (mchar)
506         {
507                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
508                 if (mh)
509                         rv = mh->GetPrefixRank();
510         }
511         return rv;
512 }
513
514 std::string Membership::GetAllPrefixChars() const
515 {
516         std::string ret;
517         for (std::string::const_iterator i = modes.begin(); i != modes.end(); ++i)
518         {
519                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(*i);
520                 if (mh && mh->GetPrefix())
521                         ret.push_back(mh->GetPrefix());
522         }
523
524         return ret;
525 }
526
527 unsigned int Channel::GetPrefixValue(User* user)
528 {
529         MemberMap::iterator m = userlist.find(user);
530         if (m == userlist.end())
531                 return 0;
532         return m->second->getRank();
533 }
534
535 bool Membership::SetPrefix(PrefixMode* delta_mh, bool adding)
536 {
537         char prefix = delta_mh->GetModeChar();
538         for (unsigned int i = 0; i < modes.length(); i++)
539         {
540                 char mchar = modes[i];
541                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(mchar);
542                 if (mh && mh->GetPrefixRank() <= delta_mh->GetPrefixRank())
543                 {
544                         modes = modes.substr(0,i) +
545                                 (adding ? std::string(1, prefix) : "") +
546                                 modes.substr(mchar == prefix ? i+1 : i);
547                         return adding != (mchar == prefix);
548                 }
549         }
550         if (adding)
551                 modes.push_back(prefix);
552         return adding;
553 }
554
555
556 void Membership::WriteNotice(const std::string& text) const
557 {
558         LocalUser* const localuser = IS_LOCAL(user);
559         if (!localuser)
560                 return;
561
562         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, this->chan, text, MSG_NOTICE);
563         localuser->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
564 }