2 * InspIRCd -- Internet Relay Chat Daemon
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 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>
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.
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
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/>.
31 #include "exitcodes.h"
34 void InspIRCd::SignalHandler(int signal)
37 if (signal == SIGTERM)
41 ServerInstance->SNO->WriteGlobalSno('a', "Rehashing due to SIGHUP");
44 else if (signal == SIGTERM)
47 Exit(EXIT_STATUS_SIGTERM);
51 void InspIRCd::Exit(int status)
54 SetServiceStopped(status);
57 ServerInstance = NULL;
62 void InspIRCd::Rehash(const std::string& uuid)
64 if (!ServerInstance->ConfigThread)
66 ServerInstance->ConfigThread = new ConfigReaderThread(uuid);
67 ServerInstance->Threads.Start(ServerInstance->ConfigThread);
71 std::string InspIRCd::GetVersionString(bool getFullVersion)
74 return INSPIRCD_VERSION ". " + Config->ServerName + " :[" + Config->sid + "] " + Config->CustomVersion;
75 return INSPIRCD_BRANCH ". " + Config->ServerName + " :" + Config->CustomVersion;
78 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
82 for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
84 for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
87 std::string sidstr = ConvToStr(sid % 1000);
88 sidstr.insert(0, 3 - sidstr.length(), '0');
92 void UIDGenerator::IncrementUID(unsigned int pos)
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
102 // If we hit Z, wrap around to 0.
103 if (current_uid[pos] == 'Z')
105 current_uid[pos] = '0';
107 else if (current_uid[pos] == '9')
110 * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
111 * e.g. A9 -> BA -> BB ..
113 current_uid[pos] = 'A';
116 // At pos 3, if we hit '9', we've run out of available UIDs, and reset to AAA..AAA.
119 this->IncrementUID(pos - 1);
123 // Anything else, nobody gives a shit. Just increment.
128 void UIDGenerator::init(const std::string& sid)
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.
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];
145 * Retrieve the next valid UUID that is free for this server.
147 std::string UIDGenerator::GetUID()
151 // Add one to the last UID
152 this->IncrementUID(UUID_LENGTH - 1);
154 if (!ServerInstance->FindUUID(current_uid))
158 * It's in use. We need to try the loop again.
165 void ISupportManager::AppendValue(std::string& buffer, const std::string& value)
167 // If this token has no value then we have nothing to do.
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)
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));
182 buffer.push_back(*iter);
186 void ISupportManager::Build()
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.
192 std::map<std::string, std::string> tokens;
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["CHANMODES"] = ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL);
198 tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
199 tokens["CHANTYPES"] = "#";
200 tokens["HOSTLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxHost);
201 tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
202 tokens["LINELEN"] = ConvToStr(ServerInstance->Config->Limits.MaxLine);
203 tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
204 tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
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);
212 // Modules can add new tokens and also edit or remove existing tokens
213 FOREACH_MOD(On005Numeric, (tokens));
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())
219 std::sort(extban->second.begin(), extban->second.end());
220 extban->second.insert(0, ",");
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;
228 for (std::map<std::string, std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
230 numeric.push(it->first);
231 std::string& token = numeric.GetParams().back();
232 AppendValue(token, it->second);
236 if (token_count % 13 == 12 || it == --tokens.end())
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();
247 void ISupportManager::SendTo(LocalUser* user)
249 for (std::vector<Numeric::Numeric>::const_iterator i = cachedlines.begin(); i != cachedlines.end(); ++i)
250 user->WriteNumeric(*i);