<commit_before>// Node.JS Core Modules
var net, http, util, zlib, path, fs;
net = require('net');
http = require('http');
util = require('util');
zlib = require('zlib');
path = require('path');
fs = require('fs');

// Third-party modules
var EventEmitter2, EventEmitter, request, WebSocket, encoder, chalk;
EventEmitter2 = require('eventemitter2').EventEmitter2;
EventEmitter = new EventEmitter2({
    wildcard: true,
    delimiter: ':'
});
request = require('request');
WebSocket = require('ws');
WebSocket.prototype._send = WebSocket.prototype.send;
WebSocket.prototype.send = function(data, options, cb) {
    data = '"' + (typeof data === 'string' ? data : JSON.stringify(data)).split('"').join('\\"') + '"';
    this._send(data, options, cb);
};
WebSocket.prototype.sendEvent = function(type, data) {
    this.send({
        a: type,
        p: data,
        t: DateUtilities.getServerEpoch()
    });
};
encoder = require('node-html-encoder').Encoder('entity');
chalk = require('chalk');

// plugAPI
var Room, Logger, PlugAPIInfo, endpoints;
Room = require('./room');
Logger = require('./logger');
PlugAPIInfo = require('../package.json');
endpoints = {
    CHAT_DELETE: 'chat/',
    MODERATE_ADD_DJ: 'booth/add',
    MODERATE_BAN: 'bans/add',
    MODERATE_BOOTH: 'booth',
    MODERATE_MOVE_DJ: 'booth/move',
    MODERATE_MUTE: 'mutes',
    MODERATE_PERMISSIONS: 'staff/update',
    MODERATE_REMOVE_DJ: 'booth/remove/',
    MODERATE_SKIP: 'booth/skip',
    MODERATE_UNBAN: 'bans/',
    MODERATE_UNMUTE: 'mutes/',
    PLAYLIST: 'playlists',
    ROOM_CYCLE_BOOTH: 'booth/cycle',
    ROOM_INFO: 'rooms/update',
    ROOM_LOCK_BOOTH: 'booth/lock',
    USER_SET_AVATAR: 'users/avatar',
    USER_SET_STATUS: 'users/status'
};

/**
 * That is this and this is that
 * @type {PlugAPI}
 */
var that = null;

/**
 * @type {null|WebSocket}
 */
var ws;
var p3Socket, initialized, commandPrefix, apiId, _authCode, _cookies, chatHistory, serverRequests, room, connectingRoomSlug, rpcHandlers, logger, floodProtectionDelay, chatQueue;
ws = null;
p3Socket = null;
initialized = false;
commandPrefix = '!';
apiId = 0;
chatHistory = [];
connectingRoomSlug = null;
_authCode = '';
_cookies = {
    cookies: {},
    path: path.resolve(__dirname, '..', 'cookies.tmp'),
    load: function() {
        this.cookies = JSON.parse(fs.readFileSync(this.path));
    },
    save: function() {
        fs.writeFileSync(this.path, JSON.stringify(this.cookies));
    },
    fromHeaders: function(headers) {
        for (var i in headers) {
            if (!headers.hasOwnProperty(i)) continue;
            if (i == 'set-cookie') {
                for (var j in headers[i]) {
                    if (!headers[i].hasOwnProperty(j)) continue;
                    var cookieString, cookieKeyValue, key, value;

                    cookieString = headers[i][j];
                    cookieKeyValue = cookieString.split(';')[0].split('=');
                    key = cookieKeyValue.shift();
                    value = cookieKeyValue.join('=');

                    this.cookies[key] = value;
                }
            }
        }
    },
    toString: function() {
        var cookies = [];
        for (var i in this.cookies) {
            if (!this.cookies.hasOwnProperty(i)) continue;
            cookies.push(i + '=' + this.cookies[i]);
        }
        return cookies.join('; ');
    }
};
serverRequests = {
    queue: [],
    sent: 0,
    limit: 10,
    running: false
};
room = new Room();
rpcHandlers = {};
logger = new Logger('PlugAPI');
floodProtectionDelay = 200;
chatQueue = [];

function __bind(fn, me) {
    return function() {
        return fn.apply(me, arguments);
    };
}

function intPM(receiver, msg) {
    p3Socket.send({
        type: 'PM',
        value: {
            id: typeof receiver === 'object' ? receiver.id : receiver,
            message: msg
        }
    });
}

(function() {
    function sendNextMessage() {
        if (chatQueue.length > 0) {
            var nextMessage = chatQueue.shift(), msg = nextMessage.msg, timeout = nextMessage.timeout;
            ws.sendEvent('chat', msg);
            if (timeout !== undefined && !isNaN(timeout) && ~~timeout > 0) {
                var specificChatDeleter = function(data) {
                    if (data.uid == room.getSelf().id && data.message.trim() == msg.trim()) {
                        setTimeout(function() {
                            that.moderateDeleteChat(data.cid);
                        }, ~~timeout * 1E3);
                        that.off('chat', specificChatDeleter);
                    }
                };
                that.on('chat', specificChatDeleter);
            }
        }
        setTimeout(function() {
            sendNextMessage();
        }, floodProtectionDelay);
    }

    sendNextMessage();
})();

function queueChat(msg, timeout) {
    chatQueue.push({
        msg: msg,
        timeout: timeout
    });
}

/**
 DateUtilities
 Copyright (C) 2014 by Plug DJ, Inc.
 Modified by TAT (<EMAIL>)
 */<commit_msg>Bot can now remove its messages<commit_after>// Node.JS Core Modules
