2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "exitcodes.h"
27 void InspIRCd::SignalHandler(int signal)
30 if (signal == SIGTERM)
34 ServerInstance->SNO->WriteGlobalSno('a', "Rehashing due to SIGHUP");
37 else if (signal == SIGTERM)
40 Exit(EXIT_STATUS_SIGTERM);
44 void InspIRCd::Exit(int status)
47 SetServiceStopped(status);
50 ServerInstance = NULL;
55 void InspIRCd::Rehash(const std::string& uuid)
57 if (!ServerInstance->ConfigThread)
59 ServerInstance->ConfigThread = new ConfigReaderThread(uuid);
60 ServerInstance->Threads.Start(ServerInstance->ConfigThread);
64 std::string InspIRCd::GetVersionString(bool getFullVersion)
67 return INSPIRCD_VERSION ". " + Config->ServerName + " :[" + Config->sid + "] " + Config->CustomVersion;
68 return INSPIRCD_BRANCH ". " + Config->ServerName + " :" + Config->CustomVersion;
71 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
75 for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
77 for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
80 std::string sidstr = ConvToStr(sid % 1000);
81 sidstr.insert(0, 3 - sidstr.length(), '0');
85 void UIDGenerator::IncrementUID(unsigned int pos)
88 * Okay. The rules for generating a UID go like this...
89 * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
90 * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
91 * A again, in an iterative fashion.. so..
92 * AAA9 -> AABA, and so on. -- w00t
95 // If we hit Z, wrap around to 0.
96 if (current_uid[pos] == 'Z')
98 current_uid[pos] = '0';
100 else if (current_uid[pos] == '9')
103 * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
104 * e.g. A9 -> BA -> BB ..
106 current_uid[pos] = 'A';
109 // At pos 3, if we hit '9', we've run out of available UIDs, and reset to AAA..AAA.
112 this->IncrementUID(pos - 1);
116 // Anything else, nobody gives a shit. Just increment.
121 void UIDGenerator::init(const std::string& sid)
124 * Copy SID into the first three digits, 9's to the rest, null term at the end
125 * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
126 * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
127 * Kind of silly, but I like how it looks.
131 current_uid.resize(UUID_LENGTH, '9');
132 current_uid[0] = sid[0];
133 current_uid[1] = sid[1];
134 current_uid[2] = sid[2];
138 * Retrieve the next valid UUID that is free for this server.
140 std::string UIDGenerator::GetUID()
144 // Add one to the last UID
145 this->IncrementUID(UUID_LENGTH - 1);
147 if (!ServerInstance->FindUUID(current_uid))
151 * It's in use. We need to try the loop again.
158 void ISupportManager::AppendValue(std::string& buffer, const std::string& value)
160 // If this token has no value then we have nothing to do.
164 // This function implements value escaping according to the rules of the ISUPPORT draft:
165 // https://tools.ietf.org/html/draft-brocklesby-irc-isupport-03
166 buffer.push_back('=');
167 for (std::string::const_iterator iter = value.begin(); iter != value.end(); ++iter)
169 // The value must be escaped if:
170 // (1) It is a banned character in an IRC <middle> parameter (NUL, LF, CR, SPACE).
171 // (2) It has special meaning within an ISUPPORT token (EQUALS, BACKSLASH).
172 if (*iter == '\0' || *iter == '\n' || *iter == '\r' || *iter == ' ' || *iter == '=' || *iter == '\\')
173 buffer.append(InspIRCd::Format("\\x%X", *iter));
175 buffer.push_back(*iter);
179 void ISupportManager::Build()
182 * This is currently the neatest way we can build the initial ISUPPORT map. In
183 * the future we can use an initializer list here.
185 std::map<std::string, std::string> tokens;
187 tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
188 tokens["CASEMAPPING"] = ServerInstance->Config->CaseMapping;
189 tokens["CHANLIMIT"] = InspIRCd::Format("#:%u", ServerInstance->Config->MaxChans);
190 tokens["CHANMODES"] = ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL);
191 tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
192 tokens["CHANTYPES"] = "#";
193 tokens["HOSTLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxHost);
194 tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
195 tokens["LINELEN"] = ConvToStr(ServerInstance->Config->Limits.MaxLine);
196 tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
197 tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
198 tokens["NETWORK"] = ServerInstance->Config->Network;
199 tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
200 tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
201 tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
202 tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
203 tokens["USERLEN"] = ConvToStr(ServerInstance->Config->Limits.IdentMax);
206 // Modules can add new tokens and also edit or remove existing tokens
207 FOREACH_MOD(On005Numeric, (tokens));
209 // EXTBAN is a special case as we need to sort it and prepend a comma.
210 std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
211 if (extban != tokens.end())
213 std::sort(extban->second.begin(), extban->second.end());
214 extban->second.insert(0, ",");
217 // Transform the map into a list of lines, ready to be sent to clients
218 Numeric::Numeric numeric(RPL_ISUPPORT);
219 unsigned int token_count = 0;
222 for (std::map<std::string, std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
224 numeric.push(it->first);
225 std::string& token = numeric.GetParams().back();
226 AppendValue(token, it->second);
230 if (token_count % 13 == 12 || it == --tokens.end())
232 // Reached maximum number of tokens for this line or the current token
233 // is the last one; finalize the line and store it for later use
234 numeric.push("are supported by this server");
235 cachedlines.push_back(numeric);
236 numeric.GetParams().clear();
241 void ISupportManager::SendTo(LocalUser* user)
243 for (std::vector<Numeric::Numeric>::const_iterator i = cachedlines.begin(); i != cachedlines.end(); ++i)
244 user->WriteNumeric(*i);