From 5413b918f926a6e21e470b2dc77e01927b9883fc Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 20 Feb 2024 21:33:08 +0000 Subject: [PATCH] Moved chatbot user instance out of the Sock Chat protocol implementation. --- src/mami.js/message.js | 56 ++++++++++++++++++++----------------- src/mami.js/sockchat_old.js | 31 ++++++++++---------- src/mami.js/user.js | 44 ++++++++++++++--------------- 3 files changed, 68 insertions(+), 63 deletions(-) diff --git a/src/mami.js/message.js b/src/mami.js/message.js index 08d9140..63fd743 100644 --- a/src/mami.js/message.js +++ b/src/mami.js/message.js @@ -1,28 +1,34 @@ -Umi.Message = function(msgId, time, user, text, channel, highlight, botInfo, isAction, isLog) { - msgId = (msgId || '').toString(); - time = time === null ? new Date() : new Date(parseInt(time || 0) * 1000); - user = user || {}; - text = (text || '').toString(); - channel = (channel || '').toString(); - highlight = !!highlight; - isAction = !!isAction; - isLog = !!isLog; - hasSeen = isLog; +#include user.js - const msgIdInt = parseInt(msgId); +Umi.Message = (() => { + const chatBot = new Umi.User('-1', 'Server'); - return { - getId: () => msgId, - getIdInt: () => msgIdInt, - getTime: () => time, - getUser: () => user, - getText: () => text, - getChannel: () => channel, - shouldHighlight: () => highlight, - getBotInfo: () => botInfo, - isAction: () => isAction, - isLog: () => isLog, - hasSeen: () => hasSeen, - markSeen: () => hasSeen = true, + return function(msgId, time, user, text, channel, highlight, botInfo, isAction, isLog) { + msgId = (msgId || '').toString(); + time = time === null ? new Date() : new Date(parseInt(time || 0) * 1000); + user = user !== null && typeof user === 'object' ? user : chatBot; + text = (text || '').toString(); + channel = (channel || '').toString(); + highlight = !!highlight; + isAction = !!isAction; + isLog = !!isLog; + hasSeen = isLog; + + const msgIdInt = parseInt(msgId); + + return { + getId: () => msgId, + getIdInt: () => msgIdInt, + getTime: () => time, + getUser: () => user, + getText: () => text, + getChannel: () => channel, + shouldHighlight: () => highlight, + getBotInfo: () => botInfo, + isAction: () => isAction, + isLog: () => isLog, + hasSeen: () => hasSeen, + markSeen: () => hasSeen = true, + }; }; -}; +})(); diff --git a/src/mami.js/sockchat_old.js b/src/mami.js/sockchat_old.js index 0f73d44..8c25618 100644 --- a/src/mami.js/sockchat_old.js +++ b/src/mami.js/sockchat_old.js @@ -19,8 +19,6 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { const pub = {}; - const chatBot = new Umi.User('-1', 'Server'); - let noReconnect = false, connectAttempts = 0, wasKicked = false, @@ -270,20 +268,20 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { } Umi.Messages.Add(new Umi.Message( - data[6], data[1], chatBot, '', channelName, false, + data[6], data[1], undefined, '', channelName, false, { type: 'join', isError: false, args: [juser.getName()], target: juser } )); break; case '2': // message let text = data[3]; - const muser = Umi.Users.Get(data[2]) || chatBot, - textParts = text.split("\f"); - isPM = data[5][4] !== '0', - isAction = data[5][1] !== '0' && data[5][3] === '0', - isBot = data[2] === '-1', - pmChannel = '@' + muser.getName(), - botInfo = {}; + const muser = Umi.Users.Get(data[2]); + const textParts = text.split("\f"); + const isPM = data[5][4] !== '0'; + const isAction = data[5][1] !== '0' && data[5][3] === '0'; + const isBot = data[2] === '-1'; + const botInfo = {}; + let pmChannel = ''; text = unfuckText(text); @@ -296,9 +294,10 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { if(isPM) { if(muser.getId() === userId) { const tmpMsg = text.split(' '); - pmChannel = '@' + tmpMsg.shift(); + pmChannel = `@${tmpMsg.shift()}`; text = tmpMsg.join(' '); - } + } else + pmChannel = `@${muser.getName()}`; if(Umi.Channels.Get(pmChannel) === null) Umi.Channels.Add(new Umi.Channel(pmChannel, false, true, true)); @@ -308,7 +307,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { Umi.Messages.Add(new Umi.Message( data[4], data[1], muser, text, - isPM ? pmChannel : (data[6] || channelName), + isPM ? pmChannel : channelName, false, botInfo, isAction )); break; @@ -317,7 +316,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { const luser = Umi.Users.Get(data[1]); Umi.Messages.Add(new Umi.Message( - data[5], data[4], chatBot, '', channelName, false, + data[5], data[4], undefined, '', channelName, false, { type: data[3], isError: false, args: [luser.getName()], target: luser } )); Umi.Users.Remove(luser); @@ -350,7 +349,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { Umi.Users.Add(umuser); Umi.Messages.Add(new Umi.Message( - data[6], null, chatBot, '', channelName, false, + data[6], null, undefined, '', channelName, false, { type: 'jchan', isError: false, args: [ umuser.getName() ], target: umuser } )); break; @@ -362,7 +361,7 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { const mouser = Umi.Users.Get(+data[2]); Umi.Messages.Add(new Umi.Message( - data[3], null, chatBot, '', channelName, false, + data[3], null, undefined, '', channelName, false, { type: 'lchan', isError: false, args: [ mouser.getName() ], target: mouser } )); Umi.Users.Remove(mouser); diff --git a/src/mami.js/user.js b/src/mami.js/user.js index 091e760..7e912c3 100644 --- a/src/mami.js/user.js +++ b/src/mami.js/user.js @@ -4,12 +4,12 @@ Umi.User = function(userId, userName, userColour, userPerms) { const userIdInt = parseInt(userId); - const setName = function(name) { + const setName = name => { userName = (name || '').toString().replace('<', '<').replace('>', '>'); }; setName(userName); - const setColour = function(colour) { + const setColour = colour => { userColour = (colour || 'inherit').toString(); }; setColour(userColour); @@ -33,34 +33,34 @@ Umi.User = function(userId, userName, userColour, userPerms) { let avatarTime = Date.now(); return { - getId: function() { return userId; }, - getIdInt: function() { return userIdInt; }, + getId: () => userId, + getIdInt: () => userIdInt, - getName: function() { return userName; }, + getName: () => userName, setName: setName, - getColour: function() { return userColour; }, + getColour: () => userColour, setColour: setColour, setPermissions: setPerms, - getRank: function() { return userRank; }, - isCurrentUser: function() { return Umi.User.currentUser && Umi.User.currentUser.userId === userId; }, - canBan: function() { return canBan; }, - canSilence: function() { return canBan; }, - canCreateChannel: function() { return canCreateChannel; }, - canSetNickName: function() { return canSetNickName; }, - getAvatarTime: function() { return avatarTime; }, - bumpAvatarTime: function() { avatarTime = Date.now(); }, + getRank: () => userRank, + isCurrentUser: () => Umi.User.currentUser && Umi.User.currentUser.userId === userId, + canBan: () => canBan, + canSilence: () => canBan, + canCreateChannel: () => canCreateChannel, + canSetNickName: () => canSetNickName, + getAvatarTime: () => avatarTime, + bumpAvatarTime: () => { avatarTime = Date.now(); }, - isBot: function() { return userId === '-1'; }, + isBot: () => userId === '-1', }; }; Umi.User.currentUser = undefined; -Umi.User.hasCurrentUser = function() { return Umi.User.currentUser !== undefined; }; -Umi.User.getCurrentUser = function() { return Umi.User.currentUser; }; -Umi.User.setCurrentUser = function(user) { Umi.User.currentUser = user; }; -Umi.User.isCurrentUser = function(user) { - return Umi.User.currentUser !== undefined - && (Umi.User.currentUser === user || Umi.User.currentUser.getId() === user.getId()); -}; +Umi.User.hasCurrentUser = () => Umi.User.currentUser !== undefined; +Umi.User.getCurrentUser = () => Umi.User.currentUser; +Umi.User.setCurrentUser = user => { Umi.User.currentUser = user; }; +Umi.User.isCurrentUser = user => user !== null && typeof user === 'object' && typeof user.getId === 'function' + && Umi.User.currentUser !== null && typeof Umi.User.currentUser === 'object' + && typeof Umi.User.currentUser.getId === 'function' + && (Umi.User.currentUser === user || Umi.User.currentUser.getId() === user.getId());