]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Read multiple GnuTLS records in one RawSocketRead operation
[user/henk/code/inspircd.git] / src / server.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 /* $Core */
15
16 #include <signal.h>
17 #include "exitcodes.h"
18 #include "inspircd.h"
19 #include "inspircd_version.h"
20
21
22 void InspIRCd::SignalHandler(int signal)
23 {
24         switch (signal)
25         {
26                 case SIGHUP:
27                         Rehash("due to SIGHUP");
28                         break;
29                 case SIGTERM:
30                         Exit(signal);
31                         break;
32         }
33 }
34
35 void InspIRCd::Exit(int status)
36 {
37 #ifdef WINDOWS
38         if (WindowsIPC)
39                 delete WindowsIPC;
40         SetServiceStopped(status);
41 #endif
42         if (this)
43         {
44                 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
45                 this->Cleanup();
46         }
47         exit (status);
48 }
49
50 void RehashHandler::Call(const std::string &reason)
51 {
52         Server->SNO->WriteToSnoMask('a', "Rehashing config file %s %s",ServerConfig::CleanFilename(Server->ConfigFileName), reason.c_str());
53         Server->RehashUsersAndChans();
54         FOREACH_MOD_I(Server, I_OnGarbageCollect, OnGarbageCollect());
55         if (!Server->ConfigThread)
56         {
57                 Server->ConfigThread = new ConfigReaderThread(Server, "");
58                 Server->Threads->Start(Server->ConfigThread);
59         }
60 }
61
62 void InspIRCd::RehashServer()
63 {
64         this->Rehash("");
65 }
66
67 std::string InspIRCd::GetVersionString()
68 {
69         char versiondata[MAXBUF];
70         if (*Config->CustomVersion)
71         {
72                 snprintf(versiondata,MAXBUF,"InspIRCd-1.2 %s :%s",Config->ServerName,Config->CustomVersion);
73         }
74         else
75         {
76                 snprintf(versiondata,MAXBUF,"InspIRCd-1.2 %s :%s (%s) [FLAGS=%s,%s,%s]",Config->ServerName,SYSTEM,VERSION,REVISION,SE->GetName().c_str(),Config->sid);
77         }
78         return versiondata;
79 }
80
81 void InspIRCd::BuildISupport()
82 {
83         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
84         std::stringstream v;
85         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;
86         v << " CASEMAPPING=rfc1459 STATUSMSG=@" << (this->Config->AllowHalfop ? "%" : "") << "+ CHARSET=ascii TOPICLEN=" << Config->Limits.MaxTopic - 1 << " KICKLEN=" << Config->Limits.MaxKick - 1 << " MAXTARGETS=" << Config->MaxTargets - 1;
87         v << " AWAYLEN=" << Config->Limits.MaxAway - 1 << " CHANMODES=" << this->Modes->GiveModeList(MASK_CHANNEL) << " FNC NETWORK=" << Config->Network << " MAXPARA=32 ELIST=MU";
88         Config->data005 = v.str();
89         FOREACH_MOD_I(this,I_On005Numeric,On005Numeric(Config->data005));
90         Config->Update005();
91 }
92
93 std::string InspIRCd::GetRevision()
94 {
95         return REVISION;
96 }
97
98 void InspIRCd::AddServerName(const std::string &servername)
99 {
100         servernamelist::iterator itr = servernames.begin();
101         for(; itr != servernames.end(); ++itr)
102                 if(**itr == servername)
103                         return;
104
105         std::string * ns = new std::string(servername);
106         servernames.push_back(ns);
107 }
108
109 const char* InspIRCd::FindServerNamePtr(const std::string &servername)
110 {
111         servernamelist::iterator itr = servernames.begin();
112         for(; itr != servernames.end(); ++itr)
113                 if(**itr == servername)
114                         return (*itr)->c_str();
115
116         servernames.push_back(new std::string(servername));
117         itr = --servernames.end();
118         return (*itr)->c_str();
119 }
120
121 bool InspIRCd::FindServerName(const std::string &servername)
122 {
123         servernamelist::iterator itr = servernames.begin();
124         for(; itr != servernames.end(); ++itr)
125                 if(**itr == servername)
126                         return true;
127         return false;
128 }
129
130 void InspIRCd::IncrementUID(int pos)
131 {
132         /*
133          * Okay. The rules for generating a UID go like this...
134          * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
135          * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
136          * A again, in an iterative fashion.. so..
137          * AAA9 -> AABA, and so on. -- w00t
138          */
139         if (pos == 3)
140         {
141                 // At pos 3, if we hit '9', we've run out of available UIDs, and need to reset to AAA..AAA.
142                 if (current_uid[pos] == '9')
143                 {
144                         for (int i = 3; i < UUID_LENGTH; i++)
145                         {
146                                 current_uid[i] = 'A';
147                                 pos  = UUID_LENGTH - 1;
148                         }
149                 }
150                 else
151                 {
152                         // Buf if we haven't, just keep incrementing merrily.
153                         current_uid[pos]++;
154                 }
155         }
156         else
157         {
158                 // If we hit Z, wrap around to 0.
159                 if (current_uid[pos] == 'Z')
160                 {
161                         current_uid[pos] = '0';
162                 }
163                 else if (current_uid[pos] == '9')
164                 {
165                         /*
166                          * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
167                          * e.g. A9 -> BA -> BB ..
168                          */
169                         current_uid[pos] = 'A';
170                         this->IncrementUID(pos - 1);
171                 }
172                 else
173                 {
174                         // Anything else, nobody gives a shit. Just increment.
175                         current_uid[pos]++;
176                 }
177         }
178 }
179
180 /*
181  * Retrieve the next valid UUID that is free for this server.
182  */
183 std::string InspIRCd::GetUID()
184 {
185         static int curindex = -1;
186
187         /*
188          * If -1, we're setting up. Copy SID into the first three digits, 9's to the rest, null term at the end
189          * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
190          * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
191          * Kind of silly, but I like how it looks.
192          *              -- w
193          */
194         if (curindex == -1)
195         {
196                 current_uid[0] = Config->sid[0];
197                 current_uid[1] = Config->sid[1];
198                 current_uid[2] = Config->sid[2];
199
200                 for (int i = 3; i < (UUID_LENGTH - 1); i++)
201                         current_uid[i] = '9';
202
203                 curindex = UUID_LENGTH - 2; // look at the end of the string now kthx, ignore null
204
205                 // Null terminator. Important.
206                 current_uid[UUID_LENGTH - 1] = '\0';
207         }
208
209         while (1)
210         {
211                 // Add one to the last UID
212                 this->IncrementUID(curindex);
213
214                 if (this->FindUUID(current_uid))
215                 {
216                         /*
217                          * It's in use. We need to try the loop again.
218                          */
219                         continue;
220                 }
221
222                 return current_uid;
223         }
224
225         /* not reached. */
226         return "";
227 }
228
229
230