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