From 1a5ae8d77ecab7ec897306eccdea0911fb0f8dea Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Fri, 11 Nov 2016 16:03:25 +0000 Subject: [PATCH] Update for v2 of woodhouse --- index.js | 183 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 93 insertions(+), 90 deletions(-) diff --git a/index.js b/index.js index 4d37310..2d7d496 100644 --- a/index.js +++ b/index.js @@ -1,107 +1,110 @@ -var xmppCore = require('node-xmpp-core'), - xmppClient = require('node-xmpp-client'), - hangouts = function(){ +'use strict'; + +const xmppCore = require('node-xmpp-core'); +const xmppClient = require('node-xmpp-client'); + +class hangouts { + constructor() { this.name = 'hangouts'; this.displayname = 'Google Hangouts Chat'; this.description = 'Send messages to woodhouse via Google Hangouts'; - this.disconnected = false; - this.defaultPrefs = [{ - name: 'username', - displayname: 'Username', - type: 'text', - value: '' - },{ - name: 'password', - displayname: 'Password', - type: 'password', - value: '' - }]; + this.defaultPrefs = { + username: { + displayname: 'Username', + type: 'text', + value: '' + }, + password: { + displayname: 'Password', + type: 'password', + value: '' + } + }; } -hangouts.prototype.init = function(){ - this.getPrefs().done(function(prefs){ - this.prefs = prefs; - this.connect(); - }.bind(this)); - -} - -hangouts.prototype.connect = function() { - this.connection = connection = new xmppClient({ - jid: this.prefs.username, - password: this.prefs.password, - host: "talk.google.com", - reconnect: true - }); - - this.connection.connection.socket.setTimeout(0) - this.connection.connection.socket.setKeepAlive(true, 10000) + init() { + this.connect().then((client) => { + this.addOnlineListener(client); + this.addStanzaListener(client); + }); + } - this.connection.on('disconnect', function() { - if (!this.disconnected) { - this.disconnected = true; - this.connection.end(); - this.connect(); - } - }.bind(this)) + connect() { + return this.getAllPrefs().then((prefs) => { + const client = new xmppClient({ + jid: prefs.username.value, + password: prefs.password.value, + host: "talk.google.com", + reconnect: true + }); - this.connection.on('online', function() { - this.disconnected = false; - connection.send(new xmppCore.Element('presence', {}) - .c('show') - .t('chat') - .up() - .c('status') - .t('Online') - ); + client.connection.socket.setTimeout(0); + client.connection.socket.setKeepAlive(true, 10000); - var roster_elem = new xmppCore.Element('iq', { - 'from': connection.jid, - 'type': 'get', - 'id': 'google-roster' - }).c('query', { - 'xmlns': 'jabber:iq:roster', - 'xmlns:gr': 'google:roster', - 'gr:ext': '2' + return client; }); + } - connection.send(roster_elem); - - this.addMessageSender(function(message, to){ - var stanza = new xmppCore.Element('message', + addOnlineListener(client) { + client.on('online', () => { + client.send(new xmppCore.Element('presence', {}) + .c('show') + .t('chat') + .up() + .c('status') + .t('Online') + ); + + client.send( + new xmppCore.Element( + 'iq', + { + 'from': client.jid, + 'type': 'get', + 'id': 'google-roster' + } + ).c( + 'query', { - to: to, - type: 'chat' - }) + 'xmlns': 'jabber:iq:roster', + 'xmlns:gr': 'google:roster', + 'gr:ext': '2' + } + ) + ); + + this.addMessageSender((to, message) => { + client.send( + new xmppCore.Element('message', {to: to, type: 'chat'}) .c('body') - .t(message); - - this.connection.send(stanza); - }.bind(this)); - }.bind(this)); - - - this.connection.on('stanza', function(stanza) { - if (stanza.is('message') && (stanza.attrs.type !== 'error') && (stanza.getChildText('body'))) { - var userParts = stanza.attrs.from.split('/'); - - userParts = userParts.slice(0, userParts.length - 1); - this.messageRecieved(stanza.attrs.from, stanza.getChildText('body'), userParts.join('/')); - } - - if(stanza.is('presence') && stanza.attrs.type === 'subscribe') { - stanza.attrs.to = stanza.attrs.from; - delete stanza.attrs.from; - - connection.send(stanza); - } - }.bind(this)); -} + .t(message) + ); + }); + }); + } -hangouts.prototype.exit = function(){ - if (this.connection) { - this.connection.end(); + addStanzaListener(client) { + client.on('stanza', (stanza) => { + if ( + stanza.is('message') && + stanza.attrs.type !== 'error' && + stanza.getChildText('body') + ) { + this.messageRecieved( + stanza.attrs.from, + stanza.getChildText('body'), + stanza.attrs.from.split('/').slice(0, -1).join('/') + ); + } + + if (stanza.is('presence') && stanza.attrs.type === 'subscribe') { + stanza.attrs.to = stanza.attrs.from; + delete stanza.attrs.from; + + client.send(stanza); + } + }); } }