Kiwi client/server API fully reverted
authorDarren <darren@darrenwhitlen.com>
Thu, 20 Sep 2012 23:38:17 +0000 (00:38 +0100)
committerDarren <darren@darrenwhitlen.com>
Thu, 20 Sep 2012 23:38:17 +0000 (00:38 +0100)
client_backbone/dev/model_gateway.js
server/client-commands.js [new file with mode: 0644]
server/client.js

index 846044919f7760ee6fd653e3e360eec1635dab2a..75dc73b9e7e73ff5064e38938359177d8279ae12 100755 (executable)
-kiwi.model.Gateway = Backbone.Model.extend(new (function () {
-    var that = this;
-
-    this.defaults = {
-        /**
-        *   The name of the network
-        *   @type    String
-        */
-        name: 'Server',
-
-        /**
-        *   The address (URL) of the network
-        *   @type    String
-        */
-        address: '',
-
-        /**
-        *   The current nickname
-        *   @type   String
-        */
-        nick: '',
-
-        /**
-        *   The channel prefix for this network
-        *   @type    String
-        */
-        channel_prefix: '#',
-
-        /**
-        *   The user prefixes for channel owner/admin/op/voice etc. on this network
-        *   @type   Array
-        */
-        user_prefixes: ['~', '&', '@', '+'],
-
-        /**
-        *   The URL to the Kiwi server
-        *   @type   String
-        */
-        //kiwi_server: '//kiwi'
-        kiwi_server: document.location.protocol + '//' + document.location.host + '/kiwi'
-    };
-
-
-    this.initialize = function () {
-        // Update `that` with this new Model object
-        that = this;
-
-        // For ease of access. The socket.io object
-        this.socket = this.get('socket');
-
-        this.server_num = null;
-
-        // Global variable? ~Jack
-        network = this;
-    };
-
-
-    /**
-    *   Connects to the server
-    *   @param  {String}    host        The hostname or IP address of the IRC server to connect to
-    *   @param  {Number}    port        The port of the IRC server to connect to
-    *   @param  {Boolean}   ssl         Whether or not to connect to the IRC server using SSL
-    *   @param  {String}    password    The password to supply to the IRC server during registration
-    *   @param  {Function}  callback    A callback function to be invoked once Kiwi's server has connected to the IRC server
-    */
-    this.connect = function (host, port, ssl, password, callback) {
-        this.socket = io.connect(this.get('kiwi_server'), {
-            'try multiple transports': true,
-            'connect timeout': 3000,
-            'max reconnection attempts': 7,
-            'reconnection delay': 2000
-        });
-        this.socket.on('connect_failed', function (reason) {
-            // TODO: When does this even actually get fired? I can't find a case! ~Darren
-            console.debug('Unable to connect Socket.IO', reason);
-            console.log("kiwi.gateway.socket.on('connect_failed')");
-            //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');
-            this.socket.disconnect();
-            this.emit("connect_fail", {reason: reason});
-        });
-
-        this.socket.on('error', function (e) {
-            this.emit("connect_fail", {reason: e});
-            console.log("kiwi.gateway.socket.on('error')", {reason: e});
-        });
-
-        this.socket.on('connecting', function (transport_type) {
-            console.log("kiwi.gateway.socket.on('connecting')");
-            this.emit("connecting");
-        });
-
-        this.socket.on('connect', function () {
-            //{command: 'connect', nick: kiwi.gateway.nick, hostname: host, port: port, ssl: ssl, password: password}
-            this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {
-                console.log('err, server_num', err, server_num);
-                if (!err) {
-                    that.server_num = server_num;
-                    console.log("kiwi.gateway.socket.on('connect')");
-                } else {
-                    console.log("kiwi.gateway.socket.on('error')", {reason: err});
-                }
-            });
-        });
-
-        this.socket.on('too_many_connections', function () {
-            this.emit("connect_fail", {reason: 'too_many_connections'});
-        });
-
-        this.socket.on('irc', function (data, callback) {
-            that.parse(data.command, data.data);
-        });
-
-        this.socket.on('disconnect', function () {
-            this.emit("disconnect", {});
-            console.log("kiwi.gateway.socket.on('disconnect')");
-        });
-
-        this.socket.on('close', function () {
-            console.log("kiwi.gateway.socket.on('close')");
-        });
-
-        this.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) {
-            console.log("kiwi.gateway.socket.on('reconnecting')");
-            this.emit("reconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts});
-        });
-
-        this.socket.on('reconnect_failed', function () {
-            console.log("kiwi.gateway.socket.on('reconnect_failed')");
-        });
-    };
-
-
-    /*
-        Events:
-            msg
-            action
-            server_connect
-            options
-            motd
-            notice
-            userlist
-            nick
-            join
-            topic
-            part
-            kick
-            quit
-            whois
-            syncchannel_redirect
-            debug
-    */
-    /**
-    *   Parses the response from the server
-    */
-    this.parse = function (command, data) {
-        console.log('gateway event', command, data);
-        if (command !== undefined) {
-            that.trigger('on' + command, data);
-
-            switch (command) {
-            case 'options':
-                $.each(data.options, function (name, value) {
-                    switch (name) {
-                    case 'CHANTYPES':
-                        // TODO: Check this. Why is it only getting the first char?
-                        that.set('channel_prefix', value.join('').charAt(0));
-                        break;
-                    case 'NETWORK':
-                        that.set('name', value);
-                        break;
-                    case 'PREFIX':
-                        that.set('user_prefixes', value);
-                        break;
-                    }
-                });
-                break;
-
-            case 'connect':
-                that.set('nick', data.nick);
-                break;
-
-            case 'nick':
-                that.set('nick', data.newnick);
-                break;
-            /*
-            case 'sync':
-                if (kiwi.gateway.onSync && kiwi.gateway.syncing) {
-                    kiwi.gateway.syncing = false;
-                    kiwi.gateway.onSync(item);
-                }
-                break;
-            */
-
-            case 'kiwi':
-                this.emit('kiwi.' + data.namespace, data);
-                break;
-            }
-        }
-    };
-
-    /**
-    *   Sends data to the server
-    *   @private
-    *   @param  {Object}    data        The data to send
-    *   @param  {Function}  callback    A callback function
-    */
-    this.sendData = function (data, callback) {
-        //this.socket.emit('message', {sid: this.session_id, data: JSON.stringify(data)}, callback);
-        //kiwi.gateway.socket.emit('irc', {server: this.server_num, data: $.toJSON(data)}, callback);
-        this.socket.emit('irc', {server: this.server_num, data: JSON.stringify(data)}, callback);
-    };
-
-    /**
-    *   Sends a PRIVMSG message
-    *   @param  {String}    target      The target of the message (e.g. a channel or nick)
-    *   @param  {String}    msg         The message to send
-    *   @param  {Function}  callback    A callback function
-    */
-    this.privmsg = function (target, msg, callback) {
-        var data = {
-            method: 'privmsg',
-            args: {
-                params: [target],
-                trailing: msg
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Sends a NOTICE message
-    *   @param  {String}    target      The target of the message (e.g. a channel or nick)
-    *   @param  {String}    msg         The message to send
-    *   @param  {Function}  callback    A callback function
-    */
-    this.notice = function (target, msg, callback) {
-        var data = {
-            method: 'notice',
-            args: {
-                params: [target],
-                trailing: msg
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Sends a CTCP message
-    *   @param  {Boolean}   request     Indicates whether this is a CTCP request (true) or reply (false)
-    *   @param  {String}    type        The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.
-    *   @param  {String}    target      The target of the message, e.g a channel or nick
-    *   @param  {String}    params      Additional paramaters
-    *   @param  {Function}  callback    A callback function
-    */
-    this.ctcp = function (request, type, target, params, callback) {
-        var data = {
-            method: 'ctcp',
-            args: {
-                request: request,
-                type: type,
-                target: target,
-                params: params
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   @param  {String}    target      The target of the message (e.g. a channel or nick)
-    *   @param  {String}    msg         The message to send
-    *   @param  {Function}  callback    A callback function
-    */
-    this.action = function (target, msg, callback) {
-        this.ctcp(true, 'ACTION', target, msg, callback);
-    };
-
-    /**
-    *   Joins a channel
-    *   @param  {String}    channel     The channel to join
-    *   @param  {String}    key         The key to the channel
-    *   @param  {Function}  callback    A callback function
-    */
-    this.join = function (channel, key, callback) {
-        var data = {
-            method: 'join',
-            args: {
-                params: [channel, key]
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Leaves a channel
-    *   @param  {String}    channel     The channel to part
-    *   @param  {Function}  callback    A callback function
-    */
-    this.part = function (channel, callback) {
-        var data = {
-            method: 'part',
-            args: {
-                params: channel
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Queries or modifies a channell topic
-    *   @param  {String}    channel     The channel to query or modify
-    *   @param  {String}    new_topic   The new topic to set
-    *   @param  {Function}  callback    A callback function
-    */
-    this.topic = function (channel, new_topic, callback) {
-        var data = {
-            method: 'topic',
-            args: {
-                params: [channel],
-                trailing: new_topic
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Kicks a user from a channel
-    *   @param  {String}    channel     The channel to kick the user from
-    *   @param  {String}    nick        The nick of the user to kick
-    *   @param  {String}    reason      The reason for kicking the user
-    *   @param  {Function}  callback    A callback function
-    */
-    this.kick = function (channel, nick, reason, callback) {
-        var data = {
-            method: 'kick',
-            args: {
-                params: [channel, nick],
-                trailing: reason
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Disconnects us from the server
-    *   @param  {String}    msg         The quit message to send to the IRC server
-    *   @param  {Function}   callback    A callback function
-    */
-    this.quit = function (msg, callback) {
-        msg = msg || "";
-        var data = {
-            method: 'quit',
-            args: {
-                trailing: msg
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Sends a string unmodified to the IRC server
-    *   @param  {String}    data        The data to send to the IRC server
-    *   @param  {Function}  callback    A callback function
-    */
-    this.raw = function (data, callback) {
-        data = {
-            method: 'raw',
-            args: {
-                data: data
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Changes our nickname
-    *   @param  {String}    new_nick    Our new nickname
-    *   @param  {Function}  callback    A callback function
-    */
-    this.changeNick = function (new_nick, callback) {
-        var data = {
-            method: 'nick',
-            args: {
-                params: [new_nick]
-            }
-        };
-
-        this.sendData(data, callback);
-    };
-
-    /**
-    *   Sends data to a fellow Kiwi IRC user
-    *   @param  {String}    target      The nick of the Kiwi IRC user to send to
-    *   @param  {String}    data        The data to send
-    *   @param  {Function}  callback    A callback function
-    */
-    this.kiwi = function (target, data, callback) {
-        data = {
-            method: 'kiwi',
-            args: {
-                target: target,
-                data: data
-            }
-        };
-
-        this.sendData(data, callback);
-    };
+kiwi.model.Gateway = Backbone.Model.extend(new (function () {\r
+    var that = this;\r
+\r
+    this.defaults = {\r
+        /**\r
+        *   The name of the network\r
+        *   @type    String\r
+        */\r
+        name: 'Server',\r
+\r
+        /**\r
+        *   The address (URL) of the network\r
+        *   @type    String\r
+        */\r
+        address: '',\r
+\r
+        /**\r
+        *   The current nickname\r
+        *   @type   String\r
+        */\r
+        nick: '',\r
+\r
+        /**\r
+        *   The channel prefix for this network\r
+        *   @type    String\r
+        */\r
+        channel_prefix: '#',\r
+\r
+        /**\r
+        *   The user prefixes for channel owner/admin/op/voice etc. on this network\r
+        *   @type   Array\r
+        */\r
+        user_prefixes: ['~', '&', '@', '+'],\r
+\r
+        /**\r
+        *   The URL to the Kiwi server\r
+        *   @type   String\r
+        */\r
+        kiwi_server: '//kiwi'\r
+    };\r
+\r
+\r
+    this.initialize = function () {\r
+        // Update `that` with this new Model object\r
+        that = this;\r
+\r
+        // For ease of access. The socket.io object\r
+        this.socket = this.get('socket');\r
+\r
+        // Redundant perhaps? Legacy\r
+        this.session_id = '';\r
+\r
+        network = this;\r
+    };\r
+\r
+\r
+    /**\r
+    *   Connects to the server\r
+    *   @param  {String}    host        The hostname or IP address of the IRC server to connect to\r
+    *   @param  {Number}    port        The port of the IRC server to connect to\r
+    *   @param  {Boolean}   ssl         Whether or not to connect to the IRC server using SSL\r
+    *   @param  {String}    password    The password to supply to the IRC server during registration\r
+    *   @param  {Function}  callback    A callback function to be invoked once Kiwi's server has connected to the IRC server\r
+    */\r
+    this.connect = function (host, port, ssl, password, callback) {\r
+        this.socket = io.connect(this.get('kiwi_server'), {\r
+            'try multiple transports': true,\r
+            'connect timeout': 3000,\r
+            'max reconnection attempts': 7,\r
+            'reconnection delay': 2000\r
+        });\r
+        this.socket.on('connect_failed', function (reason) {\r
+            // TODO: When does this even actually get fired? I can't find a case! ~Darren\r
+            console.debug('Unable to connect Socket.IO', reason);\r
+            console.log("kiwi.gateway.socket.on('connect_failed')");\r
+            //kiwi.front.tabviews.server.addMsg(null, ' ', 'Unable to connect to Kiwi IRC.\n' + reason, 'error');\r
+            this.socket.disconnect();\r
+            this.emit("connect_fail", {reason: reason});\r
+        });\r
+\r
+        this.socket.on('error', function (e) {\r
+            this.emit("connect_fail", {reason: e});\r
+            console.log("kiwi.gateway.socket.on('error')", {reason: e});\r
+        });\r
+\r
+        this.socket.on('connecting', function (transport_type) {\r
+            console.log("kiwi.gateway.socket.on('connecting')");\r
+            this.emit("connecting");\r
+            that.trigger("connecting");\r
+        });\r
+\r
+        this.socket.on('connect', function () {\r
+            this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {\r
+                console.log('err, server_num', err, server_num);\r
+                if (!err) {\r
+                    that.server_num = server_num;\r
+                    console.log("kiwi.gateway.socket.on('connect')");\r
+                } else {\r
+                    console.log("kiwi.gateway.socket.on('error')", {reason: err});\r
+                }\r
+            });\r
+            that.trigger('connect', {});\r
+        });\r
+\r
+        this.socket.on('too_many_connections', function () {\r
+            this.emit("connect_fail", {reason: 'too_many_connections'});\r
+        });\r
+\r
+        this.socket.on('irc', function (data, callback) {\r
+            that.parse(data.command, data.data);\r
+        });\r
+\r
+        this.socket.on('disconnect', function () {\r
+            that.trigger("disconnect", {});\r
+            console.log("kiwi.gateway.socket.on('disconnect')");\r
+        });\r
+\r
+        this.socket.on('close', function () {\r
+            console.log("kiwi.gateway.socket.on('close')");\r
+        });\r
+\r
+        this.socket.on('reconnecting', function (reconnectionDelay, reconnectionAttempts) {\r
+            console.log("kiwi.gateway.socket.on('reconnecting')");\r
+            that.trigger("reconnecting", {delay: reconnectionDelay, attempts: reconnectionAttempts});\r
+        });\r
+\r
+        this.socket.on('reconnect_failed', function () {\r
+            console.log("kiwi.gateway.socket.on('reconnect_failed')");\r
+        });\r
+    };\r
+\r
+\r
+    /*\r
+        Events:\r
+            msg\r
+            action\r
+            server_connect\r
+            options\r
+            motd\r
+            notice\r
+            userlist\r
+            nick\r
+            join\r
+            topic\r
+            part\r
+            kick\r
+            quit\r
+            whois\r
+            syncchannel_redirect\r
+            debug\r
+    */\r
+    /**\r
+    *   Parses the response from the server\r
+    */\r
+    this.parse = function (command, data) {\r
+        //console.log('gateway event', command, data);\r
+        if (command !== undefined) {\r
+            that.trigger('on' + command, data);\r
+\r
+            switch (command) {\r
+            case 'options':\r
+                $.each(data.options, function (name, value) {\r
+                    switch (name) {\r
+                    case 'CHANTYPES':\r
+                        // TODO: Check this. Why is it only getting the first char?\r
+                        that.set('channel_prefix', value.join('').charAt(0));\r
+                        break;\r
+                    case 'NETWORK':\r
+                        that.set('name', value);\r
+                        break;\r
+                    case 'PREFIX':\r
+                        that.set('user_prefixes', value);\r
+                        break;\r
+                    }\r
+                });\r
+                break;\r
+\r
+            case 'connect':\r
+                that.set('nick', data.nick);\r
+                break;\r
+\r
+            case 'nick':\r
+                if (data.nick === that.get('nick')) {\r
+                    that.set('nick', data.newnick);\r
+                }\r
+                break;\r
+            /*\r
+            case 'sync':\r
+                if (kiwi.gateway.onSync && kiwi.gateway.syncing) {\r
+                    kiwi.gateway.syncing = false;\r
+                    kiwi.gateway.onSync(item);\r
+                }\r
+                break;\r
+            */\r
+\r
+            case 'kiwi':\r
+                this.emit('kiwi.' + data.namespace, data.data);\r
+                break;\r
+            }\r
+        }\r
+    };\r
+\r
+    /**\r
+    *   Sends data to the server\r
+    *   @private\r
+    *   @param  {Object}    data        The data to send\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.sendData = function (data, callback) {\r
+        this.socket.emit('irc', {server: 0, data: JSON.stringify(data)}, callback);\r
+    };\r
+\r
+    /**\r
+    *   Sends a PRIVMSG message\r
+    *   @param  {String}    target      The target of the message (e.g. a channel or nick)\r
+    *   @param  {String}    msg         The message to send\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.privmsg = function (target, msg, callback) {\r
+        var data = {\r
+            method: 'privmsg',\r
+            args: {\r
+                target: target,\r
+                msg: msg\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Sends a NOTICE message\r
+    *   @param  {String}    target      The target of the message (e.g. a channel or nick)\r
+    *   @param  {String}    msg         The message to send\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.notice = function (target, msg, callback) {\r
+        var data = {\r
+            method: 'notice',\r
+            args: {\r
+                target: target,\r
+                msg: msg\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Sends a CTCP message\r
+    *   @param  {Boolean}   request     Indicates whether this is a CTCP request (true) or reply (false)\r
+    *   @param  {String}    type        The type of CTCP message, e.g. 'VERSION', 'TIME', 'PING' etc.\r
+    *   @param  {String}    target      The target of the message, e.g a channel or nick\r
+    *   @param  {String}    params      Additional paramaters\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.ctcp = function (request, type, target, params, callback) {\r
+        var data = {\r
+            method: 'ctcp',\r
+            args: {\r
+                request: request,\r
+                type: type,\r
+                target: target,\r
+                params: params\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   @param  {String}    target      The target of the message (e.g. a channel or nick)\r
+    *   @param  {String}    msg         The message to send\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.action = function (target, msg, callback) {\r
+        this.ctcp(true, 'ACTION', target, msg, callback);\r
+    };\r
+\r
+    /**\r
+    *   Joins a channel\r
+    *   @param  {String}    channel     The channel to join\r
+    *   @param  {String}    key         The key to the channel\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.join = function (channel, key, callback) {\r
+        var data = {\r
+            method: 'join',\r
+            args: {\r
+                channel: channel,\r
+                key: key\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Leaves a channel\r
+    *   @param  {String}    channel     The channel to part\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.part = function (channel, callback) {\r
+        var data = {\r
+            method: 'part',\r
+            args: {\r
+                channel: channel\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Queries or modifies a channell topic\r
+    *   @param  {String}    channel     The channel to query or modify\r
+    *   @param  {String}    new_topic   The new topic to set\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.topic = function (channel, new_topic, callback) {\r
+        var data = {\r
+            method: 'topic',\r
+            args: {\r
+                channel: channel,\r
+                topic: new_topic\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Kicks a user from a channel\r
+    *   @param  {String}    channel     The channel to kick the user from\r
+    *   @param  {String}    nick        The nick of the user to kick\r
+    *   @param  {String}    reason      The reason for kicking the user\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.kick = function (channel, nick, reason, callback) {\r
+        var data = {\r
+            method: 'kick',\r
+            args: {\r
+                channel: channel,\r
+                nick: nick,\r
+                reason: reason\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Disconnects us from the server\r
+    *   @param  {String}    msg         The quit message to send to the IRC server\r
+    *   @param  {Function}   callback    A callback function\r
+    */\r
+    this.quit = function (msg, callback) {\r
+        msg = msg || "";\r
+        var data = {\r
+            method: 'quit',\r
+            args: {\r
+                message: msg\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Sends a string unmodified to the IRC server\r
+    *   @param  {String}    data        The data to send to the IRC server\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.raw = function (data, callback) {\r
+        data = {\r
+            method: 'raw',\r
+            args: {\r
+                data: data\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Changes our nickname\r
+    *   @param  {String}    new_nick    Our new nickname\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.changeNick = function (new_nick, callback) {\r
+        var data = {\r
+            method: 'nick',\r
+            args: {\r
+                nick: new_nick\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
+\r
+    /**\r
+    *   Sends data to a fellow Kiwi IRC user\r
+    *   @param  {String}    target      The nick of the Kiwi IRC user to send to\r
+    *   @param  {String}    data        The data to send\r
+    *   @param  {Function}  callback    A callback function\r
+    */\r
+    this.kiwi = function (target, data, callback) {\r
+        data = {\r
+            method: 'kiwi',\r
+            args: {\r
+                target: target,\r
+                data: data\r
+            }\r
+        };\r
+\r
+        this.sendData(data, callback);\r
+    };\r
 })());
\ No newline at end of file
diff --git a/server/client-commands.js b/server/client-commands.js
new file mode 100644 (file)
index 0000000..4f0b3f7
--- /dev/null
@@ -0,0 +1,123 @@
+var _ = require('underscore');\r
+\r
+\r
+\r
+\r
+var ClientCommandset = function (client) {\r
+    this.client = client;\r
+};\r
+module.exports.ClientCommandset = ClientCommandset;\r
+\r
+ClientCommandset.prototype.run = function (command, args, irc_connection, callback) {\r
+    // Do we have a function to handle this command?\r
+    if (!listeners[command.toUpperCase()]) {\r
+        return;\r
+    }\r
+\r
+    return listeners[command.toUpperCase()](args, irc_connection, callback);\r
+};\r
+\r
+\r
+\r
+\r
+var listeners = {\r
+    PRIVMSG: function (args, irc_connection, callback) {\r
+        if (args.target && (args.msg)) {\r
+            // TODO: Enable plugin support here again\r
+            //obj = kiwi.kiwi_mod.run('msgsend', args, {websocket: websocket});\r
+            //if (obj !== null) {\r
+                irc_connection.write('PRIVMSG ' + args.target + ' :' + args.msg, callback);\r
+            //}\r
+        }\r
+    },\r
+    \r
+\r
+    CTCP: function (args, irc_connection, callback) {\r
+        if ((args.target) && (args.type)) {\r
+            if (args.request) {\r
+                irc_connection.write('PRIVMSG ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);\r
+            } else {\r
+                irc_connection.write('NOTICE ' + args.target + ' :' + String.fromCharCode(1) + args.type.toUpperCase() + ' ' + args.params + String.fromCharCode(1), callback);\r
+            }\r
+        }\r
+    },\r
+\r
+\r
+    RAW: function (args, irc_connection, callback) {\r
+        irc_connection.write(args.data, callback);\r
+    },\r
+\r
+\r
+    JOIN: function (args, irc_connection, callback) {\r
+        if (args.channel) {\r
+            channels = args.channel.split(",");\r
+            keys = (args.key) ? args.key.split(",") : [];\r
+            _.each(channels, function (chan, index) {\r
+                irc_connection.write('JOIN ' + chan + ' ' + (keys[index] || ''), callback);\r
+            });\r
+        }\r
+    },\r
+\r
+\r
+    PART: function (args, irc_connection, callback) {\r
+        if (args.channel) {\r
+            _.each(args.channel.split(","), function (chan) {\r
+                irc_connection.write('PART ' + chan, callback);\r
+            });\r
+        }\r
+    },\r
+\r
+\r
+    TOPIC: function (args, irc_connection, callback) {\r
+        if (args.channel) {\r
+            if (args.topic) {\r
+                irc_connection.write('TOPIC ' + args.channel + ' :' + args.topic, callback);\r
+            } else {\r
+                irc_connection.write('TOPIC ' + args.channel, callback);\r
+            }\r
+        }\r
+    },\r
+\r
+\r
+    KICK: function (args, irc_connection, callback) {\r
+        if ((args.channel) && (args.nick)) {\r
+            irc_connection.write('KICK ' + args.channel + ' ' + args.nick + ':' + args.reason, callback);\r
+        }\r
+    },\r
+\r
+\r
+    QUIT: function (args, irc_connection, callback) {\r
+        websocket.ircConnection.end('QUIT :' + args.message + '\r\n');\r
+        websocket.sentQUIT = true;\r
+        websocket.ircConnection.destroySoon();\r
+        websocket.disconnect();\r
+    },\r
+\r
+\r
+    NOTICE: function (args, irc_connection, callback) {\r
+        if ((args.target) && (args.msg)) {\r
+            irc_connection.write('NOTICE ' + args.target + ' :' + args.msg, callback);\r
+        }\r
+    },\r
+\r
+\r
+    MODE: function (args, irc_connection, callback) {\r
+        if ((args.target) && (args.mode)) {\r
+            irc_connection.write('MODE ' + args.target + ' ' + args.mode + ' ' + args.params, callback);\r
+        }\r
+    },\r
+\r
+\r
+    NICK: function (args, irc_connection, callback) {\r
+        if (args.nick) {\r
+            irc_connection.write('NICK ' + args.nick, callback);\r
+        }\r
+    },\r
+\r
+\r
+    KIWI:  function (args, irc_connection, callback) {\r
+        if ((args.target) && (args.data)) {\r
+            irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback);\r
+        }\r
+    }\r
+};\r
index 4276043c77eb1e5110b25d6650cde673fb4a4ba2..f5c124a5b2bf41588ae9ce789fe55deacb9de652 100755 (executable)
@@ -1,7 +1,8 @@
-var util            = require('util'),
-    events          = require('events'),
-    IRCConnection   = require('./irc-connection.js').IRCConnection;
-    IRCCommands     = require('./irc-commands.js');
+var util             = require('util'),
+    events           = require('events'),
+    IRCConnection    = require('./irc-connection.js').IRCConnection;
+    IRCCommands      = require('./irc-commands.js'),
+    ClientCommandset = require('./client-commands.js').ClientCommandset;
 
 var Client = function (websocket) {
     var c = this;
@@ -17,8 +18,11 @@ var Client = function (websocket) {
         motd: ''
     };
     
+    // Handler for any commands sent from the client
+    this.client_commands = new ClientCommandset(this);
+
     websocket.on('irc', function () {
-        IRC_command.apply(c, arguments);
+        handleClientMessage.apply(c, arguments);
     });
     websocket.on('kiwi', function () {
         kiwi_command.apply(c, arguments);
@@ -50,45 +54,37 @@ Client.prototype.sendKiwiCommand = function (command, callback) {
     this.websocket.emit('kiwi', command, callback);
 };
 
-var IRC_command = function (command, callback) {
-    console.log('C-->', command);
-    var method, str = '';
-    if (typeof callback !== 'function') {
-        callback = function () {};
-    }
-    if ((command.server === null) || (typeof command.server !== 'number')) {
+var handleClientMessage = function (msg, callback) {
+    var server, args, obj, channels, keys;
+
+    // Make sure we have a server number specified
+    if ((msg.server === null) || (typeof msg.server !== 'number')) {
         return callback('server not specified');
-    } else if (!this.IRC_connections[command.server]) {
+    } else if (!this.IRC_connections[msg.server]) {
         return callback('not connected to server');
     }
-    
-    command.data = JSON.parse(command.data);
-    
-    if (!_.isArray(command.data.args.params)){
-        command.data.args.params = [command.data.args.params];
+
+    // The server this command is directed to
+    server = this.IRC_connections[msg.server];
+
+    if (typeof callback !== 'function') {
+        callback = null;
     }
-    
-    if (command.data.method === 'ctcp') {
-        if (command.data.args.request) {
-            str += 'PRIVMSG ';
-        } else {
-            str += 'NOTICE ';
-        }
-        str += command.data.args.target + ' :'
-        str += String.fromCharCode(1) + command.data.args.type + ' ';
-        str += command.data.args.params + String.fromCharCode(1);
-        this.IRC_connections[command.server].write(str);
-    } else if (command.data.method === 'raw') {
-        this.IRC_connections[command.server].write(command.data.args.data);
-    } else if (command.data.method === 'kiwi') {
-        // do special Kiwi stuff here
-    } else {
-        method = command.data.method;
-        command.data = command.data.args;
-        this.IRC_connections[command.server].write(method + ((command.data.params) ? ' ' + command.data.params.join(' ') : '') + ((command.data.trailing) ? ' :' + command.data.trailing : ''), callback);
+
+    try {
+        msg.data = JSON.parse(msg.data);
+    } catch (e) {
+        kiwi.log('[handleClientMessage] JSON parsing error ' + msg.data);
+        return;
     }
+
+    // Run the client command
+    this.client_commands.run(msg.data.method, msg.data.args, server, callback);
 };
 
+
+
+
 var kiwi_command = function (command, callback) {
     var that = this;
     console.log(typeof callback);