Handle wrong password. Fixes #4.

This commit introduces a way to handle wrong password.

The only way to know it is by sending an other message after the init
one.
If we receive an answer to this second message id then we know we are
connected with the good password, otherwise we will received an onclose
event from the websocket.
This commit is contained in:
Sebastien Bourdelin 2013-10-17 12:36:53 -04:00 committed by David Cormier
parent 55b720466b
commit d6de5805a2
2 changed files with 77 additions and 37 deletions

View file

@ -56,6 +56,9 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="control-label" for="password">WeeChat relay password</label> <label class="control-label" for="password">WeeChat relay password</label>
<div class="alert alert-danger" ng-show="passwordError">
Error wrong password
</div>
<input type="password" class="form-control" id="password" ng-model="password" placeholder="Password"> <input type="password" class="form-control" id="password" ng-model="password" placeholder="Password">
<p class="help-block">Password will be stored in your browser session</p> <p class="help-block">Password will be stored in your browser session</p>
</div> </div>

View file

@ -78,6 +78,7 @@ weechat.factory('handlers', ['$rootScope', 'colors', 'models', 'plugins', functi
old.title = obj['title']; old.title = obj['title'];
old.number = obj['number']; old.number = obj['number'];
} }
var handleBufferRenamed = function(message) { var handleBufferRenamed = function(message) {
var obj = message['objects'][0]['content'][0]; var obj = message['objects'][0]['content'][0];
var buffer = obj['pointers'][0]; var buffer = obj['pointers'][0];
@ -150,8 +151,6 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
var callbacks = {} var callbacks = {}
var currentCallBackId = 0; var currentCallBackId = 0;
var doSendWithCallback = function(message) { var doSendWithCallback = function(message) {
var defer = $q.defer(); var defer = $q.defer();
callbacks[++currentCallBackId] = { callbacks[++currentCallBackId] = {
@ -180,12 +179,39 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
websocket.binaryType = "arraybuffer" websocket.binaryType = "arraybuffer"
websocket.onopen = function (evt) { websocket.onopen = function (evt) {
$log.info("Connected to relay"); $log.info("Connected to relay");
$rootScope.connected = true;
// First message must be an init request
// with the password
doSend(weeChat.Protocol.formatInit({ doSend(weeChat.Protocol.formatInit({
password: passwd, password: passwd,
compression: 'off' compression: 'off'
})); }));
// password is bad until the next message
// received proven the otherwise.
$rootScope.passwordError = true;
// We are asking for the weechat version here
// to avoid two problems :
// - If the version is below 0.4.2, we will have a bug
// with websocket.
// - If the user password is wrong, we will be disconneted
// at this step.
doSendWithCallback(weeChat.Protocol.formatInfo({
name: 'version',
})).then(function(message) {
// If we have received this message
// that means the user password is good.
$rootScope.passwordError = false;
// Parse the version info message to retrieve
// the current weechat version.
var version = message['objects'][0]['content']['value'];
$rootScope.version = version;
$log.info(version);
}).then(function() {
doSendWithCallback(weeChat.Protocol.formatHdata({ doSendWithCallback(weeChat.Protocol.formatHdata({
path: 'buffer:gui_buffers(*)', path: 'buffer:gui_buffers(*)',
keys: ['local_variables,notify,number,full_name,short_name,title'] keys: ['local_variables,notify,number,full_name,short_name,title']
@ -220,12 +246,19 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
}).then(function() { }).then(function() {
doSend(weeChat.Protocol.formatSync({})); doSend(weeChat.Protocol.formatSync({}));
$log.info("Synced"); $log.info("Synced");
// here we are really connected !
$rootScope.connected = true;
});
}); });
} }
websocket.onclose = function (evt) { websocket.onclose = function (evt) {
$log.info("Disconnected from relay"); $log.info("Disconnected from relay");
$rootScope.connected = false; $rootScope.connected = false;
if ($rootScope.passwordError == true) {
$log.info("wrong password");
}
$rootScope.$apply(); $rootScope.$apply();
} }
@ -243,6 +276,10 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers',
} }
websocket.onerror = function (evt) { websocket.onerror = function (evt) {
// on error it means the connection problem
// come from the relay not from the password.
$rootScope.passwordError = false;
if (evt.type == "error" && websocket.readyState != 1) { if (evt.type == "error" && websocket.readyState != 1) {
$rootScope.errorMessage = true; $rootScope.errorMessage = true;
} }