Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support CLIENTTAGDENY #366

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/commands/handlers/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const handlers = {
handler.network.options.CHANMODES = option[1].split(',');
} else if (option[0] === 'CASEMAPPING') {
handler.network.options.CASEMAPPING = option[1];
// https://ircv3.net/specs/extensions/message-tags#rpl_isupport-tokens
} else if (option[0] === 'CLIENTTAGDENY' && handler.network.cap.isEnabled('message-tags')) {
handler.network.options.CLIENTTAGDENY = option[1].split(',');
} else if (option[0] === 'NETWORK') {
handler.network.name = option[1];
} else if (option[0] === 'NAMESX' && !handler.network.cap.isEnabled('multi-prefix')) {
Expand Down
44 changes: 44 additions & 0 deletions src/messagetags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports.decodeValue = decodeValue;
module.exports.encodeValue = encodeValue;
module.exports.decode = decode;
module.exports.encode = encode;
module.exports.parseDenylist = parseDenylist;
module.exports.isBlocked = isBlocked;

const tokens_map = {
'\\\\': '\\',
Expand Down Expand Up @@ -68,3 +70,45 @@ function encode(tags, separator = ';') {

return parts.join(separator);
}

// Parses a raw CLIENTTAGDENY= denylist
// into a { allBlockedByDefault: boolean, explicitlyAccepted: string[], explicitlyDenied: string[] }
// structure.
function parseDenylist(raw) {
const denylist = {
allBlockedByDefault: false,
explicitlyAccepted: [],
explicitlyDenied: []
};
const parts = raw.split(',');

for (let idx = 0; idx < parts.length; idx++) {
const tag = parts[idx];
if (tag === '') {
continue;
}

if (tag === '*') {
denylist.allBlockedByDefault = true;
continue;
}

if (tag[0] === '-') {
denylist.explicitlyAccepted.push(tag.slice(1));
} else {
denylist.explicitlyDenied.push(tag);
}
}

return denylist;
}

// Takes a parsed denylist and returns whether tag is allowed
// according to current denial policies.
function isBlocked(denylist, tag) {
if (denylist.allBlockedByDefault) {
return !denylist.explicitlyAccepted.includes(tag);
} else {
return denylist.explicitlyDenied.includes(tag);
}
}
3 changes: 2 additions & 1 deletion src/networkinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function NetworkInfo() {
{ symbol: '@', mode: 'o' },
{ symbol: '%', mode: 'h' },
{ symbol: '+', mode: 'v' }
]
],
CLIENTTAGDENY: []
};

this.time_offsets = [];
Expand Down
43 changes: 43 additions & 0 deletions test/messagetags.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,49 @@ const assert = chai.assert;
chai.use(require('chai-subset'));

describe('src/messagetags.js', function() {
describe('CLIENTTAGDENY= parsing', function() {
it('should parse CLIENTTAGDENY=', function() {
assert.deepEqual(MessageTags.parseDenylist(''), {
allBlockedByDefault: false,
explicitlyDenied: [],
explicitlyAccepted: []
});
});

it('should parse CLIENTTAGDENY=*,-a', function() {
assert.deepEqual(MessageTags.parseDenylist('*,-a'), {
allBlockedByDefault: true,
explicitlyAccepted: ['a'],
explicitlyDenied: []
});
});

it('should parse CLIENTTAGDENY=a,b', function() {
assert.deepEqual(MessageTags.parseDenylist('a,b'), {
allBlockedByDefault: false,
explicitlyAccepted: [],
explicitlyDenied: ['a', 'b']
});
});
});

describe('CLIENTTAGDENY= logic', function() {
it('should block all tags (`b`) with * and no exception', function() {
assert.isTrue(MessageTags.isBlocked(MessageTags.parseDenylist('*'), 'b'));
});

it('should not block all tags with * and exceptions (`c`, `a`)', function() {
assert.isFalse(MessageTags.isBlocked(MessageTags.parseDenylist('*,-c,-a'), 'a'));
assert.isFalse(MessageTags.isBlocked(MessageTags.parseDenylist('*,-c,-a'), 'c'));
assert.isTrue(MessageTags.isBlocked(MessageTags.parseDenylist('*,-c,-a'), 'b'));
});

it('should block a specific tag if no * is present', function() {
assert.isTrue(MessageTags.isBlocked(MessageTags.parseDenylist('a'), 'a'));
assert.isFalse(MessageTags.isBlocked(MessageTags.parseDenylist('a'), 'b'));
});
});

describe('value encoding', function() {
it('should decode characters to correct strings', function() {
const plain = "Some people use IRC; others don't \\o/ Note: Use IRC\r\n";
Expand Down
30 changes: 30 additions & 0 deletions test/networkinfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,35 @@ describe('src/networkinfo.js', function() {
const results = names.map(name => client.network.isChannelName(name));
assert.deepEqual(results, [false, false, false, false, false, false]);
});

it('should parse CLIENTTAGDENY= as a list', function() {
const client = newMockClient();
client.dispatch({
command: '005',
params: ['nick', 'CLIENTTAGDENY='],
tags: []
});
assert.isEmpty(client.network.options.CLIENTTAGDENY);
});

it('should parse CLIENTTAGDENY=*,-a,-b as a list', function() {
const client = newMockClient();
client.dispatch({
command: '005',
params: ['nick', 'CLIENTTAGDENY=*,-a,-b'],
tags: []
});
assert.equal(client.network.options.CLIENTTAGDENY, ['*', '-a', '-b']);
});
});

it('should parse CLIENTTAGDENY=a,b,c as a list', function() {
const client = newMockClient();
client.dispatch({
command: '005',
params: ['nick', 'CLIENTTAGDENY=a,b,c'],
tags: []
});
assert.equal(client.network.options.CLIENTTAGDENY, ['a', 'b', 'c']);
});
});