sxirc

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 2b82fd514708a80dc4f2e59d52e9f57a0dd31ec9
parent 2254ae69911673c071276967d6823316939ae7be
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 29 Nov 2022 16:10:02 +0000

add irc infra

Diffstat:
Msxirc.c | 64+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/sxirc.c b/sxirc.c @@ -1,5 +1,9 @@ +#define _POSIX_C_SOURCE 200809L +#include <netdb.h> +#include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <sys/socket.h> #include <unistd.h> #include <libsx.h> @@ -104,10 +108,68 @@ setup_sx(int argc, char **argv, XtAppContext *app, Widget *output) { return 0; } +int sock_write(int sfd, char *msg, ...){ + va_list ap; + va_start(ap, msg); + if (vdprintf(sfd, msg, ap) < 0) { + perror("sock_write"); + va_end(ap); + return 1; + } + va_end(ap); + return 0; +} + +int +server_connect(char *nodename, char *servname) { + int s, sfd; + struct addrinfo hints = {0}, *result, *rp; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + s = getaddrinfo(nodename, servname, &hints, &result); + if (s != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + return -1; + } + + for (rp = result; rp != NULL; rp = rp->ai_next) { + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (sfd == -1) + continue; + + if (connect(sfd, rp->ai_addr, rp->ai_addrlen) == 0) + break; + + if (close(sfd) == -1) { + perror("close"); + return -1; + } + } + + freeaddrinfo(result); + + if (rp == NULL) + return -1; + + return sfd; +} + int setup_irc(int *sockfd) { + /* initially hardcode the user details; TODO make a popup to prompt for them */ + char nodename[] = "irc.libera.chat"; + char servname[] = "6667"; + char nick[] = "sxirc"; int sfd; - sfd = 0; + + sfd = server_connect(nodename, servname); + if (sfd == -1) + return 1; + + /* identify */ + sock_write(sfd, "NICK %s\r\nUSER %s 8 x :%s\r\n", nick, nick, nick); *sockfd = sfd; return 0;