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
|
#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;
struct roomdata{
char *name;
struct t_gui_buffer *buffer;
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 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');
if(p!=NULL){
fprintf(debugf,"room_input_cb: input contained newline <%s>\n",input);
tosend=strdup(input);
*strchr(tosend,'\n')='\0';
} else {
tosend=input;
}
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(tosend!=input){
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 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){
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){
room->buffer=weechat_buffer_new(
room->name,
room_input_cb,room,NULL,
room_close_cb,room,NULL);
}
weechat_printf_date_tags(
room->buffer,res.timestamp/1000000LL,NULL,
"%s\t%s",res.username,res.message);
} 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){
push_net_callback(fd,res,payload);
}
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 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++){
conn->rooms[i]=malloc(sizeof(struct roomdata));
conn->rooms[i]->name=strdup(res.items[i]);
conn->rooms[i]->buffer=weechat_buffer_new(
conn->rooms[i]->name,
room_input_cb,conn->rooms[i],NULL,
room_close_cb,conn->rooms[i],NULL);
conn->rooms[i]->conn=conn;
net_sendf(fd,history_net_callback,conn->rooms[i],"history %s 10",conn->rooms[i]->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 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));
weechat_printf(NULL,"tomsg: Connection dropped");
weechat_buffer_close(conn->buffer);
return WEECHAT_RC_OK;
}
}
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 {
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");
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;
}
|