irC

Unnamed repository
Log | Files | Refs | README

commit 56309a4479f21084931f7c574fa99c7817629c8c
parent 03e0a6da9b7cad912e067150ac1d21b401233177
Author: aabacchus <ben@bvnf.space>
Date:   Mon, 21 Jun 2021 09:17:01 +0100

tidy

Diffstat:
Mmain.c | 58+++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/main.c b/main.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <errno.h> #include <sys/socket.h> +#include <sys/types.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -15,56 +16,72 @@ int ircRead(); int main(int argc, char *argv[]) { int s, c; - struct addrinfo hints; - struct addrinfo *result; - memset(&hints, 0, sizeof hints); + struct addrinfo hints, *result; if (argc < 3) { usage(argv[0]); } - s = getaddrinfo(argv[1], argv[2], &hints, &result); - if (s != 0) { + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + if ((s = getaddrinfo(argv[1], argv[2], &hints, &result)) != 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) { + + sfd = socket(result->ai_family, result->ai_socktype, result->ai_protocol); + + if (sfd < 0) { fprintf(stderr, "error creating socket\n"); + perror("socket"); return errno; } + c = connect(sfd, result->ai_addr, result->ai_addrlen); + fprintf(stderr, "connecting to %s\n", result->ai_canonname); - if (c != 0) { + if (c == -1) { fprintf(stderr, "error connecting to %s\n", result->ai_canonname); close(sfd); + perror("connect"); return errno; } // connect as USER char *startmsg; ssize_t startmsglen; - startmsg = "NICK hirC\r\nUSER irC 0 * :phoebos\r\n"; + startmsg = "NICK hirC\r\nUSER hirC- 8 * :hirC-\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(); + ircWrite("JOIN #abcdefghhh\r\n"); + printf("we're in.. #abcdefghhh\n"); ircRead(); printf("we've read\n"); + ircWrite("PRIVMSG #abcdefghhh :hi there!\r\n"); + ircRead(); + printf("we've read after PRIVMSG\n"); char *quit = 0; char *line = NULL; - size_t * linelen = 0; - while (*quit == 0) { + size_t linelen = 0; + printf("gonna go into loop\n"); + while (quit == 0) { + printf("going into loop\n"); // read from stdin - *line = NULL; - if (getline(&line, linelen, stdin) != 0) *quit = 2; - if (strncmp(line, "/quit", 5) == 0) *quit = 1; + //*line = ""; + printf("gonna getline\n"); + if (getline(&line, &linelen, stdin) == -1) quit = (char *) 2; + printf("gotline: %s\n", line); + //if (strncmp(line, "/quit", 5) == 0) quit = 1; + //printf("strncmp\n"); ircWrite(line); - //ircWrite("PRIVMSG phoebos :hi there from hirC\r\n"); + free(line); ircRead(); } @@ -90,6 +107,9 @@ int ircRead(){ 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); + if (ret != (ssize_t) nbytes) { + perror("ircRead"); + printf("errno is %d\n", errno); + } return 0; }