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