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 Rehash("Caught SIGHUP");
36 else if (signal == SIGTERM)
43 void InspIRCd::Exit(int status)
46 SetServiceStopped(status);
50 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
53 ServerInstance = NULL;
58 void RehashHandler::Call(const std::string &reason)
60 ServerInstance->SNO->WriteToSnoMask('a', "Rehashing config file %s %s",ServerConfig::CleanFilename(ServerInstance->ConfigFileName.c_str()), reason.c_str());
61 FOREACH_MOD(I_OnGarbageCollect, OnGarbageCollect());
62 if (!ServerInstance->ConfigThread)
64 ServerInstance->ConfigThread = new ConfigReaderThread("");
65 ServerInstance->Threads->Start(ServerInstance->ConfigThread);
69 std::string InspIRCd::GetVersionString(bool getFullVersion)
72 return VERSION " " + Config->ServerName + " :" SYSTEM " [" REVISION "," + SE->GetName() + "," + Config->sid + "]";
73 return BRANCH " " + Config->ServerName + " :" + Config->CustomVersion;
76 const char InspIRCd::LogHeader[] =
77 "Log started for " VERSION " (" REVISION ", " MODULE_INIT_STR ")"
78 " - compiled on " SYSTEM;
81 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
85 for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
87 for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
90 std::string sidstr = ConvToStr(sid % 1000);
94 void UIDGenerator::IncrementUID(unsigned int pos)
97 * Okay. The rules for generating a UID go like this...
98 * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
99 * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
100 * A again, in an iterative fashion.. so..
101 * AAA9 -> AABA, and so on. -- w00t
104 // If we hit Z, wrap around to 0.
105 if (current_uid[pos] == 'Z')
107 current_uid[pos] = '0';
109 else if (current_uid[pos] == '9')
112 * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
113 * e.g. A9 -> BA -> BB ..
115 current_uid[pos] = 'A';
118 // At pos 3, if we hit '9', we've run out of available UIDs, and reset to AAA..AAA.
121 this->IncrementUID(pos - 1);
125 // Anything else, nobody gives a shit. Just increment.
130 void UIDGenerator::init(const std::string& sid)
133 * Copy SID into the first three digits, 9's to the rest, null term at the end
134 * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
135 * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
136 * Kind of silly, but I like how it looks.
140 current_uid.resize(UUID_LENGTH, '9');
141 current_uid[0] = sid[0];
142 current_uid[1] = sid[1];
143 current_uid[2] = sid[2];
147 * Retrieve the next valid UUID that is free for this server.
149 std::string UIDGenerator::GetUID()
153 // Add one to the last UID
154 this->IncrementUID(UUID_LENGTH - 1);
156 if (!ServerInstance->FindUUID(current_uid))
160 * It's in use. We need to try the loop again.
167 void ISupportManager::Build()
170 * This is currently the neatest way we can build the initial ISUPPORT map. In
171 * the future we can use an initializer list here.
173 std::map<std::string, std::string> tokens;
174 std::vector<std::string> lines;
178 tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
179 tokens["CASEMAPPING"] = "rfc1459";
180 tokens["CHANMODES"] = ConvToStr(ServerInstance->Modes->GiveModeList(MASK_CHANNEL));
181 tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
182 tokens["CHANTYPES"] = "#";
183 tokens["CHARSET"] = "ascii";
184 tokens["ELIST"] = "MU";
185 tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
186 tokens["MAXBANS"] = "64"; // TODO: make this a config setting.
187 tokens["MAXCHANNELS"] = ConvToStr(ServerInstance->Config->MaxChans);
188 tokens["MAXPARA"] = ConvToStr(MAXPARAMETERS);
189 tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
190 tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
191 tokens["NETWORK"] = ConvToStr(ServerInstance->Config->Network);
192 tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
193 tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
194 tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
195 tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
197 tokens["FNC"] = tokens["MAP"] = tokens["VBANLIST"] =
198 tokens["WALLCHOPS"] = tokens["WALLVOICES"];
200 FOREACH_MOD(I_On005Numeric, On005Numeric(tokens));
202 // EXTBAN is a special case as we need to sort it and prepend a comma.
203 std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
204 if (extban != tokens.end())
206 sort(extban->second.begin(), extban->second.end());
207 extban->second.insert(0, ",");
210 for (std::map<std::string, std::string>::iterator it = tokens.begin(); it != tokens.end(); it++)
212 line.append(it->first + (it->second.empty() ? " " : "=" + it->second + " "));
215 if (token_count % 13 == 12 || it == --tokens.end())
217 line.append(":are supported by this server");
218 lines.push_back(line);
226 void ISupportManager::SendTo(LocalUser* user)
228 for (std::vector<std::string>::iterator line = this->Lines.begin(); line != this->Lines.end(); line++)
229 user->WriteNumeric(RPL_ISUPPORT, "%s %s", user->nick.c_str(), line->c_str());