aboutsummaryrefslogtreecommitdiff
path: root/thing.c
blob: 2ffd84384f1e53cc442ced5d309b70236094dc80 (plain)
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
#ifdef LOCAL
#define HOST "0.0.0.0"
#define PORT "6970"
#else
#define HOST "ubq323.website"
#define PORT "6667"
#endif

#define NICK "ffbm"
#define USERNAME "ffbm"
#define REALNAME ",flappy fly bird man?"
#define CHANNEL "#a"

#define PIPE "/run/apionet.pipe"

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int hasjoined = 0;

ssize_t sendall(int sockfd, void *buf, size_t len) {
    ssize_t total_sent = 0;
    ssize_t sent;
    do {
        if ((sent = send(sockfd, buf+total_sent, len-total_sent, 0)) == -1) {
            return -1;
        }
        total_sent += sent;
    } while (total_sent < len);
    total_sent = 0;
    do {
        if ((sent = write(0, buf+total_sent, len-total_sent)) == -1) {
            return -1;
        }
        total_sent += sent;
    } while (total_sent < len);
    return total_sent;
}

int irc_connect() {
    struct addrinfo hints;
    int status, sockfd; 
    struct addrinfo *res;
    
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    // assuming the first one is valid because it normally is 

    if ((status = getaddrinfo(HOST, PORT, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo fail: %s\n", gai_strerror(status));
        return -1;
    }

    if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
        perror("socket");
        return -1;
    }
    if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) {
        perror("connect");
        return -1;
    }
    return sockfd;
}

int irc_handshake(int sockfd) {
    char msg[] = "NICK "NICK"\r\nUSER "USERNAME" 0 * :"REALNAME"\r\nJOIN "CHANNEL"\r\n";
    if (sendall(sockfd, msg, sizeof msg-1) == -1) {
        return -1;
    }
    return 0;
}

void irc_handle(int sockfd, char *start, char *end) {
    if (strncmp(start, "PING", 4) == 0) { 
        start[1] = 'O';
        sendall(sockfd, start, end-start);
        start[1] = 'I';
    }
    char *str = strndup(start, end-start);    
    printf("[%s]\n",str);
    free(str);
}

void irc_recv(int sockfd) {
    char recvbuf[1024];
    ssize_t amt;
    if ((amt = recv(sockfd, recvbuf, 1024, 0)) == -1) {
        perror("recv");
        exit(1);
    }

    char *start = &recvbuf[0];
    char *cur;
    char *end = start+amt ;
    for (cur = start; cur < end; cur++) { 
        if (*cur == '\r' && *(cur+1) == '\n') {
            irc_handle(sockfd, start, cur+2);
            start = cur + 2;
            cur = start;
        }
    }
}

int main(int argc, char *argv[]) {
    // this only works on linux, i think.
    // sorry
    int pipefd = open(PIPE, O_RDWR|O_NONBLOCK);
    if (pipefd == -1) {
        perror("(pipe) open");
        return 1;
    }

    printf("bees\n");
    int sockfd = irc_connect();
    if (sockfd == -1) {
        return 1;
    }
    if (irc_handshake(sockfd) != 0) {
        perror("irc_handshake");
        return 1;
    }

    // setup pollfds
    struct pollfd pollfds[2] = {
        {
            .fd = pipefd,
            .events = POLLIN,
        }, 
        {
            .fd = sockfd,
            .events = POLLIN,
        },
    };
    printf("POLLIN %d POLLERR %d POLLHUP %d\n",POLLIN,POLLERR,POLLHUP);
    for (;;) {
        int r;
        if ((r = poll(pollfds, 2, -1)) == -1) {
            perror("poll");
            return 1;
        }
        printf("poll %d\n",r);
        printf("sock %d pipe %d\n",pollfds[1].revents, pollfds[0].revents);
        
        if (pollfds[1].revents & POLLHUP) {
            // socket closed, probably
            puts("socket hup");
            break;
        }
        if (pollfds[1].revents & POLLERR) {
            // socket error 
            puts("socket err");
            break;
        }
        if (pollfds[1].revents & POLLIN) {
            // socket 
            char buf[1];
            // there is probably a better way of doing this 
            // but if there is, i don't know what it is
            ssize_t h = recv(sockfd, buf, 1, MSG_PEEK);
            if (h<0) {
                perror("recv");
                return -1;
            } else if (h == 0) {
                // eof ish 
                puts("socket end");
                break;
            } else {
                irc_recv(sockfd);
            }
        }
        if (pollfds[0].revents & POLLIN) {
            puts("pipe");
            ssize_t amt;
            unsigned char buf[128];
            if ((amt = read(pipefd, buf, 128)) == -1) {
                perror("(pipe) read");
                return 1;
            }
            for (size_t i = 0; i < amt; i++) {
                if (buf[i] == '\r' || buf[i] == '\n' || buf[i] < ' ') {
                    buf[i] = ' ';
                }
            }
            char msg[] = "PRIVMSG "CHANNEL" :";

            if (sendall(sockfd, msg, sizeof msg-1) == -1) { perror("sendall"); exit(1); }
            if (sendall(sockfd, buf, amt) == -1) { perror("sendall"); exit(1); }
            if (sendall(sockfd, "\r\n", 2) == -1) { perror("sendall"); exit(1); }
        }
    }
        
    
    
    
    return 0;
}