degesch: implement /server remove

This commit is contained in:
Přemysl Eric Janouch 2015-07-14 06:50:39 +02:00
parent 880a8ef080
commit c573435996
1 changed files with 59 additions and 2 deletions

View File

@ -1269,6 +1269,10 @@ server_free (struct server *self)
str_free (&self->read_buffer); str_free (&self->read_buffer);
str_free (&self->write_buffer); str_free (&self->write_buffer);
poller_timer_reset (&self->ping_tmr);
poller_timer_reset (&self->timeout_tmr);
poller_timer_reset (&self->reconnect_tmr);
str_map_free (&self->irc_users); str_map_free (&self->irc_users);
str_map_free (&self->irc_channels); str_map_free (&self->irc_channels);
str_map_free (&self->irc_buffer_map); str_map_free (&self->irc_buffer_map);
@ -2917,7 +2921,6 @@ buffer_remove (struct app_context *ctx, struct buffer *buffer)
{ {
hard_assert (buffer != ctx->current_buffer); hard_assert (buffer != ctx->current_buffer);
hard_assert (buffer != ctx->global_buffer); hard_assert (buffer != ctx->global_buffer);
hard_assert (buffer->type != BUFFER_SERVER);
input_destroy_buffer (&ctx->input, buffer->input_data); input_destroy_buffer (&ctx->input, buffer->input_data);
buffer->input_data = NULL; buffer->input_data = NULL;
@ -2931,6 +2934,8 @@ buffer_remove (struct app_context *ctx, struct buffer *buffer)
if (buffer == ctx->last_buffer) if (buffer == ctx->last_buffer)
ctx->last_buffer = NULL; ctx->last_buffer = NULL;
if (buffer->type == BUFFER_SERVER)
buffer->server->buffer = NULL;
str_map_set (&ctx->buffers_by_name, buffer->name, NULL); str_map_set (&ctx->buffers_by_name, buffer->name, NULL);
LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer); LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
@ -6557,6 +6562,35 @@ server_add_new (struct app_context *ctx, const char *name)
} }
} }
static void
server_remove (struct app_context *ctx, struct server *s)
{
hard_assert (!irc_is_connected (s));
if (s->buffer)
buffer_remove_safe (ctx, s->buffer);
struct str_map_iter iter;
str_map_iter_init (&iter, &s->irc_buffer_map);
struct buffer *buffer, *next = str_map_iter_next (&iter);
for (buffer = next; buffer; buffer = next)
{
next = str_map_iter_next (&iter);
buffer_remove_safe (ctx, buffer);
}
hard_assert (!s->buffer);
hard_assert (!s->irc_buffer_map.len);
hard_assert (!s->irc_channels.len);
soft_assert (!s->irc_users.len);
str_map_set (get_servers_config (ctx), s->name, NULL);
s->config = NULL;
// This actually destroys the server as it's owned by the map
str_map_set (&ctx->servers, s->name, NULL);
}
// --- User input handling ----------------------------------------------------- // --- User input handling -----------------------------------------------------
// HANDLER_NEEDS_REG is primarily for message sending commands, // HANDLER_NEEDS_REG is primarily for message sending commands,
@ -7478,7 +7512,30 @@ handle_server_add (struct handler_args *a)
struct app_context *ctx = a->ctx; struct app_context *ctx = a->ctx;
char *name = cut_word (&a->arguments); char *name = cut_word (&a->arguments);
if (check_server_name_for_addition (ctx, name)) if (check_server_name_for_addition (ctx, name))
{
server_add_new (ctx, name); server_add_new (ctx, name);
log_global_status (ctx, "Server added: #s", name);
}
return true;
}
static bool
handle_server_remove (struct handler_args *a)
{
struct app_context *ctx = a->ctx;
struct server *s = NULL;
if (!(s = resolve_server (ctx, a, "server")))
return true;
if (irc_is_connected (s))
log_server_error (s, s->buffer, "Can't remove a connected server");
else
{
char *name = xstrdup (s->name);
server_remove (ctx, s);
log_global_status (ctx, "Server removed: #s", name);
free (name);
}
return true; return true;
} }
@ -7496,7 +7553,7 @@ handle_command_server (struct handler_args *a)
else if (!strcasecmp_ascii (action, "add")) else if (!strcasecmp_ascii (action, "add"))
result = handle_server_add (a); result = handle_server_add (a);
else if (!strcasecmp_ascii (action, "remove")) else if (!strcasecmp_ascii (action, "remove"))
; // TODO: <name> result = handle_server_remove (a);
else if (!strcasecmp_ascii (action, "rename")) else if (!strcasecmp_ascii (action, "rename"))
; // TODO: <old> <new> ; // TODO: <old> <new>
else else