Last active
September 22, 2017 17:07
-
-
Save ebith/c3ef78558480831a30f665d8fe29d13a to your computer and use it in GitHub Desktop.
通知したり読み上げたりするIRC bot (node limoo.js --port 6667 --host irc.foobar.baz --password ****** --name qux --ssl --insecure)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const argv = require('minimist')(process.argv.slice(2)); | |
const net = argv.ssl ? require('tls') : require('net'); | |
const irc = require('slate-irc'); | |
const dns = require('dns'); | |
const exec = require('child_process').exec; | |
const growl = require('growly'); | |
const growlIcon = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAB0VBMVEUAAAD////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////P+BsGAAAAmnRSTlMAAQIDBAUGBwgJCgsMDRARExQVFhcYGhscHR4fICEiIyQlJicpKissLjEyNDc4Ozw/QUJDREVGR0lKS0xNTk9QUVJUVVZYXV5fYmNmaGtsbW9wc3V3eXt8fn+AgoWGiImLjI6PkZKVl5iam52go6aoqqutr7CytLW3ubq8vsDBxcfIyszR09XX2drc4OTm6Onr7e/x8/X3+fv9dOPsjgAAAztJREFUGBntweFDE2UcB/Dvc3dABiWDnFhZqRWJ1aLUJNCZmilKWVqEFdrQEikDYxnQEEc41HA6uK37/rX9nnvu1ogNdvSiN34+eKI61fbB5UWukrvUE0N9rM4rLqtyR163sJG2iyWuozgUw3pemeaGpl5GLe1pli0NH+6MNdkKUHZTrPPw8BLLJttRjXWOAe9a1xassaVr1GPgUwtrbM3RyOyzUYO9L0MjtxX/0lWibzSOdW0fpa/UhVUO0He7AxvaMUffflR4n5p3CHXp8agdRNkeaosx1KntHrXdCLS4FBkHdXNmKdxmGFMU0zYicDIUv8J3iGLRRiTOfYr3IKzHJL0YImr3SOYtAEmKY4jsBEUvgEWSeQuRWXmSd4F2il5UsFHBRiUbFfoo2tBL0YxQw1dFFvoVfKq/wOKgA0P1F1gcdBBqoejBVZILCDXkqKUVhEpTyzVAU2lqCw5CCyRHcIdkCqEhGkcgkjS+hpakMYjQCMk55EkOIFSicQciS+MvBZGlUUToE5IPsULyJAIOAysQLgMOhMuAjcApkkX8QfI8QgUa0xAzNJahzdAoIDRE8gFukhxH6AyNtyESNM5CS9DoR2iC5CTOkvQUAuo3at/Bl6J2S8GXopZWCFgeyc/xKsUuhFRy3p1JIJCYcbNJhUBixs0mFUJ7KPbC8UjewCZMUDQA31JsR2QvUowB2EYxrxCRylK8APE9xQVE9A3FL9CeKlEcQCRJCu9Z+LqpHUQEH1I7hsBFaqdRtzPUxhBSN6mNN6IuTZPU0gpl1ji15W7U4Z3H1GZtVFA/0JeJYwM75+j72cFqx2mkmlCbejNL4zOs0XGXvtdQg9r55TKN/F5UoT72KBxU0RDvu77C0KCD6v4keQvGW15u4vK50x+dHPgiNVtkhR+fQw0dFH3wnWANqVbUdIGiGUJdYlW/v9uI2uwiyVmIZzJc6+FwohnrSlJ0A9hforYwOn3vUcl9MD91faD7paexkcYVknkL2zL0XbUQzRhFb+sVGkcQ0VFqt2nc34GI3mAF75SFiJ73+I8bLYiq1WXZtTgic5YYKJ1vwSbspq8wuMvCplhZPvrpeBz/gY0n/kd/A4UDS1SaR9fhAAAAAElFTkSuQmCC', 'base64'); | |
const log = require('util').log; | |
if (argv.insecure) { process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; } | |
class Limoo { | |
constructor(config) { | |
return this.connect(config); | |
} | |
connect(config) { | |
const stream = net.connect({ | |
port: config.port, | |
host: config.host | |
}); | |
const client = irc(stream); | |
if (config.password) { | |
client.pass(config.password); | |
} | |
client.nick(config.name); | |
client.user(config.name, config.name); | |
client.once('welcome', (event) => { | |
this.keepAlive = setInterval(() => { | |
client.stream.write(`PING ${config.host}\r\n`); | |
}, 30 * 1000); | |
}); | |
// スリープから戻ったりした時に接続し直す | |
client.stream.on('close', () => { | |
const isOnline = setInterval(() => { | |
dns.lookup('google.com', (err, address, family) => { | |
if (!err) { | |
log('reconnecting'); | |
clearInterval(this.keepAlive); | |
clearInterval(isOnline); | |
this.connect(config); | |
} | |
}); | |
}, 10 * 1000); | |
}); | |
client.stream.on('error', (err) => { | |
log(err.message); | |
}); | |
client.on('message', (msg) => { // from: nick, to: channel | |
if (msg.message.includes('\u0001') || msg.from.includes('ebith')) { return; } | |
// Say | |
if (msg.to.includes('#おいら屋ファクトリー')) { | |
msg.message = msg.message.replace(/['`"\\]/g, ''); | |
exec(`say -v Kyoko "[[volm 0.5]] ${msg.message.replace(/((http|ftp)s?:\/\/\S+)/, 'URL省略')}"`); | |
} | |
// Growl | |
if (!msg.to.includes('#urls')) { | |
growl.notify(msg.message, {title: `${msg.from} ${msg.to}`, icon: growlIcon }); | |
} | |
}); | |
return client; | |
} | |
} | |
new Limoo(argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment