'use strict';

var net = require('net');

var packet_utils = require('./packet-utils.js');

var rcon_client;
var response_handlers = {};

var connect = function(options, passwd, callback) {

    rcon_client = net.connect(options, function() {

        console.error('connection established');

        rcon_client.on('data', function(data) {
            var response = packet_utils.response(data);

            if (response.id === -1) {
                return callback(new Error('command failure'));
            } else {
                if (response_handlers.hasOwnProperty(response.id)) {
                    response_handlers[response.id](response);
                    delete response_handlers[response.id];
                } else {
                    console.log('no handler for id: %s', response.id);
                }
            }
        });

        rcon_client.on('end', function () {
            console.error('connection ended');
        });

        response_handlers[0] = function(response) {
            return callback();
        };
        rcon_client.write(packet_utils.auth({ 'id': 0, 'passwd': passwd }));
    });
};

var close = function() {
    rcon_client.end();
};

var command = function (packet, handler) {

    response_handlers[packet.id] = handler;
    rcon_client.write(packet_utils.command(packet));

};

connect({
    'port': process.env.PORT || 25575,
    'host': process.env.HOST || 'minecraft-server.zifzaf.net'
}, process.env.PASSWD || '', function(err) {

    if (err) {
        console.log('authentication failure');

        close();
    } else {

        command({ 'id': 14, 'message': 'list' }, function(response) {
            console.dir(response);
        });
        command({ 'id': 12, 'message': 'whitelist list' }, function(response) {
            console.dir(response);
        });

        close();
    }
});

@@ -81,24 +81,1537 @@
 tils.js');%0A%0A
+module.exports = function(options) %7B%0A%0A    var rcon_client;%0A    var response_handlers = %7B%7D;%0A%0A    var connect = function(passwd, callback) %7B%0A        rcon_client = net.connect(options, function() %7B%0A%0A            console.error('connection established');%0A%0A            rcon_client.on('data', function(data) %7B%0A                var response = packet_utils.response(data);%0A%0A                if (response.id === -1) %7B%0A                    return callback(new Error('command failure'));%0A                %7D else %7B%0A                    if (response_handlers.hasOwnProperty(response.id)) %7B%0A                        response_handlers%5Bresponse.id%5D(response);%0A                        delete response_handlers%5Bresponse.id%5D;%0A                    %7D else %7B%0A                        console.log('no handler for id: %25s', response.id);%0A                    %7D%0A                %7D%0A            %7D);%0A%0A            rcon_client.on('end', function () %7B%0A                console.error('connection ended');%0A            %7D);%0A%0A            response_handlers%5B0%5D = function(response) %7B%0A                return callback();%0A            %7D;%0A            rcon_client.write(packet_utils.auth(%7B 'id': 0, 'passwd': passwd %7D));%0A        %7D);%0A    %7D;%0A%0A    var close = function() %7B%0A        rcon_client.end();%0A    %7D;%0A%0A    var command = function (packet, handler) %7B%0A        response_handlers%5Bpacket.id%5D = handler;%0A        rcon_client.write(packet_utils.command(packet));%0A    %7D;%0A%0A    return %7B%0A        'connect': connect,%0A        'close': close,%0A        'command': command%0A    %7D;%0A%7D;%0A%0A/*%0A
 var rcon_cli
@@ -3362,12 +3362,14 @@
 ;%0A    %7D%0A%7D);%0A
+*/

rework to library