2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "builtinmodes.h"
25 ModeUserServerNoticeMask::ModeUserServerNoticeMask() : ModeHandler(NULL, "snomask", 's', PARAM_SETONLY, MODETYPE_USER)
30 ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string ¶meter, bool adding)
34 dest->SetMode(this, true);
35 // Process the parameter (remove chars we don't understand, remove redundant chars, etc.)
36 parameter = ProcessNoticeMasks(dest, parameter);
37 return MODEACTION_ALLOW;
41 if (dest->IsModeSet(this))
43 dest->SetMode(this, false);
44 dest->snomasks.reset();
45 return MODEACTION_ALLOW;
49 // Mode not set and trying to unset, deny
50 return MODEACTION_DENY;
53 std::string ModeUserServerNoticeMask::GetUserParameter(User* user)
56 if (!user->IsModeSet(this))
60 for (unsigned char n = 0; n < 64; n++)
62 if (user->snomasks[n])
63 ret.push_back(n + 'A');
68 void ModeUserServerNoticeMask::OnParameterMissing(User* user, User* dest, Channel* channel)
70 user->WriteNotice("*** The user mode +s requires a parameter (server notice mask). Please provide a parameter, e.g. '+s +*'.");
73 std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input)
76 std::bitset<64> curr = user->snomasks;
78 for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
89 for (size_t j = 0; j < 64; j++)
91 if (ServerInstance->SNO->IsSnomaskUsable(j+'A'))
96 // For local users check whether the given snomask is valid and enabled - IsSnomaskUsable() tests both.
97 // For remote users accept what we were told, unless the snomask char is not a letter.
100 if (!ServerInstance->SNO->IsSnomaskUsable(*i))
102 user->WriteNumeric(ERR_UNKNOWNSNOMASK, *i, "is unknown snomask char to me");
106 else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z'))))
109 size_t index = ((*i) - 'A');
110 curr[index] = adding;
115 std::string plus = "+";
116 std::string minus = "-";
118 // Apply changes and construct two strings consisting of the newly added and the removed snomask chars
119 for (size_t i = 0; i < 64; i++)
121 bool isset = curr[i];
122 if (user->snomasks[i] != isset)
124 user->snomasks[i] = isset;
125 std::string& appendhere = (isset ? plus : minus);
126 appendhere.push_back(i+'A');
130 // Create the final string that will be shown to the user and sent to servers
133 if (plus.length() > 1)
136 if (minus.length() > 1)
139 // Unset the snomask usermode itself if every snomask was unset
140 if (user->snomasks.none())
141 user->SetMode(this, false);