aboutsummaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorRblSb <msrblsb@gmail.com>2021-08-10 03:22:27 +0300
committerRblSb <msrblsb@gmail.com>2021-08-10 07:56:57 +0300
commitf972d4b7b1c6a69ab20cd2aee5a43df702796075 (patch)
tree608f8031ef7e0885143eaf8670275171112c4e9a /src/client
parent96e10fe71d6428eed4bb2f120bc4b3a2801ff4be (diff)
Ban/unban commands
/ban Name 3d10h20m59s /ban Name 2h30m /unban Name /removeBan Name see #26
Diffstat (limited to 'src/client')
-rw-r--r--src/client/Buttons.hx1
-rw-r--r--src/client/Main.hx85
-rw-r--r--src/client/Utils.hx8
3 files changed, 80 insertions, 14 deletions
diff --git a/src/client/Buttons.hx b/src/client/Buttons.hx
index 86a7d82..aba629e 100644
--- a/src/client/Buttons.hx
+++ b/src/client/Buttons.hx
@@ -351,6 +351,7 @@ class Buttons {
if (Utils.isTouch()) main.scrollChatToEnd();
}
new InputWithHistory(chatline, 50, value -> {
+ if (main.handleCommands(value)) return true;
main.send({
type: Message,
message: {
diff --git a/src/client/Main.hx b/src/client/Main.hx
index c5dca67..57a2d0f 100644
--- a/src/client/Main.hx
+++ b/src/client/Main.hx
@@ -382,8 +382,8 @@ class Main {
case Connected:
onConnected(data);
onTimeGet.run();
- case Disconnected: // server-only
+ case Disconnected: // server-only
case Login:
onLogin(data.login.clients, data.login.clientName);
@@ -411,6 +411,7 @@ class Main {
personal = clients.getByName(personal.name, personal);
if (personal.group.toInt() != oldGroup) onUserGroupChanged();
+ case BanClient: // server-only
case Message:
addMessage(data.message.clientName, data.message.text);
@@ -728,7 +729,8 @@ class Main {
for (client in clients) {
list.add('<div class="userlist_item">');
if (client.isLeader) list.add('<ion-icon name="play"></ion-icon>');
- final klass = client.isAdmin ? "userlist_owner" : "";
+ var klass = client.isBanned ? "userlist_banned" : "";
+ if (client.isAdmin) klass += " userlist_owner";
list.add('<span class="$klass">${client.name}</span></div>');
}
final userlist = ge("#userlist");
@@ -764,12 +766,8 @@ class Main {
textDiv.className = "text";
text = text.htmlEscape();
- if (text.startsWith("/")) {
- if (name == personal.name) handleCommands(text.substr(1));
- } else {
- for (filter in filters) {
- text = filter.regex.replace(text, filter.replace);
- }
+ for (filter in filters) {
+ text = filter.regex.replace(text, filter.replace);
}
textDiv.innerHTML = text;
final isInChatEnd = msgBuf.scrollTop + msgBuf.clientHeight >= msgBuf.scrollHeight - 1;
@@ -828,21 +826,80 @@ class Main {
msgBuf.scrollTop = msgBuf.scrollHeight;
}
- final matchNumbers = ~/^-?[0-9]+$/;
+ /* Returns `true` if text should not be sent to chat */
+ public function handleCommands(command:String):Bool {
+ if (!command.startsWith("/")) return false;
+ final args = command.trim().split(" ");
+ command = args.shift().substr(1);
- function handleCommands(text:String):Void {
- switch (text) {
+ switch (command) {
+ case "ban":
+ final name = args[0];
+ final time = parseSimpleDate(args[1]);
+ if (time < 0) return true;
+ send({
+ type: BanClient,
+ banClient: {
+ name: name,
+ time: time
+ }
+ });
+ return true;
+ case "unban", "removeBan":
+ final name = args[0];
+ send({
+ type: BanClient,
+ banClient: {
+ name: name,
+ time: 0
+ }
+ });
+ return true;
case "clear":
- if (isAdmin()) send({type: ClearChat});
+ send({type: ClearChat});
+ return true;
}
- if (matchNumbers.match(text)) {
+ if (matchSimpleDate.match(command)) {
send({
type: Rewind,
rewind: {
- time: Std.parseInt(text)
+ time: parseSimpleDate(command)
}
});
+ return false;
+ }
+ return false;
+ }
+
+ final matchSimpleDate = ~/^-?([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s?)?$/;
+
+ function parseSimpleDate(text:Null<String>):Int {
+ if (text == null) return 0;
+ if (!matchSimpleDate.match(text)) return 0;
+ final matches:Array<String> = [];
+ final length = Utils.matchedNum(matchSimpleDate);
+ for (i in 1...length) {
+ final group = matchSimpleDate.matched(i);
+ if (group == null) continue;
+ matches.push(group);
+ }
+ var seconds = 0;
+ for (block in matches) {
+ seconds += parseSimpleDateBlock(block);
+ }
+ if (text.startsWith("-")) seconds = -seconds;
+ return seconds;
+ }
+
+ function parseSimpleDateBlock(block:String):Int {
+ inline function time():Int {
+ return Std.parseInt(block.substr(0, block.length - 1));
}
+ if (block.endsWith("s")) return time();
+ else if (block.endsWith("m")) return time() * 60;
+ else if (block.endsWith("h")) return time() * 60 * 60;
+ else if (block.endsWith("d")) return time() * 60 * 60 * 24;
+ return Std.parseInt(block);
}
public function blinkTabWithTitle(title:String):Void {
diff --git a/src/client/Utils.hx b/src/client/Utils.hx
index 232ca02..f23365e 100644
--- a/src/client/Utils.hx
+++ b/src/client/Utils.hx
@@ -89,6 +89,14 @@ class Utils {
}
}
+ public static function matchedNum(ereg:EReg):Int {
+ #if js
+ return (ereg : Dynamic).r.m.length;
+ #else
+ #error "not implemented"
+ #end
+ }
+
public static function browseFileUrl(
onFileLoad:(url:String, name:String) -> Void,
isBinary = true,
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage