irC

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

irC.c (5965B)


      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
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
/*
 * This file is in the public domain.
 * Author: Ben Fuller
 *
 */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#define BUF_LEN 1024

void usage(const char *);
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[]) {
    char *nick;
    int sfd;

    if (argc < 4) {
        usage(argv[0]);
    }
    nick = argv[3];

    printf("connecting to %s...\n", argv[1]);
    sfd = server_connect(argv[1], argv[2]);
    if (sfd == -1) {
        return 1;
    }
    irc_identify(sfd, nick);

    size_t linelen = 0;

    int pollret;
    struct pollfd fds[2];
    fds[0].fd = 0;
    fds[0].events = POLLIN;
    fds[1].fd = sfd;
    fds[1].events = POLLIN | POLLWRBAND;

    while (1) {
        pollret = poll(fds, 2, -1);
        if (pollret <= 0) {
            if (pollret == 0 || errno == EAGAIN)
                continue;
            perror("poll");
            return 1;
        }
        if (fds[1].revents & POLLIN) {
            int rec = sock_read(sfd);
            if (rec < 0)
                return 1;
            else if (rec == 0) /* received FIN */
                break;
        }
        if (fds[0].revents & POLLIN) { /* TODO: stdio doesn't work perfectly with poll */
            char *line = NULL;
            if (getline(&line, &linelen, stdin) == -1) {
                shutdown(sfd, SHUT_WR);
                free(line);
                break;
            }
            if (linelen && line[0] == '/') {
                char *command = line;
                command++;
                char *channel;
                switch (*command) {
                    case 'q':
                        /* QUIT */
                        if (sock_write(sfd, "QUIT\r\n") != 0) {
                            free(line);
                            return 1;
                        }
                        shutdown(sfd, SHUT_WR);
                        free(line);
                        goto end;
                    case 'j':
                        /* JOIN */
                        channel = get_next_word(line);
                        if (sock_write(sfd, "JOIN :%s", channel) != 0) {
                            free(line);
                            return 1;
                        }
                        break;
                }
               free(line);
               continue;
            }
            if (sock_write(sfd, line) != 0) {
                free(line);
                return 1;
            }
            free(line);
        }
        fds[0].revents = 0;
        fds[1].revents = 0;
    }

end:
    if (close(sfd) == -1) {
        perror("close");
        return 1;
    }
    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;
    hints.ai_protocol = 0;

    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 irc_identify(int sfd, char *nick) {
    return sock_write(sfd, "NICK %s\r\nUSER %s 8 x :%s\r\n", nick, nick, nick);
}


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

char *get_next_word(char *line) {
    while (!isspace(*line))
        line++;
    while (isspace(*line))
        line++;
    return line;
}

int sock_write(int sfd, char *msg, ...){
    va_list ap;
    va_start(ap, msg);
    if (vdprintf(sfd, msg, ap) < 0) {
        perror("sock_write");
        return 1;
    }
    va_end(ap);
    return 0;
}

int sock_read(int sfd){
    unsigned char buff[BUF_LEN];
    memset(&buff, 0, sizeof buff);

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

    if (ret < 0) {
        perror("sock_read");
        return -1;
    }
    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);
}