aboutsummaryrefslogtreecommitdiff
path: root/weechat/tomsg.c
blob: 3b188ecebedd70c012e0ae9d04aa0f1c0130046a (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "weechat-plugin.h"
#include "net.h"

WEECHAT_PLUGIN_NAME("tomsg")
WEECHAT_PLUGIN_DESCRIPTION("tomsg client plugin")
WEECHAT_PLUGIN_AUTHOR("Tom Smeding")
WEECHAT_PLUGIN_VERSION("0.1")
WEECHAT_PLUGIN_LICENSE("MIT")
WEECHAT_PLUGIN_PRIORITY(1000)


static const char *errpfx,*netpfx;


struct roomdata{
	char *name;
	struct t_gui_buffer *buffer;
	struct t_gui_nick_group *buffer_nickgroup;
	int nmembers;
	struct conndata *conn;  // do not free
};

struct conndata{
	int fd;
	struct t_hook *fd_hook;

	struct t_gui_buffer *buffer;

	int nrooms,roomscap;
	struct roomdata **rooms;

	char *username,*pending_username;

	i64 linebuf_sz,linebuf_len;
	char *linebuf;
};


FILE *debugf;

static struct t_weechat_plugin *weechat_plugin;

static struct t_hashtable *conntable;


static void room_update_attributes(struct roomdata *room){
	bool private=room->nmembers<=2;
	weechat_buffer_set(room->buffer,"localvar_set_type",private?"private":"channel");
	weechat_buffer_set(room->buffer,"notify",private?"3":"1");
	fprintf(debugf,"room_update_attributes: set private for room %s to %d\n",room->name,private);
	if(room->conn->username){
		weechat_buffer_set(room->buffer,"localvar_set_nick",room->conn->username);
	} else {
		weechat_buffer_set(room->buffer,"localvar_del_nick","");
	}
}

static void close_room(struct roomdata *room){
	fprintf(debugf,"close_room(room=%p)\n",room);
	free(room->name);
	if(room->buffer)weechat_buffer_close(room->buffer);
	free(room);
}

static void message_net_callback(int fd,struct net_response res,void *payload){
	(void)payload;
	fprintf(debugf,"message_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	struct conndata *conn=weechat_hashtable_get(conntable,&fd);
	assert(conn);
	if(res.type==NET_ERROR){
		weechat_printf(conn->buffer,"tomsg: send threw error: %s",res.error);
	} else if(res.type!=NET_OK){
		fprintf(debugf,"message_net_callback: res.type=%d\n",res.type);
	}
}

static int room_input_cb(const void *room_vp,void *_d,struct t_gui_buffer *buffer,const char *input){
	(void)_d; (void)buffer;
	struct roomdata *room=(struct roomdata*)room_vp;
	struct conndata *conn=room->conn;
	const char *tosend;
	const char *p=strchr(input,'\n');
	bool skipfirst=input[0]=='/'&&input[1]=='/';
	bool free_tosend=false;
	if(p!=NULL){
		fprintf(debugf,"room_input_cb: input contained newline <%s>\n",input);
		tosend=strdup(input+skipfirst);
		*strchr(tosend,'\n')='\0';
		free_tosend=true;
	} else {
		tosend=input+skipfirst;
	}

	net_sendf(conn->fd,message_net_callback,NULL,"send %s %s",room->name,tosend);
	weechat_printf(room->buffer,"%s\t%s",conn->username,tosend);

	if(free_tosend){
		free((void*)tosend);
	}
	return WEECHAT_RC_OK;
}

static int room_close_cb(const void *room_vp,void *_d,struct t_gui_buffer *buffer){
	(void)_d; (void)buffer;
	struct roomdata *room=(struct roomdata*)room_vp;
	room->buffer=NULL;
	return WEECHAT_RC_OK;
}

static void create_room_buffer(struct roomdata *room){
	if(room->buffer!=NULL){
		weechat_buffer_close(room->buffer);
	}
	room->buffer=
		weechat_buffer_new(room->name, room_input_cb,room,NULL, room_close_cb,room,NULL);
	weechat_buffer_set(room->buffer,"nicklist","1");
	weechat_buffer_set(room->buffer,"nicklist_case_sensitive","1");
	weechat_buffer_set(room->buffer,"nicklist_display_groups","0");

	room->buffer_nickgroup=NULL;
	room_update_attributes(room);
}

static void history_net_callback(int fd,struct net_response res,void *payload){
	struct roomdata *room=(struct roomdata*)payload;
	assert(room);
	fprintf(debugf,"history_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
}

static void members_net_callback(int fd,struct net_response res,void *payload){
	struct roomdata *room=(struct roomdata*)payload;
	assert(room);
	fprintf(debugf,"members_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	if(res.type!=NET_LIST){
		fprintf(debugf,"members_net_callback: res.type=%d\n",res.type);
		return;
	}
	if(room->buffer_nickgroup!=NULL){
		weechat_nicklist_remove_all(room->buffer);
	}
	room->buffer_nickgroup=
		weechat_nicklist_add_group(room->buffer,NULL,"members","weechat.color.nicklist_group",1);
	for(int i=0;i<res.nitems;i++){
		weechat_nicklist_add_nick(
			room->buffer,room->buffer_nickgroup,
			res.items[i],"chat_nick",
			"","chat_nick",
			1);
	}
	room->nmembers=res.nitems;
	room_update_attributes(room);
}

static void push_net_callback(int fd,struct net_response res,void *payload){
	(void)payload;
	fprintf(debugf,"push_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	struct conndata *conn=weechat_hashtable_get(conntable,&fd);
	assert(conn);
	if(res.type==NET_MESSAGE||res.type==NET_JOIN||res.type==NET_INVITE){
		i64 roomi;
		for(roomi=0;roomi<conn->nrooms;roomi++){
			if(strcmp(conn->rooms[roomi]->name,res.room)==0){
				break;
			}
		}
		if(roomi==conn->nrooms){
			fprintf(debugf,"push_net_callback: message to unknown room '%s'\n",res.room);
			return;
		}
		struct roomdata *room=conn->rooms[roomi];
		if(room->buffer==NULL){
			create_room_buffer(room);
		}

		if(res.type==NET_MESSAGE){
			weechat_printf_date_tags(
				room->buffer,res.timestamp/1000000LL,NULL,
				"%s\t%s",res.username,res.message);
		} else if(res.type==NET_JOIN){
			weechat_printf(room->buffer,"%sUser %s joined this room",netpfx,res.username);
			if(room->buffer_nickgroup){
				weechat_nicklist_add_nick(
					room->buffer,room->buffer_nickgroup,
					res.username,"chat_nick",
					"","chat_nick",
					1);
			}
			room->nmembers++;
			room_update_attributes(room);
		} else if(res.type==NET_INVITE){
			weechat_printf(room->buffer,"%sYou were invited into this room",netpfx);
			net_sendf(fd,history_net_callback,room,"history %s 10",room->name);
		} else {
			assert(false);
		}
	} else {
		fprintf(debugf,"push_net_callback: unknown response type %d\n",res.type);
	}
}

static void history_push_net_callback(int fd,struct net_response res,void *payload){
	fprintf(debugf,"history_ -> ");
	push_net_callback(fd,res,payload);
}

static void roomlist_net_callback(int fd,struct net_response res,void *payload){
	(void)payload;
	fprintf(debugf,"roomlist_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	struct conndata *conn=weechat_hashtable_get(conntable,&fd);
	assert(conn);
	if(res.type==NET_LIST){
		for(i64 i=0;i<conn->nrooms;i++){
			close_room(conn->rooms[i]);
		}
		if(conn->roomscap<res.nitems){
			conn->roomscap=res.nitems+1;
			conn->rooms=realloc(conn->rooms,conn->roomscap*sizeof(struct roomdata*));
			assert(conn->rooms);
		}
		conn->nrooms=res.nitems;
		for(i64 i=0;i<res.nitems;i++){
			struct roomdata *room=conn->rooms[i]=malloc(sizeof(struct roomdata));
			room->name=strdup(res.items[i]);
			room->conn=conn;
			room->buffer=NULL;
			create_room_buffer(room);
			net_sendf(fd,history_net_callback,room,"history %s 10",room->name);
			net_sendf(fd,members_net_callback,room,"list_members %s",room->name);
		}
	} else {
		fprintf(debugf,"roomlist_net_callback: res.type=%d\n",res.type);
	}
}

static void login_net_callback(int fd,struct net_response res,void *payload){
	(void)payload;
	fprintf(debugf,"login_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	struct conndata *conn=weechat_hashtable_get(conntable,&fd);
	assert(conn);
	if(res.type==NET_OK){
		if(conn->username)free(conn->username);
		conn->username=conn->pending_username;
		conn->pending_username=NULL;

		weechat_printf(conn->buffer,"Successfully logged in");
		net_sendf(conn->fd,roomlist_net_callback,NULL,"list_rooms");
	} else if(res.type==NET_ERROR){
		weechat_printf(conn->buffer,"Error logging in: %s",res.error);
	} else {
		fprintf(debugf,"login_net_callback: res.type=%d\n",res.type);
	}
}

static void pong_net_callback(int fd,struct net_response res,void *payload){
	(void)payload;
	fprintf(debugf,"pong_net_callback(fd=%d,res={.type=%d})\n",fd,res.type);
	struct conndata *conn=weechat_hashtable_get(conntable,&fd);
	assert(conn);
	if(res.type==NET_PONG){
		weechat_printf(conn->buffer,"pong");
	} else {
		fprintf(debugf,"pong_net_callback: res.type=%d\n",res.type);
	}
}

static void conn_destroy(struct conndata *conn){
	fprintf(debugf,"conn_destroy(conn=%p (fd=%d))\n",conn,conn->fd);
	weechat_unhook(conn->fd_hook);
	if(conntable)weechat_hashtable_remove(conntable,&conn->fd);
	for(int i=0;i<conn->nrooms;i++){
		close_room(conn->rooms[i]);
	}
	if(conn->username)free(conn->username);
	if(conn->pending_username)free(conn->pending_username);
	free(conn->rooms);
	free(conn->linebuf);
	close(conn->fd);
	free(conn);
}

static int fd_hook_callback(const void *conn_vp,void *_d,int fd){
	(void)_d;
	struct conndata *conn=(struct conndata*)conn_vp;
	fprintf(debugf,"fd_hook_callback(conn=%p (fd=%d))\n",conn,fd);
	assert(fd==conn->fd);
	
	if(conn->linebuf_len>conn->linebuf_sz/2){
		conn->linebuf_sz*=2;
		conn->linebuf=realloc(conn->linebuf,conn->linebuf_sz);
		assert(conn->linebuf);
	}
	i64 nr;
	while(true){
		nr=recv(fd,conn->linebuf+conn->linebuf_len,conn->linebuf_sz-conn->linebuf_len,0);
		if(nr<=0){
			if(nr<0){
				if(errno==EINTR)continue;
				if(errno==EAGAIN)return WEECHAT_RC_OK;  // next time around maybe?
				fprintf(debugf,"fd_hook_callback: recv() < 0: %s\n",strerror(errno));
			} else {
				fprintf(debugf,"fd_hook_callback: recv() == 0 (EOF)\n");
			}
			weechat_printf(NULL,"tomsg: Connection dropped");
			weechat_buffer_close(conn->buffer);
			return WEECHAT_RC_OK;
		}
		break;
	}
	conn->linebuf_len+=nr;
	while(true){
		char *p=memchr(conn->linebuf,'\n',conn->linebuf_len);
		if(p==NULL)break;
		*p='\0';
		i64 lenp1=p-conn->linebuf+1;
		net_handle_recv(conn->fd,conn->linebuf);
		memmove(conn->linebuf,conn->linebuf+lenp1,conn->linebuf_len-lenp1);
		conn->linebuf_len-=lenp1;
	}
	return WEECHAT_RC_OK;
}

static int conn_input_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffer,const char *input){
	(void)_d;
	struct conndata *conn=(struct conndata*)conn_vp;
	fprintf(debugf,"conn_input_cb(conn=%p,buffer=%p,input=\"%s\")\n",conn,buffer,input);
	
	char *input2=strdup(input);
	assert(input2);
	char *cursor=input2;

	char *cmd=strsep(&cursor," ");
	if(cmd==NULL){
		free(input2);
		return WEECHAT_RC_OK;
	}
	if(strcmp(cmd,"login")==0){
		char *username=strsep(&cursor," ");
		char *password=cursor;
		if(conn->pending_username)free(conn->pending_username);
		conn->pending_username=strdup(username);
		net_sendf(conn->fd,login_net_callback,NULL,"login %s %s",username,password);
	} else if(strcmp(cmd,"ping")==0){
		net_sendf(conn->fd,pong_net_callback,NULL,"ping");
	} else {
		weechat_printf(conn->buffer,"Unknown command '%s'",cmd);
	}

	return WEECHAT_RC_OK;
}

static int conn_close_cb(const void *conn_vp,void *_d,struct t_gui_buffer *buffer){
	(void)_d; (void)buffer;
	struct conndata *conn=(struct conndata*)conn_vp;
	fprintf(debugf,"conn_close_cb(conn=%p,buffer=%p)  fd=%d\n",conn,buffer,conn->fd);
	conn_destroy(conn);
	return WEECHAT_RC_OK;
}

static int connect_cb(const void *_p,void *hostname,int status,int _g,int fd,const char *err,const char *_i){
	(void)_p; (void)_g; (void)_i;
	switch(status){
		case WEECHAT_HOOK_CONNECT_OK: {
			struct conndata *conn=malloc(sizeof(struct conndata));
			assert(conn);
			conn->fd=fd;
			conn->fd_hook=weechat_hook_fd(fd,1,0,0,fd_hook_callback,conn,NULL);
			conn->buffer=weechat_buffer_new((char*)hostname,conn_input_cb,conn,NULL,conn_close_cb,conn,NULL);
			conn->nrooms=0;
			conn->roomscap=2;
			conn->rooms=malloc(conn->roomscap*sizeof(struct roomdata));
			assert(conn->rooms);
			conn->username=NULL;
			conn->pending_username=NULL;
			conn->linebuf_sz=512;
			conn->linebuf_len=0;
			conn->linebuf=malloc(conn->linebuf_sz);
			assert(conn->linebuf);

			weechat_printf(conn->buffer,"Connected!");

			weechat_hashtable_set(conntable,&fd,conn);

			return WEECHAT_RC_OK;
		}

		default:
			weechat_printf(NULL,"%stomsg: Could not connect to %s: %s",errpfx,(char*)hostname,err);
			return WEECHAT_RC_ERROR;
	}
}

static int cmd_tomsg_cb(const void *_p,void *_d,struct t_gui_buffer *buffer,int argc,char **argv,char **_a){
	(void)_p; (void)_d; (void)_a;
	if(argc<2){
		weechat_printf(buffer,"%stomsg: Invalid number of arguments to /tomsg",errpfx);
		return WEECHAT_RC_ERROR;
	}
	if(strcmp(argv[1],"connect")==0){
		if(argc<3||argc>4){
			weechat_printf(buffer,"%stomsg: Invalid number of arguments to /tomsg connect",errpfx);
			return WEECHAT_RC_ERROR;
		}

		char *hostname=argv[2];
		char *endp;
		int port;
		if(argc==4){
			port=strtol(argv[3],&endp,10);
			if(argv[3][0]=='\0'||*endp!='\0'||port<=0||port>=65536){
				weechat_printf(buffer,"%sInvalid port number",errpfx);
				return WEECHAT_RC_ERROR;
			}
		} else {
			port=29536;
		}
		if(strlen(hostname)==0){
			weechat_printf(buffer,"%stomsg: Invalid hostname",errpfx);
			return WEECHAT_RC_ERROR;
		}

		fprintf(debugf,"Connecting to %s:%d\n",hostname,port);
		char *hostname_copy=strdup(hostname);
		weechat_hook_connect(
			NULL,
			hostname,port,1,0,
			NULL,NULL,0,NULL,
			NULL,
			connect_cb,NULL,hostname_copy);
	} else {
		weechat_printf(buffer,"%stomsg: Unknown command \"%s\" to /tomsg",errpfx,argv[1]);
		return WEECHAT_RC_ERROR;
	}

	return WEECHAT_RC_OK;
}

int weechat_plugin_init(struct t_weechat_plugin *plugin,int argc,char **argv){
	(void)argc; (void)argv;
	weechat_plugin=plugin;

	char *fname;
	asprintf(&fname,"%s/Desktop/debugf.txt",getenv("HOME"));
	debugf=fopen(fname,"w");
	free(fname);
	setvbuf(debugf,NULL,_IONBF,0);

	fprintf(debugf,"------\n");

	errpfx=weechat_prefix("error");
	netpfx=weechat_prefix("network");

	weechat_hook_command(
		"tomsg",
		"Execute commands related to tomsg.",
		"connect <hostname> [port]",
		"  connect: Connect to a tomsg server",
		NULL,
		cmd_tomsg_cb,NULL,NULL);

	net_set_push_callback(push_net_callback);
	net_set_history_callback(history_push_net_callback);

	conntable=weechat_hashtable_new(
		16,WEECHAT_HASHTABLE_INTEGER,WEECHAT_HASHTABLE_POINTER,NULL,NULL);

	return WEECHAT_RC_OK;
}

int weechat_plugin_end(struct t_weechat_plugin *plugin){
	(void)plugin;
	weechat_hashtable_free(conntable);
	conntable=NULL;
	fclose(debugf);
	return WEECHAT_RC_OK;
}