summaryrefslogtreecommitdiff
path: root/modules/global/global.cpp
blob: 1a9f98c3ccf4304895df8178ebea295b77b73928 (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
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
/* Global core functions
 *
 * (C) 2003-2024 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 "module.h"

class GlobalCore final
	: public Module
	, public GlobalService
{
private:
	Reference<BotInfo> global;
	PrimitiveExtensibleItem<std::vector<Anope::string>> queue;

	void ServerGlobal(BotInfo *sender, Server *server, bool children, const Anope::string &message)
	{
		if (server != Me && !server->IsJuped())
			server->Notice(sender, message);

		if (children)
		{
			for (auto *link : server->GetLinks())
				this->ServerGlobal(sender, link, true, message);
		}
	}

public:
	GlobalCore(const Anope::string &modname, const Anope::string &creator)
		: Module(modname, creator, PSEUDOCLIENT | VENDOR)
		, GlobalService(this)
		, queue(this, "global-queue")
	{
	}

	void ClearQueue(NickCore *nc) override
	{
		queue.Unset(nc);
	}

	Reference<BotInfo> GetDefaultSender() const override
	{
		return global;
	}

	const std::vector<Anope::string> *GetQueue(NickCore* nc) const override
	{
		return queue.Get(nc);
	}

	size_t Queue(NickCore *nc, const Anope::string &message) override
	{
		auto *q = queue.Require(nc);
		q->push_back(message);
		return q->size();
	}

	void OnReload(Configuration::Conf *conf) override
	{
		const auto glnick = conf->GetModule(this)->Get<const Anope::string>("client");
		if (glnick.empty())
			throw ConfigException(Module::name + ": <client> must be defined");

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

		global = bi;
	}

	void OnRestart() override
	{
		const auto msg = Config->GetModule(this)->Get<const Anope::string>("globaloncycledown");
		if (!msg.empty())
			this->SendSingle(msg, nullptr, nullptr, nullptr);
	}

	void OnShutdown() override
	{
		const auto msg = Config->GetModule(this)->Get<const Anope::string>("globaloncycledown");
		if (!msg.empty())
			this->SendSingle(msg, nullptr, nullptr, nullptr);
	}

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

	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;
	}

	bool SendQueue(CommandSource &source, BotInfo *sender, Server *server) override
	{
		// We MUST have an account.
		if (!source.nc)
			return false;

		// We MUST have a message queue.
		auto *q = queue.Get(source.nc);
		if (!q || q->empty())
			return false;

		auto success = true;
		for (const auto &message : *q)
		{
			if (!SendSingle(message, &source, sender, server))
			{
				success = false;
				break;
			}
		}

		queue.Unset(source.nc);
		return success;
	}

	bool SendSingle(const Anope::string &message, CommandSource *source, BotInfo* sender, Server* server) override
	{
		// We MUST have a sender.
		if (sender)
			sender = global;
		if (!sender)
			return false;

		Anope::string line;
		if (source && !Config->GetModule(this)->Get<bool>("anonymousglobal"))
		{
			// A source is available and they're not anonymous.
			line = Anope::printf("[%s] %s", source->GetNick().c_str(), message.c_str());
		}
		else
		{
			// A source isn't available or they're anonymous.
			line = message.empty() ? " " : message;
		}

		if (server)
			this->ServerGlobal(sender, server, false, line);
		else
			this->ServerGlobal(sender, Servers::GetUplink(), true, line);
		return true;
	}

	bool Unqueue(NickCore *nc, size_t idx) override
	{
		auto *q = queue.Get(nc);
		if (!q || idx > q->size())
			return false;

		q->erase(q->begin() + idx);
		if (q->empty())
			queue.Unset(nc);

		return true;
	}
};

MODULE_INIT(GlobalCore)