var net, http, util, zlib, path, fs;
net = require('net');
http = require('http');
util = require('util');
zlib = require('zlib');
path = require('path');
fs = require('fs');

// Third-party modules
var EventEmitter2, EventEmitter, request, WebSocket, encoder, chalk;
EventEmitter2 = require('eventemitter2').EventEmitter2;
EventEmitter = new EventEmitter2({
    wildcard: true,
    delimiter: ':'
});
request = require('request');
WebSocket = require('ws');
WebSocket.prototype._send = WebSocket.prototype.send;
WebSocket.prototype.send = function(data, options, cb) {
    data = '"' + (typeof data === 'string' ? data : JSON.stringify(data)).split('"').join('\\"') + '"';
    this._send(data, options, cb);
};
WebSocket.prototype.sendEvent = function(type, data) {
    this.send({
        a: type,
        p: data,
        t: DateUtilities.getServerEpoch()
    });
};
encoder = require('node-html-encoder').Encoder('entity');
chalk = require('chalk');

// plugAPI
var Room, Logger, PlugAPIInfo, endpoints;
Room = require('./room');
Logger = require('./logger');
PlugAPIInfo = require('../package.json');
endpoints = {
    CHAT_DELETE: 'chat/',
    MODERATE_ADD_DJ: 'booth/add',
    MODERATE_BAN: 'bans/add',
    MODERATE_BOOTH: 'booth',
    MODERATE_MOVE_DJ: 'booth/move',
    MODERATE_MUTE: 'mutes',
    MODERATE_PERMISSIONS: 'staff/update',
    MODERATE_REMOVE_DJ: 'booth/remove/',
    MODERATE_SKIP: 'booth/skip',
    MODERATE_UNBAN: 'bans/',
    MODERATE_UNMUTE: 'mutes/',
    PLAYLIST: 'playlists',
    ROOM_CYCLE_BOOTH: 'booth/cycle',
    ROOM_INFO: 'rooms/update',
    ROOM_LOCK_BOOTH: 'booth/lock',
    USER_SET_AVATAR: 'users/avatar',
    USER_SET_STATUS: 'users/status'
};

/**
 * That is this and this is that
 * @type {PlugAPI}
 */
var that = null;

/**
 * @type {null|WebSocket}
 */
var ws;
var p3Socket, initialized, commandPrefix, apiId, _authCode, _cookies, chatHistory, serverRequests, room, connectingRoomSlug, rpcHandlers, logger, floodProtectionDelay, chatQueue;
ws = null;
p3Socket = null;
initialized = false;
commandPrefix = '!';
apiId = 0;
chatHistory = [];
connectingRoomSlug = null;
_authCode = '';
_cookies = {
    cookies: {},
    path: path.resolve(__dirname, '..', 'cookies.tmp'),
    load: function() {
        this.cookies = JSON.parse(fs.readFileSync(this.path));
    },
    save: function() {
        fs.writeFileSync(this.path, JSON.stringify(this.cookies));
    },
    fromHeaders: function(headers) {
        for (var i in headers) {
            if (!headers.hasOwnProperty(i)) continue;
            if (i == 'set-cookie') {
                for (var j in headers[i]) {
                    if (!headers[i].hasOwnProperty(j)) continue;
                    var cookieString, cookieKeyValue, key, value;

                    cookieString = headers[i][j];
                    cookieKeyValue = cookieString.split(';')[0].split('=');
                    key = cookieKeyValue.shift();
                    value = cookieKeyValue.join('=');

                    this.cookies[key] = value;
                }
            }
        }
    },
    toString: function() {
        var cookies = [];
        for (var i in this.cookies) {
            if (!this.cookies.hasOwnProperty(i)) continue;
            cookies.push(i + '=' + this.cookies[i]);
        }
        return cookies.join('; ');
    }
};
serverRequests = {
    queue: [],
    sent: 0,
    limit: 10,
    running: false
};
room = new Room();
rpcHandlers = {};
logger = new Logger('PlugAPI');
floodProtectionDelay = 200;
chatQueue = [];

function __bind(fn, me) {
    return function() {
        return fn.apply(me, arguments);
    };
}

function intPM(receiver, msg) {
    p3Socket.send({
        type: 'PM',
        value: {
            id: typeof receiver === 'object' ? receiver.id : receiver,
            message: msg
        }
    });
}

(function() {
    function sendNextMessage() {
        if (chatQueue.length > 0) {
            var nextMessage = chatQueue.shift(), msg = nextMessage.msg, timeout = nextMessage.timeout;
            ws.sendEvent('chat', msg);
            if (timeout !== undefined && !isNaN(timeout) && ~~timeout > 0) {
                var specificChatDeleter = function(data) {
                    if (data.raw.uid == room.getSelf().id && data.message.trim() == msg.trim()) {
                        setTimeout(function() {
                            that.moderateDeleteChat(data.raw.cid);
                        }, ~~timeout * 1E3);
                        that.off('chat', specificChatDeleter);
                    }
                };
                that.on('chat', specificChatDeleter);
            }
        }
        setTimeout(function() {
            sendNextMessage();
        }, floodProtectionDelay);
    }

    sendNextMessage();
})();

function queueChat(msg, timeout) {
    chatQueue.push({
        msg: msg,
        timeout: timeout
    });
}

/**
 DateUtilities
 Copyright (C) 2014 by Plug DJ, Inc.
 Modified by TAT (<EMAIL>)
 */
{'fraction_non_alphanumeric': 0.15493519441674974, 'fraction_numerical': 0.004785643070787637, 'mean_word_length': 2.430574555403557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6907843', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3121, 'n_words': 667}