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