From f5869438b7161f8e459da6c246c84392ec426d67 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 24 Feb 2013 14:44:03 -0500 Subject: [PATCH] basic support for weechat protocol based on weechat.js --- js/protocol.js | 121 +++++++++++++++++++++++++++++++++++++++++++++++ js/websockets.js | 17 ++++--- websockets.html | 6 ++- 3 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 js/protocol.js diff --git a/js/protocol.js b/js/protocol.js new file mode 100644 index 0000000..95c613d --- /dev/null +++ b/js/protocol.js @@ -0,0 +1,121 @@ +var Protocol = function() { + var self = this; + var getInfo = function() { + var info = {}; + info.key = getString(); + info.value = getString(); + return info; + }; + + var types = { + chr: getChar, + "int": getInt, + "str": getString, + "inf": getInfo, + }; +//TODO: IMPLEMENT THIS STUFF +// chr: this.getChar, +// 'int': getInt, + // hacks +// lon: getPointer, +// str: getString, +// buf: getBuffer, +// ptr: getPointer, + // hacks +// tim: getPointer, +// htb: getHashtable, +// hda: getHdata, +// inf: Protocol.getInfo, +// inl: getInfolist, +// arr: array +// }, + + var _uiatos =function(uia) { + var _str = []; + for (var c = 0; c < uia.length; c++) { + _str[c] = String.fromCharCode(uia[c]); + } + return _str.join(""); + }; + + var getInt = function() { + var parsed_data = new Uint8Array(getSlice(4)); + var i = ((parsed_data[0] & 0xff) << 24) | ((parsed_data[1] & 0xff) << 16) | ((parsed_data[2] & 0xff) << 8) | (parsed_data[3] & 0xff); + return i; + + }; + + var getChar = function() { + var parsed_data = new Uint8Array(getSlice(1)); + return parsed_data[0]; + } + + var getString = function() { + var l = getInt(); + if (l > 0) { + var s = getSlice(l); + var parsed_data = new Uint8Array(s); + return _uiatos(parsed_data); + } + return ""; + }; + + var getSlice = function(length) { + var slice = self.data.slice(0,length); + self.data = self.data.slice(length); + return slice; + }; + + var getType = function() { + var t = getSlice(3); + return _uiatos(new Uint8Array(t)); + }; + + var runType = function(type) { + if (type in types) { + return types[type](); + } + 0; + }; + + var getHeader = function() { + return { + length: getInt(), + compression: getChar(), + } + }; + + var getId = function() { + return getString(); + } + + var getObject = function() { + var type = getType(); + if (type) { + return object = { + type: type, + content: runType(type), + } + } + } + + self.parse = function() { + var header = getHeader(); + var id = getId(); + var objects = []; + var object = getObject(); + while(object) { + objects.push(object); + object = getObject(); + } + return { + header: header, + id: id, + objects: objects, + } + } + + self.setData = function (data) { + self.data = data; + }; +} diff --git a/js/websockets.js b/js/websockets.js index 746add8..1048294 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -1,23 +1,24 @@ var weechat = angular.module('weechat', []); weechat.factory('connection', ['$rootScope', function($scope) { - + protocol = new Protocol(); var websocket = null; + var doSend = function(message) { msgs = message.replace(/[\r\n]+$/g, "").split("\n"); for (var i = 0; i < msgs.length; i++) { - console.log("sent", "⇐ " + msgs[i]); - $scope.commands.push(msgs[i]); + console.log('=' + msgs[i] + '='); + $scope.commands.push("SENT: " + msgs[i]); } websocket.send(message); } var connect = function (hostport, proto, password) { websocket = new WebSocket("ws://" + hostport + "/weechat"); - + websocket.binaryType = "arraybuffer" websocket.onopen = function (evt) { if (proto == "weechat") { - doSend("init password=" + password + "\ninfo version\ntest\n"); + //doSend("init compression=off\nversion\n"); } else { doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n"); } @@ -30,7 +31,10 @@ weechat.factory('connection', ['$rootScope', function($scope) { } websocket.onmessage = function (evt) { console.log("recv", "⇒ " + evt.data); - $scope.commands.push(evt.data); + protocol.setData(evt.data); + console.log(protocol.parse()); + $scope.commands.push("RECV: " + evt.data + " TYPE:" + evt.type) ; + data = evt.data; $scope.$apply(); } websocket.onerror = function (evt) { @@ -41,6 +45,7 @@ weechat.factory('connection', ['$rootScope', function($scope) { } var sendMessage = function(message) { + message = message + "\n" doSend(message); } return { diff --git a/websockets.html b/websockets.html index 81d78c3..855727f 100644 --- a/websockets.html +++ b/websockets.html @@ -3,6 +3,7 @@ + @@ -36,12 +37,13 @@
- {{ commands }}
{{ command }}
+
- + +