]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / server.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 nia <nia@netbsd.org>
5  *   Copyright (C) 2013-2014, 2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2016-2017, 2020 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2013 Adam <Adam@anope.org>
8  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
12  *   Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
13  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
14  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
15  *
16  * This file is part of InspIRCd.  InspIRCd is free software: you can
17  * redistribute it and/or modify it under the terms of the GNU General Public
18  * License as published by the Free Software Foundation, version 2.
19  *
20  * This program is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  */
28
29
30 #include "inspircd.h"
31 #include "exitcodes.h"
32 #include <signal.h>
33
34 void InspIRCd::SignalHandler(int signal)
35 {
36 #ifdef _WIN32
37         if (signal == SIGTERM)
38 #else
39         if (signal == SIGHUP)
40         {
41                 ServerInstance->SNO->WriteGlobalSno('a', "Rehashing due to SIGHUP");
42                 Rehash();
43         }
44         else if (signal == SIGTERM)
45 #endif
46         {
47                 Exit(EXIT_STATUS_SIGTERM);
48         }
49 }
50
51 void InspIRCd::Exit(int status)
52 {
53 #ifdef _WIN32
54         SetServiceStopped(status);
55 #endif
56         this->Cleanup();
57         ServerInstance = NULL;
58         delete this;
59         exit (status);
60 }
61
62 void InspIRCd::Rehash(const std::string& uuid)
63 {
64         if (!ServerInstance->ConfigThread)
65         {
66                 ServerInstance->ConfigThread = new ConfigReaderThread(uuid);
67                 ServerInstance->Threads.Start(ServerInstance->ConfigThread);
68         }
69 }
70
71 std::string InspIRCd::GetVersionString(bool getFullVersion)
72 {
73         if (getFullVersion)
74                 return INSPIRCD_VERSION ". " + Config->ServerName + " :[" + Config->sid + "] " + Config->CustomVersion;
75         return INSPIRCD_BRANCH ". " + Config->GetServerName() + " :" + Config->CustomVersion;
76 }
77
78 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
79 {
80         unsigned int sid = 0;
81
82         for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
83                 sid = 5 * sid + *i;
84         for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
85                 sid = 5 * sid + *i;
86
87         std::string sidstr = ConvToStr(sid % 1000);
88         sidstr.insert(0, 3 - sidstr.length(), '0');
89         return sidstr;
90 }
91
92 void UIDGenerator::IncrementUID(unsigned int pos)
93 {
94         /*
95          * Okay. The rules for generating a UID go like this...
96          * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
97          * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
98          * A again, in an iterative fashion.. so..
99          * AAA9 -> AABA, and so on. -- w00t
100          */
101
102         // If we hit Z, wrap around to 0.
103         if (current_uid[pos] == 'Z')
104         {
105                 current_uid[pos] = '0';
106         }
107         else if (current_uid[pos] == '9')
108         {
109                 /*
110                  * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
111                  * e.g. A9 -> BA -> BB ..
112                  */
113                 current_uid[pos] = 'A';
114                 if (pos == 3)
115                 {
116                         // At pos 3, if we hit '9', we've run out of available UIDs, and reset to AAA..AAA.
117                         return;
118                 }
119                 this->IncrementUID(pos - 1);
120         }
121         else
122         {
123                 // Anything else, nobody gives a shit. Just increment.
124                 current_uid[pos]++;
125         }
126 }
127
128 void UIDGenerator::init(const std::string& sid)
129 {
130         /*
131          * Copy SID into the first three digits, 9's to the rest, null term at the end
132          * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
133          * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
134          * Kind of silly, but I like how it looks.
135          *              -- w
136          */
137
138         current_uid.resize(UUID_LENGTH, '9');
139         current_uid[0] = sid[0];
140         current_uid[1] = sid[1];
141         current_uid[2] = sid[2];
142 }
143
144 /*
145  * Retrieve the next valid UUID that is free for this server.
146  */
147 std::string UIDGenerator::GetUID()
148 {
149         while (1)
150         {
151                 // Add one to the last UID
152                 this->IncrementUID(UUID_LENGTH - 1);
153
154                 if (!ServerInstance->FindUUID(current_uid))
155                         break;
156
157                 /*
158                  * It's in use. We need to try the loop again.
159                  */
160         }
161
162         return current_uid;
163 }
164
165 void ISupportManager::AppendValue(std::string& buffer, const std::string& value)
166 {
167         // If this token has no value then we have nothing to do.
168         if (value.empty())
169                 return;
170
171         // This function implements value escaping according to the rules of the ISUPPORT draft:
172         // https://tools.ietf.org/html/draft-brocklesby-irc-isupport-03
173         buffer.push_back('=');
174         for (std::string::const_iterator iter = value.begin(); iter != value.end(); ++iter)
175         {
176                 // The value must be escaped if:
177                 //   (1) It is a banned character in an IRC <middle> parameter (NUL, LF, CR, SPACE).
178                 //   (2) It has special meaning within an ISUPPORT token (EQUALS, BACKSLASH).
179                 if (*iter == '\0' || *iter == '\n' || *iter == '\r' || *iter == ' ' || *iter == '=' || *iter == '\\')
180                         buffer.append(InspIRCd::Format("\\x%X", *iter));
181                 else
182                         buffer.push_back(*iter);
183         }
184 }
185
186 void ISupportManager::Build()
187 {
188         /**
189          * This is currently the neatest way we can build the initial ISUPPORT map. In
190          * the future we can use an initializer list here.
191          */
192         std::map<std::string, std::string> tokens;
193
194         tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
195         tokens["CASEMAPPING"] = ServerInstance->Config->CaseMapping;
196         tokens["CHANLIMIT"] = InspIRCd::Format("#:%u", ServerInstance->Config->MaxChans);
197         tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
198         tokens["CHANTYPES"] = "#";
199         tokens["HOSTLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxHost);
200         tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
201         tokens["LINELEN"] = ConvToStr(ServerInstance->Config->Limits.MaxLine);
202         tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
203         tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
204         tokens["NAMELEN"] = ConvToStr(ServerInstance->Config->Limits.MaxReal);
205         tokens["NETWORK"] = ServerInstance->Config->Network;
206         tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
207         tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
208         tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
209         tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
210         tokens["USERLEN"] = ConvToStr(ServerInstance->Config->Limits.IdentMax);
211
212         // Modules can add new tokens and also edit or remove existing tokens
213         FOREACH_MOD(On005Numeric, (tokens));
214
215         // EXTBAN is a special case as we need to sort it and prepend a comma.
216         std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
217         if (extban != tokens.end())
218         {
219                 std::sort(extban->second.begin(), extban->second.end());
220                 extban->second.insert(0, ",");
221         }
222
223         // Transform the map into a list of lines, ready to be sent to clients
224         Numeric::Numeric numeric(RPL_ISUPPORT);
225         unsigned int token_count = 0;
226         cachedlines.clear();
227
228         for (std::map<std::string, std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
229         {
230                 numeric.push(it->first);
231                 std::string& token = numeric.GetParams().back();
232                 AppendValue(token, it->second);
233
234                 token_count++;
235
236                 if (token_count % 13 == 12 || it == --tokens.end())
237                 {
238                         // Reached maximum number of tokens for this line or the current token
239                         // is the last one; finalize the line and store it for later use
240                         numeric.push("are supported by this server");
241                         cachedlines.push_back(numeric);
242                         numeric.GetParams().clear();
243                 }
244         }
245 }
246
247 void ISupportManager::SendTo(LocalUser* user)
248 {
249         for (std::vector<Numeric::Numeric>::const_iterator i = cachedlines.begin(); i != cachedlines.end(); ++i)
250                 user->WriteNumeric(*i);
251 }