Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-disable node/no-extraneous-require */
'use strict'
const {client, xml} = require('@xmpp/client')
const debug = require('@xmpp/debug')
const xmpp = client({
service: 'ws://localhost:5280/xmpp-websocket',
domain: 'localhost',
resource: 'example',
username: 'username',
password: 'password',
})
debug(xmpp, true)
xmpp.on('error', err => {
console.error(err)
})
xmpp.on('offline', () => {
console.log('offline')
})
/* eslint-disable node/no-extraneous-require */
/*
* This is a complete example of roster management with persistent versioning (caching) support
* it uses XML serialization and the filesystem for persistence but
* you can use anything you like such as IndexedDB and JSON as long as the version (ver) is correct
*/
'use strict'
const {client, xml} = require('@xmpp/client')
const debug = require('@xmpp/debug')
const Roster = require('.') // @xmpp/roster
const Storage = require('./Storage') // @xmpp/storage
const xmpp = client({
// service: 'ws://localhost:5280/xmpp-websocket',
// domain: 'localhost',
service: 'localhost',
username: 'username',
password: 'password',
})
debug(xmpp, true)
const storage = new Storage(xmpp)
// eslint-disable-next-line new-cap
xmpp.roster = Roster({...xmpp, storage})
xmpp.on('online', async () => {
const roster = await xmpp.roster.get()
console.log(
xml('presence', {from: stanza.attrs.to, to: stanza.attrs.from},
xml('x', {xmlns: 'http://jabber.org/protocol/muc#user'},
xml('item', {affiliation: 'owner', role: 'moderator'}),
xml('status', {code: '110'}),
xml('status', {code: '201'})
)
)
);
} else {
throw new Error('unknown stanza: ' + stanza.toString());
}
});
// when
const myOccupantJid = parseJid('chatroom@conference.example.com/me');
const room = await multiUserChatPlugin.joinRoom(myOccupantJid);
await multiUserChatPlugin.sendMessage(room, 'message body');
// then
expect(room.messages.length).toEqual(1);
expect(room.messages[0].body).toEqual('message body');
expect(room.messages[0].direction).toEqual(Direction.out);
expect(room.messages[0].id).not.toBeUndefined();
expect(room.messages[0].from).toEqual(myOccupantJid);
});
input: process.stdin,
output: process.stdout,
prompt: chalk.magenta.bold('✏ '),
}
if (Number(process.env.NODE_NO_READLINE)) {
options.terminal = false
}
const rl = readline.createInterface(options)
let prevent = false
let entity
if (flags.type === 'compomnent') {
entity = component.xmpp().component
} else {
entity = client.xmpp().client
}
const xconsole = new Console(entity)
xconsole.resetInput = function() {
rl.prompt()
}
xconsole.log = function(...args) {
readline.cursorTo(process.stdout, 0)
console.log(...args)
rl.prompt()
}
xconsole.info = function(...args) {
this.log(chalk.cyan.bold('🛈'), ...args)
}
xconsole.warning = function(...args) {
this.log(chalk.yellow.bold('⚠'), ...args)
describe('message archive plugin', () => {
let chatConnectionService: XmppChatConnectionService;
let chatAdapter: XmppChatAdapter;
let contactFactory: ContactFactoryService;
let xmppClientMock: SpyObj;
let contact1: Contact;
const userJid = parseJid('me@example.com/myresource');
const validArchiveStanza =
xml('message', {},
xml('result', {xmlns: 'urn:xmpp:mam:2'},
xml('forwarded', {},
xml('delay', {stamp: '2018-07-18T08:47:44.233057Z'}),
xml('message', {to: userJid.toString(), from: 'someone@else.com/resource'},
xml('origin-id', {id: 'id'}),
xml('body', {}, 'message text')))));
beforeEach(() => {
const mockClientFactory = new MockClientFactory();
xmppClientMock = mockClientFactory.clientInstance;
TestBed.configureTestingModule({
providers: [
XmppChatConnectionService,
{provide: XmppClientFactoryService, useValue: mockClientFactory},
XmppChatAdapter,
{provide: LogService, useValue: testLogService()},
ContactFactoryService
]
});
private sendAddToRoster(jid: string) {
return this.chatService.chatConnectionService.sendIq(
xml('iq', {type: 'set'},
xml('query', {xmlns: 'jabber:iq:roster'},
xml('item', {jid}))));
}
private convertSavedConferenceToElement(savedConference: SavedConference) {
const {name, autojoin, jid} = savedConference;
return xml('conference', {name, jid, autojoin: autojoin.toString()});
}
toStanza() {
return xml('iq', {type: 'get'},
xml('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'},
xml('items', {node: this.node})
)
);
}
domain = domain || getDomain(service);
this.logService.debug('registration plugin', 'connecting...');
await this.connect(username, password, service, domain);
this.logService.debug('registration plugin', 'connection established, starting registration');
await this.client.iqCaller.request(
xml('iq', {type: 'get', to: domain},
xml('query', {xmlns: 'jabber:iq:register'})
)
);
this.logService.debug('registration plugin', 'server acknowledged registration request, sending credentials');
await this.client.iqCaller.request(
xml('iq', {type: 'set'},
xml('query', {xmlns: 'jabber:iq:register'},
xml('username', {}, username),
xml('password', {}, password)
)
)
);
this.registered$.next();
await this.loggedIn$.pipe(takeUntil(this.cleanUp), first()).toPromise();
this.logService.debug('registration plugin', 'registration successful');
})(), this.registrationTimeout);
} catch (e) {
if (content.attrs.to === 'jabber.example.com'
&& content.getChild('query').attrs.xmlns === 'http://jabber.org/protocol/disco#items') {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#items'},
xml('item', {jid: 'conference.jabber.example.com'})
)
) as Stanza
);
} else if (content.getChild('query') && content.getChild('query').attrs.xmlns === 'http://jabber.org/protocol/disco#info') {
if (content.attrs.to === 'conference.jabber.example.com') {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id, from: content.attrs.to},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#info'},
xml('identity', {type: 'text', category: 'conference'})
)
) as Stanza
);
} else {
chatConnectionService.onStanzaReceived(
xml('iq', {type: 'result', id: content.attrs.id, from: content.attrs.to},
xml('query', {xmlns: 'http://jabber.org/protocol/disco#info'},
xml('identity', {type: 'type', category: 'category'})
)
) as Stanza
);
}
} else {
fail('unexpected stanza: ' + content.toString());
}
return Promise.resolve();