<commit_before>/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
/* jshint -W061 */
'use strict';

var socketio = require('socket.io');
var request = require('request');

function IOSocket(server, settings, adapter, objects, states, store) {
    if (!(this instanceof IOSocket)) return new IOSocket(server, settings, adapter, objects, states, store);

    var userKey = 'connect.sid'; // const
    var cmdSessions = {};
    var that = this;
    this.server = null;
    var cookieParser;
    var passport;

    if (settings.auth) {
        cookieParser = require('cookie-parser');
        passport     = require('passport');
    }

    var passportSocketIo = require('passport.socketio');

    // do not send too many state updates
    var eventsThreshold = {
        count: 0,
        timeActivated: 0,
        active: false,
        accidents: 0,
        repeatSeconds: 3,   // how many seconds continuously must be number of events > value
        value: parseInt(settings.thresholdValue, 10) || 200, // how many events allowed in one check interval
        checkInterval: 1000 // duration of one check interval
    };

    // static information
    var commandsPermissions = {
        getObject: {type: 'object', operation: 'read'},
        getObjects: {type: 'object', operation: 'list'},
        getObjectView: {type: 'object', operation: 'list'},
        setObject: {type: 'object', operation: 'write'},
        delObject: {type: 'object', operation: 'delete'},
        extendObject: {type: 'object', operation: 'write'},
        getHostByIp: {type: 'object', operation: 'list'},

        getStates: {type: 'state', operation: 'list'},
        getState: {type: 'state', operation: 'read'},
        setState: {type: 'state', operation: 'write'},
        delState: {type: 'state', operation: 'delete'},
        getStateHistory: {type: 'state', operation: 'read'},
        createState: {type: 'state', operation: 'create'},

        addUser: {type: 'users', operation: 'create'},
        delUser: {type: 'users', operation: 'delete'},
        addGroup: {type: 'users', operation: 'create'},
        delGroup: {type: 'users', operation: 'delete'},
        changePassword: {type: 'users', operation: 'write'},

        httpGet: {type: 'other', operation: 'http'},
        cmdExec: {type: 'other', operation: 'execute'},
        sendTo: {type: 'other', operation: 'sendto'},
        sendToHost: {type: 'other', operation: 'sendto'},

        readDir: {type: 'file', operation: 'list'},
        createFile: {type: 'file', operation: 'create'},
        writeFile: {type: 'file', operation: 'write'},
        readFile: {type: 'file', operation: 'read'},
        deleteFile: {type: 'file', operation: 'delete'},

        authEnabled: {type: '', operation: ''},
        disconnect: {type: '', operation: ''},
        listPermissions: {type: '', operation: ''},
        getUserPermissions: {type: 'object', operation: 'read'}
    };

    function addUser(user, pw, options, callback) {
        adapter.getForeignObject('system.user.' + user, options, function (err, obj) {
            if (obj) {
                if (typeof callback === 'function') {
                    callback('User yet exists');
                }
            } else {
                adapter.setForeignObject('system.user.' + user, {
                    type: 'user',
                    common: {
                        name: user,
                        enabled: true,
                        groups: []
                    }
                }, options, function () {
                    adapter.setPassword(user, pw, callback);
                });
            }
        });
    }

    function delUser(user, options, callback) {
        adapter.getForeignObject('system.user.' + user, options, function (err, obj) {
            if (err || !obj) {
                if (callback) {
                    callback('User does not exist');
                }
            } else {
                if (obj.common.dontDelete) {
                    if (callback) {
                        callback('Cannot delete user, while is system user');
                    }
                } else {
                    adapter.delForeignObject('system.user.' + user, options, function (err) {
                        // Remove this user from all groups in web client
                        if (callback) {
                            callback(err);
                        }
                    });
                }
            }
        });
    }

    function addGroup(group, desc, acl, options, callback) {
        var name = group;
        if (typeof acl === 'function') {
            callback = acl;
            acl = null;
        }
        if (typeof desc === 'function') {
            callback = desc;
            desc = null;
        }
        if (typeof options === 'function') {
            callback = options;
            options = null;
        }
        if (name && name.substring(0, 1) !== name.substring(0, 1).toUpperCase()) {
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        group = group.substring(0, 1).toLowerCase() + group.substring(1);

        adapter.getForeignObject('system.group.' + group, options, function (err, obj) {
            if (obj) {
                if (callback) {
                    callback('Group yet exists');
                }
            } else {
                adapter.setForeignObject('system.group.' + group, {
                    type: 'group',
                    common: {
                        name: name,
                        desc: desc,<commit_msg>(bluefox) filter user and groups names<commit_after>/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
/* jshint -W061 */
'use strict';

var socketio = require('socket.io');
var request = require('request');

function IOSocket(server, settings, adapter, objects, states, store) {
    if (!(this instanceof IOSocket)) return new IOSocket(server, settings, adapter, objects, states, store);

    var userKey = 'connect.sid'; // const
    var cmdSessions = {};
    var that = this;
    this.server = null;
    var cookieParser;
    var passport;

    if (settings.auth) {
        cookieParser = require('cookie-parser');
        passport     = require('passport');
    }

    var passportSocketIo = require('passport.socketio');

    // do not send too many state updates
    var eventsThreshold = {
        count: 0,
        timeActivated: 0,
        active: false,
        accidents: 0,
        repeatSeconds: 3,   // how many seconds continuously must be number of events > value
        value: parseInt(settings.thresholdValue, 10) || 200, // how many events allowed in one check interval
        checkInterval: 1000 // duration of one check interval
    };

    // static information
    var commandsPermissions = {
        getObject:      {type: 'object', operation: 'read'},
        getObjects:     {type: 'object', operation: 'list'},
        getObjectView:  {type: 'object', operation: 'list'},
        setObject:      {type: 'object', operation: 'write'},
        delObject:      {type: 'object', operation: 'delete'},
        extendObject:   {type: 'object', operation: 'write'},
        getHostByIp:    {type: 'object', operation: 'list'},

        getStates:      {type: 'state', operation: 'list'},
        getState:       {type: 'state', operation: 'read'},
        setState:       {type: 'state', operation: 'write'},
        delState:       {type: 'state', operation: 'delete'},
        getStateHistory: {type: 'state', operation: 'read'},
        createState:    {type: 'state', operation: 'create'},

        addUser:        {type: 'users', operation: 'create'},
        delUser:        {type: 'users', operation: 'delete'},
        addGroup:       {type: 'users', operation: 'create'},
        delGroup:       {type: 'users', operation: 'delete'},
        changePassword: {type: 'users', operation: 'write'},

        httpGet:        {type: 'other', operation: 'http'},
        cmdExec:        {type: 'other', operation: 'execute'},
        sendTo:         {type: 'other', operation: 'sendto'},
        sendToHost:     {type: 'other', operation: 'sendto'},

        readDir:        {type: 'file', operation: 'list'},
        createFile:     {type: 'file', operation: 'create'},
        writeFile:      {type: 'file', operation: 'write'},
        readFile:       {type: 'file', operation: 'read'},
        deleteFile:     {type: 'file', operation: 'delete'},

        authEnabled:    {type: '', operation: ''},
        disconnect:     {type: '', operation: ''},
        listPermissions: {type: '', operation: ''},
        getUserPermissions: {type: 'object', operation: 'read'}
    };

    function addUser(user, pw, options, callback) {
        if (typeof options === 'function') {
            callback = options;
            options = null;
        }

        if (!user.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&# ]+$/)) {
            if (typeof callback === 'function') {
                callback('Invalid characters in the name. Only following special characters are allowed: -@+$§=?!&# and letters');
            }
            return;
        }

        adapter.getForeignObject('system.user.' + user, options, function (err, obj) {
            if (obj) {
                if (typeof callback === 'function') {
                    callback('User yet exists');
                }
            } else {
                adapter.setForeignObject('system.user.' + user, {
                    type: 'user',
                    common: {
                        name: user,
                        enabled: true,
                        groups: []
                    }
                }, options, function () {
                    adapter.setPassword(user, pw, callback);
                });
            }
        });
    }

    function delUser(user, options, callback) {
        adapter.getForeignObject('system.user.' + user, options, function (err, obj) {
            if (err || !obj) {
                if (callback) {
                    callback('User does not exist');
                }
            } else {
                if (obj.common.dontDelete) {
                    if (callback) {
                        callback('Cannot delete user, while is system user');
                    }
                } else {
                    adapter.delForeignObject('system.user.' + user, options, function (err) {
                        // Remove this user from all groups in web client
                        if (callback) {
                            callback(err);
                        }
                    });
                }
            }
        });
    }

    function addGroup(group, desc, acl, options, callback) {
        var name = group;
        if (typeof acl === 'function') {
            callback = acl;
            acl = null;
        }
        if (typeof desc === 'function') {
            callback = desc;
            desc = null;
        }
        if (typeof options === 'function') {
            callback = options;
            options = null;
        }
        if (name && name.substring(0, 1) !== name.substring(0, 1).toUpperCase()) {
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
        }
        group = group.substring(0, 1).toLowerCase() + group.substring(1);

        if (!group.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&# ]+$/)) {
            if (typeof callback === 'function') {
                callback('Invalid characters in the group name. Only following special characters are allowed: -@+$§=?!&# and letters');
            }
            return;
        }

        adapter.getForeignObject('system.group.' + group, options, function (err, obj) {
            if (obj) {
                if (callback) {
                    callback('Group yet exists');
                }
            } else {
                adapter.setForeignObject('system.group.' + group, {
                    type: 'group',
                    common: {
                        name: name,
                        desc: desc,
{'fraction_non_alphanumeric': 0.14020465423337183, 'fraction_numerical': 0.005116355834296088, 'mean_word_length': 1.702118171683389, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5848973', 'n_tokens_mistral': 3267, 'n_tokens_neox': 3127, 'n_words': 891}