From 2b408f630dcf6376c49d7889c701b6722486b1d9 Mon Sep 17 00:00:00 2001 From: Cory Chaplin Date: Sun, 19 Mar 2017 10:46:31 +0100 Subject: [PATCH 1/2] Add banlist management --- src/channel.js | 5 +++++ src/client.js | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/channel.js b/src/channel.js index bde7ae31..7207d18b 100644 --- a/src/channel.js +++ b/src/channel.js @@ -17,6 +17,11 @@ function IrcChannel(irc_client, channel_name, key) { // this.action = _.partial(irc_client.action.bind(irc_client), channel_name); this.part = _.partial(irc_client.part.bind(irc_client), channel_name); this.join = _.partial(irc_client.join.bind(irc_client), channel_name); + this.mode = _.partial(irc_client.mode.bind(irc_client), channel_name); + this.banlist = _.partial(irc_client.banlist.bind(irc_client), channel_name); + this.ban = _.partial(irc_client.ban.bind(irc_client), channel_name); + this.unban = _.partial(irc_client.unban.bind(irc_client), channel_name); + this.users = []; irc_client.on('userlist', function(event) { diff --git a/src/client.js b/src/client.js index 0f31a2d8..e0230ca2 100644 --- a/src/client.js +++ b/src/client.js @@ -376,6 +376,33 @@ IrcClient.prototype.part = function(channel, message) { this.raw(raw); }; +IrcClient.prototype.mode = function(channel, mode, arguments) { + var raw = ['MODE', channel, mode]; + + if (arguments) { + raw.push(arguments); + } + + this.raw(raw); +}; + +IrcClient.prototype.banlist = function(channel, mode) { + var raw = ['MODE', channel, 'b']; + + this.raw(raw); +}; + +IrcClient.prototype.ban = function(channel, mask) { + var raw = ['MODE', channel, '+b', mask]; + + this.raw(raw); +}; + +IrcClient.prototype.unban = function(channel, mask) { + var raw = ['MODE', channel, '-b', mask]; + + this.raw(raw); +}; IrcClient.prototype.setTopic = function(channel, newTopic) { this.raw('TOPIC', channel, newTopic); From 30ab6223db90f88b8e1318734c383bc2ac64335f Mon Sep 17 00:00:00 2001 From: Cory Chaplin Date: Sun, 19 Mar 2017 23:11:21 +0100 Subject: [PATCH 2/2] Timed bans example middleware --- examples/timed-bans.js | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 examples/timed-bans.js diff --git a/examples/timed-bans.js b/examples/timed-bans.js new file mode 100644 index 00000000..910a7449 --- /dev/null +++ b/examples/timed-bans.js @@ -0,0 +1,78 @@ +var IRC = require('../'); + +var bot = new IRC.Client(); +bot.use(TimedBanMiddleware()); +bot.connect({ + host: 'irc.network.org', + nick: 'unban-bot', + gecos: 'unban-bot', + account: 'unban-bot', + password: '***' +}); + +// Config +var ignore_extbans = true, // Set true to ignore extended bans + chanlist = ['#testoz', '#testouille'], // List of channels for the bot + ban_ttl = 10800; // Ban expiry duration (in seconds) + + + +function TimedBanMiddleware() { + var channels = []; + + setInterval(getBanlist, 60000); + + return function(client, raw_events, parsed_events) { + parsed_events.use(theMiddleware); + } + + function getBanlist() { + for(channel_index in channels) { + bot.banlist(channels[channel_index].name); + } + } + + function expireBans(event, chan) { + var time = Math.floor(Date.now() / 1000); + + if(ignore_extbans) { + var extban_regex = new RegExp("~?[a-zA-Z]{1}:"); + } + + for(ban_index in event.bans) { + var ban_mask = event.bans[ban_index].banned, + ban_time = event.bans[ban_index].banned_at; + + // Check if ban has expired + if((time - ban_time) > ban_ttl) { + //console.log('EXPIRED:', chan.name, ban_mask); + // Only unban if the ban is not an exception type + if(ignore_extbans === false || (ignore_extbans && !extban_regex.test(ban_mask))) { + chan.unban(ban_mask); + } + } else { + // Ban is still valid, do nothing + //console.log('NOT EXPIRED:', ban_mask); + } + } + } + + + function theMiddleware(command, event, client, next) { + if (command === 'registered') { + for(channel_index in chanlist) { + chan = bot.channel(chanlist[channel_index]); + chan.join(); + channels[chan.name] = chan; + // Collect banlist on join + bot.banlist(channels[chanlist[channel_index]].name); + } + } + + if (command === 'banlist') { + expireBans(event, channels[event.channel]); + } + + next(); + } +}