irC

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

main.c (2920B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.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 ircQuit();

int main(int argc, char *argv[]) {
    int s, c;
    struct addrinfo hints, *result;
    if (argc < 3) {
        usage(argv[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(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 == -1) {
        fprintf(stderr, "error connecting to %s\n", result->ai_canonname);
        close(sfd);
        perror("connect");
        return errno;
    }
    freeaddrinfo(result);
    
    // connect as USER
    char *startmsg;
    startmsg = "NICK hirC\r\nUSER hirC- 8 x :hirC-\r\n";
    ircWrite(startmsg);
    printf("%s", startmsg);
    ircRead();
    ircWrite("JOIN #abcdefghhh\r\n");
    printf("JOIN #abcdefghhh\r\n");
    ircRead();
    printf("we've read\n");
    ircWrite("PRIVMSG #abcdefghhh :hi there!\r\n");
    printf("PRIVMSG #abcdefghhh :hi there!\r\n");
    ircRead();
    printf("we've read after PRIVMSG\n");

    char *quit = 0;
    size_t linelen = 0;
    printf("gonna go into loop\n");
    while (quit == 0) {
        char *line = NULL;
        //printf("going into loop\n");
        // read from stdin
        //printf("gonna getline\n");
        if (getline(&line, &linelen, stdin) == -1) quit = (char *) 2;
        //printf("gotline: %s\n", line);
        if (strncmp(line, "/quit", 5) == 0) return ircQuit(); //quit = (char *) 1;
        //printf("strncmp\n");
        ircWrite(line);
        free(line);
        ircRead();
    }

    close(sfd);
    return 0;
}

void usage(const char *argv0){
    fprintf(stderr, "usage: %s host port\n", argv0);
    exit(1);
}

int ircWrite(char *msg, ...){
    ssize_t ret;
    int len = strlen(msg);
    ret = send(sfd, msg, len, 0);
    if (ret != (ssize_t) len) {
        perror("ircWrite");
        return errno;
    }
    return 0;
}

int ircRead(){
    char buff[BUF_LEN];
    memset(&buff, 0, sizeof buff);
    ssize_t ret;

    ret = recv(sfd, buff, BUF_LEN, 0);

    printf("Received %zd bytes:\n%s\n", ret, buff);
    if (ret < 0) {
        perror("ircRead");
        return errno;
    }
    return 0;
}

int ircQuit(){
    ircWrite("QUIT");
    return ircRead();
}