diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-23 03:07:48 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-23 03:07:48 +0200 |
commit | cc6e9306e6d936d14724625b735b1be862dd04ac (patch) | |
tree | 9d2d13ca3491e1da9626996d40eb0e592ebef7be | |
parent | 15032f9ce7b8eec42e74428acca12dd7f0e6247a (diff) | |
download | xK-cc6e9306e6d936d14724625b735b1be862dd04ac.tar.gz xK-cc6e9306e6d936d14724625b735b1be862dd04ac.tar.xz xK-cc6e9306e6d936d14724625b735b1be862dd04ac.zip |
degesch: refactor command handlers
-rw-r--r-- | degesch.c | 88 |
1 files changed, 23 insertions, 65 deletions
@@ -2587,6 +2587,19 @@ try_decode_buffer (struct app_context *ctx, const char *word) return buffer; } +static bool +server_command_check (struct app_context *ctx, const char *action) +{ + if (ctx->current_buffer->type == BUFFER_GLOBAL) + buffer_send_error (ctx, ctx->current_buffer, + "Can't do this from a global buffer (%s)", action); + else if (ctx->irc_fd == -1) + buffer_send_error (ctx, ctx->server_buffer, "Not connected"); + else + return true; + return false; +} + static void handle_command_buffer (struct app_context *ctx, char *arguments) { @@ -2657,18 +2670,8 @@ handle_command_buffer (struct app_context *ctx, char *arguments) static void handle_command_msg (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - { - buffer_send_error (ctx, ctx->current_buffer, - "Can't send messages from a global buffer"); + if (!server_command_check (ctx, "send messages")) return; - } - - if (ctx->irc_fd == -1) - { - buffer_send_error (ctx, ctx->server_buffer, "Not connected"); - return; - } if (!*arguments) { @@ -2686,18 +2689,8 @@ handle_command_msg (struct app_context *ctx, char *arguments) static void handle_command_query (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - { - buffer_send_error (ctx, ctx->current_buffer, - "Can't send messages from a global buffer"); + if (!server_command_check (ctx, "send messages")) return; - } - - if (ctx->irc_fd == -1) - { - buffer_send_error (ctx, ctx->server_buffer, "Not connected"); - return; - } if (!*arguments) { @@ -2707,12 +2700,8 @@ handle_command_query (struct app_context *ctx, char *arguments) char *target = cut_word (&arguments); if (irc_is_channel (ctx, target)) - { buffer_send_error (ctx, ctx->server_buffer, "Cannot query a channel"); - return; - } - - if (!*arguments) + else if (!*arguments) buffer_send_error (ctx, ctx->server_buffer, "No text to send"); else { @@ -2724,18 +2713,8 @@ handle_command_query (struct app_context *ctx, char *arguments) static void handle_command_notice (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - { - buffer_send_error (ctx, ctx->current_buffer, - "Can't send messages from a global buffer"); - return; - } - - if (ctx->irc_fd == -1) - { - buffer_send_error (ctx, ctx->server_buffer, "Not connected"); + if (!server_command_check (ctx, "send messages")) return; - } if (!*arguments) { @@ -2766,18 +2745,8 @@ handle_command_quit (struct app_context *ctx, char *arguments) static void handle_command_join (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - { - buffer_send_error (ctx, ctx->current_buffer, - "Can't join from a global buffer"); + if (!server_command_check (ctx, "join")) return; - } - - if (ctx->irc_fd == -1) - { - buffer_send_error (ctx, ctx->server_buffer, "Not connected"); - return; - } if (*arguments) // TODO: check if the arguments are in the form of @@ -2803,18 +2772,8 @@ handle_command_join (struct app_context *ctx, char *arguments) static void handle_command_part (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - { - buffer_send_error (ctx, ctx->current_buffer, - "Can't part from a global buffer"); + if (!server_command_check (ctx, "part")) return; - } - - if (ctx->irc_fd == -1) - { - buffer_send_error (ctx, ctx->server_buffer, "Not connected"); - return; - } if (*arguments) // TODO: check if the arguments are in the form of "channel(,channel)*" @@ -2838,11 +2797,10 @@ handle_command_part (struct app_context *ctx, char *arguments) static void handle_command_quote (struct app_context *ctx, char *arguments) { - if (ctx->current_buffer->type == BUFFER_GLOBAL) - buffer_send_error (ctx, ctx->current_buffer, - "Can't do this from a global buffer"); - else - irc_send (ctx, arguments); + if (!server_command_check (ctx, "quote")) + return; + + irc_send (ctx, arguments); } static struct command_handler |