2013-02-18 00:49:42 +01:00
|
|
|
var weechat = angular.module('weechat', []);
|
|
|
|
|
2013-07-27 16:43:41 +02:00
|
|
|
weechat.factory('colors', [function($scope) {
|
|
|
|
|
|
|
|
// http://weechat.org/files/doc/devel/weechat_dev.en.html#color_codes_in_strings
|
|
|
|
var part, fg, bg, attrs, colors = ['', 'black', 'dark gray', 'dark red', 'light red', 'dark green', 'light green', 'brown', 'yellow', 'dark blue', 'light blue', 'dark magenta', 'light magenta', 'dark cyan', 'light cyan', 'gray', 'white'];
|
|
|
|
|
|
|
|
function setAttrs() {
|
|
|
|
while (part.match(/^[\*\/\_\|]/)) {
|
|
|
|
attrs.push(part.charAt(0));
|
|
|
|
part = part.slice(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColor() {
|
|
|
|
var c;
|
|
|
|
if (part.match(/^@/)) {
|
|
|
|
c = part.slice(1, 5);
|
|
|
|
part = part.slice(5);
|
|
|
|
} else {
|
|
|
|
c = part.slice(0, 2);
|
|
|
|
part = part.slice(2);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefixes = {
|
|
|
|
'\x19': function() {
|
|
|
|
if (part.match(/^F/)) {
|
|
|
|
part = part.slice(1);
|
|
|
|
setAttrs();
|
|
|
|
fg = getColor();
|
|
|
|
} else if (part.match(/^B/)) {
|
|
|
|
part = part.slice(1);
|
|
|
|
setAttrs();
|
|
|
|
bg = getColor();
|
|
|
|
} else {
|
|
|
|
setAttrs();
|
|
|
|
fg = getColor();
|
|
|
|
if (part.match(/^,/)) {
|
|
|
|
part = part.slice(1);
|
|
|
|
bg = getColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'\x1A': function() {
|
|
|
|
// Don't know what to do
|
|
|
|
},
|
|
|
|
'\x1B': function() {
|
|
|
|
attrs = [];
|
|
|
|
},
|
|
|
|
'\x1C': function() {
|
|
|
|
fg = '';
|
|
|
|
bg = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function parse(text) {
|
|
|
|
if (!text) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
var f, parts = text.split(/(\x19|\x1A|\x1B|\x1C)/);
|
|
|
|
if (parts.length === 1) return [{
|
|
|
|
text: parts[0]
|
|
|
|
}];
|
|
|
|
attrs = [];
|
|
|
|
|
|
|
|
return parts.map(function(p) {
|
|
|
|
var res, tmp = prefixes[p.charAt(0)];
|
|
|
|
if (f) {
|
|
|
|
part = p;
|
|
|
|
f();
|
|
|
|
res = {
|
|
|
|
text: part,
|
|
|
|
fg: colors[parseInt(fg, 10)],
|
|
|
|
bg: colors[parseInt(bg, 10)],
|
|
|
|
attrs: attrs
|
|
|
|
};
|
|
|
|
if (!res.fg) res.fg = fg;
|
|
|
|
if (!res.bg) res.bg = bg;
|
|
|
|
}
|
|
|
|
f = tmp;
|
|
|
|
return res;
|
|
|
|
}).filter(function(p) {
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
setAttrs: setAttrs,
|
|
|
|
getColor: getColor,
|
|
|
|
parse: parse,
|
|
|
|
parts: ['', 'black', 'dark gray', 'dark red', 'light red', 'dark green', 'light green', 'brown', 'yellow', 'dark blue', 'light blue', 'dark magenta', 'light magenta', 'dark cyan', 'light cyan', 'gray', 'white']
|
|
|
|
}
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
2013-08-05 03:39:23 +02:00
|
|
|
weechat.factory('handlers', ['$rootScope', 'colors', function($rootScope, colors) {
|
|
|
|
|
|
|
|
var handleBufferLineAdded = function(message) {
|
|
|
|
|
|
|
|
var buffer_line = {}
|
|
|
|
var prefix = colors.parse(message['objects'][0]['content'][0]['prefix']);
|
|
|
|
var text = colors.parse(message['objects'][0]['content'][0]['message']);
|
|
|
|
var buffer = message['objects'][0]['content'][0]['buffer'];
|
|
|
|
var message = _.union(prefix, text);
|
|
|
|
buffer_line['message'] = message;
|
|
|
|
buffer_line['metadata'] = findMetaData(text[0]['text']);
|
|
|
|
|
|
|
|
|
|
|
|
$rootScope.buffers[buffer]['lines'].push(buffer_line);
|
|
|
|
}
|
|
|
|
|
|
|
|
var handleBufferOpened = function(message) {
|
|
|
|
console.log('buffer opened');
|
|
|
|
var fullName = message['objects'][0]['content'][0]['full_name']
|
|
|
|
var buffer = message['objects'][0]['content'][0]['pointers'][0]
|
|
|
|
$rootScope.buffers[buffer] = { 'lines':[], 'full_name':fullName }
|
|
|
|
console.log($rootScope.buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
var handleEvent = function(message) {
|
|
|
|
types[message['id']](message);
|
|
|
|
}
|
|
|
|
|
|
|
|
var findMetaData = function(message) {
|
|
|
|
if (message.indexOf('youtube.com') != -1) {
|
|
|
|
var index = message.indexOf("?v=");
|
|
|
|
var token = message.substr(index+3);
|
|
|
|
return '<iframe width="560" height="315" src="http://www.youtube.com/embed/' + token + '" frameborder="0" allowfullscreen></iframe>'
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var types = {
|
|
|
|
_buffer_line_added: handleBufferLineAdded,
|
|
|
|
_buffer_opened: handleBufferOpened
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
handleEvent: handleEvent
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|
|
|
|
weechat.factory('connection', ['$rootScope', '$http', 'handlers', 'colors', function($rootScope, $http, handlers, colors) {
|
2013-08-02 03:54:12 +02:00
|
|
|
protocol = new Protocol();
|
|
|
|
var websocket = null;
|
2013-02-24 20:44:03 +01:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
var doSend = function(message) {
|
2013-07-27 16:43:41 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
msgs = message.replace(/[\r\n]+$/g, "").split("\n");
|
|
|
|
for (var i = 0; i < msgs.length; i++) {
|
2013-02-24 20:44:03 +01:00
|
|
|
console.log('=' + msgs[i] + '=');
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.commands.push("SENT: " + msgs[i]);
|
2013-02-18 00:49:42 +01:00
|
|
|
}
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.send(message);
|
|
|
|
}
|
|
|
|
var connect = function (hostport, proto, password) {
|
|
|
|
websocket = new WebSocket("ws://" + hostport + "/weechat");
|
|
|
|
websocket.binaryType = "arraybuffer"
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onopen = function (evt) {
|
|
|
|
if (proto == "weechat") {
|
2013-07-20 20:23:09 +02:00
|
|
|
doSend("init compression=off\nversion\n");
|
2013-07-30 15:01:08 +02:00
|
|
|
doSend("hdata buffer:gui_buffers(*) full_name\n");
|
2013-07-27 16:43:41 +02:00
|
|
|
doSend("sync\n");
|
2013-08-02 03:54:12 +02:00
|
|
|
} else {
|
2013-02-18 00:49:42 +01:00
|
|
|
doSend("PASS " + password + "\r\nNICK test\r\nUSER test 0 * :test\r\n");
|
|
|
|
}
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.connected = true;
|
|
|
|
$rootScope.$apply();
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onclose = function (evt) {
|
|
|
|
console.log("disconnected", "Disconnected");
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.connected = false;
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onmessage = function (evt) {
|
|
|
|
protocol.setData(evt.data);
|
|
|
|
message = protocol.parse()
|
|
|
|
console.log(message);
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.commands.push("RECV: " + evt.data + " TYPE:" + evt.type) ;
|
2013-08-02 03:54:12 +02:00
|
|
|
parseMessage(message);
|
|
|
|
data = evt.data;
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.$apply();
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
websocket.onerror = function (evt) {
|
|
|
|
console.log("error", "ERROR: " + evt.data);
|
2013-02-18 00:49:42 +01:00
|
|
|
}
|
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
this.websocket = websocket;
|
|
|
|
}
|
2013-07-31 14:40:43 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
var parseMessage = function(message) {
|
|
|
|
|
2013-08-05 03:53:23 +02:00
|
|
|
if (protocol.isEvent(message)) {
|
2013-08-05 03:39:23 +02:00
|
|
|
handlers.handleEvent(message);
|
2013-08-05 03:53:23 +02:00
|
|
|
} else {
|
|
|
|
parseObjects(message['objects']);
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-08-05 03:53:23 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
};
|
2013-07-30 15:01:08 +02:00
|
|
|
|
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
var parseObjects = function(objects) {
|
2013-07-30 15:01:08 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
for (var i = 0; i < objects.length ; i++) {
|
|
|
|
console.log('type', objects[i]['type']);
|
|
|
|
if (objects[i]['type'] == 'hda') {
|
|
|
|
parseHdaObject(objects[i]);
|
2013-07-30 15:01:08 +02:00
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
}
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
var parseHdaObject = function(hdaObject) {
|
|
|
|
console.log('parse hda', hdaObject['content'], hdaObject['content'].length);
|
|
|
|
buffers = {};
|
|
|
|
for (var i = 0; i < hdaObject['content'].length; i++) {
|
|
|
|
content = hdaObject['content'][i];
|
|
|
|
content['lines'] = [];
|
|
|
|
console.log('content', content);
|
|
|
|
var pointer = content['pointers'][0];
|
|
|
|
buffers[pointer] = content;
|
2013-07-30 15:01:08 +02:00
|
|
|
}
|
2013-08-02 03:54:12 +02:00
|
|
|
|
2013-08-02 03:55:51 +02:00
|
|
|
$rootScope.buffers = buffers;
|
2013-08-02 03:54:12 +02:00
|
|
|
}
|
2013-07-30 15:01:08 +02:00
|
|
|
|
2013-08-03 17:25:38 +02:00
|
|
|
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
var sendMessage = function(message) {
|
2013-08-02 03:55:51 +02:00
|
|
|
message = "input " + $rootScope.activeBuffer['full_name'] + " " + message + "\n"
|
|
|
|
console.log($rootScope.activeBuffer);
|
2013-08-02 03:54:12 +02:00
|
|
|
doSend(message);
|
|
|
|
}
|
2013-07-21 17:48:32 +02:00
|
|
|
|
2013-08-05 03:39:23 +02:00
|
|
|
|
2013-08-02 03:54:12 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
connect: connect,
|
|
|
|
sendMessage: sendMessage
|
|
|
|
}
|
2013-02-18 00:49:42 +01:00
|
|
|
}]);
|
|
|
|
|
|
|
|
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', 'connection', function ($rootScope, $scope, connection) {
|
|
|
|
$rootScope.commands = []
|
2013-02-16 19:18:14 +01:00
|
|
|
|
2013-07-21 17:48:32 +02:00
|
|
|
$rootScope.buffer = []
|
2013-07-30 15:01:08 +02:00
|
|
|
$rootScope.buffers = {}
|
2013-07-30 15:22:37 +02:00
|
|
|
$rootScope.activeBuffer = null;
|
2013-02-16 19:18:14 +01:00
|
|
|
$scope.hostport = "localhost:9001"
|
|
|
|
$scope.proto = "weechat"
|
|
|
|
$scope.password = ""
|
|
|
|
|
2013-07-30 15:22:37 +02:00
|
|
|
$scope.setActiveBuffer = function(key) {
|
|
|
|
console.log('change buffer');
|
|
|
|
$rootScope.activeBuffer = $rootScope.buffers[key];
|
|
|
|
};
|
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.sendMessage = function() {
|
|
|
|
connection.sendMessage($scope.command);
|
|
|
|
$scope.command = "";
|
2013-07-30 15:22:37 +02:00
|
|
|
};
|
2013-02-16 19:18:14 +01:00
|
|
|
|
2013-02-18 00:49:42 +01:00
|
|
|
$scope.connect = function() {
|
|
|
|
connection.connect($scope.hostport, $scope.proto, $scope.password);
|
2013-02-16 19:18:14 +01:00
|
|
|
}
|
2013-08-02 03:54:12 +02:00
|
|
|
}]
|
|
|
|
);
|