Skip to content

Commit

Permalink
Add options to nickserv plugin; don't broadcast to unjoined channels
Browse files Browse the repository at this point in the history
  • Loading branch information
saltire committed Jul 8, 2015
1 parent bbfd073 commit 9cd5784
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
3 changes: 3 additions & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"networks": {
"oftc": {
"password": "password",
"success": "You are successfully identified",
"channels": [
"#torontocrypto"
],
Expand All @@ -70,7 +71,9 @@
}
},
"i2p": {
"nick_first": true,
"password": "password",
"success": "You are now identified",
"channels": [
"#torontocrypto"
]
Expand Down
3 changes: 3 additions & 0 deletions config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ plugins:
networks:
oftc:
password: password
success: 'You are successfully identified'
channels:
- '#torontocrypto'
channel_keywords:
'#torontocrypto': keyword
i2p:
nick_first: true
password: password
success: 'You are now identified'
channels:
- '#torontocrypto'
rss:
Expand Down
33 changes: 18 additions & 15 deletions plugins/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
module.exports = function (bot) {
module.exports = function () {
function broadcastToTargets(msg, targets) {
targets.forEach(function (target) {
// Don't send to the same target the message came from.
if (target.network == msg.network && target.channel == msg.replyto) return;
// Don't send to channels we aren't currently in.
if (!(target.channel in this.networks[target.network].channels)) return;

var identifier = (network != msg.network ? (msg.network + ':') : '') + msg.replyto;
bot.say(target.network, target.channel, '<%s> [%s]', identifier, msg.nick, msg.text);
});
var identifier = (target.network != msg.network ? (msg.network + ':') : '') + msg.replyto;
this.say(target.network, target.channel, '<%s> [%s]', identifier, msg.nick, msg.text);
}, this);
}

bot.listen('.*', function (msg) {
if (bot.config.get('plugins.broadcast.broadcast_all')) {
// build a set of targets from all joined channels in all connected networks
this.listen('.*', function (msg) {
if (this.config.get('plugins.broadcast.broadcast_all')) {
// Build a set of targets from all joined channels in all connected networks.
var targets = [];
for (network in bot.networks) {
for (channel in bot.networks[network].channels) {
for (var network in this.networks) {
for (var channel in this.networks[network].channels) {
targets.push({
network: network,
channel: channel,
});
}
}
broadcastToTargets(msg, targets);
broadcastToTargets.call(this, msg, targets);
}
else {
// iterate through the configured sets of targets that will be broadcast to each other
bot.config.get('plugins.broadcast.target_sets', []).forEach(function (targets) {
// check that the received message's target is in this set
// Iterate through the configured sets of targets that will be broadcast to each other.
this.config.get('plugins.broadcast.target_sets', []).forEach(function (targets) {
// Check that the received message's target is in this set.
if (targets.some(function (target) {
return target.network == msg.network && target.channel == msg.replyto;
})) {
broadcastToTargets(msg, targets);
broadcastToTargets.call(this, msg, targets);
}
});
}, this);
}
});
};
28 changes: 22 additions & 6 deletions plugins/nickserv.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
var extend = require('extend');


var waitingForNickServ = {};

var default_opts = {
nick_first: false,
password: '',
success: 'You are successfully identified',
channels: [],
channel_keywords: {}
};


// Listener for NickServe notice events.
function checkForIdentifySuccess(network, nick, target, text, msg) {
var nickserv_opts = extend({}, default_opts, this.config.get('plugins.nickserv.networks.' + network, {}));

if ((waitingForNickServ[network]) &&
nick == 'NickServ' &&
target == this.networks[network].config.nick &&
text.indexOf('You are successfully identified') > -1) {

var nickserv_opts = this.config.get('plugins.nickserv.networks.' + network, {});
text.match(nickserv_opts.success)) {

// Join all NickServ-only channels on this network.
if (Array.isArray(nickserv_opts.channels) && nickserv_opts.channels.length) {
this.log('%s: Identified with NickServ, joining %d NickServ-only channel%s.',
network, nickserv_opts.channels.length, nickserv_opts.channels.length == 1 ? '' : 's');
nickserv_opts.channels.forEach(function (channel) {
if (channel in nickserv_opts.channel_keywords) {
if (channel in (nickserv_opts.channel_keywords || {})) {
channel += ' ' + nickserv_opts.channel_keywords[channel];
}
this.join(network, channel);
Expand All @@ -34,10 +45,15 @@ function checkForIdentifySuccess(network, nick, target, text, msg) {
module.exports = function () {
this.on('registered', function (network, msg) {
var nick = this.networks[network].config.nick;
var nickserv_opts = this.config.get('plugins.nickserv.networks.' + network, {});
var nickserv_opts = extend({}, default_opts, this.config.get('plugins.nickserv.networks.' + network, {}));
if (nickserv_opts.password) {
this.log('%s: Received welcome message from %s. Sending IDENTIFY to NickServ...', network, msg.server);
this.say(network, 'NickServ', 'IDENTIFY', nickserv_opts.password, nick);
if (nickserv_opts.nick_first) {
this.say(network, 'NickServ', 'IDENTIFY', nick, nickserv_opts.password);
}
else {
this.say(network, 'NickServ', 'IDENTIFY', nickserv_opts.password, nick);
}

// Listen for a notice event on this network.
if (Array.isArray(nickserv_opts.channels) && nickserv_opts.channels.length) {
Expand Down

0 comments on commit 9cd5784

Please sign in to comment.