aboutsummaryrefslogtreecommitdiff
path: root/command.c
blob: a691171ace3511402c999d7d017b1380c730e3a1 (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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/socket.h>
#include "command.h"
#include "db.h"
#include "user_data.h"


static i64 make_timestamp(void){
	struct timeval tv;
	gettimeofday(&tv,NULL);
	return (i64)tv.tv_sec*1000000+tv.tv_usec;
}


static bool send_raw_text(int fd,const char *text,i64 len){
	i64 cursor=0;
	while(cursor<len){
		i64 nwr=send(fd,text+cursor,len-cursor,0);
		if(nwr<0){
			if(errno==EINTR)continue;
			if(errno==ECONNRESET||errno==EPIPE)return true;
			die_perror("send");
		}
		cursor+=nwr;
	}
	return false;
}

static bool send_ok(int fd,const char *tag){
	char *buf=NULL;
	i64 len=asprintf(&buf,"%s ok\n",tag);
	assert(buf);
	bool closed=send_raw_text(fd,buf,len);
	free(buf);
	return closed;
}

static bool send_error(int fd,const char *tag,const char *msg){
	char *buf=NULL;
	i64 len=asprintf(&buf,"%s error %s\n",tag,msg);
	assert(buf);
	bool closed=send_raw_text(fd,buf,len);
	free(buf);
	return closed;
}

static bool send_name(int fd,const char *tag,const char *name){
	char *buf=NULL;
	i64 len=asprintf(&buf,"%s name %s\n",tag,name);
	assert(buf);
	bool closed=send_raw_text(fd,buf,len);
	free(buf);
	return closed;
}

static bool send_list(int fd,const char *tag,i64 count,const char **list){
	char *buf=NULL;
	i64 len=asprintf(&buf,"%s list %" PRIi64,tag,count);
	assert(buf);
	bool closed=send_raw_text(fd,buf,len);
	free(buf);
	if(closed)return true;

	if(count>0){
		i64 bufsz=64;
		buf=malloc(bufsz,char);

		for(i64 i=0;i<count;i++){
			i64 len=strlen(list[i]);
			if(len>=bufsz){
				bufsz=len+512;
				buf=realloc(buf,bufsz,char);
			}
			memcpy(buf+1,list[i],len);
			buf[0]=' ';
			if(send_raw_text(fd,buf,len+1)){
				free(buf);
				return true;
			}
		}

		free(buf);
	}

	return send_raw_text(fd,"\n",1);
}


static bool cmd_register(struct conn_data *data,const char *tag,const char **args){
	i64 userid=db_find_user(args[0]);
	if(userid!=-1){
		send_error(data->fd,tag,"Username already exists");
		return false;
	}
	db_create_user(args[0],args[1]);
	return send_ok(data->fd,tag);
}

static bool cmd_login(struct conn_data *data,const char *tag,const char **args){
	i64 userid=db_find_user(args[0]);
	if(userid==-1){
		send_error(data->fd,tag,"User not found");
		return false;
	}
	char *pass=db_get_pass(userid);
	bool success=strcmp(args[1],pass)==0;
	free(pass);
	if(data->userid!=-1){
		userdata_unregister(data->userid,data->fd);
	}
	if(success){
		data->userid=userid;
		userdata_register(userid,data->fd);
		send_ok(data->fd,tag);
	} else {
		data->userid=-1;
		send_error(data->fd,tag,"Incorrect password");
	}
	return false;
}

static bool cmd_list_rooms(struct conn_data *data,const char *tag,const char **args){
	(void)args;
	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	struct db_room_list rl=db_list_rooms(data->userid);
	if(rl.count<=0){
		db_nullify_room_list(rl);
		return send_list(data->fd,tag,0,NULL);
	}
	const char *names[rl.count];
	for(i64 i=0;i<rl.count;i++){
		names[i]=rl.list[i].name;
	}
	bool closed=send_list(data->fd,tag,rl.count,names);
	db_nullify_room_list(rl);
	return closed;
}

static bool cmd_list_members(struct conn_data *data,const char *tag,const char **args){
	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		send_error(data->fd,tag,"Room not found");
		return false;
	}
	if(!db_is_member(roomid,data->userid)){
		send_error(data->fd,tag,"Not in that room");
		return false;
	}

	struct db_user_list ul=db_list_members(roomid);
	if(ul.count<=0){
		db_nullify_user_list(ul);
		return send_list(data->fd,tag,0,NULL);
	}
	const char *names[ul.count];
	for(i64 i=0;i<ul.count;i++){
		names[i]=ul.list[i].name;
	}
	bool closed=send_list(data->fd,tag,ul.count,names);
	db_nullify_user_list(ul);
	return closed;
}

static bool cmd_create_room(struct conn_data *data,const char *tag,const char **args){
	(void)args;
	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	struct db_name_id room=db_create_room();
	db_add_member(room.id,data->userid);
	bool closed=send_name(data->fd,tag,room.name);
	db_nullify_name_id(room);
	return closed;
}

