]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Extract UID/SID generation logic into a new class: UIDGenerator
[user/henk/code/inspircd.git] / src / server.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include <signal.h>
24 #include "exitcodes.h"
25 #include "inspircd.h"
26
27 void InspIRCd::SignalHandler(int signal)
28 {
29 #ifdef _WIN32
30         if (signal == SIGTERM)
31 #else
32         if (signal == SIGHUP)
33         {
34                 Rehash("Caught SIGHUP");
35         }
36         else if (signal == SIGTERM)
37 #endif
38         {
39                 Exit(signal);
40         }
41 }
42
43 void InspIRCd::Exit(int status)
44 {
45 #ifdef _WIN32
46         SetServiceStopped(status);
47 #endif
48         if (this)
49         {
50                 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
51                 this->Cleanup();
52                 delete this;
53                 ServerInstance = NULL;
54         }
55         exit (status);
56 }
57
58 void RehashHandler::Call(const std::string &reason)
59 {
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)
63         {
64                 ServerInstance->ConfigThread = new ConfigReaderThread("");
65                 ServerInstance->Threads->Start(ServerInstance->ConfigThread);
66         }
67 }
68
69 std::string InspIRCd::GetVersionString(bool operstring)
70 {
71         char versiondata[MAXBUF];
72         if (operstring)
73         {
74                 std::string sename = SE->GetName();
75                 snprintf(versiondata,MAXBUF,"%s %s :%s [%s,%s,%s]",VERSION, Config->ServerName.c_str(), SYSTEM,REVISION, sename.c_str(), Config->sid.c_str());
76         }
77         else
78                 snprintf(versiondata,MAXBUF,"%s %s :%s",BRANCH,Config->ServerName.c_str(),Config->CustomVersion.c_str());
79         return versiondata;
80 }
81
82 const char InspIRCd::LogHeader[] =
83         "Log started for " VERSION " (" REVISION ", " MODULE_INIT_STR ")"
84         " - compiled on " SYSTEM;
85
86
87 std::string UIDGenerator::GenerateSID(const std::string& servername, const std::string& serverdesc)
88 {
89         unsigned int sid = 0;
90
91         for (std::string::const_iterator i = servername.begin(); i != servername.end(); ++i)
92                 sid = 5 * sid + *i;
93         for (std::string::const_iterator i = serverdesc.begin(); i != serverdesc.end(); ++i)
94                 sid = 5 * sid + *i;
95
96         std::string sidstr = ConvToStr(sid % 1000);
97         return sidstr;
98 }
99
100 void UIDGenerator::IncrementUID(unsigned int pos)
101 {
102         /*
103          * Okay. The rules for generating a UID go like this...
104          * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
105          * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
106          * A again, in an iterative fashion.. so..
107          * AAA9 -> AABA, and so on. -- w00t
108          */
109         if ((pos == 3) && (current_uid[3] == '9'))
110         {
111                 // At pos 3, if we hit '9', we've run out of available UIDs, and need to reset to AAA..AAA.
112                 for (int i = 3; i < UUID_LENGTH-1; i++)
113                 {
114                         current_uid[i] = 'A';
115                 }
116         }
117         else
118         {
119                 // If we hit Z, wrap around to 0.
120                 if (current_uid[pos] == 'Z')
121                 {
122                         current_uid[pos] = '0';
123                 }
124                 else if (current_uid[pos] == '9')
125                 {
126                         /*
127                          * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
128                          * e.g. A9 -> BA -> BB ..
129                          */
130                         current_uid[pos] = 'A';
131                         this->IncrementUID(pos - 1);
132                 }
133                 else
134                 {
135                         // Anything else, nobody gives a shit. Just increment.
136                         current_uid[pos]++;
137                 }
138         }
139 }
140
141 void UIDGenerator::init(const std::string& sid)
142 {
143         /*
144          * Copy SID into the first three digits, 9's to the rest, null term at the end
145          * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
146          * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
147          * Kind of silly, but I like how it looks.
148          *              -- w
149          */
150
151         current_uid[0] = sid[0];
152         current_uid[1] = sid[1];
153         current_uid[2] = sid[2];
154
155         for (int i = 3; i < (UUID_LENGTH - 1); i++)
156                 current_uid[i] = '9';
157
158         // Null terminator. Important.
159         current_uid[UUID_LENGTH - 1] = '\0';
160 }
161
162 /*
163  * Retrieve the next valid UUID that is free for this server.
164  */
165 std::string UIDGenerator::GetUID()
166 {
167         while (1)
168         {
169                 // Add one to the last UID
170                 this->IncrementUID(UUID_LENGTH - 2);
171
172                 if (!ServerInstance->FindUUID(current_uid))
173                         break;
174
175                 /*
176                  * It's in use. We need to try the loop again.
177                  */
178         }
179
180         return current_uid;
181 }
182
183 void ISupportManager::Build()
184 {
185         /**
186          * This is currently the neatest way we can build the initial ISUPPORT map. In
187          * the future we can use an initializer list here.
188          */
189         std::map<std::string, std::string> tokens;
190         std::vector<std::string> lines;
191         int token_count = 0;
192         std::string line;
193
194         tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
195         tokens["CASEMAPPING"] = "rfc1459";
196         tokens["CHANMODES"] = ConvToStr(ServerInstance->Modes->GiveModeList(MASK_CHANNEL));
197         tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
198         tokens["CHANTYPES"] = "#";
199         tokens["CHARSET"] = "ascii";
200         tokens["ELIST"] = "MU";
201         tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
202         tokens["MAXBANS"] = "64"; // TODO: make this a config setting.
203         tokens["MAXCHANNELS"] = ConvToStr(ServerInstance->Config->MaxChans);
204         tokens["MAXPARA"] = ConvToStr(MAXPARAMETERS);
205         tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
206         tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
207         tokens["NETWORK"] = ConvToStr(ServerInstance->Config->Network);
208         tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
209         tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
210         tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
211         tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
212
213         tokens["FNC"] = tokens["MAP"] = tokens["VBANLIST"] =
214                 tokens["WALLCHOPS"] = tokens["WALLVOICES"];
215
216         FOREACH_MOD(I_On005Numeric, On005Numeric(tokens));
217
218         // EXTBAN is a special case as we need to sort it and prepend a comma.
219         std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
220         if (extban != tokens.end())
221         {
222                 sort(extban->second.begin(), extban->second.end());
223                 extban->second.insert(0, ",");
224         }
225
226         for (std::map<std::string, std::string>::iterator it = tokens.begin(); it != tokens.end(); it++)
227         {
228                 line.append(it->first + (it->second.empty() ? " " : "=" + it->second + " "));
229                 token_count++;
230
231                 if (token_count % 13 == 12 || it == --tokens.end())
232                 {
233                         line.append(":are supported by this server");
234                         lines.push_back(line);
235                         line.clear();
236                 }
237         }
238
239         this->Lines = lines;
240 }
241
242 void ISupportManager::SendTo(LocalUser* user)
243 {
244         for (std::vector<std::string>::iterator line = this->Lines.begin(); line != this->Lines.end(); line++)
245                 user->WriteNumeric(RPL_ISUPPORT, "%s %s", user->nick.c_str(), line->c_str());
246 }