Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Client is the actual Client class, xml is a convenience function for building
- // valid XML.
- const { client, xml, jid } = require('@xmpp/client');
- const xmpp = client({
- service: 'xmpp://www.think5.de:5230',
- domain: 'www.think5.de',
- resource: 'greengekko',
- gglogin: {
- username: 'peter',
- password: 'peter'
- },
- guestLogin: true
- })
- var myJID;
- async function sendChannelMsg(price) {
- const message = xml(
- 'message',
- {type: 'groupchat', from: myJID, to: 'kraken-etheur@www.think5.de'},
- xml('body', {}, '{ candle: \'ETHEUR\', time: \'132342324\', close: \'' + price + '\' }')
- )
- await xmpp.send(message);
- setTimeout(sendChannelMsg, 2000, Math.random() * (110 - 90) + 90);
- }
- xmpp.on('error', err => {
- console.error('❌', err.toString())
- })
- xmpp.on('offline', () => {
- console.log('⏹', 'offline')
- })
- xmpp.on('stanza', async stanza => {
- // *******************************
- // *** request server login
- if (stanza.is('iq') && stanza.children[0] !== undefined && stanza.children[0].name == 'bind' && stanza.attrs.type == 'result') {
- myJID = stanza.children[0].children[0].children[0];
- const auth = xml(
- 'iq', {type: 'get', id: 'auth1'},
- xml('query', {xmlns: 'jabber:iq:auth'},
- xml('username', {}, myJID.split('@')[0]))
- );
- await xmpp.send(auth);
- }
- // *******************************
- // *** perform server login
- if (stanza.is('iq') && stanza.children[0] !== undefined && stanza.attrs.type != 'error' && stanza.children[0].attrs.xmlns == 'jabber:iq:auth') {
- //login plain
- const setauth = xml(
- 'iq', {type: 'set', id: 'auth1'},
- xml('query', {xmlns: 'jabber:iq:auth'}, [
- xml('username', {}, myJID.split('@')[0]),
- xml('password', {}, xmpp.options.gglogin.password),
- xml('resource', {}, 'ggb')
- ])
- );
- await xmpp.send(setauth);
- return;
- }
- // *******************************
- // *** continue after login
- if (stanza.is('iq') && stanza.children[0] == undefined && stanza.attrs.type == 'result') {
- console.log('Server confirmed our auth!');
- //xmpp.emit('online', 'guest@www.think5.de');
- //return;
- // *** enter muc channel now
- const mucp = xml(
- 'presence', {from: myJID, to: 'kraken-etheur@www.think5.de/' + myJID.split('@')[0], id: 'muc1'},
- xml('x', {xmlns: 'http://jabber.org/protocol/muc'})
- );
- await xmpp.send(mucp);
- // send some random prices into the room, testing
- setTimeout(sendChannelMsg, 1500, Math.random() * (110 - 90) + 90);
- return;
- }
- // *******************************
- // *** receive channel messages
- if (stanza.is('message') && stanza.attrs.type == 'groupchat' && stanza.children[0] !== undefined) {
- let msg = stanza.children[0].children[0]
- console.log('🛈', msg);
- //await xmpp.send(xml('presence', {type: 'unavailable'}))
- //await xmpp.stop();
- //process.exit();
- }
- })
- xmpp.on('online', async address => {
- console.log('▶', 'online as', address.toString())
- // Makes itself available
- await xmpp.send(xml('presence'))
- // Sends a chat message to itself
- /*
- const message = xml(
- 'message',
- {type: 'chat', to: address},
- xml('body', {}, 'hello world')
- )
- await xmpp.send(message)
- */
- const roster = xml(
- 'iq', {type: 'get', id: 'roster1', from: 'guest@www.think5.de/Mark'},
- xml('query', {xmlns: 'jabber:iq:roster'})
- );
- await xmpp.send(roster);
- })
- // Debug
- xmpp.on('status', async status => {
- console.debug('🛈', 'status', status)
- if (status == 'open') {
- console.log('Connection to Green Gekko Server established...now authenticating');
- if (xmpp.options.guestLogin) {
- // *******************************
- // *** iq bind, to receive guest id
- xmpp.options.gglogin.password = 'guest';
- const bind = xml(
- 'iq', {type: 'set', id: 'auth1'},
- xml('bind', {xmlns: 'urn:ietf:params:xml:ns:xmpp-bind'})
- );
- await xmpp.send(bind);
- return;
- } else {
- myJID = xmpp.options.gglogin.username + '@' + xmpp.options.gglogin.domain
- const auth = xml(
- 'iq', {type: 'get', id: 'auth1'},
- xml('query', {xmlns: 'jabber:iq:auth'},
- xml('username', {}, myJID.split('@')[0]))
- );
- await xmpp.send(auth);
- }
- }
- })
- xmpp.on('input', input => {
- console.debug('⮈', input)
- })
- xmpp.on('output', output => {
- console.debug('⮊', output)
- })
- // This actually launches the server.
- xmpp
- .start()
- //.start({uri: 'xmpp://www.think5.de:5230', domain: 'www.think5.de'})
- .catch(err => console.error('start failed', err.message));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement