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

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

void CommandSource::Reply(const char *message, ...)
{
	va_list args;
	char buf[4096]; // Messages can be really big.

	if (message)
	{
		va_start(args, message);
		vsnprintf(buf, sizeof(buf), message, args);

		this->Reply(Anope::string(buf));

		va_end(args);
	}
}

void CommandSource::Reply(const Anope::string &message)
{
	const char *translated_message = translate(this->u, message.c_str());

	sepstream sep(translated_message, '\n');
	Anope::string tok;
	while (sep.GetToken(tok))
		this->reply.push_back(tok);
}

void CommandSource::DoReply()
{
	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->c || !this->c->ci || this->reply.size() > 1)
			u->SendMessage(this->service, message);
		else if (this->c->ci->botflags.HasFlag(BS_MSG_PRIVMSG))
			ircdproto->SendPrivmsg(this->service, this->c->name, message.c_str());
		else if (this->c->ci->botflags.HasFlag(BS_MSG_NOTICE))
			ircdproto->SendNotice(this->service, this->c->name, message.c_str());
		else if (this->c->ci->botflags.HasFlag(BS_MSG_NOTICEOPS))
			ircdproto->SendNoticeChanops(this->service, this->c, message.c_str());
		else
			u->SendMessage(this->service, message);
	}
}

Command::Command(Module *o, const Anope::string &sname, size_t min_params, size_t max_params) : Service(o, sname), Flags<CommandFlag>(CommandFlagStrings), MaxParams(max_params), MinParams(min_params), module(owner)
{
}

Command::~Command()
{
}

void Command::SetDesc(const Anope::string &d)
{
	this->desc = d;
}

void Command::ClearSyntax()
{
	this->syntax.clear();
}

void Command::SetSyntax(const Anope::string &s)
{
	this->syntax.push_back(s);
}

void Command::SendSyntax(CommandSource &source)
{
	if (!this->syntax.empty())
	{
		source.Reply(_("Syntax: \002%s %s\002"), source.command.c_str(), this->syntax[0].c_str());
		for (unsigned i = 1, j = this->syntax.size(); i < j; ++i)
			source.Reply("        \002%s %s\002", source.command.c_str(), this->syntax[i].c_str());
	}
}

void Command::SendSyntax(CommandSource &source, const Anope::string &syn)
{
	source.Reply(_("Syntax: \002%s %s\002"), source.command.c_str(), syn.c_str());
	source.Reply(MORE_INFO, Config->UseStrictPrivMsgString.c_str(), source.owner->nick.c_str(), source.command.c_str());
}

const Anope::string &Command::GetDesc() const
{
	return this->desc;
}

void Command::OnServHelp(CommandSource &source)
{
	source.Reply("    %-14s %s", source.command.c_str(), translate(source.u, (this->GetDesc().c_str())));
}

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

void Command::OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
{
	this->SendSyntax(source);
	source.Reply(MORE_INFO, Config->UseStrictPrivMsgString.c_str(), source.owner->nick.c_str(), source.command.c_str());
}