1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Move functions from cmd_defs to cmd_funcs

This commit is contained in:
James Booth 2016-05-23 00:31:00 +01:00
parent dfe613418f
commit eb105e9172
4 changed files with 186 additions and 179 deletions

View File

@ -103,8 +103,6 @@
#define CMD_NOEXAMPLES { NULL } }
#define CMD_EXAMPLES(...) { __VA_ARGS__, NULL } }
static gboolean _cmd_execute(ProfWin *window, const char *const command, const char *const inp);
GHashTable *commands = NULL;
/*
@ -2241,116 +2239,38 @@ cmd_has_tag(Command *pcmd, const char *const tag)
return FALSE;
}
/*
* Take a line of input and process it, return TRUE if profanity is to
* continue, FALSE otherwise
*/
gboolean
cmd_process_input(ProfWin *window, char *inp)
Command*
cmd_get(const char *const command)
{
log_debug("Input received: %s", inp);
gboolean result = FALSE;
g_strchomp(inp);
// just carry on if no input
if (strlen(inp) == 0) {
result = TRUE;
// handle command if input starts with a '/'
} else if (inp[0] == '/') {
char *inp_cpy = strdup(inp);
char *command = strtok(inp_cpy, " ");
char *question_mark = strchr(command, '?');
if (question_mark) {
*question_mark = '\0';
char *fakeinp;
if (asprintf(&fakeinp, "/help %s", command+1)) {
result = _cmd_execute(window, "/help", fakeinp);
free(fakeinp);
}
} else {
result = _cmd_execute(window, command, inp);
}
free(inp_cpy);
// call a default handler if input didn't start with '/'
if (commands) {
return g_hash_table_lookup(commands, command);
} else {
result = cmd_execute_default(window, inp);
return NULL;
}
return result;
}
// Command execution
void
cmd_execute_connect(ProfWin *window, const char *const account)
GList*
cmd_get_ordered(const char *const tag)
{
GString *command = g_string_new("/connect ");
g_string_append(command, account);
cmd_process_input(window, command->str);
g_string_free(command, TRUE);
}
GList *ordered_commands = NULL;
static gboolean
_cmd_execute(ProfWin *window, const char *const command, const char *const inp)
{
if (g_str_has_prefix(command, "/field") && window->type == WIN_MUC_CONFIG) {
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
if (!result) {
ui_current_print_formatted_line('!', 0, "Invalid command, see /form help");
result = TRUE;
} else {
gchar **tokens = g_strsplit(inp, " ", 2);
char *field = tokens[0] + 1;
result = cmd_form_field(window, field, args);
g_strfreev(tokens);
}
GHashTableIter iter;
gpointer key;
gpointer value;
g_strfreev(args);
return result;
}
Command *cmd = g_hash_table_lookup(commands, command);
gboolean result = FALSE;
if (cmd) {
gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
if (result == FALSE) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
return TRUE;
}
if (args[0] && cmd->sub_funcs[0][0]) {
int i = 0;
while (cmd->sub_funcs[i][0]) {
if (g_strcmp0(args[0], (char*)cmd->sub_funcs[i][0]) == 0) {
gboolean (*func)(ProfWin *window, const char *const command, gchar **args) = cmd->sub_funcs[i][1];
gboolean result = func(window, command, args);
g_strfreev(args);
return result;
}
i++;
g_hash_table_iter_init(&iter, commands);
while (g_hash_table_iter_next(&iter, &key, &value)) {
Command *pcmd = (Command *)value;
if (tag) {
if (cmd_has_tag(pcmd, tag)) {
ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
}
}
if (!cmd->func) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
return TRUE;
}
gboolean result = cmd->func(window, command, args);
g_strfreev(args);
return result;
} else if (plugins_run_command(inp)) {
return TRUE;
} else {
gboolean ran_alias = FALSE;
gboolean alias_result = cmd_execute_alias(window, inp, &ran_alias);
if (!ran_alias) {
return cmd_execute_default(window, inp);
} else {
return alias_result;
ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
}
}
return ordered_commands;
}
static int

View File

@ -39,25 +39,15 @@
#include "ui/ui.h"
GHashTable *commands;
void cmd_init(void);
void cmd_uninit(void);
Command* cmd_get(const char *const command);
GList* cmd_get_ordered(const char *const tag);
gboolean cmd_valid_tag(const char *const str);
gboolean cmd_has_tag(Command *pcmd, const char *const tag);
gboolean cmd_process_input(ProfWin *window, char *inp);
void cmd_execute_connect(ProfWin *window, const char *const account);
GSList* cmd_get_basic_help(void);
GSList* cmd_get_settings_help(void);
GSList* cmd_get_presence_help(void);
void cmd_history_append(char *inp);
char* cmd_history_previous(char *inp);
char* cmd_history_next(char *inp);
void command_docgen(void);
#endif

View File

@ -91,8 +91,120 @@ static void _cmd_set_boolean_preference(gchar *arg, const char *const command,
const char *const display, preference_t pref);
static void _who_room(ProfWin *window, const char *const command, gchar **args);
static void _who_roster(ProfWin *window, const char *const command, gchar **args);
static gboolean _cmd_execute(ProfWin *window, const char *const command, const char *const inp);
static gboolean _cmd_execute_alias(ProfWin *window, const char *const inp, gboolean *ran);
extern GHashTable *commands;
/*
* Take a line of input and process it, return TRUE if profanity is to
* continue, FALSE otherwise
*/
gboolean
cmd_process_input(ProfWin *window, char *inp)
{
log_debug("Input received: %s", inp);
gboolean result = FALSE;
g_strchomp(inp);
// just carry on if no input
if (strlen(inp) == 0) {
result = TRUE;
// handle command if input starts with a '/'
} else if (inp[0] == '/') {
char *inp_cpy = strdup(inp);
char *command = strtok(inp_cpy, " ");
char *question_mark = strchr(command, '?');
if (question_mark) {
*question_mark = '\0';
char *fakeinp;
if (asprintf(&fakeinp, "/help %s", command+1)) {
result = _cmd_execute(window, "/help", fakeinp);
free(fakeinp);
}
} else {
result = _cmd_execute(window, command, inp);
}
free(inp_cpy);
// call a default handler if input didn't start with '/'
} else {
result = cmd_execute_default(window, inp);
}
return result;
}
// Command execution
void
cmd_execute_connect(ProfWin *window, const char *const account)
{
GString *command = g_string_new("/connect ");
g_string_append(command, account);
cmd_process_input(window, command->str);
g_string_free(command, TRUE);
}
static gboolean
_cmd_execute(ProfWin *window, const char *const command, const char *const inp)
{
if (g_str_has_prefix(command, "/field") && window->type == WIN_MUC_CONFIG) {
gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
if (!result) {
ui_current_print_formatted_line('!', 0, "Invalid command, see /form help");
result = TRUE;
} else {
gchar **tokens = g_strsplit(inp, " ", 2);
char *field = tokens[0] + 1;
result = cmd_form_field(window, field, args);
g_strfreev(tokens);
}
g_strfreev(args);
return result;
}
Command *cmd = cmd_get(command);
gboolean result = FALSE;
if (cmd) {
gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
if (result == FALSE) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
return TRUE;
}
if (args[0] && cmd->sub_funcs[0][0]) {
int i = 0;
while (cmd->sub_funcs[i][0]) {
if (g_strcmp0(args[0], (char*)cmd->sub_funcs[i][0]) == 0) {
gboolean (*func)(ProfWin *window, const char *const command, gchar **args) = cmd->sub_funcs[i][1];
gboolean result = func(window, command, args);
g_strfreev(args);
return result;
}
i++;
}
}
if (!cmd->func) {
ui_invalid_command_usage(cmd->cmd, cmd->setting_func);
return TRUE;
}
gboolean result = cmd->func(window, command, args);
g_strfreev(args);
return result;
} else if (plugins_run_command(inp)) {
return TRUE;
} else {
gboolean ran_alias = FALSE;
gboolean alias_result = _cmd_execute_alias(window, inp, &ran_alias);
if (!ran_alias) {
return cmd_execute_default(window, inp);
} else {
return alias_result;
}
}
}
gboolean
cmd_execute_default(ProfWin *window, const char *inp)
@ -161,8 +273,8 @@ cmd_execute_default(ProfWin *window, const char *inp)
return TRUE;
}
gboolean
cmd_execute_alias(ProfWin *window, const char *const inp, gboolean *ran)
static gboolean
_cmd_execute_alias(ProfWin *window, const char *const inp, gboolean *ran)
{
if (inp[0] != '/') {
ran = FALSE;
@ -1523,21 +1635,7 @@ _cmd_help_cmd_list(const char *const tag)
}
g_list_free(plugins_cmds);
} else {
GHashTableIter iter;
gpointer key;
gpointer value;
g_hash_table_iter_init(&iter, commands);
while (g_hash_table_iter_next(&iter, &key, &value)) {
Command *pcmd = (Command *)value;
if (tag) {
if (cmd_has_tag(pcmd, tag)) {
ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
}
} else {
ordered_commands = g_list_insert_sorted(ordered_commands, pcmd->cmd, (GCompareFunc)g_strcmp0);
}
}
ordered_commands = cmd_get_ordered(tag);
// add plugins if showing all commands
if (!tag) {
@ -1608,7 +1706,7 @@ cmd_help(ProfWin *window, const char *const command, gchar **args)
char cmd_with_slash[1 + strlen(cmd) + 1];
sprintf(cmd_with_slash, "/%s", cmd);
Command *command = g_hash_table_lookup(commands, cmd_with_slash);
Command *command = cmd_get(cmd_with_slash);
if (command) {
cons_show_help(cmd_with_slash, &command->help);
} else {
@ -3861,7 +3959,7 @@ cmd_form(ProfWin *window, const char *const command, gchar **args)
mucconfwin_form_help(confwin);
const gchar **help_text = NULL;
Command *command = g_hash_table_lookup(commands, "/form");
Command *command = cmd_get("/form");
if (command) {
help_text = command->help.synopsis;

View File

@ -69,22 +69,12 @@ typedef struct cmd_t {
CommandHelp help;
} Command;
gboolean cmd_execute_alias(ProfWin *window, const char *const inp, gboolean *ran);
gboolean cmd_process_input(ProfWin *window, char *inp);
void cmd_execute_connect(ProfWin *window, const char *const account);
gboolean cmd_execute_default(ProfWin *window, const char *inp);
gboolean cmd_about(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_list(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_show(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_add(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_remove(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_enable(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_disable(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_rename(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_default(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_set(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_clear(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_autoaway(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_autoconnect(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_autoping(ProfWin *window, const char *const command, gchar **args);
@ -96,14 +86,6 @@ gboolean cmd_chlog(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_clear(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_close(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_connect(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_certpath(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_trust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_trusted(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_revoke(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_show(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_cert(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_decline(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_disco(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_sendfile(ProfWin *window, const char *const command, gchar **args);
@ -130,22 +112,6 @@ gboolean cmd_msg(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_nick(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_notify(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_online(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_char(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_log(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_libver(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_policy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_gen(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_myfp(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_theirfp(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_start(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_end(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_trust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_untrust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_secret(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_question(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_answer(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_pgp(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_outtype(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_prefs(ProfWin *window, const char *const command, gchar **args);
@ -168,14 +134,6 @@ gboolean cmd_titlebar(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_vercheck(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_who(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_win(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_unread(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_tidy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_prune(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_swap(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_autotidy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_xa(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_alias(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_xmlconsole(ProfWin *window, const char *const command, gchar **args);
@ -201,6 +159,47 @@ gboolean cmd_console(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_blocked(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_list(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_show(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_add(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_remove(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_enable(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_disable(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_rename(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_default(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_set(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_account_clear(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_certpath(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_trust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_trusted(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_revoke(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_show(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_tls_cert(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_char(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_log(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_libver(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_policy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_gen(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_myfp(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_theirfp(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_start(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_end(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_trust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_untrust(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_secret(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_question(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_otr_answer(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_unread(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_tidy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_prune(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_swap(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_wins_autotidy(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_form_field(ProfWin *window, char *tag, gchar **args);
#endif