﻿// opt => {channel,room, socket, io, auto:t/f}

// fnc =>  joinroom(room, cb),
//         leaveroom(room, cb),
//         changeroom(room, cb),
//         watchallroom,
//         watchoneroom(room),
//         sID,
//         FileBOT(base64, kind, cb)

var fs = require('fs');

module.exports = function (opt) {
    var init = function (opt) {
        this.io = opt.io || null;
        this.channel = opt.channel || null;
        this.room = opt.room || 'default';
        this.socket = opt.socket || null;
        if (opt.auto == true) {
            return auto(this);
        } else {
            return this;
        };
    };

    //start
    var auto = function (object) {
        var socket = object.socket;
        var room = object.room;
        socket.join(room);
        return object;
    };

    //join room
    var joinroom = function (room, cb) {
        var socket = this.socket;
        socket.join(room, function () {
            cb(true);
        });
    };

    //leave room
    var leaveroom = function (room, cb) {
        var socket = this.socket;
        socket.leave(room, function () {
            cb(true);
        });
    };

    //change room will leave old room first
    var changeroom = function (room, cb) {
        var socket = this.socket;
        var oldroom = this.room;
        var newroom = room;
        socket.leave(oldroom, function () {
            socket.join(newroom, function () {
                cb(true);
            });
        });
    };

    //watch all room and users list from socket.io namespace
    var watchallroom = function () {
        var io = this.io;
        var socket = this.socket;
        var name = io.sockets.name;
        return io.nsps[name].adapter.rooms;
    };

    //watch one room and users list from socket.io namespace
    var watchoneroom = function (room) {
        var io = this.io;
        var socket = this.socket;
        var name = io.sockets.name;
        return io.nsps[name].adapter.rooms[room];
    };

    //watch the sessionID from socket.io
    var sID = function () {
        var socket = this.socket;
        return socket.handshake.sessionID;
    };

    //File System BOT Fnc
    function base64_encode(file) {
        var f = fs.readFileSync(file);
        return new Buffer(f).toString('base64');
    };
    function base64_decode(base64str, file, cb) {
        var f = new Buffer(base64str, 'base64');
        fs.writeFile(file, f, function (error) {
            if (error) {
                cb({ result: 0, info: file });
            } else {
                cb({ result: 1, info: file });
            };
        });

    };
    var FileBOT = function (data, cb) {
        var base64 = data.body;
        var kind = data.kind;
        base64_decode(base64, './files/' + kind, function (data) {
            cb(data);
        });
    };

    //Encrypt BOT Fnc
    var EncryptBOT = function () {
    };

    var channel = {
        init: init,
        joinroom: joinroom,
        leaveroom: leaveroom,
        changeroom: changeroom,
        watchallroom: watchallroom,
        watchoneroom: watchoneroom,
        sID: sID,
        FileBOT: FileBOT,
        EncryptBOT: EncryptBOT
    };

    return channel.init(opt);
};
@@ -233,25 +233,47 @@
 BOT(
-base64
+data, cb) =%3E data=%7Bfileid, body
 , kind
-, cb)
+ %7D
 %0A%0Ava
@@ -293,16 +293,48 @@
 e('fs');
+%0Avar crypto = require('crypto');
 %0A%0Amodule
@@ -2664,17 +2664,16 @@
     %7D);%0A
-%0A
     %7D;%0A 
@@ -2723,22 +2723,54 @@
     var 
-base64
+fileid = data.fileid;%0A        var body
  = data.
@@ -2828,21 +2828,19 @@
 decode(b
-ase64
+ody
 , './fil
@@ -2866,24 +2866,103 @@
 on (data) %7B%0A
+            data%5B'fileid'%5D = fileid;%0A            data%5B'response'%5D = 'FileBOT';%0A
             
@@ -3040,27 +3040,712 @@
 = function (
-) %7B
+data, cb) %7B%0A        var messageid = data.messageid;%0A        var method = data.method %7C%7C 'MD5';%0A        var encrypt = data.encrypt;%0A        var body = data.body;%0A        var response = 'EncryptBOT';%0A%0A        if (typeof encrypt == 'undefined') %7B%0A            switch (method) %7B%0A                case 'MD5':%0A                    data.body = crypto.createHash('md5').update(body).digest('hex');%0A                    data%5B'encrypt'%5D = 'MD5';%0A                    data%5B'response'%5D = 'EncryptBOT';%0A                    data%5B'result'%5D = 1;%0A                    data%5B'info'%5D = '';%0A                    cb(data);%0A                case 'SHA1':%0A            %7D;%0A%0A        %7D else %7B%0A            //decrypt%0A        %7D;
 %0A    %7D;%0A%0A   

Fix FileBOT and Add EncryptBOT