2013-05-25 21:13:35 +02:00
|
|
|
/**
|
|
|
|
* Name: Log
|
|
|
|
* Description: Log commands to a channel.
|
|
|
|
*/
|
2013-09-08 12:51:21 +02:00
|
|
|
var _ = require('underscore')._,
|
2016-12-14 22:58:42 +01:00
|
|
|
process = require('process');
|
2013-05-25 21:13:35 +02:00
|
|
|
|
|
|
|
var log = function(dbot) {
|
2016-12-14 22:58:42 +01:00
|
|
|
this.ignoredCommands = [];
|
2013-08-18 19:42:23 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
this.api = {
|
|
|
|
'log': function(server, user, message) {
|
2016-12-14 23:09:19 +01:00
|
|
|
this.api.logWithChannel(server, user, 'nochan', message);
|
|
|
|
},
|
|
|
|
|
|
|
|
'logWithChannel': function(server, user, channel, message) { // muh legacy
|
2016-12-14 22:58:42 +01:00
|
|
|
var logChannel = this.config.logChannel[server];
|
|
|
|
dbot.say(server, logChannel, dbot.t('log_message', {
|
|
|
|
'time': new Date().toUTCString(),
|
|
|
|
'command': message,
|
2016-12-14 23:09:19 +01:00
|
|
|
'channel': channel,
|
2016-12-14 22:58:42 +01:00
|
|
|
'user': user
|
|
|
|
}));
|
|
|
|
},
|
2013-08-18 19:42:23 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
'logError': function(server, err) {
|
|
|
|
var stack = err.stack.split('\n').slice(1, dbot.config.debugLevel + 1),
|
|
|
|
logChannel = this.config.logChannel[server],
|
|
|
|
time = new Date().toUTCString();
|
2013-09-08 12:51:21 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
dbot.say(server, logChannel, dbot.t('error_message', {
|
|
|
|
'time': time,
|
|
|
|
'error': 'Message: ' + err
|
|
|
|
}));
|
2013-09-08 12:51:21 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
_.each(stack, function(stackLine, index) {
|
|
|
|
dbot.say(server, logChannel, dbot.t('error_message', {
|
|
|
|
'time': time,
|
|
|
|
'error': 'Stack[' + index + ']: ' +
|
|
|
|
stackLine.trim()
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
2013-09-08 12:51:21 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
'ignoreCommand': function(commandName) {
|
|
|
|
this.ignoredCommands.push(commandName);
|
|
|
|
}
|
|
|
|
};
|
2013-08-18 18:37:30 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
this.onLoad = function() {
|
|
|
|
dbot.api.event.addHook('command', function(event) {
|
|
|
|
var logChannel = this.config.logChannel[event.server];
|
|
|
|
channel = event.channel.name || 'PM';
|
|
|
|
if(logChannel && !_.include(this.ignoredCommands, event.message.split(' ')[0])) {
|
|
|
|
dbot.say(event.server, logChannel, dbot.t('log_message', {
|
|
|
|
'time': new Date().toUTCString(),
|
|
|
|
'channel': channel,
|
|
|
|
'command': event.message,
|
|
|
|
'user': event.user
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-09-08 12:51:21 +02:00
|
|
|
|
2016-12-14 22:58:42 +01:00
|
|
|
process.on('uncaughtException', function(err) {
|
|
|
|
console.log(err);
|
|
|
|
_.each(this.config.logChannel, function(chan, server) {
|
|
|
|
this.api.logError(server, err);
|
|
|
|
}, this);
|
|
|
|
process.exit(1);
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
2013-05-25 21:13:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2016-12-14 22:58:42 +01:00
|
|
|
return new log(dbot);
|
2013-05-25 21:13:35 +02:00
|
|
|
};
|