kulmapaikka-ircbot/app.js

129 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-09-01 22:44:55 +03:00
// General includes
const irc = require('irc')
const path = require('path')
2018-04-29 17:37:07 +03:00
2015-09-01 22:44:55 +03:00
// 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()
2015-09-02 21:13:39 +03:00
// Configure logger
// create a write stream (in append mode)
const logger = require('./logger')
2015-09-02 21:13:39 +03:00
if (!config.irc.connect) {
logger.info('Not connecting to IRC. Exit')
process.exit()
}
2015-09-01 22:44:55 +03:00
// Start client
var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: false,
2018-04-29 17:37:07 +03:00
channels: config.irc.channels,
// floodProtection: true,
// sasl: true,
millisecondsOfSilenceBeforePingSent: 60000
})
2015-07-17 01:12:59 +03:00
var handleMessage = function (from, to, message) {
logger.info(from + ' => ' + to + ': ' + message)
2018-04-29 17:37:07 +03:00
2019-11-20 22:38:03 +02:00
pluginManager.testPlugins(message).then((results) => {
for (var i = 0; i < results.length; i++) {
2019-11-20 22:38:03 +02:00
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)
2018-04-29 17:37:07 +03:00
}
}, function (error) {
logger.error('Plugin error: ', error)
})
2018-04-29 17:37:07 +03:00
// Op
if (message.match(/^\.op$/i)) {
client.send('NAMES', to)
2018-04-29 17:37:07 +03:00
}
// 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)
2018-04-29 17:37:07 +03:00
}
}
2015-07-17 01:12:59 +03:00
2015-09-01 22:44:55 +03:00
client.addListener('message', function (from, to, message) {
if (String(from) !== String(config.irc.nick)) {
handleMessage(from, to, message)
2018-04-29 17:37:07 +03:00
}
})
2015-07-17 01:12:59 +03:00
2015-09-01 22:44:55 +03:00
client.addListener('pm', function (from, message) {
if (String(from) !== String(config.irc.nick)) {
handleMessage(from, from, message)
2018-04-29 17:37:07 +03:00
}
})
2015-06-20 15:08:25 +03:00
client.addListener('join', function (channel, who) {
client.whois(who, function (result) {
console.log(result)
2015-06-20 15:08:25 +03:00
var members = config.members
for (var memberNick in members) {
var member = members[memberNick]
2015-06-20 15:08:25 +03:00
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)
}
2018-04-29 17:37:07 +03:00
}
}
})
})
2015-06-20 15:08:25 +03:00
client.addListener('error', function (message) {
console.log('error: ', message)
})
2015-06-20 15:08:25 +03:00
client.addListener('names', function (channel, nicks) {
if (typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') {
2018-04-29 17:37:07 +03:00
// 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)
2018-04-29 17:37:07 +03:00
}
}
})
2018-04-29 17:37:07 +03:00
}
}
}
})
2018-10-27 14:50:31 +03:00
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)
2018-10-27 14:50:31 +03:00
}
}
})
2018-10-27 14:50:31 +03:00
}