<commit_before>var irc = require('irc');
var _ = require('underscore');
var C = require('./Commands.js');
var jf = require('jsonfile');
var fs = require('fs');

var argv = require('yargs')
          .default('server','irc.irchighway.net')
          .default('nick','Praecantatio')
          .default('channel','#empire')
          .default('password',null)
          .argv
		  ;

		  
var client = new irc.Client(argv.server, argv.nick, {
    channels: [argv.channel,'#game'],
	userName: 'Prae',
	realName: 'Praecantatio',
	floodProtection: true,
	floodProtectionDelay: 500,
	stripColors: true,
	password: <PASSWORD>.password
});

var commandList = [];
var users = [];

var PAObj;

//Modules
var modules = [
	{
		name: 'Commands.js',
		exp: undefined,
	},{
		name: 'memo.js',
		exp: undefined
	},{
		name: 'deeps.js',
		exp: undefined
	}
];
function loadModules() {
	commandList = [];
	_.each(modules, function(module){
		try{
			if ( module.exp)
				module.exp.onReassemble();
			module.exp = requireUncached('./'+module.name);
			module.exp.setMain(PAObj);
			_.each(module.exp.commandList, function(cmd){
				commandList.push(cmd);
			});
		}catch(ex){
			console.log("[ERR] Loading module '"+module.name+"': "+ ex);
		}
	});
}

//EOModules

//TODO: Change to jsonfile
function loadUsers() {
	User.find({}, function(err, data ){
		if ( err ) {console.dir(err); return ;}
		users = data;
		PAObj.users = users;
		console.log("Users Loaded!");
	});
};

loadUsers();
var loadUsersEvent = setTimeout(loadUsers, 60000);

//Public Access Object
PAObj = {
	client: client,
	checkForPermission: checkForPermission,
	users: users,
	loadUsers: loadUsers,
	commandList: commandList,
};

loadModules();

client.addListener('error', function(message) {
    console.log('error: ', message);
});

//Commands
client.addListener('message', function (from, to, message, messageObj) {
    var messageArr = message.split(" ");
	var commandStr = messageArr[0].substring(1);
	var isDenied = false;
	_.each(denied, function(den) {
		if ( den.toLowerCase() == from.toLowerCase() ){
			isDenied = true;
			return;
		}
	});
	if ( isDenied ) return;
	
    if ( message.charAt(0) == '+' ) {
		
		if ( commandStr == "reassemble" && checkForPermission(from,messageObj.host,'z') ){
			console.log(from + ' reassembled me!');
			
			loadModules();
			return;
		}
		
		var commandObj = getCommand(commandStr);
		if ( commandObj ){
			try {
				var dest;
				if ( !checkForPermission(from,messageObj.host,commandObj.permission) ){
					client.notice(from,"Access denied. Flag "+commandObj.permission+" is required.");
					return;
				}
				if (  commandObj.minParams && messageArr.length < commandObj.minParams+1 ){
					getCommand("help").func(from,to,from,['+help',commandStr],messageObj);
                    return;
				}

				console.log(from+" issued the command: "+messageArr);
				
				if ( commandObj.dest ){
					switch (commandObj.dest){
						case "source": dest = from; break;
						case "channel": {
							if ( to.charAt(0)=='#' ){
								dest = to;
								break;
							}
							else {
								client.notice(from,"Can't use this command outside a channel.");
								return;
							}
						}
						case "any":
						default: dest = to.charAt(0)=='#'?to:from; break;
					}
				} else {
					dest = to.charAt(0)=='#'?to:from;
				}

				commandObj.func(from,to,dest,messageArr,messageObj);
			}catch(ex){
				console.log(ex);
			}
		}
    } else if ( message.indexOf(argv.nick) > -1 ) {
		var dest = to.charAt(0)=='#'?to:from;
		client.say(dest,mhal.getReplyFromSentence(message));
		
	} else if ( message.charAt(0) == '!') {
		
	} else {
		mhal.addMass(message);
		mhal.save();
	}

});

function getCommand(commandStr){
	return _.find(commandList, function(commandObj) { return commandObj.command.toLowerCase() == commandStr.toLowerCase() });
}

//Reload
function requireUncached(module){
    delete require.cache[require.resolve(module)]
    return require(module)
}

function compareChannelLevels(l1, l2){
	var values = {
		"~" : 20,
		"&" : 15,
		"!" : 10,
		"@" : 5,
		"%" : 4,
		"+" : 3
	};

	var ll1 = l1;
	var y = 0;
	for ( var i=0; i<ll1.length; i++ ){
		var x = 0;
		try {
			x = values[ll1[i]];
		} catch( e ){
			x = 0;
		}
		if ( x > y ) {
			l1 = ll1[i];
			y = x;
		}
	}
	
	var v1;
	try {
		v1 = values[l1];
	} catch( e ){
		v1 = 0;
	}
	var v2;
	try {
		v2 = values[l2];
	} catch( e ){
		v2 = 0;
	}

	return v1 >= v2;
}

function checkForPermission( nick, vhost, permission ){
	if ( users.length == 0 ) return true;
	if (typeof permission === 'undefined') return true;
	var regexVhost = new RegExp(vhost,'i');
	var user = _.find(users, function(usr){ return usr.nick.toLowerCase() == nick.toLowerCase();});
	if ( !user ) return false;
	if ( !user.host.match(regexVhost) ) return false;
	if ( user.permissions.indexOf(permission) > -1 || user.permissions.indexOf('z') > -1) return true;
	return false;
}


