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);
49 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
51 ServerInstance = NULL;
56 void InspIRCd::Rehash(const std::string& uuid)
58 if (!ServerInstance->ConfigThread)
60 ServerInstance->ConfigThread = new ConfigReaderThread(uuid);
61 ServerInstance->Threads.Start(ServerInstance->ConfigThread);
65 std::string InspIRCd::GetVersionString(bool getFullVersion)
68 return INSPIRCD_VERSION " " + Config->ServerName + " :" INSPIRCD_SYSTEM " [" INSPIRCD_REVISION "," INSPIRCD_SOCKETENGINE_NAME "," + Config->sid + "]";
69 return INSPIRCD_BRANCH " " + Config->ServerName + " :" + Config->CustomVersion;
72 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
76 for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
78 for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
81 std::string sidstr = ConvToStr(sid % 1000);
82 sidstr.insert(0, 3 - sidstr.length(), '0');
86 void UIDGenerator::IncrementUID(unsigned int pos)
89 * Okay. The rules for generating a UID go like this...
90 * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
91 * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
92 * A again, in an iterative fashion.. so..
93 * AAA9 -> AABA, and so on. -- w00t
96 // If we hit Z, wrap around to 0.
97 if (current_uid[pos] == 'Z')
99 current_uid[pos] = '0';
101 else if (current_uid[pos] == '9')
104 * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
105 * e.g. A9 -> BA -> BB ..
107 current_uid[pos] = 'A';
110 // At pos 3, if we hit '9', we've run out of available UIDs, and reset to AAA..AAA.
113 this->IncrementUID(pos - 1);
117 // Anything else, nobody gives a shit. Just increment.
122 void UIDGenerator::init(const std::string& sid)
125 * Copy SID into the first three digits, 9's to the rest, null term at the end
126 * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
127 * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
128 * Kind of silly, but I like how it looks.
132 current_uid.resize(UUID_LENGTH, '9');
133 current_uid[0] = sid[0];
134 current_uid[1] = sid[1];
135 current_uid[2] = sid[2];
139 * Retrieve the next valid UUID that is free for this server.
141 std::string UIDGenerator::GetUID()
145 // Add one to the last UID
146 this->IncrementUID(UUID_LENGTH - 1);
148 if (!ServerInstance->FindUUID(current_uid))
152 * It's in use. We need to try the loop again.
159 void ISupportManager::Build()
162 * This is currently the neatest way we can build the initial ISUPPORT map. In
163 * the future we can use an initializer list here.
165 std::map<std::string, std::string> tokens;
167 tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
168 tokens["CASEMAPPING"] = "rfc1459";
169 tokens["CHANMODES"] = ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL);
170 tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
171 tokens["CHANTYPES"] = "#";
172 tokens["ELIST"] = "MU";
173 tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
174 tokens["MAXBANS"] = "64"; // TODO: make this a config setting.
175 tokens["MAXCHANNELS"] = ConvToStr(ServerInstance->Config->MaxChans);
176 tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
177 tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
178 tokens["NETWORK"] = ServerInstance->Config->Network;
179 tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
180 tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
181 tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
182 tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
184 tokens["FNC"] = tokens["VBANLIST"];
186 // Modules can add new tokens and also edit or remove existing tokens
187 FOREACH_MOD(On005Numeric, (tokens));
189 // EXTBAN is a special case as we need to sort it and prepend a comma.
190 std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
191 if (extban != tokens.end())
193 std::sort(extban->second.begin(), extban->second.end());
194 extban->second.insert(0, ",");
197 // Transform the map into a list of lines, ready to be sent to clients
199 unsigned int token_count = 0;
202 for (std::map<std::string, std::string>::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
204 line.append(it->first);
206 // If this token has a value then append a '=' char after the name and then the value itself
207 if (!it->second.empty())
208 line.append(1, '=').append(it->second);
210 // Always append a space, even if it's the last token because all lines will be suffixed
214 if (token_count % 13 == 12 || it == --tokens.end())
216 // Reached maximum number of tokens for this line or the current token
217 // is the last one; finalize the line and store it for later use
218 line.append(":are supported by this server");
219 cachedlines.push_back(line);
225 void ISupportManager::SendTo(LocalUser* user)
227 for (std::vector<std::string>::const_iterator i = cachedlines.begin(); i != cachedlines.end(); ++i)
228 user->WriteNumeric(RPL_ISUPPORT, *i);