-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.js
193 lines (164 loc) · 4.47 KB
/
room.js
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
const crypto = require("crypto");
const fs = require("fs");
const Packet = require("./server_packet").Packet;
const { Document } = require("./document");
const { ServerName } = require("./ServerName");
const one_hour = 1000 * 60 * 60;
function quicklog(s, f) {
const logpath = "/tmp/" + f + ".log";
s = s.toString().replace(/\r\n|\r/g, "\n"); // hack
const fd = fs.openSync(logpath, "a+", 0o666);
fs.writeSync(fd, s + "\n");
fs.closeSync(fd);
}
const Room = function (url, rooms) {
const chat = [];
let members = [];
let doc = new Document();
const max_members = 255;
const timeout_time = one_hour * 24;
let timeout;
let images = {};
let closed = false;
const timeout_func = function () {
if (closed === true) {
quicklog(`Attempt to close already closed room ${url}!`, "close_errors");
clearTimeout(timeout);
return;
}
closed = true;
members.forEach(function (c) {
c.Disconnect("The room has timed out.");
});
//Delete room
delete rooms[url];
members = [];
doc.Free();
doc = null;
images = null;
fs.readdir(roomcacheurl, function (err, files) {
if (!err) {
files.forEach(function (file) {
fs.unlinkSync(roomcacheurl + "/" + file);
});
fs.rmdirSync(roomcacheurl);
} else {
console.log(err);
}
});
clearTimeout(timeout);
};
const extend_time = function (time) {
time = time || timeout_time;
clearTimeout(timeout);
timeout = setTimeout(timeout_func, time);
};
const roomhash = crypto.createHash("md5");
const roomcacheurl = "static/roomcache/" + roomhash.digest("hex");
try {
fs.mkdirSync(roomcacheurl, "0777");
} catch (e) {
if (e.code != "EEXIST") {
throw e;
}
}
this.member_count = 0;
extend_time();
doc.DoCommand({ cmd: "new_layer", params: {} });
this.Chat = function (client, text) {
chat.push({
sender: client.info,
text: text,
});
new Packet().Chat(client, text).broadcastToRoom(this);
};
// Return the number of places left in the room.
this.getRemainingSpace = function () {
return max_members - members.length;
};
this.clientByName = function (name) {
if (name === ServerName) return {};
members.forEach(function (m) {
if (m.info.name === name) return m;
});
return null;
};
this.clientById = function (id) {
members.forEach(function (m) {
if (m.info.id === id) return m;
});
};
//Add a member to this room.
this.addMember = function (client, newroom) {
if (this.getRemainingSpace() === 0) {
client.Disconnect("Room has no free spaces", this);
return false;
}
//Broadcast info about this new member.
this.member_count = members.length + 1;
client.data.room = this;
let t = 1;
let newname = client.info.name;
while (this.clientByName(newname) !== null) {
newname = client.info.name + t;
t += 1;
}
client.info.name = newname;
new Packet().newMember(client, this).broadcastToRoom(this);
members.push(client);
new Packet()
.acceptJoin(client, newroom)
.Set("history", doc.getHistory())
.chatHistory(chat)
.Send(client);
extend_time();
return true;
};
this.removeClient = function (client) {
members = members.filter((member) => member !== client);
this.member_count = members.length;
if (members.length === 0) {
extend_time();
}
};
this.DoCommand = function (command) {
if (command.cmd === "image") {
if (images[command.key] === undefined) {
return false;
} else {
command.url = images[command.key].url;
}
}
if (doc.DoCommand(command) === true) {
extend_time();
return true;
}
};
this.addImage = function (req, buf, cb) {
const hash = crypto.createHash("md5");
hash.update(buf);
const key = hash.digest("hex");
const ext = req.headers["x-file-name"].substr(ext.lastIndexOf("."));
if (images[key] !== undefined) {
cb(images[key]);
} else {
const imgurl = roomcacheurl + "/" + key + ext;
images[key] = {
url: imgurl.substr(roomcacheurl.indexOf("/")),
key: key,
};
fs.writeFile(imgurl, buf, "binary", function (err) {
if (!err) {
cb(images[key]);
}
});
}
};
this.getMembers = function () {
return members;
};
this.getMembersInfo = function () {
return members.map((member) => member.info);
};
};
exports.Room = Room;