aboutsummaryrefslogtreecommitdiff
path: root/client/client.c
blob: 6609f89df17b1c3c44c041d3d2b6b7a7ef2c6b2b (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
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <termio.h>
#include "global.h"


static const Style normal_style={9,9,false,false};
static const Style bold_style={9,9,true,false};
static const Style command_style={3,9,false,false};
static const Style server_style={6,9,false,false};
static const Style error_style={1,9,true,false};


static i64 uniqid(void){
	static i64 id=1;
	return id++;
}

static ssize_t send_all(int socket,const void *buffer,i64 length){
	ssize_t cursor=0;
	while(cursor<length){
		ssize_t nwr=send(socket,buffer+cursor,length-cursor,0);
		if(nwr<=0)return nwr;
		cursor+=nwr;
	}
	return cursor;
}


static int connect_server(const char *hostname,int port){
	struct addrinfo hints,*res;

	memset(&hints,0,sizeof(hints));
	hints.ai_family=AF_UNSPEC;
	hints.ai_socktype=SOCK_STREAM;

	char portbuf[16];
	snprintf(portbuf,16,"%d",port);
	int ret=getaddrinfo(hostname,portbuf,&hints,&res);
	if(ret!=0){
		fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(ret));
		exit(1);
	}

	int sock;
	struct addrinfo *current=res;
	while(current){
		sock=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
		if(sock!=-1){
			if(connect(sock,res->ai_addr,res->ai_addrlen)==0){
				break;
			}
		}
		close(sock);
		sock=-1;
		current=current->ai_next;
	}
	freeaddrinfo(res);

	return sock;
}

static bool termio_needs_reset=false;

static void termio_reset(void){
	if(!termio_needs_reset)return;
	endkeyboard();
	endscreen();
	termio_needs_reset=false;
}

static void termio_init(void){
	initscreen();
	initkeyboard(false);
	installCLhandler(true);
	atexit(termio_reset);
	termio_needs_reset=true;
}


static i64 bar_width=11;

static char *user_buffer=NULL;
static i64 user_bufsz=0;
static i64 user_buflen=0;

static int server_sock=-1;


enum log_type{
	LOG_MESSAGE,
	LOG_SERVER,
	LOG_COMMAND,
	LOG_ERROR,
};

static void emit_log_line(enum log_type type,const char *head,const char *line){
	i64 headlen=strlen(head);
	if(headlen+1>bar_width){
		bar_width=headlen+1;
	}
	Size termsize=gettermsize();
	assert(termsize.w>bar_width);

	i64 linelen=strlen(line);
	i64 nlines=1,cursor=termsize.w-bar_width;
	while(cursor<linelen){
		cursor+=termsize.w-bar_width;
		nlines++;
	}

	scrollterm(0,1,termsize.w,termsize.h-2,nlines);

	char buffer[termsize.w+1];

	switch(type){
		case LOG_MESSAGE: setstyle(&normal_style); break;
		case LOG_SERVER: setstyle(&server_style); break;
		case LOG_COMMAND: setstyle(&command_style); break;
		case LOG_ERROR: setstyle(&error_style); break;
	}

	pushcursor();
	moveto(bar_width-1-headlen,termsize.h-1-nlines);
	tprintf("%s",head);
	cursor=0;
	for(int y=0;y<nlines;y++){
		memcpy(buffer,line+cursor,termsize.w-bar_width);
		buffer[termsize.w-bar_width]='\0';

		moveto(bar_width,termsize.h-1-nlines);
		tprintf("%s",buffer);

		cursor+=termsize.w-bar_width;
	}
	popcursor();
	redraw();
}

__attribute__((format (printf, 3, 4)))
static void emit_log_line_f(enum log_type type,const char *head,const char *format,...){
	va_list ap;
	va_start(ap,format);
	char *buf=NULL;
	vasprintf(&buf,format,ap);
	va_end(ap);
	assert(buf);
	emit_log_line(type,head,buf);
	free(buf);
}

static void redraw_prompt(void){
	Size termsize=gettermsize();
	moveto(0,termsize.h-1);
	setstyle(&bold_style);
	tprintf("> ");

	setstyle(&normal_style);
	fillrect(2,termsize.h-1,termsize.w-2,1,' ');
	tprintf("%s",user_buffer);
	redraw();
}


static char *server_user=NULL;
static char *server_room=NULL;

static void send_message(const char *msg){
	if(server_user==NULL){
		emit_log_line(LOG_ERROR,"--","Cannot send messages while not logged in");
		return;
	}
	if(server_room==NULL){
		emit_log_line(LOG_ERROR,"--","Cannot send messages while not in a room");
		return;
	}
}

static void cmd_register(const char **args){
	emit_log_line_f(LOG_COMMAND,"##","/register %s %s",args[0],args[1]);
}

static void cmd_login(const char **args){
	emit_log_line_f(LOG_COMMAND,"##","Login called with %s and %s",args[0],args[1]);
}

static void cmd_quit(const char **args){
	close(server_sock);
	exit(0);
}

struct cmd_list_item{
	const char *name;
	int nargs;
	bool longlast;
	void (*func)(const char **args);
};

static struct cmd_list_item cmd_list[]={
	{"register",2,true,cmd_register},
	{"login",2,true,cmd_login},
	{"quit",0,false,cmd_quit},
};
#define NCOMMANDS ((i64)(sizeof(cmd_list)/sizeof(cmd_list[0])))

