Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public getAccount(username: string, protocolId: string, mxid: string): IPurpleAccount|null {
const j = jid(username);
if (j.domain === this.myAddress.domain &&
j.local.startsWith("#") &&
this.serviceHandler.parseAliasFromJID(j)) {
// Account is an gateway alias, not trying.
return null;
}
const uLower = username.toLowerCase();
log.debug("Getting account", username);
if (protocolId !== "xmpp-js") {
return null;
}
if (this.accounts.has(uLower)) {
return this.accounts.get(uLower)!;
}
const acct = new XmppJsAccount(username, this.defaultRes, this, mxid);
this.accounts.set(uLower, acct);
private async handleMessageStanza(stanza: Element, alias: string|null) {
if (!stanza.attrs.from || !stanza.attrs.to) {
return;
}
const to = jid(stanza.attrs.to)!;
let localAcct: any = this.accounts.get(`${to!.local}@${to!.domain}`)!;
let from = jid(stanza.attrs.from);
let convName = `${from.local}@${from.domain}`;
if (alias) {
// If this is an alias, we want to do some gateway related things.
if (!to.resource) {
// Group message to a MUC, so reflect it to other XMPP users
// and set the right to/from addresses.
convName = `${to.local}@${to.domain}`;
log.info(`Sending gateway group message to ${convName}`);
if (!this.gateway!.reflectXMPPMessage(convName, stanza)) {
log.warn(`Message could not be sent, not forwarding to Matrix`);
return;
}
stanza.attrs.from = this.gateway!.getAnonIDForJID(
private async handleMessageStanza(stanza: Element, alias: string|null) {
if (!stanza.attrs.from || !stanza.attrs.to) {
return;
}
const to = jid(stanza.attrs.to)!;
let localAcct: any = this.accounts.get(`${to!.local}@${to!.domain}`)!;
let from = jid(stanza.attrs.from);
let convName = `${from.local}@${from.domain}`;
if (alias) {
// If this is an alias, we want to do some gateway related things.
if (!to.resource) {
// Group message to a MUC, so reflect it to other XMPP users
// and set the right to/from addresses.
convName = `${to.local}@${to.domain}`;
log.info(`Sending gateway group message to ${convName}`);
if (!this.gateway!.reflectXMPPMessage(convName, stanza)) {
log.warn(`Message could not be sent, not forwarding to Matrix`);
return;
}
stanza.attrs.from = this.gateway!.getAnonIDForJID(
convName, stanza.attrs.from) || false;
if (stanza.attrs.from === false) {
private handlePresenceStanza(stanza: Element, gatewayAlias: string|null) {
const to = jid(stanza.getAttr("to"));
// XMPP is case insensitive.
const localAcct = this.accounts.get(`${to.local}@${to.domain}`);
const from = jid(stanza.getAttr("from"));
const convName = `${from.local}@${from.domain}`;
const delta = this.presenceCache.add(stanza);
if (!delta) {
return;
}
if (delta.error && localAcct) {
if (delta.error === "conflict") {
log.info(`${from.toString()} conflicted with another user, attempting to fix`);
localAcct.xmppRetryJoin(from).catch((err) => {
log.error("Failed to retry join", err);
});
// This is a PM *to* matrix
log.info(`Sending gateway PM to ${userId} (${to})`);
localAcct = undefined;
for (const acct of this.accounts.values()) {
if (acct.mxId === userId) {
localAcct = acct;
break;
}
}
if (localAcct === undefined) {
log.warn(`No account defined for ${userId}, registering new account.`);
localAcct = await this.autoRegister!.registerUser(XMPP_PROTOCOL.id, userId) as XmppJsAccount;
}
const anonJid = this.gateway!.getAnonIDForJID(`${to.local}@${to.domain}`, from);
if (anonJid) {
from = jid(anonJid);
} else {
log.error("Couldn't find anon jid for PM");
return;
}
} else {
// This is a PM to another XMPP user, easy.
log.info(`Sending gateway PM to XMPP user (${to})`);
this.gateway!.reflectPM(stanza);
return;
}
}
}
const chatState = stanza.getChildByAttr("xmlns", "http://jabber.org/protocol/chatstates");
if (stanza.attrs.type === "error") {
// We got an error back from sending a message, let's handle it.
private remoteLeft(stanza: Element) {
log.info(`${stanza.attrs.from} left ${stanza.attrs.to}`);
const to = jid(stanza.attrs.to);
const chatName = `${to.local}@${to.domain}`;
const user = this.members.getXmppMemberByRealJid(chatName, stanza.attrs.from);
if (!user) {
log.error(`User tried to leave room, but they aren't in the member list`);
return;
}
this.members.removeXmppMember(chatName, stanza.attrs.from);
const leaveStza = new StzaPresenceItem(
user.anonymousJid.toString(),
stanza.attrs.to,
undefined,
"member",
"none",
true,
stanza.attrs.from,
);
public async getUserInfo(who: string): Promise {
const j = jid(who);
const status = this.xmpp.presenceCache.getStatus(who);
const ui: IUserInfo = {
Nickname: j.resource || j.local,
eventName: "meh",
who,
account: {
protocol_id: this.protocol.id,
username: this.remoteId,
},
};
if (status && status.photoId) {
ui.Avatar = status.photoId;
}
return ui;
}
private async handleRoomDiscovery(toStr: string, from: string, id: string) {
const to = jid(toStr);
const alias = this.parseAliasFromJID(to);
try {
if (!alias) {
throw Error("Not a valid alias");
}
log.debug(`Running room discovery for ${toStr}`);
let roomId = this.existingAliases.get(alias);
if (!roomId) {
roomId = await this.queryRoom(alias, true) as string;
this.existingAliases.set(alias, roomId);
}
log.info(`Response for alias request ${toStr} (${alias}) -> ${roomId}`);
await this.xmpp.xmppWriteToStream(
x("iq", {
type: "result",
to: from,