1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/* Initalization and related routines.
*
* (C) 2003-2011 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "services.h"
#include "modules.h"
Uplink *uplink_server;
void introduce_user(const Anope::string &user)
{
/* Watch out for infinite loops... */
time_t now = Anope::CurTime;
static time_t lasttime = now - 4;
if (lasttime >= now - 3)
throw FatalException("introduce_user loop detected");
lasttime = now;
if (!user.empty())
{
User *u = finduser(user);
if (u)
{
ircdproto->SendClientIntroduction(u, ircd->pseudoclient_mode);
BotInfo *bi = findbot(u->nick);
if (bi)
{
XLine x(bi->nick, "Reserved for services");
ircdproto->SendSQLine(NULL, &x);
for (UChannelList::const_iterator cit = bi->chans.begin(), cit_end = bi->chans.end(); cit != cit_end; ++cit)
ircdproto->SendJoin(bi, (*cit)->chan, &Config->BotModeList);
}
}
return;
}
ircdproto->SendBOB();
for (unsigned i = 0; i < Me->GetLinks().size(); ++i)
{
Server *s = Me->GetLinks()[i];
if (s->HasFlag(SERVER_JUPED))
{
ircdproto->SendServer(s);
}
}
/* We make the bots go online */
for (Anope::insensitive_map<User *>::iterator it = UserListByNick.begin(), it_end = UserListByNick.end(); it != it_end; ++it)
{
User *u = it->second;
ircdproto->SendClientIntroduction(u, ircd->pseudoclient_mode);
BotInfo *bi = findbot(u->nick);
if (bi)
{
XLine x(bi->nick, "Reserved for services");
ircdproto->SendSQLine(NULL, &x);
for (UChannelList::const_iterator cit = bi->chans.begin(), cit_end = bi->chans.end(); cit != cit_end; ++cit)
ircdproto->SendJoin(bi, (*cit)->chan, &Config->BotModeList);
}
}
/* Load MLock from the database now that we know what modes exist */
for (registered_channel_map::iterator it = RegisteredChannelList.begin(), it_end = RegisteredChannelList.end(); it != it_end; ++it)
it->second->LoadMLock();
}
/*************************************************************************/
/* Set GID if necessary. Return 0 if successful (or if RUNGROUP not
* defined), else print an error message to logfile and return -1.
*/
static int set_group()
{
#if defined(RUNGROUP) && defined(HAVE_SETGRENT)
struct group *gr;
setgrent();
while ((gr = getgrent()))
{
if (!strcmp(gr->gr_name, RUNGROUP))
break;
}
endgrent();
if (gr)
{
setgid(gr->gr_gid);
}
else
{
Log() << "Unknown run group '" << RUNGROUP << "'";
return -1;
}
#endif
return 0;
}
/*************************************************************************/
/* Vector of pairs of command line arguments and their params */
static std::vector<std::pair<Anope::string, Anope::string> > CommandLineArguments;
/** Called on startup to organize our starting arguments in a better way
* and check for errors
* @param ac number of args
* @param av args
*/
static void ParseCommandLineArguments(int ac, char **av)
{
for (int i = 1; i < ac; ++i)
{
Anope::string option = av[i];
Anope::string param;
while (!option.empty() && option[0] == '-')
option.erase(option.begin());
size_t t = option.find('=');
if (t != Anope::string::npos)
{
param = option.substr(t + 1);
option.erase(t);
}
if (option.empty())
continue;
CommandLineArguments.push_back(std::make_pair(option, param));
}
}
/** Check if an argument was given on startup
* @param name The argument name
* @param shortname A shorter name, eg --debug and -d
* @return true if name/shortname was found, false if not
*/
bool GetCommandLineArgument(const Anope::string &name, char shortname)
{
Anope::string Unused;
return GetCommandLineArgument(name, shortname, Unused);
}
/** Check if an argument was given on startup and its parameter
* @param name The argument name
* @param shortname A shorter name, eg --debug and -d
* @param param A string to put the param, if any, of the argument
* @return true if name/shortname was found, false if not
*/
bool GetCommandLineArgument(const Anope::string &name, char shortname, Anope::string ¶m)
{
param.clear();
for (std::vector<std::pair<Anope::string, Anope::string> >::iterator it = CommandLineArguments.begin(), it_end = CommandLineArguments.end(); it != it_end; ++it)
{
if (it->first.equals_ci(name) || it->first[0] == shortname)
{
param = it->second;
return true;
}
}
return false;
}
/*************************************************************************/
/* Remove our PID file. Done at exit. */
static void remove_pidfile()
{
remove(Config->PIDFilename.c_str());
}
/*************************************************************************/
/* Create our PID file and write the PID to it. */
static void write_pidfile()
{
FILE *pidfile;
pidfile = fopen(Config->PIDFilename.c_str(), "w");
if (pidfile)
{
#ifdef _WIN32
fprintf(pidfile, "%d\n", static_cast<int>(GetCurrentProcessId()));
#else
fprintf(pidfile, "%d\n", static_cast<int>(getpid()));
#endif
fclose(pidfile);
atexit(remove_pidfile);
}
else
throw FatalException("Can not write to PID file " + Config->PIDFilename);
}
/*************************************************************************/
void Init(int ac, char **av)
{
int started_from_term = isatty(0) && isatty(1) && isatty(2);
/* Set file creation mask and group ID. */
#if defined(DEFUMASK) && HAVE_UMASK
umask(DEFUMASK);
#endif
if (set_group() < 0)
throw FatalException("set_group() fail");
/* Parse command line arguments */
ParseCommandLineArguments(ac, av);
if (GetCommandLineArgument("version", 'v'))
{
Log(LOG_TERMINAL) << "Anope-" << Anope::Version() << " -- " << Anope::VersionBuildString();
throw FatalException();
}
if (GetCommandLineArgument("help", 'h'))
{
Log(LOG_TERMINAL) << "Anope-" << Anope::Version() << " -- " << Anope::VersionBuildString();
Log(LOG_TERMINAL) << "Anope IRC Services (http://www.anope.org)";
Log(LOG_TERMINAL) << "Usage ./" << services_bin << " [options] ...";
Log(LOG_TERMINAL) << "-c, --config=filename.conf";
Log(LOG_TERMINAL) << "-d, --debug[=level]";
Log(LOG_TERMINAL) << " --dir=services_directory";
Log(LOG_TERMINAL) << "-h, --help";
Log(LOG_TERMINAL) << "-e, --noexpire";
Log(LOG_TERMINAL) << "-n, --nofork";
Log(LOG_TERMINAL) << " --nothird";
Log(LOG_TERMINAL) << " --protocoldebug";
Log(LOG_TERMINAL) << "-r, --readonly";
Log(LOG_TERMINAL) << "-s, --support";
Log(LOG_TERMINAL) << "-v, --version";
Log(LOG_TERMINAL) << "";
Log(LOG_TERMINAL) << "Further support is available from http://www.anope.org";
Log(LOG_TERMINAL) << "Or visit us on IRC at irc.anope.org #anope";
throw FatalException();
}
if (GetCommandLineArgument("nofork", 'n'))
nofork = true;
if (GetCommandLineArgument("support", 's'))
{
nofork = nothird = true;
++debug;
}
if (GetCommandLineArgument("readonly", 'r'))
readonly = true;
if (GetCommandLineArgument("nothird"))
nothird = true;
if (GetCommandLineArgument("noexpire", 'e'))
noexpire = true;
if (GetCommandLineArgument("protocoldebug"))
protocoldebug = true;
Anope::string Arg;
if (GetCommandLineArgument("debug", 'd', Arg))
{
if (!Arg.empty())
{
int level = Arg.is_number_only() ? convertTo<int>(Arg) : -1;
if (level > 0)
debug = level;
else
throw FatalException("Invalid option given to --debug");
}
else
++debug;
}
if (GetCommandLineArgument("config", 'c', Arg))
{
if (Arg.empty())
throw FatalException("The --config option requires a file name");
services_conf = ConfigurationFile(Arg, false);
}
if (GetCommandLineArgument("dir", 0, Arg))
{
if (Arg.empty())
throw FatalException("The --dir option requires a directory name");
services_dir = Arg;
}
/* Chdir to Services data directory. */
if (chdir(services_dir.c_str()) < 0)
{
throw FatalException("Unable to chdir to " + services_dir + ": " + Anope::LastError());
}
init_core_messages();
Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::VersionBuildString();
#ifdef _WIN32
Log(LOG_TERMINAL) << "Using configuration file " << services_dir << "\\" << services_conf.GetName();
#else
Log(LOG_TERMINAL) << "Using configuration file " << services_dir << "/" << services_conf.GetName();
#endif
/* Read configuration file; exit if there are problems. */
try
{
Config = new ServerConfig();
}
catch (const ConfigException &ex)
{
Log(LOG_TERMINAL) << ex.GetReason();
Log(LOG_TERMINAL) << "*** Support resources: Read through the services.conf self-contained";
Log(LOG_TERMINAL) << "*** documentation. Read the documentation files found in the 'docs'";
Log(LOG_TERMINAL) << "*** folder. Visit our portal located at http://www.anope.org/. Join";
Log(LOG_TERMINAL) << "*** our support channel on /server irc.anope.org channel #anope.";
throw FatalException("Configuration file failed to validate");
}
/* Initialize the socket engine */
SocketEngine::Init();
/* Add IRCD Protocol Module; exit if there are errors */
if (protocol_module_init())
throw FatalException("Unable to load protocol module");
/* Create me */
Me = new Server(NULL, Config->ServerName, 0, Config->ServerDesc, Config->Numeric);
/* Add Encryption Modules */
ModuleManager::LoadModuleList(Config->EncModuleList);
/* Add Database Modules */
ModuleManager::LoadModuleList(Config->DBModuleList);
DNSEngine = new DNSManager();
#ifndef _WIN32
if (!nofork)
{
int i;
if ((i = fork()) < 0)
throw FatalException("Unable to fork");
else if (i != 0)
{
Log(LOG_TERMINAL) << "PID " << i;
exit(0);
}
if (started_from_term)
{
close(0);
close(1);
close(2);
}
if (setpgid(0, 0) < 0)
throw FatalException("Unable to setpgid()");
}
#else
if (!SupportedWindowsVersion())
throw FatalException(GetWindowsVersion() + " is not a supported version of Windows");
if (!nofork)
{
Log(LOG_TERMINAL) << "PID " << GetCurrentProcessId();
Log() << "Launching Anope into the background";
FreeConsole();
}
#endif
/* Write our PID to the PID file. */
write_pidfile();
/* Announce ourselves to the logfile. */
Log() << "Anope " << Anope::Version() << " (ircd protocol: " << ircd->name << ") starting up" << (debug || readonly ? " (options:" : "") << (debug ? " debug" : "") << (readonly ? " readonly" : "") << (debug || readonly ? ")" : "");
start_time = Anope::CurTime;
/* Set signal handlers. Catch certain signals to let us do things or
* panic as necessary, and ignore all others.
*/
#ifndef _WIN32
signal(SIGHUP, sighandler);
#endif
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
/* Initialize multi-language support */
Log(LOG_DEBUG) << "Loading Languages...";
InitLanguages();
/* load any custom modules */
if (!nothird)
ModuleManager::LoadModuleList(Config->ModulesAutoLoad);
/* Initialize random number generator */
rand_init();
add_entropy_userkeys();
/* Load up databases */
Log() << "Loading databases...";
EventReturn MOD_RESULT;
FOREACH_RESULT(I_OnLoadDatabase, OnLoadDatabase());
Log() << "Databases loaded";
FOREACH_MOD(I_OnPostLoadDatabases, OnPostLoadDatabases());
}
/*************************************************************************/
|