Skip to content

Commit

Permalink
Don't strip HTML when not needed
Browse files Browse the repository at this point in the history
Chat messages would always strip HTML, even though it's not needed from non RS clients.

Make /quote not use quotes but a > in the next line instead.

Added /flip, /random, /shrug and /tableflip /table to the action commands.
  • Loading branch information
zapek committed Jan 18, 2025
1 parent a3e465e commit fedae46
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
14 changes: 9 additions & 5 deletions app/src/main/java/io/xeres/app/util/UnHtml.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 by David Gerber - https://zapek.com
* Copyright (c) 2023-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand All @@ -20,6 +20,7 @@
package io.xeres.app.util;

import io.xeres.app.util.markdown.Markdown;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand All @@ -28,8 +29,6 @@
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Safelist;

import java.util.Locale;

import static io.xeres.app.util.markdown.Markdown.HeaderSize.*;
import static org.apache.commons.lang3.StringUtils.isBlank;

Expand All @@ -49,6 +48,11 @@ private UnHtml()

public static String cleanupChat(String text)
{
if (isBlank(text) ||
!(StringUtils.startsWithIgnoreCase(text, "<body>") && StringUtils.endsWithIgnoreCase(text, "</body>")))
{
return text;
}
return Entities.unescape( // &lt; -> <
Jsoup.clean(text, "", Safelist.none() // <span> -> nothing
.addAttributes("img", "src")
Expand All @@ -64,8 +68,8 @@ public static String cleanupMessage(String text)
{
// Only process HTML
if (isBlank(text) ||
(!text.toLowerCase(Locale.ROOT).startsWith("<body>") &&
!text.toLowerCase(Locale.ROOT).startsWith("<html>")))
(!StringUtils.startsWithIgnoreCase(text, "<body>") &&
!StringUtils.startsWithIgnoreCase(text, "<html>")))
{
return text;
}
Expand Down
95 changes: 88 additions & 7 deletions ui/src/main/java/io/xeres/ui/support/chat/ChatCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
* Copyright (c) 2024-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand All @@ -20,12 +20,30 @@
package io.xeres.ui.support.chat;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;

/**
* An utility class to parse outgoing commands.
*/
public final class ChatCommand
{
private static final Logger log = LoggerFactory.getLogger(ChatCommand.class);

private static final Pattern SPACE_PATTERN = Pattern.compile("\\s");

private static final String COMMAND_CODE = "/code ";
private static final String COMMAND_PRE = "/pre ";
private static final String COMMAND_QUOTE = "/quote ";
private static final String COMMAND_FLIP = "/flip";
private static final String COMMAND_RANDOM = "/random";
private static final String COMMAND_SHRUG = "/shrug";
private static final String COMMAND_TABLEFLIP = "/tableflip";
private static final String COMMAND_TABLE = "/table";

private ChatCommand()
{
throw new UnsupportedOperationException("Utility class");
Expand Down Expand Up @@ -55,21 +73,66 @@ public static String parseCommands(String s)

var pre = false;

if (s.startsWith("/code "))
if (s.startsWith(COMMAND_CODE))
{
pre = true;

s = s.substring(6);
s = s.substring(COMMAND_CODE.length());
}
else if (s.startsWith("/pre "))
else if (s.startsWith(COMMAND_PRE))
{
pre = true;

s = s.substring(5);
s = s.substring(COMMAND_PRE.length());
}
else if (s.startsWith(COMMAND_QUOTE))
{
s = "\n> " + s.substring(COMMAND_QUOTE.length());
}
else if (s.startsWith(COMMAND_FLIP))
{
return "🪙 (" + (ThreadLocalRandom.current().nextBoolean() ? "heads" : "tails") + ")";
}
else if (s.startsWith(COMMAND_RANDOM))
{
var min = 1;
var max = 11;

if (s.length() > COMMAND_RANDOM.length() + 1)
{
s = s.substring(COMMAND_RANDOM.length() + 1);

try
{
s = SPACE_PATTERN.matcher(s).replaceAll("");
if (s.contains("-"))
{
min = Integer.parseInt(s.substring(0, s.indexOf("-")));
max = Integer.parseInt(s.substring(s.indexOf("-") + 1));
}
else
{
max = Integer.parseInt(s);
}
}
catch (NumberFormatException | IndexOutOfBoundsException exception)
{
log.error("Couldn't parse /random input: [{}], {}", s, exception.getMessage());
}
}
return "🎲 " + ThreadLocalRandom.current().nextInt(min, max);
}
else if (s.startsWith(COMMAND_SHRUG))
{
return suffixWithSpaceIfNeeded(s.substring(COMMAND_SHRUG.length())) + \\_(ツ)_/¯";
}
else if (s.startsWith(COMMAND_TABLEFLIP))
{
return "(╯°□°)╯︵ ┻━┻" + prefixWithSpaceIfNeeded(s.substring(COMMAND_TABLEFLIP.length()));
}
else if (s.startsWith("/quote "))
else if (s.startsWith(COMMAND_TABLE))
{
s = "\"" + s.substring(7) + "\"";
return "(╯°□°)╯︵ ┻━┻" + prefixWithSpaceIfNeeded(s.substring(COMMAND_TABLE.length()));
}

if (pre)
Expand All @@ -78,4 +141,22 @@ else if (s.startsWith("/quote "))
}
return s;
}

private static String prefixWithSpaceIfNeeded(String s)
{
if (!StringUtils.isBlank(s))
{
return " " + s;
}
return "";
}

private static String suffixWithSpaceIfNeeded(String s)
{
if (!StringUtils.isBlank(s))
{
return s + " ";
}
return "";
}
}

0 comments on commit fedae46

Please sign in to comment.