-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
84 lines (70 loc) · 2.37 KB
/
bot.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
/**
* Module dependencies
*/
var xmpp = require('node-xmpp')
, services = require('./services')
, config = require('./config')
, debug = require('debug')('gbot');
var Bot = module.exports = function(config) {
var conn = this.conn = new xmpp.Client(config.client);
conn.socket.setTimeout(0);
conn.socket.setKeepAlive(true, 10000);
conn.on('online', function(){ debug('Connected'); });
conn.on('online', this.setStatus.bind(this, config.status_message));
if(config.allow_auto_subscribe) {
conn.addListener('online', this.requestGoogleRoster.bind(this));
conn.addListener('stanza', this.acceptSubscriptionRequests.bind(this));
}
conn.addListener('stanza', this.requestHandler.bind(this));
};
Bot.prototype.setStatus = function(status_msg) {
var presence_elem = new xmpp.Element('presence', { })
.c('show').t('chat').up()
.c('status').t(status_msg);
this.conn.send(presence_elem);
};
Bot.prototype.requestGoogleRoster = function() {
var roster_elem = new xmpp.Element('iq', { from: this.conn.jid, type: 'get', id: 'google-roster'})
.c('query', { xmlns: 'jabber:iq:roster', 'xmlns:gr': 'google:roster', 'gr:ext': '2' });
this.conn.send(roster_elem);
};
Bot.prototype.acceptSubscriptionRequests = function(stanza) {
if(stanza.is('presence') && stanza.attrs.type === 'subscribe') {
var subscribe_elem = new xmpp.Element('presence', {
to: stanza.attrs.from,
type: 'subscribed'
});
this.conn.send(subscribe_elem);
this.send(config.welcome_message, stanza.attrs.from);
debug('Accepted new contact: ' + stanza.attrs.from);
}
};
Bot.prototype.requestHandler = function(stanza) {
var self = this;
if(stanza.is('message')) {
services.selectService(fetchMessage(stanza), function(out){
self.send(out, stanza.attrs.from);
});
}
};
Bot.prototype.send = function(msg, to) {
this.conn.send(
new xmpp.Element('message',
{ to: to,
type: 'chat'}).
c('body').
t(msg);
);
debug('Message: ' + msg + '\n sent to ' + to);
};
var fetchMessage = function(stanza) {
var msg = '';
stanza.children.some(function(el){
if(el.name === "body") {
msg = el.children.join('\n');
return true;
}
});
return msg;
};
services.load(config.services);