Crudely handle reconnecting after suspend

weechat-hashes
Curtis McEnroe 2018-12-01 22:05:37 -05:00
parent e3cff14e03
commit 5c17393d2b
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
5 changed files with 113 additions and 84 deletions

7
chat.h
View File

@ -44,6 +44,7 @@ void selfJoin(const char *join);
void eventWait(const char *argv[static 2]); void eventWait(const char *argv[static 2]);
void eventPipe(const char *argv[static 2]); void eventPipe(const char *argv[static 2]);
void eventQuit(void);
noreturn void eventLoop(void); noreturn void eventLoop(void);
struct Tag { struct Tag {
@ -103,14 +104,14 @@ void inputTab(void);
void ircInit(char *host, char *port, char *pass, char *webPass); void ircInit(char *host, char *port, char *pass, char *webPass);
int ircConnect(void); int ircConnect(void);
void ircDisconnect(const char *quit); bool ircRead(void);
void ircRead(void);
void ircWrite(const char *ptr, size_t len); void ircWrite(const char *ptr, size_t len);
void ircFmt(const char *format, ...) __attribute__((format(printf, 1, 2))); void ircFmt(const char *format, ...) __attribute__((format(printf, 1, 2)));
void uiInit(void); void uiInit(void);
void uiShow(void);
void uiHide(void); void uiHide(void);
void uiExit(void); noreturn void uiExit(void);
void uiDraw(void); void uiDraw(void);
void uiRead(void); void uiRead(void);
void uiPrompt(void); void uiPrompt(void);

144
event.c
View File

@ -19,6 +19,7 @@
#include <errno.h> #include <errno.h>
#include <poll.h> #include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdnoreturn.h> #include <stdnoreturn.h>
@ -30,10 +31,19 @@
#include "chat.h" #include "chat.h"
static struct { static struct {
bool quit;
bool wait; bool wait;
bool pipe; bool susp;
int fd; int irc;
} spawn; int pipe;
} event = {
.irc = -1,
.pipe = -1,
};
void eventQuit(void) {
event.quit = true;
}
void eventWait(const char *argv[static 2]) { void eventWait(const char *argv[static 2]) {
uiHide(); uiHide();
@ -43,11 +53,27 @@ void eventWait(const char *argv[static 2]) {
execvp(argv[0], (char *const *)argv); execvp(argv[0], (char *const *)argv);
err(EX_CONFIG, "%s", argv[0]); err(EX_CONFIG, "%s", argv[0]);
} }
spawn.wait = true; event.wait = true;
}
static void childWait(void) {
uiShow();
int status;
pid_t pid = wait(&status);
if (pid < 0) err(EX_OSERR, "wait");
if (WIFEXITED(status) && WEXITSTATUS(status)) {
uiFmt(TagStatus, UIHot, "event: exit %d", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
uiFmt(
TagStatus, UIHot,
"event: signal %s", strsignal(WTERMSIG(status))
);
}
event.wait = false;
} }
void eventPipe(const char *argv[static 2]) { void eventPipe(const char *argv[static 2]) {
if (spawn.pipe) { if (event.pipe > 0) {
uiLog(TagStatus, UIHot, L"event: existing pipe"); uiLog(TagStatus, UIHot, L"event: existing pipe");
return; return;
} }
@ -70,42 +96,23 @@ void eventPipe(const char *argv[static 2]) {
} }
close(rw[1]); close(rw[1]);
spawn.fd = rw[0]; event.pipe = rw[0];
spawn.pipe = true;
} }
static void pipeRead(void) { static void pipeRead(void) {
char buf[256]; char buf[256];
ssize_t len = read(spawn.fd, buf, sizeof(buf) - 1); ssize_t len = read(event.pipe, buf, sizeof(buf) - 1);
if (len < 0) err(EX_IOERR, "read"); if (len < 0) err(EX_IOERR, "read");
if (len) { if (len) {
buf[len] = '\0'; buf[len] = '\0';
len = strcspn(buf, "\n"); buf[strcspn(buf, "\n")] = '\0';
uiFmt(TagStatus, UIHot, "event: %.*s", (int)len, buf); uiFmt(TagStatus, UIHot, "event: %s", buf);
} else { } else {
close(spawn.fd); close(event.pipe);
spawn.pipe = false; event.pipe = -1;
} }
} }
static void handleChild(void) {
int status;
pid_t pid = wait(&status);
if (pid < 0) err(EX_OSERR, "wait");
if (WIFEXITED(status) && WEXITSTATUS(status)) {
uiFmt(TagStatus, UIHot, "event: exit %d", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
uiFmt(TagStatus, UIHot, "event: signal %d", WTERMSIG(status));
}
spawn.wait = false;
}
static void handleInterrupt(void) {
input(TagStatus, "/quit");
uiExit();
exit(EX_OK);
}
static sig_atomic_t sig[NSIG]; static sig_atomic_t sig[NSIG];
static void handler(int n) { static void handler(int n) {
sig[n] = 1; sig[n] = 1;
@ -114,43 +121,47 @@ static void handler(int n) {
noreturn void eventLoop(void) { noreturn void eventLoop(void) {
sigset_t mask; sigset_t mask;
sigemptyset(&mask); sigemptyset(&mask);
struct sigaction sa = { struct sigaction action = {
.sa_handler = handler, .sa_handler = handler,
.sa_mask = mask, .sa_mask = mask,
.sa_flags = SA_RESTART, .sa_flags = SA_RESTART | SA_NOCLDSTOP,
}; };
sigaction(SIGCHLD, &sa, NULL); sigaction(SIGCHLD, &action, NULL);
sigaction(SIGINT, &sa, NULL); sigaction(SIGINT, &action, NULL);
sigaction(SIGTSTP, &action, NULL);
struct sigaction curses; struct sigaction curses;
sigaction(SIGWINCH, &sa, &curses); sigaction(SIGWINCH, &action, &curses);
assert(!(curses.sa_flags & SA_SIGINFO)); assert(!(curses.sa_flags & SA_SIGINFO));
int irc = ircConnect(); event.irc = ircConnect();
struct pollfd fds[3] = {
{ irc, POLLIN, 0 },
{ STDIN_FILENO, POLLIN, 0 },
{ -1, POLLIN, 0 },
};
for (;;) { for (;;) {
if (sig[SIGCHLD]) handleChild(); if (sig[SIGCHLD]) childWait();
if (sig[SIGINT]) handleInterrupt(); if (sig[SIGINT]) {
signal(SIGINT, SIG_DFL);
ircFmt("QUIT :Goodbye\r\n");
event.quit = true;
}
if (sig[SIGTSTP]) {
signal(SIGTSTP, SIG_DFL);
ircFmt("QUIT :zzz\r\n");
event.susp = true;
}
if (sig[SIGWINCH]) { if (sig[SIGWINCH]) {
curses.sa_handler(SIGWINCH); curses.sa_handler(SIGWINCH);
uiRead(); uiRead();
uiDraw();
} }
sig[SIGCHLD] = 0; sig[SIGCHLD] = sig[SIGINT] = sig[SIGTSTP] = sig[SIGWINCH] = 0;
sig[SIGINT] = 0;
sig[SIGWINCH] = 0;
nfds_t nfds = 2; nfds_t nfds = 0;
if (spawn.wait) nfds = 1; struct pollfd fds[3] = {
if (spawn.pipe) { { .events = POLLIN },
fds[2].fd = spawn.fd; { .events = POLLIN },
nfds = 3; { .events = POLLIN },
} };
if (!event.wait) fds[nfds++].fd = STDIN_FILENO;
if (event.irc > 0) fds[nfds++].fd = event.irc;
if (event.pipe > 0) fds[nfds++].fd = event.pipe;
int ready = poll(fds, nfds, -1); int ready = poll(fds, nfds, -1);
if (ready < 0) { if (ready < 0) {
@ -158,10 +169,25 @@ noreturn void eventLoop(void) {
err(EX_IOERR, "poll"); err(EX_IOERR, "poll");
} }
if (fds[0].revents) ircRead(); for (nfds_t i = 0; i < nfds; ++i) {
if (nfds > 1 && fds[1].revents) uiRead(); if (!fds[i].revents) continue;
if (nfds > 2 && fds[2].revents) pipeRead(); if (fds[i].fd == STDIN_FILENO) uiRead();
if (fds[i].fd == event.pipe) pipeRead();
if (nfds > 1) uiDraw(); if (fds[i].fd == event.irc) {
if (ircRead()) continue;
event.irc = -1;
// TODO: Handle unintended disconnects.
if (event.quit) uiExit();
if (event.susp) {
uiHide();
raise(SIGTSTP);
sigaction(SIGTSTP, &action, NULL);
uiShow();
event.irc = ircConnect();
event.susp = false;
}
}
}
uiDraw();
} }
} }

View File

@ -110,6 +110,7 @@ static void inputQuit(struct Tag tag, char *params) {
} else { } else {
ircFmt("QUIT :Goodbye\r\n"); ircFmt("QUIT :Goodbye\r\n");
} }
eventQuit();
} }
static void inputURL(struct Tag tag, char *params) { static void inputURL(struct Tag tag, char *params) {

37
irc.c
View File

@ -19,6 +19,7 @@
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -103,27 +104,16 @@ int ircConnect(void) {
); );
} }
/// FIXME
if (self.user[0] == '~') selfUser(&self.user[1]);
if (irc.pass) ircFmt("PASS :%s\r\n", irc.pass); if (irc.pass) ircFmt("PASS :%s\r\n", irc.pass);
ircFmt( ircFmt("NICK %s\r\n", self.nick);
"NICK %s\r\n" ircFmt("USER %s 0 * :%s\r\n", self.user, self.nick);
"USER %s 0 * :%s\r\n",
self.nick, self.user, self.nick
);
return irc.sock; return irc.sock;
} }
void ircDisconnect(const char *quit) {
// TODO: Wait for response, send quit to UI.
ircFmt("QUIT :%s\r\n", quit);
int error = tls_close(irc.client);
if (error) errx(EX_IOERR, "tls_close: %s", tls_error(irc.client));
error = close(irc.sock);
if (error) err(EX_IOERR, "close");
}
void ircWrite(const char *ptr, size_t len) { void ircWrite(const char *ptr, size_t len) {
while (len) { while (len) {
ssize_t ret = tls_write(irc.client, ptr, len); ssize_t ret = tls_write(irc.client, ptr, len);
@ -151,15 +141,23 @@ void ircFmt(const char *format, ...) {
free(buf); free(buf);
} }
void ircRead(void) { static void disconnect(void) {
int error = tls_close(irc.client);
if (error) errx(EX_IOERR, "tls_close: %s", tls_error(irc.client));
error = close(irc.sock);
if (error) err(EX_IOERR, "close");
}
bool ircRead(void) {
static char buf[4096]; static char buf[4096];
static size_t len; static size_t len;
ssize_t read = tls_read(irc.client, &buf[len], sizeof(buf) - len); ssize_t read = tls_read(irc.client, &buf[len], sizeof(buf) - len);
if (read < 0) errx(EX_IOERR, "tls_read: %s", tls_error(irc.client)); if (read < 0) errx(EX_IOERR, "tls_read: %s", tls_error(irc.client));
if (!read) { if (!read) {
uiExit(); disconnect();
exit(EX_OK); len = 0;
return false;
} }
len += read; len += read;
@ -178,4 +176,5 @@ void ircRead(void) {
len -= line - buf; len -= line - buf;
memmove(buf, line, len); memmove(buf, line, len);
return true;
} }

8
ui.c
View File

@ -22,6 +22,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h> #include <string.h>
#include <sysexits.h> #include <sysexits.h>
#include <wchar.h> #include <wchar.h>
@ -81,9 +82,10 @@ static struct {
struct View *view; struct View *view;
} ui; } ui;
static void uiShow(void) { void uiShow(void) {
ui.hide = false; ui.hide = false;
termMode(TermFocus, true); termMode(TermFocus, true);
uiDraw();
} }
void uiHide(void) { void uiHide(void) {
@ -109,12 +111,13 @@ void uiInit(void) {
uiViewTag(TagStatus); uiViewTag(TagStatus);
} }
void uiExit(void) { noreturn void uiExit(void) {
uiHide(); uiHide();
printf( printf(
"This program is AGPLv3 Free Software!\n" "This program is AGPLv3 Free Software!\n"
"The source is available at <" SOURCE_URL ">.\n" "The source is available at <" SOURCE_URL ">.\n"
); );
exit(EX_OK);
} }
static int lastLine(void) { static int lastLine(void) {
@ -557,7 +560,6 @@ void uiPrompt(void) {
} }
void uiRead(void) { void uiRead(void) {
uiShow();
int ret; int ret;
wint_t ch; wint_t ch;
while (ERR != (ret = wget_wch(ui.input, &ch))) { while (ERR != (ret = wget_wch(ui.input, &ch))) {