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
|
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "db.h"
#include "firebase.h"
#include "user_data.h"
#define JS_PLUGIN_PATH "firebase-io/firebase-io.js"
FILE *js_write=NULL;
static void write_field(FILE *f,const char *key,const char *value){
fprintf(f," %s %zu %s",key,strlen(value),value);
}
static void token_send(const char *token,const char *to_user,const char *room,const char *user,const char *msg){
fprintf(stderr,"token_send(%s,%s,%s,%s,%s)\n",token,to_user,room,user,msg);
fprintf(js_write,"message");
write_field(js_write,"token",token);
write_field(js_write,"room",room);
write_field(js_write,"user",user);
write_field(js_write,"to_user",to_user);
write_field(js_write,"message",msg);
fprintf(js_write,"\n");
fflush(js_write);
}
static void script_output_listener(FILE *js_read){
// Do not use js_write here, it's NULL
char *line=NULL;
size_t linecap=0;
ssize_t linelen;
while(true){
linelen=getline(&line,&linecap,js_read);
if(linelen<0){
if(errno==EINTR)continue;
break;
}
if(line[linelen-1]!='\n'){
fprintf(stderr,"firebase thread: unexpected EOF from js script\n");
return;
}
line[linelen-1]='\0';
linelen--;
char *spacep=strchr(line,' ');
if(spacep==NULL)goto invalid_line;
*spacep='\0';
char *cmd=line;
if(strcmp(cmd,"add_token")==0||strcmp(cmd,"delete_token")==0){
char *username=spacep+1;
spacep=strchr(username,' ');
if(spacep==NULL)goto invalid_line;
*spacep='\0';
char *token=spacep+1;
i64 userid=db_find_user(username);
if(userid==-1){
fprintf(stderr,"firebase thread: unknown username '%s'\n",username);
continue;
}
if(strcmp(cmd,"add_token")==0){
fprintf(stderr,"firebase thread: adding token '%s' to %" PRIi64 "\n",token,userid);
db_add_token(userid,token);
} else {
fprintf(stderr,"firebase thread: deleting token '%s' from %" PRIi64 "\n",token,userid);
db_delete_token(userid,token);
}
}
continue;
invalid_line:
fprintf(stderr,"firebase thread: Invalid line from js script: <%s>\n",line);
}
if(linelen<0){
if(feof(js_read)){
fprintf(stderr,"firebase thread: feof(js_read)\n");
} else {
fprintf(stderr,"firebase thread: getline: errno = %d (%s)\n",errno,strerror(errno));
}
}
}
void firebase_init(void){
int wrpipe[2],rdpipe[2];
if(pipe(wrpipe)<0||pipe(rdpipe)<0){
perror("pipe");
exit(1);
}
pid_t pid=fork(); // The JS script
if(pid<0){
perror("fork");
exit(1);
}
if(pid==0){
close(wrpipe[1]);
close(rdpipe[0]);
dup2(wrpipe[0],0);
dup2(rdpipe[1],1);
execl(JS_PLUGIN_PATH,JS_PLUGIN_PATH,NULL);
perror("execl");
exit(1);
}
close(wrpipe[0]);
close(rdpipe[1]);
pid=fork(); // The script stdout listener
if(pid<0){
perror("fork");
exit(1);
}
if(pid==0){
close(wrpipe[1]);
FILE *js_read=fdopen(rdpipe[0],"r");
db_reinit();
script_output_listener(js_read);
fclose(js_read);
exit(0);
}
js_write=fdopen(wrpipe[1],"w");
printf("Started js firebase plugin\n");
}
void firebase_stop(void){
if(js_write){
// Script will stop itself on EOF on stdin
fclose(js_write);
js_write=NULL;
}
}
void firebase_send_message(const char *room,i64 roomid,const char *user,const char *msg){
// If not initialised, do nothing
if(!js_write)return;
if(roomid==-1)roomid=db_find_room(room);
if(roomid==-1){
debug("firebase_send_message: Cannot find roomid for room '%s'",room);
return;
}
struct db_user_list members=db_list_members(roomid);
for(i64 i=0;i<members.count;i++){
if(userdata_is_active(members.list[i].id))continue;
struct db_strings_list tokens=db_user_tokens(members.list[i].id);
for(i64 j=0;j<tokens.count;j++){
token_send(tokens.list[j],members.list[i].name,room,user,msg);
}
db_nullify_strings_list(tokens);
}
db_nullify_user_list(members);
}
|