148 lines
3.8 KiB
JavaScript
148 lines
3.8 KiB
JavaScript
// General includes
|
|
const irc = require('irc');
|
|
const fs = require('fs');
|
|
const winston = require('winston');
|
|
require('winston-daily-rotate-file');
|
|
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 logDirectory = __dirname + '/log';
|
|
// ensure log directory exists
|
|
if(!fs.existsSync(logDirectory)) {
|
|
fs.mkdirSync(logDirectory);
|
|
}
|
|
|
|
var logger = new (winston.Logger)({
|
|
transports: [
|
|
new (winston.transports.Console)(),
|
|
new (winston.transports.DailyRotateFile)({
|
|
filename: logDirectory + '/ircbot.log',
|
|
datePattern: 'YYYY-MM-DD',
|
|
maxsize: 20000
|
|
})
|
|
]
|
|
});
|
|
|
|
if(!config.irc.connect) {
|
|
logger.info('Not connecting to IRC. Exit');
|
|
return;
|
|
}
|
|
|
|
var isConnected = false
|
|
// 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(function(results) {
|
|
for(var i = 0; i < results.length; i++) {
|
|
client.say(to, results[i]);
|
|
}
|
|
}, function(error) {
|
|
logger.error('Plugin error: ', error);
|
|
});
|
|
|
|
var res = null;
|
|
|
|
// 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];
|
|
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]
|
|
let that = this;
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
}
|