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