'use strict'; /* App Controllers */ function ConnectCtl($scope, StropheSrv, $log, $rootScope) { $rootScope.alias = {jid: "", status: 'offline' }; $rootScope.is_connected = function () { return $rootScope.status == Strophe.Status.CONNECTED; }; function connect_callback(status){ $rootScope.status = status; switch(status) { case Strophe.Status.CONNECTING: $log.log('Strophe is connecting.'); break; case Strophe.Status.CONNFAIL: $log.log('Strophe failed to connect.'); break; case Strophe.Status.DISCONNECTING: $log.log('Strophe is disconnecting.'); break; case Strophe.Status.DISCONNECTED: $log.log('Strophe is disconnected.'); break; case Strophe.Status.CONNECTED: $log.log('Strophe is connected.'); $rootScope.$broadcast('connected', true); } }; $scope.login = function () { StropheSrv.login($scope.username, $scope.password, connect_callback); $rootScope.alias["jid"] = $scope.username; }; $scope.disconnect = function() { StropheSrv.disconnect(); }; $scope.$on('connected', function() { $scope.getRegister(); }); $scope.getRegister = function() { var init = $iq({to: server_component, type:'get'}); init.c('query',{xmlns:'jabber:iq:register'}); StropheSrv.sendIQ(init, onRegister); }; function onRegister(iq) { $scope.form = $(iq).find('query x'); if ($(iq).find('registered').length !== 0){ // user is registered, get the info from the form var pubkey = $scope.form.find('field[var="pubkey"] > value').text(); var privkey = $scope.form.find('field[var="privkey"] > value').text(); privkey = sjcl.decrypt($scope.password, privkey); pubkey = JSON.parse(pubkey); privkey = JSON.parse(privkey); var rsa_key = new RSAKey(); rsa_key.setPublic(pubkey.n, pubkey.e); rsa_key.setPrivateEx(pubkey.n, pubkey.e, privkey.d, privkey.p, privkey.q, privkey.dp, privkey.dq, privkey.c); $rootScope.alias.rsa_key = rsa_key; }else{ $("#register .modal-body").append($scope.form.xmppForm('render')); $("#register").modal("show"); } }; $scope.generate_key = function() { var rsa_key = new RSAKey(); rsa_key.generate(1024, "10001"); $rootScope.alias.rsa_key = rsa_key; var pubkey = { n: rsa_key.n.toString(16), e: rsa_key.e.toString(16) }; var privkey = { d: rsa_key.d.toString(16), p: rsa_key.p.toString(16), q: rsa_key.q.toString(16), dp: rsa_key.dmp1.toString(16), dq: rsa_key.dmq1.toString(16), c: rsa_key.coeff.toString(16) }; privkey = sjcl.encrypt($scope.password, JSON.stringify(privkey)); $("#form-pubkey","#register").val(JSON.stringify(pubkey)); $("#form-privkey","#register").val(privkey); }; $scope.send_registration = function() { var pubkey = $(' body"); if (body.length === 0) { body = $(message).find('body'); if (body.length > 0) { body = body.text(); } else { body = null; } } else { body = body.contents(); var span = $(""); body.each(function () { if (document.importNode) { $(document.importNode(this, true)).appendTo(span); } else { // IE workaround span.append(this.xml); } }); body = span; } if (body) { addMessage(jid,jid,body); } }; $scope.sendMessage = function(message,to) { var msg = $msg({to: to, "type": "chat"}).c('body').t(message); addMessage(to,$rootScope.alias.jid, message); StropheSrv.send(msg); msg=''; }; $scope.$on('connected', function() { StropheSrv.addHandler(onMessage, 'message', 'chat'); }); $scope.$on('msgrequest', function(event, contact) { addTab(contact); $scope.activeConversation = contact; }); $scope.delete = function(conversation) { delete $scope.conversations[conversation]; }; $scope.activate = function(conversation) { $scope.activeConversation = conversation; }; $scope.isActive = function(conversation) { return $scope.activeConversation == conversation ? 'active' : ''; }; } MsgCtl.$inject = ['$scope','$log','StropheSrv', '$rootScope'];