From 89a06314a63cff322c71b080869de48f47319078 Mon Sep 17 00:00:00 2001
From: ubq323 <ubq323@ubq323.website>
Date: Thu, 13 Jan 2022 23:05:02 +0000
Subject: add parsing of irc messages, not used anywhere yet

---
 thing.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 66 insertions(+), 6 deletions(-)

diff --git a/thing.c b/thing.c
index b3854ae..6914624 100644
--- a/thing.c
+++ b/thing.c
@@ -46,11 +46,11 @@ ssize_t sendall(int sockfd, void *buf, size_t len) {
 		}
 		total_sent += sent;
 	} while (total_sent < len);
-
+#ifdef DEBUG
 	char *the = strndup(buf, len);
 	printf("SEND [%s]\n",the);
 	free(the);
-
+#endif
 	return total_sent;
 }
 
@@ -97,19 +97,75 @@ int irc_handshake(int sockfd) {
 
 void irc_handle(int sockfd, char *start, char *end) {
 	DP("irc_handle...");
+#ifdef DEBUG
     char *the = strndup(start,end-start);
 	printf("HANDLE [%s]\n",the);
 	free(the);
+#endif
 	
 	if (strncmp(start, "PING", 4) == 0) { 
 		start[1] = 'O';
 		sendall(sockfd, start, end-start);
 		start[1] = 'I';
+		return;
 	}
 
-	
+	char *msg = start;
+	size_t msglen = end-start;
+	if (msglen > 512) {
+		fprintf(stderr,"message longer than 512 bytes, this is bad probably\n");
+		return;
+	}
+
+	// this is quite bad
+	// i should change it one day
+
+	char msg_source[512];
+	msg_source[0] = '\0';
+
+	char msg_command[512];
+	msg_command[0] = '\0';
 
+	char msg_params[16][512];
+	for (int i = 0; i < 16; i++)
+		msg_params[i][0] = '\0';
 	
+	int i = 0;
+	if (msg[0] == ':') {
+		for (i = 1; i < msglen && msg[i] != ' ' && msg[i] != '\r' && msg[i] != '\n'; i++)
+			msg_source[i-1] = msg[i];
+		msg_source[i-1] = '\0';
+		i++;
+	}
+	int j;
+	for (j=0; i < msglen && msg[i] != ' ' && msg[i] != '\r' && msg[i] != '\n'; i++, j++)
+		msg_command[j] = msg[i];
+	msg_command[j] = '\0';
+	i++;
+
+	for (int px = 0; px < 16; px++) {
+		if (i >= msglen) break;
+		if (msg[i] == ':') {
+			memcpy(msg_params[px],&msg[i+1],msglen-i-2);
+			msg_params[px][msglen-i-1] = '\0';
+			break;
+		}
+		for (j = 0; i < msglen && msg[i] != ' ' && msg[i] != '\r' && msg[i] != '\n'; i++, j++)
+			msg_params[px][j] = msg[i];
+		msg_params[px][j] = '\0';
+		i++;
+	}
+#ifdef DEBUG
+	printf("source [%s]\n",msg_source);
+	printf("command [%s]\n",msg_command);
+	printf("params: ");
+	for (int px = 0; px < 16; px++)
+		printf("%d:[%s] ",px,msg_params[px]);
+	printf("\n");
+#endif
+
+
+
 }
 
 #define IRC_RECVBUF_SIZE 1024
@@ -132,11 +188,11 @@ void irc_recv(int sockfd) {
 		perror("recv");
 		exit(1);
 	}
-
+#ifdef DEBUG
 	char *the = strndup(irc_recvbuf_cur,s);
 	printf("RECV %ld [%s]\n",s,the);
 	free(the);
-
+#endif
 	char *buf_last = irc_recvbuf_cur+s;
 
 	char *cur;
@@ -252,7 +308,9 @@ int main(int argc, char *argv[]) {
 			.events = POLLIN,
 		},
 	};
+#ifdef DEBUG
 	printf("POLLIN %d POLLERR %d POLLHUP %d\n",POLLIN,POLLERR,POLLHUP);
+#endif
 
 	for (;;) {
 		int r;
@@ -260,7 +318,9 @@ int main(int argc, char *argv[]) {
 			perror("poll");
 			return 1;
 		}
+#ifdef DEBUG
 		printf("sock %d unix %d\n",pollfds[1].revents, pollfds[0].revents);
+#endif
 
 
 		if (pollfds[1].revents & POLLHUP) {
@@ -286,7 +346,7 @@ int main(int argc, char *argv[]) {
 				return 1;
 			}
 			for (size_t i = 0; i < amt; i++)
-				if (buf[i] == '\r' || buf[i] == '\n' || buf[i] < ' ')
+				if (buf[i] == '\r' || buf[i] == '\n' || buf[i] == '\0')
 					buf[i] = ' ';
 			char msg[] = "PRIVMSG "CHANNEL" :";
 
-- 
cgit v1.2.3