This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tools.js
259 lines (247 loc) · 9.18 KB
/
tools.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
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
'use strict';
const fs = require('fs');
const https = require('https');
const mysql = require('mysql');
const sqlite3 = require('sqlite3');
let regdateCache = {};
let sqliteDb;
try {
regdateCache = JSON.parse(fs.readFileSync('config/regdate.json', 'utf8'));
} catch (e) {}
if (!Config.mysql) {
sqliteDb = new sqlite3.Database('./config/users.db', function () {
sqliteDb.run("CREATE TABLE IF NOT EXISTS users (id TEXT, userid TEXT PRIMARY KEY, name TEXT, lastOnline INTEGER, lastOnlineServer TEXT, lastOnlineAction TEXT, room TEXT)");
sqliteDb.run("CREATE TABLE IF NOT EXISTS tells (userid TEXT, sender TEXT, message TEXT, date INTEGER)");
});
} else {
// lets make sure the database is setup
let connection = mysql.createConnection({
host: Config.mysql.host,
port: Config.mysql.port,
user: Config.mysql.user,
password: Config.mysql.password,
});
connection.connect(function (err) {
if (err) {
Config.mysql = false; // something went wrong, revert back to using sqlite
return console.log('Error connecting to mysql server: ' + err.stack);
}
connection.query('CREATE DATABASE IF NOT EXISTS ' + Config.mysql.dbName + ';', function (err, results, fields) {
if (err) {
Config.mysql = false;
return console.log('Error creating mysql database.');
}
connection.changeUser({database: Config.mysql.dbName}, function (err) {
if (err) {
Config.mysql = false;
return console.log('Error changing mysql databases.');
}
connection.query('CREATE TABLE IF NOT EXISTS users (id VARCHAR(32), firstSeen BIGINT, userid VARCHAR(19) PRIMARY KEY, name VARCHAR(32), lastOnline BIGINT, lastOnlineServer VARCHAR(32), lastOnlineAction VARCHAR(100), room VARCHAR(100))', function (error, results, fields) {
if (err) {
Config.mysql = false;
return console.log('Error creating mysql users table.');
}
connection.query('CREATE TABLE IF NOT EXISTS logs (id INT PRIMARY KEY AUTO_INCREMENT, date BIGINT, server VARCHAR(32), room VARCHAR(100), user VARCHAR(32), userid VARCHAR(19), messageType VARCHAR(20), message TEXT)', function (error, results, fields) {
if (err) {
Config.mysql = false;
return console.log('Error creating mysql logs table.');
}
connection.query('CREATE TABLE IF NOT EXISTS tells (userid VARCHAR(19), sender VARCHAR(32), message TEXT, date BIGINT)', function (error, results, fields) {
if (err) {
Config.mysql = false;
return console.log('Error creating mysql tells table.');
}
});
});
});
});
});
});
}
module.exports = {
query: function (queryString, options, callback) {
if (!callback) callback = options;
if (Config.mysql) {
let connection = mysql.createConnection({
host: Config.mysql.host,
port: Config.mysql.port,
user: Config.mysql.user,
password: Config.mysql.password,
database: Config.mysql.dbName,
});
connection.connect(function (err) {
if (err) {
console.log('Error connecting to mysql server: ' + err);
return callback(err);
}
connection.query(queryString, function (err, rows) {
if (err) {
console.log('Error querying mysql database: ' + err);
return callback(err);
}
return callback(false, rows);
});
connection.end(function (err) {
if (err) console.log('Error ending mysql connection: ' + err);
});
});
connection.on('error', function (err) {
if (err) console.log('mysql err: ' + err);
});
} else {
sqliteDb.all(queryString, options, function (err, rows) {
if (err) {
console.log('Error querying sqlite database: ' + err);
return callback(err);
}
return callback(false, rows);
});
}
},
sanitize: function (message) {
if (message.charAt(0) === '/') message = '/' + message;
if (message.charAt(0) === '!' || message.substr(0, 2) === '>>') message = ' ' + message;
return message.replace(/\n/g, '');
},
log: function (text, serverid, error) {
if (error) {
fs.appendFileSync('logs/error.txt', '[' + serverid + '] ' + text);
}
fs.appendFileSync('logs/' + serverid + '.log', text + '\n');
console.log(text);
},
randomString: function (length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
},
toTimeStamp: function (date, options) {
// Return a timestamp in the form {yyyy}-{MM}-{dd} {hh}:{mm}:{ss}.
// Optionally reports hours in mod-12 format.
const isHour12 = options && options.hour12;
let parts = [date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()];
if (isHour12) {
parts.push(parts[3] >= 12 ? 'pm' : 'am');
parts[3] = parts[3] % 12 || 12;
}
parts = parts.map(val => val < 10 ? '0' + val : '' + val);
return parts.slice(0, 3).join("-") + " " + parts.slice(3, 6).join(":") + (isHour12 ? " " + parts[6] : "");
},
regdate: function (target, callback) {
target = toId(target);
if (regdateCache[target]) return callback(regdateCache[target]);
let options = {
host: 'pokemonshowdown.com',
port: 443,
path: '/users/' + target + '.json',
method: 'GET',
};
https.get(options, function (res) {
let data = '';
res.on('data', function (chunk) {
data += chunk;
}).on('end', function () {
if (data.charAt(0) !== '{') data = JSON.stringify({registertime: 0});
data = JSON.parse(data);
let date = data['registertime'];
if (date !== 0 && date.toString().length < 13) {
while (date.toString().length < 13) {
date = Number(date.toString() + '0');
}
}
if (date !== 0) {
regdateCache[target] = date;
fs.writeFileSync('config/regdate.json', JSON.stringify(regdateCache));
}
callback((date === 0 ? false : date));
});
});
},
// toDurationString from: https://github.com/Zarel/Pokemon-Showdown/blob/master/chat.js
toDurationString: function (number, options) {
// TODO: replace by Intl.DurationFormat or equivalent when it becomes available (ECMA-402)
// https://github.com/tc39/ecma402/issues/47
const date = new Date(+number);
const parts = [date.getUTCFullYear() - 1970, date.getUTCMonth(), date.getUTCDate() - 1, date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()];
const unitNames = ["second", "minute", "hour", "day", "month", "year"];
const positiveIndex = parts.findIndex(elem => elem > 0);
if (options && options.hhmmss) {
let string = parts.slice(positiveIndex).map(value => value < 10 ? "0" + value : "" + value).join(":");
return string.length === 2 ? "00:" + string : string;
}
return parts.slice(positiveIndex).reverse().map((value, index) => value ? value + " " + unitNames[index] + (value > 1 ? "s" : "") : "").reverse().join(" ").trim();
},
// uploadToHastebin function by TalkTakesTime (https://github.com/TalkTakesTime/Pokemon-Showdown-Bot)
uploadToHastebin: function (toUpload, callback) {
if (typeof callback !== 'function') return false;
let reqOpts = {
hostname: 'hastebin.com',
port: 443,
method: 'POST',
path: '/documents',
};
let req = https.request(reqOpts, function (res) {
res.on('data', function (chunk) {
// CloudFlare can go to hell for sending the body in a header request like this
if (typeof chunk === 'string' && chunk.substr(0, 15) === '<!DOCTYPE html>') return callback('Error uploading to Hastebin.');
let filename = JSON.parse(chunk.toString()).key;
callback('http://hastebin.com/raw/' + filename);
});
});
req.on('error', function (e) {
callback('Error uploading to Hastebin: ' + e.message);
});
req.write(toUpload);
req.end();
},
getServers: function (callback) {
https.get('https://pokemonshowdown.com/servers/', res => {
let servers = [];
let data = '';
res.on('data', chunk => {
data += chunk;
}).on('end', () => {
data = data.replace(/\n/g, '').replace(/\s+/g, ' ');
//let servers = {};
let rows = data.split('<tr>');
rows.splice(0, 1);
for (let u in rows) {
if (rows[u].includes('(offline)')) continue;
let serverName = rows[u].split('<strong>')[1].split('</strong>')[0];
let serverid = rows[u].match(/<a href="\/servers\/[a-z]+/)[0].replace(/<a href="\/servers\//, '');
let owners = (rows[u].match(/<small>([a,-z]+)<\/small>/) ? rows[u].match(/<small>([a,-z]+)<\/small>/)[0].replace(/(<small>|<\/small>)/g, '') : '');
let userCount = rows[u].match(/\([0-9]+\)/)[0].replace(/(\(|\))/, '');
servers.push({
name: serverName,
id: serverid,
userCount: userCount,
owners: owners,
});
}
return callback(servers);
});
}).on('error', e => {
console.log('Error retrieving server list: ' + e);
});
},
getServerIp: function (id, callback) {
if (id === 'showdown') {
return callback({host: 'sim.smogon.com', port: 8000, name: 'Smogon University'});
}
https.get('https://play.pokemonshowdown.com/crossdomain.php?host=' + id + '.psim.us', res => {
let data = '';
res.on('data', chunk => {
data += chunk;
}).on('end', () => {
let lines = data.split('\n');
let config;
for (let u in lines) {
if (lines[u].includes('config')) {
config = lines[u].replace(/\\"/g, '"').substr(14);
config = config.substr(0, config.length - 2);
break;
}
}
return callback(JSON.parse(config));
});
});
},
};