Skip to content

Commit

Permalink
Display serial commonality with !serialinfo too
Browse files Browse the repository at this point in the history
Fixes #719
  • Loading branch information
RussellLVP committed Jun 14, 2020
1 parent a6e5536 commit 2d26cd4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
49 changes: 39 additions & 10 deletions javascript/features/punishments/ban_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,31 @@ const RECENT_IP_COUNT_QUERY = `
// MySQL queries to find the serial numbers that have (recently) been used by a particular nickname.
const RECENT_SERIAL_QUERY = `
SELECT
gpci_hash,
COUNT(session_id) AS total
sessions.gpci_hash,
IFNULL(sessions_common_gpci.hits, 0) AS hits,
COUNT(sessions.session_id) AS total
FROM
sessions
LEFT JOIN
sessions_common_gpci ON sessions_common_gpci.gpci_hash = sessions.gpci_hash
WHERE
nickname = ? AND
DATEDIFF(NOW(), session_date) < ?
sessions.nickname = ? AND
DATEDIFF(NOW(), sessions.session_date) < ?
GROUP BY
gpci_hash
sessions.gpci_hash
ORDER BY
session_date DESC
sessions.session_date DESC
LIMIT
10`;

const FREQUENT_SERIAL_QUERY = `
SELECT
sessions_common_gpci.hits
FROM
sessions_common_gpci
WHERE
gpci_hash = ?`

const RECENT_SERIAL_COUNT_QUERY = `
SELECT
COUNT(DISTINCT gpci_hash) AS total
Expand Down Expand Up @@ -477,7 +488,13 @@ export class BanDatabase {

// Finds the nicknames that have (recently) been used by the given |serial|.
async findNicknamesForSerial({ serial, ...options } = {}) {
return this._findNicknamesQuery({ serial, ...options });
const [ results, serialHits ] = await Promise.all([
this._findNicknamesQuery({ serial, ...options }),
this._isCommonSerialQuery(serial),
]);

results.commonSerial = serialHits > 250;
return results;
}

// Actually runs the queries necessary to determine the active nicknames for either an IP
Expand All @@ -491,7 +508,8 @@ export class BanDatabase {

const results = {
total: 0,
entries: []
entries: [],
commonSerial: false,
};

if (totalResults && totalResults.rows.length === 1)
Expand All @@ -505,6 +523,12 @@ export class BanDatabase {
return results;
}

// Returns a boolean indicating whether the given |serial| is considered common or not.
async _isCommonSerialQuery(serial) {
const result = await server.database.query(FREQUENT_SERIAL_QUERY, serial);
return result && result.rows.length ? result.rows[0].hits : 0;
}

// Finds the IP addresses that have (recently) been used by the given |nickname|.
async findIpAddressesForNickname({ nickname, maxAge = 1095 } = {}) {
const [ totalResults, topResults ] = await Promise.all([
Expand Down Expand Up @@ -544,8 +568,13 @@ export class BanDatabase {
results.total = totalResults.rows[0].total;

if (topResults && topResults.rows.length) {
for (const row of topResults.rows)
results.entries.push({ text: row.gpci_hash, sessions: row.total });
for (const row of topResults.rows) {
results.entries.push({
text: row.gpci_hash,
common: row.hits > 250,
sessions: row.total
});
}
}

return results;
Expand Down
17 changes: 15 additions & 2 deletions javascript/features/punishments/nuwani_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ export class NuwaniCommands {
async onSerialInfoCommand(context, value, maxAge = 1095) {
let serialNumber = parseInt(value, 10);
let results = null;
let warning = '';

if (!Number.isNaN(serialNumber) && serialNumber.toString().length == value.length) {
results = await this.database_.findNicknamesForSerial({
Expand All @@ -606,6 +607,10 @@ export class NuwaniCommands {
context.respond(`4Error: No nicknames found for the serial number ${value}.`);
return;
}

if (results.commonSerial)
warning = ' 4(common serial!)';

} else {
results = await this.database_.findSerialsForNickname({
nickname: value,
Expand All @@ -622,7 +627,7 @@ export class NuwaniCommands {
if (results.total > results.entries.length)
suffix = ` 15[${results.total - results.entries.length} omitted...]`;

context.respond('5Result: ' + this.formatInfoResults(results) + suffix);
context.respond(`5Result${warning}: ${this.formatInfoResults(results)}${suffix}`);
}

// !unban [nickname | ip | ip range | serial] [reason]
Expand Down Expand Up @@ -872,8 +877,16 @@ export class NuwaniCommands {
let resultText = [];

for (const entry of results.entries) {
const meta = [];

if (entry.common)
meta.push('4common14');

if (entry.sessions > 1)
resultText.push(`${entry.text} 14(${entry.sessions}x)`);
meta.push(format('%dx', entry.sessions));

if (meta.length >= 1)
resultText.push(`${entry.text} 14(${meta.join(', ')})`);
else
resultText.push(entry.text);
}
Expand Down
5 changes: 4 additions & 1 deletion javascript/features/punishments/test/mock_ban_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ export class MockBanDatabase extends BanDatabase {
};
}

// Overridden.
async _isCommonSerialQuery(serial) { return 500; }

// Overridden.
async findIpAddressesForNickname({ nickname, maxAge = 30 } = {}) {
if (nickname === 'Xanland') {
Expand All @@ -157,7 +160,7 @@ export class MockBanDatabase extends BanDatabase {
total: 13,
entries: [
{ text: 2657120904, sessions: 122 },
{ text: 5642214798, sessions: 1 },
{ text: 5642214798, common: true, sessions: 1 },
],
};
}
Expand Down

0 comments on commit 2d26cd4

Please sign in to comment.