wm: ticl

Download patch

ref: 58611c5389c4b4e820b61176617bc7ed1187f1a8
parent: 49ca82832b3505a7ce86e96f11a90599219afaab
author: libredev <libredev@ircforever.org>
date: Thu Jan 19 11:36:21 EST 2023

fixed clone quit

--- a/main.c
+++ b/main.c
@@ -24,23 +24,21 @@
 #include "htable.h"
 #include "util.c"
 
-#define FALSE 0
-#define TRUE 1
+#define CLONE_COOLDOWN	1
+#define CLONE_ADDEND	10
+/* #define PING_TIMEOUT	240 */
 
-#define EV_READ 1
-#define EV_WRITE 2
-
 #define EVENT_ADDEND	100
 #define NET_ADDEND	10
 #define USER_ADDEND	100
-#define PING_TIMEOUT	240
-#define CLONE_COOLDOWN	1
-#define CLONE_ADDEND	10
 
 #define BUFSIZE		1024
 #define NICK_LEN	16
 #define MAX_EVENTS	10
 
+#define EV_READ 1
+#define EV_WRITE 2
+
 enum {
 	IDLE = 0,
 	RESET,
@@ -133,7 +131,7 @@
 	struct kevent	kevs [MAX_EVENTS];	/* maximum event to handle */
 	struct timespec	tmsp;
 #endif
-	int	i, fd, id, nev;
+	int	i, r, fd, id, nev;
 	time_t	timeout;
 	time_t	ntime;		/* next timeout time */
 
@@ -218,9 +216,12 @@
 #else
 			} else if (kevs[i].filter == EVFILT_READ) {
 #endif
-				if (event_read(id)) {
+				r = event_read(id);
+				if (r == 1) {
 					timeout_id = -1;
 					ntime = 0;
+				} else if (r == -2) {
+					break;
 				}
 			} else {
 				errx(1, "unknown event");
@@ -304,6 +305,9 @@
 		return;
 	}
 
+	if (!*buffer)
+		return;
+
 	buf = buffer;
 	cmd = split(&buf, ' ');
 	if (strcmp(cmd, "netadd") == 0) {
@@ -368,7 +372,6 @@
 				if (!n->ready || ids[i] != 0)
 					continue;
 				ids[i] = clone_add(nick, i);
-				printf("%d: %s\n", events[ids[i]].fd, nick);
 			}
 			if (++j >= CLONE_ADDEND)
 				goto calculate;
@@ -621,11 +624,9 @@
 		   || (strcmp(cmd, "PART") == 0)) {
 		snprintf(msg, sizeof(msg), "QUIT :%s\r\n", buf);
 		nick_add_symb(nick, netid);
-		if (htsearch(users, nick) != NULL) {
-			printf("userdel: %s\n", nick);
+		if (htsearch(users, nick) != NULL)
 			user_del(nick, msg);
-		}
-		return 0;
+		return -2;
 	} else if (strcmp(cmd, "NICK") == 0) {
 		int *ids, i;
 		char *newnick;
@@ -693,6 +694,7 @@
 		return 0;
 	}
 printbuffer:
+	printf("%d: %s\n", fd, backup);
 	return 0;
 }
 
@@ -841,17 +843,14 @@
 	sprintf(nick, "%s[%s]", unick, networks[netid].symb);
 	ids[netid] = -1;
 
-	printf("useradd: %s\n", nick);
+	if (debug)
+		printf("useradd: %s\n", nick);
 
 	if (clone) {
 		/* clone the user on all other network */
 		for (i = 0; i < netlen; i++) {
-			if (networks[i].ready == FALSE)
-				continue;
-			if (i != netid) {
+			if (networks[i].ready && i != netid)
 				ids[i] = clone_add(nick, i);
-				printf("%d: %s\n", events[ids[i]].fd, nick);
-			}
 		}
 	}
 
@@ -871,7 +870,10 @@
 	for (i = 0; i < netlen; i++) {
 		if (ids[i] <= 0)
 			continue;
-		if (events[ids[i]].ready == TRUE)
+		printf("%d: del[%s]: %s\n", events[ids[i]].fd,
+		    networks[i].symb, nick);
+
+		if (events[ids[i]].ready)
 			writeall(events[ids[i]].fd, msg);
 		event_del(ids[i]);
 	}
@@ -913,6 +915,7 @@
 		warn("enable to connect to %s\n", n->host);
 		return -1;
 	}
+	printf("%d: add[%s]: %s\n", fd, n->symb, nick);
 	return event_add(fd, netid, nick);
 }
 
--- a/util.c
+++ b/util.c
@@ -20,6 +20,9 @@
 #include <err.h>
 #endif
 
+#define FALSE 0
+#define TRUE 1
+
 /*
  * ecalloc - calloc with error handling
  */
@@ -142,34 +145,22 @@
 ssize_t
 readline(int fd, char *buffer, size_t size)
 {
-	static int left_fd;
-	static char left_buf[1024];
-
 	size_t i = 0;
 	char c;
 	ssize_t l;
 
-	if (fd == left_fd && strlen(left_buf) > 0) {
-		strlcpy(buffer, left_buf, size);
-		*left_buf = '\0';
-		i = strlen(buffer);
-	}
-
 	do {
-		if ((l = read(fd, &c, sizeof(char))) != sizeof(char)) {
-			if (l == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
-				buffer[i] = '\0';
-				left_fd = fd;
-				strlcpy(left_buf, buffer, size);
-				*buffer = '\0';
-				return 1;
-			}
-			return l;
-		}
-		buffer[i++] = c;
+		l = read(fd, &c, sizeof(char));
+		if (l > 0)
+			buffer[i++] = c;
+		else if (l == 0)
+			return 0;
+		else if (l == -1 && errno == EAGAIN)
+			continue;
+		else
+			return -1;
 	} while ((i < size) && (c != '\r') && (c != '\n'));
 
 	buffer[i-1] = '\0';
 	return (ssize_t)i;
-
 }