wm: ticl

Download patch

ref: 8d0c143cd5f978544806f8b58bcefc22aef92cb5
parent: d6907025da2d1bb738aba0ead2e860316d3d5b80
author: libredev <libredev@ircforever.org>
date: Wed Nov 23 20:20:34 EST 2022

fifo workaround

--- a/main.c
+++ b/main.c
@@ -23,9 +23,11 @@
 #define NET_ADDEND	10
 #define USER_ADDEND	100
 #define EVENT_TIMEOUT	10000
-#define FIFO_TIMEOUT	5
-#define EVENT_FREQUENCY	5
 
+#define MAX_FDS		10000
+#define CLONE_COOLDOWN	5
+#define CLONE_ADDEND	5
+
 #define UNUSED(x) (void)(x)
 
 struct network {
@@ -45,6 +47,9 @@
 	int	 suffix;	/* suffix count	*/
 };
 
+static char	msg [BUFFER_LEN];
+static int	done;
+
 static struct event	*events;	/* events array	  */
 static int		 evlen;		/* array length	  */
 static int		 evcap;		/* array capacity */
@@ -59,11 +64,8 @@
  * value -> array of all user's clones event ids indexed
  *	    corresponding to its connected network
  */
-static struct Htable	*users;		
+static struct Htable	*users;
 
-static char	msg [BUFFER_LEN];
-static int	done;
-
 /* functions prototype */
 void	 fifo_event_cb(picoev_loop *, int, int, void *);
 void	 server_event_cb(picoev_loop *, int, int, void *);
@@ -78,12 +80,12 @@
 int	 clone_add(picoev_loop *, char *, int);
 int	 event_add(picoev_loop *, int, int, char *);
 void	 event_del(picoev_loop *, int);
-void   	 nick_add_symb(char *, int);
-void   	 privmsg_update(char *, char *, int);
-void   	 clean_exit(picoev_loop *, int);
-void   	 print_table(void);
-void   	 print_htable(void);
-void   	 print_users(void);
+void	 nick_add_symb(char *, int);
+void	 privmsg_update(char *, char *, int);
+void	 clean_exit(picoev_loop *, int);
+void	 print_table(void);
+void	 print_htable(void);
+void	 print_users(void);
 void	 print_border(void);
 
 int
