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