You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/events.js

140 lines
3.6 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var fs = require('fs'),
winston = require('winston'),
11 years ago
path = require('path'),
nconf = require('nconf'),
user = require('./user');
(function(events) {
11 years ago
var logFileName = 'logs/events.log';
11 years ago
events.logPasswordChange = function(uid) {
11 years ago
events.logWithUser(uid, 'changed password');
};
events.logAdminChangeUserPassword = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'changed password of', callback);
};
events.logAdminUserDelete = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'deleted', callback);
};
11 years ago
function logAdminEvent(adminUid, theirUid, message, callback) {
11 years ago
user.getMultipleUserFields([adminUid, theirUid], ['username'], function(err, userData) {
if(err) {
return winston.error('Error logging event. ' + err.message);
}
var msg = userData[0].username + '(uid ' + adminUid + ') ' + message + ' ' + userData[1].username + '(uid ' + theirUid + ')';
events.log(msg, callback);
11 years ago
});
}
11 years ago
events.logPasswordReset = function(uid) {
11 years ago
events.logWithUser(uid, 'reset password');
};
11 years ago
events.logEmailChange = function(uid, oldEmail, newEmail) {
11 years ago
events.logWithUser(uid,'changed email from "' + oldEmail + '" to "' + newEmail +'"');
};
11 years ago
events.logUsernameChange = function(uid, oldUsername, newUsername) {
11 years ago
events.logWithUser(uid,'changed username from "' + oldUsername + '" to "' + newUsername +'"');
};
11 years ago
events.logAdminLogin = function(uid) {
11 years ago
events.logWithUser(uid, 'logged into admin panel');
};
11 years ago
events.logPostEdit = function(uid, pid) {
11 years ago
events.logWithUser(uid, 'edited post (pid ' + pid + ')');
};
11 years ago
events.logPostDelete = function(uid, pid) {
11 years ago
events.logWithUser(uid, 'deleted post (pid ' + pid + ')');
};
11 years ago
events.logPostRestore = function(uid, pid) {
11 years ago
events.logWithUser(uid, 'restored post (pid ' + pid + ')');
};
11 years ago
events.logTopicDelete = function(uid, tid) {
11 years ago
events.logWithUser(uid, 'deleted topic (tid ' + tid + ')');
};
11 years ago
events.logTopicRestore = function(uid, tid) {
11 years ago
events.logWithUser(uid, 'restored topic (tid ' + tid + ')');
};
11 years ago
11 years ago
events.logWithUser = function(uid, string) {
11 years ago
user.getUserField(uid, 'username', function(err, username) {
if(err) {
11 years ago
return winston.error('Error logging event. ' + err.message);
11 years ago
}
11 years ago
var msg = username + '(uid ' + uid + ') ' + string;
11 years ago
events.log(msg);
});
}
11 years ago
events.log = function(msg, callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
11 years ago
11 years ago
msg = '[' + new Date().toUTCString() + '] - ' + msg;
fs.appendFile(logFile, msg + '\n', function(err) {
if(err) {
winston.error('Error logging event. ' + err.message);
if (typeof callback === 'function') {
callback(err);
}
return;
}
if (typeof callback === 'function') {
callback();
}
11 years ago
});
};
11 years ago
10 years ago
events.getLog = function(end, len, callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
11 years ago
10 years ago
fs.stat(logFile, function(err, stat) {
if (err) {
11 years ago
return callback(null, 'No logs found!');
}
10 years ago
var buffer = '';
var size = stat.size;
if (end === -1) {
end = size;
}
end = parseInt(end, 10);
var start = Math.max(0, end - len);
var rs = fs.createReadStream(logFile, {start: start, end: end});
rs.addListener('data', function(lines) {
buffer += lines.toString();
});
rs.addListener('end', function() {
var firstNewline = buffer.indexOf('\n');
if (firstNewline !== -1) {
buffer = buffer.slice(firstNewline);
buffer = buffer.split('\n').reverse().join('\n');
}
callback(null, {data: buffer, next: end - buffer.length});
})
});
10 years ago
};
11 years ago
}(module.exports));