degesch: implement /server rename

This commit is contained in:
Přemysl Eric Janouch 2015-07-14 20:48:17 +02:00
parent 0c5591fed2
commit 955552f4fa
2 changed files with 86 additions and 13 deletions

View File

@ -70,6 +70,17 @@ str_vector_find (const struct str_vector *v, const char *s)
return -1;
}
static void *
str_map_steal (struct str_map *self, const char *key)
{
void *value = str_map_find (self, key);
void (*free_backup) (void *) = self->free;
self->free = NULL;
str_map_set (self, key, NULL);
self->free = free_backup;
return value;
}
// --- Logging -----------------------------------------------------------------
static void

View File

@ -2959,6 +2959,7 @@ buffer_print_backlog (struct app_context *ctx, struct buffer *buffer)
{
// The prompt can take considerable time to redraw
input_hide (&ctx->input);
// FIXME: encoding
print_status ("%s", buffer->name);
// That is, minus the buffer switch line and the readline prompt
@ -4519,6 +4520,7 @@ refresh_prompt (struct app_context *ctx)
make_prompt (ctx, &prompt);
str_append_c (&prompt, ' ');
// FIXME: encoding
if (have_attributes)
{
// XXX: to be completely correct, we should use tputs, but we cannot
@ -6586,6 +6588,43 @@ server_remove (struct app_context *ctx, struct server *s)
str_map_set (&ctx->servers, s->name, NULL);
}
static void
server_rename (struct app_context *ctx, struct server *s, const char *new_name)
{
str_map_set (&ctx->servers, new_name,
str_map_steal (&ctx->servers, s->name));
struct str_map *servers = get_servers_config (ctx);
str_map_set (servers, new_name, str_map_steal (servers, s->name));
free (s->name);
s->name = xstrdup (new_name);
buffer_rename (ctx, s->buffer, new_name);
struct str_map_iter iter;
str_map_iter_init (&iter, &s->irc_buffer_map);
struct buffer *buffer;
while ((buffer = str_map_iter_next (&iter)))
{
// FIXME: creation of buffer names should be centralized
char *x = NULL;
switch (buffer->type)
{
case BUFFER_PM:
x = xstrdup_printf ("%s.%s", s->name, buffer->user->nickname);
break;
case BUFFER_CHANNEL:
x = xstrdup_printf ("%s.%s", s->name, buffer->channel->name);
break;
default:
hard_assert (!"unexpected type of server-related buffer");
}
buffer_rename (ctx, buffer, x);
free (x);
}
}
// --- User input handling -----------------------------------------------------
// HANDLER_NEEDS_REG is primarily for message sending commands,
@ -7538,25 +7577,48 @@ handle_server_remove (struct handler_args *a)
}
static bool
handle_command_server (struct handler_args *a)
handle_server_rename (struct handler_args *a)
{
struct app_context *ctx = a->ctx;
if (!*a->arguments)
return show_servers_list (ctx);
return false;
char *old_name = cut_word (&a->arguments);
if (!*a->arguments)
return false;
char *new_name = cut_word (&a->arguments);
struct server *s;
const char *err;
if (!(s = str_map_find (&ctx->servers, old_name)))
log_global_error (ctx, "/#s: #s: #s",
"server", "no such server", old_name);
else if ((err = check_server_name_for_addition (ctx, new_name)))
log_global_error (ctx,
"Cannot rename server to `#s': #s", new_name, err);
else
{
server_rename (ctx, s, new_name);
log_global_status (ctx, "Server renamed: #s to #s", old_name, new_name);
}
return true;
}
static bool
handle_command_server (struct handler_args *a)
{
if (!*a->arguments)
return show_servers_list (a->ctx);
char *action = cut_word (&a->arguments);
bool result = true;
if (!strcasecmp_ascii (action, "list"))
result = show_servers_list (ctx);
else if (!strcasecmp_ascii (action, "add"))
result = handle_server_add (a);
else if (!strcasecmp_ascii (action, "remove"))
result = handle_server_remove (a);
else if (!strcasecmp_ascii (action, "rename"))
; // TODO: <old> <new>
else
result = false;
return result;
return show_servers_list (a->ctx);
if (!strcasecmp_ascii (action, "add"))
return handle_server_add (a);
if (!strcasecmp_ascii (action, "remove"))
return handle_server_remove (a);
if (!strcasecmp_ascii (action, "rename"))
return handle_server_rename (a);
return false;
}
static bool