summaryrefslogtreecommitdiff
path: root/modules/operserv/modinfo.cpp
blob: 2307b26be1f648ed8373cccc776196c031cd6570 (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
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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2003-2017 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"

class CommandOSModInfo : public Command
{
 public:
	CommandOSModInfo(Module *creator) : Command(creator, "operserv/modinfo", 1, 1)
	{
		this->SetDesc(_("Info about a loaded module"));
		this->SetSyntax(_("\037modname\037"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &file = params[0];

		logger.Admin(source, _("{source} used {command} on {0}"), file);

		Module *m = ModuleManager::FindModule(file);
		if (m == nullptr)
		{
			source.Reply(_("No information about module \002{0}\002 is available."), file);
			return;
		}

		source.Reply(_("Module: \002{0}\002 Version: \002{1}\002 Author: \002{2}\002 Loaded: \002{3}\002"), m->name, !m->version.empty() ? m->version : "?", !m->author.empty() ? m->author : "Unknown", Anope::strftime(m->created, source.GetAccount()));
		if (Anope::Debug)
			source.Reply(_(" Loaded at: {0}"), Anope::printf("0x%x", m->handle));

		std::vector<Command *> commands = ServiceManager::Get()->FindServices<Command *>();
		for (Command *c : commands)
		{
			if (c->GetOwner() != m)
				continue;

			source.Reply(_("   Providing service: \002{0}\002"), c->GetName());

			for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
			{
				User *u = it->second;

				if (u->type != UserType::BOT)
					continue;

				ServiceBot *bi = anope_dynamic_static_cast<ServiceBot *>(u);

				for (CommandInfo::map::const_iterator cit = bi->commands.begin(), cit_end = bi->commands.end(); cit != cit_end; ++cit)
				{
					const Anope::string &c_name = cit->first;
					const CommandInfo &info = cit->second;
					if (info.name != c->GetName())
						continue;
					source.Reply(_("   Command \002{0}\002 on \002{1}\002 is linked to \002{2}\002"), c_name, bi->nick, c->GetName());
				}
			}
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("This command lists information about the specified loaded module."));
		return true;
	}
};

class CommandOSModList : public Command
{
 public:
	CommandOSModList(Module *creator) : Command(creator, "operserv/modlist", 0, 1)
	{
		this->SetDesc(_("List loaded modules"));
		this->SetSyntax("[all|third|vendor|extra|database|encryption|pseudoclient|protocol]");
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &param = !params.empty() ? params[0] : "";

		if (!param.empty())
			logger.Admin(source, _("{source} used {command} for {0}"), param);
		else
			logger.Admin(source, _("{source} used {command}"),
		source.GetSource(), source.GetCommand());

		bool third = false, vendor = false, extra = false, database = false, encryption = false, pseudoclient = false, protocol = false;

		if (param.equals_ci("all"))
			third = vendor = extra = database = encryption = pseudoclient = protocol = true;
		else if (param.equals_ci("third"))
			third = true;
		else if (param.equals_ci("vendor"))
			vendor = true;
		else if (param.equals_ci("extra"))
			extra = true;
		else if (param.equals_ci("database"))
			database = true;
		else if (param.equals_ci("encryption"))
			encryption = true;
		else if (param.equals_ci("pseudoclient"))
			pseudoclient = true;
		else if (param.equals_ci("protocol"))
			protocol = true;
		else
			third = extra = database = encryption = protocol = true;

		Module *protomod = ModuleManager::FindFirstOf(PROTOCOL);

		source.Reply(_("Current module list:"));

		int count = 0;
		for (std::list<Module *>::iterator it = ModuleManager::Modules.begin(), it_end = ModuleManager::Modules.end(); it != it_end; ++it)
		{
			Module *m = *it;

			bool show = false;
			Anope::string mtype;

			if (m->type & PROTOCOL)
			{
				show |= protocol;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Protocol";
			}
			if (m->type & PSEUDOCLIENT)
			{
				show |= pseudoclient;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Pseudoclient";
			}
			if (m->type & ENCRYPTION)
			{
				show |= encryption;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Encryption";
			}
			if (m->type & DATABASE)
			{
				show |= database;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Database";
			}
			if (m->type & EXTRA)
			{
				show |= extra;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Extra";
			}
			if (m->type & VENDOR)
			{
				show |= vendor;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Vendor";
			}
			if (m->type & THIRD)
			{
				show |= third;
				if (!mtype.empty())
					mtype += ", ";
				mtype += "Third";
			}

			if (!show)
				continue;
			else if (m->type & PROTOCOL && param.empty() && m != protomod)
				continue;

			++count;

			source.Reply(_("Module: \002{0}\002 [{1}] [{2}]"), m->name, m->version, mtype);
		}

		if (!count)
			source.Reply(_("No modules currently loaded matching that criteria."));
		else
			source.Reply(_("\002{0}\002 modules loaded."), count);
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Lists currently loaded modules."));
		return true;
	}
};

class OSModInfo : public Module
{
	CommandOSModInfo commandosmodinfo;
	CommandOSModList commandosmodlist;

 public:
	OSModInfo(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, commandosmodinfo(this)
		, commandosmodlist(this)
	{

	}
};

MODULE_INIT(OSModInfo)