From f05fb72aaa07aeabed999a77cab34c5192b13bc1 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 18:30:16 -0400 Subject: [PATCH 1/9] weechat-protocol.js: comment object and methods --- js/weechat-protocol.js | 173 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 172 insertions(+), 1 deletion(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index b2829ae..4104fc3 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,4 +1,12 @@ +/** + * WeeChat protocol handling. + * + * This object parses messages for the WeeChat protocol. It's + * independent from the communication layer and thus may be used + * with any network mechanism. + */ var WeeChatProtocol = function() { + // specific parsing for each message type this._types = { 'chr': this._getChar, 'int': this._getInt, @@ -10,8 +18,13 @@ var WeeChatProtocol = function() { 'tim': this._getTime, 'buf': this._getString, 'arr': this._getArray, - 'htb': this._getHashTable + 'htb': this._getHashTable, + 'inl': function() { + this._warnUnimplemented('infolist'); + } }; + + // string value for some message types this._typesStr = { 'chr': this._strDirect, 'str': this._strDirect, @@ -20,6 +33,13 @@ var WeeChatProtocol = function() { 'ptr': this._strDirect }; }; + +/** + * Unsigned integer array to string. + * + * @param uia Unsigned integer array + * @return Decoded string + */ WeeChatProtocol._uia2s = function(uia) { var str = []; @@ -29,36 +49,95 @@ WeeChatProtocol._uia2s = function(uia) { return decodeURIComponent(escape(str.join(''))); }; + WeeChatProtocol.prototype = { + /** + * Warns that message parsing is not implemented for a + * specific type. + * + * @param type Message type to display + */ + _warnUnimplemented: function(type) { + console.log('Warning: ' + type + ' message parsing is not implemented'); + }, + + /** + * Reads a 3-character message type token value from current + * set data. + * + * @return Type + */ _getType: function() { var t = this._getSlice(3); return WeeChatProtocol._uia2s(new Uint8Array(t)); }, + + /** + * Runs the appropriate read routine for the specified message type. + * + * @param type Message type + * @return Data value + */ _runType: function(type) { var cb = this._types[type]; var boundCb = cb.bind(this); return boundCb(); }, + + /** + * Reads a "number as a string" token value from current set data. + * + * @return Number as a string + */ _getStrNumber: function() { var len = this._getByte(); var str = this._getSlice(len); return WeeChatProtocol._uia2s(new Uint8Array(str)); }, + + /** + * Returns the passed object. + * + * @param obj Object + * @return Passed object + */ _strDirect: function(obj) { return obj; }, + + /** + * Calls toString() on the passed object and returns the value. + * + * @param obj Object to call toString() on + * @return String value of object + */ _strToString: function(obj) { return obj.toString(); }, + + /** + * Gets the string value of an object representing the message + * value for a specified type. + * + * @param obj Object for which to get the string value + * @param type Message type + * @return String value of object + */ _objToString: function(obj, type) { var cb = this._typesStr[type]; var boundCb = cb.bind(this); return boundCb(obj); }, + + /** + * Reads an info token value from current set data. + * + * @return Info object + */ _getInfo: function() { var info = {}; info.key = this._getString(); @@ -66,6 +145,12 @@ WeeChatProtocol.prototype = { return info; }, + + /** + * Reads an hdata token value from current set data. + * + * @return Hdata object + */ _getHdata: function() { var self = this; var paths; @@ -95,14 +180,32 @@ WeeChatProtocol.prototype = { return objs; }, + + /** + * Reads a pointer token value from current set data. + * + * @return Pointer value + */ _getPointer: function() { return this._getStrNumber(); }, + + /** + * Reads a time token value from current set data. + * + * @return Time value (Date) + */ _getTime: function() { var str = this._getStrNumber(); return new Date(parseInt(str)); }, + + /** + * Reads an integer token value from current set data. + * + * @return Integer value + */ _getInt: function() { var parsedData = new Uint8Array(this._getSlice(4)); @@ -111,14 +214,32 @@ WeeChatProtocol.prototype = { ((parsedData[2] & 0xff) << 8) | (parsedData[3] & 0xff); }, + + /** + * Reads a byte from current set data. + * + * @return Byte value (integer) + */ _getByte: function() { var parsedData = new Uint8Array(this._getSlice(1)); return parsedData[0]; }, + + /** + * Reads a character token value from current set data. + * + * @return Character (string) + */ _getChar: function() { return String.fromCharCode(this._getByte()); }, + + /** + * Reads a string token value from current set data. + * + * @return String value + */ _getString: function() { var l = this._getInt(); @@ -131,6 +252,12 @@ WeeChatProtocol.prototype = { return ""; }, + + /** + * Reads a message header from current set data. + * + * @return Header object + */ _getHeader: function() { var len = this._getInt(); var comp = this._getByte(); @@ -140,9 +267,21 @@ WeeChatProtocol.prototype = { compression: comp, }; }, + + /** + * Reads a message header ID from current set data. + * + * @return Message ID (string) + */ _getId: function() { return this._getString(); }, + + /** + * Reads an arbitrary object token from current set data. + * + * @return Object value + */ _getObject: function() { var self = this; var type = this._getType(); @@ -154,6 +293,12 @@ WeeChatProtocol.prototype = { }; } }, + + /** + * Reads an hash table token from current set data. + * + * @return Hash table + */ _getHashTable: function() { var self = this; var typeKeys, typeValues, count; @@ -172,6 +317,12 @@ WeeChatProtocol.prototype = { return dict; }, + + /** + * Reads an array token from current set data. + * + * @return Array + */ _getArray: function() { var self = this; var type; @@ -188,6 +339,13 @@ WeeChatProtocol.prototype = { return values; }, + + /** + * Reads a specified number of bytes from current set data. + * + * @param length Number of bytes to read + * @return Sliced array + */ _getSlice: function(length) { var slice = this._data.slice(this._dataAt, this._dataAt + length); @@ -195,9 +353,22 @@ WeeChatProtocol.prototype = { return slice; }, + + /** + * Sets the current data. + * + * @param data Current data + */ _setData: function (data) { this._data = data; }, + + /** + * Parses a WeeChat message. + * + * @param data Message data (ArrayBuffer) + * @return Message value + */ parse: function(data) { var self = this; From 187f22036b3092ee010ad7a174381c8eb8e5cb21 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 19:10:03 -0400 Subject: [PATCH 2/9] weechat-protocol.js: stricter slicing and type checking --- js/weechat-protocol.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 4104fc3..6fbf703 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -70,6 +70,10 @@ WeeChatProtocol.prototype = { _getType: function() { var t = this._getSlice(3); + if (!t) { + return null; + } + return WeeChatProtocol._uia2s(new Uint8Array(t)); }, @@ -347,6 +351,10 @@ WeeChatProtocol.prototype = { * @return Sliced array */ _getSlice: function(length) { + if (this.dataAt + length > this._data.byteLength) { + return null; + } + var slice = this._data.slice(this._dataAt, this._dataAt + length); this._dataAt += length; From bd4bfe0cb34afcc4dd49cbea80dd1d3feefe4f1e Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 19:28:54 -0400 Subject: [PATCH 3/9] weechat-protocol.js: fix hash table parsing --- js/weechat-protocol.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 6fbf703..e5090f9 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -315,7 +315,7 @@ WeeChatProtocol.prototype = { for (var i = 0; i < count; ++i) { var key = self._runType(typeKeys); var keyStr = self._objToString(key, typeKeys); - var value = self.runType(typeValues); + var value = self._runType(typeValues); dict[keyStr] = value; } From 257b4f01cf7a174bd7bae8f019a4c668e7b52801 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 19:54:07 -0400 Subject: [PATCH 4/9] weechat-protocol.js: add command formatting helper functions --- js/websockets.js | 23 ++++-- js/weechat-protocol.js | 160 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 7 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index 6344fa9..1336985 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -337,18 +337,25 @@ weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', funct } websocket.send(message); } - + // Takes care of the connection and websocket hooks - var connect = function (hostport, proto, password) { + var connect = function (hostport, proto, passwd) { websocket = new WebSocket("ws://" + hostport + "/weechat"); websocket.binaryType = "arraybuffer" websocket.onopen = function (evt) { // FIXME: does password need to be sent only if protocol is not weechat? if (proto == "weechat") { - if (password) { - doSend("init compression=off,password=" + password + "\n(bufinfo) hdata buffer:gui_buffers(*) full_name\nsync\n"); - } + doSend(WeeChatProtocol.formatInit({ + password: passwd, + compression: 'off' + })); + doSend(WeeChatProtocol.formatHdata({ + id: 'bufinfo', + path: 'buffer:gui_buffers(*)', + keys: ['full_name'] + })); + doSend(WeeChatProtocol.formatSync({})); } else { } @@ -381,8 +388,10 @@ weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', funct } var sendMessage = function(message) { - message = "input " + $rootScope.activeBuffer['full_name'] + " " + message + "\n" - doSend(message); + doSend(WeeChatProtocol.formatInput({ + buffer: $rootScope.activeBuffer['full_name'], + data: message + })); } return { diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index e5090f9..594961d 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -50,6 +50,166 @@ WeeChatProtocol._uia2s = function(uia) { return decodeURIComponent(escape(str.join(''))); }; +WeeChatProtocol._mergeParams = function(defaults, override) { + for (var v in override) { + defaults[v] = override[v]; + } + + return defaults; +} + +WeeChatProtocol._formatWrapCmdParts = function(parts) { + return parts.join(' ') + '\n'; +}; + +WeeChatProtocol._formatBeginCmd = function(id, name) { + var cmd; + + cmd = (id !== null) ? '(' + id + ') ' : ''; + cmd += name; + + return cmd; +} + +WeeChatProtocol.formatInit = function(params) { + var keys = []; + var parts = []; + var defaultParams = { + password: null, + compression: 'off' + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(null, 'init')); + keys.push('compression=' + params.compression); + if (params.password !== null) { + keys.push('password=' + params.password); + } + parts.push(keys.join(',')); + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol.formatHdata = function(params) { + var parts = []; + var defaultParams = { + id: null, + keys: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'hdata')); + parts.push(params.path); + if (params.keys !== null) { + parts.push(params.keys.join(',')); + } + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol.formatInfo = function(params) { + var parts = []; + var defaultParams = { + id: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'info')); + parts.push(params.name); + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol.formatNicklist = function(params) { + var parts = []; + var defaultParams = { + id: null, + buffer: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'nicklist')); + if (params.buffer !== null) { + parts.push(params.buffer); + } + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol.formatInput = function(params) { + var parts = []; + var defaultParams = { + id: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'input')); + parts.push(params.buffer); + parts.push(params.data); + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol._formatSyncDesync = function(cmdName, params) { + var parts = []; + var defaultParams = { + id: null, + buffers: null, + options: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, cmdName)); + if (params.buffers !== null) { + parts.push(params.buffers.join(',')); + if (params.options !== null) { + parts.push(params.options.join(',')); + } + } + + return WeeChatProtocol._formatWrapCmdParts(parts); +} + +WeeChatProtocol.formatSync = function(params) { + return WeeChatProtocol._formatSyncDesync('sync', params); +}; + +WeeChatProtocol.formatDesync = function(params) { + return WeeChatProtocol._formatSyncDesync('desync', params); +}; + +WeeChatProtocol.formatTest = function(params) { + var parts = []; + var defaultParams = { + id: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'test')); + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + +WeeChatProtocol.formatQuit = function() { + return WeeChatProtocol._formatWrapCmdParts(['quit']); +}; + +WeeChatProtocol.formatPing = function(params) { + var parts = []; + var defaultParams = { + id: null, + args: null + }; + + params = WeeChatProtocol._mergeParams(defaultParams, params); + parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'ping')); + if (params.args !== null) { + parts.push(params.args.join(' ')); + } + + return WeeChatProtocol._formatWrapCmdParts(parts); +}; + WeeChatProtocol.prototype = { /** * Warns that message parsing is not implemented for a From 4f07d6048fb8e227d2ff882b132cf9da66808896 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 20:05:57 -0400 Subject: [PATCH 5/9] weechat-protocol.js: add comments to formatting functions --- js/weechat-protocol.js | 106 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index 594961d..e904cf4 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -50,6 +50,13 @@ WeeChatProtocol._uia2s = function(uia) { return decodeURIComponent(escape(str.join(''))); }; +/** + * Merges default parameters with overriding parameters. + * + * @param defaults Default parameters + * @param override Overriding parameters + * @return Merged parameters + */ WeeChatProtocol._mergeParams = function(defaults, override) { for (var v in override) { defaults[v] = override[v]; @@ -58,10 +65,23 @@ WeeChatProtocol._mergeParams = function(defaults, override) { return defaults; } +/** + * Joins parts of a full command and wraps it for transmission. + * + * @param parts Parts to join + * @return Complete command string + */ WeeChatProtocol._formatWrapCmdParts = function(parts) { return parts.join(' ') + '\n'; }; +/** + * Begins a command (gets the first part). + * + * @param id ID of command (or null for no ID) + * @param name Name of command + * @return First part of command + */ WeeChatProtocol._formatBeginCmd = function(id, name) { var cmd; @@ -71,6 +91,14 @@ WeeChatProtocol._formatBeginCmd = function(id, name) { return cmd; } +/** + * Formats an init command. + * + * @param params Parameters: + * password: password (optional) + * compression: compression ('off' or 'zlib') (optional) + * @return Formatted init command string + */ WeeChatProtocol.formatInit = function(params) { var keys = []; var parts = []; @@ -90,6 +118,15 @@ WeeChatProtocol.formatInit = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats an hdata command. + * + * @param params Parameters: + * id: command ID (optional) + * path: hdata path (mandatory) + * keys: array of keys (optional) + * @return Formatted hdata command string + */ WeeChatProtocol.formatHdata = function(params) { var parts = []; var defaultParams = { @@ -107,6 +144,14 @@ WeeChatProtocol.formatHdata = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats an info command. + * + * @param params Parameters: + * id: command ID (optional) + * name: info name (mandatory) + * @return Formatted info command string + */ WeeChatProtocol.formatInfo = function(params) { var parts = []; var defaultParams = { @@ -120,6 +165,14 @@ WeeChatProtocol.formatInfo = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats a nicklist command. + * + * @param params Parameters: + * id: command ID (optional) + * buffer: buffer name (optional) + * @return Formatted nicklist command string + */ WeeChatProtocol.formatNicklist = function(params) { var parts = []; var defaultParams = { @@ -136,6 +189,15 @@ WeeChatProtocol.formatNicklist = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats an input command. + * + * @param params Parameters: + * id: command ID (optional) + * buffer: target buffer (mandatory) + * data: input data (mandatory) + * @return Formatted input command string + */ WeeChatProtocol.formatInput = function(params) { var parts = []; var defaultParams = { @@ -150,6 +212,12 @@ WeeChatProtocol.formatInput = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats a sync or a desync command. + * + * @param params Parameters (see _formatSync and _formatDesync) + * @return Formatted sync/desync command string + */ WeeChatProtocol._formatSyncDesync = function(cmdName, params) { var parts = []; var defaultParams = { @@ -170,14 +238,39 @@ WeeChatProtocol._formatSyncDesync = function(cmdName, params) { return WeeChatProtocol._formatWrapCmdParts(parts); } +/** + * Formats a sync command. + * + * @param params Parameters: + * id: command ID (optional) + * buffers: array of buffers to sync (optional) + * options: array of options (optional) + * @return Formatted sync command string + */ WeeChatProtocol.formatSync = function(params) { return WeeChatProtocol._formatSyncDesync('sync', params); }; +/** + * Formats a desync command. + * + * @param params Parameters: + * id: command ID (optional) + * buffers: array of buffers to desync (optional) + * options: array of options (optional) + * @return Formatted desync command string + */ WeeChatProtocol.formatDesync = function(params) { return WeeChatProtocol._formatSyncDesync('desync', params); }; +/** + * Formats a test command. + * + * @param params Parameters: + * id: command ID (optional) + * @return Formatted test command string + */ WeeChatProtocol.formatTest = function(params) { var parts = []; var defaultParams = { @@ -190,10 +283,23 @@ WeeChatProtocol.formatTest = function(params) { return WeeChatProtocol._formatWrapCmdParts(parts); }; +/** + * Formats a quit command. + * + * @return Formatted quit command string + */ WeeChatProtocol.formatQuit = function() { return WeeChatProtocol._formatWrapCmdParts(['quit']); }; +/** + * Formats a ping command. + * + * @param params Parameters: + * id: command ID (optional) + * args: array of custom arguments (optional) + * @return Formatted ping command string + */ WeeChatProtocol.formatPing = function(params) { var parts = []; var defaultParams = { From e8ae26d50e485c5a61f2e64ea25d63152a9c3de3 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 20:06:28 -0400 Subject: [PATCH 6/9] Strip trailing slashes --- js/websockets.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/js/websockets.js b/js/websockets.js index 1336985..29933f3 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -99,7 +99,7 @@ weechat.factory('colors', [function($scope) { return { - + setAttrs: setAttrs, getColor: getColor, prepareCss: prepareCss, @@ -118,7 +118,7 @@ weechat.factory('pluginManager', ['youtubePlugin', 'urlPlugin', 'imagePlugin', f } var contentForMessage = function(message) { - + var content = []; for (var i = 0; i < plugins.length; i++) { var pluginContent = plugins[i].contentForMessage(message); @@ -131,7 +131,7 @@ weechat.factory('pluginManager', ['youtubePlugin', 'urlPlugin', 'imagePlugin', f } } } - + return content; } @@ -204,7 +204,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ /* * Parse the text elements from the buffer line added - * + * */ function parseLineAddedTextElements(message) { var prefix = colors.parse(message['objects'][0]['content'][0]['prefix']); @@ -220,7 +220,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ return text_element; }); return text_elements; - } + } var buffer = message['objects'][0]['content'][0]['buffer']; @@ -240,9 +240,9 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ var handleBufferLineAdded = function(message) { var buffer_line = {} - + message = new BufferLine(message); - + if (!_isActiveBuffer(message.buffer)) { $rootScope.buffers[message.buffer]['notification'] = true; } @@ -265,7 +265,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ var fullName = message['objects'][0]['content'][0]['full_name'] var buffer = message['objects'][0]['content'][0]['pointers'][0] $rootScope.buffers[buffer] = { 'id': buffer, 'lines':[], 'full_name':fullName } - + } /* @@ -292,7 +292,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ } $rootScope.buffers = buffers; } - + var handleEvent = function(event) { if (_.has(eventHandlers, event['id'])) { eventHandlers[event['id']](event); @@ -311,7 +311,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ var eventHandlers = { bufinfo: handleBufferInfo, - _buffer_closing: handleBufferClosing, + _buffer_closing: handleBufferClosing, _buffer_line_added: handleBufferLineAdded, _buffer_opened: handleBufferOpened } @@ -328,7 +328,7 @@ weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', funct var websocket = null; - // Sanitizes messages to be sent to the weechat relay + // Sanitizes messages to be sent to the weechat relay var doSend = function(message) { msgs = message.replace(/[\r\n]+$/g, "").split("\n"); for (var i = 0; i < msgs.length; i++) { From 7780a66264152dee5d12d8545e39f294beada9f4 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 20:09:59 -0400 Subject: [PATCH 7/9] weechat-protocol.js: file header: mention command formatting --- js/weechat-protocol.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index e904cf4..bd6baa6 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -1,9 +1,9 @@ /** * WeeChat protocol handling. * - * This object parses messages for the WeeChat protocol. It's - * independent from the communication layer and thus may be used - * with any network mechanism. + * This object parses messages and formats commands for the WeeChat + * protocol. It's independent from the communication layer and thus + * may be used with any network mechanism. */ var WeeChatProtocol = function() { // specific parsing for each message type From 8a07be5aef733031e0aed56096a020bcf880361c Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 5 Oct 2013 21:52:01 -0400 Subject: [PATCH 8/9] weechat-protocol.js: factor common commands formatting code --- js/weechat-protocol.js | 75 +++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index bd6baa6..dd4ad5a 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -66,30 +66,25 @@ WeeChatProtocol._mergeParams = function(defaults, override) { } /** - * Joins parts of a full command and wraps it for transmission. + * Formats a command. * - * @param parts Parts to join - * @return Complete command string + * @param id Command ID (null for no ID) + * @param name Command name + * @param parts Command parts + * @return Formatted command string */ -WeeChatProtocol._formatWrapCmdParts = function(parts) { - return parts.join(' ') + '\n'; -}; - -/** - * Begins a command (gets the first part). - * - * @param id ID of command (or null for no ID) - * @param name Name of command - * @return First part of command - */ -WeeChatProtocol._formatBeginCmd = function(id, name) { +WeeChatProtocol._formatCmd = function(id, name, parts) { + var cmdIdName; var cmd; - cmd = (id !== null) ? '(' + id + ') ' : ''; - cmd += name; + cmdIdName = (id !== null) ? '(' + id + ') ' : ''; + cmdIdName += name; + parts.unshift(cmdIdName); + cmd = parts.join(' '); + cmd += '\n'; return cmd; -} +}; /** * Formats an init command. @@ -100,22 +95,21 @@ WeeChatProtocol._formatBeginCmd = function(id, name) { * @return Formatted init command string */ WeeChatProtocol.formatInit = function(params) { - var keys = []; - var parts = []; var defaultParams = { password: null, compression: 'off' }; + var keys = []; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(null, 'init')); keys.push('compression=' + params.compression); if (params.password !== null) { keys.push('password=' + params.password); } parts.push(keys.join(',')); - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(null, 'init', parts); }; /** @@ -128,20 +122,19 @@ WeeChatProtocol.formatInit = function(params) { * @return Formatted hdata command string */ WeeChatProtocol.formatHdata = function(params) { - var parts = []; var defaultParams = { id: null, keys: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'hdata')); parts.push(params.path); if (params.keys !== null) { parts.push(params.keys.join(',')); } - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'hdata', parts); }; /** @@ -153,16 +146,15 @@ WeeChatProtocol.formatHdata = function(params) { * @return Formatted info command string */ WeeChatProtocol.formatInfo = function(params) { - var parts = []; var defaultParams = { id: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'info')); parts.push(params.name); - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'info', parts); }; /** @@ -174,19 +166,18 @@ WeeChatProtocol.formatInfo = function(params) { * @return Formatted nicklist command string */ WeeChatProtocol.formatNicklist = function(params) { - var parts = []; var defaultParams = { id: null, buffer: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'nicklist')); if (params.buffer !== null) { parts.push(params.buffer); } - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'nicklist', parts); }; /** @@ -199,17 +190,16 @@ WeeChatProtocol.formatNicklist = function(params) { * @return Formatted input command string */ WeeChatProtocol.formatInput = function(params) { - var parts = []; var defaultParams = { id: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'input')); parts.push(params.buffer); parts.push(params.data); - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'input', parts); }; /** @@ -219,15 +209,14 @@ WeeChatProtocol.formatInput = function(params) { * @return Formatted sync/desync command string */ WeeChatProtocol._formatSyncDesync = function(cmdName, params) { - var parts = []; var defaultParams = { id: null, buffers: null, options: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, cmdName)); if (params.buffers !== null) { parts.push(params.buffers.join(',')); if (params.options !== null) { @@ -235,7 +224,7 @@ WeeChatProtocol._formatSyncDesync = function(cmdName, params) { } } - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, cmdName, parts); } /** @@ -272,15 +261,14 @@ WeeChatProtocol.formatDesync = function(params) { * @return Formatted test command string */ WeeChatProtocol.formatTest = function(params) { - var parts = []; var defaultParams = { id: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'test')); - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'test', parts); }; /** @@ -289,7 +277,7 @@ WeeChatProtocol.formatTest = function(params) { * @return Formatted quit command string */ WeeChatProtocol.formatQuit = function() { - return WeeChatProtocol._formatWrapCmdParts(['quit']); + return WeeChatProtocol._formatCmd(null, 'quit', []); }; /** @@ -301,19 +289,18 @@ WeeChatProtocol.formatQuit = function() { * @return Formatted ping command string */ WeeChatProtocol.formatPing = function(params) { - var parts = []; var defaultParams = { id: null, args: null }; + var parts = []; params = WeeChatProtocol._mergeParams(defaultParams, params); - parts.push(WeeChatProtocol._formatBeginCmd(params.id, 'ping')); if (params.args !== null) { parts.push(params.args.join(' ')); } - return WeeChatProtocol._formatWrapCmdParts(parts); + return WeeChatProtocol._formatCmd(params.id, 'ping', parts); }; WeeChatProtocol.prototype = { From d332635a4eaea80d6729cb8e231c5d6554846b70 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sun, 6 Oct 2013 00:17:13 -0400 Subject: [PATCH 9/9] Fix message time display --- index.html | 2 +- js/websockets.js | 4 ++-- js/weechat-protocol.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index e0880c0..6594bc9 100644 --- a/index.html +++ b/index.html @@ -50,7 +50,7 @@ {{ content.full_name }}
- {{ bufferline.timestamp | date: 'h:mm:ss'}} + {{ bufferline.date | date: 'H:mm:ss'}} {{ part.text }} diff --git a/js/websockets.js b/js/websockets.js index 29933f3..d89748a 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -224,7 +224,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ var buffer = message['objects'][0]['content'][0]['buffer']; - var timestamp = parseInt(message['objects'][0]['content'][0]['date']) * 1e3; + var date = message['objects'][0]['content'][0]['date']; var text = colors.parse(message['objects'][0]['content'][0]['message']); var content = parseLineAddedTextElements(message); var additionalContent = pluginManager.contentForMessage(text[0]['text']); @@ -232,7 +232,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'pluginManager', function($ return { metadata: additionalContent, content: content, - timestamp: timestamp, + date: date, buffer: buffer } diff --git a/js/weechat-protocol.js b/js/weechat-protocol.js index dd4ad5a..362763e 100644 --- a/js/weechat-protocol.js +++ b/js/weechat-protocol.js @@ -455,7 +455,7 @@ WeeChatProtocol.prototype = { _getTime: function() { var str = this._getStrNumber(); - return new Date(parseInt(str)); + return new Date(parseInt(str) * 1000); }, /**