diff --git a/programs/dtchat/irc_adapter.c b/programs/dtchat/irc_adapter.c index 1aa5c8ab..264b6f8a 100644 --- a/programs/dtchat/irc_adapter.c +++ b/programs/dtchat/irc_adapter.c @@ -36,14 +36,19 @@ static char nick[64] = ""; static char password[256] = ""; static char channels[512] = ""; static int configured; -static int connect_pending; static int tls_want_write; +static int want_connection; +static long long reconnect_at; +static int backoff_ms = 2000; +static int nick_collisions; static int cap_done; static int sasl_offered; static int sasl_requested; static int sasl_done; static char capbuf[1024]; +static long long now_ms(void); + #ifdef HAVE_LIBTLS static struct tls *tls_ctx; #endif @@ -239,7 +244,6 @@ open_socket(void) continue; } } - connect_pending = 0; break; } freeaddrinfo(res); @@ -250,6 +254,16 @@ open_socket(void) return fd; } +static void +schedule_reconnect(void) +{ + if (!want_connection) + return; + send_state("offline", NULL); + reconnect_at = now_ms() + backoff_ms; + backoff_ms = backoff_ms * 2 > 60000 ? 60000 : backoff_ms * 2; +} + static char * b64_encode(const unsigned char *in, size_t len) { @@ -517,6 +531,26 @@ handshake(int fd) (void)send_all(fd, line); } +static void +start_connection(int *sockp) +{ + int fd; + + send_state("connecting", host); + fd = open_socket(); + if (fd < 0) { + schedule_reconnect(); + return; + } + if (use_tls && tls_begin(fd) != 0) { + close(fd); + schedule_reconnect(); + return; + } + *sockp = fd; + handshake(fd); +} + #define IRC_MAX_CHANNELS 16 #define IRC_MAX_MEMBERS 128 @@ -830,6 +864,9 @@ handle_line(int fd, const char *line) snprintf(reply, sizeof(reply), "PONG :%s\r\n", token); (void)send_all(fd, reply); } else if (strcmp(msg.command, "001") == 0) { + backoff_ms = 2000; + nick_collisions = 0; + reconnect_at = 0; send_state("online", host); join_configured_channels(fd); } else if (strcmp(msg.command, "353") == 0) { @@ -1034,10 +1071,18 @@ handle_line(int fd, const char *line) } else if (strcmp(msg.command, "ERROR") == 0) { send_error("protocol", msg.trailing != NULL ? msg.trailing : "server error", 0); - send_state("offline", NULL); } else if (strcmp(msg.command, "433") == 0) { - send_error("nick", "nickname already in use", 0); - send_state("offline", NULL); + if (nick_collisions < 3 && strlen(nick) < 60) { + char line[128]; + + nick_collisions++; + strcat(nick, "_"); + snprintf(line, sizeof(line), "NICK %s\r\n", nick); + (void)send_all(fd, line); + } else { + send_error("nick", "nickname already in use", 0); + (void)send_all(fd, "QUIT :nick in use\r\n"); + } } else if (strcmp(msg.command, "CAP") == 0) { const char *sub = msg.nparams > 1 ? msg.params[1] : ""; @@ -1108,9 +1153,15 @@ handle_line(int fd, const char *line) } else if (strcmp(msg.command, "904") == 0 || strcmp(msg.command, "905") == 0 || strcmp(msg.command, "906") == 0) { - send_error("auth", "SASL authentication failed", 0); - (void)send_all(fd, "CAP END\r\n"); - cap_done = 1; + DcJson *bye = dc_json_new_object(); + + send_error("auth", "SASL authentication failed", 1); + (void)dc_json_object_set(bye, "type", + dc_json_new_string("bye")); + (void)dc_json_object_set(bye, "reason", + dc_json_new_string("auth")); + send_json(bye); + _exit(0); } dc_irc_message_free(&msg); } @@ -1167,35 +1218,20 @@ read_stdin(DcFrameBuf *frame, int *sockp) snprintf(password, sizeof(password), "%s", v); configured = 1; } else if (strcmp(type, "connect") == 0) { - int fd; - if (!configured || nick[0] == '\0') { send_error("config", "nickname is required", 0); send_state("offline", NULL); } else { - send_state("connecting", host); - fd = open_socket(); - if (fd >= 0) { - *sockp = fd; - if (!connect_pending) { - int ok = 1; - - if (use_tls) - ok = tls_begin(fd) == 0; - if (ok) { - handshake(fd); - } else { - close(fd); - *sockp = -1; - send_state("offline", - NULL); - } - } - } + want_connection = 1; + backoff_ms = 2000; + reconnect_at = 0; + start_connection(sockp); } } else if (strcmp(type, "disconnect") == 0) { DcJson *bye = dc_json_new_object(); + want_connection = 0; + (void)dc_json_object_set(bye, "type", dc_json_new_string("bye")); (void)dc_json_object_set(bye, "reason", @@ -1261,14 +1297,24 @@ main(int argc, char **argv) if (sock >= 0) { fds[nfds].fd = sock; fds[nfds].events = POLLIN | - (connect_pending || tls_want_write ? - POLLOUT : 0); + (tls_want_write ? POLLOUT : 0); fds[nfds].revents = 0; nfds++; } { long long wait = pump_outq(sock); + if (reconnect_at > 0 && sock < 0) { + long long due = reconnect_at - now_ms(); + + if (due <= 0) { + reconnect_at = 0; + start_connection(&sock); + continue; + } + if (wait < 0 || due < wait) + wait = due; + } ret = poll(fds, (nfds_t)nfds, wait < 0 ? -1 : (int)wait); } @@ -1280,32 +1326,7 @@ main(int argc, char **argv) if (fds[0].revents & (POLLIN | POLLHUP)) read_stdin(&frame, &sock); if (nfds > 1 && fds[1].revents != 0) { - if (connect_pending && (fds[1].revents & POLLOUT)) { - int err = 0; - socklen_t len = sizeof(err); - - (void)getsockopt(sock, SOL_SOCKET, SO_ERROR, - &err, &len); - connect_pending = 0; - if (err != 0) { - send_error("network", - "cannot connect to the IRC server", - 0); - send_state("offline", NULL); - close(sock); - sock = -1; - } else if (use_tls) { - if (tls_begin(sock) == 0) - handshake(sock); - else { - close(sock); - sock = -1; - send_state("offline", NULL); - } - } else { - handshake(sock); - } - } else if (fds[1].revents & (POLLIN | POLLHUP | + if (fds[1].revents & (POLLIN | POLLHUP | POLLERR)) { char buf[4096]; ssize_t n = connection_read(sock, buf, @@ -1314,12 +1335,12 @@ main(int argc, char **argv) if (n == -1 || n == -2) { tls_want_write = n == -2; } else if (n <= 0) { - send_state("offline", NULL); tls_teardown(); tls_want_write = 0; close(sock); sock = -1; inlen = 0; + schedule_reconnect(); } else { char *start; char *nl; @@ -1347,6 +1368,7 @@ main(int argc, char **argv) } } } + tls_teardown(); if (sock >= 0) close(sock); dc_frame_free(&frame);