]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modes/umode_s.cpp
08ee7f562486dc4190a9f6cf94867106402d8bf3
[user/henk/code/inspircd.git] / src / modes / umode_s.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "builtinmodes.h"
24
25 ModeUserServerNoticeMask::ModeUserServerNoticeMask() : ModeHandler(NULL, "snomask", 's', PARAM_SETONLY, MODETYPE_USER)
26 {
27         oper = true;
28 }
29
30 ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string &parameter, bool adding)
31 {
32         if (adding)
33         {
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;
38         }
39         else
40         {
41                 if (dest->IsModeSet(this))
42                 {
43                         dest->SetMode(this, false);
44                         dest->snomasks.reset();
45                         return MODEACTION_ALLOW;
46                 }
47         }
48
49         // Mode not set and trying to unset, deny
50         return MODEACTION_DENY;
51 }
52
53 std::string ModeUserServerNoticeMask::GetUserParameter(User* user)
54 {
55         std::string ret;
56         if (!user->IsModeSet(this))
57                 return ret;
58
59         ret.push_back('+');
60         for (unsigned char n = 0; n < 64; n++)
61         {
62                 if (user->snomasks[n])
63                         ret.push_back(n + 'A');
64         }
65         return ret;
66 }
67
68 void ModeUserServerNoticeMask::OnParameterMissing(User* user, User* dest, Channel* channel)
69 {
70         user->WriteNotice("*** The user mode +s requires a parameter (server notice mask). Please provide a parameter, e.g. '+s +*'.");
71 }
72
73 std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input)
74 {
75         bool adding = true;
76         std::bitset<64> curr = user->snomasks;
77
78         for (std::string::const_iterator i = input.begin(); i != input.end(); ++i)
79         {
80                 switch (*i)
81                 {
82                         case '+':
83                                 adding = true;
84                         break;
85                         case '-':
86                                 adding = false;
87                         break;
88                         case '*':
89                                 for (size_t j = 0; j < 64; j++)
90                                 {
91                                         if (ServerInstance->SNO->IsSnomaskUsable(j+'A'))
92                                                 curr[j] = adding;
93                                 }
94                         break;
95                         default:
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.
98                                 if (IS_LOCAL(user))
99                                 {
100                                         if (!ServerInstance->SNO->IsSnomaskUsable(*i))
101                                         {
102                                                 user->WriteNumeric(ERR_UNKNOWNSNOMASK, "%c :is unknown snomask char to me", *i);
103                                                 continue;
104                                         }
105                                 }
106                                 else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z'))))
107                                         continue;
108
109                                 size_t index = ((*i) - 'A');
110                                 curr[index] = adding;
111                         break;
112                 }
113         }
114
115         std::string plus = "+";
116         std::string minus = "-";
117
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++)
120         {
121                 bool isset = curr[i];
122                 if (user->snomasks[i] != isset)
123                 {
124                         user->snomasks[i] = isset;
125                         std::string& appendhere = (isset ? plus : minus);
126                         appendhere.push_back(i+'A');
127                 }
128         }
129
130         // Create the final string that will be shown to the user and sent to servers
131         // Form: "+ABc-de"
132         std::string output;
133         if (plus.length() > 1)
134                 output = plus;
135
136         if (minus.length() > 1)
137                 output += minus;
138
139         // Unset the snomask usermode itself if every snomask was unset
140         if (user->snomasks.none())
141                 user->SetMode(this, false);
142
143         return output;
144 }