<commit_before>/* jslint node: true */
'use strict';

//	ENiGMA½
const Config							= require('./config.js').config;
const Log								= require('./logger.js').log;
const getMessageAreaByTag				= require('./message_area.js').getMessageAreaByTag;
const getMessageConferenceByTag			= require('./message_area.js').getMessageConferenceByTag;
const clientConnections					= require('./client_connections.js');
const StatLog							= require('./stat_log.js');
const FileBaseFilters					= require('./file_base_filter.js');
const formatByteSize					= require('./string_util.js').formatByteSize;

//	deps
const packageJson 			= require('../package.json');
const os					= require('os');
const _						= require('lodash');
const moment				= require('moment');

exports.getPredefinedMCIValue	= getPredefinedMCIValue;
exports.init					= init;

function init(cb) {
	setNextRandomRumor(cb);
}

function setNextRandomRumor(cb) {
	StatLog.getSystemLogEntries('system_rumorz', StatLog.Order.Random, 1, (err, entry) => {
		if(entry) {
			entry = entry[0];
		}
		const randRumor = entry && entry.log_value ? entry.log_value : '';
		StatLog.setNonPeristentSystemStat('random_rumor', randRumor);
		if(cb) {
			return cb(null);
		}
	});
}

function getUserRatio(client, propA, propB) {
	const a = StatLog.getUserStatNum(client.user, propA);
	const b	= StatLog.getUserStatNum(client.user, propB);
	const ratio	= ~~((a / b) * 100);
	return `${ratio}%`;
}

function userStatAsString(client, statName, defaultValue) {
	return (StatLog.getUserStat(client.user, statName) || defaultValue).toString();
}

function sysStatAsString(statName, defaultValue) {
	return (StatLog.getSystemStat(statName) || defaultValue).toString();
}

