commands.h

Go to the documentation of this file.
00001 /* Declarations for command data.
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 #ifndef COMMAND_H
00013 #define COMMAND_H
00014 
00015 #include "service.h"
00016 #include "anope.h"
00017 #include "channels.h"
00018 
00019 struct CommandGroup
00020 {
00021         Anope::string name, description;
00022 };
00023 
00024 /* Used in BotInfo::commands */
00025 struct CommandInfo
00026 {
00027         typedef Anope::map<CommandInfo> map;
00028 
00029         CommandInfo() : hide(false), prepend_channel(false) { }
00030 
00031         /* Service name of the command */
00032         Anope::string name;
00033         /* Permission required to execute the command */
00034         Anope::string permission;
00035         /* Group this command is in */
00036         Anope::string group;
00037         /* whether or not to hide this command in help output */
00038         bool hide;
00039         /* Only used with fantasy */
00040         bool prepend_channel;
00041 };
00042 
00043 /* Where the replies from commands go to. User inheits from this and is the normal
00044  * source of a CommandReply
00045  */
00046 struct CoreExport CommandReply
00047 {
00048         virtual ~CommandReply() { }
00049         virtual void SendMessage(const BotInfo *source, const Anope::string &msg) = 0;
00050 };
00051 
00052 /* The source for a command */
00053 class CoreExport CommandSource
00054 {
00055         /* The nick executing the command */
00056         Anope::string nick;
00057         /* User executing the command, may be NULL */
00058         Reference<User> u;
00059  public:
00060         /* The account executing the command */
00061         Reference<NickCore> nc;
00062         /* Where the reply should go */
00063         CommandReply *reply;
00064         /* Channel the command was executed on (fantasy) */
00065         Reference<Channel> c;
00066         /* The service this command is on */
00067         Reference<BotInfo> service;
00068         /* The actual name of the command being executed */
00069         Anope::string command;
00070         /* The permission of the command being executed */
00071         Anope::string permission;
00072 
00073         CommandSource(const Anope::string &n, User *user, NickCore *core, CommandReply *reply, BotInfo *bi);
00074 
00075         const Anope::string &GetNick() const;
00076         User *GetUser();
00077         NickCore *GetAccount();
00078         AccessGroup AccessFor(ChannelInfo *ci);
00079         bool IsFounder(ChannelInfo *ci);
00080 
00081         void Reply(const char *message, ...);
00082         void Reply(const Anope::string &message);
00083 
00084         bool HasCommand(const Anope::string &cmd);
00085         bool HasPriv(const Anope::string &cmd);
00086         bool IsServicesOper();
00087         bool IsOper();
00088 };
00089 
00092 class CoreExport Command : public Service
00093 {
00094         Anope::string desc;
00095         std::vector<Anope::string> syntax;
00096         /* Allow unregistered users to use this command */
00097         bool allow_unregistered;
00098         /* Command requires that a user is executing it */
00099         bool require_user;
00100 
00101  public:
00102         /* Maximum paramaters accepted by this command */
00103         size_t max_params;
00104         /* Minimum parameters required to use this command */
00105         size_t min_params;
00106 
00107         /* Module which owns us */
00108         Module *module;
00109 
00110  protected:
00118         Command(Module *owner, const Anope::string &sname, size_t min_params, size_t max_params = 0);
00119 
00120  public:
00121         virtual ~Command();
00122 
00123  protected:
00124         void SetDesc(const Anope::string &d);
00125 
00126         void ClearSyntax();
00127         void SetSyntax(const Anope::string &s);
00128         void SendSyntax(CommandSource &);
00129         void SendSyntax(CommandSource &, const Anope::string &syntax);
00130 
00131         void AllowUnregistered(bool b);
00132         void RequireUser(bool b);
00133 
00134  public:
00135         bool AllowUnregistered() const;
00136         bool RequireUser() const;
00137 
00141         const Anope::string &GetDesc() const;
00142 
00147         virtual void Execute(CommandSource &source, const std::vector<Anope::string> &params) = 0;
00148 
00152         virtual void OnServHelp(CommandSource &source);
00153 
00159         virtual bool OnHelp(CommandSource &source, const Anope::string &subcommand);
00160 
00165         virtual void OnSyntaxError(CommandSource &source, const Anope::string &subcommand);
00166 };
00167 
00168 extern CoreExport void RunCommand(CommandSource &source, const Anope::string &message);
00169 
00170 #endif // COMMANDS_H