glowingbear-mainbox/js/websockets.js

533 lines
18 KiB
JavaScript
Raw Normal View History

var weechat = angular.module('weechat', ['localStorage', 'weechatModels', 'plugins', 'ngSanitize']);
2013-02-18 00:49:42 +01:00
weechat.filter('toArray', function () {
'use strict';
return function (obj) {
if (!(obj instanceof Object)) {
return obj;
}
return Object.keys(obj).map(function (key) {
return Object.defineProperty(obj[key], '$key', {__proto__: null, value: key});
});
}
});
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'];
// XTerm 8-bit pallete
var colors = [
'#000000', '#AA0000', '#00AA00', '#AA5500', '#0000AA',
'#AA00AA', '#00AAAA', '#AAAAAA', '#555555', '#FF5555',
'#55FF55', '#FFFF55', '#5555FF', '#FF55FF', '#55FFFF',
'#FFFFFF', '#000000', '#00005F', '#000087', '#0000AF',
'#0000D7', '#0000FF', '#005F00', '#005F5F', '#005F87',
'#005FAF', '#005FD7', '#005FFF', '#008700', '#00875F',
'#008787', '#0087AF', '#0087D7', '#00AF00', '#00AF5F',
'#00AF87', '#00AFAF', '#00AFD7', '#00AFFF', '#00D700',
'#00D75F', '#00D787', '#00D7AF', '#00D7D7', '#00D7FF',
'#00FF00', '#00FF5F', '#00FF87', '#00FFAF', '#00FFD7',
'#00FFFF', '#5F0000', '#5F005F', '#5F0087', '#5F00AF',
'#5F00D7', '#5F00FF', '#5F5F00', '#5F5F5F', '#5F5F87',
'#5F5FAF', '#5F5FD7', '#5F5FFF', '#5F8700', '#5F875F',
'#5F8787', '#5F87AF', '#5F87D7', '#5F87FF', '#5FAF00',
'#5FAF5F', '#5FAF87', '#5FAFAF', '#5FAFD7', '#5FAFFF',
'#5FD700', '#5FD75F', '#5FD787', '#5FD7AF', '#5FD7D7',
'#5FD7FF', '#5FFF00', '#5FFF5F', '#5FFF87', '#5FFFAF',
'#5FFFD7', '#5FFFFF', '#870000', '#87005F', '#870087',
'#8700AF', '#8700D7', '#8700FF', '#875F00', '#875F5F',
'#875F87', '#875FAF', '#875FD7', '#875FFF', '#878700',
'#87875F', '#878787', '#8787AF', '#8787D7', '#8787FF',
'#87AF00', '#87AF5F', '#87AF87', '#87AFAF', '#87AFD7',
'#87AFFF', '#87D700', '#87D75F', '#87D787', '#87D7AF',
'#87D7D7', '#87D7FF', '#87FF00', '#87FF5F', '#87FF87',
'#87FFAF', '#87FFD7', '#87FFFF', '#AF0000', '#AF005F',
'#AF0087', '#AF00AF', '#AF00D7', '#AF00FF', '#AF5F00',
'#AF5F5F', '#AF5F87', '#AF5FAF', '#AF5FD7', '#AF5FFF',
'#AF8700', '#AF875F', '#AF8787', '#AF87AF', '#AF87D7',
'#AF87FF', '#AFAF00', '#AFAF5F', '#AFAF87', '#AFAFAF',
'#AFAFD7', '#AFAFFF', '#AFD700', '#AFD75F', '#AFD787',
'#AFD7AF', '#AFD7D7', '#AFD7FF', '#AFFF00', '#AFFF5F',
'#AFFF87', '#AFFFAF', '#AFFFD7', '#AFFFFF', '#D70000',
'#D7005F', '#D70087', '#D700AF', '#D700D7', '#D700FF',
'#D75F00', '#D75F5F', '#D75F87', '#D75FAF', '#D75FD7',
'#D75FFF', '#D78700', '#D7875F', '#D78787', '#D787AF',
'#D787D7', '#D787FF', '#D7AF00', '#D7AF5F', '#D7AF87',
'#D7AFAF', '#D7AFD7', '#D7AFFF', '#D7D700', '#D7D75F',
'#D7D787', '#D7D7AF', '#D7D7D7', '#D7D7FF', '#D7FF00',
'#D7FF5F', '#D7FF87', '#D7FFAF', '#D7FFD7', '#D7FFFF',
'#FF0000', '#FF005F', '#FF0087', '#FF00AF', '#FF00D7',
'#FF00FF', '#FF5F00', '#FF5F5F', '#FF5F87', '#FF5FAF',
'#FF5FD7', '#FF5FFF', '#FF8700', '#FF875F', '#FF8787',
'#FF87AF', '#FF87D7', '#FF87FF', '#FFAF00', '#FFAF5F',
'#FFAF87', '#FFAFAF', '#FFAFD7', '#FFAFFF', '#FFD700',
'#FFD75F', '#FFD787', '#FFD7AF', '#FFD7D7', '#FFD7FF',
'#FFFF00', '#FFFF5F', '#FFFF87', '#FFFFAF', '#FFFFD7',
'#FFFFFF', '#080808', '#121212', '#1C1C1C', '#262626',
'#303030', '#3A3A3A', '#444444', '#4E4E4E', '#585858',
'#626262', '#6C6C6C', '#767676', '#808080', '#8A8A8A',
'#949494', '#9E9E9E', '#A8A8A8', '#B2B2B2', '#BCBCBC',
'#C6C6C6', '#D0D0D0', '#DADADA', '#E4E4E4', '#EEEEEE'
]
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, 6);
part = part.slice(6);
} 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 to CSS
2013-10-04 20:19:28 +02:00
*/
return 'color: ' + color;
2013-10-04 20:19:28 +02:00
}
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-08 23:55:30 +02:00
weechat.factory('handlers', ['$rootScope', 'colors', 'models', 'plugins', function($rootScope, colors, models, plugins) {
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);
2013-10-08 16:13:48 +02:00
models.closeBuffer(buffer);
2013-10-02 02:32:18 +02:00
}
var handleLine = function(line, initial) {
2013-10-09 17:53:25 +02:00
var message = new models.BufferLine(line);
2013-10-06 12:34:41 +02:00
// Only react to line if its displayed
2013-10-09 17:53:25 +02:00
if(message.displayed) {
var buffer = models.getBuffer(message.buffer);
message.metadata = plugins.PluginManager.contentForMessage(message.text);
buffer.addLine(message);
2013-10-06 02:06:28 +02:00
2013-10-09 17:53:25 +02:00
if (buffer.active) {
$rootScope.scrollToBottom();
2013-10-06 12:34:41 +02:00
}
2013-10-08 15:55:07 +02:00
2013-10-09 17:53:25 +02:00
if (!initial) {
if (!buffer.active && _.contains(message.tags, 'notify_message') && !_.contains(message.tags, 'notify_none')) {
if (buffer.unread == '' || buffer.unread == undefined) {
buffer.unread = 1;
}else {
buffer.unread++;
}
}
2013-10-09 17:53:25 +02:00
if(message.highlight || _.contains(message.tags, 'notify_private') ) {
$rootScope.createHighlight(buffer, message);
buffer.notification = true;
}
2013-10-06 12:34:41 +02:00
}
2013-10-08 16:03:44 +02:00
}
2013-08-05 03:39:23 +02:00
}
2013-10-06 20:20:34 +02:00
var handleBufferLineAdded = function(message) {
message['objects'][0]['content'].forEach(function(l) {
handleLine(l, false);
2013-10-06 20:20:34 +02:00
});
}
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);
2013-10-08 16:05:46 +02:00
models.addBuffer(buffer);
2013-08-05 03:39:23 +02:00
}
2013-10-06 02:06:28 +02:00
2013-10-07 00:24:18 +02:00
var handleBufferTitleChanged = function(message) {
var obj = message['objects'][0]['content'][0];
var buffer = obj['pointers'][0];
var old = models.getBuffer(buffer);
old.fullName = obj['full_name'];
old.title = obj['title'];
old.number = obj['number'];
}
var handleBufferRenamed = function(message) {
var obj = message['objects'][0]['content'][0];
var buffer = obj['pointers'][0];
var old = models.getBuffer(buffer);
old.fullName = obj['full_name'];
old.shortName = obj['short_name'];
2013-08-05 03:39:23 +02:00
}
2013-10-06 12:34:41 +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
for (var i = 0; i < bufferInfos.length ; i++) {
2013-10-08 03:15:25 +02:00
var buffer = new models.Buffer(bufferInfos[i]);
2013-10-08 15:55:07 +02:00
models.addBuffer(buffer);
2013-10-11 16:11:19 +02:00
// Switch to first buffer on startup
if (i == 0) {
models.setActiveBuffer(buffer.id);
}
}
2013-10-08 15:55:07 +02:00
2013-10-06 20:20:34 +02:00
// Request latest buffer lines for each buffer
$rootScope.getLines();
}
/*
* Handle answers to (lineinfo) messages
*
* (lineinfo) messages are specified by this client. It is request after bufinfo completes
*/
var handleLineInfo = function(message) {
var lines = message['objects'][0]['content'].reverse();
lines.forEach(function(l) {
handleLine(l, true);
2013-10-06 20:20:34 +02:00
});
}
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 eventHandlers = {
bufinfo: handleBufferInfo,
2013-10-06 20:20:34 +02:00
lineinfo: handleLineInfo,
2013-10-06 02:06:28 +02:00
_buffer_closing: handleBufferClosing,
2013-08-05 03:39:23 +02:00
_buffer_line_added: handleBufferLineAdded,
2013-10-07 00:24:18 +02:00
_buffer_opened: handleBufferOpened,
_buffer_title_changed: handleBufferTitleChanged,
_buffer_renamed: handleBufferRenamed
2013-08-05 03:39:23 +02:00
}
return {
handleEvent: handleEvent
}
}]);
2013-10-08 15:55:07 +02:00
weechat.factory('connection', ['$rootScope', '$log', 'handlers', 'colors', 'models', function($rootScope, $log, handlers, colors, models) {
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 (host, port, passwd, ssl) {
2013-10-07 15:38:47 +02:00
var proto = ssl ? 'wss':'ws';
websocket = new WebSocket(proto+"://" + host + ':' + port + "/weechat");
2013-08-02 03:54:12 +02:00
websocket.binaryType = "arraybuffer"
2013-02-18 00:49:42 +01:00
2013-08-02 03:54:12 +02:00
websocket.onopen = function (evt) {
doSend(WeeChatProtocol.formatInit({
password: passwd,
compression: 'off'
}));
doSend(WeeChatProtocol.formatHdata({
id: 'bufinfo',
path: 'buffer:gui_buffers(*)',
2013-10-09 17:53:25 +02:00
keys: ['number,full_name,short_name,title']
}));
doSend(WeeChatProtocol.formatSync({}));
2013-08-05 21:53:59 +02: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-07 15:38:47 +02:00
if (evt.type == "error" && websocket.readyState != 1) {
2013-10-03 01:55:30 +02:00
$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 15:55:07 +02:00
buffer: models.getActiveBuffer()['fullName'],
data: message
}));
2013-08-02 03:54:12 +02:00
}
2013-10-06 20:20:34 +02:00
var getLines = function(count) {
2013-10-09 17:53:25 +02:00
doSend(WeeChatProtocol.formatHdata({
id: 'lineinfo',
path: "buffer:gui_buffers(*)/own_lines/last_line(-"+count+")/data",
keys: []
}));
2013-10-06 20:20:34 +02:00
}
2013-08-02 03:54:12 +02:00
return {
2013-10-06 20:20:34 +02:00
send: doSend,
getLines: getLines,
2013-08-02 03:54:12 +02:00
connect: connect,
sendMessage: sendMessage
}
2013-02-18 00:49:42 +01:00
}]);
2013-10-11 00:52:16 +02:00
weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout', 'models', 'connection', function ($rootScope, $scope, $store, $timeout, models, connection, testService) {
2013-10-08 15:55:07 +02:00
2013-10-06 12:34:41 +02:00
// Request notification permission
Notification.requestPermission(function (status) {
console.log('Notification permission status:',status);
if (Notification.permission !== status) {
Notification.permission = status;
}
});
if(window.webkitNotifications != undefined) {
if (window.webkitNotifications.checkPermission() == 0) { // 0 is PERMISSION_ALLOWED
console.log('Notification permission status:', window.webkitNotifications.checkPermission() == 0);
window.webkitNotifications.requestPermission();
}
}
2013-10-09 17:53:25 +02:00
2013-10-08 15:55:07 +02:00
$scope.buffers = models.model.buffers;
$scope.activeBuffer = models.getActiveBuffer
$scope.incrementAge = function () {
models.model.age++;
models.model.cats.push('nouveau chat');
}
$scope.clickS = function () {
$scope.countS = testService.incrementCount();
};
2013-02-18 00:49:42 +01:00
$rootScope.commands = []
2013-02-16 19:18:14 +01:00
2013-10-08 15:55:07 +02:00
$rootScope.models = models;
$rootScope.buffer = []
2013-10-08 15:55:07 +02:00
$store.bind($scope, "host", "localhost");
$store.bind($scope, "port", "9001");
$store.bind($scope, "proto", "weechat");
$store.bind($scope, "password", "");
2013-10-09 17:53:25 +02:00
$store.bind($scope, "ssl", false);
$store.bind($scope, "lines", "40");
// TODO checkbox for saving password or not?
// $scope.password = "";
2013-10-02 02:32:18 +02:00
2013-07-30 15:22:37 +02:00
$scope.setActiveBuffer = function(key) {
2013-10-08 15:55:07 +02:00
models.setActiveBuffer(key);
2013-10-09 17:53:25 +02:00
var ab = models.getActiveBuffer();
$rootScope.pageTitle = ab.shortName + ' | ' + ab.title;
2013-07-30 15:22:37 +02:00
};
2013-10-11 00:52:16 +02:00
$scope.$watch('models.getActiveBuffer()', function(newVal, oldVal) {
2013-10-07 16:58:41 +02:00
if (newVal && newVal !== oldVal) {
$rootScope.scrollToBottom();
}
});
$rootScope.scrollToBottom = function() {
// FIXME doesn't work if the settimeout runs without a short delay
// 300 ms seems to do the trick but creates a noticable flickr
$timeout(function() {
// TODO in the future, implement scrolling to last read line
2013-10-11 18:45:01 +02:00
var lastline = document.querySelector('.bufferline:last-child');
if(lastline) {
window.scrollTo(0, lastline.offsetTop);
}
}, 300);
2013-10-07 16:58:41 +02:00
}
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.host, $scope.port, $scope.password, $scope.ssl);
2013-02-16 19:18:14 +01:00
}
2013-10-06 20:20:34 +02:00
$rootScope.getLines = function() {
connection.getLines($scope.lines);
2013-02-16 19:18:14 +01:00
}
2013-10-06 12:34:41 +02:00
/* Function gets called from bufferLineAdded code if user should be notified */
2013-10-09 17:53:25 +02:00
$rootScope.createHighlight = function(buffer, message) {
2013-10-06 12:34:41 +02:00
var messages = "";
2013-10-09 17:53:25 +02:00
message.content.forEach(function(part) {
if (part.text != undefined)
messages += part.text + " ";
});
2013-10-06 12:34:41 +02:00
2013-10-09 17:53:25 +02:00
var title = buffer.fullName;
var content = messages;
2013-10-06 12:34:41 +02:00
var timeout = 15*1000;
2013-10-09 17:53:25 +02:00
console.log('Displaying notification:buffer:',buffer,',message:',message,',with timeout:',timeout);
2013-10-06 12:34:41 +02:00
var notification = new Notification(title, {body:content, icon:'img/favicon.png'});
// Cancel notification automatically
notification.onshow = function() {
setTimeout(function() { notification.close() }, timeout);
}
};
$scope.hasUnread = function(buffer) {
if($scope.onlyUnread) {
return (parseInt(buffer.unread) || 0) > 0;
}
return true;
};
2013-10-11 15:59:55 +02:00
$rootScope.switchToActivityBuffer = function() {
// Find next buffer with activity and switch to it
2013-10-11 22:43:01 +02:00
for(i in $scope.buffers) {
var buffer = $scope.buffers[i];
2013-10-11 15:59:55 +02:00
if(buffer.notification) {
$scope.setActiveBuffer(buffer.id);
2013-10-11 22:43:01 +02:00
break;
2013-10-11 15:59:55 +02:00
}else if((parseInt(buffer.unread) || 0) > 0) {
$scope.setActiveBuffer(buffer.id);
2013-10-11 22:43:01 +02:00
break;
2013-10-11 15:59:55 +02:00
}
2013-10-11 22:43:01 +02:00
}
2013-10-11 15:59:55 +02:00
}
$scope.handleKeyPress = function($event) {
2013-10-11 16:24:25 +02:00
// Support different browser quirks
var code = $event.keyCode ? $event.keyCode : $event.charCode;
2013-10-11 15:59:55 +02:00
//console.log('keypress', $event.charCode, $event.altKey);
2013-10-11 16:24:25 +02:00
2013-10-11 15:59:55 +02:00
// Handle alt-a
2013-10-11 16:24:25 +02:00
if($event.altKey && (code == 97 || code == 65)) {
2013-10-11 15:59:55 +02:00
$rootScope.switchToActivityBuffer();
return true;
}
// Handle ctrl-g
2013-10-11 16:24:25 +02:00
if($event.ctrlKey && (code == 103 || code == 71)) {
2013-10-11 15:59:55 +02:00
document.querySelector('#bufferFilter').focus();
return true;
}
};
2013-08-02 03:54:12 +02:00
}]
);