irC

Unnamed repository
Log | Files | Refs | README

commit 03e0a6da9b7cad912e067150ac1d21b401233177
parent 07e0027af49a1071c14b45d3d6ff576e590a27c2
Author: aabacchus <ben@bvnf.space>
Date:   Mon, 21 Jun 2021 09:03:53 +0100

base

Diffstat:
Amain.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 95 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c @@ -0,0 +1,95 @@ +#include <stdio.h> +#include <errno.h> +#include <sys/socket.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <netdb.h> + +#define BUF_LEN 512 + +int sfd; +void usage(const char *argv0); +int ircWrite(char *format, ...); +int ircRead(); + +int main(int argc, char *argv[]) { + int s, c; + struct addrinfo hints; + struct addrinfo *result; + memset(&hints, 0, sizeof hints); + if (argc < 3) { + usage(argv[0]); + } + + s = getaddrinfo(argv[1], argv[2], &hints, &result); + if (s != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + exit(EXIT_FAILURE); + } + sfd = socket(AF_INET,SOCK_STREAM,0); +// protocol 6 -> TCP (/etc/protocols) + if (sfd < -1) { + fprintf(stderr, "error creating socket\n"); + return errno; + } + c = connect(sfd, result->ai_addr, result->ai_addrlen); + fprintf(stderr, "connecting to %s\n", result->ai_canonname); + if (c != 0) { + fprintf(stderr, "error connecting to %s\n", result->ai_canonname); + close(sfd); + return errno; + } + + // connect as USER + char *startmsg; + ssize_t startmsglen; + startmsg = "NICK hirC\r\nUSER irC 0 * :phoebos\r\n"; + startmsglen = strlen(startmsg); + if (write(sfd, &startmsg, startmsglen) != startmsglen) { + fprintf(stderr, "incomplete write\n"); + return errno; + } + printf("we're in.\r\n"); + ircWrite("PRIVMSG phoebos :hi there from hirC\r\n"); + printf("we're in..\n"); + ircRead(); + printf("we've read\n"); + char *quit = 0; + char *line = NULL; + size_t * linelen = 0; + while (*quit == 0) { + // read from stdin + *line = NULL; + if (getline(&line, linelen, stdin) != 0) *quit = 2; + if (strncmp(line, "/quit", 5) == 0) *quit = 1; + ircWrite(line); + //ircWrite("PRIVMSG phoebos :hi there from hirC\r\n"); + ircRead(); + } + + close(sfd); + return 0; +} + +void usage(const char *argv0){ + fprintf(stderr, "usage: %s host port\n", argv0); + exit(1); +} + +int ircWrite(char *format, ...){ + ssize_t ret; + ret = write(sfd, format, strlen(format)); + if (!ret) perror("ircWrite"); //exit(errno); + return 0; +} + +int ircRead(){ + char buff[BUF_LEN]; + ssize_t ret; + size_t nbytes = sizeof(buff); + ret = read(sfd, buff, BUF_LEN); + printf("Received %zd bytes:\n%s\n", ret, buff); + if (ret != nbytes) perror("ircRead"); //exit(errno); + return 0; +}