static void execute_command(char *line){
	if(line[0]=='/'){
		send_message(line);
		return;
	}

	i64 linelen=strlen(line);
	char *sepp=memchr(line,' ',linelen);
	if(sepp==NULL)sepp=line+linelen;
	*sepp='\0';
	i64 cmdlen=sepp-line;
	i64 cmdi;
	for(cmdi=0;cmdi<NCOMMANDS;cmdi++){
		if(strncmp(line,cmd_list[cmdi].name,cmdlen)==0){
			break;
		}
	}

	if(cmdi==NCOMMANDS){
		emit_log_line_f(LOG_ERROR,"--","Unknown command '%s'",line);
		return;
	}

	int nargs=cmd_list[cmdi].nargs;
	char *args[nargs];
	i64 cursor=cmdlen+1;
	while(line[cursor]==' ')cursor++;

	for(int i=0;i<nargs;i++){
		if(cursor>linelen){
			emit_log_line_f(LOG_ERROR,"--","Too few parameters to command '%s'",line);
			return;
		}
		if(i==nargs-1&&cmd_list[cmdi].longlast){
			sepp=line+linelen;
		} else {
			sepp=memchr(line+cursor,' ',linelen-cursor);
			if(sepp==NULL)sepp=line+linelen;
		}
		*sepp='\0';
		args[i]=line+cursor;
		cursor=sepp-line+1;
		while(line[cursor]==' ')cursor++;
	}
	if(sepp-line<linelen){
		emit_log_line_f(LOG_ERROR,"--","Too many parameters to command '%s'",line);
		return;
	}

	cmd_list[cmdi].func((const char**)args);
}


// Returns whether stdin was closed
static bool handle_stdin(void){
	int key=tgetkey();
	switch(key){
		case KEY_LF:
		case KEY_CR: {
			if(user_buffer[0]=='/'){
				execute_command(user_buffer+1);
			} else {
				send_message(user_buffer);
			}
			user_buflen=0;
			user_buffer[0]='\0';
			redraw_prompt();
			break;
		}

		case KEY_BACKSPACE:
			if(user_buflen>0){
				user_buflen--;
				user_buffer[user_buflen]='\0';
				redraw_prompt();
			} else {
				bel();
			}
			break;

		case KEY_CTRL+'U':
			user_buflen=0;
			user_buffer[0]='\0';
			redraw_prompt();
			break;

		default:
			if(key>=32&&key<=126){
				if(user_buflen==user_bufsz-1){
					user_bufsz*=2;
					user_buffer=realloc(user_buffer,user_bufsz,char);
				}
				user_buffer[user_buflen++]=key;
				user_buffer[user_buflen]='\0';
				redraw_prompt();
			} else {
				bel();
			}
			break;
	}
	return false;
}

// Returns whether socket was closed
static bool handle_server(void){
	static i64 bufsz=256,buflen=0;
	static char *buffer=NULL;
	if(buffer==NULL){
		buffer=malloc(bufsz,char);
	}

	if(bufsz-buflen<128){
		bufsz*=2;
		buffer=realloc(buffer,bufsz,char);
	}

	ssize_t nr;
	while(true){
		nr=read(server_sock,buffer+buflen,bufsz-buflen);
		if(nr<0){
			if(errno==EINTR)continue;
			if(errno==ECONNRESET){
				buflen=0;
				return true;
			}
			perror("read");
			exit(1);
		}
		if(nr==0){
			buflen=0;
			return true;
		}
		buflen+=nr;
		break;
	}

	char *lfp=memchr(buffer,'\n',buflen);
	if(lfp==NULL)return false;
	*lfp='\0';
	emit_log_line(LOG_SERVER,">>",buffer);
	i64 linelen=lfp-buffer;
	memmove(buffer,buffer+linelen+1,buflen-linelen-1);
	buflen-=linelen+1;
	return false;
}


int main(int argc,char **argv){
	const char *hostname="tomsmeding.com";
	int port=29536;

	if(argc>=2){
		hostname=argv[1];
		if(argc>=3){
			port=strtol(argv[2],NULL,10);
			if(argc>=4){
				fprintf(stderr,"Usage: %s [host [port]]\n",argv[0]);
				return 1;
			}
		}
	}

	server_sock=connect_server(hostname,port);
	if(server_sock==-1){
		fprintf(stderr,"Could not connect to %s port %d\n",hostname,port);
		return 1;
	}

	termio_init();

	clearscreen();
	moveto(0,0);
	setstyle(&bold_style);
	tprintf("TOMSG CLIENT");

	user_bufsz=256;
	user_buflen=0;
	user_buffer=malloc(user_bufsz,char);

	redraw_prompt();

	while(true){
		fd_set inset;
		FD_ZERO(&inset);
		FD_SET(STDIN_FILENO,&inset);
		FD_SET(server_sock,&inset);
		int ret=select(server_sock+1,&inset,NULL,NULL,NULL);
		if(ret==-1){
			if(errno==EINTR)continue;
			perror("select");
			return 1;
		}
		if(FD_ISSET(STDIN_FILENO,&inset)){
			if(handle_stdin())break;
		}
		if(FD_ISSET(server_sock,&inset)){
			if(handle_server())break;
		}
	}

	close(server_sock);
	termio_reset();
}