From cc6e9306e6d936d14724625b735b1be862dd04ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 23 Apr 2015 03:07:48 +0200 Subject: degesch: refactor command handlers --- degesch.c | 88 +++++++++++++++++---------------------------------------------- 1 file changed, 23 insertions(+), 65 deletions(-) diff --git a/degesch.c b/degesch.c index 765fdd7..ead3247 100644 --- a/degesch.c +++ b/degesch.c @@ -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 -- cgit v1.2.3