static bool cmd_invite(struct conn_data *data,const char *tag,const char **args){
	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		send_error(data->fd,tag,"Room not found");
		return false;
	}

	i64 user2=db_find_user(args[1]);
	if(user2==-1){
		send_error(data->fd,tag,"User not found");
		return false;
	}

	if(!db_is_member(roomid,data->userid)){
		send_error(data->fd,tag,"Not in that room");
		return false;
	}

	db_add_member(roomid,user2);
	return send_ok(data->fd,tag);
}

static bool cmd_send(struct conn_data *data,const char *tag,const char **args){
	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		send_error(data->fd,tag,"Room not found");
		return false;
	}
	if(!db_is_member(roomid,data->userid)){
		send_error(data->fd,tag,"Not in that room");
		return false;
	}

	db_create_message(roomid,data->userid,make_timestamp(),args[1]);
	bool closed=send_ok(data->fd,tag);

	char *pushbuf=NULL;
	char *username=db_get_username(data->userid);
	i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %s\n",args[0],username,args[1]);
	assert(pushbuf);
	free(username);

	struct db_user_list members=db_list_members(roomid);
	for(i64 i=0;i<members.count;i++){
		i64 nfds;
		const int *fds=userdata_online(members.list[i].id,&nfds);
		if(nfds<=0)continue;
		for(i64 j=0;j<nfds;j++){
			if(fds[j]!=data->fd){
				send_raw_text(fds[j],pushbuf,pushbuflen);
			}
		}
	}

	free(pushbuf);

	return closed;
}

static bool cmd_history(struct conn_data *data,const char *tag,const char **args){
	char *endp;
	i64 nrequested=strtoll(args[1],&endp,10);
	if(args[1][0]=='\0'||*endp!='\0'||nrequested<0){
		debug("Connection fd=%d sent an invalid number for 'history': '%s'",data->fd,args[1]);
		return true;
	}

	if(data->userid==-1){
		send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		send_error(data->fd,tag,"Room not found");
		return false;
	}
	if(!db_is_member(roomid,data->userid)){
		send_error(data->fd,tag,"Not in that room");
		return false;
	}

	struct db_message_list ml=db_get_messages(roomid,nrequested);
	char *buf=NULL;
	i64 len=asprintf(&buf,"%s history %" PRIi64 "\n",tag,ml.count);
	assert(buf);
	bool closed=send_raw_text(data->fd,buf,len);
	free(buf);

	if(closed){
		db_nullify_message_list(ml);
		return true;
	}

	for(i64 i=ml.count-1;i>=0;i--){
		char *username=db_get_username(ml.list[i].userid);
		len=asprintf(&buf,"%s history_item %" PRIi64 " %s %" PRIi64 " %s\n",
				tag,ml.count-1-i,username,ml.list[i].timestamp,ml.list[i].message);
		assert(buf);
		closed=send_raw_text(data->fd,buf,len);
		free(buf);
		if(closed)break;
	}

	db_nullify_message_list(ml);
	return closed;
}


struct cmd_info{
	const char *cmdname;
	int nargs;
	bool longlast;  // whether the last argument should span the rest of the input line
	bool (*handler)(struct conn_data *data,const char *tag,const char **args);
};

static const struct cmd_info commands[]={
	{"register",2,true,cmd_register},
	{"login",2,true,cmd_login},
	{"list_rooms",0,false,cmd_list_rooms},
	{"list_members",1,false,cmd_list_members},
	{"create_room",0,false,cmd_create_room},
	{"invite",2,false,cmd_invite},
	{"send",2,true,cmd_send},
	{"history",2,false,cmd_history},
};
#define NCOMMANDS (sizeof(commands)/sizeof(commands[0]))


bool handle_input_line(struct conn_data *data,char *line,size_t linelen){
	line[linelen]='\0';
	char *sepp=memchr(line,' ',linelen);
	if(sepp==NULL){
		debug("No space in input line from connection %d",data->fd);
		return true;
	}
	char *tag=line;
	size_t taglen=sepp-tag;
	*sepp='\0';
	line+=taglen+1;
	linelen-=taglen+1;

	sepp=memchr(line,' ',linelen);
	if(sepp==NULL)sepp=line+linelen;
	size_t cmdlen=sepp-line;
	size_t cmdi;
	for(cmdi=0;cmdi<NCOMMANDS;cmdi++){
		if(strncmp(line,commands[cmdi].cmdname,cmdlen)==0){
			break;
		}
	}

	if(cmdi==NCOMMANDS){
		debug("Unknown command %s on connection %d",line,data->fd);
		return true;
	}

	int nargs=commands[cmdi].nargs;
	char *args[nargs];
	size_t cursor=cmdlen+1;

	for(int i=0;i<nargs;i++){
		if(cursor>linelen){
			debug("Connection %d sent too few parameters to command %s",data->fd,commands[cmdi].cmdname);
			return true;
		}
		if(i==nargs-1&&commands[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;
	}
	if(sepp-line<(i64)linelen){
		debug("Connection %d sent too many parameters to command %s",data->fd,commands[cmdi].cmdname);
		return true;
	}

	return commands[cmdi].handler(data,tag,(const char**)args);
}