From 8f5dec04569d26f762bc34634fd06ac005f82bcf Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sat, 31 Oct 2020 23:42:49 +0100 Subject: degesch: buffer creation cleanup --- degesch.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/degesch.c b/degesch.c index 71f2931..049e9c8 100644 --- a/degesch.c +++ b/degesch.c @@ -1591,12 +1591,14 @@ static struct ispect_field g_buffer_ispect[] = }; static struct buffer * -buffer_new (struct input *input) +buffer_new (struct input *input, enum buffer_type type, char *name) { struct buffer *self = xcalloc (1, sizeof *self); self->ref_count = 1; self->input = input; self->input_data = CALL (input, buffer_new); + self->type = type; + self->name = name; return self; } @@ -4446,9 +4448,8 @@ buffer_remove_safe (struct app_context *ctx, struct buffer *buffer) static void init_global_buffer (struct app_context *ctx) { - struct buffer *global = ctx->global_buffer = buffer_new (ctx->input); - global->type = BUFFER_GLOBAL; - global->name = xstrdup (PROGRAM_NAME); + struct buffer *global = ctx->global_buffer = + buffer_new (ctx->input, BUFFER_GLOBAL, xstrdup (PROGRAM_NAME)); buffer_add (ctx, global); buffer_activate (ctx, global); @@ -4456,6 +4457,19 @@ init_global_buffer (struct app_context *ctx) // --- Users, channels --------------------------------------------------------- +static char * +irc_make_buffer_name (struct server *s, const char *target) +{ + if (!target) + return xstrdup (s->name); + + // XXX: this may be able to trigger the uniqueness assertion with non-UTF-8 + char *target_utf8 = irc_to_utf8 (target); + char *result = xstrdup_printf ("%s.%s", s->name, target_utf8); + free (target_utf8); + return result; +} + static void irc_user_on_destroy (void *object, void *user_data) { @@ -4495,11 +4509,8 @@ irc_get_or_make_user_buffer (struct server *s, const char *nickname) struct user *user = irc_get_or_make_user (s, nickname); // Open a new buffer for the user - buffer = buffer_new (s->ctx->input); - buffer->type = BUFFER_PM; - char *nickname_utf8 = irc_to_utf8 (nickname); - buffer->name = xstrdup_printf ("%s.%s", s->name, nickname_utf8); - free (nickname_utf8); + buffer = buffer_new (s->ctx->input, + BUFFER_PM, irc_make_buffer_name (s, nickname)); buffer->server = s; buffer->user = user; str_map_set (&s->irc_buffer_map, user->nickname, buffer); @@ -6644,11 +6655,8 @@ irc_handle_join (struct server *s, const struct irc_message *msg) if (!irc_is_this_us (s, msg->prefix)) return; - buffer = buffer_new (s->ctx->input); - buffer->type = BUFFER_CHANNEL; - char *channel_name_utf8 = irc_to_utf8 (channel_name); - buffer->name = xstrdup_printf ("%s.%s", s->name, channel_name_utf8); - free (channel_name_utf8); + buffer = buffer_new (s->ctx->input, + BUFFER_CHANNEL, irc_make_buffer_name (s, channel_name)); buffer->server = s; buffer->channel = channel = irc_make_channel (s, xstrdup (channel_name)); @@ -8318,9 +8326,8 @@ server_add (struct app_context *ctx, s->config = subtree; // Add a buffer and activate it - struct buffer *buffer = s->buffer = buffer_new (ctx->input); - buffer->type = BUFFER_SERVER; - buffer->name = xstrdup (s->name); + struct buffer *buffer = s->buffer = buffer_new (ctx->input, + BUFFER_SERVER, irc_make_buffer_name (s, NULL)); buffer->server = s; buffer_add (ctx, buffer); -- cgit v1.2.3-70-g09d2