add personal USB disks

This commit is contained in:
Elijah R 2024-06-23 23:08:01 -04:00
parent 977cf1e837
commit 050b670f77
2 changed files with 40 additions and 3 deletions

View file

@ -6,6 +6,7 @@ export default interface Config {
id: string; id: string;
usesIde2: boolean; usesIde2: boolean;
hasFloppy: boolean; hasFloppy: boolean;
hasUsb: boolean;
}[]; }[];
BOT_PREFIX: string; BOT_PREFIX: string;
ADMIN_TOKEN: string; ADMIN_TOKEN: string;

View file

@ -1,5 +1,6 @@
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import * as child_process from "child_process";
import CollabVMClient from "./client.js"; import CollabVMClient from "./client.js";
import Config from "./config.js"; import Config from "./config.js";
@ -9,6 +10,9 @@ const blankflp = Buffer.alloc(1440000);
if (!fs.existsSync("media/flp/")) if (!fs.existsSync("media/flp/"))
fs.mkdirSync("media/flp/", { recursive: true }); fs.mkdirSync("media/flp/", { recursive: true });
if (!fs.existsSync("media/img/"))
fs.mkdirSync("media/img/", { recursive: true });
function Log(...args: string[]) { function Log(...args: string[]) {
console.log("[AnyOSBot]", args.join(" ")); console.log("[AnyOSBot]", args.join(" "));
} }
@ -101,16 +105,18 @@ class HelperBot extends CollabVMClient {
private _vmId: string; private _vmId: string;
private _ide2: boolean; private _ide2: boolean;
private _hasFloppy: boolean; private _hasFloppy: boolean;
private _hasUsb: boolean;
private GeneralCmdLimit: RateLimit | undefined; private GeneralCmdLimit: RateLimit | undefined;
private RebootLimit: RateLimit | undefined; private RebootLimit: RateLimit | undefined;
constructor(wsUri: string, vmId: string, ide2: boolean, floppy: boolean) { constructor(wsUri: string, vmId: string, ide2: boolean, floppy: boolean, usb: boolean) {
super(); super();
this._wsUri = wsUri; this._wsUri = wsUri;
this._vmId = vmId; this._vmId = vmId;
this._ide2 = ide2; this._ide2 = ide2;
this._hasFloppy = floppy; this._hasFloppy = floppy;
this._hasUsb = usb;
} }
DoConn() { DoConn() {
@ -177,6 +183,7 @@ class HelperBot extends CollabVMClient {
} }
QemuChangeDevice(devname: string, source: string, opts: string) { QemuChangeDevice(devname: string, source: string, opts: string) {
console.log(`change ${devname} "${source}" ${opts}`);
this.SendMonitorCommand(`change ${devname} "${source}" ${opts}`); this.SendMonitorCommand(`change ${devname} "${source}" ${opts}`);
} }
@ -189,6 +196,10 @@ class HelperBot extends CollabVMClient {
if (this._hasFloppy) this.QemuEjectDevice("vm.floppy"); if (this._hasFloppy) this.QemuEjectDevice("vm.floppy");
} }
QemuRemoveUSB() {
if (this._hasUsb) this.QemuEjectDevice("vm.usbstorage");
}
QemuChangeCd(source: string, opts: string) { QemuChangeCd(source: string, opts: string) {
if (this._ide2) this.QemuChangeDevice("ide2-cd0", source, opts); if (this._ide2) this.QemuChangeDevice("ide2-cd0", source, opts);
else this.QemuChangeDevice("vm.cd", source, opts); else this.QemuChangeDevice("vm.cd", source, opts);
@ -201,6 +212,10 @@ class HelperBot extends CollabVMClient {
if (this._hasFloppy) this.QemuChangeDevice("vm.floppy", source, opts); if (this._hasFloppy) this.QemuChangeDevice("vm.floppy", source, opts);
} }
QemuChangeUSB(source: string) {
this.QemuChangeDevice("vm.usbstorage", source, "");
}
OnChat(username: string, message: string) { OnChat(username: string, message: string) {
if (username == this.GetUsername()) return; if (username == this.GetUsername()) return;
@ -304,6 +319,10 @@ class HelperBot extends CollabVMClient {
help: "Insert and/or create your personal floppy disk", help: "Insert and/or create your personal floppy disk",
usesFloppy: true, usesFloppy: true,
}, },
{
command: "myusb",
help: "Insert and/or create your personal 100MB USB Drive",
},
{ {
command: "lilyflp [path]", command: "lilyflp [path]",
help: "Change Floppy image to Lily IMG/flp image", help: "Change Floppy image to Lily IMG/flp image",
@ -314,7 +333,7 @@ class HelperBot extends CollabVMClient {
help: "Change CD image to HTTP server ISO file. Whitelisted domains only (see computernewb.com/CHOCOLATEMAN/domains.txt for a list)", help: "Change CD image to HTTP server ISO file. Whitelisted domains only (see computernewb.com/CHOCOLATEMAN/domains.txt for a list)",
}, },
{ {
command: "eject [cd/flp]", command: "eject [cd/flp/usb]",
help: "Ejects media from the specified drive.", help: "Ejects media from the specified drive.",
}, },
{ {
@ -514,6 +533,9 @@ class HelperBot extends CollabVMClient {
case "flp": case "flp":
this.QemuEjectFloppy(); this.QemuEjectFloppy();
break; break;
case "usb":
this.QemuRemoveUSB();
break;
} }
} }
break; break;
@ -543,6 +565,20 @@ class HelperBot extends CollabVMClient {
this.Chat("Tried to put media into specified device."); this.Chat("Tried to put media into specified device.");
break; break;
} }
case "myusb": {
if (!DoLimit(this.GeneralCmdLimit!)) return;
if (!this._hasUsb) {
this.Chat("This VM does not have a USB drive.");
return;
}
let imgpath = path.resolve(`media/img/${username}.qcow2`);
if (!fs.existsSync(imgpath)) {
child_process.execSync(`qemu-img create -f qcow2 "${imgpath}" 100M`);
}
this.QemuChangeUSB(imgpath);
this.Chat("Tried to put media into specified device.");
break;
}
default: default:
this.Chat(`Unknown command ${command}. See !help?`); this.Chat(`Unknown command ${command}. See !help?`);
break; break;
@ -595,5 +631,5 @@ class HelperBot extends CollabVMClient {
for (let vm of config.INSTALLBOT_VMS) { for (let vm of config.INSTALLBOT_VMS) {
// initalize this bot instance // initalize this bot instance
new HelperBot(vm.uri, vm.id, vm.usesIde2, vm.hasFloppy).DoConn(); new HelperBot(vm.uri, vm.id, vm.usesIde2, vm.hasFloppy, vm.hasUsb).DoConn();
} }