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 "core_user.h"
25 ModeUserServerNoticeMask::ModeUserServerNoticeMask(Module* Creator)
26 : ModeHandler(Creator, "snomask", 's', PARAM_SETONLY, MODETYPE_USER)
29 syntax = "(+|-)<snomasks>";
32 ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string ¶meter, bool adding)
36 dest->SetMode(this, true);
37 // Process the parameter (remove chars we don't understand, remove redundant chars, etc.)
38 parameter = ProcessNoticeMasks(dest, parameter);
39 return MODEACTION_ALLOW;
43 if (dest->IsModeSet(this))
45 dest->SetMode(this, false);
46 dest->snomasks.reset();
47 return MODEACTION_ALLOW;
51 // Mode not set and trying to unset, deny
52 return MODEACTION_DENY;
55 std::string ModeUserServerNoticeMask::GetUserParameter(const User* user) const
58 if (!user->IsModeSet(this))
62 for (unsigned char n = 0; n < 64; n++)
64 if (user->snomasks[n])
65 ret.push_back(n + 'A');
70 void ModeUserServerNoticeMask::OnParameterMissing(User* user, User* dest, Channel* channel)
72 user->WriteNotice("*** The user mode +s requires a parameter (server notice mask). Please provide a parameter, e.g. '+s +*'.");
75 std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input)
78 std::bitset<64> curr = user->snomasks;
80 for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
91 for (size_t j = 0; j < 64; j++)
93 if (ServerInstance->SNO->IsSnomaskUsable(j+'A'))
98 // For local users check whether the given snomask is valid and enabled - IsSnomaskUsable() tests both.
99 // For remote users accept what we were told, unless the snomask char is not a letter.
102 if (!ServerInstance->SNO->IsSnomaskUsable(*i))
104 user->WriteNumeric(ERR_UNKNOWNSNOMASK, *i, "is an unknown snomask character");
108 else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z'))))
111 size_t index = ((*i) - 'A');
112 curr[index] = adding;
117 std::string plus = "+";
118 std::string minus = "-";
120 // Apply changes and construct two strings consisting of the newly added and the removed snomask chars
121 for (size_t i = 0; i < 64; i++)
123 bool isset = curr[i];
124 if (user->snomasks[i] != isset)
126 user->snomasks[i] = isset;
127 std::string& appendhere = (isset ? plus : minus);
128 appendhere.push_back(i+'A');
132 // Create the final string that will be shown to the user and sent to servers
135 if (plus.length() > 1)
138 if (minus.length() > 1)
141 // Unset the snomask usermode itself if every snomask was unset
142 if (user->snomasks.none())
143 user->SetMode(this, false);