const PREDEFINED_MCI_GENERATORS = {
	//
	//	Board
	//
	BN	: function boardName() { return Config.general.boardName; },

	//	ENiGMA
	VL	: function versionLabel() { return 'ENiGMA½ v' + packageJson.version; },
	VN	: function version() { return packageJson.version; },

	//	+op info
	SN	: function opUserName() { return StatLog.getSystemStat('sysop_username'); },
	SR	: function opRealName() { return StatLog.getSystemStat('sysop_real_name'); },
	SL	: function opLocation() { return StatLog.getSystemStat('sysop_location'); },
	SA	: function opAffils() { return StatLog.getSystemStat('sysop_affiliation'); },
	SS	: function opSex() { return StatLog.getSystemStat('sysop_sex'); },
	SE	: function opEmail() { return StatLog.getSystemStat('sysop_email_address'); },
	//	:TODO: op age, web, ?????

	//
	//	Current user / session
	//
	UN	: function userName(client) { return client.user.username; },
	UI	: function userId(client) { return client.user.userId.toString(); },
	UG	: function groups(client) { return _.values(client.user.groups).join(', '); },
	UR	: function realName(client) { return userStatAsString(client, 'real_name', ''); },
	LO	: function location(client) { return userStatAsString(client, 'location', ''); },
	UA	: function age(client) { return client.user.getAge().toString(); },
	BD	: function birthdate(client) { return moment(client.user.properties.birthdate).format(client.currentTheme.helpers.getDateFormat()); },	//	iNiQUiTY
	US	: function sex(client) { return userStatAsString(client, 'sex', ''); },
	UE	: function emailAddres(client) { return userStatAsString(client, 'email_address', ''); },
	UW	: function webAddress(client) { return userStatAsString(client, 'web_address', ''); },
	UF	: function affils(client) { return userStatAsString(client, 'affiliation', ''); },
	UT	: function themeId(client) { return userStatAsString(client, 'theme_id', ''); },
	UC	: function loginCount(client) { return userStatAsString(client, 'login_count', 0); },
	ND	: function connectedNode(client) { return client.node.toString(); },
	IP	: function clientIpAddress(client) { return client.remoteAddress; },
	ST	: function serverName(client) { return client.session.serverName; },
	FN	: function activeFileBaseFilterName(client) {
		const activeFilter = FileBaseFilters.getActiveFilter(client);
		return activeFilter ? activeFilter.name : ''; 
	},
	DN	: function userNumDownloads(client) { return userStatAsString(client, 'dl_total_count', 0); },		//	Obv/2
	DK	: function userByteDownload(client) {	//	Obv/2 uses DK=downloaded Kbytes
		const byteSize = StatLog.getUserStatNum(client.user, 'dl_total_bytes');
		return formatByteSize(byteSize, true);	//	true=withAbbr
	},
	UP	: function userNumUploads(client) { return userStatAsString(client, 'ul_total_count', 0); },			//	Obv/2
	UK	: function userByteUpload(client) { //	Obv/2 uses UK=uploaded Kbytes
		const byteSize = StatLog.getUserStatNum(client.user, 'ul_total_bytes');
		return formatByteSize(byteSize, true);	//	true=withAbbr
	},
	NR	: function userUpDownRatio(client) {	//	Obv/2
		return getUserRatio(client, 'ul_total_count', 'dl_total_count');
	},<commit_msg>Always format IPv4 when prefixed/within IPv6 space<commit_after>/* jslint node: true */
'use strict';

//	ENiGMA½
const Config							= require('./config.js').config;
const Log								= require('./logger.js').log;
const getMessageAreaByTag				= require('./message_area.js').getMessageAreaByTag;
const getMessageConferenceByTag			= require('./message_area.js').getMessageConferenceByTag;
const clientConnections					= require('./client_connections.js');
const StatLog							= require('./stat_log.js');
const FileBaseFilters					= require('./file_base_filter.js');
const formatByteSize					= require('./string_util.js').formatByteSize;

//	deps
const packageJson 			= require('../package.json');
const os					= require('os');
const _						= require('lodash');
const moment				= require('moment');

exports.getPredefinedMCIValue	= getPredefinedMCIValue;
exports.init					= init;

function init(cb) {
	setNextRandomRumor(cb);
}

function setNextRandomRumor(cb) {
	StatLog.getSystemLogEntries('system_rumorz', StatLog.Order.Random, 1, (err, entry) => {
		if(entry) {
			entry = entry[0];
		}
		const randRumor = entry && entry.log_value ? entry.log_value : '';
		StatLog.setNonPeristentSystemStat('random_rumor', randRumor);
		if(cb) {
			return cb(null);
		}
	});
}

function getUserRatio(client, propA, propB) {
	const a = StatLog.getUserStatNum(client.user, propA);
	const b	= StatLog.getUserStatNum(client.user, propB);
	const ratio	= ~~((a / b) * 100);
	return `${ratio}%`;
}

function userStatAsString(client, statName, defaultValue) {
	return (StatLog.getUserStat(client.user, statName) || defaultValue).toString();
}

function sysStatAsString(statName, defaultValue) {
	return (StatLog.getSystemStat(statName) || defaultValue).toString();
}

const PREDEFINED_MCI_GENERATORS = {
	//
	//	Board
	//
	BN	: function boardName() { return Config.general.boardName; },

	//	ENiGMA
	VL	: function versionLabel() { return 'ENiGMA½ v' + packageJson.version; },
	VN	: function version() { return packageJson.version; },

	//	+op info
	SN	: function opUserName() { return StatLog.getSystemStat('sysop_username'); },
	SR	: function opRealName() { return StatLog.getSystemStat('sysop_real_name'); },
	SL	: function opLocation() { return StatLog.getSystemStat('sysop_location'); },
	SA	: function opAffils() { return StatLog.getSystemStat('sysop_affiliation'); },
	SS	: function opSex() { return StatLog.getSystemStat('sysop_sex'); },
	SE	: function opEmail() { return StatLog.getSystemStat('sysop_email_address'); },
	//	:TODO: op age, web, ?????

	//
	//	Current user / session
	//
	UN	: function userName(client) { return client.user.username; },
	UI	: function userId(client) { return client.user.userId.toString(); },
	UG	: function groups(client) { return _.values(client.user.groups).join(', '); },
	UR	: function realName(client) { return userStatAsString(client, 'real_name', ''); },
	LO	: function location(client) { return userStatAsString(client, 'location', ''); },
	UA	: function age(client) { return client.user.getAge().toString(); },
	BD	: function birthdate(client) { return moment(client.user.properties.birthdate).format(client.currentTheme.helpers.getDateFormat()); },	//	iNiQUiTY
	US	: function sex(client) { return userStatAsString(client, 'sex', ''); },
	UE	: function emailAddres(client) { return userStatAsString(client, 'email_address', ''); },
	UW	: function webAddress(client) { return userStatAsString(client, 'web_address', ''); },
	UF	: function affils(client) { return userStatAsString(client, 'affiliation', ''); },
	UT	: function themeId(client) { return userStatAsString(client, 'theme_id', ''); },
	UC	: function loginCount(client) { return userStatAsString(client, 'login_count', 0); },
	ND	: function connectedNode(client) { return client.node.toString(); },
	IP	: function clientIpAddress(client) { return client.remoteAddress.replace(/^::ffff:/, ''); },	//	convert any :ffff: IPv4's to 32bit version
	ST	: function serverName(client) { return client.session.serverName; },
	FN	: function activeFileBaseFilterName(client) {
		const activeFilter = FileBaseFilters.getActiveFilter(client);
		return activeFilter ? activeFilter.name : ''; 
	},
	DN	: function userNumDownloads(client) { return userStatAsString(client, 'dl_total_count', 0); },		//	Obv/2
	DK	: function userByteDownload(client) {	//	Obv/2 uses DK=downloaded Kbytes
		const byteSize = StatLog.getUserStatNum(client.user, 'dl_total_bytes');
		return formatByteSize(byteSize, true);	//	true=withAbbr
	},
	UP	: function userNumUploads(client) { return userStatAsString(client, 'ul_total_count', 0); },			//	Obv/2
	UK	: function userByteUpload(client) { //	Obv/2 uses UK=uploaded Kbytes
		const byteSize = StatLog.getUserStatNum(client.user, 'ul_total_bytes');
		return formatByteSize(byteSize, true);	//	true=withAbbr
	},
	NR	: function userUpDownRatio(client) {	//	Obv/2
		return getUserRatio(client, 'ul_total_count', 'dl_total_count');
	},
{'fraction_non_alphanumeric': 0.1516675218060544, 'fraction_numerical': 0.003181118522319138, 'mean_word_length': 5.121859296482412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3789948', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3233, 'n_words': 645}