<commit_before>const userService = require('../services/user.service.es6');
const config = require('../config/env.config.es6');
const phraseDao = require('../daos/phrase.dao.es6');
const socketService = require('../services/socket.service.es6');

function parse(message, userId) {
    let command = null;
    let matches = message.match(/^\\([^\\].*?):([^:]*):?(.*)$/);
    if (matches) {
        command = {
            name: matches[1],
            action: matches[2],
            data: matches[3],
            user: userId
        }
    }
    return command;
}

function executeCommand(command) {
    switch (command.name) {
        case 'admin':
            return adminAction(command);
        case 'users':
            return usersAction(command);
        case 'phrases':
            return phrasesAction(command);
        case 'messages':
            return messageAction(command);
        default:
            return returnError();
    }
}

function messageAction(command) {
    switch (command.action) {
        case 'broadcast':
            return messageBroadcast(command);
        default:
            return returnError();
    }
}

function messageBroadcast(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin && command.data) {
        let users = userService.getAll();
        for (let toUserId in users) {
            if(users.hasOwnProperty(toUserId)) {
                let toUser = users[toUserId];
                if (command.user === toUser.id) continue;
                let socket = socketService.getSocketById(toUser.socket);
                socket.emit('message', command.data);
            }
        }
        return returnOK();
    }
    return returnError();
}

function phrasesAction(command) {
    switch (command.action) {
        case 'insert':
            return phrasesInsert(command);
        default:
            return returnError();
    }
}

function phrasesInsert(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin && command.data) {
        phraseDao.insert(command.data);
        return returnOK();
    }
    return returnError();
}

function usersAction(command) {
    switch (command.action) {
        case 'count':
            return usersCount(command);
        default:
            return returnError();
    }
}

function usersCount(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin) {
        return userService.countUsers();
    }
    return returnError();
}

function adminAction(command) {
    switch (command.action) {
        case 'login':<commit_msg>adding list and drop users<commit_after>const userService = require('../services/user.service.es6');
const config = require('../config/env.config.es6');
const phraseDao = require('../daos/phrase.dao.es6');
const socketService = require('../services/socket.service.es6');

function parse(message, userId) {
    let command = null;
    let matches = message.match(/^\\([^\\].*?):([^:]*):?(.*)$/);
    if (matches) {
        command = {
            name: matches[1],
            action: matches[2],
            data: matches[3],
            user: userId
        }
    }
    return command;
}

function executeCommand(command) {
    switch (command.name) {
        case 'admin':
            return adminAction(command);
        case 'users':
            return usersAction(command);
        case 'phrases':
            return phrasesAction(command);
        case 'messages':
            return messageAction(command);
        default:
            return returnError();
    }
}

function messageAction(command) {
    switch (command.action) {
        case 'broadcast':
            return messageBroadcast(command);
        default:
            return returnError();
    }
}

function messageBroadcast(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin && command.data) {
        let users = userService.getAll();
        for (let toUserId in users) {
            if (users.hasOwnProperty(toUserId)) {
                let toUser = users[toUserId];
                if (command.user === toUser.id) continue;
                let socket = socketService.getSocketById(toUser.socket);
                socket.emit('message', command.data);
            }
        }
        return returnOK();
    }
    return returnError();
}

function phrasesAction(command) {
    switch (command.action) {
        case 'insert':
            return phrasesInsert(command);
        default:
            return returnError();
    }
}

function phrasesInsert(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin && command.data) {
        phraseDao.insert(command.data);
        return returnOK();
    }
    return returnError();
}

function usersAction(command) {
    switch (command.action) {
        case 'count':
            return usersCount(command);
        case 'list':
            return usersList(command);
        default:
            return returnError();
    }
}

function usersList(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin) {
        let keys = Object.keys(userService.getAll());
        let index = keys.indexOf(command.user);
        keys.splice(index, 1);
        return keys.join(', ');
    }
    return returnError();
}

function usersDrop(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin && command.data) {
        let userToDrop = userService.getUserById(command.data);
        if (userToDrop) {
            let socket = socketService.getSocketById(userToDrop.socket);
            socket.disconnect();
            return returnOK();
        }
    }
    return returnError();
}

function usersCount(command) {
    let user = userService.getUserById(command.user);
    if (user.isAdmin) {
        return userService.countUsers();
    }
    return returnError();
}

function adminAction(command) {
    switch (command.action) {
        case 'login':
{'fraction_non_alphanumeric': 0.1275459098497496, 'fraction_numerical': 0.0025041736227045075, 'mean_word_length': 2.210610932475884, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6512367', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1656, 'n_words': 354}