]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Unneeded
[user/henk/code/inspircd.git] / src / server.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDserver */
15
16 #include <signal.h>
17 #include "exitcodes.h"
18 #include "inspircd.h"
19
20
21 void InspIRCd::SignalHandler(int signal)
22 {
23         switch (signal)
24         {
25                 case SIGHUP:
26                         Rehash();
27                         break;
28                 case SIGTERM:
29                         Exit(signal);
30                         break;
31         }
32 }
33
34 void InspIRCd::Exit(int status)
35 {
36 #ifdef WINDOWS
37         delete WindowsIPC;
38 #endif
39         if (this)
40         {
41                 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
42                 this->Cleanup();
43         }
44         exit (status);
45 }
46
47 void InspIRCd::Rehash()
48 {
49         this->SNO->WriteToSnoMask('A', "Rehashing config file %s due to SIGHUP",ServerConfig::CleanFilename(this->ConfigFileName));
50         this->RehashUsersAndChans();
51         FOREACH_MOD_I(this, I_OnGarbageCollect, OnGarbageCollect());
52         if (!this->ConfigThread)
53         {
54                 Config->RehashUser = NULL;
55                 Config->RehashParameter = "";
56
57                 ConfigThread = new ConfigReaderThread(this, false, NULL);
58                 Threads->Create(ConfigThread);
59         }
60 }
61
62 void InspIRCd::RehashServer()
63 {
64         this->SNO->WriteToSnoMask('A', "Rehashing config file");
65         this->RehashUsersAndChans();
66         if (!this->ConfigThread)
67         {
68                 Config->RehashUser = NULL;
69                 Config->RehashParameter = "";
70
71                 ConfigThread = new ConfigReaderThread(this, false, NULL);
72                 Threads->Create(ConfigThread);
73         }
74 }
75
76 std::string InspIRCd::GetVersionString()
77 {
78         char versiondata[MAXBUF];
79         if (*Config->CustomVersion)
80         {
81                 snprintf(versiondata,MAXBUF,"%s %s :%s",VERSION,Config->ServerName,Config->CustomVersion);
82         }
83         else
84         {
85                 snprintf(versiondata,MAXBUF,"%s %s :%s [FLAGS=%s,%s,%s]",VERSION,Config->ServerName,SYSTEM,REVISION,SE->GetName().c_str(),Config->sid);
86         }
87         return versiondata;
88 }
89
90 void InspIRCd::BuildISupport()
91 {
92         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
93         std::stringstream v;
94         v << "WALLCHOPS WALLVOICES MODES=" << MAXMODES << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << Config->MaxChans << " MAXBANS=60 VBANLIST NICKLEN=" << NICKMAX-1;
95         v << " CASEMAPPING=rfc1459 STATUSMSG=@" << (this->Config->AllowHalfop ? "%" : "") << "+ CHARSET=ascii TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=" << Config->MaxTargets;
96         v << " AWAYLEN=" << MAXAWAY << " CHANMODES=" << this->Modes->ChanModes() << " FNC NETWORK=" << Config->Network << " MAXPARA=32 ELIST=MU";
97         Config->data005 = v.str();
98         FOREACH_MOD_I(this,I_On005Numeric,On005Numeric(Config->data005));
99         Config->Update005();
100 }
101
102 std::string InspIRCd::GetRevision()
103 {
104         return REVISION;
105 }
106
107 void InspIRCd::AddServerName(const std::string &servername)
108 {
109         servernamelist::iterator itr = servernames.begin();
110         for(; itr != servernames.end(); ++itr)
111                 if(**itr == servername)
112                         return;
113
114         std::string * ns = new std::string(servername);
115         servernames.push_back(ns);
116 }
117
118 const char* InspIRCd::FindServerNamePtr(const std::string &servername)
119 {
120         servernamelist::iterator itr = servernames.begin();
121         for(; itr != servernames.end(); ++itr)
122                 if(**itr == servername)
123                         return (*itr)->c_str();
124
125         servernames.push_back(new std::string(servername));
126         itr = --servernames.end();
127         return (*itr)->c_str();
128 }
129
130 bool InspIRCd::FindServerName(const std::string &servername)
131 {
132         servernamelist::iterator itr = servernames.begin();
133         for(; itr != servernames.end(); ++itr)
134                 if(**itr == servername)
135                         return true;
136         return false;
137 }
138
139 /*
140  * Retrieve the next valid UUID that is free for this server.
141  */
142 std::string InspIRCd::GetUID()
143 {
144         static int curindex = -1;
145
146         if (curindex == -1)
147         {
148                 // Starting up
149                 current_uid[0] = Config->sid[0];
150                 current_uid[1] = Config->sid[1];
151                 current_uid[2] = Config->sid[2];
152
153                 for (int i = 3; i < UUID_LENGTH; i++)
154                         current_uid[i] = 'Z';
155
156                 current_uid[3] = 'Y'; // force fake client to get ZZZZZZZZ
157
158                 curindex = 3;
159         }
160
161         while (1)
162         {
163                 printf("Getting ID. curindex %d, current_uid %s\n", curindex, current_uid);
164
165                 if (curindex == 3)
166                 {
167                         // Down to the last few.
168                         if (current_uid[curindex] == 'Z')
169                         {
170                                 // Reset.
171                                 for (int i = 3; i < UUID_LENGTH; i++)
172                                 {
173                                         current_uid[i] = 'A';
174                                         curindex  = UUID_LENGTH - 1;
175                                 }
176                         }
177                         else
178                                 current_uid[curindex]++;
179                 }
180                 else
181                 {
182                         if (current_uid[curindex] == 'Z')
183                                 current_uid[curindex] = '0';
184                         else if (current_uid[curindex] == '9')
185                         {
186                                 current_uid[curindex] = 'A';
187                                 curindex--;
188                                 continue;
189                         }
190                         else
191                                 current_uid[curindex]++;
192                 }
193
194                         if (this->FindUUID(current_uid))
195                         {
196                                 /*
197                                  * It's in use. We need to try the loop again.
198                                  */
199                                 continue;
200                         }
201
202                         return current_uid;
203         }
204
205 #if 0
206
207         /*
208          * This will only finish once we return a UUID that is not in use.
209          */
210         while (1)
211         {
212                 /*
213                  * Okay. The rules for generating a UID go like this...
214                  * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
215                  * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
216                  * A again, in an iterative fashion.. so..
217                  * AAA9 -> AABA, and so on. -- w00t
218                  */
219
220                 /* start at the end of the current UID string, work backwards. don't trample on SID! */
221                 for (i = UUID_LENGTH - 2; i > 3; i--)
222                 {
223                         if (current_uid[i] == 'Z')
224                         {
225                                 /* reached the end of alphabetical, go to numeric range */
226                                 current_uid[i] = '0';
227                         }
228                         else if (current_uid[i] == '9')
229                         {
230                                 /* we reached the end of the sequence, set back to A */
231                                 current_uid[i] = 'A';
232
233                                 /* we also need to increment the next digit. */
234                                 continue;
235                         }
236                         else
237                         {
238                                 /* most common case .. increment current UID */
239                                 current_uid[i]++;
240                         }
241
242                         if (current_uid[3] == 'Z')
243                         {
244                                 /*
245                                  * Ugh. We have run out of room.. roll back around to the
246                                  * start of the UUID namespace. -- w00t
247                                  */
248                                 this->InitialiseUID();
249
250                                 /*
251                                  * and now we need to break the inner for () to continue the while (),
252                                  * which will start the checking process over again. -- w00t
253                                  */
254                                 break;
255                                 
256                         }
257                         
258                         if (this->FindUUID(current_uid))
259                         {
260                                 /*
261                                  * It's in use. We need to try the loop again.
262                                  */
263                                 continue;
264                         }
265
266                         return current_uid;
267                 }
268         }
269 #endif
270         /* not reached. */
271         return "";
272 }
273
274
275