]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/server.cpp
Fix some more incorrect socket use
[user/henk/code/inspircd.git] / src / server.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 #include "inspircd_version.h"
18
19 void InspIRCd::SignalHandler(int signal)
20 {
21         if (signal == SIGHUP)
22         {
23                 Rehash("Caught SIGHUP");
24         }
25         else if (signal == SIGTERM)
26         {
27                 Exit(signal);
28         }
29 }
30
31 void InspIRCd::Exit(int status)
32 {
33 #ifdef WINDOWS
34         if (WindowsIPC)
35                 delete WindowsIPC;
36         SetServiceStopped(status);
37 #endif
38         if (this)
39         {
40                 this->SendError("Exiting with status " + ConvToStr(status) + " (" + std::string(ExitCodes[status]) + ")");
41                 this->Cleanup();
42                 delete this;
43                 ServerInstance = NULL;
44         }
45         exit (status);
46 }
47
48 void RehashHandler::Call(const std::string &reason)
49 {
50         ServerInstance->SNO->WriteToSnoMask('a', "Rehashing config file %s %s",ServerConfig::CleanFilename(ServerInstance->ConfigFileName.c_str()), reason.c_str());
51         ServerInstance->RehashUsersAndChans();
52         FOREACH_MOD(I_OnGarbageCollect, OnGarbageCollect());
53         if (!ServerInstance->ConfigThread)
54         {
55                 ServerInstance->ConfigThread = new ConfigReaderThread("");
56                 ServerInstance->Threads->Start(ServerInstance->ConfigThread);
57         }
58 }
59
60 std::string InspIRCd::GetVersionString(bool operstring)
61 {
62         char versiondata[MAXBUF];
63         if (operstring)
64                 snprintf(versiondata,MAXBUF,"%s %s :%s [%s,%s,%s]",VERSION,Config->ServerName.c_str(),SYSTEM,REVISION,SE->GetName().c_str(),Config->sid.c_str());
65         else
66                 snprintf(versiondata,MAXBUF,"InspIRCd-2.0 %s :%s",Config->ServerName.c_str(),Config->CustomVersion.c_str());
67         return versiondata;
68 }
69
70 void InspIRCd::BuildISupport()
71 {
72         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
73         std::stringstream v;
74         v << "WALLCHOPS WALLVOICES MODES=" << Config->Limits.MaxModes - 1 << " CHANTYPES=# PREFIX=" << this->Modes->BuildPrefixes() << " MAP MAXCHANNELS=" << Config->MaxChans << " MAXBANS=60 VBANLIST NICKLEN=" << Config->Limits.NickMax - 1;
75         v << " CASEMAPPING=rfc1459 STATUSMSG=" << Modes->BuildPrefixes(false) << " CHARSET=ascii TOPICLEN=" << Config->Limits.MaxTopic - 1 << " KICKLEN=" << Config->Limits.MaxKick - 1 << " MAXTARGETS=" << Config->MaxTargets - 1;
76         v << " AWAYLEN=" << Config->Limits.MaxAway - 1 << " CHANMODES=" << this->Modes->GiveModeList(MASK_CHANNEL) << " FNC NETWORK=" << Config->Network << " MAXPARA=32 ELIST=MU";
77         Config->data005 = v.str();
78         FOREACH_MOD(I_On005Numeric,On005Numeric(Config->data005));
79         Config->Update005();
80 }
81
82 void InspIRCd::IncrementUID(int pos)
83 {
84         /*
85          * Okay. The rules for generating a UID go like this...
86          * -- > ABCDEFGHIJKLMNOPQRSTUVWXYZ --> 012345679 --> WRAP
87          * That is, we start at A. When we reach Z, we go to 0. At 9, we go to
88          * A again, in an iterative fashion.. so..
89          * AAA9 -> AABA, and so on. -- w00t
90          */
91         if (pos == 3)
92         {
93                 // At pos 3, if we hit '9', we've run out of available UIDs, and need to reset to AAA..AAA.
94                 if (current_uid[pos] == '9')
95                 {
96                         for (int i = 3; i < (UUID_LENGTH - 1); i++)
97                         {
98                                 current_uid[i] = 'A';
99                                 pos  = UUID_LENGTH - 1;
100                         }
101                 }
102                 else
103                 {
104                         // Buf if we haven't, just keep incrementing merrily.
105                         current_uid[pos]++;
106                 }
107         }
108         else
109         {
110                 // If we hit Z, wrap around to 0.
111                 if (current_uid[pos] == 'Z')
112                 {
113                         current_uid[pos] = '0';
114                 }
115                 else if (current_uid[pos] == '9')
116                 {
117                         /*
118                          * Or, if we hit 9, wrap around to pos = 'A' and (pos - 1)++,
119                          * e.g. A9 -> BA -> BB ..
120                          */
121                         current_uid[pos] = 'A';
122                         this->IncrementUID(pos - 1);
123                 }
124                 else
125                 {
126                         // Anything else, nobody gives a shit. Just increment.
127                         current_uid[pos]++;
128                 }
129         }
130 }
131
132 /*
133  * Retrieve the next valid UUID that is free for this server.
134  */
135 std::string InspIRCd::GetUID()
136 {
137         static int curindex = -1;
138
139         /*
140          * If -1, we're setting up. Copy SID into the first three digits, 9's to the rest, null term at the end
141          * Why 9? Well, we increment before we find, otherwise we have an unnecessary copy, and I want UID to start at AAA..AA
142          * and not AA..AB. So by initialising to 99999, we force it to rollover to AAAAA on the first IncrementUID call.
143          * Kind of silly, but I like how it looks.
144          *              -- w
145          */
146         if (curindex == -1)
147         {
148                 current_uid[0] = Config->sid[0];
149                 current_uid[1] = Config->sid[1];
150                 current_uid[2] = Config->sid[2];
151
152                 for (int i = 3; i < (UUID_LENGTH - 1); i++)
153                         current_uid[i] = '9';
154
155                 curindex = UUID_LENGTH - 2; // look at the end of the string now kthx, ignore null
156
157                 // Null terminator. Important.
158                 current_uid[UUID_LENGTH - 1] = '\0';
159         }
160
161         while (1)
162         {
163                 // Add one to the last UID
164                 this->IncrementUID(curindex);
165
166                 if (this->FindUUID(current_uid))
167                 {
168                         /*
169                          * It's in use. We need to try the loop again.
170                          */
171                         continue;
172                 }
173
174                 return current_uid;
175         }
176
177         /* not reached. */
178         return "";
179 }
180
181
182