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