Advertisement
Guest User

Untitled

a guest
Jan 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. // Client is the actual Client class, xml is a convenience function for building
  2. // valid XML.
  3. const { client, xml, jid } = require('@xmpp/client');
  4.  
  5. const xmpp = client({
  6. service: 'xmpp://www.think5.de:5230',
  7. domain: 'www.think5.de',
  8. resource: 'greengekko',
  9. gglogin: {
  10. username: 'peter',
  11. password: 'peter'
  12. },
  13. guestLogin: true
  14. })
  15. var myJID;
  16.  
  17.  
  18. async function sendChannelMsg(price) {
  19. const message = xml(
  20. 'message',
  21. {type: 'groupchat', from: myJID, to: 'kraken-etheur@www.think5.de'},
  22. xml('body', {}, '{ candle: \'ETHEUR\', time: \'132342324\', close: \'' + price + '\' }')
  23. )
  24. await xmpp.send(message);
  25.  
  26. setTimeout(sendChannelMsg, 2000, Math.random() * (110 - 90) + 90);
  27. }
  28.  
  29.  
  30. xmpp.on('error', err => {
  31. console.error('❌', err.toString())
  32. })
  33.  
  34.  
  35. xmpp.on('offline', () => {
  36. console.log('⏹', 'offline')
  37. })
  38.  
  39.  
  40. xmpp.on('stanza', async stanza => {
  41. // *******************************
  42. // *** request server login
  43. if (stanza.is('iq') && stanza.children[0] !== undefined && stanza.children[0].name == 'bind' && stanza.attrs.type == 'result') {
  44. myJID = stanza.children[0].children[0].children[0];
  45. const auth = xml(
  46. 'iq', {type: 'get', id: 'auth1'},
  47. xml('query', {xmlns: 'jabber:iq:auth'},
  48. xml('username', {}, myJID.split('@')[0]))
  49. );
  50. await xmpp.send(auth);
  51. }
  52.  
  53.  
  54. // *******************************
  55. // *** perform server login
  56. if (stanza.is('iq') && stanza.children[0] !== undefined && stanza.attrs.type != 'error' && stanza.children[0].attrs.xmlns == 'jabber:iq:auth') {
  57. //login plain
  58. const setauth = xml(
  59. 'iq', {type: 'set', id: 'auth1'},
  60. xml('query', {xmlns: 'jabber:iq:auth'}, [
  61. xml('username', {}, myJID.split('@')[0]),
  62. xml('password', {}, xmpp.options.gglogin.password),
  63. xml('resource', {}, 'ggb')
  64. ])
  65. );
  66.  
  67. await xmpp.send(setauth);
  68. return;
  69. }
  70.  
  71.  
  72. // *******************************
  73. // *** continue after login
  74. if (stanza.is('iq') && stanza.children[0] == undefined && stanza.attrs.type == 'result') {
  75. console.log('Server confirmed our auth!');
  76. //xmpp.emit('online', 'guest@www.think5.de');
  77. //return;
  78.  
  79. // *** enter muc channel now
  80. const mucp = xml(
  81. 'presence', {from: myJID, to: 'kraken-etheur@www.think5.de/' + myJID.split('@')[0], id: 'muc1'},
  82. xml('x', {xmlns: 'http://jabber.org/protocol/muc'})
  83. );
  84.  
  85. await xmpp.send(mucp);
  86. // send some random prices into the room, testing
  87. setTimeout(sendChannelMsg, 1500, Math.random() * (110 - 90) + 90);
  88. return;
  89. }
  90.  
  91.  
  92. // *******************************
  93. // *** receive channel messages
  94. if (stanza.is('message') && stanza.attrs.type == 'groupchat' && stanza.children[0] !== undefined) {
  95. let msg = stanza.children[0].children[0]
  96. console.log('🛈', msg);
  97. //await xmpp.send(xml('presence', {type: 'unavailable'}))
  98. //await xmpp.stop();
  99. //process.exit();
  100. }
  101. })
  102.  
  103. xmpp.on('online', async address => {
  104. console.log('▶', 'online as', address.toString())
  105.  
  106. // Makes itself available
  107. await xmpp.send(xml('presence'))
  108.  
  109. // Sends a chat message to itself
  110. /*
  111. const message = xml(
  112. 'message',
  113. {type: 'chat', to: address},
  114. xml('body', {}, 'hello world')
  115. )
  116. await xmpp.send(message)
  117. */
  118.  
  119. const roster = xml(
  120. 'iq', {type: 'get', id: 'roster1', from: 'guest@www.think5.de/Mark'},
  121. xml('query', {xmlns: 'jabber:iq:roster'})
  122. );
  123. await xmpp.send(roster);
  124. })
  125.  
  126. // Debug
  127. xmpp.on('status', async status => {
  128. console.debug('🛈', 'status', status)
  129.  
  130. if (status == 'open') {
  131. console.log('Connection to Green Gekko Server established...now authenticating');
  132.  
  133. if (xmpp.options.guestLogin) {
  134. // *******************************
  135. // *** iq bind, to receive guest id
  136. xmpp.options.gglogin.password = 'guest';
  137. const bind = xml(
  138. 'iq', {type: 'set', id: 'auth1'},
  139. xml('bind', {xmlns: 'urn:ietf:params:xml:ns:xmpp-bind'})
  140. );
  141. await xmpp.send(bind);
  142. return;
  143. } else {
  144. myJID = xmpp.options.gglogin.username + '@' + xmpp.options.gglogin.domain
  145. const auth = xml(
  146. 'iq', {type: 'get', id: 'auth1'},
  147. xml('query', {xmlns: 'jabber:iq:auth'},
  148. xml('username', {}, myJID.split('@')[0]))
  149. );
  150. await xmpp.send(auth);
  151. }
  152. }
  153. })
  154. xmpp.on('input', input => {
  155. console.debug('⮈', input)
  156. })
  157. xmpp.on('output', output => {
  158. console.debug('⮊', output)
  159. })
  160.  
  161.  
  162.  
  163. // This actually launches the server.
  164. xmpp
  165. .start()
  166. //.start({uri: 'xmpp://www.think5.de:5230', domain: 'www.think5.de'})
  167. .catch(err => console.error('start failed', err.message));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement