]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
m_spanningtree Remove unneeded #includes
[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 void InspIRCd::IncrementUID(int pos)
87 {
88         /*
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
94          */
95         if ((pos == 3) && (current_uid[3] == '9'))
96         {
97                 // At pos 3, if we hit '9', we've run out of available UIDs, and need to reset to AAA..AAA.
98                 for (int i = 3; i < UUID_LENGTH-1; i++)
99                 {
100                         current_uid[i] = 'A';
101                 }
102         }
103         else
104         {
105                 // If we hit Z, wrap around to 0.
106                 if (current_uid[pos] == 'Z')
107                 {
108                         current_uid[pos] = '0';
109                 }
110                 else if (current_uid[pos] == '9')
111                 {
112                         /*
113                          * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
114                          * e.g. A9 -> BA -> BB ..
115                          */
116                         current_uid[pos] = 'A';
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
127 /*
128  * Retrieve the next valid UUID that is free for this server.
129  */
130 std::string InspIRCd::GetUID()
131 {
132         static bool inited = false;
133
134         /*
135          * If we're setting up, copy SID into the first three digits, 9's to the rest, null term at the end
136          * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
137          * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
138          * Kind of silly, but I like how it looks.
139          *              -- w
140          */
141         if (!inited)
142         {
143                 inited = true;
144                 current_uid[0] = Config->sid[0];
145                 current_uid[1] = Config->sid[1];
146                 current_uid[2] = Config->sid[2];
147
148                 for (int i = 3; i < (UUID_LENGTH - 1); i++)
149                         current_uid[i] = '9';
150
151                 // Null terminator. Important.
152                 current_uid[UUID_LENGTH - 1] = '\0';
153         }
154
155         while (1)
156         {
157                 // Add one to the last UID
158                 this->IncrementUID(UUID_LENGTH - 2);
159
160                 if (this->FindUUID(current_uid))
161                 {
162                         /*
163                          * It's in use. We need to try the loop again.
164                          */
165                         continue;
166                 }
167
168                 return current_uid;
169         }
170
171         /* not reached. */
172         return "";
173 }
174
175 void ISupportManager::Build()
176 {
177         /**
178          * This is currently the neatest way we can build the initial ISUPPORT map. In
179          * the future we can use an initializer list here.
180          */
181         std::map<std::string, std::string> tokens;
182         std::vector<std::string> lines;
183         int token_count = 0;
184         std::string line;
185
186         tokens["AWAYLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxAway);
187         tokens["CASEMAPPING"] = "rfc1459";
188         tokens["CHANMODES"] = ConvToStr(ServerInstance->Modes->GiveModeList(MASK_CHANNEL));
189         tokens["CHANNELLEN"] = ConvToStr(ServerInstance->Config->Limits.ChanMax);
190         tokens["CHANTYPES"] = "#";
191         tokens["CHARSET"] = "ascii";
192         tokens["ELIST"] = "MU";
193         tokens["KICKLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxKick);
194         tokens["MAXBANS"] = "64"; // TODO: make this a config setting.
195         tokens["MAXCHANNELS"] = ConvToStr(ServerInstance->Config->MaxChans);
196         tokens["MAXPARA"] = ConvToStr(MAXPARAMETERS);
197         tokens["MAXTARGETS"] = ConvToStr(ServerInstance->Config->MaxTargets);
198         tokens["MODES"] = ConvToStr(ServerInstance->Config->Limits.MaxModes);
199         tokens["NETWORK"] = ConvToStr(ServerInstance->Config->Network);
200         tokens["NICKLEN"] = ConvToStr(ServerInstance->Config->Limits.NickMax);
201         tokens["PREFIX"] = ServerInstance->Modes->BuildPrefixes();
202         tokens["STATUSMSG"] = ServerInstance->Modes->BuildPrefixes(false);
203         tokens["TOPICLEN"] = ConvToStr(ServerInstance->Config->Limits.MaxTopic);
204
205         tokens["FNC"] = tokens["MAP"] = tokens["VBANLIST"] =
206                 tokens["WALLCHOPS"] = tokens["WALLVOICES"];
207
208         FOREACH_MOD(I_On005Numeric, On005Numeric(tokens));
209
210         // EXTBAN is a special case as we need to sort it and prepend a comma.
211         std::map<std::string, std::string>::iterator extban = tokens.find("EXTBAN");
212         if (extban != tokens.end())
213         {
214                 sort(extban->second.begin(), extban->second.end());
215                 extban->second.insert(0, ",");
216         }
217
218         for (std::map<std::string, std::string>::iterator it = tokens.begin(); it != tokens.end(); it++)
219         {
220                 line.append(it->first + (it->second.empty() ? " " : "=" + it->second + " "));
221                 token_count++;
222
223                 if (token_count % 13 == 12 || it == --tokens.end())
224                 {
225                         line.append(":are supported by this server");
226                         lines.push_back(line);
227                         line.clear();
228                 }
229         }
230
231         this->Lines = lines;
232 }
233
234 void ISupportManager::SendTo(LocalUser* user)
235 {
236         for (std::vector<std::string>::iterator line = this->Lines.begin(); line != this->Lines.end(); line++)
237                 user->WriteNumeric(RPL_ISUPPORT, "%s %s", user->nick.c_str(), line->c_str());
238 }