forked from xmppjs/xmpp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_mixin.js
56 lines (50 loc) · 1.36 KB
/
echo_mixin.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
/**
* Demonstrates how the echo behavior can be abstracted into a decorator,
* working equally well on clients, components or S2S.
*/
var xmpp = require('../lib/node-xmpp');
var argv = process.argv;
if (argv.length < 5 && argv) {
console.error('Usage: node echo_mixin.js client <my-jid> <my-password>');
console.error('Or: node echo_mixin.js component <my-jid> <my-password> <host> <port>');
process.exit(1);
}
function echoMixin(connection) {
connection.on('stanza',
function(stanza) {
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {
// Swap addresses...
stanza.attrs.to = stanza.attrs.from;
delete stanza.attrs.from;
// and send back.
connection.send(stanza);
}
});
}
function errorMixin(connection) {
connection.on('error',
function(e) {
console.error(e);
});
}
var cl = null;
if(argv[2] == "client")
cl = new xmpp.Client({ jid: argv[3],
password: argv[4]});
else
cl = new xmpp.Component({ jid: argv[3],
password: argv[4],
host: argv[5],
port: argv[6] });
cl.on('online',
function() {
cl.send(new xmpp.Element('presence',
{ type: 'chat'}).
c('show').t('chat').up().
c('status').t('Happily echoing your <message/> stanzas')
);
});
cl.addMixin(echoMixin);
cl.addMixin(errorMixin);