Skip to content

Instantly share code, notes, and snippets.

@prawnsalad
Created January 9, 2017 08:32
Show Gist options
  • Save prawnsalad/a9b74adc567d29f6e6b46055f37f5db9 to your computer and use it in GitHub Desktop.
Save prawnsalad/a9b74adc567d29f6e6b46055f37f5db9 to your computer and use it in GitHub Desktop.
var Irc = require('irc-framework');
/**
* Example middleware structure to handle NickServ authentication
* Accepts a `services` object from the client connect() options
*/
function ServiceX() {
return function(client, raw_events, parsed_events) {
raw_events.use(rawEventHandler);
client.on('connecting', () => {
var services = client.options.services;
if (!services || !services.username) {
return;
}
var modes = [];
modes.push(services.hide_hostmask ? '+x' : '-x');
modes.push(services.login_required ? '+!' : '-!');
client.options.password = modes.join('') + ' ' +
services.username + ' ' +
services.password;
});
};
function rawEventHandler(command, event, raw, client, next) {
if (command === 'IS THERE A AUTH SUCCESS / FAIL COMMAND?') {
client.emit('auth_success');
}
next();
}
}
var bot = new Irc.Client();
bot.use(ServiceX());
bot.connect({
host: '127.0.0.1',
port: 6667,
ssl: false,
nick: 'servicextest',
services: {
username: 'username',
password: 'password',
// Service X specific options below
login_required: true,
hide_hostmask: true,
},
});
bot.on('registered', function() {
console.log('Connected and registered!');
bot.join('#test');
});
bot.on('debug', (line) => console.log('[irc-fw] ' + line));
bot.on('raw', (event) => console.log((event.from_server ? '[s]' : '[c]') + ' ' + event.line));
bot.on('irc error', function(error) {
console.log('[irc error]', error);
});
bot.on('close', function(had_error) {
console.log('Connection close. had_error:', had_error);
});
bot.on('message', function(event) {
var time = event.time ? new Date(event.time) : new Date();
console.log((event.from_server?'server ':'') + time.toString() + ' <' + event.target + '/' + event.nick + '>', event.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment