degesch: get rid of some repeating code

This commit is contained in:
Přemysl Eric Janouch 2015-06-19 22:54:10 +02:00
parent 64725b24bd
commit 5be1cad4da
1 changed files with 55 additions and 118 deletions

173
degesch.c
View File

@ -5763,6 +5763,22 @@ server_command_check (struct app_context *ctx, const char *action,
return false; 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 static bool
validate_channel_name (void *user_data, char *word) validate_channel_name (void *user_data, char *word)
{ {
@ -6046,12 +6062,11 @@ handle_command_save (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_msg (struct app_context *ctx, char *arguments) handle_command_msg (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "send messages", true)) SERVER_COMMAND ("send messages", true)
return true;
if (!*arguments) if (!*arguments)
return false; return false;
struct server *s = ctx->current_buffer->server;
char *target = cut_word (&arguments); char *target = cut_word (&arguments);
if (!*arguments) if (!*arguments)
buffer_send_error (ctx, s->buffer, "No text to send"); 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 static bool
handle_command_query (struct app_context *ctx, char *arguments) handle_command_query (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "send messages", true)) SERVER_COMMAND ("send messages", true)
return true;
if (!*arguments) if (!*arguments)
return false; return false;
struct server *s = ctx->current_buffer->server;
char *target = cut_word (&arguments); char *target = cut_word (&arguments);
if (irc_is_channel (s, target)) if (irc_is_channel (s, target))
buffer_send_error (ctx, s->buffer, "Cannot query a channel"); 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 static bool
handle_command_notice (struct app_context *ctx, char *arguments) handle_command_notice (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "send messages", true)) SERVER_COMMAND ("send messages", true)
return true;
if (!*arguments) if (!*arguments)
return false; return false;
struct server *s = ctx->current_buffer->server;
char *target = cut_word (&arguments); char *target = cut_word (&arguments);
if (!*arguments) if (!*arguments)
buffer_send_error (ctx, s->buffer, "No text to send"); 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 static bool
handle_command_ctcp (struct app_context *ctx, char *arguments) handle_command_ctcp (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "send messages", true)) SERVER_COMMAND ("send messages", true)
return true;
if (!*arguments) if (!*arguments)
return false; return false;
@ -6115,7 +6128,6 @@ handle_command_ctcp (struct app_context *ctx, char *arguments)
for (char *p = tag; *p; p++) for (char *p = tag; *p; p++)
*p = toupper_ascii (*p); *p = toupper_ascii (*p);
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "PRIVMSG %s :\x01%s %s\x01", target, tag, arguments); irc_send (s, "PRIVMSG %s :\x01%s %s\x01", target, tag, arguments);
else else
@ -6129,10 +6141,8 @@ handle_command_ctcp (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_me (struct app_context *ctx, char *arguments) handle_command_me (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "send messages", true)) SERVER_COMMAND ("send messages", true)
return true;
struct server *s = ctx->current_buffer->server;
if (ctx->current_buffer->type == BUFFER_CHANNEL) if (ctx->current_buffer->type == BUFFER_CHANNEL)
SEND_AUTOSPLIT_ACTION (s, SEND_AUTOSPLIT_ACTION (s,
ctx->current_buffer->channel->name, arguments); ctx->current_buffer->channel->name, arguments);
@ -6166,10 +6176,8 @@ handle_command_quit (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_join (struct app_context *ctx, char *arguments) handle_command_join (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "join", true)) SERVER_COMMAND ("join", true)
return true;
struct server *s = ctx->current_buffer->server;
// XXX: send the last known channel key? // XXX: send the last known channel key?
if (irc_is_channel (s, arguments)) if (irc_is_channel (s, arguments))
// XXX: we may want to split the list of channels // 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 static bool
handle_command_part (struct app_context *ctx, char *arguments) handle_command_part (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "part", true)) SERVER_COMMAND ("part", true)
return true;
struct server *s = ctx->current_buffer->server;
if (irc_is_channel (s, arguments)) if (irc_is_channel (s, arguments))
{ {
struct str_vector v; struct str_vector v;
@ -6251,10 +6257,8 @@ cycle_channel (struct server *s, const char *channel_name, const char *reason)
static bool static bool
handle_command_cycle (struct app_context *ctx, char *arguments) handle_command_cycle (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "cycle", true)) SERVER_COMMAND ("cycle", true)
return true;
struct server *s = ctx->current_buffer->server;
if (irc_is_channel (s, arguments)) if (irc_is_channel (s, arguments))
{ {
struct str_vector v; struct str_vector v;
@ -6308,16 +6312,9 @@ static bool
handle_command_channel_mode (struct app_context *ctx, char *arguments, handle_command_channel_mode (struct app_context *ctx, char *arguments,
const char *command_name, bool adding, char mode_char) const char *command_name, bool adding, char mode_char)
{ {
if (!server_command_check (ctx, command_name, true)) CHANNEL_COMMAND (command_name, true)
return true;
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
{ {
struct str_vector v; struct str_vector v;
str_vector_init (&v); str_vector_init (&v);
@ -6344,10 +6341,7 @@ CHANMODE_HANDLER (voice, true, 'v') CHANMODE_HANDLER (devoice, false, 'v')
static bool static bool
handle_command_mode (struct app_context *ctx, char *arguments) handle_command_mode (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "mode", true)) SERVER_COMMAND ("mode", true)
return true;
struct server *s = ctx->current_buffer->server;
// Channel names prefixed by "+" collide with mode strings, // Channel names prefixed by "+" collide with mode strings,
// so we just disallow specifying these channels // so we just disallow specifying these channels
@ -6381,16 +6375,10 @@ handle_command_mode (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_topic (struct app_context *ctx, char *arguments) handle_command_topic (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "topic", true)) // FIXME: we want "change topic" in the other error message
return true; CHANNEL_COMMAND ("topic", true)
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
// FIXME: there's no way to unset the topic // FIXME: there's no way to unset the topic
irc_send (s, "TOPIC %s :%s", channel_name, arguments); irc_send (s, "TOPIC %s :%s", channel_name, arguments);
else else
@ -6401,16 +6389,9 @@ handle_command_topic (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_kick (struct app_context *ctx, char *arguments) handle_command_kick (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "kick", true)) CHANNEL_COMMAND ("kick", true)
return true;
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
{ {
char *target = cut_word (&arguments); char *target = cut_word (&arguments);
if (*arguments) if (*arguments)
@ -6426,16 +6407,9 @@ handle_command_kick (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_kickban (struct app_context *ctx, char *arguments) handle_command_kickban (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "kickban", true)) CHANNEL_COMMAND ("kickban", true)
return true;
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
{ {
char *target = cut_word (&arguments); char *target = cut_word (&arguments);
if (strpbrk (target, "!@*?")) if (strpbrk (target, "!@*?"))
@ -6472,16 +6446,9 @@ complete_user_masks (struct str_vector *v)
static bool static bool
handle_command_ban (struct app_context *ctx, char *arguments) handle_command_ban (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "ban", true)) CHANNEL_COMMAND ("ban", true)
return true;
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
{ {
struct str_vector v; struct str_vector v;
str_vector_init (&v); str_vector_init (&v);
@ -6498,16 +6465,9 @@ handle_command_ban (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_unban (struct app_context *ctx, char *arguments) handle_command_unban (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "unban", true)) CHANNEL_COMMAND ("unban", true)
return true;
struct server *s = ctx->current_buffer->server; if (*arguments)
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)
{ {
struct str_vector v; struct str_vector v;
str_vector_init (&v); str_vector_init (&v);
@ -6524,10 +6484,7 @@ handle_command_unban (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_invite (struct app_context *ctx, char *arguments) handle_command_invite (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "invite", true)) SERVER_COMMAND ("invite", true)
return true;
struct server *s = ctx->current_buffer->server;
struct str_vector v; struct str_vector v;
str_vector_init (&v); str_vector_init (&v);
@ -6543,7 +6500,7 @@ handle_command_invite (struct app_context *ctx, char *arguments)
bool result = true; bool result = true;
if (!channel_name) if (!channel_name)
buffer_send_error (ctx, ctx->current_buffer, 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"); "no channel name given and this buffer is not a channel");
else if (v.len) else if (v.len)
for (size_t i = 0; i < v.len; i++) for (size_t i = 0; i < v.len; i++)
@ -6625,10 +6582,8 @@ handle_command_disconnect (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_list (struct app_context *ctx, char *arguments) handle_command_list (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "list channels", true)) SERVER_COMMAND ("list channels", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "LIST %s", arguments); irc_send (s, "LIST %s", arguments);
else else
@ -6639,10 +6594,8 @@ handle_command_list (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_names (struct app_context *ctx, char *arguments) handle_command_names (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "names", true)) SERVER_COMMAND ("names", true)
return true;
struct server *s = ctx->current_buffer->server;
char *channel_name = try_get_channel (ctx, &arguments); char *channel_name = try_get_channel (ctx, &arguments);
if (!channel_name) if (!channel_name)
irc_send (s, "NAMES"); irc_send (s, "NAMES");
@ -6654,10 +6607,8 @@ handle_command_names (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_who (struct app_context *ctx, char *arguments) handle_command_who (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "who", true)) SERVER_COMMAND ("who", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "WHO %s", arguments); irc_send (s, "WHO %s", arguments);
else else
@ -6668,10 +6619,8 @@ handle_command_who (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_whois (struct app_context *ctx, char *arguments) handle_command_whois (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "whois", true)) SERVER_COMMAND ("whois", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "WHOIS %s", arguments); irc_send (s, "WHOIS %s", arguments);
else if (ctx->current_buffer->type == BUFFER_PM) else if (ctx->current_buffer->type == BUFFER_PM)
@ -6688,10 +6637,8 @@ handle_command_whois (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_whowas (struct app_context *ctx, char *arguments) handle_command_whowas (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "whowas", true)) SERVER_COMMAND ("whowas", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "WHOWAS %s", arguments); irc_send (s, "WHOWAS %s", arguments);
else if (ctx->current_buffer->type == BUFFER_PM) else if (ctx->current_buffer->type == BUFFER_PM)
@ -6706,10 +6653,8 @@ handle_command_whowas (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_motd (struct app_context *ctx, char *arguments) handle_command_motd (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "motd", true)) SERVER_COMMAND ("motd", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "MOTD %s", arguments); irc_send (s, "MOTD %s", arguments);
else else
@ -6720,10 +6665,8 @@ handle_command_motd (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_stats (struct app_context *ctx, char *arguments) handle_command_stats (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "stats", true)) SERVER_COMMAND ("stats", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "STATS %s", arguments); irc_send (s, "STATS %s", arguments);
else else
@ -6734,10 +6677,8 @@ handle_command_stats (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_away (struct app_context *ctx, char *arguments) handle_command_away (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "away", true)) SERVER_COMMAND ("away", true)
return true;
struct server *s = ctx->current_buffer->server;
if (*arguments) if (*arguments)
irc_send (s, "AWAY %s", arguments); irc_send (s, "AWAY %s", arguments);
else else
@ -6748,12 +6689,11 @@ handle_command_away (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_nick (struct app_context *ctx, char *arguments) handle_command_nick (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "change nickname", false)) SERVER_COMMAND ("change nickname", false)
return true;
if (!*arguments) if (!*arguments)
return false; return false;
struct server *s = ctx->current_buffer->server;
irc_send (s, "NICK %s", cut_word (&arguments)); irc_send (s, "NICK %s", cut_word (&arguments));
return true; return true;
} }
@ -6761,10 +6701,7 @@ handle_command_nick (struct app_context *ctx, char *arguments)
static bool static bool
handle_command_quote (struct app_context *ctx, char *arguments) handle_command_quote (struct app_context *ctx, char *arguments)
{ {
if (!server_command_check (ctx, "quote", false)) SERVER_COMMAND ("quote", false)
return true;
struct server *s = ctx->current_buffer->server;
irc_send (s, "%s", arguments); irc_send (s, "%s", arguments);
return true; return true;
} }