diff --git a/src/mami.js/compat.js b/src/mami.js/compat.js index bb1dce5..bf56e1f 100644 --- a/src/mami.js/compat.js +++ b/src/mami.js/compat.js @@ -1,10 +1,8 @@ -#include server.js - -const MamiCompat = (current, path, handler) => { +const MamiCompat = (path, handler) => { if(typeof path !== 'string') throw 'path must be a string'; - if(typeof handler !== 'function') - throw 'handler must be a function'; + if(typeof handler !== 'object') + throw 'handler must be a property definition'; path = path.split('.'); @@ -12,6 +10,7 @@ const MamiCompat = (current, path, handler) => { if(final === undefined) throw 'invalid path'; + let current = window; for(const part of path) { if(!(part in current)) current[part] = {}; @@ -19,15 +18,5 @@ const MamiCompat = (current, path, handler) => { } if(!(final in current)) - current[final] = handler; + Object.defineProperty(current, final, handler); }; - -// Backwards compat for scripts -// Keep in sync with for as long as possible -MamiCompat(Umi, 'Server.SendMessage', text => Umi.Server.sendMessage(text)); -MamiCompat(Umi, 'Protocol.SockChat.Protocol.Instance.SendMessage', text => Umi.Server.sendMessage(text)); -MamiCompat(Umi, 'Protocol.SockLegacy.Protocol.Instance.SendMessage', text => Umi.Server.sendMessage(text)); -MamiCompat(Umi, 'Parser.SockChatBBcode.EmbedStub', () => {}); // intentionally a no-op -MamiCompat(Umi, 'UI.View.SetText', text => console.log(`Umi.UI.View.SetText(text: ${text})`)); -MamiCompat(Umi, 'UI.Menus.Add', (baseId, title, initiallyHidden) => console.log(`Umi.UI.Menus.Add(baseId: ${baseId}, title: ${title}, initiallyHidden: ${initiallyHidden})`)); -MamiCompat(Umi, 'UI.Menus.Get', (baseId, icon) => console.log(`Umi.UI.Menus.Get(baseId: ${baseId}, icon: ${icon})`)); diff --git a/src/mami.js/main.js b/src/mami.js/main.js index a724dcc..bc0e5b5 100644 --- a/src/mami.js/main.js +++ b/src/mami.js/main.js @@ -4,13 +4,14 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; #include channel.js #include channels.js #include common.js +#include compat.js #include context.js #include emotes.js #include message.js #include messages.js #include mszauth.js #include parsing.js -#include server.js +#include sockchat_old.js #include txtrigs.js #include user.js #include users.js @@ -199,10 +200,8 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; onHashChange(); window.addEventListener('keydown', ev => { - if(ev.altKey && ev.shiftKey && (ev.key === 'R' || ev.key === 'r')) { - Umi.Server.close(); + if(ev.altKey && ev.shiftKey && (ev.key === 'R' || ev.key === 'r')) location.hash = 'reset'; - } }); @@ -299,6 +298,11 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; loadingOverlay.setMessage('Building menus...'); + MamiCompat('Umi.Parser.SockChatBBcode.EmbedStub', { value: () => {} }); // intentionally a no-op + MamiCompat('Umi.UI.View.SetText', { value: text => console.log(`Umi.UI.View.SetText(text: ${text})`) }); + MamiCompat('Umi.UI.Menus.Add', { value: (baseId, title, initiallyHidden) => console.log(`Umi.UI.Menus.Add(baseId: ${baseId}, title: ${title}, initiallyHidden: ${initiallyHidden})`) }); + MamiCompat('Umi.UI.Menus.Get', { value: (baseId, icon) => console.log(`Umi.UI.Menus.Get(baseId: ${baseId}, icon: ${icon})`) }); + Umi.UI.Menus.Add('users', 'Users'); Umi.UI.Menus.Add('channels', 'Channels', !settings.get('showChannelList')); Umi.UI.Menus.Add('settings', 'Settings'); @@ -420,7 +424,6 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; click: () => alert(pingToggle.title), }, 'Ready~'); pingToggle.appendChild(pingIndicator.getElement()); - console.log(pingIndicator); if(eeprom !== undefined) { @@ -580,11 +583,20 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; 'joinfail': 'You are banned.', }; + const sockChat = new Umi.Protocol.SockChat.Protocol; + MamiCompat('Umi.Server', { get: () => sockChat, configurable: true }); + let dumpEvents = false; settings.watch('dumpEvents', value => dumpEvents = value); - settings.watch('dumpPackets', value => Umi.Server.setDumpPackets(value)); + settings.watch('dumpPackets', value => sockChat.setDumpPackets(value)); - Umi.Server.watch('conn:init', init => { + Umi.UI.Hooks.SetCallbacks(sockChat.sendMessage, sockChat.switchChannel); + + MamiCompat('Umi.Server.SendMessage', { value: text => sockChat.sendMessage(text), configurable: true }); + MamiCompat('Umi.Protocol.SockChat.Protocol.Instance.SendMessage', { value: text => sockChat.sendMessage(text), configurable: true }); + MamiCompat('Umi.Protocol.SockLegacy.Protocol.Instance.SendMessage', { value: text => sockChat.sendMessage(text), configurable: true }); + + sockChat.watch('conn:init', init => { if(dumpEvents) console.log('conn:init', init); let message = 'Connecting to server...'; @@ -593,15 +605,15 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; getLoadingOverlay('spinner', 'Loading...', message); }); - Umi.Server.watch('conn:ready', ready => { + sockChat.watch('conn:ready', ready => { if(dumpEvents) console.log('conn:ready', ready); getLoadingOverlay('spinner', 'Loading...', 'Authenticating...'); const authInfo = MamiMisuzuAuth.getInfo(); - Umi.Server.sendAuth(authInfo.method, authInfo.token); + sockChat.sendAuth(authInfo.method, authInfo.token); }); - Umi.Server.watch('conn:lost', lost => { + sockChat.watch('conn:lost', lost => { if(dumpEvents) console.log('conn:lost', lost); getLoadingOverlay( @@ -609,20 +621,20 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; wsCloseReasons[`_${lost.code}`] ?? `Something caused an unexpected connection loss. (${lost.code})` ); }); - Umi.Server.watch('conn:error', error => { + sockChat.watch('conn:error', error => { console.error('conn:error', error); }); - Umi.Server.watch('ping:send', send => { + sockChat.watch('ping:send', send => { if(dumpEvents) console.log('ping:send', send); }); - Umi.Server.watch('ping:long', long => { + sockChat.watch('ping:long', long => { if(dumpEvents) console.log('ping:long', long); pingToggle.title = '+2000ms'; pingIndicator.setStrength(0); }); - Umi.Server.watch('ping:recv', recv => { + sockChat.watch('ping:recv', recv => { if(dumpEvents) console.log('ping:recv', recv); let strength = 3; @@ -634,7 +646,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; }); - Umi.Server.watch('session:start', start => { + sockChat.watch('session:start', start => { if(dumpEvents) console.log('session:start', start); const userInfo = new Umi.User(start.user.id, start.user.name, start.user.colour, start.user.permsRaw); @@ -663,7 +675,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; }, })); }); - Umi.Server.watch('session:fail', fail => { + sockChat.watch('session:fail', fail => { if(dumpEvents) console.log('session:fail', fail); if(fail.baka !== undefined) { @@ -679,13 +691,13 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; if(fail.session.needsAuth) setTimeout(() => location.assign(futami.get('login')), 1000); }); - Umi.Server.watch('session:term', term => { + sockChat.watch('session:term', term => { if(dumpEvents) console.log('session:term', term); new MamiForceDisconnectNotice(term.baka).pushOn(views); }); - Umi.Server.watch('user:add', add => { + sockChat.watch('user:add', add => { if(dumpEvents) console.log('user:add', add); if(add.user.self) @@ -705,7 +717,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; } )); }); - Umi.Server.watch('user:remove', remove => { + sockChat.watch('user:remove', remove => { if(dumpEvents) console.log('user:remove', remove); const userInfo = Umi.Users.Get(remove.user.id); @@ -730,7 +742,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; Umi.Users.Remove(userInfo); }); - Umi.Server.watch('user:update', update => { + sockChat.watch('user:update', update => { if(dumpEvents) console.log('user:update', update); const userInfo = Umi.Users.Get(update.user.id); @@ -739,7 +751,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; userInfo.setPermissions(update.user.permsRaw); Umi.Users.Update(userInfo.getId(), userInfo); }); - Umi.Server.watch('user:clear', () => { + sockChat.watch('user:clear', () => { if(dumpEvents) console.log('user:clear'); const self = Umi.User.currentUser; @@ -748,7 +760,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; Umi.Users.Add(self); }); - Umi.Server.watch('chan:add', add => { + sockChat.watch('chan:add', add => { if(dumpEvents) console.log('chan:add', add); Umi.Channels.Add(new Umi.Channel( @@ -757,12 +769,12 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; add.channel.isTemporary, )); }); - Umi.Server.watch('chan:remove', remove => { + sockChat.watch('chan:remove', remove => { if(dumpEvents) console.log('chan:remove', remove); Umi.Channels.Remove(Umi.Channels.Get(remove.channel.name)); }); - Umi.Server.watch('chan:update', update => { + sockChat.watch('chan:update', update => { if(dumpEvents) console.log('chan:update', update); const chanInfo = Umi.Channels.Get(update.channel.previousName); @@ -771,17 +783,17 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; chanInfo.setTemporary(update.channel.isTemporary); Umi.Channels.Update(update.channel.previousName, chanInfo); }); - Umi.Server.watch('chan:clear', () => { + sockChat.watch('chan:clear', () => { if(dumpEvents) console.log('chan:clear'); Umi.Channels.Clear(); }); - Umi.Server.watch('chan:focus', focus => { + sockChat.watch('chan:focus', focus => { if(dumpEvents) console.log('chan:focus', focus); Umi.Channels.Switch(Umi.Channels.Get(focus.channel.name)); }); - Umi.Server.watch('chan:join', join => { + sockChat.watch('chan:join', join => { if(dumpEvents) console.log('chan:join', join); const userInfo = new Umi.User(join.user.id, join.user.name, join.user.colour, join.user.permsRaw); @@ -798,7 +810,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; }, )); }); - Umi.Server.watch('chan:leave', leave => { + sockChat.watch('chan:leave', leave => { if(dumpEvents) console.log('chan:leave', leave); if(leave.user.self) @@ -822,7 +834,7 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; Umi.Users.Remove(userInfo); }); - Umi.Server.watch('msg:add', add => { + sockChat.watch('msg:add', add => { if(dumpEvents) console.log('msg:add', add); const senderInfo = add.msg.sender; @@ -859,21 +871,19 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; add.msg.silent, )); }); - Umi.Server.watch('msg:remove', remove => { + sockChat.watch('msg:remove', remove => { if(dumpEvents) console.log('msg:remove', remove); Umi.Messages.Remove(Umi.Messages.Get(remove.msg.id)); }); - Umi.Server.watch('msg:clear', () => { + sockChat.watch('msg:clear', () => { if(dumpEvents) console.log('msg:clear'); Umi.UI.Messages.RemoveAll(); }); - Umi.Server.open(); + sockChat.open(); if(window.dispatchEvent) window.dispatchEvent(new Event('umi:connect')); })(); - -#include compat.js diff --git a/src/mami.js/server.js b/src/mami.js/server.js deleted file mode 100644 index da2a7fa..0000000 --- a/src/mami.js/server.js +++ /dev/null @@ -1,3 +0,0 @@ -#include sockchat_old.js - -Umi.Server = new Umi.Protocol.SockChat.Protocol; diff --git a/src/mami.js/ui/hooks.js b/src/mami.js/ui/hooks.js index 7dab259..c068e4f 100644 --- a/src/mami.js/ui/hooks.js +++ b/src/mami.js/ui/hooks.js @@ -1,6 +1,4 @@ #include channels.js -#include common.js -#include server.js #include user.js #include utility.js #include sound/umisound.js @@ -10,7 +8,14 @@ #include ui/view.js Umi.UI.Hooks = (function() { + let sendMessage; + let switchChannel; + return { + SetCallbacks: (sendMessageFunc, switchChannelFunc) => { + sendMessage = sendMessageFunc; + switchChannel = switchChannelFunc; + }, AddHooks: function() { Umi.Users.OnAdd.push(function(user) { Umi.UI.Users.Add(user); @@ -47,7 +52,9 @@ Umi.UI.Hooks = (function() { Umi.Channels.OnSwitch.push(function(name, channel) { Umi.UI.Channels.Reload(name === null); - Umi.Server.switchChannel(channel); + + if(typeof switchChannel === 'function') + switchChannel(channel); }); @@ -62,13 +69,16 @@ Umi.UI.Hooks = (function() { $i('umi-msg-form').addEventListener('submit', ev => { ev.preventDefault(); + if(typeof sendMessage !== 'function') + return; + const textField = ev.target.elements.namedItem('text'); if(textField instanceof HTMLTextAreaElement) { let text = textField.value; textField.value = ''; text = text.replace(/\t/g, ' '); - Umi.Server.sendMessage(text); + sendMessage(text); } }); diff --git a/src/mami.js/ui/users.js b/src/mami.js/ui/users.js index 35b49d5..86491dd 100644 --- a/src/mami.js/ui/users.js +++ b/src/mami.js/ui/users.js @@ -114,7 +114,7 @@ Umi.UI.Users = (function() { 'click': function() { if(confirm('You are about to detonate the fucking bomb. Are you sure?')) { const targets = Umi.Users.All(); - for(const target of targets) + for(const target of targets) // this shouldn't call it like this but will have to leave it for now Umi.Server.sendMessage('/kick ' + target.getName()); } }