diff options
Diffstat (limited to 'src/init.cpp')
-rw-r--r-- | src/init.cpp | 188 |
1 files changed, 109 insertions, 79 deletions
diff --git a/src/init.cpp b/src/init.cpp index 7e5722603..2922edaf1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1,12 +1,20 @@ -/* Initialization and related routines. +/* + * Anope IRC Services * - * (C) 2003-2016 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2003-2016 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ #include "services.h" @@ -18,6 +26,8 @@ #include "socketengine.h" #include "servers.h" #include "language.h" +#include "event.h" +#include "modules.h" #ifndef _WIN32 #include <sys/wait.h> @@ -105,7 +115,7 @@ void Anope::Fork() { #ifndef _WIN32 kill(getppid(), SIGUSR2); - + freopen("/dev/null", "r", stdin); freopen("/dev/null", "w", stdout); freopen("/dev/null", "w", stderr); @@ -124,8 +134,6 @@ void Anope::HandleSignal() { case SIGHUP: { - Anope::SaveDatabases(); - try { Configuration::Conf *new_config = new Configuration::Conf(); @@ -136,21 +144,20 @@ void Anope::HandleSignal() } catch (const ConfigException &ex) { - Log() << "Error reloading configuration file: " << ex.GetReason(); + Anope::Logger.Log("Error reloading configuration file: {0}", ex.GetReason()); } break; } case SIGTERM: case SIGINT: #ifndef _WIN32 - Log() << "Received " << strsignal(Signal) << " signal (" << Signal << "), exiting."; + Anope::Logger.Log(_("Received {0} signal ({1}), exiting"), strsignal(Signal), Signal); Anope::QuitReason = Anope::string("Services terminating via signal ") + strsignal(Signal) + " (" + stringify(Signal) + ")"; #else - Log() << "Received signal " << Signal << ", exiting."; - Anope::QuitReason = Anope::string("Services terminating via signal ") + stringify(Signal); + Anope::Logger.Log(_("Received signal {0}, exiting"), Signal); + Anope::QuitReason = "Services terminating via signal " + stringify(Signal); #endif Anope::Quitting = true; - Anope::SaveDatabases(); break; } @@ -207,14 +214,14 @@ static void InitSignals() static void remove_pidfile() { - remove(Config->GetBlock("serverinfo")->Get<const Anope::string>("pid").c_str()); + remove(Config->GetBlock("serverinfo")->Get<Anope::string>("pid").c_str()); } /* Create our PID file and write the PID to it. */ static void write_pidfile() { - FILE *pidfile = fopen(Config->GetBlock("serverinfo")->Get<const Anope::string>("pid").c_str(), "w"); + FILE *pidfile = fopen(Config->GetBlock("serverinfo")->Get<Anope::string>("pid").c_str(), "w"); if (pidfile) { #ifdef _WIN32 @@ -226,7 +233,7 @@ static void write_pidfile() atexit(remove_pidfile); } else - throw CoreException("Can not write to PID file " + Config->GetBlock("serverinfo")->Get<const Anope::string>("pid")); + throw CoreException("Can not write to PID file " + Config->GetBlock("serverinfo")->Get<Anope::string>("pid")); } static void setuidgid() @@ -236,21 +243,21 @@ static void setuidgid() uid_t uid = -1; gid_t gid = -1; - if (!options->Get<const Anope::string>("user").empty()) + if (!options->Get<Anope::string>("user").empty()) { errno = 0; - struct passwd *u = getpwnam(options->Get<const Anope::string>("user").c_str()); + struct passwd *u = getpwnam(options->Get<Anope::string>("user").c_str()); if (u == NULL) - Log() << "Unable to setuid to " << options->Get<const Anope::string>("user") << ": " << Anope::LastError(); + Anope::Logger.Log(_("Unable to setuid to {0}: {1}"), options->Get<Anope::string>("user"), Anope::LastError()); else uid = u->pw_uid; } - if (!options->Get<const Anope::string>("group").empty()) + if (!options->Get<Anope::string>("group").empty()) { errno = 0; - struct group *g = getgrnam(options->Get<const Anope::string>("group").c_str()); + struct group *g = getgrnam(options->Get<Anope::string>("group").c_str()); if (g == NULL) - Log() << "Unable to setgid to " << options->Get<const Anope::string>("group") << ": " << Anope::LastError(); + Anope::Logger.Log(_("Unable to setgid to {0}: {1}"), options->Get<Anope::string>("group"), Anope::LastError()); else gid = g->gr_gid; } @@ -270,16 +277,16 @@ static void setuidgid() if (static_cast<int>(gid) != -1) { if (setgid(gid) == -1) - Log() << "Unable to setgid to " << options->Get<const Anope::string>("group") << ": " << Anope::LastError(); + Anope::Logger.Log(_("Unable to setgid to {0}: {1}"), options->Get<Anope::string>("group"), Anope::LastError()); else - Log() << "Successfully set group to " << options->Get<const Anope::string>("group"); + Anope::Logger.Log(_("Successfully set group to {0"), options->Get<Anope::string>("group")); } if (static_cast<int>(uid) != -1) { if (setuid(uid) == -1) - Log() << "Unable to setuid to " << options->Get<const Anope::string>("user") << ": " << Anope::LastError(); + Anope::Logger.Log(_("Unable to setuid to {0}: {1}"), options->Get<Anope::string>("user"), Anope::LastError()); else - Log() << "Successfully set user to " << options->Get<const Anope::string>("user"); + Anope::Logger.Log(_("Successfully set user to {0}"), options->Get<Anope::string>("user")); } #endif } @@ -291,41 +298,39 @@ void Anope::Init(int ac, char **av) umask(DEFUMASK); #endif - Serialize::RegisterTypes(); - /* Parse command line arguments */ ParseCommandLineArguments(ac, av); if (GetCommandLineArgument("version", 'v')) { - Log(LOG_TERMINAL) << "Anope-" << Anope::Version() << " -- " << Anope::VersionBuildString(); - throw CoreException(); + Anope::Logger.Terminal(_("Anope-{0} -- Built: {1} -- Flags: {2}"), Anope::Version(), Anope::VersionBuildTime(), Anope::VersionFlags()); + exit(EXIT_SUCCESS); } 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 ./" << Anope::ServicesBin << " [options] ..."; - Log(LOG_TERMINAL) << "-c, --config=filename.conf"; - Log(LOG_TERMINAL) << " --confdir=conf file direcory"; - Log(LOG_TERMINAL) << " --dbdir=database directory"; - Log(LOG_TERMINAL) << "-d, --debug[=level]"; - Log(LOG_TERMINAL) << "-h, --help"; - Log(LOG_TERMINAL) << " --localedir=locale directory"; - Log(LOG_TERMINAL) << " --logdir=logs directory"; - Log(LOG_TERMINAL) << " --modulesdir=modules directory"; - 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 CoreException(); + Anope::Logger.Terminal(_("Anope-{0}"), Anope::Version()); + Anope::Logger.Terminal(_("Anope IRC Services (https://anope.org)")); + Anope::Logger.Terminal(_("Usage ./{0} [options] ..."), Anope::ServicesBin); + Anope::Logger.Terminal(_("-c, --config=filename.conf")); + Anope::Logger.Terminal(_(" --confdir=conf file direcory")); + Anope::Logger.Terminal(_(" --dbdir=database directory")); + Anope::Logger.Terminal(_("-d, --debug[=level]")); + Anope::Logger.Terminal(_("-h, --help")); + Anope::Logger.Terminal(_(" --localedir=locale directory")); + Anope::Logger.Terminal(_(" --logdir=logs directory")); + Anope::Logger.Terminal(_(" --modulesdir=modules directory")); + Anope::Logger.Terminal(_("-e, --noexpire")); + Anope::Logger.Terminal(_("-n, --nofork")); + Anope::Logger.Terminal(_(" --nothird")); + Anope::Logger.Terminal(_(" --protocoldebug")); + Anope::Logger.Terminal(_("-r, --readonly")); + Anope::Logger.Terminal(_("-s, --support")); + Anope::Logger.Terminal(_("-v, --version")); + Anope::Logger.Terminal(""); + Anope::Logger.Terminal(_("Further support is available from https://anope.org")); + Anope::Logger.Terminal(_("Or visit us on IRC at irc.anope.org #anope")); + exit(EXIT_SUCCESS); } if (GetCommandLineArgument("nofork", 'n')) @@ -412,7 +417,7 @@ void Anope::Init(int ac, char **av) throw CoreException("Unable to chdir to " + Anope::ServicesDir + ": " + Anope::LastError()); } - Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::VersionBuildString(); + Anope::Logger.Terminal("Anope-{0} -- Built: {1} -- Flags: {2}", Anope::Version(), Anope::VersionBuildTime(), Anope::VersionFlags()); #ifdef _WIN32 if (!SupportedWindowsVersion()) @@ -423,7 +428,7 @@ void Anope::Init(int ac, char **av) { /* If we are configured to setuid later, don't issue a warning */ Configuration::Block *options = Config->GetBlock("options"); - if (options->Get<const Anope::string>("user").empty()) + if (options->Get<Anope::string>("user").empty()) { std::cerr << "WARNING: You are currently running Anope as the root superuser. Anope does not" << std::endl; std::cerr << " require root privileges to run, and it is discouraged that you run Anope" << std::endl; @@ -434,9 +439,9 @@ void Anope::Init(int ac, char **av) #endif #ifdef _WIN32 - Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "\\" << ServicesConf.GetName(); + Anope::Logger.Terminal("Using configuration file {0}\\{1}", Anope::ConfigDir, ServicesConf.GetName()); #else - Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "/" << ServicesConf.GetName(); + Anope::Logger.Terminal("Using configuration file {0}/{1}", Anope::ConfigDir, ServicesConf.GetName()); /* Fork to background */ if (!Anope::NoFork) @@ -466,7 +471,7 @@ void Anope::Init(int ac, char **av) } else if (i == -1) { - Log() << "Error, unable to fork: " << Anope::LastError(); + Anope::Logger.Terminal("Error, unable to fork: {0}", Anope::LastError()); Anope::NoFork = true; } @@ -480,6 +485,13 @@ void Anope::Init(int ac, char **av) /* Initialize the socket engine. Note that some engines can not survive a fork(), so this must be here. */ SocketEngine::Init(); + ServiceManager::Init(); + EventManager::Init(); + + new BotInfoType(); + new XLineType(nullptr); + new OperBlockType(); + /* Read configuration file; exit if there are problems. */ try { @@ -487,25 +499,35 @@ void Anope::Init(int ac, char **av) } 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."; + Anope::Logger.Terminal(ex.GetReason()); + Anope::Logger.Terminal("*** Support resources: Read through the anope.conf self-contained"); + Anope::Logger.Terminal("*** documentation. Read the documentation files found in the 'docs'"); + Anope::Logger.Terminal("*** folder. Visit our portal located at https://anope.org/. Join"); + Anope::Logger.Terminal("*** our support channel on irc.anope.org #anope."); throw CoreException("Configuration file failed to validate"); } /* Create me */ Configuration::Block *block = Config->GetBlock("serverinfo"); - Me = new Server(NULL, block->Get<const Anope::string>("name"), 0, block->Get<const Anope::string>("description"), block->Get<const Anope::string>("id")); - for (botinfo_map::const_iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it) + Me = new Server(NULL, block->Get<Anope::string>("name"), 0, block->Get<Anope::string>("description"), block->Get<Anope::string>("id")); + for (std::pair<Anope::string, User *> p : UserListByNick) { - it->second->server = Me; + User *u = p.second; + if (u->type != UserType::BOT) + continue; + + ServiceBot *bi = anope_dynamic_static_cast<ServiceBot *>(u); + bi->server = Me; ++Me->users; } /* Announce ourselves to the logfile. */ - Log() << "Anope " << Anope::Version() << " starting up" << (Anope::Debug || Anope::ReadOnly ? " (options:" : "") << (Anope::Debug ? " debug" : "") << (Anope::ReadOnly ? " readonly" : "") << (Anope::Debug || Anope::ReadOnly ? ")" : ""); + if (Anope::Debug || Anope::ReadOnly) + Anope::Logger.Log("Anope {0} starting up ({1})", + Anope::Version(), + Anope::Format("options:{0}{1}", Anope::Debug ? " debug" : "", Anope::ReadOnly ? " readonly" : "")); + else + Anope::Logger.Log("Anope {0} starting up", Anope::Version()); InitSignals(); @@ -516,10 +538,15 @@ void Anope::Init(int ac, char **av) block = Config->GetBlock("options"); srand(block->Get<unsigned>("seed") ^ time(NULL)); + ModeManager::Apply(nullptr); + /* load modules */ - Log() << "Loading modules..."; + Anope::Logger.Log("Loading modules..."); for (int i = 0; i < Config->CountBlock("module"); ++i) - ModuleManager::LoadModule(Config->GetBlock("module", i)->Get<const Anope::string>("name"), NULL); + ModuleManager::LoadModule(Config->GetBlock("module", i)->Get<Anope::string>("name"), NULL); + + Config->LoadOpers(); + Config->ApplyBots(); #ifndef _WIN32 /* We won't background later, so we should setuid now */ @@ -534,7 +561,7 @@ void Anope::Init(int ac, char **av) /* Write our PID to the PID file. */ write_pidfile(); - Log() << "Using IRCd protocol " << protocol->name; + Anope::Logger.Log("Using IRCd protocol {0}", protocol->GetName()); /* Auto assign sid if applicable */ if (IRCD->RequiresID) @@ -542,20 +569,23 @@ void Anope::Init(int ac, char **av) Anope::string sid = IRCD->SID_Retrieve(); if (Me->GetSID() == Me->GetName()) Me->SetSID(sid); - for (botinfo_map::iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it) - it->second->GenerateUID(); + for (std::pair<Anope::string, User *> p : UserListByNick) + { + User *u = p.second; + if (u->type != UserType::BOT) + continue; + + ServiceBot *bi = anope_dynamic_static_cast<ServiceBot *>(u); + bi->GenerateUID(); + } } /* Load up databases */ - Log() << "Loading databases..."; - EventReturn MOD_RESULT; - FOREACH_RESULT(OnLoadDatabase, MOD_RESULT, ()); - static_cast<void>(MOD_RESULT); - Log() << "Databases loaded"; + Anope::Logger.Log("Loading databases..."); + EventManager::Get()->Dispatch(&Event::LoadDatabase::OnLoadDatabase);; + Anope::Logger.Log("Databases loaded"); for (channel_map::const_iterator it = ChannelList.begin(), it_end = ChannelList.end(); it != it_end; ++it) it->second->Sync(); - - Serialize::CheckTypes(); } |