2014-08-24 18:53:55 +02:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
var weechat = angular.module('weechat');
|
|
|
|
|
|
|
|
weechat.factory('connection',
|
|
|
|
['$rootScope', '$log', 'handlers', 'models', 'ngWebsockets', function($rootScope,
|
|
|
|
$log,
|
|
|
|
handlers,
|
|
|
|
models,
|
|
|
|
ngWebsockets) {
|
|
|
|
|
2014-08-24 18:53:55 +02:00
|
|
|
var protocol = new weeChat.Protocol();
|
2014-08-28 18:25:40 +02:00
|
|
|
|
2014-10-18 12:25:14 +02:00
|
|
|
var connectionData = [];
|
|
|
|
var reconnectTimer;
|
2014-08-28 18:25:40 +02:00
|
|
|
|
2016-04-10 11:53:07 +02:00
|
|
|
// Global connection lock to prevent multiple connections from being opened
|
|
|
|
var locked = false;
|
|
|
|
|
2014-10-18 12:25:14 +02:00
|
|
|
// Takes care of the connection and websocket hooks
|
|
|
|
var connect = function (host, port, passwd, ssl, noCompression, successCallback, failCallback) {
|
2015-12-16 14:01:38 +01:00
|
|
|
$rootScope.passwordError = false;
|
2014-10-18 12:25:14 +02:00
|
|
|
connectionData = [host, port, passwd, ssl, noCompression];
|
2014-08-28 18:25:40 +02:00
|
|
|
var proto = ssl ? 'wss' : 'ws';
|
|
|
|
// If host is an IPv6 literal wrap it in brackets
|
2015-11-13 21:56:43 +01:00
|
|
|
if (host.indexOf(":") !== -1 && host[0] !== "[" && host[host.length-1] !== "]") {
|
2014-08-28 18:25:40 +02:00
|
|
|
host = "[" + host + "]";
|
|
|
|
}
|
|
|
|
var url = proto + "://" + host + ":" + port + "/weechat";
|
|
|
|
$log.debug('Connecting to URL: ', url);
|
|
|
|
|
|
|
|
var onopen = function () {
|
|
|
|
|
|
|
|
|
|
|
|
// Helper methods for initialization commands
|
|
|
|
var _initializeConnection = function(passwd) {
|
|
|
|
// This is not the proper way to do this.
|
|
|
|
// WeeChat does not send a confirmation for the init.
|
|
|
|
// Until it does, We need to "assume" that formatInit
|
|
|
|
// will be received before formatInfo
|
|
|
|
ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatInit({
|
|
|
|
password: passwd,
|
|
|
|
compression: noCompression ? 'off' : 'zlib'
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatInfo({
|
|
|
|
name: 'version'
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var _requestHotlist = function() {
|
|
|
|
return ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatHdata({
|
|
|
|
path: "hotlist:gui_hotlist(*)",
|
|
|
|
keys: []
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var _requestBufferInfos = function() {
|
|
|
|
return ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatHdata({
|
|
|
|
path: 'buffer:gui_buffers(*)',
|
2015-11-28 20:55:24 +01:00
|
|
|
keys: ['local_variables,notify,number,full_name,short_name,title,hidden,type']
|
2014-08-28 18:25:40 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
var _requestSync = function() {
|
|
|
|
return ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatSync({})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-07-19 05:00:11 +02:00
|
|
|
var _parseWeechatTimeFormat = function() {
|
2016-09-19 09:35:52 +02:00
|
|
|
// helper function to get a custom delimiter span
|
|
|
|
var _timeDelimiter = function(delim) {
|
|
|
|
return "'<span class=\"cof-chat_time_delimiters cob-chat_time_delimiters coa-chat_time_delimiters\">" + delim + "</span>'";
|
|
|
|
};
|
|
|
|
|
2016-07-19 05:00:11 +02:00
|
|
|
// Fetch the buffer time format from weechat
|
|
|
|
var timeFormat = models.wconfig['weechat.look.buffer_time_format'];
|
|
|
|
|
|
|
|
// Weechat uses strftime, with time specifiers such as %I:%M:%S for 12h time
|
|
|
|
// The time formatter we use, AngularJS' date filter, uses a different format
|
|
|
|
// Where %I:%M:%S would be represented as hh:mm:ss
|
|
|
|
// Here, we detect what format the user has set in Weechat and slot it into
|
|
|
|
// one of four formats, (short|long) (12|24)-hour time
|
|
|
|
var angularFormat = "";
|
|
|
|
|
2016-08-29 14:50:01 +02:00
|
|
|
var timeDelimiter = _timeDelimiter(":");
|
2016-07-19 05:00:11 +02:00
|
|
|
|
|
|
|
var left12 = "hh" + timeDelimiter + "mm";
|
|
|
|
var right12 = "' 'a";
|
|
|
|
|
|
|
|
var short12 = left12 + right12;
|
|
|
|
var long12 = left12 + timeDelimiter + "ss" + right12;
|
|
|
|
|
|
|
|
var short24 = "HH" + timeDelimiter + "mm";
|
|
|
|
var long24 = short24 + timeDelimiter + "ss";
|
|
|
|
|
|
|
|
if (timeFormat.indexOf("%H") > -1 ||
|
|
|
|
timeFormat.indexOf("%k") > -1) {
|
|
|
|
// 24h time detected
|
|
|
|
if (timeFormat.indexOf("%S") > -1) {
|
|
|
|
// show seconds
|
|
|
|
angularFormat = long24;
|
|
|
|
} else {
|
|
|
|
// don't show seconds
|
|
|
|
angularFormat = short24;
|
|
|
|
}
|
|
|
|
} else if (timeFormat.indexOf("%I") > -1 ||
|
|
|
|
timeFormat.indexOf("%l") > -1 ||
|
|
|
|
timeFormat.indexOf("%p") > -1 ||
|
|
|
|
timeFormat.indexOf("%P") > -1) {
|
|
|
|
// 12h time detected
|
|
|
|
if (timeFormat.indexOf("%S") > -1) {
|
|
|
|
// show seconds
|
|
|
|
angularFormat = long12;
|
|
|
|
} else {
|
|
|
|
// don't show seconds
|
|
|
|
angularFormat = short12;
|
|
|
|
}
|
|
|
|
} else if (timeFormat.indexOf("%r") > -1) {
|
|
|
|
// strftime doesn't have an equivalent for short12???
|
|
|
|
angularFormat = long12;
|
|
|
|
} else if (timeFormat.indexOf("%T") > -1) {
|
|
|
|
angularFormat = long24;
|
|
|
|
} else if (timeFormat.indexOf("%R") > -1) {
|
|
|
|
angularFormat = short24;
|
|
|
|
} else {
|
|
|
|
angularFormat = short24;
|
|
|
|
}
|
|
|
|
|
2016-09-19 10:04:15 +02:00
|
|
|
// Assemble date format
|
2016-09-19 10:02:31 +02:00
|
|
|
var date_components = [];
|
|
|
|
|
|
|
|
// Check for day of month in time format
|
|
|
|
var day_pos = Math.max(timeFormat.indexOf("%d"),
|
|
|
|
timeFormat.indexOf("%e"));
|
|
|
|
date_components.push([day_pos, "dd"]);
|
|
|
|
|
|
|
|
// month of year?
|
|
|
|
var month_pos = timeFormat.indexOf("%m");
|
|
|
|
date_components.push([month_pos, "MM"]);
|
|
|
|
|
|
|
|
// year as well?
|
|
|
|
var year_pos = Math.max(timeFormat.indexOf("%y"),
|
|
|
|
timeFormat.indexOf("%Y"));
|
|
|
|
if (timeFormat.indexOf("%y") > -1) {
|
|
|
|
date_components.push([year_pos, "yy"]);
|
|
|
|
} else if (timeFormat.indexOf("%Y") > -1) {
|
|
|
|
date_components.push([year_pos, "yyyy"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a date, assemble it in the right order
|
|
|
|
if (date_components.length > 0) {
|
|
|
|
date_components.sort();
|
|
|
|
var format_array = [];
|
|
|
|
for (var i = 0; i < date_components.length; i++) {
|
|
|
|
if (date_components[i][0] == -1) continue;
|
|
|
|
format_array.push(date_components[i][1]);
|
2016-08-29 14:50:01 +02:00
|
|
|
}
|
2016-09-19 10:02:31 +02:00
|
|
|
// TODO: parse delimiter as well? For now, use '/' as it is
|
|
|
|
// more common internationally than '-'
|
|
|
|
var date_format = format_array.join(_timeDelimiter("/"));
|
|
|
|
angularFormat = date_format + _timeDelimiter(" ") + angularFormat;
|
2016-08-29 14:50:01 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 05:00:11 +02:00
|
|
|
$rootScope.angularTimeFormat = angularFormat;
|
|
|
|
};
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
|
|
|
|
// First command asks for the password and issues
|
|
|
|
// a version command. If it fails, it means the we
|
|
|
|
// did not provide the proper password.
|
|
|
|
_initializeConnection(passwd).then(
|
2015-03-21 14:08:09 +01:00
|
|
|
function(version) {
|
|
|
|
handlers.handleVersionInfo(version);
|
2014-08-28 18:25:40 +02:00
|
|
|
// Connection is successful
|
|
|
|
// Send all the other commands required for initialization
|
|
|
|
_requestBufferInfos().then(function(bufinfo) {
|
2014-10-18 12:25:14 +02:00
|
|
|
handlers.handleBufferInfo(bufinfo);
|
2014-08-28 18:25:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
_requestHotlist().then(function(hotlist) {
|
|
|
|
handlers.handleHotlistInfo(hotlist);
|
2014-10-18 12:25:14 +02:00
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
});
|
2016-07-29 14:12:46 +02:00
|
|
|
// Schedule hotlist syncing every so often so that this
|
|
|
|
// client will have unread counts (mostly) in sync with
|
|
|
|
// other clients or terminal usage directly.
|
|
|
|
setInterval(function() {
|
|
|
|
if ($rootScope.connected) {
|
|
|
|
_requestHotlist().then(function(hotlist) {
|
|
|
|
handlers.handleHotlistInfo(hotlist);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 60000); // Sync hotlist every 60 second
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
|
2016-07-19 05:00:11 +02:00
|
|
|
// Fetch weechat time format for displaying timestamps
|
|
|
|
fetchConfValue('weechat.look.buffer_time_format',
|
|
|
|
function() {
|
2016-07-29 14:12:46 +02:00
|
|
|
// Will set models.wconfig['weechat.look.buffer_time_format']
|
2016-07-19 05:00:11 +02:00
|
|
|
_parseWeechatTimeFormat();
|
2016-07-29 14:12:46 +02:00
|
|
|
});
|
2016-07-19 05:00:11 +02:00
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
_requestSync();
|
|
|
|
$log.info("Connected to relay");
|
|
|
|
$rootScope.connected = true;
|
2016-07-29 14:12:46 +02:00
|
|
|
if (successCallback) {
|
|
|
|
successCallback();
|
|
|
|
}
|
2014-08-28 18:25:40 +02:00
|
|
|
},
|
2015-12-17 17:45:34 +01:00
|
|
|
function() {
|
2015-12-16 14:01:38 +01:00
|
|
|
handleWrongPassword();
|
2014-08-28 18:25:40 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var onmessage = function() {
|
|
|
|
// If we recieve a message from WeeChat it means that
|
|
|
|
// password was OK. Store that result and check for it
|
|
|
|
// in the failure handler.
|
|
|
|
$rootScope.waseverconnected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var onclose = function (evt) {
|
|
|
|
/*
|
|
|
|
* Handles websocket disconnection
|
|
|
|
*/
|
|
|
|
$log.info("Disconnected from relay");
|
2015-12-16 14:01:38 +01:00
|
|
|
$rootScope.$emit('relayDisconnect');
|
2016-04-10 11:53:07 +02:00
|
|
|
locked = false;
|
2014-10-18 12:25:14 +02:00
|
|
|
if ($rootScope.userdisconnect || !$rootScope.waseverconnected) {
|
|
|
|
handleClose(evt);
|
|
|
|
$rootScope.userdisconnect = false;
|
|
|
|
} else {
|
|
|
|
reconnect(evt);
|
|
|
|
}
|
2015-12-16 14:01:38 +01:00
|
|
|
handleWrongPassword();
|
2014-10-18 12:25:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var handleClose = function (evt) {
|
|
|
|
if (ssl && evt && evt.code === 1006) {
|
2014-08-28 18:25:40 +02:00
|
|
|
// A password error doesn't trigger onerror, but certificate issues do. Check time of last error.
|
|
|
|
if (typeof $rootScope.lastError !== "undefined" && (Date.now() - $rootScope.lastError) < 1000) {
|
|
|
|
// abnormal disconnect by client, most likely ssl error
|
|
|
|
$rootScope.sslError = true;
|
2015-03-27 10:22:42 +01:00
|
|
|
$rootScope.$apply();
|
2014-08-28 18:25:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-16 14:01:38 +01:00
|
|
|
var handleWrongPassword = function() {
|
|
|
|
// Connection got closed, lets check if we ever was connected successfully
|
|
|
|
if (!$rootScope.waseverconnected && !$rootScope.errorMessage) {
|
|
|
|
$rootScope.passwordError = true;
|
|
|
|
$rootScope.$apply();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
var onerror = function (evt) {
|
|
|
|
/*
|
|
|
|
* Handles cases when connection issues come from
|
|
|
|
* the relay.
|
|
|
|
*/
|
|
|
|
$log.error("Relay error", evt);
|
2016-04-10 11:53:07 +02:00
|
|
|
locked = false; // release connection lock
|
2014-08-28 18:25:40 +02:00
|
|
|
$rootScope.lastError = Date.now();
|
|
|
|
|
|
|
|
if (evt.type === "error" && this.readyState !== 1) {
|
2014-08-24 18:53:55 +02:00
|
|
|
ngWebsockets.failCallbacks('error');
|
2014-08-28 18:25:40 +02:00
|
|
|
$rootScope.errorMessage = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-10 11:53:07 +02:00
|
|
|
if (locked) {
|
|
|
|
// We already have an open connection
|
|
|
|
$log.debug("Aborting connection (lock in use)");
|
|
|
|
}
|
|
|
|
// Kinda need a compare-and-swap here...
|
|
|
|
locked = true;
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
try {
|
|
|
|
ngWebsockets.connect(url,
|
|
|
|
protocol,
|
|
|
|
{
|
|
|
|
'binaryType': "arraybuffer",
|
|
|
|
'onopen': onopen,
|
|
|
|
'onclose': onclose,
|
|
|
|
'onmessage': onmessage,
|
|
|
|
'onerror': onerror
|
|
|
|
});
|
|
|
|
} catch(e) {
|
2016-04-10 11:53:07 +02:00
|
|
|
locked = false;
|
2014-08-28 18:25:40 +02:00
|
|
|
$log.debug("Websocket caught DOMException:", e);
|
|
|
|
$rootScope.lastError = Date.now();
|
|
|
|
$rootScope.errorMessage = true;
|
|
|
|
$rootScope.securityError = true;
|
|
|
|
$rootScope.$emit('relayDisconnect');
|
2014-10-18 12:25:14 +02:00
|
|
|
|
|
|
|
if (failCallback) {
|
|
|
|
failCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var attemptReconnect = function (bufferId, timeout) {
|
|
|
|
$log.info('Attempting to reconnect...');
|
|
|
|
var d = connectionData;
|
|
|
|
connect(d[0], d[1], d[2], d[3], d[4], function() {
|
2015-03-14 21:10:19 +01:00
|
|
|
$rootScope.reconnecting = false;
|
2014-10-18 12:25:14 +02:00
|
|
|
// on success, update active buffer
|
|
|
|
models.setActiveBuffer(bufferId);
|
|
|
|
$log.info('Sucessfully reconnected to relay');
|
|
|
|
}, function() {
|
|
|
|
// on failure, schedule another attempt
|
|
|
|
if (timeout >= 600000) {
|
|
|
|
// If timeout is ten minutes or more, give up
|
|
|
|
$log.info('Failed to reconnect, giving up');
|
|
|
|
handleClose();
|
|
|
|
} else {
|
|
|
|
$log.info('Failed to reconnect, scheduling next attempt in', timeout/1000, 'seconds');
|
|
|
|
// Clear previous timer, if exists
|
|
|
|
if (reconnectTimer !== undefined) {
|
|
|
|
clearTimeout(reconnectTimer);
|
|
|
|
}
|
|
|
|
reconnectTimer = setTimeout(function() {
|
|
|
|
// exponential timeout increase
|
|
|
|
attemptReconnect(bufferId, timeout * 1.5);
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var reconnect = function (evt) {
|
|
|
|
if (connectionData.length < 5) {
|
|
|
|
// something is wrong
|
|
|
|
$log.error('Cannot reconnect, connection information is missing');
|
|
|
|
return;
|
2014-08-28 18:25:40 +02:00
|
|
|
}
|
|
|
|
|
2015-03-15 23:57:13 +01:00
|
|
|
// reinitialise everything, clear all buffers
|
|
|
|
// TODO: this can be further extended in the future by looking
|
|
|
|
// at the last line in ever buffer and request more buffers from
|
|
|
|
// WeeChat based on that
|
|
|
|
models.reinitialize();
|
2014-10-18 12:25:14 +02:00
|
|
|
$rootScope.reconnecting = true;
|
2015-03-14 21:10:19 +01:00
|
|
|
// Have to do this to get the reconnect banner to show
|
|
|
|
$rootScope.$apply();
|
2014-10-18 12:25:14 +02:00
|
|
|
|
|
|
|
var bufferId = models.getActiveBuffer().id,
|
|
|
|
timeout = 3000; // start with a three-second timeout
|
|
|
|
|
|
|
|
reconnectTimer = setTimeout(function() {
|
|
|
|
attemptReconnect(bufferId, timeout);
|
|
|
|
}, timeout);
|
2014-08-28 18:25:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var disconnect = function() {
|
2015-03-27 10:22:42 +01:00
|
|
|
$log.info('Disconnecting from relay');
|
2014-10-18 12:25:14 +02:00
|
|
|
$rootScope.userdisconnect = true;
|
2014-08-28 18:25:40 +02:00
|
|
|
ngWebsockets.send(weeChat.Protocol.formatQuit());
|
2015-03-27 10:22:42 +01:00
|
|
|
// In case the backend doesn't repond we will close from our end
|
|
|
|
var closeTimer = setTimeout(function() {
|
|
|
|
ngWebsockets.disconnect();
|
|
|
|
// We pretend we are not connected anymore
|
|
|
|
// The connection can time out on its own
|
|
|
|
ngWebsockets.failCallbacks('disconnection');
|
|
|
|
$rootScope.connected = false;
|
2016-04-10 11:53:07 +02:00
|
|
|
locked = false; // release the connection lock
|
2015-03-27 10:22:42 +01:00
|
|
|
$rootScope.$emit('relayDisconnect');
|
|
|
|
$rootScope.$apply();
|
|
|
|
});
|
2014-08-28 18:25:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format and send a weechat message
|
|
|
|
*
|
|
|
|
* @returns the angular promise
|
|
|
|
*/
|
|
|
|
var sendMessage = function(message) {
|
|
|
|
ngWebsockets.send(weeChat.Protocol.formatInput({
|
2015-03-26 11:02:06 +01:00
|
|
|
buffer: models.getActiveBufferReference(),
|
2014-08-28 18:25:40 +02:00
|
|
|
data: message
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
var sendCoreCommand = function(command) {
|
|
|
|
ngWebsockets.send(weeChat.Protocol.formatInput({
|
|
|
|
buffer: 'core.weechat',
|
|
|
|
data: command
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2015-03-23 12:58:56 +01:00
|
|
|
var sendHotlistClear = function() {
|
2015-06-06 16:53:38 +02:00
|
|
|
if (models.version[0] >= 1) {
|
2015-03-23 12:58:56 +01:00
|
|
|
// WeeChat >= 1 supports clearing hotlist with this command
|
|
|
|
sendMessage('/buffer set hotlist -1');
|
2015-04-18 12:10:49 +02:00
|
|
|
// Also move read marker
|
|
|
|
sendMessage('/input set_unread_current_buffer');
|
2015-03-23 12:58:56 +01:00
|
|
|
} else {
|
|
|
|
// If user wants to sync hotlist with weechat
|
|
|
|
// we will send a /buffer bufferName command every time
|
|
|
|
// the user switches a buffer. This will ensure that notifications
|
|
|
|
// are cleared in the buffer the user switches to
|
|
|
|
sendCoreCommand('/buffer ' + models.getActiveBuffer().fullName);
|
|
|
|
}
|
|
|
|
};
|
2014-08-28 18:25:40 +02:00
|
|
|
|
|
|
|
var requestNicklist = function(bufferId, callback) {
|
2015-11-28 12:22:56 +01:00
|
|
|
// Prevent requesting nicklist for all buffers if bufferId is invalid
|
|
|
|
if (!bufferId) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-28 18:25:40 +02:00
|
|
|
ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatNicklist({
|
2015-11-28 12:22:56 +01:00
|
|
|
buffer: "0x"+bufferId
|
2014-08-28 18:25:40 +02:00
|
|
|
})
|
|
|
|
).then(function(nicklist) {
|
|
|
|
handlers.handleNicklist(nicklist);
|
|
|
|
if (callback !== undefined) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-19 03:33:27 +02:00
|
|
|
var fetchConfValue = function(name, callback) {
|
2015-12-17 17:45:34 +01:00
|
|
|
ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatInfolist({
|
|
|
|
name: "option",
|
|
|
|
pointer: 0,
|
|
|
|
args: name
|
|
|
|
})
|
|
|
|
).then(function(i) {
|
|
|
|
handlers.handleConfValue(i);
|
2016-07-19 03:33:27 +02:00
|
|
|
if (callback !== undefined) {
|
|
|
|
callback();
|
|
|
|
}
|
2015-12-17 17:45:34 +01:00
|
|
|
});
|
|
|
|
};
|
2014-08-28 18:25:40 +02:00
|
|
|
|
|
|
|
var fetchMoreLines = function(numLines) {
|
|
|
|
$log.debug('Fetching ', numLines, ' lines');
|
|
|
|
var buffer = models.getActiveBuffer();
|
|
|
|
if (numLines === undefined) {
|
|
|
|
// Math.max(undefined, *) = NaN -> need a number here
|
|
|
|
numLines = 0;
|
|
|
|
}
|
|
|
|
// Calculate number of lines to fetch, at least as many as the parameter
|
|
|
|
numLines = Math.max(numLines, buffer.requestedLines * 2);
|
|
|
|
|
|
|
|
// Indicator that we are loading lines, hides "load more lines" link
|
|
|
|
$rootScope.loadingLines = true;
|
|
|
|
// Send hdata request to fetch lines for this particular buffer
|
|
|
|
return ngWebsockets.send(
|
|
|
|
weeChat.Protocol.formatHdata({
|
|
|
|
// "0x" is important, otherwise it won't work
|
|
|
|
path: "buffer:0x" + buffer.id + "/own_lines/last_line(-" + numLines + ")/data",
|
|
|
|
keys: []
|
|
|
|
})
|
|
|
|
).then(function(lineinfo) {
|
|
|
|
//XXX move to handlers?
|
|
|
|
// delete old lines and add new ones
|
|
|
|
var oldLength = buffer.lines.length;
|
2014-09-13 18:58:31 +02:00
|
|
|
// whether we already had all unread lines
|
|
|
|
var hadAllUnreadLines = buffer.lastSeen >= 0;
|
|
|
|
|
|
|
|
// clear the old lines
|
2014-08-28 18:25:40 +02:00
|
|
|
buffer.lines.length = 0;
|
|
|
|
// We need to set the number of requested lines to 0 here, because parsing a line
|
|
|
|
// increments it. This is needed to also count newly arriving lines while we're
|
|
|
|
// already connected.
|
|
|
|
buffer.requestedLines = 0;
|
|
|
|
// Count number of lines recieved
|
|
|
|
var linesReceivedCount = lineinfo.objects[0].content.length;
|
|
|
|
|
|
|
|
// Parse the lines
|
|
|
|
handlers.handleLineInfo(lineinfo, true);
|
|
|
|
|
2014-09-13 18:58:31 +02:00
|
|
|
// Correct the read marker for the lines that were counted twice
|
|
|
|
buffer.lastSeen -= oldLength;
|
|
|
|
|
2014-08-28 18:25:40 +02:00
|
|
|
// We requested more lines than we got, no more lines.
|
|
|
|
if (linesReceivedCount < numLines) {
|
|
|
|
buffer.allLinesFetched = true;
|
|
|
|
}
|
|
|
|
$rootScope.loadingLines = false;
|
2014-09-13 18:58:31 +02:00
|
|
|
|
|
|
|
// Only scroll to read marker if we didn't have all unread lines previously, but have them now
|
|
|
|
var scrollToReadmarker = !hadAllUnreadLines && buffer.lastSeen >= 0;
|
|
|
|
// Scroll to correct position
|
|
|
|
$rootScope.scrollWithBuffer(scrollToReadmarker, true);
|
2014-08-28 18:25:40 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
connect: connect,
|
|
|
|
disconnect: disconnect,
|
|
|
|
sendMessage: sendMessage,
|
|
|
|
sendCoreCommand: sendCoreCommand,
|
2015-03-23 12:58:56 +01:00
|
|
|
sendHotlistClear: sendHotlistClear,
|
2014-08-28 18:25:40 +02:00
|
|
|
fetchMoreLines: fetchMoreLines,
|
2014-10-18 12:25:14 +02:00
|
|
|
requestNicklist: requestNicklist,
|
|
|
|
attemptReconnect: attemptReconnect
|
2014-08-28 18:25:40 +02:00
|
|
|
};
|
|
|
|
}]);
|
2014-08-24 18:53:55 +02:00
|
|
|
})();
|