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