From 0981df485a2cead652817f14044a7842451a0bd4 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Thu, 20 Apr 2017 20:47:39 +0200 Subject: degesch: simplify quitting - send a QUIT on C-c, too - shut down the connection on /disconnect, too Connection management is one of the few fucked up parts that remain in that state for historical reasons. --- degesch.c | 85 +++++++++++++++++++++++++++------------------------------------ 1 file changed, 36 insertions(+), 49 deletions(-) diff --git a/degesch.c b/degesch.c index ecd1226..80939fc 100644 --- a/degesch.c +++ b/degesch.c @@ -4836,36 +4836,6 @@ try_finish_quit (struct app_context *ctx) ctx->polling = false; } -static void -initiate_quit (struct app_context *ctx) -{ - log_global_status (ctx, "Shutting down"); - - // Hide the user interface - CALL (ctx->input, hide); - - // Initiate a connection close - struct str_map_iter iter; - str_map_iter_init (&iter, &ctx->servers); - struct server *s; - while ((s = str_map_iter_next (&iter))) - { - // There may be a timer set to reconnect to the server - poller_timer_reset (&s->reconnect_tmr); - - if (irc_is_connected (s)) - { - irc_shutdown (s); - s->manual_disconnect = true; - } - else if (s->state == IRC_CONNECTING) - irc_destroy_connector (s); - } - - ctx->quitting = true; - try_finish_quit (ctx); -} - static void irc_destroy_transport (struct server *s) { @@ -4954,13 +4924,46 @@ irc_initiate_disconnect (struct server *s, const char *reason) log_server_error (s, s->buffer, "%s: %s", "Disconnected from server", "connection torn down early per user request"); irc_disconnect (s); + return; } - else if (reason) + + if (reason) irc_send (s, "QUIT :%s", reason); else + // TODO: make the default QUIT message customizable + // -> global/per server/both? + // -> implement IRC output hooks for plugins? irc_send (s, "QUIT :%s", PROGRAM_NAME " " PROGRAM_VERSION); s->manual_disconnect = true; + irc_shutdown (s); +} + +static void +initiate_quit (struct app_context *ctx, const char *message) +{ + log_global_status (ctx, "Shutting down"); + + // Hide the user interface + CALL (ctx->input, hide); + + // Initiate a connection close + struct str_map_iter iter; + str_map_iter_init (&iter, &ctx->servers); + struct server *s; + while ((s = str_map_iter_next (&iter))) + { + // There may be a timer set to reconnect to the server + poller_timer_reset (&s->reconnect_tmr); + + if (irc_is_connected (s)) + irc_initiate_disconnect (s, message); + else if (s->state == IRC_CONNECTING) + irc_destroy_connector (s); + } + + ctx->quitting = true; + try_finish_quit (ctx); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -11131,18 +11134,7 @@ handle_command_me (struct handler_args *a) static bool handle_command_quit (struct handler_args *a) { - struct str_map_iter iter; - str_map_iter_init (&iter, &a->ctx->servers); - - // FIXME: we should pass the message as an argument to initiate_quit() - struct server *s; - while ((s = str_map_iter_next (&iter))) - { - if (irc_is_connected (s)) - irc_initiate_disconnect (s, *a->arguments ? a->arguments : NULL); - } - - initiate_quit (a->ctx); + initiate_quit (a->ctx, *a->arguments ? a->arguments : NULL); return true; } @@ -13565,12 +13557,7 @@ on_signal_pipe_readable (const struct pollfd *fd, struct app_context *ctx) ; if (g_termination_requested && !ctx->quitting) - // TODO: this way we don't send a QUIT message but just close the - // connection from our side and wait for a full close. - // Once we allow for custom quit messages, we will probably want to - // call irc_initiate_disconnect() for all servers. - initiate_quit (ctx); - + initiate_quit (ctx, NULL); if (g_winch_received) { redraw_screen (ctx); -- cgit v1.2.3-70-g09d2