kulmapaikka-ircbot/app.js
2019-11-20 22:38:03 +02:00

129 lines
3.4 KiB
JavaScript

// General includes
const irc = require('irc')
const path = require('path')
// Load config
const config = require('./config')
const cron = require('node-cron')
// Load new plugins
const PluginManager = require('./plugins/pluginManager.js')
const pluginManager = new PluginManager(config)
pluginManager.loadAllPlugins()
// Configure logger
// create a write stream (in append mode)
const logger = require('./logger')
if (!config.irc.connect) {
logger.info('Not connecting to IRC. Exit')
process.exit()
}
// Start client
var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: false,
channels: config.irc.channels,
// floodProtection: true,
// sasl: true,
millisecondsOfSilenceBeforePingSent: 60000
})
var handleMessage = function (from, to, message) {
logger.info(from + ' => ' + to + ': ' + message)
pluginManager.testPlugins(message).then((results) => {
for (var i = 0; i < results.length; i++) {
logger.info('BOT => ' + results[i].to + ': ' + message)
// TODO handle TO (nick/channel
if (results[i].to === 'nick') {
to = from
}
client.say(to, results[i].message)
}
}, function (error) {
logger.error('Plugin error: ', error)
})
// Op
if (message.match(/^\.op$/i)) {
client.send('NAMES', to)
}
// reload config
if (message.match(/^\.paivita$/i)) {
// Using require for config files is dangerous
// Rewrite this in the future
var filename = path.resolve('./config.js')
delete require.cache[filename]
var config = require('./config')
client.say(to, 'Ladattiin asetustiedosto. ' + Object.keys(config.members).length + ' käyttäjäasetusta. Päivitetään operaattorioikeuksia...')
client.send('NAMES', to)
}
}
client.addListener('message', function (from, to, message) {
if (String(from) !== String(config.irc.nick)) {
handleMessage(from, to, message)
}
})
client.addListener('pm', function (from, message) {
if (String(from) !== String(config.irc.nick)) {
handleMessage(from, from, message)
}
})
client.addListener('join', function (channel, who) {
client.whois(who, function (result) {
console.log(result)
var members = config.members
for (var memberNick in members) {
var member = members[memberNick]
if (typeof result.host !== 'undefined' && typeof result.user !== 'undefined') {
if (result.host.match(member.host) && result.user.match(member.user)) {
client.send('MODE', channel, '+o', result.nick)
}
}
}
})
})
client.addListener('error', function (message) {
console.log('error: ', message)
})
client.addListener('names', function (channel, nicks) {
if (typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') {
// go through nicks
for (var nick in nicks) {
if (nicks[nick] !== '@') {
client.whois(nick, function (result) {
var members = config.members
for (var memberNick in members) {
var member = members[memberNick]
if (result.host.match(member.host) && result.user.match(member.user)) {
client.send('MODE', channel, '+o', result.nick)
}
}
})
}
}
}
})
for (let i = 0; i < config.cron.length; i++) {
let row = config.cron[i]
cron.schedule(row.cron, () => {
if (client.conn !== null) {
for (let c = 0; c < config.irc.channels.length; c++) {
handleMessage('BOT', config.irc.channels[c], row.command)
}
}
})
}