Skip to content

Feature timed ban example #90

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
78 changes: 78 additions & 0 deletions examples/timed-bans.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
5 changes: 5 additions & 0 deletions src/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down