]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Restore prefix
[user/henk/code/inspircd.git] / src / server.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <signal.h>
15 #include "exitcodes.h"
16 #include "inspircd.h"
17 #include "inspircd_version.h"
18
19 void InspIRCd::SignalHandler(int signal)
20 {
21         if (signal == SIGHUP)
22         {
23                 Rehash("Caught SIGHUP");
24         }
25         else if (signal == SIGTERM)
26         {
27                 Exit(signal);
28         }
29 }
30
31 void InspIRCd::Exit(int status)
32 {
33 #ifdef WINDOWS
34         if (WindowsIPC)
35                 delete WindowsIPC;
36         SetServiceStopped(status);
37 #endif
38         if (this)
39         {
40                 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
41                 this->Cleanup();
42                 delete this;
43                 ServerInstance = NULL;
44         }
45         exit (status);
46 }
47
48 void RehashHandler::Call(const std::string &reason)
49 {
50         ServerInstance->SNO->WriteToSnoMask('a', "Rehashing config file %s %s",ServerConfig::CleanFilename(ServerInstance->ConfigFileName.c_str()), reason.c_str());
51         ServerInstance->RehashUsersAndChans();
52         FOREACH_MOD(I_OnGarbageCollect, OnGarbageCollect());
53         if (!ServerInstance->ConfigThread)
54         {
55                 ServerInstance->ConfigThread = new ConfigReaderThread("");
56                 ServerInstance->Threads->Start(ServerInstance->ConfigThread);
57         }
58 }
59
60 std::string InspIRCd::GetVersionString(bool operstring)
61 {
62         char versiondata[MAXBUF];
63         if (operstring)
64                 snprintf(versiondata,MAXBUF,"%s %s :%s [%s,%s,%s]",VERSION,Config->ServerName.c_str(),SYSTEM,REVISION,SE->GetName().c_str(),Config->sid.c_str());
65         else
66                 snprintf(versiondata,MAXBUF,"InspIRCd-2.0 %s :%s",Config->ServerName.c_str(),Config->CustomVersion.c_str());
67         return versiondata;
68 }
69
70 const char InspIRCd::LogHeader[] =
71         "Log started for " VERSION " (" REVISION ", " MODULE_INIT_STR ")"
72         " - compiled on " SYSTEM;
73
74 void InspIRCd::BuildISupport()
75 {
76         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
77         std::stringstream v;
78         v << "WALLCHOPS WALLVOICES MODES=" << Config->Limits.MaxModes - 1 << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << Config->MaxChans << " MAXBANS=60 VBANLIST NICKLEN=" << Config->Limits.NickMax - 1;
79         v << " CASEMAPPING=rfc1459 STATUSMSG=" << Modes->BuildPrefixes(false) << " CHARSET=ascii TOPICLEN=" << Config->Limits.MaxTopic - 1 << " KICKLEN=" << Config->Limits.MaxKick - 1 << " MAXTARGETS=" << Config->MaxTargets - 1;
80         v << " AWAYLEN=" << Config->Limits.MaxAway - 1 << " CHANMODES=" << this->Modes->GiveModeList(MASK_CHANNEL) << " FNC NETWORK=" << Config->Network << " MAXPARA=32 ELIST=MU";
81         Config->data005 = v.str();
82         FOREACH_MOD(I_On005Numeric,On005Numeric(Config->data005));
83         Config->Update005();
84 }
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)
96         {
97                 // At pos 3, if we hit '9', we've run out of available UIDs, and need to reset to AAA..AAA.
98                 if (current_uid[pos] == '9')
99                 {
100                         for (int i = 3; i < (UUID_LENGTH - 1); i++)
101                         {
102                                 current_uid[i] = 'A';
103                                 pos  = UUID_LENGTH - 1;
104                         }
105                 }
106                 else
107                 {
108                         // Buf if we haven't, just keep incrementing merrily.
109                         current_uid[pos]++;
110                 }
111         }
112         else
113         {
114                 // If we hit Z, wrap around to 0.
115                 if (current_uid[pos] == 'Z')
116                 {
117                         current_uid[pos] = '0';
118                 }
119                 else if (current_uid[pos] == '9')
120                 {
121                         /*
122                          * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
123                          * e.g. A9 -> BA -> BB ..
124                          */
125                         current_uid[pos] = 'A';
126                         this->IncrementUID(pos - 1);
127                 }
128                 else
129                 {
130                         // Anything else, nobody gives a shit. Just increment.
131                         current_uid[pos]++;
132                 }
133         }
134 }
135
136 /*
137  * Retrieve the next valid UUID that is free for this server.
138  */
139 std::string InspIRCd::GetUID()
140 {
141         static int curindex = -1;
142
143         /*
144          * If -1, we're setting up. 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         if (curindex == -1)
151         {
152                 current_uid[0] = Config->sid[0];
153                 current_uid[1] = Config->sid[1];
154                 current_uid[2] = Config->sid[2];
155
156                 for (int i = 3; i < (UUID_LENGTH - 1); i++)
157                         current_uid[i] = '9';
158
159                 curindex = UUID_LENGTH - 2; // look at the end of the string now kthx, ignore null
160
161                 // Null terminator. Important.
162                 current_uid[UUID_LENGTH - 1] = '\0';
163         }
164
165         while (1)
166         {
167                 // Add one to the last UID
168                 this->IncrementUID(curindex);
169
170                 if (this->FindUUID(current_uid))
171                 {
172                         /*
173                          * It's in use. We need to try the loop again.
174                          */
175                         continue;
176                 }
177
178                 return current_uid;
179         }
180
181         /* not reached. */
182         return "";
183 }
184
185
186