]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Threadengine stuff
[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         /*this->Config->Read(false,NULL);*/
53         this->ResetMaxBans();
54         this->Res->Rehash();
55         FOREACH_MOD_I(this,I_OnRehash,OnRehash(NULL,""));
56         this->BuildISupport();
57 }
58
59 void InspIRCd::RehashServer()
60 {
61         this->SNO->WriteToSnoMask('A', "Rehashing config file");
62         this->RehashUsersAndChans();
63         /*this->Config->Read(false,NULL);*/
64         this->ResetMaxBans();
65         this->Res->Rehash();
66 }
67
68 std::string InspIRCd::GetVersionString()
69 {
70         char versiondata[MAXBUF];
71         if (*Config->CustomVersion)
72         {
73                 snprintf(versiondata,MAXBUF,"%s %s :%s",VERSION,Config->ServerName,Config->CustomVersion);
74         }
75         else
76         {
77                 snprintf(versiondata,MAXBUF,"%s %s :%s [FLAGS=%s,%s,%s]",VERSION,Config->ServerName,SYSTEM,REVISION,SE->GetName().c_str(),Config->sid);
78         }
79         return versiondata;
80 }
81
82 void InspIRCd::BuildISupport()
83 {
84         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
85         std::stringstream v;
86         v << "WALLCHOPS WALLVOICES MODES=" << MAXMODES << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << Config->MaxChans << " MAXBANS=60 VBANLIST NICKLEN=" << NICKMAX-1;
87         v << " CASEMAPPING=rfc1459 STATUSMSG=@" << (this->Config->AllowHalfop ? "%" : "") << "+ CHARSET=ascii TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=" << Config->MaxTargets;
88         v << " AWAYLEN=" << MAXAWAY << " CHANMODES=" << this->Modes->ChanModes() << " FNC NETWORK=" << Config->Network << " MAXPARA=32 ELIST=MU";
89         Config->data005 = v.str();
90         FOREACH_MOD_I(this,I_On005Numeric,On005Numeric(Config->data005));
91         Config->Update005();
92 }
93
94 std::string InspIRCd::GetRevision()
95 {
96         return REVISION;
97 }
98
99 void InspIRCd::AddServerName(const std::string &servername)
100 {
101         servernamelist::iterator itr = servernames.begin();
102         for(; itr != servernames.end(); ++itr)
103                 if(**itr == servername)
104                         return;
105
106         std::string * ns = new std::string(servername);
107         servernames.push_back(ns);
108 }
109
110 const char* InspIRCd::FindServerNamePtr(const std::string &servername)
111 {
112         servernamelist::iterator itr = servernames.begin();
113         for(; itr != servernames.end(); ++itr)
114                 if(**itr == servername)
115                         return (*itr)->c_str();
116
117         servernames.push_back(new std::string(servername));
118         itr = --servernames.end();
119         return (*itr)->c_str();
120 }
121
122 bool InspIRCd::FindServerName(const std::string &servername)
123 {
124         servernamelist::iterator itr = servernames.begin();
125         for(; itr != servernames.end(); ++itr)
126                 if(**itr == servername)
127                         return true;
128         return false;
129 }
130
131 /*
132  * Retrieve the next valid UUID that is free for this server.
133  */
134 std::string InspIRCd::GetUID()
135 {
136         int i;
137
138         /*
139          * This will only finish once we return a UUID that is not in use.
140          */
141         while (1)
142         {
143                 /*
144                  * Okay. The rules for generating a UID go like this...
145                  * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
146                  * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
147                  * A again, in an iterative fashion.. so..
148                  * AAA9 -> AABA, and so on. -- w00t
149                  */
150
151                 /* start at the end of the current UID string, work backwards. don't trample on SID! */
152                 for (i = UUID_LENGTH - 2; i > 3; i--)
153                 {
154                         if (current_uid[i] == 'Z')
155                         {
156                                 /* reached the end of alphabetical, go to numeric range */
157                                 current_uid[i] = '0';
158                         }
159                         else if (current_uid[i] == '9')
160                         {
161                                 /* we reached the end of the sequence, set back to A */
162                                 current_uid[i] = 'A';
163
164                                 /* we also need to increment the next digit. */
165                                 continue;
166                         }
167                         else
168                         {
169                                 /* most common case .. increment current UID */
170                                 current_uid[i]++;
171                         }
172
173                         if (current_uid[3] == 'Z')
174                         {
175                                 /*
176                                  * Ugh. We have run out of room.. roll back around to the
177                                  * start of the UUID namespace. -- w00t
178                                  */
179                                 this->InitialiseUID();
180
181                                 /*
182                                  * and now we need to break the inner for () to continue the while (),
183                                  * which will start the checking process over again. -- w00t
184                                  */
185                                 break;
186                                 
187                         }
188                         
189                         if (this->FindUUID(current_uid))
190                         {
191                                 /*
192                                  * It's in use. We need to try the loop again.
193                                  */
194                                 continue;
195                         }
196
197                         return current_uid;
198                 }
199         }
200
201         /* not reached. */
202         return "";
203 }
204
205
206