-
Notifications
You must be signed in to change notification settings - Fork 12
/
tells.js
172 lines (163 loc) · 6.09 KB
/
tells.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
/**
* Tells
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Tells are the offline messaging system for PS. They are received when a
* user successfully connects under a name that has tells waiting, and are
* sent when a pm is sent to a user who does not exist or is not online.
*
* Tells are cleared after they have existed for a certain length of time
* in order to remove any inactive messages. This length of time can be
* specified in config.js
*
* @license MIT license
*/
// jscs:disable
var fs = require('fs');
var color = require('./infinite/color');
var tells = {inbox: {}, outbox: {}};
try {
tells = JSON.parse(fs.readFileSync('config/tells.json'));
} catch (e) {} // file doesn't exist (yet)
/**
* Purge expired messages from those stored
* @param threshold The age limit of an "old" tell, in ms
*/
var pruneOld = exports.pruneOld = function (threshold) {
var now = Date.now();
var receivers = Object.keys(Tells.inbox);
for (var i = 0; i < receivers.length; i++) {
for (var n = 0; n < Tells.inbox[receivers[i]].length; n++) {
if ((now - Tells.inbox[receivers[i]][n].time) >= threshold) {
var ips = Object.keys(Tells.inbox[receivers[i]][n].ips);
for (var ip = 0; ip < ips.length; ip++) {
if (Tells.outbox[ips[ip]]) Tells.outbox[ips[ip]]--;
if (Tells.outbox[ips[ip]] <= 0) delete Tells.outbox[ips[ip]];
}
Tells.inbox[receivers[i]].splice(n, 1);
n--;
}
}
if (!Tells.inbox[receivers[i]].length) delete Tells.inbox[receivers[i]];
}
Tells.writeTells();
};
exports.inbox = tells.inbox || {};
exports.outbox = tells.outbox || {};
/**
* Write the inbox and outbox to file
*/
exports.writeTells = (function () {
var writing = false;
var writePending = false; // whether or not a new write is pending
var finishWriting = function () {
writing = false;
if (writePending) {
writePending = false;
Tells.writeTells();
}
};
return function () {
if (writing) {
writePending = true;
return;
}
writing = true;
var data = JSON.stringify({inbox: Tells.inbox, outbox: Tells.outbox});
fs.writeFile('config/tells.json.0', data, function () {
// rename is atomic on POSIX, but will throw an error on Windows
fs.rename('config/tells.json.0', 'config/tells.json', function (err) {
if (err) {
// This should only happen on Windows.
fs.writeFile('config/tells.json', data, finishWriting);
return;
}
finishWriting();
});
});
};
})();
/**
* Format a user's inbox and send it on to the client to be delivered
* @param userid The userid whose tells to send
* @param user The User object to send the tells to
*/
exports.sendTell = function (userid, user) {
var buffer = '|raw|';
var tellsToSend = Tells.inbox[userid];
for (var i = 0; i < tellsToSend.length; i++) {
var ips = Object.keys(tellsToSend[i].ips);
for (var ip = 0; ip < ips.length; ip++) {
if (Tells.outbox[ips[ip]]) Tells.outbox[ips[ip]]--;
if (Tells.outbox[ips[ip]] <= 0) delete Tells.outbox[ips[ip]];
}
var timeStr = Tells.getTellTime(tellsToSend[i].time);
buffer += '<div class="chat"><font color="gray">[' + timeStr + ' ago]</font> <b><font color="' + color(toId(tellsToSend[i].sender)) + '">' + tellsToSend[i].sender + ':</font></b> ' + Tools.escapeHTML(tellsToSend[i].msg.replace(/\|/g, '|')) + '</div>';
}
user.send(buffer);
delete Tells.inbox[userid];
Tells.writeTells();
};
/**
* Store a tell to be received later
* @param sender The User object of the sender
* @param receiver The target userid
* @param msg The message to be send
* @return false if the receiver has a full inbox
* null if the sender has a full outbox
* otherwise true
*/
exports.addTell = function (sender, receiver, msg) {
if (Tells.inbox[receiver] && Tells.inbox[receiver].length >= 5) return false;
var ips = Object.keys(sender.ips);
for (var i = 0; i < ips.length; i++) {
if (!Tells.outbox[ips[i]]) {
Tells.outbox[ips[i]] = 1;
} else {
if (Tells.outbox[ips[i]] >= 10) return null;
Tells.outbox[ips[i]]++;
}
}
if (!Tells.inbox[receiver]) Tells.inbox[receiver] = [];
var newTell = {
'sender': sender.name,
time: Date.now(),
'msg': msg,
ips: sender.ips
};
Tells.inbox[receiver].push(newTell);
Tells.writeTells();
return true;
};
/**
* Converts a UNIX timestamp into 'x minutes, y seconds ago' form
* @param time UNIX timestamp (e.g., 1405460769855)
* @return A human readable time difference between now and the given time
*/
exports.getTellTime = function (time) {
time = Date.now() - time;
time = Math.round(time / 1000); // rounds to nearest second
var seconds = time % 60;
var times = [];
if (seconds) times.push(String(seconds) + (seconds === 1 ? ' second' : ' seconds'));
var minutes, hours, days;
if (time >= 60) {
time = (time - seconds) / 60; // converts to minutes
minutes = time % 60;
if (minutes) times.unshift(String(minutes) + (minutes === 1 ? ' minute' : ' minutes'));
if (time >= 60) {
time = (time - minutes) / 60; // converts to hours
hours = time % 24;
if (hours) times.unshift(String(hours) + (hours === 1 ? ' hour' : ' hours'));
if (time >= 24) {
days = (time - hours) / 24; // you can probably guess this one
if (days) times.unshift(String(days) + (days === 1 ? ' day' : ' days'));
}
}
}
if (!times.length) times.push('0 seconds');
return times.join(', ');
};
// clear old messages every two hours
exports.pruneOldTimer = setInterval(pruneOld, 1000 * 60 * 60 * 2,
Config.tellsexpiryage || 1000 * 60 * 60 * 24 * 7);