Skip to content

Commit

Permalink
Update for v2 of woodhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Bonaccorsi committed Nov 11, 2016
1 parent b69ad17 commit 1a5ae8d
Showing 1 changed file with 93 additions and 90 deletions.
183 changes: 93 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}

Expand Down

0 comments on commit 1a5ae8d

Please sign in to comment.