2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2012-05-01 18:15:43 +00:00
|
|
|
* Copyright (C) 2007-2012, Jan Vidar Krey
|
2009-02-19 16:14:09 +00:00
|
|
|
*
|
|
|
|
* This program 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; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
#ifndef HAVE_UHUB_COMMANDS_H
|
|
|
|
#define HAVE_UHUB_COMMANDS_H
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2011-12-09 09:29:50 +00:00
|
|
|
struct command_base;
|
|
|
|
struct command_handle;
|
2011-12-18 23:34:20 +00:00
|
|
|
struct hub_command;
|
2011-12-09 09:29:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Argument codes are used to automatically parse arguments
|
|
|
|
* for a a hub command.
|
|
|
|
*
|
2012-04-18 21:03:05 +00:00
|
|
|
* u = user (must exist in hub session, or will cause error)
|
|
|
|
* n = nick name (string)
|
2011-12-09 09:29:50 +00:00
|
|
|
* i = CID (must exist in hub)
|
|
|
|
* a = (IP) address (must be a valid IPv4 or IPv6 address)
|
2012-04-18 21:03:05 +00:00
|
|
|
* r = (IP) address range (either: IP-IP or IP/mask, both IPv4 or IPv6 work)
|
2011-12-09 09:29:50 +00:00
|
|
|
* m = message (string)
|
|
|
|
* p = password (string)
|
|
|
|
* C = credentials (see auth_string_to_cred).
|
|
|
|
* c = command (name of command)
|
|
|
|
* N = number (integer)
|
|
|
|
*
|
|
|
|
* Prefix an argument with ? to make it optional.
|
2012-04-18 22:21:49 +00:00
|
|
|
* Prefix with + to make the argument greedy, which causes it to grab the rest of the line ignoring boundaries (only supported for string types).
|
|
|
|
*
|
|
|
|
* NOTE: if an argument is optional then all following arguments must also be optional.
|
|
|
|
* NOTE: You can combine optional and greedy, example: "?+m" would match "", "a", "a b c", etc.
|
2011-12-09 09:29:50 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* "nia" means "nick cid ip"
|
|
|
|
* "n?p" means "nick [password]" where password is optional.
|
2012-04-21 04:08:36 +00:00
|
|
|
* "?N?N" means zero, one, or two integers.
|
|
|
|
* "?NN" means zero or two integers.
|
2012-04-18 22:21:49 +00:00
|
|
|
* "?+m" means an optional string which may contain spaces that would otherwise be split into separate arguments.
|
2011-12-09 09:29:50 +00:00
|
|
|
*/
|
|
|
|
struct command_handle
|
|
|
|
{
|
2011-12-18 23:34:20 +00:00
|
|
|
const char* prefix; /**<<< "Command prefix, for instance 'help' would be the prefix for the !help command." */
|
|
|
|
size_t length; /**<<< "Length of the prefix" */
|
|
|
|
const char* args; /**<<< "Argument codes (see above)" */
|
|
|
|
enum auth_credentials cred; /**<<< "Minimum access level for the command" */
|
|
|
|
command_handler handler; /**<<< "Function pointer for the command" */
|
|
|
|
const char* description; /**<<< "Description for the command" */
|
|
|
|
const char* origin; /**<<< "Name of module where the command is implemented." */
|
|
|
|
void* ptr; /**<<< "A pointer which will be passed along to the handler. @See hub_command::ptr" */
|
2011-12-09 09:29:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns NULL on error, or handle
|
|
|
|
*/
|
|
|
|
extern struct command_base* command_initialize(struct hub_info* hub);
|
|
|
|
extern void command_shutdown(struct command_base* cbase);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new command to the command base.
|
|
|
|
* Returns 1 on success, or 0 on error.
|
|
|
|
*/
|
|
|
|
extern int command_add(struct command_base*, struct command_handle*, void* ptr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a command from the command base.
|
|
|
|
* Returns 1 on success, or 0 on error.
|
|
|
|
*/
|
|
|
|
extern int command_del(struct command_base*, struct command_handle*);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch a message and forward it as a command.
|
|
|
|
* Returns 1 if the message should be forwarded as a chat message, or 0 if
|
|
|
|
* it is supposed to be handled internally in the dispatcher.
|
|
|
|
*
|
|
|
|
* This will break the message down into a struct hub_command and invoke the command handler
|
|
|
|
* for that command if the sufficient access credentials are met.
|
|
|
|
*/
|
|
|
|
extern int command_invoke(struct command_base*, struct hub_user* user, const char* message);
|
2011-12-18 23:34:20 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-01 18:15:43 +00:00
|
|
|
* Returns 1 if the command handle can be used with the given credentials, 0 otherwise.
|
2011-12-18 23:34:20 +00:00
|
|
|
*/
|
2012-05-01 18:15:43 +00:00
|
|
|
int command_is_available(struct command_handle* handle, enum auth_credentials credentials);
|
2011-12-18 23:34:20 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-01 18:15:43 +00:00
|
|
|
* Lookup a command handle based on prefix.
|
|
|
|
* If no matching command handle is found then NULL is returned.
|
2011-12-18 23:34:20 +00:00
|
|
|
*/
|
2012-05-01 18:15:43 +00:00
|
|
|
struct command_handle* command_handler_lookup(struct command_base* cbase, const char* prefix);
|
2011-12-19 09:51:17 +00:00
|
|
|
|
|
|
|
extern void commands_builtin_add(struct command_base*);
|
|
|
|
extern void commands_builtin_remove(struct command_base*);
|
|
|
|
|
2011-12-18 23:34:20 +00:00
|
|
|
#endif /* HAVE_UHUB_COMMANDS_H */
|