aboutsummaryrefslogtreecommitdiffstats
path: root/kirc.c
blob: 3dac1dc850a252527859844851bf39aacc9736fc (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
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
238
/* See LICENSE file for license details. */
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <stdarg.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <termios.h>

#define MSG_MAX       512                /* guaranteed max message length */
#define CHA_MAX       200                /* gauranteed max channel length */

static int    conn;                      /* connection socket */
static size_t verb = 0;                  /* verbose output (e.g. raw stream) */
static size_t cmax = 80;                 /* max number of chars per line */
static size_t gutl = 10;                 /* max char width of left column */
static char * host = "irc.freenode.org"; /* irc host address */
static char * chan = "kirc";             /* channel */
static char * port = "6667";             /* server port */
static char * nick = NULL;               /* nickname */
static char * pass = NULL;               /* server password */
static char * user = NULL;               /* server user name */
static char * real = NULL;               /* server user real name */
static char * olog = NULL;               /* log irc stream path */
static char * inic = NULL;               /* server command after connection */

static void
printa(char *str) {
    FILE *out = fopen(olog, "a");
    fprintf(out, "%s", str);
    fclose(out);
}

static int
kbhit(void) {

    int    byteswaiting;
    fd_set fds;
    struct timespec ts = {0};

    FD_ZERO(&fds);
    FD_SET(0, &fds);
    byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL);

    return byteswaiting > 0;
}

static void
raw(char *fmt, ...) {

    va_list ap;
    char *cmd_str = malloc(MSG_MAX);

    va_start(ap, fmt);
    vsnprintf(cmd_str, MSG_MAX, fmt, ap);
    va_end(ap);

    if (verb) printf("<< %s", cmd_str);
    if (olog) printa(cmd_str);
    if (write(conn, cmd_str, strlen(cmd_str)) < 0) exit(1);

    free(cmd_str);
}

static void
irc_init() {

    struct addrinfo *res, hints = {
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM
    };

    getaddrinfo(host, port, &hints, &res);
    conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    connect(conn, res->ai_addr, res->ai_addrlen);
    fcntl(conn, F_SETFL, O_NONBLOCK);
}

static void
printw(const char *format, ...) {

    va_list argptr;
    char    *tok, line[MSG_MAX];
    size_t  i, wordwidth, spaceleft, spacewidth = 1;

    va_start(argptr, format);
    vsnprintf(line, MSG_MAX, format, argptr);
    va_end(argptr);

    if (olog) printa(line);

    for (i = 0; isspace(line[i]); i++) putchar(line[i]);

    spaceleft = cmax + gutl - (i - 1);

    for(tok = strtok(&line[i], " "); tok != NULL; tok = strtok(NULL, " ")) {
        wordwidth = strlen(tok);
        if ((wordwidth + spacewidth) > spaceleft) {
            printf("\n%*.s%s ", (int) gutl + 1, "", tok);
            spaceleft = cmax - (gutl + 1 + wordwidth);
        } else {
            printf("%s ", tok);
            spaceleft = spaceleft - (wordwidth + spacewidth);
        }
    }
    printf("\n");
}

