irC

an attempt at writing an irc "client"
git clone git://bvnf.space/irC.git
Log | Files | Refs | README

commit e46991174fc09daa66ffe2cbb5c875e43cffcad7
parent d66db6cb9eeacaf866fba7d7b7fa4eb97a793946
Author: aabacchus <ben@bvnf.space>
Date:   Sun, 13 Mar 2022 03:54:10 +0000

begin parsing server data: PING

Diffstat:
MirC.c | 44+++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/irC.c b/irC.c @@ -3,6 +3,7 @@ * Author: Ben Fuller * */ +#include <assert.h> #include <ctype.h> #include <errno.h> #include <netdb.h> @@ -22,6 +23,8 @@ int sock_write(int sfd, char *msg, ...); int sock_read(int); int server_connect(char *nodename, char *servname); int irc_identify(int sfd, char *nick); +void handle_server_input(int sfd, unsigned char *buf, size_t len); +void parse_line(int sfd, unsigned char *buf, size_t len); char *get_next_word(char *); int main(int argc, char *argv[]) { @@ -190,6 +193,45 @@ int sock_read(int sfd){ perror("sock_read"); return -1; } - write(1, buff, ret); + if (ret == 0) + return 0; + + handle_server_input(sfd, buff, (size_t)ret); + //write(1, buff, ret); return ret; } + +void handle_server_input(int sfd, unsigned char *buf, size_t len) { + unsigned char *nextline, *orig = buf; + do { + assert(len >= (size_t)(buf - orig)); + nextline = memchr(buf, '\n', len - (buf - orig)); + if (nextline == NULL) + parse_line(sfd, buf, len - (buf - orig)); + else { + parse_line(sfd, buf, 1 + nextline - buf); + buf = nextline + 1; + } + } while (nextline != NULL); + +} + +void parse_line(int sfd, unsigned char *buf, size_t len) { + if (memcmp(buf, "PING", 4) == 0) { + unsigned char *d = memchr(buf, ':', len); + if (d == NULL) + d = buf + 4 + 1; /* 4 for PING + 1 for a space */ + else + d++; + + /* bit messy: buf[len] is probably a '\n', but so that we can use + * printf, remember the byte there and write a 0, then restore the byte. + */ + unsigned char old = buf[len]; + buf[len] = '\0'; + sock_write(sfd, "PONG :%s\n", d); + //printf("PONG :%s\n", d); + buf[len] = old; + } else + write(1, buf, len); +}