ii

irc it, simple FIFO based irc client
git clone git://git.suckless.org/ii
Log | Files | Refs | README | LICENSE

commit c25c3793466e2516395e40e2061134ffe553cfe8
parent 39907c79a4c5509db8500b4ad3fb7947eb7b94d9
Author: Petr Vaněk <arkamar@atlas.cz>
Date:   Thu, 29 Sep 2022 10:40:48 +0200

Always clean before exit

This improves situations when ii exits unexpectedly, due to a ping
timeout or closed remote connection, for example. If this happens, all
"in" FIFOs are removed before exit in similar fashion to regular exit
issued with "/q" command.

Diffstat:
Mii.c | 27+++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/ii.c b/ii.c @@ -54,6 +54,7 @@ static int channel_open(Channel *); static void channel_print(Channel *, const char *); static int channel_reopen(Channel *); static void channel_rm(Channel *); +static void clean(void); static void create_dirtree(const char *); static void create_filepath(char *, size_t, const char *, const char *, const char *); static void die(const char *, ...); @@ -92,6 +93,8 @@ die(const char *fmt, ...) va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); + + clean(); exit(1); } @@ -171,6 +174,20 @@ channel_normalize_name(char *s) } static void +clean(void) +{ + Channel *c, *tmp; + + if (channelmaster) + channel_leave(channelmaster); + + for (c = channels; c; c = tmp) { + tmp = c->next; + channel_leave(c); + } +} + +static void create_filepath(char *filepath, size_t len, const char *path, const char *channel, const char *suffix) { @@ -757,6 +774,7 @@ run(int ircfd, const char *host) } else if (r == 0) { if (time(NULL) - last_response >= PING_TIMEOUT) { channel_print(channelmaster, "-!- ii shutting down: ping timeout"); + clean(); exit(2); /* status code 2 for timeout */ } ewritestr(ircfd, ping_msg); @@ -777,7 +795,6 @@ run(int ircfd, const char *host) int main(int argc, char *argv[]) { - Channel *c, *tmp; struct passwd *spw; const char *key = NULL, *fullname = NULL, *host = ""; const char *uds = NULL, *service = "6667"; @@ -843,13 +860,7 @@ main(int argc, char *argv[]) loginuser(ircfd, host, fullname && *fullname ? fullname : nick); setup(); run(ircfd, host); - if (channelmaster) - channel_leave(channelmaster); - - for (c = channels; c; c = tmp) { - tmp = c->next; - channel_leave(c); - } + clean(); return 0; }