From 5be1cad4da5f5c72239a04c04bcab3afe28c2abe Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Fri, 19 Jun 2015 22:54:10 +0200 Subject: degesch: get rid of some repeating code --- degesch.c | 173 ++++++++++++++++++++------------------------------------------ 1 file changed, 55 insertions(+), 118 deletions(-) diff --git a/degesch.c b/degesch.c index 37fc014..ed4a902 100644 --- a/degesch.c +++ b/degesch.c @@ -5763,6 +5763,22 @@ server_command_check (struct app_context *ctx, const char *action, return false; } +#define SERVER_COMMAND(name, need_registration) \ + if (!server_command_check (ctx, (name), (need_registration))) \ + return true; \ + struct server *s = ctx->current_buffer->server; + +#define CHANNEL_COMMAND(name, need_registration) \ + SERVER_COMMAND ((name), (need_registration)) \ + char *channel_name = try_get_channel (ctx, &arguments); \ + if (!channel_name) \ + { \ + buffer_send_error (ctx, ctx->current_buffer, \ + "Can't %s: %s", (name), \ + "no channel name given and this buffer is not a channel"); \ + return true; \ + } + static bool validate_channel_name (void *user_data, char *word) { @@ -6046,12 +6062,11 @@ handle_command_save (struct app_context *ctx, char *arguments) static bool handle_command_msg (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "send messages", true)) - return true; + SERVER_COMMAND ("send messages", true) + if (!*arguments) return false; - struct server *s = ctx->current_buffer->server; char *target = cut_word (&arguments); if (!*arguments) buffer_send_error (ctx, s->buffer, "No text to send"); @@ -6063,12 +6078,11 @@ handle_command_msg (struct app_context *ctx, char *arguments) static bool handle_command_query (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "send messages", true)) - return true; + SERVER_COMMAND ("send messages", true) + if (!*arguments) return false; - struct server *s = ctx->current_buffer->server; char *target = cut_word (&arguments); if (irc_is_channel (s, target)) buffer_send_error (ctx, s->buffer, "Cannot query a channel"); @@ -6085,12 +6099,11 @@ handle_command_query (struct app_context *ctx, char *arguments) static bool handle_command_notice (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "send messages", true)) - return true; + SERVER_COMMAND ("send messages", true) + if (!*arguments) return false; - struct server *s = ctx->current_buffer->server; char *target = cut_word (&arguments); if (!*arguments) buffer_send_error (ctx, s->buffer, "No text to send"); @@ -6102,8 +6115,8 @@ handle_command_notice (struct app_context *ctx, char *arguments) static bool handle_command_ctcp (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "send messages", true)) - return true; + SERVER_COMMAND ("send messages", true) + if (!*arguments) return false; @@ -6115,7 +6128,6 @@ handle_command_ctcp (struct app_context *ctx, char *arguments) for (char *p = tag; *p; p++) *p = toupper_ascii (*p); - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "PRIVMSG %s :\x01%s %s\x01", target, tag, arguments); else @@ -6129,10 +6141,8 @@ handle_command_ctcp (struct app_context *ctx, char *arguments) static bool handle_command_me (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "send messages", true)) - return true; + SERVER_COMMAND ("send messages", true) - struct server *s = ctx->current_buffer->server; if (ctx->current_buffer->type == BUFFER_CHANNEL) SEND_AUTOSPLIT_ACTION (s, ctx->current_buffer->channel->name, arguments); @@ -6166,10 +6176,8 @@ handle_command_quit (struct app_context *ctx, char *arguments) static bool handle_command_join (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "join", true)) - return true; + SERVER_COMMAND ("join", true) - struct server *s = ctx->current_buffer->server; // XXX: send the last known channel key? if (irc_is_channel (s, arguments)) // XXX: we may want to split the list of channels @@ -6194,10 +6202,8 @@ handle_command_join (struct app_context *ctx, char *arguments) static bool handle_command_part (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "part", true)) - return true; + SERVER_COMMAND ("part", true) - struct server *s = ctx->current_buffer->server; if (irc_is_channel (s, arguments)) { struct str_vector v; @@ -6251,10 +6257,8 @@ cycle_channel (struct server *s, const char *channel_name, const char *reason) static bool handle_command_cycle (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "cycle", true)) - return true; + SERVER_COMMAND ("cycle", true) - struct server *s = ctx->current_buffer->server; if (irc_is_channel (s, arguments)) { struct str_vector v; @@ -6308,16 +6312,9 @@ static bool handle_command_channel_mode (struct app_context *ctx, char *arguments, const char *command_name, bool adding, char mode_char) { - if (!server_command_check (ctx, command_name, true)) - return true; + CHANNEL_COMMAND (command_name, true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't set mode", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) { struct str_vector v; str_vector_init (&v); @@ -6344,10 +6341,7 @@ CHANMODE_HANDLER (voice, true, 'v') CHANMODE_HANDLER (devoice, false, 'v') static bool handle_command_mode (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "mode", true)) - return true; - - struct server *s = ctx->current_buffer->server; + SERVER_COMMAND ("mode", true) // Channel names prefixed by "+" collide with mode strings, // so we just disallow specifying these channels @@ -6381,16 +6375,10 @@ handle_command_mode (struct app_context *ctx, char *arguments) static bool handle_command_topic (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "topic", true)) - return true; + // FIXME: we want "change topic" in the other error message + CHANNEL_COMMAND ("topic", true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't change topic", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) // FIXME: there's no way to unset the topic irc_send (s, "TOPIC %s :%s", channel_name, arguments); else @@ -6401,16 +6389,9 @@ handle_command_topic (struct app_context *ctx, char *arguments) static bool handle_command_kick (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "kick", true)) - return true; + CHANNEL_COMMAND ("kick", true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't kick", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) { char *target = cut_word (&arguments); if (*arguments) @@ -6426,16 +6407,9 @@ handle_command_kick (struct app_context *ctx, char *arguments) static bool handle_command_kickban (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "kickban", true)) - return true; + CHANNEL_COMMAND ("kickban", true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't kickban", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) { char *target = cut_word (&arguments); if (strpbrk (target, "!@*?")) @@ -6472,16 +6446,9 @@ complete_user_masks (struct str_vector *v) static bool handle_command_ban (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "ban", true)) - return true; + CHANNEL_COMMAND ("ban", true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't ban", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) { struct str_vector v; str_vector_init (&v); @@ -6498,16 +6465,9 @@ handle_command_ban (struct app_context *ctx, char *arguments) static bool handle_command_unban (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "unban", true)) - return true; + CHANNEL_COMMAND ("unban", true) - struct server *s = ctx->current_buffer->server; - char *channel_name = try_get_channel (ctx, &arguments); - if (!channel_name) - buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't unban", - "no channel name given and this buffer is not a channel"); - else if (*arguments) + if (*arguments) { struct str_vector v; str_vector_init (&v); @@ -6524,10 +6484,7 @@ handle_command_unban (struct app_context *ctx, char *arguments) static bool handle_command_invite (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "invite", true)) - return true; - - struct server *s = ctx->current_buffer->server; + SERVER_COMMAND ("invite", true) struct str_vector v; str_vector_init (&v); @@ -6543,7 +6500,7 @@ handle_command_invite (struct app_context *ctx, char *arguments) bool result = true; if (!channel_name) buffer_send_error (ctx, ctx->current_buffer, - "%s: %s", "Can't invite", + "Can't %s: %s", "invite", "no channel name given and this buffer is not a channel"); else if (v.len) for (size_t i = 0; i < v.len; i++) @@ -6625,10 +6582,8 @@ handle_command_disconnect (struct app_context *ctx, char *arguments) static bool handle_command_list (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "list channels", true)) - return true; + SERVER_COMMAND ("list channels", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "LIST %s", arguments); else @@ -6639,10 +6594,8 @@ handle_command_list (struct app_context *ctx, char *arguments) static bool handle_command_names (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "names", true)) - return true; + SERVER_COMMAND ("names", true) - struct server *s = ctx->current_buffer->server; char *channel_name = try_get_channel (ctx, &arguments); if (!channel_name) irc_send (s, "NAMES"); @@ -6654,10 +6607,8 @@ handle_command_names (struct app_context *ctx, char *arguments) static bool handle_command_who (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "who", true)) - return true; + SERVER_COMMAND ("who", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "WHO %s", arguments); else @@ -6668,10 +6619,8 @@ handle_command_who (struct app_context *ctx, char *arguments) static bool handle_command_whois (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "whois", true)) - return true; + SERVER_COMMAND ("whois", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "WHOIS %s", arguments); else if (ctx->current_buffer->type == BUFFER_PM) @@ -6688,10 +6637,8 @@ handle_command_whois (struct app_context *ctx, char *arguments) static bool handle_command_whowas (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "whowas", true)) - return true; + SERVER_COMMAND ("whowas", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "WHOWAS %s", arguments); else if (ctx->current_buffer->type == BUFFER_PM) @@ -6706,10 +6653,8 @@ handle_command_whowas (struct app_context *ctx, char *arguments) static bool handle_command_motd (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "motd", true)) - return true; + SERVER_COMMAND ("motd", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "MOTD %s", arguments); else @@ -6720,10 +6665,8 @@ handle_command_motd (struct app_context *ctx, char *arguments) static bool handle_command_stats (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "stats", true)) - return true; + SERVER_COMMAND ("stats", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "STATS %s", arguments); else @@ -6734,10 +6677,8 @@ handle_command_stats (struct app_context *ctx, char *arguments) static bool handle_command_away (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "away", true)) - return true; + SERVER_COMMAND ("away", true) - struct server *s = ctx->current_buffer->server; if (*arguments) irc_send (s, "AWAY %s", arguments); else @@ -6748,12 +6689,11 @@ handle_command_away (struct app_context *ctx, char *arguments) static bool handle_command_nick (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "change nickname", false)) - return true; + SERVER_COMMAND ("change nickname", false) + if (!*arguments) return false; - struct server *s = ctx->current_buffer->server; irc_send (s, "NICK %s", cut_word (&arguments)); return true; } @@ -6761,10 +6701,7 @@ handle_command_nick (struct app_context *ctx, char *arguments) static bool handle_command_quote (struct app_context *ctx, char *arguments) { - if (!server_command_check (ctx, "quote", false)) - return true; - - struct server *s = ctx->current_buffer->server; + SERVER_COMMAND ("quote", false) irc_send (s, "%s", arguments); return true; } -- cgit v1.2.3-70-g09d2