1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00

implemented motd, locklevel and initial channels

Delete public channels from the config.json, then do: npm run config
This commit is contained in:
marzavec 2024-01-06 23:09:28 -08:00
parent 08e5062241
commit bbb5c7eeb3
4 changed files with 72 additions and 21 deletions

View File

@ -15,9 +15,11 @@ import {
import { import {
canJoinChannel, canJoinChannel,
socketInChannel, socketInChannel,
getChannelSettings,
} from '../utility/_Channels.js'; } from '../utility/_Channels.js';
import { import {
Errors, Errors,
SystemMOTDs,
} from '../utility/_Constants.js'; } from '../utility/_Constants.js';
import { import {
upgradeLegacyJoin, upgradeLegacyJoin,
@ -93,6 +95,17 @@ export async function run({
// get trip and level // get trip and level
const { trip, level } = getUserPerms(pass, core.saltKey, core.appConfig.data, channel); const { trip, level } = getUserPerms(pass, core.saltKey, core.appConfig.data, channel);
const channelSettings = getChannelSettings(core.appConfig.data, channel);
if (level < channelSettings.lockLevel) {
return server.reply({
cmd: 'warn',
text: 'You may not join that channel.',
// id: Errors.Join., // @todo Add numeric error code as `id`
channel: false, // @todo Multichannel, false for global event
}, socket);
}
// store the user values // store the user values
const userInfo = { const userInfo = {
nick, nick,
@ -168,6 +181,17 @@ export async function run({
channel, // @todo Multichannel (?) channel, // @todo Multichannel (?)
}, socket); }, socket);
var motd = channelSettings.motd;
if (motd === '') {
motd = SystemMOTDs[Math.floor(Math.random() * SystemMOTDs.length)];
}
server.reply({
cmd: 'info',
text: motd,
channel,
}, socket);
// update client with new session info // update client with new session info
server.reply({ server.reply({
cmd: 'session', cmd: 'session',

View File

@ -104,24 +104,25 @@ export function getChannelSettings(config, channel) {
const channelHash = getChannelHash(channel); const channelHash = getChannelHash(channel);
if (typeof config.permissions[channelHash] === 'undefined') { if (typeof config.permissions[channelHash] === 'undefined') {
const configPath = `../../channels/${channelHash[0]}/${channelHash}.json`; const configPath = `./channels/${channelHash[0]}/${channelHash}.json`;
if (!existsSync(configPath)) { if (!existsSync(configPath)) {
return DefaultChannelSettings; config.permissions[channelHash] = {
...DefaultChannelSettings,
};
} else {
try {
config.permissions[channelHash] = JSON.parse(readFileSync(configPath, 'utf8'));
} catch (e) {
console.log(`Corrupted channel config: ${configPath}`);
config.permissions[channelHash] = {
...DefaultChannelSettings,
};
}
} }
let configData; // @todo Check last access date here, if too old; delete file and use DefaultChannelSettings
try {
configData = JSON.parse(readFileSync(configPath, 'utf8'));
} catch (e) {
console.log(`Corrupted channel config: ${configPath}`);
return DefaultChannelSettings;
}
// Check last access date here, if too old; delete file and return DefaultChannelSettings
config.permissions[channelHash] = configData;
} }
config.permissions[channelHash].lastAccessed = new Date(); config.permissions[channelHash].lastAccessed = new Date();

View File

@ -10,7 +10,7 @@
* Internal version, used mainly for debugging * Internal version, used mainly for debugging
* @typedef {object} CodebaseVersion * @typedef {object} CodebaseVersion
*/ */
export const CodebaseVersion = '2.2.21b'; export const CodebaseVersion = '2.2.23b';
/* Base error ranges */ /* Base error ranges */
const GlobalErrors = 10; const GlobalErrors = 10;
@ -71,8 +71,8 @@ export const DefaultChannelSettings = {
* @typedef {object} SystemMOTDs * @typedef {object} SystemMOTDs
*/ */
export const SystemMOTDs = [ export const SystemMOTDs = [
'Protip: Using any hex color code, you can change your name color- for example: /color #FFFFFF`', 'Protip: Using any hex color code, you can change your name color- for example: /color #FFFFFF',
'Protip: You can easily change your name with a command: /nick bob`', 'Protip: You can easily change your name with a command: /nick bob',
]; ];
/** /**

View File

@ -16,6 +16,12 @@ import {
} from 'lowdb'; } from 'lowdb';
import crypto from 'crypto'; import crypto from 'crypto';
import enquirerPkg from 'enquirer'; import enquirerPkg from 'enquirer';
import {
getChannelHash,
} from '../commands/utility/_Channels.js';
import {
DefaultChannelSettings,
} from '../commands/utility/_Constants.js';
const { const {
Select, Select,
@ -150,8 +156,6 @@ const setupChannels = async () => {
if (mode === standardMode) { if (mode === standardMode) {
config.data.publicChannels = defaultChannels; config.data.publicChannels = defaultChannels;
await config.write();
} else { } else {
const channels = []; const channels = [];
let newChannel = ''; let newChannel = '';
@ -168,9 +172,31 @@ const setupChannels = async () => {
} }
config.data.publicChannels = channels; config.data.publicChannels = channels;
await config.write();
} }
const now = new Date();
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 1825);
const channelSettings = {
...DefaultChannelSettings,
...{
owned: true,
ownerTrip: 'Admin',
lastAccessed: now,
claimExpires: expirationDate,
}
}
for (let i = 0, j = config.data.publicChannels.length; i < j; i += 1) {
const channelHash = getChannelHash(config.data.publicChannels[i]);
const configPath = `./channels/${channelHash[0]}/${channelHash}.json`;
console.log(`Storing ${config.data.publicChannels[i]} -> ${configPath}`);
writeFileSync(configPath, JSON.stringify(channelSettings));
}
await config.write();
}; };
// check if pulic channels have been initialized // check if pulic channels have been initialized