<commit_msg>Changed users to local-json mode.<commit_after>var irc = require('irc');
var _ = require('underscore');
var C = require('./Commands.js');
var jf = require('jsonfile');
var fs = require('fs');

var argv = require('yargs')
          .default('server','irc.irchighway.net')
          .default('nick','Praecantatio')
          .default('channel','#empire')
          .default('password',<PASSWORD>)
          .argv
		  ;

		  
var client = new irc.Client(argv.server, argv.nick, {
    channels: [argv.channel,'#game'],
	userName: 'Prae',
	realName: 'Praecantatio',
	floodProtection: true,
	floodProtectionDelay: 500,
	stripColors: true,
	password: argv.password
});

var commandList = [];
var users = [];

var PAObj;

//Modules
var modules = [
	{
		name: 'Commands.js',
		exp: undefined,
	}
];
function loadModules() {
	commandList = [];
	_.each(modules, function(module){
		try{
			if ( module.exp)
				module.exp.onReassemble();
			module.exp = requireUncached('./'+module.name);
			module.exp.setMain(PAObj);
			_.each(module.exp.commandList, function(cmd){
				commandList.push(cmd);
			});
		}catch(ex){
			console.log("[ERR] Loading module '"+module.name+"': "+ ex);
		}
	});
}

//EOModules

//TODO: Change to jsonfile
function loadUsers() {
	users = jf.readFileSync("users.json");
	PAObj.users = users;
};

var loadUsersEvent = setTimeout(loadUsers, 60000);

//Public Access Object
PAObj = {
	client: client,
	checkForPermission: checkForPermission,
	users: users,
	loadUsers: loadUsers,
	commandList: commandList,
};

loadUsers();
loadModules();

client.addListener('error', function(message) {
    console.log('error: ', message);
});

//Commands
client.addListener('message', function (from, to, message, messageObj) {
    var messageArr = message.split(" ");
	var commandStr = messageArr[0].substring(1);
	var isDenied = false;
	_.each(denied, function(den) {
		if ( den.toLowerCase() == from.toLowerCase() ){
			isDenied = true;
			return;
		}
	});
	if ( isDenied ) return;
	
    if ( message.charAt(0) == '+' ) {
		
		if ( commandStr == "reassemble" && checkForPermission(from,messageObj.host,'z') ){
			console.log(from + ' reassembled me!');
			
			loadModules();
			return;
		}
		
		var commandObj = getCommand(commandStr);
		if ( commandObj ){
			try {
				var dest;
				if ( !checkForPermission(from,messageObj.host,commandObj.permission) ){
					client.notice(from,"Access denied. Flag "+commandObj.permission+" is required.");
					return;
				}
				if (  commandObj.minParams && messageArr.length < commandObj.minParams+1 ){
					getCommand("help").func(from,to,from,['+help',commandStr],messageObj);
                    return;
				}

				console.log(from+" issued the command: "+messageArr);
				
				if ( commandObj.dest ){
					switch (commandObj.dest){
						case "source": dest = from; break;
						case "channel": {
							if ( to.charAt(0)=='#' ){
								dest = to;
								break;
							}
							else {
								client.notice(from,"Can't use this command outside a channel.");
								return;
							}
						}
						case "any":
						default: dest = to.charAt(0)=='#'?to:from; break;
					}
				} else {
					dest = to.charAt(0)=='#'?to:from;
				}

				commandObj.func(from,to,dest,messageArr,messageObj);
			}catch(ex){
				console.log(ex);
			}
		}
    } else if ( message.indexOf(argv.nick) > -1 ) {
		var dest = to.charAt(0)=='#'?to:from;
		client.say(dest,mhal.getReplyFromSentence(message));
		
	} else if ( message.charAt(0) == '!') {
		
	} else {
		mhal.addMass(message);
		mhal.save();
	}

});

function getCommand(commandStr){
	return _.find(commandList, function(commandObj) { return commandObj.command.toLowerCase() == commandStr.toLowerCase() });
}

//Reload
function requireUncached(module){
    delete require.cache[require.resolve(module)]
    return require(module)
}

function compareChannelLevels(l1, l2){
	var values = {
		"~" : 20,
		"&" : 15,
		"!" : 10,
		"@" : 5,
		"%" : 4,
		"+" : 3
	};

	var ll1 = l1;
	var y = 0;
	for ( var i=0; i<ll1.length; i++ ){
		var x = 0;
		try {
			x = values[ll1[i]];
		} catch( e ){
			x = 0;
		}
		if ( x > y ) {
			l1 = ll1[i];
			y = x;
		}
	}
	
	var v1;
	try {
		v1 = values[l1];
	} catch( e ){
		v1 = 0;
	}
	var v2;
	try {
		v2 = values[l2];
	} catch( e ){
		v2 = 0;
	}

	return v1 >= v2;
}

function checkForPermission( nick, vhost, permission ){
	if ( users.length == 0 ) return true;
	if (typeof permission === 'undefined') return true;
	var regexVhost = new RegExp(vhost,'i');
	var user = _.find(users, function(usr){ return usr.nick.toLowerCase() == nick.toLowerCase();});
	if ( !user ) return false;
	if ( !user.host.match(regexVhost) ) return false;
	if ( user.permissions.indexOf(permission) > -1 || user.permissions.indexOf('z') > -1) return true;
	return false;
}



{'fraction_non_alphanumeric': 0.1759966948977484, 'fraction_numerical': 0.011154720099153068, 'mean_word_length': 3.180915371329879, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7008256', 'n_tokens_mistral': 3974, 'n_tokens_neox': 3448, 'n_words': 713}