From 0875bbfba7b46c77699d15a29522e663bddc41e0 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 20 Jun 2015 20:13:37 +0200
Subject: degesch: further cleanups
---
degesch.c | 69 +++++++++++++++++++++++++++------------------------------------
1 file changed, 29 insertions(+), 40 deletions(-)
diff --git a/degesch.c b/degesch.c
index 079e750..8345ff0 100644
--- a/degesch.c
+++ b/degesch.c
@@ -6192,6 +6192,15 @@ handle_command_join (struct app_context *ctx, struct handler_args *a)
return true;
}
+static void
+part_channel (struct server *s, const char *channel_name, const char *reason)
+{
+ if (*reason)
+ irc_send (s, "PART %s :%s", channel_name, reason);
+ else
+ irc_send (s, "PART %s", channel_name);
+}
+
static bool
handle_command_part (struct app_context *ctx, struct handler_args *a)
{
@@ -6201,13 +6210,7 @@ handle_command_part (struct app_context *ctx, struct handler_args *a)
str_vector_init (&v);
split_str_ignore_empty (cut_word (&a->arguments), ' ', &v);
for (size_t i = 0; i < v.len; i++)
- {
- // TODO: move this out
- if (*a->arguments)
- irc_send (a->s, "PART %s :%s", v.vector[i], a->arguments);
- else
- irc_send (a->s, "PART %s", v.vector[i]);
- }
+ part_channel (a->s, v.vector[i], a->arguments);
str_vector_free (&v);
}
else if (a->buffer->type != BUFFER_CHANNEL)
@@ -6218,10 +6221,8 @@ handle_command_part (struct app_context *ctx, struct handler_args *a)
else if (!a->buffer->channel->users)
buffer_send_error (ctx, a->buffer,
"%s: %s", "Can't part", "you're not on the channel");
- else if (*a->arguments)
- irc_send (a->s, "PART %s :%s", a->buffer->channel->name, a->arguments);
else
- irc_send (a->s, "PART %s", a->buffer->channel->name);
+ part_channel (a->s, a->buffer->channel->name, a->arguments);
return true;
}
@@ -6234,7 +6235,7 @@ cycle_channel (struct server *s, const char *channel_name, const char *reason)
if ((channel = str_map_find (&s->irc_channels, channel_name)))
key = str_map_find (&channel->param_modes, "k");
- if (reason)
+ if (*reason)
irc_send (s, "PART %s :%s", channel_name, reason);
else
irc_send (s, "PART %s", channel_name);
@@ -6254,9 +6255,7 @@ handle_command_cycle (struct app_context *ctx, struct handler_args *a)
str_vector_init (&v);
split_str_ignore_empty (cut_word (&a->arguments), ' ', &v);
for (size_t i = 0; i < v.len; i++)
- // TODO: just send the arguments, also elsewhere
- cycle_channel (a->s, v.vector[i],
- *a->arguments ? a->arguments : NULL);
+ cycle_channel (a->s, v.vector[i], a->arguments);
str_vector_free (&v);
}
else if (a->buffer->type != BUFFER_CHANNEL)
@@ -6267,10 +6266,8 @@ handle_command_cycle (struct app_context *ctx, struct handler_args *a)
else if (!a->buffer->channel->users)
buffer_send_error (ctx, a->buffer,
"%s: %s", "Can't cycle", "you're not on the channel");
- else if (*a->arguments)
- cycle_channel (a->s, a->buffer->channel->name, a->arguments);
else
- cycle_channel (a->s, a->buffer->channel->name, NULL);
+ cycle_channel (a->s, a->buffer->channel->name, a->arguments);
return true;
}
@@ -6448,25 +6445,31 @@ handle_command_invite (struct app_context *ctx, struct handler_args *a)
return result;
}
-static bool
-handle_command_connect (struct app_context *ctx, struct handler_args *a)
+static struct server *
+resolve_server (struct app_context *ctx, struct handler_args *a,
+ const char *command_name)
{
struct server *s = NULL;
if (*a->arguments)
{
char *server_name = cut_word (&a->arguments);
if (!(s = str_map_find (&ctx->servers, server_name)))
- buffer_send_error (ctx, ctx->global_buffer, "%s: %s: %s",
- "Can't connect", "no such server", server_name);
+ buffer_send_error (ctx, ctx->global_buffer, "/%s: %s: %s",
+ command_name, "no such server", server_name);
}
else if (a->buffer->type == BUFFER_GLOBAL)
- buffer_send_error (ctx, a->buffer,
- "%s: %s", "Can't connect",
- "no server name given and this buffer is global");
+ buffer_send_error (ctx, a->buffer, "/%s: %s",
+ command_name, "no server name given and this buffer is global");
else
s = a->buffer->server;
+ return s;
+}
- if (!s)
+static bool
+handle_command_connect (struct app_context *ctx, struct handler_args *a)
+{
+ struct server *s = NULL;
+ if (!(s = resolve_server (ctx, a, "connect")))
return true;
if (irc_is_connected (s))
@@ -6486,21 +6489,7 @@ static bool
handle_command_disconnect (struct app_context *ctx, struct handler_args *a)
{
struct server *s = NULL;
- if (*a->arguments)
- {
- char *server_name = cut_word (&a->arguments);
- if (!(s = str_map_find (&ctx->servers, server_name)))
- buffer_send_error (ctx, a->buffer, "%s: %s: %s",
- "Can't disconnect", "no such server", server_name);
- }
- else if (a->buffer->type == BUFFER_GLOBAL)
- buffer_send_error (ctx, a->buffer,
- "%s: %s", "Can't disconnect",
- "no server name given and this buffer is global");
- else
- s = a->buffer->server;
-
- if (!s)
+ if (!(s = resolve_server (ctx, a, "disconnect")))
return true;
if (s->state == IRC_CONNECTING)
--
cgit v1.2.3-70-g09d2