mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Port the globalChat plugin to the new plugin system. Since there's no config system yet the config options for it is removed.
59 lines
1.2 KiB
JavaScript
59 lines
1.2 KiB
JavaScript
const plugin = require("lib/plugin");
|
|
|
|
|
|
/**
|
|
* Removes gps and train tags from messags
|
|
*/
|
|
function removeTags(content) {
|
|
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
|
|
}
|
|
|
|
/**
|
|
* Escapes a string for inclusion into a lua string
|
|
*/
|
|
function escapeLuaString(content) {
|
|
return content
|
|
.replace(/\\/g, "\\\\")
|
|
.replace(/"/g, '\\"')
|
|
.replace(/'/g, "\\'")
|
|
.replace(/\0/g, "\\0")
|
|
.replace(/\n/g, "\\n")
|
|
.replace(/\r/g, "\\r")
|
|
;
|
|
}
|
|
|
|
class InstancePlugin extends plugin.BaseInstancePlugin {
|
|
constructor(...args) {
|
|
super(...args);
|
|
}
|
|
|
|
async init() {
|
|
// XXX Nothing?
|
|
}
|
|
|
|
async chatEventHandler(message) {
|
|
// TODO check if cross server chat is enabled
|
|
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
|
|
await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`);
|
|
}
|
|
|
|
async onOutput(output) {
|
|
if (output.type === "action" && output.action === "CHAT") {
|
|
this.info.messages.chat.send(this.instance, {
|
|
instance_name: this.instance.name,
|
|
content: output.message,
|
|
});
|
|
}
|
|
}
|
|
|
|
// TODO implement info command in lua?
|
|
}
|
|
|
|
module.exports = {
|
|
InstancePlugin,
|
|
|
|
// For testing only
|
|
_removeTags: removeTags,
|
|
_escapeLuaString: escapeLuaString,
|
|
}
|