static void
raw_parser(char *usrin) {
    if (verb) printf(">> %s\n", usrin);
    if (!strncmp(usrin, "PING", 4)) {
        usrin[1] = 'O';
        raw("%s\r\n", usrin);
    } else if (usrin[0] == ':') {
        char *prefix = strtok(usrin, " ") + 1, *suffix = strtok(NULL, ":"),
             *message = strtok(NULL, "\r"), *nickname = strtok(prefix, "!"),
             *command = strtok(suffix, "#& "), *channel = strtok(NULL, " ");
        if (!strncmp(command, "001", 3)) raw("JOIN #%s\r\n", chan);
        else if (!strncmp(command, "QUIT", 4)) {
            printw("%*s \x1b[34;1m%s\x1b[0m", (int)gutl, "<--", nickname);
        } else if (!strncmp(command, "JOIN", 4)) {
            printw("%*s \x1b[32;1m%s\x1b[0m", (int)gutl, "-->", nickname);
        } else if (!strncmp(command, "PRIVMSG", 7) && 
                   !strncmp(channel, nick, strlen(nick))) {
            int s = gutl - (strlen(nickname) <= gutl ? strlen(nickname) : gutl);
            printw("%*s\x1b[43;1m%-.*s\x1b[0m %s", s, "", (int)gutl, nickname, message);
        } else if (!strncmp(command, "PRIVMSG", 7) && strstr(channel, chan) == NULL) {
            int s = gutl - (strlen(nickname) <= gutl ? strlen(nickname) : gutl);
            printw("%*s\x1b[33;1m%-.*s\x1b[0m [%s] %s", s, "", (int)gutl, nickname, channel, message);
        } else {
            int s = gutl - (strlen(nickname) <= gutl ? strlen(nickname) : gutl);
            printw("%*s\x1b[33;1m%-.*s\x1b[0m %s", s, "", (int)gutl, nickname, message);
        }
    }
}
int
main(int argc, char **argv) {

    int fd[2], cval;

    while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:x:w:W:vV")) != -1) {
        switch (cval) {
            case 'v' : puts("kirc-0.0.8");  return 0;
            case 'V' : verb = 1;            break;
            case 's' : host = optarg;       break;
            case 'w' : gutl = atoi(optarg); break;
            case 'W' : cmax = atoi(optarg); break;
            case 'p' : port = optarg;       break;
            case 'r' : real = optarg;       break;
            case 'u' : user = optarg;       break;
            case 'o' : olog = optarg;       break;
            case 'n' : nick = optarg;       break;
            case 'k' : pass = optarg;       break;
            case 'c' : chan = optarg;       break;
            case 'x' : inic = optarg;       break;
            case '?' :                      return 1;
        }
    }

    if (!nick) {
        fprintf(stderr, "nick not specified\n");
        return 1;
    }

    if (pipe(fd) < 0) {
        fprintf(stderr, "pipe() failed\n");
        return 2;
    }

    pid_t pid = fork();

    if (pid == 0) {
        int  sl, i, o = 0;
        char u[MSG_MAX], s, b[MSG_MAX];

        irc_init();

        if (nick)                   raw("NICK %s\r\n", nick);
        if (user && real)           raw("USER %s - - :%s\r\n", user, real);
        if (user && !real && nick)  raw("USER %s - - :%s\r\n", user, nick);
        if (!user && !real && nick) raw("USER %s - - :%s\r\n", nick, nick);
        if (pass)                   raw("PASS %s\r\n", pass);
        if (inic)                   raw("%s\r\n", inic);

        while ((sl = read(conn, &s, 1))) {
            if (sl > 0) b[o] = s;

            if ((o > 0 && b[o - 1] == '\r' && b[o] == '\n') || o == MSG_MAX) {
                b[o + 1] = '\0';
                raw_parser(b);
                o = 0;
            } else if (sl > 0) o++;

            if (read(fd[0], u, MSG_MAX) > 0) {
                for (i = 0; u[i] != '\n'; i++) continue;
                if (u[0] != '/') raw("%-*.*s\r\n", i, i, u);
            }
        }
    }
    else {
        char usrin[MSG_MAX], v1[MSG_MAX - CHA_MAX], v2[CHA_MAX], c1;
        struct termios tp, save;

        tcgetattr(STDIN_FILENO, &tp);
        save = tp;
        tp.c_cc[VERASE] = 127;

        if (tcsetattr(STDIN_FILENO, TCSANOW, &tp) < 0) return 2;
        while (waitpid(pid, NULL, WNOHANG) == 0) {
            if (!kbhit()) dprintf(fd[1], "/\n");
            else if (fgets(usrin, MSG_MAX, stdin) != NULL) {
                if (sscanf(usrin, "/%[m] %s %[^\n]\n", &c1, v2, v1) > 2 ||
                    sscanf(usrin, "/%[xMQqnjp] %[^\n]\n", &c1, v1) > 0) {
                    switch (c1) {
                        case 'x': dprintf(fd[1], "%s\n", v1);                   break;
                        case 'q': dprintf(fd[1], "quit\n");                     break;
                        case 'Q': dprintf(fd[1], "quit %s\n", v1);              break;
                        case 'j': dprintf(fd[1], "join %s\n", v1);              break;
                        case 'p': dprintf(fd[1], "part %s\n", v1);              break;
                        case 'n': dprintf(fd[1], "names #%s\n", chan);          break;
                        case 'M': dprintf(fd[1], "privmsg nickserv :%s\n", v1); break;
                        case 'm': dprintf(fd[1], "privmsg %s :%s\n", v2, v1);   break;
                    }
                } else dprintf(fd[1], "privmsg #%s :%s", chan, usrin);
            }
        }

        if (tcsetattr(STDIN_FILENO, TCSANOW, &save) < 0) return 2;
        puts("<< connection closed");
    }
    return 0;
}