@@ -90,7 +92,7 @@
 main(int argc, char *argv[])
 {
 	picoev_loop *loop;
-	int fd;
+	/* int fd; */
 
 	/* set stdout to unbufferd */
 	setvbuf(stdout, NULL, _IONBF, 0);
@@ -106,16 +108,21 @@
 	netcap = NET_ADDEND;
 	events = ecalloc((size_t)evcap, sizeof(struct event));
 	networks = ecalloc((size_t)netcap, sizeof(struct network));
-	users = htcreate((KeyLenFn *)strlen, (KeyCmpFn *)strcmp, free, free, USER_ADDEND);
+	users = htcreate((KeyLenFn *)strlen, (KeyCmpFn *)strcmp, free, free,
+			 USER_ADDEND);
 
 	/* init picoev */
-	picoev_init(1000);
+	picoev_init(MAX_FDS);
 	/* create loop */
 	loop = picoev_create_loop(60);
 	/* get fifo fd */
-	fd = fifo_open(argv[1]);
+	/* fd = fifo_open(argv[1]); */
 	/* add fifo fd */
-	picoev_add(loop, fd, PICOEV_READ, FIFO_TIMEOUT, fifo_event_cb, argv[1]);
+	/* picoev_add(loop, fd, PICOEV_READ, 0, fifo_event_cb, argv[1]); */
+
+	net_add(loop, "libre",  "L", "38.87.162.53", "6667", "#debian2");
+	net_add(loop, "ircnow",  "N", "irc.ircnow.org", "6667", "#debian");
+
 	/* loop */
 	while (!done)
 		picoev_loop_once(loop, 10);
@@ -133,33 +140,8 @@
 	char	*cmd;
 	ssize_t	 n;
 
-	int	 i, count = 0, *ids;
-	Htiter	 it = {0};
+	UNUSED(revents);
 
-	if ((revents & PICOEV_TIMEOUT) != 0) {
-		printf("TIME to add %d more connections\n", EVENT_FREQUENCY);
-		while (htiterate(users, &it)) {
-			for (i = 0; i < netlen; i++) {
-				ids = (int *)it.node->val;
-				buf = (char *)it.node->key;
-				if (ids[i] == 0) {
-					fd = clone_add(loop, buf, i);
-					ids[i] = fd;
-					printf("%d. %d: %s\n", count, fd, buf);
-					count++;
-					if (count >= EVENT_FREQUENCY)
-						goto return_time;
-				}
-			}
-		}
-return_time:
-		picoev_del(loop, fd);
-		close(fd);
-		fd = fifo_open((char *)cb_arg);
-		picoev_add(loop, fd, PICOEV_READ, FIFO_TIMEOUT, fifo_event_cb, cb_arg);
-		return;
-	}
-
 	n = readline(fd, buffer, sizeof(buffer));
 	if (n == -1) {
 		if (errno == EAGAIN || errno == EWOULDBLOCK)
@@ -170,7 +152,7 @@
 		picoev_del(loop, fd);
 		close(fd);
 		fd = fifo_open((char *)cb_arg);
-		picoev_add(loop, fd, PICOEV_READ, FIFO_TIMEOUT, fifo_event_cb, cb_arg);
+		picoev_add(loop, fd, PICOEV_READ, 0, fifo_event_cb, cb_arg);
 		return;
 	}
 
@@ -210,10 +192,10 @@
 {
 	char	 buffer	[BUFFER_LEN];
 	char	 backup	[BUFFER_LEN];
-	char 	 lnick	[NICK_LEN];	/* linker nick */
-	char 	*buf;
-	char 	*cmd;
-	char 	*nick;
+	char	 lnick	[NICK_LEN];	/* linker nick */
+	char	*buf;
+	char	*cmd;
+	char	*nick;
 	int	 i, id, netid;
 	ssize_t	 n;
 
@@ -220,6 +202,30 @@
 	id = (int)((struct event *)cb_arg - events);
 	netid = events[id].netid;
 
+	if ((revents & PICOEV_TIMEOUT) != 0) {
+		int	*ids, cfd, count = 0;
+		Htiter	 it = {0};
+
+		printf("info: time to clone %d users\n", CLONE_ADDEND);
+		while (htiterate(users, &it)) {
+			ids = (int *)it.node->val;
+			buf = (char *)it.node->key;
+			if (ids[netid] == 0) {
+				cfd = clone_add(loop, buf, netid);
+				ids[netid] = cfd;
+				printf("%d. %d: %s\n", count, cfd, buf);
+				count++;
+				if (count >= CLONE_ADDEND) {
+					picoev_set_timeout(loop, fd, CLONE_COOLDOWN);
+					return;
+				}
+			}
+		}
+		printf("FUCK\n");
+		picoev_set_timeout(loop, fd, 0);
+		return;
+	}
+
 	if ((revents & PICOEV_WRITE) != 0) {
 		if (events[id].user == NULL) {	/* linker */
 			snprintf(msg, sizeof(msg), "NICK linker\r\n");
@@ -284,25 +290,25 @@
 
 	/* ignore all the info messages */
 	if ((strcmp(cmd, "002") == 0)
-	|| (strcmp(cmd, "003") == 0)
-	|| (strcmp(cmd, "004") == 0)
-	|| (strcmp(cmd, "005") == 0)
-	|| (strcmp(cmd, "003") == 0)
-	|| (strcmp(cmd, "251") == 0)
-	|| (strcmp(cmd, "252") == 0)
-	|| (strcmp(cmd, "253") == 0)	/* unknown connection(s) */
-	|| (strcmp(cmd, "254") == 0)
-	|| (strcmp(cmd, "255") == 0)
-	|| (strcmp(cmd, "265") == 0)
-	|| (strcmp(cmd, "266") == 0)
-	|| (strcmp(cmd, "250") == 0)
-	|| (strcmp(cmd, "375") == 0)
-	|| (strcmp(cmd, "372") == 0)
-	|| (strcmp(cmd, "376") == 0)
-	|| (strcmp(cmd, "396") == 0)
-	|| (strcmp(cmd, "366") == 0)
-	|| (strcmp(cmd, "MODE") == 0)
-	|| (strcmp(cmd, "NOTICE") == 0)) {
+	    || (strcmp(cmd, "003") == 0)
+	    || (strcmp(cmd, "004") == 0)
+	    || (strcmp(cmd, "005") == 0)
+	    || (strcmp(cmd, "003") == 0)
+	    || (strcmp(cmd, "251") == 0)
+	    || (strcmp(cmd, "252") == 0)
+	    || (strcmp(cmd, "253") == 0)	/* unknown connection(s) */
+	    || (strcmp(cmd, "254") == 0)
+	    || (strcmp(cmd, "255") == 0)
+	    || (strcmp(cmd, "265") == 0)
+	    || (strcmp(cmd, "266") == 0)
+	    || (strcmp(cmd, "250") == 0)
+	    || (strcmp(cmd, "375") == 0)
+	    || (strcmp(cmd, "372") == 0)
+	    || (strcmp(cmd, "376") == 0)
+	    || (strcmp(cmd, "396") == 0)
+	    || (strcmp(cmd, "366") == 0)
+	    || (strcmp(cmd, "MODE") == 0)
+	    || (strcmp(cmd, "NOTICE") == 0)) {
 		return;
 	} else if (strcmp(cmd, "433") == 0) {	/* Nickname already in use */
 		split(&buf, ' ');
@@ -376,11 +382,11 @@
 	/* these messages are handled by linker */
 	if (events[id].user != NULL) {	/* if clone */
 		if ((strcmp(cmd, "353") == 0)
-		|| (strcmp(cmd, "JOIN") == 0)
-		|| (strcmp(cmd, "QUIT") == 0)
-		|| (strcmp(cmd, "PART") == 0)
-		|| (strcmp(cmd, "KICK") == 0)
-		|| (strcmp(cmd, "NICK") == 0))
+		    || (strcmp(cmd, "JOIN") == 0)
+		    || (strcmp(cmd, "QUIT") == 0)
+		    || (strcmp(cmd, "PART") == 0)
+		    || (strcmp(cmd, "KICK") == 0)
+		    || (strcmp(cmd, "NICK") == 0))
 			return;
 	} else if (strcmp(cmd, "353") == 0) {
 		char *nick;
@@ -389,23 +395,25 @@
 		/* net_update(loop, netid); */
 		while (*(nick = split(&buf, ' ')) != '\0') {
 			if (*nick == '@'
-			|| *nick == '&'
-			|| *nick == '~'
-			|| *nick == '%'
-			|| *nick == '+'
-			|| *nick == '\\')
+			    || *nick == '&'
+			    || *nick == '~'
+			    || *nick == '%'
+			    || *nick == '+'
+			    || *nick == '\\')
 				nick++;
 			if (strcmp(nick, lnick) != 0)
 				user_add(loop, nick, netid);
 		}
+		for (i = 0; i < netlen; i++)
+			picoev_set_timeout(loop, events[networks[i].id].fd, CLONE_COOLDOWN);
 		return;
 	} else if (strcmp(cmd, "JOIN") == 0) {
 		if ((strcmp(nick, lnick) != 0)
-		&& (clone_get_user_ids(nick, netid) == NULL))	/* if not clone */
+		    && (clone_get_user_ids(nick, netid) == NULL))	/* if not clone */
 			user_add(loop, nick, netid);
 		return;
 	} else if ((strcmp(cmd, "QUIT") == 0)
-		|| (strcmp(cmd, "PART") == 0)) {
+		   || (strcmp(cmd, "PART") == 0)) {
 		snprintf(msg, sizeof(msg), "QUIT :%s\r\n", buf);
 		nick_add_symb(nick, netid);
 		if (htsearch(users, nick) != NULL)
@@ -498,8 +506,8 @@
 			return;
 		}
 		if ((strcmp(networks[i].host, host) == 0)
-		&&  (strcmp(networks[i].port, port) == 0)
-		&&  (strcmp(networks[i].chan, chan) == 0)) {
+		    && (strcmp(networks[i].port, port) == 0)
+		    && (strcmp(networks[i].chan, chan) == 0)) {
 			printf("error: network configuration already exists\n");
 			return;
 		}
@@ -508,9 +516,11 @@
 	/* resize if full */
 	if (netlen == netcap) {
 		Htiter it = {0};
-		networks = erecalloc(networks, (size_t)netcap, NET_ADDEND, sizeof(struct network));
+		networks = erecalloc(networks, (size_t)netcap, NET_ADDEND,
+				     sizeof(struct network));
 		while (htiterate(users, &it))
-			it.node->val = erecalloc(it.node->val, (size_t)netcap, NET_ADDEND, sizeof(int));
+			it.node->val = erecalloc(it.node->val, (size_t)netcap,
+						 NET_ADDEND, sizeof(int));
 		netcap += NET_ADDEND;
 	}
 
@@ -605,7 +615,8 @@
 {
 	Htiter it = {0};
 	while (htiterate(users, &it))
-		((int *)it.node->val)[netid] = clone_add(loop, it.node->key, netid);
+		((int *)it.node->val)[netid] = clone_add(loop, it.node->key,
+							 netid);
 }
 
 void
@@ -719,7 +730,8 @@
 	int i = evlen;
 
 	if (evlen == evcap) {
-		events = erecalloc(events, (size_t)evcap, EVENT_ADDEND, sizeof(struct event));
+		events = erecalloc(events, (size_t)evcap, EVENT_ADDEND,
+				   sizeof(struct event));
 		evcap += EVENT_ADDEND;
 	}