mirror of
https://git.sr.ht/~emerson/reflectionircd
synced 2025-08-05 16:59:10 +00:00
basic message sending
This commit is contained in:
parent
c5aae31973
commit
c6b7488a9c
1 changed files with 60 additions and 9 deletions
|
@ -4,28 +4,25 @@ import { IRCUser } from "./IRCUser.js";
|
||||||
import { IRCMessage } from "./Message.js";
|
import { IRCMessage } from "./Message.js";
|
||||||
import { Client } from "./Client.js";
|
import { Client } from "./Client.js";
|
||||||
import numerics from "./numerics.js";
|
import numerics from "./numerics.js";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
export class Channel {
|
export class Channel {
|
||||||
public name: string
|
public name: string
|
||||||
private matrixUsers: Map<string, MatrixUser>
|
private matrixUsers: Map<string, MatrixUser>
|
||||||
private ircUsers: Map<string, IRCUser>
|
private ircUsers: Map<string, IRCUser>
|
||||||
private nickToMXid: Map<string, string>
|
|
||||||
private powerLevels: Map<string, number>
|
private powerLevels: Map<string, number>
|
||||||
private topic: Map<string, string>;
|
private topic: Map<string, string>;
|
||||||
private modes: Map<string, string>
|
private modes: Map<string, string>
|
||||||
private messages: Map<string, IRCMessage>;
|
private eventIDsSeen: Set<string>;
|
||||||
private tsToEventId: Map<number, string>;
|
|
||||||
constructor(public roomId: string, private server: Server) {
|
constructor(public roomId: string, private server: Server) {
|
||||||
this.name = roomId;
|
this.name = roomId;
|
||||||
this.matrixUsers = new Map();
|
this.matrixUsers = new Map();
|
||||||
this.ircUsers = new Map();
|
this.ircUsers = new Map();
|
||||||
this.nickToMXid = new Map();
|
|
||||||
this.powerLevels = new Map();
|
this.powerLevels = new Map();
|
||||||
this.topic = new Map([['text', ''], ['timestamp', '0'], ['setter', 'matrix']]);
|
this.topic = new Map([['text', ''], ['timestamp', '0'], ['setter', 'matrix']]);
|
||||||
this.modes = new Map();
|
this.modes = new Map();
|
||||||
this.modes.set('n', '');
|
this.modes.set('n', '');
|
||||||
this.messages = new Map();
|
this.eventIDsSeen = new Set();
|
||||||
this.tsToEventId = new Map();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getNickPowerLevelMapping(nick: string): string {
|
getNickPowerLevelMapping(nick: string): string {
|
||||||
|
@ -44,9 +41,9 @@ export class Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
joinNewIRCClient(client: Client, passedTags: Map<string, string>) {
|
joinNewIRCClient(client: Client, passedTags: Map<string, string>) {
|
||||||
console.log('joining channel')
|
|
||||||
if (!client.user)
|
if (!client.user)
|
||||||
return;
|
return;
|
||||||
|
this.ircUsers.set(client.user.nick, client.user);
|
||||||
if (client.enabledCaps.has('extended-join')) {
|
if (client.enabledCaps.has('extended-join')) {
|
||||||
client.sendMessage(client.user.getMask(), "JOIN", [this.name, client.user.accountName, client.user.mxid], new Map([['account', client.user.realname]]));
|
client.sendMessage(client.user.getMask(), "JOIN", [this.name, client.user.accountName, client.user.mxid], new Map([['account', client.user.realname]]));
|
||||||
} else {
|
} else {
|
||||||
|
@ -126,13 +123,19 @@ export class Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
routeMatrixEvent(event: any) {
|
routeMatrixEvent(event: any) {
|
||||||
if (!event["type"])
|
if (!event["type"] || !event["event_id"] || !event["origin_server_ts"])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (event["type"]) {
|
switch (event["type"]) {
|
||||||
case 'm.room.member':
|
case 'm.room.member':
|
||||||
this.handleMatrixMember(event);
|
this.handleMatrixMember(event);
|
||||||
break;
|
break;
|
||||||
|
case 'm.room.message': {
|
||||||
|
if (this.eventIDsSeen.has(event["event_id"]))
|
||||||
|
return;
|
||||||
|
this.eventIDsSeen.add(event["event_id"]);
|
||||||
|
this.handleMatrixMessage(event);
|
||||||
|
}
|
||||||
case 'm.room.power_levels':
|
case 'm.room.power_levels':
|
||||||
this.handleMatrixPL(event);
|
this.handleMatrixPL(event);
|
||||||
break;
|
break;
|
||||||
|
@ -145,10 +148,58 @@ export class Channel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
joinMatrixUser(matrixUser: MatrixUser, event: any) {
|
||||||
|
this.matrixUsers.set(matrixUser.nick, matrixUser);
|
||||||
|
const prefix = matrixUser.getMask();
|
||||||
|
if (event) {
|
||||||
|
const tags = new Map([["account", matrixUser.accountName], ['time', new Date(event["origin_server_ts"]).toISOString()]])
|
||||||
|
for (const user of this.ircUsers.values()) {
|
||||||
|
user.sendToAllWithCap('extended-join', prefix, "JOIN", [this.name, matrixUser.accountName, matrixUser.realname], tags);
|
||||||
|
user.sendToAllWithoutCap('extended-join', prefix, "JOIN", [this.name], tags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleMatrixMember(event: any) {
|
handleMatrixMember(event: any) {
|
||||||
const thisMatrixUser = this.server.getOrCreateMatrixUser(event["sender"]);
|
const thisMatrixUser = this.server.getOrCreateMatrixUser(event["sender"]);
|
||||||
this.matrixUsers.set(thisMatrixUser.nick, thisMatrixUser);
|
this.matrixUsers.set(thisMatrixUser.nick, thisMatrixUser);
|
||||||
this.nickToMXid.set(thisMatrixUser.nick, thisMatrixUser.mxid);
|
}
|
||||||
|
|
||||||
|
handleMatrixMessage(event: any) {
|
||||||
|
const thisMatrixUser = this.server.getOrCreateMatrixUser(event["sender"]);
|
||||||
|
if (!this.matrixUsers.has(thisMatrixUser.nick)) {
|
||||||
|
this.joinMatrixUser(thisMatrixUser, event);
|
||||||
|
}
|
||||||
|
const content = event["content"];
|
||||||
|
const msgtype = content["msgtype"];
|
||||||
|
let messageContent = content["body"];
|
||||||
|
const tags: Map<string, string> = new Map();
|
||||||
|
tags.set('msgid', event["event_id"]);
|
||||||
|
tags.set('account', thisMatrixUser.accountName);
|
||||||
|
tags.set('time', new Date(event["origin_server_ts"]).toISOString())
|
||||||
|
const maybeReply = content["m.relates_to"]?.["m.in_reply_to"]?.["event_id"];
|
||||||
|
if (maybeReply) {
|
||||||
|
tags.set('+draft/reply', maybeReply);
|
||||||
|
}
|
||||||
|
const ircCommand = (msgtype === 'm.notice') ? 'NOTICE' : 'PRIVMSG';
|
||||||
|
if (msgtype === 'm.emote') {
|
||||||
|
messageContent = `\x01ACTION ${messageContent}\x01`;
|
||||||
|
}
|
||||||
|
else if (['m.image', 'm.file', 'm.audio', 'm.video'].includes(msgtype)) {
|
||||||
|
let url: string = content["url"];
|
||||||
|
if (!url)
|
||||||
|
return;
|
||||||
|
messageContent = `\x01ACTION shared ${messageContent}: ${url}\x01`;
|
||||||
|
}
|
||||||
|
const msgArray = (messageContent.indexOf('\n') !== -1) ? messageContent.split('\n'): [messageContent];
|
||||||
|
msgArray.forEach((msg: string) => {
|
||||||
|
if (msg) {
|
||||||
|
this.ircUsers.forEach((user) => {
|
||||||
|
//@ts-ignore
|
||||||
|
user.sendToAll(sender.getMask(), ircCommand, [this.name, msg], tags)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMatrixPL(event: any) {
|
handleMatrixPL(event: any) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue