summaryrefslogtreecommitdiff
path: root/modules/global/main/global.cpp
blob: cbc0e0757a39cd5d69ccdec569134730127f5e81 (plain)
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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2011-2016 Anope Team <team@anope.org>
 *
 * 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.
 *
 * 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 "module.h"
#include "modules/help.h"
#include "modules/global.h"

class GlobalCore : public Module
	, public Global::GlobalService
	, public EventHook<Event::Restart>
	, public EventHook<Event::Shutdown>
	, public EventHook<Event::NewServer>
	, public EventHook<Event::Help>
{
	Reference<ServiceBot> Global;

	void ServerGlobal(ServiceBot *sender, Server *s, const Anope::string &message)
	{
		if (s != Me && !s->IsJuped())
			s->Notice(sender, message);
		for (unsigned i = 0, j = s->GetLinks().size(); i < j; ++i)
			this->ServerGlobal(sender, s->GetLinks()[i], message);
	}

 public:
	GlobalCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR)
		, EventHook<Event::Restart>(this)
		, EventHook<Event::Shutdown>(this)
		, EventHook<Event::NewServer>(this)
		, EventHook<Event::Help>(this)
		, GlobalService(this)
	{
	}

	void SendGlobal(ServiceBot *sender, const Anope::string &source, const Anope::string &message) override
	{
		if (Me->GetLinks().empty())
			return;
		if (!sender)
			sender = Global;
		if (!sender)
			return;

		Anope::string rmessage;

		if (!source.empty() && !Config->GetModule("global")->Get<bool>("anonymousglobal"))
			rmessage = "[" + source + "] " + message;
		else
			rmessage = message;

		this->ServerGlobal(sender, Servers::GetUplink(), rmessage);
	}

	void OnReload(Configuration::Conf *conf) override
	{
		const Anope::string &glnick = conf->GetModule(this)->Get<Anope::string>("client");

		if (glnick.empty())
			throw ConfigException(Module::name + ": <client> must be defined");

		ServiceBot *bi = ServiceBot::Find(glnick, true);
		if (!bi)
			throw ConfigException(Module::name + ": no bot named " + glnick);

		Global = bi;
	}

	void OnRestart() override
	{
		const Anope::string &gl = Config->GetModule(this)->Get<Anope::string>("globaloncycledown");
		if (!gl.empty())
			this->SendGlobal(Global, "", gl);
	}

	void OnShutdown() override
	{
		const Anope::string &gl = Config->GetModule(this)->Get<Anope::string>("globaloncycledown");
		if (!gl.empty())
			this->SendGlobal(Global, "", gl);
	}

	void OnNewServer(Server *s) override
	{
		const Anope::string &gl = Config->GetModule(this)->Get<Anope::string>("globaloncycleup");
		if (!gl.empty() && !Me->IsSynced())
			s->Notice(Global, gl);
	}

	EventReturn OnPreHelp(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		if (!params.empty() || source.c || source.service != *Global)
			return EVENT_CONTINUE;
		source.Reply(_("%s commands:"), Global->nick.c_str());
		return EVENT_CONTINUE;
	}

	void OnPostHelp(CommandSource &source, const std::vector<Anope::string> &params) override
	{
	}
};

MODULE_INIT(GlobalCore)