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)
31 ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string ¶meter, bool adding)
35 dest->SetMode(this, true);
36 // Process the parameter (remove chars we don't understand, remove redundant chars, etc.)
37 parameter = ProcessNoticeMasks(dest, parameter);
38 return MODEACTION_ALLOW;
42 if (dest->IsModeSet(this))
44 dest->SetMode(this, false);
45 dest->snomasks.reset();
46 return MODEACTION_ALLOW;
50 // Mode not set and trying to unset, deny
51 return MODEACTION_DENY;
54 std::string ModeUserServerNoticeMask::GetUserParameter(const User* user) const
57 if (!user->IsModeSet(this))
61 for (unsigned char n = 0; n < 64; n++)
63 if (user->snomasks[n])
64 ret.push_back(n + 'A');
69 void ModeUserServerNoticeMask::OnParameterMissing(User* user, User* dest, Channel* channel)
71 user->WriteNotice("*** The user mode +s requires a parameter (server notice mask). Please provide a parameter, e.g. '+s +*'.");
74 std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input)
77 std::bitset<64> curr = user->snomasks;
79 for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
90 for (size_t j = 0; j < 64; j++)
92 if (ServerInstance->SNO->IsSnomaskUsable(j+'A'))
97 // For local users check whether the given snomask is valid and enabled - IsSnomaskUsable() tests both.
98 // For remote users accept what we were told, unless the snomask char is not a letter.
101 if (!ServerInstance->SNO->IsSnomaskUsable(*i))
103 user->WriteNumeric(ERR_UNKNOWNSNOMASK, *i, "is unknown snomask char to me");
107 else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z'))))
110 size_t index = ((*i) - 'A');
111 curr[index] = adding;
116 std::string plus = "+";
117 std::string minus = "-";
119 // Apply changes and construct two strings consisting of the newly added and the removed snomask chars
120 for (size_t i = 0; i < 64; i++)
122 bool isset = curr[i];
123 if (user->snomasks[i] != isset)
125 user->snomasks[i] = isset;
126 std::string& appendhere = (isset ? plus : minus);
127 appendhere.push_back(i+'A');
131 // Create the final string that will be shown to the user and sent to servers
134 if (plus.length() > 1)
137 if (minus.length() > 1)
140 // Unset the snomask usermode itself if every snomask was unset
141 if (user->snomasks.none())
142 user->SetMode(this, false);