-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitalk.c
394 lines (320 loc) · 9.39 KB
/
minitalk.c
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
/*
Copyright (c) 2019 Andrew Benson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* minichat - small chat system for multiple users on a UNIX-like host */
#define VERSION "0.3.1"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <readline/readline.h>
#include <readline/history.h>
/* The maximum length of a nick string (will be truncated) */
#define MAX_NICK_SIZE 15
/* Time format will be fixed format [YYYY-MM-DD HH:MM:SS] */
#define TIME_FORMAT "%Y-%m-%d %H:%M:%S"
#define TIME_STRSIZE 20
/* Maximum supported message size, including datetime/username */
#define MSG_LEN 1025
#define PROMPT "> "
/* GLOBALS */
/* File used for chat */
static FILE *ctrl = NULL;
static char nick[MAX_NICK_SIZE+1] = {0};
/* Keep track of the last position we read from. */
static long last_read_pos = 0;
/* BYO...Boolean. */
typedef enum { NO, YES} BOOL;
/* The program will run as long as this is set to YES. */
BOOL cont = YES;
/* Build the text string for the time. */
static void get_time(char *dt)
{
struct tm tm = {0};
time_t t = 0;
t = time(NULL);
gmtime_r(&t, &tm); /* UTC */
strftime(dt, TIME_STRSIZE, TIME_FORMAT, &tm);
}
/* This is a "message", i.e. a chat from one user to another. */
static void write_msg(const char *msg)
{
char dt[TIME_STRSIZE] = {0};
get_time(dt);
fprintf(ctrl, "[%s] <%s> %s\n", dt, nick, msg);
fflush(ctrl);
}
/* This is a "status", i.e. user joined */
static void write_status(const char *status)
{
char dt[TIME_STRSIZE] = {0};
get_time(dt);
fprintf(ctrl, "[%s] *** %s %s\n", dt, nick, status);
fflush(ctrl);
}
/* Handle readline stuff and print a message to the screen. */
static void print_line(char *line)
{
char *saved_line = NULL;
int saved_point = 0;
/* This is readline stuff.
- save the cursor position
- save the current line contents
- set the line to blank
- tell readline we're done mucking
- print the message
- restore the standard prompt
- restore the line contents
- restore the cursor position
- tell readline we're done mucking (again)
*/
saved_point = rl_point;
saved_line = rl_copy_text(0, rl_end);
rl_set_prompt("");
rl_clear_message();
rl_replace_line("", 0);
rl_redisplay();
fprintf(stdout, "%s", line);
rl_set_prompt(PROMPT);
rl_replace_line(saved_line, 0);
rl_point = saved_point;
rl_redisplay();
rl_free(saved_line);
}
/* Read all of the messages from the transcript since the last read */
static void check_msgs(void)
{
char msg[MSG_LEN] = {0};
int count = 0;
fd_set fds;
int fd;
struct timeval t = {0};
/* Timeout of 0. */
t.tv_sec = 0;
t.tv_usec = 0;
/* Does ctrl have data available for reading? */
FD_ZERO(&fds);
fd = fileno(ctrl);
FD_SET(fd, &fds);
count = select(FD_SETSIZE, &fds, NULL, NULL, &t);
if(count > 0) {
/* Go back to the last read point. */
fseek(ctrl, last_read_pos, SEEK_SET);
while(fgets(msg, MSG_LEN, ctrl) != NULL) {
char *str;
if(str = strstr(msg, ">")) {
if(strstr(str, nick)) {
rl_ding();
}
}
print_line(msg);
}
/* Update the last read position */
last_read_pos = ftell(ctrl);
/* Set the file position at the end of the file */
fseek(ctrl, 0L, SEEK_END);
}
}
static BOOL is_nick_allowed(char *nick)
{
/* Verify there are only alpha-numeric characters in the nick,
it is at least one character, and is no more than the maximum
buffer size */
if(nick == NULL || strlen(nick) < 1 || strlen(nick) > MAX_NICK_SIZE) {
return NO;
}
while(*nick) {
if(!isalnum(*nick)) {
return NO;
}
nick++;
}
return YES;
}
static void handle_msg(char *msg)
{
char lmsg[MSG_LEN] = {0};
char nnick[MAX_NICK_SIZE+1] = {0};
/* Only care if we're called on a real message. */
if(msg) {
/* readline keeps history, let's use */
add_history(msg);
/* Quit if told to quit. Only write the message if there's something to say. */
if(strncmp(msg, "/quit", 5) == 0) {
cont = NO;
} else if(strncmp(msg, "/nick", 5) == 0) {
if(is_nick_allowed(msg+6)) {
strncpy(nnick, msg+6, MAX_NICK_SIZE);
snprintf(lmsg, MSG_LEN, "is now known as %s", nnick);
write_status(lmsg);
strncpy(nick, nnick, MAX_NICK_SIZE);
} else {
print_line("Please specify a valid nick. Nicks must 1-15 characters and only\n"
"include numbers and letters.\n");
}
} else if(strncmp(msg, "/", 1) == 0) {
snprintf(lmsg, MSG_LEN, "Unknown command: %s\n", msg+1);
print_line(lmsg);
} else if(strlen(msg) > 0) {
write_msg(msg);
}
}
}
static void handle_line_fake(char *line)
{
/* We want to ignore when readline says it has the end of a message, as we'll
only care if the user presses ENTER.
*/
if (line != NULL) {
rl_set_prompt(PROMPT);
rl_already_prompted = 1;
} else {
rl_set_prompt("");
rl_replace_line("", 0);
rl_redisplay();
rl_set_prompt(PROMPT);
}
}
static int handle_enter(int x, int y)
{
char *line = NULL;
/* Handle when a user presses enter.
- Save the contents of the line.
- Set the prompt to nothing.
- Blank the line.
- pass the message to the message handler
- rl_copy_text returns malloc'd mem, so free it
- restore the prompt
- tell readline we're done mucking
*/
line = rl_copy_text(0, rl_end);
if(strlen(line) > 0) {
rl_set_prompt("");
rl_replace_line("", 1);
rl_redisplay();
handle_msg(line);
free(line);
rl_set_prompt(PROMPT);
rl_redisplay();
rl_done = 1;
}
return 0;
}
/* Don't block on I/O. */
static void get_input(void) {
int count = 0;
fd_set fds;
struct timeval t = {0};
/* Timeout of 0. */
t.tv_sec = 0;
t.tv_usec = 0;
/* Does stdin have data available for reading? */
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
count = select(FD_SETSIZE, &fds, NULL, NULL, &t);
/* If data is avaiable, read it. Else carry on */
if (count > 0) {
rl_callback_read_char();
}
}
static void handle_exit_signal(int signal)
{
fprintf(stderr, "Received signal %d, exiting.\n", signal);
cont = NO;
}
int main(int argc, char *argv[])
{
struct sigaction sa_exit = {0};
/* We always require a file, will guess a nick if none provided.*/
if(argc < 2) {
fprintf(stderr, "%s v%s - a small chat system for multiple users\n", argv[0], VERSION);
fprintf(stderr, "usage: %s file [nick]\n", argv[0]);
return 1;
}
if(argv[2] == NULL) {
/* If no nick is provided, pick based on the username */
if(getenv("LOGNAME") == NULL) {
fprintf(stderr, "Can't determine username. Fix your environment or specify\n"
"a name on the command line.\n");
return 1;
}
if(!is_nick_allowed(getenv("LOGNAME"))) {
fprintf(stderr, "Nicks must be 1-15 characters and only include numbers\n"
"and letters. Your username does not meet these requirements; please\n"
"specify a nick on the command line.\n");
return 1;
}
strncpy(nick, getenv("LOGNAME"), MAX_NICK_SIZE);
} else {
/* Copy from command line. */
if(!is_nick_allowed(argv[2])) {
fprintf(stderr, "Nicks must be 1-15 characters and only include numbers\n"
"and letters. The name you have provided does not meet these\n"
"requirements.\n");
return 1;
}
strncpy(nick, argv[2], MAX_NICK_SIZE);
}
/* Setup signal handlers */
sa_exit.sa_handler = handle_exit_signal;
sigemptyset(&sa_exit.sa_mask);
sigaction(SIGINT, &sa_exit, NULL);
sigaction(SIGTERM, &sa_exit, NULL);
/* Open the file. Always write at the end. */
ctrl = fopen(argv[1], "a+");
if(ctrl == NULL) {
fprintf(stderr, "Unable to open %s!\n", argv[1]);
return 1;
}
/* We don't want to see past messages, as they could be loooooong.*/
fseek(ctrl, 0L, SEEK_END);
/* This is now our "last read" position. */
last_read_pos = ftell(ctrl);
/* Tell readline to let us know when the user hits enter. */
rl_bind_key(RETURN, handle_enter);
/* Setup the fake handler for when readline thinks we're done. */
rl_callback_handler_install(PROMPT, handle_line_fake);
/* Done with setup!
Now let everyone know the user has arrived. */
write_status("joined");
/* Until we decide to quit, tell readline to grab characters if they're available. */
while(cont) {
get_input();
usleep(10000);
check_msgs();
usleep(10000);
}
/* We're quitting now. Say goodbye. */
write_status("left");
/* Clean up for readline now */
rl_unbind_key(RETURN);
rl_callback_handler_remove();
/* Being a good citizen. Closing file handles. */
fclose(ctrl);
/* Clean up screen. */
rl_set_prompt("");
rl_clear_message();
rl_redisplay();
return 0;
}