anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

commands.h (5855B)

      1 /* Declarations for command data.
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #ifndef COMMAND_H
     13 #define COMMAND_H
     14 
     15 #include "service.h"
     16 #include "anope.h"
     17 #include "channels.h"
     18 
     19 struct CommandGroup
     20 {
     21 	Anope::string name, description;
     22 };
     23 
     24 /* Used in BotInfo::commands */
     25 struct CommandInfo
     26 {
     27 	typedef Anope::map<CommandInfo> map;
     28 
     29 	CommandInfo() : hide(false), prepend_channel(false) { }
     30 
     31 	/* Service name of the command */
     32 	Anope::string name;
     33 	/* Permission required to execute the command */
     34 	Anope::string permission;
     35 	/* Group this command is in */
     36 	Anope::string group;
     37 	/* whether or not to hide this command in help output */
     38 	bool hide;
     39 	/* Only used with fantasy */
     40 	bool prepend_channel;
     41 };
     42 
     43 /* Where the replies from commands go to. User inherits from this and is the normal
     44  * source of a CommandReply
     45  */
     46 struct CoreExport CommandReply
     47 {
     48 	virtual ~CommandReply() { }
     49 	virtual void SendMessage(BotInfo *source, const Anope::string &msg) = 0;
     50 };
     51 
     52 /* The source for a command */
     53 class CoreExport CommandSource
     54 {
     55 	/* The nick executing the command */
     56 	Anope::string nick;
     57 	/* User executing the command, may be NULL */
     58 	Reference<User> u;
     59  public:
     60 	/* The account executing the command */
     61 	Reference<NickCore> nc;
     62 	/* for web clients */
     63 	Anope::string ip;
     64 	/* Where the reply should go */
     65 	CommandReply *reply;
     66 	/* Channel the command was executed on (fantasy) */
     67 	Reference<Channel> c;
     68 	/* The service this command is on */
     69 	Reference<BotInfo> service;
     70 	/* The actual name of the command being executed */
     71 	Anope::string command;
     72 	/* The permission of the command being executed */
     73 	Anope::string permission;
     74 
     75 	CommandSource(const Anope::string &n, User *user, NickCore *core, CommandReply *reply, BotInfo *bi);
     76 
     77 	const Anope::string &GetNick() const;
     78 	User *GetUser();
     79 	NickCore *GetAccount();
     80 	AccessGroup AccessFor(ChannelInfo *ci);
     81 	bool IsFounder(ChannelInfo *ci);
     82 
     83 	void Reply(const char *message, ...);
     84 	void Reply(const Anope::string &message);
     85 
     86 	bool HasCommand(const Anope::string &cmd);
     87 	bool HasPriv(const Anope::string &cmd);
     88 	bool IsServicesOper();
     89 	bool IsOper();
     90 };
     91 
     92 /** Every services command is a class, inheriting from Command.
     93  */
     94 class CoreExport Command : public Service
     95 {
     96 	Anope::string desc;
     97 	std::vector<Anope::string> syntax;
     98 	/* Allow unregistered users to use this command */
     99 	bool allow_unregistered;
    100 	/* Command requires that a user is executing it */
    101 	bool require_user;
    102 
    103  public:
    104 	/* Maximum parameters accepted by this command */
    105 	size_t max_params;
    106 	/* Minimum parameters required to use this command */
    107 	size_t min_params;
    108 
    109 	/* Module which owns us */
    110 	Module *module;
    111 
    112  protected:
    113 	/** Create a new command.
    114 	 * @param owner The owner of the command
    115 	 * @param sname The command name
    116 	 * @param min_params The minimum number of parameters the parser will require to execute this command
    117 	 * @param max_params The maximum number of parameters the parser will create, after max_params, all will be combined into the last argument.
    118 	 * NOTE: If max_params is not set (default), there is no limit to the max number of params.
    119 	 */
    120 	Command(Module *owner, const Anope::string &sname, size_t min_params, size_t max_params = 0);
    121 
    122  public:
    123 	virtual ~Command();
    124 
    125  protected:
    126 	void SetDesc(const Anope::string &d);
    127 
    128 	void ClearSyntax();
    129 	void SetSyntax(const Anope::string &s);
    130 	void SendSyntax(CommandSource &);
    131 
    132 	void AllowUnregistered(bool b);
    133 	void RequireUser(bool b);
    134 
    135  public:
    136 	bool AllowUnregistered() const;
    137 	bool RequireUser() const;
    138 
    139 	/** Get the command description
    140 	 * @param source The source wanting the command description
    141 	 * @return The commands description
    142 	 */
    143 	virtual const Anope::string GetDesc(CommandSource &source) const;
    144 
    145 	/** Execute this command.
    146 	 * @param source The source
    147 	 * @param params Command parameters
    148 	 */
    149 	virtual void Execute(CommandSource &source, const std::vector<Anope::string> &params) = 0;
    150 
    151 	/** Called when HELP is requested for the client this command is on.
    152 	 * @param source The source
    153 	 */
    154 	virtual void OnServHelp(CommandSource &source);
    155 
    156 	/** Requested when the user is requesting help on this command. Help on this command should be sent to the user.
    157 	 * @param source The source
    158 	 * @param subcommand The subcommand the user is requesting help on, or an empty string. (e.g. /ns help set foo bar lol gives a subcommand of "FOO BAR LOL")
    159 	 * @return true if help was provided to the user, false otherwise.
    160 	 */
    161 	virtual bool OnHelp(CommandSource &source, const Anope::string &subcommand);
    162 
    163 	/** Requested when the user provides bad syntax to this command (not enough params, etc).
    164 	 * @param source The source
    165 	 * @param subcommand The subcommand the user tried to use
    166 	 */
    167 	virtual void OnSyntaxError(CommandSource &source, const Anope::string &subcommand);
    168 
    169 	/** Runs a command
    170 	 * @param source The source of the command
    171 	 * @param message The full message to run, the command is at the beginning of the message
    172 	 */
    173 	static void Run(CommandSource &source, const Anope::string &message);
    174 
    175 	void Run(CommandSource &source, const Anope::string &, const CommandInfo &, std::vector<Anope::string> &params);
    176 
    177 	/** Looks up a command name from the service name.
    178 	 * Note that if the same command exists multiple places this will return the first one encountered
    179 	 * @param command_service The command service to lookup, eg, nickserv/register
    180 	 * @param bot If found, is set to the bot the command is on, eg NickServ
    181 	 * @param name If found, is set to the command name, eg REGISTER
    182 	 * @return true if the given command service exists
    183 	 */
    184 	static bool FindCommandFromService(const Anope::string &command_service, BotInfo* &bi, Anope::string &name);
    185 };
    186 
    187 #endif // COMMANDS_H