aboutsummaryrefslogtreecommitdiff
path: root/command.c
blob: 1b70369ef05eade934bce882e05bfcf03f9597fc (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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include "broadcast.h"
#include "command.h"
#include "db.h"
#include "event.h"
#include "net.h"
#include "firebase.h"
#include "user_data.h"
#include "util.h"


static bool cmd_register(struct conn_data *data,const char *tag,const char **args){
	i64 userid=db_find_user(args[0]);
	if(userid!=-1){
		net_send_error(data->fd,tag,"Username already exists");
		return false;
	}
	db_create_user(args[0],args[1]);
	return net_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){
		net_send_error(data->fd,tag,"User not found");
		if(data->userid!=-1){
			userdata_unregister(data->userid,data->fd);
			broadcast_online_change(data->userid);
			data->userid=-1;
		}
		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);
		broadcast_online_change(data->userid);
	}
	if(success){
		data->userid=userid;
		userdata_register(userid,data->fd);
		net_send_ok(data->fd,tag);
		broadcast_online_change(userid);
	} else {
		data->userid=-1;
		net_send_error(data->fd,tag,"Incorrect password");
	}
	return false;
}

static bool cmd_logout(struct conn_data *data,const char *tag,const char **args){
	(void)args;
	if(data->userid!=-1){
		userdata_unregister(data->userid,data->fd);
		broadcast_online_change(data->userid);
		data->userid=-1;
	}
	net_send_ok(data->fd,tag);
	return false;
}

static bool cmd_list_rooms(struct conn_data *data,const char *tag,const char **args){
	(void)args;
	if(data->userid==-1){
		net_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 net_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=net_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){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		net_send_error(data->fd,tag,"Room not found");
		return false;
	}
	if(!db_is_member(roomid,data->userid)){
		net_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 net_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=net_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){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	userdata_mark_active(data->userid,data->fd);
	struct db_name_id room=db_create_room();
	db_add_member(room.id,data->userid);
	bool closed=net_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){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	userdata_mark_active(data->userid,data->fd);
	const char *roomname=args[0];
	i64 roomid=db_find_room(roomname);
	if(roomid==-1){
		net_send_error(data->fd,tag,"Room not found");
		return false;
	}

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

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

	db_add_member(roomid,user2);

	const char *username=args[1];
	char *joinbuf=NULL;
	i64 joinbuflen=asprintf(&joinbuf,"_push join %s %s\n",roomname,username);
	char *invitebuf=NULL;
	i64 invitebuflen=asprintf(&invitebuf,"_push invite %s\n",roomname);

	event_emit_join(make_timestamp(),username,roomname);

	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){
				if(members.list[i].id==user2){
					net_send_raw_text(fds[j],invitebuf,invitebuflen);
				} else {
					net_send_raw_text(fds[j],joinbuf,joinbuflen);
				}
			}
		}
	}

	db_nullify_user_list(members);
	free(joinbuf);
	free(invitebuf);

	return net_send_ok(data->fd,tag);
}

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

	i64 timestamp=make_timestamp();
	db_create_message(roomid,data->userid,make_timestamp(),message);
	bool closed=net_send_ok(data->fd,tag);

	char *pushbuf=NULL;
	char *username=db_get_username(data->userid);
	i64 pushbuflen=asprintf(&pushbuf,"_push message %s %s %" PRIi64 " %s\n",
			roomname,username,timestamp,message);

	event_emit_message(timestamp,message,username,roomname);
	firebase_send_message(roomname,roomid,username,message);
	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){
				net_send_raw_text(fds[j],pushbuf,pushbuflen);
			}
		}
	}

	db_nullify_user_list(members);
	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){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	i64 roomid=db_find_room(args[0]);
	if(roomid==-1){
		net_send_error(data->fd,tag,"Room not found");
		return false;
	}
	if(!db_is_member(roomid,data->userid)){
		net_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);
	bool closed=net_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_message %" PRIi64 " %s %s %" PRIi64 " %s\n",
				tag,ml.count-1-i,args[0],username,ml.list[i].timestamp,ml.list[i].message);
		closed=net_send_raw_text(data->fd,buf,len);
		free(buf);
		if(closed)break;
	}

	db_nullify_message_list(ml);
	return closed;
}

static bool cmd_ping(struct conn_data *data,const char *tag,const char **args){
	(void)args;
	return net_send_pong(data->fd,tag);
}

static bool cmd_is_online(struct conn_data *data,const char *tag,const char **args){
	i64 userid=db_find_user(args[0]);
	if(userid==-1){
		net_send_error(data->fd,tag,"User not found");
		return false;
	}
	i64 nfds;
	(void)userdata_online(userid,&nfds);
	return net_send_number(data->fd,tag,nfds);
}

static bool cmd_firebase_token(struct conn_data *data,const char *tag,const char **args){
	if(data->userid==-1){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	db_add_token(data->userid,args[0]);
	return net_send_ok(data->fd,tag);
}

static bool cmd_delete_firebase_token(struct conn_data *data,const char *tag,const char **args){
	if(data->userid==-1){
		net_send_error(data->fd,tag,"Not logged in");
		return false;
	}
	db_delete_token(data->userid,args[0]);
	return net_send_ok(data->fd,tag);
}


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},
	{"logout",0,false,cmd_logout},
	{"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},
	{"ping",0,false,cmd_ping},
	{"is_online",1,false,cmd_is_online},
	{"firebase_token",1,false,cmd_firebase_token},
	{"delete_firebase_token",1,false,cmd_delete_firebase_token},
};
#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(cmdlen==strlen(commands[cmdi].cmdname)&&
				memcmp(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);
}