~emerson/clarityirc

8621fcf3840a5818c54079fc921489e3cb66f237 — emerson 1 year, 4 months ago 70fa638
add NICK and PART
1 files changed, 43 insertions(+), 0 deletions(-)

M src/irc/WebSocketListener.ts
M src/irc/WebSocketListener.ts => src/irc/WebSocketListener.ts +43 -0
@@ 3,6 3,7 @@ import { IRCMessage, parseIRCMessage } from "./IRCMessage";
import { IRCNetwork } from "./IRCNetwork";
import { addNetworkToStore, addWebSocketToStore, getOrCreateChannelToStore } from "@/main";
import { DisplayMessage } from "./DisplayMessage";
import { useNetworkStore } from '@/stores/networks';


export class WebSocketListener {


@@ 150,6 151,12 @@ export class WebSocketListener {
					if (m.command === 'JOIN') {
						return new DisplayMessage(timeSent, m.prefix.slice(1), "JOIN", "has joined", message);
					}
					if (m.command === 'NICK') {
						return new DisplayMessage(timeSent, m.prefix.slice(1), "NICK", `has changed nick to ${message.params[0]}`, message);
					}
					if (m.command === 'PART') {
						return new DisplayMessage(timeSent, m.prefix.slice(1), "PART", "has left", message);
					}
					if (m.command === 'NOTICE' || m.command === 'PRIVMSG') {
						return new DisplayMessage(timeSent, m.prefix.slice(1), m.command, m.params[1], message);
					}


@@ 249,6 256,25 @@ export class WebSocketListener {
		targetChannel.messages.push(newDisplayMessage);
	}

	handleNICK(message: IRCMessage) {
		const maskRegex = message.prefix.slice(1).match('^(.+)!(.+)@(.+)');
		let oldNick = message.prefix.slice(1)
		if (maskRegex) {
			oldNick = maskRegex[1]
		}
		const newNick = message.params[0]
		const timeSent = new Date(message.tags.get('time') ?? new Date())
		const newDisplayMessage = new DisplayMessage(timeSent, oldNick, "NICK", `has changed nick to ${newNick}`, message);
		const { channels } = useNetworkStore()
		channels.get(this.network.name)?.forEach(c => {
			if (c.nicklist.has(oldNick)) {
				c.nicklist.delete(oldNick);
				c.nicklist.add(newNick);
				c.messages.push(newDisplayMessage);
			}
		})
	}

	handleNOTICE(message: IRCMessage) {
		const targetChannel = getOrCreateChannelToStore(message.params[0], this.network)
		if (!targetChannel) {


@@ 265,6 291,23 @@ export class WebSocketListener {
		targetChannel.addMessage(newDisplayMessage);
	}

	handlePART(message: IRCMessage) {
		const targetChannel = getOrCreateChannelToStore(message.params[0], this.network)
		if (!targetChannel) {
			this.network.serverMessages.push(message);
			return;
		}
		const maskRegex = message.prefix.slice(1).match('^(.+)!(.+)@(.+)');
		let user = message.prefix.slice(1)
		if (maskRegex) {
			user = maskRegex[1]
		}
		targetChannel.nicklist.add(user)
		const timeSent = new Date(message.tags.get('time') ?? new Date())
		const newDisplayMessage = new DisplayMessage(timeSent, user, "PART", "has left", message);
		targetChannel.messages.push(newDisplayMessage);
	}

	handlePRIVMSG(message: IRCMessage) {
		const targetChannel = getOrCreateChannelToStore(message.params[0], this.network)
		if (!targetChannel) {