2015-09-01 22:44:55 +03:00
|
|
|
// General includes
|
2015-06-20 03:04:20 +03:00
|
|
|
var irc = require('irc');
|
2015-09-02 21:13:39 +03:00
|
|
|
var fs = require('fs');
|
|
|
|
var winston = require('winston');
|
|
|
|
var path = require('path');
|
|
|
|
|
2015-06-20 15:08:25 +03:00
|
|
|
var iconCodeToText = {
|
|
|
|
'01d': 'selkeää',
|
|
|
|
'02d': 'puolipilvistä',
|
|
|
|
'03d': 'pilvistä',
|
|
|
|
'04d': 'pilvipeite rakoilee',
|
|
|
|
'09d': 'sadekuuroja',
|
|
|
|
'10d': 'vesisadetta',
|
|
|
|
'11d': 'ukkostaa',
|
|
|
|
'13d': 'lumisadetta',
|
|
|
|
'50d': 'sumua',
|
|
|
|
'01n': 'selkeää',
|
|
|
|
'02n': 'puolipilvistä',
|
|
|
|
'03n': 'pilvistä',
|
|
|
|
'04n': 'pilvipeite rakoilee',
|
|
|
|
'09n': 'sadekuuroja',
|
|
|
|
'10n': 'vesisadetta',
|
|
|
|
'11n': 'ukkostaa',
|
|
|
|
'13n': 'lumisadetta',
|
|
|
|
'50n': 'sumua'
|
|
|
|
};
|
2015-09-01 22:44:55 +03:00
|
|
|
// Load config
|
|
|
|
var config = require('./config');
|
2015-06-20 15:08:25 +03:00
|
|
|
|
2017-03-12 18:36:32 +02:00
|
|
|
|
|
|
|
// Load new plugins
|
|
|
|
var PluginManager = require('./plugins/pluginManager.js');
|
|
|
|
var pluginManager = new PluginManager(config);
|
|
|
|
pluginManager.loadAllPlugins();
|
|
|
|
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Load plugins
|
2015-10-02 20:14:31 +03:00
|
|
|
var googletin = require('./googleapi.js')(config);
|
|
|
|
var owmapi = require('./owmapi')(config);
|
|
|
|
var getTemperatureString = require('./plugins/getWeather')(config);
|
2015-09-01 22:44:55 +03:00
|
|
|
var getDnDCharacter = require('./plugins/getCharacter')();
|
2017-03-12 00:06:38 +02:00
|
|
|
var getDnDCharacter2 = require('./plugins/getCharacter2')();
|
2015-09-01 22:44:55 +03:00
|
|
|
var getDiceString = require('./plugins/getDice')();
|
|
|
|
var getUrlString = require('./plugins/getUrl')();
|
2015-10-02 20:14:31 +03:00
|
|
|
var getTweet = require('./plugins/getTweet')(config);
|
2015-11-22 12:01:33 +02:00
|
|
|
var getInstantAnswer = require('./plugins/getInstantAnswer')(config);
|
2015-07-17 01:12:59 +03:00
|
|
|
|
2015-09-02 21:13:39 +03:00
|
|
|
// Configure logger
|
|
|
|
// create a write stream (in append mode)
|
|
|
|
var 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
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-03-12 18:36:32 +02:00
|
|
|
if(!config.irc.connect) {
|
|
|
|
logger.info('Not connecting to IRC. Exit');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Start client
|
|
|
|
var client = new irc.Client(config.irc.server, config.irc.nick, {
|
|
|
|
debug: true,
|
|
|
|
channels: config.irc.channels,
|
|
|
|
});
|
2015-07-17 01:12:59 +03:00
|
|
|
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
var handleMessage = function(from, to, message) {
|
2015-09-02 21:13:39 +03:00
|
|
|
logger.info(from + ' => ' + to + ': ' + message);
|
2017-03-12 18:36:32 +02:00
|
|
|
|
|
|
|
pluginManager.testPlugins(message).then(function(results) {
|
|
|
|
for(var i = 0; i < results.length; i++) {
|
|
|
|
client.say(to, results[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
//if (to.match(/^[#&]/)) {
|
|
|
|
// channel message
|
|
|
|
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
|
|
|
|
getTemperatureString(config.owmCityIds).then(function(str) {
|
|
|
|
client.say(to, 'Sää: ' + str);
|
|
|
|
});
|
2015-07-17 01:12:59 +03:00
|
|
|
}
|
2015-06-20 03:04:20 +03:00
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
var res = null;
|
|
|
|
|
|
|
|
// Weather
|
|
|
|
if (res = message.match(/^\.s(ää|aa) ([^0-9]+)$/i)) {
|
|
|
|
//console.log('kaupunki', res[1]);
|
|
|
|
owmapi.getTemperature(res[2]).then(function(result) {
|
|
|
|
//console.log(result);
|
|
|
|
if(result.cod === '404') {
|
|
|
|
//console.log('Eioo: ' + res[1]);
|
|
|
|
client.say(to, 'Eioo: ' + res[2]);
|
|
|
|
} else {
|
|
|
|
var city = result.name;
|
|
|
|
var country = result.sys.country;
|
|
|
|
var temp = (result.main.temp-273.15).toFixed(1);
|
|
|
|
var code = result.weather[0].icon;
|
|
|
|
var sunriseDate = new Date(result.sys.sunrise*1000);
|
|
|
|
var sunsetDate = new Date(result.sys.sunset*1000);
|
|
|
|
|
|
|
|
var sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes();
|
|
|
|
var sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes();
|
|
|
|
|
|
|
|
var humidity = result.main.humidity;
|
|
|
|
var pressure = result.main.pressure;
|
|
|
|
|
|
|
|
var windspeed = result.wind.speed;
|
|
|
|
|
|
|
|
client.say(to, 'Sää: '+city+', '+country+' ('+temp+' °C, '+ iconCodeToText[code] +', ilmankosteus: '+humidity+' %, ilmanpaine: '+pressure+' hPa, tuulen nopeus: '+windspeed+' m/s)');
|
2015-06-20 03:04:20 +03:00
|
|
|
}
|
2015-09-01 22:44:55 +03:00
|
|
|
}, function(error) {
|
|
|
|
console.log('ERROR ',error);
|
|
|
|
client.say(to, 'Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
|
2015-06-20 03:04:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Create Character
|
|
|
|
if (res = message.match(/^\.luohahmo$/i)) {
|
|
|
|
var character = getDnDCharacter();
|
|
|
|
client.say(to, ''+character);
|
2015-06-20 03:04:20 +03:00
|
|
|
}
|
2017-03-12 00:06:38 +02:00
|
|
|
|
|
|
|
// Create Character
|
|
|
|
if (res = message.match(/^\.luohahmo2$/i)) {
|
|
|
|
var character = getDnDCharacter2();
|
|
|
|
client.say(to, ''+character);
|
|
|
|
}
|
2015-11-22 12:01:33 +02:00
|
|
|
// Instant Answer
|
|
|
|
if (res = message.match(/^\.wtf (.+)$/i)) {
|
|
|
|
getInstantAnswer(res[1]).then(function(answer) {
|
|
|
|
client.say(to, '' + answer);
|
|
|
|
})
|
|
|
|
}
|
2016-05-22 00:24:02 +03:00
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Throw dice
|
|
|
|
if (res = message.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
|
|
|
|
var th = getDiceString(res[2], res[3]);
|
|
|
|
client.say(to, 'Heitettiin: '+th);
|
|
|
|
}
|
|
|
|
if (message.match(/^\.heit(a|ä) voltti homo$/i)) {
|
|
|
|
client.say(to, 'steam://run/1250');
|
2015-06-20 03:04:20 +03:00
|
|
|
}
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Op
|
|
|
|
if (message.match(/^\.op$/i)) {
|
|
|
|
client.send('NAMES', to);
|
|
|
|
}
|
2015-06-20 15:08:25 +03:00
|
|
|
|
2015-09-02 21:13:39 +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];
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
// Url specific handling
|
2016-05-22 00:24:02 +03:00
|
|
|
if(res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?([a-zA-Z0-9_=#-]+&)*v=([a-zA-Z0-9_-]+)))/)) {
|
2015-09-01 22:44:55 +03:00
|
|
|
console.log(res);
|
|
|
|
var id = res[7];
|
|
|
|
if(typeof id === 'undefined') {
|
|
|
|
id = res[5];
|
2015-07-17 01:12:59 +03:00
|
|
|
}
|
2015-09-01 22:44:55 +03:00
|
|
|
if(typeof id !== 'undefined') {
|
2015-07-17 01:12:59 +03:00
|
|
|
googletin.getYoutubeVideoById(id).then(function(result) {
|
|
|
|
var resultString = 'Youtube: ';
|
|
|
|
resultString += result.title;
|
|
|
|
resultString += ', esittäjänä ' + result.channelTitle;
|
|
|
|
var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2);
|
|
|
|
resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )';
|
|
|
|
client.say(to, ''+resultString);
|
|
|
|
}, function(err) {
|
|
|
|
console.log('error', err);
|
|
|
|
client.say(to, 'Youtube: jokin meni pieleen...');
|
|
|
|
});
|
|
|
|
}
|
2015-09-01 22:44:55 +03:00
|
|
|
} else if (res = message.match(/^\.youtube (.+)$/i)) {
|
|
|
|
|
|
|
|
console.log(res);
|
|
|
|
googletin.getYoutubeVideo(res[1]).then(function(result) {
|
|
|
|
var resultString = 'Youtube: ';
|
|
|
|
resultString += result.title;
|
|
|
|
resultString += ', esittäjänä ' + result.channelTitle;
|
|
|
|
|
|
|
|
var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2);
|
|
|
|
|
|
|
|
//resultString += ' ('+result.duration+')';
|
|
|
|
resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )';
|
|
|
|
//console.log(resultString);
|
|
|
|
client.say(to, ''+resultString);
|
|
|
|
}, function(err) {
|
|
|
|
console.log('error');
|
|
|
|
client.say(to, 'Youtube: jokin meni pieleen...');
|
|
|
|
});
|
2015-10-02 20:14:31 +03:00
|
|
|
} else if (res = message.match(/http(s)?:\/\/(www\.)?((twitter\.com\/([a-zA-Z0-9_-]*)\/status\/([a-zA-Z0-9_-]*)))/)) {
|
|
|
|
var status = res[6];
|
|
|
|
getTweet(status).then(function(tweet) {
|
|
|
|
client.say(to, 'Twiitti: ' + tweet);
|
|
|
|
});
|
2015-09-01 22:44:55 +03:00
|
|
|
} else if (res = message.match(/http(s)?:\/\/([^ ]*)/i)) {
|
2015-10-02 20:14:31 +03:00
|
|
|
// Not liked. Disabled for now
|
|
|
|
/*var url = res[0];
|
2015-09-02 21:13:39 +03:00
|
|
|
console.log('Get url: ' + url);
|
2015-09-01 22:44:55 +03:00
|
|
|
getUrlString(url).then(function(str) {
|
|
|
|
client.say(to, 'Otsikko: ' + str);
|
2015-10-02 20:14:31 +03:00
|
|
|
});*/
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
} else {
|
|
|
|
// Do nothing? :D
|
|
|
|
}
|
2015-07-17 01:12:59 +03:00
|
|
|
|
2015-09-01 22:44:55 +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);
|
|
|
|
}
|
|
|
|
});
|
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);
|
2015-06-20 03:04:20 +03:00
|
|
|
}
|
|
|
|
});
|
2015-06-20 15:08:25 +03:00
|
|
|
|
2015-06-20 03:04:20 +03:00
|
|
|
client.addListener('join', function(channel, who) {
|
2015-06-20 15:08:25 +03:00
|
|
|
client.whois(who, function(result) {
|
|
|
|
console.log(result);
|
|
|
|
|
2015-09-01 22:44:55 +03:00
|
|
|
var members = config.members;
|
2015-06-20 15:08:25 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2015-06-20 03:04:20 +03:00
|
|
|
});
|
|
|
|
|
2015-06-20 15:08:25 +03:00
|
|
|
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] === '@') {
|
2015-07-17 01:12:59 +03:00
|
|
|
// go through nicks
|
|
|
|
for(var nick in nicks) {
|
|
|
|
if(nicks[nick] !== '@') {
|
|
|
|
client.whois(nick, function(result) {
|
2015-09-01 22:44:55 +03:00
|
|
|
var members = config.members;
|
2015-07-17 01:12:59 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-06-20 15:08:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|