Repository navigation
Expand file tree
/
Copy pathirc.c
More file actions
523 lines (453 loc) · 16.4 KB
/
Copy pathirc.c
File metadata and controls
523 lines (453 loc) · 16.4 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "modules/modules.h"
#include "modules/constants.h"
#include "irc.h"
static volatile sig_atomic_t gtfotime = 0;
pthread_mutex_t screen_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t send_mutex = PTHREAD_MUTEX_INITIALIZER;
static unsigned int saved_cli_ctrl = 0;
static unsigned short tty_chan = 0;
static unsigned int exit_status;
static unsigned int ctrl_y_ef;
static int ctrl_y_ef_allocated = 0;
static int cli_ctrl_disabled = 0;
static int exit_handler_registered = 0;
static int screen_active = 0;
static volatile sig_atomic_t send_failed = 0;
static void restore_cli_ctrl(unsigned int *status) {
unsigned int mask = LIB$M_CLI_CTRLY;
if (cli_ctrl_disabled) {
if (saved_cli_ctrl & LIB$M_CLI_CTRLY) {
lib$enable_ctrl(&mask, 0);
}
cli_ctrl_disabled = 0;
}
}
static struct {
unsigned int flink;
void (*handler)(unsigned int *);
unsigned int arg_count;
unsigned int *status;
} exit_block = {0, restore_cli_ctrl, 1, &exit_status};
static void request_quit(void) {
gtfotime = 1;
if (tty_chan != 0) {
sys$cancel(tty_chan);
}
}
static void ctrly_ast(int unused);
static void ctrly_ast(int unused) {
request_quit();
}
static void ctrl_y_cleanup(void) {
unsigned int ast_status = sys$setast(0);
if (tty_chan != 0) {
sys$dassgn(tty_chan); // removes the one-shot Ctrl-Y AST.
tty_chan = 0;
}
if (ctrl_y_ef_allocated) {
lib$free_ef(&ctrl_y_ef);
ctrl_y_ef_allocated = 0;
}
restore_cli_ctrl(&exit_status);
if (exit_handler_registered) {
sys$canexh(&exit_block);
exit_handler_registered = 0;
}
if (ast_status == SS$_WASSET) {
sys$setast(1);
}
}
static unsigned int ctrl_y_init(void) {
$DESCRIPTOR(tty_name, "SYS$COMMAND");
unsigned int mask = LIB$M_CLI_CTRLY;
unsigned int status;
struct {
unsigned short status;
unsigned short count;
unsigned int unused;
} iosb;
status = sys$assign(&tty_name, &tty_chan, 0, 0);
if (!(status & 1)) return status;
status = lib$get_ef(&ctrl_y_ef);
if (!(status & 1)) goto fail;
ctrl_y_ef_allocated = 1;
status = sys$dclexh(&exit_block);
if (!(status & 1)) goto fail;
exit_handler_registered = 1;
// Check both submission and completion before taking over from DCL.
status = sys$qiow(ctrl_y_ef, tty_chan, IO$_SETMODE | IO$M_CTRLYAST,
&iosb, 0, 0, (void *)ctrly_ast, 0, PSL$C_USER, 0, 0, 0);
if (!(status & 1)) goto fail;
status = iosb.status;
if (!(status & 1)) goto fail;
status = lib$disable_ctrl(&mask, &saved_cli_ctrl);
if (!(status & 1)) goto fail;
cli_ctrl_disabled = 1;
return SS$_NORMAL;
fail:
ctrl_y_cleanup();
return status;
}
static int wait_for_socket(int client_socket, int writing) {
// ensure quit is always checked
while (!gtfotime) {
fd_set ready;
struct timeval timeout;
int status;
FD_ZERO(&ready);
FD_SET(client_socket, &ready);
timeout.tv_sec = 0;
timeout.tv_usec = 200000;
status = select(client_socket + 1, writing ? NULL : &ready,
writing ? &ready : NULL, NULL, &timeout);
if (gtfotime) return 0;
if (status > 0) return 1;
if (status < 0 && errno != EINTR) return -1;
}
return 0;
}
static int connect_to_server(int client_socket, struct sockaddr_in *address) {
int nonblocking = 1;
int error = 0;
unsigned int error_length = sizeof(error);
int status;
if (gtfotime) return 0;
if (ioctl(client_socket, FIONBIO, &nonblocking) < 0) return -1;
status = connect(client_socket, (struct sockaddr *)address, sizeof(*address));
if (status < 0) {
if (errno != EINPROGRESS && errno != EWOULDBLOCK && errno != EINTR) {
return gtfotime ? 0 : -1;
}
status = wait_for_socket(client_socket, 1);
if (status <= 0) return status;
if (getsockopt(client_socket, SOL_SOCKET, SO_ERROR,
&error, &error_length) < 0) return -1;
if (error != 0) {
errno = error;
return -1;
}
}
if (gtfotime) return 0;
return 1;
}
int send_irc(int client_socket, const char *message, size_t length) {
size_t offset = 0;
int result;
// Keep writes nonblocking even if ctrl-y hits before write posts
pthread_mutex_lock(&send_mutex);
while (offset < length && !gtfotime) {
int ready = wait_for_socket(client_socket, 1);
int sent;
if (ready == 0) break;
if (ready < 0) break;
sent = write(client_socket, message + offset, length - offset);
if (sent > 0) {
offset += sent;
} else if (sent < 0 && (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)) {
continue;
} else {
if (sent == 0) errno = EPIPE;
break;
}
}
if (gtfotime) {
result = 0;
} else if (offset == length) {
result = 1;
} else {
perror("write");
send_failed = 1;
request_quit();
result = -1;
}
pthread_mutex_unlock(&send_mutex);
return result;
}
void output_wrapped(WINDOW *output, const char *text) {
int row, column;
for (; *text != '\0'; ++text) {
if (*text == '\r' && text[1] == '\n') continue;
getyx(output, row, column);
// Leave the final column free to avoid curses' implicit edge handling.
if (*text == '\n' || ((unsigned char)*text >= ' ' && column >= COLS - 1)) {
waddch(output, '\n');
// OpenVMS newline keeps the old column; reset it after advancing.
getyx(output, row, column);
wmove(output, row, 0);
if (*text == '\n') continue;
}
waddch(output, *text);
}
}
void process_incoming_message(int client_socket, char *message, WINDOW *output) {
if (strncmp(message, "PING", 4) == 0) {
char pong[BUFFER_SIZE];
snprintf(pong, sizeof(pong), "PONG%s\r\n", message + 4);
pthread_mutex_lock(&screen_mutex);
output_wrapped(output, "Sending PONG response\n");
wrefresh(output);
pthread_mutex_unlock(&screen_mutex);
send_irc(client_socket, pong, strlen(pong));
} else if (strstr(message, " PRIVMSG ") != NULL) { // Check for PRIVMSG
privmsg_handler(client_socket, message, output);
} else {
pthread_mutex_lock(&screen_mutex);
output_wrapped(output, message);
output_wrapped(output, "\n");
wrefresh(output);
pthread_mutex_unlock(&screen_mutex);
// Display unhandled input. Useful to find out what to implement next and/or see
// unhandled server messages and types!
}
}
typedef struct {
int client_socket;
WINDOW *entry_window;
char *channel;
WINDOW *output_window;
const char *nickname;
} thread_data_t;
void *user_input_thread(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
int client_socket = data->client_socket;
WINDOW *entry_window = data->entry_window;
WINDOW *output_window = data->output_window;
char *channel = data->channel;
char input_message[BUFFER_SIZE];
char send_buffer[BUFFER_SIZE];
while (!gtfotime) {
pthread_mutex_lock(&screen_mutex);
wmove(entry_window, 1, 0);
wclrtoeol(entry_window);
wrefresh(entry_window);
pthread_mutex_unlock(&screen_mutex);
if (read_input_line(tty_chan, entry_window, input_message,
sizeof(input_message), >fotime) == ERR) {
request_quit();
break;
}
if (gtfotime) break;
if (strlen(input_message) > 0) {
if (strcmp(input_message, "/quit") == 0) {
request_quit();
break;
} else if (strncmp(input_message, "/msg", 4) == 0 &&
(input_message[4] == ' ' || input_message[4] == '\0')) {
char *recipient = input_message + 4;
char *message;
while (*recipient == ' ') ++recipient;
message = strchr(recipient, ' ');
// Incomplete /msg commands must never be sent to the channel.
if (message == NULL) continue;
*message++ = '\0';
while (*message == ' ') ++message;
if (*message == '\0') continue;
// Include the IRC prefix, separator, CRLF, and terminating NUL.
if (strlen(recipient) + strlen(message) + 13 > sizeof(send_buffer)) continue;
snprintf(send_buffer, sizeof(send_buffer), "PRIVMSG %s :%s\r\n",
recipient, message);
send_irc(client_socket, send_buffer, strlen(send_buffer));
} else {
if (strlen(channel) + strlen(input_message) + 13 > sizeof(send_buffer)) continue;
snprintf(send_buffer, BUFFER_SIZE, "PRIVMSG %s :%s\r\n", channel, input_message);
if (send_irc(client_socket, send_buffer, strlen(send_buffer)) == 1) {
pthread_mutex_lock(&screen_mutex);
output_wrapped(output_window, "<");
output_wrapped(output_window, data->nickname);
output_wrapped(output_window, "> ");
output_wrapped(output_window, input_message);
output_wrapped(output_window, "\n");
wrefresh(output_window);
pthread_mutex_unlock(&screen_mutex);
}
memset(input_message, 0, BUFFER_SIZE);
}
}
}
return NULL;
}
int process_initial_messages(int client_socket, WINDOW *output) {
char buffer[BUFFER_SIZE];
int registration_complete = 0;
while (!registration_complete && !gtfotime) {
int ready = wait_for_socket(client_socket, 0);
if (ready < 0) return -1;
if (ready == 0) break;
int n = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (gtfotime) break;
if (n < 0) {
if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) continue;
return -1;
} else if (n == 0) {
break;
}
buffer[n] = '\0';
char *message_start = buffer;
char *message_end;
while ((message_end = strstr(message_start, "\r\n")) != NULL) {
*message_end = '\0';
process_incoming_message(client_socket, message_start, output);
if (strstr(message_start, " 001 ") != NULL) { // Welcome message received
registration_complete = 1;
}
message_start = message_end + 2;
}
}
return registration_complete;
}
void endwin_noreturn(void) {
if (screen_active) {
endwin();
screen_active = 0;
}
}
int main(int argc, char **argv) {
WINDOW *output_window;
WINDOW *entry_window;
struct hostent *server;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
int client_socket = -1;
int result = EXIT_SUCCESS;
int input_thread_started = 0;
int status;
unsigned int vms_status;
pthread_t input_thread;
if (argc != 2) {
fprintf(stderr, "Usage: %s server\n", argv[0]);
return EXIT_FAILURE;
}
// Resolve before changing terminal or CLI state. it's a blocking call
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
return EXIT_FAILURE;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
memcpy(&server_addr.sin_addr.s_addr, server->h_addr, server->h_length);
server_addr.sin_port = htons(SERVER_PORT);
vms_status = ctrl_y_init();
if (!(vms_status & 1)) {
fprintf(stderr, "Cannot install Ctrl-Y handler (status %08X)\n", vms_status);
return EXIT_FAILURE;
}
initscr();
screen_active = 1;
clear();
atexit(endwin_noreturn);
output_window = newwin(LINES - 2, COLS, 0, 0);
entry_window = newwin(2, COLS, LINES - 2, 0);
scrollok(output_window, TRUE);
wrapok(output_window, TRUE);
char banner[BANNER_LENGTH];
char banner_contents[512];
extern char *BANNER_DEFINITION;
extern char *BANNER_TOP;
sprintf(banner_contents, BANNER_DEFINITION, "NICK", "*PLACEHOLDER*", "CHANNEL");
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(output_window);
wrefresh(entry_window);
if (gtfotime) goto cleanup;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
result = EXIT_FAILURE;
goto cleanup;
}
status = connect_to_server(client_socket, &server_addr);
if (gtfotime || status == 0) goto cleanup;
if (status < 0) {
perror("connect");
result = EXIT_FAILURE;
goto cleanup;
}
char nickname[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter your nickname: ");
if (read_input_line(tty_chan, entry_window, nickname,
sizeof(nickname), >fotime) == ERR || gtfotime) goto cleanup;
snprintf(buffer, BUFFER_SIZE, "NICK %s\r\n", nickname);
send_irc(client_socket, buffer, strlen(buffer));
sprintf(banner_contents, BANNER_DEFINITION, nickname, "*PLACEHOLDER*", "CHANNEL");
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(entry_window);
char username[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter your username: ");
wrefresh(entry_window);
if (read_input_line(tty_chan, entry_window, username,
sizeof(username), >fotime) == ERR || gtfotime) goto cleanup;
snprintf(buffer, BUFFER_SIZE, "USER %s 0 * :%s\r\n", username, username);
send_irc(client_socket, buffer, strlen(buffer));
status = process_initial_messages(client_socket, output_window);
if (gtfotime) goto cleanup;
if (status != 1) {
if (status < 0) perror("recv");
result = EXIT_FAILURE;
goto cleanup;
}
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
char channel[BUFFER_SIZE];
mvwaddstr(entry_window, 1, 0, "Enter channel to join: ");
wrefresh(entry_window);
if (read_input_line(tty_chan, entry_window, channel,
sizeof(channel), >fotime) == ERR || gtfotime) goto cleanup;
snprintf(buffer, BUFFER_SIZE, "JOIN %s\r\n", channel);
send_irc(client_socket, buffer, strlen(buffer));
memset(banner_contents, 0, strlen(banner_contents));
memset(banner, 0, strlen(banner));
sprintf(banner_contents, BANNER_DEFINITION, nickname, "*PLACEHOLDER*", channel);
sprintf(banner, BANNER_TOP, "placeholder", banner_contents);
wclear(entry_window);
mvwaddstr(entry_window, 0, 0, banner);
wrefresh(entry_window);
thread_data_t thread_data = {client_socket, entry_window, channel, output_window, nickname};
if (gtfotime) goto cleanup;
status = pthread_create(&input_thread, NULL, user_input_thread, &thread_data);
if (status != 0) {
fprintf(stderr, "Cannot start input thread (%d)\n", status);
result = EXIT_FAILURE;
goto cleanup;
}
input_thread_started = 1;
while (!gtfotime) {
status = wait_for_socket(client_socket, 0);
if (status == 0) break;
if (status < 0) {
perror("select");
result = EXIT_FAILURE;
break;
}
int n = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (gtfotime) break;
if (n < 0) {
if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) continue;
perror("recv");
result = EXIT_FAILURE;
break;
} else if (n == 0) {
break;
}
buffer[n] = '\0';
char *message_start = buffer;
char *message_end;
while ((message_end = strstr(message_start, "\r\n")) != NULL) {
*message_end = '\0';
process_incoming_message(client_socket, message_start, output_window);
message_start = message_end + 2;
}
fflush(stdout);
}
cleanup:
request_quit();
if (input_thread_started) {
pthread_join(input_thread, NULL);
}
if (client_socket >= 0) close(client_socket);
endwin_noreturn();
ctrl_y_cleanup();
return send_failed ? EXIT_FAILURE : result;
}