<commit_before>#! /usr/bin/env node

var program = require('commander');
var modulePackage = require('./package');
var spawn = require('child_process').spawn;
var talkyServer = 'https://api.talky.io:443';
var simpleServer = 'http://signaling.simplewebrtc.com:8888';
var defaults = {
    server: process.env.SERVER || talkyServer,
    room: process.env.ROOM || 'lukes-magical-r2d2-telephone'
};

program
    .version(modulePackage.version)
    .option('--server [server]', 'Server', String, defaults.server)
    .option('--room [room]', 'Room', String, defaults.room)
    .option('--dry [dry]', 'Dry run', Boolean, false)
    .option('--verbose [verbose]', 'Verbose', Boolean, false)
    .option('--xhr [xhr]', 'xhr', Boolean, false)
    .option('--ip [ip]', 'IP', String, '')
    .parse(process.argv);

if (program.server === 'talky') {
    program.server = talkyServer;
} else if (program.server === 'signalmaster') {
    program.server = simpleServer;
}

var io = require('socket.io-client');
if (program.xhr) io.transports = ['xhr-polling'];

var socket = io.connect(program.server);
var _ = require('underscore');
var R2D2 = require('./index');
var R2D2ringer = R2D2({
    ascii: false
});
var here = [];

if (program.verbose) {
    socket.emit = _.wrap(socket.emit, function (fn, name) {
        console.log('-->', name, _.toArray(arguments).slice(2));
        fn.apply(this, _.toArray(arguments).slice(1));
    });

    socket.$emit = _.wrap(socket.$emit, function (fn) {
        console.log('<--', arguments[1], _.toArray(arguments).slice(2));
        return fn.apply(this, _.toArray(arguments).slice(1));
    });
}

function canRing(cb) {
    if (!program.ip) {
        return cb(null);
    }

    spawn('ping', [
        '-c',
        '1',
        program.ip
    ]).on('close', function (code) {
        cb(code === 0 ? null : new Error('ping for ' + program.ip + ' exited with code ' + code));
    });
}

function ring(event) {
    function log(name, logItem) { console.log(name || 'RING:', logItem || event); }

    canRing(function (err) {
        if (err) {
            log('ERROR:', err.message);
        } else if (program.dry) {
            log('DRYRING:');
        } else {
            R2D2ringer(log);
        }
    });
}

function removeFromArray(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax = arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}

console.log('CONNECTING:', program.server);
socket.on('connect', function () {
    console.log('CONNECTED:', socket.socket.sessionid);

    console.log('JOINING:', program.room);
    socket.emit('join', program.room, function (err, room) {
        if (err) {
            console.error('ROOM ERROR:', err);
            return;
        }
        here = _.keys(room.clients);
        console.log('ROOM:', program.room);
        console.log('HERE:', here.join(', ') || 'empty');
        if (here.length > 0) {
            ring(here.join(', '));
        }
    });

    socket.on('message', function (message) {
        if (message.type === 'offer') {
            console.log('JOINED:', message.from);
            if (here.length === 0) {
                ring(message.from);
            }
            here.push(message.from);
        }
    });

    socket.on('remove', function (message) {
        console.log('LEFT:', message.id);
        removeFromArray(here, message.id);
    });
});<commit_msg>dont ring on server start; its an edge case anyway and it relies on the signaling server properly removing clients which doesnt always happen<commit_after>#! /usr/bin/env node

var program = require('commander');
var modulePackage = require('./package');
var spawn = require('child_process').spawn;
var talkyServer = 'https://api.talky.io:443';
var simpleServer = 'http://signaling.simplewebrtc.com:8888';
var defaults = {
    server: process.env.SERVER || talkyServer,
    room: process.env.ROOM || 'lukes-magical-r2d2-telephone'
};

program
    .version(modulePackage.version)
    .option('--server [server]', 'Server', String, defaults.server)
    .option('--room [room]', 'Room', String, defaults.room)
    .option('--dry [dry]', 'Dry run', Boolean, false)
    .option('--verbose [verbose]', 'Verbose', Boolean, false)
    .option('--xhr [xhr]', 'xhr', Boolean, false)
    .option('--ip [ip]', 'IP', String, '')
    .parse(process.argv);

if (program.server === 'talky') {
    program.server = talkyServer;
} else if (program.server === 'signalmaster') {
    program.server = simpleServer;
}

var io = require('socket.io-client');
if (program.xhr) io.transports = ['xhr-polling'];

var socket = io.connect(program.server);
var _ = require('underscore');
var R2D2 = require('./index');
var R2D2ringer = R2D2({
    ascii: false
});
var here = [];

if (program.verbose) {
    socket.emit = _.wrap(socket.emit, function (fn, name) {
        console.log('-->', name, _.toArray(arguments).slice(2));
        fn.apply(this, _.toArray(arguments).slice(1));
    });

    socket.$emit = _.wrap(socket.$emit, function (fn) {
        console.log('<--', arguments[1], _.toArray(arguments).slice(2));
        return fn.apply(this, _.toArray(arguments).slice(1));
    });
}

function canRing(cb) {
    if (!program.ip) {
        return cb(null);
    }

    spawn('ping', [
        '-c',
        '1',
        program.ip
    ]).on('close', function (code) {
        cb(code === 0 ? null : new Error('ping for ' + program.ip + ' exited with code ' + code));
    });
}

function ring(event) {
    function log(name, logItem) { console.log(name || 'RING:', logItem || event); }

    canRing(function (err) {
        if (err) {
            log('ERROR:', err.message);
        } else if (program.dry) {
            log('DRYRING:');
        } else {
            R2D2ringer(log);
        }
    });
}

function removeFromArray(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax = arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}

console.log('CONNECTING:', program.server);
socket.on('connect', function () {
    console.log('CONNECTED:', socket.socket.sessionid);

    console.log('JOINING:', program.room);
    socket.emit('join', program.room, function (err) {
        if (err) {
            console.error('JOIN ERROR:', err);
            return;
        }
        console.log('JOINED:', program.room);
    });

    socket.on('message', function (message) {
        if (message.type === 'offer') {
            console.log('JOINED:', message.from);
            if (here.length === 0) {
                ring(message.from);
            }
            here.push(message.from);
        }
    });

    socket.on('remove', function (message) {
        console.log('LEFT:', message.id);
        removeFromArray(here, message.id);
    });
});
{'fraction_non_alphanumeric': 0.1896576628629207, 'fraction_numerical': 0.008233424815831288, 'mean_word_length': 2.574599896747548, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '3455023', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2249, 'n_words': 477}