From d1b3e8b3777af1b391545b05cebd3929bf3c6e09 Mon Sep 17 00:00:00 2001 From: Vinicius Daly Felizardo Date: Thu, 20 Jun 2013 03:20:48 -0400 Subject: [PATCH] Finished /encoding command to change encoding on the fly --- client/assets/src/models/application.js | 8 ++++++++ client/assets/src/models/gateway.js | 21 +++++++++++++++++---- config.example.js | 6 ++++++ server/clientcommands.js | 10 ++++++++++ server/irc/connection.js | 20 ++++++++++++++++++-- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/client/assets/src/models/application.js b/client/assets/src/models/application.js index 79f2310..a3b1392 100644 --- a/client/assets/src/models/application.js +++ b/client/assets/src/models/application.js @@ -461,6 +461,7 @@ _kiwi.model.Application = function () { controlbox.on('command:whowas', whowasCommand); + controlbox.on('command:encoding', encodingCommand); controlbox.on('command:css', function (ev) { var queryString = '?reload=' + new Date().getTime(); @@ -789,6 +790,13 @@ _kiwi.model.Application = function () { _kiwi.app.connections.active_connection.gateway.raw('WHOWAS ' + nick); } + function encodingCommand (ev) { + if (ev.params[0]) { + _kiwi.gateway.setEncoding(null, ev.params[0], function (msg) { + _kiwi.app.panels().active.addMsg('', msg); + }); + } + } function serverCommand (ev) { var server, port, ssl, password, nick, diff --git a/client/assets/src/models/gateway.js b/client/assets/src/models/gateway.js index 7074108..a13771c 100644 --- a/client/assets/src/models/gateway.js +++ b/client/assets/src/models/gateway.js @@ -247,8 +247,6 @@ _kiwi.model.Gateway = function () { }); }; - - this.isConnected = function () { return this.socket.socket.connected; }; @@ -535,6 +533,22 @@ _kiwi.model.Gateway = function () { this.sendData(connection_id, data, callback); }; + /** + * Sends ENCODING change request to server. + * @param {String} new_encoding The new proposed encode + * @param {Fucntion} callback A callback function + */ + this.setEncoding = function (connection_id, new_encoding, callback) { + var data = { + method: 'encoding', + args: { + encoding: new_encoding + } + }; + + this.sendData(connection_id, data, callback); + }; + /** * Sends data to a fellow Kiwi IRC user * @param {String} target The nick of the Kiwi IRC user to send to @@ -553,7 +567,6 @@ _kiwi.model.Gateway = function () { this.sendData(data, callback); }; - // Check a nick alongside our ignore list this.isNickIgnored = function (nick) { var idx, list = this.get('ignore_list'); @@ -573,4 +586,4 @@ _kiwi.model.Gateway = function () { return new (Backbone.Model.extend(this))(arguments); -}; \ No newline at end of file +}; diff --git a/config.example.js b/config.example.js index 9732497..626b804 100644 --- a/config.example.js +++ b/config.example.js @@ -60,6 +60,12 @@ conf.max_client_conns = 5; // - Kiwi is running in restricted server mode. conf.max_server_conns = 0; +/* +* Available encodings supported by the server +* As specified and limited to iconv-lite library support. +* All upper-case. Default is the first element on the list. +*/ +conf.available_encodings = ['UTF-8', 'WINDOWS-1252']; /* * Client side plugins diff --git a/server/clientcommands.js b/server/clientcommands.js index 0aed72d..9448611 100644 --- a/server/clientcommands.js +++ b/server/clientcommands.js @@ -154,5 +154,15 @@ var listeners = { if ((args.target) && (args.data)) { irc_connection.write('PRIVMSG ' + args.target + ': ' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1), callback); } + }, + + ENCODING: function (args, irc_connection, callback) { + if (args.encoding) { + if (irc_connection.setEncoding(args.encoding)) { + return callback('Encoding modified to '+args.encoding); + } else { + return callback(args.encoding+' is not a valid encoding'); + } + } } }; diff --git a/server/irc/connection.js b/server/irc/connection.js index a7202bd..3e0f75a 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -30,6 +30,9 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { }); this.setMaxListeners(0); + // Set the first configured encoding as the default encoding + this.encoding = global.config.available_encodings[0]; + // Socket state this.connected = false; @@ -198,7 +201,7 @@ IrcConnection.prototype.clientEvent = function (event_name, data, callback) { */ IrcConnection.prototype.write = function (data, callback) { //ENCODE string to encoding of the server - encoded_buffer = iconv.encode(data + '\r\n', "windows-1252"); + encoded_buffer = iconv.encode(data + '\r\n', this.encoding); this.socket.write(encoded_buffer); }; @@ -251,6 +254,18 @@ IrcConnection.prototype.disposeSocket = function () { } }; +/** + * Set a new encoding for this connection + * Return true in case of success + */ + +IrcConnection.prototype.setEncoding = function (encoding) { + if (global.config.available_encodings.indexOf(encoding.toUpperCase()) >= 0) { + this.encoding = encoding.toUpperCase(); + return true; + } + return false; +}; function onChannelJoin(event) { @@ -426,8 +441,9 @@ var parse = function (data) { tag; //DECODE server encoding - data = iconv.decode(data, 'windows-1252'); + data = iconv.decode(data, this.encoding); console.log(data); + if (this.hold_last && this.held_data !== '') { data = this.held_data + data; this.hold_last = false; -- 2.25.1