/*
Core  server-side administration module
 */

'use strict';

let caps = require('../server/caps'),
	check = require('../server/msgcheck'),
    common = require('../common'),
	config = require('../config'),
	db = require('../db'),
	events = require('events'),
    okyaku = require('../server/okyaku'),
	mnemonics = require('./mnemonic/mnemonics'),
	Muggle = require('../util/etc').Muggle,
	winston = require('winston');

let mnemonizer = new mnemonics.mnemonizer(config.SECURE_SALT);

function genMnemonic(ip) {
	return ip && mnemonizer.Apply_mnemonic(ip);
}
exports.genMnemonic = genMnemonic;

let dispatcher = okyaku.dispatcher,
	redis = global.redis;

function modHandler(method, errMsg) {
	return function (nums, client) {
		return caps.checkAuth('janitor', client.ident)
			&& check('id...', nums)
			&& client.db.modHandler(method, nums, function (err) {
				if (err)
					client.kotowaru(Muggle(errMsg, err));
			});
	};
}

dispatcher[common.SPOILER_IMAGES] = modHandler('spoilerImages',
	'Couldn\'t spoiler images.'
);

dispatcher[common.DELETE_IMAGES] = modHandler('deleteImages',
	'Couldn\'t delete images.'
);

// Non-persistent global live admin notifications
dispatcher[common.NOTIFICATION] = function (msg, client) {
	msg = msg[0];
	if (!caps.checkAuth('admin', client.ident) || !check('string', msg))
		return false;
	okyaku.push([0, common.NOTIFICATION, common.escape_html(msg)]);
	return true;
};

dispatcher[common.MOD_LOG] = function (msg, client) {
	if (!caps.checkAuth('janitor', client.ident))
		return false;

	redis.zrange('modLog', 0, -1, function (err, log) {
		if (err)
			return winston.error('Moderation log fetch error:', err);
		if (!log.length)
			return;
		client.send([0, common.MOD_LOG, db.destrigifyList(log)]);
	});
	return true;
};

@@ -1744,28 +1744,308 @@
 g)%5D);%0A%09%7D);%0A%09return true;%0A%7D;%0A
+%0A// Clean up moderation log entries older than one week%0Afunction cleanLog() %7B%0A%09redis.zremrangebyscore('imageDups', 0, Date.now() + 1000*60*60*24*7,%0A%09%09function (err) %7B%0A%09%09%09if (err)%0A%09%09%09%09winston.error('Error cleaning up moderation log:', err);%0A%09%09%7D%0A%09);%0A%7D%0AsetInterval(cleanLog, 60000);%0A

Delete old moderation log entries