var prompt = require('prompt');
var plugapi = require('plugapi');

prompt.message = '';
prompt.delimiter = '';

var credentials_schema = {
    properties: {
        email: {
            description: 'Email:'.cyan.bold,
            pattern: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
            message: 'Please enter a valid email adress'.red.bold,
            required: true
        },
        password: {
            description: 'Password:'.cyan.bold,
            hidden: true,
            required: true
        }
    }
};

var room_schema = {
    properties: {
        room: {
            description: 'Room:'.cyan.bold,
            required: true
        }
    }
};

var bot = {};

bot.init = function(){
    console.log('Please enter the login credentials for the bot:');
    
    prompt.get(credentials_schema, function(err, result){
        if(err){ console.log(err); return; }
        
        bot.email = result.email;
        bot.password = result.password;
        bot.room = result.room;
        
        bot.login();
    });
};

bot.login = function(){
    new plugapi({
        "email": bot.email,
        "password": bot.password
    }, function(api){
        bot.api = api;
    });
    
    var i = 0;
    
    process.stdout.write('Connecting');
    
    (function check(){
        if(bot.api === undefined){
            if(i++ < 10){
                process.stdout.write('.');
                setTimeout(check, 1000);
            } else {
                console.log('\nCould not login with the credentials you supplied.');
                bot.init();
            }
        } else {
            process.stdout.write('\n');
            bot.prompt_room();
        }
    })();
};

bot.prompt_room = function(){
    console.log('Which room do you want the bot to join?');
    prompt.get(room_schema, function(err, result){
        if(err){ console.log(err); return; }
        
        bot.room = result.room;
        bot.connect();
    });
};

bot.connect = function(){
    bot.api.connect(bot.room);
};

prompt.start();

bot.init();
@@ -785,21 +785,385 @@
 %7D;%0A%0A
-var bot = %7B%7D;
+function bot_join(data)%7B%0A    if(data.length %3E 0)%7B%0A        console.log('Bot successfully joined https://plug.dj/' + data);%0A    %7D%0A%7D%0A%0Avar bot = %7B%7D;%0A%0Abot.events = Array();%0Abot.core_events = %5B%7B%0A    'event': 'roomJoin',%0A    'action': bot_join%0A%7D%5D;%0A%0Abot.connected = false;                  // Id this is true, the bot is IN the room specified in bot.room; This becomes true on joinRoom
 %0A%0Abo
@@ -1446,48 +1446,8 @@
 rd;%0A
-        bot.room = result.room;%0A        
 %0A   
@@ -2102,24 +2102,74 @@
 mpt_room();%0A
+            bot.register_events(bot.core_events);%0A
         %7D%0A  
@@ -2387,23 +2387,24 @@
     bot.
-room = 
+connect(
 result.r
@@ -2406,30 +2406,69 @@
 ult.room
+)
 ;%0A    
+%7D);%0A%7D;%0A%0Abot.connect = function(room)%7B%0A
     bot.
 connect(
@@ -2459,24 +2459,28 @@
     bot.
+api.
 connect(
 );%0A    %7D
@@ -2475,34 +2475,183 @@
 ect(
-);%0A    %7D);%0A%7D;%0A%0Abot.connect
+room);%0A%7D;%0A%0Abot.hook = function(event, action)%7B%0A    bot.api.on(event, action);%0A%7D;%0A%0Abot.unhook = function(event, action)%7B%0A    bot.api.off(event, action);%0A%7D;%0A%0Abot.register_events
  = f
@@ -2662,41 +2662,459 @@
 ion(
+events
 )%7B%0A    
-bot.api.connect(bot.room);
+var pair, i;%0A%0A    for (i = 0; i %3C events.length; i++)%7B%0A        pair = events%5Bi%5D;%0A        bot.hook(pair%5B%22event%22%5D, pair%5B%22action%22%5D);%0A        %0A        bot.events.push(events%5Bi%5D);%0A    %7D%0A%7D;%0A%0Abot.unregister_events = function(events)%7B%0A    var pair, i;%0A%0A    for (i = 0; i %3C events.length; i++)%7B%0A        pair = events%5Bi%5D;%0A        bot.unhook(pair%5B%22event%22%5D, pair%5B%22action%22%5D);%0A        %0A        bot.events.splice(bot.events.indexOf(events%5Bi%5D), 1);%0A    %7D
 %0A%7D;%0A

Add some core functionality