-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (97 loc) · 3.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'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.defaultPrefs = {
username: {
displayname: 'Username',
type: 'text',
value: ''
},
password: {
displayname: 'Password',
type: 'password',
value: ''
}
};
}
init() {
this.connect().then((client) => {
this.addOnlineListener(client);
this.addStanzaListener(client);
});
}
connect() {
return this.getAllPrefs().then((prefs) => {
const client = new xmppClient({
jid: prefs.username.value,
password: prefs.password.value,
host: "talk.google.com",
reconnect: true
});
client.connection.socket.setTimeout(0);
client.connection.socket.setKeepAlive(true, 10000);
return client;
});
}
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',
{
'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)
);
});
});
}
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);
}
});
}
}
module.exports = hangouts;