aboutsummaryrefslogtreecommitdiff
path: root/weechat/net.c
blob: ce8da6a69e8460a8bbdded96acacfa7edb14fe70 (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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <assert.h>
#include "net.h"
#include "debug.h"


struct store_item{
	char *id;
	net_callback_t *cb;
	void *payload;
};

static i64 store_cap=16,store_len=0;
static struct store_item *store=NULL;

static net_callback_t *push_callback=NULL;
static net_callback_t *history_callback=NULL;


#define ALPHABET "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

static void uniqid8(char *dst){
	static i64 static_id=0;
	static const int alphabet_len=strlen(ALPHABET);

	int id=static_id;
	static_id++;
	for(int i=7;i>=0;i--){
		dst[i]=ALPHABET[id%alphabet_len];
		id/=alphabet_len;
	}
}

void net_set_push_callback(net_callback_t *callback){
	push_callback=callback;
}

void net_set_history_callback(net_callback_t *callback){
	history_callback=callback;
}

__attribute__((format (printf, 4, 5)))
bool net_sendf(int fd,net_callback_t *callback,void *payload,const char *format,...){
	if(store==NULL){
		store=malloc(store_cap*sizeof(struct store_item));
	} else if(store_len==store_cap){
		store_cap*=2;
		store=realloc(store,store_cap*sizeof(struct store_item));
	}
	assert(store);

	debugf("net_sendf(%d,%p,\"%s\",...)\n",fd,callback,format);

	va_list ap,ap2;
	va_start(ap,format);
	va_copy(ap2,ap);
	i64 len=vsnprintf(NULL,0,format,ap2);
	va_end(ap2);
	assert(len>=0);

	char *buf=malloc(9+len+2);
	assert(buf);
	uniqid8(buf);
	buf[8]=' ';
	i64 len2=vsprintf(buf+9,format,ap);
	va_end(ap);
	assert(len2==len);
	buf[9+len]='\n';
	buf[9+len+1]='\0';

	store[store_len].id=malloc(9);
	assert(store[store_len].id);
	memcpy(store[store_len].id,buf,8);
	store[store_len].id[8]='\0';
	store[store_len].cb=callback;
	store[store_len].payload=payload;
	store_len++;
	
	i64 cursor=0,total=9+len+1;
	while(cursor<total){
		i64 nwr=send(fd,buf+cursor,total-cursor,0);
		if(nwr<=0)return false;
		cursor+=nwr;
	}
	free(buf);
	return true;
}

void net_handle_recv(int fd,const char *msg){
	i64 msglen=strlen(msg);
	const char *p=strchr(msg,' ');
	if(p==NULL){
		debugf("net_handle_recv: no space in message <%s>\n",msg);
		return;
	}
	i64 taglen=p-msg;

	net_callback_t *cb=NULL;
	void *payload=NULL;
	if(taglen==5&&memcmp(msg,"_push",5)==0){
		cb=push_callback;
	} else if(*p!='\0'&&memcmp(p+1,"history_message",15)==0){
		cb=history_callback;
	} else {
		if(taglen!=8){
			debugf("net_handle_recv: tag not length 8 <%s>\n",msg);
			return;
		}
		for(i64 i=0;i<store_len;i++){
			if(memcmp(store[i].id,msg,taglen)==0){
				cb=store[i].cb;
				payload=store[i].payload;
				memmove(store+i,store+(i+1),(store_len-i-1)*sizeof(struct store_item));
				store_len--;
				break;
			}
		}
		if(cb==NULL){
			debugf("net_handle_recv: no viable callback found <%s>\n",msg);
			return;
		}
	}

	const char *cmd=p+1;
	p=strchr(cmd,' ');
	if(p==NULL)p=msg+msglen;
	i64 cmdlen=p-cmd;

	// Now `p` points to the space (or '\0') after the command name

	if(cmdlen==2&&memcmp(cmd,"ok",2)==0){
		cb(fd,(struct net_response){.type=NET_OK},payload);
	} else if(cmdlen==6&&memcmp(cmd,"number",6)==0){
		if(*p=='\0'){
			debugf("net_handle_recv: no number argument <%s>\n",msg);
			return;
		}
		const char *nump=p+1;
		struct net_response res=(struct net_response){
			.type=NET_NUMBER
		};
		const char *endp;
		res.number=strtol(nump,(char**)&endp,10);
		if(nump[0]=='\0'||*endp!='\0'){
			debugf("net_handle_recv: invalid number argument <%s>\n",msg);
			return;
		}
		cb(fd,res,payload);
	} else if(cmdlen==5&&memcmp(cmd,"error",5)==0){
		struct net_response res=(struct net_response){
			.type=NET_ERROR,
			.error=strdup(*p=='\0'?p:p+1)
		};
		cb(fd,res,payload);
		free(res.error);
	} else if(cmdlen==4&&memcmp(cmd,"name",4)==0){
		struct net_response res=(struct net_response){
			.type=NET_NAME,
			.name=strdup(*p=='\0'?p:p+1)
		};
		cb(fd,res,payload);
		free(res.name);
	} else if(cmdlen==4&&memcmp(cmd,"ping",4)==0){
		// Do nothing
	} else if(cmdlen==4&&memcmp(cmd,"pong",4)==0){
		struct net_response res=(struct net_response){
			.type=NET_PONG
		};
		cb(fd,res,payload);
	} else if(cmdlen==4&&memcmp(cmd,"list",4)==0){
		struct net_response res=(struct net_response){.type=NET_LIST};
		if(*p=='\0'){
			debugf("net_handle_recv: no list count <%s>\n",msg);
			return;
		}
		const char *cursor=p+1;
		p=strchr(cursor,' ');
		res.nitems=strtol(cursor,NULL,10);
		if(res.nitems<=0){
			debugf("net_handle_recv: -- 0 items <%s>\n",msg);
			res.nitems=0;
			res.items=NULL;
			cb(fd,res,payload);
			return;
		}
		res.items=malloc(res.nitems*sizeof(char*));
		assert(res.items);
		cursor=p;
		for(i64 i=0;i<res.nitems;i++){
			if(cursor==NULL||*cursor=='\0'){
				free(res.items);
				debugf("net_handle_recv: short list <%s>\n",msg);
				return;
			}
			cursor++;
			p=strchr(cursor,' ');
			if(p==NULL)p=msg+msglen;
			res.items[i]=malloc(p-cursor+1);
			assert(res.items[i]);
			memcpy(res.items[i],cursor,p-cursor);
			res.items[i][p-cursor]='\0';
			debugf("net_handle_recv: -- item \"%s\" <%s>\n",res.items[i],msg);
			cursor=p;
		}
		cb(fd,res,payload);
		for(i64 i=0;i<res.nitems;i++){
			free(res.items[i]);
		}
		free(res.items);
	} else if(cmdlen==7&&memcmp(cmd,"message",7)==0){
		if(*p=='\0'){
			debugf("net_handle_recv: no arguments to 'message' <%s>\n",msg);
			return;
		}
		const char *roomp=p+1;
		p=strchr(roomp,' ');
		const char *q,*r,*s,*t;
		if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL
				||(s=strchr(r+1,' '))==NULL||(t=strchr(s+1,' '))==NULL){
			debugf("net_handle_recv: not enough arguments to 'message' <%s>\n",msg);
			return;
		}
		i64 roomlen=p-roomp;
		const char *usernamep=p+1;
		i64 usernamelen=q-usernamep;
		const char *stampp=q+1;
		i64 stamplen=r-stampp;
		const char *msgidp=r+1;
		i64 msgidlen=s-msgidp;
		const char *replyidp=s+1;
		i64 replyidlen=t-replyidp;
		const char *textp=t+1;
		i64 textlen=msglen-(textp-msg);

		(void)msgidp; (void)msgidlen; (void)replyidlen;

		struct net_response res;
		res.type=NET_MESSAGE;
		const char *endp;
		res.timestamp=strtoll(stampp,(char**)&endp,10);
		if(endp-stampp!=stamplen){
			debugf("net_handle_recv: timestamp not a number in 'message' <%s>\n",msg);
			return;
		}
		res.msgid=strtoll(msgidp,(char**)&endp,10);
		if(endp-msgidp!=msgidlen){
			debugf("net_handle_recv: msgid not a number in 'message' <%s>\n",msg);
			return;
		}
		res.replyid=strtoll(replyidp,(char**)&endp,10);
		if(endp-replyidp!=replyidlen){
			debugf("net_handle_recv: replyid not a number in 'message' <%s>\n",msg);
			return;
		}
		res.room=malloc(roomlen+1);
		res.username=malloc(usernamelen+1);
		res.message=malloc(textlen+1);
		assert(res.room&&res.username&&res.message);
		memcpy(res.room,roomp,roomlen);
		res.room[roomlen]='\0';
		memcpy(res.username,usernamep,usernamelen);
		res.username[usernamelen]='\0';
		memcpy(res.message,textp,textlen);
		res.message[textlen]='\0';
		cb(fd,res,payload);
		free(res.room);
		free(res.username);
		free(res.message);
	} else if(cmdlen==7&&memcmp(cmd,"history",7)==0){
		struct net_response res;
		res.type=NET_OK;
		cb(fd,res,payload);
	} else if(cmdlen==15&&memcmp(cmd,"history_message",15)==0){
		if(*p=='\0'){
			debugf("net_handle_recv: no arguments to 'history_message' <%s>\n",msg);
			return;
		}
		p=strchr(p+1,' ');
		if(*p=='\0'){
			debugf("net_handle_recv: no arguments past index to 'history_message' <%s>\n",msg);
			return;
		}
		const char *roomp=p+1;
		p=strchr(roomp,' ');
		const char *q,*r,*s,*t;
		if(p==NULL||(q=strchr(p+1,' '))==NULL||(r=strchr(q+1,' '))==NULL
				||(s=strchr(r+1,' '))==NULL||(t=strchr(s+1,' '))==NULL){
			debugf("net_handle_recv: not enough arguments to 'history_message' <%s>\n",msg);
			return;
		}
		i64 roomlen=p-roomp;
		const char *usernamep=p+1;
		i64 usernamelen=q-usernamep;
		const char *stampp=q+1;
		i64 stamplen=r-stampp;
		const char *msgidp=r+1;
		i64 msgidlen=s-msgidp;
		const char *replyidp=s+1;
		i64 replyidlen=t-replyidp;
		const char *textp=t+1;
		i64 textlen=msglen-(textp-msg);

		struct net_response res;
		res.type=NET_HISTORY;
		const char *endp;
		res.timestamp=strtoll(stampp,(char**)&endp,10);
		if(endp-stampp!=stamplen){
			debugf("net_handle_recv: timestamp not a number in 'history_message' <%s>\n",msg);
			return;
		}
		res.msgid=strtoll(msgidp,(char**)&endp,10);
		if(endp-msgidp!=msgidlen){
			debugf("net_handle_recv: msgid not a number in 'history_message' <%s>\n",msg);
			return;
		}
		res.replyid=strtoll(replyidp,(char**)&endp,10);
		if(endp-replyidp!=replyidlen){
			debugf("net_handle_recv: replyid not a number in 'history_message' <%s>\n",msg);
			return;
		}
		res.room=malloc(roomlen+1);
		res.username=malloc(usernamelen+1);
		res.message=malloc(textlen+1);
		assert(res.room&&res.username&&res.message);
		memcpy(res.room,roomp,roomlen);
		res.room[roomlen]='\0';
		memcpy(res.username,usernamep,usernamelen);
		res.username[usernamelen]='\0';
		memcpy(res.message,textp,textlen);
		res.message[textlen]='\0';
		cb(fd,res,payload);
		free(res.room);
		free(res.username);
		free(res.message);
	} else if(cmdlen==4&&memcmp(cmd,"join",4)==0){
		const char *q;
		if(*p=='\0'||(q=strchr(p+1,' '))==NULL){
			debugf("net_handle_recv: not enough arguments to 'join' <%s>\n",msg);
			return;
		}
		const char *roomp=p+1;
		i64 roomlen=q-roomp;
		const char *userp=q+1;
		i64 userlen=msglen-(userp-msg);

		struct net_response res;
		res.type=NET_JOIN;
		res.room=malloc(roomlen+1);
		res.username=malloc(userlen+1);
		assert(res.room&&res.username);
		memcpy(res.room,roomp,roomlen);
		res.room[roomlen]='\0';
		memcpy(res.username,userp,userlen);
		res.username[userlen]='\0';
		cb(fd,res,payload);
		free(res.room);
		free(res.username);
	} else if(cmdlen==6&&memcmp(cmd,"invite",6)==0){
		const char *q;
		if(*p=='\0'||(q=strchr(p+1,' '))==NULL){
			debugf("net_handle_recv: no arguments to 'invite' <%s>\n",msg);
			return;
		}
		const char *roomp=p+1;
		i64 roomlen=q-roomp;
		const char *userp=q+1;
		i64 userlen=msglen-(userp-msg);

		struct net_response res;
		res.type=NET_INVITE;
		res.room=malloc(roomlen+1);
		res.username=malloc(userlen+1);
		assert(res.room&&res.username);
		memcpy(res.room,roomp,roomlen);
		res.room[roomlen]='\0';
		memcpy(res.username,userp,userlen);
		res.username[userlen]='\0';
		cb(fd,res,payload);
		free(res.room);
	} else if(cmdlen==6&&memcmp(cmd,"online",6)==0){
		const char *q;
		if(*p=='\0'||(q=strchr(p+1,' '))==NULL){
			debugf("net_handle_recv: not enough arguments to 'online' <%s>\n",msg);
			return;
		}
		const char *nump=p+1;
		const char *usernamep=q+1;
		const char *endp;
		struct net_response res=(struct net_response){
			.type=NET_ONLINE,
			.online.username=strdup(usernamep),
			.online.num=strtol(nump,(char**)&endp,10)
		};
		if(nump[0]==' '||*endp!=' '){
			debugf("net_handle_recv: invalid number argument to 'online' <%s>\n",msg);
			return;
		}
		cb(fd,res,payload);
		free(res.online.username);
	} else {
		debugf("net_handle_recv: unknown command <%s>\n",msg);
	}
}