summary refs log tree commit diff stats
path: root/receive.c
diff options
context:
space:
mode:
authoraabacchus <ben@bvnf.space>2023-05-27 19:21:23 +0100
committeraabacchus <ben@bvnf.space>2023-05-27 19:21:23 +0100
commit72925c599a0870ada9100a69aa6500b04d0fd84e (patch)
tree79753fa2ca781fe43f150645aa4a4fe971bbe89f /receive.c
parent191856c5a614e9765dfe0308125bf395557bea21 (diff)
downloadcbot-72925c599a0870ada9100a69aa6500b04d0fd84e.tar.gz
write from client to irc
Diffstat (limited to 'receive.c')
-rw-r--r--receive.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/receive.c b/receive.c
index d85a1ac..9b620e1 100644
--- a/receive.c
+++ b/receive.c
@@ -1,3 +1,5 @@
+#define _POSIX_C_SOURCE
+#include <assert.h>
 #include <err.h>
 #include <errno.h>
 #include <netdb.h>
@@ -15,26 +17,45 @@
 #define SOCK_PATH "/tmp/kissbot"
 #define IRC_HOST  "irc.libera.chat"
 #define IRC_PORT  "6667"
-#define IRC_NICK  "_kissbot_"
+#define IRC_NICK  "kissbot_"
+#define IRC_CHANNEL "#test"
 
 int sock_write(int s, char *msg, ...);
 
 int
-read_print(int s) {
+handle_client(int client, int irc) {
 	char buf[0x1000];
 	ssize_t n;
-	n = recv(s, buf, sizeof buf, 0);
-	if (n > 0)
-		write(1, buf, n);
-	if (n < 0)
+	n = recv(client, buf, (sizeof buf) - 1, 0);
+	if (n < 0) {
 		perror("recv");
+		return 1;
+	}
+	assert((size_t)n < sizeof buf);
+	char *b = buf;
+	while (1) {
+		char *l = memchr(b, '\n', n - (b - buf));
+		if (l == NULL)
+			buf[n+1] = '\0';
+		else
+			*l = '\0';
+
+		sock_write(irc, "PRIVMSG " IRC_CHANNEL " :%s\n", b);
+
+		if (l == NULL)
+			break;
+		else
+			b = l + 1;
+		if (l - buf >= n - 1) /* -1 because the end of the line became \0 */
+			break;
+	}
 	return 0;
 }
 
 int
 parse_irc(int s, char *p, size_t len) {
 	/* look for the beginning of lines and handle some commands */
-	fprintf(stderr, "parse_irc got '%s'\n", p);
+	//fprintf(stderr, "parse_irc got %zu bytes '%s'\n", len, p);
 	while (1) {
 		char *e = memchr(p, '\n', len);
 		size_t off = len;
@@ -70,10 +91,11 @@ parse_irc(int s, char *p, size_t len) {
 
 int
 handle_irc(int s) {
-	char buf[0x1000];
-	ssize_t n = recv(s, buf, sizeof buf, 0);
+	char buf[0x1001];
+	ssize_t n = recv(s, buf, (sizeof buf)-1, 0);
 	if (n <= 0)
 		return 1;
+	buf[n] = '\0';
 	return parse_irc(s, buf, n);
 }
 
@@ -116,7 +138,7 @@ sock_write(int s, char *msg, ...) {
 
 int
 irc_identify(int s) {
-	return sock_write(s, "NICK %s\r\nUSER %s 8 x :%s\r\n", IRC_NICK, IRC_NICK, IRC_NICK);
+	return sock_write(s, "NICK %s\r\nUSER %s 8 x :%s\r\nJOIN :%s\r\n", IRC_NICK, IRC_NICK, IRC_NICK, IRC_CHANNEL);
 }
 
 int
@@ -200,7 +222,7 @@ main(void) {
 			errx(1, "unexpected error, quitting\n");
 		}
 		if (fds[2].revents & POLLIN) {
-			read_print(fds[2].fd);
+			handle_client(fds[2].fd, fds[1].fd);
 		}
 		if (fds[2].revents & POLLHUP) {
 			close(fds[2].fd);
@@ -230,8 +252,7 @@ int main(void) {
 		{strdup("PING :ih"), 8},
 		{strdup(""), 0},
 		{strdup("PING no colon\n"), 14},
-		{strdup("PING :too short len"), 10},
-		{strdup("PING :too long len"), 20},
+		{strdup("PING :first line\nPING :second\nASDF :not a ping\nPING :3rd ping\n"), 62},
 	};
 	printf("%s", s[0].s);
 	for (size_t i = 0; i < sizeof(s)/sizeof(s[0]); i++) {