Command arguments handling + cleanups
Fix bug #185 - Args of !commands lost/damaged. All string arguments were incorrectly freed after being added to the argument list for a command. Instead this fix makes sure it is properly copied into a new string, and by doing so this requires a new API for dealing with hub command arguments in a type safe manner, and also allows for each argument to be cleaned up properly when the command is no longer needed. This also fixes issues with parse errors for certain types, and optional arguments (previously it was impossible to tell the difference for an integer with value 0 or if no integer was given). All arguments can now be accessed through the new functions hub_command_arg_reset() and hub_command_arg_next(). These functions are also exposed to plug-ins. The argument type notations for 'n' has changed to mean nick (string), and 'u' is used for a user (struct hub_user - must be online).
This commit is contained in:
@@ -35,6 +35,19 @@ struct plugin_command
|
||||
struct linked_list* args;
|
||||
};
|
||||
|
||||
struct plugin_command_arg_data
|
||||
{
|
||||
enum plugin_command_arg_type type;
|
||||
union {
|
||||
int integer;
|
||||
char* string;
|
||||
struct plugin_user* user;
|
||||
struct ip_addr_encap* address;
|
||||
struct ip_range* range;
|
||||
enum auth_credentials credentials;
|
||||
} data;
|
||||
};
|
||||
|
||||
typedef int (*plugin_command_handler)(struct plugin_handle*, struct plugin_user* to, struct plugin_command*);
|
||||
|
||||
struct plugin_command_handle
|
||||
@@ -62,18 +75,5 @@ struct plugin_command_handle
|
||||
PTR->description = DESC; \
|
||||
} while (0)
|
||||
|
||||
extern int plugin_command_add(struct plugin_handle*, struct plugin_command_handle*);
|
||||
extern int plugin_command_del(struct plugin_handle*, struct plugin_command_handle*);
|
||||
|
||||
/**
|
||||
* Send a message to a user.
|
||||
* From the user's perspective the message will originate from the hub.
|
||||
*/
|
||||
extern int plugin_command_send_message(struct plugin_handle*, struct plugin_user* to, const char* message);
|
||||
|
||||
/**
|
||||
* Send a reply to a command.
|
||||
*/
|
||||
extern int plugin_command_send_reply(struct plugin_handle*, struct plugin_user* user, struct plugin_command* command, const char* message);
|
||||
|
||||
#endif /* HAVE_UHUB_PLUGIN_API_H */
|
||||
|
||||
@@ -106,6 +106,8 @@ struct plugin_funcs
|
||||
};
|
||||
|
||||
struct plugin_command_handle;
|
||||
struct plugin_command;
|
||||
struct plugin_command_arg_data;
|
||||
|
||||
typedef int (*hfunc_send_message)(struct plugin_handle*, struct plugin_user* user, const char* message);
|
||||
typedef int (*hfunc_send_status)(struct plugin_handle*, struct plugin_user* to, int code, const char* message);
|
||||
@@ -113,6 +115,9 @@ typedef int (*hfunc_user_disconnect)(struct plugin_handle*, struct plugin_user*
|
||||
typedef int (*hfunc_command_add)(struct plugin_handle*, struct plugin_command_handle*);
|
||||
typedef int (*hfunc_command_del)(struct plugin_handle*, struct plugin_command_handle*);
|
||||
|
||||
typedef size_t (*hfunc_command_arg_reset)(struct plugin_handle*, struct plugin_command*);
|
||||
typedef struct plugin_command_arg_data* (*hfunc_command_arg_next)(struct plugin_handle*, struct plugin_command*, enum plugin_command_arg_type);
|
||||
|
||||
/**
|
||||
* These are functions created and initialized by the hub and which can be used
|
||||
* by plugins to access functionality internal to the hub.
|
||||
@@ -124,9 +129,10 @@ struct plugin_hub_funcs
|
||||
hfunc_user_disconnect user_disconnect;
|
||||
hfunc_command_add command_add;
|
||||
hfunc_command_del command_del;
|
||||
hfunc_command_arg_reset command_arg_reset;
|
||||
hfunc_command_arg_next command_arg_next;
|
||||
};
|
||||
|
||||
|
||||
struct plugin_handle
|
||||
{
|
||||
struct uhub_plugin* handle; /* Must NOT be modified by the plugin */
|
||||
|
||||
@@ -93,4 +93,15 @@ struct ban_info
|
||||
time_t expiry; /* Time when the ban record expires */
|
||||
};
|
||||
|
||||
enum plugin_command_arg_type
|
||||
{
|
||||
plugin_cmd_arg_type_integer,
|
||||
plugin_cmd_arg_type_string,
|
||||
plugin_cmd_arg_type_user,
|
||||
plugin_cmd_arg_type_address,
|
||||
plugin_cmd_arg_type_range,
|
||||
plugin_cmd_arg_type_credentials,
|
||||
};
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_PLUGIN_TYPES_H */
|
||||
|
||||
Reference in New Issue
Block a user