glowingbear-mainbox/js/websockets.js

395 lines
12 KiB
JavaScript
Raw Normal View History

2013-10-08 02:43:09 +02:00
var weechat = angular.module('weechat', ['localStorage', 'weechatModels']);
2013-02-18 00:49:42 +01: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;
}
2013-10-04 20:19:28 +02:00
function prepareCss(color) {
/*
* Translates a weechat color string to CSS
*/
return'color-' + color.replace(' ', '-');
}
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 {
2013-10-06 02:06:28 +02:00
setAttrs: setAttrs,
getColor: getColor,
2013-10-04 20:19:28 +02:00
prepareCss: prepareCss,
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-10-02 02:43:29 +02:00
weechat.factory('pluginManager', ['youtubePlugin', 'urlPlugin', 'imagePlugin', function(youtubePlugin, urlPlugin, imagePlugin) {
2013-10-02 02:43:29 +02:00
var plugins = [youtubePlugin, urlPlugin, imagePlugin]
var hookPlugin = function(plugin) {
plugins.push(plugin);
}
var contentForMessage = function(message) {
2013-10-06 02:06:28 +02:00
var content = [];
for (var i = 0; i < plugins.length; i++) {
var pluginContent = plugins[i].contentForMessage(message);
if (pluginContent) {
var pluginContent = {'visible': false, 'content': pluginContent }
content.push(pluginContent);
if (plugins[i].exclusive) {
break;
}
}
}
2013-10-06 02:06:28 +02:00
return content;
}
return {
hookPlugin: hookPlugin,
contentForMessage: contentForMessage
}
}]);
weechat.factory('youtubePlugin', [function() {
2013-10-02 01:39:24 +02:00
var contentForMessage = 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;
}
return {
contentForMessage: contentForMessage,
exclusive: true
}
2013-10-02 01:39:24 +02:00
}]);
2013-10-02 02:03:05 +02:00
weechat.factory('urlPlugin', [function() {
var contentForMessage = function(message) {
2013-10-02 03:20:49 +02:00
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/;
var url = message.match(urlPattern);
if (url) {
return '<a href="' + url[0] + '">' + message + '</a>';
2013-10-02 02:03:05 +02:00
}
return null;
}
return {
contentForMessage: contentForMessage,
exclusive: false
2013-10-02 02:03:05 +02:00
}
}]);
2013-10-02 02:43:29 +02:00
weechat.factory('imagePlugin', [function() {
var contentForMessage = function(message) {
var urls = message.match(/https?:\/\/[^\s]*\.(jpg|png|gif)\b/)
if (urls != null) {
var url = urls[0]; /* Actually parse one url per message */
return '<img src="' + url + '" height="300">';
}
return null;
}
2013-10-02 02:03:05 +02:00
2013-10-02 02:43:29 +02:00
return {
contentForMessage: contentForMessage
}
}]);
2013-10-08 02:43:09 +02:00
weechat.factory('handlers', ['$rootScope', 'colors', 'models', 'pluginManager', function($rootScope, colors, models, pluginManager) {
2013-08-05 03:39:23 +02:00
2013-10-02 02:32:18 +02:00
var handleBufferClosing = function(message) {
2013-10-08 03:15:25 +02:00
var bufferMessage = message['objects'][0]['content'][0];
var buffer = new models.Buffer(bufferMessage);
$rootScope.closeBuffer(buffer.id);
2013-10-02 02:32:18 +02:00
}
2013-10-05 17:57:46 +02:00
var handleBufferLineAdded = function(message) {
var buffer_line = {}
2013-10-06 02:06:28 +02:00
2013-10-08 02:43:09 +02:00
message = new models.BufferLine(message);
message.metadata = pluginManager.contentForMessage(message.text);
2013-10-06 02:06:28 +02:00
2013-10-05 17:57:46 +02:00
if (!_isActiveBuffer(message.buffer)) {
$rootScope.buffers[message.buffer]['notification'] = true;
}
2013-10-05 17:57:46 +02:00
$rootScope.buffers[message.buffer]['lines'].push(message);
2013-08-05 03:39:23 +02:00
}
2013-08-06 17:06:42 +02:00
/*
* Returns whether or not this buffer is the active buffer
*/
var _isActiveBuffer = function(buffer) {
if ($rootScope.activeBuffer['id'] == buffer) {
return true;
} else {
return false;
}
}
2013-08-05 03:39:23 +02:00
var handleBufferOpened = function(message) {
2013-10-08 03:15:25 +02:00
var bufferMessage = message['objects'][0]['content'][0];
var buffer = new models.Buffer(bufferMessage);
$rootScope.buffers[buffer.id] = buffer;
2013-10-06 02:06:28 +02:00
2013-08-05 03:39:23 +02:00
}
/*
* Handle answers to (bufinfo) messages
*
* (bufinfo) messages are specified by this client. It is the first
* message that is sent to the relay after connection.
*/
var handleBufferInfo = function(message) {
// buffer info from message
var bufferInfos = message['objects'][0]['content'];
// buffers objects
var buffers = {};
for (var i = 0; i < bufferInfos.length ; i++) {
2013-10-08 03:15:25 +02:00
var buffer = new models.Buffer(bufferInfos[i]);
buffers[buffer.id] = buffer;
2013-10-02 01:12:29 +02:00
if (i == 0) {
// first buffer is active buffer by default
2013-10-08 03:15:25 +02:00
$rootScope.activeBuffer = buffers[buffer.id];
2013-10-02 01:12:29 +02:00
}
}
$rootScope.buffers = buffers;
}
2013-10-06 02:06:28 +02:00
var handleEvent = function(event) {
if (_.has(eventHandlers, event['id'])) {
eventHandlers[event['id']](event);
}
2013-08-05 03:39:23 +02:00
}
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 eventHandlers = {
bufinfo: handleBufferInfo,
2013-10-06 02:06:28 +02:00
_buffer_closing: handleBufferClosing,
2013-08-05 03:39:23 +02:00
_buffer_line_added: handleBufferLineAdded,
_buffer_opened: handleBufferOpened
}
return {
handleEvent: handleEvent
}
}]);
weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', function($rootScope, $log, handlers, colors) {
protocol = new WeeChatProtocol();
2013-08-02 03:54:12 +02:00
var websocket = null;
2013-10-06 02:06:28 +02:00
// Sanitizes messages to be sent to the weechat relay
var doSend = function(message) {
2013-08-02 03:54:12 +02:00
msgs = message.replace(/[\r\n]+$/g, "").split("\n");
for (var i = 0; i < msgs.length; i++) {
$log.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);
}
// Takes care of the connection and websocket hooks
var connect = function (hostport, proto, passwd) {
2013-08-02 03:54:12 +02:00
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) {
// FIXME: does password need to be sent only if protocol is not weechat?
2013-08-02 03:54:12 +02:00
if (proto == "weechat") {
doSend(WeeChatProtocol.formatInit({
password: passwd,
compression: 'off'
}));
doSend(WeeChatProtocol.formatHdata({
id: 'bufinfo',
path: 'buffer:gui_buffers(*)',
keys: ['full_name']
}));
doSend(WeeChatProtocol.formatSync({}));
2013-08-02 03:54:12 +02:00
} else {
2013-08-05 21:53:59 +02:00
2013-02-18 00:49:42 +01:00
}
$log.info("Connected to relay");
2013-08-02 03:55:51 +02:00
$rootScope.connected = true;
$rootScope.$apply();
2013-08-02 03:54:12 +02:00
}
2013-08-02 03:54:12 +02:00
websocket.onclose = function (evt) {
$log.info("Disconnected from relay");
2013-08-02 03:55:51 +02:00
$rootScope.connected = false;
$rootScope.$apply();
2013-08-02 03:54:12 +02:00
}
2013-08-02 03:54:12 +02:00
websocket.onmessage = function (evt) {
message = protocol.parse(evt.data)
handlers.handleEvent(message);
2013-08-02 03:55:51 +02:00
$rootScope.commands.push("RECV: " + evt.data + " TYPE:" + evt.type) ;
$rootScope.$apply();
2013-08-02 03:54:12 +02:00
}
2013-08-02 03:54:12 +02:00
websocket.onerror = function (evt) {
2013-10-03 01:55:30 +02:00
if (evt.type == "error" && websocket.readyState == 0) {
$rootScope.errorMessage = true;
}
$log.error("Relay error " + evt.data);
2013-02-18 00:49:42 +01:00
}
2013-08-02 03:54:12 +02:00
this.websocket = websocket;
}
2013-08-02 03:54:12 +02:00
var sendMessage = function(message) {
doSend(WeeChatProtocol.formatInput({
2013-10-08 03:15:25 +02:00
buffer: $rootScope.activeBuffer['fullName'],
data: message
}));
2013-08-02 03:54:12 +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', '$store', 'connection', function ($rootScope, $scope, $store, connection) {
2013-02-18 00:49:42 +01:00
$rootScope.commands = []
2013-02-16 19:18:14 +01:00
$rootScope.buffer = []
2013-07-30 15:01:08 +02:00
$rootScope.buffers = {}
2013-07-30 15:22:37 +02:00
$rootScope.activeBuffer = null;
$store.bind($scope, "hostport", "localhost:9001");
$store.bind($scope, "proto", "weechat");
$store.bind($scope, "password", "");
// TODO checkbox for saving password or not?
// $scope.password = "";
2013-10-02 02:32:18 +02:00
$rootScope.closeBuffer = function(buffer_pointer) {
delete($rootScope.buffers[buffer_pointer]);
var first_buffer = _.keys($rootScope.buffers)[0];
$scope.setActiveBuffer(first_buffer);
}
2013-07-30 15:22:37 +02:00
$scope.setActiveBuffer = function(key) {
2013-08-06 17:06:42 +02:00
$rootScope.buffers[key]['notification'] = false;
2013-07-30 15:22:37 +02:00
$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
}]
);