summaryrefslogtreecommitdiff
path: root/src/command.cpp
blob: 290f74d1fc54ff2c741ea4135c34be45ca91df45 (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
/*
 * Copyright (C) 2008-2010 Robin Burchell <w00t@inspircd.org>
 * Copyright (C) 2008-2010 Anope Team <team@anope.org>
 *
 * Please read COPYING and README for further details.
 */

#include "services.h"
#include "modules.h"

CommandSource::~CommandSource()
{
	for (std::list<Anope::string>::iterator it = this->reply.begin(), it_end = this->reply.end(); it != it_end; ++it)
	{
		const Anope::string &message = *it;

		// Send to the user if the reply is more than one line
		if (!this->fantasy || !this->ci || this->reply.size() > 1)
			u->SendMessage(this->service, message);
		else if (this->ci->botflags.HasFlag(BS_MSG_PRIVMSG))
			ircdproto->SendPrivmsg(this->service, this->ci->name, message.c_str());
		else if (this->ci->botflags.HasFlag(BS_MSG_NOTICE))
			ircdproto->SendNotice(this->service, this->ci->name, message.c_str());
		else if (this->ci->botflags.HasFlag(BS_MSG_NOTICEOPS))
			ircdproto->SendNoticeChanops(this->service, this->ci->c, message.c_str());
		else
			u->SendMessage(this->service, message);
	}
}

void CommandSource::Reply(LanguageString message, ...)
{
	Anope::string m = GetString(this->u, message);

	m = m.replace_all_cs("%S", this->owner->nick);

	if (m.length() >= 4096)
		Log() << "Warning, language string " << message << " is longer than 4096 bytes";

	va_list args;
	char buf[4096];
	va_start(args, message);
	vsnprintf(buf, sizeof(buf) - 1, m.c_str(), args);
	va_end(args);

	sepstream sep(buf, '\n');
	Anope::string line;

	while (sep.GetToken(line))
		this->Reply(line.empty() ? " " : line.c_str());
}

void CommandSource::Reply(const char *message, ...)
{
	va_list args;
	char buf[BUFSIZE] = "";

	if (message)
	{
		va_start(args, message);
		vsnprintf(buf, BUFSIZE - 1, message, args);

		this->reply.push_back(buf);

		va_end(args);
	}
}

Command::Command(const Anope::string &sname, size_t min_params, size_t max_params, const Anope::string &spermission) : MaxParams(max_params), MinParams(min_params), name(sname), permission(spermission)
{
	this->module = NULL;
	this->service = NULL;
}

Command::~Command()
{
	if (this->module)
		this->module->DelCommand(this->service, this);
}

void Command::OnServHelp(CommandSource &source) { }

bool Command::OnHelp(CommandSource &source, const Anope::string &subcommand) { return false; }

void Command::OnSyntaxError(CommandSource &source, const Anope::string &subcommand) { }

void Command::SetPermission(const Anope::string &reststr)
{
	this->permission = reststr;
}

bool Command::AddSubcommand(Module *creator, Command *c)
{
	return false;
}

bool Command::DelSubcommand(Command *c)
{
	return false;
}

Command *Command::FindSubcommand(const Anope::string &subcommand)
{
	return NULL;
}